rtl930x.c 75 KB

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