blob: f917bc9c87969cc58abd190b1277f305ac0e82b6 [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);
Ioana Ciorneia5ff8b72021-02-11 21:51:22 +0200402 if (unlikely(err)) {
403 addr = dma_map_page(priv->net_dev->dev.parent,
404 virt_to_page(vaddr), 0,
405 priv->rx_buf_size, DMA_BIDIRECTIONAL);
406 if (unlikely(dma_mapping_error(priv->net_dev->dev.parent, addr))) {
407 free_pages((unsigned long)vaddr, 0);
408 } else {
409 ch->buf_count++;
410 dpaa2_eth_xdp_release_buf(priv, ch, addr);
411 }
Ioana Radulescud678be12019-03-01 17:47:24 +0000412 ch->stats.xdp_drop++;
Ioana Ciorneia5ff8b72021-02-11 21:51:22 +0200413 } else {
Ioana Radulescud678be12019-03-01 17:47:24 +0000414 ch->stats.xdp_redirect++;
Ioana Ciorneia5ff8b72021-02-11 21:51:22 +0200415 }
Ioana Radulescud678be12019-03-01 17:47:24 +0000416 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000417 }
418
Ioana Radulescud678be12019-03-01 17:47:24 +0000419 ch->xdp.res |= xdp_act;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000420out:
421 rcu_read_unlock();
422 return xdp_act;
423}
424
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500425/* Main Rx frame processing routine */
426static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
427 struct dpaa2_eth_channel *ch,
428 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000429 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500430{
431 dma_addr_t addr = dpaa2_fd_get_addr(fd);
432 u8 fd_format = dpaa2_fd_get_format(fd);
433 void *vaddr;
434 struct sk_buff *skb;
435 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500436 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500437 struct device *dev = priv->net_dev->dev.parent;
438 struct dpaa2_fas *fas;
Ioana Radulescud695e762017-06-06 10:00:35 -0500439 void *buf_data;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500440 u32 status = 0;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000441 u32 xdp_act;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500442
Ioana Radulescu56361872017-04-28 04:50:32 -0500443 /* Tracing point */
444 trace_dpaa2_rx_fd(priv->net_dev, fd);
445
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500446 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300447 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu18c2e772018-11-26 16:27:32 +0000448 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500449
Ioana Radulescu54ce8912017-12-08 06:47:53 -0600450 fas = dpaa2_get_fas(vaddr, false);
Ioana Radulescud695e762017-06-06 10:00:35 -0500451 prefetch(fas);
452 buf_data = vaddr + dpaa2_fd_get_offset(fd);
453 prefetch(buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500454
455 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500456 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500457
458 if (fd_format == dpaa2_fd_single) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300459 xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000460 if (xdp_act != XDP_PASS) {
461 percpu_stats->rx_packets++;
462 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
463 return;
464 }
465
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300466 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000467 DMA_BIDIRECTIONAL);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300468 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500469 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000470 WARN_ON(priv->xdp_prog);
471
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300472 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000473 DMA_BIDIRECTIONAL);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300474 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000475 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500476 percpu_extras->rx_sg_frames++;
477 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500478 } else {
479 /* We don't support any other format */
480 goto err_frame_format;
481 }
482
483 if (unlikely(!skb))
484 goto err_build_skb;
485
486 prefetch(skb->data);
487
Ioana Radulescu859f9982018-04-26 18:23:47 +0800488 /* Get the timestamp value */
489 if (priv->rx_tstamp) {
490 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
491 __le64 *ts = dpaa2_get_ts(vaddr, false);
492 u64 ns;
493
494 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
495
496 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
497 shhwtstamps->hwtstamp = ns_to_ktime(ns);
498 }
499
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500500 /* Check if we need to validate the L4 csum */
501 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500502 status = le32_to_cpu(fas->status);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300503 dpaa2_eth_validate_rx_csum(priv, status, skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500504 }
505
506 skb->protocol = eth_type_trans(skb, priv->net_dev);
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000507 skb_record_rx_queue(skb, fq->flowid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500508
509 percpu_stats->rx_packets++;
510 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
511
Ioana Ciornei0a25d922019-03-25 13:42:39 +0000512 list_add_tail(&skb->list, ch->rx_list);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500513
514 return;
515
516err_build_skb:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300517 dpaa2_eth_free_rx_fd(priv, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500518err_frame_format:
519 percpu_stats->rx_dropped++;
520}
521
Ioana Ciornei061d6312020-10-01 18:11:48 +0300522/* Processing of Rx frames received on the error FQ
523 * We check and print the error bits and then free the frame
524 */
525static void dpaa2_eth_rx_err(struct dpaa2_eth_priv *priv,
526 struct dpaa2_eth_channel *ch,
527 const struct dpaa2_fd *fd,
528 struct dpaa2_eth_fq *fq __always_unused)
529{
530 struct device *dev = priv->net_dev->dev.parent;
531 dma_addr_t addr = dpaa2_fd_get_addr(fd);
532 u8 fd_format = dpaa2_fd_get_format(fd);
533 struct rtnl_link_stats64 *percpu_stats;
534 struct dpaa2_eth_trap_item *trap_item;
535 struct dpaa2_fapr *fapr;
536 struct sk_buff *skb;
537 void *buf_data;
538 void *vaddr;
539
540 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
541 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
542 DMA_BIDIRECTIONAL);
543
544 buf_data = vaddr + dpaa2_fd_get_offset(fd);
545
546 if (fd_format == dpaa2_fd_single) {
547 dma_unmap_page(dev, addr, priv->rx_buf_size,
548 DMA_BIDIRECTIONAL);
549 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
550 } else if (fd_format == dpaa2_fd_sg) {
551 dma_unmap_page(dev, addr, priv->rx_buf_size,
552 DMA_BIDIRECTIONAL);
553 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
554 free_pages((unsigned long)vaddr, 0);
555 } else {
556 /* We don't support any other format */
557 dpaa2_eth_free_rx_fd(priv, fd, vaddr);
558 goto err_frame_format;
559 }
560
561 fapr = dpaa2_get_fapr(vaddr, false);
562 trap_item = dpaa2_eth_dl_get_trap(priv, fapr);
563 if (trap_item)
564 devlink_trap_report(priv->devlink, skb, trap_item->trap_ctx,
565 &priv->devlink_port, NULL);
566 consume_skb(skb);
567
568err_frame_format:
569 percpu_stats = this_cpu_ptr(priv->percpu_stats);
570 percpu_stats->rx_errors++;
571 ch->buf_count--;
572}
573
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500574/* Consume all frames pull-dequeued into the store. This is the simplest way to
575 * make sure we don't accidentally issue another volatile dequeue which would
576 * overwrite (leak) frames already in the store.
577 *
578 * Observance of NAPI budget is not our concern, leaving that to the caller.
579 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300580static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
581 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500582{
583 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000584 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500585 struct dpaa2_dq *dq;
586 const struct dpaa2_fd *fd;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300587 int cleaned = 0, retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500588 int is_last;
589
590 do {
591 dq = dpaa2_io_store_next(ch->store, &is_last);
592 if (unlikely(!dq)) {
593 /* If we're here, we *must* have placed a
594 * volatile dequeue comnmand, so keep reading through
595 * the store until we get some sort of valid response
596 * token (either a valid frame or an "empty dequeue")
597 */
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300598 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
599 netdev_err_once(priv->net_dev,
600 "Unable to read a valid dequeue response\n");
601 return -ETIMEDOUT;
602 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500603 continue;
604 }
605
606 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600607 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500608
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000609 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500610 cleaned++;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300611 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500612 } while (!is_last);
613
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000614 if (!cleaned)
615 return 0;
616
617 fq->stats.frames += cleaned;
Ioana Ciornei460fd832020-04-24 12:33:18 +0300618 ch->stats.frames += cleaned;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000619
620 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000621 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000622 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000623 if (src)
624 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000625
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500626 return cleaned;
627}
628
Yangbo Luc5521182020-09-18 17:08:02 +0800629static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
630 u8 *msgtype, u8 *twostep, u8 *udp,
631 u16 *correction_offset,
632 u16 *origintimestamp_offset)
Ioana Radulescu859f9982018-04-26 18:23:47 +0800633{
Yangbo Luc5521182020-09-18 17:08:02 +0800634 unsigned int ptp_class;
635 struct ptp_header *hdr;
636 unsigned int type;
637 u8 *base;
638
639 ptp_class = ptp_classify_raw(skb);
640 if (ptp_class == PTP_CLASS_NONE)
641 return -EINVAL;
642
643 hdr = ptp_parse_header(skb, ptp_class);
644 if (!hdr)
645 return -EINVAL;
646
647 *msgtype = ptp_get_msgtype(hdr, ptp_class);
648 *twostep = hdr->flag_field[0] & 0x2;
649
650 type = ptp_class & PTP_CLASS_PMASK;
651 if (type == PTP_CLASS_IPV4 ||
652 type == PTP_CLASS_IPV6)
653 *udp = 1;
654 else
655 *udp = 0;
656
657 base = skb_mac_header(skb);
658 *correction_offset = (u8 *)&hdr->correction - base;
659 *origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
660
661 return 0;
662}
663
664/* Configure the egress frame annotation for timestamp update */
665static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
666 struct dpaa2_fd *fd,
667 void *buf_start,
668 struct sk_buff *skb)
669{
670 struct ptp_tstamp origin_timestamp;
671 struct dpni_single_step_cfg cfg;
672 u8 msgtype, twostep, udp;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800673 struct dpaa2_faead *faead;
Yangbo Luc5521182020-09-18 17:08:02 +0800674 struct dpaa2_fas *fas;
675 struct timespec64 ts;
676 u16 offset1, offset2;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800677 u32 ctrl, frc;
Yangbo Luc5521182020-09-18 17:08:02 +0800678 __le64 *ns;
679 u8 *data;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800680
681 /* Mark the egress frame annotation area as valid */
682 frc = dpaa2_fd_get_frc(fd);
683 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
684
685 /* Set hardware annotation size */
686 ctrl = dpaa2_fd_get_ctrl(fd);
687 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
688
689 /* enable UPD (update prepanded data) bit in FAEAD field of
690 * hardware frame annotation area
691 */
692 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
693 faead = dpaa2_get_faead(buf_start, true);
694 faead->ctrl = cpu_to_le32(ctrl);
Yangbo Luc5521182020-09-18 17:08:02 +0800695
696 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
697 if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
698 &offset1, &offset2) ||
699 msgtype != 0 || twostep) {
700 WARN_ONCE(1, "Bad packet for one-step timestamping\n");
701 return;
702 }
703
704 /* Mark the frame annotation status as valid */
705 frc = dpaa2_fd_get_frc(fd);
706 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);
707
708 /* Mark the PTP flag for one step timestamping */
709 fas = dpaa2_get_fas(buf_start, true);
710 fas->status = cpu_to_le32(DPAA2_FAS_PTP);
711
712 dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
713 ns = dpaa2_get_ts(buf_start, true);
714 *ns = cpu_to_le64(timespec64_to_ns(&ts) /
715 DPAA2_PTP_CLK_PERIOD_NS);
716
717 /* Update current time to PTP message originTimestamp field */
718 ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
719 data = skb_mac_header(skb);
720 *(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
721 *(__be32 *)(data + offset2 + 2) =
722 htonl(origin_timestamp.sec_lsb);
723 *(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);
724
725 cfg.en = 1;
726 cfg.ch_update = udp;
727 cfg.offset = offset1;
728 cfg.peer_delay = 0;
729
730 if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token,
731 &cfg))
732 WARN_ONCE(1, "Failed to set single step register");
733 }
Ioana Radulescu859f9982018-04-26 18:23:47 +0800734}
735
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500736/* Create a frame descriptor based on a fragmented skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300737static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
738 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800739 struct dpaa2_fd *fd,
740 void **swa_addr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500741{
742 struct device *dev = priv->net_dev->dev.parent;
743 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500744 dma_addr_t addr;
745 int nr_frags = skb_shinfo(skb)->nr_frags;
746 struct dpaa2_sg_entry *sgt;
747 int i, err;
748 int sgt_buf_size;
749 struct scatterlist *scl, *crt_scl;
750 int num_sg;
751 int num_dma_bufs;
752 struct dpaa2_eth_swa *swa;
753
754 /* Create and map scatterlist.
755 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
756 * to go beyond nr_frags+1.
757 * Note: We don't support chained scatterlists
758 */
759 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
760 return -EINVAL;
761
Julia Lawalld4ceb8d2020-09-20 13:26:15 +0200762 scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500763 if (unlikely(!scl))
764 return -ENOMEM;
765
766 sg_init_table(scl, nr_frags + 1);
767 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Ciornei37fbbdda2020-06-24 14:34:18 +0300768 if (unlikely(num_sg < 0)) {
769 err = -ENOMEM;
770 goto dma_map_sg_failed;
771 }
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500772 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500773 if (unlikely(!num_dma_bufs)) {
774 err = -ENOMEM;
775 goto dma_map_sg_failed;
776 }
777
778 /* Prepare the HW SGT structure */
779 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500780 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Sebastian Andrzej Siewior90bc6d42019-06-07 21:20:37 +0200781 sgt_buf = napi_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500782 if (unlikely(!sgt_buf)) {
783 err = -ENOMEM;
784 goto sgt_buf_alloc_failed;
785 }
786 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500787 memset(sgt_buf, 0, sgt_buf_size);
788
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500789 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
790
791 /* Fill in the HW SGT structure.
792 *
793 * sgt_buf is zeroed out, so the following fields are implicit
794 * in all sgt entries:
795 * - offset is 0
796 * - format is 'dpaa2_sg_single'
797 */
798 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
799 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
800 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
801 }
802 dpaa2_sg_set_final(&sgt[i - 1], true);
803
804 /* Store the skb backpointer in the SGT buffer.
805 * Fit the scatterlist and the number of buffers alongside the
806 * skb backpointer in the software annotation area. We'll need
807 * all of them on Tx Conf.
808 */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800809 *swa_addr = (void *)sgt_buf;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500810 swa = (struct dpaa2_eth_swa *)sgt_buf;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000811 swa->type = DPAA2_ETH_SWA_SG;
812 swa->sg.skb = skb;
813 swa->sg.scl = scl;
814 swa->sg.num_sg = num_sg;
815 swa->sg.sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500816
817 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500818 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500819 if (unlikely(dma_mapping_error(dev, addr))) {
820 err = -ENOMEM;
821 goto dma_map_single_failed;
822 }
823 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
824 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
825 dpaa2_fd_set_addr(fd, addr);
826 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000827 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500828
829 return 0;
830
831dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500832 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500833sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500834 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500835dma_map_sg_failed:
836 kfree(scl);
837 return err;
838}
839
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300840/* Create a SG frame descriptor based on a linear skb.
841 *
842 * This function is used on the Tx path when the skb headroom is not large
843 * enough for the HW requirements, thus instead of realloc-ing the skb we
844 * create a SG frame descriptor with only one entry.
845 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300846static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
847 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800848 struct dpaa2_fd *fd,
849 void **swa_addr)
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300850{
851 struct device *dev = priv->net_dev->dev.parent;
852 struct dpaa2_eth_sgt_cache *sgt_cache;
853 struct dpaa2_sg_entry *sgt;
854 struct dpaa2_eth_swa *swa;
855 dma_addr_t addr, sgt_addr;
856 void *sgt_buf = NULL;
857 int sgt_buf_size;
858 int err;
859
860 /* Prepare the HW SGT structure */
861 sgt_cache = this_cpu_ptr(priv->sgt_cache);
862 sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
863
864 if (sgt_cache->count == 0)
865 sgt_buf = kzalloc(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN,
866 GFP_ATOMIC);
867 else
868 sgt_buf = sgt_cache->buf[--sgt_cache->count];
869 if (unlikely(!sgt_buf))
870 return -ENOMEM;
871
872 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
873 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
874
875 addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
876 if (unlikely(dma_mapping_error(dev, addr))) {
877 err = -ENOMEM;
878 goto data_map_failed;
879 }
880
881 /* Fill in the HW SGT structure */
882 dpaa2_sg_set_addr(sgt, addr);
883 dpaa2_sg_set_len(sgt, skb->len);
884 dpaa2_sg_set_final(sgt, true);
885
886 /* Store the skb backpointer in the SGT buffer */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800887 *swa_addr = (void *)sgt_buf;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300888 swa = (struct dpaa2_eth_swa *)sgt_buf;
889 swa->type = DPAA2_ETH_SWA_SINGLE;
890 swa->single.skb = skb;
Ioana Ciornei5c0109f2020-12-11 19:16:07 +0200891 swa->single.sgt_size = sgt_buf_size;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300892
893 /* Separately map the SGT buffer */
894 sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
895 if (unlikely(dma_mapping_error(dev, sgt_addr))) {
896 err = -ENOMEM;
897 goto sgt_map_failed;
898 }
899
900 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
901 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
902 dpaa2_fd_set_addr(fd, sgt_addr);
903 dpaa2_fd_set_len(fd, skb->len);
904 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
905
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300906 return 0;
907
908sgt_map_failed:
909 dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
910data_map_failed:
911 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
912 kfree(sgt_buf);
913 else
914 sgt_cache->buf[sgt_cache->count++] = sgt_buf;
915
916 return err;
917}
918
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500919/* Create a frame descriptor based on a linear skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300920static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
921 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800922 struct dpaa2_fd *fd,
923 void **swa_addr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500924{
925 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600926 u8 *buffer_start, *aligned_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000927 struct dpaa2_eth_swa *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500928 dma_addr_t addr;
929
Yangbo Lu1cf773b2020-09-18 17:08:01 +0800930 buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
Ioana Radulescuc1636852017-12-08 06:47:58 -0600931
932 /* If there's enough room to align the FD address, do it.
933 * It will help hardware optimize accesses.
934 */
935 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
936 DPAA2_ETH_TX_BUF_ALIGN);
937 if (aligned_start >= skb->head)
938 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500939
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500940 /* Store a backpointer to the skb at the beginning of the buffer
941 * (in the private data area) such that we can release it
942 * on Tx confirm
943 */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800944 *swa_addr = (void *)buffer_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000945 swa = (struct dpaa2_eth_swa *)buffer_start;
946 swa->type = DPAA2_ETH_SWA_SINGLE;
947 swa->single.skb = skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500948
949 addr = dma_map_single(dev, buffer_start,
950 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500951 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500952 if (unlikely(dma_mapping_error(dev, addr)))
953 return -ENOMEM;
954
955 dpaa2_fd_set_addr(fd, addr);
956 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
957 dpaa2_fd_set_len(fd, skb->len);
958 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000959 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500960
961 return 0;
962}
963
964/* FD freeing routine on the Tx path
965 *
966 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
967 * back-pointed to is also freed.
968 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
969 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500970 */
Yangbo Luc5521182020-09-18 17:08:02 +0800971static void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300972 struct dpaa2_eth_fq *fq,
973 const struct dpaa2_fd *fd, bool in_napi)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500974{
975 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300976 dma_addr_t fd_addr, sg_addr;
Ioana Radulescud678be12019-03-01 17:47:24 +0000977 struct sk_buff *skb = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500978 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500979 struct dpaa2_eth_swa *swa;
980 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescud678be12019-03-01 17:47:24 +0000981 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500982
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300983 struct dpaa2_eth_sgt_cache *sgt_cache;
984 struct dpaa2_sg_entry *sgt;
985
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500986 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000987 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
988 swa = (struct dpaa2_eth_swa *)buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500989
990 if (fd_format == dpaa2_fd_single) {
Ioana Radulescud678be12019-03-01 17:47:24 +0000991 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
992 skb = swa->single.skb;
993 /* Accessing the skb buffer is safe before dma unmap,
994 * because we didn't map the actual skb shell.
995 */
996 dma_unmap_single(dev, fd_addr,
997 skb_tail_pointer(skb) - buffer_start,
998 DMA_BIDIRECTIONAL);
999 } else {
1000 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
1001 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
1002 DMA_BIDIRECTIONAL);
1003 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001004 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001005 if (swa->type == DPAA2_ETH_SWA_SG) {
1006 skb = swa->sg.skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001007
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001008 /* Unmap the scatterlist */
1009 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
1010 DMA_BIDIRECTIONAL);
1011 kfree(swa->sg.scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001012
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001013 /* Unmap the SGT buffer */
1014 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
1015 DMA_BIDIRECTIONAL);
1016 } else {
1017 skb = swa->single.skb;
1018
1019 /* Unmap the SGT Buffer */
1020 dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
1021 DMA_BIDIRECTIONAL);
1022
1023 sgt = (struct dpaa2_sg_entry *)(buffer_start +
1024 priv->tx_data_offset);
1025 sg_addr = dpaa2_sg_get_addr(sgt);
1026 dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
1027 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001028 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06001029 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001030 return;
1031 }
1032
Ioana Radulescud678be12019-03-01 17:47:24 +00001033 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
1034 fq->dq_frames++;
1035 fq->dq_bytes += fd_len;
1036 }
1037
1038 if (swa->type == DPAA2_ETH_SWA_XDP) {
1039 xdp_return_frame(swa->xdp.xdpf);
1040 return;
1041 }
1042
Ioana Radulescu859f9982018-04-26 18:23:47 +08001043 /* Get the timestamp value */
Yangbo Lu1cf773b2020-09-18 17:08:01 +08001044 if (skb->cb[0] == TX_TSTAMP) {
Ioana Radulescu859f9982018-04-26 18:23:47 +08001045 struct skb_shared_hwtstamps shhwtstamps;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +00001046 __le64 *ts = dpaa2_get_ts(buffer_start, true);
Ioana Radulescu859f9982018-04-26 18:23:47 +08001047 u64 ns;
1048
1049 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1050
1051 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
1052 shhwtstamps.hwtstamp = ns_to_ktime(ns);
1053 skb_tstamp_tx(skb, &shhwtstamps);
Yangbo Luc5521182020-09-18 17:08:02 +08001054 } else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1055 mutex_unlock(&priv->onestep_tstamp_lock);
Ioana Radulescu859f9982018-04-26 18:23:47 +08001056 }
1057
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -05001058 /* Free SGT buffer allocated on tx */
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001059 if (fd_format != dpaa2_fd_single) {
1060 sgt_cache = this_cpu_ptr(priv->sgt_cache);
1061 if (swa->type == DPAA2_ETH_SWA_SG) {
1062 skb_free_frag(buffer_start);
1063 } else {
1064 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
1065 kfree(buffer_start);
1066 else
1067 sgt_cache->buf[sgt_cache->count++] = buffer_start;
1068 }
1069 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001070
1071 /* Move on with skb release */
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +00001072 napi_consume_skb(skb, in_napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001073}
1074
Yangbo Luc5521182020-09-18 17:08:02 +08001075static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1076 struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001077{
1078 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1079 struct dpaa2_fd fd;
1080 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001081 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001082 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001083 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001084 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -06001085 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001086 u32 fd_len;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001087 u8 prio = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001088 int err, i;
Yangbo Lu64a965d2020-09-18 17:08:00 +08001089 void *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001090
1091 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001092 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001093
Yangbo Lu1cf773b2020-09-18 17:08:01 +08001094 needed_headroom = dpaa2_eth_needed_headroom(skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001095
1096 /* We'll be holding a back-reference to the skb until Tx Confirmation;
1097 * we don't want that overwritten by a concurrent Tx with a cloned skb.
1098 */
1099 skb = skb_unshare(skb, GFP_ATOMIC);
1100 if (unlikely(!skb)) {
1101 /* skb_unshare() has already freed the skb */
1102 percpu_stats->tx_dropped++;
1103 return NETDEV_TX_OK;
1104 }
1105
1106 /* Setup the FD fields */
1107 memset(&fd, 0, sizeof(fd));
1108
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001109 if (skb_is_nonlinear(skb)) {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001110 err = dpaa2_eth_build_sg_fd(priv, skb, &fd, &swa);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001111 percpu_extras->tx_sg_frames++;
1112 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001113 } else if (skb_headroom(skb) < needed_headroom) {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001114 err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, &fd, &swa);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001115 percpu_extras->tx_sg_frames++;
1116 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciornei4c96c0a2020-06-29 21:47:12 +03001117 percpu_extras->tx_converted_sg_frames++;
1118 percpu_extras->tx_converted_sg_bytes += skb->len;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001119 } else {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001120 err = dpaa2_eth_build_single_fd(priv, skb, &fd, &swa);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001121 }
1122
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001123 if (unlikely(err)) {
1124 percpu_stats->tx_dropped++;
1125 goto err_build_fd;
1126 }
1127
Yangbo Luc5521182020-09-18 17:08:02 +08001128 if (skb->cb[0])
1129 dpaa2_eth_enable_tx_tstamp(priv, &fd, swa, skb);
Yangbo Lu64a965d2020-09-18 17:08:00 +08001130
Ioana Radulescu56361872017-04-28 04:50:32 -05001131 /* Tracing point */
1132 trace_dpaa2_tx_fd(net_dev, &fd);
1133
Ioana Radulescu537336c2017-12-21 06:33:20 -06001134 /* TxConf FQ selection relies on queue id from the stack.
1135 * In case of a forwarded frame from another DPNI interface, we choose
1136 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001137 */
Ioana Radulescu537336c2017-12-21 06:33:20 -06001138 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001139
1140 if (net_dev->num_tc) {
1141 prio = netdev_txq_to_tc(net_dev, queue_mapping);
1142 /* Hardware interprets priority level 0 as being the highest,
1143 * so we need to do a reverse mapping to the netdev tc index
1144 */
1145 prio = net_dev->num_tc - prio - 1;
1146 /* We have only one FQ array entry for all Tx hardware queues
1147 * with the same flow id (but different priority levels)
1148 */
1149 queue_mapping %= dpaa2_eth_queue_count(priv);
1150 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001151 fq = &priv->fq[queue_mapping];
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001152
1153 fd_len = dpaa2_fd_get_len(&fd);
1154 nq = netdev_get_tx_queue(net_dev, queue_mapping);
1155 netdev_tx_sent_queue(nq, fd_len);
1156
1157 /* Everything that happens after this enqueues might race with
1158 * the Tx confirmation callback for this frame
1159 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001160 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Ciornei6ff80442020-04-22 15:05:11 +03001161 err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001162 if (err != -EBUSY)
1163 break;
1164 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001165 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001166 if (unlikely(err < 0)) {
1167 percpu_stats->tx_errors++;
1168 /* Clean up everything, including freeing the skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001169 dpaa2_eth_free_tx_fd(priv, fq, &fd, false);
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001170 netdev_tx_completed_queue(nq, 1, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001171 } else {
1172 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001173 percpu_stats->tx_bytes += fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001174 }
1175
1176 return NETDEV_TX_OK;
1177
1178err_build_fd:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001179 dev_kfree_skb(skb);
1180
1181 return NETDEV_TX_OK;
1182}
1183
Yangbo Luc5521182020-09-18 17:08:02 +08001184static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1185{
1186 struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1187 tx_onestep_tstamp);
1188 struct sk_buff *skb;
1189
1190 while (true) {
1191 skb = skb_dequeue(&priv->tx_skbs);
1192 if (!skb)
1193 return;
1194
1195 /* Lock just before TX one-step timestamping packet,
1196 * and release the lock in dpaa2_eth_free_tx_fd when
1197 * confirm the packet has been sent on hardware, or
1198 * when clean up during transmit failure.
1199 */
1200 mutex_lock(&priv->onestep_tstamp_lock);
1201 __dpaa2_eth_tx(skb, priv->net_dev);
1202 }
1203}
1204
1205static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1206{
1207 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1208 u8 msgtype, twostep, udp;
1209 u16 offset1, offset2;
1210
1211 /* Utilize skb->cb[0] for timestamping request per skb */
1212 skb->cb[0] = 0;
1213
1214 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1215 if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1216 skb->cb[0] = TX_TSTAMP;
1217 else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1218 skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1219 }
1220
1221 /* TX for one-step timestamping PTP Sync packet */
1222 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1223 if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1224 &offset1, &offset2))
1225 if (msgtype == 0 && twostep == 0) {
1226 skb_queue_tail(&priv->tx_skbs, skb);
1227 queue_work(priv->dpaa2_ptp_wq,
1228 &priv->tx_onestep_tstamp);
1229 return NETDEV_TX_OK;
1230 }
1231 /* Use two-step timestamping if not one-step timestamping
1232 * PTP Sync packet
1233 */
1234 skb->cb[0] = TX_TSTAMP;
1235 }
1236
1237 /* TX for other packets */
1238 return __dpaa2_eth_tx(skb, net_dev);
1239}
1240
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001241/* Tx confirmation frame processing routine */
1242static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +00001243 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001244 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001245 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001246{
1247 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001248 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001249 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001250 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001251
Ioana Radulescu56361872017-04-28 04:50:32 -05001252 /* Tracing point */
1253 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1254
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001255 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1256 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001257 percpu_extras->tx_conf_bytes += fd_len;
1258
Ioana Radulescu39163c02017-06-06 10:00:39 -05001259 /* Check frame errors in the FD field */
1260 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001261 dpaa2_eth_free_tx_fd(priv, fq, fd, true);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001262
1263 if (likely(!fd_errors))
1264 return;
1265
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06001266 if (net_ratelimit())
1267 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1268 fd_errors);
1269
Ioana Radulescu39163c02017-06-06 10:00:39 -05001270 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1271 /* Tx-conf logically pertains to the egress path. */
1272 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001273}
1274
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001275static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001276{
1277 int err;
1278
1279 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1280 DPNI_OFF_RX_L3_CSUM, enable);
1281 if (err) {
1282 netdev_err(priv->net_dev,
1283 "dpni_set_offload(RX_L3_CSUM) failed\n");
1284 return err;
1285 }
1286
1287 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1288 DPNI_OFF_RX_L4_CSUM, enable);
1289 if (err) {
1290 netdev_err(priv->net_dev,
1291 "dpni_set_offload(RX_L4_CSUM) failed\n");
1292 return err;
1293 }
1294
1295 return 0;
1296}
1297
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001298static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001299{
1300 int err;
1301
1302 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1303 DPNI_OFF_TX_L3_CSUM, enable);
1304 if (err) {
1305 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1306 return err;
1307 }
1308
1309 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1310 DPNI_OFF_TX_L4_CSUM, enable);
1311 if (err) {
1312 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1313 return err;
1314 }
1315
1316 return 0;
1317}
1318
1319/* Perform a single release command to add buffers
1320 * to the specified buffer pool
1321 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001322static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1323 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001324{
1325 struct device *dev = priv->net_dev->dev.parent;
1326 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001327 struct page *page;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001328 dma_addr_t addr;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001329 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001330 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001331
1332 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1333 /* Allocate buffer visible to WRIOP + skb shared info +
1334 * alignment padding
1335 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001336 /* allocate one page for each Rx buffer. WRIOP sees
1337 * the entire page except for a tailroom reserved for
1338 * skb shared info
1339 */
1340 page = dev_alloc_pages(0);
1341 if (!page)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001342 goto err_alloc;
1343
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001344 addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001345 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001346 if (unlikely(dma_mapping_error(dev, addr)))
1347 goto err_map;
1348
1349 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -05001350
1351 /* tracing point */
1352 trace_dpaa2_eth_buf_seed(priv->net_dev,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001353 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001354 addr, priv->rx_buf_size,
Ioana Radulescu56361872017-04-28 04:50:32 -05001355 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001356 }
1357
1358release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001359 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001360 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001361 buf_array, i)) == -EBUSY) {
1362 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1363 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001364 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001365 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001366
1367 /* If release command failed, clean up and bail out;
1368 * not much else we can do about it
1369 */
1370 if (err) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001371 dpaa2_eth_free_bufs(priv, buf_array, i);
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001372 return 0;
1373 }
1374
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001375 return i;
1376
1377err_map:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001378 __free_pages(page, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001379err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001380 /* If we managed to allocate at least some buffers,
1381 * release them to hardware
1382 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001383 if (i)
1384 goto release_bufs;
1385
1386 return 0;
1387}
1388
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001389static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001390{
1391 int i, j;
1392 int new_count;
1393
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001394 for (j = 0; j < priv->num_channels; j++) {
1395 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1396 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001397 new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001398 priv->channel[j]->buf_count += new_count;
1399
1400 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001401 return -ENOMEM;
1402 }
1403 }
1404 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001405
1406 return 0;
1407}
1408
Jesse Brandeburgd0ea5cb2020-09-25 15:24:45 -07001409/*
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001410 * Drain the specified number of buffers from the DPNI's private buffer pool.
1411 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1412 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001413static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001414{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001415 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001416 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001417 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001418
1419 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001420 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001421 buf_array, count);
1422 if (ret < 0) {
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001423 if (ret == -EBUSY &&
Ioana Ciornei0e5ad752020-06-24 14:34:19 +03001424 retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001425 continue;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001426 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1427 return;
1428 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001429 dpaa2_eth_free_bufs(priv, buf_array, ret);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001430 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001431 } while (ret);
1432}
1433
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001434static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001435{
1436 int i;
1437
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001438 dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1439 dpaa2_eth_drain_bufs(priv, 1);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001440
1441 for (i = 0; i < priv->num_channels; i++)
1442 priv->channel[i]->buf_count = 0;
1443}
1444
1445/* Function is called from softirq context only, so we don't need to guard
1446 * the access to percpu count
1447 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001448static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1449 struct dpaa2_eth_channel *ch,
1450 u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001451{
1452 int new_count;
1453
1454 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1455 return 0;
1456
1457 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001458 new_count = dpaa2_eth_add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001459 if (unlikely(!new_count)) {
1460 /* Out of memory; abort for now, we'll try later on */
1461 break;
1462 }
1463 ch->buf_count += new_count;
1464 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1465
1466 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1467 return -ENOMEM;
1468
1469 return 0;
1470}
1471
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001472static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1473{
1474 struct dpaa2_eth_sgt_cache *sgt_cache;
1475 u16 count;
1476 int k, i;
1477
Ioana Ciornei0fe665d2020-07-06 17:55:54 +03001478 for_each_possible_cpu(k) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001479 sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1480 count = sgt_cache->count;
1481
1482 for (i = 0; i < count; i++)
1483 kfree(sgt_cache->buf[i]);
1484 sgt_cache->count = 0;
1485 }
1486}
1487
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001488static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001489{
1490 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001491 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001492
1493 /* Retry while portal is busy */
1494 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001495 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1496 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001497 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001498 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001499 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001500
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001501 ch->stats.dequeue_portal_busy += dequeues;
1502 if (unlikely(err))
1503 ch->stats.pull_err++;
1504
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001505 return err;
1506}
1507
1508/* NAPI poll routine
1509 *
1510 * Frames are dequeued from the QMan channel associated with this NAPI context.
1511 * Rx, Tx confirmation and (if configured) Rx error frames all count
1512 * towards the NAPI budget.
1513 */
1514static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1515{
1516 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001517 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001518 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001519 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1520 struct netdev_queue *nq;
1521 int store_cleaned, work_done;
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001522 struct list_head rx_list;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001523 int retries = 0;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001524 u16 flowid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001525 int err;
1526
1527 ch = container_of(napi, struct dpaa2_eth_channel, napi);
Ioana Radulescud678be12019-03-01 17:47:24 +00001528 ch->xdp.res = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001529 priv = ch->priv;
1530
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001531 INIT_LIST_HEAD(&rx_list);
1532 ch->rx_list = &rx_list;
1533
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001534 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001535 err = dpaa2_eth_pull_channel(ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001536 if (unlikely(err))
1537 break;
1538
1539 /* Refill pool if appropriate */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001540 dpaa2_eth_refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001541
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001542 store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001543 if (store_cleaned <= 0)
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001544 break;
1545 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001546 rx_cleaned += store_cleaned;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001547 flowid = fq->flowid;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001548 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001549 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001550 /* We have a single Tx conf FQ on this channel */
1551 txc_fq = fq;
1552 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001553
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001554 /* If we either consumed the whole NAPI budget with Rx frames
1555 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001556 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001557 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001558 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1559 work_done = budget;
1560 goto out;
1561 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001562 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001563
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001564 /* We didn't consume the entire budget, so finish napi and
1565 * re-enable data availability notifications
1566 */
1567 napi_complete_done(napi, rx_cleaned);
1568 do {
1569 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1570 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001571 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001572 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1573 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001574
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001575 work_done = max(rx_cleaned, 1);
1576
1577out:
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001578 netif_receive_skb_list(ch->rx_list);
1579
Ioana Radulescud678be12019-03-01 17:47:24 +00001580 if (txc_fq && txc_fq->dq_frames) {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001581 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1582 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1583 txc_fq->dq_bytes);
1584 txc_fq->dq_frames = 0;
1585 txc_fq->dq_bytes = 0;
1586 }
1587
Ioana Radulescud678be12019-03-01 17:47:24 +00001588 if (ch->xdp.res & XDP_REDIRECT)
1589 xdp_do_flush_map();
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001590 else if (rx_cleaned && ch->xdp.res & XDP_TX)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001591 dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
Ioana Radulescud678be12019-03-01 17:47:24 +00001592
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001593 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001594}
1595
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001596static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001597{
1598 struct dpaa2_eth_channel *ch;
1599 int i;
1600
1601 for (i = 0; i < priv->num_channels; i++) {
1602 ch = priv->channel[i];
1603 napi_enable(&ch->napi);
1604 }
1605}
1606
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001607static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001608{
1609 struct dpaa2_eth_channel *ch;
1610 int i;
1611
1612 for (i = 0; i < priv->num_channels; i++) {
1613 ch = priv->channel[i];
1614 napi_disable(&ch->napi);
1615 }
1616}
1617
Ioana Ciornei07beb162020-05-31 00:08:14 +03001618void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1619 bool tx_pause, bool pfc)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001620{
1621 struct dpni_taildrop td = {0};
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001622 struct dpaa2_eth_fq *fq;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001623 int i, err;
1624
Ioana Ciornei07beb162020-05-31 00:08:14 +03001625 /* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1626 * flow control is disabled (as it might interfere with either the
1627 * buffer pool depletion trigger for pause frames or with the group
1628 * congestion trigger for PFC frames)
1629 */
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001630 td.enable = !tx_pause;
Ioana Ciornei07beb162020-05-31 00:08:14 +03001631 if (priv->rx_fqtd_enabled == td.enable)
1632 goto set_cgtd;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001633
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001634 td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1635 td.units = DPNI_CONGESTION_UNIT_BYTES;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001636
1637 for (i = 0; i < priv->num_fqs; i++) {
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001638 fq = &priv->fq[i];
1639 if (fq->type != DPAA2_RX_FQ)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001640 continue;
1641 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001642 DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1643 fq->tc, fq->flowid, &td);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001644 if (err) {
1645 netdev_err(priv->net_dev,
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001646 "dpni_set_taildrop(FQ) failed\n");
1647 return;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001648 }
1649 }
1650
Ioana Ciornei07beb162020-05-31 00:08:14 +03001651 priv->rx_fqtd_enabled = td.enable;
1652
1653set_cgtd:
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001654 /* Congestion group taildrop: threshold is in frames, per group
1655 * of FQs belonging to the same traffic class
Ioana Ciornei07beb162020-05-31 00:08:14 +03001656 * Enabled if general Tx pause disabled or if PFCs are enabled
1657 * (congestion group threhsold for PFC generation is lower than the
1658 * CG taildrop threshold, so it won't interfere with it; we also
1659 * want frames in non-PFC enabled traffic classes to be kept in check)
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001660 */
Ioana Ciornei07beb162020-05-31 00:08:14 +03001661 td.enable = !tx_pause || (tx_pause && pfc);
1662 if (priv->rx_cgtd_enabled == td.enable)
1663 return;
1664
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001665 td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
1666 td.units = DPNI_CONGESTION_UNIT_FRAMES;
1667 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
1668 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1669 DPNI_CP_GROUP, DPNI_QUEUE_RX,
1670 i, 0, &td);
1671 if (err) {
1672 netdev_err(priv->net_dev,
1673 "dpni_set_taildrop(CG) failed\n");
1674 return;
1675 }
1676 }
1677
Ioana Ciornei07beb162020-05-31 00:08:14 +03001678 priv->rx_cgtd_enabled = td.enable;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001679}
1680
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001681static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001682{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001683 struct dpni_link_state state = {0};
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001684 bool tx_pause;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001685 int err;
1686
1687 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1688 if (unlikely(err)) {
1689 netdev_err(priv->net_dev,
1690 "dpni_get_link_state() failed\n");
1691 return err;
1692 }
1693
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001694 /* If Tx pause frame settings have changed, we need to update
1695 * Rx FQ taildrop configuration as well. We configure taildrop
1696 * only when pause frame generation is disabled.
1697 */
Ioana Radulescuad054f22020-05-31 00:08:10 +03001698 tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
Ioana Ciornei07beb162020-05-31 00:08:14 +03001699 dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001700
Ioana Ciornei71947922019-10-31 01:18:31 +02001701 /* When we manage the MAC/PHY using phylink there is no need
1702 * to manually update the netif_carrier.
1703 */
1704 if (priv->mac)
1705 goto out;
1706
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001707 /* Chech link state; speed / duplex changes are not treated yet */
1708 if (priv->link_state.up == state.up)
Ioana Radulescucce629432019-08-28 17:08:14 +03001709 goto out;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001710
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001711 if (state.up) {
1712 netif_carrier_on(priv->net_dev);
1713 netif_tx_start_all_queues(priv->net_dev);
1714 } else {
1715 netif_tx_stop_all_queues(priv->net_dev);
1716 netif_carrier_off(priv->net_dev);
1717 }
1718
Ioana Radulescu77160af2017-06-06 10:00:28 -05001719 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001720 state.up ? "up" : "down");
1721
Ioana Radulescucce629432019-08-28 17:08:14 +03001722out:
1723 priv->link_state = state;
1724
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001725 return 0;
1726}
1727
1728static int dpaa2_eth_open(struct net_device *net_dev)
1729{
1730 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1731 int err;
1732
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001733 err = dpaa2_eth_seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001734 if (err) {
1735 /* Not much to do; the buffer pool, though not filled up,
1736 * may still contain some buffers which would enable us
1737 * to limp on.
1738 */
1739 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001740 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001741 }
1742
Ioana Ciornei71947922019-10-31 01:18:31 +02001743 if (!priv->mac) {
1744 /* We'll only start the txqs when the link is actually ready;
1745 * make sure we don't race against the link up notification,
1746 * which may come immediately after dpni_enable();
1747 */
1748 netif_tx_stop_all_queues(net_dev);
1749
1750 /* Also, explicitly set carrier off, otherwise
1751 * netif_carrier_ok() will return true and cause 'ip link show'
1752 * to report the LOWER_UP flag, even though the link
1753 * notification wasn't even received.
1754 */
1755 netif_carrier_off(net_dev);
1756 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001757 dpaa2_eth_enable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001758
1759 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1760 if (err < 0) {
1761 netdev_err(net_dev, "dpni_enable() failed\n");
1762 goto enable_err;
1763 }
1764
Ioana Ciornei4c33a5b2020-09-25 17:44:20 +03001765 if (priv->mac)
Ioana Ciornei71947922019-10-31 01:18:31 +02001766 phylink_start(priv->mac->phylink);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001767
1768 return 0;
1769
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001770enable_err:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001771 dpaa2_eth_disable_ch_napi(priv);
1772 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001773 return err;
1774}
1775
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001776/* Total number of in-flight frames on ingress queues */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001777static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001778{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001779 struct dpaa2_eth_fq *fq;
1780 u32 fcnt = 0, bcnt = 0, total = 0;
1781 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001782
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001783 for (i = 0; i < priv->num_fqs; i++) {
1784 fq = &priv->fq[i];
1785 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1786 if (err) {
1787 netdev_warn(priv->net_dev, "query_fq_count failed");
1788 break;
1789 }
1790 total += fcnt;
1791 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001792
1793 return total;
1794}
1795
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001796static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001797{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001798 int retries = 10;
1799 u32 pending;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001800
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001801 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001802 pending = dpaa2_eth_ingress_fq_count(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001803 if (pending)
1804 msleep(100);
1805 } while (pending && --retries);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001806}
1807
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001808#define DPNI_TX_PENDING_VER_MAJOR 7
1809#define DPNI_TX_PENDING_VER_MINOR 13
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001810static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001811{
1812 union dpni_statistics stats;
1813 int retries = 10;
1814 int err;
1815
1816 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1817 DPNI_TX_PENDING_VER_MINOR) < 0)
1818 goto out;
1819
1820 do {
1821 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1822 &stats);
1823 if (err)
1824 goto out;
1825 if (stats.page_6.tx_pending_frames == 0)
1826 return;
1827 } while (--retries);
1828
1829out:
1830 msleep(500);
1831}
1832
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001833static int dpaa2_eth_stop(struct net_device *net_dev)
1834{
1835 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001836 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001837 int retries = 10;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001838
Ioana Ciornei71947922019-10-31 01:18:31 +02001839 if (!priv->mac) {
1840 netif_tx_stop_all_queues(net_dev);
1841 netif_carrier_off(net_dev);
1842 } else {
1843 phylink_stop(priv->mac->phylink);
1844 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001845
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001846 /* On dpni_disable(), the MC firmware will:
1847 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1848 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1849 * of all in flight Tx frames is finished (and corresponding Tx conf
1850 * frames are enqueued back to software)
1851 *
1852 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1853 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1854 * and Tx conf queues are consumed on NAPI poll.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001855 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001856 dpaa2_eth_wait_for_egress_fq_empty(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001857
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001858 do {
1859 dpni_disable(priv->mc_io, 0, priv->mc_token);
1860 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1861 if (dpni_enabled)
1862 /* Allow the hardware some slack */
1863 msleep(100);
1864 } while (dpni_enabled && --retries);
1865 if (!retries) {
1866 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1867 /* Must go on and disable NAPI nonetheless, so we don't crash at
1868 * the next "ifconfig up"
1869 */
1870 }
1871
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001872 dpaa2_eth_wait_for_ingress_fq_empty(priv);
1873 dpaa2_eth_disable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001874
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001875 /* Empty the buffer pool */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001876 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001877
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001878 /* Empty the Scatter-Gather Buffer cache */
1879 dpaa2_eth_sgt_cache_drain(priv);
1880
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001881 return 0;
1882}
1883
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001884static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1885{
1886 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1887 struct device *dev = net_dev->dev.parent;
1888 int err;
1889
1890 err = eth_mac_addr(net_dev, addr);
1891 if (err < 0) {
1892 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1893 return err;
1894 }
1895
1896 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1897 net_dev->dev_addr);
1898 if (err) {
1899 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1900 return err;
1901 }
1902
1903 return 0;
1904}
1905
1906/** Fill in counters maintained by the GPP driver. These may be different from
1907 * the hardware counters obtained by ethtool.
1908 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001909static void dpaa2_eth_get_stats(struct net_device *net_dev,
1910 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001911{
1912 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1913 struct rtnl_link_stats64 *percpu_stats;
1914 u64 *cpustats;
1915 u64 *netstats = (u64 *)stats;
1916 int i, j;
1917 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1918
1919 for_each_possible_cpu(i) {
1920 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1921 cpustats = (u64 *)percpu_stats;
1922 for (j = 0; j < num; j++)
1923 netstats[j] += cpustats[j];
1924 }
1925}
1926
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001927/* Copy mac unicast addresses from @net_dev to @priv.
1928 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1929 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001930static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
1931 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001932{
1933 struct netdev_hw_addr *ha;
1934 int err;
1935
1936 netdev_for_each_uc_addr(ha, net_dev) {
1937 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1938 ha->addr);
1939 if (err)
1940 netdev_warn(priv->net_dev,
1941 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1942 ha->addr, err);
1943 }
1944}
1945
1946/* Copy mac multicast addresses from @net_dev to @priv
1947 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1948 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001949static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
1950 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001951{
1952 struct netdev_hw_addr *ha;
1953 int err;
1954
1955 netdev_for_each_mc_addr(ha, net_dev) {
1956 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1957 ha->addr);
1958 if (err)
1959 netdev_warn(priv->net_dev,
1960 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1961 ha->addr, err);
1962 }
1963}
1964
1965static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1966{
1967 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1968 int uc_count = netdev_uc_count(net_dev);
1969 int mc_count = netdev_mc_count(net_dev);
1970 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1971 u32 options = priv->dpni_attrs.options;
1972 u16 mc_token = priv->mc_token;
1973 struct fsl_mc_io *mc_io = priv->mc_io;
1974 int err;
1975
1976 /* Basic sanity checks; these probably indicate a misconfiguration */
1977 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1978 netdev_info(net_dev,
1979 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1980 max_mac);
1981
1982 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1983 if (uc_count > max_mac) {
1984 netdev_info(net_dev,
1985 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1986 uc_count, max_mac);
1987 goto force_promisc;
1988 }
1989 if (mc_count + uc_count > max_mac) {
1990 netdev_info(net_dev,
1991 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1992 uc_count + mc_count, max_mac);
1993 goto force_mc_promisc;
1994 }
1995
1996 /* Adjust promisc settings due to flag combinations */
1997 if (net_dev->flags & IFF_PROMISC)
1998 goto force_promisc;
1999 if (net_dev->flags & IFF_ALLMULTI) {
2000 /* First, rebuild unicast filtering table. This should be done
2001 * in promisc mode, in order to avoid frame loss while we
2002 * progressively add entries to the table.
2003 * We don't know whether we had been in promisc already, and
2004 * making an MC call to find out is expensive; so set uc promisc
2005 * nonetheless.
2006 */
2007 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2008 if (err)
2009 netdev_warn(net_dev, "Can't set uc promisc\n");
2010
2011 /* Actual uc table reconstruction. */
2012 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
2013 if (err)
2014 netdev_warn(net_dev, "Can't clear uc filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002015 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002016
2017 /* Finally, clear uc promisc and set mc promisc as requested. */
2018 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2019 if (err)
2020 netdev_warn(net_dev, "Can't clear uc promisc\n");
2021 goto force_mc_promisc;
2022 }
2023
2024 /* Neither unicast, nor multicast promisc will be on... eventually.
2025 * For now, rebuild mac filtering tables while forcing both of them on.
2026 */
2027 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2028 if (err)
2029 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
2030 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2031 if (err)
2032 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
2033
2034 /* Actual mac filtering tables reconstruction */
2035 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
2036 if (err)
2037 netdev_warn(net_dev, "Can't clear mac filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002038 dpaa2_eth_add_mc_hw_addr(net_dev, priv);
2039 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002040
2041 /* Now we can clear both ucast and mcast promisc, without risking
2042 * to drop legitimate frames anymore.
2043 */
2044 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2045 if (err)
2046 netdev_warn(net_dev, "Can't clear ucast promisc\n");
2047 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
2048 if (err)
2049 netdev_warn(net_dev, "Can't clear mcast promisc\n");
2050
2051 return;
2052
2053force_promisc:
2054 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2055 if (err)
2056 netdev_warn(net_dev, "Can't set ucast promisc\n");
2057force_mc_promisc:
2058 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2059 if (err)
2060 netdev_warn(net_dev, "Can't set mcast promisc\n");
2061}
2062
2063static int dpaa2_eth_set_features(struct net_device *net_dev,
2064 netdev_features_t features)
2065{
2066 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2067 netdev_features_t changed = features ^ net_dev->features;
2068 bool enable;
2069 int err;
2070
2071 if (changed & NETIF_F_RXCSUM) {
2072 enable = !!(features & NETIF_F_RXCSUM);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002073 err = dpaa2_eth_set_rx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002074 if (err)
2075 return err;
2076 }
2077
2078 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2079 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002080 err = dpaa2_eth_set_tx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002081 if (err)
2082 return err;
2083 }
2084
2085 return 0;
2086}
2087
Ioana Radulescu859f9982018-04-26 18:23:47 +08002088static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2089{
2090 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2091 struct hwtstamp_config config;
2092
Yangbo Luc5521182020-09-18 17:08:02 +08002093 if (!dpaa2_ptp)
2094 return -EINVAL;
2095
Ioana Radulescu859f9982018-04-26 18:23:47 +08002096 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
2097 return -EFAULT;
2098
2099 switch (config.tx_type) {
2100 case HWTSTAMP_TX_OFF:
Ioana Radulescu859f9982018-04-26 18:23:47 +08002101 case HWTSTAMP_TX_ON:
Yangbo Luc5521182020-09-18 17:08:02 +08002102 case HWTSTAMP_TX_ONESTEP_SYNC:
Yangbo Lu1cf773b2020-09-18 17:08:01 +08002103 priv->tx_tstamp_type = config.tx_type;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002104 break;
2105 default:
2106 return -ERANGE;
2107 }
2108
2109 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
2110 priv->rx_tstamp = false;
2111 } else {
2112 priv->rx_tstamp = true;
2113 /* TS is set for all frame types, not only those requested */
2114 config.rx_filter = HWTSTAMP_FILTER_ALL;
2115 }
2116
2117 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
2118 -EFAULT : 0;
2119}
2120
2121static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2122{
Russell King4a841822020-02-27 12:00:21 +00002123 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2124
Ioana Radulescu859f9982018-04-26 18:23:47 +08002125 if (cmd == SIOCSHWTSTAMP)
2126 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
2127
Russell King4a841822020-02-27 12:00:21 +00002128 if (priv->mac)
2129 return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2130
2131 return -EOPNOTSUPP;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002132}
2133
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002134static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2135{
2136 int mfl, linear_mfl;
2137
2138 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03002139 linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002140 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002141
2142 if (mfl > linear_mfl) {
2143 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2144 linear_mfl - VLAN_ETH_HLEN);
2145 return false;
2146 }
2147
2148 return true;
2149}
2150
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002151static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002152{
2153 int mfl, err;
2154
2155 /* We enforce a maximum Rx frame length based on MTU only if we have
2156 * an XDP program attached (in order to avoid Rx S/G frames).
2157 * Otherwise, we accept all incoming frames as long as they are not
2158 * larger than maximum size supported in hardware
2159 */
2160 if (has_xdp)
2161 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2162 else
2163 mfl = DPAA2_ETH_MFL;
2164
2165 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2166 if (err) {
2167 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2168 return err;
2169 }
2170
2171 return 0;
2172}
2173
2174static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2175{
2176 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2177 int err;
2178
2179 if (!priv->xdp_prog)
2180 goto out;
2181
2182 if (!xdp_mtu_valid(priv, new_mtu))
2183 return -EINVAL;
2184
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002185 err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002186 if (err)
2187 return err;
2188
2189out:
2190 dev->mtu = new_mtu;
2191 return 0;
2192}
2193
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002194static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002195{
2196 struct dpni_buffer_layout buf_layout = {0};
2197 int err;
2198
2199 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2200 DPNI_QUEUE_RX, &buf_layout);
2201 if (err) {
2202 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2203 return err;
2204 }
2205
2206 /* Reserve extra headroom for XDP header size changes */
2207 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2208 (has_xdp ? XDP_PACKET_HEADROOM : 0);
2209 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2210 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2211 DPNI_QUEUE_RX, &buf_layout);
2212 if (err) {
2213 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2214 return err;
2215 }
2216
2217 return 0;
2218}
2219
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002220static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002221{
2222 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2223 struct dpaa2_eth_channel *ch;
2224 struct bpf_prog *old;
2225 bool up, need_update;
2226 int i, err;
2227
2228 if (prog && !xdp_mtu_valid(priv, dev->mtu))
2229 return -EINVAL;
2230
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002231 if (prog)
2232 bpf_prog_add(prog, priv->num_channels);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002233
2234 up = netif_running(dev);
2235 need_update = (!!priv->xdp_prog != !!prog);
2236
2237 if (up)
2238 dpaa2_eth_stop(dev);
2239
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002240 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2241 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2242 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2243 * so we are sure no old format buffers will be used from now on.
2244 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002245 if (need_update) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002246 err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002247 if (err)
2248 goto out_err;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002249 err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002250 if (err)
2251 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002252 }
2253
2254 old = xchg(&priv->xdp_prog, prog);
2255 if (old)
2256 bpf_prog_put(old);
2257
2258 for (i = 0; i < priv->num_channels; i++) {
2259 ch = priv->channel[i];
2260 old = xchg(&ch->xdp.prog, prog);
2261 if (old)
2262 bpf_prog_put(old);
2263 }
2264
2265 if (up) {
2266 err = dpaa2_eth_open(dev);
2267 if (err)
2268 return err;
2269 }
2270
2271 return 0;
2272
2273out_err:
2274 if (prog)
2275 bpf_prog_sub(prog, priv->num_channels);
2276 if (up)
2277 dpaa2_eth_open(dev);
2278
2279 return err;
2280}
2281
2282static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2283{
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002284 switch (xdp->command) {
2285 case XDP_SETUP_PROG:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002286 return dpaa2_eth_setup_xdp(dev, xdp->prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002287 default:
2288 return -EINVAL;
2289 }
2290
2291 return 0;
2292}
2293
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002294static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2295 struct xdp_frame *xdpf,
2296 struct dpaa2_fd *fd)
Ioana Radulescud678be12019-03-01 17:47:24 +00002297{
Ioana Radulescud678be12019-03-01 17:47:24 +00002298 struct device *dev = net_dev->dev.parent;
Ioana Radulescud678be12019-03-01 17:47:24 +00002299 unsigned int needed_headroom;
2300 struct dpaa2_eth_swa *swa;
Ioana Radulescud678be12019-03-01 17:47:24 +00002301 void *buffer_start, *aligned_start;
2302 dma_addr_t addr;
Ioana Radulescud678be12019-03-01 17:47:24 +00002303
2304 /* We require a minimum headroom to be able to transmit the frame.
2305 * Otherwise return an error and let the original net_device handle it
2306 */
Yangbo Lu1cf773b2020-09-18 17:08:01 +08002307 needed_headroom = dpaa2_eth_needed_headroom(NULL);
Ioana Radulescud678be12019-03-01 17:47:24 +00002308 if (xdpf->headroom < needed_headroom)
2309 return -EINVAL;
2310
Ioana Radulescud678be12019-03-01 17:47:24 +00002311 /* Setup the FD fields */
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002312 memset(fd, 0, sizeof(*fd));
Ioana Radulescud678be12019-03-01 17:47:24 +00002313
2314 /* Align FD address, if possible */
2315 buffer_start = xdpf->data - needed_headroom;
2316 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2317 DPAA2_ETH_TX_BUF_ALIGN);
2318 if (aligned_start >= xdpf->data - xdpf->headroom)
2319 buffer_start = aligned_start;
2320
2321 swa = (struct dpaa2_eth_swa *)buffer_start;
2322 /* fill in necessary fields here */
2323 swa->type = DPAA2_ETH_SWA_XDP;
2324 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2325 swa->xdp.xdpf = xdpf;
2326
2327 addr = dma_map_single(dev, buffer_start,
2328 swa->xdp.dma_size,
2329 DMA_BIDIRECTIONAL);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002330 if (unlikely(dma_mapping_error(dev, addr)))
Ioana Radulescud678be12019-03-01 17:47:24 +00002331 return -ENOMEM;
Ioana Radulescud678be12019-03-01 17:47:24 +00002332
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002333 dpaa2_fd_set_addr(fd, addr);
2334 dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2335 dpaa2_fd_set_len(fd, xdpf->len);
2336 dpaa2_fd_set_format(fd, dpaa2_fd_single);
2337 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescud678be12019-03-01 17:47:24 +00002338
2339 return 0;
2340}
2341
2342static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2343 struct xdp_frame **frames, u32 flags)
2344{
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002345 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002346 struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002347 struct rtnl_link_stats64 *percpu_stats;
2348 struct dpaa2_eth_fq *fq;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002349 struct dpaa2_fd *fds;
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002350 int enqueued, i, err;
Ioana Radulescud678be12019-03-01 17:47:24 +00002351
2352 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2353 return -EINVAL;
2354
2355 if (!netif_running(net_dev))
2356 return -ENETDOWN;
2357
Ioana Ciornei8665d972020-04-22 15:05:13 +03002358 fq = &priv->fq[smp_processor_id()];
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002359 xdp_redirect_fds = &fq->xdp_redirect_fds;
2360 fds = xdp_redirect_fds->fds;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002361
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002362 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002363
Ioana Ciornei8665d972020-04-22 15:05:13 +03002364 /* create a FD for each xdp_frame in the list received */
Ioana Radulescud678be12019-03-01 17:47:24 +00002365 for (i = 0; i < n; i++) {
Ioana Ciornei8665d972020-04-22 15:05:13 +03002366 err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2367 if (err)
2368 break;
2369 }
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002370 xdp_redirect_fds->num = i;
Ioana Radulescud678be12019-03-01 17:47:24 +00002371
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002372 /* enqueue all the frame descriptors */
2373 enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
Ioana Radulescud678be12019-03-01 17:47:24 +00002374
Ioana Ciornei8665d972020-04-22 15:05:13 +03002375 /* update statistics */
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002376 percpu_stats->tx_packets += enqueued;
2377 for (i = 0; i < enqueued; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002378 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002379 for (i = enqueued; i < n; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002380 xdp_return_frame_rx_napi(frames[i]);
2381
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002382 return enqueued;
Ioana Radulescud678be12019-03-01 17:47:24 +00002383}
2384
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002385static int update_xps(struct dpaa2_eth_priv *priv)
2386{
2387 struct net_device *net_dev = priv->net_dev;
2388 struct cpumask xps_mask;
2389 struct dpaa2_eth_fq *fq;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002390 int i, num_queues, netdev_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002391 int err = 0;
2392
2393 num_queues = dpaa2_eth_queue_count(priv);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002394 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002395
2396 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2397 * queues, so only process those
2398 */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002399 for (i = 0; i < netdev_queues; i++) {
2400 fq = &priv->fq[i % num_queues];
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002401
2402 cpumask_clear(&xps_mask);
2403 cpumask_set_cpu(fq->target_cpu, &xps_mask);
2404
2405 err = netif_set_xps_queue(net_dev, &xps_mask, i);
2406 if (err) {
2407 netdev_warn_once(net_dev, "Error setting XPS queue\n");
2408 break;
2409 }
2410 }
2411
2412 return err;
2413}
2414
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002415static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2416 struct tc_mqprio_qopt *mqprio)
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002417{
2418 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002419 u8 num_tc, num_queues;
2420 int i;
2421
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002422 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2423 num_queues = dpaa2_eth_queue_count(priv);
2424 num_tc = mqprio->num_tc;
2425
2426 if (num_tc == net_dev->num_tc)
2427 return 0;
2428
2429 if (num_tc > dpaa2_eth_tc_count(priv)) {
2430 netdev_err(net_dev, "Max %d traffic classes supported\n",
2431 dpaa2_eth_tc_count(priv));
Jesper Dangaard Brouerb89c1e62020-04-23 16:57:50 +02002432 return -EOPNOTSUPP;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002433 }
2434
2435 if (!num_tc) {
2436 netdev_reset_tc(net_dev);
2437 netif_set_real_num_tx_queues(net_dev, num_queues);
2438 goto out;
2439 }
2440
2441 netdev_set_num_tc(net_dev, num_tc);
2442 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2443
2444 for (i = 0; i < num_tc; i++)
2445 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2446
2447out:
2448 update_xps(priv);
2449
2450 return 0;
2451}
2452
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002453#define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2454
2455static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2456{
2457 struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2458 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2459 struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2460 struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2461 int err;
2462
2463 if (p->command == TC_TBF_STATS)
2464 return -EOPNOTSUPP;
2465
2466 /* Only per port Tx shaping */
2467 if (p->parent != TC_H_ROOT)
2468 return -EOPNOTSUPP;
2469
2470 if (p->command == TC_TBF_REPLACE) {
2471 if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2472 netdev_err(net_dev, "burst size cannot be greater than %d\n",
2473 DPAA2_ETH_MAX_BURST_SIZE);
2474 return -EINVAL;
2475 }
2476
2477 tx_cr_shaper.max_burst_size = cfg->max_size;
2478 /* The TBF interface is in bytes/s, whereas DPAA2 expects the
2479 * rate in Mbits/s
2480 */
2481 tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2482 }
2483
2484 err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2485 &tx_er_shaper, 0);
2486 if (err) {
2487 netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2488 return err;
2489 }
2490
2491 return 0;
2492}
2493
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002494static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2495 enum tc_setup_type type, void *type_data)
2496{
2497 switch (type) {
2498 case TC_SETUP_QDISC_MQPRIO:
2499 return dpaa2_eth_setup_mqprio(net_dev, type_data);
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002500 case TC_SETUP_QDISC_TBF:
2501 return dpaa2_eth_setup_tbf(net_dev, type_data);
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002502 default:
2503 return -EOPNOTSUPP;
2504 }
2505}
2506
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002507static const struct net_device_ops dpaa2_eth_ops = {
2508 .ndo_open = dpaa2_eth_open,
2509 .ndo_start_xmit = dpaa2_eth_tx,
2510 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002511 .ndo_set_mac_address = dpaa2_eth_set_addr,
2512 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002513 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2514 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08002515 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002516 .ndo_change_mtu = dpaa2_eth_change_mtu,
2517 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescud678be12019-03-01 17:47:24 +00002518 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002519 .ndo_setup_tc = dpaa2_eth_setup_tc,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002520};
2521
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002522static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002523{
2524 struct dpaa2_eth_channel *ch;
2525
2526 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002527
2528 /* Update NAPI statistics */
2529 ch->stats.cdan++;
2530
Jiafei Pan6c33ae12020-08-03 23:10:08 +03002531 napi_schedule(&ch->napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002532}
2533
2534/* Allocate and configure a DPCON object */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002535static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002536{
2537 struct fsl_mc_device *dpcon;
2538 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002539 int err;
2540
2541 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2542 FSL_MC_POOL_DPCON, &dpcon);
2543 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002544 if (err == -ENXIO)
2545 err = -EPROBE_DEFER;
2546 else
2547 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2548 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002549 }
2550
2551 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2552 if (err) {
2553 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002554 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002555 }
2556
2557 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2558 if (err) {
2559 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002560 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002561 }
2562
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002563 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2564 if (err) {
2565 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002566 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002567 }
2568
2569 return dpcon;
2570
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002571close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002572 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002573free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002574 fsl_mc_object_free(dpcon);
2575
YueHaibing02afa9c2020-08-04 21:26:43 +08002576 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002577}
2578
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002579static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
2580 struct fsl_mc_device *dpcon)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002581{
2582 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2583 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2584 fsl_mc_object_free(dpcon);
2585}
2586
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002587static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002588{
2589 struct dpaa2_eth_channel *channel;
2590 struct dpcon_attr attr;
2591 struct device *dev = priv->net_dev->dev.parent;
2592 int err;
2593
2594 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2595 if (!channel)
2596 return NULL;
2597
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002598 channel->dpcon = dpaa2_eth_setup_dpcon(priv);
YueHaibing02afa9c2020-08-04 21:26:43 +08002599 if (IS_ERR(channel->dpcon)) {
2600 err = PTR_ERR(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002601 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002602 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002603
2604 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2605 &attr);
2606 if (err) {
2607 dev_err(dev, "dpcon_get_attributes() failed\n");
2608 goto err_get_attr;
2609 }
2610
2611 channel->dpcon_id = attr.id;
2612 channel->ch_id = attr.qbman_ch_id;
2613 channel->priv = priv;
2614
2615 return channel;
2616
2617err_get_attr:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002618 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002619err_setup:
2620 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002621 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002622}
2623
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002624static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
2625 struct dpaa2_eth_channel *channel)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002626{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002627 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002628 kfree(channel);
2629}
2630
2631/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2632 * and register data availability notifications
2633 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002634static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002635{
2636 struct dpaa2_io_notification_ctx *nctx;
2637 struct dpaa2_eth_channel *channel;
2638 struct dpcon_notification_cfg dpcon_notif_cfg;
2639 struct device *dev = priv->net_dev->dev.parent;
2640 int i, err;
2641
2642 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2643 * many cores as possible, so we need one channel for each core
2644 * (unless there's fewer queues than cores, in which case the extra
2645 * channels would be wasted).
2646 * Allocate one channel per core and register it to the core's
2647 * affine DPIO. If not enough channels are available for all cores
2648 * or if some cores don't have an affine DPIO, there will be no
2649 * ingress frame processing on those cores.
2650 */
2651 cpumask_clear(&priv->dpio_cpumask);
2652 for_each_online_cpu(i) {
2653 /* Try to allocate a channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002654 channel = dpaa2_eth_alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002655 if (IS_ERR_OR_NULL(channel)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002656 err = PTR_ERR_OR_ZERO(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002657 if (err != -EPROBE_DEFER)
2658 dev_info(dev,
2659 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002660 goto err_alloc_ch;
2661 }
2662
2663 priv->channel[priv->num_channels] = channel;
2664
2665 nctx = &channel->nctx;
2666 nctx->is_cdan = 1;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002667 nctx->cb = dpaa2_eth_cdan_cb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002668 nctx->id = channel->ch_id;
2669 nctx->desired_cpu = i;
2670
2671 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06002672 channel->dpio = dpaa2_io_service_select(i);
Ioana Ciornei47441f72018-12-10 16:50:19 +00002673 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002674 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002675 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002676 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002677 * none available for next cores either. Signal we want
2678 * to retry later, in case the DPIO devices weren't
2679 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002680 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002681 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002682 goto err_service_reg;
2683 }
2684
2685 /* Register DPCON notification with MC */
2686 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2687 dpcon_notif_cfg.priority = 0;
2688 dpcon_notif_cfg.user_ctx = nctx->qman64;
2689 err = dpcon_set_notification(priv->mc_io, 0,
2690 channel->dpcon->mc_handle,
2691 &dpcon_notif_cfg);
2692 if (err) {
2693 dev_err(dev, "dpcon_set_notification failed()\n");
2694 goto err_set_cdan;
2695 }
2696
2697 /* If we managed to allocate a channel and also found an affine
2698 * DPIO for this core, add it to the final mask
2699 */
2700 cpumask_set_cpu(i, &priv->dpio_cpumask);
2701 priv->num_channels++;
2702
2703 /* Stop if we already have enough channels to accommodate all
2704 * RX and TX conf queues
2705 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00002706 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002707 break;
2708 }
2709
2710 return 0;
2711
2712err_set_cdan:
Ioana Ciornei47441f72018-12-10 16:50:19 +00002713 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002714err_service_reg:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002715 dpaa2_eth_free_channel(priv, channel);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002716err_alloc_ch:
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002717 if (err == -EPROBE_DEFER) {
2718 for (i = 0; i < priv->num_channels; i++) {
2719 channel = priv->channel[i];
2720 nctx = &channel->nctx;
2721 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002722 dpaa2_eth_free_channel(priv, channel);
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002723 }
2724 priv->num_channels = 0;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002725 return err;
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002726 }
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002727
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002728 if (cpumask_empty(&priv->dpio_cpumask)) {
2729 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002730 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002731 }
2732
2733 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2734 cpumask_pr_args(&priv->dpio_cpumask));
2735
2736 return 0;
2737}
2738
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002739static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002740{
Ioana Ciornei47441f72018-12-10 16:50:19 +00002741 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002742 struct dpaa2_eth_channel *ch;
Ioana Ciornei47441f72018-12-10 16:50:19 +00002743 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002744
2745 /* deregister CDAN notifications and free channels */
2746 for (i = 0; i < priv->num_channels; i++) {
2747 ch = priv->channel[i];
Ioana Ciornei47441f72018-12-10 16:50:19 +00002748 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002749 dpaa2_eth_free_channel(priv, ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002750 }
2751}
2752
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002753static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
2754 int cpu)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002755{
2756 struct device *dev = priv->net_dev->dev.parent;
2757 int i;
2758
2759 for (i = 0; i < priv->num_channels; i++)
2760 if (priv->channel[i]->nctx.desired_cpu == cpu)
2761 return priv->channel[i];
2762
2763 /* We should never get here. Issue a warning and return
2764 * the first channel, because it's still better than nothing
2765 */
2766 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2767
2768 return priv->channel[0];
2769}
2770
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002771static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002772{
2773 struct device *dev = priv->net_dev->dev.parent;
2774 struct dpaa2_eth_fq *fq;
2775 int rx_cpu, txc_cpu;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002776 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002777
2778 /* For each FQ, pick one channel/CPU to deliver frames to.
2779 * This may well change at runtime, either through irqbalance or
2780 * through direct user intervention.
2781 */
2782 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2783
2784 for (i = 0; i < priv->num_fqs; i++) {
2785 fq = &priv->fq[i];
2786 switch (fq->type) {
2787 case DPAA2_RX_FQ:
Ioana Ciornei061d6312020-10-01 18:11:48 +03002788 case DPAA2_RX_ERR_FQ:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002789 fq->target_cpu = rx_cpu;
2790 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2791 if (rx_cpu >= nr_cpu_ids)
2792 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2793 break;
2794 case DPAA2_TX_CONF_FQ:
2795 fq->target_cpu = txc_cpu;
2796 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2797 if (txc_cpu >= nr_cpu_ids)
2798 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2799 break;
2800 default:
2801 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2802 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002803 fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002804 }
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002805
2806 update_xps(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002807}
2808
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002809static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002810{
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002811 int i, j;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002812
2813 /* We have one TxConf FQ per Tx flow.
2814 * The number of Tx and Rx queues is the same.
2815 * Tx queues come first in the fq array.
2816 */
2817 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2818 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2819 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2820 priv->fq[priv->num_fqs++].flowid = (u16)i;
2821 }
2822
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002823 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2824 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2825 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2826 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2827 priv->fq[priv->num_fqs].tc = (u8)j;
2828 priv->fq[priv->num_fqs++].flowid = (u16)i;
2829 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002830 }
2831
Ioana Ciornei061d6312020-10-01 18:11:48 +03002832 /* We have exactly one Rx error queue per DPNI */
2833 priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
2834 priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
2835
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002836 /* For each FQ, decide on which core to process incoming frames */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002837 dpaa2_eth_set_fq_affinity(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002838}
2839
2840/* Allocate and configure one buffer pool for each interface */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002841static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002842{
2843 int err;
2844 struct fsl_mc_device *dpbp_dev;
2845 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002846 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002847
2848 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2849 &dpbp_dev);
2850 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002851 if (err == -ENXIO)
2852 err = -EPROBE_DEFER;
2853 else
2854 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002855 return err;
2856 }
2857
2858 priv->dpbp_dev = dpbp_dev;
2859
2860 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2861 &dpbp_dev->mc_handle);
2862 if (err) {
2863 dev_err(dev, "dpbp_open() failed\n");
2864 goto err_open;
2865 }
2866
Ioana Radulescud00defe2017-06-06 10:00:32 -05002867 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2868 if (err) {
2869 dev_err(dev, "dpbp_reset() failed\n");
2870 goto err_reset;
2871 }
2872
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002873 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2874 if (err) {
2875 dev_err(dev, "dpbp_enable() failed\n");
2876 goto err_enable;
2877 }
2878
2879 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002880 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002881 if (err) {
2882 dev_err(dev, "dpbp_get_attributes() failed\n");
2883 goto err_get_attr;
2884 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002885 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002886
2887 return 0;
2888
2889err_get_attr:
2890 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2891err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002892err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002893 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2894err_open:
2895 fsl_mc_object_free(dpbp_dev);
2896
2897 return err;
2898}
2899
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002900static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002901{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002902 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002903 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2904 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2905 fsl_mc_object_free(priv->dpbp_dev);
2906}
2907
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002908static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002909{
2910 struct device *dev = priv->net_dev->dev.parent;
2911 struct dpni_buffer_layout buf_layout = {0};
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002912 u16 rx_buf_align;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002913 int err;
2914
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002915 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2916 * version, this number is not always provided correctly on rev1.
2917 * We need to check for both alternatives in this situation.
2918 */
2919 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2920 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002921 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002922 else
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002923 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002924
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03002925 /* We need to ensure that the buffer size seen by WRIOP is a multiple
2926 * of 64 or 256 bytes depending on the WRIOP version.
2927 */
2928 priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
2929
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002930 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002931 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002932 buf_layout.pass_timestamp = true;
Yangbo Luc5521182020-09-18 17:08:02 +08002933 buf_layout.pass_frame_status = true;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002934 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
Yangbo Luc5521182020-09-18 17:08:02 +08002935 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, &buf_layout);
2939 if (err) {
2940 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2941 return err;
2942 }
2943
2944 /* tx-confirm buffer */
Yangbo Luc5521182020-09-18 17:08:02 +08002945 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
2946 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002947 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2948 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2949 if (err) {
2950 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2951 return err;
2952 }
2953
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002954 /* Now that we've set our tx buffer layout, retrieve the minimum
2955 * required tx data offset.
2956 */
2957 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2958 &priv->tx_data_offset);
2959 if (err) {
2960 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2961 return err;
2962 }
2963
2964 if ((priv->tx_data_offset % 64) != 0)
2965 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2966 priv->tx_data_offset);
2967
2968 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06002969 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002970 buf_layout.pass_parser_result = true;
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002971 buf_layout.data_align = rx_buf_align;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002972 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2973 buf_layout.private_data_size = 0;
2974 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2975 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2976 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08002977 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2978 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002979 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2980 DPNI_QUEUE_RX, &buf_layout);
2981 if (err) {
2982 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2983 return err;
2984 }
2985
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002986 return 0;
2987}
2988
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002989#define DPNI_ENQUEUE_FQID_VER_MAJOR 7
2990#define DPNI_ENQUEUE_FQID_VER_MINOR 9
2991
2992static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2993 struct dpaa2_eth_fq *fq,
Ioana Ciornei48c04812020-04-22 15:05:10 +03002994 struct dpaa2_fd *fd, u8 prio,
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002995 u32 num_frames __always_unused,
Ioana Ciornei48c04812020-04-22 15:05:10 +03002996 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002997{
Ioana Ciornei48c04812020-04-22 15:05:10 +03002998 int err;
2999
3000 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
3001 priv->tx_qdid, prio,
3002 fq->tx_qdbin, fd);
3003 if (!err && frames_enqueued)
3004 *frames_enqueued = 1;
3005 return err;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003006}
3007
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003008static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
3009 struct dpaa2_eth_fq *fq,
3010 struct dpaa2_fd *fd,
3011 u8 prio, u32 num_frames,
3012 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003013{
Ioana Ciornei48c04812020-04-22 15:05:10 +03003014 int err;
3015
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003016 err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
3017 fq->tx_fqid[prio],
3018 fd, num_frames);
3019
3020 if (err == 0)
3021 return -EBUSY;
3022
3023 if (frames_enqueued)
3024 *frames_enqueued = err;
3025 return 0;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003026}
3027
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003028static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003029{
3030 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3031 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3032 priv->enqueue = dpaa2_eth_enqueue_qd;
3033 else
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003034 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003035}
3036
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003037static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003038{
3039 struct device *dev = priv->net_dev->dev.parent;
3040 struct dpni_link_cfg link_cfg = {0};
3041 int err;
3042
3043 /* Get the default link options so we don't override other flags */
3044 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3045 if (err) {
3046 dev_err(dev, "dpni_get_link_cfg() failed\n");
3047 return err;
3048 }
3049
3050 /* By default, enable both Rx and Tx pause frames */
3051 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
3052 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
3053 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3054 if (err) {
3055 dev_err(dev, "dpni_set_link_cfg() failed\n");
3056 return err;
3057 }
3058
3059 priv->link_state.options = link_cfg.options;
3060
3061 return 0;
3062}
3063
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003064static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
Ioana Radulescua690af4f2019-10-16 10:36:23 +03003065{
3066 struct dpni_queue_id qid = {0};
3067 struct dpaa2_eth_fq *fq;
3068 struct dpni_queue queue;
3069 int i, j, err;
3070
3071 /* We only use Tx FQIDs for FQID-based enqueue, so check
3072 * if DPNI version supports it before updating FQIDs
3073 */
3074 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3075 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3076 return;
3077
3078 for (i = 0; i < priv->num_fqs; i++) {
3079 fq = &priv->fq[i];
3080 if (fq->type != DPAA2_TX_CONF_FQ)
3081 continue;
3082 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3083 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3084 DPNI_QUEUE_TX, j, fq->flowid,
3085 &queue, &qid);
3086 if (err)
3087 goto out_err;
3088
3089 fq->tx_fqid[j] = qid.fqid;
3090 if (fq->tx_fqid[j] == 0)
3091 goto out_err;
3092 }
3093 }
3094
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003095 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Radulescua690af4f2019-10-16 10:36:23 +03003096
3097 return;
3098
3099out_err:
3100 netdev_info(priv->net_dev,
3101 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3102 priv->enqueue = dpaa2_eth_enqueue_qd;
3103}
3104
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003105/* Configure ingress classification based on VLAN PCP */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003106static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003107{
3108 struct device *dev = priv->net_dev->dev.parent;
3109 struct dpkg_profile_cfg kg_cfg = {0};
3110 struct dpni_qos_tbl_cfg qos_cfg = {0};
3111 struct dpni_rule_cfg key_params;
3112 void *dma_mem, *key, *mask;
3113 u8 key_size = 2; /* VLAN TCI field */
3114 int i, pcp, err;
3115
3116 /* VLAN-based classification only makes sense if we have multiple
3117 * traffic classes.
3118 * Also, we need to extract just the 3-bit PCP field from the VLAN
3119 * header and we can only do that by using a mask
3120 */
3121 if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3122 dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3123 return -EOPNOTSUPP;
3124 }
3125
3126 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3127 if (!dma_mem)
3128 return -ENOMEM;
3129
3130 kg_cfg.num_extracts = 1;
3131 kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3132 kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3133 kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3134 kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3135
3136 err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3137 if (err) {
3138 dev_err(dev, "dpni_prepare_key_cfg failed\n");
3139 goto out_free_tbl;
3140 }
3141
3142 /* set QoS table */
3143 qos_cfg.default_tc = 0;
3144 qos_cfg.discard_on_miss = 0;
3145 qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3146 DPAA2_CLASSIFIER_DMA_SIZE,
3147 DMA_TO_DEVICE);
3148 if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3149 dev_err(dev, "QoS table DMA mapping failed\n");
3150 err = -ENOMEM;
3151 goto out_free_tbl;
3152 }
3153
3154 err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3155 if (err) {
3156 dev_err(dev, "dpni_set_qos_table failed\n");
3157 goto out_unmap_tbl;
3158 }
3159
3160 /* Add QoS table entries */
3161 key = kzalloc(key_size * 2, GFP_KERNEL);
3162 if (!key) {
3163 err = -ENOMEM;
3164 goto out_unmap_tbl;
3165 }
3166 mask = key + key_size;
3167 *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3168
3169 key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3170 DMA_TO_DEVICE);
3171 if (dma_mapping_error(dev, key_params.key_iova)) {
3172 dev_err(dev, "Qos table entry DMA mapping failed\n");
3173 err = -ENOMEM;
3174 goto out_free_key;
3175 }
3176
3177 key_params.mask_iova = key_params.key_iova + key_size;
3178 key_params.key_size = key_size;
3179
3180 /* We add rules for PCP-based distribution starting with highest
3181 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3182 * classes to accommodate all priority levels, the lowest ones end up
3183 * on TC 0 which was configured as default
3184 */
3185 for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3186 *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3187 dma_sync_single_for_device(dev, key_params.key_iova,
3188 key_size * 2, DMA_TO_DEVICE);
3189
3190 err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3191 &key_params, i, i);
3192 if (err) {
3193 dev_err(dev, "dpni_add_qos_entry failed\n");
3194 dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3195 goto out_unmap_key;
3196 }
3197 }
3198
3199 priv->vlan_cls_enabled = true;
3200
3201 /* Table and key memory is not persistent, clean everything up after
3202 * configuration is finished
3203 */
3204out_unmap_key:
3205 dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3206out_free_key:
3207 kfree(key);
3208out_unmap_tbl:
3209 dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3210 DMA_TO_DEVICE);
3211out_free_tbl:
3212 kfree(dma_mem);
3213
3214 return err;
3215}
3216
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003217/* Configure the DPNI object this interface is associated with */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003218static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003219{
3220 struct device *dev = &ls_dev->dev;
3221 struct dpaa2_eth_priv *priv;
3222 struct net_device *net_dev;
3223 int err;
3224
3225 net_dev = dev_get_drvdata(dev);
3226 priv = netdev_priv(net_dev);
3227
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003228 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05003229 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003230 if (err) {
3231 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003232 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003233 }
3234
Ioana Radulescu311cffa2018-03-23 08:44:09 -05003235 /* Check if we can work with this DPNI object */
3236 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3237 &priv->dpni_ver_minor);
3238 if (err) {
3239 dev_err(dev, "dpni_get_api_version() failed\n");
3240 goto close;
3241 }
3242 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3243 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3244 priv->dpni_ver_major, priv->dpni_ver_minor,
3245 DPNI_VER_MAJOR, DPNI_VER_MINOR);
3246 err = -ENOTSUPP;
3247 goto close;
3248 }
3249
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003250 ls_dev->mc_io = priv->mc_io;
3251 ls_dev->mc_handle = priv->mc_token;
3252
3253 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3254 if (err) {
3255 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003256 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003257 }
3258
3259 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3260 &priv->dpni_attrs);
3261 if (err) {
3262 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003263 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003264 }
3265
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003266 err = dpaa2_eth_set_buffer_layout(priv);
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003267 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003268 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003269
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003270 dpaa2_eth_set_enqueue_mode(priv);
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003271
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003272 /* Enable pause frame support */
3273 if (dpaa2_eth_has_pause_support(priv)) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003274 err = dpaa2_eth_set_pause(priv);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003275 if (err)
3276 goto close;
3277 }
3278
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003279 err = dpaa2_eth_set_vlan_qos(priv);
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003280 if (err && err != -EOPNOTSUPP)
3281 goto close;
3282
Xu Wang9334d5b2020-06-11 02:45:20 +00003283 priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3284 sizeof(struct dpaa2_eth_cls_rule),
3285 GFP_KERNEL);
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003286 if (!priv->cls_rules) {
3287 err = -ENOMEM;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003288 goto close;
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003289 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003290
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003291 return 0;
3292
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003293close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003294 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003295
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003296 return err;
3297}
3298
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003299static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003300{
3301 int err;
3302
3303 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3304 if (err)
3305 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3306 err);
3307
3308 dpni_close(priv->mc_io, 0, priv->mc_token);
3309}
3310
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003311static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3312 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003313{
3314 struct device *dev = priv->net_dev->dev.parent;
3315 struct dpni_queue queue;
3316 struct dpni_queue_id qid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003317 int err;
3318
3319 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003320 DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003321 if (err) {
3322 dev_err(dev, "dpni_get_queue(RX) failed\n");
3323 return err;
3324 }
3325
3326 fq->fqid = qid.fqid;
3327
3328 queue.destination.id = fq->channel->dpcon_id;
3329 queue.destination.type = DPNI_DEST_DPCON;
3330 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003331 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003332 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003333 DPNI_QUEUE_RX, fq->tc, fq->flowid,
Ioana Radulescu16fa1cf2019-05-23 17:38:22 +03003334 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003335 &queue);
3336 if (err) {
3337 dev_err(dev, "dpni_set_queue(RX) failed\n");
3338 return err;
3339 }
3340
Ioana Radulescud678be12019-03-01 17:47:24 +00003341 /* xdp_rxq setup */
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003342 /* only once for each channel */
3343 if (fq->tc > 0)
3344 return 0;
3345
Ioana Radulescud678be12019-03-01 17:47:24 +00003346 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3347 fq->flowid);
3348 if (err) {
3349 dev_err(dev, "xdp_rxq_info_reg failed\n");
3350 return err;
3351 }
3352
3353 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3354 MEM_TYPE_PAGE_ORDER0, NULL);
3355 if (err) {
3356 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3357 return err;
3358 }
3359
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003360 return 0;
3361}
3362
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003363static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3364 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003365{
3366 struct device *dev = priv->net_dev->dev.parent;
3367 struct dpni_queue queue;
3368 struct dpni_queue_id qid;
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003369 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003370
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003371 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3372 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3373 DPNI_QUEUE_TX, i, fq->flowid,
3374 &queue, &qid);
3375 if (err) {
3376 dev_err(dev, "dpni_get_queue(TX) failed\n");
3377 return err;
3378 }
3379 fq->tx_fqid[i] = qid.fqid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003380 }
3381
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003382 /* All Tx queues belonging to the same flowid have the same qdbin */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003383 fq->tx_qdbin = qid.qdbin;
3384
3385 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3386 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3387 &queue, &qid);
3388 if (err) {
3389 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3390 return err;
3391 }
3392
3393 fq->fqid = qid.fqid;
3394
3395 queue.destination.id = fq->channel->dpcon_id;
3396 queue.destination.type = DPNI_DEST_DPCON;
3397 queue.destination.priority = 0;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003398 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003399 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3400 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3401 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3402 &queue);
3403 if (err) {
3404 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3405 return err;
3406 }
3407
3408 return 0;
3409}
3410
Ioana Ciornei061d6312020-10-01 18:11:48 +03003411static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
3412 struct dpaa2_eth_fq *fq)
3413{
3414 struct device *dev = priv->net_dev->dev.parent;
3415 struct dpni_queue q = { { 0 } };
3416 struct dpni_queue_id qid;
3417 u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
3418 int err;
3419
3420 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3421 DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
3422 if (err) {
3423 dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
3424 return err;
3425 }
3426
3427 fq->fqid = qid.fqid;
3428
3429 q.destination.id = fq->channel->dpcon_id;
3430 q.destination.type = DPNI_DEST_DPCON;
3431 q.destination.priority = 1;
3432 q.user_context = (u64)(uintptr_t)fq;
3433 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3434 DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
3435 if (err) {
3436 dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
3437 return err;
3438 }
3439
3440 return 0;
3441}
3442
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003443/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003444static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003445 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003446 /* L2 header */
3447 .rxnfc_field = RXH_L2DA,
3448 .cls_prot = NET_PROT_ETH,
3449 .cls_field = NH_FLD_ETH_DA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003450 .id = DPAA2_ETH_DIST_ETHDST,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003451 .size = 6,
3452 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003453 .cls_prot = NET_PROT_ETH,
3454 .cls_field = NH_FLD_ETH_SA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003455 .id = DPAA2_ETH_DIST_ETHSRC,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003456 .size = 6,
3457 }, {
3458 /* This is the last ethertype field parsed:
3459 * depending on frame format, it can be the MAC ethertype
3460 * or the VLAN etype.
3461 */
3462 .cls_prot = NET_PROT_ETH,
3463 .cls_field = NH_FLD_ETH_TYPE,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003464 .id = DPAA2_ETH_DIST_ETHTYPE,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003465 .size = 2,
3466 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003467 /* VLAN header */
3468 .rxnfc_field = RXH_VLAN,
3469 .cls_prot = NET_PROT_VLAN,
3470 .cls_field = NH_FLD_VLAN_TCI,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003471 .id = DPAA2_ETH_DIST_VLAN,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003472 .size = 2,
3473 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003474 /* IP header */
3475 .rxnfc_field = RXH_IP_SRC,
3476 .cls_prot = NET_PROT_IP,
3477 .cls_field = NH_FLD_IP_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003478 .id = DPAA2_ETH_DIST_IPSRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003479 .size = 4,
3480 }, {
3481 .rxnfc_field = RXH_IP_DST,
3482 .cls_prot = NET_PROT_IP,
3483 .cls_field = NH_FLD_IP_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003484 .id = DPAA2_ETH_DIST_IPDST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003485 .size = 4,
3486 }, {
3487 .rxnfc_field = RXH_L3_PROTO,
3488 .cls_prot = NET_PROT_IP,
3489 .cls_field = NH_FLD_IP_PROTO,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003490 .id = DPAA2_ETH_DIST_IPPROTO,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003491 .size = 1,
3492 }, {
3493 /* Using UDP ports, this is functionally equivalent to raw
3494 * byte pairs from L4 header.
3495 */
3496 .rxnfc_field = RXH_L4_B_0_1,
3497 .cls_prot = NET_PROT_UDP,
3498 .cls_field = NH_FLD_UDP_PORT_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003499 .id = DPAA2_ETH_DIST_L4SRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003500 .size = 2,
3501 }, {
3502 .rxnfc_field = RXH_L4_B_2_3,
3503 .cls_prot = NET_PROT_UDP,
3504 .cls_field = NH_FLD_UDP_PORT_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003505 .id = DPAA2_ETH_DIST_L4DST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003506 .size = 2,
3507 },
3508};
3509
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003510/* Configure the Rx hash key using the legacy API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003511static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003512{
3513 struct device *dev = priv->net_dev->dev.parent;
3514 struct dpni_rx_tc_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003515 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003516
3517 memset(&dist_cfg, 0, sizeof(dist_cfg));
3518
3519 dist_cfg.key_cfg_iova = key;
3520 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3521 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3522
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003523 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3524 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3525 i, &dist_cfg);
3526 if (err) {
3527 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3528 break;
3529 }
3530 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003531
3532 return err;
3533}
3534
3535/* Configure the Rx hash key using the new API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003536static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003537{
3538 struct device *dev = priv->net_dev->dev.parent;
3539 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003540 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003541
3542 memset(&dist_cfg, 0, sizeof(dist_cfg));
3543
3544 dist_cfg.key_cfg_iova = key;
3545 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3546 dist_cfg.enable = 1;
3547
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003548 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3549 dist_cfg.tc = i;
3550 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3551 &dist_cfg);
3552 if (err) {
3553 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3554 break;
3555 }
Ionut-robert Aron5e29c162020-09-25 17:44:21 +03003556
3557 /* If the flow steering / hashing key is shared between all
3558 * traffic classes, install it just once
3559 */
3560 if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3561 break;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003562 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003563
3564 return err;
3565}
3566
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003567/* Configure the Rx flow classification key */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003568static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003569{
3570 struct device *dev = priv->net_dev->dev.parent;
3571 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003572 int i, err = 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003573
3574 memset(&dist_cfg, 0, sizeof(dist_cfg));
3575
3576 dist_cfg.key_cfg_iova = key;
3577 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3578 dist_cfg.enable = 1;
3579
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003580 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3581 dist_cfg.tc = i;
3582 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3583 &dist_cfg);
3584 if (err) {
3585 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3586 break;
3587 }
Ionut-robert Aron5e29c162020-09-25 17:44:21 +03003588
3589 /* If the flow steering / hashing key is shared between all
3590 * traffic classes, install it just once
3591 */
3592 if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3593 break;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003594 }
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003595
3596 return err;
3597}
3598
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003599/* Size of the Rx flow classification key */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003600int dpaa2_eth_cls_key_size(u64 fields)
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003601{
3602 int i, size = 0;
3603
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003604 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3605 if (!(fields & dist_fields[i].id))
3606 continue;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003607 size += dist_fields[i].size;
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003608 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003609
3610 return size;
3611}
3612
3613/* Offset of header field in Rx classification key */
3614int dpaa2_eth_cls_fld_off(int prot, int field)
3615{
3616 int i, off = 0;
3617
3618 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3619 if (dist_fields[i].cls_prot == prot &&
3620 dist_fields[i].cls_field == field)
3621 return off;
3622 off += dist_fields[i].size;
3623 }
3624
3625 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3626 return 0;
3627}
3628
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003629/* Prune unused fields from the classification rule.
3630 * Used when masking is not supported
3631 */
3632void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3633{
3634 int off = 0, new_off = 0;
3635 int i, size;
3636
3637 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3638 size = dist_fields[i].size;
3639 if (dist_fields[i].id & fields) {
3640 memcpy(key_mem + new_off, key_mem + off, size);
3641 new_off += size;
3642 }
3643 off += size;
3644 }
3645}
3646
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003647/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003648 * flags is a combination of RXH_ bits
3649 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00003650static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
3651 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003652{
3653 struct device *dev = net_dev->dev.parent;
3654 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3655 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003656 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003657 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003658 u8 *dma_mem;
3659 int i;
3660 int err = 0;
3661
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003662 memset(&cls_cfg, 0, sizeof(cls_cfg));
3663
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003664 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003665 struct dpkg_extract *key =
3666 &cls_cfg.extracts[cls_cfg.num_extracts];
3667
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003668 /* For both Rx hashing and classification keys
3669 * we set only the selected fields.
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003670 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003671 if (!(flags & dist_fields[i].id))
3672 continue;
3673 if (type == DPAA2_ETH_RX_DIST_HASH)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003674 rx_hash_fields |= dist_fields[i].rxnfc_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003675
3676 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
3677 dev_err(dev, "error adding key extraction rule, too many rules?\n");
3678 return -E2BIG;
3679 }
3680
3681 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003682 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003683 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003684 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003685 cls_cfg.num_extracts++;
3686 }
3687
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05003688 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003689 if (!dma_mem)
3690 return -ENOMEM;
3691
3692 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3693 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003694 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003695 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003696 }
3697
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003698 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003699 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3700 DMA_TO_DEVICE);
3701 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003702 dev_err(dev, "DMA mapping failed\n");
3703 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003704 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003705 }
3706
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003707 if (type == DPAA2_ETH_RX_DIST_HASH) {
3708 if (dpaa2_eth_has_legacy_dist(priv))
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003709 err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003710 else
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003711 err = dpaa2_eth_config_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003712 } else {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003713 err = dpaa2_eth_config_cls_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003714 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003715
3716 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3717 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003718 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003719 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003720
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003721free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003722 kfree(dma_mem);
3723 return err;
3724}
3725
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003726int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3727{
3728 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003729 u64 key = 0;
3730 int i;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003731
3732 if (!dpaa2_eth_hash_enabled(priv))
3733 return -EOPNOTSUPP;
3734
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003735 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3736 if (dist_fields[i].rxnfc_field & flags)
3737 key |= dist_fields[i].id;
3738
3739 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003740}
3741
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003742int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3743{
3744 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3745}
3746
3747static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003748{
3749 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003750 int err;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003751
3752 /* Check if we actually support Rx flow classification */
3753 if (dpaa2_eth_has_legacy_dist(priv)) {
3754 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3755 return -EOPNOTSUPP;
3756 }
3757
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003758 if (!dpaa2_eth_fs_enabled(priv)) {
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003759 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3760 return -EOPNOTSUPP;
3761 }
3762
3763 if (!dpaa2_eth_hash_enabled(priv)) {
3764 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3765 return -EOPNOTSUPP;
3766 }
3767
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003768 /* If there is no support for masking in the classification table,
3769 * we don't set a default key, as it will depend on the rules
3770 * added by the user at runtime.
3771 */
3772 if (!dpaa2_eth_fs_mask_enabled(priv))
3773 goto out;
3774
3775 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003776 if (err)
3777 return err;
3778
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003779out:
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003780 priv->rx_cls_enabled = 1;
3781
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003782 return 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003783}
3784
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003785/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3786 * frame queues and channels
3787 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003788static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003789{
3790 struct net_device *net_dev = priv->net_dev;
3791 struct device *dev = net_dev->dev.parent;
3792 struct dpni_pools_cfg pools_params;
3793 struct dpni_error_cfg err_cfg;
3794 int err = 0;
3795 int i;
3796
3797 pools_params.num_dpbp = 1;
3798 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3799 pools_params.pools[0].backup_pool = 0;
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03003800 pools_params.pools[0].buffer_size = priv->rx_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003801 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3802 if (err) {
3803 dev_err(dev, "dpni_set_pools() failed\n");
3804 return err;
3805 }
3806
Ioana Radulescu227686b2018-07-27 09:12:59 -05003807 /* have the interface implicitly distribute traffic based on
3808 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003809 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05003810 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003811 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05003812 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003813
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003814 /* Configure the flow classification key; it includes all
3815 * supported header fields and cannot be modified at runtime
3816 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003817 err = dpaa2_eth_set_default_cls(priv);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003818 if (err && err != -EOPNOTSUPP)
3819 dev_err(dev, "Failed to configure Rx classification key\n");
3820
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003821 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05003822 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003823 err_cfg.set_frame_annotation = 1;
3824 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3825 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3826 &err_cfg);
3827 if (err) {
3828 dev_err(dev, "dpni_set_errors_behavior failed\n");
3829 return err;
3830 }
3831
3832 /* Configure Rx and Tx conf queues to generate CDANs */
3833 for (i = 0; i < priv->num_fqs; i++) {
3834 switch (priv->fq[i].type) {
3835 case DPAA2_RX_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003836 err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003837 break;
3838 case DPAA2_TX_CONF_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003839 err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003840 break;
Ioana Ciornei061d6312020-10-01 18:11:48 +03003841 case DPAA2_RX_ERR_FQ:
3842 err = setup_rx_err_flow(priv, &priv->fq[i]);
3843 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003844 default:
3845 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3846 return -EINVAL;
3847 }
3848 if (err)
3849 return err;
3850 }
3851
3852 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3853 DPNI_QUEUE_TX, &priv->tx_qdid);
3854 if (err) {
3855 dev_err(dev, "dpni_get_qdid() failed\n");
3856 return err;
3857 }
3858
3859 return 0;
3860}
3861
3862/* Allocate rings for storing incoming frame descriptors */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003863static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003864{
3865 struct net_device *net_dev = priv->net_dev;
3866 struct device *dev = net_dev->dev.parent;
3867 int i;
3868
3869 for (i = 0; i < priv->num_channels; i++) {
3870 priv->channel[i]->store =
3871 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3872 if (!priv->channel[i]->store) {
3873 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3874 goto err_ring;
3875 }
3876 }
3877
3878 return 0;
3879
3880err_ring:
3881 for (i = 0; i < priv->num_channels; i++) {
3882 if (!priv->channel[i]->store)
3883 break;
3884 dpaa2_io_store_destroy(priv->channel[i]->store);
3885 }
3886
3887 return -ENOMEM;
3888}
3889
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003890static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003891{
3892 int i;
3893
3894 for (i = 0; i < priv->num_channels; i++)
3895 dpaa2_io_store_destroy(priv->channel[i]->store);
3896}
3897
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003898static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003899{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003900 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003901 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003902 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003903 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003904
3905 /* Get firmware address, if any */
3906 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3907 if (err) {
3908 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3909 return err;
3910 }
3911
3912 /* Get DPNI attributes address, if any */
3913 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3914 dpni_mac_addr);
3915 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003916 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003917 return err;
3918 }
3919
3920 /* First check if firmware has any address configured by bootloader */
3921 if (!is_zero_ether_addr(mac_addr)) {
3922 /* If the DPMAC addr != DPNI addr, update it */
3923 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
3924 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
3925 priv->mc_token,
3926 mac_addr);
3927 if (err) {
3928 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3929 return err;
3930 }
3931 }
3932 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
3933 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003934 /* No MAC address configured, fill in net_dev->dev_addr
3935 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003936 */
3937 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003938 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
3939
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003940 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3941 net_dev->dev_addr);
3942 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003943 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003944 return err;
3945 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003946
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003947 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
3948 * practical purposes, this will be our "permanent" mac address,
3949 * at least until the next reboot. This move will also permit
3950 * register_netdevice() to properly fill up net_dev->perm_addr.
3951 */
3952 net_dev->addr_assign_type = NET_ADDR_PERM;
3953 } else {
3954 /* NET_ADDR_PERM is default, all we have to do is
3955 * fill in the device addr.
3956 */
3957 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
3958 }
3959
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003960 return 0;
3961}
3962
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003963static int dpaa2_eth_netdev_init(struct net_device *net_dev)
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003964{
3965 struct device *dev = net_dev->dev.parent;
3966 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003967 u32 options = priv->dpni_attrs.options;
3968 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003969 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003970 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003971 int err;
3972
3973 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003974 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003975
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003976 err = dpaa2_eth_set_mac_addr(priv);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003977 if (err)
3978 return err;
3979
3980 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003981 eth_broadcast_addr(bcast_addr);
3982 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3983 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003984 dev_err(dev, "dpni_add_mac_addr() failed\n");
3985 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003986 }
3987
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05003988 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003989 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05003990 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05003991 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05003992 if (err) {
3993 dev_err(dev, "dpni_set_max_frame_length() failed\n");
3994 return err;
3995 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003996
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003997 /* Set actual number of queues in the net device */
3998 num_queues = dpaa2_eth_queue_count(priv);
3999 err = netif_set_real_num_tx_queues(net_dev, num_queues);
4000 if (err) {
4001 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
4002 return err;
4003 }
4004 err = netif_set_real_num_rx_queues(net_dev, num_queues);
4005 if (err) {
4006 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
4007 return err;
4008 }
4009
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004010 /* Capabilities listing */
4011 supported |= IFF_LIVE_ADDR_CHANGE;
4012
4013 if (options & DPNI_OPT_NO_MAC_FILTER)
4014 not_supported |= IFF_UNICAST_FLT;
4015 else
4016 supported |= IFF_UNICAST_FLT;
4017
4018 net_dev->priv_flags |= supported;
4019 net_dev->priv_flags &= ~not_supported;
4020
4021 /* Features */
4022 net_dev->features = NETIF_F_RXCSUM |
4023 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4024 NETIF_F_SG | NETIF_F_HIGHDMA |
Ioana Ciornei3657cda2020-07-21 19:38:25 +03004025 NETIF_F_LLTX | NETIF_F_HW_TC;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004026 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004027
4028 return 0;
4029}
4030
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004031static int dpaa2_eth_poll_link_state(void *arg)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004032{
4033 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
4034 int err;
4035
4036 while (!kthread_should_stop()) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004037 err = dpaa2_eth_link_state_update(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004038 if (unlikely(err))
4039 return err;
4040
4041 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
4042 }
4043
4044 return 0;
4045}
4046
Ioana Ciornei71947922019-10-31 01:18:31 +02004047static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
4048{
4049 struct fsl_mc_device *dpni_dev, *dpmac_dev;
4050 struct dpaa2_mac *mac;
4051 int err;
4052
4053 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
4054 dpmac_dev = fsl_mc_get_endpoint(dpni_dev);
Ioana Ciornei841eb402020-07-14 15:08:16 +03004055 if (IS_ERR_OR_NULL(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
Ioana Ciornei71947922019-10-31 01:18:31 +02004056 return 0;
4057
4058 if (dpaa2_mac_is_type_fixed(dpmac_dev, priv->mc_io))
4059 return 0;
4060
4061 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
4062 if (!mac)
4063 return -ENOMEM;
4064
4065 mac->mc_dev = dpmac_dev;
4066 mac->mc_io = priv->mc_io;
4067 mac->net_dev = priv->net_dev;
4068
4069 err = dpaa2_mac_connect(mac);
4070 if (err) {
4071 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint\n");
4072 kfree(mac);
4073 return err;
4074 }
4075 priv->mac = mac;
4076
4077 return 0;
4078}
4079
4080static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
4081{
4082 if (!priv->mac)
4083 return;
4084
4085 dpaa2_mac_disconnect(priv->mac);
4086 kfree(priv->mac);
4087 priv->mac = NULL;
4088}
4089
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004090static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
4091{
Ioana Radulescu112197d2017-10-11 08:29:49 -05004092 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004093 struct device *dev = (struct device *)arg;
4094 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
4095 struct net_device *net_dev = dev_get_drvdata(dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02004096 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004097 int err;
4098
4099 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
4100 DPNI_IRQ_INDEX, &status);
4101 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004102 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05004103 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004104 }
4105
Ioana Radulescu112197d2017-10-11 08:29:49 -05004106 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004107 dpaa2_eth_link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004108
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02004109 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004110 dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
4111 dpaa2_eth_update_tx_fqids(priv);
Ioana Ciornei71947922019-10-31 01:18:31 +02004112
4113 rtnl_lock();
4114 if (priv->mac)
4115 dpaa2_eth_disconnect_mac(priv);
4116 else
4117 dpaa2_eth_connect_mac(priv);
4118 rtnl_unlock();
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02004119 }
Florin Chiculita8398b372019-10-16 10:36:22 +03004120
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004121 return IRQ_HANDLED;
4122}
4123
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004124static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004125{
4126 int err = 0;
4127 struct fsl_mc_device_irq *irq;
4128
4129 err = fsl_mc_allocate_irqs(ls_dev);
4130 if (err) {
4131 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4132 return err;
4133 }
4134
4135 irq = ls_dev->irqs[0];
4136 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05004137 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004138 IRQF_NO_SUSPEND | IRQF_ONESHOT,
4139 dev_name(&ls_dev->dev), &ls_dev->dev);
4140 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004141 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004142 goto free_mc_irq;
4143 }
4144
4145 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
Florin Chiculita8398b372019-10-16 10:36:22 +03004146 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4147 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004148 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004149 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004150 goto free_irq;
4151 }
4152
4153 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4154 DPNI_IRQ_INDEX, 1);
4155 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004156 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004157 goto free_irq;
4158 }
4159
4160 return 0;
4161
4162free_irq:
4163 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
4164free_mc_irq:
4165 fsl_mc_free_irqs(ls_dev);
4166
4167 return err;
4168}
4169
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004170static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004171{
4172 int i;
4173 struct dpaa2_eth_channel *ch;
4174
4175 for (i = 0; i < priv->num_channels; i++) {
4176 ch = priv->channel[i];
4177 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4178 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
4179 NAPI_POLL_WEIGHT);
4180 }
4181}
4182
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004183static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004184{
4185 int i;
4186 struct dpaa2_eth_channel *ch;
4187
4188 for (i = 0; i < priv->num_channels; i++) {
4189 ch = priv->channel[i];
4190 netif_napi_del(&ch->napi);
4191 }
4192}
4193
4194static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4195{
4196 struct device *dev;
4197 struct net_device *net_dev = NULL;
4198 struct dpaa2_eth_priv *priv = NULL;
4199 int err = 0;
4200
4201 dev = &dpni_dev->dev;
4202
4203 /* Net device */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03004204 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004205 if (!net_dev) {
4206 dev_err(dev, "alloc_etherdev_mq() failed\n");
4207 return -ENOMEM;
4208 }
4209
4210 SET_NETDEV_DEV(net_dev, dev);
4211 dev_set_drvdata(dev, net_dev);
4212
4213 priv = netdev_priv(net_dev);
4214 priv->net_dev = net_dev;
4215
Ioana Radulescu08eb2392017-05-24 07:13:27 -05004216 priv->iommu_domain = iommu_get_domain_for_dev(dev);
4217
Yangbo Lu1cf773b2020-09-18 17:08:01 +08004218 priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4219 priv->rx_tstamp = false;
4220
Yangbo Luc5521182020-09-18 17:08:02 +08004221 priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
4222 if (!priv->dpaa2_ptp_wq) {
4223 err = -ENOMEM;
4224 goto err_wq_alloc;
4225 }
4226
4227 INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4228
4229 skb_queue_head_init(&priv->tx_skbs);
4230
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004231 /* Obtain a MC portal */
4232 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4233 &priv->mc_io);
4234 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05004235 if (err == -ENXIO)
4236 err = -EPROBE_DEFER;
4237 else
4238 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004239 goto err_portal_alloc;
4240 }
4241
4242 /* MC objects initialization and configuration */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004243 err = dpaa2_eth_setup_dpni(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004244 if (err)
4245 goto err_dpni_setup;
4246
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004247 err = dpaa2_eth_setup_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004248 if (err)
4249 goto err_dpio_setup;
4250
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004251 dpaa2_eth_setup_fqs(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004252
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004253 err = dpaa2_eth_setup_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004254 if (err)
4255 goto err_dpbp_setup;
4256
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004257 err = dpaa2_eth_bind_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004258 if (err)
4259 goto err_bind;
4260
4261 /* Add a NAPI context for each channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004262 dpaa2_eth_add_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004263
4264 /* Percpu statistics */
4265 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4266 if (!priv->percpu_stats) {
4267 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4268 err = -ENOMEM;
4269 goto err_alloc_percpu_stats;
4270 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004271 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4272 if (!priv->percpu_extras) {
4273 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4274 err = -ENOMEM;
4275 goto err_alloc_percpu_extras;
4276 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004277
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004278 priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4279 if (!priv->sgt_cache) {
4280 dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4281 err = -ENOMEM;
4282 goto err_alloc_sgt_cache;
4283 }
4284
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004285 err = dpaa2_eth_netdev_init(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004286 if (err)
4287 goto err_netdev_init;
4288
4289 /* Configure checksum offload based on current interface flags */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004290 err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004291 if (err)
4292 goto err_csum;
4293
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004294 err = dpaa2_eth_set_tx_csum(priv,
4295 !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004296 if (err)
4297 goto err_csum;
4298
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004299 err = dpaa2_eth_alloc_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004300 if (err)
4301 goto err_alloc_rings;
4302
Ioana Ciorneif395b692020-05-31 00:08:13 +03004303#ifdef CONFIG_FSL_DPAA2_ETH_DCB
4304 if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4305 priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4306 net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4307 } else {
4308 dev_dbg(dev, "PFC not supported\n");
4309 }
4310#endif
4311
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004312 err = dpaa2_eth_setup_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004313 if (err) {
4314 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004315 priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004316 "%s_poll_link", net_dev->name);
4317 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004318 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004319 goto err_poll_thread;
4320 }
4321 priv->do_link_poll = true;
4322 }
4323
Ioana Ciornei71947922019-10-31 01:18:31 +02004324 err = dpaa2_eth_connect_mac(priv);
4325 if (err)
4326 goto err_connect_mac;
4327
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004328 err = dpaa2_eth_dl_register(priv);
4329 if (err)
4330 goto err_dl_register;
4331
Ioana Ciornei061d6312020-10-01 18:11:48 +03004332 err = dpaa2_eth_dl_traps_register(priv);
4333 if (err)
4334 goto err_dl_trap_register;
4335
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004336 err = dpaa2_eth_dl_port_add(priv);
4337 if (err)
4338 goto err_dl_port_add;
4339
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004340 err = register_netdev(net_dev);
4341 if (err < 0) {
4342 dev_err(dev, "register_netdev() failed\n");
4343 goto err_netdev_reg;
4344 }
4345
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004346#ifdef CONFIG_DEBUG_FS
4347 dpaa2_dbg_add(priv);
4348#endif
4349
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004350 dev_info(dev, "Probed interface %s\n", net_dev->name);
4351 return 0;
4352
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004353err_netdev_reg:
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004354 dpaa2_eth_dl_port_del(priv);
4355err_dl_port_add:
Ioana Ciornei061d6312020-10-01 18:11:48 +03004356 dpaa2_eth_dl_traps_unregister(priv);
4357err_dl_trap_register:
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004358 dpaa2_eth_dl_unregister(priv);
4359err_dl_register:
Ioana Ciornei71947922019-10-31 01:18:31 +02004360 dpaa2_eth_disconnect_mac(priv);
4361err_connect_mac:
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004362 if (priv->do_link_poll)
4363 kthread_stop(priv->poll_thread);
4364 else
4365 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004366err_poll_thread:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004367 dpaa2_eth_free_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004368err_alloc_rings:
4369err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004370err_netdev_init:
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004371 free_percpu(priv->sgt_cache);
4372err_alloc_sgt_cache:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004373 free_percpu(priv->percpu_extras);
4374err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004375 free_percpu(priv->percpu_stats);
4376err_alloc_percpu_stats:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004377 dpaa2_eth_del_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004378err_bind:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004379 dpaa2_eth_free_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004380err_dpbp_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004381 dpaa2_eth_free_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004382err_dpio_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004383 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004384err_dpni_setup:
4385 fsl_mc_portal_free(priv->mc_io);
4386err_portal_alloc:
Yangbo Luc5521182020-09-18 17:08:02 +08004387 destroy_workqueue(priv->dpaa2_ptp_wq);
4388err_wq_alloc:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004389 dev_set_drvdata(dev, NULL);
4390 free_netdev(net_dev);
4391
4392 return err;
4393}
4394
4395static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4396{
4397 struct device *dev;
4398 struct net_device *net_dev;
4399 struct dpaa2_eth_priv *priv;
4400
4401 dev = &ls_dev->dev;
4402 net_dev = dev_get_drvdata(dev);
4403 priv = netdev_priv(net_dev);
4404
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004405#ifdef CONFIG_DEBUG_FS
4406 dpaa2_dbg_remove(priv);
4407#endif
Robert-Ionut Alexaf8edc6f2022-02-09 17:57:43 +02004408
4409 unregister_netdev(net_dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02004410 rtnl_lock();
4411 dpaa2_eth_disconnect_mac(priv);
4412 rtnl_unlock();
4413
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004414 dpaa2_eth_dl_port_del(priv);
Ioana Ciornei061d6312020-10-01 18:11:48 +03004415 dpaa2_eth_dl_traps_unregister(priv);
Ioana Ciorneiceeb03a2020-10-01 18:11:47 +03004416 dpaa2_eth_dl_unregister(priv);
4417
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004418 if (priv->do_link_poll)
4419 kthread_stop(priv->poll_thread);
4420 else
4421 fsl_mc_free_irqs(ls_dev);
4422
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004423 dpaa2_eth_free_rings(priv);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004424 free_percpu(priv->sgt_cache);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004425 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004426 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004427
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004428 dpaa2_eth_del_ch_napi(priv);
4429 dpaa2_eth_free_dpbp(priv);
4430 dpaa2_eth_free_dpio(priv);
4431 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004432
4433 fsl_mc_portal_free(priv->mc_io);
4434
Dongliang Mue26dab72021-11-30 12:05:54 +08004435 destroy_workqueue(priv->dpaa2_ptp_wq);
4436
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05004437 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05004438
Pavel Skripkin1c4099d2021-11-16 18:17:12 +03004439 free_netdev(net_dev);
4440
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004441 return 0;
4442}
4443
4444static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
4445 {
4446 .vendor = FSL_MC_VENDOR_FREESCALE,
4447 .obj_type = "dpni",
4448 },
4449 { .vendor = 0x0 }
4450};
4451MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
4452
4453static struct fsl_mc_driver dpaa2_eth_driver = {
4454 .driver = {
4455 .name = KBUILD_MODNAME,
4456 .owner = THIS_MODULE,
4457 },
4458 .probe = dpaa2_eth_probe,
4459 .remove = dpaa2_eth_remove,
4460 .match_id_table = dpaa2_eth_match_id_table
4461};
4462
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004463static int __init dpaa2_eth_driver_init(void)
4464{
4465 int err;
4466
4467 dpaa2_eth_dbg_init();
4468 err = fsl_mc_driver_register(&dpaa2_eth_driver);
4469 if (err) {
4470 dpaa2_eth_dbg_exit();
4471 return err;
4472 }
4473
4474 return 0;
4475}
4476
4477static void __exit dpaa2_eth_driver_exit(void)
4478{
4479 dpaa2_eth_dbg_exit();
4480 fsl_mc_driver_unregister(&dpaa2_eth_driver);
4481}
4482
4483module_init(dpaa2_eth_driver_init);
4484module_exit(dpaa2_eth_driver_exit);