blob: acc56606d3a5831721a4e9d84bfec15b638b26c2 [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 Ciornei71947922019-10-31 01:18:31 +02003 * Copyright 2016-2019 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>
Ioana Radulescu859f9982018-04-26 18:23:47 +080014#include <linux/net_tstamp.h>
Bogdan Purcareata6bd067c2018-02-05 08:07:42 -060015#include <linux/fsl/mc.h>
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +000016#include <linux/bpf.h>
17#include <linux/bpf_trace.h>
Ioana Radulescu859f9982018-04-26 18:23:47 +080018#include <net/sock.h>
19
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050020#include "dpaa2-eth.h"
21
Ioana Radulescu56361872017-04-28 04:50:32 -050022/* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
23 * using trace events only need to #include <trace/events/sched.h>
24 */
25#define CREATE_TRACE_POINTS
26#include "dpaa2-eth-trace.h"
27
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050028MODULE_LICENSE("Dual BSD/GPL");
29MODULE_AUTHOR("Freescale Semiconductor, Inc");
30MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
31
Ioana Radulescu08eb2392017-05-24 07:13:27 -050032static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
33 dma_addr_t iova_addr)
34{
35 phys_addr_t phys_addr;
36
37 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
38
39 return phys_to_virt(phys_addr);
40}
41
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050042static void validate_rx_csum(struct dpaa2_eth_priv *priv,
43 u32 fd_status,
44 struct sk_buff *skb)
45{
46 skb_checksum_none_assert(skb);
47
48 /* HW checksum validation is disabled, nothing to do here */
49 if (!(priv->net_dev->features & NETIF_F_RXCSUM))
50 return;
51
52 /* Read checksum validation bits */
53 if (!((fd_status & DPAA2_FAS_L3CV) &&
54 (fd_status & DPAA2_FAS_L4CV)))
55 return;
56
57 /* Inform the stack there's no need to compute L3/L4 csum anymore */
58 skb->ip_summed = CHECKSUM_UNNECESSARY;
59}
60
61/* Free a received FD.
62 * Not to be used for Tx conf FDs or on any other paths.
63 */
64static void free_rx_fd(struct dpaa2_eth_priv *priv,
65 const struct dpaa2_fd *fd,
66 void *vaddr)
67{
68 struct device *dev = priv->net_dev->dev.parent;
69 dma_addr_t addr = dpaa2_fd_get_addr(fd);
70 u8 fd_format = dpaa2_fd_get_format(fd);
71 struct dpaa2_sg_entry *sgt;
72 void *sg_vaddr;
73 int i;
74
75 /* If single buffer frame, just free the data buffer */
76 if (fd_format == dpaa2_fd_single)
77 goto free_buf;
78 else if (fd_format != dpaa2_fd_sg)
79 /* We don't support any other format */
80 return;
81
Ioana Radulescu729d79b2017-10-11 08:29:48 -050082 /* For S/G frames, we first need to free all SG entries
83 * except the first one, which was taken care of already
84 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050085 sgt = vaddr + dpaa2_fd_get_offset(fd);
Ioana Radulescu729d79b2017-10-11 08:29:48 -050086 for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050087 addr = dpaa2_sg_get_addr(&sgt[i]);
Ioana Radulescu08eb2392017-05-24 07:13:27 -050088 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000089 dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
90 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050091
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000092 free_pages((unsigned long)sg_vaddr, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050093 if (dpaa2_sg_is_final(&sgt[i]))
94 break;
95 }
96
97free_buf:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000098 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050099}
100
101/* Build a linear skb based on a single-buffer frame descriptor */
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +0000102static struct sk_buff *build_linear_skb(struct dpaa2_eth_channel *ch,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500103 const struct dpaa2_fd *fd,
104 void *fd_vaddr)
105{
106 struct sk_buff *skb = NULL;
107 u16 fd_offset = dpaa2_fd_get_offset(fd);
108 u32 fd_length = dpaa2_fd_get_len(fd);
109
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500110 ch->buf_count--;
111
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000112 skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500113 if (unlikely(!skb))
114 return NULL;
115
116 skb_reserve(skb, fd_offset);
117 skb_put(skb, fd_length);
118
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500119 return skb;
120}
121
122/* Build a non linear (fragmented) skb based on a S/G table */
123static struct sk_buff *build_frag_skb(struct dpaa2_eth_priv *priv,
124 struct dpaa2_eth_channel *ch,
125 struct dpaa2_sg_entry *sgt)
126{
127 struct sk_buff *skb = NULL;
128 struct device *dev = priv->net_dev->dev.parent;
129 void *sg_vaddr;
130 dma_addr_t sg_addr;
131 u16 sg_offset;
132 u32 sg_length;
133 struct page *page, *head_page;
134 int page_offset;
135 int i;
136
137 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
138 struct dpaa2_sg_entry *sge = &sgt[i];
139
140 /* NOTE: We only support SG entries in dpaa2_sg_single format,
141 * but this is the only format we may receive from HW anyway
142 */
143
144 /* Get the address and length from the S/G entry */
145 sg_addr = dpaa2_sg_get_addr(sge);
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500146 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000147 dma_unmap_page(dev, sg_addr, DPAA2_ETH_RX_BUF_SIZE,
148 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500149
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500150 sg_length = dpaa2_sg_get_len(sge);
151
152 if (i == 0) {
153 /* We build the skb around the first data buffer */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000154 skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500155 if (unlikely(!skb)) {
Ioana Radulescu729d79b2017-10-11 08:29:48 -0500156 /* Free the first SG entry now, since we already
157 * unmapped it and obtained the virtual address
158 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000159 free_pages((unsigned long)sg_vaddr, 0);
Ioana Radulescu729d79b2017-10-11 08:29:48 -0500160
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500161 /* We still need to subtract the buffers used
162 * by this FD from our software counter
163 */
164 while (!dpaa2_sg_is_final(&sgt[i]) &&
165 i < DPAA2_ETH_MAX_SG_ENTRIES)
166 i++;
167 break;
168 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500169
170 sg_offset = dpaa2_sg_get_offset(sge);
171 skb_reserve(skb, sg_offset);
172 skb_put(skb, sg_length);
173 } else {
174 /* Rest of the data buffers are stored as skb frags */
175 page = virt_to_page(sg_vaddr);
176 head_page = virt_to_head_page(sg_vaddr);
177
178 /* Offset in page (which may be compound).
179 * Data in subsequent SG entries is stored from the
180 * beginning of the buffer, so we don't need to add the
181 * sg_offset.
182 */
183 page_offset = ((unsigned long)sg_vaddr &
184 (PAGE_SIZE - 1)) +
185 (page_address(page) - page_address(head_page));
186
187 skb_add_rx_frag(skb, i - 1, head_page, page_offset,
188 sg_length, DPAA2_ETH_RX_BUF_SIZE);
189 }
190
191 if (dpaa2_sg_is_final(sge))
192 break;
193 }
194
Ioana Radulescub63baf72017-10-11 08:29:45 -0500195 WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
196
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500197 /* Count all data buffers + SG table buffer */
198 ch->buf_count -= i + 2;
199
200 return skb;
201}
202
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000203/* Free buffers acquired from the buffer pool or which were meant to
204 * be released in the pool
205 */
206static void free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array, int count)
207{
208 struct device *dev = priv->net_dev->dev.parent;
209 void *vaddr;
210 int i;
211
212 for (i = 0; i < count; i++) {
213 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000214 dma_unmap_page(dev, buf_array[i], DPAA2_ETH_RX_BUF_SIZE,
215 DMA_BIDIRECTIONAL);
216 free_pages((unsigned long)vaddr, 0);
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000217 }
218}
219
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000220static void xdp_release_buf(struct dpaa2_eth_priv *priv,
221 struct dpaa2_eth_channel *ch,
222 dma_addr_t addr)
223{
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300224 int retries = 0;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000225 int err;
226
227 ch->xdp.drop_bufs[ch->xdp.drop_cnt++] = addr;
228 if (ch->xdp.drop_cnt < DPAA2_ETH_BUFS_PER_CMD)
229 return;
230
231 while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
232 ch->xdp.drop_bufs,
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300233 ch->xdp.drop_cnt)) == -EBUSY) {
234 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
235 break;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000236 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300237 }
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000238
239 if (err) {
240 free_bufs(priv, ch->xdp.drop_bufs, ch->xdp.drop_cnt);
241 ch->buf_count -= ch->xdp.drop_cnt;
242 }
243
244 ch->xdp.drop_cnt = 0;
245}
246
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000247static int xdp_enqueue(struct dpaa2_eth_priv *priv, struct dpaa2_fd *fd,
248 void *buf_start, u16 queue_id)
249{
250 struct dpaa2_eth_fq *fq;
251 struct dpaa2_faead *faead;
252 u32 ctrl, frc;
253 int i, err;
254
255 /* Mark the egress frame hardware annotation area as valid */
256 frc = dpaa2_fd_get_frc(fd);
257 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
258 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
259
260 /* Instruct hardware to release the FD buffer directly into
261 * the buffer pool once transmission is completed, instead of
262 * sending a Tx confirmation frame to us
263 */
264 ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
265 faead = dpaa2_get_faead(buf_start, false);
266 faead->ctrl = cpu_to_le32(ctrl);
267 faead->conf_fqid = 0;
268
269 fq = &priv->fq[queue_id];
270 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +0000271 err = priv->enqueue(priv, fq, fd, 0);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000272 if (err != -EBUSY)
273 break;
274 }
275
276 return err;
277}
278
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000279static u32 run_xdp(struct dpaa2_eth_priv *priv,
280 struct dpaa2_eth_channel *ch,
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000281 struct dpaa2_eth_fq *rx_fq,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000282 struct dpaa2_fd *fd, void *vaddr)
283{
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000284 dma_addr_t addr = dpaa2_fd_get_addr(fd);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000285 struct rtnl_link_stats64 *percpu_stats;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000286 struct bpf_prog *xdp_prog;
287 struct xdp_buff xdp;
288 u32 xdp_act = XDP_PASS;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000289 int err;
290
291 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000292
293 rcu_read_lock();
294
295 xdp_prog = READ_ONCE(ch->xdp.prog);
296 if (!xdp_prog)
297 goto out;
298
299 xdp.data = vaddr + dpaa2_fd_get_offset(fd);
300 xdp.data_end = xdp.data + dpaa2_fd_get_len(fd);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000301 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000302 xdp_set_data_meta_invalid(&xdp);
Ioana Radulescud678be12019-03-01 17:47:24 +0000303 xdp.rxq = &ch->xdp_rxq;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000304
305 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
306
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000307 /* xdp.data pointer may have changed */
308 dpaa2_fd_set_offset(fd, xdp.data - vaddr);
309 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
310
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000311 switch (xdp_act) {
312 case XDP_PASS:
313 break;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000314 case XDP_TX:
315 err = xdp_enqueue(priv, fd, vaddr, rx_fq->flowid);
316 if (err) {
317 xdp_release_buf(priv, ch, addr);
318 percpu_stats->tx_errors++;
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000319 ch->stats.xdp_tx_err++;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000320 } else {
321 percpu_stats->tx_packets++;
322 percpu_stats->tx_bytes += dpaa2_fd_get_len(fd);
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000323 ch->stats.xdp_tx++;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000324 }
325 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000326 default:
327 bpf_warn_invalid_xdp_action(xdp_act);
Ioana Ciocoi Radulescuc1cb11b2018-11-29 08:43:40 +0000328 /* fall through */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000329 case XDP_ABORTED:
330 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
Ioana Ciocoi Radulescuc1cb11b2018-11-29 08:43:40 +0000331 /* fall through */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000332 case XDP_DROP:
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000333 xdp_release_buf(priv, ch, addr);
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000334 ch->stats.xdp_drop++;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000335 break;
Ioana Radulescud678be12019-03-01 17:47:24 +0000336 case XDP_REDIRECT:
337 dma_unmap_page(priv->net_dev->dev.parent, addr,
338 DPAA2_ETH_RX_BUF_SIZE, DMA_BIDIRECTIONAL);
339 ch->buf_count--;
340 xdp.data_hard_start = vaddr;
341 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
342 if (unlikely(err))
343 ch->stats.xdp_drop++;
344 else
345 ch->stats.xdp_redirect++;
346 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000347 }
348
Ioana Radulescud678be12019-03-01 17:47:24 +0000349 ch->xdp.res |= xdp_act;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000350out:
351 rcu_read_unlock();
352 return xdp_act;
353}
354
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500355/* Main Rx frame processing routine */
356static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
357 struct dpaa2_eth_channel *ch,
358 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000359 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500360{
361 dma_addr_t addr = dpaa2_fd_get_addr(fd);
362 u8 fd_format = dpaa2_fd_get_format(fd);
363 void *vaddr;
364 struct sk_buff *skb;
365 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500366 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500367 struct device *dev = priv->net_dev->dev.parent;
368 struct dpaa2_fas *fas;
Ioana Radulescud695e762017-06-06 10:00:35 -0500369 void *buf_data;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500370 u32 status = 0;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000371 u32 xdp_act;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500372
Ioana Radulescu56361872017-04-28 04:50:32 -0500373 /* Tracing point */
374 trace_dpaa2_rx_fd(priv->net_dev, fd);
375
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500376 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000377 dma_sync_single_for_cpu(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
Ioana Ciocoi Radulescu18c2e772018-11-26 16:27:32 +0000378 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500379
Ioana Radulescu54ce8912017-12-08 06:47:53 -0600380 fas = dpaa2_get_fas(vaddr, false);
Ioana Radulescud695e762017-06-06 10:00:35 -0500381 prefetch(fas);
382 buf_data = vaddr + dpaa2_fd_get_offset(fd);
383 prefetch(buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500384
385 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500386 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500387
388 if (fd_format == dpaa2_fd_single) {
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000389 xdp_act = run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000390 if (xdp_act != XDP_PASS) {
391 percpu_stats->rx_packets++;
392 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
393 return;
394 }
395
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000396 dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
397 DMA_BIDIRECTIONAL);
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +0000398 skb = build_linear_skb(ch, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500399 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000400 WARN_ON(priv->xdp_prog);
401
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000402 dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
403 DMA_BIDIRECTIONAL);
Ioana Radulescud695e762017-06-06 10:00:35 -0500404 skb = build_frag_skb(priv, ch, buf_data);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000405 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500406 percpu_extras->rx_sg_frames++;
407 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500408 } else {
409 /* We don't support any other format */
410 goto err_frame_format;
411 }
412
413 if (unlikely(!skb))
414 goto err_build_skb;
415
416 prefetch(skb->data);
417
Ioana Radulescu859f9982018-04-26 18:23:47 +0800418 /* Get the timestamp value */
419 if (priv->rx_tstamp) {
420 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
421 __le64 *ts = dpaa2_get_ts(vaddr, false);
422 u64 ns;
423
424 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
425
426 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
427 shhwtstamps->hwtstamp = ns_to_ktime(ns);
428 }
429
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500430 /* Check if we need to validate the L4 csum */
431 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500432 status = le32_to_cpu(fas->status);
433 validate_rx_csum(priv, status, skb);
434 }
435
436 skb->protocol = eth_type_trans(skb, priv->net_dev);
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000437 skb_record_rx_queue(skb, fq->flowid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500438
439 percpu_stats->rx_packets++;
440 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
441
Ioana Ciornei0a25d922019-03-25 13:42:39 +0000442 list_add_tail(&skb->list, ch->rx_list);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500443
444 return;
445
446err_build_skb:
447 free_rx_fd(priv, fd, vaddr);
448err_frame_format:
449 percpu_stats->rx_dropped++;
450}
451
452/* Consume all frames pull-dequeued into the store. This is the simplest way to
453 * make sure we don't accidentally issue another volatile dequeue which would
454 * overwrite (leak) frames already in the store.
455 *
456 * Observance of NAPI budget is not our concern, leaving that to the caller.
457 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000458static int consume_frames(struct dpaa2_eth_channel *ch,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000459 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500460{
461 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000462 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500463 struct dpaa2_dq *dq;
464 const struct dpaa2_fd *fd;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300465 int cleaned = 0, retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500466 int is_last;
467
468 do {
469 dq = dpaa2_io_store_next(ch->store, &is_last);
470 if (unlikely(!dq)) {
471 /* If we're here, we *must* have placed a
472 * volatile dequeue comnmand, so keep reading through
473 * the store until we get some sort of valid response
474 * token (either a valid frame or an "empty dequeue")
475 */
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300476 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
477 netdev_err_once(priv->net_dev,
478 "Unable to read a valid dequeue response\n");
479 return -ETIMEDOUT;
480 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500481 continue;
482 }
483
484 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600485 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500486
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000487 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500488 cleaned++;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300489 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500490 } while (!is_last);
491
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000492 if (!cleaned)
493 return 0;
494
495 fq->stats.frames += cleaned;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000496
497 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000498 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000499 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000500 if (src)
501 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000502
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500503 return cleaned;
504}
505
Ioana Radulescu859f9982018-04-26 18:23:47 +0800506/* Configure the egress frame annotation for timestamp update */
507static void enable_tx_tstamp(struct dpaa2_fd *fd, void *buf_start)
508{
509 struct dpaa2_faead *faead;
510 u32 ctrl, frc;
511
512 /* Mark the egress frame annotation area as valid */
513 frc = dpaa2_fd_get_frc(fd);
514 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
515
516 /* Set hardware annotation size */
517 ctrl = dpaa2_fd_get_ctrl(fd);
518 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
519
520 /* enable UPD (update prepanded data) bit in FAEAD field of
521 * hardware frame annotation area
522 */
523 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
524 faead = dpaa2_get_faead(buf_start, true);
525 faead->ctrl = cpu_to_le32(ctrl);
526}
527
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500528/* Create a frame descriptor based on a fragmented skb */
529static int build_sg_fd(struct dpaa2_eth_priv *priv,
530 struct sk_buff *skb,
531 struct dpaa2_fd *fd)
532{
533 struct device *dev = priv->net_dev->dev.parent;
534 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500535 dma_addr_t addr;
536 int nr_frags = skb_shinfo(skb)->nr_frags;
537 struct dpaa2_sg_entry *sgt;
538 int i, err;
539 int sgt_buf_size;
540 struct scatterlist *scl, *crt_scl;
541 int num_sg;
542 int num_dma_bufs;
543 struct dpaa2_eth_swa *swa;
544
545 /* Create and map scatterlist.
546 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
547 * to go beyond nr_frags+1.
548 * Note: We don't support chained scatterlists
549 */
550 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
551 return -EINVAL;
552
553 scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
554 if (unlikely(!scl))
555 return -ENOMEM;
556
557 sg_init_table(scl, nr_frags + 1);
558 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500559 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500560 if (unlikely(!num_dma_bufs)) {
561 err = -ENOMEM;
562 goto dma_map_sg_failed;
563 }
564
565 /* Prepare the HW SGT structure */
566 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500567 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Sebastian Andrzej Siewior90bc6d42019-06-07 21:20:37 +0200568 sgt_buf = napi_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500569 if (unlikely(!sgt_buf)) {
570 err = -ENOMEM;
571 goto sgt_buf_alloc_failed;
572 }
573 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500574 memset(sgt_buf, 0, sgt_buf_size);
575
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500576 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
577
578 /* Fill in the HW SGT structure.
579 *
580 * sgt_buf is zeroed out, so the following fields are implicit
581 * in all sgt entries:
582 * - offset is 0
583 * - format is 'dpaa2_sg_single'
584 */
585 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
586 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
587 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
588 }
589 dpaa2_sg_set_final(&sgt[i - 1], true);
590
591 /* Store the skb backpointer in the SGT buffer.
592 * Fit the scatterlist and the number of buffers alongside the
593 * skb backpointer in the software annotation area. We'll need
594 * all of them on Tx Conf.
595 */
596 swa = (struct dpaa2_eth_swa *)sgt_buf;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000597 swa->type = DPAA2_ETH_SWA_SG;
598 swa->sg.skb = skb;
599 swa->sg.scl = scl;
600 swa->sg.num_sg = num_sg;
601 swa->sg.sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500602
603 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500604 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500605 if (unlikely(dma_mapping_error(dev, addr))) {
606 err = -ENOMEM;
607 goto dma_map_single_failed;
608 }
609 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
610 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
611 dpaa2_fd_set_addr(fd, addr);
612 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000613 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500614
Ioana Radulescu859f9982018-04-26 18:23:47 +0800615 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
616 enable_tx_tstamp(fd, sgt_buf);
617
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500618 return 0;
619
620dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500621 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500622sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500623 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500624dma_map_sg_failed:
625 kfree(scl);
626 return err;
627}
628
629/* Create a frame descriptor based on a linear skb */
630static int build_single_fd(struct dpaa2_eth_priv *priv,
631 struct sk_buff *skb,
632 struct dpaa2_fd *fd)
633{
634 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600635 u8 *buffer_start, *aligned_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000636 struct dpaa2_eth_swa *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500637 dma_addr_t addr;
638
Ioana Radulescuc1636852017-12-08 06:47:58 -0600639 buffer_start = skb->data - dpaa2_eth_needed_headroom(priv, skb);
640
641 /* If there's enough room to align the FD address, do it.
642 * It will help hardware optimize accesses.
643 */
644 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
645 DPAA2_ETH_TX_BUF_ALIGN);
646 if (aligned_start >= skb->head)
647 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500648
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500649 /* Store a backpointer to the skb at the beginning of the buffer
650 * (in the private data area) such that we can release it
651 * on Tx confirm
652 */
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000653 swa = (struct dpaa2_eth_swa *)buffer_start;
654 swa->type = DPAA2_ETH_SWA_SINGLE;
655 swa->single.skb = skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500656
657 addr = dma_map_single(dev, buffer_start,
658 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500659 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500660 if (unlikely(dma_mapping_error(dev, addr)))
661 return -ENOMEM;
662
663 dpaa2_fd_set_addr(fd, addr);
664 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
665 dpaa2_fd_set_len(fd, skb->len);
666 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000667 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500668
Ioana Radulescu859f9982018-04-26 18:23:47 +0800669 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
670 enable_tx_tstamp(fd, buffer_start);
671
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500672 return 0;
673}
674
675/* FD freeing routine on the Tx path
676 *
677 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
678 * back-pointed to is also freed.
679 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
680 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500681 */
682static void free_tx_fd(const struct dpaa2_eth_priv *priv,
Ioana Radulescud678be12019-03-01 17:47:24 +0000683 struct dpaa2_eth_fq *fq,
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +0000684 const struct dpaa2_fd *fd, bool in_napi)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500685{
686 struct device *dev = priv->net_dev->dev.parent;
687 dma_addr_t fd_addr;
Ioana Radulescud678be12019-03-01 17:47:24 +0000688 struct sk_buff *skb = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500689 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500690 struct dpaa2_eth_swa *swa;
691 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescud678be12019-03-01 17:47:24 +0000692 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500693
694 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000695 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
696 swa = (struct dpaa2_eth_swa *)buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500697
698 if (fd_format == dpaa2_fd_single) {
Ioana Radulescud678be12019-03-01 17:47:24 +0000699 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
700 skb = swa->single.skb;
701 /* Accessing the skb buffer is safe before dma unmap,
702 * because we didn't map the actual skb shell.
703 */
704 dma_unmap_single(dev, fd_addr,
705 skb_tail_pointer(skb) - buffer_start,
706 DMA_BIDIRECTIONAL);
707 } else {
708 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
709 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
710 DMA_BIDIRECTIONAL);
711 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500712 } else if (fd_format == dpaa2_fd_sg) {
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000713 skb = swa->sg.skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500714
715 /* Unmap the scatterlist */
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000716 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
717 DMA_BIDIRECTIONAL);
718 kfree(swa->sg.scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500719
720 /* Unmap the SGT buffer */
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000721 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
Ioana Radulescub2718e62018-03-23 08:44:11 -0500722 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500723 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600724 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500725 return;
726 }
727
Ioana Radulescud678be12019-03-01 17:47:24 +0000728 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
729 fq->dq_frames++;
730 fq->dq_bytes += fd_len;
731 }
732
733 if (swa->type == DPAA2_ETH_SWA_XDP) {
734 xdp_return_frame(swa->xdp.xdpf);
735 return;
736 }
737
Ioana Radulescu859f9982018-04-26 18:23:47 +0800738 /* Get the timestamp value */
739 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
740 struct skb_shared_hwtstamps shhwtstamps;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000741 __le64 *ts = dpaa2_get_ts(buffer_start, true);
Ioana Radulescu859f9982018-04-26 18:23:47 +0800742 u64 ns;
743
744 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
745
746 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
747 shhwtstamps.hwtstamp = ns_to_ktime(ns);
748 skb_tstamp_tx(skb, &shhwtstamps);
749 }
750
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500751 /* Free SGT buffer allocated on tx */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500752 if (fd_format != dpaa2_fd_single)
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000753 skb_free_frag(buffer_start);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500754
755 /* Move on with skb release */
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +0000756 napi_consume_skb(skb, in_napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500757}
758
Ioana Radulescuc433db42017-06-06 10:00:26 -0500759static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500760{
761 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
762 struct dpaa2_fd fd;
763 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500764 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500765 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000766 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500767 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -0600768 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000769 u32 fd_len;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +0300770 u8 prio = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500771 int err, i;
772
773 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500774 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500775
Ioana Radulescu18c21462017-12-08 06:47:57 -0600776 needed_headroom = dpaa2_eth_needed_headroom(priv, skb);
777 if (skb_headroom(skb) < needed_headroom) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500778 struct sk_buff *ns;
779
Ioana Radulescu18c21462017-12-08 06:47:57 -0600780 ns = skb_realloc_headroom(skb, needed_headroom);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500781 if (unlikely(!ns)) {
782 percpu_stats->tx_dropped++;
783 goto err_alloc_headroom;
784 }
Ioana Radulescu6662b5e2017-12-08 06:47:55 -0600785 percpu_extras->tx_reallocs++;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800786
787 if (skb->sk)
788 skb_set_owner_w(ns, skb->sk);
789
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500790 dev_kfree_skb(skb);
791 skb = ns;
792 }
793
794 /* We'll be holding a back-reference to the skb until Tx Confirmation;
795 * we don't want that overwritten by a concurrent Tx with a cloned skb.
796 */
797 skb = skb_unshare(skb, GFP_ATOMIC);
798 if (unlikely(!skb)) {
799 /* skb_unshare() has already freed the skb */
800 percpu_stats->tx_dropped++;
801 return NETDEV_TX_OK;
802 }
803
804 /* Setup the FD fields */
805 memset(&fd, 0, sizeof(fd));
806
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500807 if (skb_is_nonlinear(skb)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500808 err = build_sg_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500809 percpu_extras->tx_sg_frames++;
810 percpu_extras->tx_sg_bytes += skb->len;
811 } else {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500812 err = build_single_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500813 }
814
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500815 if (unlikely(err)) {
816 percpu_stats->tx_dropped++;
817 goto err_build_fd;
818 }
819
Ioana Radulescu56361872017-04-28 04:50:32 -0500820 /* Tracing point */
821 trace_dpaa2_tx_fd(net_dev, &fd);
822
Ioana Radulescu537336c2017-12-21 06:33:20 -0600823 /* TxConf FQ selection relies on queue id from the stack.
824 * In case of a forwarded frame from another DPNI interface, we choose
825 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500826 */
Ioana Radulescu537336c2017-12-21 06:33:20 -0600827 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +0300828
829 if (net_dev->num_tc) {
830 prio = netdev_txq_to_tc(net_dev, queue_mapping);
831 /* Hardware interprets priority level 0 as being the highest,
832 * so we need to do a reverse mapping to the netdev tc index
833 */
834 prio = net_dev->num_tc - prio - 1;
835 /* We have only one FQ array entry for all Tx hardware queues
836 * with the same flow id (but different priority levels)
837 */
838 queue_mapping %= dpaa2_eth_queue_count(priv);
839 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500840 fq = &priv->fq[queue_mapping];
Ioana Ciornei8c838f52019-03-25 13:06:22 +0000841
842 fd_len = dpaa2_fd_get_len(&fd);
843 nq = netdev_get_tx_queue(net_dev, queue_mapping);
844 netdev_tx_sent_queue(nq, fd_len);
845
846 /* Everything that happens after this enqueues might race with
847 * the Tx confirmation callback for this frame
848 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500849 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Radulescuab1e6de2019-06-11 14:50:03 +0300850 err = priv->enqueue(priv, fq, &fd, prio);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500851 if (err != -EBUSY)
852 break;
853 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500854 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500855 if (unlikely(err < 0)) {
856 percpu_stats->tx_errors++;
857 /* Clean up everything, including freeing the skb */
Ioana Radulescud678be12019-03-01 17:47:24 +0000858 free_tx_fd(priv, fq, &fd, false);
Ioana Ciornei8c838f52019-03-25 13:06:22 +0000859 netdev_tx_completed_queue(nq, 1, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500860 } else {
861 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000862 percpu_stats->tx_bytes += fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500863 }
864
865 return NETDEV_TX_OK;
866
867err_build_fd:
868err_alloc_headroom:
869 dev_kfree_skb(skb);
870
871 return NETDEV_TX_OK;
872}
873
874/* Tx confirmation frame processing routine */
875static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +0000876 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500877 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000878 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500879{
880 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500881 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000882 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500883 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500884
Ioana Radulescu56361872017-04-28 04:50:32 -0500885 /* Tracing point */
886 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
887
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500888 percpu_extras = this_cpu_ptr(priv->percpu_extras);
889 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000890 percpu_extras->tx_conf_bytes += fd_len;
891
Ioana Radulescu39163c02017-06-06 10:00:39 -0500892 /* Check frame errors in the FD field */
893 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Radulescud678be12019-03-01 17:47:24 +0000894 free_tx_fd(priv, fq, fd, true);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500895
896 if (likely(!fd_errors))
897 return;
898
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600899 if (net_ratelimit())
900 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
901 fd_errors);
902
Ioana Radulescu39163c02017-06-06 10:00:39 -0500903 percpu_stats = this_cpu_ptr(priv->percpu_stats);
904 /* Tx-conf logically pertains to the egress path. */
905 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500906}
907
908static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
909{
910 int err;
911
912 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
913 DPNI_OFF_RX_L3_CSUM, enable);
914 if (err) {
915 netdev_err(priv->net_dev,
916 "dpni_set_offload(RX_L3_CSUM) failed\n");
917 return err;
918 }
919
920 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
921 DPNI_OFF_RX_L4_CSUM, enable);
922 if (err) {
923 netdev_err(priv->net_dev,
924 "dpni_set_offload(RX_L4_CSUM) failed\n");
925 return err;
926 }
927
928 return 0;
929}
930
931static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
932{
933 int err;
934
935 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
936 DPNI_OFF_TX_L3_CSUM, enable);
937 if (err) {
938 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
939 return err;
940 }
941
942 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
943 DPNI_OFF_TX_L4_CSUM, enable);
944 if (err) {
945 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
946 return err;
947 }
948
949 return 0;
950}
951
952/* Perform a single release command to add buffers
953 * to the specified buffer pool
954 */
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600955static int add_bufs(struct dpaa2_eth_priv *priv,
956 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500957{
958 struct device *dev = priv->net_dev->dev.parent;
959 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000960 struct page *page;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500961 dma_addr_t addr;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300962 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500963 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500964
965 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
966 /* Allocate buffer visible to WRIOP + skb shared info +
967 * alignment padding
968 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000969 /* allocate one page for each Rx buffer. WRIOP sees
970 * the entire page except for a tailroom reserved for
971 * skb shared info
972 */
973 page = dev_alloc_pages(0);
974 if (!page)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500975 goto err_alloc;
976
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000977 addr = dma_map_page(dev, page, 0, DPAA2_ETH_RX_BUF_SIZE,
978 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500979 if (unlikely(dma_mapping_error(dev, addr)))
980 goto err_map;
981
982 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -0500983
984 /* tracing point */
985 trace_dpaa2_eth_buf_seed(priv->net_dev,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000986 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
Ioana Radulescu56361872017-04-28 04:50:32 -0500987 addr, DPAA2_ETH_RX_BUF_SIZE,
988 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500989 }
990
991release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500992 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600993 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300994 buf_array, i)) == -EBUSY) {
995 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
996 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500997 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300998 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500999
1000 /* If release command failed, clean up and bail out;
1001 * not much else we can do about it
1002 */
1003 if (err) {
1004 free_bufs(priv, buf_array, i);
1005 return 0;
1006 }
1007
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001008 return i;
1009
1010err_map:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001011 __free_pages(page, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001012err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001013 /* If we managed to allocate at least some buffers,
1014 * release them to hardware
1015 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001016 if (i)
1017 goto release_bufs;
1018
1019 return 0;
1020}
1021
1022static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
1023{
1024 int i, j;
1025 int new_count;
1026
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001027 for (j = 0; j < priv->num_channels; j++) {
1028 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1029 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001030 new_count = add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001031 priv->channel[j]->buf_count += new_count;
1032
1033 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001034 return -ENOMEM;
1035 }
1036 }
1037 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001038
1039 return 0;
1040}
1041
1042/**
1043 * Drain the specified number of buffers from the DPNI's private buffer pool.
1044 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1045 */
1046static void drain_bufs(struct dpaa2_eth_priv *priv, int count)
1047{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001048 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001049 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001050 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001051
1052 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001053 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001054 buf_array, count);
1055 if (ret < 0) {
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001056 if (ret == -EBUSY &&
1057 retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1058 continue;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001059 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1060 return;
1061 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001062 free_bufs(priv, buf_array, ret);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001063 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001064 } while (ret);
1065}
1066
1067static void drain_pool(struct dpaa2_eth_priv *priv)
1068{
1069 int i;
1070
1071 drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1072 drain_bufs(priv, 1);
1073
1074 for (i = 0; i < priv->num_channels; i++)
1075 priv->channel[i]->buf_count = 0;
1076}
1077
1078/* Function is called from softirq context only, so we don't need to guard
1079 * the access to percpu count
1080 */
1081static int refill_pool(struct dpaa2_eth_priv *priv,
1082 struct dpaa2_eth_channel *ch,
1083 u16 bpid)
1084{
1085 int new_count;
1086
1087 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1088 return 0;
1089
1090 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001091 new_count = add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001092 if (unlikely(!new_count)) {
1093 /* Out of memory; abort for now, we'll try later on */
1094 break;
1095 }
1096 ch->buf_count += new_count;
1097 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1098
1099 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1100 return -ENOMEM;
1101
1102 return 0;
1103}
1104
1105static int pull_channel(struct dpaa2_eth_channel *ch)
1106{
1107 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001108 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001109
1110 /* Retry while portal is busy */
1111 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001112 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1113 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001114 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001115 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001116 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001117
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001118 ch->stats.dequeue_portal_busy += dequeues;
1119 if (unlikely(err))
1120 ch->stats.pull_err++;
1121
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001122 return err;
1123}
1124
1125/* NAPI poll routine
1126 *
1127 * Frames are dequeued from the QMan channel associated with this NAPI context.
1128 * Rx, Tx confirmation and (if configured) Rx error frames all count
1129 * towards the NAPI budget.
1130 */
1131static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1132{
1133 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001134 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001135 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001136 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1137 struct netdev_queue *nq;
1138 int store_cleaned, work_done;
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001139 struct list_head rx_list;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001140 int retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001141 int err;
1142
1143 ch = container_of(napi, struct dpaa2_eth_channel, napi);
Ioana Radulescud678be12019-03-01 17:47:24 +00001144 ch->xdp.res = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001145 priv = ch->priv;
1146
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001147 INIT_LIST_HEAD(&rx_list);
1148 ch->rx_list = &rx_list;
1149
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001150 do {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001151 err = pull_channel(ch);
1152 if (unlikely(err))
1153 break;
1154
1155 /* Refill pool if appropriate */
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001156 refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001157
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001158 store_cleaned = consume_frames(ch, &fq);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001159 if (store_cleaned <= 0)
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001160 break;
1161 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001162 rx_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001163 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001164 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001165 /* We have a single Tx conf FQ on this channel */
1166 txc_fq = fq;
1167 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001168
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001169 /* If we either consumed the whole NAPI budget with Rx frames
1170 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001171 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001172 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001173 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1174 work_done = budget;
1175 goto out;
1176 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001177 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001178
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001179 /* We didn't consume the entire budget, so finish napi and
1180 * re-enable data availability notifications
1181 */
1182 napi_complete_done(napi, rx_cleaned);
1183 do {
1184 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1185 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001186 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001187 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1188 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001189
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001190 work_done = max(rx_cleaned, 1);
1191
1192out:
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001193 netif_receive_skb_list(ch->rx_list);
1194
Ioana Radulescud678be12019-03-01 17:47:24 +00001195 if (txc_fq && txc_fq->dq_frames) {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001196 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1197 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1198 txc_fq->dq_bytes);
1199 txc_fq->dq_frames = 0;
1200 txc_fq->dq_bytes = 0;
1201 }
1202
Ioana Radulescud678be12019-03-01 17:47:24 +00001203 if (ch->xdp.res & XDP_REDIRECT)
1204 xdp_do_flush_map();
1205
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001206 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001207}
1208
1209static void enable_ch_napi(struct dpaa2_eth_priv *priv)
1210{
1211 struct dpaa2_eth_channel *ch;
1212 int i;
1213
1214 for (i = 0; i < priv->num_channels; i++) {
1215 ch = priv->channel[i];
1216 napi_enable(&ch->napi);
1217 }
1218}
1219
1220static void disable_ch_napi(struct dpaa2_eth_priv *priv)
1221{
1222 struct dpaa2_eth_channel *ch;
1223 int i;
1224
1225 for (i = 0; i < priv->num_channels; i++) {
1226 ch = priv->channel[i];
1227 napi_disable(&ch->napi);
1228 }
1229}
1230
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001231static void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv, bool enable)
1232{
1233 struct dpni_taildrop td = {0};
1234 int i, err;
1235
1236 if (priv->rx_td_enabled == enable)
1237 return;
1238
1239 td.enable = enable;
1240 td.threshold = DPAA2_ETH_TAILDROP_THRESH;
1241
1242 for (i = 0; i < priv->num_fqs; i++) {
1243 if (priv->fq[i].type != DPAA2_RX_FQ)
1244 continue;
1245 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1246 DPNI_CP_QUEUE, DPNI_QUEUE_RX, 0,
1247 priv->fq[i].flowid, &td);
1248 if (err) {
1249 netdev_err(priv->net_dev,
1250 "dpni_set_taildrop() failed\n");
1251 break;
1252 }
1253 }
1254
1255 priv->rx_td_enabled = enable;
1256}
1257
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001258static int link_state_update(struct dpaa2_eth_priv *priv)
1259{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001260 struct dpni_link_state state = {0};
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001261 bool tx_pause;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001262 int err;
1263
1264 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1265 if (unlikely(err)) {
1266 netdev_err(priv->net_dev,
1267 "dpni_get_link_state() failed\n");
1268 return err;
1269 }
1270
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001271 /* If Tx pause frame settings have changed, we need to update
1272 * Rx FQ taildrop configuration as well. We configure taildrop
1273 * only when pause frame generation is disabled.
1274 */
1275 tx_pause = !!(state.options & DPNI_LINK_OPT_PAUSE) ^
1276 !!(state.options & DPNI_LINK_OPT_ASYM_PAUSE);
1277 dpaa2_eth_set_rx_taildrop(priv, !tx_pause);
1278
Ioana Ciornei71947922019-10-31 01:18:31 +02001279 /* When we manage the MAC/PHY using phylink there is no need
1280 * to manually update the netif_carrier.
1281 */
1282 if (priv->mac)
1283 goto out;
1284
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001285 /* Chech link state; speed / duplex changes are not treated yet */
1286 if (priv->link_state.up == state.up)
Ioana Radulescucce629432019-08-28 17:08:14 +03001287 goto out;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001288
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001289 if (state.up) {
1290 netif_carrier_on(priv->net_dev);
1291 netif_tx_start_all_queues(priv->net_dev);
1292 } else {
1293 netif_tx_stop_all_queues(priv->net_dev);
1294 netif_carrier_off(priv->net_dev);
1295 }
1296
Ioana Radulescu77160af2017-06-06 10:00:28 -05001297 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001298 state.up ? "up" : "down");
1299
Ioana Radulescucce629432019-08-28 17:08:14 +03001300out:
1301 priv->link_state = state;
1302
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001303 return 0;
1304}
1305
1306static int dpaa2_eth_open(struct net_device *net_dev)
1307{
1308 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1309 int err;
1310
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001311 err = seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001312 if (err) {
1313 /* Not much to do; the buffer pool, though not filled up,
1314 * may still contain some buffers which would enable us
1315 * to limp on.
1316 */
1317 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001318 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001319 }
1320
Ioana Ciornei71947922019-10-31 01:18:31 +02001321 if (!priv->mac) {
1322 /* We'll only start the txqs when the link is actually ready;
1323 * make sure we don't race against the link up notification,
1324 * which may come immediately after dpni_enable();
1325 */
1326 netif_tx_stop_all_queues(net_dev);
1327
1328 /* Also, explicitly set carrier off, otherwise
1329 * netif_carrier_ok() will return true and cause 'ip link show'
1330 * to report the LOWER_UP flag, even though the link
1331 * notification wasn't even received.
1332 */
1333 netif_carrier_off(net_dev);
1334 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001335 enable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001336
1337 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1338 if (err < 0) {
1339 netdev_err(net_dev, "dpni_enable() failed\n");
1340 goto enable_err;
1341 }
1342
Ioana Ciornei71947922019-10-31 01:18:31 +02001343 if (!priv->mac) {
1344 /* If the DPMAC object has already processed the link up
1345 * interrupt, we have to learn the link state ourselves.
1346 */
1347 err = link_state_update(priv);
1348 if (err < 0) {
1349 netdev_err(net_dev, "Can't update link state\n");
1350 goto link_state_err;
1351 }
1352 } else {
1353 phylink_start(priv->mac->phylink);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001354 }
1355
1356 return 0;
1357
1358link_state_err:
1359enable_err:
1360 disable_ch_napi(priv);
1361 drain_pool(priv);
1362 return err;
1363}
1364
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001365/* Total number of in-flight frames on ingress queues */
1366static u32 ingress_fq_count(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001367{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001368 struct dpaa2_eth_fq *fq;
1369 u32 fcnt = 0, bcnt = 0, total = 0;
1370 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001371
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001372 for (i = 0; i < priv->num_fqs; i++) {
1373 fq = &priv->fq[i];
1374 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1375 if (err) {
1376 netdev_warn(priv->net_dev, "query_fq_count failed");
1377 break;
1378 }
1379 total += fcnt;
1380 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001381
1382 return total;
1383}
1384
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001385static void wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001386{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001387 int retries = 10;
1388 u32 pending;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001389
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001390 do {
1391 pending = ingress_fq_count(priv);
1392 if (pending)
1393 msleep(100);
1394 } while (pending && --retries);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001395}
1396
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001397#define DPNI_TX_PENDING_VER_MAJOR 7
1398#define DPNI_TX_PENDING_VER_MINOR 13
1399static void wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
1400{
1401 union dpni_statistics stats;
1402 int retries = 10;
1403 int err;
1404
1405 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1406 DPNI_TX_PENDING_VER_MINOR) < 0)
1407 goto out;
1408
1409 do {
1410 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1411 &stats);
1412 if (err)
1413 goto out;
1414 if (stats.page_6.tx_pending_frames == 0)
1415 return;
1416 } while (--retries);
1417
1418out:
1419 msleep(500);
1420}
1421
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001422static int dpaa2_eth_stop(struct net_device *net_dev)
1423{
1424 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001425 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001426 int retries = 10;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001427
Ioana Ciornei71947922019-10-31 01:18:31 +02001428 if (!priv->mac) {
1429 netif_tx_stop_all_queues(net_dev);
1430 netif_carrier_off(net_dev);
1431 } else {
1432 phylink_stop(priv->mac->phylink);
1433 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001434
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001435 /* On dpni_disable(), the MC firmware will:
1436 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1437 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1438 * of all in flight Tx frames is finished (and corresponding Tx conf
1439 * frames are enqueued back to software)
1440 *
1441 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1442 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1443 * and Tx conf queues are consumed on NAPI poll.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001444 */
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001445 wait_for_egress_fq_empty(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001446
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001447 do {
1448 dpni_disable(priv->mc_io, 0, priv->mc_token);
1449 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1450 if (dpni_enabled)
1451 /* Allow the hardware some slack */
1452 msleep(100);
1453 } while (dpni_enabled && --retries);
1454 if (!retries) {
1455 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1456 /* Must go on and disable NAPI nonetheless, so we don't crash at
1457 * the next "ifconfig up"
1458 */
1459 }
1460
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001461 wait_for_ingress_fq_empty(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001462 disable_ch_napi(priv);
1463
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001464 /* Empty the buffer pool */
1465 drain_pool(priv);
1466
1467 return 0;
1468}
1469
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001470static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1471{
1472 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1473 struct device *dev = net_dev->dev.parent;
1474 int err;
1475
1476 err = eth_mac_addr(net_dev, addr);
1477 if (err < 0) {
1478 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1479 return err;
1480 }
1481
1482 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1483 net_dev->dev_addr);
1484 if (err) {
1485 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1486 return err;
1487 }
1488
1489 return 0;
1490}
1491
1492/** Fill in counters maintained by the GPP driver. These may be different from
1493 * the hardware counters obtained by ethtool.
1494 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001495static void dpaa2_eth_get_stats(struct net_device *net_dev,
1496 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001497{
1498 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1499 struct rtnl_link_stats64 *percpu_stats;
1500 u64 *cpustats;
1501 u64 *netstats = (u64 *)stats;
1502 int i, j;
1503 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1504
1505 for_each_possible_cpu(i) {
1506 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1507 cpustats = (u64 *)percpu_stats;
1508 for (j = 0; j < num; j++)
1509 netstats[j] += cpustats[j];
1510 }
1511}
1512
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001513/* Copy mac unicast addresses from @net_dev to @priv.
1514 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1515 */
1516static void add_uc_hw_addr(const struct net_device *net_dev,
1517 struct dpaa2_eth_priv *priv)
1518{
1519 struct netdev_hw_addr *ha;
1520 int err;
1521
1522 netdev_for_each_uc_addr(ha, net_dev) {
1523 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1524 ha->addr);
1525 if (err)
1526 netdev_warn(priv->net_dev,
1527 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1528 ha->addr, err);
1529 }
1530}
1531
1532/* Copy mac multicast addresses from @net_dev to @priv
1533 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1534 */
1535static void add_mc_hw_addr(const struct net_device *net_dev,
1536 struct dpaa2_eth_priv *priv)
1537{
1538 struct netdev_hw_addr *ha;
1539 int err;
1540
1541 netdev_for_each_mc_addr(ha, net_dev) {
1542 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1543 ha->addr);
1544 if (err)
1545 netdev_warn(priv->net_dev,
1546 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1547 ha->addr, err);
1548 }
1549}
1550
1551static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1552{
1553 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1554 int uc_count = netdev_uc_count(net_dev);
1555 int mc_count = netdev_mc_count(net_dev);
1556 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1557 u32 options = priv->dpni_attrs.options;
1558 u16 mc_token = priv->mc_token;
1559 struct fsl_mc_io *mc_io = priv->mc_io;
1560 int err;
1561
1562 /* Basic sanity checks; these probably indicate a misconfiguration */
1563 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1564 netdev_info(net_dev,
1565 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1566 max_mac);
1567
1568 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1569 if (uc_count > max_mac) {
1570 netdev_info(net_dev,
1571 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1572 uc_count, max_mac);
1573 goto force_promisc;
1574 }
1575 if (mc_count + uc_count > max_mac) {
1576 netdev_info(net_dev,
1577 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1578 uc_count + mc_count, max_mac);
1579 goto force_mc_promisc;
1580 }
1581
1582 /* Adjust promisc settings due to flag combinations */
1583 if (net_dev->flags & IFF_PROMISC)
1584 goto force_promisc;
1585 if (net_dev->flags & IFF_ALLMULTI) {
1586 /* First, rebuild unicast filtering table. This should be done
1587 * in promisc mode, in order to avoid frame loss while we
1588 * progressively add entries to the table.
1589 * We don't know whether we had been in promisc already, and
1590 * making an MC call to find out is expensive; so set uc promisc
1591 * nonetheless.
1592 */
1593 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1594 if (err)
1595 netdev_warn(net_dev, "Can't set uc promisc\n");
1596
1597 /* Actual uc table reconstruction. */
1598 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1599 if (err)
1600 netdev_warn(net_dev, "Can't clear uc filters\n");
1601 add_uc_hw_addr(net_dev, priv);
1602
1603 /* Finally, clear uc promisc and set mc promisc as requested. */
1604 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1605 if (err)
1606 netdev_warn(net_dev, "Can't clear uc promisc\n");
1607 goto force_mc_promisc;
1608 }
1609
1610 /* Neither unicast, nor multicast promisc will be on... eventually.
1611 * For now, rebuild mac filtering tables while forcing both of them on.
1612 */
1613 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1614 if (err)
1615 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1616 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1617 if (err)
1618 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1619
1620 /* Actual mac filtering tables reconstruction */
1621 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1622 if (err)
1623 netdev_warn(net_dev, "Can't clear mac filters\n");
1624 add_mc_hw_addr(net_dev, priv);
1625 add_uc_hw_addr(net_dev, priv);
1626
1627 /* Now we can clear both ucast and mcast promisc, without risking
1628 * to drop legitimate frames anymore.
1629 */
1630 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1631 if (err)
1632 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1633 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1634 if (err)
1635 netdev_warn(net_dev, "Can't clear mcast promisc\n");
1636
1637 return;
1638
1639force_promisc:
1640 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1641 if (err)
1642 netdev_warn(net_dev, "Can't set ucast promisc\n");
1643force_mc_promisc:
1644 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1645 if (err)
1646 netdev_warn(net_dev, "Can't set mcast promisc\n");
1647}
1648
1649static int dpaa2_eth_set_features(struct net_device *net_dev,
1650 netdev_features_t features)
1651{
1652 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1653 netdev_features_t changed = features ^ net_dev->features;
1654 bool enable;
1655 int err;
1656
1657 if (changed & NETIF_F_RXCSUM) {
1658 enable = !!(features & NETIF_F_RXCSUM);
1659 err = set_rx_csum(priv, enable);
1660 if (err)
1661 return err;
1662 }
1663
1664 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1665 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
1666 err = set_tx_csum(priv, enable);
1667 if (err)
1668 return err;
1669 }
1670
1671 return 0;
1672}
1673
Ioana Radulescu859f9982018-04-26 18:23:47 +08001674static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1675{
1676 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1677 struct hwtstamp_config config;
1678
1679 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
1680 return -EFAULT;
1681
1682 switch (config.tx_type) {
1683 case HWTSTAMP_TX_OFF:
1684 priv->tx_tstamp = false;
1685 break;
1686 case HWTSTAMP_TX_ON:
1687 priv->tx_tstamp = true;
1688 break;
1689 default:
1690 return -ERANGE;
1691 }
1692
1693 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
1694 priv->rx_tstamp = false;
1695 } else {
1696 priv->rx_tstamp = true;
1697 /* TS is set for all frame types, not only those requested */
1698 config.rx_filter = HWTSTAMP_FILTER_ALL;
1699 }
1700
1701 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
1702 -EFAULT : 0;
1703}
1704
1705static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1706{
1707 if (cmd == SIOCSHWTSTAMP)
1708 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
1709
1710 return -EINVAL;
1711}
1712
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001713static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
1714{
1715 int mfl, linear_mfl;
1716
1717 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1718 linear_mfl = DPAA2_ETH_RX_BUF_SIZE - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001719 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001720
1721 if (mfl > linear_mfl) {
1722 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
1723 linear_mfl - VLAN_ETH_HLEN);
1724 return false;
1725 }
1726
1727 return true;
1728}
1729
1730static int set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
1731{
1732 int mfl, err;
1733
1734 /* We enforce a maximum Rx frame length based on MTU only if we have
1735 * an XDP program attached (in order to avoid Rx S/G frames).
1736 * Otherwise, we accept all incoming frames as long as they are not
1737 * larger than maximum size supported in hardware
1738 */
1739 if (has_xdp)
1740 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1741 else
1742 mfl = DPAA2_ETH_MFL;
1743
1744 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
1745 if (err) {
1746 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
1747 return err;
1748 }
1749
1750 return 0;
1751}
1752
1753static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
1754{
1755 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1756 int err;
1757
1758 if (!priv->xdp_prog)
1759 goto out;
1760
1761 if (!xdp_mtu_valid(priv, new_mtu))
1762 return -EINVAL;
1763
1764 err = set_rx_mfl(priv, new_mtu, true);
1765 if (err)
1766 return err;
1767
1768out:
1769 dev->mtu = new_mtu;
1770 return 0;
1771}
1772
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001773static int update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
1774{
1775 struct dpni_buffer_layout buf_layout = {0};
1776 int err;
1777
1778 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
1779 DPNI_QUEUE_RX, &buf_layout);
1780 if (err) {
1781 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
1782 return err;
1783 }
1784
1785 /* Reserve extra headroom for XDP header size changes */
1786 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
1787 (has_xdp ? XDP_PACKET_HEADROOM : 0);
1788 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
1789 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
1790 DPNI_QUEUE_RX, &buf_layout);
1791 if (err) {
1792 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
1793 return err;
1794 }
1795
1796 return 0;
1797}
1798
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001799static int setup_xdp(struct net_device *dev, struct bpf_prog *prog)
1800{
1801 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1802 struct dpaa2_eth_channel *ch;
1803 struct bpf_prog *old;
1804 bool up, need_update;
1805 int i, err;
1806
1807 if (prog && !xdp_mtu_valid(priv, dev->mtu))
1808 return -EINVAL;
1809
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001810 if (prog)
1811 bpf_prog_add(prog, priv->num_channels);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001812
1813 up = netif_running(dev);
1814 need_update = (!!priv->xdp_prog != !!prog);
1815
1816 if (up)
1817 dpaa2_eth_stop(dev);
1818
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001819 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
1820 * Also, when switching between xdp/non-xdp modes we need to reconfigure
1821 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
1822 * so we are sure no old format buffers will be used from now on.
1823 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001824 if (need_update) {
1825 err = set_rx_mfl(priv, dev->mtu, !!prog);
1826 if (err)
1827 goto out_err;
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001828 err = update_rx_buffer_headroom(priv, !!prog);
1829 if (err)
1830 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001831 }
1832
1833 old = xchg(&priv->xdp_prog, prog);
1834 if (old)
1835 bpf_prog_put(old);
1836
1837 for (i = 0; i < priv->num_channels; i++) {
1838 ch = priv->channel[i];
1839 old = xchg(&ch->xdp.prog, prog);
1840 if (old)
1841 bpf_prog_put(old);
1842 }
1843
1844 if (up) {
1845 err = dpaa2_eth_open(dev);
1846 if (err)
1847 return err;
1848 }
1849
1850 return 0;
1851
1852out_err:
1853 if (prog)
1854 bpf_prog_sub(prog, priv->num_channels);
1855 if (up)
1856 dpaa2_eth_open(dev);
1857
1858 return err;
1859}
1860
1861static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1862{
1863 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1864
1865 switch (xdp->command) {
1866 case XDP_SETUP_PROG:
1867 return setup_xdp(dev, xdp->prog);
1868 case XDP_QUERY_PROG:
1869 xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0;
1870 break;
1871 default:
1872 return -EINVAL;
1873 }
1874
1875 return 0;
1876}
1877
Ioana Radulescud678be12019-03-01 17:47:24 +00001878static int dpaa2_eth_xdp_xmit_frame(struct net_device *net_dev,
1879 struct xdp_frame *xdpf)
1880{
1881 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1882 struct device *dev = net_dev->dev.parent;
1883 struct rtnl_link_stats64 *percpu_stats;
1884 struct dpaa2_eth_drv_stats *percpu_extras;
1885 unsigned int needed_headroom;
1886 struct dpaa2_eth_swa *swa;
1887 struct dpaa2_eth_fq *fq;
1888 struct dpaa2_fd fd;
1889 void *buffer_start, *aligned_start;
1890 dma_addr_t addr;
1891 int err, i;
1892
1893 /* We require a minimum headroom to be able to transmit the frame.
1894 * Otherwise return an error and let the original net_device handle it
1895 */
1896 needed_headroom = dpaa2_eth_needed_headroom(priv, NULL);
1897 if (xdpf->headroom < needed_headroom)
1898 return -EINVAL;
1899
1900 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1901 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1902
1903 /* Setup the FD fields */
1904 memset(&fd, 0, sizeof(fd));
1905
1906 /* Align FD address, if possible */
1907 buffer_start = xdpf->data - needed_headroom;
1908 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
1909 DPAA2_ETH_TX_BUF_ALIGN);
1910 if (aligned_start >= xdpf->data - xdpf->headroom)
1911 buffer_start = aligned_start;
1912
1913 swa = (struct dpaa2_eth_swa *)buffer_start;
1914 /* fill in necessary fields here */
1915 swa->type = DPAA2_ETH_SWA_XDP;
1916 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
1917 swa->xdp.xdpf = xdpf;
1918
1919 addr = dma_map_single(dev, buffer_start,
1920 swa->xdp.dma_size,
1921 DMA_BIDIRECTIONAL);
1922 if (unlikely(dma_mapping_error(dev, addr))) {
1923 percpu_stats->tx_dropped++;
1924 return -ENOMEM;
1925 }
1926
1927 dpaa2_fd_set_addr(&fd, addr);
1928 dpaa2_fd_set_offset(&fd, xdpf->data - buffer_start);
1929 dpaa2_fd_set_len(&fd, xdpf->len);
1930 dpaa2_fd_set_format(&fd, dpaa2_fd_single);
1931 dpaa2_fd_set_ctrl(&fd, FD_CTRL_PTA);
1932
Ioana Ciocoi Radulescu64447502019-03-20 14:11:04 +00001933 fq = &priv->fq[smp_processor_id() % dpaa2_eth_queue_count(priv)];
Ioana Radulescud678be12019-03-01 17:47:24 +00001934 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
1935 err = priv->enqueue(priv, fq, &fd, 0);
1936 if (err != -EBUSY)
1937 break;
1938 }
1939 percpu_extras->tx_portal_busy += i;
1940 if (unlikely(err < 0)) {
1941 percpu_stats->tx_errors++;
1942 /* let the Rx device handle the cleanup */
1943 return err;
1944 }
1945
1946 percpu_stats->tx_packets++;
1947 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fd);
1948
1949 return 0;
1950}
1951
1952static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
1953 struct xdp_frame **frames, u32 flags)
1954{
1955 int drops = 0;
1956 int i, err;
1957
1958 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1959 return -EINVAL;
1960
1961 if (!netif_running(net_dev))
1962 return -ENETDOWN;
1963
1964 for (i = 0; i < n; i++) {
1965 struct xdp_frame *xdpf = frames[i];
1966
1967 err = dpaa2_eth_xdp_xmit_frame(net_dev, xdpf);
1968 if (err) {
1969 xdp_return_frame_rx_napi(xdpf);
1970 drops++;
1971 }
1972 }
1973
1974 return n - drops;
1975}
1976
Ioana Radulescu06d5b172019-06-11 14:50:01 +03001977static int update_xps(struct dpaa2_eth_priv *priv)
1978{
1979 struct net_device *net_dev = priv->net_dev;
1980 struct cpumask xps_mask;
1981 struct dpaa2_eth_fq *fq;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001982 int i, num_queues, netdev_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03001983 int err = 0;
1984
1985 num_queues = dpaa2_eth_queue_count(priv);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001986 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03001987
1988 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
1989 * queues, so only process those
1990 */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001991 for (i = 0; i < netdev_queues; i++) {
1992 fq = &priv->fq[i % num_queues];
Ioana Radulescu06d5b172019-06-11 14:50:01 +03001993
1994 cpumask_clear(&xps_mask);
1995 cpumask_set_cpu(fq->target_cpu, &xps_mask);
1996
1997 err = netif_set_xps_queue(net_dev, &xps_mask, i);
1998 if (err) {
1999 netdev_warn_once(net_dev, "Error setting XPS queue\n");
2000 break;
2001 }
2002 }
2003
2004 return err;
2005}
2006
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002007static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2008 enum tc_setup_type type, void *type_data)
2009{
2010 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2011 struct tc_mqprio_qopt *mqprio = type_data;
2012 u8 num_tc, num_queues;
2013 int i;
2014
2015 if (type != TC_SETUP_QDISC_MQPRIO)
2016 return -EINVAL;
2017
2018 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2019 num_queues = dpaa2_eth_queue_count(priv);
2020 num_tc = mqprio->num_tc;
2021
2022 if (num_tc == net_dev->num_tc)
2023 return 0;
2024
2025 if (num_tc > dpaa2_eth_tc_count(priv)) {
2026 netdev_err(net_dev, "Max %d traffic classes supported\n",
2027 dpaa2_eth_tc_count(priv));
2028 return -EINVAL;
2029 }
2030
2031 if (!num_tc) {
2032 netdev_reset_tc(net_dev);
2033 netif_set_real_num_tx_queues(net_dev, num_queues);
2034 goto out;
2035 }
2036
2037 netdev_set_num_tc(net_dev, num_tc);
2038 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2039
2040 for (i = 0; i < num_tc; i++)
2041 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2042
2043out:
2044 update_xps(priv);
2045
2046 return 0;
2047}
2048
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002049static const struct net_device_ops dpaa2_eth_ops = {
2050 .ndo_open = dpaa2_eth_open,
2051 .ndo_start_xmit = dpaa2_eth_tx,
2052 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002053 .ndo_set_mac_address = dpaa2_eth_set_addr,
2054 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002055 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2056 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08002057 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002058 .ndo_change_mtu = dpaa2_eth_change_mtu,
2059 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescud678be12019-03-01 17:47:24 +00002060 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002061 .ndo_setup_tc = dpaa2_eth_setup_tc,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002062};
2063
2064static void cdan_cb(struct dpaa2_io_notification_ctx *ctx)
2065{
2066 struct dpaa2_eth_channel *ch;
2067
2068 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002069
2070 /* Update NAPI statistics */
2071 ch->stats.cdan++;
2072
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002073 napi_schedule_irqoff(&ch->napi);
2074}
2075
2076/* Allocate and configure a DPCON object */
2077static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv)
2078{
2079 struct fsl_mc_device *dpcon;
2080 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002081 int err;
2082
2083 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2084 FSL_MC_POOL_DPCON, &dpcon);
2085 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002086 if (err == -ENXIO)
2087 err = -EPROBE_DEFER;
2088 else
2089 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2090 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002091 }
2092
2093 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2094 if (err) {
2095 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002096 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002097 }
2098
2099 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2100 if (err) {
2101 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002102 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002103 }
2104
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002105 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2106 if (err) {
2107 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002108 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002109 }
2110
2111 return dpcon;
2112
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002113close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002114 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002115free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002116 fsl_mc_object_free(dpcon);
2117
2118 return NULL;
2119}
2120
2121static void free_dpcon(struct dpaa2_eth_priv *priv,
2122 struct fsl_mc_device *dpcon)
2123{
2124 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2125 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2126 fsl_mc_object_free(dpcon);
2127}
2128
2129static struct dpaa2_eth_channel *
2130alloc_channel(struct dpaa2_eth_priv *priv)
2131{
2132 struct dpaa2_eth_channel *channel;
2133 struct dpcon_attr attr;
2134 struct device *dev = priv->net_dev->dev.parent;
2135 int err;
2136
2137 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2138 if (!channel)
2139 return NULL;
2140
2141 channel->dpcon = setup_dpcon(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002142 if (IS_ERR_OR_NULL(channel->dpcon)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002143 err = PTR_ERR_OR_ZERO(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002144 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002145 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002146
2147 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2148 &attr);
2149 if (err) {
2150 dev_err(dev, "dpcon_get_attributes() failed\n");
2151 goto err_get_attr;
2152 }
2153
2154 channel->dpcon_id = attr.id;
2155 channel->ch_id = attr.qbman_ch_id;
2156 channel->priv = priv;
2157
2158 return channel;
2159
2160err_get_attr:
2161 free_dpcon(priv, channel->dpcon);
2162err_setup:
2163 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002164 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002165}
2166
2167static void free_channel(struct dpaa2_eth_priv *priv,
2168 struct dpaa2_eth_channel *channel)
2169{
2170 free_dpcon(priv, channel->dpcon);
2171 kfree(channel);
2172}
2173
2174/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2175 * and register data availability notifications
2176 */
2177static int setup_dpio(struct dpaa2_eth_priv *priv)
2178{
2179 struct dpaa2_io_notification_ctx *nctx;
2180 struct dpaa2_eth_channel *channel;
2181 struct dpcon_notification_cfg dpcon_notif_cfg;
2182 struct device *dev = priv->net_dev->dev.parent;
2183 int i, err;
2184
2185 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2186 * many cores as possible, so we need one channel for each core
2187 * (unless there's fewer queues than cores, in which case the extra
2188 * channels would be wasted).
2189 * Allocate one channel per core and register it to the core's
2190 * affine DPIO. If not enough channels are available for all cores
2191 * or if some cores don't have an affine DPIO, there will be no
2192 * ingress frame processing on those cores.
2193 */
2194 cpumask_clear(&priv->dpio_cpumask);
2195 for_each_online_cpu(i) {
2196 /* Try to allocate a channel */
2197 channel = alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002198 if (IS_ERR_OR_NULL(channel)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002199 err = PTR_ERR_OR_ZERO(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002200 if (err != -EPROBE_DEFER)
2201 dev_info(dev,
2202 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002203 goto err_alloc_ch;
2204 }
2205
2206 priv->channel[priv->num_channels] = channel;
2207
2208 nctx = &channel->nctx;
2209 nctx->is_cdan = 1;
2210 nctx->cb = cdan_cb;
2211 nctx->id = channel->ch_id;
2212 nctx->desired_cpu = i;
2213
2214 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06002215 channel->dpio = dpaa2_io_service_select(i);
Ioana Ciornei47441f72018-12-10 16:50:19 +00002216 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002217 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002218 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002219 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002220 * none available for next cores either. Signal we want
2221 * to retry later, in case the DPIO devices weren't
2222 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002223 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002224 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002225 goto err_service_reg;
2226 }
2227
2228 /* Register DPCON notification with MC */
2229 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2230 dpcon_notif_cfg.priority = 0;
2231 dpcon_notif_cfg.user_ctx = nctx->qman64;
2232 err = dpcon_set_notification(priv->mc_io, 0,
2233 channel->dpcon->mc_handle,
2234 &dpcon_notif_cfg);
2235 if (err) {
2236 dev_err(dev, "dpcon_set_notification failed()\n");
2237 goto err_set_cdan;
2238 }
2239
2240 /* If we managed to allocate a channel and also found an affine
2241 * DPIO for this core, add it to the final mask
2242 */
2243 cpumask_set_cpu(i, &priv->dpio_cpumask);
2244 priv->num_channels++;
2245
2246 /* Stop if we already have enough channels to accommodate all
2247 * RX and TX conf queues
2248 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00002249 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002250 break;
2251 }
2252
2253 return 0;
2254
2255err_set_cdan:
Ioana Ciornei47441f72018-12-10 16:50:19 +00002256 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002257err_service_reg:
2258 free_channel(priv, channel);
2259err_alloc_ch:
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002260 if (err == -EPROBE_DEFER)
2261 return err;
2262
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002263 if (cpumask_empty(&priv->dpio_cpumask)) {
2264 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002265 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002266 }
2267
2268 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2269 cpumask_pr_args(&priv->dpio_cpumask));
2270
2271 return 0;
2272}
2273
2274static void free_dpio(struct dpaa2_eth_priv *priv)
2275{
Ioana Ciornei47441f72018-12-10 16:50:19 +00002276 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002277 struct dpaa2_eth_channel *ch;
Ioana Ciornei47441f72018-12-10 16:50:19 +00002278 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002279
2280 /* deregister CDAN notifications and free channels */
2281 for (i = 0; i < priv->num_channels; i++) {
2282 ch = priv->channel[i];
Ioana Ciornei47441f72018-12-10 16:50:19 +00002283 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002284 free_channel(priv, ch);
2285 }
2286}
2287
2288static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv,
2289 int cpu)
2290{
2291 struct device *dev = priv->net_dev->dev.parent;
2292 int i;
2293
2294 for (i = 0; i < priv->num_channels; i++)
2295 if (priv->channel[i]->nctx.desired_cpu == cpu)
2296 return priv->channel[i];
2297
2298 /* We should never get here. Issue a warning and return
2299 * the first channel, because it's still better than nothing
2300 */
2301 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2302
2303 return priv->channel[0];
2304}
2305
2306static void set_fq_affinity(struct dpaa2_eth_priv *priv)
2307{
2308 struct device *dev = priv->net_dev->dev.parent;
2309 struct dpaa2_eth_fq *fq;
2310 int rx_cpu, txc_cpu;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002311 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002312
2313 /* For each FQ, pick one channel/CPU to deliver frames to.
2314 * This may well change at runtime, either through irqbalance or
2315 * through direct user intervention.
2316 */
2317 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2318
2319 for (i = 0; i < priv->num_fqs; i++) {
2320 fq = &priv->fq[i];
2321 switch (fq->type) {
2322 case DPAA2_RX_FQ:
2323 fq->target_cpu = rx_cpu;
2324 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2325 if (rx_cpu >= nr_cpu_ids)
2326 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2327 break;
2328 case DPAA2_TX_CONF_FQ:
2329 fq->target_cpu = txc_cpu;
2330 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2331 if (txc_cpu >= nr_cpu_ids)
2332 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2333 break;
2334 default:
2335 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2336 }
2337 fq->channel = get_affine_channel(priv, fq->target_cpu);
2338 }
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002339
2340 update_xps(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002341}
2342
2343static void setup_fqs(struct dpaa2_eth_priv *priv)
2344{
2345 int i;
2346
2347 /* We have one TxConf FQ per Tx flow.
2348 * The number of Tx and Rx queues is the same.
2349 * Tx queues come first in the fq array.
2350 */
2351 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2352 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2353 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2354 priv->fq[priv->num_fqs++].flowid = (u16)i;
2355 }
2356
2357 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2358 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2359 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2360 priv->fq[priv->num_fqs++].flowid = (u16)i;
2361 }
2362
2363 /* For each FQ, decide on which core to process incoming frames */
2364 set_fq_affinity(priv);
2365}
2366
2367/* Allocate and configure one buffer pool for each interface */
2368static int setup_dpbp(struct dpaa2_eth_priv *priv)
2369{
2370 int err;
2371 struct fsl_mc_device *dpbp_dev;
2372 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002373 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002374
2375 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2376 &dpbp_dev);
2377 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002378 if (err == -ENXIO)
2379 err = -EPROBE_DEFER;
2380 else
2381 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002382 return err;
2383 }
2384
2385 priv->dpbp_dev = dpbp_dev;
2386
2387 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2388 &dpbp_dev->mc_handle);
2389 if (err) {
2390 dev_err(dev, "dpbp_open() failed\n");
2391 goto err_open;
2392 }
2393
Ioana Radulescud00defe2017-06-06 10:00:32 -05002394 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2395 if (err) {
2396 dev_err(dev, "dpbp_reset() failed\n");
2397 goto err_reset;
2398 }
2399
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002400 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2401 if (err) {
2402 dev_err(dev, "dpbp_enable() failed\n");
2403 goto err_enable;
2404 }
2405
2406 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002407 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002408 if (err) {
2409 dev_err(dev, "dpbp_get_attributes() failed\n");
2410 goto err_get_attr;
2411 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002412 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002413
2414 return 0;
2415
2416err_get_attr:
2417 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2418err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002419err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002420 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2421err_open:
2422 fsl_mc_object_free(dpbp_dev);
2423
2424 return err;
2425}
2426
2427static void free_dpbp(struct dpaa2_eth_priv *priv)
2428{
2429 drain_pool(priv);
2430 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2431 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2432 fsl_mc_object_free(priv->dpbp_dev);
2433}
2434
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002435static int set_buffer_layout(struct dpaa2_eth_priv *priv)
2436{
2437 struct device *dev = priv->net_dev->dev.parent;
2438 struct dpni_buffer_layout buf_layout = {0};
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002439 u16 rx_buf_align;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002440 int err;
2441
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002442 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2443 * version, this number is not always provided correctly on rev1.
2444 * We need to check for both alternatives in this situation.
2445 */
2446 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2447 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002448 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002449 else
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002450 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002451
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002452 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002453 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002454 buf_layout.pass_timestamp = true;
2455 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2456 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002457 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2458 DPNI_QUEUE_TX, &buf_layout);
2459 if (err) {
2460 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2461 return err;
2462 }
2463
2464 /* tx-confirm buffer */
Ioana Radulescu859f9982018-04-26 18:23:47 +08002465 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002466 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2467 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2468 if (err) {
2469 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2470 return err;
2471 }
2472
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002473 /* Now that we've set our tx buffer layout, retrieve the minimum
2474 * required tx data offset.
2475 */
2476 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2477 &priv->tx_data_offset);
2478 if (err) {
2479 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2480 return err;
2481 }
2482
2483 if ((priv->tx_data_offset % 64) != 0)
2484 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2485 priv->tx_data_offset);
2486
2487 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06002488 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002489 buf_layout.pass_parser_result = true;
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002490 buf_layout.data_align = rx_buf_align;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002491 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2492 buf_layout.private_data_size = 0;
2493 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2494 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2495 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08002496 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2497 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002498 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2499 DPNI_QUEUE_RX, &buf_layout);
2500 if (err) {
2501 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2502 return err;
2503 }
2504
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002505 return 0;
2506}
2507
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002508#define DPNI_ENQUEUE_FQID_VER_MAJOR 7
2509#define DPNI_ENQUEUE_FQID_VER_MINOR 9
2510
2511static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2512 struct dpaa2_eth_fq *fq,
2513 struct dpaa2_fd *fd, u8 prio)
2514{
2515 return dpaa2_io_service_enqueue_qd(fq->channel->dpio,
2516 priv->tx_qdid, prio,
2517 fq->tx_qdbin, fd);
2518}
2519
2520static inline int dpaa2_eth_enqueue_fq(struct dpaa2_eth_priv *priv,
2521 struct dpaa2_eth_fq *fq,
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002522 struct dpaa2_fd *fd, u8 prio)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002523{
2524 return dpaa2_io_service_enqueue_fq(fq->channel->dpio,
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002525 fq->tx_fqid[prio], fd);
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002526}
2527
2528static void set_enqueue_mode(struct dpaa2_eth_priv *priv)
2529{
2530 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2531 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2532 priv->enqueue = dpaa2_eth_enqueue_qd;
2533 else
2534 priv->enqueue = dpaa2_eth_enqueue_fq;
2535}
2536
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03002537static int set_pause(struct dpaa2_eth_priv *priv)
2538{
2539 struct device *dev = priv->net_dev->dev.parent;
2540 struct dpni_link_cfg link_cfg = {0};
2541 int err;
2542
2543 /* Get the default link options so we don't override other flags */
2544 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2545 if (err) {
2546 dev_err(dev, "dpni_get_link_cfg() failed\n");
2547 return err;
2548 }
2549
2550 /* By default, enable both Rx and Tx pause frames */
2551 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
2552 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
2553 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2554 if (err) {
2555 dev_err(dev, "dpni_set_link_cfg() failed\n");
2556 return err;
2557 }
2558
2559 priv->link_state.options = link_cfg.options;
2560
2561 return 0;
2562}
2563
Ioana Radulescua690af4f2019-10-16 10:36:23 +03002564static void update_tx_fqids(struct dpaa2_eth_priv *priv)
2565{
2566 struct dpni_queue_id qid = {0};
2567 struct dpaa2_eth_fq *fq;
2568 struct dpni_queue queue;
2569 int i, j, err;
2570
2571 /* We only use Tx FQIDs for FQID-based enqueue, so check
2572 * if DPNI version supports it before updating FQIDs
2573 */
2574 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2575 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2576 return;
2577
2578 for (i = 0; i < priv->num_fqs; i++) {
2579 fq = &priv->fq[i];
2580 if (fq->type != DPAA2_TX_CONF_FQ)
2581 continue;
2582 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2583 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2584 DPNI_QUEUE_TX, j, fq->flowid,
2585 &queue, &qid);
2586 if (err)
2587 goto out_err;
2588
2589 fq->tx_fqid[j] = qid.fqid;
2590 if (fq->tx_fqid[j] == 0)
2591 goto out_err;
2592 }
2593 }
2594
2595 priv->enqueue = dpaa2_eth_enqueue_fq;
2596
2597 return;
2598
2599out_err:
2600 netdev_info(priv->net_dev,
2601 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
2602 priv->enqueue = dpaa2_eth_enqueue_qd;
2603}
2604
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002605/* Configure the DPNI object this interface is associated with */
2606static int setup_dpni(struct fsl_mc_device *ls_dev)
2607{
2608 struct device *dev = &ls_dev->dev;
2609 struct dpaa2_eth_priv *priv;
2610 struct net_device *net_dev;
2611 int err;
2612
2613 net_dev = dev_get_drvdata(dev);
2614 priv = netdev_priv(net_dev);
2615
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002616 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05002617 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002618 if (err) {
2619 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002620 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002621 }
2622
Ioana Radulescu311cffa2018-03-23 08:44:09 -05002623 /* Check if we can work with this DPNI object */
2624 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
2625 &priv->dpni_ver_minor);
2626 if (err) {
2627 dev_err(dev, "dpni_get_api_version() failed\n");
2628 goto close;
2629 }
2630 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
2631 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
2632 priv->dpni_ver_major, priv->dpni_ver_minor,
2633 DPNI_VER_MAJOR, DPNI_VER_MINOR);
2634 err = -ENOTSUPP;
2635 goto close;
2636 }
2637
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002638 ls_dev->mc_io = priv->mc_io;
2639 ls_dev->mc_handle = priv->mc_token;
2640
2641 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2642 if (err) {
2643 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002644 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002645 }
2646
2647 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
2648 &priv->dpni_attrs);
2649 if (err) {
2650 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002651 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002652 }
2653
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002654 err = set_buffer_layout(priv);
2655 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002656 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002657
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002658 set_enqueue_mode(priv);
2659
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03002660 /* Enable pause frame support */
2661 if (dpaa2_eth_has_pause_support(priv)) {
2662 err = set_pause(priv);
2663 if (err)
2664 goto close;
2665 }
2666
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002667 priv->cls_rules = devm_kzalloc(dev, sizeof(struct dpaa2_eth_cls_rule) *
2668 dpaa2_eth_fs_count(priv), GFP_KERNEL);
2669 if (!priv->cls_rules)
2670 goto close;
2671
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002672 return 0;
2673
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002674close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002675 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002676
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002677 return err;
2678}
2679
2680static void free_dpni(struct dpaa2_eth_priv *priv)
2681{
2682 int err;
2683
2684 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2685 if (err)
2686 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
2687 err);
2688
2689 dpni_close(priv->mc_io, 0, priv->mc_token);
2690}
2691
2692static int setup_rx_flow(struct dpaa2_eth_priv *priv,
2693 struct dpaa2_eth_fq *fq)
2694{
2695 struct device *dev = priv->net_dev->dev.parent;
2696 struct dpni_queue queue;
2697 struct dpni_queue_id qid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002698 int err;
2699
2700 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2701 DPNI_QUEUE_RX, 0, fq->flowid, &queue, &qid);
2702 if (err) {
2703 dev_err(dev, "dpni_get_queue(RX) failed\n");
2704 return err;
2705 }
2706
2707 fq->fqid = qid.fqid;
2708
2709 queue.destination.id = fq->channel->dpcon_id;
2710 queue.destination.type = DPNI_DEST_DPCON;
2711 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002712 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002713 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2714 DPNI_QUEUE_RX, 0, fq->flowid,
Ioana Radulescu16fa1cf2019-05-23 17:38:22 +03002715 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002716 &queue);
2717 if (err) {
2718 dev_err(dev, "dpni_set_queue(RX) failed\n");
2719 return err;
2720 }
2721
Ioana Radulescud678be12019-03-01 17:47:24 +00002722 /* xdp_rxq setup */
2723 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
2724 fq->flowid);
2725 if (err) {
2726 dev_err(dev, "xdp_rxq_info_reg failed\n");
2727 return err;
2728 }
2729
2730 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
2731 MEM_TYPE_PAGE_ORDER0, NULL);
2732 if (err) {
2733 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
2734 return err;
2735 }
2736
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002737 return 0;
2738}
2739
2740static int setup_tx_flow(struct dpaa2_eth_priv *priv,
2741 struct dpaa2_eth_fq *fq)
2742{
2743 struct device *dev = priv->net_dev->dev.parent;
2744 struct dpni_queue queue;
2745 struct dpni_queue_id qid;
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002746 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002747
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002748 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
2749 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2750 DPNI_QUEUE_TX, i, fq->flowid,
2751 &queue, &qid);
2752 if (err) {
2753 dev_err(dev, "dpni_get_queue(TX) failed\n");
2754 return err;
2755 }
2756 fq->tx_fqid[i] = qid.fqid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002757 }
2758
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002759 /* All Tx queues belonging to the same flowid have the same qdbin */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002760 fq->tx_qdbin = qid.qdbin;
2761
2762 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2763 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2764 &queue, &qid);
2765 if (err) {
2766 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
2767 return err;
2768 }
2769
2770 fq->fqid = qid.fqid;
2771
2772 queue.destination.id = fq->channel->dpcon_id;
2773 queue.destination.type = DPNI_DEST_DPCON;
2774 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002775 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002776 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2777 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2778 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2779 &queue);
2780 if (err) {
2781 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
2782 return err;
2783 }
2784
2785 return 0;
2786}
2787
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002788/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002789static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002790 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002791 /* L2 header */
2792 .rxnfc_field = RXH_L2DA,
2793 .cls_prot = NET_PROT_ETH,
2794 .cls_field = NH_FLD_ETH_DA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002795 .id = DPAA2_ETH_DIST_ETHDST,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002796 .size = 6,
2797 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002798 .cls_prot = NET_PROT_ETH,
2799 .cls_field = NH_FLD_ETH_SA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002800 .id = DPAA2_ETH_DIST_ETHSRC,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002801 .size = 6,
2802 }, {
2803 /* This is the last ethertype field parsed:
2804 * depending on frame format, it can be the MAC ethertype
2805 * or the VLAN etype.
2806 */
2807 .cls_prot = NET_PROT_ETH,
2808 .cls_field = NH_FLD_ETH_TYPE,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002809 .id = DPAA2_ETH_DIST_ETHTYPE,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002810 .size = 2,
2811 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002812 /* VLAN header */
2813 .rxnfc_field = RXH_VLAN,
2814 .cls_prot = NET_PROT_VLAN,
2815 .cls_field = NH_FLD_VLAN_TCI,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002816 .id = DPAA2_ETH_DIST_VLAN,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002817 .size = 2,
2818 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002819 /* IP header */
2820 .rxnfc_field = RXH_IP_SRC,
2821 .cls_prot = NET_PROT_IP,
2822 .cls_field = NH_FLD_IP_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002823 .id = DPAA2_ETH_DIST_IPSRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002824 .size = 4,
2825 }, {
2826 .rxnfc_field = RXH_IP_DST,
2827 .cls_prot = NET_PROT_IP,
2828 .cls_field = NH_FLD_IP_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002829 .id = DPAA2_ETH_DIST_IPDST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002830 .size = 4,
2831 }, {
2832 .rxnfc_field = RXH_L3_PROTO,
2833 .cls_prot = NET_PROT_IP,
2834 .cls_field = NH_FLD_IP_PROTO,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002835 .id = DPAA2_ETH_DIST_IPPROTO,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002836 .size = 1,
2837 }, {
2838 /* Using UDP ports, this is functionally equivalent to raw
2839 * byte pairs from L4 header.
2840 */
2841 .rxnfc_field = RXH_L4_B_0_1,
2842 .cls_prot = NET_PROT_UDP,
2843 .cls_field = NH_FLD_UDP_PORT_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002844 .id = DPAA2_ETH_DIST_L4SRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002845 .size = 2,
2846 }, {
2847 .rxnfc_field = RXH_L4_B_2_3,
2848 .cls_prot = NET_PROT_UDP,
2849 .cls_field = NH_FLD_UDP_PORT_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002850 .id = DPAA2_ETH_DIST_L4DST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002851 .size = 2,
2852 },
2853};
2854
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002855/* Configure the Rx hash key using the legacy API */
2856static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2857{
2858 struct device *dev = priv->net_dev->dev.parent;
2859 struct dpni_rx_tc_dist_cfg dist_cfg;
2860 int err;
2861
2862 memset(&dist_cfg, 0, sizeof(dist_cfg));
2863
2864 dist_cfg.key_cfg_iova = key;
2865 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2866 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
2867
2868 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg);
2869 if (err)
2870 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
2871
2872 return err;
2873}
2874
2875/* Configure the Rx hash key using the new API */
2876static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2877{
2878 struct device *dev = priv->net_dev->dev.parent;
2879 struct dpni_rx_dist_cfg dist_cfg;
2880 int err;
2881
2882 memset(&dist_cfg, 0, sizeof(dist_cfg));
2883
2884 dist_cfg.key_cfg_iova = key;
2885 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2886 dist_cfg.enable = 1;
2887
2888 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2889 if (err)
2890 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
2891
2892 return err;
2893}
2894
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002895/* Configure the Rx flow classification key */
2896static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2897{
2898 struct device *dev = priv->net_dev->dev.parent;
2899 struct dpni_rx_dist_cfg dist_cfg;
2900 int err;
2901
2902 memset(&dist_cfg, 0, sizeof(dist_cfg));
2903
2904 dist_cfg.key_cfg_iova = key;
2905 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2906 dist_cfg.enable = 1;
2907
2908 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2909 if (err)
2910 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
2911
2912 return err;
2913}
2914
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002915/* Size of the Rx flow classification key */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002916int dpaa2_eth_cls_key_size(u64 fields)
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002917{
2918 int i, size = 0;
2919
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002920 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2921 if (!(fields & dist_fields[i].id))
2922 continue;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002923 size += dist_fields[i].size;
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002924 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002925
2926 return size;
2927}
2928
2929/* Offset of header field in Rx classification key */
2930int dpaa2_eth_cls_fld_off(int prot, int field)
2931{
2932 int i, off = 0;
2933
2934 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2935 if (dist_fields[i].cls_prot == prot &&
2936 dist_fields[i].cls_field == field)
2937 return off;
2938 off += dist_fields[i].size;
2939 }
2940
2941 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
2942 return 0;
2943}
2944
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002945/* Prune unused fields from the classification rule.
2946 * Used when masking is not supported
2947 */
2948void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
2949{
2950 int off = 0, new_off = 0;
2951 int i, size;
2952
2953 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2954 size = dist_fields[i].size;
2955 if (dist_fields[i].id & fields) {
2956 memcpy(key_mem + new_off, key_mem + off, size);
2957 new_off += size;
2958 }
2959 off += size;
2960 }
2961}
2962
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002963/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002964 * flags is a combination of RXH_ bits
2965 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00002966static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
2967 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002968{
2969 struct device *dev = net_dev->dev.parent;
2970 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2971 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002972 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002973 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002974 u8 *dma_mem;
2975 int i;
2976 int err = 0;
2977
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002978 memset(&cls_cfg, 0, sizeof(cls_cfg));
2979
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002980 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002981 struct dpkg_extract *key =
2982 &cls_cfg.extracts[cls_cfg.num_extracts];
2983
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002984 /* For both Rx hashing and classification keys
2985 * we set only the selected fields.
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002986 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002987 if (!(flags & dist_fields[i].id))
2988 continue;
2989 if (type == DPAA2_ETH_RX_DIST_HASH)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002990 rx_hash_fields |= dist_fields[i].rxnfc_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002991
2992 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
2993 dev_err(dev, "error adding key extraction rule, too many rules?\n");
2994 return -E2BIG;
2995 }
2996
2997 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002998 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002999 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003000 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003001 cls_cfg.num_extracts++;
3002 }
3003
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05003004 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003005 if (!dma_mem)
3006 return -ENOMEM;
3007
3008 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3009 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003010 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003011 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003012 }
3013
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003014 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003015 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3016 DMA_TO_DEVICE);
3017 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003018 dev_err(dev, "DMA mapping failed\n");
3019 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003020 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003021 }
3022
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003023 if (type == DPAA2_ETH_RX_DIST_HASH) {
3024 if (dpaa2_eth_has_legacy_dist(priv))
3025 err = config_legacy_hash_key(priv, key_iova);
3026 else
3027 err = config_hash_key(priv, key_iova);
3028 } else {
3029 err = config_cls_key(priv, key_iova);
3030 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003031
3032 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3033 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003034 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003035 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003036
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003037free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003038 kfree(dma_mem);
3039 return err;
3040}
3041
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003042int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3043{
3044 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003045 u64 key = 0;
3046 int i;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003047
3048 if (!dpaa2_eth_hash_enabled(priv))
3049 return -EOPNOTSUPP;
3050
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003051 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3052 if (dist_fields[i].rxnfc_field & flags)
3053 key |= dist_fields[i].id;
3054
3055 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003056}
3057
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003058int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3059{
3060 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3061}
3062
3063static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003064{
3065 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003066 int err;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003067
3068 /* Check if we actually support Rx flow classification */
3069 if (dpaa2_eth_has_legacy_dist(priv)) {
3070 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3071 return -EOPNOTSUPP;
3072 }
3073
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003074 if (!dpaa2_eth_fs_enabled(priv)) {
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003075 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3076 return -EOPNOTSUPP;
3077 }
3078
3079 if (!dpaa2_eth_hash_enabled(priv)) {
3080 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3081 return -EOPNOTSUPP;
3082 }
3083
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003084 /* If there is no support for masking in the classification table,
3085 * we don't set a default key, as it will depend on the rules
3086 * added by the user at runtime.
3087 */
3088 if (!dpaa2_eth_fs_mask_enabled(priv))
3089 goto out;
3090
3091 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003092 if (err)
3093 return err;
3094
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003095out:
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003096 priv->rx_cls_enabled = 1;
3097
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003098 return 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003099}
3100
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003101/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3102 * frame queues and channels
3103 */
3104static int bind_dpni(struct dpaa2_eth_priv *priv)
3105{
3106 struct net_device *net_dev = priv->net_dev;
3107 struct device *dev = net_dev->dev.parent;
3108 struct dpni_pools_cfg pools_params;
3109 struct dpni_error_cfg err_cfg;
3110 int err = 0;
3111 int i;
3112
3113 pools_params.num_dpbp = 1;
3114 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3115 pools_params.pools[0].backup_pool = 0;
3116 pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE;
3117 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3118 if (err) {
3119 dev_err(dev, "dpni_set_pools() failed\n");
3120 return err;
3121 }
3122
Ioana Radulescu227686b2018-07-27 09:12:59 -05003123 /* have the interface implicitly distribute traffic based on
3124 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003125 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05003126 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003127 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05003128 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003129
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003130 /* Configure the flow classification key; it includes all
3131 * supported header fields and cannot be modified at runtime
3132 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003133 err = dpaa2_eth_set_default_cls(priv);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003134 if (err && err != -EOPNOTSUPP)
3135 dev_err(dev, "Failed to configure Rx classification key\n");
3136
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003137 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05003138 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003139 err_cfg.set_frame_annotation = 1;
3140 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3141 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3142 &err_cfg);
3143 if (err) {
3144 dev_err(dev, "dpni_set_errors_behavior failed\n");
3145 return err;
3146 }
3147
3148 /* Configure Rx and Tx conf queues to generate CDANs */
3149 for (i = 0; i < priv->num_fqs; i++) {
3150 switch (priv->fq[i].type) {
3151 case DPAA2_RX_FQ:
3152 err = setup_rx_flow(priv, &priv->fq[i]);
3153 break;
3154 case DPAA2_TX_CONF_FQ:
3155 err = setup_tx_flow(priv, &priv->fq[i]);
3156 break;
3157 default:
3158 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3159 return -EINVAL;
3160 }
3161 if (err)
3162 return err;
3163 }
3164
3165 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3166 DPNI_QUEUE_TX, &priv->tx_qdid);
3167 if (err) {
3168 dev_err(dev, "dpni_get_qdid() failed\n");
3169 return err;
3170 }
3171
3172 return 0;
3173}
3174
3175/* Allocate rings for storing incoming frame descriptors */
3176static int alloc_rings(struct dpaa2_eth_priv *priv)
3177{
3178 struct net_device *net_dev = priv->net_dev;
3179 struct device *dev = net_dev->dev.parent;
3180 int i;
3181
3182 for (i = 0; i < priv->num_channels; i++) {
3183 priv->channel[i]->store =
3184 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3185 if (!priv->channel[i]->store) {
3186 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3187 goto err_ring;
3188 }
3189 }
3190
3191 return 0;
3192
3193err_ring:
3194 for (i = 0; i < priv->num_channels; i++) {
3195 if (!priv->channel[i]->store)
3196 break;
3197 dpaa2_io_store_destroy(priv->channel[i]->store);
3198 }
3199
3200 return -ENOMEM;
3201}
3202
3203static void free_rings(struct dpaa2_eth_priv *priv)
3204{
3205 int i;
3206
3207 for (i = 0; i < priv->num_channels; i++)
3208 dpaa2_io_store_destroy(priv->channel[i]->store);
3209}
3210
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003211static int set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003212{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003213 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003214 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003215 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003216 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003217
3218 /* Get firmware address, if any */
3219 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3220 if (err) {
3221 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3222 return err;
3223 }
3224
3225 /* Get DPNI attributes address, if any */
3226 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3227 dpni_mac_addr);
3228 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003229 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003230 return err;
3231 }
3232
3233 /* First check if firmware has any address configured by bootloader */
3234 if (!is_zero_ether_addr(mac_addr)) {
3235 /* If the DPMAC addr != DPNI addr, update it */
3236 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
3237 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
3238 priv->mc_token,
3239 mac_addr);
3240 if (err) {
3241 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3242 return err;
3243 }
3244 }
3245 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
3246 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003247 /* No MAC address configured, fill in net_dev->dev_addr
3248 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003249 */
3250 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003251 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
3252
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003253 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3254 net_dev->dev_addr);
3255 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003256 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003257 return err;
3258 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003259
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003260 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
3261 * practical purposes, this will be our "permanent" mac address,
3262 * at least until the next reboot. This move will also permit
3263 * register_netdevice() to properly fill up net_dev->perm_addr.
3264 */
3265 net_dev->addr_assign_type = NET_ADDR_PERM;
3266 } else {
3267 /* NET_ADDR_PERM is default, all we have to do is
3268 * fill in the device addr.
3269 */
3270 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
3271 }
3272
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003273 return 0;
3274}
3275
3276static int netdev_init(struct net_device *net_dev)
3277{
3278 struct device *dev = net_dev->dev.parent;
3279 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003280 u32 options = priv->dpni_attrs.options;
3281 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003282 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003283 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003284 int err;
3285
3286 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003287 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003288
3289 err = set_mac_addr(priv);
3290 if (err)
3291 return err;
3292
3293 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003294 eth_broadcast_addr(bcast_addr);
3295 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3296 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003297 dev_err(dev, "dpni_add_mac_addr() failed\n");
3298 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003299 }
3300
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05003301 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003302 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05003303 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05003304 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05003305 if (err) {
3306 dev_err(dev, "dpni_set_max_frame_length() failed\n");
3307 return err;
3308 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003309
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003310 /* Set actual number of queues in the net device */
3311 num_queues = dpaa2_eth_queue_count(priv);
3312 err = netif_set_real_num_tx_queues(net_dev, num_queues);
3313 if (err) {
3314 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
3315 return err;
3316 }
3317 err = netif_set_real_num_rx_queues(net_dev, num_queues);
3318 if (err) {
3319 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
3320 return err;
3321 }
3322
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003323 /* Capabilities listing */
3324 supported |= IFF_LIVE_ADDR_CHANGE;
3325
3326 if (options & DPNI_OPT_NO_MAC_FILTER)
3327 not_supported |= IFF_UNICAST_FLT;
3328 else
3329 supported |= IFF_UNICAST_FLT;
3330
3331 net_dev->priv_flags |= supported;
3332 net_dev->priv_flags &= ~not_supported;
3333
3334 /* Features */
3335 net_dev->features = NETIF_F_RXCSUM |
3336 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3337 NETIF_F_SG | NETIF_F_HIGHDMA |
3338 NETIF_F_LLTX;
3339 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003340
3341 return 0;
3342}
3343
3344static int poll_link_state(void *arg)
3345{
3346 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
3347 int err;
3348
3349 while (!kthread_should_stop()) {
3350 err = link_state_update(priv);
3351 if (unlikely(err))
3352 return err;
3353
3354 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
3355 }
3356
3357 return 0;
3358}
3359
Ioana Ciornei71947922019-10-31 01:18:31 +02003360static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
3361{
3362 struct fsl_mc_device *dpni_dev, *dpmac_dev;
3363 struct dpaa2_mac *mac;
3364 int err;
3365
3366 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
3367 dpmac_dev = fsl_mc_get_endpoint(dpni_dev);
3368 if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
3369 return 0;
3370
3371 if (dpaa2_mac_is_type_fixed(dpmac_dev, priv->mc_io))
3372 return 0;
3373
3374 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
3375 if (!mac)
3376 return -ENOMEM;
3377
3378 mac->mc_dev = dpmac_dev;
3379 mac->mc_io = priv->mc_io;
3380 mac->net_dev = priv->net_dev;
3381
3382 err = dpaa2_mac_connect(mac);
3383 if (err) {
3384 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint\n");
3385 kfree(mac);
3386 return err;
3387 }
3388 priv->mac = mac;
3389
3390 return 0;
3391}
3392
3393static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
3394{
3395 if (!priv->mac)
3396 return;
3397
3398 dpaa2_mac_disconnect(priv->mac);
3399 kfree(priv->mac);
3400 priv->mac = NULL;
3401}
3402
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003403static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
3404{
Ioana Radulescu112197d2017-10-11 08:29:49 -05003405 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003406 struct device *dev = (struct device *)arg;
3407 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
3408 struct net_device *net_dev = dev_get_drvdata(dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02003409 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003410 int err;
3411
3412 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
3413 DPNI_IRQ_INDEX, &status);
3414 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003415 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05003416 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003417 }
3418
Ioana Radulescu112197d2017-10-11 08:29:49 -05003419 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003420 link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003421
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02003422 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
Florin Chiculita8398b372019-10-16 10:36:22 +03003423 set_mac_addr(netdev_priv(net_dev));
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02003424 update_tx_fqids(priv);
Ioana Ciornei71947922019-10-31 01:18:31 +02003425
3426 rtnl_lock();
3427 if (priv->mac)
3428 dpaa2_eth_disconnect_mac(priv);
3429 else
3430 dpaa2_eth_connect_mac(priv);
3431 rtnl_unlock();
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02003432 }
Florin Chiculita8398b372019-10-16 10:36:22 +03003433
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003434 return IRQ_HANDLED;
3435}
3436
3437static int setup_irqs(struct fsl_mc_device *ls_dev)
3438{
3439 int err = 0;
3440 struct fsl_mc_device_irq *irq;
3441
3442 err = fsl_mc_allocate_irqs(ls_dev);
3443 if (err) {
3444 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
3445 return err;
3446 }
3447
3448 irq = ls_dev->irqs[0];
3449 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05003450 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003451 IRQF_NO_SUSPEND | IRQF_ONESHOT,
3452 dev_name(&ls_dev->dev), &ls_dev->dev);
3453 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003454 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003455 goto free_mc_irq;
3456 }
3457
3458 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
Florin Chiculita8398b372019-10-16 10:36:22 +03003459 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
3460 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003461 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003462 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003463 goto free_irq;
3464 }
3465
3466 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
3467 DPNI_IRQ_INDEX, 1);
3468 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003469 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003470 goto free_irq;
3471 }
3472
3473 return 0;
3474
3475free_irq:
3476 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
3477free_mc_irq:
3478 fsl_mc_free_irqs(ls_dev);
3479
3480 return err;
3481}
3482
3483static void add_ch_napi(struct dpaa2_eth_priv *priv)
3484{
3485 int i;
3486 struct dpaa2_eth_channel *ch;
3487
3488 for (i = 0; i < priv->num_channels; i++) {
3489 ch = priv->channel[i];
3490 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
3491 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
3492 NAPI_POLL_WEIGHT);
3493 }
3494}
3495
3496static void del_ch_napi(struct dpaa2_eth_priv *priv)
3497{
3498 int i;
3499 struct dpaa2_eth_channel *ch;
3500
3501 for (i = 0; i < priv->num_channels; i++) {
3502 ch = priv->channel[i];
3503 netif_napi_del(&ch->napi);
3504 }
3505}
3506
3507static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
3508{
3509 struct device *dev;
3510 struct net_device *net_dev = NULL;
3511 struct dpaa2_eth_priv *priv = NULL;
3512 int err = 0;
3513
3514 dev = &dpni_dev->dev;
3515
3516 /* Net device */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03003517 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003518 if (!net_dev) {
3519 dev_err(dev, "alloc_etherdev_mq() failed\n");
3520 return -ENOMEM;
3521 }
3522
3523 SET_NETDEV_DEV(net_dev, dev);
3524 dev_set_drvdata(dev, net_dev);
3525
3526 priv = netdev_priv(net_dev);
3527 priv->net_dev = net_dev;
3528
Ioana Radulescu08eb2392017-05-24 07:13:27 -05003529 priv->iommu_domain = iommu_get_domain_for_dev(dev);
3530
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003531 /* Obtain a MC portal */
3532 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3533 &priv->mc_io);
3534 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05003535 if (err == -ENXIO)
3536 err = -EPROBE_DEFER;
3537 else
3538 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003539 goto err_portal_alloc;
3540 }
3541
3542 /* MC objects initialization and configuration */
3543 err = setup_dpni(dpni_dev);
3544 if (err)
3545 goto err_dpni_setup;
3546
3547 err = setup_dpio(priv);
3548 if (err)
3549 goto err_dpio_setup;
3550
3551 setup_fqs(priv);
3552
3553 err = setup_dpbp(priv);
3554 if (err)
3555 goto err_dpbp_setup;
3556
3557 err = bind_dpni(priv);
3558 if (err)
3559 goto err_bind;
3560
3561 /* Add a NAPI context for each channel */
3562 add_ch_napi(priv);
3563
3564 /* Percpu statistics */
3565 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
3566 if (!priv->percpu_stats) {
3567 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
3568 err = -ENOMEM;
3569 goto err_alloc_percpu_stats;
3570 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003571 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
3572 if (!priv->percpu_extras) {
3573 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
3574 err = -ENOMEM;
3575 goto err_alloc_percpu_extras;
3576 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003577
3578 err = netdev_init(net_dev);
3579 if (err)
3580 goto err_netdev_init;
3581
3582 /* Configure checksum offload based on current interface flags */
3583 err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
3584 if (err)
3585 goto err_csum;
3586
3587 err = set_tx_csum(priv, !!(net_dev->features &
3588 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
3589 if (err)
3590 goto err_csum;
3591
3592 err = alloc_rings(priv);
3593 if (err)
3594 goto err_alloc_rings;
3595
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003596 err = setup_irqs(dpni_dev);
3597 if (err) {
3598 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
3599 priv->poll_thread = kthread_run(poll_link_state, priv,
3600 "%s_poll_link", net_dev->name);
3601 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003602 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003603 goto err_poll_thread;
3604 }
3605 priv->do_link_poll = true;
3606 }
3607
Ioana Ciornei71947922019-10-31 01:18:31 +02003608 err = dpaa2_eth_connect_mac(priv);
3609 if (err)
3610 goto err_connect_mac;
3611
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003612 err = register_netdev(net_dev);
3613 if (err < 0) {
3614 dev_err(dev, "register_netdev() failed\n");
3615 goto err_netdev_reg;
3616 }
3617
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003618#ifdef CONFIG_DEBUG_FS
3619 dpaa2_dbg_add(priv);
3620#endif
3621
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003622 dev_info(dev, "Probed interface %s\n", net_dev->name);
3623 return 0;
3624
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003625err_netdev_reg:
Ioana Ciornei71947922019-10-31 01:18:31 +02003626 dpaa2_eth_disconnect_mac(priv);
3627err_connect_mac:
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003628 if (priv->do_link_poll)
3629 kthread_stop(priv->poll_thread);
3630 else
3631 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003632err_poll_thread:
3633 free_rings(priv);
3634err_alloc_rings:
3635err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003636err_netdev_init:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003637 free_percpu(priv->percpu_extras);
3638err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003639 free_percpu(priv->percpu_stats);
3640err_alloc_percpu_stats:
3641 del_ch_napi(priv);
3642err_bind:
3643 free_dpbp(priv);
3644err_dpbp_setup:
3645 free_dpio(priv);
3646err_dpio_setup:
3647 free_dpni(priv);
3648err_dpni_setup:
3649 fsl_mc_portal_free(priv->mc_io);
3650err_portal_alloc:
3651 dev_set_drvdata(dev, NULL);
3652 free_netdev(net_dev);
3653
3654 return err;
3655}
3656
3657static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
3658{
3659 struct device *dev;
3660 struct net_device *net_dev;
3661 struct dpaa2_eth_priv *priv;
3662
3663 dev = &ls_dev->dev;
3664 net_dev = dev_get_drvdata(dev);
3665 priv = netdev_priv(net_dev);
3666
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003667#ifdef CONFIG_DEBUG_FS
3668 dpaa2_dbg_remove(priv);
3669#endif
Ioana Ciornei71947922019-10-31 01:18:31 +02003670 rtnl_lock();
3671 dpaa2_eth_disconnect_mac(priv);
3672 rtnl_unlock();
3673
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003674 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003675
3676 if (priv->do_link_poll)
3677 kthread_stop(priv->poll_thread);
3678 else
3679 fsl_mc_free_irqs(ls_dev);
3680
3681 free_rings(priv);
3682 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003683 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003684
3685 del_ch_napi(priv);
3686 free_dpbp(priv);
3687 free_dpio(priv);
3688 free_dpni(priv);
3689
3690 fsl_mc_portal_free(priv->mc_io);
3691
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003692 free_netdev(net_dev);
3693
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05003694 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05003695
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003696 return 0;
3697}
3698
3699static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
3700 {
3701 .vendor = FSL_MC_VENDOR_FREESCALE,
3702 .obj_type = "dpni",
3703 },
3704 { .vendor = 0x0 }
3705};
3706MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
3707
3708static struct fsl_mc_driver dpaa2_eth_driver = {
3709 .driver = {
3710 .name = KBUILD_MODNAME,
3711 .owner = THIS_MODULE,
3712 },
3713 .probe = dpaa2_eth_probe,
3714 .remove = dpaa2_eth_remove,
3715 .match_id_table = dpaa2_eth_match_id_table
3716};
3717
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003718static int __init dpaa2_eth_driver_init(void)
3719{
3720 int err;
3721
3722 dpaa2_eth_dbg_init();
3723 err = fsl_mc_driver_register(&dpaa2_eth_driver);
3724 if (err) {
3725 dpaa2_eth_dbg_exit();
3726 return err;
3727 }
3728
3729 return 0;
3730}
3731
3732static void __exit dpaa2_eth_driver_exit(void)
3733{
3734 dpaa2_eth_dbg_exit();
3735 fsl_mc_driver_unregister(&dpaa2_eth_driver);
3736}
3737
3738module_init(dpaa2_eth_driver_init);
3739module_exit(dpaa2_eth_driver_exit);