blob: 19379bae014430e3828f037dc7dfa3ea47a9a1d7 [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 Radulescuab1e6de2019-06-11 14:50:03 +0300760 u8 prio = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500761 int err, i;
762
763 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500764 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500765
Ioana Radulescu18c21462017-12-08 06:47:57 -0600766 needed_headroom = dpaa2_eth_needed_headroom(priv, skb);
767 if (skb_headroom(skb) < needed_headroom) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500768 struct sk_buff *ns;
769
Ioana Radulescu18c21462017-12-08 06:47:57 -0600770 ns = skb_realloc_headroom(skb, needed_headroom);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500771 if (unlikely(!ns)) {
772 percpu_stats->tx_dropped++;
773 goto err_alloc_headroom;
774 }
Ioana Radulescu6662b5e2017-12-08 06:47:55 -0600775 percpu_extras->tx_reallocs++;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800776
777 if (skb->sk)
778 skb_set_owner_w(ns, skb->sk);
779
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500780 dev_kfree_skb(skb);
781 skb = ns;
782 }
783
784 /* We'll be holding a back-reference to the skb until Tx Confirmation;
785 * we don't want that overwritten by a concurrent Tx with a cloned skb.
786 */
787 skb = skb_unshare(skb, GFP_ATOMIC);
788 if (unlikely(!skb)) {
789 /* skb_unshare() has already freed the skb */
790 percpu_stats->tx_dropped++;
791 return NETDEV_TX_OK;
792 }
793
794 /* Setup the FD fields */
795 memset(&fd, 0, sizeof(fd));
796
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500797 if (skb_is_nonlinear(skb)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500798 err = build_sg_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500799 percpu_extras->tx_sg_frames++;
800 percpu_extras->tx_sg_bytes += skb->len;
801 } else {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500802 err = build_single_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500803 }
804
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500805 if (unlikely(err)) {
806 percpu_stats->tx_dropped++;
807 goto err_build_fd;
808 }
809
Ioana Radulescu56361872017-04-28 04:50:32 -0500810 /* Tracing point */
811 trace_dpaa2_tx_fd(net_dev, &fd);
812
Ioana Radulescu537336c2017-12-21 06:33:20 -0600813 /* TxConf FQ selection relies on queue id from the stack.
814 * In case of a forwarded frame from another DPNI interface, we choose
815 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500816 */
Ioana Radulescu537336c2017-12-21 06:33:20 -0600817 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +0300818
819 if (net_dev->num_tc) {
820 prio = netdev_txq_to_tc(net_dev, queue_mapping);
821 /* Hardware interprets priority level 0 as being the highest,
822 * so we need to do a reverse mapping to the netdev tc index
823 */
824 prio = net_dev->num_tc - prio - 1;
825 /* We have only one FQ array entry for all Tx hardware queues
826 * with the same flow id (but different priority levels)
827 */
828 queue_mapping %= dpaa2_eth_queue_count(priv);
829 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500830 fq = &priv->fq[queue_mapping];
Ioana Ciornei8c838f52019-03-25 13:06:22 +0000831
832 fd_len = dpaa2_fd_get_len(&fd);
833 nq = netdev_get_tx_queue(net_dev, queue_mapping);
834 netdev_tx_sent_queue(nq, fd_len);
835
836 /* Everything that happens after this enqueues might race with
837 * the Tx confirmation callback for this frame
838 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500839 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Radulescuab1e6de2019-06-11 14:50:03 +0300840 err = priv->enqueue(priv, fq, &fd, prio);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500841 if (err != -EBUSY)
842 break;
843 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500844 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500845 if (unlikely(err < 0)) {
846 percpu_stats->tx_errors++;
847 /* Clean up everything, including freeing the skb */
Ioana Radulescud678be12019-03-01 17:47:24 +0000848 free_tx_fd(priv, fq, &fd, false);
Ioana Ciornei8c838f52019-03-25 13:06:22 +0000849 netdev_tx_completed_queue(nq, 1, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500850 } else {
851 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000852 percpu_stats->tx_bytes += fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500853 }
854
855 return NETDEV_TX_OK;
856
857err_build_fd:
858err_alloc_headroom:
859 dev_kfree_skb(skb);
860
861 return NETDEV_TX_OK;
862}
863
864/* Tx confirmation frame processing routine */
865static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +0000866 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500867 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000868 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500869{
870 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500871 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000872 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500873 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500874
Ioana Radulescu56361872017-04-28 04:50:32 -0500875 /* Tracing point */
876 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
877
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500878 percpu_extras = this_cpu_ptr(priv->percpu_extras);
879 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000880 percpu_extras->tx_conf_bytes += fd_len;
881
Ioana Radulescu39163c02017-06-06 10:00:39 -0500882 /* Check frame errors in the FD field */
883 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Radulescud678be12019-03-01 17:47:24 +0000884 free_tx_fd(priv, fq, fd, true);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500885
886 if (likely(!fd_errors))
887 return;
888
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600889 if (net_ratelimit())
890 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
891 fd_errors);
892
Ioana Radulescu39163c02017-06-06 10:00:39 -0500893 percpu_stats = this_cpu_ptr(priv->percpu_stats);
894 /* Tx-conf logically pertains to the egress path. */
895 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500896}
897
898static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
899{
900 int err;
901
902 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
903 DPNI_OFF_RX_L3_CSUM, enable);
904 if (err) {
905 netdev_err(priv->net_dev,
906 "dpni_set_offload(RX_L3_CSUM) failed\n");
907 return err;
908 }
909
910 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
911 DPNI_OFF_RX_L4_CSUM, enable);
912 if (err) {
913 netdev_err(priv->net_dev,
914 "dpni_set_offload(RX_L4_CSUM) failed\n");
915 return err;
916 }
917
918 return 0;
919}
920
921static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
922{
923 int err;
924
925 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
926 DPNI_OFF_TX_L3_CSUM, enable);
927 if (err) {
928 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
929 return err;
930 }
931
932 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
933 DPNI_OFF_TX_L4_CSUM, enable);
934 if (err) {
935 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
936 return err;
937 }
938
939 return 0;
940}
941
942/* Perform a single release command to add buffers
943 * to the specified buffer pool
944 */
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600945static int add_bufs(struct dpaa2_eth_priv *priv,
946 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500947{
948 struct device *dev = priv->net_dev->dev.parent;
949 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000950 struct page *page;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500951 dma_addr_t addr;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500952 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500953
954 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
955 /* Allocate buffer visible to WRIOP + skb shared info +
956 * alignment padding
957 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000958 /* allocate one page for each Rx buffer. WRIOP sees
959 * the entire page except for a tailroom reserved for
960 * skb shared info
961 */
962 page = dev_alloc_pages(0);
963 if (!page)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500964 goto err_alloc;
965
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000966 addr = dma_map_page(dev, page, 0, DPAA2_ETH_RX_BUF_SIZE,
967 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500968 if (unlikely(dma_mapping_error(dev, addr)))
969 goto err_map;
970
971 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -0500972
973 /* tracing point */
974 trace_dpaa2_eth_buf_seed(priv->net_dev,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000975 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
Ioana Radulescu56361872017-04-28 04:50:32 -0500976 addr, DPAA2_ETH_RX_BUF_SIZE,
977 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500978 }
979
980release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500981 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -0600982 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500983 buf_array, i)) == -EBUSY)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500984 cpu_relax();
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500985
986 /* If release command failed, clean up and bail out;
987 * not much else we can do about it
988 */
989 if (err) {
990 free_bufs(priv, buf_array, i);
991 return 0;
992 }
993
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500994 return i;
995
996err_map:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000997 __free_pages(page, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500998err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -0500999 /* If we managed to allocate at least some buffers,
1000 * release them to hardware
1001 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001002 if (i)
1003 goto release_bufs;
1004
1005 return 0;
1006}
1007
1008static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
1009{
1010 int i, j;
1011 int new_count;
1012
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001013 for (j = 0; j < priv->num_channels; j++) {
1014 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1015 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001016 new_count = add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001017 priv->channel[j]->buf_count += new_count;
1018
1019 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001020 return -ENOMEM;
1021 }
1022 }
1023 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001024
1025 return 0;
1026}
1027
1028/**
1029 * Drain the specified number of buffers from the DPNI's private buffer pool.
1030 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1031 */
1032static void drain_bufs(struct dpaa2_eth_priv *priv, int count)
1033{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001034 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001035 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001036
1037 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001038 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001039 buf_array, count);
1040 if (ret < 0) {
1041 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1042 return;
1043 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001044 free_bufs(priv, buf_array, ret);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001045 } while (ret);
1046}
1047
1048static void drain_pool(struct dpaa2_eth_priv *priv)
1049{
1050 int i;
1051
1052 drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1053 drain_bufs(priv, 1);
1054
1055 for (i = 0; i < priv->num_channels; i++)
1056 priv->channel[i]->buf_count = 0;
1057}
1058
1059/* Function is called from softirq context only, so we don't need to guard
1060 * the access to percpu count
1061 */
1062static int refill_pool(struct dpaa2_eth_priv *priv,
1063 struct dpaa2_eth_channel *ch,
1064 u16 bpid)
1065{
1066 int new_count;
1067
1068 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1069 return 0;
1070
1071 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001072 new_count = add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001073 if (unlikely(!new_count)) {
1074 /* Out of memory; abort for now, we'll try later on */
1075 break;
1076 }
1077 ch->buf_count += new_count;
1078 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1079
1080 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1081 return -ENOMEM;
1082
1083 return 0;
1084}
1085
1086static int pull_channel(struct dpaa2_eth_channel *ch)
1087{
1088 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001089 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001090
1091 /* Retry while portal is busy */
1092 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001093 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1094 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001095 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001096 cpu_relax();
1097 } while (err == -EBUSY);
1098
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001099 ch->stats.dequeue_portal_busy += dequeues;
1100 if (unlikely(err))
1101 ch->stats.pull_err++;
1102
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001103 return err;
1104}
1105
1106/* NAPI poll routine
1107 *
1108 * Frames are dequeued from the QMan channel associated with this NAPI context.
1109 * Rx, Tx confirmation and (if configured) Rx error frames all count
1110 * towards the NAPI budget.
1111 */
1112static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1113{
1114 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001115 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001116 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001117 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1118 struct netdev_queue *nq;
1119 int store_cleaned, work_done;
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001120 struct list_head rx_list;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001121 int err;
1122
1123 ch = container_of(napi, struct dpaa2_eth_channel, napi);
Ioana Radulescud678be12019-03-01 17:47:24 +00001124 ch->xdp.res = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001125 priv = ch->priv;
1126
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001127 INIT_LIST_HEAD(&rx_list);
1128 ch->rx_list = &rx_list;
1129
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001130 do {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001131 err = pull_channel(ch);
1132 if (unlikely(err))
1133 break;
1134
1135 /* Refill pool if appropriate */
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001136 refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001137
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001138 store_cleaned = consume_frames(ch, &fq);
1139 if (!store_cleaned)
1140 break;
1141 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001142 rx_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001143 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001144 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001145 /* We have a single Tx conf FQ on this channel */
1146 txc_fq = fq;
1147 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001148
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001149 /* If we either consumed the whole NAPI budget with Rx frames
1150 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001151 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001152 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001153 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1154 work_done = budget;
1155 goto out;
1156 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001157 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001158
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001159 /* We didn't consume the entire budget, so finish napi and
1160 * re-enable data availability notifications
1161 */
1162 napi_complete_done(napi, rx_cleaned);
1163 do {
1164 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1165 cpu_relax();
1166 } while (err == -EBUSY);
1167 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1168 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001169
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001170 work_done = max(rx_cleaned, 1);
1171
1172out:
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001173 netif_receive_skb_list(ch->rx_list);
1174
Ioana Radulescud678be12019-03-01 17:47:24 +00001175 if (txc_fq && txc_fq->dq_frames) {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001176 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1177 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1178 txc_fq->dq_bytes);
1179 txc_fq->dq_frames = 0;
1180 txc_fq->dq_bytes = 0;
1181 }
1182
Ioana Radulescud678be12019-03-01 17:47:24 +00001183 if (ch->xdp.res & XDP_REDIRECT)
1184 xdp_do_flush_map();
1185
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001186 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001187}
1188
1189static void enable_ch_napi(struct dpaa2_eth_priv *priv)
1190{
1191 struct dpaa2_eth_channel *ch;
1192 int i;
1193
1194 for (i = 0; i < priv->num_channels; i++) {
1195 ch = priv->channel[i];
1196 napi_enable(&ch->napi);
1197 }
1198}
1199
1200static void disable_ch_napi(struct dpaa2_eth_priv *priv)
1201{
1202 struct dpaa2_eth_channel *ch;
1203 int i;
1204
1205 for (i = 0; i < priv->num_channels; i++) {
1206 ch = priv->channel[i];
1207 napi_disable(&ch->napi);
1208 }
1209}
1210
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001211static void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv, bool enable)
1212{
1213 struct dpni_taildrop td = {0};
1214 int i, err;
1215
1216 if (priv->rx_td_enabled == enable)
1217 return;
1218
1219 td.enable = enable;
1220 td.threshold = DPAA2_ETH_TAILDROP_THRESH;
1221
1222 for (i = 0; i < priv->num_fqs; i++) {
1223 if (priv->fq[i].type != DPAA2_RX_FQ)
1224 continue;
1225 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1226 DPNI_CP_QUEUE, DPNI_QUEUE_RX, 0,
1227 priv->fq[i].flowid, &td);
1228 if (err) {
1229 netdev_err(priv->net_dev,
1230 "dpni_set_taildrop() failed\n");
1231 break;
1232 }
1233 }
1234
1235 priv->rx_td_enabled = enable;
1236}
1237
Ioana Radulescua690af4f2019-10-16 10:36:23 +03001238static void update_tx_fqids(struct dpaa2_eth_priv *priv);
1239
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001240static int link_state_update(struct dpaa2_eth_priv *priv)
1241{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001242 struct dpni_link_state state = {0};
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001243 bool tx_pause;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001244 int err;
1245
1246 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1247 if (unlikely(err)) {
1248 netdev_err(priv->net_dev,
1249 "dpni_get_link_state() failed\n");
1250 return err;
1251 }
1252
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001253 /* If Tx pause frame settings have changed, we need to update
1254 * Rx FQ taildrop configuration as well. We configure taildrop
1255 * only when pause frame generation is disabled.
1256 */
1257 tx_pause = !!(state.options & DPNI_LINK_OPT_PAUSE) ^
1258 !!(state.options & DPNI_LINK_OPT_ASYM_PAUSE);
1259 dpaa2_eth_set_rx_taildrop(priv, !tx_pause);
1260
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001261 /* Chech link state; speed / duplex changes are not treated yet */
1262 if (priv->link_state.up == state.up)
Ioana Radulescucce629432019-08-28 17:08:14 +03001263 goto out;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001264
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001265 if (state.up) {
Ioana Radulescua690af4f2019-10-16 10:36:23 +03001266 update_tx_fqids(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001267 netif_carrier_on(priv->net_dev);
1268 netif_tx_start_all_queues(priv->net_dev);
1269 } else {
1270 netif_tx_stop_all_queues(priv->net_dev);
1271 netif_carrier_off(priv->net_dev);
1272 }
1273
Ioana Radulescu77160af2017-06-06 10:00:28 -05001274 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001275 state.up ? "up" : "down");
1276
Ioana Radulescucce629432019-08-28 17:08:14 +03001277out:
1278 priv->link_state = state;
1279
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001280 return 0;
1281}
1282
1283static int dpaa2_eth_open(struct net_device *net_dev)
1284{
1285 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1286 int err;
1287
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001288 err = seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001289 if (err) {
1290 /* Not much to do; the buffer pool, though not filled up,
1291 * may still contain some buffers which would enable us
1292 * to limp on.
1293 */
1294 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001295 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001296 }
1297
1298 /* We'll only start the txqs when the link is actually ready; make sure
1299 * we don't race against the link up notification, which may come
1300 * immediately after dpni_enable();
1301 */
1302 netif_tx_stop_all_queues(net_dev);
1303 enable_ch_napi(priv);
1304 /* Also, explicitly set carrier off, otherwise netif_carrier_ok() will
1305 * return true and cause 'ip link show' to report the LOWER_UP flag,
1306 * even though the link notification wasn't even received.
1307 */
1308 netif_carrier_off(net_dev);
1309
1310 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1311 if (err < 0) {
1312 netdev_err(net_dev, "dpni_enable() failed\n");
1313 goto enable_err;
1314 }
1315
1316 /* If the DPMAC object has already processed the link up interrupt,
1317 * we have to learn the link state ourselves.
1318 */
1319 err = link_state_update(priv);
1320 if (err < 0) {
1321 netdev_err(net_dev, "Can't update link state\n");
1322 goto link_state_err;
1323 }
1324
1325 return 0;
1326
1327link_state_err:
1328enable_err:
1329 disable_ch_napi(priv);
1330 drain_pool(priv);
1331 return err;
1332}
1333
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001334/* Total number of in-flight frames on ingress queues */
1335static u32 ingress_fq_count(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001336{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001337 struct dpaa2_eth_fq *fq;
1338 u32 fcnt = 0, bcnt = 0, total = 0;
1339 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001340
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001341 for (i = 0; i < priv->num_fqs; i++) {
1342 fq = &priv->fq[i];
1343 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1344 if (err) {
1345 netdev_warn(priv->net_dev, "query_fq_count failed");
1346 break;
1347 }
1348 total += fcnt;
1349 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001350
1351 return total;
1352}
1353
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001354static void wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001355{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001356 int retries = 10;
1357 u32 pending;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001358
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001359 do {
1360 pending = ingress_fq_count(priv);
1361 if (pending)
1362 msleep(100);
1363 } while (pending && --retries);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001364}
1365
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001366#define DPNI_TX_PENDING_VER_MAJOR 7
1367#define DPNI_TX_PENDING_VER_MINOR 13
1368static void wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
1369{
1370 union dpni_statistics stats;
1371 int retries = 10;
1372 int err;
1373
1374 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1375 DPNI_TX_PENDING_VER_MINOR) < 0)
1376 goto out;
1377
1378 do {
1379 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1380 &stats);
1381 if (err)
1382 goto out;
1383 if (stats.page_6.tx_pending_frames == 0)
1384 return;
1385 } while (--retries);
1386
1387out:
1388 msleep(500);
1389}
1390
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001391static int dpaa2_eth_stop(struct net_device *net_dev)
1392{
1393 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001394 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001395 int retries = 10;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001396
1397 netif_tx_stop_all_queues(net_dev);
1398 netif_carrier_off(net_dev);
1399
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001400 /* On dpni_disable(), the MC firmware will:
1401 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1402 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1403 * of all in flight Tx frames is finished (and corresponding Tx conf
1404 * frames are enqueued back to software)
1405 *
1406 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1407 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1408 * and Tx conf queues are consumed on NAPI poll.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001409 */
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001410 wait_for_egress_fq_empty(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001411
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001412 do {
1413 dpni_disable(priv->mc_io, 0, priv->mc_token);
1414 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1415 if (dpni_enabled)
1416 /* Allow the hardware some slack */
1417 msleep(100);
1418 } while (dpni_enabled && --retries);
1419 if (!retries) {
1420 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1421 /* Must go on and disable NAPI nonetheless, so we don't crash at
1422 * the next "ifconfig up"
1423 */
1424 }
1425
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001426 wait_for_ingress_fq_empty(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001427 disable_ch_napi(priv);
1428
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001429 /* Empty the buffer pool */
1430 drain_pool(priv);
1431
1432 return 0;
1433}
1434
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001435static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1436{
1437 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1438 struct device *dev = net_dev->dev.parent;
1439 int err;
1440
1441 err = eth_mac_addr(net_dev, addr);
1442 if (err < 0) {
1443 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1444 return err;
1445 }
1446
1447 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1448 net_dev->dev_addr);
1449 if (err) {
1450 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1451 return err;
1452 }
1453
1454 return 0;
1455}
1456
1457/** Fill in counters maintained by the GPP driver. These may be different from
1458 * the hardware counters obtained by ethtool.
1459 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001460static void dpaa2_eth_get_stats(struct net_device *net_dev,
1461 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001462{
1463 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1464 struct rtnl_link_stats64 *percpu_stats;
1465 u64 *cpustats;
1466 u64 *netstats = (u64 *)stats;
1467 int i, j;
1468 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1469
1470 for_each_possible_cpu(i) {
1471 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1472 cpustats = (u64 *)percpu_stats;
1473 for (j = 0; j < num; j++)
1474 netstats[j] += cpustats[j];
1475 }
1476}
1477
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001478/* Copy mac unicast addresses from @net_dev to @priv.
1479 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1480 */
1481static void add_uc_hw_addr(const struct net_device *net_dev,
1482 struct dpaa2_eth_priv *priv)
1483{
1484 struct netdev_hw_addr *ha;
1485 int err;
1486
1487 netdev_for_each_uc_addr(ha, net_dev) {
1488 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1489 ha->addr);
1490 if (err)
1491 netdev_warn(priv->net_dev,
1492 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1493 ha->addr, err);
1494 }
1495}
1496
1497/* Copy mac multicast addresses from @net_dev to @priv
1498 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1499 */
1500static void add_mc_hw_addr(const struct net_device *net_dev,
1501 struct dpaa2_eth_priv *priv)
1502{
1503 struct netdev_hw_addr *ha;
1504 int err;
1505
1506 netdev_for_each_mc_addr(ha, net_dev) {
1507 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1508 ha->addr);
1509 if (err)
1510 netdev_warn(priv->net_dev,
1511 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1512 ha->addr, err);
1513 }
1514}
1515
1516static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1517{
1518 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1519 int uc_count = netdev_uc_count(net_dev);
1520 int mc_count = netdev_mc_count(net_dev);
1521 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1522 u32 options = priv->dpni_attrs.options;
1523 u16 mc_token = priv->mc_token;
1524 struct fsl_mc_io *mc_io = priv->mc_io;
1525 int err;
1526
1527 /* Basic sanity checks; these probably indicate a misconfiguration */
1528 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1529 netdev_info(net_dev,
1530 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1531 max_mac);
1532
1533 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1534 if (uc_count > max_mac) {
1535 netdev_info(net_dev,
1536 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1537 uc_count, max_mac);
1538 goto force_promisc;
1539 }
1540 if (mc_count + uc_count > max_mac) {
1541 netdev_info(net_dev,
1542 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1543 uc_count + mc_count, max_mac);
1544 goto force_mc_promisc;
1545 }
1546
1547 /* Adjust promisc settings due to flag combinations */
1548 if (net_dev->flags & IFF_PROMISC)
1549 goto force_promisc;
1550 if (net_dev->flags & IFF_ALLMULTI) {
1551 /* First, rebuild unicast filtering table. This should be done
1552 * in promisc mode, in order to avoid frame loss while we
1553 * progressively add entries to the table.
1554 * We don't know whether we had been in promisc already, and
1555 * making an MC call to find out is expensive; so set uc promisc
1556 * nonetheless.
1557 */
1558 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1559 if (err)
1560 netdev_warn(net_dev, "Can't set uc promisc\n");
1561
1562 /* Actual uc table reconstruction. */
1563 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1564 if (err)
1565 netdev_warn(net_dev, "Can't clear uc filters\n");
1566 add_uc_hw_addr(net_dev, priv);
1567
1568 /* Finally, clear uc promisc and set mc promisc as requested. */
1569 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1570 if (err)
1571 netdev_warn(net_dev, "Can't clear uc promisc\n");
1572 goto force_mc_promisc;
1573 }
1574
1575 /* Neither unicast, nor multicast promisc will be on... eventually.
1576 * For now, rebuild mac filtering tables while forcing both of them on.
1577 */
1578 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1579 if (err)
1580 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1581 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1582 if (err)
1583 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1584
1585 /* Actual mac filtering tables reconstruction */
1586 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1587 if (err)
1588 netdev_warn(net_dev, "Can't clear mac filters\n");
1589 add_mc_hw_addr(net_dev, priv);
1590 add_uc_hw_addr(net_dev, priv);
1591
1592 /* Now we can clear both ucast and mcast promisc, without risking
1593 * to drop legitimate frames anymore.
1594 */
1595 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1596 if (err)
1597 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1598 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1599 if (err)
1600 netdev_warn(net_dev, "Can't clear mcast promisc\n");
1601
1602 return;
1603
1604force_promisc:
1605 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1606 if (err)
1607 netdev_warn(net_dev, "Can't set ucast promisc\n");
1608force_mc_promisc:
1609 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1610 if (err)
1611 netdev_warn(net_dev, "Can't set mcast promisc\n");
1612}
1613
1614static int dpaa2_eth_set_features(struct net_device *net_dev,
1615 netdev_features_t features)
1616{
1617 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1618 netdev_features_t changed = features ^ net_dev->features;
1619 bool enable;
1620 int err;
1621
1622 if (changed & NETIF_F_RXCSUM) {
1623 enable = !!(features & NETIF_F_RXCSUM);
1624 err = set_rx_csum(priv, enable);
1625 if (err)
1626 return err;
1627 }
1628
1629 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1630 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
1631 err = set_tx_csum(priv, enable);
1632 if (err)
1633 return err;
1634 }
1635
1636 return 0;
1637}
1638
Ioana Radulescu859f9982018-04-26 18:23:47 +08001639static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1640{
1641 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1642 struct hwtstamp_config config;
1643
1644 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
1645 return -EFAULT;
1646
1647 switch (config.tx_type) {
1648 case HWTSTAMP_TX_OFF:
1649 priv->tx_tstamp = false;
1650 break;
1651 case HWTSTAMP_TX_ON:
1652 priv->tx_tstamp = true;
1653 break;
1654 default:
1655 return -ERANGE;
1656 }
1657
1658 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
1659 priv->rx_tstamp = false;
1660 } else {
1661 priv->rx_tstamp = true;
1662 /* TS is set for all frame types, not only those requested */
1663 config.rx_filter = HWTSTAMP_FILTER_ALL;
1664 }
1665
1666 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
1667 -EFAULT : 0;
1668}
1669
1670static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1671{
1672 if (cmd == SIOCSHWTSTAMP)
1673 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
1674
1675 return -EINVAL;
1676}
1677
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001678static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
1679{
1680 int mfl, linear_mfl;
1681
1682 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1683 linear_mfl = DPAA2_ETH_RX_BUF_SIZE - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001684 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001685
1686 if (mfl > linear_mfl) {
1687 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
1688 linear_mfl - VLAN_ETH_HLEN);
1689 return false;
1690 }
1691
1692 return true;
1693}
1694
1695static int set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
1696{
1697 int mfl, err;
1698
1699 /* We enforce a maximum Rx frame length based on MTU only if we have
1700 * an XDP program attached (in order to avoid Rx S/G frames).
1701 * Otherwise, we accept all incoming frames as long as they are not
1702 * larger than maximum size supported in hardware
1703 */
1704 if (has_xdp)
1705 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1706 else
1707 mfl = DPAA2_ETH_MFL;
1708
1709 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
1710 if (err) {
1711 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
1712 return err;
1713 }
1714
1715 return 0;
1716}
1717
1718static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
1719{
1720 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1721 int err;
1722
1723 if (!priv->xdp_prog)
1724 goto out;
1725
1726 if (!xdp_mtu_valid(priv, new_mtu))
1727 return -EINVAL;
1728
1729 err = set_rx_mfl(priv, new_mtu, true);
1730 if (err)
1731 return err;
1732
1733out:
1734 dev->mtu = new_mtu;
1735 return 0;
1736}
1737
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001738static int update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
1739{
1740 struct dpni_buffer_layout buf_layout = {0};
1741 int err;
1742
1743 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
1744 DPNI_QUEUE_RX, &buf_layout);
1745 if (err) {
1746 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
1747 return err;
1748 }
1749
1750 /* Reserve extra headroom for XDP header size changes */
1751 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
1752 (has_xdp ? XDP_PACKET_HEADROOM : 0);
1753 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
1754 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
1755 DPNI_QUEUE_RX, &buf_layout);
1756 if (err) {
1757 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
1758 return err;
1759 }
1760
1761 return 0;
1762}
1763
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001764static int setup_xdp(struct net_device *dev, struct bpf_prog *prog)
1765{
1766 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1767 struct dpaa2_eth_channel *ch;
1768 struct bpf_prog *old;
1769 bool up, need_update;
1770 int i, err;
1771
1772 if (prog && !xdp_mtu_valid(priv, dev->mtu))
1773 return -EINVAL;
1774
1775 if (prog) {
1776 prog = bpf_prog_add(prog, priv->num_channels);
1777 if (IS_ERR(prog))
1778 return PTR_ERR(prog);
1779 }
1780
1781 up = netif_running(dev);
1782 need_update = (!!priv->xdp_prog != !!prog);
1783
1784 if (up)
1785 dpaa2_eth_stop(dev);
1786
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001787 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
1788 * Also, when switching between xdp/non-xdp modes we need to reconfigure
1789 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
1790 * so we are sure no old format buffers will be used from now on.
1791 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001792 if (need_update) {
1793 err = set_rx_mfl(priv, dev->mtu, !!prog);
1794 if (err)
1795 goto out_err;
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001796 err = update_rx_buffer_headroom(priv, !!prog);
1797 if (err)
1798 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001799 }
1800
1801 old = xchg(&priv->xdp_prog, prog);
1802 if (old)
1803 bpf_prog_put(old);
1804
1805 for (i = 0; i < priv->num_channels; i++) {
1806 ch = priv->channel[i];
1807 old = xchg(&ch->xdp.prog, prog);
1808 if (old)
1809 bpf_prog_put(old);
1810 }
1811
1812 if (up) {
1813 err = dpaa2_eth_open(dev);
1814 if (err)
1815 return err;
1816 }
1817
1818 return 0;
1819
1820out_err:
1821 if (prog)
1822 bpf_prog_sub(prog, priv->num_channels);
1823 if (up)
1824 dpaa2_eth_open(dev);
1825
1826 return err;
1827}
1828
1829static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1830{
1831 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1832
1833 switch (xdp->command) {
1834 case XDP_SETUP_PROG:
1835 return setup_xdp(dev, xdp->prog);
1836 case XDP_QUERY_PROG:
1837 xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0;
1838 break;
1839 default:
1840 return -EINVAL;
1841 }
1842
1843 return 0;
1844}
1845
Ioana Radulescud678be12019-03-01 17:47:24 +00001846static int dpaa2_eth_xdp_xmit_frame(struct net_device *net_dev,
1847 struct xdp_frame *xdpf)
1848{
1849 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1850 struct device *dev = net_dev->dev.parent;
1851 struct rtnl_link_stats64 *percpu_stats;
1852 struct dpaa2_eth_drv_stats *percpu_extras;
1853 unsigned int needed_headroom;
1854 struct dpaa2_eth_swa *swa;
1855 struct dpaa2_eth_fq *fq;
1856 struct dpaa2_fd fd;
1857 void *buffer_start, *aligned_start;
1858 dma_addr_t addr;
1859 int err, i;
1860
1861 /* We require a minimum headroom to be able to transmit the frame.
1862 * Otherwise return an error and let the original net_device handle it
1863 */
1864 needed_headroom = dpaa2_eth_needed_headroom(priv, NULL);
1865 if (xdpf->headroom < needed_headroom)
1866 return -EINVAL;
1867
1868 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1869 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1870
1871 /* Setup the FD fields */
1872 memset(&fd, 0, sizeof(fd));
1873
1874 /* Align FD address, if possible */
1875 buffer_start = xdpf->data - needed_headroom;
1876 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
1877 DPAA2_ETH_TX_BUF_ALIGN);
1878 if (aligned_start >= xdpf->data - xdpf->headroom)
1879 buffer_start = aligned_start;
1880
1881 swa = (struct dpaa2_eth_swa *)buffer_start;
1882 /* fill in necessary fields here */
1883 swa->type = DPAA2_ETH_SWA_XDP;
1884 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
1885 swa->xdp.xdpf = xdpf;
1886
1887 addr = dma_map_single(dev, buffer_start,
1888 swa->xdp.dma_size,
1889 DMA_BIDIRECTIONAL);
1890 if (unlikely(dma_mapping_error(dev, addr))) {
1891 percpu_stats->tx_dropped++;
1892 return -ENOMEM;
1893 }
1894
1895 dpaa2_fd_set_addr(&fd, addr);
1896 dpaa2_fd_set_offset(&fd, xdpf->data - buffer_start);
1897 dpaa2_fd_set_len(&fd, xdpf->len);
1898 dpaa2_fd_set_format(&fd, dpaa2_fd_single);
1899 dpaa2_fd_set_ctrl(&fd, FD_CTRL_PTA);
1900
Ioana Ciocoi Radulescu64447502019-03-20 14:11:04 +00001901 fq = &priv->fq[smp_processor_id() % dpaa2_eth_queue_count(priv)];
Ioana Radulescud678be12019-03-01 17:47:24 +00001902 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
1903 err = priv->enqueue(priv, fq, &fd, 0);
1904 if (err != -EBUSY)
1905 break;
1906 }
1907 percpu_extras->tx_portal_busy += i;
1908 if (unlikely(err < 0)) {
1909 percpu_stats->tx_errors++;
1910 /* let the Rx device handle the cleanup */
1911 return err;
1912 }
1913
1914 percpu_stats->tx_packets++;
1915 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fd);
1916
1917 return 0;
1918}
1919
1920static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
1921 struct xdp_frame **frames, u32 flags)
1922{
1923 int drops = 0;
1924 int i, err;
1925
1926 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1927 return -EINVAL;
1928
1929 if (!netif_running(net_dev))
1930 return -ENETDOWN;
1931
1932 for (i = 0; i < n; i++) {
1933 struct xdp_frame *xdpf = frames[i];
1934
1935 err = dpaa2_eth_xdp_xmit_frame(net_dev, xdpf);
1936 if (err) {
1937 xdp_return_frame_rx_napi(xdpf);
1938 drops++;
1939 }
1940 }
1941
1942 return n - drops;
1943}
1944
Ioana Radulescu06d5b172019-06-11 14:50:01 +03001945static int update_xps(struct dpaa2_eth_priv *priv)
1946{
1947 struct net_device *net_dev = priv->net_dev;
1948 struct cpumask xps_mask;
1949 struct dpaa2_eth_fq *fq;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001950 int i, num_queues, netdev_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03001951 int err = 0;
1952
1953 num_queues = dpaa2_eth_queue_count(priv);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001954 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03001955
1956 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
1957 * queues, so only process those
1958 */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001959 for (i = 0; i < netdev_queues; i++) {
1960 fq = &priv->fq[i % num_queues];
Ioana Radulescu06d5b172019-06-11 14:50:01 +03001961
1962 cpumask_clear(&xps_mask);
1963 cpumask_set_cpu(fq->target_cpu, &xps_mask);
1964
1965 err = netif_set_xps_queue(net_dev, &xps_mask, i);
1966 if (err) {
1967 netdev_warn_once(net_dev, "Error setting XPS queue\n");
1968 break;
1969 }
1970 }
1971
1972 return err;
1973}
1974
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001975static int dpaa2_eth_setup_tc(struct net_device *net_dev,
1976 enum tc_setup_type type, void *type_data)
1977{
1978 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1979 struct tc_mqprio_qopt *mqprio = type_data;
1980 u8 num_tc, num_queues;
1981 int i;
1982
1983 if (type != TC_SETUP_QDISC_MQPRIO)
1984 return -EINVAL;
1985
1986 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
1987 num_queues = dpaa2_eth_queue_count(priv);
1988 num_tc = mqprio->num_tc;
1989
1990 if (num_tc == net_dev->num_tc)
1991 return 0;
1992
1993 if (num_tc > dpaa2_eth_tc_count(priv)) {
1994 netdev_err(net_dev, "Max %d traffic classes supported\n",
1995 dpaa2_eth_tc_count(priv));
1996 return -EINVAL;
1997 }
1998
1999 if (!num_tc) {
2000 netdev_reset_tc(net_dev);
2001 netif_set_real_num_tx_queues(net_dev, num_queues);
2002 goto out;
2003 }
2004
2005 netdev_set_num_tc(net_dev, num_tc);
2006 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2007
2008 for (i = 0; i < num_tc; i++)
2009 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2010
2011out:
2012 update_xps(priv);
2013
2014 return 0;
2015}
2016
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002017static const struct net_device_ops dpaa2_eth_ops = {
2018 .ndo_open = dpaa2_eth_open,
2019 .ndo_start_xmit = dpaa2_eth_tx,
2020 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002021 .ndo_set_mac_address = dpaa2_eth_set_addr,
2022 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002023 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2024 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08002025 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002026 .ndo_change_mtu = dpaa2_eth_change_mtu,
2027 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescud678be12019-03-01 17:47:24 +00002028 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002029 .ndo_setup_tc = dpaa2_eth_setup_tc,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002030};
2031
2032static void cdan_cb(struct dpaa2_io_notification_ctx *ctx)
2033{
2034 struct dpaa2_eth_channel *ch;
2035
2036 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002037
2038 /* Update NAPI statistics */
2039 ch->stats.cdan++;
2040
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002041 napi_schedule_irqoff(&ch->napi);
2042}
2043
2044/* Allocate and configure a DPCON object */
2045static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv)
2046{
2047 struct fsl_mc_device *dpcon;
2048 struct device *dev = priv->net_dev->dev.parent;
2049 struct dpcon_attr attrs;
2050 int err;
2051
2052 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2053 FSL_MC_POOL_DPCON, &dpcon);
2054 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002055 if (err == -ENXIO)
2056 err = -EPROBE_DEFER;
2057 else
2058 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2059 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002060 }
2061
2062 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2063 if (err) {
2064 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002065 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002066 }
2067
2068 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2069 if (err) {
2070 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002071 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002072 }
2073
2074 err = dpcon_get_attributes(priv->mc_io, 0, dpcon->mc_handle, &attrs);
2075 if (err) {
2076 dev_err(dev, "dpcon_get_attributes() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002077 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002078 }
2079
2080 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2081 if (err) {
2082 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002083 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002084 }
2085
2086 return dpcon;
2087
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002088close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002089 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002090free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002091 fsl_mc_object_free(dpcon);
2092
2093 return NULL;
2094}
2095
2096static void free_dpcon(struct dpaa2_eth_priv *priv,
2097 struct fsl_mc_device *dpcon)
2098{
2099 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2100 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2101 fsl_mc_object_free(dpcon);
2102}
2103
2104static struct dpaa2_eth_channel *
2105alloc_channel(struct dpaa2_eth_priv *priv)
2106{
2107 struct dpaa2_eth_channel *channel;
2108 struct dpcon_attr attr;
2109 struct device *dev = priv->net_dev->dev.parent;
2110 int err;
2111
2112 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2113 if (!channel)
2114 return NULL;
2115
2116 channel->dpcon = setup_dpcon(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002117 if (IS_ERR_OR_NULL(channel->dpcon)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002118 err = PTR_ERR_OR_ZERO(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002119 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002120 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002121
2122 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2123 &attr);
2124 if (err) {
2125 dev_err(dev, "dpcon_get_attributes() failed\n");
2126 goto err_get_attr;
2127 }
2128
2129 channel->dpcon_id = attr.id;
2130 channel->ch_id = attr.qbman_ch_id;
2131 channel->priv = priv;
2132
2133 return channel;
2134
2135err_get_attr:
2136 free_dpcon(priv, channel->dpcon);
2137err_setup:
2138 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002139 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002140}
2141
2142static void free_channel(struct dpaa2_eth_priv *priv,
2143 struct dpaa2_eth_channel *channel)
2144{
2145 free_dpcon(priv, channel->dpcon);
2146 kfree(channel);
2147}
2148
2149/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2150 * and register data availability notifications
2151 */
2152static int setup_dpio(struct dpaa2_eth_priv *priv)
2153{
2154 struct dpaa2_io_notification_ctx *nctx;
2155 struct dpaa2_eth_channel *channel;
2156 struct dpcon_notification_cfg dpcon_notif_cfg;
2157 struct device *dev = priv->net_dev->dev.parent;
2158 int i, err;
2159
2160 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2161 * many cores as possible, so we need one channel for each core
2162 * (unless there's fewer queues than cores, in which case the extra
2163 * channels would be wasted).
2164 * Allocate one channel per core and register it to the core's
2165 * affine DPIO. If not enough channels are available for all cores
2166 * or if some cores don't have an affine DPIO, there will be no
2167 * ingress frame processing on those cores.
2168 */
2169 cpumask_clear(&priv->dpio_cpumask);
2170 for_each_online_cpu(i) {
2171 /* Try to allocate a channel */
2172 channel = alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002173 if (IS_ERR_OR_NULL(channel)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002174 err = PTR_ERR_OR_ZERO(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002175 if (err != -EPROBE_DEFER)
2176 dev_info(dev,
2177 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002178 goto err_alloc_ch;
2179 }
2180
2181 priv->channel[priv->num_channels] = channel;
2182
2183 nctx = &channel->nctx;
2184 nctx->is_cdan = 1;
2185 nctx->cb = cdan_cb;
2186 nctx->id = channel->ch_id;
2187 nctx->desired_cpu = i;
2188
2189 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06002190 channel->dpio = dpaa2_io_service_select(i);
Ioana Ciornei47441f72018-12-10 16:50:19 +00002191 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002192 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002193 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002194 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002195 * none available for next cores either. Signal we want
2196 * to retry later, in case the DPIO devices weren't
2197 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002198 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002199 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002200 goto err_service_reg;
2201 }
2202
2203 /* Register DPCON notification with MC */
2204 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2205 dpcon_notif_cfg.priority = 0;
2206 dpcon_notif_cfg.user_ctx = nctx->qman64;
2207 err = dpcon_set_notification(priv->mc_io, 0,
2208 channel->dpcon->mc_handle,
2209 &dpcon_notif_cfg);
2210 if (err) {
2211 dev_err(dev, "dpcon_set_notification failed()\n");
2212 goto err_set_cdan;
2213 }
2214
2215 /* If we managed to allocate a channel and also found an affine
2216 * DPIO for this core, add it to the final mask
2217 */
2218 cpumask_set_cpu(i, &priv->dpio_cpumask);
2219 priv->num_channels++;
2220
2221 /* Stop if we already have enough channels to accommodate all
2222 * RX and TX conf queues
2223 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00002224 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002225 break;
2226 }
2227
2228 return 0;
2229
2230err_set_cdan:
Ioana Ciornei47441f72018-12-10 16:50:19 +00002231 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002232err_service_reg:
2233 free_channel(priv, channel);
2234err_alloc_ch:
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002235 if (err == -EPROBE_DEFER)
2236 return err;
2237
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002238 if (cpumask_empty(&priv->dpio_cpumask)) {
2239 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002240 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002241 }
2242
2243 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2244 cpumask_pr_args(&priv->dpio_cpumask));
2245
2246 return 0;
2247}
2248
2249static void free_dpio(struct dpaa2_eth_priv *priv)
2250{
Ioana Ciornei47441f72018-12-10 16:50:19 +00002251 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002252 struct dpaa2_eth_channel *ch;
Ioana Ciornei47441f72018-12-10 16:50:19 +00002253 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002254
2255 /* deregister CDAN notifications and free channels */
2256 for (i = 0; i < priv->num_channels; i++) {
2257 ch = priv->channel[i];
Ioana Ciornei47441f72018-12-10 16:50:19 +00002258 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002259 free_channel(priv, ch);
2260 }
2261}
2262
2263static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv,
2264 int cpu)
2265{
2266 struct device *dev = priv->net_dev->dev.parent;
2267 int i;
2268
2269 for (i = 0; i < priv->num_channels; i++)
2270 if (priv->channel[i]->nctx.desired_cpu == cpu)
2271 return priv->channel[i];
2272
2273 /* We should never get here. Issue a warning and return
2274 * the first channel, because it's still better than nothing
2275 */
2276 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2277
2278 return priv->channel[0];
2279}
2280
2281static void set_fq_affinity(struct dpaa2_eth_priv *priv)
2282{
2283 struct device *dev = priv->net_dev->dev.parent;
2284 struct dpaa2_eth_fq *fq;
2285 int rx_cpu, txc_cpu;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002286 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002287
2288 /* For each FQ, pick one channel/CPU to deliver frames to.
2289 * This may well change at runtime, either through irqbalance or
2290 * through direct user intervention.
2291 */
2292 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2293
2294 for (i = 0; i < priv->num_fqs; i++) {
2295 fq = &priv->fq[i];
2296 switch (fq->type) {
2297 case DPAA2_RX_FQ:
2298 fq->target_cpu = rx_cpu;
2299 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2300 if (rx_cpu >= nr_cpu_ids)
2301 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2302 break;
2303 case DPAA2_TX_CONF_FQ:
2304 fq->target_cpu = txc_cpu;
2305 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2306 if (txc_cpu >= nr_cpu_ids)
2307 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2308 break;
2309 default:
2310 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2311 }
2312 fq->channel = get_affine_channel(priv, fq->target_cpu);
2313 }
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002314
2315 update_xps(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002316}
2317
2318static void setup_fqs(struct dpaa2_eth_priv *priv)
2319{
2320 int i;
2321
2322 /* We have one TxConf FQ per Tx flow.
2323 * The number of Tx and Rx queues is the same.
2324 * Tx queues come first in the fq array.
2325 */
2326 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2327 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2328 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2329 priv->fq[priv->num_fqs++].flowid = (u16)i;
2330 }
2331
2332 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2333 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2334 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2335 priv->fq[priv->num_fqs++].flowid = (u16)i;
2336 }
2337
2338 /* For each FQ, decide on which core to process incoming frames */
2339 set_fq_affinity(priv);
2340}
2341
2342/* Allocate and configure one buffer pool for each interface */
2343static int setup_dpbp(struct dpaa2_eth_priv *priv)
2344{
2345 int err;
2346 struct fsl_mc_device *dpbp_dev;
2347 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002348 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002349
2350 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2351 &dpbp_dev);
2352 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002353 if (err == -ENXIO)
2354 err = -EPROBE_DEFER;
2355 else
2356 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002357 return err;
2358 }
2359
2360 priv->dpbp_dev = dpbp_dev;
2361
2362 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2363 &dpbp_dev->mc_handle);
2364 if (err) {
2365 dev_err(dev, "dpbp_open() failed\n");
2366 goto err_open;
2367 }
2368
Ioana Radulescud00defe2017-06-06 10:00:32 -05002369 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2370 if (err) {
2371 dev_err(dev, "dpbp_reset() failed\n");
2372 goto err_reset;
2373 }
2374
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002375 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2376 if (err) {
2377 dev_err(dev, "dpbp_enable() failed\n");
2378 goto err_enable;
2379 }
2380
2381 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002382 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002383 if (err) {
2384 dev_err(dev, "dpbp_get_attributes() failed\n");
2385 goto err_get_attr;
2386 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002387 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002388
2389 return 0;
2390
2391err_get_attr:
2392 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2393err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002394err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002395 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2396err_open:
2397 fsl_mc_object_free(dpbp_dev);
2398
2399 return err;
2400}
2401
2402static void free_dpbp(struct dpaa2_eth_priv *priv)
2403{
2404 drain_pool(priv);
2405 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2406 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2407 fsl_mc_object_free(priv->dpbp_dev);
2408}
2409
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002410static int set_buffer_layout(struct dpaa2_eth_priv *priv)
2411{
2412 struct device *dev = priv->net_dev->dev.parent;
2413 struct dpni_buffer_layout buf_layout = {0};
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002414 u16 rx_buf_align;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002415 int err;
2416
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002417 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2418 * version, this number is not always provided correctly on rev1.
2419 * We need to check for both alternatives in this situation.
2420 */
2421 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2422 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002423 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002424 else
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002425 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002426
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002427 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002428 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002429 buf_layout.pass_timestamp = true;
2430 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2431 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002432 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2433 DPNI_QUEUE_TX, &buf_layout);
2434 if (err) {
2435 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2436 return err;
2437 }
2438
2439 /* tx-confirm buffer */
Ioana Radulescu859f9982018-04-26 18:23:47 +08002440 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002441 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2442 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2443 if (err) {
2444 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2445 return err;
2446 }
2447
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002448 /* Now that we've set our tx buffer layout, retrieve the minimum
2449 * required tx data offset.
2450 */
2451 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2452 &priv->tx_data_offset);
2453 if (err) {
2454 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2455 return err;
2456 }
2457
2458 if ((priv->tx_data_offset % 64) != 0)
2459 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2460 priv->tx_data_offset);
2461
2462 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06002463 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002464 buf_layout.pass_parser_result = true;
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002465 buf_layout.data_align = rx_buf_align;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002466 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2467 buf_layout.private_data_size = 0;
2468 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2469 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2470 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08002471 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2472 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002473 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2474 DPNI_QUEUE_RX, &buf_layout);
2475 if (err) {
2476 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2477 return err;
2478 }
2479
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002480 return 0;
2481}
2482
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002483#define DPNI_ENQUEUE_FQID_VER_MAJOR 7
2484#define DPNI_ENQUEUE_FQID_VER_MINOR 9
2485
2486static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2487 struct dpaa2_eth_fq *fq,
2488 struct dpaa2_fd *fd, u8 prio)
2489{
2490 return dpaa2_io_service_enqueue_qd(fq->channel->dpio,
2491 priv->tx_qdid, prio,
2492 fq->tx_qdbin, fd);
2493}
2494
2495static inline int dpaa2_eth_enqueue_fq(struct dpaa2_eth_priv *priv,
2496 struct dpaa2_eth_fq *fq,
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002497 struct dpaa2_fd *fd, u8 prio)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002498{
2499 return dpaa2_io_service_enqueue_fq(fq->channel->dpio,
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002500 fq->tx_fqid[prio], fd);
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002501}
2502
2503static void set_enqueue_mode(struct dpaa2_eth_priv *priv)
2504{
2505 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2506 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2507 priv->enqueue = dpaa2_eth_enqueue_qd;
2508 else
2509 priv->enqueue = dpaa2_eth_enqueue_fq;
2510}
2511
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03002512static int set_pause(struct dpaa2_eth_priv *priv)
2513{
2514 struct device *dev = priv->net_dev->dev.parent;
2515 struct dpni_link_cfg link_cfg = {0};
2516 int err;
2517
2518 /* Get the default link options so we don't override other flags */
2519 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2520 if (err) {
2521 dev_err(dev, "dpni_get_link_cfg() failed\n");
2522 return err;
2523 }
2524
2525 /* By default, enable both Rx and Tx pause frames */
2526 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
2527 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
2528 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2529 if (err) {
2530 dev_err(dev, "dpni_set_link_cfg() failed\n");
2531 return err;
2532 }
2533
2534 priv->link_state.options = link_cfg.options;
2535
2536 return 0;
2537}
2538
Ioana Radulescua690af4f2019-10-16 10:36:23 +03002539static void update_tx_fqids(struct dpaa2_eth_priv *priv)
2540{
2541 struct dpni_queue_id qid = {0};
2542 struct dpaa2_eth_fq *fq;
2543 struct dpni_queue queue;
2544 int i, j, err;
2545
2546 /* We only use Tx FQIDs for FQID-based enqueue, so check
2547 * if DPNI version supports it before updating FQIDs
2548 */
2549 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2550 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2551 return;
2552
2553 for (i = 0; i < priv->num_fqs; i++) {
2554 fq = &priv->fq[i];
2555 if (fq->type != DPAA2_TX_CONF_FQ)
2556 continue;
2557 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2558 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2559 DPNI_QUEUE_TX, j, fq->flowid,
2560 &queue, &qid);
2561 if (err)
2562 goto out_err;
2563
2564 fq->tx_fqid[j] = qid.fqid;
2565 if (fq->tx_fqid[j] == 0)
2566 goto out_err;
2567 }
2568 }
2569
2570 priv->enqueue = dpaa2_eth_enqueue_fq;
2571
2572 return;
2573
2574out_err:
2575 netdev_info(priv->net_dev,
2576 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
2577 priv->enqueue = dpaa2_eth_enqueue_qd;
2578}
2579
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002580/* Configure the DPNI object this interface is associated with */
2581static int setup_dpni(struct fsl_mc_device *ls_dev)
2582{
2583 struct device *dev = &ls_dev->dev;
2584 struct dpaa2_eth_priv *priv;
2585 struct net_device *net_dev;
2586 int err;
2587
2588 net_dev = dev_get_drvdata(dev);
2589 priv = netdev_priv(net_dev);
2590
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002591 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05002592 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002593 if (err) {
2594 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002595 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002596 }
2597
Ioana Radulescu311cffa2018-03-23 08:44:09 -05002598 /* Check if we can work with this DPNI object */
2599 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
2600 &priv->dpni_ver_minor);
2601 if (err) {
2602 dev_err(dev, "dpni_get_api_version() failed\n");
2603 goto close;
2604 }
2605 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
2606 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
2607 priv->dpni_ver_major, priv->dpni_ver_minor,
2608 DPNI_VER_MAJOR, DPNI_VER_MINOR);
2609 err = -ENOTSUPP;
2610 goto close;
2611 }
2612
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002613 ls_dev->mc_io = priv->mc_io;
2614 ls_dev->mc_handle = priv->mc_token;
2615
2616 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2617 if (err) {
2618 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002619 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002620 }
2621
2622 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
2623 &priv->dpni_attrs);
2624 if (err) {
2625 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002626 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002627 }
2628
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002629 err = set_buffer_layout(priv);
2630 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002631 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002632
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002633 set_enqueue_mode(priv);
2634
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03002635 /* Enable pause frame support */
2636 if (dpaa2_eth_has_pause_support(priv)) {
2637 err = set_pause(priv);
2638 if (err)
2639 goto close;
2640 }
2641
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002642 priv->cls_rules = devm_kzalloc(dev, sizeof(struct dpaa2_eth_cls_rule) *
2643 dpaa2_eth_fs_count(priv), GFP_KERNEL);
2644 if (!priv->cls_rules)
2645 goto close;
2646
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002647 return 0;
2648
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002649close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002650 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002651
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002652 return err;
2653}
2654
2655static void free_dpni(struct dpaa2_eth_priv *priv)
2656{
2657 int err;
2658
2659 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2660 if (err)
2661 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
2662 err);
2663
2664 dpni_close(priv->mc_io, 0, priv->mc_token);
2665}
2666
2667static int setup_rx_flow(struct dpaa2_eth_priv *priv,
2668 struct dpaa2_eth_fq *fq)
2669{
2670 struct device *dev = priv->net_dev->dev.parent;
2671 struct dpni_queue queue;
2672 struct dpni_queue_id qid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002673 int err;
2674
2675 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2676 DPNI_QUEUE_RX, 0, fq->flowid, &queue, &qid);
2677 if (err) {
2678 dev_err(dev, "dpni_get_queue(RX) failed\n");
2679 return err;
2680 }
2681
2682 fq->fqid = qid.fqid;
2683
2684 queue.destination.id = fq->channel->dpcon_id;
2685 queue.destination.type = DPNI_DEST_DPCON;
2686 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002687 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002688 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2689 DPNI_QUEUE_RX, 0, fq->flowid,
Ioana Radulescu16fa1cf2019-05-23 17:38:22 +03002690 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002691 &queue);
2692 if (err) {
2693 dev_err(dev, "dpni_set_queue(RX) failed\n");
2694 return err;
2695 }
2696
Ioana Radulescud678be12019-03-01 17:47:24 +00002697 /* xdp_rxq setup */
2698 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
2699 fq->flowid);
2700 if (err) {
2701 dev_err(dev, "xdp_rxq_info_reg failed\n");
2702 return err;
2703 }
2704
2705 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
2706 MEM_TYPE_PAGE_ORDER0, NULL);
2707 if (err) {
2708 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
2709 return err;
2710 }
2711
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002712 return 0;
2713}
2714
2715static int setup_tx_flow(struct dpaa2_eth_priv *priv,
2716 struct dpaa2_eth_fq *fq)
2717{
2718 struct device *dev = priv->net_dev->dev.parent;
2719 struct dpni_queue queue;
2720 struct dpni_queue_id qid;
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002721 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002722
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002723 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
2724 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2725 DPNI_QUEUE_TX, i, fq->flowid,
2726 &queue, &qid);
2727 if (err) {
2728 dev_err(dev, "dpni_get_queue(TX) failed\n");
2729 return err;
2730 }
2731 fq->tx_fqid[i] = qid.fqid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002732 }
2733
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002734 /* All Tx queues belonging to the same flowid have the same qdbin */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002735 fq->tx_qdbin = qid.qdbin;
2736
2737 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2738 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2739 &queue, &qid);
2740 if (err) {
2741 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
2742 return err;
2743 }
2744
2745 fq->fqid = qid.fqid;
2746
2747 queue.destination.id = fq->channel->dpcon_id;
2748 queue.destination.type = DPNI_DEST_DPCON;
2749 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002750 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002751 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2752 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2753 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2754 &queue);
2755 if (err) {
2756 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
2757 return err;
2758 }
2759
2760 return 0;
2761}
2762
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002763/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002764static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002765 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002766 /* L2 header */
2767 .rxnfc_field = RXH_L2DA,
2768 .cls_prot = NET_PROT_ETH,
2769 .cls_field = NH_FLD_ETH_DA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002770 .id = DPAA2_ETH_DIST_ETHDST,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002771 .size = 6,
2772 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002773 .cls_prot = NET_PROT_ETH,
2774 .cls_field = NH_FLD_ETH_SA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002775 .id = DPAA2_ETH_DIST_ETHSRC,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002776 .size = 6,
2777 }, {
2778 /* This is the last ethertype field parsed:
2779 * depending on frame format, it can be the MAC ethertype
2780 * or the VLAN etype.
2781 */
2782 .cls_prot = NET_PROT_ETH,
2783 .cls_field = NH_FLD_ETH_TYPE,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002784 .id = DPAA2_ETH_DIST_ETHTYPE,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002785 .size = 2,
2786 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002787 /* VLAN header */
2788 .rxnfc_field = RXH_VLAN,
2789 .cls_prot = NET_PROT_VLAN,
2790 .cls_field = NH_FLD_VLAN_TCI,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002791 .id = DPAA2_ETH_DIST_VLAN,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002792 .size = 2,
2793 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002794 /* IP header */
2795 .rxnfc_field = RXH_IP_SRC,
2796 .cls_prot = NET_PROT_IP,
2797 .cls_field = NH_FLD_IP_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002798 .id = DPAA2_ETH_DIST_IPSRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002799 .size = 4,
2800 }, {
2801 .rxnfc_field = RXH_IP_DST,
2802 .cls_prot = NET_PROT_IP,
2803 .cls_field = NH_FLD_IP_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002804 .id = DPAA2_ETH_DIST_IPDST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002805 .size = 4,
2806 }, {
2807 .rxnfc_field = RXH_L3_PROTO,
2808 .cls_prot = NET_PROT_IP,
2809 .cls_field = NH_FLD_IP_PROTO,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002810 .id = DPAA2_ETH_DIST_IPPROTO,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002811 .size = 1,
2812 }, {
2813 /* Using UDP ports, this is functionally equivalent to raw
2814 * byte pairs from L4 header.
2815 */
2816 .rxnfc_field = RXH_L4_B_0_1,
2817 .cls_prot = NET_PROT_UDP,
2818 .cls_field = NH_FLD_UDP_PORT_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002819 .id = DPAA2_ETH_DIST_L4SRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002820 .size = 2,
2821 }, {
2822 .rxnfc_field = RXH_L4_B_2_3,
2823 .cls_prot = NET_PROT_UDP,
2824 .cls_field = NH_FLD_UDP_PORT_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00002825 .id = DPAA2_ETH_DIST_L4DST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002826 .size = 2,
2827 },
2828};
2829
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002830/* Configure the Rx hash key using the legacy API */
2831static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2832{
2833 struct device *dev = priv->net_dev->dev.parent;
2834 struct dpni_rx_tc_dist_cfg dist_cfg;
2835 int err;
2836
2837 memset(&dist_cfg, 0, sizeof(dist_cfg));
2838
2839 dist_cfg.key_cfg_iova = key;
2840 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2841 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
2842
2843 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg);
2844 if (err)
2845 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
2846
2847 return err;
2848}
2849
2850/* Configure the Rx hash key using the new API */
2851static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2852{
2853 struct device *dev = priv->net_dev->dev.parent;
2854 struct dpni_rx_dist_cfg dist_cfg;
2855 int err;
2856
2857 memset(&dist_cfg, 0, sizeof(dist_cfg));
2858
2859 dist_cfg.key_cfg_iova = key;
2860 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2861 dist_cfg.enable = 1;
2862
2863 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2864 if (err)
2865 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
2866
2867 return err;
2868}
2869
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002870/* Configure the Rx flow classification key */
2871static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2872{
2873 struct device *dev = priv->net_dev->dev.parent;
2874 struct dpni_rx_dist_cfg dist_cfg;
2875 int err;
2876
2877 memset(&dist_cfg, 0, sizeof(dist_cfg));
2878
2879 dist_cfg.key_cfg_iova = key;
2880 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2881 dist_cfg.enable = 1;
2882
2883 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2884 if (err)
2885 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
2886
2887 return err;
2888}
2889
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002890/* Size of the Rx flow classification key */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002891int dpaa2_eth_cls_key_size(u64 fields)
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002892{
2893 int i, size = 0;
2894
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002895 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2896 if (!(fields & dist_fields[i].id))
2897 continue;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002898 size += dist_fields[i].size;
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002899 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002900
2901 return size;
2902}
2903
2904/* Offset of header field in Rx classification key */
2905int dpaa2_eth_cls_fld_off(int prot, int field)
2906{
2907 int i, off = 0;
2908
2909 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2910 if (dist_fields[i].cls_prot == prot &&
2911 dist_fields[i].cls_field == field)
2912 return off;
2913 off += dist_fields[i].size;
2914 }
2915
2916 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
2917 return 0;
2918}
2919
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002920/* Prune unused fields from the classification rule.
2921 * Used when masking is not supported
2922 */
2923void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
2924{
2925 int off = 0, new_off = 0;
2926 int i, size;
2927
2928 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2929 size = dist_fields[i].size;
2930 if (dist_fields[i].id & fields) {
2931 memcpy(key_mem + new_off, key_mem + off, size);
2932 new_off += size;
2933 }
2934 off += size;
2935 }
2936}
2937
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002938/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002939 * flags is a combination of RXH_ bits
2940 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00002941static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
2942 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002943{
2944 struct device *dev = net_dev->dev.parent;
2945 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2946 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00002947 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002948 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002949 u8 *dma_mem;
2950 int i;
2951 int err = 0;
2952
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002953 memset(&cls_cfg, 0, sizeof(cls_cfg));
2954
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002955 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002956 struct dpkg_extract *key =
2957 &cls_cfg.extracts[cls_cfg.num_extracts];
2958
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002959 /* For both Rx hashing and classification keys
2960 * we set only the selected fields.
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002961 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00002962 if (!(flags & dist_fields[i].id))
2963 continue;
2964 if (type == DPAA2_ETH_RX_DIST_HASH)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002965 rx_hash_fields |= dist_fields[i].rxnfc_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002966
2967 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
2968 dev_err(dev, "error adding key extraction rule, too many rules?\n");
2969 return -E2BIG;
2970 }
2971
2972 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002973 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002974 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03002975 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002976 cls_cfg.num_extracts++;
2977 }
2978
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05002979 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002980 if (!dma_mem)
2981 return -ENOMEM;
2982
2983 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
2984 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05002985 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002986 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002987 }
2988
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002989 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002990 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
2991 DMA_TO_DEVICE);
2992 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002993 dev_err(dev, "DMA mapping failed\n");
2994 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03002995 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002996 }
2997
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03002998 if (type == DPAA2_ETH_RX_DIST_HASH) {
2999 if (dpaa2_eth_has_legacy_dist(priv))
3000 err = config_legacy_hash_key(priv, key_iova);
3001 else
3002 err = config_hash_key(priv, key_iova);
3003 } else {
3004 err = config_cls_key(priv, key_iova);
3005 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003006
3007 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3008 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003009 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003010 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003011
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003012free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003013 kfree(dma_mem);
3014 return err;
3015}
3016
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003017int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3018{
3019 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003020 u64 key = 0;
3021 int i;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003022
3023 if (!dpaa2_eth_hash_enabled(priv))
3024 return -EOPNOTSUPP;
3025
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003026 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3027 if (dist_fields[i].rxnfc_field & flags)
3028 key |= dist_fields[i].id;
3029
3030 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003031}
3032
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003033int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3034{
3035 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3036}
3037
3038static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003039{
3040 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003041 int err;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003042
3043 /* Check if we actually support Rx flow classification */
3044 if (dpaa2_eth_has_legacy_dist(priv)) {
3045 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3046 return -EOPNOTSUPP;
3047 }
3048
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003049 if (!dpaa2_eth_fs_enabled(priv)) {
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003050 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3051 return -EOPNOTSUPP;
3052 }
3053
3054 if (!dpaa2_eth_hash_enabled(priv)) {
3055 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3056 return -EOPNOTSUPP;
3057 }
3058
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003059 /* If there is no support for masking in the classification table,
3060 * we don't set a default key, as it will depend on the rules
3061 * added by the user at runtime.
3062 */
3063 if (!dpaa2_eth_fs_mask_enabled(priv))
3064 goto out;
3065
3066 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003067 if (err)
3068 return err;
3069
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003070out:
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003071 priv->rx_cls_enabled = 1;
3072
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003073 return 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003074}
3075
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003076/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3077 * frame queues and channels
3078 */
3079static int bind_dpni(struct dpaa2_eth_priv *priv)
3080{
3081 struct net_device *net_dev = priv->net_dev;
3082 struct device *dev = net_dev->dev.parent;
3083 struct dpni_pools_cfg pools_params;
3084 struct dpni_error_cfg err_cfg;
3085 int err = 0;
3086 int i;
3087
3088 pools_params.num_dpbp = 1;
3089 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3090 pools_params.pools[0].backup_pool = 0;
3091 pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE;
3092 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3093 if (err) {
3094 dev_err(dev, "dpni_set_pools() failed\n");
3095 return err;
3096 }
3097
Ioana Radulescu227686b2018-07-27 09:12:59 -05003098 /* have the interface implicitly distribute traffic based on
3099 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003100 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05003101 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003102 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05003103 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003104
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003105 /* Configure the flow classification key; it includes all
3106 * supported header fields and cannot be modified at runtime
3107 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003108 err = dpaa2_eth_set_default_cls(priv);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003109 if (err && err != -EOPNOTSUPP)
3110 dev_err(dev, "Failed to configure Rx classification key\n");
3111
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003112 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05003113 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003114 err_cfg.set_frame_annotation = 1;
3115 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3116 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3117 &err_cfg);
3118 if (err) {
3119 dev_err(dev, "dpni_set_errors_behavior failed\n");
3120 return err;
3121 }
3122
3123 /* Configure Rx and Tx conf queues to generate CDANs */
3124 for (i = 0; i < priv->num_fqs; i++) {
3125 switch (priv->fq[i].type) {
3126 case DPAA2_RX_FQ:
3127 err = setup_rx_flow(priv, &priv->fq[i]);
3128 break;
3129 case DPAA2_TX_CONF_FQ:
3130 err = setup_tx_flow(priv, &priv->fq[i]);
3131 break;
3132 default:
3133 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3134 return -EINVAL;
3135 }
3136 if (err)
3137 return err;
3138 }
3139
3140 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3141 DPNI_QUEUE_TX, &priv->tx_qdid);
3142 if (err) {
3143 dev_err(dev, "dpni_get_qdid() failed\n");
3144 return err;
3145 }
3146
3147 return 0;
3148}
3149
3150/* Allocate rings for storing incoming frame descriptors */
3151static int alloc_rings(struct dpaa2_eth_priv *priv)
3152{
3153 struct net_device *net_dev = priv->net_dev;
3154 struct device *dev = net_dev->dev.parent;
3155 int i;
3156
3157 for (i = 0; i < priv->num_channels; i++) {
3158 priv->channel[i]->store =
3159 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3160 if (!priv->channel[i]->store) {
3161 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3162 goto err_ring;
3163 }
3164 }
3165
3166 return 0;
3167
3168err_ring:
3169 for (i = 0; i < priv->num_channels; i++) {
3170 if (!priv->channel[i]->store)
3171 break;
3172 dpaa2_io_store_destroy(priv->channel[i]->store);
3173 }
3174
3175 return -ENOMEM;
3176}
3177
3178static void free_rings(struct dpaa2_eth_priv *priv)
3179{
3180 int i;
3181
3182 for (i = 0; i < priv->num_channels; i++)
3183 dpaa2_io_store_destroy(priv->channel[i]->store);
3184}
3185
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003186static int set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003187{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003188 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003189 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003190 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003191 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003192
3193 /* Get firmware address, if any */
3194 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3195 if (err) {
3196 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3197 return err;
3198 }
3199
3200 /* Get DPNI attributes address, if any */
3201 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3202 dpni_mac_addr);
3203 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003204 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003205 return err;
3206 }
3207
3208 /* First check if firmware has any address configured by bootloader */
3209 if (!is_zero_ether_addr(mac_addr)) {
3210 /* If the DPMAC addr != DPNI addr, update it */
3211 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
3212 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
3213 priv->mc_token,
3214 mac_addr);
3215 if (err) {
3216 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3217 return err;
3218 }
3219 }
3220 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
3221 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003222 /* No MAC address configured, fill in net_dev->dev_addr
3223 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003224 */
3225 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003226 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
3227
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003228 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3229 net_dev->dev_addr);
3230 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003231 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003232 return err;
3233 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003234
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003235 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
3236 * practical purposes, this will be our "permanent" mac address,
3237 * at least until the next reboot. This move will also permit
3238 * register_netdevice() to properly fill up net_dev->perm_addr.
3239 */
3240 net_dev->addr_assign_type = NET_ADDR_PERM;
3241 } else {
3242 /* NET_ADDR_PERM is default, all we have to do is
3243 * fill in the device addr.
3244 */
3245 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
3246 }
3247
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003248 return 0;
3249}
3250
3251static int netdev_init(struct net_device *net_dev)
3252{
3253 struct device *dev = net_dev->dev.parent;
3254 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003255 u32 options = priv->dpni_attrs.options;
3256 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003257 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003258 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003259 int err;
3260
3261 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003262 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003263
3264 err = set_mac_addr(priv);
3265 if (err)
3266 return err;
3267
3268 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003269 eth_broadcast_addr(bcast_addr);
3270 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3271 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003272 dev_err(dev, "dpni_add_mac_addr() failed\n");
3273 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003274 }
3275
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05003276 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003277 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05003278 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05003279 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05003280 if (err) {
3281 dev_err(dev, "dpni_set_max_frame_length() failed\n");
3282 return err;
3283 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003284
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003285 /* Set actual number of queues in the net device */
3286 num_queues = dpaa2_eth_queue_count(priv);
3287 err = netif_set_real_num_tx_queues(net_dev, num_queues);
3288 if (err) {
3289 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
3290 return err;
3291 }
3292 err = netif_set_real_num_rx_queues(net_dev, num_queues);
3293 if (err) {
3294 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
3295 return err;
3296 }
3297
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003298 /* Capabilities listing */
3299 supported |= IFF_LIVE_ADDR_CHANGE;
3300
3301 if (options & DPNI_OPT_NO_MAC_FILTER)
3302 not_supported |= IFF_UNICAST_FLT;
3303 else
3304 supported |= IFF_UNICAST_FLT;
3305
3306 net_dev->priv_flags |= supported;
3307 net_dev->priv_flags &= ~not_supported;
3308
3309 /* Features */
3310 net_dev->features = NETIF_F_RXCSUM |
3311 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3312 NETIF_F_SG | NETIF_F_HIGHDMA |
3313 NETIF_F_LLTX;
3314 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003315
3316 return 0;
3317}
3318
3319static int poll_link_state(void *arg)
3320{
3321 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
3322 int err;
3323
3324 while (!kthread_should_stop()) {
3325 err = link_state_update(priv);
3326 if (unlikely(err))
3327 return err;
3328
3329 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
3330 }
3331
3332 return 0;
3333}
3334
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003335static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
3336{
Ioana Radulescu112197d2017-10-11 08:29:49 -05003337 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003338 struct device *dev = (struct device *)arg;
3339 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
3340 struct net_device *net_dev = dev_get_drvdata(dev);
3341 int err;
3342
3343 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
3344 DPNI_IRQ_INDEX, &status);
3345 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003346 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05003347 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003348 }
3349
Ioana Radulescu112197d2017-10-11 08:29:49 -05003350 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003351 link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003352
Florin Chiculita8398b372019-10-16 10:36:22 +03003353 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED)
3354 set_mac_addr(netdev_priv(net_dev));
3355
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003356 return IRQ_HANDLED;
3357}
3358
3359static int setup_irqs(struct fsl_mc_device *ls_dev)
3360{
3361 int err = 0;
3362 struct fsl_mc_device_irq *irq;
3363
3364 err = fsl_mc_allocate_irqs(ls_dev);
3365 if (err) {
3366 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
3367 return err;
3368 }
3369
3370 irq = ls_dev->irqs[0];
3371 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05003372 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003373 IRQF_NO_SUSPEND | IRQF_ONESHOT,
3374 dev_name(&ls_dev->dev), &ls_dev->dev);
3375 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003376 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003377 goto free_mc_irq;
3378 }
3379
3380 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
Florin Chiculita8398b372019-10-16 10:36:22 +03003381 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
3382 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003383 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003384 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003385 goto free_irq;
3386 }
3387
3388 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
3389 DPNI_IRQ_INDEX, 1);
3390 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003391 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003392 goto free_irq;
3393 }
3394
3395 return 0;
3396
3397free_irq:
3398 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
3399free_mc_irq:
3400 fsl_mc_free_irqs(ls_dev);
3401
3402 return err;
3403}
3404
3405static void add_ch_napi(struct dpaa2_eth_priv *priv)
3406{
3407 int i;
3408 struct dpaa2_eth_channel *ch;
3409
3410 for (i = 0; i < priv->num_channels; i++) {
3411 ch = priv->channel[i];
3412 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
3413 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
3414 NAPI_POLL_WEIGHT);
3415 }
3416}
3417
3418static void del_ch_napi(struct dpaa2_eth_priv *priv)
3419{
3420 int i;
3421 struct dpaa2_eth_channel *ch;
3422
3423 for (i = 0; i < priv->num_channels; i++) {
3424 ch = priv->channel[i];
3425 netif_napi_del(&ch->napi);
3426 }
3427}
3428
3429static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
3430{
3431 struct device *dev;
3432 struct net_device *net_dev = NULL;
3433 struct dpaa2_eth_priv *priv = NULL;
3434 int err = 0;
3435
3436 dev = &dpni_dev->dev;
3437
3438 /* Net device */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03003439 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003440 if (!net_dev) {
3441 dev_err(dev, "alloc_etherdev_mq() failed\n");
3442 return -ENOMEM;
3443 }
3444
3445 SET_NETDEV_DEV(net_dev, dev);
3446 dev_set_drvdata(dev, net_dev);
3447
3448 priv = netdev_priv(net_dev);
3449 priv->net_dev = net_dev;
3450
Ioana Radulescu08eb2392017-05-24 07:13:27 -05003451 priv->iommu_domain = iommu_get_domain_for_dev(dev);
3452
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003453 /* Obtain a MC portal */
3454 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3455 &priv->mc_io);
3456 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05003457 if (err == -ENXIO)
3458 err = -EPROBE_DEFER;
3459 else
3460 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003461 goto err_portal_alloc;
3462 }
3463
3464 /* MC objects initialization and configuration */
3465 err = setup_dpni(dpni_dev);
3466 if (err)
3467 goto err_dpni_setup;
3468
3469 err = setup_dpio(priv);
3470 if (err)
3471 goto err_dpio_setup;
3472
3473 setup_fqs(priv);
3474
3475 err = setup_dpbp(priv);
3476 if (err)
3477 goto err_dpbp_setup;
3478
3479 err = bind_dpni(priv);
3480 if (err)
3481 goto err_bind;
3482
3483 /* Add a NAPI context for each channel */
3484 add_ch_napi(priv);
3485
3486 /* Percpu statistics */
3487 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
3488 if (!priv->percpu_stats) {
3489 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
3490 err = -ENOMEM;
3491 goto err_alloc_percpu_stats;
3492 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003493 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
3494 if (!priv->percpu_extras) {
3495 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
3496 err = -ENOMEM;
3497 goto err_alloc_percpu_extras;
3498 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003499
3500 err = netdev_init(net_dev);
3501 if (err)
3502 goto err_netdev_init;
3503
3504 /* Configure checksum offload based on current interface flags */
3505 err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
3506 if (err)
3507 goto err_csum;
3508
3509 err = set_tx_csum(priv, !!(net_dev->features &
3510 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
3511 if (err)
3512 goto err_csum;
3513
3514 err = alloc_rings(priv);
3515 if (err)
3516 goto err_alloc_rings;
3517
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003518 err = setup_irqs(dpni_dev);
3519 if (err) {
3520 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
3521 priv->poll_thread = kthread_run(poll_link_state, priv,
3522 "%s_poll_link", net_dev->name);
3523 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003524 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003525 goto err_poll_thread;
3526 }
3527 priv->do_link_poll = true;
3528 }
3529
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003530 err = register_netdev(net_dev);
3531 if (err < 0) {
3532 dev_err(dev, "register_netdev() failed\n");
3533 goto err_netdev_reg;
3534 }
3535
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003536#ifdef CONFIG_DEBUG_FS
3537 dpaa2_dbg_add(priv);
3538#endif
3539
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003540 dev_info(dev, "Probed interface %s\n", net_dev->name);
3541 return 0;
3542
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003543err_netdev_reg:
3544 if (priv->do_link_poll)
3545 kthread_stop(priv->poll_thread);
3546 else
3547 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003548err_poll_thread:
3549 free_rings(priv);
3550err_alloc_rings:
3551err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003552err_netdev_init:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003553 free_percpu(priv->percpu_extras);
3554err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003555 free_percpu(priv->percpu_stats);
3556err_alloc_percpu_stats:
3557 del_ch_napi(priv);
3558err_bind:
3559 free_dpbp(priv);
3560err_dpbp_setup:
3561 free_dpio(priv);
3562err_dpio_setup:
3563 free_dpni(priv);
3564err_dpni_setup:
3565 fsl_mc_portal_free(priv->mc_io);
3566err_portal_alloc:
3567 dev_set_drvdata(dev, NULL);
3568 free_netdev(net_dev);
3569
3570 return err;
3571}
3572
3573static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
3574{
3575 struct device *dev;
3576 struct net_device *net_dev;
3577 struct dpaa2_eth_priv *priv;
3578
3579 dev = &ls_dev->dev;
3580 net_dev = dev_get_drvdata(dev);
3581 priv = netdev_priv(net_dev);
3582
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003583#ifdef CONFIG_DEBUG_FS
3584 dpaa2_dbg_remove(priv);
3585#endif
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003586 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003587
3588 if (priv->do_link_poll)
3589 kthread_stop(priv->poll_thread);
3590 else
3591 fsl_mc_free_irqs(ls_dev);
3592
3593 free_rings(priv);
3594 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003595 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003596
3597 del_ch_napi(priv);
3598 free_dpbp(priv);
3599 free_dpio(priv);
3600 free_dpni(priv);
3601
3602 fsl_mc_portal_free(priv->mc_io);
3603
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003604 free_netdev(net_dev);
3605
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05003606 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05003607
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003608 return 0;
3609}
3610
3611static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
3612 {
3613 .vendor = FSL_MC_VENDOR_FREESCALE,
3614 .obj_type = "dpni",
3615 },
3616 { .vendor = 0x0 }
3617};
3618MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
3619
3620static struct fsl_mc_driver dpaa2_eth_driver = {
3621 .driver = {
3622 .name = KBUILD_MODNAME,
3623 .owner = THIS_MODULE,
3624 },
3625 .probe = dpaa2_eth_probe,
3626 .remove = dpaa2_eth_remove,
3627 .match_id_table = dpaa2_eth_match_id_table
3628};
3629
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003630static int __init dpaa2_eth_driver_init(void)
3631{
3632 int err;
3633
3634 dpaa2_eth_dbg_init();
3635 err = fsl_mc_driver_register(&dpaa2_eth_driver);
3636 if (err) {
3637 dpaa2_eth_dbg_exit();
3638 return err;
3639 }
3640
3641 return 0;
3642}
3643
3644static void __exit dpaa2_eth_driver_exit(void)
3645{
3646 dpaa2_eth_dbg_exit();
3647 fsl_mc_driver_unregister(&dpaa2_eth_driver);
3648}
3649
3650module_init(dpaa2_eth_driver_init);
3651module_exit(dpaa2_eth_driver_exit);