blob: 34f189271ef2b6801b6df3af9c30152bc684fadb [file] [log] [blame]
Ioana Ciornei0bb29b22018-07-31 12:02:47 -05001// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002/* Copyright 2014-2016 Freescale Semiconductor Inc.
Ioana Ciornei48c04812020-04-22 15:05:10 +03003 * Copyright 2016-2020 NXP
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004 */
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>
Ioana Radulescu08eb2392017-05-24 07:13:27 -050013#include <linux/iommu.h>
Bogdan Purcareata6bd067c2018-02-05 08:07:42 -060014#include <linux/fsl/mc.h>
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +000015#include <linux/bpf.h>
16#include <linux/bpf_trace.h>
Yangbo Lud21c7842020-09-18 17:07:59 +080017#include <linux/fsl/ptp_qoriq.h>
Yangbo Luc5521182020-09-18 17:08:02 +080018#include <linux/ptp_classify.h>
Ioana Ciornei3657cda2020-07-21 19:38:25 +030019#include <net/pkt_cls.h>
Ioana Radulescu859f9982018-04-26 18:23:47 +080020#include <net/sock.h>
21
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050022#include "dpaa2-eth.h"
23
Ioana Radulescu56361872017-04-28 04:50:32 -050024/* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
25 * using trace events only need to #include <trace/events/sched.h>
26 */
27#define CREATE_TRACE_POINTS
28#include "dpaa2-eth-trace.h"
29
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050030MODULE_LICENSE("Dual BSD/GPL");
31MODULE_AUTHOR("Freescale Semiconductor, Inc");
32MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
33
Yangbo Lud21c7842020-09-18 17:07:59 +080034struct ptp_qoriq *dpaa2_ptp;
35EXPORT_SYMBOL(dpaa2_ptp);
36
Ioana Radulescu08eb2392017-05-24 07:13:27 -050037static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
38 dma_addr_t iova_addr)
39{
40 phys_addr_t phys_addr;
41
42 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
43
44 return phys_to_virt(phys_addr);
45}
46
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +030047static void dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv *priv,
48 u32 fd_status,
49 struct sk_buff *skb)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050050{
51 skb_checksum_none_assert(skb);
52
53 /* HW checksum validation is disabled, nothing to do here */
54 if (!(priv->net_dev->features & NETIF_F_RXCSUM))
55 return;
56
57 /* Read checksum validation bits */
58 if (!((fd_status & DPAA2_FAS_L3CV) &&
59 (fd_status & DPAA2_FAS_L4CV)))
60 return;
61
62 /* Inform the stack there's no need to compute L3/L4 csum anymore */
63 skb->ip_summed = CHECKSUM_UNNECESSARY;
64}
65
66/* Free a received FD.
67 * Not to be used for Tx conf FDs or on any other paths.
68 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +030069static void dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv,
70 const struct dpaa2_fd *fd,
71 void *vaddr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050072{
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;
77 void *sg_vaddr;
78 int i;
79
80 /* If single buffer frame, just free the data buffer */
81 if (fd_format == dpaa2_fd_single)
82 goto free_buf;
83 else if (fd_format != dpaa2_fd_sg)
84 /* We don't support any other format */
85 return;
86
Ioana Radulescu729d79b2017-10-11 08:29:48 -050087 /* For S/G frames, we first need to free all SG entries
88 * except the first one, which was taken care of already
89 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050090 sgt = vaddr + dpaa2_fd_get_offset(fd);
Ioana Radulescu729d79b2017-10-11 08:29:48 -050091 for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050092 addr = dpaa2_sg_get_addr(&sgt[i]);
Ioana Radulescu08eb2392017-05-24 07:13:27 -050093 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +030094 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000095 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050096
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000097 free_pages((unsigned long)sg_vaddr, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050098 if (dpaa2_sg_is_final(&sgt[i]))
99 break;
100 }
101
102free_buf:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000103 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500104}
105
106/* Build a linear skb based on a single-buffer frame descriptor */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300107static struct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch,
108 const struct dpaa2_fd *fd,
109 void *fd_vaddr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500110{
111 struct sk_buff *skb = NULL;
112 u16 fd_offset = dpaa2_fd_get_offset(fd);
113 u32 fd_length = dpaa2_fd_get_len(fd);
114
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500115 ch->buf_count--;
116
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000117 skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500118 if (unlikely(!skb))
119 return NULL;
120
121 skb_reserve(skb, fd_offset);
122 skb_put(skb, fd_length);
123
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500124 return skb;
125}
126
127/* Build a non linear (fragmented) skb based on a S/G table */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300128static 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)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500131{
132 struct sk_buff *skb = NULL;
133 struct device *dev = priv->net_dev->dev.parent;
134 void *sg_vaddr;
135 dma_addr_t sg_addr;
136 u16 sg_offset;
137 u32 sg_length;
138 struct page *page, *head_page;
139 int page_offset;
140 int i;
141
142 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
143 struct dpaa2_sg_entry *sge = &sgt[i];
144
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
147 */
148
149 /* Get the address and length from the S/G entry */
150 sg_addr = dpaa2_sg_get_addr(sge);
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500151 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300152 dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000153 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500154
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500155 sg_length = dpaa2_sg_get_len(sge);
156
157 if (i == 0) {
158 /* We build the skb around the first data buffer */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000159 skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500160 if (unlikely(!skb)) {
Ioana Radulescu729d79b2017-10-11 08:29:48 -0500161 /* Free the first SG entry now, since we already
162 * unmapped it and obtained the virtual address
163 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000164 free_pages((unsigned long)sg_vaddr, 0);
Ioana Radulescu729d79b2017-10-11 08:29:48 -0500165
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500166 /* We still need to subtract the buffers used
167 * by this FD from our software counter
168 */
169 while (!dpaa2_sg_is_final(&sgt[i]) &&
170 i < DPAA2_ETH_MAX_SG_ENTRIES)
171 i++;
172 break;
173 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500174
175 sg_offset = dpaa2_sg_get_offset(sge);
176 skb_reserve(skb, sg_offset);
177 skb_put(skb, sg_length);
178 } else {
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);
182
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
186 * sg_offset.
187 */
188 page_offset = ((unsigned long)sg_vaddr &
189 (PAGE_SIZE - 1)) +
190 (page_address(page) - page_address(head_page));
191
192 skb_add_rx_frag(skb, i - 1, head_page, page_offset,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300193 sg_length, priv->rx_buf_size);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500194 }
195
196 if (dpaa2_sg_is_final(sge))
197 break;
198 }
199
Ioana Radulescub63baf72017-10-11 08:29:45 -0500200 WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
201
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500202 /* Count all data buffers + SG table buffer */
203 ch->buf_count -= i + 2;
204
205 return skb;
206}
207
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000208/* Free buffers acquired from the buffer pool or which were meant to
209 * be released in the pool
210 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300211static void dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array,
212 int count)
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000213{
214 struct device *dev = priv->net_dev->dev.parent;
215 void *vaddr;
216 int i;
217
218 for (i = 0; i < count; i++) {
219 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300220 dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000221 DMA_BIDIRECTIONAL);
222 free_pages((unsigned long)vaddr, 0);
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000223 }
224}
225
Ioana Ciornei28d137c2021-04-02 12:55:30 +0300226static void dpaa2_eth_recycle_buf(struct dpaa2_eth_priv *priv,
227 struct dpaa2_eth_channel *ch,
228 dma_addr_t addr)
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000229{
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300230 int retries = 0;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000231 int err;
232
Ioana Ciornei28d137c2021-04-02 12:55:30 +0300233 ch->recycled_bufs[ch->recycled_bufs_cnt++] = addr;
234 if (ch->recycled_bufs_cnt < DPAA2_ETH_BUFS_PER_CMD)
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000235 return;
236
237 while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
Ioana Ciornei28d137c2021-04-02 12:55:30 +0300238 ch->recycled_bufs,
239 ch->recycled_bufs_cnt)) == -EBUSY) {
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300240 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
241 break;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000242 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300243 }
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000244
245 if (err) {
Ioana Ciornei28d137c2021-04-02 12:55:30 +0300246 dpaa2_eth_free_bufs(priv, ch->recycled_bufs, ch->recycled_bufs_cnt);
247 ch->buf_count -= ch->recycled_bufs_cnt;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000248 }
249
Ioana Ciornei28d137c2021-04-02 12:55:30 +0300250 ch->recycled_bufs_cnt = 0;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000251}
252
Ioana Ciornei38c440b2020-05-06 20:47:17 +0300253static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
254 struct dpaa2_eth_fq *fq,
255 struct dpaa2_eth_xdp_fds *xdp_fds)
256{
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;
261
262 percpu_extras = this_cpu_ptr(priv->percpu_extras);
263
264 /* try to enqueue all the FDs until the max number of retries is hit */
265 fds = xdp_fds->fds;
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);
271 if (err == -EBUSY) {
272 percpu_extras->tx_portal_busy += ++retries;
273 continue;
274 }
275 total_enqueued += enqueued;
276 }
277 xdp_fds->num = 0;
278
279 return total_enqueued;
280}
281
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300282static void dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv *priv,
283 struct dpaa2_eth_channel *ch,
284 struct dpaa2_eth_fq *fq)
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000285{
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300286 struct rtnl_link_stats64 *percpu_stats;
287 struct dpaa2_fd *fds;
288 int enqueued, i;
289
290 percpu_stats = this_cpu_ptr(priv->percpu_stats);
291
292 // enqueue the array of XDP_TX frames
293 enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);
294
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]);
300 ch->stats.xdp_tx++;
301 }
302 for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
Ioana Ciornei28d137c2021-04-02 12:55:30 +0300303 dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300304 percpu_stats->tx_errors++;
305 ch->stats.xdp_tx_err++;
306 }
307 fq->xdp_tx_fds.num = 0;
308}
309
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300310static void dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv *priv,
311 struct dpaa2_eth_channel *ch,
312 struct dpaa2_fd *fd,
313 void *buf_start, u16 queue_id)
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300314{
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000315 struct dpaa2_faead *faead;
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300316 struct dpaa2_fd *dest_fd;
317 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000318 u32 ctrl, frc;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000319
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);
324
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
328 */
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;
333
334 fq = &priv->fq[queue_id];
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300335 dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
336 memcpy(dest_fd, fd, sizeof(*dest_fd));
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000337
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300338 if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
339 return;
340
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300341 dpaa2_eth_xdp_tx_flush(priv, ch, fq);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000342}
343
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300344static 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)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000348{
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000349 dma_addr_t addr = dpaa2_fd_get_addr(fd);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000350 struct bpf_prog *xdp_prog;
351 struct xdp_buff xdp;
352 u32 xdp_act = XDP_PASS;
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +0100353 int err, offset;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000354
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000355 xdp_prog = READ_ONCE(ch->xdp.prog);
356 if (!xdp_prog)
357 goto out;
358
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +0100359 offset = dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM;
360 xdp_init_buff(&xdp, DPAA2_ETH_RX_BUF_RAW_SIZE - offset, &ch->xdp_rxq);
361 xdp_prepare_buff(&xdp, vaddr + offset, XDP_PACKET_HEADROOM,
362 dpaa2_fd_get_len(fd), false);
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200363
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000364 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
365
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000366 /* xdp.data pointer may have changed */
367 dpaa2_fd_set_offset(fd, xdp.data - vaddr);
368 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
369
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000370 switch (xdp_act) {
371 case XDP_PASS:
372 break;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000373 case XDP_TX:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300374 dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000375 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000376 default:
377 bpf_warn_invalid_xdp_action(xdp_act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500378 fallthrough;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000379 case XDP_ABORTED:
380 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500381 fallthrough;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000382 case XDP_DROP:
Ioana Ciornei28d137c2021-04-02 12:55:30 +0300383 dpaa2_eth_recycle_buf(priv, ch, addr);
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000384 ch->stats.xdp_drop++;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000385 break;
Ioana Radulescud678be12019-03-01 17:47:24 +0000386 case XDP_REDIRECT:
387 dma_unmap_page(priv->net_dev->dev.parent, addr,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300388 priv->rx_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescud678be12019-03-01 17:47:24 +0000389 ch->buf_count--;
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200390
391 /* Allow redirect use of full headroom */
Ioana Radulescud678be12019-03-01 17:47:24 +0000392 xdp.data_hard_start = vaddr;
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200393 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
394
Ioana Radulescud678be12019-03-01 17:47:24 +0000395 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
Ioana Ciorneie12be912021-02-11 21:51:22 +0200396 if (unlikely(err)) {
397 addr = dma_map_page(priv->net_dev->dev.parent,
398 virt_to_page(vaddr), 0,
399 priv->rx_buf_size, DMA_BIDIRECTIONAL);
400 if (unlikely(dma_mapping_error(priv->net_dev->dev.parent, addr))) {
401 free_pages((unsigned long)vaddr, 0);
402 } else {
403 ch->buf_count++;
Ioana Ciornei28d137c2021-04-02 12:55:30 +0300404 dpaa2_eth_recycle_buf(priv, ch, addr);
Ioana Ciorneie12be912021-02-11 21:51:22 +0200405 }
Ioana Radulescud678be12019-03-01 17:47:24 +0000406 ch->stats.xdp_drop++;
Ioana Ciorneie12be912021-02-11 21:51:22 +0200407 } else {
Ioana Radulescud678be12019-03-01 17:47:24 +0000408 ch->stats.xdp_redirect++;
Ioana Ciorneie12be912021-02-11 21:51:22 +0200409 }
Ioana Radulescud678be12019-03-01 17:47:24 +0000410 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000411 }
412
Ioana Radulescud678be12019-03-01 17:47:24 +0000413 ch->xdp.res |= xdp_act;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000414out:
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000415 return xdp_act;
416}
417
Ioana Ciornei50f82692021-04-02 12:55:31 +0300418static struct sk_buff *dpaa2_eth_copybreak(struct dpaa2_eth_channel *ch,
419 const struct dpaa2_fd *fd,
420 void *fd_vaddr)
421{
422 u16 fd_offset = dpaa2_fd_get_offset(fd);
Ioana Ciornei8ed3cef2021-04-02 12:55:32 +0300423 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciornei50f82692021-04-02 12:55:31 +0300424 u32 fd_length = dpaa2_fd_get_len(fd);
425 struct sk_buff *skb = NULL;
426 unsigned int skb_len;
427
Ioana Ciornei8ed3cef2021-04-02 12:55:32 +0300428 if (fd_length > priv->rx_copybreak)
Ioana Ciornei50f82692021-04-02 12:55:31 +0300429 return NULL;
430
431 skb_len = fd_length + dpaa2_eth_needed_headroom(NULL);
432
433 skb = napi_alloc_skb(&ch->napi, skb_len);
434 if (!skb)
435 return NULL;
436
437 skb_reserve(skb, dpaa2_eth_needed_headroom(NULL));
438 skb_put(skb, fd_length);
439
440 memcpy(skb->data, fd_vaddr + fd_offset, fd_length);
441
Ioana Ciornei8ed3cef2021-04-02 12:55:32 +0300442 dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(fd));
Ioana Ciornei50f82692021-04-02 12:55:31 +0300443
444 return skb;
445}
446
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500447/* Main Rx frame processing routine */
448static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
449 struct dpaa2_eth_channel *ch,
450 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000451 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500452{
453 dma_addr_t addr = dpaa2_fd_get_addr(fd);
454 u8 fd_format = dpaa2_fd_get_format(fd);
455 void *vaddr;
456 struct sk_buff *skb;
457 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500458 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500459 struct device *dev = priv->net_dev->dev.parent;
460 struct dpaa2_fas *fas;
Ioana Radulescud695e762017-06-06 10:00:35 -0500461 void *buf_data;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500462 u32 status = 0;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000463 u32 xdp_act;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500464
Ioana Radulescu56361872017-04-28 04:50:32 -0500465 /* Tracing point */
466 trace_dpaa2_rx_fd(priv->net_dev, fd);
467
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500468 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300469 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu18c2e772018-11-26 16:27:32 +0000470 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500471
Ioana Radulescu54ce8912017-12-08 06:47:53 -0600472 fas = dpaa2_get_fas(vaddr, false);
Ioana Radulescud695e762017-06-06 10:00:35 -0500473 prefetch(fas);
474 buf_data = vaddr + dpaa2_fd_get_offset(fd);
475 prefetch(buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500476
477 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500478 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500479
480 if (fd_format == dpaa2_fd_single) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300481 xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000482 if (xdp_act != XDP_PASS) {
483 percpu_stats->rx_packets++;
484 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
485 return;
486 }
487
Ioana Ciornei50f82692021-04-02 12:55:31 +0300488 skb = dpaa2_eth_copybreak(ch, fd, vaddr);
489 if (!skb) {
490 dma_unmap_page(dev, addr, priv->rx_buf_size,
491 DMA_BIDIRECTIONAL);
492 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
493 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500494 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000495 WARN_ON(priv->xdp_prog);
496
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300497 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000498 DMA_BIDIRECTIONAL);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300499 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000500 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500501 percpu_extras->rx_sg_frames++;
502 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500503 } else {
504 /* We don't support any other format */
505 goto err_frame_format;
506 }
507
508 if (unlikely(!skb))
509 goto err_build_skb;
510
511 prefetch(skb->data);
512
Ioana Radulescu859f9982018-04-26 18:23:47 +0800513 /* Get the timestamp value */
514 if (priv->rx_tstamp) {
515 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
516 __le64 *ts = dpaa2_get_ts(vaddr, false);
517 u64 ns;
518
519 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
520
521 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
522 shhwtstamps->hwtstamp = ns_to_ktime(ns);
523 }
524
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500525 /* Check if we need to validate the L4 csum */
526 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500527 status = le32_to_cpu(fas->status);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300528 dpaa2_eth_validate_rx_csum(priv, status, skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500529 }
530
531 skb->protocol = eth_type_trans(skb, priv->net_dev);
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000532 skb_record_rx_queue(skb, fq->flowid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500533
534 percpu_stats->rx_packets++;
535 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
536
Ioana Ciornei0a25d922019-03-25 13:42:39 +0000537 list_add_tail(&skb->list, ch->rx_list);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500538
539 return;
540
541err_build_skb:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300542 dpaa2_eth_free_rx_fd(priv, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500543err_frame_format:
544 percpu_stats->rx_dropped++;
545}
546
Ioana Ciornei061d6312020-10-01 18:11:48 +0300547/* Processing of Rx frames received on the error FQ
548 * We check and print the error bits and then free the frame
549 */
550static void dpaa2_eth_rx_err(struct dpaa2_eth_priv *priv,
551 struct dpaa2_eth_channel *ch,
552 const struct dpaa2_fd *fd,
553 struct dpaa2_eth_fq *fq __always_unused)
554{
555 struct device *dev = priv->net_dev->dev.parent;
556 dma_addr_t addr = dpaa2_fd_get_addr(fd);
557 u8 fd_format = dpaa2_fd_get_format(fd);
558 struct rtnl_link_stats64 *percpu_stats;
559 struct dpaa2_eth_trap_item *trap_item;
560 struct dpaa2_fapr *fapr;
561 struct sk_buff *skb;
562 void *buf_data;
563 void *vaddr;
564
565 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
566 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
567 DMA_BIDIRECTIONAL);
568
569 buf_data = vaddr + dpaa2_fd_get_offset(fd);
570
571 if (fd_format == dpaa2_fd_single) {
572 dma_unmap_page(dev, addr, priv->rx_buf_size,
573 DMA_BIDIRECTIONAL);
574 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
575 } else if (fd_format == dpaa2_fd_sg) {
576 dma_unmap_page(dev, addr, priv->rx_buf_size,
577 DMA_BIDIRECTIONAL);
578 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
579 free_pages((unsigned long)vaddr, 0);
580 } else {
581 /* We don't support any other format */
582 dpaa2_eth_free_rx_fd(priv, fd, vaddr);
583 goto err_frame_format;
584 }
585
586 fapr = dpaa2_get_fapr(vaddr, false);
587 trap_item = dpaa2_eth_dl_get_trap(priv, fapr);
588 if (trap_item)
589 devlink_trap_report(priv->devlink, skb, trap_item->trap_ctx,
590 &priv->devlink_port, NULL);
591 consume_skb(skb);
592
593err_frame_format:
594 percpu_stats = this_cpu_ptr(priv->percpu_stats);
595 percpu_stats->rx_errors++;
596 ch->buf_count--;
597}
598
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500599/* Consume all frames pull-dequeued into the store. This is the simplest way to
600 * make sure we don't accidentally issue another volatile dequeue which would
601 * overwrite (leak) frames already in the store.
602 *
603 * Observance of NAPI budget is not our concern, leaving that to the caller.
604 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300605static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
606 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500607{
608 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000609 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500610 struct dpaa2_dq *dq;
611 const struct dpaa2_fd *fd;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300612 int cleaned = 0, retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500613 int is_last;
614
615 do {
616 dq = dpaa2_io_store_next(ch->store, &is_last);
617 if (unlikely(!dq)) {
618 /* If we're here, we *must* have placed a
619 * volatile dequeue comnmand, so keep reading through
620 * the store until we get some sort of valid response
621 * token (either a valid frame or an "empty dequeue")
622 */
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300623 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
624 netdev_err_once(priv->net_dev,
625 "Unable to read a valid dequeue response\n");
626 return -ETIMEDOUT;
627 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500628 continue;
629 }
630
631 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600632 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500633
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000634 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500635 cleaned++;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300636 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500637 } while (!is_last);
638
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000639 if (!cleaned)
640 return 0;
641
642 fq->stats.frames += cleaned;
Ioana Ciornei460fd832020-04-24 12:33:18 +0300643 ch->stats.frames += cleaned;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000644
645 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000646 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000647 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000648 if (src)
649 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000650
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500651 return cleaned;
652}
653
Yangbo Luc5521182020-09-18 17:08:02 +0800654static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
655 u8 *msgtype, u8 *twostep, u8 *udp,
656 u16 *correction_offset,
657 u16 *origintimestamp_offset)
Ioana Radulescu859f9982018-04-26 18:23:47 +0800658{
Yangbo Luc5521182020-09-18 17:08:02 +0800659 unsigned int ptp_class;
660 struct ptp_header *hdr;
661 unsigned int type;
662 u8 *base;
663
664 ptp_class = ptp_classify_raw(skb);
665 if (ptp_class == PTP_CLASS_NONE)
666 return -EINVAL;
667
668 hdr = ptp_parse_header(skb, ptp_class);
669 if (!hdr)
670 return -EINVAL;
671
672 *msgtype = ptp_get_msgtype(hdr, ptp_class);
673 *twostep = hdr->flag_field[0] & 0x2;
674
675 type = ptp_class & PTP_CLASS_PMASK;
676 if (type == PTP_CLASS_IPV4 ||
677 type == PTP_CLASS_IPV6)
678 *udp = 1;
679 else
680 *udp = 0;
681
682 base = skb_mac_header(skb);
683 *correction_offset = (u8 *)&hdr->correction - base;
684 *origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
685
686 return 0;
687}
688
689/* Configure the egress frame annotation for timestamp update */
690static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
691 struct dpaa2_fd *fd,
692 void *buf_start,
693 struct sk_buff *skb)
694{
695 struct ptp_tstamp origin_timestamp;
696 struct dpni_single_step_cfg cfg;
697 u8 msgtype, twostep, udp;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800698 struct dpaa2_faead *faead;
Yangbo Luc5521182020-09-18 17:08:02 +0800699 struct dpaa2_fas *fas;
700 struct timespec64 ts;
701 u16 offset1, offset2;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800702 u32 ctrl, frc;
Yangbo Luc5521182020-09-18 17:08:02 +0800703 __le64 *ns;
704 u8 *data;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800705
706 /* Mark the egress frame annotation area as valid */
707 frc = dpaa2_fd_get_frc(fd);
708 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
709
710 /* Set hardware annotation size */
711 ctrl = dpaa2_fd_get_ctrl(fd);
712 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
713
714 /* enable UPD (update prepanded data) bit in FAEAD field of
715 * hardware frame annotation area
716 */
717 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
718 faead = dpaa2_get_faead(buf_start, true);
719 faead->ctrl = cpu_to_le32(ctrl);
Yangbo Luc5521182020-09-18 17:08:02 +0800720
721 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
722 if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
723 &offset1, &offset2) ||
Christian Eggers6b6817c2020-11-20 09:41:05 +0100724 msgtype != PTP_MSGTYPE_SYNC || twostep) {
Yangbo Luc5521182020-09-18 17:08:02 +0800725 WARN_ONCE(1, "Bad packet for one-step timestamping\n");
726 return;
727 }
728
729 /* Mark the frame annotation status as valid */
730 frc = dpaa2_fd_get_frc(fd);
731 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);
732
733 /* Mark the PTP flag for one step timestamping */
734 fas = dpaa2_get_fas(buf_start, true);
735 fas->status = cpu_to_le32(DPAA2_FAS_PTP);
736
737 dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
738 ns = dpaa2_get_ts(buf_start, true);
739 *ns = cpu_to_le64(timespec64_to_ns(&ts) /
740 DPAA2_PTP_CLK_PERIOD_NS);
741
742 /* Update current time to PTP message originTimestamp field */
743 ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
744 data = skb_mac_header(skb);
745 *(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
746 *(__be32 *)(data + offset2 + 2) =
747 htonl(origin_timestamp.sec_lsb);
748 *(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);
749
750 cfg.en = 1;
751 cfg.ch_update = udp;
752 cfg.offset = offset1;
753 cfg.peer_delay = 0;
754
755 if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token,
756 &cfg))
757 WARN_ONCE(1, "Failed to set single step register");
758 }
Ioana Radulescu859f9982018-04-26 18:23:47 +0800759}
760
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500761/* Create a frame descriptor based on a fragmented skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300762static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
763 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800764 struct dpaa2_fd *fd,
765 void **swa_addr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500766{
767 struct device *dev = priv->net_dev->dev.parent;
768 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500769 dma_addr_t addr;
770 int nr_frags = skb_shinfo(skb)->nr_frags;
771 struct dpaa2_sg_entry *sgt;
772 int i, err;
773 int sgt_buf_size;
774 struct scatterlist *scl, *crt_scl;
775 int num_sg;
776 int num_dma_bufs;
777 struct dpaa2_eth_swa *swa;
778
779 /* Create and map scatterlist.
780 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
781 * to go beyond nr_frags+1.
782 * Note: We don't support chained scatterlists
783 */
784 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
785 return -EINVAL;
786
Julia Lawalld4ceb8d2020-09-20 13:26:15 +0200787 scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500788 if (unlikely(!scl))
789 return -ENOMEM;
790
791 sg_init_table(scl, nr_frags + 1);
792 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Ciornei37fbbdda2020-06-24 14:34:18 +0300793 if (unlikely(num_sg < 0)) {
794 err = -ENOMEM;
795 goto dma_map_sg_failed;
796 }
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500797 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500798 if (unlikely(!num_dma_bufs)) {
799 err = -ENOMEM;
800 goto dma_map_sg_failed;
801 }
802
803 /* Prepare the HW SGT structure */
804 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500805 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Kevin Haod0dfbb92021-02-04 18:56:38 +0800806 sgt_buf = napi_alloc_frag_align(sgt_buf_size, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500807 if (unlikely(!sgt_buf)) {
808 err = -ENOMEM;
809 goto sgt_buf_alloc_failed;
810 }
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500811 memset(sgt_buf, 0, sgt_buf_size);
812
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500813 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
814
815 /* Fill in the HW SGT structure.
816 *
817 * sgt_buf is zeroed out, so the following fields are implicit
818 * in all sgt entries:
819 * - offset is 0
820 * - format is 'dpaa2_sg_single'
821 */
822 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
823 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
824 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
825 }
826 dpaa2_sg_set_final(&sgt[i - 1], true);
827
828 /* Store the skb backpointer in the SGT buffer.
829 * Fit the scatterlist and the number of buffers alongside the
830 * skb backpointer in the software annotation area. We'll need
831 * all of them on Tx Conf.
832 */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800833 *swa_addr = (void *)sgt_buf;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500834 swa = (struct dpaa2_eth_swa *)sgt_buf;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000835 swa->type = DPAA2_ETH_SWA_SG;
836 swa->sg.skb = skb;
837 swa->sg.scl = scl;
838 swa->sg.num_sg = num_sg;
839 swa->sg.sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500840
841 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500842 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500843 if (unlikely(dma_mapping_error(dev, addr))) {
844 err = -ENOMEM;
845 goto dma_map_single_failed;
846 }
847 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
848 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
849 dpaa2_fd_set_addr(fd, addr);
850 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000851 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500852
853 return 0;
854
855dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500856 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500857sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500858 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500859dma_map_sg_failed:
860 kfree(scl);
861 return err;
862}
863
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300864/* Create a SG frame descriptor based on a linear skb.
865 *
866 * This function is used on the Tx path when the skb headroom is not large
867 * enough for the HW requirements, thus instead of realloc-ing the skb we
868 * create a SG frame descriptor with only one entry.
869 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300870static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
871 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800872 struct dpaa2_fd *fd,
873 void **swa_addr)
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300874{
875 struct device *dev = priv->net_dev->dev.parent;
876 struct dpaa2_eth_sgt_cache *sgt_cache;
877 struct dpaa2_sg_entry *sgt;
878 struct dpaa2_eth_swa *swa;
879 dma_addr_t addr, sgt_addr;
880 void *sgt_buf = NULL;
881 int sgt_buf_size;
882 int err;
883
884 /* Prepare the HW SGT structure */
885 sgt_cache = this_cpu_ptr(priv->sgt_cache);
886 sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
887
888 if (sgt_cache->count == 0)
889 sgt_buf = kzalloc(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN,
890 GFP_ATOMIC);
891 else
892 sgt_buf = sgt_cache->buf[--sgt_cache->count];
893 if (unlikely(!sgt_buf))
894 return -ENOMEM;
895
896 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
897 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
898
899 addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
900 if (unlikely(dma_mapping_error(dev, addr))) {
901 err = -ENOMEM;
902 goto data_map_failed;
903 }
904
905 /* Fill in the HW SGT structure */
906 dpaa2_sg_set_addr(sgt, addr);
907 dpaa2_sg_set_len(sgt, skb->len);
908 dpaa2_sg_set_final(sgt, true);
909
910 /* Store the skb backpointer in the SGT buffer */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800911 *swa_addr = (void *)sgt_buf;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300912 swa = (struct dpaa2_eth_swa *)sgt_buf;
913 swa->type = DPAA2_ETH_SWA_SINGLE;
914 swa->single.skb = skb;
Ioana Ciornei54a57d12020-12-11 19:16:07 +0200915 swa->single.sgt_size = sgt_buf_size;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300916
917 /* Separately map the SGT buffer */
918 sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
919 if (unlikely(dma_mapping_error(dev, sgt_addr))) {
920 err = -ENOMEM;
921 goto sgt_map_failed;
922 }
923
924 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
925 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
926 dpaa2_fd_set_addr(fd, sgt_addr);
927 dpaa2_fd_set_len(fd, skb->len);
928 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
929
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300930 return 0;
931
932sgt_map_failed:
933 dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
934data_map_failed:
935 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
936 kfree(sgt_buf);
937 else
938 sgt_cache->buf[sgt_cache->count++] = sgt_buf;
939
940 return err;
941}
942
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500943/* Create a frame descriptor based on a linear skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300944static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
945 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800946 struct dpaa2_fd *fd,
947 void **swa_addr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500948{
949 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600950 u8 *buffer_start, *aligned_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000951 struct dpaa2_eth_swa *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500952 dma_addr_t addr;
953
Yangbo Lu1cf773b2020-09-18 17:08:01 +0800954 buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
Ioana Radulescuc1636852017-12-08 06:47:58 -0600955
956 /* If there's enough room to align the FD address, do it.
957 * It will help hardware optimize accesses.
958 */
959 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
960 DPAA2_ETH_TX_BUF_ALIGN);
961 if (aligned_start >= skb->head)
962 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500963
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500964 /* Store a backpointer to the skb at the beginning of the buffer
965 * (in the private data area) such that we can release it
966 * on Tx confirm
967 */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800968 *swa_addr = (void *)buffer_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000969 swa = (struct dpaa2_eth_swa *)buffer_start;
970 swa->type = DPAA2_ETH_SWA_SINGLE;
971 swa->single.skb = skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500972
973 addr = dma_map_single(dev, buffer_start,
974 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500975 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500976 if (unlikely(dma_mapping_error(dev, addr)))
977 return -ENOMEM;
978
979 dpaa2_fd_set_addr(fd, addr);
980 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
981 dpaa2_fd_set_len(fd, skb->len);
982 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000983 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500984
985 return 0;
986}
987
988/* FD freeing routine on the Tx path
989 *
990 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
991 * back-pointed to is also freed.
992 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
993 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500994 */
Yangbo Luc5521182020-09-18 17:08:02 +0800995static void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300996 struct dpaa2_eth_fq *fq,
997 const struct dpaa2_fd *fd, bool in_napi)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500998{
999 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001000 dma_addr_t fd_addr, sg_addr;
Ioana Radulescud678be12019-03-01 17:47:24 +00001001 struct sk_buff *skb = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001002 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001003 struct dpaa2_eth_swa *swa;
1004 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescud678be12019-03-01 17:47:24 +00001005 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001006
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001007 struct dpaa2_eth_sgt_cache *sgt_cache;
1008 struct dpaa2_sg_entry *sgt;
1009
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001010 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +00001011 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
1012 swa = (struct dpaa2_eth_swa *)buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001013
1014 if (fd_format == dpaa2_fd_single) {
Ioana Radulescud678be12019-03-01 17:47:24 +00001015 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
1016 skb = swa->single.skb;
1017 /* Accessing the skb buffer is safe before dma unmap,
1018 * because we didn't map the actual skb shell.
1019 */
1020 dma_unmap_single(dev, fd_addr,
1021 skb_tail_pointer(skb) - buffer_start,
1022 DMA_BIDIRECTIONAL);
1023 } else {
1024 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
1025 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
1026 DMA_BIDIRECTIONAL);
1027 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001028 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001029 if (swa->type == DPAA2_ETH_SWA_SG) {
1030 skb = swa->sg.skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001031
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001032 /* Unmap the scatterlist */
1033 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
1034 DMA_BIDIRECTIONAL);
1035 kfree(swa->sg.scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001036
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001037 /* Unmap the SGT buffer */
1038 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
1039 DMA_BIDIRECTIONAL);
1040 } else {
1041 skb = swa->single.skb;
1042
1043 /* Unmap the SGT Buffer */
1044 dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
1045 DMA_BIDIRECTIONAL);
1046
1047 sgt = (struct dpaa2_sg_entry *)(buffer_start +
1048 priv->tx_data_offset);
1049 sg_addr = dpaa2_sg_get_addr(sgt);
1050 dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
1051 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001052 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06001053 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001054 return;
1055 }
1056
Ioana Radulescud678be12019-03-01 17:47:24 +00001057 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
1058 fq->dq_frames++;
1059 fq->dq_bytes += fd_len;
1060 }
1061
1062 if (swa->type == DPAA2_ETH_SWA_XDP) {
1063 xdp_return_frame(swa->xdp.xdpf);
1064 return;
1065 }
1066
Ioana Radulescu859f9982018-04-26 18:23:47 +08001067 /* Get the timestamp value */
Yangbo Lu1cf773b2020-09-18 17:08:01 +08001068 if (skb->cb[0] == TX_TSTAMP) {
Ioana Radulescu859f9982018-04-26 18:23:47 +08001069 struct skb_shared_hwtstamps shhwtstamps;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +00001070 __le64 *ts = dpaa2_get_ts(buffer_start, true);
Ioana Radulescu859f9982018-04-26 18:23:47 +08001071 u64 ns;
1072
1073 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1074
1075 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
1076 shhwtstamps.hwtstamp = ns_to_ktime(ns);
1077 skb_tstamp_tx(skb, &shhwtstamps);
Yangbo Luc5521182020-09-18 17:08:02 +08001078 } else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1079 mutex_unlock(&priv->onestep_tstamp_lock);
Ioana Radulescu859f9982018-04-26 18:23:47 +08001080 }
1081
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -05001082 /* Free SGT buffer allocated on tx */
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001083 if (fd_format != dpaa2_fd_single) {
1084 sgt_cache = this_cpu_ptr(priv->sgt_cache);
1085 if (swa->type == DPAA2_ETH_SWA_SG) {
1086 skb_free_frag(buffer_start);
1087 } else {
1088 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
1089 kfree(buffer_start);
1090 else
1091 sgt_cache->buf[sgt_cache->count++] = buffer_start;
1092 }
1093 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001094
1095 /* Move on with skb release */
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +00001096 napi_consume_skb(skb, in_napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001097}
1098
Yangbo Luc5521182020-09-18 17:08:02 +08001099static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1100 struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001101{
1102 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1103 struct dpaa2_fd fd;
1104 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001105 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001106 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001107 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001108 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -06001109 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001110 u32 fd_len;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001111 u8 prio = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001112 int err, i;
Yangbo Lu64a965d2020-09-18 17:08:00 +08001113 void *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001114
1115 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001116 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001117
Yangbo Lu1cf773b2020-09-18 17:08:01 +08001118 needed_headroom = dpaa2_eth_needed_headroom(skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001119
1120 /* We'll be holding a back-reference to the skb until Tx Confirmation;
1121 * we don't want that overwritten by a concurrent Tx with a cloned skb.
1122 */
1123 skb = skb_unshare(skb, GFP_ATOMIC);
1124 if (unlikely(!skb)) {
1125 /* skb_unshare() has already freed the skb */
1126 percpu_stats->tx_dropped++;
1127 return NETDEV_TX_OK;
1128 }
1129
1130 /* Setup the FD fields */
1131 memset(&fd, 0, sizeof(fd));
1132
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001133 if (skb_is_nonlinear(skb)) {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001134 err = dpaa2_eth_build_sg_fd(priv, skb, &fd, &swa);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001135 percpu_extras->tx_sg_frames++;
1136 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001137 } else if (skb_headroom(skb) < needed_headroom) {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001138 err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, &fd, &swa);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001139 percpu_extras->tx_sg_frames++;
1140 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciornei4c96c0a2020-06-29 21:47:12 +03001141 percpu_extras->tx_converted_sg_frames++;
1142 percpu_extras->tx_converted_sg_bytes += skb->len;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001143 } else {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001144 err = dpaa2_eth_build_single_fd(priv, skb, &fd, &swa);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001145 }
1146
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001147 if (unlikely(err)) {
1148 percpu_stats->tx_dropped++;
1149 goto err_build_fd;
1150 }
1151
Yangbo Luc5521182020-09-18 17:08:02 +08001152 if (skb->cb[0])
1153 dpaa2_eth_enable_tx_tstamp(priv, &fd, swa, skb);
Yangbo Lu64a965d2020-09-18 17:08:00 +08001154
Ioana Radulescu56361872017-04-28 04:50:32 -05001155 /* Tracing point */
1156 trace_dpaa2_tx_fd(net_dev, &fd);
1157
Ioana Radulescu537336c2017-12-21 06:33:20 -06001158 /* TxConf FQ selection relies on queue id from the stack.
1159 * In case of a forwarded frame from another DPNI interface, we choose
1160 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001161 */
Ioana Radulescu537336c2017-12-21 06:33:20 -06001162 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001163
1164 if (net_dev->num_tc) {
1165 prio = netdev_txq_to_tc(net_dev, queue_mapping);
1166 /* Hardware interprets priority level 0 as being the highest,
1167 * so we need to do a reverse mapping to the netdev tc index
1168 */
1169 prio = net_dev->num_tc - prio - 1;
1170 /* We have only one FQ array entry for all Tx hardware queues
1171 * with the same flow id (but different priority levels)
1172 */
1173 queue_mapping %= dpaa2_eth_queue_count(priv);
1174 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001175 fq = &priv->fq[queue_mapping];
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001176
1177 fd_len = dpaa2_fd_get_len(&fd);
1178 nq = netdev_get_tx_queue(net_dev, queue_mapping);
1179 netdev_tx_sent_queue(nq, fd_len);
1180
1181 /* Everything that happens after this enqueues might race with
1182 * the Tx confirmation callback for this frame
1183 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001184 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Ciornei6ff80442020-04-22 15:05:11 +03001185 err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001186 if (err != -EBUSY)
1187 break;
1188 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001189 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001190 if (unlikely(err < 0)) {
1191 percpu_stats->tx_errors++;
1192 /* Clean up everything, including freeing the skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001193 dpaa2_eth_free_tx_fd(priv, fq, &fd, false);
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001194 netdev_tx_completed_queue(nq, 1, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001195 } else {
1196 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001197 percpu_stats->tx_bytes += fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001198 }
1199
1200 return NETDEV_TX_OK;
1201
1202err_build_fd:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001203 dev_kfree_skb(skb);
1204
1205 return NETDEV_TX_OK;
1206}
1207
Yangbo Luc5521182020-09-18 17:08:02 +08001208static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1209{
1210 struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1211 tx_onestep_tstamp);
1212 struct sk_buff *skb;
1213
1214 while (true) {
1215 skb = skb_dequeue(&priv->tx_skbs);
1216 if (!skb)
1217 return;
1218
1219 /* Lock just before TX one-step timestamping packet,
1220 * and release the lock in dpaa2_eth_free_tx_fd when
1221 * confirm the packet has been sent on hardware, or
1222 * when clean up during transmit failure.
1223 */
1224 mutex_lock(&priv->onestep_tstamp_lock);
1225 __dpaa2_eth_tx(skb, priv->net_dev);
1226 }
1227}
1228
1229static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1230{
1231 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1232 u8 msgtype, twostep, udp;
1233 u16 offset1, offset2;
1234
1235 /* Utilize skb->cb[0] for timestamping request per skb */
1236 skb->cb[0] = 0;
1237
1238 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1239 if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1240 skb->cb[0] = TX_TSTAMP;
1241 else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1242 skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1243 }
1244
1245 /* TX for one-step timestamping PTP Sync packet */
1246 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1247 if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1248 &offset1, &offset2))
Christian Eggers6b6817c2020-11-20 09:41:05 +01001249 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) {
Yangbo Luc5521182020-09-18 17:08:02 +08001250 skb_queue_tail(&priv->tx_skbs, skb);
1251 queue_work(priv->dpaa2_ptp_wq,
1252 &priv->tx_onestep_tstamp);
1253 return NETDEV_TX_OK;
1254 }
1255 /* Use two-step timestamping if not one-step timestamping
1256 * PTP Sync packet
1257 */
1258 skb->cb[0] = TX_TSTAMP;
1259 }
1260
1261 /* TX for other packets */
1262 return __dpaa2_eth_tx(skb, net_dev);
1263}
1264
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001265/* Tx confirmation frame processing routine */
1266static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +00001267 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001268 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001269 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001270{
1271 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001272 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001273 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001274 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001275
Ioana Radulescu56361872017-04-28 04:50:32 -05001276 /* Tracing point */
1277 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1278
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001279 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1280 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001281 percpu_extras->tx_conf_bytes += fd_len;
1282
Ioana Radulescu39163c02017-06-06 10:00:39 -05001283 /* Check frame errors in the FD field */
1284 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001285 dpaa2_eth_free_tx_fd(priv, fq, fd, true);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001286
1287 if (likely(!fd_errors))
1288 return;
1289
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06001290 if (net_ratelimit())
1291 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1292 fd_errors);
1293
Ioana Radulescu39163c02017-06-06 10:00:39 -05001294 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1295 /* Tx-conf logically pertains to the egress path. */
1296 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001297}
1298
Ionut-robert Aron70b32d82021-01-11 19:07:25 +02001299static int dpaa2_eth_set_rx_vlan_filtering(struct dpaa2_eth_priv *priv,
1300 bool enable)
1301{
1302 int err;
1303
1304 err = dpni_enable_vlan_filter(priv->mc_io, 0, priv->mc_token, enable);
1305
1306 if (err) {
1307 netdev_err(priv->net_dev,
1308 "dpni_enable_vlan_filter failed\n");
1309 return err;
1310 }
1311
1312 return 0;
1313}
1314
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001315static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001316{
1317 int err;
1318
1319 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1320 DPNI_OFF_RX_L3_CSUM, enable);
1321 if (err) {
1322 netdev_err(priv->net_dev,
1323 "dpni_set_offload(RX_L3_CSUM) failed\n");
1324 return err;
1325 }
1326
1327 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1328 DPNI_OFF_RX_L4_CSUM, enable);
1329 if (err) {
1330 netdev_err(priv->net_dev,
1331 "dpni_set_offload(RX_L4_CSUM) failed\n");
1332 return err;
1333 }
1334
1335 return 0;
1336}
1337
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001338static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001339{
1340 int err;
1341
1342 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1343 DPNI_OFF_TX_L3_CSUM, enable);
1344 if (err) {
1345 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1346 return err;
1347 }
1348
1349 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1350 DPNI_OFF_TX_L4_CSUM, enable);
1351 if (err) {
1352 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1353 return err;
1354 }
1355
1356 return 0;
1357}
1358
1359/* Perform a single release command to add buffers
1360 * to the specified buffer pool
1361 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001362static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1363 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001364{
1365 struct device *dev = priv->net_dev->dev.parent;
1366 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001367 struct page *page;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001368 dma_addr_t addr;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001369 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001370 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001371
1372 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1373 /* Allocate buffer visible to WRIOP + skb shared info +
1374 * alignment padding
1375 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001376 /* allocate one page for each Rx buffer. WRIOP sees
1377 * the entire page except for a tailroom reserved for
1378 * skb shared info
1379 */
1380 page = dev_alloc_pages(0);
1381 if (!page)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001382 goto err_alloc;
1383
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001384 addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001385 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001386 if (unlikely(dma_mapping_error(dev, addr)))
1387 goto err_map;
1388
1389 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -05001390
1391 /* tracing point */
1392 trace_dpaa2_eth_buf_seed(priv->net_dev,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001393 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001394 addr, priv->rx_buf_size,
Ioana Radulescu56361872017-04-28 04:50:32 -05001395 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001396 }
1397
1398release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001399 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001400 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001401 buf_array, i)) == -EBUSY) {
1402 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1403 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001404 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001405 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001406
1407 /* If release command failed, clean up and bail out;
1408 * not much else we can do about it
1409 */
1410 if (err) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001411 dpaa2_eth_free_bufs(priv, buf_array, i);
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001412 return 0;
1413 }
1414
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001415 return i;
1416
1417err_map:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001418 __free_pages(page, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001419err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001420 /* If we managed to allocate at least some buffers,
1421 * release them to hardware
1422 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001423 if (i)
1424 goto release_bufs;
1425
1426 return 0;
1427}
1428
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001429static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001430{
1431 int i, j;
1432 int new_count;
1433
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001434 for (j = 0; j < priv->num_channels; j++) {
1435 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1436 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001437 new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001438 priv->channel[j]->buf_count += new_count;
1439
1440 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001441 return -ENOMEM;
1442 }
1443 }
1444 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001445
1446 return 0;
1447}
1448
Jesse Brandeburgd0ea5cb2020-09-25 15:24:45 -07001449/*
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001450 * Drain the specified number of buffers from the DPNI's private buffer pool.
1451 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1452 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001453static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001454{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001455 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001456 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001457 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001458
1459 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001460 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001461 buf_array, count);
1462 if (ret < 0) {
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001463 if (ret == -EBUSY &&
Ioana Ciornei0e5ad752020-06-24 14:34:19 +03001464 retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001465 continue;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001466 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1467 return;
1468 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001469 dpaa2_eth_free_bufs(priv, buf_array, ret);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001470 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001471 } while (ret);
1472}
1473
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001474static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001475{
1476 int i;
1477
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001478 dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1479 dpaa2_eth_drain_bufs(priv, 1);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001480
1481 for (i = 0; i < priv->num_channels; i++)
1482 priv->channel[i]->buf_count = 0;
1483}
1484
1485/* Function is called from softirq context only, so we don't need to guard
1486 * the access to percpu count
1487 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001488static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1489 struct dpaa2_eth_channel *ch,
1490 u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001491{
1492 int new_count;
1493
1494 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1495 return 0;
1496
1497 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001498 new_count = dpaa2_eth_add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001499 if (unlikely(!new_count)) {
1500 /* Out of memory; abort for now, we'll try later on */
1501 break;
1502 }
1503 ch->buf_count += new_count;
1504 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1505
1506 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1507 return -ENOMEM;
1508
1509 return 0;
1510}
1511
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001512static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1513{
1514 struct dpaa2_eth_sgt_cache *sgt_cache;
1515 u16 count;
1516 int k, i;
1517
Ioana Ciornei0fe665d2020-07-06 17:55:54 +03001518 for_each_possible_cpu(k) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001519 sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1520 count = sgt_cache->count;
1521
1522 for (i = 0; i < count; i++)
1523 kfree(sgt_cache->buf[i]);
1524 sgt_cache->count = 0;
1525 }
1526}
1527
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001528static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001529{
1530 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001531 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001532
1533 /* Retry while portal is busy */
1534 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001535 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1536 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001537 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001538 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001539 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001540
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001541 ch->stats.dequeue_portal_busy += dequeues;
1542 if (unlikely(err))
1543 ch->stats.pull_err++;
1544
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001545 return err;
1546}
1547
1548/* NAPI poll routine
1549 *
1550 * Frames are dequeued from the QMan channel associated with this NAPI context.
1551 * Rx, Tx confirmation and (if configured) Rx error frames all count
1552 * towards the NAPI budget.
1553 */
1554static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1555{
1556 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001557 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001558 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001559 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1560 struct netdev_queue *nq;
1561 int store_cleaned, work_done;
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001562 struct list_head rx_list;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001563 int retries = 0;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001564 u16 flowid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001565 int err;
1566
1567 ch = container_of(napi, struct dpaa2_eth_channel, napi);
Ioana Radulescud678be12019-03-01 17:47:24 +00001568 ch->xdp.res = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001569 priv = ch->priv;
1570
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001571 INIT_LIST_HEAD(&rx_list);
1572 ch->rx_list = &rx_list;
1573
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001574 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001575 err = dpaa2_eth_pull_channel(ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001576 if (unlikely(err))
1577 break;
1578
1579 /* Refill pool if appropriate */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001580 dpaa2_eth_refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001581
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001582 store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001583 if (store_cleaned <= 0)
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001584 break;
1585 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001586 rx_cleaned += store_cleaned;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001587 flowid = fq->flowid;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001588 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001589 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001590 /* We have a single Tx conf FQ on this channel */
1591 txc_fq = fq;
1592 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001593
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001594 /* If we either consumed the whole NAPI budget with Rx frames
1595 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001596 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001597 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001598 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1599 work_done = budget;
1600 goto out;
1601 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001602 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001603
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001604 /* We didn't consume the entire budget, so finish napi and
1605 * re-enable data availability notifications
1606 */
1607 napi_complete_done(napi, rx_cleaned);
1608 do {
1609 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1610 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001611 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001612 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1613 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001614
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001615 work_done = max(rx_cleaned, 1);
1616
1617out:
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001618 netif_receive_skb_list(ch->rx_list);
1619
Ioana Radulescud678be12019-03-01 17:47:24 +00001620 if (txc_fq && txc_fq->dq_frames) {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001621 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1622 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1623 txc_fq->dq_bytes);
1624 txc_fq->dq_frames = 0;
1625 txc_fq->dq_bytes = 0;
1626 }
1627
Ioana Radulescud678be12019-03-01 17:47:24 +00001628 if (ch->xdp.res & XDP_REDIRECT)
1629 xdp_do_flush_map();
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001630 else if (rx_cleaned && ch->xdp.res & XDP_TX)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001631 dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
Ioana Radulescud678be12019-03-01 17:47:24 +00001632
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001633 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001634}
1635
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001636static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001637{
1638 struct dpaa2_eth_channel *ch;
1639 int i;
1640
1641 for (i = 0; i < priv->num_channels; i++) {
1642 ch = priv->channel[i];
1643 napi_enable(&ch->napi);
1644 }
1645}
1646
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001647static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001648{
1649 struct dpaa2_eth_channel *ch;
1650 int i;
1651
1652 for (i = 0; i < priv->num_channels; i++) {
1653 ch = priv->channel[i];
1654 napi_disable(&ch->napi);
1655 }
1656}
1657
Ioana Ciornei07beb162020-05-31 00:08:14 +03001658void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1659 bool tx_pause, bool pfc)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001660{
1661 struct dpni_taildrop td = {0};
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001662 struct dpaa2_eth_fq *fq;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001663 int i, err;
1664
Ioana Ciornei07beb162020-05-31 00:08:14 +03001665 /* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1666 * flow control is disabled (as it might interfere with either the
1667 * buffer pool depletion trigger for pause frames or with the group
1668 * congestion trigger for PFC frames)
1669 */
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001670 td.enable = !tx_pause;
Ioana Ciornei07beb162020-05-31 00:08:14 +03001671 if (priv->rx_fqtd_enabled == td.enable)
1672 goto set_cgtd;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001673
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001674 td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1675 td.units = DPNI_CONGESTION_UNIT_BYTES;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001676
1677 for (i = 0; i < priv->num_fqs; i++) {
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001678 fq = &priv->fq[i];
1679 if (fq->type != DPAA2_RX_FQ)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001680 continue;
1681 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001682 DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1683 fq->tc, fq->flowid, &td);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001684 if (err) {
1685 netdev_err(priv->net_dev,
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001686 "dpni_set_taildrop(FQ) failed\n");
1687 return;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001688 }
1689 }
1690
Ioana Ciornei07beb162020-05-31 00:08:14 +03001691 priv->rx_fqtd_enabled = td.enable;
1692
1693set_cgtd:
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001694 /* Congestion group taildrop: threshold is in frames, per group
1695 * of FQs belonging to the same traffic class
Ioana Ciornei07beb162020-05-31 00:08:14 +03001696 * Enabled if general Tx pause disabled or if PFCs are enabled
1697 * (congestion group threhsold for PFC generation is lower than the
1698 * CG taildrop threshold, so it won't interfere with it; we also
1699 * want frames in non-PFC enabled traffic classes to be kept in check)
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001700 */
Jiapeng Chongb91b3a22021-02-02 18:02:37 +08001701 td.enable = !tx_pause || pfc;
Ioana Ciornei07beb162020-05-31 00:08:14 +03001702 if (priv->rx_cgtd_enabled == td.enable)
1703 return;
1704
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001705 td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
1706 td.units = DPNI_CONGESTION_UNIT_FRAMES;
1707 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
1708 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1709 DPNI_CP_GROUP, DPNI_QUEUE_RX,
1710 i, 0, &td);
1711 if (err) {
1712 netdev_err(priv->net_dev,
1713 "dpni_set_taildrop(CG) failed\n");
1714 return;
1715 }
1716 }
1717
Ioana Ciornei07beb162020-05-31 00:08:14 +03001718 priv->rx_cgtd_enabled = td.enable;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001719}
1720
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001721static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001722{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001723 struct dpni_link_state state = {0};
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001724 bool tx_pause;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001725 int err;
1726
1727 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1728 if (unlikely(err)) {
1729 netdev_err(priv->net_dev,
1730 "dpni_get_link_state() failed\n");
1731 return err;
1732 }
1733
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001734 /* If Tx pause frame settings have changed, we need to update
1735 * Rx FQ taildrop configuration as well. We configure taildrop
1736 * only when pause frame generation is disabled.
1737 */
Ioana Radulescuad054f22020-05-31 00:08:10 +03001738 tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
Ioana Ciornei07beb162020-05-31 00:08:14 +03001739 dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001740
Ioana Ciornei71947922019-10-31 01:18:31 +02001741 /* When we manage the MAC/PHY using phylink there is no need
1742 * to manually update the netif_carrier.
1743 */
Ioana Ciorneid87e6062021-01-08 11:07:23 +02001744 if (dpaa2_eth_is_type_phy(priv))
Ioana Ciornei71947922019-10-31 01:18:31 +02001745 goto out;
1746
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001747 /* Chech link state; speed / duplex changes are not treated yet */
1748 if (priv->link_state.up == state.up)
Ioana Radulescucce629432019-08-28 17:08:14 +03001749 goto out;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001750
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001751 if (state.up) {
1752 netif_carrier_on(priv->net_dev);
1753 netif_tx_start_all_queues(priv->net_dev);
1754 } else {
1755 netif_tx_stop_all_queues(priv->net_dev);
1756 netif_carrier_off(priv->net_dev);
1757 }
1758
Ioana Radulescu77160af2017-06-06 10:00:28 -05001759 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001760 state.up ? "up" : "down");
1761
Ioana Radulescucce629432019-08-28 17:08:14 +03001762out:
1763 priv->link_state = state;
1764
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001765 return 0;
1766}
1767
1768static int dpaa2_eth_open(struct net_device *net_dev)
1769{
1770 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1771 int err;
1772
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001773 err = dpaa2_eth_seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001774 if (err) {
1775 /* Not much to do; the buffer pool, though not filled up,
1776 * may still contain some buffers which would enable us
1777 * to limp on.
1778 */
1779 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001780 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001781 }
1782
Ioana Ciorneid87e6062021-01-08 11:07:23 +02001783 if (!dpaa2_eth_is_type_phy(priv)) {
Ioana Ciornei71947922019-10-31 01:18:31 +02001784 /* We'll only start the txqs when the link is actually ready;
1785 * make sure we don't race against the link up notification,
1786 * which may come immediately after dpni_enable();
1787 */
1788 netif_tx_stop_all_queues(net_dev);
1789
1790 /* Also, explicitly set carrier off, otherwise
1791 * netif_carrier_ok() will return true and cause 'ip link show'
1792 * to report the LOWER_UP flag, even though the link
1793 * notification wasn't even received.
1794 */
1795 netif_carrier_off(net_dev);
1796 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001797 dpaa2_eth_enable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001798
1799 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1800 if (err < 0) {
1801 netdev_err(net_dev, "dpni_enable() failed\n");
1802 goto enable_err;
1803 }
1804
Ioana Ciorneid87e6062021-01-08 11:07:23 +02001805 if (dpaa2_eth_is_type_phy(priv))
Ioana Ciornei71947922019-10-31 01:18:31 +02001806 phylink_start(priv->mac->phylink);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001807
1808 return 0;
1809
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001810enable_err:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001811 dpaa2_eth_disable_ch_napi(priv);
1812 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001813 return err;
1814}
1815
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001816/* Total number of in-flight frames on ingress queues */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001817static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001818{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001819 struct dpaa2_eth_fq *fq;
1820 u32 fcnt = 0, bcnt = 0, total = 0;
1821 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001822
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001823 for (i = 0; i < priv->num_fqs; i++) {
1824 fq = &priv->fq[i];
1825 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1826 if (err) {
1827 netdev_warn(priv->net_dev, "query_fq_count failed");
1828 break;
1829 }
1830 total += fcnt;
1831 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001832
1833 return total;
1834}
1835
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001836static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001837{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001838 int retries = 10;
1839 u32 pending;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001840
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001841 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001842 pending = dpaa2_eth_ingress_fq_count(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001843 if (pending)
1844 msleep(100);
1845 } while (pending && --retries);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001846}
1847
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001848#define DPNI_TX_PENDING_VER_MAJOR 7
1849#define DPNI_TX_PENDING_VER_MINOR 13
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001850static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001851{
1852 union dpni_statistics stats;
1853 int retries = 10;
1854 int err;
1855
1856 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1857 DPNI_TX_PENDING_VER_MINOR) < 0)
1858 goto out;
1859
1860 do {
1861 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1862 &stats);
1863 if (err)
1864 goto out;
1865 if (stats.page_6.tx_pending_frames == 0)
1866 return;
1867 } while (--retries);
1868
1869out:
1870 msleep(500);
1871}
1872
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001873static int dpaa2_eth_stop(struct net_device *net_dev)
1874{
1875 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001876 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001877 int retries = 10;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001878
Ioana Ciorneid87e6062021-01-08 11:07:23 +02001879 if (dpaa2_eth_is_type_phy(priv)) {
1880 phylink_stop(priv->mac->phylink);
1881 } else {
Ioana Ciornei71947922019-10-31 01:18:31 +02001882 netif_tx_stop_all_queues(net_dev);
1883 netif_carrier_off(net_dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02001884 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001885
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001886 /* On dpni_disable(), the MC firmware will:
1887 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1888 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1889 * of all in flight Tx frames is finished (and corresponding Tx conf
1890 * frames are enqueued back to software)
1891 *
1892 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1893 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1894 * and Tx conf queues are consumed on NAPI poll.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001895 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001896 dpaa2_eth_wait_for_egress_fq_empty(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001897
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001898 do {
1899 dpni_disable(priv->mc_io, 0, priv->mc_token);
1900 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1901 if (dpni_enabled)
1902 /* Allow the hardware some slack */
1903 msleep(100);
1904 } while (dpni_enabled && --retries);
1905 if (!retries) {
1906 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1907 /* Must go on and disable NAPI nonetheless, so we don't crash at
1908 * the next "ifconfig up"
1909 */
1910 }
1911
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001912 dpaa2_eth_wait_for_ingress_fq_empty(priv);
1913 dpaa2_eth_disable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001914
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001915 /* Empty the buffer pool */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001916 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001917
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001918 /* Empty the Scatter-Gather Buffer cache */
1919 dpaa2_eth_sgt_cache_drain(priv);
1920
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001921 return 0;
1922}
1923
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001924static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1925{
1926 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1927 struct device *dev = net_dev->dev.parent;
1928 int err;
1929
1930 err = eth_mac_addr(net_dev, addr);
1931 if (err < 0) {
1932 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1933 return err;
1934 }
1935
1936 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1937 net_dev->dev_addr);
1938 if (err) {
1939 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1940 return err;
1941 }
1942
1943 return 0;
1944}
1945
1946/** Fill in counters maintained by the GPP driver. These may be different from
1947 * the hardware counters obtained by ethtool.
1948 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001949static void dpaa2_eth_get_stats(struct net_device *net_dev,
1950 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001951{
1952 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1953 struct rtnl_link_stats64 *percpu_stats;
1954 u64 *cpustats;
1955 u64 *netstats = (u64 *)stats;
1956 int i, j;
1957 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1958
1959 for_each_possible_cpu(i) {
1960 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1961 cpustats = (u64 *)percpu_stats;
1962 for (j = 0; j < num; j++)
1963 netstats[j] += cpustats[j];
1964 }
1965}
1966
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001967/* Copy mac unicast addresses from @net_dev to @priv.
1968 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1969 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001970static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
1971 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001972{
1973 struct netdev_hw_addr *ha;
1974 int err;
1975
1976 netdev_for_each_uc_addr(ha, net_dev) {
1977 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1978 ha->addr);
1979 if (err)
1980 netdev_warn(priv->net_dev,
1981 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1982 ha->addr, err);
1983 }
1984}
1985
1986/* Copy mac multicast addresses from @net_dev to @priv
1987 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1988 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001989static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
1990 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001991{
1992 struct netdev_hw_addr *ha;
1993 int err;
1994
1995 netdev_for_each_mc_addr(ha, net_dev) {
1996 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1997 ha->addr);
1998 if (err)
1999 netdev_warn(priv->net_dev,
2000 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
2001 ha->addr, err);
2002 }
2003}
2004
Ionut-robert Aron70b32d82021-01-11 19:07:25 +02002005static int dpaa2_eth_rx_add_vid(struct net_device *net_dev,
2006 __be16 vlan_proto, u16 vid)
2007{
2008 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2009 int err;
2010
2011 err = dpni_add_vlan_id(priv->mc_io, 0, priv->mc_token,
2012 vid, 0, 0, 0);
2013
2014 if (err) {
2015 netdev_warn(priv->net_dev,
2016 "Could not add the vlan id %u\n",
2017 vid);
2018 return err;
2019 }
2020
2021 return 0;
2022}
2023
2024static int dpaa2_eth_rx_kill_vid(struct net_device *net_dev,
2025 __be16 vlan_proto, u16 vid)
2026{
2027 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2028 int err;
2029
2030 err = dpni_remove_vlan_id(priv->mc_io, 0, priv->mc_token, vid);
2031
2032 if (err) {
2033 netdev_warn(priv->net_dev,
2034 "Could not remove the vlan id %u\n",
2035 vid);
2036 return err;
2037 }
2038
2039 return 0;
2040}
2041
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002042static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
2043{
2044 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2045 int uc_count = netdev_uc_count(net_dev);
2046 int mc_count = netdev_mc_count(net_dev);
2047 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
2048 u32 options = priv->dpni_attrs.options;
2049 u16 mc_token = priv->mc_token;
2050 struct fsl_mc_io *mc_io = priv->mc_io;
2051 int err;
2052
2053 /* Basic sanity checks; these probably indicate a misconfiguration */
2054 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
2055 netdev_info(net_dev,
2056 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
2057 max_mac);
2058
2059 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
2060 if (uc_count > max_mac) {
2061 netdev_info(net_dev,
2062 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
2063 uc_count, max_mac);
2064 goto force_promisc;
2065 }
2066 if (mc_count + uc_count > max_mac) {
2067 netdev_info(net_dev,
2068 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
2069 uc_count + mc_count, max_mac);
2070 goto force_mc_promisc;
2071 }
2072
2073 /* Adjust promisc settings due to flag combinations */
2074 if (net_dev->flags & IFF_PROMISC)
2075 goto force_promisc;
2076 if (net_dev->flags & IFF_ALLMULTI) {
2077 /* First, rebuild unicast filtering table. This should be done
2078 * in promisc mode, in order to avoid frame loss while we
2079 * progressively add entries to the table.
2080 * We don't know whether we had been in promisc already, and
2081 * making an MC call to find out is expensive; so set uc promisc
2082 * nonetheless.
2083 */
2084 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2085 if (err)
2086 netdev_warn(net_dev, "Can't set uc promisc\n");
2087
2088 /* Actual uc table reconstruction. */
2089 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
2090 if (err)
2091 netdev_warn(net_dev, "Can't clear uc filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002092 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002093
2094 /* Finally, clear uc promisc and set mc promisc as requested. */
2095 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2096 if (err)
2097 netdev_warn(net_dev, "Can't clear uc promisc\n");
2098 goto force_mc_promisc;
2099 }
2100
2101 /* Neither unicast, nor multicast promisc will be on... eventually.
2102 * For now, rebuild mac filtering tables while forcing both of them on.
2103 */
2104 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2105 if (err)
2106 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
2107 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2108 if (err)
2109 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
2110
2111 /* Actual mac filtering tables reconstruction */
2112 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
2113 if (err)
2114 netdev_warn(net_dev, "Can't clear mac filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002115 dpaa2_eth_add_mc_hw_addr(net_dev, priv);
2116 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002117
2118 /* Now we can clear both ucast and mcast promisc, without risking
2119 * to drop legitimate frames anymore.
2120 */
2121 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2122 if (err)
2123 netdev_warn(net_dev, "Can't clear ucast promisc\n");
2124 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
2125 if (err)
2126 netdev_warn(net_dev, "Can't clear mcast promisc\n");
2127
2128 return;
2129
2130force_promisc:
2131 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2132 if (err)
2133 netdev_warn(net_dev, "Can't set ucast promisc\n");
2134force_mc_promisc:
2135 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2136 if (err)
2137 netdev_warn(net_dev, "Can't set mcast promisc\n");
2138}
2139
2140static int dpaa2_eth_set_features(struct net_device *net_dev,
2141 netdev_features_t features)
2142{
2143 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2144 netdev_features_t changed = features ^ net_dev->features;
2145 bool enable;
2146 int err;
2147
Ionut-robert Aron70b32d82021-01-11 19:07:25 +02002148 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
2149 enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER);
2150 err = dpaa2_eth_set_rx_vlan_filtering(priv, enable);
2151 if (err)
2152 return err;
2153 }
2154
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002155 if (changed & NETIF_F_RXCSUM) {
2156 enable = !!(features & NETIF_F_RXCSUM);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002157 err = dpaa2_eth_set_rx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002158 if (err)
2159 return err;
2160 }
2161
2162 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2163 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002164 err = dpaa2_eth_set_tx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002165 if (err)
2166 return err;
2167 }
2168
2169 return 0;
2170}
2171
Ioana Radulescu859f9982018-04-26 18:23:47 +08002172static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2173{
2174 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2175 struct hwtstamp_config config;
2176
Yangbo Luc5521182020-09-18 17:08:02 +08002177 if (!dpaa2_ptp)
2178 return -EINVAL;
2179
Ioana Radulescu859f9982018-04-26 18:23:47 +08002180 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
2181 return -EFAULT;
2182
2183 switch (config.tx_type) {
2184 case HWTSTAMP_TX_OFF:
Ioana Radulescu859f9982018-04-26 18:23:47 +08002185 case HWTSTAMP_TX_ON:
Yangbo Luc5521182020-09-18 17:08:02 +08002186 case HWTSTAMP_TX_ONESTEP_SYNC:
Yangbo Lu1cf773b2020-09-18 17:08:01 +08002187 priv->tx_tstamp_type = config.tx_type;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002188 break;
2189 default:
2190 return -ERANGE;
2191 }
2192
2193 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
2194 priv->rx_tstamp = false;
2195 } else {
2196 priv->rx_tstamp = true;
2197 /* TS is set for all frame types, not only those requested */
2198 config.rx_filter = HWTSTAMP_FILTER_ALL;
2199 }
2200
2201 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
2202 -EFAULT : 0;
2203}
2204
2205static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2206{
Russell King4a841822020-02-27 12:00:21 +00002207 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2208
Ioana Radulescu859f9982018-04-26 18:23:47 +08002209 if (cmd == SIOCSHWTSTAMP)
2210 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
2211
Ioana Ciorneid87e6062021-01-08 11:07:23 +02002212 if (dpaa2_eth_is_type_phy(priv))
Russell King4a841822020-02-27 12:00:21 +00002213 return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2214
2215 return -EOPNOTSUPP;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002216}
2217
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002218static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2219{
2220 int mfl, linear_mfl;
2221
2222 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03002223 linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002224 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002225
2226 if (mfl > linear_mfl) {
2227 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2228 linear_mfl - VLAN_ETH_HLEN);
2229 return false;
2230 }
2231
2232 return true;
2233}
2234
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002235static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002236{
2237 int mfl, err;
2238
2239 /* We enforce a maximum Rx frame length based on MTU only if we have
2240 * an XDP program attached (in order to avoid Rx S/G frames).
2241 * Otherwise, we accept all incoming frames as long as they are not
2242 * larger than maximum size supported in hardware
2243 */
2244 if (has_xdp)
2245 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2246 else
2247 mfl = DPAA2_ETH_MFL;
2248
2249 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2250 if (err) {
2251 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2252 return err;
2253 }
2254
2255 return 0;
2256}
2257
2258static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2259{
2260 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2261 int err;
2262
2263 if (!priv->xdp_prog)
2264 goto out;
2265
2266 if (!xdp_mtu_valid(priv, new_mtu))
2267 return -EINVAL;
2268
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002269 err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002270 if (err)
2271 return err;
2272
2273out:
2274 dev->mtu = new_mtu;
2275 return 0;
2276}
2277
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002278static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002279{
2280 struct dpni_buffer_layout buf_layout = {0};
2281 int err;
2282
2283 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2284 DPNI_QUEUE_RX, &buf_layout);
2285 if (err) {
2286 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2287 return err;
2288 }
2289
2290 /* Reserve extra headroom for XDP header size changes */
2291 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2292 (has_xdp ? XDP_PACKET_HEADROOM : 0);
2293 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2294 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2295 DPNI_QUEUE_RX, &buf_layout);
2296 if (err) {
2297 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2298 return err;
2299 }
2300
2301 return 0;
2302}
2303
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002304static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002305{
2306 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2307 struct dpaa2_eth_channel *ch;
2308 struct bpf_prog *old;
2309 bool up, need_update;
2310 int i, err;
2311
2312 if (prog && !xdp_mtu_valid(priv, dev->mtu))
2313 return -EINVAL;
2314
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002315 if (prog)
2316 bpf_prog_add(prog, priv->num_channels);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002317
2318 up = netif_running(dev);
2319 need_update = (!!priv->xdp_prog != !!prog);
2320
2321 if (up)
2322 dpaa2_eth_stop(dev);
2323
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002324 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2325 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2326 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2327 * so we are sure no old format buffers will be used from now on.
2328 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002329 if (need_update) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002330 err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002331 if (err)
2332 goto out_err;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002333 err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002334 if (err)
2335 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002336 }
2337
2338 old = xchg(&priv->xdp_prog, prog);
2339 if (old)
2340 bpf_prog_put(old);
2341
2342 for (i = 0; i < priv->num_channels; i++) {
2343 ch = priv->channel[i];
2344 old = xchg(&ch->xdp.prog, prog);
2345 if (old)
2346 bpf_prog_put(old);
2347 }
2348
2349 if (up) {
2350 err = dpaa2_eth_open(dev);
2351 if (err)
2352 return err;
2353 }
2354
2355 return 0;
2356
2357out_err:
2358 if (prog)
2359 bpf_prog_sub(prog, priv->num_channels);
2360 if (up)
2361 dpaa2_eth_open(dev);
2362
2363 return err;
2364}
2365
2366static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2367{
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002368 switch (xdp->command) {
2369 case XDP_SETUP_PROG:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002370 return dpaa2_eth_setup_xdp(dev, xdp->prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002371 default:
2372 return -EINVAL;
2373 }
2374
2375 return 0;
2376}
2377
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002378static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2379 struct xdp_frame *xdpf,
2380 struct dpaa2_fd *fd)
Ioana Radulescud678be12019-03-01 17:47:24 +00002381{
Ioana Radulescud678be12019-03-01 17:47:24 +00002382 struct device *dev = net_dev->dev.parent;
Ioana Radulescud678be12019-03-01 17:47:24 +00002383 unsigned int needed_headroom;
2384 struct dpaa2_eth_swa *swa;
Ioana Radulescud678be12019-03-01 17:47:24 +00002385 void *buffer_start, *aligned_start;
2386 dma_addr_t addr;
Ioana Radulescud678be12019-03-01 17:47:24 +00002387
2388 /* We require a minimum headroom to be able to transmit the frame.
2389 * Otherwise return an error and let the original net_device handle it
2390 */
Yangbo Lu1cf773b2020-09-18 17:08:01 +08002391 needed_headroom = dpaa2_eth_needed_headroom(NULL);
Ioana Radulescud678be12019-03-01 17:47:24 +00002392 if (xdpf->headroom < needed_headroom)
2393 return -EINVAL;
2394
Ioana Radulescud678be12019-03-01 17:47:24 +00002395 /* Setup the FD fields */
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002396 memset(fd, 0, sizeof(*fd));
Ioana Radulescud678be12019-03-01 17:47:24 +00002397
2398 /* Align FD address, if possible */
2399 buffer_start = xdpf->data - needed_headroom;
2400 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2401 DPAA2_ETH_TX_BUF_ALIGN);
2402 if (aligned_start >= xdpf->data - xdpf->headroom)
2403 buffer_start = aligned_start;
2404
2405 swa = (struct dpaa2_eth_swa *)buffer_start;
2406 /* fill in necessary fields here */
2407 swa->type = DPAA2_ETH_SWA_XDP;
2408 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2409 swa->xdp.xdpf = xdpf;
2410
2411 addr = dma_map_single(dev, buffer_start,
2412 swa->xdp.dma_size,
2413 DMA_BIDIRECTIONAL);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002414 if (unlikely(dma_mapping_error(dev, addr)))
Ioana Radulescud678be12019-03-01 17:47:24 +00002415 return -ENOMEM;
Ioana Radulescud678be12019-03-01 17:47:24 +00002416
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002417 dpaa2_fd_set_addr(fd, addr);
2418 dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2419 dpaa2_fd_set_len(fd, xdpf->len);
2420 dpaa2_fd_set_format(fd, dpaa2_fd_single);
2421 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescud678be12019-03-01 17:47:24 +00002422
2423 return 0;
2424}
2425
2426static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2427 struct xdp_frame **frames, u32 flags)
2428{
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002429 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002430 struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002431 struct rtnl_link_stats64 *percpu_stats;
2432 struct dpaa2_eth_fq *fq;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002433 struct dpaa2_fd *fds;
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002434 int enqueued, i, err;
Ioana Radulescud678be12019-03-01 17:47:24 +00002435
2436 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2437 return -EINVAL;
2438
2439 if (!netif_running(net_dev))
2440 return -ENETDOWN;
2441
Ioana Ciornei8665d972020-04-22 15:05:13 +03002442 fq = &priv->fq[smp_processor_id()];
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002443 xdp_redirect_fds = &fq->xdp_redirect_fds;
2444 fds = xdp_redirect_fds->fds;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002445
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002446 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002447
Ioana Ciornei8665d972020-04-22 15:05:13 +03002448 /* create a FD for each xdp_frame in the list received */
Ioana Radulescud678be12019-03-01 17:47:24 +00002449 for (i = 0; i < n; i++) {
Ioana Ciornei8665d972020-04-22 15:05:13 +03002450 err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2451 if (err)
2452 break;
2453 }
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002454 xdp_redirect_fds->num = i;
Ioana Radulescud678be12019-03-01 17:47:24 +00002455
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002456 /* enqueue all the frame descriptors */
2457 enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
Ioana Radulescud678be12019-03-01 17:47:24 +00002458
Ioana Ciornei8665d972020-04-22 15:05:13 +03002459 /* update statistics */
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002460 percpu_stats->tx_packets += enqueued;
2461 for (i = 0; i < enqueued; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002462 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
Ioana Ciornei8665d972020-04-22 15:05:13 +03002463
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002464 return enqueued;
Ioana Radulescud678be12019-03-01 17:47:24 +00002465}
2466
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002467static int update_xps(struct dpaa2_eth_priv *priv)
2468{
2469 struct net_device *net_dev = priv->net_dev;
2470 struct cpumask xps_mask;
2471 struct dpaa2_eth_fq *fq;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002472 int i, num_queues, netdev_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002473 int err = 0;
2474
2475 num_queues = dpaa2_eth_queue_count(priv);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002476 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002477
2478 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2479 * queues, so only process those
2480 */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002481 for (i = 0; i < netdev_queues; i++) {
2482 fq = &priv->fq[i % num_queues];
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002483
2484 cpumask_clear(&xps_mask);
2485 cpumask_set_cpu(fq->target_cpu, &xps_mask);
2486
2487 err = netif_set_xps_queue(net_dev, &xps_mask, i);
2488 if (err) {
2489 netdev_warn_once(net_dev, "Error setting XPS queue\n");
2490 break;
2491 }
2492 }
2493
2494 return err;
2495}
2496
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002497static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2498 struct tc_mqprio_qopt *mqprio)
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002499{
2500 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002501 u8 num_tc, num_queues;
2502 int i;
2503
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002504 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2505 num_queues = dpaa2_eth_queue_count(priv);
2506 num_tc = mqprio->num_tc;
2507
2508 if (num_tc == net_dev->num_tc)
2509 return 0;
2510
2511 if (num_tc > dpaa2_eth_tc_count(priv)) {
2512 netdev_err(net_dev, "Max %d traffic classes supported\n",
2513 dpaa2_eth_tc_count(priv));
Jesper Dangaard Brouerb89c1e62020-04-23 16:57:50 +02002514 return -EOPNOTSUPP;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002515 }
2516
2517 if (!num_tc) {
2518 netdev_reset_tc(net_dev);
2519 netif_set_real_num_tx_queues(net_dev, num_queues);
2520 goto out;
2521 }
2522
2523 netdev_set_num_tc(net_dev, num_tc);
2524 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2525
2526 for (i = 0; i < num_tc; i++)
2527 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2528
2529out:
2530 update_xps(priv);
2531
2532 return 0;
2533}
2534
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002535#define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2536
2537static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2538{
2539 struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2540 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2541 struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2542 struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2543 int err;
2544
2545 if (p->command == TC_TBF_STATS)
2546 return -EOPNOTSUPP;
2547
2548 /* Only per port Tx shaping */
2549 if (p->parent != TC_H_ROOT)
2550 return -EOPNOTSUPP;
2551
2552 if (p->command == TC_TBF_REPLACE) {
2553 if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2554 netdev_err(net_dev, "burst size cannot be greater than %d\n",
2555 DPAA2_ETH_MAX_BURST_SIZE);
2556 return -EINVAL;
2557 }
2558
2559 tx_cr_shaper.max_burst_size = cfg->max_size;
2560 /* The TBF interface is in bytes/s, whereas DPAA2 expects the
2561 * rate in Mbits/s
2562 */
2563 tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2564 }
2565
2566 err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2567 &tx_er_shaper, 0);
2568 if (err) {
2569 netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2570 return err;
2571 }
2572
2573 return 0;
2574}
2575
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002576static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2577 enum tc_setup_type type, void *type_data)
2578{
2579 switch (type) {
2580 case TC_SETUP_QDISC_MQPRIO:
2581 return dpaa2_eth_setup_mqprio(net_dev, type_data);
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002582 case TC_SETUP_QDISC_TBF:
2583 return dpaa2_eth_setup_tbf(net_dev, type_data);
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002584 default:
2585 return -EOPNOTSUPP;
2586 }
2587}
2588
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002589static const struct net_device_ops dpaa2_eth_ops = {
2590 .ndo_open = dpaa2_eth_open,
2591 .ndo_start_xmit = dpaa2_eth_tx,
2592 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002593 .ndo_set_mac_address = dpaa2_eth_set_addr,
2594 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002595 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2596 .ndo_set_features = dpaa2_eth_set_features,
Arnd Bergmanna7605372021-07-27 15:45:13 +02002597 .ndo_eth_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002598 .ndo_change_mtu = dpaa2_eth_change_mtu,
2599 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescud678be12019-03-01 17:47:24 +00002600 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002601 .ndo_setup_tc = dpaa2_eth_setup_tc,
Ionut-robert Aron70b32d82021-01-11 19:07:25 +02002602 .ndo_vlan_rx_add_vid = dpaa2_eth_rx_add_vid,
2603 .ndo_vlan_rx_kill_vid = dpaa2_eth_rx_kill_vid
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002604};
2605
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002606static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002607{
2608 struct dpaa2_eth_channel *ch;
2609
2610 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002611
2612 /* Update NAPI statistics */
2613 ch->stats.cdan++;
2614
Jiafei Pan6c33ae12020-08-03 23:10:08 +03002615 napi_schedule(&ch->napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002616}
2617
2618/* Allocate and configure a DPCON object */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002619static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002620{
2621 struct fsl_mc_device *dpcon;
2622 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002623 int err;
2624
2625 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2626 FSL_MC_POOL_DPCON, &dpcon);
2627 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002628 if (err == -ENXIO)
2629 err = -EPROBE_DEFER;
2630 else
2631 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2632 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002633 }
2634
2635 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2636 if (err) {
2637 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002638 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002639 }
2640
2641 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2642 if (err) {
2643 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002644 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002645 }
2646
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002647 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2648 if (err) {
2649 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002650 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002651 }
2652
2653 return dpcon;
2654
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002655close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002656 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002657free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002658 fsl_mc_object_free(dpcon);
2659
YueHaibing02afa9c2020-08-04 21:26:43 +08002660 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002661}
2662
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002663static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
2664 struct fsl_mc_device *dpcon)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002665{
2666 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2667 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2668 fsl_mc_object_free(dpcon);
2669}
2670
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002671static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002672{
2673 struct dpaa2_eth_channel *channel;
2674 struct dpcon_attr attr;
2675 struct device *dev = priv->net_dev->dev.parent;
2676 int err;
2677
2678 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2679 if (!channel)
2680 return NULL;
2681
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002682 channel->dpcon = dpaa2_eth_setup_dpcon(priv);
YueHaibing02afa9c2020-08-04 21:26:43 +08002683 if (IS_ERR(channel->dpcon)) {
2684 err = PTR_ERR(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002685 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002686 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002687
2688 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2689 &attr);
2690 if (err) {
2691 dev_err(dev, "dpcon_get_attributes() failed\n");
2692 goto err_get_attr;
2693 }
2694
2695 channel->dpcon_id = attr.id;
2696 channel->ch_id = attr.qbman_ch_id;
2697 channel->priv = priv;
2698
2699 return channel;
2700
2701err_get_attr:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002702 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002703err_setup:
2704 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002705 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002706}
2707
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002708static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
2709 struct dpaa2_eth_channel *channel)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002710{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002711 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002712 kfree(channel);
2713}
2714
2715/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2716 * and register data availability notifications
2717 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002718static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002719{
2720 struct dpaa2_io_notification_ctx *nctx;
2721 struct dpaa2_eth_channel *channel;
2722 struct dpcon_notification_cfg dpcon_notif_cfg;
2723 struct device *dev = priv->net_dev->dev.parent;
2724 int i, err;
2725
2726 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2727 * many cores as possible, so we need one channel for each core
2728 * (unless there's fewer queues than cores, in which case the extra
2729 * channels would be wasted).
2730 * Allocate one channel per core and register it to the core's
2731 * affine DPIO. If not enough channels are available for all cores
2732 * or if some cores don't have an affine DPIO, there will be no
2733 * ingress frame processing on those cores.
2734 */
2735 cpumask_clear(&priv->dpio_cpumask);
2736 for_each_online_cpu(i) {
2737 /* Try to allocate a channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002738 channel = dpaa2_eth_alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002739 if (IS_ERR_OR_NULL(channel)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002740 err = PTR_ERR_OR_ZERO(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002741 if (err != -EPROBE_DEFER)
2742 dev_info(dev,
2743 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002744 goto err_alloc_ch;
2745 }
2746
2747 priv->channel[priv->num_channels] = channel;
2748
2749 nctx = &channel->nctx;
2750 nctx->is_cdan = 1;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002751 nctx->cb = dpaa2_eth_cdan_cb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002752 nctx->id = channel->ch_id;
2753 nctx->desired_cpu = i;
2754
2755 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06002756 channel->dpio = dpaa2_io_service_select(i);
Ioana Ciornei47441f72018-12-10 16:50:19 +00002757 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002758 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002759 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002760 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002761 * none available for next cores either. Signal we want
2762 * to retry later, in case the DPIO devices weren't
2763 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002764 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002765 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002766 goto err_service_reg;
2767 }
2768
2769 /* Register DPCON notification with MC */
2770 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2771 dpcon_notif_cfg.priority = 0;
2772 dpcon_notif_cfg.user_ctx = nctx->qman64;
2773 err = dpcon_set_notification(priv->mc_io, 0,
2774 channel->dpcon->mc_handle,
2775 &dpcon_notif_cfg);
2776 if (err) {
2777 dev_err(dev, "dpcon_set_notification failed()\n");
2778 goto err_set_cdan;
2779 }
2780
2781 /* If we managed to allocate a channel and also found an affine
2782 * DPIO for this core, add it to the final mask
2783 */
2784 cpumask_set_cpu(i, &priv->dpio_cpumask);
2785 priv->num_channels++;
2786
2787 /* Stop if we already have enough channels to accommodate all
2788 * RX and TX conf queues
2789 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00002790 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002791 break;
2792 }
2793
2794 return 0;
2795
2796err_set_cdan:
Ioana Ciornei47441f72018-12-10 16:50:19 +00002797 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002798err_service_reg:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002799 dpaa2_eth_free_channel(priv, channel);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002800err_alloc_ch:
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002801 if (err == -EPROBE_DEFER) {
2802 for (i = 0; i < priv->num_channels; i++) {
2803 channel = priv->channel[i];
2804 nctx = &channel->nctx;
2805 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002806 dpaa2_eth_free_channel(priv, channel);
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002807 }
2808 priv->num_channels = 0;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002809 return err;
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002810 }
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002811
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002812 if (cpumask_empty(&priv->dpio_cpumask)) {
2813 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002814 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002815 }
2816
2817 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2818 cpumask_pr_args(&priv->dpio_cpumask));
2819
2820 return 0;
2821}
2822
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002823static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002824{
Ioana Ciornei47441f72018-12-10 16:50:19 +00002825 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002826 struct dpaa2_eth_channel *ch;
Ioana Ciornei47441f72018-12-10 16:50:19 +00002827 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002828
2829 /* deregister CDAN notifications and free channels */
2830 for (i = 0; i < priv->num_channels; i++) {
2831 ch = priv->channel[i];
Ioana Ciornei47441f72018-12-10 16:50:19 +00002832 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002833 dpaa2_eth_free_channel(priv, ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002834 }
2835}
2836
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002837static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
2838 int cpu)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002839{
2840 struct device *dev = priv->net_dev->dev.parent;
2841 int i;
2842
2843 for (i = 0; i < priv->num_channels; i++)
2844 if (priv->channel[i]->nctx.desired_cpu == cpu)
2845 return priv->channel[i];
2846
2847 /* We should never get here. Issue a warning and return
2848 * the first channel, because it's still better than nothing
2849 */
2850 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2851
2852 return priv->channel[0];
2853}
2854
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002855static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002856{
2857 struct device *dev = priv->net_dev->dev.parent;
2858 struct dpaa2_eth_fq *fq;
2859 int rx_cpu, txc_cpu;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002860 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002861
2862 /* For each FQ, pick one channel/CPU to deliver frames to.
2863 * This may well change at runtime, either through irqbalance or
2864 * through direct user intervention.
2865 */
2866 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2867
2868 for (i = 0; i < priv->num_fqs; i++) {
2869 fq = &priv->fq[i];
2870 switch (fq->type) {
2871 case DPAA2_RX_FQ:
Ioana Ciornei061d6312020-10-01 18:11:48 +03002872 case DPAA2_RX_ERR_FQ:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002873 fq->target_cpu = rx_cpu;
2874 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2875 if (rx_cpu >= nr_cpu_ids)
2876 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2877 break;
2878 case DPAA2_TX_CONF_FQ:
2879 fq->target_cpu = txc_cpu;
2880 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2881 if (txc_cpu >= nr_cpu_ids)
2882 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2883 break;
2884 default:
2885 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2886 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002887 fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002888 }
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002889
2890 update_xps(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002891}
2892
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002893static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002894{
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002895 int i, j;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002896
2897 /* We have one TxConf FQ per Tx flow.
2898 * The number of Tx and Rx queues is the same.
2899 * Tx queues come first in the fq array.
2900 */
2901 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2902 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2903 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2904 priv->fq[priv->num_fqs++].flowid = (u16)i;
2905 }
2906
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002907 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2908 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2909 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2910 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2911 priv->fq[priv->num_fqs].tc = (u8)j;
2912 priv->fq[priv->num_fqs++].flowid = (u16)i;
2913 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002914 }
2915
Ioana Ciornei061d6312020-10-01 18:11:48 +03002916 /* We have exactly one Rx error queue per DPNI */
2917 priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
2918 priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
2919
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002920 /* For each FQ, decide on which core to process incoming frames */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002921 dpaa2_eth_set_fq_affinity(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002922}
2923
2924/* Allocate and configure one buffer pool for each interface */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002925static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002926{
2927 int err;
2928 struct fsl_mc_device *dpbp_dev;
2929 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002930 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002931
2932 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2933 &dpbp_dev);
2934 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002935 if (err == -ENXIO)
2936 err = -EPROBE_DEFER;
2937 else
2938 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002939 return err;
2940 }
2941
2942 priv->dpbp_dev = dpbp_dev;
2943
2944 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2945 &dpbp_dev->mc_handle);
2946 if (err) {
2947 dev_err(dev, "dpbp_open() failed\n");
2948 goto err_open;
2949 }
2950
Ioana Radulescud00defe2017-06-06 10:00:32 -05002951 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2952 if (err) {
2953 dev_err(dev, "dpbp_reset() failed\n");
2954 goto err_reset;
2955 }
2956
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002957 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2958 if (err) {
2959 dev_err(dev, "dpbp_enable() failed\n");
2960 goto err_enable;
2961 }
2962
2963 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002964 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002965 if (err) {
2966 dev_err(dev, "dpbp_get_attributes() failed\n");
2967 goto err_get_attr;
2968 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002969 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002970
2971 return 0;
2972
2973err_get_attr:
2974 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2975err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002976err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002977 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2978err_open:
2979 fsl_mc_object_free(dpbp_dev);
2980
2981 return err;
2982}
2983
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002984static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002985{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002986 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002987 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2988 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2989 fsl_mc_object_free(priv->dpbp_dev);
2990}
2991
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002992static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002993{
2994 struct device *dev = priv->net_dev->dev.parent;
2995 struct dpni_buffer_layout buf_layout = {0};
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002996 u16 rx_buf_align;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002997 int err;
2998
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002999 /* We need to check for WRIOP version 1.0.0, but depending on the MC
3000 * version, this number is not always provided correctly on rev1.
3001 * We need to check for both alternatives in this situation.
3002 */
3003 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
3004 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00003005 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00003006 else
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00003007 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00003008
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03003009 /* We need to ensure that the buffer size seen by WRIOP is a multiple
3010 * of 64 or 256 bytes depending on the WRIOP version.
3011 */
3012 priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
3013
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00003014 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003015 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08003016 buf_layout.pass_timestamp = true;
Yangbo Luc5521182020-09-18 17:08:02 +08003017 buf_layout.pass_frame_status = true;
Ioana Radulescu859f9982018-04-26 18:23:47 +08003018 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
Yangbo Luc5521182020-09-18 17:08:02 +08003019 DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3020 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003021 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3022 DPNI_QUEUE_TX, &buf_layout);
3023 if (err) {
3024 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
3025 return err;
3026 }
3027
3028 /* tx-confirm buffer */
Yangbo Luc5521182020-09-18 17:08:02 +08003029 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3030 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003031 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3032 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
3033 if (err) {
3034 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
3035 return err;
3036 }
3037
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00003038 /* Now that we've set our tx buffer layout, retrieve the minimum
3039 * required tx data offset.
3040 */
3041 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
3042 &priv->tx_data_offset);
3043 if (err) {
3044 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
3045 return err;
3046 }
3047
3048 if ((priv->tx_data_offset % 64) != 0)
3049 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
3050 priv->tx_data_offset);
3051
3052 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06003053 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00003054 buf_layout.pass_parser_result = true;
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00003055 buf_layout.data_align = rx_buf_align;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00003056 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
3057 buf_layout.private_data_size = 0;
3058 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
3059 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
3060 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08003061 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
3062 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00003063 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3064 DPNI_QUEUE_RX, &buf_layout);
3065 if (err) {
3066 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
3067 return err;
3068 }
3069
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003070 return 0;
3071}
3072
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003073#define DPNI_ENQUEUE_FQID_VER_MAJOR 7
3074#define DPNI_ENQUEUE_FQID_VER_MINOR 9
3075
3076static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
3077 struct dpaa2_eth_fq *fq,
Ioana Ciornei48c04812020-04-22 15:05:10 +03003078 struct dpaa2_fd *fd, u8 prio,
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003079 u32 num_frames __always_unused,
Ioana Ciornei48c04812020-04-22 15:05:10 +03003080 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003081{
Ioana Ciornei48c04812020-04-22 15:05:10 +03003082 int err;
3083
3084 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
3085 priv->tx_qdid, prio,
3086 fq->tx_qdbin, fd);
3087 if (!err && frames_enqueued)
3088 *frames_enqueued = 1;
3089 return err;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003090}
3091
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003092static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
3093 struct dpaa2_eth_fq *fq,
3094 struct dpaa2_fd *fd,
3095 u8 prio, u32 num_frames,
3096 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003097{
Ioana Ciornei48c04812020-04-22 15:05:10 +03003098 int err;
3099
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003100 err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
3101 fq->tx_fqid[prio],
3102 fd, num_frames);
3103
3104 if (err == 0)
3105 return -EBUSY;
3106
3107 if (frames_enqueued)
3108 *frames_enqueued = err;
3109 return 0;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003110}
3111
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003112static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003113{
3114 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3115 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3116 priv->enqueue = dpaa2_eth_enqueue_qd;
3117 else
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003118 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003119}
3120
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003121static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003122{
3123 struct device *dev = priv->net_dev->dev.parent;
3124 struct dpni_link_cfg link_cfg = {0};
3125 int err;
3126
3127 /* Get the default link options so we don't override other flags */
3128 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3129 if (err) {
3130 dev_err(dev, "dpni_get_link_cfg() failed\n");
3131 return err;
3132 }
3133
3134 /* By default, enable both Rx and Tx pause frames */
3135 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
3136 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
3137 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3138 if (err) {
3139 dev_err(dev, "dpni_set_link_cfg() failed\n");
3140 return err;
3141 }
3142
3143 priv->link_state.options = link_cfg.options;
3144
3145 return 0;
3146}
3147
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003148static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
Ioana Radulescua690af4f2019-10-16 10:36:23 +03003149{
3150 struct dpni_queue_id qid = {0};
3151 struct dpaa2_eth_fq *fq;
3152 struct dpni_queue queue;
3153 int i, j, err;
3154
3155 /* We only use Tx FQIDs for FQID-based enqueue, so check
3156 * if DPNI version supports it before updating FQIDs
3157 */
3158 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3159 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3160 return;
3161
3162 for (i = 0; i < priv->num_fqs; i++) {
3163 fq = &priv->fq[i];
3164 if (fq->type != DPAA2_TX_CONF_FQ)
3165 continue;
3166 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3167 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3168 DPNI_QUEUE_TX, j, fq->flowid,
3169 &queue, &qid);
3170 if (err)
3171 goto out_err;
3172
3173 fq->tx_fqid[j] = qid.fqid;
3174 if (fq->tx_fqid[j] == 0)
3175 goto out_err;
3176 }
3177 }
3178
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003179 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Radulescua690af4f2019-10-16 10:36:23 +03003180
3181 return;
3182
3183out_err:
3184 netdev_info(priv->net_dev,
3185 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3186 priv->enqueue = dpaa2_eth_enqueue_qd;
3187}
3188
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003189/* Configure ingress classification based on VLAN PCP */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003190static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003191{
3192 struct device *dev = priv->net_dev->dev.parent;
3193 struct dpkg_profile_cfg kg_cfg = {0};
3194 struct dpni_qos_tbl_cfg qos_cfg = {0};
3195 struct dpni_rule_cfg key_params;
3196 void *dma_mem, *key, *mask;
3197 u8 key_size = 2; /* VLAN TCI field */
3198 int i, pcp, err;
3199
3200 /* VLAN-based classification only makes sense if we have multiple
3201 * traffic classes.
3202 * Also, we need to extract just the 3-bit PCP field from the VLAN
3203 * header and we can only do that by using a mask
3204 */
3205 if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3206 dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3207 return -EOPNOTSUPP;
3208 }
3209
3210 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3211 if (!dma_mem)
3212 return -ENOMEM;
3213
3214 kg_cfg.num_extracts = 1;
3215 kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3216 kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3217 kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3218 kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3219
3220 err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3221 if (err) {
3222 dev_err(dev, "dpni_prepare_key_cfg failed\n");
3223 goto out_free_tbl;
3224 }
3225
3226 /* set QoS table */
3227 qos_cfg.default_tc = 0;
3228 qos_cfg.discard_on_miss = 0;
3229 qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3230 DPAA2_CLASSIFIER_DMA_SIZE,
3231 DMA_TO_DEVICE);
3232 if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3233 dev_err(dev, "QoS table DMA mapping failed\n");
3234 err = -ENOMEM;
3235 goto out_free_tbl;
3236 }
3237
3238 err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3239 if (err) {
3240 dev_err(dev, "dpni_set_qos_table failed\n");
3241 goto out_unmap_tbl;
3242 }
3243
3244 /* Add QoS table entries */
3245 key = kzalloc(key_size * 2, GFP_KERNEL);
3246 if (!key) {
3247 err = -ENOMEM;
3248 goto out_unmap_tbl;
3249 }
3250 mask = key + key_size;
3251 *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3252
3253 key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3254 DMA_TO_DEVICE);
3255 if (dma_mapping_error(dev, key_params.key_iova)) {
3256 dev_err(dev, "Qos table entry DMA mapping failed\n");
3257 err = -ENOMEM;
3258 goto out_free_key;
3259 }
3260
3261 key_params.mask_iova = key_params.key_iova + key_size;
3262 key_params.key_size = key_size;
3263
3264 /* We add rules for PCP-based distribution starting with highest
3265 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3266 * classes to accommodate all priority levels, the lowest ones end up
3267 * on TC 0 which was configured as default
3268 */
3269 for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3270 *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3271 dma_sync_single_for_device(dev, key_params.key_iova,
3272 key_size * 2, DMA_TO_DEVICE);
3273
3274 err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3275 &key_params, i, i);
3276 if (err) {
3277 dev_err(dev, "dpni_add_qos_entry failed\n");
3278 dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3279 goto out_unmap_key;
3280 }
3281 }
3282
3283 priv->vlan_cls_enabled = true;
3284
3285 /* Table and key memory is not persistent, clean everything up after
3286 * configuration is finished
3287 */
3288out_unmap_key:
3289 dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3290out_free_key:
3291 kfree(key);
3292out_unmap_tbl:
3293 dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3294 DMA_TO_DEVICE);
3295out_free_tbl:
3296 kfree(dma_mem);
3297
3298 return err;
3299}
3300
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003301/* Configure the DPNI object this interface is associated with */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003302static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003303{
3304 struct device *dev = &ls_dev->dev;
3305 struct dpaa2_eth_priv *priv;
3306 struct net_device *net_dev;
3307 int err;
3308
3309 net_dev = dev_get_drvdata(dev);
3310 priv = netdev_priv(net_dev);
3311
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003312 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05003313 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003314 if (err) {
3315 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003316 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003317 }
3318
Ioana Radulescu311cffa2018-03-23 08:44:09 -05003319 /* Check if we can work with this DPNI object */
3320 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3321 &priv->dpni_ver_minor);
3322 if (err) {
3323 dev_err(dev, "dpni_get_api_version() failed\n");
3324 goto close;
3325 }
3326 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3327 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3328 priv->dpni_ver_major, priv->dpni_ver_minor,
3329 DPNI_VER_MAJOR, DPNI_VER_MINOR);
3330 err = -ENOTSUPP;
3331 goto close;
3332 }
3333
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003334 ls_dev->mc_io = priv->mc_io;
3335 ls_dev->mc_handle = priv->mc_token;
3336
3337 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3338 if (err) {
3339 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003340 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003341 }
3342
3343 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3344 &priv->dpni_attrs);
3345 if (err) {
3346 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003347 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003348 }
3349
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003350 err = dpaa2_eth_set_buffer_layout(priv);
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003351 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003352 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003353
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003354 dpaa2_eth_set_enqueue_mode(priv);
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003355
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003356 /* Enable pause frame support */
3357 if (dpaa2_eth_has_pause_support(priv)) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003358 err = dpaa2_eth_set_pause(priv);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003359 if (err)
3360 goto close;
3361 }
3362
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003363 err = dpaa2_eth_set_vlan_qos(priv);
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003364 if (err && err != -EOPNOTSUPP)
3365 goto close;
3366
Xu Wang9334d5b2020-06-11 02:45:20 +00003367 priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3368 sizeof(struct dpaa2_eth_cls_rule),
3369 GFP_KERNEL);
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003370 if (!priv->cls_rules) {
3371 err = -ENOMEM;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003372 goto close;
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003373 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003374
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003375 return 0;
3376
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003377close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003378 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003379
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003380 return err;
3381}
3382
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003383static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003384{
3385 int err;
3386
3387 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3388 if (err)
3389 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3390 err);
3391
3392 dpni_close(priv->mc_io, 0, priv->mc_token);
3393}
3394
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003395static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3396 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003397{
3398 struct device *dev = priv->net_dev->dev.parent;
3399 struct dpni_queue queue;
3400 struct dpni_queue_id qid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003401 int err;
3402
3403 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003404 DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003405 if (err) {
3406 dev_err(dev, "dpni_get_queue(RX) failed\n");
3407 return err;
3408 }
3409
3410 fq->fqid = qid.fqid;
3411
3412 queue.destination.id = fq->channel->dpcon_id;
3413 queue.destination.type = DPNI_DEST_DPCON;
3414 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003415 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003416 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003417 DPNI_QUEUE_RX, fq->tc, fq->flowid,
Ioana Radulescu16fa1cf2019-05-23 17:38:22 +03003418 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003419 &queue);
3420 if (err) {
3421 dev_err(dev, "dpni_set_queue(RX) failed\n");
3422 return err;
3423 }
3424
Ioana Radulescud678be12019-03-01 17:47:24 +00003425 /* xdp_rxq setup */
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003426 /* only once for each channel */
3427 if (fq->tc > 0)
3428 return 0;
3429
Ioana Radulescud678be12019-03-01 17:47:24 +00003430 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
Björn Töpelb02e5a02020-11-30 19:52:01 +01003431 fq->flowid, 0);
Ioana Radulescud678be12019-03-01 17:47:24 +00003432 if (err) {
3433 dev_err(dev, "xdp_rxq_info_reg failed\n");
3434 return err;
3435 }
3436
3437 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3438 MEM_TYPE_PAGE_ORDER0, NULL);
3439 if (err) {
3440 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3441 return err;
3442 }
3443
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003444 return 0;
3445}
3446
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003447static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3448 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003449{
3450 struct device *dev = priv->net_dev->dev.parent;
3451 struct dpni_queue queue;
3452 struct dpni_queue_id qid;
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003453 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003454
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003455 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3456 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3457 DPNI_QUEUE_TX, i, fq->flowid,
3458 &queue, &qid);
3459 if (err) {
3460 dev_err(dev, "dpni_get_queue(TX) failed\n");
3461 return err;
3462 }
3463 fq->tx_fqid[i] = qid.fqid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003464 }
3465
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003466 /* All Tx queues belonging to the same flowid have the same qdbin */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003467 fq->tx_qdbin = qid.qdbin;
3468
3469 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3470 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3471 &queue, &qid);
3472 if (err) {
3473 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3474 return err;
3475 }
3476
3477 fq->fqid = qid.fqid;
3478
3479 queue.destination.id = fq->channel->dpcon_id;
3480 queue.destination.type = DPNI_DEST_DPCON;
3481 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003482 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003483 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3484 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3485 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3486 &queue);
3487 if (err) {
3488 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3489 return err;
3490 }
3491
3492 return 0;
3493}
3494
Ioana Ciornei061d6312020-10-01 18:11:48 +03003495static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
3496 struct dpaa2_eth_fq *fq)
3497{
3498 struct device *dev = priv->net_dev->dev.parent;
3499 struct dpni_queue q = { { 0 } };
3500 struct dpni_queue_id qid;
3501 u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
3502 int err;
3503
3504 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3505 DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
3506 if (err) {
3507 dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
3508 return err;
3509 }
3510
3511 fq->fqid = qid.fqid;
3512
3513 q.destination.id = fq->channel->dpcon_id;
3514 q.destination.type = DPNI_DEST_DPCON;
3515 q.destination.priority = 1;
3516 q.user_context = (u64)(uintptr_t)fq;
3517 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3518 DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
3519 if (err) {
3520 dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
3521 return err;
3522 }
3523
3524 return 0;
3525}
3526
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003527/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003528static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003529 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003530 /* L2 header */
3531 .rxnfc_field = RXH_L2DA,
3532 .cls_prot = NET_PROT_ETH,
3533 .cls_field = NH_FLD_ETH_DA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003534 .id = DPAA2_ETH_DIST_ETHDST,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003535 .size = 6,
3536 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003537 .cls_prot = NET_PROT_ETH,
3538 .cls_field = NH_FLD_ETH_SA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003539 .id = DPAA2_ETH_DIST_ETHSRC,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003540 .size = 6,
3541 }, {
3542 /* This is the last ethertype field parsed:
3543 * depending on frame format, it can be the MAC ethertype
3544 * or the VLAN etype.
3545 */
3546 .cls_prot = NET_PROT_ETH,
3547 .cls_field = NH_FLD_ETH_TYPE,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003548 .id = DPAA2_ETH_DIST_ETHTYPE,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003549 .size = 2,
3550 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003551 /* VLAN header */
3552 .rxnfc_field = RXH_VLAN,
3553 .cls_prot = NET_PROT_VLAN,
3554 .cls_field = NH_FLD_VLAN_TCI,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003555 .id = DPAA2_ETH_DIST_VLAN,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003556 .size = 2,
3557 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003558 /* IP header */
3559 .rxnfc_field = RXH_IP_SRC,
3560 .cls_prot = NET_PROT_IP,
3561 .cls_field = NH_FLD_IP_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003562 .id = DPAA2_ETH_DIST_IPSRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003563 .size = 4,
3564 }, {
3565 .rxnfc_field = RXH_IP_DST,
3566 .cls_prot = NET_PROT_IP,
3567 .cls_field = NH_FLD_IP_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003568 .id = DPAA2_ETH_DIST_IPDST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003569 .size = 4,
3570 }, {
3571 .rxnfc_field = RXH_L3_PROTO,
3572 .cls_prot = NET_PROT_IP,
3573 .cls_field = NH_FLD_IP_PROTO,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003574 .id = DPAA2_ETH_DIST_IPPROTO,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003575 .size = 1,
3576 }, {
3577 /* Using UDP ports, this is functionally equivalent to raw
3578 * byte pairs from L4 header.
3579 */
3580 .rxnfc_field = RXH_L4_B_0_1,
3581 .cls_prot = NET_PROT_UDP,
3582 .cls_field = NH_FLD_UDP_PORT_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003583 .id = DPAA2_ETH_DIST_L4SRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003584 .size = 2,
3585 }, {
3586 .rxnfc_field = RXH_L4_B_2_3,
3587 .cls_prot = NET_PROT_UDP,
3588 .cls_field = NH_FLD_UDP_PORT_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003589 .id = DPAA2_ETH_DIST_L4DST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003590 .size = 2,
3591 },
3592};
3593
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003594/* Configure the Rx hash key using the legacy API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003595static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003596{
3597 struct device *dev = priv->net_dev->dev.parent;
3598 struct dpni_rx_tc_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003599 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003600
3601 memset(&dist_cfg, 0, sizeof(dist_cfg));
3602
3603 dist_cfg.key_cfg_iova = key;
3604 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3605 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3606
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003607 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3608 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3609 i, &dist_cfg);
3610 if (err) {
3611 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3612 break;
3613 }
3614 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003615
3616 return err;
3617}
3618
3619/* Configure the Rx hash key using the new API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003620static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003621{
3622 struct device *dev = priv->net_dev->dev.parent;
3623 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003624 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003625
3626 memset(&dist_cfg, 0, sizeof(dist_cfg));
3627
3628 dist_cfg.key_cfg_iova = key;
3629 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3630 dist_cfg.enable = 1;
3631
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003632 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3633 dist_cfg.tc = i;
3634 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3635 &dist_cfg);
3636 if (err) {
3637 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3638 break;
3639 }
Ionut-robert Aron5e29c162020-09-25 17:44:21 +03003640
3641 /* If the flow steering / hashing key is shared between all
3642 * traffic classes, install it just once
3643 */
3644 if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3645 break;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003646 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003647
3648 return err;
3649}
3650
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003651/* Configure the Rx flow classification key */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003652static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003653{
3654 struct device *dev = priv->net_dev->dev.parent;
3655 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003656 int i, err = 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003657
3658 memset(&dist_cfg, 0, sizeof(dist_cfg));
3659
3660 dist_cfg.key_cfg_iova = key;
3661 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3662 dist_cfg.enable = 1;
3663
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003664 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3665 dist_cfg.tc = i;
3666 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3667 &dist_cfg);
3668 if (err) {
3669 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3670 break;
3671 }
Ionut-robert Aron5e29c162020-09-25 17:44:21 +03003672
3673 /* If the flow steering / hashing key is shared between all
3674 * traffic classes, install it just once
3675 */
3676 if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3677 break;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003678 }
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003679
3680 return err;
3681}
3682
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003683/* Size of the Rx flow classification key */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003684int dpaa2_eth_cls_key_size(u64 fields)
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003685{
3686 int i, size = 0;
3687
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003688 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3689 if (!(fields & dist_fields[i].id))
3690 continue;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003691 size += dist_fields[i].size;
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003692 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003693
3694 return size;
3695}
3696
3697/* Offset of header field in Rx classification key */
3698int dpaa2_eth_cls_fld_off(int prot, int field)
3699{
3700 int i, off = 0;
3701
3702 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3703 if (dist_fields[i].cls_prot == prot &&
3704 dist_fields[i].cls_field == field)
3705 return off;
3706 off += dist_fields[i].size;
3707 }
3708
3709 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3710 return 0;
3711}
3712
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003713/* Prune unused fields from the classification rule.
3714 * Used when masking is not supported
3715 */
3716void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3717{
3718 int off = 0, new_off = 0;
3719 int i, size;
3720
3721 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3722 size = dist_fields[i].size;
3723 if (dist_fields[i].id & fields) {
3724 memcpy(key_mem + new_off, key_mem + off, size);
3725 new_off += size;
3726 }
3727 off += size;
3728 }
3729}
3730
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003731/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003732 * flags is a combination of RXH_ bits
3733 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00003734static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
3735 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003736{
3737 struct device *dev = net_dev->dev.parent;
3738 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3739 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003740 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003741 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003742 u8 *dma_mem;
3743 int i;
3744 int err = 0;
3745
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003746 memset(&cls_cfg, 0, sizeof(cls_cfg));
3747
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003748 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003749 struct dpkg_extract *key =
3750 &cls_cfg.extracts[cls_cfg.num_extracts];
3751
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003752 /* For both Rx hashing and classification keys
3753 * we set only the selected fields.
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003754 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003755 if (!(flags & dist_fields[i].id))
3756 continue;
3757 if (type == DPAA2_ETH_RX_DIST_HASH)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003758 rx_hash_fields |= dist_fields[i].rxnfc_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003759
3760 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
3761 dev_err(dev, "error adding key extraction rule, too many rules?\n");
3762 return -E2BIG;
3763 }
3764
3765 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003766 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003767 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003768 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003769 cls_cfg.num_extracts++;
3770 }
3771
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05003772 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003773 if (!dma_mem)
3774 return -ENOMEM;
3775
3776 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3777 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003778 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003779 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003780 }
3781
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003782 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003783 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3784 DMA_TO_DEVICE);
3785 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003786 dev_err(dev, "DMA mapping failed\n");
3787 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003788 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003789 }
3790
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003791 if (type == DPAA2_ETH_RX_DIST_HASH) {
3792 if (dpaa2_eth_has_legacy_dist(priv))
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003793 err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003794 else
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003795 err = dpaa2_eth_config_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003796 } else {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003797 err = dpaa2_eth_config_cls_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003798 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003799
3800 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3801 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003802 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003803 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003804
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003805free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003806 kfree(dma_mem);
3807 return err;
3808}
3809
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003810int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3811{
3812 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003813 u64 key = 0;
3814 int i;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003815
3816 if (!dpaa2_eth_hash_enabled(priv))
3817 return -EOPNOTSUPP;
3818
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003819 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3820 if (dist_fields[i].rxnfc_field & flags)
3821 key |= dist_fields[i].id;
3822
3823 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003824}
3825
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003826int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3827{
3828 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3829}
3830
3831static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003832{
3833 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003834 int err;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003835
3836 /* Check if we actually support Rx flow classification */
3837 if (dpaa2_eth_has_legacy_dist(priv)) {
3838 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3839 return -EOPNOTSUPP;
3840 }
3841
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003842 if (!dpaa2_eth_fs_enabled(priv)) {
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003843 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3844 return -EOPNOTSUPP;
3845 }
3846
3847 if (!dpaa2_eth_hash_enabled(priv)) {
3848 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3849 return -EOPNOTSUPP;
3850 }
3851
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003852 /* If there is no support for masking in the classification table,
3853 * we don't set a default key, as it will depend on the rules
3854 * added by the user at runtime.
3855 */
3856 if (!dpaa2_eth_fs_mask_enabled(priv))
3857 goto out;
3858
3859 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003860 if (err)
3861 return err;
3862
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003863out:
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003864 priv->rx_cls_enabled = 1;
3865
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003866 return 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003867}
3868
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003869/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3870 * frame queues and channels
3871 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003872static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003873{
3874 struct net_device *net_dev = priv->net_dev;
3875 struct device *dev = net_dev->dev.parent;
3876 struct dpni_pools_cfg pools_params;
3877 struct dpni_error_cfg err_cfg;
3878 int err = 0;
3879 int i;
3880
3881 pools_params.num_dpbp = 1;
3882 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3883 pools_params.pools[0].backup_pool = 0;
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03003884 pools_params.pools[0].buffer_size = priv->rx_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003885 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3886 if (err) {
3887 dev_err(dev, "dpni_set_pools() failed\n");
3888 return err;
3889 }
3890
Ioana Radulescu227686b2018-07-27 09:12:59 -05003891 /* have the interface implicitly distribute traffic based on
3892 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003893 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05003894 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003895 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05003896 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003897
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003898 /* Configure the flow classification key; it includes all
3899 * supported header fields and cannot be modified at runtime
3900 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003901 err = dpaa2_eth_set_default_cls(priv);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003902 if (err && err != -EOPNOTSUPP)
3903 dev_err(dev, "Failed to configure Rx classification key\n");
3904
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003905 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05003906 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003907 err_cfg.set_frame_annotation = 1;
3908 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3909 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3910 &err_cfg);
3911 if (err) {
3912 dev_err(dev, "dpni_set_errors_behavior failed\n");
3913 return err;
3914 }
3915
3916 /* Configure Rx and Tx conf queues to generate CDANs */
3917 for (i = 0; i < priv->num_fqs; i++) {
3918 switch (priv->fq[i].type) {
3919 case DPAA2_RX_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003920 err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003921 break;
3922 case DPAA2_TX_CONF_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003923 err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003924 break;
Ioana Ciornei061d6312020-10-01 18:11:48 +03003925 case DPAA2_RX_ERR_FQ:
3926 err = setup_rx_err_flow(priv, &priv->fq[i]);
3927 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003928 default:
3929 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3930 return -EINVAL;
3931 }
3932 if (err)
3933 return err;
3934 }
3935
3936 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3937 DPNI_QUEUE_TX, &priv->tx_qdid);
3938 if (err) {
3939 dev_err(dev, "dpni_get_qdid() failed\n");
3940 return err;
3941 }
3942
3943 return 0;
3944}
3945
3946/* Allocate rings for storing incoming frame descriptors */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003947static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003948{
3949 struct net_device *net_dev = priv->net_dev;
3950 struct device *dev = net_dev->dev.parent;
3951 int i;
3952
3953 for (i = 0; i < priv->num_channels; i++) {
3954 priv->channel[i]->store =
3955 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3956 if (!priv->channel[i]->store) {
3957 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3958 goto err_ring;
3959 }
3960 }
3961
3962 return 0;
3963
3964err_ring:
3965 for (i = 0; i < priv->num_channels; i++) {
3966 if (!priv->channel[i]->store)
3967 break;
3968 dpaa2_io_store_destroy(priv->channel[i]->store);
3969 }
3970
3971 return -ENOMEM;
3972}
3973
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003974static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003975{
3976 int i;
3977
3978 for (i = 0; i < priv->num_channels; i++)
3979 dpaa2_io_store_destroy(priv->channel[i]->store);
3980}
3981
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003982static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003983{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003984 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003985 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003986 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003987 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003988
3989 /* Get firmware address, if any */
3990 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3991 if (err) {
3992 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3993 return err;
3994 }
3995
3996 /* Get DPNI attributes address, if any */
3997 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3998 dpni_mac_addr);
3999 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004000 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004001 return err;
4002 }
4003
4004 /* First check if firmware has any address configured by bootloader */
4005 if (!is_zero_ether_addr(mac_addr)) {
4006 /* If the DPMAC addr != DPNI addr, update it */
4007 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
4008 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
4009 priv->mc_token,
4010 mac_addr);
4011 if (err) {
4012 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4013 return err;
4014 }
4015 }
Jakub Kicinskia05e4c02021-10-04 09:05:21 -07004016 eth_hw_addr_set(net_dev, mac_addr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004017 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004018 /* No MAC address configured, fill in net_dev->dev_addr
4019 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004020 */
4021 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004022 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
4023
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004024 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4025 net_dev->dev_addr);
4026 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004027 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004028 return err;
4029 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004030
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004031 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
4032 * practical purposes, this will be our "permanent" mac address,
4033 * at least until the next reboot. This move will also permit
4034 * register_netdevice() to properly fill up net_dev->perm_addr.
4035 */
4036 net_dev->addr_assign_type = NET_ADDR_PERM;
4037 } else {
4038 /* NET_ADDR_PERM is default, all we have to do is
4039 * fill in the device addr.
4040 */
Jakub Kicinskia05e4c02021-10-04 09:05:21 -07004041 eth_hw_addr_set(net_dev, dpni_mac_addr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004042 }
4043
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004044 return 0;
4045}
4046
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004047static int dpaa2_eth_netdev_init(struct net_device *net_dev)
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004048{
4049 struct device *dev = net_dev->dev.parent;
4050 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004051 u32 options = priv->dpni_attrs.options;
4052 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004053 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05004054 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004055 int err;
4056
4057 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004058 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004059
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004060 err = dpaa2_eth_set_mac_addr(priv);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004061 if (err)
4062 return err;
4063
4064 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004065 eth_broadcast_addr(bcast_addr);
4066 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
4067 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004068 dev_err(dev, "dpni_add_mac_addr() failed\n");
4069 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004070 }
4071
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05004072 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004073 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05004074 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05004075 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05004076 if (err) {
4077 dev_err(dev, "dpni_set_max_frame_length() failed\n");
4078 return err;
4079 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004080
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05004081 /* Set actual number of queues in the net device */
4082 num_queues = dpaa2_eth_queue_count(priv);
4083 err = netif_set_real_num_tx_queues(net_dev, num_queues);
4084 if (err) {
4085 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
4086 return err;
4087 }
4088 err = netif_set_real_num_rx_queues(net_dev, num_queues);
4089 if (err) {
4090 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
4091 return err;
4092 }
4093
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004094 /* Capabilities listing */
4095 supported |= IFF_LIVE_ADDR_CHANGE;
4096
4097 if (options & DPNI_OPT_NO_MAC_FILTER)
4098 not_supported |= IFF_UNICAST_FLT;
4099 else
4100 supported |= IFF_UNICAST_FLT;
4101
4102 net_dev->priv_flags |= supported;
4103 net_dev->priv_flags &= ~not_supported;
4104
4105 /* Features */
4106 net_dev->features = NETIF_F_RXCSUM |
4107 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4108 NETIF_F_SG | NETIF_F_HIGHDMA |
Ioana Ciornei3657cda2020-07-21 19:38:25 +03004109 NETIF_F_LLTX | NETIF_F_HW_TC;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004110 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004111
Ionut-robert Aron70b32d82021-01-11 19:07:25 +02004112 if (priv->dpni_attrs.vlan_filter_entries)
4113 net_dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4114
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004115 return 0;
4116}
4117
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004118static int dpaa2_eth_poll_link_state(void *arg)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004119{
4120 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
4121 int err;
4122
4123 while (!kthread_should_stop()) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004124 err = dpaa2_eth_link_state_update(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004125 if (unlikely(err))
4126 return err;
4127
4128 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
4129 }
4130
4131 return 0;
4132}
4133
Ioana Ciornei71947922019-10-31 01:18:31 +02004134static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
4135{
4136 struct fsl_mc_device *dpni_dev, *dpmac_dev;
4137 struct dpaa2_mac *mac;
4138 int err;
4139
4140 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
Ioana Ciornei27cfdad2021-08-03 19:57:42 +03004141 dpmac_dev = fsl_mc_get_endpoint(dpni_dev, 0);
Ioana Ciornei47325da2021-01-08 11:07:25 +02004142
4143 if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER)
4144 return PTR_ERR(dpmac_dev);
4145
4146 if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
Ioana Ciornei71947922019-10-31 01:18:31 +02004147 return 0;
4148
Ioana Ciornei71947922019-10-31 01:18:31 +02004149 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
4150 if (!mac)
4151 return -ENOMEM;
4152
4153 mac->mc_dev = dpmac_dev;
4154 mac->mc_io = priv->mc_io;
4155 mac->net_dev = priv->net_dev;
4156
Ioana Ciornei095dca12021-01-08 11:07:22 +02004157 err = dpaa2_mac_open(mac);
4158 if (err)
4159 goto err_free_mac;
Ioana Ciornei71947922019-10-31 01:18:31 +02004160 priv->mac = mac;
4161
Ioana Ciorneid87e6062021-01-08 11:07:23 +02004162 if (dpaa2_eth_is_type_phy(priv)) {
4163 err = dpaa2_mac_connect(mac);
Vladimir Olteanf5120f52021-05-21 17:12:20 +03004164 if (err && err != -EPROBE_DEFER)
4165 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint: %pe",
4166 ERR_PTR(err));
4167 if (err)
Ioana Ciorneid87e6062021-01-08 11:07:23 +02004168 goto err_close_mac;
Ioana Ciorneid87e6062021-01-08 11:07:23 +02004169 }
4170
Ioana Ciornei71947922019-10-31 01:18:31 +02004171 return 0;
Ioana Ciornei095dca12021-01-08 11:07:22 +02004172
4173err_close_mac:
4174 dpaa2_mac_close(mac);
Ioana Ciorneid87e6062021-01-08 11:07:23 +02004175 priv->mac = NULL;
Ioana Ciornei095dca12021-01-08 11:07:22 +02004176err_free_mac:
4177 kfree(mac);
4178 return err;
Ioana Ciornei71947922019-10-31 01:18:31 +02004179}
4180
4181static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
4182{
Ioana Ciorneid87e6062021-01-08 11:07:23 +02004183 if (dpaa2_eth_is_type_phy(priv))
4184 dpaa2_mac_disconnect(priv->mac);
Ioana Ciornei71947922019-10-31 01:18:31 +02004185
Ioana Ciornei848c1902021-01-11 19:18:02 +02004186 if (!dpaa2_eth_has_mac(priv))
4187 return;
4188
Ioana Ciornei095dca12021-01-08 11:07:22 +02004189 dpaa2_mac_close(priv->mac);
Ioana Ciornei71947922019-10-31 01:18:31 +02004190 kfree(priv->mac);
4191 priv->mac = NULL;
4192}
4193
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004194static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
4195{
Ioana Radulescu112197d2017-10-11 08:29:49 -05004196 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004197 struct device *dev = (struct device *)arg;
4198 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
4199 struct net_device *net_dev = dev_get_drvdata(dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02004200 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004201 int err;
4202
4203 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
4204 DPNI_IRQ_INDEX, &status);
4205 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004206 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05004207 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004208 }
4209
Ioana Radulescu112197d2017-10-11 08:29:49 -05004210 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004211 dpaa2_eth_link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004212
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02004213 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004214 dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
4215 dpaa2_eth_update_tx_fqids(priv);
Ioana Ciornei71947922019-10-31 01:18:31 +02004216
4217 rtnl_lock();
Ioana Ciorneid87e6062021-01-08 11:07:23 +02004218 if (dpaa2_eth_has_mac(priv))
Ioana Ciornei71947922019-10-31 01:18:31 +02004219 dpaa2_eth_disconnect_mac(priv);
4220 else
4221 dpaa2_eth_connect_mac(priv);
4222 rtnl_unlock();
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02004223 }
Florin Chiculita8398b372019-10-16 10:36:22 +03004224
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004225 return IRQ_HANDLED;
4226}
4227
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004228static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004229{
4230 int err = 0;
4231 struct fsl_mc_device_irq *irq;
4232
4233 err = fsl_mc_allocate_irqs(ls_dev);
4234 if (err) {
4235 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4236 return err;
4237 }
4238
4239 irq = ls_dev->irqs[0];
4240 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05004241 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004242 IRQF_NO_SUSPEND | IRQF_ONESHOT,
4243 dev_name(&ls_dev->dev), &ls_dev->dev);
4244 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004245 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004246 goto free_mc_irq;
4247 }
4248
4249 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
Florin Chiculita8398b372019-10-16 10:36:22 +03004250 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4251 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004252 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004253 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004254 goto free_irq;
4255 }
4256
4257 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4258 DPNI_IRQ_INDEX, 1);
4259 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004260 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004261 goto free_irq;
4262 }
4263
4264 return 0;
4265
4266free_irq:
4267 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
4268free_mc_irq:
4269 fsl_mc_free_irqs(ls_dev);
4270
4271 return err;
4272}
4273
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004274static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004275{
4276 int i;
4277 struct dpaa2_eth_channel *ch;
4278
4279 for (i = 0; i < priv->num_channels; i++) {
4280 ch = priv->channel[i];
4281 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4282 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
4283 NAPI_POLL_WEIGHT);
4284 }
4285}
4286
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004287static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004288{
4289 int i;
4290 struct dpaa2_eth_channel *ch;
4291
4292 for (i = 0; i < priv->num_channels; i++) {
4293 ch = priv->channel[i];
4294 netif_napi_del(&ch->napi);
4295 }
4296}
4297
4298static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4299{
4300 struct device *dev;
4301 struct net_device *net_dev = NULL;
4302 struct dpaa2_eth_priv *priv = NULL;
4303 int err = 0;
4304
4305 dev = &dpni_dev->dev;
4306
4307 /* Net device */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03004308 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004309 if (!net_dev) {
4310 dev_err(dev, "alloc_etherdev_mq() failed\n");
4311 return -ENOMEM;
4312 }
4313
4314 SET_NETDEV_DEV(net_dev, dev);
4315 dev_set_drvdata(dev, net_dev);
4316
4317 priv = netdev_priv(net_dev);
4318 priv->net_dev = net_dev;
4319
Ioana Radulescu08eb2392017-05-24 07:13:27 -05004320 priv->iommu_domain = iommu_get_domain_for_dev(dev);
4321
Yangbo Lu1cf773b2020-09-18 17:08:01 +08004322 priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4323 priv->rx_tstamp = false;
4324
Yangbo Luc5521182020-09-18 17:08:02 +08004325 priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
4326 if (!priv->dpaa2_ptp_wq) {
4327 err = -ENOMEM;
4328 goto err_wq_alloc;
4329 }
4330
4331 INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4332
4333 skb_queue_head_init(&priv->tx_skbs);
4334
Ioana Ciornei8ed3cef2021-04-02 12:55:32 +03004335 priv->rx_copybreak = DPAA2_ETH_DEFAULT_COPYBREAK;
4336
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004337 /* Obtain a MC portal */
4338 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4339 &priv->mc_io);
4340 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05004341 if (err == -ENXIO)
4342 err = -EPROBE_DEFER;
4343 else
4344 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004345 goto err_portal_alloc;
4346 }
4347
4348 /* MC objects initialization and configuration */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004349 err = dpaa2_eth_setup_dpni(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004350 if (err)
4351 goto err_dpni_setup;
4352
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004353 err = dpaa2_eth_setup_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004354 if (err)
4355 goto err_dpio_setup;
4356
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004357 dpaa2_eth_setup_fqs(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004358
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004359 err = dpaa2_eth_setup_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004360 if (err)
4361 goto err_dpbp_setup;
4362
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004363 err = dpaa2_eth_bind_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004364 if (err)
4365 goto err_bind;
4366
4367 /* Add a NAPI context for each channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004368 dpaa2_eth_add_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004369
4370 /* Percpu statistics */
4371 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4372 if (!priv->percpu_stats) {
4373 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4374 err = -ENOMEM;
4375 goto err_alloc_percpu_stats;
4376 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004377 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4378 if (!priv->percpu_extras) {
4379 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4380 err = -ENOMEM;
4381 goto err_alloc_percpu_extras;
4382 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004383
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004384 priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4385 if (!priv->sgt_cache) {
4386 dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4387 err = -ENOMEM;
4388 goto err_alloc_sgt_cache;
4389 }
4390
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004391 err = dpaa2_eth_netdev_init(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004392 if (err)
4393 goto err_netdev_init;
4394
4395 /* Configure checksum offload based on current interface flags */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004396 err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004397 if (err)
4398 goto err_csum;
4399
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004400 err = dpaa2_eth_set_tx_csum(priv,
4401 !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004402 if (err)
4403 goto err_csum;
4404
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004405 err = dpaa2_eth_alloc_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004406 if (err)
4407 goto err_alloc_rings;
4408
Ioana Ciorneif395b692020-05-31 00:08:13 +03004409#ifdef CONFIG_FSL_DPAA2_ETH_DCB
4410 if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4411 priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4412 net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4413 } else {
4414 dev_dbg(dev, "PFC not supported\n");
4415 }
4416#endif
4417
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004418 err = dpaa2_eth_setup_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004419 if (err) {
4420 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004421 priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004422 "%s_poll_link", net_dev->name);
4423 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004424 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004425 goto err_poll_thread;
4426 }
4427 priv->do_link_poll = true;
4428 }
4429
Ioana Ciornei71947922019-10-31 01:18:31 +02004430 err = dpaa2_eth_connect_mac(priv);
4431 if (err)
4432 goto err_connect_mac;
4433
Leon Romanovskybbb9ae22021-09-25 14:22:44 +03004434 err = dpaa2_eth_dl_alloc(priv);
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004435 if (err)
4436 goto err_dl_register;
4437
Ioana Ciornei061d6312020-10-01 18:11:48 +03004438 err = dpaa2_eth_dl_traps_register(priv);
4439 if (err)
4440 goto err_dl_trap_register;
4441
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004442 err = dpaa2_eth_dl_port_add(priv);
4443 if (err)
4444 goto err_dl_port_add;
4445
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004446 err = register_netdev(net_dev);
4447 if (err < 0) {
4448 dev_err(dev, "register_netdev() failed\n");
4449 goto err_netdev_reg;
4450 }
4451
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004452#ifdef CONFIG_DEBUG_FS
4453 dpaa2_dbg_add(priv);
4454#endif
4455
Leon Romanovskybbb9ae22021-09-25 14:22:44 +03004456 dpaa2_eth_dl_register(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004457 dev_info(dev, "Probed interface %s\n", net_dev->name);
4458 return 0;
4459
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004460err_netdev_reg:
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004461 dpaa2_eth_dl_port_del(priv);
4462err_dl_port_add:
Ioana Ciornei061d6312020-10-01 18:11:48 +03004463 dpaa2_eth_dl_traps_unregister(priv);
4464err_dl_trap_register:
Leon Romanovskybbb9ae22021-09-25 14:22:44 +03004465 dpaa2_eth_dl_free(priv);
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004466err_dl_register:
Ioana Ciornei71947922019-10-31 01:18:31 +02004467 dpaa2_eth_disconnect_mac(priv);
4468err_connect_mac:
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004469 if (priv->do_link_poll)
4470 kthread_stop(priv->poll_thread);
4471 else
4472 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004473err_poll_thread:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004474 dpaa2_eth_free_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004475err_alloc_rings:
4476err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004477err_netdev_init:
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004478 free_percpu(priv->sgt_cache);
4479err_alloc_sgt_cache:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004480 free_percpu(priv->percpu_extras);
4481err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004482 free_percpu(priv->percpu_stats);
4483err_alloc_percpu_stats:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004484 dpaa2_eth_del_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004485err_bind:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004486 dpaa2_eth_free_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004487err_dpbp_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004488 dpaa2_eth_free_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004489err_dpio_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004490 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004491err_dpni_setup:
4492 fsl_mc_portal_free(priv->mc_io);
4493err_portal_alloc:
Yangbo Luc5521182020-09-18 17:08:02 +08004494 destroy_workqueue(priv->dpaa2_ptp_wq);
4495err_wq_alloc:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004496 dev_set_drvdata(dev, NULL);
4497 free_netdev(net_dev);
4498
4499 return err;
4500}
4501
4502static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4503{
4504 struct device *dev;
4505 struct net_device *net_dev;
4506 struct dpaa2_eth_priv *priv;
4507
4508 dev = &ls_dev->dev;
4509 net_dev = dev_get_drvdata(dev);
4510 priv = netdev_priv(net_dev);
4511
Leon Romanovskybbb9ae22021-09-25 14:22:44 +03004512 dpaa2_eth_dl_unregister(priv);
4513
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004514#ifdef CONFIG_DEBUG_FS
4515 dpaa2_dbg_remove(priv);
4516#endif
Ioana Ciornei71947922019-10-31 01:18:31 +02004517 rtnl_lock();
4518 dpaa2_eth_disconnect_mac(priv);
4519 rtnl_unlock();
4520
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004521 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004522
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004523 dpaa2_eth_dl_port_del(priv);
Ioana Ciornei061d6312020-10-01 18:11:48 +03004524 dpaa2_eth_dl_traps_unregister(priv);
Leon Romanovskybbb9ae22021-09-25 14:22:44 +03004525 dpaa2_eth_dl_free(priv);
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004526
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004527 if (priv->do_link_poll)
4528 kthread_stop(priv->poll_thread);
4529 else
4530 fsl_mc_free_irqs(ls_dev);
4531
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004532 dpaa2_eth_free_rings(priv);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004533 free_percpu(priv->sgt_cache);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004534 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004535 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004536
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004537 dpaa2_eth_del_ch_napi(priv);
4538 dpaa2_eth_free_dpbp(priv);
4539 dpaa2_eth_free_dpio(priv);
4540 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004541
4542 fsl_mc_portal_free(priv->mc_io);
4543
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004544 free_netdev(net_dev);
4545
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05004546 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05004547
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004548 return 0;
4549}
4550
4551static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
4552 {
4553 .vendor = FSL_MC_VENDOR_FREESCALE,
4554 .obj_type = "dpni",
4555 },
4556 { .vendor = 0x0 }
4557};
4558MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
4559
4560static struct fsl_mc_driver dpaa2_eth_driver = {
4561 .driver = {
4562 .name = KBUILD_MODNAME,
4563 .owner = THIS_MODULE,
4564 },
4565 .probe = dpaa2_eth_probe,
4566 .remove = dpaa2_eth_remove,
4567 .match_id_table = dpaa2_eth_match_id_table
4568};
4569
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004570static int __init dpaa2_eth_driver_init(void)
4571{
4572 int err;
4573
4574 dpaa2_eth_dbg_init();
4575 err = fsl_mc_driver_register(&dpaa2_eth_driver);
4576 if (err) {
4577 dpaa2_eth_dbg_exit();
4578 return err;
4579 }
4580
4581 return 0;
4582}
4583
4584static void __exit dpaa2_eth_driver_exit(void)
4585{
4586 dpaa2_eth_dbg_exit();
4587 fsl_mc_driver_unregister(&dpaa2_eth_driver);
4588}
4589
4590module_init(dpaa2_eth_driver_init);
4591module_exit(dpaa2_eth_driver_exit);