1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2019 NXP */
3 
4 #include "enetc.h"
5 
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>
11 
12 static u16 enetc_get_max_gcl_len(struct enetc_hw *hw)
13 {
14 	return enetc_rd(hw, ENETC_QBV_PTGCAPR_OFFSET)
15 		& ENETC_QBV_MAX_GCL_LEN_MASK;
16 }
17 
18 void enetc_sched_speed_set(struct enetc_ndev_priv *priv, int speed)
19 {
20 	u32 old_speed = priv->speed;
21 	u32 pspeed;
22 
23 	if (speed == old_speed)
24 		return;
25 
26 	switch (speed) {
27 	case SPEED_1000:
28 		pspeed = ENETC_PMR_PSPEED_1000M;
29 		break;
30 	case SPEED_2500:
31 		pspeed = ENETC_PMR_PSPEED_2500M;
32 		break;
33 	case SPEED_100:
34 		pspeed = ENETC_PMR_PSPEED_100M;
35 		break;
36 	case SPEED_10:
37 	default:
38 		pspeed = ENETC_PMR_PSPEED_10M;
39 	}
40 
41 	priv->speed = speed;
42 	enetc_port_wr(&priv->si->hw, ENETC_PMR,
43 		      (enetc_port_rd(&priv->si->hw, ENETC_PMR)
44 		      & (~ENETC_PMR_PSPEED_MASK))
45 		      | pspeed);
46 }
47 
48 static int enetc_setup_taprio(struct net_device *ndev,
49 			      struct tc_taprio_qopt_offload *admin_conf)
50 {
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;
55 	struct gce *gce;
56 	dma_addr_t dma;
57 	u16 data_size;
58 	u16 gcl_len;
59 	u32 tge;
60 	int err;
61 	int i;
62 
63 	if (admin_conf->num_entries > enetc_get_max_gcl_len(&priv->si->hw))
64 		return -EINVAL;
65 	gcl_len = admin_conf->num_entries;
66 
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));
72 		return 0;
73 	}
74 
75 	if (admin_conf->cycle_time > U32_MAX ||
76 	    admin_conf->cycle_time_extension > U32_MAX)
77 		return -EINVAL;
78 
79 	/* Configure the (administrative) gate control list using the
80 	 * control BD descriptor.
81 	 */
82 	gcl_config = &cbd.gcl_conf;
83 
84 	data_size = struct_size(gcl_data, entry, gcl_len);
85 	gcl_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
86 	if (!gcl_data)
87 		return -ENOMEM;
88 
89 	gce = (struct gce *)(gcl_data + 1);
90 
91 	/* Set all gates open as default */
92 	gcl_config->atc = 0xff;
93 	gcl_config->acl_len = cpu_to_le16(gcl_len);
94 
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);
99 
100 	for (i = 0; i < gcl_len; i++) {
101 		struct tc_taprio_sched_entry *temp_entry;
102 		struct gce *temp_gce = gce + i;
103 
104 		temp_entry = &admin_conf->entries[i];
105 
106 		temp_gce->gate = (u8)temp_entry->gate_mask;
107 		temp_gce->period = cpu_to_le32(temp_entry->interval);
108 	}
109 
110 	cbd.length = cpu_to_le16(data_size);
111 	cbd.status_flags = 0;
112 
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");
117 		kfree(gcl_data);
118 		return -ENOMEM;
119 	}
120 
121 	cbd.addr[0] = lower_32_bits(dma);
122 	cbd.addr[1] = upper_32_bits(dma);
123 	cbd.cls = BDCR_CMD_PORT_GCL;
124 	cbd.status_flags = 0;
125 
126 	enetc_wr(&priv->si->hw, ENETC_QBV_PTGCR_OFFSET,
127 		 tge | ENETC_QBV_TGE);
128 
129 	err = enetc_send_cmd(priv->si, &cbd);
130 	if (err)
131 		enetc_wr(&priv->si->hw,
132 			 ENETC_QBV_PTGCR_OFFSET,
133 			 tge & (~ENETC_QBV_TGE));
134 
135 	dma_unmap_single(&priv->si->pdev->dev, dma, data_size, DMA_TO_DEVICE);
136 	kfree(gcl_data);
137 
138 	return err;
139 }
140 
141 int enetc_setup_tc_taprio(struct net_device *ndev, void *type_data)
142 {
143 	struct tc_taprio_qopt_offload *taprio = type_data;
144 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
145 	int err;
146 	int i;
147 
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)
151 			return -EBUSY;
152 
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);
157 
158 	err = enetc_setup_taprio(ndev, taprio);
159 
160 	if (err)
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);
165 
166 	return err;
167 }
168 
169 static u32 enetc_get_cbs_enable(struct enetc_hw *hw, u8 tc)
170 {
171 	return enetc_port_rd(hw, ENETC_PTCCBSR0(tc)) & ENETC_CBSE;
172 }
173 
174 static u8 enetc_get_cbs_bw(struct enetc_hw *hw, u8 tc)
175 {
176 	return enetc_port_rd(hw, ENETC_PTCCBSR0(tc)) & ENETC_CBS_BW_MASK;
177 }
178 
179 int enetc_setup_tc_cbs(struct net_device *ndev, void *type_data)
180 {
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;
189 	u8 tc = cbs->queue;
190 	u8 prio_top, prio_next;
191 	int bw_sum = 0;
192 	u8 bw;
193 
194 	prio_top = netdev_get_prio_tc_map(ndev, tc_nums - 1);
195 	prio_next = netdev_get_prio_tc_map(ndev, tc_nums - 2);
196 
197 	/* Support highest prio and second prio tc in cbs mode */
198 	if (tc != prio_top && tc != prio_next)
199 		return -EOPNOTSUPP;
200 
201 	if (!cbs->enable) {
202 		/* Make sure the other TC that are numerically
203 		 * lower than this TC have been disabled.
204 		 */
205 		if (tc == prio_top &&
206 		    enetc_get_cbs_enable(&si->hw, prio_next)) {
207 			dev_err(&ndev->dev,
208 				"Disable TC%d before disable TC%d\n",
209 				prio_next, tc);
210 			return -EINVAL;
211 		}
212 
213 		enetc_port_wr(&si->hw, ENETC_PTCCBSR1(tc), 0);
214 		enetc_port_wr(&si->hw, ENETC_PTCCBSR0(tc), 0);
215 
216 		return 0;
217 	}
218 
219 	if (cbs->idleslope - cbs->sendslope != port_transmit_rate * 1000L ||
220 	    cbs->idleslope < 0 || cbs->sendslope > 0)
221 		return -EOPNOTSUPP;
222 
223 	port_frame_max_size = ndev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
224 
225 	bw = cbs->idleslope / (port_transmit_rate * 10UL);
226 
227 	/* Make sure the other TC that are numerically
228 	 * higher than this TC have been enabled.
229 	 */
230 	if (tc == prio_next) {
231 		if (!enetc_get_cbs_enable(&si->hw, prio_top)) {
232 			dev_err(&ndev->dev,
233 				"Enable TC%d first before enable TC%d\n",
234 				prio_top, prio_next);
235 			return -EINVAL;
236 		}
237 		bw_sum += enetc_get_cbs_bw(&si->hw, prio_top);
238 	}
239 
240 	if (bw_sum + bw >= 100) {
241 		dev_err(&ndev->dev,
242 			"The sum of all CBS Bandwidth can't exceed 100\n");
243 		return -EINVAL;
244 	}
245 
246 	enetc_port_rd(&si->hw, ENETC_PTCMSDUR(tc));
247 
248 	/* For top prio TC, the max_interfrence_size is maxSizedFrame.
249 	 *
250 	 * For next prio TC, the max_interfrence_size is calculated as below:
251 	 *
252 	 *      max_interference_size = M0 + Ma + Ra * M0 / (R0 - Ra)
253 	 *
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
258 	 */
259 
260 	if (tc == prio_top) {
261 		max_interference_size = port_frame_max_size * 8;
262 	} else {
263 		u32 m0, ma, r0, ra;
264 
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);
272 	}
273 
274 	/* hiCredit bits calculate by:
275 	 *
276 	 * maxSizedFrame * (idleSlope/portTxRate)
277 	 */
278 	hi_credit_bit = max_interference_size * bw / 100;
279 
280 	/* hiCredit bits to hiCredit register need to calculated as:
281 	 *
282 	 * (enetClockFrequency / portTransmitRate) * 100
283 	 */
284 	hi_credit_reg = (u32)div_u64((ENETC_CLK * 100ULL) * hi_credit_bit,
285 				     port_transmit_rate * 1000000ULL);
286 
287 	enetc_port_wr(&si->hw, ENETC_PTCCBSR1(tc), hi_credit_reg);
288 
289 	/* Set bw register and enable this traffic class */
290 	enetc_port_wr(&si->hw, ENETC_PTCCBSR0(tc), bw | ENETC_CBSE);
291 
292 	return 0;
293 }
294 
295 int enetc_setup_tc_txtime(struct net_device *ndev, void *type_data)
296 {
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);
300 	int tc;
301 
302 	if (!tc_nums)
303 		return -EOPNOTSUPP;
304 
305 	tc = qopt->queue;
306 
307 	if (tc < 0 || tc >= priv->num_tx_rings)
308 		return -EINVAL;
309 
310 	/* Do not support TXSTART and TX CSUM offload simutaniously */
311 	if (ndev->features & NETIF_F_CSUM_MASK)
312 		return -EBUSY;
313 
314 	/* TSD and Qbv are mutually exclusive in hardware */
315 	if (enetc_rd(&priv->si->hw, ENETC_QBV_PTGCR_OFFSET) & ENETC_QBV_TGE)
316 		return -EBUSY;
317 
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);
321 
322 	return 0;
323 }
324 
325 enum streamid_type {
326 	STREAMID_TYPE_RESERVED = 0,
327 	STREAMID_TYPE_NULL,
328 	STREAMID_TYPE_SMAC,
329 };
330 
331 enum streamid_vlan_tagged {
332 	STREAMID_VLAN_RESERVED = 0,
333 	STREAMID_VLAN_TAGGED,
334 	STREAMID_VLAN_UNTAGGED,
335 	STREAMID_VLAN_ALL,
336 };
337 
338 #define ENETC_PSFP_WILDCARD -1
339 #define HANDLE_OFFSET 100
340 
341 enum forward_type {
342 	FILTER_ACTION_TYPE_PSFP = BIT(0),
343 	FILTER_ACTION_TYPE_ACL = BIT(1),
344 	FILTER_ACTION_TYPE_BOTH = GENMASK(1, 0),
345 };
346 
347 /* This is for limit output type for input actions */
348 struct actions_fwd {
349 	u64 actions;
350 	u64 keys;	/* include the must needed keys */
351 	enum forward_type output;
352 };
353 
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;
361 };
362 
363 struct enetc_streamid {
364 	u32 index;
365 	union {
366 		u8 src_mac[6];
367 		u8 dst_mac[6];
368 	};
369 	u8 filtertype;
370 	u16 vid;
371 	u8 tagged;
372 	s32 handle;
373 };
374 
375 struct enetc_psfp_filter {
376 	u32 index;
377 	s32 handle;
378 	s8 prio;
379 	u32 maxsdu;
380 	u32 gate_id;
381 	s32 meter_id;
382 	refcount_t refcount;
383 	struct hlist_node node;
384 };
385 
386 struct enetc_psfp_gate {
387 	u32 index;
388 	s8 init_ipv;
389 	u64 basetime;
390 	u64 cycletime;
391 	u64 cycletimext;
392 	u32 num_entries;
393 	refcount_t refcount;
394 	struct hlist_node node;
395 	struct action_gate_entry entries[];
396 };
397 
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
401  */
402 struct enetc_psfp_meter {
403 	u32 index;
404 	u32 cir;
405 	u32 cbs;
406 	refcount_t refcount;
407 	struct hlist_node node;
408 };
409 
410 #define ENETC_PSFP_FLAGS_FMI BIT(0)
411 
412 struct enetc_stream_filter {
413 	struct enetc_streamid sid;
414 	u32 sfi_index;
415 	u32 sgi_index;
416 	u32 flags;
417 	u32 fmi_index;
418 	struct flow_stats stats;
419 	struct hlist_node node;
420 };
421 
422 struct enetc_psfp {
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 */
430 };
431 
432 static struct actions_fwd enetc_act_fwd[] = {
433 	{
434 		BIT(FLOW_ACTION_GATE),
435 		BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS),
436 		FILTER_ACTION_TYPE_PSFP
437 	},
438 	{
439 		BIT(FLOW_ACTION_POLICE) |
440 		BIT(FLOW_ACTION_GATE),
441 		BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS),
442 		FILTER_ACTION_TYPE_PSFP
443 	},
444 	/* example for ACL actions */
445 	{
446 		BIT(FLOW_ACTION_DROP),
447 		0,
448 		FILTER_ACTION_TYPE_ACL
449 	}
450 };
451 
452 static struct enetc_psfp epsfp = {
453 	.psfp_sfi_bitmap = NULL,
454 };
455 
456 static LIST_HEAD(enetc_block_cb_list);
457 
458 static inline int enetc_get_port(struct enetc_ndev_priv *priv)
459 {
460 	return priv->si->pdev->devfn & 0x7;
461 }
462 
463 /* Stream Identity Entry Set Descriptor */
464 static int enetc_streamid_hw_set(struct enetc_ndev_priv *priv,
465 				 struct enetc_streamid *sid,
466 				 u8 enable)
467 {
468 	struct enetc_cbd cbd = {.cmd = 0};
469 	struct streamid_data *si_data;
470 	struct streamid_conf *si_conf;
471 	u16 data_size;
472 	dma_addr_t dma;
473 	int err;
474 
475 	if (sid->index >= priv->psfp_cap.max_streamid)
476 		return -EINVAL;
477 
478 	if (sid->filtertype != STREAMID_TYPE_NULL &&
479 	    sid->filtertype != STREAMID_TYPE_SMAC)
480 		return -EOPNOTSUPP;
481 
482 	/* Disable operation before enable */
483 	cbd.index = cpu_to_le16((u16)sid->index);
484 	cbd.cls = BDCR_CMD_STREAM_IDENTIFY;
485 	cbd.status_flags = 0;
486 
487 	data_size = sizeof(struct streamid_data);
488 	si_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
489 	cbd.length = cpu_to_le16(data_size);
490 
491 	dma = dma_map_single(&priv->si->pdev->dev, si_data,
492 			     data_size, DMA_FROM_DEVICE);
493 	if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
494 		netdev_err(priv->si->ndev, "DMA mapping failed!\n");
495 		kfree(si_data);
496 		return -ENOMEM;
497 	}
498 
499 	cbd.addr[0] = lower_32_bits(dma);
500 	cbd.addr[1] = upper_32_bits(dma);
501 	eth_broadcast_addr(si_data->dmac);
502 	si_data->vid_vidm_tg =
503 		cpu_to_le16(ENETC_CBDR_SID_VID_MASK
504 			    + ((0x3 << 14) | ENETC_CBDR_SID_VIDM));
505 
506 	si_conf = &cbd.sid_set;
507 	/* Only one port supported for one entry, set itself */
508 	si_conf->iports = 1 << enetc_get_port(priv);
509 	si_conf->id_type = 1;
510 	si_conf->oui[2] = 0x0;
511 	si_conf->oui[1] = 0x80;
512 	si_conf->oui[0] = 0xC2;
513 
514 	err = enetc_send_cmd(priv->si, &cbd);
515 	if (err)
516 		return -EINVAL;
517 
518 	if (!enable) {
519 		kfree(si_data);
520 		return 0;
521 	}
522 
523 	/* Enable the entry overwrite again incase space flushed by hardware */
524 	memset(&cbd, 0, sizeof(cbd));
525 
526 	cbd.index = cpu_to_le16((u16)sid->index);
527 	cbd.cmd = 0;
528 	cbd.cls = BDCR_CMD_STREAM_IDENTIFY;
529 	cbd.status_flags = 0;
530 
531 	si_conf->en = 0x80;
532 	si_conf->stream_handle = cpu_to_le32(sid->handle);
533 	si_conf->iports = 1 << enetc_get_port(priv);
534 	si_conf->id_type = sid->filtertype;
535 	si_conf->oui[2] = 0x0;
536 	si_conf->oui[1] = 0x80;
537 	si_conf->oui[0] = 0xC2;
538 
539 	memset(si_data, 0, data_size);
540 
541 	cbd.length = cpu_to_le16(data_size);
542 
543 	cbd.addr[0] = lower_32_bits(dma);
544 	cbd.addr[1] = upper_32_bits(dma);
545 
546 	/* VIDM default to be 1.
547 	 * VID Match. If set (b1) then the VID must match, otherwise
548 	 * any VID is considered a match. VIDM setting is only used
549 	 * when TG is set to b01.
550 	 */
551 	if (si_conf->id_type == STREAMID_TYPE_NULL) {
552 		ether_addr_copy(si_data->dmac, sid->dst_mac);
553 		si_data->vid_vidm_tg =
554 		cpu_to_le16((sid->vid & ENETC_CBDR_SID_VID_MASK) +
555 			    ((((u16)(sid->tagged) & 0x3) << 14)
556 			     | ENETC_CBDR_SID_VIDM));
557 	} else if (si_conf->id_type == STREAMID_TYPE_SMAC) {
558 		ether_addr_copy(si_data->smac, sid->src_mac);
559 		si_data->vid_vidm_tg =
560 		cpu_to_le16((sid->vid & ENETC_CBDR_SID_VID_MASK) +
561 			    ((((u16)(sid->tagged) & 0x3) << 14)
562 			     | ENETC_CBDR_SID_VIDM));
563 	}
564 
565 	err = enetc_send_cmd(priv->si, &cbd);
566 	kfree(si_data);
567 
568 	return err;
569 }
570 
571 /* Stream Filter Instance Set Descriptor */
572 static int enetc_streamfilter_hw_set(struct enetc_ndev_priv *priv,
573 				     struct enetc_psfp_filter *sfi,
574 				     u8 enable)
575 {
576 	struct enetc_cbd cbd = {.cmd = 0};
577 	struct sfi_conf *sfi_config;
578 
579 	cbd.index = cpu_to_le16(sfi->index);
580 	cbd.cls = BDCR_CMD_STREAM_FILTER;
581 	cbd.status_flags = 0x80;
582 	cbd.length = cpu_to_le16(1);
583 
584 	sfi_config = &cbd.sfi_conf;
585 	if (!enable)
586 		goto exit;
587 
588 	sfi_config->en = 0x80;
589 
590 	if (sfi->handle >= 0) {
591 		sfi_config->stream_handle =
592 			cpu_to_le32(sfi->handle);
593 		sfi_config->sthm |= 0x80;
594 	}
595 
596 	sfi_config->sg_inst_table_index = cpu_to_le16(sfi->gate_id);
597 	sfi_config->input_ports = 1 << enetc_get_port(priv);
598 
599 	/* The priority value which may be matched against the
600 	 * frame’s priority value to determine a match for this entry.
601 	 */
602 	if (sfi->prio >= 0)
603 		sfi_config->multi |= (sfi->prio & 0x7) | 0x8;
604 
605 	/* Filter Type. Identifies the contents of the MSDU/FM_INST_INDEX
606 	 * field as being either an MSDU value or an index into the Flow
607 	 * Meter Instance table.
608 	 */
609 	if (sfi->maxsdu) {
610 		sfi_config->msdu =
611 		cpu_to_le16(sfi->maxsdu);
612 		sfi_config->multi |= 0x40;
613 	}
614 
615 	if (sfi->meter_id >= 0) {
616 		sfi_config->fm_inst_table_index = cpu_to_le16(sfi->meter_id);
617 		sfi_config->multi |= 0x80;
618 	}
619 
620 exit:
621 	return enetc_send_cmd(priv->si, &cbd);
622 }
623 
624 static int enetc_streamcounter_hw_get(struct enetc_ndev_priv *priv,
625 				      u32 index,
626 				      struct psfp_streamfilter_counters *cnt)
627 {
628 	struct enetc_cbd cbd = { .cmd = 2 };
629 	struct sfi_counter_data *data_buf;
630 	dma_addr_t dma;
631 	u16 data_size;
632 	int err;
633 
634 	cbd.index = cpu_to_le16((u16)index);
635 	cbd.cmd = 2;
636 	cbd.cls = BDCR_CMD_STREAM_FILTER;
637 	cbd.status_flags = 0;
638 
639 	data_size = sizeof(struct sfi_counter_data);
640 	data_buf = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
641 	if (!data_buf)
642 		return -ENOMEM;
643 
644 	dma = dma_map_single(&priv->si->pdev->dev, data_buf,
645 			     data_size, DMA_FROM_DEVICE);
646 	if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
647 		netdev_err(priv->si->ndev, "DMA mapping failed!\n");
648 		err = -ENOMEM;
649 		goto exit;
650 	}
651 	cbd.addr[0] = lower_32_bits(dma);
652 	cbd.addr[1] = upper_32_bits(dma);
653 
654 	cbd.length = cpu_to_le16(data_size);
655 
656 	err = enetc_send_cmd(priv->si, &cbd);
657 	if (err)
658 		goto exit;
659 
660 	cnt->matching_frames_count =
661 			((u64)le32_to_cpu(data_buf->matchh) << 32)
662 			+ data_buf->matchl;
663 
664 	cnt->not_passing_sdu_count =
665 			((u64)le32_to_cpu(data_buf->msdu_droph) << 32)
666 			+ data_buf->msdu_dropl;
667 
668 	cnt->passing_sdu_count = cnt->matching_frames_count
669 				- cnt->not_passing_sdu_count;
670 
671 	cnt->not_passing_frames_count =
672 		((u64)le32_to_cpu(data_buf->stream_gate_droph) << 32)
673 		+ le32_to_cpu(data_buf->stream_gate_dropl);
674 
675 	cnt->passing_frames_count = cnt->matching_frames_count
676 				- cnt->not_passing_sdu_count
677 				- cnt->not_passing_frames_count;
678 
679 	cnt->red_frames_count =
680 		((u64)le32_to_cpu(data_buf->flow_meter_droph) << 32)
681 		+ le32_to_cpu(data_buf->flow_meter_dropl);
682 
683 exit:
684 	kfree(data_buf);
685 	return err;
686 }
687 
688 static u64 get_ptp_now(struct enetc_hw *hw)
689 {
690 	u64 now_lo, now_hi, now;
691 
692 	now_lo = enetc_rd(hw, ENETC_SICTR0);
693 	now_hi = enetc_rd(hw, ENETC_SICTR1);
694 	now = now_lo | now_hi << 32;
695 
696 	return now;
697 }
698 
699 static int get_start_ns(u64 now, u64 cycle, u64 *start)
700 {
701 	u64 n;
702 
703 	if (!cycle)
704 		return -EFAULT;
705 
706 	n = div64_u64(now, cycle);
707 
708 	*start = (n + 1) * cycle;
709 
710 	return 0;
711 }
712 
713 /* Stream Gate Instance Set Descriptor */
714 static int enetc_streamgate_hw_set(struct enetc_ndev_priv *priv,
715 				   struct enetc_psfp_gate *sgi,
716 				   u8 enable)
717 {
718 	struct enetc_cbd cbd = { .cmd = 0 };
719 	struct sgi_table *sgi_config;
720 	struct sgcl_conf *sgcl_config;
721 	struct sgcl_data *sgcl_data;
722 	struct sgce *sgce;
723 	dma_addr_t dma;
724 	u16 data_size;
725 	int err, i;
726 	u64 now;
727 
728 	cbd.index = cpu_to_le16(sgi->index);
729 	cbd.cmd = 0;
730 	cbd.cls = BDCR_CMD_STREAM_GCL;
731 	cbd.status_flags = 0x80;
732 
733 	/* disable */
734 	if (!enable)
735 		return enetc_send_cmd(priv->si, &cbd);
736 
737 	if (!sgi->num_entries)
738 		return 0;
739 
740 	if (sgi->num_entries > priv->psfp_cap.max_psfp_gatelist ||
741 	    !sgi->cycletime)
742 		return -EINVAL;
743 
744 	/* enable */
745 	sgi_config = &cbd.sgi_table;
746 
747 	/* Keep open before gate list start */
748 	sgi_config->ocgtst = 0x80;
749 
750 	sgi_config->oipv = (sgi->init_ipv < 0) ?
751 				0x0 : ((sgi->init_ipv & 0x7) | 0x8);
752 
753 	sgi_config->en = 0x80;
754 
755 	/* Basic config */
756 	err = enetc_send_cmd(priv->si, &cbd);
757 	if (err)
758 		return -EINVAL;
759 
760 	memset(&cbd, 0, sizeof(cbd));
761 
762 	cbd.index = cpu_to_le16(sgi->index);
763 	cbd.cmd = 1;
764 	cbd.cls = BDCR_CMD_STREAM_GCL;
765 	cbd.status_flags = 0;
766 
767 	sgcl_config = &cbd.sgcl_conf;
768 
769 	sgcl_config->acl_len = (sgi->num_entries - 1) & 0x3;
770 
771 	data_size = struct_size(sgcl_data, sgcl, sgi->num_entries);
772 
773 	sgcl_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
774 	if (!sgcl_data)
775 		return -ENOMEM;
776 
777 	cbd.length = cpu_to_le16(data_size);
778 
779 	dma = dma_map_single(&priv->si->pdev->dev,
780 			     sgcl_data, data_size,
781 			     DMA_FROM_DEVICE);
782 	if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
783 		netdev_err(priv->si->ndev, "DMA mapping failed!\n");
784 		kfree(sgcl_data);
785 		return -ENOMEM;
786 	}
787 
788 	cbd.addr[0] = lower_32_bits(dma);
789 	cbd.addr[1] = upper_32_bits(dma);
790 
791 	sgce = &sgcl_data->sgcl[0];
792 
793 	sgcl_config->agtst = 0x80;
794 
795 	sgcl_data->ct = cpu_to_le32(sgi->cycletime);
796 	sgcl_data->cte = cpu_to_le32(sgi->cycletimext);
797 
798 	if (sgi->init_ipv >= 0)
799 		sgcl_config->aipv = (sgi->init_ipv & 0x7) | 0x8;
800 
801 	for (i = 0; i < sgi->num_entries; i++) {
802 		struct action_gate_entry *from = &sgi->entries[i];
803 		struct sgce *to = &sgce[i];
804 
805 		if (from->gate_state)
806 			to->multi |= 0x10;
807 
808 		if (from->ipv >= 0)
809 			to->multi |= ((from->ipv & 0x7) << 5) | 0x08;
810 
811 		if (from->maxoctets >= 0) {
812 			to->multi |= 0x01;
813 			to->msdu[0] = from->maxoctets & 0xFF;
814 			to->msdu[1] = (from->maxoctets >> 8) & 0xFF;
815 			to->msdu[2] = (from->maxoctets >> 16) & 0xFF;
816 		}
817 
818 		to->interval = cpu_to_le32(from->interval);
819 	}
820 
821 	/* If basetime is less than now, calculate start time */
822 	now = get_ptp_now(&priv->si->hw);
823 
824 	if (sgi->basetime < now) {
825 		u64 start;
826 
827 		err = get_start_ns(now, sgi->cycletime, &start);
828 		if (err)
829 			goto exit;
830 		sgcl_data->btl = cpu_to_le32(lower_32_bits(start));
831 		sgcl_data->bth = cpu_to_le32(upper_32_bits(start));
832 	} else {
833 		u32 hi, lo;
834 
835 		hi = upper_32_bits(sgi->basetime);
836 		lo = lower_32_bits(sgi->basetime);
837 		sgcl_data->bth = cpu_to_le32(hi);
838 		sgcl_data->btl = cpu_to_le32(lo);
839 	}
840 
841 	err = enetc_send_cmd(priv->si, &cbd);
842 
843 exit:
844 	kfree(sgcl_data);
845 
846 	return err;
847 }
848 
849 static int enetc_flowmeter_hw_set(struct enetc_ndev_priv *priv,
850 				  struct enetc_psfp_meter *fmi,
851 				  u8 enable)
852 {
853 	struct enetc_cbd cbd = { .cmd = 0 };
854 	struct fmi_conf *fmi_config;
855 	u64 temp = 0;
856 
857 	cbd.index = cpu_to_le16((u16)fmi->index);
858 	cbd.cls = BDCR_CMD_FLOW_METER;
859 	cbd.status_flags = 0x80;
860 
861 	if (!enable)
862 		return enetc_send_cmd(priv->si, &cbd);
863 
864 	fmi_config = &cbd.fmi_conf;
865 	fmi_config->en = 0x80;
866 
867 	if (fmi->cir) {
868 		temp = (u64)8000 * fmi->cir;
869 		temp = div_u64(temp, 3725);
870 	}
871 
872 	fmi_config->cir = cpu_to_le32((u32)temp);
873 	fmi_config->cbs = cpu_to_le32(fmi->cbs);
874 
875 	/* Default for eir ebs disable */
876 	fmi_config->eir = 0;
877 	fmi_config->ebs = 0;
878 
879 	/* Default:
880 	 * mark red disable
881 	 * drop on yellow disable
882 	 * color mode disable
883 	 * couple flag disable
884 	 */
885 	fmi_config->conf = 0;
886 
887 	return enetc_send_cmd(priv->si, &cbd);
888 }
889 
890 static struct enetc_stream_filter *enetc_get_stream_by_index(u32 index)
891 {
892 	struct enetc_stream_filter *f;
893 
894 	hlist_for_each_entry(f, &epsfp.stream_list, node)
895 		if (f->sid.index == index)
896 			return f;
897 
898 	return NULL;
899 }
900 
901 static struct enetc_psfp_gate *enetc_get_gate_by_index(u32 index)
902 {
903 	struct enetc_psfp_gate *g;
904 
905 	hlist_for_each_entry(g, &epsfp.psfp_gate_list, node)
906 		if (g->index == index)
907 			return g;
908 
909 	return NULL;
910 }
911 
912 static struct enetc_psfp_filter *enetc_get_filter_by_index(u32 index)
913 {
914 	struct enetc_psfp_filter *s;
915 
916 	hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
917 		if (s->index == index)
918 			return s;
919 
920 	return NULL;
921 }
922 
923 static struct enetc_psfp_meter *enetc_get_meter_by_index(u32 index)
924 {
925 	struct enetc_psfp_meter *m;
926 
927 	hlist_for_each_entry(m, &epsfp.psfp_meter_list, node)
928 		if (m->index == index)
929 			return m;
930 
931 	return NULL;
932 }
933 
934 static struct enetc_psfp_filter
935 	*enetc_psfp_check_sfi(struct enetc_psfp_filter *sfi)
936 {
937 	struct enetc_psfp_filter *s;
938 
939 	hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
940 		if (s->gate_id == sfi->gate_id &&
941 		    s->prio == sfi->prio &&
942 		    s->maxsdu == sfi->maxsdu &&
943 		    s->meter_id == sfi->meter_id)
944 			return s;
945 
946 	return NULL;
947 }
948 
949 static int enetc_get_free_index(struct enetc_ndev_priv *priv)
950 {
951 	u32 max_size = priv->psfp_cap.max_psfp_filter;
952 	unsigned long index;
953 
954 	index = find_first_zero_bit(epsfp.psfp_sfi_bitmap, max_size);
955 	if (index == max_size)
956 		return -1;
957 
958 	return index;
959 }
960 
961 static void stream_filter_unref(struct enetc_ndev_priv *priv, u32 index)
962 {
963 	struct enetc_psfp_filter *sfi;
964 	u8 z;
965 
966 	sfi = enetc_get_filter_by_index(index);
967 	WARN_ON(!sfi);
968 	z = refcount_dec_and_test(&sfi->refcount);
969 
970 	if (z) {
971 		enetc_streamfilter_hw_set(priv, sfi, false);
972 		hlist_del(&sfi->node);
973 		kfree(sfi);
974 		clear_bit(index, epsfp.psfp_sfi_bitmap);
975 	}
976 }
977 
978 static void stream_gate_unref(struct enetc_ndev_priv *priv, u32 index)
979 {
980 	struct enetc_psfp_gate *sgi;
981 	u8 z;
982 
983 	sgi = enetc_get_gate_by_index(index);
984 	WARN_ON(!sgi);
985 	z = refcount_dec_and_test(&sgi->refcount);
986 	if (z) {
987 		enetc_streamgate_hw_set(priv, sgi, false);
988 		hlist_del(&sgi->node);
989 		kfree(sgi);
990 	}
991 }
992 
993 static void flow_meter_unref(struct enetc_ndev_priv *priv, u32 index)
994 {
995 	struct enetc_psfp_meter *fmi;
996 	u8 z;
997 
998 	fmi = enetc_get_meter_by_index(index);
999 	WARN_ON(!fmi);
1000 	z = refcount_dec_and_test(&fmi->refcount);
1001 	if (z) {
1002 		enetc_flowmeter_hw_set(priv, fmi, false);
1003 		hlist_del(&fmi->node);
1004 		kfree(fmi);
1005 	}
1006 }
1007 
1008 static void remove_one_chain(struct enetc_ndev_priv *priv,
1009 			     struct enetc_stream_filter *filter)
1010 {
1011 	if (filter->flags & ENETC_PSFP_FLAGS_FMI)
1012 		flow_meter_unref(priv, filter->fmi_index);
1013 
1014 	stream_gate_unref(priv, filter->sgi_index);
1015 	stream_filter_unref(priv, filter->sfi_index);
1016 
1017 	hlist_del(&filter->node);
1018 	kfree(filter);
1019 }
1020 
1021 static int enetc_psfp_hw_set(struct enetc_ndev_priv *priv,
1022 			     struct enetc_streamid *sid,
1023 			     struct enetc_psfp_filter *sfi,
1024 			     struct enetc_psfp_gate *sgi,
1025 			     struct enetc_psfp_meter *fmi)
1026 {
1027 	int err;
1028 
1029 	err = enetc_streamid_hw_set(priv, sid, true);
1030 	if (err)
1031 		return err;
1032 
1033 	if (sfi) {
1034 		err = enetc_streamfilter_hw_set(priv, sfi, true);
1035 		if (err)
1036 			goto revert_sid;
1037 	}
1038 
1039 	err = enetc_streamgate_hw_set(priv, sgi, true);
1040 	if (err)
1041 		goto revert_sfi;
1042 
1043 	if (fmi) {
1044 		err = enetc_flowmeter_hw_set(priv, fmi, true);
1045 		if (err)
1046 			goto revert_sgi;
1047 	}
1048 
1049 	return 0;
1050 
1051 revert_sgi:
1052 	enetc_streamgate_hw_set(priv, sgi, false);
1053 revert_sfi:
1054 	if (sfi)
1055 		enetc_streamfilter_hw_set(priv, sfi, false);
1056 revert_sid:
1057 	enetc_streamid_hw_set(priv, sid, false);
1058 	return err;
1059 }
1060 
1061 static struct actions_fwd *enetc_check_flow_actions(u64 acts,
1062 						    unsigned int inputkeys)
1063 {
1064 	int i;
1065 
1066 	for (i = 0; i < ARRAY_SIZE(enetc_act_fwd); i++)
1067 		if (acts == enetc_act_fwd[i].actions &&
1068 		    inputkeys & enetc_act_fwd[i].keys)
1069 			return &enetc_act_fwd[i];
1070 
1071 	return NULL;
1072 }
1073 
1074 static int enetc_psfp_parse_clsflower(struct enetc_ndev_priv *priv,
1075 				      struct flow_cls_offload *f)
1076 {
1077 	struct flow_action_entry *entryg = NULL, *entryp = NULL;
1078 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1079 	struct netlink_ext_ack *extack = f->common.extack;
1080 	struct enetc_stream_filter *filter, *old_filter;
1081 	struct enetc_psfp_meter *fmi = NULL, *old_fmi;
1082 	struct enetc_psfp_filter *sfi, *old_sfi;
1083 	struct enetc_psfp_gate *sgi, *old_sgi;
1084 	struct flow_action_entry *entry;
1085 	struct action_gate_entry *e;
1086 	u8 sfi_overwrite = 0;
1087 	int entries_size;
1088 	int i, err;
1089 
1090 	if (f->common.chain_index >= priv->psfp_cap.max_streamid) {
1091 		NL_SET_ERR_MSG_MOD(extack, "No Stream identify resource!");
1092 		return -ENOSPC;
1093 	}
1094 
1095 	flow_action_for_each(i, entry, &rule->action)
1096 		if (entry->id == FLOW_ACTION_GATE)
1097 			entryg = entry;
1098 		else if (entry->id == FLOW_ACTION_POLICE)
1099 			entryp = entry;
1100 
1101 	/* Not support without gate action */
1102 	if (!entryg)
1103 		return -EINVAL;
1104 
1105 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1106 	if (!filter)
1107 		return -ENOMEM;
1108 
1109 	filter->sid.index = f->common.chain_index;
1110 
1111 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1112 		struct flow_match_eth_addrs match;
1113 
1114 		flow_rule_match_eth_addrs(rule, &match);
1115 
1116 		if (!is_zero_ether_addr(match.mask->dst) &&
1117 		    !is_zero_ether_addr(match.mask->src)) {
1118 			NL_SET_ERR_MSG_MOD(extack,
1119 					   "Cannot match on both source and destination MAC");
1120 			err = -EINVAL;
1121 			goto free_filter;
1122 		}
1123 
1124 		if (!is_zero_ether_addr(match.mask->dst)) {
1125 			if (!is_broadcast_ether_addr(match.mask->dst)) {
1126 				NL_SET_ERR_MSG_MOD(extack,
1127 						   "Masked matching on destination MAC not supported");
1128 				err = -EINVAL;
1129 				goto free_filter;
1130 			}
1131 			ether_addr_copy(filter->sid.dst_mac, match.key->dst);
1132 			filter->sid.filtertype = STREAMID_TYPE_NULL;
1133 		}
1134 
1135 		if (!is_zero_ether_addr(match.mask->src)) {
1136 			if (!is_broadcast_ether_addr(match.mask->src)) {
1137 				NL_SET_ERR_MSG_MOD(extack,
1138 						   "Masked matching on source MAC not supported");
1139 				err = -EINVAL;
1140 				goto free_filter;
1141 			}
1142 			ether_addr_copy(filter->sid.src_mac, match.key->src);
1143 			filter->sid.filtertype = STREAMID_TYPE_SMAC;
1144 		}
1145 	} else {
1146 		NL_SET_ERR_MSG_MOD(extack, "Unsupported, must include ETH_ADDRS");
1147 		err = -EINVAL;
1148 		goto free_filter;
1149 	}
1150 
1151 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
1152 		struct flow_match_vlan match;
1153 
1154 		flow_rule_match_vlan(rule, &match);
1155 		if (match.mask->vlan_priority) {
1156 			if (match.mask->vlan_priority !=
1157 			    (VLAN_PRIO_MASK >> VLAN_PRIO_SHIFT)) {
1158 				NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN priority");
1159 				err = -EINVAL;
1160 				goto free_filter;
1161 			}
1162 		}
1163 
1164 		if (match.mask->vlan_id) {
1165 			if (match.mask->vlan_id != VLAN_VID_MASK) {
1166 				NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN id");
1167 				err = -EINVAL;
1168 				goto free_filter;
1169 			}
1170 
1171 			filter->sid.vid = match.key->vlan_id;
1172 			if (!filter->sid.vid)
1173 				filter->sid.tagged = STREAMID_VLAN_UNTAGGED;
1174 			else
1175 				filter->sid.tagged = STREAMID_VLAN_TAGGED;
1176 		}
1177 	} else {
1178 		filter->sid.tagged = STREAMID_VLAN_ALL;
1179 	}
1180 
1181 	/* parsing gate action */
1182 	if (entryg->gate.index >= priv->psfp_cap.max_psfp_gate) {
1183 		NL_SET_ERR_MSG_MOD(extack, "No Stream Gate resource!");
1184 		err = -ENOSPC;
1185 		goto free_filter;
1186 	}
1187 
1188 	if (entryg->gate.num_entries >= priv->psfp_cap.max_psfp_gatelist) {
1189 		NL_SET_ERR_MSG_MOD(extack, "No Stream Gate resource!");
1190 		err = -ENOSPC;
1191 		goto free_filter;
1192 	}
1193 
1194 	entries_size = struct_size(sgi, entries, entryg->gate.num_entries);
1195 	sgi = kzalloc(entries_size, GFP_KERNEL);
1196 	if (!sgi) {
1197 		err = -ENOMEM;
1198 		goto free_filter;
1199 	}
1200 
1201 	refcount_set(&sgi->refcount, 1);
1202 	sgi->index = entryg->gate.index;
1203 	sgi->init_ipv = entryg->gate.prio;
1204 	sgi->basetime = entryg->gate.basetime;
1205 	sgi->cycletime = entryg->gate.cycletime;
1206 	sgi->num_entries = entryg->gate.num_entries;
1207 
1208 	e = sgi->entries;
1209 	for (i = 0; i < entryg->gate.num_entries; i++) {
1210 		e[i].gate_state = entryg->gate.entries[i].gate_state;
1211 		e[i].interval = entryg->gate.entries[i].interval;
1212 		e[i].ipv = entryg->gate.entries[i].ipv;
1213 		e[i].maxoctets = entryg->gate.entries[i].maxoctets;
1214 	}
1215 
1216 	filter->sgi_index = sgi->index;
1217 
1218 	sfi = kzalloc(sizeof(*sfi), GFP_KERNEL);
1219 	if (!sfi) {
1220 		err = -ENOMEM;
1221 		goto free_gate;
1222 	}
1223 
1224 	refcount_set(&sfi->refcount, 1);
1225 	sfi->gate_id = sgi->index;
1226 	sfi->meter_id = ENETC_PSFP_WILDCARD;
1227 
1228 	/* Flow meter and max frame size */
1229 	if (entryp) {
1230 		if (entryp->police.burst) {
1231 			fmi = kzalloc(sizeof(*fmi), GFP_KERNEL);
1232 			if (!fmi) {
1233 				err = -ENOMEM;
1234 				goto free_sfi;
1235 			}
1236 			refcount_set(&fmi->refcount, 1);
1237 			fmi->cir = entryp->police.rate_bytes_ps;
1238 			fmi->cbs = entryp->police.burst;
1239 			fmi->index = entryp->police.index;
1240 			filter->flags |= ENETC_PSFP_FLAGS_FMI;
1241 			filter->fmi_index = fmi->index;
1242 			sfi->meter_id = fmi->index;
1243 		}
1244 
1245 		if (entryp->police.mtu)
1246 			sfi->maxsdu = entryp->police.mtu;
1247 	}
1248 
1249 	/* prio ref the filter prio */
1250 	if (f->common.prio && f->common.prio <= BIT(3))
1251 		sfi->prio = f->common.prio - 1;
1252 	else
1253 		sfi->prio = ENETC_PSFP_WILDCARD;
1254 
1255 	old_sfi = enetc_psfp_check_sfi(sfi);
1256 	if (!old_sfi) {
1257 		int index;
1258 
1259 		index = enetc_get_free_index(priv);
1260 		if (sfi->handle < 0) {
1261 			NL_SET_ERR_MSG_MOD(extack, "No Stream Filter resource!");
1262 			err = -ENOSPC;
1263 			goto free_fmi;
1264 		}
1265 
1266 		sfi->index = index;
1267 		sfi->handle = index + HANDLE_OFFSET;
1268 		/* Update the stream filter handle also */
1269 		filter->sid.handle = sfi->handle;
1270 		filter->sfi_index = sfi->index;
1271 		sfi_overwrite = 0;
1272 	} else {
1273 		filter->sfi_index = old_sfi->index;
1274 		filter->sid.handle = old_sfi->handle;
1275 		sfi_overwrite = 1;
1276 	}
1277 
1278 	err = enetc_psfp_hw_set(priv, &filter->sid,
1279 				sfi_overwrite ? NULL : sfi, sgi, fmi);
1280 	if (err)
1281 		goto free_fmi;
1282 
1283 	spin_lock(&epsfp.psfp_lock);
1284 	if (filter->flags & ENETC_PSFP_FLAGS_FMI) {
1285 		old_fmi = enetc_get_meter_by_index(filter->fmi_index);
1286 		if (old_fmi) {
1287 			fmi->refcount = old_fmi->refcount;
1288 			refcount_set(&fmi->refcount,
1289 				     refcount_read(&old_fmi->refcount) + 1);
1290 			hlist_del(&old_fmi->node);
1291 			kfree(old_fmi);
1292 		}
1293 		hlist_add_head(&fmi->node, &epsfp.psfp_meter_list);
1294 	}
1295 
1296 	/* Remove the old node if exist and update with a new node */
1297 	old_sgi = enetc_get_gate_by_index(filter->sgi_index);
1298 	if (old_sgi) {
1299 		refcount_set(&sgi->refcount,
1300 			     refcount_read(&old_sgi->refcount) + 1);
1301 		hlist_del(&old_sgi->node);
1302 		kfree(old_sgi);
1303 	}
1304 
1305 	hlist_add_head(&sgi->node, &epsfp.psfp_gate_list);
1306 
1307 	if (!old_sfi) {
1308 		hlist_add_head(&sfi->node, &epsfp.psfp_filter_list);
1309 		set_bit(sfi->index, epsfp.psfp_sfi_bitmap);
1310 	} else {
1311 		kfree(sfi);
1312 		refcount_inc(&old_sfi->refcount);
1313 	}
1314 
1315 	old_filter = enetc_get_stream_by_index(filter->sid.index);
1316 	if (old_filter)
1317 		remove_one_chain(priv, old_filter);
1318 
1319 	filter->stats.lastused = jiffies;
1320 	hlist_add_head(&filter->node, &epsfp.stream_list);
1321 
1322 	spin_unlock(&epsfp.psfp_lock);
1323 
1324 	return 0;
1325 
1326 free_fmi:
1327 	kfree(fmi);
1328 free_sfi:
1329 	kfree(sfi);
1330 free_gate:
1331 	kfree(sgi);
1332 free_filter:
1333 	kfree(filter);
1334 
1335 	return err;
1336 }
1337 
1338 static int enetc_config_clsflower(struct enetc_ndev_priv *priv,
1339 				  struct flow_cls_offload *cls_flower)
1340 {
1341 	struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1342 	struct netlink_ext_ack *extack = cls_flower->common.extack;
1343 	struct flow_dissector *dissector = rule->match.dissector;
1344 	struct flow_action *action = &rule->action;
1345 	struct flow_action_entry *entry;
1346 	struct actions_fwd *fwd;
1347 	u64 actions = 0;
1348 	int i, err;
1349 
1350 	if (!flow_action_has_entries(action)) {
1351 		NL_SET_ERR_MSG_MOD(extack, "At least one action is needed");
1352 		return -EINVAL;
1353 	}
1354 
1355 	flow_action_for_each(i, entry, action)
1356 		actions |= BIT(entry->id);
1357 
1358 	fwd = enetc_check_flow_actions(actions, dissector->used_keys);
1359 	if (!fwd) {
1360 		NL_SET_ERR_MSG_MOD(extack, "Unsupported filter type!");
1361 		return -EOPNOTSUPP;
1362 	}
1363 
1364 	if (fwd->output & FILTER_ACTION_TYPE_PSFP) {
1365 		err = enetc_psfp_parse_clsflower(priv, cls_flower);
1366 		if (err) {
1367 			NL_SET_ERR_MSG_MOD(extack, "Invalid PSFP inputs");
1368 			return err;
1369 		}
1370 	} else {
1371 		NL_SET_ERR_MSG_MOD(extack, "Unsupported actions");
1372 		return -EOPNOTSUPP;
1373 	}
1374 
1375 	return 0;
1376 }
1377 
1378 static int enetc_psfp_destroy_clsflower(struct enetc_ndev_priv *priv,
1379 					struct flow_cls_offload *f)
1380 {
1381 	struct enetc_stream_filter *filter;
1382 	struct netlink_ext_ack *extack = f->common.extack;
1383 	int err;
1384 
1385 	if (f->common.chain_index >= priv->psfp_cap.max_streamid) {
1386 		NL_SET_ERR_MSG_MOD(extack, "No Stream identify resource!");
1387 		return -ENOSPC;
1388 	}
1389 
1390 	filter = enetc_get_stream_by_index(f->common.chain_index);
1391 	if (!filter)
1392 		return -EINVAL;
1393 
1394 	err = enetc_streamid_hw_set(priv, &filter->sid, false);
1395 	if (err)
1396 		return err;
1397 
1398 	remove_one_chain(priv, filter);
1399 
1400 	return 0;
1401 }
1402 
1403 static int enetc_destroy_clsflower(struct enetc_ndev_priv *priv,
1404 				   struct flow_cls_offload *f)
1405 {
1406 	return enetc_psfp_destroy_clsflower(priv, f);
1407 }
1408 
1409 static int enetc_psfp_get_stats(struct enetc_ndev_priv *priv,
1410 				struct flow_cls_offload *f)
1411 {
1412 	struct psfp_streamfilter_counters counters = {};
1413 	struct enetc_stream_filter *filter;
1414 	struct flow_stats stats = {};
1415 	int err;
1416 
1417 	filter = enetc_get_stream_by_index(f->common.chain_index);
1418 	if (!filter)
1419 		return -EINVAL;
1420 
1421 	err = enetc_streamcounter_hw_get(priv, filter->sfi_index, &counters);
1422 	if (err)
1423 		return -EINVAL;
1424 
1425 	spin_lock(&epsfp.psfp_lock);
1426 	stats.pkts = counters.matching_frames_count +
1427 		     counters.not_passing_sdu_count -
1428 		     filter->stats.pkts;
1429 	stats.drops = counters.not_passing_frames_count +
1430 		      counters.not_passing_sdu_count +
1431 		      counters.red_frames_count -
1432 		      filter->stats.drops;
1433 	stats.lastused = filter->stats.lastused;
1434 	filter->stats.pkts += stats.pkts;
1435 	filter->stats.drops += stats.drops;
1436 	spin_unlock(&epsfp.psfp_lock);
1437 
1438 	flow_stats_update(&f->stats, 0x0, stats.pkts, stats.drops,
1439 			  stats.lastused, FLOW_ACTION_HW_STATS_DELAYED);
1440 
1441 	return 0;
1442 }
1443 
1444 static int enetc_setup_tc_cls_flower(struct enetc_ndev_priv *priv,
1445 				     struct flow_cls_offload *cls_flower)
1446 {
1447 	switch (cls_flower->command) {
1448 	case FLOW_CLS_REPLACE:
1449 		return enetc_config_clsflower(priv, cls_flower);
1450 	case FLOW_CLS_DESTROY:
1451 		return enetc_destroy_clsflower(priv, cls_flower);
1452 	case FLOW_CLS_STATS:
1453 		return enetc_psfp_get_stats(priv, cls_flower);
1454 	default:
1455 		return -EOPNOTSUPP;
1456 	}
1457 }
1458 
1459 static inline void clean_psfp_sfi_bitmap(void)
1460 {
1461 	bitmap_free(epsfp.psfp_sfi_bitmap);
1462 	epsfp.psfp_sfi_bitmap = NULL;
1463 }
1464 
1465 static void clean_stream_list(void)
1466 {
1467 	struct enetc_stream_filter *s;
1468 	struct hlist_node *tmp;
1469 
1470 	hlist_for_each_entry_safe(s, tmp, &epsfp.stream_list, node) {
1471 		hlist_del(&s->node);
1472 		kfree(s);
1473 	}
1474 }
1475 
1476 static void clean_sfi_list(void)
1477 {
1478 	struct enetc_psfp_filter *sfi;
1479 	struct hlist_node *tmp;
1480 
1481 	hlist_for_each_entry_safe(sfi, tmp, &epsfp.psfp_filter_list, node) {
1482 		hlist_del(&sfi->node);
1483 		kfree(sfi);
1484 	}
1485 }
1486 
1487 static void clean_sgi_list(void)
1488 {
1489 	struct enetc_psfp_gate *sgi;
1490 	struct hlist_node *tmp;
1491 
1492 	hlist_for_each_entry_safe(sgi, tmp, &epsfp.psfp_gate_list, node) {
1493 		hlist_del(&sgi->node);
1494 		kfree(sgi);
1495 	}
1496 }
1497 
1498 static void clean_psfp_all(void)
1499 {
1500 	/* Disable all list nodes and free all memory */
1501 	clean_sfi_list();
1502 	clean_sgi_list();
1503 	clean_stream_list();
1504 	epsfp.dev_bitmap = 0;
1505 	clean_psfp_sfi_bitmap();
1506 }
1507 
1508 int enetc_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1509 			    void *cb_priv)
1510 {
1511 	struct net_device *ndev = cb_priv;
1512 
1513 	if (!tc_can_offload(ndev))
1514 		return -EOPNOTSUPP;
1515 
1516 	switch (type) {
1517 	case TC_SETUP_CLSFLOWER:
1518 		return enetc_setup_tc_cls_flower(netdev_priv(ndev), type_data);
1519 	default:
1520 		return -EOPNOTSUPP;
1521 	}
1522 }
1523 
1524 int enetc_psfp_init(struct enetc_ndev_priv *priv)
1525 {
1526 	if (epsfp.psfp_sfi_bitmap)
1527 		return 0;
1528 
1529 	epsfp.psfp_sfi_bitmap = bitmap_zalloc(priv->psfp_cap.max_psfp_filter,
1530 					      GFP_KERNEL);
1531 	if (!epsfp.psfp_sfi_bitmap)
1532 		return -ENOMEM;
1533 
1534 	spin_lock_init(&epsfp.psfp_lock);
1535 
1536 	if (list_empty(&enetc_block_cb_list))
1537 		epsfp.dev_bitmap = 0;
1538 
1539 	return 0;
1540 }
1541 
1542 int enetc_psfp_clean(struct enetc_ndev_priv *priv)
1543 {
1544 	if (!list_empty(&enetc_block_cb_list))
1545 		return -EBUSY;
1546 
1547 	clean_psfp_all();
1548 
1549 	return 0;
1550 }
1551 
1552 int enetc_setup_tc_psfp(struct net_device *ndev, void *type_data)
1553 {
1554 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1555 	struct flow_block_offload *f = type_data;
1556 	int err;
1557 
1558 	err = flow_block_cb_setup_simple(f, &enetc_block_cb_list,
1559 					 enetc_setup_tc_block_cb,
1560 					 ndev, ndev, true);
1561 	if (err)
1562 		return err;
1563 
1564 	switch (f->command) {
1565 	case FLOW_BLOCK_BIND:
1566 		set_bit(enetc_get_port(priv), &epsfp.dev_bitmap);
1567 		break;
1568 	case FLOW_BLOCK_UNBIND:
1569 		clear_bit(enetc_get_port(priv), &epsfp.dev_bitmap);
1570 		if (!epsfp.dev_bitmap)
1571 			clean_psfp_all();
1572 		break;
1573 	}
1574 
1575 	return 0;
1576 }
1577