rtl931x.c 50 KB

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