blob: d3cfed4a54b34220dbedfe6e5cf5e92be9c0eed5 [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.
3 * Copyright 2016-2017 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 Radulescu6e2387e2017-04-28 04:50:29 -050089 dma_unmap_single(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
90 DMA_FROM_DEVICE);
91
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050092 skb_free_frag(sg_vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050093 if (dpaa2_sg_is_final(&sgt[i]))
94 break;
95 }
96
97free_buf:
98 skb_free_frag(vaddr);
99}
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
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +0000112 skb = build_skb(fd_vaddr, DPAA2_ETH_SKB_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 Radulescu6e2387e2017-04-28 04:50:29 -0500147 dma_unmap_single(dev, sg_addr, DPAA2_ETH_RX_BUF_SIZE,
148 DMA_FROM_DEVICE);
149
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 */
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +0000154 skb = build_skb(sg_vaddr, DPAA2_ETH_SKB_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 */
159 skb_free_frag(sg_vaddr);
160
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 Radulescu7e273a82018-11-26 16:27:29 +0000203static u32 run_xdp(struct dpaa2_eth_priv *priv,
204 struct dpaa2_eth_channel *ch,
205 struct dpaa2_fd *fd, void *vaddr)
206{
207 struct bpf_prog *xdp_prog;
208 struct xdp_buff xdp;
209 u32 xdp_act = XDP_PASS;
210
211 rcu_read_lock();
212
213 xdp_prog = READ_ONCE(ch->xdp.prog);
214 if (!xdp_prog)
215 goto out;
216
217 xdp.data = vaddr + dpaa2_fd_get_offset(fd);
218 xdp.data_end = xdp.data + dpaa2_fd_get_len(fd);
219 xdp.data_hard_start = xdp.data;
220 xdp_set_data_meta_invalid(&xdp);
221
222 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
223
224 switch (xdp_act) {
225 case XDP_PASS:
226 break;
227 default:
228 bpf_warn_invalid_xdp_action(xdp_act);
229 case XDP_ABORTED:
230 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
231 case XDP_DROP:
232 ch->buf_count--;
233 free_rx_fd(priv, fd, vaddr);
234 break;
235 }
236
237out:
238 rcu_read_unlock();
239 return xdp_act;
240}
241
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500242/* Main Rx frame processing routine */
243static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
244 struct dpaa2_eth_channel *ch,
245 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000246 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500247{
248 dma_addr_t addr = dpaa2_fd_get_addr(fd);
249 u8 fd_format = dpaa2_fd_get_format(fd);
250 void *vaddr;
251 struct sk_buff *skb;
252 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500253 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500254 struct device *dev = priv->net_dev->dev.parent;
255 struct dpaa2_fas *fas;
Ioana Radulescud695e762017-06-06 10:00:35 -0500256 void *buf_data;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500257 u32 status = 0;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000258 u32 xdp_act;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500259
Ioana Radulescu56361872017-04-28 04:50:32 -0500260 /* Tracing point */
261 trace_dpaa2_rx_fd(priv->net_dev, fd);
262
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500263 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500264 dma_unmap_single(dev, addr, DPAA2_ETH_RX_BUF_SIZE, DMA_FROM_DEVICE);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500265
Ioana Radulescu54ce8912017-12-08 06:47:53 -0600266 fas = dpaa2_get_fas(vaddr, false);
Ioana Radulescud695e762017-06-06 10:00:35 -0500267 prefetch(fas);
268 buf_data = vaddr + dpaa2_fd_get_offset(fd);
269 prefetch(buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500270
271 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500272 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500273
274 if (fd_format == dpaa2_fd_single) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000275 xdp_act = run_xdp(priv, ch, (struct dpaa2_fd *)fd, vaddr);
276 if (xdp_act != XDP_PASS) {
277 percpu_stats->rx_packets++;
278 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
279 return;
280 }
281
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +0000282 skb = build_linear_skb(ch, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500283 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000284 WARN_ON(priv->xdp_prog);
285
Ioana Radulescud695e762017-06-06 10:00:35 -0500286 skb = build_frag_skb(priv, ch, buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500287 skb_free_frag(vaddr);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500288 percpu_extras->rx_sg_frames++;
289 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500290 } else {
291 /* We don't support any other format */
292 goto err_frame_format;
293 }
294
295 if (unlikely(!skb))
296 goto err_build_skb;
297
298 prefetch(skb->data);
299
Ioana Radulescu859f9982018-04-26 18:23:47 +0800300 /* Get the timestamp value */
301 if (priv->rx_tstamp) {
302 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
303 __le64 *ts = dpaa2_get_ts(vaddr, false);
304 u64 ns;
305
306 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
307
308 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
309 shhwtstamps->hwtstamp = ns_to_ktime(ns);
310 }
311
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500312 /* Check if we need to validate the L4 csum */
313 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500314 status = le32_to_cpu(fas->status);
315 validate_rx_csum(priv, status, skb);
316 }
317
318 skb->protocol = eth_type_trans(skb, priv->net_dev);
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000319 skb_record_rx_queue(skb, fq->flowid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500320
321 percpu_stats->rx_packets++;
322 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
323
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000324 napi_gro_receive(&ch->napi, skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500325
326 return;
327
328err_build_skb:
329 free_rx_fd(priv, fd, vaddr);
330err_frame_format:
331 percpu_stats->rx_dropped++;
332}
333
334/* Consume all frames pull-dequeued into the store. This is the simplest way to
335 * make sure we don't accidentally issue another volatile dequeue which would
336 * overwrite (leak) frames already in the store.
337 *
338 * Observance of NAPI budget is not our concern, leaving that to the caller.
339 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000340static int consume_frames(struct dpaa2_eth_channel *ch,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000341 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500342{
343 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000344 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500345 struct dpaa2_dq *dq;
346 const struct dpaa2_fd *fd;
347 int cleaned = 0;
348 int is_last;
349
350 do {
351 dq = dpaa2_io_store_next(ch->store, &is_last);
352 if (unlikely(!dq)) {
353 /* If we're here, we *must* have placed a
354 * volatile dequeue comnmand, so keep reading through
355 * the store until we get some sort of valid response
356 * token (either a valid frame or an "empty dequeue")
357 */
358 continue;
359 }
360
361 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600362 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500363
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000364 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500365 cleaned++;
366 } while (!is_last);
367
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000368 if (!cleaned)
369 return 0;
370
371 fq->stats.frames += cleaned;
372 ch->stats.frames += cleaned;
373
374 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000375 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000376 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000377 if (src)
378 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000379
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500380 return cleaned;
381}
382
Ioana Radulescu859f9982018-04-26 18:23:47 +0800383/* Configure the egress frame annotation for timestamp update */
384static void enable_tx_tstamp(struct dpaa2_fd *fd, void *buf_start)
385{
386 struct dpaa2_faead *faead;
387 u32 ctrl, frc;
388
389 /* Mark the egress frame annotation area as valid */
390 frc = dpaa2_fd_get_frc(fd);
391 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
392
393 /* Set hardware annotation size */
394 ctrl = dpaa2_fd_get_ctrl(fd);
395 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
396
397 /* enable UPD (update prepanded data) bit in FAEAD field of
398 * hardware frame annotation area
399 */
400 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
401 faead = dpaa2_get_faead(buf_start, true);
402 faead->ctrl = cpu_to_le32(ctrl);
403}
404
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500405/* Create a frame descriptor based on a fragmented skb */
406static int build_sg_fd(struct dpaa2_eth_priv *priv,
407 struct sk_buff *skb,
408 struct dpaa2_fd *fd)
409{
410 struct device *dev = priv->net_dev->dev.parent;
411 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500412 dma_addr_t addr;
413 int nr_frags = skb_shinfo(skb)->nr_frags;
414 struct dpaa2_sg_entry *sgt;
415 int i, err;
416 int sgt_buf_size;
417 struct scatterlist *scl, *crt_scl;
418 int num_sg;
419 int num_dma_bufs;
420 struct dpaa2_eth_swa *swa;
421
422 /* Create and map scatterlist.
423 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
424 * to go beyond nr_frags+1.
425 * Note: We don't support chained scatterlists
426 */
427 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
428 return -EINVAL;
429
430 scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
431 if (unlikely(!scl))
432 return -ENOMEM;
433
434 sg_init_table(scl, nr_frags + 1);
435 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500436 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500437 if (unlikely(!num_dma_bufs)) {
438 err = -ENOMEM;
439 goto dma_map_sg_failed;
440 }
441
442 /* Prepare the HW SGT structure */
443 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500444 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500445 sgt_buf = netdev_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500446 if (unlikely(!sgt_buf)) {
447 err = -ENOMEM;
448 goto sgt_buf_alloc_failed;
449 }
450 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500451 memset(sgt_buf, 0, sgt_buf_size);
452
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500453 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
454
455 /* Fill in the HW SGT structure.
456 *
457 * sgt_buf is zeroed out, so the following fields are implicit
458 * in all sgt entries:
459 * - offset is 0
460 * - format is 'dpaa2_sg_single'
461 */
462 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
463 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
464 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
465 }
466 dpaa2_sg_set_final(&sgt[i - 1], true);
467
468 /* Store the skb backpointer in the SGT buffer.
469 * Fit the scatterlist and the number of buffers alongside the
470 * skb backpointer in the software annotation area. We'll need
471 * all of them on Tx Conf.
472 */
473 swa = (struct dpaa2_eth_swa *)sgt_buf;
474 swa->skb = skb;
475 swa->scl = scl;
476 swa->num_sg = num_sg;
Ioana Radulescub2718e62018-03-23 08:44:11 -0500477 swa->sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500478
479 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500480 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500481 if (unlikely(dma_mapping_error(dev, addr))) {
482 err = -ENOMEM;
483 goto dma_map_single_failed;
484 }
485 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
486 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
487 dpaa2_fd_set_addr(fd, addr);
488 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000489 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500490
Ioana Radulescu859f9982018-04-26 18:23:47 +0800491 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
492 enable_tx_tstamp(fd, sgt_buf);
493
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500494 return 0;
495
496dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500497 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500498sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500499 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500500dma_map_sg_failed:
501 kfree(scl);
502 return err;
503}
504
505/* Create a frame descriptor based on a linear skb */
506static int build_single_fd(struct dpaa2_eth_priv *priv,
507 struct sk_buff *skb,
508 struct dpaa2_fd *fd)
509{
510 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600511 u8 *buffer_start, *aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500512 struct sk_buff **skbh;
513 dma_addr_t addr;
514
Ioana Radulescuc1636852017-12-08 06:47:58 -0600515 buffer_start = skb->data - dpaa2_eth_needed_headroom(priv, skb);
516
517 /* If there's enough room to align the FD address, do it.
518 * It will help hardware optimize accesses.
519 */
520 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
521 DPAA2_ETH_TX_BUF_ALIGN);
522 if (aligned_start >= skb->head)
523 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500524
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500525 /* Store a backpointer to the skb at the beginning of the buffer
526 * (in the private data area) such that we can release it
527 * on Tx confirm
528 */
529 skbh = (struct sk_buff **)buffer_start;
530 *skbh = skb;
531
532 addr = dma_map_single(dev, buffer_start,
533 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500534 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500535 if (unlikely(dma_mapping_error(dev, addr)))
536 return -ENOMEM;
537
538 dpaa2_fd_set_addr(fd, addr);
539 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
540 dpaa2_fd_set_len(fd, skb->len);
541 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000542 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500543
Ioana Radulescu859f9982018-04-26 18:23:47 +0800544 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
545 enable_tx_tstamp(fd, buffer_start);
546
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500547 return 0;
548}
549
550/* FD freeing routine on the Tx path
551 *
552 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
553 * back-pointed to is also freed.
554 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
555 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500556 */
557static void free_tx_fd(const struct dpaa2_eth_priv *priv,
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600558 const struct dpaa2_fd *fd)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500559{
560 struct device *dev = priv->net_dev->dev.parent;
561 dma_addr_t fd_addr;
562 struct sk_buff **skbh, *skb;
563 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500564 struct dpaa2_eth_swa *swa;
565 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500566
567 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500568 skbh = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500569
570 if (fd_format == dpaa2_fd_single) {
571 skb = *skbh;
572 buffer_start = (unsigned char *)skbh;
573 /* Accessing the skb buffer is safe before dma unmap, because
574 * we didn't map the actual skb shell.
575 */
576 dma_unmap_single(dev, fd_addr,
577 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500578 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500579 } else if (fd_format == dpaa2_fd_sg) {
580 swa = (struct dpaa2_eth_swa *)skbh;
581 skb = swa->skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500582
583 /* Unmap the scatterlist */
Ioana Radulescub2718e62018-03-23 08:44:11 -0500584 dma_unmap_sg(dev, swa->scl, swa->num_sg, DMA_BIDIRECTIONAL);
585 kfree(swa->scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500586
587 /* Unmap the SGT buffer */
Ioana Radulescub2718e62018-03-23 08:44:11 -0500588 dma_unmap_single(dev, fd_addr, swa->sgt_size,
589 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500590 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600591 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500592 return;
593 }
594
Ioana Radulescu859f9982018-04-26 18:23:47 +0800595 /* Get the timestamp value */
596 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
597 struct skb_shared_hwtstamps shhwtstamps;
598 __le64 *ts = dpaa2_get_ts(skbh, true);
599 u64 ns;
600
601 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
602
603 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
604 shhwtstamps.hwtstamp = ns_to_ktime(ns);
605 skb_tstamp_tx(skb, &shhwtstamps);
606 }
607
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500608 /* Free SGT buffer allocated on tx */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500609 if (fd_format != dpaa2_fd_single)
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500610 skb_free_frag(skbh);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500611
612 /* Move on with skb release */
613 dev_kfree_skb(skb);
614}
615
Ioana Radulescuc433db42017-06-06 10:00:26 -0500616static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500617{
618 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
619 struct dpaa2_fd fd;
620 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500621 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500622 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000623 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500624 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -0600625 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000626 u32 fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500627 int err, i;
628
629 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500630 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500631
Ioana Radulescu18c21462017-12-08 06:47:57 -0600632 needed_headroom = dpaa2_eth_needed_headroom(priv, skb);
633 if (skb_headroom(skb) < needed_headroom) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500634 struct sk_buff *ns;
635
Ioana Radulescu18c21462017-12-08 06:47:57 -0600636 ns = skb_realloc_headroom(skb, needed_headroom);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500637 if (unlikely(!ns)) {
638 percpu_stats->tx_dropped++;
639 goto err_alloc_headroom;
640 }
Ioana Radulescu6662b5e2017-12-08 06:47:55 -0600641 percpu_extras->tx_reallocs++;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800642
643 if (skb->sk)
644 skb_set_owner_w(ns, skb->sk);
645
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500646 dev_kfree_skb(skb);
647 skb = ns;
648 }
649
650 /* We'll be holding a back-reference to the skb until Tx Confirmation;
651 * we don't want that overwritten by a concurrent Tx with a cloned skb.
652 */
653 skb = skb_unshare(skb, GFP_ATOMIC);
654 if (unlikely(!skb)) {
655 /* skb_unshare() has already freed the skb */
656 percpu_stats->tx_dropped++;
657 return NETDEV_TX_OK;
658 }
659
660 /* Setup the FD fields */
661 memset(&fd, 0, sizeof(fd));
662
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500663 if (skb_is_nonlinear(skb)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500664 err = build_sg_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500665 percpu_extras->tx_sg_frames++;
666 percpu_extras->tx_sg_bytes += skb->len;
667 } else {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500668 err = build_single_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500669 }
670
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500671 if (unlikely(err)) {
672 percpu_stats->tx_dropped++;
673 goto err_build_fd;
674 }
675
Ioana Radulescu56361872017-04-28 04:50:32 -0500676 /* Tracing point */
677 trace_dpaa2_tx_fd(net_dev, &fd);
678
Ioana Radulescu537336c2017-12-21 06:33:20 -0600679 /* TxConf FQ selection relies on queue id from the stack.
680 * In case of a forwarded frame from another DPNI interface, we choose
681 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500682 */
Ioana Radulescu537336c2017-12-21 06:33:20 -0600683 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500684 fq = &priv->fq[queue_mapping];
685 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600686 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
687 priv->tx_qdid, 0,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500688 fq->tx_qdbin, &fd);
689 if (err != -EBUSY)
690 break;
691 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500692 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500693 if (unlikely(err < 0)) {
694 percpu_stats->tx_errors++;
695 /* Clean up everything, including freeing the skb */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600696 free_tx_fd(priv, &fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500697 } else {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000698 fd_len = dpaa2_fd_get_len(&fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500699 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000700 percpu_stats->tx_bytes += fd_len;
701
702 nq = netdev_get_tx_queue(net_dev, queue_mapping);
703 netdev_tx_sent_queue(nq, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500704 }
705
706 return NETDEV_TX_OK;
707
708err_build_fd:
709err_alloc_headroom:
710 dev_kfree_skb(skb);
711
712 return NETDEV_TX_OK;
713}
714
715/* Tx confirmation frame processing routine */
716static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +0000717 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500718 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000719 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500720{
721 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500722 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000723 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500724 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500725
Ioana Radulescu56361872017-04-28 04:50:32 -0500726 /* Tracing point */
727 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
728
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500729 percpu_extras = this_cpu_ptr(priv->percpu_extras);
730 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000731 percpu_extras->tx_conf_bytes += fd_len;
732
733 fq->dq_frames++;
734 fq->dq_bytes += fd_len;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500735
Ioana Radulescu39163c02017-06-06 10:00:39 -0500736 /* Check frame errors in the FD field */
737 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600738 free_tx_fd(priv, fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500739
740 if (likely(!fd_errors))
741 return;
742
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600743 if (net_ratelimit())
744 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
745 fd_errors);
746
Ioana Radulescu39163c02017-06-06 10:00:39 -0500747 percpu_stats = this_cpu_ptr(priv->percpu_stats);
748 /* Tx-conf logically pertains to the egress path. */
749 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500750}
751
752static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
753{
754 int err;
755
756 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
757 DPNI_OFF_RX_L3_CSUM, enable);
758 if (err) {
759 netdev_err(priv->net_dev,
760 "dpni_set_offload(RX_L3_CSUM) failed\n");
761 return err;
762 }
763
764 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
765 DPNI_OFF_RX_L4_CSUM, enable);
766 if (err) {
767 netdev_err(priv->net_dev,
768 "dpni_set_offload(RX_L4_CSUM) failed\n");
769 return err;
770 }
771
772 return 0;
773}
774
775static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
776{
777 int err;
778
779 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
780 DPNI_OFF_TX_L3_CSUM, enable);
781 if (err) {
782 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
783 return err;
784 }
785
786 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
787 DPNI_OFF_TX_L4_CSUM, enable);
788 if (err) {
789 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
790 return err;
791 }
792
793 return 0;
794}
795
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500796/* Free buffers acquired from the buffer pool or which were meant to
797 * be released in the pool
798 */
799static void free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array, int count)
800{
801 struct device *dev = priv->net_dev->dev.parent;
802 void *vaddr;
803 int i;
804
805 for (i = 0; i < count; i++) {
806 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
807 dma_unmap_single(dev, buf_array[i], DPAA2_ETH_RX_BUF_SIZE,
Ioana Radulescu466bcdc2018-07-09 10:01:07 -0500808 DMA_FROM_DEVICE);
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500809 skb_free_frag(vaddr);
810 }
811}
812
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500813/* Perform a single release command to add buffers
814 * to the specified buffer pool
815 */
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600816static int add_bufs(struct dpaa2_eth_priv *priv,
817 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500818{
819 struct device *dev = priv->net_dev->dev.parent;
820 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
821 void *buf;
822 dma_addr_t addr;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500823 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500824
825 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
826 /* Allocate buffer visible to WRIOP + skb shared info +
827 * alignment padding
828 */
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +0000829 buf = napi_alloc_frag(dpaa2_eth_buf_raw_size(priv));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500830 if (unlikely(!buf))
831 goto err_alloc;
832
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +0000833 buf = PTR_ALIGN(buf, priv->rx_buf_align);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500834
835 addr = dma_map_single(dev, buf, DPAA2_ETH_RX_BUF_SIZE,
836 DMA_FROM_DEVICE);
837 if (unlikely(dma_mapping_error(dev, addr)))
838 goto err_map;
839
840 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -0500841
842 /* tracing point */
843 trace_dpaa2_eth_buf_seed(priv->net_dev,
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +0000844 buf, dpaa2_eth_buf_raw_size(priv),
Ioana Radulescu56361872017-04-28 04:50:32 -0500845 addr, DPAA2_ETH_RX_BUF_SIZE,
846 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500847 }
848
849release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500850 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600851 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500852 buf_array, i)) == -EBUSY)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500853 cpu_relax();
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500854
855 /* If release command failed, clean up and bail out;
856 * not much else we can do about it
857 */
858 if (err) {
859 free_bufs(priv, buf_array, i);
860 return 0;
861 }
862
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500863 return i;
864
865err_map:
866 skb_free_frag(buf);
867err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500868 /* If we managed to allocate at least some buffers,
869 * release them to hardware
870 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500871 if (i)
872 goto release_bufs;
873
874 return 0;
875}
876
877static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
878{
879 int i, j;
880 int new_count;
881
882 /* This is the lazy seeding of Rx buffer pools.
883 * dpaa2_add_bufs() is also used on the Rx hotpath and calls
884 * napi_alloc_frag(). The trouble with that is that it in turn ends up
885 * calling this_cpu_ptr(), which mandates execution in atomic context.
886 * Rather than splitting up the code, do a one-off preempt disable.
887 */
888 preempt_disable();
889 for (j = 0; j < priv->num_channels; j++) {
890 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
891 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600892 new_count = add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500893 priv->channel[j]->buf_count += new_count;
894
895 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
896 preempt_enable();
897 return -ENOMEM;
898 }
899 }
900 }
901 preempt_enable();
902
903 return 0;
904}
905
906/**
907 * Drain the specified number of buffers from the DPNI's private buffer pool.
908 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
909 */
910static void drain_bufs(struct dpaa2_eth_priv *priv, int count)
911{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500912 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500913 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500914
915 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -0500916 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500917 buf_array, count);
918 if (ret < 0) {
919 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
920 return;
921 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500922 free_bufs(priv, buf_array, ret);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500923 } while (ret);
924}
925
926static void drain_pool(struct dpaa2_eth_priv *priv)
927{
928 int i;
929
930 drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
931 drain_bufs(priv, 1);
932
933 for (i = 0; i < priv->num_channels; i++)
934 priv->channel[i]->buf_count = 0;
935}
936
937/* Function is called from softirq context only, so we don't need to guard
938 * the access to percpu count
939 */
940static int refill_pool(struct dpaa2_eth_priv *priv,
941 struct dpaa2_eth_channel *ch,
942 u16 bpid)
943{
944 int new_count;
945
946 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
947 return 0;
948
949 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600950 new_count = add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500951 if (unlikely(!new_count)) {
952 /* Out of memory; abort for now, we'll try later on */
953 break;
954 }
955 ch->buf_count += new_count;
956 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
957
958 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
959 return -ENOMEM;
960
961 return 0;
962}
963
964static int pull_channel(struct dpaa2_eth_channel *ch)
965{
966 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500967 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500968
969 /* Retry while portal is busy */
970 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600971 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
972 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500973 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500974 cpu_relax();
975 } while (err == -EBUSY);
976
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500977 ch->stats.dequeue_portal_busy += dequeues;
978 if (unlikely(err))
979 ch->stats.pull_err++;
980
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500981 return err;
982}
983
984/* NAPI poll routine
985 *
986 * Frames are dequeued from the QMan channel associated with this NAPI context.
987 * Rx, Tx confirmation and (if configured) Rx error frames all count
988 * towards the NAPI budget.
989 */
990static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
991{
992 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500993 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000994 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000995 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
996 struct netdev_queue *nq;
997 int store_cleaned, work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500998 int err;
999
1000 ch = container_of(napi, struct dpaa2_eth_channel, napi);
1001 priv = ch->priv;
1002
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001003 do {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001004 err = pull_channel(ch);
1005 if (unlikely(err))
1006 break;
1007
1008 /* Refill pool if appropriate */
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001009 refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001010
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001011 store_cleaned = consume_frames(ch, &fq);
1012 if (!store_cleaned)
1013 break;
1014 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001015 rx_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001016 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001017 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001018 /* We have a single Tx conf FQ on this channel */
1019 txc_fq = fq;
1020 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001021
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001022 /* If we either consumed the whole NAPI budget with Rx frames
1023 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001024 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001025 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001026 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1027 work_done = budget;
1028 goto out;
1029 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001030 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001031
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001032 /* We didn't consume the entire budget, so finish napi and
1033 * re-enable data availability notifications
1034 */
1035 napi_complete_done(napi, rx_cleaned);
1036 do {
1037 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1038 cpu_relax();
1039 } while (err == -EBUSY);
1040 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1041 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001042
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001043 work_done = max(rx_cleaned, 1);
1044
1045out:
1046 if (txc_fq) {
1047 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1048 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1049 txc_fq->dq_bytes);
1050 txc_fq->dq_frames = 0;
1051 txc_fq->dq_bytes = 0;
1052 }
1053
1054 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001055}
1056
1057static void enable_ch_napi(struct dpaa2_eth_priv *priv)
1058{
1059 struct dpaa2_eth_channel *ch;
1060 int i;
1061
1062 for (i = 0; i < priv->num_channels; i++) {
1063 ch = priv->channel[i];
1064 napi_enable(&ch->napi);
1065 }
1066}
1067
1068static void disable_ch_napi(struct dpaa2_eth_priv *priv)
1069{
1070 struct dpaa2_eth_channel *ch;
1071 int i;
1072
1073 for (i = 0; i < priv->num_channels; i++) {
1074 ch = priv->channel[i];
1075 napi_disable(&ch->napi);
1076 }
1077}
1078
1079static int link_state_update(struct dpaa2_eth_priv *priv)
1080{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001081 struct dpni_link_state state = {0};
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001082 int err;
1083
1084 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1085 if (unlikely(err)) {
1086 netdev_err(priv->net_dev,
1087 "dpni_get_link_state() failed\n");
1088 return err;
1089 }
1090
1091 /* Chech link state; speed / duplex changes are not treated yet */
1092 if (priv->link_state.up == state.up)
1093 return 0;
1094
1095 priv->link_state = state;
1096 if (state.up) {
1097 netif_carrier_on(priv->net_dev);
1098 netif_tx_start_all_queues(priv->net_dev);
1099 } else {
1100 netif_tx_stop_all_queues(priv->net_dev);
1101 netif_carrier_off(priv->net_dev);
1102 }
1103
Ioana Radulescu77160af2017-06-06 10:00:28 -05001104 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001105 state.up ? "up" : "down");
1106
1107 return 0;
1108}
1109
1110static int dpaa2_eth_open(struct net_device *net_dev)
1111{
1112 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1113 int err;
1114
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001115 err = seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001116 if (err) {
1117 /* Not much to do; the buffer pool, though not filled up,
1118 * may still contain some buffers which would enable us
1119 * to limp on.
1120 */
1121 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001122 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001123 }
1124
1125 /* We'll only start the txqs when the link is actually ready; make sure
1126 * we don't race against the link up notification, which may come
1127 * immediately after dpni_enable();
1128 */
1129 netif_tx_stop_all_queues(net_dev);
1130 enable_ch_napi(priv);
1131 /* Also, explicitly set carrier off, otherwise netif_carrier_ok() will
1132 * return true and cause 'ip link show' to report the LOWER_UP flag,
1133 * even though the link notification wasn't even received.
1134 */
1135 netif_carrier_off(net_dev);
1136
1137 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1138 if (err < 0) {
1139 netdev_err(net_dev, "dpni_enable() failed\n");
1140 goto enable_err;
1141 }
1142
1143 /* If the DPMAC object has already processed the link up interrupt,
1144 * we have to learn the link state ourselves.
1145 */
1146 err = link_state_update(priv);
1147 if (err < 0) {
1148 netdev_err(net_dev, "Can't update link state\n");
1149 goto link_state_err;
1150 }
1151
1152 return 0;
1153
1154link_state_err:
1155enable_err:
1156 disable_ch_napi(priv);
1157 drain_pool(priv);
1158 return err;
1159}
1160
1161/* The DPIO store must be empty when we call this,
1162 * at the end of every NAPI cycle.
1163 */
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +00001164static u32 drain_channel(struct dpaa2_eth_channel *ch)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001165{
1166 u32 drained = 0, total = 0;
1167
1168 do {
1169 pull_channel(ch);
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001170 drained = consume_frames(ch, NULL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001171 total += drained;
1172 } while (drained);
1173
1174 return total;
1175}
1176
1177static u32 drain_ingress_frames(struct dpaa2_eth_priv *priv)
1178{
1179 struct dpaa2_eth_channel *ch;
1180 int i;
1181 u32 drained = 0;
1182
1183 for (i = 0; i < priv->num_channels; i++) {
1184 ch = priv->channel[i];
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +00001185 drained += drain_channel(ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001186 }
1187
1188 return drained;
1189}
1190
1191static int dpaa2_eth_stop(struct net_device *net_dev)
1192{
1193 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001194 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001195 int retries = 10;
1196 u32 drained;
1197
1198 netif_tx_stop_all_queues(net_dev);
1199 netif_carrier_off(net_dev);
1200
1201 /* Loop while dpni_disable() attempts to drain the egress FQs
1202 * and confirm them back to us.
1203 */
1204 do {
1205 dpni_disable(priv->mc_io, 0, priv->mc_token);
1206 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1207 if (dpni_enabled)
1208 /* Allow the hardware some slack */
1209 msleep(100);
1210 } while (dpni_enabled && --retries);
1211 if (!retries) {
1212 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1213 /* Must go on and disable NAPI nonetheless, so we don't crash at
1214 * the next "ifconfig up"
1215 */
1216 }
1217
1218 /* Wait for NAPI to complete on every core and disable it.
1219 * In particular, this will also prevent NAPI from being rescheduled if
1220 * a new CDAN is serviced, effectively discarding the CDAN. We therefore
1221 * don't even need to disarm the channels, except perhaps for the case
1222 * of a huge coalescing value.
1223 */
1224 disable_ch_napi(priv);
1225
1226 /* Manually drain the Rx and TxConf queues */
1227 drained = drain_ingress_frames(priv);
1228 if (drained)
1229 netdev_dbg(net_dev, "Drained %d frames.\n", drained);
1230
1231 /* Empty the buffer pool */
1232 drain_pool(priv);
1233
1234 return 0;
1235}
1236
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001237static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1238{
1239 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1240 struct device *dev = net_dev->dev.parent;
1241 int err;
1242
1243 err = eth_mac_addr(net_dev, addr);
1244 if (err < 0) {
1245 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1246 return err;
1247 }
1248
1249 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1250 net_dev->dev_addr);
1251 if (err) {
1252 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1253 return err;
1254 }
1255
1256 return 0;
1257}
1258
1259/** Fill in counters maintained by the GPP driver. These may be different from
1260 * the hardware counters obtained by ethtool.
1261 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001262static void dpaa2_eth_get_stats(struct net_device *net_dev,
1263 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001264{
1265 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1266 struct rtnl_link_stats64 *percpu_stats;
1267 u64 *cpustats;
1268 u64 *netstats = (u64 *)stats;
1269 int i, j;
1270 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1271
1272 for_each_possible_cpu(i) {
1273 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1274 cpustats = (u64 *)percpu_stats;
1275 for (j = 0; j < num; j++)
1276 netstats[j] += cpustats[j];
1277 }
1278}
1279
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001280/* Copy mac unicast addresses from @net_dev to @priv.
1281 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1282 */
1283static void add_uc_hw_addr(const struct net_device *net_dev,
1284 struct dpaa2_eth_priv *priv)
1285{
1286 struct netdev_hw_addr *ha;
1287 int err;
1288
1289 netdev_for_each_uc_addr(ha, net_dev) {
1290 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1291 ha->addr);
1292 if (err)
1293 netdev_warn(priv->net_dev,
1294 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1295 ha->addr, err);
1296 }
1297}
1298
1299/* Copy mac multicast addresses from @net_dev to @priv
1300 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1301 */
1302static void add_mc_hw_addr(const struct net_device *net_dev,
1303 struct dpaa2_eth_priv *priv)
1304{
1305 struct netdev_hw_addr *ha;
1306 int err;
1307
1308 netdev_for_each_mc_addr(ha, net_dev) {
1309 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1310 ha->addr);
1311 if (err)
1312 netdev_warn(priv->net_dev,
1313 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1314 ha->addr, err);
1315 }
1316}
1317
1318static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1319{
1320 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1321 int uc_count = netdev_uc_count(net_dev);
1322 int mc_count = netdev_mc_count(net_dev);
1323 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1324 u32 options = priv->dpni_attrs.options;
1325 u16 mc_token = priv->mc_token;
1326 struct fsl_mc_io *mc_io = priv->mc_io;
1327 int err;
1328
1329 /* Basic sanity checks; these probably indicate a misconfiguration */
1330 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1331 netdev_info(net_dev,
1332 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1333 max_mac);
1334
1335 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1336 if (uc_count > max_mac) {
1337 netdev_info(net_dev,
1338 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1339 uc_count, max_mac);
1340 goto force_promisc;
1341 }
1342 if (mc_count + uc_count > max_mac) {
1343 netdev_info(net_dev,
1344 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1345 uc_count + mc_count, max_mac);
1346 goto force_mc_promisc;
1347 }
1348
1349 /* Adjust promisc settings due to flag combinations */
1350 if (net_dev->flags & IFF_PROMISC)
1351 goto force_promisc;
1352 if (net_dev->flags & IFF_ALLMULTI) {
1353 /* First, rebuild unicast filtering table. This should be done
1354 * in promisc mode, in order to avoid frame loss while we
1355 * progressively add entries to the table.
1356 * We don't know whether we had been in promisc already, and
1357 * making an MC call to find out is expensive; so set uc promisc
1358 * nonetheless.
1359 */
1360 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1361 if (err)
1362 netdev_warn(net_dev, "Can't set uc promisc\n");
1363
1364 /* Actual uc table reconstruction. */
1365 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1366 if (err)
1367 netdev_warn(net_dev, "Can't clear uc filters\n");
1368 add_uc_hw_addr(net_dev, priv);
1369
1370 /* Finally, clear uc promisc and set mc promisc as requested. */
1371 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1372 if (err)
1373 netdev_warn(net_dev, "Can't clear uc promisc\n");
1374 goto force_mc_promisc;
1375 }
1376
1377 /* Neither unicast, nor multicast promisc will be on... eventually.
1378 * For now, rebuild mac filtering tables while forcing both of them on.
1379 */
1380 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1381 if (err)
1382 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1383 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1384 if (err)
1385 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1386
1387 /* Actual mac filtering tables reconstruction */
1388 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1389 if (err)
1390 netdev_warn(net_dev, "Can't clear mac filters\n");
1391 add_mc_hw_addr(net_dev, priv);
1392 add_uc_hw_addr(net_dev, priv);
1393
1394 /* Now we can clear both ucast and mcast promisc, without risking
1395 * to drop legitimate frames anymore.
1396 */
1397 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1398 if (err)
1399 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1400 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1401 if (err)
1402 netdev_warn(net_dev, "Can't clear mcast promisc\n");
1403
1404 return;
1405
1406force_promisc:
1407 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1408 if (err)
1409 netdev_warn(net_dev, "Can't set ucast promisc\n");
1410force_mc_promisc:
1411 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1412 if (err)
1413 netdev_warn(net_dev, "Can't set mcast promisc\n");
1414}
1415
1416static int dpaa2_eth_set_features(struct net_device *net_dev,
1417 netdev_features_t features)
1418{
1419 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1420 netdev_features_t changed = features ^ net_dev->features;
1421 bool enable;
1422 int err;
1423
1424 if (changed & NETIF_F_RXCSUM) {
1425 enable = !!(features & NETIF_F_RXCSUM);
1426 err = set_rx_csum(priv, enable);
1427 if (err)
1428 return err;
1429 }
1430
1431 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1432 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
1433 err = set_tx_csum(priv, enable);
1434 if (err)
1435 return err;
1436 }
1437
1438 return 0;
1439}
1440
Ioana Radulescu859f9982018-04-26 18:23:47 +08001441static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1442{
1443 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1444 struct hwtstamp_config config;
1445
1446 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
1447 return -EFAULT;
1448
1449 switch (config.tx_type) {
1450 case HWTSTAMP_TX_OFF:
1451 priv->tx_tstamp = false;
1452 break;
1453 case HWTSTAMP_TX_ON:
1454 priv->tx_tstamp = true;
1455 break;
1456 default:
1457 return -ERANGE;
1458 }
1459
1460 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
1461 priv->rx_tstamp = false;
1462 } else {
1463 priv->rx_tstamp = true;
1464 /* TS is set for all frame types, not only those requested */
1465 config.rx_filter = HWTSTAMP_FILTER_ALL;
1466 }
1467
1468 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
1469 -EFAULT : 0;
1470}
1471
1472static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1473{
1474 if (cmd == SIOCSHWTSTAMP)
1475 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
1476
1477 return -EINVAL;
1478}
1479
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001480static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
1481{
1482 int mfl, linear_mfl;
1483
1484 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1485 linear_mfl = DPAA2_ETH_RX_BUF_SIZE - DPAA2_ETH_RX_HWA_SIZE -
1486 dpaa2_eth_rx_head_room(priv);
1487
1488 if (mfl > linear_mfl) {
1489 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
1490 linear_mfl - VLAN_ETH_HLEN);
1491 return false;
1492 }
1493
1494 return true;
1495}
1496
1497static int set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
1498{
1499 int mfl, err;
1500
1501 /* We enforce a maximum Rx frame length based on MTU only if we have
1502 * an XDP program attached (in order to avoid Rx S/G frames).
1503 * Otherwise, we accept all incoming frames as long as they are not
1504 * larger than maximum size supported in hardware
1505 */
1506 if (has_xdp)
1507 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1508 else
1509 mfl = DPAA2_ETH_MFL;
1510
1511 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
1512 if (err) {
1513 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
1514 return err;
1515 }
1516
1517 return 0;
1518}
1519
1520static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
1521{
1522 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1523 int err;
1524
1525 if (!priv->xdp_prog)
1526 goto out;
1527
1528 if (!xdp_mtu_valid(priv, new_mtu))
1529 return -EINVAL;
1530
1531 err = set_rx_mfl(priv, new_mtu, true);
1532 if (err)
1533 return err;
1534
1535out:
1536 dev->mtu = new_mtu;
1537 return 0;
1538}
1539
1540static int setup_xdp(struct net_device *dev, struct bpf_prog *prog)
1541{
1542 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1543 struct dpaa2_eth_channel *ch;
1544 struct bpf_prog *old;
1545 bool up, need_update;
1546 int i, err;
1547
1548 if (prog && !xdp_mtu_valid(priv, dev->mtu))
1549 return -EINVAL;
1550
1551 if (prog) {
1552 prog = bpf_prog_add(prog, priv->num_channels);
1553 if (IS_ERR(prog))
1554 return PTR_ERR(prog);
1555 }
1556
1557 up = netif_running(dev);
1558 need_update = (!!priv->xdp_prog != !!prog);
1559
1560 if (up)
1561 dpaa2_eth_stop(dev);
1562
1563 /* While in xdp mode, enforce a maximum Rx frame size based on MTU */
1564 if (need_update) {
1565 err = set_rx_mfl(priv, dev->mtu, !!prog);
1566 if (err)
1567 goto out_err;
1568 }
1569
1570 old = xchg(&priv->xdp_prog, prog);
1571 if (old)
1572 bpf_prog_put(old);
1573
1574 for (i = 0; i < priv->num_channels; i++) {
1575 ch = priv->channel[i];
1576 old = xchg(&ch->xdp.prog, prog);
1577 if (old)
1578 bpf_prog_put(old);
1579 }
1580
1581 if (up) {
1582 err = dpaa2_eth_open(dev);
1583 if (err)
1584 return err;
1585 }
1586
1587 return 0;
1588
1589out_err:
1590 if (prog)
1591 bpf_prog_sub(prog, priv->num_channels);
1592 if (up)
1593 dpaa2_eth_open(dev);
1594
1595 return err;
1596}
1597
1598static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1599{
1600 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1601
1602 switch (xdp->command) {
1603 case XDP_SETUP_PROG:
1604 return setup_xdp(dev, xdp->prog);
1605 case XDP_QUERY_PROG:
1606 xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0;
1607 break;
1608 default:
1609 return -EINVAL;
1610 }
1611
1612 return 0;
1613}
1614
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001615static const struct net_device_ops dpaa2_eth_ops = {
1616 .ndo_open = dpaa2_eth_open,
1617 .ndo_start_xmit = dpaa2_eth_tx,
1618 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001619 .ndo_set_mac_address = dpaa2_eth_set_addr,
1620 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001621 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
1622 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08001623 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001624 .ndo_change_mtu = dpaa2_eth_change_mtu,
1625 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001626};
1627
1628static void cdan_cb(struct dpaa2_io_notification_ctx *ctx)
1629{
1630 struct dpaa2_eth_channel *ch;
1631
1632 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001633
1634 /* Update NAPI statistics */
1635 ch->stats.cdan++;
1636
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001637 napi_schedule_irqoff(&ch->napi);
1638}
1639
1640/* Allocate and configure a DPCON object */
1641static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv)
1642{
1643 struct fsl_mc_device *dpcon;
1644 struct device *dev = priv->net_dev->dev.parent;
1645 struct dpcon_attr attrs;
1646 int err;
1647
1648 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
1649 FSL_MC_POOL_DPCON, &dpcon);
1650 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001651 if (err == -ENXIO)
1652 err = -EPROBE_DEFER;
1653 else
1654 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
1655 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001656 }
1657
1658 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
1659 if (err) {
1660 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001661 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001662 }
1663
1664 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
1665 if (err) {
1666 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001667 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001668 }
1669
1670 err = dpcon_get_attributes(priv->mc_io, 0, dpcon->mc_handle, &attrs);
1671 if (err) {
1672 dev_err(dev, "dpcon_get_attributes() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001673 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001674 }
1675
1676 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
1677 if (err) {
1678 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001679 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001680 }
1681
1682 return dpcon;
1683
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001684close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001685 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001686free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001687 fsl_mc_object_free(dpcon);
1688
1689 return NULL;
1690}
1691
1692static void free_dpcon(struct dpaa2_eth_priv *priv,
1693 struct fsl_mc_device *dpcon)
1694{
1695 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
1696 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
1697 fsl_mc_object_free(dpcon);
1698}
1699
1700static struct dpaa2_eth_channel *
1701alloc_channel(struct dpaa2_eth_priv *priv)
1702{
1703 struct dpaa2_eth_channel *channel;
1704 struct dpcon_attr attr;
1705 struct device *dev = priv->net_dev->dev.parent;
1706 int err;
1707
1708 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
1709 if (!channel)
1710 return NULL;
1711
1712 channel->dpcon = setup_dpcon(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001713 if (IS_ERR_OR_NULL(channel->dpcon)) {
1714 err = PTR_ERR(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001715 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001716 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001717
1718 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
1719 &attr);
1720 if (err) {
1721 dev_err(dev, "dpcon_get_attributes() failed\n");
1722 goto err_get_attr;
1723 }
1724
1725 channel->dpcon_id = attr.id;
1726 channel->ch_id = attr.qbman_ch_id;
1727 channel->priv = priv;
1728
1729 return channel;
1730
1731err_get_attr:
1732 free_dpcon(priv, channel->dpcon);
1733err_setup:
1734 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001735 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001736}
1737
1738static void free_channel(struct dpaa2_eth_priv *priv,
1739 struct dpaa2_eth_channel *channel)
1740{
1741 free_dpcon(priv, channel->dpcon);
1742 kfree(channel);
1743}
1744
1745/* DPIO setup: allocate and configure QBMan channels, setup core affinity
1746 * and register data availability notifications
1747 */
1748static int setup_dpio(struct dpaa2_eth_priv *priv)
1749{
1750 struct dpaa2_io_notification_ctx *nctx;
1751 struct dpaa2_eth_channel *channel;
1752 struct dpcon_notification_cfg dpcon_notif_cfg;
1753 struct device *dev = priv->net_dev->dev.parent;
1754 int i, err;
1755
1756 /* We want the ability to spread ingress traffic (RX, TX conf) to as
1757 * many cores as possible, so we need one channel for each core
1758 * (unless there's fewer queues than cores, in which case the extra
1759 * channels would be wasted).
1760 * Allocate one channel per core and register it to the core's
1761 * affine DPIO. If not enough channels are available for all cores
1762 * or if some cores don't have an affine DPIO, there will be no
1763 * ingress frame processing on those cores.
1764 */
1765 cpumask_clear(&priv->dpio_cpumask);
1766 for_each_online_cpu(i) {
1767 /* Try to allocate a channel */
1768 channel = alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001769 if (IS_ERR_OR_NULL(channel)) {
1770 err = PTR_ERR(channel);
1771 if (err != -EPROBE_DEFER)
1772 dev_info(dev,
1773 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001774 goto err_alloc_ch;
1775 }
1776
1777 priv->channel[priv->num_channels] = channel;
1778
1779 nctx = &channel->nctx;
1780 nctx->is_cdan = 1;
1781 nctx->cb = cdan_cb;
1782 nctx->id = channel->ch_id;
1783 nctx->desired_cpu = i;
1784
1785 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001786 channel->dpio = dpaa2_io_service_select(i);
1787 err = dpaa2_io_service_register(channel->dpio, nctx);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001788 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05001789 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001790 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05001791 * none available for next cores either. Signal we want
1792 * to retry later, in case the DPIO devices weren't
1793 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001794 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05001795 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001796 goto err_service_reg;
1797 }
1798
1799 /* Register DPCON notification with MC */
1800 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
1801 dpcon_notif_cfg.priority = 0;
1802 dpcon_notif_cfg.user_ctx = nctx->qman64;
1803 err = dpcon_set_notification(priv->mc_io, 0,
1804 channel->dpcon->mc_handle,
1805 &dpcon_notif_cfg);
1806 if (err) {
1807 dev_err(dev, "dpcon_set_notification failed()\n");
1808 goto err_set_cdan;
1809 }
1810
1811 /* If we managed to allocate a channel and also found an affine
1812 * DPIO for this core, add it to the final mask
1813 */
1814 cpumask_set_cpu(i, &priv->dpio_cpumask);
1815 priv->num_channels++;
1816
1817 /* Stop if we already have enough channels to accommodate all
1818 * RX and TX conf queues
1819 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00001820 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001821 break;
1822 }
1823
1824 return 0;
1825
1826err_set_cdan:
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001827 dpaa2_io_service_deregister(channel->dpio, nctx);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001828err_service_reg:
1829 free_channel(priv, channel);
1830err_alloc_ch:
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001831 if (err == -EPROBE_DEFER)
1832 return err;
1833
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001834 if (cpumask_empty(&priv->dpio_cpumask)) {
1835 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001836 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001837 }
1838
1839 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
1840 cpumask_pr_args(&priv->dpio_cpumask));
1841
1842 return 0;
1843}
1844
1845static void free_dpio(struct dpaa2_eth_priv *priv)
1846{
1847 int i;
1848 struct dpaa2_eth_channel *ch;
1849
1850 /* deregister CDAN notifications and free channels */
1851 for (i = 0; i < priv->num_channels; i++) {
1852 ch = priv->channel[i];
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001853 dpaa2_io_service_deregister(ch->dpio, &ch->nctx);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001854 free_channel(priv, ch);
1855 }
1856}
1857
1858static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv,
1859 int cpu)
1860{
1861 struct device *dev = priv->net_dev->dev.parent;
1862 int i;
1863
1864 for (i = 0; i < priv->num_channels; i++)
1865 if (priv->channel[i]->nctx.desired_cpu == cpu)
1866 return priv->channel[i];
1867
1868 /* We should never get here. Issue a warning and return
1869 * the first channel, because it's still better than nothing
1870 */
1871 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
1872
1873 return priv->channel[0];
1874}
1875
1876static void set_fq_affinity(struct dpaa2_eth_priv *priv)
1877{
1878 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu93ddf0b2017-12-21 06:33:21 -06001879 struct cpumask xps_mask;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001880 struct dpaa2_eth_fq *fq;
1881 int rx_cpu, txc_cpu;
Ioana Radulescu93ddf0b2017-12-21 06:33:21 -06001882 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001883
1884 /* For each FQ, pick one channel/CPU to deliver frames to.
1885 * This may well change at runtime, either through irqbalance or
1886 * through direct user intervention.
1887 */
1888 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
1889
1890 for (i = 0; i < priv->num_fqs; i++) {
1891 fq = &priv->fq[i];
1892 switch (fq->type) {
1893 case DPAA2_RX_FQ:
1894 fq->target_cpu = rx_cpu;
1895 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
1896 if (rx_cpu >= nr_cpu_ids)
1897 rx_cpu = cpumask_first(&priv->dpio_cpumask);
1898 break;
1899 case DPAA2_TX_CONF_FQ:
1900 fq->target_cpu = txc_cpu;
Ioana Radulescu93ddf0b2017-12-21 06:33:21 -06001901
1902 /* Tell the stack to affine to txc_cpu the Tx queue
1903 * associated with the confirmation one
1904 */
1905 cpumask_clear(&xps_mask);
1906 cpumask_set_cpu(txc_cpu, &xps_mask);
1907 err = netif_set_xps_queue(priv->net_dev, &xps_mask,
1908 fq->flowid);
1909 if (err)
1910 dev_err(dev, "Error setting XPS queue\n");
1911
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001912 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
1913 if (txc_cpu >= nr_cpu_ids)
1914 txc_cpu = cpumask_first(&priv->dpio_cpumask);
1915 break;
1916 default:
1917 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
1918 }
1919 fq->channel = get_affine_channel(priv, fq->target_cpu);
1920 }
1921}
1922
1923static void setup_fqs(struct dpaa2_eth_priv *priv)
1924{
1925 int i;
1926
1927 /* We have one TxConf FQ per Tx flow.
1928 * The number of Tx and Rx queues is the same.
1929 * Tx queues come first in the fq array.
1930 */
1931 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
1932 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
1933 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
1934 priv->fq[priv->num_fqs++].flowid = (u16)i;
1935 }
1936
1937 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
1938 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
1939 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
1940 priv->fq[priv->num_fqs++].flowid = (u16)i;
1941 }
1942
1943 /* For each FQ, decide on which core to process incoming frames */
1944 set_fq_affinity(priv);
1945}
1946
1947/* Allocate and configure one buffer pool for each interface */
1948static int setup_dpbp(struct dpaa2_eth_priv *priv)
1949{
1950 int err;
1951 struct fsl_mc_device *dpbp_dev;
1952 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001953 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001954
1955 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
1956 &dpbp_dev);
1957 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001958 if (err == -ENXIO)
1959 err = -EPROBE_DEFER;
1960 else
1961 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001962 return err;
1963 }
1964
1965 priv->dpbp_dev = dpbp_dev;
1966
1967 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
1968 &dpbp_dev->mc_handle);
1969 if (err) {
1970 dev_err(dev, "dpbp_open() failed\n");
1971 goto err_open;
1972 }
1973
Ioana Radulescud00defe2017-06-06 10:00:32 -05001974 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
1975 if (err) {
1976 dev_err(dev, "dpbp_reset() failed\n");
1977 goto err_reset;
1978 }
1979
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001980 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
1981 if (err) {
1982 dev_err(dev, "dpbp_enable() failed\n");
1983 goto err_enable;
1984 }
1985
1986 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001987 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001988 if (err) {
1989 dev_err(dev, "dpbp_get_attributes() failed\n");
1990 goto err_get_attr;
1991 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001992 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001993
1994 return 0;
1995
1996err_get_attr:
1997 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
1998err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05001999err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002000 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2001err_open:
2002 fsl_mc_object_free(dpbp_dev);
2003
2004 return err;
2005}
2006
2007static void free_dpbp(struct dpaa2_eth_priv *priv)
2008{
2009 drain_pool(priv);
2010 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2011 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2012 fsl_mc_object_free(priv->dpbp_dev);
2013}
2014
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002015static int set_buffer_layout(struct dpaa2_eth_priv *priv)
2016{
2017 struct device *dev = priv->net_dev->dev.parent;
2018 struct dpni_buffer_layout buf_layout = {0};
2019 int err;
2020
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002021 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2022 * version, this number is not always provided correctly on rev1.
2023 * We need to check for both alternatives in this situation.
2024 */
2025 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2026 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
2027 priv->rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
2028 else
2029 priv->rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
2030
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002031 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002032 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002033 buf_layout.pass_timestamp = true;
2034 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2035 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002036 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2037 DPNI_QUEUE_TX, &buf_layout);
2038 if (err) {
2039 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2040 return err;
2041 }
2042
2043 /* tx-confirm buffer */
Ioana Radulescu859f9982018-04-26 18:23:47 +08002044 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002045 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2046 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2047 if (err) {
2048 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2049 return err;
2050 }
2051
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002052 /* Now that we've set our tx buffer layout, retrieve the minimum
2053 * required tx data offset.
2054 */
2055 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2056 &priv->tx_data_offset);
2057 if (err) {
2058 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2059 return err;
2060 }
2061
2062 if ((priv->tx_data_offset % 64) != 0)
2063 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2064 priv->tx_data_offset);
2065
2066 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06002067 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002068 buf_layout.pass_parser_result = true;
2069 buf_layout.data_align = priv->rx_buf_align;
2070 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2071 buf_layout.private_data_size = 0;
2072 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2073 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2074 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08002075 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2076 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002077 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2078 DPNI_QUEUE_RX, &buf_layout);
2079 if (err) {
2080 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2081 return err;
2082 }
2083
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002084 return 0;
2085}
2086
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002087/* Configure the DPNI object this interface is associated with */
2088static int setup_dpni(struct fsl_mc_device *ls_dev)
2089{
2090 struct device *dev = &ls_dev->dev;
2091 struct dpaa2_eth_priv *priv;
2092 struct net_device *net_dev;
2093 int err;
2094
2095 net_dev = dev_get_drvdata(dev);
2096 priv = netdev_priv(net_dev);
2097
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002098 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05002099 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002100 if (err) {
2101 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002102 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002103 }
2104
Ioana Radulescu311cffa2018-03-23 08:44:09 -05002105 /* Check if we can work with this DPNI object */
2106 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
2107 &priv->dpni_ver_minor);
2108 if (err) {
2109 dev_err(dev, "dpni_get_api_version() failed\n");
2110 goto close;
2111 }
2112 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
2113 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
2114 priv->dpni_ver_major, priv->dpni_ver_minor,
2115 DPNI_VER_MAJOR, DPNI_VER_MINOR);
2116 err = -ENOTSUPP;
2117 goto close;
2118 }
2119
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002120 ls_dev->mc_io = priv->mc_io;
2121 ls_dev->mc_handle = priv->mc_token;
2122
2123 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2124 if (err) {
2125 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002126 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002127 }
2128
2129 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
2130 &priv->dpni_attrs);
2131 if (err) {
2132 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002133 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002134 }
2135
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002136 err = set_buffer_layout(priv);
2137 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002138 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002139
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002140 priv->cls_rules = devm_kzalloc(dev, sizeof(struct dpaa2_eth_cls_rule) *
2141 dpaa2_eth_fs_count(priv), GFP_KERNEL);
2142 if (!priv->cls_rules)
2143 goto close;
2144
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002145 return 0;
2146
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002147close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002148 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002149
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002150 return err;
2151}
2152
2153static void free_dpni(struct dpaa2_eth_priv *priv)
2154{
2155 int err;
2156
2157 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2158 if (err)
2159 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
2160 err);
2161
2162 dpni_close(priv->mc_io, 0, priv->mc_token);
2163}
2164
2165static int setup_rx_flow(struct dpaa2_eth_priv *priv,
2166 struct dpaa2_eth_fq *fq)
2167{
2168 struct device *dev = priv->net_dev->dev.parent;
2169 struct dpni_queue queue;
2170 struct dpni_queue_id qid;
2171 struct dpni_taildrop td;
2172 int err;
2173
2174 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2175 DPNI_QUEUE_RX, 0, fq->flowid, &queue, &qid);
2176 if (err) {
2177 dev_err(dev, "dpni_get_queue(RX) failed\n");
2178 return err;
2179 }
2180
2181 fq->fqid = qid.fqid;
2182
2183 queue.destination.id = fq->channel->dpcon_id;
2184 queue.destination.type = DPNI_DEST_DPCON;
2185 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002186 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002187 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2188 DPNI_QUEUE_RX, 0, fq->flowid,
2189 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2190 &queue);
2191 if (err) {
2192 dev_err(dev, "dpni_set_queue(RX) failed\n");
2193 return err;
2194 }
2195
2196 td.enable = 1;
2197 td.threshold = DPAA2_ETH_TAILDROP_THRESH;
2198 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, DPNI_CP_QUEUE,
2199 DPNI_QUEUE_RX, 0, fq->flowid, &td);
2200 if (err) {
2201 dev_err(dev, "dpni_set_threshold() failed\n");
2202 return err;
2203 }
2204
2205 return 0;
2206}
2207
2208static int setup_tx_flow(struct dpaa2_eth_priv *priv,
2209 struct dpaa2_eth_fq *fq)
2210{
2211 struct device *dev = priv->net_dev->dev.parent;
2212 struct dpni_queue queue;
2213 struct dpni_queue_id qid;
2214 int err;
2215
2216 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2217 DPNI_QUEUE_TX, 0, fq->flowid, &queue, &qid);
2218 if (err) {
2219 dev_err(dev, "dpni_get_queue(TX) failed\n");
2220 return err;
2221 }
2222
2223 fq->tx_qdbin = qid.qdbin;
2224
2225 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2226 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2227 &queue, &qid);
2228 if (err) {
2229 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
2230 return err;
2231 }
2232
2233 fq->fqid = qid.fqid;
2234
2235 queue.destination.id = fq->channel->dpcon_id;
2236 queue.destination.type = DPNI_DEST_DPCON;
2237 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002238 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002239 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2240 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2241 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2242 &queue);
2243 if (err) {
2244 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
2245 return err;
2246 }
2247
2248 return 0;
2249}
2250
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002251/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002252static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002253 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002254 /* L2 header */
2255 .rxnfc_field = RXH_L2DA,
2256 .cls_prot = NET_PROT_ETH,
2257 .cls_field = NH_FLD_ETH_DA,
2258 .size = 6,
2259 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002260 .cls_prot = NET_PROT_ETH,
2261 .cls_field = NH_FLD_ETH_SA,
2262 .size = 6,
2263 }, {
2264 /* This is the last ethertype field parsed:
2265 * depending on frame format, it can be the MAC ethertype
2266 * or the VLAN etype.
2267 */
2268 .cls_prot = NET_PROT_ETH,
2269 .cls_field = NH_FLD_ETH_TYPE,
2270 .size = 2,
2271 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002272 /* VLAN header */
2273 .rxnfc_field = RXH_VLAN,
2274 .cls_prot = NET_PROT_VLAN,
2275 .cls_field = NH_FLD_VLAN_TCI,
2276 .size = 2,
2277 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002278 /* IP header */
2279 .rxnfc_field = RXH_IP_SRC,
2280 .cls_prot = NET_PROT_IP,
2281 .cls_field = NH_FLD_IP_SRC,
2282 .size = 4,
2283 }, {
2284 .rxnfc_field = RXH_IP_DST,
2285 .cls_prot = NET_PROT_IP,
2286 .cls_field = NH_FLD_IP_DST,
2287 .size = 4,
2288 }, {
2289 .rxnfc_field = RXH_L3_PROTO,
2290 .cls_prot = NET_PROT_IP,
2291 .cls_field = NH_FLD_IP_PROTO,
2292 .size = 1,
2293 }, {
2294 /* Using UDP ports, this is functionally equivalent to raw
2295 * byte pairs from L4 header.
2296 */
2297 .rxnfc_field = RXH_L4_B_0_1,
2298 .cls_prot = NET_PROT_UDP,
2299 .cls_field = NH_FLD_UDP_PORT_SRC,
2300 .size = 2,
2301 }, {
2302 .rxnfc_field = RXH_L4_B_2_3,
2303 .cls_prot = NET_PROT_UDP,
2304 .cls_field = NH_FLD_UDP_PORT_DST,
2305 .size = 2,
2306 },
2307};
2308
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002309/* Configure the Rx hash key using the legacy API */
2310static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2311{
2312 struct device *dev = priv->net_dev->dev.parent;
2313 struct dpni_rx_tc_dist_cfg dist_cfg;
2314 int err;
2315
2316 memset(&dist_cfg, 0, sizeof(dist_cfg));
2317
2318 dist_cfg.key_cfg_iova = key;
2319 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2320 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
2321
2322 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg);
2323 if (err)
2324 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
2325
2326 return err;
2327}
2328
2329/* Configure the Rx hash key using the new API */
2330static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2331{
2332 struct device *dev = priv->net_dev->dev.parent;
2333 struct dpni_rx_dist_cfg dist_cfg;
2334 int err;
2335
2336 memset(&dist_cfg, 0, sizeof(dist_cfg));
2337
2338 dist_cfg.key_cfg_iova = key;
2339 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2340 dist_cfg.enable = 1;
2341
2342 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2343 if (err)
2344 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
2345
2346 return err;
2347}
2348
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002349/* Configure the Rx flow classification key */
2350static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2351{
2352 struct device *dev = priv->net_dev->dev.parent;
2353 struct dpni_rx_dist_cfg dist_cfg;
2354 int err;
2355
2356 memset(&dist_cfg, 0, sizeof(dist_cfg));
2357
2358 dist_cfg.key_cfg_iova = key;
2359 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2360 dist_cfg.enable = 1;
2361
2362 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2363 if (err)
2364 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
2365
2366 return err;
2367}
2368
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002369/* Size of the Rx flow classification key */
2370int dpaa2_eth_cls_key_size(void)
2371{
2372 int i, size = 0;
2373
2374 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
2375 size += dist_fields[i].size;
2376
2377 return size;
2378}
2379
2380/* Offset of header field in Rx classification key */
2381int dpaa2_eth_cls_fld_off(int prot, int field)
2382{
2383 int i, off = 0;
2384
2385 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2386 if (dist_fields[i].cls_prot == prot &&
2387 dist_fields[i].cls_field == field)
2388 return off;
2389 off += dist_fields[i].size;
2390 }
2391
2392 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
2393 return 0;
2394}
2395
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002396/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002397 * flags is a combination of RXH_ bits
2398 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00002399static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
2400 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002401{
2402 struct device *dev = net_dev->dev.parent;
2403 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2404 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002405 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002406 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002407 u8 *dma_mem;
2408 int i;
2409 int err = 0;
2410
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002411 memset(&cls_cfg, 0, sizeof(cls_cfg));
2412
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002413 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002414 struct dpkg_extract *key =
2415 &cls_cfg.extracts[cls_cfg.num_extracts];
2416
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002417 /* For Rx hashing key we set only the selected fields.
2418 * For Rx flow classification key we set all supported fields
2419 */
2420 if (type == DPAA2_ETH_RX_DIST_HASH) {
2421 if (!(flags & dist_fields[i].rxnfc_field))
2422 continue;
2423 rx_hash_fields |= dist_fields[i].rxnfc_field;
2424 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002425
2426 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
2427 dev_err(dev, "error adding key extraction rule, too many rules?\n");
2428 return -E2BIG;
2429 }
2430
2431 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002432 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002433 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002434 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002435 cls_cfg.num_extracts++;
2436 }
2437
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05002438 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002439 if (!dma_mem)
2440 return -ENOMEM;
2441
2442 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
2443 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002444 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002445 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002446 }
2447
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002448 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002449 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
2450 DMA_TO_DEVICE);
2451 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002452 dev_err(dev, "DMA mapping failed\n");
2453 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002454 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002455 }
2456
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002457 if (type == DPAA2_ETH_RX_DIST_HASH) {
2458 if (dpaa2_eth_has_legacy_dist(priv))
2459 err = config_legacy_hash_key(priv, key_iova);
2460 else
2461 err = config_hash_key(priv, key_iova);
2462 } else {
2463 err = config_cls_key(priv, key_iova);
2464 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002465
2466 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
2467 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002468 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002469 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002470
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002471free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002472 kfree(dma_mem);
2473 return err;
2474}
2475
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002476int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
2477{
2478 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2479
2480 if (!dpaa2_eth_hash_enabled(priv))
2481 return -EOPNOTSUPP;
2482
2483 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, flags);
2484}
2485
2486static int dpaa2_eth_set_cls(struct dpaa2_eth_priv *priv)
2487{
2488 struct device *dev = priv->net_dev->dev.parent;
2489
2490 /* Check if we actually support Rx flow classification */
2491 if (dpaa2_eth_has_legacy_dist(priv)) {
2492 dev_dbg(dev, "Rx cls not supported by current MC version\n");
2493 return -EOPNOTSUPP;
2494 }
2495
2496 if (priv->dpni_attrs.options & DPNI_OPT_NO_FS ||
2497 !(priv->dpni_attrs.options & DPNI_OPT_HAS_KEY_MASKING)) {
2498 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
2499 return -EOPNOTSUPP;
2500 }
2501
2502 if (!dpaa2_eth_hash_enabled(priv)) {
2503 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
2504 return -EOPNOTSUPP;
2505 }
2506
2507 priv->rx_cls_enabled = 1;
2508
2509 return dpaa2_eth_set_dist_key(priv->net_dev, DPAA2_ETH_RX_DIST_CLS, 0);
2510}
2511
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002512/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
2513 * frame queues and channels
2514 */
2515static int bind_dpni(struct dpaa2_eth_priv *priv)
2516{
2517 struct net_device *net_dev = priv->net_dev;
2518 struct device *dev = net_dev->dev.parent;
2519 struct dpni_pools_cfg pools_params;
2520 struct dpni_error_cfg err_cfg;
2521 int err = 0;
2522 int i;
2523
2524 pools_params.num_dpbp = 1;
2525 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
2526 pools_params.pools[0].backup_pool = 0;
2527 pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE;
2528 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
2529 if (err) {
2530 dev_err(dev, "dpni_set_pools() failed\n");
2531 return err;
2532 }
2533
Ioana Radulescu227686b2018-07-27 09:12:59 -05002534 /* have the interface implicitly distribute traffic based on
2535 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002536 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05002537 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002538 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05002539 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002540
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002541 /* Configure the flow classification key; it includes all
2542 * supported header fields and cannot be modified at runtime
2543 */
2544 err = dpaa2_eth_set_cls(priv);
2545 if (err && err != -EOPNOTSUPP)
2546 dev_err(dev, "Failed to configure Rx classification key\n");
2547
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002548 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05002549 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002550 err_cfg.set_frame_annotation = 1;
2551 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
2552 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
2553 &err_cfg);
2554 if (err) {
2555 dev_err(dev, "dpni_set_errors_behavior failed\n");
2556 return err;
2557 }
2558
2559 /* Configure Rx and Tx conf queues to generate CDANs */
2560 for (i = 0; i < priv->num_fqs; i++) {
2561 switch (priv->fq[i].type) {
2562 case DPAA2_RX_FQ:
2563 err = setup_rx_flow(priv, &priv->fq[i]);
2564 break;
2565 case DPAA2_TX_CONF_FQ:
2566 err = setup_tx_flow(priv, &priv->fq[i]);
2567 break;
2568 default:
2569 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
2570 return -EINVAL;
2571 }
2572 if (err)
2573 return err;
2574 }
2575
2576 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
2577 DPNI_QUEUE_TX, &priv->tx_qdid);
2578 if (err) {
2579 dev_err(dev, "dpni_get_qdid() failed\n");
2580 return err;
2581 }
2582
2583 return 0;
2584}
2585
2586/* Allocate rings for storing incoming frame descriptors */
2587static int alloc_rings(struct dpaa2_eth_priv *priv)
2588{
2589 struct net_device *net_dev = priv->net_dev;
2590 struct device *dev = net_dev->dev.parent;
2591 int i;
2592
2593 for (i = 0; i < priv->num_channels; i++) {
2594 priv->channel[i]->store =
2595 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
2596 if (!priv->channel[i]->store) {
2597 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
2598 goto err_ring;
2599 }
2600 }
2601
2602 return 0;
2603
2604err_ring:
2605 for (i = 0; i < priv->num_channels; i++) {
2606 if (!priv->channel[i]->store)
2607 break;
2608 dpaa2_io_store_destroy(priv->channel[i]->store);
2609 }
2610
2611 return -ENOMEM;
2612}
2613
2614static void free_rings(struct dpaa2_eth_priv *priv)
2615{
2616 int i;
2617
2618 for (i = 0; i < priv->num_channels; i++)
2619 dpaa2_io_store_destroy(priv->channel[i]->store);
2620}
2621
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002622static int set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002623{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002624 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002625 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002626 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002627 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002628
2629 /* Get firmware address, if any */
2630 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
2631 if (err) {
2632 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
2633 return err;
2634 }
2635
2636 /* Get DPNI attributes address, if any */
2637 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2638 dpni_mac_addr);
2639 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002640 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002641 return err;
2642 }
2643
2644 /* First check if firmware has any address configured by bootloader */
2645 if (!is_zero_ether_addr(mac_addr)) {
2646 /* If the DPMAC addr != DPNI addr, update it */
2647 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
2648 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
2649 priv->mc_token,
2650 mac_addr);
2651 if (err) {
2652 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
2653 return err;
2654 }
2655 }
2656 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
2657 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002658 /* No MAC address configured, fill in net_dev->dev_addr
2659 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002660 */
2661 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002662 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
2663
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002664 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2665 net_dev->dev_addr);
2666 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002667 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002668 return err;
2669 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002670
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002671 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
2672 * practical purposes, this will be our "permanent" mac address,
2673 * at least until the next reboot. This move will also permit
2674 * register_netdevice() to properly fill up net_dev->perm_addr.
2675 */
2676 net_dev->addr_assign_type = NET_ADDR_PERM;
2677 } else {
2678 /* NET_ADDR_PERM is default, all we have to do is
2679 * fill in the device addr.
2680 */
2681 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
2682 }
2683
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002684 return 0;
2685}
2686
2687static int netdev_init(struct net_device *net_dev)
2688{
2689 struct device *dev = net_dev->dev.parent;
2690 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002691 u32 options = priv->dpni_attrs.options;
2692 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002693 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05002694 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002695 int err;
2696
2697 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002698 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002699
2700 err = set_mac_addr(priv);
2701 if (err)
2702 return err;
2703
2704 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002705 eth_broadcast_addr(bcast_addr);
2706 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
2707 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002708 dev_err(dev, "dpni_add_mac_addr() failed\n");
2709 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002710 }
2711
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05002712 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002713 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05002714 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05002715 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05002716 if (err) {
2717 dev_err(dev, "dpni_set_max_frame_length() failed\n");
2718 return err;
2719 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002720
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05002721 /* Set actual number of queues in the net device */
2722 num_queues = dpaa2_eth_queue_count(priv);
2723 err = netif_set_real_num_tx_queues(net_dev, num_queues);
2724 if (err) {
2725 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
2726 return err;
2727 }
2728 err = netif_set_real_num_rx_queues(net_dev, num_queues);
2729 if (err) {
2730 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
2731 return err;
2732 }
2733
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002734 /* Capabilities listing */
2735 supported |= IFF_LIVE_ADDR_CHANGE;
2736
2737 if (options & DPNI_OPT_NO_MAC_FILTER)
2738 not_supported |= IFF_UNICAST_FLT;
2739 else
2740 supported |= IFF_UNICAST_FLT;
2741
2742 net_dev->priv_flags |= supported;
2743 net_dev->priv_flags &= ~not_supported;
2744
2745 /* Features */
2746 net_dev->features = NETIF_F_RXCSUM |
2747 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2748 NETIF_F_SG | NETIF_F_HIGHDMA |
2749 NETIF_F_LLTX;
2750 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002751
2752 return 0;
2753}
2754
2755static int poll_link_state(void *arg)
2756{
2757 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
2758 int err;
2759
2760 while (!kthread_should_stop()) {
2761 err = link_state_update(priv);
2762 if (unlikely(err))
2763 return err;
2764
2765 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
2766 }
2767
2768 return 0;
2769}
2770
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002771static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
2772{
Ioana Radulescu112197d2017-10-11 08:29:49 -05002773 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002774 struct device *dev = (struct device *)arg;
2775 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
2776 struct net_device *net_dev = dev_get_drvdata(dev);
2777 int err;
2778
2779 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
2780 DPNI_IRQ_INDEX, &status);
2781 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002782 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05002783 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002784 }
2785
Ioana Radulescu112197d2017-10-11 08:29:49 -05002786 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002787 link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002788
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002789 return IRQ_HANDLED;
2790}
2791
2792static int setup_irqs(struct fsl_mc_device *ls_dev)
2793{
2794 int err = 0;
2795 struct fsl_mc_device_irq *irq;
2796
2797 err = fsl_mc_allocate_irqs(ls_dev);
2798 if (err) {
2799 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
2800 return err;
2801 }
2802
2803 irq = ls_dev->irqs[0];
2804 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05002805 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002806 IRQF_NO_SUSPEND | IRQF_ONESHOT,
2807 dev_name(&ls_dev->dev), &ls_dev->dev);
2808 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002809 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002810 goto free_mc_irq;
2811 }
2812
2813 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
2814 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED);
2815 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002816 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002817 goto free_irq;
2818 }
2819
2820 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
2821 DPNI_IRQ_INDEX, 1);
2822 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002823 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002824 goto free_irq;
2825 }
2826
2827 return 0;
2828
2829free_irq:
2830 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
2831free_mc_irq:
2832 fsl_mc_free_irqs(ls_dev);
2833
2834 return err;
2835}
2836
2837static void add_ch_napi(struct dpaa2_eth_priv *priv)
2838{
2839 int i;
2840 struct dpaa2_eth_channel *ch;
2841
2842 for (i = 0; i < priv->num_channels; i++) {
2843 ch = priv->channel[i];
2844 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
2845 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
2846 NAPI_POLL_WEIGHT);
2847 }
2848}
2849
2850static void del_ch_napi(struct dpaa2_eth_priv *priv)
2851{
2852 int i;
2853 struct dpaa2_eth_channel *ch;
2854
2855 for (i = 0; i < priv->num_channels; i++) {
2856 ch = priv->channel[i];
2857 netif_napi_del(&ch->napi);
2858 }
2859}
2860
2861static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
2862{
2863 struct device *dev;
2864 struct net_device *net_dev = NULL;
2865 struct dpaa2_eth_priv *priv = NULL;
2866 int err = 0;
2867
2868 dev = &dpni_dev->dev;
2869
2870 /* Net device */
2871 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_TX_QUEUES);
2872 if (!net_dev) {
2873 dev_err(dev, "alloc_etherdev_mq() failed\n");
2874 return -ENOMEM;
2875 }
2876
2877 SET_NETDEV_DEV(net_dev, dev);
2878 dev_set_drvdata(dev, net_dev);
2879
2880 priv = netdev_priv(net_dev);
2881 priv->net_dev = net_dev;
2882
Ioana Radulescu08eb2392017-05-24 07:13:27 -05002883 priv->iommu_domain = iommu_get_domain_for_dev(dev);
2884
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002885 /* Obtain a MC portal */
2886 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
2887 &priv->mc_io);
2888 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05002889 if (err == -ENXIO)
2890 err = -EPROBE_DEFER;
2891 else
2892 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002893 goto err_portal_alloc;
2894 }
2895
2896 /* MC objects initialization and configuration */
2897 err = setup_dpni(dpni_dev);
2898 if (err)
2899 goto err_dpni_setup;
2900
2901 err = setup_dpio(priv);
2902 if (err)
2903 goto err_dpio_setup;
2904
2905 setup_fqs(priv);
2906
2907 err = setup_dpbp(priv);
2908 if (err)
2909 goto err_dpbp_setup;
2910
2911 err = bind_dpni(priv);
2912 if (err)
2913 goto err_bind;
2914
2915 /* Add a NAPI context for each channel */
2916 add_ch_napi(priv);
2917
2918 /* Percpu statistics */
2919 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
2920 if (!priv->percpu_stats) {
2921 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
2922 err = -ENOMEM;
2923 goto err_alloc_percpu_stats;
2924 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002925 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
2926 if (!priv->percpu_extras) {
2927 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
2928 err = -ENOMEM;
2929 goto err_alloc_percpu_extras;
2930 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002931
2932 err = netdev_init(net_dev);
2933 if (err)
2934 goto err_netdev_init;
2935
2936 /* Configure checksum offload based on current interface flags */
2937 err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
2938 if (err)
2939 goto err_csum;
2940
2941 err = set_tx_csum(priv, !!(net_dev->features &
2942 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
2943 if (err)
2944 goto err_csum;
2945
2946 err = alloc_rings(priv);
2947 if (err)
2948 goto err_alloc_rings;
2949
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002950 err = setup_irqs(dpni_dev);
2951 if (err) {
2952 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
2953 priv->poll_thread = kthread_run(poll_link_state, priv,
2954 "%s_poll_link", net_dev->name);
2955 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002956 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002957 goto err_poll_thread;
2958 }
2959 priv->do_link_poll = true;
2960 }
2961
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002962 err = register_netdev(net_dev);
2963 if (err < 0) {
2964 dev_err(dev, "register_netdev() failed\n");
2965 goto err_netdev_reg;
2966 }
2967
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002968 dev_info(dev, "Probed interface %s\n", net_dev->name);
2969 return 0;
2970
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002971err_netdev_reg:
2972 if (priv->do_link_poll)
2973 kthread_stop(priv->poll_thread);
2974 else
2975 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002976err_poll_thread:
2977 free_rings(priv);
2978err_alloc_rings:
2979err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002980err_netdev_init:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002981 free_percpu(priv->percpu_extras);
2982err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002983 free_percpu(priv->percpu_stats);
2984err_alloc_percpu_stats:
2985 del_ch_napi(priv);
2986err_bind:
2987 free_dpbp(priv);
2988err_dpbp_setup:
2989 free_dpio(priv);
2990err_dpio_setup:
2991 free_dpni(priv);
2992err_dpni_setup:
2993 fsl_mc_portal_free(priv->mc_io);
2994err_portal_alloc:
2995 dev_set_drvdata(dev, NULL);
2996 free_netdev(net_dev);
2997
2998 return err;
2999}
3000
3001static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
3002{
3003 struct device *dev;
3004 struct net_device *net_dev;
3005 struct dpaa2_eth_priv *priv;
3006
3007 dev = &ls_dev->dev;
3008 net_dev = dev_get_drvdata(dev);
3009 priv = netdev_priv(net_dev);
3010
3011 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003012
3013 if (priv->do_link_poll)
3014 kthread_stop(priv->poll_thread);
3015 else
3016 fsl_mc_free_irqs(ls_dev);
3017
3018 free_rings(priv);
3019 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003020 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003021
3022 del_ch_napi(priv);
3023 free_dpbp(priv);
3024 free_dpio(priv);
3025 free_dpni(priv);
3026
3027 fsl_mc_portal_free(priv->mc_io);
3028
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003029 free_netdev(net_dev);
3030
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05003031 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05003032
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003033 return 0;
3034}
3035
3036static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
3037 {
3038 .vendor = FSL_MC_VENDOR_FREESCALE,
3039 .obj_type = "dpni",
3040 },
3041 { .vendor = 0x0 }
3042};
3043MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
3044
3045static struct fsl_mc_driver dpaa2_eth_driver = {
3046 .driver = {
3047 .name = KBUILD_MODNAME,
3048 .owner = THIS_MODULE,
3049 },
3050 .probe = dpaa2_eth_probe,
3051 .remove = dpaa2_eth_remove,
3052 .match_id_table = dpaa2_eth_match_id_table
3053};
3054
3055module_fsl_mc_driver(dpaa2_eth_driver);