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 /* Do not support TXSTART and TX CSUM offload simutaniously */
301 if (ndev->features & NETIF_F_CSUM_MASK)
304 /* TSD and Qbv are mutually exclusive in hardware */
305 if (enetc_rd(&priv->si->hw, ENETC_QBV_PTGCR_OFFSET) & ENETC_QBV_TGE)
308 priv->tx_ring[tc]->tsd_enable = qopt->enable;
309 enetc_port_wr(&priv->si->hw, ENETC_PTCTSDR(tc),
310 qopt->enable ? ENETC_TSDE : 0);
316 STREAMID_TYPE_RESERVED = 0,
321 enum streamid_vlan_tagged {
322 STREAMID_VLAN_RESERVED = 0,
323 STREAMID_VLAN_TAGGED,
324 STREAMID_VLAN_UNTAGGED,
328 #define ENETC_PSFP_WILDCARD -1
329 #define HANDLE_OFFSET 100
332 FILTER_ACTION_TYPE_PSFP = BIT(0),
333 FILTER_ACTION_TYPE_ACL = BIT(1),
334 FILTER_ACTION_TYPE_BOTH = GENMASK(1, 0),
337 /* This is for limit output type for input actions */
340 u64 keys; /* include the must needed keys */
341 enum forward_type output;
344 struct psfp_streamfilter_counters {
345 u64 matching_frames_count;
346 u64 passing_frames_count;
347 u64 not_passing_frames_count;
348 u64 passing_sdu_count;
349 u64 not_passing_sdu_count;
350 u64 red_frames_count;
353 struct enetc_streamid {
365 struct enetc_psfp_filter {
373 struct hlist_node node;
376 struct enetc_psfp_gate {
384 struct hlist_node node;
385 struct action_gate_entry entries[];
388 /* Only enable the green color frame now
389 * Will add eir and ebs color blind, couple flag etc when
390 * policing action add more offloading parameters
392 struct enetc_psfp_meter {
397 struct hlist_node node;
400 #define ENETC_PSFP_FLAGS_FMI BIT(0)
402 struct enetc_stream_filter {
403 struct enetc_streamid sid;
408 struct flow_stats stats;
409 struct hlist_node node;
413 unsigned long dev_bitmap;
414 unsigned long *psfp_sfi_bitmap;
415 struct hlist_head stream_list;
416 struct hlist_head psfp_filter_list;
417 struct hlist_head psfp_gate_list;
418 struct hlist_head psfp_meter_list;
419 spinlock_t psfp_lock; /* spinlock for the struct enetc_psfp r/w */
422 static struct actions_fwd enetc_act_fwd[] = {
424 BIT(FLOW_ACTION_GATE),
425 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS),
426 FILTER_ACTION_TYPE_PSFP
429 BIT(FLOW_ACTION_POLICE) |
430 BIT(FLOW_ACTION_GATE),
431 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS),
432 FILTER_ACTION_TYPE_PSFP
434 /* example for ACL actions */
436 BIT(FLOW_ACTION_DROP),
438 FILTER_ACTION_TYPE_ACL
442 static struct enetc_psfp epsfp = {
444 .psfp_sfi_bitmap = NULL,
447 static LIST_HEAD(enetc_block_cb_list);
449 /* Stream Identity Entry Set Descriptor */
450 static int enetc_streamid_hw_set(struct enetc_ndev_priv *priv,
451 struct enetc_streamid *sid,
454 struct enetc_cbd cbd = {.cmd = 0};
455 struct streamid_data *si_data;
456 struct streamid_conf *si_conf;
463 port = enetc_pf_to_port(priv->si->pdev);
467 if (sid->index >= priv->psfp_cap.max_streamid)
470 if (sid->filtertype != STREAMID_TYPE_NULL &&
471 sid->filtertype != STREAMID_TYPE_SMAC)
474 /* Disable operation before enable */
475 cbd.index = cpu_to_le16((u16)sid->index);
476 cbd.cls = BDCR_CMD_STREAM_IDENTIFY;
477 cbd.status_flags = 0;
479 data_size = sizeof(struct streamid_data);
480 tmp = enetc_cbd_alloc_data_mem(priv->si, &cbd, data_size,
481 &dma, (void *)&si_data);
485 eth_broadcast_addr(si_data->dmac);
486 si_data->vid_vidm_tg = (ENETC_CBDR_SID_VID_MASK
487 + ((0x3 << 14) | ENETC_CBDR_SID_VIDM));
489 si_conf = &cbd.sid_set;
490 /* Only one port supported for one entry, set itself */
491 si_conf->iports = cpu_to_le32(1 << port);
492 si_conf->id_type = 1;
493 si_conf->oui[2] = 0x0;
494 si_conf->oui[1] = 0x80;
495 si_conf->oui[0] = 0xC2;
497 err = enetc_send_cmd(priv->si, &cbd);
504 /* Enable the entry overwrite again incase space flushed by hardware */
505 cbd.status_flags = 0;
508 si_conf->stream_handle = cpu_to_le32(sid->handle);
509 si_conf->iports = cpu_to_le32(1 << port);
510 si_conf->id_type = sid->filtertype;
511 si_conf->oui[2] = 0x0;
512 si_conf->oui[1] = 0x80;
513 si_conf->oui[0] = 0xC2;
515 memset(si_data, 0, data_size);
517 /* VIDM default to be 1.
518 * VID Match. If set (b1) then the VID must match, otherwise
519 * any VID is considered a match. VIDM setting is only used
520 * when TG is set to b01.
522 if (si_conf->id_type == STREAMID_TYPE_NULL) {
523 ether_addr_copy(si_data->dmac, sid->dst_mac);
524 si_data->vid_vidm_tg = (sid->vid & ENETC_CBDR_SID_VID_MASK) +
525 ((((u16)(sid->tagged) & 0x3) << 14)
526 | ENETC_CBDR_SID_VIDM);
527 } else if (si_conf->id_type == STREAMID_TYPE_SMAC) {
528 ether_addr_copy(si_data->smac, sid->src_mac);
529 si_data->vid_vidm_tg = (sid->vid & ENETC_CBDR_SID_VID_MASK) +
530 ((((u16)(sid->tagged) & 0x3) << 14)
531 | ENETC_CBDR_SID_VIDM);
534 err = enetc_send_cmd(priv->si, &cbd);
536 enetc_cbd_free_data_mem(priv->si, data_size, tmp, &dma);
541 /* Stream Filter Instance Set Descriptor */
542 static int enetc_streamfilter_hw_set(struct enetc_ndev_priv *priv,
543 struct enetc_psfp_filter *sfi,
546 struct enetc_cbd cbd = {.cmd = 0};
547 struct sfi_conf *sfi_config;
550 port = enetc_pf_to_port(priv->si->pdev);
554 cbd.index = cpu_to_le16(sfi->index);
555 cbd.cls = BDCR_CMD_STREAM_FILTER;
556 cbd.status_flags = 0x80;
557 cbd.length = cpu_to_le16(1);
559 sfi_config = &cbd.sfi_conf;
563 sfi_config->en = 0x80;
565 if (sfi->handle >= 0) {
566 sfi_config->stream_handle =
567 cpu_to_le32(sfi->handle);
568 sfi_config->sthm |= 0x80;
571 sfi_config->sg_inst_table_index = cpu_to_le16(sfi->gate_id);
572 sfi_config->input_ports = cpu_to_le32(1 << port);
574 /* The priority value which may be matched against the
575 * frame’s priority value to determine a match for this entry.
578 sfi_config->multi |= (sfi->prio & 0x7) | 0x8;
580 /* Filter Type. Identifies the contents of the MSDU/FM_INST_INDEX
581 * field as being either an MSDU value or an index into the Flow
582 * Meter Instance table.
586 cpu_to_le16(sfi->maxsdu);
587 sfi_config->multi |= 0x40;
590 if (sfi->meter_id >= 0) {
591 sfi_config->fm_inst_table_index = cpu_to_le16(sfi->meter_id);
592 sfi_config->multi |= 0x80;
596 return enetc_send_cmd(priv->si, &cbd);
599 static int enetc_streamcounter_hw_get(struct enetc_ndev_priv *priv,
601 struct psfp_streamfilter_counters *cnt)
603 struct enetc_cbd cbd = { .cmd = 2 };
604 struct sfi_counter_data *data_buf;
610 cbd.index = cpu_to_le16((u16)index);
612 cbd.cls = BDCR_CMD_STREAM_FILTER;
613 cbd.status_flags = 0;
615 data_size = sizeof(struct sfi_counter_data);
617 tmp = enetc_cbd_alloc_data_mem(priv->si, &cbd, data_size,
618 &dma, (void *)&data_buf);
622 err = enetc_send_cmd(priv->si, &cbd);
626 cnt->matching_frames_count = ((u64)data_buf->matchh << 32) +
629 cnt->not_passing_sdu_count = ((u64)data_buf->msdu_droph << 32) +
630 data_buf->msdu_dropl;
632 cnt->passing_sdu_count = cnt->matching_frames_count
633 - cnt->not_passing_sdu_count;
635 cnt->not_passing_frames_count =
636 ((u64)data_buf->stream_gate_droph << 32) +
637 data_buf->stream_gate_dropl;
639 cnt->passing_frames_count = cnt->matching_frames_count -
640 cnt->not_passing_sdu_count -
641 cnt->not_passing_frames_count;
643 cnt->red_frames_count = ((u64)data_buf->flow_meter_droph << 32) +
644 data_buf->flow_meter_dropl;
647 enetc_cbd_free_data_mem(priv->si, data_size, tmp, &dma);
652 static u64 get_ptp_now(struct enetc_hw *hw)
654 u64 now_lo, now_hi, now;
656 now_lo = enetc_rd(hw, ENETC_SICTR0);
657 now_hi = enetc_rd(hw, ENETC_SICTR1);
658 now = now_lo | now_hi << 32;
663 static int get_start_ns(u64 now, u64 cycle, u64 *start)
670 n = div64_u64(now, cycle);
672 *start = (n + 1) * cycle;
677 /* Stream Gate Instance Set Descriptor */
678 static int enetc_streamgate_hw_set(struct enetc_ndev_priv *priv,
679 struct enetc_psfp_gate *sgi,
682 struct enetc_cbd cbd = { .cmd = 0 };
683 struct sgi_table *sgi_config;
684 struct sgcl_conf *sgcl_config;
685 struct sgcl_data *sgcl_data;
693 cbd.index = cpu_to_le16(sgi->index);
695 cbd.cls = BDCR_CMD_STREAM_GCL;
696 cbd.status_flags = 0x80;
700 return enetc_send_cmd(priv->si, &cbd);
702 if (!sgi->num_entries)
705 if (sgi->num_entries > priv->psfp_cap.max_psfp_gatelist ||
710 sgi_config = &cbd.sgi_table;
712 /* Keep open before gate list start */
713 sgi_config->ocgtst = 0x80;
715 sgi_config->oipv = (sgi->init_ipv < 0) ?
716 0x0 : ((sgi->init_ipv & 0x7) | 0x8);
718 sgi_config->en = 0x80;
721 err = enetc_send_cmd(priv->si, &cbd);
725 memset(&cbd, 0, sizeof(cbd));
727 cbd.index = cpu_to_le16(sgi->index);
729 cbd.cls = BDCR_CMD_STREAM_GCL;
730 cbd.status_flags = 0;
732 sgcl_config = &cbd.sgcl_conf;
734 sgcl_config->acl_len = (sgi->num_entries - 1) & 0x3;
736 data_size = struct_size(sgcl_data, sgcl, sgi->num_entries);
737 tmp = enetc_cbd_alloc_data_mem(priv->si, &cbd, data_size,
738 &dma, (void *)&sgcl_data);
742 sgce = &sgcl_data->sgcl[0];
744 sgcl_config->agtst = 0x80;
746 sgcl_data->ct = sgi->cycletime;
747 sgcl_data->cte = sgi->cycletimext;
749 if (sgi->init_ipv >= 0)
750 sgcl_config->aipv = (sgi->init_ipv & 0x7) | 0x8;
752 for (i = 0; i < sgi->num_entries; i++) {
753 struct action_gate_entry *from = &sgi->entries[i];
754 struct sgce *to = &sgce[i];
756 if (from->gate_state)
760 to->multi |= ((from->ipv & 0x7) << 5) | 0x08;
762 if (from->maxoctets >= 0) {
764 to->msdu[0] = from->maxoctets & 0xFF;
765 to->msdu[1] = (from->maxoctets >> 8) & 0xFF;
766 to->msdu[2] = (from->maxoctets >> 16) & 0xFF;
769 to->interval = from->interval;
772 /* If basetime is less than now, calculate start time */
773 now = get_ptp_now(&priv->si->hw);
775 if (sgi->basetime < now) {
778 err = get_start_ns(now, sgi->cycletime, &start);
781 sgcl_data->btl = lower_32_bits(start);
782 sgcl_data->bth = upper_32_bits(start);
786 hi = upper_32_bits(sgi->basetime);
787 lo = lower_32_bits(sgi->basetime);
792 err = enetc_send_cmd(priv->si, &cbd);
795 enetc_cbd_free_data_mem(priv->si, data_size, tmp, &dma);
799 static int enetc_flowmeter_hw_set(struct enetc_ndev_priv *priv,
800 struct enetc_psfp_meter *fmi,
803 struct enetc_cbd cbd = { .cmd = 0 };
804 struct fmi_conf *fmi_config;
807 cbd.index = cpu_to_le16((u16)fmi->index);
808 cbd.cls = BDCR_CMD_FLOW_METER;
809 cbd.status_flags = 0x80;
812 return enetc_send_cmd(priv->si, &cbd);
814 fmi_config = &cbd.fmi_conf;
815 fmi_config->en = 0x80;
818 temp = (u64)8000 * fmi->cir;
819 temp = div_u64(temp, 3725);
822 fmi_config->cir = cpu_to_le32((u32)temp);
823 fmi_config->cbs = cpu_to_le32(fmi->cbs);
825 /* Default for eir ebs disable */
831 * drop on yellow disable
833 * couple flag disable
835 fmi_config->conf = 0;
837 return enetc_send_cmd(priv->si, &cbd);
840 static struct enetc_stream_filter *enetc_get_stream_by_index(u32 index)
842 struct enetc_stream_filter *f;
844 hlist_for_each_entry(f, &epsfp.stream_list, node)
845 if (f->sid.index == index)
851 static struct enetc_psfp_gate *enetc_get_gate_by_index(u32 index)
853 struct enetc_psfp_gate *g;
855 hlist_for_each_entry(g, &epsfp.psfp_gate_list, node)
856 if (g->index == index)
862 static struct enetc_psfp_filter *enetc_get_filter_by_index(u32 index)
864 struct enetc_psfp_filter *s;
866 hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
867 if (s->index == index)
873 static struct enetc_psfp_meter *enetc_get_meter_by_index(u32 index)
875 struct enetc_psfp_meter *m;
877 hlist_for_each_entry(m, &epsfp.psfp_meter_list, node)
878 if (m->index == index)
884 static struct enetc_psfp_filter
885 *enetc_psfp_check_sfi(struct enetc_psfp_filter *sfi)
887 struct enetc_psfp_filter *s;
889 hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
890 if (s->gate_id == sfi->gate_id &&
891 s->prio == sfi->prio &&
892 s->maxsdu == sfi->maxsdu &&
893 s->meter_id == sfi->meter_id)
899 static int enetc_get_free_index(struct enetc_ndev_priv *priv)
901 u32 max_size = priv->psfp_cap.max_psfp_filter;
904 index = find_first_zero_bit(epsfp.psfp_sfi_bitmap, max_size);
905 if (index == max_size)
911 static void stream_filter_unref(struct enetc_ndev_priv *priv, u32 index)
913 struct enetc_psfp_filter *sfi;
916 sfi = enetc_get_filter_by_index(index);
918 z = refcount_dec_and_test(&sfi->refcount);
921 enetc_streamfilter_hw_set(priv, sfi, false);
922 hlist_del(&sfi->node);
924 clear_bit(index, epsfp.psfp_sfi_bitmap);
928 static void stream_gate_unref(struct enetc_ndev_priv *priv, u32 index)
930 struct enetc_psfp_gate *sgi;
933 sgi = enetc_get_gate_by_index(index);
935 z = refcount_dec_and_test(&sgi->refcount);
937 enetc_streamgate_hw_set(priv, sgi, false);
938 hlist_del(&sgi->node);
943 static void flow_meter_unref(struct enetc_ndev_priv *priv, u32 index)
945 struct enetc_psfp_meter *fmi;
948 fmi = enetc_get_meter_by_index(index);
950 z = refcount_dec_and_test(&fmi->refcount);
952 enetc_flowmeter_hw_set(priv, fmi, false);
953 hlist_del(&fmi->node);
958 static void remove_one_chain(struct enetc_ndev_priv *priv,
959 struct enetc_stream_filter *filter)
961 if (filter->flags & ENETC_PSFP_FLAGS_FMI)
962 flow_meter_unref(priv, filter->fmi_index);
964 stream_gate_unref(priv, filter->sgi_index);
965 stream_filter_unref(priv, filter->sfi_index);
967 hlist_del(&filter->node);
971 static int enetc_psfp_hw_set(struct enetc_ndev_priv *priv,
972 struct enetc_streamid *sid,
973 struct enetc_psfp_filter *sfi,
974 struct enetc_psfp_gate *sgi,
975 struct enetc_psfp_meter *fmi)
979 err = enetc_streamid_hw_set(priv, sid, true);
984 err = enetc_streamfilter_hw_set(priv, sfi, true);
989 err = enetc_streamgate_hw_set(priv, sgi, true);
994 err = enetc_flowmeter_hw_set(priv, fmi, true);
1002 enetc_streamgate_hw_set(priv, sgi, false);
1005 enetc_streamfilter_hw_set(priv, sfi, false);
1007 enetc_streamid_hw_set(priv, sid, false);
1011 static struct actions_fwd *enetc_check_flow_actions(u64 acts,
1012 unsigned int inputkeys)
1016 for (i = 0; i < ARRAY_SIZE(enetc_act_fwd); i++)
1017 if (acts == enetc_act_fwd[i].actions &&
1018 inputkeys & enetc_act_fwd[i].keys)
1019 return &enetc_act_fwd[i];
1024 static int enetc_psfp_policer_validate(const struct flow_action *action,
1025 const struct flow_action_entry *act,
1026 struct netlink_ext_ack *extack)
1028 if (act->police.exceed.act_id != FLOW_ACTION_DROP) {
1029 NL_SET_ERR_MSG_MOD(extack,
1030 "Offload not supported when exceed action is not drop");
1034 if (act->police.notexceed.act_id != FLOW_ACTION_PIPE &&
1035 act->police.notexceed.act_id != FLOW_ACTION_ACCEPT) {
1036 NL_SET_ERR_MSG_MOD(extack,
1037 "Offload not supported when conform action is not pipe or ok");
1041 if (act->police.notexceed.act_id == FLOW_ACTION_ACCEPT &&
1042 !flow_action_is_last_entry(action, act)) {
1043 NL_SET_ERR_MSG_MOD(extack,
1044 "Offload not supported when conform action is ok, but action is not last");
1048 if (act->police.peakrate_bytes_ps ||
1049 act->police.avrate || act->police.overhead) {
1050 NL_SET_ERR_MSG_MOD(extack,
1051 "Offload not supported when peakrate/avrate/overhead is configured");
1055 if (act->police.rate_pkt_ps) {
1056 NL_SET_ERR_MSG_MOD(extack,
1057 "QoS offload not support packets per second");
1064 static int enetc_psfp_parse_clsflower(struct enetc_ndev_priv *priv,
1065 struct flow_cls_offload *f)
1067 struct flow_action_entry *entryg = NULL, *entryp = NULL;
1068 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1069 struct netlink_ext_ack *extack = f->common.extack;
1070 struct enetc_stream_filter *filter, *old_filter;
1071 struct enetc_psfp_meter *fmi = NULL, *old_fmi;
1072 struct enetc_psfp_filter *sfi, *old_sfi;
1073 struct enetc_psfp_gate *sgi, *old_sgi;
1074 struct flow_action_entry *entry;
1075 struct action_gate_entry *e;
1076 u8 sfi_overwrite = 0;
1080 if (f->common.chain_index >= priv->psfp_cap.max_streamid) {
1081 NL_SET_ERR_MSG_MOD(extack, "No Stream identify resource!");
1085 flow_action_for_each(i, entry, &rule->action)
1086 if (entry->id == FLOW_ACTION_GATE)
1088 else if (entry->id == FLOW_ACTION_POLICE)
1091 /* Not support without gate action */
1095 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1099 filter->sid.index = f->common.chain_index;
1101 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1102 struct flow_match_eth_addrs match;
1104 flow_rule_match_eth_addrs(rule, &match);
1106 if (!is_zero_ether_addr(match.mask->dst) &&
1107 !is_zero_ether_addr(match.mask->src)) {
1108 NL_SET_ERR_MSG_MOD(extack,
1109 "Cannot match on both source and destination MAC");
1114 if (!is_zero_ether_addr(match.mask->dst)) {
1115 if (!is_broadcast_ether_addr(match.mask->dst)) {
1116 NL_SET_ERR_MSG_MOD(extack,
1117 "Masked matching on destination MAC not supported");
1121 ether_addr_copy(filter->sid.dst_mac, match.key->dst);
1122 filter->sid.filtertype = STREAMID_TYPE_NULL;
1125 if (!is_zero_ether_addr(match.mask->src)) {
1126 if (!is_broadcast_ether_addr(match.mask->src)) {
1127 NL_SET_ERR_MSG_MOD(extack,
1128 "Masked matching on source MAC not supported");
1132 ether_addr_copy(filter->sid.src_mac, match.key->src);
1133 filter->sid.filtertype = STREAMID_TYPE_SMAC;
1136 NL_SET_ERR_MSG_MOD(extack, "Unsupported, must include ETH_ADDRS");
1141 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
1142 struct flow_match_vlan match;
1144 flow_rule_match_vlan(rule, &match);
1145 if (match.mask->vlan_priority) {
1146 if (match.mask->vlan_priority !=
1147 (VLAN_PRIO_MASK >> VLAN_PRIO_SHIFT)) {
1148 NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN priority");
1154 if (match.mask->vlan_id) {
1155 if (match.mask->vlan_id != VLAN_VID_MASK) {
1156 NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN id");
1161 filter->sid.vid = match.key->vlan_id;
1162 if (!filter->sid.vid)
1163 filter->sid.tagged = STREAMID_VLAN_UNTAGGED;
1165 filter->sid.tagged = STREAMID_VLAN_TAGGED;
1168 filter->sid.tagged = STREAMID_VLAN_ALL;
1171 /* parsing gate action */
1172 if (entryg->hw_index >= priv->psfp_cap.max_psfp_gate) {
1173 NL_SET_ERR_MSG_MOD(extack, "No Stream Gate resource!");
1178 if (entryg->gate.num_entries >= priv->psfp_cap.max_psfp_gatelist) {
1179 NL_SET_ERR_MSG_MOD(extack, "No Stream Gate resource!");
1184 entries_size = struct_size(sgi, entries, entryg->gate.num_entries);
1185 sgi = kzalloc(entries_size, GFP_KERNEL);
1191 refcount_set(&sgi->refcount, 1);
1192 sgi->index = entryg->hw_index;
1193 sgi->init_ipv = entryg->gate.prio;
1194 sgi->basetime = entryg->gate.basetime;
1195 sgi->cycletime = entryg->gate.cycletime;
1196 sgi->num_entries = entryg->gate.num_entries;
1199 for (i = 0; i < entryg->gate.num_entries; i++) {
1200 e[i].gate_state = entryg->gate.entries[i].gate_state;
1201 e[i].interval = entryg->gate.entries[i].interval;
1202 e[i].ipv = entryg->gate.entries[i].ipv;
1203 e[i].maxoctets = entryg->gate.entries[i].maxoctets;
1206 filter->sgi_index = sgi->index;
1208 sfi = kzalloc(sizeof(*sfi), GFP_KERNEL);
1214 refcount_set(&sfi->refcount, 1);
1215 sfi->gate_id = sgi->index;
1216 sfi->meter_id = ENETC_PSFP_WILDCARD;
1218 /* Flow meter and max frame size */
1220 err = enetc_psfp_policer_validate(&rule->action, entryp, extack);
1224 if (entryp->police.burst) {
1225 fmi = kzalloc(sizeof(*fmi), GFP_KERNEL);
1230 refcount_set(&fmi->refcount, 1);
1231 fmi->cir = entryp->police.rate_bytes_ps;
1232 fmi->cbs = entryp->police.burst;
1233 fmi->index = entryp->hw_index;
1234 filter->flags |= ENETC_PSFP_FLAGS_FMI;
1235 filter->fmi_index = fmi->index;
1236 sfi->meter_id = fmi->index;
1239 if (entryp->police.mtu)
1240 sfi->maxsdu = entryp->police.mtu;
1243 /* prio ref the filter prio */
1244 if (f->common.prio && f->common.prio <= BIT(3))
1245 sfi->prio = f->common.prio - 1;
1247 sfi->prio = ENETC_PSFP_WILDCARD;
1249 old_sfi = enetc_psfp_check_sfi(sfi);
1253 index = enetc_get_free_index(priv);
1254 if (sfi->handle < 0) {
1255 NL_SET_ERR_MSG_MOD(extack, "No Stream Filter resource!");
1261 sfi->handle = index + HANDLE_OFFSET;
1262 /* Update the stream filter handle also */
1263 filter->sid.handle = sfi->handle;
1264 filter->sfi_index = sfi->index;
1267 filter->sfi_index = old_sfi->index;
1268 filter->sid.handle = old_sfi->handle;
1272 err = enetc_psfp_hw_set(priv, &filter->sid,
1273 sfi_overwrite ? NULL : sfi, sgi, fmi);
1277 spin_lock(&epsfp.psfp_lock);
1278 if (filter->flags & ENETC_PSFP_FLAGS_FMI) {
1279 old_fmi = enetc_get_meter_by_index(filter->fmi_index);
1281 fmi->refcount = old_fmi->refcount;
1282 refcount_set(&fmi->refcount,
1283 refcount_read(&old_fmi->refcount) + 1);
1284 hlist_del(&old_fmi->node);
1287 hlist_add_head(&fmi->node, &epsfp.psfp_meter_list);
1290 /* Remove the old node if exist and update with a new node */
1291 old_sgi = enetc_get_gate_by_index(filter->sgi_index);
1293 refcount_set(&sgi->refcount,
1294 refcount_read(&old_sgi->refcount) + 1);
1295 hlist_del(&old_sgi->node);
1299 hlist_add_head(&sgi->node, &epsfp.psfp_gate_list);
1302 hlist_add_head(&sfi->node, &epsfp.psfp_filter_list);
1303 set_bit(sfi->index, epsfp.psfp_sfi_bitmap);
1306 refcount_inc(&old_sfi->refcount);
1309 old_filter = enetc_get_stream_by_index(filter->sid.index);
1311 remove_one_chain(priv, old_filter);
1313 filter->stats.lastused = jiffies;
1314 hlist_add_head(&filter->node, &epsfp.stream_list);
1316 spin_unlock(&epsfp.psfp_lock);
1332 static int enetc_config_clsflower(struct enetc_ndev_priv *priv,
1333 struct flow_cls_offload *cls_flower)
1335 struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1336 struct netlink_ext_ack *extack = cls_flower->common.extack;
1337 struct flow_dissector *dissector = rule->match.dissector;
1338 struct flow_action *action = &rule->action;
1339 struct flow_action_entry *entry;
1340 struct actions_fwd *fwd;
1344 if (!flow_action_has_entries(action)) {
1345 NL_SET_ERR_MSG_MOD(extack, "At least one action is needed");
1349 flow_action_for_each(i, entry, action)
1350 actions |= BIT(entry->id);
1352 fwd = enetc_check_flow_actions(actions, dissector->used_keys);
1354 NL_SET_ERR_MSG_MOD(extack, "Unsupported filter type!");
1358 if (fwd->output & FILTER_ACTION_TYPE_PSFP) {
1359 err = enetc_psfp_parse_clsflower(priv, cls_flower);
1361 NL_SET_ERR_MSG_MOD(extack, "Invalid PSFP inputs");
1365 NL_SET_ERR_MSG_MOD(extack, "Unsupported actions");
1372 static int enetc_psfp_destroy_clsflower(struct enetc_ndev_priv *priv,
1373 struct flow_cls_offload *f)
1375 struct enetc_stream_filter *filter;
1376 struct netlink_ext_ack *extack = f->common.extack;
1379 if (f->common.chain_index >= priv->psfp_cap.max_streamid) {
1380 NL_SET_ERR_MSG_MOD(extack, "No Stream identify resource!");
1384 filter = enetc_get_stream_by_index(f->common.chain_index);
1388 err = enetc_streamid_hw_set(priv, &filter->sid, false);
1392 remove_one_chain(priv, filter);
1397 static int enetc_destroy_clsflower(struct enetc_ndev_priv *priv,
1398 struct flow_cls_offload *f)
1400 return enetc_psfp_destroy_clsflower(priv, f);
1403 static int enetc_psfp_get_stats(struct enetc_ndev_priv *priv,
1404 struct flow_cls_offload *f)
1406 struct psfp_streamfilter_counters counters = {};
1407 struct enetc_stream_filter *filter;
1408 struct flow_stats stats = {};
1411 filter = enetc_get_stream_by_index(f->common.chain_index);
1415 err = enetc_streamcounter_hw_get(priv, filter->sfi_index, &counters);
1419 spin_lock(&epsfp.psfp_lock);
1420 stats.pkts = counters.matching_frames_count +
1421 counters.not_passing_sdu_count -
1423 stats.drops = counters.not_passing_frames_count +
1424 counters.not_passing_sdu_count +
1425 counters.red_frames_count -
1426 filter->stats.drops;
1427 stats.lastused = filter->stats.lastused;
1428 filter->stats.pkts += stats.pkts;
1429 filter->stats.drops += stats.drops;
1430 spin_unlock(&epsfp.psfp_lock);
1432 flow_stats_update(&f->stats, 0x0, stats.pkts, stats.drops,
1433 stats.lastused, FLOW_ACTION_HW_STATS_DELAYED);
1438 static int enetc_setup_tc_cls_flower(struct enetc_ndev_priv *priv,
1439 struct flow_cls_offload *cls_flower)
1441 switch (cls_flower->command) {
1442 case FLOW_CLS_REPLACE:
1443 return enetc_config_clsflower(priv, cls_flower);
1444 case FLOW_CLS_DESTROY:
1445 return enetc_destroy_clsflower(priv, cls_flower);
1446 case FLOW_CLS_STATS:
1447 return enetc_psfp_get_stats(priv, cls_flower);
1453 static inline void clean_psfp_sfi_bitmap(void)
1455 bitmap_free(epsfp.psfp_sfi_bitmap);
1456 epsfp.psfp_sfi_bitmap = NULL;
1459 static void clean_stream_list(void)
1461 struct enetc_stream_filter *s;
1462 struct hlist_node *tmp;
1464 hlist_for_each_entry_safe(s, tmp, &epsfp.stream_list, node) {
1465 hlist_del(&s->node);
1470 static void clean_sfi_list(void)
1472 struct enetc_psfp_filter *sfi;
1473 struct hlist_node *tmp;
1475 hlist_for_each_entry_safe(sfi, tmp, &epsfp.psfp_filter_list, node) {
1476 hlist_del(&sfi->node);
1481 static void clean_sgi_list(void)
1483 struct enetc_psfp_gate *sgi;
1484 struct hlist_node *tmp;
1486 hlist_for_each_entry_safe(sgi, tmp, &epsfp.psfp_gate_list, node) {
1487 hlist_del(&sgi->node);
1492 static void clean_psfp_all(void)
1494 /* Disable all list nodes and free all memory */
1497 clean_stream_list();
1498 epsfp.dev_bitmap = 0;
1499 clean_psfp_sfi_bitmap();
1502 int enetc_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1505 struct net_device *ndev = cb_priv;
1507 if (!tc_can_offload(ndev))
1511 case TC_SETUP_CLSFLOWER:
1512 return enetc_setup_tc_cls_flower(netdev_priv(ndev), type_data);
1518 int enetc_psfp_init(struct enetc_ndev_priv *priv)
1520 if (epsfp.psfp_sfi_bitmap)
1523 epsfp.psfp_sfi_bitmap = bitmap_zalloc(priv->psfp_cap.max_psfp_filter,
1525 if (!epsfp.psfp_sfi_bitmap)
1528 spin_lock_init(&epsfp.psfp_lock);
1530 if (list_empty(&enetc_block_cb_list))
1531 epsfp.dev_bitmap = 0;
1536 int enetc_psfp_clean(struct enetc_ndev_priv *priv)
1538 if (!list_empty(&enetc_block_cb_list))
1546 int enetc_setup_tc_psfp(struct net_device *ndev, void *type_data)
1548 struct enetc_ndev_priv *priv = netdev_priv(ndev);
1549 struct flow_block_offload *f = type_data;
1552 err = flow_block_cb_setup_simple(f, &enetc_block_cb_list,
1553 enetc_setup_tc_block_cb,
1558 switch (f->command) {
1559 case FLOW_BLOCK_BIND:
1560 port = enetc_pf_to_port(priv->si->pdev);
1564 set_bit(port, &epsfp.dev_bitmap);
1566 case FLOW_BLOCK_UNBIND:
1567 port = enetc_pf_to_port(priv->si->pdev);
1571 clear_bit(port, &epsfp.dev_bitmap);
1572 if (!epsfp.dev_bitmap)