rtl930x.c 78 KB

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