blob: 3bf5df92ecfa8764d3c77f5ac8900bb5fdff40d5 [file] [log] [blame]
Ioana Ciornei0bb29b22018-07-31 12:02:47 -05001// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002/* Copyright 2014-2016 Freescale Semiconductor Inc.
Ioana Ciornei48c04812020-04-22 15:05:10 +03003 * Copyright 2016-2020 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 Ciorneiefa6a7d2020-05-15 15:30:22 +030089 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000090 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 Ciorneiefa6a7d2020-05-15 15:30:22 +0300147 dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000148 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,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300188 sg_length, priv->rx_buf_size);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500189 }
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 Ciorneiefa6a7d2020-05-15 15:30:22 +0300214 dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000215 DMA_BIDIRECTIONAL);
216 free_pages((unsigned long)vaddr, 0);
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000217 }
218}
219
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000220static void xdp_release_buf(struct dpaa2_eth_priv *priv,
221 struct dpaa2_eth_channel *ch,
222 dma_addr_t addr)
223{
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300224 int retries = 0;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000225 int err;
226
227 ch->xdp.drop_bufs[ch->xdp.drop_cnt++] = addr;
228 if (ch->xdp.drop_cnt < DPAA2_ETH_BUFS_PER_CMD)
229 return;
230
231 while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
232 ch->xdp.drop_bufs,
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300233 ch->xdp.drop_cnt)) == -EBUSY) {
234 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
235 break;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000236 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300237 }
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000238
239 if (err) {
240 free_bufs(priv, ch->xdp.drop_bufs, ch->xdp.drop_cnt);
241 ch->buf_count -= ch->xdp.drop_cnt;
242 }
243
244 ch->xdp.drop_cnt = 0;
245}
246
Ioana Ciornei38c440b2020-05-06 20:47:17 +0300247static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
248 struct dpaa2_eth_fq *fq,
249 struct dpaa2_eth_xdp_fds *xdp_fds)
250{
251 int total_enqueued = 0, retries = 0, enqueued;
252 struct dpaa2_eth_drv_stats *percpu_extras;
253 int num_fds, err, max_retries;
254 struct dpaa2_fd *fds;
255
256 percpu_extras = this_cpu_ptr(priv->percpu_extras);
257
258 /* try to enqueue all the FDs until the max number of retries is hit */
259 fds = xdp_fds->fds;
260 num_fds = xdp_fds->num;
261 max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
262 while (total_enqueued < num_fds && retries < max_retries) {
263 err = priv->enqueue(priv, fq, &fds[total_enqueued],
264 0, num_fds - total_enqueued, &enqueued);
265 if (err == -EBUSY) {
266 percpu_extras->tx_portal_busy += ++retries;
267 continue;
268 }
269 total_enqueued += enqueued;
270 }
271 xdp_fds->num = 0;
272
273 return total_enqueued;
274}
275
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300276static void xdp_tx_flush(struct dpaa2_eth_priv *priv,
277 struct dpaa2_eth_channel *ch,
278 struct dpaa2_eth_fq *fq)
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000279{
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300280 struct rtnl_link_stats64 *percpu_stats;
281 struct dpaa2_fd *fds;
282 int enqueued, i;
283
284 percpu_stats = this_cpu_ptr(priv->percpu_stats);
285
286 // enqueue the array of XDP_TX frames
287 enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);
288
289 /* update statistics */
290 percpu_stats->tx_packets += enqueued;
291 fds = fq->xdp_tx_fds.fds;
292 for (i = 0; i < enqueued; i++) {
293 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
294 ch->stats.xdp_tx++;
295 }
296 for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
297 xdp_release_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
298 percpu_stats->tx_errors++;
299 ch->stats.xdp_tx_err++;
300 }
301 fq->xdp_tx_fds.num = 0;
302}
303
304static void xdp_enqueue(struct dpaa2_eth_priv *priv,
305 struct dpaa2_eth_channel *ch,
306 struct dpaa2_fd *fd,
307 void *buf_start, u16 queue_id)
308{
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000309 struct dpaa2_faead *faead;
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300310 struct dpaa2_fd *dest_fd;
311 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000312 u32 ctrl, frc;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000313
314 /* Mark the egress frame hardware annotation area as valid */
315 frc = dpaa2_fd_get_frc(fd);
316 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
317 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
318
319 /* Instruct hardware to release the FD buffer directly into
320 * the buffer pool once transmission is completed, instead of
321 * sending a Tx confirmation frame to us
322 */
323 ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
324 faead = dpaa2_get_faead(buf_start, false);
325 faead->ctrl = cpu_to_le32(ctrl);
326 faead->conf_fqid = 0;
327
328 fq = &priv->fq[queue_id];
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300329 dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
330 memcpy(dest_fd, fd, sizeof(*dest_fd));
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000331
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300332 if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
333 return;
334
335 xdp_tx_flush(priv, ch, fq);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000336}
337
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000338static u32 run_xdp(struct dpaa2_eth_priv *priv,
339 struct dpaa2_eth_channel *ch,
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000340 struct dpaa2_eth_fq *rx_fq,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000341 struct dpaa2_fd *fd, void *vaddr)
342{
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000343 dma_addr_t addr = dpaa2_fd_get_addr(fd);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000344 struct bpf_prog *xdp_prog;
345 struct xdp_buff xdp;
346 u32 xdp_act = XDP_PASS;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000347 int err;
348
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000349 rcu_read_lock();
350
351 xdp_prog = READ_ONCE(ch->xdp.prog);
352 if (!xdp_prog)
353 goto out;
354
355 xdp.data = vaddr + dpaa2_fd_get_offset(fd);
356 xdp.data_end = xdp.data + dpaa2_fd_get_len(fd);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000357 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000358 xdp_set_data_meta_invalid(&xdp);
Ioana Radulescud678be12019-03-01 17:47:24 +0000359 xdp.rxq = &ch->xdp_rxq;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000360
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200361 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE -
362 (dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM);
363
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000364 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
365
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000366 /* xdp.data pointer may have changed */
367 dpaa2_fd_set_offset(fd, xdp.data - vaddr);
368 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
369
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000370 switch (xdp_act) {
371 case XDP_PASS:
372 break;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000373 case XDP_TX:
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300374 xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000375 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000376 default:
377 bpf_warn_invalid_xdp_action(xdp_act);
Ioana Ciocoi Radulescuc1cb11b2018-11-29 08:43:40 +0000378 /* fall through */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000379 case XDP_ABORTED:
380 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
Ioana Ciocoi Radulescuc1cb11b2018-11-29 08:43:40 +0000381 /* fall through */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000382 case XDP_DROP:
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000383 xdp_release_buf(priv, ch, addr);
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000384 ch->stats.xdp_drop++;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000385 break;
Ioana Radulescud678be12019-03-01 17:47:24 +0000386 case XDP_REDIRECT:
387 dma_unmap_page(priv->net_dev->dev.parent, addr,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300388 priv->rx_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescud678be12019-03-01 17:47:24 +0000389 ch->buf_count--;
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200390
391 /* Allow redirect use of full headroom */
Ioana Radulescud678be12019-03-01 17:47:24 +0000392 xdp.data_hard_start = vaddr;
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200393 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
394
Ioana Radulescud678be12019-03-01 17:47:24 +0000395 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
396 if (unlikely(err))
397 ch->stats.xdp_drop++;
398 else
399 ch->stats.xdp_redirect++;
400 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000401 }
402
Ioana Radulescud678be12019-03-01 17:47:24 +0000403 ch->xdp.res |= xdp_act;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000404out:
405 rcu_read_unlock();
406 return xdp_act;
407}
408
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500409/* Main Rx frame processing routine */
410static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
411 struct dpaa2_eth_channel *ch,
412 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000413 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500414{
415 dma_addr_t addr = dpaa2_fd_get_addr(fd);
416 u8 fd_format = dpaa2_fd_get_format(fd);
417 void *vaddr;
418 struct sk_buff *skb;
419 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500420 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500421 struct device *dev = priv->net_dev->dev.parent;
422 struct dpaa2_fas *fas;
Ioana Radulescud695e762017-06-06 10:00:35 -0500423 void *buf_data;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500424 u32 status = 0;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000425 u32 xdp_act;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500426
Ioana Radulescu56361872017-04-28 04:50:32 -0500427 /* Tracing point */
428 trace_dpaa2_rx_fd(priv->net_dev, fd);
429
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500430 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300431 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu18c2e772018-11-26 16:27:32 +0000432 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500433
Ioana Radulescu54ce8912017-12-08 06:47:53 -0600434 fas = dpaa2_get_fas(vaddr, false);
Ioana Radulescud695e762017-06-06 10:00:35 -0500435 prefetch(fas);
436 buf_data = vaddr + dpaa2_fd_get_offset(fd);
437 prefetch(buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500438
439 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500440 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500441
442 if (fd_format == dpaa2_fd_single) {
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000443 xdp_act = run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000444 if (xdp_act != XDP_PASS) {
445 percpu_stats->rx_packets++;
446 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
447 return;
448 }
449
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300450 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000451 DMA_BIDIRECTIONAL);
Ioana Ciorneifdb6ca92018-10-12 16:27:35 +0000452 skb = build_linear_skb(ch, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500453 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000454 WARN_ON(priv->xdp_prog);
455
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300456 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000457 DMA_BIDIRECTIONAL);
Ioana Radulescud695e762017-06-06 10:00:35 -0500458 skb = build_frag_skb(priv, ch, buf_data);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000459 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500460 percpu_extras->rx_sg_frames++;
461 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500462 } else {
463 /* We don't support any other format */
464 goto err_frame_format;
465 }
466
467 if (unlikely(!skb))
468 goto err_build_skb;
469
470 prefetch(skb->data);
471
Ioana Radulescu859f9982018-04-26 18:23:47 +0800472 /* Get the timestamp value */
473 if (priv->rx_tstamp) {
474 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
475 __le64 *ts = dpaa2_get_ts(vaddr, false);
476 u64 ns;
477
478 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
479
480 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
481 shhwtstamps->hwtstamp = ns_to_ktime(ns);
482 }
483
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500484 /* Check if we need to validate the L4 csum */
485 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500486 status = le32_to_cpu(fas->status);
487 validate_rx_csum(priv, status, skb);
488 }
489
490 skb->protocol = eth_type_trans(skb, priv->net_dev);
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000491 skb_record_rx_queue(skb, fq->flowid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500492
493 percpu_stats->rx_packets++;
494 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
495
Ioana Ciornei0a25d922019-03-25 13:42:39 +0000496 list_add_tail(&skb->list, ch->rx_list);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500497
498 return;
499
500err_build_skb:
501 free_rx_fd(priv, fd, vaddr);
502err_frame_format:
503 percpu_stats->rx_dropped++;
504}
505
506/* Consume all frames pull-dequeued into the store. This is the simplest way to
507 * make sure we don't accidentally issue another volatile dequeue which would
508 * overwrite (leak) frames already in the store.
509 *
510 * Observance of NAPI budget is not our concern, leaving that to the caller.
511 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000512static int consume_frames(struct dpaa2_eth_channel *ch,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000513 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500514{
515 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000516 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500517 struct dpaa2_dq *dq;
518 const struct dpaa2_fd *fd;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300519 int cleaned = 0, retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500520 int is_last;
521
522 do {
523 dq = dpaa2_io_store_next(ch->store, &is_last);
524 if (unlikely(!dq)) {
525 /* If we're here, we *must* have placed a
526 * volatile dequeue comnmand, so keep reading through
527 * the store until we get some sort of valid response
528 * token (either a valid frame or an "empty dequeue")
529 */
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300530 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
531 netdev_err_once(priv->net_dev,
532 "Unable to read a valid dequeue response\n");
533 return -ETIMEDOUT;
534 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500535 continue;
536 }
537
538 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600539 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500540
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000541 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500542 cleaned++;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300543 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500544 } while (!is_last);
545
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000546 if (!cleaned)
547 return 0;
548
549 fq->stats.frames += cleaned;
Ioana Ciornei460fd832020-04-24 12:33:18 +0300550 ch->stats.frames += cleaned;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000551
552 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000553 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000554 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000555 if (src)
556 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000557
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500558 return cleaned;
559}
560
Ioana Radulescu859f9982018-04-26 18:23:47 +0800561/* Configure the egress frame annotation for timestamp update */
562static void enable_tx_tstamp(struct dpaa2_fd *fd, void *buf_start)
563{
564 struct dpaa2_faead *faead;
565 u32 ctrl, frc;
566
567 /* Mark the egress frame annotation area as valid */
568 frc = dpaa2_fd_get_frc(fd);
569 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
570
571 /* Set hardware annotation size */
572 ctrl = dpaa2_fd_get_ctrl(fd);
573 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
574
575 /* enable UPD (update prepanded data) bit in FAEAD field of
576 * hardware frame annotation area
577 */
578 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
579 faead = dpaa2_get_faead(buf_start, true);
580 faead->ctrl = cpu_to_le32(ctrl);
581}
582
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500583/* Create a frame descriptor based on a fragmented skb */
584static int build_sg_fd(struct dpaa2_eth_priv *priv,
585 struct sk_buff *skb,
586 struct dpaa2_fd *fd)
587{
588 struct device *dev = priv->net_dev->dev.parent;
589 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500590 dma_addr_t addr;
591 int nr_frags = skb_shinfo(skb)->nr_frags;
592 struct dpaa2_sg_entry *sgt;
593 int i, err;
594 int sgt_buf_size;
595 struct scatterlist *scl, *crt_scl;
596 int num_sg;
597 int num_dma_bufs;
598 struct dpaa2_eth_swa *swa;
599
600 /* Create and map scatterlist.
601 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
602 * to go beyond nr_frags+1.
603 * Note: We don't support chained scatterlists
604 */
605 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
606 return -EINVAL;
607
608 scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
609 if (unlikely(!scl))
610 return -ENOMEM;
611
612 sg_init_table(scl, nr_frags + 1);
613 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500614 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500615 if (unlikely(!num_dma_bufs)) {
616 err = -ENOMEM;
617 goto dma_map_sg_failed;
618 }
619
620 /* Prepare the HW SGT structure */
621 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500622 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Sebastian Andrzej Siewior90bc6d42019-06-07 21:20:37 +0200623 sgt_buf = napi_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500624 if (unlikely(!sgt_buf)) {
625 err = -ENOMEM;
626 goto sgt_buf_alloc_failed;
627 }
628 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500629 memset(sgt_buf, 0, sgt_buf_size);
630
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500631 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
632
633 /* Fill in the HW SGT structure.
634 *
635 * sgt_buf is zeroed out, so the following fields are implicit
636 * in all sgt entries:
637 * - offset is 0
638 * - format is 'dpaa2_sg_single'
639 */
640 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
641 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
642 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
643 }
644 dpaa2_sg_set_final(&sgt[i - 1], true);
645
646 /* Store the skb backpointer in the SGT buffer.
647 * Fit the scatterlist and the number of buffers alongside the
648 * skb backpointer in the software annotation area. We'll need
649 * all of them on Tx Conf.
650 */
651 swa = (struct dpaa2_eth_swa *)sgt_buf;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000652 swa->type = DPAA2_ETH_SWA_SG;
653 swa->sg.skb = skb;
654 swa->sg.scl = scl;
655 swa->sg.num_sg = num_sg;
656 swa->sg.sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500657
658 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500659 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500660 if (unlikely(dma_mapping_error(dev, addr))) {
661 err = -ENOMEM;
662 goto dma_map_single_failed;
663 }
664 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
665 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
666 dpaa2_fd_set_addr(fd, addr);
667 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000668 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500669
Ioana Radulescu859f9982018-04-26 18:23:47 +0800670 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
671 enable_tx_tstamp(fd, sgt_buf);
672
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500673 return 0;
674
675dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500676 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500677sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500678 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500679dma_map_sg_failed:
680 kfree(scl);
681 return err;
682}
683
684/* Create a frame descriptor based on a linear skb */
685static int build_single_fd(struct dpaa2_eth_priv *priv,
686 struct sk_buff *skb,
687 struct dpaa2_fd *fd)
688{
689 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600690 u8 *buffer_start, *aligned_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000691 struct dpaa2_eth_swa *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500692 dma_addr_t addr;
693
Ioana Radulescuc1636852017-12-08 06:47:58 -0600694 buffer_start = skb->data - dpaa2_eth_needed_headroom(priv, skb);
695
696 /* If there's enough room to align the FD address, do it.
697 * It will help hardware optimize accesses.
698 */
699 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
700 DPAA2_ETH_TX_BUF_ALIGN);
701 if (aligned_start >= skb->head)
702 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500703
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500704 /* Store a backpointer to the skb at the beginning of the buffer
705 * (in the private data area) such that we can release it
706 * on Tx confirm
707 */
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000708 swa = (struct dpaa2_eth_swa *)buffer_start;
709 swa->type = DPAA2_ETH_SWA_SINGLE;
710 swa->single.skb = skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500711
712 addr = dma_map_single(dev, buffer_start,
713 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500714 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500715 if (unlikely(dma_mapping_error(dev, addr)))
716 return -ENOMEM;
717
718 dpaa2_fd_set_addr(fd, addr);
719 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
720 dpaa2_fd_set_len(fd, skb->len);
721 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000722 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500723
Ioana Radulescu859f9982018-04-26 18:23:47 +0800724 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
725 enable_tx_tstamp(fd, buffer_start);
726
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500727 return 0;
728}
729
730/* FD freeing routine on the Tx path
731 *
732 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
733 * back-pointed to is also freed.
734 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
735 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500736 */
737static void free_tx_fd(const struct dpaa2_eth_priv *priv,
Ioana Radulescud678be12019-03-01 17:47:24 +0000738 struct dpaa2_eth_fq *fq,
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +0000739 const struct dpaa2_fd *fd, bool in_napi)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500740{
741 struct device *dev = priv->net_dev->dev.parent;
742 dma_addr_t fd_addr;
Ioana Radulescud678be12019-03-01 17:47:24 +0000743 struct sk_buff *skb = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500744 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500745 struct dpaa2_eth_swa *swa;
746 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescud678be12019-03-01 17:47:24 +0000747 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500748
749 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000750 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
751 swa = (struct dpaa2_eth_swa *)buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500752
753 if (fd_format == dpaa2_fd_single) {
Ioana Radulescud678be12019-03-01 17:47:24 +0000754 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
755 skb = swa->single.skb;
756 /* Accessing the skb buffer is safe before dma unmap,
757 * because we didn't map the actual skb shell.
758 */
759 dma_unmap_single(dev, fd_addr,
760 skb_tail_pointer(skb) - buffer_start,
761 DMA_BIDIRECTIONAL);
762 } else {
763 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
764 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
765 DMA_BIDIRECTIONAL);
766 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500767 } else if (fd_format == dpaa2_fd_sg) {
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000768 skb = swa->sg.skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500769
770 /* Unmap the scatterlist */
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000771 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
772 DMA_BIDIRECTIONAL);
773 kfree(swa->sg.scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500774
775 /* Unmap the SGT buffer */
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000776 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
Ioana Radulescub2718e62018-03-23 08:44:11 -0500777 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500778 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600779 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500780 return;
781 }
782
Ioana Radulescud678be12019-03-01 17:47:24 +0000783 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
784 fq->dq_frames++;
785 fq->dq_bytes += fd_len;
786 }
787
788 if (swa->type == DPAA2_ETH_SWA_XDP) {
789 xdp_return_frame(swa->xdp.xdpf);
790 return;
791 }
792
Ioana Radulescu859f9982018-04-26 18:23:47 +0800793 /* Get the timestamp value */
794 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
795 struct skb_shared_hwtstamps shhwtstamps;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000796 __le64 *ts = dpaa2_get_ts(buffer_start, true);
Ioana Radulescu859f9982018-04-26 18:23:47 +0800797 u64 ns;
798
799 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
800
801 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
802 shhwtstamps.hwtstamp = ns_to_ktime(ns);
803 skb_tstamp_tx(skb, &shhwtstamps);
804 }
805
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500806 /* Free SGT buffer allocated on tx */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500807 if (fd_format != dpaa2_fd_single)
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000808 skb_free_frag(buffer_start);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500809
810 /* Move on with skb release */
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +0000811 napi_consume_skb(skb, in_napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500812}
813
Ioana Radulescuc433db42017-06-06 10:00:26 -0500814static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500815{
816 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
817 struct dpaa2_fd fd;
818 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500819 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500820 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000821 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500822 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -0600823 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000824 u32 fd_len;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +0300825 u8 prio = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500826 int err, i;
827
828 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500829 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500830
Ioana Radulescu18c21462017-12-08 06:47:57 -0600831 needed_headroom = dpaa2_eth_needed_headroom(priv, skb);
832 if (skb_headroom(skb) < needed_headroom) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500833 struct sk_buff *ns;
834
Ioana Radulescu18c21462017-12-08 06:47:57 -0600835 ns = skb_realloc_headroom(skb, needed_headroom);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500836 if (unlikely(!ns)) {
837 percpu_stats->tx_dropped++;
838 goto err_alloc_headroom;
839 }
Ioana Radulescu6662b5e2017-12-08 06:47:55 -0600840 percpu_extras->tx_reallocs++;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800841
842 if (skb->sk)
843 skb_set_owner_w(ns, skb->sk);
844
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500845 dev_kfree_skb(skb);
846 skb = ns;
847 }
848
849 /* We'll be holding a back-reference to the skb until Tx Confirmation;
850 * we don't want that overwritten by a concurrent Tx with a cloned skb.
851 */
852 skb = skb_unshare(skb, GFP_ATOMIC);
853 if (unlikely(!skb)) {
854 /* skb_unshare() has already freed the skb */
855 percpu_stats->tx_dropped++;
856 return NETDEV_TX_OK;
857 }
858
859 /* Setup the FD fields */
860 memset(&fd, 0, sizeof(fd));
861
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500862 if (skb_is_nonlinear(skb)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500863 err = build_sg_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500864 percpu_extras->tx_sg_frames++;
865 percpu_extras->tx_sg_bytes += skb->len;
866 } else {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500867 err = build_single_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500868 }
869
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500870 if (unlikely(err)) {
871 percpu_stats->tx_dropped++;
872 goto err_build_fd;
873 }
874
Ioana Radulescu56361872017-04-28 04:50:32 -0500875 /* Tracing point */
876 trace_dpaa2_tx_fd(net_dev, &fd);
877
Ioana Radulescu537336c2017-12-21 06:33:20 -0600878 /* TxConf FQ selection relies on queue id from the stack.
879 * In case of a forwarded frame from another DPNI interface, we choose
880 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500881 */
Ioana Radulescu537336c2017-12-21 06:33:20 -0600882 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +0300883
884 if (net_dev->num_tc) {
885 prio = netdev_txq_to_tc(net_dev, queue_mapping);
886 /* Hardware interprets priority level 0 as being the highest,
887 * so we need to do a reverse mapping to the netdev tc index
888 */
889 prio = net_dev->num_tc - prio - 1;
890 /* We have only one FQ array entry for all Tx hardware queues
891 * with the same flow id (but different priority levels)
892 */
893 queue_mapping %= dpaa2_eth_queue_count(priv);
894 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500895 fq = &priv->fq[queue_mapping];
Ioana Ciornei8c838f52019-03-25 13:06:22 +0000896
897 fd_len = dpaa2_fd_get_len(&fd);
898 nq = netdev_get_tx_queue(net_dev, queue_mapping);
899 netdev_tx_sent_queue(nq, fd_len);
900
901 /* Everything that happens after this enqueues might race with
902 * the Tx confirmation callback for this frame
903 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500904 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Ciornei6ff80442020-04-22 15:05:11 +0300905 err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500906 if (err != -EBUSY)
907 break;
908 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500909 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500910 if (unlikely(err < 0)) {
911 percpu_stats->tx_errors++;
912 /* Clean up everything, including freeing the skb */
Ioana Radulescud678be12019-03-01 17:47:24 +0000913 free_tx_fd(priv, fq, &fd, false);
Ioana Ciornei8c838f52019-03-25 13:06:22 +0000914 netdev_tx_completed_queue(nq, 1, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500915 } else {
916 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000917 percpu_stats->tx_bytes += fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500918 }
919
920 return NETDEV_TX_OK;
921
922err_build_fd:
923err_alloc_headroom:
924 dev_kfree_skb(skb);
925
926 return NETDEV_TX_OK;
927}
928
929/* Tx confirmation frame processing routine */
930static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +0000931 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500932 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000933 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500934{
935 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500936 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000937 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500938 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500939
Ioana Radulescu56361872017-04-28 04:50:32 -0500940 /* Tracing point */
941 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
942
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500943 percpu_extras = this_cpu_ptr(priv->percpu_extras);
944 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000945 percpu_extras->tx_conf_bytes += fd_len;
946
Ioana Radulescu39163c02017-06-06 10:00:39 -0500947 /* Check frame errors in the FD field */
948 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Radulescud678be12019-03-01 17:47:24 +0000949 free_tx_fd(priv, fq, fd, true);
Ioana Radulescu39163c02017-06-06 10:00:39 -0500950
951 if (likely(!fd_errors))
952 return;
953
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600954 if (net_ratelimit())
955 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
956 fd_errors);
957
Ioana Radulescu39163c02017-06-06 10:00:39 -0500958 percpu_stats = this_cpu_ptr(priv->percpu_stats);
959 /* Tx-conf logically pertains to the egress path. */
960 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500961}
962
963static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
964{
965 int err;
966
967 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
968 DPNI_OFF_RX_L3_CSUM, enable);
969 if (err) {
970 netdev_err(priv->net_dev,
971 "dpni_set_offload(RX_L3_CSUM) failed\n");
972 return err;
973 }
974
975 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
976 DPNI_OFF_RX_L4_CSUM, enable);
977 if (err) {
978 netdev_err(priv->net_dev,
979 "dpni_set_offload(RX_L4_CSUM) failed\n");
980 return err;
981 }
982
983 return 0;
984}
985
986static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
987{
988 int err;
989
990 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
991 DPNI_OFF_TX_L3_CSUM, enable);
992 if (err) {
993 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
994 return err;
995 }
996
997 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
998 DPNI_OFF_TX_L4_CSUM, enable);
999 if (err) {
1000 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1001 return err;
1002 }
1003
1004 return 0;
1005}
1006
1007/* Perform a single release command to add buffers
1008 * to the specified buffer pool
1009 */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001010static int add_bufs(struct dpaa2_eth_priv *priv,
1011 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001012{
1013 struct device *dev = priv->net_dev->dev.parent;
1014 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001015 struct page *page;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001016 dma_addr_t addr;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001017 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001018 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001019
1020 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1021 /* Allocate buffer visible to WRIOP + skb shared info +
1022 * alignment padding
1023 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001024 /* allocate one page for each Rx buffer. WRIOP sees
1025 * the entire page except for a tailroom reserved for
1026 * skb shared info
1027 */
1028 page = dev_alloc_pages(0);
1029 if (!page)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001030 goto err_alloc;
1031
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001032 addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001033 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001034 if (unlikely(dma_mapping_error(dev, addr)))
1035 goto err_map;
1036
1037 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -05001038
1039 /* tracing point */
1040 trace_dpaa2_eth_buf_seed(priv->net_dev,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001041 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001042 addr, priv->rx_buf_size,
Ioana Radulescu56361872017-04-28 04:50:32 -05001043 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001044 }
1045
1046release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001047 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001048 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001049 buf_array, i)) == -EBUSY) {
1050 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1051 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001052 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001053 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001054
1055 /* If release command failed, clean up and bail out;
1056 * not much else we can do about it
1057 */
1058 if (err) {
1059 free_bufs(priv, buf_array, i);
1060 return 0;
1061 }
1062
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001063 return i;
1064
1065err_map:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001066 __free_pages(page, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001067err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001068 /* If we managed to allocate at least some buffers,
1069 * release them to hardware
1070 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001071 if (i)
1072 goto release_bufs;
1073
1074 return 0;
1075}
1076
1077static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
1078{
1079 int i, j;
1080 int new_count;
1081
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001082 for (j = 0; j < priv->num_channels; j++) {
1083 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1084 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001085 new_count = add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001086 priv->channel[j]->buf_count += new_count;
1087
1088 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001089 return -ENOMEM;
1090 }
1091 }
1092 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001093
1094 return 0;
1095}
1096
1097/**
1098 * Drain the specified number of buffers from the DPNI's private buffer pool.
1099 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1100 */
1101static void drain_bufs(struct dpaa2_eth_priv *priv, int count)
1102{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001103 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001104 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001105 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001106
1107 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001108 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001109 buf_array, count);
1110 if (ret < 0) {
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001111 if (ret == -EBUSY &&
1112 retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1113 continue;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001114 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1115 return;
1116 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001117 free_bufs(priv, buf_array, ret);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001118 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001119 } while (ret);
1120}
1121
1122static void drain_pool(struct dpaa2_eth_priv *priv)
1123{
1124 int i;
1125
1126 drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1127 drain_bufs(priv, 1);
1128
1129 for (i = 0; i < priv->num_channels; i++)
1130 priv->channel[i]->buf_count = 0;
1131}
1132
1133/* Function is called from softirq context only, so we don't need to guard
1134 * the access to percpu count
1135 */
1136static int refill_pool(struct dpaa2_eth_priv *priv,
1137 struct dpaa2_eth_channel *ch,
1138 u16 bpid)
1139{
1140 int new_count;
1141
1142 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1143 return 0;
1144
1145 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001146 new_count = add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001147 if (unlikely(!new_count)) {
1148 /* Out of memory; abort for now, we'll try later on */
1149 break;
1150 }
1151 ch->buf_count += new_count;
1152 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1153
1154 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1155 return -ENOMEM;
1156
1157 return 0;
1158}
1159
1160static int pull_channel(struct dpaa2_eth_channel *ch)
1161{
1162 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001163 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001164
1165 /* Retry while portal is busy */
1166 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001167 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1168 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001169 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001170 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001171 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001172
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001173 ch->stats.dequeue_portal_busy += dequeues;
1174 if (unlikely(err))
1175 ch->stats.pull_err++;
1176
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001177 return err;
1178}
1179
1180/* NAPI poll routine
1181 *
1182 * Frames are dequeued from the QMan channel associated with this NAPI context.
1183 * Rx, Tx confirmation and (if configured) Rx error frames all count
1184 * towards the NAPI budget.
1185 */
1186static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1187{
1188 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001189 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001190 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001191 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1192 struct netdev_queue *nq;
1193 int store_cleaned, work_done;
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001194 struct list_head rx_list;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001195 int retries = 0;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001196 u16 flowid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001197 int err;
1198
1199 ch = container_of(napi, struct dpaa2_eth_channel, napi);
Ioana Radulescud678be12019-03-01 17:47:24 +00001200 ch->xdp.res = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001201 priv = ch->priv;
1202
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001203 INIT_LIST_HEAD(&rx_list);
1204 ch->rx_list = &rx_list;
1205
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001206 do {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001207 err = pull_channel(ch);
1208 if (unlikely(err))
1209 break;
1210
1211 /* Refill pool if appropriate */
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001212 refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001213
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001214 store_cleaned = consume_frames(ch, &fq);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001215 if (store_cleaned <= 0)
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001216 break;
1217 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001218 rx_cleaned += store_cleaned;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001219 flowid = fq->flowid;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001220 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001221 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001222 /* We have a single Tx conf FQ on this channel */
1223 txc_fq = fq;
1224 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001225
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001226 /* If we either consumed the whole NAPI budget with Rx frames
1227 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001228 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001229 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001230 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1231 work_done = budget;
1232 goto out;
1233 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001234 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001235
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001236 /* We didn't consume the entire budget, so finish napi and
1237 * re-enable data availability notifications
1238 */
1239 napi_complete_done(napi, rx_cleaned);
1240 do {
1241 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1242 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001243 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001244 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1245 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001246
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001247 work_done = max(rx_cleaned, 1);
1248
1249out:
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001250 netif_receive_skb_list(ch->rx_list);
1251
Ioana Radulescud678be12019-03-01 17:47:24 +00001252 if (txc_fq && txc_fq->dq_frames) {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001253 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1254 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1255 txc_fq->dq_bytes);
1256 txc_fq->dq_frames = 0;
1257 txc_fq->dq_bytes = 0;
1258 }
1259
Ioana Radulescud678be12019-03-01 17:47:24 +00001260 if (ch->xdp.res & XDP_REDIRECT)
1261 xdp_do_flush_map();
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001262 else if (rx_cleaned && ch->xdp.res & XDP_TX)
1263 xdp_tx_flush(priv, ch, &priv->fq[flowid]);
Ioana Radulescud678be12019-03-01 17:47:24 +00001264
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001265 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001266}
1267
1268static void enable_ch_napi(struct dpaa2_eth_priv *priv)
1269{
1270 struct dpaa2_eth_channel *ch;
1271 int i;
1272
1273 for (i = 0; i < priv->num_channels; i++) {
1274 ch = priv->channel[i];
1275 napi_enable(&ch->napi);
1276 }
1277}
1278
1279static void disable_ch_napi(struct dpaa2_eth_priv *priv)
1280{
1281 struct dpaa2_eth_channel *ch;
1282 int i;
1283
1284 for (i = 0; i < priv->num_channels; i++) {
1285 ch = priv->channel[i];
1286 napi_disable(&ch->napi);
1287 }
1288}
1289
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001290static void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv, bool enable)
1291{
1292 struct dpni_taildrop td = {0};
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001293 struct dpaa2_eth_fq *fq;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001294 int i, err;
1295
1296 if (priv->rx_td_enabled == enable)
1297 return;
1298
1299 td.enable = enable;
1300 td.threshold = DPAA2_ETH_TAILDROP_THRESH;
1301
1302 for (i = 0; i < priv->num_fqs; i++) {
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001303 fq = &priv->fq[i];
1304 if (fq->type != DPAA2_RX_FQ)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001305 continue;
1306 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001307 DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1308 fq->tc, fq->flowid, &td);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001309 if (err) {
1310 netdev_err(priv->net_dev,
1311 "dpni_set_taildrop() failed\n");
1312 break;
1313 }
1314 }
1315
1316 priv->rx_td_enabled = enable;
1317}
1318
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001319static int link_state_update(struct dpaa2_eth_priv *priv)
1320{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001321 struct dpni_link_state state = {0};
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001322 bool tx_pause;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001323 int err;
1324
1325 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1326 if (unlikely(err)) {
1327 netdev_err(priv->net_dev,
1328 "dpni_get_link_state() failed\n");
1329 return err;
1330 }
1331
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001332 /* If Tx pause frame settings have changed, we need to update
1333 * Rx FQ taildrop configuration as well. We configure taildrop
1334 * only when pause frame generation is disabled.
1335 */
1336 tx_pause = !!(state.options & DPNI_LINK_OPT_PAUSE) ^
1337 !!(state.options & DPNI_LINK_OPT_ASYM_PAUSE);
1338 dpaa2_eth_set_rx_taildrop(priv, !tx_pause);
1339
Ioana Ciornei71947922019-10-31 01:18:31 +02001340 /* When we manage the MAC/PHY using phylink there is no need
1341 * to manually update the netif_carrier.
1342 */
1343 if (priv->mac)
1344 goto out;
1345
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001346 /* Chech link state; speed / duplex changes are not treated yet */
1347 if (priv->link_state.up == state.up)
Ioana Radulescucce629432019-08-28 17:08:14 +03001348 goto out;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001349
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001350 if (state.up) {
1351 netif_carrier_on(priv->net_dev);
1352 netif_tx_start_all_queues(priv->net_dev);
1353 } else {
1354 netif_tx_stop_all_queues(priv->net_dev);
1355 netif_carrier_off(priv->net_dev);
1356 }
1357
Ioana Radulescu77160af2017-06-06 10:00:28 -05001358 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001359 state.up ? "up" : "down");
1360
Ioana Radulescucce629432019-08-28 17:08:14 +03001361out:
1362 priv->link_state = state;
1363
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001364 return 0;
1365}
1366
1367static int dpaa2_eth_open(struct net_device *net_dev)
1368{
1369 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1370 int err;
1371
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001372 err = seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001373 if (err) {
1374 /* Not much to do; the buffer pool, though not filled up,
1375 * may still contain some buffers which would enable us
1376 * to limp on.
1377 */
1378 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001379 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001380 }
1381
Ioana Ciornei71947922019-10-31 01:18:31 +02001382 if (!priv->mac) {
1383 /* We'll only start the txqs when the link is actually ready;
1384 * make sure we don't race against the link up notification,
1385 * which may come immediately after dpni_enable();
1386 */
1387 netif_tx_stop_all_queues(net_dev);
1388
1389 /* Also, explicitly set carrier off, otherwise
1390 * netif_carrier_ok() will return true and cause 'ip link show'
1391 * to report the LOWER_UP flag, even though the link
1392 * notification wasn't even received.
1393 */
1394 netif_carrier_off(net_dev);
1395 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001396 enable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001397
1398 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1399 if (err < 0) {
1400 netdev_err(net_dev, "dpni_enable() failed\n");
1401 goto enable_err;
1402 }
1403
Ioana Ciornei71947922019-10-31 01:18:31 +02001404 if (!priv->mac) {
1405 /* If the DPMAC object has already processed the link up
1406 * interrupt, we have to learn the link state ourselves.
1407 */
1408 err = link_state_update(priv);
1409 if (err < 0) {
1410 netdev_err(net_dev, "Can't update link state\n");
1411 goto link_state_err;
1412 }
1413 } else {
1414 phylink_start(priv->mac->phylink);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001415 }
1416
1417 return 0;
1418
1419link_state_err:
1420enable_err:
1421 disable_ch_napi(priv);
1422 drain_pool(priv);
1423 return err;
1424}
1425
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001426/* Total number of in-flight frames on ingress queues */
1427static u32 ingress_fq_count(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001428{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001429 struct dpaa2_eth_fq *fq;
1430 u32 fcnt = 0, bcnt = 0, total = 0;
1431 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001432
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001433 for (i = 0; i < priv->num_fqs; i++) {
1434 fq = &priv->fq[i];
1435 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1436 if (err) {
1437 netdev_warn(priv->net_dev, "query_fq_count failed");
1438 break;
1439 }
1440 total += fcnt;
1441 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001442
1443 return total;
1444}
1445
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001446static void wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001447{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001448 int retries = 10;
1449 u32 pending;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001450
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001451 do {
1452 pending = ingress_fq_count(priv);
1453 if (pending)
1454 msleep(100);
1455 } while (pending && --retries);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001456}
1457
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001458#define DPNI_TX_PENDING_VER_MAJOR 7
1459#define DPNI_TX_PENDING_VER_MINOR 13
1460static void wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
1461{
1462 union dpni_statistics stats;
1463 int retries = 10;
1464 int err;
1465
1466 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1467 DPNI_TX_PENDING_VER_MINOR) < 0)
1468 goto out;
1469
1470 do {
1471 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1472 &stats);
1473 if (err)
1474 goto out;
1475 if (stats.page_6.tx_pending_frames == 0)
1476 return;
1477 } while (--retries);
1478
1479out:
1480 msleep(500);
1481}
1482
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001483static int dpaa2_eth_stop(struct net_device *net_dev)
1484{
1485 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001486 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001487 int retries = 10;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001488
Ioana Ciornei71947922019-10-31 01:18:31 +02001489 if (!priv->mac) {
1490 netif_tx_stop_all_queues(net_dev);
1491 netif_carrier_off(net_dev);
1492 } else {
1493 phylink_stop(priv->mac->phylink);
1494 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001495
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001496 /* On dpni_disable(), the MC firmware will:
1497 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1498 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1499 * of all in flight Tx frames is finished (and corresponding Tx conf
1500 * frames are enqueued back to software)
1501 *
1502 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1503 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1504 * and Tx conf queues are consumed on NAPI poll.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001505 */
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001506 wait_for_egress_fq_empty(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001507
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001508 do {
1509 dpni_disable(priv->mc_io, 0, priv->mc_token);
1510 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1511 if (dpni_enabled)
1512 /* Allow the hardware some slack */
1513 msleep(100);
1514 } while (dpni_enabled && --retries);
1515 if (!retries) {
1516 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1517 /* Must go on and disable NAPI nonetheless, so we don't crash at
1518 * the next "ifconfig up"
1519 */
1520 }
1521
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001522 wait_for_ingress_fq_empty(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001523 disable_ch_napi(priv);
1524
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001525 /* Empty the buffer pool */
1526 drain_pool(priv);
1527
1528 return 0;
1529}
1530
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001531static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1532{
1533 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1534 struct device *dev = net_dev->dev.parent;
1535 int err;
1536
1537 err = eth_mac_addr(net_dev, addr);
1538 if (err < 0) {
1539 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1540 return err;
1541 }
1542
1543 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1544 net_dev->dev_addr);
1545 if (err) {
1546 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1547 return err;
1548 }
1549
1550 return 0;
1551}
1552
1553/** Fill in counters maintained by the GPP driver. These may be different from
1554 * the hardware counters obtained by ethtool.
1555 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001556static void dpaa2_eth_get_stats(struct net_device *net_dev,
1557 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001558{
1559 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1560 struct rtnl_link_stats64 *percpu_stats;
1561 u64 *cpustats;
1562 u64 *netstats = (u64 *)stats;
1563 int i, j;
1564 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1565
1566 for_each_possible_cpu(i) {
1567 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1568 cpustats = (u64 *)percpu_stats;
1569 for (j = 0; j < num; j++)
1570 netstats[j] += cpustats[j];
1571 }
1572}
1573
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001574/* Copy mac unicast addresses from @net_dev to @priv.
1575 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1576 */
1577static void add_uc_hw_addr(const struct net_device *net_dev,
1578 struct dpaa2_eth_priv *priv)
1579{
1580 struct netdev_hw_addr *ha;
1581 int err;
1582
1583 netdev_for_each_uc_addr(ha, net_dev) {
1584 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1585 ha->addr);
1586 if (err)
1587 netdev_warn(priv->net_dev,
1588 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1589 ha->addr, err);
1590 }
1591}
1592
1593/* Copy mac multicast addresses from @net_dev to @priv
1594 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1595 */
1596static void add_mc_hw_addr(const struct net_device *net_dev,
1597 struct dpaa2_eth_priv *priv)
1598{
1599 struct netdev_hw_addr *ha;
1600 int err;
1601
1602 netdev_for_each_mc_addr(ha, net_dev) {
1603 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1604 ha->addr);
1605 if (err)
1606 netdev_warn(priv->net_dev,
1607 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1608 ha->addr, err);
1609 }
1610}
1611
1612static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1613{
1614 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1615 int uc_count = netdev_uc_count(net_dev);
1616 int mc_count = netdev_mc_count(net_dev);
1617 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1618 u32 options = priv->dpni_attrs.options;
1619 u16 mc_token = priv->mc_token;
1620 struct fsl_mc_io *mc_io = priv->mc_io;
1621 int err;
1622
1623 /* Basic sanity checks; these probably indicate a misconfiguration */
1624 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1625 netdev_info(net_dev,
1626 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1627 max_mac);
1628
1629 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1630 if (uc_count > max_mac) {
1631 netdev_info(net_dev,
1632 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1633 uc_count, max_mac);
1634 goto force_promisc;
1635 }
1636 if (mc_count + uc_count > max_mac) {
1637 netdev_info(net_dev,
1638 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1639 uc_count + mc_count, max_mac);
1640 goto force_mc_promisc;
1641 }
1642
1643 /* Adjust promisc settings due to flag combinations */
1644 if (net_dev->flags & IFF_PROMISC)
1645 goto force_promisc;
1646 if (net_dev->flags & IFF_ALLMULTI) {
1647 /* First, rebuild unicast filtering table. This should be done
1648 * in promisc mode, in order to avoid frame loss while we
1649 * progressively add entries to the table.
1650 * We don't know whether we had been in promisc already, and
1651 * making an MC call to find out is expensive; so set uc promisc
1652 * nonetheless.
1653 */
1654 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1655 if (err)
1656 netdev_warn(net_dev, "Can't set uc promisc\n");
1657
1658 /* Actual uc table reconstruction. */
1659 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1660 if (err)
1661 netdev_warn(net_dev, "Can't clear uc filters\n");
1662 add_uc_hw_addr(net_dev, priv);
1663
1664 /* Finally, clear uc promisc and set mc promisc as requested. */
1665 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1666 if (err)
1667 netdev_warn(net_dev, "Can't clear uc promisc\n");
1668 goto force_mc_promisc;
1669 }
1670
1671 /* Neither unicast, nor multicast promisc will be on... eventually.
1672 * For now, rebuild mac filtering tables while forcing both of them on.
1673 */
1674 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1675 if (err)
1676 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1677 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1678 if (err)
1679 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1680
1681 /* Actual mac filtering tables reconstruction */
1682 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1683 if (err)
1684 netdev_warn(net_dev, "Can't clear mac filters\n");
1685 add_mc_hw_addr(net_dev, priv);
1686 add_uc_hw_addr(net_dev, priv);
1687
1688 /* Now we can clear both ucast and mcast promisc, without risking
1689 * to drop legitimate frames anymore.
1690 */
1691 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1692 if (err)
1693 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1694 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1695 if (err)
1696 netdev_warn(net_dev, "Can't clear mcast promisc\n");
1697
1698 return;
1699
1700force_promisc:
1701 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1702 if (err)
1703 netdev_warn(net_dev, "Can't set ucast promisc\n");
1704force_mc_promisc:
1705 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1706 if (err)
1707 netdev_warn(net_dev, "Can't set mcast promisc\n");
1708}
1709
1710static int dpaa2_eth_set_features(struct net_device *net_dev,
1711 netdev_features_t features)
1712{
1713 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1714 netdev_features_t changed = features ^ net_dev->features;
1715 bool enable;
1716 int err;
1717
1718 if (changed & NETIF_F_RXCSUM) {
1719 enable = !!(features & NETIF_F_RXCSUM);
1720 err = set_rx_csum(priv, enable);
1721 if (err)
1722 return err;
1723 }
1724
1725 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1726 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
1727 err = set_tx_csum(priv, enable);
1728 if (err)
1729 return err;
1730 }
1731
1732 return 0;
1733}
1734
Ioana Radulescu859f9982018-04-26 18:23:47 +08001735static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1736{
1737 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1738 struct hwtstamp_config config;
1739
1740 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
1741 return -EFAULT;
1742
1743 switch (config.tx_type) {
1744 case HWTSTAMP_TX_OFF:
1745 priv->tx_tstamp = false;
1746 break;
1747 case HWTSTAMP_TX_ON:
1748 priv->tx_tstamp = true;
1749 break;
1750 default:
1751 return -ERANGE;
1752 }
1753
1754 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
1755 priv->rx_tstamp = false;
1756 } else {
1757 priv->rx_tstamp = true;
1758 /* TS is set for all frame types, not only those requested */
1759 config.rx_filter = HWTSTAMP_FILTER_ALL;
1760 }
1761
1762 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
1763 -EFAULT : 0;
1764}
1765
1766static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1767{
Russell King4a841822020-02-27 12:00:21 +00001768 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1769
Ioana Radulescu859f9982018-04-26 18:23:47 +08001770 if (cmd == SIOCSHWTSTAMP)
1771 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
1772
Russell King4a841822020-02-27 12:00:21 +00001773 if (priv->mac)
1774 return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
1775
1776 return -EOPNOTSUPP;
Ioana Radulescu859f9982018-04-26 18:23:47 +08001777}
1778
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001779static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
1780{
1781 int mfl, linear_mfl;
1782
1783 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001784 linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001785 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001786
1787 if (mfl > linear_mfl) {
1788 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
1789 linear_mfl - VLAN_ETH_HLEN);
1790 return false;
1791 }
1792
1793 return true;
1794}
1795
1796static int set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
1797{
1798 int mfl, err;
1799
1800 /* We enforce a maximum Rx frame length based on MTU only if we have
1801 * an XDP program attached (in order to avoid Rx S/G frames).
1802 * Otherwise, we accept all incoming frames as long as they are not
1803 * larger than maximum size supported in hardware
1804 */
1805 if (has_xdp)
1806 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1807 else
1808 mfl = DPAA2_ETH_MFL;
1809
1810 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
1811 if (err) {
1812 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
1813 return err;
1814 }
1815
1816 return 0;
1817}
1818
1819static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
1820{
1821 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1822 int err;
1823
1824 if (!priv->xdp_prog)
1825 goto out;
1826
1827 if (!xdp_mtu_valid(priv, new_mtu))
1828 return -EINVAL;
1829
1830 err = set_rx_mfl(priv, new_mtu, true);
1831 if (err)
1832 return err;
1833
1834out:
1835 dev->mtu = new_mtu;
1836 return 0;
1837}
1838
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001839static int update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
1840{
1841 struct dpni_buffer_layout buf_layout = {0};
1842 int err;
1843
1844 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
1845 DPNI_QUEUE_RX, &buf_layout);
1846 if (err) {
1847 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
1848 return err;
1849 }
1850
1851 /* Reserve extra headroom for XDP header size changes */
1852 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
1853 (has_xdp ? XDP_PACKET_HEADROOM : 0);
1854 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
1855 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
1856 DPNI_QUEUE_RX, &buf_layout);
1857 if (err) {
1858 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
1859 return err;
1860 }
1861
1862 return 0;
1863}
1864
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001865static int setup_xdp(struct net_device *dev, struct bpf_prog *prog)
1866{
1867 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1868 struct dpaa2_eth_channel *ch;
1869 struct bpf_prog *old;
1870 bool up, need_update;
1871 int i, err;
1872
1873 if (prog && !xdp_mtu_valid(priv, dev->mtu))
1874 return -EINVAL;
1875
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001876 if (prog)
1877 bpf_prog_add(prog, priv->num_channels);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001878
1879 up = netif_running(dev);
1880 need_update = (!!priv->xdp_prog != !!prog);
1881
1882 if (up)
1883 dpaa2_eth_stop(dev);
1884
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001885 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
1886 * Also, when switching between xdp/non-xdp modes we need to reconfigure
1887 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
1888 * so we are sure no old format buffers will be used from now on.
1889 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001890 if (need_update) {
1891 err = set_rx_mfl(priv, dev->mtu, !!prog);
1892 if (err)
1893 goto out_err;
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001894 err = update_rx_buffer_headroom(priv, !!prog);
1895 if (err)
1896 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001897 }
1898
1899 old = xchg(&priv->xdp_prog, prog);
1900 if (old)
1901 bpf_prog_put(old);
1902
1903 for (i = 0; i < priv->num_channels; i++) {
1904 ch = priv->channel[i];
1905 old = xchg(&ch->xdp.prog, prog);
1906 if (old)
1907 bpf_prog_put(old);
1908 }
1909
1910 if (up) {
1911 err = dpaa2_eth_open(dev);
1912 if (err)
1913 return err;
1914 }
1915
1916 return 0;
1917
1918out_err:
1919 if (prog)
1920 bpf_prog_sub(prog, priv->num_channels);
1921 if (up)
1922 dpaa2_eth_open(dev);
1923
1924 return err;
1925}
1926
1927static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1928{
1929 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1930
1931 switch (xdp->command) {
1932 case XDP_SETUP_PROG:
1933 return setup_xdp(dev, xdp->prog);
1934 case XDP_QUERY_PROG:
1935 xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0;
1936 break;
1937 default:
1938 return -EINVAL;
1939 }
1940
1941 return 0;
1942}
1943
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03001944static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
1945 struct xdp_frame *xdpf,
1946 struct dpaa2_fd *fd)
Ioana Radulescud678be12019-03-01 17:47:24 +00001947{
1948 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1949 struct device *dev = net_dev->dev.parent;
Ioana Radulescud678be12019-03-01 17:47:24 +00001950 unsigned int needed_headroom;
1951 struct dpaa2_eth_swa *swa;
Ioana Radulescud678be12019-03-01 17:47:24 +00001952 void *buffer_start, *aligned_start;
1953 dma_addr_t addr;
Ioana Radulescud678be12019-03-01 17:47:24 +00001954
1955 /* We require a minimum headroom to be able to transmit the frame.
1956 * Otherwise return an error and let the original net_device handle it
1957 */
1958 needed_headroom = dpaa2_eth_needed_headroom(priv, NULL);
1959 if (xdpf->headroom < needed_headroom)
1960 return -EINVAL;
1961
Ioana Radulescud678be12019-03-01 17:47:24 +00001962 /* Setup the FD fields */
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03001963 memset(fd, 0, sizeof(*fd));
Ioana Radulescud678be12019-03-01 17:47:24 +00001964
1965 /* Align FD address, if possible */
1966 buffer_start = xdpf->data - needed_headroom;
1967 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
1968 DPAA2_ETH_TX_BUF_ALIGN);
1969 if (aligned_start >= xdpf->data - xdpf->headroom)
1970 buffer_start = aligned_start;
1971
1972 swa = (struct dpaa2_eth_swa *)buffer_start;
1973 /* fill in necessary fields here */
1974 swa->type = DPAA2_ETH_SWA_XDP;
1975 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
1976 swa->xdp.xdpf = xdpf;
1977
1978 addr = dma_map_single(dev, buffer_start,
1979 swa->xdp.dma_size,
1980 DMA_BIDIRECTIONAL);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03001981 if (unlikely(dma_mapping_error(dev, addr)))
Ioana Radulescud678be12019-03-01 17:47:24 +00001982 return -ENOMEM;
Ioana Radulescud678be12019-03-01 17:47:24 +00001983
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03001984 dpaa2_fd_set_addr(fd, addr);
1985 dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
1986 dpaa2_fd_set_len(fd, xdpf->len);
1987 dpaa2_fd_set_format(fd, dpaa2_fd_single);
1988 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescud678be12019-03-01 17:47:24 +00001989
1990 return 0;
1991}
1992
1993static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
1994 struct xdp_frame **frames, u32 flags)
1995{
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03001996 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03001997 struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03001998 struct rtnl_link_stats64 *percpu_stats;
1999 struct dpaa2_eth_fq *fq;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002000 struct dpaa2_fd *fds;
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002001 int enqueued, i, err;
Ioana Radulescud678be12019-03-01 17:47:24 +00002002
2003 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2004 return -EINVAL;
2005
2006 if (!netif_running(net_dev))
2007 return -ENETDOWN;
2008
Ioana Ciornei8665d972020-04-22 15:05:13 +03002009 fq = &priv->fq[smp_processor_id()];
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002010 xdp_redirect_fds = &fq->xdp_redirect_fds;
2011 fds = xdp_redirect_fds->fds;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002012
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002013 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002014
Ioana Ciornei8665d972020-04-22 15:05:13 +03002015 /* create a FD for each xdp_frame in the list received */
Ioana Radulescud678be12019-03-01 17:47:24 +00002016 for (i = 0; i < n; i++) {
Ioana Ciornei8665d972020-04-22 15:05:13 +03002017 err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2018 if (err)
2019 break;
2020 }
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002021 xdp_redirect_fds->num = i;
Ioana Radulescud678be12019-03-01 17:47:24 +00002022
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002023 /* enqueue all the frame descriptors */
2024 enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
Ioana Radulescud678be12019-03-01 17:47:24 +00002025
Ioana Ciornei8665d972020-04-22 15:05:13 +03002026 /* update statistics */
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002027 percpu_stats->tx_packets += enqueued;
2028 for (i = 0; i < enqueued; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002029 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002030 for (i = enqueued; i < n; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002031 xdp_return_frame_rx_napi(frames[i]);
2032
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002033 return enqueued;
Ioana Radulescud678be12019-03-01 17:47:24 +00002034}
2035
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002036static int update_xps(struct dpaa2_eth_priv *priv)
2037{
2038 struct net_device *net_dev = priv->net_dev;
2039 struct cpumask xps_mask;
2040 struct dpaa2_eth_fq *fq;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002041 int i, num_queues, netdev_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002042 int err = 0;
2043
2044 num_queues = dpaa2_eth_queue_count(priv);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002045 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002046
2047 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2048 * queues, so only process those
2049 */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002050 for (i = 0; i < netdev_queues; i++) {
2051 fq = &priv->fq[i % num_queues];
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002052
2053 cpumask_clear(&xps_mask);
2054 cpumask_set_cpu(fq->target_cpu, &xps_mask);
2055
2056 err = netif_set_xps_queue(net_dev, &xps_mask, i);
2057 if (err) {
2058 netdev_warn_once(net_dev, "Error setting XPS queue\n");
2059 break;
2060 }
2061 }
2062
2063 return err;
2064}
2065
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002066static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2067 enum tc_setup_type type, void *type_data)
2068{
2069 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2070 struct tc_mqprio_qopt *mqprio = type_data;
2071 u8 num_tc, num_queues;
2072 int i;
2073
2074 if (type != TC_SETUP_QDISC_MQPRIO)
Jesper Dangaard Brouerb89c1e62020-04-23 16:57:50 +02002075 return -EOPNOTSUPP;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002076
2077 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2078 num_queues = dpaa2_eth_queue_count(priv);
2079 num_tc = mqprio->num_tc;
2080
2081 if (num_tc == net_dev->num_tc)
2082 return 0;
2083
2084 if (num_tc > dpaa2_eth_tc_count(priv)) {
2085 netdev_err(net_dev, "Max %d traffic classes supported\n",
2086 dpaa2_eth_tc_count(priv));
Jesper Dangaard Brouerb89c1e62020-04-23 16:57:50 +02002087 return -EOPNOTSUPP;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002088 }
2089
2090 if (!num_tc) {
2091 netdev_reset_tc(net_dev);
2092 netif_set_real_num_tx_queues(net_dev, num_queues);
2093 goto out;
2094 }
2095
2096 netdev_set_num_tc(net_dev, num_tc);
2097 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2098
2099 for (i = 0; i < num_tc; i++)
2100 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2101
2102out:
2103 update_xps(priv);
2104
2105 return 0;
2106}
2107
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002108static const struct net_device_ops dpaa2_eth_ops = {
2109 .ndo_open = dpaa2_eth_open,
2110 .ndo_start_xmit = dpaa2_eth_tx,
2111 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002112 .ndo_set_mac_address = dpaa2_eth_set_addr,
2113 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002114 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2115 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08002116 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002117 .ndo_change_mtu = dpaa2_eth_change_mtu,
2118 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescud678be12019-03-01 17:47:24 +00002119 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002120 .ndo_setup_tc = dpaa2_eth_setup_tc,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002121};
2122
2123static void cdan_cb(struct dpaa2_io_notification_ctx *ctx)
2124{
2125 struct dpaa2_eth_channel *ch;
2126
2127 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002128
2129 /* Update NAPI statistics */
2130 ch->stats.cdan++;
2131
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002132 napi_schedule_irqoff(&ch->napi);
2133}
2134
2135/* Allocate and configure a DPCON object */
2136static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv)
2137{
2138 struct fsl_mc_device *dpcon;
2139 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002140 int err;
2141
2142 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2143 FSL_MC_POOL_DPCON, &dpcon);
2144 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002145 if (err == -ENXIO)
2146 err = -EPROBE_DEFER;
2147 else
2148 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2149 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002150 }
2151
2152 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2153 if (err) {
2154 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002155 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002156 }
2157
2158 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2159 if (err) {
2160 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002161 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002162 }
2163
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002164 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2165 if (err) {
2166 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002167 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002168 }
2169
2170 return dpcon;
2171
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002172close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002173 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002174free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002175 fsl_mc_object_free(dpcon);
2176
2177 return NULL;
2178}
2179
2180static void free_dpcon(struct dpaa2_eth_priv *priv,
2181 struct fsl_mc_device *dpcon)
2182{
2183 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2184 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2185 fsl_mc_object_free(dpcon);
2186}
2187
2188static struct dpaa2_eth_channel *
2189alloc_channel(struct dpaa2_eth_priv *priv)
2190{
2191 struct dpaa2_eth_channel *channel;
2192 struct dpcon_attr attr;
2193 struct device *dev = priv->net_dev->dev.parent;
2194 int err;
2195
2196 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2197 if (!channel)
2198 return NULL;
2199
2200 channel->dpcon = setup_dpcon(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002201 if (IS_ERR_OR_NULL(channel->dpcon)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002202 err = PTR_ERR_OR_ZERO(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002203 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002204 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002205
2206 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2207 &attr);
2208 if (err) {
2209 dev_err(dev, "dpcon_get_attributes() failed\n");
2210 goto err_get_attr;
2211 }
2212
2213 channel->dpcon_id = attr.id;
2214 channel->ch_id = attr.qbman_ch_id;
2215 channel->priv = priv;
2216
2217 return channel;
2218
2219err_get_attr:
2220 free_dpcon(priv, channel->dpcon);
2221err_setup:
2222 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002223 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002224}
2225
2226static void free_channel(struct dpaa2_eth_priv *priv,
2227 struct dpaa2_eth_channel *channel)
2228{
2229 free_dpcon(priv, channel->dpcon);
2230 kfree(channel);
2231}
2232
2233/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2234 * and register data availability notifications
2235 */
2236static int setup_dpio(struct dpaa2_eth_priv *priv)
2237{
2238 struct dpaa2_io_notification_ctx *nctx;
2239 struct dpaa2_eth_channel *channel;
2240 struct dpcon_notification_cfg dpcon_notif_cfg;
2241 struct device *dev = priv->net_dev->dev.parent;
2242 int i, err;
2243
2244 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2245 * many cores as possible, so we need one channel for each core
2246 * (unless there's fewer queues than cores, in which case the extra
2247 * channels would be wasted).
2248 * Allocate one channel per core and register it to the core's
2249 * affine DPIO. If not enough channels are available for all cores
2250 * or if some cores don't have an affine DPIO, there will be no
2251 * ingress frame processing on those cores.
2252 */
2253 cpumask_clear(&priv->dpio_cpumask);
2254 for_each_online_cpu(i) {
2255 /* Try to allocate a channel */
2256 channel = alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002257 if (IS_ERR_OR_NULL(channel)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002258 err = PTR_ERR_OR_ZERO(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002259 if (err != -EPROBE_DEFER)
2260 dev_info(dev,
2261 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002262 goto err_alloc_ch;
2263 }
2264
2265 priv->channel[priv->num_channels] = channel;
2266
2267 nctx = &channel->nctx;
2268 nctx->is_cdan = 1;
2269 nctx->cb = cdan_cb;
2270 nctx->id = channel->ch_id;
2271 nctx->desired_cpu = i;
2272
2273 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06002274 channel->dpio = dpaa2_io_service_select(i);
Ioana Ciornei47441f72018-12-10 16:50:19 +00002275 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002276 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002277 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002278 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002279 * none available for next cores either. Signal we want
2280 * to retry later, in case the DPIO devices weren't
2281 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002282 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002283 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002284 goto err_service_reg;
2285 }
2286
2287 /* Register DPCON notification with MC */
2288 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2289 dpcon_notif_cfg.priority = 0;
2290 dpcon_notif_cfg.user_ctx = nctx->qman64;
2291 err = dpcon_set_notification(priv->mc_io, 0,
2292 channel->dpcon->mc_handle,
2293 &dpcon_notif_cfg);
2294 if (err) {
2295 dev_err(dev, "dpcon_set_notification failed()\n");
2296 goto err_set_cdan;
2297 }
2298
2299 /* If we managed to allocate a channel and also found an affine
2300 * DPIO for this core, add it to the final mask
2301 */
2302 cpumask_set_cpu(i, &priv->dpio_cpumask);
2303 priv->num_channels++;
2304
2305 /* Stop if we already have enough channels to accommodate all
2306 * RX and TX conf queues
2307 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00002308 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002309 break;
2310 }
2311
2312 return 0;
2313
2314err_set_cdan:
Ioana Ciornei47441f72018-12-10 16:50:19 +00002315 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002316err_service_reg:
2317 free_channel(priv, channel);
2318err_alloc_ch:
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002319 if (err == -EPROBE_DEFER) {
2320 for (i = 0; i < priv->num_channels; i++) {
2321 channel = priv->channel[i];
2322 nctx = &channel->nctx;
2323 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
2324 free_channel(priv, channel);
2325 }
2326 priv->num_channels = 0;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002327 return err;
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002328 }
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002329
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002330 if (cpumask_empty(&priv->dpio_cpumask)) {
2331 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002332 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002333 }
2334
2335 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2336 cpumask_pr_args(&priv->dpio_cpumask));
2337
2338 return 0;
2339}
2340
2341static void free_dpio(struct dpaa2_eth_priv *priv)
2342{
Ioana Ciornei47441f72018-12-10 16:50:19 +00002343 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002344 struct dpaa2_eth_channel *ch;
Ioana Ciornei47441f72018-12-10 16:50:19 +00002345 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002346
2347 /* deregister CDAN notifications and free channels */
2348 for (i = 0; i < priv->num_channels; i++) {
2349 ch = priv->channel[i];
Ioana Ciornei47441f72018-12-10 16:50:19 +00002350 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002351 free_channel(priv, ch);
2352 }
2353}
2354
2355static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv,
2356 int cpu)
2357{
2358 struct device *dev = priv->net_dev->dev.parent;
2359 int i;
2360
2361 for (i = 0; i < priv->num_channels; i++)
2362 if (priv->channel[i]->nctx.desired_cpu == cpu)
2363 return priv->channel[i];
2364
2365 /* We should never get here. Issue a warning and return
2366 * the first channel, because it's still better than nothing
2367 */
2368 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2369
2370 return priv->channel[0];
2371}
2372
2373static void set_fq_affinity(struct dpaa2_eth_priv *priv)
2374{
2375 struct device *dev = priv->net_dev->dev.parent;
2376 struct dpaa2_eth_fq *fq;
2377 int rx_cpu, txc_cpu;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002378 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002379
2380 /* For each FQ, pick one channel/CPU to deliver frames to.
2381 * This may well change at runtime, either through irqbalance or
2382 * through direct user intervention.
2383 */
2384 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2385
2386 for (i = 0; i < priv->num_fqs; i++) {
2387 fq = &priv->fq[i];
2388 switch (fq->type) {
2389 case DPAA2_RX_FQ:
2390 fq->target_cpu = rx_cpu;
2391 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2392 if (rx_cpu >= nr_cpu_ids)
2393 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2394 break;
2395 case DPAA2_TX_CONF_FQ:
2396 fq->target_cpu = txc_cpu;
2397 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2398 if (txc_cpu >= nr_cpu_ids)
2399 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2400 break;
2401 default:
2402 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2403 }
2404 fq->channel = get_affine_channel(priv, fq->target_cpu);
2405 }
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002406
2407 update_xps(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002408}
2409
2410static void setup_fqs(struct dpaa2_eth_priv *priv)
2411{
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002412 int i, j;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002413
2414 /* We have one TxConf FQ per Tx flow.
2415 * The number of Tx and Rx queues is the same.
2416 * Tx queues come first in the fq array.
2417 */
2418 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2419 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2420 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2421 priv->fq[priv->num_fqs++].flowid = (u16)i;
2422 }
2423
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002424 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2425 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2426 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2427 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2428 priv->fq[priv->num_fqs].tc = (u8)j;
2429 priv->fq[priv->num_fqs++].flowid = (u16)i;
2430 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002431 }
2432
2433 /* For each FQ, decide on which core to process incoming frames */
2434 set_fq_affinity(priv);
2435}
2436
2437/* Allocate and configure one buffer pool for each interface */
2438static int setup_dpbp(struct dpaa2_eth_priv *priv)
2439{
2440 int err;
2441 struct fsl_mc_device *dpbp_dev;
2442 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002443 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002444
2445 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2446 &dpbp_dev);
2447 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002448 if (err == -ENXIO)
2449 err = -EPROBE_DEFER;
2450 else
2451 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002452 return err;
2453 }
2454
2455 priv->dpbp_dev = dpbp_dev;
2456
2457 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2458 &dpbp_dev->mc_handle);
2459 if (err) {
2460 dev_err(dev, "dpbp_open() failed\n");
2461 goto err_open;
2462 }
2463
Ioana Radulescud00defe2017-06-06 10:00:32 -05002464 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2465 if (err) {
2466 dev_err(dev, "dpbp_reset() failed\n");
2467 goto err_reset;
2468 }
2469
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002470 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2471 if (err) {
2472 dev_err(dev, "dpbp_enable() failed\n");
2473 goto err_enable;
2474 }
2475
2476 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002477 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002478 if (err) {
2479 dev_err(dev, "dpbp_get_attributes() failed\n");
2480 goto err_get_attr;
2481 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002482 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002483
2484 return 0;
2485
2486err_get_attr:
2487 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2488err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002489err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002490 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2491err_open:
2492 fsl_mc_object_free(dpbp_dev);
2493
2494 return err;
2495}
2496
2497static void free_dpbp(struct dpaa2_eth_priv *priv)
2498{
2499 drain_pool(priv);
2500 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2501 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2502 fsl_mc_object_free(priv->dpbp_dev);
2503}
2504
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002505static int set_buffer_layout(struct dpaa2_eth_priv *priv)
2506{
2507 struct device *dev = priv->net_dev->dev.parent;
2508 struct dpni_buffer_layout buf_layout = {0};
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002509 u16 rx_buf_align;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002510 int err;
2511
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002512 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2513 * version, this number is not always provided correctly on rev1.
2514 * We need to check for both alternatives in this situation.
2515 */
2516 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2517 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002518 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002519 else
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002520 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002521
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03002522 /* We need to ensure that the buffer size seen by WRIOP is a multiple
2523 * of 64 or 256 bytes depending on the WRIOP version.
2524 */
2525 priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
2526
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002527 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002528 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002529 buf_layout.pass_timestamp = true;
2530 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2531 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002532 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2533 DPNI_QUEUE_TX, &buf_layout);
2534 if (err) {
2535 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2536 return err;
2537 }
2538
2539 /* tx-confirm buffer */
Ioana Radulescu859f9982018-04-26 18:23:47 +08002540 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002541 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2542 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2543 if (err) {
2544 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2545 return err;
2546 }
2547
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002548 /* Now that we've set our tx buffer layout, retrieve the minimum
2549 * required tx data offset.
2550 */
2551 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2552 &priv->tx_data_offset);
2553 if (err) {
2554 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2555 return err;
2556 }
2557
2558 if ((priv->tx_data_offset % 64) != 0)
2559 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2560 priv->tx_data_offset);
2561
2562 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06002563 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002564 buf_layout.pass_parser_result = true;
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002565 buf_layout.data_align = rx_buf_align;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002566 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2567 buf_layout.private_data_size = 0;
2568 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2569 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2570 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08002571 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2572 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002573 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2574 DPNI_QUEUE_RX, &buf_layout);
2575 if (err) {
2576 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2577 return err;
2578 }
2579
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002580 return 0;
2581}
2582
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002583#define DPNI_ENQUEUE_FQID_VER_MAJOR 7
2584#define DPNI_ENQUEUE_FQID_VER_MINOR 9
2585
2586static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2587 struct dpaa2_eth_fq *fq,
Ioana Ciornei48c04812020-04-22 15:05:10 +03002588 struct dpaa2_fd *fd, u8 prio,
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002589 u32 num_frames __always_unused,
Ioana Ciornei48c04812020-04-22 15:05:10 +03002590 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002591{
Ioana Ciornei48c04812020-04-22 15:05:10 +03002592 int err;
2593
2594 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
2595 priv->tx_qdid, prio,
2596 fq->tx_qdbin, fd);
2597 if (!err && frames_enqueued)
2598 *frames_enqueued = 1;
2599 return err;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002600}
2601
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002602static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
2603 struct dpaa2_eth_fq *fq,
2604 struct dpaa2_fd *fd,
2605 u8 prio, u32 num_frames,
2606 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002607{
Ioana Ciornei48c04812020-04-22 15:05:10 +03002608 int err;
2609
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002610 err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
2611 fq->tx_fqid[prio],
2612 fd, num_frames);
2613
2614 if (err == 0)
2615 return -EBUSY;
2616
2617 if (frames_enqueued)
2618 *frames_enqueued = err;
2619 return 0;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002620}
2621
2622static void set_enqueue_mode(struct dpaa2_eth_priv *priv)
2623{
2624 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2625 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2626 priv->enqueue = dpaa2_eth_enqueue_qd;
2627 else
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002628 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002629}
2630
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03002631static int set_pause(struct dpaa2_eth_priv *priv)
2632{
2633 struct device *dev = priv->net_dev->dev.parent;
2634 struct dpni_link_cfg link_cfg = {0};
2635 int err;
2636
2637 /* Get the default link options so we don't override other flags */
2638 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2639 if (err) {
2640 dev_err(dev, "dpni_get_link_cfg() failed\n");
2641 return err;
2642 }
2643
2644 /* By default, enable both Rx and Tx pause frames */
2645 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
2646 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
2647 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2648 if (err) {
2649 dev_err(dev, "dpni_set_link_cfg() failed\n");
2650 return err;
2651 }
2652
2653 priv->link_state.options = link_cfg.options;
2654
2655 return 0;
2656}
2657
Ioana Radulescua690af4f2019-10-16 10:36:23 +03002658static void update_tx_fqids(struct dpaa2_eth_priv *priv)
2659{
2660 struct dpni_queue_id qid = {0};
2661 struct dpaa2_eth_fq *fq;
2662 struct dpni_queue queue;
2663 int i, j, err;
2664
2665 /* We only use Tx FQIDs for FQID-based enqueue, so check
2666 * if DPNI version supports it before updating FQIDs
2667 */
2668 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2669 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2670 return;
2671
2672 for (i = 0; i < priv->num_fqs; i++) {
2673 fq = &priv->fq[i];
2674 if (fq->type != DPAA2_TX_CONF_FQ)
2675 continue;
2676 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2677 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2678 DPNI_QUEUE_TX, j, fq->flowid,
2679 &queue, &qid);
2680 if (err)
2681 goto out_err;
2682
2683 fq->tx_fqid[j] = qid.fqid;
2684 if (fq->tx_fqid[j] == 0)
2685 goto out_err;
2686 }
2687 }
2688
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002689 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Radulescua690af4f2019-10-16 10:36:23 +03002690
2691 return;
2692
2693out_err:
2694 netdev_info(priv->net_dev,
2695 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
2696 priv->enqueue = dpaa2_eth_enqueue_qd;
2697}
2698
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03002699/* Configure ingress classification based on VLAN PCP */
2700static int set_vlan_qos(struct dpaa2_eth_priv *priv)
2701{
2702 struct device *dev = priv->net_dev->dev.parent;
2703 struct dpkg_profile_cfg kg_cfg = {0};
2704 struct dpni_qos_tbl_cfg qos_cfg = {0};
2705 struct dpni_rule_cfg key_params;
2706 void *dma_mem, *key, *mask;
2707 u8 key_size = 2; /* VLAN TCI field */
2708 int i, pcp, err;
2709
2710 /* VLAN-based classification only makes sense if we have multiple
2711 * traffic classes.
2712 * Also, we need to extract just the 3-bit PCP field from the VLAN
2713 * header and we can only do that by using a mask
2714 */
2715 if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
2716 dev_dbg(dev, "VLAN-based QoS classification not supported\n");
2717 return -EOPNOTSUPP;
2718 }
2719
2720 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
2721 if (!dma_mem)
2722 return -ENOMEM;
2723
2724 kg_cfg.num_extracts = 1;
2725 kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
2726 kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
2727 kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
2728 kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
2729
2730 err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
2731 if (err) {
2732 dev_err(dev, "dpni_prepare_key_cfg failed\n");
2733 goto out_free_tbl;
2734 }
2735
2736 /* set QoS table */
2737 qos_cfg.default_tc = 0;
2738 qos_cfg.discard_on_miss = 0;
2739 qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
2740 DPAA2_CLASSIFIER_DMA_SIZE,
2741 DMA_TO_DEVICE);
2742 if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
2743 dev_err(dev, "QoS table DMA mapping failed\n");
2744 err = -ENOMEM;
2745 goto out_free_tbl;
2746 }
2747
2748 err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
2749 if (err) {
2750 dev_err(dev, "dpni_set_qos_table failed\n");
2751 goto out_unmap_tbl;
2752 }
2753
2754 /* Add QoS table entries */
2755 key = kzalloc(key_size * 2, GFP_KERNEL);
2756 if (!key) {
2757 err = -ENOMEM;
2758 goto out_unmap_tbl;
2759 }
2760 mask = key + key_size;
2761 *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
2762
2763 key_params.key_iova = dma_map_single(dev, key, key_size * 2,
2764 DMA_TO_DEVICE);
2765 if (dma_mapping_error(dev, key_params.key_iova)) {
2766 dev_err(dev, "Qos table entry DMA mapping failed\n");
2767 err = -ENOMEM;
2768 goto out_free_key;
2769 }
2770
2771 key_params.mask_iova = key_params.key_iova + key_size;
2772 key_params.key_size = key_size;
2773
2774 /* We add rules for PCP-based distribution starting with highest
2775 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
2776 * classes to accommodate all priority levels, the lowest ones end up
2777 * on TC 0 which was configured as default
2778 */
2779 for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
2780 *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
2781 dma_sync_single_for_device(dev, key_params.key_iova,
2782 key_size * 2, DMA_TO_DEVICE);
2783
2784 err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
2785 &key_params, i, i);
2786 if (err) {
2787 dev_err(dev, "dpni_add_qos_entry failed\n");
2788 dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
2789 goto out_unmap_key;
2790 }
2791 }
2792
2793 priv->vlan_cls_enabled = true;
2794
2795 /* Table and key memory is not persistent, clean everything up after
2796 * configuration is finished
2797 */
2798out_unmap_key:
2799 dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
2800out_free_key:
2801 kfree(key);
2802out_unmap_tbl:
2803 dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
2804 DMA_TO_DEVICE);
2805out_free_tbl:
2806 kfree(dma_mem);
2807
2808 return err;
2809}
2810
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002811/* Configure the DPNI object this interface is associated with */
2812static int setup_dpni(struct fsl_mc_device *ls_dev)
2813{
2814 struct device *dev = &ls_dev->dev;
2815 struct dpaa2_eth_priv *priv;
2816 struct net_device *net_dev;
2817 int err;
2818
2819 net_dev = dev_get_drvdata(dev);
2820 priv = netdev_priv(net_dev);
2821
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002822 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05002823 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002824 if (err) {
2825 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002826 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002827 }
2828
Ioana Radulescu311cffa2018-03-23 08:44:09 -05002829 /* Check if we can work with this DPNI object */
2830 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
2831 &priv->dpni_ver_minor);
2832 if (err) {
2833 dev_err(dev, "dpni_get_api_version() failed\n");
2834 goto close;
2835 }
2836 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
2837 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
2838 priv->dpni_ver_major, priv->dpni_ver_minor,
2839 DPNI_VER_MAJOR, DPNI_VER_MINOR);
2840 err = -ENOTSUPP;
2841 goto close;
2842 }
2843
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002844 ls_dev->mc_io = priv->mc_io;
2845 ls_dev->mc_handle = priv->mc_token;
2846
2847 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2848 if (err) {
2849 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002850 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002851 }
2852
2853 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
2854 &priv->dpni_attrs);
2855 if (err) {
2856 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002857 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002858 }
2859
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002860 err = set_buffer_layout(priv);
2861 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002862 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002863
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002864 set_enqueue_mode(priv);
2865
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03002866 /* Enable pause frame support */
2867 if (dpaa2_eth_has_pause_support(priv)) {
2868 err = set_pause(priv);
2869 if (err)
2870 goto close;
2871 }
2872
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03002873 err = set_vlan_qos(priv);
2874 if (err && err != -EOPNOTSUPP)
2875 goto close;
2876
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002877 priv->cls_rules = devm_kzalloc(dev, sizeof(struct dpaa2_eth_cls_rule) *
2878 dpaa2_eth_fs_count(priv), GFP_KERNEL);
Wei Yongjun97fff7c2020-04-27 10:43:22 +00002879 if (!priv->cls_rules) {
2880 err = -ENOMEM;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002881 goto close;
Wei Yongjun97fff7c2020-04-27 10:43:22 +00002882 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03002883
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002884 return 0;
2885
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002886close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002887 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002888
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002889 return err;
2890}
2891
2892static void free_dpni(struct dpaa2_eth_priv *priv)
2893{
2894 int err;
2895
2896 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2897 if (err)
2898 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
2899 err);
2900
2901 dpni_close(priv->mc_io, 0, priv->mc_token);
2902}
2903
2904static int setup_rx_flow(struct dpaa2_eth_priv *priv,
2905 struct dpaa2_eth_fq *fq)
2906{
2907 struct device *dev = priv->net_dev->dev.parent;
2908 struct dpni_queue queue;
2909 struct dpni_queue_id qid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002910 int err;
2911
2912 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002913 DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002914 if (err) {
2915 dev_err(dev, "dpni_get_queue(RX) failed\n");
2916 return err;
2917 }
2918
2919 fq->fqid = qid.fqid;
2920
2921 queue.destination.id = fq->channel->dpcon_id;
2922 queue.destination.type = DPNI_DEST_DPCON;
2923 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002924 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002925 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002926 DPNI_QUEUE_RX, fq->tc, fq->flowid,
Ioana Radulescu16fa1cf2019-05-23 17:38:22 +03002927 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002928 &queue);
2929 if (err) {
2930 dev_err(dev, "dpni_set_queue(RX) failed\n");
2931 return err;
2932 }
2933
Ioana Radulescud678be12019-03-01 17:47:24 +00002934 /* xdp_rxq setup */
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002935 /* only once for each channel */
2936 if (fq->tc > 0)
2937 return 0;
2938
Ioana Radulescud678be12019-03-01 17:47:24 +00002939 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
2940 fq->flowid);
2941 if (err) {
2942 dev_err(dev, "xdp_rxq_info_reg failed\n");
2943 return err;
2944 }
2945
2946 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
2947 MEM_TYPE_PAGE_ORDER0, NULL);
2948 if (err) {
2949 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
2950 return err;
2951 }
2952
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002953 return 0;
2954}
2955
2956static int setup_tx_flow(struct dpaa2_eth_priv *priv,
2957 struct dpaa2_eth_fq *fq)
2958{
2959 struct device *dev = priv->net_dev->dev.parent;
2960 struct dpni_queue queue;
2961 struct dpni_queue_id qid;
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002962 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002963
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002964 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
2965 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2966 DPNI_QUEUE_TX, i, fq->flowid,
2967 &queue, &qid);
2968 if (err) {
2969 dev_err(dev, "dpni_get_queue(TX) failed\n");
2970 return err;
2971 }
2972 fq->tx_fqid[i] = qid.fqid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002973 }
2974
Ioana Radulescu15c87f62019-06-11 14:50:02 +03002975 /* All Tx queues belonging to the same flowid have the same qdbin */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002976 fq->tx_qdbin = qid.qdbin;
2977
2978 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2979 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2980 &queue, &qid);
2981 if (err) {
2982 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
2983 return err;
2984 }
2985
2986 fq->fqid = qid.fqid;
2987
2988 queue.destination.id = fq->channel->dpcon_id;
2989 queue.destination.type = DPNI_DEST_DPCON;
2990 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06002991 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002992 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2993 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2994 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2995 &queue);
2996 if (err) {
2997 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
2998 return err;
2999 }
3000
3001 return 0;
3002}
3003
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003004/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003005static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003006 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003007 /* L2 header */
3008 .rxnfc_field = RXH_L2DA,
3009 .cls_prot = NET_PROT_ETH,
3010 .cls_field = NH_FLD_ETH_DA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003011 .id = DPAA2_ETH_DIST_ETHDST,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003012 .size = 6,
3013 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003014 .cls_prot = NET_PROT_ETH,
3015 .cls_field = NH_FLD_ETH_SA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003016 .id = DPAA2_ETH_DIST_ETHSRC,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003017 .size = 6,
3018 }, {
3019 /* This is the last ethertype field parsed:
3020 * depending on frame format, it can be the MAC ethertype
3021 * or the VLAN etype.
3022 */
3023 .cls_prot = NET_PROT_ETH,
3024 .cls_field = NH_FLD_ETH_TYPE,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003025 .id = DPAA2_ETH_DIST_ETHTYPE,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003026 .size = 2,
3027 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003028 /* VLAN header */
3029 .rxnfc_field = RXH_VLAN,
3030 .cls_prot = NET_PROT_VLAN,
3031 .cls_field = NH_FLD_VLAN_TCI,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003032 .id = DPAA2_ETH_DIST_VLAN,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003033 .size = 2,
3034 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003035 /* IP header */
3036 .rxnfc_field = RXH_IP_SRC,
3037 .cls_prot = NET_PROT_IP,
3038 .cls_field = NH_FLD_IP_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003039 .id = DPAA2_ETH_DIST_IPSRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003040 .size = 4,
3041 }, {
3042 .rxnfc_field = RXH_IP_DST,
3043 .cls_prot = NET_PROT_IP,
3044 .cls_field = NH_FLD_IP_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003045 .id = DPAA2_ETH_DIST_IPDST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003046 .size = 4,
3047 }, {
3048 .rxnfc_field = RXH_L3_PROTO,
3049 .cls_prot = NET_PROT_IP,
3050 .cls_field = NH_FLD_IP_PROTO,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003051 .id = DPAA2_ETH_DIST_IPPROTO,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003052 .size = 1,
3053 }, {
3054 /* Using UDP ports, this is functionally equivalent to raw
3055 * byte pairs from L4 header.
3056 */
3057 .rxnfc_field = RXH_L4_B_0_1,
3058 .cls_prot = NET_PROT_UDP,
3059 .cls_field = NH_FLD_UDP_PORT_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003060 .id = DPAA2_ETH_DIST_L4SRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003061 .size = 2,
3062 }, {
3063 .rxnfc_field = RXH_L4_B_2_3,
3064 .cls_prot = NET_PROT_UDP,
3065 .cls_field = NH_FLD_UDP_PORT_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003066 .id = DPAA2_ETH_DIST_L4DST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003067 .size = 2,
3068 },
3069};
3070
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003071/* Configure the Rx hash key using the legacy API */
3072static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3073{
3074 struct device *dev = priv->net_dev->dev.parent;
3075 struct dpni_rx_tc_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003076 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003077
3078 memset(&dist_cfg, 0, sizeof(dist_cfg));
3079
3080 dist_cfg.key_cfg_iova = key;
3081 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3082 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3083
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003084 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3085 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3086 i, &dist_cfg);
3087 if (err) {
3088 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3089 break;
3090 }
3091 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003092
3093 return err;
3094}
3095
3096/* Configure the Rx hash key using the new API */
3097static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3098{
3099 struct device *dev = priv->net_dev->dev.parent;
3100 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003101 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003102
3103 memset(&dist_cfg, 0, sizeof(dist_cfg));
3104
3105 dist_cfg.key_cfg_iova = key;
3106 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3107 dist_cfg.enable = 1;
3108
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003109 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3110 dist_cfg.tc = i;
3111 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3112 &dist_cfg);
3113 if (err) {
3114 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3115 break;
3116 }
3117 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003118
3119 return err;
3120}
3121
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003122/* Configure the Rx flow classification key */
3123static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3124{
3125 struct device *dev = priv->net_dev->dev.parent;
3126 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003127 int i, err = 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003128
3129 memset(&dist_cfg, 0, sizeof(dist_cfg));
3130
3131 dist_cfg.key_cfg_iova = key;
3132 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3133 dist_cfg.enable = 1;
3134
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003135 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3136 dist_cfg.tc = i;
3137 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3138 &dist_cfg);
3139 if (err) {
3140 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3141 break;
3142 }
3143 }
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003144
3145 return err;
3146}
3147
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003148/* Size of the Rx flow classification key */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003149int dpaa2_eth_cls_key_size(u64 fields)
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003150{
3151 int i, size = 0;
3152
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003153 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3154 if (!(fields & dist_fields[i].id))
3155 continue;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003156 size += dist_fields[i].size;
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003157 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003158
3159 return size;
3160}
3161
3162/* Offset of header field in Rx classification key */
3163int dpaa2_eth_cls_fld_off(int prot, int field)
3164{
3165 int i, off = 0;
3166
3167 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3168 if (dist_fields[i].cls_prot == prot &&
3169 dist_fields[i].cls_field == field)
3170 return off;
3171 off += dist_fields[i].size;
3172 }
3173
3174 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3175 return 0;
3176}
3177
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003178/* Prune unused fields from the classification rule.
3179 * Used when masking is not supported
3180 */
3181void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3182{
3183 int off = 0, new_off = 0;
3184 int i, size;
3185
3186 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3187 size = dist_fields[i].size;
3188 if (dist_fields[i].id & fields) {
3189 memcpy(key_mem + new_off, key_mem + off, size);
3190 new_off += size;
3191 }
3192 off += size;
3193 }
3194}
3195
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003196/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003197 * flags is a combination of RXH_ bits
3198 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00003199static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
3200 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003201{
3202 struct device *dev = net_dev->dev.parent;
3203 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3204 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003205 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003206 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003207 u8 *dma_mem;
3208 int i;
3209 int err = 0;
3210
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003211 memset(&cls_cfg, 0, sizeof(cls_cfg));
3212
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003213 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003214 struct dpkg_extract *key =
3215 &cls_cfg.extracts[cls_cfg.num_extracts];
3216
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003217 /* For both Rx hashing and classification keys
3218 * we set only the selected fields.
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003219 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003220 if (!(flags & dist_fields[i].id))
3221 continue;
3222 if (type == DPAA2_ETH_RX_DIST_HASH)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003223 rx_hash_fields |= dist_fields[i].rxnfc_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003224
3225 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
3226 dev_err(dev, "error adding key extraction rule, too many rules?\n");
3227 return -E2BIG;
3228 }
3229
3230 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003231 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003232 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003233 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003234 cls_cfg.num_extracts++;
3235 }
3236
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05003237 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003238 if (!dma_mem)
3239 return -ENOMEM;
3240
3241 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3242 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003243 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003244 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003245 }
3246
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003247 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003248 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3249 DMA_TO_DEVICE);
3250 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003251 dev_err(dev, "DMA mapping failed\n");
3252 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003253 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003254 }
3255
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003256 if (type == DPAA2_ETH_RX_DIST_HASH) {
3257 if (dpaa2_eth_has_legacy_dist(priv))
3258 err = config_legacy_hash_key(priv, key_iova);
3259 else
3260 err = config_hash_key(priv, key_iova);
3261 } else {
3262 err = config_cls_key(priv, key_iova);
3263 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003264
3265 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3266 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003267 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003268 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003269
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003270free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003271 kfree(dma_mem);
3272 return err;
3273}
3274
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003275int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3276{
3277 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003278 u64 key = 0;
3279 int i;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003280
3281 if (!dpaa2_eth_hash_enabled(priv))
3282 return -EOPNOTSUPP;
3283
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003284 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3285 if (dist_fields[i].rxnfc_field & flags)
3286 key |= dist_fields[i].id;
3287
3288 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003289}
3290
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003291int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3292{
3293 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3294}
3295
3296static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003297{
3298 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003299 int err;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003300
3301 /* Check if we actually support Rx flow classification */
3302 if (dpaa2_eth_has_legacy_dist(priv)) {
3303 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3304 return -EOPNOTSUPP;
3305 }
3306
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003307 if (!dpaa2_eth_fs_enabled(priv)) {
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003308 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3309 return -EOPNOTSUPP;
3310 }
3311
3312 if (!dpaa2_eth_hash_enabled(priv)) {
3313 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3314 return -EOPNOTSUPP;
3315 }
3316
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003317 /* If there is no support for masking in the classification table,
3318 * we don't set a default key, as it will depend on the rules
3319 * added by the user at runtime.
3320 */
3321 if (!dpaa2_eth_fs_mask_enabled(priv))
3322 goto out;
3323
3324 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003325 if (err)
3326 return err;
3327
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003328out:
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003329 priv->rx_cls_enabled = 1;
3330
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003331 return 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003332}
3333
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003334/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3335 * frame queues and channels
3336 */
3337static int bind_dpni(struct dpaa2_eth_priv *priv)
3338{
3339 struct net_device *net_dev = priv->net_dev;
3340 struct device *dev = net_dev->dev.parent;
3341 struct dpni_pools_cfg pools_params;
3342 struct dpni_error_cfg err_cfg;
3343 int err = 0;
3344 int i;
3345
3346 pools_params.num_dpbp = 1;
3347 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3348 pools_params.pools[0].backup_pool = 0;
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03003349 pools_params.pools[0].buffer_size = priv->rx_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003350 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3351 if (err) {
3352 dev_err(dev, "dpni_set_pools() failed\n");
3353 return err;
3354 }
3355
Ioana Radulescu227686b2018-07-27 09:12:59 -05003356 /* have the interface implicitly distribute traffic based on
3357 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003358 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05003359 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003360 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05003361 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003362
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003363 /* Configure the flow classification key; it includes all
3364 * supported header fields and cannot be modified at runtime
3365 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003366 err = dpaa2_eth_set_default_cls(priv);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003367 if (err && err != -EOPNOTSUPP)
3368 dev_err(dev, "Failed to configure Rx classification key\n");
3369
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003370 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05003371 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003372 err_cfg.set_frame_annotation = 1;
3373 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3374 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3375 &err_cfg);
3376 if (err) {
3377 dev_err(dev, "dpni_set_errors_behavior failed\n");
3378 return err;
3379 }
3380
3381 /* Configure Rx and Tx conf queues to generate CDANs */
3382 for (i = 0; i < priv->num_fqs; i++) {
3383 switch (priv->fq[i].type) {
3384 case DPAA2_RX_FQ:
3385 err = setup_rx_flow(priv, &priv->fq[i]);
3386 break;
3387 case DPAA2_TX_CONF_FQ:
3388 err = setup_tx_flow(priv, &priv->fq[i]);
3389 break;
3390 default:
3391 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3392 return -EINVAL;
3393 }
3394 if (err)
3395 return err;
3396 }
3397
3398 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3399 DPNI_QUEUE_TX, &priv->tx_qdid);
3400 if (err) {
3401 dev_err(dev, "dpni_get_qdid() failed\n");
3402 return err;
3403 }
3404
3405 return 0;
3406}
3407
3408/* Allocate rings for storing incoming frame descriptors */
3409static int alloc_rings(struct dpaa2_eth_priv *priv)
3410{
3411 struct net_device *net_dev = priv->net_dev;
3412 struct device *dev = net_dev->dev.parent;
3413 int i;
3414
3415 for (i = 0; i < priv->num_channels; i++) {
3416 priv->channel[i]->store =
3417 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3418 if (!priv->channel[i]->store) {
3419 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3420 goto err_ring;
3421 }
3422 }
3423
3424 return 0;
3425
3426err_ring:
3427 for (i = 0; i < priv->num_channels; i++) {
3428 if (!priv->channel[i]->store)
3429 break;
3430 dpaa2_io_store_destroy(priv->channel[i]->store);
3431 }
3432
3433 return -ENOMEM;
3434}
3435
3436static void free_rings(struct dpaa2_eth_priv *priv)
3437{
3438 int i;
3439
3440 for (i = 0; i < priv->num_channels; i++)
3441 dpaa2_io_store_destroy(priv->channel[i]->store);
3442}
3443
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003444static int set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003445{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003446 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003447 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003448 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003449 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003450
3451 /* Get firmware address, if any */
3452 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3453 if (err) {
3454 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3455 return err;
3456 }
3457
3458 /* Get DPNI attributes address, if any */
3459 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3460 dpni_mac_addr);
3461 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003462 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003463 return err;
3464 }
3465
3466 /* First check if firmware has any address configured by bootloader */
3467 if (!is_zero_ether_addr(mac_addr)) {
3468 /* If the DPMAC addr != DPNI addr, update it */
3469 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
3470 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
3471 priv->mc_token,
3472 mac_addr);
3473 if (err) {
3474 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3475 return err;
3476 }
3477 }
3478 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
3479 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003480 /* No MAC address configured, fill in net_dev->dev_addr
3481 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003482 */
3483 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003484 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
3485
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003486 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3487 net_dev->dev_addr);
3488 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003489 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003490 return err;
3491 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003492
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003493 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
3494 * practical purposes, this will be our "permanent" mac address,
3495 * at least until the next reboot. This move will also permit
3496 * register_netdevice() to properly fill up net_dev->perm_addr.
3497 */
3498 net_dev->addr_assign_type = NET_ADDR_PERM;
3499 } else {
3500 /* NET_ADDR_PERM is default, all we have to do is
3501 * fill in the device addr.
3502 */
3503 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
3504 }
3505
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003506 return 0;
3507}
3508
3509static int netdev_init(struct net_device *net_dev)
3510{
3511 struct device *dev = net_dev->dev.parent;
3512 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003513 u32 options = priv->dpni_attrs.options;
3514 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003515 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003516 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003517 int err;
3518
3519 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003520 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003521
3522 err = set_mac_addr(priv);
3523 if (err)
3524 return err;
3525
3526 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003527 eth_broadcast_addr(bcast_addr);
3528 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3529 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003530 dev_err(dev, "dpni_add_mac_addr() failed\n");
3531 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003532 }
3533
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05003534 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003535 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05003536 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05003537 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05003538 if (err) {
3539 dev_err(dev, "dpni_set_max_frame_length() failed\n");
3540 return err;
3541 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003542
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003543 /* Set actual number of queues in the net device */
3544 num_queues = dpaa2_eth_queue_count(priv);
3545 err = netif_set_real_num_tx_queues(net_dev, num_queues);
3546 if (err) {
3547 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
3548 return err;
3549 }
3550 err = netif_set_real_num_rx_queues(net_dev, num_queues);
3551 if (err) {
3552 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
3553 return err;
3554 }
3555
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003556 /* Capabilities listing */
3557 supported |= IFF_LIVE_ADDR_CHANGE;
3558
3559 if (options & DPNI_OPT_NO_MAC_FILTER)
3560 not_supported |= IFF_UNICAST_FLT;
3561 else
3562 supported |= IFF_UNICAST_FLT;
3563
3564 net_dev->priv_flags |= supported;
3565 net_dev->priv_flags &= ~not_supported;
3566
3567 /* Features */
3568 net_dev->features = NETIF_F_RXCSUM |
3569 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3570 NETIF_F_SG | NETIF_F_HIGHDMA |
3571 NETIF_F_LLTX;
3572 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003573
3574 return 0;
3575}
3576
3577static int poll_link_state(void *arg)
3578{
3579 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
3580 int err;
3581
3582 while (!kthread_should_stop()) {
3583 err = link_state_update(priv);
3584 if (unlikely(err))
3585 return err;
3586
3587 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
3588 }
3589
3590 return 0;
3591}
3592
Ioana Ciornei71947922019-10-31 01:18:31 +02003593static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
3594{
3595 struct fsl_mc_device *dpni_dev, *dpmac_dev;
3596 struct dpaa2_mac *mac;
3597 int err;
3598
3599 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
3600 dpmac_dev = fsl_mc_get_endpoint(dpni_dev);
3601 if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
3602 return 0;
3603
3604 if (dpaa2_mac_is_type_fixed(dpmac_dev, priv->mc_io))
3605 return 0;
3606
3607 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
3608 if (!mac)
3609 return -ENOMEM;
3610
3611 mac->mc_dev = dpmac_dev;
3612 mac->mc_io = priv->mc_io;
3613 mac->net_dev = priv->net_dev;
3614
3615 err = dpaa2_mac_connect(mac);
3616 if (err) {
3617 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint\n");
3618 kfree(mac);
3619 return err;
3620 }
3621 priv->mac = mac;
3622
3623 return 0;
3624}
3625
3626static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
3627{
3628 if (!priv->mac)
3629 return;
3630
3631 dpaa2_mac_disconnect(priv->mac);
3632 kfree(priv->mac);
3633 priv->mac = NULL;
3634}
3635
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003636static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
3637{
Ioana Radulescu112197d2017-10-11 08:29:49 -05003638 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003639 struct device *dev = (struct device *)arg;
3640 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
3641 struct net_device *net_dev = dev_get_drvdata(dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02003642 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003643 int err;
3644
3645 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
3646 DPNI_IRQ_INDEX, &status);
3647 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003648 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05003649 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003650 }
3651
Ioana Radulescu112197d2017-10-11 08:29:49 -05003652 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003653 link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003654
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02003655 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
Florin Chiculita8398b372019-10-16 10:36:22 +03003656 set_mac_addr(netdev_priv(net_dev));
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02003657 update_tx_fqids(priv);
Ioana Ciornei71947922019-10-31 01:18:31 +02003658
3659 rtnl_lock();
3660 if (priv->mac)
3661 dpaa2_eth_disconnect_mac(priv);
3662 else
3663 dpaa2_eth_connect_mac(priv);
3664 rtnl_unlock();
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02003665 }
Florin Chiculita8398b372019-10-16 10:36:22 +03003666
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003667 return IRQ_HANDLED;
3668}
3669
3670static int setup_irqs(struct fsl_mc_device *ls_dev)
3671{
3672 int err = 0;
3673 struct fsl_mc_device_irq *irq;
3674
3675 err = fsl_mc_allocate_irqs(ls_dev);
3676 if (err) {
3677 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
3678 return err;
3679 }
3680
3681 irq = ls_dev->irqs[0];
3682 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05003683 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003684 IRQF_NO_SUSPEND | IRQF_ONESHOT,
3685 dev_name(&ls_dev->dev), &ls_dev->dev);
3686 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003687 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003688 goto free_mc_irq;
3689 }
3690
3691 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
Florin Chiculita8398b372019-10-16 10:36:22 +03003692 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
3693 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003694 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003695 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003696 goto free_irq;
3697 }
3698
3699 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
3700 DPNI_IRQ_INDEX, 1);
3701 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003702 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003703 goto free_irq;
3704 }
3705
3706 return 0;
3707
3708free_irq:
3709 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
3710free_mc_irq:
3711 fsl_mc_free_irqs(ls_dev);
3712
3713 return err;
3714}
3715
3716static void add_ch_napi(struct dpaa2_eth_priv *priv)
3717{
3718 int i;
3719 struct dpaa2_eth_channel *ch;
3720
3721 for (i = 0; i < priv->num_channels; i++) {
3722 ch = priv->channel[i];
3723 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
3724 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
3725 NAPI_POLL_WEIGHT);
3726 }
3727}
3728
3729static void del_ch_napi(struct dpaa2_eth_priv *priv)
3730{
3731 int i;
3732 struct dpaa2_eth_channel *ch;
3733
3734 for (i = 0; i < priv->num_channels; i++) {
3735 ch = priv->channel[i];
3736 netif_napi_del(&ch->napi);
3737 }
3738}
3739
3740static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
3741{
3742 struct device *dev;
3743 struct net_device *net_dev = NULL;
3744 struct dpaa2_eth_priv *priv = NULL;
3745 int err = 0;
3746
3747 dev = &dpni_dev->dev;
3748
3749 /* Net device */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03003750 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003751 if (!net_dev) {
3752 dev_err(dev, "alloc_etherdev_mq() failed\n");
3753 return -ENOMEM;
3754 }
3755
3756 SET_NETDEV_DEV(net_dev, dev);
3757 dev_set_drvdata(dev, net_dev);
3758
3759 priv = netdev_priv(net_dev);
3760 priv->net_dev = net_dev;
3761
Ioana Radulescu08eb2392017-05-24 07:13:27 -05003762 priv->iommu_domain = iommu_get_domain_for_dev(dev);
3763
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003764 /* Obtain a MC portal */
3765 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3766 &priv->mc_io);
3767 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05003768 if (err == -ENXIO)
3769 err = -EPROBE_DEFER;
3770 else
3771 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003772 goto err_portal_alloc;
3773 }
3774
3775 /* MC objects initialization and configuration */
3776 err = setup_dpni(dpni_dev);
3777 if (err)
3778 goto err_dpni_setup;
3779
3780 err = setup_dpio(priv);
3781 if (err)
3782 goto err_dpio_setup;
3783
3784 setup_fqs(priv);
3785
3786 err = setup_dpbp(priv);
3787 if (err)
3788 goto err_dpbp_setup;
3789
3790 err = bind_dpni(priv);
3791 if (err)
3792 goto err_bind;
3793
3794 /* Add a NAPI context for each channel */
3795 add_ch_napi(priv);
3796
3797 /* Percpu statistics */
3798 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
3799 if (!priv->percpu_stats) {
3800 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
3801 err = -ENOMEM;
3802 goto err_alloc_percpu_stats;
3803 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003804 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
3805 if (!priv->percpu_extras) {
3806 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
3807 err = -ENOMEM;
3808 goto err_alloc_percpu_extras;
3809 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003810
3811 err = netdev_init(net_dev);
3812 if (err)
3813 goto err_netdev_init;
3814
3815 /* Configure checksum offload based on current interface flags */
3816 err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
3817 if (err)
3818 goto err_csum;
3819
3820 err = set_tx_csum(priv, !!(net_dev->features &
3821 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
3822 if (err)
3823 goto err_csum;
3824
3825 err = alloc_rings(priv);
3826 if (err)
3827 goto err_alloc_rings;
3828
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003829 err = setup_irqs(dpni_dev);
3830 if (err) {
3831 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
3832 priv->poll_thread = kthread_run(poll_link_state, priv,
3833 "%s_poll_link", net_dev->name);
3834 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003835 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003836 goto err_poll_thread;
3837 }
3838 priv->do_link_poll = true;
3839 }
3840
Ioana Ciornei71947922019-10-31 01:18:31 +02003841 err = dpaa2_eth_connect_mac(priv);
3842 if (err)
3843 goto err_connect_mac;
3844
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003845 err = register_netdev(net_dev);
3846 if (err < 0) {
3847 dev_err(dev, "register_netdev() failed\n");
3848 goto err_netdev_reg;
3849 }
3850
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003851#ifdef CONFIG_DEBUG_FS
3852 dpaa2_dbg_add(priv);
3853#endif
3854
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003855 dev_info(dev, "Probed interface %s\n", net_dev->name);
3856 return 0;
3857
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003858err_netdev_reg:
Ioana Ciornei71947922019-10-31 01:18:31 +02003859 dpaa2_eth_disconnect_mac(priv);
3860err_connect_mac:
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003861 if (priv->do_link_poll)
3862 kthread_stop(priv->poll_thread);
3863 else
3864 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003865err_poll_thread:
3866 free_rings(priv);
3867err_alloc_rings:
3868err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003869err_netdev_init:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003870 free_percpu(priv->percpu_extras);
3871err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003872 free_percpu(priv->percpu_stats);
3873err_alloc_percpu_stats:
3874 del_ch_napi(priv);
3875err_bind:
3876 free_dpbp(priv);
3877err_dpbp_setup:
3878 free_dpio(priv);
3879err_dpio_setup:
3880 free_dpni(priv);
3881err_dpni_setup:
3882 fsl_mc_portal_free(priv->mc_io);
3883err_portal_alloc:
3884 dev_set_drvdata(dev, NULL);
3885 free_netdev(net_dev);
3886
3887 return err;
3888}
3889
3890static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
3891{
3892 struct device *dev;
3893 struct net_device *net_dev;
3894 struct dpaa2_eth_priv *priv;
3895
3896 dev = &ls_dev->dev;
3897 net_dev = dev_get_drvdata(dev);
3898 priv = netdev_priv(net_dev);
3899
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003900#ifdef CONFIG_DEBUG_FS
3901 dpaa2_dbg_remove(priv);
3902#endif
Ioana Ciornei71947922019-10-31 01:18:31 +02003903 rtnl_lock();
3904 dpaa2_eth_disconnect_mac(priv);
3905 rtnl_unlock();
3906
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003907 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003908
3909 if (priv->do_link_poll)
3910 kthread_stop(priv->poll_thread);
3911 else
3912 fsl_mc_free_irqs(ls_dev);
3913
3914 free_rings(priv);
3915 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05003916 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003917
3918 del_ch_napi(priv);
3919 free_dpbp(priv);
3920 free_dpio(priv);
3921 free_dpni(priv);
3922
3923 fsl_mc_portal_free(priv->mc_io);
3924
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003925 free_netdev(net_dev);
3926
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05003927 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05003928
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003929 return 0;
3930}
3931
3932static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
3933 {
3934 .vendor = FSL_MC_VENDOR_FREESCALE,
3935 .obj_type = "dpni",
3936 },
3937 { .vendor = 0x0 }
3938};
3939MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
3940
3941static struct fsl_mc_driver dpaa2_eth_driver = {
3942 .driver = {
3943 .name = KBUILD_MODNAME,
3944 .owner = THIS_MODULE,
3945 },
3946 .probe = dpaa2_eth_probe,
3947 .remove = dpaa2_eth_remove,
3948 .match_id_table = dpaa2_eth_match_id_table
3949};
3950
Ioana Radulescu091a19e2019-01-18 16:16:00 +00003951static int __init dpaa2_eth_driver_init(void)
3952{
3953 int err;
3954
3955 dpaa2_eth_dbg_init();
3956 err = fsl_mc_driver_register(&dpaa2_eth_driver);
3957 if (err) {
3958 dpaa2_eth_dbg_exit();
3959 return err;
3960 }
3961
3962 return 0;
3963}
3964
3965static void __exit dpaa2_eth_driver_exit(void)
3966{
3967 dpaa2_eth_dbg_exit();
3968 fsl_mc_driver_unregister(&dpaa2_eth_driver);
3969}
3970
3971module_init(dpaa2_eth_driver_init);
3972module_exit(dpaa2_eth_driver_exit);