rtl930x.c 75 KB

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