rtl931x.c 49 KB

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