dpaa2-eth.c 118.2 KB
Newer Older
1
// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2
/* Copyright 2014-2016 Freescale Semiconductor Inc.
3
 * Copyright 2016-2020 NXP
4 5 6 7 8 9 10 11 12
 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/etherdevice.h>
#include <linux/of_net.h>
#include <linux/interrupt.h>
#include <linux/msi.h>
#include <linux/kthread.h>
13
#include <linux/iommu.h>
14
#include <linux/fsl/mc.h>
15 16
#include <linux/bpf.h>
#include <linux/bpf_trace.h>
17
#include <linux/fsl/ptp_qoriq.h>
18
#include <linux/ptp_classify.h>
19
#include <net/pkt_cls.h>
20 21
#include <net/sock.h>

22 23
#include "dpaa2-eth.h"

24 25 26 27 28 29
/* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
 * using trace events only need to #include <trace/events/sched.h>
 */
#define CREATE_TRACE_POINTS
#include "dpaa2-eth-trace.h"

30 31 32 33
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Freescale Semiconductor, Inc");
MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");

34 35 36
struct ptp_qoriq *dpaa2_ptp;
EXPORT_SYMBOL(dpaa2_ptp);

37 38 39 40 41 42 43 44 45 46
static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
				dma_addr_t iova_addr)
{
	phys_addr_t phys_addr;

	phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;

	return phys_to_virt(phys_addr);
}

47 48 49
static void dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv *priv,
				       u32 fd_status,
				       struct sk_buff *skb)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
{
	skb_checksum_none_assert(skb);

	/* HW checksum validation is disabled, nothing to do here */
	if (!(priv->net_dev->features & NETIF_F_RXCSUM))
		return;

	/* Read checksum validation bits */
	if (!((fd_status & DPAA2_FAS_L3CV) &&
	      (fd_status & DPAA2_FAS_L4CV)))
		return;

	/* Inform the stack there's no need to compute L3/L4 csum anymore */
	skb->ip_summed = CHECKSUM_UNNECESSARY;
}

/* Free a received FD.
 * Not to be used for Tx conf FDs or on any other paths.
 */
69 70 71
static void dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv,
				 const struct dpaa2_fd *fd,
				 void *vaddr)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
{
	struct device *dev = priv->net_dev->dev.parent;
	dma_addr_t addr = dpaa2_fd_get_addr(fd);
	u8 fd_format = dpaa2_fd_get_format(fd);
	struct dpaa2_sg_entry *sgt;
	void *sg_vaddr;
	int i;

	/* If single buffer frame, just free the data buffer */
	if (fd_format == dpaa2_fd_single)
		goto free_buf;
	else if (fd_format != dpaa2_fd_sg)
		/* We don't support any other format */
		return;

87 88 89
	/* For S/G frames, we first need to free all SG entries
	 * except the first one, which was taken care of already
	 */
90
	sgt = vaddr + dpaa2_fd_get_offset(fd);
91
	for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
92
		addr = dpaa2_sg_get_addr(&sgt[i]);
93
		sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
94
		dma_unmap_page(dev, addr, priv->rx_buf_size,
95
			       DMA_BIDIRECTIONAL);
96

97
		free_pages((unsigned long)sg_vaddr, 0);
98 99 100 101 102
		if (dpaa2_sg_is_final(&sgt[i]))
			break;
	}

free_buf:
103
	free_pages((unsigned long)vaddr, 0);
104 105 106
}

/* Build a linear skb based on a single-buffer frame descriptor */
107 108 109
static struct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch,
						  const struct dpaa2_fd *fd,
						  void *fd_vaddr)
110 111 112 113 114
{
	struct sk_buff *skb = NULL;
	u16 fd_offset = dpaa2_fd_get_offset(fd);
	u32 fd_length = dpaa2_fd_get_len(fd);

115 116
	ch->buf_count--;

117
	skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
118 119 120 121 122 123 124 125 126 127
	if (unlikely(!skb))
		return NULL;

	skb_reserve(skb, fd_offset);
	skb_put(skb, fd_length);

	return skb;
}

/* Build a non linear (fragmented) skb based on a S/G table */
128 129 130
static struct sk_buff *dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv *priv,
						struct dpaa2_eth_channel *ch,
						struct dpaa2_sg_entry *sgt)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
{
	struct sk_buff *skb = NULL;
	struct device *dev = priv->net_dev->dev.parent;
	void *sg_vaddr;
	dma_addr_t sg_addr;
	u16 sg_offset;
	u32 sg_length;
	struct page *page, *head_page;
	int page_offset;
	int i;

	for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
		struct dpaa2_sg_entry *sge = &sgt[i];

		/* NOTE: We only support SG entries in dpaa2_sg_single format,
		 * but this is the only format we may receive from HW anyway
		 */

		/* Get the address and length from the S/G entry */
		sg_addr = dpaa2_sg_get_addr(sge);
151
		sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
152
		dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
153
			       DMA_BIDIRECTIONAL);
154 155 156 157 158

		sg_length = dpaa2_sg_get_len(sge);

		if (i == 0) {
			/* We build the skb around the first data buffer */
159
			skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
160
			if (unlikely(!skb)) {
161 162 163
				/* Free the first SG entry now, since we already
				 * unmapped it and obtained the virtual address
				 */
164
				free_pages((unsigned long)sg_vaddr, 0);
165

166 167 168 169 170 171 172 173
				/* We still need to subtract the buffers used
				 * by this FD from our software counter
				 */
				while (!dpaa2_sg_is_final(&sgt[i]) &&
				       i < DPAA2_ETH_MAX_SG_ENTRIES)
					i++;
				break;
			}
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

			sg_offset = dpaa2_sg_get_offset(sge);
			skb_reserve(skb, sg_offset);
			skb_put(skb, sg_length);
		} else {
			/* Rest of the data buffers are stored as skb frags */
			page = virt_to_page(sg_vaddr);
			head_page = virt_to_head_page(sg_vaddr);

			/* Offset in page (which may be compound).
			 * Data in subsequent SG entries is stored from the
			 * beginning of the buffer, so we don't need to add the
			 * sg_offset.
			 */
			page_offset = ((unsigned long)sg_vaddr &
				(PAGE_SIZE - 1)) +
				(page_address(page) - page_address(head_page));

			skb_add_rx_frag(skb, i - 1, head_page, page_offset,
193
					sg_length, priv->rx_buf_size);
194 195 196 197 198 199
		}

		if (dpaa2_sg_is_final(sge))
			break;
	}

200 201
	WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");

202 203 204 205 206 207
	/* Count all data buffers + SG table buffer */
	ch->buf_count -= i + 2;

	return skb;
}

I
Ioana Ciocoi Radulescu 已提交
208 209 210
/* Free buffers acquired from the buffer pool or which were meant to
 * be released in the pool
 */
211 212
static void dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array,
				int count)
I
Ioana Ciocoi Radulescu 已提交
213 214 215 216 217 218 219
{
	struct device *dev = priv->net_dev->dev.parent;
	void *vaddr;
	int i;

	for (i = 0; i < count; i++) {
		vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
220
		dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
221 222
			       DMA_BIDIRECTIONAL);
		free_pages((unsigned long)vaddr, 0);
I
Ioana Ciocoi Radulescu 已提交
223 224 225
	}
}

226 227 228
static void dpaa2_eth_recycle_buf(struct dpaa2_eth_priv *priv,
				  struct dpaa2_eth_channel *ch,
				  dma_addr_t addr)
229
{
230
	int retries = 0;
231 232
	int err;

233 234
	ch->recycled_bufs[ch->recycled_bufs_cnt++] = addr;
	if (ch->recycled_bufs_cnt < DPAA2_ETH_BUFS_PER_CMD)
235 236 237
		return;

	while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
238 239
					       ch->recycled_bufs,
					       ch->recycled_bufs_cnt)) == -EBUSY) {
240 241
		if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
			break;
242
		cpu_relax();
243
	}
244 245

	if (err) {
246 247
		dpaa2_eth_free_bufs(priv, ch->recycled_bufs, ch->recycled_bufs_cnt);
		ch->buf_count -= ch->recycled_bufs_cnt;
248 249
	}

250
	ch->recycled_bufs_cnt = 0;
251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
			       struct dpaa2_eth_fq *fq,
			       struct dpaa2_eth_xdp_fds *xdp_fds)
{
	int total_enqueued = 0, retries = 0, enqueued;
	struct dpaa2_eth_drv_stats *percpu_extras;
	int num_fds, err, max_retries;
	struct dpaa2_fd *fds;

	percpu_extras = this_cpu_ptr(priv->percpu_extras);

	/* try to enqueue all the FDs until the max number of retries is hit */
	fds = xdp_fds->fds;
	num_fds = xdp_fds->num;
	max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
	while (total_enqueued < num_fds && retries < max_retries) {
		err = priv->enqueue(priv, fq, &fds[total_enqueued],
				    0, num_fds - total_enqueued, &enqueued);
		if (err == -EBUSY) {
			percpu_extras->tx_portal_busy += ++retries;
			continue;
		}
		total_enqueued += enqueued;
	}
	xdp_fds->num = 0;

	return total_enqueued;
}

282 283 284
static void dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv *priv,
				   struct dpaa2_eth_channel *ch,
				   struct dpaa2_eth_fq *fq)
I
Ioana Ciornei 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
{
	struct rtnl_link_stats64 *percpu_stats;
	struct dpaa2_fd *fds;
	int enqueued, i;

	percpu_stats = this_cpu_ptr(priv->percpu_stats);

	// enqueue the array of XDP_TX frames
	enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);

	/* update statistics */
	percpu_stats->tx_packets += enqueued;
	fds = fq->xdp_tx_fds.fds;
	for (i = 0; i < enqueued; i++) {
		percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
		ch->stats.xdp_tx++;
	}
	for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
303
		dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
I
Ioana Ciornei 已提交
304 305 306 307 308 309
		percpu_stats->tx_errors++;
		ch->stats.xdp_tx_err++;
	}
	fq->xdp_tx_fds.num = 0;
}

310 311 312 313
static void dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv *priv,
				  struct dpaa2_eth_channel *ch,
				  struct dpaa2_fd *fd,
				  void *buf_start, u16 queue_id)
314 315
{
	struct dpaa2_faead *faead;
I
Ioana Ciornei 已提交
316 317
	struct dpaa2_fd *dest_fd;
	struct dpaa2_eth_fq *fq;
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	u32 ctrl, frc;

	/* Mark the egress frame hardware annotation area as valid */
	frc = dpaa2_fd_get_frc(fd);
	dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
	dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);

	/* Instruct hardware to release the FD buffer directly into
	 * the buffer pool once transmission is completed, instead of
	 * sending a Tx confirmation frame to us
	 */
	ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
	faead = dpaa2_get_faead(buf_start, false);
	faead->ctrl = cpu_to_le32(ctrl);
	faead->conf_fqid = 0;

	fq = &priv->fq[queue_id];
I
Ioana Ciornei 已提交
335 336
	dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
	memcpy(dest_fd, fd, sizeof(*dest_fd));
337

I
Ioana Ciornei 已提交
338 339 340
	if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
		return;

341
	dpaa2_eth_xdp_tx_flush(priv, ch, fq);
342 343
}

344 345 346 347
static u32 dpaa2_eth_run_xdp(struct dpaa2_eth_priv *priv,
			     struct dpaa2_eth_channel *ch,
			     struct dpaa2_eth_fq *rx_fq,
			     struct dpaa2_fd *fd, void *vaddr)
348
{
349
	dma_addr_t addr = dpaa2_fd_get_addr(fd);
350 351 352
	struct bpf_prog *xdp_prog;
	struct xdp_buff xdp;
	u32 xdp_act = XDP_PASS;
353
	int err, offset;
354

355 356 357 358
	xdp_prog = READ_ONCE(ch->xdp.prog);
	if (!xdp_prog)
		goto out;

359 360 361 362
	offset = dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM;
	xdp_init_buff(&xdp, DPAA2_ETH_RX_BUF_RAW_SIZE - offset, &ch->xdp_rxq);
	xdp_prepare_buff(&xdp, vaddr + offset, XDP_PACKET_HEADROOM,
			 dpaa2_fd_get_len(fd), false);
363

364 365
	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);

366 367 368 369
	/* xdp.data pointer may have changed */
	dpaa2_fd_set_offset(fd, xdp.data - vaddr);
	dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);

370 371 372
	switch (xdp_act) {
	case XDP_PASS:
		break;
373
	case XDP_TX:
374
		dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
375
		break;
376
	default:
377
		bpf_warn_invalid_xdp_action(priv->net_dev, xdp_prog, xdp_act);
378
		fallthrough;
379 380
	case XDP_ABORTED:
		trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
381
		fallthrough;
382
	case XDP_DROP:
383
		dpaa2_eth_recycle_buf(priv, ch, addr);
384
		ch->stats.xdp_drop++;
385
		break;
386 387
	case XDP_REDIRECT:
		dma_unmap_page(priv->net_dev->dev.parent, addr,
388
			       priv->rx_buf_size, DMA_BIDIRECTIONAL);
389
		ch->buf_count--;
390 391

		/* Allow redirect use of full headroom */
392
		xdp.data_hard_start = vaddr;
393 394
		xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;

395
		err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
396 397 398 399 400 401 402 403
		if (unlikely(err)) {
			addr = dma_map_page(priv->net_dev->dev.parent,
					    virt_to_page(vaddr), 0,
					    priv->rx_buf_size, DMA_BIDIRECTIONAL);
			if (unlikely(dma_mapping_error(priv->net_dev->dev.parent, addr))) {
				free_pages((unsigned long)vaddr, 0);
			} else {
				ch->buf_count++;
404
				dpaa2_eth_recycle_buf(priv, ch, addr);
405
			}
406
			ch->stats.xdp_drop++;
407
		} else {
408
			ch->stats.xdp_redirect++;
409
		}
410
		break;
411 412
	}

413
	ch->xdp.res |= xdp_act;
414 415 416 417
out:
	return xdp_act;
}

418 419 420 421 422
static struct sk_buff *dpaa2_eth_copybreak(struct dpaa2_eth_channel *ch,
					   const struct dpaa2_fd *fd,
					   void *fd_vaddr)
{
	u16 fd_offset = dpaa2_fd_get_offset(fd);
423
	struct dpaa2_eth_priv *priv = ch->priv;
424 425 426 427
	u32 fd_length = dpaa2_fd_get_len(fd);
	struct sk_buff *skb = NULL;
	unsigned int skb_len;

428
	if (fd_length > priv->rx_copybreak)
429 430 431 432 433 434 435 436 437 438 439 440 441
		return NULL;

	skb_len = fd_length + dpaa2_eth_needed_headroom(NULL);

	skb = napi_alloc_skb(&ch->napi, skb_len);
	if (!skb)
		return NULL;

	skb_reserve(skb, dpaa2_eth_needed_headroom(NULL));
	skb_put(skb, fd_length);

	memcpy(skb->data, fd_vaddr + fd_offset, fd_length);

442
	dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(fd));
443 444 445 446

	return skb;
}

447 448 449 450
/* Main Rx frame processing routine */
static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
			 struct dpaa2_eth_channel *ch,
			 const struct dpaa2_fd *fd,
451
			 struct dpaa2_eth_fq *fq)
452 453 454 455 456 457
{
	dma_addr_t addr = dpaa2_fd_get_addr(fd);
	u8 fd_format = dpaa2_fd_get_format(fd);
	void *vaddr;
	struct sk_buff *skb;
	struct rtnl_link_stats64 *percpu_stats;
458
	struct dpaa2_eth_drv_stats *percpu_extras;
459 460
	struct device *dev = priv->net_dev->dev.parent;
	struct dpaa2_fas *fas;
461
	void *buf_data;
462
	u32 status = 0;
463
	u32 xdp_act;
464

465 466 467
	/* Tracing point */
	trace_dpaa2_rx_fd(priv->net_dev, fd);

468
	vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
469
	dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
470
				DMA_BIDIRECTIONAL);
471

472
	fas = dpaa2_get_fas(vaddr, false);
473 474 475
	prefetch(fas);
	buf_data = vaddr + dpaa2_fd_get_offset(fd);
	prefetch(buf_data);
476 477

	percpu_stats = this_cpu_ptr(priv->percpu_stats);
478
	percpu_extras = this_cpu_ptr(priv->percpu_extras);
479 480

	if (fd_format == dpaa2_fd_single) {
481
		xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
482 483 484 485 486 487
		if (xdp_act != XDP_PASS) {
			percpu_stats->rx_packets++;
			percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
			return;
		}

488 489 490 491 492 493
		skb = dpaa2_eth_copybreak(ch, fd, vaddr);
		if (!skb) {
			dma_unmap_page(dev, addr, priv->rx_buf_size,
				       DMA_BIDIRECTIONAL);
			skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
		}
494
	} else if (fd_format == dpaa2_fd_sg) {
495 496
		WARN_ON(priv->xdp_prog);

497
		dma_unmap_page(dev, addr, priv->rx_buf_size,
498
			       DMA_BIDIRECTIONAL);
499
		skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
500
		free_pages((unsigned long)vaddr, 0);
501 502
		percpu_extras->rx_sg_frames++;
		percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
503 504 505 506 507 508 509 510 511 512
	} else {
		/* We don't support any other format */
		goto err_frame_format;
	}

	if (unlikely(!skb))
		goto err_build_skb;

	prefetch(skb->data);

513 514 515 516 517 518 519 520 521 522 523 524
	/* Get the timestamp value */
	if (priv->rx_tstamp) {
		struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
		__le64 *ts = dpaa2_get_ts(vaddr, false);
		u64 ns;

		memset(shhwtstamps, 0, sizeof(*shhwtstamps));

		ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
		shhwtstamps->hwtstamp = ns_to_ktime(ns);
	}

525 526 527
	/* Check if we need to validate the L4 csum */
	if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
		status = le32_to_cpu(fas->status);
528
		dpaa2_eth_validate_rx_csum(priv, status, skb);
529 530 531
	}

	skb->protocol = eth_type_trans(skb, priv->net_dev);
532
	skb_record_rx_queue(skb, fq->flowid);
533 534 535

	percpu_stats->rx_packets++;
	percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
536
	ch->stats.bytes_per_cdan += dpaa2_fd_get_len(fd);
537

538
	list_add_tail(&skb->list, ch->rx_list);
539 540 541 542

	return;

err_build_skb:
543
	dpaa2_eth_free_rx_fd(priv, fd, vaddr);
544 545 546 547
err_frame_format:
	percpu_stats->rx_dropped++;
}

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
/* Processing of Rx frames received on the error FQ
 * We check and print the error bits and then free the frame
 */
static void dpaa2_eth_rx_err(struct dpaa2_eth_priv *priv,
			     struct dpaa2_eth_channel *ch,
			     const struct dpaa2_fd *fd,
			     struct dpaa2_eth_fq *fq __always_unused)
{
	struct device *dev = priv->net_dev->dev.parent;
	dma_addr_t addr = dpaa2_fd_get_addr(fd);
	u8 fd_format = dpaa2_fd_get_format(fd);
	struct rtnl_link_stats64 *percpu_stats;
	struct dpaa2_eth_trap_item *trap_item;
	struct dpaa2_fapr *fapr;
	struct sk_buff *skb;
	void *buf_data;
	void *vaddr;

	vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
	dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
				DMA_BIDIRECTIONAL);

	buf_data = vaddr + dpaa2_fd_get_offset(fd);

	if (fd_format == dpaa2_fd_single) {
		dma_unmap_page(dev, addr, priv->rx_buf_size,
			       DMA_BIDIRECTIONAL);
		skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
	} else if (fd_format == dpaa2_fd_sg) {
		dma_unmap_page(dev, addr, priv->rx_buf_size,
			       DMA_BIDIRECTIONAL);
		skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
		free_pages((unsigned long)vaddr, 0);
	} else {
		/* We don't support any other format */
		dpaa2_eth_free_rx_fd(priv, fd, vaddr);
		goto err_frame_format;
	}

	fapr = dpaa2_get_fapr(vaddr, false);
	trap_item = dpaa2_eth_dl_get_trap(priv, fapr);
	if (trap_item)
		devlink_trap_report(priv->devlink, skb, trap_item->trap_ctx,
				    &priv->devlink_port, NULL);
	consume_skb(skb);

err_frame_format:
	percpu_stats = this_cpu_ptr(priv->percpu_stats);
	percpu_stats->rx_errors++;
	ch->buf_count--;
}

600 601 602 603 604 605
/* Consume all frames pull-dequeued into the store. This is the simplest way to
 * make sure we don't accidentally issue another volatile dequeue which would
 * overwrite (leak) frames already in the store.
 *
 * Observance of NAPI budget is not our concern, leaving that to the caller.
 */
606 607
static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
				    struct dpaa2_eth_fq **src)
608 609
{
	struct dpaa2_eth_priv *priv = ch->priv;
610
	struct dpaa2_eth_fq *fq = NULL;
611 612
	struct dpaa2_dq *dq;
	const struct dpaa2_fd *fd;
613
	int cleaned = 0, retries = 0;
614 615 616 617 618 619 620 621 622 623
	int is_last;

	do {
		dq = dpaa2_io_store_next(ch->store, &is_last);
		if (unlikely(!dq)) {
			/* If we're here, we *must* have placed a
			 * volatile dequeue comnmand, so keep reading through
			 * the store until we get some sort of valid response
			 * token (either a valid frame or an "empty dequeue")
			 */
624 625 626 627 628
			if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
				netdev_err_once(priv->net_dev,
						"Unable to read a valid dequeue response\n");
				return -ETIMEDOUT;
			}
629 630 631 632
			continue;
		}

		fd = dpaa2_dq_fd(dq);
633
		fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
634

635
		fq->consume(priv, ch, fd, fq);
636
		cleaned++;
637
		retries = 0;
638 639
	} while (!is_last);

640 641 642 643
	if (!cleaned)
		return 0;

	fq->stats.frames += cleaned;
644
	ch->stats.frames += cleaned;
645
	ch->stats.frames_per_cdan += cleaned;
646 647

	/* A dequeue operation only pulls frames from a single queue
I
Ioana Ciocoi Radulescu 已提交
648
	 * into the store. Return the frame queue as an out param.
649
	 */
I
Ioana Ciocoi Radulescu 已提交
650 651
	if (src)
		*src = fq;
652

653 654 655
	return cleaned;
}

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
			       u8 *msgtype, u8 *twostep, u8 *udp,
			       u16 *correction_offset,
			       u16 *origintimestamp_offset)
{
	unsigned int ptp_class;
	struct ptp_header *hdr;
	unsigned int type;
	u8 *base;

	ptp_class = ptp_classify_raw(skb);
	if (ptp_class == PTP_CLASS_NONE)
		return -EINVAL;

	hdr = ptp_parse_header(skb, ptp_class);
	if (!hdr)
		return -EINVAL;

	*msgtype = ptp_get_msgtype(hdr, ptp_class);
	*twostep = hdr->flag_field[0] & 0x2;

	type = ptp_class & PTP_CLASS_PMASK;
	if (type == PTP_CLASS_IPV4 ||
	    type == PTP_CLASS_IPV6)
		*udp = 1;
	else
		*udp = 0;

	base = skb_mac_header(skb);
	*correction_offset = (u8 *)&hdr->correction - base;
	*origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;

	return 0;
}

691
/* Configure the egress frame annotation for timestamp update */
692 693 694 695
static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
				       struct dpaa2_fd *fd,
				       void *buf_start,
				       struct sk_buff *skb)
696
{
697 698 699
	struct ptp_tstamp origin_timestamp;
	struct dpni_single_step_cfg cfg;
	u8 msgtype, twostep, udp;
700
	struct dpaa2_faead *faead;
701 702 703
	struct dpaa2_fas *fas;
	struct timespec64 ts;
	u16 offset1, offset2;
704
	u32 ctrl, frc;
705 706
	__le64 *ns;
	u8 *data;
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721

	/* Mark the egress frame annotation area as valid */
	frc = dpaa2_fd_get_frc(fd);
	dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);

	/* Set hardware annotation size */
	ctrl = dpaa2_fd_get_ctrl(fd);
	dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);

	/* enable UPD (update prepanded data) bit in FAEAD field of
	 * hardware frame annotation area
	 */
	ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
	faead = dpaa2_get_faead(buf_start, true);
	faead->ctrl = cpu_to_le32(ctrl);
722 723 724 725

	if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
		if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
					&offset1, &offset2) ||
726
		    msgtype != PTP_MSGTYPE_SYNC || twostep) {
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
			WARN_ONCE(1, "Bad packet for one-step timestamping\n");
			return;
		}

		/* Mark the frame annotation status as valid */
		frc = dpaa2_fd_get_frc(fd);
		dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);

		/* Mark the PTP flag for one step timestamping */
		fas = dpaa2_get_fas(buf_start, true);
		fas->status = cpu_to_le32(DPAA2_FAS_PTP);

		dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
		ns = dpaa2_get_ts(buf_start, true);
		*ns = cpu_to_le64(timespec64_to_ns(&ts) /
				  DPAA2_PTP_CLK_PERIOD_NS);

		/* Update current time to PTP message originTimestamp field */
		ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
		data = skb_mac_header(skb);
		*(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
		*(__be32 *)(data + offset2 + 2) =
			htonl(origin_timestamp.sec_lsb);
		*(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);

		cfg.en = 1;
		cfg.ch_update = udp;
		cfg.offset = offset1;
		cfg.peer_delay = 0;

		if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token,
					     &cfg))
			WARN_ONCE(1, "Failed to set single step register");
	}
761 762
}

763 764 765 766 767 768 769
static void *dpaa2_eth_sgt_get(struct dpaa2_eth_priv *priv)
{
	struct dpaa2_eth_sgt_cache *sgt_cache;
	void *sgt_buf = NULL;
	int sgt_buf_size;

	sgt_cache = this_cpu_ptr(priv->sgt_cache);
770 771
	sgt_buf_size = priv->tx_data_offset +
		DPAA2_ETH_SG_ENTRIES_MAX * sizeof(struct dpaa2_sg_entry);
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795

	if (sgt_cache->count == 0)
		sgt_buf = napi_alloc_frag_align(sgt_buf_size, DPAA2_ETH_TX_BUF_ALIGN);
	else
		sgt_buf = sgt_cache->buf[--sgt_cache->count];
	if (!sgt_buf)
		return NULL;

	memset(sgt_buf, 0, sgt_buf_size);

	return sgt_buf;
}

static void dpaa2_eth_sgt_recycle(struct dpaa2_eth_priv *priv, void *sgt_buf)
{
	struct dpaa2_eth_sgt_cache *sgt_cache;

	sgt_cache = this_cpu_ptr(priv->sgt_cache);
	if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
		skb_free_frag(sgt_buf);
	else
		sgt_cache->buf[sgt_cache->count++] = sgt_buf;
}

796
/* Create a frame descriptor based on a fragmented skb */
797 798
static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
				 struct sk_buff *skb,
799 800
				 struct dpaa2_fd *fd,
				 void **swa_addr)
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
{
	struct device *dev = priv->net_dev->dev.parent;
	void *sgt_buf = NULL;
	dma_addr_t addr;
	int nr_frags = skb_shinfo(skb)->nr_frags;
	struct dpaa2_sg_entry *sgt;
	int i, err;
	int sgt_buf_size;
	struct scatterlist *scl, *crt_scl;
	int num_sg;
	int num_dma_bufs;
	struct dpaa2_eth_swa *swa;

	/* Create and map scatterlist.
	 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
	 * to go beyond nr_frags+1.
	 * Note: We don't support chained scatterlists
	 */
	if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
		return -EINVAL;

J
Julia Lawall 已提交
822
	scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
823 824 825 826 827
	if (unlikely(!scl))
		return -ENOMEM;

	sg_init_table(scl, nr_frags + 1);
	num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
828 829 830 831
	if (unlikely(num_sg < 0)) {
		err = -ENOMEM;
		goto dma_map_sg_failed;
	}
832
	num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
833 834 835 836 837 838 839
	if (unlikely(!num_dma_bufs)) {
		err = -ENOMEM;
		goto dma_map_sg_failed;
	}

	/* Prepare the HW SGT structure */
	sgt_buf_size = priv->tx_data_offset +
840
		       sizeof(struct dpaa2_sg_entry) *  num_dma_bufs;
841
	sgt_buf = dpaa2_eth_sgt_get(priv);
842 843 844 845
	if (unlikely(!sgt_buf)) {
		err = -ENOMEM;
		goto sgt_buf_alloc_failed;
	}
846

847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);

	/* Fill in the HW SGT structure.
	 *
	 * sgt_buf is zeroed out, so the following fields are implicit
	 * in all sgt entries:
	 *   - offset is 0
	 *   - format is 'dpaa2_sg_single'
	 */
	for_each_sg(scl, crt_scl, num_dma_bufs, i) {
		dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
		dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
	}
	dpaa2_sg_set_final(&sgt[i - 1], true);

	/* Store the skb backpointer in the SGT buffer.
	 * Fit the scatterlist and the number of buffers alongside the
	 * skb backpointer in the software annotation area. We'll need
	 * all of them on Tx Conf.
	 */
867
	*swa_addr = (void *)sgt_buf;
868
	swa = (struct dpaa2_eth_swa *)sgt_buf;
869 870 871 872 873
	swa->type = DPAA2_ETH_SWA_SG;
	swa->sg.skb = skb;
	swa->sg.scl = scl;
	swa->sg.num_sg = num_sg;
	swa->sg.sgt_size = sgt_buf_size;
874 875

	/* Separately map the SGT buffer */
876
	addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
877 878 879 880 881 882 883 884
	if (unlikely(dma_mapping_error(dev, addr))) {
		err = -ENOMEM;
		goto dma_map_single_failed;
	}
	dpaa2_fd_set_offset(fd, priv->tx_data_offset);
	dpaa2_fd_set_format(fd, dpaa2_fd_sg);
	dpaa2_fd_set_addr(fd, addr);
	dpaa2_fd_set_len(fd, skb->len);
885
	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
886 887 888 889

	return 0;

dma_map_single_failed:
890
	dpaa2_eth_sgt_recycle(priv, sgt_buf);
891
sgt_buf_alloc_failed:
892
	dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
893 894 895 896 897
dma_map_sg_failed:
	kfree(scl);
	return err;
}

898 899 900 901 902 903
/* Create a SG frame descriptor based on a linear skb.
 *
 * This function is used on the Tx path when the skb headroom is not large
 * enough for the HW requirements, thus instead of realloc-ing the skb we
 * create a SG frame descriptor with only one entry.
 */
904 905
static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
					    struct sk_buff *skb,
906 907
					    struct dpaa2_fd *fd,
					    void **swa_addr)
908 909 910 911 912 913 914 915 916 917 918
{
	struct device *dev = priv->net_dev->dev.parent;
	struct dpaa2_sg_entry *sgt;
	struct dpaa2_eth_swa *swa;
	dma_addr_t addr, sgt_addr;
	void *sgt_buf = NULL;
	int sgt_buf_size;
	int err;

	/* Prepare the HW SGT structure */
	sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
919
	sgt_buf = dpaa2_eth_sgt_get(priv);
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
	if (unlikely(!sgt_buf))
		return -ENOMEM;
	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);

	addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
	if (unlikely(dma_mapping_error(dev, addr))) {
		err = -ENOMEM;
		goto data_map_failed;
	}

	/* Fill in the HW SGT structure */
	dpaa2_sg_set_addr(sgt, addr);
	dpaa2_sg_set_len(sgt, skb->len);
	dpaa2_sg_set_final(sgt, true);

	/* Store the skb backpointer in the SGT buffer */
936
	*swa_addr = (void *)sgt_buf;
937 938 939
	swa = (struct dpaa2_eth_swa *)sgt_buf;
	swa->type = DPAA2_ETH_SWA_SINGLE;
	swa->single.skb = skb;
940
	swa->single.sgt_size = sgt_buf_size;
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

	/* Separately map the SGT buffer */
	sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
	if (unlikely(dma_mapping_error(dev, sgt_addr))) {
		err = -ENOMEM;
		goto sgt_map_failed;
	}

	dpaa2_fd_set_offset(fd, priv->tx_data_offset);
	dpaa2_fd_set_format(fd, dpaa2_fd_sg);
	dpaa2_fd_set_addr(fd, sgt_addr);
	dpaa2_fd_set_len(fd, skb->len);
	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);

	return 0;

sgt_map_failed:
	dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
data_map_failed:
960
	dpaa2_eth_sgt_recycle(priv, sgt_buf);
961 962 963 964

	return err;
}

965
/* Create a frame descriptor based on a linear skb */
966 967
static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
				     struct sk_buff *skb,
968 969
				     struct dpaa2_fd *fd,
				     void **swa_addr)
970 971
{
	struct device *dev = priv->net_dev->dev.parent;
972
	u8 *buffer_start, *aligned_start;
973
	struct dpaa2_eth_swa *swa;
974 975
	dma_addr_t addr;

976
	buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
977 978 979 980 981 982 983 984

	/* If there's enough room to align the FD address, do it.
	 * It will help hardware optimize accesses.
	 */
	aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
				  DPAA2_ETH_TX_BUF_ALIGN);
	if (aligned_start >= skb->head)
		buffer_start = aligned_start;
985 986 987 988 989

	/* Store a backpointer to the skb at the beginning of the buffer
	 * (in the private data area) such that we can release it
	 * on Tx confirm
	 */
990
	*swa_addr = (void *)buffer_start;
991 992 993
	swa = (struct dpaa2_eth_swa *)buffer_start;
	swa->type = DPAA2_ETH_SWA_SINGLE;
	swa->single.skb = skb;
994 995 996

	addr = dma_map_single(dev, buffer_start,
			      skb_tail_pointer(skb) - buffer_start,
997
			      DMA_BIDIRECTIONAL);
998 999 1000 1001 1002 1003 1004
	if (unlikely(dma_mapping_error(dev, addr)))
		return -ENOMEM;

	dpaa2_fd_set_addr(fd, addr);
	dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
	dpaa2_fd_set_len(fd, skb->len);
	dpaa2_fd_set_format(fd, dpaa2_fd_single);
1005
	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

	return 0;
}

/* FD freeing routine on the Tx path
 *
 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
 * back-pointed to is also freed.
 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
 * dpaa2_eth_tx().
 */
1017
static void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
1018 1019
				 struct dpaa2_eth_fq *fq,
				 const struct dpaa2_fd *fd, bool in_napi)
1020 1021
{
	struct device *dev = priv->net_dev->dev.parent;
1022
	dma_addr_t fd_addr, sg_addr;
1023
	struct sk_buff *skb = NULL;
1024 1025 1026
	unsigned char *buffer_start;
	struct dpaa2_eth_swa *swa;
	u8 fd_format = dpaa2_fd_get_format(fd);
1027
	u32 fd_len = dpaa2_fd_get_len(fd);
1028 1029
	struct dpaa2_sg_entry *sgt;

1030
	fd_addr = dpaa2_fd_get_addr(fd);
1031 1032
	buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
	swa = (struct dpaa2_eth_swa *)buffer_start;
1033 1034

	if (fd_format == dpaa2_fd_single) {
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
		if (swa->type == DPAA2_ETH_SWA_SINGLE) {
			skb = swa->single.skb;
			/* Accessing the skb buffer is safe before dma unmap,
			 * because we didn't map the actual skb shell.
			 */
			dma_unmap_single(dev, fd_addr,
					 skb_tail_pointer(skb) - buffer_start,
					 DMA_BIDIRECTIONAL);
		} else {
			WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
			dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
					 DMA_BIDIRECTIONAL);
		}
1048
	} else if (fd_format == dpaa2_fd_sg) {
1049 1050 1051 1052 1053 1054 1055
		if (swa->type == DPAA2_ETH_SWA_SG) {
			skb = swa->sg.skb;

			/* Unmap the scatterlist */
			dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
				     DMA_BIDIRECTIONAL);
			kfree(swa->sg.scl);
1056

1057 1058 1059 1060 1061
			/* Unmap the SGT buffer */
			dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
					 DMA_BIDIRECTIONAL);
		} else {
			skb = swa->single.skb;
1062

1063 1064 1065 1066 1067 1068 1069 1070 1071
			/* Unmap the SGT Buffer */
			dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
					 DMA_BIDIRECTIONAL);

			sgt = (struct dpaa2_sg_entry *)(buffer_start +
							priv->tx_data_offset);
			sg_addr = dpaa2_sg_get_addr(sgt);
			dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
		}
1072
	} else {
1073
		netdev_dbg(priv->net_dev, "Invalid FD format\n");
1074 1075 1076
		return;
	}

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
	if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
		fq->dq_frames++;
		fq->dq_bytes += fd_len;
	}

	if (swa->type == DPAA2_ETH_SWA_XDP) {
		xdp_return_frame(swa->xdp.xdpf);
		return;
	}

1087
	/* Get the timestamp value */
1088
	if (skb->cb[0] == TX_TSTAMP) {
1089
		struct skb_shared_hwtstamps shhwtstamps;
1090
		__le64 *ts = dpaa2_get_ts(buffer_start, true);
1091 1092 1093 1094 1095 1096 1097
		u64 ns;

		memset(&shhwtstamps, 0, sizeof(shhwtstamps));

		ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
		shhwtstamps.hwtstamp = ns_to_ktime(ns);
		skb_tstamp_tx(skb, &shhwtstamps);
1098 1099
	} else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
		mutex_unlock(&priv->onestep_tstamp_lock);
1100 1101
	}

1102
	/* Free SGT buffer allocated on tx */
1103 1104
	if (fd_format != dpaa2_fd_single)
		dpaa2_eth_sgt_recycle(priv, buffer_start);
1105 1106

	/* Move on with skb release */
1107
	napi_consume_skb(skb, in_napi);
1108 1109
}

1110 1111
static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
				  struct net_device *net_dev)
1112 1113
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1114
	struct dpaa2_eth_drv_stats *percpu_extras;
1115 1116
	struct rtnl_link_stats64 *percpu_stats;
	unsigned int needed_headroom;
1117
	struct dpaa2_eth_fq *fq;
I
Ioana Ciocoi Radulescu 已提交
1118
	struct netdev_queue *nq;
1119
	struct dpaa2_fd fd;
1120
	u16 queue_mapping;
I
Ioana Radulescu 已提交
1121
	u8 prio = 0;
1122
	int err, i;
1123
	u32 fd_len;
1124
	void *swa;
1125 1126

	percpu_stats = this_cpu_ptr(priv->percpu_stats);
1127
	percpu_extras = this_cpu_ptr(priv->percpu_extras);
1128

1129
	needed_headroom = dpaa2_eth_needed_headroom(skb);
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

	/* We'll be holding a back-reference to the skb until Tx Confirmation;
	 * we don't want that overwritten by a concurrent Tx with a cloned skb.
	 */
	skb = skb_unshare(skb, GFP_ATOMIC);
	if (unlikely(!skb)) {
		/* skb_unshare() has already freed the skb */
		percpu_stats->tx_dropped++;
		return NETDEV_TX_OK;
	}

	/* Setup the FD fields */
	memset(&fd, 0, sizeof(fd));

1144
	if (skb_is_nonlinear(skb)) {
1145
		err = dpaa2_eth_build_sg_fd(priv, skb, &fd, &swa);
1146 1147
		percpu_extras->tx_sg_frames++;
		percpu_extras->tx_sg_bytes += skb->len;
1148
	} else if (skb_headroom(skb) < needed_headroom) {
1149
		err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, &fd, &swa);
1150 1151
		percpu_extras->tx_sg_frames++;
		percpu_extras->tx_sg_bytes += skb->len;
1152 1153
		percpu_extras->tx_converted_sg_frames++;
		percpu_extras->tx_converted_sg_bytes += skb->len;
1154
	} else {
1155
		err = dpaa2_eth_build_single_fd(priv, skb, &fd, &swa);
1156 1157
	}

1158 1159 1160 1161 1162
	if (unlikely(err)) {
		percpu_stats->tx_dropped++;
		goto err_build_fd;
	}

1163 1164
	if (skb->cb[0])
		dpaa2_eth_enable_tx_tstamp(priv, &fd, swa, skb);
1165

1166 1167 1168
	/* Tracing point */
	trace_dpaa2_tx_fd(net_dev, &fd);

1169 1170 1171
	/* TxConf FQ selection relies on queue id from the stack.
	 * In case of a forwarded frame from another DPNI interface, we choose
	 * a queue affined to the same core that processed the Rx frame
1172
	 */
1173
	queue_mapping = skb_get_queue_mapping(skb);
I
Ioana Radulescu 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

	if (net_dev->num_tc) {
		prio = netdev_txq_to_tc(net_dev, queue_mapping);
		/* Hardware interprets priority level 0 as being the highest,
		 * so we need to do a reverse mapping to the netdev tc index
		 */
		prio = net_dev->num_tc - prio - 1;
		/* We have only one FQ array entry for all Tx hardware queues
		 * with the same flow id (but different priority levels)
		 */
		queue_mapping %= dpaa2_eth_queue_count(priv);
	}
1186
	fq = &priv->fq[queue_mapping];
1187 1188 1189 1190 1191 1192 1193 1194

	fd_len = dpaa2_fd_get_len(&fd);
	nq = netdev_get_tx_queue(net_dev, queue_mapping);
	netdev_tx_sent_queue(nq, fd_len);

	/* Everything that happens after this enqueues might race with
	 * the Tx confirmation callback for this frame
	 */
1195
	for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
1196
		err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
1197 1198 1199
		if (err != -EBUSY)
			break;
	}
1200
	percpu_extras->tx_portal_busy += i;
1201 1202 1203
	if (unlikely(err < 0)) {
		percpu_stats->tx_errors++;
		/* Clean up everything, including freeing the skb */
1204
		dpaa2_eth_free_tx_fd(priv, fq, &fd, false);
1205
		netdev_tx_completed_queue(nq, 1, fd_len);
1206 1207
	} else {
		percpu_stats->tx_packets++;
I
Ioana Ciocoi Radulescu 已提交
1208
		percpu_stats->tx_bytes += fd_len;
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
	}

	return NETDEV_TX_OK;

err_build_fd:
	dev_kfree_skb(skb);

	return NETDEV_TX_OK;
}

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
{
	struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
						   tx_onestep_tstamp);
	struct sk_buff *skb;

	while (true) {
		skb = skb_dequeue(&priv->tx_skbs);
		if (!skb)
			return;

		/* Lock just before TX one-step timestamping packet,
		 * and release the lock in dpaa2_eth_free_tx_fd when
		 * confirm the packet has been sent on hardware, or
		 * when clean up during transmit failure.
		 */
		mutex_lock(&priv->onestep_tstamp_lock);
		__dpaa2_eth_tx(skb, priv->net_dev);
	}
}

static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	u8 msgtype, twostep, udp;
	u16 offset1, offset2;

	/* Utilize skb->cb[0] for timestamping request per skb */
	skb->cb[0] = 0;

	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
		if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
			skb->cb[0] = TX_TSTAMP;
		else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
			skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
	}

	/* TX for one-step timestamping PTP Sync packet */
	if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
		if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
					 &offset1, &offset2))
1260
			if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) {
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
				skb_queue_tail(&priv->tx_skbs, skb);
				queue_work(priv->dpaa2_ptp_wq,
					   &priv->tx_onestep_tstamp);
				return NETDEV_TX_OK;
			}
		/* Use two-step timestamping if not one-step timestamping
		 * PTP Sync packet
		 */
		skb->cb[0] = TX_TSTAMP;
	}

	/* TX for other packets */
	return __dpaa2_eth_tx(skb, net_dev);
}

1276 1277
/* Tx confirmation frame processing routine */
static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
1278
			      struct dpaa2_eth_channel *ch,
1279
			      const struct dpaa2_fd *fd,
I
Ioana Ciocoi Radulescu 已提交
1280
			      struct dpaa2_eth_fq *fq)
1281 1282
{
	struct rtnl_link_stats64 *percpu_stats;
1283
	struct dpaa2_eth_drv_stats *percpu_extras;
I
Ioana Ciocoi Radulescu 已提交
1284
	u32 fd_len = dpaa2_fd_get_len(fd);
1285
	u32 fd_errors;
1286

1287 1288 1289
	/* Tracing point */
	trace_dpaa2_tx_conf_fd(priv->net_dev, fd);

1290 1291
	percpu_extras = this_cpu_ptr(priv->percpu_extras);
	percpu_extras->tx_conf_frames++;
I
Ioana Ciocoi Radulescu 已提交
1292
	percpu_extras->tx_conf_bytes += fd_len;
1293
	ch->stats.bytes_per_cdan += fd_len;
I
Ioana Ciocoi Radulescu 已提交
1294

1295 1296
	/* Check frame errors in the FD field */
	fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
1297
	dpaa2_eth_free_tx_fd(priv, fq, fd, true);
1298 1299 1300 1301

	if (likely(!fd_errors))
		return;

1302 1303 1304 1305
	if (net_ratelimit())
		netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
			   fd_errors);

1306 1307 1308
	percpu_stats = this_cpu_ptr(priv->percpu_stats);
	/* Tx-conf logically pertains to the egress path. */
	percpu_stats->tx_errors++;
1309 1310
}

1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
static int dpaa2_eth_set_rx_vlan_filtering(struct dpaa2_eth_priv *priv,
					   bool enable)
{
	int err;

	err = dpni_enable_vlan_filter(priv->mc_io, 0, priv->mc_token, enable);

	if (err) {
		netdev_err(priv->net_dev,
			   "dpni_enable_vlan_filter failed\n");
		return err;
	}

	return 0;
}

1327
static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
{
	int err;

	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
			       DPNI_OFF_RX_L3_CSUM, enable);
	if (err) {
		netdev_err(priv->net_dev,
			   "dpni_set_offload(RX_L3_CSUM) failed\n");
		return err;
	}

	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
			       DPNI_OFF_RX_L4_CSUM, enable);
	if (err) {
		netdev_err(priv->net_dev,
			   "dpni_set_offload(RX_L4_CSUM) failed\n");
		return err;
	}

	return 0;
}

1350
static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
{
	int err;

	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
			       DPNI_OFF_TX_L3_CSUM, enable);
	if (err) {
		netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
		return err;
	}

	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
			       DPNI_OFF_TX_L4_CSUM, enable);
	if (err) {
		netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
		return err;
	}

	return 0;
}

/* Perform a single release command to add buffers
 * to the specified buffer pool
 */
1374 1375
static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
			      struct dpaa2_eth_channel *ch, u16 bpid)
1376 1377 1378
{
	struct device *dev = priv->net_dev->dev.parent;
	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1379
	struct page *page;
1380
	dma_addr_t addr;
1381
	int retries = 0;
1382
	int i, err;
1383 1384 1385 1386 1387

	for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
		/* Allocate buffer visible to WRIOP + skb shared info +
		 * alignment padding
		 */
1388 1389 1390 1391 1392 1393
		/* allocate one page for each Rx buffer. WRIOP sees
		 * the entire page except for a tailroom reserved for
		 * skb shared info
		 */
		page = dev_alloc_pages(0);
		if (!page)
1394 1395
			goto err_alloc;

1396
		addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
1397
				    DMA_BIDIRECTIONAL);
1398 1399 1400 1401
		if (unlikely(dma_mapping_error(dev, addr)))
			goto err_map;

		buf_array[i] = addr;
1402 1403 1404

		/* tracing point */
		trace_dpaa2_eth_buf_seed(priv->net_dev,
1405
					 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
1406
					 addr, priv->rx_buf_size,
1407
					 bpid);
1408 1409 1410
	}

release_bufs:
1411
	/* In case the portal is busy, retry until successful */
1412
	while ((err = dpaa2_io_service_release(ch->dpio, bpid,
1413 1414 1415
					       buf_array, i)) == -EBUSY) {
		if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
			break;
1416
		cpu_relax();
1417
	}
1418 1419 1420 1421 1422

	/* If release command failed, clean up and bail out;
	 * not much else we can do about it
	 */
	if (err) {
1423
		dpaa2_eth_free_bufs(priv, buf_array, i);
1424 1425 1426
		return 0;
	}

1427 1428 1429
	return i;

err_map:
1430
	__free_pages(page, 0);
1431
err_alloc:
1432 1433 1434
	/* If we managed to allocate at least some buffers,
	 * release them to hardware
	 */
1435 1436 1437 1438 1439 1440
	if (i)
		goto release_bufs;

	return 0;
}

1441
static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
1442 1443 1444 1445 1446 1447 1448
{
	int i, j;
	int new_count;

	for (j = 0; j < priv->num_channels; j++) {
		for (i = 0; i < DPAA2_ETH_NUM_BUFS;
		     i += DPAA2_ETH_BUFS_PER_CMD) {
1449
			new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid);
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
			priv->channel[j]->buf_count += new_count;

			if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
				return -ENOMEM;
			}
		}
	}

	return 0;
}

1461
/*
1462 1463 1464
 * Drain the specified number of buffers from the DPNI's private buffer pool.
 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
 */
1465
static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count)
1466 1467
{
	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1468
	int retries = 0;
1469
	int ret;
1470 1471

	do {
1472
		ret = dpaa2_io_service_acquire(NULL, priv->bpid,
1473 1474
					       buf_array, count);
		if (ret < 0) {
1475
			if (ret == -EBUSY &&
1476
			    retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
1477
				continue;
1478 1479 1480
			netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
			return;
		}
1481
		dpaa2_eth_free_bufs(priv, buf_array, ret);
1482
		retries = 0;
1483 1484 1485
	} while (ret);
}

1486
static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv)
1487 1488 1489
{
	int i;

1490 1491
	dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
	dpaa2_eth_drain_bufs(priv, 1);
1492 1493 1494 1495 1496 1497 1498 1499

	for (i = 0; i < priv->num_channels; i++)
		priv->channel[i]->buf_count = 0;
}

/* Function is called from softirq context only, so we don't need to guard
 * the access to percpu count
 */
1500 1501 1502
static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
				 struct dpaa2_eth_channel *ch,
				 u16 bpid)
1503 1504 1505 1506 1507 1508 1509
{
	int new_count;

	if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
		return 0;

	do {
1510
		new_count = dpaa2_eth_add_bufs(priv, ch, bpid);
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
		if (unlikely(!new_count)) {
			/* Out of memory; abort for now, we'll try later on */
			break;
		}
		ch->buf_count += new_count;
	} while (ch->buf_count < DPAA2_ETH_NUM_BUFS);

	if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
		return -ENOMEM;

	return 0;
}

1524 1525 1526 1527 1528 1529
static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
{
	struct dpaa2_eth_sgt_cache *sgt_cache;
	u16 count;
	int k, i;

1530
	for_each_possible_cpu(k) {
1531 1532 1533 1534
		sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
		count = sgt_cache->count;

		for (i = 0; i < count; i++)
1535
			skb_free_frag(sgt_cache->buf[i]);
1536 1537 1538 1539
		sgt_cache->count = 0;
	}
}

1540
static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
1541 1542
{
	int err;
1543
	int dequeues = -1;
1544 1545 1546

	/* Retry while portal is busy */
	do {
1547 1548
		err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
						    ch->store);
1549
		dequeues++;
1550
		cpu_relax();
1551
	} while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
1552

1553 1554 1555 1556
	ch->stats.dequeue_portal_busy += dequeues;
	if (unlikely(err))
		ch->stats.pull_err++;

1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
	return err;
}

/* NAPI poll routine
 *
 * Frames are dequeued from the QMan channel associated with this NAPI context.
 * Rx, Tx confirmation and (if configured) Rx error frames all count
 * towards the NAPI budget.
 */
static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
{
	struct dpaa2_eth_channel *ch;
	struct dpaa2_eth_priv *priv;
1570
	int rx_cleaned = 0, txconf_cleaned = 0;
I
Ioana Ciocoi Radulescu 已提交
1571 1572 1573
	struct dpaa2_eth_fq *fq, *txc_fq = NULL;
	struct netdev_queue *nq;
	int store_cleaned, work_done;
1574
	struct list_head rx_list;
1575
	int retries = 0;
I
Ioana Ciornei 已提交
1576
	u16 flowid;
1577 1578 1579
	int err;

	ch = container_of(napi, struct dpaa2_eth_channel, napi);
1580
	ch->xdp.res = 0;
1581 1582
	priv = ch->priv;

1583 1584 1585
	INIT_LIST_HEAD(&rx_list);
	ch->rx_list = &rx_list;

1586
	do {
1587
		err = dpaa2_eth_pull_channel(ch);
1588 1589 1590 1591
		if (unlikely(err))
			break;

		/* Refill pool if appropriate */
1592
		dpaa2_eth_refill_pool(priv, ch, priv->bpid);
1593

1594
		store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
1595
		if (store_cleaned <= 0)
I
Ioana Ciocoi Radulescu 已提交
1596 1597
			break;
		if (fq->type == DPAA2_RX_FQ) {
1598
			rx_cleaned += store_cleaned;
I
Ioana Ciornei 已提交
1599
			flowid = fq->flowid;
I
Ioana Ciocoi Radulescu 已提交
1600
		} else {
1601
			txconf_cleaned += store_cleaned;
I
Ioana Ciocoi Radulescu 已提交
1602 1603 1604
			/* We have a single Tx conf FQ on this channel */
			txc_fq = fq;
		}
1605

1606 1607
		/* If we either consumed the whole NAPI budget with Rx frames
		 * or we reached the Tx confirmations threshold, we're done.
1608
		 */
1609
		if (rx_cleaned >= budget ||
I
Ioana Ciocoi Radulescu 已提交
1610 1611 1612 1613
		    txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
			work_done = budget;
			goto out;
		}
1614
	} while (store_cleaned);
1615

1616 1617 1618 1619 1620 1621
	/* Update NET DIM with the values for this CDAN */
	dpaa2_io_update_net_dim(ch->dpio, ch->stats.frames_per_cdan,
				ch->stats.bytes_per_cdan);
	ch->stats.frames_per_cdan = 0;
	ch->stats.bytes_per_cdan = 0;

1622 1623 1624 1625 1626 1627 1628
	/* We didn't consume the entire budget, so finish napi and
	 * re-enable data availability notifications
	 */
	napi_complete_done(napi, rx_cleaned);
	do {
		err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
		cpu_relax();
1629
	} while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
1630 1631
	WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
		  ch->nctx.desired_cpu);
1632

I
Ioana Ciocoi Radulescu 已提交
1633 1634 1635
	work_done = max(rx_cleaned, 1);

out:
1636 1637
	netif_receive_skb_list(ch->rx_list);

1638
	if (txc_fq && txc_fq->dq_frames) {
I
Ioana Ciocoi Radulescu 已提交
1639 1640 1641 1642 1643 1644 1645
		nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
		netdev_tx_completed_queue(nq, txc_fq->dq_frames,
					  txc_fq->dq_bytes);
		txc_fq->dq_frames = 0;
		txc_fq->dq_bytes = 0;
	}

1646 1647
	if (ch->xdp.res & XDP_REDIRECT)
		xdp_do_flush_map();
I
Ioana Ciornei 已提交
1648
	else if (rx_cleaned && ch->xdp.res & XDP_TX)
1649
		dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
1650

I
Ioana Ciocoi Radulescu 已提交
1651
	return work_done;
1652 1653
}

1654
static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
{
	struct dpaa2_eth_channel *ch;
	int i;

	for (i = 0; i < priv->num_channels; i++) {
		ch = priv->channel[i];
		napi_enable(&ch->napi);
	}
}

1665
static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
{
	struct dpaa2_eth_channel *ch;
	int i;

	for (i = 0; i < priv->num_channels; i++) {
		ch = priv->channel[i];
		napi_disable(&ch->napi);
	}
}

1676 1677
void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
			       bool tx_pause, bool pfc)
1678 1679
{
	struct dpni_taildrop td = {0};
1680
	struct dpaa2_eth_fq *fq;
1681 1682
	int i, err;

1683 1684 1685 1686 1687
	/* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
	 * flow control is disabled (as it might interfere with either the
	 * buffer pool depletion trigger for pause frames or with the group
	 * congestion trigger for PFC frames)
	 */
1688
	td.enable = !tx_pause;
1689 1690
	if (priv->rx_fqtd_enabled == td.enable)
		goto set_cgtd;
1691

1692 1693
	td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
	td.units = DPNI_CONGESTION_UNIT_BYTES;
1694 1695

	for (i = 0; i < priv->num_fqs; i++) {
1696 1697
		fq = &priv->fq[i];
		if (fq->type != DPAA2_RX_FQ)
1698 1699
			continue;
		err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1700 1701
					DPNI_CP_QUEUE, DPNI_QUEUE_RX,
					fq->tc, fq->flowid, &td);
1702 1703
		if (err) {
			netdev_err(priv->net_dev,
1704 1705 1706 1707 1708
				   "dpni_set_taildrop(FQ) failed\n");
			return;
		}
	}

1709 1710 1711
	priv->rx_fqtd_enabled = td.enable;

set_cgtd:
1712 1713
	/* Congestion group taildrop: threshold is in frames, per group
	 * of FQs belonging to the same traffic class
1714 1715 1716 1717
	 * Enabled if general Tx pause disabled or if PFCs are enabled
	 * (congestion group threhsold for PFC generation is lower than the
	 * CG taildrop threshold, so it won't interfere with it; we also
	 * want frames in non-PFC enabled traffic classes to be kept in check)
1718
	 */
1719
	td.enable = !tx_pause || pfc;
1720 1721 1722
	if (priv->rx_cgtd_enabled == td.enable)
		return;

1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
	td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
	td.units = DPNI_CONGESTION_UNIT_FRAMES;
	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
		err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
					DPNI_CP_GROUP, DPNI_QUEUE_RX,
					i, 0, &td);
		if (err) {
			netdev_err(priv->net_dev,
				   "dpni_set_taildrop(CG) failed\n");
			return;
1733 1734 1735
		}
	}

1736
	priv->rx_cgtd_enabled = td.enable;
1737 1738
}

1739
static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
1740
{
1741
	struct dpni_link_state state = {0};
1742
	bool tx_pause;
1743 1744 1745 1746 1747 1748 1749 1750 1751
	int err;

	err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
	if (unlikely(err)) {
		netdev_err(priv->net_dev,
			   "dpni_get_link_state() failed\n");
		return err;
	}

1752 1753 1754 1755
	/* If Tx pause frame settings have changed, we need to update
	 * Rx FQ taildrop configuration as well. We configure taildrop
	 * only when pause frame generation is disabled.
	 */
1756
	tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
1757
	dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
1758

1759 1760 1761
	/* When we manage the MAC/PHY using phylink there is no need
	 * to manually update the netif_carrier.
	 */
1762
	if (dpaa2_eth_is_type_phy(priv))
1763 1764
		goto out;

1765 1766
	/* Chech link state; speed / duplex changes are not treated yet */
	if (priv->link_state.up == state.up)
1767
		goto out;
1768 1769 1770 1771 1772 1773 1774 1775 1776

	if (state.up) {
		netif_carrier_on(priv->net_dev);
		netif_tx_start_all_queues(priv->net_dev);
	} else {
		netif_tx_stop_all_queues(priv->net_dev);
		netif_carrier_off(priv->net_dev);
	}

1777
	netdev_info(priv->net_dev, "Link Event: state %s\n",
1778 1779
		    state.up ? "up" : "down");

1780 1781 1782
out:
	priv->link_state = state;

1783 1784 1785 1786 1787 1788 1789 1790
	return 0;
}

static int dpaa2_eth_open(struct net_device *net_dev)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	int err;

1791
	err = dpaa2_eth_seed_pool(priv, priv->bpid);
1792 1793 1794 1795 1796 1797
	if (err) {
		/* Not much to do; the buffer pool, though not filled up,
		 * may still contain some buffers which would enable us
		 * to limp on.
		 */
		netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
1798
			   priv->dpbp_dev->obj_desc.id, priv->bpid);
1799 1800
	}

1801
	if (!dpaa2_eth_is_type_phy(priv)) {
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
		/* We'll only start the txqs when the link is actually ready;
		 * make sure we don't race against the link up notification,
		 * which may come immediately after dpni_enable();
		 */
		netif_tx_stop_all_queues(net_dev);

		/* Also, explicitly set carrier off, otherwise
		 * netif_carrier_ok() will return true and cause 'ip link show'
		 * to report the LOWER_UP flag, even though the link
		 * notification wasn't even received.
		 */
		netif_carrier_off(net_dev);
	}
1815
	dpaa2_eth_enable_ch_napi(priv);
1816 1817 1818 1819 1820 1821 1822

	err = dpni_enable(priv->mc_io, 0, priv->mc_token);
	if (err < 0) {
		netdev_err(net_dev, "dpni_enable() failed\n");
		goto enable_err;
	}

1823
	if (dpaa2_eth_is_type_phy(priv))
1824
		phylink_start(priv->mac->phylink);
1825 1826 1827 1828

	return 0;

enable_err:
1829 1830
	dpaa2_eth_disable_ch_napi(priv);
	dpaa2_eth_drain_pool(priv);
1831 1832 1833
	return err;
}

1834
/* Total number of in-flight frames on ingress queues */
1835
static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
1836
{
1837 1838 1839
	struct dpaa2_eth_fq *fq;
	u32 fcnt = 0, bcnt = 0, total = 0;
	int i, err;
1840

1841 1842 1843 1844 1845 1846 1847 1848 1849
	for (i = 0; i < priv->num_fqs; i++) {
		fq = &priv->fq[i];
		err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
		if (err) {
			netdev_warn(priv->net_dev, "query_fq_count failed");
			break;
		}
		total += fcnt;
	}
1850 1851 1852 1853

	return total;
}

1854
static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
1855
{
1856 1857
	int retries = 10;
	u32 pending;
1858

1859
	do {
1860
		pending = dpaa2_eth_ingress_fq_count(priv);
1861 1862 1863
		if (pending)
			msleep(100);
	} while (pending && --retries);
1864 1865
}

1866 1867
#define DPNI_TX_PENDING_VER_MAJOR	7
#define DPNI_TX_PENDING_VER_MINOR	13
1868
static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
{
	union dpni_statistics stats;
	int retries = 10;
	int err;

	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
				   DPNI_TX_PENDING_VER_MINOR) < 0)
		goto out;

	do {
		err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
					  &stats);
		if (err)
			goto out;
		if (stats.page_6.tx_pending_frames == 0)
			return;
	} while (--retries);

out:
	msleep(500);
}

1891 1892 1893
static int dpaa2_eth_stop(struct net_device *net_dev)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1894
	int dpni_enabled = 0;
1895 1896
	int retries = 10;

1897 1898 1899
	if (dpaa2_eth_is_type_phy(priv)) {
		phylink_stop(priv->mac->phylink);
	} else {
1900 1901 1902
		netif_tx_stop_all_queues(net_dev);
		netif_carrier_off(net_dev);
	}
1903

1904 1905 1906 1907 1908 1909 1910 1911 1912
	/* On dpni_disable(), the MC firmware will:
	 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
	 * - cut off WRIOP dequeues from egress FQs and wait until transmission
	 * of all in flight Tx frames is finished (and corresponding Tx conf
	 * frames are enqueued back to software)
	 *
	 * Before calling dpni_disable(), we wait for all Tx frames to arrive
	 * on WRIOP. After it finishes, wait until all remaining frames on Rx
	 * and Tx conf queues are consumed on NAPI poll.
1913
	 */
1914
	dpaa2_eth_wait_for_egress_fq_empty(priv);
1915

1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929
	do {
		dpni_disable(priv->mc_io, 0, priv->mc_token);
		dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
		if (dpni_enabled)
			/* Allow the hardware some slack */
			msleep(100);
	} while (dpni_enabled && --retries);
	if (!retries) {
		netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
		/* Must go on and disable NAPI nonetheless, so we don't crash at
		 * the next "ifconfig up"
		 */
	}

1930 1931
	dpaa2_eth_wait_for_ingress_fq_empty(priv);
	dpaa2_eth_disable_ch_napi(priv);
1932 1933

	/* Empty the buffer pool */
1934
	dpaa2_eth_drain_pool(priv);
1935

1936 1937 1938
	/* Empty the Scatter-Gather Buffer cache */
	dpaa2_eth_sgt_cache_drain(priv);

1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
	return 0;
}

static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	struct device *dev = net_dev->dev.parent;
	int err;

	err = eth_mac_addr(net_dev, addr);
	if (err < 0) {
		dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
		return err;
	}

	err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
					net_dev->dev_addr);
	if (err) {
		dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
		return err;
	}

	return 0;
}

/** Fill in counters maintained by the GPP driver. These may be different from
 * the hardware counters obtained by ethtool.
 */
1967 1968
static void dpaa2_eth_get_stats(struct net_device *net_dev,
				struct rtnl_link_stats64 *stats)
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	struct rtnl_link_stats64 *percpu_stats;
	u64 *cpustats;
	u64 *netstats = (u64 *)stats;
	int i, j;
	int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);

	for_each_possible_cpu(i) {
		percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
		cpustats = (u64 *)percpu_stats;
		for (j = 0; j < num; j++)
			netstats[j] += cpustats[j];
	}
}

/* Copy mac unicast addresses from @net_dev to @priv.
 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
 */
1988 1989
static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
				     struct dpaa2_eth_priv *priv)
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
{
	struct netdev_hw_addr *ha;
	int err;

	netdev_for_each_uc_addr(ha, net_dev) {
		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
					ha->addr);
		if (err)
			netdev_warn(priv->net_dev,
				    "Could not add ucast MAC %pM to the filtering table (err %d)\n",
				    ha->addr, err);
	}
}

/* Copy mac multicast addresses from @net_dev to @priv
 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
 */
2007 2008
static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
				     struct dpaa2_eth_priv *priv)
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
{
	struct netdev_hw_addr *ha;
	int err;

	netdev_for_each_mc_addr(ha, net_dev) {
		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
					ha->addr);
		if (err)
			netdev_warn(priv->net_dev,
				    "Could not add mcast MAC %pM to the filtering table (err %d)\n",
				    ha->addr, err);
	}
}

2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
static int dpaa2_eth_rx_add_vid(struct net_device *net_dev,
				__be16 vlan_proto, u16 vid)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	int err;

	err = dpni_add_vlan_id(priv->mc_io, 0, priv->mc_token,
			       vid, 0, 0, 0);

	if (err) {
		netdev_warn(priv->net_dev,
			    "Could not add the vlan id %u\n",
			    vid);
		return err;
	}

	return 0;
}

static int dpaa2_eth_rx_kill_vid(struct net_device *net_dev,
				 __be16 vlan_proto, u16 vid)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	int err;

	err = dpni_remove_vlan_id(priv->mc_io, 0, priv->mc_token, vid);

	if (err) {
		netdev_warn(priv->net_dev,
			    "Could not remove the vlan id %u\n",
			    vid);
		return err;
	}

	return 0;
}

2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	int uc_count = netdev_uc_count(net_dev);
	int mc_count = netdev_mc_count(net_dev);
	u8 max_mac = priv->dpni_attrs.mac_filter_entries;
	u32 options = priv->dpni_attrs.options;
	u16 mc_token = priv->mc_token;
	struct fsl_mc_io *mc_io = priv->mc_io;
	int err;

	/* Basic sanity checks; these probably indicate a misconfiguration */
	if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
		netdev_info(net_dev,
			    "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
			    max_mac);

	/* Force promiscuous if the uc or mc counts exceed our capabilities. */
	if (uc_count > max_mac) {
		netdev_info(net_dev,
			    "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
			    uc_count, max_mac);
		goto force_promisc;
	}
	if (mc_count + uc_count > max_mac) {
		netdev_info(net_dev,
			    "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
			    uc_count + mc_count, max_mac);
		goto force_mc_promisc;
	}

	/* Adjust promisc settings due to flag combinations */
	if (net_dev->flags & IFF_PROMISC)
		goto force_promisc;
	if (net_dev->flags & IFF_ALLMULTI) {
		/* First, rebuild unicast filtering table. This should be done
		 * in promisc mode, in order to avoid frame loss while we
		 * progressively add entries to the table.
		 * We don't know whether we had been in promisc already, and
		 * making an MC call to find out is expensive; so set uc promisc
		 * nonetheless.
		 */
		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
		if (err)
			netdev_warn(net_dev, "Can't set uc promisc\n");

		/* Actual uc table reconstruction. */
		err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
		if (err)
			netdev_warn(net_dev, "Can't clear uc filters\n");
2110
		dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

		/* Finally, clear uc promisc and set mc promisc as requested. */
		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
		if (err)
			netdev_warn(net_dev, "Can't clear uc promisc\n");
		goto force_mc_promisc;
	}

	/* Neither unicast, nor multicast promisc will be on... eventually.
	 * For now, rebuild mac filtering tables while forcing both of them on.
	 */
	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
	if (err)
		netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
	if (err)
		netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);

	/* Actual mac filtering tables reconstruction */
	err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
	if (err)
		netdev_warn(net_dev, "Can't clear mac filters\n");
2133 2134
	dpaa2_eth_add_mc_hw_addr(net_dev, priv);
	dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165

	/* Now we can clear both ucast and mcast promisc, without risking
	 * to drop legitimate frames anymore.
	 */
	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
	if (err)
		netdev_warn(net_dev, "Can't clear ucast promisc\n");
	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
	if (err)
		netdev_warn(net_dev, "Can't clear mcast promisc\n");

	return;

force_promisc:
	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
	if (err)
		netdev_warn(net_dev, "Can't set ucast promisc\n");
force_mc_promisc:
	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
	if (err)
		netdev_warn(net_dev, "Can't set mcast promisc\n");
}

static int dpaa2_eth_set_features(struct net_device *net_dev,
				  netdev_features_t features)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	netdev_features_t changed = features ^ net_dev->features;
	bool enable;
	int err;

2166 2167 2168 2169 2170 2171 2172
	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
		enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER);
		err = dpaa2_eth_set_rx_vlan_filtering(priv, enable);
		if (err)
			return err;
	}

2173 2174
	if (changed & NETIF_F_RXCSUM) {
		enable = !!(features & NETIF_F_RXCSUM);
2175
		err = dpaa2_eth_set_rx_csum(priv, enable);
2176 2177 2178 2179 2180 2181
		if (err)
			return err;
	}

	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
		enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
2182
		err = dpaa2_eth_set_tx_csum(priv, enable);
2183 2184 2185 2186 2187 2188 2189
		if (err)
			return err;
	}

	return 0;
}

2190 2191 2192 2193 2194
static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	struct dpaa2_eth_priv *priv = netdev_priv(dev);
	struct hwtstamp_config config;

2195 2196 2197
	if (!dpaa2_ptp)
		return -EINVAL;

2198 2199 2200 2201 2202 2203
	if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
		return -EFAULT;

	switch (config.tx_type) {
	case HWTSTAMP_TX_OFF:
	case HWTSTAMP_TX_ON:
2204
	case HWTSTAMP_TX_ONESTEP_SYNC:
2205
		priv->tx_tstamp_type = config.tx_type;
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
		break;
	default:
		return -ERANGE;
	}

	if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
		priv->rx_tstamp = false;
	} else {
		priv->rx_tstamp = true;
		/* TS is set for all frame types, not only those requested */
		config.rx_filter = HWTSTAMP_FILTER_ALL;
	}

	return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
			-EFAULT : 0;
}

static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
2225 2226
	struct dpaa2_eth_priv *priv = netdev_priv(dev);

2227 2228 2229
	if (cmd == SIOCSHWTSTAMP)
		return dpaa2_eth_ts_ioctl(dev, rq, cmd);

2230
	if (dpaa2_eth_is_type_phy(priv))
2231 2232 2233
		return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);

	return -EOPNOTSUPP;
2234 2235
}

2236 2237 2238 2239 2240
static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
{
	int mfl, linear_mfl;

	mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2241
	linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
2242
		     dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
2243 2244 2245 2246 2247 2248 2249 2250 2251 2252

	if (mfl > linear_mfl) {
		netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
			    linear_mfl - VLAN_ETH_HLEN);
		return false;
	}

	return true;
}

2253
static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
{
	int mfl, err;

	/* We enforce a maximum Rx frame length based on MTU only if we have
	 * an XDP program attached (in order to avoid Rx S/G frames).
	 * Otherwise, we accept all incoming frames as long as they are not
	 * larger than maximum size supported in hardware
	 */
	if (has_xdp)
		mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
	else
		mfl = DPAA2_ETH_MFL;

	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
	if (err) {
		netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
		return err;
	}

	return 0;
}

static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
{
	struct dpaa2_eth_priv *priv = netdev_priv(dev);
	int err;

	if (!priv->xdp_prog)
		goto out;

	if (!xdp_mtu_valid(priv, new_mtu))
		return -EINVAL;

2287
	err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
2288 2289 2290 2291 2292 2293 2294 2295
	if (err)
		return err;

out:
	dev->mtu = new_mtu;
	return 0;
}

2296
static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
{
	struct dpni_buffer_layout buf_layout = {0};
	int err;

	err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
				     DPNI_QUEUE_RX, &buf_layout);
	if (err) {
		netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
		return err;
	}

	/* Reserve extra headroom for XDP header size changes */
	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
				    (has_xdp ? XDP_PACKET_HEADROOM : 0);
	buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
				     DPNI_QUEUE_RX, &buf_layout);
	if (err) {
		netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
		return err;
	}

	return 0;
}

2322
static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
{
	struct dpaa2_eth_priv *priv = netdev_priv(dev);
	struct dpaa2_eth_channel *ch;
	struct bpf_prog *old;
	bool up, need_update;
	int i, err;

	if (prog && !xdp_mtu_valid(priv, dev->mtu))
		return -EINVAL;

2333 2334
	if (prog)
		bpf_prog_add(prog, priv->num_channels);
2335 2336 2337 2338 2339 2340 2341

	up = netif_running(dev);
	need_update = (!!priv->xdp_prog != !!prog);

	if (up)
		dpaa2_eth_stop(dev);

2342 2343 2344 2345 2346
	/* While in xdp mode, enforce a maximum Rx frame size based on MTU.
	 * Also, when switching between xdp/non-xdp modes we need to reconfigure
	 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
	 * so we are sure no old format buffers will be used from now on.
	 */
2347
	if (need_update) {
2348
		err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
2349 2350
		if (err)
			goto out_err;
2351
		err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
2352 2353
		if (err)
			goto out_err;
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
	}

	old = xchg(&priv->xdp_prog, prog);
	if (old)
		bpf_prog_put(old);

	for (i = 0; i < priv->num_channels; i++) {
		ch = priv->channel[i];
		old = xchg(&ch->xdp.prog, prog);
		if (old)
			bpf_prog_put(old);
	}

	if (up) {
		err = dpaa2_eth_open(dev);
		if (err)
			return err;
	}

	return 0;

out_err:
	if (prog)
		bpf_prog_sub(prog, priv->num_channels);
	if (up)
		dpaa2_eth_open(dev);

	return err;
}

static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
{
	switch (xdp->command) {
	case XDP_SETUP_PROG:
2388
		return dpaa2_eth_setup_xdp(dev, xdp->prog);
2389 2390 2391 2392 2393 2394 2395
	default:
		return -EINVAL;
	}

	return 0;
}

2396 2397 2398
static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
				   struct xdp_frame *xdpf,
				   struct dpaa2_fd *fd)
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
{
	struct device *dev = net_dev->dev.parent;
	unsigned int needed_headroom;
	struct dpaa2_eth_swa *swa;
	void *buffer_start, *aligned_start;
	dma_addr_t addr;

	/* We require a minimum headroom to be able to transmit the frame.
	 * Otherwise return an error and let the original net_device handle it
	 */
2409
	needed_headroom = dpaa2_eth_needed_headroom(NULL);
2410 2411 2412 2413
	if (xdpf->headroom < needed_headroom)
		return -EINVAL;

	/* Setup the FD fields */
2414
	memset(fd, 0, sizeof(*fd));
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431

	/* Align FD address, if possible */
	buffer_start = xdpf->data - needed_headroom;
	aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
				  DPAA2_ETH_TX_BUF_ALIGN);
	if (aligned_start >= xdpf->data - xdpf->headroom)
		buffer_start = aligned_start;

	swa = (struct dpaa2_eth_swa *)buffer_start;
	/* fill in necessary fields here */
	swa->type = DPAA2_ETH_SWA_XDP;
	swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
	swa->xdp.xdpf = xdpf;

	addr = dma_map_single(dev, buffer_start,
			      swa->xdp.dma_size,
			      DMA_BIDIRECTIONAL);
2432
	if (unlikely(dma_mapping_error(dev, addr)))
2433 2434
		return -ENOMEM;

2435 2436 2437 2438 2439
	dpaa2_fd_set_addr(fd, addr);
	dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
	dpaa2_fd_set_len(fd, xdpf->len);
	dpaa2_fd_set_format(fd, dpaa2_fd_single);
	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
2440 2441 2442 2443 2444 2445 2446

	return 0;
}

static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
			      struct xdp_frame **frames, u32 flags)
{
2447
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2448
	struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
2449 2450
	struct rtnl_link_stats64 *percpu_stats;
	struct dpaa2_eth_fq *fq;
2451
	struct dpaa2_fd *fds;
2452
	int enqueued, i, err;
2453 2454 2455 2456 2457 2458 2459

	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
		return -EINVAL;

	if (!netif_running(net_dev))
		return -ENETDOWN;

2460
	fq = &priv->fq[smp_processor_id()];
2461 2462
	xdp_redirect_fds = &fq->xdp_redirect_fds;
	fds = xdp_redirect_fds->fds;
2463

2464 2465
	percpu_stats = this_cpu_ptr(priv->percpu_stats);

2466
	/* create a FD for each xdp_frame in the list received */
2467
	for (i = 0; i < n; i++) {
2468 2469 2470 2471
		err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
		if (err)
			break;
	}
2472
	xdp_redirect_fds->num = i;
2473

2474 2475
	/* enqueue all the frame descriptors */
	enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
2476

2477
	/* update statistics */
2478 2479
	percpu_stats->tx_packets += enqueued;
	for (i = 0; i < enqueued; i++)
2480 2481
		percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);

2482
	return enqueued;
2483 2484
}

I
Ioana Radulescu 已提交
2485 2486 2487 2488 2489
static int update_xps(struct dpaa2_eth_priv *priv)
{
	struct net_device *net_dev = priv->net_dev;
	struct cpumask xps_mask;
	struct dpaa2_eth_fq *fq;
I
Ioana Radulescu 已提交
2490
	int i, num_queues, netdev_queues;
I
Ioana Radulescu 已提交
2491 2492 2493
	int err = 0;

	num_queues = dpaa2_eth_queue_count(priv);
I
Ioana Radulescu 已提交
2494
	netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
I
Ioana Radulescu 已提交
2495 2496 2497 2498

	/* The first <num_queues> entries in priv->fq array are Tx/Tx conf
	 * queues, so only process those
	 */
I
Ioana Radulescu 已提交
2499 2500
	for (i = 0; i < netdev_queues; i++) {
		fq = &priv->fq[i % num_queues];
I
Ioana Radulescu 已提交
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514

		cpumask_clear(&xps_mask);
		cpumask_set_cpu(fq->target_cpu, &xps_mask);

		err = netif_set_xps_queue(net_dev, &xps_mask, i);
		if (err) {
			netdev_warn_once(net_dev, "Error setting XPS queue\n");
			break;
		}
	}

	return err;
}

2515 2516
static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
				  struct tc_mqprio_qopt *mqprio)
I
Ioana Radulescu 已提交
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	u8 num_tc, num_queues;
	int i;

	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
	num_queues = dpaa2_eth_queue_count(priv);
	num_tc = mqprio->num_tc;

	if (num_tc == net_dev->num_tc)
		return 0;

	if (num_tc  > dpaa2_eth_tc_count(priv)) {
		netdev_err(net_dev, "Max %d traffic classes supported\n",
			   dpaa2_eth_tc_count(priv));
2532
		return -EOPNOTSUPP;
I
Ioana Radulescu 已提交
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
	}

	if (!num_tc) {
		netdev_reset_tc(net_dev);
		netif_set_real_num_tx_queues(net_dev, num_queues);
		goto out;
	}

	netdev_set_num_tc(net_dev, num_tc);
	netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);

	for (i = 0; i < num_tc; i++)
		netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);

out:
	update_xps(priv);

	return 0;
}

2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
#define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)

static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
{
	struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
	struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
	int err;

	if (p->command == TC_TBF_STATS)
		return -EOPNOTSUPP;

	/* Only per port Tx shaping */
	if (p->parent != TC_H_ROOT)
		return -EOPNOTSUPP;

	if (p->command == TC_TBF_REPLACE) {
		if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
			netdev_err(net_dev, "burst size cannot be greater than %d\n",
				   DPAA2_ETH_MAX_BURST_SIZE);
			return -EINVAL;
		}

		tx_cr_shaper.max_burst_size = cfg->max_size;
		/* The TBF interface is in bytes/s, whereas DPAA2 expects the
		 * rate in Mbits/s
		 */
		tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
	}

	err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
				  &tx_er_shaper, 0);
	if (err) {
		netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
		return err;
	}

	return 0;
}

2594 2595 2596 2597 2598 2599
static int dpaa2_eth_setup_tc(struct net_device *net_dev,
			      enum tc_setup_type type, void *type_data)
{
	switch (type) {
	case TC_SETUP_QDISC_MQPRIO:
		return dpaa2_eth_setup_mqprio(net_dev, type_data);
2600 2601
	case TC_SETUP_QDISC_TBF:
		return dpaa2_eth_setup_tbf(net_dev, type_data);
2602 2603 2604 2605 2606
	default:
		return -EOPNOTSUPP;
	}
}

2607 2608 2609 2610 2611 2612 2613 2614
static const struct net_device_ops dpaa2_eth_ops = {
	.ndo_open = dpaa2_eth_open,
	.ndo_start_xmit = dpaa2_eth_tx,
	.ndo_stop = dpaa2_eth_stop,
	.ndo_set_mac_address = dpaa2_eth_set_addr,
	.ndo_get_stats64 = dpaa2_eth_get_stats,
	.ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
	.ndo_set_features = dpaa2_eth_set_features,
2615
	.ndo_eth_ioctl = dpaa2_eth_ioctl,
2616 2617
	.ndo_change_mtu = dpaa2_eth_change_mtu,
	.ndo_bpf = dpaa2_eth_xdp,
2618
	.ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
I
Ioana Radulescu 已提交
2619
	.ndo_setup_tc = dpaa2_eth_setup_tc,
2620 2621
	.ndo_vlan_rx_add_vid = dpaa2_eth_rx_add_vid,
	.ndo_vlan_rx_kill_vid = dpaa2_eth_rx_kill_vid
2622 2623
};

2624
static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
2625 2626 2627 2628
{
	struct dpaa2_eth_channel *ch;

	ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
2629 2630 2631 2632

	/* Update NAPI statistics */
	ch->stats.cdan++;

2633
	napi_schedule(&ch->napi);
2634 2635 2636
}

/* Allocate and configure a DPCON object */
2637
static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
2638 2639 2640 2641 2642 2643 2644 2645
{
	struct fsl_mc_device *dpcon;
	struct device *dev = priv->net_dev->dev.parent;
	int err;

	err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
				     FSL_MC_POOL_DPCON, &dpcon);
	if (err) {
2646 2647 2648 2649 2650
		if (err == -ENXIO)
			err = -EPROBE_DEFER;
		else
			dev_info(dev, "Not enough DPCONs, will go on as-is\n");
		return ERR_PTR(err);
2651 2652 2653 2654 2655
	}

	err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
	if (err) {
		dev_err(dev, "dpcon_open() failed\n");
2656
		goto free;
2657 2658 2659 2660 2661
	}

	err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
	if (err) {
		dev_err(dev, "dpcon_reset() failed\n");
2662
		goto close;
2663 2664 2665 2666 2667
	}

	err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
	if (err) {
		dev_err(dev, "dpcon_enable() failed\n");
2668
		goto close;
2669 2670 2671 2672
	}

	return dpcon;

2673
close:
2674
	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2675
free:
2676 2677
	fsl_mc_object_free(dpcon);

2678
	return ERR_PTR(err);
2679 2680
}

2681 2682
static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
				 struct fsl_mc_device *dpcon)
2683 2684 2685 2686 2687 2688
{
	dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
	fsl_mc_object_free(dpcon);
}

2689
static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699
{
	struct dpaa2_eth_channel *channel;
	struct dpcon_attr attr;
	struct device *dev = priv->net_dev->dev.parent;
	int err;

	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
	if (!channel)
		return NULL;

2700
	channel->dpcon = dpaa2_eth_setup_dpcon(priv);
2701 2702
	if (IS_ERR(channel->dpcon)) {
		err = PTR_ERR(channel->dpcon);
2703
		goto err_setup;
2704
	}
2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719

	err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
				   &attr);
	if (err) {
		dev_err(dev, "dpcon_get_attributes() failed\n");
		goto err_get_attr;
	}

	channel->dpcon_id = attr.id;
	channel->ch_id = attr.qbman_ch_id;
	channel->priv = priv;

	return channel;

err_get_attr:
2720
	dpaa2_eth_free_dpcon(priv, channel->dpcon);
2721 2722
err_setup:
	kfree(channel);
2723
	return ERR_PTR(err);
2724 2725
}

2726 2727
static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
				   struct dpaa2_eth_channel *channel)
2728
{
2729
	dpaa2_eth_free_dpcon(priv, channel->dpcon);
2730 2731 2732 2733 2734 2735
	kfree(channel);
}

/* DPIO setup: allocate and configure QBMan channels, setup core affinity
 * and register data availability notifications
 */
2736
static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
{
	struct dpaa2_io_notification_ctx *nctx;
	struct dpaa2_eth_channel *channel;
	struct dpcon_notification_cfg dpcon_notif_cfg;
	struct device *dev = priv->net_dev->dev.parent;
	int i, err;

	/* We want the ability to spread ingress traffic (RX, TX conf) to as
	 * many cores as possible, so we need one channel for each core
	 * (unless there's fewer queues than cores, in which case the extra
	 * channels would be wasted).
	 * Allocate one channel per core and register it to the core's
	 * affine DPIO. If not enough channels are available for all cores
	 * or if some cores don't have an affine DPIO, there will be no
	 * ingress frame processing on those cores.
	 */
	cpumask_clear(&priv->dpio_cpumask);
	for_each_online_cpu(i) {
		/* Try to allocate a channel */
2756
		channel = dpaa2_eth_alloc_channel(priv);
2757
		if (IS_ERR_OR_NULL(channel)) {
2758
			err = PTR_ERR_OR_ZERO(channel);
2759 2760 2761
			if (err != -EPROBE_DEFER)
				dev_info(dev,
					 "No affine channel for cpu %d and above\n", i);
2762 2763 2764 2765 2766 2767 2768
			goto err_alloc_ch;
		}

		priv->channel[priv->num_channels] = channel;

		nctx = &channel->nctx;
		nctx->is_cdan = 1;
2769
		nctx->cb = dpaa2_eth_cdan_cb;
2770 2771 2772 2773
		nctx->id = channel->ch_id;
		nctx->desired_cpu = i;

		/* Register the new context */
2774
		channel->dpio = dpaa2_io_service_select(i);
2775
		err = dpaa2_io_service_register(channel->dpio, nctx, dev);
2776
		if (err) {
2777
			dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
2778
			/* If no affine DPIO for this core, there's probably
2779 2780 2781
			 * none available for next cores either. Signal we want
			 * to retry later, in case the DPIO devices weren't
			 * probed yet.
2782
			 */
2783
			err = -EPROBE_DEFER;
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
			goto err_service_reg;
		}

		/* Register DPCON notification with MC */
		dpcon_notif_cfg.dpio_id = nctx->dpio_id;
		dpcon_notif_cfg.priority = 0;
		dpcon_notif_cfg.user_ctx = nctx->qman64;
		err = dpcon_set_notification(priv->mc_io, 0,
					     channel->dpcon->mc_handle,
					     &dpcon_notif_cfg);
		if (err) {
			dev_err(dev, "dpcon_set_notification failed()\n");
			goto err_set_cdan;
		}

		/* If we managed to allocate a channel and also found an affine
		 * DPIO for this core, add it to the final mask
		 */
		cpumask_set_cpu(i, &priv->dpio_cpumask);
		priv->num_channels++;

		/* Stop if we already have enough channels to accommodate all
		 * RX and TX conf queues
		 */
2808
		if (priv->num_channels == priv->dpni_attrs.num_queues)
2809 2810 2811 2812 2813 2814
			break;
	}

	return 0;

err_set_cdan:
2815
	dpaa2_io_service_deregister(channel->dpio, nctx, dev);
2816
err_service_reg:
2817
	dpaa2_eth_free_channel(priv, channel);
2818
err_alloc_ch:
2819 2820 2821 2822 2823
	if (err == -EPROBE_DEFER) {
		for (i = 0; i < priv->num_channels; i++) {
			channel = priv->channel[i];
			nctx = &channel->nctx;
			dpaa2_io_service_deregister(channel->dpio, nctx, dev);
2824
			dpaa2_eth_free_channel(priv, channel);
2825 2826
		}
		priv->num_channels = 0;
2827
		return err;
2828
	}
2829

2830 2831
	if (cpumask_empty(&priv->dpio_cpumask)) {
		dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
2832
		return -ENODEV;
2833 2834 2835 2836 2837 2838 2839 2840
	}

	dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
		 cpumask_pr_args(&priv->dpio_cpumask));

	return 0;
}

2841
static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
2842
{
2843
	struct device *dev = priv->net_dev->dev.parent;
2844
	struct dpaa2_eth_channel *ch;
2845
	int i;
2846 2847 2848 2849

	/* deregister CDAN notifications and free channels */
	for (i = 0; i < priv->num_channels; i++) {
		ch = priv->channel[i];
2850
		dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
2851
		dpaa2_eth_free_channel(priv, ch);
2852 2853 2854
	}
}

2855 2856
static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
							      int cpu)
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872
{
	struct device *dev = priv->net_dev->dev.parent;
	int i;

	for (i = 0; i < priv->num_channels; i++)
		if (priv->channel[i]->nctx.desired_cpu == cpu)
			return priv->channel[i];

	/* We should never get here. Issue a warning and return
	 * the first channel, because it's still better than nothing
	 */
	dev_warn(dev, "No affine channel found for cpu %d\n", cpu);

	return priv->channel[0];
}

2873
static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
2874 2875 2876 2877
{
	struct device *dev = priv->net_dev->dev.parent;
	struct dpaa2_eth_fq *fq;
	int rx_cpu, txc_cpu;
I
Ioana Radulescu 已提交
2878
	int i;
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889

	/* For each FQ, pick one channel/CPU to deliver frames to.
	 * This may well change at runtime, either through irqbalance or
	 * through direct user intervention.
	 */
	rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);

	for (i = 0; i < priv->num_fqs; i++) {
		fq = &priv->fq[i];
		switch (fq->type) {
		case DPAA2_RX_FQ:
2890
		case DPAA2_RX_ERR_FQ:
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
			fq->target_cpu = rx_cpu;
			rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
			if (rx_cpu >= nr_cpu_ids)
				rx_cpu = cpumask_first(&priv->dpio_cpumask);
			break;
		case DPAA2_TX_CONF_FQ:
			fq->target_cpu = txc_cpu;
			txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
			if (txc_cpu >= nr_cpu_ids)
				txc_cpu = cpumask_first(&priv->dpio_cpumask);
			break;
		default:
			dev_err(dev, "Unknown FQ type: %d\n", fq->type);
		}
2905
		fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
2906
	}
I
Ioana Radulescu 已提交
2907 2908

	update_xps(priv);
2909 2910
}

2911
static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
2912
{
2913
	int i, j;
2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924

	/* We have one TxConf FQ per Tx flow.
	 * The number of Tx and Rx queues is the same.
	 * Tx queues come first in the fq array.
	 */
	for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
		priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
		priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
		priv->fq[priv->num_fqs++].flowid = (u16)i;
	}

2925 2926 2927 2928 2929 2930 2931
	for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
		for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
			priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
			priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
			priv->fq[priv->num_fqs].tc = (u8)j;
			priv->fq[priv->num_fqs++].flowid = (u16)i;
		}
2932 2933
	}

2934 2935 2936 2937
	/* We have exactly one Rx error queue per DPNI */
	priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
	priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;

2938
	/* For each FQ, decide on which core to process incoming frames */
2939
	dpaa2_eth_set_fq_affinity(priv);
2940 2941 2942
}

/* Allocate and configure one buffer pool for each interface */
2943
static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv)
2944 2945 2946 2947
{
	int err;
	struct fsl_mc_device *dpbp_dev;
	struct device *dev = priv->net_dev->dev.parent;
2948
	struct dpbp_attr dpbp_attrs;
2949 2950 2951 2952

	err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
				     &dpbp_dev);
	if (err) {
2953 2954 2955 2956
		if (err == -ENXIO)
			err = -EPROBE_DEFER;
		else
			dev_err(dev, "DPBP device allocation failed\n");
2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968
		return err;
	}

	priv->dpbp_dev = dpbp_dev;

	err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
			&dpbp_dev->mc_handle);
	if (err) {
		dev_err(dev, "dpbp_open() failed\n");
		goto err_open;
	}

2969 2970 2971 2972 2973 2974
	err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
	if (err) {
		dev_err(dev, "dpbp_reset() failed\n");
		goto err_reset;
	}

2975 2976 2977 2978 2979 2980 2981
	err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
	if (err) {
		dev_err(dev, "dpbp_enable() failed\n");
		goto err_enable;
	}

	err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
2982
				  &dpbp_attrs);
2983 2984 2985 2986
	if (err) {
		dev_err(dev, "dpbp_get_attributes() failed\n");
		goto err_get_attr;
	}
2987
	priv->bpid = dpbp_attrs.bpid;
2988 2989 2990 2991 2992 2993

	return 0;

err_get_attr:
	dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
err_enable:
2994
err_reset:
2995 2996 2997 2998 2999 3000 3001
	dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
err_open:
	fsl_mc_object_free(dpbp_dev);

	return err;
}

3002
static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv)
3003
{
3004
	dpaa2_eth_drain_pool(priv);
3005 3006 3007 3008 3009
	dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
	dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
	fsl_mc_object_free(priv->dpbp_dev);
}

3010
static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
3011
{
3012
	struct device *dev = priv->net_dev->dev.parent;
3013
	struct dpni_buffer_layout buf_layout = {0};
3014
	u16 rx_buf_align;
3015 3016
	int err;

3017 3018 3019 3020 3021 3022
	/* We need to check for WRIOP version 1.0.0, but depending on the MC
	 * version, this number is not always provided correctly on rev1.
	 * We need to check for both alternatives in this situation.
	 */
	if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
	    priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
3023
		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
3024
	else
3025
		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
3026

3027 3028 3029 3030 3031
	/* We need to ensure that the buffer size seen by WRIOP is a multiple
	 * of 64 or 256 bytes depending on the WRIOP version.
	 */
	priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);

3032
	/* tx buffer */
3033
	buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
3034
	buf_layout.pass_timestamp = true;
3035
	buf_layout.pass_frame_status = true;
3036
	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
3037 3038
			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3039
	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3040
				     DPNI_QUEUE_TX, &buf_layout);
3041 3042
	if (err) {
		dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
3043
		return err;
3044 3045 3046
	}

	/* tx-confirm buffer */
3047 3048
	buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3049
	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3050
				     DPNI_QUEUE_TX_CONFIRM, &buf_layout);
3051 3052
	if (err) {
		dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
3053 3054 3055
		return err;
	}

3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070
	/* Now that we've set our tx buffer layout, retrieve the minimum
	 * required tx data offset.
	 */
	err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
				      &priv->tx_data_offset);
	if (err) {
		dev_err(dev, "dpni_get_tx_data_offset() failed\n");
		return err;
	}

	if ((priv->tx_data_offset % 64) != 0)
		dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
			 priv->tx_data_offset);

	/* rx buffer */
3071
	buf_layout.pass_frame_status = true;
3072
	buf_layout.pass_parser_result = true;
3073
	buf_layout.data_align = rx_buf_align;
3074 3075 3076 3077 3078
	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
	buf_layout.private_data_size = 0;
	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
			     DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
3079 3080
			     DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
3081 3082 3083 3084 3085 3086 3087
	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
				     DPNI_QUEUE_RX, &buf_layout);
	if (err) {
		dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
		return err;
	}

3088 3089 3090
	return 0;
}

3091 3092 3093 3094 3095
#define DPNI_ENQUEUE_FQID_VER_MAJOR	7
#define DPNI_ENQUEUE_FQID_VER_MINOR	9

static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
				       struct dpaa2_eth_fq *fq,
3096
				       struct dpaa2_fd *fd, u8 prio,
3097
				       u32 num_frames __always_unused,
3098
				       int *frames_enqueued)
3099
{
3100 3101 3102 3103 3104 3105 3106 3107
	int err;

	err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
					  priv->tx_qdid, prio,
					  fq->tx_qdbin, fd);
	if (!err && frames_enqueued)
		*frames_enqueued = 1;
	return err;
3108 3109
}

3110 3111 3112 3113 3114
static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
						struct dpaa2_eth_fq *fq,
						struct dpaa2_fd *fd,
						u8 prio, u32 num_frames,
						int *frames_enqueued)
3115
{
3116 3117
	int err;

3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
	err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
						   fq->tx_fqid[prio],
						   fd, num_frames);

	if (err == 0)
		return -EBUSY;

	if (frames_enqueued)
		*frames_enqueued = err;
	return 0;
3128 3129
}

3130
static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
3131 3132 3133 3134 3135
{
	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
		priv->enqueue = dpaa2_eth_enqueue_qd;
	else
3136
		priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3137 3138
}

3139
static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165
{
	struct device *dev = priv->net_dev->dev.parent;
	struct dpni_link_cfg link_cfg = {0};
	int err;

	/* Get the default link options so we don't override other flags */
	err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
	if (err) {
		dev_err(dev, "dpni_get_link_cfg() failed\n");
		return err;
	}

	/* By default, enable both Rx and Tx pause frames */
	link_cfg.options |= DPNI_LINK_OPT_PAUSE;
	link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
	if (err) {
		dev_err(dev, "dpni_set_link_cfg() failed\n");
		return err;
	}

	priv->link_state.options = link_cfg.options;

	return 0;
}

3166
static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
I
Ioana Radulescu 已提交
3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196
{
	struct dpni_queue_id qid = {0};
	struct dpaa2_eth_fq *fq;
	struct dpni_queue queue;
	int i, j, err;

	/* We only use Tx FQIDs for FQID-based enqueue, so check
	 * if DPNI version supports it before updating FQIDs
	 */
	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
		return;

	for (i = 0; i < priv->num_fqs; i++) {
		fq = &priv->fq[i];
		if (fq->type != DPAA2_TX_CONF_FQ)
			continue;
		for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
			err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
					     DPNI_QUEUE_TX, j, fq->flowid,
					     &queue, &qid);
			if (err)
				goto out_err;

			fq->tx_fqid[j] = qid.fqid;
			if (fq->tx_fqid[j] == 0)
				goto out_err;
		}
	}

3197
	priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
I
Ioana Radulescu 已提交
3198 3199 3200 3201 3202 3203 3204 3205 3206

	return;

out_err:
	netdev_info(priv->net_dev,
		    "Error reading Tx FQID, fallback to QDID-based enqueue\n");
	priv->enqueue = dpaa2_eth_enqueue_qd;
}

3207
/* Configure ingress classification based on VLAN PCP */
3208
static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318
{
	struct device *dev = priv->net_dev->dev.parent;
	struct dpkg_profile_cfg kg_cfg = {0};
	struct dpni_qos_tbl_cfg qos_cfg = {0};
	struct dpni_rule_cfg key_params;
	void *dma_mem, *key, *mask;
	u8 key_size = 2;	/* VLAN TCI field */
	int i, pcp, err;

	/* VLAN-based classification only makes sense if we have multiple
	 * traffic classes.
	 * Also, we need to extract just the 3-bit PCP field from the VLAN
	 * header and we can only do that by using a mask
	 */
	if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
		dev_dbg(dev, "VLAN-based QoS classification not supported\n");
		return -EOPNOTSUPP;
	}

	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
	if (!dma_mem)
		return -ENOMEM;

	kg_cfg.num_extracts = 1;
	kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
	kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
	kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
	kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;

	err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
	if (err) {
		dev_err(dev, "dpni_prepare_key_cfg failed\n");
		goto out_free_tbl;
	}

	/* set QoS table */
	qos_cfg.default_tc = 0;
	qos_cfg.discard_on_miss = 0;
	qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
					      DPAA2_CLASSIFIER_DMA_SIZE,
					      DMA_TO_DEVICE);
	if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
		dev_err(dev, "QoS table DMA mapping failed\n");
		err = -ENOMEM;
		goto out_free_tbl;
	}

	err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
	if (err) {
		dev_err(dev, "dpni_set_qos_table failed\n");
		goto out_unmap_tbl;
	}

	/* Add QoS table entries */
	key = kzalloc(key_size * 2, GFP_KERNEL);
	if (!key) {
		err = -ENOMEM;
		goto out_unmap_tbl;
	}
	mask = key + key_size;
	*(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);

	key_params.key_iova = dma_map_single(dev, key, key_size * 2,
					     DMA_TO_DEVICE);
	if (dma_mapping_error(dev, key_params.key_iova)) {
		dev_err(dev, "Qos table entry DMA mapping failed\n");
		err = -ENOMEM;
		goto out_free_key;
	}

	key_params.mask_iova = key_params.key_iova + key_size;
	key_params.key_size = key_size;

	/* We add rules for PCP-based distribution starting with highest
	 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
	 * classes to accommodate all priority levels, the lowest ones end up
	 * on TC 0 which was configured as default
	 */
	for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
		*(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
		dma_sync_single_for_device(dev, key_params.key_iova,
					   key_size * 2, DMA_TO_DEVICE);

		err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
					 &key_params, i, i);
		if (err) {
			dev_err(dev, "dpni_add_qos_entry failed\n");
			dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
			goto out_unmap_key;
		}
	}

	priv->vlan_cls_enabled = true;

	/* Table and key memory is not persistent, clean everything up after
	 * configuration is finished
	 */
out_unmap_key:
	dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
out_free_key:
	kfree(key);
out_unmap_tbl:
	dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
			 DMA_TO_DEVICE);
out_free_tbl:
	kfree(dma_mem);

	return err;
}

3319
/* Configure the DPNI object this interface is associated with */
3320
static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
{
	struct device *dev = &ls_dev->dev;
	struct dpaa2_eth_priv *priv;
	struct net_device *net_dev;
	int err;

	net_dev = dev_get_drvdata(dev);
	priv = netdev_priv(net_dev);

	/* get a handle for the DPNI object */
	err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
	if (err) {
		dev_err(dev, "dpni_open() failed\n");
		return err;
	}

3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351
	/* Check if we can work with this DPNI object */
	err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
				   &priv->dpni_ver_minor);
	if (err) {
		dev_err(dev, "dpni_get_api_version() failed\n");
		goto close;
	}
	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
		dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
			priv->dpni_ver_major, priv->dpni_ver_minor,
			DPNI_VER_MAJOR, DPNI_VER_MINOR);
		err = -ENOTSUPP;
		goto close;
	}

3352 3353 3354 3355 3356 3357
	ls_dev->mc_io = priv->mc_io;
	ls_dev->mc_handle = priv->mc_token;

	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
	if (err) {
		dev_err(dev, "dpni_reset() failed\n");
3358
		goto close;
3359 3360
	}

3361 3362 3363 3364 3365 3366 3367
	err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
				  &priv->dpni_attrs);
	if (err) {
		dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
		goto close;
	}

3368
	err = dpaa2_eth_set_buffer_layout(priv);
3369 3370 3371
	if (err)
		goto close;

3372
	dpaa2_eth_set_enqueue_mode(priv);
3373

3374 3375
	/* Enable pause frame support */
	if (dpaa2_eth_has_pause_support(priv)) {
3376
		err = dpaa2_eth_set_pause(priv);
3377 3378 3379 3380
		if (err)
			goto close;
	}

3381
	err = dpaa2_eth_set_vlan_qos(priv);
3382 3383 3384
	if (err && err != -EOPNOTSUPP)
		goto close;

3385 3386 3387
	priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
				       sizeof(struct dpaa2_eth_cls_rule),
				       GFP_KERNEL);
3388 3389
	if (!priv->cls_rules) {
		err = -ENOMEM;
3390
		goto close;
3391
	}
3392

3393 3394
	return 0;

3395
close:
3396
	dpni_close(priv->mc_io, 0, priv->mc_token);
3397

3398 3399 3400
	return err;
}

3401
static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
{
	int err;

	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
	if (err)
		netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
			    err);

	dpni_close(priv->mc_io, 0, priv->mc_token);
}

3413 3414
static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
				   struct dpaa2_eth_fq *fq)
3415 3416 3417 3418 3419 3420 3421
{
	struct device *dev = priv->net_dev->dev.parent;
	struct dpni_queue queue;
	struct dpni_queue_id qid;
	int err;

	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3422
			     DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432
	if (err) {
		dev_err(dev, "dpni_get_queue(RX) failed\n");
		return err;
	}

	fq->fqid = qid.fqid;

	queue.destination.id = fq->channel->dpcon_id;
	queue.destination.type = DPNI_DEST_DPCON;
	queue.destination.priority = 1;
3433
	queue.user_context = (u64)(uintptr_t)fq;
3434
	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3435
			     DPNI_QUEUE_RX, fq->tc, fq->flowid,
3436
			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3437 3438 3439 3440 3441 3442
			     &queue);
	if (err) {
		dev_err(dev, "dpni_set_queue(RX) failed\n");
		return err;
	}

3443
	/* xdp_rxq setup */
3444 3445 3446 3447
	/* only once for each channel */
	if (fq->tc > 0)
		return 0;

3448
	err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3449
			       fq->flowid, 0);
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461
	if (err) {
		dev_err(dev, "xdp_rxq_info_reg failed\n");
		return err;
	}

	err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
					 MEM_TYPE_PAGE_ORDER0, NULL);
	if (err) {
		dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
		return err;
	}

3462 3463 3464
	return 0;
}

3465 3466
static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
				   struct dpaa2_eth_fq *fq)
3467 3468 3469 3470
{
	struct device *dev = priv->net_dev->dev.parent;
	struct dpni_queue queue;
	struct dpni_queue_id qid;
3471
	int i, err;
3472

3473 3474 3475 3476 3477 3478 3479 3480 3481
	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
		err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
				     DPNI_QUEUE_TX, i, fq->flowid,
				     &queue, &qid);
		if (err) {
			dev_err(dev, "dpni_get_queue(TX) failed\n");
			return err;
		}
		fq->tx_fqid[i] = qid.fqid;
3482 3483
	}

3484
	/* All Tx queues belonging to the same flowid have the same qdbin */
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
	fq->tx_qdbin = qid.qdbin;

	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
			     &queue, &qid);
	if (err) {
		dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
		return err;
	}

	fq->fqid = qid.fqid;

	queue.destination.id = fq->channel->dpcon_id;
	queue.destination.type = DPNI_DEST_DPCON;
	queue.destination.priority = 0;
3500
	queue.user_context = (u64)(uintptr_t)fq;
3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512
	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
			     &queue);
	if (err) {
		dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
		return err;
	}

	return 0;
}

3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544
static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
			     struct dpaa2_eth_fq *fq)
{
	struct device *dev = priv->net_dev->dev.parent;
	struct dpni_queue q = { { 0 } };
	struct dpni_queue_id qid;
	u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
	int err;

	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
			     DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
	if (err) {
		dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
		return err;
	}

	fq->fqid = qid.fqid;

	q.destination.id = fq->channel->dpcon_id;
	q.destination.type = DPNI_DEST_DPCON;
	q.destination.priority = 1;
	q.user_context = (u64)(uintptr_t)fq;
	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
			     DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
	if (err) {
		dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
		return err;
	}

	return 0;
}

3545
/* Supported header fields for Rx hash distribution key */
I
Ioana Radulescu 已提交
3546
static const struct dpaa2_eth_dist_fields dist_fields[] = {
3547
	{
3548 3549 3550 3551
		/* L2 header */
		.rxnfc_field = RXH_L2DA,
		.cls_prot = NET_PROT_ETH,
		.cls_field = NH_FLD_ETH_DA,
3552
		.id = DPAA2_ETH_DIST_ETHDST,
3553
		.size = 6,
3554 3555 3556
	}, {
		.cls_prot = NET_PROT_ETH,
		.cls_field = NH_FLD_ETH_SA,
3557
		.id = DPAA2_ETH_DIST_ETHSRC,
3558 3559 3560 3561 3562 3563 3564 3565
		.size = 6,
	}, {
		/* This is the last ethertype field parsed:
		 * depending on frame format, it can be the MAC ethertype
		 * or the VLAN etype.
		 */
		.cls_prot = NET_PROT_ETH,
		.cls_field = NH_FLD_ETH_TYPE,
3566
		.id = DPAA2_ETH_DIST_ETHTYPE,
3567
		.size = 2,
3568 3569 3570 3571 3572
	}, {
		/* VLAN header */
		.rxnfc_field = RXH_VLAN,
		.cls_prot = NET_PROT_VLAN,
		.cls_field = NH_FLD_VLAN_TCI,
3573
		.id = DPAA2_ETH_DIST_VLAN,
3574 3575
		.size = 2,
	}, {
3576 3577 3578 3579
		/* IP header */
		.rxnfc_field = RXH_IP_SRC,
		.cls_prot = NET_PROT_IP,
		.cls_field = NH_FLD_IP_SRC,
3580
		.id = DPAA2_ETH_DIST_IPSRC,
3581 3582 3583 3584 3585
		.size = 4,
	}, {
		.rxnfc_field = RXH_IP_DST,
		.cls_prot = NET_PROT_IP,
		.cls_field = NH_FLD_IP_DST,
3586
		.id = DPAA2_ETH_DIST_IPDST,
3587 3588 3589 3590 3591
		.size = 4,
	}, {
		.rxnfc_field = RXH_L3_PROTO,
		.cls_prot = NET_PROT_IP,
		.cls_field = NH_FLD_IP_PROTO,
3592
		.id = DPAA2_ETH_DIST_IPPROTO,
3593 3594 3595 3596 3597 3598 3599 3600
		.size = 1,
	}, {
		/* Using UDP ports, this is functionally equivalent to raw
		 * byte pairs from L4 header.
		 */
		.rxnfc_field = RXH_L4_B_0_1,
		.cls_prot = NET_PROT_UDP,
		.cls_field = NH_FLD_UDP_PORT_SRC,
3601
		.id = DPAA2_ETH_DIST_L4SRC,
3602 3603 3604 3605 3606
		.size = 2,
	}, {
		.rxnfc_field = RXH_L4_B_2_3,
		.cls_prot = NET_PROT_UDP,
		.cls_field = NH_FLD_UDP_PORT_DST,
3607
		.id = DPAA2_ETH_DIST_L4DST,
3608 3609 3610 3611
		.size = 2,
	},
};

3612
/* Configure the Rx hash key using the legacy API */
3613
static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3614 3615 3616
{
	struct device *dev = priv->net_dev->dev.parent;
	struct dpni_rx_tc_dist_cfg dist_cfg;
3617
	int i, err = 0;
3618 3619 3620 3621 3622 3623 3624

	memset(&dist_cfg, 0, sizeof(dist_cfg));

	dist_cfg.key_cfg_iova = key;
	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
	dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;

3625 3626 3627 3628 3629 3630 3631 3632
	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
		err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
					  i, &dist_cfg);
		if (err) {
			dev_err(dev, "dpni_set_rx_tc_dist failed\n");
			break;
		}
	}
3633 3634 3635 3636 3637

	return err;
}

/* Configure the Rx hash key using the new API */
3638
static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3639 3640 3641
{
	struct device *dev = priv->net_dev->dev.parent;
	struct dpni_rx_dist_cfg dist_cfg;
3642
	int i, err = 0;
3643 3644 3645 3646 3647 3648 3649

	memset(&dist_cfg, 0, sizeof(dist_cfg));

	dist_cfg.key_cfg_iova = key;
	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
	dist_cfg.enable = 1;

3650 3651 3652 3653 3654 3655 3656 3657
	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
		dist_cfg.tc = i;
		err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
					    &dist_cfg);
		if (err) {
			dev_err(dev, "dpni_set_rx_hash_dist failed\n");
			break;
		}
3658 3659 3660 3661 3662 3663

		/* If the flow steering / hashing key is shared between all
		 * traffic classes, install it just once
		 */
		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
			break;
3664
	}
3665 3666 3667 3668

	return err;
}

3669
/* Configure the Rx flow classification key */
3670
static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3671 3672 3673
{
	struct device *dev = priv->net_dev->dev.parent;
	struct dpni_rx_dist_cfg dist_cfg;
3674
	int i, err = 0;
3675 3676 3677 3678 3679 3680 3681

	memset(&dist_cfg, 0, sizeof(dist_cfg));

	dist_cfg.key_cfg_iova = key;
	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
	dist_cfg.enable = 1;

3682 3683 3684 3685 3686 3687 3688 3689
	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
		dist_cfg.tc = i;
		err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
					  &dist_cfg);
		if (err) {
			dev_err(dev, "dpni_set_rx_fs_dist failed\n");
			break;
		}
3690 3691 3692 3693 3694 3695

		/* If the flow steering / hashing key is shared between all
		 * traffic classes, install it just once
		 */
		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
			break;
3696
	}
3697 3698 3699 3700

	return err;
}

3701
/* Size of the Rx flow classification key */
3702
int dpaa2_eth_cls_key_size(u64 fields)
3703 3704 3705
{
	int i, size = 0;

3706 3707 3708
	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
		if (!(fields & dist_fields[i].id))
			continue;
3709
		size += dist_fields[i].size;
3710
	}
3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730

	return size;
}

/* Offset of header field in Rx classification key */
int dpaa2_eth_cls_fld_off(int prot, int field)
{
	int i, off = 0;

	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
		if (dist_fields[i].cls_prot == prot &&
		    dist_fields[i].cls_field == field)
			return off;
		off += dist_fields[i].size;
	}

	WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
	return 0;
}

3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
/* Prune unused fields from the classification rule.
 * Used when masking is not supported
 */
void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
{
	int off = 0, new_off = 0;
	int i, size;

	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
		size = dist_fields[i].size;
		if (dist_fields[i].id & fields) {
			memcpy(key_mem + new_off, key_mem + off, size);
			new_off += size;
		}
		off += size;
	}
}

3749
/* Set Rx distribution (hash or flow classification) key
3750 3751
 * flags is a combination of RXH_ bits
 */
3752 3753
static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
				  enum dpaa2_eth_rx_dist type, u64 flags)
3754 3755 3756 3757
{
	struct device *dev = net_dev->dev.parent;
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
	struct dpkg_profile_cfg cls_cfg;
3758
	u32 rx_hash_fields = 0;
3759
	dma_addr_t key_iova;
3760 3761 3762 3763 3764 3765
	u8 *dma_mem;
	int i;
	int err = 0;

	memset(&cls_cfg, 0, sizeof(cls_cfg));

I
Ioana Radulescu 已提交
3766
	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3767 3768 3769
		struct dpkg_extract *key =
			&cls_cfg.extracts[cls_cfg.num_extracts];

3770 3771
		/* For both Rx hashing and classification keys
		 * we set only the selected fields.
3772
		 */
3773 3774 3775
		if (!(flags & dist_fields[i].id))
			continue;
		if (type == DPAA2_ETH_RX_DIST_HASH)
3776
			rx_hash_fields |= dist_fields[i].rxnfc_field;
3777 3778 3779 3780 3781 3782 3783

		if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
			dev_err(dev, "error adding key extraction rule, too many rules?\n");
			return -E2BIG;
		}

		key->type = DPKG_EXTRACT_FROM_HDR;
I
Ioana Radulescu 已提交
3784
		key->extract.from_hdr.prot = dist_fields[i].cls_prot;
3785
		key->extract.from_hdr.type = DPKG_FULL_FIELD;
I
Ioana Radulescu 已提交
3786
		key->extract.from_hdr.field = dist_fields[i].cls_field;
3787 3788 3789
		cls_cfg.num_extracts++;
	}

3790
	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3791 3792 3793 3794 3795
	if (!dma_mem)
		return -ENOMEM;

	err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
	if (err) {
3796
		dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
3797
		goto free_key;
3798 3799 3800
	}

	/* Prepare for setting the rx dist */
3801 3802 3803
	key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
				  DMA_TO_DEVICE);
	if (dma_mapping_error(dev, key_iova)) {
3804 3805
		dev_err(dev, "DMA mapping failed\n");
		err = -ENOMEM;
3806
		goto free_key;
3807 3808
	}

3809 3810
	if (type == DPAA2_ETH_RX_DIST_HASH) {
		if (dpaa2_eth_has_legacy_dist(priv))
3811
			err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
3812
		else
3813
			err = dpaa2_eth_config_hash_key(priv, key_iova);
3814
	} else {
3815
		err = dpaa2_eth_config_cls_key(priv, key_iova);
3816
	}
3817 3818 3819

	dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
			 DMA_TO_DEVICE);
3820
	if (!err && type == DPAA2_ETH_RX_DIST_HASH)
3821
		priv->rx_hash_fields = rx_hash_fields;
3822

3823
free_key:
3824 3825 3826 3827
	kfree(dma_mem);
	return err;
}

3828 3829 3830
int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
{
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3831 3832
	u64 key = 0;
	int i;
3833 3834 3835 3836

	if (!dpaa2_eth_hash_enabled(priv))
		return -EOPNOTSUPP;

3837 3838 3839 3840 3841
	for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
		if (dist_fields[i].rxnfc_field & flags)
			key |= dist_fields[i].id;

	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
3842 3843
}

3844 3845 3846 3847 3848 3849
int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
{
	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
}

static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
3850 3851
{
	struct device *dev = priv->net_dev->dev.parent;
3852
	int err;
3853 3854 3855 3856 3857 3858 3859

	/* Check if we actually support Rx flow classification */
	if (dpaa2_eth_has_legacy_dist(priv)) {
		dev_dbg(dev, "Rx cls not supported by current MC version\n");
		return -EOPNOTSUPP;
	}

3860
	if (!dpaa2_eth_fs_enabled(priv)) {
3861 3862 3863 3864 3865 3866 3867 3868 3869
		dev_dbg(dev, "Rx cls disabled in DPNI options\n");
		return -EOPNOTSUPP;
	}

	if (!dpaa2_eth_hash_enabled(priv)) {
		dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
		return -EOPNOTSUPP;
	}

3870 3871 3872 3873 3874 3875 3876 3877
	/* If there is no support for masking in the classification table,
	 * we don't set a default key, as it will depend on the rules
	 * added by the user at runtime.
	 */
	if (!dpaa2_eth_fs_mask_enabled(priv))
		goto out;

	err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
3878 3879 3880
	if (err)
		return err;

3881
out:
3882 3883
	priv->rx_cls_enabled = 1;

3884
	return 0;
3885 3886
}

3887 3888 3889
/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
 * frame queues and channels
 */
3890
static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901
{
	struct net_device *net_dev = priv->net_dev;
	struct device *dev = net_dev->dev.parent;
	struct dpni_pools_cfg pools_params;
	struct dpni_error_cfg err_cfg;
	int err = 0;
	int i;

	pools_params.num_dpbp = 1;
	pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
	pools_params.pools[0].backup_pool = 0;
3902
	pools_params.pools[0].buffer_size = priv->rx_buf_size;
3903 3904 3905 3906 3907 3908
	err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
	if (err) {
		dev_err(dev, "dpni_set_pools() failed\n");
		return err;
	}

3909 3910
	/* have the interface implicitly distribute traffic based on
	 * the default hash key
3911
	 */
3912
	err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
3913
	if (err && err != -EOPNOTSUPP)
3914
		dev_err(dev, "Failed to configure hashing\n");
3915

3916 3917 3918
	/* Configure the flow classification key; it includes all
	 * supported header fields and cannot be modified at runtime
	 */
3919
	err = dpaa2_eth_set_default_cls(priv);
3920 3921 3922
	if (err && err != -EOPNOTSUPP)
		dev_err(dev, "Failed to configure Rx classification key\n");

3923
	/* Configure handling of error frames */
3924
	err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937
	err_cfg.set_frame_annotation = 1;
	err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
	err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
				       &err_cfg);
	if (err) {
		dev_err(dev, "dpni_set_errors_behavior failed\n");
		return err;
	}

	/* Configure Rx and Tx conf queues to generate CDANs */
	for (i = 0; i < priv->num_fqs; i++) {
		switch (priv->fq[i].type) {
		case DPAA2_RX_FQ:
3938
			err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
3939 3940
			break;
		case DPAA2_TX_CONF_FQ:
3941
			err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
3942
			break;
3943 3944 3945
		case DPAA2_RX_ERR_FQ:
			err = setup_rx_err_flow(priv, &priv->fq[i]);
			break;
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964
		default:
			dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
			return -EINVAL;
		}
		if (err)
			return err;
	}

	err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
			    DPNI_QUEUE_TX, &priv->tx_qdid);
	if (err) {
		dev_err(dev, "dpni_get_qdid() failed\n");
		return err;
	}

	return 0;
}

/* Allocate rings for storing incoming frame descriptors */
3965
static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991
{
	struct net_device *net_dev = priv->net_dev;
	struct device *dev = net_dev->dev.parent;
	int i;

	for (i = 0; i < priv->num_channels; i++) {
		priv->channel[i]->store =
			dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
		if (!priv->channel[i]->store) {
			netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
			goto err_ring;
		}
	}

	return 0;

err_ring:
	for (i = 0; i < priv->num_channels; i++) {
		if (!priv->channel[i]->store)
			break;
		dpaa2_io_store_destroy(priv->channel[i]->store);
	}

	return -ENOMEM;
}

3992
static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
3993 3994 3995 3996 3997 3998 3999
{
	int i;

	for (i = 0; i < priv->num_channels; i++)
		dpaa2_io_store_destroy(priv->channel[i]->store);
}

4000
static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
4001
{
4002
	struct net_device *net_dev = priv->net_dev;
4003 4004
	struct device *dev = net_dev->dev.parent;
	u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
4005
	int err;
4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017

	/* Get firmware address, if any */
	err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
	if (err) {
		dev_err(dev, "dpni_get_port_mac_addr() failed\n");
		return err;
	}

	/* Get DPNI attributes address, if any */
	err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
					dpni_mac_addr);
	if (err) {
4018
		dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033
		return err;
	}

	/* First check if firmware has any address configured by bootloader */
	if (!is_zero_ether_addr(mac_addr)) {
		/* If the DPMAC addr != DPNI addr, update it */
		if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
			err = dpni_set_primary_mac_addr(priv->mc_io, 0,
							priv->mc_token,
							mac_addr);
			if (err) {
				dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
				return err;
			}
		}
4034
		eth_hw_addr_set(net_dev, mac_addr);
4035
	} else if (is_zero_ether_addr(dpni_mac_addr)) {
4036 4037
		/* No MAC address configured, fill in net_dev->dev_addr
		 * with a random one
4038 4039
		 */
		eth_hw_addr_random(net_dev);
4040 4041
		dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");

4042 4043 4044
		err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
						net_dev->dev_addr);
		if (err) {
4045
			dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4046 4047
			return err;
		}
4048

4049 4050 4051 4052 4053 4054 4055 4056 4057 4058
		/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
		 * practical purposes, this will be our "permanent" mac address,
		 * at least until the next reboot. This move will also permit
		 * register_netdevice() to properly fill up net_dev->perm_addr.
		 */
		net_dev->addr_assign_type = NET_ADDR_PERM;
	} else {
		/* NET_ADDR_PERM is default, all we have to do is
		 * fill in the device addr.
		 */
4059
		eth_hw_addr_set(net_dev, dpni_mac_addr);
4060 4061
	}

4062 4063 4064
	return 0;
}

4065
static int dpaa2_eth_netdev_init(struct net_device *net_dev)
4066 4067 4068
{
	struct device *dev = net_dev->dev.parent;
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4069 4070
	u32 options = priv->dpni_attrs.options;
	u64 supported = 0, not_supported = 0;
4071
	u8 bcast_addr[ETH_ALEN];
4072
	u8 num_queues;
4073 4074 4075
	int err;

	net_dev->netdev_ops = &dpaa2_eth_ops;
4076
	net_dev->ethtool_ops = &dpaa2_ethtool_ops;
4077

4078
	err = dpaa2_eth_set_mac_addr(priv);
4079 4080 4081 4082
	if (err)
		return err;

	/* Explicitly add the broadcast address to the MAC filtering table */
4083 4084 4085
	eth_broadcast_addr(bcast_addr);
	err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
	if (err) {
4086 4087
		dev_err(dev, "dpni_add_mac_addr() failed\n");
		return err;
4088 4089
	}

4090
	/* Set MTU upper limit; lower limit is 68B (default value) */
4091
	net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
4092
	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
4093
					DPAA2_ETH_MFL);
4094 4095 4096 4097
	if (err) {
		dev_err(dev, "dpni_set_max_frame_length() failed\n");
		return err;
	}
4098

4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111
	/* Set actual number of queues in the net device */
	num_queues = dpaa2_eth_queue_count(priv);
	err = netif_set_real_num_tx_queues(net_dev, num_queues);
	if (err) {
		dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
		return err;
	}
	err = netif_set_real_num_rx_queues(net_dev, num_queues);
	if (err) {
		dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
		return err;
	}

4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126
	/* Capabilities listing */
	supported |= IFF_LIVE_ADDR_CHANGE;

	if (options & DPNI_OPT_NO_MAC_FILTER)
		not_supported |= IFF_UNICAST_FLT;
	else
		supported |= IFF_UNICAST_FLT;

	net_dev->priv_flags |= supported;
	net_dev->priv_flags &= ~not_supported;

	/* Features */
	net_dev->features = NETIF_F_RXCSUM |
			    NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
			    NETIF_F_SG | NETIF_F_HIGHDMA |
4127
			    NETIF_F_LLTX | NETIF_F_HW_TC;
4128
	net_dev->hw_features = net_dev->features;
4129

4130 4131 4132
	if (priv->dpni_attrs.vlan_filter_entries)
		net_dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;

4133 4134 4135
	return 0;
}

4136
static int dpaa2_eth_poll_link_state(void *arg)
4137 4138 4139 4140 4141
{
	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
	int err;

	while (!kthread_should_stop()) {
4142
		err = dpaa2_eth_link_state_update(priv);
4143 4144 4145 4146 4147 4148 4149 4150 4151
		if (unlikely(err))
			return err;

		msleep(DPAA2_ETH_LINK_STATE_REFRESH);
	}

	return 0;
}

4152 4153 4154 4155 4156 4157 4158
static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
{
	struct fsl_mc_device *dpni_dev, *dpmac_dev;
	struct dpaa2_mac *mac;
	int err;

	dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
4159
	dpmac_dev = fsl_mc_get_endpoint(dpni_dev, 0);
4160 4161 4162 4163 4164

	if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER)
		return PTR_ERR(dpmac_dev);

	if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
4165 4166 4167 4168 4169 4170 4171 4172 4173 4174
		return 0;

	mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
	if (!mac)
		return -ENOMEM;

	mac->mc_dev = dpmac_dev;
	mac->mc_io = priv->mc_io;
	mac->net_dev = priv->net_dev;

4175 4176 4177
	err = dpaa2_mac_open(mac);
	if (err)
		goto err_free_mac;
4178
	priv->mac = mac;
4179

4180 4181
	if (dpaa2_eth_is_type_phy(priv)) {
		err = dpaa2_mac_connect(mac);
4182 4183 4184 4185
		if (err && err != -EPROBE_DEFER)
			netdev_err(priv->net_dev, "Error connecting to the MAC endpoint: %pe",
				   ERR_PTR(err));
		if (err)
4186
			goto err_close_mac;
4187 4188 4189
	}

	return 0;
4190 4191 4192

err_close_mac:
	dpaa2_mac_close(mac);
4193
	priv->mac = NULL;
4194 4195 4196
err_free_mac:
	kfree(mac);
	return err;
4197 4198 4199 4200
}

static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
{
4201 4202
	if (dpaa2_eth_is_type_phy(priv))
		dpaa2_mac_disconnect(priv->mac);
4203

4204 4205 4206
	if (!dpaa2_eth_has_mac(priv))
		return;

4207
	dpaa2_mac_close(priv->mac);
4208 4209 4210 4211
	kfree(priv->mac);
	priv->mac = NULL;
}

4212 4213
static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
{
4214
	u32 status = ~0;
4215 4216 4217
	struct device *dev = (struct device *)arg;
	struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
	struct net_device *net_dev = dev_get_drvdata(dev);
4218
	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4219 4220 4221 4222 4223
	int err;

	err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
				  DPNI_IRQ_INDEX, &status);
	if (unlikely(err)) {
4224
		netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
4225
		return IRQ_HANDLED;
4226 4227
	}

4228
	if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
4229
		dpaa2_eth_link_state_update(netdev_priv(net_dev));
4230

4231
	if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
4232 4233
		dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
		dpaa2_eth_update_tx_fqids(priv);
4234 4235

		rtnl_lock();
4236
		if (dpaa2_eth_has_mac(priv))
4237 4238 4239 4240
			dpaa2_eth_disconnect_mac(priv);
		else
			dpaa2_eth_connect_mac(priv);
		rtnl_unlock();
4241
	}
4242

4243 4244 4245
	return IRQ_HANDLED;
}

4246
static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257
{
	int err = 0;
	struct fsl_mc_device_irq *irq;

	err = fsl_mc_allocate_irqs(ls_dev);
	if (err) {
		dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
		return err;
	}

	irq = ls_dev->irqs[0];
4258
	err = devm_request_threaded_irq(&ls_dev->dev, irq->virq,
4259
					NULL, dpni_irq0_handler_thread,
4260 4261 4262
					IRQF_NO_SUSPEND | IRQF_ONESHOT,
					dev_name(&ls_dev->dev), &ls_dev->dev);
	if (err < 0) {
4263
		dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
4264 4265 4266 4267
		goto free_mc_irq;
	}

	err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
4268 4269
				DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
				DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
4270
	if (err < 0) {
4271
		dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
4272 4273 4274 4275 4276 4277
		goto free_irq;
	}

	err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
				  DPNI_IRQ_INDEX, 1);
	if (err < 0) {
4278
		dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
4279 4280 4281 4282 4283 4284
		goto free_irq;
	}

	return 0;

free_irq:
4285
	devm_free_irq(&ls_dev->dev, irq->virq, &ls_dev->dev);
4286 4287 4288 4289 4290 4291
free_mc_irq:
	fsl_mc_free_irqs(ls_dev);

	return err;
}

4292
static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304
{
	int i;
	struct dpaa2_eth_channel *ch;

	for (i = 0; i < priv->num_channels; i++) {
		ch = priv->channel[i];
		/* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
		netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
			       NAPI_POLL_WEIGHT);
	}
}

4305
static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325
{
	int i;
	struct dpaa2_eth_channel *ch;

	for (i = 0; i < priv->num_channels; i++) {
		ch = priv->channel[i];
		netif_napi_del(&ch->napi);
	}
}

static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
{
	struct device *dev;
	struct net_device *net_dev = NULL;
	struct dpaa2_eth_priv *priv = NULL;
	int err = 0;

	dev = &dpni_dev->dev;

	/* Net device */
I
Ioana Radulescu 已提交
4326
	net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337
	if (!net_dev) {
		dev_err(dev, "alloc_etherdev_mq() failed\n");
		return -ENOMEM;
	}

	SET_NETDEV_DEV(net_dev, dev);
	dev_set_drvdata(dev, net_dev);

	priv = netdev_priv(net_dev);
	priv->net_dev = net_dev;

4338 4339
	priv->iommu_domain = iommu_get_domain_for_dev(dev);

4340 4341 4342
	priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
	priv->rx_tstamp = false;

4343 4344 4345 4346 4347 4348 4349 4350 4351 4352
	priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
	if (!priv->dpaa2_ptp_wq) {
		err = -ENOMEM;
		goto err_wq_alloc;
	}

	INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);

	skb_queue_head_init(&priv->tx_skbs);

4353 4354
	priv->rx_copybreak = DPAA2_ETH_DEFAULT_COPYBREAK;

4355 4356 4357 4358
	/* Obtain a MC portal */
	err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
				     &priv->mc_io);
	if (err) {
4359 4360 4361 4362
		if (err == -ENXIO)
			err = -EPROBE_DEFER;
		else
			dev_err(dev, "MC portal allocation failed\n");
4363 4364 4365 4366
		goto err_portal_alloc;
	}

	/* MC objects initialization and configuration */
4367
	err = dpaa2_eth_setup_dpni(dpni_dev);
4368 4369 4370
	if (err)
		goto err_dpni_setup;

4371
	err = dpaa2_eth_setup_dpio(priv);
4372 4373 4374
	if (err)
		goto err_dpio_setup;

4375
	dpaa2_eth_setup_fqs(priv);
4376

4377
	err = dpaa2_eth_setup_dpbp(priv);
4378 4379 4380
	if (err)
		goto err_dpbp_setup;

4381
	err = dpaa2_eth_bind_dpni(priv);
4382 4383 4384 4385
	if (err)
		goto err_bind;

	/* Add a NAPI context for each channel */
4386
	dpaa2_eth_add_ch_napi(priv);
4387 4388 4389 4390 4391 4392 4393 4394

	/* Percpu statistics */
	priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
	if (!priv->percpu_stats) {
		dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
		err = -ENOMEM;
		goto err_alloc_percpu_stats;
	}
4395 4396 4397 4398 4399 4400
	priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
	if (!priv->percpu_extras) {
		dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
		err = -ENOMEM;
		goto err_alloc_percpu_extras;
	}
4401

4402 4403 4404 4405 4406 4407 4408
	priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
	if (!priv->sgt_cache) {
		dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
		err = -ENOMEM;
		goto err_alloc_sgt_cache;
	}

4409
	err = dpaa2_eth_netdev_init(net_dev);
4410 4411 4412 4413
	if (err)
		goto err_netdev_init;

	/* Configure checksum offload based on current interface flags */
4414
	err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
4415 4416 4417
	if (err)
		goto err_csum;

4418 4419
	err = dpaa2_eth_set_tx_csum(priv,
				    !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
4420 4421 4422
	if (err)
		goto err_csum;

4423
	err = dpaa2_eth_alloc_rings(priv);
4424 4425 4426
	if (err)
		goto err_alloc_rings;

4427 4428 4429 4430 4431 4432 4433 4434 4435
#ifdef CONFIG_FSL_DPAA2_ETH_DCB
	if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
		priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
		net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
	} else {
		dev_dbg(dev, "PFC not supported\n");
	}
#endif

4436
	err = dpaa2_eth_setup_irqs(dpni_dev);
4437 4438
	if (err) {
		netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
4439
		priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
4440 4441
						"%s_poll_link", net_dev->name);
		if (IS_ERR(priv->poll_thread)) {
4442
			dev_err(dev, "Error starting polling thread\n");
4443 4444 4445 4446 4447
			goto err_poll_thread;
		}
		priv->do_link_poll = true;
	}

4448 4449 4450 4451
	err = dpaa2_eth_connect_mac(priv);
	if (err)
		goto err_connect_mac;

4452
	err = dpaa2_eth_dl_alloc(priv);
4453 4454 4455
	if (err)
		goto err_dl_register;

4456 4457 4458 4459
	err = dpaa2_eth_dl_traps_register(priv);
	if (err)
		goto err_dl_trap_register;

4460 4461 4462 4463
	err = dpaa2_eth_dl_port_add(priv);
	if (err)
		goto err_dl_port_add;

4464 4465 4466 4467 4468 4469
	err = register_netdev(net_dev);
	if (err < 0) {
		dev_err(dev, "register_netdev() failed\n");
		goto err_netdev_reg;
	}

4470 4471 4472 4473
#ifdef CONFIG_DEBUG_FS
	dpaa2_dbg_add(priv);
#endif

4474
	dpaa2_eth_dl_register(priv);
4475 4476 4477
	dev_info(dev, "Probed interface %s\n", net_dev->name);
	return 0;

4478
err_netdev_reg:
4479 4480
	dpaa2_eth_dl_port_del(priv);
err_dl_port_add:
4481 4482
	dpaa2_eth_dl_traps_unregister(priv);
err_dl_trap_register:
4483
	dpaa2_eth_dl_free(priv);
4484
err_dl_register:
4485 4486
	dpaa2_eth_disconnect_mac(priv);
err_connect_mac:
4487 4488 4489 4490
	if (priv->do_link_poll)
		kthread_stop(priv->poll_thread);
	else
		fsl_mc_free_irqs(dpni_dev);
4491
err_poll_thread:
4492
	dpaa2_eth_free_rings(priv);
4493 4494 4495
err_alloc_rings:
err_csum:
err_netdev_init:
4496 4497
	free_percpu(priv->sgt_cache);
err_alloc_sgt_cache:
4498 4499
	free_percpu(priv->percpu_extras);
err_alloc_percpu_extras:
4500 4501
	free_percpu(priv->percpu_stats);
err_alloc_percpu_stats:
4502
	dpaa2_eth_del_ch_napi(priv);
4503
err_bind:
4504
	dpaa2_eth_free_dpbp(priv);
4505
err_dpbp_setup:
4506
	dpaa2_eth_free_dpio(priv);
4507
err_dpio_setup:
4508
	dpaa2_eth_free_dpni(priv);
4509 4510 4511
err_dpni_setup:
	fsl_mc_portal_free(priv->mc_io);
err_portal_alloc:
4512 4513
	destroy_workqueue(priv->dpaa2_ptp_wq);
err_wq_alloc:
4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529
	dev_set_drvdata(dev, NULL);
	free_netdev(net_dev);

	return err;
}

static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
{
	struct device *dev;
	struct net_device *net_dev;
	struct dpaa2_eth_priv *priv;

	dev = &ls_dev->dev;
	net_dev = dev_get_drvdata(dev);
	priv = netdev_priv(net_dev);

4530 4531
	dpaa2_eth_dl_unregister(priv);

4532 4533 4534
#ifdef CONFIG_DEBUG_FS
	dpaa2_dbg_remove(priv);
#endif
4535 4536 4537 4538
	rtnl_lock();
	dpaa2_eth_disconnect_mac(priv);
	rtnl_unlock();

4539 4540
	unregister_netdev(net_dev);

4541
	dpaa2_eth_dl_port_del(priv);
4542
	dpaa2_eth_dl_traps_unregister(priv);
4543
	dpaa2_eth_dl_free(priv);
4544

4545 4546 4547 4548 4549
	if (priv->do_link_poll)
		kthread_stop(priv->poll_thread);
	else
		fsl_mc_free_irqs(ls_dev);

4550
	dpaa2_eth_free_rings(priv);
4551
	free_percpu(priv->sgt_cache);
4552
	free_percpu(priv->percpu_stats);
4553
	free_percpu(priv->percpu_extras);
4554

4555 4556 4557 4558
	dpaa2_eth_del_ch_napi(priv);
	dpaa2_eth_free_dpbp(priv);
	dpaa2_eth_free_dpio(priv);
	dpaa2_eth_free_dpni(priv);
4559 4560 4561

	fsl_mc_portal_free(priv->mc_io);

4562 4563
	destroy_workqueue(priv->dpaa2_ptp_wq);

4564
	dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
4565

4566 4567
	free_netdev(net_dev);

4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589
	return 0;
}

static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
	{
		.vendor = FSL_MC_VENDOR_FREESCALE,
		.obj_type = "dpni",
	},
	{ .vendor = 0x0 }
};
MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);

static struct fsl_mc_driver dpaa2_eth_driver = {
	.driver = {
		.name = KBUILD_MODNAME,
		.owner = THIS_MODULE,
	},
	.probe = dpaa2_eth_probe,
	.remove = dpaa2_eth_remove,
	.match_id_table = dpaa2_eth_match_id_table
};

4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611
static int __init dpaa2_eth_driver_init(void)
{
	int err;

	dpaa2_eth_dbg_init();
	err = fsl_mc_driver_register(&dpaa2_eth_driver);
	if (err) {
		dpaa2_eth_dbg_exit();
		return err;
	}

	return 0;
}

static void __exit dpaa2_eth_driver_exit(void)
{
	dpaa2_eth_dbg_exit();
	fsl_mc_driver_unregister(&dpaa2_eth_driver);
}

module_init(dpaa2_eth_driver_init);
module_exit(dpaa2_eth_driver_exit);