blob: daf8fd44aa093c7b0b83917c28874e15f2784d55 [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>
Yangbo Lud21c7842020-09-18 17:07:59 +080018#include <linux/fsl/ptp_qoriq.h>
Ioana Ciornei3657cda2020-07-21 19:38:25 +030019#include <net/pkt_cls.h>
Ioana Radulescu859f9982018-04-26 18:23:47 +080020#include <net/sock.h>
21
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050022#include "dpaa2-eth.h"
23
Ioana Radulescu56361872017-04-28 04:50:32 -050024/* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
25 * using trace events only need to #include <trace/events/sched.h>
26 */
27#define CREATE_TRACE_POINTS
28#include "dpaa2-eth-trace.h"
29
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050030MODULE_LICENSE("Dual BSD/GPL");
31MODULE_AUTHOR("Freescale Semiconductor, Inc");
32MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
33
Yangbo Lud21c7842020-09-18 17:07:59 +080034struct ptp_qoriq *dpaa2_ptp;
35EXPORT_SYMBOL(dpaa2_ptp);
36
Ioana Radulescu08eb2392017-05-24 07:13:27 -050037static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
38 dma_addr_t iova_addr)
39{
40 phys_addr_t phys_addr;
41
42 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
43
44 return phys_to_virt(phys_addr);
45}
46
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +030047static void dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv *priv,
48 u32 fd_status,
49 struct sk_buff *skb)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050050{
51 skb_checksum_none_assert(skb);
52
53 /* HW checksum validation is disabled, nothing to do here */
54 if (!(priv->net_dev->features & NETIF_F_RXCSUM))
55 return;
56
57 /* Read checksum validation bits */
58 if (!((fd_status & DPAA2_FAS_L3CV) &&
59 (fd_status & DPAA2_FAS_L4CV)))
60 return;
61
62 /* Inform the stack there's no need to compute L3/L4 csum anymore */
63 skb->ip_summed = CHECKSUM_UNNECESSARY;
64}
65
66/* Free a received FD.
67 * Not to be used for Tx conf FDs or on any other paths.
68 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +030069static void dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv,
70 const struct dpaa2_fd *fd,
71 void *vaddr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050072{
73 struct device *dev = priv->net_dev->dev.parent;
74 dma_addr_t addr = dpaa2_fd_get_addr(fd);
75 u8 fd_format = dpaa2_fd_get_format(fd);
76 struct dpaa2_sg_entry *sgt;
77 void *sg_vaddr;
78 int i;
79
80 /* If single buffer frame, just free the data buffer */
81 if (fd_format == dpaa2_fd_single)
82 goto free_buf;
83 else if (fd_format != dpaa2_fd_sg)
84 /* We don't support any other format */
85 return;
86
Ioana Radulescu729d79b2017-10-11 08:29:48 -050087 /* For S/G frames, we first need to free all SG entries
88 * except the first one, which was taken care of already
89 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050090 sgt = vaddr + dpaa2_fd_get_offset(fd);
Ioana Radulescu729d79b2017-10-11 08:29:48 -050091 for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050092 addr = dpaa2_sg_get_addr(&sgt[i]);
Ioana Radulescu08eb2392017-05-24 07:13:27 -050093 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +030094 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000095 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050096
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000097 free_pages((unsigned long)sg_vaddr, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050098 if (dpaa2_sg_is_final(&sgt[i]))
99 break;
100 }
101
102free_buf:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000103 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500104}
105
106/* Build a linear skb based on a single-buffer frame descriptor */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300107static struct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch,
108 const struct dpaa2_fd *fd,
109 void *fd_vaddr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500110{
111 struct sk_buff *skb = NULL;
112 u16 fd_offset = dpaa2_fd_get_offset(fd);
113 u32 fd_length = dpaa2_fd_get_len(fd);
114
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500115 ch->buf_count--;
116
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000117 skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500118 if (unlikely(!skb))
119 return NULL;
120
121 skb_reserve(skb, fd_offset);
122 skb_put(skb, fd_length);
123
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500124 return skb;
125}
126
127/* Build a non linear (fragmented) skb based on a S/G table */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300128static struct sk_buff *dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv *priv,
129 struct dpaa2_eth_channel *ch,
130 struct dpaa2_sg_entry *sgt)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500131{
132 struct sk_buff *skb = NULL;
133 struct device *dev = priv->net_dev->dev.parent;
134 void *sg_vaddr;
135 dma_addr_t sg_addr;
136 u16 sg_offset;
137 u32 sg_length;
138 struct page *page, *head_page;
139 int page_offset;
140 int i;
141
142 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
143 struct dpaa2_sg_entry *sge = &sgt[i];
144
145 /* NOTE: We only support SG entries in dpaa2_sg_single format,
146 * but this is the only format we may receive from HW anyway
147 */
148
149 /* Get the address and length from the S/G entry */
150 sg_addr = dpaa2_sg_get_addr(sge);
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500151 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300152 dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000153 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500154
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500155 sg_length = dpaa2_sg_get_len(sge);
156
157 if (i == 0) {
158 /* We build the skb around the first data buffer */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000159 skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500160 if (unlikely(!skb)) {
Ioana Radulescu729d79b2017-10-11 08:29:48 -0500161 /* Free the first SG entry now, since we already
162 * unmapped it and obtained the virtual address
163 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000164 free_pages((unsigned long)sg_vaddr, 0);
Ioana Radulescu729d79b2017-10-11 08:29:48 -0500165
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500166 /* We still need to subtract the buffers used
167 * by this FD from our software counter
168 */
169 while (!dpaa2_sg_is_final(&sgt[i]) &&
170 i < DPAA2_ETH_MAX_SG_ENTRIES)
171 i++;
172 break;
173 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500174
175 sg_offset = dpaa2_sg_get_offset(sge);
176 skb_reserve(skb, sg_offset);
177 skb_put(skb, sg_length);
178 } else {
179 /* Rest of the data buffers are stored as skb frags */
180 page = virt_to_page(sg_vaddr);
181 head_page = virt_to_head_page(sg_vaddr);
182
183 /* Offset in page (which may be compound).
184 * Data in subsequent SG entries is stored from the
185 * beginning of the buffer, so we don't need to add the
186 * sg_offset.
187 */
188 page_offset = ((unsigned long)sg_vaddr &
189 (PAGE_SIZE - 1)) +
190 (page_address(page) - page_address(head_page));
191
192 skb_add_rx_frag(skb, i - 1, head_page, page_offset,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300193 sg_length, priv->rx_buf_size);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500194 }
195
196 if (dpaa2_sg_is_final(sge))
197 break;
198 }
199
Ioana Radulescub63baf72017-10-11 08:29:45 -0500200 WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
201
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500202 /* Count all data buffers + SG table buffer */
203 ch->buf_count -= i + 2;
204
205 return skb;
206}
207
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000208/* Free buffers acquired from the buffer pool or which were meant to
209 * be released in the pool
210 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300211static void dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array,
212 int count)
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000213{
214 struct device *dev = priv->net_dev->dev.parent;
215 void *vaddr;
216 int i;
217
218 for (i = 0; i < count; i++) {
219 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300220 dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000221 DMA_BIDIRECTIONAL);
222 free_pages((unsigned long)vaddr, 0);
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000223 }
224}
225
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300226static void dpaa2_eth_xdp_release_buf(struct dpaa2_eth_priv *priv,
227 struct dpaa2_eth_channel *ch,
228 dma_addr_t addr)
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000229{
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300230 int retries = 0;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000231 int err;
232
233 ch->xdp.drop_bufs[ch->xdp.drop_cnt++] = addr;
234 if (ch->xdp.drop_cnt < DPAA2_ETH_BUFS_PER_CMD)
235 return;
236
237 while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
238 ch->xdp.drop_bufs,
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300239 ch->xdp.drop_cnt)) == -EBUSY) {
240 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
241 break;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000242 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300243 }
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000244
245 if (err) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300246 dpaa2_eth_free_bufs(priv, ch->xdp.drop_bufs, ch->xdp.drop_cnt);
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000247 ch->buf_count -= ch->xdp.drop_cnt;
248 }
249
250 ch->xdp.drop_cnt = 0;
251}
252
Ioana Ciornei38c440b2020-05-06 20:47:17 +0300253static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
254 struct dpaa2_eth_fq *fq,
255 struct dpaa2_eth_xdp_fds *xdp_fds)
256{
257 int total_enqueued = 0, retries = 0, enqueued;
258 struct dpaa2_eth_drv_stats *percpu_extras;
259 int num_fds, err, max_retries;
260 struct dpaa2_fd *fds;
261
262 percpu_extras = this_cpu_ptr(priv->percpu_extras);
263
264 /* try to enqueue all the FDs until the max number of retries is hit */
265 fds = xdp_fds->fds;
266 num_fds = xdp_fds->num;
267 max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
268 while (total_enqueued < num_fds && retries < max_retries) {
269 err = priv->enqueue(priv, fq, &fds[total_enqueued],
270 0, num_fds - total_enqueued, &enqueued);
271 if (err == -EBUSY) {
272 percpu_extras->tx_portal_busy += ++retries;
273 continue;
274 }
275 total_enqueued += enqueued;
276 }
277 xdp_fds->num = 0;
278
279 return total_enqueued;
280}
281
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300282static void dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv *priv,
283 struct dpaa2_eth_channel *ch,
284 struct dpaa2_eth_fq *fq)
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000285{
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300286 struct rtnl_link_stats64 *percpu_stats;
287 struct dpaa2_fd *fds;
288 int enqueued, i;
289
290 percpu_stats = this_cpu_ptr(priv->percpu_stats);
291
292 // enqueue the array of XDP_TX frames
293 enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);
294
295 /* update statistics */
296 percpu_stats->tx_packets += enqueued;
297 fds = fq->xdp_tx_fds.fds;
298 for (i = 0; i < enqueued; i++) {
299 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
300 ch->stats.xdp_tx++;
301 }
302 for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300303 dpaa2_eth_xdp_release_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300304 percpu_stats->tx_errors++;
305 ch->stats.xdp_tx_err++;
306 }
307 fq->xdp_tx_fds.num = 0;
308}
309
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300310static void dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv *priv,
311 struct dpaa2_eth_channel *ch,
312 struct dpaa2_fd *fd,
313 void *buf_start, u16 queue_id)
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300314{
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000315 struct dpaa2_faead *faead;
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300316 struct dpaa2_fd *dest_fd;
317 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000318 u32 ctrl, frc;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000319
320 /* Mark the egress frame hardware annotation area as valid */
321 frc = dpaa2_fd_get_frc(fd);
322 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
323 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
324
325 /* Instruct hardware to release the FD buffer directly into
326 * the buffer pool once transmission is completed, instead of
327 * sending a Tx confirmation frame to us
328 */
329 ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
330 faead = dpaa2_get_faead(buf_start, false);
331 faead->ctrl = cpu_to_le32(ctrl);
332 faead->conf_fqid = 0;
333
334 fq = &priv->fq[queue_id];
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300335 dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
336 memcpy(dest_fd, fd, sizeof(*dest_fd));
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000337
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300338 if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
339 return;
340
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300341 dpaa2_eth_xdp_tx_flush(priv, ch, fq);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000342}
343
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300344static u32 dpaa2_eth_run_xdp(struct dpaa2_eth_priv *priv,
345 struct dpaa2_eth_channel *ch,
346 struct dpaa2_eth_fq *rx_fq,
347 struct dpaa2_fd *fd, void *vaddr)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000348{
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000349 dma_addr_t addr = dpaa2_fd_get_addr(fd);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000350 struct bpf_prog *xdp_prog;
351 struct xdp_buff xdp;
352 u32 xdp_act = XDP_PASS;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000353 int err;
354
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000355 rcu_read_lock();
356
357 xdp_prog = READ_ONCE(ch->xdp.prog);
358 if (!xdp_prog)
359 goto out;
360
361 xdp.data = vaddr + dpaa2_fd_get_offset(fd);
362 xdp.data_end = xdp.data + dpaa2_fd_get_len(fd);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000363 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000364 xdp_set_data_meta_invalid(&xdp);
Ioana Radulescud678be12019-03-01 17:47:24 +0000365 xdp.rxq = &ch->xdp_rxq;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000366
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200367 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE -
368 (dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM);
369
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000370 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
371
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000372 /* xdp.data pointer may have changed */
373 dpaa2_fd_set_offset(fd, xdp.data - vaddr);
374 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
375
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000376 switch (xdp_act) {
377 case XDP_PASS:
378 break;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000379 case XDP_TX:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300380 dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000381 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000382 default:
383 bpf_warn_invalid_xdp_action(xdp_act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500384 fallthrough;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000385 case XDP_ABORTED:
386 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500387 fallthrough;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000388 case XDP_DROP:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300389 dpaa2_eth_xdp_release_buf(priv, ch, addr);
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000390 ch->stats.xdp_drop++;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000391 break;
Ioana Radulescud678be12019-03-01 17:47:24 +0000392 case XDP_REDIRECT:
393 dma_unmap_page(priv->net_dev->dev.parent, addr,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300394 priv->rx_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescud678be12019-03-01 17:47:24 +0000395 ch->buf_count--;
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200396
397 /* Allow redirect use of full headroom */
Ioana Radulescud678be12019-03-01 17:47:24 +0000398 xdp.data_hard_start = vaddr;
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200399 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
400
Ioana Radulescud678be12019-03-01 17:47:24 +0000401 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
402 if (unlikely(err))
403 ch->stats.xdp_drop++;
404 else
405 ch->stats.xdp_redirect++;
406 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000407 }
408
Ioana Radulescud678be12019-03-01 17:47:24 +0000409 ch->xdp.res |= xdp_act;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000410out:
411 rcu_read_unlock();
412 return xdp_act;
413}
414
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500415/* Main Rx frame processing routine */
416static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
417 struct dpaa2_eth_channel *ch,
418 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000419 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500420{
421 dma_addr_t addr = dpaa2_fd_get_addr(fd);
422 u8 fd_format = dpaa2_fd_get_format(fd);
423 void *vaddr;
424 struct sk_buff *skb;
425 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500426 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500427 struct device *dev = priv->net_dev->dev.parent;
428 struct dpaa2_fas *fas;
Ioana Radulescud695e762017-06-06 10:00:35 -0500429 void *buf_data;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500430 u32 status = 0;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000431 u32 xdp_act;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500432
Ioana Radulescu56361872017-04-28 04:50:32 -0500433 /* Tracing point */
434 trace_dpaa2_rx_fd(priv->net_dev, fd);
435
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500436 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300437 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu18c2e772018-11-26 16:27:32 +0000438 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500439
Ioana Radulescu54ce8912017-12-08 06:47:53 -0600440 fas = dpaa2_get_fas(vaddr, false);
Ioana Radulescud695e762017-06-06 10:00:35 -0500441 prefetch(fas);
442 buf_data = vaddr + dpaa2_fd_get_offset(fd);
443 prefetch(buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500444
445 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500446 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500447
448 if (fd_format == dpaa2_fd_single) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300449 xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000450 if (xdp_act != XDP_PASS) {
451 percpu_stats->rx_packets++;
452 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
453 return;
454 }
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 Ciornei5d8dccf2020-08-31 21:12:39 +0300458 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500459 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000460 WARN_ON(priv->xdp_prog);
461
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300462 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000463 DMA_BIDIRECTIONAL);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300464 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000465 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500466 percpu_extras->rx_sg_frames++;
467 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500468 } else {
469 /* We don't support any other format */
470 goto err_frame_format;
471 }
472
473 if (unlikely(!skb))
474 goto err_build_skb;
475
476 prefetch(skb->data);
477
Ioana Radulescu859f9982018-04-26 18:23:47 +0800478 /* Get the timestamp value */
479 if (priv->rx_tstamp) {
480 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
481 __le64 *ts = dpaa2_get_ts(vaddr, false);
482 u64 ns;
483
484 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
485
486 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
487 shhwtstamps->hwtstamp = ns_to_ktime(ns);
488 }
489
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500490 /* Check if we need to validate the L4 csum */
491 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500492 status = le32_to_cpu(fas->status);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300493 dpaa2_eth_validate_rx_csum(priv, status, skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500494 }
495
496 skb->protocol = eth_type_trans(skb, priv->net_dev);
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000497 skb_record_rx_queue(skb, fq->flowid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500498
499 percpu_stats->rx_packets++;
500 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
501
Ioana Ciornei0a25d922019-03-25 13:42:39 +0000502 list_add_tail(&skb->list, ch->rx_list);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500503
504 return;
505
506err_build_skb:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300507 dpaa2_eth_free_rx_fd(priv, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500508err_frame_format:
509 percpu_stats->rx_dropped++;
510}
511
512/* Consume all frames pull-dequeued into the store. This is the simplest way to
513 * make sure we don't accidentally issue another volatile dequeue which would
514 * overwrite (leak) frames already in the store.
515 *
516 * Observance of NAPI budget is not our concern, leaving that to the caller.
517 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300518static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
519 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500520{
521 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000522 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500523 struct dpaa2_dq *dq;
524 const struct dpaa2_fd *fd;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300525 int cleaned = 0, retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500526 int is_last;
527
528 do {
529 dq = dpaa2_io_store_next(ch->store, &is_last);
530 if (unlikely(!dq)) {
531 /* If we're here, we *must* have placed a
532 * volatile dequeue comnmand, so keep reading through
533 * the store until we get some sort of valid response
534 * token (either a valid frame or an "empty dequeue")
535 */
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300536 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
537 netdev_err_once(priv->net_dev,
538 "Unable to read a valid dequeue response\n");
539 return -ETIMEDOUT;
540 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500541 continue;
542 }
543
544 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600545 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500546
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000547 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500548 cleaned++;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300549 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500550 } while (!is_last);
551
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000552 if (!cleaned)
553 return 0;
554
555 fq->stats.frames += cleaned;
Ioana Ciornei460fd832020-04-24 12:33:18 +0300556 ch->stats.frames += cleaned;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000557
558 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000559 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000560 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000561 if (src)
562 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000563
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500564 return cleaned;
565}
566
Ioana Radulescu859f9982018-04-26 18:23:47 +0800567/* Configure the egress frame annotation for timestamp update */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300568static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_fd *fd, void *buf_start)
Ioana Radulescu859f9982018-04-26 18:23:47 +0800569{
570 struct dpaa2_faead *faead;
571 u32 ctrl, frc;
572
573 /* Mark the egress frame annotation area as valid */
574 frc = dpaa2_fd_get_frc(fd);
575 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
576
577 /* Set hardware annotation size */
578 ctrl = dpaa2_fd_get_ctrl(fd);
579 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
580
581 /* enable UPD (update prepanded data) bit in FAEAD field of
582 * hardware frame annotation area
583 */
584 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
585 faead = dpaa2_get_faead(buf_start, true);
586 faead->ctrl = cpu_to_le32(ctrl);
587}
588
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500589/* Create a frame descriptor based on a fragmented skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300590static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
591 struct sk_buff *skb,
592 struct dpaa2_fd *fd)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500593{
594 struct device *dev = priv->net_dev->dev.parent;
595 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500596 dma_addr_t addr;
597 int nr_frags = skb_shinfo(skb)->nr_frags;
598 struct dpaa2_sg_entry *sgt;
599 int i, err;
600 int sgt_buf_size;
601 struct scatterlist *scl, *crt_scl;
602 int num_sg;
603 int num_dma_bufs;
604 struct dpaa2_eth_swa *swa;
605
606 /* Create and map scatterlist.
607 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
608 * to go beyond nr_frags+1.
609 * Note: We don't support chained scatterlists
610 */
611 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
612 return -EINVAL;
613
614 scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
615 if (unlikely(!scl))
616 return -ENOMEM;
617
618 sg_init_table(scl, nr_frags + 1);
619 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Ciornei37fbbdda2020-06-24 14:34:18 +0300620 if (unlikely(num_sg < 0)) {
621 err = -ENOMEM;
622 goto dma_map_sg_failed;
623 }
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500624 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500625 if (unlikely(!num_dma_bufs)) {
626 err = -ENOMEM;
627 goto dma_map_sg_failed;
628 }
629
630 /* Prepare the HW SGT structure */
631 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500632 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Sebastian Andrzej Siewior90bc6d42019-06-07 21:20:37 +0200633 sgt_buf = napi_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500634 if (unlikely(!sgt_buf)) {
635 err = -ENOMEM;
636 goto sgt_buf_alloc_failed;
637 }
638 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500639 memset(sgt_buf, 0, sgt_buf_size);
640
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500641 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
642
643 /* Fill in the HW SGT structure.
644 *
645 * sgt_buf is zeroed out, so the following fields are implicit
646 * in all sgt entries:
647 * - offset is 0
648 * - format is 'dpaa2_sg_single'
649 */
650 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
651 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
652 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
653 }
654 dpaa2_sg_set_final(&sgt[i - 1], true);
655
656 /* Store the skb backpointer in the SGT buffer.
657 * Fit the scatterlist and the number of buffers alongside the
658 * skb backpointer in the software annotation area. We'll need
659 * all of them on Tx Conf.
660 */
661 swa = (struct dpaa2_eth_swa *)sgt_buf;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000662 swa->type = DPAA2_ETH_SWA_SG;
663 swa->sg.skb = skb;
664 swa->sg.scl = scl;
665 swa->sg.num_sg = num_sg;
666 swa->sg.sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500667
668 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500669 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500670 if (unlikely(dma_mapping_error(dev, addr))) {
671 err = -ENOMEM;
672 goto dma_map_single_failed;
673 }
674 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
675 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
676 dpaa2_fd_set_addr(fd, addr);
677 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000678 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500679
Ioana Radulescu859f9982018-04-26 18:23:47 +0800680 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300681 dpaa2_eth_enable_tx_tstamp(fd, sgt_buf);
Ioana Radulescu859f9982018-04-26 18:23:47 +0800682
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500683 return 0;
684
685dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500686 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500687sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500688 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500689dma_map_sg_failed:
690 kfree(scl);
691 return err;
692}
693
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300694/* Create a SG frame descriptor based on a linear skb.
695 *
696 * This function is used on the Tx path when the skb headroom is not large
697 * enough for the HW requirements, thus instead of realloc-ing the skb we
698 * create a SG frame descriptor with only one entry.
699 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300700static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
701 struct sk_buff *skb,
702 struct dpaa2_fd *fd)
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300703{
704 struct device *dev = priv->net_dev->dev.parent;
705 struct dpaa2_eth_sgt_cache *sgt_cache;
706 struct dpaa2_sg_entry *sgt;
707 struct dpaa2_eth_swa *swa;
708 dma_addr_t addr, sgt_addr;
709 void *sgt_buf = NULL;
710 int sgt_buf_size;
711 int err;
712
713 /* Prepare the HW SGT structure */
714 sgt_cache = this_cpu_ptr(priv->sgt_cache);
715 sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
716
717 if (sgt_cache->count == 0)
718 sgt_buf = kzalloc(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN,
719 GFP_ATOMIC);
720 else
721 sgt_buf = sgt_cache->buf[--sgt_cache->count];
722 if (unlikely(!sgt_buf))
723 return -ENOMEM;
724
725 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
726 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
727
728 addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
729 if (unlikely(dma_mapping_error(dev, addr))) {
730 err = -ENOMEM;
731 goto data_map_failed;
732 }
733
734 /* Fill in the HW SGT structure */
735 dpaa2_sg_set_addr(sgt, addr);
736 dpaa2_sg_set_len(sgt, skb->len);
737 dpaa2_sg_set_final(sgt, true);
738
739 /* Store the skb backpointer in the SGT buffer */
740 swa = (struct dpaa2_eth_swa *)sgt_buf;
741 swa->type = DPAA2_ETH_SWA_SINGLE;
742 swa->single.skb = skb;
743 swa->sg.sgt_size = sgt_buf_size;
744
745 /* Separately map the SGT buffer */
746 sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
747 if (unlikely(dma_mapping_error(dev, sgt_addr))) {
748 err = -ENOMEM;
749 goto sgt_map_failed;
750 }
751
752 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
753 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
754 dpaa2_fd_set_addr(fd, sgt_addr);
755 dpaa2_fd_set_len(fd, skb->len);
756 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
757
758 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300759 dpaa2_eth_enable_tx_tstamp(fd, sgt_buf);
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300760
761 return 0;
762
763sgt_map_failed:
764 dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
765data_map_failed:
766 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
767 kfree(sgt_buf);
768 else
769 sgt_cache->buf[sgt_cache->count++] = sgt_buf;
770
771 return err;
772}
773
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500774/* Create a frame descriptor based on a linear skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300775static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
776 struct sk_buff *skb,
777 struct dpaa2_fd *fd)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500778{
779 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600780 u8 *buffer_start, *aligned_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000781 struct dpaa2_eth_swa *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500782 dma_addr_t addr;
783
Ioana Radulescuc1636852017-12-08 06:47:58 -0600784 buffer_start = skb->data - dpaa2_eth_needed_headroom(priv, skb);
785
786 /* If there's enough room to align the FD address, do it.
787 * It will help hardware optimize accesses.
788 */
789 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
790 DPAA2_ETH_TX_BUF_ALIGN);
791 if (aligned_start >= skb->head)
792 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500793
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500794 /* Store a backpointer to the skb at the beginning of the buffer
795 * (in the private data area) such that we can release it
796 * on Tx confirm
797 */
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000798 swa = (struct dpaa2_eth_swa *)buffer_start;
799 swa->type = DPAA2_ETH_SWA_SINGLE;
800 swa->single.skb = skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500801
802 addr = dma_map_single(dev, buffer_start,
803 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500804 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500805 if (unlikely(dma_mapping_error(dev, addr)))
806 return -ENOMEM;
807
808 dpaa2_fd_set_addr(fd, addr);
809 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
810 dpaa2_fd_set_len(fd, skb->len);
811 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000812 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500813
Ioana Radulescu859f9982018-04-26 18:23:47 +0800814 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300815 dpaa2_eth_enable_tx_tstamp(fd, buffer_start);
Ioana Radulescu859f9982018-04-26 18:23:47 +0800816
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500817 return 0;
818}
819
820/* FD freeing routine on the Tx path
821 *
822 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
823 * back-pointed to is also freed.
824 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
825 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500826 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300827static void dpaa2_eth_free_tx_fd(const struct dpaa2_eth_priv *priv,
828 struct dpaa2_eth_fq *fq,
829 const struct dpaa2_fd *fd, bool in_napi)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500830{
831 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300832 dma_addr_t fd_addr, sg_addr;
Ioana Radulescud678be12019-03-01 17:47:24 +0000833 struct sk_buff *skb = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500834 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500835 struct dpaa2_eth_swa *swa;
836 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescud678be12019-03-01 17:47:24 +0000837 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500838
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300839 struct dpaa2_eth_sgt_cache *sgt_cache;
840 struct dpaa2_sg_entry *sgt;
841
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500842 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000843 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
844 swa = (struct dpaa2_eth_swa *)buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500845
846 if (fd_format == dpaa2_fd_single) {
Ioana Radulescud678be12019-03-01 17:47:24 +0000847 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
848 skb = swa->single.skb;
849 /* Accessing the skb buffer is safe before dma unmap,
850 * because we didn't map the actual skb shell.
851 */
852 dma_unmap_single(dev, fd_addr,
853 skb_tail_pointer(skb) - buffer_start,
854 DMA_BIDIRECTIONAL);
855 } else {
856 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
857 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
858 DMA_BIDIRECTIONAL);
859 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500860 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300861 if (swa->type == DPAA2_ETH_SWA_SG) {
862 skb = swa->sg.skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500863
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300864 /* Unmap the scatterlist */
865 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
866 DMA_BIDIRECTIONAL);
867 kfree(swa->sg.scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500868
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300869 /* Unmap the SGT buffer */
870 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
871 DMA_BIDIRECTIONAL);
872 } else {
873 skb = swa->single.skb;
874
875 /* Unmap the SGT Buffer */
876 dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
877 DMA_BIDIRECTIONAL);
878
879 sgt = (struct dpaa2_sg_entry *)(buffer_start +
880 priv->tx_data_offset);
881 sg_addr = dpaa2_sg_get_addr(sgt);
882 dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
883 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500884 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600885 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500886 return;
887 }
888
Ioana Radulescud678be12019-03-01 17:47:24 +0000889 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
890 fq->dq_frames++;
891 fq->dq_bytes += fd_len;
892 }
893
894 if (swa->type == DPAA2_ETH_SWA_XDP) {
895 xdp_return_frame(swa->xdp.xdpf);
896 return;
897 }
898
Ioana Radulescu859f9982018-04-26 18:23:47 +0800899 /* Get the timestamp value */
900 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
901 struct skb_shared_hwtstamps shhwtstamps;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000902 __le64 *ts = dpaa2_get_ts(buffer_start, true);
Ioana Radulescu859f9982018-04-26 18:23:47 +0800903 u64 ns;
904
905 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
906
907 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
908 shhwtstamps.hwtstamp = ns_to_ktime(ns);
909 skb_tstamp_tx(skb, &shhwtstamps);
910 }
911
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500912 /* Free SGT buffer allocated on tx */
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300913 if (fd_format != dpaa2_fd_single) {
914 sgt_cache = this_cpu_ptr(priv->sgt_cache);
915 if (swa->type == DPAA2_ETH_SWA_SG) {
916 skb_free_frag(buffer_start);
917 } else {
918 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
919 kfree(buffer_start);
920 else
921 sgt_cache->buf[sgt_cache->count++] = buffer_start;
922 }
923 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500924
925 /* Move on with skb release */
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +0000926 napi_consume_skb(skb, in_napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500927}
928
Ioana Radulescuc433db42017-06-06 10:00:26 -0500929static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500930{
931 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
932 struct dpaa2_fd fd;
933 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500934 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500935 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000936 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500937 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -0600938 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000939 u32 fd_len;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +0300940 u8 prio = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500941 int err, i;
942
943 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500944 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500945
Ioana Radulescu18c21462017-12-08 06:47:57 -0600946 needed_headroom = dpaa2_eth_needed_headroom(priv, skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500947
948 /* We'll be holding a back-reference to the skb until Tx Confirmation;
949 * we don't want that overwritten by a concurrent Tx with a cloned skb.
950 */
951 skb = skb_unshare(skb, GFP_ATOMIC);
952 if (unlikely(!skb)) {
953 /* skb_unshare() has already freed the skb */
954 percpu_stats->tx_dropped++;
955 return NETDEV_TX_OK;
956 }
957
958 /* Setup the FD fields */
959 memset(&fd, 0, sizeof(fd));
960
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500961 if (skb_is_nonlinear(skb)) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300962 err = dpaa2_eth_build_sg_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500963 percpu_extras->tx_sg_frames++;
964 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300965 } else if (skb_headroom(skb) < needed_headroom) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300966 err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, &fd);
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300967 percpu_extras->tx_sg_frames++;
968 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciornei4c96c0a2020-06-29 21:47:12 +0300969 percpu_extras->tx_converted_sg_frames++;
970 percpu_extras->tx_converted_sg_bytes += skb->len;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500971 } else {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300972 err = dpaa2_eth_build_single_fd(priv, skb, &fd);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500973 }
974
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500975 if (unlikely(err)) {
976 percpu_stats->tx_dropped++;
977 goto err_build_fd;
978 }
979
Ioana Radulescu56361872017-04-28 04:50:32 -0500980 /* Tracing point */
981 trace_dpaa2_tx_fd(net_dev, &fd);
982
Ioana Radulescu537336c2017-12-21 06:33:20 -0600983 /* TxConf FQ selection relies on queue id from the stack.
984 * In case of a forwarded frame from another DPNI interface, we choose
985 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500986 */
Ioana Radulescu537336c2017-12-21 06:33:20 -0600987 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +0300988
989 if (net_dev->num_tc) {
990 prio = netdev_txq_to_tc(net_dev, queue_mapping);
991 /* Hardware interprets priority level 0 as being the highest,
992 * so we need to do a reverse mapping to the netdev tc index
993 */
994 prio = net_dev->num_tc - prio - 1;
995 /* We have only one FQ array entry for all Tx hardware queues
996 * with the same flow id (but different priority levels)
997 */
998 queue_mapping %= dpaa2_eth_queue_count(priv);
999 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001000 fq = &priv->fq[queue_mapping];
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001001
1002 fd_len = dpaa2_fd_get_len(&fd);
1003 nq = netdev_get_tx_queue(net_dev, queue_mapping);
1004 netdev_tx_sent_queue(nq, fd_len);
1005
1006 /* Everything that happens after this enqueues might race with
1007 * the Tx confirmation callback for this frame
1008 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001009 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Ciornei6ff80442020-04-22 15:05:11 +03001010 err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001011 if (err != -EBUSY)
1012 break;
1013 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001014 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001015 if (unlikely(err < 0)) {
1016 percpu_stats->tx_errors++;
1017 /* Clean up everything, including freeing the skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001018 dpaa2_eth_free_tx_fd(priv, fq, &fd, false);
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001019 netdev_tx_completed_queue(nq, 1, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001020 } else {
1021 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001022 percpu_stats->tx_bytes += fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001023 }
1024
1025 return NETDEV_TX_OK;
1026
1027err_build_fd:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001028 dev_kfree_skb(skb);
1029
1030 return NETDEV_TX_OK;
1031}
1032
1033/* Tx confirmation frame processing routine */
1034static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +00001035 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001036 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001037 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001038{
1039 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001040 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001041 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001042 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001043
Ioana Radulescu56361872017-04-28 04:50:32 -05001044 /* Tracing point */
1045 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1046
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001047 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1048 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001049 percpu_extras->tx_conf_bytes += fd_len;
1050
Ioana Radulescu39163c02017-06-06 10:00:39 -05001051 /* Check frame errors in the FD field */
1052 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001053 dpaa2_eth_free_tx_fd(priv, fq, fd, true);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001054
1055 if (likely(!fd_errors))
1056 return;
1057
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06001058 if (net_ratelimit())
1059 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1060 fd_errors);
1061
Ioana Radulescu39163c02017-06-06 10:00:39 -05001062 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1063 /* Tx-conf logically pertains to the egress path. */
1064 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001065}
1066
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001067static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001068{
1069 int err;
1070
1071 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1072 DPNI_OFF_RX_L3_CSUM, enable);
1073 if (err) {
1074 netdev_err(priv->net_dev,
1075 "dpni_set_offload(RX_L3_CSUM) failed\n");
1076 return err;
1077 }
1078
1079 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1080 DPNI_OFF_RX_L4_CSUM, enable);
1081 if (err) {
1082 netdev_err(priv->net_dev,
1083 "dpni_set_offload(RX_L4_CSUM) failed\n");
1084 return err;
1085 }
1086
1087 return 0;
1088}
1089
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001090static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001091{
1092 int err;
1093
1094 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1095 DPNI_OFF_TX_L3_CSUM, enable);
1096 if (err) {
1097 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1098 return err;
1099 }
1100
1101 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1102 DPNI_OFF_TX_L4_CSUM, enable);
1103 if (err) {
1104 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1105 return err;
1106 }
1107
1108 return 0;
1109}
1110
1111/* Perform a single release command to add buffers
1112 * to the specified buffer pool
1113 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001114static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1115 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001116{
1117 struct device *dev = priv->net_dev->dev.parent;
1118 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001119 struct page *page;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001120 dma_addr_t addr;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001121 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001122 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001123
1124 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1125 /* Allocate buffer visible to WRIOP + skb shared info +
1126 * alignment padding
1127 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001128 /* allocate one page for each Rx buffer. WRIOP sees
1129 * the entire page except for a tailroom reserved for
1130 * skb shared info
1131 */
1132 page = dev_alloc_pages(0);
1133 if (!page)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001134 goto err_alloc;
1135
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001136 addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001137 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001138 if (unlikely(dma_mapping_error(dev, addr)))
1139 goto err_map;
1140
1141 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -05001142
1143 /* tracing point */
1144 trace_dpaa2_eth_buf_seed(priv->net_dev,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001145 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001146 addr, priv->rx_buf_size,
Ioana Radulescu56361872017-04-28 04:50:32 -05001147 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001148 }
1149
1150release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001151 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001152 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001153 buf_array, i)) == -EBUSY) {
1154 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1155 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001156 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001157 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001158
1159 /* If release command failed, clean up and bail out;
1160 * not much else we can do about it
1161 */
1162 if (err) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001163 dpaa2_eth_free_bufs(priv, buf_array, i);
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001164 return 0;
1165 }
1166
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001167 return i;
1168
1169err_map:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001170 __free_pages(page, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001171err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001172 /* If we managed to allocate at least some buffers,
1173 * release them to hardware
1174 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001175 if (i)
1176 goto release_bufs;
1177
1178 return 0;
1179}
1180
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001181static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001182{
1183 int i, j;
1184 int new_count;
1185
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001186 for (j = 0; j < priv->num_channels; j++) {
1187 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1188 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001189 new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001190 priv->channel[j]->buf_count += new_count;
1191
1192 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001193 return -ENOMEM;
1194 }
1195 }
1196 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001197
1198 return 0;
1199}
1200
1201/**
1202 * Drain the specified number of buffers from the DPNI's private buffer pool.
1203 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1204 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001205static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001206{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001207 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001208 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001209 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001210
1211 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001212 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001213 buf_array, count);
1214 if (ret < 0) {
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001215 if (ret == -EBUSY &&
Ioana Ciornei0e5ad752020-06-24 14:34:19 +03001216 retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001217 continue;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001218 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1219 return;
1220 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001221 dpaa2_eth_free_bufs(priv, buf_array, ret);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001222 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001223 } while (ret);
1224}
1225
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001226static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001227{
1228 int i;
1229
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001230 dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1231 dpaa2_eth_drain_bufs(priv, 1);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001232
1233 for (i = 0; i < priv->num_channels; i++)
1234 priv->channel[i]->buf_count = 0;
1235}
1236
1237/* Function is called from softirq context only, so we don't need to guard
1238 * the access to percpu count
1239 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001240static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1241 struct dpaa2_eth_channel *ch,
1242 u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001243{
1244 int new_count;
1245
1246 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1247 return 0;
1248
1249 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001250 new_count = dpaa2_eth_add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001251 if (unlikely(!new_count)) {
1252 /* Out of memory; abort for now, we'll try later on */
1253 break;
1254 }
1255 ch->buf_count += new_count;
1256 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1257
1258 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1259 return -ENOMEM;
1260
1261 return 0;
1262}
1263
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001264static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1265{
1266 struct dpaa2_eth_sgt_cache *sgt_cache;
1267 u16 count;
1268 int k, i;
1269
Ioana Ciornei0fe665d2020-07-06 17:55:54 +03001270 for_each_possible_cpu(k) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001271 sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1272 count = sgt_cache->count;
1273
1274 for (i = 0; i < count; i++)
1275 kfree(sgt_cache->buf[i]);
1276 sgt_cache->count = 0;
1277 }
1278}
1279
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001280static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001281{
1282 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001283 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001284
1285 /* Retry while portal is busy */
1286 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001287 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1288 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001289 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001290 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001291 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001292
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001293 ch->stats.dequeue_portal_busy += dequeues;
1294 if (unlikely(err))
1295 ch->stats.pull_err++;
1296
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001297 return err;
1298}
1299
1300/* NAPI poll routine
1301 *
1302 * Frames are dequeued from the QMan channel associated with this NAPI context.
1303 * Rx, Tx confirmation and (if configured) Rx error frames all count
1304 * towards the NAPI budget.
1305 */
1306static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1307{
1308 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001309 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001310 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001311 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1312 struct netdev_queue *nq;
1313 int store_cleaned, work_done;
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001314 struct list_head rx_list;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001315 int retries = 0;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001316 u16 flowid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001317 int err;
1318
1319 ch = container_of(napi, struct dpaa2_eth_channel, napi);
Ioana Radulescud678be12019-03-01 17:47:24 +00001320 ch->xdp.res = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001321 priv = ch->priv;
1322
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001323 INIT_LIST_HEAD(&rx_list);
1324 ch->rx_list = &rx_list;
1325
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001326 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001327 err = dpaa2_eth_pull_channel(ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001328 if (unlikely(err))
1329 break;
1330
1331 /* Refill pool if appropriate */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001332 dpaa2_eth_refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001333
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001334 store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001335 if (store_cleaned <= 0)
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001336 break;
1337 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001338 rx_cleaned += store_cleaned;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001339 flowid = fq->flowid;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001340 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001341 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001342 /* We have a single Tx conf FQ on this channel */
1343 txc_fq = fq;
1344 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001345
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001346 /* If we either consumed the whole NAPI budget with Rx frames
1347 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001348 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001349 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001350 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1351 work_done = budget;
1352 goto out;
1353 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001354 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001355
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001356 /* We didn't consume the entire budget, so finish napi and
1357 * re-enable data availability notifications
1358 */
1359 napi_complete_done(napi, rx_cleaned);
1360 do {
1361 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1362 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001363 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001364 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1365 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001366
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001367 work_done = max(rx_cleaned, 1);
1368
1369out:
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001370 netif_receive_skb_list(ch->rx_list);
1371
Ioana Radulescud678be12019-03-01 17:47:24 +00001372 if (txc_fq && txc_fq->dq_frames) {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001373 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1374 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1375 txc_fq->dq_bytes);
1376 txc_fq->dq_frames = 0;
1377 txc_fq->dq_bytes = 0;
1378 }
1379
Ioana Radulescud678be12019-03-01 17:47:24 +00001380 if (ch->xdp.res & XDP_REDIRECT)
1381 xdp_do_flush_map();
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001382 else if (rx_cleaned && ch->xdp.res & XDP_TX)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001383 dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
Ioana Radulescud678be12019-03-01 17:47:24 +00001384
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001385 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001386}
1387
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001388static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001389{
1390 struct dpaa2_eth_channel *ch;
1391 int i;
1392
1393 for (i = 0; i < priv->num_channels; i++) {
1394 ch = priv->channel[i];
1395 napi_enable(&ch->napi);
1396 }
1397}
1398
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001399static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001400{
1401 struct dpaa2_eth_channel *ch;
1402 int i;
1403
1404 for (i = 0; i < priv->num_channels; i++) {
1405 ch = priv->channel[i];
1406 napi_disable(&ch->napi);
1407 }
1408}
1409
Ioana Ciornei07beb162020-05-31 00:08:14 +03001410void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1411 bool tx_pause, bool pfc)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001412{
1413 struct dpni_taildrop td = {0};
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001414 struct dpaa2_eth_fq *fq;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001415 int i, err;
1416
Ioana Ciornei07beb162020-05-31 00:08:14 +03001417 /* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1418 * flow control is disabled (as it might interfere with either the
1419 * buffer pool depletion trigger for pause frames or with the group
1420 * congestion trigger for PFC frames)
1421 */
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001422 td.enable = !tx_pause;
Ioana Ciornei07beb162020-05-31 00:08:14 +03001423 if (priv->rx_fqtd_enabled == td.enable)
1424 goto set_cgtd;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001425
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001426 td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1427 td.units = DPNI_CONGESTION_UNIT_BYTES;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001428
1429 for (i = 0; i < priv->num_fqs; i++) {
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001430 fq = &priv->fq[i];
1431 if (fq->type != DPAA2_RX_FQ)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001432 continue;
1433 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001434 DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1435 fq->tc, fq->flowid, &td);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001436 if (err) {
1437 netdev_err(priv->net_dev,
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001438 "dpni_set_taildrop(FQ) failed\n");
1439 return;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001440 }
1441 }
1442
Ioana Ciornei07beb162020-05-31 00:08:14 +03001443 priv->rx_fqtd_enabled = td.enable;
1444
1445set_cgtd:
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001446 /* Congestion group taildrop: threshold is in frames, per group
1447 * of FQs belonging to the same traffic class
Ioana Ciornei07beb162020-05-31 00:08:14 +03001448 * Enabled if general Tx pause disabled or if PFCs are enabled
1449 * (congestion group threhsold for PFC generation is lower than the
1450 * CG taildrop threshold, so it won't interfere with it; we also
1451 * want frames in non-PFC enabled traffic classes to be kept in check)
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001452 */
Ioana Ciornei07beb162020-05-31 00:08:14 +03001453 td.enable = !tx_pause || (tx_pause && pfc);
1454 if (priv->rx_cgtd_enabled == td.enable)
1455 return;
1456
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001457 td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
1458 td.units = DPNI_CONGESTION_UNIT_FRAMES;
1459 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
1460 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1461 DPNI_CP_GROUP, DPNI_QUEUE_RX,
1462 i, 0, &td);
1463 if (err) {
1464 netdev_err(priv->net_dev,
1465 "dpni_set_taildrop(CG) failed\n");
1466 return;
1467 }
1468 }
1469
Ioana Ciornei07beb162020-05-31 00:08:14 +03001470 priv->rx_cgtd_enabled = td.enable;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001471}
1472
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001473static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001474{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001475 struct dpni_link_state state = {0};
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001476 bool tx_pause;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001477 int err;
1478
1479 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1480 if (unlikely(err)) {
1481 netdev_err(priv->net_dev,
1482 "dpni_get_link_state() failed\n");
1483 return err;
1484 }
1485
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001486 /* If Tx pause frame settings have changed, we need to update
1487 * Rx FQ taildrop configuration as well. We configure taildrop
1488 * only when pause frame generation is disabled.
1489 */
Ioana Radulescuad054f22020-05-31 00:08:10 +03001490 tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
Ioana Ciornei07beb162020-05-31 00:08:14 +03001491 dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001492
Ioana Ciornei71947922019-10-31 01:18:31 +02001493 /* When we manage the MAC/PHY using phylink there is no need
1494 * to manually update the netif_carrier.
1495 */
1496 if (priv->mac)
1497 goto out;
1498
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001499 /* Chech link state; speed / duplex changes are not treated yet */
1500 if (priv->link_state.up == state.up)
Ioana Radulescucce629432019-08-28 17:08:14 +03001501 goto out;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001502
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001503 if (state.up) {
1504 netif_carrier_on(priv->net_dev);
1505 netif_tx_start_all_queues(priv->net_dev);
1506 } else {
1507 netif_tx_stop_all_queues(priv->net_dev);
1508 netif_carrier_off(priv->net_dev);
1509 }
1510
Ioana Radulescu77160af2017-06-06 10:00:28 -05001511 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001512 state.up ? "up" : "down");
1513
Ioana Radulescucce629432019-08-28 17:08:14 +03001514out:
1515 priv->link_state = state;
1516
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001517 return 0;
1518}
1519
1520static int dpaa2_eth_open(struct net_device *net_dev)
1521{
1522 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1523 int err;
1524
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001525 err = dpaa2_eth_seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001526 if (err) {
1527 /* Not much to do; the buffer pool, though not filled up,
1528 * may still contain some buffers which would enable us
1529 * to limp on.
1530 */
1531 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001532 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001533 }
1534
Ioana Ciornei71947922019-10-31 01:18:31 +02001535 if (!priv->mac) {
1536 /* We'll only start the txqs when the link is actually ready;
1537 * make sure we don't race against the link up notification,
1538 * which may come immediately after dpni_enable();
1539 */
1540 netif_tx_stop_all_queues(net_dev);
1541
1542 /* Also, explicitly set carrier off, otherwise
1543 * netif_carrier_ok() will return true and cause 'ip link show'
1544 * to report the LOWER_UP flag, even though the link
1545 * notification wasn't even received.
1546 */
1547 netif_carrier_off(net_dev);
1548 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001549 dpaa2_eth_enable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001550
1551 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1552 if (err < 0) {
1553 netdev_err(net_dev, "dpni_enable() failed\n");
1554 goto enable_err;
1555 }
1556
Ioana Ciornei71947922019-10-31 01:18:31 +02001557 if (!priv->mac) {
1558 /* If the DPMAC object has already processed the link up
1559 * interrupt, we have to learn the link state ourselves.
1560 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001561 err = dpaa2_eth_link_state_update(priv);
Ioana Ciornei71947922019-10-31 01:18:31 +02001562 if (err < 0) {
1563 netdev_err(net_dev, "Can't update link state\n");
1564 goto link_state_err;
1565 }
1566 } else {
1567 phylink_start(priv->mac->phylink);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001568 }
1569
1570 return 0;
1571
1572link_state_err:
1573enable_err:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001574 dpaa2_eth_disable_ch_napi(priv);
1575 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001576 return err;
1577}
1578
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001579/* Total number of in-flight frames on ingress queues */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001580static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001581{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001582 struct dpaa2_eth_fq *fq;
1583 u32 fcnt = 0, bcnt = 0, total = 0;
1584 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001585
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001586 for (i = 0; i < priv->num_fqs; i++) {
1587 fq = &priv->fq[i];
1588 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1589 if (err) {
1590 netdev_warn(priv->net_dev, "query_fq_count failed");
1591 break;
1592 }
1593 total += fcnt;
1594 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001595
1596 return total;
1597}
1598
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001599static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001600{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001601 int retries = 10;
1602 u32 pending;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001603
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001604 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001605 pending = dpaa2_eth_ingress_fq_count(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001606 if (pending)
1607 msleep(100);
1608 } while (pending && --retries);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001609}
1610
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001611#define DPNI_TX_PENDING_VER_MAJOR 7
1612#define DPNI_TX_PENDING_VER_MINOR 13
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001613static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001614{
1615 union dpni_statistics stats;
1616 int retries = 10;
1617 int err;
1618
1619 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1620 DPNI_TX_PENDING_VER_MINOR) < 0)
1621 goto out;
1622
1623 do {
1624 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1625 &stats);
1626 if (err)
1627 goto out;
1628 if (stats.page_6.tx_pending_frames == 0)
1629 return;
1630 } while (--retries);
1631
1632out:
1633 msleep(500);
1634}
1635
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001636static int dpaa2_eth_stop(struct net_device *net_dev)
1637{
1638 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001639 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001640 int retries = 10;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001641
Ioana Ciornei71947922019-10-31 01:18:31 +02001642 if (!priv->mac) {
1643 netif_tx_stop_all_queues(net_dev);
1644 netif_carrier_off(net_dev);
1645 } else {
1646 phylink_stop(priv->mac->phylink);
1647 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001648
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001649 /* On dpni_disable(), the MC firmware will:
1650 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1651 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1652 * of all in flight Tx frames is finished (and corresponding Tx conf
1653 * frames are enqueued back to software)
1654 *
1655 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1656 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1657 * and Tx conf queues are consumed on NAPI poll.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001658 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001659 dpaa2_eth_wait_for_egress_fq_empty(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001660
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001661 do {
1662 dpni_disable(priv->mc_io, 0, priv->mc_token);
1663 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1664 if (dpni_enabled)
1665 /* Allow the hardware some slack */
1666 msleep(100);
1667 } while (dpni_enabled && --retries);
1668 if (!retries) {
1669 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1670 /* Must go on and disable NAPI nonetheless, so we don't crash at
1671 * the next "ifconfig up"
1672 */
1673 }
1674
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001675 dpaa2_eth_wait_for_ingress_fq_empty(priv);
1676 dpaa2_eth_disable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001677
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001678 /* Empty the buffer pool */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001679 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001680
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001681 /* Empty the Scatter-Gather Buffer cache */
1682 dpaa2_eth_sgt_cache_drain(priv);
1683
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001684 return 0;
1685}
1686
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001687static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1688{
1689 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1690 struct device *dev = net_dev->dev.parent;
1691 int err;
1692
1693 err = eth_mac_addr(net_dev, addr);
1694 if (err < 0) {
1695 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1696 return err;
1697 }
1698
1699 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1700 net_dev->dev_addr);
1701 if (err) {
1702 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1703 return err;
1704 }
1705
1706 return 0;
1707}
1708
1709/** Fill in counters maintained by the GPP driver. These may be different from
1710 * the hardware counters obtained by ethtool.
1711 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001712static void dpaa2_eth_get_stats(struct net_device *net_dev,
1713 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001714{
1715 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1716 struct rtnl_link_stats64 *percpu_stats;
1717 u64 *cpustats;
1718 u64 *netstats = (u64 *)stats;
1719 int i, j;
1720 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1721
1722 for_each_possible_cpu(i) {
1723 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1724 cpustats = (u64 *)percpu_stats;
1725 for (j = 0; j < num; j++)
1726 netstats[j] += cpustats[j];
1727 }
1728}
1729
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001730/* Copy mac unicast addresses from @net_dev to @priv.
1731 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1732 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001733static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
1734 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001735{
1736 struct netdev_hw_addr *ha;
1737 int err;
1738
1739 netdev_for_each_uc_addr(ha, net_dev) {
1740 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1741 ha->addr);
1742 if (err)
1743 netdev_warn(priv->net_dev,
1744 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1745 ha->addr, err);
1746 }
1747}
1748
1749/* Copy mac multicast addresses from @net_dev to @priv
1750 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1751 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001752static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
1753 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001754{
1755 struct netdev_hw_addr *ha;
1756 int err;
1757
1758 netdev_for_each_mc_addr(ha, net_dev) {
1759 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1760 ha->addr);
1761 if (err)
1762 netdev_warn(priv->net_dev,
1763 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1764 ha->addr, err);
1765 }
1766}
1767
1768static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1769{
1770 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1771 int uc_count = netdev_uc_count(net_dev);
1772 int mc_count = netdev_mc_count(net_dev);
1773 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1774 u32 options = priv->dpni_attrs.options;
1775 u16 mc_token = priv->mc_token;
1776 struct fsl_mc_io *mc_io = priv->mc_io;
1777 int err;
1778
1779 /* Basic sanity checks; these probably indicate a misconfiguration */
1780 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1781 netdev_info(net_dev,
1782 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1783 max_mac);
1784
1785 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1786 if (uc_count > max_mac) {
1787 netdev_info(net_dev,
1788 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1789 uc_count, max_mac);
1790 goto force_promisc;
1791 }
1792 if (mc_count + uc_count > max_mac) {
1793 netdev_info(net_dev,
1794 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1795 uc_count + mc_count, max_mac);
1796 goto force_mc_promisc;
1797 }
1798
1799 /* Adjust promisc settings due to flag combinations */
1800 if (net_dev->flags & IFF_PROMISC)
1801 goto force_promisc;
1802 if (net_dev->flags & IFF_ALLMULTI) {
1803 /* First, rebuild unicast filtering table. This should be done
1804 * in promisc mode, in order to avoid frame loss while we
1805 * progressively add entries to the table.
1806 * We don't know whether we had been in promisc already, and
1807 * making an MC call to find out is expensive; so set uc promisc
1808 * nonetheless.
1809 */
1810 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1811 if (err)
1812 netdev_warn(net_dev, "Can't set uc promisc\n");
1813
1814 /* Actual uc table reconstruction. */
1815 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1816 if (err)
1817 netdev_warn(net_dev, "Can't clear uc filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001818 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001819
1820 /* Finally, clear uc promisc and set mc promisc as requested. */
1821 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1822 if (err)
1823 netdev_warn(net_dev, "Can't clear uc promisc\n");
1824 goto force_mc_promisc;
1825 }
1826
1827 /* Neither unicast, nor multicast promisc will be on... eventually.
1828 * For now, rebuild mac filtering tables while forcing both of them on.
1829 */
1830 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1831 if (err)
1832 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1833 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1834 if (err)
1835 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1836
1837 /* Actual mac filtering tables reconstruction */
1838 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1839 if (err)
1840 netdev_warn(net_dev, "Can't clear mac filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001841 dpaa2_eth_add_mc_hw_addr(net_dev, priv);
1842 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001843
1844 /* Now we can clear both ucast and mcast promisc, without risking
1845 * to drop legitimate frames anymore.
1846 */
1847 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1848 if (err)
1849 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1850 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1851 if (err)
1852 netdev_warn(net_dev, "Can't clear mcast promisc\n");
1853
1854 return;
1855
1856force_promisc:
1857 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1858 if (err)
1859 netdev_warn(net_dev, "Can't set ucast promisc\n");
1860force_mc_promisc:
1861 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1862 if (err)
1863 netdev_warn(net_dev, "Can't set mcast promisc\n");
1864}
1865
1866static int dpaa2_eth_set_features(struct net_device *net_dev,
1867 netdev_features_t features)
1868{
1869 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1870 netdev_features_t changed = features ^ net_dev->features;
1871 bool enable;
1872 int err;
1873
1874 if (changed & NETIF_F_RXCSUM) {
1875 enable = !!(features & NETIF_F_RXCSUM);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001876 err = dpaa2_eth_set_rx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001877 if (err)
1878 return err;
1879 }
1880
1881 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1882 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001883 err = dpaa2_eth_set_tx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001884 if (err)
1885 return err;
1886 }
1887
1888 return 0;
1889}
1890
Ioana Radulescu859f9982018-04-26 18:23:47 +08001891static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1892{
1893 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1894 struct hwtstamp_config config;
1895
1896 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
1897 return -EFAULT;
1898
1899 switch (config.tx_type) {
1900 case HWTSTAMP_TX_OFF:
1901 priv->tx_tstamp = false;
1902 break;
1903 case HWTSTAMP_TX_ON:
1904 priv->tx_tstamp = true;
1905 break;
1906 default:
1907 return -ERANGE;
1908 }
1909
1910 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
1911 priv->rx_tstamp = false;
1912 } else {
1913 priv->rx_tstamp = true;
1914 /* TS is set for all frame types, not only those requested */
1915 config.rx_filter = HWTSTAMP_FILTER_ALL;
1916 }
1917
1918 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
1919 -EFAULT : 0;
1920}
1921
1922static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1923{
Russell King4a841822020-02-27 12:00:21 +00001924 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1925
Ioana Radulescu859f9982018-04-26 18:23:47 +08001926 if (cmd == SIOCSHWTSTAMP)
1927 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
1928
Russell King4a841822020-02-27 12:00:21 +00001929 if (priv->mac)
1930 return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
1931
1932 return -EOPNOTSUPP;
Ioana Radulescu859f9982018-04-26 18:23:47 +08001933}
1934
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001935static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
1936{
1937 int mfl, linear_mfl;
1938
1939 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001940 linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001941 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001942
1943 if (mfl > linear_mfl) {
1944 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
1945 linear_mfl - VLAN_ETH_HLEN);
1946 return false;
1947 }
1948
1949 return true;
1950}
1951
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001952static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001953{
1954 int mfl, err;
1955
1956 /* We enforce a maximum Rx frame length based on MTU only if we have
1957 * an XDP program attached (in order to avoid Rx S/G frames).
1958 * Otherwise, we accept all incoming frames as long as they are not
1959 * larger than maximum size supported in hardware
1960 */
1961 if (has_xdp)
1962 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1963 else
1964 mfl = DPAA2_ETH_MFL;
1965
1966 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
1967 if (err) {
1968 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
1969 return err;
1970 }
1971
1972 return 0;
1973}
1974
1975static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
1976{
1977 struct dpaa2_eth_priv *priv = netdev_priv(dev);
1978 int err;
1979
1980 if (!priv->xdp_prog)
1981 goto out;
1982
1983 if (!xdp_mtu_valid(priv, new_mtu))
1984 return -EINVAL;
1985
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001986 err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00001987 if (err)
1988 return err;
1989
1990out:
1991 dev->mtu = new_mtu;
1992 return 0;
1993}
1994
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001995static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00001996{
1997 struct dpni_buffer_layout buf_layout = {0};
1998 int err;
1999
2000 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2001 DPNI_QUEUE_RX, &buf_layout);
2002 if (err) {
2003 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2004 return err;
2005 }
2006
2007 /* Reserve extra headroom for XDP header size changes */
2008 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2009 (has_xdp ? XDP_PACKET_HEADROOM : 0);
2010 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2011 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2012 DPNI_QUEUE_RX, &buf_layout);
2013 if (err) {
2014 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2015 return err;
2016 }
2017
2018 return 0;
2019}
2020
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002021static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002022{
2023 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2024 struct dpaa2_eth_channel *ch;
2025 struct bpf_prog *old;
2026 bool up, need_update;
2027 int i, err;
2028
2029 if (prog && !xdp_mtu_valid(priv, dev->mtu))
2030 return -EINVAL;
2031
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002032 if (prog)
2033 bpf_prog_add(prog, priv->num_channels);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002034
2035 up = netif_running(dev);
2036 need_update = (!!priv->xdp_prog != !!prog);
2037
2038 if (up)
2039 dpaa2_eth_stop(dev);
2040
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002041 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2042 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2043 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2044 * so we are sure no old format buffers will be used from now on.
2045 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002046 if (need_update) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002047 err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002048 if (err)
2049 goto out_err;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002050 err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002051 if (err)
2052 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002053 }
2054
2055 old = xchg(&priv->xdp_prog, prog);
2056 if (old)
2057 bpf_prog_put(old);
2058
2059 for (i = 0; i < priv->num_channels; i++) {
2060 ch = priv->channel[i];
2061 old = xchg(&ch->xdp.prog, prog);
2062 if (old)
2063 bpf_prog_put(old);
2064 }
2065
2066 if (up) {
2067 err = dpaa2_eth_open(dev);
2068 if (err)
2069 return err;
2070 }
2071
2072 return 0;
2073
2074out_err:
2075 if (prog)
2076 bpf_prog_sub(prog, priv->num_channels);
2077 if (up)
2078 dpaa2_eth_open(dev);
2079
2080 return err;
2081}
2082
2083static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2084{
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002085 switch (xdp->command) {
2086 case XDP_SETUP_PROG:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002087 return dpaa2_eth_setup_xdp(dev, xdp->prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002088 default:
2089 return -EINVAL;
2090 }
2091
2092 return 0;
2093}
2094
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002095static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2096 struct xdp_frame *xdpf,
2097 struct dpaa2_fd *fd)
Ioana Radulescud678be12019-03-01 17:47:24 +00002098{
2099 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2100 struct device *dev = net_dev->dev.parent;
Ioana Radulescud678be12019-03-01 17:47:24 +00002101 unsigned int needed_headroom;
2102 struct dpaa2_eth_swa *swa;
Ioana Radulescud678be12019-03-01 17:47:24 +00002103 void *buffer_start, *aligned_start;
2104 dma_addr_t addr;
Ioana Radulescud678be12019-03-01 17:47:24 +00002105
2106 /* We require a minimum headroom to be able to transmit the frame.
2107 * Otherwise return an error and let the original net_device handle it
2108 */
2109 needed_headroom = dpaa2_eth_needed_headroom(priv, NULL);
2110 if (xdpf->headroom < needed_headroom)
2111 return -EINVAL;
2112
Ioana Radulescud678be12019-03-01 17:47:24 +00002113 /* Setup the FD fields */
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002114 memset(fd, 0, sizeof(*fd));
Ioana Radulescud678be12019-03-01 17:47:24 +00002115
2116 /* Align FD address, if possible */
2117 buffer_start = xdpf->data - needed_headroom;
2118 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2119 DPAA2_ETH_TX_BUF_ALIGN);
2120 if (aligned_start >= xdpf->data - xdpf->headroom)
2121 buffer_start = aligned_start;
2122
2123 swa = (struct dpaa2_eth_swa *)buffer_start;
2124 /* fill in necessary fields here */
2125 swa->type = DPAA2_ETH_SWA_XDP;
2126 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2127 swa->xdp.xdpf = xdpf;
2128
2129 addr = dma_map_single(dev, buffer_start,
2130 swa->xdp.dma_size,
2131 DMA_BIDIRECTIONAL);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002132 if (unlikely(dma_mapping_error(dev, addr)))
Ioana Radulescud678be12019-03-01 17:47:24 +00002133 return -ENOMEM;
Ioana Radulescud678be12019-03-01 17:47:24 +00002134
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002135 dpaa2_fd_set_addr(fd, addr);
2136 dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2137 dpaa2_fd_set_len(fd, xdpf->len);
2138 dpaa2_fd_set_format(fd, dpaa2_fd_single);
2139 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescud678be12019-03-01 17:47:24 +00002140
2141 return 0;
2142}
2143
2144static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2145 struct xdp_frame **frames, u32 flags)
2146{
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002147 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002148 struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002149 struct rtnl_link_stats64 *percpu_stats;
2150 struct dpaa2_eth_fq *fq;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002151 struct dpaa2_fd *fds;
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002152 int enqueued, i, err;
Ioana Radulescud678be12019-03-01 17:47:24 +00002153
2154 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2155 return -EINVAL;
2156
2157 if (!netif_running(net_dev))
2158 return -ENETDOWN;
2159
Ioana Ciornei8665d972020-04-22 15:05:13 +03002160 fq = &priv->fq[smp_processor_id()];
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002161 xdp_redirect_fds = &fq->xdp_redirect_fds;
2162 fds = xdp_redirect_fds->fds;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002163
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002164 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002165
Ioana Ciornei8665d972020-04-22 15:05:13 +03002166 /* create a FD for each xdp_frame in the list received */
Ioana Radulescud678be12019-03-01 17:47:24 +00002167 for (i = 0; i < n; i++) {
Ioana Ciornei8665d972020-04-22 15:05:13 +03002168 err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2169 if (err)
2170 break;
2171 }
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002172 xdp_redirect_fds->num = i;
Ioana Radulescud678be12019-03-01 17:47:24 +00002173
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002174 /* enqueue all the frame descriptors */
2175 enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
Ioana Radulescud678be12019-03-01 17:47:24 +00002176
Ioana Ciornei8665d972020-04-22 15:05:13 +03002177 /* update statistics */
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002178 percpu_stats->tx_packets += enqueued;
2179 for (i = 0; i < enqueued; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002180 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002181 for (i = enqueued; i < n; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002182 xdp_return_frame_rx_napi(frames[i]);
2183
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002184 return enqueued;
Ioana Radulescud678be12019-03-01 17:47:24 +00002185}
2186
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002187static int update_xps(struct dpaa2_eth_priv *priv)
2188{
2189 struct net_device *net_dev = priv->net_dev;
2190 struct cpumask xps_mask;
2191 struct dpaa2_eth_fq *fq;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002192 int i, num_queues, netdev_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002193 int err = 0;
2194
2195 num_queues = dpaa2_eth_queue_count(priv);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002196 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002197
2198 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2199 * queues, so only process those
2200 */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002201 for (i = 0; i < netdev_queues; i++) {
2202 fq = &priv->fq[i % num_queues];
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002203
2204 cpumask_clear(&xps_mask);
2205 cpumask_set_cpu(fq->target_cpu, &xps_mask);
2206
2207 err = netif_set_xps_queue(net_dev, &xps_mask, i);
2208 if (err) {
2209 netdev_warn_once(net_dev, "Error setting XPS queue\n");
2210 break;
2211 }
2212 }
2213
2214 return err;
2215}
2216
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002217static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2218 struct tc_mqprio_qopt *mqprio)
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002219{
2220 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002221 u8 num_tc, num_queues;
2222 int i;
2223
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002224 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2225 num_queues = dpaa2_eth_queue_count(priv);
2226 num_tc = mqprio->num_tc;
2227
2228 if (num_tc == net_dev->num_tc)
2229 return 0;
2230
2231 if (num_tc > dpaa2_eth_tc_count(priv)) {
2232 netdev_err(net_dev, "Max %d traffic classes supported\n",
2233 dpaa2_eth_tc_count(priv));
Jesper Dangaard Brouerb89c1e62020-04-23 16:57:50 +02002234 return -EOPNOTSUPP;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002235 }
2236
2237 if (!num_tc) {
2238 netdev_reset_tc(net_dev);
2239 netif_set_real_num_tx_queues(net_dev, num_queues);
2240 goto out;
2241 }
2242
2243 netdev_set_num_tc(net_dev, num_tc);
2244 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2245
2246 for (i = 0; i < num_tc; i++)
2247 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2248
2249out:
2250 update_xps(priv);
2251
2252 return 0;
2253}
2254
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002255#define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2256
2257static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2258{
2259 struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2260 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2261 struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2262 struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2263 int err;
2264
2265 if (p->command == TC_TBF_STATS)
2266 return -EOPNOTSUPP;
2267
2268 /* Only per port Tx shaping */
2269 if (p->parent != TC_H_ROOT)
2270 return -EOPNOTSUPP;
2271
2272 if (p->command == TC_TBF_REPLACE) {
2273 if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2274 netdev_err(net_dev, "burst size cannot be greater than %d\n",
2275 DPAA2_ETH_MAX_BURST_SIZE);
2276 return -EINVAL;
2277 }
2278
2279 tx_cr_shaper.max_burst_size = cfg->max_size;
2280 /* The TBF interface is in bytes/s, whereas DPAA2 expects the
2281 * rate in Mbits/s
2282 */
2283 tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2284 }
2285
2286 err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2287 &tx_er_shaper, 0);
2288 if (err) {
2289 netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2290 return err;
2291 }
2292
2293 return 0;
2294}
2295
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002296static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2297 enum tc_setup_type type, void *type_data)
2298{
2299 switch (type) {
2300 case TC_SETUP_QDISC_MQPRIO:
2301 return dpaa2_eth_setup_mqprio(net_dev, type_data);
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002302 case TC_SETUP_QDISC_TBF:
2303 return dpaa2_eth_setup_tbf(net_dev, type_data);
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002304 default:
2305 return -EOPNOTSUPP;
2306 }
2307}
2308
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002309static const struct net_device_ops dpaa2_eth_ops = {
2310 .ndo_open = dpaa2_eth_open,
2311 .ndo_start_xmit = dpaa2_eth_tx,
2312 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002313 .ndo_set_mac_address = dpaa2_eth_set_addr,
2314 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002315 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2316 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08002317 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002318 .ndo_change_mtu = dpaa2_eth_change_mtu,
2319 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescud678be12019-03-01 17:47:24 +00002320 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002321 .ndo_setup_tc = dpaa2_eth_setup_tc,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002322};
2323
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002324static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002325{
2326 struct dpaa2_eth_channel *ch;
2327
2328 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002329
2330 /* Update NAPI statistics */
2331 ch->stats.cdan++;
2332
Jiafei Pan6c33ae12020-08-03 23:10:08 +03002333 napi_schedule(&ch->napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002334}
2335
2336/* Allocate and configure a DPCON object */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002337static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002338{
2339 struct fsl_mc_device *dpcon;
2340 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002341 int err;
2342
2343 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2344 FSL_MC_POOL_DPCON, &dpcon);
2345 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002346 if (err == -ENXIO)
2347 err = -EPROBE_DEFER;
2348 else
2349 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2350 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002351 }
2352
2353 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2354 if (err) {
2355 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002356 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002357 }
2358
2359 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2360 if (err) {
2361 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002362 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002363 }
2364
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002365 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2366 if (err) {
2367 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002368 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002369 }
2370
2371 return dpcon;
2372
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002373close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002374 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002375free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002376 fsl_mc_object_free(dpcon);
2377
YueHaibing02afa9c2020-08-04 21:26:43 +08002378 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002379}
2380
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002381static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
2382 struct fsl_mc_device *dpcon)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002383{
2384 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2385 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2386 fsl_mc_object_free(dpcon);
2387}
2388
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002389static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002390{
2391 struct dpaa2_eth_channel *channel;
2392 struct dpcon_attr attr;
2393 struct device *dev = priv->net_dev->dev.parent;
2394 int err;
2395
2396 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2397 if (!channel)
2398 return NULL;
2399
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002400 channel->dpcon = dpaa2_eth_setup_dpcon(priv);
YueHaibing02afa9c2020-08-04 21:26:43 +08002401 if (IS_ERR(channel->dpcon)) {
2402 err = PTR_ERR(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002403 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002404 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002405
2406 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2407 &attr);
2408 if (err) {
2409 dev_err(dev, "dpcon_get_attributes() failed\n");
2410 goto err_get_attr;
2411 }
2412
2413 channel->dpcon_id = attr.id;
2414 channel->ch_id = attr.qbman_ch_id;
2415 channel->priv = priv;
2416
2417 return channel;
2418
2419err_get_attr:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002420 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002421err_setup:
2422 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002423 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002424}
2425
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002426static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
2427 struct dpaa2_eth_channel *channel)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002428{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002429 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002430 kfree(channel);
2431}
2432
2433/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2434 * and register data availability notifications
2435 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002436static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002437{
2438 struct dpaa2_io_notification_ctx *nctx;
2439 struct dpaa2_eth_channel *channel;
2440 struct dpcon_notification_cfg dpcon_notif_cfg;
2441 struct device *dev = priv->net_dev->dev.parent;
2442 int i, err;
2443
2444 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2445 * many cores as possible, so we need one channel for each core
2446 * (unless there's fewer queues than cores, in which case the extra
2447 * channels would be wasted).
2448 * Allocate one channel per core and register it to the core's
2449 * affine DPIO. If not enough channels are available for all cores
2450 * or if some cores don't have an affine DPIO, there will be no
2451 * ingress frame processing on those cores.
2452 */
2453 cpumask_clear(&priv->dpio_cpumask);
2454 for_each_online_cpu(i) {
2455 /* Try to allocate a channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002456 channel = dpaa2_eth_alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002457 if (IS_ERR_OR_NULL(channel)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002458 err = PTR_ERR_OR_ZERO(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002459 if (err != -EPROBE_DEFER)
2460 dev_info(dev,
2461 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002462 goto err_alloc_ch;
2463 }
2464
2465 priv->channel[priv->num_channels] = channel;
2466
2467 nctx = &channel->nctx;
2468 nctx->is_cdan = 1;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002469 nctx->cb = dpaa2_eth_cdan_cb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002470 nctx->id = channel->ch_id;
2471 nctx->desired_cpu = i;
2472
2473 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06002474 channel->dpio = dpaa2_io_service_select(i);
Ioana Ciornei47441f72018-12-10 16:50:19 +00002475 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002476 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002477 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002478 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002479 * none available for next cores either. Signal we want
2480 * to retry later, in case the DPIO devices weren't
2481 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002482 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002483 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002484 goto err_service_reg;
2485 }
2486
2487 /* Register DPCON notification with MC */
2488 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2489 dpcon_notif_cfg.priority = 0;
2490 dpcon_notif_cfg.user_ctx = nctx->qman64;
2491 err = dpcon_set_notification(priv->mc_io, 0,
2492 channel->dpcon->mc_handle,
2493 &dpcon_notif_cfg);
2494 if (err) {
2495 dev_err(dev, "dpcon_set_notification failed()\n");
2496 goto err_set_cdan;
2497 }
2498
2499 /* If we managed to allocate a channel and also found an affine
2500 * DPIO for this core, add it to the final mask
2501 */
2502 cpumask_set_cpu(i, &priv->dpio_cpumask);
2503 priv->num_channels++;
2504
2505 /* Stop if we already have enough channels to accommodate all
2506 * RX and TX conf queues
2507 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00002508 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002509 break;
2510 }
2511
2512 return 0;
2513
2514err_set_cdan:
Ioana Ciornei47441f72018-12-10 16:50:19 +00002515 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002516err_service_reg:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002517 dpaa2_eth_free_channel(priv, channel);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002518err_alloc_ch:
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002519 if (err == -EPROBE_DEFER) {
2520 for (i = 0; i < priv->num_channels; i++) {
2521 channel = priv->channel[i];
2522 nctx = &channel->nctx;
2523 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002524 dpaa2_eth_free_channel(priv, channel);
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002525 }
2526 priv->num_channels = 0;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002527 return err;
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002528 }
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002529
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002530 if (cpumask_empty(&priv->dpio_cpumask)) {
2531 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002532 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002533 }
2534
2535 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2536 cpumask_pr_args(&priv->dpio_cpumask));
2537
2538 return 0;
2539}
2540
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002541static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002542{
Ioana Ciornei47441f72018-12-10 16:50:19 +00002543 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002544 struct dpaa2_eth_channel *ch;
Ioana Ciornei47441f72018-12-10 16:50:19 +00002545 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002546
2547 /* deregister CDAN notifications and free channels */
2548 for (i = 0; i < priv->num_channels; i++) {
2549 ch = priv->channel[i];
Ioana Ciornei47441f72018-12-10 16:50:19 +00002550 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002551 dpaa2_eth_free_channel(priv, ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002552 }
2553}
2554
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002555static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
2556 int cpu)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002557{
2558 struct device *dev = priv->net_dev->dev.parent;
2559 int i;
2560
2561 for (i = 0; i < priv->num_channels; i++)
2562 if (priv->channel[i]->nctx.desired_cpu == cpu)
2563 return priv->channel[i];
2564
2565 /* We should never get here. Issue a warning and return
2566 * the first channel, because it's still better than nothing
2567 */
2568 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2569
2570 return priv->channel[0];
2571}
2572
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002573static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002574{
2575 struct device *dev = priv->net_dev->dev.parent;
2576 struct dpaa2_eth_fq *fq;
2577 int rx_cpu, txc_cpu;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002578 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002579
2580 /* For each FQ, pick one channel/CPU to deliver frames to.
2581 * This may well change at runtime, either through irqbalance or
2582 * through direct user intervention.
2583 */
2584 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2585
2586 for (i = 0; i < priv->num_fqs; i++) {
2587 fq = &priv->fq[i];
2588 switch (fq->type) {
2589 case DPAA2_RX_FQ:
2590 fq->target_cpu = rx_cpu;
2591 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2592 if (rx_cpu >= nr_cpu_ids)
2593 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2594 break;
2595 case DPAA2_TX_CONF_FQ:
2596 fq->target_cpu = txc_cpu;
2597 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2598 if (txc_cpu >= nr_cpu_ids)
2599 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2600 break;
2601 default:
2602 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2603 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002604 fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002605 }
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002606
2607 update_xps(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002608}
2609
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002610static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002611{
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002612 int i, j;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002613
2614 /* We have one TxConf FQ per Tx flow.
2615 * The number of Tx and Rx queues is the same.
2616 * Tx queues come first in the fq array.
2617 */
2618 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2619 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2620 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2621 priv->fq[priv->num_fqs++].flowid = (u16)i;
2622 }
2623
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002624 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2625 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2626 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2627 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2628 priv->fq[priv->num_fqs].tc = (u8)j;
2629 priv->fq[priv->num_fqs++].flowid = (u16)i;
2630 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002631 }
2632
2633 /* For each FQ, decide on which core to process incoming frames */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002634 dpaa2_eth_set_fq_affinity(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002635}
2636
2637/* Allocate and configure one buffer pool for each interface */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002638static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002639{
2640 int err;
2641 struct fsl_mc_device *dpbp_dev;
2642 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002643 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002644
2645 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2646 &dpbp_dev);
2647 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002648 if (err == -ENXIO)
2649 err = -EPROBE_DEFER;
2650 else
2651 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002652 return err;
2653 }
2654
2655 priv->dpbp_dev = dpbp_dev;
2656
2657 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2658 &dpbp_dev->mc_handle);
2659 if (err) {
2660 dev_err(dev, "dpbp_open() failed\n");
2661 goto err_open;
2662 }
2663
Ioana Radulescud00defe2017-06-06 10:00:32 -05002664 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2665 if (err) {
2666 dev_err(dev, "dpbp_reset() failed\n");
2667 goto err_reset;
2668 }
2669
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002670 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2671 if (err) {
2672 dev_err(dev, "dpbp_enable() failed\n");
2673 goto err_enable;
2674 }
2675
2676 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002677 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002678 if (err) {
2679 dev_err(dev, "dpbp_get_attributes() failed\n");
2680 goto err_get_attr;
2681 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002682 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002683
2684 return 0;
2685
2686err_get_attr:
2687 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2688err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002689err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002690 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2691err_open:
2692 fsl_mc_object_free(dpbp_dev);
2693
2694 return err;
2695}
2696
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002697static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002698{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002699 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002700 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2701 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2702 fsl_mc_object_free(priv->dpbp_dev);
2703}
2704
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002705static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002706{
2707 struct device *dev = priv->net_dev->dev.parent;
2708 struct dpni_buffer_layout buf_layout = {0};
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002709 u16 rx_buf_align;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002710 int err;
2711
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002712 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2713 * version, this number is not always provided correctly on rev1.
2714 * We need to check for both alternatives in this situation.
2715 */
2716 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2717 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002718 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002719 else
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002720 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002721
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03002722 /* We need to ensure that the buffer size seen by WRIOP is a multiple
2723 * of 64 or 256 bytes depending on the WRIOP version.
2724 */
2725 priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
2726
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002727 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002728 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002729 buf_layout.pass_timestamp = true;
2730 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2731 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002732 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2733 DPNI_QUEUE_TX, &buf_layout);
2734 if (err) {
2735 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2736 return err;
2737 }
2738
2739 /* tx-confirm buffer */
Ioana Radulescu859f9982018-04-26 18:23:47 +08002740 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002741 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2742 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2743 if (err) {
2744 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2745 return err;
2746 }
2747
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002748 /* Now that we've set our tx buffer layout, retrieve the minimum
2749 * required tx data offset.
2750 */
2751 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2752 &priv->tx_data_offset);
2753 if (err) {
2754 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2755 return err;
2756 }
2757
2758 if ((priv->tx_data_offset % 64) != 0)
2759 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2760 priv->tx_data_offset);
2761
2762 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06002763 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002764 buf_layout.pass_parser_result = true;
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002765 buf_layout.data_align = rx_buf_align;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002766 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2767 buf_layout.private_data_size = 0;
2768 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2769 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2770 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08002771 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2772 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002773 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2774 DPNI_QUEUE_RX, &buf_layout);
2775 if (err) {
2776 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2777 return err;
2778 }
2779
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002780 return 0;
2781}
2782
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002783#define DPNI_ENQUEUE_FQID_VER_MAJOR 7
2784#define DPNI_ENQUEUE_FQID_VER_MINOR 9
2785
2786static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2787 struct dpaa2_eth_fq *fq,
Ioana Ciornei48c04812020-04-22 15:05:10 +03002788 struct dpaa2_fd *fd, u8 prio,
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002789 u32 num_frames __always_unused,
Ioana Ciornei48c04812020-04-22 15:05:10 +03002790 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002791{
Ioana Ciornei48c04812020-04-22 15:05:10 +03002792 int err;
2793
2794 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
2795 priv->tx_qdid, prio,
2796 fq->tx_qdbin, fd);
2797 if (!err && frames_enqueued)
2798 *frames_enqueued = 1;
2799 return err;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002800}
2801
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002802static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
2803 struct dpaa2_eth_fq *fq,
2804 struct dpaa2_fd *fd,
2805 u8 prio, u32 num_frames,
2806 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002807{
Ioana Ciornei48c04812020-04-22 15:05:10 +03002808 int err;
2809
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002810 err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
2811 fq->tx_fqid[prio],
2812 fd, num_frames);
2813
2814 if (err == 0)
2815 return -EBUSY;
2816
2817 if (frames_enqueued)
2818 *frames_enqueued = err;
2819 return 0;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002820}
2821
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002822static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002823{
2824 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2825 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2826 priv->enqueue = dpaa2_eth_enqueue_qd;
2827 else
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002828 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002829}
2830
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002831static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03002832{
2833 struct device *dev = priv->net_dev->dev.parent;
2834 struct dpni_link_cfg link_cfg = {0};
2835 int err;
2836
2837 /* Get the default link options so we don't override other flags */
2838 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2839 if (err) {
2840 dev_err(dev, "dpni_get_link_cfg() failed\n");
2841 return err;
2842 }
2843
2844 /* By default, enable both Rx and Tx pause frames */
2845 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
2846 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
2847 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2848 if (err) {
2849 dev_err(dev, "dpni_set_link_cfg() failed\n");
2850 return err;
2851 }
2852
2853 priv->link_state.options = link_cfg.options;
2854
2855 return 0;
2856}
2857
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002858static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
Ioana Radulescua690af4f2019-10-16 10:36:23 +03002859{
2860 struct dpni_queue_id qid = {0};
2861 struct dpaa2_eth_fq *fq;
2862 struct dpni_queue queue;
2863 int i, j, err;
2864
2865 /* We only use Tx FQIDs for FQID-based enqueue, so check
2866 * if DPNI version supports it before updating FQIDs
2867 */
2868 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2869 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2870 return;
2871
2872 for (i = 0; i < priv->num_fqs; i++) {
2873 fq = &priv->fq[i];
2874 if (fq->type != DPAA2_TX_CONF_FQ)
2875 continue;
2876 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2877 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2878 DPNI_QUEUE_TX, j, fq->flowid,
2879 &queue, &qid);
2880 if (err)
2881 goto out_err;
2882
2883 fq->tx_fqid[j] = qid.fqid;
2884 if (fq->tx_fqid[j] == 0)
2885 goto out_err;
2886 }
2887 }
2888
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002889 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Radulescua690af4f2019-10-16 10:36:23 +03002890
2891 return;
2892
2893out_err:
2894 netdev_info(priv->net_dev,
2895 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
2896 priv->enqueue = dpaa2_eth_enqueue_qd;
2897}
2898
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03002899/* Configure ingress classification based on VLAN PCP */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002900static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03002901{
2902 struct device *dev = priv->net_dev->dev.parent;
2903 struct dpkg_profile_cfg kg_cfg = {0};
2904 struct dpni_qos_tbl_cfg qos_cfg = {0};
2905 struct dpni_rule_cfg key_params;
2906 void *dma_mem, *key, *mask;
2907 u8 key_size = 2; /* VLAN TCI field */
2908 int i, pcp, err;
2909
2910 /* VLAN-based classification only makes sense if we have multiple
2911 * traffic classes.
2912 * Also, we need to extract just the 3-bit PCP field from the VLAN
2913 * header and we can only do that by using a mask
2914 */
2915 if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
2916 dev_dbg(dev, "VLAN-based QoS classification not supported\n");
2917 return -EOPNOTSUPP;
2918 }
2919
2920 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
2921 if (!dma_mem)
2922 return -ENOMEM;
2923
2924 kg_cfg.num_extracts = 1;
2925 kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
2926 kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
2927 kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
2928 kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
2929
2930 err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
2931 if (err) {
2932 dev_err(dev, "dpni_prepare_key_cfg failed\n");
2933 goto out_free_tbl;
2934 }
2935
2936 /* set QoS table */
2937 qos_cfg.default_tc = 0;
2938 qos_cfg.discard_on_miss = 0;
2939 qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
2940 DPAA2_CLASSIFIER_DMA_SIZE,
2941 DMA_TO_DEVICE);
2942 if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
2943 dev_err(dev, "QoS table DMA mapping failed\n");
2944 err = -ENOMEM;
2945 goto out_free_tbl;
2946 }
2947
2948 err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
2949 if (err) {
2950 dev_err(dev, "dpni_set_qos_table failed\n");
2951 goto out_unmap_tbl;
2952 }
2953
2954 /* Add QoS table entries */
2955 key = kzalloc(key_size * 2, GFP_KERNEL);
2956 if (!key) {
2957 err = -ENOMEM;
2958 goto out_unmap_tbl;
2959 }
2960 mask = key + key_size;
2961 *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
2962
2963 key_params.key_iova = dma_map_single(dev, key, key_size * 2,
2964 DMA_TO_DEVICE);
2965 if (dma_mapping_error(dev, key_params.key_iova)) {
2966 dev_err(dev, "Qos table entry DMA mapping failed\n");
2967 err = -ENOMEM;
2968 goto out_free_key;
2969 }
2970
2971 key_params.mask_iova = key_params.key_iova + key_size;
2972 key_params.key_size = key_size;
2973
2974 /* We add rules for PCP-based distribution starting with highest
2975 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
2976 * classes to accommodate all priority levels, the lowest ones end up
2977 * on TC 0 which was configured as default
2978 */
2979 for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
2980 *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
2981 dma_sync_single_for_device(dev, key_params.key_iova,
2982 key_size * 2, DMA_TO_DEVICE);
2983
2984 err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
2985 &key_params, i, i);
2986 if (err) {
2987 dev_err(dev, "dpni_add_qos_entry failed\n");
2988 dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
2989 goto out_unmap_key;
2990 }
2991 }
2992
2993 priv->vlan_cls_enabled = true;
2994
2995 /* Table and key memory is not persistent, clean everything up after
2996 * configuration is finished
2997 */
2998out_unmap_key:
2999 dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3000out_free_key:
3001 kfree(key);
3002out_unmap_tbl:
3003 dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3004 DMA_TO_DEVICE);
3005out_free_tbl:
3006 kfree(dma_mem);
3007
3008 return err;
3009}
3010
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003011/* Configure the DPNI object this interface is associated with */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003012static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003013{
3014 struct device *dev = &ls_dev->dev;
3015 struct dpaa2_eth_priv *priv;
3016 struct net_device *net_dev;
3017 int err;
3018
3019 net_dev = dev_get_drvdata(dev);
3020 priv = netdev_priv(net_dev);
3021
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003022 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05003023 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003024 if (err) {
3025 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003026 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003027 }
3028
Ioana Radulescu311cffa2018-03-23 08:44:09 -05003029 /* Check if we can work with this DPNI object */
3030 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3031 &priv->dpni_ver_minor);
3032 if (err) {
3033 dev_err(dev, "dpni_get_api_version() failed\n");
3034 goto close;
3035 }
3036 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3037 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3038 priv->dpni_ver_major, priv->dpni_ver_minor,
3039 DPNI_VER_MAJOR, DPNI_VER_MINOR);
3040 err = -ENOTSUPP;
3041 goto close;
3042 }
3043
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003044 ls_dev->mc_io = priv->mc_io;
3045 ls_dev->mc_handle = priv->mc_token;
3046
3047 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3048 if (err) {
3049 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003050 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003051 }
3052
3053 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3054 &priv->dpni_attrs);
3055 if (err) {
3056 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003057 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003058 }
3059
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003060 err = dpaa2_eth_set_buffer_layout(priv);
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003061 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003062 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003063
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003064 dpaa2_eth_set_enqueue_mode(priv);
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003065
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003066 /* Enable pause frame support */
3067 if (dpaa2_eth_has_pause_support(priv)) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003068 err = dpaa2_eth_set_pause(priv);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003069 if (err)
3070 goto close;
3071 }
3072
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003073 err = dpaa2_eth_set_vlan_qos(priv);
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003074 if (err && err != -EOPNOTSUPP)
3075 goto close;
3076
Xu Wang9334d5b2020-06-11 02:45:20 +00003077 priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3078 sizeof(struct dpaa2_eth_cls_rule),
3079 GFP_KERNEL);
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003080 if (!priv->cls_rules) {
3081 err = -ENOMEM;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003082 goto close;
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003083 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003084
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003085 return 0;
3086
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003087close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003088 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003089
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003090 return err;
3091}
3092
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003093static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003094{
3095 int err;
3096
3097 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3098 if (err)
3099 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3100 err);
3101
3102 dpni_close(priv->mc_io, 0, priv->mc_token);
3103}
3104
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003105static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3106 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003107{
3108 struct device *dev = priv->net_dev->dev.parent;
3109 struct dpni_queue queue;
3110 struct dpni_queue_id qid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003111 int err;
3112
3113 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003114 DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003115 if (err) {
3116 dev_err(dev, "dpni_get_queue(RX) failed\n");
3117 return err;
3118 }
3119
3120 fq->fqid = qid.fqid;
3121
3122 queue.destination.id = fq->channel->dpcon_id;
3123 queue.destination.type = DPNI_DEST_DPCON;
3124 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003125 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003126 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003127 DPNI_QUEUE_RX, fq->tc, fq->flowid,
Ioana Radulescu16fa1cf2019-05-23 17:38:22 +03003128 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003129 &queue);
3130 if (err) {
3131 dev_err(dev, "dpni_set_queue(RX) failed\n");
3132 return err;
3133 }
3134
Ioana Radulescud678be12019-03-01 17:47:24 +00003135 /* xdp_rxq setup */
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003136 /* only once for each channel */
3137 if (fq->tc > 0)
3138 return 0;
3139
Ioana Radulescud678be12019-03-01 17:47:24 +00003140 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3141 fq->flowid);
3142 if (err) {
3143 dev_err(dev, "xdp_rxq_info_reg failed\n");
3144 return err;
3145 }
3146
3147 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3148 MEM_TYPE_PAGE_ORDER0, NULL);
3149 if (err) {
3150 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3151 return err;
3152 }
3153
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003154 return 0;
3155}
3156
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003157static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3158 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003159{
3160 struct device *dev = priv->net_dev->dev.parent;
3161 struct dpni_queue queue;
3162 struct dpni_queue_id qid;
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003163 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003164
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003165 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3166 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3167 DPNI_QUEUE_TX, i, fq->flowid,
3168 &queue, &qid);
3169 if (err) {
3170 dev_err(dev, "dpni_get_queue(TX) failed\n");
3171 return err;
3172 }
3173 fq->tx_fqid[i] = qid.fqid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003174 }
3175
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003176 /* All Tx queues belonging to the same flowid have the same qdbin */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003177 fq->tx_qdbin = qid.qdbin;
3178
3179 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3180 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3181 &queue, &qid);
3182 if (err) {
3183 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3184 return err;
3185 }
3186
3187 fq->fqid = qid.fqid;
3188
3189 queue.destination.id = fq->channel->dpcon_id;
3190 queue.destination.type = DPNI_DEST_DPCON;
3191 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003192 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003193 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3194 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3195 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3196 &queue);
3197 if (err) {
3198 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3199 return err;
3200 }
3201
3202 return 0;
3203}
3204
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003205/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003206static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003207 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003208 /* L2 header */
3209 .rxnfc_field = RXH_L2DA,
3210 .cls_prot = NET_PROT_ETH,
3211 .cls_field = NH_FLD_ETH_DA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003212 .id = DPAA2_ETH_DIST_ETHDST,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003213 .size = 6,
3214 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003215 .cls_prot = NET_PROT_ETH,
3216 .cls_field = NH_FLD_ETH_SA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003217 .id = DPAA2_ETH_DIST_ETHSRC,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003218 .size = 6,
3219 }, {
3220 /* This is the last ethertype field parsed:
3221 * depending on frame format, it can be the MAC ethertype
3222 * or the VLAN etype.
3223 */
3224 .cls_prot = NET_PROT_ETH,
3225 .cls_field = NH_FLD_ETH_TYPE,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003226 .id = DPAA2_ETH_DIST_ETHTYPE,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003227 .size = 2,
3228 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003229 /* VLAN header */
3230 .rxnfc_field = RXH_VLAN,
3231 .cls_prot = NET_PROT_VLAN,
3232 .cls_field = NH_FLD_VLAN_TCI,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003233 .id = DPAA2_ETH_DIST_VLAN,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003234 .size = 2,
3235 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003236 /* IP header */
3237 .rxnfc_field = RXH_IP_SRC,
3238 .cls_prot = NET_PROT_IP,
3239 .cls_field = NH_FLD_IP_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003240 .id = DPAA2_ETH_DIST_IPSRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003241 .size = 4,
3242 }, {
3243 .rxnfc_field = RXH_IP_DST,
3244 .cls_prot = NET_PROT_IP,
3245 .cls_field = NH_FLD_IP_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003246 .id = DPAA2_ETH_DIST_IPDST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003247 .size = 4,
3248 }, {
3249 .rxnfc_field = RXH_L3_PROTO,
3250 .cls_prot = NET_PROT_IP,
3251 .cls_field = NH_FLD_IP_PROTO,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003252 .id = DPAA2_ETH_DIST_IPPROTO,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003253 .size = 1,
3254 }, {
3255 /* Using UDP ports, this is functionally equivalent to raw
3256 * byte pairs from L4 header.
3257 */
3258 .rxnfc_field = RXH_L4_B_0_1,
3259 .cls_prot = NET_PROT_UDP,
3260 .cls_field = NH_FLD_UDP_PORT_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003261 .id = DPAA2_ETH_DIST_L4SRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003262 .size = 2,
3263 }, {
3264 .rxnfc_field = RXH_L4_B_2_3,
3265 .cls_prot = NET_PROT_UDP,
3266 .cls_field = NH_FLD_UDP_PORT_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003267 .id = DPAA2_ETH_DIST_L4DST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003268 .size = 2,
3269 },
3270};
3271
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003272/* Configure the Rx hash key using the legacy API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003273static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003274{
3275 struct device *dev = priv->net_dev->dev.parent;
3276 struct dpni_rx_tc_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003277 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003278
3279 memset(&dist_cfg, 0, sizeof(dist_cfg));
3280
3281 dist_cfg.key_cfg_iova = key;
3282 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3283 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3284
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003285 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3286 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3287 i, &dist_cfg);
3288 if (err) {
3289 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3290 break;
3291 }
3292 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003293
3294 return err;
3295}
3296
3297/* Configure the Rx hash key using the new API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003298static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003299{
3300 struct device *dev = priv->net_dev->dev.parent;
3301 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003302 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003303
3304 memset(&dist_cfg, 0, sizeof(dist_cfg));
3305
3306 dist_cfg.key_cfg_iova = key;
3307 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3308 dist_cfg.enable = 1;
3309
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003310 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3311 dist_cfg.tc = i;
3312 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3313 &dist_cfg);
3314 if (err) {
3315 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3316 break;
3317 }
3318 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003319
3320 return err;
3321}
3322
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003323/* Configure the Rx flow classification key */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003324static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003325{
3326 struct device *dev = priv->net_dev->dev.parent;
3327 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003328 int i, err = 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003329
3330 memset(&dist_cfg, 0, sizeof(dist_cfg));
3331
3332 dist_cfg.key_cfg_iova = key;
3333 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3334 dist_cfg.enable = 1;
3335
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003336 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3337 dist_cfg.tc = i;
3338 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3339 &dist_cfg);
3340 if (err) {
3341 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3342 break;
3343 }
3344 }
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003345
3346 return err;
3347}
3348
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003349/* Size of the Rx flow classification key */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003350int dpaa2_eth_cls_key_size(u64 fields)
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003351{
3352 int i, size = 0;
3353
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003354 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3355 if (!(fields & dist_fields[i].id))
3356 continue;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003357 size += dist_fields[i].size;
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003358 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003359
3360 return size;
3361}
3362
3363/* Offset of header field in Rx classification key */
3364int dpaa2_eth_cls_fld_off(int prot, int field)
3365{
3366 int i, off = 0;
3367
3368 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3369 if (dist_fields[i].cls_prot == prot &&
3370 dist_fields[i].cls_field == field)
3371 return off;
3372 off += dist_fields[i].size;
3373 }
3374
3375 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3376 return 0;
3377}
3378
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003379/* Prune unused fields from the classification rule.
3380 * Used when masking is not supported
3381 */
3382void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3383{
3384 int off = 0, new_off = 0;
3385 int i, size;
3386
3387 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3388 size = dist_fields[i].size;
3389 if (dist_fields[i].id & fields) {
3390 memcpy(key_mem + new_off, key_mem + off, size);
3391 new_off += size;
3392 }
3393 off += size;
3394 }
3395}
3396
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003397/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003398 * flags is a combination of RXH_ bits
3399 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00003400static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
3401 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003402{
3403 struct device *dev = net_dev->dev.parent;
3404 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3405 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003406 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003407 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003408 u8 *dma_mem;
3409 int i;
3410 int err = 0;
3411
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003412 memset(&cls_cfg, 0, sizeof(cls_cfg));
3413
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003414 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003415 struct dpkg_extract *key =
3416 &cls_cfg.extracts[cls_cfg.num_extracts];
3417
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003418 /* For both Rx hashing and classification keys
3419 * we set only the selected fields.
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003420 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003421 if (!(flags & dist_fields[i].id))
3422 continue;
3423 if (type == DPAA2_ETH_RX_DIST_HASH)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003424 rx_hash_fields |= dist_fields[i].rxnfc_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003425
3426 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
3427 dev_err(dev, "error adding key extraction rule, too many rules?\n");
3428 return -E2BIG;
3429 }
3430
3431 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003432 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003433 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003434 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003435 cls_cfg.num_extracts++;
3436 }
3437
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05003438 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003439 if (!dma_mem)
3440 return -ENOMEM;
3441
3442 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3443 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003444 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003445 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003446 }
3447
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003448 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003449 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3450 DMA_TO_DEVICE);
3451 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003452 dev_err(dev, "DMA mapping failed\n");
3453 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003454 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003455 }
3456
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003457 if (type == DPAA2_ETH_RX_DIST_HASH) {
3458 if (dpaa2_eth_has_legacy_dist(priv))
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003459 err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003460 else
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003461 err = dpaa2_eth_config_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003462 } else {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003463 err = dpaa2_eth_config_cls_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003464 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003465
3466 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3467 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003468 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003469 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003470
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003471free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003472 kfree(dma_mem);
3473 return err;
3474}
3475
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003476int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3477{
3478 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003479 u64 key = 0;
3480 int i;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003481
3482 if (!dpaa2_eth_hash_enabled(priv))
3483 return -EOPNOTSUPP;
3484
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003485 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3486 if (dist_fields[i].rxnfc_field & flags)
3487 key |= dist_fields[i].id;
3488
3489 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003490}
3491
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003492int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3493{
3494 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3495}
3496
3497static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003498{
3499 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003500 int err;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003501
3502 /* Check if we actually support Rx flow classification */
3503 if (dpaa2_eth_has_legacy_dist(priv)) {
3504 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3505 return -EOPNOTSUPP;
3506 }
3507
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003508 if (!dpaa2_eth_fs_enabled(priv)) {
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003509 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3510 return -EOPNOTSUPP;
3511 }
3512
3513 if (!dpaa2_eth_hash_enabled(priv)) {
3514 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3515 return -EOPNOTSUPP;
3516 }
3517
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003518 /* If there is no support for masking in the classification table,
3519 * we don't set a default key, as it will depend on the rules
3520 * added by the user at runtime.
3521 */
3522 if (!dpaa2_eth_fs_mask_enabled(priv))
3523 goto out;
3524
3525 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003526 if (err)
3527 return err;
3528
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003529out:
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003530 priv->rx_cls_enabled = 1;
3531
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003532 return 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003533}
3534
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003535/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3536 * frame queues and channels
3537 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003538static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003539{
3540 struct net_device *net_dev = priv->net_dev;
3541 struct device *dev = net_dev->dev.parent;
3542 struct dpni_pools_cfg pools_params;
3543 struct dpni_error_cfg err_cfg;
3544 int err = 0;
3545 int i;
3546
3547 pools_params.num_dpbp = 1;
3548 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3549 pools_params.pools[0].backup_pool = 0;
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03003550 pools_params.pools[0].buffer_size = priv->rx_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003551 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3552 if (err) {
3553 dev_err(dev, "dpni_set_pools() failed\n");
3554 return err;
3555 }
3556
Ioana Radulescu227686b2018-07-27 09:12:59 -05003557 /* have the interface implicitly distribute traffic based on
3558 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003559 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05003560 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003561 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05003562 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003563
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003564 /* Configure the flow classification key; it includes all
3565 * supported header fields and cannot be modified at runtime
3566 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003567 err = dpaa2_eth_set_default_cls(priv);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003568 if (err && err != -EOPNOTSUPP)
3569 dev_err(dev, "Failed to configure Rx classification key\n");
3570
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003571 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05003572 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003573 err_cfg.set_frame_annotation = 1;
3574 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3575 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3576 &err_cfg);
3577 if (err) {
3578 dev_err(dev, "dpni_set_errors_behavior failed\n");
3579 return err;
3580 }
3581
3582 /* Configure Rx and Tx conf queues to generate CDANs */
3583 for (i = 0; i < priv->num_fqs; i++) {
3584 switch (priv->fq[i].type) {
3585 case DPAA2_RX_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003586 err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003587 break;
3588 case DPAA2_TX_CONF_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003589 err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003590 break;
3591 default:
3592 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3593 return -EINVAL;
3594 }
3595 if (err)
3596 return err;
3597 }
3598
3599 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3600 DPNI_QUEUE_TX, &priv->tx_qdid);
3601 if (err) {
3602 dev_err(dev, "dpni_get_qdid() failed\n");
3603 return err;
3604 }
3605
3606 return 0;
3607}
3608
3609/* Allocate rings for storing incoming frame descriptors */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003610static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003611{
3612 struct net_device *net_dev = priv->net_dev;
3613 struct device *dev = net_dev->dev.parent;
3614 int i;
3615
3616 for (i = 0; i < priv->num_channels; i++) {
3617 priv->channel[i]->store =
3618 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3619 if (!priv->channel[i]->store) {
3620 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3621 goto err_ring;
3622 }
3623 }
3624
3625 return 0;
3626
3627err_ring:
3628 for (i = 0; i < priv->num_channels; i++) {
3629 if (!priv->channel[i]->store)
3630 break;
3631 dpaa2_io_store_destroy(priv->channel[i]->store);
3632 }
3633
3634 return -ENOMEM;
3635}
3636
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003637static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003638{
3639 int i;
3640
3641 for (i = 0; i < priv->num_channels; i++)
3642 dpaa2_io_store_destroy(priv->channel[i]->store);
3643}
3644
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003645static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003646{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003647 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003648 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003649 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003650 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003651
3652 /* Get firmware address, if any */
3653 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3654 if (err) {
3655 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3656 return err;
3657 }
3658
3659 /* Get DPNI attributes address, if any */
3660 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3661 dpni_mac_addr);
3662 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003663 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003664 return err;
3665 }
3666
3667 /* First check if firmware has any address configured by bootloader */
3668 if (!is_zero_ether_addr(mac_addr)) {
3669 /* If the DPMAC addr != DPNI addr, update it */
3670 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
3671 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
3672 priv->mc_token,
3673 mac_addr);
3674 if (err) {
3675 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3676 return err;
3677 }
3678 }
3679 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
3680 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003681 /* No MAC address configured, fill in net_dev->dev_addr
3682 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003683 */
3684 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003685 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
3686
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003687 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3688 net_dev->dev_addr);
3689 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003690 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003691 return err;
3692 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003693
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003694 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
3695 * practical purposes, this will be our "permanent" mac address,
3696 * at least until the next reboot. This move will also permit
3697 * register_netdevice() to properly fill up net_dev->perm_addr.
3698 */
3699 net_dev->addr_assign_type = NET_ADDR_PERM;
3700 } else {
3701 /* NET_ADDR_PERM is default, all we have to do is
3702 * fill in the device addr.
3703 */
3704 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
3705 }
3706
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003707 return 0;
3708}
3709
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003710static int dpaa2_eth_netdev_init(struct net_device *net_dev)
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003711{
3712 struct device *dev = net_dev->dev.parent;
3713 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003714 u32 options = priv->dpni_attrs.options;
3715 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003716 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003717 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003718 int err;
3719
3720 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003721 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003722
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003723 err = dpaa2_eth_set_mac_addr(priv);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003724 if (err)
3725 return err;
3726
3727 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003728 eth_broadcast_addr(bcast_addr);
3729 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3730 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003731 dev_err(dev, "dpni_add_mac_addr() failed\n");
3732 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003733 }
3734
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05003735 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003736 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05003737 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05003738 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05003739 if (err) {
3740 dev_err(dev, "dpni_set_max_frame_length() failed\n");
3741 return err;
3742 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003743
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003744 /* Set actual number of queues in the net device */
3745 num_queues = dpaa2_eth_queue_count(priv);
3746 err = netif_set_real_num_tx_queues(net_dev, num_queues);
3747 if (err) {
3748 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
3749 return err;
3750 }
3751 err = netif_set_real_num_rx_queues(net_dev, num_queues);
3752 if (err) {
3753 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
3754 return err;
3755 }
3756
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003757 /* Capabilities listing */
3758 supported |= IFF_LIVE_ADDR_CHANGE;
3759
3760 if (options & DPNI_OPT_NO_MAC_FILTER)
3761 not_supported |= IFF_UNICAST_FLT;
3762 else
3763 supported |= IFF_UNICAST_FLT;
3764
3765 net_dev->priv_flags |= supported;
3766 net_dev->priv_flags &= ~not_supported;
3767
3768 /* Features */
3769 net_dev->features = NETIF_F_RXCSUM |
3770 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3771 NETIF_F_SG | NETIF_F_HIGHDMA |
Ioana Ciornei3657cda2020-07-21 19:38:25 +03003772 NETIF_F_LLTX | NETIF_F_HW_TC;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003773 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003774
3775 return 0;
3776}
3777
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003778static int dpaa2_eth_poll_link_state(void *arg)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003779{
3780 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
3781 int err;
3782
3783 while (!kthread_should_stop()) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003784 err = dpaa2_eth_link_state_update(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003785 if (unlikely(err))
3786 return err;
3787
3788 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
3789 }
3790
3791 return 0;
3792}
3793
Ioana Ciornei71947922019-10-31 01:18:31 +02003794static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
3795{
3796 struct fsl_mc_device *dpni_dev, *dpmac_dev;
3797 struct dpaa2_mac *mac;
3798 int err;
3799
3800 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
3801 dpmac_dev = fsl_mc_get_endpoint(dpni_dev);
Ioana Ciornei841eb402020-07-14 15:08:16 +03003802 if (IS_ERR_OR_NULL(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
Ioana Ciornei71947922019-10-31 01:18:31 +02003803 return 0;
3804
3805 if (dpaa2_mac_is_type_fixed(dpmac_dev, priv->mc_io))
3806 return 0;
3807
3808 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
3809 if (!mac)
3810 return -ENOMEM;
3811
3812 mac->mc_dev = dpmac_dev;
3813 mac->mc_io = priv->mc_io;
3814 mac->net_dev = priv->net_dev;
3815
3816 err = dpaa2_mac_connect(mac);
3817 if (err) {
3818 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint\n");
3819 kfree(mac);
3820 return err;
3821 }
3822 priv->mac = mac;
3823
3824 return 0;
3825}
3826
3827static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
3828{
3829 if (!priv->mac)
3830 return;
3831
3832 dpaa2_mac_disconnect(priv->mac);
3833 kfree(priv->mac);
3834 priv->mac = NULL;
3835}
3836
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003837static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
3838{
Ioana Radulescu112197d2017-10-11 08:29:49 -05003839 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003840 struct device *dev = (struct device *)arg;
3841 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
3842 struct net_device *net_dev = dev_get_drvdata(dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02003843 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003844 int err;
3845
3846 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
3847 DPNI_IRQ_INDEX, &status);
3848 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003849 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05003850 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003851 }
3852
Ioana Radulescu112197d2017-10-11 08:29:49 -05003853 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003854 dpaa2_eth_link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003855
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02003856 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003857 dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
3858 dpaa2_eth_update_tx_fqids(priv);
Ioana Ciornei71947922019-10-31 01:18:31 +02003859
3860 rtnl_lock();
3861 if (priv->mac)
3862 dpaa2_eth_disconnect_mac(priv);
3863 else
3864 dpaa2_eth_connect_mac(priv);
3865 rtnl_unlock();
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02003866 }
Florin Chiculita8398b372019-10-16 10:36:22 +03003867
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003868 return IRQ_HANDLED;
3869}
3870
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003871static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003872{
3873 int err = 0;
3874 struct fsl_mc_device_irq *irq;
3875
3876 err = fsl_mc_allocate_irqs(ls_dev);
3877 if (err) {
3878 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
3879 return err;
3880 }
3881
3882 irq = ls_dev->irqs[0];
3883 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05003884 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003885 IRQF_NO_SUSPEND | IRQF_ONESHOT,
3886 dev_name(&ls_dev->dev), &ls_dev->dev);
3887 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003888 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003889 goto free_mc_irq;
3890 }
3891
3892 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
Florin Chiculita8398b372019-10-16 10:36:22 +03003893 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
3894 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003895 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003896 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003897 goto free_irq;
3898 }
3899
3900 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
3901 DPNI_IRQ_INDEX, 1);
3902 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003903 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003904 goto free_irq;
3905 }
3906
3907 return 0;
3908
3909free_irq:
3910 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
3911free_mc_irq:
3912 fsl_mc_free_irqs(ls_dev);
3913
3914 return err;
3915}
3916
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003917static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003918{
3919 int i;
3920 struct dpaa2_eth_channel *ch;
3921
3922 for (i = 0; i < priv->num_channels; i++) {
3923 ch = priv->channel[i];
3924 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
3925 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
3926 NAPI_POLL_WEIGHT);
3927 }
3928}
3929
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003930static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003931{
3932 int i;
3933 struct dpaa2_eth_channel *ch;
3934
3935 for (i = 0; i < priv->num_channels; i++) {
3936 ch = priv->channel[i];
3937 netif_napi_del(&ch->napi);
3938 }
3939}
3940
3941static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
3942{
3943 struct device *dev;
3944 struct net_device *net_dev = NULL;
3945 struct dpaa2_eth_priv *priv = NULL;
3946 int err = 0;
3947
3948 dev = &dpni_dev->dev;
3949
3950 /* Net device */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03003951 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003952 if (!net_dev) {
3953 dev_err(dev, "alloc_etherdev_mq() failed\n");
3954 return -ENOMEM;
3955 }
3956
3957 SET_NETDEV_DEV(net_dev, dev);
3958 dev_set_drvdata(dev, net_dev);
3959
3960 priv = netdev_priv(net_dev);
3961 priv->net_dev = net_dev;
3962
Ioana Radulescu08eb2392017-05-24 07:13:27 -05003963 priv->iommu_domain = iommu_get_domain_for_dev(dev);
3964
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003965 /* Obtain a MC portal */
3966 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3967 &priv->mc_io);
3968 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05003969 if (err == -ENXIO)
3970 err = -EPROBE_DEFER;
3971 else
3972 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003973 goto err_portal_alloc;
3974 }
3975
3976 /* MC objects initialization and configuration */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003977 err = dpaa2_eth_setup_dpni(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003978 if (err)
3979 goto err_dpni_setup;
3980
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003981 err = dpaa2_eth_setup_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003982 if (err)
3983 goto err_dpio_setup;
3984
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003985 dpaa2_eth_setup_fqs(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003986
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003987 err = dpaa2_eth_setup_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003988 if (err)
3989 goto err_dpbp_setup;
3990
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003991 err = dpaa2_eth_bind_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003992 if (err)
3993 goto err_bind;
3994
3995 /* Add a NAPI context for each channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003996 dpaa2_eth_add_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003997
3998 /* Percpu statistics */
3999 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4000 if (!priv->percpu_stats) {
4001 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4002 err = -ENOMEM;
4003 goto err_alloc_percpu_stats;
4004 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004005 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4006 if (!priv->percpu_extras) {
4007 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4008 err = -ENOMEM;
4009 goto err_alloc_percpu_extras;
4010 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004011
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004012 priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4013 if (!priv->sgt_cache) {
4014 dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4015 err = -ENOMEM;
4016 goto err_alloc_sgt_cache;
4017 }
4018
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004019 err = dpaa2_eth_netdev_init(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004020 if (err)
4021 goto err_netdev_init;
4022
4023 /* Configure checksum offload based on current interface flags */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004024 err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004025 if (err)
4026 goto err_csum;
4027
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004028 err = dpaa2_eth_set_tx_csum(priv,
4029 !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004030 if (err)
4031 goto err_csum;
4032
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004033 err = dpaa2_eth_alloc_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004034 if (err)
4035 goto err_alloc_rings;
4036
Ioana Ciorneif395b692020-05-31 00:08:13 +03004037#ifdef CONFIG_FSL_DPAA2_ETH_DCB
4038 if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4039 priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4040 net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4041 } else {
4042 dev_dbg(dev, "PFC not supported\n");
4043 }
4044#endif
4045
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004046 err = dpaa2_eth_setup_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004047 if (err) {
4048 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004049 priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004050 "%s_poll_link", net_dev->name);
4051 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004052 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004053 goto err_poll_thread;
4054 }
4055 priv->do_link_poll = true;
4056 }
4057
Ioana Ciornei71947922019-10-31 01:18:31 +02004058 err = dpaa2_eth_connect_mac(priv);
4059 if (err)
4060 goto err_connect_mac;
4061
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004062 err = register_netdev(net_dev);
4063 if (err < 0) {
4064 dev_err(dev, "register_netdev() failed\n");
4065 goto err_netdev_reg;
4066 }
4067
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004068#ifdef CONFIG_DEBUG_FS
4069 dpaa2_dbg_add(priv);
4070#endif
4071
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004072 dev_info(dev, "Probed interface %s\n", net_dev->name);
4073 return 0;
4074
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004075err_netdev_reg:
Ioana Ciornei71947922019-10-31 01:18:31 +02004076 dpaa2_eth_disconnect_mac(priv);
4077err_connect_mac:
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004078 if (priv->do_link_poll)
4079 kthread_stop(priv->poll_thread);
4080 else
4081 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004082err_poll_thread:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004083 dpaa2_eth_free_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004084err_alloc_rings:
4085err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004086err_netdev_init:
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004087 free_percpu(priv->sgt_cache);
4088err_alloc_sgt_cache:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004089 free_percpu(priv->percpu_extras);
4090err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004091 free_percpu(priv->percpu_stats);
4092err_alloc_percpu_stats:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004093 dpaa2_eth_del_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004094err_bind:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004095 dpaa2_eth_free_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004096err_dpbp_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004097 dpaa2_eth_free_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004098err_dpio_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004099 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004100err_dpni_setup:
4101 fsl_mc_portal_free(priv->mc_io);
4102err_portal_alloc:
4103 dev_set_drvdata(dev, NULL);
4104 free_netdev(net_dev);
4105
4106 return err;
4107}
4108
4109static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4110{
4111 struct device *dev;
4112 struct net_device *net_dev;
4113 struct dpaa2_eth_priv *priv;
4114
4115 dev = &ls_dev->dev;
4116 net_dev = dev_get_drvdata(dev);
4117 priv = netdev_priv(net_dev);
4118
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004119#ifdef CONFIG_DEBUG_FS
4120 dpaa2_dbg_remove(priv);
4121#endif
Ioana Ciornei71947922019-10-31 01:18:31 +02004122 rtnl_lock();
4123 dpaa2_eth_disconnect_mac(priv);
4124 rtnl_unlock();
4125
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004126 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004127
4128 if (priv->do_link_poll)
4129 kthread_stop(priv->poll_thread);
4130 else
4131 fsl_mc_free_irqs(ls_dev);
4132
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004133 dpaa2_eth_free_rings(priv);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004134 free_percpu(priv->sgt_cache);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004135 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004136 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004137
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004138 dpaa2_eth_del_ch_napi(priv);
4139 dpaa2_eth_free_dpbp(priv);
4140 dpaa2_eth_free_dpio(priv);
4141 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004142
4143 fsl_mc_portal_free(priv->mc_io);
4144
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004145 free_netdev(net_dev);
4146
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05004147 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05004148
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004149 return 0;
4150}
4151
4152static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
4153 {
4154 .vendor = FSL_MC_VENDOR_FREESCALE,
4155 .obj_type = "dpni",
4156 },
4157 { .vendor = 0x0 }
4158};
4159MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
4160
4161static struct fsl_mc_driver dpaa2_eth_driver = {
4162 .driver = {
4163 .name = KBUILD_MODNAME,
4164 .owner = THIS_MODULE,
4165 },
4166 .probe = dpaa2_eth_probe,
4167 .remove = dpaa2_eth_remove,
4168 .match_id_table = dpaa2_eth_match_id_table
4169};
4170
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004171static int __init dpaa2_eth_driver_init(void)
4172{
4173 int err;
4174
4175 dpaa2_eth_dbg_init();
4176 err = fsl_mc_driver_register(&dpaa2_eth_driver);
4177 if (err) {
4178 dpaa2_eth_dbg_exit();
4179 return err;
4180 }
4181
4182 return 0;
4183}
4184
4185static void __exit dpaa2_eth_driver_exit(void)
4186{
4187 dpaa2_eth_dbg_exit();
4188 fsl_mc_driver_unregister(&dpaa2_eth_driver);
4189}
4190
4191module_init(dpaa2_eth_driver_init);
4192module_exit(dpaa2_eth_driver_exit);