1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2019 NXP */
6 #include <net/pkt_sched.h>
7 #include <linux/math64.h>
8 #include <linux/refcount.h>
9 #include <net/pkt_cls.h>
10 #include <net/tc_act/tc_gate.h>
12 static u16 enetc_get_max_gcl_len(struct enetc_hw *hw)
14 return enetc_rd(hw, ENETC_QBV_PTGCAPR_OFFSET)
15 & ENETC_QBV_MAX_GCL_LEN_MASK;
18 void enetc_sched_speed_set(struct enetc_ndev_priv *priv, int speed)
20 u32 old_speed = priv->speed;
23 if (speed == old_speed)
28 pspeed = ENETC_PMR_PSPEED_1000M;
31 pspeed = ENETC_PMR_PSPEED_2500M;
34 pspeed = ENETC_PMR_PSPEED_100M;
38 pspeed = ENETC_PMR_PSPEED_10M;
42 enetc_port_wr(&priv->si->hw, ENETC_PMR,
43 (enetc_port_rd(&priv->si->hw, ENETC_PMR)
44 & (~ENETC_PMR_PSPEED_MASK))
48 static int enetc_setup_taprio(struct net_device *ndev,
49 struct tc_taprio_qopt_offload *admin_conf)
51 struct enetc_ndev_priv *priv = netdev_priv(ndev);
52 struct enetc_cbd cbd = {.cmd = 0};
53 struct tgs_gcl_conf *gcl_config;
54 struct tgs_gcl_data *gcl_data;
64 if (admin_conf->num_entries > enetc_get_max_gcl_len(&priv->si->hw))
66 gcl_len = admin_conf->num_entries;
68 tge = enetc_rd(&priv->si->hw, ENETC_QBV_PTGCR_OFFSET);
69 if (!admin_conf->enable) {
70 enetc_wr(&priv->si->hw,
71 ENETC_QBV_PTGCR_OFFSET,
72 tge & (~ENETC_QBV_TGE));
76 if (admin_conf->cycle_time > U32_MAX ||
77 admin_conf->cycle_time_extension > U32_MAX)
80 /* Configure the (administrative) gate control list using the
81 * control BD descriptor.
83 gcl_config = &cbd.gcl_conf;
85 data_size = struct_size(gcl_data, entry, gcl_len);
86 tmp = enetc_cbd_alloc_data_mem(priv->si, &cbd, data_size,
87 &dma, (void *)&gcl_data);
91 gce = (struct gce *)(gcl_data + 1);
93 /* Set all gates open as default */
94 gcl_config->atc = 0xff;
95 gcl_config->acl_len = cpu_to_le16(gcl_len);
97 gcl_data->btl = cpu_to_le32(lower_32_bits(admin_conf->base_time));
98 gcl_data->bth = cpu_to_le32(upper_32_bits(admin_conf->base_time));
99 gcl_data->ct = cpu_to_le32(admin_conf->cycle_time);
100 gcl_data->cte = cpu_to_le32(admin_conf->cycle_time_extension);
102 for (i = 0; i < gcl_len; i++) {
103 struct tc_taprio_sched_entry *temp_entry;
104 struct gce *temp_gce = gce + i;
106 temp_entry = &admin_conf->entries[i];
108 temp_gce->gate = (u8)temp_entry->gate_mask;
109 temp_gce->period = cpu_to_le32(temp_entry->interval);
112 cbd.status_flags = 0;
114 cbd.cls = BDCR_CMD_PORT_GCL;
115 cbd.status_flags = 0;
117 enetc_wr(&priv->si->hw, ENETC_QBV_PTGCR_OFFSET,
118 tge | ENETC_QBV_TGE);
120 err = enetc_send_cmd(priv->si, &cbd);
122 enetc_wr(&priv->si->hw,
123 ENETC_QBV_PTGCR_OFFSET,
124 tge & (~ENETC_QBV_TGE));
126 enetc_cbd_free_data_mem(priv->si, data_size, tmp, &dma);
131 int enetc_setup_tc_taprio(struct net_device *ndev, void *type_data)
133 struct tc_taprio_qopt_offload *taprio = type_data;
134 struct enetc_ndev_priv *priv = netdev_priv(ndev);
138 /* TSD and Qbv are mutually exclusive in hardware */
139 for (i = 0; i < priv->num_tx_rings; i++)
140 if (priv->tx_ring[i]->tsd_enable)
143 for (i = 0; i < priv->num_tx_rings; i++)
144 enetc_set_bdr_prio(&priv->si->hw,
145 priv->tx_ring[i]->index,
146 taprio->enable ? i : 0);
148 err = enetc_setup_taprio(ndev, taprio);
151 for (i = 0; i < priv->num_tx_rings; i++)
152 enetc_set_bdr_prio(&priv->si->hw,
153 priv->tx_ring[i]->index,
154 taprio->enable ? 0 : i);
159 static u32 enetc_get_cbs_enable(struct enetc_hw *hw, u8 tc)
161 return enetc_port_rd(hw, ENETC_PTCCBSR0(tc)) & ENETC_CBSE;
164 static u8 enetc_get_cbs_bw(struct enetc_hw *hw, u8 tc)
166 return enetc_port_rd(hw, ENETC_PTCCBSR0(tc)) & ENETC_CBS_BW_MASK;
169 int enetc_setup_tc_cbs(struct net_device *ndev, void *type_data)
171 struct enetc_ndev_priv *priv = netdev_priv(ndev);
172 struct tc_cbs_qopt_offload *cbs = type_data;
173 u32 port_transmit_rate = priv->speed;
174 u8 tc_nums = netdev_get_num_tc(ndev);
175 struct enetc_si *si = priv->si;
176 u32 hi_credit_bit, hi_credit_reg;
177 u32 max_interference_size;
178 u32 port_frame_max_size;
180 u8 prio_top, prio_next;
184 prio_top = netdev_get_prio_tc_map(ndev, tc_nums - 1);
185 prio_next = netdev_get_prio_tc_map(ndev, tc_nums - 2);
187 /* Support highest prio and second prio tc in cbs mode */
188 if (tc != prio_top && tc != prio_next)
192 /* Make sure the other TC that are numerically
193 * lower than this TC have been disabled.
195 if (tc == prio_top &&
196 enetc_get_cbs_enable(&si->hw, prio_next)) {
198 "Disable TC%d before disable TC%d\n",
203 enetc_port_wr(&si->hw, ENETC_PTCCBSR1(tc), 0);
204 enetc_port_wr(&si->hw, ENETC_PTCCBSR0(tc), 0);
209 if (cbs->idleslope - cbs->sendslope != port_transmit_rate * 1000L ||
210 cbs->idleslope < 0 || cbs->sendslope > 0)
213 port_frame_max_size = ndev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
215 bw = cbs->idleslope / (port_transmit_rate * 10UL);
217 /* Make sure the other TC that are numerically
218 * higher than this TC have been enabled.
220 if (tc == prio_next) {
221 if (!enetc_get_cbs_enable(&si->hw, prio_top)) {
223 "Enable TC%d first before enable TC%d\n",
224 prio_top, prio_next);
227 bw_sum += enetc_get_cbs_bw(&si->hw, prio_top);
230 if (bw_sum + bw >= 100) {
232 "The sum of all CBS Bandwidth can't exceed 100\n");
236 enetc_port_rd(&si->hw, ENETC_PTCMSDUR(tc));
238 /* For top prio TC, the max_interfrence_size is maxSizedFrame.
240 * For next prio TC, the max_interfrence_size is calculated as below:
242 * max_interference_size = M0 + Ma + Ra * M0 / (R0 - Ra)
244 * - RA: idleSlope for AVB Class A
245 * - R0: port transmit rate
246 * - M0: maximum sized frame for the port
247 * - MA: maximum sized frame for AVB Class A
250 if (tc == prio_top) {
251 max_interference_size = port_frame_max_size * 8;
255 m0 = port_frame_max_size * 8;
256 ma = enetc_port_rd(&si->hw, ENETC_PTCMSDUR(prio_top)) * 8;
257 ra = enetc_get_cbs_bw(&si->hw, prio_top) *
258 port_transmit_rate * 10000ULL;
259 r0 = port_transmit_rate * 1000000ULL;
260 max_interference_size = m0 + ma +
261 (u32)div_u64((u64)ra * m0, r0 - ra);
264 /* hiCredit bits calculate by:
266 * maxSizedFrame * (idleSlope/portTxRate)
268 hi_credit_bit = max_interference_size * bw / 100;
270 /* hiCredit bits to hiCredit register need to calculated as:
272 * (enetClockFrequency / portTransmitRate) * 100
274 hi_credit_reg = (u32)div_u64((ENETC_CLK * 100ULL) * hi_credit_bit,
275 port_transmit_rate * 1000000ULL);
277 enetc_port_wr(&si->hw, ENETC_PTCCBSR1(tc), hi_credit_reg);
279 /* Set bw register and enable this traffic class */
280 enetc_port_wr(&si->hw, ENETC_PTCCBSR0(tc), bw | ENETC_CBSE);
285 int enetc_setup_tc_txtime(struct net_device *ndev, void *type_data)
287 struct enetc_ndev_priv *priv = netdev_priv(ndev);
288 struct tc_etf_qopt_offload *qopt = type_data;
289 u8 tc_nums = netdev_get_num_tc(ndev);
297 if (tc < 0 || tc >= priv->num_tx_rings)
300 /* TSD and Qbv are mutually exclusive in hardware */
301 if (enetc_rd(&priv->si->hw, ENETC_QBV_PTGCR_OFFSET) & ENETC_QBV_TGE)
304 priv->tx_ring[tc]->tsd_enable = qopt->enable;
305 enetc_port_wr(&priv->si->hw, ENETC_PTCTSDR(tc),
306 qopt->enable ? ENETC_TSDE : 0);
312 STREAMID_TYPE_RESERVED = 0,
317 enum streamid_vlan_tagged {
318 STREAMID_VLAN_RESERVED = 0,
319 STREAMID_VLAN_TAGGED,
320 STREAMID_VLAN_UNTAGGED,
324 #define ENETC_PSFP_WILDCARD -1
325 #define HANDLE_OFFSET 100
328 FILTER_ACTION_TYPE_PSFP = BIT(0),
329 FILTER_ACTION_TYPE_ACL = BIT(1),
330 FILTER_ACTION_TYPE_BOTH = GENMASK(1, 0),
333 /* This is for limit output type for input actions */
336 u64 keys; /* include the must needed keys */
337 enum forward_type output;
340 struct psfp_streamfilter_counters {
341 u64 matching_frames_count;
342 u64 passing_frames_count;
343 u64 not_passing_frames_count;
344 u64 passing_sdu_count;
345 u64 not_passing_sdu_count;
346 u64 red_frames_count;
349 struct enetc_streamid {
361 struct enetc_psfp_filter {
369 struct hlist_node node;
372 struct enetc_psfp_gate {
380 struct hlist_node node;
381 struct action_gate_entry entries[];
384 /* Only enable the green color frame now
385 * Will add eir and ebs color blind, couple flag etc when
386 * policing action add more offloading parameters
388 struct enetc_psfp_meter {
393 struct hlist_node node;
396 #define ENETC_PSFP_FLAGS_FMI BIT(0)
398 struct enetc_stream_filter {
399 struct enetc_streamid sid;
404 struct flow_stats stats;
405 struct hlist_node node;
409 unsigned long dev_bitmap;
410 unsigned long *psfp_sfi_bitmap;
411 struct hlist_head stream_list;
412 struct hlist_head psfp_filter_list;
413 struct hlist_head psfp_gate_list;
414 struct hlist_head psfp_meter_list;
415 spinlock_t psfp_lock; /* spinlock for the struct enetc_psfp r/w */
418 static struct actions_fwd enetc_act_fwd[] = {
420 BIT(FLOW_ACTION_GATE),
421 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS),
422 FILTER_ACTION_TYPE_PSFP
425 BIT(FLOW_ACTION_POLICE) |
426 BIT(FLOW_ACTION_GATE),
427 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS),
428 FILTER_ACTION_TYPE_PSFP
430 /* example for ACL actions */
432 BIT(FLOW_ACTION_DROP),
434 FILTER_ACTION_TYPE_ACL
438 static struct enetc_psfp epsfp = {
440 .psfp_sfi_bitmap = NULL,
443 static LIST_HEAD(enetc_block_cb_list);
445 /* Stream Identity Entry Set Descriptor */
446 static int enetc_streamid_hw_set(struct enetc_ndev_priv *priv,
447 struct enetc_streamid *sid,
450 struct enetc_cbd cbd = {.cmd = 0};
451 struct streamid_data *si_data;
452 struct streamid_conf *si_conf;
459 port = enetc_pf_to_port(priv->si->pdev);
463 if (sid->index >= priv->psfp_cap.max_streamid)
466 if (sid->filtertype != STREAMID_TYPE_NULL &&
467 sid->filtertype != STREAMID_TYPE_SMAC)
470 /* Disable operation before enable */
471 cbd.index = cpu_to_le16((u16)sid->index);
472 cbd.cls = BDCR_CMD_STREAM_IDENTIFY;
473 cbd.status_flags = 0;
475 data_size = sizeof(struct streamid_data);
476 tmp = enetc_cbd_alloc_data_mem(priv->si, &cbd, data_size,
477 &dma, (void *)&si_data);
481 eth_broadcast_addr(si_data->dmac);
482 si_data->vid_vidm_tg = (ENETC_CBDR_SID_VID_MASK
483 + ((0x3 << 14) | ENETC_CBDR_SID_VIDM));
485 si_conf = &cbd.sid_set;
486 /* Only one port supported for one entry, set itself */
487 si_conf->iports = cpu_to_le32(1 << port);
488 si_conf->id_type = 1;
489 si_conf->oui[2] = 0x0;
490 si_conf->oui[1] = 0x80;
491 si_conf->oui[0] = 0xC2;
493 err = enetc_send_cmd(priv->si, &cbd);
500 /* Enable the entry overwrite again incase space flushed by hardware */
501 cbd.status_flags = 0;
504 si_conf->stream_handle = cpu_to_le32(sid->handle);
505 si_conf->iports = cpu_to_le32(1 << port);
506 si_conf->id_type = sid->filtertype;
507 si_conf->oui[2] = 0x0;
508 si_conf->oui[1] = 0x80;
509 si_conf->oui[0] = 0xC2;
511 memset(si_data, 0, data_size);
513 /* VIDM default to be 1.
514 * VID Match. If set (b1) then the VID must match, otherwise
515 * any VID is considered a match. VIDM setting is only used
516 * when TG is set to b01.
518 if (si_conf->id_type == STREAMID_TYPE_NULL) {
519 ether_addr_copy(si_data->dmac, sid->dst_mac);
520 si_data->vid_vidm_tg = (sid->vid & ENETC_CBDR_SID_VID_MASK) +
521 ((((u16)(sid->tagged) & 0x3) << 14)
522 | ENETC_CBDR_SID_VIDM);
523 } else if (si_conf->id_type == STREAMID_TYPE_SMAC) {
524 ether_addr_copy(si_data->smac, sid->src_mac);
525 si_data->vid_vidm_tg = (sid->vid & ENETC_CBDR_SID_VID_MASK) +
526 ((((u16)(sid->tagged) & 0x3) << 14)
527 | ENETC_CBDR_SID_VIDM);
530 err = enetc_send_cmd(priv->si, &cbd);
532 enetc_cbd_free_data_mem(priv->si, data_size, tmp, &dma);
537 /* Stream Filter Instance Set Descriptor */
538 static int enetc_streamfilter_hw_set(struct enetc_ndev_priv *priv,
539 struct enetc_psfp_filter *sfi,
542 struct enetc_cbd cbd = {.cmd = 0};
543 struct sfi_conf *sfi_config;
546 port = enetc_pf_to_port(priv->si->pdev);
550 cbd.index = cpu_to_le16(sfi->index);
551 cbd.cls = BDCR_CMD_STREAM_FILTER;
552 cbd.status_flags = 0x80;
553 cbd.length = cpu_to_le16(1);
555 sfi_config = &cbd.sfi_conf;
559 sfi_config->en = 0x80;
561 if (sfi->handle >= 0) {
562 sfi_config->stream_handle =
563 cpu_to_le32(sfi->handle);
564 sfi_config->sthm |= 0x80;
567 sfi_config->sg_inst_table_index = cpu_to_le16(sfi->gate_id);
568 sfi_config->input_ports = cpu_to_le32(1 << port);
570 /* The priority value which may be matched against the
571 * frame’s priority value to determine a match for this entry.
574 sfi_config->multi |= (sfi->prio & 0x7) | 0x8;
576 /* Filter Type. Identifies the contents of the MSDU/FM_INST_INDEX
577 * field as being either an MSDU value or an index into the Flow
578 * Meter Instance table.
582 cpu_to_le16(sfi->maxsdu);
583 sfi_config->multi |= 0x40;
586 if (sfi->meter_id >= 0) {
587 sfi_config->fm_inst_table_index = cpu_to_le16(sfi->meter_id);
588 sfi_config->multi |= 0x80;
592 return enetc_send_cmd(priv->si, &cbd);
595 static int enetc_streamcounter_hw_get(struct enetc_ndev_priv *priv,
597 struct psfp_streamfilter_counters *cnt)
599 struct enetc_cbd cbd = { .cmd = 2 };
600 struct sfi_counter_data *data_buf;
606 cbd.index = cpu_to_le16((u16)index);
608 cbd.cls = BDCR_CMD_STREAM_FILTER;
609 cbd.status_flags = 0;
611 data_size = sizeof(struct sfi_counter_data);
613 tmp = enetc_cbd_alloc_data_mem(priv->si, &cbd, data_size,
614 &dma, (void *)&data_buf);
618 err = enetc_send_cmd(priv->si, &cbd);
622 cnt->matching_frames_count = ((u64)data_buf->matchh << 32) +
625 cnt->not_passing_sdu_count = ((u64)data_buf->msdu_droph << 32) +
626 data_buf->msdu_dropl;
628 cnt->passing_sdu_count = cnt->matching_frames_count
629 - cnt->not_passing_sdu_count;
631 cnt->not_passing_frames_count =
632 ((u64)data_buf->stream_gate_droph << 32) +
633 data_buf->stream_gate_dropl;
635 cnt->passing_frames_count = cnt->matching_frames_count -
636 cnt->not_passing_sdu_count -
637 cnt->not_passing_frames_count;
639 cnt->red_frames_count = ((u64)data_buf->flow_meter_droph << 32) +
640 data_buf->flow_meter_dropl;
643 enetc_cbd_free_data_mem(priv->si, data_size, tmp, &dma);
648 static u64 get_ptp_now(struct enetc_hw *hw)
650 u64 now_lo, now_hi, now;
652 now_lo = enetc_rd(hw, ENETC_SICTR0);
653 now_hi = enetc_rd(hw, ENETC_SICTR1);
654 now = now_lo | now_hi << 32;
659 static int get_start_ns(u64 now, u64 cycle, u64 *start)
666 n = div64_u64(now, cycle);
668 *start = (n + 1) * cycle;
673 /* Stream Gate Instance Set Descriptor */
674 static int enetc_streamgate_hw_set(struct enetc_ndev_priv *priv,
675 struct enetc_psfp_gate *sgi,
678 struct enetc_cbd cbd = { .cmd = 0 };
679 struct sgi_table *sgi_config;
680 struct sgcl_conf *sgcl_config;
681 struct sgcl_data *sgcl_data;
689 cbd.index = cpu_to_le16(sgi->index);
691 cbd.cls = BDCR_CMD_STREAM_GCL;
692 cbd.status_flags = 0x80;
696 return enetc_send_cmd(priv->si, &cbd);
698 if (!sgi->num_entries)
701 if (sgi->num_entries > priv->psfp_cap.max_psfp_gatelist ||
706 sgi_config = &cbd.sgi_table;
708 /* Keep open before gate list start */
709 sgi_config->ocgtst = 0x80;
711 sgi_config->oipv = (sgi->init_ipv < 0) ?
712 0x0 : ((sgi->init_ipv & 0x7) | 0x8);
714 sgi_config->en = 0x80;
717 err = enetc_send_cmd(priv->si, &cbd);
721 memset(&cbd, 0, sizeof(cbd));
723 cbd.index = cpu_to_le16(sgi->index);
725 cbd.cls = BDCR_CMD_STREAM_GCL;
726 cbd.status_flags = 0;
728 sgcl_config = &cbd.sgcl_conf;
730 sgcl_config->acl_len = (sgi->num_entries - 1) & 0x3;
732 data_size = struct_size(sgcl_data, sgcl, sgi->num_entries);
733 tmp = enetc_cbd_alloc_data_mem(priv->si, &cbd, data_size,
734 &dma, (void *)&sgcl_data);
738 sgce = &sgcl_data->sgcl[0];
740 sgcl_config->agtst = 0x80;
742 sgcl_data->ct = sgi->cycletime;
743 sgcl_data->cte = sgi->cycletimext;
745 if (sgi->init_ipv >= 0)
746 sgcl_config->aipv = (sgi->init_ipv & 0x7) | 0x8;
748 for (i = 0; i < sgi->num_entries; i++) {
749 struct action_gate_entry *from = &sgi->entries[i];
750 struct sgce *to = &sgce[i];
752 if (from->gate_state)
756 to->multi |= ((from->ipv & 0x7) << 5) | 0x08;
758 if (from->maxoctets >= 0) {
760 to->msdu[0] = from->maxoctets & 0xFF;
761 to->msdu[1] = (from->maxoctets >> 8) & 0xFF;
762 to->msdu[2] = (from->maxoctets >> 16) & 0xFF;
765 to->interval = from->interval;
768 /* If basetime is less than now, calculate start time */
769 now = get_ptp_now(&priv->si->hw);
771 if (sgi->basetime < now) {
774 err = get_start_ns(now, sgi->cycletime, &start);
777 sgcl_data->btl = lower_32_bits(start);
778 sgcl_data->bth = upper_32_bits(start);
782 hi = upper_32_bits(sgi->basetime);
783 lo = lower_32_bits(sgi->basetime);
788 err = enetc_send_cmd(priv->si, &cbd);
791 enetc_cbd_free_data_mem(priv->si, data_size, tmp, &dma);
795 static int enetc_flowmeter_hw_set(struct enetc_ndev_priv *priv,
796 struct enetc_psfp_meter *fmi,
799 struct enetc_cbd cbd = { .cmd = 0 };
800 struct fmi_conf *fmi_config;
803 cbd.index = cpu_to_le16((u16)fmi->index);
804 cbd.cls = BDCR_CMD_FLOW_METER;
805 cbd.status_flags = 0x80;
808 return enetc_send_cmd(priv->si, &cbd);
810 fmi_config = &cbd.fmi_conf;
811 fmi_config->en = 0x80;
814 temp = (u64)8000 * fmi->cir;
815 temp = div_u64(temp, 3725);
818 fmi_config->cir = cpu_to_le32((u32)temp);
819 fmi_config->cbs = cpu_to_le32(fmi->cbs);
821 /* Default for eir ebs disable */
827 * drop on yellow disable
829 * couple flag disable
831 fmi_config->conf = 0;
833 return enetc_send_cmd(priv->si, &cbd);
836 static struct enetc_stream_filter *enetc_get_stream_by_index(u32 index)
838 struct enetc_stream_filter *f;
840 hlist_for_each_entry(f, &epsfp.stream_list, node)
841 if (f->sid.index == index)
847 static struct enetc_psfp_gate *enetc_get_gate_by_index(u32 index)
849 struct enetc_psfp_gate *g;
851 hlist_for_each_entry(g, &epsfp.psfp_gate_list, node)
852 if (g->index == index)
858 static struct enetc_psfp_filter *enetc_get_filter_by_index(u32 index)
860 struct enetc_psfp_filter *s;
862 hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
863 if (s->index == index)
869 static struct enetc_psfp_meter *enetc_get_meter_by_index(u32 index)
871 struct enetc_psfp_meter *m;
873 hlist_for_each_entry(m, &epsfp.psfp_meter_list, node)
874 if (m->index == index)
880 static struct enetc_psfp_filter
881 *enetc_psfp_check_sfi(struct enetc_psfp_filter *sfi)
883 struct enetc_psfp_filter *s;
885 hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
886 if (s->gate_id == sfi->gate_id &&
887 s->prio == sfi->prio &&
888 s->maxsdu == sfi->maxsdu &&
889 s->meter_id == sfi->meter_id)
895 static int enetc_get_free_index(struct enetc_ndev_priv *priv)
897 u32 max_size = priv->psfp_cap.max_psfp_filter;
900 index = find_first_zero_bit(epsfp.psfp_sfi_bitmap, max_size);
901 if (index == max_size)
907 static void stream_filter_unref(struct enetc_ndev_priv *priv, u32 index)
909 struct enetc_psfp_filter *sfi;
912 sfi = enetc_get_filter_by_index(index);
914 z = refcount_dec_and_test(&sfi->refcount);
917 enetc_streamfilter_hw_set(priv, sfi, false);
918 hlist_del(&sfi->node);
920 clear_bit(index, epsfp.psfp_sfi_bitmap);
924 static void stream_gate_unref(struct enetc_ndev_priv *priv, u32 index)
926 struct enetc_psfp_gate *sgi;
929 sgi = enetc_get_gate_by_index(index);
931 z = refcount_dec_and_test(&sgi->refcount);
933 enetc_streamgate_hw_set(priv, sgi, false);
934 hlist_del(&sgi->node);
939 static void flow_meter_unref(struct enetc_ndev_priv *priv, u32 index)
941 struct enetc_psfp_meter *fmi;
944 fmi = enetc_get_meter_by_index(index);
946 z = refcount_dec_and_test(&fmi->refcount);
948 enetc_flowmeter_hw_set(priv, fmi, false);
949 hlist_del(&fmi->node);
954 static void remove_one_chain(struct enetc_ndev_priv *priv,
955 struct enetc_stream_filter *filter)
957 if (filter->flags & ENETC_PSFP_FLAGS_FMI)
958 flow_meter_unref(priv, filter->fmi_index);
960 stream_gate_unref(priv, filter->sgi_index);
961 stream_filter_unref(priv, filter->sfi_index);
963 hlist_del(&filter->node);
967 static int enetc_psfp_hw_set(struct enetc_ndev_priv *priv,
968 struct enetc_streamid *sid,
969 struct enetc_psfp_filter *sfi,
970 struct enetc_psfp_gate *sgi,
971 struct enetc_psfp_meter *fmi)
975 err = enetc_streamid_hw_set(priv, sid, true);
980 err = enetc_streamfilter_hw_set(priv, sfi, true);
985 err = enetc_streamgate_hw_set(priv, sgi, true);
990 err = enetc_flowmeter_hw_set(priv, fmi, true);
998 enetc_streamgate_hw_set(priv, sgi, false);
1001 enetc_streamfilter_hw_set(priv, sfi, false);
1003 enetc_streamid_hw_set(priv, sid, false);
1007 static struct actions_fwd *enetc_check_flow_actions(u64 acts,
1008 unsigned int inputkeys)
1012 for (i = 0; i < ARRAY_SIZE(enetc_act_fwd); i++)
1013 if (acts == enetc_act_fwd[i].actions &&
1014 inputkeys & enetc_act_fwd[i].keys)
1015 return &enetc_act_fwd[i];
1020 static int enetc_psfp_policer_validate(const struct flow_action *action,
1021 const struct flow_action_entry *act,
1022 struct netlink_ext_ack *extack)
1024 if (act->police.exceed.act_id != FLOW_ACTION_DROP) {
1025 NL_SET_ERR_MSG_MOD(extack,
1026 "Offload not supported when exceed action is not drop");
1030 if (act->police.notexceed.act_id != FLOW_ACTION_PIPE &&
1031 act->police.notexceed.act_id != FLOW_ACTION_ACCEPT) {
1032 NL_SET_ERR_MSG_MOD(extack,
1033 "Offload not supported when conform action is not pipe or ok");
1037 if (act->police.notexceed.act_id == FLOW_ACTION_ACCEPT &&
1038 !flow_action_is_last_entry(action, act)) {
1039 NL_SET_ERR_MSG_MOD(extack,
1040 "Offload not supported when conform action is ok, but action is not last");
1044 if (act->police.peakrate_bytes_ps ||
1045 act->police.avrate || act->police.overhead) {
1046 NL_SET_ERR_MSG_MOD(extack,
1047 "Offload not supported when peakrate/avrate/overhead is configured");
1051 if (act->police.rate_pkt_ps) {
1052 NL_SET_ERR_MSG_MOD(extack,
1053 "QoS offload not support packets per second");
1060 static int enetc_psfp_parse_clsflower(struct enetc_ndev_priv *priv,
1061 struct flow_cls_offload *f)
1063 struct flow_action_entry *entryg = NULL, *entryp = NULL;
1064 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1065 struct netlink_ext_ack *extack = f->common.extack;
1066 struct enetc_stream_filter *filter, *old_filter;
1067 struct enetc_psfp_meter *fmi = NULL, *old_fmi;
1068 struct enetc_psfp_filter *sfi, *old_sfi;
1069 struct enetc_psfp_gate *sgi, *old_sgi;
1070 struct flow_action_entry *entry;
1071 struct action_gate_entry *e;
1072 u8 sfi_overwrite = 0;
1076 if (f->common.chain_index >= priv->psfp_cap.max_streamid) {
1077 NL_SET_ERR_MSG_MOD(extack, "No Stream identify resource!");
1081 flow_action_for_each(i, entry, &rule->action)
1082 if (entry->id == FLOW_ACTION_GATE)
1084 else if (entry->id == FLOW_ACTION_POLICE)
1087 /* Not support without gate action */
1091 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1095 filter->sid.index = f->common.chain_index;
1097 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1098 struct flow_match_eth_addrs match;
1100 flow_rule_match_eth_addrs(rule, &match);
1102 if (!is_zero_ether_addr(match.mask->dst) &&
1103 !is_zero_ether_addr(match.mask->src)) {
1104 NL_SET_ERR_MSG_MOD(extack,
1105 "Cannot match on both source and destination MAC");
1110 if (!is_zero_ether_addr(match.mask->dst)) {
1111 if (!is_broadcast_ether_addr(match.mask->dst)) {
1112 NL_SET_ERR_MSG_MOD(extack,
1113 "Masked matching on destination MAC not supported");
1117 ether_addr_copy(filter->sid.dst_mac, match.key->dst);
1118 filter->sid.filtertype = STREAMID_TYPE_NULL;
1121 if (!is_zero_ether_addr(match.mask->src)) {
1122 if (!is_broadcast_ether_addr(match.mask->src)) {
1123 NL_SET_ERR_MSG_MOD(extack,
1124 "Masked matching on source MAC not supported");
1128 ether_addr_copy(filter->sid.src_mac, match.key->src);
1129 filter->sid.filtertype = STREAMID_TYPE_SMAC;
1132 NL_SET_ERR_MSG_MOD(extack, "Unsupported, must include ETH_ADDRS");
1137 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
1138 struct flow_match_vlan match;
1140 flow_rule_match_vlan(rule, &match);
1141 if (match.mask->vlan_priority) {
1142 if (match.mask->vlan_priority !=
1143 (VLAN_PRIO_MASK >> VLAN_PRIO_SHIFT)) {
1144 NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN priority");
1150 if (match.mask->vlan_id) {
1151 if (match.mask->vlan_id != VLAN_VID_MASK) {
1152 NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN id");
1157 filter->sid.vid = match.key->vlan_id;
1158 if (!filter->sid.vid)
1159 filter->sid.tagged = STREAMID_VLAN_UNTAGGED;
1161 filter->sid.tagged = STREAMID_VLAN_TAGGED;
1164 filter->sid.tagged = STREAMID_VLAN_ALL;
1167 /* parsing gate action */
1168 if (entryg->hw_index >= priv->psfp_cap.max_psfp_gate) {
1169 NL_SET_ERR_MSG_MOD(extack, "No Stream Gate resource!");
1174 if (entryg->gate.num_entries >= priv->psfp_cap.max_psfp_gatelist) {
1175 NL_SET_ERR_MSG_MOD(extack, "No Stream Gate resource!");
1180 entries_size = struct_size(sgi, entries, entryg->gate.num_entries);
1181 sgi = kzalloc(entries_size, GFP_KERNEL);
1187 refcount_set(&sgi->refcount, 1);
1188 sgi->index = entryg->hw_index;
1189 sgi->init_ipv = entryg->gate.prio;
1190 sgi->basetime = entryg->gate.basetime;
1191 sgi->cycletime = entryg->gate.cycletime;
1192 sgi->num_entries = entryg->gate.num_entries;
1195 for (i = 0; i < entryg->gate.num_entries; i++) {
1196 e[i].gate_state = entryg->gate.entries[i].gate_state;
1197 e[i].interval = entryg->gate.entries[i].interval;
1198 e[i].ipv = entryg->gate.entries[i].ipv;
1199 e[i].maxoctets = entryg->gate.entries[i].maxoctets;
1202 filter->sgi_index = sgi->index;
1204 sfi = kzalloc(sizeof(*sfi), GFP_KERNEL);
1210 refcount_set(&sfi->refcount, 1);
1211 sfi->gate_id = sgi->index;
1212 sfi->meter_id = ENETC_PSFP_WILDCARD;
1214 /* Flow meter and max frame size */
1216 err = enetc_psfp_policer_validate(&rule->action, entryp, extack);
1220 if (entryp->police.burst) {
1221 fmi = kzalloc(sizeof(*fmi), GFP_KERNEL);
1226 refcount_set(&fmi->refcount, 1);
1227 fmi->cir = entryp->police.rate_bytes_ps;
1228 fmi->cbs = entryp->police.burst;
1229 fmi->index = entryp->hw_index;
1230 filter->flags |= ENETC_PSFP_FLAGS_FMI;
1231 filter->fmi_index = fmi->index;
1232 sfi->meter_id = fmi->index;
1235 if (entryp->police.mtu)
1236 sfi->maxsdu = entryp->police.mtu;
1239 /* prio ref the filter prio */
1240 if (f->common.prio && f->common.prio <= BIT(3))
1241 sfi->prio = f->common.prio - 1;
1243 sfi->prio = ENETC_PSFP_WILDCARD;
1245 old_sfi = enetc_psfp_check_sfi(sfi);
1249 index = enetc_get_free_index(priv);
1250 if (sfi->handle < 0) {
1251 NL_SET_ERR_MSG_MOD(extack, "No Stream Filter resource!");
1257 sfi->handle = index + HANDLE_OFFSET;
1258 /* Update the stream filter handle also */
1259 filter->sid.handle = sfi->handle;
1260 filter->sfi_index = sfi->index;
1263 filter->sfi_index = old_sfi->index;
1264 filter->sid.handle = old_sfi->handle;
1268 err = enetc_psfp_hw_set(priv, &filter->sid,
1269 sfi_overwrite ? NULL : sfi, sgi, fmi);
1273 spin_lock(&epsfp.psfp_lock);
1274 if (filter->flags & ENETC_PSFP_FLAGS_FMI) {
1275 old_fmi = enetc_get_meter_by_index(filter->fmi_index);
1277 fmi->refcount = old_fmi->refcount;
1278 refcount_set(&fmi->refcount,
1279 refcount_read(&old_fmi->refcount) + 1);
1280 hlist_del(&old_fmi->node);
1283 hlist_add_head(&fmi->node, &epsfp.psfp_meter_list);
1286 /* Remove the old node if exist and update with a new node */
1287 old_sgi = enetc_get_gate_by_index(filter->sgi_index);
1289 refcount_set(&sgi->refcount,
1290 refcount_read(&old_sgi->refcount) + 1);
1291 hlist_del(&old_sgi->node);
1295 hlist_add_head(&sgi->node, &epsfp.psfp_gate_list);
1298 hlist_add_head(&sfi->node, &epsfp.psfp_filter_list);
1299 set_bit(sfi->index, epsfp.psfp_sfi_bitmap);
1302 refcount_inc(&old_sfi->refcount);
1305 old_filter = enetc_get_stream_by_index(filter->sid.index);
1307 remove_one_chain(priv, old_filter);
1309 filter->stats.lastused = jiffies;
1310 hlist_add_head(&filter->node, &epsfp.stream_list);
1312 spin_unlock(&epsfp.psfp_lock);
1328 static int enetc_config_clsflower(struct enetc_ndev_priv *priv,
1329 struct flow_cls_offload *cls_flower)
1331 struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1332 struct netlink_ext_ack *extack = cls_flower->common.extack;
1333 struct flow_dissector *dissector = rule->match.dissector;
1334 struct flow_action *action = &rule->action;
1335 struct flow_action_entry *entry;
1336 struct actions_fwd *fwd;
1340 if (!flow_action_has_entries(action)) {
1341 NL_SET_ERR_MSG_MOD(extack, "At least one action is needed");
1345 flow_action_for_each(i, entry, action)
1346 actions |= BIT(entry->id);
1348 fwd = enetc_check_flow_actions(actions, dissector->used_keys);
1350 NL_SET_ERR_MSG_MOD(extack, "Unsupported filter type!");
1354 if (fwd->output & FILTER_ACTION_TYPE_PSFP) {
1355 err = enetc_psfp_parse_clsflower(priv, cls_flower);
1357 NL_SET_ERR_MSG_MOD(extack, "Invalid PSFP inputs");
1361 NL_SET_ERR_MSG_MOD(extack, "Unsupported actions");
1368 static int enetc_psfp_destroy_clsflower(struct enetc_ndev_priv *priv,
1369 struct flow_cls_offload *f)
1371 struct enetc_stream_filter *filter;
1372 struct netlink_ext_ack *extack = f->common.extack;
1375 if (f->common.chain_index >= priv->psfp_cap.max_streamid) {
1376 NL_SET_ERR_MSG_MOD(extack, "No Stream identify resource!");
1380 filter = enetc_get_stream_by_index(f->common.chain_index);
1384 err = enetc_streamid_hw_set(priv, &filter->sid, false);
1388 remove_one_chain(priv, filter);
1393 static int enetc_destroy_clsflower(struct enetc_ndev_priv *priv,
1394 struct flow_cls_offload *f)
1396 return enetc_psfp_destroy_clsflower(priv, f);
1399 static int enetc_psfp_get_stats(struct enetc_ndev_priv *priv,
1400 struct flow_cls_offload *f)
1402 struct psfp_streamfilter_counters counters = {};
1403 struct enetc_stream_filter *filter;
1404 struct flow_stats stats = {};
1407 filter = enetc_get_stream_by_index(f->common.chain_index);
1411 err = enetc_streamcounter_hw_get(priv, filter->sfi_index, &counters);
1415 spin_lock(&epsfp.psfp_lock);
1416 stats.pkts = counters.matching_frames_count +
1417 counters.not_passing_sdu_count -
1419 stats.drops = counters.not_passing_frames_count +
1420 counters.not_passing_sdu_count +
1421 counters.red_frames_count -
1422 filter->stats.drops;
1423 stats.lastused = filter->stats.lastused;
1424 filter->stats.pkts += stats.pkts;
1425 filter->stats.drops += stats.drops;
1426 spin_unlock(&epsfp.psfp_lock);
1428 flow_stats_update(&f->stats, 0x0, stats.pkts, stats.drops,
1429 stats.lastused, FLOW_ACTION_HW_STATS_DELAYED);
1434 static int enetc_setup_tc_cls_flower(struct enetc_ndev_priv *priv,
1435 struct flow_cls_offload *cls_flower)
1437 switch (cls_flower->command) {
1438 case FLOW_CLS_REPLACE:
1439 return enetc_config_clsflower(priv, cls_flower);
1440 case FLOW_CLS_DESTROY:
1441 return enetc_destroy_clsflower(priv, cls_flower);
1442 case FLOW_CLS_STATS:
1443 return enetc_psfp_get_stats(priv, cls_flower);
1449 static inline void clean_psfp_sfi_bitmap(void)
1451 bitmap_free(epsfp.psfp_sfi_bitmap);
1452 epsfp.psfp_sfi_bitmap = NULL;
1455 static void clean_stream_list(void)
1457 struct enetc_stream_filter *s;
1458 struct hlist_node *tmp;
1460 hlist_for_each_entry_safe(s, tmp, &epsfp.stream_list, node) {
1461 hlist_del(&s->node);
1466 static void clean_sfi_list(void)
1468 struct enetc_psfp_filter *sfi;
1469 struct hlist_node *tmp;
1471 hlist_for_each_entry_safe(sfi, tmp, &epsfp.psfp_filter_list, node) {
1472 hlist_del(&sfi->node);
1477 static void clean_sgi_list(void)
1479 struct enetc_psfp_gate *sgi;
1480 struct hlist_node *tmp;
1482 hlist_for_each_entry_safe(sgi, tmp, &epsfp.psfp_gate_list, node) {
1483 hlist_del(&sgi->node);
1488 static void clean_psfp_all(void)
1490 /* Disable all list nodes and free all memory */
1493 clean_stream_list();
1494 epsfp.dev_bitmap = 0;
1495 clean_psfp_sfi_bitmap();
1498 int enetc_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1501 struct net_device *ndev = cb_priv;
1503 if (!tc_can_offload(ndev))
1507 case TC_SETUP_CLSFLOWER:
1508 return enetc_setup_tc_cls_flower(netdev_priv(ndev), type_data);
1514 int enetc_psfp_init(struct enetc_ndev_priv *priv)
1516 if (epsfp.psfp_sfi_bitmap)
1519 epsfp.psfp_sfi_bitmap = bitmap_zalloc(priv->psfp_cap.max_psfp_filter,
1521 if (!epsfp.psfp_sfi_bitmap)
1524 spin_lock_init(&epsfp.psfp_lock);
1526 if (list_empty(&enetc_block_cb_list))
1527 epsfp.dev_bitmap = 0;
1532 int enetc_psfp_clean(struct enetc_ndev_priv *priv)
1534 if (!list_empty(&enetc_block_cb_list))
1542 int enetc_setup_tc_psfp(struct net_device *ndev, void *type_data)
1544 struct enetc_ndev_priv *priv = netdev_priv(ndev);
1545 struct flow_block_offload *f = type_data;
1548 err = flow_block_cb_setup_simple(f, &enetc_block_cb_list,
1549 enetc_setup_tc_block_cb,
1554 switch (f->command) {
1555 case FLOW_BLOCK_BIND:
1556 port = enetc_pf_to_port(priv->si->pdev);
1560 set_bit(port, &epsfp.dev_bitmap);
1562 case FLOW_BLOCK_UNBIND:
1563 port = enetc_pf_to_port(priv->si->pdev);
1567 clear_bit(port, &epsfp.dev_bitmap);
1568 if (!epsfp.dev_bitmap)