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/fsl/mc.h>
15 #include <linux/bpf.h>
16 #include <linux/bpf_trace.h>
17 #include <linux/fsl/ptp_qoriq.h>
18 #include <linux/ptp_classify.h>
19 #include <net/pkt_cls.h>
22 #include "dpaa2-eth.h"
24 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
25 * using trace events only need to #include <trace/events/sched.h>
27 #define CREATE_TRACE_POINTS
28 #include "dpaa2-eth-trace.h"
30 MODULE_LICENSE("Dual BSD/GPL");
31 MODULE_AUTHOR("Freescale Semiconductor, Inc");
32 MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
34 struct ptp_qoriq *dpaa2_ptp;
35 EXPORT_SYMBOL(dpaa2_ptp);
37 static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
40 phys_addr_t phys_addr;
42 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
44 return phys_to_virt(phys_addr);
47 static void dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv *priv,
51 skb_checksum_none_assert(skb);
53 /* HW checksum validation is disabled, nothing to do here */
54 if (!(priv->net_dev->features & NETIF_F_RXCSUM))
57 /* Read checksum validation bits */
58 if (!((fd_status & DPAA2_FAS_L3CV) &&
59 (fd_status & DPAA2_FAS_L4CV)))
62 /* Inform the stack there's no need to compute L3/L4 csum anymore */
63 skb->ip_summed = CHECKSUM_UNNECESSARY;
66 /* Free a received FD.
67 * Not to be used for Tx conf FDs or on any other paths.
69 static void dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv,
70 const struct dpaa2_fd *fd,
73 struct device *dev = priv->net_dev->dev.parent;
74 dma_addr_t addr = dpaa2_fd_get_addr(fd);
75 u8 fd_format = dpaa2_fd_get_format(fd);
76 struct dpaa2_sg_entry *sgt;
80 /* If single buffer frame, just free the data buffer */
81 if (fd_format == dpaa2_fd_single)
83 else if (fd_format != dpaa2_fd_sg)
84 /* We don't support any other format */
87 /* For S/G frames, we first need to free all SG entries
88 * except the first one, which was taken care of already
90 sgt = vaddr + dpaa2_fd_get_offset(fd);
91 for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
92 addr = dpaa2_sg_get_addr(&sgt[i]);
93 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
94 dma_unmap_page(dev, addr, priv->rx_buf_size,
97 free_pages((unsigned long)sg_vaddr, 0);
98 if (dpaa2_sg_is_final(&sgt[i]))
103 free_pages((unsigned long)vaddr, 0);
106 /* Build a linear skb based on a single-buffer frame descriptor */
107 static struct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch,
108 const struct dpaa2_fd *fd,
111 struct sk_buff *skb = NULL;
112 u16 fd_offset = dpaa2_fd_get_offset(fd);
113 u32 fd_length = dpaa2_fd_get_len(fd);
117 skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
121 skb_reserve(skb, fd_offset);
122 skb_put(skb, fd_length);
127 /* Build a non linear (fragmented) skb based on a S/G table */
128 static struct sk_buff *dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv *priv,
129 struct dpaa2_eth_channel *ch,
130 struct dpaa2_sg_entry *sgt)
132 struct sk_buff *skb = NULL;
133 struct device *dev = priv->net_dev->dev.parent;
138 struct page *page, *head_page;
142 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
143 struct dpaa2_sg_entry *sge = &sgt[i];
145 /* NOTE: We only support SG entries in dpaa2_sg_single format,
146 * but this is the only format we may receive from HW anyway
149 /* Get the address and length from the S/G entry */
150 sg_addr = dpaa2_sg_get_addr(sge);
151 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
152 dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
155 sg_length = dpaa2_sg_get_len(sge);
158 /* We build the skb around the first data buffer */
159 skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
160 if (unlikely(!skb)) {
161 /* Free the first SG entry now, since we already
162 * unmapped it and obtained the virtual address
164 free_pages((unsigned long)sg_vaddr, 0);
166 /* We still need to subtract the buffers used
167 * by this FD from our software counter
169 while (!dpaa2_sg_is_final(&sgt[i]) &&
170 i < DPAA2_ETH_MAX_SG_ENTRIES)
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, priv->rx_buf_size);
196 if (dpaa2_sg_is_final(sge))
200 WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
202 /* Count all data buffers + SG table buffer */
203 ch->buf_count -= i + 2;
208 /* Free buffers acquired from the buffer pool or which were meant to
209 * be released in the pool
211 static void dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array,
214 struct device *dev = priv->net_dev->dev.parent;
218 for (i = 0; i < count; i++) {
219 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
220 dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
222 free_pages((unsigned long)vaddr, 0);
226 static void dpaa2_eth_xdp_release_buf(struct dpaa2_eth_priv *priv,
227 struct dpaa2_eth_channel *ch,
233 ch->xdp.drop_bufs[ch->xdp.drop_cnt++] = addr;
234 if (ch->xdp.drop_cnt < DPAA2_ETH_BUFS_PER_CMD)
237 while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
239 ch->xdp.drop_cnt)) == -EBUSY) {
240 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
246 dpaa2_eth_free_bufs(priv, ch->xdp.drop_bufs, ch->xdp.drop_cnt);
247 ch->buf_count -= ch->xdp.drop_cnt;
250 ch->xdp.drop_cnt = 0;
253 static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
254 struct dpaa2_eth_fq *fq,
255 struct dpaa2_eth_xdp_fds *xdp_fds)
257 int total_enqueued = 0, retries = 0, enqueued;
258 struct dpaa2_eth_drv_stats *percpu_extras;
259 int num_fds, err, max_retries;
260 struct dpaa2_fd *fds;
262 percpu_extras = this_cpu_ptr(priv->percpu_extras);
264 /* try to enqueue all the FDs until the max number of retries is hit */
266 num_fds = xdp_fds->num;
267 max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
268 while (total_enqueued < num_fds && retries < max_retries) {
269 err = priv->enqueue(priv, fq, &fds[total_enqueued],
270 0, num_fds - total_enqueued, &enqueued);
272 percpu_extras->tx_portal_busy += ++retries;
275 total_enqueued += enqueued;
279 return total_enqueued;
282 static void dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv *priv,
283 struct dpaa2_eth_channel *ch,
284 struct dpaa2_eth_fq *fq)
286 struct rtnl_link_stats64 *percpu_stats;
287 struct dpaa2_fd *fds;
290 percpu_stats = this_cpu_ptr(priv->percpu_stats);
292 // enqueue the array of XDP_TX frames
293 enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);
295 /* update statistics */
296 percpu_stats->tx_packets += enqueued;
297 fds = fq->xdp_tx_fds.fds;
298 for (i = 0; i < enqueued; i++) {
299 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
302 for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
303 dpaa2_eth_xdp_release_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
304 percpu_stats->tx_errors++;
305 ch->stats.xdp_tx_err++;
307 fq->xdp_tx_fds.num = 0;
310 static void dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv *priv,
311 struct dpaa2_eth_channel *ch,
313 void *buf_start, u16 queue_id)
315 struct dpaa2_faead *faead;
316 struct dpaa2_fd *dest_fd;
317 struct dpaa2_eth_fq *fq;
320 /* Mark the egress frame hardware annotation area as valid */
321 frc = dpaa2_fd_get_frc(fd);
322 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
323 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
325 /* Instruct hardware to release the FD buffer directly into
326 * the buffer pool once transmission is completed, instead of
327 * sending a Tx confirmation frame to us
329 ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
330 faead = dpaa2_get_faead(buf_start, false);
331 faead->ctrl = cpu_to_le32(ctrl);
332 faead->conf_fqid = 0;
334 fq = &priv->fq[queue_id];
335 dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
336 memcpy(dest_fd, fd, sizeof(*dest_fd));
338 if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
341 dpaa2_eth_xdp_tx_flush(priv, ch, fq);
344 static u32 dpaa2_eth_run_xdp(struct dpaa2_eth_priv *priv,
345 struct dpaa2_eth_channel *ch,
346 struct dpaa2_eth_fq *rx_fq,
347 struct dpaa2_fd *fd, void *vaddr)
349 dma_addr_t addr = dpaa2_fd_get_addr(fd);
350 struct bpf_prog *xdp_prog;
352 u32 xdp_act = XDP_PASS;
357 xdp_prog = READ_ONCE(ch->xdp.prog);
361 xdp.data = vaddr + dpaa2_fd_get_offset(fd);
362 xdp.data_end = xdp.data + dpaa2_fd_get_len(fd);
363 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
364 xdp_set_data_meta_invalid(&xdp);
365 xdp.rxq = &ch->xdp_rxq;
367 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE -
368 (dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM);
370 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
372 /* xdp.data pointer may have changed */
373 dpaa2_fd_set_offset(fd, xdp.data - vaddr);
374 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
380 dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
383 bpf_warn_invalid_xdp_action(xdp_act);
386 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
389 dpaa2_eth_xdp_release_buf(priv, ch, addr);
390 ch->stats.xdp_drop++;
393 dma_unmap_page(priv->net_dev->dev.parent, addr,
394 priv->rx_buf_size, DMA_BIDIRECTIONAL);
397 /* Allow redirect use of full headroom */
398 xdp.data_hard_start = vaddr;
399 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
401 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
403 ch->stats.xdp_drop++;
405 ch->stats.xdp_redirect++;
409 ch->xdp.res |= xdp_act;
415 /* Main Rx frame processing routine */
416 static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
417 struct dpaa2_eth_channel *ch,
418 const struct dpaa2_fd *fd,
419 struct dpaa2_eth_fq *fq)
421 dma_addr_t addr = dpaa2_fd_get_addr(fd);
422 u8 fd_format = dpaa2_fd_get_format(fd);
425 struct rtnl_link_stats64 *percpu_stats;
426 struct dpaa2_eth_drv_stats *percpu_extras;
427 struct device *dev = priv->net_dev->dev.parent;
428 struct dpaa2_fas *fas;
434 trace_dpaa2_rx_fd(priv->net_dev, fd);
436 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
437 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
440 fas = dpaa2_get_fas(vaddr, false);
442 buf_data = vaddr + dpaa2_fd_get_offset(fd);
445 percpu_stats = this_cpu_ptr(priv->percpu_stats);
446 percpu_extras = this_cpu_ptr(priv->percpu_extras);
448 if (fd_format == dpaa2_fd_single) {
449 xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
450 if (xdp_act != XDP_PASS) {
451 percpu_stats->rx_packets++;
452 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
456 dma_unmap_page(dev, addr, priv->rx_buf_size,
458 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
459 } else if (fd_format == dpaa2_fd_sg) {
460 WARN_ON(priv->xdp_prog);
462 dma_unmap_page(dev, addr, priv->rx_buf_size,
464 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
465 free_pages((unsigned long)vaddr, 0);
466 percpu_extras->rx_sg_frames++;
467 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
469 /* We don't support any other format */
470 goto err_frame_format;
478 /* Get the timestamp value */
479 if (priv->rx_tstamp) {
480 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
481 __le64 *ts = dpaa2_get_ts(vaddr, false);
484 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
486 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
487 shhwtstamps->hwtstamp = ns_to_ktime(ns);
490 /* Check if we need to validate the L4 csum */
491 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
492 status = le32_to_cpu(fas->status);
493 dpaa2_eth_validate_rx_csum(priv, status, skb);
496 skb->protocol = eth_type_trans(skb, priv->net_dev);
497 skb_record_rx_queue(skb, fq->flowid);
499 percpu_stats->rx_packets++;
500 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
502 list_add_tail(&skb->list, ch->rx_list);
507 dpaa2_eth_free_rx_fd(priv, fd, vaddr);
509 percpu_stats->rx_dropped++;
512 /* Consume all frames pull-dequeued into the store. This is the simplest way to
513 * make sure we don't accidentally issue another volatile dequeue which would
514 * overwrite (leak) frames already in the store.
516 * Observance of NAPI budget is not our concern, leaving that to the caller.
518 static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
519 struct dpaa2_eth_fq **src)
521 struct dpaa2_eth_priv *priv = ch->priv;
522 struct dpaa2_eth_fq *fq = NULL;
524 const struct dpaa2_fd *fd;
525 int cleaned = 0, retries = 0;
529 dq = dpaa2_io_store_next(ch->store, &is_last);
531 /* If we're here, we *must* have placed a
532 * volatile dequeue comnmand, so keep reading through
533 * the store until we get some sort of valid response
534 * token (either a valid frame or an "empty dequeue")
536 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
537 netdev_err_once(priv->net_dev,
538 "Unable to read a valid dequeue response\n");
544 fd = dpaa2_dq_fd(dq);
545 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
547 fq->consume(priv, ch, fd, fq);
555 fq->stats.frames += cleaned;
556 ch->stats.frames += cleaned;
558 /* A dequeue operation only pulls frames from a single queue
559 * into the store. Return the frame queue as an out param.
567 static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
568 u8 *msgtype, u8 *twostep, u8 *udp,
569 u16 *correction_offset,
570 u16 *origintimestamp_offset)
572 unsigned int ptp_class;
573 struct ptp_header *hdr;
577 ptp_class = ptp_classify_raw(skb);
578 if (ptp_class == PTP_CLASS_NONE)
581 hdr = ptp_parse_header(skb, ptp_class);
585 *msgtype = ptp_get_msgtype(hdr, ptp_class);
586 *twostep = hdr->flag_field[0] & 0x2;
588 type = ptp_class & PTP_CLASS_PMASK;
589 if (type == PTP_CLASS_IPV4 ||
590 type == PTP_CLASS_IPV6)
595 base = skb_mac_header(skb);
596 *correction_offset = (u8 *)&hdr->correction - base;
597 *origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
602 /* Configure the egress frame annotation for timestamp update */
603 static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
608 struct ptp_tstamp origin_timestamp;
609 struct dpni_single_step_cfg cfg;
610 u8 msgtype, twostep, udp;
611 struct dpaa2_faead *faead;
612 struct dpaa2_fas *fas;
613 struct timespec64 ts;
614 u16 offset1, offset2;
619 /* Mark the egress frame annotation area as valid */
620 frc = dpaa2_fd_get_frc(fd);
621 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
623 /* Set hardware annotation size */
624 ctrl = dpaa2_fd_get_ctrl(fd);
625 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
627 /* enable UPD (update prepanded data) bit in FAEAD field of
628 * hardware frame annotation area
630 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
631 faead = dpaa2_get_faead(buf_start, true);
632 faead->ctrl = cpu_to_le32(ctrl);
634 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
635 if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
636 &offset1, &offset2) ||
637 msgtype != 0 || twostep) {
638 WARN_ONCE(1, "Bad packet for one-step timestamping\n");
642 /* Mark the frame annotation status as valid */
643 frc = dpaa2_fd_get_frc(fd);
644 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);
646 /* Mark the PTP flag for one step timestamping */
647 fas = dpaa2_get_fas(buf_start, true);
648 fas->status = cpu_to_le32(DPAA2_FAS_PTP);
650 dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
651 ns = dpaa2_get_ts(buf_start, true);
652 *ns = cpu_to_le64(timespec64_to_ns(&ts) /
653 DPAA2_PTP_CLK_PERIOD_NS);
655 /* Update current time to PTP message originTimestamp field */
656 ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
657 data = skb_mac_header(skb);
658 *(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
659 *(__be32 *)(data + offset2 + 2) =
660 htonl(origin_timestamp.sec_lsb);
661 *(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);
665 cfg.offset = offset1;
668 if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token,
670 WARN_ONCE(1, "Failed to set single step register");
674 /* Create a frame descriptor based on a fragmented skb */
675 static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
680 struct device *dev = priv->net_dev->dev.parent;
681 void *sgt_buf = NULL;
683 int nr_frags = skb_shinfo(skb)->nr_frags;
684 struct dpaa2_sg_entry *sgt;
687 struct scatterlist *scl, *crt_scl;
690 struct dpaa2_eth_swa *swa;
692 /* Create and map scatterlist.
693 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
694 * to go beyond nr_frags+1.
695 * Note: We don't support chained scatterlists
697 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
700 scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
704 sg_init_table(scl, nr_frags + 1);
705 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
706 if (unlikely(num_sg < 0)) {
708 goto dma_map_sg_failed;
710 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
711 if (unlikely(!num_dma_bufs)) {
713 goto dma_map_sg_failed;
716 /* Prepare the HW SGT structure */
717 sgt_buf_size = priv->tx_data_offset +
718 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
719 sgt_buf = napi_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
720 if (unlikely(!sgt_buf)) {
722 goto sgt_buf_alloc_failed;
724 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
725 memset(sgt_buf, 0, sgt_buf_size);
727 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
729 /* Fill in the HW SGT structure.
731 * sgt_buf is zeroed out, so the following fields are implicit
732 * in all sgt entries:
734 * - format is 'dpaa2_sg_single'
736 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
737 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
738 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
740 dpaa2_sg_set_final(&sgt[i - 1], true);
742 /* Store the skb backpointer in the SGT buffer.
743 * Fit the scatterlist and the number of buffers alongside the
744 * skb backpointer in the software annotation area. We'll need
745 * all of them on Tx Conf.
747 *swa_addr = (void *)sgt_buf;
748 swa = (struct dpaa2_eth_swa *)sgt_buf;
749 swa->type = DPAA2_ETH_SWA_SG;
752 swa->sg.num_sg = num_sg;
753 swa->sg.sgt_size = sgt_buf_size;
755 /* Separately map the SGT buffer */
756 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
757 if (unlikely(dma_mapping_error(dev, addr))) {
759 goto dma_map_single_failed;
761 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
762 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
763 dpaa2_fd_set_addr(fd, addr);
764 dpaa2_fd_set_len(fd, skb->len);
765 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
769 dma_map_single_failed:
770 skb_free_frag(sgt_buf);
771 sgt_buf_alloc_failed:
772 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
778 /* Create a SG frame descriptor based on a linear skb.
780 * This function is used on the Tx path when the skb headroom is not large
781 * enough for the HW requirements, thus instead of realloc-ing the skb we
782 * create a SG frame descriptor with only one entry.
784 static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
789 struct device *dev = priv->net_dev->dev.parent;
790 struct dpaa2_eth_sgt_cache *sgt_cache;
791 struct dpaa2_sg_entry *sgt;
792 struct dpaa2_eth_swa *swa;
793 dma_addr_t addr, sgt_addr;
794 void *sgt_buf = NULL;
798 /* Prepare the HW SGT structure */
799 sgt_cache = this_cpu_ptr(priv->sgt_cache);
800 sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
802 if (sgt_cache->count == 0)
803 sgt_buf = kzalloc(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN,
806 sgt_buf = sgt_cache->buf[--sgt_cache->count];
807 if (unlikely(!sgt_buf))
810 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
811 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
813 addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
814 if (unlikely(dma_mapping_error(dev, addr))) {
816 goto data_map_failed;
819 /* Fill in the HW SGT structure */
820 dpaa2_sg_set_addr(sgt, addr);
821 dpaa2_sg_set_len(sgt, skb->len);
822 dpaa2_sg_set_final(sgt, true);
824 /* Store the skb backpointer in the SGT buffer */
825 *swa_addr = (void *)sgt_buf;
826 swa = (struct dpaa2_eth_swa *)sgt_buf;
827 swa->type = DPAA2_ETH_SWA_SINGLE;
828 swa->single.skb = skb;
829 swa->sg.sgt_size = sgt_buf_size;
831 /* Separately map the SGT buffer */
832 sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
833 if (unlikely(dma_mapping_error(dev, sgt_addr))) {
838 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
839 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
840 dpaa2_fd_set_addr(fd, sgt_addr);
841 dpaa2_fd_set_len(fd, skb->len);
842 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
847 dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
849 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
852 sgt_cache->buf[sgt_cache->count++] = sgt_buf;
857 /* Create a frame descriptor based on a linear skb */
858 static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
863 struct device *dev = priv->net_dev->dev.parent;
864 u8 *buffer_start, *aligned_start;
865 struct dpaa2_eth_swa *swa;
868 buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
870 /* If there's enough room to align the FD address, do it.
871 * It will help hardware optimize accesses.
873 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
874 DPAA2_ETH_TX_BUF_ALIGN);
875 if (aligned_start >= skb->head)
876 buffer_start = aligned_start;
878 /* Store a backpointer to the skb at the beginning of the buffer
879 * (in the private data area) such that we can release it
882 *swa_addr = (void *)buffer_start;
883 swa = (struct dpaa2_eth_swa *)buffer_start;
884 swa->type = DPAA2_ETH_SWA_SINGLE;
885 swa->single.skb = skb;
887 addr = dma_map_single(dev, buffer_start,
888 skb_tail_pointer(skb) - buffer_start,
890 if (unlikely(dma_mapping_error(dev, addr)))
893 dpaa2_fd_set_addr(fd, addr);
894 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
895 dpaa2_fd_set_len(fd, skb->len);
896 dpaa2_fd_set_format(fd, dpaa2_fd_single);
897 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
902 /* FD freeing routine on the Tx path
904 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
905 * back-pointed to is also freed.
906 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
909 static void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
910 struct dpaa2_eth_fq *fq,
911 const struct dpaa2_fd *fd, bool in_napi)
913 struct device *dev = priv->net_dev->dev.parent;
914 dma_addr_t fd_addr, sg_addr;
915 struct sk_buff *skb = NULL;
916 unsigned char *buffer_start;
917 struct dpaa2_eth_swa *swa;
918 u8 fd_format = dpaa2_fd_get_format(fd);
919 u32 fd_len = dpaa2_fd_get_len(fd);
921 struct dpaa2_eth_sgt_cache *sgt_cache;
922 struct dpaa2_sg_entry *sgt;
924 fd_addr = dpaa2_fd_get_addr(fd);
925 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
926 swa = (struct dpaa2_eth_swa *)buffer_start;
928 if (fd_format == dpaa2_fd_single) {
929 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
930 skb = swa->single.skb;
931 /* Accessing the skb buffer is safe before dma unmap,
932 * because we didn't map the actual skb shell.
934 dma_unmap_single(dev, fd_addr,
935 skb_tail_pointer(skb) - buffer_start,
938 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
939 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
942 } else if (fd_format == dpaa2_fd_sg) {
943 if (swa->type == DPAA2_ETH_SWA_SG) {
946 /* Unmap the scatterlist */
947 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
951 /* Unmap the SGT buffer */
952 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
955 skb = swa->single.skb;
957 /* Unmap the SGT Buffer */
958 dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
961 sgt = (struct dpaa2_sg_entry *)(buffer_start +
962 priv->tx_data_offset);
963 sg_addr = dpaa2_sg_get_addr(sgt);
964 dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
967 netdev_dbg(priv->net_dev, "Invalid FD format\n");
971 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
973 fq->dq_bytes += fd_len;
976 if (swa->type == DPAA2_ETH_SWA_XDP) {
977 xdp_return_frame(swa->xdp.xdpf);
981 /* Get the timestamp value */
982 if (skb->cb[0] == TX_TSTAMP) {
983 struct skb_shared_hwtstamps shhwtstamps;
984 __le64 *ts = dpaa2_get_ts(buffer_start, true);
987 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
989 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
990 shhwtstamps.hwtstamp = ns_to_ktime(ns);
991 skb_tstamp_tx(skb, &shhwtstamps);
992 } else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
993 mutex_unlock(&priv->onestep_tstamp_lock);
996 /* Free SGT buffer allocated on tx */
997 if (fd_format != dpaa2_fd_single) {
998 sgt_cache = this_cpu_ptr(priv->sgt_cache);
999 if (swa->type == DPAA2_ETH_SWA_SG) {
1000 skb_free_frag(buffer_start);
1002 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
1003 kfree(buffer_start);
1005 sgt_cache->buf[sgt_cache->count++] = buffer_start;
1009 /* Move on with skb release */
1010 napi_consume_skb(skb, in_napi);
1013 static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1014 struct net_device *net_dev)
1016 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1018 struct rtnl_link_stats64 *percpu_stats;
1019 struct dpaa2_eth_drv_stats *percpu_extras;
1020 struct dpaa2_eth_fq *fq;
1021 struct netdev_queue *nq;
1023 unsigned int needed_headroom;
1029 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1030 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1032 needed_headroom = dpaa2_eth_needed_headroom(skb);
1034 /* We'll be holding a back-reference to the skb until Tx Confirmation;
1035 * we don't want that overwritten by a concurrent Tx with a cloned skb.
1037 skb = skb_unshare(skb, GFP_ATOMIC);
1038 if (unlikely(!skb)) {
1039 /* skb_unshare() has already freed the skb */
1040 percpu_stats->tx_dropped++;
1041 return NETDEV_TX_OK;
1044 /* Setup the FD fields */
1045 memset(&fd, 0, sizeof(fd));
1047 if (skb_is_nonlinear(skb)) {
1048 err = dpaa2_eth_build_sg_fd(priv, skb, &fd, &swa);
1049 percpu_extras->tx_sg_frames++;
1050 percpu_extras->tx_sg_bytes += skb->len;
1051 } else if (skb_headroom(skb) < needed_headroom) {
1052 err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, &fd, &swa);
1053 percpu_extras->tx_sg_frames++;
1054 percpu_extras->tx_sg_bytes += skb->len;
1055 percpu_extras->tx_converted_sg_frames++;
1056 percpu_extras->tx_converted_sg_bytes += skb->len;
1058 err = dpaa2_eth_build_single_fd(priv, skb, &fd, &swa);
1061 if (unlikely(err)) {
1062 percpu_stats->tx_dropped++;
1067 dpaa2_eth_enable_tx_tstamp(priv, &fd, swa, skb);
1070 trace_dpaa2_tx_fd(net_dev, &fd);
1072 /* TxConf FQ selection relies on queue id from the stack.
1073 * In case of a forwarded frame from another DPNI interface, we choose
1074 * a queue affined to the same core that processed the Rx frame
1076 queue_mapping = skb_get_queue_mapping(skb);
1078 if (net_dev->num_tc) {
1079 prio = netdev_txq_to_tc(net_dev, queue_mapping);
1080 /* Hardware interprets priority level 0 as being the highest,
1081 * so we need to do a reverse mapping to the netdev tc index
1083 prio = net_dev->num_tc - prio - 1;
1084 /* We have only one FQ array entry for all Tx hardware queues
1085 * with the same flow id (but different priority levels)
1087 queue_mapping %= dpaa2_eth_queue_count(priv);
1089 fq = &priv->fq[queue_mapping];
1091 fd_len = dpaa2_fd_get_len(&fd);
1092 nq = netdev_get_tx_queue(net_dev, queue_mapping);
1093 netdev_tx_sent_queue(nq, fd_len);
1095 /* Everything that happens after this enqueues might race with
1096 * the Tx confirmation callback for this frame
1098 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
1099 err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
1103 percpu_extras->tx_portal_busy += i;
1104 if (unlikely(err < 0)) {
1105 percpu_stats->tx_errors++;
1106 /* Clean up everything, including freeing the skb */
1107 dpaa2_eth_free_tx_fd(priv, fq, &fd, false);
1108 netdev_tx_completed_queue(nq, 1, fd_len);
1110 percpu_stats->tx_packets++;
1111 percpu_stats->tx_bytes += fd_len;
1114 return NETDEV_TX_OK;
1119 return NETDEV_TX_OK;
1122 static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1124 struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1126 struct sk_buff *skb;
1129 skb = skb_dequeue(&priv->tx_skbs);
1133 /* Lock just before TX one-step timestamping packet,
1134 * and release the lock in dpaa2_eth_free_tx_fd when
1135 * confirm the packet has been sent on hardware, or
1136 * when clean up during transmit failure.
1138 mutex_lock(&priv->onestep_tstamp_lock);
1139 __dpaa2_eth_tx(skb, priv->net_dev);
1143 static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1145 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1146 u8 msgtype, twostep, udp;
1147 u16 offset1, offset2;
1149 /* Utilize skb->cb[0] for timestamping request per skb */
1152 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1153 if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1154 skb->cb[0] = TX_TSTAMP;
1155 else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1156 skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1159 /* TX for one-step timestamping PTP Sync packet */
1160 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1161 if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1162 &offset1, &offset2))
1163 if (msgtype == 0 && twostep == 0) {
1164 skb_queue_tail(&priv->tx_skbs, skb);
1165 queue_work(priv->dpaa2_ptp_wq,
1166 &priv->tx_onestep_tstamp);
1167 return NETDEV_TX_OK;
1169 /* Use two-step timestamping if not one-step timestamping
1172 skb->cb[0] = TX_TSTAMP;
1175 /* TX for other packets */
1176 return __dpaa2_eth_tx(skb, net_dev);
1179 /* Tx confirmation frame processing routine */
1180 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
1181 struct dpaa2_eth_channel *ch __always_unused,
1182 const struct dpaa2_fd *fd,
1183 struct dpaa2_eth_fq *fq)
1185 struct rtnl_link_stats64 *percpu_stats;
1186 struct dpaa2_eth_drv_stats *percpu_extras;
1187 u32 fd_len = dpaa2_fd_get_len(fd);
1191 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1193 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1194 percpu_extras->tx_conf_frames++;
1195 percpu_extras->tx_conf_bytes += fd_len;
1197 /* Check frame errors in the FD field */
1198 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
1199 dpaa2_eth_free_tx_fd(priv, fq, fd, true);
1201 if (likely(!fd_errors))
1204 if (net_ratelimit())
1205 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1208 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1209 /* Tx-conf logically pertains to the egress path. */
1210 percpu_stats->tx_errors++;
1213 static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
1217 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1218 DPNI_OFF_RX_L3_CSUM, enable);
1220 netdev_err(priv->net_dev,
1221 "dpni_set_offload(RX_L3_CSUM) failed\n");
1225 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1226 DPNI_OFF_RX_L4_CSUM, enable);
1228 netdev_err(priv->net_dev,
1229 "dpni_set_offload(RX_L4_CSUM) failed\n");
1236 static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
1240 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1241 DPNI_OFF_TX_L3_CSUM, enable);
1243 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1247 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1248 DPNI_OFF_TX_L4_CSUM, enable);
1250 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1257 /* Perform a single release command to add buffers
1258 * to the specified buffer pool
1260 static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1261 struct dpaa2_eth_channel *ch, u16 bpid)
1263 struct device *dev = priv->net_dev->dev.parent;
1264 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1270 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1271 /* Allocate buffer visible to WRIOP + skb shared info +
1274 /* allocate one page for each Rx buffer. WRIOP sees
1275 * the entire page except for a tailroom reserved for
1278 page = dev_alloc_pages(0);
1282 addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
1284 if (unlikely(dma_mapping_error(dev, addr)))
1287 buf_array[i] = addr;
1290 trace_dpaa2_eth_buf_seed(priv->net_dev,
1291 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
1292 addr, priv->rx_buf_size,
1297 /* In case the portal is busy, retry until successful */
1298 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
1299 buf_array, i)) == -EBUSY) {
1300 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1305 /* If release command failed, clean up and bail out;
1306 * not much else we can do about it
1309 dpaa2_eth_free_bufs(priv, buf_array, i);
1316 __free_pages(page, 0);
1318 /* If we managed to allocate at least some buffers,
1319 * release them to hardware
1327 static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
1332 for (j = 0; j < priv->num_channels; j++) {
1333 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1334 i += DPAA2_ETH_BUFS_PER_CMD) {
1335 new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid);
1336 priv->channel[j]->buf_count += new_count;
1338 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
1348 * Drain the specified number of buffers from the DPNI's private buffer pool.
1349 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1351 static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count)
1353 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1358 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
1361 if (ret == -EBUSY &&
1362 retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
1364 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1367 dpaa2_eth_free_bufs(priv, buf_array, ret);
1372 static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv)
1376 dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1377 dpaa2_eth_drain_bufs(priv, 1);
1379 for (i = 0; i < priv->num_channels; i++)
1380 priv->channel[i]->buf_count = 0;
1383 /* Function is called from softirq context only, so we don't need to guard
1384 * the access to percpu count
1386 static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1387 struct dpaa2_eth_channel *ch,
1392 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1396 new_count = dpaa2_eth_add_bufs(priv, ch, bpid);
1397 if (unlikely(!new_count)) {
1398 /* Out of memory; abort for now, we'll try later on */
1401 ch->buf_count += new_count;
1402 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1404 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1410 static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1412 struct dpaa2_eth_sgt_cache *sgt_cache;
1416 for_each_possible_cpu(k) {
1417 sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1418 count = sgt_cache->count;
1420 for (i = 0; i < count; i++)
1421 kfree(sgt_cache->buf[i]);
1422 sgt_cache->count = 0;
1426 static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
1431 /* Retry while portal is busy */
1433 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1437 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
1439 ch->stats.dequeue_portal_busy += dequeues;
1441 ch->stats.pull_err++;
1446 /* NAPI poll routine
1448 * Frames are dequeued from the QMan channel associated with this NAPI context.
1449 * Rx, Tx confirmation and (if configured) Rx error frames all count
1450 * towards the NAPI budget.
1452 static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1454 struct dpaa2_eth_channel *ch;
1455 struct dpaa2_eth_priv *priv;
1456 int rx_cleaned = 0, txconf_cleaned = 0;
1457 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1458 struct netdev_queue *nq;
1459 int store_cleaned, work_done;
1460 struct list_head rx_list;
1465 ch = container_of(napi, struct dpaa2_eth_channel, napi);
1469 INIT_LIST_HEAD(&rx_list);
1470 ch->rx_list = &rx_list;
1473 err = dpaa2_eth_pull_channel(ch);
1477 /* Refill pool if appropriate */
1478 dpaa2_eth_refill_pool(priv, ch, priv->bpid);
1480 store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
1481 if (store_cleaned <= 0)
1483 if (fq->type == DPAA2_RX_FQ) {
1484 rx_cleaned += store_cleaned;
1485 flowid = fq->flowid;
1487 txconf_cleaned += store_cleaned;
1488 /* We have a single Tx conf FQ on this channel */
1492 /* If we either consumed the whole NAPI budget with Rx frames
1493 * or we reached the Tx confirmations threshold, we're done.
1495 if (rx_cleaned >= budget ||
1496 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1500 } while (store_cleaned);
1502 /* We didn't consume the entire budget, so finish napi and
1503 * re-enable data availability notifications
1505 napi_complete_done(napi, rx_cleaned);
1507 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1509 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
1510 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1511 ch->nctx.desired_cpu);
1513 work_done = max(rx_cleaned, 1);
1516 netif_receive_skb_list(ch->rx_list);
1518 if (txc_fq && txc_fq->dq_frames) {
1519 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1520 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1522 txc_fq->dq_frames = 0;
1523 txc_fq->dq_bytes = 0;
1526 if (ch->xdp.res & XDP_REDIRECT)
1528 else if (rx_cleaned && ch->xdp.res & XDP_TX)
1529 dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
1534 static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
1536 struct dpaa2_eth_channel *ch;
1539 for (i = 0; i < priv->num_channels; i++) {
1540 ch = priv->channel[i];
1541 napi_enable(&ch->napi);
1545 static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
1547 struct dpaa2_eth_channel *ch;
1550 for (i = 0; i < priv->num_channels; i++) {
1551 ch = priv->channel[i];
1552 napi_disable(&ch->napi);
1556 void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1557 bool tx_pause, bool pfc)
1559 struct dpni_taildrop td = {0};
1560 struct dpaa2_eth_fq *fq;
1563 /* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1564 * flow control is disabled (as it might interfere with either the
1565 * buffer pool depletion trigger for pause frames or with the group
1566 * congestion trigger for PFC frames)
1568 td.enable = !tx_pause;
1569 if (priv->rx_fqtd_enabled == td.enable)
1572 td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1573 td.units = DPNI_CONGESTION_UNIT_BYTES;
1575 for (i = 0; i < priv->num_fqs; i++) {
1577 if (fq->type != DPAA2_RX_FQ)
1579 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1580 DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1581 fq->tc, fq->flowid, &td);
1583 netdev_err(priv->net_dev,
1584 "dpni_set_taildrop(FQ) failed\n");
1589 priv->rx_fqtd_enabled = td.enable;
1592 /* Congestion group taildrop: threshold is in frames, per group
1593 * of FQs belonging to the same traffic class
1594 * Enabled if general Tx pause disabled or if PFCs are enabled
1595 * (congestion group threhsold for PFC generation is lower than the
1596 * CG taildrop threshold, so it won't interfere with it; we also
1597 * want frames in non-PFC enabled traffic classes to be kept in check)
1599 td.enable = !tx_pause || (tx_pause && pfc);
1600 if (priv->rx_cgtd_enabled == td.enable)
1603 td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
1604 td.units = DPNI_CONGESTION_UNIT_FRAMES;
1605 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
1606 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1607 DPNI_CP_GROUP, DPNI_QUEUE_RX,
1610 netdev_err(priv->net_dev,
1611 "dpni_set_taildrop(CG) failed\n");
1616 priv->rx_cgtd_enabled = td.enable;
1619 static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
1621 struct dpni_link_state state = {0};
1625 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1626 if (unlikely(err)) {
1627 netdev_err(priv->net_dev,
1628 "dpni_get_link_state() failed\n");
1632 /* If Tx pause frame settings have changed, we need to update
1633 * Rx FQ taildrop configuration as well. We configure taildrop
1634 * only when pause frame generation is disabled.
1636 tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
1637 dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
1639 /* When we manage the MAC/PHY using phylink there is no need
1640 * to manually update the netif_carrier.
1645 /* Chech link state; speed / duplex changes are not treated yet */
1646 if (priv->link_state.up == state.up)
1650 netif_carrier_on(priv->net_dev);
1651 netif_tx_start_all_queues(priv->net_dev);
1653 netif_tx_stop_all_queues(priv->net_dev);
1654 netif_carrier_off(priv->net_dev);
1657 netdev_info(priv->net_dev, "Link Event: state %s\n",
1658 state.up ? "up" : "down");
1661 priv->link_state = state;
1666 static int dpaa2_eth_open(struct net_device *net_dev)
1668 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1671 err = dpaa2_eth_seed_pool(priv, priv->bpid);
1673 /* Not much to do; the buffer pool, though not filled up,
1674 * may still contain some buffers which would enable us
1677 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
1678 priv->dpbp_dev->obj_desc.id, priv->bpid);
1682 /* We'll only start the txqs when the link is actually ready;
1683 * make sure we don't race against the link up notification,
1684 * which may come immediately after dpni_enable();
1686 netif_tx_stop_all_queues(net_dev);
1688 /* Also, explicitly set carrier off, otherwise
1689 * netif_carrier_ok() will return true and cause 'ip link show'
1690 * to report the LOWER_UP flag, even though the link
1691 * notification wasn't even received.
1693 netif_carrier_off(net_dev);
1695 dpaa2_eth_enable_ch_napi(priv);
1697 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1699 netdev_err(net_dev, "dpni_enable() failed\n");
1704 /* If the DPMAC object has already processed the link up
1705 * interrupt, we have to learn the link state ourselves.
1707 err = dpaa2_eth_link_state_update(priv);
1709 netdev_err(net_dev, "Can't update link state\n");
1710 goto link_state_err;
1713 phylink_start(priv->mac->phylink);
1720 dpaa2_eth_disable_ch_napi(priv);
1721 dpaa2_eth_drain_pool(priv);
1725 /* Total number of in-flight frames on ingress queues */
1726 static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
1728 struct dpaa2_eth_fq *fq;
1729 u32 fcnt = 0, bcnt = 0, total = 0;
1732 for (i = 0; i < priv->num_fqs; i++) {
1734 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1736 netdev_warn(priv->net_dev, "query_fq_count failed");
1745 static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
1751 pending = dpaa2_eth_ingress_fq_count(priv);
1754 } while (pending && --retries);
1757 #define DPNI_TX_PENDING_VER_MAJOR 7
1758 #define DPNI_TX_PENDING_VER_MINOR 13
1759 static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
1761 union dpni_statistics stats;
1765 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1766 DPNI_TX_PENDING_VER_MINOR) < 0)
1770 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1774 if (stats.page_6.tx_pending_frames == 0)
1776 } while (--retries);
1782 static int dpaa2_eth_stop(struct net_device *net_dev)
1784 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1785 int dpni_enabled = 0;
1789 netif_tx_stop_all_queues(net_dev);
1790 netif_carrier_off(net_dev);
1792 phylink_stop(priv->mac->phylink);
1795 /* On dpni_disable(), the MC firmware will:
1796 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1797 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1798 * of all in flight Tx frames is finished (and corresponding Tx conf
1799 * frames are enqueued back to software)
1801 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1802 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1803 * and Tx conf queues are consumed on NAPI poll.
1805 dpaa2_eth_wait_for_egress_fq_empty(priv);
1808 dpni_disable(priv->mc_io, 0, priv->mc_token);
1809 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1811 /* Allow the hardware some slack */
1813 } while (dpni_enabled && --retries);
1815 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1816 /* Must go on and disable NAPI nonetheless, so we don't crash at
1817 * the next "ifconfig up"
1821 dpaa2_eth_wait_for_ingress_fq_empty(priv);
1822 dpaa2_eth_disable_ch_napi(priv);
1824 /* Empty the buffer pool */
1825 dpaa2_eth_drain_pool(priv);
1827 /* Empty the Scatter-Gather Buffer cache */
1828 dpaa2_eth_sgt_cache_drain(priv);
1833 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1835 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1836 struct device *dev = net_dev->dev.parent;
1839 err = eth_mac_addr(net_dev, addr);
1841 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1845 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1848 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1855 /** Fill in counters maintained by the GPP driver. These may be different from
1856 * the hardware counters obtained by ethtool.
1858 static void dpaa2_eth_get_stats(struct net_device *net_dev,
1859 struct rtnl_link_stats64 *stats)
1861 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1862 struct rtnl_link_stats64 *percpu_stats;
1864 u64 *netstats = (u64 *)stats;
1866 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1868 for_each_possible_cpu(i) {
1869 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1870 cpustats = (u64 *)percpu_stats;
1871 for (j = 0; j < num; j++)
1872 netstats[j] += cpustats[j];
1876 /* Copy mac unicast addresses from @net_dev to @priv.
1877 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1879 static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
1880 struct dpaa2_eth_priv *priv)
1882 struct netdev_hw_addr *ha;
1885 netdev_for_each_uc_addr(ha, net_dev) {
1886 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1889 netdev_warn(priv->net_dev,
1890 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1895 /* Copy mac multicast addresses from @net_dev to @priv
1896 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1898 static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
1899 struct dpaa2_eth_priv *priv)
1901 struct netdev_hw_addr *ha;
1904 netdev_for_each_mc_addr(ha, net_dev) {
1905 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1908 netdev_warn(priv->net_dev,
1909 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1914 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1916 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1917 int uc_count = netdev_uc_count(net_dev);
1918 int mc_count = netdev_mc_count(net_dev);
1919 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1920 u32 options = priv->dpni_attrs.options;
1921 u16 mc_token = priv->mc_token;
1922 struct fsl_mc_io *mc_io = priv->mc_io;
1925 /* Basic sanity checks; these probably indicate a misconfiguration */
1926 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1927 netdev_info(net_dev,
1928 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1931 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1932 if (uc_count > max_mac) {
1933 netdev_info(net_dev,
1934 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1938 if (mc_count + uc_count > max_mac) {
1939 netdev_info(net_dev,
1940 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1941 uc_count + mc_count, max_mac);
1942 goto force_mc_promisc;
1945 /* Adjust promisc settings due to flag combinations */
1946 if (net_dev->flags & IFF_PROMISC)
1948 if (net_dev->flags & IFF_ALLMULTI) {
1949 /* First, rebuild unicast filtering table. This should be done
1950 * in promisc mode, in order to avoid frame loss while we
1951 * progressively add entries to the table.
1952 * We don't know whether we had been in promisc already, and
1953 * making an MC call to find out is expensive; so set uc promisc
1956 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1958 netdev_warn(net_dev, "Can't set uc promisc\n");
1960 /* Actual uc table reconstruction. */
1961 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1963 netdev_warn(net_dev, "Can't clear uc filters\n");
1964 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
1966 /* Finally, clear uc promisc and set mc promisc as requested. */
1967 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1969 netdev_warn(net_dev, "Can't clear uc promisc\n");
1970 goto force_mc_promisc;
1973 /* Neither unicast, nor multicast promisc will be on... eventually.
1974 * For now, rebuild mac filtering tables while forcing both of them on.
1976 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1978 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1979 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1981 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1983 /* Actual mac filtering tables reconstruction */
1984 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1986 netdev_warn(net_dev, "Can't clear mac filters\n");
1987 dpaa2_eth_add_mc_hw_addr(net_dev, priv);
1988 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
1990 /* Now we can clear both ucast and mcast promisc, without risking
1991 * to drop legitimate frames anymore.
1993 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1995 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1996 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1998 netdev_warn(net_dev, "Can't clear mcast promisc\n");
2003 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2005 netdev_warn(net_dev, "Can't set ucast promisc\n");
2007 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2009 netdev_warn(net_dev, "Can't set mcast promisc\n");
2012 static int dpaa2_eth_set_features(struct net_device *net_dev,
2013 netdev_features_t features)
2015 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2016 netdev_features_t changed = features ^ net_dev->features;
2020 if (changed & NETIF_F_RXCSUM) {
2021 enable = !!(features & NETIF_F_RXCSUM);
2022 err = dpaa2_eth_set_rx_csum(priv, enable);
2027 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2028 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
2029 err = dpaa2_eth_set_tx_csum(priv, enable);
2037 static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2039 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2040 struct hwtstamp_config config;
2045 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
2048 switch (config.tx_type) {
2049 case HWTSTAMP_TX_OFF:
2050 case HWTSTAMP_TX_ON:
2051 case HWTSTAMP_TX_ONESTEP_SYNC:
2052 priv->tx_tstamp_type = config.tx_type;
2058 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
2059 priv->rx_tstamp = false;
2061 priv->rx_tstamp = true;
2062 /* TS is set for all frame types, not only those requested */
2063 config.rx_filter = HWTSTAMP_FILTER_ALL;
2066 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
2070 static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2072 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2074 if (cmd == SIOCSHWTSTAMP)
2075 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
2078 return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2083 static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2085 int mfl, linear_mfl;
2087 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2088 linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
2089 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
2091 if (mfl > linear_mfl) {
2092 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2093 linear_mfl - VLAN_ETH_HLEN);
2100 static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
2104 /* We enforce a maximum Rx frame length based on MTU only if we have
2105 * an XDP program attached (in order to avoid Rx S/G frames).
2106 * Otherwise, we accept all incoming frames as long as they are not
2107 * larger than maximum size supported in hardware
2110 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2112 mfl = DPAA2_ETH_MFL;
2114 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2116 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2123 static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2125 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2128 if (!priv->xdp_prog)
2131 if (!xdp_mtu_valid(priv, new_mtu))
2134 err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
2143 static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
2145 struct dpni_buffer_layout buf_layout = {0};
2148 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2149 DPNI_QUEUE_RX, &buf_layout);
2151 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2155 /* Reserve extra headroom for XDP header size changes */
2156 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2157 (has_xdp ? XDP_PACKET_HEADROOM : 0);
2158 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2159 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2160 DPNI_QUEUE_RX, &buf_layout);
2162 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2169 static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
2171 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2172 struct dpaa2_eth_channel *ch;
2173 struct bpf_prog *old;
2174 bool up, need_update;
2177 if (prog && !xdp_mtu_valid(priv, dev->mtu))
2181 bpf_prog_add(prog, priv->num_channels);
2183 up = netif_running(dev);
2184 need_update = (!!priv->xdp_prog != !!prog);
2187 dpaa2_eth_stop(dev);
2189 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2190 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2191 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2192 * so we are sure no old format buffers will be used from now on.
2195 err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
2198 err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
2203 old = xchg(&priv->xdp_prog, prog);
2207 for (i = 0; i < priv->num_channels; i++) {
2208 ch = priv->channel[i];
2209 old = xchg(&ch->xdp.prog, prog);
2215 err = dpaa2_eth_open(dev);
2224 bpf_prog_sub(prog, priv->num_channels);
2226 dpaa2_eth_open(dev);
2231 static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2233 switch (xdp->command) {
2234 case XDP_SETUP_PROG:
2235 return dpaa2_eth_setup_xdp(dev, xdp->prog);
2243 static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2244 struct xdp_frame *xdpf,
2245 struct dpaa2_fd *fd)
2247 struct device *dev = net_dev->dev.parent;
2248 unsigned int needed_headroom;
2249 struct dpaa2_eth_swa *swa;
2250 void *buffer_start, *aligned_start;
2253 /* We require a minimum headroom to be able to transmit the frame.
2254 * Otherwise return an error and let the original net_device handle it
2256 needed_headroom = dpaa2_eth_needed_headroom(NULL);
2257 if (xdpf->headroom < needed_headroom)
2260 /* Setup the FD fields */
2261 memset(fd, 0, sizeof(*fd));
2263 /* Align FD address, if possible */
2264 buffer_start = xdpf->data - needed_headroom;
2265 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2266 DPAA2_ETH_TX_BUF_ALIGN);
2267 if (aligned_start >= xdpf->data - xdpf->headroom)
2268 buffer_start = aligned_start;
2270 swa = (struct dpaa2_eth_swa *)buffer_start;
2271 /* fill in necessary fields here */
2272 swa->type = DPAA2_ETH_SWA_XDP;
2273 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2274 swa->xdp.xdpf = xdpf;
2276 addr = dma_map_single(dev, buffer_start,
2279 if (unlikely(dma_mapping_error(dev, addr)))
2282 dpaa2_fd_set_addr(fd, addr);
2283 dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2284 dpaa2_fd_set_len(fd, xdpf->len);
2285 dpaa2_fd_set_format(fd, dpaa2_fd_single);
2286 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
2291 static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2292 struct xdp_frame **frames, u32 flags)
2294 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2295 struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
2296 struct rtnl_link_stats64 *percpu_stats;
2297 struct dpaa2_eth_fq *fq;
2298 struct dpaa2_fd *fds;
2299 int enqueued, i, err;
2301 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2304 if (!netif_running(net_dev))
2307 fq = &priv->fq[smp_processor_id()];
2308 xdp_redirect_fds = &fq->xdp_redirect_fds;
2309 fds = xdp_redirect_fds->fds;
2311 percpu_stats = this_cpu_ptr(priv->percpu_stats);
2313 /* create a FD for each xdp_frame in the list received */
2314 for (i = 0; i < n; i++) {
2315 err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2319 xdp_redirect_fds->num = i;
2321 /* enqueue all the frame descriptors */
2322 enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
2324 /* update statistics */
2325 percpu_stats->tx_packets += enqueued;
2326 for (i = 0; i < enqueued; i++)
2327 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
2328 for (i = enqueued; i < n; i++)
2329 xdp_return_frame_rx_napi(frames[i]);
2334 static int update_xps(struct dpaa2_eth_priv *priv)
2336 struct net_device *net_dev = priv->net_dev;
2337 struct cpumask xps_mask;
2338 struct dpaa2_eth_fq *fq;
2339 int i, num_queues, netdev_queues;
2342 num_queues = dpaa2_eth_queue_count(priv);
2343 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
2345 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2346 * queues, so only process those
2348 for (i = 0; i < netdev_queues; i++) {
2349 fq = &priv->fq[i % num_queues];
2351 cpumask_clear(&xps_mask);
2352 cpumask_set_cpu(fq->target_cpu, &xps_mask);
2354 err = netif_set_xps_queue(net_dev, &xps_mask, i);
2356 netdev_warn_once(net_dev, "Error setting XPS queue\n");
2364 static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2365 struct tc_mqprio_qopt *mqprio)
2367 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2368 u8 num_tc, num_queues;
2371 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2372 num_queues = dpaa2_eth_queue_count(priv);
2373 num_tc = mqprio->num_tc;
2375 if (num_tc == net_dev->num_tc)
2378 if (num_tc > dpaa2_eth_tc_count(priv)) {
2379 netdev_err(net_dev, "Max %d traffic classes supported\n",
2380 dpaa2_eth_tc_count(priv));
2385 netdev_reset_tc(net_dev);
2386 netif_set_real_num_tx_queues(net_dev, num_queues);
2390 netdev_set_num_tc(net_dev, num_tc);
2391 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2393 for (i = 0; i < num_tc; i++)
2394 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2402 #define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2404 static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2406 struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2407 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2408 struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2409 struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2412 if (p->command == TC_TBF_STATS)
2415 /* Only per port Tx shaping */
2416 if (p->parent != TC_H_ROOT)
2419 if (p->command == TC_TBF_REPLACE) {
2420 if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2421 netdev_err(net_dev, "burst size cannot be greater than %d\n",
2422 DPAA2_ETH_MAX_BURST_SIZE);
2426 tx_cr_shaper.max_burst_size = cfg->max_size;
2427 /* The TBF interface is in bytes/s, whereas DPAA2 expects the
2430 tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2433 err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2436 netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2443 static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2444 enum tc_setup_type type, void *type_data)
2447 case TC_SETUP_QDISC_MQPRIO:
2448 return dpaa2_eth_setup_mqprio(net_dev, type_data);
2449 case TC_SETUP_QDISC_TBF:
2450 return dpaa2_eth_setup_tbf(net_dev, type_data);
2456 static const struct net_device_ops dpaa2_eth_ops = {
2457 .ndo_open = dpaa2_eth_open,
2458 .ndo_start_xmit = dpaa2_eth_tx,
2459 .ndo_stop = dpaa2_eth_stop,
2460 .ndo_set_mac_address = dpaa2_eth_set_addr,
2461 .ndo_get_stats64 = dpaa2_eth_get_stats,
2462 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2463 .ndo_set_features = dpaa2_eth_set_features,
2464 .ndo_do_ioctl = dpaa2_eth_ioctl,
2465 .ndo_change_mtu = dpaa2_eth_change_mtu,
2466 .ndo_bpf = dpaa2_eth_xdp,
2467 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
2468 .ndo_setup_tc = dpaa2_eth_setup_tc,
2471 static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
2473 struct dpaa2_eth_channel *ch;
2475 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
2477 /* Update NAPI statistics */
2480 napi_schedule(&ch->napi);
2483 /* Allocate and configure a DPCON object */
2484 static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
2486 struct fsl_mc_device *dpcon;
2487 struct device *dev = priv->net_dev->dev.parent;
2490 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2491 FSL_MC_POOL_DPCON, &dpcon);
2494 err = -EPROBE_DEFER;
2496 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2497 return ERR_PTR(err);
2500 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2502 dev_err(dev, "dpcon_open() failed\n");
2506 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2508 dev_err(dev, "dpcon_reset() failed\n");
2512 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2514 dev_err(dev, "dpcon_enable() failed\n");
2521 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2523 fsl_mc_object_free(dpcon);
2525 return ERR_PTR(err);
2528 static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
2529 struct fsl_mc_device *dpcon)
2531 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2532 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2533 fsl_mc_object_free(dpcon);
2536 static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
2538 struct dpaa2_eth_channel *channel;
2539 struct dpcon_attr attr;
2540 struct device *dev = priv->net_dev->dev.parent;
2543 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2547 channel->dpcon = dpaa2_eth_setup_dpcon(priv);
2548 if (IS_ERR(channel->dpcon)) {
2549 err = PTR_ERR(channel->dpcon);
2553 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2556 dev_err(dev, "dpcon_get_attributes() failed\n");
2560 channel->dpcon_id = attr.id;
2561 channel->ch_id = attr.qbman_ch_id;
2562 channel->priv = priv;
2567 dpaa2_eth_free_dpcon(priv, channel->dpcon);
2570 return ERR_PTR(err);
2573 static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
2574 struct dpaa2_eth_channel *channel)
2576 dpaa2_eth_free_dpcon(priv, channel->dpcon);
2580 /* DPIO setup: allocate and configure QBMan channels, setup core affinity
2581 * and register data availability notifications
2583 static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
2585 struct dpaa2_io_notification_ctx *nctx;
2586 struct dpaa2_eth_channel *channel;
2587 struct dpcon_notification_cfg dpcon_notif_cfg;
2588 struct device *dev = priv->net_dev->dev.parent;
2591 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2592 * many cores as possible, so we need one channel for each core
2593 * (unless there's fewer queues than cores, in which case the extra
2594 * channels would be wasted).
2595 * Allocate one channel per core and register it to the core's
2596 * affine DPIO. If not enough channels are available for all cores
2597 * or if some cores don't have an affine DPIO, there will be no
2598 * ingress frame processing on those cores.
2600 cpumask_clear(&priv->dpio_cpumask);
2601 for_each_online_cpu(i) {
2602 /* Try to allocate a channel */
2603 channel = dpaa2_eth_alloc_channel(priv);
2604 if (IS_ERR_OR_NULL(channel)) {
2605 err = PTR_ERR_OR_ZERO(channel);
2606 if (err != -EPROBE_DEFER)
2608 "No affine channel for cpu %d and above\n", i);
2612 priv->channel[priv->num_channels] = channel;
2614 nctx = &channel->nctx;
2616 nctx->cb = dpaa2_eth_cdan_cb;
2617 nctx->id = channel->ch_id;
2618 nctx->desired_cpu = i;
2620 /* Register the new context */
2621 channel->dpio = dpaa2_io_service_select(i);
2622 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
2624 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
2625 /* If no affine DPIO for this core, there's probably
2626 * none available for next cores either. Signal we want
2627 * to retry later, in case the DPIO devices weren't
2630 err = -EPROBE_DEFER;
2631 goto err_service_reg;
2634 /* Register DPCON notification with MC */
2635 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2636 dpcon_notif_cfg.priority = 0;
2637 dpcon_notif_cfg.user_ctx = nctx->qman64;
2638 err = dpcon_set_notification(priv->mc_io, 0,
2639 channel->dpcon->mc_handle,
2642 dev_err(dev, "dpcon_set_notification failed()\n");
2646 /* If we managed to allocate a channel and also found an affine
2647 * DPIO for this core, add it to the final mask
2649 cpumask_set_cpu(i, &priv->dpio_cpumask);
2650 priv->num_channels++;
2652 /* Stop if we already have enough channels to accommodate all
2653 * RX and TX conf queues
2655 if (priv->num_channels == priv->dpni_attrs.num_queues)
2662 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
2664 dpaa2_eth_free_channel(priv, channel);
2666 if (err == -EPROBE_DEFER) {
2667 for (i = 0; i < priv->num_channels; i++) {
2668 channel = priv->channel[i];
2669 nctx = &channel->nctx;
2670 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
2671 dpaa2_eth_free_channel(priv, channel);
2673 priv->num_channels = 0;
2677 if (cpumask_empty(&priv->dpio_cpumask)) {
2678 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
2682 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2683 cpumask_pr_args(&priv->dpio_cpumask));
2688 static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
2690 struct device *dev = priv->net_dev->dev.parent;
2691 struct dpaa2_eth_channel *ch;
2694 /* deregister CDAN notifications and free channels */
2695 for (i = 0; i < priv->num_channels; i++) {
2696 ch = priv->channel[i];
2697 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
2698 dpaa2_eth_free_channel(priv, ch);
2702 static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
2705 struct device *dev = priv->net_dev->dev.parent;
2708 for (i = 0; i < priv->num_channels; i++)
2709 if (priv->channel[i]->nctx.desired_cpu == cpu)
2710 return priv->channel[i];
2712 /* We should never get here. Issue a warning and return
2713 * the first channel, because it's still better than nothing
2715 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2717 return priv->channel[0];
2720 static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
2722 struct device *dev = priv->net_dev->dev.parent;
2723 struct dpaa2_eth_fq *fq;
2724 int rx_cpu, txc_cpu;
2727 /* For each FQ, pick one channel/CPU to deliver frames to.
2728 * This may well change at runtime, either through irqbalance or
2729 * through direct user intervention.
2731 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2733 for (i = 0; i < priv->num_fqs; i++) {
2737 fq->target_cpu = rx_cpu;
2738 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2739 if (rx_cpu >= nr_cpu_ids)
2740 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2742 case DPAA2_TX_CONF_FQ:
2743 fq->target_cpu = txc_cpu;
2744 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2745 if (txc_cpu >= nr_cpu_ids)
2746 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2749 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2751 fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
2757 static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
2761 /* We have one TxConf FQ per Tx flow.
2762 * The number of Tx and Rx queues is the same.
2763 * Tx queues come first in the fq array.
2765 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2766 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2767 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2768 priv->fq[priv->num_fqs++].flowid = (u16)i;
2771 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2772 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2773 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2774 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2775 priv->fq[priv->num_fqs].tc = (u8)j;
2776 priv->fq[priv->num_fqs++].flowid = (u16)i;
2780 /* For each FQ, decide on which core to process incoming frames */
2781 dpaa2_eth_set_fq_affinity(priv);
2784 /* Allocate and configure one buffer pool for each interface */
2785 static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv)
2788 struct fsl_mc_device *dpbp_dev;
2789 struct device *dev = priv->net_dev->dev.parent;
2790 struct dpbp_attr dpbp_attrs;
2792 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2796 err = -EPROBE_DEFER;
2798 dev_err(dev, "DPBP device allocation failed\n");
2802 priv->dpbp_dev = dpbp_dev;
2804 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2805 &dpbp_dev->mc_handle);
2807 dev_err(dev, "dpbp_open() failed\n");
2811 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2813 dev_err(dev, "dpbp_reset() failed\n");
2817 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2819 dev_err(dev, "dpbp_enable() failed\n");
2823 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
2826 dev_err(dev, "dpbp_get_attributes() failed\n");
2829 priv->bpid = dpbp_attrs.bpid;
2834 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2837 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2839 fsl_mc_object_free(dpbp_dev);
2844 static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv)
2846 dpaa2_eth_drain_pool(priv);
2847 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2848 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2849 fsl_mc_object_free(priv->dpbp_dev);
2852 static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
2854 struct device *dev = priv->net_dev->dev.parent;
2855 struct dpni_buffer_layout buf_layout = {0};
2859 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2860 * version, this number is not always provided correctly on rev1.
2861 * We need to check for both alternatives in this situation.
2863 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2864 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
2865 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
2867 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
2869 /* We need to ensure that the buffer size seen by WRIOP is a multiple
2870 * of 64 or 256 bytes depending on the WRIOP version.
2872 priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
2875 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
2876 buf_layout.pass_timestamp = true;
2877 buf_layout.pass_frame_status = true;
2878 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2879 DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
2880 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
2881 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2882 DPNI_QUEUE_TX, &buf_layout);
2884 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2888 /* tx-confirm buffer */
2889 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
2890 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
2891 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2892 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2894 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2898 /* Now that we've set our tx buffer layout, retrieve the minimum
2899 * required tx data offset.
2901 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2902 &priv->tx_data_offset);
2904 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2908 if ((priv->tx_data_offset % 64) != 0)
2909 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2910 priv->tx_data_offset);
2913 buf_layout.pass_frame_status = true;
2914 buf_layout.pass_parser_result = true;
2915 buf_layout.data_align = rx_buf_align;
2916 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2917 buf_layout.private_data_size = 0;
2918 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2919 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2920 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
2921 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2922 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
2923 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2924 DPNI_QUEUE_RX, &buf_layout);
2926 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2933 #define DPNI_ENQUEUE_FQID_VER_MAJOR 7
2934 #define DPNI_ENQUEUE_FQID_VER_MINOR 9
2936 static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2937 struct dpaa2_eth_fq *fq,
2938 struct dpaa2_fd *fd, u8 prio,
2939 u32 num_frames __always_unused,
2940 int *frames_enqueued)
2944 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
2945 priv->tx_qdid, prio,
2947 if (!err && frames_enqueued)
2948 *frames_enqueued = 1;
2952 static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
2953 struct dpaa2_eth_fq *fq,
2954 struct dpaa2_fd *fd,
2955 u8 prio, u32 num_frames,
2956 int *frames_enqueued)
2960 err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
2967 if (frames_enqueued)
2968 *frames_enqueued = err;
2972 static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
2974 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2975 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2976 priv->enqueue = dpaa2_eth_enqueue_qd;
2978 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
2981 static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
2983 struct device *dev = priv->net_dev->dev.parent;
2984 struct dpni_link_cfg link_cfg = {0};
2987 /* Get the default link options so we don't override other flags */
2988 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2990 dev_err(dev, "dpni_get_link_cfg() failed\n");
2994 /* By default, enable both Rx and Tx pause frames */
2995 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
2996 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
2997 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2999 dev_err(dev, "dpni_set_link_cfg() failed\n");
3003 priv->link_state.options = link_cfg.options;
3008 static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
3010 struct dpni_queue_id qid = {0};
3011 struct dpaa2_eth_fq *fq;
3012 struct dpni_queue queue;
3015 /* We only use Tx FQIDs for FQID-based enqueue, so check
3016 * if DPNI version supports it before updating FQIDs
3018 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3019 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3022 for (i = 0; i < priv->num_fqs; i++) {
3024 if (fq->type != DPAA2_TX_CONF_FQ)
3026 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3027 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3028 DPNI_QUEUE_TX, j, fq->flowid,
3033 fq->tx_fqid[j] = qid.fqid;
3034 if (fq->tx_fqid[j] == 0)
3039 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3044 netdev_info(priv->net_dev,
3045 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3046 priv->enqueue = dpaa2_eth_enqueue_qd;
3049 /* Configure ingress classification based on VLAN PCP */
3050 static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
3052 struct device *dev = priv->net_dev->dev.parent;
3053 struct dpkg_profile_cfg kg_cfg = {0};
3054 struct dpni_qos_tbl_cfg qos_cfg = {0};
3055 struct dpni_rule_cfg key_params;
3056 void *dma_mem, *key, *mask;
3057 u8 key_size = 2; /* VLAN TCI field */
3060 /* VLAN-based classification only makes sense if we have multiple
3062 * Also, we need to extract just the 3-bit PCP field from the VLAN
3063 * header and we can only do that by using a mask
3065 if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3066 dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3070 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3074 kg_cfg.num_extracts = 1;
3075 kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3076 kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3077 kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3078 kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3080 err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3082 dev_err(dev, "dpni_prepare_key_cfg failed\n");
3087 qos_cfg.default_tc = 0;
3088 qos_cfg.discard_on_miss = 0;
3089 qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3090 DPAA2_CLASSIFIER_DMA_SIZE,
3092 if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3093 dev_err(dev, "QoS table DMA mapping failed\n");
3098 err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3100 dev_err(dev, "dpni_set_qos_table failed\n");
3104 /* Add QoS table entries */
3105 key = kzalloc(key_size * 2, GFP_KERNEL);
3110 mask = key + key_size;
3111 *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3113 key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3115 if (dma_mapping_error(dev, key_params.key_iova)) {
3116 dev_err(dev, "Qos table entry DMA mapping failed\n");
3121 key_params.mask_iova = key_params.key_iova + key_size;
3122 key_params.key_size = key_size;
3124 /* We add rules for PCP-based distribution starting with highest
3125 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3126 * classes to accommodate all priority levels, the lowest ones end up
3127 * on TC 0 which was configured as default
3129 for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3130 *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3131 dma_sync_single_for_device(dev, key_params.key_iova,
3132 key_size * 2, DMA_TO_DEVICE);
3134 err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3137 dev_err(dev, "dpni_add_qos_entry failed\n");
3138 dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3143 priv->vlan_cls_enabled = true;
3145 /* Table and key memory is not persistent, clean everything up after
3146 * configuration is finished
3149 dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3153 dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3161 /* Configure the DPNI object this interface is associated with */
3162 static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
3164 struct device *dev = &ls_dev->dev;
3165 struct dpaa2_eth_priv *priv;
3166 struct net_device *net_dev;
3169 net_dev = dev_get_drvdata(dev);
3170 priv = netdev_priv(net_dev);
3172 /* get a handle for the DPNI object */
3173 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
3175 dev_err(dev, "dpni_open() failed\n");
3179 /* Check if we can work with this DPNI object */
3180 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3181 &priv->dpni_ver_minor);
3183 dev_err(dev, "dpni_get_api_version() failed\n");
3186 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3187 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3188 priv->dpni_ver_major, priv->dpni_ver_minor,
3189 DPNI_VER_MAJOR, DPNI_VER_MINOR);
3194 ls_dev->mc_io = priv->mc_io;
3195 ls_dev->mc_handle = priv->mc_token;
3197 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3199 dev_err(dev, "dpni_reset() failed\n");
3203 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3206 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
3210 err = dpaa2_eth_set_buffer_layout(priv);
3214 dpaa2_eth_set_enqueue_mode(priv);
3216 /* Enable pause frame support */
3217 if (dpaa2_eth_has_pause_support(priv)) {
3218 err = dpaa2_eth_set_pause(priv);
3223 err = dpaa2_eth_set_vlan_qos(priv);
3224 if (err && err != -EOPNOTSUPP)
3227 priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3228 sizeof(struct dpaa2_eth_cls_rule),
3230 if (!priv->cls_rules) {
3238 dpni_close(priv->mc_io, 0, priv->mc_token);
3243 static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
3247 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3249 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3252 dpni_close(priv->mc_io, 0, priv->mc_token);
3255 static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3256 struct dpaa2_eth_fq *fq)
3258 struct device *dev = priv->net_dev->dev.parent;
3259 struct dpni_queue queue;
3260 struct dpni_queue_id qid;
3263 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3264 DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
3266 dev_err(dev, "dpni_get_queue(RX) failed\n");
3270 fq->fqid = qid.fqid;
3272 queue.destination.id = fq->channel->dpcon_id;
3273 queue.destination.type = DPNI_DEST_DPCON;
3274 queue.destination.priority = 1;
3275 queue.user_context = (u64)(uintptr_t)fq;
3276 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3277 DPNI_QUEUE_RX, fq->tc, fq->flowid,
3278 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3281 dev_err(dev, "dpni_set_queue(RX) failed\n");
3286 /* only once for each channel */
3290 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3293 dev_err(dev, "xdp_rxq_info_reg failed\n");
3297 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3298 MEM_TYPE_PAGE_ORDER0, NULL);
3300 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3307 static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3308 struct dpaa2_eth_fq *fq)
3310 struct device *dev = priv->net_dev->dev.parent;
3311 struct dpni_queue queue;
3312 struct dpni_queue_id qid;
3315 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3316 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3317 DPNI_QUEUE_TX, i, fq->flowid,
3320 dev_err(dev, "dpni_get_queue(TX) failed\n");
3323 fq->tx_fqid[i] = qid.fqid;
3326 /* All Tx queues belonging to the same flowid have the same qdbin */
3327 fq->tx_qdbin = qid.qdbin;
3329 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3330 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3333 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3337 fq->fqid = qid.fqid;
3339 queue.destination.id = fq->channel->dpcon_id;
3340 queue.destination.type = DPNI_DEST_DPCON;
3341 queue.destination.priority = 0;
3342 queue.user_context = (u64)(uintptr_t)fq;
3343 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3344 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3345 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3348 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3355 /* Supported header fields for Rx hash distribution key */
3356 static const struct dpaa2_eth_dist_fields dist_fields[] = {
3359 .rxnfc_field = RXH_L2DA,
3360 .cls_prot = NET_PROT_ETH,
3361 .cls_field = NH_FLD_ETH_DA,
3362 .id = DPAA2_ETH_DIST_ETHDST,
3365 .cls_prot = NET_PROT_ETH,
3366 .cls_field = NH_FLD_ETH_SA,
3367 .id = DPAA2_ETH_DIST_ETHSRC,
3370 /* This is the last ethertype field parsed:
3371 * depending on frame format, it can be the MAC ethertype
3372 * or the VLAN etype.
3374 .cls_prot = NET_PROT_ETH,
3375 .cls_field = NH_FLD_ETH_TYPE,
3376 .id = DPAA2_ETH_DIST_ETHTYPE,
3380 .rxnfc_field = RXH_VLAN,
3381 .cls_prot = NET_PROT_VLAN,
3382 .cls_field = NH_FLD_VLAN_TCI,
3383 .id = DPAA2_ETH_DIST_VLAN,
3387 .rxnfc_field = RXH_IP_SRC,
3388 .cls_prot = NET_PROT_IP,
3389 .cls_field = NH_FLD_IP_SRC,
3390 .id = DPAA2_ETH_DIST_IPSRC,
3393 .rxnfc_field = RXH_IP_DST,
3394 .cls_prot = NET_PROT_IP,
3395 .cls_field = NH_FLD_IP_DST,
3396 .id = DPAA2_ETH_DIST_IPDST,
3399 .rxnfc_field = RXH_L3_PROTO,
3400 .cls_prot = NET_PROT_IP,
3401 .cls_field = NH_FLD_IP_PROTO,
3402 .id = DPAA2_ETH_DIST_IPPROTO,
3405 /* Using UDP ports, this is functionally equivalent to raw
3406 * byte pairs from L4 header.
3408 .rxnfc_field = RXH_L4_B_0_1,
3409 .cls_prot = NET_PROT_UDP,
3410 .cls_field = NH_FLD_UDP_PORT_SRC,
3411 .id = DPAA2_ETH_DIST_L4SRC,
3414 .rxnfc_field = RXH_L4_B_2_3,
3415 .cls_prot = NET_PROT_UDP,
3416 .cls_field = NH_FLD_UDP_PORT_DST,
3417 .id = DPAA2_ETH_DIST_L4DST,
3422 /* Configure the Rx hash key using the legacy API */
3423 static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3425 struct device *dev = priv->net_dev->dev.parent;
3426 struct dpni_rx_tc_dist_cfg dist_cfg;
3429 memset(&dist_cfg, 0, sizeof(dist_cfg));
3431 dist_cfg.key_cfg_iova = key;
3432 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3433 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3435 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3436 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3439 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3447 /* Configure the Rx hash key using the new API */
3448 static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3450 struct device *dev = priv->net_dev->dev.parent;
3451 struct dpni_rx_dist_cfg dist_cfg;
3454 memset(&dist_cfg, 0, sizeof(dist_cfg));
3456 dist_cfg.key_cfg_iova = key;
3457 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3458 dist_cfg.enable = 1;
3460 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3462 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3465 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3473 /* Configure the Rx flow classification key */
3474 static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3476 struct device *dev = priv->net_dev->dev.parent;
3477 struct dpni_rx_dist_cfg dist_cfg;
3480 memset(&dist_cfg, 0, sizeof(dist_cfg));
3482 dist_cfg.key_cfg_iova = key;
3483 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3484 dist_cfg.enable = 1;
3486 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3488 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3491 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3499 /* Size of the Rx flow classification key */
3500 int dpaa2_eth_cls_key_size(u64 fields)
3504 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3505 if (!(fields & dist_fields[i].id))
3507 size += dist_fields[i].size;
3513 /* Offset of header field in Rx classification key */
3514 int dpaa2_eth_cls_fld_off(int prot, int field)
3518 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3519 if (dist_fields[i].cls_prot == prot &&
3520 dist_fields[i].cls_field == field)
3522 off += dist_fields[i].size;
3525 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3529 /* Prune unused fields from the classification rule.
3530 * Used when masking is not supported
3532 void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3534 int off = 0, new_off = 0;
3537 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3538 size = dist_fields[i].size;
3539 if (dist_fields[i].id & fields) {
3540 memcpy(key_mem + new_off, key_mem + off, size);
3547 /* Set Rx distribution (hash or flow classification) key
3548 * flags is a combination of RXH_ bits
3550 static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
3551 enum dpaa2_eth_rx_dist type, u64 flags)
3553 struct device *dev = net_dev->dev.parent;
3554 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3555 struct dpkg_profile_cfg cls_cfg;
3556 u32 rx_hash_fields = 0;
3557 dma_addr_t key_iova;
3562 memset(&cls_cfg, 0, sizeof(cls_cfg));
3564 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3565 struct dpkg_extract *key =
3566 &cls_cfg.extracts[cls_cfg.num_extracts];
3568 /* For both Rx hashing and classification keys
3569 * we set only the selected fields.
3571 if (!(flags & dist_fields[i].id))
3573 if (type == DPAA2_ETH_RX_DIST_HASH)
3574 rx_hash_fields |= dist_fields[i].rxnfc_field;
3576 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
3577 dev_err(dev, "error adding key extraction rule, too many rules?\n");
3581 key->type = DPKG_EXTRACT_FROM_HDR;
3582 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
3583 key->extract.from_hdr.type = DPKG_FULL_FIELD;
3584 key->extract.from_hdr.field = dist_fields[i].cls_field;
3585 cls_cfg.num_extracts++;
3588 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3592 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3594 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
3598 /* Prepare for setting the rx dist */
3599 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3601 if (dma_mapping_error(dev, key_iova)) {
3602 dev_err(dev, "DMA mapping failed\n");
3607 if (type == DPAA2_ETH_RX_DIST_HASH) {
3608 if (dpaa2_eth_has_legacy_dist(priv))
3609 err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
3611 err = dpaa2_eth_config_hash_key(priv, key_iova);
3613 err = dpaa2_eth_config_cls_key(priv, key_iova);
3616 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3618 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
3619 priv->rx_hash_fields = rx_hash_fields;
3626 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3628 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3632 if (!dpaa2_eth_hash_enabled(priv))
3635 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3636 if (dist_fields[i].rxnfc_field & flags)
3637 key |= dist_fields[i].id;
3639 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
3642 int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3644 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3647 static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
3649 struct device *dev = priv->net_dev->dev.parent;
3652 /* Check if we actually support Rx flow classification */
3653 if (dpaa2_eth_has_legacy_dist(priv)) {
3654 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3658 if (!dpaa2_eth_fs_enabled(priv)) {
3659 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3663 if (!dpaa2_eth_hash_enabled(priv)) {
3664 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3668 /* If there is no support for masking in the classification table,
3669 * we don't set a default key, as it will depend on the rules
3670 * added by the user at runtime.
3672 if (!dpaa2_eth_fs_mask_enabled(priv))
3675 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
3680 priv->rx_cls_enabled = 1;
3685 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3686 * frame queues and channels
3688 static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
3690 struct net_device *net_dev = priv->net_dev;
3691 struct device *dev = net_dev->dev.parent;
3692 struct dpni_pools_cfg pools_params;
3693 struct dpni_error_cfg err_cfg;
3697 pools_params.num_dpbp = 1;
3698 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3699 pools_params.pools[0].backup_pool = 0;
3700 pools_params.pools[0].buffer_size = priv->rx_buf_size;
3701 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3703 dev_err(dev, "dpni_set_pools() failed\n");
3707 /* have the interface implicitly distribute traffic based on
3708 * the default hash key
3710 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
3711 if (err && err != -EOPNOTSUPP)
3712 dev_err(dev, "Failed to configure hashing\n");
3714 /* Configure the flow classification key; it includes all
3715 * supported header fields and cannot be modified at runtime
3717 err = dpaa2_eth_set_default_cls(priv);
3718 if (err && err != -EOPNOTSUPP)
3719 dev_err(dev, "Failed to configure Rx classification key\n");
3721 /* Configure handling of error frames */
3722 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
3723 err_cfg.set_frame_annotation = 1;
3724 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3725 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3728 dev_err(dev, "dpni_set_errors_behavior failed\n");
3732 /* Configure Rx and Tx conf queues to generate CDANs */
3733 for (i = 0; i < priv->num_fqs; i++) {
3734 switch (priv->fq[i].type) {
3736 err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
3738 case DPAA2_TX_CONF_FQ:
3739 err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
3742 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3749 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3750 DPNI_QUEUE_TX, &priv->tx_qdid);
3752 dev_err(dev, "dpni_get_qdid() failed\n");
3759 /* Allocate rings for storing incoming frame descriptors */
3760 static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
3762 struct net_device *net_dev = priv->net_dev;
3763 struct device *dev = net_dev->dev.parent;
3766 for (i = 0; i < priv->num_channels; i++) {
3767 priv->channel[i]->store =
3768 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3769 if (!priv->channel[i]->store) {
3770 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3778 for (i = 0; i < priv->num_channels; i++) {
3779 if (!priv->channel[i]->store)
3781 dpaa2_io_store_destroy(priv->channel[i]->store);
3787 static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
3791 for (i = 0; i < priv->num_channels; i++)
3792 dpaa2_io_store_destroy(priv->channel[i]->store);
3795 static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
3797 struct net_device *net_dev = priv->net_dev;
3798 struct device *dev = net_dev->dev.parent;
3799 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
3802 /* Get firmware address, if any */
3803 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3805 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3809 /* Get DPNI attributes address, if any */
3810 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3813 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
3817 /* First check if firmware has any address configured by bootloader */
3818 if (!is_zero_ether_addr(mac_addr)) {
3819 /* If the DPMAC addr != DPNI addr, update it */
3820 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
3821 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
3825 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3829 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
3830 } else if (is_zero_ether_addr(dpni_mac_addr)) {
3831 /* No MAC address configured, fill in net_dev->dev_addr
3834 eth_hw_addr_random(net_dev);
3835 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
3837 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3840 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3844 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
3845 * practical purposes, this will be our "permanent" mac address,
3846 * at least until the next reboot. This move will also permit
3847 * register_netdevice() to properly fill up net_dev->perm_addr.
3849 net_dev->addr_assign_type = NET_ADDR_PERM;
3851 /* NET_ADDR_PERM is default, all we have to do is
3852 * fill in the device addr.
3854 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
3860 static int dpaa2_eth_netdev_init(struct net_device *net_dev)
3862 struct device *dev = net_dev->dev.parent;
3863 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3864 u32 options = priv->dpni_attrs.options;
3865 u64 supported = 0, not_supported = 0;
3866 u8 bcast_addr[ETH_ALEN];
3870 net_dev->netdev_ops = &dpaa2_eth_ops;
3871 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
3873 err = dpaa2_eth_set_mac_addr(priv);
3877 /* Explicitly add the broadcast address to the MAC filtering table */
3878 eth_broadcast_addr(bcast_addr);
3879 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3881 dev_err(dev, "dpni_add_mac_addr() failed\n");
3885 /* Set MTU upper limit; lower limit is 68B (default value) */
3886 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
3887 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
3890 dev_err(dev, "dpni_set_max_frame_length() failed\n");
3894 /* Set actual number of queues in the net device */
3895 num_queues = dpaa2_eth_queue_count(priv);
3896 err = netif_set_real_num_tx_queues(net_dev, num_queues);
3898 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
3901 err = netif_set_real_num_rx_queues(net_dev, num_queues);
3903 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
3907 /* Capabilities listing */
3908 supported |= IFF_LIVE_ADDR_CHANGE;
3910 if (options & DPNI_OPT_NO_MAC_FILTER)
3911 not_supported |= IFF_UNICAST_FLT;
3913 supported |= IFF_UNICAST_FLT;
3915 net_dev->priv_flags |= supported;
3916 net_dev->priv_flags &= ~not_supported;
3919 net_dev->features = NETIF_F_RXCSUM |
3920 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3921 NETIF_F_SG | NETIF_F_HIGHDMA |
3922 NETIF_F_LLTX | NETIF_F_HW_TC;
3923 net_dev->hw_features = net_dev->features;
3928 static int dpaa2_eth_poll_link_state(void *arg)
3930 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
3933 while (!kthread_should_stop()) {
3934 err = dpaa2_eth_link_state_update(priv);
3938 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
3944 static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
3946 struct fsl_mc_device *dpni_dev, *dpmac_dev;
3947 struct dpaa2_mac *mac;
3950 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
3951 dpmac_dev = fsl_mc_get_endpoint(dpni_dev);
3952 if (IS_ERR_OR_NULL(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
3955 if (dpaa2_mac_is_type_fixed(dpmac_dev, priv->mc_io))
3958 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
3962 mac->mc_dev = dpmac_dev;
3963 mac->mc_io = priv->mc_io;
3964 mac->net_dev = priv->net_dev;
3966 err = dpaa2_mac_connect(mac);
3968 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint\n");
3977 static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
3982 dpaa2_mac_disconnect(priv->mac);
3987 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
3990 struct device *dev = (struct device *)arg;
3991 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
3992 struct net_device *net_dev = dev_get_drvdata(dev);
3993 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3996 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
3997 DPNI_IRQ_INDEX, &status);
3998 if (unlikely(err)) {
3999 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
4003 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
4004 dpaa2_eth_link_state_update(netdev_priv(net_dev));
4006 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
4007 dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
4008 dpaa2_eth_update_tx_fqids(priv);
4012 dpaa2_eth_disconnect_mac(priv);
4014 dpaa2_eth_connect_mac(priv);
4021 static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
4024 struct fsl_mc_device_irq *irq;
4026 err = fsl_mc_allocate_irqs(ls_dev);
4028 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4032 irq = ls_dev->irqs[0];
4033 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
4034 NULL, dpni_irq0_handler_thread,
4035 IRQF_NO_SUSPEND | IRQF_ONESHOT,
4036 dev_name(&ls_dev->dev), &ls_dev->dev);
4038 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
4042 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
4043 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4044 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
4046 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
4050 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4053 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
4060 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
4062 fsl_mc_free_irqs(ls_dev);
4067 static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
4070 struct dpaa2_eth_channel *ch;
4072 for (i = 0; i < priv->num_channels; i++) {
4073 ch = priv->channel[i];
4074 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4075 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
4080 static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
4083 struct dpaa2_eth_channel *ch;
4085 for (i = 0; i < priv->num_channels; i++) {
4086 ch = priv->channel[i];
4087 netif_napi_del(&ch->napi);
4091 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4094 struct net_device *net_dev = NULL;
4095 struct dpaa2_eth_priv *priv = NULL;
4098 dev = &dpni_dev->dev;
4101 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
4103 dev_err(dev, "alloc_etherdev_mq() failed\n");
4107 SET_NETDEV_DEV(net_dev, dev);
4108 dev_set_drvdata(dev, net_dev);
4110 priv = netdev_priv(net_dev);
4111 priv->net_dev = net_dev;
4113 priv->iommu_domain = iommu_get_domain_for_dev(dev);
4115 priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4116 priv->rx_tstamp = false;
4118 priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
4119 if (!priv->dpaa2_ptp_wq) {
4124 INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4126 skb_queue_head_init(&priv->tx_skbs);
4128 /* Obtain a MC portal */
4129 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4133 err = -EPROBE_DEFER;
4135 dev_err(dev, "MC portal allocation failed\n");
4136 goto err_portal_alloc;
4139 /* MC objects initialization and configuration */
4140 err = dpaa2_eth_setup_dpni(dpni_dev);
4142 goto err_dpni_setup;
4144 err = dpaa2_eth_setup_dpio(priv);
4146 goto err_dpio_setup;
4148 dpaa2_eth_setup_fqs(priv);
4150 err = dpaa2_eth_setup_dpbp(priv);
4152 goto err_dpbp_setup;
4154 err = dpaa2_eth_bind_dpni(priv);
4158 /* Add a NAPI context for each channel */
4159 dpaa2_eth_add_ch_napi(priv);
4161 /* Percpu statistics */
4162 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4163 if (!priv->percpu_stats) {
4164 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4166 goto err_alloc_percpu_stats;
4168 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4169 if (!priv->percpu_extras) {
4170 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4172 goto err_alloc_percpu_extras;
4175 priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4176 if (!priv->sgt_cache) {
4177 dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4179 goto err_alloc_sgt_cache;
4182 err = dpaa2_eth_netdev_init(net_dev);
4184 goto err_netdev_init;
4186 /* Configure checksum offload based on current interface flags */
4187 err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
4191 err = dpaa2_eth_set_tx_csum(priv,
4192 !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
4196 err = dpaa2_eth_alloc_rings(priv);
4198 goto err_alloc_rings;
4200 #ifdef CONFIG_FSL_DPAA2_ETH_DCB
4201 if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4202 priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4203 net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4205 dev_dbg(dev, "PFC not supported\n");
4209 err = dpaa2_eth_setup_irqs(dpni_dev);
4211 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
4212 priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
4213 "%s_poll_link", net_dev->name);
4214 if (IS_ERR(priv->poll_thread)) {
4215 dev_err(dev, "Error starting polling thread\n");
4216 goto err_poll_thread;
4218 priv->do_link_poll = true;
4221 err = dpaa2_eth_connect_mac(priv);
4223 goto err_connect_mac;
4225 err = register_netdev(net_dev);
4227 dev_err(dev, "register_netdev() failed\n");
4228 goto err_netdev_reg;
4231 #ifdef CONFIG_DEBUG_FS
4232 dpaa2_dbg_add(priv);
4235 dev_info(dev, "Probed interface %s\n", net_dev->name);
4239 dpaa2_eth_disconnect_mac(priv);
4241 if (priv->do_link_poll)
4242 kthread_stop(priv->poll_thread);
4244 fsl_mc_free_irqs(dpni_dev);
4246 dpaa2_eth_free_rings(priv);
4250 free_percpu(priv->sgt_cache);
4251 err_alloc_sgt_cache:
4252 free_percpu(priv->percpu_extras);
4253 err_alloc_percpu_extras:
4254 free_percpu(priv->percpu_stats);
4255 err_alloc_percpu_stats:
4256 dpaa2_eth_del_ch_napi(priv);
4258 dpaa2_eth_free_dpbp(priv);
4260 dpaa2_eth_free_dpio(priv);
4262 dpaa2_eth_free_dpni(priv);
4264 fsl_mc_portal_free(priv->mc_io);
4266 destroy_workqueue(priv->dpaa2_ptp_wq);
4268 dev_set_drvdata(dev, NULL);
4269 free_netdev(net_dev);
4274 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4277 struct net_device *net_dev;
4278 struct dpaa2_eth_priv *priv;
4281 net_dev = dev_get_drvdata(dev);
4282 priv = netdev_priv(net_dev);
4284 #ifdef CONFIG_DEBUG_FS
4285 dpaa2_dbg_remove(priv);
4288 dpaa2_eth_disconnect_mac(priv);
4291 unregister_netdev(net_dev);
4293 if (priv->do_link_poll)
4294 kthread_stop(priv->poll_thread);
4296 fsl_mc_free_irqs(ls_dev);
4298 dpaa2_eth_free_rings(priv);
4299 free_percpu(priv->sgt_cache);
4300 free_percpu(priv->percpu_stats);
4301 free_percpu(priv->percpu_extras);
4303 dpaa2_eth_del_ch_napi(priv);
4304 dpaa2_eth_free_dpbp(priv);
4305 dpaa2_eth_free_dpio(priv);
4306 dpaa2_eth_free_dpni(priv);
4308 fsl_mc_portal_free(priv->mc_io);
4310 free_netdev(net_dev);
4312 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
4317 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
4319 .vendor = FSL_MC_VENDOR_FREESCALE,
4324 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
4326 static struct fsl_mc_driver dpaa2_eth_driver = {
4328 .name = KBUILD_MODNAME,
4329 .owner = THIS_MODULE,
4331 .probe = dpaa2_eth_probe,
4332 .remove = dpaa2_eth_remove,
4333 .match_id_table = dpaa2_eth_match_id_table
4336 static int __init dpaa2_eth_driver_init(void)
4340 dpaa2_eth_dbg_init();
4341 err = fsl_mc_driver_register(&dpaa2_eth_driver);
4343 dpaa2_eth_dbg_exit();
4350 static void __exit dpaa2_eth_driver_exit(void)
4352 dpaa2_eth_dbg_exit();
4353 fsl_mc_driver_unregister(&dpaa2_eth_driver);
4356 module_init(dpaa2_eth_driver_init);
4357 module_exit(dpaa2_eth_driver_exit);