blob: fb0bcd18ec0c1c6e82ae89a4b63a9872bb9dab4d [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>
Bogdan Purcareata6bd067c2018-02-05 08:07:42 -060014#include <linux/fsl/mc.h>
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +000015#include <linux/bpf.h>
16#include <linux/bpf_trace.h>
Yangbo Lud21c7842020-09-18 17:07:59 +080017#include <linux/fsl/ptp_qoriq.h>
Yangbo Luc5521182020-09-18 17:08:02 +080018#include <linux/ptp_classify.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
Ioana Ciornei061d6312020-10-01 18:11:48 +0300512/* Processing of Rx frames received on the error FQ
513 * We check and print the error bits and then free the frame
514 */
515static void dpaa2_eth_rx_err(struct dpaa2_eth_priv *priv,
516 struct dpaa2_eth_channel *ch,
517 const struct dpaa2_fd *fd,
518 struct dpaa2_eth_fq *fq __always_unused)
519{
520 struct device *dev = priv->net_dev->dev.parent;
521 dma_addr_t addr = dpaa2_fd_get_addr(fd);
522 u8 fd_format = dpaa2_fd_get_format(fd);
523 struct rtnl_link_stats64 *percpu_stats;
524 struct dpaa2_eth_trap_item *trap_item;
525 struct dpaa2_fapr *fapr;
526 struct sk_buff *skb;
527 void *buf_data;
528 void *vaddr;
529
530 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
531 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
532 DMA_BIDIRECTIONAL);
533
534 buf_data = vaddr + dpaa2_fd_get_offset(fd);
535
536 if (fd_format == dpaa2_fd_single) {
537 dma_unmap_page(dev, addr, priv->rx_buf_size,
538 DMA_BIDIRECTIONAL);
539 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
540 } else if (fd_format == dpaa2_fd_sg) {
541 dma_unmap_page(dev, addr, priv->rx_buf_size,
542 DMA_BIDIRECTIONAL);
543 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
544 free_pages((unsigned long)vaddr, 0);
545 } else {
546 /* We don't support any other format */
547 dpaa2_eth_free_rx_fd(priv, fd, vaddr);
548 goto err_frame_format;
549 }
550
551 fapr = dpaa2_get_fapr(vaddr, false);
552 trap_item = dpaa2_eth_dl_get_trap(priv, fapr);
553 if (trap_item)
554 devlink_trap_report(priv->devlink, skb, trap_item->trap_ctx,
555 &priv->devlink_port, NULL);
556 consume_skb(skb);
557
558err_frame_format:
559 percpu_stats = this_cpu_ptr(priv->percpu_stats);
560 percpu_stats->rx_errors++;
561 ch->buf_count--;
562}
563
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500564/* Consume all frames pull-dequeued into the store. This is the simplest way to
565 * make sure we don't accidentally issue another volatile dequeue which would
566 * overwrite (leak) frames already in the store.
567 *
568 * Observance of NAPI budget is not our concern, leaving that to the caller.
569 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300570static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
571 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500572{
573 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000574 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500575 struct dpaa2_dq *dq;
576 const struct dpaa2_fd *fd;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300577 int cleaned = 0, retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500578 int is_last;
579
580 do {
581 dq = dpaa2_io_store_next(ch->store, &is_last);
582 if (unlikely(!dq)) {
583 /* If we're here, we *must* have placed a
584 * volatile dequeue comnmand, so keep reading through
585 * the store until we get some sort of valid response
586 * token (either a valid frame or an "empty dequeue")
587 */
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300588 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
589 netdev_err_once(priv->net_dev,
590 "Unable to read a valid dequeue response\n");
591 return -ETIMEDOUT;
592 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500593 continue;
594 }
595
596 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600597 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500598
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000599 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500600 cleaned++;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300601 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500602 } while (!is_last);
603
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000604 if (!cleaned)
605 return 0;
606
607 fq->stats.frames += cleaned;
Ioana Ciornei460fd832020-04-24 12:33:18 +0300608 ch->stats.frames += cleaned;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000609
610 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000611 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000612 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000613 if (src)
614 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000615
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500616 return cleaned;
617}
618
Yangbo Luc5521182020-09-18 17:08:02 +0800619static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
620 u8 *msgtype, u8 *twostep, u8 *udp,
621 u16 *correction_offset,
622 u16 *origintimestamp_offset)
Ioana Radulescu859f9982018-04-26 18:23:47 +0800623{
Yangbo Luc5521182020-09-18 17:08:02 +0800624 unsigned int ptp_class;
625 struct ptp_header *hdr;
626 unsigned int type;
627 u8 *base;
628
629 ptp_class = ptp_classify_raw(skb);
630 if (ptp_class == PTP_CLASS_NONE)
631 return -EINVAL;
632
633 hdr = ptp_parse_header(skb, ptp_class);
634 if (!hdr)
635 return -EINVAL;
636
637 *msgtype = ptp_get_msgtype(hdr, ptp_class);
638 *twostep = hdr->flag_field[0] & 0x2;
639
640 type = ptp_class & PTP_CLASS_PMASK;
641 if (type == PTP_CLASS_IPV4 ||
642 type == PTP_CLASS_IPV6)
643 *udp = 1;
644 else
645 *udp = 0;
646
647 base = skb_mac_header(skb);
648 *correction_offset = (u8 *)&hdr->correction - base;
649 *origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
650
651 return 0;
652}
653
654/* Configure the egress frame annotation for timestamp update */
655static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
656 struct dpaa2_fd *fd,
657 void *buf_start,
658 struct sk_buff *skb)
659{
660 struct ptp_tstamp origin_timestamp;
661 struct dpni_single_step_cfg cfg;
662 u8 msgtype, twostep, udp;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800663 struct dpaa2_faead *faead;
Yangbo Luc5521182020-09-18 17:08:02 +0800664 struct dpaa2_fas *fas;
665 struct timespec64 ts;
666 u16 offset1, offset2;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800667 u32 ctrl, frc;
Yangbo Luc5521182020-09-18 17:08:02 +0800668 __le64 *ns;
669 u8 *data;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800670
671 /* Mark the egress frame annotation area as valid */
672 frc = dpaa2_fd_get_frc(fd);
673 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
674
675 /* Set hardware annotation size */
676 ctrl = dpaa2_fd_get_ctrl(fd);
677 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
678
679 /* enable UPD (update prepanded data) bit in FAEAD field of
680 * hardware frame annotation area
681 */
682 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
683 faead = dpaa2_get_faead(buf_start, true);
684 faead->ctrl = cpu_to_le32(ctrl);
Yangbo Luc5521182020-09-18 17:08:02 +0800685
686 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
687 if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
688 &offset1, &offset2) ||
Christian Eggers6b6817c2020-11-20 09:41:05 +0100689 msgtype != PTP_MSGTYPE_SYNC || twostep) {
Yangbo Luc5521182020-09-18 17:08:02 +0800690 WARN_ONCE(1, "Bad packet for one-step timestamping\n");
691 return;
692 }
693
694 /* Mark the frame annotation status as valid */
695 frc = dpaa2_fd_get_frc(fd);
696 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);
697
698 /* Mark the PTP flag for one step timestamping */
699 fas = dpaa2_get_fas(buf_start, true);
700 fas->status = cpu_to_le32(DPAA2_FAS_PTP);
701
702 dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
703 ns = dpaa2_get_ts(buf_start, true);
704 *ns = cpu_to_le64(timespec64_to_ns(&ts) /
705 DPAA2_PTP_CLK_PERIOD_NS);
706
707 /* Update current time to PTP message originTimestamp field */
708 ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
709 data = skb_mac_header(skb);
710 *(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
711 *(__be32 *)(data + offset2 + 2) =
712 htonl(origin_timestamp.sec_lsb);
713 *(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);
714
715 cfg.en = 1;
716 cfg.ch_update = udp;
717 cfg.offset = offset1;
718 cfg.peer_delay = 0;
719
720 if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token,
721 &cfg))
722 WARN_ONCE(1, "Failed to set single step register");
723 }
Ioana Radulescu859f9982018-04-26 18:23:47 +0800724}
725
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500726/* Create a frame descriptor based on a fragmented skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300727static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
728 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800729 struct dpaa2_fd *fd,
730 void **swa_addr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500731{
732 struct device *dev = priv->net_dev->dev.parent;
733 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500734 dma_addr_t addr;
735 int nr_frags = skb_shinfo(skb)->nr_frags;
736 struct dpaa2_sg_entry *sgt;
737 int i, err;
738 int sgt_buf_size;
739 struct scatterlist *scl, *crt_scl;
740 int num_sg;
741 int num_dma_bufs;
742 struct dpaa2_eth_swa *swa;
743
744 /* Create and map scatterlist.
745 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
746 * to go beyond nr_frags+1.
747 * Note: We don't support chained scatterlists
748 */
749 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
750 return -EINVAL;
751
Julia Lawalld4ceb8d2020-09-20 13:26:15 +0200752 scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500753 if (unlikely(!scl))
754 return -ENOMEM;
755
756 sg_init_table(scl, nr_frags + 1);
757 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Ciornei37fbbdda2020-06-24 14:34:18 +0300758 if (unlikely(num_sg < 0)) {
759 err = -ENOMEM;
760 goto dma_map_sg_failed;
761 }
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500762 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500763 if (unlikely(!num_dma_bufs)) {
764 err = -ENOMEM;
765 goto dma_map_sg_failed;
766 }
767
768 /* Prepare the HW SGT structure */
769 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500770 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Sebastian Andrzej Siewior90bc6d42019-06-07 21:20:37 +0200771 sgt_buf = napi_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500772 if (unlikely(!sgt_buf)) {
773 err = -ENOMEM;
774 goto sgt_buf_alloc_failed;
775 }
776 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500777 memset(sgt_buf, 0, sgt_buf_size);
778
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500779 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
780
781 /* Fill in the HW SGT structure.
782 *
783 * sgt_buf is zeroed out, so the following fields are implicit
784 * in all sgt entries:
785 * - offset is 0
786 * - format is 'dpaa2_sg_single'
787 */
788 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
789 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
790 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
791 }
792 dpaa2_sg_set_final(&sgt[i - 1], true);
793
794 /* Store the skb backpointer in the SGT buffer.
795 * Fit the scatterlist and the number of buffers alongside the
796 * skb backpointer in the software annotation area. We'll need
797 * all of them on Tx Conf.
798 */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800799 *swa_addr = (void *)sgt_buf;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500800 swa = (struct dpaa2_eth_swa *)sgt_buf;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000801 swa->type = DPAA2_ETH_SWA_SG;
802 swa->sg.skb = skb;
803 swa->sg.scl = scl;
804 swa->sg.num_sg = num_sg;
805 swa->sg.sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500806
807 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500808 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500809 if (unlikely(dma_mapping_error(dev, addr))) {
810 err = -ENOMEM;
811 goto dma_map_single_failed;
812 }
813 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
814 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
815 dpaa2_fd_set_addr(fd, addr);
816 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000817 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500818
819 return 0;
820
821dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500822 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500823sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500824 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500825dma_map_sg_failed:
826 kfree(scl);
827 return err;
828}
829
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300830/* Create a SG frame descriptor based on a linear skb.
831 *
832 * This function is used on the Tx path when the skb headroom is not large
833 * enough for the HW requirements, thus instead of realloc-ing the skb we
834 * create a SG frame descriptor with only one entry.
835 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300836static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
837 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800838 struct dpaa2_fd *fd,
839 void **swa_addr)
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300840{
841 struct device *dev = priv->net_dev->dev.parent;
842 struct dpaa2_eth_sgt_cache *sgt_cache;
843 struct dpaa2_sg_entry *sgt;
844 struct dpaa2_eth_swa *swa;
845 dma_addr_t addr, sgt_addr;
846 void *sgt_buf = NULL;
847 int sgt_buf_size;
848 int err;
849
850 /* Prepare the HW SGT structure */
851 sgt_cache = this_cpu_ptr(priv->sgt_cache);
852 sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
853
854 if (sgt_cache->count == 0)
855 sgt_buf = kzalloc(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN,
856 GFP_ATOMIC);
857 else
858 sgt_buf = sgt_cache->buf[--sgt_cache->count];
859 if (unlikely(!sgt_buf))
860 return -ENOMEM;
861
862 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
863 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
864
865 addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
866 if (unlikely(dma_mapping_error(dev, addr))) {
867 err = -ENOMEM;
868 goto data_map_failed;
869 }
870
871 /* Fill in the HW SGT structure */
872 dpaa2_sg_set_addr(sgt, addr);
873 dpaa2_sg_set_len(sgt, skb->len);
874 dpaa2_sg_set_final(sgt, true);
875
876 /* Store the skb backpointer in the SGT buffer */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800877 *swa_addr = (void *)sgt_buf;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300878 swa = (struct dpaa2_eth_swa *)sgt_buf;
879 swa->type = DPAA2_ETH_SWA_SINGLE;
880 swa->single.skb = skb;
Ioana Ciornei54a57d12020-12-11 19:16:07 +0200881 swa->single.sgt_size = sgt_buf_size;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300882
883 /* Separately map the SGT buffer */
884 sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
885 if (unlikely(dma_mapping_error(dev, sgt_addr))) {
886 err = -ENOMEM;
887 goto sgt_map_failed;
888 }
889
890 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
891 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
892 dpaa2_fd_set_addr(fd, sgt_addr);
893 dpaa2_fd_set_len(fd, skb->len);
894 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
895
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300896 return 0;
897
898sgt_map_failed:
899 dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
900data_map_failed:
901 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
902 kfree(sgt_buf);
903 else
904 sgt_cache->buf[sgt_cache->count++] = sgt_buf;
905
906 return err;
907}
908
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500909/* Create a frame descriptor based on a linear skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300910static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
911 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800912 struct dpaa2_fd *fd,
913 void **swa_addr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500914{
915 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600916 u8 *buffer_start, *aligned_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000917 struct dpaa2_eth_swa *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500918 dma_addr_t addr;
919
Yangbo Lu1cf773b2020-09-18 17:08:01 +0800920 buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
Ioana Radulescuc1636852017-12-08 06:47:58 -0600921
922 /* If there's enough room to align the FD address, do it.
923 * It will help hardware optimize accesses.
924 */
925 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
926 DPAA2_ETH_TX_BUF_ALIGN);
927 if (aligned_start >= skb->head)
928 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500929
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500930 /* Store a backpointer to the skb at the beginning of the buffer
931 * (in the private data area) such that we can release it
932 * on Tx confirm
933 */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800934 *swa_addr = (void *)buffer_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000935 swa = (struct dpaa2_eth_swa *)buffer_start;
936 swa->type = DPAA2_ETH_SWA_SINGLE;
937 swa->single.skb = skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500938
939 addr = dma_map_single(dev, buffer_start,
940 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500941 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500942 if (unlikely(dma_mapping_error(dev, addr)))
943 return -ENOMEM;
944
945 dpaa2_fd_set_addr(fd, addr);
946 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
947 dpaa2_fd_set_len(fd, skb->len);
948 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000949 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500950
951 return 0;
952}
953
954/* FD freeing routine on the Tx path
955 *
956 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
957 * back-pointed to is also freed.
958 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
959 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500960 */
Yangbo Luc5521182020-09-18 17:08:02 +0800961static void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300962 struct dpaa2_eth_fq *fq,
963 const struct dpaa2_fd *fd, bool in_napi)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500964{
965 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300966 dma_addr_t fd_addr, sg_addr;
Ioana Radulescud678be12019-03-01 17:47:24 +0000967 struct sk_buff *skb = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500968 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500969 struct dpaa2_eth_swa *swa;
970 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescud678be12019-03-01 17:47:24 +0000971 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500972
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300973 struct dpaa2_eth_sgt_cache *sgt_cache;
974 struct dpaa2_sg_entry *sgt;
975
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500976 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000977 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
978 swa = (struct dpaa2_eth_swa *)buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500979
980 if (fd_format == dpaa2_fd_single) {
Ioana Radulescud678be12019-03-01 17:47:24 +0000981 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
982 skb = swa->single.skb;
983 /* Accessing the skb buffer is safe before dma unmap,
984 * because we didn't map the actual skb shell.
985 */
986 dma_unmap_single(dev, fd_addr,
987 skb_tail_pointer(skb) - buffer_start,
988 DMA_BIDIRECTIONAL);
989 } else {
990 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
991 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
992 DMA_BIDIRECTIONAL);
993 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500994 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300995 if (swa->type == DPAA2_ETH_SWA_SG) {
996 skb = swa->sg.skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500997
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300998 /* Unmap the scatterlist */
999 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
1000 DMA_BIDIRECTIONAL);
1001 kfree(swa->sg.scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001002
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001003 /* Unmap the SGT buffer */
1004 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
1005 DMA_BIDIRECTIONAL);
1006 } else {
1007 skb = swa->single.skb;
1008
1009 /* Unmap the SGT Buffer */
1010 dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
1011 DMA_BIDIRECTIONAL);
1012
1013 sgt = (struct dpaa2_sg_entry *)(buffer_start +
1014 priv->tx_data_offset);
1015 sg_addr = dpaa2_sg_get_addr(sgt);
1016 dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
1017 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001018 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06001019 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001020 return;
1021 }
1022
Ioana Radulescud678be12019-03-01 17:47:24 +00001023 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
1024 fq->dq_frames++;
1025 fq->dq_bytes += fd_len;
1026 }
1027
1028 if (swa->type == DPAA2_ETH_SWA_XDP) {
1029 xdp_return_frame(swa->xdp.xdpf);
1030 return;
1031 }
1032
Ioana Radulescu859f9982018-04-26 18:23:47 +08001033 /* Get the timestamp value */
Yangbo Lu1cf773b2020-09-18 17:08:01 +08001034 if (skb->cb[0] == TX_TSTAMP) {
Ioana Radulescu859f9982018-04-26 18:23:47 +08001035 struct skb_shared_hwtstamps shhwtstamps;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +00001036 __le64 *ts = dpaa2_get_ts(buffer_start, true);
Ioana Radulescu859f9982018-04-26 18:23:47 +08001037 u64 ns;
1038
1039 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1040
1041 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
1042 shhwtstamps.hwtstamp = ns_to_ktime(ns);
1043 skb_tstamp_tx(skb, &shhwtstamps);
Yangbo Luc5521182020-09-18 17:08:02 +08001044 } else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1045 mutex_unlock(&priv->onestep_tstamp_lock);
Ioana Radulescu859f9982018-04-26 18:23:47 +08001046 }
1047
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -05001048 /* Free SGT buffer allocated on tx */
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001049 if (fd_format != dpaa2_fd_single) {
1050 sgt_cache = this_cpu_ptr(priv->sgt_cache);
1051 if (swa->type == DPAA2_ETH_SWA_SG) {
1052 skb_free_frag(buffer_start);
1053 } else {
1054 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
1055 kfree(buffer_start);
1056 else
1057 sgt_cache->buf[sgt_cache->count++] = buffer_start;
1058 }
1059 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001060
1061 /* Move on with skb release */
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +00001062 napi_consume_skb(skb, in_napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001063}
1064
Yangbo Luc5521182020-09-18 17:08:02 +08001065static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1066 struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001067{
1068 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1069 struct dpaa2_fd fd;
1070 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001071 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001072 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001073 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001074 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -06001075 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001076 u32 fd_len;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001077 u8 prio = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001078 int err, i;
Yangbo Lu64a965d2020-09-18 17:08:00 +08001079 void *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001080
1081 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001082 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001083
Yangbo Lu1cf773b2020-09-18 17:08:01 +08001084 needed_headroom = dpaa2_eth_needed_headroom(skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001085
1086 /* We'll be holding a back-reference to the skb until Tx Confirmation;
1087 * we don't want that overwritten by a concurrent Tx with a cloned skb.
1088 */
1089 skb = skb_unshare(skb, GFP_ATOMIC);
1090 if (unlikely(!skb)) {
1091 /* skb_unshare() has already freed the skb */
1092 percpu_stats->tx_dropped++;
1093 return NETDEV_TX_OK;
1094 }
1095
1096 /* Setup the FD fields */
1097 memset(&fd, 0, sizeof(fd));
1098
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001099 if (skb_is_nonlinear(skb)) {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001100 err = dpaa2_eth_build_sg_fd(priv, skb, &fd, &swa);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001101 percpu_extras->tx_sg_frames++;
1102 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001103 } else if (skb_headroom(skb) < needed_headroom) {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001104 err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, &fd, &swa);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001105 percpu_extras->tx_sg_frames++;
1106 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciornei4c96c0a2020-06-29 21:47:12 +03001107 percpu_extras->tx_converted_sg_frames++;
1108 percpu_extras->tx_converted_sg_bytes += skb->len;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001109 } else {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001110 err = dpaa2_eth_build_single_fd(priv, skb, &fd, &swa);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001111 }
1112
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001113 if (unlikely(err)) {
1114 percpu_stats->tx_dropped++;
1115 goto err_build_fd;
1116 }
1117
Yangbo Luc5521182020-09-18 17:08:02 +08001118 if (skb->cb[0])
1119 dpaa2_eth_enable_tx_tstamp(priv, &fd, swa, skb);
Yangbo Lu64a965d2020-09-18 17:08:00 +08001120
Ioana Radulescu56361872017-04-28 04:50:32 -05001121 /* Tracing point */
1122 trace_dpaa2_tx_fd(net_dev, &fd);
1123
Ioana Radulescu537336c2017-12-21 06:33:20 -06001124 /* TxConf FQ selection relies on queue id from the stack.
1125 * In case of a forwarded frame from another DPNI interface, we choose
1126 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001127 */
Ioana Radulescu537336c2017-12-21 06:33:20 -06001128 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001129
1130 if (net_dev->num_tc) {
1131 prio = netdev_txq_to_tc(net_dev, queue_mapping);
1132 /* Hardware interprets priority level 0 as being the highest,
1133 * so we need to do a reverse mapping to the netdev tc index
1134 */
1135 prio = net_dev->num_tc - prio - 1;
1136 /* We have only one FQ array entry for all Tx hardware queues
1137 * with the same flow id (but different priority levels)
1138 */
1139 queue_mapping %= dpaa2_eth_queue_count(priv);
1140 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001141 fq = &priv->fq[queue_mapping];
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001142
1143 fd_len = dpaa2_fd_get_len(&fd);
1144 nq = netdev_get_tx_queue(net_dev, queue_mapping);
1145 netdev_tx_sent_queue(nq, fd_len);
1146
1147 /* Everything that happens after this enqueues might race with
1148 * the Tx confirmation callback for this frame
1149 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001150 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Ciornei6ff80442020-04-22 15:05:11 +03001151 err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001152 if (err != -EBUSY)
1153 break;
1154 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001155 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001156 if (unlikely(err < 0)) {
1157 percpu_stats->tx_errors++;
1158 /* Clean up everything, including freeing the skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001159 dpaa2_eth_free_tx_fd(priv, fq, &fd, false);
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001160 netdev_tx_completed_queue(nq, 1, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001161 } else {
1162 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001163 percpu_stats->tx_bytes += fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001164 }
1165
1166 return NETDEV_TX_OK;
1167
1168err_build_fd:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001169 dev_kfree_skb(skb);
1170
1171 return NETDEV_TX_OK;
1172}
1173
Yangbo Luc5521182020-09-18 17:08:02 +08001174static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1175{
1176 struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1177 tx_onestep_tstamp);
1178 struct sk_buff *skb;
1179
1180 while (true) {
1181 skb = skb_dequeue(&priv->tx_skbs);
1182 if (!skb)
1183 return;
1184
1185 /* Lock just before TX one-step timestamping packet,
1186 * and release the lock in dpaa2_eth_free_tx_fd when
1187 * confirm the packet has been sent on hardware, or
1188 * when clean up during transmit failure.
1189 */
1190 mutex_lock(&priv->onestep_tstamp_lock);
1191 __dpaa2_eth_tx(skb, priv->net_dev);
1192 }
1193}
1194
1195static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1196{
1197 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1198 u8 msgtype, twostep, udp;
1199 u16 offset1, offset2;
1200
1201 /* Utilize skb->cb[0] for timestamping request per skb */
1202 skb->cb[0] = 0;
1203
1204 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1205 if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1206 skb->cb[0] = TX_TSTAMP;
1207 else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1208 skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1209 }
1210
1211 /* TX for one-step timestamping PTP Sync packet */
1212 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1213 if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1214 &offset1, &offset2))
Christian Eggers6b6817c2020-11-20 09:41:05 +01001215 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) {
Yangbo Luc5521182020-09-18 17:08:02 +08001216 skb_queue_tail(&priv->tx_skbs, skb);
1217 queue_work(priv->dpaa2_ptp_wq,
1218 &priv->tx_onestep_tstamp);
1219 return NETDEV_TX_OK;
1220 }
1221 /* Use two-step timestamping if not one-step timestamping
1222 * PTP Sync packet
1223 */
1224 skb->cb[0] = TX_TSTAMP;
1225 }
1226
1227 /* TX for other packets */
1228 return __dpaa2_eth_tx(skb, net_dev);
1229}
1230
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001231/* Tx confirmation frame processing routine */
1232static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +00001233 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001234 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001235 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001236{
1237 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001238 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001239 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001240 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001241
Ioana Radulescu56361872017-04-28 04:50:32 -05001242 /* Tracing point */
1243 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1244
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001245 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1246 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001247 percpu_extras->tx_conf_bytes += fd_len;
1248
Ioana Radulescu39163c02017-06-06 10:00:39 -05001249 /* Check frame errors in the FD field */
1250 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001251 dpaa2_eth_free_tx_fd(priv, fq, fd, true);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001252
1253 if (likely(!fd_errors))
1254 return;
1255
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06001256 if (net_ratelimit())
1257 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1258 fd_errors);
1259
Ioana Radulescu39163c02017-06-06 10:00:39 -05001260 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1261 /* Tx-conf logically pertains to the egress path. */
1262 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001263}
1264
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001265static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001266{
1267 int err;
1268
1269 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1270 DPNI_OFF_RX_L3_CSUM, enable);
1271 if (err) {
1272 netdev_err(priv->net_dev,
1273 "dpni_set_offload(RX_L3_CSUM) failed\n");
1274 return err;
1275 }
1276
1277 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1278 DPNI_OFF_RX_L4_CSUM, enable);
1279 if (err) {
1280 netdev_err(priv->net_dev,
1281 "dpni_set_offload(RX_L4_CSUM) failed\n");
1282 return err;
1283 }
1284
1285 return 0;
1286}
1287
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001288static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001289{
1290 int err;
1291
1292 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1293 DPNI_OFF_TX_L3_CSUM, enable);
1294 if (err) {
1295 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1296 return err;
1297 }
1298
1299 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1300 DPNI_OFF_TX_L4_CSUM, enable);
1301 if (err) {
1302 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1303 return err;
1304 }
1305
1306 return 0;
1307}
1308
1309/* Perform a single release command to add buffers
1310 * to the specified buffer pool
1311 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001312static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1313 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001314{
1315 struct device *dev = priv->net_dev->dev.parent;
1316 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001317 struct page *page;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001318 dma_addr_t addr;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001319 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001320 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001321
1322 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1323 /* Allocate buffer visible to WRIOP + skb shared info +
1324 * alignment padding
1325 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001326 /* allocate one page for each Rx buffer. WRIOP sees
1327 * the entire page except for a tailroom reserved for
1328 * skb shared info
1329 */
1330 page = dev_alloc_pages(0);
1331 if (!page)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001332 goto err_alloc;
1333
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001334 addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001335 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001336 if (unlikely(dma_mapping_error(dev, addr)))
1337 goto err_map;
1338
1339 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -05001340
1341 /* tracing point */
1342 trace_dpaa2_eth_buf_seed(priv->net_dev,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001343 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001344 addr, priv->rx_buf_size,
Ioana Radulescu56361872017-04-28 04:50:32 -05001345 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001346 }
1347
1348release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001349 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001350 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001351 buf_array, i)) == -EBUSY) {
1352 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1353 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001354 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001355 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001356
1357 /* If release command failed, clean up and bail out;
1358 * not much else we can do about it
1359 */
1360 if (err) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001361 dpaa2_eth_free_bufs(priv, buf_array, i);
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001362 return 0;
1363 }
1364
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001365 return i;
1366
1367err_map:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001368 __free_pages(page, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001369err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001370 /* If we managed to allocate at least some buffers,
1371 * release them to hardware
1372 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001373 if (i)
1374 goto release_bufs;
1375
1376 return 0;
1377}
1378
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001379static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001380{
1381 int i, j;
1382 int new_count;
1383
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001384 for (j = 0; j < priv->num_channels; j++) {
1385 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1386 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001387 new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001388 priv->channel[j]->buf_count += new_count;
1389
1390 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001391 return -ENOMEM;
1392 }
1393 }
1394 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001395
1396 return 0;
1397}
1398
Jesse Brandeburgd0ea5cb2020-09-25 15:24:45 -07001399/*
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001400 * Drain the specified number of buffers from the DPNI's private buffer pool.
1401 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1402 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001403static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001404{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001405 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001406 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001407 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001408
1409 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001410 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001411 buf_array, count);
1412 if (ret < 0) {
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001413 if (ret == -EBUSY &&
Ioana Ciornei0e5ad752020-06-24 14:34:19 +03001414 retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001415 continue;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001416 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1417 return;
1418 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001419 dpaa2_eth_free_bufs(priv, buf_array, ret);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001420 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001421 } while (ret);
1422}
1423
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001424static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001425{
1426 int i;
1427
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001428 dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1429 dpaa2_eth_drain_bufs(priv, 1);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001430
1431 for (i = 0; i < priv->num_channels; i++)
1432 priv->channel[i]->buf_count = 0;
1433}
1434
1435/* Function is called from softirq context only, so we don't need to guard
1436 * the access to percpu count
1437 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001438static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1439 struct dpaa2_eth_channel *ch,
1440 u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001441{
1442 int new_count;
1443
1444 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1445 return 0;
1446
1447 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001448 new_count = dpaa2_eth_add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001449 if (unlikely(!new_count)) {
1450 /* Out of memory; abort for now, we'll try later on */
1451 break;
1452 }
1453 ch->buf_count += new_count;
1454 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1455
1456 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1457 return -ENOMEM;
1458
1459 return 0;
1460}
1461
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001462static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1463{
1464 struct dpaa2_eth_sgt_cache *sgt_cache;
1465 u16 count;
1466 int k, i;
1467
Ioana Ciornei0fe665d2020-07-06 17:55:54 +03001468 for_each_possible_cpu(k) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001469 sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1470 count = sgt_cache->count;
1471
1472 for (i = 0; i < count; i++)
1473 kfree(sgt_cache->buf[i]);
1474 sgt_cache->count = 0;
1475 }
1476}
1477
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001478static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001479{
1480 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001481 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001482
1483 /* Retry while portal is busy */
1484 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001485 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1486 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001487 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001488 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001489 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001490
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001491 ch->stats.dequeue_portal_busy += dequeues;
1492 if (unlikely(err))
1493 ch->stats.pull_err++;
1494
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001495 return err;
1496}
1497
1498/* NAPI poll routine
1499 *
1500 * Frames are dequeued from the QMan channel associated with this NAPI context.
1501 * Rx, Tx confirmation and (if configured) Rx error frames all count
1502 * towards the NAPI budget.
1503 */
1504static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1505{
1506 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001507 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001508 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001509 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1510 struct netdev_queue *nq;
1511 int store_cleaned, work_done;
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001512 struct list_head rx_list;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001513 int retries = 0;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001514 u16 flowid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001515 int err;
1516
1517 ch = container_of(napi, struct dpaa2_eth_channel, napi);
Ioana Radulescud678be12019-03-01 17:47:24 +00001518 ch->xdp.res = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001519 priv = ch->priv;
1520
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001521 INIT_LIST_HEAD(&rx_list);
1522 ch->rx_list = &rx_list;
1523
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001524 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001525 err = dpaa2_eth_pull_channel(ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001526 if (unlikely(err))
1527 break;
1528
1529 /* Refill pool if appropriate */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001530 dpaa2_eth_refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001531
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001532 store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001533 if (store_cleaned <= 0)
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001534 break;
1535 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001536 rx_cleaned += store_cleaned;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001537 flowid = fq->flowid;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001538 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001539 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001540 /* We have a single Tx conf FQ on this channel */
1541 txc_fq = fq;
1542 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001543
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001544 /* If we either consumed the whole NAPI budget with Rx frames
1545 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001546 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001547 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001548 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1549 work_done = budget;
1550 goto out;
1551 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001552 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001553
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001554 /* We didn't consume the entire budget, so finish napi and
1555 * re-enable data availability notifications
1556 */
1557 napi_complete_done(napi, rx_cleaned);
1558 do {
1559 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1560 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001561 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001562 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1563 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001564
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001565 work_done = max(rx_cleaned, 1);
1566
1567out:
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001568 netif_receive_skb_list(ch->rx_list);
1569
Ioana Radulescud678be12019-03-01 17:47:24 +00001570 if (txc_fq && txc_fq->dq_frames) {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001571 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1572 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1573 txc_fq->dq_bytes);
1574 txc_fq->dq_frames = 0;
1575 txc_fq->dq_bytes = 0;
1576 }
1577
Ioana Radulescud678be12019-03-01 17:47:24 +00001578 if (ch->xdp.res & XDP_REDIRECT)
1579 xdp_do_flush_map();
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001580 else if (rx_cleaned && ch->xdp.res & XDP_TX)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001581 dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
Ioana Radulescud678be12019-03-01 17:47:24 +00001582
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001583 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001584}
1585
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001586static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001587{
1588 struct dpaa2_eth_channel *ch;
1589 int i;
1590
1591 for (i = 0; i < priv->num_channels; i++) {
1592 ch = priv->channel[i];
1593 napi_enable(&ch->napi);
1594 }
1595}
1596
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001597static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001598{
1599 struct dpaa2_eth_channel *ch;
1600 int i;
1601
1602 for (i = 0; i < priv->num_channels; i++) {
1603 ch = priv->channel[i];
1604 napi_disable(&ch->napi);
1605 }
1606}
1607
Ioana Ciornei07beb162020-05-31 00:08:14 +03001608void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1609 bool tx_pause, bool pfc)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001610{
1611 struct dpni_taildrop td = {0};
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001612 struct dpaa2_eth_fq *fq;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001613 int i, err;
1614
Ioana Ciornei07beb162020-05-31 00:08:14 +03001615 /* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1616 * flow control is disabled (as it might interfere with either the
1617 * buffer pool depletion trigger for pause frames or with the group
1618 * congestion trigger for PFC frames)
1619 */
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001620 td.enable = !tx_pause;
Ioana Ciornei07beb162020-05-31 00:08:14 +03001621 if (priv->rx_fqtd_enabled == td.enable)
1622 goto set_cgtd;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001623
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001624 td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1625 td.units = DPNI_CONGESTION_UNIT_BYTES;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001626
1627 for (i = 0; i < priv->num_fqs; i++) {
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001628 fq = &priv->fq[i];
1629 if (fq->type != DPAA2_RX_FQ)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001630 continue;
1631 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001632 DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1633 fq->tc, fq->flowid, &td);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001634 if (err) {
1635 netdev_err(priv->net_dev,
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001636 "dpni_set_taildrop(FQ) failed\n");
1637 return;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001638 }
1639 }
1640
Ioana Ciornei07beb162020-05-31 00:08:14 +03001641 priv->rx_fqtd_enabled = td.enable;
1642
1643set_cgtd:
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001644 /* Congestion group taildrop: threshold is in frames, per group
1645 * of FQs belonging to the same traffic class
Ioana Ciornei07beb162020-05-31 00:08:14 +03001646 * Enabled if general Tx pause disabled or if PFCs are enabled
1647 * (congestion group threhsold for PFC generation is lower than the
1648 * CG taildrop threshold, so it won't interfere with it; we also
1649 * want frames in non-PFC enabled traffic classes to be kept in check)
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001650 */
Ioana Ciornei07beb162020-05-31 00:08:14 +03001651 td.enable = !tx_pause || (tx_pause && pfc);
1652 if (priv->rx_cgtd_enabled == td.enable)
1653 return;
1654
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001655 td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
1656 td.units = DPNI_CONGESTION_UNIT_FRAMES;
1657 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
1658 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1659 DPNI_CP_GROUP, DPNI_QUEUE_RX,
1660 i, 0, &td);
1661 if (err) {
1662 netdev_err(priv->net_dev,
1663 "dpni_set_taildrop(CG) failed\n");
1664 return;
1665 }
1666 }
1667
Ioana Ciornei07beb162020-05-31 00:08:14 +03001668 priv->rx_cgtd_enabled = td.enable;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001669}
1670
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001671static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001672{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001673 struct dpni_link_state state = {0};
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001674 bool tx_pause;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001675 int err;
1676
1677 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1678 if (unlikely(err)) {
1679 netdev_err(priv->net_dev,
1680 "dpni_get_link_state() failed\n");
1681 return err;
1682 }
1683
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001684 /* If Tx pause frame settings have changed, we need to update
1685 * Rx FQ taildrop configuration as well. We configure taildrop
1686 * only when pause frame generation is disabled.
1687 */
Ioana Radulescuad054f22020-05-31 00:08:10 +03001688 tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
Ioana Ciornei07beb162020-05-31 00:08:14 +03001689 dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001690
Ioana Ciornei71947922019-10-31 01:18:31 +02001691 /* When we manage the MAC/PHY using phylink there is no need
1692 * to manually update the netif_carrier.
1693 */
1694 if (priv->mac)
1695 goto out;
1696
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001697 /* Chech link state; speed / duplex changes are not treated yet */
1698 if (priv->link_state.up == state.up)
Ioana Radulescucce629432019-08-28 17:08:14 +03001699 goto out;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001700
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001701 if (state.up) {
1702 netif_carrier_on(priv->net_dev);
1703 netif_tx_start_all_queues(priv->net_dev);
1704 } else {
1705 netif_tx_stop_all_queues(priv->net_dev);
1706 netif_carrier_off(priv->net_dev);
1707 }
1708
Ioana Radulescu77160af2017-06-06 10:00:28 -05001709 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001710 state.up ? "up" : "down");
1711
Ioana Radulescucce629432019-08-28 17:08:14 +03001712out:
1713 priv->link_state = state;
1714
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001715 return 0;
1716}
1717
1718static int dpaa2_eth_open(struct net_device *net_dev)
1719{
1720 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1721 int err;
1722
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001723 err = dpaa2_eth_seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001724 if (err) {
1725 /* Not much to do; the buffer pool, though not filled up,
1726 * may still contain some buffers which would enable us
1727 * to limp on.
1728 */
1729 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001730 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001731 }
1732
Ioana Ciornei71947922019-10-31 01:18:31 +02001733 if (!priv->mac) {
1734 /* We'll only start the txqs when the link is actually ready;
1735 * make sure we don't race against the link up notification,
1736 * which may come immediately after dpni_enable();
1737 */
1738 netif_tx_stop_all_queues(net_dev);
1739
1740 /* Also, explicitly set carrier off, otherwise
1741 * netif_carrier_ok() will return true and cause 'ip link show'
1742 * to report the LOWER_UP flag, even though the link
1743 * notification wasn't even received.
1744 */
1745 netif_carrier_off(net_dev);
1746 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001747 dpaa2_eth_enable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001748
1749 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1750 if (err < 0) {
1751 netdev_err(net_dev, "dpni_enable() failed\n");
1752 goto enable_err;
1753 }
1754
Ioana Ciornei4c33a5b2020-09-25 17:44:20 +03001755 if (priv->mac)
Ioana Ciornei71947922019-10-31 01:18:31 +02001756 phylink_start(priv->mac->phylink);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001757
1758 return 0;
1759
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001760enable_err:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001761 dpaa2_eth_disable_ch_napi(priv);
1762 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001763 return err;
1764}
1765
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001766/* Total number of in-flight frames on ingress queues */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001767static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001768{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001769 struct dpaa2_eth_fq *fq;
1770 u32 fcnt = 0, bcnt = 0, total = 0;
1771 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001772
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001773 for (i = 0; i < priv->num_fqs; i++) {
1774 fq = &priv->fq[i];
1775 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1776 if (err) {
1777 netdev_warn(priv->net_dev, "query_fq_count failed");
1778 break;
1779 }
1780 total += fcnt;
1781 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001782
1783 return total;
1784}
1785
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001786static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001787{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001788 int retries = 10;
1789 u32 pending;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001790
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001791 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001792 pending = dpaa2_eth_ingress_fq_count(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001793 if (pending)
1794 msleep(100);
1795 } while (pending && --retries);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001796}
1797
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001798#define DPNI_TX_PENDING_VER_MAJOR 7
1799#define DPNI_TX_PENDING_VER_MINOR 13
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001800static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001801{
1802 union dpni_statistics stats;
1803 int retries = 10;
1804 int err;
1805
1806 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1807 DPNI_TX_PENDING_VER_MINOR) < 0)
1808 goto out;
1809
1810 do {
1811 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1812 &stats);
1813 if (err)
1814 goto out;
1815 if (stats.page_6.tx_pending_frames == 0)
1816 return;
1817 } while (--retries);
1818
1819out:
1820 msleep(500);
1821}
1822
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001823static int dpaa2_eth_stop(struct net_device *net_dev)
1824{
1825 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001826 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001827 int retries = 10;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001828
Ioana Ciornei71947922019-10-31 01:18:31 +02001829 if (!priv->mac) {
1830 netif_tx_stop_all_queues(net_dev);
1831 netif_carrier_off(net_dev);
1832 } else {
1833 phylink_stop(priv->mac->phylink);
1834 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001835
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001836 /* On dpni_disable(), the MC firmware will:
1837 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1838 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1839 * of all in flight Tx frames is finished (and corresponding Tx conf
1840 * frames are enqueued back to software)
1841 *
1842 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1843 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1844 * and Tx conf queues are consumed on NAPI poll.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001845 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001846 dpaa2_eth_wait_for_egress_fq_empty(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001847
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001848 do {
1849 dpni_disable(priv->mc_io, 0, priv->mc_token);
1850 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1851 if (dpni_enabled)
1852 /* Allow the hardware some slack */
1853 msleep(100);
1854 } while (dpni_enabled && --retries);
1855 if (!retries) {
1856 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1857 /* Must go on and disable NAPI nonetheless, so we don't crash at
1858 * the next "ifconfig up"
1859 */
1860 }
1861
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001862 dpaa2_eth_wait_for_ingress_fq_empty(priv);
1863 dpaa2_eth_disable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001864
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001865 /* Empty the buffer pool */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001866 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001867
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001868 /* Empty the Scatter-Gather Buffer cache */
1869 dpaa2_eth_sgt_cache_drain(priv);
1870
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001871 return 0;
1872}
1873
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001874static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1875{
1876 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1877 struct device *dev = net_dev->dev.parent;
1878 int err;
1879
1880 err = eth_mac_addr(net_dev, addr);
1881 if (err < 0) {
1882 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1883 return err;
1884 }
1885
1886 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1887 net_dev->dev_addr);
1888 if (err) {
1889 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1890 return err;
1891 }
1892
1893 return 0;
1894}
1895
1896/** Fill in counters maintained by the GPP driver. These may be different from
1897 * the hardware counters obtained by ethtool.
1898 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001899static void dpaa2_eth_get_stats(struct net_device *net_dev,
1900 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001901{
1902 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1903 struct rtnl_link_stats64 *percpu_stats;
1904 u64 *cpustats;
1905 u64 *netstats = (u64 *)stats;
1906 int i, j;
1907 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1908
1909 for_each_possible_cpu(i) {
1910 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1911 cpustats = (u64 *)percpu_stats;
1912 for (j = 0; j < num; j++)
1913 netstats[j] += cpustats[j];
1914 }
1915}
1916
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001917/* Copy mac unicast addresses from @net_dev to @priv.
1918 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1919 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001920static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
1921 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001922{
1923 struct netdev_hw_addr *ha;
1924 int err;
1925
1926 netdev_for_each_uc_addr(ha, net_dev) {
1927 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1928 ha->addr);
1929 if (err)
1930 netdev_warn(priv->net_dev,
1931 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1932 ha->addr, err);
1933 }
1934}
1935
1936/* Copy mac multicast addresses from @net_dev to @priv
1937 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1938 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001939static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
1940 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001941{
1942 struct netdev_hw_addr *ha;
1943 int err;
1944
1945 netdev_for_each_mc_addr(ha, net_dev) {
1946 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1947 ha->addr);
1948 if (err)
1949 netdev_warn(priv->net_dev,
1950 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1951 ha->addr, err);
1952 }
1953}
1954
1955static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1956{
1957 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1958 int uc_count = netdev_uc_count(net_dev);
1959 int mc_count = netdev_mc_count(net_dev);
1960 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1961 u32 options = priv->dpni_attrs.options;
1962 u16 mc_token = priv->mc_token;
1963 struct fsl_mc_io *mc_io = priv->mc_io;
1964 int err;
1965
1966 /* Basic sanity checks; these probably indicate a misconfiguration */
1967 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1968 netdev_info(net_dev,
1969 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1970 max_mac);
1971
1972 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1973 if (uc_count > max_mac) {
1974 netdev_info(net_dev,
1975 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1976 uc_count, max_mac);
1977 goto force_promisc;
1978 }
1979 if (mc_count + uc_count > max_mac) {
1980 netdev_info(net_dev,
1981 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1982 uc_count + mc_count, max_mac);
1983 goto force_mc_promisc;
1984 }
1985
1986 /* Adjust promisc settings due to flag combinations */
1987 if (net_dev->flags & IFF_PROMISC)
1988 goto force_promisc;
1989 if (net_dev->flags & IFF_ALLMULTI) {
1990 /* First, rebuild unicast filtering table. This should be done
1991 * in promisc mode, in order to avoid frame loss while we
1992 * progressively add entries to the table.
1993 * We don't know whether we had been in promisc already, and
1994 * making an MC call to find out is expensive; so set uc promisc
1995 * nonetheless.
1996 */
1997 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1998 if (err)
1999 netdev_warn(net_dev, "Can't set uc promisc\n");
2000
2001 /* Actual uc table reconstruction. */
2002 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
2003 if (err)
2004 netdev_warn(net_dev, "Can't clear uc filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002005 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002006
2007 /* Finally, clear uc promisc and set mc promisc as requested. */
2008 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2009 if (err)
2010 netdev_warn(net_dev, "Can't clear uc promisc\n");
2011 goto force_mc_promisc;
2012 }
2013
2014 /* Neither unicast, nor multicast promisc will be on... eventually.
2015 * For now, rebuild mac filtering tables while forcing both of them on.
2016 */
2017 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2018 if (err)
2019 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
2020 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2021 if (err)
2022 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
2023
2024 /* Actual mac filtering tables reconstruction */
2025 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
2026 if (err)
2027 netdev_warn(net_dev, "Can't clear mac filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002028 dpaa2_eth_add_mc_hw_addr(net_dev, priv);
2029 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002030
2031 /* Now we can clear both ucast and mcast promisc, without risking
2032 * to drop legitimate frames anymore.
2033 */
2034 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2035 if (err)
2036 netdev_warn(net_dev, "Can't clear ucast promisc\n");
2037 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
2038 if (err)
2039 netdev_warn(net_dev, "Can't clear mcast promisc\n");
2040
2041 return;
2042
2043force_promisc:
2044 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2045 if (err)
2046 netdev_warn(net_dev, "Can't set ucast promisc\n");
2047force_mc_promisc:
2048 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2049 if (err)
2050 netdev_warn(net_dev, "Can't set mcast promisc\n");
2051}
2052
2053static int dpaa2_eth_set_features(struct net_device *net_dev,
2054 netdev_features_t features)
2055{
2056 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2057 netdev_features_t changed = features ^ net_dev->features;
2058 bool enable;
2059 int err;
2060
2061 if (changed & NETIF_F_RXCSUM) {
2062 enable = !!(features & NETIF_F_RXCSUM);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002063 err = dpaa2_eth_set_rx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002064 if (err)
2065 return err;
2066 }
2067
2068 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2069 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002070 err = dpaa2_eth_set_tx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002071 if (err)
2072 return err;
2073 }
2074
2075 return 0;
2076}
2077
Ioana Radulescu859f9982018-04-26 18:23:47 +08002078static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2079{
2080 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2081 struct hwtstamp_config config;
2082
Yangbo Luc5521182020-09-18 17:08:02 +08002083 if (!dpaa2_ptp)
2084 return -EINVAL;
2085
Ioana Radulescu859f9982018-04-26 18:23:47 +08002086 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
2087 return -EFAULT;
2088
2089 switch (config.tx_type) {
2090 case HWTSTAMP_TX_OFF:
Ioana Radulescu859f9982018-04-26 18:23:47 +08002091 case HWTSTAMP_TX_ON:
Yangbo Luc5521182020-09-18 17:08:02 +08002092 case HWTSTAMP_TX_ONESTEP_SYNC:
Yangbo Lu1cf773b2020-09-18 17:08:01 +08002093 priv->tx_tstamp_type = config.tx_type;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002094 break;
2095 default:
2096 return -ERANGE;
2097 }
2098
2099 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
2100 priv->rx_tstamp = false;
2101 } else {
2102 priv->rx_tstamp = true;
2103 /* TS is set for all frame types, not only those requested */
2104 config.rx_filter = HWTSTAMP_FILTER_ALL;
2105 }
2106
2107 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
2108 -EFAULT : 0;
2109}
2110
2111static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2112{
Russell King4a841822020-02-27 12:00:21 +00002113 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2114
Ioana Radulescu859f9982018-04-26 18:23:47 +08002115 if (cmd == SIOCSHWTSTAMP)
2116 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
2117
Russell King4a841822020-02-27 12:00:21 +00002118 if (priv->mac)
2119 return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2120
2121 return -EOPNOTSUPP;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002122}
2123
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002124static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2125{
2126 int mfl, linear_mfl;
2127
2128 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03002129 linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002130 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002131
2132 if (mfl > linear_mfl) {
2133 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2134 linear_mfl - VLAN_ETH_HLEN);
2135 return false;
2136 }
2137
2138 return true;
2139}
2140
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002141static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002142{
2143 int mfl, err;
2144
2145 /* We enforce a maximum Rx frame length based on MTU only if we have
2146 * an XDP program attached (in order to avoid Rx S/G frames).
2147 * Otherwise, we accept all incoming frames as long as they are not
2148 * larger than maximum size supported in hardware
2149 */
2150 if (has_xdp)
2151 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2152 else
2153 mfl = DPAA2_ETH_MFL;
2154
2155 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2156 if (err) {
2157 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2158 return err;
2159 }
2160
2161 return 0;
2162}
2163
2164static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2165{
2166 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2167 int err;
2168
2169 if (!priv->xdp_prog)
2170 goto out;
2171
2172 if (!xdp_mtu_valid(priv, new_mtu))
2173 return -EINVAL;
2174
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002175 err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002176 if (err)
2177 return err;
2178
2179out:
2180 dev->mtu = new_mtu;
2181 return 0;
2182}
2183
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002184static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002185{
2186 struct dpni_buffer_layout buf_layout = {0};
2187 int err;
2188
2189 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2190 DPNI_QUEUE_RX, &buf_layout);
2191 if (err) {
2192 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2193 return err;
2194 }
2195
2196 /* Reserve extra headroom for XDP header size changes */
2197 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2198 (has_xdp ? XDP_PACKET_HEADROOM : 0);
2199 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2200 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2201 DPNI_QUEUE_RX, &buf_layout);
2202 if (err) {
2203 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2204 return err;
2205 }
2206
2207 return 0;
2208}
2209
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002210static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002211{
2212 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2213 struct dpaa2_eth_channel *ch;
2214 struct bpf_prog *old;
2215 bool up, need_update;
2216 int i, err;
2217
2218 if (prog && !xdp_mtu_valid(priv, dev->mtu))
2219 return -EINVAL;
2220
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002221 if (prog)
2222 bpf_prog_add(prog, priv->num_channels);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002223
2224 up = netif_running(dev);
2225 need_update = (!!priv->xdp_prog != !!prog);
2226
2227 if (up)
2228 dpaa2_eth_stop(dev);
2229
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002230 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2231 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2232 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2233 * so we are sure no old format buffers will be used from now on.
2234 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002235 if (need_update) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002236 err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002237 if (err)
2238 goto out_err;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002239 err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002240 if (err)
2241 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002242 }
2243
2244 old = xchg(&priv->xdp_prog, prog);
2245 if (old)
2246 bpf_prog_put(old);
2247
2248 for (i = 0; i < priv->num_channels; i++) {
2249 ch = priv->channel[i];
2250 old = xchg(&ch->xdp.prog, prog);
2251 if (old)
2252 bpf_prog_put(old);
2253 }
2254
2255 if (up) {
2256 err = dpaa2_eth_open(dev);
2257 if (err)
2258 return err;
2259 }
2260
2261 return 0;
2262
2263out_err:
2264 if (prog)
2265 bpf_prog_sub(prog, priv->num_channels);
2266 if (up)
2267 dpaa2_eth_open(dev);
2268
2269 return err;
2270}
2271
2272static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2273{
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002274 switch (xdp->command) {
2275 case XDP_SETUP_PROG:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002276 return dpaa2_eth_setup_xdp(dev, xdp->prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002277 default:
2278 return -EINVAL;
2279 }
2280
2281 return 0;
2282}
2283
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002284static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2285 struct xdp_frame *xdpf,
2286 struct dpaa2_fd *fd)
Ioana Radulescud678be12019-03-01 17:47:24 +00002287{
Ioana Radulescud678be12019-03-01 17:47:24 +00002288 struct device *dev = net_dev->dev.parent;
Ioana Radulescud678be12019-03-01 17:47:24 +00002289 unsigned int needed_headroom;
2290 struct dpaa2_eth_swa *swa;
Ioana Radulescud678be12019-03-01 17:47:24 +00002291 void *buffer_start, *aligned_start;
2292 dma_addr_t addr;
Ioana Radulescud678be12019-03-01 17:47:24 +00002293
2294 /* We require a minimum headroom to be able to transmit the frame.
2295 * Otherwise return an error and let the original net_device handle it
2296 */
Yangbo Lu1cf773b2020-09-18 17:08:01 +08002297 needed_headroom = dpaa2_eth_needed_headroom(NULL);
Ioana Radulescud678be12019-03-01 17:47:24 +00002298 if (xdpf->headroom < needed_headroom)
2299 return -EINVAL;
2300
Ioana Radulescud678be12019-03-01 17:47:24 +00002301 /* Setup the FD fields */
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002302 memset(fd, 0, sizeof(*fd));
Ioana Radulescud678be12019-03-01 17:47:24 +00002303
2304 /* Align FD address, if possible */
2305 buffer_start = xdpf->data - needed_headroom;
2306 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2307 DPAA2_ETH_TX_BUF_ALIGN);
2308 if (aligned_start >= xdpf->data - xdpf->headroom)
2309 buffer_start = aligned_start;
2310
2311 swa = (struct dpaa2_eth_swa *)buffer_start;
2312 /* fill in necessary fields here */
2313 swa->type = DPAA2_ETH_SWA_XDP;
2314 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2315 swa->xdp.xdpf = xdpf;
2316
2317 addr = dma_map_single(dev, buffer_start,
2318 swa->xdp.dma_size,
2319 DMA_BIDIRECTIONAL);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002320 if (unlikely(dma_mapping_error(dev, addr)))
Ioana Radulescud678be12019-03-01 17:47:24 +00002321 return -ENOMEM;
Ioana Radulescud678be12019-03-01 17:47:24 +00002322
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002323 dpaa2_fd_set_addr(fd, addr);
2324 dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2325 dpaa2_fd_set_len(fd, xdpf->len);
2326 dpaa2_fd_set_format(fd, dpaa2_fd_single);
2327 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescud678be12019-03-01 17:47:24 +00002328
2329 return 0;
2330}
2331
2332static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2333 struct xdp_frame **frames, u32 flags)
2334{
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002335 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002336 struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002337 struct rtnl_link_stats64 *percpu_stats;
2338 struct dpaa2_eth_fq *fq;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002339 struct dpaa2_fd *fds;
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002340 int enqueued, i, err;
Ioana Radulescud678be12019-03-01 17:47:24 +00002341
2342 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2343 return -EINVAL;
2344
2345 if (!netif_running(net_dev))
2346 return -ENETDOWN;
2347
Ioana Ciornei8665d972020-04-22 15:05:13 +03002348 fq = &priv->fq[smp_processor_id()];
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002349 xdp_redirect_fds = &fq->xdp_redirect_fds;
2350 fds = xdp_redirect_fds->fds;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002351
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002352 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002353
Ioana Ciornei8665d972020-04-22 15:05:13 +03002354 /* create a FD for each xdp_frame in the list received */
Ioana Radulescud678be12019-03-01 17:47:24 +00002355 for (i = 0; i < n; i++) {
Ioana Ciornei8665d972020-04-22 15:05:13 +03002356 err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2357 if (err)
2358 break;
2359 }
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002360 xdp_redirect_fds->num = i;
Ioana Radulescud678be12019-03-01 17:47:24 +00002361
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002362 /* enqueue all the frame descriptors */
2363 enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
Ioana Radulescud678be12019-03-01 17:47:24 +00002364
Ioana Ciornei8665d972020-04-22 15:05:13 +03002365 /* update statistics */
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002366 percpu_stats->tx_packets += enqueued;
2367 for (i = 0; i < enqueued; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002368 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002369 for (i = enqueued; i < n; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002370 xdp_return_frame_rx_napi(frames[i]);
2371
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002372 return enqueued;
Ioana Radulescud678be12019-03-01 17:47:24 +00002373}
2374
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002375static int update_xps(struct dpaa2_eth_priv *priv)
2376{
2377 struct net_device *net_dev = priv->net_dev;
2378 struct cpumask xps_mask;
2379 struct dpaa2_eth_fq *fq;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002380 int i, num_queues, netdev_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002381 int err = 0;
2382
2383 num_queues = dpaa2_eth_queue_count(priv);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002384 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002385
2386 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2387 * queues, so only process those
2388 */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002389 for (i = 0; i < netdev_queues; i++) {
2390 fq = &priv->fq[i % num_queues];
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002391
2392 cpumask_clear(&xps_mask);
2393 cpumask_set_cpu(fq->target_cpu, &xps_mask);
2394
2395 err = netif_set_xps_queue(net_dev, &xps_mask, i);
2396 if (err) {
2397 netdev_warn_once(net_dev, "Error setting XPS queue\n");
2398 break;
2399 }
2400 }
2401
2402 return err;
2403}
2404
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002405static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2406 struct tc_mqprio_qopt *mqprio)
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002407{
2408 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002409 u8 num_tc, num_queues;
2410 int i;
2411
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002412 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2413 num_queues = dpaa2_eth_queue_count(priv);
2414 num_tc = mqprio->num_tc;
2415
2416 if (num_tc == net_dev->num_tc)
2417 return 0;
2418
2419 if (num_tc > dpaa2_eth_tc_count(priv)) {
2420 netdev_err(net_dev, "Max %d traffic classes supported\n",
2421 dpaa2_eth_tc_count(priv));
Jesper Dangaard Brouerb89c1e62020-04-23 16:57:50 +02002422 return -EOPNOTSUPP;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002423 }
2424
2425 if (!num_tc) {
2426 netdev_reset_tc(net_dev);
2427 netif_set_real_num_tx_queues(net_dev, num_queues);
2428 goto out;
2429 }
2430
2431 netdev_set_num_tc(net_dev, num_tc);
2432 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2433
2434 for (i = 0; i < num_tc; i++)
2435 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2436
2437out:
2438 update_xps(priv);
2439
2440 return 0;
2441}
2442
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002443#define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2444
2445static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2446{
2447 struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2448 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2449 struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2450 struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2451 int err;
2452
2453 if (p->command == TC_TBF_STATS)
2454 return -EOPNOTSUPP;
2455
2456 /* Only per port Tx shaping */
2457 if (p->parent != TC_H_ROOT)
2458 return -EOPNOTSUPP;
2459
2460 if (p->command == TC_TBF_REPLACE) {
2461 if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2462 netdev_err(net_dev, "burst size cannot be greater than %d\n",
2463 DPAA2_ETH_MAX_BURST_SIZE);
2464 return -EINVAL;
2465 }
2466
2467 tx_cr_shaper.max_burst_size = cfg->max_size;
2468 /* The TBF interface is in bytes/s, whereas DPAA2 expects the
2469 * rate in Mbits/s
2470 */
2471 tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2472 }
2473
2474 err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2475 &tx_er_shaper, 0);
2476 if (err) {
2477 netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2478 return err;
2479 }
2480
2481 return 0;
2482}
2483
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002484static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2485 enum tc_setup_type type, void *type_data)
2486{
2487 switch (type) {
2488 case TC_SETUP_QDISC_MQPRIO:
2489 return dpaa2_eth_setup_mqprio(net_dev, type_data);
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002490 case TC_SETUP_QDISC_TBF:
2491 return dpaa2_eth_setup_tbf(net_dev, type_data);
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002492 default:
2493 return -EOPNOTSUPP;
2494 }
2495}
2496
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002497static const struct net_device_ops dpaa2_eth_ops = {
2498 .ndo_open = dpaa2_eth_open,
2499 .ndo_start_xmit = dpaa2_eth_tx,
2500 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002501 .ndo_set_mac_address = dpaa2_eth_set_addr,
2502 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002503 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2504 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08002505 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002506 .ndo_change_mtu = dpaa2_eth_change_mtu,
2507 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescud678be12019-03-01 17:47:24 +00002508 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002509 .ndo_setup_tc = dpaa2_eth_setup_tc,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002510};
2511
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002512static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002513{
2514 struct dpaa2_eth_channel *ch;
2515
2516 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002517
2518 /* Update NAPI statistics */
2519 ch->stats.cdan++;
2520
Jiafei Pan6c33ae12020-08-03 23:10:08 +03002521 napi_schedule(&ch->napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002522}
2523
2524/* Allocate and configure a DPCON object */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002525static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002526{
2527 struct fsl_mc_device *dpcon;
2528 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002529 int err;
2530
2531 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2532 FSL_MC_POOL_DPCON, &dpcon);
2533 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002534 if (err == -ENXIO)
2535 err = -EPROBE_DEFER;
2536 else
2537 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2538 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002539 }
2540
2541 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2542 if (err) {
2543 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002544 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002545 }
2546
2547 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2548 if (err) {
2549 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002550 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002551 }
2552
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002553 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2554 if (err) {
2555 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002556 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002557 }
2558
2559 return dpcon;
2560
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002561close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002562 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002563free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002564 fsl_mc_object_free(dpcon);
2565
YueHaibing02afa9c2020-08-04 21:26:43 +08002566 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002567}
2568
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002569static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
2570 struct fsl_mc_device *dpcon)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002571{
2572 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2573 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2574 fsl_mc_object_free(dpcon);
2575}
2576
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002577static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002578{
2579 struct dpaa2_eth_channel *channel;
2580 struct dpcon_attr attr;
2581 struct device *dev = priv->net_dev->dev.parent;
2582 int err;
2583
2584 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2585 if (!channel)
2586 return NULL;
2587
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002588 channel->dpcon = dpaa2_eth_setup_dpcon(priv);
YueHaibing02afa9c2020-08-04 21:26:43 +08002589 if (IS_ERR(channel->dpcon)) {
2590 err = PTR_ERR(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002591 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002592 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002593
2594 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2595 &attr);
2596 if (err) {
2597 dev_err(dev, "dpcon_get_attributes() failed\n");
2598 goto err_get_attr;
2599 }
2600
2601 channel->dpcon_id = attr.id;
2602 channel->ch_id = attr.qbman_ch_id;
2603 channel->priv = priv;
2604
2605 return channel;
2606
2607err_get_attr:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002608 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002609err_setup:
2610 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002611 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002612}
2613
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002614static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
2615 struct dpaa2_eth_channel *channel)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002616{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002617 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002618 kfree(channel);
2619}
2620
2621/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2622 * and register data availability notifications
2623 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002624static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002625{
2626 struct dpaa2_io_notification_ctx *nctx;
2627 struct dpaa2_eth_channel *channel;
2628 struct dpcon_notification_cfg dpcon_notif_cfg;
2629 struct device *dev = priv->net_dev->dev.parent;
2630 int i, err;
2631
2632 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2633 * many cores as possible, so we need one channel for each core
2634 * (unless there's fewer queues than cores, in which case the extra
2635 * channels would be wasted).
2636 * Allocate one channel per core and register it to the core's
2637 * affine DPIO. If not enough channels are available for all cores
2638 * or if some cores don't have an affine DPIO, there will be no
2639 * ingress frame processing on those cores.
2640 */
2641 cpumask_clear(&priv->dpio_cpumask);
2642 for_each_online_cpu(i) {
2643 /* Try to allocate a channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002644 channel = dpaa2_eth_alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002645 if (IS_ERR_OR_NULL(channel)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002646 err = PTR_ERR_OR_ZERO(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002647 if (err != -EPROBE_DEFER)
2648 dev_info(dev,
2649 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002650 goto err_alloc_ch;
2651 }
2652
2653 priv->channel[priv->num_channels] = channel;
2654
2655 nctx = &channel->nctx;
2656 nctx->is_cdan = 1;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002657 nctx->cb = dpaa2_eth_cdan_cb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002658 nctx->id = channel->ch_id;
2659 nctx->desired_cpu = i;
2660
2661 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06002662 channel->dpio = dpaa2_io_service_select(i);
Ioana Ciornei47441f72018-12-10 16:50:19 +00002663 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002664 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002665 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002666 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002667 * none available for next cores either. Signal we want
2668 * to retry later, in case the DPIO devices weren't
2669 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002670 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002671 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002672 goto err_service_reg;
2673 }
2674
2675 /* Register DPCON notification with MC */
2676 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2677 dpcon_notif_cfg.priority = 0;
2678 dpcon_notif_cfg.user_ctx = nctx->qman64;
2679 err = dpcon_set_notification(priv->mc_io, 0,
2680 channel->dpcon->mc_handle,
2681 &dpcon_notif_cfg);
2682 if (err) {
2683 dev_err(dev, "dpcon_set_notification failed()\n");
2684 goto err_set_cdan;
2685 }
2686
2687 /* If we managed to allocate a channel and also found an affine
2688 * DPIO for this core, add it to the final mask
2689 */
2690 cpumask_set_cpu(i, &priv->dpio_cpumask);
2691 priv->num_channels++;
2692
2693 /* Stop if we already have enough channels to accommodate all
2694 * RX and TX conf queues
2695 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00002696 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002697 break;
2698 }
2699
2700 return 0;
2701
2702err_set_cdan:
Ioana Ciornei47441f72018-12-10 16:50:19 +00002703 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002704err_service_reg:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002705 dpaa2_eth_free_channel(priv, channel);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002706err_alloc_ch:
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002707 if (err == -EPROBE_DEFER) {
2708 for (i = 0; i < priv->num_channels; i++) {
2709 channel = priv->channel[i];
2710 nctx = &channel->nctx;
2711 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002712 dpaa2_eth_free_channel(priv, channel);
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002713 }
2714 priv->num_channels = 0;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002715 return err;
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002716 }
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002717
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002718 if (cpumask_empty(&priv->dpio_cpumask)) {
2719 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002720 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002721 }
2722
2723 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2724 cpumask_pr_args(&priv->dpio_cpumask));
2725
2726 return 0;
2727}
2728
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002729static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002730{
Ioana Ciornei47441f72018-12-10 16:50:19 +00002731 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002732 struct dpaa2_eth_channel *ch;
Ioana Ciornei47441f72018-12-10 16:50:19 +00002733 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002734
2735 /* deregister CDAN notifications and free channels */
2736 for (i = 0; i < priv->num_channels; i++) {
2737 ch = priv->channel[i];
Ioana Ciornei47441f72018-12-10 16:50:19 +00002738 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002739 dpaa2_eth_free_channel(priv, ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002740 }
2741}
2742
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002743static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
2744 int cpu)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002745{
2746 struct device *dev = priv->net_dev->dev.parent;
2747 int i;
2748
2749 for (i = 0; i < priv->num_channels; i++)
2750 if (priv->channel[i]->nctx.desired_cpu == cpu)
2751 return priv->channel[i];
2752
2753 /* We should never get here. Issue a warning and return
2754 * the first channel, because it's still better than nothing
2755 */
2756 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2757
2758 return priv->channel[0];
2759}
2760
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002761static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002762{
2763 struct device *dev = priv->net_dev->dev.parent;
2764 struct dpaa2_eth_fq *fq;
2765 int rx_cpu, txc_cpu;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002766 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002767
2768 /* For each FQ, pick one channel/CPU to deliver frames to.
2769 * This may well change at runtime, either through irqbalance or
2770 * through direct user intervention.
2771 */
2772 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2773
2774 for (i = 0; i < priv->num_fqs; i++) {
2775 fq = &priv->fq[i];
2776 switch (fq->type) {
2777 case DPAA2_RX_FQ:
Ioana Ciornei061d6312020-10-01 18:11:48 +03002778 case DPAA2_RX_ERR_FQ:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002779 fq->target_cpu = rx_cpu;
2780 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2781 if (rx_cpu >= nr_cpu_ids)
2782 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2783 break;
2784 case DPAA2_TX_CONF_FQ:
2785 fq->target_cpu = txc_cpu;
2786 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2787 if (txc_cpu >= nr_cpu_ids)
2788 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2789 break;
2790 default:
2791 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2792 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002793 fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002794 }
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002795
2796 update_xps(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002797}
2798
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002799static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002800{
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002801 int i, j;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002802
2803 /* We have one TxConf FQ per Tx flow.
2804 * The number of Tx and Rx queues is the same.
2805 * Tx queues come first in the fq array.
2806 */
2807 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2808 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2809 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2810 priv->fq[priv->num_fqs++].flowid = (u16)i;
2811 }
2812
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002813 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2814 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2815 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2816 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2817 priv->fq[priv->num_fqs].tc = (u8)j;
2818 priv->fq[priv->num_fqs++].flowid = (u16)i;
2819 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002820 }
2821
Ioana Ciornei061d6312020-10-01 18:11:48 +03002822 /* We have exactly one Rx error queue per DPNI */
2823 priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
2824 priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
2825
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002826 /* For each FQ, decide on which core to process incoming frames */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002827 dpaa2_eth_set_fq_affinity(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002828}
2829
2830/* Allocate and configure one buffer pool for each interface */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002831static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002832{
2833 int err;
2834 struct fsl_mc_device *dpbp_dev;
2835 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002836 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002837
2838 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2839 &dpbp_dev);
2840 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002841 if (err == -ENXIO)
2842 err = -EPROBE_DEFER;
2843 else
2844 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002845 return err;
2846 }
2847
2848 priv->dpbp_dev = dpbp_dev;
2849
2850 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2851 &dpbp_dev->mc_handle);
2852 if (err) {
2853 dev_err(dev, "dpbp_open() failed\n");
2854 goto err_open;
2855 }
2856
Ioana Radulescud00defe2017-06-06 10:00:32 -05002857 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2858 if (err) {
2859 dev_err(dev, "dpbp_reset() failed\n");
2860 goto err_reset;
2861 }
2862
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002863 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2864 if (err) {
2865 dev_err(dev, "dpbp_enable() failed\n");
2866 goto err_enable;
2867 }
2868
2869 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002870 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002871 if (err) {
2872 dev_err(dev, "dpbp_get_attributes() failed\n");
2873 goto err_get_attr;
2874 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002875 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002876
2877 return 0;
2878
2879err_get_attr:
2880 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2881err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002882err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002883 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2884err_open:
2885 fsl_mc_object_free(dpbp_dev);
2886
2887 return err;
2888}
2889
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002890static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002891{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002892 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002893 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2894 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2895 fsl_mc_object_free(priv->dpbp_dev);
2896}
2897
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002898static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002899{
2900 struct device *dev = priv->net_dev->dev.parent;
2901 struct dpni_buffer_layout buf_layout = {0};
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002902 u16 rx_buf_align;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002903 int err;
2904
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002905 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2906 * version, this number is not always provided correctly on rev1.
2907 * We need to check for both alternatives in this situation.
2908 */
2909 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2910 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002911 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002912 else
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002913 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002914
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03002915 /* We need to ensure that the buffer size seen by WRIOP is a multiple
2916 * of 64 or 256 bytes depending on the WRIOP version.
2917 */
2918 priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
2919
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002920 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002921 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002922 buf_layout.pass_timestamp = true;
Yangbo Luc5521182020-09-18 17:08:02 +08002923 buf_layout.pass_frame_status = true;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002924 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
Yangbo Luc5521182020-09-18 17:08:02 +08002925 DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
2926 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002927 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2928 DPNI_QUEUE_TX, &buf_layout);
2929 if (err) {
2930 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2931 return err;
2932 }
2933
2934 /* tx-confirm buffer */
Yangbo Luc5521182020-09-18 17:08:02 +08002935 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
2936 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002937 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2938 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2939 if (err) {
2940 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2941 return err;
2942 }
2943
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002944 /* Now that we've set our tx buffer layout, retrieve the minimum
2945 * required tx data offset.
2946 */
2947 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2948 &priv->tx_data_offset);
2949 if (err) {
2950 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2951 return err;
2952 }
2953
2954 if ((priv->tx_data_offset % 64) != 0)
2955 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2956 priv->tx_data_offset);
2957
2958 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06002959 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002960 buf_layout.pass_parser_result = true;
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002961 buf_layout.data_align = rx_buf_align;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002962 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2963 buf_layout.private_data_size = 0;
2964 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2965 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2966 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08002967 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2968 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002969 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2970 DPNI_QUEUE_RX, &buf_layout);
2971 if (err) {
2972 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2973 return err;
2974 }
2975
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002976 return 0;
2977}
2978
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002979#define DPNI_ENQUEUE_FQID_VER_MAJOR 7
2980#define DPNI_ENQUEUE_FQID_VER_MINOR 9
2981
2982static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2983 struct dpaa2_eth_fq *fq,
Ioana Ciornei48c04812020-04-22 15:05:10 +03002984 struct dpaa2_fd *fd, u8 prio,
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002985 u32 num_frames __always_unused,
Ioana Ciornei48c04812020-04-22 15:05:10 +03002986 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002987{
Ioana Ciornei48c04812020-04-22 15:05:10 +03002988 int err;
2989
2990 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
2991 priv->tx_qdid, prio,
2992 fq->tx_qdbin, fd);
2993 if (!err && frames_enqueued)
2994 *frames_enqueued = 1;
2995 return err;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002996}
2997
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002998static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
2999 struct dpaa2_eth_fq *fq,
3000 struct dpaa2_fd *fd,
3001 u8 prio, u32 num_frames,
3002 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003003{
Ioana Ciornei48c04812020-04-22 15:05:10 +03003004 int err;
3005
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003006 err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
3007 fq->tx_fqid[prio],
3008 fd, num_frames);
3009
3010 if (err == 0)
3011 return -EBUSY;
3012
3013 if (frames_enqueued)
3014 *frames_enqueued = err;
3015 return 0;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003016}
3017
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003018static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003019{
3020 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3021 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3022 priv->enqueue = dpaa2_eth_enqueue_qd;
3023 else
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003024 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003025}
3026
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003027static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003028{
3029 struct device *dev = priv->net_dev->dev.parent;
3030 struct dpni_link_cfg link_cfg = {0};
3031 int err;
3032
3033 /* Get the default link options so we don't override other flags */
3034 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3035 if (err) {
3036 dev_err(dev, "dpni_get_link_cfg() failed\n");
3037 return err;
3038 }
3039
3040 /* By default, enable both Rx and Tx pause frames */
3041 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
3042 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
3043 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3044 if (err) {
3045 dev_err(dev, "dpni_set_link_cfg() failed\n");
3046 return err;
3047 }
3048
3049 priv->link_state.options = link_cfg.options;
3050
3051 return 0;
3052}
3053
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003054static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
Ioana Radulescua690af4f2019-10-16 10:36:23 +03003055{
3056 struct dpni_queue_id qid = {0};
3057 struct dpaa2_eth_fq *fq;
3058 struct dpni_queue queue;
3059 int i, j, err;
3060
3061 /* We only use Tx FQIDs for FQID-based enqueue, so check
3062 * if DPNI version supports it before updating FQIDs
3063 */
3064 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3065 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3066 return;
3067
3068 for (i = 0; i < priv->num_fqs; i++) {
3069 fq = &priv->fq[i];
3070 if (fq->type != DPAA2_TX_CONF_FQ)
3071 continue;
3072 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3073 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3074 DPNI_QUEUE_TX, j, fq->flowid,
3075 &queue, &qid);
3076 if (err)
3077 goto out_err;
3078
3079 fq->tx_fqid[j] = qid.fqid;
3080 if (fq->tx_fqid[j] == 0)
3081 goto out_err;
3082 }
3083 }
3084
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003085 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Radulescua690af4f2019-10-16 10:36:23 +03003086
3087 return;
3088
3089out_err:
3090 netdev_info(priv->net_dev,
3091 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3092 priv->enqueue = dpaa2_eth_enqueue_qd;
3093}
3094
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003095/* Configure ingress classification based on VLAN PCP */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003096static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003097{
3098 struct device *dev = priv->net_dev->dev.parent;
3099 struct dpkg_profile_cfg kg_cfg = {0};
3100 struct dpni_qos_tbl_cfg qos_cfg = {0};
3101 struct dpni_rule_cfg key_params;
3102 void *dma_mem, *key, *mask;
3103 u8 key_size = 2; /* VLAN TCI field */
3104 int i, pcp, err;
3105
3106 /* VLAN-based classification only makes sense if we have multiple
3107 * traffic classes.
3108 * Also, we need to extract just the 3-bit PCP field from the VLAN
3109 * header and we can only do that by using a mask
3110 */
3111 if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3112 dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3113 return -EOPNOTSUPP;
3114 }
3115
3116 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3117 if (!dma_mem)
3118 return -ENOMEM;
3119
3120 kg_cfg.num_extracts = 1;
3121 kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3122 kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3123 kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3124 kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3125
3126 err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3127 if (err) {
3128 dev_err(dev, "dpni_prepare_key_cfg failed\n");
3129 goto out_free_tbl;
3130 }
3131
3132 /* set QoS table */
3133 qos_cfg.default_tc = 0;
3134 qos_cfg.discard_on_miss = 0;
3135 qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3136 DPAA2_CLASSIFIER_DMA_SIZE,
3137 DMA_TO_DEVICE);
3138 if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3139 dev_err(dev, "QoS table DMA mapping failed\n");
3140 err = -ENOMEM;
3141 goto out_free_tbl;
3142 }
3143
3144 err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3145 if (err) {
3146 dev_err(dev, "dpni_set_qos_table failed\n");
3147 goto out_unmap_tbl;
3148 }
3149
3150 /* Add QoS table entries */
3151 key = kzalloc(key_size * 2, GFP_KERNEL);
3152 if (!key) {
3153 err = -ENOMEM;
3154 goto out_unmap_tbl;
3155 }
3156 mask = key + key_size;
3157 *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3158
3159 key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3160 DMA_TO_DEVICE);
3161 if (dma_mapping_error(dev, key_params.key_iova)) {
3162 dev_err(dev, "Qos table entry DMA mapping failed\n");
3163 err = -ENOMEM;
3164 goto out_free_key;
3165 }
3166
3167 key_params.mask_iova = key_params.key_iova + key_size;
3168 key_params.key_size = key_size;
3169
3170 /* We add rules for PCP-based distribution starting with highest
3171 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3172 * classes to accommodate all priority levels, the lowest ones end up
3173 * on TC 0 which was configured as default
3174 */
3175 for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3176 *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3177 dma_sync_single_for_device(dev, key_params.key_iova,
3178 key_size * 2, DMA_TO_DEVICE);
3179
3180 err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3181 &key_params, i, i);
3182 if (err) {
3183 dev_err(dev, "dpni_add_qos_entry failed\n");
3184 dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3185 goto out_unmap_key;
3186 }
3187 }
3188
3189 priv->vlan_cls_enabled = true;
3190
3191 /* Table and key memory is not persistent, clean everything up after
3192 * configuration is finished
3193 */
3194out_unmap_key:
3195 dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3196out_free_key:
3197 kfree(key);
3198out_unmap_tbl:
3199 dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3200 DMA_TO_DEVICE);
3201out_free_tbl:
3202 kfree(dma_mem);
3203
3204 return err;
3205}
3206
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003207/* Configure the DPNI object this interface is associated with */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003208static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003209{
3210 struct device *dev = &ls_dev->dev;
3211 struct dpaa2_eth_priv *priv;
3212 struct net_device *net_dev;
3213 int err;
3214
3215 net_dev = dev_get_drvdata(dev);
3216 priv = netdev_priv(net_dev);
3217
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003218 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05003219 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003220 if (err) {
3221 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003222 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003223 }
3224
Ioana Radulescu311cffa2018-03-23 08:44:09 -05003225 /* Check if we can work with this DPNI object */
3226 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3227 &priv->dpni_ver_minor);
3228 if (err) {
3229 dev_err(dev, "dpni_get_api_version() failed\n");
3230 goto close;
3231 }
3232 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3233 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3234 priv->dpni_ver_major, priv->dpni_ver_minor,
3235 DPNI_VER_MAJOR, DPNI_VER_MINOR);
3236 err = -ENOTSUPP;
3237 goto close;
3238 }
3239
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003240 ls_dev->mc_io = priv->mc_io;
3241 ls_dev->mc_handle = priv->mc_token;
3242
3243 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3244 if (err) {
3245 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003246 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003247 }
3248
3249 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3250 &priv->dpni_attrs);
3251 if (err) {
3252 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003253 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003254 }
3255
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003256 err = dpaa2_eth_set_buffer_layout(priv);
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003257 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003258 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003259
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003260 dpaa2_eth_set_enqueue_mode(priv);
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003261
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003262 /* Enable pause frame support */
3263 if (dpaa2_eth_has_pause_support(priv)) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003264 err = dpaa2_eth_set_pause(priv);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003265 if (err)
3266 goto close;
3267 }
3268
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003269 err = dpaa2_eth_set_vlan_qos(priv);
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003270 if (err && err != -EOPNOTSUPP)
3271 goto close;
3272
Xu Wang9334d5b2020-06-11 02:45:20 +00003273 priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3274 sizeof(struct dpaa2_eth_cls_rule),
3275 GFP_KERNEL);
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003276 if (!priv->cls_rules) {
3277 err = -ENOMEM;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003278 goto close;
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003279 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003280
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003281 return 0;
3282
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003283close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003284 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003285
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003286 return err;
3287}
3288
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003289static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003290{
3291 int err;
3292
3293 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3294 if (err)
3295 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3296 err);
3297
3298 dpni_close(priv->mc_io, 0, priv->mc_token);
3299}
3300
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003301static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3302 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003303{
3304 struct device *dev = priv->net_dev->dev.parent;
3305 struct dpni_queue queue;
3306 struct dpni_queue_id qid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003307 int err;
3308
3309 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003310 DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003311 if (err) {
3312 dev_err(dev, "dpni_get_queue(RX) failed\n");
3313 return err;
3314 }
3315
3316 fq->fqid = qid.fqid;
3317
3318 queue.destination.id = fq->channel->dpcon_id;
3319 queue.destination.type = DPNI_DEST_DPCON;
3320 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003321 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003322 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003323 DPNI_QUEUE_RX, fq->tc, fq->flowid,
Ioana Radulescu16fa1cf2019-05-23 17:38:22 +03003324 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003325 &queue);
3326 if (err) {
3327 dev_err(dev, "dpni_set_queue(RX) failed\n");
3328 return err;
3329 }
3330
Ioana Radulescud678be12019-03-01 17:47:24 +00003331 /* xdp_rxq setup */
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003332 /* only once for each channel */
3333 if (fq->tc > 0)
3334 return 0;
3335
Ioana Radulescud678be12019-03-01 17:47:24 +00003336 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
Björn Töpelb02e5a02020-11-30 19:52:01 +01003337 fq->flowid, 0);
Ioana Radulescud678be12019-03-01 17:47:24 +00003338 if (err) {
3339 dev_err(dev, "xdp_rxq_info_reg failed\n");
3340 return err;
3341 }
3342
3343 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3344 MEM_TYPE_PAGE_ORDER0, NULL);
3345 if (err) {
3346 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3347 return err;
3348 }
3349
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003350 return 0;
3351}
3352
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003353static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3354 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003355{
3356 struct device *dev = priv->net_dev->dev.parent;
3357 struct dpni_queue queue;
3358 struct dpni_queue_id qid;
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003359 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003360
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003361 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3362 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3363 DPNI_QUEUE_TX, i, fq->flowid,
3364 &queue, &qid);
3365 if (err) {
3366 dev_err(dev, "dpni_get_queue(TX) failed\n");
3367 return err;
3368 }
3369 fq->tx_fqid[i] = qid.fqid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003370 }
3371
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003372 /* All Tx queues belonging to the same flowid have the same qdbin */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003373 fq->tx_qdbin = qid.qdbin;
3374
3375 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3376 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3377 &queue, &qid);
3378 if (err) {
3379 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3380 return err;
3381 }
3382
3383 fq->fqid = qid.fqid;
3384
3385 queue.destination.id = fq->channel->dpcon_id;
3386 queue.destination.type = DPNI_DEST_DPCON;
3387 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003388 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003389 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3390 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3391 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3392 &queue);
3393 if (err) {
3394 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3395 return err;
3396 }
3397
3398 return 0;
3399}
3400
Ioana Ciornei061d6312020-10-01 18:11:48 +03003401static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
3402 struct dpaa2_eth_fq *fq)
3403{
3404 struct device *dev = priv->net_dev->dev.parent;
3405 struct dpni_queue q = { { 0 } };
3406 struct dpni_queue_id qid;
3407 u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
3408 int err;
3409
3410 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3411 DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
3412 if (err) {
3413 dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
3414 return err;
3415 }
3416
3417 fq->fqid = qid.fqid;
3418
3419 q.destination.id = fq->channel->dpcon_id;
3420 q.destination.type = DPNI_DEST_DPCON;
3421 q.destination.priority = 1;
3422 q.user_context = (u64)(uintptr_t)fq;
3423 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3424 DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
3425 if (err) {
3426 dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
3427 return err;
3428 }
3429
3430 return 0;
3431}
3432
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003433/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003434static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003435 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003436 /* L2 header */
3437 .rxnfc_field = RXH_L2DA,
3438 .cls_prot = NET_PROT_ETH,
3439 .cls_field = NH_FLD_ETH_DA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003440 .id = DPAA2_ETH_DIST_ETHDST,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003441 .size = 6,
3442 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003443 .cls_prot = NET_PROT_ETH,
3444 .cls_field = NH_FLD_ETH_SA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003445 .id = DPAA2_ETH_DIST_ETHSRC,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003446 .size = 6,
3447 }, {
3448 /* This is the last ethertype field parsed:
3449 * depending on frame format, it can be the MAC ethertype
3450 * or the VLAN etype.
3451 */
3452 .cls_prot = NET_PROT_ETH,
3453 .cls_field = NH_FLD_ETH_TYPE,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003454 .id = DPAA2_ETH_DIST_ETHTYPE,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003455 .size = 2,
3456 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003457 /* VLAN header */
3458 .rxnfc_field = RXH_VLAN,
3459 .cls_prot = NET_PROT_VLAN,
3460 .cls_field = NH_FLD_VLAN_TCI,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003461 .id = DPAA2_ETH_DIST_VLAN,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003462 .size = 2,
3463 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003464 /* IP header */
3465 .rxnfc_field = RXH_IP_SRC,
3466 .cls_prot = NET_PROT_IP,
3467 .cls_field = NH_FLD_IP_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003468 .id = DPAA2_ETH_DIST_IPSRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003469 .size = 4,
3470 }, {
3471 .rxnfc_field = RXH_IP_DST,
3472 .cls_prot = NET_PROT_IP,
3473 .cls_field = NH_FLD_IP_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003474 .id = DPAA2_ETH_DIST_IPDST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003475 .size = 4,
3476 }, {
3477 .rxnfc_field = RXH_L3_PROTO,
3478 .cls_prot = NET_PROT_IP,
3479 .cls_field = NH_FLD_IP_PROTO,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003480 .id = DPAA2_ETH_DIST_IPPROTO,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003481 .size = 1,
3482 }, {
3483 /* Using UDP ports, this is functionally equivalent to raw
3484 * byte pairs from L4 header.
3485 */
3486 .rxnfc_field = RXH_L4_B_0_1,
3487 .cls_prot = NET_PROT_UDP,
3488 .cls_field = NH_FLD_UDP_PORT_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003489 .id = DPAA2_ETH_DIST_L4SRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003490 .size = 2,
3491 }, {
3492 .rxnfc_field = RXH_L4_B_2_3,
3493 .cls_prot = NET_PROT_UDP,
3494 .cls_field = NH_FLD_UDP_PORT_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003495 .id = DPAA2_ETH_DIST_L4DST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003496 .size = 2,
3497 },
3498};
3499
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003500/* Configure the Rx hash key using the legacy API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003501static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003502{
3503 struct device *dev = priv->net_dev->dev.parent;
3504 struct dpni_rx_tc_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003505 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003506
3507 memset(&dist_cfg, 0, sizeof(dist_cfg));
3508
3509 dist_cfg.key_cfg_iova = key;
3510 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3511 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3512
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003513 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3514 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3515 i, &dist_cfg);
3516 if (err) {
3517 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3518 break;
3519 }
3520 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003521
3522 return err;
3523}
3524
3525/* Configure the Rx hash key using the new API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003526static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003527{
3528 struct device *dev = priv->net_dev->dev.parent;
3529 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003530 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003531
3532 memset(&dist_cfg, 0, sizeof(dist_cfg));
3533
3534 dist_cfg.key_cfg_iova = key;
3535 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3536 dist_cfg.enable = 1;
3537
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003538 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3539 dist_cfg.tc = i;
3540 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3541 &dist_cfg);
3542 if (err) {
3543 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3544 break;
3545 }
Ionut-robert Aron5e29c162020-09-25 17:44:21 +03003546
3547 /* If the flow steering / hashing key is shared between all
3548 * traffic classes, install it just once
3549 */
3550 if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3551 break;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003552 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003553
3554 return err;
3555}
3556
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003557/* Configure the Rx flow classification key */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003558static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003559{
3560 struct device *dev = priv->net_dev->dev.parent;
3561 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003562 int i, err = 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003563
3564 memset(&dist_cfg, 0, sizeof(dist_cfg));
3565
3566 dist_cfg.key_cfg_iova = key;
3567 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3568 dist_cfg.enable = 1;
3569
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003570 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3571 dist_cfg.tc = i;
3572 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3573 &dist_cfg);
3574 if (err) {
3575 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3576 break;
3577 }
Ionut-robert Aron5e29c162020-09-25 17:44:21 +03003578
3579 /* If the flow steering / hashing key is shared between all
3580 * traffic classes, install it just once
3581 */
3582 if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3583 break;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003584 }
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003585
3586 return err;
3587}
3588
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003589/* Size of the Rx flow classification key */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003590int dpaa2_eth_cls_key_size(u64 fields)
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003591{
3592 int i, size = 0;
3593
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003594 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3595 if (!(fields & dist_fields[i].id))
3596 continue;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003597 size += dist_fields[i].size;
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003598 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003599
3600 return size;
3601}
3602
3603/* Offset of header field in Rx classification key */
3604int dpaa2_eth_cls_fld_off(int prot, int field)
3605{
3606 int i, off = 0;
3607
3608 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3609 if (dist_fields[i].cls_prot == prot &&
3610 dist_fields[i].cls_field == field)
3611 return off;
3612 off += dist_fields[i].size;
3613 }
3614
3615 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3616 return 0;
3617}
3618
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003619/* Prune unused fields from the classification rule.
3620 * Used when masking is not supported
3621 */
3622void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3623{
3624 int off = 0, new_off = 0;
3625 int i, size;
3626
3627 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3628 size = dist_fields[i].size;
3629 if (dist_fields[i].id & fields) {
3630 memcpy(key_mem + new_off, key_mem + off, size);
3631 new_off += size;
3632 }
3633 off += size;
3634 }
3635}
3636
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003637/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003638 * flags is a combination of RXH_ bits
3639 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00003640static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
3641 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003642{
3643 struct device *dev = net_dev->dev.parent;
3644 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3645 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003646 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003647 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003648 u8 *dma_mem;
3649 int i;
3650 int err = 0;
3651
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003652 memset(&cls_cfg, 0, sizeof(cls_cfg));
3653
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003654 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003655 struct dpkg_extract *key =
3656 &cls_cfg.extracts[cls_cfg.num_extracts];
3657
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003658 /* For both Rx hashing and classification keys
3659 * we set only the selected fields.
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003660 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003661 if (!(flags & dist_fields[i].id))
3662 continue;
3663 if (type == DPAA2_ETH_RX_DIST_HASH)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003664 rx_hash_fields |= dist_fields[i].rxnfc_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003665
3666 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
3667 dev_err(dev, "error adding key extraction rule, too many rules?\n");
3668 return -E2BIG;
3669 }
3670
3671 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003672 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003673 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003674 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003675 cls_cfg.num_extracts++;
3676 }
3677
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05003678 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003679 if (!dma_mem)
3680 return -ENOMEM;
3681
3682 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3683 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003684 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003685 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003686 }
3687
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003688 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003689 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3690 DMA_TO_DEVICE);
3691 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003692 dev_err(dev, "DMA mapping failed\n");
3693 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003694 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003695 }
3696
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003697 if (type == DPAA2_ETH_RX_DIST_HASH) {
3698 if (dpaa2_eth_has_legacy_dist(priv))
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003699 err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003700 else
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003701 err = dpaa2_eth_config_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003702 } else {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003703 err = dpaa2_eth_config_cls_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003704 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003705
3706 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3707 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003708 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003709 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003710
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003711free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003712 kfree(dma_mem);
3713 return err;
3714}
3715
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003716int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3717{
3718 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003719 u64 key = 0;
3720 int i;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003721
3722 if (!dpaa2_eth_hash_enabled(priv))
3723 return -EOPNOTSUPP;
3724
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003725 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3726 if (dist_fields[i].rxnfc_field & flags)
3727 key |= dist_fields[i].id;
3728
3729 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003730}
3731
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003732int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3733{
3734 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3735}
3736
3737static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003738{
3739 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003740 int err;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003741
3742 /* Check if we actually support Rx flow classification */
3743 if (dpaa2_eth_has_legacy_dist(priv)) {
3744 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3745 return -EOPNOTSUPP;
3746 }
3747
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003748 if (!dpaa2_eth_fs_enabled(priv)) {
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003749 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3750 return -EOPNOTSUPP;
3751 }
3752
3753 if (!dpaa2_eth_hash_enabled(priv)) {
3754 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3755 return -EOPNOTSUPP;
3756 }
3757
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003758 /* If there is no support for masking in the classification table,
3759 * we don't set a default key, as it will depend on the rules
3760 * added by the user at runtime.
3761 */
3762 if (!dpaa2_eth_fs_mask_enabled(priv))
3763 goto out;
3764
3765 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003766 if (err)
3767 return err;
3768
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003769out:
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003770 priv->rx_cls_enabled = 1;
3771
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003772 return 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003773}
3774
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003775/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3776 * frame queues and channels
3777 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003778static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003779{
3780 struct net_device *net_dev = priv->net_dev;
3781 struct device *dev = net_dev->dev.parent;
3782 struct dpni_pools_cfg pools_params;
3783 struct dpni_error_cfg err_cfg;
3784 int err = 0;
3785 int i;
3786
3787 pools_params.num_dpbp = 1;
3788 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3789 pools_params.pools[0].backup_pool = 0;
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03003790 pools_params.pools[0].buffer_size = priv->rx_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003791 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3792 if (err) {
3793 dev_err(dev, "dpni_set_pools() failed\n");
3794 return err;
3795 }
3796
Ioana Radulescu227686b2018-07-27 09:12:59 -05003797 /* have the interface implicitly distribute traffic based on
3798 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003799 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05003800 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003801 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05003802 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003803
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003804 /* Configure the flow classification key; it includes all
3805 * supported header fields and cannot be modified at runtime
3806 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003807 err = dpaa2_eth_set_default_cls(priv);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003808 if (err && err != -EOPNOTSUPP)
3809 dev_err(dev, "Failed to configure Rx classification key\n");
3810
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003811 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05003812 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003813 err_cfg.set_frame_annotation = 1;
3814 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3815 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3816 &err_cfg);
3817 if (err) {
3818 dev_err(dev, "dpni_set_errors_behavior failed\n");
3819 return err;
3820 }
3821
3822 /* Configure Rx and Tx conf queues to generate CDANs */
3823 for (i = 0; i < priv->num_fqs; i++) {
3824 switch (priv->fq[i].type) {
3825 case DPAA2_RX_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003826 err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003827 break;
3828 case DPAA2_TX_CONF_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003829 err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003830 break;
Ioana Ciornei061d6312020-10-01 18:11:48 +03003831 case DPAA2_RX_ERR_FQ:
3832 err = setup_rx_err_flow(priv, &priv->fq[i]);
3833 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003834 default:
3835 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3836 return -EINVAL;
3837 }
3838 if (err)
3839 return err;
3840 }
3841
3842 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3843 DPNI_QUEUE_TX, &priv->tx_qdid);
3844 if (err) {
3845 dev_err(dev, "dpni_get_qdid() failed\n");
3846 return err;
3847 }
3848
3849 return 0;
3850}
3851
3852/* Allocate rings for storing incoming frame descriptors */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003853static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003854{
3855 struct net_device *net_dev = priv->net_dev;
3856 struct device *dev = net_dev->dev.parent;
3857 int i;
3858
3859 for (i = 0; i < priv->num_channels; i++) {
3860 priv->channel[i]->store =
3861 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3862 if (!priv->channel[i]->store) {
3863 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3864 goto err_ring;
3865 }
3866 }
3867
3868 return 0;
3869
3870err_ring:
3871 for (i = 0; i < priv->num_channels; i++) {
3872 if (!priv->channel[i]->store)
3873 break;
3874 dpaa2_io_store_destroy(priv->channel[i]->store);
3875 }
3876
3877 return -ENOMEM;
3878}
3879
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003880static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003881{
3882 int i;
3883
3884 for (i = 0; i < priv->num_channels; i++)
3885 dpaa2_io_store_destroy(priv->channel[i]->store);
3886}
3887
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003888static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003889{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003890 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003891 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003892 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003893 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003894
3895 /* Get firmware address, if any */
3896 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3897 if (err) {
3898 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3899 return err;
3900 }
3901
3902 /* Get DPNI attributes address, if any */
3903 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3904 dpni_mac_addr);
3905 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003906 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003907 return err;
3908 }
3909
3910 /* First check if firmware has any address configured by bootloader */
3911 if (!is_zero_ether_addr(mac_addr)) {
3912 /* If the DPMAC addr != DPNI addr, update it */
3913 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
3914 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
3915 priv->mc_token,
3916 mac_addr);
3917 if (err) {
3918 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3919 return err;
3920 }
3921 }
3922 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
3923 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003924 /* No MAC address configured, fill in net_dev->dev_addr
3925 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003926 */
3927 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003928 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
3929
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003930 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3931 net_dev->dev_addr);
3932 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003933 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003934 return err;
3935 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003936
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003937 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
3938 * practical purposes, this will be our "permanent" mac address,
3939 * at least until the next reboot. This move will also permit
3940 * register_netdevice() to properly fill up net_dev->perm_addr.
3941 */
3942 net_dev->addr_assign_type = NET_ADDR_PERM;
3943 } else {
3944 /* NET_ADDR_PERM is default, all we have to do is
3945 * fill in the device addr.
3946 */
3947 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
3948 }
3949
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003950 return 0;
3951}
3952
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003953static int dpaa2_eth_netdev_init(struct net_device *net_dev)
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003954{
3955 struct device *dev = net_dev->dev.parent;
3956 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003957 u32 options = priv->dpni_attrs.options;
3958 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003959 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003960 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003961 int err;
3962
3963 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003964 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003965
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003966 err = dpaa2_eth_set_mac_addr(priv);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003967 if (err)
3968 return err;
3969
3970 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003971 eth_broadcast_addr(bcast_addr);
3972 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3973 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003974 dev_err(dev, "dpni_add_mac_addr() failed\n");
3975 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003976 }
3977
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05003978 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003979 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05003980 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05003981 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05003982 if (err) {
3983 dev_err(dev, "dpni_set_max_frame_length() failed\n");
3984 return err;
3985 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003986
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003987 /* Set actual number of queues in the net device */
3988 num_queues = dpaa2_eth_queue_count(priv);
3989 err = netif_set_real_num_tx_queues(net_dev, num_queues);
3990 if (err) {
3991 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
3992 return err;
3993 }
3994 err = netif_set_real_num_rx_queues(net_dev, num_queues);
3995 if (err) {
3996 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
3997 return err;
3998 }
3999
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004000 /* Capabilities listing */
4001 supported |= IFF_LIVE_ADDR_CHANGE;
4002
4003 if (options & DPNI_OPT_NO_MAC_FILTER)
4004 not_supported |= IFF_UNICAST_FLT;
4005 else
4006 supported |= IFF_UNICAST_FLT;
4007
4008 net_dev->priv_flags |= supported;
4009 net_dev->priv_flags &= ~not_supported;
4010
4011 /* Features */
4012 net_dev->features = NETIF_F_RXCSUM |
4013 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4014 NETIF_F_SG | NETIF_F_HIGHDMA |
Ioana Ciornei3657cda2020-07-21 19:38:25 +03004015 NETIF_F_LLTX | NETIF_F_HW_TC;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004016 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004017
4018 return 0;
4019}
4020
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004021static int dpaa2_eth_poll_link_state(void *arg)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004022{
4023 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
4024 int err;
4025
4026 while (!kthread_should_stop()) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004027 err = dpaa2_eth_link_state_update(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004028 if (unlikely(err))
4029 return err;
4030
4031 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
4032 }
4033
4034 return 0;
4035}
4036
Ioana Ciornei71947922019-10-31 01:18:31 +02004037static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
4038{
4039 struct fsl_mc_device *dpni_dev, *dpmac_dev;
4040 struct dpaa2_mac *mac;
4041 int err;
4042
4043 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
4044 dpmac_dev = fsl_mc_get_endpoint(dpni_dev);
Ioana Ciornei841eb402020-07-14 15:08:16 +03004045 if (IS_ERR_OR_NULL(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
Ioana Ciornei71947922019-10-31 01:18:31 +02004046 return 0;
4047
4048 if (dpaa2_mac_is_type_fixed(dpmac_dev, priv->mc_io))
4049 return 0;
4050
4051 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
4052 if (!mac)
4053 return -ENOMEM;
4054
4055 mac->mc_dev = dpmac_dev;
4056 mac->mc_io = priv->mc_io;
4057 mac->net_dev = priv->net_dev;
4058
4059 err = dpaa2_mac_connect(mac);
4060 if (err) {
4061 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint\n");
4062 kfree(mac);
4063 return err;
4064 }
4065 priv->mac = mac;
4066
4067 return 0;
4068}
4069
4070static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
4071{
4072 if (!priv->mac)
4073 return;
4074
4075 dpaa2_mac_disconnect(priv->mac);
4076 kfree(priv->mac);
4077 priv->mac = NULL;
4078}
4079
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004080static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
4081{
Ioana Radulescu112197d2017-10-11 08:29:49 -05004082 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004083 struct device *dev = (struct device *)arg;
4084 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
4085 struct net_device *net_dev = dev_get_drvdata(dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02004086 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004087 int err;
4088
4089 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
4090 DPNI_IRQ_INDEX, &status);
4091 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004092 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05004093 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004094 }
4095
Ioana Radulescu112197d2017-10-11 08:29:49 -05004096 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004097 dpaa2_eth_link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004098
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02004099 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004100 dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
4101 dpaa2_eth_update_tx_fqids(priv);
Ioana Ciornei71947922019-10-31 01:18:31 +02004102
4103 rtnl_lock();
4104 if (priv->mac)
4105 dpaa2_eth_disconnect_mac(priv);
4106 else
4107 dpaa2_eth_connect_mac(priv);
4108 rtnl_unlock();
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02004109 }
Florin Chiculita8398b372019-10-16 10:36:22 +03004110
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004111 return IRQ_HANDLED;
4112}
4113
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004114static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004115{
4116 int err = 0;
4117 struct fsl_mc_device_irq *irq;
4118
4119 err = fsl_mc_allocate_irqs(ls_dev);
4120 if (err) {
4121 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4122 return err;
4123 }
4124
4125 irq = ls_dev->irqs[0];
4126 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05004127 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004128 IRQF_NO_SUSPEND | IRQF_ONESHOT,
4129 dev_name(&ls_dev->dev), &ls_dev->dev);
4130 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004131 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004132 goto free_mc_irq;
4133 }
4134
4135 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
Florin Chiculita8398b372019-10-16 10:36:22 +03004136 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4137 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004138 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004139 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004140 goto free_irq;
4141 }
4142
4143 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4144 DPNI_IRQ_INDEX, 1);
4145 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004146 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004147 goto free_irq;
4148 }
4149
4150 return 0;
4151
4152free_irq:
4153 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
4154free_mc_irq:
4155 fsl_mc_free_irqs(ls_dev);
4156
4157 return err;
4158}
4159
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004160static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004161{
4162 int i;
4163 struct dpaa2_eth_channel *ch;
4164
4165 for (i = 0; i < priv->num_channels; i++) {
4166 ch = priv->channel[i];
4167 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4168 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
4169 NAPI_POLL_WEIGHT);
4170 }
4171}
4172
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004173static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004174{
4175 int i;
4176 struct dpaa2_eth_channel *ch;
4177
4178 for (i = 0; i < priv->num_channels; i++) {
4179 ch = priv->channel[i];
4180 netif_napi_del(&ch->napi);
4181 }
4182}
4183
4184static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4185{
4186 struct device *dev;
4187 struct net_device *net_dev = NULL;
4188 struct dpaa2_eth_priv *priv = NULL;
4189 int err = 0;
4190
4191 dev = &dpni_dev->dev;
4192
4193 /* Net device */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03004194 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004195 if (!net_dev) {
4196 dev_err(dev, "alloc_etherdev_mq() failed\n");
4197 return -ENOMEM;
4198 }
4199
4200 SET_NETDEV_DEV(net_dev, dev);
4201 dev_set_drvdata(dev, net_dev);
4202
4203 priv = netdev_priv(net_dev);
4204 priv->net_dev = net_dev;
4205
Ioana Radulescu08eb2392017-05-24 07:13:27 -05004206 priv->iommu_domain = iommu_get_domain_for_dev(dev);
4207
Yangbo Lu1cf773b2020-09-18 17:08:01 +08004208 priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4209 priv->rx_tstamp = false;
4210
Yangbo Luc5521182020-09-18 17:08:02 +08004211 priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
4212 if (!priv->dpaa2_ptp_wq) {
4213 err = -ENOMEM;
4214 goto err_wq_alloc;
4215 }
4216
4217 INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4218
4219 skb_queue_head_init(&priv->tx_skbs);
4220
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004221 /* Obtain a MC portal */
4222 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4223 &priv->mc_io);
4224 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05004225 if (err == -ENXIO)
4226 err = -EPROBE_DEFER;
4227 else
4228 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004229 goto err_portal_alloc;
4230 }
4231
4232 /* MC objects initialization and configuration */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004233 err = dpaa2_eth_setup_dpni(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004234 if (err)
4235 goto err_dpni_setup;
4236
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004237 err = dpaa2_eth_setup_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004238 if (err)
4239 goto err_dpio_setup;
4240
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004241 dpaa2_eth_setup_fqs(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004242
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004243 err = dpaa2_eth_setup_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004244 if (err)
4245 goto err_dpbp_setup;
4246
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004247 err = dpaa2_eth_bind_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004248 if (err)
4249 goto err_bind;
4250
4251 /* Add a NAPI context for each channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004252 dpaa2_eth_add_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004253
4254 /* Percpu statistics */
4255 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4256 if (!priv->percpu_stats) {
4257 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4258 err = -ENOMEM;
4259 goto err_alloc_percpu_stats;
4260 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004261 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4262 if (!priv->percpu_extras) {
4263 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4264 err = -ENOMEM;
4265 goto err_alloc_percpu_extras;
4266 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004267
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004268 priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4269 if (!priv->sgt_cache) {
4270 dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4271 err = -ENOMEM;
4272 goto err_alloc_sgt_cache;
4273 }
4274
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004275 err = dpaa2_eth_netdev_init(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004276 if (err)
4277 goto err_netdev_init;
4278
4279 /* Configure checksum offload based on current interface flags */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004280 err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004281 if (err)
4282 goto err_csum;
4283
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004284 err = dpaa2_eth_set_tx_csum(priv,
4285 !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004286 if (err)
4287 goto err_csum;
4288
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004289 err = dpaa2_eth_alloc_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004290 if (err)
4291 goto err_alloc_rings;
4292
Ioana Ciorneif395b692020-05-31 00:08:13 +03004293#ifdef CONFIG_FSL_DPAA2_ETH_DCB
4294 if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4295 priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4296 net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4297 } else {
4298 dev_dbg(dev, "PFC not supported\n");
4299 }
4300#endif
4301
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004302 err = dpaa2_eth_setup_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004303 if (err) {
4304 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004305 priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004306 "%s_poll_link", net_dev->name);
4307 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004308 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004309 goto err_poll_thread;
4310 }
4311 priv->do_link_poll = true;
4312 }
4313
Ioana Ciornei71947922019-10-31 01:18:31 +02004314 err = dpaa2_eth_connect_mac(priv);
4315 if (err)
4316 goto err_connect_mac;
4317
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004318 err = dpaa2_eth_dl_register(priv);
4319 if (err)
4320 goto err_dl_register;
4321
Ioana Ciornei061d6312020-10-01 18:11:48 +03004322 err = dpaa2_eth_dl_traps_register(priv);
4323 if (err)
4324 goto err_dl_trap_register;
4325
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004326 err = dpaa2_eth_dl_port_add(priv);
4327 if (err)
4328 goto err_dl_port_add;
4329
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004330 err = register_netdev(net_dev);
4331 if (err < 0) {
4332 dev_err(dev, "register_netdev() failed\n");
4333 goto err_netdev_reg;
4334 }
4335
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004336#ifdef CONFIG_DEBUG_FS
4337 dpaa2_dbg_add(priv);
4338#endif
4339
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004340 dev_info(dev, "Probed interface %s\n", net_dev->name);
4341 return 0;
4342
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004343err_netdev_reg:
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004344 dpaa2_eth_dl_port_del(priv);
4345err_dl_port_add:
Ioana Ciornei061d6312020-10-01 18:11:48 +03004346 dpaa2_eth_dl_traps_unregister(priv);
4347err_dl_trap_register:
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004348 dpaa2_eth_dl_unregister(priv);
4349err_dl_register:
Ioana Ciornei71947922019-10-31 01:18:31 +02004350 dpaa2_eth_disconnect_mac(priv);
4351err_connect_mac:
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004352 if (priv->do_link_poll)
4353 kthread_stop(priv->poll_thread);
4354 else
4355 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004356err_poll_thread:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004357 dpaa2_eth_free_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004358err_alloc_rings:
4359err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004360err_netdev_init:
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004361 free_percpu(priv->sgt_cache);
4362err_alloc_sgt_cache:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004363 free_percpu(priv->percpu_extras);
4364err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004365 free_percpu(priv->percpu_stats);
4366err_alloc_percpu_stats:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004367 dpaa2_eth_del_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004368err_bind:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004369 dpaa2_eth_free_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004370err_dpbp_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004371 dpaa2_eth_free_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004372err_dpio_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004373 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004374err_dpni_setup:
4375 fsl_mc_portal_free(priv->mc_io);
4376err_portal_alloc:
Yangbo Luc5521182020-09-18 17:08:02 +08004377 destroy_workqueue(priv->dpaa2_ptp_wq);
4378err_wq_alloc:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004379 dev_set_drvdata(dev, NULL);
4380 free_netdev(net_dev);
4381
4382 return err;
4383}
4384
4385static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4386{
4387 struct device *dev;
4388 struct net_device *net_dev;
4389 struct dpaa2_eth_priv *priv;
4390
4391 dev = &ls_dev->dev;
4392 net_dev = dev_get_drvdata(dev);
4393 priv = netdev_priv(net_dev);
4394
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004395#ifdef CONFIG_DEBUG_FS
4396 dpaa2_dbg_remove(priv);
4397#endif
Ioana Ciornei71947922019-10-31 01:18:31 +02004398 rtnl_lock();
4399 dpaa2_eth_disconnect_mac(priv);
4400 rtnl_unlock();
4401
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004402 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004403
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004404 dpaa2_eth_dl_port_del(priv);
Ioana Ciornei061d6312020-10-01 18:11:48 +03004405 dpaa2_eth_dl_traps_unregister(priv);
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004406 dpaa2_eth_dl_unregister(priv);
4407
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004408 if (priv->do_link_poll)
4409 kthread_stop(priv->poll_thread);
4410 else
4411 fsl_mc_free_irqs(ls_dev);
4412
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004413 dpaa2_eth_free_rings(priv);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004414 free_percpu(priv->sgt_cache);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004415 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004416 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004417
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004418 dpaa2_eth_del_ch_napi(priv);
4419 dpaa2_eth_free_dpbp(priv);
4420 dpaa2_eth_free_dpio(priv);
4421 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004422
4423 fsl_mc_portal_free(priv->mc_io);
4424
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004425 free_netdev(net_dev);
4426
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05004427 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05004428
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004429 return 0;
4430}
4431
4432static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
4433 {
4434 .vendor = FSL_MC_VENDOR_FREESCALE,
4435 .obj_type = "dpni",
4436 },
4437 { .vendor = 0x0 }
4438};
4439MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
4440
4441static struct fsl_mc_driver dpaa2_eth_driver = {
4442 .driver = {
4443 .name = KBUILD_MODNAME,
4444 .owner = THIS_MODULE,
4445 },
4446 .probe = dpaa2_eth_probe,
4447 .remove = dpaa2_eth_remove,
4448 .match_id_table = dpaa2_eth_match_id_table
4449};
4450
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004451static int __init dpaa2_eth_driver_init(void)
4452{
4453 int err;
4454
4455 dpaa2_eth_dbg_init();
4456 err = fsl_mc_driver_register(&dpaa2_eth_driver);
4457 if (err) {
4458 dpaa2_eth_dbg_exit();
4459 return err;
4460 }
4461
4462 return 0;
4463}
4464
4465static void __exit dpaa2_eth_driver_exit(void)
4466{
4467 dpaa2_eth_dbg_exit();
4468 fsl_mc_driver_unregister(&dpaa2_eth_driver);
4469}
4470
4471module_init(dpaa2_eth_driver_init);
4472module_exit(dpaa2_eth_driver_exit);