blob: 48679940c2c9cdccaaa581e414d4103d14ee4f1b [file] [log] [blame]
Ioana Ciornei0bb29b22018-07-31 12:02:47 -05001// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002/* Copyright 2014-2016 Freescale Semiconductor Inc.
Ioana Ciornei48c04812020-04-22 15:05:10 +03003 * Copyright 2016-2020 NXP
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004 */
5#include <linux/init.h>
6#include <linux/module.h>
7#include <linux/platform_device.h>
8#include <linux/etherdevice.h>
9#include <linux/of_net.h>
10#include <linux/interrupt.h>
11#include <linux/msi.h>
12#include <linux/kthread.h>
Ioana Radulescu08eb2392017-05-24 07:13:27 -050013#include <linux/iommu.h>
Bogdan Purcareata6bd067c2018-02-05 08:07:42 -060014#include <linux/fsl/mc.h>
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +000015#include <linux/bpf.h>
16#include <linux/bpf_trace.h>
Yangbo Lud21c7842020-09-18 17:07:59 +080017#include <linux/fsl/ptp_qoriq.h>
Yangbo Luc5521182020-09-18 17:08:02 +080018#include <linux/ptp_classify.h>
Ioana Ciornei3657cda2020-07-21 19:38:25 +030019#include <net/pkt_cls.h>
Ioana Radulescu859f9982018-04-26 18:23:47 +080020#include <net/sock.h>
21
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050022#include "dpaa2-eth.h"
23
Ioana Radulescu56361872017-04-28 04:50:32 -050024/* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
25 * using trace events only need to #include <trace/events/sched.h>
26 */
27#define CREATE_TRACE_POINTS
28#include "dpaa2-eth-trace.h"
29
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050030MODULE_LICENSE("Dual BSD/GPL");
31MODULE_AUTHOR("Freescale Semiconductor, Inc");
32MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
33
Yangbo Lud21c7842020-09-18 17:07:59 +080034struct ptp_qoriq *dpaa2_ptp;
35EXPORT_SYMBOL(dpaa2_ptp);
36
Ioana Radulescu08eb2392017-05-24 07:13:27 -050037static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
38 dma_addr_t iova_addr)
39{
40 phys_addr_t phys_addr;
41
42 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
43
44 return phys_to_virt(phys_addr);
45}
46
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +030047static void dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv *priv,
48 u32 fd_status,
49 struct sk_buff *skb)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050050{
51 skb_checksum_none_assert(skb);
52
53 /* HW checksum validation is disabled, nothing to do here */
54 if (!(priv->net_dev->features & NETIF_F_RXCSUM))
55 return;
56
57 /* Read checksum validation bits */
58 if (!((fd_status & DPAA2_FAS_L3CV) &&
59 (fd_status & DPAA2_FAS_L4CV)))
60 return;
61
62 /* Inform the stack there's no need to compute L3/L4 csum anymore */
63 skb->ip_summed = CHECKSUM_UNNECESSARY;
64}
65
66/* Free a received FD.
67 * Not to be used for Tx conf FDs or on any other paths.
68 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +030069static void dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv,
70 const struct dpaa2_fd *fd,
71 void *vaddr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050072{
73 struct device *dev = priv->net_dev->dev.parent;
74 dma_addr_t addr = dpaa2_fd_get_addr(fd);
75 u8 fd_format = dpaa2_fd_get_format(fd);
76 struct dpaa2_sg_entry *sgt;
77 void *sg_vaddr;
78 int i;
79
80 /* If single buffer frame, just free the data buffer */
81 if (fd_format == dpaa2_fd_single)
82 goto free_buf;
83 else if (fd_format != dpaa2_fd_sg)
84 /* We don't support any other format */
85 return;
86
Ioana Radulescu729d79b2017-10-11 08:29:48 -050087 /* For S/G frames, we first need to free all SG entries
88 * except the first one, which was taken care of already
89 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050090 sgt = vaddr + dpaa2_fd_get_offset(fd);
Ioana Radulescu729d79b2017-10-11 08:29:48 -050091 for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050092 addr = dpaa2_sg_get_addr(&sgt[i]);
Ioana Radulescu08eb2392017-05-24 07:13:27 -050093 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +030094 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000095 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050096
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +000097 free_pages((unsigned long)sg_vaddr, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -050098 if (dpaa2_sg_is_final(&sgt[i]))
99 break;
100 }
101
102free_buf:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000103 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500104}
105
106/* Build a linear skb based on a single-buffer frame descriptor */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300107static struct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch,
108 const struct dpaa2_fd *fd,
109 void *fd_vaddr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500110{
111 struct sk_buff *skb = NULL;
112 u16 fd_offset = dpaa2_fd_get_offset(fd);
113 u32 fd_length = dpaa2_fd_get_len(fd);
114
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500115 ch->buf_count--;
116
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000117 skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500118 if (unlikely(!skb))
119 return NULL;
120
121 skb_reserve(skb, fd_offset);
122 skb_put(skb, fd_length);
123
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500124 return skb;
125}
126
127/* Build a non linear (fragmented) skb based on a S/G table */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300128static struct sk_buff *dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv *priv,
129 struct dpaa2_eth_channel *ch,
130 struct dpaa2_sg_entry *sgt)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500131{
132 struct sk_buff *skb = NULL;
133 struct device *dev = priv->net_dev->dev.parent;
134 void *sg_vaddr;
135 dma_addr_t sg_addr;
136 u16 sg_offset;
137 u32 sg_length;
138 struct page *page, *head_page;
139 int page_offset;
140 int i;
141
142 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
143 struct dpaa2_sg_entry *sge = &sgt[i];
144
145 /* NOTE: We only support SG entries in dpaa2_sg_single format,
146 * but this is the only format we may receive from HW anyway
147 */
148
149 /* Get the address and length from the S/G entry */
150 sg_addr = dpaa2_sg_get_addr(sge);
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500151 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300152 dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000153 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500154
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500155 sg_length = dpaa2_sg_get_len(sge);
156
157 if (i == 0) {
158 /* We build the skb around the first data buffer */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000159 skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500160 if (unlikely(!skb)) {
Ioana Radulescu729d79b2017-10-11 08:29:48 -0500161 /* Free the first SG entry now, since we already
162 * unmapped it and obtained the virtual address
163 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000164 free_pages((unsigned long)sg_vaddr, 0);
Ioana Radulescu729d79b2017-10-11 08:29:48 -0500165
Ioana Radulescucbb3ea42017-10-11 08:29:44 -0500166 /* We still need to subtract the buffers used
167 * by this FD from our software counter
168 */
169 while (!dpaa2_sg_is_final(&sgt[i]) &&
170 i < DPAA2_ETH_MAX_SG_ENTRIES)
171 i++;
172 break;
173 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500174
175 sg_offset = dpaa2_sg_get_offset(sge);
176 skb_reserve(skb, sg_offset);
177 skb_put(skb, sg_length);
178 } else {
179 /* Rest of the data buffers are stored as skb frags */
180 page = virt_to_page(sg_vaddr);
181 head_page = virt_to_head_page(sg_vaddr);
182
183 /* Offset in page (which may be compound).
184 * Data in subsequent SG entries is stored from the
185 * beginning of the buffer, so we don't need to add the
186 * sg_offset.
187 */
188 page_offset = ((unsigned long)sg_vaddr &
189 (PAGE_SIZE - 1)) +
190 (page_address(page) - page_address(head_page));
191
192 skb_add_rx_frag(skb, i - 1, head_page, page_offset,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300193 sg_length, priv->rx_buf_size);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500194 }
195
196 if (dpaa2_sg_is_final(sge))
197 break;
198 }
199
Ioana Radulescub63baf72017-10-11 08:29:45 -0500200 WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
201
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500202 /* Count all data buffers + SG table buffer */
203 ch->buf_count -= i + 2;
204
205 return skb;
206}
207
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000208/* Free buffers acquired from the buffer pool or which were meant to
209 * be released in the pool
210 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300211static void dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array,
212 int count)
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000213{
214 struct device *dev = priv->net_dev->dev.parent;
215 void *vaddr;
216 int i;
217
218 for (i = 0; i < count; i++) {
219 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300220 dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000221 DMA_BIDIRECTIONAL);
222 free_pages((unsigned long)vaddr, 0);
Ioana Ciocoi Radulescu569375f2018-11-26 16:27:31 +0000223 }
224}
225
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300226static void dpaa2_eth_xdp_release_buf(struct dpaa2_eth_priv *priv,
227 struct dpaa2_eth_channel *ch,
228 dma_addr_t addr)
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000229{
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300230 int retries = 0;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000231 int err;
232
233 ch->xdp.drop_bufs[ch->xdp.drop_cnt++] = addr;
234 if (ch->xdp.drop_cnt < DPAA2_ETH_BUFS_PER_CMD)
235 return;
236
237 while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
238 ch->xdp.drop_bufs,
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300239 ch->xdp.drop_cnt)) == -EBUSY) {
240 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
241 break;
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000242 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300243 }
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000244
245 if (err) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300246 dpaa2_eth_free_bufs(priv, ch->xdp.drop_bufs, ch->xdp.drop_cnt);
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000247 ch->buf_count -= ch->xdp.drop_cnt;
248 }
249
250 ch->xdp.drop_cnt = 0;
251}
252
Ioana Ciornei38c440b2020-05-06 20:47:17 +0300253static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
254 struct dpaa2_eth_fq *fq,
255 struct dpaa2_eth_xdp_fds *xdp_fds)
256{
257 int total_enqueued = 0, retries = 0, enqueued;
258 struct dpaa2_eth_drv_stats *percpu_extras;
259 int num_fds, err, max_retries;
260 struct dpaa2_fd *fds;
261
262 percpu_extras = this_cpu_ptr(priv->percpu_extras);
263
264 /* try to enqueue all the FDs until the max number of retries is hit */
265 fds = xdp_fds->fds;
266 num_fds = xdp_fds->num;
267 max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
268 while (total_enqueued < num_fds && retries < max_retries) {
269 err = priv->enqueue(priv, fq, &fds[total_enqueued],
270 0, num_fds - total_enqueued, &enqueued);
271 if (err == -EBUSY) {
272 percpu_extras->tx_portal_busy += ++retries;
273 continue;
274 }
275 total_enqueued += enqueued;
276 }
277 xdp_fds->num = 0;
278
279 return total_enqueued;
280}
281
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300282static void dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv *priv,
283 struct dpaa2_eth_channel *ch,
284 struct dpaa2_eth_fq *fq)
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000285{
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300286 struct rtnl_link_stats64 *percpu_stats;
287 struct dpaa2_fd *fds;
288 int enqueued, i;
289
290 percpu_stats = this_cpu_ptr(priv->percpu_stats);
291
292 // enqueue the array of XDP_TX frames
293 enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);
294
295 /* update statistics */
296 percpu_stats->tx_packets += enqueued;
297 fds = fq->xdp_tx_fds.fds;
298 for (i = 0; i < enqueued; i++) {
299 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
300 ch->stats.xdp_tx++;
301 }
302 for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300303 dpaa2_eth_xdp_release_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300304 percpu_stats->tx_errors++;
305 ch->stats.xdp_tx_err++;
306 }
307 fq->xdp_tx_fds.num = 0;
308}
309
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300310static void dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv *priv,
311 struct dpaa2_eth_channel *ch,
312 struct dpaa2_fd *fd,
313 void *buf_start, u16 queue_id)
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300314{
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000315 struct dpaa2_faead *faead;
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300316 struct dpaa2_fd *dest_fd;
317 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000318 u32 ctrl, frc;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000319
320 /* Mark the egress frame hardware annotation area as valid */
321 frc = dpaa2_fd_get_frc(fd);
322 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
323 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
324
325 /* Instruct hardware to release the FD buffer directly into
326 * the buffer pool once transmission is completed, instead of
327 * sending a Tx confirmation frame to us
328 */
329 ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
330 faead = dpaa2_get_faead(buf_start, false);
331 faead->ctrl = cpu_to_le32(ctrl);
332 faead->conf_fqid = 0;
333
334 fq = &priv->fq[queue_id];
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300335 dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
336 memcpy(dest_fd, fd, sizeof(*dest_fd));
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000337
Ioana Ciornei74a1c052020-05-13 16:55:46 +0300338 if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
339 return;
340
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300341 dpaa2_eth_xdp_tx_flush(priv, ch, fq);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000342}
343
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300344static u32 dpaa2_eth_run_xdp(struct dpaa2_eth_priv *priv,
345 struct dpaa2_eth_channel *ch,
346 struct dpaa2_eth_fq *rx_fq,
347 struct dpaa2_fd *fd, void *vaddr)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000348{
Ioana Ciocoi Radulescu5d39dc22018-11-26 16:27:31 +0000349 dma_addr_t addr = dpaa2_fd_get_addr(fd);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000350 struct bpf_prog *xdp_prog;
351 struct xdp_buff xdp;
352 u32 xdp_act = XDP_PASS;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000353 int err;
354
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000355 rcu_read_lock();
356
357 xdp_prog = READ_ONCE(ch->xdp.prog);
358 if (!xdp_prog)
359 goto out;
360
361 xdp.data = vaddr + dpaa2_fd_get_offset(fd);
362 xdp.data_end = xdp.data + dpaa2_fd_get_len(fd);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000363 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000364 xdp_set_data_meta_invalid(&xdp);
Ioana Radulescud678be12019-03-01 17:47:24 +0000365 xdp.rxq = &ch->xdp_rxq;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000366
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200367 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE -
368 (dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM);
369
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000370 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
371
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +0000372 /* xdp.data pointer may have changed */
373 dpaa2_fd_set_offset(fd, xdp.data - vaddr);
374 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
375
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000376 switch (xdp_act) {
377 case XDP_PASS:
378 break;
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000379 case XDP_TX:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300380 dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
Ioana Ciocoi Radulescu99e43522018-11-26 16:27:33 +0000381 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000382 default:
383 bpf_warn_invalid_xdp_action(xdp_act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500384 fallthrough;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000385 case XDP_ABORTED:
386 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500387 fallthrough;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000388 case XDP_DROP:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300389 dpaa2_eth_xdp_release_buf(priv, ch, addr);
Ioana Ciocoi Radulescua4a7b762018-11-26 16:27:34 +0000390 ch->stats.xdp_drop++;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000391 break;
Ioana Radulescud678be12019-03-01 17:47:24 +0000392 case XDP_REDIRECT:
393 dma_unmap_page(priv->net_dev->dev.parent, addr,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300394 priv->rx_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescud678be12019-03-01 17:47:24 +0000395 ch->buf_count--;
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200396
397 /* Allow redirect use of full headroom */
Ioana Radulescud678be12019-03-01 17:47:24 +0000398 xdp.data_hard_start = vaddr;
Jesper Dangaard Brouer4a9b0522020-05-14 12:49:53 +0200399 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
400
Ioana Radulescud678be12019-03-01 17:47:24 +0000401 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
402 if (unlikely(err))
403 ch->stats.xdp_drop++;
404 else
405 ch->stats.xdp_redirect++;
406 break;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000407 }
408
Ioana Radulescud678be12019-03-01 17:47:24 +0000409 ch->xdp.res |= xdp_act;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000410out:
411 rcu_read_unlock();
412 return xdp_act;
413}
414
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500415/* Main Rx frame processing routine */
416static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
417 struct dpaa2_eth_channel *ch,
418 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000419 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500420{
421 dma_addr_t addr = dpaa2_fd_get_addr(fd);
422 u8 fd_format = dpaa2_fd_get_format(fd);
423 void *vaddr;
424 struct sk_buff *skb;
425 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500426 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500427 struct device *dev = priv->net_dev->dev.parent;
428 struct dpaa2_fas *fas;
Ioana Radulescud695e762017-06-06 10:00:35 -0500429 void *buf_data;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500430 u32 status = 0;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000431 u32 xdp_act;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500432
Ioana Radulescu56361872017-04-28 04:50:32 -0500433 /* Tracing point */
434 trace_dpaa2_rx_fd(priv->net_dev, fd);
435
Ioana Radulescu08eb2392017-05-24 07:13:27 -0500436 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300437 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu18c2e772018-11-26 16:27:32 +0000438 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500439
Ioana Radulescu54ce8912017-12-08 06:47:53 -0600440 fas = dpaa2_get_fas(vaddr, false);
Ioana Radulescud695e762017-06-06 10:00:35 -0500441 prefetch(fas);
442 buf_data = vaddr + dpaa2_fd_get_offset(fd);
443 prefetch(buf_data);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500444
445 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500446 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500447
448 if (fd_format == dpaa2_fd_single) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300449 xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000450 if (xdp_act != XDP_PASS) {
451 percpu_stats->rx_packets++;
452 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
453 return;
454 }
455
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300456 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000457 DMA_BIDIRECTIONAL);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300458 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500459 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +0000460 WARN_ON(priv->xdp_prog);
461
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +0300462 dma_unmap_page(dev, addr, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000463 DMA_BIDIRECTIONAL);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300464 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +0000465 free_pages((unsigned long)vaddr, 0);
Ioana Radulescu85047ab2017-04-28 04:50:31 -0500466 percpu_extras->rx_sg_frames++;
467 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500468 } else {
469 /* We don't support any other format */
470 goto err_frame_format;
471 }
472
473 if (unlikely(!skb))
474 goto err_build_skb;
475
476 prefetch(skb->data);
477
Ioana Radulescu859f9982018-04-26 18:23:47 +0800478 /* Get the timestamp value */
479 if (priv->rx_tstamp) {
480 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
481 __le64 *ts = dpaa2_get_ts(vaddr, false);
482 u64 ns;
483
484 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
485
486 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
487 shhwtstamps->hwtstamp = ns_to_ktime(ns);
488 }
489
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500490 /* Check if we need to validate the L4 csum */
491 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500492 status = le32_to_cpu(fas->status);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300493 dpaa2_eth_validate_rx_csum(priv, status, skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500494 }
495
496 skb->protocol = eth_type_trans(skb, priv->net_dev);
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000497 skb_record_rx_queue(skb, fq->flowid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500498
499 percpu_stats->rx_packets++;
500 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
501
Ioana Ciornei0a25d922019-03-25 13:42:39 +0000502 list_add_tail(&skb->list, ch->rx_list);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500503
504 return;
505
506err_build_skb:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300507 dpaa2_eth_free_rx_fd(priv, fd, vaddr);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500508err_frame_format:
509 percpu_stats->rx_dropped++;
510}
511
512/* Consume all frames pull-dequeued into the store. This is the simplest way to
513 * make sure we don't accidentally issue another volatile dequeue which would
514 * overwrite (leak) frames already in the store.
515 *
516 * Observance of NAPI budget is not our concern, leaving that to the caller.
517 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300518static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
519 struct dpaa2_eth_fq **src)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500520{
521 struct dpaa2_eth_priv *priv = ch->priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000522 struct dpaa2_eth_fq *fq = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500523 struct dpaa2_dq *dq;
524 const struct dpaa2_fd *fd;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300525 int cleaned = 0, retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500526 int is_last;
527
528 do {
529 dq = dpaa2_io_store_next(ch->store, &is_last);
530 if (unlikely(!dq)) {
531 /* If we're here, we *must* have placed a
532 * volatile dequeue comnmand, so keep reading through
533 * the store until we get some sort of valid response
534 * token (either a valid frame or an "empty dequeue")
535 */
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300536 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
537 netdev_err_once(priv->net_dev,
538 "Unable to read a valid dequeue response\n");
539 return -ETIMEDOUT;
540 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500541 continue;
542 }
543
544 fd = dpaa2_dq_fd(dq);
Ioana Radulescu75c583a2018-02-26 10:28:06 -0600545 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500546
Ioana Ciocoi Radulescudbcdf722018-11-14 11:48:35 +0000547 fq->consume(priv, ch, fd, fq);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500548 cleaned++;
Ioana Radulescuef17bd72019-10-07 14:38:28 +0300549 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500550 } while (!is_last);
551
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000552 if (!cleaned)
553 return 0;
554
555 fq->stats.frames += cleaned;
Ioana Ciornei460fd832020-04-24 12:33:18 +0300556 ch->stats.frames += cleaned;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000557
558 /* A dequeue operation only pulls frames from a single queue
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000559 * into the store. Return the frame queue as an out param.
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000560 */
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +0000561 if (src)
562 *src = fq;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +0000563
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500564 return cleaned;
565}
566
Yangbo Luc5521182020-09-18 17:08:02 +0800567static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
568 u8 *msgtype, u8 *twostep, u8 *udp,
569 u16 *correction_offset,
570 u16 *origintimestamp_offset)
Ioana Radulescu859f9982018-04-26 18:23:47 +0800571{
Yangbo Luc5521182020-09-18 17:08:02 +0800572 unsigned int ptp_class;
573 struct ptp_header *hdr;
574 unsigned int type;
575 u8 *base;
576
577 ptp_class = ptp_classify_raw(skb);
578 if (ptp_class == PTP_CLASS_NONE)
579 return -EINVAL;
580
581 hdr = ptp_parse_header(skb, ptp_class);
582 if (!hdr)
583 return -EINVAL;
584
585 *msgtype = ptp_get_msgtype(hdr, ptp_class);
586 *twostep = hdr->flag_field[0] & 0x2;
587
588 type = ptp_class & PTP_CLASS_PMASK;
589 if (type == PTP_CLASS_IPV4 ||
590 type == PTP_CLASS_IPV6)
591 *udp = 1;
592 else
593 *udp = 0;
594
595 base = skb_mac_header(skb);
596 *correction_offset = (u8 *)&hdr->correction - base;
597 *origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
598
599 return 0;
600}
601
602/* Configure the egress frame annotation for timestamp update */
603static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
604 struct dpaa2_fd *fd,
605 void *buf_start,
606 struct sk_buff *skb)
607{
608 struct ptp_tstamp origin_timestamp;
609 struct dpni_single_step_cfg cfg;
610 u8 msgtype, twostep, udp;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800611 struct dpaa2_faead *faead;
Yangbo Luc5521182020-09-18 17:08:02 +0800612 struct dpaa2_fas *fas;
613 struct timespec64 ts;
614 u16 offset1, offset2;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800615 u32 ctrl, frc;
Yangbo Luc5521182020-09-18 17:08:02 +0800616 __le64 *ns;
617 u8 *data;
Ioana Radulescu859f9982018-04-26 18:23:47 +0800618
619 /* Mark the egress frame annotation area as valid */
620 frc = dpaa2_fd_get_frc(fd);
621 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
622
623 /* Set hardware annotation size */
624 ctrl = dpaa2_fd_get_ctrl(fd);
625 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
626
627 /* enable UPD (update prepanded data) bit in FAEAD field of
628 * hardware frame annotation area
629 */
630 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
631 faead = dpaa2_get_faead(buf_start, true);
632 faead->ctrl = cpu_to_le32(ctrl);
Yangbo Luc5521182020-09-18 17:08:02 +0800633
634 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
635 if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
636 &offset1, &offset2) ||
637 msgtype != 0 || twostep) {
638 WARN_ONCE(1, "Bad packet for one-step timestamping\n");
639 return;
640 }
641
642 /* Mark the frame annotation status as valid */
643 frc = dpaa2_fd_get_frc(fd);
644 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);
645
646 /* Mark the PTP flag for one step timestamping */
647 fas = dpaa2_get_fas(buf_start, true);
648 fas->status = cpu_to_le32(DPAA2_FAS_PTP);
649
650 dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
651 ns = dpaa2_get_ts(buf_start, true);
652 *ns = cpu_to_le64(timespec64_to_ns(&ts) /
653 DPAA2_PTP_CLK_PERIOD_NS);
654
655 /* Update current time to PTP message originTimestamp field */
656 ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
657 data = skb_mac_header(skb);
658 *(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
659 *(__be32 *)(data + offset2 + 2) =
660 htonl(origin_timestamp.sec_lsb);
661 *(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);
662
663 cfg.en = 1;
664 cfg.ch_update = udp;
665 cfg.offset = offset1;
666 cfg.peer_delay = 0;
667
668 if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token,
669 &cfg))
670 WARN_ONCE(1, "Failed to set single step register");
671 }
Ioana Radulescu859f9982018-04-26 18:23:47 +0800672}
673
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500674/* Create a frame descriptor based on a fragmented skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300675static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
676 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800677 struct dpaa2_fd *fd,
678 void **swa_addr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500679{
680 struct device *dev = priv->net_dev->dev.parent;
681 void *sgt_buf = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500682 dma_addr_t addr;
683 int nr_frags = skb_shinfo(skb)->nr_frags;
684 struct dpaa2_sg_entry *sgt;
685 int i, err;
686 int sgt_buf_size;
687 struct scatterlist *scl, *crt_scl;
688 int num_sg;
689 int num_dma_bufs;
690 struct dpaa2_eth_swa *swa;
691
692 /* Create and map scatterlist.
693 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
694 * to go beyond nr_frags+1.
695 * Note: We don't support chained scatterlists
696 */
697 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
698 return -EINVAL;
699
Julia Lawalld4ceb8d2020-09-20 13:26:15 +0200700 scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500701 if (unlikely(!scl))
702 return -ENOMEM;
703
704 sg_init_table(scl, nr_frags + 1);
705 num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
Ioana Ciornei37fbbdda2020-06-24 14:34:18 +0300706 if (unlikely(num_sg < 0)) {
707 err = -ENOMEM;
708 goto dma_map_sg_failed;
709 }
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500710 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500711 if (unlikely(!num_dma_bufs)) {
712 err = -ENOMEM;
713 goto dma_map_sg_failed;
714 }
715
716 /* Prepare the HW SGT structure */
717 sgt_buf_size = priv->tx_data_offset +
Ioana Radulescufa722c02018-03-23 08:44:12 -0500718 sizeof(struct dpaa2_sg_entry) * num_dma_bufs;
Sebastian Andrzej Siewior90bc6d42019-06-07 21:20:37 +0200719 sgt_buf = napi_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500720 if (unlikely(!sgt_buf)) {
721 err = -ENOMEM;
722 goto sgt_buf_alloc_failed;
723 }
724 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500725 memset(sgt_buf, 0, sgt_buf_size);
726
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500727 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
728
729 /* Fill in the HW SGT structure.
730 *
731 * sgt_buf is zeroed out, so the following fields are implicit
732 * in all sgt entries:
733 * - offset is 0
734 * - format is 'dpaa2_sg_single'
735 */
736 for_each_sg(scl, crt_scl, num_dma_bufs, i) {
737 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
738 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
739 }
740 dpaa2_sg_set_final(&sgt[i - 1], true);
741
742 /* Store the skb backpointer in the SGT buffer.
743 * Fit the scatterlist and the number of buffers alongside the
744 * skb backpointer in the software annotation area. We'll need
745 * all of them on Tx Conf.
746 */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800747 *swa_addr = (void *)sgt_buf;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500748 swa = (struct dpaa2_eth_swa *)sgt_buf;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000749 swa->type = DPAA2_ETH_SWA_SG;
750 swa->sg.skb = skb;
751 swa->sg.scl = scl;
752 swa->sg.num_sg = num_sg;
753 swa->sg.sgt_size = sgt_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500754
755 /* Separately map the SGT buffer */
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500756 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500757 if (unlikely(dma_mapping_error(dev, addr))) {
758 err = -ENOMEM;
759 goto dma_map_single_failed;
760 }
761 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
762 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
763 dpaa2_fd_set_addr(fd, addr);
764 dpaa2_fd_set_len(fd, skb->len);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000765 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500766
767 return 0;
768
769dma_map_single_failed:
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500770 skb_free_frag(sgt_buf);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500771sgt_buf_alloc_failed:
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500772 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500773dma_map_sg_failed:
774 kfree(scl);
775 return err;
776}
777
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300778/* Create a SG frame descriptor based on a linear skb.
779 *
780 * This function is used on the Tx path when the skb headroom is not large
781 * enough for the HW requirements, thus instead of realloc-ing the skb we
782 * create a SG frame descriptor with only one entry.
783 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300784static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
785 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800786 struct dpaa2_fd *fd,
787 void **swa_addr)
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300788{
789 struct device *dev = priv->net_dev->dev.parent;
790 struct dpaa2_eth_sgt_cache *sgt_cache;
791 struct dpaa2_sg_entry *sgt;
792 struct dpaa2_eth_swa *swa;
793 dma_addr_t addr, sgt_addr;
794 void *sgt_buf = NULL;
795 int sgt_buf_size;
796 int err;
797
798 /* Prepare the HW SGT structure */
799 sgt_cache = this_cpu_ptr(priv->sgt_cache);
800 sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
801
802 if (sgt_cache->count == 0)
803 sgt_buf = kzalloc(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN,
804 GFP_ATOMIC);
805 else
806 sgt_buf = sgt_cache->buf[--sgt_cache->count];
807 if (unlikely(!sgt_buf))
808 return -ENOMEM;
809
810 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
811 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
812
813 addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
814 if (unlikely(dma_mapping_error(dev, addr))) {
815 err = -ENOMEM;
816 goto data_map_failed;
817 }
818
819 /* Fill in the HW SGT structure */
820 dpaa2_sg_set_addr(sgt, addr);
821 dpaa2_sg_set_len(sgt, skb->len);
822 dpaa2_sg_set_final(sgt, true);
823
824 /* Store the skb backpointer in the SGT buffer */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800825 *swa_addr = (void *)sgt_buf;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300826 swa = (struct dpaa2_eth_swa *)sgt_buf;
827 swa->type = DPAA2_ETH_SWA_SINGLE;
828 swa->single.skb = skb;
829 swa->sg.sgt_size = sgt_buf_size;
830
831 /* Separately map the SGT buffer */
832 sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
833 if (unlikely(dma_mapping_error(dev, sgt_addr))) {
834 err = -ENOMEM;
835 goto sgt_map_failed;
836 }
837
838 dpaa2_fd_set_offset(fd, priv->tx_data_offset);
839 dpaa2_fd_set_format(fd, dpaa2_fd_sg);
840 dpaa2_fd_set_addr(fd, sgt_addr);
841 dpaa2_fd_set_len(fd, skb->len);
842 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
843
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300844 return 0;
845
846sgt_map_failed:
847 dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
848data_map_failed:
849 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
850 kfree(sgt_buf);
851 else
852 sgt_cache->buf[sgt_cache->count++] = sgt_buf;
853
854 return err;
855}
856
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500857/* Create a frame descriptor based on a linear skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300858static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
859 struct sk_buff *skb,
Yangbo Lu64a965d2020-09-18 17:08:00 +0800860 struct dpaa2_fd *fd,
861 void **swa_addr)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500862{
863 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescuc1636852017-12-08 06:47:58 -0600864 u8 *buffer_start, *aligned_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000865 struct dpaa2_eth_swa *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500866 dma_addr_t addr;
867
Yangbo Lu1cf773b2020-09-18 17:08:01 +0800868 buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
Ioana Radulescuc1636852017-12-08 06:47:58 -0600869
870 /* If there's enough room to align the FD address, do it.
871 * It will help hardware optimize accesses.
872 */
873 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
874 DPAA2_ETH_TX_BUF_ALIGN);
875 if (aligned_start >= skb->head)
876 buffer_start = aligned_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500877
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500878 /* Store a backpointer to the skb at the beginning of the buffer
879 * (in the private data area) such that we can release it
880 * on Tx confirm
881 */
Yangbo Lu64a965d2020-09-18 17:08:00 +0800882 *swa_addr = (void *)buffer_start;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000883 swa = (struct dpaa2_eth_swa *)buffer_start;
884 swa->type = DPAA2_ETH_SWA_SINGLE;
885 swa->single.skb = skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500886
887 addr = dma_map_single(dev, buffer_start,
888 skb_tail_pointer(skb) - buffer_start,
Ioana Radulescu1e5fa9e2017-05-24 07:13:28 -0500889 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500890 if (unlikely(dma_mapping_error(dev, addr)))
891 return -ENOMEM;
892
893 dpaa2_fd_set_addr(fd, addr);
894 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
895 dpaa2_fd_set_len(fd, skb->len);
896 dpaa2_fd_set_format(fd, dpaa2_fd_single);
Ioana Radulescub948c8c2018-10-12 16:27:40 +0000897 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500898
899 return 0;
900}
901
902/* FD freeing routine on the Tx path
903 *
904 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
905 * back-pointed to is also freed.
906 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
907 * dpaa2_eth_tx().
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500908 */
Yangbo Luc5521182020-09-18 17:08:02 +0800909static void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +0300910 struct dpaa2_eth_fq *fq,
911 const struct dpaa2_fd *fd, bool in_napi)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500912{
913 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300914 dma_addr_t fd_addr, sg_addr;
Ioana Radulescud678be12019-03-01 17:47:24 +0000915 struct sk_buff *skb = NULL;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500916 unsigned char *buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500917 struct dpaa2_eth_swa *swa;
918 u8 fd_format = dpaa2_fd_get_format(fd);
Ioana Radulescud678be12019-03-01 17:47:24 +0000919 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500920
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300921 struct dpaa2_eth_sgt_cache *sgt_cache;
922 struct dpaa2_sg_entry *sgt;
923
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500924 fd_addr = dpaa2_fd_get_addr(fd);
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000925 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
926 swa = (struct dpaa2_eth_swa *)buffer_start;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500927
928 if (fd_format == dpaa2_fd_single) {
Ioana Radulescud678be12019-03-01 17:47:24 +0000929 if (swa->type == DPAA2_ETH_SWA_SINGLE) {
930 skb = swa->single.skb;
931 /* Accessing the skb buffer is safe before dma unmap,
932 * because we didn't map the actual skb shell.
933 */
934 dma_unmap_single(dev, fd_addr,
935 skb_tail_pointer(skb) - buffer_start,
936 DMA_BIDIRECTIONAL);
937 } else {
938 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
939 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
940 DMA_BIDIRECTIONAL);
941 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500942 } else if (fd_format == dpaa2_fd_sg) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300943 if (swa->type == DPAA2_ETH_SWA_SG) {
944 skb = swa->sg.skb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500945
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300946 /* Unmap the scatterlist */
947 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
948 DMA_BIDIRECTIONAL);
949 kfree(swa->sg.scl);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500950
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300951 /* Unmap the SGT buffer */
952 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
953 DMA_BIDIRECTIONAL);
954 } else {
955 skb = swa->single.skb;
956
957 /* Unmap the SGT Buffer */
958 dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
959 DMA_BIDIRECTIONAL);
960
961 sgt = (struct dpaa2_sg_entry *)(buffer_start +
962 priv->tx_data_offset);
963 sg_addr = dpaa2_sg_get_addr(sgt);
964 dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
965 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500966 } else {
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -0600967 netdev_dbg(priv->net_dev, "Invalid FD format\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -0500968 return;
969 }
970
Ioana Radulescud678be12019-03-01 17:47:24 +0000971 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
972 fq->dq_frames++;
973 fq->dq_bytes += fd_len;
974 }
975
976 if (swa->type == DPAA2_ETH_SWA_XDP) {
977 xdp_return_frame(swa->xdp.xdpf);
978 return;
979 }
980
Ioana Radulescu859f9982018-04-26 18:23:47 +0800981 /* Get the timestamp value */
Yangbo Lu1cf773b2020-09-18 17:08:01 +0800982 if (skb->cb[0] == TX_TSTAMP) {
Ioana Radulescu859f9982018-04-26 18:23:47 +0800983 struct skb_shared_hwtstamps shhwtstamps;
Ioana Radulescue3fdf6b2019-03-01 17:47:23 +0000984 __le64 *ts = dpaa2_get_ts(buffer_start, true);
Ioana Radulescu859f9982018-04-26 18:23:47 +0800985 u64 ns;
986
987 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
988
989 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
990 shhwtstamps.hwtstamp = ns_to_ktime(ns);
991 skb_tstamp_tx(skb, &shhwtstamps);
Yangbo Luc5521182020-09-18 17:08:02 +0800992 } else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
993 mutex_unlock(&priv->onestep_tstamp_lock);
Ioana Radulescu859f9982018-04-26 18:23:47 +0800994 }
995
Ioana Radulescu6a9bbe52018-03-14 15:04:51 -0500996 /* Free SGT buffer allocated on tx */
Ioana Ciorneid70446e2020-06-29 21:47:11 +0300997 if (fd_format != dpaa2_fd_single) {
998 sgt_cache = this_cpu_ptr(priv->sgt_cache);
999 if (swa->type == DPAA2_ETH_SWA_SG) {
1000 skb_free_frag(buffer_start);
1001 } else {
1002 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
1003 kfree(buffer_start);
1004 else
1005 sgt_cache->buf[sgt_cache->count++] = buffer_start;
1006 }
1007 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001008
1009 /* Move on with skb release */
Ioana Ciocoi Radulescu0723a3a2019-02-04 17:00:35 +00001010 napi_consume_skb(skb, in_napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001011}
1012
Yangbo Luc5521182020-09-18 17:08:02 +08001013static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1014 struct net_device *net_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001015{
1016 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1017 struct dpaa2_fd fd;
1018 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001019 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001020 struct dpaa2_eth_fq *fq;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001021 struct netdev_queue *nq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001022 u16 queue_mapping;
Ioana Radulescu18c21462017-12-08 06:47:57 -06001023 unsigned int needed_headroom;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001024 u32 fd_len;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001025 u8 prio = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001026 int err, i;
Yangbo Lu64a965d2020-09-18 17:08:00 +08001027 void *swa;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001028
1029 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001030 percpu_extras = this_cpu_ptr(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001031
Yangbo Lu1cf773b2020-09-18 17:08:01 +08001032 needed_headroom = dpaa2_eth_needed_headroom(skb);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001033
1034 /* We'll be holding a back-reference to the skb until Tx Confirmation;
1035 * we don't want that overwritten by a concurrent Tx with a cloned skb.
1036 */
1037 skb = skb_unshare(skb, GFP_ATOMIC);
1038 if (unlikely(!skb)) {
1039 /* skb_unshare() has already freed the skb */
1040 percpu_stats->tx_dropped++;
1041 return NETDEV_TX_OK;
1042 }
1043
1044 /* Setup the FD fields */
1045 memset(&fd, 0, sizeof(fd));
1046
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001047 if (skb_is_nonlinear(skb)) {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001048 err = dpaa2_eth_build_sg_fd(priv, skb, &fd, &swa);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001049 percpu_extras->tx_sg_frames++;
1050 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001051 } else if (skb_headroom(skb) < needed_headroom) {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001052 err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, &fd, &swa);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001053 percpu_extras->tx_sg_frames++;
1054 percpu_extras->tx_sg_bytes += skb->len;
Ioana Ciornei4c96c0a2020-06-29 21:47:12 +03001055 percpu_extras->tx_converted_sg_frames++;
1056 percpu_extras->tx_converted_sg_bytes += skb->len;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001057 } else {
Yangbo Lu64a965d2020-09-18 17:08:00 +08001058 err = dpaa2_eth_build_single_fd(priv, skb, &fd, &swa);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001059 }
1060
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001061 if (unlikely(err)) {
1062 percpu_stats->tx_dropped++;
1063 goto err_build_fd;
1064 }
1065
Yangbo Luc5521182020-09-18 17:08:02 +08001066 if (skb->cb[0])
1067 dpaa2_eth_enable_tx_tstamp(priv, &fd, swa, skb);
Yangbo Lu64a965d2020-09-18 17:08:00 +08001068
Ioana Radulescu56361872017-04-28 04:50:32 -05001069 /* Tracing point */
1070 trace_dpaa2_tx_fd(net_dev, &fd);
1071
Ioana Radulescu537336c2017-12-21 06:33:20 -06001072 /* TxConf FQ selection relies on queue id from the stack.
1073 * In case of a forwarded frame from another DPNI interface, we choose
1074 * a queue affined to the same core that processed the Rx frame
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001075 */
Ioana Radulescu537336c2017-12-21 06:33:20 -06001076 queue_mapping = skb_get_queue_mapping(skb);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03001077
1078 if (net_dev->num_tc) {
1079 prio = netdev_txq_to_tc(net_dev, queue_mapping);
1080 /* Hardware interprets priority level 0 as being the highest,
1081 * so we need to do a reverse mapping to the netdev tc index
1082 */
1083 prio = net_dev->num_tc - prio - 1;
1084 /* We have only one FQ array entry for all Tx hardware queues
1085 * with the same flow id (but different priority levels)
1086 */
1087 queue_mapping %= dpaa2_eth_queue_count(priv);
1088 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001089 fq = &priv->fq[queue_mapping];
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001090
1091 fd_len = dpaa2_fd_get_len(&fd);
1092 nq = netdev_get_tx_queue(net_dev, queue_mapping);
1093 netdev_tx_sent_queue(nq, fd_len);
1094
1095 /* Everything that happens after this enqueues might race with
1096 * the Tx confirmation callback for this frame
1097 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001098 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
Ioana Ciornei6ff80442020-04-22 15:05:11 +03001099 err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001100 if (err != -EBUSY)
1101 break;
1102 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001103 percpu_extras->tx_portal_busy += i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001104 if (unlikely(err < 0)) {
1105 percpu_stats->tx_errors++;
1106 /* Clean up everything, including freeing the skb */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001107 dpaa2_eth_free_tx_fd(priv, fq, &fd, false);
Ioana Ciornei8c838f52019-03-25 13:06:22 +00001108 netdev_tx_completed_queue(nq, 1, fd_len);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001109 } else {
1110 percpu_stats->tx_packets++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001111 percpu_stats->tx_bytes += fd_len;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001112 }
1113
1114 return NETDEV_TX_OK;
1115
1116err_build_fd:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001117 dev_kfree_skb(skb);
1118
1119 return NETDEV_TX_OK;
1120}
1121
Yangbo Luc5521182020-09-18 17:08:02 +08001122static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1123{
1124 struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1125 tx_onestep_tstamp);
1126 struct sk_buff *skb;
1127
1128 while (true) {
1129 skb = skb_dequeue(&priv->tx_skbs);
1130 if (!skb)
1131 return;
1132
1133 /* Lock just before TX one-step timestamping packet,
1134 * and release the lock in dpaa2_eth_free_tx_fd when
1135 * confirm the packet has been sent on hardware, or
1136 * when clean up during transmit failure.
1137 */
1138 mutex_lock(&priv->onestep_tstamp_lock);
1139 __dpaa2_eth_tx(skb, priv->net_dev);
1140 }
1141}
1142
1143static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1144{
1145 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1146 u8 msgtype, twostep, udp;
1147 u16 offset1, offset2;
1148
1149 /* Utilize skb->cb[0] for timestamping request per skb */
1150 skb->cb[0] = 0;
1151
1152 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1153 if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1154 skb->cb[0] = TX_TSTAMP;
1155 else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1156 skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1157 }
1158
1159 /* TX for one-step timestamping PTP Sync packet */
1160 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1161 if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1162 &offset1, &offset2))
1163 if (msgtype == 0 && twostep == 0) {
1164 skb_queue_tail(&priv->tx_skbs, skb);
1165 queue_work(priv->dpaa2_ptp_wq,
1166 &priv->tx_onestep_tstamp);
1167 return NETDEV_TX_OK;
1168 }
1169 /* Use two-step timestamping if not one-step timestamping
1170 * PTP Sync packet
1171 */
1172 skb->cb[0] = TX_TSTAMP;
1173 }
1174
1175 /* TX for other packets */
1176 return __dpaa2_eth_tx(skb, net_dev);
1177}
1178
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001179/* Tx confirmation frame processing routine */
1180static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
Ioana Ciorneib00c8982018-10-12 16:27:38 +00001181 struct dpaa2_eth_channel *ch __always_unused,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001182 const struct dpaa2_fd *fd,
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001183 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001184{
1185 struct rtnl_link_stats64 *percpu_stats;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001186 struct dpaa2_eth_drv_stats *percpu_extras;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001187 u32 fd_len = dpaa2_fd_get_len(fd);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001188 u32 fd_errors;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001189
Ioana Radulescu56361872017-04-28 04:50:32 -05001190 /* Tracing point */
1191 trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1192
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001193 percpu_extras = this_cpu_ptr(priv->percpu_extras);
1194 percpu_extras->tx_conf_frames++;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001195 percpu_extras->tx_conf_bytes += fd_len;
1196
Ioana Radulescu39163c02017-06-06 10:00:39 -05001197 /* Check frame errors in the FD field */
1198 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001199 dpaa2_eth_free_tx_fd(priv, fq, fd, true);
Ioana Radulescu39163c02017-06-06 10:00:39 -05001200
1201 if (likely(!fd_errors))
1202 return;
1203
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06001204 if (net_ratelimit())
1205 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1206 fd_errors);
1207
Ioana Radulescu39163c02017-06-06 10:00:39 -05001208 percpu_stats = this_cpu_ptr(priv->percpu_stats);
1209 /* Tx-conf logically pertains to the egress path. */
1210 percpu_stats->tx_errors++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001211}
1212
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001213static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001214{
1215 int err;
1216
1217 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1218 DPNI_OFF_RX_L3_CSUM, enable);
1219 if (err) {
1220 netdev_err(priv->net_dev,
1221 "dpni_set_offload(RX_L3_CSUM) failed\n");
1222 return err;
1223 }
1224
1225 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1226 DPNI_OFF_RX_L4_CSUM, enable);
1227 if (err) {
1228 netdev_err(priv->net_dev,
1229 "dpni_set_offload(RX_L4_CSUM) failed\n");
1230 return err;
1231 }
1232
1233 return 0;
1234}
1235
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001236static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001237{
1238 int err;
1239
1240 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1241 DPNI_OFF_TX_L3_CSUM, enable);
1242 if (err) {
1243 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1244 return err;
1245 }
1246
1247 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1248 DPNI_OFF_TX_L4_CSUM, enable);
1249 if (err) {
1250 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1251 return err;
1252 }
1253
1254 return 0;
1255}
1256
1257/* Perform a single release command to add buffers
1258 * to the specified buffer pool
1259 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001260static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1261 struct dpaa2_eth_channel *ch, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001262{
1263 struct device *dev = priv->net_dev->dev.parent;
1264 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001265 struct page *page;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001266 dma_addr_t addr;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001267 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001268 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001269
1270 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1271 /* Allocate buffer visible to WRIOP + skb shared info +
1272 * alignment padding
1273 */
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001274 /* allocate one page for each Rx buffer. WRIOP sees
1275 * the entire page except for a tailroom reserved for
1276 * skb shared info
1277 */
1278 page = dev_alloc_pages(0);
1279 if (!page)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001280 goto err_alloc;
1281
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001282 addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001283 DMA_BIDIRECTIONAL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001284 if (unlikely(dma_mapping_error(dev, addr)))
1285 goto err_map;
1286
1287 buf_array[i] = addr;
Ioana Radulescu56361872017-04-28 04:50:32 -05001288
1289 /* tracing point */
1290 trace_dpaa2_eth_buf_seed(priv->net_dev,
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001291 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03001292 addr, priv->rx_buf_size,
Ioana Radulescu56361872017-04-28 04:50:32 -05001293 bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001294 }
1295
1296release_bufs:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001297 /* In case the portal is busy, retry until successful */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001298 while ((err = dpaa2_io_service_release(ch->dpio, bpid,
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001299 buf_array, i)) == -EBUSY) {
1300 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1301 break;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001302 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001303 }
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001304
1305 /* If release command failed, clean up and bail out;
1306 * not much else we can do about it
1307 */
1308 if (err) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001309 dpaa2_eth_free_bufs(priv, buf_array, i);
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001310 return 0;
1311 }
1312
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001313 return i;
1314
1315err_map:
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00001316 __free_pages(page, 0);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001317err_alloc:
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001318 /* If we managed to allocate at least some buffers,
1319 * release them to hardware
1320 */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001321 if (i)
1322 goto release_bufs;
1323
1324 return 0;
1325}
1326
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001327static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001328{
1329 int i, j;
1330 int new_count;
1331
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001332 for (j = 0; j < priv->num_channels; j++) {
1333 for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1334 i += DPAA2_ETH_BUFS_PER_CMD) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001335 new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001336 priv->channel[j]->buf_count += new_count;
1337
1338 if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001339 return -ENOMEM;
1340 }
1341 }
1342 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001343
1344 return 0;
1345}
1346
Jesse Brandeburgd0ea5cb2020-09-25 15:24:45 -07001347/*
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001348 * Drain the specified number of buffers from the DPNI's private buffer pool.
1349 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1350 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001351static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001352{
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001353 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001354 int retries = 0;
Ioana Radulescu87eb55e2017-10-11 08:29:43 -05001355 int ret;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001356
1357 do {
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001358 ret = dpaa2_io_service_acquire(NULL, priv->bpid,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001359 buf_array, count);
1360 if (ret < 0) {
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001361 if (ret == -EBUSY &&
Ioana Ciornei0e5ad752020-06-24 14:34:19 +03001362 retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001363 continue;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001364 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1365 return;
1366 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001367 dpaa2_eth_free_bufs(priv, buf_array, ret);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001368 retries = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001369 } while (ret);
1370}
1371
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001372static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001373{
1374 int i;
1375
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001376 dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1377 dpaa2_eth_drain_bufs(priv, 1);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001378
1379 for (i = 0; i < priv->num_channels; i++)
1380 priv->channel[i]->buf_count = 0;
1381}
1382
1383/* Function is called from softirq context only, so we don't need to guard
1384 * the access to percpu count
1385 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001386static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1387 struct dpaa2_eth_channel *ch,
1388 u16 bpid)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001389{
1390 int new_count;
1391
1392 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1393 return 0;
1394
1395 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001396 new_count = dpaa2_eth_add_bufs(priv, ch, bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001397 if (unlikely(!new_count)) {
1398 /* Out of memory; abort for now, we'll try later on */
1399 break;
1400 }
1401 ch->buf_count += new_count;
1402 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1403
1404 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1405 return -ENOMEM;
1406
1407 return 0;
1408}
1409
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001410static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1411{
1412 struct dpaa2_eth_sgt_cache *sgt_cache;
1413 u16 count;
1414 int k, i;
1415
Ioana Ciornei0fe665d2020-07-06 17:55:54 +03001416 for_each_possible_cpu(k) {
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001417 sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1418 count = sgt_cache->count;
1419
1420 for (i = 0; i < count; i++)
1421 kfree(sgt_cache->buf[i]);
1422 sgt_cache->count = 0;
1423 }
1424}
1425
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001426static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001427{
1428 int err;
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001429 int dequeues = -1;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001430
1431 /* Retry while portal is busy */
1432 do {
Ioana Radulescu7ec05962018-01-05 05:04:32 -06001433 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1434 ch->store);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001435 dequeues++;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001436 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001437 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001438
Ioana Radulescu85047ab2017-04-28 04:50:31 -05001439 ch->stats.dequeue_portal_busy += dequeues;
1440 if (unlikely(err))
1441 ch->stats.pull_err++;
1442
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001443 return err;
1444}
1445
1446/* NAPI poll routine
1447 *
1448 * Frames are dequeued from the QMan channel associated with this NAPI context.
1449 * Rx, Tx confirmation and (if configured) Rx error frames all count
1450 * towards the NAPI budget.
1451 */
1452static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1453{
1454 struct dpaa2_eth_channel *ch;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001455 struct dpaa2_eth_priv *priv;
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001456 int rx_cleaned = 0, txconf_cleaned = 0;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001457 struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1458 struct netdev_queue *nq;
1459 int store_cleaned, work_done;
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001460 struct list_head rx_list;
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001461 int retries = 0;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001462 u16 flowid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001463 int err;
1464
1465 ch = container_of(napi, struct dpaa2_eth_channel, napi);
Ioana Radulescud678be12019-03-01 17:47:24 +00001466 ch->xdp.res = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001467 priv = ch->priv;
1468
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001469 INIT_LIST_HEAD(&rx_list);
1470 ch->rx_list = &rx_list;
1471
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001472 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001473 err = dpaa2_eth_pull_channel(ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001474 if (unlikely(err))
1475 break;
1476
1477 /* Refill pool if appropriate */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001478 dpaa2_eth_refill_pool(priv, ch, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001479
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001480 store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001481 if (store_cleaned <= 0)
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001482 break;
1483 if (fq->type == DPAA2_RX_FQ) {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001484 rx_cleaned += store_cleaned;
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001485 flowid = fq->flowid;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001486 } else {
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001487 txconf_cleaned += store_cleaned;
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001488 /* We have a single Tx conf FQ on this channel */
1489 txc_fq = fq;
1490 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001491
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001492 /* If we either consumed the whole NAPI budget with Rx frames
1493 * or we reached the Tx confirmations threshold, we're done.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001494 */
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001495 if (rx_cleaned >= budget ||
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001496 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1497 work_done = budget;
1498 goto out;
1499 }
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001500 } while (store_cleaned);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001501
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001502 /* We didn't consume the entire budget, so finish napi and
1503 * re-enable data availability notifications
1504 */
1505 napi_complete_done(napi, rx_cleaned);
1506 do {
1507 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1508 cpu_relax();
Ioana Radulescuef17bd72019-10-07 14:38:28 +03001509 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
Ioana Ciocoi Radulescu68049a52018-10-08 14:16:31 +00001510 WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1511 ch->nctx.desired_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001512
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001513 work_done = max(rx_cleaned, 1);
1514
1515out:
Ioana Ciornei0a25d922019-03-25 13:42:39 +00001516 netif_receive_skb_list(ch->rx_list);
1517
Ioana Radulescud678be12019-03-01 17:47:24 +00001518 if (txc_fq && txc_fq->dq_frames) {
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001519 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1520 netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1521 txc_fq->dq_bytes);
1522 txc_fq->dq_frames = 0;
1523 txc_fq->dq_bytes = 0;
1524 }
1525
Ioana Radulescud678be12019-03-01 17:47:24 +00001526 if (ch->xdp.res & XDP_REDIRECT)
1527 xdp_do_flush_map();
Ioana Ciornei74a1c052020-05-13 16:55:46 +03001528 else if (rx_cleaned && ch->xdp.res & XDP_TX)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001529 dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
Ioana Radulescud678be12019-03-01 17:47:24 +00001530
Ioana Ciocoi Radulescu569dac62018-11-14 11:48:36 +00001531 return work_done;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001532}
1533
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001534static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001535{
1536 struct dpaa2_eth_channel *ch;
1537 int i;
1538
1539 for (i = 0; i < priv->num_channels; i++) {
1540 ch = priv->channel[i];
1541 napi_enable(&ch->napi);
1542 }
1543}
1544
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001545static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001546{
1547 struct dpaa2_eth_channel *ch;
1548 int i;
1549
1550 for (i = 0; i < priv->num_channels; i++) {
1551 ch = priv->channel[i];
1552 napi_disable(&ch->napi);
1553 }
1554}
1555
Ioana Ciornei07beb162020-05-31 00:08:14 +03001556void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1557 bool tx_pause, bool pfc)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001558{
1559 struct dpni_taildrop td = {0};
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001560 struct dpaa2_eth_fq *fq;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001561 int i, err;
1562
Ioana Ciornei07beb162020-05-31 00:08:14 +03001563 /* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1564 * flow control is disabled (as it might interfere with either the
1565 * buffer pool depletion trigger for pause frames or with the group
1566 * congestion trigger for PFC frames)
1567 */
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001568 td.enable = !tx_pause;
Ioana Ciornei07beb162020-05-31 00:08:14 +03001569 if (priv->rx_fqtd_enabled == td.enable)
1570 goto set_cgtd;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001571
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001572 td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1573 td.units = DPNI_CONGESTION_UNIT_BYTES;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001574
1575 for (i = 0; i < priv->num_fqs; i++) {
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001576 fq = &priv->fq[i];
1577 if (fq->type != DPAA2_RX_FQ)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001578 continue;
1579 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03001580 DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1581 fq->tc, fq->flowid, &td);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001582 if (err) {
1583 netdev_err(priv->net_dev,
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001584 "dpni_set_taildrop(FQ) failed\n");
1585 return;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001586 }
1587 }
1588
Ioana Ciornei07beb162020-05-31 00:08:14 +03001589 priv->rx_fqtd_enabled = td.enable;
1590
1591set_cgtd:
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001592 /* Congestion group taildrop: threshold is in frames, per group
1593 * of FQs belonging to the same traffic class
Ioana Ciornei07beb162020-05-31 00:08:14 +03001594 * Enabled if general Tx pause disabled or if PFCs are enabled
1595 * (congestion group threhsold for PFC generation is lower than the
1596 * CG taildrop threshold, so it won't interfere with it; we also
1597 * want frames in non-PFC enabled traffic classes to be kept in check)
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001598 */
Ioana Ciornei07beb162020-05-31 00:08:14 +03001599 td.enable = !tx_pause || (tx_pause && pfc);
1600 if (priv->rx_cgtd_enabled == td.enable)
1601 return;
1602
Ioana Radulescu2c8d1c82020-05-31 00:08:11 +03001603 td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
1604 td.units = DPNI_CONGESTION_UNIT_FRAMES;
1605 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
1606 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1607 DPNI_CP_GROUP, DPNI_QUEUE_RX,
1608 i, 0, &td);
1609 if (err) {
1610 netdev_err(priv->net_dev,
1611 "dpni_set_taildrop(CG) failed\n");
1612 return;
1613 }
1614 }
1615
Ioana Ciornei07beb162020-05-31 00:08:14 +03001616 priv->rx_cgtd_enabled = td.enable;
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001617}
1618
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001619static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001620{
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001621 struct dpni_link_state state = {0};
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001622 bool tx_pause;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001623 int err;
1624
1625 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1626 if (unlikely(err)) {
1627 netdev_err(priv->net_dev,
1628 "dpni_get_link_state() failed\n");
1629 return err;
1630 }
1631
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001632 /* If Tx pause frame settings have changed, we need to update
1633 * Rx FQ taildrop configuration as well. We configure taildrop
1634 * only when pause frame generation is disabled.
1635 */
Ioana Radulescuad054f22020-05-31 00:08:10 +03001636 tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
Ioana Ciornei07beb162020-05-31 00:08:14 +03001637 dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03001638
Ioana Ciornei71947922019-10-31 01:18:31 +02001639 /* When we manage the MAC/PHY using phylink there is no need
1640 * to manually update the netif_carrier.
1641 */
1642 if (priv->mac)
1643 goto out;
1644
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001645 /* Chech link state; speed / duplex changes are not treated yet */
1646 if (priv->link_state.up == state.up)
Ioana Radulescucce629432019-08-28 17:08:14 +03001647 goto out;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001648
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001649 if (state.up) {
1650 netif_carrier_on(priv->net_dev);
1651 netif_tx_start_all_queues(priv->net_dev);
1652 } else {
1653 netif_tx_stop_all_queues(priv->net_dev);
1654 netif_carrier_off(priv->net_dev);
1655 }
1656
Ioana Radulescu77160af2017-06-06 10:00:28 -05001657 netdev_info(priv->net_dev, "Link Event: state %s\n",
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001658 state.up ? "up" : "down");
1659
Ioana Radulescucce629432019-08-28 17:08:14 +03001660out:
1661 priv->link_state = state;
1662
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001663 return 0;
1664}
1665
1666static int dpaa2_eth_open(struct net_device *net_dev)
1667{
1668 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1669 int err;
1670
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001671 err = dpaa2_eth_seed_pool(priv, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001672 if (err) {
1673 /* Not much to do; the buffer pool, though not filled up,
1674 * may still contain some buffers which would enable us
1675 * to limp on.
1676 */
1677 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05001678 priv->dpbp_dev->obj_desc.id, priv->bpid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001679 }
1680
Ioana Ciornei71947922019-10-31 01:18:31 +02001681 if (!priv->mac) {
1682 /* We'll only start the txqs when the link is actually ready;
1683 * make sure we don't race against the link up notification,
1684 * which may come immediately after dpni_enable();
1685 */
1686 netif_tx_stop_all_queues(net_dev);
1687
1688 /* Also, explicitly set carrier off, otherwise
1689 * netif_carrier_ok() will return true and cause 'ip link show'
1690 * to report the LOWER_UP flag, even though the link
1691 * notification wasn't even received.
1692 */
1693 netif_carrier_off(net_dev);
1694 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001695 dpaa2_eth_enable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001696
1697 err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1698 if (err < 0) {
1699 netdev_err(net_dev, "dpni_enable() failed\n");
1700 goto enable_err;
1701 }
1702
Ioana Ciornei4c33a5b2020-09-25 17:44:20 +03001703 if (priv->mac)
Ioana Ciornei71947922019-10-31 01:18:31 +02001704 phylink_start(priv->mac->phylink);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001705
1706 return 0;
1707
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001708enable_err:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001709 dpaa2_eth_disable_ch_napi(priv);
1710 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001711 return err;
1712}
1713
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001714/* Total number of in-flight frames on ingress queues */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001715static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001716{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001717 struct dpaa2_eth_fq *fq;
1718 u32 fcnt = 0, bcnt = 0, total = 0;
1719 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001720
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001721 for (i = 0; i < priv->num_fqs; i++) {
1722 fq = &priv->fq[i];
1723 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1724 if (err) {
1725 netdev_warn(priv->net_dev, "query_fq_count failed");
1726 break;
1727 }
1728 total += fcnt;
1729 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001730
1731 return total;
1732}
1733
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001734static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001735{
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001736 int retries = 10;
1737 u32 pending;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001738
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001739 do {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001740 pending = dpaa2_eth_ingress_fq_count(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001741 if (pending)
1742 msleep(100);
1743 } while (pending && --retries);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001744}
1745
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001746#define DPNI_TX_PENDING_VER_MAJOR 7
1747#define DPNI_TX_PENDING_VER_MINOR 13
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001748static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
Ioana Radulescu52b6a4f2019-09-02 13:23:19 +03001749{
1750 union dpni_statistics stats;
1751 int retries = 10;
1752 int err;
1753
1754 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1755 DPNI_TX_PENDING_VER_MINOR) < 0)
1756 goto out;
1757
1758 do {
1759 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1760 &stats);
1761 if (err)
1762 goto out;
1763 if (stats.page_6.tx_pending_frames == 0)
1764 return;
1765 } while (--retries);
1766
1767out:
1768 msleep(500);
1769}
1770
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001771static int dpaa2_eth_stop(struct net_device *net_dev)
1772{
1773 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei85b7a342018-10-12 16:27:33 +00001774 int dpni_enabled = 0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001775 int retries = 10;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001776
Ioana Ciornei71947922019-10-31 01:18:31 +02001777 if (!priv->mac) {
1778 netif_tx_stop_all_queues(net_dev);
1779 netif_carrier_off(net_dev);
1780 } else {
1781 phylink_stop(priv->mac->phylink);
1782 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001783
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001784 /* On dpni_disable(), the MC firmware will:
1785 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1786 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1787 * of all in flight Tx frames is finished (and corresponding Tx conf
1788 * frames are enqueued back to software)
1789 *
1790 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1791 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1792 * and Tx conf queues are consumed on NAPI poll.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001793 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001794 dpaa2_eth_wait_for_egress_fq_empty(priv);
Ioana Ciocoi Radulescu68d74312019-01-16 16:51:44 +00001795
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001796 do {
1797 dpni_disable(priv->mc_io, 0, priv->mc_token);
1798 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1799 if (dpni_enabled)
1800 /* Allow the hardware some slack */
1801 msleep(100);
1802 } while (dpni_enabled && --retries);
1803 if (!retries) {
1804 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1805 /* Must go on and disable NAPI nonetheless, so we don't crash at
1806 * the next "ifconfig up"
1807 */
1808 }
1809
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001810 dpaa2_eth_wait_for_ingress_fq_empty(priv);
1811 dpaa2_eth_disable_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001812
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001813 /* Empty the buffer pool */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001814 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001815
Ioana Ciorneid70446e2020-06-29 21:47:11 +03001816 /* Empty the Scatter-Gather Buffer cache */
1817 dpaa2_eth_sgt_cache_drain(priv);
1818
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001819 return 0;
1820}
1821
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001822static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1823{
1824 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1825 struct device *dev = net_dev->dev.parent;
1826 int err;
1827
1828 err = eth_mac_addr(net_dev, addr);
1829 if (err < 0) {
1830 dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1831 return err;
1832 }
1833
1834 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1835 net_dev->dev_addr);
1836 if (err) {
1837 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1838 return err;
1839 }
1840
1841 return 0;
1842}
1843
1844/** Fill in counters maintained by the GPP driver. These may be different from
1845 * the hardware counters obtained by ethtool.
1846 */
Ioana Radulescuacbff8e2017-06-06 10:00:24 -05001847static void dpaa2_eth_get_stats(struct net_device *net_dev,
1848 struct rtnl_link_stats64 *stats)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001849{
1850 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1851 struct rtnl_link_stats64 *percpu_stats;
1852 u64 *cpustats;
1853 u64 *netstats = (u64 *)stats;
1854 int i, j;
1855 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1856
1857 for_each_possible_cpu(i) {
1858 percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1859 cpustats = (u64 *)percpu_stats;
1860 for (j = 0; j < num; j++)
1861 netstats[j] += cpustats[j];
1862 }
1863}
1864
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001865/* Copy mac unicast addresses from @net_dev to @priv.
1866 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1867 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001868static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
1869 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001870{
1871 struct netdev_hw_addr *ha;
1872 int err;
1873
1874 netdev_for_each_uc_addr(ha, net_dev) {
1875 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1876 ha->addr);
1877 if (err)
1878 netdev_warn(priv->net_dev,
1879 "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1880 ha->addr, err);
1881 }
1882}
1883
1884/* Copy mac multicast addresses from @net_dev to @priv
1885 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1886 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001887static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
1888 struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001889{
1890 struct netdev_hw_addr *ha;
1891 int err;
1892
1893 netdev_for_each_mc_addr(ha, net_dev) {
1894 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1895 ha->addr);
1896 if (err)
1897 netdev_warn(priv->net_dev,
1898 "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1899 ha->addr, err);
1900 }
1901}
1902
1903static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1904{
1905 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1906 int uc_count = netdev_uc_count(net_dev);
1907 int mc_count = netdev_mc_count(net_dev);
1908 u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1909 u32 options = priv->dpni_attrs.options;
1910 u16 mc_token = priv->mc_token;
1911 struct fsl_mc_io *mc_io = priv->mc_io;
1912 int err;
1913
1914 /* Basic sanity checks; these probably indicate a misconfiguration */
1915 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1916 netdev_info(net_dev,
1917 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1918 max_mac);
1919
1920 /* Force promiscuous if the uc or mc counts exceed our capabilities. */
1921 if (uc_count > max_mac) {
1922 netdev_info(net_dev,
1923 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1924 uc_count, max_mac);
1925 goto force_promisc;
1926 }
1927 if (mc_count + uc_count > max_mac) {
1928 netdev_info(net_dev,
1929 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1930 uc_count + mc_count, max_mac);
1931 goto force_mc_promisc;
1932 }
1933
1934 /* Adjust promisc settings due to flag combinations */
1935 if (net_dev->flags & IFF_PROMISC)
1936 goto force_promisc;
1937 if (net_dev->flags & IFF_ALLMULTI) {
1938 /* First, rebuild unicast filtering table. This should be done
1939 * in promisc mode, in order to avoid frame loss while we
1940 * progressively add entries to the table.
1941 * We don't know whether we had been in promisc already, and
1942 * making an MC call to find out is expensive; so set uc promisc
1943 * nonetheless.
1944 */
1945 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1946 if (err)
1947 netdev_warn(net_dev, "Can't set uc promisc\n");
1948
1949 /* Actual uc table reconstruction. */
1950 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1951 if (err)
1952 netdev_warn(net_dev, "Can't clear uc filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001953 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001954
1955 /* Finally, clear uc promisc and set mc promisc as requested. */
1956 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1957 if (err)
1958 netdev_warn(net_dev, "Can't clear uc promisc\n");
1959 goto force_mc_promisc;
1960 }
1961
1962 /* Neither unicast, nor multicast promisc will be on... eventually.
1963 * For now, rebuild mac filtering tables while forcing both of them on.
1964 */
1965 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1966 if (err)
1967 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1968 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1969 if (err)
1970 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1971
1972 /* Actual mac filtering tables reconstruction */
1973 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1974 if (err)
1975 netdev_warn(net_dev, "Can't clear mac filters\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03001976 dpaa2_eth_add_mc_hw_addr(net_dev, priv);
1977 dpaa2_eth_add_uc_hw_addr(net_dev, priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05001978
1979 /* Now we can clear both ucast and mcast promisc, without risking
1980 * to drop legitimate frames anymore.
1981 */
1982 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1983 if (err)
1984 netdev_warn(net_dev, "Can't clear ucast promisc\n");
1985 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1986 if (err)
1987 netdev_warn(net_dev, "Can't clear mcast promisc\n");
1988
1989 return;
1990
1991force_promisc:
1992 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1993 if (err)
1994 netdev_warn(net_dev, "Can't set ucast promisc\n");
1995force_mc_promisc:
1996 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1997 if (err)
1998 netdev_warn(net_dev, "Can't set mcast promisc\n");
1999}
2000
2001static int dpaa2_eth_set_features(struct net_device *net_dev,
2002 netdev_features_t features)
2003{
2004 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2005 netdev_features_t changed = features ^ net_dev->features;
2006 bool enable;
2007 int err;
2008
2009 if (changed & NETIF_F_RXCSUM) {
2010 enable = !!(features & NETIF_F_RXCSUM);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002011 err = dpaa2_eth_set_rx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002012 if (err)
2013 return err;
2014 }
2015
2016 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2017 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002018 err = dpaa2_eth_set_tx_csum(priv, enable);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002019 if (err)
2020 return err;
2021 }
2022
2023 return 0;
2024}
2025
Ioana Radulescu859f9982018-04-26 18:23:47 +08002026static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2027{
2028 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2029 struct hwtstamp_config config;
2030
Yangbo Luc5521182020-09-18 17:08:02 +08002031 if (!dpaa2_ptp)
2032 return -EINVAL;
2033
Ioana Radulescu859f9982018-04-26 18:23:47 +08002034 if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
2035 return -EFAULT;
2036
2037 switch (config.tx_type) {
2038 case HWTSTAMP_TX_OFF:
Ioana Radulescu859f9982018-04-26 18:23:47 +08002039 case HWTSTAMP_TX_ON:
Yangbo Luc5521182020-09-18 17:08:02 +08002040 case HWTSTAMP_TX_ONESTEP_SYNC:
Yangbo Lu1cf773b2020-09-18 17:08:01 +08002041 priv->tx_tstamp_type = config.tx_type;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002042 break;
2043 default:
2044 return -ERANGE;
2045 }
2046
2047 if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
2048 priv->rx_tstamp = false;
2049 } else {
2050 priv->rx_tstamp = true;
2051 /* TS is set for all frame types, not only those requested */
2052 config.rx_filter = HWTSTAMP_FILTER_ALL;
2053 }
2054
2055 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
2056 -EFAULT : 0;
2057}
2058
2059static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2060{
Russell King4a841822020-02-27 12:00:21 +00002061 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2062
Ioana Radulescu859f9982018-04-26 18:23:47 +08002063 if (cmd == SIOCSHWTSTAMP)
2064 return dpaa2_eth_ts_ioctl(dev, rq, cmd);
2065
Russell King4a841822020-02-27 12:00:21 +00002066 if (priv->mac)
2067 return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2068
2069 return -EOPNOTSUPP;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002070}
2071
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002072static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2073{
2074 int mfl, linear_mfl;
2075
2076 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03002077 linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002078 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002079
2080 if (mfl > linear_mfl) {
2081 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2082 linear_mfl - VLAN_ETH_HLEN);
2083 return false;
2084 }
2085
2086 return true;
2087}
2088
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002089static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002090{
2091 int mfl, err;
2092
2093 /* We enforce a maximum Rx frame length based on MTU only if we have
2094 * an XDP program attached (in order to avoid Rx S/G frames).
2095 * Otherwise, we accept all incoming frames as long as they are not
2096 * larger than maximum size supported in hardware
2097 */
2098 if (has_xdp)
2099 mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2100 else
2101 mfl = DPAA2_ETH_MFL;
2102
2103 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2104 if (err) {
2105 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2106 return err;
2107 }
2108
2109 return 0;
2110}
2111
2112static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2113{
2114 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2115 int err;
2116
2117 if (!priv->xdp_prog)
2118 goto out;
2119
2120 if (!xdp_mtu_valid(priv, new_mtu))
2121 return -EINVAL;
2122
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002123 err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002124 if (err)
2125 return err;
2126
2127out:
2128 dev->mtu = new_mtu;
2129 return 0;
2130}
2131
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002132static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002133{
2134 struct dpni_buffer_layout buf_layout = {0};
2135 int err;
2136
2137 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2138 DPNI_QUEUE_RX, &buf_layout);
2139 if (err) {
2140 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2141 return err;
2142 }
2143
2144 /* Reserve extra headroom for XDP header size changes */
2145 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2146 (has_xdp ? XDP_PACKET_HEADROOM : 0);
2147 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2148 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2149 DPNI_QUEUE_RX, &buf_layout);
2150 if (err) {
2151 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2152 return err;
2153 }
2154
2155 return 0;
2156}
2157
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002158static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002159{
2160 struct dpaa2_eth_priv *priv = netdev_priv(dev);
2161 struct dpaa2_eth_channel *ch;
2162 struct bpf_prog *old;
2163 bool up, need_update;
2164 int i, err;
2165
2166 if (prog && !xdp_mtu_valid(priv, dev->mtu))
2167 return -EINVAL;
2168
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002169 if (prog)
2170 bpf_prog_add(prog, priv->num_channels);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002171
2172 up = netif_running(dev);
2173 need_update = (!!priv->xdp_prog != !!prog);
2174
2175 if (up)
2176 dpaa2_eth_stop(dev);
2177
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002178 /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2179 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2180 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2181 * so we are sure no old format buffers will be used from now on.
2182 */
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002183 if (need_update) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002184 err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002185 if (err)
2186 goto out_err;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002187 err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
Ioana Ciocoi Radulescu7b1eea12018-11-26 16:27:30 +00002188 if (err)
2189 goto out_err;
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002190 }
2191
2192 old = xchg(&priv->xdp_prog, prog);
2193 if (old)
2194 bpf_prog_put(old);
2195
2196 for (i = 0; i < priv->num_channels; i++) {
2197 ch = priv->channel[i];
2198 old = xchg(&ch->xdp.prog, prog);
2199 if (old)
2200 bpf_prog_put(old);
2201 }
2202
2203 if (up) {
2204 err = dpaa2_eth_open(dev);
2205 if (err)
2206 return err;
2207 }
2208
2209 return 0;
2210
2211out_err:
2212 if (prog)
2213 bpf_prog_sub(prog, priv->num_channels);
2214 if (up)
2215 dpaa2_eth_open(dev);
2216
2217 return err;
2218}
2219
2220static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2221{
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002222 switch (xdp->command) {
2223 case XDP_SETUP_PROG:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002224 return dpaa2_eth_setup_xdp(dev, xdp->prog);
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002225 default:
2226 return -EINVAL;
2227 }
2228
2229 return 0;
2230}
2231
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002232static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2233 struct xdp_frame *xdpf,
2234 struct dpaa2_fd *fd)
Ioana Radulescud678be12019-03-01 17:47:24 +00002235{
Ioana Radulescud678be12019-03-01 17:47:24 +00002236 struct device *dev = net_dev->dev.parent;
Ioana Radulescud678be12019-03-01 17:47:24 +00002237 unsigned int needed_headroom;
2238 struct dpaa2_eth_swa *swa;
Ioana Radulescud678be12019-03-01 17:47:24 +00002239 void *buffer_start, *aligned_start;
2240 dma_addr_t addr;
Ioana Radulescud678be12019-03-01 17:47:24 +00002241
2242 /* We require a minimum headroom to be able to transmit the frame.
2243 * Otherwise return an error and let the original net_device handle it
2244 */
Yangbo Lu1cf773b2020-09-18 17:08:01 +08002245 needed_headroom = dpaa2_eth_needed_headroom(NULL);
Ioana Radulescud678be12019-03-01 17:47:24 +00002246 if (xdpf->headroom < needed_headroom)
2247 return -EINVAL;
2248
Ioana Radulescud678be12019-03-01 17:47:24 +00002249 /* Setup the FD fields */
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002250 memset(fd, 0, sizeof(*fd));
Ioana Radulescud678be12019-03-01 17:47:24 +00002251
2252 /* Align FD address, if possible */
2253 buffer_start = xdpf->data - needed_headroom;
2254 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2255 DPAA2_ETH_TX_BUF_ALIGN);
2256 if (aligned_start >= xdpf->data - xdpf->headroom)
2257 buffer_start = aligned_start;
2258
2259 swa = (struct dpaa2_eth_swa *)buffer_start;
2260 /* fill in necessary fields here */
2261 swa->type = DPAA2_ETH_SWA_XDP;
2262 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2263 swa->xdp.xdpf = xdpf;
2264
2265 addr = dma_map_single(dev, buffer_start,
2266 swa->xdp.dma_size,
2267 DMA_BIDIRECTIONAL);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002268 if (unlikely(dma_mapping_error(dev, addr)))
Ioana Radulescud678be12019-03-01 17:47:24 +00002269 return -ENOMEM;
Ioana Radulescud678be12019-03-01 17:47:24 +00002270
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002271 dpaa2_fd_set_addr(fd, addr);
2272 dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2273 dpaa2_fd_set_len(fd, xdpf->len);
2274 dpaa2_fd_set_format(fd, dpaa2_fd_single);
2275 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
Ioana Radulescud678be12019-03-01 17:47:24 +00002276
2277 return 0;
2278}
2279
2280static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2281 struct xdp_frame **frames, u32 flags)
2282{
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002283 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002284 struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002285 struct rtnl_link_stats64 *percpu_stats;
2286 struct dpaa2_eth_fq *fq;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002287 struct dpaa2_fd *fds;
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002288 int enqueued, i, err;
Ioana Radulescud678be12019-03-01 17:47:24 +00002289
2290 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2291 return -EINVAL;
2292
2293 if (!netif_running(net_dev))
2294 return -ENETDOWN;
2295
Ioana Ciornei8665d972020-04-22 15:05:13 +03002296 fq = &priv->fq[smp_processor_id()];
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002297 xdp_redirect_fds = &fq->xdp_redirect_fds;
2298 fds = xdp_redirect_fds->fds;
Ioana Ciornei8665d972020-04-22 15:05:13 +03002299
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002300 percpu_stats = this_cpu_ptr(priv->percpu_stats);
Ioana Ciornei6aa40b92020-04-22 15:05:12 +03002301
Ioana Ciornei8665d972020-04-22 15:05:13 +03002302 /* create a FD for each xdp_frame in the list received */
Ioana Radulescud678be12019-03-01 17:47:24 +00002303 for (i = 0; i < n; i++) {
Ioana Ciornei8665d972020-04-22 15:05:13 +03002304 err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2305 if (err)
2306 break;
2307 }
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002308 xdp_redirect_fds->num = i;
Ioana Radulescud678be12019-03-01 17:47:24 +00002309
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002310 /* enqueue all the frame descriptors */
2311 enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
Ioana Radulescud678be12019-03-01 17:47:24 +00002312
Ioana Ciornei8665d972020-04-22 15:05:13 +03002313 /* update statistics */
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002314 percpu_stats->tx_packets += enqueued;
2315 for (i = 0; i < enqueued; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002316 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002317 for (i = enqueued; i < n; i++)
Ioana Ciornei8665d972020-04-22 15:05:13 +03002318 xdp_return_frame_rx_napi(frames[i]);
2319
Ioana Ciornei38c440b2020-05-06 20:47:17 +03002320 return enqueued;
Ioana Radulescud678be12019-03-01 17:47:24 +00002321}
2322
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002323static int update_xps(struct dpaa2_eth_priv *priv)
2324{
2325 struct net_device *net_dev = priv->net_dev;
2326 struct cpumask xps_mask;
2327 struct dpaa2_eth_fq *fq;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002328 int i, num_queues, netdev_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002329 int err = 0;
2330
2331 num_queues = dpaa2_eth_queue_count(priv);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002332 netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002333
2334 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2335 * queues, so only process those
2336 */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002337 for (i = 0; i < netdev_queues; i++) {
2338 fq = &priv->fq[i % num_queues];
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002339
2340 cpumask_clear(&xps_mask);
2341 cpumask_set_cpu(fq->target_cpu, &xps_mask);
2342
2343 err = netif_set_xps_queue(net_dev, &xps_mask, i);
2344 if (err) {
2345 netdev_warn_once(net_dev, "Error setting XPS queue\n");
2346 break;
2347 }
2348 }
2349
2350 return err;
2351}
2352
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002353static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2354 struct tc_mqprio_qopt *mqprio)
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002355{
2356 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002357 u8 num_tc, num_queues;
2358 int i;
2359
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002360 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2361 num_queues = dpaa2_eth_queue_count(priv);
2362 num_tc = mqprio->num_tc;
2363
2364 if (num_tc == net_dev->num_tc)
2365 return 0;
2366
2367 if (num_tc > dpaa2_eth_tc_count(priv)) {
2368 netdev_err(net_dev, "Max %d traffic classes supported\n",
2369 dpaa2_eth_tc_count(priv));
Jesper Dangaard Brouerb89c1e62020-04-23 16:57:50 +02002370 return -EOPNOTSUPP;
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002371 }
2372
2373 if (!num_tc) {
2374 netdev_reset_tc(net_dev);
2375 netif_set_real_num_tx_queues(net_dev, num_queues);
2376 goto out;
2377 }
2378
2379 netdev_set_num_tc(net_dev, num_tc);
2380 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2381
2382 for (i = 0; i < num_tc; i++)
2383 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2384
2385out:
2386 update_xps(priv);
2387
2388 return 0;
2389}
2390
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002391#define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2392
2393static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2394{
2395 struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2396 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2397 struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2398 struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2399 int err;
2400
2401 if (p->command == TC_TBF_STATS)
2402 return -EOPNOTSUPP;
2403
2404 /* Only per port Tx shaping */
2405 if (p->parent != TC_H_ROOT)
2406 return -EOPNOTSUPP;
2407
2408 if (p->command == TC_TBF_REPLACE) {
2409 if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2410 netdev_err(net_dev, "burst size cannot be greater than %d\n",
2411 DPAA2_ETH_MAX_BURST_SIZE);
2412 return -EINVAL;
2413 }
2414
2415 tx_cr_shaper.max_burst_size = cfg->max_size;
2416 /* The TBF interface is in bytes/s, whereas DPAA2 expects the
2417 * rate in Mbits/s
2418 */
2419 tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2420 }
2421
2422 err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2423 &tx_er_shaper, 0);
2424 if (err) {
2425 netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2426 return err;
2427 }
2428
2429 return 0;
2430}
2431
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002432static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2433 enum tc_setup_type type, void *type_data)
2434{
2435 switch (type) {
2436 case TC_SETUP_QDISC_MQPRIO:
2437 return dpaa2_eth_setup_mqprio(net_dev, type_data);
Ioana Ciornei3657cda2020-07-21 19:38:25 +03002438 case TC_SETUP_QDISC_TBF:
2439 return dpaa2_eth_setup_tbf(net_dev, type_data);
Ioana Ciorneie3ec13b2020-07-21 19:38:23 +03002440 default:
2441 return -EOPNOTSUPP;
2442 }
2443}
2444
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002445static const struct net_device_ops dpaa2_eth_ops = {
2446 .ndo_open = dpaa2_eth_open,
2447 .ndo_start_xmit = dpaa2_eth_tx,
2448 .ndo_stop = dpaa2_eth_stop,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002449 .ndo_set_mac_address = dpaa2_eth_set_addr,
2450 .ndo_get_stats64 = dpaa2_eth_get_stats,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002451 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2452 .ndo_set_features = dpaa2_eth_set_features,
Ioana Radulescu859f9982018-04-26 18:23:47 +08002453 .ndo_do_ioctl = dpaa2_eth_ioctl,
Ioana Ciocoi Radulescu7e273a82018-11-26 16:27:29 +00002454 .ndo_change_mtu = dpaa2_eth_change_mtu,
2455 .ndo_bpf = dpaa2_eth_xdp,
Ioana Radulescud678be12019-03-01 17:47:24 +00002456 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03002457 .ndo_setup_tc = dpaa2_eth_setup_tc,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002458};
2459
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002460static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002461{
2462 struct dpaa2_eth_channel *ch;
2463
2464 ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05002465
2466 /* Update NAPI statistics */
2467 ch->stats.cdan++;
2468
Jiafei Pan6c33ae12020-08-03 23:10:08 +03002469 napi_schedule(&ch->napi);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002470}
2471
2472/* Allocate and configure a DPCON object */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002473static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002474{
2475 struct fsl_mc_device *dpcon;
2476 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002477 int err;
2478
2479 err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2480 FSL_MC_POOL_DPCON, &dpcon);
2481 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002482 if (err == -ENXIO)
2483 err = -EPROBE_DEFER;
2484 else
2485 dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2486 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002487 }
2488
2489 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2490 if (err) {
2491 dev_err(dev, "dpcon_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002492 goto free;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002493 }
2494
2495 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2496 if (err) {
2497 dev_err(dev, "dpcon_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002498 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002499 }
2500
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002501 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2502 if (err) {
2503 dev_err(dev, "dpcon_enable() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002504 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002505 }
2506
2507 return dpcon;
2508
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002509close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002510 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00002511free:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002512 fsl_mc_object_free(dpcon);
2513
YueHaibing02afa9c2020-08-04 21:26:43 +08002514 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002515}
2516
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002517static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
2518 struct fsl_mc_device *dpcon)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002519{
2520 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2521 dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2522 fsl_mc_object_free(dpcon);
2523}
2524
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002525static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002526{
2527 struct dpaa2_eth_channel *channel;
2528 struct dpcon_attr attr;
2529 struct device *dev = priv->net_dev->dev.parent;
2530 int err;
2531
2532 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2533 if (!channel)
2534 return NULL;
2535
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002536 channel->dpcon = dpaa2_eth_setup_dpcon(priv);
YueHaibing02afa9c2020-08-04 21:26:43 +08002537 if (IS_ERR(channel->dpcon)) {
2538 err = PTR_ERR(channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002539 goto err_setup;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002540 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002541
2542 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2543 &attr);
2544 if (err) {
2545 dev_err(dev, "dpcon_get_attributes() failed\n");
2546 goto err_get_attr;
2547 }
2548
2549 channel->dpcon_id = attr.id;
2550 channel->ch_id = attr.qbman_ch_id;
2551 channel->priv = priv;
2552
2553 return channel;
2554
2555err_get_attr:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002556 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002557err_setup:
2558 kfree(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002559 return ERR_PTR(err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002560}
2561
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002562static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
2563 struct dpaa2_eth_channel *channel)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002564{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002565 dpaa2_eth_free_dpcon(priv, channel->dpcon);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002566 kfree(channel);
2567}
2568
2569/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2570 * and register data availability notifications
2571 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002572static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002573{
2574 struct dpaa2_io_notification_ctx *nctx;
2575 struct dpaa2_eth_channel *channel;
2576 struct dpcon_notification_cfg dpcon_notif_cfg;
2577 struct device *dev = priv->net_dev->dev.parent;
2578 int i, err;
2579
2580 /* We want the ability to spread ingress traffic (RX, TX conf) to as
2581 * many cores as possible, so we need one channel for each core
2582 * (unless there's fewer queues than cores, in which case the extra
2583 * channels would be wasted).
2584 * Allocate one channel per core and register it to the core's
2585 * affine DPIO. If not enough channels are available for all cores
2586 * or if some cores don't have an affine DPIO, there will be no
2587 * ingress frame processing on those cores.
2588 */
2589 cpumask_clear(&priv->dpio_cpumask);
2590 for_each_online_cpu(i) {
2591 /* Try to allocate a channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002592 channel = dpaa2_eth_alloc_channel(priv);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002593 if (IS_ERR_OR_NULL(channel)) {
Ioana Radulescubd8460f2019-05-24 18:15:16 +03002594 err = PTR_ERR_OR_ZERO(channel);
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002595 if (err != -EPROBE_DEFER)
2596 dev_info(dev,
2597 "No affine channel for cpu %d and above\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002598 goto err_alloc_ch;
2599 }
2600
2601 priv->channel[priv->num_channels] = channel;
2602
2603 nctx = &channel->nctx;
2604 nctx->is_cdan = 1;
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002605 nctx->cb = dpaa2_eth_cdan_cb;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002606 nctx->id = channel->ch_id;
2607 nctx->desired_cpu = i;
2608
2609 /* Register the new context */
Ioana Radulescu7ec05962018-01-05 05:04:32 -06002610 channel->dpio = dpaa2_io_service_select(i);
Ioana Ciornei47441f72018-12-10 16:50:19 +00002611 err = dpaa2_io_service_register(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002612 if (err) {
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002613 dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002614 /* If no affine DPIO for this core, there's probably
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002615 * none available for next cores either. Signal we want
2616 * to retry later, in case the DPIO devices weren't
2617 * probed yet.
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002618 */
Ioana Radulescu5206d8d2017-06-06 10:00:33 -05002619 err = -EPROBE_DEFER;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002620 goto err_service_reg;
2621 }
2622
2623 /* Register DPCON notification with MC */
2624 dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2625 dpcon_notif_cfg.priority = 0;
2626 dpcon_notif_cfg.user_ctx = nctx->qman64;
2627 err = dpcon_set_notification(priv->mc_io, 0,
2628 channel->dpcon->mc_handle,
2629 &dpcon_notif_cfg);
2630 if (err) {
2631 dev_err(dev, "dpcon_set_notification failed()\n");
2632 goto err_set_cdan;
2633 }
2634
2635 /* If we managed to allocate a channel and also found an affine
2636 * DPIO for this core, add it to the final mask
2637 */
2638 cpumask_set_cpu(i, &priv->dpio_cpumask);
2639 priv->num_channels++;
2640
2641 /* Stop if we already have enough channels to accommodate all
2642 * RX and TX conf queues
2643 */
Ioana Ciocoi Radulescub0e4f372018-11-14 11:48:35 +00002644 if (priv->num_channels == priv->dpni_attrs.num_queues)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002645 break;
2646 }
2647
2648 return 0;
2649
2650err_set_cdan:
Ioana Ciornei47441f72018-12-10 16:50:19 +00002651 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002652err_service_reg:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002653 dpaa2_eth_free_channel(priv, channel);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002654err_alloc_ch:
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002655 if (err == -EPROBE_DEFER) {
2656 for (i = 0; i < priv->num_channels; i++) {
2657 channel = priv->channel[i];
2658 nctx = &channel->nctx;
2659 dpaa2_io_service_deregister(channel->dpio, nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002660 dpaa2_eth_free_channel(priv, channel);
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002661 }
2662 priv->num_channels = 0;
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002663 return err;
Ioana Ciornei5aa42772019-11-12 18:21:52 +02002664 }
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002665
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002666 if (cpumask_empty(&priv->dpio_cpumask)) {
2667 dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002668 return -ENODEV;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002669 }
2670
2671 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2672 cpumask_pr_args(&priv->dpio_cpumask));
2673
2674 return 0;
2675}
2676
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002677static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002678{
Ioana Ciornei47441f72018-12-10 16:50:19 +00002679 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002680 struct dpaa2_eth_channel *ch;
Ioana Ciornei47441f72018-12-10 16:50:19 +00002681 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002682
2683 /* deregister CDAN notifications and free channels */
2684 for (i = 0; i < priv->num_channels; i++) {
2685 ch = priv->channel[i];
Ioana Ciornei47441f72018-12-10 16:50:19 +00002686 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002687 dpaa2_eth_free_channel(priv, ch);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002688 }
2689}
2690
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002691static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
2692 int cpu)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002693{
2694 struct device *dev = priv->net_dev->dev.parent;
2695 int i;
2696
2697 for (i = 0; i < priv->num_channels; i++)
2698 if (priv->channel[i]->nctx.desired_cpu == cpu)
2699 return priv->channel[i];
2700
2701 /* We should never get here. Issue a warning and return
2702 * the first channel, because it's still better than nothing
2703 */
2704 dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2705
2706 return priv->channel[0];
2707}
2708
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002709static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002710{
2711 struct device *dev = priv->net_dev->dev.parent;
2712 struct dpaa2_eth_fq *fq;
2713 int rx_cpu, txc_cpu;
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002714 int i;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002715
2716 /* For each FQ, pick one channel/CPU to deliver frames to.
2717 * This may well change at runtime, either through irqbalance or
2718 * through direct user intervention.
2719 */
2720 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2721
2722 for (i = 0; i < priv->num_fqs; i++) {
2723 fq = &priv->fq[i];
2724 switch (fq->type) {
2725 case DPAA2_RX_FQ:
2726 fq->target_cpu = rx_cpu;
2727 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2728 if (rx_cpu >= nr_cpu_ids)
2729 rx_cpu = cpumask_first(&priv->dpio_cpumask);
2730 break;
2731 case DPAA2_TX_CONF_FQ:
2732 fq->target_cpu = txc_cpu;
2733 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2734 if (txc_cpu >= nr_cpu_ids)
2735 txc_cpu = cpumask_first(&priv->dpio_cpumask);
2736 break;
2737 default:
2738 dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2739 }
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002740 fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002741 }
Ioana Radulescu06d5b172019-06-11 14:50:01 +03002742
2743 update_xps(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002744}
2745
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002746static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002747{
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002748 int i, j;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002749
2750 /* We have one TxConf FQ per Tx flow.
2751 * The number of Tx and Rx queues is the same.
2752 * Tx queues come first in the fq array.
2753 */
2754 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2755 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2756 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2757 priv->fq[priv->num_fqs++].flowid = (u16)i;
2758 }
2759
Ioana Radulescu685e39e2020-05-31 00:08:08 +03002760 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2761 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2762 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2763 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2764 priv->fq[priv->num_fqs].tc = (u8)j;
2765 priv->fq[priv->num_fqs++].flowid = (u16)i;
2766 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002767 }
2768
2769 /* For each FQ, decide on which core to process incoming frames */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002770 dpaa2_eth_set_fq_affinity(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002771}
2772
2773/* Allocate and configure one buffer pool for each interface */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002774static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002775{
2776 int err;
2777 struct fsl_mc_device *dpbp_dev;
2778 struct device *dev = priv->net_dev->dev.parent;
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002779 struct dpbp_attr dpbp_attrs;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002780
2781 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2782 &dpbp_dev);
2783 if (err) {
Ioana Ciorneid7f5a9d2018-11-09 15:26:45 +00002784 if (err == -ENXIO)
2785 err = -EPROBE_DEFER;
2786 else
2787 dev_err(dev, "DPBP device allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002788 return err;
2789 }
2790
2791 priv->dpbp_dev = dpbp_dev;
2792
2793 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2794 &dpbp_dev->mc_handle);
2795 if (err) {
2796 dev_err(dev, "dpbp_open() failed\n");
2797 goto err_open;
2798 }
2799
Ioana Radulescud00defe2017-06-06 10:00:32 -05002800 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2801 if (err) {
2802 dev_err(dev, "dpbp_reset() failed\n");
2803 goto err_reset;
2804 }
2805
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002806 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2807 if (err) {
2808 dev_err(dev, "dpbp_enable() failed\n");
2809 goto err_enable;
2810 }
2811
2812 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002813 &dpbp_attrs);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002814 if (err) {
2815 dev_err(dev, "dpbp_get_attributes() failed\n");
2816 goto err_get_attr;
2817 }
Ioana Radulescu05fa39c2017-06-06 10:00:37 -05002818 priv->bpid = dpbp_attrs.bpid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002819
2820 return 0;
2821
2822err_get_attr:
2823 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2824err_enable:
Ioana Radulescud00defe2017-06-06 10:00:32 -05002825err_reset:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002826 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2827err_open:
2828 fsl_mc_object_free(dpbp_dev);
2829
2830 return err;
2831}
2832
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002833static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002834{
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002835 dpaa2_eth_drain_pool(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05002836 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2837 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2838 fsl_mc_object_free(priv->dpbp_dev);
2839}
2840
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002841static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002842{
2843 struct device *dev = priv->net_dev->dev.parent;
2844 struct dpni_buffer_layout buf_layout = {0};
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002845 u16 rx_buf_align;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002846 int err;
2847
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002848 /* We need to check for WRIOP version 1.0.0, but depending on the MC
2849 * version, this number is not always provided correctly on rev1.
2850 * We need to check for both alternatives in this situation.
2851 */
2852 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2853 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002854 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002855 else
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002856 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
Bogdan Purcareata8a4fd872017-10-29 08:20:42 +00002857
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03002858 /* We need to ensure that the buffer size seen by WRIOP is a multiple
2859 * of 64 or 256 bytes depending on the WRIOP version.
2860 */
2861 priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
2862
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002863 /* tx buffer */
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002864 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002865 buf_layout.pass_timestamp = true;
Yangbo Luc5521182020-09-18 17:08:02 +08002866 buf_layout.pass_frame_status = true;
Ioana Radulescu859f9982018-04-26 18:23:47 +08002867 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
Yangbo Luc5521182020-09-18 17:08:02 +08002868 DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
2869 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002870 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2871 DPNI_QUEUE_TX, &buf_layout);
2872 if (err) {
2873 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2874 return err;
2875 }
2876
2877 /* tx-confirm buffer */
Yangbo Luc5521182020-09-18 17:08:02 +08002878 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
2879 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002880 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2881 DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2882 if (err) {
2883 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2884 return err;
2885 }
2886
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002887 /* Now that we've set our tx buffer layout, retrieve the minimum
2888 * required tx data offset.
2889 */
2890 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2891 &priv->tx_data_offset);
2892 if (err) {
2893 dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2894 return err;
2895 }
2896
2897 if ((priv->tx_data_offset % 64) != 0)
2898 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2899 priv->tx_data_offset);
2900
2901 /* rx buffer */
Ioana Radulescu2b7c86e2017-12-08 06:47:56 -06002902 buf_layout.pass_frame_status = true;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002903 buf_layout.pass_parser_result = true;
Ioana Ciocoi Radulescu27c87482019-02-04 17:00:35 +00002904 buf_layout.data_align = rx_buf_align;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002905 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2906 buf_layout.private_data_size = 0;
2907 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2908 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2909 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
Ioana Radulescu859f9982018-04-26 18:23:47 +08002910 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2911 DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
Bogdan Purcareata4b2d9fe2017-10-29 08:20:43 +00002912 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2913 DPNI_QUEUE_RX, &buf_layout);
2914 if (err) {
2915 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2916 return err;
2917 }
2918
Ioana Radulescu308f64e2017-10-29 08:20:40 +00002919 return 0;
2920}
2921
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002922#define DPNI_ENQUEUE_FQID_VER_MAJOR 7
2923#define DPNI_ENQUEUE_FQID_VER_MINOR 9
2924
2925static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2926 struct dpaa2_eth_fq *fq,
Ioana Ciornei48c04812020-04-22 15:05:10 +03002927 struct dpaa2_fd *fd, u8 prio,
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002928 u32 num_frames __always_unused,
Ioana Ciornei48c04812020-04-22 15:05:10 +03002929 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002930{
Ioana Ciornei48c04812020-04-22 15:05:10 +03002931 int err;
2932
2933 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
2934 priv->tx_qdid, prio,
2935 fq->tx_qdbin, fd);
2936 if (!err && frames_enqueued)
2937 *frames_enqueued = 1;
2938 return err;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002939}
2940
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002941static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
2942 struct dpaa2_eth_fq *fq,
2943 struct dpaa2_fd *fd,
2944 u8 prio, u32 num_frames,
2945 int *frames_enqueued)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002946{
Ioana Ciornei48c04812020-04-22 15:05:10 +03002947 int err;
2948
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002949 err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
2950 fq->tx_fqid[prio],
2951 fd, num_frames);
2952
2953 if (err == 0)
2954 return -EBUSY;
2955
2956 if (frames_enqueued)
2957 *frames_enqueued = err;
2958 return 0;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002959}
2960
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002961static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002962{
2963 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2964 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2965 priv->enqueue = dpaa2_eth_enqueue_qd;
2966 else
Ioana Ciornei6ff80442020-04-22 15:05:11 +03002967 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00002968}
2969
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002970static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03002971{
2972 struct device *dev = priv->net_dev->dev.parent;
2973 struct dpni_link_cfg link_cfg = {0};
2974 int err;
2975
2976 /* Get the default link options so we don't override other flags */
2977 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2978 if (err) {
2979 dev_err(dev, "dpni_get_link_cfg() failed\n");
2980 return err;
2981 }
2982
2983 /* By default, enable both Rx and Tx pause frames */
2984 link_cfg.options |= DPNI_LINK_OPT_PAUSE;
2985 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
2986 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
2987 if (err) {
2988 dev_err(dev, "dpni_set_link_cfg() failed\n");
2989 return err;
2990 }
2991
2992 priv->link_state.options = link_cfg.options;
2993
2994 return 0;
2995}
2996
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03002997static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
Ioana Radulescua690af4f2019-10-16 10:36:23 +03002998{
2999 struct dpni_queue_id qid = {0};
3000 struct dpaa2_eth_fq *fq;
3001 struct dpni_queue queue;
3002 int i, j, err;
3003
3004 /* We only use Tx FQIDs for FQID-based enqueue, so check
3005 * if DPNI version supports it before updating FQIDs
3006 */
3007 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3008 DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3009 return;
3010
3011 for (i = 0; i < priv->num_fqs; i++) {
3012 fq = &priv->fq[i];
3013 if (fq->type != DPAA2_TX_CONF_FQ)
3014 continue;
3015 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3016 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3017 DPNI_QUEUE_TX, j, fq->flowid,
3018 &queue, &qid);
3019 if (err)
3020 goto out_err;
3021
3022 fq->tx_fqid[j] = qid.fqid;
3023 if (fq->tx_fqid[j] == 0)
3024 goto out_err;
3025 }
3026 }
3027
Ioana Ciornei6ff80442020-04-22 15:05:11 +03003028 priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
Ioana Radulescua690af4f2019-10-16 10:36:23 +03003029
3030 return;
3031
3032out_err:
3033 netdev_info(priv->net_dev,
3034 "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3035 priv->enqueue = dpaa2_eth_enqueue_qd;
3036}
3037
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003038/* Configure ingress classification based on VLAN PCP */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003039static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003040{
3041 struct device *dev = priv->net_dev->dev.parent;
3042 struct dpkg_profile_cfg kg_cfg = {0};
3043 struct dpni_qos_tbl_cfg qos_cfg = {0};
3044 struct dpni_rule_cfg key_params;
3045 void *dma_mem, *key, *mask;
3046 u8 key_size = 2; /* VLAN TCI field */
3047 int i, pcp, err;
3048
3049 /* VLAN-based classification only makes sense if we have multiple
3050 * traffic classes.
3051 * Also, we need to extract just the 3-bit PCP field from the VLAN
3052 * header and we can only do that by using a mask
3053 */
3054 if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3055 dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3056 return -EOPNOTSUPP;
3057 }
3058
3059 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3060 if (!dma_mem)
3061 return -ENOMEM;
3062
3063 kg_cfg.num_extracts = 1;
3064 kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3065 kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3066 kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3067 kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3068
3069 err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3070 if (err) {
3071 dev_err(dev, "dpni_prepare_key_cfg failed\n");
3072 goto out_free_tbl;
3073 }
3074
3075 /* set QoS table */
3076 qos_cfg.default_tc = 0;
3077 qos_cfg.discard_on_miss = 0;
3078 qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3079 DPAA2_CLASSIFIER_DMA_SIZE,
3080 DMA_TO_DEVICE);
3081 if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3082 dev_err(dev, "QoS table DMA mapping failed\n");
3083 err = -ENOMEM;
3084 goto out_free_tbl;
3085 }
3086
3087 err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3088 if (err) {
3089 dev_err(dev, "dpni_set_qos_table failed\n");
3090 goto out_unmap_tbl;
3091 }
3092
3093 /* Add QoS table entries */
3094 key = kzalloc(key_size * 2, GFP_KERNEL);
3095 if (!key) {
3096 err = -ENOMEM;
3097 goto out_unmap_tbl;
3098 }
3099 mask = key + key_size;
3100 *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3101
3102 key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3103 DMA_TO_DEVICE);
3104 if (dma_mapping_error(dev, key_params.key_iova)) {
3105 dev_err(dev, "Qos table entry DMA mapping failed\n");
3106 err = -ENOMEM;
3107 goto out_free_key;
3108 }
3109
3110 key_params.mask_iova = key_params.key_iova + key_size;
3111 key_params.key_size = key_size;
3112
3113 /* We add rules for PCP-based distribution starting with highest
3114 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3115 * classes to accommodate all priority levels, the lowest ones end up
3116 * on TC 0 which was configured as default
3117 */
3118 for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3119 *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3120 dma_sync_single_for_device(dev, key_params.key_iova,
3121 key_size * 2, DMA_TO_DEVICE);
3122
3123 err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3124 &key_params, i, i);
3125 if (err) {
3126 dev_err(dev, "dpni_add_qos_entry failed\n");
3127 dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3128 goto out_unmap_key;
3129 }
3130 }
3131
3132 priv->vlan_cls_enabled = true;
3133
3134 /* Table and key memory is not persistent, clean everything up after
3135 * configuration is finished
3136 */
3137out_unmap_key:
3138 dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3139out_free_key:
3140 kfree(key);
3141out_unmap_tbl:
3142 dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3143 DMA_TO_DEVICE);
3144out_free_tbl:
3145 kfree(dma_mem);
3146
3147 return err;
3148}
3149
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003150/* Configure the DPNI object this interface is associated with */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003151static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003152{
3153 struct device *dev = &ls_dev->dev;
3154 struct dpaa2_eth_priv *priv;
3155 struct net_device *net_dev;
3156 int err;
3157
3158 net_dev = dev_get_drvdata(dev);
3159 priv = netdev_priv(net_dev);
3160
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003161 /* get a handle for the DPNI object */
Ioana Radulescu50eacbc2017-06-06 10:00:36 -05003162 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003163 if (err) {
3164 dev_err(dev, "dpni_open() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003165 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003166 }
3167
Ioana Radulescu311cffa2018-03-23 08:44:09 -05003168 /* Check if we can work with this DPNI object */
3169 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3170 &priv->dpni_ver_minor);
3171 if (err) {
3172 dev_err(dev, "dpni_get_api_version() failed\n");
3173 goto close;
3174 }
3175 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3176 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3177 priv->dpni_ver_major, priv->dpni_ver_minor,
3178 DPNI_VER_MAJOR, DPNI_VER_MINOR);
3179 err = -ENOTSUPP;
3180 goto close;
3181 }
3182
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003183 ls_dev->mc_io = priv->mc_io;
3184 ls_dev->mc_handle = priv->mc_token;
3185
3186 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3187 if (err) {
3188 dev_err(dev, "dpni_reset() failed\n");
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003189 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003190 }
3191
3192 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3193 &priv->dpni_attrs);
3194 if (err) {
3195 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003196 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003197 }
3198
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003199 err = dpaa2_eth_set_buffer_layout(priv);
Ioana Radulescu308f64e2017-10-29 08:20:40 +00003200 if (err)
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003201 goto close;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003202
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003203 dpaa2_eth_set_enqueue_mode(priv);
Ioana Ciocoi Radulescu1fa0f682019-02-04 17:00:36 +00003204
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003205 /* Enable pause frame support */
3206 if (dpaa2_eth_has_pause_support(priv)) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003207 err = dpaa2_eth_set_pause(priv);
Ioana Radulescu8eb3cef2019-08-28 17:08:15 +03003208 if (err)
3209 goto close;
3210 }
3211
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003212 err = dpaa2_eth_set_vlan_qos(priv);
Ioana Radulescu6aa90fe2020-05-31 00:08:09 +03003213 if (err && err != -EOPNOTSUPP)
3214 goto close;
3215
Xu Wang9334d5b2020-06-11 02:45:20 +00003216 priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3217 sizeof(struct dpaa2_eth_cls_rule),
3218 GFP_KERNEL);
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003219 if (!priv->cls_rules) {
3220 err = -ENOMEM;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003221 goto close;
Wei Yongjun97fff7c2020-04-27 10:43:22 +00003222 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003223
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003224 return 0;
3225
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003226close:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003227 dpni_close(priv->mc_io, 0, priv->mc_token);
Ioana Radulescuf6dda802017-10-29 08:20:39 +00003228
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003229 return err;
3230}
3231
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003232static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003233{
3234 int err;
3235
3236 err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3237 if (err)
3238 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3239 err);
3240
3241 dpni_close(priv->mc_io, 0, priv->mc_token);
3242}
3243
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003244static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3245 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003246{
3247 struct device *dev = priv->net_dev->dev.parent;
3248 struct dpni_queue queue;
3249 struct dpni_queue_id qid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003250 int err;
3251
3252 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003253 DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003254 if (err) {
3255 dev_err(dev, "dpni_get_queue(RX) failed\n");
3256 return err;
3257 }
3258
3259 fq->fqid = qid.fqid;
3260
3261 queue.destination.id = fq->channel->dpcon_id;
3262 queue.destination.type = DPNI_DEST_DPCON;
3263 queue.destination.priority = 1;
Ioana Radulescu75c583a2018-02-26 10:28:06 -06003264 queue.user_context = (u64)(uintptr_t)fq;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003265 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003266 DPNI_QUEUE_RX, fq->tc, fq->flowid,
Ioana Radulescu16fa1cf2019-05-23 17:38:22 +03003267 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003268 &queue);
3269 if (err) {
3270 dev_err(dev, "dpni_set_queue(RX) failed\n");
3271 return err;
3272 }
3273
Ioana Radulescud678be12019-03-01 17:47:24 +00003274 /* xdp_rxq setup */
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003275 /* only once for each channel */
3276 if (fq->tc > 0)
3277 return 0;
3278
Ioana Radulescud678be12019-03-01 17:47:24 +00003279 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3280 fq->flowid);
3281 if (err) {
3282 dev_err(dev, "xdp_rxq_info_reg failed\n");
3283 return err;
3284 }
3285
3286 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3287 MEM_TYPE_PAGE_ORDER0, NULL);
3288 if (err) {
3289 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3290 return err;
3291 }
3292
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003293 return 0;
3294}
3295
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003296static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3297 struct dpaa2_eth_fq *fq)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003298{
3299 struct device *dev = priv->net_dev->dev.parent;
3300 struct dpni_queue queue;
3301 struct dpni_queue_id qid;
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003302 int i, err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003303
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003304 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3305 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3306 DPNI_QUEUE_TX, i, fq->flowid,
3307 &queue, &qid);
3308 if (err) {
3309 dev_err(dev, "dpni_get_queue(TX) failed\n");
3310 return err;
3311 }
3312 fq->tx_fqid[i] = qid.fqid;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003313 }
3314
Ioana Radulescu15c87f62019-06-11 14:50:02 +03003315 /* All Tx queues belonging to the same flowid have the same qdbin */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003316 fq->tx_qdbin = qid.qdbin;
3317
3318 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3319 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3320 &queue, &qid);
3321 if (err) {
3322 dev_err(dev, "dpni_get_queue(TX_CONF) 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 = 0;
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,
3333 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3334 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3335 &queue);
3336 if (err) {
3337 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3338 return err;
3339 }
3340
3341 return 0;
3342}
3343
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003344/* Supported header fields for Rx hash distribution key */
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003345static const struct dpaa2_eth_dist_fields dist_fields[] = {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003346 {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003347 /* L2 header */
3348 .rxnfc_field = RXH_L2DA,
3349 .cls_prot = NET_PROT_ETH,
3350 .cls_field = NH_FLD_ETH_DA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003351 .id = DPAA2_ETH_DIST_ETHDST,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003352 .size = 6,
3353 }, {
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003354 .cls_prot = NET_PROT_ETH,
3355 .cls_field = NH_FLD_ETH_SA,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003356 .id = DPAA2_ETH_DIST_ETHSRC,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003357 .size = 6,
3358 }, {
3359 /* This is the last ethertype field parsed:
3360 * depending on frame format, it can be the MAC ethertype
3361 * or the VLAN etype.
3362 */
3363 .cls_prot = NET_PROT_ETH,
3364 .cls_field = NH_FLD_ETH_TYPE,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003365 .id = DPAA2_ETH_DIST_ETHTYPE,
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003366 .size = 2,
3367 }, {
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003368 /* VLAN header */
3369 .rxnfc_field = RXH_VLAN,
3370 .cls_prot = NET_PROT_VLAN,
3371 .cls_field = NH_FLD_VLAN_TCI,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003372 .id = DPAA2_ETH_DIST_VLAN,
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003373 .size = 2,
3374 }, {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003375 /* IP header */
3376 .rxnfc_field = RXH_IP_SRC,
3377 .cls_prot = NET_PROT_IP,
3378 .cls_field = NH_FLD_IP_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003379 .id = DPAA2_ETH_DIST_IPSRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003380 .size = 4,
3381 }, {
3382 .rxnfc_field = RXH_IP_DST,
3383 .cls_prot = NET_PROT_IP,
3384 .cls_field = NH_FLD_IP_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003385 .id = DPAA2_ETH_DIST_IPDST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003386 .size = 4,
3387 }, {
3388 .rxnfc_field = RXH_L3_PROTO,
3389 .cls_prot = NET_PROT_IP,
3390 .cls_field = NH_FLD_IP_PROTO,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003391 .id = DPAA2_ETH_DIST_IPPROTO,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003392 .size = 1,
3393 }, {
3394 /* Using UDP ports, this is functionally equivalent to raw
3395 * byte pairs from L4 header.
3396 */
3397 .rxnfc_field = RXH_L4_B_0_1,
3398 .cls_prot = NET_PROT_UDP,
3399 .cls_field = NH_FLD_UDP_PORT_SRC,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003400 .id = DPAA2_ETH_DIST_L4SRC,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003401 .size = 2,
3402 }, {
3403 .rxnfc_field = RXH_L4_B_2_3,
3404 .cls_prot = NET_PROT_UDP,
3405 .cls_field = NH_FLD_UDP_PORT_DST,
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003406 .id = DPAA2_ETH_DIST_L4DST,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003407 .size = 2,
3408 },
3409};
3410
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003411/* Configure the Rx hash key using the legacy API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003412static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003413{
3414 struct device *dev = priv->net_dev->dev.parent;
3415 struct dpni_rx_tc_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003416 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003417
3418 memset(&dist_cfg, 0, sizeof(dist_cfg));
3419
3420 dist_cfg.key_cfg_iova = key;
3421 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3422 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3423
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003424 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3425 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3426 i, &dist_cfg);
3427 if (err) {
3428 dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3429 break;
3430 }
3431 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003432
3433 return err;
3434}
3435
3436/* Configure the Rx hash key using the new API */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003437static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003438{
3439 struct device *dev = priv->net_dev->dev.parent;
3440 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003441 int i, err = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003442
3443 memset(&dist_cfg, 0, sizeof(dist_cfg));
3444
3445 dist_cfg.key_cfg_iova = key;
3446 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3447 dist_cfg.enable = 1;
3448
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003449 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3450 dist_cfg.tc = i;
3451 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3452 &dist_cfg);
3453 if (err) {
3454 dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3455 break;
3456 }
3457 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003458
3459 return err;
3460}
3461
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003462/* Configure the Rx flow classification key */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003463static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003464{
3465 struct device *dev = priv->net_dev->dev.parent;
3466 struct dpni_rx_dist_cfg dist_cfg;
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003467 int i, err = 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003468
3469 memset(&dist_cfg, 0, sizeof(dist_cfg));
3470
3471 dist_cfg.key_cfg_iova = key;
3472 dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3473 dist_cfg.enable = 1;
3474
Ioana Radulescu685e39e2020-05-31 00:08:08 +03003475 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3476 dist_cfg.tc = i;
3477 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3478 &dist_cfg);
3479 if (err) {
3480 dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3481 break;
3482 }
3483 }
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003484
3485 return err;
3486}
3487
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003488/* Size of the Rx flow classification key */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003489int dpaa2_eth_cls_key_size(u64 fields)
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003490{
3491 int i, size = 0;
3492
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003493 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3494 if (!(fields & dist_fields[i].id))
3495 continue;
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003496 size += dist_fields[i].size;
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003497 }
Ioana Radulescuafb90db2018-10-01 13:44:58 +03003498
3499 return size;
3500}
3501
3502/* Offset of header field in Rx classification key */
3503int dpaa2_eth_cls_fld_off(int prot, int field)
3504{
3505 int i, off = 0;
3506
3507 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3508 if (dist_fields[i].cls_prot == prot &&
3509 dist_fields[i].cls_field == field)
3510 return off;
3511 off += dist_fields[i].size;
3512 }
3513
3514 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3515 return 0;
3516}
3517
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003518/* Prune unused fields from the classification rule.
3519 * Used when masking is not supported
3520 */
3521void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3522{
3523 int off = 0, new_off = 0;
3524 int i, size;
3525
3526 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3527 size = dist_fields[i].size;
3528 if (dist_fields[i].id & fields) {
3529 memcpy(key_mem + new_off, key_mem + off, size);
3530 new_off += size;
3531 }
3532 off += size;
3533 }
3534}
3535
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003536/* Set Rx distribution (hash or flow classification) key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003537 * flags is a combination of RXH_ bits
3538 */
Ioana Ciornei3233c152018-10-12 16:27:29 +00003539static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
3540 enum dpaa2_eth_rx_dist type, u64 flags)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003541{
3542 struct device *dev = net_dev->dev.parent;
3543 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3544 struct dpkg_profile_cfg cls_cfg;
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003545 u32 rx_hash_fields = 0;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003546 dma_addr_t key_iova;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003547 u8 *dma_mem;
3548 int i;
3549 int err = 0;
3550
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003551 memset(&cls_cfg, 0, sizeof(cls_cfg));
3552
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003553 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003554 struct dpkg_extract *key =
3555 &cls_cfg.extracts[cls_cfg.num_extracts];
3556
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003557 /* For both Rx hashing and classification keys
3558 * we set only the selected fields.
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003559 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003560 if (!(flags & dist_fields[i].id))
3561 continue;
3562 if (type == DPAA2_ETH_RX_DIST_HASH)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003563 rx_hash_fields |= dist_fields[i].rxnfc_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003564
3565 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
3566 dev_err(dev, "error adding key extraction rule, too many rules?\n");
3567 return -E2BIG;
3568 }
3569
3570 key->type = DPKG_EXTRACT_FROM_HDR;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003571 key->extract.from_hdr.prot = dist_fields[i].cls_prot;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003572 key->extract.from_hdr.type = DPKG_FULL_FIELD;
Ioana Radulescuf76c4832018-10-01 13:44:56 +03003573 key->extract.from_hdr.field = dist_fields[i].cls_field;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003574 cls_cfg.num_extracts++;
3575 }
3576
Ioana Radulescue40ef9e2017-06-06 10:00:30 -05003577 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003578 if (!dma_mem)
3579 return -ENOMEM;
3580
3581 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3582 if (err) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003583 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003584 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003585 }
3586
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003587 /* Prepare for setting the rx dist */
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003588 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3589 DMA_TO_DEVICE);
3590 if (dma_mapping_error(dev, key_iova)) {
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003591 dev_err(dev, "DMA mapping failed\n");
3592 err = -ENOMEM;
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003593 goto free_key;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003594 }
3595
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003596 if (type == DPAA2_ETH_RX_DIST_HASH) {
3597 if (dpaa2_eth_has_legacy_dist(priv))
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003598 err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003599 else
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003600 err = dpaa2_eth_config_hash_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003601 } else {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003602 err = dpaa2_eth_config_cls_key(priv, key_iova);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003603 }
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003604
3605 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3606 DMA_TO_DEVICE);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003607 if (!err && type == DPAA2_ETH_RX_DIST_HASH)
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003608 priv->rx_hash_fields = rx_hash_fields;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003609
Ioana Radulescudf85aeb2018-10-01 13:44:55 +03003610free_key:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003611 kfree(dma_mem);
3612 return err;
3613}
3614
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003615int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3616{
3617 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003618 u64 key = 0;
3619 int i;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003620
3621 if (!dpaa2_eth_hash_enabled(priv))
3622 return -EOPNOTSUPP;
3623
Ioana Ciocoi Radulescu3a1e6b82019-04-16 17:13:29 +00003624 for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3625 if (dist_fields[i].rxnfc_field & flags)
3626 key |= dist_fields[i].id;
3627
3628 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003629}
3630
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003631int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3632{
3633 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3634}
3635
3636static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003637{
3638 struct device *dev = priv->net_dev->dev.parent;
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003639 int err;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003640
3641 /* Check if we actually support Rx flow classification */
3642 if (dpaa2_eth_has_legacy_dist(priv)) {
3643 dev_dbg(dev, "Rx cls not supported by current MC version\n");
3644 return -EOPNOTSUPP;
3645 }
3646
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003647 if (!dpaa2_eth_fs_enabled(priv)) {
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003648 dev_dbg(dev, "Rx cls disabled in DPNI options\n");
3649 return -EOPNOTSUPP;
3650 }
3651
3652 if (!dpaa2_eth_hash_enabled(priv)) {
3653 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
3654 return -EOPNOTSUPP;
3655 }
3656
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003657 /* If there is no support for masking in the classification table,
3658 * we don't set a default key, as it will depend on the rules
3659 * added by the user at runtime.
3660 */
3661 if (!dpaa2_eth_fs_mask_enabled(priv))
3662 goto out;
3663
3664 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003665 if (err)
3666 return err;
3667
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003668out:
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003669 priv->rx_cls_enabled = 1;
3670
Ioana Ciocoi Radulescudf8e2492019-04-16 17:13:28 +00003671 return 0;
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003672}
3673
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003674/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
3675 * frame queues and channels
3676 */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003677static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003678{
3679 struct net_device *net_dev = priv->net_dev;
3680 struct device *dev = net_dev->dev.parent;
3681 struct dpni_pools_cfg pools_params;
3682 struct dpni_error_cfg err_cfg;
3683 int err = 0;
3684 int i;
3685
3686 pools_params.num_dpbp = 1;
3687 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
3688 pools_params.pools[0].backup_pool = 0;
Ioana Ciorneiefa6a7d2020-05-15 15:30:22 +03003689 pools_params.pools[0].buffer_size = priv->rx_buf_size;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003690 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
3691 if (err) {
3692 dev_err(dev, "dpni_set_pools() failed\n");
3693 return err;
3694 }
3695
Ioana Radulescu227686b2018-07-27 09:12:59 -05003696 /* have the interface implicitly distribute traffic based on
3697 * the default hash key
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003698 */
Ioana Radulescu227686b2018-07-27 09:12:59 -05003699 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
Ioana Ciocoi Radulescuedad8d22018-09-24 15:36:21 +00003700 if (err && err != -EOPNOTSUPP)
Ioana Radulescu0f4c2952017-10-11 08:29:50 -05003701 dev_err(dev, "Failed to configure hashing\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003702
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003703 /* Configure the flow classification key; it includes all
3704 * supported header fields and cannot be modified at runtime
3705 */
Ioana Ciocoi Radulescu2d680232019-04-16 17:13:30 +00003706 err = dpaa2_eth_set_default_cls(priv);
Ioana Radulescu4aaaf9b2018-10-01 13:44:57 +03003707 if (err && err != -EOPNOTSUPP)
3708 dev_err(dev, "Failed to configure Rx classification key\n");
3709
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003710 /* Configure handling of error frames */
Ioana Radulescu39163c02017-06-06 10:00:39 -05003711 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003712 err_cfg.set_frame_annotation = 1;
3713 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
3714 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
3715 &err_cfg);
3716 if (err) {
3717 dev_err(dev, "dpni_set_errors_behavior failed\n");
3718 return err;
3719 }
3720
3721 /* Configure Rx and Tx conf queues to generate CDANs */
3722 for (i = 0; i < priv->num_fqs; i++) {
3723 switch (priv->fq[i].type) {
3724 case DPAA2_RX_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003725 err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003726 break;
3727 case DPAA2_TX_CONF_FQ:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003728 err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003729 break;
3730 default:
3731 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
3732 return -EINVAL;
3733 }
3734 if (err)
3735 return err;
3736 }
3737
3738 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
3739 DPNI_QUEUE_TX, &priv->tx_qdid);
3740 if (err) {
3741 dev_err(dev, "dpni_get_qdid() failed\n");
3742 return err;
3743 }
3744
3745 return 0;
3746}
3747
3748/* Allocate rings for storing incoming frame descriptors */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003749static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003750{
3751 struct net_device *net_dev = priv->net_dev;
3752 struct device *dev = net_dev->dev.parent;
3753 int i;
3754
3755 for (i = 0; i < priv->num_channels; i++) {
3756 priv->channel[i]->store =
3757 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
3758 if (!priv->channel[i]->store) {
3759 netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
3760 goto err_ring;
3761 }
3762 }
3763
3764 return 0;
3765
3766err_ring:
3767 for (i = 0; i < priv->num_channels; i++) {
3768 if (!priv->channel[i]->store)
3769 break;
3770 dpaa2_io_store_destroy(priv->channel[i]->store);
3771 }
3772
3773 return -ENOMEM;
3774}
3775
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003776static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003777{
3778 int i;
3779
3780 for (i = 0; i < priv->num_channels; i++)
3781 dpaa2_io_store_destroy(priv->channel[i]->store);
3782}
3783
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003784static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003785{
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003786 struct net_device *net_dev = priv->net_dev;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003787 struct device *dev = net_dev->dev.parent;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003788 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003789 int err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003790
3791 /* Get firmware address, if any */
3792 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
3793 if (err) {
3794 dev_err(dev, "dpni_get_port_mac_addr() failed\n");
3795 return err;
3796 }
3797
3798 /* Get DPNI attributes address, if any */
3799 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3800 dpni_mac_addr);
3801 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003802 dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003803 return err;
3804 }
3805
3806 /* First check if firmware has any address configured by bootloader */
3807 if (!is_zero_ether_addr(mac_addr)) {
3808 /* If the DPMAC addr != DPNI addr, update it */
3809 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
3810 err = dpni_set_primary_mac_addr(priv->mc_io, 0,
3811 priv->mc_token,
3812 mac_addr);
3813 if (err) {
3814 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
3815 return err;
3816 }
3817 }
3818 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
3819 } else if (is_zero_ether_addr(dpni_mac_addr)) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003820 /* No MAC address configured, fill in net_dev->dev_addr
3821 * with a random one
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003822 */
3823 eth_hw_addr_random(net_dev);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003824 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
3825
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003826 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
3827 net_dev->dev_addr);
3828 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003829 dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003830 return err;
3831 }
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003832
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003833 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
3834 * practical purposes, this will be our "permanent" mac address,
3835 * at least until the next reboot. This move will also permit
3836 * register_netdevice() to properly fill up net_dev->perm_addr.
3837 */
3838 net_dev->addr_assign_type = NET_ADDR_PERM;
3839 } else {
3840 /* NET_ADDR_PERM is default, all we have to do is
3841 * fill in the device addr.
3842 */
3843 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
3844 }
3845
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003846 return 0;
3847}
3848
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003849static int dpaa2_eth_netdev_init(struct net_device *net_dev)
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003850{
3851 struct device *dev = net_dev->dev.parent;
3852 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003853 u32 options = priv->dpni_attrs.options;
3854 u64 supported = 0, not_supported = 0;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003855 u8 bcast_addr[ETH_ALEN];
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003856 u8 num_queues;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003857 int err;
3858
3859 net_dev->netdev_ops = &dpaa2_eth_ops;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003860 net_dev->ethtool_ops = &dpaa2_ethtool_ops;
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003861
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003862 err = dpaa2_eth_set_mac_addr(priv);
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003863 if (err)
3864 return err;
3865
3866 /* Explicitly add the broadcast address to the MAC filtering table */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003867 eth_broadcast_addr(bcast_addr);
3868 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3869 if (err) {
Ioana Radulescu6ab00862017-06-06 10:00:40 -05003870 dev_err(dev, "dpni_add_mac_addr() failed\n");
3871 return err;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003872 }
3873
Ioana Radulescu3ccc8d42018-07-09 10:01:10 -05003874 /* Set MTU upper limit; lower limit is 68B (default value) */
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003875 net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
Ioana Radulescu00fee002018-07-09 10:01:11 -05003876 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
Ioana Radulescu81f34e92018-07-12 12:12:29 -05003877 DPAA2_ETH_MFL);
Ioana Radulescu00fee002018-07-09 10:01:11 -05003878 if (err) {
3879 dev_err(dev, "dpni_set_max_frame_length() failed\n");
3880 return err;
3881 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003882
Ioana Radulescubb5b42c2017-06-06 10:00:41 -05003883 /* Set actual number of queues in the net device */
3884 num_queues = dpaa2_eth_queue_count(priv);
3885 err = netif_set_real_num_tx_queues(net_dev, num_queues);
3886 if (err) {
3887 dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
3888 return err;
3889 }
3890 err = netif_set_real_num_rx_queues(net_dev, num_queues);
3891 if (err) {
3892 dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
3893 return err;
3894 }
3895
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003896 /* Capabilities listing */
3897 supported |= IFF_LIVE_ADDR_CHANGE;
3898
3899 if (options & DPNI_OPT_NO_MAC_FILTER)
3900 not_supported |= IFF_UNICAST_FLT;
3901 else
3902 supported |= IFF_UNICAST_FLT;
3903
3904 net_dev->priv_flags |= supported;
3905 net_dev->priv_flags &= ~not_supported;
3906
3907 /* Features */
3908 net_dev->features = NETIF_F_RXCSUM |
3909 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3910 NETIF_F_SG | NETIF_F_HIGHDMA |
Ioana Ciornei3657cda2020-07-21 19:38:25 +03003911 NETIF_F_LLTX | NETIF_F_HW_TC;
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05003912 net_dev->hw_features = net_dev->features;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003913
3914 return 0;
3915}
3916
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003917static int dpaa2_eth_poll_link_state(void *arg)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003918{
3919 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
3920 int err;
3921
3922 while (!kthread_should_stop()) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003923 err = dpaa2_eth_link_state_update(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003924 if (unlikely(err))
3925 return err;
3926
3927 msleep(DPAA2_ETH_LINK_STATE_REFRESH);
3928 }
3929
3930 return 0;
3931}
3932
Ioana Ciornei71947922019-10-31 01:18:31 +02003933static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
3934{
3935 struct fsl_mc_device *dpni_dev, *dpmac_dev;
3936 struct dpaa2_mac *mac;
3937 int err;
3938
3939 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
3940 dpmac_dev = fsl_mc_get_endpoint(dpni_dev);
Ioana Ciornei841eb402020-07-14 15:08:16 +03003941 if (IS_ERR_OR_NULL(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
Ioana Ciornei71947922019-10-31 01:18:31 +02003942 return 0;
3943
3944 if (dpaa2_mac_is_type_fixed(dpmac_dev, priv->mc_io))
3945 return 0;
3946
3947 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
3948 if (!mac)
3949 return -ENOMEM;
3950
3951 mac->mc_dev = dpmac_dev;
3952 mac->mc_io = priv->mc_io;
3953 mac->net_dev = priv->net_dev;
3954
3955 err = dpaa2_mac_connect(mac);
3956 if (err) {
3957 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint\n");
3958 kfree(mac);
3959 return err;
3960 }
3961 priv->mac = mac;
3962
3963 return 0;
3964}
3965
3966static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
3967{
3968 if (!priv->mac)
3969 return;
3970
3971 dpaa2_mac_disconnect(priv->mac);
3972 kfree(priv->mac);
3973 priv->mac = NULL;
3974}
3975
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003976static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
3977{
Ioana Radulescu112197d2017-10-11 08:29:49 -05003978 u32 status = ~0;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003979 struct device *dev = (struct device *)arg;
3980 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
3981 struct net_device *net_dev = dev_get_drvdata(dev);
Ioana Ciornei71947922019-10-31 01:18:31 +02003982 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003983 int err;
3984
3985 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
3986 DPNI_IRQ_INDEX, &status);
3987 if (unlikely(err)) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05003988 netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
Ioana Radulescu112197d2017-10-11 08:29:49 -05003989 return IRQ_HANDLED;
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003990 }
3991
Ioana Radulescu112197d2017-10-11 08:29:49 -05003992 if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003993 dpaa2_eth_link_state_update(netdev_priv(net_dev));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05003994
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02003995 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03003996 dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
3997 dpaa2_eth_update_tx_fqids(priv);
Ioana Ciornei71947922019-10-31 01:18:31 +02003998
3999 rtnl_lock();
4000 if (priv->mac)
4001 dpaa2_eth_disconnect_mac(priv);
4002 else
4003 dpaa2_eth_connect_mac(priv);
4004 rtnl_unlock();
Ioana Ciorneif5c3fff2019-10-31 01:18:30 +02004005 }
Florin Chiculita8398b372019-10-16 10:36:22 +03004006
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004007 return IRQ_HANDLED;
4008}
4009
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004010static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004011{
4012 int err = 0;
4013 struct fsl_mc_device_irq *irq;
4014
4015 err = fsl_mc_allocate_irqs(ls_dev);
4016 if (err) {
4017 dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4018 return err;
4019 }
4020
4021 irq = ls_dev->irqs[0];
4022 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
Ioana Radulescufdc9b532018-03-23 08:44:05 -05004023 NULL, dpni_irq0_handler_thread,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004024 IRQF_NO_SUSPEND | IRQF_ONESHOT,
4025 dev_name(&ls_dev->dev), &ls_dev->dev);
4026 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004027 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004028 goto free_mc_irq;
4029 }
4030
4031 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
Florin Chiculita8398b372019-10-16 10:36:22 +03004032 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4033 DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004034 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004035 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004036 goto free_irq;
4037 }
4038
4039 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4040 DPNI_IRQ_INDEX, 1);
4041 if (err < 0) {
Ioana Radulescu77160af2017-06-06 10:00:28 -05004042 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004043 goto free_irq;
4044 }
4045
4046 return 0;
4047
4048free_irq:
4049 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
4050free_mc_irq:
4051 fsl_mc_free_irqs(ls_dev);
4052
4053 return err;
4054}
4055
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004056static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004057{
4058 int i;
4059 struct dpaa2_eth_channel *ch;
4060
4061 for (i = 0; i < priv->num_channels; i++) {
4062 ch = priv->channel[i];
4063 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4064 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
4065 NAPI_POLL_WEIGHT);
4066 }
4067}
4068
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004069static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004070{
4071 int i;
4072 struct dpaa2_eth_channel *ch;
4073
4074 for (i = 0; i < priv->num_channels; i++) {
4075 ch = priv->channel[i];
4076 netif_napi_del(&ch->napi);
4077 }
4078}
4079
4080static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4081{
4082 struct device *dev;
4083 struct net_device *net_dev = NULL;
4084 struct dpaa2_eth_priv *priv = NULL;
4085 int err = 0;
4086
4087 dev = &dpni_dev->dev;
4088
4089 /* Net device */
Ioana Radulescuab1e6de2019-06-11 14:50:03 +03004090 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004091 if (!net_dev) {
4092 dev_err(dev, "alloc_etherdev_mq() failed\n");
4093 return -ENOMEM;
4094 }
4095
4096 SET_NETDEV_DEV(net_dev, dev);
4097 dev_set_drvdata(dev, net_dev);
4098
4099 priv = netdev_priv(net_dev);
4100 priv->net_dev = net_dev;
4101
Ioana Radulescu08eb2392017-05-24 07:13:27 -05004102 priv->iommu_domain = iommu_get_domain_for_dev(dev);
4103
Yangbo Lu1cf773b2020-09-18 17:08:01 +08004104 priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4105 priv->rx_tstamp = false;
4106
Yangbo Luc5521182020-09-18 17:08:02 +08004107 priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
4108 if (!priv->dpaa2_ptp_wq) {
4109 err = -ENOMEM;
4110 goto err_wq_alloc;
4111 }
4112
4113 INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4114
4115 skb_queue_head_init(&priv->tx_skbs);
4116
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004117 /* Obtain a MC portal */
4118 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4119 &priv->mc_io);
4120 if (err) {
Ioana Radulescu8c369612018-03-20 07:04:46 -05004121 if (err == -ENXIO)
4122 err = -EPROBE_DEFER;
4123 else
4124 dev_err(dev, "MC portal allocation failed\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004125 goto err_portal_alloc;
4126 }
4127
4128 /* MC objects initialization and configuration */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004129 err = dpaa2_eth_setup_dpni(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004130 if (err)
4131 goto err_dpni_setup;
4132
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004133 err = dpaa2_eth_setup_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004134 if (err)
4135 goto err_dpio_setup;
4136
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004137 dpaa2_eth_setup_fqs(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004138
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004139 err = dpaa2_eth_setup_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004140 if (err)
4141 goto err_dpbp_setup;
4142
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004143 err = dpaa2_eth_bind_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004144 if (err)
4145 goto err_bind;
4146
4147 /* Add a NAPI context for each channel */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004148 dpaa2_eth_add_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004149
4150 /* Percpu statistics */
4151 priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4152 if (!priv->percpu_stats) {
4153 dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4154 err = -ENOMEM;
4155 goto err_alloc_percpu_stats;
4156 }
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004157 priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4158 if (!priv->percpu_extras) {
4159 dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4160 err = -ENOMEM;
4161 goto err_alloc_percpu_extras;
4162 }
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004163
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004164 priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4165 if (!priv->sgt_cache) {
4166 dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4167 err = -ENOMEM;
4168 goto err_alloc_sgt_cache;
4169 }
4170
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004171 err = dpaa2_eth_netdev_init(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004172 if (err)
4173 goto err_netdev_init;
4174
4175 /* Configure checksum offload based on current interface flags */
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004176 err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004177 if (err)
4178 goto err_csum;
4179
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004180 err = dpaa2_eth_set_tx_csum(priv,
4181 !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004182 if (err)
4183 goto err_csum;
4184
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004185 err = dpaa2_eth_alloc_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004186 if (err)
4187 goto err_alloc_rings;
4188
Ioana Ciorneif395b692020-05-31 00:08:13 +03004189#ifdef CONFIG_FSL_DPAA2_ETH_DCB
4190 if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4191 priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4192 net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4193 } else {
4194 dev_dbg(dev, "PFC not supported\n");
4195 }
4196#endif
4197
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004198 err = dpaa2_eth_setup_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004199 if (err) {
4200 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004201 priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004202 "%s_poll_link", net_dev->name);
4203 if (IS_ERR(priv->poll_thread)) {
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004204 dev_err(dev, "Error starting polling thread\n");
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004205 goto err_poll_thread;
4206 }
4207 priv->do_link_poll = true;
4208 }
4209
Ioana Ciornei71947922019-10-31 01:18:31 +02004210 err = dpaa2_eth_connect_mac(priv);
4211 if (err)
4212 goto err_connect_mac;
4213
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004214 err = register_netdev(net_dev);
4215 if (err < 0) {
4216 dev_err(dev, "register_netdev() failed\n");
4217 goto err_netdev_reg;
4218 }
4219
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004220#ifdef CONFIG_DEBUG_FS
4221 dpaa2_dbg_add(priv);
4222#endif
4223
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004224 dev_info(dev, "Probed interface %s\n", net_dev->name);
4225 return 0;
4226
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004227err_netdev_reg:
Ioana Ciornei71947922019-10-31 01:18:31 +02004228 dpaa2_eth_disconnect_mac(priv);
4229err_connect_mac:
Ioana Radulescu7f12c8a32018-08-29 04:42:39 -05004230 if (priv->do_link_poll)
4231 kthread_stop(priv->poll_thread);
4232 else
4233 fsl_mc_free_irqs(dpni_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004234err_poll_thread:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004235 dpaa2_eth_free_rings(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004236err_alloc_rings:
4237err_csum:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004238err_netdev_init:
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004239 free_percpu(priv->sgt_cache);
4240err_alloc_sgt_cache:
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004241 free_percpu(priv->percpu_extras);
4242err_alloc_percpu_extras:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004243 free_percpu(priv->percpu_stats);
4244err_alloc_percpu_stats:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004245 dpaa2_eth_del_ch_napi(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004246err_bind:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004247 dpaa2_eth_free_dpbp(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004248err_dpbp_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004249 dpaa2_eth_free_dpio(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004250err_dpio_setup:
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004251 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004252err_dpni_setup:
4253 fsl_mc_portal_free(priv->mc_io);
4254err_portal_alloc:
Yangbo Luc5521182020-09-18 17:08:02 +08004255 destroy_workqueue(priv->dpaa2_ptp_wq);
4256err_wq_alloc:
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004257 dev_set_drvdata(dev, NULL);
4258 free_netdev(net_dev);
4259
4260 return err;
4261}
4262
4263static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4264{
4265 struct device *dev;
4266 struct net_device *net_dev;
4267 struct dpaa2_eth_priv *priv;
4268
4269 dev = &ls_dev->dev;
4270 net_dev = dev_get_drvdata(dev);
4271 priv = netdev_priv(net_dev);
4272
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004273#ifdef CONFIG_DEBUG_FS
4274 dpaa2_dbg_remove(priv);
4275#endif
Ioana Ciornei71947922019-10-31 01:18:31 +02004276 rtnl_lock();
4277 dpaa2_eth_disconnect_mac(priv);
4278 rtnl_unlock();
4279
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004280 unregister_netdev(net_dev);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004281
4282 if (priv->do_link_poll)
4283 kthread_stop(priv->poll_thread);
4284 else
4285 fsl_mc_free_irqs(ls_dev);
4286
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004287 dpaa2_eth_free_rings(priv);
Ioana Ciorneid70446e2020-06-29 21:47:11 +03004288 free_percpu(priv->sgt_cache);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004289 free_percpu(priv->percpu_stats);
Ioana Radulescu85047ab2017-04-28 04:50:31 -05004290 free_percpu(priv->percpu_extras);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004291
Ioana Ciornei5d8dccf2020-08-31 21:12:39 +03004292 dpaa2_eth_del_ch_napi(priv);
4293 dpaa2_eth_free_dpbp(priv);
4294 dpaa2_eth_free_dpio(priv);
4295 dpaa2_eth_free_dpni(priv);
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004296
4297 fsl_mc_portal_free(priv->mc_io);
4298
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004299 free_netdev(net_dev);
4300
Ioana Radulescu4bc07aa2018-03-23 10:23:36 -05004301 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
Ioana Radulescu7472dd92018-03-23 08:44:06 -05004302
Ioana Radulescu6e2387e2017-04-28 04:50:29 -05004303 return 0;
4304}
4305
4306static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
4307 {
4308 .vendor = FSL_MC_VENDOR_FREESCALE,
4309 .obj_type = "dpni",
4310 },
4311 { .vendor = 0x0 }
4312};
4313MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
4314
4315static struct fsl_mc_driver dpaa2_eth_driver = {
4316 .driver = {
4317 .name = KBUILD_MODNAME,
4318 .owner = THIS_MODULE,
4319 },
4320 .probe = dpaa2_eth_probe,
4321 .remove = dpaa2_eth_remove,
4322 .match_id_table = dpaa2_eth_match_id_table
4323};
4324
Ioana Radulescu091a19e2019-01-18 16:16:00 +00004325static int __init dpaa2_eth_driver_init(void)
4326{
4327 int err;
4328
4329 dpaa2_eth_dbg_init();
4330 err = fsl_mc_driver_register(&dpaa2_eth_driver);
4331 if (err) {
4332 dpaa2_eth_dbg_exit();
4333 return err;
4334 }
4335
4336 return 0;
4337}
4338
4339static void __exit dpaa2_eth_driver_exit(void)
4340{
4341 dpaa2_eth_dbg_exit();
4342 fsl_mc_driver_unregister(&dpaa2_eth_driver);
4343}
4344
4345module_init(dpaa2_eth_driver_init);
4346module_exit(dpaa2_eth_driver_exit);