rtl930x.c 77 KB

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