1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
3 * Copyright 2016-2020 NXP
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/msi.h>
12 #include <linux/kthread.h>
13 #include <linux/iommu.h>
14 #include <linux/net_tstamp.h>
15 #include <linux/fsl/mc.h>
16 #include <linux/bpf.h>
17 #include <linux/bpf_trace.h>
20 #include "dpaa2-eth.h"
22 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
23 * using trace events only need to #include <trace/events/sched.h>
25 #define CREATE_TRACE_POINTS
26 #include "dpaa2-eth-trace.h"
28 MODULE_LICENSE("Dual BSD/GPL");
29 MODULE_AUTHOR("Freescale Semiconductor, Inc");
30 MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
32 static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
35 phys_addr_t phys_addr;
37 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
39 return phys_to_virt(phys_addr);
42 static void validate_rx_csum(struct dpaa2_eth_priv *priv,
46 skb_checksum_none_assert(skb);
48 /* HW checksum validation is disabled, nothing to do here */
49 if (!(priv->net_dev->features & NETIF_F_RXCSUM))
52 /* Read checksum validation bits */
53 if (!((fd_status & DPAA2_FAS_L3CV) &&
54 (fd_status & DPAA2_FAS_L4CV)))
57 /* Inform the stack there's no need to compute L3/L4 csum anymore */
58 skb->ip_summed = CHECKSUM_UNNECESSARY;
61 /* Free a received FD.
62 * Not to be used for Tx conf FDs or on any other paths.
64 static void free_rx_fd(struct dpaa2_eth_priv *priv,
65 const struct dpaa2_fd *fd,
68 struct device *dev = priv->net_dev->dev.parent;
69 dma_addr_t addr = dpaa2_fd_get_addr(fd);
70 u8 fd_format = dpaa2_fd_get_format(fd);
71 struct dpaa2_sg_entry *sgt;
75 /* If single buffer frame, just free the data buffer */
76 if (fd_format == dpaa2_fd_single)
78 else if (fd_format != dpaa2_fd_sg)
79 /* We don't support any other format */
82 /* For S/G frames, we first need to free all SG entries
83 * except the first one, which was taken care of already
85 sgt = vaddr + dpaa2_fd_get_offset(fd);
86 for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
87 addr = dpaa2_sg_get_addr(&sgt[i]);
88 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
89 dma_unmap_page(dev, addr, priv->rx_buf_size,
92 free_pages((unsigned long)sg_vaddr, 0);
93 if (dpaa2_sg_is_final(&sgt[i]))
98 free_pages((unsigned long)vaddr, 0);
101 /* Build a linear skb based on a single-buffer frame descriptor */
102 static struct sk_buff *build_linear_skb(struct dpaa2_eth_channel *ch,
103 const struct dpaa2_fd *fd,
106 struct sk_buff *skb = NULL;
107 u16 fd_offset = dpaa2_fd_get_offset(fd);
108 u32 fd_length = dpaa2_fd_get_len(fd);
112 skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
116 skb_reserve(skb, fd_offset);
117 skb_put(skb, fd_length);
122 /* Build a non linear (fragmented) skb based on a S/G table */
123 static struct sk_buff *build_frag_skb(struct dpaa2_eth_priv *priv,
124 struct dpaa2_eth_channel *ch,
125 struct dpaa2_sg_entry *sgt)
127 struct sk_buff *skb = NULL;
128 struct device *dev = priv->net_dev->dev.parent;
133 struct page *page, *head_page;
137 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
138 struct dpaa2_sg_entry *sge = &sgt[i];
140 /* NOTE: We only support SG entries in dpaa2_sg_single format,
141 * but this is the only format we may receive from HW anyway
144 /* Get the address and length from the S/G entry */
145 sg_addr = dpaa2_sg_get_addr(sge);
146 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
147 dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
150 sg_length = dpaa2_sg_get_len(sge);
153 /* We build the skb around the first data buffer */
154 skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
155 if (unlikely(!skb)) {
156 /* Free the first SG entry now, since we already
157 * unmapped it and obtained the virtual address
159 free_pages((unsigned long)sg_vaddr, 0);
161 /* We still need to subtract the buffers used
162 * by this FD from our software counter
164 while (!dpaa2_sg_is_final(&sgt[i]) &&
165 i < DPAA2_ETH_MAX_SG_ENTRIES)
170 sg_offset = dpaa2_sg_get_offset(sge);
171 skb_reserve(skb, sg_offset);
172 skb_put(skb, sg_length);
174 /* Rest of the data buffers are stored as skb frags */
175 page = virt_to_page(sg_vaddr);
176 head_page = virt_to_head_page(sg_vaddr);
178 /* Offset in page (which may be compound).
179 * Data in subsequent SG entries is stored from the
180 * beginning of the buffer, so we don't need to add the
183 page_offset = ((unsigned long)sg_vaddr &
185 (page_address(page) - page_address(head_page));
187 skb_add_rx_frag(skb, i - 1, head_page, page_offset,
188 sg_length, priv->rx_buf_size);
191 if (dpaa2_sg_is_final(sge))
195 WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
197 /* Count all data buffers + SG table buffer */
198 ch->buf_count -= i + 2;
203 /* Free buffers acquired from the buffer pool or which were meant to
204 * be released in the pool
206 static void free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array, int count)
208 struct device *dev = priv->net_dev->dev.parent;
212 for (i = 0; i < count; i++) {
213 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
214 dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
216 free_pages((unsigned long)vaddr, 0);
220 static void xdp_release_buf(struct dpaa2_eth_priv *priv,
221 struct dpaa2_eth_channel *ch,
227 ch->xdp.drop_bufs[ch->xdp.drop_cnt++] = addr;
228 if (ch->xdp.drop_cnt < DPAA2_ETH_BUFS_PER_CMD)
231 while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
233 ch->xdp.drop_cnt)) == -EBUSY) {
234 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
240 free_bufs(priv, ch->xdp.drop_bufs, ch->xdp.drop_cnt);
241 ch->buf_count -= ch->xdp.drop_cnt;
244 ch->xdp.drop_cnt = 0;
247 static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
248 struct dpaa2_eth_fq *fq,
249 struct dpaa2_eth_xdp_fds *xdp_fds)
251 int total_enqueued = 0, retries = 0, enqueued;
252 struct dpaa2_eth_drv_stats *percpu_extras;
253 int num_fds, err, max_retries;
254 struct dpaa2_fd *fds;
256 percpu_extras = this_cpu_ptr(priv->percpu_extras);
258 /* try to enqueue all the FDs until the max number of retries is hit */
260 num_fds = xdp_fds->num;
261 max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
262 while (total_enqueued < num_fds && retries < max_retries) {
263 err = priv->enqueue(priv, fq, &fds[total_enqueued],
264 0, num_fds - total_enqueued, &enqueued);
266 percpu_extras->tx_portal_busy += ++retries;
269 total_enqueued += enqueued;
273 return total_enqueued;
276 static void xdp_tx_flush(struct dpaa2_eth_priv *priv,
277 struct dpaa2_eth_channel *ch,
278 struct dpaa2_eth_fq *fq)
280 struct rtnl_link_stats64 *percpu_stats;
281 struct dpaa2_fd *fds;
284 percpu_stats = this_cpu_ptr(priv->percpu_stats);
286 // enqueue the array of XDP_TX frames
287 enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);
289 /* update statistics */
290 percpu_stats->tx_packets += enqueued;
291 fds = fq->xdp_tx_fds.fds;
292 for (i = 0; i < enqueued; i++) {
293 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
296 for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
297 xdp_release_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
298 percpu_stats->tx_errors++;
299 ch->stats.xdp_tx_err++;
301 fq->xdp_tx_fds.num = 0;
304 static void xdp_enqueue(struct dpaa2_eth_priv *priv,
305 struct dpaa2_eth_channel *ch,
307 void *buf_start, u16 queue_id)
309 struct dpaa2_faead *faead;
310 struct dpaa2_fd *dest_fd;
311 struct dpaa2_eth_fq *fq;
314 /* Mark the egress frame hardware annotation area as valid */
315 frc = dpaa2_fd_get_frc(fd);
316 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
317 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
319 /* Instruct hardware to release the FD buffer directly into
320 * the buffer pool once transmission is completed, instead of
321 * sending a Tx confirmation frame to us
323 ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
324 faead = dpaa2_get_faead(buf_start, false);
325 faead->ctrl = cpu_to_le32(ctrl);
326 faead->conf_fqid = 0;
328 fq = &priv->fq[queue_id];
329 dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
330 memcpy(dest_fd, fd, sizeof(*dest_fd));
332 if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
335 xdp_tx_flush(priv, ch, fq);
338 static u32 run_xdp(struct dpaa2_eth_priv *priv,
339 struct dpaa2_eth_channel *ch,
340 struct dpaa2_eth_fq *rx_fq,
341 struct dpaa2_fd *fd, void *vaddr)
343 dma_addr_t addr = dpaa2_fd_get_addr(fd);
344 struct bpf_prog *xdp_prog;
346 u32 xdp_act = XDP_PASS;
351 xdp_prog = READ_ONCE(ch->xdp.prog);
355 xdp.data = vaddr + dpaa2_fd_get_offset(fd);
356 xdp.data_end = xdp.data + dpaa2_fd_get_len(fd);
357 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
358 xdp_set_data_meta_invalid(&xdp);
359 xdp.rxq = &ch->xdp_rxq;
361 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE -
362 (dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM);
364 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
366 /* xdp.data pointer may have changed */
367 dpaa2_fd_set_offset(fd, xdp.data - vaddr);
368 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
374 xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
377 bpf_warn_invalid_xdp_action(xdp_act);
380 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
383 xdp_release_buf(priv, ch, addr);
384 ch->stats.xdp_drop++;
387 dma_unmap_page(priv->net_dev->dev.parent, addr,
388 priv->rx_buf_size, DMA_BIDIRECTIONAL);
391 /* Allow redirect use of full headroom */
392 xdp.data_hard_start = vaddr;
393 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
395 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
397 ch->stats.xdp_drop++;
399 ch->stats.xdp_redirect++;
403 ch->xdp.res |= xdp_act;
409 /* Main Rx frame processing routine */
410 static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
411 struct dpaa2_eth_channel *ch,
412 const struct dpaa2_fd *fd,
413 struct dpaa2_eth_fq *fq)
415 dma_addr_t addr = dpaa2_fd_get_addr(fd);
416 u8 fd_format = dpaa2_fd_get_format(fd);
419 struct rtnl_link_stats64 *percpu_stats;
420 struct dpaa2_eth_drv_stats *percpu_extras;
421 struct device *dev = priv->net_dev->dev.parent;
422 struct dpaa2_fas *fas;
428 trace_dpaa2_rx_fd(priv->net_dev, fd);
430 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
431 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
434 fas = dpaa2_get_fas(vaddr, false);
436 buf_data = vaddr + dpaa2_fd_get_offset(fd);
439 percpu_stats = this_cpu_ptr(priv->percpu_stats);
440 percpu_extras = this_cpu_ptr(priv->percpu_extras);
442 if (fd_format == dpaa2_fd_single) {
443 xdp_act = run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
444 if (xdp_act != XDP_PASS) {
445 percpu_stats->rx_packets++;
446 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
450 dma_unmap_page(dev, addr, priv->rx_buf_size,
452 skb = build_linear_skb(ch, fd, vaddr);
453 } else if (fd_format == dpaa2_fd_sg) {
454 WARN_ON(priv->xdp_prog);
456 dma_unmap_page(dev, addr, priv->rx_buf_size,
458 skb = build_frag_skb(priv, ch, buf_data);
459 free_pages((unsigned long)vaddr, 0);
460 percpu_extras->rx_sg_frames++;
461 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
463 /* We don't support any other format */
464 goto err_frame_format;
472 /* Get the timestamp value */
473 if (priv->rx_tstamp) {
474 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
475 __le64 *ts = dpaa2_get_ts(vaddr, false);
478 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
480 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
481 shhwtstamps->hwtstamp = ns_to_ktime(ns);
484 /* Check if we need to validate the L4 csum */
485 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
486 status = le32_to_cpu(fas->status);
487 validate_rx_csum(priv, status, skb);
490 skb->protocol = eth_type_trans(skb, priv->net_dev);
491 skb_record_rx_queue(skb, fq->flowid);
493 percpu_stats->rx_packets++;
494 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
496 list_add_tail(&skb->list, ch->rx_list);
501 free_rx_fd(priv, fd, vaddr);
503 percpu_stats->rx_dropped++;
506 /* Consume all frames pull-dequeued into the store. This is the simplest way to
507 * make sure we don't accidentally issue another volatile dequeue which would
508 * overwrite (leak) frames already in the store.
510 * Observance of NAPI budget is not our concern, leaving that to the caller.
512 static int consume_frames(struct dpaa2_eth_channel *ch,
513 struct dpaa2_eth_fq **src)
515 struct dpaa2_eth_priv *priv = ch->priv;
516 struct dpaa2_eth_fq *fq = NULL;
518 const struct dpaa2_fd *fd;
519 int cleaned = 0, retries = 0;
523 dq = dpaa2_io_store_next(ch->store, &is_last);
525 /* If we're here, we *must* have placed a
526 * volatile dequeue comnmand, so keep reading through
527 * the store until we get some sort of valid response
528 * token (either a valid frame or an "empty dequeue")
530 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
531 netdev_err_once(priv->net_dev,
532 "Unable to read a valid dequeue response\n");
538 fd = dpaa2_dq_fd(dq);
539 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
541 fq->consume(priv, ch, fd, fq);
549 fq->stats.frames += cleaned;
550 ch->stats.frames += cleaned;
552 /* A dequeue operation only pulls frames from a single queue
553 * into the store. Return the frame queue as an out param.
561 /* Configure the egress frame annotation for timestamp update */
562 static void enable_tx_tstamp(struct dpaa2_fd *fd, void *buf_start)
564 struct dpaa2_faead *faead;
567 /* Mark the egress frame annotation area as valid */
568 frc = dpaa2_fd_get_frc(fd);
569 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
571 /* Set hardware annotation size */
572 ctrl = dpaa2_fd_get_ctrl(fd);
573 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
575 /* enable UPD (update prepanded data) bit in FAEAD field of
576 * hardware frame annotation area
578 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
579 faead = dpaa2_get_faead(buf_start, true);
580 faead->ctrl = cpu_to_le32(ctrl);
583 /* Create a frame descriptor based on a fragmented skb */
584 static int build_sg_fd(struct dpaa2_eth_priv *priv,
588 struct device *dev = priv->net_dev->dev.parent;
589 void *sgt_buf = NULL;
591 int nr_frags = skb_shinfo(skb)->nr_frags;
592 struct dpaa2_sg_entry *sgt;
595 struct scatterlist *scl, *crt_scl;
598 struct dpaa2_eth_swa *swa;
600 /* Create and map scatterlist.
601 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
602 * to go beyond nr_frags+1.
603 * Note: We don't support chained scatterlists
605 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
608 scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
612 sg_init_table(scl, nr_frags + 1);
613 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
614 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
615 if (unlikely(!num_dma_bufs)) {
617 goto dma_map_sg_failed;
620 /* Prepare the HW SGT structure */
621 sgt_buf_size = priv->tx_data_offset +
622 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
623 sgt_buf = napi_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
624 if (unlikely(!sgt_buf)) {
626 goto sgt_buf_alloc_failed;
628 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
629 memset(sgt_buf, 0, sgt_buf_size);
631 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
633 /* Fill in the HW SGT structure.
635 * sgt_buf is zeroed out, so the following fields are implicit
636 * in all sgt entries:
638 * - format is 'dpaa2_sg_single'
640 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
641 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
642 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
644 dpaa2_sg_set_final(&sgt[i - 1], true);
646 /* Store the skb backpointer in the SGT buffer.
647 * Fit the scatterlist and the number of buffers alongside the
648 * skb backpointer in the software annotation area. We'll need
649 * all of them on Tx Conf.
651 swa = (struct dpaa2_eth_swa *)sgt_buf;
652 swa->type = DPAA2_ETH_SWA_SG;
655 swa->sg.num_sg = num_sg;
656 swa->sg.sgt_size = sgt_buf_size;
658 /* Separately map the SGT buffer */
659 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
660 if (unlikely(dma_mapping_error(dev, addr))) {
662 goto dma_map_single_failed;
664 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
665 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
666 dpaa2_fd_set_addr(fd, addr);
667 dpaa2_fd_set_len(fd, skb->len);
668 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
670 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
671 enable_tx_tstamp(fd, sgt_buf);
675 dma_map_single_failed:
676 skb_free_frag(sgt_buf);
677 sgt_buf_alloc_failed:
678 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
684 /* Create a frame descriptor based on a linear skb */
685 static int build_single_fd(struct dpaa2_eth_priv *priv,
689 struct device *dev = priv->net_dev->dev.parent;
690 u8 *buffer_start, *aligned_start;
691 struct dpaa2_eth_swa *swa;
694 buffer_start = skb->data - dpaa2_eth_needed_headroom(priv, skb);
696 /* If there's enough room to align the FD address, do it.
697 * It will help hardware optimize accesses.
699 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
700 DPAA2_ETH_TX_BUF_ALIGN);
701 if (aligned_start >= skb->head)
702 buffer_start = aligned_start;
704 /* Store a backpointer to the skb at the beginning of the buffer
705 * (in the private data area) such that we can release it
708 swa = (struct dpaa2_eth_swa *)buffer_start;
709 swa->type = DPAA2_ETH_SWA_SINGLE;
710 swa->single.skb = skb;
712 addr = dma_map_single(dev, buffer_start,
713 skb_tail_pointer(skb) - buffer_start,
715 if (unlikely(dma_mapping_error(dev, addr)))
718 dpaa2_fd_set_addr(fd, addr);
719 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
720 dpaa2_fd_set_len(fd, skb->len);
721 dpaa2_fd_set_format(fd, dpaa2_fd_single);
722 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
724 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
725 enable_tx_tstamp(fd, buffer_start);
730 /* FD freeing routine on the Tx path
732 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
733 * back-pointed to is also freed.
734 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
737 static void free_tx_fd(const struct dpaa2_eth_priv *priv,
738 struct dpaa2_eth_fq *fq,
739 const struct dpaa2_fd *fd, bool in_napi)
741 struct device *dev = priv->net_dev->dev.parent;
743 struct sk_buff *skb = NULL;
744 unsigned char *buffer_start;
745 struct dpaa2_eth_swa *swa;
746 u8 fd_format = dpaa2_fd_get_format(fd);
747 u32 fd_len = dpaa2_fd_get_len(fd);
749 fd_addr = dpaa2_fd_get_addr(fd);
750 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
751 swa = (struct dpaa2_eth_swa *)buffer_start;
753 if (fd_format == dpaa2_fd_single) {
754 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
755 skb = swa->single.skb;
756 /* Accessing the skb buffer is safe before dma unmap,
757 * because we didn't map the actual skb shell.
759 dma_unmap_single(dev, fd_addr,
760 skb_tail_pointer(skb) - buffer_start,
763 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
764 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
767 } else if (fd_format == dpaa2_fd_sg) {
770 /* Unmap the scatterlist */
771 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
775 /* Unmap the SGT buffer */
776 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
779 netdev_dbg(priv->net_dev, "Invalid FD format\n");
783 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
785 fq->dq_bytes += fd_len;
788 if (swa->type == DPAA2_ETH_SWA_XDP) {
789 xdp_return_frame(swa->xdp.xdpf);
793 /* Get the timestamp value */
794 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
795 struct skb_shared_hwtstamps shhwtstamps;
796 __le64 *ts = dpaa2_get_ts(buffer_start, true);
799 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
801 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
802 shhwtstamps.hwtstamp = ns_to_ktime(ns);
803 skb_tstamp_tx(skb, &shhwtstamps);
806 /* Free SGT buffer allocated on tx */
807 if (fd_format != dpaa2_fd_single)
808 skb_free_frag(buffer_start);
810 /* Move on with skb release */
811 napi_consume_skb(skb, in_napi);
814 static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
816 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
818 struct rtnl_link_stats64 *percpu_stats;
819 struct dpaa2_eth_drv_stats *percpu_extras;
820 struct dpaa2_eth_fq *fq;
821 struct netdev_queue *nq;
823 unsigned int needed_headroom;
828 percpu_stats = this_cpu_ptr(priv->percpu_stats);
829 percpu_extras = this_cpu_ptr(priv->percpu_extras);
831 needed_headroom = dpaa2_eth_needed_headroom(priv, skb);
832 if (skb_headroom(skb) < needed_headroom) {
835 ns = skb_realloc_headroom(skb, needed_headroom);
837 percpu_stats->tx_dropped++;
838 goto err_alloc_headroom;
840 percpu_extras->tx_reallocs++;
843 skb_set_owner_w(ns, skb->sk);
849 /* We'll be holding a back-reference to the skb until Tx Confirmation;
850 * we don't want that overwritten by a concurrent Tx with a cloned skb.
852 skb = skb_unshare(skb, GFP_ATOMIC);
853 if (unlikely(!skb)) {
854 /* skb_unshare() has already freed the skb */
855 percpu_stats->tx_dropped++;
859 /* Setup the FD fields */
860 memset(&fd, 0, sizeof(fd));
862 if (skb_is_nonlinear(skb)) {
863 err = build_sg_fd(priv, skb, &fd);
864 percpu_extras->tx_sg_frames++;
865 percpu_extras->tx_sg_bytes += skb->len;
867 err = build_single_fd(priv, skb, &fd);
871 percpu_stats->tx_dropped++;
876 trace_dpaa2_tx_fd(net_dev, &fd);
878 /* TxConf FQ selection relies on queue id from the stack.
879 * In case of a forwarded frame from another DPNI interface, we choose
880 * a queue affined to the same core that processed the Rx frame
882 queue_mapping = skb_get_queue_mapping(skb);
884 if (net_dev->num_tc) {
885 prio = netdev_txq_to_tc(net_dev, queue_mapping);
886 /* Hardware interprets priority level 0 as being the highest,
887 * so we need to do a reverse mapping to the netdev tc index
889 prio = net_dev->num_tc - prio - 1;
890 /* We have only one FQ array entry for all Tx hardware queues
891 * with the same flow id (but different priority levels)
893 queue_mapping %= dpaa2_eth_queue_count(priv);
895 fq = &priv->fq[queue_mapping];
897 fd_len = dpaa2_fd_get_len(&fd);
898 nq = netdev_get_tx_queue(net_dev, queue_mapping);
899 netdev_tx_sent_queue(nq, fd_len);
901 /* Everything that happens after this enqueues might race with
902 * the Tx confirmation callback for this frame
904 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
905 err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
909 percpu_extras->tx_portal_busy += i;
910 if (unlikely(err < 0)) {
911 percpu_stats->tx_errors++;
912 /* Clean up everything, including freeing the skb */
913 free_tx_fd(priv, fq, &fd, false);
914 netdev_tx_completed_queue(nq, 1, fd_len);
916 percpu_stats->tx_packets++;
917 percpu_stats->tx_bytes += fd_len;
929 /* Tx confirmation frame processing routine */
930 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
931 struct dpaa2_eth_channel *ch __always_unused,
932 const struct dpaa2_fd *fd,
933 struct dpaa2_eth_fq *fq)
935 struct rtnl_link_stats64 *percpu_stats;
936 struct dpaa2_eth_drv_stats *percpu_extras;
937 u32 fd_len = dpaa2_fd_get_len(fd);
941 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
943 percpu_extras = this_cpu_ptr(priv->percpu_extras);
944 percpu_extras->tx_conf_frames++;
945 percpu_extras->tx_conf_bytes += fd_len;
947 /* Check frame errors in the FD field */
948 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
949 free_tx_fd(priv, fq, fd, true);
951 if (likely(!fd_errors))
955 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
958 percpu_stats = this_cpu_ptr(priv->percpu_stats);
959 /* Tx-conf logically pertains to the egress path. */
960 percpu_stats->tx_errors++;
963 static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
967 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
968 DPNI_OFF_RX_L3_CSUM, enable);
970 netdev_err(priv->net_dev,
971 "dpni_set_offload(RX_L3_CSUM) failed\n");
975 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
976 DPNI_OFF_RX_L4_CSUM, enable);
978 netdev_err(priv->net_dev,
979 "dpni_set_offload(RX_L4_CSUM) failed\n");
986 static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
990 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
991 DPNI_OFF_TX_L3_CSUM, enable);
993 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
997 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
998 DPNI_OFF_TX_L4_CSUM, enable);
1000 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1007 /* Perform a single release command to add buffers
1008 * to the specified buffer pool
1010 static int add_bufs(struct dpaa2_eth_priv *priv,
1011 struct dpaa2_eth_channel *ch, u16 bpid)
1013 struct device *dev = priv->net_dev->dev.parent;
1014 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1020 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1021 /* Allocate buffer visible to WRIOP + skb shared info +
1024 /* allocate one page for each Rx buffer. WRIOP sees
1025 * the entire page except for a tailroom reserved for
1028 page = dev_alloc_pages(0);
1032 addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
1034 if (unlikely(dma_mapping_error(dev, addr)))
1037 buf_array[i] = addr;
1040 trace_dpaa2_eth_buf_seed(priv->net_dev,
1041 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
1042 addr, priv->rx_buf_size,
1047 /* In case the portal is busy, retry until successful */
1048 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
1049 buf_array, i)) == -EBUSY) {
1050 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1055 /* If release command failed, clean up and bail out;
1056 * not much else we can do about it
1059 free_bufs(priv, buf_array, i);
1066 __free_pages(page, 0);
1068 /* If we managed to allocate at least some buffers,
1069 * release them to hardware
1077 static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
1082 for (j = 0; j < priv->num_channels; j++) {
1083 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1084 i += DPAA2_ETH_BUFS_PER_CMD) {
1085 new_count = add_bufs(priv, priv->channel[j], bpid);
1086 priv->channel[j]->buf_count += new_count;
1088 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
1098 * Drain the specified number of buffers from the DPNI's private buffer pool.
1099 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1101 static void drain_bufs(struct dpaa2_eth_priv *priv, int count)
1103 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1108 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
1111 if (ret == -EBUSY &&
1112 retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1114 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1117 free_bufs(priv, buf_array, ret);
1122 static void drain_pool(struct dpaa2_eth_priv *priv)
1126 drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1127 drain_bufs(priv, 1);
1129 for (i = 0; i < priv->num_channels; i++)
1130 priv->channel[i]->buf_count = 0;
1133 /* Function is called from softirq context only, so we don't need to guard
1134 * the access to percpu count
1136 static int refill_pool(struct dpaa2_eth_priv *priv,
1137 struct dpaa2_eth_channel *ch,
1142 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1146 new_count = add_bufs(priv, ch, bpid);
1147 if (unlikely(!new_count)) {
1148 /* Out of memory; abort for now, we'll try later on */
1151 ch->buf_count += new_count;
1152 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1154 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1160 static int pull_channel(struct dpaa2_eth_channel *ch)
1165 /* Retry while portal is busy */
1167 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1171 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
1173 ch->stats.dequeue_portal_busy += dequeues;
1175 ch->stats.pull_err++;
1180 /* NAPI poll routine
1182 * Frames are dequeued from the QMan channel associated with this NAPI context.
1183 * Rx, Tx confirmation and (if configured) Rx error frames all count
1184 * towards the NAPI budget.
1186 static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1188 struct dpaa2_eth_channel *ch;
1189 struct dpaa2_eth_priv *priv;
1190 int rx_cleaned = 0, txconf_cleaned = 0;
1191 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1192 struct netdev_queue *nq;
1193 int store_cleaned, work_done;
1194 struct list_head rx_list;
1199 ch = container_of(napi, struct dpaa2_eth_channel, napi);
1203 INIT_LIST_HEAD(&rx_list);
1204 ch->rx_list = &rx_list;
1207 err = pull_channel(ch);
1211 /* Refill pool if appropriate */
1212 refill_pool(priv, ch, priv->bpid);
1214 store_cleaned = consume_frames(ch, &fq);
1215 if (store_cleaned <= 0)
1217 if (fq->type == DPAA2_RX_FQ) {
1218 rx_cleaned += store_cleaned;
1219 flowid = fq->flowid;
1221 txconf_cleaned += store_cleaned;
1222 /* We have a single Tx conf FQ on this channel */
1226 /* If we either consumed the whole NAPI budget with Rx frames
1227 * or we reached the Tx confirmations threshold, we're done.
1229 if (rx_cleaned >= budget ||
1230 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1234 } while (store_cleaned);
1236 /* We didn't consume the entire budget, so finish napi and
1237 * re-enable data availability notifications
1239 napi_complete_done(napi, rx_cleaned);
1241 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1243 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
1244 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1245 ch->nctx.desired_cpu);
1247 work_done = max(rx_cleaned, 1);
1250 netif_receive_skb_list(ch->rx_list);
1252 if (txc_fq && txc_fq->dq_frames) {
1253 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1254 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1256 txc_fq->dq_frames = 0;
1257 txc_fq->dq_bytes = 0;
1260 if (ch->xdp.res & XDP_REDIRECT)
1262 else if (rx_cleaned && ch->xdp.res & XDP_TX)
1263 xdp_tx_flush(priv, ch, &priv->fq[flowid]);
1268 static void enable_ch_napi(struct dpaa2_eth_priv *priv)
1270 struct dpaa2_eth_channel *ch;
1273 for (i = 0; i < priv->num_channels; i++) {
1274 ch = priv->channel[i];
1275 napi_enable(&ch->napi);
1279 static void disable_ch_napi(struct dpaa2_eth_priv *priv)
1281 struct dpaa2_eth_channel *ch;
1284 for (i = 0; i < priv->num_channels; i++) {
1285 ch = priv->channel[i];
1286 napi_disable(&ch->napi);
1290 void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1291 bool tx_pause, bool pfc)
1293 struct dpni_taildrop td = {0};
1294 struct dpaa2_eth_fq *fq;
1297 /* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1298 * flow control is disabled (as it might interfere with either the
1299 * buffer pool depletion trigger for pause frames or with the group
1300 * congestion trigger for PFC frames)
1302 td.enable = !tx_pause;
1303 if (priv->rx_fqtd_enabled == td.enable)
1306 td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1307 td.units = DPNI_CONGESTION_UNIT_BYTES;
1309 for (i = 0; i < priv->num_fqs; i++) {
1311 if (fq->type != DPAA2_RX_FQ)
1313 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1314 DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1315 fq->tc, fq->flowid, &td);
1317 netdev_err(priv->net_dev,
1318 "dpni_set_taildrop(FQ) failed\n");
1323 priv->rx_fqtd_enabled = td.enable;
1326 /* Congestion group taildrop: threshold is in frames, per group
1327 * of FQs belonging to the same traffic class
1328 * Enabled if general Tx pause disabled or if PFCs are enabled
1329 * (congestion group threhsold for PFC generation is lower than the
1330 * CG taildrop threshold, so it won't interfere with it; we also
1331 * want frames in non-PFC enabled traffic classes to be kept in check)
1333 td.enable = !tx_pause || (tx_pause && pfc);
1334 if (priv->rx_cgtd_enabled == td.enable)
1337 td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
1338 td.units = DPNI_CONGESTION_UNIT_FRAMES;
1339 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
1340 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1341 DPNI_CP_GROUP, DPNI_QUEUE_RX,
1344 netdev_err(priv->net_dev,
1345 "dpni_set_taildrop(CG) failed\n");
1350 priv->rx_cgtd_enabled = td.enable;
1353 static int link_state_update(struct dpaa2_eth_priv *priv)
1355 struct dpni_link_state state = {0};
1359 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1360 if (unlikely(err)) {
1361 netdev_err(priv->net_dev,
1362 "dpni_get_link_state() failed\n");
1366 /* If Tx pause frame settings have changed, we need to update
1367 * Rx FQ taildrop configuration as well. We configure taildrop
1368 * only when pause frame generation is disabled.
1370 tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
1371 dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
1373 /* When we manage the MAC/PHY using phylink there is no need
1374 * to manually update the netif_carrier.
1379 /* Chech link state; speed / duplex changes are not treated yet */
1380 if (priv->link_state.up == state.up)
1384 netif_carrier_on(priv->net_dev);
1385 netif_tx_start_all_queues(priv->net_dev);
1387 netif_tx_stop_all_queues(priv->net_dev);
1388 netif_carrier_off(priv->net_dev);
1391 netdev_info(priv->net_dev, "Link Event: state %s\n",
1392 state.up ? "up" : "down");
1395 priv->link_state = state;
1400 static int dpaa2_eth_open(struct net_device *net_dev)
1402 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1405 err = seed_pool(priv, priv->bpid);
1407 /* Not much to do; the buffer pool, though not filled up,
1408 * may still contain some buffers which would enable us
1411 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
1412 priv->dpbp_dev->obj_desc.id, priv->bpid);
1416 /* We'll only start the txqs when the link is actually ready;
1417 * make sure we don't race against the link up notification,
1418 * which may come immediately after dpni_enable();
1420 netif_tx_stop_all_queues(net_dev);
1422 /* Also, explicitly set carrier off, otherwise
1423 * netif_carrier_ok() will return true and cause 'ip link show'
1424 * to report the LOWER_UP flag, even though the link
1425 * notification wasn't even received.
1427 netif_carrier_off(net_dev);
1429 enable_ch_napi(priv);
1431 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1433 netdev_err(net_dev, "dpni_enable() failed\n");
1438 /* If the DPMAC object has already processed the link up
1439 * interrupt, we have to learn the link state ourselves.
1441 err = link_state_update(priv);
1443 netdev_err(net_dev, "Can't update link state\n");
1444 goto link_state_err;
1447 phylink_start(priv->mac->phylink);
1454 disable_ch_napi(priv);
1459 /* Total number of in-flight frames on ingress queues */
1460 static u32 ingress_fq_count(struct dpaa2_eth_priv *priv)
1462 struct dpaa2_eth_fq *fq;
1463 u32 fcnt = 0, bcnt = 0, total = 0;
1466 for (i = 0; i < priv->num_fqs; i++) {
1468 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1470 netdev_warn(priv->net_dev, "query_fq_count failed");
1479 static void wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
1485 pending = ingress_fq_count(priv);
1488 } while (pending && --retries);
1491 #define DPNI_TX_PENDING_VER_MAJOR 7
1492 #define DPNI_TX_PENDING_VER_MINOR 13
1493 static void wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
1495 union dpni_statistics stats;
1499 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1500 DPNI_TX_PENDING_VER_MINOR) < 0)
1504 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1508 if (stats.page_6.tx_pending_frames == 0)
1510 } while (--retries);
1516 static int dpaa2_eth_stop(struct net_device *net_dev)
1518 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1519 int dpni_enabled = 0;
1523 netif_tx_stop_all_queues(net_dev);
1524 netif_carrier_off(net_dev);
1526 phylink_stop(priv->mac->phylink);
1529 /* On dpni_disable(), the MC firmware will:
1530 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1531 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1532 * of all in flight Tx frames is finished (and corresponding Tx conf
1533 * frames are enqueued back to software)
1535 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1536 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1537 * and Tx conf queues are consumed on NAPI poll.
1539 wait_for_egress_fq_empty(priv);
1542 dpni_disable(priv->mc_io, 0, priv->mc_token);
1543 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1545 /* Allow the hardware some slack */
1547 } while (dpni_enabled && --retries);
1549 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1550 /* Must go on and disable NAPI nonetheless, so we don't crash at
1551 * the next "ifconfig up"
1555 wait_for_ingress_fq_empty(priv);
1556 disable_ch_napi(priv);
1558 /* Empty the buffer pool */
1564 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1566 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1567 struct device *dev = net_dev->dev.parent;
1570 err = eth_mac_addr(net_dev, addr);
1572 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1576 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1579 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1586 /** Fill in counters maintained by the GPP driver. These may be different from
1587 * the hardware counters obtained by ethtool.
1589 static void dpaa2_eth_get_stats(struct net_device *net_dev,
1590 struct rtnl_link_stats64 *stats)
1592 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1593 struct rtnl_link_stats64 *percpu_stats;
1595 u64 *netstats = (u64 *)stats;
1597 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1599 for_each_possible_cpu(i) {
1600 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1601 cpustats = (u64 *)percpu_stats;
1602 for (j = 0; j < num; j++)
1603 netstats[j] += cpustats[j];
1607 /* Copy mac unicast addresses from @net_dev to @priv.
1608 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1610 static void add_uc_hw_addr(const struct net_device *net_dev,
1611 struct dpaa2_eth_priv *priv)
1613 struct netdev_hw_addr *ha;
1616 netdev_for_each_uc_addr(ha, net_dev) {
1617 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1620 netdev_warn(priv->net_dev,
1621 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1626 /* Copy mac multicast addresses from @net_dev to @priv
1627 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1629 static void add_mc_hw_addr(const struct net_device *net_dev,
1630 struct dpaa2_eth_priv *priv)
1632 struct netdev_hw_addr *ha;
1635 netdev_for_each_mc_addr(ha, net_dev) {
1636 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1639 netdev_warn(priv->net_dev,
1640 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1645 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1647 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1648 int uc_count = netdev_uc_count(net_dev);
1649 int mc_count = netdev_mc_count(net_dev);
1650 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1651 u32 options = priv->dpni_attrs.options;
1652 u16 mc_token = priv->mc_token;
1653 struct fsl_mc_io *mc_io = priv->mc_io;
1656 /* Basic sanity checks; these probably indicate a misconfiguration */
1657 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1658 netdev_info(net_dev,
1659 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1662 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1663 if (uc_count > max_mac) {
1664 netdev_info(net_dev,
1665 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1669 if (mc_count + uc_count > max_mac) {
1670 netdev_info(net_dev,
1671 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1672 uc_count + mc_count, max_mac);
1673 goto force_mc_promisc;
1676 /* Adjust promisc settings due to flag combinations */
1677 if (net_dev->flags & IFF_PROMISC)
1679 if (net_dev->flags & IFF_ALLMULTI) {
1680 /* First, rebuild unicast filtering table. This should be done
1681 * in promisc mode, in order to avoid frame loss while we
1682 * progressively add entries to the table.
1683 * We don't know whether we had been in promisc already, and
1684 * making an MC call to find out is expensive; so set uc promisc
1687 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1689 netdev_warn(net_dev, "Can't set uc promisc\n");
1691 /* Actual uc table reconstruction. */
1692 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1694 netdev_warn(net_dev, "Can't clear uc filters\n");
1695 add_uc_hw_addr(net_dev, priv);
1697 /* Finally, clear uc promisc and set mc promisc as requested. */
1698 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1700 netdev_warn(net_dev, "Can't clear uc promisc\n");
1701 goto force_mc_promisc;
1704 /* Neither unicast, nor multicast promisc will be on... eventually.
1705 * For now, rebuild mac filtering tables while forcing both of them on.
1707 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1709 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1710 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1712 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1714 /* Actual mac filtering tables reconstruction */
1715 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1717 netdev_warn(net_dev, "Can't clear mac filters\n");
1718 add_mc_hw_addr(net_dev, priv);
1719 add_uc_hw_addr(net_dev, priv);
1721 /* Now we can clear both ucast and mcast promisc, without risking
1722 * to drop legitimate frames anymore.
1724 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1726 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1727 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1729 netdev_warn(net_dev, "Can't clear mcast promisc\n");
1734 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1736 netdev_warn(net_dev, "Can't set ucast promisc\n");
1738 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1740 netdev_warn(net_dev, "Can't set mcast promisc\n");
1743 static int dpaa2_eth_set_features(struct net_device *net_dev,
1744 netdev_features_t features)
1746 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1747 netdev_features_t changed = features ^ net_dev->features;
1751 if (changed & NETIF_F_RXCSUM) {
1752 enable = !!(features & NETIF_F_RXCSUM);
1753 err = set_rx_csum(priv, enable);
1758 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1759 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
1760 err = set_tx_csum(priv, enable);
1768 static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1770 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1771 struct hwtstamp_config config;
1773 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
1776 switch (config.tx_type) {
1777 case HWTSTAMP_TX_OFF:
1778 priv->tx_tstamp = false;
1780 case HWTSTAMP_TX_ON:
1781 priv->tx_tstamp = true;
1787 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
1788 priv->rx_tstamp = false;
1790 priv->rx_tstamp = true;
1791 /* TS is set for all frame types, not only those requested */
1792 config.rx_filter = HWTSTAMP_FILTER_ALL;
1795 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
1799 static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1801 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1803 if (cmd == SIOCSHWTSTAMP)
1804 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
1807 return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
1812 static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
1814 int mfl, linear_mfl;
1816 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1817 linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
1818 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
1820 if (mfl > linear_mfl) {
1821 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
1822 linear_mfl - VLAN_ETH_HLEN);
1829 static int set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
1833 /* We enforce a maximum Rx frame length based on MTU only if we have
1834 * an XDP program attached (in order to avoid Rx S/G frames).
1835 * Otherwise, we accept all incoming frames as long as they are not
1836 * larger than maximum size supported in hardware
1839 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1841 mfl = DPAA2_ETH_MFL;
1843 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
1845 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
1852 static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
1854 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1857 if (!priv->xdp_prog)
1860 if (!xdp_mtu_valid(priv, new_mtu))
1863 err = set_rx_mfl(priv, new_mtu, true);
1872 static int update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
1874 struct dpni_buffer_layout buf_layout = {0};
1877 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
1878 DPNI_QUEUE_RX, &buf_layout);
1880 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
1884 /* Reserve extra headroom for XDP header size changes */
1885 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
1886 (has_xdp ? XDP_PACKET_HEADROOM : 0);
1887 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
1888 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
1889 DPNI_QUEUE_RX, &buf_layout);
1891 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
1898 static int setup_xdp(struct net_device *dev, struct bpf_prog *prog)
1900 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1901 struct dpaa2_eth_channel *ch;
1902 struct bpf_prog *old;
1903 bool up, need_update;
1906 if (prog && !xdp_mtu_valid(priv, dev->mtu))
1910 bpf_prog_add(prog, priv->num_channels);
1912 up = netif_running(dev);
1913 need_update = (!!priv->xdp_prog != !!prog);
1916 dpaa2_eth_stop(dev);
1918 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
1919 * Also, when switching between xdp/non-xdp modes we need to reconfigure
1920 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
1921 * so we are sure no old format buffers will be used from now on.
1924 err = set_rx_mfl(priv, dev->mtu, !!prog);
1927 err = update_rx_buffer_headroom(priv, !!prog);
1932 old = xchg(&priv->xdp_prog, prog);
1936 for (i = 0; i < priv->num_channels; i++) {
1937 ch = priv->channel[i];
1938 old = xchg(&ch->xdp.prog, prog);
1944 err = dpaa2_eth_open(dev);
1953 bpf_prog_sub(prog, priv->num_channels);
1955 dpaa2_eth_open(dev);
1960 static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1962 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1964 switch (xdp->command) {
1965 case XDP_SETUP_PROG:
1966 return setup_xdp(dev, xdp->prog);
1967 case XDP_QUERY_PROG:
1968 xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0;
1977 static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
1978 struct xdp_frame *xdpf,
1979 struct dpaa2_fd *fd)
1981 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1982 struct device *dev = net_dev->dev.parent;
1983 unsigned int needed_headroom;
1984 struct dpaa2_eth_swa *swa;
1985 void *buffer_start, *aligned_start;
1988 /* We require a minimum headroom to be able to transmit the frame.
1989 * Otherwise return an error and let the original net_device handle it
1991 needed_headroom = dpaa2_eth_needed_headroom(priv, NULL);
1992 if (xdpf->headroom < needed_headroom)
1995 /* Setup the FD fields */
1996 memset(fd, 0, sizeof(*fd));
1998 /* Align FD address, if possible */
1999 buffer_start = xdpf->data - needed_headroom;
2000 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2001 DPAA2_ETH_TX_BUF_ALIGN);
2002 if (aligned_start >= xdpf->data - xdpf->headroom)
2003 buffer_start = aligned_start;
2005 swa = (struct dpaa2_eth_swa *)buffer_start;
2006 /* fill in necessary fields here */
2007 swa->type = DPAA2_ETH_SWA_XDP;
2008 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2009 swa->xdp.xdpf = xdpf;
2011 addr = dma_map_single(dev, buffer_start,
2014 if (unlikely(dma_mapping_error(dev, addr)))
2017 dpaa2_fd_set_addr(fd, addr);
2018 dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2019 dpaa2_fd_set_len(fd, xdpf->len);
2020 dpaa2_fd_set_format(fd, dpaa2_fd_single);
2021 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
2026 static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2027 struct xdp_frame **frames, u32 flags)
2029 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2030 struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
2031 struct rtnl_link_stats64 *percpu_stats;
2032 struct dpaa2_eth_fq *fq;
2033 struct dpaa2_fd *fds;
2034 int enqueued, i, err;
2036 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2039 if (!netif_running(net_dev))
2042 fq = &priv->fq[smp_processor_id()];
2043 xdp_redirect_fds = &fq->xdp_redirect_fds;
2044 fds = xdp_redirect_fds->fds;
2046 percpu_stats = this_cpu_ptr(priv->percpu_stats);
2048 /* create a FD for each xdp_frame in the list received */
2049 for (i = 0; i < n; i++) {
2050 err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2054 xdp_redirect_fds->num = i;
2056 /* enqueue all the frame descriptors */
2057 enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
2059 /* update statistics */
2060 percpu_stats->tx_packets += enqueued;
2061 for (i = 0; i < enqueued; i++)
2062 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
2063 for (i = enqueued; i < n; i++)
2064 xdp_return_frame_rx_napi(frames[i]);
2069 static int update_xps(struct dpaa2_eth_priv *priv)
2071 struct net_device *net_dev = priv->net_dev;
2072 struct cpumask xps_mask;
2073 struct dpaa2_eth_fq *fq;
2074 int i, num_queues, netdev_queues;
2077 num_queues = dpaa2_eth_queue_count(priv);
2078 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
2080 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2081 * queues, so only process those
2083 for (i = 0; i < netdev_queues; i++) {
2084 fq = &priv->fq[i % num_queues];
2086 cpumask_clear(&xps_mask);
2087 cpumask_set_cpu(fq->target_cpu, &xps_mask);
2089 err = netif_set_xps_queue(net_dev, &xps_mask, i);
2091 netdev_warn_once(net_dev, "Error setting XPS queue\n");
2099 static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2100 enum tc_setup_type type, void *type_data)
2102 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2103 struct tc_mqprio_qopt *mqprio = type_data;
2104 u8 num_tc, num_queues;
2107 if (type != TC_SETUP_QDISC_MQPRIO)
2110 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2111 num_queues = dpaa2_eth_queue_count(priv);
2112 num_tc = mqprio->num_tc;
2114 if (num_tc == net_dev->num_tc)
2117 if (num_tc > dpaa2_eth_tc_count(priv)) {
2118 netdev_err(net_dev, "Max %d traffic classes supported\n",
2119 dpaa2_eth_tc_count(priv));
2124 netdev_reset_tc(net_dev);
2125 netif_set_real_num_tx_queues(net_dev, num_queues);
2129 netdev_set_num_tc(net_dev, num_tc);
2130 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2132 for (i = 0; i < num_tc; i++)
2133 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2141 static const struct net_device_ops dpaa2_eth_ops = {
2142 .ndo_open = dpaa2_eth_open,
2143 .ndo_start_xmit = dpaa2_eth_tx,
2144 .ndo_stop = dpaa2_eth_stop,
2145 .ndo_set_mac_address = dpaa2_eth_set_addr,
2146 .ndo_get_stats64 = dpaa2_eth_get_stats,
2147 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2148 .ndo_set_features = dpaa2_eth_set_features,
2149 .ndo_do_ioctl = dpaa2_eth_ioctl,
2150 .ndo_change_mtu = dpaa2_eth_change_mtu,
2151 .ndo_bpf = dpaa2_eth_xdp,
2152 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
2153 .ndo_setup_tc = dpaa2_eth_setup_tc,
2156 static void cdan_cb(struct dpaa2_io_notification_ctx *ctx)
2158 struct dpaa2_eth_channel *ch;
2160 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
2162 /* Update NAPI statistics */
2165 napi_schedule_irqoff(&ch->napi);
2168 /* Allocate and configure a DPCON object */
2169 static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv)
2171 struct fsl_mc_device *dpcon;
2172 struct device *dev = priv->net_dev->dev.parent;
2175 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2176 FSL_MC_POOL_DPCON, &dpcon);
2179 err = -EPROBE_DEFER;
2181 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2182 return ERR_PTR(err);
2185 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2187 dev_err(dev, "dpcon_open() failed\n");
2191 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2193 dev_err(dev, "dpcon_reset() failed\n");
2197 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2199 dev_err(dev, "dpcon_enable() failed\n");
2206 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2208 fsl_mc_object_free(dpcon);
2213 static void free_dpcon(struct dpaa2_eth_priv *priv,
2214 struct fsl_mc_device *dpcon)
2216 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2217 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2218 fsl_mc_object_free(dpcon);
2221 static struct dpaa2_eth_channel *
2222 alloc_channel(struct dpaa2_eth_priv *priv)
2224 struct dpaa2_eth_channel *channel;
2225 struct dpcon_attr attr;
2226 struct device *dev = priv->net_dev->dev.parent;
2229 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2233 channel->dpcon = setup_dpcon(priv);
2234 if (IS_ERR_OR_NULL(channel->dpcon)) {
2235 err = PTR_ERR_OR_ZERO(channel->dpcon);
2239 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2242 dev_err(dev, "dpcon_get_attributes() failed\n");
2246 channel->dpcon_id = attr.id;
2247 channel->ch_id = attr.qbman_ch_id;
2248 channel->priv = priv;
2253 free_dpcon(priv, channel->dpcon);
2256 return ERR_PTR(err);
2259 static void free_channel(struct dpaa2_eth_priv *priv,
2260 struct dpaa2_eth_channel *channel)
2262 free_dpcon(priv, channel->dpcon);
2266 /* DPIO setup: allocate and configure QBMan channels, setup core affinity
2267 * and register data availability notifications
2269 static int setup_dpio(struct dpaa2_eth_priv *priv)
2271 struct dpaa2_io_notification_ctx *nctx;
2272 struct dpaa2_eth_channel *channel;
2273 struct dpcon_notification_cfg dpcon_notif_cfg;
2274 struct device *dev = priv->net_dev->dev.parent;
2277 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2278 * many cores as possible, so we need one channel for each core
2279 * (unless there's fewer queues than cores, in which case the extra
2280 * channels would be wasted).
2281 * Allocate one channel per core and register it to the core's
2282 * affine DPIO. If not enough channels are available for all cores
2283 * or if some cores don't have an affine DPIO, there will be no
2284 * ingress frame processing on those cores.
2286 cpumask_clear(&priv->dpio_cpumask);
2287 for_each_online_cpu(i) {
2288 /* Try to allocate a channel */
2289 channel = alloc_channel(priv);
2290 if (IS_ERR_OR_NULL(channel)) {
2291 err = PTR_ERR_OR_ZERO(channel);
2292 if (err != -EPROBE_DEFER)
2294 "No affine channel for cpu %d and above\n", i);
2298 priv->channel[priv->num_channels] = channel;
2300 nctx = &channel->nctx;
2303 nctx->id = channel->ch_id;
2304 nctx->desired_cpu = i;
2306 /* Register the new context */
2307 channel->dpio = dpaa2_io_service_select(i);
2308 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
2310 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
2311 /* If no affine DPIO for this core, there's probably
2312 * none available for next cores either. Signal we want
2313 * to retry later, in case the DPIO devices weren't
2316 err = -EPROBE_DEFER;
2317 goto err_service_reg;
2320 /* Register DPCON notification with MC */
2321 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2322 dpcon_notif_cfg.priority = 0;
2323 dpcon_notif_cfg.user_ctx = nctx->qman64;
2324 err = dpcon_set_notification(priv->mc_io, 0,
2325 channel->dpcon->mc_handle,
2328 dev_err(dev, "dpcon_set_notification failed()\n");
2332 /* If we managed to allocate a channel and also found an affine
2333 * DPIO for this core, add it to the final mask
2335 cpumask_set_cpu(i, &priv->dpio_cpumask);
2336 priv->num_channels++;
2338 /* Stop if we already have enough channels to accommodate all
2339 * RX and TX conf queues
2341 if (priv->num_channels == priv->dpni_attrs.num_queues)
2348 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
2350 free_channel(priv, channel);
2352 if (err == -EPROBE_DEFER) {
2353 for (i = 0; i < priv->num_channels; i++) {
2354 channel = priv->channel[i];
2355 nctx = &channel->nctx;
2356 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
2357 free_channel(priv, channel);
2359 priv->num_channels = 0;
2363 if (cpumask_empty(&priv->dpio_cpumask)) {
2364 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
2368 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2369 cpumask_pr_args(&priv->dpio_cpumask));
2374 static void free_dpio(struct dpaa2_eth_priv *priv)
2376 struct device *dev = priv->net_dev->dev.parent;
2377 struct dpaa2_eth_channel *ch;
2380 /* deregister CDAN notifications and free channels */
2381 for (i = 0; i < priv->num_channels; i++) {
2382 ch = priv->channel[i];
2383 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
2384 free_channel(priv, ch);
2388 static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv,
2391 struct device *dev = priv->net_dev->dev.parent;
2394 for (i = 0; i < priv->num_channels; i++)
2395 if (priv->channel[i]->nctx.desired_cpu == cpu)
2396 return priv->channel[i];
2398 /* We should never get here. Issue a warning and return
2399 * the first channel, because it's still better than nothing
2401 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2403 return priv->channel[0];
2406 static void set_fq_affinity(struct dpaa2_eth_priv *priv)
2408 struct device *dev = priv->net_dev->dev.parent;
2409 struct dpaa2_eth_fq *fq;
2410 int rx_cpu, txc_cpu;
2413 /* For each FQ, pick one channel/CPU to deliver frames to.
2414 * This may well change at runtime, either through irqbalance or
2415 * through direct user intervention.
2417 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2419 for (i = 0; i < priv->num_fqs; i++) {
2423 fq->target_cpu = rx_cpu;
2424 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2425 if (rx_cpu >= nr_cpu_ids)
2426 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2428 case DPAA2_TX_CONF_FQ:
2429 fq->target_cpu = txc_cpu;
2430 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2431 if (txc_cpu >= nr_cpu_ids)
2432 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2435 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2437 fq->channel = get_affine_channel(priv, fq->target_cpu);
2443 static void setup_fqs(struct dpaa2_eth_priv *priv)
2447 /* We have one TxConf FQ per Tx flow.
2448 * The number of Tx and Rx queues is the same.
2449 * Tx queues come first in the fq array.
2451 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2452 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2453 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2454 priv->fq[priv->num_fqs++].flowid = (u16)i;
2457 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2458 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2459 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2460 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2461 priv->fq[priv->num_fqs].tc = (u8)j;
2462 priv->fq[priv->num_fqs++].flowid = (u16)i;
2466 /* For each FQ, decide on which core to process incoming frames */
2467 set_fq_affinity(priv);
2470 /* Allocate and configure one buffer pool for each interface */
2471 static int setup_dpbp(struct dpaa2_eth_priv *priv)
2474 struct fsl_mc_device *dpbp_dev;
2475 struct device *dev = priv->net_dev->dev.parent;
2476 struct dpbp_attr dpbp_attrs;
2478 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2482 err = -EPROBE_DEFER;
2484 dev_err(dev, "DPBP device allocation failed\n");
2488 priv->dpbp_dev = dpbp_dev;
2490 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2491 &dpbp_dev->mc_handle);
2493 dev_err(dev, "dpbp_open() failed\n");
2497 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2499 dev_err(dev, "dpbp_reset() failed\n");
2503 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2505 dev_err(dev, "dpbp_enable() failed\n");
2509 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
2512 dev_err(dev, "dpbp_get_attributes() failed\n");
2515 priv->bpid = dpbp_attrs.bpid;
2520 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2523 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2525 fsl_mc_object_free(dpbp_dev);
2530 static void free_dpbp(struct dpaa2_eth_priv *priv)
2533 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2534 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2535 fsl_mc_object_free(priv->dpbp_dev);
2538 static int set_buffer_layout(struct dpaa2_eth_priv *priv)
2540 struct device *dev = priv->net_dev->dev.parent;
2541 struct dpni_buffer_layout buf_layout = {0};
2545 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2546 * version, this number is not always provided correctly on rev1.
2547 * We need to check for both alternatives in this situation.
2549 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2550 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
2551 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
2553 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
2555 /* We need to ensure that the buffer size seen by WRIOP is a multiple
2556 * of 64 or 256 bytes depending on the WRIOP version.
2558 priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
2561 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
2562 buf_layout.pass_timestamp = true;
2563 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2564 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
2565 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2566 DPNI_QUEUE_TX, &buf_layout);
2568 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2572 /* tx-confirm buffer */
2573 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
2574 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2575 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2577 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2581 /* Now that we've set our tx buffer layout, retrieve the minimum
2582 * required tx data offset.
2584 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2585 &priv->tx_data_offset);
2587 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2591 if ((priv->tx_data_offset % 64) != 0)
2592 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2593 priv->tx_data_offset);
2596 buf_layout.pass_frame_status = true;
2597 buf_layout.pass_parser_result = true;
2598 buf_layout.data_align = rx_buf_align;
2599 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2600 buf_layout.private_data_size = 0;
2601 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2602 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2603 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
2604 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2605 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
2606 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2607 DPNI_QUEUE_RX, &buf_layout);
2609 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2616 #define DPNI_ENQUEUE_FQID_VER_MAJOR 7
2617 #define DPNI_ENQUEUE_FQID_VER_MINOR 9
2619 static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2620 struct dpaa2_eth_fq *fq,
2621 struct dpaa2_fd *fd, u8 prio,
2622 u32 num_frames __always_unused,
2623 int *frames_enqueued)
2627 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
2628 priv->tx_qdid, prio,
2630 if (!err && frames_enqueued)
2631 *frames_enqueued = 1;
2635 static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
2636 struct dpaa2_eth_fq *fq,
2637 struct dpaa2_fd *fd,
2638 u8 prio, u32 num_frames,
2639 int *frames_enqueued)
2643 err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
2650 if (frames_enqueued)
2651 *frames_enqueued = err;
2655 static void set_enqueue_mode(struct dpaa2_eth_priv *priv)
2657 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2658 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2659 priv->enqueue = dpaa2_eth_enqueue_qd;
2661 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
2664 static int set_pause(struct dpaa2_eth_priv *priv)
2666 struct device *dev = priv->net_dev->dev.parent;
2667 struct dpni_link_cfg link_cfg = {0};
2670 /* Get the default link options so we don't override other flags */
2671 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2673 dev_err(dev, "dpni_get_link_cfg() failed\n");
2677 /* By default, enable both Rx and Tx pause frames */
2678 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
2679 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
2680 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2682 dev_err(dev, "dpni_set_link_cfg() failed\n");
2686 priv->link_state.options = link_cfg.options;
2691 static void update_tx_fqids(struct dpaa2_eth_priv *priv)
2693 struct dpni_queue_id qid = {0};
2694 struct dpaa2_eth_fq *fq;
2695 struct dpni_queue queue;
2698 /* We only use Tx FQIDs for FQID-based enqueue, so check
2699 * if DPNI version supports it before updating FQIDs
2701 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2702 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2705 for (i = 0; i < priv->num_fqs; i++) {
2707 if (fq->type != DPAA2_TX_CONF_FQ)
2709 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2710 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2711 DPNI_QUEUE_TX, j, fq->flowid,
2716 fq->tx_fqid[j] = qid.fqid;
2717 if (fq->tx_fqid[j] == 0)
2722 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
2727 netdev_info(priv->net_dev,
2728 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
2729 priv->enqueue = dpaa2_eth_enqueue_qd;
2732 /* Configure ingress classification based on VLAN PCP */
2733 static int set_vlan_qos(struct dpaa2_eth_priv *priv)
2735 struct device *dev = priv->net_dev->dev.parent;
2736 struct dpkg_profile_cfg kg_cfg = {0};
2737 struct dpni_qos_tbl_cfg qos_cfg = {0};
2738 struct dpni_rule_cfg key_params;
2739 void *dma_mem, *key, *mask;
2740 u8 key_size = 2; /* VLAN TCI field */
2743 /* VLAN-based classification only makes sense if we have multiple
2745 * Also, we need to extract just the 3-bit PCP field from the VLAN
2746 * header and we can only do that by using a mask
2748 if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
2749 dev_dbg(dev, "VLAN-based QoS classification not supported\n");
2753 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
2757 kg_cfg.num_extracts = 1;
2758 kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
2759 kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
2760 kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
2761 kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
2763 err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
2765 dev_err(dev, "dpni_prepare_key_cfg failed\n");
2770 qos_cfg.default_tc = 0;
2771 qos_cfg.discard_on_miss = 0;
2772 qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
2773 DPAA2_CLASSIFIER_DMA_SIZE,
2775 if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
2776 dev_err(dev, "QoS table DMA mapping failed\n");
2781 err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
2783 dev_err(dev, "dpni_set_qos_table failed\n");
2787 /* Add QoS table entries */
2788 key = kzalloc(key_size * 2, GFP_KERNEL);
2793 mask = key + key_size;
2794 *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
2796 key_params.key_iova = dma_map_single(dev, key, key_size * 2,
2798 if (dma_mapping_error(dev, key_params.key_iova)) {
2799 dev_err(dev, "Qos table entry DMA mapping failed\n");
2804 key_params.mask_iova = key_params.key_iova + key_size;
2805 key_params.key_size = key_size;
2807 /* We add rules for PCP-based distribution starting with highest
2808 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
2809 * classes to accommodate all priority levels, the lowest ones end up
2810 * on TC 0 which was configured as default
2812 for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
2813 *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
2814 dma_sync_single_for_device(dev, key_params.key_iova,
2815 key_size * 2, DMA_TO_DEVICE);
2817 err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
2820 dev_err(dev, "dpni_add_qos_entry failed\n");
2821 dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
2826 priv->vlan_cls_enabled = true;
2828 /* Table and key memory is not persistent, clean everything up after
2829 * configuration is finished
2832 dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
2836 dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
2844 /* Configure the DPNI object this interface is associated with */
2845 static int setup_dpni(struct fsl_mc_device *ls_dev)
2847 struct device *dev = &ls_dev->dev;
2848 struct dpaa2_eth_priv *priv;
2849 struct net_device *net_dev;
2852 net_dev = dev_get_drvdata(dev);
2853 priv = netdev_priv(net_dev);
2855 /* get a handle for the DPNI object */
2856 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
2858 dev_err(dev, "dpni_open() failed\n");
2862 /* Check if we can work with this DPNI object */
2863 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
2864 &priv->dpni_ver_minor);
2866 dev_err(dev, "dpni_get_api_version() failed\n");
2869 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
2870 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
2871 priv->dpni_ver_major, priv->dpni_ver_minor,
2872 DPNI_VER_MAJOR, DPNI_VER_MINOR);
2877 ls_dev->mc_io = priv->mc_io;
2878 ls_dev->mc_handle = priv->mc_token;
2880 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2882 dev_err(dev, "dpni_reset() failed\n");
2886 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
2889 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
2893 err = set_buffer_layout(priv);
2897 set_enqueue_mode(priv);
2899 /* Enable pause frame support */
2900 if (dpaa2_eth_has_pause_support(priv)) {
2901 err = set_pause(priv);
2906 err = set_vlan_qos(priv);
2907 if (err && err != -EOPNOTSUPP)
2910 priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
2911 sizeof(struct dpaa2_eth_cls_rule),
2913 if (!priv->cls_rules) {
2921 dpni_close(priv->mc_io, 0, priv->mc_token);
2926 static void free_dpni(struct dpaa2_eth_priv *priv)
2930 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2932 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
2935 dpni_close(priv->mc_io, 0, priv->mc_token);
2938 static int setup_rx_flow(struct dpaa2_eth_priv *priv,
2939 struct dpaa2_eth_fq *fq)
2941 struct device *dev = priv->net_dev->dev.parent;
2942 struct dpni_queue queue;
2943 struct dpni_queue_id qid;
2946 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2947 DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
2949 dev_err(dev, "dpni_get_queue(RX) failed\n");
2953 fq->fqid = qid.fqid;
2955 queue.destination.id = fq->channel->dpcon_id;
2956 queue.destination.type = DPNI_DEST_DPCON;
2957 queue.destination.priority = 1;
2958 queue.user_context = (u64)(uintptr_t)fq;
2959 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2960 DPNI_QUEUE_RX, fq->tc, fq->flowid,
2961 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2964 dev_err(dev, "dpni_set_queue(RX) failed\n");
2969 /* only once for each channel */
2973 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
2976 dev_err(dev, "xdp_rxq_info_reg failed\n");
2980 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
2981 MEM_TYPE_PAGE_ORDER0, NULL);
2983 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
2990 static int setup_tx_flow(struct dpaa2_eth_priv *priv,
2991 struct dpaa2_eth_fq *fq)
2993 struct device *dev = priv->net_dev->dev.parent;
2994 struct dpni_queue queue;
2995 struct dpni_queue_id qid;
2998 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
2999 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3000 DPNI_QUEUE_TX, i, fq->flowid,
3003 dev_err(dev, "dpni_get_queue(TX) failed\n");
3006 fq->tx_fqid[i] = qid.fqid;
3009 /* All Tx queues belonging to the same flowid have the same qdbin */
3010 fq->tx_qdbin = qid.qdbin;
3012 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3013 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3016 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3020 fq->fqid = qid.fqid;
3022 queue.destination.id = fq->channel->dpcon_id;
3023 queue.destination.type = DPNI_DEST_DPCON;
3024 queue.destination.priority = 0;
3025 queue.user_context = (u64)(uintptr_t)fq;
3026 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3027 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3028 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3031 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3038 /* Supported header fields for Rx hash distribution key */
3039 static const struct dpaa2_eth_dist_fields dist_fields[] = {
3042 .rxnfc_field = RXH_L2DA,
3043 .cls_prot = NET_PROT_ETH,
3044 .cls_field = NH_FLD_ETH_DA,
3045 .id = DPAA2_ETH_DIST_ETHDST,
3048 .cls_prot = NET_PROT_ETH,
3049 .cls_field = NH_FLD_ETH_SA,
3050 .id = DPAA2_ETH_DIST_ETHSRC,
3053 /* This is the last ethertype field parsed:
3054 * depending on frame format, it can be the MAC ethertype
3055 * or the VLAN etype.
3057 .cls_prot = NET_PROT_ETH,
3058 .cls_field = NH_FLD_ETH_TYPE,
3059 .id = DPAA2_ETH_DIST_ETHTYPE,
3063 .rxnfc_field = RXH_VLAN,
3064 .cls_prot = NET_PROT_VLAN,
3065 .cls_field = NH_FLD_VLAN_TCI,
3066 .id = DPAA2_ETH_DIST_VLAN,
3070 .rxnfc_field = RXH_IP_SRC,
3071 .cls_prot = NET_PROT_IP,
3072 .cls_field = NH_FLD_IP_SRC,
3073 .id = DPAA2_ETH_DIST_IPSRC,
3076 .rxnfc_field = RXH_IP_DST,
3077 .cls_prot = NET_PROT_IP,
3078 .cls_field = NH_FLD_IP_DST,
3079 .id = DPAA2_ETH_DIST_IPDST,
3082 .rxnfc_field = RXH_L3_PROTO,
3083 .cls_prot = NET_PROT_IP,
3084 .cls_field = NH_FLD_IP_PROTO,
3085 .id = DPAA2_ETH_DIST_IPPROTO,
3088 /* Using UDP ports, this is functionally equivalent to raw
3089 * byte pairs from L4 header.
3091 .rxnfc_field = RXH_L4_B_0_1,
3092 .cls_prot = NET_PROT_UDP,
3093 .cls_field = NH_FLD_UDP_PORT_SRC,
3094 .id = DPAA2_ETH_DIST_L4SRC,
3097 .rxnfc_field = RXH_L4_B_2_3,
3098 .cls_prot = NET_PROT_UDP,
3099 .cls_field = NH_FLD_UDP_PORT_DST,
3100 .id = DPAA2_ETH_DIST_L4DST,
3105 /* Configure the Rx hash key using the legacy API */
3106 static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3108 struct device *dev = priv->net_dev->dev.parent;
3109 struct dpni_rx_tc_dist_cfg dist_cfg;
3112 memset(&dist_cfg, 0, sizeof(dist_cfg));
3114 dist_cfg.key_cfg_iova = key;
3115 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3116 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3118 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3119 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3122 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3130 /* Configure the Rx hash key using the new API */
3131 static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3133 struct device *dev = priv->net_dev->dev.parent;
3134 struct dpni_rx_dist_cfg dist_cfg;
3137 memset(&dist_cfg, 0, sizeof(dist_cfg));
3139 dist_cfg.key_cfg_iova = key;
3140 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3141 dist_cfg.enable = 1;
3143 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3145 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3148 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3156 /* Configure the Rx flow classification key */
3157 static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3159 struct device *dev = priv->net_dev->dev.parent;
3160 struct dpni_rx_dist_cfg dist_cfg;
3163 memset(&dist_cfg, 0, sizeof(dist_cfg));
3165 dist_cfg.key_cfg_iova = key;
3166 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3167 dist_cfg.enable = 1;
3169 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3171 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3174 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3182 /* Size of the Rx flow classification key */
3183 int dpaa2_eth_cls_key_size(u64 fields)
3187 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3188 if (!(fields & dist_fields[i].id))
3190 size += dist_fields[i].size;
3196 /* Offset of header field in Rx classification key */
3197 int dpaa2_eth_cls_fld_off(int prot, int field)
3201 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3202 if (dist_fields[i].cls_prot == prot &&
3203 dist_fields[i].cls_field == field)
3205 off += dist_fields[i].size;
3208 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3212 /* Prune unused fields from the classification rule.
3213 * Used when masking is not supported
3215 void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3217 int off = 0, new_off = 0;
3220 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3221 size = dist_fields[i].size;
3222 if (dist_fields[i].id & fields) {
3223 memcpy(key_mem + new_off, key_mem + off, size);
3230 /* Set Rx distribution (hash or flow classification) key
3231 * flags is a combination of RXH_ bits
3233 static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
3234 enum dpaa2_eth_rx_dist type, u64 flags)
3236 struct device *dev = net_dev->dev.parent;
3237 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3238 struct dpkg_profile_cfg cls_cfg;
3239 u32 rx_hash_fields = 0;
3240 dma_addr_t key_iova;
3245 memset(&cls_cfg, 0, sizeof(cls_cfg));
3247 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3248 struct dpkg_extract *key =
3249 &cls_cfg.extracts[cls_cfg.num_extracts];
3251 /* For both Rx hashing and classification keys
3252 * we set only the selected fields.
3254 if (!(flags & dist_fields[i].id))
3256 if (type == DPAA2_ETH_RX_DIST_HASH)
3257 rx_hash_fields |= dist_fields[i].rxnfc_field;
3259 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
3260 dev_err(dev, "error adding key extraction rule, too many rules?\n");
3264 key->type = DPKG_EXTRACT_FROM_HDR;
3265 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
3266 key->extract.from_hdr.type = DPKG_FULL_FIELD;
3267 key->extract.from_hdr.field = dist_fields[i].cls_field;
3268 cls_cfg.num_extracts++;
3271 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3275 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3277 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
3281 /* Prepare for setting the rx dist */
3282 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3284 if (dma_mapping_error(dev, key_iova)) {
3285 dev_err(dev, "DMA mapping failed\n");
3290 if (type == DPAA2_ETH_RX_DIST_HASH) {
3291 if (dpaa2_eth_has_legacy_dist(priv))
3292 err = config_legacy_hash_key(priv, key_iova);
3294 err = config_hash_key(priv, key_iova);
3296 err = config_cls_key(priv, key_iova);
3299 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3301 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
3302 priv->rx_hash_fields = rx_hash_fields;
3309 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3311 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3315 if (!dpaa2_eth_hash_enabled(priv))
3318 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3319 if (dist_fields[i].rxnfc_field & flags)
3320 key |= dist_fields[i].id;
3322 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
3325 int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3327 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3330 static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
3332 struct device *dev = priv->net_dev->dev.parent;
3335 /* Check if we actually support Rx flow classification */
3336 if (dpaa2_eth_has_legacy_dist(priv)) {
3337 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3341 if (!dpaa2_eth_fs_enabled(priv)) {
3342 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3346 if (!dpaa2_eth_hash_enabled(priv)) {
3347 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3351 /* If there is no support for masking in the classification table,
3352 * we don't set a default key, as it will depend on the rules
3353 * added by the user at runtime.
3355 if (!dpaa2_eth_fs_mask_enabled(priv))
3358 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
3363 priv->rx_cls_enabled = 1;
3368 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3369 * frame queues and channels
3371 static int bind_dpni(struct dpaa2_eth_priv *priv)
3373 struct net_device *net_dev = priv->net_dev;
3374 struct device *dev = net_dev->dev.parent;
3375 struct dpni_pools_cfg pools_params;
3376 struct dpni_error_cfg err_cfg;
3380 pools_params.num_dpbp = 1;
3381 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3382 pools_params.pools[0].backup_pool = 0;
3383 pools_params.pools[0].buffer_size = priv->rx_buf_size;
3384 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3386 dev_err(dev, "dpni_set_pools() failed\n");
3390 /* have the interface implicitly distribute traffic based on
3391 * the default hash key
3393 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
3394 if (err && err != -EOPNOTSUPP)
3395 dev_err(dev, "Failed to configure hashing\n");
3397 /* Configure the flow classification key; it includes all
3398 * supported header fields and cannot be modified at runtime
3400 err = dpaa2_eth_set_default_cls(priv);
3401 if (err && err != -EOPNOTSUPP)
3402 dev_err(dev, "Failed to configure Rx classification key\n");
3404 /* Configure handling of error frames */
3405 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
3406 err_cfg.set_frame_annotation = 1;
3407 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3408 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3411 dev_err(dev, "dpni_set_errors_behavior failed\n");
3415 /* Configure Rx and Tx conf queues to generate CDANs */
3416 for (i = 0; i < priv->num_fqs; i++) {
3417 switch (priv->fq[i].type) {
3419 err = setup_rx_flow(priv, &priv->fq[i]);
3421 case DPAA2_TX_CONF_FQ:
3422 err = setup_tx_flow(priv, &priv->fq[i]);
3425 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3432 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3433 DPNI_QUEUE_TX, &priv->tx_qdid);
3435 dev_err(dev, "dpni_get_qdid() failed\n");
3442 /* Allocate rings for storing incoming frame descriptors */
3443 static int alloc_rings(struct dpaa2_eth_priv *priv)
3445 struct net_device *net_dev = priv->net_dev;
3446 struct device *dev = net_dev->dev.parent;
3449 for (i = 0; i < priv->num_channels; i++) {
3450 priv->channel[i]->store =
3451 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3452 if (!priv->channel[i]->store) {
3453 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3461 for (i = 0; i < priv->num_channels; i++) {
3462 if (!priv->channel[i]->store)
3464 dpaa2_io_store_destroy(priv->channel[i]->store);
3470 static void free_rings(struct dpaa2_eth_priv *priv)
3474 for (i = 0; i < priv->num_channels; i++)
3475 dpaa2_io_store_destroy(priv->channel[i]->store);
3478 static int set_mac_addr(struct dpaa2_eth_priv *priv)
3480 struct net_device *net_dev = priv->net_dev;
3481 struct device *dev = net_dev->dev.parent;
3482 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
3485 /* Get firmware address, if any */
3486 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3488 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3492 /* Get DPNI attributes address, if any */
3493 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3496 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
3500 /* First check if firmware has any address configured by bootloader */
3501 if (!is_zero_ether_addr(mac_addr)) {
3502 /* If the DPMAC addr != DPNI addr, update it */
3503 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
3504 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
3508 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3512 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
3513 } else if (is_zero_ether_addr(dpni_mac_addr)) {
3514 /* No MAC address configured, fill in net_dev->dev_addr
3517 eth_hw_addr_random(net_dev);
3518 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
3520 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3523 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3527 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
3528 * practical purposes, this will be our "permanent" mac address,
3529 * at least until the next reboot. This move will also permit
3530 * register_netdevice() to properly fill up net_dev->perm_addr.
3532 net_dev->addr_assign_type = NET_ADDR_PERM;
3534 /* NET_ADDR_PERM is default, all we have to do is
3535 * fill in the device addr.
3537 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
3543 static int netdev_init(struct net_device *net_dev)
3545 struct device *dev = net_dev->dev.parent;
3546 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3547 u32 options = priv->dpni_attrs.options;
3548 u64 supported = 0, not_supported = 0;
3549 u8 bcast_addr[ETH_ALEN];
3553 net_dev->netdev_ops = &dpaa2_eth_ops;
3554 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
3556 err = set_mac_addr(priv);
3560 /* Explicitly add the broadcast address to the MAC filtering table */
3561 eth_broadcast_addr(bcast_addr);
3562 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3564 dev_err(dev, "dpni_add_mac_addr() failed\n");
3568 /* Set MTU upper limit; lower limit is 68B (default value) */
3569 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
3570 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
3573 dev_err(dev, "dpni_set_max_frame_length() failed\n");
3577 /* Set actual number of queues in the net device */
3578 num_queues = dpaa2_eth_queue_count(priv);
3579 err = netif_set_real_num_tx_queues(net_dev, num_queues);
3581 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
3584 err = netif_set_real_num_rx_queues(net_dev, num_queues);
3586 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
3590 /* Capabilities listing */
3591 supported |= IFF_LIVE_ADDR_CHANGE;
3593 if (options & DPNI_OPT_NO_MAC_FILTER)
3594 not_supported |= IFF_UNICAST_FLT;
3596 supported |= IFF_UNICAST_FLT;
3598 net_dev->priv_flags |= supported;
3599 net_dev->priv_flags &= ~not_supported;
3602 net_dev->features = NETIF_F_RXCSUM |
3603 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3604 NETIF_F_SG | NETIF_F_HIGHDMA |
3606 net_dev->hw_features = net_dev->features;
3611 static int poll_link_state(void *arg)
3613 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
3616 while (!kthread_should_stop()) {
3617 err = link_state_update(priv);
3621 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
3627 static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
3629 struct fsl_mc_device *dpni_dev, *dpmac_dev;
3630 struct dpaa2_mac *mac;
3633 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
3634 dpmac_dev = fsl_mc_get_endpoint(dpni_dev);
3635 if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
3638 if (dpaa2_mac_is_type_fixed(dpmac_dev, priv->mc_io))
3641 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
3645 mac->mc_dev = dpmac_dev;
3646 mac->mc_io = priv->mc_io;
3647 mac->net_dev = priv->net_dev;
3649 err = dpaa2_mac_connect(mac);
3651 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint\n");
3660 static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
3665 dpaa2_mac_disconnect(priv->mac);
3670 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
3673 struct device *dev = (struct device *)arg;
3674 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
3675 struct net_device *net_dev = dev_get_drvdata(dev);
3676 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3679 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
3680 DPNI_IRQ_INDEX, &status);
3681 if (unlikely(err)) {
3682 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
3686 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
3687 link_state_update(netdev_priv(net_dev));
3689 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
3690 set_mac_addr(netdev_priv(net_dev));
3691 update_tx_fqids(priv);
3695 dpaa2_eth_disconnect_mac(priv);
3697 dpaa2_eth_connect_mac(priv);
3704 static int setup_irqs(struct fsl_mc_device *ls_dev)
3707 struct fsl_mc_device_irq *irq;
3709 err = fsl_mc_allocate_irqs(ls_dev);
3711 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
3715 irq = ls_dev->irqs[0];
3716 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
3717 NULL, dpni_irq0_handler_thread,
3718 IRQF_NO_SUSPEND | IRQF_ONESHOT,
3719 dev_name(&ls_dev->dev), &ls_dev->dev);
3721 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
3725 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
3726 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
3727 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
3729 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
3733 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
3736 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
3743 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
3745 fsl_mc_free_irqs(ls_dev);
3750 static void add_ch_napi(struct dpaa2_eth_priv *priv)
3753 struct dpaa2_eth_channel *ch;
3755 for (i = 0; i < priv->num_channels; i++) {
3756 ch = priv->channel[i];
3757 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
3758 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
3763 static void del_ch_napi(struct dpaa2_eth_priv *priv)
3766 struct dpaa2_eth_channel *ch;
3768 for (i = 0; i < priv->num_channels; i++) {
3769 ch = priv->channel[i];
3770 netif_napi_del(&ch->napi);
3774 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
3777 struct net_device *net_dev = NULL;
3778 struct dpaa2_eth_priv *priv = NULL;
3781 dev = &dpni_dev->dev;
3784 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
3786 dev_err(dev, "alloc_etherdev_mq() failed\n");
3790 SET_NETDEV_DEV(net_dev, dev);
3791 dev_set_drvdata(dev, net_dev);
3793 priv = netdev_priv(net_dev);
3794 priv->net_dev = net_dev;
3796 priv->iommu_domain = iommu_get_domain_for_dev(dev);
3798 /* Obtain a MC portal */
3799 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3803 err = -EPROBE_DEFER;
3805 dev_err(dev, "MC portal allocation failed\n");
3806 goto err_portal_alloc;
3809 /* MC objects initialization and configuration */
3810 err = setup_dpni(dpni_dev);
3812 goto err_dpni_setup;
3814 err = setup_dpio(priv);
3816 goto err_dpio_setup;
3820 err = setup_dpbp(priv);
3822 goto err_dpbp_setup;
3824 err = bind_dpni(priv);
3828 /* Add a NAPI context for each channel */
3831 /* Percpu statistics */
3832 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
3833 if (!priv->percpu_stats) {
3834 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
3836 goto err_alloc_percpu_stats;
3838 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
3839 if (!priv->percpu_extras) {
3840 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
3842 goto err_alloc_percpu_extras;
3845 err = netdev_init(net_dev);
3847 goto err_netdev_init;
3849 /* Configure checksum offload based on current interface flags */
3850 err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
3854 err = set_tx_csum(priv, !!(net_dev->features &
3855 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
3859 err = alloc_rings(priv);
3861 goto err_alloc_rings;
3863 #ifdef CONFIG_FSL_DPAA2_ETH_DCB
3864 if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
3865 priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
3866 net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
3868 dev_dbg(dev, "PFC not supported\n");
3872 err = setup_irqs(dpni_dev);
3874 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
3875 priv->poll_thread = kthread_run(poll_link_state, priv,
3876 "%s_poll_link", net_dev->name);
3877 if (IS_ERR(priv->poll_thread)) {
3878 dev_err(dev, "Error starting polling thread\n");
3879 goto err_poll_thread;
3881 priv->do_link_poll = true;
3884 err = dpaa2_eth_connect_mac(priv);
3886 goto err_connect_mac;
3888 err = register_netdev(net_dev);
3890 dev_err(dev, "register_netdev() failed\n");
3891 goto err_netdev_reg;
3894 #ifdef CONFIG_DEBUG_FS
3895 dpaa2_dbg_add(priv);
3898 dev_info(dev, "Probed interface %s\n", net_dev->name);
3902 dpaa2_eth_disconnect_mac(priv);
3904 if (priv->do_link_poll)
3905 kthread_stop(priv->poll_thread);
3907 fsl_mc_free_irqs(dpni_dev);
3913 free_percpu(priv->percpu_extras);
3914 err_alloc_percpu_extras:
3915 free_percpu(priv->percpu_stats);
3916 err_alloc_percpu_stats:
3925 fsl_mc_portal_free(priv->mc_io);
3927 dev_set_drvdata(dev, NULL);
3928 free_netdev(net_dev);
3933 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
3936 struct net_device *net_dev;
3937 struct dpaa2_eth_priv *priv;
3940 net_dev = dev_get_drvdata(dev);
3941 priv = netdev_priv(net_dev);
3943 #ifdef CONFIG_DEBUG_FS
3944 dpaa2_dbg_remove(priv);
3947 dpaa2_eth_disconnect_mac(priv);
3950 unregister_netdev(net_dev);
3952 if (priv->do_link_poll)
3953 kthread_stop(priv->poll_thread);
3955 fsl_mc_free_irqs(ls_dev);
3958 free_percpu(priv->percpu_stats);
3959 free_percpu(priv->percpu_extras);
3966 fsl_mc_portal_free(priv->mc_io);
3968 free_netdev(net_dev);
3970 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
3975 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
3977 .vendor = FSL_MC_VENDOR_FREESCALE,
3982 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
3984 static struct fsl_mc_driver dpaa2_eth_driver = {
3986 .name = KBUILD_MODNAME,
3987 .owner = THIS_MODULE,
3989 .probe = dpaa2_eth_probe,
3990 .remove = dpaa2_eth_remove,
3991 .match_id_table = dpaa2_eth_match_id_table
3994 static int __init dpaa2_eth_driver_init(void)
3998 dpaa2_eth_dbg_init();
3999 err = fsl_mc_driver_register(&dpaa2_eth_driver);
4001 dpaa2_eth_dbg_exit();
4008 static void __exit dpaa2_eth_driver_exit(void)
4010 dpaa2_eth_dbg_exit();
4011 fsl_mc_driver_unregister(&dpaa2_eth_driver);
4014 module_init(dpaa2_eth_driver_init);
4015 module_exit(dpaa2_eth_driver_exit);