rtl930x.c 79 KB

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