bcm_sf2_cfp.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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. port_num >= priv->hw_params.num_ports)
  650. return -EINVAL;
  651. /*
  652. * We have a small oddity where Port 6 just does not have a
  653. * valid bit here (so we substract by one).
  654. */
  655. queue_num = fs->ring_cookie % SF2_NUM_EGRESS_QUEUES;
  656. if (port_num >= 7)
  657. port_num -= 1;
  658. switch (fs->flow_type & ~FLOW_EXT) {
  659. case TCP_V4_FLOW:
  660. case UDP_V4_FLOW:
  661. ret = bcm_sf2_cfp_ipv4_rule_set(priv, port, port_num,
  662. queue_num, fs);
  663. break;
  664. case TCP_V6_FLOW:
  665. case UDP_V6_FLOW:
  666. ret = bcm_sf2_cfp_ipv6_rule_set(priv, port, port_num,
  667. queue_num, fs);
  668. break;
  669. default:
  670. break;
  671. }
  672. return ret;
  673. }
  674. static int bcm_sf2_cfp_rule_del_one(struct bcm_sf2_priv *priv, int port,
  675. u32 loc, u32 *next_loc)
  676. {
  677. int ret;
  678. u32 reg;
  679. /* Indicate which rule we want to read */
  680. bcm_sf2_cfp_rule_addr_set(priv, loc);
  681. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
  682. if (ret)
  683. return ret;
  684. /* Check if this is possibly an IPv6 rule that would
  685. * indicate we need to delete its companion rule
  686. * as well
  687. */
  688. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  689. if (next_loc)
  690. *next_loc = (reg >> 24) & CHAIN_ID_MASK;
  691. /* Clear its valid bits */
  692. reg = core_readl(priv, CORE_CFP_DATA_PORT(0));
  693. reg &= ~SLICE_VALID;
  694. core_writel(priv, reg, CORE_CFP_DATA_PORT(0));
  695. /* Write back this entry into the TCAM now */
  696. ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
  697. if (ret)
  698. return ret;
  699. clear_bit(loc, priv->cfp.used);
  700. clear_bit(loc, priv->cfp.unique);
  701. return 0;
  702. }
  703. static int bcm_sf2_cfp_rule_del(struct bcm_sf2_priv *priv, int port,
  704. u32 loc)
  705. {
  706. u32 next_loc = 0;
  707. int ret;
  708. /* Refuse deleting unused rules, and those that are not unique since
  709. * that could leave IPv6 rules with one of the chained rule in the
  710. * table.
  711. */
  712. if (!test_bit(loc, priv->cfp.unique) || loc == 0)
  713. return -EINVAL;
  714. ret = bcm_sf2_cfp_rule_del_one(priv, port, loc, &next_loc);
  715. if (ret)
  716. return ret;
  717. /* If this was an IPv6 rule, delete is companion rule too */
  718. if (next_loc)
  719. ret = bcm_sf2_cfp_rule_del_one(priv, port, next_loc, NULL);
  720. return ret;
  721. }
  722. static void bcm_sf2_invert_masks(struct ethtool_rx_flow_spec *flow)
  723. {
  724. unsigned int i;
  725. for (i = 0; i < sizeof(flow->m_u); i++)
  726. flow->m_u.hdata[i] ^= 0xff;
  727. flow->m_ext.vlan_etype ^= cpu_to_be16(~0);
  728. flow->m_ext.vlan_tci ^= cpu_to_be16(~0);
  729. flow->m_ext.data[0] ^= cpu_to_be32(~0);
  730. flow->m_ext.data[1] ^= cpu_to_be32(~0);
  731. }
  732. static int bcm_sf2_cfp_unslice_ipv4(struct bcm_sf2_priv *priv,
  733. struct ethtool_tcpip4_spec *v4_spec,
  734. bool mask)
  735. {
  736. u32 reg, offset, ipv4;
  737. u16 src_dst_port;
  738. if (mask)
  739. offset = CORE_CFP_MASK_PORT(3);
  740. else
  741. offset = CORE_CFP_DATA_PORT(3);
  742. reg = core_readl(priv, offset);
  743. /* src port [15:8] */
  744. src_dst_port = reg << 8;
  745. if (mask)
  746. offset = CORE_CFP_MASK_PORT(2);
  747. else
  748. offset = CORE_CFP_DATA_PORT(2);
  749. reg = core_readl(priv, offset);
  750. /* src port [7:0] */
  751. src_dst_port |= (reg >> 24);
  752. v4_spec->pdst = cpu_to_be16(src_dst_port);
  753. v4_spec->psrc = cpu_to_be16((u16)(reg >> 8));
  754. /* IPv4 dst [15:8] */
  755. ipv4 = (reg & 0xff) << 8;
  756. if (mask)
  757. offset = CORE_CFP_MASK_PORT(1);
  758. else
  759. offset = CORE_CFP_DATA_PORT(1);
  760. reg = core_readl(priv, offset);
  761. /* IPv4 dst [31:16] */
  762. ipv4 |= ((reg >> 8) & 0xffff) << 16;
  763. /* IPv4 dst [7:0] */
  764. ipv4 |= (reg >> 24) & 0xff;
  765. v4_spec->ip4dst = cpu_to_be32(ipv4);
  766. /* IPv4 src [15:8] */
  767. ipv4 = (reg & 0xff) << 8;
  768. if (mask)
  769. offset = CORE_CFP_MASK_PORT(0);
  770. else
  771. offset = CORE_CFP_DATA_PORT(0);
  772. reg = core_readl(priv, offset);
  773. /* Once the TCAM is programmed, the mask reflects the slice number
  774. * being matched, don't bother checking it when reading back the
  775. * mask spec
  776. */
  777. if (!mask && !(reg & SLICE_VALID))
  778. return -EINVAL;
  779. /* IPv4 src [7:0] */
  780. ipv4 |= (reg >> 24) & 0xff;
  781. /* IPv4 src [31:16] */
  782. ipv4 |= ((reg >> 8) & 0xffff) << 16;
  783. v4_spec->ip4src = cpu_to_be32(ipv4);
  784. return 0;
  785. }
  786. static int bcm_sf2_cfp_ipv4_rule_get(struct bcm_sf2_priv *priv, int port,
  787. struct ethtool_rx_flow_spec *fs)
  788. {
  789. struct ethtool_tcpip4_spec *v4_spec = NULL, *v4_m_spec = NULL;
  790. u32 reg;
  791. int ret;
  792. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  793. switch ((reg & IPPROTO_MASK) >> IPPROTO_SHIFT) {
  794. case IPPROTO_TCP:
  795. fs->flow_type = TCP_V4_FLOW;
  796. v4_spec = &fs->h_u.tcp_ip4_spec;
  797. v4_m_spec = &fs->m_u.tcp_ip4_spec;
  798. break;
  799. case IPPROTO_UDP:
  800. fs->flow_type = UDP_V4_FLOW;
  801. v4_spec = &fs->h_u.udp_ip4_spec;
  802. v4_m_spec = &fs->m_u.udp_ip4_spec;
  803. break;
  804. default:
  805. return -EINVAL;
  806. }
  807. fs->m_ext.data[0] = cpu_to_be32((reg >> IP_FRAG_SHIFT) & 1);
  808. v4_spec->tos = (reg >> IPTOS_SHIFT) & IPTOS_MASK;
  809. ret = bcm_sf2_cfp_unslice_ipv4(priv, v4_spec, false);
  810. if (ret)
  811. return ret;
  812. return bcm_sf2_cfp_unslice_ipv4(priv, v4_m_spec, true);
  813. }
  814. static int bcm_sf2_cfp_unslice_ipv6(struct bcm_sf2_priv *priv,
  815. __be32 *ip6_addr, __be16 *port,
  816. bool mask)
  817. {
  818. u32 reg, tmp, offset;
  819. /* C-Tag [31:24]
  820. * UDF_n_B8 [23:8] (port)
  821. * UDF_n_B7 (upper) [7:0] (addr[15:8])
  822. */
  823. if (mask)
  824. offset = CORE_CFP_MASK_PORT(4);
  825. else
  826. offset = CORE_CFP_DATA_PORT(4);
  827. reg = core_readl(priv, offset);
  828. *port = cpu_to_be32(reg) >> 8;
  829. tmp = (u32)(reg & 0xff) << 8;
  830. /* UDF_n_B7 (lower) [31:24] (addr[7:0])
  831. * UDF_n_B6 [23:8] (addr[31:16])
  832. * UDF_n_B5 (upper) [7:0] (addr[47:40])
  833. */
  834. if (mask)
  835. offset = CORE_CFP_MASK_PORT(3);
  836. else
  837. offset = CORE_CFP_DATA_PORT(3);
  838. reg = core_readl(priv, offset);
  839. tmp |= (reg >> 24) & 0xff;
  840. tmp |= (u32)((reg >> 8) << 16);
  841. ip6_addr[3] = cpu_to_be32(tmp);
  842. tmp = (u32)(reg & 0xff) << 8;
  843. /* UDF_n_B5 (lower) [31:24] (addr[39:32])
  844. * UDF_n_B4 [23:8] (addr[63:48])
  845. * UDF_n_B3 (upper) [7:0] (addr[79:72])
  846. */
  847. if (mask)
  848. offset = CORE_CFP_MASK_PORT(2);
  849. else
  850. offset = CORE_CFP_DATA_PORT(2);
  851. reg = core_readl(priv, offset);
  852. tmp |= (reg >> 24) & 0xff;
  853. tmp |= (u32)((reg >> 8) << 16);
  854. ip6_addr[2] = cpu_to_be32(tmp);
  855. tmp = (u32)(reg & 0xff) << 8;
  856. /* UDF_n_B3 (lower) [31:24] (addr[71:64])
  857. * UDF_n_B2 [23:8] (addr[95:80])
  858. * UDF_n_B1 (upper) [7:0] (addr[111:104])
  859. */
  860. if (mask)
  861. offset = CORE_CFP_MASK_PORT(1);
  862. else
  863. offset = CORE_CFP_DATA_PORT(1);
  864. reg = core_readl(priv, offset);
  865. tmp |= (reg >> 24) & 0xff;
  866. tmp |= (u32)((reg >> 8) << 16);
  867. ip6_addr[1] = cpu_to_be32(tmp);
  868. tmp = (u32)(reg & 0xff) << 8;
  869. /* UDF_n_B1 (lower) [31:24] (addr[103:96])
  870. * UDF_n_B0 [23:8] (addr[127:112])
  871. * Reserved [7:4]
  872. * Slice ID [3:2]
  873. * Slice valid [1:0]
  874. */
  875. if (mask)
  876. offset = CORE_CFP_MASK_PORT(0);
  877. else
  878. offset = CORE_CFP_DATA_PORT(0);
  879. reg = core_readl(priv, offset);
  880. tmp |= (reg >> 24) & 0xff;
  881. tmp |= (u32)((reg >> 8) << 16);
  882. ip6_addr[0] = cpu_to_be32(tmp);
  883. if (!mask && !(reg & SLICE_VALID))
  884. return -EINVAL;
  885. return 0;
  886. }
  887. static int bcm_sf2_cfp_ipv6_rule_get(struct bcm_sf2_priv *priv, int port,
  888. struct ethtool_rx_flow_spec *fs,
  889. u32 next_loc)
  890. {
  891. struct ethtool_tcpip6_spec *v6_spec = NULL, *v6_m_spec = NULL;
  892. u32 reg;
  893. int ret;
  894. /* UDPv6 and TCPv6 both use ethtool_tcpip6_spec so we are fine
  895. * assuming tcp_ip6_spec here being an union.
  896. */
  897. v6_spec = &fs->h_u.tcp_ip6_spec;
  898. v6_m_spec = &fs->m_u.tcp_ip6_spec;
  899. /* Read the second half first */
  900. ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_spec->ip6dst, &v6_spec->pdst,
  901. false);
  902. if (ret)
  903. return ret;
  904. ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_m_spec->ip6dst,
  905. &v6_m_spec->pdst, true);
  906. if (ret)
  907. return ret;
  908. /* Read last to avoid next entry clobbering the results during search
  909. * operations. We would not have the port enabled for this rule, so
  910. * don't bother checking it.
  911. */
  912. (void)core_readl(priv, CORE_CFP_DATA_PORT(7));
  913. /* The slice number is valid, so read the rule we are chained from now
  914. * which is our first half.
  915. */
  916. bcm_sf2_cfp_rule_addr_set(priv, next_loc);
  917. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
  918. if (ret)
  919. return ret;
  920. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  921. switch ((reg & IPPROTO_MASK) >> IPPROTO_SHIFT) {
  922. case IPPROTO_TCP:
  923. fs->flow_type = TCP_V6_FLOW;
  924. break;
  925. case IPPROTO_UDP:
  926. fs->flow_type = UDP_V6_FLOW;
  927. break;
  928. default:
  929. return -EINVAL;
  930. }
  931. ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_spec->ip6src, &v6_spec->psrc,
  932. false);
  933. if (ret)
  934. return ret;
  935. return bcm_sf2_cfp_unslice_ipv6(priv, v6_m_spec->ip6src,
  936. &v6_m_spec->psrc, true);
  937. }
  938. static int bcm_sf2_cfp_rule_get(struct bcm_sf2_priv *priv, int port,
  939. struct ethtool_rxnfc *nfc)
  940. {
  941. u32 reg, ipv4_or_chain_id;
  942. unsigned int queue_num;
  943. int ret;
  944. bcm_sf2_cfp_rule_addr_set(priv, nfc->fs.location);
  945. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | ACT_POL_RAM);
  946. if (ret)
  947. return ret;
  948. reg = core_readl(priv, CORE_ACT_POL_DATA0);
  949. ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
  950. if (ret)
  951. return ret;
  952. /* Extract the destination port */
  953. nfc->fs.ring_cookie = fls((reg >> DST_MAP_IB_SHIFT) &
  954. DST_MAP_IB_MASK) - 1;
  955. /* There is no Port 6, so we compensate for that here */
  956. if (nfc->fs.ring_cookie >= 6)
  957. nfc->fs.ring_cookie++;
  958. nfc->fs.ring_cookie *= SF2_NUM_EGRESS_QUEUES;
  959. /* Extract the destination queue */
  960. queue_num = (reg >> NEW_TC_SHIFT) & NEW_TC_MASK;
  961. nfc->fs.ring_cookie += queue_num;
  962. /* Extract the L3_FRAMING or CHAIN_ID */
  963. reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
  964. /* With IPv6 rules this would contain a non-zero chain ID since
  965. * we reserve entry 0 and it cannot be used. So if we read 0 here
  966. * this means an IPv4 rule.
  967. */
  968. ipv4_or_chain_id = (reg >> L3_FRAMING_SHIFT) & 0xff;
  969. if (ipv4_or_chain_id == 0)
  970. ret = bcm_sf2_cfp_ipv4_rule_get(priv, port, &nfc->fs);
  971. else
  972. ret = bcm_sf2_cfp_ipv6_rule_get(priv, port, &nfc->fs,
  973. ipv4_or_chain_id);
  974. if (ret)
  975. return ret;
  976. /* Read last to avoid next entry clobbering the results during search
  977. * operations
  978. */
  979. reg = core_readl(priv, CORE_CFP_DATA_PORT(7));
  980. if (!(reg & 1 << port))
  981. return -EINVAL;
  982. bcm_sf2_invert_masks(&nfc->fs);
  983. /* Put the TCAM size here */
  984. nfc->data = bcm_sf2_cfp_rule_size(priv);
  985. return 0;
  986. }
  987. /* We implement the search doing a TCAM search operation */
  988. static int bcm_sf2_cfp_rule_get_all(struct bcm_sf2_priv *priv,
  989. int port, struct ethtool_rxnfc *nfc,
  990. u32 *rule_locs)
  991. {
  992. unsigned int index = 1, rules_cnt = 0;
  993. for_each_set_bit_from(index, priv->cfp.unique, priv->num_cfp_rules) {
  994. rule_locs[rules_cnt] = index;
  995. rules_cnt++;
  996. }
  997. /* Put the TCAM size here */
  998. nfc->data = bcm_sf2_cfp_rule_size(priv);
  999. nfc->rule_cnt = rules_cnt;
  1000. return 0;
  1001. }
  1002. int bcm_sf2_get_rxnfc(struct dsa_switch *ds, int port,
  1003. struct ethtool_rxnfc *nfc, u32 *rule_locs)
  1004. {
  1005. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  1006. int ret = 0;
  1007. mutex_lock(&priv->cfp.lock);
  1008. switch (nfc->cmd) {
  1009. case ETHTOOL_GRXCLSRLCNT:
  1010. /* Subtract the default, unusable rule */
  1011. nfc->rule_cnt = bitmap_weight(priv->cfp.unique,
  1012. priv->num_cfp_rules) - 1;
  1013. /* We support specifying rule locations */
  1014. nfc->data |= RX_CLS_LOC_SPECIAL;
  1015. break;
  1016. case ETHTOOL_GRXCLSRULE:
  1017. ret = bcm_sf2_cfp_rule_get(priv, port, nfc);
  1018. break;
  1019. case ETHTOOL_GRXCLSRLALL:
  1020. ret = bcm_sf2_cfp_rule_get_all(priv, port, nfc, rule_locs);
  1021. break;
  1022. default:
  1023. ret = -EOPNOTSUPP;
  1024. break;
  1025. }
  1026. mutex_unlock(&priv->cfp.lock);
  1027. return ret;
  1028. }
  1029. int bcm_sf2_set_rxnfc(struct dsa_switch *ds, int port,
  1030. struct ethtool_rxnfc *nfc)
  1031. {
  1032. struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
  1033. int ret = 0;
  1034. mutex_lock(&priv->cfp.lock);
  1035. switch (nfc->cmd) {
  1036. case ETHTOOL_SRXCLSRLINS:
  1037. ret = bcm_sf2_cfp_rule_set(ds, port, &nfc->fs);
  1038. break;
  1039. case ETHTOOL_SRXCLSRLDEL:
  1040. ret = bcm_sf2_cfp_rule_del(priv, port, nfc->fs.location);
  1041. break;
  1042. default:
  1043. ret = -EOPNOTSUPP;
  1044. break;
  1045. }
  1046. mutex_unlock(&priv->cfp.lock);
  1047. return ret;
  1048. }
  1049. int bcm_sf2_cfp_rst(struct bcm_sf2_priv *priv)
  1050. {
  1051. unsigned int timeout = 1000;
  1052. u32 reg;
  1053. reg = core_readl(priv, CORE_CFP_ACC);
  1054. reg |= TCAM_RESET;
  1055. core_writel(priv, reg, CORE_CFP_ACC);
  1056. do {
  1057. reg = core_readl(priv, CORE_CFP_ACC);
  1058. if (!(reg & TCAM_RESET))
  1059. break;
  1060. cpu_relax();
  1061. } while (timeout--);
  1062. if (!timeout)
  1063. return -ETIMEDOUT;
  1064. return 0;
  1065. }