xref: /linux/drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
3  * Copyright 2016-2022 NXP
4  */
5 #include <linux/init.h>
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/etherdevice.h>
9 #include <linux/of_net.h>
10 #include <linux/interrupt.h>
11 #include <linux/kthread.h>
12 #include <linux/iommu.h>
13 #include <linux/fsl/mc.h>
14 #include <linux/bpf.h>
15 #include <linux/bpf_trace.h>
16 #include <linux/fsl/ptp_qoriq.h>
17 #include <linux/ptp_classify.h>
18 #include <net/pkt_cls.h>
19 #include <net/sock.h>
20 #include <net/tso.h>
21 #include <net/xdp_sock_drv.h>
22 
23 #include "dpaa2-eth.h"
24 
25 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
26  * using trace events only need to #include <trace/events/sched.h>
27  */
28 #define CREATE_TRACE_POINTS
29 #include "dpaa2-eth-trace.h"
30 
31 MODULE_LICENSE("Dual BSD/GPL");
32 MODULE_AUTHOR("Freescale Semiconductor, Inc");
33 MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
34 
35 struct ptp_qoriq *dpaa2_ptp;
36 EXPORT_SYMBOL(dpaa2_ptp);
37 
dpaa2_eth_detect_features(struct dpaa2_eth_priv * priv)38 static void dpaa2_eth_detect_features(struct dpaa2_eth_priv *priv)
39 {
40 	priv->features = 0;
41 
42 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_PTP_ONESTEP_VER_MAJOR,
43 				   DPNI_PTP_ONESTEP_VER_MINOR) >= 0)
44 		priv->features |= DPAA2_ETH_FEATURE_ONESTEP_CFG_DIRECT;
45 }
46 
dpaa2_update_ptp_onestep_indirect(struct dpaa2_eth_priv * priv,u32 offset,u8 udp)47 static void dpaa2_update_ptp_onestep_indirect(struct dpaa2_eth_priv *priv,
48 					      u32 offset, u8 udp)
49 {
50 	struct dpni_single_step_cfg cfg;
51 
52 	cfg.en = 1;
53 	cfg.ch_update = udp;
54 	cfg.offset = offset;
55 	cfg.peer_delay = 0;
56 
57 	if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token, &cfg))
58 		WARN_ONCE(1, "Failed to set single step register");
59 }
60 
dpaa2_update_ptp_onestep_direct(struct dpaa2_eth_priv * priv,u32 offset,u8 udp)61 static void dpaa2_update_ptp_onestep_direct(struct dpaa2_eth_priv *priv,
62 					    u32 offset, u8 udp)
63 {
64 	u32 val = 0;
65 
66 	val = DPAA2_PTP_SINGLE_STEP_ENABLE |
67 	       DPAA2_PTP_SINGLE_CORRECTION_OFF(offset);
68 
69 	if (udp)
70 		val |= DPAA2_PTP_SINGLE_STEP_CH;
71 
72 	if (priv->onestep_reg_base)
73 		writel(val, priv->onestep_reg_base);
74 }
75 
dpaa2_ptp_onestep_reg_update_method(struct dpaa2_eth_priv * priv)76 static void dpaa2_ptp_onestep_reg_update_method(struct dpaa2_eth_priv *priv)
77 {
78 	struct device *dev = priv->net_dev->dev.parent;
79 	struct dpni_single_step_cfg ptp_cfg;
80 
81 	priv->dpaa2_set_onestep_params_cb = dpaa2_update_ptp_onestep_indirect;
82 
83 	if (!(priv->features & DPAA2_ETH_FEATURE_ONESTEP_CFG_DIRECT))
84 		return;
85 
86 	if (dpni_get_single_step_cfg(priv->mc_io, 0,
87 				     priv->mc_token, &ptp_cfg)) {
88 		dev_err(dev, "dpni_get_single_step_cfg cannot retrieve onestep reg, falling back to indirect update\n");
89 		return;
90 	}
91 
92 	if (!ptp_cfg.ptp_onestep_reg_base) {
93 		dev_err(dev, "1588 onestep reg not available, falling back to indirect update\n");
94 		return;
95 	}
96 
97 	priv->onestep_reg_base = ioremap(ptp_cfg.ptp_onestep_reg_base,
98 					 sizeof(u32));
99 	if (!priv->onestep_reg_base) {
100 		dev_err(dev, "1588 onestep reg cannot be mapped, falling back to indirect update\n");
101 		return;
102 	}
103 
104 	priv->dpaa2_set_onestep_params_cb = dpaa2_update_ptp_onestep_direct;
105 }
106 
dpaa2_iova_to_virt(struct iommu_domain * domain,dma_addr_t iova_addr)107 void *dpaa2_iova_to_virt(struct iommu_domain *domain,
108 			 dma_addr_t iova_addr)
109 {
110 	phys_addr_t phys_addr;
111 
112 	phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
113 
114 	return phys_to_virt(phys_addr);
115 }
116 
dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv * priv,u32 fd_status,struct sk_buff * skb)117 static void dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv *priv,
118 				       u32 fd_status,
119 				       struct sk_buff *skb)
120 {
121 	skb_checksum_none_assert(skb);
122 
123 	/* HW checksum validation is disabled, nothing to do here */
124 	if (!(priv->net_dev->features & NETIF_F_RXCSUM))
125 		return;
126 
127 	/* Read checksum validation bits */
128 	if (!((fd_status & DPAA2_FAS_L3CV) &&
129 	      (fd_status & DPAA2_FAS_L4CV)))
130 		return;
131 
132 	/* Inform the stack there's no need to compute L3/L4 csum anymore */
133 	skb->ip_summed = CHECKSUM_UNNECESSARY;
134 }
135 
136 /* Free a received FD.
137  * Not to be used for Tx conf FDs or on any other paths.
138  */
dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv * priv,const struct dpaa2_fd * fd,void * vaddr)139 static void dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv,
140 				 const struct dpaa2_fd *fd,
141 				 void *vaddr)
142 {
143 	struct device *dev = priv->net_dev->dev.parent;
144 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
145 	u8 fd_format = dpaa2_fd_get_format(fd);
146 	struct dpaa2_sg_entry *sgt;
147 	void *sg_vaddr;
148 	int i;
149 
150 	/* If single buffer frame, just free the data buffer */
151 	if (fd_format == dpaa2_fd_single)
152 		goto free_buf;
153 	else if (fd_format != dpaa2_fd_sg)
154 		/* We don't support any other format */
155 		return;
156 
157 	/* For S/G frames, we first need to free all SG entries
158 	 * except the first one, which was taken care of already
159 	 */
160 	sgt = vaddr + dpaa2_fd_get_offset(fd);
161 	for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
162 		addr = dpaa2_sg_get_addr(&sgt[i]);
163 		sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
164 		dma_unmap_page(dev, addr, priv->rx_buf_size,
165 			       DMA_BIDIRECTIONAL);
166 
167 		free_pages((unsigned long)sg_vaddr, 0);
168 		if (dpaa2_sg_is_final(&sgt[i]))
169 			break;
170 	}
171 
172 free_buf:
173 	free_pages((unsigned long)vaddr, 0);
174 }
175 
176 /* Build a linear skb based on a single-buffer frame descriptor */
dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel * ch,const struct dpaa2_fd * fd,void * fd_vaddr)177 static struct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch,
178 						  const struct dpaa2_fd *fd,
179 						  void *fd_vaddr)
180 {
181 	struct sk_buff *skb = NULL;
182 	u16 fd_offset = dpaa2_fd_get_offset(fd);
183 	u32 fd_length = dpaa2_fd_get_len(fd);
184 
185 	ch->buf_count--;
186 
187 	skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
188 	if (unlikely(!skb))
189 		return NULL;
190 
191 	skb_reserve(skb, fd_offset);
192 	skb_put(skb, fd_length);
193 
194 	return skb;
195 }
196 
197 /* Build a non linear (fragmented) skb based on a S/G table */
dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,struct dpaa2_sg_entry * sgt)198 static struct sk_buff *dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv *priv,
199 						struct dpaa2_eth_channel *ch,
200 						struct dpaa2_sg_entry *sgt)
201 {
202 	struct sk_buff *skb = NULL;
203 	struct device *dev = priv->net_dev->dev.parent;
204 	void *sg_vaddr;
205 	dma_addr_t sg_addr;
206 	u16 sg_offset;
207 	u32 sg_length;
208 	struct page *page, *head_page;
209 	int page_offset;
210 	int i;
211 
212 	for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
213 		struct dpaa2_sg_entry *sge = &sgt[i];
214 
215 		/* NOTE: We only support SG entries in dpaa2_sg_single format,
216 		 * but this is the only format we may receive from HW anyway
217 		 */
218 
219 		/* Get the address and length from the S/G entry */
220 		sg_addr = dpaa2_sg_get_addr(sge);
221 		sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
222 		dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
223 			       DMA_BIDIRECTIONAL);
224 
225 		sg_length = dpaa2_sg_get_len(sge);
226 
227 		if (i == 0) {
228 			/* We build the skb around the first data buffer */
229 			skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
230 			if (unlikely(!skb)) {
231 				/* Free the first SG entry now, since we already
232 				 * unmapped it and obtained the virtual address
233 				 */
234 				free_pages((unsigned long)sg_vaddr, 0);
235 
236 				/* We still need to subtract the buffers used
237 				 * by this FD from our software counter
238 				 */
239 				while (!dpaa2_sg_is_final(&sgt[i]) &&
240 				       i < DPAA2_ETH_MAX_SG_ENTRIES)
241 					i++;
242 				break;
243 			}
244 
245 			sg_offset = dpaa2_sg_get_offset(sge);
246 			skb_reserve(skb, sg_offset);
247 			skb_put(skb, sg_length);
248 		} else {
249 			/* Rest of the data buffers are stored as skb frags */
250 			page = virt_to_page(sg_vaddr);
251 			head_page = virt_to_head_page(sg_vaddr);
252 
253 			/* Offset in page (which may be compound).
254 			 * Data in subsequent SG entries is stored from the
255 			 * beginning of the buffer, so we don't need to add the
256 			 * sg_offset.
257 			 */
258 			page_offset = ((unsigned long)sg_vaddr &
259 				(PAGE_SIZE - 1)) +
260 				(page_address(page) - page_address(head_page));
261 
262 			skb_add_rx_frag(skb, i - 1, head_page, page_offset,
263 					sg_length, priv->rx_buf_size);
264 		}
265 
266 		if (dpaa2_sg_is_final(sge))
267 			break;
268 	}
269 
270 	WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
271 
272 	/* Count all data buffers + SG table buffer */
273 	ch->buf_count -= i + 2;
274 
275 	return skb;
276 }
277 
278 /* Free buffers acquired from the buffer pool or which were meant to
279  * be released in the pool
280  */
dpaa2_eth_free_bufs(struct dpaa2_eth_priv * priv,u64 * buf_array,int count,bool xsk_zc)281 static void dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array,
282 				int count, bool xsk_zc)
283 {
284 	struct device *dev = priv->net_dev->dev.parent;
285 	struct dpaa2_eth_swa *swa;
286 	struct xdp_buff *xdp_buff;
287 	void *vaddr;
288 	int i;
289 
290 	for (i = 0; i < count; i++) {
291 		vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
292 
293 		if (!xsk_zc) {
294 			dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
295 				       DMA_BIDIRECTIONAL);
296 			free_pages((unsigned long)vaddr, 0);
297 		} else {
298 			swa = (struct dpaa2_eth_swa *)
299 				(vaddr + DPAA2_ETH_RX_HWA_SIZE);
300 			xdp_buff = swa->xsk.xdp_buff;
301 			xsk_buff_free(xdp_buff);
302 		}
303 	}
304 }
305 
dpaa2_eth_recycle_buf(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,dma_addr_t addr)306 void dpaa2_eth_recycle_buf(struct dpaa2_eth_priv *priv,
307 			   struct dpaa2_eth_channel *ch,
308 			   dma_addr_t addr)
309 {
310 	int retries = 0;
311 	int err;
312 
313 	ch->recycled_bufs[ch->recycled_bufs_cnt++] = addr;
314 	if (ch->recycled_bufs_cnt < DPAA2_ETH_BUFS_PER_CMD)
315 		return;
316 
317 	while ((err = dpaa2_io_service_release(ch->dpio, ch->bp->bpid,
318 					       ch->recycled_bufs,
319 					       ch->recycled_bufs_cnt)) == -EBUSY) {
320 		if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
321 			break;
322 		cpu_relax();
323 	}
324 
325 	if (err) {
326 		dpaa2_eth_free_bufs(priv, ch->recycled_bufs,
327 				    ch->recycled_bufs_cnt, ch->xsk_zc);
328 		ch->buf_count -= ch->recycled_bufs_cnt;
329 	}
330 
331 	ch->recycled_bufs_cnt = 0;
332 }
333 
dpaa2_eth_xdp_flush(struct dpaa2_eth_priv * priv,struct dpaa2_eth_fq * fq,struct dpaa2_eth_xdp_fds * xdp_fds)334 static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
335 			       struct dpaa2_eth_fq *fq,
336 			       struct dpaa2_eth_xdp_fds *xdp_fds)
337 {
338 	int total_enqueued = 0, retries = 0, enqueued;
339 	struct dpaa2_eth_drv_stats *percpu_extras;
340 	int num_fds, err, max_retries;
341 	struct dpaa2_fd *fds;
342 
343 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
344 
345 	/* try to enqueue all the FDs until the max number of retries is hit */
346 	fds = xdp_fds->fds;
347 	num_fds = xdp_fds->num;
348 	max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
349 	while (total_enqueued < num_fds && retries < max_retries) {
350 		err = priv->enqueue(priv, fq, &fds[total_enqueued],
351 				    0, num_fds - total_enqueued, &enqueued);
352 		if (err == -EBUSY) {
353 			percpu_extras->tx_portal_busy += ++retries;
354 			continue;
355 		}
356 		total_enqueued += enqueued;
357 	}
358 	xdp_fds->num = 0;
359 
360 	return total_enqueued;
361 }
362 
dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,struct dpaa2_eth_fq * fq)363 static void dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv *priv,
364 				   struct dpaa2_eth_channel *ch,
365 				   struct dpaa2_eth_fq *fq)
366 {
367 	struct rtnl_link_stats64 *percpu_stats;
368 	struct dpaa2_fd *fds;
369 	int enqueued, i;
370 
371 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
372 
373 	// enqueue the array of XDP_TX frames
374 	enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);
375 
376 	/* update statistics */
377 	percpu_stats->tx_packets += enqueued;
378 	fds = fq->xdp_tx_fds.fds;
379 	for (i = 0; i < enqueued; i++) {
380 		percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
381 		ch->stats.xdp_tx++;
382 	}
383 	for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
384 		dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
385 		percpu_stats->tx_errors++;
386 		ch->stats.xdp_tx_err++;
387 	}
388 	fq->xdp_tx_fds.num = 0;
389 }
390 
dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,struct dpaa2_fd * fd,void * buf_start,u16 queue_id)391 void dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv *priv,
392 			   struct dpaa2_eth_channel *ch,
393 			   struct dpaa2_fd *fd,
394 			   void *buf_start, u16 queue_id)
395 {
396 	struct dpaa2_faead *faead;
397 	struct dpaa2_fd *dest_fd;
398 	struct dpaa2_eth_fq *fq;
399 	u32 ctrl, frc;
400 
401 	/* Mark the egress frame hardware annotation area as valid */
402 	frc = dpaa2_fd_get_frc(fd);
403 	dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
404 	dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
405 
406 	/* Instruct hardware to release the FD buffer directly into
407 	 * the buffer pool once transmission is completed, instead of
408 	 * sending a Tx confirmation frame to us
409 	 */
410 	ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
411 	faead = dpaa2_get_faead(buf_start, false);
412 	faead->ctrl = cpu_to_le32(ctrl);
413 	faead->conf_fqid = 0;
414 
415 	fq = &priv->fq[queue_id];
416 	dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
417 	memcpy(dest_fd, fd, sizeof(*dest_fd));
418 
419 	if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
420 		return;
421 
422 	dpaa2_eth_xdp_tx_flush(priv, ch, fq);
423 }
424 
dpaa2_eth_run_xdp(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,struct dpaa2_eth_fq * rx_fq,struct dpaa2_fd * fd,void * vaddr)425 static u32 dpaa2_eth_run_xdp(struct dpaa2_eth_priv *priv,
426 			     struct dpaa2_eth_channel *ch,
427 			     struct dpaa2_eth_fq *rx_fq,
428 			     struct dpaa2_fd *fd, void *vaddr)
429 {
430 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
431 	struct bpf_prog *xdp_prog;
432 	struct xdp_buff xdp;
433 	u32 xdp_act = XDP_PASS;
434 	int err, offset;
435 
436 	xdp_prog = READ_ONCE(ch->xdp.prog);
437 	if (!xdp_prog)
438 		goto out;
439 
440 	offset = dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM;
441 	xdp_init_buff(&xdp, DPAA2_ETH_RX_BUF_RAW_SIZE - offset, &ch->xdp_rxq);
442 	xdp_prepare_buff(&xdp, vaddr + offset, XDP_PACKET_HEADROOM,
443 			 dpaa2_fd_get_len(fd), false);
444 
445 	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
446 
447 	/* xdp.data pointer may have changed */
448 	dpaa2_fd_set_offset(fd, xdp.data - vaddr);
449 	dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
450 
451 	switch (xdp_act) {
452 	case XDP_PASS:
453 		break;
454 	case XDP_TX:
455 		dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
456 		break;
457 	default:
458 		bpf_warn_invalid_xdp_action(priv->net_dev, xdp_prog, xdp_act);
459 		fallthrough;
460 	case XDP_ABORTED:
461 		trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
462 		fallthrough;
463 	case XDP_DROP:
464 		dpaa2_eth_recycle_buf(priv, ch, addr);
465 		ch->stats.xdp_drop++;
466 		break;
467 	case XDP_REDIRECT:
468 		dma_unmap_page(priv->net_dev->dev.parent, addr,
469 			       priv->rx_buf_size, DMA_BIDIRECTIONAL);
470 		ch->buf_count--;
471 
472 		/* Allow redirect use of full headroom */
473 		xdp.data_hard_start = vaddr;
474 		xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
475 
476 		err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
477 		if (unlikely(err)) {
478 			addr = dma_map_page(priv->net_dev->dev.parent,
479 					    virt_to_page(vaddr), 0,
480 					    priv->rx_buf_size, DMA_BIDIRECTIONAL);
481 			if (unlikely(dma_mapping_error(priv->net_dev->dev.parent, addr))) {
482 				free_pages((unsigned long)vaddr, 0);
483 			} else {
484 				ch->buf_count++;
485 				dpaa2_eth_recycle_buf(priv, ch, addr);
486 			}
487 			ch->stats.xdp_drop++;
488 		} else {
489 			ch->stats.xdp_redirect++;
490 		}
491 		break;
492 	}
493 
494 	ch->xdp.res |= xdp_act;
495 out:
496 	return xdp_act;
497 }
498 
dpaa2_eth_alloc_skb(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,const struct dpaa2_fd * fd,u32 fd_length,void * fd_vaddr)499 struct sk_buff *dpaa2_eth_alloc_skb(struct dpaa2_eth_priv *priv,
500 				    struct dpaa2_eth_channel *ch,
501 				    const struct dpaa2_fd *fd, u32 fd_length,
502 				    void *fd_vaddr)
503 {
504 	u16 fd_offset = dpaa2_fd_get_offset(fd);
505 	struct sk_buff *skb = NULL;
506 	unsigned int skb_len;
507 
508 	skb_len = fd_length + dpaa2_eth_needed_headroom(NULL);
509 
510 	skb = napi_alloc_skb(&ch->napi, skb_len);
511 	if (!skb)
512 		return NULL;
513 
514 	skb_reserve(skb, dpaa2_eth_needed_headroom(NULL));
515 	skb_put(skb, fd_length);
516 
517 	memcpy(skb->data, fd_vaddr + fd_offset, fd_length);
518 
519 	return skb;
520 }
521 
dpaa2_eth_copybreak(struct dpaa2_eth_channel * ch,const struct dpaa2_fd * fd,void * fd_vaddr)522 static struct sk_buff *dpaa2_eth_copybreak(struct dpaa2_eth_channel *ch,
523 					   const struct dpaa2_fd *fd,
524 					   void *fd_vaddr)
525 {
526 	struct dpaa2_eth_priv *priv = ch->priv;
527 	u32 fd_length = dpaa2_fd_get_len(fd);
528 
529 	if (fd_length > priv->rx_copybreak)
530 		return NULL;
531 
532 	return dpaa2_eth_alloc_skb(priv, ch, fd, fd_length, fd_vaddr);
533 }
534 
dpaa2_eth_receive_skb(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,const struct dpaa2_fd * fd,void * vaddr,struct dpaa2_eth_fq * fq,struct rtnl_link_stats64 * percpu_stats,struct sk_buff * skb)535 void dpaa2_eth_receive_skb(struct dpaa2_eth_priv *priv,
536 			   struct dpaa2_eth_channel *ch,
537 			   const struct dpaa2_fd *fd, void *vaddr,
538 			   struct dpaa2_eth_fq *fq,
539 			   struct rtnl_link_stats64 *percpu_stats,
540 			   struct sk_buff *skb)
541 {
542 	struct dpaa2_fas *fas;
543 	u32 status = 0;
544 
545 	fas = dpaa2_get_fas(vaddr, false);
546 	prefetch(fas);
547 	prefetch(skb->data);
548 
549 	/* Get the timestamp value */
550 	if (priv->rx_tstamp) {
551 		struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
552 		__le64 *ts = dpaa2_get_ts(vaddr, false);
553 		u64 ns;
554 
555 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
556 
557 		ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
558 		shhwtstamps->hwtstamp = ns_to_ktime(ns);
559 	}
560 
561 	/* Check if we need to validate the L4 csum */
562 	if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
563 		status = le32_to_cpu(fas->status);
564 		dpaa2_eth_validate_rx_csum(priv, status, skb);
565 	}
566 
567 	skb->protocol = eth_type_trans(skb, priv->net_dev);
568 	skb_record_rx_queue(skb, fq->flowid);
569 
570 	percpu_stats->rx_packets++;
571 	percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
572 	ch->stats.bytes_per_cdan += dpaa2_fd_get_len(fd);
573 
574 	list_add_tail(&skb->list, ch->rx_list);
575 }
576 
577 /* Main Rx frame processing routine */
dpaa2_eth_rx(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,const struct dpaa2_fd * fd,struct dpaa2_eth_fq * fq)578 void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
579 		  struct dpaa2_eth_channel *ch,
580 		  const struct dpaa2_fd *fd,
581 		  struct dpaa2_eth_fq *fq)
582 {
583 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
584 	u8 fd_format = dpaa2_fd_get_format(fd);
585 	void *vaddr;
586 	struct sk_buff *skb;
587 	struct rtnl_link_stats64 *percpu_stats;
588 	struct dpaa2_eth_drv_stats *percpu_extras;
589 	struct device *dev = priv->net_dev->dev.parent;
590 	bool recycle_rx_buf = false;
591 	void *buf_data;
592 	u32 xdp_act;
593 
594 	/* Tracing point */
595 	trace_dpaa2_rx_fd(priv->net_dev, fd);
596 
597 	vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
598 	dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
599 				DMA_BIDIRECTIONAL);
600 
601 	buf_data = vaddr + dpaa2_fd_get_offset(fd);
602 	prefetch(buf_data);
603 
604 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
605 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
606 
607 	if (fd_format == dpaa2_fd_single) {
608 		xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
609 		if (xdp_act != XDP_PASS) {
610 			percpu_stats->rx_packets++;
611 			percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
612 			return;
613 		}
614 
615 		skb = dpaa2_eth_copybreak(ch, fd, vaddr);
616 		if (!skb) {
617 			dma_unmap_page(dev, addr, priv->rx_buf_size,
618 				       DMA_BIDIRECTIONAL);
619 			skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
620 		} else {
621 			recycle_rx_buf = true;
622 		}
623 	} else if (fd_format == dpaa2_fd_sg) {
624 		WARN_ON(priv->xdp_prog);
625 
626 		dma_unmap_page(dev, addr, priv->rx_buf_size,
627 			       DMA_BIDIRECTIONAL);
628 		skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
629 		free_pages((unsigned long)vaddr, 0);
630 		percpu_extras->rx_sg_frames++;
631 		percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
632 	} else {
633 		/* We don't support any other format */
634 		goto err_frame_format;
635 	}
636 
637 	if (unlikely(!skb))
638 		goto err_build_skb;
639 
640 	dpaa2_eth_receive_skb(priv, ch, fd, vaddr, fq, percpu_stats, skb);
641 
642 	if (recycle_rx_buf)
643 		dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(fd));
644 	return;
645 
646 err_build_skb:
647 	dpaa2_eth_free_rx_fd(priv, fd, vaddr);
648 err_frame_format:
649 	percpu_stats->rx_dropped++;
650 }
651 
652 /* Processing of Rx frames received on the error FQ
653  * We check and print the error bits and then free the frame
654  */
dpaa2_eth_rx_err(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,const struct dpaa2_fd * fd,struct dpaa2_eth_fq * fq __always_unused)655 static void dpaa2_eth_rx_err(struct dpaa2_eth_priv *priv,
656 			     struct dpaa2_eth_channel *ch,
657 			     const struct dpaa2_fd *fd,
658 			     struct dpaa2_eth_fq *fq __always_unused)
659 {
660 	struct device *dev = priv->net_dev->dev.parent;
661 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
662 	u8 fd_format = dpaa2_fd_get_format(fd);
663 	struct rtnl_link_stats64 *percpu_stats;
664 	struct dpaa2_eth_trap_item *trap_item;
665 	struct dpaa2_fapr *fapr;
666 	struct sk_buff *skb;
667 	void *buf_data;
668 	void *vaddr;
669 
670 	vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
671 	dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
672 				DMA_BIDIRECTIONAL);
673 
674 	buf_data = vaddr + dpaa2_fd_get_offset(fd);
675 
676 	if (fd_format == dpaa2_fd_single) {
677 		dma_unmap_page(dev, addr, priv->rx_buf_size,
678 			       DMA_BIDIRECTIONAL);
679 		skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
680 	} else if (fd_format == dpaa2_fd_sg) {
681 		dma_unmap_page(dev, addr, priv->rx_buf_size,
682 			       DMA_BIDIRECTIONAL);
683 		skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
684 		free_pages((unsigned long)vaddr, 0);
685 	} else {
686 		/* We don't support any other format */
687 		dpaa2_eth_free_rx_fd(priv, fd, vaddr);
688 		goto err_frame_format;
689 	}
690 
691 	fapr = dpaa2_get_fapr(vaddr, false);
692 	trap_item = dpaa2_eth_dl_get_trap(priv, fapr);
693 	if (trap_item)
694 		devlink_trap_report(priv->devlink, skb, trap_item->trap_ctx,
695 				    &priv->devlink_port, NULL);
696 	consume_skb(skb);
697 
698 err_frame_format:
699 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
700 	percpu_stats->rx_errors++;
701 	ch->buf_count--;
702 }
703 
704 /* Consume all frames pull-dequeued into the store. This is the simplest way to
705  * make sure we don't accidentally issue another volatile dequeue which would
706  * overwrite (leak) frames already in the store.
707  *
708  * Observance of NAPI budget is not our concern, leaving that to the caller.
709  */
dpaa2_eth_consume_frames(struct dpaa2_eth_channel * ch,struct dpaa2_eth_fq ** src)710 static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
711 				    struct dpaa2_eth_fq **src)
712 {
713 	struct dpaa2_eth_priv *priv = ch->priv;
714 	struct dpaa2_eth_fq *fq = NULL;
715 	struct dpaa2_dq *dq;
716 	const struct dpaa2_fd *fd;
717 	int cleaned = 0, retries = 0;
718 	int is_last;
719 
720 	do {
721 		dq = dpaa2_io_store_next(ch->store, &is_last);
722 		if (unlikely(!dq)) {
723 			/* If we're here, we *must* have placed a
724 			 * volatile dequeue comnmand, so keep reading through
725 			 * the store until we get some sort of valid response
726 			 * token (either a valid frame or an "empty dequeue")
727 			 */
728 			if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
729 				netdev_err_once(priv->net_dev,
730 						"Unable to read a valid dequeue response\n");
731 				return -ETIMEDOUT;
732 			}
733 			continue;
734 		}
735 
736 		fd = dpaa2_dq_fd(dq);
737 		fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
738 
739 		fq->consume(priv, ch, fd, fq);
740 		cleaned++;
741 		retries = 0;
742 	} while (!is_last);
743 
744 	if (!cleaned)
745 		return 0;
746 
747 	fq->stats.frames += cleaned;
748 	ch->stats.frames += cleaned;
749 	ch->stats.frames_per_cdan += cleaned;
750 
751 	/* A dequeue operation only pulls frames from a single queue
752 	 * into the store. Return the frame queue as an out param.
753 	 */
754 	if (src)
755 		*src = fq;
756 
757 	return cleaned;
758 }
759 
dpaa2_eth_ptp_parse(struct sk_buff * skb,u8 * msgtype,u8 * twostep,u8 * udp,u16 * correction_offset,u16 * origintimestamp_offset)760 static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
761 			       u8 *msgtype, u8 *twostep, u8 *udp,
762 			       u16 *correction_offset,
763 			       u16 *origintimestamp_offset)
764 {
765 	unsigned int ptp_class;
766 	struct ptp_header *hdr;
767 	unsigned int type;
768 	u8 *base;
769 
770 	ptp_class = ptp_classify_raw(skb);
771 	if (ptp_class == PTP_CLASS_NONE)
772 		return -EINVAL;
773 
774 	hdr = ptp_parse_header(skb, ptp_class);
775 	if (!hdr)
776 		return -EINVAL;
777 
778 	*msgtype = ptp_get_msgtype(hdr, ptp_class);
779 	*twostep = hdr->flag_field[0] & 0x2;
780 
781 	type = ptp_class & PTP_CLASS_PMASK;
782 	if (type == PTP_CLASS_IPV4 ||
783 	    type == PTP_CLASS_IPV6)
784 		*udp = 1;
785 	else
786 		*udp = 0;
787 
788 	base = skb_mac_header(skb);
789 	*correction_offset = (u8 *)&hdr->correction - base;
790 	*origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
791 
792 	return 0;
793 }
794 
795 /* Configure the egress frame annotation for timestamp update */
dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv * priv,struct dpaa2_fd * fd,void * buf_start,struct sk_buff * skb)796 static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
797 				       struct dpaa2_fd *fd,
798 				       void *buf_start,
799 				       struct sk_buff *skb)
800 {
801 	struct ptp_tstamp origin_timestamp;
802 	u8 msgtype, twostep, udp;
803 	struct dpaa2_faead *faead;
804 	struct dpaa2_fas *fas;
805 	struct timespec64 ts;
806 	u16 offset1, offset2;
807 	u32 ctrl, frc;
808 	__le64 *ns;
809 	u8 *data;
810 
811 	/* Mark the egress frame annotation area as valid */
812 	frc = dpaa2_fd_get_frc(fd);
813 	dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
814 
815 	/* Set hardware annotation size */
816 	ctrl = dpaa2_fd_get_ctrl(fd);
817 	dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
818 
819 	/* enable UPD (update prepanded data) bit in FAEAD field of
820 	 * hardware frame annotation area
821 	 */
822 	ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
823 	faead = dpaa2_get_faead(buf_start, true);
824 	faead->ctrl = cpu_to_le32(ctrl);
825 
826 	if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
827 		if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
828 					&offset1, &offset2) ||
829 		    msgtype != PTP_MSGTYPE_SYNC || twostep) {
830 			WARN_ONCE(1, "Bad packet for one-step timestamping\n");
831 			return;
832 		}
833 
834 		/* Mark the frame annotation status as valid */
835 		frc = dpaa2_fd_get_frc(fd);
836 		dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);
837 
838 		/* Mark the PTP flag for one step timestamping */
839 		fas = dpaa2_get_fas(buf_start, true);
840 		fas->status = cpu_to_le32(DPAA2_FAS_PTP);
841 
842 		dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
843 		ns = dpaa2_get_ts(buf_start, true);
844 		*ns = cpu_to_le64(timespec64_to_ns(&ts) /
845 				  DPAA2_PTP_CLK_PERIOD_NS);
846 
847 		/* Update current time to PTP message originTimestamp field */
848 		ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
849 		data = skb_mac_header(skb);
850 		*(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
851 		*(__be32 *)(data + offset2 + 2) =
852 			htonl(origin_timestamp.sec_lsb);
853 		*(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);
854 
855 		if (priv->ptp_correction_off == offset1)
856 			return;
857 
858 		priv->dpaa2_set_onestep_params_cb(priv, offset1, udp);
859 		priv->ptp_correction_off = offset1;
860 
861 	}
862 }
863 
dpaa2_eth_sgt_get(struct dpaa2_eth_priv * priv)864 void *dpaa2_eth_sgt_get(struct dpaa2_eth_priv *priv)
865 {
866 	struct dpaa2_eth_sgt_cache *sgt_cache;
867 	void *sgt_buf = NULL;
868 	int sgt_buf_size;
869 
870 	sgt_cache = this_cpu_ptr(priv->sgt_cache);
871 	sgt_buf_size = priv->tx_data_offset +
872 		DPAA2_ETH_SG_ENTRIES_MAX * sizeof(struct dpaa2_sg_entry);
873 
874 	if (sgt_cache->count == 0)
875 		sgt_buf = napi_alloc_frag_align(sgt_buf_size, DPAA2_ETH_TX_BUF_ALIGN);
876 	else
877 		sgt_buf = sgt_cache->buf[--sgt_cache->count];
878 	if (!sgt_buf)
879 		return NULL;
880 
881 	memset(sgt_buf, 0, sgt_buf_size);
882 
883 	return sgt_buf;
884 }
885 
dpaa2_eth_sgt_recycle(struct dpaa2_eth_priv * priv,void * sgt_buf)886 void dpaa2_eth_sgt_recycle(struct dpaa2_eth_priv *priv, void *sgt_buf)
887 {
888 	struct dpaa2_eth_sgt_cache *sgt_cache;
889 
890 	sgt_cache = this_cpu_ptr(priv->sgt_cache);
891 	if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
892 		skb_free_frag(sgt_buf);
893 	else
894 		sgt_cache->buf[sgt_cache->count++] = sgt_buf;
895 }
896 
897 /* Create a frame descriptor based on a fragmented skb */
dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv * priv,struct sk_buff * skb,struct dpaa2_fd * fd,void ** swa_addr)898 static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
899 				 struct sk_buff *skb,
900 				 struct dpaa2_fd *fd,
901 				 void **swa_addr)
902 {
903 	struct device *dev = priv->net_dev->dev.parent;
904 	void *sgt_buf = NULL;
905 	dma_addr_t addr;
906 	int nr_frags = skb_shinfo(skb)->nr_frags;
907 	struct dpaa2_sg_entry *sgt;
908 	int i, err;
909 	int sgt_buf_size;
910 	struct scatterlist *scl, *crt_scl;
911 	int num_sg;
912 	int num_dma_bufs;
913 	struct dpaa2_eth_swa *swa;
914 
915 	/* Create and map scatterlist.
916 	 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
917 	 * to go beyond nr_frags+1.
918 	 * Note: We don't support chained scatterlists
919 	 */
920 	if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
921 		return -EINVAL;
922 
923 	scl = kmalloc_objs(struct scatterlist, nr_frags + 1, GFP_ATOMIC);
924 	if (unlikely(!scl))
925 		return -ENOMEM;
926 
927 	sg_init_table(scl, nr_frags + 1);
928 	num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
929 	if (unlikely(num_sg < 0)) {
930 		err = -ENOMEM;
931 		goto dma_map_sg_failed;
932 	}
933 	num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
934 	if (unlikely(!num_dma_bufs)) {
935 		err = -ENOMEM;
936 		goto dma_map_sg_failed;
937 	}
938 
939 	/* Prepare the HW SGT structure */
940 	sgt_buf_size = priv->tx_data_offset +
941 		       sizeof(struct dpaa2_sg_entry) *  num_dma_bufs;
942 	sgt_buf = dpaa2_eth_sgt_get(priv);
943 	if (unlikely(!sgt_buf)) {
944 		err = -ENOMEM;
945 		goto sgt_buf_alloc_failed;
946 	}
947 
948 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
949 
950 	/* Fill in the HW SGT structure.
951 	 *
952 	 * sgt_buf is zeroed out, so the following fields are implicit
953 	 * in all sgt entries:
954 	 *   - offset is 0
955 	 *   - format is 'dpaa2_sg_single'
956 	 */
957 	for_each_sg(scl, crt_scl, num_dma_bufs, i) {
958 		dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
959 		dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
960 	}
961 	dpaa2_sg_set_final(&sgt[i - 1], true);
962 
963 	/* Store the skb backpointer in the SGT buffer.
964 	 * Fit the scatterlist and the number of buffers alongside the
965 	 * skb backpointer in the software annotation area. We'll need
966 	 * all of them on Tx Conf.
967 	 */
968 	*swa_addr = (void *)sgt_buf;
969 	swa = (struct dpaa2_eth_swa *)sgt_buf;
970 	swa->type = DPAA2_ETH_SWA_SG;
971 	swa->sg.skb = skb;
972 	swa->sg.scl = scl;
973 	swa->sg.num_sg = num_sg;
974 	swa->sg.sgt_size = sgt_buf_size;
975 
976 	/* Separately map the SGT buffer */
977 	addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
978 	if (unlikely(dma_mapping_error(dev, addr))) {
979 		err = -ENOMEM;
980 		goto dma_map_single_failed;
981 	}
982 	memset(fd, 0, sizeof(struct dpaa2_fd));
983 	dpaa2_fd_set_offset(fd, priv->tx_data_offset);
984 	dpaa2_fd_set_format(fd, dpaa2_fd_sg);
985 	dpaa2_fd_set_addr(fd, addr);
986 	dpaa2_fd_set_len(fd, skb->len);
987 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
988 
989 	return 0;
990 
991 dma_map_single_failed:
992 	dpaa2_eth_sgt_recycle(priv, sgt_buf);
993 sgt_buf_alloc_failed:
994 	dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
995 dma_map_sg_failed:
996 	kfree(scl);
997 	return err;
998 }
999 
1000 /* Create a SG frame descriptor based on a linear skb.
1001  *
1002  * This function is used on the Tx path when the skb headroom is not large
1003  * enough for the HW requirements, thus instead of realloc-ing the skb we
1004  * create a SG frame descriptor with only one entry.
1005  */
dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv * priv,struct sk_buff * skb,struct dpaa2_fd * fd,void ** swa_addr)1006 static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
1007 					    struct sk_buff *skb,
1008 					    struct dpaa2_fd *fd,
1009 					    void **swa_addr)
1010 {
1011 	struct device *dev = priv->net_dev->dev.parent;
1012 	struct dpaa2_sg_entry *sgt;
1013 	struct dpaa2_eth_swa *swa;
1014 	dma_addr_t addr, sgt_addr;
1015 	void *sgt_buf = NULL;
1016 	int sgt_buf_size;
1017 	int err;
1018 
1019 	/* Prepare the HW SGT structure */
1020 	sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
1021 	sgt_buf = dpaa2_eth_sgt_get(priv);
1022 	if (unlikely(!sgt_buf))
1023 		return -ENOMEM;
1024 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1025 
1026 	addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
1027 	if (unlikely(dma_mapping_error(dev, addr))) {
1028 		err = -ENOMEM;
1029 		goto data_map_failed;
1030 	}
1031 
1032 	/* Fill in the HW SGT structure */
1033 	dpaa2_sg_set_addr(sgt, addr);
1034 	dpaa2_sg_set_len(sgt, skb->len);
1035 	dpaa2_sg_set_final(sgt, true);
1036 
1037 	/* Store the skb backpointer in the SGT buffer */
1038 	*swa_addr = (void *)sgt_buf;
1039 	swa = (struct dpaa2_eth_swa *)sgt_buf;
1040 	swa->type = DPAA2_ETH_SWA_SINGLE;
1041 	swa->single.skb = skb;
1042 	swa->single.sgt_size = sgt_buf_size;
1043 
1044 	/* Separately map the SGT buffer */
1045 	sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
1046 	if (unlikely(dma_mapping_error(dev, sgt_addr))) {
1047 		err = -ENOMEM;
1048 		goto sgt_map_failed;
1049 	}
1050 
1051 	memset(fd, 0, sizeof(struct dpaa2_fd));
1052 	dpaa2_fd_set_offset(fd, priv->tx_data_offset);
1053 	dpaa2_fd_set_format(fd, dpaa2_fd_sg);
1054 	dpaa2_fd_set_addr(fd, sgt_addr);
1055 	dpaa2_fd_set_len(fd, skb->len);
1056 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1057 
1058 	return 0;
1059 
1060 sgt_map_failed:
1061 	dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
1062 data_map_failed:
1063 	dpaa2_eth_sgt_recycle(priv, sgt_buf);
1064 
1065 	return err;
1066 }
1067 
1068 /* Create a frame descriptor based on a linear skb */
dpaa2_eth_build_single_fd(struct dpaa2_eth_priv * priv,struct sk_buff * skb,struct dpaa2_fd * fd,void ** swa_addr)1069 static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
1070 				     struct sk_buff *skb,
1071 				     struct dpaa2_fd *fd,
1072 				     void **swa_addr)
1073 {
1074 	struct device *dev = priv->net_dev->dev.parent;
1075 	u8 *buffer_start, *aligned_start;
1076 	struct dpaa2_eth_swa *swa;
1077 	dma_addr_t addr;
1078 
1079 	buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
1080 	aligned_start = PTR_ALIGN(buffer_start, DPAA2_ETH_TX_BUF_ALIGN);
1081 	if (aligned_start >= skb->head)
1082 		buffer_start = aligned_start;
1083 	else
1084 		return -ENOMEM;
1085 
1086 	/* Store a backpointer to the skb at the beginning of the buffer
1087 	 * (in the private data area) such that we can release it
1088 	 * on Tx confirm
1089 	 */
1090 	*swa_addr = (void *)buffer_start;
1091 	swa = (struct dpaa2_eth_swa *)buffer_start;
1092 	swa->type = DPAA2_ETH_SWA_SINGLE;
1093 	swa->single.skb = skb;
1094 
1095 	addr = dma_map_single(dev, buffer_start,
1096 			      skb_tail_pointer(skb) - buffer_start,
1097 			      DMA_BIDIRECTIONAL);
1098 	if (unlikely(dma_mapping_error(dev, addr)))
1099 		return -ENOMEM;
1100 
1101 	memset(fd, 0, sizeof(struct dpaa2_fd));
1102 	dpaa2_fd_set_addr(fd, addr);
1103 	dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
1104 	dpaa2_fd_set_len(fd, skb->len);
1105 	dpaa2_fd_set_format(fd, dpaa2_fd_single);
1106 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1107 
1108 	return 0;
1109 }
1110 
1111 /* FD freeing routine on the Tx path
1112  *
1113  * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
1114  * back-pointed to is also freed.
1115  * This can be called either from dpaa2_eth_tx_conf() or on the error path of
1116  * dpaa2_eth_tx().
1117  */
dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,struct dpaa2_eth_fq * fq,const struct dpaa2_fd * fd,bool in_napi)1118 void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
1119 			  struct dpaa2_eth_channel *ch,
1120 			  struct dpaa2_eth_fq *fq,
1121 			  const struct dpaa2_fd *fd, bool in_napi)
1122 {
1123 	struct device *dev = priv->net_dev->dev.parent;
1124 	dma_addr_t fd_addr, sg_addr;
1125 	struct sk_buff *skb = NULL;
1126 	unsigned char *buffer_start;
1127 	struct dpaa2_eth_swa *swa;
1128 	u8 fd_format = dpaa2_fd_get_format(fd);
1129 	u32 fd_len = dpaa2_fd_get_len(fd);
1130 	struct dpaa2_sg_entry *sgt;
1131 	int should_free_skb = 1;
1132 	void *tso_hdr;
1133 	int i;
1134 
1135 	fd_addr = dpaa2_fd_get_addr(fd);
1136 	buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
1137 	swa = (struct dpaa2_eth_swa *)buffer_start;
1138 
1139 	if (fd_format == dpaa2_fd_single) {
1140 		if (swa->type == DPAA2_ETH_SWA_SINGLE) {
1141 			skb = swa->single.skb;
1142 			/* Accessing the skb buffer is safe before dma unmap,
1143 			 * because we didn't map the actual skb shell.
1144 			 */
1145 			dma_unmap_single(dev, fd_addr,
1146 					 skb_tail_pointer(skb) - buffer_start,
1147 					 DMA_BIDIRECTIONAL);
1148 		} else {
1149 			WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
1150 			dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
1151 					 DMA_BIDIRECTIONAL);
1152 		}
1153 	} else if (fd_format == dpaa2_fd_sg) {
1154 		if (swa->type == DPAA2_ETH_SWA_SG) {
1155 			skb = swa->sg.skb;
1156 
1157 			/* Unmap the scatterlist */
1158 			dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
1159 				     DMA_BIDIRECTIONAL);
1160 			kfree(swa->sg.scl);
1161 
1162 			/* Unmap the SGT buffer */
1163 			dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
1164 					 DMA_BIDIRECTIONAL);
1165 		} else if (swa->type == DPAA2_ETH_SWA_SW_TSO) {
1166 			skb = swa->tso.skb;
1167 
1168 			sgt = (struct dpaa2_sg_entry *)(buffer_start +
1169 							priv->tx_data_offset);
1170 
1171 			/* Unmap the SGT buffer */
1172 			dma_unmap_single(dev, fd_addr, swa->tso.sgt_size,
1173 					 DMA_BIDIRECTIONAL);
1174 
1175 			/* Unmap and free the header */
1176 			tso_hdr = dpaa2_iova_to_virt(priv->iommu_domain, dpaa2_sg_get_addr(sgt));
1177 			dma_unmap_single(dev, dpaa2_sg_get_addr(sgt), TSO_HEADER_SIZE,
1178 					 DMA_TO_DEVICE);
1179 			kfree(tso_hdr);
1180 
1181 			/* Unmap the other SG entries for the data */
1182 			for (i = 1; i < swa->tso.num_sg; i++)
1183 				dma_unmap_single(dev, dpaa2_sg_get_addr(&sgt[i]),
1184 						 dpaa2_sg_get_len(&sgt[i]), DMA_TO_DEVICE);
1185 
1186 			if (!swa->tso.is_last_fd)
1187 				should_free_skb = 0;
1188 		} else if (swa->type == DPAA2_ETH_SWA_XSK) {
1189 			/* Unmap the SGT Buffer */
1190 			dma_unmap_single(dev, fd_addr, swa->xsk.sgt_size,
1191 					 DMA_BIDIRECTIONAL);
1192 		} else {
1193 			skb = swa->single.skb;
1194 
1195 			/* Unmap the SGT Buffer */
1196 			dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
1197 					 DMA_BIDIRECTIONAL);
1198 
1199 			sgt = (struct dpaa2_sg_entry *)(buffer_start +
1200 							priv->tx_data_offset);
1201 			sg_addr = dpaa2_sg_get_addr(sgt);
1202 			dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
1203 		}
1204 	} else {
1205 		netdev_dbg(priv->net_dev, "Invalid FD format\n");
1206 		return;
1207 	}
1208 
1209 	if (swa->type == DPAA2_ETH_SWA_XSK) {
1210 		ch->xsk_tx_pkts_sent++;
1211 		dpaa2_eth_sgt_recycle(priv, buffer_start);
1212 		return;
1213 	}
1214 
1215 	if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
1216 		fq->dq_frames++;
1217 		fq->dq_bytes += fd_len;
1218 	}
1219 
1220 	if (swa->type == DPAA2_ETH_SWA_XDP) {
1221 		xdp_return_frame(swa->xdp.xdpf);
1222 		return;
1223 	}
1224 
1225 	/* Get the timestamp value */
1226 	if (swa->type != DPAA2_ETH_SWA_SW_TSO) {
1227 		if (skb->cb[0] == TX_TSTAMP) {
1228 			struct skb_shared_hwtstamps shhwtstamps;
1229 			__le64 *ts = dpaa2_get_ts(buffer_start, true);
1230 			u64 ns;
1231 
1232 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1233 
1234 			ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
1235 			shhwtstamps.hwtstamp = ns_to_ktime(ns);
1236 			skb_tstamp_tx(skb, &shhwtstamps);
1237 		} else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1238 			mutex_unlock(&priv->onestep_tstamp_lock);
1239 		}
1240 	}
1241 
1242 	/* Free SGT buffer allocated on tx */
1243 	if (fd_format != dpaa2_fd_single)
1244 		dpaa2_eth_sgt_recycle(priv, buffer_start);
1245 
1246 	/* Move on with skb release. If we are just confirming multiple FDs
1247 	 * from the same TSO skb then only the last one will need to free the
1248 	 * skb.
1249 	 */
1250 	if (should_free_skb)
1251 		napi_consume_skb(skb, in_napi);
1252 }
1253 
dpaa2_eth_build_gso_fd(struct dpaa2_eth_priv * priv,struct sk_buff * skb,struct dpaa2_fd * fd,int * num_fds,u32 * total_fds_len)1254 static int dpaa2_eth_build_gso_fd(struct dpaa2_eth_priv *priv,
1255 				  struct sk_buff *skb, struct dpaa2_fd *fd,
1256 				  int *num_fds, u32 *total_fds_len)
1257 {
1258 	struct device *dev = priv->net_dev->dev.parent;
1259 	int hdr_len, total_len, data_left, fd_len;
1260 	int num_sge, err, i, sgt_buf_size;
1261 	struct dpaa2_fd *fd_start = fd;
1262 	struct dpaa2_sg_entry *sgt;
1263 	struct dpaa2_eth_swa *swa;
1264 	dma_addr_t sgt_addr, addr;
1265 	dma_addr_t tso_hdr_dma;
1266 	unsigned int index = 0;
1267 	struct tso_t tso;
1268 	char *tso_hdr;
1269 	void *sgt_buf;
1270 
1271 	/* Initialize the TSO handler, and prepare the first payload */
1272 	hdr_len = tso_start(skb, &tso);
1273 	*total_fds_len = 0;
1274 
1275 	total_len = skb->len - hdr_len;
1276 	while (total_len > 0) {
1277 		/* Prepare the HW SGT structure for this frame */
1278 		sgt_buf = dpaa2_eth_sgt_get(priv);
1279 		if (unlikely(!sgt_buf)) {
1280 			netdev_err(priv->net_dev, "dpaa2_eth_sgt_get() failed\n");
1281 			err = -ENOMEM;
1282 			goto err_sgt_get;
1283 		}
1284 		sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1285 
1286 		/* Determine the data length of this frame */
1287 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1288 		total_len -= data_left;
1289 		fd_len = data_left + hdr_len;
1290 
1291 		/* Prepare packet headers: MAC + IP + TCP */
1292 		tso_hdr = kmalloc(TSO_HEADER_SIZE, GFP_ATOMIC);
1293 		if (!tso_hdr) {
1294 			err =  -ENOMEM;
1295 			goto err_alloc_tso_hdr;
1296 		}
1297 
1298 		tso_build_hdr(skb, tso_hdr, &tso, data_left, total_len == 0);
1299 		tso_hdr_dma = dma_map_single(dev, tso_hdr, TSO_HEADER_SIZE, DMA_TO_DEVICE);
1300 		if (dma_mapping_error(dev, tso_hdr_dma)) {
1301 			netdev_err(priv->net_dev, "dma_map_single(tso_hdr) failed\n");
1302 			err = -ENOMEM;
1303 			goto err_map_tso_hdr;
1304 		}
1305 
1306 		/* Setup the SG entry for the header */
1307 		dpaa2_sg_set_addr(sgt, tso_hdr_dma);
1308 		dpaa2_sg_set_len(sgt, hdr_len);
1309 		dpaa2_sg_set_final(sgt, data_left <= 0);
1310 
1311 		/* Compose the SG entries for each fragment of data */
1312 		num_sge = 1;
1313 		while (data_left > 0) {
1314 			int size;
1315 
1316 			/* Move to the next SG entry */
1317 			sgt++;
1318 			size = min_t(int, tso.size, data_left);
1319 
1320 			addr = dma_map_single(dev, tso.data, size, DMA_TO_DEVICE);
1321 			if (dma_mapping_error(dev, addr)) {
1322 				netdev_err(priv->net_dev, "dma_map_single(tso.data) failed\n");
1323 				err = -ENOMEM;
1324 				goto err_map_data;
1325 			}
1326 			dpaa2_sg_set_addr(sgt, addr);
1327 			dpaa2_sg_set_len(sgt, size);
1328 			dpaa2_sg_set_final(sgt, size == data_left);
1329 
1330 			num_sge++;
1331 
1332 			/* Build the data for the __next__ fragment */
1333 			data_left -= size;
1334 			tso_build_data(skb, &tso, size);
1335 		}
1336 
1337 		/* Store the skb backpointer in the SGT buffer */
1338 		sgt_buf_size = priv->tx_data_offset + num_sge * sizeof(struct dpaa2_sg_entry);
1339 		swa = (struct dpaa2_eth_swa *)sgt_buf;
1340 		swa->type = DPAA2_ETH_SWA_SW_TSO;
1341 		swa->tso.skb = skb;
1342 		swa->tso.num_sg = num_sge;
1343 		swa->tso.sgt_size = sgt_buf_size;
1344 		swa->tso.is_last_fd = total_len == 0 ? 1 : 0;
1345 
1346 		/* Separately map the SGT buffer */
1347 		sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
1348 		if (unlikely(dma_mapping_error(dev, sgt_addr))) {
1349 			netdev_err(priv->net_dev, "dma_map_single(sgt_buf) failed\n");
1350 			err = -ENOMEM;
1351 			goto err_map_sgt;
1352 		}
1353 
1354 		/* Setup the frame descriptor */
1355 		memset(fd, 0, sizeof(struct dpaa2_fd));
1356 		dpaa2_fd_set_offset(fd, priv->tx_data_offset);
1357 		dpaa2_fd_set_format(fd, dpaa2_fd_sg);
1358 		dpaa2_fd_set_addr(fd, sgt_addr);
1359 		dpaa2_fd_set_len(fd, fd_len);
1360 		dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1361 
1362 		*total_fds_len += fd_len;
1363 		/* Advance to the next frame descriptor */
1364 		fd++;
1365 		index++;
1366 	}
1367 
1368 	*num_fds = index;
1369 
1370 	return 0;
1371 
1372 err_map_sgt:
1373 err_map_data:
1374 	/* Unmap all the data S/G entries for the current FD */
1375 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1376 	for (i = 1; i < num_sge; i++)
1377 		dma_unmap_single(dev, dpaa2_sg_get_addr(&sgt[i]),
1378 				 dpaa2_sg_get_len(&sgt[i]), DMA_TO_DEVICE);
1379 
1380 	/* Unmap the header entry */
1381 	dma_unmap_single(dev, tso_hdr_dma, TSO_HEADER_SIZE, DMA_TO_DEVICE);
1382 err_map_tso_hdr:
1383 	kfree(tso_hdr);
1384 err_alloc_tso_hdr:
1385 	dpaa2_eth_sgt_recycle(priv, sgt_buf);
1386 err_sgt_get:
1387 	/* Free all the other FDs that were already fully created */
1388 	for (i = 0; i < index; i++)
1389 		dpaa2_eth_free_tx_fd(priv, NULL, NULL, &fd_start[i], false);
1390 
1391 	return err;
1392 }
1393 
__dpaa2_eth_tx(struct sk_buff * skb,struct net_device * net_dev)1394 static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1395 				  struct net_device *net_dev)
1396 {
1397 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1398 	int total_enqueued = 0, retries = 0, enqueued;
1399 	struct dpaa2_eth_drv_stats *percpu_extras;
1400 	struct rtnl_link_stats64 *percpu_stats;
1401 	unsigned int needed_headroom;
1402 	int num_fds = 1, max_retries;
1403 	struct dpaa2_eth_fq *fq;
1404 	struct netdev_queue *nq;
1405 	struct dpaa2_fd *fd;
1406 	int err, i, num_tc;
1407 	u16 queue_mapping;
1408 	void *swa = NULL;
1409 	u8 prio = 0;
1410 	u32 fd_len;
1411 
1412 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
1413 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
1414 	fd = (this_cpu_ptr(priv->fd))->array;
1415 
1416 	needed_headroom = dpaa2_eth_needed_headroom(skb);
1417 
1418 	/* We'll be holding a back-reference to the skb until Tx Confirmation;
1419 	 * we don't want that overwritten by a concurrent Tx with a cloned skb.
1420 	 */
1421 	skb = skb_unshare(skb, GFP_ATOMIC);
1422 	if (unlikely(!skb)) {
1423 		/* skb_unshare() has already freed the skb */
1424 		percpu_stats->tx_dropped++;
1425 		return NETDEV_TX_OK;
1426 	}
1427 
1428 	/* Setup the FD fields */
1429 
1430 	if (skb_is_gso(skb)) {
1431 		err = dpaa2_eth_build_gso_fd(priv, skb, fd, &num_fds, &fd_len);
1432 		percpu_extras->tx_sg_frames += num_fds;
1433 		percpu_extras->tx_sg_bytes += fd_len;
1434 		percpu_extras->tx_tso_frames += num_fds;
1435 		percpu_extras->tx_tso_bytes += fd_len;
1436 	} else if (skb_is_nonlinear(skb)) {
1437 		err = dpaa2_eth_build_sg_fd(priv, skb, fd, &swa);
1438 		percpu_extras->tx_sg_frames++;
1439 		percpu_extras->tx_sg_bytes += skb->len;
1440 		fd_len = dpaa2_fd_get_len(fd);
1441 	} else if (skb_headroom(skb) < needed_headroom) {
1442 		err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, fd, &swa);
1443 		percpu_extras->tx_sg_frames++;
1444 		percpu_extras->tx_sg_bytes += skb->len;
1445 		percpu_extras->tx_converted_sg_frames++;
1446 		percpu_extras->tx_converted_sg_bytes += skb->len;
1447 		fd_len = dpaa2_fd_get_len(fd);
1448 	} else {
1449 		err = dpaa2_eth_build_single_fd(priv, skb, fd, &swa);
1450 		fd_len = dpaa2_fd_get_len(fd);
1451 	}
1452 
1453 	if (unlikely(err)) {
1454 		percpu_stats->tx_dropped++;
1455 		goto err_build_fd;
1456 	}
1457 
1458 	if (swa && skb->cb[0])
1459 		dpaa2_eth_enable_tx_tstamp(priv, fd, swa, skb);
1460 
1461 	/* Tracing point */
1462 	for (i = 0; i < num_fds; i++)
1463 		trace_dpaa2_tx_fd(net_dev, &fd[i]);
1464 
1465 	/* TxConf FQ selection relies on queue id from the stack.
1466 	 * In case of a forwarded frame from another DPNI interface, we choose
1467 	 * a queue affined to the same core that processed the Rx frame
1468 	 */
1469 	queue_mapping = skb_get_queue_mapping(skb);
1470 
1471 	num_tc = netdev_get_num_tc(net_dev);
1472 
1473 	if (num_tc) {
1474 		prio = netdev_txq_to_tc(net_dev, queue_mapping);
1475 		/* Hardware interprets priority level 0 as being the highest,
1476 		 * so we need to do a reverse mapping to the netdev tc index
1477 		 */
1478 		prio = num_tc - prio - 1;
1479 		/* We have only one FQ array entry for all Tx hardware queues
1480 		 * with the same flow id (but different priority levels)
1481 		 */
1482 		queue_mapping %= dpaa2_eth_queue_count(priv);
1483 	}
1484 	fq = &priv->fq[queue_mapping];
1485 	nq = netdev_get_tx_queue(net_dev, queue_mapping);
1486 	netdev_tx_sent_queue(nq, fd_len);
1487 
1488 	/* Everything that happens after this enqueues might race with
1489 	 * the Tx confirmation callback for this frame
1490 	 */
1491 	max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
1492 	while (total_enqueued < num_fds && retries < max_retries) {
1493 		err = priv->enqueue(priv, fq, &fd[total_enqueued],
1494 				    prio, num_fds - total_enqueued, &enqueued);
1495 		if (err == -EBUSY) {
1496 			retries++;
1497 			continue;
1498 		}
1499 
1500 		total_enqueued += enqueued;
1501 	}
1502 	percpu_extras->tx_portal_busy += retries;
1503 
1504 	if (unlikely(err < 0)) {
1505 		percpu_stats->tx_errors++;
1506 		/* Clean up everything, including freeing the skb */
1507 		dpaa2_eth_free_tx_fd(priv, NULL, fq, fd, false);
1508 		netdev_tx_completed_queue(nq, 1, fd_len);
1509 	} else {
1510 		percpu_stats->tx_packets += total_enqueued;
1511 		percpu_stats->tx_bytes += fd_len;
1512 	}
1513 
1514 	return NETDEV_TX_OK;
1515 
1516 err_build_fd:
1517 	dev_kfree_skb(skb);
1518 
1519 	return NETDEV_TX_OK;
1520 }
1521 
dpaa2_eth_tx_onestep_tstamp(struct work_struct * work)1522 static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1523 {
1524 	struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1525 						   tx_onestep_tstamp);
1526 	struct sk_buff *skb;
1527 
1528 	while (true) {
1529 		skb = skb_dequeue(&priv->tx_skbs);
1530 		if (!skb)
1531 			return;
1532 
1533 		/* Lock just before TX one-step timestamping packet,
1534 		 * and release the lock in dpaa2_eth_free_tx_fd when
1535 		 * confirm the packet has been sent on hardware, or
1536 		 * when clean up during transmit failure.
1537 		 */
1538 		mutex_lock(&priv->onestep_tstamp_lock);
1539 		__dpaa2_eth_tx(skb, priv->net_dev);
1540 	}
1541 }
1542 
dpaa2_eth_tx(struct sk_buff * skb,struct net_device * net_dev)1543 static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1544 {
1545 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1546 	u8 msgtype, twostep, udp;
1547 	u16 offset1, offset2;
1548 
1549 	/* Utilize skb->cb[0] for timestamping request per skb */
1550 	skb->cb[0] = 0;
1551 
1552 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1553 		if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1554 			skb->cb[0] = TX_TSTAMP;
1555 		else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1556 			skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1557 	}
1558 
1559 	/* TX for one-step timestamping PTP Sync packet */
1560 	if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1561 		if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1562 					 &offset1, &offset2))
1563 			if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) {
1564 				skb_queue_tail(&priv->tx_skbs, skb);
1565 				queue_work(priv->dpaa2_ptp_wq,
1566 					   &priv->tx_onestep_tstamp);
1567 				return NETDEV_TX_OK;
1568 			}
1569 		/* Use two-step timestamping if not one-step timestamping
1570 		 * PTP Sync packet
1571 		 */
1572 		skb->cb[0] = TX_TSTAMP;
1573 	}
1574 
1575 	/* TX for other packets */
1576 	return __dpaa2_eth_tx(skb, net_dev);
1577 }
1578 
1579 /* Tx confirmation frame processing routine */
dpaa2_eth_tx_conf(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch,const struct dpaa2_fd * fd,struct dpaa2_eth_fq * fq)1580 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
1581 			      struct dpaa2_eth_channel *ch,
1582 			      const struct dpaa2_fd *fd,
1583 			      struct dpaa2_eth_fq *fq)
1584 {
1585 	struct rtnl_link_stats64 *percpu_stats;
1586 	struct dpaa2_eth_drv_stats *percpu_extras;
1587 	u32 fd_len = dpaa2_fd_get_len(fd);
1588 	u32 fd_errors;
1589 
1590 	/* Tracing point */
1591 	trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1592 
1593 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
1594 	percpu_extras->tx_conf_frames++;
1595 	percpu_extras->tx_conf_bytes += fd_len;
1596 	ch->stats.bytes_per_cdan += fd_len;
1597 
1598 	/* Check frame errors in the FD field */
1599 	fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
1600 	dpaa2_eth_free_tx_fd(priv, ch, fq, fd, true);
1601 
1602 	if (likely(!fd_errors))
1603 		return;
1604 
1605 	if (net_ratelimit())
1606 		netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1607 			   fd_errors);
1608 
1609 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
1610 	/* Tx-conf logically pertains to the egress path. */
1611 	percpu_stats->tx_errors++;
1612 }
1613 
dpaa2_eth_set_rx_vlan_filtering(struct dpaa2_eth_priv * priv,bool enable)1614 static int dpaa2_eth_set_rx_vlan_filtering(struct dpaa2_eth_priv *priv,
1615 					   bool enable)
1616 {
1617 	int err;
1618 
1619 	err = dpni_enable_vlan_filter(priv->mc_io, 0, priv->mc_token, enable);
1620 
1621 	if (err) {
1622 		netdev_err(priv->net_dev,
1623 			   "dpni_enable_vlan_filter failed\n");
1624 		return err;
1625 	}
1626 
1627 	return 0;
1628 }
1629 
dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv * priv,bool enable)1630 static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
1631 {
1632 	int err;
1633 
1634 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1635 			       DPNI_OFF_RX_L3_CSUM, enable);
1636 	if (err) {
1637 		netdev_err(priv->net_dev,
1638 			   "dpni_set_offload(RX_L3_CSUM) failed\n");
1639 		return err;
1640 	}
1641 
1642 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1643 			       DPNI_OFF_RX_L4_CSUM, enable);
1644 	if (err) {
1645 		netdev_err(priv->net_dev,
1646 			   "dpni_set_offload(RX_L4_CSUM) failed\n");
1647 		return err;
1648 	}
1649 
1650 	return 0;
1651 }
1652 
dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv * priv,bool enable)1653 static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
1654 {
1655 	int err;
1656 
1657 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1658 			       DPNI_OFF_TX_L3_CSUM, enable);
1659 	if (err) {
1660 		netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1661 		return err;
1662 	}
1663 
1664 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1665 			       DPNI_OFF_TX_L4_CSUM, enable);
1666 	if (err) {
1667 		netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1668 		return err;
1669 	}
1670 
1671 	return 0;
1672 }
1673 
1674 /* Perform a single release command to add buffers
1675  * to the specified buffer pool
1676  */
dpaa2_eth_add_bufs(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch)1677 static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1678 			      struct dpaa2_eth_channel *ch)
1679 {
1680 	struct xdp_buff *xdp_buffs[DPAA2_ETH_BUFS_PER_CMD];
1681 	struct device *dev = priv->net_dev->dev.parent;
1682 	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1683 	struct dpaa2_eth_swa *swa;
1684 	struct page *page;
1685 	dma_addr_t addr;
1686 	int retries = 0;
1687 	int i = 0, err;
1688 	u32 batch;
1689 
1690 	/* Allocate buffers visible to WRIOP */
1691 	if (!ch->xsk_zc) {
1692 		for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1693 			/* Also allocate skb shared info and alignment padding.
1694 			 * There is one page for each Rx buffer. WRIOP sees
1695 			 * the entire page except for a tailroom reserved for
1696 			 * skb shared info
1697 			 */
1698 			page = dev_alloc_pages(0);
1699 			if (!page)
1700 				goto err_alloc;
1701 
1702 			addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
1703 					    DMA_BIDIRECTIONAL);
1704 			if (unlikely(dma_mapping_error(dev, addr)))
1705 				goto err_map;
1706 
1707 			buf_array[i] = addr;
1708 
1709 			/* tracing point */
1710 			trace_dpaa2_eth_buf_seed(priv->net_dev,
1711 						 page_address(page),
1712 						 DPAA2_ETH_RX_BUF_RAW_SIZE,
1713 						 addr, priv->rx_buf_size,
1714 						 ch->bp->bpid);
1715 		}
1716 	} else if (xsk_buff_can_alloc(ch->xsk_pool, DPAA2_ETH_BUFS_PER_CMD)) {
1717 		/* Allocate XSK buffers for AF_XDP fast path in batches
1718 		 * of DPAA2_ETH_BUFS_PER_CMD. Bail out if the UMEM cannot
1719 		 * provide enough buffers at the moment
1720 		 */
1721 		batch = xsk_buff_alloc_batch(ch->xsk_pool, xdp_buffs,
1722 					     DPAA2_ETH_BUFS_PER_CMD);
1723 		if (!batch)
1724 			goto err_alloc;
1725 
1726 		for (i = 0; i < batch; i++) {
1727 			swa = (struct dpaa2_eth_swa *)(xdp_buffs[i]->data_hard_start +
1728 						       DPAA2_ETH_RX_HWA_SIZE);
1729 			swa->xsk.xdp_buff = xdp_buffs[i];
1730 
1731 			addr = xsk_buff_xdp_get_frame_dma(xdp_buffs[i]);
1732 			if (unlikely(dma_mapping_error(dev, addr)))
1733 				goto err_map;
1734 
1735 			buf_array[i] = addr;
1736 
1737 			trace_dpaa2_xsk_buf_seed(priv->net_dev,
1738 						 xdp_buffs[i]->data_hard_start,
1739 						 DPAA2_ETH_RX_BUF_RAW_SIZE,
1740 						 addr, priv->rx_buf_size,
1741 						 ch->bp->bpid);
1742 		}
1743 	}
1744 
1745 release_bufs:
1746 	/* In case the portal is busy, retry until successful */
1747 	while ((err = dpaa2_io_service_release(ch->dpio, ch->bp->bpid,
1748 					       buf_array, i)) == -EBUSY) {
1749 		if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1750 			break;
1751 		cpu_relax();
1752 	}
1753 
1754 	/* If release command failed, clean up and bail out;
1755 	 * not much else we can do about it
1756 	 */
1757 	if (err) {
1758 		dpaa2_eth_free_bufs(priv, buf_array, i, ch->xsk_zc);
1759 		return 0;
1760 	}
1761 
1762 	return i;
1763 
1764 err_map:
1765 	if (!ch->xsk_zc) {
1766 		__free_pages(page, 0);
1767 	} else {
1768 		for (; i < batch; i++)
1769 			xsk_buff_free(xdp_buffs[i]);
1770 	}
1771 err_alloc:
1772 	/* If we managed to allocate at least some buffers,
1773 	 * release them to hardware
1774 	 */
1775 	if (i)
1776 		goto release_bufs;
1777 
1778 	return 0;
1779 }
1780 
dpaa2_eth_seed_pool(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch)1781 static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv,
1782 			       struct dpaa2_eth_channel *ch)
1783 {
1784 	int i;
1785 	int new_count;
1786 
1787 	for (i = 0; i < DPAA2_ETH_NUM_BUFS; i += DPAA2_ETH_BUFS_PER_CMD) {
1788 		new_count = dpaa2_eth_add_bufs(priv, ch);
1789 		ch->buf_count += new_count;
1790 
1791 		if (new_count < DPAA2_ETH_BUFS_PER_CMD)
1792 			return -ENOMEM;
1793 	}
1794 
1795 	return 0;
1796 }
1797 
dpaa2_eth_seed_pools(struct dpaa2_eth_priv * priv)1798 static void dpaa2_eth_seed_pools(struct dpaa2_eth_priv *priv)
1799 {
1800 	struct net_device *net_dev = priv->net_dev;
1801 	struct dpaa2_eth_channel *channel;
1802 	int i, err = 0;
1803 
1804 	for (i = 0; i < priv->num_channels; i++) {
1805 		channel = priv->channel[i];
1806 
1807 		err = dpaa2_eth_seed_pool(priv, channel);
1808 
1809 		/* Not much to do; the buffer pool, though not filled up,
1810 		 * may still contain some buffers which would enable us
1811 		 * to limp on.
1812 		 */
1813 		if (err)
1814 			netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
1815 				   channel->bp->dev->obj_desc.id,
1816 				   channel->bp->bpid);
1817 	}
1818 }
1819 
1820 /*
1821  * Drain the specified number of buffers from one of the DPNI's private buffer
1822  * pools.
1823  * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1824  */
dpaa2_eth_drain_bufs(struct dpaa2_eth_priv * priv,int bpid,int count)1825 static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int bpid,
1826 				 int count)
1827 {
1828 	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1829 	bool xsk_zc = false;
1830 	int retries = 0;
1831 	int i, ret;
1832 
1833 	for (i = 0; i < priv->num_channels; i++)
1834 		if (priv->channel[i]->bp->bpid == bpid)
1835 			xsk_zc = priv->channel[i]->xsk_zc;
1836 
1837 	do {
1838 		ret = dpaa2_io_service_acquire(NULL, bpid, buf_array, count);
1839 		if (ret < 0) {
1840 			if (ret == -EBUSY &&
1841 			    retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
1842 				continue;
1843 			netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1844 			return;
1845 		}
1846 		dpaa2_eth_free_bufs(priv, buf_array, ret, xsk_zc);
1847 		retries = 0;
1848 	} while (ret);
1849 }
1850 
dpaa2_eth_drain_pool(struct dpaa2_eth_priv * priv,int bpid)1851 static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv, int bpid)
1852 {
1853 	int i;
1854 
1855 	/* Drain the buffer pool */
1856 	dpaa2_eth_drain_bufs(priv, bpid, DPAA2_ETH_BUFS_PER_CMD);
1857 	dpaa2_eth_drain_bufs(priv, bpid, 1);
1858 
1859 	/* Setup to zero the buffer count of all channels which were
1860 	 * using this buffer pool.
1861 	 */
1862 	for (i = 0; i < priv->num_channels; i++)
1863 		if (priv->channel[i]->bp->bpid == bpid)
1864 			priv->channel[i]->buf_count = 0;
1865 }
1866 
dpaa2_eth_drain_pools(struct dpaa2_eth_priv * priv)1867 static void dpaa2_eth_drain_pools(struct dpaa2_eth_priv *priv)
1868 {
1869 	int i;
1870 
1871 	for (i = 0; i < priv->num_bps; i++)
1872 		dpaa2_eth_drain_pool(priv, priv->bp[i]->bpid);
1873 }
1874 
1875 /* Function is called from softirq context only, so we don't need to guard
1876  * the access to percpu count
1877  */
dpaa2_eth_refill_pool(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * ch)1878 static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1879 				 struct dpaa2_eth_channel *ch)
1880 {
1881 	int new_count;
1882 
1883 	if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1884 		return 0;
1885 
1886 	do {
1887 		new_count = dpaa2_eth_add_bufs(priv, ch);
1888 		if (unlikely(!new_count)) {
1889 			/* Out of memory; abort for now, we'll try later on */
1890 			break;
1891 		}
1892 		ch->buf_count += new_count;
1893 	} while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1894 
1895 	if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1896 		return -ENOMEM;
1897 
1898 	return 0;
1899 }
1900 
dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv * priv)1901 static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1902 {
1903 	struct dpaa2_eth_sgt_cache *sgt_cache;
1904 	u16 count;
1905 	int k, i;
1906 
1907 	for_each_possible_cpu(k) {
1908 		sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1909 		count = sgt_cache->count;
1910 
1911 		for (i = 0; i < count; i++)
1912 			skb_free_frag(sgt_cache->buf[i]);
1913 		sgt_cache->count = 0;
1914 	}
1915 }
1916 
dpaa2_eth_pull_channel(struct dpaa2_eth_channel * ch)1917 static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
1918 {
1919 	int err;
1920 	int dequeues = -1;
1921 
1922 	/* Retry while portal is busy */
1923 	do {
1924 		err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1925 						    ch->store);
1926 		dequeues++;
1927 		cpu_relax();
1928 	} while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
1929 
1930 	ch->stats.dequeue_portal_busy += dequeues;
1931 	if (unlikely(err))
1932 		ch->stats.pull_err++;
1933 
1934 	return err;
1935 }
1936 
1937 /* NAPI poll routine
1938  *
1939  * Frames are dequeued from the QMan channel associated with this NAPI context.
1940  * Rx, Tx confirmation and (if configured) Rx error frames all count
1941  * towards the NAPI budget.
1942  */
dpaa2_eth_poll(struct napi_struct * napi,int budget)1943 static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1944 {
1945 	struct dpaa2_eth_channel *ch;
1946 	struct dpaa2_eth_priv *priv;
1947 	int rx_cleaned = 0, txconf_cleaned = 0;
1948 	struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1949 	struct netdev_queue *nq;
1950 	int store_cleaned, work_done;
1951 	bool work_done_zc = false;
1952 	struct list_head rx_list;
1953 	int retries = 0;
1954 	u16 flowid;
1955 	int err;
1956 
1957 	ch = container_of(napi, struct dpaa2_eth_channel, napi);
1958 	ch->xdp.res = 0;
1959 	priv = ch->priv;
1960 
1961 	INIT_LIST_HEAD(&rx_list);
1962 	ch->rx_list = &rx_list;
1963 
1964 	if (ch->xsk_zc) {
1965 		work_done_zc = dpaa2_xsk_tx(priv, ch);
1966 		/* If we reached the XSK Tx per NAPI threshold, we're done */
1967 		if (work_done_zc) {
1968 			work_done = budget;
1969 			goto out;
1970 		}
1971 	}
1972 
1973 	do {
1974 		err = dpaa2_eth_pull_channel(ch);
1975 		if (unlikely(err))
1976 			break;
1977 
1978 		/* Refill pool if appropriate */
1979 		dpaa2_eth_refill_pool(priv, ch);
1980 
1981 		store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
1982 		if (store_cleaned <= 0)
1983 			break;
1984 		if (fq->type == DPAA2_RX_FQ) {
1985 			rx_cleaned += store_cleaned;
1986 			flowid = fq->flowid;
1987 		} else {
1988 			txconf_cleaned += store_cleaned;
1989 			/* We have a single Tx conf FQ on this channel */
1990 			txc_fq = fq;
1991 		}
1992 
1993 		/* If we either consumed the whole NAPI budget with Rx frames
1994 		 * or we reached the Tx confirmations threshold, we're done.
1995 		 */
1996 		if (rx_cleaned >= budget ||
1997 		    txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1998 			work_done = budget;
1999 			if (ch->xdp.res & XDP_REDIRECT)
2000 				xdp_do_flush();
2001 			goto out;
2002 		}
2003 	} while (store_cleaned);
2004 
2005 	if (ch->xdp.res & XDP_REDIRECT)
2006 		xdp_do_flush();
2007 
2008 	/* Update NET DIM with the values for this CDAN */
2009 	dpaa2_io_update_net_dim(ch->dpio, ch->stats.frames_per_cdan,
2010 				ch->stats.bytes_per_cdan);
2011 	ch->stats.frames_per_cdan = 0;
2012 	ch->stats.bytes_per_cdan = 0;
2013 
2014 	/* We didn't consume the entire budget, so finish napi and
2015 	 * re-enable data availability notifications
2016 	 */
2017 	napi_complete_done(napi, rx_cleaned);
2018 	do {
2019 		err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
2020 		cpu_relax();
2021 	} while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
2022 	WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
2023 		  ch->nctx.desired_cpu);
2024 
2025 	work_done = max(rx_cleaned, 1);
2026 
2027 out:
2028 	netif_receive_skb_list(ch->rx_list);
2029 
2030 	if (ch->xsk_tx_pkts_sent) {
2031 		xsk_tx_completed(ch->xsk_pool, ch->xsk_tx_pkts_sent);
2032 		ch->xsk_tx_pkts_sent = 0;
2033 	}
2034 
2035 	if (txc_fq && txc_fq->dq_frames) {
2036 		nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
2037 		netdev_tx_completed_queue(nq, txc_fq->dq_frames,
2038 					  txc_fq->dq_bytes);
2039 		txc_fq->dq_frames = 0;
2040 		txc_fq->dq_bytes = 0;
2041 	}
2042 
2043 	if (rx_cleaned && ch->xdp.res & XDP_TX)
2044 		dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
2045 
2046 	return work_done;
2047 }
2048 
dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv * priv)2049 static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
2050 {
2051 	struct dpaa2_eth_channel *ch;
2052 	int i;
2053 
2054 	for (i = 0; i < priv->num_channels; i++) {
2055 		ch = priv->channel[i];
2056 		napi_enable(&ch->napi);
2057 	}
2058 }
2059 
dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv * priv)2060 static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
2061 {
2062 	struct dpaa2_eth_channel *ch;
2063 	int i;
2064 
2065 	for (i = 0; i < priv->num_channels; i++) {
2066 		ch = priv->channel[i];
2067 		napi_disable(&ch->napi);
2068 	}
2069 }
2070 
dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv * priv,bool tx_pause,bool pfc)2071 void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
2072 			       bool tx_pause, bool pfc)
2073 {
2074 	struct dpni_taildrop td = {0};
2075 	struct dpaa2_eth_fq *fq;
2076 	int i, err;
2077 
2078 	/* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
2079 	 * flow control is disabled (as it might interfere with either the
2080 	 * buffer pool depletion trigger for pause frames or with the group
2081 	 * congestion trigger for PFC frames)
2082 	 */
2083 	td.enable = !tx_pause;
2084 	if (priv->rx_fqtd_enabled == td.enable)
2085 		goto set_cgtd;
2086 
2087 	td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
2088 	td.units = DPNI_CONGESTION_UNIT_BYTES;
2089 
2090 	for (i = 0; i < priv->num_fqs; i++) {
2091 		fq = &priv->fq[i];
2092 		if (fq->type != DPAA2_RX_FQ)
2093 			continue;
2094 		err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
2095 					DPNI_CP_QUEUE, DPNI_QUEUE_RX,
2096 					fq->tc, fq->flowid, &td);
2097 		if (err) {
2098 			netdev_err(priv->net_dev,
2099 				   "dpni_set_taildrop(FQ) failed\n");
2100 			return;
2101 		}
2102 	}
2103 
2104 	priv->rx_fqtd_enabled = td.enable;
2105 
2106 set_cgtd:
2107 	/* Congestion group taildrop: threshold is in frames, per group
2108 	 * of FQs belonging to the same traffic class
2109 	 * Enabled if general Tx pause disabled or if PFCs are enabled
2110 	 * (congestion group threhsold for PFC generation is lower than the
2111 	 * CG taildrop threshold, so it won't interfere with it; we also
2112 	 * want frames in non-PFC enabled traffic classes to be kept in check)
2113 	 */
2114 	td.enable = !tx_pause || pfc;
2115 	if (priv->rx_cgtd_enabled == td.enable)
2116 		return;
2117 
2118 	td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
2119 	td.units = DPNI_CONGESTION_UNIT_FRAMES;
2120 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
2121 		err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
2122 					DPNI_CP_GROUP, DPNI_QUEUE_RX,
2123 					i, 0, &td);
2124 		if (err) {
2125 			netdev_err(priv->net_dev,
2126 				   "dpni_set_taildrop(CG) failed\n");
2127 			return;
2128 		}
2129 	}
2130 
2131 	priv->rx_cgtd_enabled = td.enable;
2132 }
2133 
dpaa2_eth_link_state_update(struct dpaa2_eth_priv * priv)2134 static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
2135 {
2136 	struct dpni_link_state state = {0};
2137 	bool tx_pause;
2138 	int err;
2139 
2140 	err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
2141 	if (unlikely(err)) {
2142 		netdev_err(priv->net_dev,
2143 			   "dpni_get_link_state() failed\n");
2144 		return err;
2145 	}
2146 
2147 	/* If Tx pause frame settings have changed, we need to update
2148 	 * Rx FQ taildrop configuration as well. We configure taildrop
2149 	 * only when pause frame generation is disabled.
2150 	 */
2151 	tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
2152 	dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
2153 
2154 	/* When we manage the MAC/PHY using phylink there is no need
2155 	 * to manually update the netif_carrier.
2156 	 * We can avoid locking because we are called from the "link changed"
2157 	 * IRQ handler, which is the same as the "endpoint changed" IRQ handler
2158 	 * (the writer to priv->mac), so we cannot race with it.
2159 	 */
2160 	if (dpaa2_mac_is_type_phy(priv->mac))
2161 		goto out;
2162 
2163 	/* Chech link state; speed / duplex changes are not treated yet */
2164 	if (priv->link_state.up == state.up)
2165 		goto out;
2166 
2167 	if (state.up) {
2168 		netif_carrier_on(priv->net_dev);
2169 		netif_tx_start_all_queues(priv->net_dev);
2170 	} else {
2171 		netif_tx_stop_all_queues(priv->net_dev);
2172 		netif_carrier_off(priv->net_dev);
2173 	}
2174 
2175 	netdev_info(priv->net_dev, "Link Event: state %s\n",
2176 		    state.up ? "up" : "down");
2177 
2178 out:
2179 	priv->link_state = state;
2180 
2181 	return 0;
2182 }
2183 
dpaa2_eth_open(struct net_device * net_dev)2184 static int dpaa2_eth_open(struct net_device *net_dev)
2185 {
2186 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2187 	int err;
2188 
2189 	dpaa2_eth_seed_pools(priv);
2190 
2191 	mutex_lock(&priv->mac_lock);
2192 
2193 	if (!dpaa2_eth_is_type_phy(priv)) {
2194 		/* We'll only start the txqs when the link is actually ready;
2195 		 * make sure we don't race against the link up notification,
2196 		 * which may come immediately after dpni_enable();
2197 		 */
2198 		netif_tx_stop_all_queues(net_dev);
2199 
2200 		/* Also, explicitly set carrier off, otherwise
2201 		 * netif_carrier_ok() will return true and cause 'ip link show'
2202 		 * to report the LOWER_UP flag, even though the link
2203 		 * notification wasn't even received.
2204 		 */
2205 		netif_carrier_off(net_dev);
2206 	}
2207 	dpaa2_eth_enable_ch_napi(priv);
2208 
2209 	err = dpni_enable(priv->mc_io, 0, priv->mc_token);
2210 	if (err < 0) {
2211 		mutex_unlock(&priv->mac_lock);
2212 		netdev_err(net_dev, "dpni_enable() failed\n");
2213 		goto enable_err;
2214 	}
2215 
2216 	if (dpaa2_eth_is_type_phy(priv))
2217 		dpaa2_mac_start(priv->mac);
2218 
2219 	mutex_unlock(&priv->mac_lock);
2220 
2221 	return 0;
2222 
2223 enable_err:
2224 	dpaa2_eth_disable_ch_napi(priv);
2225 	dpaa2_eth_drain_pools(priv);
2226 	return err;
2227 }
2228 
2229 /* Total number of in-flight frames on ingress queues */
dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv * priv)2230 static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
2231 {
2232 	struct dpaa2_eth_fq *fq;
2233 	u32 fcnt = 0, bcnt = 0, total = 0;
2234 	int i, err;
2235 
2236 	for (i = 0; i < priv->num_fqs; i++) {
2237 		fq = &priv->fq[i];
2238 		err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
2239 		if (err) {
2240 			netdev_warn(priv->net_dev, "query_fq_count failed");
2241 			break;
2242 		}
2243 		total += fcnt;
2244 	}
2245 
2246 	return total;
2247 }
2248 
dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv * priv)2249 static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
2250 {
2251 	int retries = 10;
2252 	u32 pending;
2253 
2254 	do {
2255 		pending = dpaa2_eth_ingress_fq_count(priv);
2256 		if (pending)
2257 			msleep(100);
2258 	} while (pending && --retries);
2259 }
2260 
2261 #define DPNI_TX_PENDING_VER_MAJOR	7
2262 #define DPNI_TX_PENDING_VER_MINOR	13
dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv * priv)2263 static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
2264 {
2265 	union dpni_statistics stats;
2266 	int retries = 10;
2267 	int err;
2268 
2269 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
2270 				   DPNI_TX_PENDING_VER_MINOR) < 0)
2271 		goto out;
2272 
2273 	do {
2274 		err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
2275 					  &stats);
2276 		if (err)
2277 			goto out;
2278 		if (stats.page_6.tx_pending_frames == 0)
2279 			return;
2280 	} while (--retries);
2281 
2282 out:
2283 	msleep(500);
2284 }
2285 
dpaa2_eth_stop(struct net_device * net_dev)2286 static int dpaa2_eth_stop(struct net_device *net_dev)
2287 {
2288 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2289 	int dpni_enabled = 0;
2290 	int retries = 10;
2291 
2292 	mutex_lock(&priv->mac_lock);
2293 
2294 	if (dpaa2_eth_is_type_phy(priv)) {
2295 		dpaa2_mac_stop(priv->mac);
2296 	} else {
2297 		netif_tx_stop_all_queues(net_dev);
2298 		netif_carrier_off(net_dev);
2299 	}
2300 
2301 	mutex_unlock(&priv->mac_lock);
2302 
2303 	/* On dpni_disable(), the MC firmware will:
2304 	 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
2305 	 * - cut off WRIOP dequeues from egress FQs and wait until transmission
2306 	 * of all in flight Tx frames is finished (and corresponding Tx conf
2307 	 * frames are enqueued back to software)
2308 	 *
2309 	 * Before calling dpni_disable(), we wait for all Tx frames to arrive
2310 	 * on WRIOP. After it finishes, wait until all remaining frames on Rx
2311 	 * and Tx conf queues are consumed on NAPI poll.
2312 	 */
2313 	dpaa2_eth_wait_for_egress_fq_empty(priv);
2314 
2315 	do {
2316 		dpni_disable(priv->mc_io, 0, priv->mc_token);
2317 		dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
2318 		if (dpni_enabled)
2319 			/* Allow the hardware some slack */
2320 			msleep(100);
2321 	} while (dpni_enabled && --retries);
2322 	if (!retries) {
2323 		netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
2324 		/* Must go on and disable NAPI nonetheless, so we don't crash at
2325 		 * the next "ifconfig up"
2326 		 */
2327 	}
2328 
2329 	dpaa2_eth_wait_for_ingress_fq_empty(priv);
2330 	dpaa2_eth_disable_ch_napi(priv);
2331 
2332 	/* Empty the buffer pool */
2333 	dpaa2_eth_drain_pools(priv);
2334 
2335 	/* Empty the Scatter-Gather Buffer cache */
2336 	dpaa2_eth_sgt_cache_drain(priv);
2337 
2338 	return 0;
2339 }
2340 
dpaa2_eth_set_addr(struct net_device * net_dev,void * addr)2341 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
2342 {
2343 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2344 	struct device *dev = net_dev->dev.parent;
2345 	int err;
2346 
2347 	err = eth_mac_addr(net_dev, addr);
2348 	if (err < 0) {
2349 		dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
2350 		return err;
2351 	}
2352 
2353 	err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2354 					net_dev->dev_addr);
2355 	if (err) {
2356 		dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
2357 		return err;
2358 	}
2359 
2360 	return 0;
2361 }
2362 
2363 /** Fill in counters maintained by the GPP driver. These may be different from
2364  * the hardware counters obtained by ethtool.
2365  */
dpaa2_eth_get_stats(struct net_device * net_dev,struct rtnl_link_stats64 * stats)2366 static void dpaa2_eth_get_stats(struct net_device *net_dev,
2367 				struct rtnl_link_stats64 *stats)
2368 {
2369 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2370 	struct rtnl_link_stats64 *percpu_stats;
2371 	u64 *cpustats;
2372 	u64 *netstats = (u64 *)stats;
2373 	int i, j;
2374 	int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
2375 
2376 	for_each_possible_cpu(i) {
2377 		percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
2378 		cpustats = (u64 *)percpu_stats;
2379 		for (j = 0; j < num; j++)
2380 			netstats[j] += cpustats[j];
2381 	}
2382 }
2383 
2384 /* Copy mac unicast addresses from @net_dev to @priv.
2385  * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
2386  */
dpaa2_eth_add_uc_hw_addr(const struct net_device * net_dev,struct dpaa2_eth_priv * priv)2387 static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
2388 				     struct dpaa2_eth_priv *priv)
2389 {
2390 	struct netdev_hw_addr *ha;
2391 	int err;
2392 
2393 	netdev_for_each_uc_addr(ha, net_dev) {
2394 		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
2395 					ha->addr);
2396 		if (err)
2397 			netdev_warn(priv->net_dev,
2398 				    "Could not add ucast MAC %pM to the filtering table (err %d)\n",
2399 				    ha->addr, err);
2400 	}
2401 }
2402 
2403 /* Copy mac multicast addresses from @net_dev to @priv
2404  * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
2405  */
dpaa2_eth_add_mc_hw_addr(const struct net_device * net_dev,struct dpaa2_eth_priv * priv)2406 static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
2407 				     struct dpaa2_eth_priv *priv)
2408 {
2409 	struct netdev_hw_addr *ha;
2410 	int err;
2411 
2412 	netdev_for_each_mc_addr(ha, net_dev) {
2413 		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
2414 					ha->addr);
2415 		if (err)
2416 			netdev_warn(priv->net_dev,
2417 				    "Could not add mcast MAC %pM to the filtering table (err %d)\n",
2418 				    ha->addr, err);
2419 	}
2420 }
2421 
dpaa2_eth_rx_add_vid(struct net_device * net_dev,__be16 vlan_proto,u16 vid)2422 static int dpaa2_eth_rx_add_vid(struct net_device *net_dev,
2423 				__be16 vlan_proto, u16 vid)
2424 {
2425 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2426 	int err;
2427 
2428 	err = dpni_add_vlan_id(priv->mc_io, 0, priv->mc_token,
2429 			       vid, 0, 0, 0);
2430 
2431 	if (err) {
2432 		netdev_warn(priv->net_dev,
2433 			    "Could not add the vlan id %u\n",
2434 			    vid);
2435 		return err;
2436 	}
2437 
2438 	return 0;
2439 }
2440 
dpaa2_eth_rx_kill_vid(struct net_device * net_dev,__be16 vlan_proto,u16 vid)2441 static int dpaa2_eth_rx_kill_vid(struct net_device *net_dev,
2442 				 __be16 vlan_proto, u16 vid)
2443 {
2444 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2445 	int err;
2446 
2447 	err = dpni_remove_vlan_id(priv->mc_io, 0, priv->mc_token, vid);
2448 
2449 	if (err) {
2450 		netdev_warn(priv->net_dev,
2451 			    "Could not remove the vlan id %u\n",
2452 			    vid);
2453 		return err;
2454 	}
2455 
2456 	return 0;
2457 }
2458 
dpaa2_eth_set_rx_mode(struct net_device * net_dev)2459 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
2460 {
2461 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2462 	int uc_count = netdev_uc_count(net_dev);
2463 	int mc_count = netdev_mc_count(net_dev);
2464 	u8 max_mac = priv->dpni_attrs.mac_filter_entries;
2465 	u32 options = priv->dpni_attrs.options;
2466 	u16 mc_token = priv->mc_token;
2467 	struct fsl_mc_io *mc_io = priv->mc_io;
2468 	int err;
2469 
2470 	/* Basic sanity checks; these probably indicate a misconfiguration */
2471 	if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
2472 		netdev_info(net_dev,
2473 			    "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
2474 			    max_mac);
2475 
2476 	/* Force promiscuous if the uc or mc counts exceed our capabilities. */
2477 	if (uc_count > max_mac) {
2478 		netdev_info(net_dev,
2479 			    "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
2480 			    uc_count, max_mac);
2481 		goto force_promisc;
2482 	}
2483 	if (mc_count + uc_count > max_mac) {
2484 		netdev_info(net_dev,
2485 			    "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
2486 			    uc_count + mc_count, max_mac);
2487 		goto force_mc_promisc;
2488 	}
2489 
2490 	/* Adjust promisc settings due to flag combinations */
2491 	if (net_dev->flags & IFF_PROMISC)
2492 		goto force_promisc;
2493 	if (net_dev->flags & IFF_ALLMULTI) {
2494 		/* First, rebuild unicast filtering table. This should be done
2495 		 * in promisc mode, in order to avoid frame loss while we
2496 		 * progressively add entries to the table.
2497 		 * We don't know whether we had been in promisc already, and
2498 		 * making an MC call to find out is expensive; so set uc promisc
2499 		 * nonetheless.
2500 		 */
2501 		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2502 		if (err)
2503 			netdev_warn(net_dev, "Can't set uc promisc\n");
2504 
2505 		/* Actual uc table reconstruction. */
2506 		err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
2507 		if (err)
2508 			netdev_warn(net_dev, "Can't clear uc filters\n");
2509 		dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2510 
2511 		/* Finally, clear uc promisc and set mc promisc as requested. */
2512 		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2513 		if (err)
2514 			netdev_warn(net_dev, "Can't clear uc promisc\n");
2515 		goto force_mc_promisc;
2516 	}
2517 
2518 	/* Neither unicast, nor multicast promisc will be on... eventually.
2519 	 * For now, rebuild mac filtering tables while forcing both of them on.
2520 	 */
2521 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2522 	if (err)
2523 		netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
2524 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2525 	if (err)
2526 		netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
2527 
2528 	/* Actual mac filtering tables reconstruction */
2529 	err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
2530 	if (err)
2531 		netdev_warn(net_dev, "Can't clear mac filters\n");
2532 	dpaa2_eth_add_mc_hw_addr(net_dev, priv);
2533 	dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2534 
2535 	/* Now we can clear both ucast and mcast promisc, without risking
2536 	 * to drop legitimate frames anymore.
2537 	 */
2538 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2539 	if (err)
2540 		netdev_warn(net_dev, "Can't clear ucast promisc\n");
2541 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
2542 	if (err)
2543 		netdev_warn(net_dev, "Can't clear mcast promisc\n");
2544 
2545 	return;
2546 
2547 force_promisc:
2548 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2549 	if (err)
2550 		netdev_warn(net_dev, "Can't set ucast promisc\n");
2551 force_mc_promisc:
2552 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2553 	if (err)
2554 		netdev_warn(net_dev, "Can't set mcast promisc\n");
2555 }
2556 
dpaa2_eth_set_features(struct net_device * net_dev,netdev_features_t features)2557 static int dpaa2_eth_set_features(struct net_device *net_dev,
2558 				  netdev_features_t features)
2559 {
2560 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2561 	netdev_features_t changed = features ^ net_dev->features;
2562 	bool enable;
2563 	int err;
2564 
2565 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
2566 		enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER);
2567 		err = dpaa2_eth_set_rx_vlan_filtering(priv, enable);
2568 		if (err)
2569 			return err;
2570 	}
2571 
2572 	if (changed & NETIF_F_RXCSUM) {
2573 		enable = !!(features & NETIF_F_RXCSUM);
2574 		err = dpaa2_eth_set_rx_csum(priv, enable);
2575 		if (err)
2576 			return err;
2577 	}
2578 
2579 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2580 		enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
2581 		err = dpaa2_eth_set_tx_csum(priv, enable);
2582 		if (err)
2583 			return err;
2584 	}
2585 
2586 	return 0;
2587 }
2588 
dpaa2_eth_hwtstamp_set(struct net_device * dev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)2589 static int dpaa2_eth_hwtstamp_set(struct net_device *dev,
2590 				  struct kernel_hwtstamp_config *config,
2591 				  struct netlink_ext_ack *extack)
2592 {
2593 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2594 
2595 	if (!dpaa2_ptp)
2596 		return -EINVAL;
2597 
2598 	switch (config->tx_type) {
2599 	case HWTSTAMP_TX_OFF:
2600 	case HWTSTAMP_TX_ON:
2601 	case HWTSTAMP_TX_ONESTEP_SYNC:
2602 		priv->tx_tstamp_type = config->tx_type;
2603 		break;
2604 	default:
2605 		return -ERANGE;
2606 	}
2607 
2608 	if (config->rx_filter == HWTSTAMP_FILTER_NONE) {
2609 		priv->rx_tstamp = false;
2610 	} else {
2611 		priv->rx_tstamp = true;
2612 		/* TS is set for all frame types, not only those requested */
2613 		config->rx_filter = HWTSTAMP_FILTER_ALL;
2614 	}
2615 
2616 	if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
2617 		dpaa2_ptp_onestep_reg_update_method(priv);
2618 
2619 	return 0;
2620 }
2621 
dpaa2_eth_hwtstamp_get(struct net_device * dev,struct kernel_hwtstamp_config * config)2622 static int dpaa2_eth_hwtstamp_get(struct net_device *dev,
2623 				  struct kernel_hwtstamp_config *config)
2624 {
2625 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2626 
2627 	if (!dpaa2_ptp)
2628 		return -EINVAL;
2629 
2630 	config->tx_type = priv->tx_tstamp_type;
2631 	config->rx_filter = priv->rx_tstamp ? HWTSTAMP_FILTER_ALL :
2632 			    HWTSTAMP_FILTER_NONE;
2633 
2634 	return 0;
2635 }
2636 
dpaa2_eth_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)2637 static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2638 {
2639 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2640 	int err;
2641 
2642 	mutex_lock(&priv->mac_lock);
2643 
2644 	if (dpaa2_eth_is_type_phy(priv)) {
2645 		err = phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2646 		mutex_unlock(&priv->mac_lock);
2647 		return err;
2648 	}
2649 
2650 	mutex_unlock(&priv->mac_lock);
2651 
2652 	return -EOPNOTSUPP;
2653 }
2654 
xdp_mtu_valid(struct dpaa2_eth_priv * priv,int mtu)2655 static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2656 {
2657 	int mfl, linear_mfl;
2658 
2659 	mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2660 	linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
2661 		     dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
2662 
2663 	if (mfl > linear_mfl) {
2664 		netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2665 			    linear_mfl - VLAN_ETH_HLEN);
2666 		return false;
2667 	}
2668 
2669 	return true;
2670 }
2671 
dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv * priv,int mtu,bool has_xdp)2672 static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
2673 {
2674 	int mfl, err;
2675 
2676 	/* We enforce a maximum Rx frame length based on MTU only if we have
2677 	 * an XDP program attached (in order to avoid Rx S/G frames).
2678 	 * Otherwise, we accept all incoming frames as long as they are not
2679 	 * larger than maximum size supported in hardware
2680 	 */
2681 	if (has_xdp)
2682 		mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2683 	else
2684 		mfl = DPAA2_ETH_MFL;
2685 
2686 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2687 	if (err) {
2688 		netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2689 		return err;
2690 	}
2691 
2692 	return 0;
2693 }
2694 
dpaa2_eth_change_mtu(struct net_device * dev,int new_mtu)2695 static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2696 {
2697 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2698 	int err;
2699 
2700 	if (!priv->xdp_prog)
2701 		goto out;
2702 
2703 	if (!xdp_mtu_valid(priv, new_mtu))
2704 		return -EINVAL;
2705 
2706 	err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
2707 	if (err)
2708 		return err;
2709 
2710 out:
2711 	WRITE_ONCE(dev->mtu, new_mtu);
2712 	return 0;
2713 }
2714 
dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv * priv,bool has_xdp)2715 static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
2716 {
2717 	struct dpni_buffer_layout buf_layout = {0};
2718 	int err;
2719 
2720 	err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2721 				     DPNI_QUEUE_RX, &buf_layout);
2722 	if (err) {
2723 		netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2724 		return err;
2725 	}
2726 
2727 	/* Reserve extra headroom for XDP header size changes */
2728 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2729 				    (has_xdp ? XDP_PACKET_HEADROOM : 0);
2730 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2731 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2732 				     DPNI_QUEUE_RX, &buf_layout);
2733 	if (err) {
2734 		netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2735 		return err;
2736 	}
2737 
2738 	return 0;
2739 }
2740 
dpaa2_eth_setup_xdp(struct net_device * dev,struct bpf_prog * prog)2741 static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
2742 {
2743 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2744 	struct dpaa2_eth_channel *ch;
2745 	struct bpf_prog *old;
2746 	bool up, need_update;
2747 	int i, err;
2748 
2749 	if (prog && !xdp_mtu_valid(priv, dev->mtu))
2750 		return -EINVAL;
2751 
2752 	if (prog)
2753 		bpf_prog_add(prog, priv->num_channels);
2754 
2755 	up = netif_running(dev);
2756 	need_update = (!!priv->xdp_prog != !!prog);
2757 
2758 	if (up)
2759 		dev_close(dev);
2760 
2761 	/* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2762 	 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2763 	 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2764 	 * so we are sure no old format buffers will be used from now on.
2765 	 */
2766 	if (need_update) {
2767 		err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
2768 		if (err)
2769 			goto out_err;
2770 		err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
2771 		if (err)
2772 			goto out_err;
2773 	}
2774 
2775 	old = xchg(&priv->xdp_prog, prog);
2776 	if (old)
2777 		bpf_prog_put(old);
2778 
2779 	for (i = 0; i < priv->num_channels; i++) {
2780 		ch = priv->channel[i];
2781 		old = xchg(&ch->xdp.prog, prog);
2782 		if (old)
2783 			bpf_prog_put(old);
2784 	}
2785 
2786 	if (up) {
2787 		err = dev_open(dev, NULL);
2788 		if (err)
2789 			return err;
2790 	}
2791 
2792 	return 0;
2793 
2794 out_err:
2795 	if (prog)
2796 		bpf_prog_sub(prog, priv->num_channels);
2797 	if (up)
2798 		dev_open(dev, NULL);
2799 
2800 	return err;
2801 }
2802 
dpaa2_eth_xdp(struct net_device * dev,struct netdev_bpf * xdp)2803 static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2804 {
2805 	switch (xdp->command) {
2806 	case XDP_SETUP_PROG:
2807 		return dpaa2_eth_setup_xdp(dev, xdp->prog);
2808 	case XDP_SETUP_XSK_POOL:
2809 		return dpaa2_xsk_setup_pool(dev, xdp->xsk.pool, xdp->xsk.queue_id);
2810 	default:
2811 		return -EINVAL;
2812 	}
2813 
2814 	return 0;
2815 }
2816 
dpaa2_eth_xdp_create_fd(struct net_device * net_dev,struct xdp_frame * xdpf,struct dpaa2_fd * fd)2817 static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2818 				   struct xdp_frame *xdpf,
2819 				   struct dpaa2_fd *fd)
2820 {
2821 	struct device *dev = net_dev->dev.parent;
2822 	unsigned int needed_headroom;
2823 	struct dpaa2_eth_swa *swa;
2824 	void *buffer_start, *aligned_start;
2825 	dma_addr_t addr;
2826 
2827 	/* We require a minimum headroom to be able to transmit the frame.
2828 	 * Otherwise return an error and let the original net_device handle it
2829 	 */
2830 	needed_headroom = dpaa2_eth_needed_headroom(NULL);
2831 	if (xdpf->headroom < needed_headroom)
2832 		return -EINVAL;
2833 
2834 	/* Setup the FD fields */
2835 	memset(fd, 0, sizeof(*fd));
2836 
2837 	/* Align FD address, if possible */
2838 	buffer_start = xdpf->data - needed_headroom;
2839 	aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2840 				  DPAA2_ETH_TX_BUF_ALIGN);
2841 	if (aligned_start >= xdpf->data - xdpf->headroom)
2842 		buffer_start = aligned_start;
2843 
2844 	swa = (struct dpaa2_eth_swa *)buffer_start;
2845 	/* fill in necessary fields here */
2846 	swa->type = DPAA2_ETH_SWA_XDP;
2847 	swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2848 	swa->xdp.xdpf = xdpf;
2849 
2850 	addr = dma_map_single(dev, buffer_start,
2851 			      swa->xdp.dma_size,
2852 			      DMA_BIDIRECTIONAL);
2853 	if (unlikely(dma_mapping_error(dev, addr)))
2854 		return -ENOMEM;
2855 
2856 	dpaa2_fd_set_addr(fd, addr);
2857 	dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2858 	dpaa2_fd_set_len(fd, xdpf->len);
2859 	dpaa2_fd_set_format(fd, dpaa2_fd_single);
2860 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
2861 
2862 	return 0;
2863 }
2864 
dpaa2_eth_xdp_xmit(struct net_device * net_dev,int n,struct xdp_frame ** frames,u32 flags)2865 static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2866 			      struct xdp_frame **frames, u32 flags)
2867 {
2868 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2869 	struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
2870 	struct rtnl_link_stats64 *percpu_stats;
2871 	struct dpaa2_eth_fq *fq;
2872 	struct dpaa2_fd *fds;
2873 	int enqueued, i, err;
2874 
2875 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2876 		return -EINVAL;
2877 
2878 	if (!netif_running(net_dev))
2879 		return -ENETDOWN;
2880 
2881 	fq = &priv->fq[smp_processor_id()];
2882 	xdp_redirect_fds = &fq->xdp_redirect_fds;
2883 	fds = xdp_redirect_fds->fds;
2884 
2885 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
2886 
2887 	/* create a FD for each xdp_frame in the list received */
2888 	for (i = 0; i < n; i++) {
2889 		err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2890 		if (err)
2891 			break;
2892 	}
2893 	xdp_redirect_fds->num = i;
2894 
2895 	/* enqueue all the frame descriptors */
2896 	enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
2897 
2898 	/* update statistics */
2899 	percpu_stats->tx_packets += enqueued;
2900 	for (i = 0; i < enqueued; i++)
2901 		percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
2902 
2903 	return enqueued;
2904 }
2905 
update_xps(struct dpaa2_eth_priv * priv)2906 static int update_xps(struct dpaa2_eth_priv *priv)
2907 {
2908 	struct net_device *net_dev = priv->net_dev;
2909 	int i, num_queues, netdev_queues;
2910 	struct dpaa2_eth_fq *fq;
2911 	cpumask_var_t xps_mask;
2912 	int err = 0;
2913 
2914 	if (!alloc_cpumask_var(&xps_mask, GFP_KERNEL))
2915 		return -ENOMEM;
2916 
2917 	num_queues = dpaa2_eth_queue_count(priv);
2918 	netdev_queues = (netdev_get_num_tc(net_dev) ? : 1) * num_queues;
2919 
2920 	/* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2921 	 * queues, so only process those
2922 	 */
2923 	for (i = 0; i < netdev_queues; i++) {
2924 		fq = &priv->fq[i % num_queues];
2925 
2926 		cpumask_clear(xps_mask);
2927 		cpumask_set_cpu(fq->target_cpu, xps_mask);
2928 
2929 		err = netif_set_xps_queue(net_dev, xps_mask, i);
2930 		if (err) {
2931 			netdev_warn_once(net_dev, "Error setting XPS queue\n");
2932 			break;
2933 		}
2934 	}
2935 
2936 	free_cpumask_var(xps_mask);
2937 	return err;
2938 }
2939 
dpaa2_eth_setup_mqprio(struct net_device * net_dev,struct tc_mqprio_qopt * mqprio)2940 static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2941 				  struct tc_mqprio_qopt *mqprio)
2942 {
2943 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2944 	u8 num_tc, num_queues;
2945 	int i;
2946 
2947 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2948 	num_queues = dpaa2_eth_queue_count(priv);
2949 	num_tc = mqprio->num_tc;
2950 
2951 	if (num_tc == netdev_get_num_tc(net_dev))
2952 		return 0;
2953 
2954 	if (num_tc  > dpaa2_eth_tc_count(priv)) {
2955 		netdev_err(net_dev, "Max %d traffic classes supported\n",
2956 			   dpaa2_eth_tc_count(priv));
2957 		return -EOPNOTSUPP;
2958 	}
2959 
2960 	if (!num_tc) {
2961 		netdev_reset_tc(net_dev);
2962 		netif_set_real_num_tx_queues(net_dev, num_queues);
2963 		goto out;
2964 	}
2965 
2966 	netdev_set_num_tc(net_dev, num_tc);
2967 	netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2968 
2969 	for (i = 0; i < num_tc; i++)
2970 		netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2971 
2972 out:
2973 	update_xps(priv);
2974 
2975 	return 0;
2976 }
2977 
2978 #define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2979 
dpaa2_eth_setup_tbf(struct net_device * net_dev,struct tc_tbf_qopt_offload * p)2980 static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2981 {
2982 	struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2983 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2984 	struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2985 	struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2986 	int err;
2987 
2988 	if (p->command == TC_TBF_STATS)
2989 		return -EOPNOTSUPP;
2990 
2991 	/* Only per port Tx shaping */
2992 	if (p->parent != TC_H_ROOT)
2993 		return -EOPNOTSUPP;
2994 
2995 	if (p->command == TC_TBF_REPLACE) {
2996 		if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2997 			netdev_err(net_dev, "burst size cannot be greater than %d\n",
2998 				   DPAA2_ETH_MAX_BURST_SIZE);
2999 			return -EINVAL;
3000 		}
3001 
3002 		tx_cr_shaper.max_burst_size = cfg->max_size;
3003 		/* The TBF interface is in bytes/s, whereas DPAA2 expects the
3004 		 * rate in Mbits/s
3005 		 */
3006 		tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
3007 	}
3008 
3009 	err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
3010 				  &tx_er_shaper, 0);
3011 	if (err) {
3012 		netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
3013 		return err;
3014 	}
3015 
3016 	return 0;
3017 }
3018 
dpaa2_eth_setup_tc(struct net_device * net_dev,enum tc_setup_type type,void * type_data)3019 static int dpaa2_eth_setup_tc(struct net_device *net_dev,
3020 			      enum tc_setup_type type, void *type_data)
3021 {
3022 	switch (type) {
3023 	case TC_SETUP_QDISC_MQPRIO:
3024 		return dpaa2_eth_setup_mqprio(net_dev, type_data);
3025 	case TC_SETUP_QDISC_TBF:
3026 		return dpaa2_eth_setup_tbf(net_dev, type_data);
3027 	default:
3028 		return -EOPNOTSUPP;
3029 	}
3030 }
3031 
3032 static const struct net_device_ops dpaa2_eth_ops = {
3033 	.ndo_open = dpaa2_eth_open,
3034 	.ndo_start_xmit = dpaa2_eth_tx,
3035 	.ndo_stop = dpaa2_eth_stop,
3036 	.ndo_set_mac_address = dpaa2_eth_set_addr,
3037 	.ndo_get_stats64 = dpaa2_eth_get_stats,
3038 	.ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
3039 	.ndo_set_features = dpaa2_eth_set_features,
3040 	.ndo_eth_ioctl = dpaa2_eth_ioctl,
3041 	.ndo_change_mtu = dpaa2_eth_change_mtu,
3042 	.ndo_bpf = dpaa2_eth_xdp,
3043 	.ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
3044 	.ndo_xsk_wakeup = dpaa2_xsk_wakeup,
3045 	.ndo_setup_tc = dpaa2_eth_setup_tc,
3046 	.ndo_vlan_rx_add_vid = dpaa2_eth_rx_add_vid,
3047 	.ndo_vlan_rx_kill_vid = dpaa2_eth_rx_kill_vid,
3048 	.ndo_hwtstamp_get = dpaa2_eth_hwtstamp_get,
3049 	.ndo_hwtstamp_set = dpaa2_eth_hwtstamp_set,
3050 };
3051 
dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx * ctx)3052 static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
3053 {
3054 	struct dpaa2_eth_channel *ch;
3055 
3056 	ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
3057 
3058 	/* Update NAPI statistics */
3059 	ch->stats.cdan++;
3060 
3061 	/* NAPI can also be scheduled from the AF_XDP Tx path. Mark a missed
3062 	 * so that it can be rescheduled again.
3063 	 */
3064 	if (!napi_if_scheduled_mark_missed(&ch->napi))
3065 		napi_schedule(&ch->napi);
3066 }
3067 
3068 /* Allocate and configure a DPCON object */
dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv * priv)3069 static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
3070 {
3071 	struct fsl_mc_device *dpcon;
3072 	struct device *dev = priv->net_dev->dev.parent;
3073 	int err;
3074 
3075 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
3076 				     FSL_MC_POOL_DPCON, &dpcon);
3077 	if (err) {
3078 		if (err == -ENXIO) {
3079 			dev_dbg(dev, "Waiting for DPCON\n");
3080 			err = -EPROBE_DEFER;
3081 		} else {
3082 			dev_info(dev, "Not enough DPCONs, will go on as-is\n");
3083 		}
3084 		return ERR_PTR(err);
3085 	}
3086 
3087 	err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
3088 	if (err) {
3089 		dev_err(dev, "dpcon_open() failed\n");
3090 		goto free;
3091 	}
3092 
3093 	err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
3094 	if (err) {
3095 		dev_err(dev, "dpcon_reset() failed\n");
3096 		goto close;
3097 	}
3098 
3099 	err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
3100 	if (err) {
3101 		dev_err(dev, "dpcon_enable() failed\n");
3102 		goto close;
3103 	}
3104 
3105 	return dpcon;
3106 
3107 close:
3108 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
3109 free:
3110 	fsl_mc_object_free(dpcon);
3111 
3112 	return ERR_PTR(err);
3113 }
3114 
dpaa2_eth_free_dpcon(struct dpaa2_eth_priv * priv,struct fsl_mc_device * dpcon)3115 static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
3116 				 struct fsl_mc_device *dpcon)
3117 {
3118 	dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
3119 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
3120 	fsl_mc_object_free(dpcon);
3121 }
3122 
dpaa2_eth_alloc_channel(struct dpaa2_eth_priv * priv)3123 static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
3124 {
3125 	struct dpaa2_eth_channel *channel;
3126 	struct dpcon_attr attr;
3127 	struct device *dev = priv->net_dev->dev.parent;
3128 	int err;
3129 
3130 	channel = kzalloc_obj(*channel);
3131 	if (!channel)
3132 		return NULL;
3133 
3134 	channel->dpcon = dpaa2_eth_setup_dpcon(priv);
3135 	if (IS_ERR(channel->dpcon)) {
3136 		err = PTR_ERR(channel->dpcon);
3137 		goto err_setup;
3138 	}
3139 
3140 	err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
3141 				   &attr);
3142 	if (err) {
3143 		dev_err(dev, "dpcon_get_attributes() failed\n");
3144 		goto err_get_attr;
3145 	}
3146 
3147 	channel->dpcon_id = attr.id;
3148 	channel->ch_id = attr.qbman_ch_id;
3149 	channel->priv = priv;
3150 
3151 	return channel;
3152 
3153 err_get_attr:
3154 	dpaa2_eth_free_dpcon(priv, channel->dpcon);
3155 err_setup:
3156 	kfree(channel);
3157 	return ERR_PTR(err);
3158 }
3159 
dpaa2_eth_free_channel(struct dpaa2_eth_priv * priv,struct dpaa2_eth_channel * channel)3160 static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
3161 				   struct dpaa2_eth_channel *channel)
3162 {
3163 	dpaa2_eth_free_dpcon(priv, channel->dpcon);
3164 	kfree(channel);
3165 }
3166 
3167 /* DPIO setup: allocate and configure QBMan channels, setup core affinity
3168  * and register data availability notifications
3169  */
dpaa2_eth_setup_dpio(struct dpaa2_eth_priv * priv)3170 static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
3171 {
3172 	struct dpaa2_io_notification_ctx *nctx;
3173 	struct dpaa2_eth_channel *channel;
3174 	struct dpcon_notification_cfg dpcon_notif_cfg;
3175 	struct device *dev = priv->net_dev->dev.parent;
3176 	int i, err;
3177 
3178 	/* We want the ability to spread ingress traffic (RX, TX conf) to as
3179 	 * many cores as possible, so we need one channel for each core
3180 	 * (unless there's fewer queues than cores, in which case the extra
3181 	 * channels would be wasted).
3182 	 * Allocate one channel per core and register it to the core's
3183 	 * affine DPIO. If not enough channels are available for all cores
3184 	 * or if some cores don't have an affine DPIO, there will be no
3185 	 * ingress frame processing on those cores.
3186 	 */
3187 	cpumask_clear(&priv->dpio_cpumask);
3188 	for_each_online_cpu(i) {
3189 		/* Try to allocate a channel */
3190 		channel = dpaa2_eth_alloc_channel(priv);
3191 		if (IS_ERR_OR_NULL(channel)) {
3192 			err = PTR_ERR_OR_ZERO(channel);
3193 			if (err == -EPROBE_DEFER)
3194 				dev_dbg(dev, "waiting for affine channel\n");
3195 			else
3196 				dev_info(dev,
3197 					 "No affine channel for cpu %d and above\n", i);
3198 			goto err_alloc_ch;
3199 		}
3200 
3201 		priv->channel[priv->num_channels] = channel;
3202 
3203 		nctx = &channel->nctx;
3204 		nctx->is_cdan = 1;
3205 		nctx->cb = dpaa2_eth_cdan_cb;
3206 		nctx->id = channel->ch_id;
3207 		nctx->desired_cpu = i;
3208 
3209 		/* Register the new context */
3210 		channel->dpio = dpaa2_io_service_select(i);
3211 		err = dpaa2_io_service_register(channel->dpio, nctx, dev);
3212 		if (err) {
3213 			dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
3214 			/* If no affine DPIO for this core, there's probably
3215 			 * none available for next cores either. Signal we want
3216 			 * to retry later, in case the DPIO devices weren't
3217 			 * probed yet.
3218 			 */
3219 			err = -EPROBE_DEFER;
3220 			goto err_service_reg;
3221 		}
3222 
3223 		/* Register DPCON notification with MC */
3224 		dpcon_notif_cfg.dpio_id = nctx->dpio_id;
3225 		dpcon_notif_cfg.priority = 0;
3226 		dpcon_notif_cfg.user_ctx = nctx->qman64;
3227 		err = dpcon_set_notification(priv->mc_io, 0,
3228 					     channel->dpcon->mc_handle,
3229 					     &dpcon_notif_cfg);
3230 		if (err) {
3231 			dev_err(dev, "dpcon_set_notification failed()\n");
3232 			goto err_set_cdan;
3233 		}
3234 
3235 		/* If we managed to allocate a channel and also found an affine
3236 		 * DPIO for this core, add it to the final mask
3237 		 */
3238 		cpumask_set_cpu(i, &priv->dpio_cpumask);
3239 		priv->num_channels++;
3240 
3241 		/* Stop if we already have enough channels to accommodate all
3242 		 * RX and TX conf queues
3243 		 */
3244 		if (priv->num_channels == priv->dpni_attrs.num_queues)
3245 			break;
3246 	}
3247 
3248 	return 0;
3249 
3250 err_set_cdan:
3251 	dpaa2_io_service_deregister(channel->dpio, nctx, dev);
3252 err_service_reg:
3253 	dpaa2_eth_free_channel(priv, channel);
3254 err_alloc_ch:
3255 	if (err == -EPROBE_DEFER) {
3256 		for (i = 0; i < priv->num_channels; i++) {
3257 			channel = priv->channel[i];
3258 			nctx = &channel->nctx;
3259 			dpaa2_io_service_deregister(channel->dpio, nctx, dev);
3260 			dpaa2_eth_free_channel(priv, channel);
3261 		}
3262 		priv->num_channels = 0;
3263 		return err;
3264 	}
3265 
3266 	if (cpumask_empty(&priv->dpio_cpumask)) {
3267 		dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
3268 		return -ENODEV;
3269 	}
3270 
3271 	dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
3272 		 cpumask_pr_args(&priv->dpio_cpumask));
3273 
3274 	return 0;
3275 }
3276 
dpaa2_eth_free_dpio(struct dpaa2_eth_priv * priv)3277 static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
3278 {
3279 	struct device *dev = priv->net_dev->dev.parent;
3280 	struct dpaa2_eth_channel *ch;
3281 	int i;
3282 
3283 	/* deregister CDAN notifications and free channels */
3284 	for (i = 0; i < priv->num_channels; i++) {
3285 		ch = priv->channel[i];
3286 		dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
3287 		dpaa2_eth_free_channel(priv, ch);
3288 	}
3289 }
3290 
dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv * priv,int cpu)3291 static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
3292 							      int cpu)
3293 {
3294 	struct device *dev = priv->net_dev->dev.parent;
3295 	int i;
3296 
3297 	for (i = 0; i < priv->num_channels; i++)
3298 		if (priv->channel[i]->nctx.desired_cpu == cpu)
3299 			return priv->channel[i];
3300 
3301 	/* We should never get here. Issue a warning and return
3302 	 * the first channel, because it's still better than nothing
3303 	 */
3304 	dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
3305 
3306 	return priv->channel[0];
3307 }
3308 
dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv * priv)3309 static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
3310 {
3311 	struct device *dev = priv->net_dev->dev.parent;
3312 	struct dpaa2_eth_fq *fq;
3313 	int rx_cpu, txc_cpu;
3314 	int i;
3315 
3316 	/* For each FQ, pick one channel/CPU to deliver frames to.
3317 	 * This may well change at runtime, either through irqbalance or
3318 	 * through direct user intervention.
3319 	 */
3320 	rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
3321 
3322 	for (i = 0; i < priv->num_fqs; i++) {
3323 		fq = &priv->fq[i];
3324 		switch (fq->type) {
3325 		case DPAA2_RX_FQ:
3326 		case DPAA2_RX_ERR_FQ:
3327 			fq->target_cpu = rx_cpu;
3328 			rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
3329 			if (rx_cpu >= nr_cpu_ids)
3330 				rx_cpu = cpumask_first(&priv->dpio_cpumask);
3331 			break;
3332 		case DPAA2_TX_CONF_FQ:
3333 			fq->target_cpu = txc_cpu;
3334 			txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
3335 			if (txc_cpu >= nr_cpu_ids)
3336 				txc_cpu = cpumask_first(&priv->dpio_cpumask);
3337 			break;
3338 		default:
3339 			dev_err(dev, "Unknown FQ type: %d\n", fq->type);
3340 		}
3341 		fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
3342 	}
3343 
3344 	update_xps(priv);
3345 }
3346 
dpaa2_eth_setup_fqs(struct dpaa2_eth_priv * priv)3347 static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
3348 {
3349 	int i, j;
3350 
3351 	/* We have one TxConf FQ per Tx flow.
3352 	 * The number of Tx and Rx queues is the same.
3353 	 * Tx queues come first in the fq array.
3354 	 */
3355 	for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
3356 		priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
3357 		priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
3358 		priv->fq[priv->num_fqs++].flowid = (u16)i;
3359 	}
3360 
3361 	for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3362 		for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
3363 			priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
3364 			priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
3365 			priv->fq[priv->num_fqs].tc = (u8)j;
3366 			priv->fq[priv->num_fqs++].flowid = (u16)i;
3367 		}
3368 	}
3369 
3370 	/* We have exactly one Rx error queue per DPNI */
3371 	priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
3372 	priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
3373 
3374 	/* For each FQ, decide on which core to process incoming frames */
3375 	dpaa2_eth_set_fq_affinity(priv);
3376 }
3377 
3378 /* Allocate and configure a buffer pool */
dpaa2_eth_allocate_dpbp(struct dpaa2_eth_priv * priv)3379 struct dpaa2_eth_bp *dpaa2_eth_allocate_dpbp(struct dpaa2_eth_priv *priv)
3380 {
3381 	struct device *dev = priv->net_dev->dev.parent;
3382 	struct fsl_mc_device *dpbp_dev;
3383 	struct dpbp_attr dpbp_attrs;
3384 	struct dpaa2_eth_bp *bp;
3385 	int err;
3386 
3387 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
3388 				     &dpbp_dev);
3389 	if (err) {
3390 		if (err == -ENXIO)
3391 			err = -EPROBE_DEFER;
3392 		else
3393 			dev_err(dev, "DPBP device allocation failed\n");
3394 		return ERR_PTR(err);
3395 	}
3396 
3397 	bp = kzalloc_obj(*bp);
3398 	if (!bp) {
3399 		err = -ENOMEM;
3400 		goto err_alloc;
3401 	}
3402 
3403 	err = dpbp_open(priv->mc_io, 0, dpbp_dev->obj_desc.id,
3404 			&dpbp_dev->mc_handle);
3405 	if (err) {
3406 		dev_err(dev, "dpbp_open() failed\n");
3407 		goto err_open;
3408 	}
3409 
3410 	err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
3411 	if (err) {
3412 		dev_err(dev, "dpbp_reset() failed\n");
3413 		goto err_reset;
3414 	}
3415 
3416 	err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
3417 	if (err) {
3418 		dev_err(dev, "dpbp_enable() failed\n");
3419 		goto err_enable;
3420 	}
3421 
3422 	err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
3423 				  &dpbp_attrs);
3424 	if (err) {
3425 		dev_err(dev, "dpbp_get_attributes() failed\n");
3426 		goto err_get_attr;
3427 	}
3428 
3429 	bp->dev = dpbp_dev;
3430 	bp->bpid = dpbp_attrs.bpid;
3431 
3432 	return bp;
3433 
3434 err_get_attr:
3435 	dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
3436 err_enable:
3437 err_reset:
3438 	dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
3439 err_open:
3440 	kfree(bp);
3441 err_alloc:
3442 	fsl_mc_object_free(dpbp_dev);
3443 
3444 	return ERR_PTR(err);
3445 }
3446 
dpaa2_eth_setup_default_dpbp(struct dpaa2_eth_priv * priv)3447 static int dpaa2_eth_setup_default_dpbp(struct dpaa2_eth_priv *priv)
3448 {
3449 	struct dpaa2_eth_bp *bp;
3450 	int i;
3451 
3452 	bp = dpaa2_eth_allocate_dpbp(priv);
3453 	if (IS_ERR(bp))
3454 		return PTR_ERR(bp);
3455 
3456 	priv->bp[DPAA2_ETH_DEFAULT_BP_IDX] = bp;
3457 	priv->num_bps++;
3458 
3459 	for (i = 0; i < priv->num_channels; i++)
3460 		priv->channel[i]->bp = bp;
3461 
3462 	return 0;
3463 }
3464 
dpaa2_eth_free_dpbp(struct dpaa2_eth_priv * priv,struct dpaa2_eth_bp * bp)3465 void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv, struct dpaa2_eth_bp *bp)
3466 {
3467 	int idx_bp;
3468 
3469 	/* Find the index at which this BP is stored */
3470 	for (idx_bp = 0; idx_bp < priv->num_bps; idx_bp++)
3471 		if (priv->bp[idx_bp] == bp)
3472 			break;
3473 
3474 	/* Drain the pool and disable the associated MC object */
3475 	dpaa2_eth_drain_pool(priv, bp->bpid);
3476 	dpbp_disable(priv->mc_io, 0, bp->dev->mc_handle);
3477 	dpbp_close(priv->mc_io, 0, bp->dev->mc_handle);
3478 	fsl_mc_object_free(bp->dev);
3479 	kfree(bp);
3480 
3481 	/* Move the last in use DPBP over in this position */
3482 	priv->bp[idx_bp] = priv->bp[priv->num_bps - 1];
3483 	priv->num_bps--;
3484 }
3485 
dpaa2_eth_free_dpbps(struct dpaa2_eth_priv * priv)3486 static void dpaa2_eth_free_dpbps(struct dpaa2_eth_priv *priv)
3487 {
3488 	int i;
3489 
3490 	for (i = 0; i < priv->num_bps; i++)
3491 		dpaa2_eth_free_dpbp(priv, priv->bp[i]);
3492 }
3493 
dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv * priv)3494 static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
3495 {
3496 	struct device *dev = priv->net_dev->dev.parent;
3497 	struct dpni_buffer_layout buf_layout = {0};
3498 	u16 rx_buf_align;
3499 	int err;
3500 
3501 	/* We need to check for WRIOP version 1.0.0, but depending on the MC
3502 	 * version, this number is not always provided correctly on rev1.
3503 	 * We need to check for both alternatives in this situation.
3504 	 */
3505 	if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
3506 	    priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
3507 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
3508 	else
3509 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
3510 
3511 	/* We need to ensure that the buffer size seen by WRIOP is a multiple
3512 	 * of 64 or 256 bytes depending on the WRIOP version.
3513 	 */
3514 	priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
3515 
3516 	/* tx buffer */
3517 	buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
3518 	buf_layout.pass_timestamp = true;
3519 	buf_layout.pass_frame_status = true;
3520 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
3521 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3522 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3523 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3524 				     DPNI_QUEUE_TX, &buf_layout);
3525 	if (err) {
3526 		dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
3527 		return err;
3528 	}
3529 
3530 	/* tx-confirm buffer */
3531 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3532 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3533 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3534 				     DPNI_QUEUE_TX_CONFIRM, &buf_layout);
3535 	if (err) {
3536 		dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
3537 		return err;
3538 	}
3539 
3540 	/* Now that we've set our tx buffer layout, retrieve the minimum
3541 	 * required tx data offset.
3542 	 */
3543 	err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
3544 				      &priv->tx_data_offset);
3545 	if (err) {
3546 		dev_err(dev, "dpni_get_tx_data_offset() failed\n");
3547 		return err;
3548 	}
3549 
3550 	if ((priv->tx_data_offset % 64) != 0)
3551 		dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
3552 			 priv->tx_data_offset);
3553 
3554 	/* rx buffer */
3555 	buf_layout.pass_frame_status = true;
3556 	buf_layout.pass_parser_result = true;
3557 	buf_layout.data_align = rx_buf_align;
3558 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
3559 	buf_layout.private_data_size = 0;
3560 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
3561 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
3562 			     DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
3563 			     DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
3564 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
3565 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3566 				     DPNI_QUEUE_RX, &buf_layout);
3567 	if (err) {
3568 		dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
3569 		return err;
3570 	}
3571 
3572 	return 0;
3573 }
3574 
3575 #define DPNI_ENQUEUE_FQID_VER_MAJOR	7
3576 #define DPNI_ENQUEUE_FQID_VER_MINOR	9
3577 
dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv * priv,struct dpaa2_eth_fq * fq,struct dpaa2_fd * fd,u8 prio,u32 num_frames __always_unused,int * frames_enqueued)3578 static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
3579 				       struct dpaa2_eth_fq *fq,
3580 				       struct dpaa2_fd *fd, u8 prio,
3581 				       u32 num_frames __always_unused,
3582 				       int *frames_enqueued)
3583 {
3584 	int err;
3585 
3586 	err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
3587 					  priv->tx_qdid, prio,
3588 					  fq->tx_qdbin, fd);
3589 	if (!err && frames_enqueued)
3590 		*frames_enqueued = 1;
3591 	return err;
3592 }
3593 
dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv * priv,struct dpaa2_eth_fq * fq,struct dpaa2_fd * fd,u8 prio,u32 num_frames,int * frames_enqueued)3594 static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
3595 						struct dpaa2_eth_fq *fq,
3596 						struct dpaa2_fd *fd,
3597 						u8 prio, u32 num_frames,
3598 						int *frames_enqueued)
3599 {
3600 	int err;
3601 
3602 	err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
3603 						   fq->tx_fqid[prio],
3604 						   fd, num_frames);
3605 
3606 	if (err == 0)
3607 		return -EBUSY;
3608 
3609 	if (frames_enqueued)
3610 		*frames_enqueued = err;
3611 	return 0;
3612 }
3613 
dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv * priv)3614 static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
3615 {
3616 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3617 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3618 		priv->enqueue = dpaa2_eth_enqueue_qd;
3619 	else
3620 		priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3621 }
3622 
dpaa2_eth_set_pause(struct dpaa2_eth_priv * priv)3623 static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
3624 {
3625 	struct device *dev = priv->net_dev->dev.parent;
3626 	struct dpni_link_cfg link_cfg = {0};
3627 	int err;
3628 
3629 	/* Get the default link options so we don't override other flags */
3630 	err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3631 	if (err) {
3632 		dev_err(dev, "dpni_get_link_cfg() failed\n");
3633 		return err;
3634 	}
3635 
3636 	/* By default, enable both Rx and Tx pause frames */
3637 	link_cfg.options |= DPNI_LINK_OPT_PAUSE;
3638 	link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
3639 	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3640 	if (err) {
3641 		dev_err(dev, "dpni_set_link_cfg() failed\n");
3642 		return err;
3643 	}
3644 
3645 	priv->link_state.options = link_cfg.options;
3646 
3647 	return 0;
3648 }
3649 
dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv * priv)3650 static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
3651 {
3652 	struct dpni_queue_id qid = {0};
3653 	struct dpaa2_eth_fq *fq;
3654 	struct dpni_queue queue;
3655 	int i, j, err;
3656 
3657 	/* We only use Tx FQIDs for FQID-based enqueue, so check
3658 	 * if DPNI version supports it before updating FQIDs
3659 	 */
3660 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3661 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3662 		return;
3663 
3664 	for (i = 0; i < priv->num_fqs; i++) {
3665 		fq = &priv->fq[i];
3666 		if (fq->type != DPAA2_TX_CONF_FQ)
3667 			continue;
3668 		for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3669 			err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3670 					     DPNI_QUEUE_TX, j, fq->flowid,
3671 					     &queue, &qid);
3672 			if (err)
3673 				goto out_err;
3674 
3675 			fq->tx_fqid[j] = qid.fqid;
3676 			if (fq->tx_fqid[j] == 0)
3677 				goto out_err;
3678 		}
3679 	}
3680 
3681 	priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3682 
3683 	return;
3684 
3685 out_err:
3686 	netdev_info(priv->net_dev,
3687 		    "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3688 	priv->enqueue = dpaa2_eth_enqueue_qd;
3689 }
3690 
3691 /* Configure ingress classification based on VLAN PCP */
dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv * priv)3692 static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
3693 {
3694 	struct device *dev = priv->net_dev->dev.parent;
3695 	struct dpkg_profile_cfg kg_cfg = {0};
3696 	struct dpni_qos_tbl_cfg qos_cfg = {0};
3697 	struct dpni_rule_cfg key_params;
3698 	void *dma_mem, *key, *mask;
3699 	u8 key_size = 2;	/* VLAN TCI field */
3700 	int i, pcp, err;
3701 
3702 	/* VLAN-based classification only makes sense if we have multiple
3703 	 * traffic classes.
3704 	 * Also, we need to extract just the 3-bit PCP field from the VLAN
3705 	 * header and we can only do that by using a mask
3706 	 */
3707 	if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3708 		dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3709 		return -EOPNOTSUPP;
3710 	}
3711 
3712 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3713 	if (!dma_mem)
3714 		return -ENOMEM;
3715 
3716 	kg_cfg.num_extracts = 1;
3717 	kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3718 	kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3719 	kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3720 	kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3721 
3722 	err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3723 	if (err) {
3724 		dev_err(dev, "dpni_prepare_key_cfg failed\n");
3725 		goto out_free_tbl;
3726 	}
3727 
3728 	/* set QoS table */
3729 	qos_cfg.default_tc = 0;
3730 	qos_cfg.discard_on_miss = 0;
3731 	qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3732 					      DPAA2_CLASSIFIER_DMA_SIZE,
3733 					      DMA_TO_DEVICE);
3734 	if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3735 		dev_err(dev, "QoS table DMA mapping failed\n");
3736 		err = -ENOMEM;
3737 		goto out_free_tbl;
3738 	}
3739 
3740 	err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3741 	if (err) {
3742 		dev_err(dev, "dpni_set_qos_table failed\n");
3743 		goto out_unmap_tbl;
3744 	}
3745 
3746 	/* Add QoS table entries */
3747 	key = kzalloc(key_size * 2, GFP_KERNEL);
3748 	if (!key) {
3749 		err = -ENOMEM;
3750 		goto out_unmap_tbl;
3751 	}
3752 	mask = key + key_size;
3753 	*(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3754 
3755 	key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3756 					     DMA_TO_DEVICE);
3757 	if (dma_mapping_error(dev, key_params.key_iova)) {
3758 		dev_err(dev, "Qos table entry DMA mapping failed\n");
3759 		err = -ENOMEM;
3760 		goto out_free_key;
3761 	}
3762 
3763 	key_params.mask_iova = key_params.key_iova + key_size;
3764 	key_params.key_size = key_size;
3765 
3766 	/* We add rules for PCP-based distribution starting with highest
3767 	 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3768 	 * classes to accommodate all priority levels, the lowest ones end up
3769 	 * on TC 0 which was configured as default
3770 	 */
3771 	for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3772 		*(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3773 		dma_sync_single_for_device(dev, key_params.key_iova,
3774 					   key_size * 2, DMA_TO_DEVICE);
3775 
3776 		err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3777 					 &key_params, i, i);
3778 		if (err) {
3779 			dev_err(dev, "dpni_add_qos_entry failed\n");
3780 			dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3781 			goto out_unmap_key;
3782 		}
3783 	}
3784 
3785 	priv->vlan_cls_enabled = true;
3786 
3787 	/* Table and key memory is not persistent, clean everything up after
3788 	 * configuration is finished
3789 	 */
3790 out_unmap_key:
3791 	dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3792 out_free_key:
3793 	kfree(key);
3794 out_unmap_tbl:
3795 	dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3796 			 DMA_TO_DEVICE);
3797 out_free_tbl:
3798 	kfree(dma_mem);
3799 
3800 	return err;
3801 }
3802 
3803 /* Configure the DPNI object this interface is associated with */
dpaa2_eth_setup_dpni(struct fsl_mc_device * ls_dev)3804 static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
3805 {
3806 	struct device *dev = &ls_dev->dev;
3807 	struct dpaa2_eth_priv *priv;
3808 	struct net_device *net_dev;
3809 	int err;
3810 
3811 	net_dev = dev_get_drvdata(dev);
3812 	priv = netdev_priv(net_dev);
3813 
3814 	/* get a handle for the DPNI object */
3815 	err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
3816 	if (err) {
3817 		dev_err(dev, "dpni_open() failed\n");
3818 		return err;
3819 	}
3820 
3821 	/* Check if we can work with this DPNI object */
3822 	err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3823 				   &priv->dpni_ver_minor);
3824 	if (err) {
3825 		dev_err(dev, "dpni_get_api_version() failed\n");
3826 		goto close;
3827 	}
3828 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3829 		dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3830 			priv->dpni_ver_major, priv->dpni_ver_minor,
3831 			DPNI_VER_MAJOR, DPNI_VER_MINOR);
3832 		err = -EOPNOTSUPP;
3833 		goto close;
3834 	}
3835 
3836 	ls_dev->mc_io = priv->mc_io;
3837 	ls_dev->mc_handle = priv->mc_token;
3838 
3839 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3840 	if (err) {
3841 		dev_err(dev, "dpni_reset() failed\n");
3842 		goto close;
3843 	}
3844 
3845 	err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3846 				  &priv->dpni_attrs);
3847 	if (err) {
3848 		dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
3849 		goto close;
3850 	}
3851 
3852 	err = dpaa2_eth_set_buffer_layout(priv);
3853 	if (err)
3854 		goto close;
3855 
3856 	dpaa2_eth_set_enqueue_mode(priv);
3857 
3858 	/* Enable pause frame support */
3859 	if (dpaa2_eth_has_pause_support(priv)) {
3860 		err = dpaa2_eth_set_pause(priv);
3861 		if (err)
3862 			goto close;
3863 	}
3864 
3865 	err = dpaa2_eth_set_vlan_qos(priv);
3866 	if (err && err != -EOPNOTSUPP)
3867 		goto close;
3868 
3869 	priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3870 				       sizeof(struct dpaa2_eth_cls_rule),
3871 				       GFP_KERNEL);
3872 	if (!priv->cls_rules) {
3873 		err = -ENOMEM;
3874 		goto close;
3875 	}
3876 
3877 	return 0;
3878 
3879 close:
3880 	dpni_close(priv->mc_io, 0, priv->mc_token);
3881 
3882 	return err;
3883 }
3884 
dpaa2_eth_free_dpni(struct dpaa2_eth_priv * priv)3885 static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
3886 {
3887 	int err;
3888 
3889 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3890 	if (err)
3891 		netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3892 			    err);
3893 
3894 	dpni_close(priv->mc_io, 0, priv->mc_token);
3895 }
3896 
dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv * priv,struct dpaa2_eth_fq * fq)3897 static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3898 				   struct dpaa2_eth_fq *fq)
3899 {
3900 	struct device *dev = priv->net_dev->dev.parent;
3901 	struct dpni_queue queue;
3902 	struct dpni_queue_id qid;
3903 	int err;
3904 
3905 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3906 			     DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
3907 	if (err) {
3908 		dev_err(dev, "dpni_get_queue(RX) failed\n");
3909 		return err;
3910 	}
3911 
3912 	fq->fqid = qid.fqid;
3913 
3914 	queue.destination.id = fq->channel->dpcon_id;
3915 	queue.destination.type = DPNI_DEST_DPCON;
3916 	queue.destination.priority = 1;
3917 	queue.user_context = (u64)(uintptr_t)fq;
3918 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3919 			     DPNI_QUEUE_RX, fq->tc, fq->flowid,
3920 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3921 			     &queue);
3922 	if (err) {
3923 		dev_err(dev, "dpni_set_queue(RX) failed\n");
3924 		return err;
3925 	}
3926 
3927 	/* xdp_rxq setup */
3928 	/* only once for each channel */
3929 	if (fq->tc > 0)
3930 		return 0;
3931 
3932 	err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3933 			       fq->flowid, 0);
3934 	if (err) {
3935 		dev_err(dev, "xdp_rxq_info_reg failed\n");
3936 		return err;
3937 	}
3938 
3939 	err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3940 					 MEM_TYPE_PAGE_ORDER0, NULL);
3941 	if (err) {
3942 		dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3943 		xdp_rxq_info_unreg(&fq->channel->xdp_rxq);
3944 		return err;
3945 	}
3946 
3947 	return 0;
3948 }
3949 
dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv * priv,struct dpaa2_eth_fq * fq)3950 static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3951 				   struct dpaa2_eth_fq *fq)
3952 {
3953 	struct device *dev = priv->net_dev->dev.parent;
3954 	struct dpni_queue queue;
3955 	struct dpni_queue_id qid;
3956 	int i, err;
3957 
3958 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3959 		err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3960 				     DPNI_QUEUE_TX, i, fq->flowid,
3961 				     &queue, &qid);
3962 		if (err) {
3963 			dev_err(dev, "dpni_get_queue(TX) failed\n");
3964 			return err;
3965 		}
3966 		fq->tx_fqid[i] = qid.fqid;
3967 	}
3968 
3969 	/* All Tx queues belonging to the same flowid have the same qdbin */
3970 	fq->tx_qdbin = qid.qdbin;
3971 
3972 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3973 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3974 			     &queue, &qid);
3975 	if (err) {
3976 		dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3977 		return err;
3978 	}
3979 
3980 	fq->fqid = qid.fqid;
3981 
3982 	queue.destination.id = fq->channel->dpcon_id;
3983 	queue.destination.type = DPNI_DEST_DPCON;
3984 	queue.destination.priority = 0;
3985 	queue.user_context = (u64)(uintptr_t)fq;
3986 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3987 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3988 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3989 			     &queue);
3990 	if (err) {
3991 		dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3992 		return err;
3993 	}
3994 
3995 	return 0;
3996 }
3997 
setup_rx_err_flow(struct dpaa2_eth_priv * priv,struct dpaa2_eth_fq * fq)3998 static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
3999 			     struct dpaa2_eth_fq *fq)
4000 {
4001 	struct device *dev = priv->net_dev->dev.parent;
4002 	struct dpni_queue q = { { 0 } };
4003 	struct dpni_queue_id qid;
4004 	u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
4005 	int err;
4006 
4007 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
4008 			     DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
4009 	if (err) {
4010 		dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
4011 		return err;
4012 	}
4013 
4014 	fq->fqid = qid.fqid;
4015 
4016 	q.destination.id = fq->channel->dpcon_id;
4017 	q.destination.type = DPNI_DEST_DPCON;
4018 	q.destination.priority = 1;
4019 	q.user_context = (u64)(uintptr_t)fq;
4020 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
4021 			     DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
4022 	if (err) {
4023 		dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
4024 		return err;
4025 	}
4026 
4027 	return 0;
4028 }
4029 
4030 /* Supported header fields for Rx hash distribution key */
4031 static const struct dpaa2_eth_dist_fields dist_fields[] = {
4032 	{
4033 		/* L2 header */
4034 		.rxnfc_field = RXH_L2DA,
4035 		.cls_prot = NET_PROT_ETH,
4036 		.cls_field = NH_FLD_ETH_DA,
4037 		.id = DPAA2_ETH_DIST_ETHDST,
4038 		.size = 6,
4039 	}, {
4040 		.cls_prot = NET_PROT_ETH,
4041 		.cls_field = NH_FLD_ETH_SA,
4042 		.id = DPAA2_ETH_DIST_ETHSRC,
4043 		.size = 6,
4044 	}, {
4045 		/* This is the last ethertype field parsed:
4046 		 * depending on frame format, it can be the MAC ethertype
4047 		 * or the VLAN etype.
4048 		 */
4049 		.cls_prot = NET_PROT_ETH,
4050 		.cls_field = NH_FLD_ETH_TYPE,
4051 		.id = DPAA2_ETH_DIST_ETHTYPE,
4052 		.size = 2,
4053 	}, {
4054 		/* VLAN header */
4055 		.rxnfc_field = RXH_VLAN,
4056 		.cls_prot = NET_PROT_VLAN,
4057 		.cls_field = NH_FLD_VLAN_TCI,
4058 		.id = DPAA2_ETH_DIST_VLAN,
4059 		.size = 2,
4060 	}, {
4061 		/* IP header */
4062 		.rxnfc_field = RXH_IP_SRC,
4063 		.cls_prot = NET_PROT_IP,
4064 		.cls_field = NH_FLD_IP_SRC,
4065 		.id = DPAA2_ETH_DIST_IPSRC,
4066 		.size = 4,
4067 	}, {
4068 		.rxnfc_field = RXH_IP_DST,
4069 		.cls_prot = NET_PROT_IP,
4070 		.cls_field = NH_FLD_IP_DST,
4071 		.id = DPAA2_ETH_DIST_IPDST,
4072 		.size = 4,
4073 	}, {
4074 		.rxnfc_field = RXH_L3_PROTO,
4075 		.cls_prot = NET_PROT_IP,
4076 		.cls_field = NH_FLD_IP_PROTO,
4077 		.id = DPAA2_ETH_DIST_IPPROTO,
4078 		.size = 1,
4079 	}, {
4080 		/* Using UDP ports, this is functionally equivalent to raw
4081 		 * byte pairs from L4 header.
4082 		 */
4083 		.rxnfc_field = RXH_L4_B_0_1,
4084 		.cls_prot = NET_PROT_UDP,
4085 		.cls_field = NH_FLD_UDP_PORT_SRC,
4086 		.id = DPAA2_ETH_DIST_L4SRC,
4087 		.size = 2,
4088 	}, {
4089 		.rxnfc_field = RXH_L4_B_2_3,
4090 		.cls_prot = NET_PROT_UDP,
4091 		.cls_field = NH_FLD_UDP_PORT_DST,
4092 		.id = DPAA2_ETH_DIST_L4DST,
4093 		.size = 2,
4094 	},
4095 };
4096 
4097 /* Configure the Rx hash key using the legacy API */
dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv * priv,dma_addr_t key)4098 static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
4099 {
4100 	struct device *dev = priv->net_dev->dev.parent;
4101 	struct dpni_rx_tc_dist_cfg dist_cfg;
4102 	int i, err = 0;
4103 
4104 	memset(&dist_cfg, 0, sizeof(dist_cfg));
4105 
4106 	dist_cfg.key_cfg_iova = key;
4107 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
4108 	dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
4109 
4110 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
4111 		err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
4112 					  i, &dist_cfg);
4113 		if (err) {
4114 			dev_err(dev, "dpni_set_rx_tc_dist failed\n");
4115 			break;
4116 		}
4117 	}
4118 
4119 	return err;
4120 }
4121 
4122 /* Configure the Rx hash key using the new API */
dpaa2_eth_config_hash_key(struct dpaa2_eth_priv * priv,dma_addr_t key)4123 static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
4124 {
4125 	struct device *dev = priv->net_dev->dev.parent;
4126 	struct dpni_rx_dist_cfg dist_cfg;
4127 	int i, err = 0;
4128 
4129 	memset(&dist_cfg, 0, sizeof(dist_cfg));
4130 
4131 	dist_cfg.key_cfg_iova = key;
4132 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
4133 	dist_cfg.enable = 1;
4134 
4135 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
4136 		dist_cfg.tc = i;
4137 		err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
4138 					    &dist_cfg);
4139 		if (err) {
4140 			dev_err(dev, "dpni_set_rx_hash_dist failed\n");
4141 			break;
4142 		}
4143 
4144 		/* If the flow steering / hashing key is shared between all
4145 		 * traffic classes, install it just once
4146 		 */
4147 		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
4148 			break;
4149 	}
4150 
4151 	return err;
4152 }
4153 
4154 /* Configure the Rx flow classification key */
dpaa2_eth_config_cls_key(struct dpaa2_eth_priv * priv,dma_addr_t key)4155 static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
4156 {
4157 	struct device *dev = priv->net_dev->dev.parent;
4158 	struct dpni_rx_dist_cfg dist_cfg;
4159 	int i, err = 0;
4160 
4161 	memset(&dist_cfg, 0, sizeof(dist_cfg));
4162 
4163 	dist_cfg.key_cfg_iova = key;
4164 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
4165 	dist_cfg.enable = 1;
4166 
4167 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
4168 		dist_cfg.tc = i;
4169 		err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
4170 					  &dist_cfg);
4171 		if (err) {
4172 			dev_err(dev, "dpni_set_rx_fs_dist failed\n");
4173 			break;
4174 		}
4175 
4176 		/* If the flow steering / hashing key is shared between all
4177 		 * traffic classes, install it just once
4178 		 */
4179 		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
4180 			break;
4181 	}
4182 
4183 	return err;
4184 }
4185 
4186 /* Size of the Rx flow classification key */
dpaa2_eth_cls_key_size(u64 fields)4187 int dpaa2_eth_cls_key_size(u64 fields)
4188 {
4189 	int i, size = 0;
4190 
4191 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4192 		if (!(fields & dist_fields[i].id))
4193 			continue;
4194 		size += dist_fields[i].size;
4195 	}
4196 
4197 	return size;
4198 }
4199 
4200 /* Offset of header field in Rx classification key */
dpaa2_eth_cls_fld_off(int prot,int field)4201 int dpaa2_eth_cls_fld_off(int prot, int field)
4202 {
4203 	int i, off = 0;
4204 
4205 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4206 		if (dist_fields[i].cls_prot == prot &&
4207 		    dist_fields[i].cls_field == field)
4208 			return off;
4209 		off += dist_fields[i].size;
4210 	}
4211 
4212 	WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
4213 	return 0;
4214 }
4215 
4216 /* Prune unused fields from the classification rule.
4217  * Used when masking is not supported
4218  */
dpaa2_eth_cls_trim_rule(void * key_mem,u64 fields)4219 void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
4220 {
4221 	int off = 0, new_off = 0;
4222 	int i, size;
4223 
4224 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4225 		size = dist_fields[i].size;
4226 		if (dist_fields[i].id & fields) {
4227 			memcpy(key_mem + new_off, key_mem + off, size);
4228 			new_off += size;
4229 		}
4230 		off += size;
4231 	}
4232 }
4233 
4234 /* Set Rx distribution (hash or flow classification) key
4235  * flags is a combination of RXH_ bits
4236  */
dpaa2_eth_set_dist_key(struct net_device * net_dev,enum dpaa2_eth_rx_dist type,u64 flags)4237 static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
4238 				  enum dpaa2_eth_rx_dist type, u64 flags)
4239 {
4240 	struct device *dev = net_dev->dev.parent;
4241 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4242 	struct dpkg_profile_cfg cls_cfg;
4243 	u32 rx_hash_fields = 0;
4244 	dma_addr_t key_iova;
4245 	u8 *dma_mem;
4246 	int i;
4247 	int err = 0;
4248 
4249 	memset(&cls_cfg, 0, sizeof(cls_cfg));
4250 
4251 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4252 		struct dpkg_extract *key =
4253 			&cls_cfg.extracts[cls_cfg.num_extracts];
4254 
4255 		/* For both Rx hashing and classification keys
4256 		 * we set only the selected fields.
4257 		 */
4258 		if (!(flags & dist_fields[i].id))
4259 			continue;
4260 		if (type == DPAA2_ETH_RX_DIST_HASH)
4261 			rx_hash_fields |= dist_fields[i].rxnfc_field;
4262 
4263 		if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
4264 			dev_err(dev, "error adding key extraction rule, too many rules?\n");
4265 			return -E2BIG;
4266 		}
4267 
4268 		key->type = DPKG_EXTRACT_FROM_HDR;
4269 		key->extract.from_hdr.prot = dist_fields[i].cls_prot;
4270 		key->extract.from_hdr.type = DPKG_FULL_FIELD;
4271 		key->extract.from_hdr.field = dist_fields[i].cls_field;
4272 		cls_cfg.num_extracts++;
4273 	}
4274 
4275 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
4276 	if (!dma_mem)
4277 		return -ENOMEM;
4278 
4279 	err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
4280 	if (err) {
4281 		dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
4282 		goto free_key;
4283 	}
4284 
4285 	/* Prepare for setting the rx dist */
4286 	key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
4287 				  DMA_TO_DEVICE);
4288 	if (dma_mapping_error(dev, key_iova)) {
4289 		dev_err(dev, "DMA mapping failed\n");
4290 		err = -ENOMEM;
4291 		goto free_key;
4292 	}
4293 
4294 	if (type == DPAA2_ETH_RX_DIST_HASH) {
4295 		if (dpaa2_eth_has_legacy_dist(priv))
4296 			err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
4297 		else
4298 			err = dpaa2_eth_config_hash_key(priv, key_iova);
4299 	} else {
4300 		err = dpaa2_eth_config_cls_key(priv, key_iova);
4301 	}
4302 
4303 	dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
4304 			 DMA_TO_DEVICE);
4305 	if (!err && type == DPAA2_ETH_RX_DIST_HASH)
4306 		priv->rx_hash_fields = rx_hash_fields;
4307 
4308 free_key:
4309 	kfree(dma_mem);
4310 	return err;
4311 }
4312 
dpaa2_eth_set_hash(struct net_device * net_dev,u64 flags)4313 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
4314 {
4315 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4316 	u64 key = 0;
4317 	int i;
4318 
4319 	if (!dpaa2_eth_hash_enabled(priv))
4320 		return -EOPNOTSUPP;
4321 
4322 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
4323 		if (dist_fields[i].rxnfc_field & flags)
4324 			key |= dist_fields[i].id;
4325 
4326 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
4327 }
4328 
dpaa2_eth_set_cls(struct net_device * net_dev,u64 flags)4329 int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
4330 {
4331 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
4332 }
4333 
dpaa2_eth_set_default_cls(struct dpaa2_eth_priv * priv)4334 static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
4335 {
4336 	struct device *dev = priv->net_dev->dev.parent;
4337 	int err;
4338 
4339 	/* Check if we actually support Rx flow classification */
4340 	if (dpaa2_eth_has_legacy_dist(priv)) {
4341 		dev_dbg(dev, "Rx cls not supported by current MC version\n");
4342 		return -EOPNOTSUPP;
4343 	}
4344 
4345 	if (!dpaa2_eth_fs_enabled(priv)) {
4346 		dev_dbg(dev, "Rx cls disabled in DPNI options\n");
4347 		return -EOPNOTSUPP;
4348 	}
4349 
4350 	if (!dpaa2_eth_hash_enabled(priv)) {
4351 		dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
4352 		return -EOPNOTSUPP;
4353 	}
4354 
4355 	/* If there is no support for masking in the classification table,
4356 	 * we don't set a default key, as it will depend on the rules
4357 	 * added by the user at runtime.
4358 	 */
4359 	if (!dpaa2_eth_fs_mask_enabled(priv))
4360 		goto out;
4361 
4362 	err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
4363 	if (err)
4364 		return err;
4365 
4366 out:
4367 	priv->rx_cls_enabled = 1;
4368 
4369 	return 0;
4370 }
4371 
4372 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
4373  * frame queues and channels
4374  */
dpaa2_eth_bind_dpni(struct dpaa2_eth_priv * priv)4375 static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
4376 {
4377 	struct dpaa2_eth_bp *bp = priv->bp[DPAA2_ETH_DEFAULT_BP_IDX];
4378 	struct net_device *net_dev = priv->net_dev;
4379 	struct dpni_pools_cfg pools_params = { 0 };
4380 	struct device *dev = net_dev->dev.parent;
4381 	struct dpni_error_cfg err_cfg;
4382 	int err = 0;
4383 	int i;
4384 
4385 	pools_params.num_dpbp = 1;
4386 	pools_params.pools[0].dpbp_id = bp->dev->obj_desc.id;
4387 	pools_params.pools[0].backup_pool = 0;
4388 	pools_params.pools[0].buffer_size = priv->rx_buf_size;
4389 	err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
4390 	if (err) {
4391 		dev_err(dev, "dpni_set_pools() failed\n");
4392 		return err;
4393 	}
4394 
4395 	/* have the interface implicitly distribute traffic based on
4396 	 * the default hash key
4397 	 */
4398 	err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
4399 	if (err && err != -EOPNOTSUPP)
4400 		dev_err(dev, "Failed to configure hashing\n");
4401 
4402 	/* Configure the flow classification key; it includes all
4403 	 * supported header fields and cannot be modified at runtime
4404 	 */
4405 	err = dpaa2_eth_set_default_cls(priv);
4406 	if (err && err != -EOPNOTSUPP)
4407 		dev_err(dev, "Failed to configure Rx classification key\n");
4408 
4409 	/* Configure handling of error frames */
4410 	err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
4411 	err_cfg.set_frame_annotation = 1;
4412 	err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
4413 	err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
4414 				       &err_cfg);
4415 	if (err) {
4416 		dev_err(dev, "dpni_set_errors_behavior failed\n");
4417 		return err;
4418 	}
4419 
4420 	/* Configure Rx and Tx conf queues to generate CDANs */
4421 	for (i = 0; i < priv->num_fqs; i++) {
4422 		switch (priv->fq[i].type) {
4423 		case DPAA2_RX_FQ:
4424 			err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
4425 			break;
4426 		case DPAA2_TX_CONF_FQ:
4427 			err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
4428 			break;
4429 		case DPAA2_RX_ERR_FQ:
4430 			err = setup_rx_err_flow(priv, &priv->fq[i]);
4431 			break;
4432 		default:
4433 			dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
4434 			return -EINVAL;
4435 		}
4436 		if (err)
4437 			goto out;
4438 	}
4439 
4440 	err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
4441 			    DPNI_QUEUE_TX, &priv->tx_qdid);
4442 	if (err) {
4443 		dev_err(dev, "dpni_get_qdid() failed\n");
4444 		goto out;
4445 	}
4446 
4447 	return 0;
4448 
4449 out:
4450 	while (i--) {
4451 		if (priv->fq[i].type == DPAA2_RX_FQ &&
4452 		    xdp_rxq_info_is_reg(&priv->fq[i].channel->xdp_rxq))
4453 			xdp_rxq_info_unreg(&priv->fq[i].channel->xdp_rxq);
4454 	}
4455 	return err;
4456 }
4457 
4458 /* Allocate rings for storing incoming frame descriptors */
dpaa2_eth_alloc_rings(struct dpaa2_eth_priv * priv)4459 static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
4460 {
4461 	struct net_device *net_dev = priv->net_dev;
4462 	struct device *dev = net_dev->dev.parent;
4463 	int i;
4464 
4465 	for (i = 0; i < priv->num_channels; i++) {
4466 		priv->channel[i]->store =
4467 			dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
4468 		if (!priv->channel[i]->store) {
4469 			netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
4470 			goto err_ring;
4471 		}
4472 	}
4473 
4474 	return 0;
4475 
4476 err_ring:
4477 	for (i = 0; i < priv->num_channels; i++) {
4478 		if (!priv->channel[i]->store)
4479 			break;
4480 		dpaa2_io_store_destroy(priv->channel[i]->store);
4481 	}
4482 
4483 	return -ENOMEM;
4484 }
4485 
dpaa2_eth_free_rings(struct dpaa2_eth_priv * priv)4486 static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
4487 {
4488 	int i;
4489 
4490 	for (i = 0; i < priv->num_channels; i++)
4491 		dpaa2_io_store_destroy(priv->channel[i]->store);
4492 }
4493 
dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv * priv)4494 static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
4495 {
4496 	struct net_device *net_dev = priv->net_dev;
4497 	struct device *dev = net_dev->dev.parent;
4498 	u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
4499 	int err;
4500 
4501 	/* Get firmware address, if any */
4502 	err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
4503 	if (err) {
4504 		dev_err(dev, "dpni_get_port_mac_addr() failed\n");
4505 		return err;
4506 	}
4507 
4508 	/* Get DPNI attributes address, if any */
4509 	err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4510 					dpni_mac_addr);
4511 	if (err) {
4512 		dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
4513 		return err;
4514 	}
4515 
4516 	/* First check if firmware has any address configured by bootloader */
4517 	if (!is_zero_ether_addr(mac_addr)) {
4518 		/* If the DPMAC addr != DPNI addr, update it */
4519 		if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
4520 			err = dpni_set_primary_mac_addr(priv->mc_io, 0,
4521 							priv->mc_token,
4522 							mac_addr);
4523 			if (err) {
4524 				dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4525 				return err;
4526 			}
4527 		}
4528 		eth_hw_addr_set(net_dev, mac_addr);
4529 	} else if (is_zero_ether_addr(dpni_mac_addr)) {
4530 		/* No MAC address configured, fill in net_dev->dev_addr
4531 		 * with a random one
4532 		 */
4533 		eth_hw_addr_random(net_dev);
4534 		dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
4535 
4536 		err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4537 						net_dev->dev_addr);
4538 		if (err) {
4539 			dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4540 			return err;
4541 		}
4542 
4543 		/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
4544 		 * practical purposes, this will be our "permanent" mac address,
4545 		 * at least until the next reboot. This move will also permit
4546 		 * register_netdevice() to properly fill up net_dev->perm_addr.
4547 		 */
4548 		net_dev->addr_assign_type = NET_ADDR_PERM;
4549 	} else {
4550 		/* NET_ADDR_PERM is default, all we have to do is
4551 		 * fill in the device addr.
4552 		 */
4553 		eth_hw_addr_set(net_dev, dpni_mac_addr);
4554 	}
4555 
4556 	return 0;
4557 }
4558 
dpaa2_eth_netdev_init(struct net_device * net_dev)4559 static int dpaa2_eth_netdev_init(struct net_device *net_dev)
4560 {
4561 	struct device *dev = net_dev->dev.parent;
4562 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4563 	u32 options = priv->dpni_attrs.options;
4564 	u64 supported = 0, not_supported = 0;
4565 	u8 bcast_addr[ETH_ALEN];
4566 	u8 num_queues;
4567 	int err;
4568 
4569 	net_dev->netdev_ops = &dpaa2_eth_ops;
4570 	net_dev->ethtool_ops = &dpaa2_ethtool_ops;
4571 
4572 	err = dpaa2_eth_set_mac_addr(priv);
4573 	if (err)
4574 		return err;
4575 
4576 	/* Explicitly add the broadcast address to the MAC filtering table */
4577 	eth_broadcast_addr(bcast_addr);
4578 	err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
4579 	if (err) {
4580 		dev_err(dev, "dpni_add_mac_addr() failed\n");
4581 		return err;
4582 	}
4583 
4584 	/* Set MTU upper limit; lower limit is 68B (default value) */
4585 	net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
4586 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
4587 					DPAA2_ETH_MFL);
4588 	if (err) {
4589 		dev_err(dev, "dpni_set_max_frame_length() failed\n");
4590 		return err;
4591 	}
4592 
4593 	/* Set actual number of queues in the net device */
4594 	num_queues = dpaa2_eth_queue_count(priv);
4595 	err = netif_set_real_num_tx_queues(net_dev, num_queues);
4596 	if (err) {
4597 		dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
4598 		return err;
4599 	}
4600 	err = netif_set_real_num_rx_queues(net_dev, num_queues);
4601 	if (err) {
4602 		dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
4603 		return err;
4604 	}
4605 
4606 	dpaa2_eth_detect_features(priv);
4607 
4608 	/* Capabilities listing */
4609 	supported |= IFF_LIVE_ADDR_CHANGE;
4610 
4611 	if (options & DPNI_OPT_NO_MAC_FILTER)
4612 		not_supported |= IFF_UNICAST_FLT;
4613 	else
4614 		supported |= IFF_UNICAST_FLT;
4615 
4616 	net_dev->priv_flags |= supported;
4617 	net_dev->priv_flags &= ~not_supported;
4618 	net_dev->lltx = true;
4619 
4620 	/* Features */
4621 	net_dev->features = NETIF_F_RXCSUM |
4622 			    NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4623 			    NETIF_F_SG | NETIF_F_HIGHDMA |
4624 			    NETIF_F_HW_TC | NETIF_F_TSO;
4625 	net_dev->gso_max_segs = DPAA2_ETH_ENQUEUE_MAX_FDS;
4626 	net_dev->hw_features = net_dev->features;
4627 	net_dev->xdp_features = NETDEV_XDP_ACT_BASIC |
4628 				NETDEV_XDP_ACT_REDIRECT |
4629 				NETDEV_XDP_ACT_NDO_XMIT;
4630 	if (priv->dpni_attrs.wriop_version >= DPAA2_WRIOP_VERSION(3, 0, 0) &&
4631 	    priv->dpni_attrs.num_queues <= 8)
4632 		net_dev->xdp_features |= NETDEV_XDP_ACT_XSK_ZEROCOPY;
4633 
4634 	if (priv->dpni_attrs.vlan_filter_entries)
4635 		net_dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4636 
4637 	return 0;
4638 }
4639 
dpaa2_eth_poll_link_state(void * arg)4640 static int dpaa2_eth_poll_link_state(void *arg)
4641 {
4642 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
4643 	int err;
4644 
4645 	while (!kthread_should_stop()) {
4646 		err = dpaa2_eth_link_state_update(priv);
4647 		if (unlikely(err))
4648 			return err;
4649 
4650 		msleep(DPAA2_ETH_LINK_STATE_REFRESH);
4651 	}
4652 
4653 	return 0;
4654 }
4655 
dpaa2_eth_connect_mac(struct dpaa2_eth_priv * priv)4656 static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
4657 {
4658 	struct fsl_mc_device *dpni_dev, *dpmac_dev;
4659 	struct dpaa2_mac *mac;
4660 	int err;
4661 
4662 	dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
4663 	dpmac_dev = fsl_mc_get_endpoint(dpni_dev, 0);
4664 
4665 	if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER) {
4666 		netdev_dbg(priv->net_dev, "waiting for mac\n");
4667 		return PTR_ERR(dpmac_dev);
4668 	}
4669 
4670 	if (IS_ERR(dpmac_dev))
4671 		return 0;
4672 
4673 	if (dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type) {
4674 		err = 0;
4675 		goto out_put_device;
4676 	}
4677 
4678 	mac = kzalloc_obj(struct dpaa2_mac);
4679 	if (!mac) {
4680 		err = -ENOMEM;
4681 		goto out_put_device;
4682 	}
4683 
4684 	mac->mc_dev = dpmac_dev;
4685 	mac->mc_io = priv->mc_io;
4686 	mac->net_dev = priv->net_dev;
4687 
4688 	err = dpaa2_mac_open(mac);
4689 	if (err)
4690 		goto err_free_mac;
4691 
4692 	if (dpaa2_mac_is_type_phy(mac)) {
4693 		err = dpaa2_mac_connect(mac);
4694 		if (err) {
4695 			if (err == -EPROBE_DEFER)
4696 				netdev_dbg(priv->net_dev,
4697 					   "could not connect to MAC\n");
4698 			else
4699 				netdev_err(priv->net_dev,
4700 					   "Error connecting to the MAC endpoint: %pe",
4701 					   ERR_PTR(err));
4702 			goto err_close_mac;
4703 		}
4704 	}
4705 
4706 	mutex_lock(&priv->mac_lock);
4707 	priv->mac = mac;
4708 	mutex_unlock(&priv->mac_lock);
4709 
4710 	return 0;
4711 
4712 err_close_mac:
4713 	dpaa2_mac_close(mac);
4714 err_free_mac:
4715 	kfree(mac);
4716 out_put_device:
4717 	put_device(&dpmac_dev->dev);
4718 	return err;
4719 }
4720 
dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv * priv)4721 static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
4722 {
4723 	struct dpaa2_mac *mac;
4724 
4725 	mutex_lock(&priv->mac_lock);
4726 	mac = priv->mac;
4727 	priv->mac = NULL;
4728 	mutex_unlock(&priv->mac_lock);
4729 
4730 	if (!mac)
4731 		return;
4732 
4733 	if (dpaa2_mac_is_type_phy(mac))
4734 		dpaa2_mac_disconnect(mac);
4735 
4736 	dpaa2_mac_close(mac);
4737 	put_device(&mac->mc_dev->dev);
4738 	kfree(mac);
4739 }
4740 
dpni_irq0_handler_thread(int irq_num,void * arg)4741 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
4742 {
4743 	u32 status = ~0;
4744 	struct device *dev = (struct device *)arg;
4745 	struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
4746 	struct net_device *net_dev = dev_get_drvdata(dev);
4747 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4748 	bool had_mac;
4749 	int err;
4750 
4751 	err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
4752 				  DPNI_IRQ_INDEX, &status);
4753 	if (unlikely(err)) {
4754 		netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
4755 		return IRQ_HANDLED;
4756 	}
4757 
4758 	if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
4759 		dpaa2_eth_link_state_update(netdev_priv(net_dev));
4760 
4761 	if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
4762 		dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
4763 		dpaa2_eth_update_tx_fqids(priv);
4764 
4765 		/* We can avoid locking because the "endpoint changed" IRQ
4766 		 * handler is the only one who changes priv->mac at runtime,
4767 		 * so we are not racing with anyone.
4768 		 */
4769 		had_mac = !!priv->mac;
4770 		if (had_mac)
4771 			dpaa2_eth_disconnect_mac(priv);
4772 		else
4773 			dpaa2_eth_connect_mac(priv);
4774 	}
4775 
4776 	return IRQ_HANDLED;
4777 }
4778 
dpaa2_eth_setup_irqs(struct fsl_mc_device * ls_dev)4779 static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
4780 {
4781 	int err = 0;
4782 	struct fsl_mc_device_irq *irq;
4783 
4784 	err = fsl_mc_allocate_irqs(ls_dev);
4785 	if (err) {
4786 		dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4787 		return err;
4788 	}
4789 
4790 	irq = ls_dev->irqs[0];
4791 	err = devm_request_threaded_irq(&ls_dev->dev, irq->virq,
4792 					NULL, dpni_irq0_handler_thread,
4793 					IRQF_NO_SUSPEND | IRQF_ONESHOT,
4794 					dev_name(&ls_dev->dev), &ls_dev->dev);
4795 	if (err < 0) {
4796 		dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
4797 		goto free_mc_irq;
4798 	}
4799 
4800 	err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
4801 				DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4802 				DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
4803 	if (err < 0) {
4804 		dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
4805 		goto free_irq;
4806 	}
4807 
4808 	err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4809 				  DPNI_IRQ_INDEX, 1);
4810 	if (err < 0) {
4811 		dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
4812 		goto free_irq;
4813 	}
4814 
4815 	return 0;
4816 
4817 free_irq:
4818 	devm_free_irq(&ls_dev->dev, irq->virq, &ls_dev->dev);
4819 free_mc_irq:
4820 	fsl_mc_free_irqs(ls_dev);
4821 
4822 	return err;
4823 }
4824 
dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv * priv)4825 static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
4826 {
4827 	int i;
4828 	struct dpaa2_eth_channel *ch;
4829 
4830 	for (i = 0; i < priv->num_channels; i++) {
4831 		ch = priv->channel[i];
4832 		/* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4833 		netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll);
4834 	}
4835 }
4836 
dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv * priv)4837 static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
4838 {
4839 	int i;
4840 	struct dpaa2_eth_channel *ch;
4841 
4842 	for (i = 0; i < priv->num_channels; i++) {
4843 		ch = priv->channel[i];
4844 		netif_napi_del(&ch->napi);
4845 	}
4846 }
4847 
dpaa2_eth_free_rx_xdp_rxq(struct dpaa2_eth_priv * priv)4848 static void dpaa2_eth_free_rx_xdp_rxq(struct dpaa2_eth_priv *priv)
4849 {
4850 	int i;
4851 
4852 	for (i = 0; i < priv->num_fqs; i++) {
4853 		if (priv->fq[i].type == DPAA2_RX_FQ &&
4854 		    xdp_rxq_info_is_reg(&priv->fq[i].channel->xdp_rxq))
4855 			xdp_rxq_info_unreg(&priv->fq[i].channel->xdp_rxq);
4856 	}
4857 }
4858 
dpaa2_eth_probe(struct fsl_mc_device * dpni_dev)4859 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4860 {
4861 	struct device *dev;
4862 	struct net_device *net_dev = NULL;
4863 	struct dpaa2_eth_priv *priv = NULL;
4864 	int err = 0;
4865 
4866 	dev = &dpni_dev->dev;
4867 
4868 	/* Net device */
4869 	net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
4870 	if (!net_dev) {
4871 		dev_err(dev, "alloc_etherdev_mq() failed\n");
4872 		return -ENOMEM;
4873 	}
4874 
4875 	SET_NETDEV_DEV(net_dev, dev);
4876 	dev_set_drvdata(dev, net_dev);
4877 
4878 	priv = netdev_priv(net_dev);
4879 	priv->net_dev = net_dev;
4880 	SET_NETDEV_DEVLINK_PORT(net_dev, &priv->devlink_port);
4881 
4882 	mutex_init(&priv->mac_lock);
4883 
4884 	priv->iommu_domain = iommu_get_domain_for_dev(dev);
4885 
4886 	priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4887 	priv->rx_tstamp = false;
4888 
4889 	priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", WQ_PERCPU, 0);
4890 	if (!priv->dpaa2_ptp_wq) {
4891 		err = -ENOMEM;
4892 		goto err_wq_alloc;
4893 	}
4894 
4895 	INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4896 	mutex_init(&priv->onestep_tstamp_lock);
4897 	skb_queue_head_init(&priv->tx_skbs);
4898 
4899 	priv->rx_copybreak = DPAA2_ETH_DEFAULT_COPYBREAK;
4900 
4901 	/* Obtain a MC portal */
4902 	err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4903 				     &priv->mc_io);
4904 	if (err) {
4905 		if (err == -ENXIO) {
4906 			dev_dbg(dev, "waiting for MC portal\n");
4907 			err = -EPROBE_DEFER;
4908 		} else {
4909 			dev_err(dev, "MC portal allocation failed\n");
4910 		}
4911 		goto err_portal_alloc;
4912 	}
4913 
4914 	/* MC objects initialization and configuration */
4915 	err = dpaa2_eth_setup_dpni(dpni_dev);
4916 	if (err)
4917 		goto err_dpni_setup;
4918 
4919 	err = dpaa2_eth_setup_dpio(priv);
4920 	if (err)
4921 		goto err_dpio_setup;
4922 
4923 	dpaa2_eth_setup_fqs(priv);
4924 
4925 	err = dpaa2_eth_setup_default_dpbp(priv);
4926 	if (err)
4927 		goto err_dpbp_setup;
4928 
4929 	err = dpaa2_eth_bind_dpni(priv);
4930 	if (err)
4931 		goto err_bind;
4932 
4933 	/* Add a NAPI context for each channel */
4934 	dpaa2_eth_add_ch_napi(priv);
4935 
4936 	/* Percpu statistics */
4937 	priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4938 	if (!priv->percpu_stats) {
4939 		dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4940 		err = -ENOMEM;
4941 		goto err_alloc_percpu_stats;
4942 	}
4943 	priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4944 	if (!priv->percpu_extras) {
4945 		dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4946 		err = -ENOMEM;
4947 		goto err_alloc_percpu_extras;
4948 	}
4949 
4950 	priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4951 	if (!priv->sgt_cache) {
4952 		dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4953 		err = -ENOMEM;
4954 		goto err_alloc_sgt_cache;
4955 	}
4956 
4957 	priv->fd = alloc_percpu(*priv->fd);
4958 	if (!priv->fd) {
4959 		dev_err(dev, "alloc_percpu(fds) failed\n");
4960 		err = -ENOMEM;
4961 		goto err_alloc_fds;
4962 	}
4963 
4964 	err = dpaa2_eth_netdev_init(net_dev);
4965 	if (err)
4966 		goto err_netdev_init;
4967 
4968 	/* Configure checksum offload based on current interface flags */
4969 	err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
4970 	if (err)
4971 		goto err_csum;
4972 
4973 	err = dpaa2_eth_set_tx_csum(priv,
4974 				    !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
4975 	if (err)
4976 		goto err_csum;
4977 
4978 	err = dpaa2_eth_alloc_rings(priv);
4979 	if (err)
4980 		goto err_alloc_rings;
4981 
4982 #ifdef CONFIG_FSL_DPAA2_ETH_DCB
4983 	if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4984 		priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4985 		net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4986 	} else {
4987 		dev_dbg(dev, "PFC not supported\n");
4988 	}
4989 #endif
4990 
4991 	err = dpaa2_eth_connect_mac(priv);
4992 	if (err)
4993 		goto err_connect_mac;
4994 
4995 	err = dpaa2_eth_setup_irqs(dpni_dev);
4996 	if (err) {
4997 		netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
4998 		priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
4999 						"%s_poll_link", net_dev->name);
5000 		if (IS_ERR(priv->poll_thread)) {
5001 			dev_err(dev, "Error starting polling thread\n");
5002 			goto err_poll_thread;
5003 		}
5004 		priv->do_link_poll = true;
5005 	}
5006 
5007 	err = dpaa2_eth_dl_alloc(priv);
5008 	if (err)
5009 		goto err_dl_register;
5010 
5011 	err = dpaa2_eth_dl_traps_register(priv);
5012 	if (err)
5013 		goto err_dl_trap_register;
5014 
5015 	err = dpaa2_eth_dl_port_add(priv);
5016 	if (err)
5017 		goto err_dl_port_add;
5018 
5019 	net_dev->needed_headroom = DPAA2_ETH_SWA_SIZE + DPAA2_ETH_TX_BUF_ALIGN;
5020 
5021 	err = register_netdev(net_dev);
5022 	if (err < 0) {
5023 		dev_err(dev, "register_netdev() failed\n");
5024 		goto err_netdev_reg;
5025 	}
5026 
5027 #ifdef CONFIG_DEBUG_FS
5028 	dpaa2_dbg_add(priv);
5029 #endif
5030 
5031 	dpaa2_eth_dl_register(priv);
5032 	dev_info(dev, "Probed interface %s\n", net_dev->name);
5033 	return 0;
5034 
5035 err_netdev_reg:
5036 	dpaa2_eth_dl_port_del(priv);
5037 err_dl_port_add:
5038 	dpaa2_eth_dl_traps_unregister(priv);
5039 err_dl_trap_register:
5040 	dpaa2_eth_dl_free(priv);
5041 err_dl_register:
5042 	if (priv->do_link_poll)
5043 		kthread_stop(priv->poll_thread);
5044 	else
5045 		fsl_mc_free_irqs(dpni_dev);
5046 err_poll_thread:
5047 	dpaa2_eth_disconnect_mac(priv);
5048 err_connect_mac:
5049 	dpaa2_eth_free_rings(priv);
5050 err_alloc_rings:
5051 err_csum:
5052 err_netdev_init:
5053 	free_percpu(priv->fd);
5054 err_alloc_fds:
5055 	free_percpu(priv->sgt_cache);
5056 err_alloc_sgt_cache:
5057 	free_percpu(priv->percpu_extras);
5058 err_alloc_percpu_extras:
5059 	free_percpu(priv->percpu_stats);
5060 err_alloc_percpu_stats:
5061 	dpaa2_eth_del_ch_napi(priv);
5062 	dpaa2_eth_free_rx_xdp_rxq(priv);
5063 err_bind:
5064 	dpaa2_eth_free_dpbps(priv);
5065 err_dpbp_setup:
5066 	dpaa2_eth_free_dpio(priv);
5067 err_dpio_setup:
5068 	dpaa2_eth_free_dpni(priv);
5069 err_dpni_setup:
5070 	fsl_mc_portal_free(priv->mc_io);
5071 err_portal_alloc:
5072 	destroy_workqueue(priv->dpaa2_ptp_wq);
5073 err_wq_alloc:
5074 	dev_set_drvdata(dev, NULL);
5075 	free_netdev(net_dev);
5076 
5077 	return err;
5078 }
5079 
dpaa2_eth_remove(struct fsl_mc_device * ls_dev)5080 static void dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
5081 {
5082 	struct device *dev;
5083 	struct net_device *net_dev;
5084 	struct dpaa2_eth_priv *priv;
5085 
5086 	dev = &ls_dev->dev;
5087 	net_dev = dev_get_drvdata(dev);
5088 	priv = netdev_priv(net_dev);
5089 
5090 	dpaa2_eth_dl_unregister(priv);
5091 
5092 #ifdef CONFIG_DEBUG_FS
5093 	dpaa2_dbg_remove(priv);
5094 #endif
5095 
5096 	unregister_netdev(net_dev);
5097 
5098 	dpaa2_eth_dl_port_del(priv);
5099 	dpaa2_eth_dl_traps_unregister(priv);
5100 	dpaa2_eth_dl_free(priv);
5101 
5102 	if (priv->do_link_poll)
5103 		kthread_stop(priv->poll_thread);
5104 	else
5105 		fsl_mc_free_irqs(ls_dev);
5106 
5107 	dpaa2_eth_disconnect_mac(priv);
5108 	dpaa2_eth_free_rings(priv);
5109 	free_percpu(priv->fd);
5110 	free_percpu(priv->sgt_cache);
5111 	free_percpu(priv->percpu_stats);
5112 	free_percpu(priv->percpu_extras);
5113 
5114 	dpaa2_eth_del_ch_napi(priv);
5115 	dpaa2_eth_free_rx_xdp_rxq(priv);
5116 	dpaa2_eth_free_dpbps(priv);
5117 	dpaa2_eth_free_dpio(priv);
5118 	dpaa2_eth_free_dpni(priv);
5119 	if (priv->onestep_reg_base)
5120 		iounmap(priv->onestep_reg_base);
5121 
5122 	fsl_mc_portal_free(priv->mc_io);
5123 
5124 	destroy_workqueue(priv->dpaa2_ptp_wq);
5125 
5126 	dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
5127 
5128 	free_netdev(net_dev);
5129 }
5130 
5131 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
5132 	{
5133 		.vendor = FSL_MC_VENDOR_FREESCALE,
5134 		.obj_type = "dpni",
5135 	},
5136 	{ .vendor = 0x0 }
5137 };
5138 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
5139 
5140 static struct fsl_mc_driver dpaa2_eth_driver = {
5141 	.driver = {
5142 		.name = KBUILD_MODNAME,
5143 	},
5144 	.probe = dpaa2_eth_probe,
5145 	.remove = dpaa2_eth_remove,
5146 	.match_id_table = dpaa2_eth_match_id_table
5147 };
5148 
dpaa2_eth_driver_init(void)5149 static int __init dpaa2_eth_driver_init(void)
5150 {
5151 	int err;
5152 
5153 	dpaa2_eth_dbg_init();
5154 	err = fsl_mc_driver_register(&dpaa2_eth_driver);
5155 	if (err) {
5156 		dpaa2_eth_dbg_exit();
5157 		return err;
5158 	}
5159 
5160 	return 0;
5161 }
5162 
dpaa2_eth_driver_exit(void)5163 static void __exit dpaa2_eth_driver_exit(void)
5164 {
5165 	dpaa2_eth_dbg_exit();
5166 	fsl_mc_driver_unregister(&dpaa2_eth_driver);
5167 }
5168 
5169 module_init(dpaa2_eth_driver_init);
5170 module_exit(dpaa2_eth_driver_exit);
5171