1 /* Copyright 2014-2016 Freescale Semiconductor Inc.
2 * Copyright 2016-2017 NXP
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of Freescale Semiconductor nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
16 * ALTERNATIVELY, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") as published by the Free Software
18 * Foundation, either version 2 of that License or (at your option) any
21 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/etherdevice.h>
36 #include <linux/of_net.h>
37 #include <linux/interrupt.h>
38 #include <linux/msi.h>
39 #include <linux/kthread.h>
41 #include "../../fsl-mc/include/mc.h"
42 #include "../../fsl-mc/include/mc-sys.h"
43 #include "dpaa2-eth.h"
45 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
46 * using trace events only need to #include <trace/events/sched.h>
48 #define CREATE_TRACE_POINTS
49 #include "dpaa2-eth-trace.h"
51 MODULE_LICENSE("Dual BSD/GPL");
52 MODULE_AUTHOR("Freescale Semiconductor, Inc");
53 MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
55 const char dpaa2_eth_drv_version[] = "0.1";
57 static void validate_rx_csum(struct dpaa2_eth_priv *priv,
61 skb_checksum_none_assert(skb);
63 /* HW checksum validation is disabled, nothing to do here */
64 if (!(priv->net_dev->features & NETIF_F_RXCSUM))
67 /* Read checksum validation bits */
68 if (!((fd_status & DPAA2_FAS_L3CV) &&
69 (fd_status & DPAA2_FAS_L4CV)))
72 /* Inform the stack there's no need to compute L3/L4 csum anymore */
73 skb->ip_summed = CHECKSUM_UNNECESSARY;
76 /* Free a received FD.
77 * Not to be used for Tx conf FDs or on any other paths.
79 static void free_rx_fd(struct dpaa2_eth_priv *priv,
80 const struct dpaa2_fd *fd,
83 struct device *dev = priv->net_dev->dev.parent;
84 dma_addr_t addr = dpaa2_fd_get_addr(fd);
85 u8 fd_format = dpaa2_fd_get_format(fd);
86 struct dpaa2_sg_entry *sgt;
90 /* If single buffer frame, just free the data buffer */
91 if (fd_format == dpaa2_fd_single)
93 else if (fd_format != dpaa2_fd_sg)
94 /* We don't support any other format */
97 /* For S/G frames, we first need to free all SG entries */
98 sgt = vaddr + dpaa2_fd_get_offset(fd);
99 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
100 addr = dpaa2_sg_get_addr(&sgt[i]);
101 dma_unmap_single(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
104 sg_vaddr = phys_to_virt(addr);
105 skb_free_frag(sg_vaddr);
107 if (dpaa2_sg_is_final(&sgt[i]))
112 skb_free_frag(vaddr);
115 /* Build a linear skb based on a single-buffer frame descriptor */
116 static struct sk_buff *build_linear_skb(struct dpaa2_eth_priv *priv,
117 struct dpaa2_eth_channel *ch,
118 const struct dpaa2_fd *fd,
121 struct sk_buff *skb = NULL;
122 u16 fd_offset = dpaa2_fd_get_offset(fd);
123 u32 fd_length = dpaa2_fd_get_len(fd);
125 skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_SIZE +
126 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
130 skb_reserve(skb, fd_offset);
131 skb_put(skb, fd_length);
138 /* Build a non linear (fragmented) skb based on a S/G table */
139 static struct sk_buff *build_frag_skb(struct dpaa2_eth_priv *priv,
140 struct dpaa2_eth_channel *ch,
141 struct dpaa2_sg_entry *sgt)
143 struct sk_buff *skb = NULL;
144 struct device *dev = priv->net_dev->dev.parent;
149 struct page *page, *head_page;
153 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
154 struct dpaa2_sg_entry *sge = &sgt[i];
156 /* NOTE: We only support SG entries in dpaa2_sg_single format,
157 * but this is the only format we may receive from HW anyway
160 /* Get the address and length from the S/G entry */
161 sg_addr = dpaa2_sg_get_addr(sge);
162 dma_unmap_single(dev, sg_addr, DPAA2_ETH_RX_BUF_SIZE,
165 sg_vaddr = phys_to_virt(sg_addr);
166 sg_length = dpaa2_sg_get_len(sge);
169 /* We build the skb around the first data buffer */
170 skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_SIZE +
171 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
175 sg_offset = dpaa2_sg_get_offset(sge);
176 skb_reserve(skb, sg_offset);
177 skb_put(skb, sg_length);
179 /* Rest of the data buffers are stored as skb frags */
180 page = virt_to_page(sg_vaddr);
181 head_page = virt_to_head_page(sg_vaddr);
183 /* Offset in page (which may be compound).
184 * Data in subsequent SG entries is stored from the
185 * beginning of the buffer, so we don't need to add the
188 page_offset = ((unsigned long)sg_vaddr &
190 (page_address(page) - page_address(head_page));
192 skb_add_rx_frag(skb, i - 1, head_page, page_offset,
193 sg_length, DPAA2_ETH_RX_BUF_SIZE);
196 if (dpaa2_sg_is_final(sge))
200 /* Count all data buffers + SG table buffer */
201 ch->buf_count -= i + 2;
206 /* Main Rx frame processing routine */
207 static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
208 struct dpaa2_eth_channel *ch,
209 const struct dpaa2_fd *fd,
210 struct napi_struct *napi)
212 dma_addr_t addr = dpaa2_fd_get_addr(fd);
213 u8 fd_format = dpaa2_fd_get_format(fd);
216 struct rtnl_link_stats64 *percpu_stats;
217 struct dpaa2_eth_drv_stats *percpu_extras;
218 struct device *dev = priv->net_dev->dev.parent;
219 struct dpaa2_fas *fas;
223 trace_dpaa2_rx_fd(priv->net_dev, fd);
225 dma_unmap_single(dev, addr, DPAA2_ETH_RX_BUF_SIZE, DMA_FROM_DEVICE);
226 vaddr = phys_to_virt(addr);
228 prefetch(vaddr + priv->buf_layout.private_data_size);
229 prefetch(vaddr + dpaa2_fd_get_offset(fd));
231 percpu_stats = this_cpu_ptr(priv->percpu_stats);
232 percpu_extras = this_cpu_ptr(priv->percpu_extras);
234 if (fd_format == dpaa2_fd_single) {
235 skb = build_linear_skb(priv, ch, fd, vaddr);
236 } else if (fd_format == dpaa2_fd_sg) {
237 struct dpaa2_sg_entry *sgt =
238 vaddr + dpaa2_fd_get_offset(fd);
239 skb = build_frag_skb(priv, ch, sgt);
240 skb_free_frag(vaddr);
241 percpu_extras->rx_sg_frames++;
242 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
244 /* We don't support any other format */
245 goto err_frame_format;
253 /* Check if we need to validate the L4 csum */
254 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
255 fas = (struct dpaa2_fas *)
256 (vaddr + priv->buf_layout.private_data_size);
257 status = le32_to_cpu(fas->status);
258 validate_rx_csum(priv, status, skb);
261 skb->protocol = eth_type_trans(skb, priv->net_dev);
263 percpu_stats->rx_packets++;
264 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
266 if (priv->net_dev->features & NETIF_F_GRO)
267 napi_gro_receive(napi, skb);
269 netif_receive_skb(skb);
274 free_rx_fd(priv, fd, vaddr);
276 percpu_stats->rx_dropped++;
279 /* Consume all frames pull-dequeued into the store. This is the simplest way to
280 * make sure we don't accidentally issue another volatile dequeue which would
281 * overwrite (leak) frames already in the store.
283 * Observance of NAPI budget is not our concern, leaving that to the caller.
285 static int consume_frames(struct dpaa2_eth_channel *ch)
287 struct dpaa2_eth_priv *priv = ch->priv;
288 struct dpaa2_eth_fq *fq;
290 const struct dpaa2_fd *fd;
295 dq = dpaa2_io_store_next(ch->store, &is_last);
297 /* If we're here, we *must* have placed a
298 * volatile dequeue comnmand, so keep reading through
299 * the store until we get some sort of valid response
300 * token (either a valid frame or an "empty dequeue")
305 fd = dpaa2_dq_fd(dq);
306 fq = (struct dpaa2_eth_fq *)dpaa2_dq_fqd_ctx(dq);
309 fq->consume(priv, ch, fd, &ch->napi);
316 /* Create a frame descriptor based on a fragmented skb */
317 static int build_sg_fd(struct dpaa2_eth_priv *priv,
321 struct device *dev = priv->net_dev->dev.parent;
322 void *sgt_buf = NULL;
325 int nr_frags = skb_shinfo(skb)->nr_frags;
326 struct dpaa2_sg_entry *sgt;
329 struct scatterlist *scl, *crt_scl;
332 struct dpaa2_eth_swa *swa;
334 /* Create and map scatterlist.
335 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
336 * to go beyond nr_frags+1.
337 * Note: We don't support chained scatterlists
339 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
342 scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
346 sg_init_table(scl, nr_frags + 1);
347 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
348 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_TO_DEVICE);
349 if (unlikely(!num_dma_bufs)) {
351 goto dma_map_sg_failed;
354 /* Prepare the HW SGT structure */
355 sgt_buf_size = priv->tx_data_offset +
356 sizeof(struct dpaa2_sg_entry) * (1 + num_dma_bufs);
357 sgt_buf = kzalloc(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN, GFP_ATOMIC);
358 if (unlikely(!sgt_buf)) {
360 goto sgt_buf_alloc_failed;
362 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
364 /* PTA from egress side is passed as is to the confirmation side so
365 * we need to clear some fields here in order to find consistent values
366 * on TX confirmation. We are clearing FAS (Frame Annotation Status)
367 * field from the hardware annotation area
369 hwa = sgt_buf + priv->buf_layout.private_data_size;
370 memset(hwa + DPAA2_FAS_OFFSET, 0, DPAA2_FAS_SIZE);
372 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
374 /* Fill in the HW SGT structure.
376 * sgt_buf is zeroed out, so the following fields are implicit
377 * in all sgt entries:
379 * - format is 'dpaa2_sg_single'
381 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
382 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
383 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
385 dpaa2_sg_set_final(&sgt[i - 1], true);
387 /* Store the skb backpointer in the SGT buffer.
388 * Fit the scatterlist and the number of buffers alongside the
389 * skb backpointer in the software annotation area. We'll need
390 * all of them on Tx Conf.
392 swa = (struct dpaa2_eth_swa *)sgt_buf;
395 swa->num_sg = num_sg;
396 swa->num_dma_bufs = num_dma_bufs;
398 /* Separately map the SGT buffer */
399 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_TO_DEVICE);
400 if (unlikely(dma_mapping_error(dev, addr))) {
402 goto dma_map_single_failed;
404 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
405 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
406 dpaa2_fd_set_addr(fd, addr);
407 dpaa2_fd_set_len(fd, skb->len);
408 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL | DPAA2_FD_CTRL_PTA |
413 dma_map_single_failed:
415 sgt_buf_alloc_failed:
416 dma_unmap_sg(dev, scl, num_sg, DMA_TO_DEVICE);
422 /* Create a frame descriptor based on a linear skb */
423 static int build_single_fd(struct dpaa2_eth_priv *priv,
427 struct device *dev = priv->net_dev->dev.parent;
430 struct sk_buff **skbh;
433 buffer_start = PTR_ALIGN(skb->data - priv->tx_data_offset -
434 DPAA2_ETH_TX_BUF_ALIGN,
435 DPAA2_ETH_TX_BUF_ALIGN);
437 /* PTA from egress side is passed as is to the confirmation side so
438 * we need to clear some fields here in order to find consistent values
439 * on TX confirmation. We are clearing FAS (Frame Annotation Status)
440 * field from the hardware annotation area
442 hwa = buffer_start + priv->buf_layout.private_data_size;
443 memset(hwa + DPAA2_FAS_OFFSET, 0, DPAA2_FAS_SIZE);
445 /* Store a backpointer to the skb at the beginning of the buffer
446 * (in the private data area) such that we can release it
449 skbh = (struct sk_buff **)buffer_start;
452 addr = dma_map_single(dev, buffer_start,
453 skb_tail_pointer(skb) - buffer_start,
455 if (unlikely(dma_mapping_error(dev, addr)))
458 dpaa2_fd_set_addr(fd, addr);
459 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
460 dpaa2_fd_set_len(fd, skb->len);
461 dpaa2_fd_set_format(fd, dpaa2_fd_single);
462 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL | DPAA2_FD_CTRL_PTA |
468 /* FD freeing routine on the Tx path
470 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
471 * back-pointed to is also freed.
472 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
474 * Optionally, return the frame annotation status word (FAS), which needs
475 * to be checked if we're on the confirmation path.
477 static void free_tx_fd(const struct dpaa2_eth_priv *priv,
478 const struct dpaa2_fd *fd,
481 struct device *dev = priv->net_dev->dev.parent;
483 struct sk_buff **skbh, *skb;
484 unsigned char *buffer_start;
486 struct scatterlist *scl;
487 int num_sg, num_dma_bufs;
488 struct dpaa2_eth_swa *swa;
489 u8 fd_format = dpaa2_fd_get_format(fd);
490 struct dpaa2_fas *fas;
492 fd_addr = dpaa2_fd_get_addr(fd);
493 skbh = phys_to_virt(fd_addr);
495 if (fd_format == dpaa2_fd_single) {
497 buffer_start = (unsigned char *)skbh;
498 /* Accessing the skb buffer is safe before dma unmap, because
499 * we didn't map the actual skb shell.
501 dma_unmap_single(dev, fd_addr,
502 skb_tail_pointer(skb) - buffer_start,
504 } else if (fd_format == dpaa2_fd_sg) {
505 swa = (struct dpaa2_eth_swa *)skbh;
508 num_sg = swa->num_sg;
509 num_dma_bufs = swa->num_dma_bufs;
511 /* Unmap the scatterlist */
512 dma_unmap_sg(dev, scl, num_sg, DMA_TO_DEVICE);
515 /* Unmap the SGT buffer */
516 unmap_size = priv->tx_data_offset +
517 sizeof(struct dpaa2_sg_entry) * (1 + num_dma_bufs);
518 dma_unmap_single(dev, fd_addr, unmap_size, DMA_TO_DEVICE);
520 /* Unsupported format, mark it as errored and give up */
526 /* Read the status from the Frame Annotation after we unmap the first
527 * buffer but before we free it. The caller function is responsible
528 * for checking the status value.
530 if (status && (dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
531 fas = (struct dpaa2_fas *)
532 ((void *)skbh + priv->buf_layout.private_data_size);
533 *status = le32_to_cpu(fas->status);
536 /* Free SGT buffer kmalloc'ed on tx */
537 if (fd_format != dpaa2_fd_single)
540 /* Move on with skb release */
544 static int dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
546 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
548 struct rtnl_link_stats64 *percpu_stats;
549 struct dpaa2_eth_drv_stats *percpu_extras;
550 struct dpaa2_eth_fq *fq;
554 percpu_stats = this_cpu_ptr(priv->percpu_stats);
555 percpu_extras = this_cpu_ptr(priv->percpu_extras);
557 if (unlikely(skb_headroom(skb) < DPAA2_ETH_NEEDED_HEADROOM(priv))) {
560 ns = skb_realloc_headroom(skb, DPAA2_ETH_NEEDED_HEADROOM(priv));
562 percpu_stats->tx_dropped++;
563 goto err_alloc_headroom;
569 /* We'll be holding a back-reference to the skb until Tx Confirmation;
570 * we don't want that overwritten by a concurrent Tx with a cloned skb.
572 skb = skb_unshare(skb, GFP_ATOMIC);
573 if (unlikely(!skb)) {
574 /* skb_unshare() has already freed the skb */
575 percpu_stats->tx_dropped++;
579 /* Setup the FD fields */
580 memset(&fd, 0, sizeof(fd));
582 if (skb_is_nonlinear(skb)) {
583 err = build_sg_fd(priv, skb, &fd);
584 percpu_extras->tx_sg_frames++;
585 percpu_extras->tx_sg_bytes += skb->len;
587 err = build_single_fd(priv, skb, &fd);
591 percpu_stats->tx_dropped++;
596 trace_dpaa2_tx_fd(net_dev, &fd);
598 /* TxConf FQ selection primarily based on cpu affinity; this is
599 * non-migratable context, so it's safe to call smp_processor_id().
601 queue_mapping = smp_processor_id() % dpaa2_eth_queue_count(priv);
602 fq = &priv->fq[queue_mapping];
603 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
604 err = dpaa2_io_service_enqueue_qd(NULL, priv->tx_qdid, 0,
609 percpu_extras->tx_portal_busy += i;
610 if (unlikely(err < 0)) {
611 percpu_stats->tx_errors++;
612 /* Clean up everything, including freeing the skb */
613 free_tx_fd(priv, &fd, NULL);
615 percpu_stats->tx_packets++;
616 percpu_stats->tx_bytes += skb->len;
628 /* Tx confirmation frame processing routine */
629 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
630 struct dpaa2_eth_channel *ch,
631 const struct dpaa2_fd *fd,
632 struct napi_struct *napi __always_unused)
634 struct rtnl_link_stats64 *percpu_stats;
635 struct dpaa2_eth_drv_stats *percpu_extras;
639 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
641 percpu_extras = this_cpu_ptr(priv->percpu_extras);
642 percpu_extras->tx_conf_frames++;
643 percpu_extras->tx_conf_bytes += dpaa2_fd_get_len(fd);
645 free_tx_fd(priv, fd, &status);
647 if (unlikely(status & DPAA2_ETH_TXCONF_ERR_MASK)) {
648 percpu_stats = this_cpu_ptr(priv->percpu_stats);
649 /* Tx-conf logically pertains to the egress path. */
650 percpu_stats->tx_errors++;
654 static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
658 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
659 DPNI_OFF_RX_L3_CSUM, enable);
661 netdev_err(priv->net_dev,
662 "dpni_set_offload(RX_L3_CSUM) failed\n");
666 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
667 DPNI_OFF_RX_L4_CSUM, enable);
669 netdev_err(priv->net_dev,
670 "dpni_set_offload(RX_L4_CSUM) failed\n");
677 static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
681 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
682 DPNI_OFF_TX_L3_CSUM, enable);
684 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
688 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
689 DPNI_OFF_TX_L4_CSUM, enable);
691 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
698 /* Perform a single release command to add buffers
699 * to the specified buffer pool
701 static int add_bufs(struct dpaa2_eth_priv *priv, u16 bpid)
703 struct device *dev = priv->net_dev->dev.parent;
704 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
709 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
710 /* Allocate buffer visible to WRIOP + skb shared info +
713 buf = napi_alloc_frag(DPAA2_ETH_BUF_RAW_SIZE);
717 buf = PTR_ALIGN(buf, DPAA2_ETH_RX_BUF_ALIGN);
719 addr = dma_map_single(dev, buf, DPAA2_ETH_RX_BUF_SIZE,
721 if (unlikely(dma_mapping_error(dev, addr)))
727 trace_dpaa2_eth_buf_seed(priv->net_dev,
728 buf, DPAA2_ETH_BUF_RAW_SIZE,
729 addr, DPAA2_ETH_RX_BUF_SIZE,
734 /* In case the portal is busy, retry until successful.
735 * The buffer release function would only fail if the QBMan portal
736 * was busy, which implies portal contention (i.e. more CPUs than
737 * portals, i.e. GPPs w/o affine DPIOs). For all practical purposes,
738 * there is little we can realistically do, short of giving up -
739 * in which case we'd risk depleting the buffer pool and never again
740 * receiving the Rx interrupt which would kick-start the refill logic.
741 * So just keep retrying, at the risk of being moved to ksoftirqd.
743 while (dpaa2_io_service_release(NULL, bpid, buf_array, i))
756 static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
761 /* This is the lazy seeding of Rx buffer pools.
762 * dpaa2_add_bufs() is also used on the Rx hotpath and calls
763 * napi_alloc_frag(). The trouble with that is that it in turn ends up
764 * calling this_cpu_ptr(), which mandates execution in atomic context.
765 * Rather than splitting up the code, do a one-off preempt disable.
768 for (j = 0; j < priv->num_channels; j++) {
769 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
770 i += DPAA2_ETH_BUFS_PER_CMD) {
771 new_count = add_bufs(priv, bpid);
772 priv->channel[j]->buf_count += new_count;
774 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
786 * Drain the specified number of buffers from the DPNI's private buffer pool.
787 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
789 static void drain_bufs(struct dpaa2_eth_priv *priv, int count)
791 struct device *dev = priv->net_dev->dev.parent;
792 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
797 ret = dpaa2_io_service_acquire(NULL, priv->dpbp_attrs.bpid,
800 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
803 for (i = 0; i < ret; i++) {
804 /* Same logic as on regular Rx path */
805 dma_unmap_single(dev, buf_array[i],
806 DPAA2_ETH_RX_BUF_SIZE,
808 vaddr = phys_to_virt(buf_array[i]);
809 skb_free_frag(vaddr);
814 static void drain_pool(struct dpaa2_eth_priv *priv)
818 drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
821 for (i = 0; i < priv->num_channels; i++)
822 priv->channel[i]->buf_count = 0;
825 /* Function is called from softirq context only, so we don't need to guard
826 * the access to percpu count
828 static int refill_pool(struct dpaa2_eth_priv *priv,
829 struct dpaa2_eth_channel *ch,
834 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
838 new_count = add_bufs(priv, bpid);
839 if (unlikely(!new_count)) {
840 /* Out of memory; abort for now, we'll try later on */
843 ch->buf_count += new_count;
844 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
846 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
852 static int pull_channel(struct dpaa2_eth_channel *ch)
857 /* Retry while portal is busy */
859 err = dpaa2_io_service_pull_channel(NULL, ch->ch_id, ch->store);
862 } while (err == -EBUSY);
864 ch->stats.dequeue_portal_busy += dequeues;
866 ch->stats.pull_err++;
873 * Frames are dequeued from the QMan channel associated with this NAPI context.
874 * Rx, Tx confirmation and (if configured) Rx error frames all count
875 * towards the NAPI budget.
877 static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
879 struct dpaa2_eth_channel *ch;
880 int cleaned = 0, store_cleaned;
881 struct dpaa2_eth_priv *priv;
884 ch = container_of(napi, struct dpaa2_eth_channel, napi);
887 while (cleaned < budget) {
888 err = pull_channel(ch);
892 /* Refill pool if appropriate */
893 refill_pool(priv, ch, priv->dpbp_attrs.bpid);
895 store_cleaned = consume_frames(ch);
896 cleaned += store_cleaned;
898 /* If we have enough budget left for a full store,
899 * try a new pull dequeue, otherwise we're done here
901 if (store_cleaned == 0 ||
902 cleaned > budget - DPAA2_ETH_STORE_SIZE)
906 if (cleaned < budget) {
907 napi_complete_done(napi, cleaned);
908 /* Re-enable data available notifications */
910 err = dpaa2_io_service_rearm(NULL, &ch->nctx);
912 } while (err == -EBUSY);
915 ch->stats.frames += cleaned;
920 static void enable_ch_napi(struct dpaa2_eth_priv *priv)
922 struct dpaa2_eth_channel *ch;
925 for (i = 0; i < priv->num_channels; i++) {
926 ch = priv->channel[i];
927 napi_enable(&ch->napi);
931 static void disable_ch_napi(struct dpaa2_eth_priv *priv)
933 struct dpaa2_eth_channel *ch;
936 for (i = 0; i < priv->num_channels; i++) {
937 ch = priv->channel[i];
938 napi_disable(&ch->napi);
942 static int link_state_update(struct dpaa2_eth_priv *priv)
944 struct dpni_link_state state;
947 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
949 netdev_err(priv->net_dev,
950 "dpni_get_link_state() failed\n");
954 /* Chech link state; speed / duplex changes are not treated yet */
955 if (priv->link_state.up == state.up)
958 priv->link_state = state;
960 netif_carrier_on(priv->net_dev);
961 netif_tx_start_all_queues(priv->net_dev);
963 netif_tx_stop_all_queues(priv->net_dev);
964 netif_carrier_off(priv->net_dev);
967 netdev_info(priv->net_dev, "Link Event: state %s",
968 state.up ? "up" : "down");
973 static int dpaa2_eth_open(struct net_device *net_dev)
975 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
978 err = seed_pool(priv, priv->dpbp_attrs.bpid);
980 /* Not much to do; the buffer pool, though not filled up,
981 * may still contain some buffers which would enable us
984 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
985 priv->dpbp_dev->obj_desc.id, priv->dpbp_attrs.bpid);
988 /* We'll only start the txqs when the link is actually ready; make sure
989 * we don't race against the link up notification, which may come
990 * immediately after dpni_enable();
992 netif_tx_stop_all_queues(net_dev);
993 enable_ch_napi(priv);
994 /* Also, explicitly set carrier off, otherwise netif_carrier_ok() will
995 * return true and cause 'ip link show' to report the LOWER_UP flag,
996 * even though the link notification wasn't even received.
998 netif_carrier_off(net_dev);
1000 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1002 netdev_err(net_dev, "dpni_enable() failed\n");
1006 /* If the DPMAC object has already processed the link up interrupt,
1007 * we have to learn the link state ourselves.
1009 err = link_state_update(priv);
1011 netdev_err(net_dev, "Can't update link state\n");
1012 goto link_state_err;
1019 disable_ch_napi(priv);
1024 /* The DPIO store must be empty when we call this,
1025 * at the end of every NAPI cycle.
1027 static u32 drain_channel(struct dpaa2_eth_priv *priv,
1028 struct dpaa2_eth_channel *ch)
1030 u32 drained = 0, total = 0;
1034 drained = consume_frames(ch);
1041 static u32 drain_ingress_frames(struct dpaa2_eth_priv *priv)
1043 struct dpaa2_eth_channel *ch;
1047 for (i = 0; i < priv->num_channels; i++) {
1048 ch = priv->channel[i];
1049 drained += drain_channel(priv, ch);
1055 static int dpaa2_eth_stop(struct net_device *net_dev)
1057 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1062 netif_tx_stop_all_queues(net_dev);
1063 netif_carrier_off(net_dev);
1065 /* Loop while dpni_disable() attempts to drain the egress FQs
1066 * and confirm them back to us.
1069 dpni_disable(priv->mc_io, 0, priv->mc_token);
1070 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1072 /* Allow the hardware some slack */
1074 } while (dpni_enabled && --retries);
1076 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1077 /* Must go on and disable NAPI nonetheless, so we don't crash at
1078 * the next "ifconfig up"
1082 /* Wait for NAPI to complete on every core and disable it.
1083 * In particular, this will also prevent NAPI from being rescheduled if
1084 * a new CDAN is serviced, effectively discarding the CDAN. We therefore
1085 * don't even need to disarm the channels, except perhaps for the case
1086 * of a huge coalescing value.
1088 disable_ch_napi(priv);
1090 /* Manually drain the Rx and TxConf queues */
1091 drained = drain_ingress_frames(priv);
1093 netdev_dbg(net_dev, "Drained %d frames.\n", drained);
1095 /* Empty the buffer pool */
1101 static int dpaa2_eth_init(struct net_device *net_dev)
1104 u64 not_supported = 0;
1105 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1106 u32 options = priv->dpni_attrs.options;
1108 /* Capabilities listing */
1109 supported |= IFF_LIVE_ADDR_CHANGE;
1111 if (options & DPNI_OPT_NO_MAC_FILTER)
1112 not_supported |= IFF_UNICAST_FLT;
1114 supported |= IFF_UNICAST_FLT;
1116 net_dev->priv_flags |= supported;
1117 net_dev->priv_flags &= ~not_supported;
1120 net_dev->features = NETIF_F_RXCSUM |
1121 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1122 NETIF_F_SG | NETIF_F_HIGHDMA |
1124 net_dev->hw_features = net_dev->features;
1129 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1131 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1132 struct device *dev = net_dev->dev.parent;
1135 err = eth_mac_addr(net_dev, addr);
1137 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1141 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1144 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1151 /** Fill in counters maintained by the GPP driver. These may be different from
1152 * the hardware counters obtained by ethtool.
1154 void dpaa2_eth_get_stats(struct net_device *net_dev,
1155 struct rtnl_link_stats64 *stats)
1157 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1158 struct rtnl_link_stats64 *percpu_stats;
1160 u64 *netstats = (u64 *)stats;
1162 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1164 for_each_possible_cpu(i) {
1165 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1166 cpustats = (u64 *)percpu_stats;
1167 for (j = 0; j < num; j++)
1168 netstats[j] += cpustats[j];
1172 static int dpaa2_eth_change_mtu(struct net_device *net_dev, int mtu)
1174 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1177 /* Set the maximum Rx frame length to match the transmit side;
1178 * account for L2 headers when computing the MFL
1180 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
1181 (u16)DPAA2_ETH_L2_MAX_FRM(mtu));
1183 netdev_err(net_dev, "dpni_set_max_frame_length() failed\n");
1191 /* Copy mac unicast addresses from @net_dev to @priv.
1192 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1194 static void add_uc_hw_addr(const struct net_device *net_dev,
1195 struct dpaa2_eth_priv *priv)
1197 struct netdev_hw_addr *ha;
1200 netdev_for_each_uc_addr(ha, net_dev) {
1201 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1204 netdev_warn(priv->net_dev,
1205 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1210 /* Copy mac multicast addresses from @net_dev to @priv
1211 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1213 static void add_mc_hw_addr(const struct net_device *net_dev,
1214 struct dpaa2_eth_priv *priv)
1216 struct netdev_hw_addr *ha;
1219 netdev_for_each_mc_addr(ha, net_dev) {
1220 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1223 netdev_warn(priv->net_dev,
1224 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1229 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1231 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1232 int uc_count = netdev_uc_count(net_dev);
1233 int mc_count = netdev_mc_count(net_dev);
1234 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1235 u32 options = priv->dpni_attrs.options;
1236 u16 mc_token = priv->mc_token;
1237 struct fsl_mc_io *mc_io = priv->mc_io;
1240 /* Basic sanity checks; these probably indicate a misconfiguration */
1241 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1242 netdev_info(net_dev,
1243 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1246 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1247 if (uc_count > max_mac) {
1248 netdev_info(net_dev,
1249 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1253 if (mc_count + uc_count > max_mac) {
1254 netdev_info(net_dev,
1255 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1256 uc_count + mc_count, max_mac);
1257 goto force_mc_promisc;
1260 /* Adjust promisc settings due to flag combinations */
1261 if (net_dev->flags & IFF_PROMISC)
1263 if (net_dev->flags & IFF_ALLMULTI) {
1264 /* First, rebuild unicast filtering table. This should be done
1265 * in promisc mode, in order to avoid frame loss while we
1266 * progressively add entries to the table.
1267 * We don't know whether we had been in promisc already, and
1268 * making an MC call to find out is expensive; so set uc promisc
1271 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1273 netdev_warn(net_dev, "Can't set uc promisc\n");
1275 /* Actual uc table reconstruction. */
1276 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1278 netdev_warn(net_dev, "Can't clear uc filters\n");
1279 add_uc_hw_addr(net_dev, priv);
1281 /* Finally, clear uc promisc and set mc promisc as requested. */
1282 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1284 netdev_warn(net_dev, "Can't clear uc promisc\n");
1285 goto force_mc_promisc;
1288 /* Neither unicast, nor multicast promisc will be on... eventually.
1289 * For now, rebuild mac filtering tables while forcing both of them on.
1291 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1293 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1294 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1296 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1298 /* Actual mac filtering tables reconstruction */
1299 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1301 netdev_warn(net_dev, "Can't clear mac filters\n");
1302 add_mc_hw_addr(net_dev, priv);
1303 add_uc_hw_addr(net_dev, priv);
1305 /* Now we can clear both ucast and mcast promisc, without risking
1306 * to drop legitimate frames anymore.
1308 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1310 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1311 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1313 netdev_warn(net_dev, "Can't clear mcast promisc\n");
1318 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1320 netdev_warn(net_dev, "Can't set ucast promisc\n");
1322 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1324 netdev_warn(net_dev, "Can't set mcast promisc\n");
1327 static int dpaa2_eth_set_features(struct net_device *net_dev,
1328 netdev_features_t features)
1330 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1331 netdev_features_t changed = features ^ net_dev->features;
1335 if (changed & NETIF_F_RXCSUM) {
1336 enable = !!(features & NETIF_F_RXCSUM);
1337 err = set_rx_csum(priv, enable);
1342 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1343 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
1344 err = set_tx_csum(priv, enable);
1352 static const struct net_device_ops dpaa2_eth_ops = {
1353 .ndo_open = dpaa2_eth_open,
1354 .ndo_start_xmit = dpaa2_eth_tx,
1355 .ndo_stop = dpaa2_eth_stop,
1356 .ndo_init = dpaa2_eth_init,
1357 .ndo_set_mac_address = dpaa2_eth_set_addr,
1358 .ndo_get_stats64 = dpaa2_eth_get_stats,
1359 .ndo_change_mtu = dpaa2_eth_change_mtu,
1360 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
1361 .ndo_set_features = dpaa2_eth_set_features,
1364 static void cdan_cb(struct dpaa2_io_notification_ctx *ctx)
1366 struct dpaa2_eth_channel *ch;
1368 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
1370 /* Update NAPI statistics */
1373 napi_schedule_irqoff(&ch->napi);
1376 /* Allocate and configure a DPCON object */
1377 static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv)
1379 struct fsl_mc_device *dpcon;
1380 struct device *dev = priv->net_dev->dev.parent;
1381 struct dpcon_attr attrs;
1384 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
1385 FSL_MC_POOL_DPCON, &dpcon);
1387 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
1391 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
1393 dev_err(dev, "dpcon_open() failed\n");
1397 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
1399 dev_err(dev, "dpcon_reset() failed\n");
1403 err = dpcon_get_attributes(priv->mc_io, 0, dpcon->mc_handle, &attrs);
1405 dev_err(dev, "dpcon_get_attributes() failed\n");
1409 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
1411 dev_err(dev, "dpcon_enable() failed\n");
1420 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
1422 fsl_mc_object_free(dpcon);
1427 static void free_dpcon(struct dpaa2_eth_priv *priv,
1428 struct fsl_mc_device *dpcon)
1430 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
1431 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
1432 fsl_mc_object_free(dpcon);
1435 static struct dpaa2_eth_channel *
1436 alloc_channel(struct dpaa2_eth_priv *priv)
1438 struct dpaa2_eth_channel *channel;
1439 struct dpcon_attr attr;
1440 struct device *dev = priv->net_dev->dev.parent;
1443 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
1447 channel->dpcon = setup_dpcon(priv);
1448 if (!channel->dpcon)
1451 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
1454 dev_err(dev, "dpcon_get_attributes() failed\n");
1458 channel->dpcon_id = attr.id;
1459 channel->ch_id = attr.qbman_ch_id;
1460 channel->priv = priv;
1465 free_dpcon(priv, channel->dpcon);
1471 static void free_channel(struct dpaa2_eth_priv *priv,
1472 struct dpaa2_eth_channel *channel)
1474 free_dpcon(priv, channel->dpcon);
1478 /* DPIO setup: allocate and configure QBMan channels, setup core affinity
1479 * and register data availability notifications
1481 static int setup_dpio(struct dpaa2_eth_priv *priv)
1483 struct dpaa2_io_notification_ctx *nctx;
1484 struct dpaa2_eth_channel *channel;
1485 struct dpcon_notification_cfg dpcon_notif_cfg;
1486 struct device *dev = priv->net_dev->dev.parent;
1489 /* We want the ability to spread ingress traffic (RX, TX conf) to as
1490 * many cores as possible, so we need one channel for each core
1491 * (unless there's fewer queues than cores, in which case the extra
1492 * channels would be wasted).
1493 * Allocate one channel per core and register it to the core's
1494 * affine DPIO. If not enough channels are available for all cores
1495 * or if some cores don't have an affine DPIO, there will be no
1496 * ingress frame processing on those cores.
1498 cpumask_clear(&priv->dpio_cpumask);
1499 for_each_online_cpu(i) {
1500 /* Try to allocate a channel */
1501 channel = alloc_channel(priv);
1504 "No affine channel for cpu %d and above\n", i);
1508 priv->channel[priv->num_channels] = channel;
1510 nctx = &channel->nctx;
1513 nctx->id = channel->ch_id;
1514 nctx->desired_cpu = i;
1516 /* Register the new context */
1517 err = dpaa2_io_service_register(NULL, nctx);
1519 dev_info(dev, "No affine DPIO for cpu %d\n", i);
1520 /* If no affine DPIO for this core, there's probably
1521 * none available for next cores either.
1523 goto err_service_reg;
1526 /* Register DPCON notification with MC */
1527 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
1528 dpcon_notif_cfg.priority = 0;
1529 dpcon_notif_cfg.user_ctx = nctx->qman64;
1530 err = dpcon_set_notification(priv->mc_io, 0,
1531 channel->dpcon->mc_handle,
1534 dev_err(dev, "dpcon_set_notification failed()\n");
1538 /* If we managed to allocate a channel and also found an affine
1539 * DPIO for this core, add it to the final mask
1541 cpumask_set_cpu(i, &priv->dpio_cpumask);
1542 priv->num_channels++;
1544 /* Stop if we already have enough channels to accommodate all
1545 * RX and TX conf queues
1547 if (priv->num_channels == dpaa2_eth_queue_count(priv))
1554 dpaa2_io_service_deregister(NULL, nctx);
1556 free_channel(priv, channel);
1558 if (cpumask_empty(&priv->dpio_cpumask)) {
1559 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
1563 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
1564 cpumask_pr_args(&priv->dpio_cpumask));
1569 static void free_dpio(struct dpaa2_eth_priv *priv)
1572 struct dpaa2_eth_channel *ch;
1574 /* deregister CDAN notifications and free channels */
1575 for (i = 0; i < priv->num_channels; i++) {
1576 ch = priv->channel[i];
1577 dpaa2_io_service_deregister(NULL, &ch->nctx);
1578 free_channel(priv, ch);
1582 static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv,
1585 struct device *dev = priv->net_dev->dev.parent;
1588 for (i = 0; i < priv->num_channels; i++)
1589 if (priv->channel[i]->nctx.desired_cpu == cpu)
1590 return priv->channel[i];
1592 /* We should never get here. Issue a warning and return
1593 * the first channel, because it's still better than nothing
1595 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
1597 return priv->channel[0];
1600 static void set_fq_affinity(struct dpaa2_eth_priv *priv)
1602 struct device *dev = priv->net_dev->dev.parent;
1603 struct dpaa2_eth_fq *fq;
1604 int rx_cpu, txc_cpu;
1607 /* For each FQ, pick one channel/CPU to deliver frames to.
1608 * This may well change at runtime, either through irqbalance or
1609 * through direct user intervention.
1611 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
1613 for (i = 0; i < priv->num_fqs; i++) {
1617 fq->target_cpu = rx_cpu;
1618 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
1619 if (rx_cpu >= nr_cpu_ids)
1620 rx_cpu = cpumask_first(&priv->dpio_cpumask);
1622 case DPAA2_TX_CONF_FQ:
1623 fq->target_cpu = txc_cpu;
1624 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
1625 if (txc_cpu >= nr_cpu_ids)
1626 txc_cpu = cpumask_first(&priv->dpio_cpumask);
1629 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
1631 fq->channel = get_affine_channel(priv, fq->target_cpu);
1635 static void setup_fqs(struct dpaa2_eth_priv *priv)
1639 /* We have one TxConf FQ per Tx flow.
1640 * The number of Tx and Rx queues is the same.
1641 * Tx queues come first in the fq array.
1643 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
1644 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
1645 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
1646 priv->fq[priv->num_fqs++].flowid = (u16)i;
1649 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
1650 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
1651 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
1652 priv->fq[priv->num_fqs++].flowid = (u16)i;
1655 /* For each FQ, decide on which core to process incoming frames */
1656 set_fq_affinity(priv);
1659 /* Allocate and configure one buffer pool for each interface */
1660 static int setup_dpbp(struct dpaa2_eth_priv *priv)
1663 struct fsl_mc_device *dpbp_dev;
1664 struct device *dev = priv->net_dev->dev.parent;
1666 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
1669 dev_err(dev, "DPBP device allocation failed\n");
1673 priv->dpbp_dev = dpbp_dev;
1675 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
1676 &dpbp_dev->mc_handle);
1678 dev_err(dev, "dpbp_open() failed\n");
1682 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
1684 dev_err(dev, "dpbp_enable() failed\n");
1688 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
1691 dev_err(dev, "dpbp_get_attributes() failed\n");
1698 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
1700 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
1702 fsl_mc_object_free(dpbp_dev);
1707 static void free_dpbp(struct dpaa2_eth_priv *priv)
1710 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
1711 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
1712 fsl_mc_object_free(priv->dpbp_dev);
1715 /* Configure the DPNI object this interface is associated with */
1716 static int setup_dpni(struct fsl_mc_device *ls_dev)
1718 struct device *dev = &ls_dev->dev;
1719 struct dpaa2_eth_priv *priv;
1720 struct net_device *net_dev;
1723 net_dev = dev_get_drvdata(dev);
1724 priv = netdev_priv(net_dev);
1726 priv->dpni_id = ls_dev->obj_desc.id;
1728 /* get a handle for the DPNI object */
1729 err = dpni_open(priv->mc_io, 0, priv->dpni_id, &priv->mc_token);
1731 dev_err(dev, "dpni_open() failed\n");
1735 ls_dev->mc_io = priv->mc_io;
1736 ls_dev->mc_handle = priv->mc_token;
1738 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
1740 dev_err(dev, "dpni_reset() failed\n");
1744 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
1747 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
1751 /* Configure buffer layouts */
1753 priv->buf_layout.pass_parser_result = true;
1754 priv->buf_layout.pass_frame_status = true;
1755 priv->buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
1756 priv->buf_layout.data_align = DPAA2_ETH_RX_BUF_ALIGN;
1757 priv->buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
1758 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
1759 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
1760 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN;
1761 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
1762 DPNI_QUEUE_RX, &priv->buf_layout);
1764 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
1765 goto err_buf_layout;
1769 priv->buf_layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
1770 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
1771 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
1772 DPNI_QUEUE_TX, &priv->buf_layout);
1774 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
1775 goto err_buf_layout;
1778 /* tx-confirm buffer */
1779 priv->buf_layout.options = DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
1780 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
1781 DPNI_QUEUE_TX_CONFIRM, &priv->buf_layout);
1783 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
1784 goto err_buf_layout;
1787 /* Now that we've set our tx buffer layout, retrieve the minimum
1788 * required tx data offset.
1790 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
1791 &priv->tx_data_offset);
1793 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
1794 goto err_data_offset;
1797 if ((priv->tx_data_offset % 64) != 0)
1798 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B",
1799 priv->tx_data_offset);
1801 /* Accommodate software annotation space (SWA) */
1802 priv->tx_data_offset += DPAA2_ETH_SWA_SIZE;
1810 dpni_close(priv->mc_io, 0, priv->mc_token);
1815 static void free_dpni(struct dpaa2_eth_priv *priv)
1819 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
1821 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
1824 dpni_close(priv->mc_io, 0, priv->mc_token);
1827 static int setup_rx_flow(struct dpaa2_eth_priv *priv,
1828 struct dpaa2_eth_fq *fq)
1830 struct device *dev = priv->net_dev->dev.parent;
1831 struct dpni_queue queue;
1832 struct dpni_queue_id qid;
1833 struct dpni_taildrop td;
1836 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
1837 DPNI_QUEUE_RX, 0, fq->flowid, &queue, &qid);
1839 dev_err(dev, "dpni_get_queue(RX) failed\n");
1843 fq->fqid = qid.fqid;
1845 queue.destination.id = fq->channel->dpcon_id;
1846 queue.destination.type = DPNI_DEST_DPCON;
1847 queue.destination.priority = 1;
1848 queue.user_context = (u64)fq;
1849 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
1850 DPNI_QUEUE_RX, 0, fq->flowid,
1851 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
1854 dev_err(dev, "dpni_set_queue(RX) failed\n");
1859 td.threshold = DPAA2_ETH_TAILDROP_THRESH;
1860 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, DPNI_CP_QUEUE,
1861 DPNI_QUEUE_RX, 0, fq->flowid, &td);
1863 dev_err(dev, "dpni_set_threshold() failed\n");
1870 static int setup_tx_flow(struct dpaa2_eth_priv *priv,
1871 struct dpaa2_eth_fq *fq)
1873 struct device *dev = priv->net_dev->dev.parent;
1874 struct dpni_queue queue;
1875 struct dpni_queue_id qid;
1878 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
1879 DPNI_QUEUE_TX, 0, fq->flowid, &queue, &qid);
1881 dev_err(dev, "dpni_get_queue(TX) failed\n");
1885 fq->tx_qdbin = qid.qdbin;
1887 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
1888 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
1891 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
1895 fq->fqid = qid.fqid;
1897 queue.destination.id = fq->channel->dpcon_id;
1898 queue.destination.type = DPNI_DEST_DPCON;
1899 queue.destination.priority = 0;
1900 queue.user_context = (u64)fq;
1901 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
1902 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
1903 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
1906 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
1913 /* Hash key is a 5-tuple: IPsrc, IPdst, IPnextproto, L4src, L4dst */
1914 static const struct dpaa2_eth_hash_fields hash_fields[] = {
1917 .rxnfc_field = RXH_IP_SRC,
1918 .cls_prot = NET_PROT_IP,
1919 .cls_field = NH_FLD_IP_SRC,
1922 .rxnfc_field = RXH_IP_DST,
1923 .cls_prot = NET_PROT_IP,
1924 .cls_field = NH_FLD_IP_DST,
1927 .rxnfc_field = RXH_L3_PROTO,
1928 .cls_prot = NET_PROT_IP,
1929 .cls_field = NH_FLD_IP_PROTO,
1932 /* Using UDP ports, this is functionally equivalent to raw
1933 * byte pairs from L4 header.
1935 .rxnfc_field = RXH_L4_B_0_1,
1936 .cls_prot = NET_PROT_UDP,
1937 .cls_field = NH_FLD_UDP_PORT_SRC,
1940 .rxnfc_field = RXH_L4_B_2_3,
1941 .cls_prot = NET_PROT_UDP,
1942 .cls_field = NH_FLD_UDP_PORT_DST,
1947 /* Set RX hash options
1948 * flags is a combination of RXH_ bits
1950 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
1952 struct device *dev = net_dev->dev.parent;
1953 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1954 struct dpkg_profile_cfg cls_cfg;
1955 struct dpni_rx_tc_dist_cfg dist_cfg;
1960 if (!dpaa2_eth_hash_enabled(priv)) {
1961 dev_err(dev, "Hashing support is not enabled\n");
1965 memset(&cls_cfg, 0, sizeof(cls_cfg));
1967 for (i = 0; i < ARRAY_SIZE(hash_fields); i++) {
1968 struct dpkg_extract *key =
1969 &cls_cfg.extracts[cls_cfg.num_extracts];
1971 if (!(flags & hash_fields[i].rxnfc_field))
1974 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
1975 dev_err(dev, "error adding key extraction rule, too many rules?\n");
1979 key->type = DPKG_EXTRACT_FROM_HDR;
1980 key->extract.from_hdr.prot = hash_fields[i].cls_prot;
1981 key->extract.from_hdr.type = DPKG_FULL_FIELD;
1982 key->extract.from_hdr.field = hash_fields[i].cls_field;
1983 cls_cfg.num_extracts++;
1985 priv->rx_hash_fields |= hash_fields[i].rxnfc_field;
1988 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_DMA | GFP_KERNEL);
1992 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
1994 dev_err(dev, "dpni_prepare_key_cfg error %d", err);
1998 memset(&dist_cfg, 0, sizeof(dist_cfg));
2000 /* Prepare for setting the rx dist */
2001 dist_cfg.key_cfg_iova = dma_map_single(net_dev->dev.parent, dma_mem,
2002 DPAA2_CLASSIFIER_DMA_SIZE,
2004 if (dma_mapping_error(net_dev->dev.parent, dist_cfg.key_cfg_iova)) {
2005 dev_err(dev, "DMA mapping failed\n");
2010 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2011 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
2013 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg);
2014 dma_unmap_single(net_dev->dev.parent, dist_cfg.key_cfg_iova,
2015 DPAA2_CLASSIFIER_DMA_SIZE, DMA_TO_DEVICE);
2017 dev_err(dev, "dpni_set_rx_tc_dist() error %d\n", err);
2025 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
2026 * frame queues and channels
2028 static int bind_dpni(struct dpaa2_eth_priv *priv)
2030 struct net_device *net_dev = priv->net_dev;
2031 struct device *dev = net_dev->dev.parent;
2032 struct dpni_pools_cfg pools_params;
2033 struct dpni_error_cfg err_cfg;
2037 pools_params.num_dpbp = 1;
2038 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
2039 pools_params.pools[0].backup_pool = 0;
2040 pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE;
2041 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
2043 dev_err(dev, "dpni_set_pools() failed\n");
2047 /* have the interface implicitly distribute traffic based on supported
2050 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_SUPPORTED);
2052 netdev_err(net_dev, "Failed to configure hashing\n");
2054 /* Configure handling of error frames */
2055 err_cfg.errors = DPAA2_ETH_RX_ERR_MASK;
2056 err_cfg.set_frame_annotation = 1;
2057 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
2058 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
2061 dev_err(dev, "dpni_set_errors_behavior failed\n");
2065 /* Configure Rx and Tx conf queues to generate CDANs */
2066 for (i = 0; i < priv->num_fqs; i++) {
2067 switch (priv->fq[i].type) {
2069 err = setup_rx_flow(priv, &priv->fq[i]);
2071 case DPAA2_TX_CONF_FQ:
2072 err = setup_tx_flow(priv, &priv->fq[i]);
2075 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
2082 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
2083 DPNI_QUEUE_TX, &priv->tx_qdid);
2085 dev_err(dev, "dpni_get_qdid() failed\n");
2092 /* Allocate rings for storing incoming frame descriptors */
2093 static int alloc_rings(struct dpaa2_eth_priv *priv)
2095 struct net_device *net_dev = priv->net_dev;
2096 struct device *dev = net_dev->dev.parent;
2099 for (i = 0; i < priv->num_channels; i++) {
2100 priv->channel[i]->store =
2101 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
2102 if (!priv->channel[i]->store) {
2103 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
2111 for (i = 0; i < priv->num_channels; i++) {
2112 if (!priv->channel[i]->store)
2114 dpaa2_io_store_destroy(priv->channel[i]->store);
2120 static void free_rings(struct dpaa2_eth_priv *priv)
2124 for (i = 0; i < priv->num_channels; i++)
2125 dpaa2_io_store_destroy(priv->channel[i]->store);
2128 static int netdev_init(struct net_device *net_dev)
2131 struct device *dev = net_dev->dev.parent;
2132 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2133 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
2134 u8 bcast_addr[ETH_ALEN];
2136 net_dev->netdev_ops = &dpaa2_eth_ops;
2138 /* Get firmware address, if any */
2139 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
2141 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
2145 /* Get DPNI attributes address, if any */
2146 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2149 dev_err(dev, "dpni_get_primary_mac_addr() failed (%d)\n", err);
2153 /* First check if firmware has any address configured by bootloader */
2154 if (!is_zero_ether_addr(mac_addr)) {
2155 /* If the DPMAC addr != DPNI addr, update it */
2156 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
2157 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
2161 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
2165 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
2166 } else if (is_zero_ether_addr(dpni_mac_addr)) {
2167 /* Fills in net_dev->dev_addr, as required by
2168 * register_netdevice()
2170 eth_hw_addr_random(net_dev);
2171 /* Make the user aware, without cluttering the boot log */
2172 dev_dbg_once(dev, " device(s) have all-zero hwaddr, replaced with random\n");
2173 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2176 dev_err(dev, "dpni_set_primary_mac_addr(): %d\n", err);
2179 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
2180 * practical purposes, this will be our "permanent" mac address,
2181 * at least until the next reboot. This move will also permit
2182 * register_netdevice() to properly fill up net_dev->perm_addr.
2184 net_dev->addr_assign_type = NET_ADDR_PERM;
2186 /* NET_ADDR_PERM is default, all we have to do is
2187 * fill in the device addr.
2189 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
2192 /* Explicitly add the broadcast address to the MAC filtering table;
2193 * the MC won't do that for us.
2195 eth_broadcast_addr(bcast_addr);
2196 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
2198 dev_warn(dev, "dpni_add_mac_addr() failed (%d)\n", err);
2199 /* Won't return an error; at least, we'd have egress traffic */
2202 /* Reserve enough space to align buffer as per hardware requirement;
2203 * NOTE: priv->tx_data_offset MUST be initialized at this point.
2205 net_dev->needed_headroom = DPAA2_ETH_NEEDED_HEADROOM(priv);
2207 /* Set MTU limits */
2208 net_dev->min_mtu = 68;
2209 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
2211 /* Our .ndo_init will be called herein */
2212 err = register_netdev(net_dev);
2214 dev_err(dev, "register_netdev() failed\n");
2221 static int poll_link_state(void *arg)
2223 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
2226 while (!kthread_should_stop()) {
2227 err = link_state_update(priv);
2231 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
2237 static irqreturn_t dpni_irq0_handler(int irq_num, void *arg)
2239 return IRQ_WAKE_THREAD;
2242 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
2244 u32 status, clear = 0;
2245 struct device *dev = (struct device *)arg;
2246 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
2247 struct net_device *net_dev = dev_get_drvdata(dev);
2250 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
2251 DPNI_IRQ_INDEX, &status);
2252 if (unlikely(err)) {
2253 netdev_err(net_dev, "Can't get irq status (err %d)", err);
2258 if (status & DPNI_IRQ_EVENT_LINK_CHANGED) {
2259 clear |= DPNI_IRQ_EVENT_LINK_CHANGED;
2260 link_state_update(netdev_priv(net_dev));
2264 dpni_clear_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
2265 DPNI_IRQ_INDEX, clear);
2269 static int setup_irqs(struct fsl_mc_device *ls_dev)
2272 struct fsl_mc_device_irq *irq;
2274 err = fsl_mc_allocate_irqs(ls_dev);
2276 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
2280 irq = ls_dev->irqs[0];
2281 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
2283 dpni_irq0_handler_thread,
2284 IRQF_NO_SUSPEND | IRQF_ONESHOT,
2285 dev_name(&ls_dev->dev), &ls_dev->dev);
2287 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d", err);
2291 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
2292 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED);
2294 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d", err);
2298 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
2301 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d", err);
2308 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
2310 fsl_mc_free_irqs(ls_dev);
2315 static void add_ch_napi(struct dpaa2_eth_priv *priv)
2318 struct dpaa2_eth_channel *ch;
2320 for (i = 0; i < priv->num_channels; i++) {
2321 ch = priv->channel[i];
2322 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
2323 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
2328 static void del_ch_napi(struct dpaa2_eth_priv *priv)
2331 struct dpaa2_eth_channel *ch;
2333 for (i = 0; i < priv->num_channels; i++) {
2334 ch = priv->channel[i];
2335 netif_napi_del(&ch->napi);
2339 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
2342 struct net_device *net_dev = NULL;
2343 struct dpaa2_eth_priv *priv = NULL;
2346 dev = &dpni_dev->dev;
2349 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_TX_QUEUES);
2351 dev_err(dev, "alloc_etherdev_mq() failed\n");
2355 SET_NETDEV_DEV(net_dev, dev);
2356 dev_set_drvdata(dev, net_dev);
2358 priv = netdev_priv(net_dev);
2359 priv->net_dev = net_dev;
2361 /* Obtain a MC portal */
2362 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
2365 dev_err(dev, "MC portal allocation failed\n");
2366 goto err_portal_alloc;
2369 /* MC objects initialization and configuration */
2370 err = setup_dpni(dpni_dev);
2372 goto err_dpni_setup;
2374 err = setup_dpio(priv);
2376 goto err_dpio_setup;
2380 err = setup_dpbp(priv);
2382 goto err_dpbp_setup;
2384 err = bind_dpni(priv);
2388 /* Add a NAPI context for each channel */
2391 /* Percpu statistics */
2392 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
2393 if (!priv->percpu_stats) {
2394 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
2396 goto err_alloc_percpu_stats;
2398 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
2399 if (!priv->percpu_extras) {
2400 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
2402 goto err_alloc_percpu_extras;
2405 err = netdev_init(net_dev);
2407 goto err_netdev_init;
2409 /* Configure checksum offload based on current interface flags */
2410 err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
2414 err = set_tx_csum(priv, !!(net_dev->features &
2415 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
2419 err = alloc_rings(priv);
2421 goto err_alloc_rings;
2423 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
2425 err = setup_irqs(dpni_dev);
2427 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
2428 priv->poll_thread = kthread_run(poll_link_state, priv,
2429 "%s_poll_link", net_dev->name);
2430 if (IS_ERR(priv->poll_thread)) {
2431 netdev_err(net_dev, "Error starting polling thread\n");
2432 goto err_poll_thread;
2434 priv->do_link_poll = true;
2437 dev_info(dev, "Probed interface %s\n", net_dev->name);
2444 unregister_netdev(net_dev);
2446 free_percpu(priv->percpu_extras);
2447 err_alloc_percpu_extras:
2448 free_percpu(priv->percpu_stats);
2449 err_alloc_percpu_stats:
2458 fsl_mc_portal_free(priv->mc_io);
2460 dev_set_drvdata(dev, NULL);
2461 free_netdev(net_dev);
2466 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
2469 struct net_device *net_dev;
2470 struct dpaa2_eth_priv *priv;
2473 net_dev = dev_get_drvdata(dev);
2474 priv = netdev_priv(net_dev);
2476 unregister_netdev(net_dev);
2477 dev_info(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
2479 if (priv->do_link_poll)
2480 kthread_stop(priv->poll_thread);
2482 fsl_mc_free_irqs(ls_dev);
2485 free_percpu(priv->percpu_stats);
2486 free_percpu(priv->percpu_extras);
2493 fsl_mc_portal_free(priv->mc_io);
2495 dev_set_drvdata(dev, NULL);
2496 free_netdev(net_dev);
2501 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
2503 .vendor = FSL_MC_VENDOR_FREESCALE,
2508 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
2510 static struct fsl_mc_driver dpaa2_eth_driver = {
2512 .name = KBUILD_MODNAME,
2513 .owner = THIS_MODULE,
2515 .probe = dpaa2_eth_probe,
2516 .remove = dpaa2_eth_remove,
2517 .match_id_table = dpaa2_eth_match_id_table
2520 module_fsl_mc_driver(dpaa2_eth_driver);