blob: 174c960dd62df194a284f83220350d895e8110a2 [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 Radulescu569375f2018-11-26 16:27:31 +0000203/* Free buffers acquired from the buffer pool or which were meant to
204 * be released in the pool
205 */
206static void free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array, int count)
207{
208 struct device *dev = priv->net_dev->dev.parent;
209 void *vaddr;
210 int i;
211
212 for (i = 0; i < count; i++) {
213 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
214 dma_unmap_single(dev, buf_array[i], DPAA2_ETH_RX_BUF_SIZE,
215 DMA_FROM_DEVICE);
216 skb_free_frag(vaddr);
217 }
218}
219
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000220static u32 run_xdp(struct dpaa2_eth_priv *priv,
221 struct dpaa2_eth_channel *ch,
222 struct dpaa2_fd *fd, void *vaddr)
223{
224 struct bpf_prog *xdp_prog;
225 struct xdp_buff xdp;
226 u32 xdp_act = XDP_PASS;
227
228 rcu_read_lock();
229
230 xdp_prog = READ_ONCE(ch->xdp.prog);
231 if (!xdp_prog)
232 goto out;
233
234 xdp.data = vaddr + dpaa2_fd_get_offset(fd);
235 xdp.data_end = xdp.data + dpaa2_fd_get_len(fd);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000236 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000237 xdp_set_data_meta_invalid(&xdp);
238
239 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
240
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000241 /* xdp.data pointer may have changed */
242 dpaa2_fd_set_offset(fd, xdp.data - vaddr);
243 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
244
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000245 switch (xdp_act) {
246 case XDP_PASS:
247 break;
248 default:
249 bpf_warn_invalid_xdp_action(xdp_act);
250 case XDP_ABORTED:
251 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
252 case XDP_DROP:
253 ch->buf_count--;
254 free_rx_fd(priv, fd, vaddr);
255 break;
256 }
257
258out:
259 rcu_read_unlock();
260 return xdp_act;
261}
262
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500263/* Main Rx frame processing routine */
264static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
265 struct dpaa2_eth_channel *ch,
266 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000267 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500268{
269 dma_addr_t addr = dpaa2_fd_get_addr(fd);
270 u8 fd_format = dpaa2_fd_get_format(fd);
271 void *vaddr;
272 struct sk_buff *skb;
273 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500274 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500275 struct device *dev = priv->net_dev->dev.parent;
276 struct dpaa2_fas *fas;
Ioana Radulescud695e762017-06-06 10:00:35 -0500277 void *buf_data;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500278 u32 status = 0;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000279 u32 xdp_act;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500280
Ioana Radulescu56361872017-04-28 04:50:32 -0500281 /* Tracing point */
282 trace_dpaa2_rx_fd(priv->net_dev, fd);
283
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500284 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500285 dma_unmap_single(dev, addr, DPAA2_ETH_RX_BUF_SIZE, DMA_FROM_DEVICE);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500286
Ioana Radulescu54ce8912017-12-08 06:47:53 -0600287 fas = dpaa2_get_fas(vaddr, false);
Ioana Radulescud695e762017-06-06 10:00:35 -0500288 prefetch(fas);
289 buf_data = vaddr + dpaa2_fd_get_offset(fd);
290 prefetch(buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500291
292 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500293 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500294
295 if (fd_format == dpaa2_fd_single) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000296 xdp_act = run_xdp(priv, ch, (struct dpaa2_fd *)fd, vaddr);
297 if (xdp_act != XDP_PASS) {
298 percpu_stats->rx_packets++;
299 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
300 return;
301 }
302
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +0000303 skb = build_linear_skb(ch, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500304 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000305 WARN_ON(priv->xdp_prog);
306
Ioana Radulescud695e762017-06-06 10:00:35 -0500307 skb = build_frag_skb(priv, ch, buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500308 skb_free_frag(vaddr);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500309 percpu_extras->rx_sg_frames++;
310 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500311 } else {
312 /* We don't support any other format */
313 goto err_frame_format;
314 }
315
316 if (unlikely(!skb))
317 goto err_build_skb;
318
319 prefetch(skb->data);
320
Ioana Radulescu859f9982018-04-26 18:23:47 +0800321 /* Get the timestamp value */
322 if (priv->rx_tstamp) {
323 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
324 __le64 *ts = dpaa2_get_ts(vaddr, false);
325 u64 ns;
326
327 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
328
329 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
330 shhwtstamps->hwtstamp = ns_to_ktime(ns);
331 }
332
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500333 /* Check if we need to validate the L4 csum */
334 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500335 status = le32_to_cpu(fas->status);
336 validate_rx_csum(priv, status, skb);
337 }
338
339 skb->protocol = eth_type_trans(skb, priv->net_dev);
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000340 skb_record_rx_queue(skb, fq->flowid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500341
342 percpu_stats->rx_packets++;
343 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
344
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000345 napi_gro_receive(&ch->napi, skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500346
347 return;
348
349err_build_skb:
350 free_rx_fd(priv, fd, vaddr);
351err_frame_format:
352 percpu_stats->rx_dropped++;
353}
354
355/* Consume all frames pull-dequeued into the store. This is the simplest way to
356 * make sure we don't accidentally issue another volatile dequeue which would
357 * overwrite (leak) frames already in the store.
358 *
359 * Observance of NAPI budget is not our concern, leaving that to the caller.
360 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000361static int consume_frames(struct dpaa2_eth_channel *ch,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000362 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500363{
364 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000365 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500366 struct dpaa2_dq *dq;
367 const struct dpaa2_fd *fd;
368 int cleaned = 0;
369 int is_last;
370
371 do {
372 dq = dpaa2_io_store_next(ch->store, &is_last);
373 if (unlikely(!dq)) {
374 /* If we're here, we *must* have placed a
375 * volatile dequeue comnmand, so keep reading through
376 * the store until we get some sort of valid response
377 * token (either a valid frame or an "empty dequeue")
378 */
379 continue;
380 }
381
382 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600383 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500384
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000385 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500386 cleaned++;
387 } while (!is_last);
388
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000389 if (!cleaned)
390 return 0;
391
392 fq->stats.frames += cleaned;
393 ch->stats.frames += cleaned;
394
395 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000396 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000397 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000398 if (src)
399 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000400
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500401 return cleaned;
402}
403
Ioana Radulescu859f9982018-04-26 18:23:47 +0800404/* Configure the egress frame annotation for timestamp update */
405static void enable_tx_tstamp(struct dpaa2_fd *fd, void *buf_start)
406{
407 struct dpaa2_faead *faead;
408 u32 ctrl, frc;
409
410 /* Mark the egress frame annotation area as valid */
411 frc = dpaa2_fd_get_frc(fd);
412 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
413
414 /* Set hardware annotation size */
415 ctrl = dpaa2_fd_get_ctrl(fd);
416 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
417
418 /* enable UPD (update prepanded data) bit in FAEAD field of
419 * hardware frame annotation area
420 */
421 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
422 faead = dpaa2_get_faead(buf_start, true);
423 faead->ctrl = cpu_to_le32(ctrl);
424}
425
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500426/* Create a frame descriptor based on a fragmented skb */
427static int build_sg_fd(struct dpaa2_eth_priv *priv,
428 struct sk_buff *skb,
429 struct dpaa2_fd *fd)
430{
431 struct device *dev = priv->net_dev->dev.parent;
432 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500433 dma_addr_t addr;
434 int nr_frags = skb_shinfo(skb)->nr_frags;
435 struct dpaa2_sg_entry *sgt;
436 int i, err;
437 int sgt_buf_size;
438 struct scatterlist *scl, *crt_scl;
439 int num_sg;
440 int num_dma_bufs;
441 struct dpaa2_eth_swa *swa;
442
443 /* Create and map scatterlist.
444 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
445 * to go beyond nr_frags+1.
446 * Note: We don't support chained scatterlists
447 */
448 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
449 return -EINVAL;
450
451 scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
452 if (unlikely(!scl))
453 return -ENOMEM;
454
455 sg_init_table(scl, nr_frags + 1);
456 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500457 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500458 if (unlikely(!num_dma_bufs)) {
459 err = -ENOMEM;
460 goto dma_map_sg_failed;
461 }
462
463 /* Prepare the HW SGT structure */
464 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500465 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500466 sgt_buf = netdev_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500467 if (unlikely(!sgt_buf)) {
468 err = -ENOMEM;
469 goto sgt_buf_alloc_failed;
470 }
471 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500472 memset(sgt_buf, 0, sgt_buf_size);
473
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500474 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
475
476 /* Fill in the HW SGT structure.
477 *
478 * sgt_buf is zeroed out, so the following fields are implicit
479 * in all sgt entries:
480 * - offset is 0
481 * - format is 'dpaa2_sg_single'
482 */
483 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
484 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
485 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
486 }
487 dpaa2_sg_set_final(&sgt[i - 1], true);
488
489 /* Store the skb backpointer in the SGT buffer.
490 * Fit the scatterlist and the number of buffers alongside the
491 * skb backpointer in the software annotation area. We'll need
492 * all of them on Tx Conf.
493 */
494 swa = (struct dpaa2_eth_swa *)sgt_buf;
495 swa->skb = skb;
496 swa->scl = scl;
497 swa->num_sg = num_sg;
Ioana Radulescub2718e62018-03-23 08:44:11 -0500498 swa->sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500499
500 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500501 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500502 if (unlikely(dma_mapping_error(dev, addr))) {
503 err = -ENOMEM;
504 goto dma_map_single_failed;
505 }
506 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
507 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
508 dpaa2_fd_set_addr(fd, addr);
509 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000510 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500511
Ioana Radulescu859f9982018-04-26 18:23:47 +0800512 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
513 enable_tx_tstamp(fd, sgt_buf);
514
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500515 return 0;
516
517dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500518 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500519sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500520 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500521dma_map_sg_failed:
522 kfree(scl);
523 return err;
524}
525
526/* Create a frame descriptor based on a linear skb */
527static int build_single_fd(struct dpaa2_eth_priv *priv,
528 struct sk_buff *skb,
529 struct dpaa2_fd *fd)
530{
531 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600532 u8 *buffer_start, *aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500533 struct sk_buff **skbh;
534 dma_addr_t addr;
535
Ioana Radulescuc1636852017-12-08 06:47:58 -0600536 buffer_start = skb->data - dpaa2_eth_needed_headroom(priv, skb);
537
538 /* If there's enough room to align the FD address, do it.
539 * It will help hardware optimize accesses.
540 */
541 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
542 DPAA2_ETH_TX_BUF_ALIGN);
543 if (aligned_start >= skb->head)
544 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500545
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500546 /* Store a backpointer to the skb at the beginning of the buffer
547 * (in the private data area) such that we can release it
548 * on Tx confirm
549 */
550 skbh = (struct sk_buff **)buffer_start;
551 *skbh = skb;
552
553 addr = dma_map_single(dev, buffer_start,
554 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500555 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500556 if (unlikely(dma_mapping_error(dev, addr)))
557 return -ENOMEM;
558
559 dpaa2_fd_set_addr(fd, addr);
560 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
561 dpaa2_fd_set_len(fd, skb->len);
562 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000563 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500564
Ioana Radulescu859f9982018-04-26 18:23:47 +0800565 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
566 enable_tx_tstamp(fd, buffer_start);
567
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500568 return 0;
569}
570
571/* FD freeing routine on the Tx path
572 *
573 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
574 * back-pointed to is also freed.
575 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
576 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500577 */
578static void free_tx_fd(const struct dpaa2_eth_priv *priv,
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600579 const struct dpaa2_fd *fd)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500580{
581 struct device *dev = priv->net_dev->dev.parent;
582 dma_addr_t fd_addr;
583 struct sk_buff **skbh, *skb;
584 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500585 struct dpaa2_eth_swa *swa;
586 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500587
588 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500589 skbh = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500590
591 if (fd_format == dpaa2_fd_single) {
592 skb = *skbh;
593 buffer_start = (unsigned char *)skbh;
594 /* Accessing the skb buffer is safe before dma unmap, because
595 * we didn't map the actual skb shell.
596 */
597 dma_unmap_single(dev, fd_addr,
598 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500599 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500600 } else if (fd_format == dpaa2_fd_sg) {
601 swa = (struct dpaa2_eth_swa *)skbh;
602 skb = swa->skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500603
604 /* Unmap the scatterlist */
Ioana Radulescub2718e62018-03-23 08:44:11 -0500605 dma_unmap_sg(dev, swa->scl, swa->num_sg, DMA_BIDIRECTIONAL);
606 kfree(swa->scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500607
608 /* Unmap the SGT buffer */
Ioana Radulescub2718e62018-03-23 08:44:11 -0500609 dma_unmap_single(dev, fd_addr, swa->sgt_size,
610 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500611 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600612 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500613 return;
614 }
615
Ioana Radulescu859f9982018-04-26 18:23:47 +0800616 /* Get the timestamp value */
617 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
618 struct skb_shared_hwtstamps shhwtstamps;
619 __le64 *ts = dpaa2_get_ts(skbh, true);
620 u64 ns;
621
622 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
623
624 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
625 shhwtstamps.hwtstamp = ns_to_ktime(ns);
626 skb_tstamp_tx(skb, &shhwtstamps);
627 }
628
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500629 /* Free SGT buffer allocated on tx */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500630 if (fd_format != dpaa2_fd_single)
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500631 skb_free_frag(skbh);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500632
633 /* Move on with skb release */
634 dev_kfree_skb(skb);
635}
636
Ioana Radulescuc433db42017-06-06 10:00:26 -0500637static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500638{
639 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
640 struct dpaa2_fd fd;
641 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500642 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500643 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000644 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500645 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -0600646 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000647 u32 fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500648 int err, i;
649
650 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500651 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500652
Ioana Radulescu18c21462017-12-08 06:47:57 -0600653 needed_headroom = dpaa2_eth_needed_headroom(priv, skb);
654 if (skb_headroom(skb) < needed_headroom) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500655 struct sk_buff *ns;
656
Ioana Radulescu18c21462017-12-08 06:47:57 -0600657 ns = skb_realloc_headroom(skb, needed_headroom);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500658 if (unlikely(!ns)) {
659 percpu_stats->tx_dropped++;
660 goto err_alloc_headroom;
661 }
Ioana Radulescu6662b5e2017-12-08 06:47:55 -0600662 percpu_extras->tx_reallocs++;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800663
664 if (skb->sk)
665 skb_set_owner_w(ns, skb->sk);
666
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500667 dev_kfree_skb(skb);
668 skb = ns;
669 }
670
671 /* We'll be holding a back-reference to the skb until Tx Confirmation;
672 * we don't want that overwritten by a concurrent Tx with a cloned skb.
673 */
674 skb = skb_unshare(skb, GFP_ATOMIC);
675 if (unlikely(!skb)) {
676 /* skb_unshare() has already freed the skb */
677 percpu_stats->tx_dropped++;
678 return NETDEV_TX_OK;
679 }
680
681 /* Setup the FD fields */
682 memset(&fd, 0, sizeof(fd));
683
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500684 if (skb_is_nonlinear(skb)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500685 err = build_sg_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500686 percpu_extras->tx_sg_frames++;
687 percpu_extras->tx_sg_bytes += skb->len;
688 } else {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500689 err = build_single_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500690 }
691
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500692 if (unlikely(err)) {
693 percpu_stats->tx_dropped++;
694 goto err_build_fd;
695 }
696
Ioana Radulescu56361872017-04-28 04:50:32 -0500697 /* Tracing point */
698 trace_dpaa2_tx_fd(net_dev, &fd);
699
Ioana Radulescu537336c2017-12-21 06:33:20 -0600700 /* TxConf FQ selection relies on queue id from the stack.
701 * In case of a forwarded frame from another DPNI interface, we choose
702 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500703 */
Ioana Radulescu537336c2017-12-21 06:33:20 -0600704 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500705 fq = &priv->fq[queue_mapping];
706 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600707 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
708 priv->tx_qdid, 0,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500709 fq->tx_qdbin, &fd);
710 if (err != -EBUSY)
711 break;
712 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500713 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500714 if (unlikely(err < 0)) {
715 percpu_stats->tx_errors++;
716 /* Clean up everything, including freeing the skb */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600717 free_tx_fd(priv, &fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500718 } else {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000719 fd_len = dpaa2_fd_get_len(&fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500720 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000721 percpu_stats->tx_bytes += fd_len;
722
723 nq = netdev_get_tx_queue(net_dev, queue_mapping);
724 netdev_tx_sent_queue(nq, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500725 }
726
727 return NETDEV_TX_OK;
728
729err_build_fd:
730err_alloc_headroom:
731 dev_kfree_skb(skb);
732
733 return NETDEV_TX_OK;
734}
735
736/* Tx confirmation frame processing routine */
737static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +0000738 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500739 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000740 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500741{
742 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500743 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000744 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500745 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500746
Ioana Radulescu56361872017-04-28 04:50:32 -0500747 /* Tracing point */
748 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
749
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500750 percpu_extras = this_cpu_ptr(priv->percpu_extras);
751 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000752 percpu_extras->tx_conf_bytes += fd_len;
753
754 fq->dq_frames++;
755 fq->dq_bytes += fd_len;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500756
Ioana Radulescu39163c02017-06-06 10:00:39 -0500757 /* Check frame errors in the FD field */
758 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600759 free_tx_fd(priv, fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500760
761 if (likely(!fd_errors))
762 return;
763
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600764 if (net_ratelimit())
765 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
766 fd_errors);
767
Ioana Radulescu39163c02017-06-06 10:00:39 -0500768 percpu_stats = this_cpu_ptr(priv->percpu_stats);
769 /* Tx-conf logically pertains to the egress path. */
770 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500771}
772
773static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
774{
775 int err;
776
777 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
778 DPNI_OFF_RX_L3_CSUM, enable);
779 if (err) {
780 netdev_err(priv->net_dev,
781 "dpni_set_offload(RX_L3_CSUM) failed\n");
782 return err;
783 }
784
785 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
786 DPNI_OFF_RX_L4_CSUM, enable);
787 if (err) {
788 netdev_err(priv->net_dev,
789 "dpni_set_offload(RX_L4_CSUM) failed\n");
790 return err;
791 }
792
793 return 0;
794}
795
796static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
797{
798 int err;
799
800 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
801 DPNI_OFF_TX_L3_CSUM, enable);
802 if (err) {
803 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
804 return err;
805 }
806
807 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
808 DPNI_OFF_TX_L4_CSUM, enable);
809 if (err) {
810 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
811 return err;
812 }
813
814 return 0;
815}
816
817/* Perform a single release command to add buffers
818 * to the specified buffer pool
819 */
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600820static int add_bufs(struct dpaa2_eth_priv *priv,
821 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500822{
823 struct device *dev = priv->net_dev->dev.parent;
824 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
825 void *buf;
826 dma_addr_t addr;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500827 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500828
829 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
830 /* Allocate buffer visible to WRIOP + skb shared info +
831 * alignment padding
832 */
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +0000833 buf = napi_alloc_frag(dpaa2_eth_buf_raw_size(priv));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500834 if (unlikely(!buf))
835 goto err_alloc;
836
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +0000837 buf = PTR_ALIGN(buf, priv->rx_buf_align);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500838
839 addr = dma_map_single(dev, buf, DPAA2_ETH_RX_BUF_SIZE,
840 DMA_FROM_DEVICE);
841 if (unlikely(dma_mapping_error(dev, addr)))
842 goto err_map;
843
844 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -0500845
846 /* tracing point */
847 trace_dpaa2_eth_buf_seed(priv->net_dev,
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +0000848 buf, dpaa2_eth_buf_raw_size(priv),
Ioana Radulescu56361872017-04-28 04:50:32 -0500849 addr, DPAA2_ETH_RX_BUF_SIZE,
850 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500851 }
852
853release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500854 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600855 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500856 buf_array, i)) == -EBUSY)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500857 cpu_relax();
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500858
859 /* If release command failed, clean up and bail out;
860 * not much else we can do about it
861 */
862 if (err) {
863 free_bufs(priv, buf_array, i);
864 return 0;
865 }
866
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500867 return i;
868
869err_map:
870 skb_free_frag(buf);
871err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500872 /* If we managed to allocate at least some buffers,
873 * release them to hardware
874 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500875 if (i)
876 goto release_bufs;
877
878 return 0;
879}
880
881static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
882{
883 int i, j;
884 int new_count;
885
886 /* This is the lazy seeding of Rx buffer pools.
887 * dpaa2_add_bufs() is also used on the Rx hotpath and calls
888 * napi_alloc_frag(). The trouble with that is that it in turn ends up
889 * calling this_cpu_ptr(), which mandates execution in atomic context.
890 * Rather than splitting up the code, do a one-off preempt disable.
891 */
892 preempt_disable();
893 for (j = 0; j < priv->num_channels; j++) {
894 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
895 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600896 new_count = add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500897 priv->channel[j]->buf_count += new_count;
898
899 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
900 preempt_enable();
901 return -ENOMEM;
902 }
903 }
904 }
905 preempt_enable();
906
907 return 0;
908}
909
910/**
911 * Drain the specified number of buffers from the DPNI's private buffer pool.
912 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
913 */
914static void drain_bufs(struct dpaa2_eth_priv *priv, int count)
915{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500916 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500917 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500918
919 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -0500920 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500921 buf_array, count);
922 if (ret < 0) {
923 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
924 return;
925 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500926 free_bufs(priv, buf_array, ret);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500927 } while (ret);
928}
929
930static void drain_pool(struct dpaa2_eth_priv *priv)
931{
932 int i;
933
934 drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
935 drain_bufs(priv, 1);
936
937 for (i = 0; i < priv->num_channels; i++)
938 priv->channel[i]->buf_count = 0;
939}
940
941/* Function is called from softirq context only, so we don't need to guard
942 * the access to percpu count
943 */
944static int refill_pool(struct dpaa2_eth_priv *priv,
945 struct dpaa2_eth_channel *ch,
946 u16 bpid)
947{
948 int new_count;
949
950 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
951 return 0;
952
953 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600954 new_count = add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500955 if (unlikely(!new_count)) {
956 /* Out of memory; abort for now, we'll try later on */
957 break;
958 }
959 ch->buf_count += new_count;
960 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
961
962 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
963 return -ENOMEM;
964
965 return 0;
966}
967
968static int pull_channel(struct dpaa2_eth_channel *ch)
969{
970 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500971 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500972
973 /* Retry while portal is busy */
974 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600975 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
976 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500977 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500978 cpu_relax();
979 } while (err == -EBUSY);
980
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500981 ch->stats.dequeue_portal_busy += dequeues;
982 if (unlikely(err))
983 ch->stats.pull_err++;
984
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500985 return err;
986}
987
988/* NAPI poll routine
989 *
990 * Frames are dequeued from the QMan channel associated with this NAPI context.
991 * Rx, Tx confirmation and (if configured) Rx error frames all count
992 * towards the NAPI budget.
993 */
994static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
995{
996 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500997 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000998 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000999 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1000 struct netdev_queue *nq;
1001 int store_cleaned, work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001002 int err;
1003
1004 ch = container_of(napi, struct dpaa2_eth_channel, napi);
1005 priv = ch->priv;
1006
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001007 do {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001008 err = pull_channel(ch);
1009 if (unlikely(err))
1010 break;
1011
1012 /* Refill pool if appropriate */
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001013 refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001014
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001015 store_cleaned = consume_frames(ch, &fq);
1016 if (!store_cleaned)
1017 break;
1018 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001019 rx_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001020 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001021 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001022 /* We have a single Tx conf FQ on this channel */
1023 txc_fq = fq;
1024 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001025
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001026 /* If we either consumed the whole NAPI budget with Rx frames
1027 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001028 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001029 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001030 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1031 work_done = budget;
1032 goto out;
1033 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001034 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001035
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001036 /* We didn't consume the entire budget, so finish napi and
1037 * re-enable data availability notifications
1038 */
1039 napi_complete_done(napi, rx_cleaned);
1040 do {
1041 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1042 cpu_relax();
1043 } while (err == -EBUSY);
1044 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1045 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001046
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001047 work_done = max(rx_cleaned, 1);
1048
1049out:
1050 if (txc_fq) {
1051 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1052 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1053 txc_fq->dq_bytes);
1054 txc_fq->dq_frames = 0;
1055 txc_fq->dq_bytes = 0;
1056 }
1057
1058 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001059}
1060
1061static void enable_ch_napi(struct dpaa2_eth_priv *priv)
1062{
1063 struct dpaa2_eth_channel *ch;
1064 int i;
1065
1066 for (i = 0; i < priv->num_channels; i++) {
1067 ch = priv->channel[i];
1068 napi_enable(&ch->napi);
1069 }
1070}
1071
1072static void disable_ch_napi(struct dpaa2_eth_priv *priv)
1073{
1074 struct dpaa2_eth_channel *ch;
1075 int i;
1076
1077 for (i = 0; i < priv->num_channels; i++) {
1078 ch = priv->channel[i];
1079 napi_disable(&ch->napi);
1080 }
1081}
1082
1083static int link_state_update(struct dpaa2_eth_priv *priv)
1084{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001085 struct dpni_link_state state = {0};
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001086 int err;
1087
1088 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1089 if (unlikely(err)) {
1090 netdev_err(priv->net_dev,
1091 "dpni_get_link_state() failed\n");
1092 return err;
1093 }
1094
1095 /* Chech link state; speed / duplex changes are not treated yet */
1096 if (priv->link_state.up == state.up)
1097 return 0;
1098
1099 priv->link_state = state;
1100 if (state.up) {
1101 netif_carrier_on(priv->net_dev);
1102 netif_tx_start_all_queues(priv->net_dev);
1103 } else {
1104 netif_tx_stop_all_queues(priv->net_dev);
1105 netif_carrier_off(priv->net_dev);
1106 }
1107
Ioana Radulescu77160af2017-06-06 10:00:28 -05001108 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001109 state.up ? "up" : "down");
1110
1111 return 0;
1112}
1113
1114static int dpaa2_eth_open(struct net_device *net_dev)
1115{
1116 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1117 int err;
1118
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001119 err = seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001120 if (err) {
1121 /* Not much to do; the buffer pool, though not filled up,
1122 * may still contain some buffers which would enable us
1123 * to limp on.
1124 */
1125 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001126 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001127 }
1128
1129 /* We'll only start the txqs when the link is actually ready; make sure
1130 * we don't race against the link up notification, which may come
1131 * immediately after dpni_enable();
1132 */
1133 netif_tx_stop_all_queues(net_dev);
1134 enable_ch_napi(priv);
1135 /* Also, explicitly set carrier off, otherwise netif_carrier_ok() will
1136 * return true and cause 'ip link show' to report the LOWER_UP flag,
1137 * even though the link notification wasn't even received.
1138 */
1139 netif_carrier_off(net_dev);
1140
1141 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1142 if (err < 0) {
1143 netdev_err(net_dev, "dpni_enable() failed\n");
1144 goto enable_err;
1145 }
1146
1147 /* If the DPMAC object has already processed the link up interrupt,
1148 * we have to learn the link state ourselves.
1149 */
1150 err = link_state_update(priv);
1151 if (err < 0) {
1152 netdev_err(net_dev, "Can't update link state\n");
1153 goto link_state_err;
1154 }
1155
1156 return 0;
1157
1158link_state_err:
1159enable_err:
1160 disable_ch_napi(priv);
1161 drain_pool(priv);
1162 return err;
1163}
1164
1165/* The DPIO store must be empty when we call this,
1166 * at the end of every NAPI cycle.
1167 */
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +00001168static u32 drain_channel(struct dpaa2_eth_channel *ch)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001169{
1170 u32 drained = 0, total = 0;
1171
1172 do {
1173 pull_channel(ch);
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001174 drained = consume_frames(ch, NULL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001175 total += drained;
1176 } while (drained);
1177
1178 return total;
1179}
1180
1181static u32 drain_ingress_frames(struct dpaa2_eth_priv *priv)
1182{
1183 struct dpaa2_eth_channel *ch;
1184 int i;
1185 u32 drained = 0;
1186
1187 for (i = 0; i < priv->num_channels; i++) {
1188 ch = priv->channel[i];
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +00001189 drained += drain_channel(ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001190 }
1191
1192 return drained;
1193}
1194
1195static int dpaa2_eth_stop(struct net_device *net_dev)
1196{
1197 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001198 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001199 int retries = 10;
1200 u32 drained;
1201
1202 netif_tx_stop_all_queues(net_dev);
1203 netif_carrier_off(net_dev);
1204
1205 /* Loop while dpni_disable() attempts to drain the egress FQs
1206 * and confirm them back to us.
1207 */
1208 do {
1209 dpni_disable(priv->mc_io, 0, priv->mc_token);
1210 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1211 if (dpni_enabled)
1212 /* Allow the hardware some slack */
1213 msleep(100);
1214 } while (dpni_enabled && --retries);
1215 if (!retries) {
1216 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1217 /* Must go on and disable NAPI nonetheless, so we don't crash at
1218 * the next "ifconfig up"
1219 */
1220 }
1221
1222 /* Wait for NAPI to complete on every core and disable it.
1223 * In particular, this will also prevent NAPI from being rescheduled if
1224 * a new CDAN is serviced, effectively discarding the CDAN. We therefore
1225 * don't even need to disarm the channels, except perhaps for the case
1226 * of a huge coalescing value.
1227 */
1228 disable_ch_napi(priv);
1229
1230 /* Manually drain the Rx and TxConf queues */
1231 drained = drain_ingress_frames(priv);
1232 if (drained)
1233 netdev_dbg(net_dev, "Drained %d frames.\n", drained);
1234
1235 /* Empty the buffer pool */
1236 drain_pool(priv);
1237
1238 return 0;
1239}
1240
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001241static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1242{
1243 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1244 struct device *dev = net_dev->dev.parent;
1245 int err;
1246
1247 err = eth_mac_addr(net_dev, addr);
1248 if (err < 0) {
1249 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1250 return err;
1251 }
1252
1253 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1254 net_dev->dev_addr);
1255 if (err) {
1256 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1257 return err;
1258 }
1259
1260 return 0;
1261}
1262
1263/** Fill in counters maintained by the GPP driver. These may be different from
1264 * the hardware counters obtained by ethtool.
1265 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001266static void dpaa2_eth_get_stats(struct net_device *net_dev,
1267 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001268{
1269 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1270 struct rtnl_link_stats64 *percpu_stats;
1271 u64 *cpustats;
1272 u64 *netstats = (u64 *)stats;
1273 int i, j;
1274 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1275
1276 for_each_possible_cpu(i) {
1277 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1278 cpustats = (u64 *)percpu_stats;
1279 for (j = 0; j < num; j++)
1280 netstats[j] += cpustats[j];
1281 }
1282}
1283
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001284/* Copy mac unicast addresses from @net_dev to @priv.
1285 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1286 */
1287static void add_uc_hw_addr(const struct net_device *net_dev,
1288 struct dpaa2_eth_priv *priv)
1289{
1290 struct netdev_hw_addr *ha;
1291 int err;
1292
1293 netdev_for_each_uc_addr(ha, net_dev) {
1294 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1295 ha->addr);
1296 if (err)
1297 netdev_warn(priv->net_dev,
1298 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1299 ha->addr, err);
1300 }
1301}
1302
1303/* Copy mac multicast addresses from @net_dev to @priv
1304 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1305 */
1306static void add_mc_hw_addr(const struct net_device *net_dev,
1307 struct dpaa2_eth_priv *priv)
1308{
1309 struct netdev_hw_addr *ha;
1310 int err;
1311
1312 netdev_for_each_mc_addr(ha, net_dev) {
1313 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1314 ha->addr);
1315 if (err)
1316 netdev_warn(priv->net_dev,
1317 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1318 ha->addr, err);
1319 }
1320}
1321
1322static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1323{
1324 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1325 int uc_count = netdev_uc_count(net_dev);
1326 int mc_count = netdev_mc_count(net_dev);
1327 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1328 u32 options = priv->dpni_attrs.options;
1329 u16 mc_token = priv->mc_token;
1330 struct fsl_mc_io *mc_io = priv->mc_io;
1331 int err;
1332
1333 /* Basic sanity checks; these probably indicate a misconfiguration */
1334 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1335 netdev_info(net_dev,
1336 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1337 max_mac);
1338
1339 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1340 if (uc_count > max_mac) {
1341 netdev_info(net_dev,
1342 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1343 uc_count, max_mac);
1344 goto force_promisc;
1345 }
1346 if (mc_count + uc_count > max_mac) {
1347 netdev_info(net_dev,
1348 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1349 uc_count + mc_count, max_mac);
1350 goto force_mc_promisc;
1351 }
1352
1353 /* Adjust promisc settings due to flag combinations */
1354 if (net_dev->flags & IFF_PROMISC)
1355 goto force_promisc;
1356 if (net_dev->flags & IFF_ALLMULTI) {
1357 /* First, rebuild unicast filtering table. This should be done
1358 * in promisc mode, in order to avoid frame loss while we
1359 * progressively add entries to the table.
1360 * We don't know whether we had been in promisc already, and
1361 * making an MC call to find out is expensive; so set uc promisc
1362 * nonetheless.
1363 */
1364 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1365 if (err)
1366 netdev_warn(net_dev, "Can't set uc promisc\n");
1367
1368 /* Actual uc table reconstruction. */
1369 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1370 if (err)
1371 netdev_warn(net_dev, "Can't clear uc filters\n");
1372 add_uc_hw_addr(net_dev, priv);
1373
1374 /* Finally, clear uc promisc and set mc promisc as requested. */
1375 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1376 if (err)
1377 netdev_warn(net_dev, "Can't clear uc promisc\n");
1378 goto force_mc_promisc;
1379 }
1380
1381 /* Neither unicast, nor multicast promisc will be on... eventually.
1382 * For now, rebuild mac filtering tables while forcing both of them on.
1383 */
1384 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1385 if (err)
1386 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1387 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1388 if (err)
1389 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1390
1391 /* Actual mac filtering tables reconstruction */
1392 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1393 if (err)
1394 netdev_warn(net_dev, "Can't clear mac filters\n");
1395 add_mc_hw_addr(net_dev, priv);
1396 add_uc_hw_addr(net_dev, priv);
1397
1398 /* Now we can clear both ucast and mcast promisc, without risking
1399 * to drop legitimate frames anymore.
1400 */
1401 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1402 if (err)
1403 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1404 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1405 if (err)
1406 netdev_warn(net_dev, "Can't clear mcast promisc\n");
1407
1408 return;
1409
1410force_promisc:
1411 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1412 if (err)
1413 netdev_warn(net_dev, "Can't set ucast promisc\n");
1414force_mc_promisc:
1415 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1416 if (err)
1417 netdev_warn(net_dev, "Can't set mcast promisc\n");
1418}
1419
1420static int dpaa2_eth_set_features(struct net_device *net_dev,
1421 netdev_features_t features)
1422{
1423 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1424 netdev_features_t changed = features ^ net_dev->features;
1425 bool enable;
1426 int err;
1427
1428 if (changed & NETIF_F_RXCSUM) {
1429 enable = !!(features & NETIF_F_RXCSUM);
1430 err = set_rx_csum(priv, enable);
1431 if (err)
1432 return err;
1433 }
1434
1435 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1436 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
1437 err = set_tx_csum(priv, enable);
1438 if (err)
1439 return err;
1440 }
1441
1442 return 0;
1443}
1444
Ioana Radulescu859f9982018-04-26 18:23:47 +08001445static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1446{
1447 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1448 struct hwtstamp_config config;
1449
1450 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
1451 return -EFAULT;
1452
1453 switch (config.tx_type) {
1454 case HWTSTAMP_TX_OFF:
1455 priv->tx_tstamp = false;
1456 break;
1457 case HWTSTAMP_TX_ON:
1458 priv->tx_tstamp = true;
1459 break;
1460 default:
1461 return -ERANGE;
1462 }
1463
1464 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
1465 priv->rx_tstamp = false;
1466 } else {
1467 priv->rx_tstamp = true;
1468 /* TS is set for all frame types, not only those requested */
1469 config.rx_filter = HWTSTAMP_FILTER_ALL;
1470 }
1471
1472 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
1473 -EFAULT : 0;
1474}
1475
1476static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1477{
1478 if (cmd == SIOCSHWTSTAMP)
1479 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
1480
1481 return -EINVAL;
1482}
1483
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001484static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
1485{
1486 int mfl, linear_mfl;
1487
1488 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1489 linear_mfl = DPAA2_ETH_RX_BUF_SIZE - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001490 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001491
1492 if (mfl > linear_mfl) {
1493 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
1494 linear_mfl - VLAN_ETH_HLEN);
1495 return false;
1496 }
1497
1498 return true;
1499}
1500
1501static int set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
1502{
1503 int mfl, err;
1504
1505 /* We enforce a maximum Rx frame length based on MTU only if we have
1506 * an XDP program attached (in order to avoid Rx S/G frames).
1507 * Otherwise, we accept all incoming frames as long as they are not
1508 * larger than maximum size supported in hardware
1509 */
1510 if (has_xdp)
1511 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1512 else
1513 mfl = DPAA2_ETH_MFL;
1514
1515 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
1516 if (err) {
1517 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
1518 return err;
1519 }
1520
1521 return 0;
1522}
1523
1524static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
1525{
1526 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1527 int err;
1528
1529 if (!priv->xdp_prog)
1530 goto out;
1531
1532 if (!xdp_mtu_valid(priv, new_mtu))
1533 return -EINVAL;
1534
1535 err = set_rx_mfl(priv, new_mtu, true);
1536 if (err)
1537 return err;
1538
1539out:
1540 dev->mtu = new_mtu;
1541 return 0;
1542}
1543
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001544static int update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
1545{
1546 struct dpni_buffer_layout buf_layout = {0};
1547 int err;
1548
1549 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
1550 DPNI_QUEUE_RX, &buf_layout);
1551 if (err) {
1552 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
1553 return err;
1554 }
1555
1556 /* Reserve extra headroom for XDP header size changes */
1557 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
1558 (has_xdp ? XDP_PACKET_HEADROOM : 0);
1559 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
1560 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
1561 DPNI_QUEUE_RX, &buf_layout);
1562 if (err) {
1563 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
1564 return err;
1565 }
1566
1567 return 0;
1568}
1569
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001570static int setup_xdp(struct net_device *dev, struct bpf_prog *prog)
1571{
1572 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1573 struct dpaa2_eth_channel *ch;
1574 struct bpf_prog *old;
1575 bool up, need_update;
1576 int i, err;
1577
1578 if (prog && !xdp_mtu_valid(priv, dev->mtu))
1579 return -EINVAL;
1580
1581 if (prog) {
1582 prog = bpf_prog_add(prog, priv->num_channels);
1583 if (IS_ERR(prog))
1584 return PTR_ERR(prog);
1585 }
1586
1587 up = netif_running(dev);
1588 need_update = (!!priv->xdp_prog != !!prog);
1589
1590 if (up)
1591 dpaa2_eth_stop(dev);
1592
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001593 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
1594 * Also, when switching between xdp/non-xdp modes we need to reconfigure
1595 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
1596 * so we are sure no old format buffers will be used from now on.
1597 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001598 if (need_update) {
1599 err = set_rx_mfl(priv, dev->mtu, !!prog);
1600 if (err)
1601 goto out_err;
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001602 err = update_rx_buffer_headroom(priv, !!prog);
1603 if (err)
1604 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001605 }
1606
1607 old = xchg(&priv->xdp_prog, prog);
1608 if (old)
1609 bpf_prog_put(old);
1610
1611 for (i = 0; i < priv->num_channels; i++) {
1612 ch = priv->channel[i];
1613 old = xchg(&ch->xdp.prog, prog);
1614 if (old)
1615 bpf_prog_put(old);
1616 }
1617
1618 if (up) {
1619 err = dpaa2_eth_open(dev);
1620 if (err)
1621 return err;
1622 }
1623
1624 return 0;
1625
1626out_err:
1627 if (prog)
1628 bpf_prog_sub(prog, priv->num_channels);
1629 if (up)
1630 dpaa2_eth_open(dev);
1631
1632 return err;
1633}
1634
1635static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1636{
1637 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1638
1639 switch (xdp->command) {
1640 case XDP_SETUP_PROG:
1641 return setup_xdp(dev, xdp->prog);
1642 case XDP_QUERY_PROG:
1643 xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0;
1644 break;
1645 default:
1646 return -EINVAL;
1647 }
1648
1649 return 0;
1650}
1651
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001652static const struct net_device_ops dpaa2_eth_ops = {
1653 .ndo_open = dpaa2_eth_open,
1654 .ndo_start_xmit = dpaa2_eth_tx,
1655 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001656 .ndo_set_mac_address = dpaa2_eth_set_addr,
1657 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001658 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
1659 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08001660 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001661 .ndo_change_mtu = dpaa2_eth_change_mtu,
1662 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001663};
1664
1665static void cdan_cb(struct dpaa2_io_notification_ctx *ctx)
1666{
1667 struct dpaa2_eth_channel *ch;
1668
1669 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001670
1671 /* Update NAPI statistics */
1672 ch->stats.cdan++;
1673
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001674 napi_schedule_irqoff(&ch->napi);
1675}
1676
1677/* Allocate and configure a DPCON object */
1678static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv)
1679{
1680 struct fsl_mc_device *dpcon;
1681 struct device *dev = priv->net_dev->dev.parent;
1682 struct dpcon_attr attrs;
1683 int err;
1684
1685 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
1686 FSL_MC_POOL_DPCON, &dpcon);
1687 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001688 if (err == -ENXIO)
1689 err = -EPROBE_DEFER;
1690 else
1691 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
1692 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001693 }
1694
1695 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
1696 if (err) {
1697 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001698 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001699 }
1700
1701 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
1702 if (err) {
1703 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001704 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001705 }
1706
1707 err = dpcon_get_attributes(priv->mc_io, 0, dpcon->mc_handle, &attrs);
1708 if (err) {
1709 dev_err(dev, "dpcon_get_attributes() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001710 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001711 }
1712
1713 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
1714 if (err) {
1715 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001716 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001717 }
1718
1719 return dpcon;
1720
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001721close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001722 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001723free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001724 fsl_mc_object_free(dpcon);
1725
1726 return NULL;
1727}
1728
1729static void free_dpcon(struct dpaa2_eth_priv *priv,
1730 struct fsl_mc_device *dpcon)
1731{
1732 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
1733 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
1734 fsl_mc_object_free(dpcon);
1735}
1736
1737static struct dpaa2_eth_channel *
1738alloc_channel(struct dpaa2_eth_priv *priv)
1739{
1740 struct dpaa2_eth_channel *channel;
1741 struct dpcon_attr attr;
1742 struct device *dev = priv->net_dev->dev.parent;
1743 int err;
1744
1745 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
1746 if (!channel)
1747 return NULL;
1748
1749 channel->dpcon = setup_dpcon(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001750 if (IS_ERR_OR_NULL(channel->dpcon)) {
1751 err = PTR_ERR(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001752 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001753 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001754
1755 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
1756 &attr);
1757 if (err) {
1758 dev_err(dev, "dpcon_get_attributes() failed\n");
1759 goto err_get_attr;
1760 }
1761
1762 channel->dpcon_id = attr.id;
1763 channel->ch_id = attr.qbman_ch_id;
1764 channel->priv = priv;
1765
1766 return channel;
1767
1768err_get_attr:
1769 free_dpcon(priv, channel->dpcon);
1770err_setup:
1771 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001772 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001773}
1774
1775static void free_channel(struct dpaa2_eth_priv *priv,
1776 struct dpaa2_eth_channel *channel)
1777{
1778 free_dpcon(priv, channel->dpcon);
1779 kfree(channel);
1780}
1781
1782/* DPIO setup: allocate and configure QBMan channels, setup core affinity
1783 * and register data availability notifications
1784 */
1785static int setup_dpio(struct dpaa2_eth_priv *priv)
1786{
1787 struct dpaa2_io_notification_ctx *nctx;
1788 struct dpaa2_eth_channel *channel;
1789 struct dpcon_notification_cfg dpcon_notif_cfg;
1790 struct device *dev = priv->net_dev->dev.parent;
1791 int i, err;
1792
1793 /* We want the ability to spread ingress traffic (RX, TX conf) to as
1794 * many cores as possible, so we need one channel for each core
1795 * (unless there's fewer queues than cores, in which case the extra
1796 * channels would be wasted).
1797 * Allocate one channel per core and register it to the core's
1798 * affine DPIO. If not enough channels are available for all cores
1799 * or if some cores don't have an affine DPIO, there will be no
1800 * ingress frame processing on those cores.
1801 */
1802 cpumask_clear(&priv->dpio_cpumask);
1803 for_each_online_cpu(i) {
1804 /* Try to allocate a channel */
1805 channel = alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001806 if (IS_ERR_OR_NULL(channel)) {
1807 err = PTR_ERR(channel);
1808 if (err != -EPROBE_DEFER)
1809 dev_info(dev,
1810 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001811 goto err_alloc_ch;
1812 }
1813
1814 priv->channel[priv->num_channels] = channel;
1815
1816 nctx = &channel->nctx;
1817 nctx->is_cdan = 1;
1818 nctx->cb = cdan_cb;
1819 nctx->id = channel->ch_id;
1820 nctx->desired_cpu = i;
1821
1822 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001823 channel->dpio = dpaa2_io_service_select(i);
1824 err = dpaa2_io_service_register(channel->dpio, nctx);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001825 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05001826 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001827 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05001828 * none available for next cores either. Signal we want
1829 * to retry later, in case the DPIO devices weren't
1830 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001831 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05001832 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001833 goto err_service_reg;
1834 }
1835
1836 /* Register DPCON notification with MC */
1837 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
1838 dpcon_notif_cfg.priority = 0;
1839 dpcon_notif_cfg.user_ctx = nctx->qman64;
1840 err = dpcon_set_notification(priv->mc_io, 0,
1841 channel->dpcon->mc_handle,
1842 &dpcon_notif_cfg);
1843 if (err) {
1844 dev_err(dev, "dpcon_set_notification failed()\n");
1845 goto err_set_cdan;
1846 }
1847
1848 /* If we managed to allocate a channel and also found an affine
1849 * DPIO for this core, add it to the final mask
1850 */
1851 cpumask_set_cpu(i, &priv->dpio_cpumask);
1852 priv->num_channels++;
1853
1854 /* Stop if we already have enough channels to accommodate all
1855 * RX and TX conf queues
1856 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00001857 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001858 break;
1859 }
1860
1861 return 0;
1862
1863err_set_cdan:
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001864 dpaa2_io_service_deregister(channel->dpio, nctx);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001865err_service_reg:
1866 free_channel(priv, channel);
1867err_alloc_ch:
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001868 if (err == -EPROBE_DEFER)
1869 return err;
1870
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001871 if (cpumask_empty(&priv->dpio_cpumask)) {
1872 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001873 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001874 }
1875
1876 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
1877 cpumask_pr_args(&priv->dpio_cpumask));
1878
1879 return 0;
1880}
1881
1882static void free_dpio(struct dpaa2_eth_priv *priv)
1883{
1884 int i;
1885 struct dpaa2_eth_channel *ch;
1886
1887 /* deregister CDAN notifications and free channels */
1888 for (i = 0; i < priv->num_channels; i++) {
1889 ch = priv->channel[i];
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001890 dpaa2_io_service_deregister(ch->dpio, &ch->nctx);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001891 free_channel(priv, ch);
1892 }
1893}
1894
1895static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv,
1896 int cpu)
1897{
1898 struct device *dev = priv->net_dev->dev.parent;
1899 int i;
1900
1901 for (i = 0; i < priv->num_channels; i++)
1902 if (priv->channel[i]->nctx.desired_cpu == cpu)
1903 return priv->channel[i];
1904
1905 /* We should never get here. Issue a warning and return
1906 * the first channel, because it's still better than nothing
1907 */
1908 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
1909
1910 return priv->channel[0];
1911}
1912
1913static void set_fq_affinity(struct dpaa2_eth_priv *priv)
1914{
1915 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu93ddf0b2017-12-21 06:33:21 -06001916 struct cpumask xps_mask;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001917 struct dpaa2_eth_fq *fq;
1918 int rx_cpu, txc_cpu;
Ioana Radulescu93ddf0b2017-12-21 06:33:21 -06001919 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001920
1921 /* For each FQ, pick one channel/CPU to deliver frames to.
1922 * This may well change at runtime, either through irqbalance or
1923 * through direct user intervention.
1924 */
1925 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
1926
1927 for (i = 0; i < priv->num_fqs; i++) {
1928 fq = &priv->fq[i];
1929 switch (fq->type) {
1930 case DPAA2_RX_FQ:
1931 fq->target_cpu = rx_cpu;
1932 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
1933 if (rx_cpu >= nr_cpu_ids)
1934 rx_cpu = cpumask_first(&priv->dpio_cpumask);
1935 break;
1936 case DPAA2_TX_CONF_FQ:
1937 fq->target_cpu = txc_cpu;
Ioana Radulescu93ddf0b2017-12-21 06:33:21 -06001938
1939 /* Tell the stack to affine to txc_cpu the Tx queue
1940 * associated with the confirmation one
1941 */
1942 cpumask_clear(&xps_mask);
1943 cpumask_set_cpu(txc_cpu, &xps_mask);
1944 err = netif_set_xps_queue(priv->net_dev, &xps_mask,
1945 fq->flowid);
1946 if (err)
1947 dev_err(dev, "Error setting XPS queue\n");
1948
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001949 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
1950 if (txc_cpu >= nr_cpu_ids)
1951 txc_cpu = cpumask_first(&priv->dpio_cpumask);
1952 break;
1953 default:
1954 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
1955 }
1956 fq->channel = get_affine_channel(priv, fq->target_cpu);
1957 }
1958}
1959
1960static void setup_fqs(struct dpaa2_eth_priv *priv)
1961{
1962 int i;
1963
1964 /* We have one TxConf FQ per Tx flow.
1965 * The number of Tx and Rx queues is the same.
1966 * Tx queues come first in the fq array.
1967 */
1968 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
1969 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
1970 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
1971 priv->fq[priv->num_fqs++].flowid = (u16)i;
1972 }
1973
1974 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
1975 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
1976 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
1977 priv->fq[priv->num_fqs++].flowid = (u16)i;
1978 }
1979
1980 /* For each FQ, decide on which core to process incoming frames */
1981 set_fq_affinity(priv);
1982}
1983
1984/* Allocate and configure one buffer pool for each interface */
1985static int setup_dpbp(struct dpaa2_eth_priv *priv)
1986{
1987 int err;
1988 struct fsl_mc_device *dpbp_dev;
1989 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001990 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001991
1992 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
1993 &dpbp_dev);
1994 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001995 if (err == -ENXIO)
1996 err = -EPROBE_DEFER;
1997 else
1998 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001999 return err;
2000 }
2001
2002 priv->dpbp_dev = dpbp_dev;
2003
2004 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2005 &dpbp_dev->mc_handle);
2006 if (err) {
2007 dev_err(dev, "dpbp_open() failed\n");
2008 goto err_open;
2009 }
2010
Ioana Radulescud00defe2017-06-06 10:00:32 -05002011 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2012 if (err) {
2013 dev_err(dev, "dpbp_reset() failed\n");
2014 goto err_reset;
2015 }
2016
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002017 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2018 if (err) {
2019 dev_err(dev, "dpbp_enable() failed\n");
2020 goto err_enable;
2021 }
2022
2023 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002024 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002025 if (err) {
2026 dev_err(dev, "dpbp_get_attributes() failed\n");
2027 goto err_get_attr;
2028 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002029 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002030
2031 return 0;
2032
2033err_get_attr:
2034 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2035err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002036err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002037 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2038err_open:
2039 fsl_mc_object_free(dpbp_dev);
2040
2041 return err;
2042}
2043
2044static void free_dpbp(struct dpaa2_eth_priv *priv)
2045{
2046 drain_pool(priv);
2047 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2048 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2049 fsl_mc_object_free(priv->dpbp_dev);
2050}
2051
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002052static int set_buffer_layout(struct dpaa2_eth_priv *priv)
2053{
2054 struct device *dev = priv->net_dev->dev.parent;
2055 struct dpni_buffer_layout buf_layout = {0};
2056 int err;
2057
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002058 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2059 * version, this number is not always provided correctly on rev1.
2060 * We need to check for both alternatives in this situation.
2061 */
2062 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2063 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
2064 priv->rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
2065 else
2066 priv->rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
2067
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002068 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002069 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002070 buf_layout.pass_timestamp = true;
2071 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2072 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002073 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2074 DPNI_QUEUE_TX, &buf_layout);
2075 if (err) {
2076 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2077 return err;
2078 }
2079
2080 /* tx-confirm buffer */
Ioana Radulescu859f9982018-04-26 18:23:47 +08002081 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002082 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2083 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2084 if (err) {
2085 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2086 return err;
2087 }
2088
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002089 /* Now that we've set our tx buffer layout, retrieve the minimum
2090 * required tx data offset.
2091 */
2092 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2093 &priv->tx_data_offset);
2094 if (err) {
2095 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2096 return err;
2097 }
2098
2099 if ((priv->tx_data_offset % 64) != 0)
2100 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2101 priv->tx_data_offset);
2102
2103 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06002104 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002105 buf_layout.pass_parser_result = true;
2106 buf_layout.data_align = priv->rx_buf_align;
2107 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2108 buf_layout.private_data_size = 0;
2109 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2110 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2111 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08002112 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2113 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002114 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2115 DPNI_QUEUE_RX, &buf_layout);
2116 if (err) {
2117 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2118 return err;
2119 }
2120
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002121 return 0;
2122}
2123
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002124/* Configure the DPNI object this interface is associated with */
2125static int setup_dpni(struct fsl_mc_device *ls_dev)
2126{
2127 struct device *dev = &ls_dev->dev;
2128 struct dpaa2_eth_priv *priv;
2129 struct net_device *net_dev;
2130 int err;
2131
2132 net_dev = dev_get_drvdata(dev);
2133 priv = netdev_priv(net_dev);
2134
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002135 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05002136 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002137 if (err) {
2138 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002139 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002140 }
2141
Ioana Radulescu311cffa2018-03-23 08:44:09 -05002142 /* Check if we can work with this DPNI object */
2143 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
2144 &priv->dpni_ver_minor);
2145 if (err) {
2146 dev_err(dev, "dpni_get_api_version() failed\n");
2147 goto close;
2148 }
2149 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
2150 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
2151 priv->dpni_ver_major, priv->dpni_ver_minor,
2152 DPNI_VER_MAJOR, DPNI_VER_MINOR);
2153 err = -ENOTSUPP;
2154 goto close;
2155 }
2156
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002157 ls_dev->mc_io = priv->mc_io;
2158 ls_dev->mc_handle = priv->mc_token;
2159
2160 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2161 if (err) {
2162 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002163 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002164 }
2165
2166 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
2167 &priv->dpni_attrs);
2168 if (err) {
2169 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002170 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002171 }
2172
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002173 err = set_buffer_layout(priv);
2174 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002175 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002176
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002177 priv->cls_rules = devm_kzalloc(dev, sizeof(struct dpaa2_eth_cls_rule) *
2178 dpaa2_eth_fs_count(priv), GFP_KERNEL);
2179 if (!priv->cls_rules)
2180 goto close;
2181
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002182 return 0;
2183
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002184close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002185 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002186
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002187 return err;
2188}
2189
2190static void free_dpni(struct dpaa2_eth_priv *priv)
2191{
2192 int err;
2193
2194 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2195 if (err)
2196 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
2197 err);
2198
2199 dpni_close(priv->mc_io, 0, priv->mc_token);
2200}
2201
2202static int setup_rx_flow(struct dpaa2_eth_priv *priv,
2203 struct dpaa2_eth_fq *fq)
2204{
2205 struct device *dev = priv->net_dev->dev.parent;
2206 struct dpni_queue queue;
2207 struct dpni_queue_id qid;
2208 struct dpni_taildrop td;
2209 int err;
2210
2211 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2212 DPNI_QUEUE_RX, 0, fq->flowid, &queue, &qid);
2213 if (err) {
2214 dev_err(dev, "dpni_get_queue(RX) failed\n");
2215 return err;
2216 }
2217
2218 fq->fqid = qid.fqid;
2219
2220 queue.destination.id = fq->channel->dpcon_id;
2221 queue.destination.type = DPNI_DEST_DPCON;
2222 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002223 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002224 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2225 DPNI_QUEUE_RX, 0, fq->flowid,
2226 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2227 &queue);
2228 if (err) {
2229 dev_err(dev, "dpni_set_queue(RX) failed\n");
2230 return err;
2231 }
2232
2233 td.enable = 1;
2234 td.threshold = DPAA2_ETH_TAILDROP_THRESH;
2235 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, DPNI_CP_QUEUE,
2236 DPNI_QUEUE_RX, 0, fq->flowid, &td);
2237 if (err) {
2238 dev_err(dev, "dpni_set_threshold() failed\n");
2239 return err;
2240 }
2241
2242 return 0;
2243}
2244
2245static int setup_tx_flow(struct dpaa2_eth_priv *priv,
2246 struct dpaa2_eth_fq *fq)
2247{
2248 struct device *dev = priv->net_dev->dev.parent;
2249 struct dpni_queue queue;
2250 struct dpni_queue_id qid;
2251 int err;
2252
2253 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2254 DPNI_QUEUE_TX, 0, fq->flowid, &queue, &qid);
2255 if (err) {
2256 dev_err(dev, "dpni_get_queue(TX) failed\n");
2257 return err;
2258 }
2259
2260 fq->tx_qdbin = qid.qdbin;
2261
2262 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2263 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2264 &queue, &qid);
2265 if (err) {
2266 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
2267 return err;
2268 }
2269
2270 fq->fqid = qid.fqid;
2271
2272 queue.destination.id = fq->channel->dpcon_id;
2273 queue.destination.type = DPNI_DEST_DPCON;
2274 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002275 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002276 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2277 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2278 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2279 &queue);
2280 if (err) {
2281 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
2282 return err;
2283 }
2284
2285 return 0;
2286}
2287
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002288/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002289static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002290 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002291 /* L2 header */
2292 .rxnfc_field = RXH_L2DA,
2293 .cls_prot = NET_PROT_ETH,
2294 .cls_field = NH_FLD_ETH_DA,
2295 .size = 6,
2296 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002297 .cls_prot = NET_PROT_ETH,
2298 .cls_field = NH_FLD_ETH_SA,
2299 .size = 6,
2300 }, {
2301 /* This is the last ethertype field parsed:
2302 * depending on frame format, it can be the MAC ethertype
2303 * or the VLAN etype.
2304 */
2305 .cls_prot = NET_PROT_ETH,
2306 .cls_field = NH_FLD_ETH_TYPE,
2307 .size = 2,
2308 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002309 /* VLAN header */
2310 .rxnfc_field = RXH_VLAN,
2311 .cls_prot = NET_PROT_VLAN,
2312 .cls_field = NH_FLD_VLAN_TCI,
2313 .size = 2,
2314 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002315 /* IP header */
2316 .rxnfc_field = RXH_IP_SRC,
2317 .cls_prot = NET_PROT_IP,
2318 .cls_field = NH_FLD_IP_SRC,
2319 .size = 4,
2320 }, {
2321 .rxnfc_field = RXH_IP_DST,
2322 .cls_prot = NET_PROT_IP,
2323 .cls_field = NH_FLD_IP_DST,
2324 .size = 4,
2325 }, {
2326 .rxnfc_field = RXH_L3_PROTO,
2327 .cls_prot = NET_PROT_IP,
2328 .cls_field = NH_FLD_IP_PROTO,
2329 .size = 1,
2330 }, {
2331 /* Using UDP ports, this is functionally equivalent to raw
2332 * byte pairs from L4 header.
2333 */
2334 .rxnfc_field = RXH_L4_B_0_1,
2335 .cls_prot = NET_PROT_UDP,
2336 .cls_field = NH_FLD_UDP_PORT_SRC,
2337 .size = 2,
2338 }, {
2339 .rxnfc_field = RXH_L4_B_2_3,
2340 .cls_prot = NET_PROT_UDP,
2341 .cls_field = NH_FLD_UDP_PORT_DST,
2342 .size = 2,
2343 },
2344};
2345
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002346/* Configure the Rx hash key using the legacy API */
2347static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2348{
2349 struct device *dev = priv->net_dev->dev.parent;
2350 struct dpni_rx_tc_dist_cfg dist_cfg;
2351 int err;
2352
2353 memset(&dist_cfg, 0, sizeof(dist_cfg));
2354
2355 dist_cfg.key_cfg_iova = key;
2356 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2357 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
2358
2359 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg);
2360 if (err)
2361 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
2362
2363 return err;
2364}
2365
2366/* Configure the Rx hash key using the new API */
2367static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2368{
2369 struct device *dev = priv->net_dev->dev.parent;
2370 struct dpni_rx_dist_cfg dist_cfg;
2371 int err;
2372
2373 memset(&dist_cfg, 0, sizeof(dist_cfg));
2374
2375 dist_cfg.key_cfg_iova = key;
2376 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2377 dist_cfg.enable = 1;
2378
2379 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2380 if (err)
2381 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
2382
2383 return err;
2384}
2385
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002386/* Configure the Rx flow classification key */
2387static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2388{
2389 struct device *dev = priv->net_dev->dev.parent;
2390 struct dpni_rx_dist_cfg dist_cfg;
2391 int err;
2392
2393 memset(&dist_cfg, 0, sizeof(dist_cfg));
2394
2395 dist_cfg.key_cfg_iova = key;
2396 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2397 dist_cfg.enable = 1;
2398
2399 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2400 if (err)
2401 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
2402
2403 return err;
2404}
2405
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002406/* Size of the Rx flow classification key */
2407int dpaa2_eth_cls_key_size(void)
2408{
2409 int i, size = 0;
2410
2411 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
2412 size += dist_fields[i].size;
2413
2414 return size;
2415}
2416
2417/* Offset of header field in Rx classification key */
2418int dpaa2_eth_cls_fld_off(int prot, int field)
2419{
2420 int i, off = 0;
2421
2422 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2423 if (dist_fields[i].cls_prot == prot &&
2424 dist_fields[i].cls_field == field)
2425 return off;
2426 off += dist_fields[i].size;
2427 }
2428
2429 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
2430 return 0;
2431}
2432
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002433/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002434 * flags is a combination of RXH_ bits
2435 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00002436static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
2437 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002438{
2439 struct device *dev = net_dev->dev.parent;
2440 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2441 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002442 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002443 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002444 u8 *dma_mem;
2445 int i;
2446 int err = 0;
2447
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002448 memset(&cls_cfg, 0, sizeof(cls_cfg));
2449
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002450 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002451 struct dpkg_extract *key =
2452 &cls_cfg.extracts[cls_cfg.num_extracts];
2453
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002454 /* For Rx hashing key we set only the selected fields.
2455 * For Rx flow classification key we set all supported fields
2456 */
2457 if (type == DPAA2_ETH_RX_DIST_HASH) {
2458 if (!(flags & dist_fields[i].rxnfc_field))
2459 continue;
2460 rx_hash_fields |= dist_fields[i].rxnfc_field;
2461 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002462
2463 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
2464 dev_err(dev, "error adding key extraction rule, too many rules?\n");
2465 return -E2BIG;
2466 }
2467
2468 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002469 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002470 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002471 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002472 cls_cfg.num_extracts++;
2473 }
2474
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05002475 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002476 if (!dma_mem)
2477 return -ENOMEM;
2478
2479 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
2480 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002481 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002482 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002483 }
2484
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002485 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002486 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
2487 DMA_TO_DEVICE);
2488 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002489 dev_err(dev, "DMA mapping failed\n");
2490 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002491 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002492 }
2493
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002494 if (type == DPAA2_ETH_RX_DIST_HASH) {
2495 if (dpaa2_eth_has_legacy_dist(priv))
2496 err = config_legacy_hash_key(priv, key_iova);
2497 else
2498 err = config_hash_key(priv, key_iova);
2499 } else {
2500 err = config_cls_key(priv, key_iova);
2501 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002502
2503 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
2504 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002505 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002506 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002507
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002508free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002509 kfree(dma_mem);
2510 return err;
2511}
2512
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002513int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
2514{
2515 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2516
2517 if (!dpaa2_eth_hash_enabled(priv))
2518 return -EOPNOTSUPP;
2519
2520 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, flags);
2521}
2522
2523static int dpaa2_eth_set_cls(struct dpaa2_eth_priv *priv)
2524{
2525 struct device *dev = priv->net_dev->dev.parent;
2526
2527 /* Check if we actually support Rx flow classification */
2528 if (dpaa2_eth_has_legacy_dist(priv)) {
2529 dev_dbg(dev, "Rx cls not supported by current MC version\n");
2530 return -EOPNOTSUPP;
2531 }
2532
2533 if (priv->dpni_attrs.options & DPNI_OPT_NO_FS ||
2534 !(priv->dpni_attrs.options & DPNI_OPT_HAS_KEY_MASKING)) {
2535 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
2536 return -EOPNOTSUPP;
2537 }
2538
2539 if (!dpaa2_eth_hash_enabled(priv)) {
2540 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
2541 return -EOPNOTSUPP;
2542 }
2543
2544 priv->rx_cls_enabled = 1;
2545
2546 return dpaa2_eth_set_dist_key(priv->net_dev, DPAA2_ETH_RX_DIST_CLS, 0);
2547}
2548
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002549/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
2550 * frame queues and channels
2551 */
2552static int bind_dpni(struct dpaa2_eth_priv *priv)
2553{
2554 struct net_device *net_dev = priv->net_dev;
2555 struct device *dev = net_dev->dev.parent;
2556 struct dpni_pools_cfg pools_params;
2557 struct dpni_error_cfg err_cfg;
2558 int err = 0;
2559 int i;
2560
2561 pools_params.num_dpbp = 1;
2562 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
2563 pools_params.pools[0].backup_pool = 0;
2564 pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE;
2565 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
2566 if (err) {
2567 dev_err(dev, "dpni_set_pools() failed\n");
2568 return err;
2569 }
2570
Ioana Radulescu227686b2018-07-27 09:12:59 -05002571 /* have the interface implicitly distribute traffic based on
2572 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002573 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05002574 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002575 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05002576 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002577
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002578 /* Configure the flow classification key; it includes all
2579 * supported header fields and cannot be modified at runtime
2580 */
2581 err = dpaa2_eth_set_cls(priv);
2582 if (err && err != -EOPNOTSUPP)
2583 dev_err(dev, "Failed to configure Rx classification key\n");
2584
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002585 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05002586 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002587 err_cfg.set_frame_annotation = 1;
2588 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
2589 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
2590 &err_cfg);
2591 if (err) {
2592 dev_err(dev, "dpni_set_errors_behavior failed\n");
2593 return err;
2594 }
2595
2596 /* Configure Rx and Tx conf queues to generate CDANs */
2597 for (i = 0; i < priv->num_fqs; i++) {
2598 switch (priv->fq[i].type) {
2599 case DPAA2_RX_FQ:
2600 err = setup_rx_flow(priv, &priv->fq[i]);
2601 break;
2602 case DPAA2_TX_CONF_FQ:
2603 err = setup_tx_flow(priv, &priv->fq[i]);
2604 break;
2605 default:
2606 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
2607 return -EINVAL;
2608 }
2609 if (err)
2610 return err;
2611 }
2612
2613 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
2614 DPNI_QUEUE_TX, &priv->tx_qdid);
2615 if (err) {
2616 dev_err(dev, "dpni_get_qdid() failed\n");
2617 return err;
2618 }
2619
2620 return 0;
2621}
2622
2623/* Allocate rings for storing incoming frame descriptors */
2624static int alloc_rings(struct dpaa2_eth_priv *priv)
2625{
2626 struct net_device *net_dev = priv->net_dev;
2627 struct device *dev = net_dev->dev.parent;
2628 int i;
2629
2630 for (i = 0; i < priv->num_channels; i++) {
2631 priv->channel[i]->store =
2632 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
2633 if (!priv->channel[i]->store) {
2634 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
2635 goto err_ring;
2636 }
2637 }
2638
2639 return 0;
2640
2641err_ring:
2642 for (i = 0; i < priv->num_channels; i++) {
2643 if (!priv->channel[i]->store)
2644 break;
2645 dpaa2_io_store_destroy(priv->channel[i]->store);
2646 }
2647
2648 return -ENOMEM;
2649}
2650
2651static void free_rings(struct dpaa2_eth_priv *priv)
2652{
2653 int i;
2654
2655 for (i = 0; i < priv->num_channels; i++)
2656 dpaa2_io_store_destroy(priv->channel[i]->store);
2657}
2658
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002659static int set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002660{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002661 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002662 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002663 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002664 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002665
2666 /* Get firmware address, if any */
2667 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
2668 if (err) {
2669 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
2670 return err;
2671 }
2672
2673 /* Get DPNI attributes address, if any */
2674 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2675 dpni_mac_addr);
2676 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002677 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002678 return err;
2679 }
2680
2681 /* First check if firmware has any address configured by bootloader */
2682 if (!is_zero_ether_addr(mac_addr)) {
2683 /* If the DPMAC addr != DPNI addr, update it */
2684 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
2685 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
2686 priv->mc_token,
2687 mac_addr);
2688 if (err) {
2689 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
2690 return err;
2691 }
2692 }
2693 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
2694 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002695 /* No MAC address configured, fill in net_dev->dev_addr
2696 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002697 */
2698 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002699 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
2700
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002701 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2702 net_dev->dev_addr);
2703 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002704 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002705 return err;
2706 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002707
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002708 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
2709 * practical purposes, this will be our "permanent" mac address,
2710 * at least until the next reboot. This move will also permit
2711 * register_netdevice() to properly fill up net_dev->perm_addr.
2712 */
2713 net_dev->addr_assign_type = NET_ADDR_PERM;
2714 } else {
2715 /* NET_ADDR_PERM is default, all we have to do is
2716 * fill in the device addr.
2717 */
2718 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
2719 }
2720
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002721 return 0;
2722}
2723
2724static int netdev_init(struct net_device *net_dev)
2725{
2726 struct device *dev = net_dev->dev.parent;
2727 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002728 u32 options = priv->dpni_attrs.options;
2729 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002730 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05002731 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002732 int err;
2733
2734 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002735 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002736
2737 err = set_mac_addr(priv);
2738 if (err)
2739 return err;
2740
2741 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002742 eth_broadcast_addr(bcast_addr);
2743 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
2744 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002745 dev_err(dev, "dpni_add_mac_addr() failed\n");
2746 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002747 }
2748
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05002749 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002750 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05002751 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05002752 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05002753 if (err) {
2754 dev_err(dev, "dpni_set_max_frame_length() failed\n");
2755 return err;
2756 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002757
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05002758 /* Set actual number of queues in the net device */
2759 num_queues = dpaa2_eth_queue_count(priv);
2760 err = netif_set_real_num_tx_queues(net_dev, num_queues);
2761 if (err) {
2762 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
2763 return err;
2764 }
2765 err = netif_set_real_num_rx_queues(net_dev, num_queues);
2766 if (err) {
2767 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
2768 return err;
2769 }
2770
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002771 /* Capabilities listing */
2772 supported |= IFF_LIVE_ADDR_CHANGE;
2773
2774 if (options & DPNI_OPT_NO_MAC_FILTER)
2775 not_supported |= IFF_UNICAST_FLT;
2776 else
2777 supported |= IFF_UNICAST_FLT;
2778
2779 net_dev->priv_flags |= supported;
2780 net_dev->priv_flags &= ~not_supported;
2781
2782 /* Features */
2783 net_dev->features = NETIF_F_RXCSUM |
2784 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2785 NETIF_F_SG | NETIF_F_HIGHDMA |
2786 NETIF_F_LLTX;
2787 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002788
2789 return 0;
2790}
2791
2792static int poll_link_state(void *arg)
2793{
2794 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
2795 int err;
2796
2797 while (!kthread_should_stop()) {
2798 err = link_state_update(priv);
2799 if (unlikely(err))
2800 return err;
2801
2802 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
2803 }
2804
2805 return 0;
2806}
2807
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002808static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
2809{
Ioana Radulescu112197d2017-10-11 08:29:49 -05002810 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002811 struct device *dev = (struct device *)arg;
2812 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
2813 struct net_device *net_dev = dev_get_drvdata(dev);
2814 int err;
2815
2816 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
2817 DPNI_IRQ_INDEX, &status);
2818 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002819 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05002820 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002821 }
2822
Ioana Radulescu112197d2017-10-11 08:29:49 -05002823 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002824 link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002825
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002826 return IRQ_HANDLED;
2827}
2828
2829static int setup_irqs(struct fsl_mc_device *ls_dev)
2830{
2831 int err = 0;
2832 struct fsl_mc_device_irq *irq;
2833
2834 err = fsl_mc_allocate_irqs(ls_dev);
2835 if (err) {
2836 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
2837 return err;
2838 }
2839
2840 irq = ls_dev->irqs[0];
2841 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05002842 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002843 IRQF_NO_SUSPEND | IRQF_ONESHOT,
2844 dev_name(&ls_dev->dev), &ls_dev->dev);
2845 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002846 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002847 goto free_mc_irq;
2848 }
2849
2850 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
2851 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED);
2852 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002853 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002854 goto free_irq;
2855 }
2856
2857 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
2858 DPNI_IRQ_INDEX, 1);
2859 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002860 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002861 goto free_irq;
2862 }
2863
2864 return 0;
2865
2866free_irq:
2867 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
2868free_mc_irq:
2869 fsl_mc_free_irqs(ls_dev);
2870
2871 return err;
2872}
2873
2874static void add_ch_napi(struct dpaa2_eth_priv *priv)
2875{
2876 int i;
2877 struct dpaa2_eth_channel *ch;
2878
2879 for (i = 0; i < priv->num_channels; i++) {
2880 ch = priv->channel[i];
2881 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
2882 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
2883 NAPI_POLL_WEIGHT);
2884 }
2885}
2886
2887static void del_ch_napi(struct dpaa2_eth_priv *priv)
2888{
2889 int i;
2890 struct dpaa2_eth_channel *ch;
2891
2892 for (i = 0; i < priv->num_channels; i++) {
2893 ch = priv->channel[i];
2894 netif_napi_del(&ch->napi);
2895 }
2896}
2897
2898static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
2899{
2900 struct device *dev;
2901 struct net_device *net_dev = NULL;
2902 struct dpaa2_eth_priv *priv = NULL;
2903 int err = 0;
2904
2905 dev = &dpni_dev->dev;
2906
2907 /* Net device */
2908 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_TX_QUEUES);
2909 if (!net_dev) {
2910 dev_err(dev, "alloc_etherdev_mq() failed\n");
2911 return -ENOMEM;
2912 }
2913
2914 SET_NETDEV_DEV(net_dev, dev);
2915 dev_set_drvdata(dev, net_dev);
2916
2917 priv = netdev_priv(net_dev);
2918 priv->net_dev = net_dev;
2919
Ioana Radulescu08eb2392017-05-24 07:13:27 -05002920 priv->iommu_domain = iommu_get_domain_for_dev(dev);
2921
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002922 /* Obtain a MC portal */
2923 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
2924 &priv->mc_io);
2925 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05002926 if (err == -ENXIO)
2927 err = -EPROBE_DEFER;
2928 else
2929 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002930 goto err_portal_alloc;
2931 }
2932
2933 /* MC objects initialization and configuration */
2934 err = setup_dpni(dpni_dev);
2935 if (err)
2936 goto err_dpni_setup;
2937
2938 err = setup_dpio(priv);
2939 if (err)
2940 goto err_dpio_setup;
2941
2942 setup_fqs(priv);
2943
2944 err = setup_dpbp(priv);
2945 if (err)
2946 goto err_dpbp_setup;
2947
2948 err = bind_dpni(priv);
2949 if (err)
2950 goto err_bind;
2951
2952 /* Add a NAPI context for each channel */
2953 add_ch_napi(priv);
2954
2955 /* Percpu statistics */
2956 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
2957 if (!priv->percpu_stats) {
2958 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
2959 err = -ENOMEM;
2960 goto err_alloc_percpu_stats;
2961 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002962 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
2963 if (!priv->percpu_extras) {
2964 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
2965 err = -ENOMEM;
2966 goto err_alloc_percpu_extras;
2967 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002968
2969 err = netdev_init(net_dev);
2970 if (err)
2971 goto err_netdev_init;
2972
2973 /* Configure checksum offload based on current interface flags */
2974 err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
2975 if (err)
2976 goto err_csum;
2977
2978 err = set_tx_csum(priv, !!(net_dev->features &
2979 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
2980 if (err)
2981 goto err_csum;
2982
2983 err = alloc_rings(priv);
2984 if (err)
2985 goto err_alloc_rings;
2986
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002987 err = setup_irqs(dpni_dev);
2988 if (err) {
2989 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
2990 priv->poll_thread = kthread_run(poll_link_state, priv,
2991 "%s_poll_link", net_dev->name);
2992 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002993 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002994 goto err_poll_thread;
2995 }
2996 priv->do_link_poll = true;
2997 }
2998
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05002999 err = register_netdev(net_dev);
3000 if (err < 0) {
3001 dev_err(dev, "register_netdev() failed\n");
3002 goto err_netdev_reg;
3003 }
3004
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003005 dev_info(dev, "Probed interface %s\n", net_dev->name);
3006 return 0;
3007
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003008err_netdev_reg:
3009 if (priv->do_link_poll)
3010 kthread_stop(priv->poll_thread);
3011 else
3012 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003013err_poll_thread:
3014 free_rings(priv);
3015err_alloc_rings:
3016err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003017err_netdev_init:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003018 free_percpu(priv->percpu_extras);
3019err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003020 free_percpu(priv->percpu_stats);
3021err_alloc_percpu_stats:
3022 del_ch_napi(priv);
3023err_bind:
3024 free_dpbp(priv);
3025err_dpbp_setup:
3026 free_dpio(priv);
3027err_dpio_setup:
3028 free_dpni(priv);
3029err_dpni_setup:
3030 fsl_mc_portal_free(priv->mc_io);
3031err_portal_alloc:
3032 dev_set_drvdata(dev, NULL);
3033 free_netdev(net_dev);
3034
3035 return err;
3036}
3037
3038static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
3039{
3040 struct device *dev;
3041 struct net_device *net_dev;
3042 struct dpaa2_eth_priv *priv;
3043
3044 dev = &ls_dev->dev;
3045 net_dev = dev_get_drvdata(dev);
3046 priv = netdev_priv(net_dev);
3047
3048 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003049
3050 if (priv->do_link_poll)
3051 kthread_stop(priv->poll_thread);
3052 else
3053 fsl_mc_free_irqs(ls_dev);
3054
3055 free_rings(priv);
3056 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003057 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003058
3059 del_ch_napi(priv);
3060 free_dpbp(priv);
3061 free_dpio(priv);
3062 free_dpni(priv);
3063
3064 fsl_mc_portal_free(priv->mc_io);
3065
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003066 free_netdev(net_dev);
3067
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05003068 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05003069
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003070 return 0;
3071}
3072
3073static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
3074 {
3075 .vendor = FSL_MC_VENDOR_FREESCALE,
3076 .obj_type = "dpni",
3077 },
3078 { .vendor = 0x0 }
3079};
3080MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
3081
3082static struct fsl_mc_driver dpaa2_eth_driver = {
3083 .driver = {
3084 .name = KBUILD_MODNAME,
3085 .owner = THIS_MODULE,
3086 },
3087 .probe = dpaa2_eth_probe,
3088 .remove = dpaa2_eth_remove,
3089 .match_id_table = dpaa2_eth_match_id_table
3090};
3091
3092module_fsl_mc_driver(dpaa2_eth_driver);