1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Broadcom Starfighter 2 DSA switch CFP support
5 * Copyright (C) 2016, Broadcom
8 #include <linux/list.h>
9 #include <linux/ethtool.h>
10 #include <linux/if_ether.h>
12 #include <linux/netdevice.h>
14 #include <linux/bitmap.h>
15 #include <net/flow_offload.h>
18 #include "bcm_sf2_regs.h"
22 struct ethtool_rx_flow_spec fs;
23 struct list_head next;
26 struct cfp_udf_slice_layout {
27 u8 slices[UDFS_PER_SLICE];
32 struct cfp_udf_layout {
33 struct cfp_udf_slice_layout udfs[UDF_NUM_SLICES];
36 static const u8 zero_slice[UDFS_PER_SLICE] = { };
38 /* UDF slices layout for a TCPv4/UDPv4 specification */
39 static const struct cfp_udf_layout udf_tcpip4_layout = {
43 /* End of L2, byte offset 12, src IP[0:15] */
45 /* End of L2, byte offset 14, src IP[16:31] */
47 /* End of L2, byte offset 16, dst IP[0:15] */
49 /* End of L2, byte offset 18, dst IP[16:31] */
51 /* End of L3, byte offset 0, src port */
53 /* End of L3, byte offset 2, dst port */
57 .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
58 .base_offset = CORE_UDF_0_A_0_8_PORT_0 + UDF_SLICE_OFFSET,
63 /* UDF slices layout for a TCPv6/UDPv6 specification */
64 static const struct cfp_udf_layout udf_tcpip6_layout = {
68 /* End of L2, byte offset 8, src IP[0:15] */
70 /* End of L2, byte offset 10, src IP[16:31] */
72 /* End of L2, byte offset 12, src IP[32:47] */
74 /* End of L2, byte offset 14, src IP[48:63] */
76 /* End of L2, byte offset 16, src IP[64:79] */
78 /* End of L2, byte offset 18, src IP[80:95] */
80 /* End of L2, byte offset 20, src IP[96:111] */
82 /* End of L2, byte offset 22, src IP[112:127] */
84 /* End of L3, byte offset 0, src port */
87 .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
88 .base_offset = CORE_UDF_0_B_0_8_PORT_0,
92 /* End of L2, byte offset 24, dst IP[0:15] */
94 /* End of L2, byte offset 26, dst IP[16:31] */
96 /* End of L2, byte offset 28, dst IP[32:47] */
98 /* End of L2, byte offset 30, dst IP[48:63] */
100 /* End of L2, byte offset 32, dst IP[64:79] */
102 /* End of L2, byte offset 34, dst IP[80:95] */
104 /* End of L2, byte offset 36, dst IP[96:111] */
106 /* End of L2, byte offset 38, dst IP[112:127] */
108 /* End of L3, byte offset 2, dst port */
111 .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
112 .base_offset = CORE_UDF_0_D_0_11_PORT_0,
117 static inline unsigned int bcm_sf2_get_num_udf_slices(const u8 *layout)
119 unsigned int i, count = 0;
121 for (i = 0; i < UDFS_PER_SLICE; i++) {
129 static inline u32 udf_upper_bits(unsigned int num_udf)
131 return GENMASK(num_udf - 1, 0) >> (UDFS_PER_SLICE - 1);
134 static inline u32 udf_lower_bits(unsigned int num_udf)
136 return (u8)GENMASK(num_udf - 1, 0);
139 static unsigned int bcm_sf2_get_slice_number(const struct cfp_udf_layout *l,
142 const struct cfp_udf_slice_layout *slice_layout;
143 unsigned int slice_idx;
145 for (slice_idx = start; slice_idx < UDF_NUM_SLICES; slice_idx++) {
146 slice_layout = &l->udfs[slice_idx];
147 if (memcmp(slice_layout->slices, zero_slice,
155 static void bcm_sf2_cfp_udf_set(struct bcm_sf2_priv *priv,
156 const struct cfp_udf_layout *layout,
157 unsigned int slice_num)
159 u32 offset = layout->udfs[slice_num].base_offset;
162 for (i = 0; i < UDFS_PER_SLICE; i++)
163 core_writel(priv, layout->udfs[slice_num].slices[i],
167 static int bcm_sf2_cfp_op(struct bcm_sf2_priv *priv, unsigned int op)
169 unsigned int timeout = 1000;
172 reg = core_readl(priv, CORE_CFP_ACC);
173 reg &= ~(OP_SEL_MASK | RAM_SEL_MASK);
174 reg |= OP_STR_DONE | op;
175 core_writel(priv, reg, CORE_CFP_ACC);
178 reg = core_readl(priv, CORE_CFP_ACC);
179 if (!(reg & OP_STR_DONE))
191 static inline void bcm_sf2_cfp_rule_addr_set(struct bcm_sf2_priv *priv,
196 WARN_ON(addr >= priv->num_cfp_rules);
198 reg = core_readl(priv, CORE_CFP_ACC);
199 reg &= ~(XCESS_ADDR_MASK << XCESS_ADDR_SHIFT);
200 reg |= addr << XCESS_ADDR_SHIFT;
201 core_writel(priv, reg, CORE_CFP_ACC);
204 static inline unsigned int bcm_sf2_cfp_rule_size(struct bcm_sf2_priv *priv)
206 /* Entry #0 is reserved */
207 return priv->num_cfp_rules - 1;
210 static int bcm_sf2_cfp_act_pol_set(struct bcm_sf2_priv *priv,
211 unsigned int rule_index,
213 unsigned int port_num,
214 unsigned int queue_num,
220 /* Replace ARL derived destination with DST_MAP derived, define
221 * which port and queue this should be forwarded to.
224 reg = CHANGE_FWRD_MAP_IB_REP_ARL |
225 BIT(port_num + DST_MAP_IB_SHIFT) |
226 CHANGE_TC | queue_num << NEW_TC_SHIFT;
230 /* Enable looping back to the original port */
231 if (src_port == port_num)
234 core_writel(priv, reg, CORE_ACT_POL_DATA0);
236 /* Set classification ID that needs to be put in Broadcom tag */
237 core_writel(priv, rule_index << CHAIN_ID_SHIFT, CORE_ACT_POL_DATA1);
239 core_writel(priv, 0, CORE_ACT_POL_DATA2);
241 /* Configure policer RAM now */
242 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | ACT_POL_RAM);
244 pr_err("Policer entry at %d failed\n", rule_index);
248 /* Disable the policer */
249 core_writel(priv, POLICER_MODE_DISABLE, CORE_RATE_METER0);
251 /* Now the rate meter */
252 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | RATE_METER_RAM);
254 pr_err("Meter entry at %d failed\n", rule_index);
261 static void bcm_sf2_cfp_slice_ipv4(struct bcm_sf2_priv *priv,
262 struct flow_dissector_key_ipv4_addrs *addrs,
263 struct flow_dissector_key_ports *ports,
264 unsigned int slice_num,
275 offset = CORE_CFP_MASK_PORT(4);
277 offset = CORE_CFP_DATA_PORT(4);
278 core_writel(priv, reg, offset);
284 reg = be16_to_cpu(ports->dst) >> 8;
286 offset = CORE_CFP_MASK_PORT(3);
288 offset = CORE_CFP_DATA_PORT(3);
289 core_writel(priv, reg, offset);
295 reg = (be16_to_cpu(ports->dst) & 0xff) << 24 |
296 (u32)be16_to_cpu(ports->src) << 8 |
297 (be32_to_cpu(addrs->dst) & 0x0000ff00) >> 8;
299 offset = CORE_CFP_MASK_PORT(2);
301 offset = CORE_CFP_DATA_PORT(2);
302 core_writel(priv, reg, offset);
308 reg = (u32)(be32_to_cpu(addrs->dst) & 0xff) << 24 |
309 (u32)(be32_to_cpu(addrs->dst) >> 16) << 8 |
310 (be32_to_cpu(addrs->src) & 0x0000ff00) >> 8;
312 offset = CORE_CFP_MASK_PORT(1);
314 offset = CORE_CFP_DATA_PORT(1);
315 core_writel(priv, reg, offset);
323 reg = (u32)(be32_to_cpu(addrs->src) & 0xff) << 24 |
324 (u32)(be32_to_cpu(addrs->src) >> 16) << 8 |
325 SLICE_NUM(slice_num) | SLICE_VALID;
327 offset = CORE_CFP_MASK_PORT(0);
329 offset = CORE_CFP_DATA_PORT(0);
330 core_writel(priv, reg, offset);
333 static int bcm_sf2_cfp_ipv4_rule_set(struct bcm_sf2_priv *priv, int port,
334 unsigned int port_num,
335 unsigned int queue_num,
336 struct ethtool_rx_flow_spec *fs)
338 struct ethtool_rx_flow_spec_input input = {};
339 const struct cfp_udf_layout *layout;
340 unsigned int slice_num, rule_index;
341 struct ethtool_rx_flow_rule *flow;
342 struct flow_match_ipv4_addrs ipv4;
343 struct flow_match_ports ports;
344 struct flow_match_ip ip;
345 u8 ip_proto, ip_frag;
350 switch (fs->flow_type & ~FLOW_EXT) {
352 ip_proto = IPPROTO_TCP;
355 ip_proto = IPPROTO_UDP;
361 ip_frag = be32_to_cpu(fs->m_ext.data[0]);
363 /* Locate the first rule available */
364 if (fs->location == RX_CLS_LOC_ANY)
365 rule_index = find_first_zero_bit(priv->cfp.used,
366 priv->num_cfp_rules);
368 rule_index = fs->location;
370 if (rule_index > bcm_sf2_cfp_rule_size(priv))
374 flow = ethtool_rx_flow_rule_create(&input);
376 return PTR_ERR(flow);
378 flow_rule_match_ipv4_addrs(flow->rule, &ipv4);
379 flow_rule_match_ports(flow->rule, &ports);
380 flow_rule_match_ip(flow->rule, &ip);
382 layout = &udf_tcpip4_layout;
383 /* We only use one UDF slice for now */
384 slice_num = bcm_sf2_get_slice_number(layout, 0);
385 if (slice_num == UDF_NUM_SLICES) {
387 goto out_err_flow_rule;
390 num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
392 /* Apply the UDF layout for this filter */
393 bcm_sf2_cfp_udf_set(priv, layout, slice_num);
395 /* Apply to all packets received through this port */
396 core_writel(priv, BIT(port), CORE_CFP_DATA_PORT(7));
398 /* Source port map match */
399 core_writel(priv, 0xff, CORE_CFP_MASK_PORT(7));
401 /* S-Tag status [31:30]
402 * C-Tag status [29:28]
415 core_writel(priv, ip.key->tos << IPTOS_SHIFT |
416 ip_proto << IPPROTO_SHIFT | ip_frag << IP_FRAG_SHIFT |
417 udf_upper_bits(num_udf),
418 CORE_CFP_DATA_PORT(6));
420 /* Mask with the specific layout for IPv4 packets */
421 core_writel(priv, layout->udfs[slice_num].mask_value |
422 udf_upper_bits(num_udf), CORE_CFP_MASK_PORT(6));
424 /* UDF_Valid[7:0] [31:24]
428 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_DATA_PORT(5));
430 /* Mask all but valid UDFs */
431 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_MASK_PORT(5));
433 /* Program the match and the mask */
434 bcm_sf2_cfp_slice_ipv4(priv, ipv4.key, ports.key, slice_num, false);
435 bcm_sf2_cfp_slice_ipv4(priv, ipv4.mask, ports.mask, SLICE_NUM_MASK, true);
437 /* Insert into TCAM now */
438 bcm_sf2_cfp_rule_addr_set(priv, rule_index);
440 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
442 pr_err("TCAM entry at addr %d failed\n", rule_index);
443 goto out_err_flow_rule;
446 /* Insert into Action and policer RAMs now */
447 ret = bcm_sf2_cfp_act_pol_set(priv, rule_index, port, port_num,
450 goto out_err_flow_rule;
452 /* Turn on CFP for this rule now */
453 reg = core_readl(priv, CORE_CFP_CTL_REG);
455 core_writel(priv, reg, CORE_CFP_CTL_REG);
457 /* Flag the rule as being used and return it */
458 set_bit(rule_index, priv->cfp.used);
459 set_bit(rule_index, priv->cfp.unique);
460 fs->location = rule_index;
465 ethtool_rx_flow_rule_destroy(flow);
469 static void bcm_sf2_cfp_slice_ipv6(struct bcm_sf2_priv *priv,
470 const __be32 *ip6_addr, const __be16 port,
471 unsigned int slice_num,
474 u32 reg, tmp, val, offset;
477 * UDF_n_B8 [23:8] (port)
478 * UDF_n_B7 (upper) [7:0] (addr[15:8])
480 reg = be32_to_cpu(ip6_addr[3]);
481 val = (u32)be16_to_cpu(port) << 8 | ((reg >> 8) & 0xff);
483 offset = CORE_CFP_MASK_PORT(4);
485 offset = CORE_CFP_DATA_PORT(4);
486 core_writel(priv, val, offset);
488 /* UDF_n_B7 (lower) [31:24] (addr[7:0])
489 * UDF_n_B6 [23:8] (addr[31:16])
490 * UDF_n_B5 (upper) [7:0] (addr[47:40])
492 tmp = be32_to_cpu(ip6_addr[2]);
493 val = (u32)(reg & 0xff) << 24 | (u32)(reg >> 16) << 8 |
496 offset = CORE_CFP_MASK_PORT(3);
498 offset = CORE_CFP_DATA_PORT(3);
499 core_writel(priv, val, offset);
501 /* UDF_n_B5 (lower) [31:24] (addr[39:32])
502 * UDF_n_B4 [23:8] (addr[63:48])
503 * UDF_n_B3 (upper) [7:0] (addr[79:72])
505 reg = be32_to_cpu(ip6_addr[1]);
506 val = (u32)(tmp & 0xff) << 24 | (u32)(tmp >> 16) << 8 |
509 offset = CORE_CFP_MASK_PORT(2);
511 offset = CORE_CFP_DATA_PORT(2);
512 core_writel(priv, val, offset);
514 /* UDF_n_B3 (lower) [31:24] (addr[71:64])
515 * UDF_n_B2 [23:8] (addr[95:80])
516 * UDF_n_B1 (upper) [7:0] (addr[111:104])
518 tmp = be32_to_cpu(ip6_addr[0]);
519 val = (u32)(reg & 0xff) << 24 | (u32)(reg >> 16) << 8 |
522 offset = CORE_CFP_MASK_PORT(1);
524 offset = CORE_CFP_DATA_PORT(1);
525 core_writel(priv, val, offset);
527 /* UDF_n_B1 (lower) [31:24] (addr[103:96])
528 * UDF_n_B0 [23:8] (addr[127:112])
533 reg = (u32)(tmp & 0xff) << 24 | (u32)(tmp >> 16) << 8 |
534 SLICE_NUM(slice_num) | SLICE_VALID;
536 offset = CORE_CFP_MASK_PORT(0);
538 offset = CORE_CFP_DATA_PORT(0);
539 core_writel(priv, reg, offset);
542 static struct cfp_rule *bcm_sf2_cfp_rule_find(struct bcm_sf2_priv *priv,
543 int port, u32 location)
545 struct cfp_rule *rule = NULL;
547 list_for_each_entry(rule, &priv->cfp.rules_list, next) {
548 if (rule->port == port && rule->fs.location == location)
555 static int bcm_sf2_cfp_rule_cmp(struct bcm_sf2_priv *priv, int port,
556 struct ethtool_rx_flow_spec *fs)
558 struct cfp_rule *rule = NULL;
562 if (list_empty(&priv->cfp.rules_list))
565 list_for_each_entry(rule, &priv->cfp.rules_list, next) {
567 if (rule->port != port)
570 if (rule->fs.flow_type != fs->flow_type ||
571 rule->fs.ring_cookie != fs->ring_cookie ||
572 rule->fs.m_ext.data[0] != fs->m_ext.data[0])
575 switch (fs->flow_type & ~FLOW_EXT) {
578 fs_size = sizeof(struct ethtool_tcpip6_spec);
582 fs_size = sizeof(struct ethtool_tcpip4_spec);
588 ret = memcmp(&rule->fs.h_u, &fs->h_u, fs_size);
589 ret |= memcmp(&rule->fs.m_u, &fs->m_u, fs_size);
597 static int bcm_sf2_cfp_ipv6_rule_set(struct bcm_sf2_priv *priv, int port,
598 unsigned int port_num,
599 unsigned int queue_num,
600 struct ethtool_rx_flow_spec *fs)
602 struct ethtool_rx_flow_spec_input input = {};
603 unsigned int slice_num, rule_index[2];
604 const struct cfp_udf_layout *layout;
605 struct ethtool_rx_flow_rule *flow;
606 struct flow_match_ipv6_addrs ipv6;
607 struct flow_match_ports ports;
608 u8 ip_proto, ip_frag;
613 switch (fs->flow_type & ~FLOW_EXT) {
615 ip_proto = IPPROTO_TCP;
618 ip_proto = IPPROTO_UDP;
624 ip_frag = be32_to_cpu(fs->m_ext.data[0]);
626 layout = &udf_tcpip6_layout;
627 slice_num = bcm_sf2_get_slice_number(layout, 0);
628 if (slice_num == UDF_NUM_SLICES)
631 num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
633 /* Negotiate two indexes, one for the second half which we are chained
634 * from, which is what we will return to user-space, and a second one
635 * which is used to store its first half. That first half does not
636 * allow any choice of placement, so it just needs to find the next
637 * available bit. We return the second half as fs->location because
638 * that helps with the rule lookup later on since the second half is
639 * chained from its first half, we can easily identify IPv6 CFP rules
640 * by looking whether they carry a CHAIN_ID.
642 * We also want the second half to have a lower rule_index than its
643 * first half because the HW search is by incrementing addresses.
645 if (fs->location == RX_CLS_LOC_ANY)
646 rule_index[1] = find_first_zero_bit(priv->cfp.used,
647 priv->num_cfp_rules);
649 rule_index[1] = fs->location;
650 if (rule_index[1] > bcm_sf2_cfp_rule_size(priv))
653 /* Flag it as used (cleared on error path) such that we can immediately
654 * obtain a second one to chain from.
656 set_bit(rule_index[1], priv->cfp.used);
658 rule_index[0] = find_first_zero_bit(priv->cfp.used,
659 priv->num_cfp_rules);
660 if (rule_index[0] > bcm_sf2_cfp_rule_size(priv)) {
666 flow = ethtool_rx_flow_rule_create(&input);
671 flow_rule_match_ipv6_addrs(flow->rule, &ipv6);
672 flow_rule_match_ports(flow->rule, &ports);
674 /* Apply the UDF layout for this filter */
675 bcm_sf2_cfp_udf_set(priv, layout, slice_num);
677 /* Apply to all packets received through this port */
678 core_writel(priv, BIT(port), CORE_CFP_DATA_PORT(7));
680 /* Source port map match */
681 core_writel(priv, 0xff, CORE_CFP_MASK_PORT(7));
683 /* S-Tag status [31:30]
684 * C-Tag status [29:28]
697 reg = 1 << L3_FRAMING_SHIFT | ip_proto << IPPROTO_SHIFT |
698 ip_frag << IP_FRAG_SHIFT | udf_upper_bits(num_udf);
699 core_writel(priv, reg, CORE_CFP_DATA_PORT(6));
701 /* Mask with the specific layout for IPv6 packets including
704 reg = layout->udfs[slice_num].mask_value | udf_upper_bits(num_udf);
705 core_writel(priv, reg, CORE_CFP_MASK_PORT(6));
707 /* UDF_Valid[7:0] [31:24]
711 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_DATA_PORT(5));
713 /* Mask all but valid UDFs */
714 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_MASK_PORT(5));
716 /* Slice the IPv6 source address and port */
717 bcm_sf2_cfp_slice_ipv6(priv, ipv6.key->src.in6_u.u6_addr32,
718 ports.key->src, slice_num, false);
719 bcm_sf2_cfp_slice_ipv6(priv, ipv6.mask->src.in6_u.u6_addr32,
720 ports.mask->src, SLICE_NUM_MASK, true);
722 /* Insert into TCAM now because we need to insert a second rule */
723 bcm_sf2_cfp_rule_addr_set(priv, rule_index[0]);
725 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
727 pr_err("TCAM entry at addr %d failed\n", rule_index[0]);
728 goto out_err_flow_rule;
731 /* Insert into Action and policer RAMs now */
732 ret = bcm_sf2_cfp_act_pol_set(priv, rule_index[0], port, port_num,
735 goto out_err_flow_rule;
737 /* Now deal with the second slice to chain this rule */
738 slice_num = bcm_sf2_get_slice_number(layout, slice_num + 1);
739 if (slice_num == UDF_NUM_SLICES) {
741 goto out_err_flow_rule;
744 num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
746 /* Apply the UDF layout for this filter */
747 bcm_sf2_cfp_udf_set(priv, layout, slice_num);
749 /* Chained rule, source port match is coming from the rule we are
752 core_writel(priv, 0, CORE_CFP_DATA_PORT(7));
753 core_writel(priv, 0, CORE_CFP_MASK_PORT(7));
756 * CHAIN ID [31:24] chain to previous slice
758 * UDF_Valid[11:8] [19:16]
759 * UDF_Valid[7:0] [15:8]
762 reg = rule_index[0] << 24 | udf_upper_bits(num_udf) << 16 |
763 udf_lower_bits(num_udf) << 8;
764 core_writel(priv, reg, CORE_CFP_DATA_PORT(6));
766 /* Mask all except chain ID, UDF Valid[8] and UDF Valid[7:0] */
767 reg = XCESS_ADDR_MASK << 24 | udf_upper_bits(num_udf) << 16 |
768 udf_lower_bits(num_udf) << 8;
769 core_writel(priv, reg, CORE_CFP_MASK_PORT(6));
772 core_writel(priv, 0, CORE_CFP_DATA_PORT(5));
775 core_writel(priv, 0, CORE_CFP_MASK_PORT(5));
777 bcm_sf2_cfp_slice_ipv6(priv, ipv6.key->dst.in6_u.u6_addr32,
778 ports.key->dst, slice_num, false);
779 bcm_sf2_cfp_slice_ipv6(priv, ipv6.mask->dst.in6_u.u6_addr32,
780 ports.key->dst, SLICE_NUM_MASK, true);
782 /* Insert into TCAM now */
783 bcm_sf2_cfp_rule_addr_set(priv, rule_index[1]);
785 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
787 pr_err("TCAM entry at addr %d failed\n", rule_index[1]);
788 goto out_err_flow_rule;
791 /* Insert into Action and policer RAMs now, set chain ID to
792 * the one we are chained to
794 ret = bcm_sf2_cfp_act_pol_set(priv, rule_index[1], port, port_num,
797 goto out_err_flow_rule;
799 /* Turn on CFP for this rule now */
800 reg = core_readl(priv, CORE_CFP_CTL_REG);
802 core_writel(priv, reg, CORE_CFP_CTL_REG);
804 /* Flag the second half rule as being used now, return it as the
805 * location, and flag it as unique while dumping rules
807 set_bit(rule_index[0], priv->cfp.used);
808 set_bit(rule_index[1], priv->cfp.unique);
809 fs->location = rule_index[1];
814 ethtool_rx_flow_rule_destroy(flow);
816 clear_bit(rule_index[1], priv->cfp.used);
820 static int bcm_sf2_cfp_rule_insert(struct dsa_switch *ds, int port,
821 struct ethtool_rx_flow_spec *fs)
823 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
824 s8 cpu_port = ds->ports[port].cpu_dp->index;
825 __u64 ring_cookie = fs->ring_cookie;
826 unsigned int queue_num, port_num;
829 /* This rule is a Wake-on-LAN filter and we must specifically
830 * target the CPU port in order for it to be working.
832 if (ring_cookie == RX_CLS_FLOW_WAKE)
833 ring_cookie = cpu_port * SF2_NUM_EGRESS_QUEUES;
835 /* We do not support discarding packets, check that the
836 * destination port is enabled and that we are within the
837 * number of ports supported by the switch
839 port_num = ring_cookie / SF2_NUM_EGRESS_QUEUES;
841 if (ring_cookie == RX_CLS_FLOW_DISC ||
842 !(dsa_is_user_port(ds, port_num) ||
843 dsa_is_cpu_port(ds, port_num)) ||
844 port_num >= priv->hw_params.num_ports)
847 * We have a small oddity where Port 6 just does not have a
848 * valid bit here (so we substract by one).
850 queue_num = ring_cookie % SF2_NUM_EGRESS_QUEUES;
854 switch (fs->flow_type & ~FLOW_EXT) {
857 ret = bcm_sf2_cfp_ipv4_rule_set(priv, port, port_num,
862 ret = bcm_sf2_cfp_ipv6_rule_set(priv, port, port_num,
873 static int bcm_sf2_cfp_rule_set(struct dsa_switch *ds, int port,
874 struct ethtool_rx_flow_spec *fs)
876 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
877 struct cfp_rule *rule = NULL;
880 /* Check for unsupported extensions */
881 if ((fs->flow_type & FLOW_EXT) && (fs->m_ext.vlan_etype ||
885 if (fs->location != RX_CLS_LOC_ANY && fs->location >= CFP_NUM_RULES)
888 if (fs->location != RX_CLS_LOC_ANY &&
889 test_bit(fs->location, priv->cfp.used))
892 if (fs->location != RX_CLS_LOC_ANY &&
893 fs->location > bcm_sf2_cfp_rule_size(priv))
896 ret = bcm_sf2_cfp_rule_cmp(priv, port, fs);
900 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
904 ret = bcm_sf2_cfp_rule_insert(ds, port, fs);
911 memcpy(&rule->fs, fs, sizeof(*fs));
912 list_add_tail(&rule->next, &priv->cfp.rules_list);
917 static int bcm_sf2_cfp_rule_del_one(struct bcm_sf2_priv *priv, int port,
918 u32 loc, u32 *next_loc)
923 /* Indicate which rule we want to read */
924 bcm_sf2_cfp_rule_addr_set(priv, loc);
926 ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
930 /* Check if this is possibly an IPv6 rule that would
931 * indicate we need to delete its companion rule
934 reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
936 *next_loc = (reg >> 24) & CHAIN_ID_MASK;
938 /* Clear its valid bits */
939 reg = core_readl(priv, CORE_CFP_DATA_PORT(0));
941 core_writel(priv, reg, CORE_CFP_DATA_PORT(0));
943 /* Write back this entry into the TCAM now */
944 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
948 clear_bit(loc, priv->cfp.used);
949 clear_bit(loc, priv->cfp.unique);
954 static int bcm_sf2_cfp_rule_remove(struct bcm_sf2_priv *priv, int port,
960 ret = bcm_sf2_cfp_rule_del_one(priv, port, loc, &next_loc);
964 /* If this was an IPv6 rule, delete is companion rule too */
966 ret = bcm_sf2_cfp_rule_del_one(priv, port, next_loc, NULL);
971 static int bcm_sf2_cfp_rule_del(struct bcm_sf2_priv *priv, int port, u32 loc)
973 struct cfp_rule *rule;
976 if (loc >= CFP_NUM_RULES)
979 /* Refuse deleting unused rules, and those that are not unique since
980 * that could leave IPv6 rules with one of the chained rule in the
983 if (!test_bit(loc, priv->cfp.unique) || loc == 0)
986 rule = bcm_sf2_cfp_rule_find(priv, port, loc);
990 ret = bcm_sf2_cfp_rule_remove(priv, port, loc);
992 list_del(&rule->next);
998 static void bcm_sf2_invert_masks(struct ethtool_rx_flow_spec *flow)
1002 for (i = 0; i < sizeof(flow->m_u); i++)
1003 flow->m_u.hdata[i] ^= 0xff;
1005 flow->m_ext.vlan_etype ^= cpu_to_be16(~0);
1006 flow->m_ext.vlan_tci ^= cpu_to_be16(~0);
1007 flow->m_ext.data[0] ^= cpu_to_be32(~0);
1008 flow->m_ext.data[1] ^= cpu_to_be32(~0);
1011 static int bcm_sf2_cfp_rule_get(struct bcm_sf2_priv *priv, int port,
1012 struct ethtool_rxnfc *nfc)
1014 struct cfp_rule *rule;
1016 rule = bcm_sf2_cfp_rule_find(priv, port, nfc->fs.location);
1020 memcpy(&nfc->fs, &rule->fs, sizeof(rule->fs));
1022 bcm_sf2_invert_masks(&nfc->fs);
1024 /* Put the TCAM size here */
1025 nfc->data = bcm_sf2_cfp_rule_size(priv);
1030 /* We implement the search doing a TCAM search operation */
1031 static int bcm_sf2_cfp_rule_get_all(struct bcm_sf2_priv *priv,
1032 int port, struct ethtool_rxnfc *nfc,
1035 unsigned int index = 1, rules_cnt = 0;
1037 for_each_set_bit_from(index, priv->cfp.unique, priv->num_cfp_rules) {
1038 rule_locs[rules_cnt] = index;
1042 /* Put the TCAM size here */
1043 nfc->data = bcm_sf2_cfp_rule_size(priv);
1044 nfc->rule_cnt = rules_cnt;
1049 int bcm_sf2_get_rxnfc(struct dsa_switch *ds, int port,
1050 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1052 struct net_device *p = ds->ports[port].cpu_dp->master;
1053 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1056 mutex_lock(&priv->cfp.lock);
1059 case ETHTOOL_GRXCLSRLCNT:
1060 /* Subtract the default, unusable rule */
1061 nfc->rule_cnt = bitmap_weight(priv->cfp.unique,
1062 priv->num_cfp_rules) - 1;
1063 /* We support specifying rule locations */
1064 nfc->data |= RX_CLS_LOC_SPECIAL;
1066 case ETHTOOL_GRXCLSRULE:
1067 ret = bcm_sf2_cfp_rule_get(priv, port, nfc);
1069 case ETHTOOL_GRXCLSRLALL:
1070 ret = bcm_sf2_cfp_rule_get_all(priv, port, nfc, rule_locs);
1077 mutex_unlock(&priv->cfp.lock);
1082 /* Pass up the commands to the attached master network device */
1083 if (p->ethtool_ops->get_rxnfc) {
1084 ret = p->ethtool_ops->get_rxnfc(p, nfc, rule_locs);
1085 if (ret == -EOPNOTSUPP)
1092 int bcm_sf2_set_rxnfc(struct dsa_switch *ds, int port,
1093 struct ethtool_rxnfc *nfc)
1095 struct net_device *p = ds->ports[port].cpu_dp->master;
1096 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1099 mutex_lock(&priv->cfp.lock);
1102 case ETHTOOL_SRXCLSRLINS:
1103 ret = bcm_sf2_cfp_rule_set(ds, port, &nfc->fs);
1106 case ETHTOOL_SRXCLSRLDEL:
1107 ret = bcm_sf2_cfp_rule_del(priv, port, nfc->fs.location);
1114 mutex_unlock(&priv->cfp.lock);
1119 /* Pass up the commands to the attached master network device.
1120 * This can fail, so rollback the operation if we need to.
1122 if (p->ethtool_ops->set_rxnfc) {
1123 ret = p->ethtool_ops->set_rxnfc(p, nfc);
1124 if (ret && ret != -EOPNOTSUPP) {
1125 mutex_lock(&priv->cfp.lock);
1126 bcm_sf2_cfp_rule_del(priv, port, nfc->fs.location);
1127 mutex_unlock(&priv->cfp.lock);
1136 int bcm_sf2_cfp_rst(struct bcm_sf2_priv *priv)
1138 unsigned int timeout = 1000;
1141 reg = core_readl(priv, CORE_CFP_ACC);
1143 core_writel(priv, reg, CORE_CFP_ACC);
1146 reg = core_readl(priv, CORE_CFP_ACC);
1147 if (!(reg & TCAM_RESET))
1151 } while (timeout--);
1159 void bcm_sf2_cfp_exit(struct dsa_switch *ds)
1161 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1162 struct cfp_rule *rule, *n;
1164 if (list_empty(&priv->cfp.rules_list))
1167 list_for_each_entry_safe_reverse(rule, n, &priv->cfp.rules_list, next)
1168 bcm_sf2_cfp_rule_del(priv, rule->port, rule->fs.location);
1171 int bcm_sf2_cfp_resume(struct dsa_switch *ds)
1173 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1174 struct cfp_rule *rule;
1178 if (list_empty(&priv->cfp.rules_list))
1181 reg = core_readl(priv, CORE_CFP_CTL_REG);
1182 reg &= ~CFP_EN_MAP_MASK;
1183 core_writel(priv, reg, CORE_CFP_CTL_REG);
1185 ret = bcm_sf2_cfp_rst(priv);
1189 list_for_each_entry(rule, &priv->cfp.rules_list, next) {
1190 ret = bcm_sf2_cfp_rule_remove(priv, rule->port,
1193 dev_err(ds->dev, "failed to remove rule\n");
1197 ret = bcm_sf2_cfp_rule_insert(ds, rule->port, &rule->fs);
1199 dev_err(ds->dev, "failed to restore rule\n");
1207 static const struct bcm_sf2_cfp_stat {
1208 unsigned int offset;
1209 unsigned int ram_loc;
1211 } bcm_sf2_cfp_stats[] = {
1213 .offset = CORE_STAT_GREEN_CNTR,
1214 .ram_loc = GREEN_STAT_RAM,
1218 .offset = CORE_STAT_YELLOW_CNTR,
1219 .ram_loc = YELLOW_STAT_RAM,
1223 .offset = CORE_STAT_RED_CNTR,
1224 .ram_loc = RED_STAT_RAM,
1229 void bcm_sf2_cfp_get_strings(struct dsa_switch *ds, int port,
1230 u32 stringset, uint8_t *data)
1232 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1233 unsigned int s = ARRAY_SIZE(bcm_sf2_cfp_stats);
1234 char buf[ETH_GSTRING_LEN];
1235 unsigned int i, j, iter;
1237 if (stringset != ETH_SS_STATS)
1240 for (i = 1; i < priv->num_cfp_rules; i++) {
1241 for (j = 0; j < s; j++) {
1242 snprintf(buf, sizeof(buf),
1244 i, bcm_sf2_cfp_stats[j].name);
1245 iter = (i - 1) * s + j;
1246 strlcpy(data + iter * ETH_GSTRING_LEN,
1247 buf, ETH_GSTRING_LEN);
1252 void bcm_sf2_cfp_get_ethtool_stats(struct dsa_switch *ds, int port,
1255 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1256 unsigned int s = ARRAY_SIZE(bcm_sf2_cfp_stats);
1257 const struct bcm_sf2_cfp_stat *stat;
1258 unsigned int i, j, iter;
1259 struct cfp_rule *rule;
1262 mutex_lock(&priv->cfp.lock);
1263 for (i = 1; i < priv->num_cfp_rules; i++) {
1264 rule = bcm_sf2_cfp_rule_find(priv, port, i);
1268 for (j = 0; j < s; j++) {
1269 stat = &bcm_sf2_cfp_stats[j];
1271 bcm_sf2_cfp_rule_addr_set(priv, i);
1272 ret = bcm_sf2_cfp_op(priv, stat->ram_loc | OP_SEL_READ);
1276 iter = (i - 1) * s + j;
1277 data[iter] = core_readl(priv, stat->offset);
1281 mutex_unlock(&priv->cfp.lock);
1284 int bcm_sf2_cfp_get_sset_count(struct dsa_switch *ds, int port, int sset)
1286 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1288 if (sset != ETH_SS_STATS)
1291 /* 3 counters per CFP rules */
1292 return (priv->num_cfp_rules - 1) * ARRAY_SIZE(bcm_sf2_cfp_stats);