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;
63 if (admin_conf->num_entries > enetc_get_max_gcl_len(&priv->si->hw))
65 gcl_len = admin_conf->num_entries;
67 tge = enetc_rd(&priv->si->hw, ENETC_QBV_PTGCR_OFFSET);
68 if (!admin_conf->enable) {
69 enetc_wr(&priv->si->hw,
70 ENETC_QBV_PTGCR_OFFSET,
71 tge & (~ENETC_QBV_TGE));
75 if (admin_conf->cycle_time > U32_MAX ||
76 admin_conf->cycle_time_extension > U32_MAX)
79 /* Configure the (administrative) gate control list using the
80 * control BD descriptor.
82 gcl_config = &cbd.gcl_conf;
84 data_size = struct_size(gcl_data, entry, gcl_len);
85 gcl_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
89 gce = (struct gce *)(gcl_data + 1);
91 /* Set all gates open as default */
92 gcl_config->atc = 0xff;
93 gcl_config->acl_len = cpu_to_le16(gcl_len);
95 gcl_data->btl = cpu_to_le32(lower_32_bits(admin_conf->base_time));
96 gcl_data->bth = cpu_to_le32(upper_32_bits(admin_conf->base_time));
97 gcl_data->ct = cpu_to_le32(admin_conf->cycle_time);
98 gcl_data->cte = cpu_to_le32(admin_conf->cycle_time_extension);
100 for (i = 0; i < gcl_len; i++) {
101 struct tc_taprio_sched_entry *temp_entry;
102 struct gce *temp_gce = gce + i;
104 temp_entry = &admin_conf->entries[i];
106 temp_gce->gate = (u8)temp_entry->gate_mask;
107 temp_gce->period = cpu_to_le32(temp_entry->interval);
110 cbd.length = cpu_to_le16(data_size);
111 cbd.status_flags = 0;
113 dma = dma_map_single(&priv->si->pdev->dev, gcl_data,
114 data_size, DMA_TO_DEVICE);
115 if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
116 netdev_err(priv->si->ndev, "DMA mapping failed!\n");
121 cbd.addr[0] = cpu_to_le32(lower_32_bits(dma));
122 cbd.addr[1] = cpu_to_le32(upper_32_bits(dma));
123 cbd.cls = BDCR_CMD_PORT_GCL;
124 cbd.status_flags = 0;
126 enetc_wr(&priv->si->hw, ENETC_QBV_PTGCR_OFFSET,
127 tge | ENETC_QBV_TGE);
129 err = enetc_send_cmd(priv->si, &cbd);
131 enetc_wr(&priv->si->hw,
132 ENETC_QBV_PTGCR_OFFSET,
133 tge & (~ENETC_QBV_TGE));
135 dma_unmap_single(&priv->si->pdev->dev, dma, data_size, DMA_TO_DEVICE);
141 int enetc_setup_tc_taprio(struct net_device *ndev, void *type_data)
143 struct tc_taprio_qopt_offload *taprio = type_data;
144 struct enetc_ndev_priv *priv = netdev_priv(ndev);
148 /* TSD and Qbv are mutually exclusive in hardware */
149 for (i = 0; i < priv->num_tx_rings; i++)
150 if (priv->tx_ring[i]->tsd_enable)
153 for (i = 0; i < priv->num_tx_rings; i++)
154 enetc_set_bdr_prio(&priv->si->hw,
155 priv->tx_ring[i]->index,
156 taprio->enable ? i : 0);
158 err = enetc_setup_taprio(ndev, taprio);
161 for (i = 0; i < priv->num_tx_rings; i++)
162 enetc_set_bdr_prio(&priv->si->hw,
163 priv->tx_ring[i]->index,
164 taprio->enable ? 0 : i);
169 static u32 enetc_get_cbs_enable(struct enetc_hw *hw, u8 tc)
171 return enetc_port_rd(hw, ENETC_PTCCBSR0(tc)) & ENETC_CBSE;
174 static u8 enetc_get_cbs_bw(struct enetc_hw *hw, u8 tc)
176 return enetc_port_rd(hw, ENETC_PTCCBSR0(tc)) & ENETC_CBS_BW_MASK;
179 int enetc_setup_tc_cbs(struct net_device *ndev, void *type_data)
181 struct enetc_ndev_priv *priv = netdev_priv(ndev);
182 struct tc_cbs_qopt_offload *cbs = type_data;
183 u32 port_transmit_rate = priv->speed;
184 u8 tc_nums = netdev_get_num_tc(ndev);
185 struct enetc_si *si = priv->si;
186 u32 hi_credit_bit, hi_credit_reg;
187 u32 max_interference_size;
188 u32 port_frame_max_size;
190 u8 prio_top, prio_next;
194 prio_top = netdev_get_prio_tc_map(ndev, tc_nums - 1);
195 prio_next = netdev_get_prio_tc_map(ndev, tc_nums - 2);
197 /* Support highest prio and second prio tc in cbs mode */
198 if (tc != prio_top && tc != prio_next)
202 /* Make sure the other TC that are numerically
203 * lower than this TC have been disabled.
205 if (tc == prio_top &&
206 enetc_get_cbs_enable(&si->hw, prio_next)) {
208 "Disable TC%d before disable TC%d\n",
213 enetc_port_wr(&si->hw, ENETC_PTCCBSR1(tc), 0);
214 enetc_port_wr(&si->hw, ENETC_PTCCBSR0(tc), 0);
219 if (cbs->idleslope - cbs->sendslope != port_transmit_rate * 1000L ||
220 cbs->idleslope < 0 || cbs->sendslope > 0)
223 port_frame_max_size = ndev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
225 bw = cbs->idleslope / (port_transmit_rate * 10UL);
227 /* Make sure the other TC that are numerically
228 * higher than this TC have been enabled.
230 if (tc == prio_next) {
231 if (!enetc_get_cbs_enable(&si->hw, prio_top)) {
233 "Enable TC%d first before enable TC%d\n",
234 prio_top, prio_next);
237 bw_sum += enetc_get_cbs_bw(&si->hw, prio_top);
240 if (bw_sum + bw >= 100) {
242 "The sum of all CBS Bandwidth can't exceed 100\n");
246 enetc_port_rd(&si->hw, ENETC_PTCMSDUR(tc));
248 /* For top prio TC, the max_interfrence_size is maxSizedFrame.
250 * For next prio TC, the max_interfrence_size is calculated as below:
252 * max_interference_size = M0 + Ma + Ra * M0 / (R0 - Ra)
254 * - RA: idleSlope for AVB Class A
255 * - R0: port transmit rate
256 * - M0: maximum sized frame for the port
257 * - MA: maximum sized frame for AVB Class A
260 if (tc == prio_top) {
261 max_interference_size = port_frame_max_size * 8;
265 m0 = port_frame_max_size * 8;
266 ma = enetc_port_rd(&si->hw, ENETC_PTCMSDUR(prio_top)) * 8;
267 ra = enetc_get_cbs_bw(&si->hw, prio_top) *
268 port_transmit_rate * 10000ULL;
269 r0 = port_transmit_rate * 1000000ULL;
270 max_interference_size = m0 + ma +
271 (u32)div_u64((u64)ra * m0, r0 - ra);
274 /* hiCredit bits calculate by:
276 * maxSizedFrame * (idleSlope/portTxRate)
278 hi_credit_bit = max_interference_size * bw / 100;
280 /* hiCredit bits to hiCredit register need to calculated as:
282 * (enetClockFrequency / portTransmitRate) * 100
284 hi_credit_reg = (u32)div_u64((ENETC_CLK * 100ULL) * hi_credit_bit,
285 port_transmit_rate * 1000000ULL);
287 enetc_port_wr(&si->hw, ENETC_PTCCBSR1(tc), hi_credit_reg);
289 /* Set bw register and enable this traffic class */
290 enetc_port_wr(&si->hw, ENETC_PTCCBSR0(tc), bw | ENETC_CBSE);
295 int enetc_setup_tc_txtime(struct net_device *ndev, void *type_data)
297 struct enetc_ndev_priv *priv = netdev_priv(ndev);
298 struct tc_etf_qopt_offload *qopt = type_data;
299 u8 tc_nums = netdev_get_num_tc(ndev);
307 if (tc < 0 || tc >= priv->num_tx_rings)
310 /* Do not support TXSTART and TX CSUM offload simutaniously */
311 if (ndev->features & NETIF_F_CSUM_MASK)
314 /* TSD and Qbv are mutually exclusive in hardware */
315 if (enetc_rd(&priv->si->hw, ENETC_QBV_PTGCR_OFFSET) & ENETC_QBV_TGE)
318 priv->tx_ring[tc]->tsd_enable = qopt->enable;
319 enetc_port_wr(&priv->si->hw, ENETC_PTCTSDR(tc),
320 qopt->enable ? ENETC_TSDE : 0);
326 STREAMID_TYPE_RESERVED = 0,
331 enum streamid_vlan_tagged {
332 STREAMID_VLAN_RESERVED = 0,
333 STREAMID_VLAN_TAGGED,
334 STREAMID_VLAN_UNTAGGED,
338 #define ENETC_PSFP_WILDCARD -1
339 #define HANDLE_OFFSET 100
342 FILTER_ACTION_TYPE_PSFP = BIT(0),
343 FILTER_ACTION_TYPE_ACL = BIT(1),
344 FILTER_ACTION_TYPE_BOTH = GENMASK(1, 0),
347 /* This is for limit output type for input actions */
350 u64 keys; /* include the must needed keys */
351 enum forward_type output;
354 struct psfp_streamfilter_counters {
355 u64 matching_frames_count;
356 u64 passing_frames_count;
357 u64 not_passing_frames_count;
358 u64 passing_sdu_count;
359 u64 not_passing_sdu_count;
360 u64 red_frames_count;
363 struct enetc_streamid {
375 struct enetc_psfp_filter {
383 struct hlist_node node;
386 struct enetc_psfp_gate {
394 struct hlist_node node;
395 struct action_gate_entry entries[];
398 /* Only enable the green color frame now
399 * Will add eir and ebs color blind, couple flag etc when
400 * policing action add more offloading parameters
402 struct enetc_psfp_meter {
407 struct hlist_node node;
410 #define ENETC_PSFP_FLAGS_FMI BIT(0)
412 struct enetc_stream_filter {
413 struct enetc_streamid sid;
418 struct flow_stats stats;
419 struct hlist_node node;
423 unsigned long dev_bitmap;
424 unsigned long *psfp_sfi_bitmap;
425 struct hlist_head stream_list;
426 struct hlist_head psfp_filter_list;
427 struct hlist_head psfp_gate_list;
428 struct hlist_head psfp_meter_list;
429 spinlock_t psfp_lock; /* spinlock for the struct enetc_psfp r/w */
432 static struct actions_fwd enetc_act_fwd[] = {
434 BIT(FLOW_ACTION_GATE),
435 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS),
436 FILTER_ACTION_TYPE_PSFP
439 BIT(FLOW_ACTION_POLICE) |
440 BIT(FLOW_ACTION_GATE),
441 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS),
442 FILTER_ACTION_TYPE_PSFP
444 /* example for ACL actions */
446 BIT(FLOW_ACTION_DROP),
448 FILTER_ACTION_TYPE_ACL
452 static struct enetc_psfp epsfp = {
453 .psfp_sfi_bitmap = NULL,
456 static LIST_HEAD(enetc_block_cb_list);
458 /* Stream Identity Entry Set Descriptor */
459 static int enetc_streamid_hw_set(struct enetc_ndev_priv *priv,
460 struct enetc_streamid *sid,
463 struct enetc_cbd cbd = {.cmd = 0};
464 struct streamid_data *si_data;
465 struct streamid_conf *si_conf;
470 if (sid->index >= priv->psfp_cap.max_streamid)
473 if (sid->filtertype != STREAMID_TYPE_NULL &&
474 sid->filtertype != STREAMID_TYPE_SMAC)
477 /* Disable operation before enable */
478 cbd.index = cpu_to_le16((u16)sid->index);
479 cbd.cls = BDCR_CMD_STREAM_IDENTIFY;
480 cbd.status_flags = 0;
482 data_size = sizeof(struct streamid_data);
483 si_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
484 cbd.length = cpu_to_le16(data_size);
486 dma = dma_map_single(&priv->si->pdev->dev, si_data,
487 data_size, DMA_FROM_DEVICE);
488 if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
489 netdev_err(priv->si->ndev, "DMA mapping failed!\n");
494 cbd.addr[0] = cpu_to_le32(lower_32_bits(dma));
495 cbd.addr[1] = cpu_to_le32(upper_32_bits(dma));
496 eth_broadcast_addr(si_data->dmac);
497 si_data->vid_vidm_tg = (ENETC_CBDR_SID_VID_MASK
498 + ((0x3 << 14) | ENETC_CBDR_SID_VIDM));
500 si_conf = &cbd.sid_set;
501 /* Only one port supported for one entry, set itself */
502 si_conf->iports = cpu_to_le32(1 << enetc_pf_to_port(priv->si->pdev));
503 si_conf->id_type = 1;
504 si_conf->oui[2] = 0x0;
505 si_conf->oui[1] = 0x80;
506 si_conf->oui[0] = 0xC2;
508 err = enetc_send_cmd(priv->si, &cbd);
517 /* Enable the entry overwrite again incase space flushed by hardware */
518 memset(&cbd, 0, sizeof(cbd));
520 cbd.index = cpu_to_le16((u16)sid->index);
522 cbd.cls = BDCR_CMD_STREAM_IDENTIFY;
523 cbd.status_flags = 0;
526 si_conf->stream_handle = cpu_to_le32(sid->handle);
527 si_conf->iports = cpu_to_le32(1 << enetc_pf_to_port(priv->si->pdev));
528 si_conf->id_type = sid->filtertype;
529 si_conf->oui[2] = 0x0;
530 si_conf->oui[1] = 0x80;
531 si_conf->oui[0] = 0xC2;
533 memset(si_data, 0, data_size);
535 cbd.length = cpu_to_le16(data_size);
537 cbd.addr[0] = cpu_to_le32(lower_32_bits(dma));
538 cbd.addr[1] = cpu_to_le32(upper_32_bits(dma));
540 /* VIDM default to be 1.
541 * VID Match. If set (b1) then the VID must match, otherwise
542 * any VID is considered a match. VIDM setting is only used
543 * when TG is set to b01.
545 if (si_conf->id_type == STREAMID_TYPE_NULL) {
546 ether_addr_copy(si_data->dmac, sid->dst_mac);
547 si_data->vid_vidm_tg = (sid->vid & ENETC_CBDR_SID_VID_MASK) +
548 ((((u16)(sid->tagged) & 0x3) << 14)
549 | ENETC_CBDR_SID_VIDM);
550 } else if (si_conf->id_type == STREAMID_TYPE_SMAC) {
551 ether_addr_copy(si_data->smac, sid->src_mac);
552 si_data->vid_vidm_tg = (sid->vid & ENETC_CBDR_SID_VID_MASK) +
553 ((((u16)(sid->tagged) & 0x3) << 14)
554 | ENETC_CBDR_SID_VIDM);
557 err = enetc_send_cmd(priv->si, &cbd);
563 /* Stream Filter Instance Set Descriptor */
564 static int enetc_streamfilter_hw_set(struct enetc_ndev_priv *priv,
565 struct enetc_psfp_filter *sfi,
568 struct enetc_cbd cbd = {.cmd = 0};
569 struct sfi_conf *sfi_config;
571 cbd.index = cpu_to_le16(sfi->index);
572 cbd.cls = BDCR_CMD_STREAM_FILTER;
573 cbd.status_flags = 0x80;
574 cbd.length = cpu_to_le16(1);
576 sfi_config = &cbd.sfi_conf;
580 sfi_config->en = 0x80;
582 if (sfi->handle >= 0) {
583 sfi_config->stream_handle =
584 cpu_to_le32(sfi->handle);
585 sfi_config->sthm |= 0x80;
588 sfi_config->sg_inst_table_index = cpu_to_le16(sfi->gate_id);
589 sfi_config->input_ports =
590 cpu_to_le32(1 << enetc_pf_to_port(priv->si->pdev));
592 /* The priority value which may be matched against the
593 * frame’s priority value to determine a match for this entry.
596 sfi_config->multi |= (sfi->prio & 0x7) | 0x8;
598 /* Filter Type. Identifies the contents of the MSDU/FM_INST_INDEX
599 * field as being either an MSDU value or an index into the Flow
600 * Meter Instance table.
604 cpu_to_le16(sfi->maxsdu);
605 sfi_config->multi |= 0x40;
608 if (sfi->meter_id >= 0) {
609 sfi_config->fm_inst_table_index = cpu_to_le16(sfi->meter_id);
610 sfi_config->multi |= 0x80;
614 return enetc_send_cmd(priv->si, &cbd);
617 static int enetc_streamcounter_hw_get(struct enetc_ndev_priv *priv,
619 struct psfp_streamfilter_counters *cnt)
621 struct enetc_cbd cbd = { .cmd = 2 };
622 struct sfi_counter_data *data_buf;
627 cbd.index = cpu_to_le16((u16)index);
629 cbd.cls = BDCR_CMD_STREAM_FILTER;
630 cbd.status_flags = 0;
632 data_size = sizeof(struct sfi_counter_data);
633 data_buf = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
637 dma = dma_map_single(&priv->si->pdev->dev, data_buf,
638 data_size, DMA_FROM_DEVICE);
639 if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
640 netdev_err(priv->si->ndev, "DMA mapping failed!\n");
644 cbd.addr[0] = cpu_to_le32(lower_32_bits(dma));
645 cbd.addr[1] = cpu_to_le32(upper_32_bits(dma));
647 cbd.length = cpu_to_le16(data_size);
649 err = enetc_send_cmd(priv->si, &cbd);
653 cnt->matching_frames_count = ((u64)data_buf->matchh << 32) +
656 cnt->not_passing_sdu_count = ((u64)data_buf->msdu_droph << 32) +
657 data_buf->msdu_dropl;
659 cnt->passing_sdu_count = cnt->matching_frames_count
660 - cnt->not_passing_sdu_count;
662 cnt->not_passing_frames_count =
663 ((u64)data_buf->stream_gate_droph << 32) +
664 data_buf->stream_gate_dropl;
666 cnt->passing_frames_count = cnt->matching_frames_count -
667 cnt->not_passing_sdu_count -
668 cnt->not_passing_frames_count;
670 cnt->red_frames_count = ((u64)data_buf->flow_meter_droph << 32) +
671 data_buf->flow_meter_dropl;
678 static u64 get_ptp_now(struct enetc_hw *hw)
680 u64 now_lo, now_hi, now;
682 now_lo = enetc_rd(hw, ENETC_SICTR0);
683 now_hi = enetc_rd(hw, ENETC_SICTR1);
684 now = now_lo | now_hi << 32;
689 static int get_start_ns(u64 now, u64 cycle, u64 *start)
696 n = div64_u64(now, cycle);
698 *start = (n + 1) * cycle;
703 /* Stream Gate Instance Set Descriptor */
704 static int enetc_streamgate_hw_set(struct enetc_ndev_priv *priv,
705 struct enetc_psfp_gate *sgi,
708 struct enetc_cbd cbd = { .cmd = 0 };
709 struct sgi_table *sgi_config;
710 struct sgcl_conf *sgcl_config;
711 struct sgcl_data *sgcl_data;
718 cbd.index = cpu_to_le16(sgi->index);
720 cbd.cls = BDCR_CMD_STREAM_GCL;
721 cbd.status_flags = 0x80;
725 return enetc_send_cmd(priv->si, &cbd);
727 if (!sgi->num_entries)
730 if (sgi->num_entries > priv->psfp_cap.max_psfp_gatelist ||
735 sgi_config = &cbd.sgi_table;
737 /* Keep open before gate list start */
738 sgi_config->ocgtst = 0x80;
740 sgi_config->oipv = (sgi->init_ipv < 0) ?
741 0x0 : ((sgi->init_ipv & 0x7) | 0x8);
743 sgi_config->en = 0x80;
746 err = enetc_send_cmd(priv->si, &cbd);
750 memset(&cbd, 0, sizeof(cbd));
752 cbd.index = cpu_to_le16(sgi->index);
754 cbd.cls = BDCR_CMD_STREAM_GCL;
755 cbd.status_flags = 0;
757 sgcl_config = &cbd.sgcl_conf;
759 sgcl_config->acl_len = (sgi->num_entries - 1) & 0x3;
761 data_size = struct_size(sgcl_data, sgcl, sgi->num_entries);
763 sgcl_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
767 cbd.length = cpu_to_le16(data_size);
769 dma = dma_map_single(&priv->si->pdev->dev,
770 sgcl_data, data_size,
772 if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
773 netdev_err(priv->si->ndev, "DMA mapping failed!\n");
778 cbd.addr[0] = cpu_to_le32(lower_32_bits(dma));
779 cbd.addr[1] = cpu_to_le32(upper_32_bits(dma));
781 sgce = &sgcl_data->sgcl[0];
783 sgcl_config->agtst = 0x80;
785 sgcl_data->ct = sgi->cycletime;
786 sgcl_data->cte = sgi->cycletimext;
788 if (sgi->init_ipv >= 0)
789 sgcl_config->aipv = (sgi->init_ipv & 0x7) | 0x8;
791 for (i = 0; i < sgi->num_entries; i++) {
792 struct action_gate_entry *from = &sgi->entries[i];
793 struct sgce *to = &sgce[i];
795 if (from->gate_state)
799 to->multi |= ((from->ipv & 0x7) << 5) | 0x08;
801 if (from->maxoctets >= 0) {
803 to->msdu[0] = from->maxoctets & 0xFF;
804 to->msdu[1] = (from->maxoctets >> 8) & 0xFF;
805 to->msdu[2] = (from->maxoctets >> 16) & 0xFF;
808 to->interval = from->interval;
811 /* If basetime is less than now, calculate start time */
812 now = get_ptp_now(&priv->si->hw);
814 if (sgi->basetime < now) {
817 err = get_start_ns(now, sgi->cycletime, &start);
820 sgcl_data->btl = lower_32_bits(start);
821 sgcl_data->bth = upper_32_bits(start);
825 hi = upper_32_bits(sgi->basetime);
826 lo = lower_32_bits(sgi->basetime);
831 err = enetc_send_cmd(priv->si, &cbd);
839 static int enetc_flowmeter_hw_set(struct enetc_ndev_priv *priv,
840 struct enetc_psfp_meter *fmi,
843 struct enetc_cbd cbd = { .cmd = 0 };
844 struct fmi_conf *fmi_config;
847 cbd.index = cpu_to_le16((u16)fmi->index);
848 cbd.cls = BDCR_CMD_FLOW_METER;
849 cbd.status_flags = 0x80;
852 return enetc_send_cmd(priv->si, &cbd);
854 fmi_config = &cbd.fmi_conf;
855 fmi_config->en = 0x80;
858 temp = (u64)8000 * fmi->cir;
859 temp = div_u64(temp, 3725);
862 fmi_config->cir = cpu_to_le32((u32)temp);
863 fmi_config->cbs = cpu_to_le32(fmi->cbs);
865 /* Default for eir ebs disable */
871 * drop on yellow disable
873 * couple flag disable
875 fmi_config->conf = 0;
877 return enetc_send_cmd(priv->si, &cbd);
880 static struct enetc_stream_filter *enetc_get_stream_by_index(u32 index)
882 struct enetc_stream_filter *f;
884 hlist_for_each_entry(f, &epsfp.stream_list, node)
885 if (f->sid.index == index)
891 static struct enetc_psfp_gate *enetc_get_gate_by_index(u32 index)
893 struct enetc_psfp_gate *g;
895 hlist_for_each_entry(g, &epsfp.psfp_gate_list, node)
896 if (g->index == index)
902 static struct enetc_psfp_filter *enetc_get_filter_by_index(u32 index)
904 struct enetc_psfp_filter *s;
906 hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
907 if (s->index == index)
913 static struct enetc_psfp_meter *enetc_get_meter_by_index(u32 index)
915 struct enetc_psfp_meter *m;
917 hlist_for_each_entry(m, &epsfp.psfp_meter_list, node)
918 if (m->index == index)
924 static struct enetc_psfp_filter
925 *enetc_psfp_check_sfi(struct enetc_psfp_filter *sfi)
927 struct enetc_psfp_filter *s;
929 hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
930 if (s->gate_id == sfi->gate_id &&
931 s->prio == sfi->prio &&
932 s->maxsdu == sfi->maxsdu &&
933 s->meter_id == sfi->meter_id)
939 static int enetc_get_free_index(struct enetc_ndev_priv *priv)
941 u32 max_size = priv->psfp_cap.max_psfp_filter;
944 index = find_first_zero_bit(epsfp.psfp_sfi_bitmap, max_size);
945 if (index == max_size)
951 static void stream_filter_unref(struct enetc_ndev_priv *priv, u32 index)
953 struct enetc_psfp_filter *sfi;
956 sfi = enetc_get_filter_by_index(index);
958 z = refcount_dec_and_test(&sfi->refcount);
961 enetc_streamfilter_hw_set(priv, sfi, false);
962 hlist_del(&sfi->node);
964 clear_bit(index, epsfp.psfp_sfi_bitmap);
968 static void stream_gate_unref(struct enetc_ndev_priv *priv, u32 index)
970 struct enetc_psfp_gate *sgi;
973 sgi = enetc_get_gate_by_index(index);
975 z = refcount_dec_and_test(&sgi->refcount);
977 enetc_streamgate_hw_set(priv, sgi, false);
978 hlist_del(&sgi->node);
983 static void flow_meter_unref(struct enetc_ndev_priv *priv, u32 index)
985 struct enetc_psfp_meter *fmi;
988 fmi = enetc_get_meter_by_index(index);
990 z = refcount_dec_and_test(&fmi->refcount);
992 enetc_flowmeter_hw_set(priv, fmi, false);
993 hlist_del(&fmi->node);
998 static void remove_one_chain(struct enetc_ndev_priv *priv,
999 struct enetc_stream_filter *filter)
1001 if (filter->flags & ENETC_PSFP_FLAGS_FMI)
1002 flow_meter_unref(priv, filter->fmi_index);
1004 stream_gate_unref(priv, filter->sgi_index);
1005 stream_filter_unref(priv, filter->sfi_index);
1007 hlist_del(&filter->node);
1011 static int enetc_psfp_hw_set(struct enetc_ndev_priv *priv,
1012 struct enetc_streamid *sid,
1013 struct enetc_psfp_filter *sfi,
1014 struct enetc_psfp_gate *sgi,
1015 struct enetc_psfp_meter *fmi)
1019 err = enetc_streamid_hw_set(priv, sid, true);
1024 err = enetc_streamfilter_hw_set(priv, sfi, true);
1029 err = enetc_streamgate_hw_set(priv, sgi, true);
1034 err = enetc_flowmeter_hw_set(priv, fmi, true);
1042 enetc_streamgate_hw_set(priv, sgi, false);
1045 enetc_streamfilter_hw_set(priv, sfi, false);
1047 enetc_streamid_hw_set(priv, sid, false);
1051 static struct actions_fwd *enetc_check_flow_actions(u64 acts,
1052 unsigned int inputkeys)
1056 for (i = 0; i < ARRAY_SIZE(enetc_act_fwd); i++)
1057 if (acts == enetc_act_fwd[i].actions &&
1058 inputkeys & enetc_act_fwd[i].keys)
1059 return &enetc_act_fwd[i];
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->gate.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->gate.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 if (entryp->police.rate_pkt_ps) {
1221 NL_SET_ERR_MSG_MOD(extack, "QoS offload not support packets per second");
1225 if (entryp->police.burst) {
1226 fmi = kzalloc(sizeof(*fmi), GFP_KERNEL);
1231 refcount_set(&fmi->refcount, 1);
1232 fmi->cir = entryp->police.rate_bytes_ps;
1233 fmi->cbs = entryp->police.burst;
1234 fmi->index = entryp->police.index;
1235 filter->flags |= ENETC_PSFP_FLAGS_FMI;
1236 filter->fmi_index = fmi->index;
1237 sfi->meter_id = fmi->index;
1240 if (entryp->police.mtu)
1241 sfi->maxsdu = entryp->police.mtu;
1244 /* prio ref the filter prio */
1245 if (f->common.prio && f->common.prio <= BIT(3))
1246 sfi->prio = f->common.prio - 1;
1248 sfi->prio = ENETC_PSFP_WILDCARD;
1250 old_sfi = enetc_psfp_check_sfi(sfi);
1254 index = enetc_get_free_index(priv);
1255 if (sfi->handle < 0) {
1256 NL_SET_ERR_MSG_MOD(extack, "No Stream Filter resource!");
1262 sfi->handle = index + HANDLE_OFFSET;
1263 /* Update the stream filter handle also */
1264 filter->sid.handle = sfi->handle;
1265 filter->sfi_index = sfi->index;
1268 filter->sfi_index = old_sfi->index;
1269 filter->sid.handle = old_sfi->handle;
1273 err = enetc_psfp_hw_set(priv, &filter->sid,
1274 sfi_overwrite ? NULL : sfi, sgi, fmi);
1278 spin_lock(&epsfp.psfp_lock);
1279 if (filter->flags & ENETC_PSFP_FLAGS_FMI) {
1280 old_fmi = enetc_get_meter_by_index(filter->fmi_index);
1282 fmi->refcount = old_fmi->refcount;
1283 refcount_set(&fmi->refcount,
1284 refcount_read(&old_fmi->refcount) + 1);
1285 hlist_del(&old_fmi->node);
1288 hlist_add_head(&fmi->node, &epsfp.psfp_meter_list);
1291 /* Remove the old node if exist and update with a new node */
1292 old_sgi = enetc_get_gate_by_index(filter->sgi_index);
1294 refcount_set(&sgi->refcount,
1295 refcount_read(&old_sgi->refcount) + 1);
1296 hlist_del(&old_sgi->node);
1300 hlist_add_head(&sgi->node, &epsfp.psfp_gate_list);
1303 hlist_add_head(&sfi->node, &epsfp.psfp_filter_list);
1304 set_bit(sfi->index, epsfp.psfp_sfi_bitmap);
1307 refcount_inc(&old_sfi->refcount);
1310 old_filter = enetc_get_stream_by_index(filter->sid.index);
1312 remove_one_chain(priv, old_filter);
1314 filter->stats.lastused = jiffies;
1315 hlist_add_head(&filter->node, &epsfp.stream_list);
1317 spin_unlock(&epsfp.psfp_lock);
1333 static int enetc_config_clsflower(struct enetc_ndev_priv *priv,
1334 struct flow_cls_offload *cls_flower)
1336 struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1337 struct netlink_ext_ack *extack = cls_flower->common.extack;
1338 struct flow_dissector *dissector = rule->match.dissector;
1339 struct flow_action *action = &rule->action;
1340 struct flow_action_entry *entry;
1341 struct actions_fwd *fwd;
1345 if (!flow_action_has_entries(action)) {
1346 NL_SET_ERR_MSG_MOD(extack, "At least one action is needed");
1350 flow_action_for_each(i, entry, action)
1351 actions |= BIT(entry->id);
1353 fwd = enetc_check_flow_actions(actions, dissector->used_keys);
1355 NL_SET_ERR_MSG_MOD(extack, "Unsupported filter type!");
1359 if (fwd->output & FILTER_ACTION_TYPE_PSFP) {
1360 err = enetc_psfp_parse_clsflower(priv, cls_flower);
1362 NL_SET_ERR_MSG_MOD(extack, "Invalid PSFP inputs");
1366 NL_SET_ERR_MSG_MOD(extack, "Unsupported actions");
1373 static int enetc_psfp_destroy_clsflower(struct enetc_ndev_priv *priv,
1374 struct flow_cls_offload *f)
1376 struct enetc_stream_filter *filter;
1377 struct netlink_ext_ack *extack = f->common.extack;
1380 if (f->common.chain_index >= priv->psfp_cap.max_streamid) {
1381 NL_SET_ERR_MSG_MOD(extack, "No Stream identify resource!");
1385 filter = enetc_get_stream_by_index(f->common.chain_index);
1389 err = enetc_streamid_hw_set(priv, &filter->sid, false);
1393 remove_one_chain(priv, filter);
1398 static int enetc_destroy_clsflower(struct enetc_ndev_priv *priv,
1399 struct flow_cls_offload *f)
1401 return enetc_psfp_destroy_clsflower(priv, f);
1404 static int enetc_psfp_get_stats(struct enetc_ndev_priv *priv,
1405 struct flow_cls_offload *f)
1407 struct psfp_streamfilter_counters counters = {};
1408 struct enetc_stream_filter *filter;
1409 struct flow_stats stats = {};
1412 filter = enetc_get_stream_by_index(f->common.chain_index);
1416 err = enetc_streamcounter_hw_get(priv, filter->sfi_index, &counters);
1420 spin_lock(&epsfp.psfp_lock);
1421 stats.pkts = counters.matching_frames_count +
1422 counters.not_passing_sdu_count -
1424 stats.drops = counters.not_passing_frames_count +
1425 counters.not_passing_sdu_count +
1426 counters.red_frames_count -
1427 filter->stats.drops;
1428 stats.lastused = filter->stats.lastused;
1429 filter->stats.pkts += stats.pkts;
1430 filter->stats.drops += stats.drops;
1431 spin_unlock(&epsfp.psfp_lock);
1433 flow_stats_update(&f->stats, 0x0, stats.pkts, stats.drops,
1434 stats.lastused, FLOW_ACTION_HW_STATS_DELAYED);
1439 static int enetc_setup_tc_cls_flower(struct enetc_ndev_priv *priv,
1440 struct flow_cls_offload *cls_flower)
1442 switch (cls_flower->command) {
1443 case FLOW_CLS_REPLACE:
1444 return enetc_config_clsflower(priv, cls_flower);
1445 case FLOW_CLS_DESTROY:
1446 return enetc_destroy_clsflower(priv, cls_flower);
1447 case FLOW_CLS_STATS:
1448 return enetc_psfp_get_stats(priv, cls_flower);
1454 static inline void clean_psfp_sfi_bitmap(void)
1456 bitmap_free(epsfp.psfp_sfi_bitmap);
1457 epsfp.psfp_sfi_bitmap = NULL;
1460 static void clean_stream_list(void)
1462 struct enetc_stream_filter *s;
1463 struct hlist_node *tmp;
1465 hlist_for_each_entry_safe(s, tmp, &epsfp.stream_list, node) {
1466 hlist_del(&s->node);
1471 static void clean_sfi_list(void)
1473 struct enetc_psfp_filter *sfi;
1474 struct hlist_node *tmp;
1476 hlist_for_each_entry_safe(sfi, tmp, &epsfp.psfp_filter_list, node) {
1477 hlist_del(&sfi->node);
1482 static void clean_sgi_list(void)
1484 struct enetc_psfp_gate *sgi;
1485 struct hlist_node *tmp;
1487 hlist_for_each_entry_safe(sgi, tmp, &epsfp.psfp_gate_list, node) {
1488 hlist_del(&sgi->node);
1493 static void clean_psfp_all(void)
1495 /* Disable all list nodes and free all memory */
1498 clean_stream_list();
1499 epsfp.dev_bitmap = 0;
1500 clean_psfp_sfi_bitmap();
1503 int enetc_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1506 struct net_device *ndev = cb_priv;
1508 if (!tc_can_offload(ndev))
1512 case TC_SETUP_CLSFLOWER:
1513 return enetc_setup_tc_cls_flower(netdev_priv(ndev), type_data);
1519 int enetc_psfp_init(struct enetc_ndev_priv *priv)
1521 if (epsfp.psfp_sfi_bitmap)
1524 epsfp.psfp_sfi_bitmap = bitmap_zalloc(priv->psfp_cap.max_psfp_filter,
1526 if (!epsfp.psfp_sfi_bitmap)
1529 spin_lock_init(&epsfp.psfp_lock);
1531 if (list_empty(&enetc_block_cb_list))
1532 epsfp.dev_bitmap = 0;
1537 int enetc_psfp_clean(struct enetc_ndev_priv *priv)
1539 if (!list_empty(&enetc_block_cb_list))
1547 int enetc_setup_tc_psfp(struct net_device *ndev, void *type_data)
1549 struct enetc_ndev_priv *priv = netdev_priv(ndev);
1550 struct flow_block_offload *f = type_data;
1553 err = flow_block_cb_setup_simple(f, &enetc_block_cb_list,
1554 enetc_setup_tc_block_cb,
1559 switch (f->command) {
1560 case FLOW_BLOCK_BIND:
1561 set_bit(enetc_pf_to_port(priv->si->pdev), &epsfp.dev_bitmap);
1563 case FLOW_BLOCK_UNBIND:
1564 clear_bit(enetc_pf_to_port(priv->si->pdev), &epsfp.dev_bitmap);
1565 if (!epsfp.dev_bitmap)