rtl838x.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <asm/mach-rtl838x/mach-rtl83xx.h>
  3. #include <net/nexthop.h>
  4. #include "rtl83xx.h"
  5. extern struct mutex smi_lock;
  6. // see_dal_maple_acl_log2PhyTmplteField and src/app/diag_v2/src/diag_acl.c
  7. /* Definition of the RTL838X-specific template field IDs as used in the PIE */
  8. enum template_field_id {
  9. TEMPLATE_FIELD_SPMMASK = 0,
  10. TEMPLATE_FIELD_SPM0 = 1, // Source portmask ports 0-15
  11. TEMPLATE_FIELD_SPM1 = 2, // Source portmask ports 16-28
  12. TEMPLATE_FIELD_RANGE_CHK = 3,
  13. TEMPLATE_FIELD_DMAC0 = 4, // Destination MAC [15:0]
  14. TEMPLATE_FIELD_DMAC1 = 5, // Destination MAC [31:16]
  15. TEMPLATE_FIELD_DMAC2 = 6, // Destination MAC [47:32]
  16. TEMPLATE_FIELD_SMAC0 = 7, // Source MAC [15:0]
  17. TEMPLATE_FIELD_SMAC1 = 8, // Source MAC [31:16]
  18. TEMPLATE_FIELD_SMAC2 = 9, // Source MAC [47:32]
  19. TEMPLATE_FIELD_ETHERTYPE = 10, // Ethernet typ
  20. TEMPLATE_FIELD_OTAG = 11, // Outer VLAN tag
  21. TEMPLATE_FIELD_ITAG = 12, // Inner VLAN tag
  22. TEMPLATE_FIELD_SIP0 = 13, // IPv4 or IPv6 source IP[15:0] or ARP/RARP
  23. // source protocol address in header
  24. TEMPLATE_FIELD_SIP1 = 14, // IPv4 or IPv6 source IP[31:16] or ARP/RARP
  25. TEMPLATE_FIELD_DIP0 = 15, // IPv4 or IPv6 destination IP[15:0]
  26. TEMPLATE_FIELD_DIP1 = 16, // IPv4 or IPv6 destination IP[31:16]
  27. TEMPLATE_FIELD_IP_TOS_PROTO = 17, // IPv4 TOS/IPv6 traffic class and
  28. // IPv4 proto/IPv6 next header fields
  29. TEMPLATE_FIELD_L34_HEADER = 18, // packet with extra tag and IPv6 with auth, dest,
  30. // frag, route, hop-by-hop option header,
  31. // IGMP type, TCP flag
  32. TEMPLATE_FIELD_L4_SPORT = 19, // TCP/UDP source port
  33. TEMPLATE_FIELD_L4_DPORT = 20, // TCP/UDP destination port
  34. TEMPLATE_FIELD_ICMP_IGMP = 21,
  35. TEMPLATE_FIELD_IP_RANGE = 22,
  36. TEMPLATE_FIELD_FIELD_SELECTOR_VALID = 23, // Field selector mask
  37. TEMPLATE_FIELD_FIELD_SELECTOR_0 = 24,
  38. TEMPLATE_FIELD_FIELD_SELECTOR_1 = 25,
  39. TEMPLATE_FIELD_FIELD_SELECTOR_2 = 26,
  40. TEMPLATE_FIELD_FIELD_SELECTOR_3 = 27,
  41. TEMPLATE_FIELD_SIP2 = 28, // IPv6 source IP[47:32]
  42. TEMPLATE_FIELD_SIP3 = 29, // IPv6 source IP[63:48]
  43. TEMPLATE_FIELD_SIP4 = 30, // IPv6 source IP[79:64]
  44. TEMPLATE_FIELD_SIP5 = 31, // IPv6 source IP[95:80]
  45. TEMPLATE_FIELD_SIP6 = 32, // IPv6 source IP[111:96]
  46. TEMPLATE_FIELD_SIP7 = 33, // IPv6 source IP[127:112]
  47. TEMPLATE_FIELD_DIP2 = 34, // IPv6 destination IP[47:32]
  48. TEMPLATE_FIELD_DIP3 = 35, // IPv6 destination IP[63:48]
  49. TEMPLATE_FIELD_DIP4 = 36, // IPv6 destination IP[79:64]
  50. TEMPLATE_FIELD_DIP5 = 37, // IPv6 destination IP[95:80]
  51. TEMPLATE_FIELD_DIP6 = 38, // IPv6 destination IP[111:96]
  52. TEMPLATE_FIELD_DIP7 = 39, // IPv6 destination IP[127:112]
  53. TEMPLATE_FIELD_FWD_VID = 40, // Forwarding VLAN-ID
  54. TEMPLATE_FIELD_FLOW_LABEL = 41,
  55. };
  56. /*
  57. * The RTL838X SoCs use 5 fixed templates with definitions for which data fields are to
  58. * be copied from the Ethernet Frame header into the 12 User-definable fields of the Packet
  59. * Inspection Engine's buffer. The following defines the field contents for each of the fixed
  60. * templates. Additionally, 3 user-definable templates can be set up via the definitions
  61. * in RTL838X_ACL_TMPLTE_CTRL control registers.
  62. * TODO: See all src/app/diag_v2/src/diag_pie.c
  63. */
  64. #define N_FIXED_TEMPLATES 5
  65. static enum template_field_id fixed_templates[N_FIXED_TEMPLATES][N_FIXED_FIELDS] =
  66. {
  67. {
  68. TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1, TEMPLATE_FIELD_OTAG,
  69. TEMPLATE_FIELD_SMAC0, TEMPLATE_FIELD_SMAC1, TEMPLATE_FIELD_SMAC2,
  70. TEMPLATE_FIELD_DMAC0, TEMPLATE_FIELD_DMAC1, TEMPLATE_FIELD_DMAC2,
  71. TEMPLATE_FIELD_ETHERTYPE, TEMPLATE_FIELD_ITAG, TEMPLATE_FIELD_RANGE_CHK
  72. }, {
  73. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_DIP0,
  74. TEMPLATE_FIELD_DIP1,TEMPLATE_FIELD_IP_TOS_PROTO, TEMPLATE_FIELD_L4_SPORT,
  75. TEMPLATE_FIELD_L4_DPORT, TEMPLATE_FIELD_ICMP_IGMP, TEMPLATE_FIELD_ITAG,
  76. TEMPLATE_FIELD_RANGE_CHK, TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1
  77. }, {
  78. TEMPLATE_FIELD_DMAC0, TEMPLATE_FIELD_DMAC1, TEMPLATE_FIELD_DMAC2,
  79. TEMPLATE_FIELD_ITAG, TEMPLATE_FIELD_ETHERTYPE, TEMPLATE_FIELD_IP_TOS_PROTO,
  80. TEMPLATE_FIELD_L4_DPORT, TEMPLATE_FIELD_L4_SPORT, TEMPLATE_FIELD_SIP0,
  81. TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_DIP0, TEMPLATE_FIELD_DIP1
  82. }, {
  83. TEMPLATE_FIELD_DIP0, TEMPLATE_FIELD_DIP1, TEMPLATE_FIELD_DIP2,
  84. TEMPLATE_FIELD_DIP3, TEMPLATE_FIELD_DIP4, TEMPLATE_FIELD_DIP5,
  85. TEMPLATE_FIELD_DIP6, TEMPLATE_FIELD_DIP7, TEMPLATE_FIELD_L4_DPORT,
  86. TEMPLATE_FIELD_L4_SPORT, TEMPLATE_FIELD_ICMP_IGMP, TEMPLATE_FIELD_IP_TOS_PROTO
  87. }, {
  88. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_SIP2,
  89. TEMPLATE_FIELD_SIP3, TEMPLATE_FIELD_SIP4, TEMPLATE_FIELD_SIP5,
  90. TEMPLATE_FIELD_SIP6, TEMPLATE_FIELD_SIP7, TEMPLATE_FIELD_ITAG,
  91. TEMPLATE_FIELD_RANGE_CHK, TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1
  92. },
  93. };
  94. void rtl838x_print_matrix(void)
  95. {
  96. unsigned volatile int *ptr8;
  97. int i;
  98. ptr8 = RTL838X_SW_BASE + RTL838X_PORT_ISO_CTRL(0);
  99. for (i = 0; i < 28; i += 8)
  100. pr_debug("> %8x %8x %8x %8x %8x %8x %8x %8x\n",
  101. ptr8[i + 0], ptr8[i + 1], ptr8[i + 2], ptr8[i + 3],
  102. ptr8[i + 4], ptr8[i + 5], ptr8[i + 6], ptr8[i + 7]);
  103. pr_debug("CPU_PORT> %8x\n", ptr8[28]);
  104. }
  105. static inline int rtl838x_port_iso_ctrl(int p)
  106. {
  107. return RTL838X_PORT_ISO_CTRL(p);
  108. }
  109. static inline void rtl838x_exec_tbl0_cmd(u32 cmd)
  110. {
  111. sw_w32(cmd, RTL838X_TBL_ACCESS_CTRL_0);
  112. do { } while (sw_r32(RTL838X_TBL_ACCESS_CTRL_0) & BIT(15));
  113. }
  114. static inline void rtl838x_exec_tbl1_cmd(u32 cmd)
  115. {
  116. sw_w32(cmd, RTL838X_TBL_ACCESS_CTRL_1);
  117. do { } while (sw_r32(RTL838X_TBL_ACCESS_CTRL_1) & BIT(15));
  118. }
  119. static inline int rtl838x_tbl_access_data_0(int i)
  120. {
  121. return RTL838X_TBL_ACCESS_DATA_0(i);
  122. }
  123. static void rtl838x_vlan_tables_read(u32 vlan, struct rtl838x_vlan_info *info)
  124. {
  125. u32 v;
  126. // Read VLAN table (0) via register 0
  127. struct table_reg *r = rtl_table_get(RTL8380_TBL_0, 0);
  128. rtl_table_read(r, vlan);
  129. info->tagged_ports = sw_r32(rtl_table_data(r, 0));
  130. v = sw_r32(rtl_table_data(r, 1));
  131. pr_debug("VLAN_READ %d: %016llx %08x\n", vlan, info->tagged_ports, v);
  132. rtl_table_release(r);
  133. info->profile_id = v & 0x7;
  134. info->hash_mc_fid = !!(v & 0x8);
  135. info->hash_uc_fid = !!(v & 0x10);
  136. info->fid = (v >> 5) & 0x3f;
  137. // Read UNTAG table (0) via table register 1
  138. r = rtl_table_get(RTL8380_TBL_1, 0);
  139. rtl_table_read(r, vlan);
  140. info->untagged_ports = sw_r32(rtl_table_data(r, 0));
  141. rtl_table_release(r);
  142. }
  143. static void rtl838x_vlan_set_tagged(u32 vlan, struct rtl838x_vlan_info *info)
  144. {
  145. u32 v;
  146. // Access VLAN table (0) via register 0
  147. struct table_reg *r = rtl_table_get(RTL8380_TBL_0, 0);
  148. sw_w32(info->tagged_ports, rtl_table_data(r, 0));
  149. v = info->profile_id;
  150. v |= info->hash_mc_fid ? 0x8 : 0;
  151. v |= info->hash_uc_fid ? 0x10 : 0;
  152. v |= ((u32)info->fid) << 5;
  153. sw_w32(v, rtl_table_data(r, 1));
  154. rtl_table_write(r, vlan);
  155. rtl_table_release(r);
  156. }
  157. static void rtl838x_vlan_set_untagged(u32 vlan, u64 portmask)
  158. {
  159. // Access UNTAG table (0) via register 1
  160. struct table_reg *r = rtl_table_get(RTL8380_TBL_1, 0);
  161. sw_w32(portmask & 0x1fffffff, rtl_table_data(r, 0));
  162. rtl_table_write(r, vlan);
  163. rtl_table_release(r);
  164. }
  165. /* Sets the L2 forwarding to be based on either the inner VLAN tag or the outer
  166. */
  167. static void rtl838x_vlan_fwd_on_inner(int port, bool is_set)
  168. {
  169. if (is_set)
  170. sw_w32_mask(BIT(port), 0, RTL838X_VLAN_PORT_FWD);
  171. else
  172. sw_w32_mask(0, BIT(port), RTL838X_VLAN_PORT_FWD);
  173. }
  174. static u64 rtl838x_l2_hash_seed(u64 mac, u32 vid)
  175. {
  176. return mac << 12 | vid;
  177. }
  178. /*
  179. * Applies the same hash algorithm as the one used currently by the ASIC to the seed
  180. * and returns a key into the L2 hash table
  181. */
  182. static u32 rtl838x_l2_hash_key(struct rtl838x_switch_priv *priv, u64 seed)
  183. {
  184. u32 h1, h2, h3, h;
  185. if (sw_r32(priv->r->l2_ctrl_0) & 1) {
  186. h1 = (seed >> 11) & 0x7ff;
  187. h1 = ((h1 & 0x1f) << 6) | ((h1 >> 5) & 0x3f);
  188. h2 = (seed >> 33) & 0x7ff;
  189. h2 = ((h2 & 0x3f) << 5) | ((h2 >> 6) & 0x1f);
  190. h3 = (seed >> 44) & 0x7ff;
  191. h3 = ((h3 & 0x7f) << 4) | ((h3 >> 7) & 0xf);
  192. h = h1 ^ h2 ^ h3 ^ ((seed >> 55) & 0x1ff);
  193. h ^= ((seed >> 22) & 0x7ff) ^ (seed & 0x7ff);
  194. } else {
  195. h = ((seed >> 55) & 0x1ff) ^ ((seed >> 44) & 0x7ff)
  196. ^ ((seed >> 33) & 0x7ff) ^ ((seed >> 22) & 0x7ff)
  197. ^ ((seed >> 11) & 0x7ff) ^ (seed & 0x7ff);
  198. }
  199. return h;
  200. }
  201. static inline int rtl838x_mac_force_mode_ctrl(int p)
  202. {
  203. return RTL838X_MAC_FORCE_MODE_CTRL + (p << 2);
  204. }
  205. static inline int rtl838x_mac_port_ctrl(int p)
  206. {
  207. return RTL838X_MAC_PORT_CTRL(p);
  208. }
  209. static inline int rtl838x_l2_port_new_salrn(int p)
  210. {
  211. return RTL838X_L2_PORT_NEW_SALRN(p);
  212. }
  213. static inline int rtl838x_l2_port_new_sa_fwd(int p)
  214. {
  215. return RTL838X_L2_PORT_NEW_SA_FWD(p);
  216. }
  217. static inline int rtl838x_mac_link_spd_sts(int p)
  218. {
  219. return RTL838X_MAC_LINK_SPD_STS(p);
  220. }
  221. inline static int rtl838x_trk_mbr_ctr(int group)
  222. {
  223. return RTL838X_TRK_MBR_CTR + (group << 2);
  224. }
  225. /*
  226. * Fills an L2 entry structure from the SoC registers
  227. */
  228. static void rtl838x_fill_l2_entry(u32 r[], struct rtl838x_l2_entry *e)
  229. {
  230. /* Table contains different entry types, we need to identify the right one:
  231. * Check for MC entries, first
  232. * In contrast to the RTL93xx SoCs, there is no valid bit, use heuristics to
  233. * identify valid entries
  234. */
  235. e->is_ip_mc = !!(r[0] & BIT(22));
  236. e->is_ipv6_mc = !!(r[0] & BIT(21));
  237. e->type = L2_INVALID;
  238. if (!e->is_ip_mc && !e->is_ipv6_mc) {
  239. e->mac[0] = (r[1] >> 20);
  240. e->mac[1] = (r[1] >> 12);
  241. e->mac[2] = (r[1] >> 4);
  242. e->mac[3] = (r[1] & 0xf) << 4 | (r[2] >> 28);
  243. e->mac[4] = (r[2] >> 20);
  244. e->mac[5] = (r[2] >> 12);
  245. e->rvid = r[2] & 0xfff;
  246. e->vid = r[0] & 0xfff;
  247. /* Is it a unicast entry? check multicast bit */
  248. if (!(e->mac[0] & 1)) {
  249. e->is_static = !!((r[0] >> 19) & 1);
  250. e->port = (r[0] >> 12) & 0x1f;
  251. e->block_da = !!(r[1] & BIT(30));
  252. e->block_sa = !!(r[1] & BIT(31));
  253. e->suspended = !!(r[1] & BIT(29));
  254. e->next_hop = !!(r[1] & BIT(28));
  255. if (e->next_hop) {
  256. pr_debug("Found next hop entry, need to read extra data\n");
  257. e->nh_vlan_target = !!(r[0] & BIT(9));
  258. e->nh_route_id = r[0] & 0x1ff;
  259. e->vid = e->rvid;
  260. }
  261. e->age = (r[0] >> 17) & 0x3;
  262. e->valid = true;
  263. /* A valid entry has one of mutli-cast, aging, sa/da-blocking,
  264. * next-hop or static entry bit set */
  265. if (!(r[0] & 0x007c0000) && !(r[1] & 0xd0000000))
  266. e->valid = false;
  267. else
  268. e->type = L2_UNICAST;
  269. } else { // L2 multicast
  270. pr_debug("Got L2 MC entry: %08x %08x %08x\n", r[0], r[1], r[2]);
  271. e->valid = true;
  272. e->type = L2_MULTICAST;
  273. e->mc_portmask_index = (r[0] >> 12) & 0x1ff;
  274. }
  275. } else { // IPv4 and IPv6 multicast
  276. e->valid = true;
  277. e->mc_portmask_index = (r[0] >> 12) & 0x1ff;
  278. e->mc_gip = (r[1] << 20) | (r[2] >> 12);
  279. e->rvid = r[2] & 0xfff;
  280. }
  281. if (e->is_ip_mc)
  282. e->type = IP4_MULTICAST;
  283. if (e->is_ipv6_mc)
  284. e->type = IP6_MULTICAST;
  285. }
  286. /*
  287. * Fills the 3 SoC table registers r[] with the information of in the rtl838x_l2_entry
  288. */
  289. static void rtl838x_fill_l2_row(u32 r[], struct rtl838x_l2_entry *e)
  290. {
  291. u64 mac = ether_addr_to_u64(e->mac);
  292. if (!e->valid) {
  293. r[0] = r[1] = r[2] = 0;
  294. return;
  295. }
  296. r[0] = e->is_ip_mc ? BIT(22) : 0;
  297. r[0] |= e->is_ipv6_mc ? BIT(21) : 0;
  298. if (!e->is_ip_mc && !e->is_ipv6_mc) {
  299. r[1] = mac >> 20;
  300. r[2] = (mac & 0xfffff) << 12;
  301. /* Is it a unicast entry? check multicast bit */
  302. if (!(e->mac[0] & 1)) {
  303. r[0] |= e->is_static ? BIT(19) : 0;
  304. r[0] |= (e->port & 0x3f) << 12;
  305. r[0] |= e->vid;
  306. r[1] |= e->block_da ? BIT(30) : 0;
  307. r[1] |= e->block_sa ? BIT(31) : 0;
  308. r[1] |= e->suspended ? BIT(29) : 0;
  309. r[2] |= e->rvid & 0xfff;
  310. if (e->next_hop) {
  311. r[1] |= BIT(28);
  312. r[0] |= e->nh_vlan_target ? BIT(9) : 0;
  313. r[0] |= e->nh_route_id & 0x1ff;
  314. }
  315. r[0] |= (e->age & 0x3) << 17;
  316. } else { // L2 Multicast
  317. r[0] |= (e->mc_portmask_index & 0x1ff) << 12;
  318. r[2] |= e->rvid & 0xfff;
  319. r[0] |= e->vid & 0xfff;
  320. pr_debug("FILL MC: %08x %08x %08x\n", r[0], r[1], r[2]);
  321. }
  322. } else { // IPv4 and IPv6 multicast
  323. r[0] |= (e->mc_portmask_index & 0x1ff) << 12;
  324. r[1] = e->mc_gip >> 20;
  325. r[2] = e->mc_gip << 12;
  326. r[2] |= e->rvid;
  327. }
  328. }
  329. /*
  330. * Read an L2 UC or MC entry out of a hash bucket of the L2 forwarding table
  331. * hash is the id of the bucket and pos is the position of the entry in that bucket
  332. * The data read from the SoC is filled into rtl838x_l2_entry
  333. */
  334. static u64 rtl838x_read_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  335. {
  336. u64 entry;
  337. u32 r[3];
  338. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 0); // Access L2 Table 0
  339. u32 idx = (0 << 14) | (hash << 2) | pos; // Search SRAM, with hash and at pos in bucket
  340. int i;
  341. rtl_table_read(q, idx);
  342. for (i= 0; i < 3; i++)
  343. r[i] = sw_r32(rtl_table_data(q, i));
  344. rtl_table_release(q);
  345. rtl838x_fill_l2_entry(r, e);
  346. if (!e->valid)
  347. return 0;
  348. entry = (((u64) r[1]) << 32) | (r[2]); // mac and vid concatenated as hash seed
  349. return entry;
  350. }
  351. static void rtl838x_write_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  352. {
  353. u32 r[3];
  354. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 0);
  355. int i;
  356. u32 idx = (0 << 14) | (hash << 2) | pos; // Access SRAM, with hash and at pos in bucket
  357. rtl838x_fill_l2_row(r, e);
  358. for (i= 0; i < 3; i++)
  359. sw_w32(r[i], rtl_table_data(q, i));
  360. rtl_table_write(q, idx);
  361. rtl_table_release(q);
  362. }
  363. static u64 rtl838x_read_cam(int idx, struct rtl838x_l2_entry *e)
  364. {
  365. u64 entry;
  366. u32 r[3];
  367. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 1); // Access L2 Table 1
  368. int i;
  369. rtl_table_read(q, idx);
  370. for (i= 0; i < 3; i++)
  371. r[i] = sw_r32(rtl_table_data(q, i));
  372. rtl_table_release(q);
  373. rtl838x_fill_l2_entry(r, e);
  374. if (!e->valid)
  375. return 0;
  376. pr_debug("Found in CAM: R1 %x R2 %x R3 %x\n", r[0], r[1], r[2]);
  377. // Return MAC with concatenated VID ac concatenated ID
  378. entry = (((u64) r[1]) << 32) | r[2];
  379. return entry;
  380. }
  381. static void rtl838x_write_cam(int idx, struct rtl838x_l2_entry *e)
  382. {
  383. u32 r[3];
  384. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 1); // Access L2 Table 1
  385. int i;
  386. rtl838x_fill_l2_row(r, e);
  387. for (i= 0; i < 3; i++)
  388. sw_w32(r[i], rtl_table_data(q, i));
  389. rtl_table_write(q, idx);
  390. rtl_table_release(q);
  391. }
  392. static u64 rtl838x_read_mcast_pmask(int idx)
  393. {
  394. u32 portmask;
  395. // Read MC_PMSK (2) via register RTL8380_TBL_L2
  396. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 2);
  397. rtl_table_read(q, idx);
  398. portmask = sw_r32(rtl_table_data(q, 0));
  399. rtl_table_release(q);
  400. return portmask;
  401. }
  402. static void rtl838x_write_mcast_pmask(int idx, u64 portmask)
  403. {
  404. // Access MC_PMSK (2) via register RTL8380_TBL_L2
  405. struct table_reg *q = rtl_table_get(RTL8380_TBL_L2, 2);
  406. sw_w32(((u32)portmask) & 0x1fffffff, rtl_table_data(q, 0));
  407. rtl_table_write(q, idx);
  408. rtl_table_release(q);
  409. }
  410. static void rtl838x_vlan_profile_setup(int profile)
  411. {
  412. u32 pmask_id = UNKNOWN_MC_PMASK;
  413. // Enable L2 Learning BIT 0, portmask UNKNOWN_MC_PMASK for unknown MC traffic flooding
  414. u32 p = 1 | pmask_id << 1 | pmask_id << 10 | pmask_id << 19;
  415. sw_w32(p, RTL838X_VLAN_PROFILE(profile));
  416. /* RTL8380 and RTL8390 use an index into the portmask table to set the
  417. * unknown multicast portmask, setup a default at a safe location
  418. * On RTL93XX, the portmask is directly set in the profile,
  419. * see e.g. rtl9300_vlan_profile_setup
  420. */
  421. rtl838x_write_mcast_pmask(UNKNOWN_MC_PMASK, 0x1fffffff);
  422. }
  423. static void rtl838x_l2_learning_setup(void)
  424. {
  425. /* Set portmask for broadcast traffic and unknown unicast address flooding
  426. * to the reserved entry in the portmask table used also for
  427. * multicast flooding */
  428. sw_w32(UNKNOWN_MC_PMASK << 12 | UNKNOWN_MC_PMASK, RTL838X_L2_FLD_PMSK);
  429. /* Enable learning constraint system-wide (bit 0), per-port (bit 1)
  430. * and per vlan (bit 2) */
  431. sw_w32(0x7, RTL838X_L2_LRN_CONSTRT_EN);
  432. // Limit learning to maximum: 16k entries, after that just flood (bits 0-1)
  433. sw_w32((0x3fff << 2) | 0, RTL838X_L2_LRN_CONSTRT);
  434. // Do not trap ARP packets to CPU_PORT
  435. sw_w32(0, RTL838X_SPCL_TRAP_ARP_CTRL);
  436. }
  437. static void rtl838x_enable_learning(int port, bool enable)
  438. {
  439. // Limit learning to maximum: 32k entries, after that just flood (bits 0-1)
  440. if (enable) {
  441. // flood after 32k entries
  442. sw_w32((0x3fff << 2) | 0, RTL838X_L2_PORT_LRN_CONSTRT + (port << 2));
  443. } else {
  444. // just forward
  445. sw_w32(0, RTL838X_L2_PORT_LRN_CONSTRT + (port << 2));
  446. }
  447. }
  448. static void rtl838x_enable_flood(int port, bool enable)
  449. {
  450. u32 flood_mask = sw_r32(RTL838X_L2_PORT_LRN_CONSTRT + (port << 2));
  451. if (enable) {
  452. // flood
  453. flood_mask &= ~3;
  454. flood_mask |= 0;
  455. sw_w32(flood_mask, RTL838X_L2_PORT_LRN_CONSTRT + (port << 2));
  456. } else {
  457. // drop (bit 1)
  458. flood_mask &= ~3;
  459. flood_mask |= 1;
  460. sw_w32(flood_mask, RTL838X_L2_PORT_LRN_CONSTRT + (port << 2));
  461. }
  462. }
  463. static void rtl838x_enable_mcast_flood(int port, bool enable)
  464. {
  465. }
  466. static void rtl838x_enable_bcast_flood(int port, bool enable)
  467. {
  468. }
  469. static void rtl838x_stp_get(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  470. {
  471. int i;
  472. u32 cmd = 1 << 15 /* Execute cmd */
  473. | 1 << 14 /* Read */
  474. | 2 << 12 /* Table type 0b10 */
  475. | (msti & 0xfff);
  476. priv->r->exec_tbl0_cmd(cmd);
  477. for (i = 0; i < 2; i++)
  478. port_state[i] = sw_r32(priv->r->tbl_access_data_0(i));
  479. }
  480. static void rtl838x_stp_set(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  481. {
  482. int i;
  483. u32 cmd = 1 << 15 /* Execute cmd */
  484. | 0 << 14 /* Write */
  485. | 2 << 12 /* Table type 0b10 */
  486. | (msti & 0xfff);
  487. for (i = 0; i < 2; i++)
  488. sw_w32(port_state[i], priv->r->tbl_access_data_0(i));
  489. priv->r->exec_tbl0_cmd(cmd);
  490. }
  491. u64 rtl838x_traffic_get(int source)
  492. {
  493. return rtl838x_get_port_reg(rtl838x_port_iso_ctrl(source));
  494. }
  495. void rtl838x_traffic_set(int source, u64 dest_matrix)
  496. {
  497. rtl838x_set_port_reg(dest_matrix, rtl838x_port_iso_ctrl(source));
  498. }
  499. void rtl838x_traffic_enable(int source, int dest)
  500. {
  501. rtl838x_mask_port_reg(0, BIT(dest), rtl838x_port_iso_ctrl(source));
  502. }
  503. void rtl838x_traffic_disable(int source, int dest)
  504. {
  505. rtl838x_mask_port_reg(BIT(dest), 0, rtl838x_port_iso_ctrl(source));
  506. }
  507. /*
  508. * Enables or disables the EEE/EEEP capability of a port
  509. */
  510. static void rtl838x_port_eee_set(struct rtl838x_switch_priv *priv, int port, bool enable)
  511. {
  512. u32 v;
  513. // This works only for Ethernet ports, and on the RTL838X, ports from 24 are SFP
  514. if (port >= 24)
  515. return;
  516. pr_debug("In %s: setting port %d to %d\n", __func__, port, enable);
  517. v = enable ? 0x3 : 0x0;
  518. // Set EEE state for 100 (bit 9) & 1000MBit (bit 10)
  519. sw_w32_mask(0x3 << 9, v << 9, priv->r->mac_force_mode_ctrl(port));
  520. // Set TX/RX EEE state
  521. if (enable) {
  522. sw_w32_mask(0, BIT(port), RTL838X_EEE_PORT_TX_EN);
  523. sw_w32_mask(0, BIT(port), RTL838X_EEE_PORT_RX_EN);
  524. } else {
  525. sw_w32_mask(BIT(port), 0, RTL838X_EEE_PORT_TX_EN);
  526. sw_w32_mask(BIT(port), 0, RTL838X_EEE_PORT_RX_EN);
  527. }
  528. priv->ports[port].eee_enabled = enable;
  529. }
  530. /*
  531. * Get EEE own capabilities and negotiation result
  532. */
  533. static int rtl838x_eee_port_ability(struct rtl838x_switch_priv *priv,
  534. struct ethtool_eee *e, int port)
  535. {
  536. u64 link;
  537. if (port >= 24)
  538. return 0;
  539. link = rtl839x_get_port_reg_le(RTL838X_MAC_LINK_STS);
  540. if (!(link & BIT(port)))
  541. return 0;
  542. if (sw_r32(rtl838x_mac_force_mode_ctrl(port)) & BIT(9))
  543. e->advertised |= ADVERTISED_100baseT_Full;
  544. if (sw_r32(rtl838x_mac_force_mode_ctrl(port)) & BIT(10))
  545. e->advertised |= ADVERTISED_1000baseT_Full;
  546. if (sw_r32(RTL838X_MAC_EEE_ABLTY) & BIT(port)) {
  547. e->lp_advertised = ADVERTISED_100baseT_Full;
  548. e->lp_advertised |= ADVERTISED_1000baseT_Full;
  549. return 1;
  550. }
  551. return 0;
  552. }
  553. static void rtl838x_init_eee(struct rtl838x_switch_priv *priv, bool enable)
  554. {
  555. int i;
  556. pr_info("Setting up EEE, state: %d\n", enable);
  557. sw_w32_mask(0x4, 0, RTL838X_SMI_GLB_CTRL);
  558. /* Set timers for EEE */
  559. sw_w32(0x5001411, RTL838X_EEE_TX_TIMER_GIGA_CTRL);
  560. sw_w32(0x5001417, RTL838X_EEE_TX_TIMER_GELITE_CTRL);
  561. // Enable EEE MAC support on ports
  562. for (i = 0; i < priv->cpu_port; i++) {
  563. if (priv->ports[i].phy)
  564. rtl838x_port_eee_set(priv, i, enable);
  565. }
  566. priv->eee_enabled = enable;
  567. }
  568. static void rtl838x_pie_lookup_enable(struct rtl838x_switch_priv *priv, int index)
  569. {
  570. int block = index / PIE_BLOCK_SIZE;
  571. u32 block_state = sw_r32(RTL838X_ACL_BLK_LOOKUP_CTRL);
  572. // Make sure rule-lookup is enabled in the block
  573. if (!(block_state & BIT(block)))
  574. sw_w32(block_state | BIT(block), RTL838X_ACL_BLK_LOOKUP_CTRL);
  575. }
  576. static void rtl838x_pie_rule_del(struct rtl838x_switch_priv *priv, int index_from, int index_to)
  577. {
  578. int block_from = index_from / PIE_BLOCK_SIZE;
  579. int block_to = index_to / PIE_BLOCK_SIZE;
  580. u32 v = (index_from << 1)| (index_to << 12 ) | BIT(0);
  581. int block;
  582. u32 block_state;
  583. pr_debug("%s: from %d to %d\n", __func__, index_from, index_to);
  584. mutex_lock(&priv->reg_mutex);
  585. // Remember currently active blocks
  586. block_state = sw_r32(RTL838X_ACL_BLK_LOOKUP_CTRL);
  587. // Make sure rule-lookup is disabled in the relevant blocks
  588. for (block = block_from; block <= block_to; block++) {
  589. if (block_state & BIT(block))
  590. sw_w32(block_state & (~BIT(block)), RTL838X_ACL_BLK_LOOKUP_CTRL);
  591. }
  592. // Write from-to and execute bit into control register
  593. sw_w32(v, RTL838X_ACL_CLR_CTRL);
  594. // Wait until command has completed
  595. do {
  596. } while (sw_r32(RTL838X_ACL_CLR_CTRL) & BIT(0));
  597. // Re-enable rule lookup
  598. for (block = block_from; block <= block_to; block++) {
  599. if (!(block_state & BIT(block)))
  600. sw_w32(block_state | BIT(block), RTL838X_ACL_BLK_LOOKUP_CTRL);
  601. }
  602. mutex_unlock(&priv->reg_mutex);
  603. }
  604. /*
  605. * Reads the intermediate representation of the templated match-fields of the
  606. * PIE rule in the pie_rule structure and fills in the raw data fields in the
  607. * raw register space r[].
  608. * The register space configuration size is identical for the RTL8380/90 and RTL9300,
  609. * however the RTL9310 has 2 more registers / fields and the physical field-ids
  610. * are specific to every platform.
  611. */
  612. static void rtl838x_write_pie_templated(u32 r[], struct pie_rule *pr, enum template_field_id t[])
  613. {
  614. int i;
  615. enum template_field_id field_type;
  616. u16 data, data_m;
  617. for (i = 0; i < N_FIXED_FIELDS; i++) {
  618. field_type = t[i];
  619. data = data_m = 0;
  620. switch (field_type) {
  621. case TEMPLATE_FIELD_SPM0:
  622. data = pr->spm;
  623. data_m = pr->spm_m;
  624. break;
  625. case TEMPLATE_FIELD_SPM1:
  626. data = pr->spm >> 16;
  627. data_m = pr->spm_m >> 16;
  628. break;
  629. case TEMPLATE_FIELD_OTAG:
  630. data = pr->otag;
  631. data_m = pr->otag_m;
  632. break;
  633. case TEMPLATE_FIELD_SMAC0:
  634. data = pr->smac[4];
  635. data = (data << 8) | pr->smac[5];
  636. data_m = pr->smac_m[4];
  637. data_m = (data_m << 8) | pr->smac_m[5];
  638. break;
  639. case TEMPLATE_FIELD_SMAC1:
  640. data = pr->smac[2];
  641. data = (data << 8) | pr->smac[3];
  642. data_m = pr->smac_m[2];
  643. data_m = (data_m << 8) | pr->smac_m[3];
  644. break;
  645. case TEMPLATE_FIELD_SMAC2:
  646. data = pr->smac[0];
  647. data = (data << 8) | pr->smac[1];
  648. data_m = pr->smac_m[0];
  649. data_m = (data_m << 8) | pr->smac_m[1];
  650. break;
  651. case TEMPLATE_FIELD_DMAC0:
  652. data = pr->dmac[4];
  653. data = (data << 8) | pr->dmac[5];
  654. data_m = pr->dmac_m[4];
  655. data_m = (data_m << 8) | pr->dmac_m[5];
  656. break;
  657. case TEMPLATE_FIELD_DMAC1:
  658. data = pr->dmac[2];
  659. data = (data << 8) | pr->dmac[3];
  660. data_m = pr->dmac_m[2];
  661. data_m = (data_m << 8) | pr->dmac_m[3];
  662. break;
  663. case TEMPLATE_FIELD_DMAC2:
  664. data = pr->dmac[0];
  665. data = (data << 8) | pr->dmac[1];
  666. data_m = pr->dmac_m[0];
  667. data_m = (data_m << 8) | pr->dmac_m[1];
  668. break;
  669. case TEMPLATE_FIELD_ETHERTYPE:
  670. data = pr->ethertype;
  671. data_m = pr->ethertype_m;
  672. break;
  673. case TEMPLATE_FIELD_ITAG:
  674. data = pr->itag;
  675. data_m = pr->itag_m;
  676. break;
  677. case TEMPLATE_FIELD_RANGE_CHK:
  678. data = pr->field_range_check;
  679. data_m = pr->field_range_check_m;
  680. break;
  681. case TEMPLATE_FIELD_SIP0:
  682. if (pr->is_ipv6) {
  683. data = pr->sip6.s6_addr16[7];
  684. data_m = pr->sip6_m.s6_addr16[7];
  685. } else {
  686. data = pr->sip;
  687. data_m = pr->sip_m;
  688. }
  689. break;
  690. case TEMPLATE_FIELD_SIP1:
  691. if (pr->is_ipv6) {
  692. data = pr->sip6.s6_addr16[6];
  693. data_m = pr->sip6_m.s6_addr16[6];
  694. } else {
  695. data = pr->sip >> 16;
  696. data_m = pr->sip_m >> 16;
  697. }
  698. break;
  699. case TEMPLATE_FIELD_SIP2:
  700. case TEMPLATE_FIELD_SIP3:
  701. case TEMPLATE_FIELD_SIP4:
  702. case TEMPLATE_FIELD_SIP5:
  703. case TEMPLATE_FIELD_SIP6:
  704. case TEMPLATE_FIELD_SIP7:
  705. data = pr->sip6.s6_addr16[5 - (field_type - TEMPLATE_FIELD_SIP2)];
  706. data_m = pr->sip6_m.s6_addr16[5 - (field_type - TEMPLATE_FIELD_SIP2)];
  707. break;
  708. case TEMPLATE_FIELD_DIP0:
  709. if (pr->is_ipv6) {
  710. data = pr->dip6.s6_addr16[7];
  711. data_m = pr->dip6_m.s6_addr16[7];
  712. } else {
  713. data = pr->dip;
  714. data_m = pr->dip_m;
  715. }
  716. break;
  717. case TEMPLATE_FIELD_DIP1:
  718. if (pr->is_ipv6) {
  719. data = pr->dip6.s6_addr16[6];
  720. data_m = pr->dip6_m.s6_addr16[6];
  721. } else {
  722. data = pr->dip >> 16;
  723. data_m = pr->dip_m >> 16;
  724. }
  725. break;
  726. case TEMPLATE_FIELD_DIP2:
  727. case TEMPLATE_FIELD_DIP3:
  728. case TEMPLATE_FIELD_DIP4:
  729. case TEMPLATE_FIELD_DIP5:
  730. case TEMPLATE_FIELD_DIP6:
  731. case TEMPLATE_FIELD_DIP7:
  732. data = pr->dip6.s6_addr16[5 - (field_type - TEMPLATE_FIELD_DIP2)];
  733. data_m = pr->dip6_m.s6_addr16[5 - (field_type - TEMPLATE_FIELD_DIP2)];
  734. break;
  735. case TEMPLATE_FIELD_IP_TOS_PROTO:
  736. data = pr->tos_proto;
  737. data_m = pr->tos_proto_m;
  738. break;
  739. case TEMPLATE_FIELD_L4_SPORT:
  740. data = pr->sport;
  741. data_m = pr->sport_m;
  742. break;
  743. case TEMPLATE_FIELD_L4_DPORT:
  744. data = pr->dport;
  745. data_m = pr->dport_m;
  746. break;
  747. case TEMPLATE_FIELD_ICMP_IGMP:
  748. data = pr->icmp_igmp;
  749. data_m = pr->icmp_igmp_m;
  750. break;
  751. default:
  752. pr_info("%s: unknown field %d\n", __func__, field_type);
  753. continue;
  754. }
  755. if (!(i % 2)) {
  756. r[5 - i / 2] = data;
  757. r[12 - i / 2] = data_m;
  758. } else {
  759. r[5 - i / 2] |= ((u32)data) << 16;
  760. r[12 - i / 2] |= ((u32)data_m) << 16;
  761. }
  762. }
  763. }
  764. /*
  765. * Creates the intermediate representation of the templated match-fields of the
  766. * PIE rule in the pie_rule structure by reading the raw data fields in the
  767. * raw register space r[].
  768. * The register space configuration size is identical for the RTL8380/90 and RTL9300,
  769. * however the RTL9310 has 2 more registers / fields and the physical field-ids
  770. */
  771. static void rtl838x_read_pie_templated(u32 r[], struct pie_rule *pr, enum template_field_id t[])
  772. {
  773. int i;
  774. enum template_field_id field_type;
  775. u16 data, data_m;
  776. for (i = 0; i < N_FIXED_FIELDS; i++) {
  777. field_type = t[i];
  778. if (!(i % 2)) {
  779. data = r[5 - i / 2];
  780. data_m = r[12 - i / 2];
  781. } else {
  782. data = r[5 - i / 2] >> 16;
  783. data_m = r[12 - i / 2] >> 16;
  784. }
  785. switch (field_type) {
  786. case TEMPLATE_FIELD_SPM0:
  787. pr->spm = (pr->spn << 16) | data;
  788. pr->spm_m = (pr->spn << 16) | data_m;
  789. break;
  790. case TEMPLATE_FIELD_SPM1:
  791. pr->spm = data;
  792. pr->spm_m = data_m;
  793. break;
  794. case TEMPLATE_FIELD_OTAG:
  795. pr->otag = data;
  796. pr->otag_m = data_m;
  797. break;
  798. case TEMPLATE_FIELD_SMAC0:
  799. pr->smac[4] = data >> 8;
  800. pr->smac[5] = data;
  801. pr->smac_m[4] = data >> 8;
  802. pr->smac_m[5] = data;
  803. break;
  804. case TEMPLATE_FIELD_SMAC1:
  805. pr->smac[2] = data >> 8;
  806. pr->smac[3] = data;
  807. pr->smac_m[2] = data >> 8;
  808. pr->smac_m[3] = data;
  809. break;
  810. case TEMPLATE_FIELD_SMAC2:
  811. pr->smac[0] = data >> 8;
  812. pr->smac[1] = data;
  813. pr->smac_m[0] = data >> 8;
  814. pr->smac_m[1] = data;
  815. break;
  816. case TEMPLATE_FIELD_DMAC0:
  817. pr->dmac[4] = data >> 8;
  818. pr->dmac[5] = data;
  819. pr->dmac_m[4] = data >> 8;
  820. pr->dmac_m[5] = data;
  821. break;
  822. case TEMPLATE_FIELD_DMAC1:
  823. pr->dmac[2] = data >> 8;
  824. pr->dmac[3] = data;
  825. pr->dmac_m[2] = data >> 8;
  826. pr->dmac_m[3] = data;
  827. break;
  828. case TEMPLATE_FIELD_DMAC2:
  829. pr->dmac[0] = data >> 8;
  830. pr->dmac[1] = data;
  831. pr->dmac_m[0] = data >> 8;
  832. pr->dmac_m[1] = data;
  833. break;
  834. case TEMPLATE_FIELD_ETHERTYPE:
  835. pr->ethertype = data;
  836. pr->ethertype_m = data_m;
  837. break;
  838. case TEMPLATE_FIELD_ITAG:
  839. pr->itag = data;
  840. pr->itag_m = data_m;
  841. break;
  842. case TEMPLATE_FIELD_RANGE_CHK:
  843. pr->field_range_check = data;
  844. pr->field_range_check_m = data_m;
  845. break;
  846. case TEMPLATE_FIELD_SIP0:
  847. pr->sip = data;
  848. pr->sip_m = data_m;
  849. break;
  850. case TEMPLATE_FIELD_SIP1:
  851. pr->sip = (pr->sip << 16) | data;
  852. pr->sip_m = (pr->sip << 16) | data_m;
  853. break;
  854. case TEMPLATE_FIELD_SIP2:
  855. pr->is_ipv6 = true;
  856. // Make use of limitiations on the position of the match values
  857. ipv6_addr_set(&pr->sip6, pr->sip, r[5 - i / 2],
  858. r[4 - i / 2], r[3 - i / 2]);
  859. ipv6_addr_set(&pr->sip6_m, pr->sip_m, r[5 - i / 2],
  860. r[4 - i / 2], r[3 - i / 2]);
  861. case TEMPLATE_FIELD_SIP3:
  862. case TEMPLATE_FIELD_SIP4:
  863. case TEMPLATE_FIELD_SIP5:
  864. case TEMPLATE_FIELD_SIP6:
  865. case TEMPLATE_FIELD_SIP7:
  866. break;
  867. case TEMPLATE_FIELD_DIP0:
  868. pr->dip = data;
  869. pr->dip_m = data_m;
  870. break;
  871. case TEMPLATE_FIELD_DIP1:
  872. pr->dip = (pr->dip << 16) | data;
  873. pr->dip_m = (pr->dip << 16) | data_m;
  874. break;
  875. case TEMPLATE_FIELD_DIP2:
  876. pr->is_ipv6 = true;
  877. ipv6_addr_set(&pr->dip6, pr->dip, r[5 - i / 2],
  878. r[4 - i / 2], r[3 - i / 2]);
  879. ipv6_addr_set(&pr->dip6_m, pr->dip_m, r[5 - i / 2],
  880. r[4 - i / 2], r[3 - i / 2]);
  881. case TEMPLATE_FIELD_DIP3:
  882. case TEMPLATE_FIELD_DIP4:
  883. case TEMPLATE_FIELD_DIP5:
  884. case TEMPLATE_FIELD_DIP6:
  885. case TEMPLATE_FIELD_DIP7:
  886. break;
  887. case TEMPLATE_FIELD_IP_TOS_PROTO:
  888. pr->tos_proto = data;
  889. pr->tos_proto_m = data_m;
  890. break;
  891. case TEMPLATE_FIELD_L4_SPORT:
  892. pr->sport = data;
  893. pr->sport_m = data_m;
  894. break;
  895. case TEMPLATE_FIELD_L4_DPORT:
  896. pr->dport = data;
  897. pr->dport_m = data_m;
  898. break;
  899. case TEMPLATE_FIELD_ICMP_IGMP:
  900. pr->icmp_igmp = data;
  901. pr->icmp_igmp_m = data_m;
  902. break;
  903. default:
  904. pr_info("%s: unknown field %d\n", __func__, field_type);
  905. }
  906. }
  907. }
  908. static void rtl838x_read_pie_fixed_fields(u32 r[], struct pie_rule *pr)
  909. {
  910. pr->spmmask_fix = (r[6] >> 22) & 0x3;
  911. pr->spn = (r[6] >> 16) & 0x3f;
  912. pr->mgnt_vlan = (r[6] >> 15) & 1;
  913. pr->dmac_hit_sw = (r[6] >> 14) & 1;
  914. pr->not_first_frag = (r[6] >> 13) & 1;
  915. pr->frame_type_l4 = (r[6] >> 10) & 7;
  916. pr->frame_type = (r[6] >> 8) & 3;
  917. pr->otag_fmt = (r[6] >> 7) & 1;
  918. pr->itag_fmt = (r[6] >> 6) & 1;
  919. pr->otag_exist = (r[6] >> 5) & 1;
  920. pr->itag_exist = (r[6] >> 4) & 1;
  921. pr->frame_type_l2 = (r[6] >> 2) & 3;
  922. pr->tid = r[6] & 3;
  923. pr->spmmask_fix_m = (r[13] >> 22) & 0x3;
  924. pr->spn_m = (r[13] >> 16) & 0x3f;
  925. pr->mgnt_vlan_m = (r[13] >> 15) & 1;
  926. pr->dmac_hit_sw_m = (r[13] >> 14) & 1;
  927. pr->not_first_frag_m = (r[13] >> 13) & 1;
  928. pr->frame_type_l4_m = (r[13] >> 10) & 7;
  929. pr->frame_type_m = (r[13] >> 8) & 3;
  930. pr->otag_fmt_m = (r[13] >> 7) & 1;
  931. pr->itag_fmt_m = (r[13] >> 6) & 1;
  932. pr->otag_exist_m = (r[13] >> 5) & 1;
  933. pr->itag_exist_m = (r[13] >> 4) & 1;
  934. pr->frame_type_l2_m = (r[13] >> 2) & 3;
  935. pr->tid_m = r[13] & 3;
  936. pr->valid = r[14] & BIT(31);
  937. pr->cond_not = r[14] & BIT(30);
  938. pr->cond_and1 = r[14] & BIT(29);
  939. pr->cond_and2 = r[14] & BIT(28);
  940. pr->ivalid = r[14] & BIT(27);
  941. pr->drop = (r[17] >> 14) & 3;
  942. pr->fwd_sel = r[17] & BIT(13);
  943. pr->ovid_sel = r[17] & BIT(12);
  944. pr->ivid_sel = r[17] & BIT(11);
  945. pr->flt_sel = r[17] & BIT(10);
  946. pr->log_sel = r[17] & BIT(9);
  947. pr->rmk_sel = r[17] & BIT(8);
  948. pr->meter_sel = r[17] & BIT(7);
  949. pr->tagst_sel = r[17] & BIT(6);
  950. pr->mir_sel = r[17] & BIT(5);
  951. pr->nopri_sel = r[17] & BIT(4);
  952. pr->cpupri_sel = r[17] & BIT(3);
  953. pr->otpid_sel = r[17] & BIT(2);
  954. pr->itpid_sel = r[17] & BIT(1);
  955. pr->shaper_sel = r[17] & BIT(0);
  956. }
  957. static void rtl838x_write_pie_fixed_fields(u32 r[], struct pie_rule *pr)
  958. {
  959. r[6] = ((u32) (pr->spmmask_fix & 0x3)) << 22;
  960. r[6] |= ((u32) (pr->spn & 0x3f)) << 16;
  961. r[6] |= pr->mgnt_vlan ? BIT(15) : 0;
  962. r[6] |= pr->dmac_hit_sw ? BIT(14) : 0;
  963. r[6] |= pr->not_first_frag ? BIT(13) : 0;
  964. r[6] |= ((u32) (pr->frame_type_l4 & 0x7)) << 10;
  965. r[6] |= ((u32) (pr->frame_type & 0x3)) << 8;
  966. r[6] |= pr->otag_fmt ? BIT(7) : 0;
  967. r[6] |= pr->itag_fmt ? BIT(6) : 0;
  968. r[6] |= pr->otag_exist ? BIT(5) : 0;
  969. r[6] |= pr->itag_exist ? BIT(4) : 0;
  970. r[6] |= ((u32) (pr->frame_type_l2 & 0x3)) << 2;
  971. r[6] |= ((u32) (pr->tid & 0x3));
  972. r[13] = ((u32) (pr->spmmask_fix_m & 0x3)) << 22;
  973. r[13] |= ((u32) (pr->spn_m & 0x3f)) << 16;
  974. r[13] |= pr->mgnt_vlan_m ? BIT(15) : 0;
  975. r[13] |= pr->dmac_hit_sw_m ? BIT(14) : 0;
  976. r[13] |= pr->not_first_frag_m ? BIT(13) : 0;
  977. r[13] |= ((u32) (pr->frame_type_l4_m & 0x7)) << 10;
  978. r[13] |= ((u32) (pr->frame_type_m & 0x3)) << 8;
  979. r[13] |= pr->otag_fmt_m ? BIT(7) : 0;
  980. r[13] |= pr->itag_fmt_m ? BIT(6) : 0;
  981. r[13] |= pr->otag_exist_m ? BIT(5) : 0;
  982. r[13] |= pr->itag_exist_m ? BIT(4) : 0;
  983. r[13] |= ((u32) (pr->frame_type_l2_m & 0x3)) << 2;
  984. r[13] |= ((u32) (pr->tid_m & 0x3));
  985. r[14] = pr->valid ? BIT(31) : 0;
  986. r[14] |= pr->cond_not ? BIT(30) : 0;
  987. r[14] |= pr->cond_and1 ? BIT(29) : 0;
  988. r[14] |= pr->cond_and2 ? BIT(28) : 0;
  989. r[14] |= pr->ivalid ? BIT(27) : 0;
  990. if (pr->drop)
  991. r[17] = 0x1 << 14; // Standard drop action
  992. else
  993. r[17] = 0;
  994. r[17] |= pr->fwd_sel ? BIT(13) : 0;
  995. r[17] |= pr->ovid_sel ? BIT(12) : 0;
  996. r[17] |= pr->ivid_sel ? BIT(11) : 0;
  997. r[17] |= pr->flt_sel ? BIT(10) : 0;
  998. r[17] |= pr->log_sel ? BIT(9) : 0;
  999. r[17] |= pr->rmk_sel ? BIT(8) : 0;
  1000. r[17] |= pr->meter_sel ? BIT(7) : 0;
  1001. r[17] |= pr->tagst_sel ? BIT(6) : 0;
  1002. r[17] |= pr->mir_sel ? BIT(5) : 0;
  1003. r[17] |= pr->nopri_sel ? BIT(4) : 0;
  1004. r[17] |= pr->cpupri_sel ? BIT(3) : 0;
  1005. r[17] |= pr->otpid_sel ? BIT(2) : 0;
  1006. r[17] |= pr->itpid_sel ? BIT(1) : 0;
  1007. r[17] |= pr->shaper_sel ? BIT(0) : 0;
  1008. }
  1009. static int rtl838x_write_pie_action(u32 r[], struct pie_rule *pr)
  1010. {
  1011. u16 *aif = (u16 *)&r[17];
  1012. u16 data;
  1013. int fields_used = 0;
  1014. aif--;
  1015. pr_debug("%s, at %08x\n", __func__, (u32)aif);
  1016. /* Multiple actions can be linked to a match of a PIE rule,
  1017. * they have different precedence depending on their type and this precedence
  1018. * defines which Action Information Field (0-4) in the IACL table stores
  1019. * the additional data of the action (like e.g. the port number a packet is
  1020. * forwarded to) */
  1021. // TODO: count bits in selectors to limit to a maximum number of actions
  1022. if (pr->fwd_sel) { // Forwarding action
  1023. data = pr->fwd_act << 13;
  1024. data |= pr->fwd_data;
  1025. data |= pr->bypass_all ? BIT(12) : 0;
  1026. data |= pr->bypass_ibc_sc ? BIT(11) : 0;
  1027. data |= pr->bypass_igr_stp ? BIT(10) : 0;
  1028. *aif-- = data;
  1029. fields_used++;
  1030. }
  1031. if (pr->ovid_sel) { // Outer VID action
  1032. data = (pr->ovid_act & 0x3) << 12;
  1033. data |= pr->ovid_data;
  1034. *aif-- = data;
  1035. fields_used++;
  1036. }
  1037. if (pr->ivid_sel) { // Inner VID action
  1038. data = (pr->ivid_act & 0x3) << 12;
  1039. data |= pr->ivid_data;
  1040. *aif-- = data;
  1041. fields_used++;
  1042. }
  1043. if (pr->flt_sel) { // Filter action
  1044. *aif-- = pr->flt_data;
  1045. fields_used++;
  1046. }
  1047. if (pr->log_sel) { // Log action
  1048. if (fields_used >= 4)
  1049. return -1;
  1050. *aif-- = pr->log_data;
  1051. fields_used++;
  1052. }
  1053. if (pr->rmk_sel) { // Remark action
  1054. if (fields_used >= 4)
  1055. return -1;
  1056. *aif-- = pr->rmk_data;
  1057. fields_used++;
  1058. }
  1059. if (pr->meter_sel) { // Meter action
  1060. if (fields_used >= 4)
  1061. return -1;
  1062. *aif-- = pr->meter_data;
  1063. fields_used++;
  1064. }
  1065. if (pr->tagst_sel) { // Egress Tag Status action
  1066. if (fields_used >= 4)
  1067. return -1;
  1068. *aif-- = pr->tagst_data;
  1069. fields_used++;
  1070. }
  1071. if (pr->mir_sel) { // Mirror action
  1072. if (fields_used >= 4)
  1073. return -1;
  1074. *aif-- = pr->mir_data;
  1075. fields_used++;
  1076. }
  1077. if (pr->nopri_sel) { // Normal Priority action
  1078. if (fields_used >= 4)
  1079. return -1;
  1080. *aif-- = pr->nopri_data;
  1081. fields_used++;
  1082. }
  1083. if (pr->cpupri_sel) { // CPU Priority action
  1084. if (fields_used >= 4)
  1085. return -1;
  1086. *aif-- = pr->nopri_data;
  1087. fields_used++;
  1088. }
  1089. if (pr->otpid_sel) { // OTPID action
  1090. if (fields_used >= 4)
  1091. return -1;
  1092. *aif-- = pr->otpid_data;
  1093. fields_used++;
  1094. }
  1095. if (pr->itpid_sel) { // ITPID action
  1096. if (fields_used >= 4)
  1097. return -1;
  1098. *aif-- = pr->itpid_data;
  1099. fields_used++;
  1100. }
  1101. if (pr->shaper_sel) { // Traffic shaper action
  1102. if (fields_used >= 4)
  1103. return -1;
  1104. *aif-- = pr->shaper_data;
  1105. fields_used++;
  1106. }
  1107. return 0;
  1108. }
  1109. static void rtl838x_read_pie_action(u32 r[], struct pie_rule *pr)
  1110. {
  1111. u16 *aif = (u16 *)&r[17];
  1112. aif--;
  1113. pr_debug("%s, at %08x\n", __func__, (u32)aif);
  1114. if (pr->drop)
  1115. pr_debug("%s: Action Drop: %d", __func__, pr->drop);
  1116. if (pr->fwd_sel){ // Forwarding action
  1117. pr->fwd_act = *aif >> 13;
  1118. pr->fwd_data = *aif--;
  1119. pr->bypass_all = pr->fwd_data & BIT(12);
  1120. pr->bypass_ibc_sc = pr->fwd_data & BIT(11);
  1121. pr->bypass_igr_stp = pr->fwd_data & BIT(10);
  1122. if (pr->bypass_all || pr->bypass_ibc_sc || pr->bypass_igr_stp)
  1123. pr->bypass_sel = true;
  1124. }
  1125. if (pr->ovid_sel) // Outer VID action
  1126. pr->ovid_data = *aif--;
  1127. if (pr->ivid_sel) // Inner VID action
  1128. pr->ivid_data = *aif--;
  1129. if (pr->flt_sel) // Filter action
  1130. pr->flt_data = *aif--;
  1131. if (pr->log_sel) // Log action
  1132. pr->log_data = *aif--;
  1133. if (pr->rmk_sel) // Remark action
  1134. pr->rmk_data = *aif--;
  1135. if (pr->meter_sel) // Meter action
  1136. pr->meter_data = *aif--;
  1137. if (pr->tagst_sel) // Egress Tag Status action
  1138. pr->tagst_data = *aif--;
  1139. if (pr->mir_sel) // Mirror action
  1140. pr->mir_data = *aif--;
  1141. if (pr->nopri_sel) // Normal Priority action
  1142. pr->nopri_data = *aif--;
  1143. if (pr->cpupri_sel) // CPU Priority action
  1144. pr->nopri_data = *aif--;
  1145. if (pr->otpid_sel) // OTPID action
  1146. pr->otpid_data = *aif--;
  1147. if (pr->itpid_sel) // ITPID action
  1148. pr->itpid_data = *aif--;
  1149. if (pr->shaper_sel) // Traffic shaper action
  1150. pr->shaper_data = *aif--;
  1151. }
  1152. static void rtl838x_pie_rule_dump_raw(u32 r[])
  1153. {
  1154. pr_info("Raw IACL table entry:\n");
  1155. pr_info("Match : %08x %08x %08x %08x %08x %08x\n", r[0], r[1], r[2], r[3], r[4], r[5]);
  1156. pr_info("Fixed : %08x\n", r[6]);
  1157. pr_info("Match M: %08x %08x %08x %08x %08x %08x\n", r[7], r[8], r[9], r[10], r[11], r[12]);
  1158. pr_info("Fixed M: %08x\n", r[13]);
  1159. pr_info("AIF : %08x %08x %08x\n", r[14], r[15], r[16]);
  1160. pr_info("Sel : %08x\n", r[17]);
  1161. }
  1162. static void rtl838x_pie_rule_dump(struct pie_rule *pr)
  1163. {
  1164. pr_info("Drop: %d, fwd: %d, ovid: %d, ivid: %d, flt: %d, log: %d, rmk: %d, meter: %d tagst: %d, mir: %d, nopri: %d, cpupri: %d, otpid: %d, itpid: %d, shape: %d\n",
  1165. pr->drop, pr->fwd_sel, pr->ovid_sel, pr->ivid_sel, pr->flt_sel, pr->log_sel, pr->rmk_sel, pr->log_sel, pr->tagst_sel, pr->mir_sel, pr->nopri_sel,
  1166. pr->cpupri_sel, pr->otpid_sel, pr->itpid_sel, pr->shaper_sel);
  1167. if (pr->fwd_sel)
  1168. pr_info("FWD: %08x\n", pr->fwd_data);
  1169. pr_info("TID: %x, %x\n", pr->tid, pr->tid_m);
  1170. }
  1171. static int rtl838x_pie_rule_read(struct rtl838x_switch_priv *priv, int idx, struct pie_rule *pr)
  1172. {
  1173. // Read IACL table (1) via register 0
  1174. struct table_reg *q = rtl_table_get(RTL8380_TBL_0, 1);
  1175. u32 r[18];
  1176. int i;
  1177. int block = idx / PIE_BLOCK_SIZE;
  1178. u32 t_select = sw_r32(RTL838X_ACL_BLK_TMPLTE_CTRL(block));
  1179. memset(pr, 0, sizeof(*pr));
  1180. rtl_table_read(q, idx);
  1181. for (i = 0; i < 18; i++)
  1182. r[i] = sw_r32(rtl_table_data(q, i));
  1183. rtl_table_release(q);
  1184. rtl838x_read_pie_fixed_fields(r, pr);
  1185. if (!pr->valid)
  1186. return 0;
  1187. pr_info("%s: template_selectors %08x, tid: %d\n", __func__, t_select, pr->tid);
  1188. rtl838x_pie_rule_dump_raw(r);
  1189. rtl838x_read_pie_templated(r, pr, fixed_templates[(t_select >> (pr->tid * 3)) & 0x7]);
  1190. rtl838x_read_pie_action(r, pr);
  1191. return 0;
  1192. }
  1193. static int rtl838x_pie_rule_write(struct rtl838x_switch_priv *priv, int idx, struct pie_rule *pr)
  1194. {
  1195. // Access IACL table (1) via register 0
  1196. struct table_reg *q = rtl_table_get(RTL8380_TBL_0, 1);
  1197. u32 r[18];
  1198. int i, err = 0;
  1199. int block = idx / PIE_BLOCK_SIZE;
  1200. u32 t_select = sw_r32(RTL838X_ACL_BLK_TMPLTE_CTRL(block));
  1201. pr_debug("%s: %d, t_select: %08x\n", __func__, idx, t_select);
  1202. for (i = 0; i < 18; i++)
  1203. r[i] = 0;
  1204. if (!pr->valid)
  1205. goto err_out;
  1206. rtl838x_write_pie_fixed_fields(r, pr);
  1207. pr_debug("%s: template %d\n", __func__, (t_select >> (pr->tid * 3)) & 0x7);
  1208. rtl838x_write_pie_templated(r, pr, fixed_templates[(t_select >> (pr->tid * 3)) & 0x7]);
  1209. if (rtl838x_write_pie_action(r, pr)) {
  1210. pr_err("Rule actions too complex\n");
  1211. goto err_out;
  1212. }
  1213. // rtl838x_pie_rule_dump_raw(r);
  1214. for (i = 0; i < 18; i++)
  1215. sw_w32(r[i], rtl_table_data(q, i));
  1216. err_out:
  1217. rtl_table_write(q, idx);
  1218. rtl_table_release(q);
  1219. return err;
  1220. }
  1221. static bool rtl838x_pie_templ_has(int t, enum template_field_id field_type)
  1222. {
  1223. int i;
  1224. enum template_field_id ft;
  1225. for (i = 0; i < N_FIXED_FIELDS; i++) {
  1226. ft = fixed_templates[t][i];
  1227. if (field_type == ft)
  1228. return true;
  1229. }
  1230. return false;
  1231. }
  1232. static int rtl838x_pie_verify_template(struct rtl838x_switch_priv *priv,
  1233. struct pie_rule *pr, int t, int block)
  1234. {
  1235. int i;
  1236. if (!pr->is_ipv6 && pr->sip_m && !rtl838x_pie_templ_has(t, TEMPLATE_FIELD_SIP0))
  1237. return -1;
  1238. if (!pr->is_ipv6 && pr->dip_m && !rtl838x_pie_templ_has(t, TEMPLATE_FIELD_DIP0))
  1239. return -1;
  1240. if (pr->is_ipv6) {
  1241. if ((pr->sip6_m.s6_addr32[0] || pr->sip6_m.s6_addr32[1]
  1242. || pr->sip6_m.s6_addr32[2] || pr->sip6_m.s6_addr32[3])
  1243. && !rtl838x_pie_templ_has(t, TEMPLATE_FIELD_SIP2))
  1244. return -1;
  1245. if ((pr->dip6_m.s6_addr32[0] || pr->dip6_m.s6_addr32[1]
  1246. || pr->dip6_m.s6_addr32[2] || pr->dip6_m.s6_addr32[3])
  1247. && !rtl838x_pie_templ_has(t, TEMPLATE_FIELD_DIP2))
  1248. return -1;
  1249. }
  1250. if (ether_addr_to_u64(pr->smac) && !rtl838x_pie_templ_has(t, TEMPLATE_FIELD_SMAC0))
  1251. return -1;
  1252. if (ether_addr_to_u64(pr->dmac) && !rtl838x_pie_templ_has(t, TEMPLATE_FIELD_DMAC0))
  1253. return -1;
  1254. // TODO: Check more
  1255. i = find_first_zero_bit(&priv->pie_use_bm[block * 4], PIE_BLOCK_SIZE);
  1256. if (i >= PIE_BLOCK_SIZE)
  1257. return -1;
  1258. return i + PIE_BLOCK_SIZE * block;
  1259. }
  1260. static int rtl838x_pie_rule_add(struct rtl838x_switch_priv *priv, struct pie_rule *pr)
  1261. {
  1262. int idx, block, j, t;
  1263. pr_debug("In %s\n", __func__);
  1264. mutex_lock(&priv->pie_mutex);
  1265. for (block = 0; block < priv->n_pie_blocks; block++) {
  1266. for (j = 0; j < 3; j++) {
  1267. t = (sw_r32(RTL838X_ACL_BLK_TMPLTE_CTRL(block)) >> (j * 3)) & 0x7;
  1268. pr_debug("Testing block %d, template %d, template id %d\n", block, j, t);
  1269. idx = rtl838x_pie_verify_template(priv, pr, t, block);
  1270. if (idx >= 0)
  1271. break;
  1272. }
  1273. if (j < 3)
  1274. break;
  1275. }
  1276. if (block >= priv->n_pie_blocks) {
  1277. mutex_unlock(&priv->pie_mutex);
  1278. return -EOPNOTSUPP;
  1279. }
  1280. pr_debug("Using block: %d, index %d, template-id %d\n", block, idx, j);
  1281. set_bit(idx, priv->pie_use_bm);
  1282. pr->valid = true;
  1283. pr->tid = j; // Mapped to template number
  1284. pr->tid_m = 0x3;
  1285. pr->id = idx;
  1286. rtl838x_pie_lookup_enable(priv, idx);
  1287. rtl838x_pie_rule_write(priv, idx, pr);
  1288. mutex_unlock(&priv->pie_mutex);
  1289. return 0;
  1290. }
  1291. static void rtl838x_pie_rule_rm(struct rtl838x_switch_priv *priv, struct pie_rule *pr)
  1292. {
  1293. int idx = pr->id;
  1294. rtl838x_pie_rule_del(priv, idx, idx);
  1295. clear_bit(idx, priv->pie_use_bm);
  1296. }
  1297. /*
  1298. * Initializes the Packet Inspection Engine:
  1299. * powers it up, enables default matching templates for all blocks
  1300. * and clears all rules possibly installed by u-boot
  1301. */
  1302. static void rtl838x_pie_init(struct rtl838x_switch_priv *priv)
  1303. {
  1304. int i;
  1305. u32 template_selectors;
  1306. mutex_init(&priv->pie_mutex);
  1307. // Enable ACL lookup on all ports, including CPU_PORT
  1308. for (i = 0; i <= priv->cpu_port; i++)
  1309. sw_w32(1, RTL838X_ACL_PORT_LOOKUP_CTRL(i));
  1310. // Power on all PIE blocks
  1311. for (i = 0; i < priv->n_pie_blocks; i++)
  1312. sw_w32_mask(0, BIT(i), RTL838X_ACL_BLK_PWR_CTRL);
  1313. // Include IPG in metering
  1314. sw_w32(1, RTL838X_METER_GLB_CTRL);
  1315. // Delete all present rules
  1316. rtl838x_pie_rule_del(priv, 0, priv->n_pie_blocks * PIE_BLOCK_SIZE - 1);
  1317. // Routing bypasses source port filter: disable write-protection, first
  1318. sw_w32_mask(0, 3, RTL838X_INT_RW_CTRL);
  1319. sw_w32_mask(0, 1, RTL838X_DMY_REG27);
  1320. sw_w32_mask(3, 0, RTL838X_INT_RW_CTRL);
  1321. // Enable predefined templates 0, 1 and 2 for even blocks
  1322. template_selectors = 0 | (1 << 3) | (2 << 6);
  1323. for (i = 0; i < 6; i += 2)
  1324. sw_w32(template_selectors, RTL838X_ACL_BLK_TMPLTE_CTRL(i));
  1325. // Enable predefined templates 0, 3 and 4 (IPv6 support) for odd blocks
  1326. template_selectors = 0 | (3 << 3) | (4 << 6);
  1327. for (i = 1; i < priv->n_pie_blocks; i += 2)
  1328. sw_w32(template_selectors, RTL838X_ACL_BLK_TMPLTE_CTRL(i));
  1329. // Group each pair of physical blocks together to a logical block
  1330. sw_w32(0b10101010101, RTL838X_ACL_BLK_GROUP_CTRL);
  1331. }
  1332. static u32 rtl838x_packet_cntr_read(int counter)
  1333. {
  1334. u32 v;
  1335. // Read LOG table (3) via register RTL8380_TBL_0
  1336. struct table_reg *r = rtl_table_get(RTL8380_TBL_0, 3);
  1337. pr_debug("In %s, id %d\n", __func__, counter);
  1338. rtl_table_read(r, counter / 2);
  1339. pr_debug("Registers: %08x %08x\n",
  1340. sw_r32(rtl_table_data(r, 0)), sw_r32(rtl_table_data(r, 1)));
  1341. // The table has a size of 2 registers
  1342. if (counter % 2)
  1343. v = sw_r32(rtl_table_data(r, 0));
  1344. else
  1345. v = sw_r32(rtl_table_data(r, 1));
  1346. rtl_table_release(r);
  1347. return v;
  1348. }
  1349. static void rtl838x_packet_cntr_clear(int counter)
  1350. {
  1351. // Access LOG table (3) via register RTL8380_TBL_0
  1352. struct table_reg *r = rtl_table_get(RTL8380_TBL_0, 3);
  1353. pr_debug("In %s, id %d\n", __func__, counter);
  1354. // The table has a size of 2 registers
  1355. if (counter % 2)
  1356. sw_w32(0, rtl_table_data(r, 0));
  1357. else
  1358. sw_w32(0, rtl_table_data(r, 1));
  1359. rtl_table_write(r, counter / 2);
  1360. rtl_table_release(r);
  1361. }
  1362. static void rtl838x_route_read(int idx, struct rtl83xx_route *rt)
  1363. {
  1364. // Read ROUTING table (2) via register RTL8380_TBL_1
  1365. struct table_reg *r = rtl_table_get(RTL8380_TBL_1, 2);
  1366. pr_debug("In %s, id %d\n", __func__, idx);
  1367. rtl_table_read(r, idx);
  1368. // The table has a size of 2 registers
  1369. rt->nh.gw = sw_r32(rtl_table_data(r, 0));
  1370. rt->nh.gw <<= 32;
  1371. rt->nh.gw |= sw_r32(rtl_table_data(r, 1));
  1372. rtl_table_release(r);
  1373. }
  1374. static void rtl838x_route_write(int idx, struct rtl83xx_route *rt)
  1375. {
  1376. // Access ROUTING table (2) via register RTL8380_TBL_1
  1377. struct table_reg *r = rtl_table_get(RTL8380_TBL_1, 2);
  1378. pr_debug("In %s, id %d, gw: %016llx\n", __func__, idx, rt->nh.gw);
  1379. sw_w32(rt->nh.gw >> 32, rtl_table_data(r, 0));
  1380. sw_w32(rt->nh.gw, rtl_table_data(r, 1));
  1381. rtl_table_write(r, idx);
  1382. rtl_table_release(r);
  1383. }
  1384. static int rtl838x_l3_setup(struct rtl838x_switch_priv *priv)
  1385. {
  1386. // Nothing to be done
  1387. return 0;
  1388. }
  1389. void rtl838x_vlan_port_pvidmode_set(int port, enum pbvlan_type type, enum pbvlan_mode mode)
  1390. {
  1391. if (type == PBVLAN_TYPE_INNER)
  1392. sw_w32_mask(0x3, mode, RTL838X_VLAN_PORT_PB_VLAN + (port << 2));
  1393. else
  1394. sw_w32_mask(0x3 << 14, mode << 14, RTL838X_VLAN_PORT_PB_VLAN + (port << 2));
  1395. }
  1396. void rtl838x_vlan_port_pvid_set(int port, enum pbvlan_type type, int pvid)
  1397. {
  1398. if (type == PBVLAN_TYPE_INNER)
  1399. sw_w32_mask(0xfff << 2, pvid << 2, RTL838X_VLAN_PORT_PB_VLAN + (port << 2));
  1400. else
  1401. sw_w32_mask(0xfff << 16, pvid << 16, RTL838X_VLAN_PORT_PB_VLAN + (port << 2));
  1402. }
  1403. static int rtl838x_set_ageing_time(unsigned long msec)
  1404. {
  1405. int t = sw_r32(RTL838X_L2_CTRL_1);
  1406. t &= 0x7FFFFF;
  1407. t = t * 128 / 625; /* Aging time in seconds. 0: L2 aging disabled */
  1408. pr_debug("L2 AGING time: %d sec\n", t);
  1409. t = (msec * 625 + 127000) / 128000;
  1410. t = t > 0x7FFFFF ? 0x7FFFFF : t;
  1411. sw_w32_mask(0x7FFFFF, t, RTL838X_L2_CTRL_1);
  1412. pr_debug("Dynamic aging for ports: %x\n", sw_r32(RTL838X_L2_PORT_AGING_OUT));
  1413. return 0;
  1414. }
  1415. static void rtl838x_set_igr_filter(int port, enum igr_filter state)
  1416. {
  1417. sw_w32_mask(0x3 << ((port & 0xf)<<1), state << ((port & 0xf)<<1),
  1418. RTL838X_VLAN_PORT_IGR_FLTR + (((port >> 4) << 2)));
  1419. }
  1420. static void rtl838x_set_egr_filter(int port, enum egr_filter state)
  1421. {
  1422. sw_w32_mask(0x1 << (port % 0x1d), state << (port % 0x1d),
  1423. RTL838X_VLAN_PORT_EGR_FLTR + (((port / 29) << 2)));
  1424. }
  1425. void rtl838x_set_distribution_algorithm(int group, int algoidx, u32 algomsk)
  1426. {
  1427. algoidx &= 1; // RTL838X only supports 2 concurrent algorithms
  1428. sw_w32_mask(1 << (group % 8), algoidx << (group % 8),
  1429. RTL838X_TRK_HASH_IDX_CTRL + ((group >> 3) << 2));
  1430. sw_w32(algomsk, RTL838X_TRK_HASH_CTRL + (algoidx << 2));
  1431. }
  1432. void rtl838x_set_receive_management_action(int port, rma_ctrl_t type, action_type_t action)
  1433. {
  1434. switch(type) {
  1435. case BPDU:
  1436. sw_w32_mask(3 << ((port & 0xf) << 1), (action & 0x3) << ((port & 0xf) << 1),
  1437. RTL838X_RMA_BPDU_CTRL + ((port >> 4) << 2));
  1438. break;
  1439. case PTP:
  1440. sw_w32_mask(3 << ((port & 0xf) << 1), (action & 0x3) << ((port & 0xf) << 1),
  1441. RTL838X_RMA_PTP_CTRL + ((port >> 4) << 2));
  1442. break;
  1443. case LLTP:
  1444. sw_w32_mask(3 << ((port & 0xf) << 1), (action & 0x3) << ((port & 0xf) << 1),
  1445. RTL838X_RMA_LLTP_CTRL + ((port >> 4) << 2));
  1446. break;
  1447. default:
  1448. break;
  1449. }
  1450. }
  1451. const struct rtl838x_reg rtl838x_reg = {
  1452. .mask_port_reg_be = rtl838x_mask_port_reg,
  1453. .set_port_reg_be = rtl838x_set_port_reg,
  1454. .get_port_reg_be = rtl838x_get_port_reg,
  1455. .mask_port_reg_le = rtl838x_mask_port_reg,
  1456. .set_port_reg_le = rtl838x_set_port_reg,
  1457. .get_port_reg_le = rtl838x_get_port_reg,
  1458. .stat_port_rst = RTL838X_STAT_PORT_RST,
  1459. .stat_rst = RTL838X_STAT_RST,
  1460. .stat_port_std_mib = RTL838X_STAT_PORT_STD_MIB,
  1461. .port_iso_ctrl = rtl838x_port_iso_ctrl,
  1462. .traffic_enable = rtl838x_traffic_enable,
  1463. .traffic_disable = rtl838x_traffic_disable,
  1464. .traffic_get = rtl838x_traffic_get,
  1465. .traffic_set = rtl838x_traffic_set,
  1466. .l2_ctrl_0 = RTL838X_L2_CTRL_0,
  1467. .l2_ctrl_1 = RTL838X_L2_CTRL_1,
  1468. .l2_port_aging_out = RTL838X_L2_PORT_AGING_OUT,
  1469. .set_ageing_time = rtl838x_set_ageing_time,
  1470. .smi_poll_ctrl = RTL838X_SMI_POLL_CTRL,
  1471. .l2_tbl_flush_ctrl = RTL838X_L2_TBL_FLUSH_CTRL,
  1472. .exec_tbl0_cmd = rtl838x_exec_tbl0_cmd,
  1473. .exec_tbl1_cmd = rtl838x_exec_tbl1_cmd,
  1474. .tbl_access_data_0 = rtl838x_tbl_access_data_0,
  1475. .isr_glb_src = RTL838X_ISR_GLB_SRC,
  1476. .isr_port_link_sts_chg = RTL838X_ISR_PORT_LINK_STS_CHG,
  1477. .imr_port_link_sts_chg = RTL838X_IMR_PORT_LINK_STS_CHG,
  1478. .imr_glb = RTL838X_IMR_GLB,
  1479. .vlan_tables_read = rtl838x_vlan_tables_read,
  1480. .vlan_set_tagged = rtl838x_vlan_set_tagged,
  1481. .vlan_set_untagged = rtl838x_vlan_set_untagged,
  1482. .mac_force_mode_ctrl = rtl838x_mac_force_mode_ctrl,
  1483. .vlan_profile_dump = rtl838x_vlan_profile_dump,
  1484. .vlan_profile_setup = rtl838x_vlan_profile_setup,
  1485. .vlan_fwd_on_inner = rtl838x_vlan_fwd_on_inner,
  1486. .set_vlan_igr_filter = rtl838x_set_igr_filter,
  1487. .set_vlan_egr_filter = rtl838x_set_egr_filter,
  1488. .enable_learning = rtl838x_enable_learning,
  1489. .enable_flood = rtl838x_enable_flood,
  1490. .enable_mcast_flood = rtl838x_enable_mcast_flood,
  1491. .enable_bcast_flood = rtl838x_enable_bcast_flood,
  1492. .stp_get = rtl838x_stp_get,
  1493. .stp_set = rtl838x_stp_set,
  1494. .mac_port_ctrl = rtl838x_mac_port_ctrl,
  1495. .l2_port_new_salrn = rtl838x_l2_port_new_salrn,
  1496. .l2_port_new_sa_fwd = rtl838x_l2_port_new_sa_fwd,
  1497. .mir_ctrl = RTL838X_MIR_CTRL,
  1498. .mir_dpm = RTL838X_MIR_DPM_CTRL,
  1499. .mir_spm = RTL838X_MIR_SPM_CTRL,
  1500. .mac_link_sts = RTL838X_MAC_LINK_STS,
  1501. .mac_link_dup_sts = RTL838X_MAC_LINK_DUP_STS,
  1502. .mac_link_spd_sts = rtl838x_mac_link_spd_sts,
  1503. .mac_rx_pause_sts = RTL838X_MAC_RX_PAUSE_STS,
  1504. .mac_tx_pause_sts = RTL838X_MAC_TX_PAUSE_STS,
  1505. .read_l2_entry_using_hash = rtl838x_read_l2_entry_using_hash,
  1506. .write_l2_entry_using_hash = rtl838x_write_l2_entry_using_hash,
  1507. .read_cam = rtl838x_read_cam,
  1508. .write_cam = rtl838x_write_cam,
  1509. .vlan_port_tag_sts_ctrl = RTL838X_VLAN_PORT_TAG_STS_CTRL,
  1510. .vlan_port_pvidmode_set = rtl838x_vlan_port_pvidmode_set,
  1511. .vlan_port_pvid_set = rtl838x_vlan_port_pvid_set,
  1512. .trk_mbr_ctr = rtl838x_trk_mbr_ctr,
  1513. .rma_bpdu_fld_pmask = RTL838X_RMA_BPDU_FLD_PMSK,
  1514. .spcl_trap_eapol_ctrl = RTL838X_SPCL_TRAP_EAPOL_CTRL,
  1515. .init_eee = rtl838x_init_eee,
  1516. .port_eee_set = rtl838x_port_eee_set,
  1517. .eee_port_ability = rtl838x_eee_port_ability,
  1518. .l2_hash_seed = rtl838x_l2_hash_seed,
  1519. .l2_hash_key = rtl838x_l2_hash_key,
  1520. .read_mcast_pmask = rtl838x_read_mcast_pmask,
  1521. .write_mcast_pmask = rtl838x_write_mcast_pmask,
  1522. .pie_init = rtl838x_pie_init,
  1523. .pie_rule_read = rtl838x_pie_rule_read,
  1524. .pie_rule_write = rtl838x_pie_rule_write,
  1525. .pie_rule_add = rtl838x_pie_rule_add,
  1526. .pie_rule_rm = rtl838x_pie_rule_rm,
  1527. .l2_learning_setup = rtl838x_l2_learning_setup,
  1528. .packet_cntr_read = rtl838x_packet_cntr_read,
  1529. .packet_cntr_clear = rtl838x_packet_cntr_clear,
  1530. .route_read = rtl838x_route_read,
  1531. .route_write = rtl838x_route_write,
  1532. .l3_setup = rtl838x_l3_setup,
  1533. .set_distribution_algorithm = rtl838x_set_distribution_algorithm,
  1534. .set_receive_management_action = rtl838x_set_receive_management_action,
  1535. };
  1536. irqreturn_t rtl838x_switch_irq(int irq, void *dev_id)
  1537. {
  1538. struct dsa_switch *ds = dev_id;
  1539. u32 status = sw_r32(RTL838X_ISR_GLB_SRC);
  1540. u32 ports = sw_r32(RTL838X_ISR_PORT_LINK_STS_CHG);
  1541. u32 link;
  1542. int i;
  1543. /* Clear status */
  1544. sw_w32(ports, RTL838X_ISR_PORT_LINK_STS_CHG);
  1545. pr_info("RTL8380 Link change: status: %x, ports %x\n", status, ports);
  1546. for (i = 0; i < 28; i++) {
  1547. if (ports & BIT(i)) {
  1548. link = sw_r32(RTL838X_MAC_LINK_STS);
  1549. if (link & BIT(i))
  1550. dsa_port_phylink_mac_change(ds, i, true);
  1551. else
  1552. dsa_port_phylink_mac_change(ds, i, false);
  1553. }
  1554. }
  1555. return IRQ_HANDLED;
  1556. }
  1557. int rtl838x_smi_wait_op(int timeout)
  1558. {
  1559. do {
  1560. timeout--;
  1561. udelay(10);
  1562. } while ((sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_1) & 0x1) && (timeout >= 0));
  1563. if (timeout <= 0)
  1564. return -1;
  1565. return 0;
  1566. }
  1567. /*
  1568. * Reads a register in a page from the PHY
  1569. */
  1570. int rtl838x_read_phy(u32 port, u32 page, u32 reg, u32 *val)
  1571. {
  1572. u32 v;
  1573. u32 park_page;
  1574. if (port > 31) {
  1575. *val = 0xffff;
  1576. return 0;
  1577. }
  1578. if (page > 4095 || reg > 31)
  1579. return -ENOTSUPP;
  1580. mutex_lock(&smi_lock);
  1581. if (rtl838x_smi_wait_op(10000))
  1582. goto timeout;
  1583. sw_w32_mask(0xffff0000, port << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  1584. park_page = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_1) & ((0x1f << 15) | 0x2);
  1585. v = reg << 20 | page << 3;
  1586. sw_w32(v | park_page, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  1587. sw_w32_mask(0, 1, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  1588. if (rtl838x_smi_wait_op(10000))
  1589. goto timeout;
  1590. *val = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_2) & 0xffff;
  1591. mutex_unlock(&smi_lock);
  1592. return 0;
  1593. timeout:
  1594. mutex_unlock(&smi_lock);
  1595. return -ETIMEDOUT;
  1596. }
  1597. /*
  1598. * Write to a register in a page of the PHY
  1599. */
  1600. int rtl838x_write_phy(u32 port, u32 page, u32 reg, u32 val)
  1601. {
  1602. u32 v;
  1603. u32 park_page;
  1604. val &= 0xffff;
  1605. if (port > 31 || page > 4095 || reg > 31)
  1606. return -ENOTSUPP;
  1607. mutex_lock(&smi_lock);
  1608. if (rtl838x_smi_wait_op(10000))
  1609. goto timeout;
  1610. sw_w32(BIT(port), RTL838X_SMI_ACCESS_PHY_CTRL_0);
  1611. mdelay(10);
  1612. sw_w32_mask(0xffff0000, val << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  1613. park_page = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_1) & ((0x1f << 15) | 0x2);
  1614. v = reg << 20 | page << 3 | 0x4;
  1615. sw_w32(v | park_page, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  1616. sw_w32_mask(0, 1, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  1617. if (rtl838x_smi_wait_op(10000))
  1618. goto timeout;
  1619. mutex_unlock(&smi_lock);
  1620. return 0;
  1621. timeout:
  1622. mutex_unlock(&smi_lock);
  1623. return -ETIMEDOUT;
  1624. }
  1625. /*
  1626. * Read an mmd register of a PHY
  1627. */
  1628. int rtl838x_read_mmd_phy(u32 port, u32 addr, u32 reg, u32 *val)
  1629. {
  1630. u32 v;
  1631. mutex_lock(&smi_lock);
  1632. if (rtl838x_smi_wait_op(10000))
  1633. goto timeout;
  1634. sw_w32(1 << port, RTL838X_SMI_ACCESS_PHY_CTRL_0);
  1635. mdelay(10);
  1636. sw_w32_mask(0xffff0000, port << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  1637. v = addr << 16 | reg;
  1638. sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_3);
  1639. /* mmd-access | read | cmd-start */
  1640. v = 1 << 1 | 0 << 2 | 1;
  1641. sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  1642. if (rtl838x_smi_wait_op(10000))
  1643. goto timeout;
  1644. *val = sw_r32(RTL838X_SMI_ACCESS_PHY_CTRL_2) & 0xffff;
  1645. mutex_unlock(&smi_lock);
  1646. return 0;
  1647. timeout:
  1648. mutex_unlock(&smi_lock);
  1649. return -ETIMEDOUT;
  1650. }
  1651. /*
  1652. * Write to an mmd register of a PHY
  1653. */
  1654. int rtl838x_write_mmd_phy(u32 port, u32 addr, u32 reg, u32 val)
  1655. {
  1656. u32 v;
  1657. pr_debug("MMD write: port %d, dev %d, reg %d, val %x\n", port, addr, reg, val);
  1658. val &= 0xffff;
  1659. mutex_lock(&smi_lock);
  1660. if (rtl838x_smi_wait_op(10000))
  1661. goto timeout;
  1662. sw_w32(1 << port, RTL838X_SMI_ACCESS_PHY_CTRL_0);
  1663. mdelay(10);
  1664. sw_w32_mask(0xffff0000, val << 16, RTL838X_SMI_ACCESS_PHY_CTRL_2);
  1665. sw_w32_mask(0x1f << 16, addr << 16, RTL838X_SMI_ACCESS_PHY_CTRL_3);
  1666. sw_w32_mask(0xffff, reg, RTL838X_SMI_ACCESS_PHY_CTRL_3);
  1667. /* mmd-access | write | cmd-start */
  1668. v = 1 << 1 | 1 << 2 | 1;
  1669. sw_w32(v, RTL838X_SMI_ACCESS_PHY_CTRL_1);
  1670. if (rtl838x_smi_wait_op(10000))
  1671. goto timeout;
  1672. mutex_unlock(&smi_lock);
  1673. return 0;
  1674. timeout:
  1675. mutex_unlock(&smi_lock);
  1676. return -ETIMEDOUT;
  1677. }
  1678. void rtl8380_get_version(struct rtl838x_switch_priv *priv)
  1679. {
  1680. u32 rw_save, info_save;
  1681. u32 info;
  1682. rw_save = sw_r32(RTL838X_INT_RW_CTRL);
  1683. sw_w32(rw_save | 0x3, RTL838X_INT_RW_CTRL);
  1684. info_save = sw_r32(RTL838X_CHIP_INFO);
  1685. sw_w32(info_save | 0xA0000000, RTL838X_CHIP_INFO);
  1686. info = sw_r32(RTL838X_CHIP_INFO);
  1687. sw_w32(info_save, RTL838X_CHIP_INFO);
  1688. sw_w32(rw_save, RTL838X_INT_RW_CTRL);
  1689. if ((info & 0xFFFF) == 0x6275) {
  1690. if (((info >> 16) & 0x1F) == 0x1)
  1691. priv->version = RTL8380_VERSION_A;
  1692. else if (((info >> 16) & 0x1F) == 0x2)
  1693. priv->version = RTL8380_VERSION_B;
  1694. else
  1695. priv->version = RTL8380_VERSION_B;
  1696. } else {
  1697. priv->version = '-';
  1698. }
  1699. }
  1700. void rtl838x_vlan_profile_dump(int profile)
  1701. {
  1702. u32 p;
  1703. if (profile < 0 || profile > 7)
  1704. return;
  1705. p = sw_r32(RTL838X_VLAN_PROFILE(profile));
  1706. pr_info("VLAN profile %d: L2 learning: %d, UNKN L2MC FLD PMSK %d, \
  1707. UNKN IPMC FLD PMSK %d, UNKN IPv6MC FLD PMSK: %d",
  1708. profile, p & 1, (p >> 1) & 0x1ff, (p >> 10) & 0x1ff, (p >> 19) & 0x1ff);
  1709. }
  1710. void rtl8380_sds_rst(int mac)
  1711. {
  1712. u32 offset = (mac == 24) ? 0 : 0x100;
  1713. sw_w32_mask(1 << 11, 0, RTL838X_SDS4_FIB_REG0 + offset);
  1714. sw_w32_mask(0x3, 0, RTL838X_SDS4_REG28 + offset);
  1715. sw_w32_mask(0x3, 0x3, RTL838X_SDS4_REG28 + offset);
  1716. sw_w32_mask(0, 0x1 << 6, RTL838X_SDS4_DUMMY0 + offset);
  1717. sw_w32_mask(0x1 << 6, 0, RTL838X_SDS4_DUMMY0 + offset);
  1718. pr_debug("SERDES reset: %d\n", mac);
  1719. }
  1720. int rtl8380_sds_power(int mac, int val)
  1721. {
  1722. u32 mode = (val == 1) ? 0x4 : 0x9;
  1723. u32 offset = (mac == 24) ? 5 : 0;
  1724. if ((mac != 24) && (mac != 26)) {
  1725. pr_err("%s: not a fibre port: %d\n", __func__, mac);
  1726. return -1;
  1727. }
  1728. sw_w32_mask(0x1f << offset, mode << offset, RTL838X_SDS_MODE_SEL);
  1729. rtl8380_sds_rst(mac);
  1730. return 0;
  1731. }