• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
enetc_get_max_gcl_len(struct enetc_hw * hw)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 
enetc_sched_speed_set(struct enetc_ndev_priv * priv,int speed)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 
enetc_setup_taprio(struct net_device * ndev,struct tc_taprio_qopt_offload * admin_conf)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 
enetc_setup_tc_taprio(struct net_device * ndev,void * type_data)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 
enetc_get_cbs_enable(struct enetc_hw * hw,u8 tc)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 
enetc_get_cbs_bw(struct enetc_hw * hw,u8 tc)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 
enetc_setup_tc_cbs(struct net_device * ndev,void * type_data)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 
enetc_setup_tc_txtime(struct net_device * ndev,void * type_data)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 
enetc_get_port(struct enetc_ndev_priv * priv)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 */
enetc_streamid_hw_set(struct enetc_ndev_priv * priv,struct enetc_streamid * sid,u8 enable)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 	if (!si_data)
490 		return -ENOMEM;
491 	cbd.length = cpu_to_le16(data_size);
492 
493 	dma = dma_map_single(&priv->si->pdev->dev, si_data,
494 			     data_size, DMA_FROM_DEVICE);
495 	if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
496 		netdev_err(priv->si->ndev, "DMA mapping failed!\n");
497 		err = -ENOMEM;
498 		goto out;
499 	}
500 
501 	cbd.addr[0] = lower_32_bits(dma);
502 	cbd.addr[1] = upper_32_bits(dma);
503 	eth_broadcast_addr(si_data->dmac);
504 	si_data->vid_vidm_tg =
505 		cpu_to_le16(ENETC_CBDR_SID_VID_MASK
506 			    + ((0x3 << 14) | ENETC_CBDR_SID_VIDM));
507 
508 	si_conf = &cbd.sid_set;
509 	/* Only one port supported for one entry, set itself */
510 	si_conf->iports = 1 << enetc_get_port(priv);
511 	si_conf->id_type = 1;
512 	si_conf->oui[2] = 0x0;
513 	si_conf->oui[1] = 0x80;
514 	si_conf->oui[0] = 0xC2;
515 
516 	err = enetc_send_cmd(priv->si, &cbd);
517 	if (err)
518 		goto out;
519 
520 	if (!enable)
521 		goto out;
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 out:
567 	if (!dma_mapping_error(&priv->si->pdev->dev, dma))
568 		dma_unmap_single(&priv->si->pdev->dev, dma, data_size, DMA_FROM_DEVICE);
569 
570 	kfree(si_data);
571 
572 	return err;
573 }
574 
575 /* Stream Filter Instance Set Descriptor */
enetc_streamfilter_hw_set(struct enetc_ndev_priv * priv,struct enetc_psfp_filter * sfi,u8 enable)576 static int enetc_streamfilter_hw_set(struct enetc_ndev_priv *priv,
577 				     struct enetc_psfp_filter *sfi,
578 				     u8 enable)
579 {
580 	struct enetc_cbd cbd = {.cmd = 0};
581 	struct sfi_conf *sfi_config;
582 
583 	cbd.index = cpu_to_le16(sfi->index);
584 	cbd.cls = BDCR_CMD_STREAM_FILTER;
585 	cbd.status_flags = 0x80;
586 	cbd.length = cpu_to_le16(1);
587 
588 	sfi_config = &cbd.sfi_conf;
589 	if (!enable)
590 		goto exit;
591 
592 	sfi_config->en = 0x80;
593 
594 	if (sfi->handle >= 0) {
595 		sfi_config->stream_handle =
596 			cpu_to_le32(sfi->handle);
597 		sfi_config->sthm |= 0x80;
598 	}
599 
600 	sfi_config->sg_inst_table_index = cpu_to_le16(sfi->gate_id);
601 	sfi_config->input_ports = 1 << enetc_get_port(priv);
602 
603 	/* The priority value which may be matched against the
604 	 * frame’s priority value to determine a match for this entry.
605 	 */
606 	if (sfi->prio >= 0)
607 		sfi_config->multi |= (sfi->prio & 0x7) | 0x8;
608 
609 	/* Filter Type. Identifies the contents of the MSDU/FM_INST_INDEX
610 	 * field as being either an MSDU value or an index into the Flow
611 	 * Meter Instance table.
612 	 */
613 	if (sfi->maxsdu) {
614 		sfi_config->msdu =
615 		cpu_to_le16(sfi->maxsdu);
616 		sfi_config->multi |= 0x40;
617 	}
618 
619 	if (sfi->meter_id >= 0) {
620 		sfi_config->fm_inst_table_index = cpu_to_le16(sfi->meter_id);
621 		sfi_config->multi |= 0x80;
622 	}
623 
624 exit:
625 	return enetc_send_cmd(priv->si, &cbd);
626 }
627 
enetc_streamcounter_hw_get(struct enetc_ndev_priv * priv,u32 index,struct psfp_streamfilter_counters * cnt)628 static int enetc_streamcounter_hw_get(struct enetc_ndev_priv *priv,
629 				      u32 index,
630 				      struct psfp_streamfilter_counters *cnt)
631 {
632 	struct enetc_cbd cbd = { .cmd = 2 };
633 	struct sfi_counter_data *data_buf;
634 	dma_addr_t dma;
635 	u16 data_size;
636 	int err;
637 
638 	cbd.index = cpu_to_le16((u16)index);
639 	cbd.cmd = 2;
640 	cbd.cls = BDCR_CMD_STREAM_FILTER;
641 	cbd.status_flags = 0;
642 
643 	data_size = sizeof(struct sfi_counter_data);
644 	data_buf = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
645 	if (!data_buf)
646 		return -ENOMEM;
647 
648 	dma = dma_map_single(&priv->si->pdev->dev, data_buf,
649 			     data_size, DMA_FROM_DEVICE);
650 	if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
651 		netdev_err(priv->si->ndev, "DMA mapping failed!\n");
652 		err = -ENOMEM;
653 		goto exit;
654 	}
655 	cbd.addr[0] = lower_32_bits(dma);
656 	cbd.addr[1] = upper_32_bits(dma);
657 
658 	cbd.length = cpu_to_le16(data_size);
659 
660 	err = enetc_send_cmd(priv->si, &cbd);
661 	if (err)
662 		goto exit;
663 
664 	cnt->matching_frames_count =
665 			((u64)le32_to_cpu(data_buf->matchh) << 32)
666 			+ data_buf->matchl;
667 
668 	cnt->not_passing_sdu_count =
669 			((u64)le32_to_cpu(data_buf->msdu_droph) << 32)
670 			+ data_buf->msdu_dropl;
671 
672 	cnt->passing_sdu_count = cnt->matching_frames_count
673 				- cnt->not_passing_sdu_count;
674 
675 	cnt->not_passing_frames_count =
676 		((u64)le32_to_cpu(data_buf->stream_gate_droph) << 32)
677 		+ le32_to_cpu(data_buf->stream_gate_dropl);
678 
679 	cnt->passing_frames_count = cnt->matching_frames_count
680 				- cnt->not_passing_sdu_count
681 				- cnt->not_passing_frames_count;
682 
683 	cnt->red_frames_count =
684 		((u64)le32_to_cpu(data_buf->flow_meter_droph) << 32)
685 		+ le32_to_cpu(data_buf->flow_meter_dropl);
686 
687 exit:
688 	kfree(data_buf);
689 	return err;
690 }
691 
get_ptp_now(struct enetc_hw * hw)692 static u64 get_ptp_now(struct enetc_hw *hw)
693 {
694 	u64 now_lo, now_hi, now;
695 
696 	now_lo = enetc_rd(hw, ENETC_SICTR0);
697 	now_hi = enetc_rd(hw, ENETC_SICTR1);
698 	now = now_lo | now_hi << 32;
699 
700 	return now;
701 }
702 
get_start_ns(u64 now,u64 cycle,u64 * start)703 static int get_start_ns(u64 now, u64 cycle, u64 *start)
704 {
705 	u64 n;
706 
707 	if (!cycle)
708 		return -EFAULT;
709 
710 	n = div64_u64(now, cycle);
711 
712 	*start = (n + 1) * cycle;
713 
714 	return 0;
715 }
716 
717 /* Stream Gate Instance Set Descriptor */
enetc_streamgate_hw_set(struct enetc_ndev_priv * priv,struct enetc_psfp_gate * sgi,u8 enable)718 static int enetc_streamgate_hw_set(struct enetc_ndev_priv *priv,
719 				   struct enetc_psfp_gate *sgi,
720 				   u8 enable)
721 {
722 	struct enetc_cbd cbd = { .cmd = 0 };
723 	struct sgi_table *sgi_config;
724 	struct sgcl_conf *sgcl_config;
725 	struct sgcl_data *sgcl_data;
726 	struct sgce *sgce;
727 	dma_addr_t dma;
728 	u16 data_size;
729 	int err, i;
730 	u64 now;
731 
732 	cbd.index = cpu_to_le16(sgi->index);
733 	cbd.cmd = 0;
734 	cbd.cls = BDCR_CMD_STREAM_GCL;
735 	cbd.status_flags = 0x80;
736 
737 	/* disable */
738 	if (!enable)
739 		return enetc_send_cmd(priv->si, &cbd);
740 
741 	if (!sgi->num_entries)
742 		return 0;
743 
744 	if (sgi->num_entries > priv->psfp_cap.max_psfp_gatelist ||
745 	    !sgi->cycletime)
746 		return -EINVAL;
747 
748 	/* enable */
749 	sgi_config = &cbd.sgi_table;
750 
751 	/* Keep open before gate list start */
752 	sgi_config->ocgtst = 0x80;
753 
754 	sgi_config->oipv = (sgi->init_ipv < 0) ?
755 				0x0 : ((sgi->init_ipv & 0x7) | 0x8);
756 
757 	sgi_config->en = 0x80;
758 
759 	/* Basic config */
760 	err = enetc_send_cmd(priv->si, &cbd);
761 	if (err)
762 		return -EINVAL;
763 
764 	memset(&cbd, 0, sizeof(cbd));
765 
766 	cbd.index = cpu_to_le16(sgi->index);
767 	cbd.cmd = 1;
768 	cbd.cls = BDCR_CMD_STREAM_GCL;
769 	cbd.status_flags = 0;
770 
771 	sgcl_config = &cbd.sgcl_conf;
772 
773 	sgcl_config->acl_len = (sgi->num_entries - 1) & 0x3;
774 
775 	data_size = struct_size(sgcl_data, sgcl, sgi->num_entries);
776 
777 	sgcl_data = kzalloc(data_size, __GFP_DMA | GFP_KERNEL);
778 	if (!sgcl_data)
779 		return -ENOMEM;
780 
781 	cbd.length = cpu_to_le16(data_size);
782 
783 	dma = dma_map_single(&priv->si->pdev->dev,
784 			     sgcl_data, data_size,
785 			     DMA_FROM_DEVICE);
786 	if (dma_mapping_error(&priv->si->pdev->dev, dma)) {
787 		netdev_err(priv->si->ndev, "DMA mapping failed!\n");
788 		kfree(sgcl_data);
789 		return -ENOMEM;
790 	}
791 
792 	cbd.addr[0] = lower_32_bits(dma);
793 	cbd.addr[1] = upper_32_bits(dma);
794 
795 	sgce = &sgcl_data->sgcl[0];
796 
797 	sgcl_config->agtst = 0x80;
798 
799 	sgcl_data->ct = cpu_to_le32(sgi->cycletime);
800 	sgcl_data->cte = cpu_to_le32(sgi->cycletimext);
801 
802 	if (sgi->init_ipv >= 0)
803 		sgcl_config->aipv = (sgi->init_ipv & 0x7) | 0x8;
804 
805 	for (i = 0; i < sgi->num_entries; i++) {
806 		struct action_gate_entry *from = &sgi->entries[i];
807 		struct sgce *to = &sgce[i];
808 
809 		if (from->gate_state)
810 			to->multi |= 0x10;
811 
812 		if (from->ipv >= 0)
813 			to->multi |= ((from->ipv & 0x7) << 5) | 0x08;
814 
815 		if (from->maxoctets >= 0) {
816 			to->multi |= 0x01;
817 			to->msdu[0] = from->maxoctets & 0xFF;
818 			to->msdu[1] = (from->maxoctets >> 8) & 0xFF;
819 			to->msdu[2] = (from->maxoctets >> 16) & 0xFF;
820 		}
821 
822 		to->interval = cpu_to_le32(from->interval);
823 	}
824 
825 	/* If basetime is less than now, calculate start time */
826 	now = get_ptp_now(&priv->si->hw);
827 
828 	if (sgi->basetime < now) {
829 		u64 start;
830 
831 		err = get_start_ns(now, sgi->cycletime, &start);
832 		if (err)
833 			goto exit;
834 		sgcl_data->btl = cpu_to_le32(lower_32_bits(start));
835 		sgcl_data->bth = cpu_to_le32(upper_32_bits(start));
836 	} else {
837 		u32 hi, lo;
838 
839 		hi = upper_32_bits(sgi->basetime);
840 		lo = lower_32_bits(sgi->basetime);
841 		sgcl_data->bth = cpu_to_le32(hi);
842 		sgcl_data->btl = cpu_to_le32(lo);
843 	}
844 
845 	err = enetc_send_cmd(priv->si, &cbd);
846 
847 exit:
848 	kfree(sgcl_data);
849 
850 	return err;
851 }
852 
enetc_flowmeter_hw_set(struct enetc_ndev_priv * priv,struct enetc_psfp_meter * fmi,u8 enable)853 static int enetc_flowmeter_hw_set(struct enetc_ndev_priv *priv,
854 				  struct enetc_psfp_meter *fmi,
855 				  u8 enable)
856 {
857 	struct enetc_cbd cbd = { .cmd = 0 };
858 	struct fmi_conf *fmi_config;
859 	u64 temp = 0;
860 
861 	cbd.index = cpu_to_le16((u16)fmi->index);
862 	cbd.cls = BDCR_CMD_FLOW_METER;
863 	cbd.status_flags = 0x80;
864 
865 	if (!enable)
866 		return enetc_send_cmd(priv->si, &cbd);
867 
868 	fmi_config = &cbd.fmi_conf;
869 	fmi_config->en = 0x80;
870 
871 	if (fmi->cir) {
872 		temp = (u64)8000 * fmi->cir;
873 		temp = div_u64(temp, 3725);
874 	}
875 
876 	fmi_config->cir = cpu_to_le32((u32)temp);
877 	fmi_config->cbs = cpu_to_le32(fmi->cbs);
878 
879 	/* Default for eir ebs disable */
880 	fmi_config->eir = 0;
881 	fmi_config->ebs = 0;
882 
883 	/* Default:
884 	 * mark red disable
885 	 * drop on yellow disable
886 	 * color mode disable
887 	 * couple flag disable
888 	 */
889 	fmi_config->conf = 0;
890 
891 	return enetc_send_cmd(priv->si, &cbd);
892 }
893 
enetc_get_stream_by_index(u32 index)894 static struct enetc_stream_filter *enetc_get_stream_by_index(u32 index)
895 {
896 	struct enetc_stream_filter *f;
897 
898 	hlist_for_each_entry(f, &epsfp.stream_list, node)
899 		if (f->sid.index == index)
900 			return f;
901 
902 	return NULL;
903 }
904 
enetc_get_gate_by_index(u32 index)905 static struct enetc_psfp_gate *enetc_get_gate_by_index(u32 index)
906 {
907 	struct enetc_psfp_gate *g;
908 
909 	hlist_for_each_entry(g, &epsfp.psfp_gate_list, node)
910 		if (g->index == index)
911 			return g;
912 
913 	return NULL;
914 }
915 
enetc_get_filter_by_index(u32 index)916 static struct enetc_psfp_filter *enetc_get_filter_by_index(u32 index)
917 {
918 	struct enetc_psfp_filter *s;
919 
920 	hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
921 		if (s->index == index)
922 			return s;
923 
924 	return NULL;
925 }
926 
enetc_get_meter_by_index(u32 index)927 static struct enetc_psfp_meter *enetc_get_meter_by_index(u32 index)
928 {
929 	struct enetc_psfp_meter *m;
930 
931 	hlist_for_each_entry(m, &epsfp.psfp_meter_list, node)
932 		if (m->index == index)
933 			return m;
934 
935 	return NULL;
936 }
937 
938 static struct enetc_psfp_filter
enetc_psfp_check_sfi(struct enetc_psfp_filter * sfi)939 	*enetc_psfp_check_sfi(struct enetc_psfp_filter *sfi)
940 {
941 	struct enetc_psfp_filter *s;
942 
943 	hlist_for_each_entry(s, &epsfp.psfp_filter_list, node)
944 		if (s->gate_id == sfi->gate_id &&
945 		    s->prio == sfi->prio &&
946 		    s->maxsdu == sfi->maxsdu &&
947 		    s->meter_id == sfi->meter_id)
948 			return s;
949 
950 	return NULL;
951 }
952 
enetc_get_free_index(struct enetc_ndev_priv * priv)953 static int enetc_get_free_index(struct enetc_ndev_priv *priv)
954 {
955 	u32 max_size = priv->psfp_cap.max_psfp_filter;
956 	unsigned long index;
957 
958 	index = find_first_zero_bit(epsfp.psfp_sfi_bitmap, max_size);
959 	if (index == max_size)
960 		return -1;
961 
962 	return index;
963 }
964 
stream_filter_unref(struct enetc_ndev_priv * priv,u32 index)965 static void stream_filter_unref(struct enetc_ndev_priv *priv, u32 index)
966 {
967 	struct enetc_psfp_filter *sfi;
968 	u8 z;
969 
970 	sfi = enetc_get_filter_by_index(index);
971 	WARN_ON(!sfi);
972 	z = refcount_dec_and_test(&sfi->refcount);
973 
974 	if (z) {
975 		enetc_streamfilter_hw_set(priv, sfi, false);
976 		hlist_del(&sfi->node);
977 		kfree(sfi);
978 		clear_bit(index, epsfp.psfp_sfi_bitmap);
979 	}
980 }
981 
stream_gate_unref(struct enetc_ndev_priv * priv,u32 index)982 static void stream_gate_unref(struct enetc_ndev_priv *priv, u32 index)
983 {
984 	struct enetc_psfp_gate *sgi;
985 	u8 z;
986 
987 	sgi = enetc_get_gate_by_index(index);
988 	WARN_ON(!sgi);
989 	z = refcount_dec_and_test(&sgi->refcount);
990 	if (z) {
991 		enetc_streamgate_hw_set(priv, sgi, false);
992 		hlist_del(&sgi->node);
993 		kfree(sgi);
994 	}
995 }
996 
flow_meter_unref(struct enetc_ndev_priv * priv,u32 index)997 static void flow_meter_unref(struct enetc_ndev_priv *priv, u32 index)
998 {
999 	struct enetc_psfp_meter *fmi;
1000 	u8 z;
1001 
1002 	fmi = enetc_get_meter_by_index(index);
1003 	WARN_ON(!fmi);
1004 	z = refcount_dec_and_test(&fmi->refcount);
1005 	if (z) {
1006 		enetc_flowmeter_hw_set(priv, fmi, false);
1007 		hlist_del(&fmi->node);
1008 		kfree(fmi);
1009 	}
1010 }
1011 
remove_one_chain(struct enetc_ndev_priv * priv,struct enetc_stream_filter * filter)1012 static void remove_one_chain(struct enetc_ndev_priv *priv,
1013 			     struct enetc_stream_filter *filter)
1014 {
1015 	if (filter->flags & ENETC_PSFP_FLAGS_FMI)
1016 		flow_meter_unref(priv, filter->fmi_index);
1017 
1018 	stream_gate_unref(priv, filter->sgi_index);
1019 	stream_filter_unref(priv, filter->sfi_index);
1020 
1021 	hlist_del(&filter->node);
1022 	kfree(filter);
1023 }
1024 
enetc_psfp_hw_set(struct enetc_ndev_priv * priv,struct enetc_streamid * sid,struct enetc_psfp_filter * sfi,struct enetc_psfp_gate * sgi,struct enetc_psfp_meter * fmi)1025 static int enetc_psfp_hw_set(struct enetc_ndev_priv *priv,
1026 			     struct enetc_streamid *sid,
1027 			     struct enetc_psfp_filter *sfi,
1028 			     struct enetc_psfp_gate *sgi,
1029 			     struct enetc_psfp_meter *fmi)
1030 {
1031 	int err;
1032 
1033 	err = enetc_streamid_hw_set(priv, sid, true);
1034 	if (err)
1035 		return err;
1036 
1037 	if (sfi) {
1038 		err = enetc_streamfilter_hw_set(priv, sfi, true);
1039 		if (err)
1040 			goto revert_sid;
1041 	}
1042 
1043 	err = enetc_streamgate_hw_set(priv, sgi, true);
1044 	if (err)
1045 		goto revert_sfi;
1046 
1047 	if (fmi) {
1048 		err = enetc_flowmeter_hw_set(priv, fmi, true);
1049 		if (err)
1050 			goto revert_sgi;
1051 	}
1052 
1053 	return 0;
1054 
1055 revert_sgi:
1056 	enetc_streamgate_hw_set(priv, sgi, false);
1057 revert_sfi:
1058 	if (sfi)
1059 		enetc_streamfilter_hw_set(priv, sfi, false);
1060 revert_sid:
1061 	enetc_streamid_hw_set(priv, sid, false);
1062 	return err;
1063 }
1064 
enetc_check_flow_actions(u64 acts,unsigned int inputkeys)1065 static struct actions_fwd *enetc_check_flow_actions(u64 acts,
1066 						    unsigned int inputkeys)
1067 {
1068 	int i;
1069 
1070 	for (i = 0; i < ARRAY_SIZE(enetc_act_fwd); i++)
1071 		if (acts == enetc_act_fwd[i].actions &&
1072 		    inputkeys & enetc_act_fwd[i].keys)
1073 			return &enetc_act_fwd[i];
1074 
1075 	return NULL;
1076 }
1077 
enetc_psfp_parse_clsflower(struct enetc_ndev_priv * priv,struct flow_cls_offload * f)1078 static int enetc_psfp_parse_clsflower(struct enetc_ndev_priv *priv,
1079 				      struct flow_cls_offload *f)
1080 {
1081 	struct flow_action_entry *entryg = NULL, *entryp = NULL;
1082 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1083 	struct netlink_ext_ack *extack = f->common.extack;
1084 	struct enetc_stream_filter *filter, *old_filter;
1085 	struct enetc_psfp_meter *fmi = NULL, *old_fmi;
1086 	struct enetc_psfp_filter *sfi, *old_sfi;
1087 	struct enetc_psfp_gate *sgi, *old_sgi;
1088 	struct flow_action_entry *entry;
1089 	struct action_gate_entry *e;
1090 	u8 sfi_overwrite = 0;
1091 	int entries_size;
1092 	int i, err;
1093 
1094 	if (f->common.chain_index >= priv->psfp_cap.max_streamid) {
1095 		NL_SET_ERR_MSG_MOD(extack, "No Stream identify resource!");
1096 		return -ENOSPC;
1097 	}
1098 
1099 	flow_action_for_each(i, entry, &rule->action)
1100 		if (entry->id == FLOW_ACTION_GATE)
1101 			entryg = entry;
1102 		else if (entry->id == FLOW_ACTION_POLICE)
1103 			entryp = entry;
1104 
1105 	/* Not support without gate action */
1106 	if (!entryg)
1107 		return -EINVAL;
1108 
1109 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1110 	if (!filter)
1111 		return -ENOMEM;
1112 
1113 	filter->sid.index = f->common.chain_index;
1114 
1115 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1116 		struct flow_match_eth_addrs match;
1117 
1118 		flow_rule_match_eth_addrs(rule, &match);
1119 
1120 		if (!is_zero_ether_addr(match.mask->dst) &&
1121 		    !is_zero_ether_addr(match.mask->src)) {
1122 			NL_SET_ERR_MSG_MOD(extack,
1123 					   "Cannot match on both source and destination MAC");
1124 			err = -EINVAL;
1125 			goto free_filter;
1126 		}
1127 
1128 		if (!is_zero_ether_addr(match.mask->dst)) {
1129 			if (!is_broadcast_ether_addr(match.mask->dst)) {
1130 				NL_SET_ERR_MSG_MOD(extack,
1131 						   "Masked matching on destination MAC not supported");
1132 				err = -EINVAL;
1133 				goto free_filter;
1134 			}
1135 			ether_addr_copy(filter->sid.dst_mac, match.key->dst);
1136 			filter->sid.filtertype = STREAMID_TYPE_NULL;
1137 		}
1138 
1139 		if (!is_zero_ether_addr(match.mask->src)) {
1140 			if (!is_broadcast_ether_addr(match.mask->src)) {
1141 				NL_SET_ERR_MSG_MOD(extack,
1142 						   "Masked matching on source MAC not supported");
1143 				err = -EINVAL;
1144 				goto free_filter;
1145 			}
1146 			ether_addr_copy(filter->sid.src_mac, match.key->src);
1147 			filter->sid.filtertype = STREAMID_TYPE_SMAC;
1148 		}
1149 	} else {
1150 		NL_SET_ERR_MSG_MOD(extack, "Unsupported, must include ETH_ADDRS");
1151 		err = -EINVAL;
1152 		goto free_filter;
1153 	}
1154 
1155 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
1156 		struct flow_match_vlan match;
1157 
1158 		flow_rule_match_vlan(rule, &match);
1159 		if (match.mask->vlan_priority) {
1160 			if (match.mask->vlan_priority !=
1161 			    (VLAN_PRIO_MASK >> VLAN_PRIO_SHIFT)) {
1162 				NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN priority");
1163 				err = -EINVAL;
1164 				goto free_filter;
1165 			}
1166 		}
1167 
1168 		if (match.mask->vlan_id) {
1169 			if (match.mask->vlan_id != VLAN_VID_MASK) {
1170 				NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN id");
1171 				err = -EINVAL;
1172 				goto free_filter;
1173 			}
1174 
1175 			filter->sid.vid = match.key->vlan_id;
1176 			if (!filter->sid.vid)
1177 				filter->sid.tagged = STREAMID_VLAN_UNTAGGED;
1178 			else
1179 				filter->sid.tagged = STREAMID_VLAN_TAGGED;
1180 		}
1181 	} else {
1182 		filter->sid.tagged = STREAMID_VLAN_ALL;
1183 	}
1184 
1185 	/* parsing gate action */
1186 	if (entryg->gate.index >= priv->psfp_cap.max_psfp_gate) {
1187 		NL_SET_ERR_MSG_MOD(extack, "No Stream Gate resource!");
1188 		err = -ENOSPC;
1189 		goto free_filter;
1190 	}
1191 
1192 	if (entryg->gate.num_entries >= priv->psfp_cap.max_psfp_gatelist) {
1193 		NL_SET_ERR_MSG_MOD(extack, "No Stream Gate resource!");
1194 		err = -ENOSPC;
1195 		goto free_filter;
1196 	}
1197 
1198 	entries_size = struct_size(sgi, entries, entryg->gate.num_entries);
1199 	sgi = kzalloc(entries_size, GFP_KERNEL);
1200 	if (!sgi) {
1201 		err = -ENOMEM;
1202 		goto free_filter;
1203 	}
1204 
1205 	refcount_set(&sgi->refcount, 1);
1206 	sgi->index = entryg->gate.index;
1207 	sgi->init_ipv = entryg->gate.prio;
1208 	sgi->basetime = entryg->gate.basetime;
1209 	sgi->cycletime = entryg->gate.cycletime;
1210 	sgi->num_entries = entryg->gate.num_entries;
1211 
1212 	e = sgi->entries;
1213 	for (i = 0; i < entryg->gate.num_entries; i++) {
1214 		e[i].gate_state = entryg->gate.entries[i].gate_state;
1215 		e[i].interval = entryg->gate.entries[i].interval;
1216 		e[i].ipv = entryg->gate.entries[i].ipv;
1217 		e[i].maxoctets = entryg->gate.entries[i].maxoctets;
1218 	}
1219 
1220 	filter->sgi_index = sgi->index;
1221 
1222 	sfi = kzalloc(sizeof(*sfi), GFP_KERNEL);
1223 	if (!sfi) {
1224 		err = -ENOMEM;
1225 		goto free_gate;
1226 	}
1227 
1228 	refcount_set(&sfi->refcount, 1);
1229 	sfi->gate_id = sgi->index;
1230 	sfi->meter_id = ENETC_PSFP_WILDCARD;
1231 
1232 	/* Flow meter and max frame size */
1233 	if (entryp) {
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 
enetc_config_clsflower(struct enetc_ndev_priv * priv,struct flow_cls_offload * cls_flower)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 
enetc_psfp_destroy_clsflower(struct enetc_ndev_priv * priv,struct flow_cls_offload * f)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 
enetc_destroy_clsflower(struct enetc_ndev_priv * priv,struct flow_cls_offload * f)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 
enetc_psfp_get_stats(struct enetc_ndev_priv * priv,struct flow_cls_offload * f)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 
enetc_setup_tc_cls_flower(struct enetc_ndev_priv * priv,struct flow_cls_offload * cls_flower)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 
clean_psfp_sfi_bitmap(void)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 
clean_stream_list(void)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 
clean_sfi_list(void)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 
clean_sgi_list(void)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 
clean_psfp_all(void)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 
enetc_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)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 
enetc_psfp_init(struct enetc_ndev_priv * priv)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 
enetc_psfp_clean(struct enetc_ndev_priv * priv)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 
enetc_setup_tc_psfp(struct net_device * ndev,void * type_data)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 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 		set_bit(enetc_get_port(priv), &epsfp.dev_bitmap);
1571 		break;
1572 	case FLOW_BLOCK_UNBIND:
1573 		clear_bit(enetc_get_port(priv), &epsfp.dev_bitmap);
1574 		if (!epsfp.dev_bitmap)
1575 			clean_psfp_all();
1576 		break;
1577 	}
1578 
1579 	return 0;
1580 }
1581