blob: 535b9079943c78036e0889edefa61a53856b4a8f [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 rcu_read_lock();
356
357 xdp_prog = READ_ONCE(ch->xdp.prog);
358 if (!xdp_prog)
359 goto out;
360
Lorenzo Bianconibe9df4a2020-12-22 22:09:29 +0100361 offset = dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM;
362 xdp_init_buff(&xdp, DPAA2_ETH_RX_BUF_RAW_SIZE - offset, &ch->xdp_rxq);
363 xdp_prepare_buff(&xdp, vaddr + offset, XDP_PACKET_HEADROOM,
364 dpaa2_fd_get_len(fd), false);
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200365
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000366 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
367
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000368 /* xdp.data pointer may have changed */
369 dpaa2_fd_set_offset(fd, xdp.data - vaddr);
370 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
371
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000372 switch (xdp_act) {
373 case XDP_PASS:
374 break;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000375 case XDP_TX:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300376 dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000377 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000378 default:
379 bpf_warn_invalid_xdp_action(xdp_act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500380 fallthrough;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000381 case XDP_ABORTED:
382 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500383 fallthrough;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000384 case XDP_DROP:
Ioana Ciornei28d137c2021-04-02 12:55:30 +0300385 dpaa2_eth_recycle_buf(priv, ch, addr);
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000386 ch->stats.xdp_drop++;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000387 break;
Ioana Radulescud678be12019-03-01 17:47:24 +0000388 case XDP_REDIRECT:
389 dma_unmap_page(priv->net_dev->dev.parent, addr,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300390 priv->rx_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescud678be12019-03-01 17:47:24 +0000391 ch->buf_count--;
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200392
393 /* Allow redirect use of full headroom */
Ioana Radulescud678be12019-03-01 17:47:24 +0000394 xdp.data_hard_start = vaddr;
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200395 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
396
Ioana Radulescud678be12019-03-01 17:47:24 +0000397 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
Ioana Ciorneie12be912021-02-11 21:51:22 +0200398 if (unlikely(err)) {
399 addr = dma_map_page(priv->net_dev->dev.parent,
400 virt_to_page(vaddr), 0,
401 priv->rx_buf_size, DMA_BIDIRECTIONAL);
402 if (unlikely(dma_mapping_error(priv->net_dev->dev.parent, addr))) {
403 free_pages((unsigned long)vaddr, 0);
404 } else {
405 ch->buf_count++;
Ioana Ciornei28d137c2021-04-02 12:55:30 +0300406 dpaa2_eth_recycle_buf(priv, ch, addr);
Ioana Ciorneie12be912021-02-11 21:51:22 +0200407 }
Ioana Radulescud678be12019-03-01 17:47:24 +0000408 ch->stats.xdp_drop++;
Ioana Ciorneie12be912021-02-11 21:51:22 +0200409 } else {
Ioana Radulescud678be12019-03-01 17:47:24 +0000410 ch->stats.xdp_redirect++;
Ioana Ciorneie12be912021-02-11 21:51:22 +0200411 }
Ioana Radulescud678be12019-03-01 17:47:24 +0000412 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000413 }
414
Ioana Radulescud678be12019-03-01 17:47:24 +0000415 ch->xdp.res |= xdp_act;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000416out:
417 rcu_read_unlock();
418 return xdp_act;
419}
420
Ioana Ciornei50f82692021-04-02 12:55:31 +0300421static struct sk_buff *dpaa2_eth_copybreak(struct dpaa2_eth_channel *ch,
422 const struct dpaa2_fd *fd,
423 void *fd_vaddr)
424{
425 u16 fd_offset = dpaa2_fd_get_offset(fd);
426 u32 fd_length = dpaa2_fd_get_len(fd);
427 struct sk_buff *skb = NULL;
428 unsigned int skb_len;
429
430 if (fd_length > DPAA2_ETH_DEFAULT_COPYBREAK)
431 return NULL;
432
433 skb_len = fd_length + dpaa2_eth_needed_headroom(NULL);
434
435 skb = napi_alloc_skb(&ch->napi, skb_len);
436 if (!skb)
437 return NULL;
438
439 skb_reserve(skb, dpaa2_eth_needed_headroom(NULL));
440 skb_put(skb, fd_length);
441
442 memcpy(skb->data, fd_vaddr + fd_offset, fd_length);
443
444 dpaa2_eth_recycle_buf(ch->priv, ch, dpaa2_fd_get_addr(fd));
445
446 return skb;
447}
448
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500449/* Main Rx frame processing routine */
450static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
451 struct dpaa2_eth_channel *ch,
452 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000453 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500454{
455 dma_addr_t addr = dpaa2_fd_get_addr(fd);
456 u8 fd_format = dpaa2_fd_get_format(fd);
457 void *vaddr;
458 struct sk_buff *skb;
459 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500460 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500461 struct device *dev = priv->net_dev->dev.parent;
462 struct dpaa2_fas *fas;
Ioana Radulescud695e762017-06-06 10:00:35 -0500463 void *buf_data;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500464 u32 status = 0;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000465 u32 xdp_act;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500466
Ioana Radulescu56361872017-04-28 04:50:32 -0500467 /* Tracing point */
468 trace_dpaa2_rx_fd(priv->net_dev, fd);
469
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500470 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300471 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu18c2e772018-11-26 16:27:32 +0000472 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500473
Ioana Radulescu54ce8912017-12-08 06:47:53 -0600474 fas = dpaa2_get_fas(vaddr, false);
Ioana Radulescud695e762017-06-06 10:00:35 -0500475 prefetch(fas);
476 buf_data = vaddr + dpaa2_fd_get_offset(fd);
477 prefetch(buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500478
479 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500480 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500481
482 if (fd_format == dpaa2_fd_single) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300483 xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000484 if (xdp_act != XDP_PASS) {
485 percpu_stats->rx_packets++;
486 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
487 return;
488 }
489
Ioana Ciornei50f82692021-04-02 12:55:31 +0300490 skb = dpaa2_eth_copybreak(ch, fd, vaddr);
491 if (!skb) {
492 dma_unmap_page(dev, addr, priv->rx_buf_size,
493 DMA_BIDIRECTIONAL);
494 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
495 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500496 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000497 WARN_ON(priv->xdp_prog);
498
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300499 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000500 DMA_BIDIRECTIONAL);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300501 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000502 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500503 percpu_extras->rx_sg_frames++;
504 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500505 } else {
506 /* We don't support any other format */
507 goto err_frame_format;
508 }
509
510 if (unlikely(!skb))
511 goto err_build_skb;
512
513 prefetch(skb->data);
514
Ioana Radulescu859f9982018-04-26 18:23:47 +0800515 /* Get the timestamp value */
516 if (priv->rx_tstamp) {
517 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
518 __le64 *ts = dpaa2_get_ts(vaddr, false);
519 u64 ns;
520
521 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
522
523 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
524 shhwtstamps->hwtstamp = ns_to_ktime(ns);
525 }
526
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500527 /* Check if we need to validate the L4 csum */
528 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500529 status = le32_to_cpu(fas->status);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300530 dpaa2_eth_validate_rx_csum(priv, status, skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500531 }
532
533 skb->protocol = eth_type_trans(skb, priv->net_dev);
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000534 skb_record_rx_queue(skb, fq->flowid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500535
536 percpu_stats->rx_packets++;
537 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
538
Ioana Ciornei0a25d922019-03-25 13:42:39 +0000539 list_add_tail(&skb->list, ch->rx_list);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500540
541 return;
542
543err_build_skb:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300544 dpaa2_eth_free_rx_fd(priv, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500545err_frame_format:
546 percpu_stats->rx_dropped++;
547}
548
Ioana Ciornei061d6312020-10-01 18:11:48 +0300549/* Processing of Rx frames received on the error FQ
550 * We check and print the error bits and then free the frame
551 */
552static void dpaa2_eth_rx_err(struct dpaa2_eth_priv *priv,
553 struct dpaa2_eth_channel *ch,
554 const struct dpaa2_fd *fd,
555 struct dpaa2_eth_fq *fq __always_unused)
556{
557 struct device *dev = priv->net_dev->dev.parent;
558 dma_addr_t addr = dpaa2_fd_get_addr(fd);
559 u8 fd_format = dpaa2_fd_get_format(fd);
560 struct rtnl_link_stats64 *percpu_stats;
561 struct dpaa2_eth_trap_item *trap_item;
562 struct dpaa2_fapr *fapr;
563 struct sk_buff *skb;
564 void *buf_data;
565 void *vaddr;
566
567 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
568 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
569 DMA_BIDIRECTIONAL);
570
571 buf_data = vaddr + dpaa2_fd_get_offset(fd);
572
573 if (fd_format == dpaa2_fd_single) {
574 dma_unmap_page(dev, addr, priv->rx_buf_size,
575 DMA_BIDIRECTIONAL);
576 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
577 } else if (fd_format == dpaa2_fd_sg) {
578 dma_unmap_page(dev, addr, priv->rx_buf_size,
579 DMA_BIDIRECTIONAL);
580 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
581 free_pages((unsigned long)vaddr, 0);
582 } else {
583 /* We don't support any other format */
584 dpaa2_eth_free_rx_fd(priv, fd, vaddr);
585 goto err_frame_format;
586 }
587
588 fapr = dpaa2_get_fapr(vaddr, false);
589 trap_item = dpaa2_eth_dl_get_trap(priv, fapr);
590 if (trap_item)
591 devlink_trap_report(priv->devlink, skb, trap_item->trap_ctx,
592 &priv->devlink_port, NULL);
593 consume_skb(skb);
594
595err_frame_format:
596 percpu_stats = this_cpu_ptr(priv->percpu_stats);
597 percpu_stats->rx_errors++;
598 ch->buf_count--;
599}
600
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500601/* Consume all frames pull-dequeued into the store. This is the simplest way to
602 * make sure we don't accidentally issue another volatile dequeue which would
603 * overwrite (leak) frames already in the store.
604 *
605 * Observance of NAPI budget is not our concern, leaving that to the caller.
606 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300607static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
608 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500609{
610 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000611 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500612 struct dpaa2_dq *dq;
613 const struct dpaa2_fd *fd;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300614 int cleaned = 0, retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500615 int is_last;
616
617 do {
618 dq = dpaa2_io_store_next(ch->store, &is_last);
619 if (unlikely(!dq)) {
620 /* If we're here, we *must* have placed a
621 * volatile dequeue comnmand, so keep reading through
622 * the store until we get some sort of valid response
623 * token (either a valid frame or an "empty dequeue")
624 */
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300625 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
626 netdev_err_once(priv->net_dev,
627 "Unable to read a valid dequeue response\n");
628 return -ETIMEDOUT;
629 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500630 continue;
631 }
632
633 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600634 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500635
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000636 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500637 cleaned++;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300638 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500639 } while (!is_last);
640
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000641 if (!cleaned)
642 return 0;
643
644 fq->stats.frames += cleaned;
Ioana Ciornei460fd832020-04-24 12:33:18 +0300645 ch->stats.frames += cleaned;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000646
647 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000648 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000649 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000650 if (src)
651 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000652
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500653 return cleaned;
654}
655
Yangbo Luc5521182020-09-18 17:08:02 +0800656static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
657 u8 *msgtype, u8 *twostep, u8 *udp,
658 u16 *correction_offset,
659 u16 *origintimestamp_offset)
Ioana Radulescu859f9982018-04-26 18:23:47 +0800660{
Yangbo Luc5521182020-09-18 17:08:02 +0800661 unsigned int ptp_class;
662 struct ptp_header *hdr;
663 unsigned int type;
664 u8 *base;
665
666 ptp_class = ptp_classify_raw(skb);
667 if (ptp_class == PTP_CLASS_NONE)
668 return -EINVAL;
669
670 hdr = ptp_parse_header(skb, ptp_class);
671 if (!hdr)
672 return -EINVAL;
673
674 *msgtype = ptp_get_msgtype(hdr, ptp_class);
675 *twostep = hdr->flag_field[0] & 0x2;
676
677 type = ptp_class & PTP_CLASS_PMASK;
678 if (type == PTP_CLASS_IPV4 ||
679 type == PTP_CLASS_IPV6)
680 *udp = 1;
681 else
682 *udp = 0;
683
684 base = skb_mac_header(skb);
685 *correction_offset = (u8 *)&hdr->correction - base;
686 *origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
687
688 return 0;
689}
690
691/* Configure the egress frame annotation for timestamp update */
692static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
693 struct dpaa2_fd *fd,
694 void *buf_start,
695 struct sk_buff *skb)
696{
697 struct ptp_tstamp origin_timestamp;
698 struct dpni_single_step_cfg cfg;
699 u8 msgtype, twostep, udp;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800700 struct dpaa2_faead *faead;
Yangbo Luc5521182020-09-18 17:08:02 +0800701 struct dpaa2_fas *fas;
702 struct timespec64 ts;
703 u16 offset1, offset2;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800704 u32 ctrl, frc;
Yangbo Luc5521182020-09-18 17:08:02 +0800705 __le64 *ns;
706 u8 *data;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800707
708 /* Mark the egress frame annotation area as valid */
709 frc = dpaa2_fd_get_frc(fd);
710 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
711
712 /* Set hardware annotation size */
713 ctrl = dpaa2_fd_get_ctrl(fd);
714 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
715
716 /* enable UPD (update prepanded data) bit in FAEAD field of
717 * hardware frame annotation area
718 */
719 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
720 faead = dpaa2_get_faead(buf_start, true);
721 faead->ctrl = cpu_to_le32(ctrl);
Yangbo Luc5521182020-09-18 17:08:02 +0800722
723 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
724 if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
725 &offset1, &offset2) ||
Christian Eggers6b6817c2020-11-20 09:41:05 +0100726 msgtype != PTP_MSGTYPE_SYNC || twostep) {
Yangbo Luc5521182020-09-18 17:08:02 +0800727 WARN_ONCE(1, "Bad packet for one-step timestamping\n");
728 return;
729 }
730
731 /* Mark the frame annotation status as valid */
732 frc = dpaa2_fd_get_frc(fd);
733 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);
734
735 /* Mark the PTP flag for one step timestamping */
736 fas = dpaa2_get_fas(buf_start, true);
737 fas->status = cpu_to_le32(DPAA2_FAS_PTP);
738
739 dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
740 ns = dpaa2_get_ts(buf_start, true);
741 *ns = cpu_to_le64(timespec64_to_ns(&ts) /
742 DPAA2_PTP_CLK_PERIOD_NS);
743
744 /* Update current time to PTP message originTimestamp field */
745 ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
746 data = skb_mac_header(skb);
747 *(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
748 *(__be32 *)(data + offset2 + 2) =
749 htonl(origin_timestamp.sec_lsb);
750 *(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);
751
752 cfg.en = 1;
753 cfg.ch_update = udp;
754 cfg.offset = offset1;
755 cfg.peer_delay = 0;
756
757 if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token,
758 &cfg))
759 WARN_ONCE(1, "Failed to set single step register");
760 }
Ioana Radulescu859f9982018-04-26 18:23:47 +0800761}
762
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500763/* Create a frame descriptor based on a fragmented skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300764static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
765 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800766 struct dpaa2_fd *fd,
767 void **swa_addr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500768{
769 struct device *dev = priv->net_dev->dev.parent;
770 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500771 dma_addr_t addr;
772 int nr_frags = skb_shinfo(skb)->nr_frags;
773 struct dpaa2_sg_entry *sgt;
774 int i, err;
775 int sgt_buf_size;
776 struct scatterlist *scl, *crt_scl;
777 int num_sg;
778 int num_dma_bufs;
779 struct dpaa2_eth_swa *swa;
780
781 /* Create and map scatterlist.
782 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
783 * to go beyond nr_frags+1.
784 * Note: We don't support chained scatterlists
785 */
786 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
787 return -EINVAL;
788
Julia Lawalld4ceb8d2020-09-20 13:26:15 +0200789 scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500790 if (unlikely(!scl))
791 return -ENOMEM;
792
793 sg_init_table(scl, nr_frags + 1);
794 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Ciornei37fbbdda2020-06-24 14:34:18 +0300795 if (unlikely(num_sg < 0)) {
796 err = -ENOMEM;
797 goto dma_map_sg_failed;
798 }
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500799 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500800 if (unlikely(!num_dma_bufs)) {
801 err = -ENOMEM;
802 goto dma_map_sg_failed;
803 }
804
805 /* Prepare the HW SGT structure */
806 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500807 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Kevin Haod0dfbb92021-02-04 18:56:38 +0800808 sgt_buf = napi_alloc_frag_align(sgt_buf_size, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500809 if (unlikely(!sgt_buf)) {
810 err = -ENOMEM;
811 goto sgt_buf_alloc_failed;
812 }
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500813 memset(sgt_buf, 0, sgt_buf_size);
814
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500815 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
816
817 /* Fill in the HW SGT structure.
818 *
819 * sgt_buf is zeroed out, so the following fields are implicit
820 * in all sgt entries:
821 * - offset is 0
822 * - format is 'dpaa2_sg_single'
823 */
824 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
825 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
826 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
827 }
828 dpaa2_sg_set_final(&sgt[i - 1], true);
829
830 /* Store the skb backpointer in the SGT buffer.
831 * Fit the scatterlist and the number of buffers alongside the
832 * skb backpointer in the software annotation area. We'll need
833 * all of them on Tx Conf.
834 */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800835 *swa_addr = (void *)sgt_buf;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500836 swa = (struct dpaa2_eth_swa *)sgt_buf;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000837 swa->type = DPAA2_ETH_SWA_SG;
838 swa->sg.skb = skb;
839 swa->sg.scl = scl;
840 swa->sg.num_sg = num_sg;
841 swa->sg.sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500842
843 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500844 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500845 if (unlikely(dma_mapping_error(dev, addr))) {
846 err = -ENOMEM;
847 goto dma_map_single_failed;
848 }
849 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
850 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
851 dpaa2_fd_set_addr(fd, addr);
852 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000853 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500854
855 return 0;
856
857dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500858 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500859sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500860 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500861dma_map_sg_failed:
862 kfree(scl);
863 return err;
864}
865
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300866/* Create a SG frame descriptor based on a linear skb.
867 *
868 * This function is used on the Tx path when the skb headroom is not large
869 * enough for the HW requirements, thus instead of realloc-ing the skb we
870 * create a SG frame descriptor with only one entry.
871 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300872static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
873 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800874 struct dpaa2_fd *fd,
875 void **swa_addr)
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300876{
877 struct device *dev = priv->net_dev->dev.parent;
878 struct dpaa2_eth_sgt_cache *sgt_cache;
879 struct dpaa2_sg_entry *sgt;
880 struct dpaa2_eth_swa *swa;
881 dma_addr_t addr, sgt_addr;
882 void *sgt_buf = NULL;
883 int sgt_buf_size;
884 int err;
885
886 /* Prepare the HW SGT structure */
887 sgt_cache = this_cpu_ptr(priv->sgt_cache);
888 sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
889
890 if (sgt_cache->count == 0)
891 sgt_buf = kzalloc(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN,
892 GFP_ATOMIC);
893 else
894 sgt_buf = sgt_cache->buf[--sgt_cache->count];
895 if (unlikely(!sgt_buf))
896 return -ENOMEM;
897
898 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
899 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
900
901 addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
902 if (unlikely(dma_mapping_error(dev, addr))) {
903 err = -ENOMEM;
904 goto data_map_failed;
905 }
906
907 /* Fill in the HW SGT structure */
908 dpaa2_sg_set_addr(sgt, addr);
909 dpaa2_sg_set_len(sgt, skb->len);
910 dpaa2_sg_set_final(sgt, true);
911
912 /* Store the skb backpointer in the SGT buffer */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800913 *swa_addr = (void *)sgt_buf;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300914 swa = (struct dpaa2_eth_swa *)sgt_buf;
915 swa->type = DPAA2_ETH_SWA_SINGLE;
916 swa->single.skb = skb;
Ioana Ciornei54a57d12020-12-11 19:16:07 +0200917 swa->single.sgt_size = sgt_buf_size;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300918
919 /* Separately map the SGT buffer */
920 sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
921 if (unlikely(dma_mapping_error(dev, sgt_addr))) {
922 err = -ENOMEM;
923 goto sgt_map_failed;
924 }
925
926 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
927 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
928 dpaa2_fd_set_addr(fd, sgt_addr);
929 dpaa2_fd_set_len(fd, skb->len);
930 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
931
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300932 return 0;
933
934sgt_map_failed:
935 dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
936data_map_failed:
937 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
938 kfree(sgt_buf);
939 else
940 sgt_cache->buf[sgt_cache->count++] = sgt_buf;
941
942 return err;
943}
944
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500945/* Create a frame descriptor based on a linear skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300946static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
947 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800948 struct dpaa2_fd *fd,
949 void **swa_addr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500950{
951 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600952 u8 *buffer_start, *aligned_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000953 struct dpaa2_eth_swa *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500954 dma_addr_t addr;
955
Yangbo Lu1cf773b2020-09-18 17:08:01 +0800956 buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
Ioana Radulescuc1636852017-12-08 06:47:58 -0600957
958 /* If there's enough room to align the FD address, do it.
959 * It will help hardware optimize accesses.
960 */
961 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
962 DPAA2_ETH_TX_BUF_ALIGN);
963 if (aligned_start >= skb->head)
964 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500965
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500966 /* Store a backpointer to the skb at the beginning of the buffer
967 * (in the private data area) such that we can release it
968 * on Tx confirm
969 */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800970 *swa_addr = (void *)buffer_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000971 swa = (struct dpaa2_eth_swa *)buffer_start;
972 swa->type = DPAA2_ETH_SWA_SINGLE;
973 swa->single.skb = skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500974
975 addr = dma_map_single(dev, buffer_start,
976 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500977 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500978 if (unlikely(dma_mapping_error(dev, addr)))
979 return -ENOMEM;
980
981 dpaa2_fd_set_addr(fd, addr);
982 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
983 dpaa2_fd_set_len(fd, skb->len);
984 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000985 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500986
987 return 0;
988}
989
990/* FD freeing routine on the Tx path
991 *
992 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
993 * back-pointed to is also freed.
994 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
995 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500996 */
Yangbo Luc5521182020-09-18 17:08:02 +0800997static void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300998 struct dpaa2_eth_fq *fq,
999 const struct dpaa2_fd *fd, bool in_napi)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001000{
1001 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001002 dma_addr_t fd_addr, sg_addr;
Ioana Radulescud678be12019-03-01 17:47:24 +00001003 struct sk_buff *skb = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001004 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001005 struct dpaa2_eth_swa *swa;
1006 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescud678be12019-03-01 17:47:24 +00001007 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001008
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001009 struct dpaa2_eth_sgt_cache *sgt_cache;
1010 struct dpaa2_sg_entry *sgt;
1011
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001012 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +00001013 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
1014 swa = (struct dpaa2_eth_swa *)buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001015
1016 if (fd_format == dpaa2_fd_single) {
Ioana Radulescud678be12019-03-01 17:47:24 +00001017 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
1018 skb = swa->single.skb;
1019 /* Accessing the skb buffer is safe before dma unmap,
1020 * because we didn't map the actual skb shell.
1021 */
1022 dma_unmap_single(dev, fd_addr,
1023 skb_tail_pointer(skb) - buffer_start,
1024 DMA_BIDIRECTIONAL);
1025 } else {
1026 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
1027 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
1028 DMA_BIDIRECTIONAL);
1029 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001030 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001031 if (swa->type == DPAA2_ETH_SWA_SG) {
1032 skb = swa->sg.skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001033
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001034 /* Unmap the scatterlist */
1035 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
1036 DMA_BIDIRECTIONAL);
1037 kfree(swa->sg.scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001038
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001039 /* Unmap the SGT buffer */
1040 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
1041 DMA_BIDIRECTIONAL);
1042 } else {
1043 skb = swa->single.skb;
1044
1045 /* Unmap the SGT Buffer */
1046 dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
1047 DMA_BIDIRECTIONAL);
1048
1049 sgt = (struct dpaa2_sg_entry *)(buffer_start +
1050 priv->tx_data_offset);
1051 sg_addr = dpaa2_sg_get_addr(sgt);
1052 dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
1053 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001054 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06001055 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001056 return;
1057 }
1058
Ioana Radulescud678be12019-03-01 17:47:24 +00001059 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
1060 fq->dq_frames++;
1061 fq->dq_bytes += fd_len;
1062 }
1063
1064 if (swa->type == DPAA2_ETH_SWA_XDP) {
1065 xdp_return_frame(swa->xdp.xdpf);
1066 return;
1067 }
1068
Ioana Radulescu859f9982018-04-26 18:23:47 +08001069 /* Get the timestamp value */
Yangbo Lu1cf773b2020-09-18 17:08:01 +08001070 if (skb->cb[0] == TX_TSTAMP) {
Ioana Radulescu859f9982018-04-26 18:23:47 +08001071 struct skb_shared_hwtstamps shhwtstamps;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +00001072 __le64 *ts = dpaa2_get_ts(buffer_start, true);
Ioana Radulescu859f9982018-04-26 18:23:47 +08001073 u64 ns;
1074
1075 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1076
1077 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
1078 shhwtstamps.hwtstamp = ns_to_ktime(ns);
1079 skb_tstamp_tx(skb, &shhwtstamps);
Yangbo Luc5521182020-09-18 17:08:02 +08001080 } else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1081 mutex_unlock(&priv->onestep_tstamp_lock);
Ioana Radulescu859f9982018-04-26 18:23:47 +08001082 }
1083
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -05001084 /* Free SGT buffer allocated on tx */
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001085 if (fd_format != dpaa2_fd_single) {
1086 sgt_cache = this_cpu_ptr(priv->sgt_cache);
1087 if (swa->type == DPAA2_ETH_SWA_SG) {
1088 skb_free_frag(buffer_start);
1089 } else {
1090 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
1091 kfree(buffer_start);
1092 else
1093 sgt_cache->buf[sgt_cache->count++] = buffer_start;
1094 }
1095 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001096
1097 /* Move on with skb release */
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +00001098 napi_consume_skb(skb, in_napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001099}
1100
Yangbo Luc5521182020-09-18 17:08:02 +08001101static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1102 struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001103{
1104 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1105 struct dpaa2_fd fd;
1106 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001107 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001108 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001109 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001110 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -06001111 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001112 u32 fd_len;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001113 u8 prio = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001114 int err, i;
Yangbo Lu64a965d2020-09-18 17:08:00 +08001115 void *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001116
1117 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001118 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001119
Yangbo Lu1cf773b2020-09-18 17:08:01 +08001120 needed_headroom = dpaa2_eth_needed_headroom(skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001121
1122 /* We'll be holding a back-reference to the skb until Tx Confirmation;
1123 * we don't want that overwritten by a concurrent Tx with a cloned skb.
1124 */
1125 skb = skb_unshare(skb, GFP_ATOMIC);
1126 if (unlikely(!skb)) {
1127 /* skb_unshare() has already freed the skb */
1128 percpu_stats->tx_dropped++;
1129 return NETDEV_TX_OK;
1130 }
1131
1132 /* Setup the FD fields */
1133 memset(&fd, 0, sizeof(fd));
1134
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001135 if (skb_is_nonlinear(skb)) {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001136 err = dpaa2_eth_build_sg_fd(priv, skb, &fd, &swa);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001137 percpu_extras->tx_sg_frames++;
1138 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001139 } else if (skb_headroom(skb) < needed_headroom) {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001140 err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, &fd, &swa);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001141 percpu_extras->tx_sg_frames++;
1142 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciornei4c96c0a2020-06-29 21:47:12 +03001143 percpu_extras->tx_converted_sg_frames++;
1144 percpu_extras->tx_converted_sg_bytes += skb->len;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001145 } else {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001146 err = dpaa2_eth_build_single_fd(priv, skb, &fd, &swa);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001147 }
1148
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001149 if (unlikely(err)) {
1150 percpu_stats->tx_dropped++;
1151 goto err_build_fd;
1152 }
1153
Yangbo Luc5521182020-09-18 17:08:02 +08001154 if (skb->cb[0])
1155 dpaa2_eth_enable_tx_tstamp(priv, &fd, swa, skb);
Yangbo Lu64a965d2020-09-18 17:08:00 +08001156
Ioana Radulescu56361872017-04-28 04:50:32 -05001157 /* Tracing point */
1158 trace_dpaa2_tx_fd(net_dev, &fd);
1159
Ioana Radulescu537336c2017-12-21 06:33:20 -06001160 /* TxConf FQ selection relies on queue id from the stack.
1161 * In case of a forwarded frame from another DPNI interface, we choose
1162 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001163 */
Ioana Radulescu537336c2017-12-21 06:33:20 -06001164 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001165
1166 if (net_dev->num_tc) {
1167 prio = netdev_txq_to_tc(net_dev, queue_mapping);
1168 /* Hardware interprets priority level 0 as being the highest,
1169 * so we need to do a reverse mapping to the netdev tc index
1170 */
1171 prio = net_dev->num_tc - prio - 1;
1172 /* We have only one FQ array entry for all Tx hardware queues
1173 * with the same flow id (but different priority levels)
1174 */
1175 queue_mapping %= dpaa2_eth_queue_count(priv);
1176 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001177 fq = &priv->fq[queue_mapping];
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001178
1179 fd_len = dpaa2_fd_get_len(&fd);
1180 nq = netdev_get_tx_queue(net_dev, queue_mapping);
1181 netdev_tx_sent_queue(nq, fd_len);
1182
1183 /* Everything that happens after this enqueues might race with
1184 * the Tx confirmation callback for this frame
1185 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001186 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Ciornei6ff80442020-04-22 15:05:11 +03001187 err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001188 if (err != -EBUSY)
1189 break;
1190 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001191 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001192 if (unlikely(err < 0)) {
1193 percpu_stats->tx_errors++;
1194 /* Clean up everything, including freeing the skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001195 dpaa2_eth_free_tx_fd(priv, fq, &fd, false);
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001196 netdev_tx_completed_queue(nq, 1, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001197 } else {
1198 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001199 percpu_stats->tx_bytes += fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001200 }
1201
1202 return NETDEV_TX_OK;
1203
1204err_build_fd:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001205 dev_kfree_skb(skb);
1206
1207 return NETDEV_TX_OK;
1208}
1209
Yangbo Luc5521182020-09-18 17:08:02 +08001210static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1211{
1212 struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1213 tx_onestep_tstamp);
1214 struct sk_buff *skb;
1215
1216 while (true) {
1217 skb = skb_dequeue(&priv->tx_skbs);
1218 if (!skb)
1219 return;
1220
1221 /* Lock just before TX one-step timestamping packet,
1222 * and release the lock in dpaa2_eth_free_tx_fd when
1223 * confirm the packet has been sent on hardware, or
1224 * when clean up during transmit failure.
1225 */
1226 mutex_lock(&priv->onestep_tstamp_lock);
1227 __dpaa2_eth_tx(skb, priv->net_dev);
1228 }
1229}
1230
1231static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1232{
1233 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1234 u8 msgtype, twostep, udp;
1235 u16 offset1, offset2;
1236
1237 /* Utilize skb->cb[0] for timestamping request per skb */
1238 skb->cb[0] = 0;
1239
1240 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1241 if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1242 skb->cb[0] = TX_TSTAMP;
1243 else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1244 skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1245 }
1246
1247 /* TX for one-step timestamping PTP Sync packet */
1248 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1249 if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1250 &offset1, &offset2))
Christian Eggers6b6817c2020-11-20 09:41:05 +01001251 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) {
Yangbo Luc5521182020-09-18 17:08:02 +08001252 skb_queue_tail(&priv->tx_skbs, skb);
1253 queue_work(priv->dpaa2_ptp_wq,
1254 &priv->tx_onestep_tstamp);
1255 return NETDEV_TX_OK;
1256 }
1257 /* Use two-step timestamping if not one-step timestamping
1258 * PTP Sync packet
1259 */
1260 skb->cb[0] = TX_TSTAMP;
1261 }
1262
1263 /* TX for other packets */
1264 return __dpaa2_eth_tx(skb, net_dev);
1265}
1266
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001267/* Tx confirmation frame processing routine */
1268static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +00001269 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001270 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001271 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001272{
1273 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001274 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001275 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001276 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001277
Ioana Radulescu56361872017-04-28 04:50:32 -05001278 /* Tracing point */
1279 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1280
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001281 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1282 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001283 percpu_extras->tx_conf_bytes += fd_len;
1284
Ioana Radulescu39163c02017-06-06 10:00:39 -05001285 /* Check frame errors in the FD field */
1286 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001287 dpaa2_eth_free_tx_fd(priv, fq, fd, true);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001288
1289 if (likely(!fd_errors))
1290 return;
1291
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06001292 if (net_ratelimit())
1293 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1294 fd_errors);
1295
Ioana Radulescu39163c02017-06-06 10:00:39 -05001296 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1297 /* Tx-conf logically pertains to the egress path. */
1298 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001299}
1300
Ionut-robert Aron70b32d82021-01-11 19:07:25 +02001301static int dpaa2_eth_set_rx_vlan_filtering(struct dpaa2_eth_priv *priv,
1302 bool enable)
1303{
1304 int err;
1305
1306 err = dpni_enable_vlan_filter(priv->mc_io, 0, priv->mc_token, enable);
1307
1308 if (err) {
1309 netdev_err(priv->net_dev,
1310 "dpni_enable_vlan_filter failed\n");
1311 return err;
1312 }
1313
1314 return 0;
1315}
1316
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001317static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001318{
1319 int err;
1320
1321 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1322 DPNI_OFF_RX_L3_CSUM, enable);
1323 if (err) {
1324 netdev_err(priv->net_dev,
1325 "dpni_set_offload(RX_L3_CSUM) failed\n");
1326 return err;
1327 }
1328
1329 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1330 DPNI_OFF_RX_L4_CSUM, enable);
1331 if (err) {
1332 netdev_err(priv->net_dev,
1333 "dpni_set_offload(RX_L4_CSUM) failed\n");
1334 return err;
1335 }
1336
1337 return 0;
1338}
1339
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001340static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001341{
1342 int err;
1343
1344 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1345 DPNI_OFF_TX_L3_CSUM, enable);
1346 if (err) {
1347 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1348 return err;
1349 }
1350
1351 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1352 DPNI_OFF_TX_L4_CSUM, enable);
1353 if (err) {
1354 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1355 return err;
1356 }
1357
1358 return 0;
1359}
1360
1361/* Perform a single release command to add buffers
1362 * to the specified buffer pool
1363 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001364static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1365 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001366{
1367 struct device *dev = priv->net_dev->dev.parent;
1368 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001369 struct page *page;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001370 dma_addr_t addr;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001371 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001372 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001373
1374 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1375 /* Allocate buffer visible to WRIOP + skb shared info +
1376 * alignment padding
1377 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001378 /* allocate one page for each Rx buffer. WRIOP sees
1379 * the entire page except for a tailroom reserved for
1380 * skb shared info
1381 */
1382 page = dev_alloc_pages(0);
1383 if (!page)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001384 goto err_alloc;
1385
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001386 addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001387 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001388 if (unlikely(dma_mapping_error(dev, addr)))
1389 goto err_map;
1390
1391 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -05001392
1393 /* tracing point */
1394 trace_dpaa2_eth_buf_seed(priv->net_dev,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001395 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001396 addr, priv->rx_buf_size,
Ioana Radulescu56361872017-04-28 04:50:32 -05001397 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001398 }
1399
1400release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001401 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001402 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001403 buf_array, i)) == -EBUSY) {
1404 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1405 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001406 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001407 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001408
1409 /* If release command failed, clean up and bail out;
1410 * not much else we can do about it
1411 */
1412 if (err) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001413 dpaa2_eth_free_bufs(priv, buf_array, i);
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001414 return 0;
1415 }
1416
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001417 return i;
1418
1419err_map:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001420 __free_pages(page, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001421err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001422 /* If we managed to allocate at least some buffers,
1423 * release them to hardware
1424 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001425 if (i)
1426 goto release_bufs;
1427
1428 return 0;
1429}
1430
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001431static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001432{
1433 int i, j;
1434 int new_count;
1435
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001436 for (j = 0; j < priv->num_channels; j++) {
1437 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1438 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001439 new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001440 priv->channel[j]->buf_count += new_count;
1441
1442 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001443 return -ENOMEM;
1444 }
1445 }
1446 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001447
1448 return 0;
1449}
1450
Jesse Brandeburgd0ea5cb2020-09-25 15:24:45 -07001451/*
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001452 * Drain the specified number of buffers from the DPNI's private buffer pool.
1453 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1454 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001455static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001456{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001457 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001458 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001459 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001460
1461 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001462 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001463 buf_array, count);
1464 if (ret < 0) {
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001465 if (ret == -EBUSY &&
Ioana Ciornei0e5ad752020-06-24 14:34:19 +03001466 retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001467 continue;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001468 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1469 return;
1470 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001471 dpaa2_eth_free_bufs(priv, buf_array, ret);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001472 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001473 } while (ret);
1474}
1475
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001476static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001477{
1478 int i;
1479
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001480 dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1481 dpaa2_eth_drain_bufs(priv, 1);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001482
1483 for (i = 0; i < priv->num_channels; i++)
1484 priv->channel[i]->buf_count = 0;
1485}
1486
1487/* Function is called from softirq context only, so we don't need to guard
1488 * the access to percpu count
1489 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001490static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1491 struct dpaa2_eth_channel *ch,
1492 u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001493{
1494 int new_count;
1495
1496 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1497 return 0;
1498
1499 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001500 new_count = dpaa2_eth_add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001501 if (unlikely(!new_count)) {
1502 /* Out of memory; abort for now, we'll try later on */
1503 break;
1504 }
1505 ch->buf_count += new_count;
1506 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1507
1508 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1509 return -ENOMEM;
1510
1511 return 0;
1512}
1513
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001514static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1515{
1516 struct dpaa2_eth_sgt_cache *sgt_cache;
1517 u16 count;
1518 int k, i;
1519
Ioana Ciornei0fe665d2020-07-06 17:55:54 +03001520 for_each_possible_cpu(k) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001521 sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1522 count = sgt_cache->count;
1523
1524 for (i = 0; i < count; i++)
1525 kfree(sgt_cache->buf[i]);
1526 sgt_cache->count = 0;
1527 }
1528}
1529
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001530static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001531{
1532 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001533 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001534
1535 /* Retry while portal is busy */
1536 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001537 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1538 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001539 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001540 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001541 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001542
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001543 ch->stats.dequeue_portal_busy += dequeues;
1544 if (unlikely(err))
1545 ch->stats.pull_err++;
1546
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001547 return err;
1548}
1549
1550/* NAPI poll routine
1551 *
1552 * Frames are dequeued from the QMan channel associated with this NAPI context.
1553 * Rx, Tx confirmation and (if configured) Rx error frames all count
1554 * towards the NAPI budget.
1555 */
1556static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1557{
1558 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001559 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001560 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001561 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1562 struct netdev_queue *nq;
1563 int store_cleaned, work_done;
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001564 struct list_head rx_list;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001565 int retries = 0;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001566 u16 flowid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001567 int err;
1568
1569 ch = container_of(napi, struct dpaa2_eth_channel, napi);
Ioana Radulescud678be12019-03-01 17:47:24 +00001570 ch->xdp.res = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001571 priv = ch->priv;
1572
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001573 INIT_LIST_HEAD(&rx_list);
1574 ch->rx_list = &rx_list;
1575
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001576 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001577 err = dpaa2_eth_pull_channel(ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001578 if (unlikely(err))
1579 break;
1580
1581 /* Refill pool if appropriate */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001582 dpaa2_eth_refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001583
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001584 store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001585 if (store_cleaned <= 0)
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001586 break;
1587 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001588 rx_cleaned += store_cleaned;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001589 flowid = fq->flowid;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001590 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001591 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001592 /* We have a single Tx conf FQ on this channel */
1593 txc_fq = fq;
1594 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001595
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001596 /* If we either consumed the whole NAPI budget with Rx frames
1597 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001598 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001599 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001600 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1601 work_done = budget;
1602 goto out;
1603 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001604 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001605
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001606 /* We didn't consume the entire budget, so finish napi and
1607 * re-enable data availability notifications
1608 */
1609 napi_complete_done(napi, rx_cleaned);
1610 do {
1611 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1612 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001613 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001614 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1615 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001616
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001617 work_done = max(rx_cleaned, 1);
1618
1619out:
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001620 netif_receive_skb_list(ch->rx_list);
1621
Ioana Radulescud678be12019-03-01 17:47:24 +00001622 if (txc_fq && txc_fq->dq_frames) {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001623 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1624 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1625 txc_fq->dq_bytes);
1626 txc_fq->dq_frames = 0;
1627 txc_fq->dq_bytes = 0;
1628 }
1629
Ioana Radulescud678be12019-03-01 17:47:24 +00001630 if (ch->xdp.res & XDP_REDIRECT)
1631 xdp_do_flush_map();
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001632 else if (rx_cleaned && ch->xdp.res & XDP_TX)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001633 dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
Ioana Radulescud678be12019-03-01 17:47:24 +00001634
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001635 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001636}
1637
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001638static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001639{
1640 struct dpaa2_eth_channel *ch;
1641 int i;
1642
1643 for (i = 0; i < priv->num_channels; i++) {
1644 ch = priv->channel[i];
1645 napi_enable(&ch->napi);
1646 }
1647}
1648
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001649static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001650{
1651 struct dpaa2_eth_channel *ch;
1652 int i;
1653
1654 for (i = 0; i < priv->num_channels; i++) {
1655 ch = priv->channel[i];
1656 napi_disable(&ch->napi);
1657 }
1658}
1659
Ioana Ciornei07beb162020-05-31 00:08:14 +03001660void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1661 bool tx_pause, bool pfc)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001662{
1663 struct dpni_taildrop td = {0};
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001664 struct dpaa2_eth_fq *fq;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001665 int i, err;
1666
Ioana Ciornei07beb162020-05-31 00:08:14 +03001667 /* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1668 * flow control is disabled (as it might interfere with either the
1669 * buffer pool depletion trigger for pause frames or with the group
1670 * congestion trigger for PFC frames)
1671 */
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001672 td.enable = !tx_pause;
Ioana Ciornei07beb162020-05-31 00:08:14 +03001673 if (priv->rx_fqtd_enabled == td.enable)
1674 goto set_cgtd;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001675
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001676 td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1677 td.units = DPNI_CONGESTION_UNIT_BYTES;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001678
1679 for (i = 0; i < priv->num_fqs; i++) {
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001680 fq = &priv->fq[i];
1681 if (fq->type != DPAA2_RX_FQ)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001682 continue;
1683 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001684 DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1685 fq->tc, fq->flowid, &td);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001686 if (err) {
1687 netdev_err(priv->net_dev,
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001688 "dpni_set_taildrop(FQ) failed\n");
1689 return;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001690 }
1691 }
1692
Ioana Ciornei07beb162020-05-31 00:08:14 +03001693 priv->rx_fqtd_enabled = td.enable;
1694
1695set_cgtd:
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001696 /* Congestion group taildrop: threshold is in frames, per group
1697 * of FQs belonging to the same traffic class
Ioana Ciornei07beb162020-05-31 00:08:14 +03001698 * Enabled if general Tx pause disabled or if PFCs are enabled
1699 * (congestion group threhsold for PFC generation is lower than the
1700 * CG taildrop threshold, so it won't interfere with it; we also
1701 * want frames in non-PFC enabled traffic classes to be kept in check)
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001702 */
Jiapeng Chongb91b3a22021-02-02 18:02:37 +08001703 td.enable = !tx_pause || pfc;
Ioana Ciornei07beb162020-05-31 00:08:14 +03001704 if (priv->rx_cgtd_enabled == td.enable)
1705 return;
1706
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001707 td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
1708 td.units = DPNI_CONGESTION_UNIT_FRAMES;
1709 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
1710 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1711 DPNI_CP_GROUP, DPNI_QUEUE_RX,
1712 i, 0, &td);
1713 if (err) {
1714 netdev_err(priv->net_dev,
1715 "dpni_set_taildrop(CG) failed\n");
1716 return;
1717 }
1718 }
1719
Ioana Ciornei07beb162020-05-31 00:08:14 +03001720 priv->rx_cgtd_enabled = td.enable;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001721}
1722
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001723static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001724{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001725 struct dpni_link_state state = {0};
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001726 bool tx_pause;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001727 int err;
1728
1729 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1730 if (unlikely(err)) {
1731 netdev_err(priv->net_dev,
1732 "dpni_get_link_state() failed\n");
1733 return err;
1734 }
1735
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001736 /* If Tx pause frame settings have changed, we need to update
1737 * Rx FQ taildrop configuration as well. We configure taildrop
1738 * only when pause frame generation is disabled.
1739 */
Ioana Radulescuad054f22020-05-31 00:08:10 +03001740 tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
Ioana Ciornei07beb162020-05-31 00:08:14 +03001741 dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001742
Ioana Ciornei71947922019-10-31 01:18:31 +02001743 /* When we manage the MAC/PHY using phylink there is no need
1744 * to manually update the netif_carrier.
1745 */
Ioana Ciorneid87e6062021-01-08 11:07:23 +02001746 if (dpaa2_eth_is_type_phy(priv))
Ioana Ciornei71947922019-10-31 01:18:31 +02001747 goto out;
1748
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001749 /* Chech link state; speed / duplex changes are not treated yet */
1750 if (priv->link_state.up == state.up)
Ioana Radulescucce629432019-08-28 17:08:14 +03001751 goto out;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001752
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001753 if (state.up) {
1754 netif_carrier_on(priv->net_dev);
1755 netif_tx_start_all_queues(priv->net_dev);
1756 } else {
1757 netif_tx_stop_all_queues(priv->net_dev);
1758 netif_carrier_off(priv->net_dev);
1759 }
1760
Ioana Radulescu77160af2017-06-06 10:00:28 -05001761 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001762 state.up ? "up" : "down");
1763
Ioana Radulescucce629432019-08-28 17:08:14 +03001764out:
1765 priv->link_state = state;
1766
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001767 return 0;
1768}
1769
1770static int dpaa2_eth_open(struct net_device *net_dev)
1771{
1772 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1773 int err;
1774
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001775 err = dpaa2_eth_seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001776 if (err) {
1777 /* Not much to do; the buffer pool, though not filled up,
1778 * may still contain some buffers which would enable us
1779 * to limp on.
1780 */
1781 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001782 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001783 }
1784
Ioana Ciorneid87e6062021-01-08 11:07:23 +02001785 if (!dpaa2_eth_is_type_phy(priv)) {
Ioana Ciornei71947922019-10-31 01:18:31 +02001786 /* We'll only start the txqs when the link is actually ready;
1787 * make sure we don't race against the link up notification,
1788 * which may come immediately after dpni_enable();
1789 */
1790 netif_tx_stop_all_queues(net_dev);
1791
1792 /* Also, explicitly set carrier off, otherwise
1793 * netif_carrier_ok() will return true and cause 'ip link show'
1794 * to report the LOWER_UP flag, even though the link
1795 * notification wasn't even received.
1796 */
1797 netif_carrier_off(net_dev);
1798 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001799 dpaa2_eth_enable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001800
1801 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1802 if (err < 0) {
1803 netdev_err(net_dev, "dpni_enable() failed\n");
1804 goto enable_err;
1805 }
1806
Ioana Ciorneid87e6062021-01-08 11:07:23 +02001807 if (dpaa2_eth_is_type_phy(priv))
Ioana Ciornei71947922019-10-31 01:18:31 +02001808 phylink_start(priv->mac->phylink);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001809
1810 return 0;
1811
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001812enable_err:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001813 dpaa2_eth_disable_ch_napi(priv);
1814 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001815 return err;
1816}
1817
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001818/* Total number of in-flight frames on ingress queues */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001819static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001820{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001821 struct dpaa2_eth_fq *fq;
1822 u32 fcnt = 0, bcnt = 0, total = 0;
1823 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001824
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001825 for (i = 0; i < priv->num_fqs; i++) {
1826 fq = &priv->fq[i];
1827 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1828 if (err) {
1829 netdev_warn(priv->net_dev, "query_fq_count failed");
1830 break;
1831 }
1832 total += fcnt;
1833 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001834
1835 return total;
1836}
1837
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001838static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001839{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001840 int retries = 10;
1841 u32 pending;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001842
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001843 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001844 pending = dpaa2_eth_ingress_fq_count(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001845 if (pending)
1846 msleep(100);
1847 } while (pending && --retries);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001848}
1849
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001850#define DPNI_TX_PENDING_VER_MAJOR 7
1851#define DPNI_TX_PENDING_VER_MINOR 13
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001852static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001853{
1854 union dpni_statistics stats;
1855 int retries = 10;
1856 int err;
1857
1858 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1859 DPNI_TX_PENDING_VER_MINOR) < 0)
1860 goto out;
1861
1862 do {
1863 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1864 &stats);
1865 if (err)
1866 goto out;
1867 if (stats.page_6.tx_pending_frames == 0)
1868 return;
1869 } while (--retries);
1870
1871out:
1872 msleep(500);
1873}
1874
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001875static int dpaa2_eth_stop(struct net_device *net_dev)
1876{
1877 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001878 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001879 int retries = 10;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001880
Ioana Ciorneid87e6062021-01-08 11:07:23 +02001881 if (dpaa2_eth_is_type_phy(priv)) {
1882 phylink_stop(priv->mac->phylink);
1883 } else {
Ioana Ciornei71947922019-10-31 01:18:31 +02001884 netif_tx_stop_all_queues(net_dev);
1885 netif_carrier_off(net_dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02001886 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001887
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001888 /* On dpni_disable(), the MC firmware will:
1889 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1890 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1891 * of all in flight Tx frames is finished (and corresponding Tx conf
1892 * frames are enqueued back to software)
1893 *
1894 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1895 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1896 * and Tx conf queues are consumed on NAPI poll.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001897 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001898 dpaa2_eth_wait_for_egress_fq_empty(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001899
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001900 do {
1901 dpni_disable(priv->mc_io, 0, priv->mc_token);
1902 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1903 if (dpni_enabled)
1904 /* Allow the hardware some slack */
1905 msleep(100);
1906 } while (dpni_enabled && --retries);
1907 if (!retries) {
1908 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1909 /* Must go on and disable NAPI nonetheless, so we don't crash at
1910 * the next "ifconfig up"
1911 */
1912 }
1913
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001914 dpaa2_eth_wait_for_ingress_fq_empty(priv);
1915 dpaa2_eth_disable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001916
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001917 /* Empty the buffer pool */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001918 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001919
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001920 /* Empty the Scatter-Gather Buffer cache */
1921 dpaa2_eth_sgt_cache_drain(priv);
1922
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001923 return 0;
1924}
1925
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001926static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1927{
1928 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1929 struct device *dev = net_dev->dev.parent;
1930 int err;
1931
1932 err = eth_mac_addr(net_dev, addr);
1933 if (err < 0) {
1934 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1935 return err;
1936 }
1937
1938 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1939 net_dev->dev_addr);
1940 if (err) {
1941 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1942 return err;
1943 }
1944
1945 return 0;
1946}
1947
1948/** Fill in counters maintained by the GPP driver. These may be different from
1949 * the hardware counters obtained by ethtool.
1950 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001951static void dpaa2_eth_get_stats(struct net_device *net_dev,
1952 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001953{
1954 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1955 struct rtnl_link_stats64 *percpu_stats;
1956 u64 *cpustats;
1957 u64 *netstats = (u64 *)stats;
1958 int i, j;
1959 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1960
1961 for_each_possible_cpu(i) {
1962 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1963 cpustats = (u64 *)percpu_stats;
1964 for (j = 0; j < num; j++)
1965 netstats[j] += cpustats[j];
1966 }
1967}
1968
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001969/* Copy mac unicast addresses from @net_dev to @priv.
1970 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1971 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001972static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
1973 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001974{
1975 struct netdev_hw_addr *ha;
1976 int err;
1977
1978 netdev_for_each_uc_addr(ha, net_dev) {
1979 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1980 ha->addr);
1981 if (err)
1982 netdev_warn(priv->net_dev,
1983 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1984 ha->addr, err);
1985 }
1986}
1987
1988/* Copy mac multicast addresses from @net_dev to @priv
1989 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1990 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001991static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
1992 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001993{
1994 struct netdev_hw_addr *ha;
1995 int err;
1996
1997 netdev_for_each_mc_addr(ha, net_dev) {
1998 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1999 ha->addr);
2000 if (err)
2001 netdev_warn(priv->net_dev,
2002 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
2003 ha->addr, err);
2004 }
2005}
2006
Ionut-robert Aron70b32d82021-01-11 19:07:25 +02002007static int dpaa2_eth_rx_add_vid(struct net_device *net_dev,
2008 __be16 vlan_proto, u16 vid)
2009{
2010 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2011 int err;
2012
2013 err = dpni_add_vlan_id(priv->mc_io, 0, priv->mc_token,
2014 vid, 0, 0, 0);
2015
2016 if (err) {
2017 netdev_warn(priv->net_dev,
2018 "Could not add the vlan id %u\n",
2019 vid);
2020 return err;
2021 }
2022
2023 return 0;
2024}
2025
2026static int dpaa2_eth_rx_kill_vid(struct net_device *net_dev,
2027 __be16 vlan_proto, u16 vid)
2028{
2029 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2030 int err;
2031
2032 err = dpni_remove_vlan_id(priv->mc_io, 0, priv->mc_token, vid);
2033
2034 if (err) {
2035 netdev_warn(priv->net_dev,
2036 "Could not remove the vlan id %u\n",
2037 vid);
2038 return err;
2039 }
2040
2041 return 0;
2042}
2043
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002044static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
2045{
2046 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2047 int uc_count = netdev_uc_count(net_dev);
2048 int mc_count = netdev_mc_count(net_dev);
2049 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
2050 u32 options = priv->dpni_attrs.options;
2051 u16 mc_token = priv->mc_token;
2052 struct fsl_mc_io *mc_io = priv->mc_io;
2053 int err;
2054
2055 /* Basic sanity checks; these probably indicate a misconfiguration */
2056 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
2057 netdev_info(net_dev,
2058 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
2059 max_mac);
2060
2061 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
2062 if (uc_count > max_mac) {
2063 netdev_info(net_dev,
2064 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
2065 uc_count, max_mac);
2066 goto force_promisc;
2067 }
2068 if (mc_count + uc_count > max_mac) {
2069 netdev_info(net_dev,
2070 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
2071 uc_count + mc_count, max_mac);
2072 goto force_mc_promisc;
2073 }
2074
2075 /* Adjust promisc settings due to flag combinations */
2076 if (net_dev->flags & IFF_PROMISC)
2077 goto force_promisc;
2078 if (net_dev->flags & IFF_ALLMULTI) {
2079 /* First, rebuild unicast filtering table. This should be done
2080 * in promisc mode, in order to avoid frame loss while we
2081 * progressively add entries to the table.
2082 * We don't know whether we had been in promisc already, and
2083 * making an MC call to find out is expensive; so set uc promisc
2084 * nonetheless.
2085 */
2086 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2087 if (err)
2088 netdev_warn(net_dev, "Can't set uc promisc\n");
2089
2090 /* Actual uc table reconstruction. */
2091 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
2092 if (err)
2093 netdev_warn(net_dev, "Can't clear uc filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002094 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002095
2096 /* Finally, clear uc promisc and set mc promisc as requested. */
2097 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2098 if (err)
2099 netdev_warn(net_dev, "Can't clear uc promisc\n");
2100 goto force_mc_promisc;
2101 }
2102
2103 /* Neither unicast, nor multicast promisc will be on... eventually.
2104 * For now, rebuild mac filtering tables while forcing both of them on.
2105 */
2106 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2107 if (err)
2108 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
2109 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2110 if (err)
2111 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
2112
2113 /* Actual mac filtering tables reconstruction */
2114 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
2115 if (err)
2116 netdev_warn(net_dev, "Can't clear mac filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002117 dpaa2_eth_add_mc_hw_addr(net_dev, priv);
2118 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002119
2120 /* Now we can clear both ucast and mcast promisc, without risking
2121 * to drop legitimate frames anymore.
2122 */
2123 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2124 if (err)
2125 netdev_warn(net_dev, "Can't clear ucast promisc\n");
2126 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
2127 if (err)
2128 netdev_warn(net_dev, "Can't clear mcast promisc\n");
2129
2130 return;
2131
2132force_promisc:
2133 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2134 if (err)
2135 netdev_warn(net_dev, "Can't set ucast promisc\n");
2136force_mc_promisc:
2137 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2138 if (err)
2139 netdev_warn(net_dev, "Can't set mcast promisc\n");
2140}
2141
2142static int dpaa2_eth_set_features(struct net_device *net_dev,
2143 netdev_features_t features)
2144{
2145 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2146 netdev_features_t changed = features ^ net_dev->features;
2147 bool enable;
2148 int err;
2149
Ionut-robert Aron70b32d82021-01-11 19:07:25 +02002150 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
2151 enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER);
2152 err = dpaa2_eth_set_rx_vlan_filtering(priv, enable);
2153 if (err)
2154 return err;
2155 }
2156
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002157 if (changed & NETIF_F_RXCSUM) {
2158 enable = !!(features & NETIF_F_RXCSUM);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002159 err = dpaa2_eth_set_rx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002160 if (err)
2161 return err;
2162 }
2163
2164 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2165 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002166 err = dpaa2_eth_set_tx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002167 if (err)
2168 return err;
2169 }
2170
2171 return 0;
2172}
2173
Ioana Radulescu859f9982018-04-26 18:23:47 +08002174static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2175{
2176 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2177 struct hwtstamp_config config;
2178
Yangbo Luc5521182020-09-18 17:08:02 +08002179 if (!dpaa2_ptp)
2180 return -EINVAL;
2181
Ioana Radulescu859f9982018-04-26 18:23:47 +08002182 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
2183 return -EFAULT;
2184
2185 switch (config.tx_type) {
2186 case HWTSTAMP_TX_OFF:
Ioana Radulescu859f9982018-04-26 18:23:47 +08002187 case HWTSTAMP_TX_ON:
Yangbo Luc5521182020-09-18 17:08:02 +08002188 case HWTSTAMP_TX_ONESTEP_SYNC:
Yangbo Lu1cf773b2020-09-18 17:08:01 +08002189 priv->tx_tstamp_type = config.tx_type;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002190 break;
2191 default:
2192 return -ERANGE;
2193 }
2194
2195 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
2196 priv->rx_tstamp = false;
2197 } else {
2198 priv->rx_tstamp = true;
2199 /* TS is set for all frame types, not only those requested */
2200 config.rx_filter = HWTSTAMP_FILTER_ALL;
2201 }
2202
2203 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
2204 -EFAULT : 0;
2205}
2206
2207static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2208{
Russell King4a841822020-02-27 12:00:21 +00002209 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2210
Ioana Radulescu859f9982018-04-26 18:23:47 +08002211 if (cmd == SIOCSHWTSTAMP)
2212 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
2213
Ioana Ciorneid87e6062021-01-08 11:07:23 +02002214 if (dpaa2_eth_is_type_phy(priv))
Russell King4a841822020-02-27 12:00:21 +00002215 return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2216
2217 return -EOPNOTSUPP;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002218}
2219
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002220static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2221{
2222 int mfl, linear_mfl;
2223
2224 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03002225 linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002226 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002227
2228 if (mfl > linear_mfl) {
2229 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2230 linear_mfl - VLAN_ETH_HLEN);
2231 return false;
2232 }
2233
2234 return true;
2235}
2236
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002237static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002238{
2239 int mfl, err;
2240
2241 /* We enforce a maximum Rx frame length based on MTU only if we have
2242 * an XDP program attached (in order to avoid Rx S/G frames).
2243 * Otherwise, we accept all incoming frames as long as they are not
2244 * larger than maximum size supported in hardware
2245 */
2246 if (has_xdp)
2247 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2248 else
2249 mfl = DPAA2_ETH_MFL;
2250
2251 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2252 if (err) {
2253 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2254 return err;
2255 }
2256
2257 return 0;
2258}
2259
2260static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2261{
2262 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2263 int err;
2264
2265 if (!priv->xdp_prog)
2266 goto out;
2267
2268 if (!xdp_mtu_valid(priv, new_mtu))
2269 return -EINVAL;
2270
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002271 err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002272 if (err)
2273 return err;
2274
2275out:
2276 dev->mtu = new_mtu;
2277 return 0;
2278}
2279
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002280static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002281{
2282 struct dpni_buffer_layout buf_layout = {0};
2283 int err;
2284
2285 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2286 DPNI_QUEUE_RX, &buf_layout);
2287 if (err) {
2288 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2289 return err;
2290 }
2291
2292 /* Reserve extra headroom for XDP header size changes */
2293 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2294 (has_xdp ? XDP_PACKET_HEADROOM : 0);
2295 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2296 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2297 DPNI_QUEUE_RX, &buf_layout);
2298 if (err) {
2299 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2300 return err;
2301 }
2302
2303 return 0;
2304}
2305
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002306static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002307{
2308 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2309 struct dpaa2_eth_channel *ch;
2310 struct bpf_prog *old;
2311 bool up, need_update;
2312 int i, err;
2313
2314 if (prog && !xdp_mtu_valid(priv, dev->mtu))
2315 return -EINVAL;
2316
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002317 if (prog)
2318 bpf_prog_add(prog, priv->num_channels);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002319
2320 up = netif_running(dev);
2321 need_update = (!!priv->xdp_prog != !!prog);
2322
2323 if (up)
2324 dpaa2_eth_stop(dev);
2325
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002326 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2327 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2328 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2329 * so we are sure no old format buffers will be used from now on.
2330 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002331 if (need_update) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002332 err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002333 if (err)
2334 goto out_err;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002335 err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002336 if (err)
2337 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002338 }
2339
2340 old = xchg(&priv->xdp_prog, prog);
2341 if (old)
2342 bpf_prog_put(old);
2343
2344 for (i = 0; i < priv->num_channels; i++) {
2345 ch = priv->channel[i];
2346 old = xchg(&ch->xdp.prog, prog);
2347 if (old)
2348 bpf_prog_put(old);
2349 }
2350
2351 if (up) {
2352 err = dpaa2_eth_open(dev);
2353 if (err)
2354 return err;
2355 }
2356
2357 return 0;
2358
2359out_err:
2360 if (prog)
2361 bpf_prog_sub(prog, priv->num_channels);
2362 if (up)
2363 dpaa2_eth_open(dev);
2364
2365 return err;
2366}
2367
2368static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2369{
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002370 switch (xdp->command) {
2371 case XDP_SETUP_PROG:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002372 return dpaa2_eth_setup_xdp(dev, xdp->prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002373 default:
2374 return -EINVAL;
2375 }
2376
2377 return 0;
2378}
2379
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002380static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2381 struct xdp_frame *xdpf,
2382 struct dpaa2_fd *fd)
Ioana Radulescud678be12019-03-01 17:47:24 +00002383{
Ioana Radulescud678be12019-03-01 17:47:24 +00002384 struct device *dev = net_dev->dev.parent;
Ioana Radulescud678be12019-03-01 17:47:24 +00002385 unsigned int needed_headroom;
2386 struct dpaa2_eth_swa *swa;
Ioana Radulescud678be12019-03-01 17:47:24 +00002387 void *buffer_start, *aligned_start;
2388 dma_addr_t addr;
Ioana Radulescud678be12019-03-01 17:47:24 +00002389
2390 /* We require a minimum headroom to be able to transmit the frame.
2391 * Otherwise return an error and let the original net_device handle it
2392 */
Yangbo Lu1cf773b2020-09-18 17:08:01 +08002393 needed_headroom = dpaa2_eth_needed_headroom(NULL);
Ioana Radulescud678be12019-03-01 17:47:24 +00002394 if (xdpf->headroom < needed_headroom)
2395 return -EINVAL;
2396
Ioana Radulescud678be12019-03-01 17:47:24 +00002397 /* Setup the FD fields */
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002398 memset(fd, 0, sizeof(*fd));
Ioana Radulescud678be12019-03-01 17:47:24 +00002399
2400 /* Align FD address, if possible */
2401 buffer_start = xdpf->data - needed_headroom;
2402 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2403 DPAA2_ETH_TX_BUF_ALIGN);
2404 if (aligned_start >= xdpf->data - xdpf->headroom)
2405 buffer_start = aligned_start;
2406
2407 swa = (struct dpaa2_eth_swa *)buffer_start;
2408 /* fill in necessary fields here */
2409 swa->type = DPAA2_ETH_SWA_XDP;
2410 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2411 swa->xdp.xdpf = xdpf;
2412
2413 addr = dma_map_single(dev, buffer_start,
2414 swa->xdp.dma_size,
2415 DMA_BIDIRECTIONAL);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002416 if (unlikely(dma_mapping_error(dev, addr)))
Ioana Radulescud678be12019-03-01 17:47:24 +00002417 return -ENOMEM;
Ioana Radulescud678be12019-03-01 17:47:24 +00002418
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002419 dpaa2_fd_set_addr(fd, addr);
2420 dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2421 dpaa2_fd_set_len(fd, xdpf->len);
2422 dpaa2_fd_set_format(fd, dpaa2_fd_single);
2423 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescud678be12019-03-01 17:47:24 +00002424
2425 return 0;
2426}
2427
2428static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2429 struct xdp_frame **frames, u32 flags)
2430{
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002431 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002432 struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002433 struct rtnl_link_stats64 *percpu_stats;
2434 struct dpaa2_eth_fq *fq;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002435 struct dpaa2_fd *fds;
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002436 int enqueued, i, err;
Ioana Radulescud678be12019-03-01 17:47:24 +00002437
2438 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2439 return -EINVAL;
2440
2441 if (!netif_running(net_dev))
2442 return -ENETDOWN;
2443
Ioana Ciornei8665d972020-04-22 15:05:13 +03002444 fq = &priv->fq[smp_processor_id()];
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002445 xdp_redirect_fds = &fq->xdp_redirect_fds;
2446 fds = xdp_redirect_fds->fds;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002447
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002448 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002449
Ioana Ciornei8665d972020-04-22 15:05:13 +03002450 /* create a FD for each xdp_frame in the list received */
Ioana Radulescud678be12019-03-01 17:47:24 +00002451 for (i = 0; i < n; i++) {
Ioana Ciornei8665d972020-04-22 15:05:13 +03002452 err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2453 if (err)
2454 break;
2455 }
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002456 xdp_redirect_fds->num = i;
Ioana Radulescud678be12019-03-01 17:47:24 +00002457
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002458 /* enqueue all the frame descriptors */
2459 enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
Ioana Radulescud678be12019-03-01 17:47:24 +00002460
Ioana Ciornei8665d972020-04-22 15:05:13 +03002461 /* update statistics */
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002462 percpu_stats->tx_packets += enqueued;
2463 for (i = 0; i < enqueued; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002464 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
Ioana Ciornei8665d972020-04-22 15:05:13 +03002465
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002466 return enqueued;
Ioana Radulescud678be12019-03-01 17:47:24 +00002467}
2468
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002469static int update_xps(struct dpaa2_eth_priv *priv)
2470{
2471 struct net_device *net_dev = priv->net_dev;
2472 struct cpumask xps_mask;
2473 struct dpaa2_eth_fq *fq;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002474 int i, num_queues, netdev_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002475 int err = 0;
2476
2477 num_queues = dpaa2_eth_queue_count(priv);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002478 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002479
2480 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2481 * queues, so only process those
2482 */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002483 for (i = 0; i < netdev_queues; i++) {
2484 fq = &priv->fq[i % num_queues];
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002485
2486 cpumask_clear(&xps_mask);
2487 cpumask_set_cpu(fq->target_cpu, &xps_mask);
2488
2489 err = netif_set_xps_queue(net_dev, &xps_mask, i);
2490 if (err) {
2491 netdev_warn_once(net_dev, "Error setting XPS queue\n");
2492 break;
2493 }
2494 }
2495
2496 return err;
2497}
2498
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002499static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2500 struct tc_mqprio_qopt *mqprio)
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002501{
2502 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002503 u8 num_tc, num_queues;
2504 int i;
2505
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002506 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2507 num_queues = dpaa2_eth_queue_count(priv);
2508 num_tc = mqprio->num_tc;
2509
2510 if (num_tc == net_dev->num_tc)
2511 return 0;
2512
2513 if (num_tc > dpaa2_eth_tc_count(priv)) {
2514 netdev_err(net_dev, "Max %d traffic classes supported\n",
2515 dpaa2_eth_tc_count(priv));
Jesper Dangaard Brouerb89c1e62020-04-23 16:57:50 +02002516 return -EOPNOTSUPP;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002517 }
2518
2519 if (!num_tc) {
2520 netdev_reset_tc(net_dev);
2521 netif_set_real_num_tx_queues(net_dev, num_queues);
2522 goto out;
2523 }
2524
2525 netdev_set_num_tc(net_dev, num_tc);
2526 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2527
2528 for (i = 0; i < num_tc; i++)
2529 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2530
2531out:
2532 update_xps(priv);
2533
2534 return 0;
2535}
2536
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002537#define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2538
2539static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2540{
2541 struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2542 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2543 struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2544 struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2545 int err;
2546
2547 if (p->command == TC_TBF_STATS)
2548 return -EOPNOTSUPP;
2549
2550 /* Only per port Tx shaping */
2551 if (p->parent != TC_H_ROOT)
2552 return -EOPNOTSUPP;
2553
2554 if (p->command == TC_TBF_REPLACE) {
2555 if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2556 netdev_err(net_dev, "burst size cannot be greater than %d\n",
2557 DPAA2_ETH_MAX_BURST_SIZE);
2558 return -EINVAL;
2559 }
2560
2561 tx_cr_shaper.max_burst_size = cfg->max_size;
2562 /* The TBF interface is in bytes/s, whereas DPAA2 expects the
2563 * rate in Mbits/s
2564 */
2565 tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2566 }
2567
2568 err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2569 &tx_er_shaper, 0);
2570 if (err) {
2571 netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2572 return err;
2573 }
2574
2575 return 0;
2576}
2577
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002578static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2579 enum tc_setup_type type, void *type_data)
2580{
2581 switch (type) {
2582 case TC_SETUP_QDISC_MQPRIO:
2583 return dpaa2_eth_setup_mqprio(net_dev, type_data);
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002584 case TC_SETUP_QDISC_TBF:
2585 return dpaa2_eth_setup_tbf(net_dev, type_data);
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002586 default:
2587 return -EOPNOTSUPP;
2588 }
2589}
2590
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002591static const struct net_device_ops dpaa2_eth_ops = {
2592 .ndo_open = dpaa2_eth_open,
2593 .ndo_start_xmit = dpaa2_eth_tx,
2594 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002595 .ndo_set_mac_address = dpaa2_eth_set_addr,
2596 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002597 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2598 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08002599 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002600 .ndo_change_mtu = dpaa2_eth_change_mtu,
2601 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescud678be12019-03-01 17:47:24 +00002602 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002603 .ndo_setup_tc = dpaa2_eth_setup_tc,
Ionut-robert Aron70b32d82021-01-11 19:07:25 +02002604 .ndo_vlan_rx_add_vid = dpaa2_eth_rx_add_vid,
2605 .ndo_vlan_rx_kill_vid = dpaa2_eth_rx_kill_vid
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002606};
2607
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002608static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002609{
2610 struct dpaa2_eth_channel *ch;
2611
2612 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002613
2614 /* Update NAPI statistics */
2615 ch->stats.cdan++;
2616
Jiafei Pan6c33ae12020-08-03 23:10:08 +03002617 napi_schedule(&ch->napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002618}
2619
2620/* Allocate and configure a DPCON object */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002621static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002622{
2623 struct fsl_mc_device *dpcon;
2624 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002625 int err;
2626
2627 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2628 FSL_MC_POOL_DPCON, &dpcon);
2629 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002630 if (err == -ENXIO)
2631 err = -EPROBE_DEFER;
2632 else
2633 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2634 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002635 }
2636
2637 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2638 if (err) {
2639 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002640 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002641 }
2642
2643 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2644 if (err) {
2645 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002646 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002647 }
2648
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002649 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2650 if (err) {
2651 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002652 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002653 }
2654
2655 return dpcon;
2656
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002657close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002658 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002659free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002660 fsl_mc_object_free(dpcon);
2661
YueHaibing02afa9c2020-08-04 21:26:43 +08002662 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002663}
2664
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002665static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
2666 struct fsl_mc_device *dpcon)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002667{
2668 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2669 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2670 fsl_mc_object_free(dpcon);
2671}
2672
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002673static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002674{
2675 struct dpaa2_eth_channel *channel;
2676 struct dpcon_attr attr;
2677 struct device *dev = priv->net_dev->dev.parent;
2678 int err;
2679
2680 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2681 if (!channel)
2682 return NULL;
2683
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002684 channel->dpcon = dpaa2_eth_setup_dpcon(priv);
YueHaibing02afa9c2020-08-04 21:26:43 +08002685 if (IS_ERR(channel->dpcon)) {
2686 err = PTR_ERR(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002687 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002688 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002689
2690 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2691 &attr);
2692 if (err) {
2693 dev_err(dev, "dpcon_get_attributes() failed\n");
2694 goto err_get_attr;
2695 }
2696
2697 channel->dpcon_id = attr.id;
2698 channel->ch_id = attr.qbman_ch_id;
2699 channel->priv = priv;
2700
2701 return channel;
2702
2703err_get_attr:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002704 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002705err_setup:
2706 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002707 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002708}
2709
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002710static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
2711 struct dpaa2_eth_channel *channel)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002712{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002713 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002714 kfree(channel);
2715}
2716
2717/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2718 * and register data availability notifications
2719 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002720static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002721{
2722 struct dpaa2_io_notification_ctx *nctx;
2723 struct dpaa2_eth_channel *channel;
2724 struct dpcon_notification_cfg dpcon_notif_cfg;
2725 struct device *dev = priv->net_dev->dev.parent;
2726 int i, err;
2727
2728 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2729 * many cores as possible, so we need one channel for each core
2730 * (unless there's fewer queues than cores, in which case the extra
2731 * channels would be wasted).
2732 * Allocate one channel per core and register it to the core's
2733 * affine DPIO. If not enough channels are available for all cores
2734 * or if some cores don't have an affine DPIO, there will be no
2735 * ingress frame processing on those cores.
2736 */
2737 cpumask_clear(&priv->dpio_cpumask);
2738 for_each_online_cpu(i) {
2739 /* Try to allocate a channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002740 channel = dpaa2_eth_alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002741 if (IS_ERR_OR_NULL(channel)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002742 err = PTR_ERR_OR_ZERO(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002743 if (err != -EPROBE_DEFER)
2744 dev_info(dev,
2745 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002746 goto err_alloc_ch;
2747 }
2748
2749 priv->channel[priv->num_channels] = channel;
2750
2751 nctx = &channel->nctx;
2752 nctx->is_cdan = 1;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002753 nctx->cb = dpaa2_eth_cdan_cb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002754 nctx->id = channel->ch_id;
2755 nctx->desired_cpu = i;
2756
2757 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06002758 channel->dpio = dpaa2_io_service_select(i);
Ioana Ciornei47441f72018-12-10 16:50:19 +00002759 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002760 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002761 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002762 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002763 * none available for next cores either. Signal we want
2764 * to retry later, in case the DPIO devices weren't
2765 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002766 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002767 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002768 goto err_service_reg;
2769 }
2770
2771 /* Register DPCON notification with MC */
2772 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2773 dpcon_notif_cfg.priority = 0;
2774 dpcon_notif_cfg.user_ctx = nctx->qman64;
2775 err = dpcon_set_notification(priv->mc_io, 0,
2776 channel->dpcon->mc_handle,
2777 &dpcon_notif_cfg);
2778 if (err) {
2779 dev_err(dev, "dpcon_set_notification failed()\n");
2780 goto err_set_cdan;
2781 }
2782
2783 /* If we managed to allocate a channel and also found an affine
2784 * DPIO for this core, add it to the final mask
2785 */
2786 cpumask_set_cpu(i, &priv->dpio_cpumask);
2787 priv->num_channels++;
2788
2789 /* Stop if we already have enough channels to accommodate all
2790 * RX and TX conf queues
2791 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00002792 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002793 break;
2794 }
2795
2796 return 0;
2797
2798err_set_cdan:
Ioana Ciornei47441f72018-12-10 16:50:19 +00002799 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002800err_service_reg:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002801 dpaa2_eth_free_channel(priv, channel);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002802err_alloc_ch:
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002803 if (err == -EPROBE_DEFER) {
2804 for (i = 0; i < priv->num_channels; i++) {
2805 channel = priv->channel[i];
2806 nctx = &channel->nctx;
2807 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002808 dpaa2_eth_free_channel(priv, channel);
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002809 }
2810 priv->num_channels = 0;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002811 return err;
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002812 }
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002813
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002814 if (cpumask_empty(&priv->dpio_cpumask)) {
2815 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002816 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002817 }
2818
2819 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2820 cpumask_pr_args(&priv->dpio_cpumask));
2821
2822 return 0;
2823}
2824
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002825static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002826{
Ioana Ciornei47441f72018-12-10 16:50:19 +00002827 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002828 struct dpaa2_eth_channel *ch;
Ioana Ciornei47441f72018-12-10 16:50:19 +00002829 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002830
2831 /* deregister CDAN notifications and free channels */
2832 for (i = 0; i < priv->num_channels; i++) {
2833 ch = priv->channel[i];
Ioana Ciornei47441f72018-12-10 16:50:19 +00002834 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002835 dpaa2_eth_free_channel(priv, ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002836 }
2837}
2838
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002839static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
2840 int cpu)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002841{
2842 struct device *dev = priv->net_dev->dev.parent;
2843 int i;
2844
2845 for (i = 0; i < priv->num_channels; i++)
2846 if (priv->channel[i]->nctx.desired_cpu == cpu)
2847 return priv->channel[i];
2848
2849 /* We should never get here. Issue a warning and return
2850 * the first channel, because it's still better than nothing
2851 */
2852 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2853
2854 return priv->channel[0];
2855}
2856
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002857static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002858{
2859 struct device *dev = priv->net_dev->dev.parent;
2860 struct dpaa2_eth_fq *fq;
2861 int rx_cpu, txc_cpu;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002862 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002863
2864 /* For each FQ, pick one channel/CPU to deliver frames to.
2865 * This may well change at runtime, either through irqbalance or
2866 * through direct user intervention.
2867 */
2868 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2869
2870 for (i = 0; i < priv->num_fqs; i++) {
2871 fq = &priv->fq[i];
2872 switch (fq->type) {
2873 case DPAA2_RX_FQ:
Ioana Ciornei061d6312020-10-01 18:11:48 +03002874 case DPAA2_RX_ERR_FQ:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002875 fq->target_cpu = rx_cpu;
2876 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2877 if (rx_cpu >= nr_cpu_ids)
2878 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2879 break;
2880 case DPAA2_TX_CONF_FQ:
2881 fq->target_cpu = txc_cpu;
2882 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2883 if (txc_cpu >= nr_cpu_ids)
2884 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2885 break;
2886 default:
2887 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2888 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002889 fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002890 }
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002891
2892 update_xps(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002893}
2894
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002895static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002896{
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002897 int i, j;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002898
2899 /* We have one TxConf FQ per Tx flow.
2900 * The number of Tx and Rx queues is the same.
2901 * Tx queues come first in the fq array.
2902 */
2903 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2904 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2905 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2906 priv->fq[priv->num_fqs++].flowid = (u16)i;
2907 }
2908
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002909 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2910 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2911 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2912 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2913 priv->fq[priv->num_fqs].tc = (u8)j;
2914 priv->fq[priv->num_fqs++].flowid = (u16)i;
2915 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002916 }
2917
Ioana Ciornei061d6312020-10-01 18:11:48 +03002918 /* We have exactly one Rx error queue per DPNI */
2919 priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
2920 priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
2921
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002922 /* For each FQ, decide on which core to process incoming frames */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002923 dpaa2_eth_set_fq_affinity(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002924}
2925
2926/* Allocate and configure one buffer pool for each interface */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002927static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002928{
2929 int err;
2930 struct fsl_mc_device *dpbp_dev;
2931 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002932 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002933
2934 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2935 &dpbp_dev);
2936 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002937 if (err == -ENXIO)
2938 err = -EPROBE_DEFER;
2939 else
2940 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002941 return err;
2942 }
2943
2944 priv->dpbp_dev = dpbp_dev;
2945
2946 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2947 &dpbp_dev->mc_handle);
2948 if (err) {
2949 dev_err(dev, "dpbp_open() failed\n");
2950 goto err_open;
2951 }
2952
Ioana Radulescud00defe2017-06-06 10:00:32 -05002953 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2954 if (err) {
2955 dev_err(dev, "dpbp_reset() failed\n");
2956 goto err_reset;
2957 }
2958
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002959 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2960 if (err) {
2961 dev_err(dev, "dpbp_enable() failed\n");
2962 goto err_enable;
2963 }
2964
2965 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002966 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002967 if (err) {
2968 dev_err(dev, "dpbp_get_attributes() failed\n");
2969 goto err_get_attr;
2970 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002971 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002972
2973 return 0;
2974
2975err_get_attr:
2976 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2977err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002978err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002979 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2980err_open:
2981 fsl_mc_object_free(dpbp_dev);
2982
2983 return err;
2984}
2985
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002986static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002987{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002988 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002989 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2990 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2991 fsl_mc_object_free(priv->dpbp_dev);
2992}
2993
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002994static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002995{
2996 struct device *dev = priv->net_dev->dev.parent;
2997 struct dpni_buffer_layout buf_layout = {0};
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002998 u16 rx_buf_align;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002999 int err;
3000
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00003001 /* We need to check for WRIOP version 1.0.0, but depending on the MC
3002 * version, this number is not always provided correctly on rev1.
3003 * We need to check for both alternatives in this situation.
3004 */
3005 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
3006 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00003007 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00003008 else
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00003009 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00003010
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03003011 /* We need to ensure that the buffer size seen by WRIOP is a multiple
3012 * of 64 or 256 bytes depending on the WRIOP version.
3013 */
3014 priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
3015
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00003016 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003017 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08003018 buf_layout.pass_timestamp = true;
Yangbo Luc5521182020-09-18 17:08:02 +08003019 buf_layout.pass_frame_status = true;
Ioana Radulescu859f9982018-04-26 18:23:47 +08003020 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
Yangbo Luc5521182020-09-18 17:08:02 +08003021 DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3022 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003023 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3024 DPNI_QUEUE_TX, &buf_layout);
3025 if (err) {
3026 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
3027 return err;
3028 }
3029
3030 /* tx-confirm buffer */
Yangbo Luc5521182020-09-18 17:08:02 +08003031 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3032 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003033 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3034 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
3035 if (err) {
3036 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
3037 return err;
3038 }
3039
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00003040 /* Now that we've set our tx buffer layout, retrieve the minimum
3041 * required tx data offset.
3042 */
3043 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
3044 &priv->tx_data_offset);
3045 if (err) {
3046 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
3047 return err;
3048 }
3049
3050 if ((priv->tx_data_offset % 64) != 0)
3051 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
3052 priv->tx_data_offset);
3053
3054 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06003055 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00003056 buf_layout.pass_parser_result = true;
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00003057 buf_layout.data_align = rx_buf_align;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00003058 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
3059 buf_layout.private_data_size = 0;
3060 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
3061 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
3062 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08003063 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
3064 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00003065 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3066 DPNI_QUEUE_RX, &buf_layout);
3067 if (err) {
3068 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
3069 return err;
3070 }
3071
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003072 return 0;
3073}
3074
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003075#define DPNI_ENQUEUE_FQID_VER_MAJOR 7
3076#define DPNI_ENQUEUE_FQID_VER_MINOR 9
3077
3078static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
3079 struct dpaa2_eth_fq *fq,
Ioana Ciornei48c04812020-04-22 15:05:10 +03003080 struct dpaa2_fd *fd, u8 prio,
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003081 u32 num_frames __always_unused,
Ioana Ciornei48c04812020-04-22 15:05:10 +03003082 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003083{
Ioana Ciornei48c04812020-04-22 15:05:10 +03003084 int err;
3085
3086 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
3087 priv->tx_qdid, prio,
3088 fq->tx_qdbin, fd);
3089 if (!err && frames_enqueued)
3090 *frames_enqueued = 1;
3091 return err;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003092}
3093
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003094static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
3095 struct dpaa2_eth_fq *fq,
3096 struct dpaa2_fd *fd,
3097 u8 prio, u32 num_frames,
3098 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003099{
Ioana Ciornei48c04812020-04-22 15:05:10 +03003100 int err;
3101
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003102 err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
3103 fq->tx_fqid[prio],
3104 fd, num_frames);
3105
3106 if (err == 0)
3107 return -EBUSY;
3108
3109 if (frames_enqueued)
3110 *frames_enqueued = err;
3111 return 0;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003112}
3113
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003114static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003115{
3116 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3117 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3118 priv->enqueue = dpaa2_eth_enqueue_qd;
3119 else
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003120 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003121}
3122
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003123static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003124{
3125 struct device *dev = priv->net_dev->dev.parent;
3126 struct dpni_link_cfg link_cfg = {0};
3127 int err;
3128
3129 /* Get the default link options so we don't override other flags */
3130 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3131 if (err) {
3132 dev_err(dev, "dpni_get_link_cfg() failed\n");
3133 return err;
3134 }
3135
3136 /* By default, enable both Rx and Tx pause frames */
3137 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
3138 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
3139 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3140 if (err) {
3141 dev_err(dev, "dpni_set_link_cfg() failed\n");
3142 return err;
3143 }
3144
3145 priv->link_state.options = link_cfg.options;
3146
3147 return 0;
3148}
3149
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003150static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
Ioana Radulescua690af4f2019-10-16 10:36:23 +03003151{
3152 struct dpni_queue_id qid = {0};
3153 struct dpaa2_eth_fq *fq;
3154 struct dpni_queue queue;
3155 int i, j, err;
3156
3157 /* We only use Tx FQIDs for FQID-based enqueue, so check
3158 * if DPNI version supports it before updating FQIDs
3159 */
3160 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3161 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3162 return;
3163
3164 for (i = 0; i < priv->num_fqs; i++) {
3165 fq = &priv->fq[i];
3166 if (fq->type != DPAA2_TX_CONF_FQ)
3167 continue;
3168 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3169 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3170 DPNI_QUEUE_TX, j, fq->flowid,
3171 &queue, &qid);
3172 if (err)
3173 goto out_err;
3174
3175 fq->tx_fqid[j] = qid.fqid;
3176 if (fq->tx_fqid[j] == 0)
3177 goto out_err;
3178 }
3179 }
3180
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003181 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Radulescua690af4f2019-10-16 10:36:23 +03003182
3183 return;
3184
3185out_err:
3186 netdev_info(priv->net_dev,
3187 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3188 priv->enqueue = dpaa2_eth_enqueue_qd;
3189}
3190
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003191/* Configure ingress classification based on VLAN PCP */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003192static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003193{
3194 struct device *dev = priv->net_dev->dev.parent;
3195 struct dpkg_profile_cfg kg_cfg = {0};
3196 struct dpni_qos_tbl_cfg qos_cfg = {0};
3197 struct dpni_rule_cfg key_params;
3198 void *dma_mem, *key, *mask;
3199 u8 key_size = 2; /* VLAN TCI field */
3200 int i, pcp, err;
3201
3202 /* VLAN-based classification only makes sense if we have multiple
3203 * traffic classes.
3204 * Also, we need to extract just the 3-bit PCP field from the VLAN
3205 * header and we can only do that by using a mask
3206 */
3207 if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3208 dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3209 return -EOPNOTSUPP;
3210 }
3211
3212 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3213 if (!dma_mem)
3214 return -ENOMEM;
3215
3216 kg_cfg.num_extracts = 1;
3217 kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3218 kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3219 kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3220 kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3221
3222 err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3223 if (err) {
3224 dev_err(dev, "dpni_prepare_key_cfg failed\n");
3225 goto out_free_tbl;
3226 }
3227
3228 /* set QoS table */
3229 qos_cfg.default_tc = 0;
3230 qos_cfg.discard_on_miss = 0;
3231 qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3232 DPAA2_CLASSIFIER_DMA_SIZE,
3233 DMA_TO_DEVICE);
3234 if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3235 dev_err(dev, "QoS table DMA mapping failed\n");
3236 err = -ENOMEM;
3237 goto out_free_tbl;
3238 }
3239
3240 err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3241 if (err) {
3242 dev_err(dev, "dpni_set_qos_table failed\n");
3243 goto out_unmap_tbl;
3244 }
3245
3246 /* Add QoS table entries */
3247 key = kzalloc(key_size * 2, GFP_KERNEL);
3248 if (!key) {
3249 err = -ENOMEM;
3250 goto out_unmap_tbl;
3251 }
3252 mask = key + key_size;
3253 *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3254
3255 key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3256 DMA_TO_DEVICE);
3257 if (dma_mapping_error(dev, key_params.key_iova)) {
3258 dev_err(dev, "Qos table entry DMA mapping failed\n");
3259 err = -ENOMEM;
3260 goto out_free_key;
3261 }
3262
3263 key_params.mask_iova = key_params.key_iova + key_size;
3264 key_params.key_size = key_size;
3265
3266 /* We add rules for PCP-based distribution starting with highest
3267 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3268 * classes to accommodate all priority levels, the lowest ones end up
3269 * on TC 0 which was configured as default
3270 */
3271 for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3272 *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3273 dma_sync_single_for_device(dev, key_params.key_iova,
3274 key_size * 2, DMA_TO_DEVICE);
3275
3276 err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3277 &key_params, i, i);
3278 if (err) {
3279 dev_err(dev, "dpni_add_qos_entry failed\n");
3280 dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3281 goto out_unmap_key;
3282 }
3283 }
3284
3285 priv->vlan_cls_enabled = true;
3286
3287 /* Table and key memory is not persistent, clean everything up after
3288 * configuration is finished
3289 */
3290out_unmap_key:
3291 dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3292out_free_key:
3293 kfree(key);
3294out_unmap_tbl:
3295 dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3296 DMA_TO_DEVICE);
3297out_free_tbl:
3298 kfree(dma_mem);
3299
3300 return err;
3301}
3302
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003303/* Configure the DPNI object this interface is associated with */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003304static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003305{
3306 struct device *dev = &ls_dev->dev;
3307 struct dpaa2_eth_priv *priv;
3308 struct net_device *net_dev;
3309 int err;
3310
3311 net_dev = dev_get_drvdata(dev);
3312 priv = netdev_priv(net_dev);
3313
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003314 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05003315 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003316 if (err) {
3317 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003318 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003319 }
3320
Ioana Radulescu311cffa2018-03-23 08:44:09 -05003321 /* Check if we can work with this DPNI object */
3322 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3323 &priv->dpni_ver_minor);
3324 if (err) {
3325 dev_err(dev, "dpni_get_api_version() failed\n");
3326 goto close;
3327 }
3328 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3329 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3330 priv->dpni_ver_major, priv->dpni_ver_minor,
3331 DPNI_VER_MAJOR, DPNI_VER_MINOR);
3332 err = -ENOTSUPP;
3333 goto close;
3334 }
3335
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003336 ls_dev->mc_io = priv->mc_io;
3337 ls_dev->mc_handle = priv->mc_token;
3338
3339 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3340 if (err) {
3341 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003342 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003343 }
3344
3345 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3346 &priv->dpni_attrs);
3347 if (err) {
3348 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003349 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003350 }
3351
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003352 err = dpaa2_eth_set_buffer_layout(priv);
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003353 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003354 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003355
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003356 dpaa2_eth_set_enqueue_mode(priv);
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003357
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003358 /* Enable pause frame support */
3359 if (dpaa2_eth_has_pause_support(priv)) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003360 err = dpaa2_eth_set_pause(priv);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003361 if (err)
3362 goto close;
3363 }
3364
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003365 err = dpaa2_eth_set_vlan_qos(priv);
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003366 if (err && err != -EOPNOTSUPP)
3367 goto close;
3368
Xu Wang9334d5b2020-06-11 02:45:20 +00003369 priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3370 sizeof(struct dpaa2_eth_cls_rule),
3371 GFP_KERNEL);
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003372 if (!priv->cls_rules) {
3373 err = -ENOMEM;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003374 goto close;
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003375 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003376
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003377 return 0;
3378
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003379close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003380 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003381
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003382 return err;
3383}
3384
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003385static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003386{
3387 int err;
3388
3389 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3390 if (err)
3391 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3392 err);
3393
3394 dpni_close(priv->mc_io, 0, priv->mc_token);
3395}
3396
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003397static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3398 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003399{
3400 struct device *dev = priv->net_dev->dev.parent;
3401 struct dpni_queue queue;
3402 struct dpni_queue_id qid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003403 int err;
3404
3405 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003406 DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003407 if (err) {
3408 dev_err(dev, "dpni_get_queue(RX) failed\n");
3409 return err;
3410 }
3411
3412 fq->fqid = qid.fqid;
3413
3414 queue.destination.id = fq->channel->dpcon_id;
3415 queue.destination.type = DPNI_DEST_DPCON;
3416 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003417 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003418 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003419 DPNI_QUEUE_RX, fq->tc, fq->flowid,
Ioana Radulescu16fa1cf2019-05-23 17:38:22 +03003420 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003421 &queue);
3422 if (err) {
3423 dev_err(dev, "dpni_set_queue(RX) failed\n");
3424 return err;
3425 }
3426
Ioana Radulescud678be12019-03-01 17:47:24 +00003427 /* xdp_rxq setup */
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003428 /* only once for each channel */
3429 if (fq->tc > 0)
3430 return 0;
3431
Ioana Radulescud678be12019-03-01 17:47:24 +00003432 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
Björn Töpelb02e5a02020-11-30 19:52:01 +01003433 fq->flowid, 0);
Ioana Radulescud678be12019-03-01 17:47:24 +00003434 if (err) {
3435 dev_err(dev, "xdp_rxq_info_reg failed\n");
3436 return err;
3437 }
3438
3439 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3440 MEM_TYPE_PAGE_ORDER0, NULL);
3441 if (err) {
3442 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3443 return err;
3444 }
3445
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003446 return 0;
3447}
3448
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003449static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3450 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003451{
3452 struct device *dev = priv->net_dev->dev.parent;
3453 struct dpni_queue queue;
3454 struct dpni_queue_id qid;
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003455 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003456
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003457 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3458 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3459 DPNI_QUEUE_TX, i, fq->flowid,
3460 &queue, &qid);
3461 if (err) {
3462 dev_err(dev, "dpni_get_queue(TX) failed\n");
3463 return err;
3464 }
3465 fq->tx_fqid[i] = qid.fqid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003466 }
3467
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003468 /* All Tx queues belonging to the same flowid have the same qdbin */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003469 fq->tx_qdbin = qid.qdbin;
3470
3471 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3472 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3473 &queue, &qid);
3474 if (err) {
3475 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3476 return err;
3477 }
3478
3479 fq->fqid = qid.fqid;
3480
3481 queue.destination.id = fq->channel->dpcon_id;
3482 queue.destination.type = DPNI_DEST_DPCON;
3483 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003484 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003485 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3486 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3487 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3488 &queue);
3489 if (err) {
3490 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3491 return err;
3492 }
3493
3494 return 0;
3495}
3496
Ioana Ciornei061d6312020-10-01 18:11:48 +03003497static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
3498 struct dpaa2_eth_fq *fq)
3499{
3500 struct device *dev = priv->net_dev->dev.parent;
3501 struct dpni_queue q = { { 0 } };
3502 struct dpni_queue_id qid;
3503 u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
3504 int err;
3505
3506 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3507 DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
3508 if (err) {
3509 dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
3510 return err;
3511 }
3512
3513 fq->fqid = qid.fqid;
3514
3515 q.destination.id = fq->channel->dpcon_id;
3516 q.destination.type = DPNI_DEST_DPCON;
3517 q.destination.priority = 1;
3518 q.user_context = (u64)(uintptr_t)fq;
3519 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3520 DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
3521 if (err) {
3522 dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
3523 return err;
3524 }
3525
3526 return 0;
3527}
3528
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003529/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003530static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003531 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003532 /* L2 header */
3533 .rxnfc_field = RXH_L2DA,
3534 .cls_prot = NET_PROT_ETH,
3535 .cls_field = NH_FLD_ETH_DA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003536 .id = DPAA2_ETH_DIST_ETHDST,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003537 .size = 6,
3538 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003539 .cls_prot = NET_PROT_ETH,
3540 .cls_field = NH_FLD_ETH_SA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003541 .id = DPAA2_ETH_DIST_ETHSRC,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003542 .size = 6,
3543 }, {
3544 /* This is the last ethertype field parsed:
3545 * depending on frame format, it can be the MAC ethertype
3546 * or the VLAN etype.
3547 */
3548 .cls_prot = NET_PROT_ETH,
3549 .cls_field = NH_FLD_ETH_TYPE,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003550 .id = DPAA2_ETH_DIST_ETHTYPE,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003551 .size = 2,
3552 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003553 /* VLAN header */
3554 .rxnfc_field = RXH_VLAN,
3555 .cls_prot = NET_PROT_VLAN,
3556 .cls_field = NH_FLD_VLAN_TCI,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003557 .id = DPAA2_ETH_DIST_VLAN,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003558 .size = 2,
3559 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003560 /* IP header */
3561 .rxnfc_field = RXH_IP_SRC,
3562 .cls_prot = NET_PROT_IP,
3563 .cls_field = NH_FLD_IP_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003564 .id = DPAA2_ETH_DIST_IPSRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003565 .size = 4,
3566 }, {
3567 .rxnfc_field = RXH_IP_DST,
3568 .cls_prot = NET_PROT_IP,
3569 .cls_field = NH_FLD_IP_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003570 .id = DPAA2_ETH_DIST_IPDST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003571 .size = 4,
3572 }, {
3573 .rxnfc_field = RXH_L3_PROTO,
3574 .cls_prot = NET_PROT_IP,
3575 .cls_field = NH_FLD_IP_PROTO,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003576 .id = DPAA2_ETH_DIST_IPPROTO,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003577 .size = 1,
3578 }, {
3579 /* Using UDP ports, this is functionally equivalent to raw
3580 * byte pairs from L4 header.
3581 */
3582 .rxnfc_field = RXH_L4_B_0_1,
3583 .cls_prot = NET_PROT_UDP,
3584 .cls_field = NH_FLD_UDP_PORT_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003585 .id = DPAA2_ETH_DIST_L4SRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003586 .size = 2,
3587 }, {
3588 .rxnfc_field = RXH_L4_B_2_3,
3589 .cls_prot = NET_PROT_UDP,
3590 .cls_field = NH_FLD_UDP_PORT_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003591 .id = DPAA2_ETH_DIST_L4DST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003592 .size = 2,
3593 },
3594};
3595
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003596/* Configure the Rx hash key using the legacy API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003597static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003598{
3599 struct device *dev = priv->net_dev->dev.parent;
3600 struct dpni_rx_tc_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003601 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003602
3603 memset(&dist_cfg, 0, sizeof(dist_cfg));
3604
3605 dist_cfg.key_cfg_iova = key;
3606 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3607 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3608
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003609 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3610 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3611 i, &dist_cfg);
3612 if (err) {
3613 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3614 break;
3615 }
3616 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003617
3618 return err;
3619}
3620
3621/* Configure the Rx hash key using the new API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003622static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003623{
3624 struct device *dev = priv->net_dev->dev.parent;
3625 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003626 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003627
3628 memset(&dist_cfg, 0, sizeof(dist_cfg));
3629
3630 dist_cfg.key_cfg_iova = key;
3631 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3632 dist_cfg.enable = 1;
3633
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003634 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3635 dist_cfg.tc = i;
3636 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3637 &dist_cfg);
3638 if (err) {
3639 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3640 break;
3641 }
Ionut-robert Aron5e29c162020-09-25 17:44:21 +03003642
3643 /* If the flow steering / hashing key is shared between all
3644 * traffic classes, install it just once
3645 */
3646 if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3647 break;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003648 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003649
3650 return err;
3651}
3652
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003653/* Configure the Rx flow classification key */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003654static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003655{
3656 struct device *dev = priv->net_dev->dev.parent;
3657 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003658 int i, err = 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003659
3660 memset(&dist_cfg, 0, sizeof(dist_cfg));
3661
3662 dist_cfg.key_cfg_iova = key;
3663 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3664 dist_cfg.enable = 1;
3665
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003666 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3667 dist_cfg.tc = i;
3668 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3669 &dist_cfg);
3670 if (err) {
3671 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3672 break;
3673 }
Ionut-robert Aron5e29c162020-09-25 17:44:21 +03003674
3675 /* If the flow steering / hashing key is shared between all
3676 * traffic classes, install it just once
3677 */
3678 if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3679 break;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003680 }
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003681
3682 return err;
3683}
3684
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003685/* Size of the Rx flow classification key */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003686int dpaa2_eth_cls_key_size(u64 fields)
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003687{
3688 int i, size = 0;
3689
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003690 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3691 if (!(fields & dist_fields[i].id))
3692 continue;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003693 size += dist_fields[i].size;
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003694 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003695
3696 return size;
3697}
3698
3699/* Offset of header field in Rx classification key */
3700int dpaa2_eth_cls_fld_off(int prot, int field)
3701{
3702 int i, off = 0;
3703
3704 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3705 if (dist_fields[i].cls_prot == prot &&
3706 dist_fields[i].cls_field == field)
3707 return off;
3708 off += dist_fields[i].size;
3709 }
3710
3711 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3712 return 0;
3713}
3714
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003715/* Prune unused fields from the classification rule.
3716 * Used when masking is not supported
3717 */
3718void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3719{
3720 int off = 0, new_off = 0;
3721 int i, size;
3722
3723 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3724 size = dist_fields[i].size;
3725 if (dist_fields[i].id & fields) {
3726 memcpy(key_mem + new_off, key_mem + off, size);
3727 new_off += size;
3728 }
3729 off += size;
3730 }
3731}
3732
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003733/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003734 * flags is a combination of RXH_ bits
3735 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00003736static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
3737 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003738{
3739 struct device *dev = net_dev->dev.parent;
3740 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3741 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003742 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003743 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003744 u8 *dma_mem;
3745 int i;
3746 int err = 0;
3747
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003748 memset(&cls_cfg, 0, sizeof(cls_cfg));
3749
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003750 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003751 struct dpkg_extract *key =
3752 &cls_cfg.extracts[cls_cfg.num_extracts];
3753
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003754 /* For both Rx hashing and classification keys
3755 * we set only the selected fields.
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003756 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003757 if (!(flags & dist_fields[i].id))
3758 continue;
3759 if (type == DPAA2_ETH_RX_DIST_HASH)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003760 rx_hash_fields |= dist_fields[i].rxnfc_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003761
3762 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
3763 dev_err(dev, "error adding key extraction rule, too many rules?\n");
3764 return -E2BIG;
3765 }
3766
3767 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003768 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003769 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003770 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003771 cls_cfg.num_extracts++;
3772 }
3773
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05003774 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003775 if (!dma_mem)
3776 return -ENOMEM;
3777
3778 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3779 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003780 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003781 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003782 }
3783
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003784 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003785 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3786 DMA_TO_DEVICE);
3787 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003788 dev_err(dev, "DMA mapping failed\n");
3789 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003790 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003791 }
3792
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003793 if (type == DPAA2_ETH_RX_DIST_HASH) {
3794 if (dpaa2_eth_has_legacy_dist(priv))
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003795 err = dpaa2_eth_config_legacy_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_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003798 } else {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003799 err = dpaa2_eth_config_cls_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003800 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003801
3802 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3803 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003804 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003805 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003806
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003807free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003808 kfree(dma_mem);
3809 return err;
3810}
3811
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003812int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3813{
3814 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003815 u64 key = 0;
3816 int i;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003817
3818 if (!dpaa2_eth_hash_enabled(priv))
3819 return -EOPNOTSUPP;
3820
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003821 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3822 if (dist_fields[i].rxnfc_field & flags)
3823 key |= dist_fields[i].id;
3824
3825 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003826}
3827
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003828int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3829{
3830 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3831}
3832
3833static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003834{
3835 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003836 int err;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003837
3838 /* Check if we actually support Rx flow classification */
3839 if (dpaa2_eth_has_legacy_dist(priv)) {
3840 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3841 return -EOPNOTSUPP;
3842 }
3843
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003844 if (!dpaa2_eth_fs_enabled(priv)) {
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003845 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3846 return -EOPNOTSUPP;
3847 }
3848
3849 if (!dpaa2_eth_hash_enabled(priv)) {
3850 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3851 return -EOPNOTSUPP;
3852 }
3853
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003854 /* If there is no support for masking in the classification table,
3855 * we don't set a default key, as it will depend on the rules
3856 * added by the user at runtime.
3857 */
3858 if (!dpaa2_eth_fs_mask_enabled(priv))
3859 goto out;
3860
3861 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003862 if (err)
3863 return err;
3864
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003865out:
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003866 priv->rx_cls_enabled = 1;
3867
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003868 return 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003869}
3870
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003871/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3872 * frame queues and channels
3873 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003874static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003875{
3876 struct net_device *net_dev = priv->net_dev;
3877 struct device *dev = net_dev->dev.parent;
3878 struct dpni_pools_cfg pools_params;
3879 struct dpni_error_cfg err_cfg;
3880 int err = 0;
3881 int i;
3882
3883 pools_params.num_dpbp = 1;
3884 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3885 pools_params.pools[0].backup_pool = 0;
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03003886 pools_params.pools[0].buffer_size = priv->rx_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003887 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3888 if (err) {
3889 dev_err(dev, "dpni_set_pools() failed\n");
3890 return err;
3891 }
3892
Ioana Radulescu227686b2018-07-27 09:12:59 -05003893 /* have the interface implicitly distribute traffic based on
3894 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003895 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05003896 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003897 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05003898 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003899
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003900 /* Configure the flow classification key; it includes all
3901 * supported header fields and cannot be modified at runtime
3902 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003903 err = dpaa2_eth_set_default_cls(priv);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003904 if (err && err != -EOPNOTSUPP)
3905 dev_err(dev, "Failed to configure Rx classification key\n");
3906
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003907 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05003908 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003909 err_cfg.set_frame_annotation = 1;
3910 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3911 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3912 &err_cfg);
3913 if (err) {
3914 dev_err(dev, "dpni_set_errors_behavior failed\n");
3915 return err;
3916 }
3917
3918 /* Configure Rx and Tx conf queues to generate CDANs */
3919 for (i = 0; i < priv->num_fqs; i++) {
3920 switch (priv->fq[i].type) {
3921 case DPAA2_RX_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003922 err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003923 break;
3924 case DPAA2_TX_CONF_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003925 err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003926 break;
Ioana Ciornei061d6312020-10-01 18:11:48 +03003927 case DPAA2_RX_ERR_FQ:
3928 err = setup_rx_err_flow(priv, &priv->fq[i]);
3929 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003930 default:
3931 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3932 return -EINVAL;
3933 }
3934 if (err)
3935 return err;
3936 }
3937
3938 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3939 DPNI_QUEUE_TX, &priv->tx_qdid);
3940 if (err) {
3941 dev_err(dev, "dpni_get_qdid() failed\n");
3942 return err;
3943 }
3944
3945 return 0;
3946}
3947
3948/* Allocate rings for storing incoming frame descriptors */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003949static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003950{
3951 struct net_device *net_dev = priv->net_dev;
3952 struct device *dev = net_dev->dev.parent;
3953 int i;
3954
3955 for (i = 0; i < priv->num_channels; i++) {
3956 priv->channel[i]->store =
3957 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3958 if (!priv->channel[i]->store) {
3959 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3960 goto err_ring;
3961 }
3962 }
3963
3964 return 0;
3965
3966err_ring:
3967 for (i = 0; i < priv->num_channels; i++) {
3968 if (!priv->channel[i]->store)
3969 break;
3970 dpaa2_io_store_destroy(priv->channel[i]->store);
3971 }
3972
3973 return -ENOMEM;
3974}
3975
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003976static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003977{
3978 int i;
3979
3980 for (i = 0; i < priv->num_channels; i++)
3981 dpaa2_io_store_destroy(priv->channel[i]->store);
3982}
3983
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003984static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003985{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003986 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003987 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003988 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003989 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003990
3991 /* Get firmware address, if any */
3992 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3993 if (err) {
3994 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3995 return err;
3996 }
3997
3998 /* Get DPNI attributes address, if any */
3999 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4000 dpni_mac_addr);
4001 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004002 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004003 return err;
4004 }
4005
4006 /* First check if firmware has any address configured by bootloader */
4007 if (!is_zero_ether_addr(mac_addr)) {
4008 /* If the DPMAC addr != DPNI addr, update it */
4009 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
4010 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
4011 priv->mc_token,
4012 mac_addr);
4013 if (err) {
4014 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4015 return err;
4016 }
4017 }
4018 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
4019 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004020 /* No MAC address configured, fill in net_dev->dev_addr
4021 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004022 */
4023 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004024 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
4025
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004026 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4027 net_dev->dev_addr);
4028 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004029 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004030 return err;
4031 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004032
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004033 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
4034 * practical purposes, this will be our "permanent" mac address,
4035 * at least until the next reboot. This move will also permit
4036 * register_netdevice() to properly fill up net_dev->perm_addr.
4037 */
4038 net_dev->addr_assign_type = NET_ADDR_PERM;
4039 } else {
4040 /* NET_ADDR_PERM is default, all we have to do is
4041 * fill in the device addr.
4042 */
4043 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
4044 }
4045
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004046 return 0;
4047}
4048
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004049static int dpaa2_eth_netdev_init(struct net_device *net_dev)
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004050{
4051 struct device *dev = net_dev->dev.parent;
4052 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004053 u32 options = priv->dpni_attrs.options;
4054 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004055 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05004056 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004057 int err;
4058
4059 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004060 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004061
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004062 err = dpaa2_eth_set_mac_addr(priv);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004063 if (err)
4064 return err;
4065
4066 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004067 eth_broadcast_addr(bcast_addr);
4068 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
4069 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05004070 dev_err(dev, "dpni_add_mac_addr() failed\n");
4071 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004072 }
4073
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05004074 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004075 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05004076 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05004077 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05004078 if (err) {
4079 dev_err(dev, "dpni_set_max_frame_length() failed\n");
4080 return err;
4081 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004082
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05004083 /* Set actual number of queues in the net device */
4084 num_queues = dpaa2_eth_queue_count(priv);
4085 err = netif_set_real_num_tx_queues(net_dev, num_queues);
4086 if (err) {
4087 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
4088 return err;
4089 }
4090 err = netif_set_real_num_rx_queues(net_dev, num_queues);
4091 if (err) {
4092 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
4093 return err;
4094 }
4095
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004096 /* Capabilities listing */
4097 supported |= IFF_LIVE_ADDR_CHANGE;
4098
4099 if (options & DPNI_OPT_NO_MAC_FILTER)
4100 not_supported |= IFF_UNICAST_FLT;
4101 else
4102 supported |= IFF_UNICAST_FLT;
4103
4104 net_dev->priv_flags |= supported;
4105 net_dev->priv_flags &= ~not_supported;
4106
4107 /* Features */
4108 net_dev->features = NETIF_F_RXCSUM |
4109 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4110 NETIF_F_SG | NETIF_F_HIGHDMA |
Ioana Ciornei3657cda2020-07-21 19:38:25 +03004111 NETIF_F_LLTX | NETIF_F_HW_TC;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004112 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004113
Ionut-robert Aron70b32d82021-01-11 19:07:25 +02004114 if (priv->dpni_attrs.vlan_filter_entries)
4115 net_dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4116
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004117 return 0;
4118}
4119
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004120static int dpaa2_eth_poll_link_state(void *arg)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004121{
4122 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
4123 int err;
4124
4125 while (!kthread_should_stop()) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004126 err = dpaa2_eth_link_state_update(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004127 if (unlikely(err))
4128 return err;
4129
4130 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
4131 }
4132
4133 return 0;
4134}
4135
Ioana Ciornei71947922019-10-31 01:18:31 +02004136static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
4137{
4138 struct fsl_mc_device *dpni_dev, *dpmac_dev;
4139 struct dpaa2_mac *mac;
4140 int err;
4141
4142 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
4143 dpmac_dev = fsl_mc_get_endpoint(dpni_dev);
Ioana Ciornei47325da2021-01-08 11:07:25 +02004144
4145 if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER)
4146 return PTR_ERR(dpmac_dev);
4147
4148 if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
Ioana Ciornei71947922019-10-31 01:18:31 +02004149 return 0;
4150
Ioana Ciornei71947922019-10-31 01:18:31 +02004151 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
4152 if (!mac)
4153 return -ENOMEM;
4154
4155 mac->mc_dev = dpmac_dev;
4156 mac->mc_io = priv->mc_io;
4157 mac->net_dev = priv->net_dev;
4158
Ioana Ciornei095dca12021-01-08 11:07:22 +02004159 err = dpaa2_mac_open(mac);
4160 if (err)
4161 goto err_free_mac;
Ioana Ciornei71947922019-10-31 01:18:31 +02004162 priv->mac = mac;
4163
Ioana Ciorneid87e6062021-01-08 11:07:23 +02004164 if (dpaa2_eth_is_type_phy(priv)) {
4165 err = dpaa2_mac_connect(mac);
4166 if (err) {
4167 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint\n");
4168 goto err_close_mac;
4169 }
4170 }
4171
Ioana Ciornei71947922019-10-31 01:18:31 +02004172 return 0;
Ioana Ciornei095dca12021-01-08 11:07:22 +02004173
4174err_close_mac:
4175 dpaa2_mac_close(mac);
Ioana Ciorneid87e6062021-01-08 11:07:23 +02004176 priv->mac = NULL;
Ioana Ciornei095dca12021-01-08 11:07:22 +02004177err_free_mac:
4178 kfree(mac);
4179 return err;
Ioana Ciornei71947922019-10-31 01:18:31 +02004180}
4181
4182static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
4183{
Ioana Ciorneid87e6062021-01-08 11:07:23 +02004184 if (dpaa2_eth_is_type_phy(priv))
4185 dpaa2_mac_disconnect(priv->mac);
Ioana Ciornei71947922019-10-31 01:18:31 +02004186
Ioana Ciornei848c1902021-01-11 19:18:02 +02004187 if (!dpaa2_eth_has_mac(priv))
4188 return;
4189
Ioana Ciornei095dca12021-01-08 11:07:22 +02004190 dpaa2_mac_close(priv->mac);
Ioana Ciornei71947922019-10-31 01:18:31 +02004191 kfree(priv->mac);
4192 priv->mac = NULL;
4193}
4194
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004195static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
4196{
Ioana Radulescu112197d2017-10-11 08:29:49 -05004197 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004198 struct device *dev = (struct device *)arg;
4199 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
4200 struct net_device *net_dev = dev_get_drvdata(dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02004201 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004202 int err;
4203
4204 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
4205 DPNI_IRQ_INDEX, &status);
4206 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004207 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05004208 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004209 }
4210
Ioana Radulescu112197d2017-10-11 08:29:49 -05004211 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004212 dpaa2_eth_link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004213
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02004214 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004215 dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
4216 dpaa2_eth_update_tx_fqids(priv);
Ioana Ciornei71947922019-10-31 01:18:31 +02004217
4218 rtnl_lock();
Ioana Ciorneid87e6062021-01-08 11:07:23 +02004219 if (dpaa2_eth_has_mac(priv))
Ioana Ciornei71947922019-10-31 01:18:31 +02004220 dpaa2_eth_disconnect_mac(priv);
4221 else
4222 dpaa2_eth_connect_mac(priv);
4223 rtnl_unlock();
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02004224 }
Florin Chiculita8398b372019-10-16 10:36:22 +03004225
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004226 return IRQ_HANDLED;
4227}
4228
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004229static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004230{
4231 int err = 0;
4232 struct fsl_mc_device_irq *irq;
4233
4234 err = fsl_mc_allocate_irqs(ls_dev);
4235 if (err) {
4236 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4237 return err;
4238 }
4239
4240 irq = ls_dev->irqs[0];
4241 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05004242 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004243 IRQF_NO_SUSPEND | IRQF_ONESHOT,
4244 dev_name(&ls_dev->dev), &ls_dev->dev);
4245 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004246 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004247 goto free_mc_irq;
4248 }
4249
4250 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
Florin Chiculita8398b372019-10-16 10:36:22 +03004251 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4252 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004253 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004254 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004255 goto free_irq;
4256 }
4257
4258 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4259 DPNI_IRQ_INDEX, 1);
4260 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004261 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004262 goto free_irq;
4263 }
4264
4265 return 0;
4266
4267free_irq:
4268 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
4269free_mc_irq:
4270 fsl_mc_free_irqs(ls_dev);
4271
4272 return err;
4273}
4274
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004275static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004276{
4277 int i;
4278 struct dpaa2_eth_channel *ch;
4279
4280 for (i = 0; i < priv->num_channels; i++) {
4281 ch = priv->channel[i];
4282 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4283 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
4284 NAPI_POLL_WEIGHT);
4285 }
4286}
4287
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004288static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004289{
4290 int i;
4291 struct dpaa2_eth_channel *ch;
4292
4293 for (i = 0; i < priv->num_channels; i++) {
4294 ch = priv->channel[i];
4295 netif_napi_del(&ch->napi);
4296 }
4297}
4298
4299static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4300{
4301 struct device *dev;
4302 struct net_device *net_dev = NULL;
4303 struct dpaa2_eth_priv *priv = NULL;
4304 int err = 0;
4305
4306 dev = &dpni_dev->dev;
4307
4308 /* Net device */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03004309 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004310 if (!net_dev) {
4311 dev_err(dev, "alloc_etherdev_mq() failed\n");
4312 return -ENOMEM;
4313 }
4314
4315 SET_NETDEV_DEV(net_dev, dev);
4316 dev_set_drvdata(dev, net_dev);
4317
4318 priv = netdev_priv(net_dev);
4319 priv->net_dev = net_dev;
4320
Ioana Radulescu08eb2392017-05-24 07:13:27 -05004321 priv->iommu_domain = iommu_get_domain_for_dev(dev);
4322
Yangbo Lu1cf773b2020-09-18 17:08:01 +08004323 priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4324 priv->rx_tstamp = false;
4325
Yangbo Luc5521182020-09-18 17:08:02 +08004326 priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
4327 if (!priv->dpaa2_ptp_wq) {
4328 err = -ENOMEM;
4329 goto err_wq_alloc;
4330 }
4331
4332 INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4333
4334 skb_queue_head_init(&priv->tx_skbs);
4335
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004336 /* Obtain a MC portal */
4337 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4338 &priv->mc_io);
4339 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05004340 if (err == -ENXIO)
4341 err = -EPROBE_DEFER;
4342 else
4343 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004344 goto err_portal_alloc;
4345 }
4346
4347 /* MC objects initialization and configuration */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004348 err = dpaa2_eth_setup_dpni(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004349 if (err)
4350 goto err_dpni_setup;
4351
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004352 err = dpaa2_eth_setup_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004353 if (err)
4354 goto err_dpio_setup;
4355
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004356 dpaa2_eth_setup_fqs(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004357
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004358 err = dpaa2_eth_setup_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004359 if (err)
4360 goto err_dpbp_setup;
4361
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004362 err = dpaa2_eth_bind_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004363 if (err)
4364 goto err_bind;
4365
4366 /* Add a NAPI context for each channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004367 dpaa2_eth_add_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004368
4369 /* Percpu statistics */
4370 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4371 if (!priv->percpu_stats) {
4372 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4373 err = -ENOMEM;
4374 goto err_alloc_percpu_stats;
4375 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004376 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4377 if (!priv->percpu_extras) {
4378 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4379 err = -ENOMEM;
4380 goto err_alloc_percpu_extras;
4381 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004382
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004383 priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4384 if (!priv->sgt_cache) {
4385 dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4386 err = -ENOMEM;
4387 goto err_alloc_sgt_cache;
4388 }
4389
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004390 err = dpaa2_eth_netdev_init(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004391 if (err)
4392 goto err_netdev_init;
4393
4394 /* Configure checksum offload based on current interface flags */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004395 err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004396 if (err)
4397 goto err_csum;
4398
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004399 err = dpaa2_eth_set_tx_csum(priv,
4400 !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004401 if (err)
4402 goto err_csum;
4403
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004404 err = dpaa2_eth_alloc_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004405 if (err)
4406 goto err_alloc_rings;
4407
Ioana Ciorneif395b692020-05-31 00:08:13 +03004408#ifdef CONFIG_FSL_DPAA2_ETH_DCB
4409 if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4410 priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4411 net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4412 } else {
4413 dev_dbg(dev, "PFC not supported\n");
4414 }
4415#endif
4416
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004417 err = dpaa2_eth_setup_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004418 if (err) {
4419 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004420 priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004421 "%s_poll_link", net_dev->name);
4422 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004423 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004424 goto err_poll_thread;
4425 }
4426 priv->do_link_poll = true;
4427 }
4428
Ioana Ciornei71947922019-10-31 01:18:31 +02004429 err = dpaa2_eth_connect_mac(priv);
4430 if (err)
4431 goto err_connect_mac;
4432
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004433 err = dpaa2_eth_dl_register(priv);
4434 if (err)
4435 goto err_dl_register;
4436
Ioana Ciornei061d6312020-10-01 18:11:48 +03004437 err = dpaa2_eth_dl_traps_register(priv);
4438 if (err)
4439 goto err_dl_trap_register;
4440
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004441 err = dpaa2_eth_dl_port_add(priv);
4442 if (err)
4443 goto err_dl_port_add;
4444
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004445 err = register_netdev(net_dev);
4446 if (err < 0) {
4447 dev_err(dev, "register_netdev() failed\n");
4448 goto err_netdev_reg;
4449 }
4450
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004451#ifdef CONFIG_DEBUG_FS
4452 dpaa2_dbg_add(priv);
4453#endif
4454
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004455 dev_info(dev, "Probed interface %s\n", net_dev->name);
4456 return 0;
4457
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004458err_netdev_reg:
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004459 dpaa2_eth_dl_port_del(priv);
4460err_dl_port_add:
Ioana Ciornei061d6312020-10-01 18:11:48 +03004461 dpaa2_eth_dl_traps_unregister(priv);
4462err_dl_trap_register:
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004463 dpaa2_eth_dl_unregister(priv);
4464err_dl_register:
Ioana Ciornei71947922019-10-31 01:18:31 +02004465 dpaa2_eth_disconnect_mac(priv);
4466err_connect_mac:
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004467 if (priv->do_link_poll)
4468 kthread_stop(priv->poll_thread);
4469 else
4470 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004471err_poll_thread:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004472 dpaa2_eth_free_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004473err_alloc_rings:
4474err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004475err_netdev_init:
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004476 free_percpu(priv->sgt_cache);
4477err_alloc_sgt_cache:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004478 free_percpu(priv->percpu_extras);
4479err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004480 free_percpu(priv->percpu_stats);
4481err_alloc_percpu_stats:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004482 dpaa2_eth_del_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004483err_bind:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004484 dpaa2_eth_free_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004485err_dpbp_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004486 dpaa2_eth_free_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004487err_dpio_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004488 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004489err_dpni_setup:
4490 fsl_mc_portal_free(priv->mc_io);
4491err_portal_alloc:
Yangbo Luc5521182020-09-18 17:08:02 +08004492 destroy_workqueue(priv->dpaa2_ptp_wq);
4493err_wq_alloc:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004494 dev_set_drvdata(dev, NULL);
4495 free_netdev(net_dev);
4496
4497 return err;
4498}
4499
4500static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4501{
4502 struct device *dev;
4503 struct net_device *net_dev;
4504 struct dpaa2_eth_priv *priv;
4505
4506 dev = &ls_dev->dev;
4507 net_dev = dev_get_drvdata(dev);
4508 priv = netdev_priv(net_dev);
4509
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004510#ifdef CONFIG_DEBUG_FS
4511 dpaa2_dbg_remove(priv);
4512#endif
Ioana Ciornei71947922019-10-31 01:18:31 +02004513 rtnl_lock();
4514 dpaa2_eth_disconnect_mac(priv);
4515 rtnl_unlock();
4516
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004517 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004518
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004519 dpaa2_eth_dl_port_del(priv);
Ioana Ciornei061d6312020-10-01 18:11:48 +03004520 dpaa2_eth_dl_traps_unregister(priv);
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004521 dpaa2_eth_dl_unregister(priv);
4522
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004523 if (priv->do_link_poll)
4524 kthread_stop(priv->poll_thread);
4525 else
4526 fsl_mc_free_irqs(ls_dev);
4527
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004528 dpaa2_eth_free_rings(priv);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004529 free_percpu(priv->sgt_cache);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004530 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004531 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004532
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004533 dpaa2_eth_del_ch_napi(priv);
4534 dpaa2_eth_free_dpbp(priv);
4535 dpaa2_eth_free_dpio(priv);
4536 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004537
4538 fsl_mc_portal_free(priv->mc_io);
4539
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004540 free_netdev(net_dev);
4541
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05004542 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05004543
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004544 return 0;
4545}
4546
4547static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
4548 {
4549 .vendor = FSL_MC_VENDOR_FREESCALE,
4550 .obj_type = "dpni",
4551 },
4552 { .vendor = 0x0 }
4553};
4554MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
4555
4556static struct fsl_mc_driver dpaa2_eth_driver = {
4557 .driver = {
4558 .name = KBUILD_MODNAME,
4559 .owner = THIS_MODULE,
4560 },
4561 .probe = dpaa2_eth_probe,
4562 .remove = dpaa2_eth_remove,
4563 .match_id_table = dpaa2_eth_match_id_table
4564};
4565
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004566static int __init dpaa2_eth_driver_init(void)
4567{
4568 int err;
4569
4570 dpaa2_eth_dbg_init();
4571 err = fsl_mc_driver_register(&dpaa2_eth_driver);
4572 if (err) {
4573 dpaa2_eth_dbg_exit();
4574 return err;
4575 }
4576
4577 return 0;
4578}
4579
4580static void __exit dpaa2_eth_driver_exit(void)
4581{
4582 dpaa2_eth_dbg_exit();
4583 fsl_mc_driver_unregister(&dpaa2_eth_driver);
4584}
4585
4586module_init(dpaa2_eth_driver_init);
4587module_exit(dpaa2_eth_driver_exit);