rtl930x.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <asm/mach-rtl838x/mach-rtl83xx.h>
  3. #include <linux/inetdevice.h>
  4. #include "rtl83xx.h"
  5. #define RTL930X_VLAN_PORT_TAG_STS_INTERNAL 0x0
  6. #define RTL930X_VLAN_PORT_TAG_STS_UNTAG 0x1
  7. #define RTL930X_VLAN_PORT_TAG_STS_TAGGED 0x2
  8. #define RTL930X_VLAN_PORT_TAG_STS_PRIORITY_TAGGED 0x3
  9. #define RTL930X_VLAN_PORT_TAG_STS_CTRL_BASE 0xCE24
  10. /* port 0-28 */
  11. #define RTL930X_VLAN_PORT_TAG_STS_CTRL(port) \
  12. RTL930X_VLAN_PORT_TAG_STS_CTRL_BASE + (port << 2)
  13. #define RTL930X_VLAN_PORT_TAG_STS_CTRL_EGR_OTAG_STS_MASK GENMASK(7,6)
  14. #define RTL930X_VLAN_PORT_TAG_STS_CTRL_EGR_ITAG_STS_MASK GENMASK(5,4)
  15. #define RTL930X_VLAN_PORT_TAG_STS_CTRL_EGR_P_OTAG_KEEP_MASK GENMASK(3,3)
  16. #define RTL930X_VLAN_PORT_TAG_STS_CTRL_EGR_P_ITAG_KEEP_MASK GENMASK(2,2)
  17. #define RTL930X_VLAN_PORT_TAG_STS_CTRL_IGR_P_OTAG_KEEP_MASK GENMASK(1,1)
  18. #define RTL930X_VLAN_PORT_TAG_STS_CTRL_IGR_P_ITAG_KEEP_MASK GENMASK(0,0)
  19. extern struct mutex smi_lock;
  20. extern struct rtl83xx_soc_info soc_info;
  21. /* Definition of the RTL930X-specific template field IDs as used in the PIE */
  22. enum template_field_id {
  23. TEMPLATE_FIELD_SPM0 = 0, /* Source portmask ports 0-15 */
  24. TEMPLATE_FIELD_SPM1 = 1, /* Source portmask ports 16-31 */
  25. TEMPLATE_FIELD_DMAC0 = 2, /* Destination MAC [15:0] */
  26. TEMPLATE_FIELD_DMAC1 = 3, /* Destination MAC [31:16] */
  27. TEMPLATE_FIELD_DMAC2 = 4, /* Destination MAC [47:32] */
  28. TEMPLATE_FIELD_SMAC0 = 5, /* Source MAC [15:0] */
  29. TEMPLATE_FIELD_SMAC1 = 6, /* Source MAC [31:16] */
  30. TEMPLATE_FIELD_SMAC2 = 7, /* Source MAC [47:32] */
  31. TEMPLATE_FIELD_ETHERTYPE = 8, /* Ethernet frame type field */
  32. TEMPLATE_FIELD_OTAG = 9,
  33. TEMPLATE_FIELD_ITAG = 10,
  34. TEMPLATE_FIELD_SIP0 = 11,
  35. TEMPLATE_FIELD_SIP1 = 12,
  36. TEMPLATE_FIELD_DIP0 = 13,
  37. TEMPLATE_FIELD_DIP1 = 14,
  38. TEMPLATE_FIELD_IP_TOS_PROTO = 15,
  39. TEMPLATE_FIELD_L4_SPORT = 16,
  40. TEMPLATE_FIELD_L4_DPORT = 17,
  41. TEMPLATE_FIELD_L34_HEADER = 18,
  42. TEMPLATE_FIELD_TCP_INFO = 19,
  43. TEMPLATE_FIELD_FIELD_SELECTOR_VALID = 20,
  44. TEMPLATE_FIELD_FIELD_SELECTOR_0 = 21,
  45. TEMPLATE_FIELD_FIELD_SELECTOR_1 = 22,
  46. TEMPLATE_FIELD_FIELD_SELECTOR_2 = 23,
  47. TEMPLATE_FIELD_FIELD_SELECTOR_3 = 24,
  48. TEMPLATE_FIELD_FIELD_SELECTOR_4 = 25,
  49. TEMPLATE_FIELD_FIELD_SELECTOR_5 = 26,
  50. TEMPLATE_FIELD_SIP2 = 27,
  51. TEMPLATE_FIELD_SIP3 = 28,
  52. TEMPLATE_FIELD_SIP4 = 29,
  53. TEMPLATE_FIELD_SIP5 = 30,
  54. TEMPLATE_FIELD_SIP6 = 31,
  55. TEMPLATE_FIELD_SIP7 = 32,
  56. TEMPLATE_FIELD_DIP2 = 33,
  57. TEMPLATE_FIELD_DIP3 = 34,
  58. TEMPLATE_FIELD_DIP4 = 35,
  59. TEMPLATE_FIELD_DIP5 = 36,
  60. TEMPLATE_FIELD_DIP6 = 37,
  61. TEMPLATE_FIELD_DIP7 = 38,
  62. TEMPLATE_FIELD_PKT_INFO = 39,
  63. TEMPLATE_FIELD_FLOW_LABEL = 40,
  64. TEMPLATE_FIELD_DSAP_SSAP = 41,
  65. TEMPLATE_FIELD_SNAP_OUI = 42,
  66. TEMPLATE_FIELD_FWD_VID = 43,
  67. TEMPLATE_FIELD_RANGE_CHK = 44,
  68. TEMPLATE_FIELD_VLAN_GMSK = 45, /* VLAN Group Mask/IP range check */
  69. TEMPLATE_FIELD_DLP = 46,
  70. TEMPLATE_FIELD_META_DATA = 47,
  71. TEMPLATE_FIELD_SRC_FWD_VID = 48,
  72. TEMPLATE_FIELD_SLP = 49,
  73. };
  74. /* The meaning of TEMPLATE_FIELD_VLAN depends on phase and the configuration in
  75. * RTL930X_PIE_CTRL. We use always the same definition and map to the inner VLAN tag:
  76. */
  77. #define TEMPLATE_FIELD_VLAN TEMPLATE_FIELD_ITAG
  78. /* Number of fixed templates predefined in the RTL9300 SoC */
  79. #define N_FIXED_TEMPLATES 5
  80. /* RTL9300 specific predefined templates */
  81. static enum template_field_id fixed_templates[N_FIXED_TEMPLATES][N_FIXED_FIELDS] =
  82. {
  83. {
  84. TEMPLATE_FIELD_DMAC0, TEMPLATE_FIELD_DMAC1, TEMPLATE_FIELD_DMAC2,
  85. TEMPLATE_FIELD_SMAC0, TEMPLATE_FIELD_SMAC1, TEMPLATE_FIELD_SMAC2,
  86. TEMPLATE_FIELD_VLAN, TEMPLATE_FIELD_IP_TOS_PROTO, TEMPLATE_FIELD_DSAP_SSAP,
  87. TEMPLATE_FIELD_ETHERTYPE, TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1
  88. }, {
  89. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_DIP0,
  90. TEMPLATE_FIELD_DIP1, TEMPLATE_FIELD_IP_TOS_PROTO, TEMPLATE_FIELD_TCP_INFO,
  91. TEMPLATE_FIELD_L4_SPORT, TEMPLATE_FIELD_L4_DPORT, TEMPLATE_FIELD_VLAN,
  92. TEMPLATE_FIELD_RANGE_CHK, TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1
  93. }, {
  94. TEMPLATE_FIELD_DMAC0, TEMPLATE_FIELD_DMAC1, TEMPLATE_FIELD_DMAC2,
  95. TEMPLATE_FIELD_VLAN, TEMPLATE_FIELD_ETHERTYPE, TEMPLATE_FIELD_IP_TOS_PROTO,
  96. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_DIP0,
  97. TEMPLATE_FIELD_DIP1, TEMPLATE_FIELD_L4_SPORT, TEMPLATE_FIELD_L4_DPORT
  98. }, {
  99. TEMPLATE_FIELD_DIP0, TEMPLATE_FIELD_DIP1, TEMPLATE_FIELD_DIP2,
  100. TEMPLATE_FIELD_DIP3, TEMPLATE_FIELD_DIP4, TEMPLATE_FIELD_DIP5,
  101. TEMPLATE_FIELD_DIP6, TEMPLATE_FIELD_DIP7, TEMPLATE_FIELD_IP_TOS_PROTO,
  102. TEMPLATE_FIELD_TCP_INFO, TEMPLATE_FIELD_L4_SPORT, TEMPLATE_FIELD_L4_DPORT
  103. }, {
  104. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_SIP2,
  105. TEMPLATE_FIELD_SIP3, TEMPLATE_FIELD_SIP4, TEMPLATE_FIELD_SIP5,
  106. TEMPLATE_FIELD_SIP6, TEMPLATE_FIELD_SIP7, TEMPLATE_FIELD_VLAN,
  107. TEMPLATE_FIELD_RANGE_CHK, TEMPLATE_FIELD_SPM1, TEMPLATE_FIELD_SPM1
  108. },
  109. };
  110. void rtl930x_print_matrix(void)
  111. {
  112. int i;
  113. struct table_reg *r = rtl_table_get(RTL9300_TBL_0, 6);
  114. for (i = 0; i < 29; i++) {
  115. rtl_table_read(r, i);
  116. pr_debug("> %08x\n", sw_r32(rtl_table_data(r, 0)));
  117. }
  118. rtl_table_release(r);
  119. }
  120. inline void rtl930x_exec_tbl0_cmd(u32 cmd)
  121. {
  122. sw_w32(cmd, RTL930X_TBL_ACCESS_CTRL_0);
  123. do { } while (sw_r32(RTL930X_TBL_ACCESS_CTRL_0) & (1 << 17));
  124. }
  125. inline void rtl930x_exec_tbl1_cmd(u32 cmd)
  126. {
  127. sw_w32(cmd, RTL930X_TBL_ACCESS_CTRL_1);
  128. do { } while (sw_r32(RTL930X_TBL_ACCESS_CTRL_1) & (1 << 17));
  129. }
  130. inline int rtl930x_tbl_access_data_0(int i)
  131. {
  132. return RTL930X_TBL_ACCESS_DATA_0(i);
  133. }
  134. static inline int rtl930x_l2_port_new_salrn(int p)
  135. {
  136. return RTL930X_L2_PORT_SALRN(p);
  137. }
  138. static inline int rtl930x_l2_port_new_sa_fwd(int p)
  139. {
  140. /* TODO: The definition of the fields changed, because of the master-cpu in a stack */
  141. return RTL930X_L2_PORT_NEW_SA_FWD(p);
  142. }
  143. inline static int rtl930x_trk_mbr_ctr(int group)
  144. {
  145. return RTL930X_TRK_MBR_CTRL + (group << 2);
  146. }
  147. static void rtl930x_vlan_tables_read(u32 vlan, struct rtl838x_vlan_info *info)
  148. {
  149. u32 v, w;
  150. /* Read VLAN table (1) via register 0 */
  151. struct table_reg *r = rtl_table_get(RTL9300_TBL_0, 1);
  152. rtl_table_read(r, vlan);
  153. v = sw_r32(rtl_table_data(r, 0));
  154. w = sw_r32(rtl_table_data(r, 1));
  155. pr_debug("VLAN_READ %d: %08x %08x\n", vlan, v, w);
  156. rtl_table_release(r);
  157. info->tagged_ports = v >> 3;
  158. info->profile_id = (w >> 24) & 7;
  159. info->hash_mc_fid = !!(w & BIT(27));
  160. info->hash_uc_fid = !!(w & BIT(28));
  161. info->fid = ((v & 0x7) << 3) | ((w >> 29) & 0x7);
  162. /* Read UNTAG table via table register 2 */
  163. r = rtl_table_get(RTL9300_TBL_2, 0);
  164. rtl_table_read(r, vlan);
  165. v = sw_r32(rtl_table_data(r, 0));
  166. rtl_table_release(r);
  167. info->untagged_ports = v >> 3;
  168. }
  169. static void rtl930x_vlan_set_tagged(u32 vlan, struct rtl838x_vlan_info *info)
  170. {
  171. u32 v, w;
  172. /* Access VLAN table (1) via register 0 */
  173. struct table_reg *r = rtl_table_get(RTL9300_TBL_0, 1);
  174. v = info->tagged_ports << 3;
  175. v |= ((u32)info->fid) >> 3;
  176. w = ((u32)info->fid) << 29;
  177. w |= info->hash_mc_fid ? BIT(27) : 0;
  178. w |= info->hash_uc_fid ? BIT(28) : 0;
  179. w |= info->profile_id << 24;
  180. sw_w32(v, rtl_table_data(r, 0));
  181. sw_w32(w, rtl_table_data(r, 1));
  182. rtl_table_write(r, vlan);
  183. rtl_table_release(r);
  184. }
  185. void rtl930x_vlan_profile_dump(int profile)
  186. {
  187. u32 p[5];
  188. if (profile < 0 || profile > 7)
  189. return;
  190. p[0] = sw_r32(RTL930X_VLAN_PROFILE_SET(profile));
  191. p[1] = sw_r32(RTL930X_VLAN_PROFILE_SET(profile) + 4);
  192. p[2] = sw_r32(RTL930X_VLAN_PROFILE_SET(profile) + 8) & 0x1FFFFFFF;
  193. p[3] = sw_r32(RTL930X_VLAN_PROFILE_SET(profile) + 12) & 0x1FFFFFFF;
  194. p[4] = sw_r32(RTL930X_VLAN_PROFILE_SET(profile) + 16) & 0x1FFFFFFF;
  195. pr_info("VLAN %d: L2 learn: %d; Unknown MC PMasks: L2 %0x, IPv4 %0x, IPv6: %0x",
  196. profile, p[0] & (3 << 21), p[2], p[3], p[4]);
  197. pr_info(" Routing enabled: IPv4 UC %c, IPv6 UC %c, IPv4 MC %c, IPv6 MC %c\n",
  198. p[0] & BIT(17) ? 'y' : 'n', p[0] & BIT(16) ? 'y' : 'n',
  199. p[0] & BIT(13) ? 'y' : 'n', p[0] & BIT(12) ? 'y' : 'n');
  200. pr_info(" Bridge enabled: IPv4 MC %c, IPv6 MC %c,\n",
  201. p[0] & BIT(15) ? 'y' : 'n', p[0] & BIT(14) ? 'y' : 'n');
  202. pr_info("VLAN profile %d: raw %08x %08x %08x %08x %08x\n",
  203. profile, p[0], p[1], p[2], p[3], p[4]);
  204. }
  205. static void rtl930x_vlan_set_untagged(u32 vlan, u64 portmask)
  206. {
  207. struct table_reg *r = rtl_table_get(RTL9300_TBL_2, 0);
  208. sw_w32(portmask << 3, rtl_table_data(r, 0));
  209. rtl_table_write(r, vlan);
  210. rtl_table_release(r);
  211. }
  212. /* Sets the L2 forwarding to be based on either the inner VLAN tag or the outer */
  213. static void rtl930x_vlan_fwd_on_inner(int port, bool is_set)
  214. {
  215. /* Always set all tag modes to fwd based on either inner or outer tag */
  216. if (is_set)
  217. sw_w32_mask(0, 0xf, RTL930X_VLAN_PORT_FWD + (port << 2));
  218. else
  219. sw_w32_mask(0xf, 0, RTL930X_VLAN_PORT_FWD + (port << 2));
  220. }
  221. static void rtl930x_vlan_profile_setup(int profile)
  222. {
  223. u32 p[5];
  224. pr_info("In %s\n", __func__);
  225. p[0] = sw_r32(RTL930X_VLAN_PROFILE_SET(profile));
  226. p[1] = sw_r32(RTL930X_VLAN_PROFILE_SET(profile) + 4);
  227. /* Enable routing of Ipv4/6 Unicast and IPv4/6 Multicast traffic */
  228. p[0] |= BIT(17) | BIT(16) | BIT(13) | BIT(12);
  229. p[2] = 0x1fffffff; /* L2 unknown MC flooding portmask all ports, including the CPU-port */
  230. p[3] = 0x1fffffff; /* IPv4 unknown MC flooding portmask */
  231. p[4] = 0x1fffffff; /* IPv6 unknown MC flooding portmask */
  232. sw_w32(p[0], RTL930X_VLAN_PROFILE_SET(profile));
  233. sw_w32(p[1], RTL930X_VLAN_PROFILE_SET(profile) + 4);
  234. sw_w32(p[2], RTL930X_VLAN_PROFILE_SET(profile) + 8);
  235. sw_w32(p[3], RTL930X_VLAN_PROFILE_SET(profile) + 12);
  236. sw_w32(p[4], RTL930X_VLAN_PROFILE_SET(profile) + 16);
  237. }
  238. static void rtl930x_l2_learning_setup(void)
  239. {
  240. /* Portmask for flooding broadcast traffic */
  241. sw_w32(0x1fffffff, RTL930X_L2_BC_FLD_PMSK);
  242. /* Portmask for flooding unicast traffic with unknown destination */
  243. sw_w32(0x1fffffff, RTL930X_L2_UNKN_UC_FLD_PMSK);
  244. /* Limit learning to maximum: 32k entries, after that just flood (bits 0-1) */
  245. sw_w32((0x7fff << 2) | 0, RTL930X_L2_LRN_CONSTRT_CTRL);
  246. }
  247. static void rtl930x_stp_get(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  248. {
  249. int i;
  250. u32 cmd = 1 << 17 | /* Execute cmd */
  251. 0 << 16 | /* Read */
  252. 4 << 12 | /* Table type 0b10 */
  253. (msti & 0xfff);
  254. priv->r->exec_tbl0_cmd(cmd);
  255. for (i = 0; i < 2; i++)
  256. port_state[i] = sw_r32(RTL930X_TBL_ACCESS_DATA_0(i));
  257. pr_debug("MSTI: %d STATE: %08x, %08x\n", msti, port_state[0], port_state[1]);
  258. }
  259. static void rtl930x_stp_set(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  260. {
  261. int i;
  262. u32 cmd = 1 << 17 | /* Execute cmd */
  263. 1 << 16 | /* Write */
  264. 4 << 12 | /* Table type 4 */
  265. (msti & 0xfff);
  266. for (i = 0; i < 2; i++)
  267. sw_w32(port_state[i], RTL930X_TBL_ACCESS_DATA_0(i));
  268. priv->r->exec_tbl0_cmd(cmd);
  269. }
  270. static inline int rtl930x_mac_force_mode_ctrl(int p)
  271. {
  272. return RTL930X_MAC_FORCE_MODE_CTRL + (p << 2);
  273. }
  274. static inline int rtl930x_mac_port_ctrl(int p)
  275. {
  276. return RTL930X_MAC_L2_PORT_CTRL(p);
  277. }
  278. static inline int rtl930x_mac_link_spd_sts(int p)
  279. {
  280. return RTL930X_MAC_LINK_SPD_STS(p);
  281. }
  282. static u64 rtl930x_l2_hash_seed(u64 mac, u32 vid)
  283. {
  284. u64 v = vid;
  285. v <<= 48;
  286. v |= mac;
  287. return v;
  288. }
  289. /* Calculate both the block 0 and the block 1 hash by applyingthe same hash
  290. * algorithm as the one used currently by the ASIC to the seed, and return
  291. * both hashes in the lower and higher word of the return value since only 12 bit of
  292. * the hash are significant
  293. */
  294. static u32 rtl930x_l2_hash_key(struct rtl838x_switch_priv *priv, u64 seed)
  295. {
  296. u32 k0, k1, h1, h2, h;
  297. k0 = (u32) (((seed >> 55) & 0x1f) ^
  298. ((seed >> 44) & 0x7ff) ^
  299. ((seed >> 33) & 0x7ff) ^
  300. ((seed >> 22) & 0x7ff) ^
  301. ((seed >> 11) & 0x7ff) ^
  302. (seed & 0x7ff));
  303. h1 = (seed >> 11) & 0x7ff;
  304. h1 = ((h1 & 0x1f) << 6) | ((h1 >> 5) & 0x3f);
  305. h2 = (seed >> 33) & 0x7ff;
  306. h2 = ((h2 & 0x3f) << 5)| ((h2 >> 6) & 0x3f);
  307. k1 = (u32) (((seed << 55) & 0x1f) ^
  308. ((seed >> 44) & 0x7ff) ^
  309. h2 ^
  310. ((seed >> 22) & 0x7ff) ^
  311. h1 ^
  312. (seed & 0x7ff));
  313. /* Algorithm choice for block 0 */
  314. if (sw_r32(RTL930X_L2_CTRL) & BIT(0))
  315. h = k1;
  316. else
  317. h = k0;
  318. /* Algorithm choice for block 1
  319. * Since k0 and k1 are < 2048, adding 2048 will offset the hash into the second
  320. * half of hash-space
  321. * 2048 is in fact the hash-table size 16384 divided by 4 hashes per bucket
  322. * divided by 2 to divide the hash space in 2
  323. */
  324. if (sw_r32(RTL930X_L2_CTRL) & BIT(1))
  325. h |= (k1 + 2048) << 16;
  326. else
  327. h |= (k0 + 2048) << 16;
  328. return h;
  329. }
  330. /* Fills an L2 entry structure from the SoC registers */
  331. static void rtl930x_fill_l2_entry(u32 r[], struct rtl838x_l2_entry *e)
  332. {
  333. pr_debug("In %s valid?\n", __func__);
  334. e->valid = !!(r[2] & BIT(31));
  335. if (!e->valid)
  336. return;
  337. pr_debug("In %s is valid\n", __func__);
  338. e->is_ip_mc = false;
  339. e->is_ipv6_mc = false;
  340. /* TODO: Is there not a function to copy directly MAC memory? */
  341. e->mac[0] = (r[0] >> 24);
  342. e->mac[1] = (r[0] >> 16);
  343. e->mac[2] = (r[0] >> 8);
  344. e->mac[3] = r[0];
  345. e->mac[4] = (r[1] >> 24);
  346. e->mac[5] = (r[1] >> 16);
  347. e->next_hop = !!(r[2] & BIT(12));
  348. e->rvid = r[1] & 0xfff;
  349. /* Is it a unicast entry? check multicast bit */
  350. if (!(e->mac[0] & 1)) {
  351. e->type = L2_UNICAST;
  352. e->is_static = !!(r[2] & BIT(14));
  353. e->port = (r[2] >> 20) & 0x3ff;
  354. /* Check for trunk port */
  355. if (r[2] & BIT(30)) {
  356. e->is_trunk = true;
  357. e->stack_dev = (e->port >> 9) & 1;
  358. e->trunk = e->port & 0x3f;
  359. } else {
  360. e->is_trunk = false;
  361. e->stack_dev = (e->port >> 6) & 0xf;
  362. e->port = e->port & 0x3f;
  363. }
  364. e->block_da = !!(r[2] & BIT(15));
  365. e->block_sa = !!(r[2] & BIT(16));
  366. e->suspended = !!(r[2] & BIT(13));
  367. e->age = (r[2] >> 17) & 3;
  368. e->valid = true;
  369. /* the UC_VID field in hardware is used for the VID or for the route id */
  370. if (e->next_hop) {
  371. e->nh_route_id = r[2] & 0x7ff;
  372. e->vid = 0;
  373. } else {
  374. e->vid = r[2] & 0xfff;
  375. e->nh_route_id = 0;
  376. }
  377. } else {
  378. e->valid = true;
  379. e->type = L2_MULTICAST;
  380. e->mc_portmask_index = (r[2] >> 16) & 0x3ff;
  381. }
  382. }
  383. /* Fills the 3 SoC table registers r[] with the information of in the rtl838x_l2_entry */
  384. static void rtl930x_fill_l2_row(u32 r[], struct rtl838x_l2_entry *e)
  385. {
  386. u32 port;
  387. if (!e->valid) {
  388. r[0] = r[1] = r[2] = 0;
  389. return;
  390. }
  391. r[2] = BIT(31); /* Set valid bit */
  392. r[0] = ((u32)e->mac[0]) << 24 |
  393. ((u32)e->mac[1]) << 16 |
  394. ((u32)e->mac[2]) << 8 |
  395. ((u32)e->mac[3]);
  396. r[1] = ((u32)e->mac[4]) << 24 |
  397. ((u32)e->mac[5]) << 16;
  398. r[2] |= e->next_hop ? BIT(12) : 0;
  399. if (e->type == L2_UNICAST) {
  400. r[2] |= e->is_static ? BIT(14) : 0;
  401. r[1] |= e->rvid & 0xfff;
  402. r[2] |= (e->port & 0x3ff) << 20;
  403. if (e->is_trunk) {
  404. r[2] |= BIT(30);
  405. port = e->stack_dev << 9 | (e->port & 0x3f);
  406. } else {
  407. port = (e->stack_dev & 0xf) << 6;
  408. port |= e->port & 0x3f;
  409. }
  410. r[2] |= port << 20;
  411. r[2] |= e->block_da ? BIT(15) : 0;
  412. r[2] |= e->block_sa ? BIT(17) : 0;
  413. r[2] |= e->suspended ? BIT(13) : 0;
  414. r[2] |= (e->age & 0x3) << 17;
  415. /* the UC_VID field in hardware is used for the VID or for the route id */
  416. if (e->next_hop)
  417. r[2] |= e->nh_route_id & 0x7ff;
  418. else
  419. r[2] |= e->vid & 0xfff;
  420. } else { /* L2_MULTICAST */
  421. r[2] |= (e->mc_portmask_index & 0x3ff) << 16;
  422. r[2] |= e->mc_mac_index & 0x7ff;
  423. }
  424. }
  425. /* Read an L2 UC or MC entry out of a hash bucket of the L2 forwarding table
  426. * hash is the id of the bucket and pos is the position of the entry in that bucket
  427. * The data read from the SoC is filled into rtl838x_l2_entry
  428. */
  429. static u64 rtl930x_read_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  430. {
  431. u32 r[3];
  432. struct table_reg *q = rtl_table_get(RTL9300_TBL_L2, 0);
  433. u32 idx;
  434. int i;
  435. u64 mac;
  436. u64 seed;
  437. pr_debug("%s: hash %08x, pos: %d\n", __func__, hash, pos);
  438. /* On the RTL93xx, 2 different hash algorithms are used making it a
  439. * total of 8 buckets that need to be searched, 4 for each hash-half
  440. * Use second hash space when bucket is between 4 and 8
  441. */
  442. if (pos >= 4) {
  443. pos -= 4;
  444. hash >>= 16;
  445. } else {
  446. hash &= 0xffff;
  447. }
  448. idx = (0 << 14) | (hash << 2) | pos; /* Search SRAM, with hash and at pos in bucket */
  449. pr_debug("%s: NOW hash %08x, pos: %d\n", __func__, hash, pos);
  450. rtl_table_read(q, idx);
  451. for (i = 0; i < 3; i++)
  452. r[i] = sw_r32(rtl_table_data(q, i));
  453. rtl_table_release(q);
  454. rtl930x_fill_l2_entry(r, e);
  455. pr_debug("%s: valid: %d, nh: %d\n", __func__, e->valid, e->next_hop);
  456. if (!e->valid)
  457. return 0;
  458. mac = ((u64)e->mac[0]) << 40 |
  459. ((u64)e->mac[1]) << 32 |
  460. ((u64)e->mac[2]) << 24 |
  461. ((u64)e->mac[3]) << 16 |
  462. ((u64)e->mac[4]) << 8 |
  463. ((u64)e->mac[5]);
  464. seed = rtl930x_l2_hash_seed(mac, e->rvid);
  465. pr_debug("%s: mac %016llx, seed %016llx\n", __func__, mac, seed);
  466. /* return vid with concatenated mac as unique id */
  467. return seed;
  468. }
  469. static void rtl930x_write_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  470. {
  471. u32 r[3];
  472. struct table_reg *q = rtl_table_get(RTL9300_TBL_L2, 0);
  473. u32 idx = (0 << 14) | (hash << 2) | pos; /* Access SRAM, with hash and at pos in bucket */
  474. int i;
  475. pr_debug("%s: hash %d, pos %d\n", __func__, hash, pos);
  476. pr_debug("%s: index %d -> mac %02x:%02x:%02x:%02x:%02x:%02x\n", __func__, idx,
  477. e->mac[0], e->mac[1], e->mac[2], e->mac[3],e->mac[4],e->mac[5]);
  478. rtl930x_fill_l2_row(r, e);
  479. for (i = 0; i < 3; i++)
  480. sw_w32(r[i], rtl_table_data(q, i));
  481. rtl_table_write(q, idx);
  482. rtl_table_release(q);
  483. }
  484. static u64 rtl930x_read_cam(int idx, struct rtl838x_l2_entry *e)
  485. {
  486. u32 r[3];
  487. struct table_reg *q = rtl_table_get(RTL9300_TBL_L2, 1);
  488. int i;
  489. rtl_table_read(q, idx);
  490. for (i = 0; i < 3; i++)
  491. r[i] = sw_r32(rtl_table_data(q, i));
  492. rtl_table_release(q);
  493. rtl930x_fill_l2_entry(r, e);
  494. if (!e->valid)
  495. return 0;
  496. /* return mac with concatenated vid as unique id */
  497. return ((u64)r[0] << 28) | ((r[1] & 0xffff0000) >> 4) | e->vid;
  498. }
  499. static void rtl930x_write_cam(int idx, struct rtl838x_l2_entry *e)
  500. {
  501. u32 r[3];
  502. struct table_reg *q = rtl_table_get(RTL9300_TBL_L2, 1); /* Access L2 Table 1 */
  503. int i;
  504. rtl930x_fill_l2_row(r, e);
  505. for (i = 0; i < 3; i++)
  506. sw_w32(r[i], rtl_table_data(q, i));
  507. rtl_table_write(q, idx);
  508. rtl_table_release(q);
  509. }
  510. static u64 rtl930x_read_mcast_pmask(int idx)
  511. {
  512. u32 portmask;
  513. /* Read MC_PORTMASK (2) via register RTL9300_TBL_L2 */
  514. struct table_reg *q = rtl_table_get(RTL9300_TBL_L2, 2);
  515. rtl_table_read(q, idx);
  516. portmask = sw_r32(rtl_table_data(q, 0));
  517. portmask >>= 3;
  518. rtl_table_release(q);
  519. pr_debug("%s: Index idx %d has portmask %08x\n", __func__, idx, portmask);
  520. return portmask;
  521. }
  522. static void rtl930x_write_mcast_pmask(int idx, u64 portmask)
  523. {
  524. u32 pm = portmask;
  525. /* Access MC_PORTMASK (2) via register RTL9300_TBL_L2 */
  526. struct table_reg *q = rtl_table_get(RTL9300_TBL_L2, 2);
  527. pr_debug("%s: Index idx %d has portmask %08x\n", __func__, idx, pm);
  528. pm <<= 3;
  529. sw_w32(pm, rtl_table_data(q, 0));
  530. rtl_table_write(q, idx);
  531. rtl_table_release(q);
  532. }
  533. u64 rtl930x_traffic_get(int source)
  534. {
  535. u32 v;
  536. struct table_reg *r = rtl_table_get(RTL9300_TBL_0, 6);
  537. rtl_table_read(r, source);
  538. v = sw_r32(rtl_table_data(r, 0));
  539. rtl_table_release(r);
  540. v = v >> 3;
  541. return v;
  542. }
  543. /* Enable traffic between a source port and a destination port matrix */
  544. void rtl930x_traffic_set(int source, u64 dest_matrix)
  545. {
  546. struct table_reg *r = rtl_table_get(RTL9300_TBL_0, 6);
  547. sw_w32((dest_matrix << 3), rtl_table_data(r, 0));
  548. rtl_table_write(r, source);
  549. rtl_table_release(r);
  550. }
  551. void rtl930x_traffic_enable(int source, int dest)
  552. {
  553. struct table_reg *r = rtl_table_get(RTL9300_TBL_0, 6);
  554. rtl_table_read(r, source);
  555. sw_w32_mask(0, BIT(dest + 3), rtl_table_data(r, 0));
  556. rtl_table_write(r, source);
  557. rtl_table_release(r);
  558. }
  559. void rtl930x_traffic_disable(int source, int dest)
  560. {
  561. struct table_reg *r = rtl_table_get(RTL9300_TBL_0, 6);
  562. rtl_table_read(r, source);
  563. sw_w32_mask(BIT(dest + 3), 0, rtl_table_data(r, 0));
  564. rtl_table_write(r, source);
  565. rtl_table_release(r);
  566. }
  567. void rtl9300_dump_debug(void)
  568. {
  569. int i;
  570. u16 r = RTL930X_STAT_PRVTE_DROP_COUNTER0;
  571. for (i = 0; i < 10; i ++) {
  572. pr_info("# %d %08x %08x %08x %08x %08x %08x %08x %08x\n", i * 8,
  573. sw_r32(r), sw_r32(r + 4), sw_r32(r + 8), sw_r32(r + 12),
  574. sw_r32(r + 16), sw_r32(r + 20), sw_r32(r + 24), sw_r32(r + 28));
  575. r += 32;
  576. }
  577. pr_info("# %08x %08x %08x %08x %08x\n",
  578. sw_r32(r), sw_r32(r + 4), sw_r32(r + 8), sw_r32(r + 12), sw_r32(r + 16));
  579. rtl930x_print_matrix();
  580. pr_info("RTL930X_L2_PORT_SABLK_CTRL: %08x, RTL930X_L2_PORT_DABLK_CTRL %08x\n",
  581. sw_r32(RTL930X_L2_PORT_SABLK_CTRL), sw_r32(RTL930X_L2_PORT_DABLK_CTRL)
  582. );
  583. }
  584. irqreturn_t rtl930x_switch_irq(int irq, void *dev_id)
  585. {
  586. struct dsa_switch *ds = dev_id;
  587. u32 ports = sw_r32(RTL930X_ISR_PORT_LINK_STS_CHG);
  588. u32 link;
  589. int i;
  590. /* Clear status */
  591. sw_w32(ports, RTL930X_ISR_PORT_LINK_STS_CHG);
  592. for (i = 0; i < 28; i++) {
  593. if (ports & BIT(i)) {
  594. /* Read the register twice because of issues with latency at least
  595. * with the external RTL8226 PHY on the XGS1210
  596. */
  597. link = sw_r32(RTL930X_MAC_LINK_STS);
  598. link = sw_r32(RTL930X_MAC_LINK_STS);
  599. if (link & BIT(i))
  600. dsa_port_phylink_mac_change(ds, i, true);
  601. else
  602. dsa_port_phylink_mac_change(ds, i, false);
  603. }
  604. }
  605. return IRQ_HANDLED;
  606. }
  607. int rtl930x_write_phy(u32 port, u32 page, u32 reg, u32 val)
  608. {
  609. u32 v;
  610. int err = 0;
  611. pr_debug("%s: port %d, page: %d, reg: %x, val: %x\n", __func__, port, page, reg, val);
  612. if (port > 63 || page > 4095 || reg > 31)
  613. return -ENOTSUPP;
  614. val &= 0xffff;
  615. mutex_lock(&smi_lock);
  616. sw_w32(BIT(port), RTL930X_SMI_ACCESS_PHY_CTRL_0);
  617. sw_w32_mask(0xffff << 16, val << 16, RTL930X_SMI_ACCESS_PHY_CTRL_2);
  618. v = reg << 20 | page << 3 | 0x1f << 15 | BIT(2) | BIT(0);
  619. sw_w32(v, RTL930X_SMI_ACCESS_PHY_CTRL_1);
  620. do {
  621. v = sw_r32(RTL930X_SMI_ACCESS_PHY_CTRL_1);
  622. } while (v & 0x1);
  623. if (v & 0x2)
  624. err = -EIO;
  625. mutex_unlock(&smi_lock);
  626. return err;
  627. }
  628. int rtl930x_read_phy(u32 port, u32 page, u32 reg, u32 *val)
  629. {
  630. u32 v;
  631. int err = 0;
  632. if (port > 63 || page > 4095 || reg > 31)
  633. return -ENOTSUPP;
  634. mutex_lock(&smi_lock);
  635. sw_w32_mask(0xffff << 16, port << 16, RTL930X_SMI_ACCESS_PHY_CTRL_2);
  636. v = reg << 20 | page << 3 | 0x1f << 15 | 1;
  637. sw_w32(v, RTL930X_SMI_ACCESS_PHY_CTRL_1);
  638. do {
  639. v = sw_r32(RTL930X_SMI_ACCESS_PHY_CTRL_1);
  640. } while ( v & 0x1);
  641. if (v & BIT(25)) {
  642. pr_debug("Error reading phy %d, register %d\n", port, reg);
  643. err = -EIO;
  644. }
  645. *val = (sw_r32(RTL930X_SMI_ACCESS_PHY_CTRL_2) & 0xffff);
  646. pr_debug("%s: port %d, page: %d, reg: %x, val: %x\n", __func__, port, page, reg, *val);
  647. mutex_unlock(&smi_lock);
  648. return err;
  649. }
  650. /* Write to an mmd register of the PHY */
  651. int rtl930x_write_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 val)
  652. {
  653. int err = 0;
  654. u32 v;
  655. mutex_lock(&smi_lock);
  656. /* Set PHY to access */
  657. sw_w32(BIT(port), RTL930X_SMI_ACCESS_PHY_CTRL_0);
  658. /* Set data to write */
  659. sw_w32_mask(0xffff << 16, val << 16, RTL930X_SMI_ACCESS_PHY_CTRL_2);
  660. /* Set MMD device number and register to write to */
  661. sw_w32(devnum << 16 | (regnum & 0xffff), RTL930X_SMI_ACCESS_PHY_CTRL_3);
  662. v = BIT(2) | BIT(1) | BIT(0); /* WRITE | MMD-access | EXEC */
  663. sw_w32(v, RTL930X_SMI_ACCESS_PHY_CTRL_1);
  664. do {
  665. v = sw_r32(RTL930X_SMI_ACCESS_PHY_CTRL_1);
  666. } while (v & BIT(0));
  667. pr_debug("%s: port %d, regnum: %x, val: %x (err %d)\n", __func__, port, regnum, val, err);
  668. mutex_unlock(&smi_lock);
  669. return err;
  670. }
  671. /* Read an mmd register of the PHY */
  672. int rtl930x_read_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 *val)
  673. {
  674. int err = 0;
  675. u32 v;
  676. mutex_lock(&smi_lock);
  677. /* Set PHY to access */
  678. sw_w32_mask(0xffff << 16, port << 16, RTL930X_SMI_ACCESS_PHY_CTRL_2);
  679. /* Set MMD device number and register to write to */
  680. sw_w32(devnum << 16 | (regnum & 0xffff), RTL930X_SMI_ACCESS_PHY_CTRL_3);
  681. v = BIT(1) | BIT(0); /* MMD-access | EXEC */
  682. sw_w32(v, RTL930X_SMI_ACCESS_PHY_CTRL_1);
  683. do {
  684. v = sw_r32(RTL930X_SMI_ACCESS_PHY_CTRL_1);
  685. } while (v & BIT(0));
  686. /* There is no error-checking via BIT 25 of v, as it does not seem to be set correctly */
  687. *val = (sw_r32(RTL930X_SMI_ACCESS_PHY_CTRL_2) & 0xffff);
  688. pr_debug("%s: port %d, regnum: %x, val: %x (err %d)\n", __func__, port, regnum, *val, err);
  689. mutex_unlock(&smi_lock);
  690. return err;
  691. }
  692. /* Calculate both the block 0 and the block 1 hash, and return in
  693. * lower and higher word of the return value since only 12 bit of
  694. * the hash are significant
  695. */
  696. u32 rtl930x_hash(struct rtl838x_switch_priv *priv, u64 seed)
  697. {
  698. u32 k0, k1, h1, h2, h;
  699. k0 = (u32) (((seed >> 55) & 0x1f) ^
  700. ((seed >> 44) & 0x7ff) ^
  701. ((seed >> 33) & 0x7ff) ^
  702. ((seed >> 22) & 0x7ff) ^
  703. ((seed >> 11) & 0x7ff) ^
  704. (seed & 0x7ff));
  705. h1 = (seed >> 11) & 0x7ff;
  706. h1 = ((h1 & 0x1f) << 6) | ((h1 >> 5) & 0x3f);
  707. h2 = (seed >> 33) & 0x7ff;
  708. h2 = ((h2 & 0x3f) << 5) | ((h2 >> 6) & 0x3f);
  709. k1 = (u32) (((seed << 55) & 0x1f) ^
  710. ((seed >> 44) & 0x7ff) ^
  711. h2 ^
  712. ((seed >> 22) & 0x7ff) ^
  713. h1 ^
  714. (seed & 0x7ff));
  715. /* Algorithm choice for block 0 */
  716. if (sw_r32(RTL930X_L2_CTRL) & BIT(0))
  717. h = k1;
  718. else
  719. h = k0;
  720. /* Algorithm choice for block 1
  721. * Since k0 and k1 are < 2048, adding 2048 will offset the hash into the second
  722. * half of hash-space
  723. * 2048 is in fact the hash-table size 16384 divided by 4 hashes per bucket
  724. * divided by 2 to divide the hash space in 2
  725. */
  726. if (sw_r32(RTL930X_L2_CTRL) & BIT(1))
  727. h |= (k1 + 2048) << 16;
  728. else
  729. h |= (k0 + 2048) << 16;
  730. return h;
  731. }
  732. /* Enables or disables the EEE/EEEP capability of a port */
  733. void rtl930x_port_eee_set(struct rtl838x_switch_priv *priv, int port, bool enable)
  734. {
  735. u32 v;
  736. /* This works only for Ethernet ports, and on the RTL930X, ports from 26 are SFP */
  737. if (port >= 26)
  738. return;
  739. pr_debug("In %s: setting port %d to %d\n", __func__, port, enable);
  740. v = enable ? 0x3f : 0x0;
  741. /* Set EEE/EEEP state for 100, 500, 1000MBit and 2.5, 5 and 10GBit */
  742. sw_w32_mask(0, v << 10, rtl930x_mac_force_mode_ctrl(port));
  743. /* Set TX/RX EEE state */
  744. v = enable ? 0x3 : 0x0;
  745. sw_w32(v, RTL930X_EEE_CTRL(port));
  746. priv->ports[port].eee_enabled = enable;
  747. }
  748. /* Get EEE own capabilities and negotiation result */
  749. int rtl930x_eee_port_ability(struct rtl838x_switch_priv *priv, struct ethtool_eee *e, int port)
  750. {
  751. u32 link, a;
  752. if (port >= 26)
  753. return -ENOTSUPP;
  754. pr_info("In %s, port %d\n", __func__, port);
  755. link = sw_r32(RTL930X_MAC_LINK_STS);
  756. link = sw_r32(RTL930X_MAC_LINK_STS);
  757. if (!(link & BIT(port)))
  758. return 0;
  759. pr_info("Setting advertised\n");
  760. if (sw_r32(rtl930x_mac_force_mode_ctrl(port)) & BIT(10))
  761. e->advertised |= ADVERTISED_100baseT_Full;
  762. if (sw_r32(rtl930x_mac_force_mode_ctrl(port)) & BIT(12))
  763. e->advertised |= ADVERTISED_1000baseT_Full;
  764. if (priv->ports[port].is2G5 && sw_r32(rtl930x_mac_force_mode_ctrl(port)) & BIT(13)) {
  765. pr_info("ADVERTISING 2.5G EEE\n");
  766. e->advertised |= ADVERTISED_2500baseX_Full;
  767. }
  768. if (priv->ports[port].is10G && sw_r32(rtl930x_mac_force_mode_ctrl(port)) & BIT(15))
  769. e->advertised |= ADVERTISED_10000baseT_Full;
  770. a = sw_r32(RTL930X_MAC_EEE_ABLTY);
  771. a = sw_r32(RTL930X_MAC_EEE_ABLTY);
  772. pr_info("Link partner: %08x\n", a);
  773. if (a & BIT(port)) {
  774. e->lp_advertised = ADVERTISED_100baseT_Full;
  775. e->lp_advertised |= ADVERTISED_1000baseT_Full;
  776. if (priv->ports[port].is2G5)
  777. e->lp_advertised |= ADVERTISED_2500baseX_Full;
  778. if (priv->ports[port].is10G)
  779. e->lp_advertised |= ADVERTISED_10000baseT_Full;
  780. }
  781. /* Read 2x to clear latched state */
  782. a = sw_r32(RTL930X_EEEP_PORT_CTRL(port));
  783. a = sw_r32(RTL930X_EEEP_PORT_CTRL(port));
  784. pr_info("%s RTL930X_EEEP_PORT_CTRL: %08x\n", __func__, a);
  785. return 0;
  786. }
  787. static void rtl930x_init_eee(struct rtl838x_switch_priv *priv, bool enable)
  788. {
  789. int i;
  790. pr_info("Setting up EEE, state: %d\n", enable);
  791. /* Setup EEE on all ports */
  792. for (i = 0; i < priv->cpu_port; i++) {
  793. if (priv->ports[i].phy)
  794. rtl930x_port_eee_set(priv, i, enable);
  795. }
  796. priv->eee_enabled = enable;
  797. }
  798. #define HASH_PICK(val, lsb, len) ((val & (((1 << len) - 1) << lsb)) >> lsb)
  799. static u32 rtl930x_l3_hash4(u32 ip, int algorithm, bool move_dip)
  800. {
  801. u32 rows[4];
  802. u32 hash;
  803. u32 s0, s1, pH;
  804. memset(rows, 0, sizeof(rows));
  805. rows[0] = HASH_PICK(ip, 27, 5);
  806. rows[1] = HASH_PICK(ip, 18, 9);
  807. rows[2] = HASH_PICK(ip, 9, 9);
  808. if (!move_dip)
  809. rows[3] = HASH_PICK(ip, 0, 9);
  810. if (!algorithm) {
  811. hash = rows[0] ^ rows[1] ^ rows[2] ^ rows[3];
  812. } else {
  813. s0 = rows[0] + rows[1] + rows[2];
  814. s1 = (s0 & 0x1ff) + ((s0 & (0x1ff << 9)) >> 9);
  815. pH = (s1 & 0x1ff) + ((s1 & (0x1ff << 9)) >> 9);
  816. hash = pH ^ rows[3];
  817. }
  818. return hash;
  819. }
  820. static u32 rtl930x_l3_hash6(struct in6_addr *ip6, int algorithm, bool move_dip)
  821. {
  822. u32 rows[16];
  823. u32 hash;
  824. u32 s0, s1, pH;
  825. rows[0] = (HASH_PICK(ip6->s6_addr[0], 6, 2) << 0);
  826. rows[1] = (HASH_PICK(ip6->s6_addr[0], 0, 6) << 3) | HASH_PICK(ip6->s6_addr[1], 5, 3);
  827. rows[2] = (HASH_PICK(ip6->s6_addr[1], 0, 5) << 4) | HASH_PICK(ip6->s6_addr[2], 4, 4);
  828. rows[3] = (HASH_PICK(ip6->s6_addr[2], 0, 4) << 5) | HASH_PICK(ip6->s6_addr[3], 3, 5);
  829. rows[4] = (HASH_PICK(ip6->s6_addr[3], 0, 3) << 6) | HASH_PICK(ip6->s6_addr[4], 2, 6);
  830. rows[5] = (HASH_PICK(ip6->s6_addr[4], 0, 2) << 7) | HASH_PICK(ip6->s6_addr[5], 1, 7);
  831. rows[6] = (HASH_PICK(ip6->s6_addr[5], 0, 1) << 8) | HASH_PICK(ip6->s6_addr[6], 0, 8);
  832. rows[7] = (HASH_PICK(ip6->s6_addr[7], 0, 8) << 1) | HASH_PICK(ip6->s6_addr[8], 7, 1);
  833. rows[8] = (HASH_PICK(ip6->s6_addr[8], 0, 7) << 2) | HASH_PICK(ip6->s6_addr[9], 6, 2);
  834. rows[9] = (HASH_PICK(ip6->s6_addr[9], 0, 6) << 3) | HASH_PICK(ip6->s6_addr[10], 5, 3);
  835. rows[10] = (HASH_PICK(ip6->s6_addr[10], 0, 5) << 4) | HASH_PICK(ip6->s6_addr[11], 4, 4);
  836. if (!algorithm) {
  837. rows[11] = (HASH_PICK(ip6->s6_addr[11], 0, 4) << 5) |
  838. (HASH_PICK(ip6->s6_addr[12], 3, 5) << 0);
  839. rows[12] = (HASH_PICK(ip6->s6_addr[12], 0, 3) << 6) |
  840. (HASH_PICK(ip6->s6_addr[13], 2, 6) << 0);
  841. rows[13] = (HASH_PICK(ip6->s6_addr[13], 0, 2) << 7) |
  842. (HASH_PICK(ip6->s6_addr[14], 1, 7) << 0);
  843. if (!move_dip) {
  844. rows[14] = (HASH_PICK(ip6->s6_addr[14], 0, 1) << 8) |
  845. (HASH_PICK(ip6->s6_addr[15], 0, 8) << 0);
  846. }
  847. hash = rows[0] ^ rows[1] ^ rows[2] ^ rows[3] ^ rows[4] ^
  848. rows[5] ^ rows[6] ^ rows[7] ^ rows[8] ^ rows[9] ^
  849. rows[10] ^ rows[11] ^ rows[12] ^ rows[13] ^ rows[14];
  850. } else {
  851. rows[11] = (HASH_PICK(ip6->s6_addr[11], 0, 4) << 5);
  852. rows[12] = (HASH_PICK(ip6->s6_addr[12], 3, 5) << 0);
  853. rows[13] = (HASH_PICK(ip6->s6_addr[12], 0, 3) << 6) |
  854. HASH_PICK(ip6->s6_addr[13], 2, 6);
  855. rows[14] = (HASH_PICK(ip6->s6_addr[13], 0, 2) << 7) |
  856. HASH_PICK(ip6->s6_addr[14], 1, 7);
  857. if (!move_dip) {
  858. rows[15] = (HASH_PICK(ip6->s6_addr[14], 0, 1) << 8) |
  859. (HASH_PICK(ip6->s6_addr[15], 0, 8) << 0);
  860. }
  861. s0 = rows[12] + rows[13] + rows[14];
  862. s1 = (s0 & 0x1ff) + ((s0 & (0x1ff << 9)) >> 9);
  863. pH = (s1 & 0x1ff) + ((s1 & (0x1ff << 9)) >> 9);
  864. hash = rows[0] ^ rows[1] ^ rows[2] ^ rows[3] ^ rows[4] ^
  865. rows[5] ^ rows[6] ^ rows[7] ^ rows[8] ^ rows[9] ^
  866. rows[10] ^ rows[11] ^ pH ^ rows[15];
  867. }
  868. return hash;
  869. }
  870. /* Read a prefix route entry from the L3_PREFIX_ROUTE_IPUC table
  871. * We currently only support IPv4 and IPv6 unicast route
  872. */
  873. static void rtl930x_route_read(int idx, struct rtl83xx_route *rt)
  874. {
  875. u32 v, ip4_m;
  876. bool host_route, default_route;
  877. struct in6_addr ip6_m;
  878. /* Read L3_PREFIX_ROUTE_IPUC table (2) via register RTL9300_TBL_1 */
  879. struct table_reg *r = rtl_table_get(RTL9300_TBL_1, 2);
  880. rtl_table_read(r, idx);
  881. /* The table has a size of 11 registers */
  882. rt->attr.valid = !!(sw_r32(rtl_table_data(r, 0)) & BIT(31));
  883. if (!rt->attr.valid)
  884. goto out;
  885. rt->attr.type = (sw_r32(rtl_table_data(r, 0)) >> 29) & 0x3;
  886. v = sw_r32(rtl_table_data(r, 10));
  887. host_route = !!(v & BIT(21));
  888. default_route = !!(v & BIT(20));
  889. rt->prefix_len = -1;
  890. pr_info("%s: host route %d, default_route %d\n", __func__, host_route, default_route);
  891. switch (rt->attr.type) {
  892. case 0: /* IPv4 Unicast route */
  893. rt->dst_ip = sw_r32(rtl_table_data(r, 4));
  894. ip4_m = sw_r32(rtl_table_data(r, 9));
  895. pr_info("%s: Read ip4 mask: %08x\n", __func__, ip4_m);
  896. rt->prefix_len = host_route ? 32 : -1;
  897. rt->prefix_len = (rt->prefix_len < 0 && default_route) ? 0 : -1;
  898. if (rt->prefix_len < 0)
  899. rt->prefix_len = inet_mask_len(ip4_m);
  900. break;
  901. case 2: /* IPv6 Unicast route */
  902. ipv6_addr_set(&rt->dst_ip6,
  903. sw_r32(rtl_table_data(r, 1)), sw_r32(rtl_table_data(r, 2)),
  904. sw_r32(rtl_table_data(r, 3)), sw_r32(rtl_table_data(r, 4)));
  905. ipv6_addr_set(&ip6_m,
  906. sw_r32(rtl_table_data(r, 6)), sw_r32(rtl_table_data(r, 7)),
  907. sw_r32(rtl_table_data(r, 8)), sw_r32(rtl_table_data(r, 9)));
  908. rt->prefix_len = host_route ? 128 : 0;
  909. rt->prefix_len = (rt->prefix_len < 0 && default_route) ? 0 : -1;
  910. if (rt->prefix_len < 0)
  911. rt->prefix_len = find_last_bit((unsigned long int *)&ip6_m.s6_addr32,
  912. 128);
  913. break;
  914. case 1: /* IPv4 Multicast route */
  915. case 3: /* IPv6 Multicast route */
  916. pr_warn("%s: route type not supported\n", __func__);
  917. goto out;
  918. }
  919. rt->attr.hit = !!(v & BIT(22));
  920. rt->attr.action = (v >> 18) & 3;
  921. rt->nh.id = (v >> 7) & 0x7ff;
  922. rt->attr.ttl_dec = !!(v & BIT(6));
  923. rt->attr.ttl_check = !!(v & BIT(5));
  924. rt->attr.dst_null = !!(v & BIT(4));
  925. rt->attr.qos_as = !!(v & BIT(3));
  926. rt->attr.qos_prio = v & 0x7;
  927. pr_info("%s: index %d is valid: %d\n", __func__, idx, rt->attr.valid);
  928. pr_info("%s: next_hop: %d, hit: %d, action :%d, ttl_dec %d, ttl_check %d, dst_null %d\n",
  929. __func__, rt->nh.id, rt->attr.hit, rt->attr.action,
  930. rt->attr.ttl_dec, rt->attr.ttl_check, rt->attr.dst_null);
  931. pr_info("%s: GW: %pI4, prefix_len: %d\n", __func__, &rt->dst_ip, rt->prefix_len);
  932. out:
  933. rtl_table_release(r);
  934. }
  935. static void rtl930x_net6_mask(int prefix_len, struct in6_addr *ip6_m)
  936. {
  937. int o, b;
  938. /* Define network mask */
  939. o = prefix_len >> 3;
  940. b = prefix_len & 0x7;
  941. memset(ip6_m->s6_addr, 0xff, o);
  942. ip6_m->s6_addr[o] |= b ? 0xff00 >> b : 0x00;
  943. }
  944. /* Read a host route entry from the table using its index
  945. * We currently only support IPv4 and IPv6 unicast route
  946. */
  947. static void rtl930x_host_route_read(int idx, struct rtl83xx_route *rt)
  948. {
  949. u32 v;
  950. /* Read L3_HOST_ROUTE_IPUC table (1) via register RTL9300_TBL_1 */
  951. struct table_reg *r = rtl_table_get(RTL9300_TBL_1, 1);
  952. idx = ((idx / 6) * 8) + (idx % 6);
  953. pr_debug("In %s, physical index %d\n", __func__, idx);
  954. rtl_table_read(r, idx);
  955. /* The table has a size of 5 (for UC, 11 for MC) registers */
  956. v = sw_r32(rtl_table_data(r, 0));
  957. rt->attr.valid = !!(v & BIT(31));
  958. if (!rt->attr.valid)
  959. goto out;
  960. rt->attr.type = (v >> 29) & 0x3;
  961. switch (rt->attr.type) {
  962. case 0: /* IPv4 Unicast route */
  963. rt->dst_ip = sw_r32(rtl_table_data(r, 4));
  964. break;
  965. case 2: /* IPv6 Unicast route */
  966. ipv6_addr_set(&rt->dst_ip6,
  967. sw_r32(rtl_table_data(r, 3)), sw_r32(rtl_table_data(r, 2)),
  968. sw_r32(rtl_table_data(r, 1)), sw_r32(rtl_table_data(r, 0)));
  969. break;
  970. case 1: /* IPv4 Multicast route */
  971. case 3: /* IPv6 Multicast route */
  972. pr_warn("%s: route type not supported\n", __func__);
  973. goto out;
  974. }
  975. rt->attr.hit = !!(v & BIT(20));
  976. rt->attr.dst_null = !!(v & BIT(19));
  977. rt->attr.action = (v >> 17) & 3;
  978. rt->nh.id = (v >> 6) & 0x7ff;
  979. rt->attr.ttl_dec = !!(v & BIT(5));
  980. rt->attr.ttl_check = !!(v & BIT(4));
  981. rt->attr.qos_as = !!(v & BIT(3));
  982. rt->attr.qos_prio = v & 0x7;
  983. pr_debug("%s: index %d is valid: %d\n", __func__, idx, rt->attr.valid);
  984. pr_debug("%s: next_hop: %d, hit: %d, action :%d, ttl_dec %d, ttl_check %d, dst_null %d\n",
  985. __func__, rt->nh.id, rt->attr.hit, rt->attr.action, rt->attr.ttl_dec, rt->attr.ttl_check,
  986. rt->attr.dst_null);
  987. pr_debug("%s: Destination: %pI4\n", __func__, &rt->dst_ip);
  988. out:
  989. rtl_table_release(r);
  990. }
  991. /* Write a host route entry from the table using its index
  992. * We currently only support IPv4 and IPv6 unicast route
  993. */
  994. static void rtl930x_host_route_write(int idx, struct rtl83xx_route *rt)
  995. {
  996. u32 v;
  997. /* Access L3_HOST_ROUTE_IPUC table (1) via register RTL9300_TBL_1 */
  998. struct table_reg *r = rtl_table_get(RTL9300_TBL_1, 1);
  999. /* The table has a size of 5 (for UC, 11 for MC) registers */
  1000. idx = ((idx / 6) * 8) + (idx % 6);
  1001. pr_debug("%s: index %d is valid: %d\n", __func__, idx, rt->attr.valid);
  1002. pr_debug("%s: next_hop: %d, hit: %d, action :%d, ttl_dec %d, ttl_check %d, dst_null %d\n",
  1003. __func__, rt->nh.id, rt->attr.hit, rt->attr.action, rt->attr.ttl_dec, rt->attr.ttl_check,
  1004. rt->attr.dst_null);
  1005. pr_debug("%s: GW: %pI4, prefix_len: %d\n", __func__, &rt->dst_ip, rt->prefix_len);
  1006. v = BIT(31); /* Entry is valid */
  1007. v |= (rt->attr.type & 0x3) << 29;
  1008. v |= rt->attr.hit ? BIT(20) : 0;
  1009. v |= rt->attr.dst_null ? BIT(19) : 0;
  1010. v |= (rt->attr.action & 0x3) << 17;
  1011. v |= (rt->nh.id & 0x7ff) << 6;
  1012. v |= rt->attr.ttl_dec ? BIT(5) : 0;
  1013. v |= rt->attr.ttl_check ? BIT(4) : 0;
  1014. v |= rt->attr.qos_as ? BIT(3) : 0;
  1015. v |= rt->attr.qos_prio & 0x7;
  1016. sw_w32(v, rtl_table_data(r, 0));
  1017. switch (rt->attr.type) {
  1018. case 0: /* IPv4 Unicast route */
  1019. sw_w32(0, rtl_table_data(r, 1));
  1020. sw_w32(0, rtl_table_data(r, 2));
  1021. sw_w32(0, rtl_table_data(r, 3));
  1022. sw_w32(rt->dst_ip, rtl_table_data(r, 4));
  1023. break;
  1024. case 2: /* IPv6 Unicast route */
  1025. sw_w32(rt->dst_ip6.s6_addr32[0], rtl_table_data(r, 1));
  1026. sw_w32(rt->dst_ip6.s6_addr32[1], rtl_table_data(r, 2));
  1027. sw_w32(rt->dst_ip6.s6_addr32[2], rtl_table_data(r, 3));
  1028. sw_w32(rt->dst_ip6.s6_addr32[3], rtl_table_data(r, 4));
  1029. break;
  1030. case 1: /* IPv4 Multicast route */
  1031. case 3: /* IPv6 Multicast route */
  1032. pr_warn("%s: route type not supported\n", __func__);
  1033. goto out;
  1034. }
  1035. rtl_table_write(r, idx);
  1036. out:
  1037. rtl_table_release(r);
  1038. }
  1039. /* Look up the index of a prefix route in the routing table CAM for unicast IPv4/6 routes
  1040. * using hardware offload.
  1041. */
  1042. static int rtl930x_route_lookup_hw(struct rtl83xx_route *rt)
  1043. {
  1044. u32 ip4_m, v;
  1045. struct in6_addr ip6_m;
  1046. int i;
  1047. if (rt->attr.type == 1 || rt->attr.type == 3) /* Hardware only supports UC routes */
  1048. return -1;
  1049. sw_w32_mask(0x3 << 19, rt->attr.type, RTL930X_L3_HW_LU_KEY_CTRL);
  1050. if (rt->attr.type) { /* IPv6 */
  1051. rtl930x_net6_mask(rt->prefix_len, &ip6_m);
  1052. for (i = 0; i < 4; i++)
  1053. sw_w32(rt->dst_ip6.s6_addr32[0] & ip6_m.s6_addr32[0],
  1054. RTL930X_L3_HW_LU_KEY_IP_CTRL + (i << 2));
  1055. } else { /* IPv4 */
  1056. ip4_m = inet_make_mask(rt->prefix_len);
  1057. sw_w32(0, RTL930X_L3_HW_LU_KEY_IP_CTRL);
  1058. sw_w32(0, RTL930X_L3_HW_LU_KEY_IP_CTRL + 4);
  1059. sw_w32(0, RTL930X_L3_HW_LU_KEY_IP_CTRL + 8);
  1060. v = rt->dst_ip & ip4_m;
  1061. pr_info("%s: searching for %pI4\n", __func__, &v);
  1062. sw_w32(v, RTL930X_L3_HW_LU_KEY_IP_CTRL + 12);
  1063. }
  1064. /* Execute CAM lookup in SoC */
  1065. sw_w32(BIT(15), RTL930X_L3_HW_LU_CTRL);
  1066. /* Wait until execute bit clears and result is ready */
  1067. do {
  1068. v = sw_r32(RTL930X_L3_HW_LU_CTRL);
  1069. } while (v & BIT(15));
  1070. pr_info("%s: found: %d, index: %d\n", __func__, !!(v & BIT(14)), v & 0x1ff);
  1071. /* Test if search successful (BIT 14 set) */
  1072. if (v & BIT(14))
  1073. return v & 0x1ff;
  1074. return -1;
  1075. }
  1076. static int rtl930x_find_l3_slot(struct rtl83xx_route *rt, bool must_exist)
  1077. {
  1078. int t, s, slot_width, algorithm, addr, idx;
  1079. u32 hash;
  1080. struct rtl83xx_route route_entry;
  1081. /* IPv6 entries take up 3 slots */
  1082. slot_width = (rt->attr.type == 0) || (rt->attr.type == 2) ? 1 : 3;
  1083. for (t = 0; t < 2; t++) {
  1084. algorithm = (sw_r32(RTL930X_L3_HOST_TBL_CTRL) >> (2 + t)) & 0x1;
  1085. hash = rtl930x_l3_hash4(rt->dst_ip, algorithm, false);
  1086. pr_debug("%s: table %d, algorithm %d, hash %04x\n", __func__, t, algorithm, hash);
  1087. for (s = 0; s < 6; s += slot_width) {
  1088. addr = (t << 12) | ((hash & 0x1ff) << 3) | s;
  1089. pr_debug("%s physical address %d\n", __func__, addr);
  1090. idx = ((addr / 8) * 6) + (addr % 8);
  1091. pr_debug("%s logical address %d\n", __func__, idx);
  1092. rtl930x_host_route_read(idx, &route_entry);
  1093. pr_debug("%s route valid %d, route dest: %pI4, hit %d\n", __func__,
  1094. rt->attr.valid, &rt->dst_ip, rt->attr.hit);
  1095. if (!must_exist && rt->attr.valid)
  1096. return idx;
  1097. if (must_exist && route_entry.dst_ip == rt->dst_ip)
  1098. return idx;
  1099. }
  1100. }
  1101. return -1;
  1102. }
  1103. /* Write a prefix route into the routing table CAM at position idx
  1104. * Currently only IPv4 and IPv6 unicast routes are supported
  1105. */
  1106. static void rtl930x_route_write(int idx, struct rtl83xx_route *rt)
  1107. {
  1108. u32 v, ip4_m;
  1109. struct in6_addr ip6_m;
  1110. /* Access L3_PREFIX_ROUTE_IPUC table (2) via register RTL9300_TBL_1 */
  1111. /* The table has a size of 11 registers (20 for MC) */
  1112. struct table_reg *r = rtl_table_get(RTL9300_TBL_1, 2);
  1113. pr_debug("%s: index %d is valid: %d\n", __func__, idx, rt->attr.valid);
  1114. pr_debug("%s: nexthop: %d, hit: %d, action :%d, ttl_dec %d, ttl_check %d, dst_null %d\n",
  1115. __func__, rt->nh.id, rt->attr.hit, rt->attr.action,
  1116. rt->attr.ttl_dec, rt->attr.ttl_check, rt->attr.dst_null);
  1117. pr_debug("%s: GW: %pI4, prefix_len: %d\n", __func__, &rt->dst_ip, rt->prefix_len);
  1118. v = rt->attr.valid ? BIT(31) : 0;
  1119. v |= (rt->attr.type & 0x3) << 29;
  1120. sw_w32(v, rtl_table_data(r, 0));
  1121. v = rt->attr.hit ? BIT(22) : 0;
  1122. v |= (rt->attr.action & 0x3) << 18;
  1123. v |= (rt->nh.id & 0x7ff) << 7;
  1124. v |= rt->attr.ttl_dec ? BIT(6) : 0;
  1125. v |= rt->attr.ttl_check ? BIT(5) : 0;
  1126. v |= rt->attr.dst_null ? BIT(6) : 0;
  1127. v |= rt->attr.qos_as ? BIT(6) : 0;
  1128. v |= rt->attr.qos_prio & 0x7;
  1129. v |= rt->prefix_len == 0 ? BIT(20) : 0; /* set default route bit */
  1130. /* set bit mask for entry type always to 0x3 */
  1131. sw_w32(0x3 << 29, rtl_table_data(r, 5));
  1132. switch (rt->attr.type) {
  1133. case 0: /* IPv4 Unicast route */
  1134. sw_w32(0, rtl_table_data(r, 1));
  1135. sw_w32(0, rtl_table_data(r, 2));
  1136. sw_w32(0, rtl_table_data(r, 3));
  1137. sw_w32(rt->dst_ip, rtl_table_data(r, 4));
  1138. v |= rt->prefix_len == 32 ? BIT(21) : 0; /* set host-route bit */
  1139. ip4_m = inet_make_mask(rt->prefix_len);
  1140. sw_w32(0, rtl_table_data(r, 6));
  1141. sw_w32(0, rtl_table_data(r, 7));
  1142. sw_w32(0, rtl_table_data(r, 8));
  1143. sw_w32(ip4_m, rtl_table_data(r, 9));
  1144. break;
  1145. case 2: /* IPv6 Unicast route */
  1146. sw_w32(rt->dst_ip6.s6_addr32[0], rtl_table_data(r, 1));
  1147. sw_w32(rt->dst_ip6.s6_addr32[1], rtl_table_data(r, 2));
  1148. sw_w32(rt->dst_ip6.s6_addr32[2], rtl_table_data(r, 3));
  1149. sw_w32(rt->dst_ip6.s6_addr32[3], rtl_table_data(r, 4));
  1150. v |= rt->prefix_len == 128 ? BIT(21) : 0; /* set host-route bit */
  1151. rtl930x_net6_mask(rt->prefix_len, &ip6_m);
  1152. sw_w32(ip6_m.s6_addr32[0], rtl_table_data(r, 6));
  1153. sw_w32(ip6_m.s6_addr32[1], rtl_table_data(r, 7));
  1154. sw_w32(ip6_m.s6_addr32[2], rtl_table_data(r, 8));
  1155. sw_w32(ip6_m.s6_addr32[3], rtl_table_data(r, 9));
  1156. break;
  1157. case 1: /* IPv4 Multicast route */
  1158. case 3: /* IPv6 Multicast route */
  1159. pr_warn("%s: route type not supported\n", __func__);
  1160. rtl_table_release(r);
  1161. return;
  1162. }
  1163. sw_w32(v, rtl_table_data(r, 10));
  1164. pr_debug("%s: %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n", __func__,
  1165. sw_r32(rtl_table_data(r, 0)), sw_r32(rtl_table_data(r, 1)), sw_r32(rtl_table_data(r, 2)),
  1166. sw_r32(rtl_table_data(r, 3)), sw_r32(rtl_table_data(r, 4)), sw_r32(rtl_table_data(r, 5)),
  1167. sw_r32(rtl_table_data(r, 6)), sw_r32(rtl_table_data(r, 7)), sw_r32(rtl_table_data(r, 8)),
  1168. sw_r32(rtl_table_data(r, 9)), sw_r32(rtl_table_data(r, 10)));
  1169. rtl_table_write(r, idx);
  1170. rtl_table_release(r);
  1171. }
  1172. /* Get the destination MAC and L3 egress interface ID of a nexthop entry from
  1173. * the SoC's L3_NEXTHOP table
  1174. */
  1175. static void rtl930x_get_l3_nexthop(int idx, u16 *dmac_id, u16 *interface)
  1176. {
  1177. u32 v;
  1178. /* Read L3_NEXTHOP table (3) via register RTL9300_TBL_1 */
  1179. struct table_reg *r = rtl_table_get(RTL9300_TBL_1, 3);
  1180. rtl_table_read(r, idx);
  1181. /* The table has a size of 1 register */
  1182. v = sw_r32(rtl_table_data(r, 0));
  1183. rtl_table_release(r);
  1184. *dmac_id = (v >> 7) & 0x7fff;
  1185. *interface = v & 0x7f;
  1186. }
  1187. static int rtl930x_l3_mtu_del(struct rtl838x_switch_priv *priv, int mtu)
  1188. {
  1189. int i;
  1190. for (i = 0; i < MAX_INTF_MTUS; i++) {
  1191. if (mtu == priv->intf_mtus[i])
  1192. break;
  1193. }
  1194. if (i >= MAX_INTF_MTUS || !priv->intf_mtu_count[i]) {
  1195. pr_err("%s: No MTU slot found for MTU: %d\n", __func__, mtu);
  1196. return -EINVAL;
  1197. }
  1198. priv->intf_mtu_count[i]--;
  1199. }
  1200. static int rtl930x_l3_mtu_add(struct rtl838x_switch_priv *priv, int mtu)
  1201. {
  1202. int i, free_mtu;
  1203. int mtu_id;
  1204. /* Try to find an existing mtu-value or a free slot */
  1205. free_mtu = MAX_INTF_MTUS;
  1206. for (i = 0; i < MAX_INTF_MTUS && priv->intf_mtus[i] != mtu; i++) {
  1207. if ((!priv->intf_mtu_count[i]) && (free_mtu == MAX_INTF_MTUS))
  1208. free_mtu = i;
  1209. }
  1210. i = (i < MAX_INTF_MTUS) ? i : free_mtu;
  1211. if (i < MAX_INTF_MTUS) {
  1212. mtu_id = i;
  1213. } else {
  1214. pr_err("%s: No free MTU slot available!\n", __func__);
  1215. return -EINVAL;
  1216. }
  1217. priv->intf_mtus[i] = mtu;
  1218. pr_info("Writing MTU %d to slot %d\n", priv->intf_mtus[i], i);
  1219. /* Set MTU-value of the slot TODO: distinguish between IPv4/IPv6 routes / slots */
  1220. sw_w32_mask(0xffff << ((i % 2) * 16), priv->intf_mtus[i] << ((i % 2) * 16),
  1221. RTL930X_L3_IP_MTU_CTRL(i));
  1222. sw_w32_mask(0xffff << ((i % 2) * 16), priv->intf_mtus[i] << ((i % 2) * 16),
  1223. RTL930X_L3_IP6_MTU_CTRL(i));
  1224. priv->intf_mtu_count[i]++;
  1225. return mtu_id;
  1226. }
  1227. /* Creates an interface for a route by setting up the HW tables in the SoC */
  1228. static int rtl930x_l3_intf_add(struct rtl838x_switch_priv *priv, struct rtl838x_l3_intf *intf)
  1229. {
  1230. int i, intf_id, mtu_id;
  1231. /* number of MTU-values < 16384 */
  1232. /* Use the same IPv6 mtu as the ip4 mtu for this route if unset */
  1233. intf->ip6_mtu = intf->ip6_mtu ? intf->ip6_mtu : intf->ip4_mtu;
  1234. mtu_id = rtl930x_l3_mtu_add(priv, intf->ip4_mtu);
  1235. pr_info("%s: added mtu %d with mtu-id %d\n", __func__, intf->ip4_mtu, mtu_id);
  1236. if (mtu_id < 0)
  1237. return -ENOSPC;
  1238. intf->ip4_mtu_id = mtu_id;
  1239. intf->ip6_mtu_id = mtu_id;
  1240. for (i = 0; i < MAX_INTERFACES; i++) {
  1241. if (!priv->interfaces[i])
  1242. break;
  1243. }
  1244. if (i >= MAX_INTERFACES) {
  1245. pr_err("%s: cannot find free interface entry\n", __func__);
  1246. return -EINVAL;
  1247. }
  1248. intf_id = i;
  1249. priv->interfaces[i] = kzalloc(sizeof(struct rtl838x_l3_intf), GFP_KERNEL);
  1250. if (!priv->interfaces[i]) {
  1251. pr_err("%s: no memory to allocate new interface\n", __func__);
  1252. return -ENOMEM;
  1253. }
  1254. }
  1255. /* Set the destination MAC and L3 egress interface ID for a nexthop entry in the SoC's
  1256. * L3_NEXTHOP table. The nexthop entry is identified by idx.
  1257. * dmac_id is the reference to the L2 entry in the L2 forwarding table, special values are
  1258. * 0x7ffe: TRAP2CPU
  1259. * 0x7ffd: TRAP2MASTERCPU
  1260. * 0x7fff: DMAC_ID_DROP
  1261. */
  1262. static void rtl930x_set_l3_nexthop(int idx, u16 dmac_id, u16 interface)
  1263. {
  1264. /* Access L3_NEXTHOP table (3) via register RTL9300_TBL_1 */
  1265. struct table_reg *r = rtl_table_get(RTL9300_TBL_1, 3);
  1266. pr_info("%s: Writing to L3_NEXTHOP table, index %d, dmac_id %d, interface %d\n",
  1267. __func__, idx, dmac_id, interface);
  1268. sw_w32(((dmac_id & 0x7fff) << 7) | (interface & 0x7f), rtl_table_data(r, 0));
  1269. pr_info("%s: %08x\n", __func__, sw_r32(rtl_table_data(r,0)));
  1270. rtl_table_write(r, idx);
  1271. rtl_table_release(r);
  1272. }
  1273. static void rtl930x_pie_lookup_enable(struct rtl838x_switch_priv *priv, int index)
  1274. {
  1275. int block = index / PIE_BLOCK_SIZE;
  1276. sw_w32_mask(0, BIT(block), RTL930X_PIE_BLK_LOOKUP_CTRL);
  1277. }
  1278. /* Reads the intermediate representation of the templated match-fields of the
  1279. * PIE rule in the pie_rule structure and fills in the raw data fields in the
  1280. * raw register space r[].
  1281. * The register space configuration size is identical for the RTL8380/90 and RTL9300,
  1282. * however the RTL9310 has 2 more registers / fields and the physical field-ids are different
  1283. * on all SoCs
  1284. * On the RTL9300 the mask fields are not word-aligend!
  1285. */
  1286. static void rtl930x_write_pie_templated(u32 r[], struct pie_rule *pr, enum template_field_id t[])
  1287. {
  1288. int i;
  1289. enum template_field_id field_type;
  1290. u16 data, data_m;
  1291. for (i = 0; i < N_FIXED_FIELDS; i++) {
  1292. field_type = t[i];
  1293. data = data_m = 0;
  1294. switch (field_type) {
  1295. case TEMPLATE_FIELD_SPM0:
  1296. data = pr->spm;
  1297. data_m = pr->spm_m;
  1298. break;
  1299. case TEMPLATE_FIELD_SPM1:
  1300. data = pr->spm >> 16;
  1301. data_m = pr->spm_m >> 16;
  1302. break;
  1303. case TEMPLATE_FIELD_OTAG:
  1304. data = pr->otag;
  1305. data_m = pr->otag_m;
  1306. break;
  1307. case TEMPLATE_FIELD_SMAC0:
  1308. data = pr->smac[4];
  1309. data = (data << 8) | pr->smac[5];
  1310. data_m = pr->smac_m[4];
  1311. data_m = (data_m << 8) | pr->smac_m[5];
  1312. break;
  1313. case TEMPLATE_FIELD_SMAC1:
  1314. data = pr->smac[2];
  1315. data = (data << 8) | pr->smac[3];
  1316. data_m = pr->smac_m[2];
  1317. data_m = (data_m << 8) | pr->smac_m[3];
  1318. break;
  1319. case TEMPLATE_FIELD_SMAC2:
  1320. data = pr->smac[0];
  1321. data = (data << 8) | pr->smac[1];
  1322. data_m = pr->smac_m[0];
  1323. data_m = (data_m << 8) | pr->smac_m[1];
  1324. break;
  1325. case TEMPLATE_FIELD_DMAC0:
  1326. data = pr->dmac[4];
  1327. data = (data << 8) | pr->dmac[5];
  1328. data_m = pr->dmac_m[4];
  1329. data_m = (data_m << 8) | pr->dmac_m[5];
  1330. break;
  1331. case TEMPLATE_FIELD_DMAC1:
  1332. data = pr->dmac[2];
  1333. data = (data << 8) | pr->dmac[3];
  1334. data_m = pr->dmac_m[2];
  1335. data_m = (data_m << 8) | pr->dmac_m[3];
  1336. break;
  1337. case TEMPLATE_FIELD_DMAC2:
  1338. data = pr->dmac[0];
  1339. data = (data << 8) | pr->dmac[1];
  1340. data_m = pr->dmac_m[0];
  1341. data_m = (data_m << 8) | pr->dmac_m[1];
  1342. break;
  1343. case TEMPLATE_FIELD_ETHERTYPE:
  1344. data = pr->ethertype;
  1345. data_m = pr->ethertype_m;
  1346. break;
  1347. case TEMPLATE_FIELD_ITAG:
  1348. data = pr->itag;
  1349. data_m = pr->itag_m;
  1350. break;
  1351. case TEMPLATE_FIELD_SIP0:
  1352. if (pr->is_ipv6) {
  1353. data = pr->sip6.s6_addr16[7];
  1354. data_m = pr->sip6_m.s6_addr16[7];
  1355. } else {
  1356. data = pr->sip;
  1357. data_m = pr->sip_m;
  1358. }
  1359. break;
  1360. case TEMPLATE_FIELD_SIP1:
  1361. if (pr->is_ipv6) {
  1362. data = pr->sip6.s6_addr16[6];
  1363. data_m = pr->sip6_m.s6_addr16[6];
  1364. } else {
  1365. data = pr->sip >> 16;
  1366. data_m = pr->sip_m >> 16;
  1367. }
  1368. break;
  1369. case TEMPLATE_FIELD_SIP2:
  1370. case TEMPLATE_FIELD_SIP3:
  1371. case TEMPLATE_FIELD_SIP4:
  1372. case TEMPLATE_FIELD_SIP5:
  1373. case TEMPLATE_FIELD_SIP6:
  1374. case TEMPLATE_FIELD_SIP7:
  1375. data = pr->sip6.s6_addr16[5 - (field_type - TEMPLATE_FIELD_SIP2)];
  1376. data_m = pr->sip6_m.s6_addr16[5 - (field_type - TEMPLATE_FIELD_SIP2)];
  1377. break;
  1378. case TEMPLATE_FIELD_DIP0:
  1379. if (pr->is_ipv6) {
  1380. data = pr->dip6.s6_addr16[7];
  1381. data_m = pr->dip6_m.s6_addr16[7];
  1382. } else {
  1383. data = pr->dip;
  1384. data_m = pr->dip_m;
  1385. }
  1386. break;
  1387. case TEMPLATE_FIELD_DIP1:
  1388. if (pr->is_ipv6) {
  1389. data = pr->dip6.s6_addr16[6];
  1390. data_m = pr->dip6_m.s6_addr16[6];
  1391. } else {
  1392. data = pr->dip >> 16;
  1393. data_m = pr->dip_m >> 16;
  1394. }
  1395. break;
  1396. case TEMPLATE_FIELD_DIP2:
  1397. case TEMPLATE_FIELD_DIP3:
  1398. case TEMPLATE_FIELD_DIP4:
  1399. case TEMPLATE_FIELD_DIP5:
  1400. case TEMPLATE_FIELD_DIP6:
  1401. case TEMPLATE_FIELD_DIP7:
  1402. data = pr->dip6.s6_addr16[5 - (field_type - TEMPLATE_FIELD_DIP2)];
  1403. data_m = pr->dip6_m.s6_addr16[5 - (field_type - TEMPLATE_FIELD_DIP2)];
  1404. break;
  1405. case TEMPLATE_FIELD_IP_TOS_PROTO:
  1406. data = pr->tos_proto;
  1407. data_m = pr->tos_proto_m;
  1408. break;
  1409. case TEMPLATE_FIELD_L4_SPORT:
  1410. data = pr->sport;
  1411. data_m = pr->sport_m;
  1412. break;
  1413. case TEMPLATE_FIELD_L4_DPORT:
  1414. data = pr->dport;
  1415. data_m = pr->dport_m;
  1416. break;
  1417. case TEMPLATE_FIELD_DSAP_SSAP:
  1418. data = pr->dsap_ssap;
  1419. data_m = pr->dsap_ssap_m;
  1420. break;
  1421. case TEMPLATE_FIELD_TCP_INFO:
  1422. data = pr->tcp_info;
  1423. data_m = pr->tcp_info_m;
  1424. break;
  1425. case TEMPLATE_FIELD_RANGE_CHK:
  1426. pr_warn("Warning: TEMPLATE_FIELD_RANGE_CHK: not configured\n");
  1427. break;
  1428. default:
  1429. pr_info("%s: unknown field %d\n", __func__, field_type);
  1430. }
  1431. /* On the RTL9300, the mask fields are not word aligned! */
  1432. if (!(i % 2)) {
  1433. r[5 - i / 2] = data;
  1434. r[12 - i / 2] |= ((u32)data_m << 8);
  1435. } else {
  1436. r[5 - i / 2] |= ((u32)data) << 16;
  1437. r[12 - i / 2] |= ((u32)data_m) << 24;
  1438. r[11 - i / 2] |= ((u32)data_m) >> 8;
  1439. }
  1440. }
  1441. }
  1442. static void rtl930x_read_pie_fixed_fields(u32 r[], struct pie_rule *pr)
  1443. {
  1444. pr->stacking_port = r[6] & BIT(31);
  1445. pr->spn = (r[6] >> 24) & 0x7f;
  1446. pr->mgnt_vlan = r[6] & BIT(23);
  1447. if (pr->phase == PHASE_IACL)
  1448. pr->dmac_hit_sw = r[6] & BIT(22);
  1449. else
  1450. pr->content_too_deep = r[6] & BIT(22);
  1451. pr->not_first_frag = r[6] & BIT(21);
  1452. pr->frame_type_l4 = (r[6] >> 18) & 7;
  1453. pr->frame_type = (r[6] >> 16) & 3;
  1454. pr->otag_fmt = (r[6] >> 15) & 1;
  1455. pr->itag_fmt = (r[6] >> 14) & 1;
  1456. pr->otag_exist = (r[6] >> 13) & 1;
  1457. pr->itag_exist = (r[6] >> 12) & 1;
  1458. pr->frame_type_l2 = (r[6] >> 10) & 3;
  1459. pr->igr_normal_port = (r[6] >> 9) & 1;
  1460. pr->tid = (r[6] >> 8) & 1;
  1461. pr->stacking_port_m = r[12] & BIT(7);
  1462. pr->spn_m = r[12] & 0x7f;
  1463. pr->mgnt_vlan_m = r[13] & BIT(31);
  1464. if (pr->phase == PHASE_IACL)
  1465. pr->dmac_hit_sw_m = r[13] & BIT(30);
  1466. else
  1467. pr->content_too_deep_m = r[13] & BIT(30);
  1468. pr->not_first_frag_m = r[13] & BIT(29);
  1469. pr->frame_type_l4_m = (r[13] >> 26) & 7;
  1470. pr->frame_type_m = (r[13] >> 24) & 3;
  1471. pr->otag_fmt_m = r[13] & BIT(23);
  1472. pr->itag_fmt_m = r[13] & BIT(22);
  1473. pr->otag_exist_m = r[13] & BIT(21);
  1474. pr->itag_exist_m = r[13] & BIT (20);
  1475. pr->frame_type_l2_m = (r[13] >> 18) & 3;
  1476. pr->igr_normal_port_m = r[13] & BIT(17);
  1477. pr->tid_m = (r[13] >> 16) & 1;
  1478. pr->valid = r[13] & BIT(15);
  1479. pr->cond_not = r[13] & BIT(14);
  1480. pr->cond_and1 = r[13] & BIT(13);
  1481. pr->cond_and2 = r[13] & BIT(12);
  1482. }
  1483. static void rtl930x_write_pie_fixed_fields(u32 r[], struct pie_rule *pr)
  1484. {
  1485. r[6] = pr->stacking_port ? BIT(31) : 0;
  1486. r[6] |= ((u32) (pr->spn & 0x7f)) << 24;
  1487. r[6] |= pr->mgnt_vlan ? BIT(23) : 0;
  1488. if (pr->phase == PHASE_IACL)
  1489. r[6] |= pr->dmac_hit_sw ? BIT(22) : 0;
  1490. else
  1491. r[6] |= pr->content_too_deep ? BIT(22) : 0;
  1492. r[6] |= pr->not_first_frag ? BIT(21) : 0;
  1493. r[6] |= ((u32) (pr->frame_type_l4 & 0x7)) << 18;
  1494. r[6] |= ((u32) (pr->frame_type & 0x3)) << 16;
  1495. r[6] |= pr->otag_fmt ? BIT(15) : 0;
  1496. r[6] |= pr->itag_fmt ? BIT(14) : 0;
  1497. r[6] |= pr->otag_exist ? BIT(13) : 0;
  1498. r[6] |= pr->itag_exist ? BIT(12) : 0;
  1499. r[6] |= ((u32) (pr->frame_type_l2 & 0x3)) << 10;
  1500. r[6] |= pr->igr_normal_port ? BIT(9) : 0;
  1501. r[6] |= ((u32) (pr->tid & 0x1)) << 8;
  1502. r[12] |= pr->stacking_port_m ? BIT(7) : 0;
  1503. r[12] |= (u32) (pr->spn_m & 0x7f);
  1504. r[13] |= pr->mgnt_vlan_m ? BIT(31) : 0;
  1505. if (pr->phase == PHASE_IACL)
  1506. r[13] |= pr->dmac_hit_sw_m ? BIT(30) : 0;
  1507. else
  1508. r[13] |= pr->content_too_deep_m ? BIT(30) : 0;
  1509. r[13] |= pr->not_first_frag_m ? BIT(29) : 0;
  1510. r[13] |= ((u32) (pr->frame_type_l4_m & 0x7)) << 26;
  1511. r[13] |= ((u32) (pr->frame_type_m & 0x3)) << 24;
  1512. r[13] |= pr->otag_fmt_m ? BIT(23) : 0;
  1513. r[13] |= pr->itag_fmt_m ? BIT(22) : 0;
  1514. r[13] |= pr->otag_exist_m ? BIT(21) : 0;
  1515. r[13] |= pr->itag_exist_m ? BIT(20) : 0;
  1516. r[13] |= ((u32) (pr->frame_type_l2_m & 0x3)) << 18;
  1517. r[13] |= pr->igr_normal_port_m ? BIT(17) : 0;
  1518. r[13] |= ((u32) (pr->tid_m & 0x1)) << 16;
  1519. r[13] |= pr->valid ? BIT(15) : 0;
  1520. r[13] |= pr->cond_not ? BIT(14) : 0;
  1521. r[13] |= pr->cond_and1 ? BIT(13) : 0;
  1522. r[13] |= pr->cond_and2 ? BIT(12) : 0;
  1523. }
  1524. static void rtl930x_write_pie_action(u32 r[], struct pie_rule *pr)
  1525. {
  1526. /* Either drop or forward */
  1527. if (pr->drop) {
  1528. r[14] |= BIT(24) | BIT(25) | BIT(26); /* Do Green, Yellow and Red drops */
  1529. /* Actually DROP, not PERMIT in Green / Yellow / Red */
  1530. r[14] |= BIT(23) | BIT(22) | BIT(20);
  1531. } else {
  1532. r[14] |= pr->fwd_sel ? BIT(27) : 0;
  1533. r[14] |= pr->fwd_act << 18;
  1534. r[14] |= BIT(14); /* We overwrite any drop */
  1535. }
  1536. if (pr->phase == PHASE_VACL)
  1537. r[14] |= pr->fwd_sa_lrn ? BIT(15) : 0;
  1538. r[13] |= pr->bypass_sel ? BIT(5) : 0;
  1539. r[13] |= pr->nopri_sel ? BIT(4) : 0;
  1540. r[13] |= pr->tagst_sel ? BIT(3) : 0;
  1541. r[13] |= pr->ovid_sel ? BIT(1) : 0;
  1542. r[14] |= pr->ivid_sel ? BIT(31) : 0;
  1543. r[14] |= pr->meter_sel ? BIT(30) : 0;
  1544. r[14] |= pr->mir_sel ? BIT(29) : 0;
  1545. r[14] |= pr->log_sel ? BIT(28) : 0;
  1546. r[14] |= ((u32)(pr->fwd_data & 0x3fff)) << 3;
  1547. r[15] |= pr->log_octets ? BIT(31) : 0;
  1548. r[15] |= (u32)(pr->meter_data) << 23;
  1549. r[15] |= ((u32)(pr->ivid_act) << 21) & 0x3;
  1550. r[15] |= ((u32)(pr->ivid_data) << 9) & 0xfff;
  1551. r[16] |= ((u32)(pr->ovid_act) << 30) & 0x3;
  1552. r[16] |= ((u32)(pr->ovid_data) & 0xfff) << 16;
  1553. r[16] |= (pr->mir_data & 0x3) << 6;
  1554. r[17] |= ((u32)(pr->tagst_data) & 0xf) << 28;
  1555. r[17] |= ((u32)(pr->nopri_data) & 0x7) << 25;
  1556. r[17] |= pr->bypass_ibc_sc ? BIT(16) : 0;
  1557. }
  1558. void rtl930x_pie_rule_dump_raw(u32 r[])
  1559. {
  1560. pr_info("Raw IACL table entry:\n");
  1561. pr_info("r 0 - 7: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  1562. r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7]);
  1563. pr_info("r 8 - 15: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  1564. r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]);
  1565. pr_info("r 16 - 18: %08x %08x %08x\n", r[16], r[17], r[18]);
  1566. pr_info("Match : %08x %08x %08x %08x %08x %08x\n", r[0], r[1], r[2], r[3], r[4], r[5]);
  1567. pr_info("Fixed : %06x\n", r[6] >> 8);
  1568. pr_info("Match M: %08x %08x %08x %08x %08x %08x\n",
  1569. (r[6] << 24) | (r[7] >> 8), (r[7] << 24) | (r[8] >> 8), (r[8] << 24) | (r[9] >> 8),
  1570. (r[9] << 24) | (r[10] >> 8), (r[10] << 24) | (r[11] >> 8),
  1571. (r[11] << 24) | (r[12] >> 8));
  1572. pr_info("R[13]: %08x\n", r[13]);
  1573. pr_info("Fixed M: %06x\n", ((r[12] << 16) | (r[13] >> 16)) & 0xffffff);
  1574. pr_info("Valid / not / and1 / and2 : %1x\n", (r[13] >> 12) & 0xf);
  1575. pr_info("r 13-16: %08x %08x %08x %08x\n", r[13], r[14], r[15], r[16]);
  1576. }
  1577. static int rtl930x_pie_rule_write(struct rtl838x_switch_priv *priv, int idx, struct pie_rule *pr)
  1578. {
  1579. /* Access IACL table (2) via register 0 */
  1580. struct table_reg *q = rtl_table_get(RTL9300_TBL_0, 2);
  1581. u32 r[19];
  1582. int i;
  1583. int block = idx / PIE_BLOCK_SIZE;
  1584. u32 t_select = sw_r32(RTL930X_PIE_BLK_TMPLTE_CTRL(block));
  1585. pr_debug("%s: %d, t_select: %08x\n", __func__, idx, t_select);
  1586. for (i = 0; i < 19; i++)
  1587. r[i] = 0;
  1588. if (!pr->valid) {
  1589. rtl_table_write(q, idx);
  1590. rtl_table_release(q);
  1591. return 0;
  1592. }
  1593. rtl930x_write_pie_fixed_fields(r, pr);
  1594. pr_debug("%s: template %d\n", __func__, (t_select >> (pr->tid * 4)) & 0xf);
  1595. rtl930x_write_pie_templated(r, pr, fixed_templates[(t_select >> (pr->tid * 4)) & 0xf]);
  1596. rtl930x_write_pie_action(r, pr);
  1597. /* rtl930x_pie_rule_dump_raw(r); */
  1598. for (i = 0; i < 19; i++)
  1599. sw_w32(r[i], rtl_table_data(q, i));
  1600. rtl_table_write(q, idx);
  1601. rtl_table_release(q);
  1602. return 0;
  1603. }
  1604. static bool rtl930x_pie_templ_has(int t, enum template_field_id field_type)
  1605. {
  1606. int i;
  1607. enum template_field_id ft;
  1608. for (i = 0; i < N_FIXED_FIELDS; i++) {
  1609. ft = fixed_templates[t][i];
  1610. if (field_type == ft)
  1611. return true;
  1612. }
  1613. return false;
  1614. }
  1615. /* Verify that the rule pr is compatible with a given template t in block block
  1616. * Note that this function is SoC specific since the values of e.g. TEMPLATE_FIELD_SIP0
  1617. * depend on the SoC
  1618. */
  1619. static int rtl930x_pie_verify_template(struct rtl838x_switch_priv *priv,
  1620. struct pie_rule *pr, int t, int block)
  1621. {
  1622. int i;
  1623. if (!pr->is_ipv6 && pr->sip_m && !rtl930x_pie_templ_has(t, TEMPLATE_FIELD_SIP0))
  1624. return -1;
  1625. if (!pr->is_ipv6 && pr->dip_m && !rtl930x_pie_templ_has(t, TEMPLATE_FIELD_DIP0))
  1626. return -1;
  1627. if (pr->is_ipv6) {
  1628. if ((pr->sip6_m.s6_addr32[0] ||
  1629. pr->sip6_m.s6_addr32[1] ||
  1630. pr->sip6_m.s6_addr32[2] ||
  1631. pr->sip6_m.s6_addr32[3]) &&
  1632. !rtl930x_pie_templ_has(t, TEMPLATE_FIELD_SIP2))
  1633. return -1;
  1634. if ((pr->dip6_m.s6_addr32[0] ||
  1635. pr->dip6_m.s6_addr32[1] ||
  1636. pr->dip6_m.s6_addr32[2] ||
  1637. pr->dip6_m.s6_addr32[3]) &&
  1638. !rtl930x_pie_templ_has(t, TEMPLATE_FIELD_DIP2))
  1639. return -1;
  1640. }
  1641. if (ether_addr_to_u64(pr->smac) && !rtl930x_pie_templ_has(t, TEMPLATE_FIELD_SMAC0))
  1642. return -1;
  1643. if (ether_addr_to_u64(pr->dmac) && !rtl930x_pie_templ_has(t, TEMPLATE_FIELD_DMAC0))
  1644. return -1;
  1645. /* TODO: Check more */
  1646. i = find_first_zero_bit(&priv->pie_use_bm[block * 4], PIE_BLOCK_SIZE);
  1647. if (i >= PIE_BLOCK_SIZE)
  1648. return -1;
  1649. return i + PIE_BLOCK_SIZE * block;
  1650. }
  1651. static int rtl930x_pie_rule_add(struct rtl838x_switch_priv *priv, struct pie_rule *pr)
  1652. {
  1653. int idx, block, j, t;
  1654. int min_block = 0;
  1655. int max_block = priv->n_pie_blocks / 2;
  1656. if (pr->is_egress) {
  1657. min_block = max_block;
  1658. max_block = priv->n_pie_blocks;
  1659. }
  1660. pr_debug("In %s\n", __func__);
  1661. mutex_lock(&priv->pie_mutex);
  1662. for (block = min_block; block < max_block; block++) {
  1663. for (j = 0; j < 2; j++) {
  1664. t = (sw_r32(RTL930X_PIE_BLK_TMPLTE_CTRL(block)) >> (j * 4)) & 0xf;
  1665. pr_debug("Testing block %d, template %d, template id %d\n", block, j, t);
  1666. pr_debug("%s: %08x\n",
  1667. __func__, sw_r32(RTL930X_PIE_BLK_TMPLTE_CTRL(block)));
  1668. idx = rtl930x_pie_verify_template(priv, pr, t, block);
  1669. if (idx >= 0)
  1670. break;
  1671. }
  1672. if (j < 2)
  1673. break;
  1674. }
  1675. if (block >= priv->n_pie_blocks) {
  1676. mutex_unlock(&priv->pie_mutex);
  1677. return -EOPNOTSUPP;
  1678. }
  1679. pr_debug("Using block: %d, index %d, template-id %d\n", block, idx, j);
  1680. set_bit(idx, priv->pie_use_bm);
  1681. pr->valid = true;
  1682. pr->tid = j; /* Mapped to template number */
  1683. pr->tid_m = 0x1;
  1684. pr->id = idx;
  1685. rtl930x_pie_lookup_enable(priv, idx);
  1686. rtl930x_pie_rule_write(priv, idx, pr);
  1687. mutex_unlock(&priv->pie_mutex);
  1688. return 0;
  1689. }
  1690. /* Delete a range of Packet Inspection Engine rules */
  1691. static int rtl930x_pie_rule_del(struct rtl838x_switch_priv *priv, int index_from, int index_to)
  1692. {
  1693. u32 v = (index_from << 1)| (index_to << 12 ) | BIT(0);
  1694. pr_debug("%s: from %d to %d\n", __func__, index_from, index_to);
  1695. mutex_lock(&priv->reg_mutex);
  1696. /* Write from-to and execute bit into control register */
  1697. sw_w32(v, RTL930X_PIE_CLR_CTRL);
  1698. /* Wait until command has completed */
  1699. do {
  1700. } while (sw_r32(RTL930X_PIE_CLR_CTRL) & BIT(0));
  1701. mutex_unlock(&priv->reg_mutex);
  1702. return 0;
  1703. }
  1704. static void rtl930x_pie_rule_rm(struct rtl838x_switch_priv *priv, struct pie_rule *pr)
  1705. {
  1706. int idx = pr->id;
  1707. rtl930x_pie_rule_del(priv, idx, idx);
  1708. clear_bit(idx, priv->pie_use_bm);
  1709. }
  1710. static void rtl930x_pie_init(struct rtl838x_switch_priv *priv)
  1711. {
  1712. int i;
  1713. u32 template_selectors;
  1714. mutex_init(&priv->pie_mutex);
  1715. pr_info("%s\n", __func__);
  1716. /* Enable ACL lookup on all ports, including CPU_PORT */
  1717. for (i = 0; i <= priv->cpu_port; i++)
  1718. sw_w32(1, RTL930X_ACL_PORT_LOOKUP_CTRL(i));
  1719. /* Include IPG in metering */
  1720. sw_w32_mask(0, 1, RTL930X_METER_GLB_CTRL);
  1721. /* Delete all present rules, block size is 128 on all SoC families */
  1722. rtl930x_pie_rule_del(priv, 0, priv->n_pie_blocks * 128 - 1);
  1723. /* Assign blocks 0-7 to VACL phase (bit = 0), blocks 8-15 to IACL (bit = 1) */
  1724. sw_w32(0xff00, RTL930X_PIE_BLK_PHASE_CTRL);
  1725. /* Enable predefined templates 0, 1 for first quarter of all blocks */
  1726. template_selectors = 0 | (1 << 4);
  1727. for (i = 0; i < priv->n_pie_blocks / 4; i++)
  1728. sw_w32(template_selectors, RTL930X_PIE_BLK_TMPLTE_CTRL(i));
  1729. /* Enable predefined templates 2, 3 for second quarter of all blocks */
  1730. template_selectors = 2 | (3 << 4);
  1731. for (i = priv->n_pie_blocks / 4; i < priv->n_pie_blocks / 2; i++)
  1732. sw_w32(template_selectors, RTL930X_PIE_BLK_TMPLTE_CTRL(i));
  1733. /* Enable predefined templates 0, 1 for third half of all blocks */
  1734. template_selectors = 0 | (1 << 4);
  1735. for (i = priv->n_pie_blocks / 2; i < priv->n_pie_blocks * 3 / 4; i++)
  1736. sw_w32(template_selectors, RTL930X_PIE_BLK_TMPLTE_CTRL(i));
  1737. /* Enable predefined templates 2, 3 for fourth quater of all blocks */
  1738. template_selectors = 2 | (3 << 4);
  1739. for (i = priv->n_pie_blocks * 3 / 4; i < priv->n_pie_blocks; i++)
  1740. sw_w32(template_selectors, RTL930X_PIE_BLK_TMPLTE_CTRL(i));
  1741. }
  1742. /* Sets up an egress interface for L3 actions
  1743. * Actions for ip4/6_icmp_redirect, ip4/6_pbr_icmp_redirect are:
  1744. * 0: FORWARD, 1: DROP, 2: TRAP2CPU, 3: COPY2CPU, 4: TRAP2MASTERCPU 5: COPY2MASTERCPU
  1745. * 6: HARDDROP
  1746. * idx is the index in the HW interface table: idx < 0x80
  1747. */
  1748. static void rtl930x_set_l3_egress_intf(int idx, struct rtl838x_l3_intf *intf)
  1749. {
  1750. u32 u, v;
  1751. /* Read L3_EGR_INTF table (4) via register RTL9300_TBL_1 */
  1752. struct table_reg *r = rtl_table_get(RTL9300_TBL_1, 4);
  1753. /* The table has 2 registers */
  1754. u = (intf->vid & 0xfff) << 9;
  1755. u |= (intf->smac_idx & 0x3f) << 3;
  1756. u |= (intf->ip4_mtu_id & 0x7);
  1757. v = (intf->ip6_mtu_id & 0x7) << 28;
  1758. v |= (intf->ttl_scope & 0xff) << 20;
  1759. v |= (intf->hl_scope & 0xff) << 12;
  1760. v |= (intf->ip4_icmp_redirect & 0x7) << 9;
  1761. v |= (intf->ip6_icmp_redirect & 0x7)<< 6;
  1762. v |= (intf->ip4_pbr_icmp_redirect & 0x7) << 3;
  1763. v |= (intf->ip6_pbr_icmp_redirect & 0x7);
  1764. sw_w32(u, rtl_table_data(r, 0));
  1765. sw_w32(v, rtl_table_data(r, 1));
  1766. pr_info("%s writing to index %d: %08x %08x\n", __func__, idx, u, v);
  1767. rtl_table_write(r, idx & 0x7f);
  1768. rtl_table_release(r);
  1769. }
  1770. /* Reads a MAC entry for L3 termination as entry point for routing
  1771. * from the hardware table
  1772. * idx is the index into the L3_ROUTER_MAC table
  1773. */
  1774. static void rtl930x_get_l3_router_mac(u32 idx, struct rtl93xx_rt_mac *m)
  1775. {
  1776. u32 v, w;
  1777. /* Read L3_ROUTER_MAC table (0) via register RTL9300_TBL_1 */
  1778. struct table_reg *r = rtl_table_get(RTL9300_TBL_1, 0);
  1779. rtl_table_read(r, idx);
  1780. /* The table has a size of 7 registers, 64 entries */
  1781. v = sw_r32(rtl_table_data(r, 0));
  1782. w = sw_r32(rtl_table_data(r, 3));
  1783. m->valid = !!(v & BIT(20));
  1784. if (!m->valid)
  1785. goto out;
  1786. m->p_type = !!(v & BIT(19));
  1787. m->p_id = (v >> 13) & 0x3f; /* trunk id of port */
  1788. m->vid = v & 0xfff;
  1789. m->vid_mask = w & 0xfff;
  1790. m->action = sw_r32(rtl_table_data(r, 6)) & 0x7;
  1791. m->mac_mask = ((((u64)sw_r32(rtl_table_data(r, 5))) << 32) & 0xffffffffffffULL) |
  1792. (sw_r32(rtl_table_data(r, 4)));
  1793. m->mac = ((((u64)sw_r32(rtl_table_data(r, 1))) << 32) & 0xffffffffffffULL) |
  1794. (sw_r32(rtl_table_data(r, 2)));
  1795. /* Bits L3_INTF and BMSK_L3_INTF are 0 */
  1796. out:
  1797. rtl_table_release(r);
  1798. }
  1799. /* Writes a MAC entry for L3 termination as entry point for routing
  1800. * into the hardware table
  1801. * idx is the index into the L3_ROUTER_MAC table
  1802. */
  1803. static void rtl930x_set_l3_router_mac(u32 idx, struct rtl93xx_rt_mac *m)
  1804. {
  1805. u32 v, w;
  1806. /* Read L3_ROUTER_MAC table (0) via register RTL9300_TBL_1 */
  1807. struct table_reg *r = rtl_table_get(RTL9300_TBL_1, 0);
  1808. /* The table has a size of 7 registers, 64 entries */
  1809. v = BIT(20); /* mac entry valid, port type is 0: individual */
  1810. v |= (m->p_id & 0x3f) << 13;
  1811. v |= (m->vid & 0xfff); /* Set the interface_id to the vlan id */
  1812. w = m->vid_mask;
  1813. w |= (m->p_id_mask & 0x3f) << 13;
  1814. sw_w32(v, rtl_table_data(r, 0));
  1815. sw_w32(w, rtl_table_data(r, 3));
  1816. /* Set MAC address, L3_INTF (bit 12 in register 1) needs to be 0 */
  1817. sw_w32((u32)(m->mac), rtl_table_data(r, 2));
  1818. sw_w32(m->mac >> 32, rtl_table_data(r, 1));
  1819. /* Set MAC address mask, BMSK_L3_INTF (bit 12 in register 5) needs to be 0 */
  1820. sw_w32((u32)(m->mac_mask >> 32), rtl_table_data(r, 4));
  1821. sw_w32((u32)m->mac_mask, rtl_table_data(r, 5));
  1822. sw_w32(m->action & 0x7, rtl_table_data(r, 6));
  1823. pr_debug("%s writing index %d: %08x %08x %08x %08x %08x %08x %08x\n", __func__, idx,
  1824. sw_r32(rtl_table_data(r, 0)), sw_r32(rtl_table_data(r, 1)), sw_r32(rtl_table_data(r, 2)),
  1825. sw_r32(rtl_table_data(r, 3)), sw_r32(rtl_table_data(r, 4)), sw_r32(rtl_table_data(r, 5)),
  1826. sw_r32(rtl_table_data(r, 6))
  1827. );
  1828. rtl_table_write(r, idx);
  1829. rtl_table_release(r);
  1830. }
  1831. /* Get the Destination-MAC of an L3 egress interface or the Source MAC for routed packets
  1832. * from the SoC's L3_EGR_INTF_MAC table
  1833. * Indexes 0-2047 are DMACs, 2048+ are SMACs
  1834. */
  1835. static u64 rtl930x_get_l3_egress_mac(u32 idx)
  1836. {
  1837. u64 mac;
  1838. /* Read L3_EGR_INTF_MAC table (2) via register RTL9300_TBL_2 */
  1839. struct table_reg *r = rtl_table_get(RTL9300_TBL_2, 2);
  1840. rtl_table_read(r, idx);
  1841. /* The table has a size of 2 registers */
  1842. mac = sw_r32(rtl_table_data(r, 0));
  1843. mac <<= 32;
  1844. mac |= sw_r32(rtl_table_data(r, 1));
  1845. rtl_table_release(r);
  1846. return mac;
  1847. }
  1848. /* Set the Destination-MAC of a route or the Source MAC of an L3 egress interface
  1849. * in the SoC's L3_EGR_INTF_MAC table
  1850. * Indexes 0-2047 are DMACs, 2048+ are SMACs
  1851. */
  1852. static void rtl930x_set_l3_egress_mac(u32 idx, u64 mac)
  1853. {
  1854. /* Access L3_EGR_INTF_MAC table (2) via register RTL9300_TBL_2 */
  1855. struct table_reg *r = rtl_table_get(RTL9300_TBL_2, 2);
  1856. /* The table has a size of 2 registers */
  1857. sw_w32(mac >> 32, rtl_table_data(r, 0));
  1858. sw_w32(mac, rtl_table_data(r, 1));
  1859. pr_debug("%s: setting index %d to %016llx\n", __func__, idx, mac);
  1860. rtl_table_write(r, idx);
  1861. rtl_table_release(r);
  1862. }
  1863. /* Configure L3 routing settings of the device:
  1864. * - MTUs
  1865. * - Egress interface
  1866. * - The router's MAC address on which routed packets are expected
  1867. * - MAC addresses used as source macs of routed packets
  1868. */
  1869. int rtl930x_l3_setup(struct rtl838x_switch_priv *priv)
  1870. {
  1871. int i;
  1872. /* Setup MTU with id 0 for default interface */
  1873. for (i = 0; i < MAX_INTF_MTUS; i++)
  1874. priv->intf_mtu_count[i] = priv->intf_mtus[i] = 0;
  1875. priv->intf_mtu_count[0] = 0; /* Needs to stay forever */
  1876. priv->intf_mtus[0] = DEFAULT_MTU;
  1877. sw_w32_mask(0xffff, DEFAULT_MTU, RTL930X_L3_IP_MTU_CTRL(0));
  1878. sw_w32_mask(0xffff, DEFAULT_MTU, RTL930X_L3_IP6_MTU_CTRL(0));
  1879. priv->intf_mtus[1] = DEFAULT_MTU;
  1880. sw_w32_mask(0xffff0000, DEFAULT_MTU << 16, RTL930X_L3_IP_MTU_CTRL(0));
  1881. sw_w32_mask(0xffff0000, DEFAULT_MTU << 16, RTL930X_L3_IP6_MTU_CTRL(0));
  1882. sw_w32_mask(0xffff, DEFAULT_MTU, RTL930X_L3_IP_MTU_CTRL(1));
  1883. sw_w32_mask(0xffff, DEFAULT_MTU, RTL930X_L3_IP6_MTU_CTRL(1));
  1884. sw_w32_mask(0xffff0000, DEFAULT_MTU << 16, RTL930X_L3_IP_MTU_CTRL(1));
  1885. sw_w32_mask(0xffff0000, DEFAULT_MTU << 16, RTL930X_L3_IP6_MTU_CTRL(1));
  1886. /* Clear all source port MACs */
  1887. for (i = 0; i < MAX_SMACS; i++)
  1888. rtl930x_set_l3_egress_mac(L3_EGRESS_DMACS + i, 0ULL);
  1889. /* Configure the default L3 hash algorithm */
  1890. sw_w32_mask(BIT(2), 0, RTL930X_L3_HOST_TBL_CTRL); /* Algorithm selection 0 = 0 */
  1891. sw_w32_mask(0, BIT(3), RTL930X_L3_HOST_TBL_CTRL); /* Algorithm selection 1 = 1 */
  1892. pr_info("L3_IPUC_ROUTE_CTRL %08x, IPMC_ROUTE %08x, IP6UC_ROUTE %08x, IP6MC_ROUTE %08x\n",
  1893. sw_r32(RTL930X_L3_IPUC_ROUTE_CTRL), sw_r32(RTL930X_L3_IPMC_ROUTE_CTRL),
  1894. sw_r32(RTL930X_L3_IP6UC_ROUTE_CTRL), sw_r32(RTL930X_L3_IP6MC_ROUTE_CTRL));
  1895. sw_w32_mask(0, 1, RTL930X_L3_IPUC_ROUTE_CTRL);
  1896. sw_w32_mask(0, 1, RTL930X_L3_IP6UC_ROUTE_CTRL);
  1897. sw_w32_mask(0, 1, RTL930X_L3_IPMC_ROUTE_CTRL);
  1898. sw_w32_mask(0, 1, RTL930X_L3_IP6MC_ROUTE_CTRL);
  1899. sw_w32(0x00002001, RTL930X_L3_IPUC_ROUTE_CTRL);
  1900. sw_w32(0x00014581, RTL930X_L3_IP6UC_ROUTE_CTRL);
  1901. sw_w32(0x00000501, RTL930X_L3_IPMC_ROUTE_CTRL);
  1902. sw_w32(0x00012881, RTL930X_L3_IP6MC_ROUTE_CTRL);
  1903. pr_info("L3_IPUC_ROUTE_CTRL %08x, IPMC_ROUTE %08x, IP6UC_ROUTE %08x, IP6MC_ROUTE %08x\n",
  1904. sw_r32(RTL930X_L3_IPUC_ROUTE_CTRL), sw_r32(RTL930X_L3_IPMC_ROUTE_CTRL),
  1905. sw_r32(RTL930X_L3_IP6UC_ROUTE_CTRL), sw_r32(RTL930X_L3_IP6MC_ROUTE_CTRL));
  1906. /* Trap non-ip traffic to the CPU-port (e.g. ARP so we stay reachable) */
  1907. sw_w32_mask(0x3 << 8, 0x1 << 8, RTL930X_L3_IP_ROUTE_CTRL);
  1908. pr_info("L3_IP_ROUTE_CTRL %08x\n", sw_r32(RTL930X_L3_IP_ROUTE_CTRL));
  1909. /* PORT_ISO_RESTRICT_ROUTE_CTRL? */
  1910. /* Do not use prefix route 0 because of HW limitations */
  1911. set_bit(0, priv->route_use_bm);
  1912. return 0;
  1913. }
  1914. static u32 rtl930x_packet_cntr_read(int counter)
  1915. {
  1916. u32 v;
  1917. /* Read LOG table (3) via register RTL9300_TBL_0 */
  1918. struct table_reg *r = rtl_table_get(RTL9300_TBL_0, 3);
  1919. pr_debug("In %s, id %d\n", __func__, counter);
  1920. rtl_table_read(r, counter / 2);
  1921. pr_debug("Registers: %08x %08x\n",
  1922. sw_r32(rtl_table_data(r, 0)), sw_r32(rtl_table_data(r, 1)));
  1923. /* The table has a size of 2 registers */
  1924. if (counter % 2)
  1925. v = sw_r32(rtl_table_data(r, 0));
  1926. else
  1927. v = sw_r32(rtl_table_data(r, 1));
  1928. rtl_table_release(r);
  1929. return v;
  1930. }
  1931. static void rtl930x_packet_cntr_clear(int counter)
  1932. {
  1933. /* Access LOG table (3) via register RTL9300_TBL_0 */
  1934. struct table_reg *r = rtl_table_get(RTL9300_TBL_0, 3);
  1935. pr_info("In %s, id %d\n", __func__, counter);
  1936. /* The table has a size of 2 registers */
  1937. if (counter % 2)
  1938. sw_w32(0, rtl_table_data(r, 0));
  1939. else
  1940. sw_w32(0, rtl_table_data(r, 1));
  1941. rtl_table_write(r, counter / 2);
  1942. rtl_table_release(r);
  1943. }
  1944. void rtl930x_vlan_port_keep_tag_set(int port, bool keep_outer, bool keep_inner)
  1945. {
  1946. sw_w32(FIELD_PREP(RTL930X_VLAN_PORT_TAG_STS_CTRL_EGR_OTAG_STS_MASK,
  1947. keep_outer ? RTL930X_VLAN_PORT_TAG_STS_TAGGED : RTL930X_VLAN_PORT_TAG_STS_UNTAG) |
  1948. FIELD_PREP(RTL930X_VLAN_PORT_TAG_STS_CTRL_EGR_ITAG_STS_MASK,
  1949. keep_inner ? RTL930X_VLAN_PORT_TAG_STS_TAGGED : RTL930X_VLAN_PORT_TAG_STS_UNTAG),
  1950. RTL930X_VLAN_PORT_TAG_STS_CTRL(port));
  1951. }
  1952. void rtl930x_vlan_port_pvidmode_set(int port, enum pbvlan_type type, enum pbvlan_mode mode)
  1953. {
  1954. if (type == PBVLAN_TYPE_INNER)
  1955. sw_w32_mask(0x3, mode, RTL930X_VLAN_PORT_PB_VLAN + (port << 2));
  1956. else
  1957. sw_w32_mask(0x3 << 14, mode << 14 ,RTL930X_VLAN_PORT_PB_VLAN + (port << 2));
  1958. }
  1959. void rtl930x_vlan_port_pvid_set(int port, enum pbvlan_type type, int pvid)
  1960. {
  1961. if (type == PBVLAN_TYPE_INNER)
  1962. sw_w32_mask(0xfff << 2, pvid << 2, RTL930X_VLAN_PORT_PB_VLAN + (port << 2));
  1963. else
  1964. sw_w32_mask(0xfff << 16, pvid << 16, RTL930X_VLAN_PORT_PB_VLAN + (port << 2));
  1965. }
  1966. static int rtl930x_set_ageing_time(unsigned long msec)
  1967. {
  1968. int t = sw_r32(RTL930X_L2_AGE_CTRL);
  1969. t &= 0x1FFFFF;
  1970. t = (t * 7) / 10;
  1971. pr_debug("L2 AGING time: %d sec\n", t);
  1972. t = (msec / 100 + 6) / 7;
  1973. t = t > 0x1FFFFF ? 0x1FFFFF : t;
  1974. sw_w32_mask(0x1FFFFF, t, RTL930X_L2_AGE_CTRL);
  1975. pr_debug("Dynamic aging for ports: %x\n", sw_r32(RTL930X_L2_PORT_AGE_CTRL));
  1976. return 0;
  1977. }
  1978. static void rtl930x_set_igr_filter(int port, enum igr_filter state)
  1979. {
  1980. sw_w32_mask(0x3 << ((port & 0xf)<<1), state << ((port & 0xf)<<1),
  1981. RTL930X_VLAN_PORT_IGR_FLTR + (((port >> 4) << 2)));
  1982. }
  1983. static void rtl930x_set_egr_filter(int port, enum egr_filter state)
  1984. {
  1985. sw_w32_mask(0x1 << (port % 0x1D), state << (port % 0x1D),
  1986. RTL930X_VLAN_PORT_EGR_FLTR + (((port / 29) << 2)));
  1987. }
  1988. void rtl930x_set_distribution_algorithm(int group, int algoidx, u32 algomsk)
  1989. {
  1990. u32 l3shift = 0;
  1991. u32 newmask = 0;
  1992. /* TODO: for now we set algoidx to 0 */
  1993. algoidx = 0;
  1994. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SIP_BIT) {
  1995. l3shift = 4;
  1996. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SIP_BIT;
  1997. }
  1998. if (algomsk & TRUNK_DISTRIBUTION_ALGO_DIP_BIT) {
  1999. l3shift = 4;
  2000. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_DIP_BIT;
  2001. }
  2002. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SRC_L4PORT_BIT) {
  2003. l3shift = 4;
  2004. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SRC_L4PORT_BIT;
  2005. }
  2006. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SRC_L4PORT_BIT) {
  2007. l3shift = 4;
  2008. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SRC_L4PORT_BIT;
  2009. }
  2010. if (l3shift == 4) {
  2011. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SMAC_BIT)
  2012. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SMAC_BIT;
  2013. if (algomsk & TRUNK_DISTRIBUTION_ALGO_DMAC_BIT)
  2014. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_DMAC_BIT;
  2015. } else {
  2016. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SMAC_BIT)
  2017. newmask |= TRUNK_DISTRIBUTION_ALGO_L2_SMAC_BIT;
  2018. if (algomsk & TRUNK_DISTRIBUTION_ALGO_DMAC_BIT)
  2019. newmask |= TRUNK_DISTRIBUTION_ALGO_L2_DMAC_BIT;
  2020. }
  2021. sw_w32(newmask << l3shift, RTL930X_TRK_HASH_CTRL + (algoidx << 2));
  2022. }
  2023. static void rtl930x_led_init(struct rtl838x_switch_priv *priv)
  2024. {
  2025. int i, pos;
  2026. u32 v, pm = 0, set;
  2027. u32 setlen;
  2028. const __be32 *led_set;
  2029. char set_name[9];
  2030. struct device_node *node;
  2031. pr_info("%s called\n", __func__);
  2032. node = of_find_compatible_node(NULL, NULL, "realtek,rtl9300-leds");
  2033. if (!node) {
  2034. pr_info("%s No compatible LED node found\n", __func__);
  2035. return;
  2036. }
  2037. for (i = 0; i < priv->cpu_port; i++) {
  2038. pos = (i << 1) % 32;
  2039. sw_w32_mask(0x3 << pos, 0, RTL930X_LED_PORT_FIB_SET_SEL_CTRL(i));
  2040. sw_w32_mask(0x3 << pos, 0, RTL930X_LED_PORT_COPR_SET_SEL_CTRL(i));
  2041. if (!priv->ports[i].phy)
  2042. continue;
  2043. v = 0x1;
  2044. if (priv->ports[i].is10G)
  2045. v = 0x3;
  2046. if (priv->ports[i].phy_is_integrated)
  2047. v = 0x1;
  2048. sw_w32_mask(0x3 << pos, v << pos, RTL930X_LED_PORT_NUM_CTRL(i));
  2049. pm |= BIT(i);
  2050. set = priv->ports[i].led_set;
  2051. sw_w32_mask(0, set << pos, RTL930X_LED_PORT_COPR_SET_SEL_CTRL(i));
  2052. sw_w32_mask(0, set << pos, RTL930X_LED_PORT_FIB_SET_SEL_CTRL(i));
  2053. }
  2054. for (i = 0; i < 4; i++) {
  2055. sprintf(set_name, "led_set%d", i);
  2056. led_set = of_get_property(node, set_name, &setlen);
  2057. if (!led_set || setlen != 16)
  2058. break;
  2059. v = be32_to_cpup(led_set) << 16 | be32_to_cpup(led_set + 1);
  2060. sw_w32(v, RTL930X_LED_SET0_0_CTRL - 4 - i * 8);
  2061. v = be32_to_cpup(led_set + 2) << 16 | be32_to_cpup(led_set + 3);
  2062. sw_w32(v, RTL930X_LED_SET0_0_CTRL - i * 8);
  2063. }
  2064. /* Set LED mode to serial (0x1) */
  2065. sw_w32_mask(0x3, 0x1, RTL930X_LED_GLB_CTRL);
  2066. /* Set port type masks */
  2067. sw_w32(pm, RTL930X_LED_PORT_COPR_MASK_CTRL);
  2068. sw_w32(pm, RTL930X_LED_PORT_FIB_MASK_CTRL);
  2069. sw_w32(pm, RTL930X_LED_PORT_COMBO_MASK_CTRL);
  2070. for (i = 0; i < 24; i++)
  2071. pr_info("%s %08x: %08x\n",__func__, 0xbb00cc00 + i * 4, sw_r32(0xcc00 + i * 4));
  2072. }
  2073. const struct rtl838x_reg rtl930x_reg = {
  2074. .mask_port_reg_be = rtl838x_mask_port_reg,
  2075. .set_port_reg_be = rtl838x_set_port_reg,
  2076. .get_port_reg_be = rtl838x_get_port_reg,
  2077. .mask_port_reg_le = rtl838x_mask_port_reg,
  2078. .set_port_reg_le = rtl838x_set_port_reg,
  2079. .get_port_reg_le = rtl838x_get_port_reg,
  2080. .stat_port_rst = RTL930X_STAT_PORT_RST,
  2081. .stat_rst = RTL930X_STAT_RST,
  2082. .stat_port_std_mib = RTL930X_STAT_PORT_MIB_CNTR,
  2083. .traffic_enable = rtl930x_traffic_enable,
  2084. .traffic_disable = rtl930x_traffic_disable,
  2085. .traffic_get = rtl930x_traffic_get,
  2086. .traffic_set = rtl930x_traffic_set,
  2087. .l2_ctrl_0 = RTL930X_L2_CTRL,
  2088. .l2_ctrl_1 = RTL930X_L2_AGE_CTRL,
  2089. .l2_port_aging_out = RTL930X_L2_PORT_AGE_CTRL,
  2090. .set_ageing_time = rtl930x_set_ageing_time,
  2091. .smi_poll_ctrl = RTL930X_SMI_POLL_CTRL, /* TODO: Difference to RTL9300_SMI_PRVTE_POLLING_CTRL */
  2092. .l2_tbl_flush_ctrl = RTL930X_L2_TBL_FLUSH_CTRL,
  2093. .exec_tbl0_cmd = rtl930x_exec_tbl0_cmd,
  2094. .exec_tbl1_cmd = rtl930x_exec_tbl1_cmd,
  2095. .tbl_access_data_0 = rtl930x_tbl_access_data_0,
  2096. .isr_glb_src = RTL930X_ISR_GLB,
  2097. .isr_port_link_sts_chg = RTL930X_ISR_PORT_LINK_STS_CHG,
  2098. .imr_port_link_sts_chg = RTL930X_IMR_PORT_LINK_STS_CHG,
  2099. .imr_glb = RTL930X_IMR_GLB,
  2100. .vlan_tables_read = rtl930x_vlan_tables_read,
  2101. .vlan_set_tagged = rtl930x_vlan_set_tagged,
  2102. .vlan_set_untagged = rtl930x_vlan_set_untagged,
  2103. .vlan_profile_dump = rtl930x_vlan_profile_dump,
  2104. .vlan_profile_setup = rtl930x_vlan_profile_setup,
  2105. .vlan_fwd_on_inner = rtl930x_vlan_fwd_on_inner,
  2106. .set_vlan_igr_filter = rtl930x_set_igr_filter,
  2107. .set_vlan_egr_filter = rtl930x_set_egr_filter,
  2108. .stp_get = rtl930x_stp_get,
  2109. .stp_set = rtl930x_stp_set,
  2110. .mac_force_mode_ctrl = rtl930x_mac_force_mode_ctrl,
  2111. .mac_port_ctrl = rtl930x_mac_port_ctrl,
  2112. .l2_port_new_salrn = rtl930x_l2_port_new_salrn,
  2113. .l2_port_new_sa_fwd = rtl930x_l2_port_new_sa_fwd,
  2114. .mir_ctrl = RTL930X_MIR_CTRL,
  2115. .mir_dpm = RTL930X_MIR_DPM_CTRL,
  2116. .mir_spm = RTL930X_MIR_SPM_CTRL,
  2117. .mac_link_sts = RTL930X_MAC_LINK_STS,
  2118. .mac_link_dup_sts = RTL930X_MAC_LINK_DUP_STS,
  2119. .mac_link_spd_sts = rtl930x_mac_link_spd_sts,
  2120. .mac_rx_pause_sts = RTL930X_MAC_RX_PAUSE_STS,
  2121. .mac_tx_pause_sts = RTL930X_MAC_TX_PAUSE_STS,
  2122. .read_l2_entry_using_hash = rtl930x_read_l2_entry_using_hash,
  2123. .write_l2_entry_using_hash = rtl930x_write_l2_entry_using_hash,
  2124. .read_cam = rtl930x_read_cam,
  2125. .write_cam = rtl930x_write_cam,
  2126. .vlan_port_keep_tag_set = rtl930x_vlan_port_keep_tag_set,
  2127. .vlan_port_pvidmode_set = rtl930x_vlan_port_pvidmode_set,
  2128. .vlan_port_pvid_set = rtl930x_vlan_port_pvid_set,
  2129. .trk_mbr_ctr = rtl930x_trk_mbr_ctr,
  2130. .rma_bpdu_fld_pmask = RTL930X_RMA_BPDU_FLD_PMSK,
  2131. .init_eee = rtl930x_init_eee,
  2132. .port_eee_set = rtl930x_port_eee_set,
  2133. .eee_port_ability = rtl930x_eee_port_ability,
  2134. .l2_hash_seed = rtl930x_l2_hash_seed,
  2135. .l2_hash_key = rtl930x_l2_hash_key,
  2136. .read_mcast_pmask = rtl930x_read_mcast_pmask,
  2137. .write_mcast_pmask = rtl930x_write_mcast_pmask,
  2138. .pie_init = rtl930x_pie_init,
  2139. .pie_rule_write = rtl930x_pie_rule_write,
  2140. .pie_rule_add = rtl930x_pie_rule_add,
  2141. .pie_rule_rm = rtl930x_pie_rule_rm,
  2142. .l2_learning_setup = rtl930x_l2_learning_setup,
  2143. .packet_cntr_read = rtl930x_packet_cntr_read,
  2144. .packet_cntr_clear = rtl930x_packet_cntr_clear,
  2145. .route_read = rtl930x_route_read,
  2146. .route_write = rtl930x_route_write,
  2147. .host_route_write = rtl930x_host_route_write,
  2148. .l3_setup = rtl930x_l3_setup,
  2149. .set_l3_nexthop = rtl930x_set_l3_nexthop,
  2150. .get_l3_nexthop = rtl930x_get_l3_nexthop,
  2151. .get_l3_egress_mac = rtl930x_get_l3_egress_mac,
  2152. .set_l3_egress_mac = rtl930x_set_l3_egress_mac,
  2153. .find_l3_slot = rtl930x_find_l3_slot,
  2154. .route_lookup_hw = rtl930x_route_lookup_hw,
  2155. .get_l3_router_mac = rtl930x_get_l3_router_mac,
  2156. .set_l3_router_mac = rtl930x_set_l3_router_mac,
  2157. .set_l3_egress_intf = rtl930x_set_l3_egress_intf,
  2158. .set_distribution_algorithm = rtl930x_set_distribution_algorithm,
  2159. .led_init = rtl930x_led_init,
  2160. };