blob: a0988e0a18cec5eab0e2949b0fcd5fbef3065a5d [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 Ciocoi Radulescu27c87482019-02-04 17:00:35 +000089 dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
90 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050091
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000092 free_pages((unsigned long)sg_vaddr, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050093 if (dpaa2_sg_is_final(&sgt[i]))
94 break;
95 }
96
97free_buf:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000098 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050099}
100
101/* Build a linear skb based on a single-buffer frame descriptor */
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +0000102static struct sk_buff *build_linear_skb(struct dpaa2_eth_channel *ch,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500103 const struct dpaa2_fd *fd,
104 void *fd_vaddr)
105{
106 struct sk_buff *skb = NULL;
107 u16 fd_offset = dpaa2_fd_get_offset(fd);
108 u32 fd_length = dpaa2_fd_get_len(fd);
109
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500110 ch->buf_count--;
111
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000112 skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500113 if (unlikely(!skb))
114 return NULL;
115
116 skb_reserve(skb, fd_offset);
117 skb_put(skb, fd_length);
118
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500119 return skb;
120}
121
122/* Build a non linear (fragmented) skb based on a S/G table */
123static struct sk_buff *build_frag_skb(struct dpaa2_eth_priv *priv,
124 struct dpaa2_eth_channel *ch,
125 struct dpaa2_sg_entry *sgt)
126{
127 struct sk_buff *skb = NULL;
128 struct device *dev = priv->net_dev->dev.parent;
129 void *sg_vaddr;
130 dma_addr_t sg_addr;
131 u16 sg_offset;
132 u32 sg_length;
133 struct page *page, *head_page;
134 int page_offset;
135 int i;
136
137 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
138 struct dpaa2_sg_entry *sge = &sgt[i];
139
140 /* NOTE: We only support SG entries in dpaa2_sg_single format,
141 * but this is the only format we may receive from HW anyway
142 */
143
144 /* Get the address and length from the S/G entry */
145 sg_addr = dpaa2_sg_get_addr(sge);
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500146 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000147 dma_unmap_page(dev, sg_addr, DPAA2_ETH_RX_BUF_SIZE,
148 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500149
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500150 sg_length = dpaa2_sg_get_len(sge);
151
152 if (i == 0) {
153 /* We build the skb around the first data buffer */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000154 skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500155 if (unlikely(!skb)) {
Ioana Radulescu729d79b2017-10-11 08:29:48 -0500156 /* Free the first SG entry now, since we already
157 * unmapped it and obtained the virtual address
158 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000159 free_pages((unsigned long)sg_vaddr, 0);
Ioana Radulescu729d79b2017-10-11 08:29:48 -0500160
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500161 /* We still need to subtract the buffers used
162 * by this FD from our software counter
163 */
164 while (!dpaa2_sg_is_final(&sgt[i]) &&
165 i < DPAA2_ETH_MAX_SG_ENTRIES)
166 i++;
167 break;
168 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500169
170 sg_offset = dpaa2_sg_get_offset(sge);
171 skb_reserve(skb, sg_offset);
172 skb_put(skb, sg_length);
173 } else {
174 /* Rest of the data buffers are stored as skb frags */
175 page = virt_to_page(sg_vaddr);
176 head_page = virt_to_head_page(sg_vaddr);
177
178 /* Offset in page (which may be compound).
179 * Data in subsequent SG entries is stored from the
180 * beginning of the buffer, so we don't need to add the
181 * sg_offset.
182 */
183 page_offset = ((unsigned long)sg_vaddr &
184 (PAGE_SIZE - 1)) +
185 (page_address(page) - page_address(head_page));
186
187 skb_add_rx_frag(skb, i - 1, head_page, page_offset,
188 sg_length, DPAA2_ETH_RX_BUF_SIZE);
189 }
190
191 if (dpaa2_sg_is_final(sge))
192 break;
193 }
194
Ioana Radulescub63baf72017-10-11 08:29:45 -0500195 WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
196
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500197 /* Count all data buffers + SG table buffer */
198 ch->buf_count -= i + 2;
199
200 return skb;
201}
202
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000203/* Free buffers acquired from the buffer pool or which were meant to
204 * be released in the pool
205 */
206static void free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array, int count)
207{
208 struct device *dev = priv->net_dev->dev.parent;
209 void *vaddr;
210 int i;
211
212 for (i = 0; i < count; i++) {
213 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000214 dma_unmap_page(dev, buf_array[i], DPAA2_ETH_RX_BUF_SIZE,
215 DMA_BIDIRECTIONAL);
216 free_pages((unsigned long)vaddr, 0);
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000217 }
218}
219
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000220static void xdp_release_buf(struct dpaa2_eth_priv *priv,
221 struct dpaa2_eth_channel *ch,
222 dma_addr_t addr)
223{
224 int err;
225
226 ch->xdp.drop_bufs[ch->xdp.drop_cnt++] = addr;
227 if (ch->xdp.drop_cnt < DPAA2_ETH_BUFS_PER_CMD)
228 return;
229
230 while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
231 ch->xdp.drop_bufs,
232 ch->xdp.drop_cnt)) == -EBUSY)
233 cpu_relax();
234
235 if (err) {
236 free_bufs(priv, ch->xdp.drop_bufs, ch->xdp.drop_cnt);
237 ch->buf_count -= ch->xdp.drop_cnt;
238 }
239
240 ch->xdp.drop_cnt = 0;
241}
242
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000243static int xdp_enqueue(struct dpaa2_eth_priv *priv, struct dpaa2_fd *fd,
244 void *buf_start, u16 queue_id)
245{
246 struct dpaa2_eth_fq *fq;
247 struct dpaa2_faead *faead;
248 u32 ctrl, frc;
249 int i, err;
250
251 /* Mark the egress frame hardware annotation area as valid */
252 frc = dpaa2_fd_get_frc(fd);
253 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
254 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
255
256 /* Instruct hardware to release the FD buffer directly into
257 * the buffer pool once transmission is completed, instead of
258 * sending a Tx confirmation frame to us
259 */
260 ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
261 faead = dpaa2_get_faead(buf_start, false);
262 faead->ctrl = cpu_to_le32(ctrl);
263 faead->conf_fqid = 0;
264
265 fq = &priv->fq[queue_id];
266 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +0000267 err = priv->enqueue(priv, fq, fd, 0);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000268 if (err != -EBUSY)
269 break;
270 }
271
272 return err;
273}
274
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000275static u32 run_xdp(struct dpaa2_eth_priv *priv,
276 struct dpaa2_eth_channel *ch,
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000277 struct dpaa2_eth_fq *rx_fq,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000278 struct dpaa2_fd *fd, void *vaddr)
279{
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000280 dma_addr_t addr = dpaa2_fd_get_addr(fd);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000281 struct rtnl_link_stats64 *percpu_stats;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000282 struct bpf_prog *xdp_prog;
283 struct xdp_buff xdp;
284 u32 xdp_act = XDP_PASS;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000285 int err;
286
287 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000288
289 rcu_read_lock();
290
291 xdp_prog = READ_ONCE(ch->xdp.prog);
292 if (!xdp_prog)
293 goto out;
294
295 xdp.data = vaddr + dpaa2_fd_get_offset(fd);
296 xdp.data_end = xdp.data + dpaa2_fd_get_len(fd);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000297 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000298 xdp_set_data_meta_invalid(&xdp);
Ioana Radulescud678be12019-03-01 17:47:24 +0000299 xdp.rxq = &ch->xdp_rxq;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000300
301 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
302
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000303 /* xdp.data pointer may have changed */
304 dpaa2_fd_set_offset(fd, xdp.data - vaddr);
305 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
306
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000307 switch (xdp_act) {
308 case XDP_PASS:
309 break;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000310 case XDP_TX:
311 err = xdp_enqueue(priv, fd, vaddr, rx_fq->flowid);
312 if (err) {
313 xdp_release_buf(priv, ch, addr);
314 percpu_stats->tx_errors++;
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000315 ch->stats.xdp_tx_err++;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000316 } else {
317 percpu_stats->tx_packets++;
318 percpu_stats->tx_bytes += dpaa2_fd_get_len(fd);
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000319 ch->stats.xdp_tx++;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000320 }
321 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000322 default:
323 bpf_warn_invalid_xdp_action(xdp_act);
Ioana Ciocoi Radulescuc1cb11b2018-11-29 08:43:40 +0000324 /* fall through */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000325 case XDP_ABORTED:
326 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
Ioana Ciocoi Radulescuc1cb11b2018-11-29 08:43:40 +0000327 /* fall through */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000328 case XDP_DROP:
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000329 xdp_release_buf(priv, ch, addr);
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000330 ch->stats.xdp_drop++;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000331 break;
Ioana Radulescud678be12019-03-01 17:47:24 +0000332 case XDP_REDIRECT:
333 dma_unmap_page(priv->net_dev->dev.parent, addr,
334 DPAA2_ETH_RX_BUF_SIZE, DMA_BIDIRECTIONAL);
335 ch->buf_count--;
336 xdp.data_hard_start = vaddr;
337 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
338 if (unlikely(err))
339 ch->stats.xdp_drop++;
340 else
341 ch->stats.xdp_redirect++;
342 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000343 }
344
Ioana Radulescud678be12019-03-01 17:47:24 +0000345 ch->xdp.res |= xdp_act;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000346out:
347 rcu_read_unlock();
348 return xdp_act;
349}
350
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500351/* Main Rx frame processing routine */
352static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
353 struct dpaa2_eth_channel *ch,
354 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000355 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500356{
357 dma_addr_t addr = dpaa2_fd_get_addr(fd);
358 u8 fd_format = dpaa2_fd_get_format(fd);
359 void *vaddr;
360 struct sk_buff *skb;
361 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500362 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500363 struct device *dev = priv->net_dev->dev.parent;
364 struct dpaa2_fas *fas;
Ioana Radulescud695e762017-06-06 10:00:35 -0500365 void *buf_data;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500366 u32 status = 0;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000367 u32 xdp_act;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500368
Ioana Radulescu56361872017-04-28 04:50:32 -0500369 /* Tracing point */
370 trace_dpaa2_rx_fd(priv->net_dev, fd);
371
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500372 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000373 dma_sync_single_for_cpu(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
Ioana Ciocoi Radulescu18c2e772018-11-26 16:27:32 +0000374 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500375
Ioana Radulescu54ce8912017-12-08 06:47:53 -0600376 fas = dpaa2_get_fas(vaddr, false);
Ioana Radulescud695e762017-06-06 10:00:35 -0500377 prefetch(fas);
378 buf_data = vaddr + dpaa2_fd_get_offset(fd);
379 prefetch(buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500380
381 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500382 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500383
384 if (fd_format == dpaa2_fd_single) {
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000385 xdp_act = run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000386 if (xdp_act != XDP_PASS) {
387 percpu_stats->rx_packets++;
388 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
389 return;
390 }
391
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000392 dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
393 DMA_BIDIRECTIONAL);
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +0000394 skb = build_linear_skb(ch, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500395 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000396 WARN_ON(priv->xdp_prog);
397
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000398 dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
399 DMA_BIDIRECTIONAL);
Ioana Radulescud695e762017-06-06 10:00:35 -0500400 skb = build_frag_skb(priv, ch, buf_data);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000401 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500402 percpu_extras->rx_sg_frames++;
403 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500404 } else {
405 /* We don't support any other format */
406 goto err_frame_format;
407 }
408
409 if (unlikely(!skb))
410 goto err_build_skb;
411
412 prefetch(skb->data);
413
Ioana Radulescu859f9982018-04-26 18:23:47 +0800414 /* Get the timestamp value */
415 if (priv->rx_tstamp) {
416 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
417 __le64 *ts = dpaa2_get_ts(vaddr, false);
418 u64 ns;
419
420 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
421
422 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
423 shhwtstamps->hwtstamp = ns_to_ktime(ns);
424 }
425
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500426 /* Check if we need to validate the L4 csum */
427 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500428 status = le32_to_cpu(fas->status);
429 validate_rx_csum(priv, status, skb);
430 }
431
432 skb->protocol = eth_type_trans(skb, priv->net_dev);
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000433 skb_record_rx_queue(skb, fq->flowid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500434
435 percpu_stats->rx_packets++;
436 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
437
Ioana Ciornei0a25d922019-03-25 13:42:39 +0000438 list_add_tail(&skb->list, ch->rx_list);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500439
440 return;
441
442err_build_skb:
443 free_rx_fd(priv, fd, vaddr);
444err_frame_format:
445 percpu_stats->rx_dropped++;
446}
447
448/* Consume all frames pull-dequeued into the store. This is the simplest way to
449 * make sure we don't accidentally issue another volatile dequeue which would
450 * overwrite (leak) frames already in the store.
451 *
452 * Observance of NAPI budget is not our concern, leaving that to the caller.
453 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000454static int consume_frames(struct dpaa2_eth_channel *ch,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000455 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500456{
457 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000458 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500459 struct dpaa2_dq *dq;
460 const struct dpaa2_fd *fd;
461 int cleaned = 0;
462 int is_last;
463
464 do {
465 dq = dpaa2_io_store_next(ch->store, &is_last);
466 if (unlikely(!dq)) {
467 /* If we're here, we *must* have placed a
468 * volatile dequeue comnmand, so keep reading through
469 * the store until we get some sort of valid response
470 * token (either a valid frame or an "empty dequeue")
471 */
472 continue;
473 }
474
475 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600476 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500477
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000478 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500479 cleaned++;
480 } while (!is_last);
481
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000482 if (!cleaned)
483 return 0;
484
485 fq->stats.frames += cleaned;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000486
487 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000488 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000489 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000490 if (src)
491 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000492
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500493 return cleaned;
494}
495
Ioana Radulescu859f9982018-04-26 18:23:47 +0800496/* Configure the egress frame annotation for timestamp update */
497static void enable_tx_tstamp(struct dpaa2_fd *fd, void *buf_start)
498{
499 struct dpaa2_faead *faead;
500 u32 ctrl, frc;
501
502 /* Mark the egress frame annotation area as valid */
503 frc = dpaa2_fd_get_frc(fd);
504 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
505
506 /* Set hardware annotation size */
507 ctrl = dpaa2_fd_get_ctrl(fd);
508 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
509
510 /* enable UPD (update prepanded data) bit in FAEAD field of
511 * hardware frame annotation area
512 */
513 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
514 faead = dpaa2_get_faead(buf_start, true);
515 faead->ctrl = cpu_to_le32(ctrl);
516}
517
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500518/* Create a frame descriptor based on a fragmented skb */
519static int build_sg_fd(struct dpaa2_eth_priv *priv,
520 struct sk_buff *skb,
521 struct dpaa2_fd *fd)
522{
523 struct device *dev = priv->net_dev->dev.parent;
524 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500525 dma_addr_t addr;
526 int nr_frags = skb_shinfo(skb)->nr_frags;
527 struct dpaa2_sg_entry *sgt;
528 int i, err;
529 int sgt_buf_size;
530 struct scatterlist *scl, *crt_scl;
531 int num_sg;
532 int num_dma_bufs;
533 struct dpaa2_eth_swa *swa;
534
535 /* Create and map scatterlist.
536 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
537 * to go beyond nr_frags+1.
538 * Note: We don't support chained scatterlists
539 */
540 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
541 return -EINVAL;
542
543 scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
544 if (unlikely(!scl))
545 return -ENOMEM;
546
547 sg_init_table(scl, nr_frags + 1);
548 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500549 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500550 if (unlikely(!num_dma_bufs)) {
551 err = -ENOMEM;
552 goto dma_map_sg_failed;
553 }
554
555 /* Prepare the HW SGT structure */
556 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500557 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Sebastian Andrzej Siewior90bc6d42019-06-07 21:20:37 +0200558 sgt_buf = napi_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500559 if (unlikely(!sgt_buf)) {
560 err = -ENOMEM;
561 goto sgt_buf_alloc_failed;
562 }
563 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500564 memset(sgt_buf, 0, sgt_buf_size);
565
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500566 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
567
568 /* Fill in the HW SGT structure.
569 *
570 * sgt_buf is zeroed out, so the following fields are implicit
571 * in all sgt entries:
572 * - offset is 0
573 * - format is 'dpaa2_sg_single'
574 */
575 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
576 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
577 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
578 }
579 dpaa2_sg_set_final(&sgt[i - 1], true);
580
581 /* Store the skb backpointer in the SGT buffer.
582 * Fit the scatterlist and the number of buffers alongside the
583 * skb backpointer in the software annotation area. We'll need
584 * all of them on Tx Conf.
585 */
586 swa = (struct dpaa2_eth_swa *)sgt_buf;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000587 swa->type = DPAA2_ETH_SWA_SG;
588 swa->sg.skb = skb;
589 swa->sg.scl = scl;
590 swa->sg.num_sg = num_sg;
591 swa->sg.sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500592
593 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500594 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500595 if (unlikely(dma_mapping_error(dev, addr))) {
596 err = -ENOMEM;
597 goto dma_map_single_failed;
598 }
599 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
600 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
601 dpaa2_fd_set_addr(fd, addr);
602 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000603 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500604
Ioana Radulescu859f9982018-04-26 18:23:47 +0800605 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
606 enable_tx_tstamp(fd, sgt_buf);
607
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500608 return 0;
609
610dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500611 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500612sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500613 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500614dma_map_sg_failed:
615 kfree(scl);
616 return err;
617}
618
619/* Create a frame descriptor based on a linear skb */
620static int build_single_fd(struct dpaa2_eth_priv *priv,
621 struct sk_buff *skb,
622 struct dpaa2_fd *fd)
623{
624 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600625 u8 *buffer_start, *aligned_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000626 struct dpaa2_eth_swa *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500627 dma_addr_t addr;
628
Ioana Radulescuc1636852017-12-08 06:47:58 -0600629 buffer_start = skb->data - dpaa2_eth_needed_headroom(priv, skb);
630
631 /* If there's enough room to align the FD address, do it.
632 * It will help hardware optimize accesses.
633 */
634 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
635 DPAA2_ETH_TX_BUF_ALIGN);
636 if (aligned_start >= skb->head)
637 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500638
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500639 /* Store a backpointer to the skb at the beginning of the buffer
640 * (in the private data area) such that we can release it
641 * on Tx confirm
642 */
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000643 swa = (struct dpaa2_eth_swa *)buffer_start;
644 swa->type = DPAA2_ETH_SWA_SINGLE;
645 swa->single.skb = skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500646
647 addr = dma_map_single(dev, buffer_start,
648 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500649 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500650 if (unlikely(dma_mapping_error(dev, addr)))
651 return -ENOMEM;
652
653 dpaa2_fd_set_addr(fd, addr);
654 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
655 dpaa2_fd_set_len(fd, skb->len);
656 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000657 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500658
Ioana Radulescu859f9982018-04-26 18:23:47 +0800659 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
660 enable_tx_tstamp(fd, buffer_start);
661
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500662 return 0;
663}
664
665/* FD freeing routine on the Tx path
666 *
667 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
668 * back-pointed to is also freed.
669 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
670 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500671 */
672static void free_tx_fd(const struct dpaa2_eth_priv *priv,
Ioana Radulescud678be12019-03-01 17:47:24 +0000673 struct dpaa2_eth_fq *fq,
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +0000674 const struct dpaa2_fd *fd, bool in_napi)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500675{
676 struct device *dev = priv->net_dev->dev.parent;
677 dma_addr_t fd_addr;
Ioana Radulescud678be12019-03-01 17:47:24 +0000678 struct sk_buff *skb = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500679 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500680 struct dpaa2_eth_swa *swa;
681 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescud678be12019-03-01 17:47:24 +0000682 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500683
684 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000685 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
686 swa = (struct dpaa2_eth_swa *)buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500687
688 if (fd_format == dpaa2_fd_single) {
Ioana Radulescud678be12019-03-01 17:47:24 +0000689 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
690 skb = swa->single.skb;
691 /* Accessing the skb buffer is safe before dma unmap,
692 * because we didn't map the actual skb shell.
693 */
694 dma_unmap_single(dev, fd_addr,
695 skb_tail_pointer(skb) - buffer_start,
696 DMA_BIDIRECTIONAL);
697 } else {
698 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
699 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
700 DMA_BIDIRECTIONAL);
701 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500702 } else if (fd_format == dpaa2_fd_sg) {
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000703 skb = swa->sg.skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500704
705 /* Unmap the scatterlist */
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000706 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
707 DMA_BIDIRECTIONAL);
708 kfree(swa->sg.scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500709
710 /* Unmap the SGT buffer */
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000711 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
Ioana Radulescub2718e62018-03-23 08:44:11 -0500712 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500713 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600714 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500715 return;
716 }
717
Ioana Radulescud678be12019-03-01 17:47:24 +0000718 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
719 fq->dq_frames++;
720 fq->dq_bytes += fd_len;
721 }
722
723 if (swa->type == DPAA2_ETH_SWA_XDP) {
724 xdp_return_frame(swa->xdp.xdpf);
725 return;
726 }
727
Ioana Radulescu859f9982018-04-26 18:23:47 +0800728 /* Get the timestamp value */
729 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
730 struct skb_shared_hwtstamps shhwtstamps;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000731 __le64 *ts = dpaa2_get_ts(buffer_start, true);
Ioana Radulescu859f9982018-04-26 18:23:47 +0800732 u64 ns;
733
734 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
735
736 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
737 shhwtstamps.hwtstamp = ns_to_ktime(ns);
738 skb_tstamp_tx(skb, &shhwtstamps);
739 }
740
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500741 /* Free SGT buffer allocated on tx */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500742 if (fd_format != dpaa2_fd_single)
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000743 skb_free_frag(buffer_start);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500744
745 /* Move on with skb release */
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +0000746 napi_consume_skb(skb, in_napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500747}
748
Ioana Radulescuc433db42017-06-06 10:00:26 -0500749static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500750{
751 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
752 struct dpaa2_fd fd;
753 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500754 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500755 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000756 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500757 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -0600758 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000759 u32 fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500760 int err, i;
761
762 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500763 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500764
Ioana Radulescu18c21462017-12-08 06:47:57 -0600765 needed_headroom = dpaa2_eth_needed_headroom(priv, skb);
766 if (skb_headroom(skb) < needed_headroom) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500767 struct sk_buff *ns;
768
Ioana Radulescu18c21462017-12-08 06:47:57 -0600769 ns = skb_realloc_headroom(skb, needed_headroom);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500770 if (unlikely(!ns)) {
771 percpu_stats->tx_dropped++;
772 goto err_alloc_headroom;
773 }
Ioana Radulescu6662b5e2017-12-08 06:47:55 -0600774 percpu_extras->tx_reallocs++;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800775
776 if (skb->sk)
777 skb_set_owner_w(ns, skb->sk);
778
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500779 dev_kfree_skb(skb);
780 skb = ns;
781 }
782
783 /* We'll be holding a back-reference to the skb until Tx Confirmation;
784 * we don't want that overwritten by a concurrent Tx with a cloned skb.
785 */
786 skb = skb_unshare(skb, GFP_ATOMIC);
787 if (unlikely(!skb)) {
788 /* skb_unshare() has already freed the skb */
789 percpu_stats->tx_dropped++;
790 return NETDEV_TX_OK;
791 }
792
793 /* Setup the FD fields */
794 memset(&fd, 0, sizeof(fd));
795
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500796 if (skb_is_nonlinear(skb)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500797 err = build_sg_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500798 percpu_extras->tx_sg_frames++;
799 percpu_extras->tx_sg_bytes += skb->len;
800 } else {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500801 err = build_single_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500802 }
803
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500804 if (unlikely(err)) {
805 percpu_stats->tx_dropped++;
806 goto err_build_fd;
807 }
808
Ioana Radulescu56361872017-04-28 04:50:32 -0500809 /* Tracing point */
810 trace_dpaa2_tx_fd(net_dev, &fd);
811
Ioana Radulescu537336c2017-12-21 06:33:20 -0600812 /* TxConf FQ selection relies on queue id from the stack.
813 * In case of a forwarded frame from another DPNI interface, we choose
814 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500815 */
Ioana Radulescu537336c2017-12-21 06:33:20 -0600816 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500817 fq = &priv->fq[queue_mapping];
Ioana Ciornei8c838f52019-03-25 13:06:22 +0000818
819 fd_len = dpaa2_fd_get_len(&fd);
820 nq = netdev_get_tx_queue(net_dev, queue_mapping);
821 netdev_tx_sent_queue(nq, fd_len);
822
823 /* Everything that happens after this enqueues might race with
824 * the Tx confirmation callback for this frame
825 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500826 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +0000827 err = priv->enqueue(priv, fq, &fd, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500828 if (err != -EBUSY)
829 break;
830 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500831 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500832 if (unlikely(err < 0)) {
833 percpu_stats->tx_errors++;
834 /* Clean up everything, including freeing the skb */
Ioana Radulescud678be12019-03-01 17:47:24 +0000835 free_tx_fd(priv, fq, &fd, false);
Ioana Ciornei8c838f52019-03-25 13:06:22 +0000836 netdev_tx_completed_queue(nq, 1, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500837 } else {
838 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000839 percpu_stats->tx_bytes += fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500840 }
841
842 return NETDEV_TX_OK;
843
844err_build_fd:
845err_alloc_headroom:
846 dev_kfree_skb(skb);
847
848 return NETDEV_TX_OK;
849}
850
851/* Tx confirmation frame processing routine */
852static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +0000853 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500854 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000855 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500856{
857 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500858 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000859 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500860 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500861
Ioana Radulescu56361872017-04-28 04:50:32 -0500862 /* Tracing point */
863 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
864
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500865 percpu_extras = this_cpu_ptr(priv->percpu_extras);
866 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000867 percpu_extras->tx_conf_bytes += fd_len;
868
Ioana Radulescu39163c02017-06-06 10:00:39 -0500869 /* Check frame errors in the FD field */
870 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Radulescud678be12019-03-01 17:47:24 +0000871 free_tx_fd(priv, fq, fd, true);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500872
873 if (likely(!fd_errors))
874 return;
875
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600876 if (net_ratelimit())
877 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
878 fd_errors);
879
Ioana Radulescu39163c02017-06-06 10:00:39 -0500880 percpu_stats = this_cpu_ptr(priv->percpu_stats);
881 /* Tx-conf logically pertains to the egress path. */
882 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500883}
884
885static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
886{
887 int err;
888
889 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
890 DPNI_OFF_RX_L3_CSUM, enable);
891 if (err) {
892 netdev_err(priv->net_dev,
893 "dpni_set_offload(RX_L3_CSUM) failed\n");
894 return err;
895 }
896
897 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
898 DPNI_OFF_RX_L4_CSUM, enable);
899 if (err) {
900 netdev_err(priv->net_dev,
901 "dpni_set_offload(RX_L4_CSUM) failed\n");
902 return err;
903 }
904
905 return 0;
906}
907
908static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
909{
910 int err;
911
912 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
913 DPNI_OFF_TX_L3_CSUM, enable);
914 if (err) {
915 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
916 return err;
917 }
918
919 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
920 DPNI_OFF_TX_L4_CSUM, enable);
921 if (err) {
922 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
923 return err;
924 }
925
926 return 0;
927}
928
929/* Perform a single release command to add buffers
930 * to the specified buffer pool
931 */
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600932static int add_bufs(struct dpaa2_eth_priv *priv,
933 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500934{
935 struct device *dev = priv->net_dev->dev.parent;
936 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000937 struct page *page;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500938 dma_addr_t addr;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500939 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500940
941 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
942 /* Allocate buffer visible to WRIOP + skb shared info +
943 * alignment padding
944 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000945 /* allocate one page for each Rx buffer. WRIOP sees
946 * the entire page except for a tailroom reserved for
947 * skb shared info
948 */
949 page = dev_alloc_pages(0);
950 if (!page)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500951 goto err_alloc;
952
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000953 addr = dma_map_page(dev, page, 0, DPAA2_ETH_RX_BUF_SIZE,
954 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500955 if (unlikely(dma_mapping_error(dev, addr)))
956 goto err_map;
957
958 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -0500959
960 /* tracing point */
961 trace_dpaa2_eth_buf_seed(priv->net_dev,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000962 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
Ioana Radulescu56361872017-04-28 04:50:32 -0500963 addr, DPAA2_ETH_RX_BUF_SIZE,
964 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500965 }
966
967release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500968 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600969 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500970 buf_array, i)) == -EBUSY)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500971 cpu_relax();
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500972
973 /* If release command failed, clean up and bail out;
974 * not much else we can do about it
975 */
976 if (err) {
977 free_bufs(priv, buf_array, i);
978 return 0;
979 }
980
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500981 return i;
982
983err_map:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000984 __free_pages(page, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500985err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500986 /* If we managed to allocate at least some buffers,
987 * release them to hardware
988 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500989 if (i)
990 goto release_bufs;
991
992 return 0;
993}
994
995static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
996{
997 int i, j;
998 int new_count;
999
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001000 for (j = 0; j < priv->num_channels; j++) {
1001 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1002 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001003 new_count = add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001004 priv->channel[j]->buf_count += new_count;
1005
1006 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001007 return -ENOMEM;
1008 }
1009 }
1010 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001011
1012 return 0;
1013}
1014
1015/**
1016 * Drain the specified number of buffers from the DPNI's private buffer pool.
1017 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1018 */
1019static void drain_bufs(struct dpaa2_eth_priv *priv, int count)
1020{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001021 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001022 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001023
1024 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001025 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001026 buf_array, count);
1027 if (ret < 0) {
1028 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1029 return;
1030 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001031 free_bufs(priv, buf_array, ret);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001032 } while (ret);
1033}
1034
1035static void drain_pool(struct dpaa2_eth_priv *priv)
1036{
1037 int i;
1038
1039 drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1040 drain_bufs(priv, 1);
1041
1042 for (i = 0; i < priv->num_channels; i++)
1043 priv->channel[i]->buf_count = 0;
1044}
1045
1046/* Function is called from softirq context only, so we don't need to guard
1047 * the access to percpu count
1048 */
1049static int refill_pool(struct dpaa2_eth_priv *priv,
1050 struct dpaa2_eth_channel *ch,
1051 u16 bpid)
1052{
1053 int new_count;
1054
1055 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1056 return 0;
1057
1058 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001059 new_count = add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001060 if (unlikely(!new_count)) {
1061 /* Out of memory; abort for now, we'll try later on */
1062 break;
1063 }
1064 ch->buf_count += new_count;
1065 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1066
1067 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1068 return -ENOMEM;
1069
1070 return 0;
1071}
1072
1073static int pull_channel(struct dpaa2_eth_channel *ch)
1074{
1075 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001076 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001077
1078 /* Retry while portal is busy */
1079 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001080 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1081 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001082 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001083 cpu_relax();
1084 } while (err == -EBUSY);
1085
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001086 ch->stats.dequeue_portal_busy += dequeues;
1087 if (unlikely(err))
1088 ch->stats.pull_err++;
1089
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001090 return err;
1091}
1092
1093/* NAPI poll routine
1094 *
1095 * Frames are dequeued from the QMan channel associated with this NAPI context.
1096 * Rx, Tx confirmation and (if configured) Rx error frames all count
1097 * towards the NAPI budget.
1098 */
1099static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1100{
1101 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001102 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001103 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001104 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1105 struct netdev_queue *nq;
1106 int store_cleaned, work_done;
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001107 struct list_head rx_list;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001108 int err;
1109
1110 ch = container_of(napi, struct dpaa2_eth_channel, napi);
Ioana Radulescud678be12019-03-01 17:47:24 +00001111 ch->xdp.res = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001112 priv = ch->priv;
1113
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001114 INIT_LIST_HEAD(&rx_list);
1115 ch->rx_list = &rx_list;
1116
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001117 do {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001118 err = pull_channel(ch);
1119 if (unlikely(err))
1120 break;
1121
1122 /* Refill pool if appropriate */
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001123 refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001124
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001125 store_cleaned = consume_frames(ch, &fq);
1126 if (!store_cleaned)
1127 break;
1128 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001129 rx_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001130 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001131 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001132 /* We have a single Tx conf FQ on this channel */
1133 txc_fq = fq;
1134 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001135
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001136 /* If we either consumed the whole NAPI budget with Rx frames
1137 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001138 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001139 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001140 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1141 work_done = budget;
1142 goto out;
1143 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001144 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001145
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001146 /* We didn't consume the entire budget, so finish napi and
1147 * re-enable data availability notifications
1148 */
1149 napi_complete_done(napi, rx_cleaned);
1150 do {
1151 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1152 cpu_relax();
1153 } while (err == -EBUSY);
1154 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1155 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001156
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001157 work_done = max(rx_cleaned, 1);
1158
1159out:
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001160 netif_receive_skb_list(ch->rx_list);
1161
Ioana Radulescud678be12019-03-01 17:47:24 +00001162 if (txc_fq && txc_fq->dq_frames) {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001163 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1164 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1165 txc_fq->dq_bytes);
1166 txc_fq->dq_frames = 0;
1167 txc_fq->dq_bytes = 0;
1168 }
1169
Ioana Radulescud678be12019-03-01 17:47:24 +00001170 if (ch->xdp.res & XDP_REDIRECT)
1171 xdp_do_flush_map();
1172
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001173 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001174}
1175
1176static void enable_ch_napi(struct dpaa2_eth_priv *priv)
1177{
1178 struct dpaa2_eth_channel *ch;
1179 int i;
1180
1181 for (i = 0; i < priv->num_channels; i++) {
1182 ch = priv->channel[i];
1183 napi_enable(&ch->napi);
1184 }
1185}
1186
1187static void disable_ch_napi(struct dpaa2_eth_priv *priv)
1188{
1189 struct dpaa2_eth_channel *ch;
1190 int i;
1191
1192 for (i = 0; i < priv->num_channels; i++) {
1193 ch = priv->channel[i];
1194 napi_disable(&ch->napi);
1195 }
1196}
1197
1198static int link_state_update(struct dpaa2_eth_priv *priv)
1199{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001200 struct dpni_link_state state = {0};
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001201 int err;
1202
1203 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1204 if (unlikely(err)) {
1205 netdev_err(priv->net_dev,
1206 "dpni_get_link_state() failed\n");
1207 return err;
1208 }
1209
1210 /* Chech link state; speed / duplex changes are not treated yet */
1211 if (priv->link_state.up == state.up)
1212 return 0;
1213
1214 priv->link_state = state;
1215 if (state.up) {
1216 netif_carrier_on(priv->net_dev);
1217 netif_tx_start_all_queues(priv->net_dev);
1218 } else {
1219 netif_tx_stop_all_queues(priv->net_dev);
1220 netif_carrier_off(priv->net_dev);
1221 }
1222
Ioana Radulescu77160af2017-06-06 10:00:28 -05001223 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001224 state.up ? "up" : "down");
1225
1226 return 0;
1227}
1228
1229static int dpaa2_eth_open(struct net_device *net_dev)
1230{
1231 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1232 int err;
1233
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001234 err = seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001235 if (err) {
1236 /* Not much to do; the buffer pool, though not filled up,
1237 * may still contain some buffers which would enable us
1238 * to limp on.
1239 */
1240 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001241 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001242 }
1243
1244 /* We'll only start the txqs when the link is actually ready; make sure
1245 * we don't race against the link up notification, which may come
1246 * immediately after dpni_enable();
1247 */
1248 netif_tx_stop_all_queues(net_dev);
1249 enable_ch_napi(priv);
1250 /* Also, explicitly set carrier off, otherwise netif_carrier_ok() will
1251 * return true and cause 'ip link show' to report the LOWER_UP flag,
1252 * even though the link notification wasn't even received.
1253 */
1254 netif_carrier_off(net_dev);
1255
1256 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1257 if (err < 0) {
1258 netdev_err(net_dev, "dpni_enable() failed\n");
1259 goto enable_err;
1260 }
1261
1262 /* If the DPMAC object has already processed the link up interrupt,
1263 * we have to learn the link state ourselves.
1264 */
1265 err = link_state_update(priv);
1266 if (err < 0) {
1267 netdev_err(net_dev, "Can't update link state\n");
1268 goto link_state_err;
1269 }
1270
1271 return 0;
1272
1273link_state_err:
1274enable_err:
1275 disable_ch_napi(priv);
1276 drain_pool(priv);
1277 return err;
1278}
1279
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001280/* Total number of in-flight frames on ingress queues */
1281static u32 ingress_fq_count(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001282{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001283 struct dpaa2_eth_fq *fq;
1284 u32 fcnt = 0, bcnt = 0, total = 0;
1285 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001286
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001287 for (i = 0; i < priv->num_fqs; i++) {
1288 fq = &priv->fq[i];
1289 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1290 if (err) {
1291 netdev_warn(priv->net_dev, "query_fq_count failed");
1292 break;
1293 }
1294 total += fcnt;
1295 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001296
1297 return total;
1298}
1299
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001300static void wait_for_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001301{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001302 int retries = 10;
1303 u32 pending;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001304
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001305 do {
1306 pending = ingress_fq_count(priv);
1307 if (pending)
1308 msleep(100);
1309 } while (pending && --retries);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001310}
1311
1312static int dpaa2_eth_stop(struct net_device *net_dev)
1313{
1314 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001315 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001316 int retries = 10;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001317
1318 netif_tx_stop_all_queues(net_dev);
1319 netif_carrier_off(net_dev);
1320
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001321 /* On dpni_disable(), the MC firmware will:
1322 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1323 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1324 * of all in flight Tx frames is finished (and corresponding Tx conf
1325 * frames are enqueued back to software)
1326 *
1327 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1328 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1329 * and Tx conf queues are consumed on NAPI poll.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001330 */
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001331 msleep(500);
1332
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001333 do {
1334 dpni_disable(priv->mc_io, 0, priv->mc_token);
1335 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1336 if (dpni_enabled)
1337 /* Allow the hardware some slack */
1338 msleep(100);
1339 } while (dpni_enabled && --retries);
1340 if (!retries) {
1341 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1342 /* Must go on and disable NAPI nonetheless, so we don't crash at
1343 * the next "ifconfig up"
1344 */
1345 }
1346
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001347 wait_for_fq_empty(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001348 disable_ch_napi(priv);
1349
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001350 /* Empty the buffer pool */
1351 drain_pool(priv);
1352
1353 return 0;
1354}
1355
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001356static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1357{
1358 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1359 struct device *dev = net_dev->dev.parent;
1360 int err;
1361
1362 err = eth_mac_addr(net_dev, addr);
1363 if (err < 0) {
1364 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1365 return err;
1366 }
1367
1368 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1369 net_dev->dev_addr);
1370 if (err) {
1371 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1372 return err;
1373 }
1374
1375 return 0;
1376}
1377
1378/** Fill in counters maintained by the GPP driver. These may be different from
1379 * the hardware counters obtained by ethtool.
1380 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001381static void dpaa2_eth_get_stats(struct net_device *net_dev,
1382 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001383{
1384 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1385 struct rtnl_link_stats64 *percpu_stats;
1386 u64 *cpustats;
1387 u64 *netstats = (u64 *)stats;
1388 int i, j;
1389 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1390
1391 for_each_possible_cpu(i) {
1392 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1393 cpustats = (u64 *)percpu_stats;
1394 for (j = 0; j < num; j++)
1395 netstats[j] += cpustats[j];
1396 }
1397}
1398
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001399/* Copy mac unicast addresses from @net_dev to @priv.
1400 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1401 */
1402static void add_uc_hw_addr(const struct net_device *net_dev,
1403 struct dpaa2_eth_priv *priv)
1404{
1405 struct netdev_hw_addr *ha;
1406 int err;
1407
1408 netdev_for_each_uc_addr(ha, net_dev) {
1409 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1410 ha->addr);
1411 if (err)
1412 netdev_warn(priv->net_dev,
1413 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1414 ha->addr, err);
1415 }
1416}
1417
1418/* Copy mac multicast addresses from @net_dev to @priv
1419 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1420 */
1421static void add_mc_hw_addr(const struct net_device *net_dev,
1422 struct dpaa2_eth_priv *priv)
1423{
1424 struct netdev_hw_addr *ha;
1425 int err;
1426
1427 netdev_for_each_mc_addr(ha, net_dev) {
1428 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1429 ha->addr);
1430 if (err)
1431 netdev_warn(priv->net_dev,
1432 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1433 ha->addr, err);
1434 }
1435}
1436
1437static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1438{
1439 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1440 int uc_count = netdev_uc_count(net_dev);
1441 int mc_count = netdev_mc_count(net_dev);
1442 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1443 u32 options = priv->dpni_attrs.options;
1444 u16 mc_token = priv->mc_token;
1445 struct fsl_mc_io *mc_io = priv->mc_io;
1446 int err;
1447
1448 /* Basic sanity checks; these probably indicate a misconfiguration */
1449 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1450 netdev_info(net_dev,
1451 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1452 max_mac);
1453
1454 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1455 if (uc_count > max_mac) {
1456 netdev_info(net_dev,
1457 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1458 uc_count, max_mac);
1459 goto force_promisc;
1460 }
1461 if (mc_count + uc_count > max_mac) {
1462 netdev_info(net_dev,
1463 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1464 uc_count + mc_count, max_mac);
1465 goto force_mc_promisc;
1466 }
1467
1468 /* Adjust promisc settings due to flag combinations */
1469 if (net_dev->flags & IFF_PROMISC)
1470 goto force_promisc;
1471 if (net_dev->flags & IFF_ALLMULTI) {
1472 /* First, rebuild unicast filtering table. This should be done
1473 * in promisc mode, in order to avoid frame loss while we
1474 * progressively add entries to the table.
1475 * We don't know whether we had been in promisc already, and
1476 * making an MC call to find out is expensive; so set uc promisc
1477 * nonetheless.
1478 */
1479 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1480 if (err)
1481 netdev_warn(net_dev, "Can't set uc promisc\n");
1482
1483 /* Actual uc table reconstruction. */
1484 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1485 if (err)
1486 netdev_warn(net_dev, "Can't clear uc filters\n");
1487 add_uc_hw_addr(net_dev, priv);
1488
1489 /* Finally, clear uc promisc and set mc promisc as requested. */
1490 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1491 if (err)
1492 netdev_warn(net_dev, "Can't clear uc promisc\n");
1493 goto force_mc_promisc;
1494 }
1495
1496 /* Neither unicast, nor multicast promisc will be on... eventually.
1497 * For now, rebuild mac filtering tables while forcing both of them on.
1498 */
1499 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1500 if (err)
1501 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1502 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1503 if (err)
1504 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1505
1506 /* Actual mac filtering tables reconstruction */
1507 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1508 if (err)
1509 netdev_warn(net_dev, "Can't clear mac filters\n");
1510 add_mc_hw_addr(net_dev, priv);
1511 add_uc_hw_addr(net_dev, priv);
1512
1513 /* Now we can clear both ucast and mcast promisc, without risking
1514 * to drop legitimate frames anymore.
1515 */
1516 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1517 if (err)
1518 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1519 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1520 if (err)
1521 netdev_warn(net_dev, "Can't clear mcast promisc\n");
1522
1523 return;
1524
1525force_promisc:
1526 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1527 if (err)
1528 netdev_warn(net_dev, "Can't set ucast promisc\n");
1529force_mc_promisc:
1530 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1531 if (err)
1532 netdev_warn(net_dev, "Can't set mcast promisc\n");
1533}
1534
1535static int dpaa2_eth_set_features(struct net_device *net_dev,
1536 netdev_features_t features)
1537{
1538 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1539 netdev_features_t changed = features ^ net_dev->features;
1540 bool enable;
1541 int err;
1542
1543 if (changed & NETIF_F_RXCSUM) {
1544 enable = !!(features & NETIF_F_RXCSUM);
1545 err = set_rx_csum(priv, enable);
1546 if (err)
1547 return err;
1548 }
1549
1550 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1551 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
1552 err = set_tx_csum(priv, enable);
1553 if (err)
1554 return err;
1555 }
1556
1557 return 0;
1558}
1559
Ioana Radulescu859f9982018-04-26 18:23:47 +08001560static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1561{
1562 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1563 struct hwtstamp_config config;
1564
1565 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
1566 return -EFAULT;
1567
1568 switch (config.tx_type) {
1569 case HWTSTAMP_TX_OFF:
1570 priv->tx_tstamp = false;
1571 break;
1572 case HWTSTAMP_TX_ON:
1573 priv->tx_tstamp = true;
1574 break;
1575 default:
1576 return -ERANGE;
1577 }
1578
1579 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
1580 priv->rx_tstamp = false;
1581 } else {
1582 priv->rx_tstamp = true;
1583 /* TS is set for all frame types, not only those requested */
1584 config.rx_filter = HWTSTAMP_FILTER_ALL;
1585 }
1586
1587 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
1588 -EFAULT : 0;
1589}
1590
1591static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1592{
1593 if (cmd == SIOCSHWTSTAMP)
1594 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
1595
1596 return -EINVAL;
1597}
1598
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001599static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
1600{
1601 int mfl, linear_mfl;
1602
1603 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1604 linear_mfl = DPAA2_ETH_RX_BUF_SIZE - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001605 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001606
1607 if (mfl > linear_mfl) {
1608 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
1609 linear_mfl - VLAN_ETH_HLEN);
1610 return false;
1611 }
1612
1613 return true;
1614}
1615
1616static int set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
1617{
1618 int mfl, err;
1619
1620 /* We enforce a maximum Rx frame length based on MTU only if we have
1621 * an XDP program attached (in order to avoid Rx S/G frames).
1622 * Otherwise, we accept all incoming frames as long as they are not
1623 * larger than maximum size supported in hardware
1624 */
1625 if (has_xdp)
1626 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1627 else
1628 mfl = DPAA2_ETH_MFL;
1629
1630 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
1631 if (err) {
1632 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
1633 return err;
1634 }
1635
1636 return 0;
1637}
1638
1639static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
1640{
1641 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1642 int err;
1643
1644 if (!priv->xdp_prog)
1645 goto out;
1646
1647 if (!xdp_mtu_valid(priv, new_mtu))
1648 return -EINVAL;
1649
1650 err = set_rx_mfl(priv, new_mtu, true);
1651 if (err)
1652 return err;
1653
1654out:
1655 dev->mtu = new_mtu;
1656 return 0;
1657}
1658
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001659static int update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
1660{
1661 struct dpni_buffer_layout buf_layout = {0};
1662 int err;
1663
1664 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
1665 DPNI_QUEUE_RX, &buf_layout);
1666 if (err) {
1667 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
1668 return err;
1669 }
1670
1671 /* Reserve extra headroom for XDP header size changes */
1672 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
1673 (has_xdp ? XDP_PACKET_HEADROOM : 0);
1674 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
1675 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
1676 DPNI_QUEUE_RX, &buf_layout);
1677 if (err) {
1678 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
1679 return err;
1680 }
1681
1682 return 0;
1683}
1684
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001685static int setup_xdp(struct net_device *dev, struct bpf_prog *prog)
1686{
1687 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1688 struct dpaa2_eth_channel *ch;
1689 struct bpf_prog *old;
1690 bool up, need_update;
1691 int i, err;
1692
1693 if (prog && !xdp_mtu_valid(priv, dev->mtu))
1694 return -EINVAL;
1695
1696 if (prog) {
1697 prog = bpf_prog_add(prog, priv->num_channels);
1698 if (IS_ERR(prog))
1699 return PTR_ERR(prog);
1700 }
1701
1702 up = netif_running(dev);
1703 need_update = (!!priv->xdp_prog != !!prog);
1704
1705 if (up)
1706 dpaa2_eth_stop(dev);
1707
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001708 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
1709 * Also, when switching between xdp/non-xdp modes we need to reconfigure
1710 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
1711 * so we are sure no old format buffers will be used from now on.
1712 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001713 if (need_update) {
1714 err = set_rx_mfl(priv, dev->mtu, !!prog);
1715 if (err)
1716 goto out_err;
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001717 err = update_rx_buffer_headroom(priv, !!prog);
1718 if (err)
1719 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001720 }
1721
1722 old = xchg(&priv->xdp_prog, prog);
1723 if (old)
1724 bpf_prog_put(old);
1725
1726 for (i = 0; i < priv->num_channels; i++) {
1727 ch = priv->channel[i];
1728 old = xchg(&ch->xdp.prog, prog);
1729 if (old)
1730 bpf_prog_put(old);
1731 }
1732
1733 if (up) {
1734 err = dpaa2_eth_open(dev);
1735 if (err)
1736 return err;
1737 }
1738
1739 return 0;
1740
1741out_err:
1742 if (prog)
1743 bpf_prog_sub(prog, priv->num_channels);
1744 if (up)
1745 dpaa2_eth_open(dev);
1746
1747 return err;
1748}
1749
1750static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1751{
1752 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1753
1754 switch (xdp->command) {
1755 case XDP_SETUP_PROG:
1756 return setup_xdp(dev, xdp->prog);
1757 case XDP_QUERY_PROG:
1758 xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0;
1759 break;
1760 default:
1761 return -EINVAL;
1762 }
1763
1764 return 0;
1765}
1766
Ioana Radulescud678be12019-03-01 17:47:24 +00001767static int dpaa2_eth_xdp_xmit_frame(struct net_device *net_dev,
1768 struct xdp_frame *xdpf)
1769{
1770 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1771 struct device *dev = net_dev->dev.parent;
1772 struct rtnl_link_stats64 *percpu_stats;
1773 struct dpaa2_eth_drv_stats *percpu_extras;
1774 unsigned int needed_headroom;
1775 struct dpaa2_eth_swa *swa;
1776 struct dpaa2_eth_fq *fq;
1777 struct dpaa2_fd fd;
1778 void *buffer_start, *aligned_start;
1779 dma_addr_t addr;
1780 int err, i;
1781
1782 /* We require a minimum headroom to be able to transmit the frame.
1783 * Otherwise return an error and let the original net_device handle it
1784 */
1785 needed_headroom = dpaa2_eth_needed_headroom(priv, NULL);
1786 if (xdpf->headroom < needed_headroom)
1787 return -EINVAL;
1788
1789 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1790 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1791
1792 /* Setup the FD fields */
1793 memset(&fd, 0, sizeof(fd));
1794
1795 /* Align FD address, if possible */
1796 buffer_start = xdpf->data - needed_headroom;
1797 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
1798 DPAA2_ETH_TX_BUF_ALIGN);
1799 if (aligned_start >= xdpf->data - xdpf->headroom)
1800 buffer_start = aligned_start;
1801
1802 swa = (struct dpaa2_eth_swa *)buffer_start;
1803 /* fill in necessary fields here */
1804 swa->type = DPAA2_ETH_SWA_XDP;
1805 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
1806 swa->xdp.xdpf = xdpf;
1807
1808 addr = dma_map_single(dev, buffer_start,
1809 swa->xdp.dma_size,
1810 DMA_BIDIRECTIONAL);
1811 if (unlikely(dma_mapping_error(dev, addr))) {
1812 percpu_stats->tx_dropped++;
1813 return -ENOMEM;
1814 }
1815
1816 dpaa2_fd_set_addr(&fd, addr);
1817 dpaa2_fd_set_offset(&fd, xdpf->data - buffer_start);
1818 dpaa2_fd_set_len(&fd, xdpf->len);
1819 dpaa2_fd_set_format(&fd, dpaa2_fd_single);
1820 dpaa2_fd_set_ctrl(&fd, FD_CTRL_PTA);
1821
Ioana Ciocoi Radulescu64447502019-03-20 14:11:04 +00001822 fq = &priv->fq[smp_processor_id() % dpaa2_eth_queue_count(priv)];
Ioana Radulescud678be12019-03-01 17:47:24 +00001823 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
1824 err = priv->enqueue(priv, fq, &fd, 0);
1825 if (err != -EBUSY)
1826 break;
1827 }
1828 percpu_extras->tx_portal_busy += i;
1829 if (unlikely(err < 0)) {
1830 percpu_stats->tx_errors++;
1831 /* let the Rx device handle the cleanup */
1832 return err;
1833 }
1834
1835 percpu_stats->tx_packets++;
1836 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fd);
1837
1838 return 0;
1839}
1840
1841static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
1842 struct xdp_frame **frames, u32 flags)
1843{
1844 int drops = 0;
1845 int i, err;
1846
1847 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1848 return -EINVAL;
1849
1850 if (!netif_running(net_dev))
1851 return -ENETDOWN;
1852
1853 for (i = 0; i < n; i++) {
1854 struct xdp_frame *xdpf = frames[i];
1855
1856 err = dpaa2_eth_xdp_xmit_frame(net_dev, xdpf);
1857 if (err) {
1858 xdp_return_frame_rx_napi(xdpf);
1859 drops++;
1860 }
1861 }
1862
1863 return n - drops;
1864}
1865
Ioana Radulescu06d5b172019-06-11 14:50:01 +03001866static int update_xps(struct dpaa2_eth_priv *priv)
1867{
1868 struct net_device *net_dev = priv->net_dev;
1869 struct cpumask xps_mask;
1870 struct dpaa2_eth_fq *fq;
1871 int i, num_queues;
1872 int err = 0;
1873
1874 num_queues = dpaa2_eth_queue_count(priv);
1875
1876 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
1877 * queues, so only process those
1878 */
1879 for (i = 0; i < num_queues; i++) {
1880 fq = &priv->fq[i];
1881
1882 cpumask_clear(&xps_mask);
1883 cpumask_set_cpu(fq->target_cpu, &xps_mask);
1884
1885 err = netif_set_xps_queue(net_dev, &xps_mask, i);
1886 if (err) {
1887 netdev_warn_once(net_dev, "Error setting XPS queue\n");
1888 break;
1889 }
1890 }
1891
1892 return err;
1893}
1894
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001895static const struct net_device_ops dpaa2_eth_ops = {
1896 .ndo_open = dpaa2_eth_open,
1897 .ndo_start_xmit = dpaa2_eth_tx,
1898 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001899 .ndo_set_mac_address = dpaa2_eth_set_addr,
1900 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001901 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
1902 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08001903 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001904 .ndo_change_mtu = dpaa2_eth_change_mtu,
1905 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescud678be12019-03-01 17:47:24 +00001906 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001907};
1908
1909static void cdan_cb(struct dpaa2_io_notification_ctx *ctx)
1910{
1911 struct dpaa2_eth_channel *ch;
1912
1913 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001914
1915 /* Update NAPI statistics */
1916 ch->stats.cdan++;
1917
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001918 napi_schedule_irqoff(&ch->napi);
1919}
1920
1921/* Allocate and configure a DPCON object */
1922static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv)
1923{
1924 struct fsl_mc_device *dpcon;
1925 struct device *dev = priv->net_dev->dev.parent;
1926 struct dpcon_attr attrs;
1927 int err;
1928
1929 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
1930 FSL_MC_POOL_DPCON, &dpcon);
1931 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001932 if (err == -ENXIO)
1933 err = -EPROBE_DEFER;
1934 else
1935 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
1936 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001937 }
1938
1939 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
1940 if (err) {
1941 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001942 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001943 }
1944
1945 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
1946 if (err) {
1947 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001948 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001949 }
1950
1951 err = dpcon_get_attributes(priv->mc_io, 0, dpcon->mc_handle, &attrs);
1952 if (err) {
1953 dev_err(dev, "dpcon_get_attributes() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001954 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001955 }
1956
1957 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
1958 if (err) {
1959 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001960 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001961 }
1962
1963 return dpcon;
1964
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001965close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001966 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00001967free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001968 fsl_mc_object_free(dpcon);
1969
1970 return NULL;
1971}
1972
1973static void free_dpcon(struct dpaa2_eth_priv *priv,
1974 struct fsl_mc_device *dpcon)
1975{
1976 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
1977 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
1978 fsl_mc_object_free(dpcon);
1979}
1980
1981static struct dpaa2_eth_channel *
1982alloc_channel(struct dpaa2_eth_priv *priv)
1983{
1984 struct dpaa2_eth_channel *channel;
1985 struct dpcon_attr attr;
1986 struct device *dev = priv->net_dev->dev.parent;
1987 int err;
1988
1989 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
1990 if (!channel)
1991 return NULL;
1992
1993 channel->dpcon = setup_dpcon(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001994 if (IS_ERR_OR_NULL(channel->dpcon)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03001995 err = PTR_ERR_OR_ZERO(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001996 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00001997 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001998
1999 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2000 &attr);
2001 if (err) {
2002 dev_err(dev, "dpcon_get_attributes() failed\n");
2003 goto err_get_attr;
2004 }
2005
2006 channel->dpcon_id = attr.id;
2007 channel->ch_id = attr.qbman_ch_id;
2008 channel->priv = priv;
2009
2010 return channel;
2011
2012err_get_attr:
2013 free_dpcon(priv, channel->dpcon);
2014err_setup:
2015 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002016 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002017}
2018
2019static void free_channel(struct dpaa2_eth_priv *priv,
2020 struct dpaa2_eth_channel *channel)
2021{
2022 free_dpcon(priv, channel->dpcon);
2023 kfree(channel);
2024}
2025
2026/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2027 * and register data availability notifications
2028 */
2029static int setup_dpio(struct dpaa2_eth_priv *priv)
2030{
2031 struct dpaa2_io_notification_ctx *nctx;
2032 struct dpaa2_eth_channel *channel;
2033 struct dpcon_notification_cfg dpcon_notif_cfg;
2034 struct device *dev = priv->net_dev->dev.parent;
2035 int i, err;
2036
2037 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2038 * many cores as possible, so we need one channel for each core
2039 * (unless there's fewer queues than cores, in which case the extra
2040 * channels would be wasted).
2041 * Allocate one channel per core and register it to the core's
2042 * affine DPIO. If not enough channels are available for all cores
2043 * or if some cores don't have an affine DPIO, there will be no
2044 * ingress frame processing on those cores.
2045 */
2046 cpumask_clear(&priv->dpio_cpumask);
2047 for_each_online_cpu(i) {
2048 /* Try to allocate a channel */
2049 channel = alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002050 if (IS_ERR_OR_NULL(channel)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002051 err = PTR_ERR_OR_ZERO(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002052 if (err != -EPROBE_DEFER)
2053 dev_info(dev,
2054 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002055 goto err_alloc_ch;
2056 }
2057
2058 priv->channel[priv->num_channels] = channel;
2059
2060 nctx = &channel->nctx;
2061 nctx->is_cdan = 1;
2062 nctx->cb = cdan_cb;
2063 nctx->id = channel->ch_id;
2064 nctx->desired_cpu = i;
2065
2066 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06002067 channel->dpio = dpaa2_io_service_select(i);
Ioana Ciornei47441f72018-12-10 16:50:19 +00002068 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002069 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002070 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002071 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002072 * none available for next cores either. Signal we want
2073 * to retry later, in case the DPIO devices weren't
2074 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002075 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002076 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002077 goto err_service_reg;
2078 }
2079
2080 /* Register DPCON notification with MC */
2081 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2082 dpcon_notif_cfg.priority = 0;
2083 dpcon_notif_cfg.user_ctx = nctx->qman64;
2084 err = dpcon_set_notification(priv->mc_io, 0,
2085 channel->dpcon->mc_handle,
2086 &dpcon_notif_cfg);
2087 if (err) {
2088 dev_err(dev, "dpcon_set_notification failed()\n");
2089 goto err_set_cdan;
2090 }
2091
2092 /* If we managed to allocate a channel and also found an affine
2093 * DPIO for this core, add it to the final mask
2094 */
2095 cpumask_set_cpu(i, &priv->dpio_cpumask);
2096 priv->num_channels++;
2097
2098 /* Stop if we already have enough channels to accommodate all
2099 * RX and TX conf queues
2100 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00002101 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002102 break;
2103 }
2104
2105 return 0;
2106
2107err_set_cdan:
Ioana Ciornei47441f72018-12-10 16:50:19 +00002108 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002109err_service_reg:
2110 free_channel(priv, channel);
2111err_alloc_ch:
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002112 if (err == -EPROBE_DEFER)
2113 return err;
2114
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002115 if (cpumask_empty(&priv->dpio_cpumask)) {
2116 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002117 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002118 }
2119
2120 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2121 cpumask_pr_args(&priv->dpio_cpumask));
2122
2123 return 0;
2124}
2125
2126static void free_dpio(struct dpaa2_eth_priv *priv)
2127{
Ioana Ciornei47441f72018-12-10 16:50:19 +00002128 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002129 struct dpaa2_eth_channel *ch;
Ioana Ciornei47441f72018-12-10 16:50:19 +00002130 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002131
2132 /* deregister CDAN notifications and free channels */
2133 for (i = 0; i < priv->num_channels; i++) {
2134 ch = priv->channel[i];
Ioana Ciornei47441f72018-12-10 16:50:19 +00002135 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002136 free_channel(priv, ch);
2137 }
2138}
2139
2140static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv,
2141 int cpu)
2142{
2143 struct device *dev = priv->net_dev->dev.parent;
2144 int i;
2145
2146 for (i = 0; i < priv->num_channels; i++)
2147 if (priv->channel[i]->nctx.desired_cpu == cpu)
2148 return priv->channel[i];
2149
2150 /* We should never get here. Issue a warning and return
2151 * the first channel, because it's still better than nothing
2152 */
2153 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2154
2155 return priv->channel[0];
2156}
2157
2158static void set_fq_affinity(struct dpaa2_eth_priv *priv)
2159{
2160 struct device *dev = priv->net_dev->dev.parent;
2161 struct dpaa2_eth_fq *fq;
2162 int rx_cpu, txc_cpu;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002163 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002164
2165 /* For each FQ, pick one channel/CPU to deliver frames to.
2166 * This may well change at runtime, either through irqbalance or
2167 * through direct user intervention.
2168 */
2169 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2170
2171 for (i = 0; i < priv->num_fqs; i++) {
2172 fq = &priv->fq[i];
2173 switch (fq->type) {
2174 case DPAA2_RX_FQ:
2175 fq->target_cpu = rx_cpu;
2176 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2177 if (rx_cpu >= nr_cpu_ids)
2178 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2179 break;
2180 case DPAA2_TX_CONF_FQ:
2181 fq->target_cpu = txc_cpu;
2182 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2183 if (txc_cpu >= nr_cpu_ids)
2184 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2185 break;
2186 default:
2187 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2188 }
2189 fq->channel = get_affine_channel(priv, fq->target_cpu);
2190 }
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002191
2192 update_xps(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002193}
2194
2195static void setup_fqs(struct dpaa2_eth_priv *priv)
2196{
2197 int i;
2198
2199 /* We have one TxConf FQ per Tx flow.
2200 * The number of Tx and Rx queues is the same.
2201 * Tx queues come first in the fq array.
2202 */
2203 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2204 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2205 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2206 priv->fq[priv->num_fqs++].flowid = (u16)i;
2207 }
2208
2209 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2210 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2211 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2212 priv->fq[priv->num_fqs++].flowid = (u16)i;
2213 }
2214
2215 /* For each FQ, decide on which core to process incoming frames */
2216 set_fq_affinity(priv);
2217}
2218
2219/* Allocate and configure one buffer pool for each interface */
2220static int setup_dpbp(struct dpaa2_eth_priv *priv)
2221{
2222 int err;
2223 struct fsl_mc_device *dpbp_dev;
2224 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002225 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002226
2227 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2228 &dpbp_dev);
2229 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002230 if (err == -ENXIO)
2231 err = -EPROBE_DEFER;
2232 else
2233 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002234 return err;
2235 }
2236
2237 priv->dpbp_dev = dpbp_dev;
2238
2239 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2240 &dpbp_dev->mc_handle);
2241 if (err) {
2242 dev_err(dev, "dpbp_open() failed\n");
2243 goto err_open;
2244 }
2245
Ioana Radulescud00defe2017-06-06 10:00:32 -05002246 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2247 if (err) {
2248 dev_err(dev, "dpbp_reset() failed\n");
2249 goto err_reset;
2250 }
2251
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002252 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2253 if (err) {
2254 dev_err(dev, "dpbp_enable() failed\n");
2255 goto err_enable;
2256 }
2257
2258 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002259 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002260 if (err) {
2261 dev_err(dev, "dpbp_get_attributes() failed\n");
2262 goto err_get_attr;
2263 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002264 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002265
2266 return 0;
2267
2268err_get_attr:
2269 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2270err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002271err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002272 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2273err_open:
2274 fsl_mc_object_free(dpbp_dev);
2275
2276 return err;
2277}
2278
2279static void free_dpbp(struct dpaa2_eth_priv *priv)
2280{
2281 drain_pool(priv);
2282 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2283 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2284 fsl_mc_object_free(priv->dpbp_dev);
2285}
2286
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002287static int set_buffer_layout(struct dpaa2_eth_priv *priv)
2288{
2289 struct device *dev = priv->net_dev->dev.parent;
2290 struct dpni_buffer_layout buf_layout = {0};
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002291 u16 rx_buf_align;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002292 int err;
2293
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002294 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2295 * version, this number is not always provided correctly on rev1.
2296 * We need to check for both alternatives in this situation.
2297 */
2298 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2299 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002300 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002301 else
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002302 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002303
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002304 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002305 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002306 buf_layout.pass_timestamp = true;
2307 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2308 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002309 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2310 DPNI_QUEUE_TX, &buf_layout);
2311 if (err) {
2312 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2313 return err;
2314 }
2315
2316 /* tx-confirm buffer */
Ioana Radulescu859f9982018-04-26 18:23:47 +08002317 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002318 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2319 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2320 if (err) {
2321 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2322 return err;
2323 }
2324
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002325 /* Now that we've set our tx buffer layout, retrieve the minimum
2326 * required tx data offset.
2327 */
2328 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2329 &priv->tx_data_offset);
2330 if (err) {
2331 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2332 return err;
2333 }
2334
2335 if ((priv->tx_data_offset % 64) != 0)
2336 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2337 priv->tx_data_offset);
2338
2339 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06002340 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002341 buf_layout.pass_parser_result = true;
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002342 buf_layout.data_align = rx_buf_align;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002343 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2344 buf_layout.private_data_size = 0;
2345 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2346 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2347 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08002348 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2349 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002350 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2351 DPNI_QUEUE_RX, &buf_layout);
2352 if (err) {
2353 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2354 return err;
2355 }
2356
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002357 return 0;
2358}
2359
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002360#define DPNI_ENQUEUE_FQID_VER_MAJOR 7
2361#define DPNI_ENQUEUE_FQID_VER_MINOR 9
2362
2363static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2364 struct dpaa2_eth_fq *fq,
2365 struct dpaa2_fd *fd, u8 prio)
2366{
2367 return dpaa2_io_service_enqueue_qd(fq->channel->dpio,
2368 priv->tx_qdid, prio,
2369 fq->tx_qdbin, fd);
2370}
2371
2372static inline int dpaa2_eth_enqueue_fq(struct dpaa2_eth_priv *priv,
2373 struct dpaa2_eth_fq *fq,
2374 struct dpaa2_fd *fd,
2375 u8 prio __always_unused)
2376{
2377 return dpaa2_io_service_enqueue_fq(fq->channel->dpio,
2378 fq->tx_fqid, fd);
2379}
2380
2381static void set_enqueue_mode(struct dpaa2_eth_priv *priv)
2382{
2383 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2384 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2385 priv->enqueue = dpaa2_eth_enqueue_qd;
2386 else
2387 priv->enqueue = dpaa2_eth_enqueue_fq;
2388}
2389
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002390/* Configure the DPNI object this interface is associated with */
2391static int setup_dpni(struct fsl_mc_device *ls_dev)
2392{
2393 struct device *dev = &ls_dev->dev;
2394 struct dpaa2_eth_priv *priv;
2395 struct net_device *net_dev;
2396 int err;
2397
2398 net_dev = dev_get_drvdata(dev);
2399 priv = netdev_priv(net_dev);
2400
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002401 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05002402 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002403 if (err) {
2404 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002405 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002406 }
2407
Ioana Radulescu311cffa2018-03-23 08:44:09 -05002408 /* Check if we can work with this DPNI object */
2409 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
2410 &priv->dpni_ver_minor);
2411 if (err) {
2412 dev_err(dev, "dpni_get_api_version() failed\n");
2413 goto close;
2414 }
2415 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
2416 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
2417 priv->dpni_ver_major, priv->dpni_ver_minor,
2418 DPNI_VER_MAJOR, DPNI_VER_MINOR);
2419 err = -ENOTSUPP;
2420 goto close;
2421 }
2422
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002423 ls_dev->mc_io = priv->mc_io;
2424 ls_dev->mc_handle = priv->mc_token;
2425
2426 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2427 if (err) {
2428 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002429 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002430 }
2431
2432 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
2433 &priv->dpni_attrs);
2434 if (err) {
2435 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002436 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002437 }
2438
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002439 err = set_buffer_layout(priv);
2440 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002441 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002442
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002443 set_enqueue_mode(priv);
2444
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002445 priv->cls_rules = devm_kzalloc(dev, sizeof(struct dpaa2_eth_cls_rule) *
2446 dpaa2_eth_fs_count(priv), GFP_KERNEL);
2447 if (!priv->cls_rules)
2448 goto close;
2449
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002450 return 0;
2451
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002452close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002453 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002454
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002455 return err;
2456}
2457
2458static void free_dpni(struct dpaa2_eth_priv *priv)
2459{
2460 int err;
2461
2462 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2463 if (err)
2464 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
2465 err);
2466
2467 dpni_close(priv->mc_io, 0, priv->mc_token);
2468}
2469
2470static int setup_rx_flow(struct dpaa2_eth_priv *priv,
2471 struct dpaa2_eth_fq *fq)
2472{
2473 struct device *dev = priv->net_dev->dev.parent;
2474 struct dpni_queue queue;
2475 struct dpni_queue_id qid;
2476 struct dpni_taildrop td;
2477 int err;
2478
2479 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2480 DPNI_QUEUE_RX, 0, fq->flowid, &queue, &qid);
2481 if (err) {
2482 dev_err(dev, "dpni_get_queue(RX) failed\n");
2483 return err;
2484 }
2485
2486 fq->fqid = qid.fqid;
2487
2488 queue.destination.id = fq->channel->dpcon_id;
2489 queue.destination.type = DPNI_DEST_DPCON;
2490 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002491 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002492 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2493 DPNI_QUEUE_RX, 0, fq->flowid,
Ioana Radulescu16fa1cf2019-05-23 17:38:22 +03002494 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002495 &queue);
2496 if (err) {
2497 dev_err(dev, "dpni_set_queue(RX) failed\n");
2498 return err;
2499 }
2500
2501 td.enable = 1;
2502 td.threshold = DPAA2_ETH_TAILDROP_THRESH;
2503 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, DPNI_CP_QUEUE,
2504 DPNI_QUEUE_RX, 0, fq->flowid, &td);
2505 if (err) {
2506 dev_err(dev, "dpni_set_threshold() failed\n");
2507 return err;
2508 }
2509
Ioana Radulescud678be12019-03-01 17:47:24 +00002510 /* xdp_rxq setup */
2511 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
2512 fq->flowid);
2513 if (err) {
2514 dev_err(dev, "xdp_rxq_info_reg failed\n");
2515 return err;
2516 }
2517
2518 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
2519 MEM_TYPE_PAGE_ORDER0, NULL);
2520 if (err) {
2521 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
2522 return err;
2523 }
2524
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002525 return 0;
2526}
2527
2528static int setup_tx_flow(struct dpaa2_eth_priv *priv,
2529 struct dpaa2_eth_fq *fq)
2530{
2531 struct device *dev = priv->net_dev->dev.parent;
2532 struct dpni_queue queue;
2533 struct dpni_queue_id qid;
2534 int err;
2535
2536 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2537 DPNI_QUEUE_TX, 0, fq->flowid, &queue, &qid);
2538 if (err) {
2539 dev_err(dev, "dpni_get_queue(TX) failed\n");
2540 return err;
2541 }
2542
2543 fq->tx_qdbin = qid.qdbin;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002544 fq->tx_fqid = qid.fqid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002545
2546 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2547 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2548 &queue, &qid);
2549 if (err) {
2550 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
2551 return err;
2552 }
2553
2554 fq->fqid = qid.fqid;
2555
2556 queue.destination.id = fq->channel->dpcon_id;
2557 queue.destination.type = DPNI_DEST_DPCON;
2558 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002559 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002560 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2561 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2562 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2563 &queue);
2564 if (err) {
2565 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
2566 return err;
2567 }
2568
2569 return 0;
2570}
2571
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002572/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002573static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002574 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002575 /* L2 header */
2576 .rxnfc_field = RXH_L2DA,
2577 .cls_prot = NET_PROT_ETH,
2578 .cls_field = NH_FLD_ETH_DA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002579 .id = DPAA2_ETH_DIST_ETHDST,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002580 .size = 6,
2581 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002582 .cls_prot = NET_PROT_ETH,
2583 .cls_field = NH_FLD_ETH_SA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002584 .id = DPAA2_ETH_DIST_ETHSRC,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002585 .size = 6,
2586 }, {
2587 /* This is the last ethertype field parsed:
2588 * depending on frame format, it can be the MAC ethertype
2589 * or the VLAN etype.
2590 */
2591 .cls_prot = NET_PROT_ETH,
2592 .cls_field = NH_FLD_ETH_TYPE,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002593 .id = DPAA2_ETH_DIST_ETHTYPE,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002594 .size = 2,
2595 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002596 /* VLAN header */
2597 .rxnfc_field = RXH_VLAN,
2598 .cls_prot = NET_PROT_VLAN,
2599 .cls_field = NH_FLD_VLAN_TCI,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002600 .id = DPAA2_ETH_DIST_VLAN,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002601 .size = 2,
2602 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002603 /* IP header */
2604 .rxnfc_field = RXH_IP_SRC,
2605 .cls_prot = NET_PROT_IP,
2606 .cls_field = NH_FLD_IP_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002607 .id = DPAA2_ETH_DIST_IPSRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002608 .size = 4,
2609 }, {
2610 .rxnfc_field = RXH_IP_DST,
2611 .cls_prot = NET_PROT_IP,
2612 .cls_field = NH_FLD_IP_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002613 .id = DPAA2_ETH_DIST_IPDST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002614 .size = 4,
2615 }, {
2616 .rxnfc_field = RXH_L3_PROTO,
2617 .cls_prot = NET_PROT_IP,
2618 .cls_field = NH_FLD_IP_PROTO,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002619 .id = DPAA2_ETH_DIST_IPPROTO,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002620 .size = 1,
2621 }, {
2622 /* Using UDP ports, this is functionally equivalent to raw
2623 * byte pairs from L4 header.
2624 */
2625 .rxnfc_field = RXH_L4_B_0_1,
2626 .cls_prot = NET_PROT_UDP,
2627 .cls_field = NH_FLD_UDP_PORT_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002628 .id = DPAA2_ETH_DIST_L4SRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002629 .size = 2,
2630 }, {
2631 .rxnfc_field = RXH_L4_B_2_3,
2632 .cls_prot = NET_PROT_UDP,
2633 .cls_field = NH_FLD_UDP_PORT_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002634 .id = DPAA2_ETH_DIST_L4DST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002635 .size = 2,
2636 },
2637};
2638
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002639/* Configure the Rx hash key using the legacy API */
2640static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2641{
2642 struct device *dev = priv->net_dev->dev.parent;
2643 struct dpni_rx_tc_dist_cfg dist_cfg;
2644 int err;
2645
2646 memset(&dist_cfg, 0, sizeof(dist_cfg));
2647
2648 dist_cfg.key_cfg_iova = key;
2649 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2650 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
2651
2652 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg);
2653 if (err)
2654 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
2655
2656 return err;
2657}
2658
2659/* Configure the Rx hash key using the new API */
2660static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2661{
2662 struct device *dev = priv->net_dev->dev.parent;
2663 struct dpni_rx_dist_cfg dist_cfg;
2664 int err;
2665
2666 memset(&dist_cfg, 0, sizeof(dist_cfg));
2667
2668 dist_cfg.key_cfg_iova = key;
2669 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2670 dist_cfg.enable = 1;
2671
2672 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2673 if (err)
2674 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
2675
2676 return err;
2677}
2678
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002679/* Configure the Rx flow classification key */
2680static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2681{
2682 struct device *dev = priv->net_dev->dev.parent;
2683 struct dpni_rx_dist_cfg dist_cfg;
2684 int err;
2685
2686 memset(&dist_cfg, 0, sizeof(dist_cfg));
2687
2688 dist_cfg.key_cfg_iova = key;
2689 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2690 dist_cfg.enable = 1;
2691
2692 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2693 if (err)
2694 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
2695
2696 return err;
2697}
2698
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002699/* Size of the Rx flow classification key */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002700int dpaa2_eth_cls_key_size(u64 fields)
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002701{
2702 int i, size = 0;
2703
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002704 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2705 if (!(fields & dist_fields[i].id))
2706 continue;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002707 size += dist_fields[i].size;
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002708 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002709
2710 return size;
2711}
2712
2713/* Offset of header field in Rx classification key */
2714int dpaa2_eth_cls_fld_off(int prot, int field)
2715{
2716 int i, off = 0;
2717
2718 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2719 if (dist_fields[i].cls_prot == prot &&
2720 dist_fields[i].cls_field == field)
2721 return off;
2722 off += dist_fields[i].size;
2723 }
2724
2725 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
2726 return 0;
2727}
2728
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002729/* Prune unused fields from the classification rule.
2730 * Used when masking is not supported
2731 */
2732void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
2733{
2734 int off = 0, new_off = 0;
2735 int i, size;
2736
2737 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2738 size = dist_fields[i].size;
2739 if (dist_fields[i].id & fields) {
2740 memcpy(key_mem + new_off, key_mem + off, size);
2741 new_off += size;
2742 }
2743 off += size;
2744 }
2745}
2746
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002747/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002748 * flags is a combination of RXH_ bits
2749 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00002750static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
2751 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002752{
2753 struct device *dev = net_dev->dev.parent;
2754 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2755 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002756 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002757 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002758 u8 *dma_mem;
2759 int i;
2760 int err = 0;
2761
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002762 memset(&cls_cfg, 0, sizeof(cls_cfg));
2763
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002764 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002765 struct dpkg_extract *key =
2766 &cls_cfg.extracts[cls_cfg.num_extracts];
2767
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002768 /* For both Rx hashing and classification keys
2769 * we set only the selected fields.
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002770 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002771 if (!(flags & dist_fields[i].id))
2772 continue;
2773 if (type == DPAA2_ETH_RX_DIST_HASH)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002774 rx_hash_fields |= dist_fields[i].rxnfc_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002775
2776 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
2777 dev_err(dev, "error adding key extraction rule, too many rules?\n");
2778 return -E2BIG;
2779 }
2780
2781 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002782 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002783 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002784 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002785 cls_cfg.num_extracts++;
2786 }
2787
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05002788 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002789 if (!dma_mem)
2790 return -ENOMEM;
2791
2792 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
2793 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002794 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002795 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002796 }
2797
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002798 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002799 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
2800 DMA_TO_DEVICE);
2801 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002802 dev_err(dev, "DMA mapping failed\n");
2803 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002804 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002805 }
2806
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002807 if (type == DPAA2_ETH_RX_DIST_HASH) {
2808 if (dpaa2_eth_has_legacy_dist(priv))
2809 err = config_legacy_hash_key(priv, key_iova);
2810 else
2811 err = config_hash_key(priv, key_iova);
2812 } else {
2813 err = config_cls_key(priv, key_iova);
2814 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002815
2816 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
2817 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002818 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002819 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002820
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002821free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002822 kfree(dma_mem);
2823 return err;
2824}
2825
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002826int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
2827{
2828 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002829 u64 key = 0;
2830 int i;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002831
2832 if (!dpaa2_eth_hash_enabled(priv))
2833 return -EOPNOTSUPP;
2834
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002835 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
2836 if (dist_fields[i].rxnfc_field & flags)
2837 key |= dist_fields[i].id;
2838
2839 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002840}
2841
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002842int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
2843{
2844 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
2845}
2846
2847static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002848{
2849 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00002850 int err;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002851
2852 /* Check if we actually support Rx flow classification */
2853 if (dpaa2_eth_has_legacy_dist(priv)) {
2854 dev_dbg(dev, "Rx cls not supported by current MC version\n");
2855 return -EOPNOTSUPP;
2856 }
2857
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002858 if (!dpaa2_eth_fs_enabled(priv)) {
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002859 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
2860 return -EOPNOTSUPP;
2861 }
2862
2863 if (!dpaa2_eth_hash_enabled(priv)) {
2864 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
2865 return -EOPNOTSUPP;
2866 }
2867
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002868 /* If there is no support for masking in the classification table,
2869 * we don't set a default key, as it will depend on the rules
2870 * added by the user at runtime.
2871 */
2872 if (!dpaa2_eth_fs_mask_enabled(priv))
2873 goto out;
2874
2875 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00002876 if (err)
2877 return err;
2878
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002879out:
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002880 priv->rx_cls_enabled = 1;
2881
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00002882 return 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002883}
2884
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002885/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
2886 * frame queues and channels
2887 */
2888static int bind_dpni(struct dpaa2_eth_priv *priv)
2889{
2890 struct net_device *net_dev = priv->net_dev;
2891 struct device *dev = net_dev->dev.parent;
2892 struct dpni_pools_cfg pools_params;
2893 struct dpni_error_cfg err_cfg;
2894 int err = 0;
2895 int i;
2896
2897 pools_params.num_dpbp = 1;
2898 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
2899 pools_params.pools[0].backup_pool = 0;
2900 pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE;
2901 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
2902 if (err) {
2903 dev_err(dev, "dpni_set_pools() failed\n");
2904 return err;
2905 }
2906
Ioana Radulescu227686b2018-07-27 09:12:59 -05002907 /* have the interface implicitly distribute traffic based on
2908 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002909 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05002910 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002911 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05002912 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002913
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002914 /* Configure the flow classification key; it includes all
2915 * supported header fields and cannot be modified at runtime
2916 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002917 err = dpaa2_eth_set_default_cls(priv);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002918 if (err && err != -EOPNOTSUPP)
2919 dev_err(dev, "Failed to configure Rx classification key\n");
2920
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002921 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05002922 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002923 err_cfg.set_frame_annotation = 1;
2924 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
2925 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
2926 &err_cfg);
2927 if (err) {
2928 dev_err(dev, "dpni_set_errors_behavior failed\n");
2929 return err;
2930 }
2931
2932 /* Configure Rx and Tx conf queues to generate CDANs */
2933 for (i = 0; i < priv->num_fqs; i++) {
2934 switch (priv->fq[i].type) {
2935 case DPAA2_RX_FQ:
2936 err = setup_rx_flow(priv, &priv->fq[i]);
2937 break;
2938 case DPAA2_TX_CONF_FQ:
2939 err = setup_tx_flow(priv, &priv->fq[i]);
2940 break;
2941 default:
2942 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
2943 return -EINVAL;
2944 }
2945 if (err)
2946 return err;
2947 }
2948
2949 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
2950 DPNI_QUEUE_TX, &priv->tx_qdid);
2951 if (err) {
2952 dev_err(dev, "dpni_get_qdid() failed\n");
2953 return err;
2954 }
2955
2956 return 0;
2957}
2958
2959/* Allocate rings for storing incoming frame descriptors */
2960static int alloc_rings(struct dpaa2_eth_priv *priv)
2961{
2962 struct net_device *net_dev = priv->net_dev;
2963 struct device *dev = net_dev->dev.parent;
2964 int i;
2965
2966 for (i = 0; i < priv->num_channels; i++) {
2967 priv->channel[i]->store =
2968 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
2969 if (!priv->channel[i]->store) {
2970 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
2971 goto err_ring;
2972 }
2973 }
2974
2975 return 0;
2976
2977err_ring:
2978 for (i = 0; i < priv->num_channels; i++) {
2979 if (!priv->channel[i]->store)
2980 break;
2981 dpaa2_io_store_destroy(priv->channel[i]->store);
2982 }
2983
2984 return -ENOMEM;
2985}
2986
2987static void free_rings(struct dpaa2_eth_priv *priv)
2988{
2989 int i;
2990
2991 for (i = 0; i < priv->num_channels; i++)
2992 dpaa2_io_store_destroy(priv->channel[i]->store);
2993}
2994
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002995static int set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002996{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05002997 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002998 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002999 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003000 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003001
3002 /* Get firmware address, if any */
3003 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3004 if (err) {
3005 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3006 return err;
3007 }
3008
3009 /* Get DPNI attributes address, if any */
3010 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3011 dpni_mac_addr);
3012 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003013 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003014 return err;
3015 }
3016
3017 /* First check if firmware has any address configured by bootloader */
3018 if (!is_zero_ether_addr(mac_addr)) {
3019 /* If the DPMAC addr != DPNI addr, update it */
3020 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
3021 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
3022 priv->mc_token,
3023 mac_addr);
3024 if (err) {
3025 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3026 return err;
3027 }
3028 }
3029 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
3030 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003031 /* No MAC address configured, fill in net_dev->dev_addr
3032 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003033 */
3034 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003035 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
3036
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003037 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3038 net_dev->dev_addr);
3039 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003040 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003041 return err;
3042 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003043
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003044 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
3045 * practical purposes, this will be our "permanent" mac address,
3046 * at least until the next reboot. This move will also permit
3047 * register_netdevice() to properly fill up net_dev->perm_addr.
3048 */
3049 net_dev->addr_assign_type = NET_ADDR_PERM;
3050 } else {
3051 /* NET_ADDR_PERM is default, all we have to do is
3052 * fill in the device addr.
3053 */
3054 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
3055 }
3056
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003057 return 0;
3058}
3059
3060static int netdev_init(struct net_device *net_dev)
3061{
3062 struct device *dev = net_dev->dev.parent;
3063 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003064 u32 options = priv->dpni_attrs.options;
3065 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003066 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003067 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003068 int err;
3069
3070 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003071 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003072
3073 err = set_mac_addr(priv);
3074 if (err)
3075 return err;
3076
3077 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003078 eth_broadcast_addr(bcast_addr);
3079 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3080 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003081 dev_err(dev, "dpni_add_mac_addr() failed\n");
3082 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003083 }
3084
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05003085 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003086 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05003087 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05003088 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05003089 if (err) {
3090 dev_err(dev, "dpni_set_max_frame_length() failed\n");
3091 return err;
3092 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003093
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003094 /* Set actual number of queues in the net device */
3095 num_queues = dpaa2_eth_queue_count(priv);
3096 err = netif_set_real_num_tx_queues(net_dev, num_queues);
3097 if (err) {
3098 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
3099 return err;
3100 }
3101 err = netif_set_real_num_rx_queues(net_dev, num_queues);
3102 if (err) {
3103 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
3104 return err;
3105 }
3106
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003107 /* Capabilities listing */
3108 supported |= IFF_LIVE_ADDR_CHANGE;
3109
3110 if (options & DPNI_OPT_NO_MAC_FILTER)
3111 not_supported |= IFF_UNICAST_FLT;
3112 else
3113 supported |= IFF_UNICAST_FLT;
3114
3115 net_dev->priv_flags |= supported;
3116 net_dev->priv_flags &= ~not_supported;
3117
3118 /* Features */
3119 net_dev->features = NETIF_F_RXCSUM |
3120 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3121 NETIF_F_SG | NETIF_F_HIGHDMA |
3122 NETIF_F_LLTX;
3123 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003124
3125 return 0;
3126}
3127
3128static int poll_link_state(void *arg)
3129{
3130 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
3131 int err;
3132
3133 while (!kthread_should_stop()) {
3134 err = link_state_update(priv);
3135 if (unlikely(err))
3136 return err;
3137
3138 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
3139 }
3140
3141 return 0;
3142}
3143
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003144static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
3145{
Ioana Radulescu112197d2017-10-11 08:29:49 -05003146 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003147 struct device *dev = (struct device *)arg;
3148 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
3149 struct net_device *net_dev = dev_get_drvdata(dev);
3150 int err;
3151
3152 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
3153 DPNI_IRQ_INDEX, &status);
3154 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003155 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05003156 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003157 }
3158
Ioana Radulescu112197d2017-10-11 08:29:49 -05003159 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003160 link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003161
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003162 return IRQ_HANDLED;
3163}
3164
3165static int setup_irqs(struct fsl_mc_device *ls_dev)
3166{
3167 int err = 0;
3168 struct fsl_mc_device_irq *irq;
3169
3170 err = fsl_mc_allocate_irqs(ls_dev);
3171 if (err) {
3172 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
3173 return err;
3174 }
3175
3176 irq = ls_dev->irqs[0];
3177 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05003178 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003179 IRQF_NO_SUSPEND | IRQF_ONESHOT,
3180 dev_name(&ls_dev->dev), &ls_dev->dev);
3181 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003182 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003183 goto free_mc_irq;
3184 }
3185
3186 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
3187 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED);
3188 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003189 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003190 goto free_irq;
3191 }
3192
3193 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
3194 DPNI_IRQ_INDEX, 1);
3195 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003196 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003197 goto free_irq;
3198 }
3199
3200 return 0;
3201
3202free_irq:
3203 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
3204free_mc_irq:
3205 fsl_mc_free_irqs(ls_dev);
3206
3207 return err;
3208}
3209
3210static void add_ch_napi(struct dpaa2_eth_priv *priv)
3211{
3212 int i;
3213 struct dpaa2_eth_channel *ch;
3214
3215 for (i = 0; i < priv->num_channels; i++) {
3216 ch = priv->channel[i];
3217 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
3218 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
3219 NAPI_POLL_WEIGHT);
3220 }
3221}
3222
3223static void del_ch_napi(struct dpaa2_eth_priv *priv)
3224{
3225 int i;
3226 struct dpaa2_eth_channel *ch;
3227
3228 for (i = 0; i < priv->num_channels; i++) {
3229 ch = priv->channel[i];
3230 netif_napi_del(&ch->napi);
3231 }
3232}
3233
3234static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
3235{
3236 struct device *dev;
3237 struct net_device *net_dev = NULL;
3238 struct dpaa2_eth_priv *priv = NULL;
3239 int err = 0;
3240
3241 dev = &dpni_dev->dev;
3242
3243 /* Net device */
3244 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_TX_QUEUES);
3245 if (!net_dev) {
3246 dev_err(dev, "alloc_etherdev_mq() failed\n");
3247 return -ENOMEM;
3248 }
3249
3250 SET_NETDEV_DEV(net_dev, dev);
3251 dev_set_drvdata(dev, net_dev);
3252
3253 priv = netdev_priv(net_dev);
3254 priv->net_dev = net_dev;
3255
Ioana Radulescu08eb2392017-05-24 07:13:27 -05003256 priv->iommu_domain = iommu_get_domain_for_dev(dev);
3257
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003258 /* Obtain a MC portal */
3259 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3260 &priv->mc_io);
3261 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05003262 if (err == -ENXIO)
3263 err = -EPROBE_DEFER;
3264 else
3265 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003266 goto err_portal_alloc;
3267 }
3268
3269 /* MC objects initialization and configuration */
3270 err = setup_dpni(dpni_dev);
3271 if (err)
3272 goto err_dpni_setup;
3273
3274 err = setup_dpio(priv);
3275 if (err)
3276 goto err_dpio_setup;
3277
3278 setup_fqs(priv);
3279
3280 err = setup_dpbp(priv);
3281 if (err)
3282 goto err_dpbp_setup;
3283
3284 err = bind_dpni(priv);
3285 if (err)
3286 goto err_bind;
3287
3288 /* Add a NAPI context for each channel */
3289 add_ch_napi(priv);
3290
3291 /* Percpu statistics */
3292 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
3293 if (!priv->percpu_stats) {
3294 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
3295 err = -ENOMEM;
3296 goto err_alloc_percpu_stats;
3297 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003298 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
3299 if (!priv->percpu_extras) {
3300 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
3301 err = -ENOMEM;
3302 goto err_alloc_percpu_extras;
3303 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003304
3305 err = netdev_init(net_dev);
3306 if (err)
3307 goto err_netdev_init;
3308
3309 /* Configure checksum offload based on current interface flags */
3310 err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
3311 if (err)
3312 goto err_csum;
3313
3314 err = set_tx_csum(priv, !!(net_dev->features &
3315 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
3316 if (err)
3317 goto err_csum;
3318
3319 err = alloc_rings(priv);
3320 if (err)
3321 goto err_alloc_rings;
3322
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003323 err = setup_irqs(dpni_dev);
3324 if (err) {
3325 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
3326 priv->poll_thread = kthread_run(poll_link_state, priv,
3327 "%s_poll_link", net_dev->name);
3328 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003329 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003330 goto err_poll_thread;
3331 }
3332 priv->do_link_poll = true;
3333 }
3334
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003335 err = register_netdev(net_dev);
3336 if (err < 0) {
3337 dev_err(dev, "register_netdev() failed\n");
3338 goto err_netdev_reg;
3339 }
3340
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003341#ifdef CONFIG_DEBUG_FS
3342 dpaa2_dbg_add(priv);
3343#endif
3344
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003345 dev_info(dev, "Probed interface %s\n", net_dev->name);
3346 return 0;
3347
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003348err_netdev_reg:
3349 if (priv->do_link_poll)
3350 kthread_stop(priv->poll_thread);
3351 else
3352 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003353err_poll_thread:
3354 free_rings(priv);
3355err_alloc_rings:
3356err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003357err_netdev_init:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003358 free_percpu(priv->percpu_extras);
3359err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003360 free_percpu(priv->percpu_stats);
3361err_alloc_percpu_stats:
3362 del_ch_napi(priv);
3363err_bind:
3364 free_dpbp(priv);
3365err_dpbp_setup:
3366 free_dpio(priv);
3367err_dpio_setup:
3368 free_dpni(priv);
3369err_dpni_setup:
3370 fsl_mc_portal_free(priv->mc_io);
3371err_portal_alloc:
3372 dev_set_drvdata(dev, NULL);
3373 free_netdev(net_dev);
3374
3375 return err;
3376}
3377
3378static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
3379{
3380 struct device *dev;
3381 struct net_device *net_dev;
3382 struct dpaa2_eth_priv *priv;
3383
3384 dev = &ls_dev->dev;
3385 net_dev = dev_get_drvdata(dev);
3386 priv = netdev_priv(net_dev);
3387
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003388#ifdef CONFIG_DEBUG_FS
3389 dpaa2_dbg_remove(priv);
3390#endif
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003391 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003392
3393 if (priv->do_link_poll)
3394 kthread_stop(priv->poll_thread);
3395 else
3396 fsl_mc_free_irqs(ls_dev);
3397
3398 free_rings(priv);
3399 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003400 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003401
3402 del_ch_napi(priv);
3403 free_dpbp(priv);
3404 free_dpio(priv);
3405 free_dpni(priv);
3406
3407 fsl_mc_portal_free(priv->mc_io);
3408
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003409 free_netdev(net_dev);
3410
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05003411 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05003412
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003413 return 0;
3414}
3415
3416static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
3417 {
3418 .vendor = FSL_MC_VENDOR_FREESCALE,
3419 .obj_type = "dpni",
3420 },
3421 { .vendor = 0x0 }
3422};
3423MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
3424
3425static struct fsl_mc_driver dpaa2_eth_driver = {
3426 .driver = {
3427 .name = KBUILD_MODNAME,
3428 .owner = THIS_MODULE,
3429 },
3430 .probe = dpaa2_eth_probe,
3431 .remove = dpaa2_eth_remove,
3432 .match_id_table = dpaa2_eth_match_id_table
3433};
3434
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003435static int __init dpaa2_eth_driver_init(void)
3436{
3437 int err;
3438
3439 dpaa2_eth_dbg_init();
3440 err = fsl_mc_driver_register(&dpaa2_eth_driver);
3441 if (err) {
3442 dpaa2_eth_dbg_exit();
3443 return err;
3444 }
3445
3446 return 0;
3447}
3448
3449static void __exit dpaa2_eth_driver_exit(void)
3450{
3451 dpaa2_eth_dbg_exit();
3452 fsl_mc_driver_unregister(&dpaa2_eth_driver);
3453}
3454
3455module_init(dpaa2_eth_driver_init);
3456module_exit(dpaa2_eth_driver_exit);