bcm_sf2_cfp.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * Broadcom Starfighter 2 DSA switch CFP support
  3. *
  4. * Copyright (C) 2016, Broadcom
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/list.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/if_ether.h>
  14. #include <linux/in.h>
  15. #include <linux/netdevice.h>
  16. #include <net/dsa.h>
  17. #include <linux/bitmap.h>
  18. #include "bcm_sf2.h"
  19. #include "bcm_sf2_regs.h"
  20. struct cfp_udf_slice_layout {
  21. u8 slices[UDFS_PER_SLICE];
  22. u32 mask_value;
  23. u32 base_offset;
  24. };
  25. struct cfp_udf_layout {
  26. struct cfp_udf_slice_layout udfs[UDF_NUM_SLICES];
  27. };
  28. static const u8 zero_slice[UDFS_PER_SLICE] = { };
  29. /* UDF slices layout for a TCPv4/UDPv4 specification */
  30. static const struct cfp_udf_layout udf_tcpip4_layout = {
  31. .udfs = {
  32. [1] = {
  33. .slices = {
  34. /* End of L2, byte offset 12, src IP[0:15] */
  35. CFG_UDF_EOL2 | 6,
  36. /* End of L2, byte offset 14, src IP[16:31] */
  37. CFG_UDF_EOL2 | 7,
  38. /* End of L2, byte offset 16, dst IP[0:15] */
  39. CFG_UDF_EOL2 | 8,
  40. /* End of L2, byte offset 18, dst IP[16:31] */
  41. CFG_UDF_EOL2 | 9,
  42. /* End of L3, byte offset 0, src port */
  43. CFG_UDF_EOL3 | 0,
  44. /* End of L3, byte offset 2, dst port */
  45. CFG_UDF_EOL3 | 1,
  46. 0, 0, 0
  47. },
  48. .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
  49. .base_offset = CORE_UDF_0_A_0_8_PORT_0 + UDF_SLICE_OFFSET,
  50. },
  51. },
  52. };
  53. /* UDF slices layout for a TCPv6/UDPv6 specification */
  54. static const struct cfp_udf_layout udf_tcpip6_layout = {
  55. .udfs = {
  56. [0] = {
  57. .slices = {
  58. /* End of L2, byte offset 8, src IP[0:15] */
  59. CFG_UDF_EOL2 | 4,
  60. /* End of L2, byte offset 10, src IP[16:31] */
  61. CFG_UDF_EOL2 | 5,
  62. /* End of L2, byte offset 12, src IP[32:47] */
  63. CFG_UDF_EOL2 | 6,
  64. /* End of L2, byte offset 14, src IP[48:63] */
  65. CFG_UDF_EOL2 | 7,
  66. /* End of L2, byte offset 16, src IP[64:79] */
  67. CFG_UDF_EOL2 | 8,
  68. /* End of L2, byte offset 18, src IP[80:95] */
  69. CFG_UDF_EOL2 | 9,
  70. /* End of L2, byte offset 20, src IP[96:111] */
  71. CFG_UDF_EOL2 | 10,
  72. /* End of L2, byte offset 22, src IP[112:127] */
  73. CFG_UDF_EOL2 | 11,
  74. /* End of L3, byte offset 0, src port */
  75. CFG_UDF_EOL3 | 0,
  76. },
  77. .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
  78. .base_offset = CORE_UDF_0_B_0_8_PORT_0,
  79. },
  80. [3] = {
  81. .slices = {
  82. /* End of L2, byte offset 24, dst IP[0:15] */
  83. CFG_UDF_EOL2 | 12,
  84. /* End of L2, byte offset 26, dst IP[16:31] */
  85. CFG_UDF_EOL2 | 13,
  86. /* End of L2, byte offset 28, dst IP[32:47] */
  87. CFG_UDF_EOL2 | 14,
  88. /* End of L2, byte offset 30, dst IP[48:63] */
  89. CFG_UDF_EOL2 | 15,
  90. /* End of L2, byte offset 32, dst IP[64:79] */
  91. CFG_UDF_EOL2 | 16,
  92. /* End of L2, byte offset 34, dst IP[80:95] */
  93. CFG_UDF_EOL2 | 17,
  94. /* End of L2, byte offset 36, dst IP[96:111] */
  95. CFG_UDF_EOL2 | 18,
  96. /* End of L2, byte offset 38, dst IP[112:127] */
  97. CFG_UDF_EOL2 | 19,
  98. /* End of L3, byte offset 2, dst port */
  99. CFG_UDF_EOL3 | 1,
  100. },
  101. .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
  102. .base_offset = CORE_UDF_0_D_0_11_PORT_0,
  103. },
  104. },
  105. };
  106. static inline unsigned int bcm_sf2_get_num_udf_slices(const u8 *layout)
  107. {
  108. unsigned int i, count = 0;
  109. for (i = 0; i < UDFS_PER_SLICE; i++) {
  110. if (layout[i] != 0)
  111. count++;
  112. }
  113. return count;
  114. }
  115. static inline u32 udf_upper_bits(unsigned int num_udf)
  116. {
  117. return GENMASK(num_udf - 1, 0) >> (UDFS_PER_SLICE - 1);
  118. }
  119. static inline u32 udf_lower_bits(unsigned int num_udf)
  120. {
  121. return (u8)GENMASK(num_udf - 1, 0);
  122. }
  123. static unsigned int bcm_sf2_get_slice_number(const struct cfp_udf_layout *l,
  124. unsigned int start)
  125. {
  126. const struct cfp_udf_slice_layout *slice_layout;
  127. unsigned int slice_idx;
  128. for (slice_idx = start; slice_idx < UDF_NUM_SLICES; slice_idx++) {
  129. slice_layout = &l->udfs[slice_idx];
  130. if (memcmp(slice_layout->slices, zero_slice,
  131. sizeof(zero_slice)))
  132. break;
  133. }
  134. return slice_idx;
  135. }
  136. static void bcm_sf2_cfp_udf_set(struct bcm_sf2_priv *priv,
  137. const struct cfp_udf_layout *layout,
  138. unsigned int slice_num)
  139. {
  140. u32 offset = layout->udfs[slice_num].base_offset;
  141. unsigned int i;
  142. for (i = 0; i < UDFS_PER_SLICE; i++)
  143. core_writel(priv, layout->udfs[slice_num].slices[i],
  144. offset + i * 4);
  145. }
  146. static int bcm_sf2_cfp_op(struct bcm_sf2_priv *priv, unsigned int op)
  147. {
  148. unsigned int timeout = 1000;
  149. u32 reg;
  150. reg = core_readl(priv, CORE_CFP_ACC);
  151. reg &= ~(OP_SEL_MASK | RAM_SEL_MASK);
  152. reg |= OP_STR_DONE | op;
  153. core_writel(priv, reg, CORE_CFP_ACC);
  154. do {
  155. reg = core_readl(priv, CORE_CFP_ACC);
  156. if (!(reg & OP_STR_DONE))
  157. break;
  158. cpu_relax();
  159. } while (timeout--);
  160. if (!timeout)
  161. return -ETIMEDOUT;
  162. return 0;
  163. }
  164. static inline void bcm_sf2_cfp_rule_addr_set(struct bcm_sf2_priv *priv,
  165. unsigned int addr)
  166. {
  167. u32 reg;
  168. WARN_ON(addr >= priv->num_cfp_rules);
  169. reg = core_readl(priv, CORE_CFP_ACC);
  170. reg &= ~(XCESS_ADDR_MASK << XCESS_ADDR_SHIFT);
  171. reg |= addr << XCESS_ADDR_SHIFT;
  172. core_writel(priv, reg, CORE_CFP_ACC);
  173. }
  174. static inline unsigned int bcm_sf2_cfp_rule_size(struct bcm_sf2_priv *priv)
  175. {
  176. /* Entry #0 is reserved */
  177. return priv->num_cfp_rules - 1;
  178. }
  179. static int bcm_sf2_cfp_act_pol_set(struct bcm_sf2_priv *priv,
  180. unsigned int rule_index,
  181. unsigned int port_num,
  182. unsigned int queue_num,
  183. bool fwd_map_change)
  184. {
  185. int ret;
  186. u32 reg;
  187. /* Replace ARL derived destination with DST_MAP derived, define
  188. * which port and queue this should be forwarded to.
  189. */
  190. if (fwd_map_change)
  191. reg = CHANGE_FWRD_MAP_IB_REP_ARL |
  192. BIT(port_num + DST_MAP_IB_SHIFT) |
  193. CHANGE_TC | queue_num << NEW_TC_SHIFT;
  194. else
  195. reg = 0;
  196. core_writel(priv, reg, CORE_ACT_POL_DATA0);
  197. /* Set classification ID that needs to be put in Broadcom tag */
  198. core_writel(priv, rule_index << CHAIN_ID_SHIFT, CORE_ACT_POL_DATA1);
  199. core_writel(priv, 0, CORE_ACT_POL_DATA2);
  200. /* Configure policer RAM now */
  201. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | ACT_POL_RAM);
  202. if (ret) {
  203. pr_err("Policer entry at %d failed\n", rule_index);
  204. return ret;
  205. }
  206. /* Disable the policer */
  207. core_writel(priv, POLICER_MODE_DISABLE, CORE_RATE_METER0);
  208. /* Now the rate meter */
  209. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | RATE_METER_RAM);
  210. if (ret) {
  211. pr_err("Meter entry at %d failed\n", rule_index);
  212. return ret;
  213. }
  214. return 0;
  215. }
  216. static void bcm_sf2_cfp_slice_ipv4(struct bcm_sf2_priv *priv,
  217. struct ethtool_tcpip4_spec *v4_spec,
  218. unsigned int slice_num,
  219. bool mask)
  220. {
  221. u32 reg, offset;
  222. /* C-Tag [31:24]
  223. * UDF_n_A8 [23:8]
  224. * UDF_n_A7 [7:0]
  225. */
  226. reg = 0;
  227. if (mask)
  228. offset = CORE_CFP_MASK_PORT(4);
  229. else
  230. offset = CORE_CFP_DATA_PORT(4);
  231. core_writel(priv, reg, offset);
  232. /* UDF_n_A7 [31:24]
  233. * UDF_n_A6 [23:8]
  234. * UDF_n_A5 [7:0]
  235. */
  236. reg = be16_to_cpu(v4_spec->pdst) >> 8;
  237. if (mask)
  238. offset = CORE_CFP_MASK_PORT(3);
  239. else
  240. offset = CORE_CFP_DATA_PORT(3);
  241. core_writel(priv, reg, offset);
  242. /* UDF_n_A5 [31:24]
  243. * UDF_n_A4 [23:8]
  244. * UDF_n_A3 [7:0]
  245. */
  246. reg = (be16_to_cpu(v4_spec->pdst) & 0xff) << 24 |
  247. (u32)be16_to_cpu(v4_spec->psrc) << 8 |
  248. (be32_to_cpu(v4_spec->ip4dst) & 0x0000ff00) >> 8;
  249. if (mask)
  250. offset = CORE_CFP_MASK_PORT(2);
  251. else
  252. offset = CORE_CFP_DATA_PORT(2);
  253. core_writel(priv, reg, offset);
  254. /* UDF_n_A3 [31:24]
  255. * UDF_n_A2 [23:8]
  256. * UDF_n_A1 [7:0]
  257. */
  258. reg = (u32)(be32_to_cpu(v4_spec->ip4dst) & 0xff) << 24 |
  259. (u32)(be32_to_cpu(v4_spec->ip4dst) >> 16) << 8 |
  260. (be32_to_cpu(v4_spec->ip4src) & 0x0000ff00) >> 8;
  261. if (mask)
  262. offset = CORE_CFP_MASK_PORT(1);
  263. else
  264. offset = CORE_CFP_DATA_PORT(1);
  265. core_writel(priv, reg, offset);
  266. /* UDF_n_A1 [31:24]
  267. * UDF_n_A0 [23:8]
  268. * Reserved [7:4]
  269. * Slice ID [3:2]
  270. * Slice valid [1:0]
  271. */
  272. reg = (u32)(be32_to_cpu(v4_spec->ip4src) & 0xff) << 24 |
  273. (u32)(be32_to_cpu(v4_spec->ip4src) >> 16) << 8 |
  274. SLICE_NUM(slice_num) | SLICE_VALID;
  275. if (mask)
  276. offset = CORE_CFP_MASK_PORT(0);
  277. else
  278. offset = CORE_CFP_DATA_PORT(0);
  279. core_writel(priv, reg, offset);
  280. }
  281. static int bcm_sf2_cfp_ipv4_rule_set(struct bcm_sf2_priv *priv, int port,
  282. unsigned int port_num,
  283. unsigned int queue_num,
  284. struct ethtool_rx_flow_spec *fs)
  285. {
  286. struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
  287. const struct cfp_udf_layout *layout;
  288. unsigned int slice_num, rule_index;
  289. u8 ip_proto, ip_frag;
  290. u8 num_udf;
  291. u32 reg;
  292. int ret;
  293. switch (fs->flow_type & ~FLOW_EXT) {
  294. case TCP_V4_FLOW:
  295. ip_proto = IPPROTO_TCP;
  296. v4_spec = &fs->h_u.tcp_ip4_spec;
  297. v4_m_spec = &fs->m_u.tcp_ip4_spec;
  298. break;
  299. case UDP_V4_FLOW:
  300. ip_proto = IPPROTO_UDP;
  301. v4_spec = &fs->h_u.udp_ip4_spec;
  302. v4_m_spec = &fs->m_u.udp_ip4_spec;
  303. break;
  304. default:
  305. return -EINVAL;
  306. }
  307. ip_frag = be32_to_cpu(fs->m_ext.data[0]);
  308. /* Locate the first rule available */
  309. if (fs->location == RX_CLS_LOC_ANY)
  310. rule_index = find_first_zero_bit(priv->cfp.used,
  311. priv->num_cfp_rules);
  312. else
  313. rule_index = fs->location;
  314. if (rule_index > bcm_sf2_cfp_rule_size(priv))
  315. return -ENOSPC;
  316. layout = &udf_tcpip4_layout;
  317. /* We only use one UDF slice for now */
  318. slice_num = bcm_sf2_get_slice_number(layout, 0);
  319. if (slice_num == UDF_NUM_SLICES)
  320. return -EINVAL;
  321. num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
  322. /* Apply the UDF layout for this filter */
  323. bcm_sf2_cfp_udf_set(priv, layout, slice_num);
  324. /* Apply to all packets received through this port */
  325. core_writel(priv, BIT(port), CORE_CFP_DATA_PORT(7));
  326. /* Source port map match */
  327. core_writel(priv, 0xff, CORE_CFP_MASK_PORT(7));
  328. /* S-Tag status [31:30]
  329. * C-Tag status [29:28]
  330. * L2 framing [27:26]
  331. * L3 framing [25:24]
  332. * IP ToS [23:16]
  333. * IP proto [15:08]
  334. * IP Fragm [7]
  335. * Non 1st frag [6]
  336. * IP Authen [5]
  337. * TTL range [4:3]
  338. * PPPoE session [2]
  339. * Reserved [1]
  340. * UDF_Valid[8] [0]
  341. */
  342. core_writel(priv, v4_spec->tos << IPTOS_SHIFT |
  343. ip_proto << IPPROTO_SHIFT | ip_frag << IP_FRAG_SHIFT |
  344. udf_upper_bits(num_udf),
  345. CORE_CFP_DATA_PORT(6));
  346. /* Mask with the specific layout for IPv4 packets */
  347. core_writel(priv, layout->udfs[slice_num].mask_value |
  348. udf_upper_bits(num_udf), CORE_CFP_MASK_PORT(6));
  349. /* UDF_Valid[7:0] [31:24]
  350. * S-Tag [23:8]
  351. * C-Tag [7:0]
  352. */
  353. core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_DATA_PORT(5));
  354. /* Mask all but valid UDFs */
  355. core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_MASK_PORT(5));
  356. /* Program the match and the mask */
  357. bcm_sf2_cfp_slice_ipv4(priv, v4_spec, slice_num, false);
  358. bcm_sf2_cfp_slice_ipv4(priv, v4_m_spec, SLICE_NUM_MASK, true);
  359. /* Insert into TCAM now */
  360. bcm_sf2_cfp_rule_addr_set(priv, rule_index);
  361. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
  362. if (ret) {
  363. pr_err("TCAM entry at addr %d failed\n", rule_index);
  364. return ret;
  365. }
  366. /* Insert into Action and policer RAMs now */
  367. ret = bcm_sf2_cfp_act_pol_set(priv, rule_index, port_num,
  368. queue_num, true);
  369. if (ret)
  370. return ret;
  371. /* Turn on CFP for this rule now */
  372. reg = core_readl(priv, CORE_CFP_CTL_REG);
  373. reg |= BIT(port);
  374. core_writel(priv, reg, CORE_CFP_CTL_REG);
  375. /* Flag the rule as being used and return it */
  376. set_bit(rule_index, priv->cfp.used);
  377. set_bit(rule_index, priv->cfp.unique);
  378. fs->location = rule_index;
  379. return 0;
  380. }
  381. static void bcm_sf2_cfp_slice_ipv6(struct bcm_sf2_priv *priv,
  382. const __be32 *ip6_addr, const __be16 port,
  383. unsigned int slice_num,
  384. bool mask)
  385. {
  386. u32 reg, tmp, val, offset;
  387. /* C-Tag [31:24]
  388. * UDF_n_B8 [23:8] (port)
  389. * UDF_n_B7 (upper) [7:0] (addr[15:8])
  390. */
  391. reg = be32_to_cpu(ip6_addr[3]);
  392. val = (u32)be16_to_cpu(port) << 8 | ((reg >> 8) & 0xff);
  393. if (mask)
  394. offset = CORE_CFP_MASK_PORT(4);
  395. else
  396. offset = CORE_CFP_DATA_PORT(4);
  397. core_writel(priv, val, offset);
  398. /* UDF_n_B7 (lower) [31:24] (addr[7:0])
  399. * UDF_n_B6 [23:8] (addr[31:16])
  400. * UDF_n_B5 (upper) [7:0] (addr[47:40])
  401. */
  402. tmp = be32_to_cpu(ip6_addr[2]);
  403. val = (u32)(reg & 0xff) << 24 | (u32)(reg >> 16) << 8 |
  404. ((tmp >> 8) & 0xff);
  405. if (mask)
  406. offset = CORE_CFP_MASK_PORT(3);
  407. else
  408. offset = CORE_CFP_DATA_PORT(3);
  409. core_writel(priv, val, offset);
  410. /* UDF_n_B5 (lower) [31:24] (addr[39:32])
  411. * UDF_n_B4 [23:8] (addr[63:48])
  412. * UDF_n_B3 (upper) [7:0] (addr[79:72])
  413. */
  414. reg = be32_to_cpu(ip6_addr[1]);
  415. val = (u32)(tmp & 0xff) << 24 | (u32)(tmp >> 16) << 8 |
  416. ((reg >> 8) & 0xff);
  417. if (mask)
  418. offset = CORE_CFP_MASK_PORT(2);
  419. else
  420. offset = CORE_CFP_DATA_PORT(2);
  421. core_writel(priv, val, offset);
  422. /* UDF_n_B3 (lower) [31:24] (addr[71:64])
  423. * UDF_n_B2 [23:8] (addr[95:80])
  424. * UDF_n_B1 (upper) [7:0] (addr[111:104])
  425. */
  426. tmp = be32_to_cpu(ip6_addr[0]);
  427. val = (u32)(reg & 0xff) << 24 | (u32)(reg >> 16) << 8 |
  428. ((tmp >> 8) & 0xff);
  429. if (mask)
  430. offset = CORE_CFP_MASK_PORT(1);
  431. else
  432. offset = CORE_CFP_DATA_PORT(1);
  433. core_writel(priv, val, offset);
  434. /* UDF_n_B1 (lower) [31:24] (addr[103:96])
  435. * UDF_n_B0 [23:8] (addr[127:112])
  436. * Reserved [7:4]
  437. * Slice ID [3:2]
  438. * Slice valid [1:0]
  439. */
  440. reg = (u32)(tmp & 0xff) << 24 | (u32)(tmp >> 16) << 8 |
  441. SLICE_NUM(slice_num) | SLICE_VALID;
  442. if (mask)
  443. offset = CORE_CFP_MASK_PORT(0);
  444. else
  445. offset = CORE_CFP_DATA_PORT(0);
  446. core_writel(priv, reg, offset);
  447. }
  448. static int bcm_sf2_cfp_ipv6_rule_set(struct bcm_sf2_priv *priv, int port,
  449. unsigned int port_num,
  450. unsigned int queue_num,
  451. struct ethtool_rx_flow_spec *fs)
  452. {
  453. struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
  454. unsigned int slice_num, rule_index[2];
  455. const struct cfp_udf_layout *layout;
  456. u8 ip_proto, ip_frag;
  457. int ret = 0;
  458. u8 num_udf;
  459. u32 reg;
  460. switch (fs->flow_type & ~FLOW_EXT) {
  461. case TCP_V6_FLOW:
  462. ip_proto = IPPROTO_TCP;
  463. v6_spec = &fs->h_u.tcp_ip6_spec;
  464. v6_m_spec = &fs->m_u.tcp_ip6_spec;
  465. break;
  466. case UDP_V6_FLOW:
  467. ip_proto = IPPROTO_UDP;
  468. v6_spec = &fs->h_u.udp_ip6_spec;
  469. v6_m_spec = &fs->m_u.udp_ip6_spec;
  470. break;
  471. default:
  472. return -EINVAL;
  473. }
  474. ip_frag = be32_to_cpu(fs->m_ext.data[0]);
  475. layout = &udf_tcpip6_layout;
  476. slice_num = bcm_sf2_get_slice_number(layout, 0);
  477. if (slice_num == UDF_NUM_SLICES)
  478. return -EINVAL;
  479. num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
  480. /* Negotiate two indexes, one for the second half which we are chained
  481. * from, which is what we will return to user-space, and a second one
  482. * which is used to store its first half. That first half does not
  483. * allow any choice of placement, so it just needs to find the next
  484. * available bit. We return the second half as fs->location because
  485. * that helps with the rule lookup later on since the second half is
  486. * chained from its first half, we can easily identify IPv6 CFP rules
  487. * by looking whether they carry a CHAIN_ID.
  488. *
  489. * We also want the second half to have a lower rule_index than its
  490. * first half because the HW search is by incrementing addresses.
  491. */
  492. if (fs->location == RX_CLS_LOC_ANY)
  493. rule_index[1] = find_first_zero_bit(priv->cfp.used,
  494. priv->num_cfp_rules);
  495. else
  496. rule_index[1] = fs->location;
  497. if (rule_index[1] > bcm_sf2_cfp_rule_size(priv))
  498. return -ENOSPC;
  499. /* Flag it as used (cleared on error path) such that we can immediately
  500. * obtain a second one to chain from.
  501. */
  502. set_bit(rule_index[1], priv->cfp.used);
  503. rule_index[0] = find_first_zero_bit(priv->cfp.used,
  504. priv->num_cfp_rules);
  505. if (rule_index[0] > bcm_sf2_cfp_rule_size(priv)) {
  506. ret = -ENOSPC;
  507. goto out_err;
  508. }
  509. /* Apply the UDF layout for this filter */
  510. bcm_sf2_cfp_udf_set(priv, layout, slice_num);
  511. /* Apply to all packets received through this port */
  512. core_writel(priv, BIT(port), CORE_CFP_DATA_PORT(7));
  513. /* Source port map match */
  514. core_writel(priv, 0xff, CORE_CFP_MASK_PORT(7));
  515. /* S-Tag status [31:30]
  516. * C-Tag status [29:28]
  517. * L2 framing [27:26]
  518. * L3 framing [25:24]
  519. * IP ToS [23:16]
  520. * IP proto [15:08]
  521. * IP Fragm [7]
  522. * Non 1st frag [6]
  523. * IP Authen [5]
  524. * TTL range [4:3]
  525. * PPPoE session [2]
  526. * Reserved [1]
  527. * UDF_Valid[8] [0]
  528. */
  529. reg = 1 << L3_FRAMING_SHIFT | ip_proto << IPPROTO_SHIFT |
  530. ip_frag << IP_FRAG_SHIFT | udf_upper_bits(num_udf);
  531. core_writel(priv, reg, CORE_CFP_DATA_PORT(6));
  532. /* Mask with the specific layout for IPv6 packets including
  533. * UDF_Valid[8]
  534. */
  535. reg = layout->udfs[slice_num].mask_value | udf_upper_bits(num_udf);
  536. core_writel(priv, reg, CORE_CFP_MASK_PORT(6));
  537. /* UDF_Valid[7:0] [31:24]
  538. * S-Tag [23:8]
  539. * C-Tag [7:0]
  540. */
  541. core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_DATA_PORT(5));
  542. /* Mask all but valid UDFs */
  543. core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_MASK_PORT(5));
  544. /* Slice the IPv6 source address and port */
  545. bcm_sf2_cfp_slice_ipv6(priv, v6_spec->ip6src, v6_spec->psrc,
  546. slice_num, false);
  547. bcm_sf2_cfp_slice_ipv6(priv, v6_m_spec->ip6src, v6_m_spec->psrc,
  548. SLICE_NUM_MASK, true);
  549. /* Insert into TCAM now because we need to insert a second rule */
  550. bcm_sf2_cfp_rule_addr_set(priv, rule_index[0]);
  551. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
  552. if (ret) {
  553. pr_err("TCAM entry at addr %d failed\n", rule_index[0]);
  554. goto out_err;
  555. }
  556. /* Insert into Action and policer RAMs now */
  557. ret = bcm_sf2_cfp_act_pol_set(priv, rule_index[0], port_num,
  558. queue_num, false);
  559. if (ret)
  560. goto out_err;
  561. /* Now deal with the second slice to chain this rule */
  562. slice_num = bcm_sf2_get_slice_number(layout, slice_num + 1);
  563. if (slice_num == UDF_NUM_SLICES) {
  564. ret = -EINVAL;
  565. goto out_err;
  566. }
  567. num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
  568. /* Apply the UDF layout for this filter */
  569. bcm_sf2_cfp_udf_set(priv, layout, slice_num);
  570. /* Chained rule, source port match is coming from the rule we are
  571. * chained from.
  572. */
  573. core_writel(priv, 0, CORE_CFP_DATA_PORT(7));
  574. core_writel(priv, 0, CORE_CFP_MASK_PORT(7));
  575. /*
  576. * CHAIN ID [31:24] chain to previous slice
  577. * Reserved [23:20]
  578. * UDF_Valid[11:8] [19:16]
  579. * UDF_Valid[7:0] [15:8]
  580. * UDF_n_D11 [7:0]
  581. */
  582. reg = rule_index[0] << 24 | udf_upper_bits(num_udf) << 16 |
  583. udf_lower_bits(num_udf) << 8;
  584. core_writel(priv, reg, CORE_CFP_DATA_PORT(6));
  585. /* Mask all except chain ID, UDF Valid[8] and UDF Valid[7:0] */
  586. reg = XCESS_ADDR_MASK << 24 | udf_upper_bits(num_udf) << 16 |
  587. udf_lower_bits(num_udf) << 8;
  588. core_writel(priv, reg, CORE_CFP_MASK_PORT(6));
  589. /* Don't care */
  590. core_writel(priv, 0, CORE_CFP_DATA_PORT(5));
  591. /* Mask all */
  592. core_writel(priv, 0, CORE_CFP_MASK_PORT(5));
  593. bcm_sf2_cfp_slice_ipv6(priv, v6_spec->ip6dst, v6_spec->pdst, slice_num,
  594. false);
  595. bcm_sf2_cfp_slice_ipv6(priv, v6_m_spec->ip6dst, v6_m_spec->pdst,
  596. SLICE_NUM_MASK, true);
  597. /* Insert into TCAM now */
  598. bcm_sf2_cfp_rule_addr_set(priv, rule_index[1]);
  599. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
  600. if (ret) {
  601. pr_err("TCAM entry at addr %d failed\n", rule_index[1]);
  602. goto out_err;
  603. }
  604. /* Insert into Action and policer RAMs now, set chain ID to
  605. * the one we are chained to
  606. */
  607. ret = bcm_sf2_cfp_act_pol_set(priv, rule_index[1], port_num,
  608. queue_num, true);
  609. if (ret)
  610. goto out_err;
  611. /* Turn on CFP for this rule now */
  612. reg = core_readl(priv, CORE_CFP_CTL_REG);
  613. reg |= BIT(port);
  614. core_writel(priv, reg, CORE_CFP_CTL_REG);
  615. /* Flag the second half rule as being used now, return it as the
  616. * location, and flag it as unique while dumping rules
  617. */
  618. set_bit(rule_index[0], priv->cfp.used);
  619. set_bit(rule_index[1], priv->cfp.unique);
  620. fs->location = rule_index[1];
  621. return ret;
  622. out_err:
  623. clear_bit(rule_index[1], priv->cfp.used);
  624. return ret;
  625. }
  626. static int bcm_sf2_cfp_rule_set(struct dsa_switch *ds, int port,
  627. struct ethtool_rx_flow_spec *fs)
  628. {
  629. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  630. unsigned int queue_num, port_num;
  631. int ret = -EINVAL;
  632. /* Check for unsupported extensions */
  633. if ((fs->flow_type & FLOW_EXT) && (fs->m_ext.vlan_etype ||
  634. fs->m_ext.data[1]))
  635. return -EINVAL;
  636. if (fs->location != RX_CLS_LOC_ANY &&
  637. test_bit(fs->location, priv->cfp.used))
  638. return -EBUSY;
  639. if (fs->location != RX_CLS_LOC_ANY &&
  640. fs->location > bcm_sf2_cfp_rule_size(priv))
  641. return -EINVAL;
  642. /* We do not support discarding packets, check that the
  643. * destination port is enabled and that we are within the
  644. * number of ports supported by the switch
  645. */
  646. port_num = fs->ring_cookie / SF2_NUM_EGRESS_QUEUES;
  647. if (fs->ring_cookie == RX_CLS_FLOW_DISC ||
  648. !(dsa_is_user_port(ds, port_num) ||
  649. dsa_is_cpu_port(ds, port_num)) ||
  650. port_num >= priv->hw_params.num_ports)
  651. return -EINVAL;
  652. /*
  653. * We have a small oddity where Port 6 just does not have a
  654. * valid bit here (so we substract by one).
  655. */
  656. queue_num = fs->ring_cookie % SF2_NUM_EGRESS_QUEUES;
  657. if (port_num >= 7)
  658. port_num -= 1;
  659. switch (fs->flow_type & ~FLOW_EXT) {
  660. case TCP_V4_FLOW:
  661. case UDP_V4_FLOW:
  662. ret = bcm_sf2_cfp_ipv4_rule_set(priv, port, port_num,
  663. queue_num, fs);
  664. break;
  665. case TCP_V6_FLOW:
  666. case UDP_V6_FLOW:
  667. ret = bcm_sf2_cfp_ipv6_rule_set(priv, port, port_num,
  668. queue_num, fs);
  669. break;
  670. default:
  671. break;
  672. }
  673. return ret;
  674. }
  675. static int bcm_sf2_cfp_rule_del_one(struct bcm_sf2_priv *priv, int port,
  676. u32 loc, u32 *next_loc)
  677. {
  678. int ret;
  679. u32 reg;
  680. /* Indicate which rule we want to read */
  681. bcm_sf2_cfp_rule_addr_set(priv, loc);
  682. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
  683. if (ret)
  684. return ret;
  685. /* Check if this is possibly an IPv6 rule that would
  686. * indicate we need to delete its companion rule
  687. * as well
  688. */
  689. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  690. if (next_loc)
  691. *next_loc = (reg >> 24) & CHAIN_ID_MASK;
  692. /* Clear its valid bits */
  693. reg = core_readl(priv, CORE_CFP_DATA_PORT(0));
  694. reg &= ~SLICE_VALID;
  695. core_writel(priv, reg, CORE_CFP_DATA_PORT(0));
  696. /* Write back this entry into the TCAM now */
  697. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
  698. if (ret)
  699. return ret;
  700. clear_bit(loc, priv->cfp.used);
  701. clear_bit(loc, priv->cfp.unique);
  702. return 0;
  703. }
  704. static int bcm_sf2_cfp_rule_del(struct bcm_sf2_priv *priv, int port,
  705. u32 loc)
  706. {
  707. u32 next_loc = 0;
  708. int ret;
  709. /* Refuse deleting unused rules, and those that are not unique since
  710. * that could leave IPv6 rules with one of the chained rule in the
  711. * table.
  712. */
  713. if (!test_bit(loc, priv->cfp.unique) || loc == 0)
  714. return -EINVAL;
  715. ret = bcm_sf2_cfp_rule_del_one(priv, port, loc, &next_loc);
  716. if (ret)
  717. return ret;
  718. /* If this was an IPv6 rule, delete is companion rule too */
  719. if (next_loc)
  720. ret = bcm_sf2_cfp_rule_del_one(priv, port, next_loc, NULL);
  721. return ret;
  722. }
  723. static void bcm_sf2_invert_masks(struct ethtool_rx_flow_spec *flow)
  724. {
  725. unsigned int i;
  726. for (i = 0; i < sizeof(flow->m_u); i++)
  727. flow->m_u.hdata[i] ^= 0xff;
  728. flow->m_ext.vlan_etype ^= cpu_to_be16(~0);
  729. flow->m_ext.vlan_tci ^= cpu_to_be16(~0);
  730. flow->m_ext.data[0] ^= cpu_to_be32(~0);
  731. flow->m_ext.data[1] ^= cpu_to_be32(~0);
  732. }
  733. static int bcm_sf2_cfp_unslice_ipv4(struct bcm_sf2_priv *priv,
  734. struct ethtool_tcpip4_spec *v4_spec,
  735. bool mask)
  736. {
  737. u32 reg, offset, ipv4;
  738. u16 src_dst_port;
  739. if (mask)
  740. offset = CORE_CFP_MASK_PORT(3);
  741. else
  742. offset = CORE_CFP_DATA_PORT(3);
  743. reg = core_readl(priv, offset);
  744. /* src port [15:8] */
  745. src_dst_port = reg << 8;
  746. if (mask)
  747. offset = CORE_CFP_MASK_PORT(2);
  748. else
  749. offset = CORE_CFP_DATA_PORT(2);
  750. reg = core_readl(priv, offset);
  751. /* src port [7:0] */
  752. src_dst_port |= (reg >> 24);
  753. v4_spec->pdst = cpu_to_be16(src_dst_port);
  754. v4_spec->psrc = cpu_to_be16((u16)(reg >> 8));
  755. /* IPv4 dst [15:8] */
  756. ipv4 = (reg & 0xff) << 8;
  757. if (mask)
  758. offset = CORE_CFP_MASK_PORT(1);
  759. else
  760. offset = CORE_CFP_DATA_PORT(1);
  761. reg = core_readl(priv, offset);
  762. /* IPv4 dst [31:16] */
  763. ipv4 |= ((reg >> 8) & 0xffff) << 16;
  764. /* IPv4 dst [7:0] */
  765. ipv4 |= (reg >> 24) & 0xff;
  766. v4_spec->ip4dst = cpu_to_be32(ipv4);
  767. /* IPv4 src [15:8] */
  768. ipv4 = (reg & 0xff) << 8;
  769. if (mask)
  770. offset = CORE_CFP_MASK_PORT(0);
  771. else
  772. offset = CORE_CFP_DATA_PORT(0);
  773. reg = core_readl(priv, offset);
  774. /* Once the TCAM is programmed, the mask reflects the slice number
  775. * being matched, don't bother checking it when reading back the
  776. * mask spec
  777. */
  778. if (!mask && !(reg & SLICE_VALID))
  779. return -EINVAL;
  780. /* IPv4 src [7:0] */
  781. ipv4 |= (reg >> 24) & 0xff;
  782. /* IPv4 src [31:16] */
  783. ipv4 |= ((reg >> 8) & 0xffff) << 16;
  784. v4_spec->ip4src = cpu_to_be32(ipv4);
  785. return 0;
  786. }
  787. static int bcm_sf2_cfp_ipv4_rule_get(struct bcm_sf2_priv *priv, int port,
  788. struct ethtool_rx_flow_spec *fs)
  789. {
  790. struct ethtool_tcpip4_spec *v4_spec = NULL, *v4_m_spec = NULL;
  791. u32 reg;
  792. int ret;
  793. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  794. switch ((reg & IPPROTO_MASK) >> IPPROTO_SHIFT) {
  795. case IPPROTO_TCP:
  796. fs->flow_type = TCP_V4_FLOW;
  797. v4_spec = &fs->h_u.tcp_ip4_spec;
  798. v4_m_spec = &fs->m_u.tcp_ip4_spec;
  799. break;
  800. case IPPROTO_UDP:
  801. fs->flow_type = UDP_V4_FLOW;
  802. v4_spec = &fs->h_u.udp_ip4_spec;
  803. v4_m_spec = &fs->m_u.udp_ip4_spec;
  804. break;
  805. default:
  806. return -EINVAL;
  807. }
  808. fs->m_ext.data[0] = cpu_to_be32((reg >> IP_FRAG_SHIFT) & 1);
  809. v4_spec->tos = (reg >> IPTOS_SHIFT) & IPTOS_MASK;
  810. ret = bcm_sf2_cfp_unslice_ipv4(priv, v4_spec, false);
  811. if (ret)
  812. return ret;
  813. return bcm_sf2_cfp_unslice_ipv4(priv, v4_m_spec, true);
  814. }
  815. static int bcm_sf2_cfp_unslice_ipv6(struct bcm_sf2_priv *priv,
  816. __be32 *ip6_addr, __be16 *port,
  817. bool mask)
  818. {
  819. u32 reg, tmp, offset;
  820. /* C-Tag [31:24]
  821. * UDF_n_B8 [23:8] (port)
  822. * UDF_n_B7 (upper) [7:0] (addr[15:8])
  823. */
  824. if (mask)
  825. offset = CORE_CFP_MASK_PORT(4);
  826. else
  827. offset = CORE_CFP_DATA_PORT(4);
  828. reg = core_readl(priv, offset);
  829. *port = cpu_to_be32(reg) >> 8;
  830. tmp = (u32)(reg & 0xff) << 8;
  831. /* UDF_n_B7 (lower) [31:24] (addr[7:0])
  832. * UDF_n_B6 [23:8] (addr[31:16])
  833. * UDF_n_B5 (upper) [7:0] (addr[47:40])
  834. */
  835. if (mask)
  836. offset = CORE_CFP_MASK_PORT(3);
  837. else
  838. offset = CORE_CFP_DATA_PORT(3);
  839. reg = core_readl(priv, offset);
  840. tmp |= (reg >> 24) & 0xff;
  841. tmp |= (u32)((reg >> 8) << 16);
  842. ip6_addr[3] = cpu_to_be32(tmp);
  843. tmp = (u32)(reg & 0xff) << 8;
  844. /* UDF_n_B5 (lower) [31:24] (addr[39:32])
  845. * UDF_n_B4 [23:8] (addr[63:48])
  846. * UDF_n_B3 (upper) [7:0] (addr[79:72])
  847. */
  848. if (mask)
  849. offset = CORE_CFP_MASK_PORT(2);
  850. else
  851. offset = CORE_CFP_DATA_PORT(2);
  852. reg = core_readl(priv, offset);
  853. tmp |= (reg >> 24) & 0xff;
  854. tmp |= (u32)((reg >> 8) << 16);
  855. ip6_addr[2] = cpu_to_be32(tmp);
  856. tmp = (u32)(reg & 0xff) << 8;
  857. /* UDF_n_B3 (lower) [31:24] (addr[71:64])
  858. * UDF_n_B2 [23:8] (addr[95:80])
  859. * UDF_n_B1 (upper) [7:0] (addr[111:104])
  860. */
  861. if (mask)
  862. offset = CORE_CFP_MASK_PORT(1);
  863. else
  864. offset = CORE_CFP_DATA_PORT(1);
  865. reg = core_readl(priv, offset);
  866. tmp |= (reg >> 24) & 0xff;
  867. tmp |= (u32)((reg >> 8) << 16);
  868. ip6_addr[1] = cpu_to_be32(tmp);
  869. tmp = (u32)(reg & 0xff) << 8;
  870. /* UDF_n_B1 (lower) [31:24] (addr[103:96])
  871. * UDF_n_B0 [23:8] (addr[127:112])
  872. * Reserved [7:4]
  873. * Slice ID [3:2]
  874. * Slice valid [1:0]
  875. */
  876. if (mask)
  877. offset = CORE_CFP_MASK_PORT(0);
  878. else
  879. offset = CORE_CFP_DATA_PORT(0);
  880. reg = core_readl(priv, offset);
  881. tmp |= (reg >> 24) & 0xff;
  882. tmp |= (u32)((reg >> 8) << 16);
  883. ip6_addr[0] = cpu_to_be32(tmp);
  884. if (!mask && !(reg & SLICE_VALID))
  885. return -EINVAL;
  886. return 0;
  887. }
  888. static int bcm_sf2_cfp_ipv6_rule_get(struct bcm_sf2_priv *priv, int port,
  889. struct ethtool_rx_flow_spec *fs,
  890. u32 next_loc)
  891. {
  892. struct ethtool_tcpip6_spec *v6_spec = NULL, *v6_m_spec = NULL;
  893. u32 reg;
  894. int ret;
  895. /* UDPv6 and TCPv6 both use ethtool_tcpip6_spec so we are fine
  896. * assuming tcp_ip6_spec here being an union.
  897. */
  898. v6_spec = &fs->h_u.tcp_ip6_spec;
  899. v6_m_spec = &fs->m_u.tcp_ip6_spec;
  900. /* Read the second half first */
  901. ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_spec->ip6dst, &v6_spec->pdst,
  902. false);
  903. if (ret)
  904. return ret;
  905. ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_m_spec->ip6dst,
  906. &v6_m_spec->pdst, true);
  907. if (ret)
  908. return ret;
  909. /* Read last to avoid next entry clobbering the results during search
  910. * operations. We would not have the port enabled for this rule, so
  911. * don't bother checking it.
  912. */
  913. (void)core_readl(priv, CORE_CFP_DATA_PORT(7));
  914. /* The slice number is valid, so read the rule we are chained from now
  915. * which is our first half.
  916. */
  917. bcm_sf2_cfp_rule_addr_set(priv, next_loc);
  918. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
  919. if (ret)
  920. return ret;
  921. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  922. switch ((reg & IPPROTO_MASK) >> IPPROTO_SHIFT) {
  923. case IPPROTO_TCP:
  924. fs->flow_type = TCP_V6_FLOW;
  925. break;
  926. case IPPROTO_UDP:
  927. fs->flow_type = UDP_V6_FLOW;
  928. break;
  929. default:
  930. return -EINVAL;
  931. }
  932. ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_spec->ip6src, &v6_spec->psrc,
  933. false);
  934. if (ret)
  935. return ret;
  936. return bcm_sf2_cfp_unslice_ipv6(priv, v6_m_spec->ip6src,
  937. &v6_m_spec->psrc, true);
  938. }
  939. static int bcm_sf2_cfp_rule_get(struct bcm_sf2_priv *priv, int port,
  940. struct ethtool_rxnfc *nfc)
  941. {
  942. u32 reg, ipv4_or_chain_id;
  943. unsigned int queue_num;
  944. int ret;
  945. bcm_sf2_cfp_rule_addr_set(priv, nfc->fs.location);
  946. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | ACT_POL_RAM);
  947. if (ret)
  948. return ret;
  949. reg = core_readl(priv, CORE_ACT_POL_DATA0);
  950. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
  951. if (ret)
  952. return ret;
  953. /* Extract the destination port */
  954. nfc->fs.ring_cookie = fls((reg >> DST_MAP_IB_SHIFT) &
  955. DST_MAP_IB_MASK) - 1;
  956. /* There is no Port 6, so we compensate for that here */
  957. if (nfc->fs.ring_cookie >= 6)
  958. nfc->fs.ring_cookie++;
  959. nfc->fs.ring_cookie *= SF2_NUM_EGRESS_QUEUES;
  960. /* Extract the destination queue */
  961. queue_num = (reg >> NEW_TC_SHIFT) & NEW_TC_MASK;
  962. nfc->fs.ring_cookie += queue_num;
  963. /* Extract the L3_FRAMING or CHAIN_ID */
  964. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  965. /* With IPv6 rules this would contain a non-zero chain ID since
  966. * we reserve entry 0 and it cannot be used. So if we read 0 here
  967. * this means an IPv4 rule.
  968. */
  969. ipv4_or_chain_id = (reg >> L3_FRAMING_SHIFT) & 0xff;
  970. if (ipv4_or_chain_id == 0)
  971. ret = bcm_sf2_cfp_ipv4_rule_get(priv, port, &nfc->fs);
  972. else
  973. ret = bcm_sf2_cfp_ipv6_rule_get(priv, port, &nfc->fs,
  974. ipv4_or_chain_id);
  975. if (ret)
  976. return ret;
  977. /* Read last to avoid next entry clobbering the results during search
  978. * operations
  979. */
  980. reg = core_readl(priv, CORE_CFP_DATA_PORT(7));
  981. if (!(reg & 1 << port))
  982. return -EINVAL;
  983. bcm_sf2_invert_masks(&nfc->fs);
  984. /* Put the TCAM size here */
  985. nfc->data = bcm_sf2_cfp_rule_size(priv);
  986. return 0;
  987. }
  988. /* We implement the search doing a TCAM search operation */
  989. static int bcm_sf2_cfp_rule_get_all(struct bcm_sf2_priv *priv,
  990. int port, struct ethtool_rxnfc *nfc,
  991. u32 *rule_locs)
  992. {
  993. unsigned int index = 1, rules_cnt = 0;
  994. for_each_set_bit_from(index, priv->cfp.unique, priv->num_cfp_rules) {
  995. rule_locs[rules_cnt] = index;
  996. rules_cnt++;
  997. }
  998. /* Put the TCAM size here */
  999. nfc->data = bcm_sf2_cfp_rule_size(priv);
  1000. nfc->rule_cnt = rules_cnt;
  1001. return 0;
  1002. }
  1003. int bcm_sf2_get_rxnfc(struct dsa_switch *ds, int port,
  1004. struct ethtool_rxnfc *nfc, u32 *rule_locs)
  1005. {
  1006. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  1007. int ret = 0;
  1008. mutex_lock(&priv->cfp.lock);
  1009. switch (nfc->cmd) {
  1010. case ETHTOOL_GRXCLSRLCNT:
  1011. /* Subtract the default, unusable rule */
  1012. nfc->rule_cnt = bitmap_weight(priv->cfp.unique,
  1013. priv->num_cfp_rules) - 1;
  1014. /* We support specifying rule locations */
  1015. nfc->data |= RX_CLS_LOC_SPECIAL;
  1016. break;
  1017. case ETHTOOL_GRXCLSRULE:
  1018. ret = bcm_sf2_cfp_rule_get(priv, port, nfc);
  1019. break;
  1020. case ETHTOOL_GRXCLSRLALL:
  1021. ret = bcm_sf2_cfp_rule_get_all(priv, port, nfc, rule_locs);
  1022. break;
  1023. default:
  1024. ret = -EOPNOTSUPP;
  1025. break;
  1026. }
  1027. mutex_unlock(&priv->cfp.lock);
  1028. return ret;
  1029. }
  1030. int bcm_sf2_set_rxnfc(struct dsa_switch *ds, int port,
  1031. struct ethtool_rxnfc *nfc)
  1032. {
  1033. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  1034. int ret = 0;
  1035. mutex_lock(&priv->cfp.lock);
  1036. switch (nfc->cmd) {
  1037. case ETHTOOL_SRXCLSRLINS:
  1038. ret = bcm_sf2_cfp_rule_set(ds, port, &nfc->fs);
  1039. break;
  1040. case ETHTOOL_SRXCLSRLDEL:
  1041. ret = bcm_sf2_cfp_rule_del(priv, port, nfc->fs.location);
  1042. break;
  1043. default:
  1044. ret = -EOPNOTSUPP;
  1045. break;
  1046. }
  1047. mutex_unlock(&priv->cfp.lock);
  1048. return ret;
  1049. }
  1050. int bcm_sf2_cfp_rst(struct bcm_sf2_priv *priv)
  1051. {
  1052. unsigned int timeout = 1000;
  1053. u32 reg;
  1054. reg = core_readl(priv, CORE_CFP_ACC);
  1055. reg |= TCAM_RESET;
  1056. core_writel(priv, reg, CORE_CFP_ACC);
  1057. do {
  1058. reg = core_readl(priv, CORE_CFP_ACC);
  1059. if (!(reg & TCAM_RESET))
  1060. break;
  1061. cpu_relax();
  1062. } while (timeout--);
  1063. if (!timeout)
  1064. return -ETIMEDOUT;
  1065. return 0;
  1066. }