xref: /linux/drivers/net/ethernet/freescale/enetc/enetc_qos.c (revision 6beeaf48db6c548fcfc2ad32739d33af2fef3a5b)
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] = 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;
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 /* Stream Identity Entry Set Descriptor */
459 static int enetc_streamid_hw_set(struct enetc_ndev_priv *priv,
460 				 struct enetc_streamid *sid,
461 				 u8 enable)
462 {
463 	struct enetc_cbd cbd = {.cmd = 0};
464 	struct streamid_data *si_data;
465 	struct streamid_conf *si_conf;
466 	u16 data_size;
467 	dma_addr_t dma;
468 	int port;
469 	int err;
470 
471 	port = enetc_pf_to_port(priv->si->pdev);
472 	if (port < 0)
473 		return -EINVAL;
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] = cpu_to_le32(lower_32_bits(dma));
500 	cbd.addr[1] = cpu_to_le32(upper_32_bits(dma));
501 	eth_broadcast_addr(si_data->dmac);
502 	si_data->vid_vidm_tg = (ENETC_CBDR_SID_VID_MASK
503 			       + ((0x3 << 14) | ENETC_CBDR_SID_VIDM));
504 
505 	si_conf = &cbd.sid_set;
506 	/* Only one port supported for one entry, set itself */
507 	si_conf->iports = cpu_to_le32(1 << port);
508 	si_conf->id_type = 1;
509 	si_conf->oui[2] = 0x0;
510 	si_conf->oui[1] = 0x80;
511 	si_conf->oui[0] = 0xC2;
512 
513 	err = enetc_send_cmd(priv->si, &cbd);
514 	if (err)
515 		return -EINVAL;
516 
517 	if (!enable) {
518 		kfree(si_data);
519 		return 0;
520 	}
521 
522 	/* Enable the entry overwrite again incase space flushed by hardware */
523 	memset(&cbd, 0, sizeof(cbd));
524 
525 	cbd.index = cpu_to_le16((u16)sid->index);
526 	cbd.cmd = 0;
527 	cbd.cls = BDCR_CMD_STREAM_IDENTIFY;
528 	cbd.status_flags = 0;
529 
530 	si_conf->en = 0x80;
531 	si_conf->stream_handle = cpu_to_le32(sid->handle);
532 	si_conf->iports = cpu_to_le32(1 << port);
533 	si_conf->id_type = sid->filtertype;
534 	si_conf->oui[2] = 0x0;
535 	si_conf->oui[1] = 0x80;
536 	si_conf->oui[0] = 0xC2;
537 
538 	memset(si_data, 0, data_size);
539 
540 	cbd.length = cpu_to_le16(data_size);
541 
542 	cbd.addr[0] = cpu_to_le32(lower_32_bits(dma));
543 	cbd.addr[1] = cpu_to_le32(upper_32_bits(dma));
544 
545 	/* VIDM default to be 1.
546 	 * VID Match. If set (b1) then the VID must match, otherwise
547 	 * any VID is considered a match. VIDM setting is only used
548 	 * when TG is set to b01.
549 	 */
550 	if (si_conf->id_type == STREAMID_TYPE_NULL) {
551 		ether_addr_copy(si_data->dmac, sid->dst_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);
555 	} else if (si_conf->id_type == STREAMID_TYPE_SMAC) {
556 		ether_addr_copy(si_data->smac, sid->src_mac);
557 		si_data->vid_vidm_tg = (sid->vid & ENETC_CBDR_SID_VID_MASK) +
558 				       ((((u16)(sid->tagged) & 0x3) << 14)
559 				       | ENETC_CBDR_SID_VIDM);
560 	}
561 
562 	err = enetc_send_cmd(priv->si, &cbd);
563 	kfree(si_data);
564 
565 	return err;
566 }
567 
568 /* Stream Filter Instance Set Descriptor */
569 static int enetc_streamfilter_hw_set(struct enetc_ndev_priv *priv,
570 				     struct enetc_psfp_filter *sfi,
571 				     u8 enable)
572 {
573 	struct enetc_cbd cbd = {.cmd = 0};
574 	struct sfi_conf *sfi_config;
575 	int port;
576 
577 	port = enetc_pf_to_port(priv->si->pdev);
578 	if (port < 0)
579 		return -EINVAL;
580 
581 	cbd.index = cpu_to_le16(sfi->index);
582 	cbd.cls = BDCR_CMD_STREAM_FILTER;
583 	cbd.status_flags = 0x80;
584 	cbd.length = cpu_to_le16(1);
585 
586 	sfi_config = &cbd.sfi_conf;
587 	if (!enable)
588 		goto exit;
589 
590 	sfi_config->en = 0x80;
591 
592 	if (sfi->handle >= 0) {
593 		sfi_config->stream_handle =
594 			cpu_to_le32(sfi->handle);
595 		sfi_config->sthm |= 0x80;
596 	}
597 
598 	sfi_config->sg_inst_table_index = cpu_to_le16(sfi->gate_id);
599 	sfi_config->input_ports = cpu_to_le32(1 << port);
600 
601 	/* The priority value which may be matched against the
602 	 * frame’s priority value to determine a match for this entry.
603 	 */
604 	if (sfi->prio >= 0)
605 		sfi_config->multi |= (sfi->prio & 0x7) | 0x8;
606 
607 	/* Filter Type. Identifies the contents of the MSDU/FM_INST_INDEX
608 	 * field as being either an MSDU value or an index into the Flow
609 	 * Meter Instance table.
610 	 */
611 	if (sfi->maxsdu) {
612 		sfi_config->msdu =
613 		cpu_to_le16(sfi->maxsdu);
614 		sfi_config->multi |= 0x40;
615 	}
616 
617 	if (sfi->meter_id >= 0) {
618 		sfi_config->fm_inst_table_index = cpu_to_le16(sfi->meter_id);
619 		sfi_config->multi |= 0x80;
620 	}
621 
622 exit:
623 	return enetc_send_cmd(priv->si, &cbd);
624 }
625 
626 static int enetc_streamcounter_hw_get(struct enetc_ndev_priv *priv,
627 				      u32 index,
628 				      struct psfp_streamfilter_counters *cnt)
629 {
630 	struct enetc_cbd cbd = { .cmd = 2 };
631 	struct sfi_counter_data *data_buf;
632 	dma_addr_t dma;
633 	u16 data_size;
634 	int err;
635 
636 	cbd.index = cpu_to_le16((u16)index);
637 	cbd.cmd = 2;
638 	cbd.cls = BDCR_CMD_STREAM_FILTER;
639 	cbd.status_flags = 0;
640 
641 	data_size = sizeof(struct sfi_counter_data);
642 	data_buf = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
643 	if (!data_buf)
644 		return -ENOMEM;
645 
646 	dma = dma_map_single(&priv->si->pdev->dev, data_buf,
647 			     data_size, DMA_FROM_DEVICE);
648 	if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
649 		netdev_err(priv->si->ndev, "DMA mapping failed!\n");
650 		err = -ENOMEM;
651 		goto exit;
652 	}
653 	cbd.addr[0] = cpu_to_le32(lower_32_bits(dma));
654 	cbd.addr[1] = cpu_to_le32(upper_32_bits(dma));
655 
656 	cbd.length = cpu_to_le16(data_size);
657 
658 	err = enetc_send_cmd(priv->si, &cbd);
659 	if (err)
660 		goto exit;
661 
662 	cnt->matching_frames_count = ((u64)data_buf->matchh << 32) +
663 				     data_buf->matchl;
664 
665 	cnt->not_passing_sdu_count = ((u64)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)data_buf->stream_gate_droph << 32) +
673 				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 =	((u64)data_buf->flow_meter_droph << 32)	+
680 				data_buf->flow_meter_dropl;
681 
682 exit:
683 	kfree(data_buf);
684 	return err;
685 }
686 
687 static u64 get_ptp_now(struct enetc_hw *hw)
688 {
689 	u64 now_lo, now_hi, now;
690 
691 	now_lo = enetc_rd(hw, ENETC_SICTR0);
692 	now_hi = enetc_rd(hw, ENETC_SICTR1);
693 	now = now_lo | now_hi << 32;
694 
695 	return now;
696 }
697 
698 static int get_start_ns(u64 now, u64 cycle, u64 *start)
699 {
700 	u64 n;
701 
702 	if (!cycle)
703 		return -EFAULT;
704 
705 	n = div64_u64(now, cycle);
706 
707 	*start = (n + 1) * cycle;
708 
709 	return 0;
710 }
711 
712 /* Stream Gate Instance Set Descriptor */
713 static int enetc_streamgate_hw_set(struct enetc_ndev_priv *priv,
714 				   struct enetc_psfp_gate *sgi,
715 				   u8 enable)
716 {
717 	struct enetc_cbd cbd = { .cmd = 0 };
718 	struct sgi_table *sgi_config;
719 	struct sgcl_conf *sgcl_config;
720 	struct sgcl_data *sgcl_data;
721 	struct sgce *sgce;
722 	dma_addr_t dma;
723 	u16 data_size;
724 	int err, i;
725 	u64 now;
726 
727 	cbd.index = cpu_to_le16(sgi->index);
728 	cbd.cmd = 0;
729 	cbd.cls = BDCR_CMD_STREAM_GCL;
730 	cbd.status_flags = 0x80;
731 
732 	/* disable */
733 	if (!enable)
734 		return enetc_send_cmd(priv->si, &cbd);
735 
736 	if (!sgi->num_entries)
737 		return 0;
738 
739 	if (sgi->num_entries > priv->psfp_cap.max_psfp_gatelist ||
740 	    !sgi->cycletime)
741 		return -EINVAL;
742 
743 	/* enable */
744 	sgi_config = &cbd.sgi_table;
745 
746 	/* Keep open before gate list start */
747 	sgi_config->ocgtst = 0x80;
748 
749 	sgi_config->oipv = (sgi->init_ipv < 0) ?
750 				0x0 : ((sgi->init_ipv & 0x7) | 0x8);
751 
752 	sgi_config->en = 0x80;
753 
754 	/* Basic config */
755 	err = enetc_send_cmd(priv->si, &cbd);
756 	if (err)
757 		return -EINVAL;
758 
759 	memset(&cbd, 0, sizeof(cbd));
760 
761 	cbd.index = cpu_to_le16(sgi->index);
762 	cbd.cmd = 1;
763 	cbd.cls = BDCR_CMD_STREAM_GCL;
764 	cbd.status_flags = 0;
765 
766 	sgcl_config = &cbd.sgcl_conf;
767 
768 	sgcl_config->acl_len = (sgi->num_entries - 1) & 0x3;
769 
770 	data_size = struct_size(sgcl_data, sgcl, sgi->num_entries);
771 
772 	sgcl_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
773 	if (!sgcl_data)
774 		return -ENOMEM;
775 
776 	cbd.length = cpu_to_le16(data_size);
777 
778 	dma = dma_map_single(&priv->si->pdev->dev,
779 			     sgcl_data, data_size,
780 			     DMA_FROM_DEVICE);
781 	if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
782 		netdev_err(priv->si->ndev, "DMA mapping failed!\n");
783 		kfree(sgcl_data);
784 		return -ENOMEM;
785 	}
786 
787 	cbd.addr[0] = cpu_to_le32(lower_32_bits(dma));
788 	cbd.addr[1] = cpu_to_le32(upper_32_bits(dma));
789 
790 	sgce = &sgcl_data->sgcl[0];
791 
792 	sgcl_config->agtst = 0x80;
793 
794 	sgcl_data->ct = sgi->cycletime;
795 	sgcl_data->cte = sgi->cycletimext;
796 
797 	if (sgi->init_ipv >= 0)
798 		sgcl_config->aipv = (sgi->init_ipv & 0x7) | 0x8;
799 
800 	for (i = 0; i < sgi->num_entries; i++) {
801 		struct action_gate_entry *from = &sgi->entries[i];
802 		struct sgce *to = &sgce[i];
803 
804 		if (from->gate_state)
805 			to->multi |= 0x10;
806 
807 		if (from->ipv >= 0)
808 			to->multi |= ((from->ipv & 0x7) << 5) | 0x08;
809 
810 		if (from->maxoctets >= 0) {
811 			to->multi |= 0x01;
812 			to->msdu[0] = from->maxoctets & 0xFF;
813 			to->msdu[1] = (from->maxoctets >> 8) & 0xFF;
814 			to->msdu[2] = (from->maxoctets >> 16) & 0xFF;
815 		}
816 
817 		to->interval = from->interval;
818 	}
819 
820 	/* If basetime is less than now, calculate start time */
821 	now = get_ptp_now(&priv->si->hw);
822 
823 	if (sgi->basetime < now) {
824 		u64 start;
825 
826 		err = get_start_ns(now, sgi->cycletime, &start);
827 		if (err)
828 			goto exit;
829 		sgcl_data->btl = lower_32_bits(start);
830 		sgcl_data->bth = upper_32_bits(start);
831 	} else {
832 		u32 hi, lo;
833 
834 		hi = upper_32_bits(sgi->basetime);
835 		lo = lower_32_bits(sgi->basetime);
836 		sgcl_data->bth = hi;
837 		sgcl_data->btl = lo;
838 	}
839 
840 	err = enetc_send_cmd(priv->si, &cbd);
841 
842 exit:
843 	kfree(sgcl_data);
844 
845 	return err;
846 }
847 
848 static int enetc_flowmeter_hw_set(struct enetc_ndev_priv *priv,
849 				  struct enetc_psfp_meter *fmi,
850 				  u8 enable)
851 {
852 	struct enetc_cbd cbd = { .cmd = 0 };
853 	struct fmi_conf *fmi_config;
854 	u64 temp = 0;
855 
856 	cbd.index = cpu_to_le16((u16)fmi->index);
857 	cbd.cls = BDCR_CMD_FLOW_METER;
858 	cbd.status_flags = 0x80;
859 
860 	if (!enable)
861 		return enetc_send_cmd(priv->si, &cbd);
862 
863 	fmi_config = &cbd.fmi_conf;
864 	fmi_config->en = 0x80;
865 
866 	if (fmi->cir) {
867 		temp = (u64)8000 * fmi->cir;
868 		temp = div_u64(temp, 3725);
869 	}
870 
871 	fmi_config->cir = cpu_to_le32((u32)temp);
872 	fmi_config->cbs = cpu_to_le32(fmi->cbs);
873 
874 	/* Default for eir ebs disable */
875 	fmi_config->eir = 0;
876 	fmi_config->ebs = 0;
877 
878 	/* Default:
879 	 * mark red disable
880 	 * drop on yellow disable
881 	 * color mode disable
882 	 * couple flag disable
883 	 */
884 	fmi_config->conf = 0;
885 
886 	return enetc_send_cmd(priv->si, &cbd);
887 }
888 
889 static struct enetc_stream_filter *enetc_get_stream_by_index(u32 index)
890 {
891 	struct enetc_stream_filter *f;
892 
893 	hlist_for_each_entry(f, &epsfp.stream_list, node)
894 		if (f->sid.index == index)
895 			return f;
896 
897 	return NULL;
898 }
899 
900 static struct enetc_psfp_gate *enetc_get_gate_by_index(u32 index)
901 {
902 	struct enetc_psfp_gate *g;
903 
904 	hlist_for_each_entry(g, &epsfp.psfp_gate_list, node)
905 		if (g->index == index)
906 			return g;
907 
908 	return NULL;
909 }
910 
911 static struct enetc_psfp_filter *enetc_get_filter_by_index(u32 index)
912 {
913 	struct enetc_psfp_filter *s;
914 
915 	hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
916 		if (s->index == index)
917 			return s;
918 
919 	return NULL;
920 }
921 
922 static struct enetc_psfp_meter *enetc_get_meter_by_index(u32 index)
923 {
924 	struct enetc_psfp_meter *m;
925 
926 	hlist_for_each_entry(m, &epsfp.psfp_meter_list, node)
927 		if (m->index == index)
928 			return m;
929 
930 	return NULL;
931 }
932 
933 static struct enetc_psfp_filter
934 	*enetc_psfp_check_sfi(struct enetc_psfp_filter *sfi)
935 {
936 	struct enetc_psfp_filter *s;
937 
938 	hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
939 		if (s->gate_id == sfi->gate_id &&
940 		    s->prio == sfi->prio &&
941 		    s->maxsdu == sfi->maxsdu &&
942 		    s->meter_id == sfi->meter_id)
943 			return s;
944 
945 	return NULL;
946 }
947 
948 static int enetc_get_free_index(struct enetc_ndev_priv *priv)
949 {
950 	u32 max_size = priv->psfp_cap.max_psfp_filter;
951 	unsigned long index;
952 
953 	index = find_first_zero_bit(epsfp.psfp_sfi_bitmap, max_size);
954 	if (index == max_size)
955 		return -1;
956 
957 	return index;
958 }
959 
960 static void stream_filter_unref(struct enetc_ndev_priv *priv, u32 index)
961 {
962 	struct enetc_psfp_filter *sfi;
963 	u8 z;
964 
965 	sfi = enetc_get_filter_by_index(index);
966 	WARN_ON(!sfi);
967 	z = refcount_dec_and_test(&sfi->refcount);
968 
969 	if (z) {
970 		enetc_streamfilter_hw_set(priv, sfi, false);
971 		hlist_del(&sfi->node);
972 		kfree(sfi);
973 		clear_bit(index, epsfp.psfp_sfi_bitmap);
974 	}
975 }
976 
977 static void stream_gate_unref(struct enetc_ndev_priv *priv, u32 index)
978 {
979 	struct enetc_psfp_gate *sgi;
980 	u8 z;
981 
982 	sgi = enetc_get_gate_by_index(index);
983 	WARN_ON(!sgi);
984 	z = refcount_dec_and_test(&sgi->refcount);
985 	if (z) {
986 		enetc_streamgate_hw_set(priv, sgi, false);
987 		hlist_del(&sgi->node);
988 		kfree(sgi);
989 	}
990 }
991 
992 static void flow_meter_unref(struct enetc_ndev_priv *priv, u32 index)
993 {
994 	struct enetc_psfp_meter *fmi;
995 	u8 z;
996 
997 	fmi = enetc_get_meter_by_index(index);
998 	WARN_ON(!fmi);
999 	z = refcount_dec_and_test(&fmi->refcount);
1000 	if (z) {
1001 		enetc_flowmeter_hw_set(priv, fmi, false);
1002 		hlist_del(&fmi->node);
1003 		kfree(fmi);
1004 	}
1005 }
1006 
1007 static void remove_one_chain(struct enetc_ndev_priv *priv,
1008 			     struct enetc_stream_filter *filter)
1009 {
1010 	if (filter->flags & ENETC_PSFP_FLAGS_FMI)
1011 		flow_meter_unref(priv, filter->fmi_index);
1012 
1013 	stream_gate_unref(priv, filter->sgi_index);
1014 	stream_filter_unref(priv, filter->sfi_index);
1015 
1016 	hlist_del(&filter->node);
1017 	kfree(filter);
1018 }
1019 
1020 static int enetc_psfp_hw_set(struct enetc_ndev_priv *priv,
1021 			     struct enetc_streamid *sid,
1022 			     struct enetc_psfp_filter *sfi,
1023 			     struct enetc_psfp_gate *sgi,
1024 			     struct enetc_psfp_meter *fmi)
1025 {
1026 	int err;
1027 
1028 	err = enetc_streamid_hw_set(priv, sid, true);
1029 	if (err)
1030 		return err;
1031 
1032 	if (sfi) {
1033 		err = enetc_streamfilter_hw_set(priv, sfi, true);
1034 		if (err)
1035 			goto revert_sid;
1036 	}
1037 
1038 	err = enetc_streamgate_hw_set(priv, sgi, true);
1039 	if (err)
1040 		goto revert_sfi;
1041 
1042 	if (fmi) {
1043 		err = enetc_flowmeter_hw_set(priv, fmi, true);
1044 		if (err)
1045 			goto revert_sgi;
1046 	}
1047 
1048 	return 0;
1049 
1050 revert_sgi:
1051 	enetc_streamgate_hw_set(priv, sgi, false);
1052 revert_sfi:
1053 	if (sfi)
1054 		enetc_streamfilter_hw_set(priv, sfi, false);
1055 revert_sid:
1056 	enetc_streamid_hw_set(priv, sid, false);
1057 	return err;
1058 }
1059 
1060 static struct actions_fwd *enetc_check_flow_actions(u64 acts,
1061 						    unsigned int inputkeys)
1062 {
1063 	int i;
1064 
1065 	for (i = 0; i < ARRAY_SIZE(enetc_act_fwd); i++)
1066 		if (acts == enetc_act_fwd[i].actions &&
1067 		    inputkeys & enetc_act_fwd[i].keys)
1068 			return &enetc_act_fwd[i];
1069 
1070 	return NULL;
1071 }
1072 
1073 static int enetc_psfp_parse_clsflower(struct enetc_ndev_priv *priv,
1074 				      struct flow_cls_offload *f)
1075 {
1076 	struct flow_action_entry *entryg = NULL, *entryp = NULL;
1077 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1078 	struct netlink_ext_ack *extack = f->common.extack;
1079 	struct enetc_stream_filter *filter, *old_filter;
1080 	struct enetc_psfp_meter *fmi = NULL, *old_fmi;
1081 	struct enetc_psfp_filter *sfi, *old_sfi;
1082 	struct enetc_psfp_gate *sgi, *old_sgi;
1083 	struct flow_action_entry *entry;
1084 	struct action_gate_entry *e;
1085 	u8 sfi_overwrite = 0;
1086 	int entries_size;
1087 	int i, err;
1088 
1089 	if (f->common.chain_index >= priv->psfp_cap.max_streamid) {
1090 		NL_SET_ERR_MSG_MOD(extack, "No Stream identify resource!");
1091 		return -ENOSPC;
1092 	}
1093 
1094 	flow_action_for_each(i, entry, &rule->action)
1095 		if (entry->id == FLOW_ACTION_GATE)
1096 			entryg = entry;
1097 		else if (entry->id == FLOW_ACTION_POLICE)
1098 			entryp = entry;
1099 
1100 	/* Not support without gate action */
1101 	if (!entryg)
1102 		return -EINVAL;
1103 
1104 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1105 	if (!filter)
1106 		return -ENOMEM;
1107 
1108 	filter->sid.index = f->common.chain_index;
1109 
1110 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1111 		struct flow_match_eth_addrs match;
1112 
1113 		flow_rule_match_eth_addrs(rule, &match);
1114 
1115 		if (!is_zero_ether_addr(match.mask->dst) &&
1116 		    !is_zero_ether_addr(match.mask->src)) {
1117 			NL_SET_ERR_MSG_MOD(extack,
1118 					   "Cannot match on both source and destination MAC");
1119 			err = -EINVAL;
1120 			goto free_filter;
1121 		}
1122 
1123 		if (!is_zero_ether_addr(match.mask->dst)) {
1124 			if (!is_broadcast_ether_addr(match.mask->dst)) {
1125 				NL_SET_ERR_MSG_MOD(extack,
1126 						   "Masked matching on destination MAC not supported");
1127 				err = -EINVAL;
1128 				goto free_filter;
1129 			}
1130 			ether_addr_copy(filter->sid.dst_mac, match.key->dst);
1131 			filter->sid.filtertype = STREAMID_TYPE_NULL;
1132 		}
1133 
1134 		if (!is_zero_ether_addr(match.mask->src)) {
1135 			if (!is_broadcast_ether_addr(match.mask->src)) {
1136 				NL_SET_ERR_MSG_MOD(extack,
1137 						   "Masked matching on source MAC not supported");
1138 				err = -EINVAL;
1139 				goto free_filter;
1140 			}
1141 			ether_addr_copy(filter->sid.src_mac, match.key->src);
1142 			filter->sid.filtertype = STREAMID_TYPE_SMAC;
1143 		}
1144 	} else {
1145 		NL_SET_ERR_MSG_MOD(extack, "Unsupported, must include ETH_ADDRS");
1146 		err = -EINVAL;
1147 		goto free_filter;
1148 	}
1149 
1150 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
1151 		struct flow_match_vlan match;
1152 
1153 		flow_rule_match_vlan(rule, &match);
1154 		if (match.mask->vlan_priority) {
1155 			if (match.mask->vlan_priority !=
1156 			    (VLAN_PRIO_MASK >> VLAN_PRIO_SHIFT)) {
1157 				NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN priority");
1158 				err = -EINVAL;
1159 				goto free_filter;
1160 			}
1161 		}
1162 
1163 		if (match.mask->vlan_id) {
1164 			if (match.mask->vlan_id != VLAN_VID_MASK) {
1165 				NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN id");
1166 				err = -EINVAL;
1167 				goto free_filter;
1168 			}
1169 
1170 			filter->sid.vid = match.key->vlan_id;
1171 			if (!filter->sid.vid)
1172 				filter->sid.tagged = STREAMID_VLAN_UNTAGGED;
1173 			else
1174 				filter->sid.tagged = STREAMID_VLAN_TAGGED;
1175 		}
1176 	} else {
1177 		filter->sid.tagged = STREAMID_VLAN_ALL;
1178 	}
1179 
1180 	/* parsing gate action */
1181 	if (entryg->gate.index >= priv->psfp_cap.max_psfp_gate) {
1182 		NL_SET_ERR_MSG_MOD(extack, "No Stream Gate resource!");
1183 		err = -ENOSPC;
1184 		goto free_filter;
1185 	}
1186 
1187 	if (entryg->gate.num_entries >= priv->psfp_cap.max_psfp_gatelist) {
1188 		NL_SET_ERR_MSG_MOD(extack, "No Stream Gate resource!");
1189 		err = -ENOSPC;
1190 		goto free_filter;
1191 	}
1192 
1193 	entries_size = struct_size(sgi, entries, entryg->gate.num_entries);
1194 	sgi = kzalloc(entries_size, GFP_KERNEL);
1195 	if (!sgi) {
1196 		err = -ENOMEM;
1197 		goto free_filter;
1198 	}
1199 
1200 	refcount_set(&sgi->refcount, 1);
1201 	sgi->index = entryg->gate.index;
1202 	sgi->init_ipv = entryg->gate.prio;
1203 	sgi->basetime = entryg->gate.basetime;
1204 	sgi->cycletime = entryg->gate.cycletime;
1205 	sgi->num_entries = entryg->gate.num_entries;
1206 
1207 	e = sgi->entries;
1208 	for (i = 0; i < entryg->gate.num_entries; i++) {
1209 		e[i].gate_state = entryg->gate.entries[i].gate_state;
1210 		e[i].interval = entryg->gate.entries[i].interval;
1211 		e[i].ipv = entryg->gate.entries[i].ipv;
1212 		e[i].maxoctets = entryg->gate.entries[i].maxoctets;
1213 	}
1214 
1215 	filter->sgi_index = sgi->index;
1216 
1217 	sfi = kzalloc(sizeof(*sfi), GFP_KERNEL);
1218 	if (!sfi) {
1219 		err = -ENOMEM;
1220 		goto free_gate;
1221 	}
1222 
1223 	refcount_set(&sfi->refcount, 1);
1224 	sfi->gate_id = sgi->index;
1225 	sfi->meter_id = ENETC_PSFP_WILDCARD;
1226 
1227 	/* Flow meter and max frame size */
1228 	if (entryp) {
1229 		if (entryp->police.rate_pkt_ps) {
1230 			NL_SET_ERR_MSG_MOD(extack, "QoS offload not support packets per second");
1231 			err = -EOPNOTSUPP;
1232 			goto free_sfi;
1233 		}
1234 		if (entryp->police.burst) {
1235 			fmi = kzalloc(sizeof(*fmi), GFP_KERNEL);
1236 			if (!fmi) {
1237 				err = -ENOMEM;
1238 				goto free_sfi;
1239 			}
1240 			refcount_set(&fmi->refcount, 1);
1241 			fmi->cir = entryp->police.rate_bytes_ps;
1242 			fmi->cbs = entryp->police.burst;
1243 			fmi->index = entryp->police.index;
1244 			filter->flags |= ENETC_PSFP_FLAGS_FMI;
1245 			filter->fmi_index = fmi->index;
1246 			sfi->meter_id = fmi->index;
1247 		}
1248 
1249 		if (entryp->police.mtu)
1250 			sfi->maxsdu = entryp->police.mtu;
1251 	}
1252 
1253 	/* prio ref the filter prio */
1254 	if (f->common.prio && f->common.prio <= BIT(3))
1255 		sfi->prio = f->common.prio - 1;
1256 	else
1257 		sfi->prio = ENETC_PSFP_WILDCARD;
1258 
1259 	old_sfi = enetc_psfp_check_sfi(sfi);
1260 	if (!old_sfi) {
1261 		int index;
1262 
1263 		index = enetc_get_free_index(priv);
1264 		if (sfi->handle < 0) {
1265 			NL_SET_ERR_MSG_MOD(extack, "No Stream Filter resource!");
1266 			err = -ENOSPC;
1267 			goto free_fmi;
1268 		}
1269 
1270 		sfi->index = index;
1271 		sfi->handle = index + HANDLE_OFFSET;
1272 		/* Update the stream filter handle also */
1273 		filter->sid.handle = sfi->handle;
1274 		filter->sfi_index = sfi->index;
1275 		sfi_overwrite = 0;
1276 	} else {
1277 		filter->sfi_index = old_sfi->index;
1278 		filter->sid.handle = old_sfi->handle;
1279 		sfi_overwrite = 1;
1280 	}
1281 
1282 	err = enetc_psfp_hw_set(priv, &filter->sid,
1283 				sfi_overwrite ? NULL : sfi, sgi, fmi);
1284 	if (err)
1285 		goto free_fmi;
1286 
1287 	spin_lock(&epsfp.psfp_lock);
1288 	if (filter->flags & ENETC_PSFP_FLAGS_FMI) {
1289 		old_fmi = enetc_get_meter_by_index(filter->fmi_index);
1290 		if (old_fmi) {
1291 			fmi->refcount = old_fmi->refcount;
1292 			refcount_set(&fmi->refcount,
1293 				     refcount_read(&old_fmi->refcount) + 1);
1294 			hlist_del(&old_fmi->node);
1295 			kfree(old_fmi);
1296 		}
1297 		hlist_add_head(&fmi->node, &epsfp.psfp_meter_list);
1298 	}
1299 
1300 	/* Remove the old node if exist and update with a new node */
1301 	old_sgi = enetc_get_gate_by_index(filter->sgi_index);
1302 	if (old_sgi) {
1303 		refcount_set(&sgi->refcount,
1304 			     refcount_read(&old_sgi->refcount) + 1);
1305 		hlist_del(&old_sgi->node);
1306 		kfree(old_sgi);
1307 	}
1308 
1309 	hlist_add_head(&sgi->node, &epsfp.psfp_gate_list);
1310 
1311 	if (!old_sfi) {
1312 		hlist_add_head(&sfi->node, &epsfp.psfp_filter_list);
1313 		set_bit(sfi->index, epsfp.psfp_sfi_bitmap);
1314 	} else {
1315 		kfree(sfi);
1316 		refcount_inc(&old_sfi->refcount);
1317 	}
1318 
1319 	old_filter = enetc_get_stream_by_index(filter->sid.index);
1320 	if (old_filter)
1321 		remove_one_chain(priv, old_filter);
1322 
1323 	filter->stats.lastused = jiffies;
1324 	hlist_add_head(&filter->node, &epsfp.stream_list);
1325 
1326 	spin_unlock(&epsfp.psfp_lock);
1327 
1328 	return 0;
1329 
1330 free_fmi:
1331 	kfree(fmi);
1332 free_sfi:
1333 	kfree(sfi);
1334 free_gate:
1335 	kfree(sgi);
1336 free_filter:
1337 	kfree(filter);
1338 
1339 	return err;
1340 }
1341 
1342 static int enetc_config_clsflower(struct enetc_ndev_priv *priv,
1343 				  struct flow_cls_offload *cls_flower)
1344 {
1345 	struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1346 	struct netlink_ext_ack *extack = cls_flower->common.extack;
1347 	struct flow_dissector *dissector = rule->match.dissector;
1348 	struct flow_action *action = &rule->action;
1349 	struct flow_action_entry *entry;
1350 	struct actions_fwd *fwd;
1351 	u64 actions = 0;
1352 	int i, err;
1353 
1354 	if (!flow_action_has_entries(action)) {
1355 		NL_SET_ERR_MSG_MOD(extack, "At least one action is needed");
1356 		return -EINVAL;
1357 	}
1358 
1359 	flow_action_for_each(i, entry, action)
1360 		actions |= BIT(entry->id);
1361 
1362 	fwd = enetc_check_flow_actions(actions, dissector->used_keys);
1363 	if (!fwd) {
1364 		NL_SET_ERR_MSG_MOD(extack, "Unsupported filter type!");
1365 		return -EOPNOTSUPP;
1366 	}
1367 
1368 	if (fwd->output & FILTER_ACTION_TYPE_PSFP) {
1369 		err = enetc_psfp_parse_clsflower(priv, cls_flower);
1370 		if (err) {
1371 			NL_SET_ERR_MSG_MOD(extack, "Invalid PSFP inputs");
1372 			return err;
1373 		}
1374 	} else {
1375 		NL_SET_ERR_MSG_MOD(extack, "Unsupported actions");
1376 		return -EOPNOTSUPP;
1377 	}
1378 
1379 	return 0;
1380 }
1381 
1382 static int enetc_psfp_destroy_clsflower(struct enetc_ndev_priv *priv,
1383 					struct flow_cls_offload *f)
1384 {
1385 	struct enetc_stream_filter *filter;
1386 	struct netlink_ext_ack *extack = f->common.extack;
1387 	int err;
1388 
1389 	if (f->common.chain_index >= priv->psfp_cap.max_streamid) {
1390 		NL_SET_ERR_MSG_MOD(extack, "No Stream identify resource!");
1391 		return -ENOSPC;
1392 	}
1393 
1394 	filter = enetc_get_stream_by_index(f->common.chain_index);
1395 	if (!filter)
1396 		return -EINVAL;
1397 
1398 	err = enetc_streamid_hw_set(priv, &filter->sid, false);
1399 	if (err)
1400 		return err;
1401 
1402 	remove_one_chain(priv, filter);
1403 
1404 	return 0;
1405 }
1406 
1407 static int enetc_destroy_clsflower(struct enetc_ndev_priv *priv,
1408 				   struct flow_cls_offload *f)
1409 {
1410 	return enetc_psfp_destroy_clsflower(priv, f);
1411 }
1412 
1413 static int enetc_psfp_get_stats(struct enetc_ndev_priv *priv,
1414 				struct flow_cls_offload *f)
1415 {
1416 	struct psfp_streamfilter_counters counters = {};
1417 	struct enetc_stream_filter *filter;
1418 	struct flow_stats stats = {};
1419 	int err;
1420 
1421 	filter = enetc_get_stream_by_index(f->common.chain_index);
1422 	if (!filter)
1423 		return -EINVAL;
1424 
1425 	err = enetc_streamcounter_hw_get(priv, filter->sfi_index, &counters);
1426 	if (err)
1427 		return -EINVAL;
1428 
1429 	spin_lock(&epsfp.psfp_lock);
1430 	stats.pkts = counters.matching_frames_count +
1431 		     counters.not_passing_sdu_count -
1432 		     filter->stats.pkts;
1433 	stats.drops = counters.not_passing_frames_count +
1434 		      counters.not_passing_sdu_count +
1435 		      counters.red_frames_count -
1436 		      filter->stats.drops;
1437 	stats.lastused = filter->stats.lastused;
1438 	filter->stats.pkts += stats.pkts;
1439 	filter->stats.drops += stats.drops;
1440 	spin_unlock(&epsfp.psfp_lock);
1441 
1442 	flow_stats_update(&f->stats, 0x0, stats.pkts, stats.drops,
1443 			  stats.lastused, FLOW_ACTION_HW_STATS_DELAYED);
1444 
1445 	return 0;
1446 }
1447 
1448 static int enetc_setup_tc_cls_flower(struct enetc_ndev_priv *priv,
1449 				     struct flow_cls_offload *cls_flower)
1450 {
1451 	switch (cls_flower->command) {
1452 	case FLOW_CLS_REPLACE:
1453 		return enetc_config_clsflower(priv, cls_flower);
1454 	case FLOW_CLS_DESTROY:
1455 		return enetc_destroy_clsflower(priv, cls_flower);
1456 	case FLOW_CLS_STATS:
1457 		return enetc_psfp_get_stats(priv, cls_flower);
1458 	default:
1459 		return -EOPNOTSUPP;
1460 	}
1461 }
1462 
1463 static inline void clean_psfp_sfi_bitmap(void)
1464 {
1465 	bitmap_free(epsfp.psfp_sfi_bitmap);
1466 	epsfp.psfp_sfi_bitmap = NULL;
1467 }
1468 
1469 static void clean_stream_list(void)
1470 {
1471 	struct enetc_stream_filter *s;
1472 	struct hlist_node *tmp;
1473 
1474 	hlist_for_each_entry_safe(s, tmp, &epsfp.stream_list, node) {
1475 		hlist_del(&s->node);
1476 		kfree(s);
1477 	}
1478 }
1479 
1480 static void clean_sfi_list(void)
1481 {
1482 	struct enetc_psfp_filter *sfi;
1483 	struct hlist_node *tmp;
1484 
1485 	hlist_for_each_entry_safe(sfi, tmp, &epsfp.psfp_filter_list, node) {
1486 		hlist_del(&sfi->node);
1487 		kfree(sfi);
1488 	}
1489 }
1490 
1491 static void clean_sgi_list(void)
1492 {
1493 	struct enetc_psfp_gate *sgi;
1494 	struct hlist_node *tmp;
1495 
1496 	hlist_for_each_entry_safe(sgi, tmp, &epsfp.psfp_gate_list, node) {
1497 		hlist_del(&sgi->node);
1498 		kfree(sgi);
1499 	}
1500 }
1501 
1502 static void clean_psfp_all(void)
1503 {
1504 	/* Disable all list nodes and free all memory */
1505 	clean_sfi_list();
1506 	clean_sgi_list();
1507 	clean_stream_list();
1508 	epsfp.dev_bitmap = 0;
1509 	clean_psfp_sfi_bitmap();
1510 }
1511 
1512 int enetc_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1513 			    void *cb_priv)
1514 {
1515 	struct net_device *ndev = cb_priv;
1516 
1517 	if (!tc_can_offload(ndev))
1518 		return -EOPNOTSUPP;
1519 
1520 	switch (type) {
1521 	case TC_SETUP_CLSFLOWER:
1522 		return enetc_setup_tc_cls_flower(netdev_priv(ndev), type_data);
1523 	default:
1524 		return -EOPNOTSUPP;
1525 	}
1526 }
1527 
1528 int enetc_psfp_init(struct enetc_ndev_priv *priv)
1529 {
1530 	if (epsfp.psfp_sfi_bitmap)
1531 		return 0;
1532 
1533 	epsfp.psfp_sfi_bitmap = bitmap_zalloc(priv->psfp_cap.max_psfp_filter,
1534 					      GFP_KERNEL);
1535 	if (!epsfp.psfp_sfi_bitmap)
1536 		return -ENOMEM;
1537 
1538 	spin_lock_init(&epsfp.psfp_lock);
1539 
1540 	if (list_empty(&enetc_block_cb_list))
1541 		epsfp.dev_bitmap = 0;
1542 
1543 	return 0;
1544 }
1545 
1546 int enetc_psfp_clean(struct enetc_ndev_priv *priv)
1547 {
1548 	if (!list_empty(&enetc_block_cb_list))
1549 		return -EBUSY;
1550 
1551 	clean_psfp_all();
1552 
1553 	return 0;
1554 }
1555 
1556 int enetc_setup_tc_psfp(struct net_device *ndev, void *type_data)
1557 {
1558 	struct enetc_ndev_priv *priv = netdev_priv(ndev);
1559 	struct flow_block_offload *f = type_data;
1560 	int port, err;
1561 
1562 	err = flow_block_cb_setup_simple(f, &enetc_block_cb_list,
1563 					 enetc_setup_tc_block_cb,
1564 					 ndev, ndev, true);
1565 	if (err)
1566 		return err;
1567 
1568 	switch (f->command) {
1569 	case FLOW_BLOCK_BIND:
1570 		port = enetc_pf_to_port(priv->si->pdev);
1571 		if (port < 0)
1572 			return -EINVAL;
1573 
1574 		set_bit(port, &epsfp.dev_bitmap);
1575 		break;
1576 	case FLOW_BLOCK_UNBIND:
1577 		port = enetc_pf_to_port(priv->si->pdev);
1578 		if (port < 0)
1579 			return -EINVAL;
1580 
1581 		clear_bit(port, &epsfp.dev_bitmap);
1582 		if (!epsfp.dev_bitmap)
1583 			clean_psfp_all();
1584 		break;
1585 	}
1586 
1587 	return 0;
1588 }
1589