1
2
3
4
5
6
7
8
9
10
11
12#include <linux/list.h>
13#include <linux/ethtool.h>
14#include <linux/if_ether.h>
15#include <linux/in.h>
16#include <linux/netdevice.h>
17#include <net/dsa.h>
18#include <linux/bitmap.h>
19
20#include "bcm_sf2.h"
21#include "bcm_sf2_regs.h"
22
23struct cfp_udf_slice_layout {
24 u8 slices[UDFS_PER_SLICE];
25 u32 mask_value;
26 u32 base_offset;
27};
28
29struct cfp_udf_layout {
30 struct cfp_udf_slice_layout udfs[UDF_NUM_SLICES];
31};
32
33static const u8 zero_slice[UDFS_PER_SLICE] = { };
34
35
36static const struct cfp_udf_layout udf_tcpip4_layout = {
37 .udfs = {
38 [1] = {
39 .slices = {
40
41 CFG_UDF_EOL2 | 6,
42
43 CFG_UDF_EOL2 | 7,
44
45 CFG_UDF_EOL2 | 8,
46
47 CFG_UDF_EOL2 | 9,
48
49 CFG_UDF_EOL3 | 0,
50
51 CFG_UDF_EOL3 | 1,
52 0, 0, 0
53 },
54 .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
55 .base_offset = CORE_UDF_0_A_0_8_PORT_0 + UDF_SLICE_OFFSET,
56 },
57 },
58};
59
60
61static const struct cfp_udf_layout udf_tcpip6_layout = {
62 .udfs = {
63 [0] = {
64 .slices = {
65
66 CFG_UDF_EOL2 | 4,
67
68 CFG_UDF_EOL2 | 5,
69
70 CFG_UDF_EOL2 | 6,
71
72 CFG_UDF_EOL2 | 7,
73
74 CFG_UDF_EOL2 | 8,
75
76 CFG_UDF_EOL2 | 9,
77
78 CFG_UDF_EOL2 | 10,
79
80 CFG_UDF_EOL2 | 11,
81
82 CFG_UDF_EOL3 | 0,
83 },
84 .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
85 .base_offset = CORE_UDF_0_B_0_8_PORT_0,
86 },
87 [3] = {
88 .slices = {
89
90 CFG_UDF_EOL2 | 12,
91
92 CFG_UDF_EOL2 | 13,
93
94 CFG_UDF_EOL2 | 14,
95
96 CFG_UDF_EOL2 | 15,
97
98 CFG_UDF_EOL2 | 16,
99
100 CFG_UDF_EOL2 | 17,
101
102 CFG_UDF_EOL2 | 18,
103
104 CFG_UDF_EOL2 | 19,
105
106 CFG_UDF_EOL3 | 1,
107 },
108 .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
109 .base_offset = CORE_UDF_0_D_0_11_PORT_0,
110 },
111 },
112};
113
114static inline unsigned int bcm_sf2_get_num_udf_slices(const u8 *layout)
115{
116 unsigned int i, count = 0;
117
118 for (i = 0; i < UDFS_PER_SLICE; i++) {
119 if (layout[i] != 0)
120 count++;
121 }
122
123 return count;
124}
125
126static inline u32 udf_upper_bits(unsigned int num_udf)
127{
128 return GENMASK(num_udf - 1, 0) >> (UDFS_PER_SLICE - 1);
129}
130
131static inline u32 udf_lower_bits(unsigned int num_udf)
132{
133 return (u8)GENMASK(num_udf - 1, 0);
134}
135
136static unsigned int bcm_sf2_get_slice_number(const struct cfp_udf_layout *l,
137 unsigned int start)
138{
139 const struct cfp_udf_slice_layout *slice_layout;
140 unsigned int slice_idx;
141
142 for (slice_idx = start; slice_idx < UDF_NUM_SLICES; slice_idx++) {
143 slice_layout = &l->udfs[slice_idx];
144 if (memcmp(slice_layout->slices, zero_slice,
145 sizeof(zero_slice)))
146 break;
147 }
148
149 return slice_idx;
150}
151
152static void bcm_sf2_cfp_udf_set(struct bcm_sf2_priv *priv,
153 const struct cfp_udf_layout *layout,
154 unsigned int slice_num)
155{
156 u32 offset = layout->udfs[slice_num].base_offset;
157 unsigned int i;
158
159 for (i = 0; i < UDFS_PER_SLICE; i++)
160 core_writel(priv, layout->udfs[slice_num].slices[i],
161 offset + i * 4);
162}
163
164static int bcm_sf2_cfp_op(struct bcm_sf2_priv *priv, unsigned int op)
165{
166 unsigned int timeout = 1000;
167 u32 reg;
168
169 reg = core_readl(priv, CORE_CFP_ACC);
170 reg &= ~(OP_SEL_MASK | RAM_SEL_MASK);
171 reg |= OP_STR_DONE | op;
172 core_writel(priv, reg, CORE_CFP_ACC);
173
174 do {
175 reg = core_readl(priv, CORE_CFP_ACC);
176 if (!(reg & OP_STR_DONE))
177 break;
178
179 cpu_relax();
180 } while (timeout--);
181
182 if (!timeout)
183 return -ETIMEDOUT;
184
185 return 0;
186}
187
188static inline void bcm_sf2_cfp_rule_addr_set(struct bcm_sf2_priv *priv,
189 unsigned int addr)
190{
191 u32 reg;
192
193 WARN_ON(addr >= priv->num_cfp_rules);
194
195 reg = core_readl(priv, CORE_CFP_ACC);
196 reg &= ~(XCESS_ADDR_MASK << XCESS_ADDR_SHIFT);
197 reg |= addr << XCESS_ADDR_SHIFT;
198 core_writel(priv, reg, CORE_CFP_ACC);
199}
200
201static inline unsigned int bcm_sf2_cfp_rule_size(struct bcm_sf2_priv *priv)
202{
203
204 return priv->num_cfp_rules - 1;
205}
206
207static int bcm_sf2_cfp_act_pol_set(struct bcm_sf2_priv *priv,
208 unsigned int rule_index,
209 unsigned int port_num,
210 unsigned int queue_num,
211 bool fwd_map_change)
212{
213 int ret;
214 u32 reg;
215
216
217
218
219 if (fwd_map_change)
220 reg = CHANGE_FWRD_MAP_IB_REP_ARL |
221 BIT(port_num + DST_MAP_IB_SHIFT) |
222 CHANGE_TC | queue_num << NEW_TC_SHIFT;
223 else
224 reg = 0;
225
226 core_writel(priv, reg, CORE_ACT_POL_DATA0);
227
228
229 core_writel(priv, rule_index << CHAIN_ID_SHIFT, CORE_ACT_POL_DATA1);
230
231 core_writel(priv, 0, CORE_ACT_POL_DATA2);
232
233
234 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | ACT_POL_RAM);
235 if (ret) {
236 pr_err("Policer entry at %d failed\n", rule_index);
237 return ret;
238 }
239
240
241 core_writel(priv, POLICER_MODE_DISABLE, CORE_RATE_METER0);
242
243
244 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | RATE_METER_RAM);
245 if (ret) {
246 pr_err("Meter entry at %d failed\n", rule_index);
247 return ret;
248 }
249
250 return 0;
251}
252
253static void bcm_sf2_cfp_slice_ipv4(struct bcm_sf2_priv *priv,
254 struct ethtool_tcpip4_spec *v4_spec,
255 unsigned int slice_num,
256 bool mask)
257{
258 u32 reg, offset;
259
260
261
262
263
264 reg = 0;
265 if (mask)
266 offset = CORE_CFP_MASK_PORT(4);
267 else
268 offset = CORE_CFP_DATA_PORT(4);
269 core_writel(priv, reg, offset);
270
271
272
273
274
275 reg = be16_to_cpu(v4_spec->pdst) >> 8;
276 if (mask)
277 offset = CORE_CFP_MASK_PORT(3);
278 else
279 offset = CORE_CFP_DATA_PORT(3);
280 core_writel(priv, reg, offset);
281
282
283
284
285
286 reg = (be16_to_cpu(v4_spec->pdst) & 0xff) << 24 |
287 (u32)be16_to_cpu(v4_spec->psrc) << 8 |
288 (be32_to_cpu(v4_spec->ip4dst) & 0x0000ff00) >> 8;
289 if (mask)
290 offset = CORE_CFP_MASK_PORT(2);
291 else
292 offset = CORE_CFP_DATA_PORT(2);
293 core_writel(priv, reg, offset);
294
295
296
297
298
299 reg = (u32)(be32_to_cpu(v4_spec->ip4dst) & 0xff) << 24 |
300 (u32)(be32_to_cpu(v4_spec->ip4dst) >> 16) << 8 |
301 (be32_to_cpu(v4_spec->ip4src) & 0x0000ff00) >> 8;
302 if (mask)
303 offset = CORE_CFP_MASK_PORT(1);
304 else
305 offset = CORE_CFP_DATA_PORT(1);
306 core_writel(priv, reg, offset);
307
308
309
310
311
312
313
314 reg = (u32)(be32_to_cpu(v4_spec->ip4src) & 0xff) << 24 |
315 (u32)(be32_to_cpu(v4_spec->ip4src) >> 16) << 8 |
316 SLICE_NUM(slice_num) | SLICE_VALID;
317 if (mask)
318 offset = CORE_CFP_MASK_PORT(0);
319 else
320 offset = CORE_CFP_DATA_PORT(0);
321 core_writel(priv, reg, offset);
322}
323
324static int bcm_sf2_cfp_ipv4_rule_set(struct bcm_sf2_priv *priv, int port,
325 unsigned int port_num,
326 unsigned int queue_num,
327 struct ethtool_rx_flow_spec *fs)
328{
329 struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
330 const struct cfp_udf_layout *layout;
331 unsigned int slice_num, rule_index;
332 u8 ip_proto, ip_frag;
333 u8 num_udf;
334 u32 reg;
335 int ret;
336
337 switch (fs->flow_type & ~FLOW_EXT) {
338 case TCP_V4_FLOW:
339 ip_proto = IPPROTO_TCP;
340 v4_spec = &fs->h_u.tcp_ip4_spec;
341 v4_m_spec = &fs->m_u.tcp_ip4_spec;
342 break;
343 case UDP_V4_FLOW:
344 ip_proto = IPPROTO_UDP;
345 v4_spec = &fs->h_u.udp_ip4_spec;
346 v4_m_spec = &fs->m_u.udp_ip4_spec;
347 break;
348 default:
349 return -EINVAL;
350 }
351
352 ip_frag = be32_to_cpu(fs->m_ext.data[0]);
353
354
355 if (fs->location == RX_CLS_LOC_ANY)
356 rule_index = find_first_zero_bit(priv->cfp.used,
357 priv->num_cfp_rules);
358 else
359 rule_index = fs->location;
360
361 if (rule_index > bcm_sf2_cfp_rule_size(priv))
362 return -ENOSPC;
363
364 layout = &udf_tcpip4_layout;
365
366 slice_num = bcm_sf2_get_slice_number(layout, 0);
367 if (slice_num == UDF_NUM_SLICES)
368 return -EINVAL;
369
370 num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
371
372
373 bcm_sf2_cfp_udf_set(priv, layout, slice_num);
374
375
376 core_writel(priv, BIT(port), CORE_CFP_DATA_PORT(7));
377
378
379 core_writel(priv, 0xff, CORE_CFP_MASK_PORT(7));
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395 core_writel(priv, v4_spec->tos << IPTOS_SHIFT |
396 ip_proto << IPPROTO_SHIFT | ip_frag << IP_FRAG_SHIFT |
397 udf_upper_bits(num_udf),
398 CORE_CFP_DATA_PORT(6));
399
400
401 core_writel(priv, layout->udfs[slice_num].mask_value |
402 udf_upper_bits(num_udf), CORE_CFP_MASK_PORT(6));
403
404
405
406
407
408 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_DATA_PORT(5));
409
410
411 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_MASK_PORT(5));
412
413
414 bcm_sf2_cfp_slice_ipv4(priv, v4_spec, slice_num, false);
415 bcm_sf2_cfp_slice_ipv4(priv, v4_m_spec, SLICE_NUM_MASK, true);
416
417
418 bcm_sf2_cfp_rule_addr_set(priv, rule_index);
419
420 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
421 if (ret) {
422 pr_err("TCAM entry at addr %d failed\n", rule_index);
423 return ret;
424 }
425
426
427 ret = bcm_sf2_cfp_act_pol_set(priv, rule_index, port_num,
428 queue_num, true);
429 if (ret)
430 return ret;
431
432
433 reg = core_readl(priv, CORE_CFP_CTL_REG);
434 reg |= BIT(port);
435 core_writel(priv, reg, CORE_CFP_CTL_REG);
436
437
438 set_bit(rule_index, priv->cfp.used);
439 set_bit(rule_index, priv->cfp.unique);
440 fs->location = rule_index;
441
442 return 0;
443}
444
445static void bcm_sf2_cfp_slice_ipv6(struct bcm_sf2_priv *priv,
446 const __be32 *ip6_addr, const __be16 port,
447 unsigned int slice_num,
448 bool mask)
449{
450 u32 reg, tmp, val, offset;
451
452
453
454
455
456 reg = be32_to_cpu(ip6_addr[3]);
457 val = (u32)be16_to_cpu(port) << 8 | ((reg >> 8) & 0xff);
458 if (mask)
459 offset = CORE_CFP_MASK_PORT(4);
460 else
461 offset = CORE_CFP_DATA_PORT(4);
462 core_writel(priv, val, offset);
463
464
465
466
467
468 tmp = be32_to_cpu(ip6_addr[2]);
469 val = (u32)(reg & 0xff) << 24 | (u32)(reg >> 16) << 8 |
470 ((tmp >> 8) & 0xff);
471 if (mask)
472 offset = CORE_CFP_MASK_PORT(3);
473 else
474 offset = CORE_CFP_DATA_PORT(3);
475 core_writel(priv, val, offset);
476
477
478
479
480
481 reg = be32_to_cpu(ip6_addr[1]);
482 val = (u32)(tmp & 0xff) << 24 | (u32)(tmp >> 16) << 8 |
483 ((reg >> 8) & 0xff);
484 if (mask)
485 offset = CORE_CFP_MASK_PORT(2);
486 else
487 offset = CORE_CFP_DATA_PORT(2);
488 core_writel(priv, val, offset);
489
490
491
492
493
494 tmp = be32_to_cpu(ip6_addr[0]);
495 val = (u32)(reg & 0xff) << 24 | (u32)(reg >> 16) << 8 |
496 ((tmp >> 8) & 0xff);
497 if (mask)
498 offset = CORE_CFP_MASK_PORT(1);
499 else
500 offset = CORE_CFP_DATA_PORT(1);
501 core_writel(priv, val, offset);
502
503
504
505
506
507
508
509 reg = (u32)(tmp & 0xff) << 24 | (u32)(tmp >> 16) << 8 |
510 SLICE_NUM(slice_num) | SLICE_VALID;
511 if (mask)
512 offset = CORE_CFP_MASK_PORT(0);
513 else
514 offset = CORE_CFP_DATA_PORT(0);
515 core_writel(priv, reg, offset);
516}
517
518static int bcm_sf2_cfp_ipv6_rule_set(struct bcm_sf2_priv *priv, int port,
519 unsigned int port_num,
520 unsigned int queue_num,
521 struct ethtool_rx_flow_spec *fs)
522{
523 struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
524 unsigned int slice_num, rule_index[2];
525 const struct cfp_udf_layout *layout;
526 u8 ip_proto, ip_frag;
527 int ret = 0;
528 u8 num_udf;
529 u32 reg;
530
531 switch (fs->flow_type & ~FLOW_EXT) {
532 case TCP_V6_FLOW:
533 ip_proto = IPPROTO_TCP;
534 v6_spec = &fs->h_u.tcp_ip6_spec;
535 v6_m_spec = &fs->m_u.tcp_ip6_spec;
536 break;
537 case UDP_V6_FLOW:
538 ip_proto = IPPROTO_UDP;
539 v6_spec = &fs->h_u.udp_ip6_spec;
540 v6_m_spec = &fs->m_u.udp_ip6_spec;
541 break;
542 default:
543 return -EINVAL;
544 }
545
546 ip_frag = be32_to_cpu(fs->m_ext.data[0]);
547
548 layout = &udf_tcpip6_layout;
549 slice_num = bcm_sf2_get_slice_number(layout, 0);
550 if (slice_num == UDF_NUM_SLICES)
551 return -EINVAL;
552
553 num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
554
555
556
557
558
559
560
561
562
563
564
565
566
567 if (fs->location == RX_CLS_LOC_ANY)
568 rule_index[1] = find_first_zero_bit(priv->cfp.used,
569 priv->num_cfp_rules);
570 else
571 rule_index[1] = fs->location;
572 if (rule_index[1] > bcm_sf2_cfp_rule_size(priv))
573 return -ENOSPC;
574
575
576
577
578 set_bit(rule_index[1], priv->cfp.used);
579
580 rule_index[0] = find_first_zero_bit(priv->cfp.used,
581 priv->num_cfp_rules);
582 if (rule_index[0] > bcm_sf2_cfp_rule_size(priv)) {
583 ret = -ENOSPC;
584 goto out_err;
585 }
586
587
588 bcm_sf2_cfp_udf_set(priv, layout, slice_num);
589
590
591 core_writel(priv, BIT(port), CORE_CFP_DATA_PORT(7));
592
593
594 core_writel(priv, 0xff, CORE_CFP_MASK_PORT(7));
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610 reg = 1 << L3_FRAMING_SHIFT | ip_proto << IPPROTO_SHIFT |
611 ip_frag << IP_FRAG_SHIFT | udf_upper_bits(num_udf);
612 core_writel(priv, reg, CORE_CFP_DATA_PORT(6));
613
614
615
616
617 reg = layout->udfs[slice_num].mask_value | udf_upper_bits(num_udf);
618 core_writel(priv, reg, CORE_CFP_MASK_PORT(6));
619
620
621
622
623
624 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_DATA_PORT(5));
625
626
627 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_MASK_PORT(5));
628
629
630 bcm_sf2_cfp_slice_ipv6(priv, v6_spec->ip6src, v6_spec->psrc,
631 slice_num, false);
632 bcm_sf2_cfp_slice_ipv6(priv, v6_m_spec->ip6src, v6_m_spec->psrc,
633 SLICE_NUM_MASK, true);
634
635
636 bcm_sf2_cfp_rule_addr_set(priv, rule_index[0]);
637
638 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
639 if (ret) {
640 pr_err("TCAM entry at addr %d failed\n", rule_index[0]);
641 goto out_err;
642 }
643
644
645 ret = bcm_sf2_cfp_act_pol_set(priv, rule_index[0], port_num,
646 queue_num, false);
647 if (ret)
648 goto out_err;
649
650
651 slice_num = bcm_sf2_get_slice_number(layout, slice_num + 1);
652 if (slice_num == UDF_NUM_SLICES) {
653 ret = -EINVAL;
654 goto out_err;
655 }
656
657 num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
658
659
660 bcm_sf2_cfp_udf_set(priv, layout, slice_num);
661
662
663
664
665 core_writel(priv, 0, CORE_CFP_DATA_PORT(7));
666 core_writel(priv, 0, CORE_CFP_MASK_PORT(7));
667
668
669
670
671
672
673
674
675 reg = rule_index[0] << 24 | udf_upper_bits(num_udf) << 16 |
676 udf_lower_bits(num_udf) << 8;
677 core_writel(priv, reg, CORE_CFP_DATA_PORT(6));
678
679
680 reg = XCESS_ADDR_MASK << 24 | udf_upper_bits(num_udf) << 16 |
681 udf_lower_bits(num_udf) << 8;
682 core_writel(priv, reg, CORE_CFP_MASK_PORT(6));
683
684
685 core_writel(priv, 0, CORE_CFP_DATA_PORT(5));
686
687
688 core_writel(priv, 0, CORE_CFP_MASK_PORT(5));
689
690 bcm_sf2_cfp_slice_ipv6(priv, v6_spec->ip6dst, v6_spec->pdst, slice_num,
691 false);
692 bcm_sf2_cfp_slice_ipv6(priv, v6_m_spec->ip6dst, v6_m_spec->pdst,
693 SLICE_NUM_MASK, true);
694
695
696 bcm_sf2_cfp_rule_addr_set(priv, rule_index[1]);
697
698 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
699 if (ret) {
700 pr_err("TCAM entry at addr %d failed\n", rule_index[1]);
701 goto out_err;
702 }
703
704
705
706
707 ret = bcm_sf2_cfp_act_pol_set(priv, rule_index[1], port_num,
708 queue_num, true);
709 if (ret)
710 goto out_err;
711
712
713 reg = core_readl(priv, CORE_CFP_CTL_REG);
714 reg |= BIT(port);
715 core_writel(priv, reg, CORE_CFP_CTL_REG);
716
717
718
719
720 set_bit(rule_index[0], priv->cfp.used);
721 set_bit(rule_index[1], priv->cfp.unique);
722 fs->location = rule_index[1];
723
724 return ret;
725
726out_err:
727 clear_bit(rule_index[1], priv->cfp.used);
728 return ret;
729}
730
731static int bcm_sf2_cfp_rule_set(struct dsa_switch *ds, int port,
732 struct ethtool_rx_flow_spec *fs)
733{
734 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
735 unsigned int queue_num, port_num;
736 int ret = -EINVAL;
737
738
739 if ((fs->flow_type & FLOW_EXT) && (fs->m_ext.vlan_etype ||
740 fs->m_ext.data[1]))
741 return -EINVAL;
742
743 if (fs->location != RX_CLS_LOC_ANY &&
744 test_bit(fs->location, priv->cfp.used))
745 return -EBUSY;
746
747 if (fs->location != RX_CLS_LOC_ANY &&
748 fs->location > bcm_sf2_cfp_rule_size(priv))
749 return -EINVAL;
750
751
752
753
754
755 port_num = fs->ring_cookie / SF2_NUM_EGRESS_QUEUES;
756
757 if (fs->ring_cookie == RX_CLS_FLOW_DISC ||
758 !dsa_is_user_port(ds, port_num) ||
759 port_num >= priv->hw_params.num_ports)
760 return -EINVAL;
761
762
763
764
765 queue_num = fs->ring_cookie % SF2_NUM_EGRESS_QUEUES;
766 if (port_num >= 7)
767 port_num -= 1;
768
769 switch (fs->flow_type & ~FLOW_EXT) {
770 case TCP_V4_FLOW:
771 case UDP_V4_FLOW:
772 ret = bcm_sf2_cfp_ipv4_rule_set(priv, port, port_num,
773 queue_num, fs);
774 break;
775 case TCP_V6_FLOW:
776 case UDP_V6_FLOW:
777 ret = bcm_sf2_cfp_ipv6_rule_set(priv, port, port_num,
778 queue_num, fs);
779 break;
780 default:
781 break;
782 }
783
784 return ret;
785}
786
787static int bcm_sf2_cfp_rule_del_one(struct bcm_sf2_priv *priv, int port,
788 u32 loc, u32 *next_loc)
789{
790 int ret;
791 u32 reg;
792
793
794 bcm_sf2_cfp_rule_addr_set(priv, loc);
795
796 ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
797 if (ret)
798 return ret;
799
800
801
802
803
804 reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
805 if (next_loc)
806 *next_loc = (reg >> 24) & CHAIN_ID_MASK;
807
808
809 reg = core_readl(priv, CORE_CFP_DATA_PORT(0));
810 reg &= ~SLICE_VALID;
811 core_writel(priv, reg, CORE_CFP_DATA_PORT(0));
812
813
814 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
815 if (ret)
816 return ret;
817
818 clear_bit(loc, priv->cfp.used);
819 clear_bit(loc, priv->cfp.unique);
820
821 return 0;
822}
823
824static int bcm_sf2_cfp_rule_del(struct bcm_sf2_priv *priv, int port,
825 u32 loc)
826{
827 u32 next_loc = 0;
828 int ret;
829
830
831
832
833
834 if (!test_bit(loc, priv->cfp.unique) || loc == 0)
835 return -EINVAL;
836
837 ret = bcm_sf2_cfp_rule_del_one(priv, port, loc, &next_loc);
838 if (ret)
839 return ret;
840
841
842 if (next_loc)
843 ret = bcm_sf2_cfp_rule_del_one(priv, port, next_loc, NULL);
844
845 return ret;
846}
847
848static void bcm_sf2_invert_masks(struct ethtool_rx_flow_spec *flow)
849{
850 unsigned int i;
851
852 for (i = 0; i < sizeof(flow->m_u); i++)
853 flow->m_u.hdata[i] ^= 0xff;
854
855 flow->m_ext.vlan_etype ^= cpu_to_be16(~0);
856 flow->m_ext.vlan_tci ^= cpu_to_be16(~0);
857 flow->m_ext.data[0] ^= cpu_to_be32(~0);
858 flow->m_ext.data[1] ^= cpu_to_be32(~0);
859}
860
861static int bcm_sf2_cfp_unslice_ipv4(struct bcm_sf2_priv *priv,
862 struct ethtool_tcpip4_spec *v4_spec,
863 bool mask)
864{
865 u32 reg, offset, ipv4;
866 u16 src_dst_port;
867
868 if (mask)
869 offset = CORE_CFP_MASK_PORT(3);
870 else
871 offset = CORE_CFP_DATA_PORT(3);
872
873 reg = core_readl(priv, offset);
874
875 src_dst_port = reg << 8;
876
877 if (mask)
878 offset = CORE_CFP_MASK_PORT(2);
879 else
880 offset = CORE_CFP_DATA_PORT(2);
881
882 reg = core_readl(priv, offset);
883
884 src_dst_port |= (reg >> 24);
885
886 v4_spec->pdst = cpu_to_be16(src_dst_port);
887 v4_spec->psrc = cpu_to_be16((u16)(reg >> 8));
888
889
890 ipv4 = (reg & 0xff) << 8;
891
892 if (mask)
893 offset = CORE_CFP_MASK_PORT(1);
894 else
895 offset = CORE_CFP_DATA_PORT(1);
896
897 reg = core_readl(priv, offset);
898
899 ipv4 |= ((reg >> 8) & 0xffff) << 16;
900
901 ipv4 |= (reg >> 24) & 0xff;
902 v4_spec->ip4dst = cpu_to_be32(ipv4);
903
904
905 ipv4 = (reg & 0xff) << 8;
906
907 if (mask)
908 offset = CORE_CFP_MASK_PORT(0);
909 else
910 offset = CORE_CFP_DATA_PORT(0);
911 reg = core_readl(priv, offset);
912
913
914
915
916
917 if (!mask && !(reg & SLICE_VALID))
918 return -EINVAL;
919
920
921 ipv4 |= (reg >> 24) & 0xff;
922
923 ipv4 |= ((reg >> 8) & 0xffff) << 16;
924 v4_spec->ip4src = cpu_to_be32(ipv4);
925
926 return 0;
927}
928
929static int bcm_sf2_cfp_ipv4_rule_get(struct bcm_sf2_priv *priv, int port,
930 struct ethtool_rx_flow_spec *fs)
931{
932 struct ethtool_tcpip4_spec *v4_spec = NULL, *v4_m_spec = NULL;
933 u32 reg;
934 int ret;
935
936 reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
937
938 switch ((reg & IPPROTO_MASK) >> IPPROTO_SHIFT) {
939 case IPPROTO_TCP:
940 fs->flow_type = TCP_V4_FLOW;
941 v4_spec = &fs->h_u.tcp_ip4_spec;
942 v4_m_spec = &fs->m_u.tcp_ip4_spec;
943 break;
944 case IPPROTO_UDP:
945 fs->flow_type = UDP_V4_FLOW;
946 v4_spec = &fs->h_u.udp_ip4_spec;
947 v4_m_spec = &fs->m_u.udp_ip4_spec;
948 break;
949 default:
950 return -EINVAL;
951 }
952
953 fs->m_ext.data[0] = cpu_to_be32((reg >> IP_FRAG_SHIFT) & 1);
954 v4_spec->tos = (reg >> IPTOS_SHIFT) & IPTOS_MASK;
955
956 ret = bcm_sf2_cfp_unslice_ipv4(priv, v4_spec, false);
957 if (ret)
958 return ret;
959
960 return bcm_sf2_cfp_unslice_ipv4(priv, v4_m_spec, true);
961}
962
963static int bcm_sf2_cfp_unslice_ipv6(struct bcm_sf2_priv *priv,
964 __be32 *ip6_addr, __be16 *port,
965 bool mask)
966{
967 u32 reg, tmp, offset;
968
969
970
971
972
973 if (mask)
974 offset = CORE_CFP_MASK_PORT(4);
975 else
976 offset = CORE_CFP_DATA_PORT(4);
977 reg = core_readl(priv, offset);
978 *port = cpu_to_be32(reg) >> 8;
979 tmp = (u32)(reg & 0xff) << 8;
980
981
982
983
984
985 if (mask)
986 offset = CORE_CFP_MASK_PORT(3);
987 else
988 offset = CORE_CFP_DATA_PORT(3);
989 reg = core_readl(priv, offset);
990 tmp |= (reg >> 24) & 0xff;
991 tmp |= (u32)((reg >> 8) << 16);
992 ip6_addr[3] = cpu_to_be32(tmp);
993 tmp = (u32)(reg & 0xff) << 8;
994
995
996
997
998
999 if (mask)
1000 offset = CORE_CFP_MASK_PORT(2);
1001 else
1002 offset = CORE_CFP_DATA_PORT(2);
1003 reg = core_readl(priv, offset);
1004 tmp |= (reg >> 24) & 0xff;
1005 tmp |= (u32)((reg >> 8) << 16);
1006 ip6_addr[2] = cpu_to_be32(tmp);
1007 tmp = (u32)(reg & 0xff) << 8;
1008
1009
1010
1011
1012
1013 if (mask)
1014 offset = CORE_CFP_MASK_PORT(1);
1015 else
1016 offset = CORE_CFP_DATA_PORT(1);
1017 reg = core_readl(priv, offset);
1018 tmp |= (reg >> 24) & 0xff;
1019 tmp |= (u32)((reg >> 8) << 16);
1020 ip6_addr[1] = cpu_to_be32(tmp);
1021 tmp = (u32)(reg & 0xff) << 8;
1022
1023
1024
1025
1026
1027
1028
1029 if (mask)
1030 offset = CORE_CFP_MASK_PORT(0);
1031 else
1032 offset = CORE_CFP_DATA_PORT(0);
1033 reg = core_readl(priv, offset);
1034 tmp |= (reg >> 24) & 0xff;
1035 tmp |= (u32)((reg >> 8) << 16);
1036 ip6_addr[0] = cpu_to_be32(tmp);
1037
1038 if (!mask && !(reg & SLICE_VALID))
1039 return -EINVAL;
1040
1041 return 0;
1042}
1043
1044static int bcm_sf2_cfp_ipv6_rule_get(struct bcm_sf2_priv *priv, int port,
1045 struct ethtool_rx_flow_spec *fs,
1046 u32 next_loc)
1047{
1048 struct ethtool_tcpip6_spec *v6_spec = NULL, *v6_m_spec = NULL;
1049 u32 reg;
1050 int ret;
1051
1052
1053
1054
1055 v6_spec = &fs->h_u.tcp_ip6_spec;
1056 v6_m_spec = &fs->m_u.tcp_ip6_spec;
1057
1058
1059 ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_spec->ip6dst, &v6_spec->pdst,
1060 false);
1061 if (ret)
1062 return ret;
1063
1064 ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_m_spec->ip6dst,
1065 &v6_m_spec->pdst, true);
1066 if (ret)
1067 return ret;
1068
1069
1070
1071
1072
1073 (void)core_readl(priv, CORE_CFP_DATA_PORT(7));
1074
1075
1076
1077
1078 bcm_sf2_cfp_rule_addr_set(priv, next_loc);
1079 ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
1080 if (ret)
1081 return ret;
1082
1083 reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
1084
1085 switch ((reg & IPPROTO_MASK) >> IPPROTO_SHIFT) {
1086 case IPPROTO_TCP:
1087 fs->flow_type = TCP_V6_FLOW;
1088 break;
1089 case IPPROTO_UDP:
1090 fs->flow_type = UDP_V6_FLOW;
1091 break;
1092 default:
1093 return -EINVAL;
1094 }
1095
1096 ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_spec->ip6src, &v6_spec->psrc,
1097 false);
1098 if (ret)
1099 return ret;
1100
1101 return bcm_sf2_cfp_unslice_ipv6(priv, v6_m_spec->ip6src,
1102 &v6_m_spec->psrc, true);
1103}
1104
1105static int bcm_sf2_cfp_rule_get(struct bcm_sf2_priv *priv, int port,
1106 struct ethtool_rxnfc *nfc)
1107{
1108 u32 reg, ipv4_or_chain_id;
1109 unsigned int queue_num;
1110 int ret;
1111
1112 bcm_sf2_cfp_rule_addr_set(priv, nfc->fs.location);
1113
1114 ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | ACT_POL_RAM);
1115 if (ret)
1116 return ret;
1117
1118 reg = core_readl(priv, CORE_ACT_POL_DATA0);
1119
1120 ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
1121 if (ret)
1122 return ret;
1123
1124
1125 nfc->fs.ring_cookie = fls((reg >> DST_MAP_IB_SHIFT) &
1126 DST_MAP_IB_MASK) - 1;
1127
1128
1129 if (nfc->fs.ring_cookie >= 6)
1130 nfc->fs.ring_cookie++;
1131 nfc->fs.ring_cookie *= SF2_NUM_EGRESS_QUEUES;
1132
1133
1134 queue_num = (reg >> NEW_TC_SHIFT) & NEW_TC_MASK;
1135 nfc->fs.ring_cookie += queue_num;
1136
1137
1138 reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
1139
1140
1141
1142
1143
1144 ipv4_or_chain_id = (reg >> L3_FRAMING_SHIFT) & 0xff;
1145 if (ipv4_or_chain_id == 0)
1146 ret = bcm_sf2_cfp_ipv4_rule_get(priv, port, &nfc->fs);
1147 else
1148 ret = bcm_sf2_cfp_ipv6_rule_get(priv, port, &nfc->fs,
1149 ipv4_or_chain_id);
1150 if (ret)
1151 return ret;
1152
1153
1154
1155
1156 reg = core_readl(priv, CORE_CFP_DATA_PORT(7));
1157 if (!(reg & 1 << port))
1158 return -EINVAL;
1159
1160 bcm_sf2_invert_masks(&nfc->fs);
1161
1162
1163 nfc->data = bcm_sf2_cfp_rule_size(priv);
1164
1165 return 0;
1166}
1167
1168
1169static int bcm_sf2_cfp_rule_get_all(struct bcm_sf2_priv *priv,
1170 int port, struct ethtool_rxnfc *nfc,
1171 u32 *rule_locs)
1172{
1173 unsigned int index = 1, rules_cnt = 0;
1174
1175 for_each_set_bit_from(index, priv->cfp.unique, priv->num_cfp_rules) {
1176 rule_locs[rules_cnt] = index;
1177 rules_cnt++;
1178 }
1179
1180
1181 nfc->data = bcm_sf2_cfp_rule_size(priv);
1182 nfc->rule_cnt = rules_cnt;
1183
1184 return 0;
1185}
1186
1187int bcm_sf2_get_rxnfc(struct dsa_switch *ds, int port,
1188 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1189{
1190 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1191 int ret = 0;
1192
1193 mutex_lock(&priv->cfp.lock);
1194
1195 switch (nfc->cmd) {
1196 case ETHTOOL_GRXCLSRLCNT:
1197
1198 nfc->rule_cnt = bitmap_weight(priv->cfp.unique,
1199 priv->num_cfp_rules) - 1;
1200
1201 nfc->data |= RX_CLS_LOC_SPECIAL;
1202 break;
1203 case ETHTOOL_GRXCLSRULE:
1204 ret = bcm_sf2_cfp_rule_get(priv, port, nfc);
1205 break;
1206 case ETHTOOL_GRXCLSRLALL:
1207 ret = bcm_sf2_cfp_rule_get_all(priv, port, nfc, rule_locs);
1208 break;
1209 default:
1210 ret = -EOPNOTSUPP;
1211 break;
1212 }
1213
1214 mutex_unlock(&priv->cfp.lock);
1215
1216 return ret;
1217}
1218
1219int bcm_sf2_set_rxnfc(struct dsa_switch *ds, int port,
1220 struct ethtool_rxnfc *nfc)
1221{
1222 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1223 int ret = 0;
1224
1225 mutex_lock(&priv->cfp.lock);
1226
1227 switch (nfc->cmd) {
1228 case ETHTOOL_SRXCLSRLINS:
1229 ret = bcm_sf2_cfp_rule_set(ds, port, &nfc->fs);
1230 break;
1231
1232 case ETHTOOL_SRXCLSRLDEL:
1233 ret = bcm_sf2_cfp_rule_del(priv, port, nfc->fs.location);
1234 break;
1235 default:
1236 ret = -EOPNOTSUPP;
1237 break;
1238 }
1239
1240 mutex_unlock(&priv->cfp.lock);
1241
1242 return ret;
1243}
1244
1245int bcm_sf2_cfp_rst(struct bcm_sf2_priv *priv)
1246{
1247 unsigned int timeout = 1000;
1248 u32 reg;
1249
1250 reg = core_readl(priv, CORE_CFP_ACC);
1251 reg |= TCAM_RESET;
1252 core_writel(priv, reg, CORE_CFP_ACC);
1253
1254 do {
1255 reg = core_readl(priv, CORE_CFP_ACC);
1256 if (!(reg & TCAM_RESET))
1257 break;
1258
1259 cpu_relax();
1260 } while (timeout--);
1261
1262 if (!timeout)
1263 return -ETIMEDOUT;
1264
1265 return 0;
1266}
1267