bcm_sf2_cfp.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. bcm_sf2_cfp_rule_size(priv));
  312. else
  313. rule_index = fs->location;
  314. layout = &udf_tcpip4_layout;
  315. /* We only use one UDF slice for now */
  316. slice_num = bcm_sf2_get_slice_number(layout, 0);
  317. if (slice_num == UDF_NUM_SLICES)
  318. return -EINVAL;
  319. num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
  320. /* Apply the UDF layout for this filter */
  321. bcm_sf2_cfp_udf_set(priv, layout, slice_num);
  322. /* Apply to all packets received through this port */
  323. core_writel(priv, BIT(port), CORE_CFP_DATA_PORT(7));
  324. /* Source port map match */
  325. core_writel(priv, 0xff, CORE_CFP_MASK_PORT(7));
  326. /* S-Tag status [31:30]
  327. * C-Tag status [29:28]
  328. * L2 framing [27:26]
  329. * L3 framing [25:24]
  330. * IP ToS [23:16]
  331. * IP proto [15:08]
  332. * IP Fragm [7]
  333. * Non 1st frag [6]
  334. * IP Authen [5]
  335. * TTL range [4:3]
  336. * PPPoE session [2]
  337. * Reserved [1]
  338. * UDF_Valid[8] [0]
  339. */
  340. core_writel(priv, v4_spec->tos << IPTOS_SHIFT |
  341. ip_proto << IPPROTO_SHIFT | ip_frag << IP_FRAG_SHIFT |
  342. udf_upper_bits(num_udf),
  343. CORE_CFP_DATA_PORT(6));
  344. /* Mask with the specific layout for IPv4 packets */
  345. core_writel(priv, layout->udfs[slice_num].mask_value |
  346. udf_upper_bits(num_udf), CORE_CFP_MASK_PORT(6));
  347. /* UDF_Valid[7:0] [31:24]
  348. * S-Tag [23:8]
  349. * C-Tag [7:0]
  350. */
  351. core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_DATA_PORT(5));
  352. /* Mask all but valid UDFs */
  353. core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_MASK_PORT(5));
  354. /* Program the match and the mask */
  355. bcm_sf2_cfp_slice_ipv4(priv, v4_spec, slice_num, false);
  356. bcm_sf2_cfp_slice_ipv4(priv, v4_m_spec, SLICE_NUM_MASK, true);
  357. /* Insert into TCAM now */
  358. bcm_sf2_cfp_rule_addr_set(priv, rule_index);
  359. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
  360. if (ret) {
  361. pr_err("TCAM entry at addr %d failed\n", rule_index);
  362. return ret;
  363. }
  364. /* Insert into Action and policer RAMs now */
  365. ret = bcm_sf2_cfp_act_pol_set(priv, rule_index, port_num,
  366. queue_num, true);
  367. if (ret)
  368. return ret;
  369. /* Turn on CFP for this rule now */
  370. reg = core_readl(priv, CORE_CFP_CTL_REG);
  371. reg |= BIT(port);
  372. core_writel(priv, reg, CORE_CFP_CTL_REG);
  373. /* Flag the rule as being used and return it */
  374. set_bit(rule_index, priv->cfp.used);
  375. set_bit(rule_index, priv->cfp.unique);
  376. fs->location = rule_index;
  377. return 0;
  378. }
  379. static void bcm_sf2_cfp_slice_ipv6(struct bcm_sf2_priv *priv,
  380. const __be32 *ip6_addr, const __be16 port,
  381. unsigned int slice_num,
  382. bool mask)
  383. {
  384. u32 reg, tmp, val, offset;
  385. /* C-Tag [31:24]
  386. * UDF_n_B8 [23:8] (port)
  387. * UDF_n_B7 (upper) [7:0] (addr[15:8])
  388. */
  389. reg = be32_to_cpu(ip6_addr[3]);
  390. val = (u32)be16_to_cpu(port) << 8 | ((reg >> 8) & 0xff);
  391. if (mask)
  392. offset = CORE_CFP_MASK_PORT(4);
  393. else
  394. offset = CORE_CFP_DATA_PORT(4);
  395. core_writel(priv, val, offset);
  396. /* UDF_n_B7 (lower) [31:24] (addr[7:0])
  397. * UDF_n_B6 [23:8] (addr[31:16])
  398. * UDF_n_B5 (upper) [7:0] (addr[47:40])
  399. */
  400. tmp = be32_to_cpu(ip6_addr[2]);
  401. val = (u32)(reg & 0xff) << 24 | (u32)(reg >> 16) << 8 |
  402. ((tmp >> 8) & 0xff);
  403. if (mask)
  404. offset = CORE_CFP_MASK_PORT(3);
  405. else
  406. offset = CORE_CFP_DATA_PORT(3);
  407. core_writel(priv, val, offset);
  408. /* UDF_n_B5 (lower) [31:24] (addr[39:32])
  409. * UDF_n_B4 [23:8] (addr[63:48])
  410. * UDF_n_B3 (upper) [7:0] (addr[79:72])
  411. */
  412. reg = be32_to_cpu(ip6_addr[1]);
  413. val = (u32)(tmp & 0xff) << 24 | (u32)(tmp >> 16) << 8 |
  414. ((reg >> 8) & 0xff);
  415. if (mask)
  416. offset = CORE_CFP_MASK_PORT(2);
  417. else
  418. offset = CORE_CFP_DATA_PORT(2);
  419. core_writel(priv, val, offset);
  420. /* UDF_n_B3 (lower) [31:24] (addr[71:64])
  421. * UDF_n_B2 [23:8] (addr[95:80])
  422. * UDF_n_B1 (upper) [7:0] (addr[111:104])
  423. */
  424. tmp = be32_to_cpu(ip6_addr[0]);
  425. val = (u32)(reg & 0xff) << 24 | (u32)(reg >> 16) << 8 |
  426. ((tmp >> 8) & 0xff);
  427. if (mask)
  428. offset = CORE_CFP_MASK_PORT(1);
  429. else
  430. offset = CORE_CFP_DATA_PORT(1);
  431. core_writel(priv, val, offset);
  432. /* UDF_n_B1 (lower) [31:24] (addr[103:96])
  433. * UDF_n_B0 [23:8] (addr[127:112])
  434. * Reserved [7:4]
  435. * Slice ID [3:2]
  436. * Slice valid [1:0]
  437. */
  438. reg = (u32)(tmp & 0xff) << 24 | (u32)(tmp >> 16) << 8 |
  439. SLICE_NUM(slice_num) | SLICE_VALID;
  440. if (mask)
  441. offset = CORE_CFP_MASK_PORT(0);
  442. else
  443. offset = CORE_CFP_DATA_PORT(0);
  444. core_writel(priv, reg, offset);
  445. }
  446. static int bcm_sf2_cfp_ipv6_rule_set(struct bcm_sf2_priv *priv, int port,
  447. unsigned int port_num,
  448. unsigned int queue_num,
  449. struct ethtool_rx_flow_spec *fs)
  450. {
  451. struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
  452. unsigned int slice_num, rule_index[2];
  453. const struct cfp_udf_layout *layout;
  454. u8 ip_proto, ip_frag;
  455. int ret = 0;
  456. u8 num_udf;
  457. u32 reg;
  458. switch (fs->flow_type & ~FLOW_EXT) {
  459. case TCP_V6_FLOW:
  460. ip_proto = IPPROTO_TCP;
  461. v6_spec = &fs->h_u.tcp_ip6_spec;
  462. v6_m_spec = &fs->m_u.tcp_ip6_spec;
  463. break;
  464. case UDP_V6_FLOW:
  465. ip_proto = IPPROTO_UDP;
  466. v6_spec = &fs->h_u.udp_ip6_spec;
  467. v6_m_spec = &fs->m_u.udp_ip6_spec;
  468. break;
  469. default:
  470. return -EINVAL;
  471. }
  472. ip_frag = be32_to_cpu(fs->m_ext.data[0]);
  473. layout = &udf_tcpip6_layout;
  474. slice_num = bcm_sf2_get_slice_number(layout, 0);
  475. if (slice_num == UDF_NUM_SLICES)
  476. return -EINVAL;
  477. num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
  478. /* Negotiate two indexes, one for the second half which we are chained
  479. * from, which is what we will return to user-space, and a second one
  480. * which is used to store its first half. That first half does not
  481. * allow any choice of placement, so it just needs to find the next
  482. * available bit. We return the second half as fs->location because
  483. * that helps with the rule lookup later on since the second half is
  484. * chained from its first half, we can easily identify IPv6 CFP rules
  485. * by looking whether they carry a CHAIN_ID.
  486. *
  487. * We also want the second half to have a lower rule_index than its
  488. * first half because the HW search is by incrementing addresses.
  489. */
  490. if (fs->location == RX_CLS_LOC_ANY)
  491. rule_index[0] = find_first_zero_bit(priv->cfp.used,
  492. bcm_sf2_cfp_rule_size(priv));
  493. else
  494. rule_index[0] = fs->location;
  495. /* Flag it as used (cleared on error path) such that we can immediately
  496. * obtain a second one to chain from.
  497. */
  498. set_bit(rule_index[0], priv->cfp.used);
  499. rule_index[1] = find_first_zero_bit(priv->cfp.used,
  500. bcm_sf2_cfp_rule_size(priv));
  501. if (rule_index[1] > bcm_sf2_cfp_rule_size(priv)) {
  502. ret = -ENOSPC;
  503. goto out_err;
  504. }
  505. /* Apply the UDF layout for this filter */
  506. bcm_sf2_cfp_udf_set(priv, layout, slice_num);
  507. /* Apply to all packets received through this port */
  508. core_writel(priv, BIT(port), CORE_CFP_DATA_PORT(7));
  509. /* Source port map match */
  510. core_writel(priv, 0xff, CORE_CFP_MASK_PORT(7));
  511. /* S-Tag status [31:30]
  512. * C-Tag status [29:28]
  513. * L2 framing [27:26]
  514. * L3 framing [25:24]
  515. * IP ToS [23:16]
  516. * IP proto [15:08]
  517. * IP Fragm [7]
  518. * Non 1st frag [6]
  519. * IP Authen [5]
  520. * TTL range [4:3]
  521. * PPPoE session [2]
  522. * Reserved [1]
  523. * UDF_Valid[8] [0]
  524. */
  525. reg = 1 << L3_FRAMING_SHIFT | ip_proto << IPPROTO_SHIFT |
  526. ip_frag << IP_FRAG_SHIFT | udf_upper_bits(num_udf);
  527. core_writel(priv, reg, CORE_CFP_DATA_PORT(6));
  528. /* Mask with the specific layout for IPv6 packets including
  529. * UDF_Valid[8]
  530. */
  531. reg = layout->udfs[slice_num].mask_value | udf_upper_bits(num_udf);
  532. core_writel(priv, reg, CORE_CFP_MASK_PORT(6));
  533. /* UDF_Valid[7:0] [31:24]
  534. * S-Tag [23:8]
  535. * C-Tag [7:0]
  536. */
  537. core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_DATA_PORT(5));
  538. /* Mask all but valid UDFs */
  539. core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_MASK_PORT(5));
  540. /* Slice the IPv6 source address and port */
  541. bcm_sf2_cfp_slice_ipv6(priv, v6_spec->ip6src, v6_spec->psrc,
  542. slice_num, false);
  543. bcm_sf2_cfp_slice_ipv6(priv, v6_m_spec->ip6src, v6_m_spec->psrc,
  544. slice_num, true);
  545. /* Insert into TCAM now because we need to insert a second rule */
  546. bcm_sf2_cfp_rule_addr_set(priv, rule_index[0]);
  547. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
  548. if (ret) {
  549. pr_err("TCAM entry at addr %d failed\n", rule_index[0]);
  550. goto out_err;
  551. }
  552. /* Insert into Action and policer RAMs now */
  553. ret = bcm_sf2_cfp_act_pol_set(priv, rule_index[0], port_num,
  554. queue_num, false);
  555. if (ret)
  556. goto out_err;
  557. /* Now deal with the second slice to chain this rule */
  558. slice_num = bcm_sf2_get_slice_number(layout, slice_num + 1);
  559. if (slice_num == UDF_NUM_SLICES) {
  560. ret = -EINVAL;
  561. goto out_err;
  562. }
  563. num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
  564. /* Apply the UDF layout for this filter */
  565. bcm_sf2_cfp_udf_set(priv, layout, slice_num);
  566. /* Chained rule, source port match is coming from the rule we are
  567. * chained from.
  568. */
  569. core_writel(priv, 0, CORE_CFP_DATA_PORT(7));
  570. core_writel(priv, 0, CORE_CFP_MASK_PORT(7));
  571. /*
  572. * CHAIN ID [31:24] chain to previous slice
  573. * Reserved [23:20]
  574. * UDF_Valid[11:8] [19:16]
  575. * UDF_Valid[7:0] [15:8]
  576. * UDF_n_D11 [7:0]
  577. */
  578. reg = rule_index[0] << 24 | udf_upper_bits(num_udf) << 16 |
  579. udf_lower_bits(num_udf) << 8;
  580. core_writel(priv, reg, CORE_CFP_DATA_PORT(6));
  581. /* Mask all except chain ID, UDF Valid[8] and UDF Valid[7:0] */
  582. reg = XCESS_ADDR_MASK << 24 | udf_upper_bits(num_udf) << 16 |
  583. udf_lower_bits(num_udf) << 8;
  584. core_writel(priv, reg, CORE_CFP_MASK_PORT(6));
  585. /* Don't care */
  586. core_writel(priv, 0, CORE_CFP_DATA_PORT(5));
  587. /* Mask all */
  588. core_writel(priv, 0, CORE_CFP_MASK_PORT(5));
  589. bcm_sf2_cfp_slice_ipv6(priv, v6_spec->ip6dst, v6_spec->pdst, slice_num,
  590. false);
  591. bcm_sf2_cfp_slice_ipv6(priv, v6_m_spec->ip6dst, v6_m_spec->pdst,
  592. SLICE_NUM_MASK, true);
  593. /* Insert into TCAM now */
  594. bcm_sf2_cfp_rule_addr_set(priv, rule_index[1]);
  595. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
  596. if (ret) {
  597. pr_err("TCAM entry at addr %d failed\n", rule_index[1]);
  598. goto out_err;
  599. }
  600. /* Insert into Action and policer RAMs now, set chain ID to
  601. * the one we are chained to
  602. */
  603. ret = bcm_sf2_cfp_act_pol_set(priv, rule_index[0], port_num,
  604. queue_num, true);
  605. if (ret)
  606. goto out_err;
  607. /* Turn on CFP for this rule now */
  608. reg = core_readl(priv, CORE_CFP_CTL_REG);
  609. reg |= BIT(port);
  610. core_writel(priv, reg, CORE_CFP_CTL_REG);
  611. /* Flag the second half rule as being used now, return it as the
  612. * location, and flag it as unique while dumping rules
  613. */
  614. set_bit(rule_index[1], priv->cfp.used);
  615. set_bit(rule_index[1], priv->cfp.unique);
  616. fs->location = rule_index[1];
  617. return ret;
  618. out_err:
  619. clear_bit(rule_index[0], priv->cfp.used);
  620. return ret;
  621. }
  622. static int bcm_sf2_cfp_rule_set(struct dsa_switch *ds, int port,
  623. struct ethtool_rx_flow_spec *fs)
  624. {
  625. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  626. unsigned int queue_num, port_num;
  627. int ret = -EINVAL;
  628. /* Check for unsupported extensions */
  629. if ((fs->flow_type & FLOW_EXT) && (fs->m_ext.vlan_etype ||
  630. fs->m_ext.data[1]))
  631. return -EINVAL;
  632. if (fs->location != RX_CLS_LOC_ANY &&
  633. test_bit(fs->location, priv->cfp.used))
  634. return -EBUSY;
  635. if (fs->location != RX_CLS_LOC_ANY &&
  636. fs->location > bcm_sf2_cfp_rule_size(priv))
  637. return -EINVAL;
  638. /* We do not support discarding packets, check that the
  639. * destination port is enabled and that we are within the
  640. * number of ports supported by the switch
  641. */
  642. port_num = fs->ring_cookie / SF2_NUM_EGRESS_QUEUES;
  643. if (fs->ring_cookie == RX_CLS_FLOW_DISC ||
  644. !dsa_is_user_port(ds, port_num) ||
  645. port_num >= priv->hw_params.num_ports)
  646. return -EINVAL;
  647. /*
  648. * We have a small oddity where Port 6 just does not have a
  649. * valid bit here (so we substract by one).
  650. */
  651. queue_num = fs->ring_cookie % SF2_NUM_EGRESS_QUEUES;
  652. if (port_num >= 7)
  653. port_num -= 1;
  654. switch (fs->flow_type & ~FLOW_EXT) {
  655. case TCP_V4_FLOW:
  656. case UDP_V4_FLOW:
  657. ret = bcm_sf2_cfp_ipv4_rule_set(priv, port, port_num,
  658. queue_num, fs);
  659. break;
  660. case TCP_V6_FLOW:
  661. case UDP_V6_FLOW:
  662. ret = bcm_sf2_cfp_ipv6_rule_set(priv, port, port_num,
  663. queue_num, fs);
  664. break;
  665. default:
  666. break;
  667. }
  668. return ret;
  669. }
  670. static int bcm_sf2_cfp_rule_del_one(struct bcm_sf2_priv *priv, int port,
  671. u32 loc, u32 *next_loc)
  672. {
  673. int ret;
  674. u32 reg;
  675. /* Refuse deletion of unused rules, and the default reserved rule */
  676. if (!test_bit(loc, priv->cfp.used) || loc == 0)
  677. return -EINVAL;
  678. /* Indicate which rule we want to read */
  679. bcm_sf2_cfp_rule_addr_set(priv, loc);
  680. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
  681. if (ret)
  682. return ret;
  683. /* Check if this is possibly an IPv6 rule that would
  684. * indicate we need to delete its companion rule
  685. * as well
  686. */
  687. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  688. if (next_loc)
  689. *next_loc = (reg >> 24) & CHAIN_ID_MASK;
  690. /* Clear its valid bits */
  691. reg = core_readl(priv, CORE_CFP_DATA_PORT(0));
  692. reg &= ~SLICE_VALID;
  693. core_writel(priv, reg, CORE_CFP_DATA_PORT(0));
  694. /* Write back this entry into the TCAM now */
  695. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
  696. if (ret)
  697. return ret;
  698. clear_bit(loc, priv->cfp.used);
  699. clear_bit(loc, priv->cfp.unique);
  700. return 0;
  701. }
  702. static int bcm_sf2_cfp_rule_del(struct bcm_sf2_priv *priv, int port,
  703. u32 loc)
  704. {
  705. u32 next_loc = 0;
  706. int ret;
  707. ret = bcm_sf2_cfp_rule_del_one(priv, port, loc, &next_loc);
  708. if (ret)
  709. return ret;
  710. /* If this was an IPv6 rule, delete is companion rule too */
  711. if (next_loc)
  712. ret = bcm_sf2_cfp_rule_del_one(priv, port, next_loc, NULL);
  713. return ret;
  714. }
  715. static void bcm_sf2_invert_masks(struct ethtool_rx_flow_spec *flow)
  716. {
  717. unsigned int i;
  718. for (i = 0; i < sizeof(flow->m_u); i++)
  719. flow->m_u.hdata[i] ^= 0xff;
  720. flow->m_ext.vlan_etype ^= cpu_to_be16(~0);
  721. flow->m_ext.vlan_tci ^= cpu_to_be16(~0);
  722. flow->m_ext.data[0] ^= cpu_to_be32(~0);
  723. flow->m_ext.data[1] ^= cpu_to_be32(~0);
  724. }
  725. static int bcm_sf2_cfp_unslice_ipv4(struct bcm_sf2_priv *priv,
  726. struct ethtool_tcpip4_spec *v4_spec,
  727. bool mask)
  728. {
  729. u32 reg, offset, ipv4;
  730. u16 src_dst_port;
  731. if (mask)
  732. offset = CORE_CFP_MASK_PORT(3);
  733. else
  734. offset = CORE_CFP_DATA_PORT(3);
  735. reg = core_readl(priv, offset);
  736. /* src port [15:8] */
  737. src_dst_port = reg << 8;
  738. if (mask)
  739. offset = CORE_CFP_MASK_PORT(2);
  740. else
  741. offset = CORE_CFP_DATA_PORT(2);
  742. reg = core_readl(priv, offset);
  743. /* src port [7:0] */
  744. src_dst_port |= (reg >> 24);
  745. v4_spec->pdst = cpu_to_be16(src_dst_port);
  746. v4_spec->psrc = cpu_to_be16((u16)(reg >> 8));
  747. /* IPv4 dst [15:8] */
  748. ipv4 = (reg & 0xff) << 8;
  749. if (mask)
  750. offset = CORE_CFP_MASK_PORT(1);
  751. else
  752. offset = CORE_CFP_DATA_PORT(1);
  753. reg = core_readl(priv, offset);
  754. /* IPv4 dst [31:16] */
  755. ipv4 |= ((reg >> 8) & 0xffff) << 16;
  756. /* IPv4 dst [7:0] */
  757. ipv4 |= (reg >> 24) & 0xff;
  758. v4_spec->ip4dst = cpu_to_be32(ipv4);
  759. /* IPv4 src [15:8] */
  760. ipv4 = (reg & 0xff) << 8;
  761. if (mask)
  762. offset = CORE_CFP_MASK_PORT(0);
  763. else
  764. offset = CORE_CFP_DATA_PORT(0);
  765. reg = core_readl(priv, offset);
  766. /* Once the TCAM is programmed, the mask reflects the slice number
  767. * being matched, don't bother checking it when reading back the
  768. * mask spec
  769. */
  770. if (!mask && !(reg & SLICE_VALID))
  771. return -EINVAL;
  772. /* IPv4 src [7:0] */
  773. ipv4 |= (reg >> 24) & 0xff;
  774. /* IPv4 src [31:16] */
  775. ipv4 |= ((reg >> 8) & 0xffff) << 16;
  776. v4_spec->ip4src = cpu_to_be32(ipv4);
  777. return 0;
  778. }
  779. static int bcm_sf2_cfp_ipv4_rule_get(struct bcm_sf2_priv *priv, int port,
  780. struct ethtool_rx_flow_spec *fs)
  781. {
  782. struct ethtool_tcpip4_spec *v4_spec = NULL, *v4_m_spec = NULL;
  783. u32 reg;
  784. int ret;
  785. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  786. switch ((reg & IPPROTO_MASK) >> IPPROTO_SHIFT) {
  787. case IPPROTO_TCP:
  788. fs->flow_type = TCP_V4_FLOW;
  789. v4_spec = &fs->h_u.tcp_ip4_spec;
  790. v4_m_spec = &fs->m_u.tcp_ip4_spec;
  791. break;
  792. case IPPROTO_UDP:
  793. fs->flow_type = UDP_V4_FLOW;
  794. v4_spec = &fs->h_u.udp_ip4_spec;
  795. v4_m_spec = &fs->m_u.udp_ip4_spec;
  796. break;
  797. default:
  798. return -EINVAL;
  799. }
  800. fs->m_ext.data[0] = cpu_to_be32((reg >> IP_FRAG_SHIFT) & 1);
  801. v4_spec->tos = (reg >> IPTOS_SHIFT) & IPTOS_MASK;
  802. ret = bcm_sf2_cfp_unslice_ipv4(priv, v4_spec, false);
  803. if (ret)
  804. return ret;
  805. return bcm_sf2_cfp_unslice_ipv4(priv, v4_m_spec, true);
  806. }
  807. static int bcm_sf2_cfp_unslice_ipv6(struct bcm_sf2_priv *priv,
  808. __be32 *ip6_addr, __be16 *port,
  809. bool mask)
  810. {
  811. u32 reg, tmp, offset;
  812. /* C-Tag [31:24]
  813. * UDF_n_B8 [23:8] (port)
  814. * UDF_n_B7 (upper) [7:0] (addr[15:8])
  815. */
  816. if (mask)
  817. offset = CORE_CFP_MASK_PORT(4);
  818. else
  819. offset = CORE_CFP_DATA_PORT(4);
  820. reg = core_readl(priv, offset);
  821. *port = cpu_to_be32(reg) >> 8;
  822. tmp = (u32)(reg & 0xff) << 8;
  823. /* UDF_n_B7 (lower) [31:24] (addr[7:0])
  824. * UDF_n_B6 [23:8] (addr[31:16])
  825. * UDF_n_B5 (upper) [7:0] (addr[47:40])
  826. */
  827. if (mask)
  828. offset = CORE_CFP_MASK_PORT(3);
  829. else
  830. offset = CORE_CFP_DATA_PORT(3);
  831. reg = core_readl(priv, offset);
  832. tmp |= (reg >> 24) & 0xff;
  833. tmp |= (u32)((reg >> 8) << 16);
  834. ip6_addr[3] = cpu_to_be32(tmp);
  835. tmp = (u32)(reg & 0xff) << 8;
  836. /* UDF_n_B5 (lower) [31:24] (addr[39:32])
  837. * UDF_n_B4 [23:8] (addr[63:48])
  838. * UDF_n_B3 (upper) [7:0] (addr[79:72])
  839. */
  840. if (mask)
  841. offset = CORE_CFP_MASK_PORT(2);
  842. else
  843. offset = CORE_CFP_DATA_PORT(2);
  844. reg = core_readl(priv, offset);
  845. tmp |= (reg >> 24) & 0xff;
  846. tmp |= (u32)((reg >> 8) << 16);
  847. ip6_addr[2] = cpu_to_be32(tmp);
  848. tmp = (u32)(reg & 0xff) << 8;
  849. /* UDF_n_B3 (lower) [31:24] (addr[71:64])
  850. * UDF_n_B2 [23:8] (addr[95:80])
  851. * UDF_n_B1 (upper) [7:0] (addr[111:104])
  852. */
  853. if (mask)
  854. offset = CORE_CFP_MASK_PORT(1);
  855. else
  856. offset = CORE_CFP_DATA_PORT(1);
  857. reg = core_readl(priv, offset);
  858. tmp |= (reg >> 24) & 0xff;
  859. tmp |= (u32)((reg >> 8) << 16);
  860. ip6_addr[1] = cpu_to_be32(tmp);
  861. tmp = (u32)(reg & 0xff) << 8;
  862. /* UDF_n_B1 (lower) [31:24] (addr[103:96])
  863. * UDF_n_B0 [23:8] (addr[127:112])
  864. * Reserved [7:4]
  865. * Slice ID [3:2]
  866. * Slice valid [1:0]
  867. */
  868. if (mask)
  869. offset = CORE_CFP_MASK_PORT(0);
  870. else
  871. offset = CORE_CFP_DATA_PORT(0);
  872. reg = core_readl(priv, offset);
  873. tmp |= (reg >> 24) & 0xff;
  874. tmp |= (u32)((reg >> 8) << 16);
  875. ip6_addr[0] = cpu_to_be32(tmp);
  876. if (!mask && !(reg & SLICE_VALID))
  877. return -EINVAL;
  878. return 0;
  879. }
  880. static int bcm_sf2_cfp_ipv6_rule_get(struct bcm_sf2_priv *priv, int port,
  881. struct ethtool_rx_flow_spec *fs,
  882. u32 next_loc)
  883. {
  884. struct ethtool_tcpip6_spec *v6_spec = NULL, *v6_m_spec = NULL;
  885. u32 reg;
  886. int ret;
  887. /* UDPv6 and TCPv6 both use ethtool_tcpip6_spec so we are fine
  888. * assuming tcp_ip6_spec here being an union.
  889. */
  890. v6_spec = &fs->h_u.tcp_ip6_spec;
  891. v6_m_spec = &fs->m_u.tcp_ip6_spec;
  892. /* Read the second half first */
  893. ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_spec->ip6dst, &v6_spec->pdst,
  894. false);
  895. if (ret)
  896. return ret;
  897. ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_m_spec->ip6dst,
  898. &v6_m_spec->pdst, true);
  899. if (ret)
  900. return ret;
  901. /* Read last to avoid next entry clobbering the results during search
  902. * operations. We would not have the port enabled for this rule, so
  903. * don't bother checking it.
  904. */
  905. (void)core_readl(priv, CORE_CFP_DATA_PORT(7));
  906. /* The slice number is valid, so read the rule we are chained from now
  907. * which is our first half.
  908. */
  909. bcm_sf2_cfp_rule_addr_set(priv, next_loc);
  910. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
  911. if (ret)
  912. return ret;
  913. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  914. switch ((reg & IPPROTO_MASK) >> IPPROTO_SHIFT) {
  915. case IPPROTO_TCP:
  916. fs->flow_type = TCP_V6_FLOW;
  917. break;
  918. case IPPROTO_UDP:
  919. fs->flow_type = UDP_V6_FLOW;
  920. break;
  921. default:
  922. return -EINVAL;
  923. }
  924. ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_spec->ip6src, &v6_spec->psrc,
  925. false);
  926. if (ret)
  927. return ret;
  928. return bcm_sf2_cfp_unslice_ipv6(priv, v6_m_spec->ip6src,
  929. &v6_m_spec->psrc, true);
  930. }
  931. static int bcm_sf2_cfp_rule_get(struct bcm_sf2_priv *priv, int port,
  932. struct ethtool_rxnfc *nfc)
  933. {
  934. u32 reg, ipv4_or_chain_id;
  935. unsigned int queue_num;
  936. int ret;
  937. bcm_sf2_cfp_rule_addr_set(priv, nfc->fs.location);
  938. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | ACT_POL_RAM);
  939. if (ret)
  940. return ret;
  941. reg = core_readl(priv, CORE_ACT_POL_DATA0);
  942. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
  943. if (ret)
  944. return ret;
  945. /* Extract the destination port */
  946. nfc->fs.ring_cookie = fls((reg >> DST_MAP_IB_SHIFT) &
  947. DST_MAP_IB_MASK) - 1;
  948. /* There is no Port 6, so we compensate for that here */
  949. if (nfc->fs.ring_cookie >= 6)
  950. nfc->fs.ring_cookie++;
  951. nfc->fs.ring_cookie *= SF2_NUM_EGRESS_QUEUES;
  952. /* Extract the destination queue */
  953. queue_num = (reg >> NEW_TC_SHIFT) & NEW_TC_MASK;
  954. nfc->fs.ring_cookie += queue_num;
  955. /* Extract the L3_FRAMING or CHAIN_ID */
  956. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  957. /* With IPv6 rules this would contain a non-zero chain ID since
  958. * we reserve entry 0 and it cannot be used. So if we read 0 here
  959. * this means an IPv4 rule.
  960. */
  961. ipv4_or_chain_id = (reg >> L3_FRAMING_SHIFT) & 0xff;
  962. if (ipv4_or_chain_id == 0)
  963. ret = bcm_sf2_cfp_ipv4_rule_get(priv, port, &nfc->fs);
  964. else
  965. ret = bcm_sf2_cfp_ipv6_rule_get(priv, port, &nfc->fs,
  966. ipv4_or_chain_id);
  967. if (ret)
  968. return ret;
  969. /* Read last to avoid next entry clobbering the results during search
  970. * operations
  971. */
  972. reg = core_readl(priv, CORE_CFP_DATA_PORT(7));
  973. if (!(reg & 1 << port))
  974. return -EINVAL;
  975. bcm_sf2_invert_masks(&nfc->fs);
  976. /* Put the TCAM size here */
  977. nfc->data = bcm_sf2_cfp_rule_size(priv);
  978. return 0;
  979. }
  980. /* We implement the search doing a TCAM search operation */
  981. static int bcm_sf2_cfp_rule_get_all(struct bcm_sf2_priv *priv,
  982. int port, struct ethtool_rxnfc *nfc,
  983. u32 *rule_locs)
  984. {
  985. unsigned int index = 1, rules_cnt = 0;
  986. for_each_set_bit_from(index, priv->cfp.unique, priv->num_cfp_rules) {
  987. rule_locs[rules_cnt] = index;
  988. rules_cnt++;
  989. }
  990. /* Put the TCAM size here */
  991. nfc->data = bcm_sf2_cfp_rule_size(priv);
  992. nfc->rule_cnt = rules_cnt;
  993. return 0;
  994. }
  995. int bcm_sf2_get_rxnfc(struct dsa_switch *ds, int port,
  996. struct ethtool_rxnfc *nfc, u32 *rule_locs)
  997. {
  998. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  999. int ret = 0;
  1000. mutex_lock(&priv->cfp.lock);
  1001. switch (nfc->cmd) {
  1002. case ETHTOOL_GRXCLSRLCNT:
  1003. /* Subtract the default, unusable rule */
  1004. nfc->rule_cnt = bitmap_weight(priv->cfp.unique,
  1005. priv->num_cfp_rules) - 1;
  1006. /* We support specifying rule locations */
  1007. nfc->data |= RX_CLS_LOC_SPECIAL;
  1008. break;
  1009. case ETHTOOL_GRXCLSRULE:
  1010. ret = bcm_sf2_cfp_rule_get(priv, port, nfc);
  1011. break;
  1012. case ETHTOOL_GRXCLSRLALL:
  1013. ret = bcm_sf2_cfp_rule_get_all(priv, port, nfc, rule_locs);
  1014. break;
  1015. default:
  1016. ret = -EOPNOTSUPP;
  1017. break;
  1018. }
  1019. mutex_unlock(&priv->cfp.lock);
  1020. return ret;
  1021. }
  1022. int bcm_sf2_set_rxnfc(struct dsa_switch *ds, int port,
  1023. struct ethtool_rxnfc *nfc)
  1024. {
  1025. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  1026. int ret = 0;
  1027. mutex_lock(&priv->cfp.lock);
  1028. switch (nfc->cmd) {
  1029. case ETHTOOL_SRXCLSRLINS:
  1030. ret = bcm_sf2_cfp_rule_set(ds, port, &nfc->fs);
  1031. break;
  1032. case ETHTOOL_SRXCLSRLDEL:
  1033. ret = bcm_sf2_cfp_rule_del(priv, port, nfc->fs.location);
  1034. break;
  1035. default:
  1036. ret = -EOPNOTSUPP;
  1037. break;
  1038. }
  1039. mutex_unlock(&priv->cfp.lock);
  1040. return ret;
  1041. }
  1042. int bcm_sf2_cfp_rst(struct bcm_sf2_priv *priv)
  1043. {
  1044. unsigned int timeout = 1000;
  1045. u32 reg;
  1046. reg = core_readl(priv, CORE_CFP_ACC);
  1047. reg |= TCAM_RESET;
  1048. core_writel(priv, reg, CORE_CFP_ACC);
  1049. do {
  1050. reg = core_readl(priv, CORE_CFP_ACC);
  1051. if (!(reg & TCAM_RESET))
  1052. break;
  1053. cpu_relax();
  1054. } while (timeout--);
  1055. if (!timeout)
  1056. return -ETIMEDOUT;
  1057. return 0;
  1058. }