bcm_sf2_cfp.c 33 KB

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