rtl931x.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <asm/mach-rtl838x/mach-rtl83xx.h>
  3. #include "rtl83xx.h"
  4. #define RTL931X_VLAN_PORT_TAG_STS_INTERNAL 0x0
  5. #define RTL931X_VLAN_PORT_TAG_STS_UNTAG 0x1
  6. #define RTL931X_VLAN_PORT_TAG_STS_TAGGED 0x2
  7. #define RTL931X_VLAN_PORT_TAG_STS_PRIORITY_TAGGED 0x3
  8. #define RTL931X_VLAN_PORT_TAG_CTRL_BASE 0x4860
  9. /* port 0-56 */
  10. #define RTL931X_VLAN_PORT_TAG_CTRL(port) \
  11. RTL931X_VLAN_PORT_TAG_CTRL_BASE + (port << 2)
  12. #define RTL931X_VLAN_PORT_TAG_EGR_OTAG_STS_MASK GENMASK(13,12)
  13. #define RTL931X_VLAN_PORT_TAG_EGR_ITAG_STS_MASK GENMASK(11,10)
  14. #define RTL931X_VLAN_PORT_TAG_EGR_OTAG_KEEP_MASK GENMASK(9,9)
  15. #define RTL931X_VLAN_PORT_TAG_EGR_ITAG_KEEP_MASK GENMASK(8,8)
  16. #define RTL931X_VLAN_PORT_TAG_IGR_OTAG_KEEP_MASK GENMASK(7,7)
  17. #define RTL931X_VLAN_PORT_TAG_IGR_ITAG_KEEP_MASK GENMASK(6,6)
  18. #define RTL931X_VLAN_PORT_TAG_OTPID_IDX_MASK GENMASK(5,4)
  19. #define RTL931X_VLAN_PORT_TAG_OTPID_KEEP_MASK GENMASK(3,3)
  20. #define RTL931X_VLAN_PORT_TAG_ITPID_IDX_MASK GENMASK(2,1)
  21. #define RTL931X_VLAN_PORT_TAG_ITPID_KEEP_MASK GENMASK(0,0)
  22. extern struct mutex smi_lock;
  23. extern struct rtl83xx_soc_info soc_info;
  24. /* Definition of the RTL931X-specific template field IDs as used in the PIE */
  25. enum template_field_id {
  26. TEMPLATE_FIELD_SPM0 = 1,
  27. TEMPLATE_FIELD_SPM1 = 2,
  28. TEMPLATE_FIELD_SPM2 = 3,
  29. TEMPLATE_FIELD_SPM3 = 4,
  30. TEMPLATE_FIELD_DMAC0 = 9,
  31. TEMPLATE_FIELD_DMAC1 = 10,
  32. TEMPLATE_FIELD_DMAC2 = 11,
  33. TEMPLATE_FIELD_SMAC0 = 12,
  34. TEMPLATE_FIELD_SMAC1 = 13,
  35. TEMPLATE_FIELD_SMAC2 = 14,
  36. TEMPLATE_FIELD_ETHERTYPE = 15,
  37. TEMPLATE_FIELD_OTAG = 16,
  38. TEMPLATE_FIELD_ITAG = 17,
  39. TEMPLATE_FIELD_SIP0 = 18,
  40. TEMPLATE_FIELD_SIP1 = 19,
  41. TEMPLATE_FIELD_DIP0 = 20,
  42. TEMPLATE_FIELD_DIP1 = 21,
  43. TEMPLATE_FIELD_IP_TOS_PROTO = 22,
  44. TEMPLATE_FIELD_L4_SPORT = 23,
  45. TEMPLATE_FIELD_L4_DPORT = 24,
  46. TEMPLATE_FIELD_L34_HEADER = 25,
  47. TEMPLATE_FIELD_TCP_INFO = 26,
  48. TEMPLATE_FIELD_SIP2 = 34,
  49. TEMPLATE_FIELD_SIP3 = 35,
  50. TEMPLATE_FIELD_SIP4 = 36,
  51. TEMPLATE_FIELD_SIP5 = 37,
  52. TEMPLATE_FIELD_SIP6 = 38,
  53. TEMPLATE_FIELD_SIP7 = 39,
  54. TEMPLATE_FIELD_DIP2 = 42,
  55. TEMPLATE_FIELD_DIP3 = 43,
  56. TEMPLATE_FIELD_DIP4 = 44,
  57. TEMPLATE_FIELD_DIP5 = 45,
  58. TEMPLATE_FIELD_DIP6 = 46,
  59. TEMPLATE_FIELD_DIP7 = 47,
  60. TEMPLATE_FIELD_FLOW_LABEL = 49,
  61. TEMPLATE_FIELD_DSAP_SSAP = 50,
  62. TEMPLATE_FIELD_FWD_VID = 52,
  63. TEMPLATE_FIELD_RANGE_CHK = 53,
  64. TEMPLATE_FIELD_SLP = 55,
  65. TEMPLATE_FIELD_DLP = 56,
  66. TEMPLATE_FIELD_META_DATA = 57,
  67. TEMPLATE_FIELD_FIRST_MPLS1 = 60,
  68. TEMPLATE_FIELD_FIRST_MPLS2 = 61,
  69. TEMPLATE_FIELD_DPM3 = 8,
  70. };
  71. /* The meaning of TEMPLATE_FIELD_VLAN depends on phase and the configuration in
  72. * RTL931X_PIE_CTRL. We use always the same definition and map to the inner VLAN tag:
  73. */
  74. #define TEMPLATE_FIELD_VLAN TEMPLATE_FIELD_ITAG
  75. // Number of fixed templates predefined in the RTL9300 SoC
  76. #define N_FIXED_TEMPLATES 5
  77. // RTL931x specific predefined templates
  78. static enum template_field_id fixed_templates[N_FIXED_TEMPLATES][N_FIXED_FIELDS_RTL931X] =
  79. {
  80. {
  81. TEMPLATE_FIELD_DMAC0, TEMPLATE_FIELD_DMAC1, TEMPLATE_FIELD_DMAC2,
  82. TEMPLATE_FIELD_SMAC0, TEMPLATE_FIELD_SMAC1, TEMPLATE_FIELD_SMAC2,
  83. TEMPLATE_FIELD_VLAN, TEMPLATE_FIELD_IP_TOS_PROTO, TEMPLATE_FIELD_DSAP_SSAP,
  84. TEMPLATE_FIELD_ETHERTYPE, TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1,
  85. TEMPLATE_FIELD_SPM2, TEMPLATE_FIELD_SPM3
  86. }, {
  87. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_DIP0,
  88. TEMPLATE_FIELD_DIP1, TEMPLATE_FIELD_IP_TOS_PROTO, TEMPLATE_FIELD_TCP_INFO,
  89. TEMPLATE_FIELD_L4_SPORT, TEMPLATE_FIELD_L4_DPORT, TEMPLATE_FIELD_VLAN,
  90. TEMPLATE_FIELD_RANGE_CHK, TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1,
  91. TEMPLATE_FIELD_SPM2, TEMPLATE_FIELD_SPM3
  92. }, {
  93. TEMPLATE_FIELD_DMAC0, TEMPLATE_FIELD_DMAC1, TEMPLATE_FIELD_DMAC2,
  94. TEMPLATE_FIELD_VLAN, TEMPLATE_FIELD_ETHERTYPE, TEMPLATE_FIELD_IP_TOS_PROTO,
  95. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_DIP0,
  96. TEMPLATE_FIELD_DIP1, TEMPLATE_FIELD_L4_SPORT, TEMPLATE_FIELD_L4_DPORT,
  97. TEMPLATE_FIELD_META_DATA, TEMPLATE_FIELD_SLP
  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. TEMPLATE_FIELD_RANGE_CHK, TEMPLATE_FIELD_SLP
  104. }, {
  105. TEMPLATE_FIELD_SIP0, TEMPLATE_FIELD_SIP1, TEMPLATE_FIELD_SIP2,
  106. TEMPLATE_FIELD_SIP3, TEMPLATE_FIELD_SIP4, TEMPLATE_FIELD_SIP5,
  107. TEMPLATE_FIELD_SIP6, TEMPLATE_FIELD_SIP7, TEMPLATE_FIELD_META_DATA,
  108. TEMPLATE_FIELD_VLAN, TEMPLATE_FIELD_SPM0, TEMPLATE_FIELD_SPM1,
  109. TEMPLATE_FIELD_SPM2, TEMPLATE_FIELD_SPM3
  110. },
  111. };
  112. inline void rtl931x_exec_tbl0_cmd(u32 cmd)
  113. {
  114. sw_w32(cmd, RTL931X_TBL_ACCESS_CTRL_0);
  115. do { } while (sw_r32(RTL931X_TBL_ACCESS_CTRL_0) & (1 << 20));
  116. }
  117. inline void rtl931x_exec_tbl1_cmd(u32 cmd)
  118. {
  119. sw_w32(cmd, RTL931X_TBL_ACCESS_CTRL_1);
  120. do { } while (sw_r32(RTL931X_TBL_ACCESS_CTRL_1) & (1 << 17));
  121. }
  122. inline int rtl931x_tbl_access_data_0(int i)
  123. {
  124. return RTL931X_TBL_ACCESS_DATA_0(i);
  125. }
  126. void rtl931x_vlan_profile_dump(int index)
  127. {
  128. u64 profile[4];
  129. if (index < 0 || index > 15)
  130. return;
  131. profile[0] = sw_r32(RTL931X_VLAN_PROFILE_SET(index));
  132. profile[1] = (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 4) & 0x1FFFFFFFULL) << 32 |
  133. (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 8) & 0xFFFFFFFF);
  134. profile[2] = (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 16) & 0x1FFFFFFFULL) << 32 |
  135. (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 12) & 0xFFFFFFFF);
  136. profile[3] = (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 20) & 0x1FFFFFFFULL) << 32 |
  137. (sw_r32(RTL931X_VLAN_PROFILE_SET(index) + 24) & 0xFFFFFFFF);
  138. pr_info("VLAN %d: L2 learning: %d, L2 Unknown MultiCast Field %llx, \
  139. IPv4 Unknown MultiCast Field %llx, IPv6 Unknown MultiCast Field: %llx",
  140. index, (u32) (profile[0] & (3 << 14)), profile[1], profile[2], profile[3]);
  141. }
  142. static void rtl931x_stp_get(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  143. {
  144. int i;
  145. u32 cmd = 1 << 20 | /* Execute cmd */
  146. 0 << 19 | /* Read */
  147. 5 << 15 | /* Table type 0b101 */
  148. (msti & 0x3fff);
  149. priv->r->exec_tbl0_cmd(cmd);
  150. for (i = 0; i < 4; i++)
  151. port_state[i] = sw_r32(priv->r->tbl_access_data_0(i));
  152. }
  153. static void rtl931x_stp_set(struct rtl838x_switch_priv *priv, u16 msti, u32 port_state[])
  154. {
  155. int i;
  156. u32 cmd = 1 << 20 | /* Execute cmd */
  157. 1 << 19 | /* Write */
  158. 5 << 15 | /* Table type 0b101 */
  159. (msti & 0x3fff);
  160. for (i = 0; i < 4; i++)
  161. sw_w32(port_state[i], priv->r->tbl_access_data_0(i));
  162. priv->r->exec_tbl0_cmd(cmd);
  163. }
  164. inline static int rtl931x_trk_mbr_ctr(int group)
  165. {
  166. return RTL931X_TRK_MBR_CTRL + (group << 2);
  167. }
  168. static void rtl931x_vlan_tables_read(u32 vlan, struct rtl838x_vlan_info *info)
  169. {
  170. u32 v, w, x, y;
  171. // Read VLAN table (3) via register 0
  172. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 3);
  173. rtl_table_read(r, vlan);
  174. v = sw_r32(rtl_table_data(r, 0));
  175. w = sw_r32(rtl_table_data(r, 1));
  176. x = sw_r32(rtl_table_data(r, 2));
  177. y = sw_r32(rtl_table_data(r, 3));
  178. rtl_table_release(r);
  179. pr_debug("VLAN_READ %d: %08x %08x %08x %08x\n", vlan, v, w, x, y);
  180. info->tagged_ports = ((u64) v) << 25 | (w >> 7);
  181. info->profile_id = (x >> 16) & 0xf;
  182. info->fid = w & 0x7f; // AKA MSTI depending on context
  183. info->hash_uc_fid = !!(x & BIT(31));
  184. info->hash_mc_fid = !!(x & BIT(30));
  185. info->if_id = (x >> 20) & 0x3ff;
  186. info->profile_id = (x >> 16) & 0xf;
  187. info->multicast_grp_mask = x & 0xffff;
  188. if (x & BIT(31))
  189. info->l2_tunnel_list_id = y >> 18;
  190. else
  191. info->l2_tunnel_list_id = -1;
  192. pr_debug("%s read tagged %016llx, profile-id %d, uc %d, mc %d, intf-id %d\n", __func__,
  193. info->tagged_ports, info->profile_id, info->hash_uc_fid, info->hash_mc_fid,
  194. info->if_id);
  195. // Read UNTAG table via table register 3
  196. r = rtl_table_get(RTL9310_TBL_3, 0);
  197. rtl_table_read(r, vlan);
  198. v = ((u64)sw_r32(rtl_table_data(r, 0))) << 25;
  199. v |= sw_r32(rtl_table_data(r, 1)) >> 7;
  200. rtl_table_release(r);
  201. info->untagged_ports = v;
  202. }
  203. static void rtl931x_vlan_set_tagged(u32 vlan, struct rtl838x_vlan_info *info)
  204. {
  205. u32 v, w, x, y;
  206. // Access VLAN table (1) via register 0
  207. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 3);
  208. v = info->tagged_ports >> 25;
  209. w = (info->tagged_ports & 0x1fffff) << 7;
  210. w |= info->fid & 0x7f;
  211. x = info->hash_uc_fid ? BIT(31) : 0;
  212. x |= info->hash_mc_fid ? BIT(30) : 0;
  213. x |= info->if_id & 0x3ff << 20;
  214. x |= (info->profile_id & 0xf) << 16;
  215. x |= info->multicast_grp_mask & 0xffff;
  216. if (info->l2_tunnel_list_id >= 0) {
  217. y = info->l2_tunnel_list_id << 18;
  218. y |= BIT(31);
  219. } else {
  220. y = 0;
  221. }
  222. sw_w32(v, rtl_table_data(r, 0));
  223. sw_w32(w, rtl_table_data(r, 1));
  224. sw_w32(x, rtl_table_data(r, 2));
  225. sw_w32(y, rtl_table_data(r, 3));
  226. rtl_table_write(r, vlan);
  227. rtl_table_release(r);
  228. }
  229. static void rtl931x_vlan_set_untagged(u32 vlan, u64 portmask)
  230. {
  231. struct table_reg *r = rtl_table_get(RTL9310_TBL_3, 0);
  232. rtl839x_set_port_reg_be(portmask << 7, rtl_table_data(r, 0));
  233. rtl_table_write(r, vlan);
  234. rtl_table_release(r);
  235. }
  236. static inline int rtl931x_mac_force_mode_ctrl(int p)
  237. {
  238. return RTL931X_MAC_FORCE_MODE_CTRL + (p << 2);
  239. }
  240. static inline int rtl931x_mac_link_spd_sts(int p)
  241. {
  242. return RTL931X_MAC_LINK_SPD_STS + (((p >> 3) << 2));
  243. }
  244. static inline int rtl931x_mac_port_ctrl(int p)
  245. {
  246. return RTL931X_MAC_L2_PORT_CTRL + (p << 7);
  247. }
  248. static inline int rtl931x_l2_port_new_salrn(int p)
  249. {
  250. return RTL931X_L2_PORT_NEW_SALRN(p);
  251. }
  252. static inline int rtl931x_l2_port_new_sa_fwd(int p)
  253. {
  254. return RTL931X_L2_PORT_NEW_SA_FWD(p);
  255. }
  256. irqreturn_t rtl931x_switch_irq(int irq, void *dev_id)
  257. {
  258. struct dsa_switch *ds = dev_id;
  259. u32 status = sw_r32(RTL931X_ISR_GLB_SRC);
  260. u64 ports = rtl839x_get_port_reg_le(RTL931X_ISR_PORT_LINK_STS_CHG);
  261. u64 link;
  262. int i;
  263. /* Clear status */
  264. rtl839x_set_port_reg_le(ports, RTL931X_ISR_PORT_LINK_STS_CHG);
  265. pr_debug("RTL931X Link change: status: %x, ports %016llx\n", status, ports);
  266. link = rtl839x_get_port_reg_le(RTL931X_MAC_LINK_STS);
  267. // Must re-read this to get correct status
  268. link = rtl839x_get_port_reg_le(RTL931X_MAC_LINK_STS);
  269. pr_debug("RTL931X Link change: status: %x, link status %016llx\n", status, link);
  270. for (i = 0; i < 56; i++) {
  271. if (ports & BIT_ULL(i)) {
  272. if (link & BIT_ULL(i)) {
  273. pr_info("%s port %d up\n", __func__, i);
  274. dsa_port_phylink_mac_change(ds, i, true);
  275. } else {
  276. pr_info("%s port %d down\n", __func__, i);
  277. dsa_port_phylink_mac_change(ds, i, false);
  278. }
  279. }
  280. }
  281. return IRQ_HANDLED;
  282. }
  283. int rtl931x_write_phy(u32 port, u32 page, u32 reg, u32 val)
  284. {
  285. u32 v;
  286. int err = 0;
  287. val &= 0xffff;
  288. if (port > 63 || page > 4095 || reg > 31)
  289. return -ENOTSUPP;
  290. mutex_lock(&smi_lock);
  291. pr_debug("%s: writing to phy %d %d %d %d\n", __func__, port, page, reg, val);
  292. /* Clear both port registers */
  293. sw_w32(0, RTL931X_SMI_INDRT_ACCESS_CTRL_2);
  294. sw_w32(0, RTL931X_SMI_INDRT_ACCESS_CTRL_2 + 4);
  295. sw_w32_mask(0, BIT(port % 32), RTL931X_SMI_INDRT_ACCESS_CTRL_2 + (port / 32) * 4);
  296. sw_w32_mask(0xffff, val, RTL931X_SMI_INDRT_ACCESS_CTRL_3);
  297. v = reg << 6 | page << 11 ;
  298. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  299. sw_w32(0x1ff, RTL931X_SMI_INDRT_ACCESS_CTRL_1);
  300. v |= BIT(4) | 1; /* Write operation and execute */
  301. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  302. do {
  303. } while (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0) & 0x1);
  304. if (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0) & 0x2)
  305. err = -EIO;
  306. mutex_unlock(&smi_lock);
  307. return err;
  308. }
  309. int rtl931x_read_phy(u32 port, u32 page, u32 reg, u32 *val)
  310. {
  311. u32 v;
  312. if (port > 63 || page > 4095 || reg > 31)
  313. return -ENOTSUPP;
  314. mutex_lock(&smi_lock);
  315. sw_w32(port << 5, RTL931X_SMI_INDRT_ACCESS_BC_PHYID_CTRL);
  316. v = reg << 6 | page << 11 | 1;
  317. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  318. do {
  319. } while (sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0) & 0x1);
  320. v = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  321. *val = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_3);
  322. *val = (*val & 0xffff0000) >> 16;
  323. pr_debug("%s: port %d, page: %d, reg: %x, val: %x, v: %08x\n",
  324. __func__, port, page, reg, *val, v);
  325. mutex_unlock(&smi_lock);
  326. return 0;
  327. }
  328. /* Read an mmd register of the PHY */
  329. int rtl931x_read_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 *val)
  330. {
  331. int err = 0;
  332. u32 v;
  333. /* Select PHY register type
  334. * If select 1G/10G MMD register type, registers EXT_PAGE, MAIN_PAGE and REG settings are don’t care.
  335. * 0x0 Normal register (Clause 22)
  336. * 0x1: 1G MMD register (MMD via Clause 22 registers 13 and 14)
  337. * 0x2: 10G MMD register (MMD via Clause 45)
  338. */
  339. int type = (regnum & MII_ADDR_C45)?2:1;
  340. mutex_lock(&smi_lock);
  341. // Set PHY to access via port-number
  342. sw_w32(port << 5, RTL931X_SMI_INDRT_ACCESS_BC_PHYID_CTRL);
  343. // Set MMD device number and register to write to
  344. sw_w32(devnum << 16 | mdiobus_c45_regad(regnum), RTL931X_SMI_INDRT_ACCESS_MMD_CTRL);
  345. v = type << 2 | BIT(0); // MMD-access-type | EXEC
  346. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  347. do {
  348. v = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  349. } while (v & BIT(0));
  350. // Check for error condition
  351. if (v & BIT(1))
  352. err = -EIO;
  353. *val = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_3) >> 16;
  354. pr_debug("%s: port %d, dev: %x, regnum: %x, val: %x (err %d)\n", __func__,
  355. port, devnum, mdiobus_c45_regad(regnum), *val, err);
  356. mutex_unlock(&smi_lock);
  357. return err;
  358. }
  359. /* Write to an mmd register of the PHY */
  360. int rtl931x_write_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 val)
  361. {
  362. int err = 0;
  363. u32 v;
  364. int type = (regnum & MII_ADDR_C45)?2:1;
  365. u64 pm;
  366. mutex_lock(&smi_lock);
  367. // Set PHY to access via port-mask
  368. pm = (u64)1 << port;
  369. sw_w32((u32)pm, RTL931X_SMI_INDRT_ACCESS_CTRL_2);
  370. sw_w32((u32)(pm >> 32), RTL931X_SMI_INDRT_ACCESS_CTRL_2 + 4);
  371. // Set data to write
  372. sw_w32_mask(0xffff, val, RTL931X_SMI_INDRT_ACCESS_CTRL_3);
  373. // Set MMD device number and register to write to
  374. sw_w32(devnum << 16 | mdiobus_c45_regad(regnum), RTL931X_SMI_INDRT_ACCESS_MMD_CTRL);
  375. v = BIT(4) | type << 2 | BIT(0); // WRITE | MMD-access-type | EXEC
  376. sw_w32(v, RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  377. do {
  378. v = sw_r32(RTL931X_SMI_INDRT_ACCESS_CTRL_0);
  379. } while (v & BIT(0));
  380. pr_debug("%s: port %d, dev: %x, regnum: %x, val: %x (err %d)\n", __func__,
  381. port, devnum, mdiobus_c45_regad(regnum), val, err);
  382. mutex_unlock(&smi_lock);
  383. return err;
  384. }
  385. void rtl931x_print_matrix(void)
  386. {
  387. volatile u64 *ptr = RTL838X_SW_BASE + RTL839X_PORT_ISO_CTRL(0);
  388. int i;
  389. for (i = 0; i < 52; i += 4)
  390. pr_info("> %16llx %16llx %16llx %16llx\n",
  391. ptr[i + 0], ptr[i + 1], ptr[i + 2], ptr[i + 3]);
  392. pr_info("CPU_PORT> %16llx\n", ptr[52]);
  393. }
  394. void rtl931x_set_receive_management_action(int port, rma_ctrl_t type, action_type_t action)
  395. {
  396. u32 value = 0;
  397. /* hack for value mapping */
  398. if (type == GRATARP && action == COPY2CPU)
  399. action = TRAP2MASTERCPU;
  400. switch(action) {
  401. case FORWARD:
  402. value = 0;
  403. break;
  404. case DROP:
  405. value = 1;
  406. break;
  407. case TRAP2CPU:
  408. value = 2;
  409. break;
  410. case TRAP2MASTERCPU:
  411. value = 3;
  412. break;
  413. case FLOODALL:
  414. value = 4;
  415. break;
  416. default:
  417. break;
  418. }
  419. switch(type) {
  420. case BPDU:
  421. sw_w32_mask(7 << ((port % 10) * 3), value << ((port % 10) * 3), RTL931X_RMA_BPDU_CTRL + ((port / 10) << 2));
  422. break;
  423. case PTP:
  424. //udp
  425. sw_w32_mask(3 << 2, value << 2, RTL931X_RMA_PTP_CTRL + (port << 2));
  426. //eth2
  427. sw_w32_mask(3, value, RTL931X_RMA_PTP_CTRL + (port << 2));
  428. break;
  429. case PTP_UDP:
  430. sw_w32_mask(3 << 2, value << 2, RTL931X_RMA_PTP_CTRL + (port << 2));
  431. break;
  432. case PTP_ETH2:
  433. sw_w32_mask(3, value, RTL931X_RMA_PTP_CTRL + (port << 2));
  434. break;
  435. case LLTP:
  436. sw_w32_mask(7 << ((port % 10) * 3), value << ((port % 10) * 3), RTL931X_RMA_LLTP_CTRL + ((port / 10) << 2));
  437. break;
  438. case EAPOL:
  439. sw_w32_mask(7 << ((port % 10) * 3), value << ((port % 10) * 3), RTL931X_RMA_EAPOL_CTRL + ((port / 10) << 2));
  440. break;
  441. case GRATARP:
  442. sw_w32_mask(3 << ((port & 0xf) << 1), value << ((port & 0xf) << 1), RTL931X_TRAP_ARP_GRAT_PORT_ACT + ((port >> 4) << 2));
  443. break;
  444. }
  445. }
  446. u64 rtl931x_traffic_get(int source)
  447. {
  448. u32 v;
  449. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 6);
  450. rtl_table_read(r, source);
  451. v = sw_r32(rtl_table_data(r, 0));
  452. rtl_table_release(r);
  453. v = v >> 3;
  454. return v;
  455. }
  456. /* Enable traffic between a source port and a destination port matrix */
  457. void rtl931x_traffic_set(int source, u64 dest_matrix)
  458. {
  459. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 6);
  460. sw_w32((dest_matrix << 3), rtl_table_data(r, 0));
  461. rtl_table_write(r, source);
  462. rtl_table_release(r);
  463. }
  464. void rtl931x_traffic_enable(int source, int dest)
  465. {
  466. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 6);
  467. rtl_table_read(r, source);
  468. sw_w32_mask(0, BIT(dest + 3), rtl_table_data(r, 0));
  469. rtl_table_write(r, source);
  470. rtl_table_release(r);
  471. }
  472. void rtl931x_traffic_disable(int source, int dest)
  473. {
  474. struct table_reg *r = rtl_table_get(RTL9310_TBL_0, 6);
  475. rtl_table_read(r, source);
  476. sw_w32_mask(BIT(dest + 3), 0, rtl_table_data(r, 0));
  477. rtl_table_write(r, source);
  478. rtl_table_release(r);
  479. }
  480. static u64 rtl931x_l2_hash_seed(u64 mac, u32 vid)
  481. {
  482. u64 v = vid;
  483. v <<= 48;
  484. v |= mac;
  485. return v;
  486. }
  487. /* Calculate both the block 0 and the block 1 hash by applyingthe same hash
  488. * algorithm as the one used currently by the ASIC to the seed, and return
  489. * both hashes in the lower and higher word of the return value since only 12 bit of
  490. * the hash are significant.
  491. */
  492. static u32 rtl931x_l2_hash_key(struct rtl838x_switch_priv *priv, u64 seed)
  493. {
  494. u32 h, h0, h1, h2, h3, h4, k0, k1;
  495. h0 = seed & 0xfff;
  496. h1 = (seed >> 12) & 0xfff;
  497. h2 = (seed >> 24) & 0xfff;
  498. h3 = (seed >> 36) & 0xfff;
  499. h4 = (seed >> 48) & 0xfff;
  500. h4 = ((h4 & 0x7) << 9) | ((h4 >> 3) & 0x1ff);
  501. k0 = h0 ^ h1 ^ h2 ^ h3 ^ h4;
  502. h0 = seed & 0xfff;
  503. h0 = ((h0 & 0x1ff) << 3) | ((h0 >> 9) & 0x7);
  504. h1 = (seed >> 12) & 0xfff;
  505. h1 = ((h1 & 0x3f) << 6) | ((h1 >> 6) & 0x3f);
  506. h2 = (seed >> 24) & 0xfff;
  507. h3 = (seed >> 36) & 0xfff;
  508. h3 = ((h3 & 0x3f) << 6) | ((h3 >> 6) & 0x3f);
  509. h4 = (seed >> 48) & 0xfff;
  510. k1 = h0 ^ h1 ^ h2 ^ h3 ^ h4;
  511. // Algorithm choice for block 0
  512. if (sw_r32(RTL931X_L2_CTRL) & BIT(0))
  513. h = k1;
  514. else
  515. h = k0;
  516. /* Algorithm choice for block 1
  517. * Since k0 and k1 are < 4096, adding 4096 will offset the hash into the second
  518. * half of hash-space
  519. * 4096 is in fact the hash-table size 32768 divided by 4 hashes per bucket
  520. * divided by 2 to divide the hash space in 2
  521. */
  522. if (sw_r32(RTL931X_L2_CTRL) & BIT(1))
  523. h |= (k1 + 4096) << 16;
  524. else
  525. h |= (k0 + 4096) << 16;
  526. return h;
  527. }
  528. /* Fills an L2 entry structure from the SoC registers */
  529. static void rtl931x_fill_l2_entry(u32 r[], struct rtl838x_l2_entry *e)
  530. {
  531. pr_debug("In %s valid?\n", __func__);
  532. e->valid = !!(r[0] & BIT(31));
  533. if (!e->valid)
  534. return;
  535. pr_debug("%s: entry valid, raw: %08x %08x %08x %08x\n", __func__, r[0], r[1], r[2], r[3]);
  536. e->is_ip_mc = false;
  537. e->is_ipv6_mc = false;
  538. e->mac[0] = r[0] >> 8;
  539. e->mac[1] = r[0];
  540. e->mac[2] = r[1] >> 24;
  541. e->mac[3] = r[1] >> 16;
  542. e->mac[4] = r[1] >> 8;
  543. e->mac[5] = r[1];
  544. e->is_open_flow = !!(r[0] & BIT(30));
  545. e->is_pe_forward = !!(r[0] & BIT(29));
  546. e->next_hop = !!(r[2] & BIT(30));
  547. e->rvid = (r[0] >> 16) & 0xfff;
  548. /* Is it a unicast entry? check multicast bit */
  549. if (!(e->mac[0] & 1)) {
  550. e->type = L2_UNICAST;
  551. e->is_l2_tunnel = !!(r[2] & BIT(31));
  552. e->is_static = !!(r[2] & BIT(13));
  553. e->port = (r[2] >> 19) & 0x3ff;
  554. // Check for trunk port
  555. if (r[2] & BIT(29)) {
  556. e->is_trunk = true;
  557. e->stack_dev = (e->port >> 9) & 1;
  558. e->trunk = e->port & 0x3f;
  559. } else {
  560. e->is_trunk = false;
  561. e->stack_dev = (e->port >> 6) & 0xf;
  562. e->port = e->port & 0x3f;
  563. }
  564. e->block_da = !!(r[2] & BIT(14));
  565. e->block_sa = !!(r[2] & BIT(15));
  566. e->suspended = !!(r[2] & BIT(12));
  567. e->age = (r[2] >> 16) & 3;
  568. // the UC_VID field in hardware is used for the VID or for the route id
  569. if (e->next_hop) {
  570. e->nh_route_id = r[2] & 0x7ff;
  571. e->vid = 0;
  572. } else {
  573. e->vid = r[2] & 0xfff;
  574. e->nh_route_id = 0;
  575. }
  576. if (e->is_l2_tunnel)
  577. e->l2_tunnel_id = ((r[2] & 0xff) << 4) | (r[3] >> 28);
  578. // TODO: Implement VLAN conversion
  579. } else {
  580. e->type = L2_MULTICAST;
  581. e->is_local_forward = !!(r[2] & BIT(31));
  582. e->is_remote_forward = !!(r[2] & BIT(17));
  583. e->mc_portmask_index = (r[2] >> 18) & 0xfff;
  584. e->l2_tunnel_list_id = (r[2] >> 4) & 0x1fff;
  585. }
  586. }
  587. /* Fills the 3 SoC table registers r[] with the information of in the rtl838x_l2_entry */
  588. static void rtl931x_fill_l2_row(u32 r[], struct rtl838x_l2_entry *e)
  589. {
  590. u32 port;
  591. if (!e->valid) {
  592. r[0] = r[1] = r[2] = 0;
  593. return;
  594. }
  595. r[2] = BIT(31); // Set valid bit
  596. r[0] = ((u32)e->mac[0]) << 24 |
  597. ((u32)e->mac[1]) << 16 |
  598. ((u32)e->mac[2]) << 8 |
  599. ((u32)e->mac[3]);
  600. r[1] = ((u32)e->mac[4]) << 24 |
  601. ((u32)e->mac[5]) << 16;
  602. r[2] |= e->next_hop ? BIT(12) : 0;
  603. if (e->type == L2_UNICAST) {
  604. r[2] |= e->is_static ? BIT(14) : 0;
  605. r[1] |= e->rvid & 0xfff;
  606. r[2] |= (e->port & 0x3ff) << 20;
  607. if (e->is_trunk) {
  608. r[2] |= BIT(30);
  609. port = e->stack_dev << 9 | (e->port & 0x3f);
  610. } else {
  611. port = (e->stack_dev & 0xf) << 6;
  612. port |= e->port & 0x3f;
  613. }
  614. r[2] |= port << 20;
  615. r[2] |= e->block_da ? BIT(15) : 0;
  616. r[2] |= e->block_sa ? BIT(17) : 0;
  617. r[2] |= e->suspended ? BIT(13) : 0;
  618. r[2] |= (e->age & 0x3) << 17;
  619. // the UC_VID field in hardware is used for the VID or for the route id
  620. if (e->next_hop)
  621. r[2] |= e->nh_route_id & 0x7ff;
  622. else
  623. r[2] |= e->vid & 0xfff;
  624. } else { // L2_MULTICAST
  625. r[2] |= (e->mc_portmask_index & 0x3ff) << 16;
  626. r[2] |= e->mc_mac_index & 0x7ff;
  627. }
  628. }
  629. /* Read an L2 UC or MC entry out of a hash bucket of the L2 forwarding table
  630. * hash is the id of the bucket and pos is the position of the entry in that bucket
  631. * The data read from the SoC is filled into rtl838x_l2_entry
  632. */
  633. static u64 rtl931x_read_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  634. {
  635. u32 r[4];
  636. struct table_reg *q = rtl_table_get(RTL9310_TBL_0, 0);
  637. u32 idx;
  638. int i;
  639. u64 mac;
  640. u64 seed;
  641. pr_debug("%s: hash %08x, pos: %d\n", __func__, hash, pos);
  642. /* On the RTL93xx, 2 different hash algorithms are used making it a total of
  643. * 8 buckets that need to be searched, 4 for each hash-half
  644. * Use second hash space when bucket is between 4 and 8
  645. */
  646. if (pos >= 4) {
  647. pos -= 4;
  648. hash >>= 16;
  649. } else {
  650. hash &= 0xffff;
  651. }
  652. idx = (0 << 14) | (hash << 2) | pos; // Search SRAM, with hash and at pos in bucket
  653. pr_debug("%s: NOW hash %08x, pos: %d\n", __func__, hash, pos);
  654. rtl_table_read(q, idx);
  655. for (i = 0; i < 4; i++)
  656. r[i] = sw_r32(rtl_table_data(q, i));
  657. rtl_table_release(q);
  658. rtl931x_fill_l2_entry(r, e);
  659. pr_debug("%s: valid: %d, nh: %d\n", __func__, e->valid, e->next_hop);
  660. if (!e->valid)
  661. return 0;
  662. mac = ((u64)e->mac[0]) << 40 |
  663. ((u64)e->mac[1]) << 32 |
  664. ((u64)e->mac[2]) << 24 |
  665. ((u64)e->mac[3]) << 16 |
  666. ((u64)e->mac[4]) << 8 |
  667. ((u64)e->mac[5]);
  668. seed = rtl931x_l2_hash_seed(mac, e->rvid);
  669. pr_debug("%s: mac %016llx, seed %016llx\n", __func__, mac, seed);
  670. // return vid with concatenated mac as unique id
  671. return seed;
  672. }
  673. static u64 rtl931x_read_cam(int idx, struct rtl838x_l2_entry *e)
  674. {
  675. return 0;
  676. }
  677. static void rtl931x_write_cam(int idx, struct rtl838x_l2_entry *e)
  678. {
  679. }
  680. static void rtl931x_write_l2_entry_using_hash(u32 hash, u32 pos, struct rtl838x_l2_entry *e)
  681. {
  682. u32 r[4];
  683. struct table_reg *q = rtl_table_get(RTL9310_TBL_0, 0);
  684. u32 idx = (0 << 14) | (hash << 2) | pos; // Access SRAM, with hash and at pos in bucket
  685. int i;
  686. pr_info("%s: hash %d, pos %d\n", __func__, hash, pos);
  687. pr_info("%s: index %d -> mac %02x:%02x:%02x:%02x:%02x:%02x\n", __func__, idx,
  688. e->mac[0], e->mac[1], e->mac[2], e->mac[3],e->mac[4],e->mac[5]);
  689. rtl931x_fill_l2_row(r, e);
  690. pr_info("%s: %d: %08x %08x %08x\n", __func__, idx, r[0], r[1], r[2]);
  691. for (i = 0; i < 4; i++)
  692. sw_w32(r[i], rtl_table_data(q, i));
  693. rtl_table_write(q, idx);
  694. rtl_table_release(q);
  695. }
  696. static void rtl931x_vlan_fwd_on_inner(int port, bool is_set)
  697. {
  698. // Always set all tag modes to fwd based on either inner or outer tag
  699. if (is_set)
  700. sw_w32_mask(0, 0xf, RTL931X_VLAN_PORT_FWD + (port << 2));
  701. else
  702. sw_w32_mask(0xf, 0, RTL931X_VLAN_PORT_FWD + (port << 2));
  703. }
  704. static void rtl931x_vlan_profile_setup(int profile)
  705. {
  706. u32 p[7];
  707. int i;
  708. pr_info("In %s\n", __func__);
  709. if (profile > 15)
  710. return;
  711. p[0] = sw_r32(RTL931X_VLAN_PROFILE_SET(profile));
  712. // Enable routing of Ipv4/6 Unicast and IPv4/6 Multicast traffic
  713. //p[0] |= BIT(17) | BIT(16) | BIT(13) | BIT(12);
  714. p[0] |= 0x3 << 11; // COPY2CPU
  715. p[1] = 0x1FFFFFF; // L2 unknwon MC flooding portmask all ports, including the CPU-port
  716. p[2] = 0xFFFFFFFF;
  717. p[3] = 0x1FFFFFF; // IPv4 unknwon MC flooding portmask
  718. p[4] = 0xFFFFFFFF;
  719. p[5] = 0x1FFFFFF; // IPv6 unknwon MC flooding portmask
  720. p[6] = 0xFFFFFFFF;
  721. for (int i = 0; i < 7; i++)
  722. sw_w32(p[i], RTL931X_VLAN_PROFILE_SET(profile) + i * 4);
  723. pr_info("Leaving %s\n", __func__);
  724. }
  725. static void rtl931x_l2_learning_setup(void)
  726. {
  727. // Portmask for flooding broadcast traffic
  728. rtl839x_set_port_reg_be(0x1FFFFFFFFFFFFFF, RTL931X_L2_BC_FLD_PMSK);
  729. // Portmask for flooding unicast traffic with unknown destination
  730. rtl839x_set_port_reg_be(0x1FFFFFFFFFFFFFF, RTL931X_L2_UNKN_UC_FLD_PMSK);
  731. // Limit learning to maximum: 64k entries, after that just flood (bits 0-2)
  732. sw_w32((0xffff << 3) | FORWARD, RTL931X_L2_LRN_CONSTRT_CTRL);
  733. }
  734. static u64 rtl931x_read_mcast_pmask(int idx)
  735. {
  736. u64 portmask;
  737. // Read MC_PMSK (2) via register RTL9310_TBL_0
  738. struct table_reg *q = rtl_table_get(RTL9310_TBL_0, 2);
  739. rtl_table_read(q, idx);
  740. portmask = sw_r32(rtl_table_data(q, 0));
  741. portmask <<= 32;
  742. portmask |= sw_r32(rtl_table_data(q, 1));
  743. portmask >>= 7;
  744. rtl_table_release(q);
  745. pr_debug("%s: Index idx %d has portmask %016llx\n", __func__, idx, portmask);
  746. return portmask;
  747. }
  748. static void rtl931x_write_mcast_pmask(int idx, u64 portmask)
  749. {
  750. u64 pm = portmask;
  751. // Access MC_PMSK (2) via register RTL9310_TBL_0
  752. struct table_reg *q = rtl_table_get(RTL9310_TBL_0, 2);
  753. pr_debug("%s: Index idx %d has portmask %016llx\n", __func__, idx, pm);
  754. pm <<= 7;
  755. sw_w32((u32)(pm >> 32), rtl_table_data(q, 0));
  756. sw_w32((u32)pm, rtl_table_data(q, 1));
  757. rtl_table_write(q, idx);
  758. rtl_table_release(q);
  759. }
  760. static int rtl931x_set_ageing_time(unsigned long msec)
  761. {
  762. int t = sw_r32(RTL931X_L2_AGE_CTRL);
  763. t &= 0x1FFFFF;
  764. t = (t * 8) / 10;
  765. pr_debug("L2 AGING time: %d sec\n", t);
  766. t = (msec / 100 + 7) / 8;
  767. t = t > 0x1FFFFF ? 0x1FFFFF : t;
  768. sw_w32_mask(0x1FFFFF, t, RTL931X_L2_AGE_CTRL);
  769. pr_debug("Dynamic aging for ports: %x\n", sw_r32(RTL931X_L2_PORT_AGE_CTRL));
  770. return 0;
  771. }
  772. void rtl931x_sw_init(struct rtl838x_switch_priv *priv)
  773. {
  774. // rtl931x_sds_init(priv);
  775. }
  776. static void rtl931x_pie_lookup_enable(struct rtl838x_switch_priv *priv, int index)
  777. {
  778. int block = index / PIE_BLOCK_SIZE;
  779. sw_w32_mask(0, BIT(block), RTL931X_PIE_BLK_LOOKUP_CTRL);
  780. }
  781. /* Fills the data in the intermediate representation in the pie_rule structure
  782. * into a data field for a given template field field_type
  783. * TODO: This function looks very similar to the function of the rtl9300, but
  784. * since it uses the physical template_field_id, which are different for each
  785. * SoC and there are other field types, it is actually not. If we would also use
  786. * an intermediate representation for a field type, we would could have one
  787. * pie_data_fill function for all SoCs, provided we have also for each SoC a
  788. * function to map between physical and intermediate field type
  789. */
  790. int rtl931x_pie_data_fill(enum template_field_id field_type, struct pie_rule *pr, u16 *data, u16 *data_m)
  791. {
  792. *data = *data_m = 0;
  793. switch (field_type) {
  794. case TEMPLATE_FIELD_SPM0:
  795. *data = pr->spm;
  796. *data_m = pr->spm_m;
  797. break;
  798. case TEMPLATE_FIELD_SPM1:
  799. *data = pr->spm >> 16;
  800. *data_m = pr->spm_m >> 16;
  801. break;
  802. case TEMPLATE_FIELD_OTAG:
  803. *data = pr->otag;
  804. *data_m = pr->otag_m;
  805. break;
  806. case TEMPLATE_FIELD_SMAC0:
  807. *data = pr->smac[4];
  808. *data = (*data << 8) | pr->smac[5];
  809. *data_m = pr->smac_m[4];
  810. *data_m = (*data_m << 8) | pr->smac_m[5];
  811. break;
  812. case TEMPLATE_FIELD_SMAC1:
  813. *data = pr->smac[2];
  814. *data = (*data << 8) | pr->smac[3];
  815. *data_m = pr->smac_m[2];
  816. *data_m = (*data_m << 8) | pr->smac_m[3];
  817. break;
  818. case TEMPLATE_FIELD_SMAC2:
  819. *data = pr->smac[0];
  820. *data = (*data << 8) | pr->smac[1];
  821. *data_m = pr->smac_m[0];
  822. *data_m = (*data_m << 8) | pr->smac_m[1];
  823. break;
  824. case TEMPLATE_FIELD_DMAC0:
  825. *data = pr->dmac[4];
  826. *data = (*data << 8) | pr->dmac[5];
  827. *data_m = pr->dmac_m[4];
  828. *data_m = (*data_m << 8) | pr->dmac_m[5];
  829. break;
  830. case TEMPLATE_FIELD_DMAC1:
  831. *data = pr->dmac[2];
  832. *data = (*data << 8) | pr->dmac[3];
  833. *data_m = pr->dmac_m[2];
  834. *data_m = (*data_m << 8) | pr->dmac_m[3];
  835. break;
  836. case TEMPLATE_FIELD_DMAC2:
  837. *data = pr->dmac[0];
  838. *data = (*data << 8) | pr->dmac[1];
  839. *data_m = pr->dmac_m[0];
  840. *data_m = (*data_m << 8) | pr->dmac_m[1];
  841. break;
  842. case TEMPLATE_FIELD_ETHERTYPE:
  843. *data = pr->ethertype;
  844. *data_m = pr->ethertype_m;
  845. break;
  846. case TEMPLATE_FIELD_ITAG:
  847. *data = pr->itag;
  848. *data_m = pr->itag_m;
  849. break;
  850. case TEMPLATE_FIELD_SIP0:
  851. if (pr->is_ipv6) {
  852. *data = pr->sip6.s6_addr16[7];
  853. *data_m = pr->sip6_m.s6_addr16[7];
  854. } else {
  855. *data = pr->sip;
  856. *data_m = pr->sip_m;
  857. }
  858. break;
  859. case TEMPLATE_FIELD_SIP1:
  860. if (pr->is_ipv6) {
  861. *data = pr->sip6.s6_addr16[6];
  862. *data_m = pr->sip6_m.s6_addr16[6];
  863. } else {
  864. *data = pr->sip >> 16;
  865. *data_m = pr->sip_m >> 16;
  866. }
  867. break;
  868. case TEMPLATE_FIELD_SIP2:
  869. case TEMPLATE_FIELD_SIP3:
  870. case TEMPLATE_FIELD_SIP4:
  871. case TEMPLATE_FIELD_SIP5:
  872. case TEMPLATE_FIELD_SIP6:
  873. case TEMPLATE_FIELD_SIP7:
  874. *data = pr->sip6.s6_addr16[5 - (field_type - TEMPLATE_FIELD_SIP2)];
  875. *data_m = pr->sip6_m.s6_addr16[5 - (field_type - TEMPLATE_FIELD_SIP2)];
  876. break;
  877. case TEMPLATE_FIELD_DIP0:
  878. if (pr->is_ipv6) {
  879. *data = pr->dip6.s6_addr16[7];
  880. *data_m = pr->dip6_m.s6_addr16[7];
  881. } else {
  882. *data = pr->dip;
  883. *data_m = pr->dip_m;
  884. }
  885. break;
  886. case TEMPLATE_FIELD_DIP1:
  887. if (pr->is_ipv6) {
  888. *data = pr->dip6.s6_addr16[6];
  889. *data_m = pr->dip6_m.s6_addr16[6];
  890. } else {
  891. *data = pr->dip >> 16;
  892. *data_m = pr->dip_m >> 16;
  893. }
  894. break;
  895. case TEMPLATE_FIELD_DIP2:
  896. case TEMPLATE_FIELD_DIP3:
  897. case TEMPLATE_FIELD_DIP4:
  898. case TEMPLATE_FIELD_DIP5:
  899. case TEMPLATE_FIELD_DIP6:
  900. case TEMPLATE_FIELD_DIP7:
  901. *data = pr->dip6.s6_addr16[5 - (field_type - TEMPLATE_FIELD_DIP2)];
  902. *data_m = pr->dip6_m.s6_addr16[5 - (field_type - TEMPLATE_FIELD_DIP2)];
  903. break;
  904. case TEMPLATE_FIELD_IP_TOS_PROTO:
  905. *data = pr->tos_proto;
  906. *data_m = pr->tos_proto_m;
  907. break;
  908. case TEMPLATE_FIELD_L4_SPORT:
  909. *data = pr->sport;
  910. *data_m = pr->sport_m;
  911. break;
  912. case TEMPLATE_FIELD_L4_DPORT:
  913. *data = pr->dport;
  914. *data_m = pr->dport_m;
  915. break;
  916. case TEMPLATE_FIELD_DSAP_SSAP:
  917. *data = pr->dsap_ssap;
  918. *data_m = pr->dsap_ssap_m;
  919. break;
  920. case TEMPLATE_FIELD_TCP_INFO:
  921. *data = pr->tcp_info;
  922. *data_m = pr->tcp_info_m;
  923. break;
  924. case TEMPLATE_FIELD_RANGE_CHK:
  925. pr_info("TEMPLATE_FIELD_RANGE_CHK: not configured\n");
  926. break;
  927. default:
  928. pr_info("%s: unknown field %d\n", __func__, field_type);
  929. return -1;
  930. }
  931. return 0;
  932. }
  933. /* Reads the intermediate representation of the templated match-fields of the
  934. * PIE rule in the pie_rule structure and fills in the raw data fields in the
  935. * raw register space r[].
  936. * The register space configuration size is identical for the RTL8380/90 and RTL9300,
  937. * however the RTL931X has 2 more registers / fields and the physical field-ids are different
  938. * on all SoCs
  939. * On the RTL9300 the mask fields are not word-aligend!
  940. */
  941. static void rtl931x_write_pie_templated(u32 r[], struct pie_rule *pr, enum template_field_id t[])
  942. {
  943. int i;
  944. u16 data, data_m;
  945. for (i = 0; i < N_FIXED_FIELDS; i++) {
  946. rtl931x_pie_data_fill(t[i], pr, &data, &data_m);
  947. // On the RTL9300, the mask fields are not word aligned!
  948. if (!(i % 2)) {
  949. r[5 - i / 2] = data;
  950. r[12 - i / 2] |= ((u32)data_m << 8);
  951. } else {
  952. r[5 - i / 2] |= ((u32)data) << 16;
  953. r[12 - i / 2] |= ((u32)data_m) << 24;
  954. r[11 - i / 2] |= ((u32)data_m) >> 8;
  955. }
  956. }
  957. }
  958. static void rtl931x_read_pie_fixed_fields(u32 r[], struct pie_rule *pr)
  959. {
  960. pr->mgnt_vlan = r[7] & BIT(31);
  961. if (pr->phase == PHASE_IACL)
  962. pr->dmac_hit_sw = r[7] & BIT(30);
  963. else // TODO: EACL/VACL phase handling
  964. pr->content_too_deep = r[7] & BIT(30);
  965. pr->not_first_frag = r[7] & BIT(29);
  966. pr->frame_type_l4 = (r[7] >> 26) & 7;
  967. pr->frame_type = (r[7] >> 24) & 3;
  968. pr->otag_fmt = (r[7] >> 23) & 1;
  969. pr->itag_fmt = (r[7] >> 22) & 1;
  970. pr->otag_exist = (r[7] >> 21) & 1;
  971. pr->itag_exist = (r[7] >> 20) & 1;
  972. pr->frame_type_l2 = (r[7] >> 18) & 3;
  973. pr->igr_normal_port = (r[7] >> 17) & 1;
  974. pr->tid = (r[7] >> 16) & 1;
  975. pr->mgnt_vlan_m = r[14] & BIT(15);
  976. if (pr->phase == PHASE_IACL)
  977. pr->dmac_hit_sw_m = r[14] & BIT(14);
  978. else
  979. pr->content_too_deep_m = r[14] & BIT(14);
  980. pr->not_first_frag_m = r[14] & BIT(13);
  981. pr->frame_type_l4_m = (r[14] >> 10) & 7;
  982. pr->frame_type_m = (r[14] >> 8) & 3;
  983. pr->otag_fmt_m = r[14] & BIT(7);
  984. pr->itag_fmt_m = r[14] & BIT(6);
  985. pr->otag_exist_m = r[14] & BIT(5);
  986. pr->itag_exist_m = r[14] & BIT (4);
  987. pr->frame_type_l2_m = (r[14] >> 2) & 3;
  988. pr->igr_normal_port_m = r[14] & BIT(1);
  989. pr->tid_m = r[14] & 1;
  990. pr->valid = r[15] & BIT(31);
  991. pr->cond_not = r[15] & BIT(30);
  992. pr->cond_and1 = r[15] & BIT(29);
  993. pr->cond_and2 = r[15] & BIT(28);
  994. }
  995. static void rtl931x_write_pie_fixed_fields(u32 r[], struct pie_rule *pr)
  996. {
  997. r[7] |= pr->mgnt_vlan ? BIT(31) : 0;
  998. if (pr->phase == PHASE_IACL)
  999. r[7] |= pr->dmac_hit_sw ? BIT(30) : 0;
  1000. else
  1001. r[7] |= pr->content_too_deep ? BIT(30) : 0;
  1002. r[7] |= pr->not_first_frag ? BIT(29) : 0;
  1003. r[7] |= ((u32) (pr->frame_type_l4 & 0x7)) << 26;
  1004. r[7] |= ((u32) (pr->frame_type & 0x3)) << 24;
  1005. r[7] |= pr->otag_fmt ? BIT(23) : 0;
  1006. r[7] |= pr->itag_fmt ? BIT(22) : 0;
  1007. r[7] |= pr->otag_exist ? BIT(21) : 0;
  1008. r[7] |= pr->itag_exist ? BIT(20) : 0;
  1009. r[7] |= ((u32) (pr->frame_type_l2 & 0x3)) << 18;
  1010. r[7] |= pr->igr_normal_port ? BIT(17) : 0;
  1011. r[7] |= ((u32) (pr->tid & 0x1)) << 16;
  1012. r[14] |= pr->mgnt_vlan_m ? BIT(15) : 0;
  1013. if (pr->phase == PHASE_IACL)
  1014. r[14] |= pr->dmac_hit_sw_m ? BIT(14) : 0;
  1015. else
  1016. r[14] |= pr->content_too_deep_m ? BIT(14) : 0;
  1017. r[14] |= pr->not_first_frag_m ? BIT(13) : 0;
  1018. r[14] |= ((u32) (pr->frame_type_l4_m & 0x7)) << 10;
  1019. r[14] |= ((u32) (pr->frame_type_m & 0x3)) << 8;
  1020. r[14] |= pr->otag_fmt_m ? BIT(7) : 0;
  1021. r[14] |= pr->itag_fmt_m ? BIT(6) : 0;
  1022. r[14] |= pr->otag_exist_m ? BIT(5) : 0;
  1023. r[14] |= pr->itag_exist_m ? BIT(4) : 0;
  1024. r[14] |= ((u32) (pr->frame_type_l2_m & 0x3)) << 2;
  1025. r[14] |= pr->igr_normal_port_m ? BIT(1) : 0;
  1026. r[14] |= (u32) (pr->tid_m & 0x1);
  1027. r[15] |= pr->valid ? BIT(31) : 0;
  1028. r[15] |= pr->cond_not ? BIT(30) : 0;
  1029. r[15] |= pr->cond_and1 ? BIT(29) : 0;
  1030. r[15] |= pr->cond_and2 ? BIT(28) : 0;
  1031. }
  1032. static void rtl931x_write_pie_action(u32 r[], struct pie_rule *pr)
  1033. {
  1034. // Either drop or forward
  1035. if (pr->drop) {
  1036. r[15] |= BIT(11) | BIT(12) | BIT(13); // Do Green, Yellow and Red drops
  1037. // Actually DROP, not PERMIT in Green / Yellow / Red
  1038. r[16] |= BIT(27) | BIT(28) | BIT(29);
  1039. } else {
  1040. r[15] |= pr->fwd_sel ? BIT(14) : 0;
  1041. r[16] |= pr->fwd_act << 24;
  1042. r[16] |= BIT(21); // We overwrite any drop
  1043. }
  1044. if (pr->phase == PHASE_VACL)
  1045. r[16] |= pr->fwd_sa_lrn ? BIT(22) : 0;
  1046. r[15] |= pr->bypass_sel ? BIT(10) : 0;
  1047. r[15] |= pr->nopri_sel ? BIT(21) : 0;
  1048. r[15] |= pr->tagst_sel ? BIT(20) : 0;
  1049. r[15] |= pr->ovid_sel ? BIT(18) : 0;
  1050. r[15] |= pr->ivid_sel ? BIT(16) : 0;
  1051. r[15] |= pr->meter_sel ? BIT(27) : 0;
  1052. r[15] |= pr->mir_sel ? BIT(15) : 0;
  1053. r[15] |= pr->log_sel ? BIT(26) : 0;
  1054. r[16] |= ((u32)(pr->fwd_data & 0xfff)) << 9;
  1055. // r[15] |= pr->log_octets ? BIT(31) : 0;
  1056. r[15] |= (u32)(pr->meter_data) >> 2;
  1057. r[16] |= (((u32)(pr->meter_data) >> 7) & 0x3) << 29;
  1058. r[16] |= ((u32)(pr->ivid_act & 0x3)) << 21;
  1059. r[15] |= ((u32)(pr->ivid_data & 0xfff)) << 9;
  1060. r[16] |= ((u32)(pr->ovid_act & 0x3)) << 30;
  1061. r[16] |= ((u32)(pr->ovid_data & 0xfff)) << 16;
  1062. r[16] |= ((u32)(pr->mir_data & 0x3)) << 6;
  1063. r[17] |= ((u32)(pr->tagst_data & 0xf)) << 28;
  1064. r[17] |= ((u32)(pr->nopri_data & 0x7)) << 25;
  1065. r[17] |= pr->bypass_ibc_sc ? BIT(16) : 0;
  1066. }
  1067. void rtl931x_pie_rule_dump_raw(u32 r[])
  1068. {
  1069. pr_info("Raw IACL table entry:\n");
  1070. pr_info("r 0 - 7: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  1071. r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7]);
  1072. pr_info("r 8 - 15: %08x %08x %08x %08x %08x %08x %08x %08x\n",
  1073. r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]);
  1074. pr_info("r 16 - 18: %08x %08x %08x\n", r[16], r[17], r[18]);
  1075. pr_info("Match : %08x %08x %08x %08x %08x %08x\n", r[0], r[1], r[2], r[3], r[4], r[5]);
  1076. pr_info("Fixed : %06x\n", r[6] >> 8);
  1077. pr_info("Match M: %08x %08x %08x %08x %08x %08x\n",
  1078. (r[6] << 24) | (r[7] >> 8), (r[7] << 24) | (r[8] >> 8), (r[8] << 24) | (r[9] >> 8),
  1079. (r[9] << 24) | (r[10] >> 8), (r[10] << 24) | (r[11] >> 8),
  1080. (r[11] << 24) | (r[12] >> 8));
  1081. pr_info("R[13]: %08x\n", r[13]);
  1082. pr_info("Fixed M: %06x\n", ((r[12] << 16) | (r[13] >> 16)) & 0xffffff);
  1083. pr_info("Valid / not / and1 / and2 : %1x\n", (r[13] >> 12) & 0xf);
  1084. pr_info("r 13-16: %08x %08x %08x %08x\n", r[13], r[14], r[15], r[16]);
  1085. }
  1086. static int rtl931x_pie_rule_write(struct rtl838x_switch_priv *priv, int idx, struct pie_rule *pr)
  1087. {
  1088. // Access IACL table (0) via register 1, the table size is 4096
  1089. struct table_reg *q = rtl_table_get(RTL9310_TBL_1, 0);
  1090. u32 r[22];
  1091. int i;
  1092. int block = idx / PIE_BLOCK_SIZE;
  1093. u32 t_select = sw_r32(RTL931X_PIE_BLK_TMPLTE_CTRL(block));
  1094. pr_info("%s: %d, t_select: %08x\n", __func__, idx, t_select);
  1095. for (i = 0; i < 22; i++)
  1096. r[i] = 0;
  1097. if (!pr->valid) {
  1098. rtl_table_write(q, idx);
  1099. rtl_table_release(q);
  1100. return 0;
  1101. }
  1102. rtl931x_write_pie_fixed_fields(r, pr);
  1103. pr_info("%s: template %d\n", __func__, (t_select >> (pr->tid * 4)) & 0xf);
  1104. rtl931x_write_pie_templated(r, pr, fixed_templates[(t_select >> (pr->tid * 4)) & 0xf]);
  1105. rtl931x_write_pie_action(r, pr);
  1106. rtl931x_pie_rule_dump_raw(r);
  1107. for (i = 0; i < 22; i++)
  1108. sw_w32(r[i], rtl_table_data(q, i));
  1109. rtl_table_write(q, idx);
  1110. rtl_table_release(q);
  1111. return 0;
  1112. }
  1113. static bool rtl931x_pie_templ_has(int t, enum template_field_id field_type)
  1114. {
  1115. int i;
  1116. enum template_field_id ft;
  1117. for (i = 0; i < N_FIXED_FIELDS_RTL931X; i++) {
  1118. ft = fixed_templates[t][i];
  1119. if (field_type == ft)
  1120. return true;
  1121. }
  1122. return false;
  1123. }
  1124. /* Verify that the rule pr is compatible with a given template t in block block
  1125. * Note that this function is SoC specific since the values of e.g. TEMPLATE_FIELD_SIP0
  1126. * depend on the SoC
  1127. */
  1128. static int rtl931x_pie_verify_template(struct rtl838x_switch_priv *priv,
  1129. struct pie_rule *pr, int t, int block)
  1130. {
  1131. int i;
  1132. if (!pr->is_ipv6 && pr->sip_m && !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_SIP0))
  1133. return -1;
  1134. if (!pr->is_ipv6 && pr->dip_m && !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_DIP0))
  1135. return -1;
  1136. if (pr->is_ipv6) {
  1137. if ((pr->sip6_m.s6_addr32[0] ||
  1138. pr->sip6_m.s6_addr32[1] ||
  1139. pr->sip6_m.s6_addr32[2] ||
  1140. pr->sip6_m.s6_addr32[3]) &&
  1141. !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_SIP2))
  1142. return -1;
  1143. if ((pr->dip6_m.s6_addr32[0] ||
  1144. pr->dip6_m.s6_addr32[1] ||
  1145. pr->dip6_m.s6_addr32[2] ||
  1146. pr->dip6_m.s6_addr32[3]) &&
  1147. !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_DIP2))
  1148. return -1;
  1149. }
  1150. if (ether_addr_to_u64(pr->smac) && !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_SMAC0))
  1151. return -1;
  1152. if (ether_addr_to_u64(pr->dmac) && !rtl931x_pie_templ_has(t, TEMPLATE_FIELD_DMAC0))
  1153. return -1;
  1154. // TODO: Check more
  1155. i = find_first_zero_bit(&priv->pie_use_bm[block * 4], PIE_BLOCK_SIZE);
  1156. if (i >= PIE_BLOCK_SIZE)
  1157. return -1;
  1158. return i + PIE_BLOCK_SIZE * block;
  1159. }
  1160. static int rtl931x_pie_rule_add(struct rtl838x_switch_priv *priv, struct pie_rule *pr)
  1161. {
  1162. int idx, block, j, t;
  1163. int min_block = 0;
  1164. int max_block = priv->n_pie_blocks / 2;
  1165. if (pr->is_egress) {
  1166. min_block = max_block;
  1167. max_block = priv->n_pie_blocks;
  1168. }
  1169. pr_info("In %s\n", __func__);
  1170. mutex_lock(&priv->pie_mutex);
  1171. for (block = min_block; block < max_block; block++) {
  1172. for (j = 0; j < 2; j++) {
  1173. t = (sw_r32(RTL931X_PIE_BLK_TMPLTE_CTRL(block)) >> (j * 4)) & 0xf;
  1174. pr_info("Testing block %d, template %d, template id %d\n", block, j, t);
  1175. pr_info("%s: %08x\n",
  1176. __func__, sw_r32(RTL931X_PIE_BLK_TMPLTE_CTRL(block)));
  1177. idx = rtl931x_pie_verify_template(priv, pr, t, block);
  1178. if (idx >= 0)
  1179. break;
  1180. }
  1181. if (j < 2)
  1182. break;
  1183. }
  1184. if (block >= priv->n_pie_blocks) {
  1185. mutex_unlock(&priv->pie_mutex);
  1186. return -EOPNOTSUPP;
  1187. }
  1188. pr_info("Using block: %d, index %d, template-id %d\n", block, idx, j);
  1189. set_bit(idx, priv->pie_use_bm);
  1190. pr->valid = true;
  1191. pr->tid = j; // Mapped to template number
  1192. pr->tid_m = 0x1;
  1193. pr->id = idx;
  1194. rtl931x_pie_lookup_enable(priv, idx);
  1195. rtl931x_pie_rule_write(priv, idx, pr);
  1196. mutex_unlock(&priv->pie_mutex);
  1197. return 0;
  1198. }
  1199. /* Delete a range of Packet Inspection Engine rules */
  1200. static int rtl931x_pie_rule_del(struct rtl838x_switch_priv *priv, int index_from, int index_to)
  1201. {
  1202. u32 v = (index_from << 1)| (index_to << 13 ) | BIT(0);
  1203. pr_info("%s: from %d to %d\n", __func__, index_from, index_to);
  1204. mutex_lock(&priv->reg_mutex);
  1205. // Write from-to and execute bit into control register
  1206. sw_w32(v, RTL931X_PIE_CLR_CTRL);
  1207. // Wait until command has completed
  1208. do {
  1209. } while (sw_r32(RTL931X_PIE_CLR_CTRL) & BIT(0));
  1210. mutex_unlock(&priv->reg_mutex);
  1211. return 0;
  1212. }
  1213. static void rtl931x_pie_rule_rm(struct rtl838x_switch_priv *priv, struct pie_rule *pr)
  1214. {
  1215. int idx = pr->id;
  1216. rtl931x_pie_rule_del(priv, idx, idx);
  1217. clear_bit(idx, priv->pie_use_bm);
  1218. }
  1219. static void rtl931x_pie_init(struct rtl838x_switch_priv *priv)
  1220. {
  1221. int i;
  1222. u32 template_selectors;
  1223. mutex_init(&priv->pie_mutex);
  1224. pr_info("%s\n", __func__);
  1225. // Enable ACL lookup on all ports, including CPU_PORT
  1226. for (i = 0; i <= priv->cpu_port; i++)
  1227. sw_w32(1, RTL931X_ACL_PORT_LOOKUP_CTRL(i));
  1228. // Include IPG in metering
  1229. sw_w32_mask(0, 1, RTL931X_METER_GLB_CTRL);
  1230. // Delete all present rules, block size is 128 on all SoC families
  1231. rtl931x_pie_rule_del(priv, 0, priv->n_pie_blocks * 128 - 1);
  1232. // Assign first half blocks 0-7 to VACL phase, second half to IACL
  1233. // 3 bits are used for each block, values for PIE blocks are
  1234. // 6: Disabled, 0: VACL, 1: IACL, 2: EACL
  1235. // And for OpenFlow Flow blocks: 3: Ingress Flow table 0,
  1236. // 4: Ingress Flow Table 3, 5: Egress flow table 0
  1237. for (i = 0; i < priv->n_pie_blocks; i++) {
  1238. int pos = (i % 10) * 3;
  1239. u32 r = RTL931X_PIE_BLK_PHASE_CTRL + 4 * (i / 10);
  1240. if (i < priv->n_pie_blocks / 2)
  1241. sw_w32_mask(0x7 << pos, 0, r);
  1242. else
  1243. sw_w32_mask(0x7 << pos, 1 << pos, r);
  1244. }
  1245. // Enable predefined templates 0, 1 for first quarter of all blocks
  1246. template_selectors = 0 | (1 << 4);
  1247. for (i = 0; i < priv->n_pie_blocks / 4; i++)
  1248. sw_w32(template_selectors, RTL931X_PIE_BLK_TMPLTE_CTRL(i));
  1249. // Enable predefined templates 2, 3 for second quarter of all blocks
  1250. template_selectors = 2 | (3 << 4);
  1251. for (i = priv->n_pie_blocks / 4; i < priv->n_pie_blocks / 2; i++)
  1252. sw_w32(template_selectors, RTL931X_PIE_BLK_TMPLTE_CTRL(i));
  1253. // Enable predefined templates 0, 1 for third quater of all blocks
  1254. template_selectors = 0 | (1 << 4);
  1255. for (i = priv->n_pie_blocks / 2; i < priv->n_pie_blocks * 3 / 4; i++)
  1256. sw_w32(template_selectors, RTL931X_PIE_BLK_TMPLTE_CTRL(i));
  1257. // Enable predefined templates 2, 3 for fourth quater of all blocks
  1258. template_selectors = 2 | (3 << 4);
  1259. for (i = priv->n_pie_blocks * 3 / 4; i < priv->n_pie_blocks; i++)
  1260. sw_w32(template_selectors, RTL931X_PIE_BLK_TMPLTE_CTRL(i));
  1261. }
  1262. int rtl931x_l3_setup(struct rtl838x_switch_priv *priv)
  1263. {
  1264. return 0;
  1265. }
  1266. void rtl931x_vlan_port_keep_tag_set(int port, bool keep_outer, bool keep_inner)
  1267. {
  1268. sw_w32(FIELD_PREP(RTL931X_VLAN_PORT_TAG_EGR_OTAG_STS_MASK,
  1269. keep_outer ? RTL931X_VLAN_PORT_TAG_STS_TAGGED : RTL931X_VLAN_PORT_TAG_STS_UNTAG) |
  1270. FIELD_PREP(RTL931X_VLAN_PORT_TAG_EGR_ITAG_STS_MASK,
  1271. keep_inner ? RTL931X_VLAN_PORT_TAG_STS_TAGGED : RTL931X_VLAN_PORT_TAG_STS_UNTAG),
  1272. RTL931X_VLAN_PORT_TAG_CTRL(port));
  1273. }
  1274. void rtl931x_vlan_port_pvidmode_set(int port, enum pbvlan_type type, enum pbvlan_mode mode)
  1275. {
  1276. if (type == PBVLAN_TYPE_INNER)
  1277. sw_w32_mask(0x3 << 12, mode << 12, RTL931X_VLAN_PORT_IGR_CTRL + (port << 2));
  1278. else
  1279. sw_w32_mask(0x3 << 26, mode << 26, RTL931X_VLAN_PORT_IGR_CTRL + (port << 2));
  1280. }
  1281. void rtl931x_vlan_port_pvid_set(int port, enum pbvlan_type type, int pvid)
  1282. {
  1283. if (type == PBVLAN_TYPE_INNER)
  1284. sw_w32_mask(0xfff, pvid, RTL931X_VLAN_PORT_IGR_CTRL + (port << 2));
  1285. else
  1286. sw_w32_mask(0xfff << 14, pvid << 14, RTL931X_VLAN_PORT_IGR_CTRL + (port << 2));
  1287. }
  1288. static void rtl931x_set_igr_filter(int port, enum igr_filter state)
  1289. {
  1290. sw_w32_mask(0x3 << ((port & 0xf)<<1), state << ((port & 0xf)<<1),
  1291. RTL931X_VLAN_PORT_IGR_FLTR + (((port >> 4) << 2)));
  1292. }
  1293. static void rtl931x_set_egr_filter(int port, enum egr_filter state)
  1294. {
  1295. sw_w32_mask(0x1 << (port % 0x20), state << (port % 0x20),
  1296. RTL931X_VLAN_PORT_EGR_FLTR + (((port >> 5) << 2)));
  1297. }
  1298. void rtl931x_set_distribution_algorithm(int group, int algoidx, u32 algomsk)
  1299. {
  1300. u32 l3shift = 0;
  1301. u32 newmask = 0;
  1302. /* TODO: for now we set algoidx to 0 */
  1303. algoidx = 0;
  1304. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SIP_BIT) {
  1305. l3shift = 4;
  1306. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SIP_BIT;
  1307. }
  1308. if (algomsk & TRUNK_DISTRIBUTION_ALGO_DIP_BIT) {
  1309. l3shift = 4;
  1310. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_DIP_BIT;
  1311. }
  1312. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SRC_L4PORT_BIT) {
  1313. l3shift = 4;
  1314. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SRC_L4PORT_BIT;
  1315. }
  1316. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SRC_L4PORT_BIT) {
  1317. l3shift = 4;
  1318. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SRC_L4PORT_BIT;
  1319. }
  1320. if (l3shift == 4) {
  1321. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SMAC_BIT)
  1322. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_SMAC_BIT;
  1323. if (algomsk & TRUNK_DISTRIBUTION_ALGO_DMAC_BIT)
  1324. newmask |= TRUNK_DISTRIBUTION_ALGO_L3_DMAC_BIT;
  1325. } else {
  1326. if (algomsk & TRUNK_DISTRIBUTION_ALGO_SMAC_BIT)
  1327. newmask |= TRUNK_DISTRIBUTION_ALGO_L2_SMAC_BIT;
  1328. if (algomsk & TRUNK_DISTRIBUTION_ALGO_DMAC_BIT)
  1329. newmask |= TRUNK_DISTRIBUTION_ALGO_L2_DMAC_BIT;
  1330. }
  1331. sw_w32(newmask << l3shift, RTL931X_TRK_HASH_CTRL + (algoidx << 2));
  1332. }
  1333. static void rtl931x_led_init(struct rtl838x_switch_priv *priv)
  1334. {
  1335. int i, pos;
  1336. u32 v, set;
  1337. u64 pm_copper = 0, pm_fiber = 0;
  1338. u32 setlen;
  1339. const __be32 *led_set;
  1340. char set_name[9];
  1341. struct device_node *node;
  1342. pr_info("%s called\n", __func__);
  1343. node = of_find_compatible_node(NULL, NULL, "realtek,rtl9300-leds");
  1344. if (!node) {
  1345. pr_info("%s No compatible LED node found\n", __func__);
  1346. return;
  1347. }
  1348. for (i = 0; i < priv->cpu_port; i++) {
  1349. pos = (i << 1) % 32;
  1350. sw_w32_mask(0x3 << pos, 0, RTL931X_LED_PORT_FIB_SET_SEL_CTRL(i));
  1351. sw_w32_mask(0x3 << pos, 0, RTL931X_LED_PORT_COPR_SET_SEL_CTRL(i));
  1352. if (!priv->ports[i].phy)
  1353. continue;
  1354. v = 0x1; // Found on the EdgeCore, but we do not have any HW description
  1355. sw_w32_mask(0x3 << pos, v << pos, RTL931X_LED_PORT_NUM_CTRL(i));
  1356. if (priv->ports[i].phy_is_integrated)
  1357. pm_fiber |= BIT_ULL(i);
  1358. else
  1359. pm_copper |= BIT_ULL(i);
  1360. set = priv->ports[i].led_set;
  1361. sw_w32_mask(0, set << pos, RTL931X_LED_PORT_COPR_SET_SEL_CTRL(i));
  1362. sw_w32_mask(0, set << pos, RTL931X_LED_PORT_FIB_SET_SEL_CTRL(i));
  1363. }
  1364. for (i = 0; i < 4; i++) {
  1365. sprintf(set_name, "led_set%d", i);
  1366. pr_info(">%s<\n", set_name);
  1367. led_set = of_get_property(node, set_name, &setlen);
  1368. if (!led_set || setlen != 16)
  1369. break;
  1370. v = be32_to_cpup(led_set) << 16 | be32_to_cpup(led_set + 1);
  1371. sw_w32(v, RTL931X_LED_SET0_0_CTRL - 4 - i * 8);
  1372. v = be32_to_cpup(led_set + 2) << 16 | be32_to_cpup(led_set + 3);
  1373. sw_w32(v, RTL931X_LED_SET0_0_CTRL - i * 8);
  1374. }
  1375. // Set LED mode to serial (0x1)
  1376. sw_w32_mask(0x3, 0x1, RTL931X_LED_GLB_CTRL);
  1377. rtl839x_set_port_reg_le(pm_copper, RTL931X_LED_PORT_COPR_MASK_CTRL);
  1378. rtl839x_set_port_reg_le(pm_fiber, RTL931X_LED_PORT_FIB_MASK_CTRL);
  1379. rtl839x_set_port_reg_le(pm_copper | pm_fiber, RTL931X_LED_PORT_COMBO_MASK_CTRL);
  1380. for (i = 0; i < 32; i++)
  1381. pr_info("%s %08x: %08x\n",__func__, 0xbb000600 + i * 4, sw_r32(0x0600 + i * 4));
  1382. }
  1383. const struct rtl838x_reg rtl931x_reg = {
  1384. .mask_port_reg_be = rtl839x_mask_port_reg_be,
  1385. .set_port_reg_be = rtl839x_set_port_reg_be,
  1386. .get_port_reg_be = rtl839x_get_port_reg_be,
  1387. .mask_port_reg_le = rtl839x_mask_port_reg_le,
  1388. .set_port_reg_le = rtl839x_set_port_reg_le,
  1389. .get_port_reg_le = rtl839x_get_port_reg_le,
  1390. .stat_port_rst = RTL931X_STAT_PORT_RST,
  1391. .stat_rst = RTL931X_STAT_RST,
  1392. .stat_port_std_mib = 0, // Not defined
  1393. .traffic_enable = rtl931x_traffic_enable,
  1394. .traffic_disable = rtl931x_traffic_disable,
  1395. .traffic_get = rtl931x_traffic_get,
  1396. .traffic_set = rtl931x_traffic_set,
  1397. .l2_ctrl_0 = RTL931X_L2_CTRL,
  1398. .l2_ctrl_1 = RTL931X_L2_AGE_CTRL,
  1399. .l2_port_aging_out = RTL931X_L2_PORT_AGE_CTRL,
  1400. .set_ageing_time = rtl931x_set_ageing_time,
  1401. // .smi_poll_ctrl does not exist
  1402. .l2_tbl_flush_ctrl = RTL931X_L2_TBL_FLUSH_CTRL,
  1403. .exec_tbl0_cmd = rtl931x_exec_tbl0_cmd,
  1404. .exec_tbl1_cmd = rtl931x_exec_tbl1_cmd,
  1405. .tbl_access_data_0 = rtl931x_tbl_access_data_0,
  1406. .isr_glb_src = RTL931X_ISR_GLB_SRC,
  1407. .isr_port_link_sts_chg = RTL931X_ISR_PORT_LINK_STS_CHG,
  1408. .imr_port_link_sts_chg = RTL931X_IMR_PORT_LINK_STS_CHG,
  1409. // imr_glb does not exist on RTL931X
  1410. .vlan_tables_read = rtl931x_vlan_tables_read,
  1411. .vlan_set_tagged = rtl931x_vlan_set_tagged,
  1412. .vlan_set_untagged = rtl931x_vlan_set_untagged,
  1413. .vlan_profile_dump = rtl931x_vlan_profile_dump,
  1414. .vlan_profile_setup = rtl931x_vlan_profile_setup,
  1415. .vlan_fwd_on_inner = rtl931x_vlan_fwd_on_inner,
  1416. .stp_get = rtl931x_stp_get,
  1417. .stp_set = rtl931x_stp_set,
  1418. .mac_force_mode_ctrl = rtl931x_mac_force_mode_ctrl,
  1419. .mac_port_ctrl = rtl931x_mac_port_ctrl,
  1420. .l2_port_new_salrn = rtl931x_l2_port_new_salrn,
  1421. .l2_port_new_sa_fwd = rtl931x_l2_port_new_sa_fwd,
  1422. .mir_ctrl = RTL931X_MIR_CTRL,
  1423. .mir_dpm = RTL931X_MIR_DPM_CTRL,
  1424. .mir_spm = RTL931X_MIR_SPM_CTRL,
  1425. .mac_link_sts = RTL931X_MAC_LINK_STS,
  1426. .mac_link_dup_sts = RTL931X_MAC_LINK_DUP_STS,
  1427. .mac_link_spd_sts = rtl931x_mac_link_spd_sts,
  1428. .mac_rx_pause_sts = RTL931X_MAC_RX_PAUSE_STS,
  1429. .mac_tx_pause_sts = RTL931X_MAC_TX_PAUSE_STS,
  1430. .read_l2_entry_using_hash = rtl931x_read_l2_entry_using_hash,
  1431. .write_l2_entry_using_hash = rtl931x_write_l2_entry_using_hash,
  1432. .read_cam = rtl931x_read_cam,
  1433. .write_cam = rtl931x_write_cam,
  1434. .vlan_port_keep_tag_set = rtl931x_vlan_port_keep_tag_set,
  1435. .vlan_port_pvidmode_set = rtl931x_vlan_port_pvidmode_set,
  1436. .vlan_port_pvid_set = rtl931x_vlan_port_pvid_set,
  1437. .trk_mbr_ctr = rtl931x_trk_mbr_ctr,
  1438. .set_vlan_igr_filter = rtl931x_set_igr_filter,
  1439. .set_vlan_egr_filter = rtl931x_set_egr_filter,
  1440. .set_distribution_algorithm = rtl931x_set_distribution_algorithm,
  1441. .l2_hash_key = rtl931x_l2_hash_key,
  1442. .read_mcast_pmask = rtl931x_read_mcast_pmask,
  1443. .write_mcast_pmask = rtl931x_write_mcast_pmask,
  1444. .pie_init = rtl931x_pie_init,
  1445. .pie_rule_write = rtl931x_pie_rule_write,
  1446. .pie_rule_add = rtl931x_pie_rule_add,
  1447. .pie_rule_rm = rtl931x_pie_rule_rm,
  1448. .l2_learning_setup = rtl931x_l2_learning_setup,
  1449. .l3_setup = rtl931x_l3_setup,
  1450. .led_init = rtl931x_led_init,
  1451. };