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