1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* Copyright 2014-2016 Freescale Semiconductor Inc. 3 * Copyright 2016-2017 NXP 4 */ 5 #include <linux/init.h> 6 #include <linux/module.h> 7 #include <linux/platform_device.h> 8 #include <linux/etherdevice.h> 9 #include <linux/of_net.h> 10 #include <linux/interrupt.h> 11 #include <linux/msi.h> 12 #include <linux/kthread.h> 13 #include <linux/iommu.h> 14 #include <linux/net_tstamp.h> 15 #include <linux/fsl/mc.h> 16 #include <linux/bpf.h> 17 #include <linux/bpf_trace.h> 18 #include <net/sock.h> 19 20 #include "dpaa2-eth.h" 21 22 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files 23 * using trace events only need to #include <trace/events/sched.h> 24 */ 25 #define CREATE_TRACE_POINTS 26 #include "dpaa2-eth-trace.h" 27 28 MODULE_LICENSE("Dual BSD/GPL"); 29 MODULE_AUTHOR("Freescale Semiconductor, Inc"); 30 MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver"); 31 32 static void *dpaa2_iova_to_virt(struct iommu_domain *domain, 33 dma_addr_t iova_addr) 34 { 35 phys_addr_t phys_addr; 36 37 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr; 38 39 return phys_to_virt(phys_addr); 40 } 41 42 static void validate_rx_csum(struct dpaa2_eth_priv *priv, 43 u32 fd_status, 44 struct sk_buff *skb) 45 { 46 skb_checksum_none_assert(skb); 47 48 /* HW checksum validation is disabled, nothing to do here */ 49 if (!(priv->net_dev->features & NETIF_F_RXCSUM)) 50 return; 51 52 /* Read checksum validation bits */ 53 if (!((fd_status & DPAA2_FAS_L3CV) && 54 (fd_status & DPAA2_FAS_L4CV))) 55 return; 56 57 /* Inform the stack there's no need to compute L3/L4 csum anymore */ 58 skb->ip_summed = CHECKSUM_UNNECESSARY; 59 } 60 61 /* Free a received FD. 62 * Not to be used for Tx conf FDs or on any other paths. 63 */ 64 static void free_rx_fd(struct dpaa2_eth_priv *priv, 65 const struct dpaa2_fd *fd, 66 void *vaddr) 67 { 68 struct device *dev = priv->net_dev->dev.parent; 69 dma_addr_t addr = dpaa2_fd_get_addr(fd); 70 u8 fd_format = dpaa2_fd_get_format(fd); 71 struct dpaa2_sg_entry *sgt; 72 void *sg_vaddr; 73 int i; 74 75 /* If single buffer frame, just free the data buffer */ 76 if (fd_format == dpaa2_fd_single) 77 goto free_buf; 78 else if (fd_format != dpaa2_fd_sg) 79 /* We don't support any other format */ 80 return; 81 82 /* For S/G frames, we first need to free all SG entries 83 * except the first one, which was taken care of already 84 */ 85 sgt = vaddr + dpaa2_fd_get_offset(fd); 86 for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) { 87 addr = dpaa2_sg_get_addr(&sgt[i]); 88 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr); 89 dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE, 90 DMA_BIDIRECTIONAL); 91 92 free_pages((unsigned long)sg_vaddr, 0); 93 if (dpaa2_sg_is_final(&sgt[i])) 94 break; 95 } 96 97 free_buf: 98 free_pages((unsigned long)vaddr, 0); 99 } 100 101 /* Build a linear skb based on a single-buffer frame descriptor */ 102 static struct sk_buff *build_linear_skb(struct dpaa2_eth_channel *ch, 103 const struct dpaa2_fd *fd, 104 void *fd_vaddr) 105 { 106 struct sk_buff *skb = NULL; 107 u16 fd_offset = dpaa2_fd_get_offset(fd); 108 u32 fd_length = dpaa2_fd_get_len(fd); 109 110 ch->buf_count--; 111 112 skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE); 113 if (unlikely(!skb)) 114 return NULL; 115 116 skb_reserve(skb, fd_offset); 117 skb_put(skb, fd_length); 118 119 return skb; 120 } 121 122 /* Build a non linear (fragmented) skb based on a S/G table */ 123 static struct sk_buff *build_frag_skb(struct dpaa2_eth_priv *priv, 124 struct dpaa2_eth_channel *ch, 125 struct dpaa2_sg_entry *sgt) 126 { 127 struct sk_buff *skb = NULL; 128 struct device *dev = priv->net_dev->dev.parent; 129 void *sg_vaddr; 130 dma_addr_t sg_addr; 131 u16 sg_offset; 132 u32 sg_length; 133 struct page *page, *head_page; 134 int page_offset; 135 int i; 136 137 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) { 138 struct dpaa2_sg_entry *sge = &sgt[i]; 139 140 /* NOTE: We only support SG entries in dpaa2_sg_single format, 141 * but this is the only format we may receive from HW anyway 142 */ 143 144 /* Get the address and length from the S/G entry */ 145 sg_addr = dpaa2_sg_get_addr(sge); 146 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr); 147 dma_unmap_page(dev, sg_addr, DPAA2_ETH_RX_BUF_SIZE, 148 DMA_BIDIRECTIONAL); 149 150 sg_length = dpaa2_sg_get_len(sge); 151 152 if (i == 0) { 153 /* We build the skb around the first data buffer */ 154 skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE); 155 if (unlikely(!skb)) { 156 /* Free the first SG entry now, since we already 157 * unmapped it and obtained the virtual address 158 */ 159 free_pages((unsigned long)sg_vaddr, 0); 160 161 /* We still need to subtract the buffers used 162 * by this FD from our software counter 163 */ 164 while (!dpaa2_sg_is_final(&sgt[i]) && 165 i < DPAA2_ETH_MAX_SG_ENTRIES) 166 i++; 167 break; 168 } 169 170 sg_offset = dpaa2_sg_get_offset(sge); 171 skb_reserve(skb, sg_offset); 172 skb_put(skb, sg_length); 173 } else { 174 /* Rest of the data buffers are stored as skb frags */ 175 page = virt_to_page(sg_vaddr); 176 head_page = virt_to_head_page(sg_vaddr); 177 178 /* Offset in page (which may be compound). 179 * Data in subsequent SG entries is stored from the 180 * beginning of the buffer, so we don't need to add the 181 * sg_offset. 182 */ 183 page_offset = ((unsigned long)sg_vaddr & 184 (PAGE_SIZE - 1)) + 185 (page_address(page) - page_address(head_page)); 186 187 skb_add_rx_frag(skb, i - 1, head_page, page_offset, 188 sg_length, DPAA2_ETH_RX_BUF_SIZE); 189 } 190 191 if (dpaa2_sg_is_final(sge)) 192 break; 193 } 194 195 WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT"); 196 197 /* Count all data buffers + SG table buffer */ 198 ch->buf_count -= i + 2; 199 200 return skb; 201 } 202 203 /* Free buffers acquired from the buffer pool or which were meant to 204 * be released in the pool 205 */ 206 static void free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array, int count) 207 { 208 struct device *dev = priv->net_dev->dev.parent; 209 void *vaddr; 210 int i; 211 212 for (i = 0; i < count; i++) { 213 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]); 214 dma_unmap_page(dev, buf_array[i], DPAA2_ETH_RX_BUF_SIZE, 215 DMA_BIDIRECTIONAL); 216 free_pages((unsigned long)vaddr, 0); 217 } 218 } 219 220 static void xdp_release_buf(struct dpaa2_eth_priv *priv, 221 struct dpaa2_eth_channel *ch, 222 dma_addr_t addr) 223 { 224 int err; 225 226 ch->xdp.drop_bufs[ch->xdp.drop_cnt++] = addr; 227 if (ch->xdp.drop_cnt < DPAA2_ETH_BUFS_PER_CMD) 228 return; 229 230 while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid, 231 ch->xdp.drop_bufs, 232 ch->xdp.drop_cnt)) == -EBUSY) 233 cpu_relax(); 234 235 if (err) { 236 free_bufs(priv, ch->xdp.drop_bufs, ch->xdp.drop_cnt); 237 ch->buf_count -= ch->xdp.drop_cnt; 238 } 239 240 ch->xdp.drop_cnt = 0; 241 } 242 243 static int xdp_enqueue(struct dpaa2_eth_priv *priv, struct dpaa2_fd *fd, 244 void *buf_start, u16 queue_id) 245 { 246 struct dpaa2_eth_fq *fq; 247 struct dpaa2_faead *faead; 248 u32 ctrl, frc; 249 int i, err; 250 251 /* Mark the egress frame hardware annotation area as valid */ 252 frc = dpaa2_fd_get_frc(fd); 253 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV); 254 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL); 255 256 /* Instruct hardware to release the FD buffer directly into 257 * the buffer pool once transmission is completed, instead of 258 * sending a Tx confirmation frame to us 259 */ 260 ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV; 261 faead = dpaa2_get_faead(buf_start, false); 262 faead->ctrl = cpu_to_le32(ctrl); 263 faead->conf_fqid = 0; 264 265 fq = &priv->fq[queue_id]; 266 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) { 267 err = priv->enqueue(priv, fq, fd, 0); 268 if (err != -EBUSY) 269 break; 270 } 271 272 return err; 273 } 274 275 static u32 run_xdp(struct dpaa2_eth_priv *priv, 276 struct dpaa2_eth_channel *ch, 277 struct dpaa2_eth_fq *rx_fq, 278 struct dpaa2_fd *fd, void *vaddr) 279 { 280 dma_addr_t addr = dpaa2_fd_get_addr(fd); 281 struct rtnl_link_stats64 *percpu_stats; 282 struct bpf_prog *xdp_prog; 283 struct xdp_buff xdp; 284 u32 xdp_act = XDP_PASS; 285 int err; 286 287 percpu_stats = this_cpu_ptr(priv->percpu_stats); 288 289 rcu_read_lock(); 290 291 xdp_prog = READ_ONCE(ch->xdp.prog); 292 if (!xdp_prog) 293 goto out; 294 295 xdp.data = vaddr + dpaa2_fd_get_offset(fd); 296 xdp.data_end = xdp.data + dpaa2_fd_get_len(fd); 297 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM; 298 xdp_set_data_meta_invalid(&xdp); 299 xdp.rxq = &ch->xdp_rxq; 300 301 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp); 302 303 /* xdp.data pointer may have changed */ 304 dpaa2_fd_set_offset(fd, xdp.data - vaddr); 305 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data); 306 307 switch (xdp_act) { 308 case XDP_PASS: 309 break; 310 case XDP_TX: 311 err = xdp_enqueue(priv, fd, vaddr, rx_fq->flowid); 312 if (err) { 313 xdp_release_buf(priv, ch, addr); 314 percpu_stats->tx_errors++; 315 ch->stats.xdp_tx_err++; 316 } else { 317 percpu_stats->tx_packets++; 318 percpu_stats->tx_bytes += dpaa2_fd_get_len(fd); 319 ch->stats.xdp_tx++; 320 } 321 break; 322 default: 323 bpf_warn_invalid_xdp_action(xdp_act); 324 /* fall through */ 325 case XDP_ABORTED: 326 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act); 327 /* fall through */ 328 case XDP_DROP: 329 xdp_release_buf(priv, ch, addr); 330 ch->stats.xdp_drop++; 331 break; 332 case XDP_REDIRECT: 333 dma_unmap_page(priv->net_dev->dev.parent, addr, 334 DPAA2_ETH_RX_BUF_SIZE, DMA_BIDIRECTIONAL); 335 ch->buf_count--; 336 xdp.data_hard_start = vaddr; 337 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog); 338 if (unlikely(err)) 339 ch->stats.xdp_drop++; 340 else 341 ch->stats.xdp_redirect++; 342 break; 343 } 344 345 ch->xdp.res |= xdp_act; 346 out: 347 rcu_read_unlock(); 348 return xdp_act; 349 } 350 351 /* Main Rx frame processing routine */ 352 static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv, 353 struct dpaa2_eth_channel *ch, 354 const struct dpaa2_fd *fd, 355 struct dpaa2_eth_fq *fq) 356 { 357 dma_addr_t addr = dpaa2_fd_get_addr(fd); 358 u8 fd_format = dpaa2_fd_get_format(fd); 359 void *vaddr; 360 struct sk_buff *skb; 361 struct rtnl_link_stats64 *percpu_stats; 362 struct dpaa2_eth_drv_stats *percpu_extras; 363 struct device *dev = priv->net_dev->dev.parent; 364 struct dpaa2_fas *fas; 365 void *buf_data; 366 u32 status = 0; 367 u32 xdp_act; 368 369 /* Tracing point */ 370 trace_dpaa2_rx_fd(priv->net_dev, fd); 371 372 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr); 373 dma_sync_single_for_cpu(dev, addr, DPAA2_ETH_RX_BUF_SIZE, 374 DMA_BIDIRECTIONAL); 375 376 fas = dpaa2_get_fas(vaddr, false); 377 prefetch(fas); 378 buf_data = vaddr + dpaa2_fd_get_offset(fd); 379 prefetch(buf_data); 380 381 percpu_stats = this_cpu_ptr(priv->percpu_stats); 382 percpu_extras = this_cpu_ptr(priv->percpu_extras); 383 384 if (fd_format == dpaa2_fd_single) { 385 xdp_act = run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr); 386 if (xdp_act != XDP_PASS) { 387 percpu_stats->rx_packets++; 388 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd); 389 return; 390 } 391 392 dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE, 393 DMA_BIDIRECTIONAL); 394 skb = build_linear_skb(ch, fd, vaddr); 395 } else if (fd_format == dpaa2_fd_sg) { 396 WARN_ON(priv->xdp_prog); 397 398 dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE, 399 DMA_BIDIRECTIONAL); 400 skb = build_frag_skb(priv, ch, buf_data); 401 free_pages((unsigned long)vaddr, 0); 402 percpu_extras->rx_sg_frames++; 403 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd); 404 } else { 405 /* We don't support any other format */ 406 goto err_frame_format; 407 } 408 409 if (unlikely(!skb)) 410 goto err_build_skb; 411 412 prefetch(skb->data); 413 414 /* Get the timestamp value */ 415 if (priv->rx_tstamp) { 416 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 417 __le64 *ts = dpaa2_get_ts(vaddr, false); 418 u64 ns; 419 420 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 421 422 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts); 423 shhwtstamps->hwtstamp = ns_to_ktime(ns); 424 } 425 426 /* Check if we need to validate the L4 csum */ 427 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) { 428 status = le32_to_cpu(fas->status); 429 validate_rx_csum(priv, status, skb); 430 } 431 432 skb->protocol = eth_type_trans(skb, priv->net_dev); 433 skb_record_rx_queue(skb, fq->flowid); 434 435 percpu_stats->rx_packets++; 436 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd); 437 438 list_add_tail(&skb->list, ch->rx_list); 439 440 return; 441 442 err_build_skb: 443 free_rx_fd(priv, fd, vaddr); 444 err_frame_format: 445 percpu_stats->rx_dropped++; 446 } 447 448 /* Consume all frames pull-dequeued into the store. This is the simplest way to 449 * make sure we don't accidentally issue another volatile dequeue which would 450 * overwrite (leak) frames already in the store. 451 * 452 * Observance of NAPI budget is not our concern, leaving that to the caller. 453 */ 454 static int consume_frames(struct dpaa2_eth_channel *ch, 455 struct dpaa2_eth_fq **src) 456 { 457 struct dpaa2_eth_priv *priv = ch->priv; 458 struct dpaa2_eth_fq *fq = NULL; 459 struct dpaa2_dq *dq; 460 const struct dpaa2_fd *fd; 461 int cleaned = 0; 462 int is_last; 463 464 do { 465 dq = dpaa2_io_store_next(ch->store, &is_last); 466 if (unlikely(!dq)) { 467 /* If we're here, we *must* have placed a 468 * volatile dequeue comnmand, so keep reading through 469 * the store until we get some sort of valid response 470 * token (either a valid frame or an "empty dequeue") 471 */ 472 continue; 473 } 474 475 fd = dpaa2_dq_fd(dq); 476 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq); 477 478 fq->consume(priv, ch, fd, fq); 479 cleaned++; 480 } while (!is_last); 481 482 if (!cleaned) 483 return 0; 484 485 fq->stats.frames += cleaned; 486 487 /* A dequeue operation only pulls frames from a single queue 488 * into the store. Return the frame queue as an out param. 489 */ 490 if (src) 491 *src = fq; 492 493 return cleaned; 494 } 495 496 /* Configure the egress frame annotation for timestamp update */ 497 static void enable_tx_tstamp(struct dpaa2_fd *fd, void *buf_start) 498 { 499 struct dpaa2_faead *faead; 500 u32 ctrl, frc; 501 502 /* Mark the egress frame annotation area as valid */ 503 frc = dpaa2_fd_get_frc(fd); 504 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV); 505 506 /* Set hardware annotation size */ 507 ctrl = dpaa2_fd_get_ctrl(fd); 508 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL); 509 510 /* enable UPD (update prepanded data) bit in FAEAD field of 511 * hardware frame annotation area 512 */ 513 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD; 514 faead = dpaa2_get_faead(buf_start, true); 515 faead->ctrl = cpu_to_le32(ctrl); 516 } 517 518 /* Create a frame descriptor based on a fragmented skb */ 519 static int build_sg_fd(struct dpaa2_eth_priv *priv, 520 struct sk_buff *skb, 521 struct dpaa2_fd *fd) 522 { 523 struct device *dev = priv->net_dev->dev.parent; 524 void *sgt_buf = NULL; 525 dma_addr_t addr; 526 int nr_frags = skb_shinfo(skb)->nr_frags; 527 struct dpaa2_sg_entry *sgt; 528 int i, err; 529 int sgt_buf_size; 530 struct scatterlist *scl, *crt_scl; 531 int num_sg; 532 int num_dma_bufs; 533 struct dpaa2_eth_swa *swa; 534 535 /* Create and map scatterlist. 536 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have 537 * to go beyond nr_frags+1. 538 * Note: We don't support chained scatterlists 539 */ 540 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1)) 541 return -EINVAL; 542 543 scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC); 544 if (unlikely(!scl)) 545 return -ENOMEM; 546 547 sg_init_table(scl, nr_frags + 1); 548 num_sg = skb_to_sgvec(skb, scl, 0, skb->len); 549 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL); 550 if (unlikely(!num_dma_bufs)) { 551 err = -ENOMEM; 552 goto dma_map_sg_failed; 553 } 554 555 /* Prepare the HW SGT structure */ 556 sgt_buf_size = priv->tx_data_offset + 557 sizeof(struct dpaa2_sg_entry) * num_dma_bufs; 558 sgt_buf = napi_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN); 559 if (unlikely(!sgt_buf)) { 560 err = -ENOMEM; 561 goto sgt_buf_alloc_failed; 562 } 563 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN); 564 memset(sgt_buf, 0, sgt_buf_size); 565 566 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset); 567 568 /* Fill in the HW SGT structure. 569 * 570 * sgt_buf is zeroed out, so the following fields are implicit 571 * in all sgt entries: 572 * - offset is 0 573 * - format is 'dpaa2_sg_single' 574 */ 575 for_each_sg(scl, crt_scl, num_dma_bufs, i) { 576 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl)); 577 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl)); 578 } 579 dpaa2_sg_set_final(&sgt[i - 1], true); 580 581 /* Store the skb backpointer in the SGT buffer. 582 * Fit the scatterlist and the number of buffers alongside the 583 * skb backpointer in the software annotation area. We'll need 584 * all of them on Tx Conf. 585 */ 586 swa = (struct dpaa2_eth_swa *)sgt_buf; 587 swa->type = DPAA2_ETH_SWA_SG; 588 swa->sg.skb = skb; 589 swa->sg.scl = scl; 590 swa->sg.num_sg = num_sg; 591 swa->sg.sgt_size = sgt_buf_size; 592 593 /* Separately map the SGT buffer */ 594 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL); 595 if (unlikely(dma_mapping_error(dev, addr))) { 596 err = -ENOMEM; 597 goto dma_map_single_failed; 598 } 599 dpaa2_fd_set_offset(fd, priv->tx_data_offset); 600 dpaa2_fd_set_format(fd, dpaa2_fd_sg); 601 dpaa2_fd_set_addr(fd, addr); 602 dpaa2_fd_set_len(fd, skb->len); 603 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA); 604 605 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) 606 enable_tx_tstamp(fd, sgt_buf); 607 608 return 0; 609 610 dma_map_single_failed: 611 skb_free_frag(sgt_buf); 612 sgt_buf_alloc_failed: 613 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL); 614 dma_map_sg_failed: 615 kfree(scl); 616 return err; 617 } 618 619 /* Create a frame descriptor based on a linear skb */ 620 static int build_single_fd(struct dpaa2_eth_priv *priv, 621 struct sk_buff *skb, 622 struct dpaa2_fd *fd) 623 { 624 struct device *dev = priv->net_dev->dev.parent; 625 u8 *buffer_start, *aligned_start; 626 struct dpaa2_eth_swa *swa; 627 dma_addr_t addr; 628 629 buffer_start = skb->data - dpaa2_eth_needed_headroom(priv, skb); 630 631 /* If there's enough room to align the FD address, do it. 632 * It will help hardware optimize accesses. 633 */ 634 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN, 635 DPAA2_ETH_TX_BUF_ALIGN); 636 if (aligned_start >= skb->head) 637 buffer_start = aligned_start; 638 639 /* Store a backpointer to the skb at the beginning of the buffer 640 * (in the private data area) such that we can release it 641 * on Tx confirm 642 */ 643 swa = (struct dpaa2_eth_swa *)buffer_start; 644 swa->type = DPAA2_ETH_SWA_SINGLE; 645 swa->single.skb = skb; 646 647 addr = dma_map_single(dev, buffer_start, 648 skb_tail_pointer(skb) - buffer_start, 649 DMA_BIDIRECTIONAL); 650 if (unlikely(dma_mapping_error(dev, addr))) 651 return -ENOMEM; 652 653 dpaa2_fd_set_addr(fd, addr); 654 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start)); 655 dpaa2_fd_set_len(fd, skb->len); 656 dpaa2_fd_set_format(fd, dpaa2_fd_single); 657 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA); 658 659 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) 660 enable_tx_tstamp(fd, buffer_start); 661 662 return 0; 663 } 664 665 /* FD freeing routine on the Tx path 666 * 667 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb 668 * back-pointed to is also freed. 669 * This can be called either from dpaa2_eth_tx_conf() or on the error path of 670 * dpaa2_eth_tx(). 671 */ 672 static void free_tx_fd(const struct dpaa2_eth_priv *priv, 673 struct dpaa2_eth_fq *fq, 674 const struct dpaa2_fd *fd, bool in_napi) 675 { 676 struct device *dev = priv->net_dev->dev.parent; 677 dma_addr_t fd_addr; 678 struct sk_buff *skb = NULL; 679 unsigned char *buffer_start; 680 struct dpaa2_eth_swa *swa; 681 u8 fd_format = dpaa2_fd_get_format(fd); 682 u32 fd_len = dpaa2_fd_get_len(fd); 683 684 fd_addr = dpaa2_fd_get_addr(fd); 685 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr); 686 swa = (struct dpaa2_eth_swa *)buffer_start; 687 688 if (fd_format == dpaa2_fd_single) { 689 if (swa->type == DPAA2_ETH_SWA_SINGLE) { 690 skb = swa->single.skb; 691 /* Accessing the skb buffer is safe before dma unmap, 692 * because we didn't map the actual skb shell. 693 */ 694 dma_unmap_single(dev, fd_addr, 695 skb_tail_pointer(skb) - buffer_start, 696 DMA_BIDIRECTIONAL); 697 } else { 698 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type"); 699 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size, 700 DMA_BIDIRECTIONAL); 701 } 702 } else if (fd_format == dpaa2_fd_sg) { 703 skb = swa->sg.skb; 704 705 /* Unmap the scatterlist */ 706 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg, 707 DMA_BIDIRECTIONAL); 708 kfree(swa->sg.scl); 709 710 /* Unmap the SGT buffer */ 711 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size, 712 DMA_BIDIRECTIONAL); 713 } else { 714 netdev_dbg(priv->net_dev, "Invalid FD format\n"); 715 return; 716 } 717 718 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) { 719 fq->dq_frames++; 720 fq->dq_bytes += fd_len; 721 } 722 723 if (swa->type == DPAA2_ETH_SWA_XDP) { 724 xdp_return_frame(swa->xdp.xdpf); 725 return; 726 } 727 728 /* Get the timestamp value */ 729 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) { 730 struct skb_shared_hwtstamps shhwtstamps; 731 __le64 *ts = dpaa2_get_ts(buffer_start, true); 732 u64 ns; 733 734 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 735 736 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts); 737 shhwtstamps.hwtstamp = ns_to_ktime(ns); 738 skb_tstamp_tx(skb, &shhwtstamps); 739 } 740 741 /* Free SGT buffer allocated on tx */ 742 if (fd_format != dpaa2_fd_single) 743 skb_free_frag(buffer_start); 744 745 /* Move on with skb release */ 746 napi_consume_skb(skb, in_napi); 747 } 748 749 static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev) 750 { 751 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 752 struct dpaa2_fd fd; 753 struct rtnl_link_stats64 *percpu_stats; 754 struct dpaa2_eth_drv_stats *percpu_extras; 755 struct dpaa2_eth_fq *fq; 756 struct netdev_queue *nq; 757 u16 queue_mapping; 758 unsigned int needed_headroom; 759 u32 fd_len; 760 u8 prio = 0; 761 int err, i; 762 763 percpu_stats = this_cpu_ptr(priv->percpu_stats); 764 percpu_extras = this_cpu_ptr(priv->percpu_extras); 765 766 needed_headroom = dpaa2_eth_needed_headroom(priv, skb); 767 if (skb_headroom(skb) < needed_headroom) { 768 struct sk_buff *ns; 769 770 ns = skb_realloc_headroom(skb, needed_headroom); 771 if (unlikely(!ns)) { 772 percpu_stats->tx_dropped++; 773 goto err_alloc_headroom; 774 } 775 percpu_extras->tx_reallocs++; 776 777 if (skb->sk) 778 skb_set_owner_w(ns, skb->sk); 779 780 dev_kfree_skb(skb); 781 skb = ns; 782 } 783 784 /* We'll be holding a back-reference to the skb until Tx Confirmation; 785 * we don't want that overwritten by a concurrent Tx with a cloned skb. 786 */ 787 skb = skb_unshare(skb, GFP_ATOMIC); 788 if (unlikely(!skb)) { 789 /* skb_unshare() has already freed the skb */ 790 percpu_stats->tx_dropped++; 791 return NETDEV_TX_OK; 792 } 793 794 /* Setup the FD fields */ 795 memset(&fd, 0, sizeof(fd)); 796 797 if (skb_is_nonlinear(skb)) { 798 err = build_sg_fd(priv, skb, &fd); 799 percpu_extras->tx_sg_frames++; 800 percpu_extras->tx_sg_bytes += skb->len; 801 } else { 802 err = build_single_fd(priv, skb, &fd); 803 } 804 805 if (unlikely(err)) { 806 percpu_stats->tx_dropped++; 807 goto err_build_fd; 808 } 809 810 /* Tracing point */ 811 trace_dpaa2_tx_fd(net_dev, &fd); 812 813 /* TxConf FQ selection relies on queue id from the stack. 814 * In case of a forwarded frame from another DPNI interface, we choose 815 * a queue affined to the same core that processed the Rx frame 816 */ 817 queue_mapping = skb_get_queue_mapping(skb); 818 819 if (net_dev->num_tc) { 820 prio = netdev_txq_to_tc(net_dev, queue_mapping); 821 /* Hardware interprets priority level 0 as being the highest, 822 * so we need to do a reverse mapping to the netdev tc index 823 */ 824 prio = net_dev->num_tc - prio - 1; 825 /* We have only one FQ array entry for all Tx hardware queues 826 * with the same flow id (but different priority levels) 827 */ 828 queue_mapping %= dpaa2_eth_queue_count(priv); 829 } 830 fq = &priv->fq[queue_mapping]; 831 832 fd_len = dpaa2_fd_get_len(&fd); 833 nq = netdev_get_tx_queue(net_dev, queue_mapping); 834 netdev_tx_sent_queue(nq, fd_len); 835 836 /* Everything that happens after this enqueues might race with 837 * the Tx confirmation callback for this frame 838 */ 839 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) { 840 err = priv->enqueue(priv, fq, &fd, prio); 841 if (err != -EBUSY) 842 break; 843 } 844 percpu_extras->tx_portal_busy += i; 845 if (unlikely(err < 0)) { 846 percpu_stats->tx_errors++; 847 /* Clean up everything, including freeing the skb */ 848 free_tx_fd(priv, fq, &fd, false); 849 netdev_tx_completed_queue(nq, 1, fd_len); 850 } else { 851 percpu_stats->tx_packets++; 852 percpu_stats->tx_bytes += fd_len; 853 } 854 855 return NETDEV_TX_OK; 856 857 err_build_fd: 858 err_alloc_headroom: 859 dev_kfree_skb(skb); 860 861 return NETDEV_TX_OK; 862 } 863 864 /* Tx confirmation frame processing routine */ 865 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv, 866 struct dpaa2_eth_channel *ch __always_unused, 867 const struct dpaa2_fd *fd, 868 struct dpaa2_eth_fq *fq) 869 { 870 struct rtnl_link_stats64 *percpu_stats; 871 struct dpaa2_eth_drv_stats *percpu_extras; 872 u32 fd_len = dpaa2_fd_get_len(fd); 873 u32 fd_errors; 874 875 /* Tracing point */ 876 trace_dpaa2_tx_conf_fd(priv->net_dev, fd); 877 878 percpu_extras = this_cpu_ptr(priv->percpu_extras); 879 percpu_extras->tx_conf_frames++; 880 percpu_extras->tx_conf_bytes += fd_len; 881 882 /* Check frame errors in the FD field */ 883 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK; 884 free_tx_fd(priv, fq, fd, true); 885 886 if (likely(!fd_errors)) 887 return; 888 889 if (net_ratelimit()) 890 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n", 891 fd_errors); 892 893 percpu_stats = this_cpu_ptr(priv->percpu_stats); 894 /* Tx-conf logically pertains to the egress path. */ 895 percpu_stats->tx_errors++; 896 } 897 898 static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable) 899 { 900 int err; 901 902 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 903 DPNI_OFF_RX_L3_CSUM, enable); 904 if (err) { 905 netdev_err(priv->net_dev, 906 "dpni_set_offload(RX_L3_CSUM) failed\n"); 907 return err; 908 } 909 910 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 911 DPNI_OFF_RX_L4_CSUM, enable); 912 if (err) { 913 netdev_err(priv->net_dev, 914 "dpni_set_offload(RX_L4_CSUM) failed\n"); 915 return err; 916 } 917 918 return 0; 919 } 920 921 static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable) 922 { 923 int err; 924 925 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 926 DPNI_OFF_TX_L3_CSUM, enable); 927 if (err) { 928 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n"); 929 return err; 930 } 931 932 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 933 DPNI_OFF_TX_L4_CSUM, enable); 934 if (err) { 935 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n"); 936 return err; 937 } 938 939 return 0; 940 } 941 942 /* Perform a single release command to add buffers 943 * to the specified buffer pool 944 */ 945 static int add_bufs(struct dpaa2_eth_priv *priv, 946 struct dpaa2_eth_channel *ch, u16 bpid) 947 { 948 struct device *dev = priv->net_dev->dev.parent; 949 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD]; 950 struct page *page; 951 dma_addr_t addr; 952 int i, err; 953 954 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) { 955 /* Allocate buffer visible to WRIOP + skb shared info + 956 * alignment padding 957 */ 958 /* allocate one page for each Rx buffer. WRIOP sees 959 * the entire page except for a tailroom reserved for 960 * skb shared info 961 */ 962 page = dev_alloc_pages(0); 963 if (!page) 964 goto err_alloc; 965 966 addr = dma_map_page(dev, page, 0, DPAA2_ETH_RX_BUF_SIZE, 967 DMA_BIDIRECTIONAL); 968 if (unlikely(dma_mapping_error(dev, addr))) 969 goto err_map; 970 971 buf_array[i] = addr; 972 973 /* tracing point */ 974 trace_dpaa2_eth_buf_seed(priv->net_dev, 975 page, DPAA2_ETH_RX_BUF_RAW_SIZE, 976 addr, DPAA2_ETH_RX_BUF_SIZE, 977 bpid); 978 } 979 980 release_bufs: 981 /* In case the portal is busy, retry until successful */ 982 while ((err = dpaa2_io_service_release(ch->dpio, bpid, 983 buf_array, i)) == -EBUSY) 984 cpu_relax(); 985 986 /* If release command failed, clean up and bail out; 987 * not much else we can do about it 988 */ 989 if (err) { 990 free_bufs(priv, buf_array, i); 991 return 0; 992 } 993 994 return i; 995 996 err_map: 997 __free_pages(page, 0); 998 err_alloc: 999 /* If we managed to allocate at least some buffers, 1000 * release them to hardware 1001 */ 1002 if (i) 1003 goto release_bufs; 1004 1005 return 0; 1006 } 1007 1008 static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid) 1009 { 1010 int i, j; 1011 int new_count; 1012 1013 for (j = 0; j < priv->num_channels; j++) { 1014 for (i = 0; i < DPAA2_ETH_NUM_BUFS; 1015 i += DPAA2_ETH_BUFS_PER_CMD) { 1016 new_count = add_bufs(priv, priv->channel[j], bpid); 1017 priv->channel[j]->buf_count += new_count; 1018 1019 if (new_count < DPAA2_ETH_BUFS_PER_CMD) { 1020 return -ENOMEM; 1021 } 1022 } 1023 } 1024 1025 return 0; 1026 } 1027 1028 /** 1029 * Drain the specified number of buffers from the DPNI's private buffer pool. 1030 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD 1031 */ 1032 static void drain_bufs(struct dpaa2_eth_priv *priv, int count) 1033 { 1034 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD]; 1035 int ret; 1036 1037 do { 1038 ret = dpaa2_io_service_acquire(NULL, priv->bpid, 1039 buf_array, count); 1040 if (ret < 0) { 1041 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n"); 1042 return; 1043 } 1044 free_bufs(priv, buf_array, ret); 1045 } while (ret); 1046 } 1047 1048 static void drain_pool(struct dpaa2_eth_priv *priv) 1049 { 1050 int i; 1051 1052 drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD); 1053 drain_bufs(priv, 1); 1054 1055 for (i = 0; i < priv->num_channels; i++) 1056 priv->channel[i]->buf_count = 0; 1057 } 1058 1059 /* Function is called from softirq context only, so we don't need to guard 1060 * the access to percpu count 1061 */ 1062 static int refill_pool(struct dpaa2_eth_priv *priv, 1063 struct dpaa2_eth_channel *ch, 1064 u16 bpid) 1065 { 1066 int new_count; 1067 1068 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH)) 1069 return 0; 1070 1071 do { 1072 new_count = add_bufs(priv, ch, bpid); 1073 if (unlikely(!new_count)) { 1074 /* Out of memory; abort for now, we'll try later on */ 1075 break; 1076 } 1077 ch->buf_count += new_count; 1078 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS); 1079 1080 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS)) 1081 return -ENOMEM; 1082 1083 return 0; 1084 } 1085 1086 static int pull_channel(struct dpaa2_eth_channel *ch) 1087 { 1088 int err; 1089 int dequeues = -1; 1090 1091 /* Retry while portal is busy */ 1092 do { 1093 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id, 1094 ch->store); 1095 dequeues++; 1096 cpu_relax(); 1097 } while (err == -EBUSY); 1098 1099 ch->stats.dequeue_portal_busy += dequeues; 1100 if (unlikely(err)) 1101 ch->stats.pull_err++; 1102 1103 return err; 1104 } 1105 1106 /* NAPI poll routine 1107 * 1108 * Frames are dequeued from the QMan channel associated with this NAPI context. 1109 * Rx, Tx confirmation and (if configured) Rx error frames all count 1110 * towards the NAPI budget. 1111 */ 1112 static int dpaa2_eth_poll(struct napi_struct *napi, int budget) 1113 { 1114 struct dpaa2_eth_channel *ch; 1115 struct dpaa2_eth_priv *priv; 1116 int rx_cleaned = 0, txconf_cleaned = 0; 1117 struct dpaa2_eth_fq *fq, *txc_fq = NULL; 1118 struct netdev_queue *nq; 1119 int store_cleaned, work_done; 1120 struct list_head rx_list; 1121 int err; 1122 1123 ch = container_of(napi, struct dpaa2_eth_channel, napi); 1124 ch->xdp.res = 0; 1125 priv = ch->priv; 1126 1127 INIT_LIST_HEAD(&rx_list); 1128 ch->rx_list = &rx_list; 1129 1130 do { 1131 err = pull_channel(ch); 1132 if (unlikely(err)) 1133 break; 1134 1135 /* Refill pool if appropriate */ 1136 refill_pool(priv, ch, priv->bpid); 1137 1138 store_cleaned = consume_frames(ch, &fq); 1139 if (!store_cleaned) 1140 break; 1141 if (fq->type == DPAA2_RX_FQ) { 1142 rx_cleaned += store_cleaned; 1143 } else { 1144 txconf_cleaned += store_cleaned; 1145 /* We have a single Tx conf FQ on this channel */ 1146 txc_fq = fq; 1147 } 1148 1149 /* If we either consumed the whole NAPI budget with Rx frames 1150 * or we reached the Tx confirmations threshold, we're done. 1151 */ 1152 if (rx_cleaned >= budget || 1153 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) { 1154 work_done = budget; 1155 goto out; 1156 } 1157 } while (store_cleaned); 1158 1159 /* We didn't consume the entire budget, so finish napi and 1160 * re-enable data availability notifications 1161 */ 1162 napi_complete_done(napi, rx_cleaned); 1163 do { 1164 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx); 1165 cpu_relax(); 1166 } while (err == -EBUSY); 1167 WARN_ONCE(err, "CDAN notifications rearm failed on core %d", 1168 ch->nctx.desired_cpu); 1169 1170 work_done = max(rx_cleaned, 1); 1171 1172 out: 1173 netif_receive_skb_list(ch->rx_list); 1174 1175 if (txc_fq && txc_fq->dq_frames) { 1176 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid); 1177 netdev_tx_completed_queue(nq, txc_fq->dq_frames, 1178 txc_fq->dq_bytes); 1179 txc_fq->dq_frames = 0; 1180 txc_fq->dq_bytes = 0; 1181 } 1182 1183 if (ch->xdp.res & XDP_REDIRECT) 1184 xdp_do_flush_map(); 1185 1186 return work_done; 1187 } 1188 1189 static void enable_ch_napi(struct dpaa2_eth_priv *priv) 1190 { 1191 struct dpaa2_eth_channel *ch; 1192 int i; 1193 1194 for (i = 0; i < priv->num_channels; i++) { 1195 ch = priv->channel[i]; 1196 napi_enable(&ch->napi); 1197 } 1198 } 1199 1200 static void disable_ch_napi(struct dpaa2_eth_priv *priv) 1201 { 1202 struct dpaa2_eth_channel *ch; 1203 int i; 1204 1205 for (i = 0; i < priv->num_channels; i++) { 1206 ch = priv->channel[i]; 1207 napi_disable(&ch->napi); 1208 } 1209 } 1210 1211 static void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv, bool enable) 1212 { 1213 struct dpni_taildrop td = {0}; 1214 int i, err; 1215 1216 if (priv->rx_td_enabled == enable) 1217 return; 1218 1219 td.enable = enable; 1220 td.threshold = DPAA2_ETH_TAILDROP_THRESH; 1221 1222 for (i = 0; i < priv->num_fqs; i++) { 1223 if (priv->fq[i].type != DPAA2_RX_FQ) 1224 continue; 1225 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, 1226 DPNI_CP_QUEUE, DPNI_QUEUE_RX, 0, 1227 priv->fq[i].flowid, &td); 1228 if (err) { 1229 netdev_err(priv->net_dev, 1230 "dpni_set_taildrop() failed\n"); 1231 break; 1232 } 1233 } 1234 1235 priv->rx_td_enabled = enable; 1236 } 1237 1238 static int link_state_update(struct dpaa2_eth_priv *priv) 1239 { 1240 struct dpni_link_state state = {0}; 1241 bool tx_pause; 1242 int err; 1243 1244 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state); 1245 if (unlikely(err)) { 1246 netdev_err(priv->net_dev, 1247 "dpni_get_link_state() failed\n"); 1248 return err; 1249 } 1250 1251 /* If Tx pause frame settings have changed, we need to update 1252 * Rx FQ taildrop configuration as well. We configure taildrop 1253 * only when pause frame generation is disabled. 1254 */ 1255 tx_pause = !!(state.options & DPNI_LINK_OPT_PAUSE) ^ 1256 !!(state.options & DPNI_LINK_OPT_ASYM_PAUSE); 1257 dpaa2_eth_set_rx_taildrop(priv, !tx_pause); 1258 1259 /* Chech link state; speed / duplex changes are not treated yet */ 1260 if (priv->link_state.up == state.up) 1261 goto out; 1262 1263 if (state.up) { 1264 netif_carrier_on(priv->net_dev); 1265 netif_tx_start_all_queues(priv->net_dev); 1266 } else { 1267 netif_tx_stop_all_queues(priv->net_dev); 1268 netif_carrier_off(priv->net_dev); 1269 } 1270 1271 netdev_info(priv->net_dev, "Link Event: state %s\n", 1272 state.up ? "up" : "down"); 1273 1274 out: 1275 priv->link_state = state; 1276 1277 return 0; 1278 } 1279 1280 static int dpaa2_eth_open(struct net_device *net_dev) 1281 { 1282 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1283 int err; 1284 1285 err = seed_pool(priv, priv->bpid); 1286 if (err) { 1287 /* Not much to do; the buffer pool, though not filled up, 1288 * may still contain some buffers which would enable us 1289 * to limp on. 1290 */ 1291 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n", 1292 priv->dpbp_dev->obj_desc.id, priv->bpid); 1293 } 1294 1295 /* We'll only start the txqs when the link is actually ready; make sure 1296 * we don't race against the link up notification, which may come 1297 * immediately after dpni_enable(); 1298 */ 1299 netif_tx_stop_all_queues(net_dev); 1300 enable_ch_napi(priv); 1301 /* Also, explicitly set carrier off, otherwise netif_carrier_ok() will 1302 * return true and cause 'ip link show' to report the LOWER_UP flag, 1303 * even though the link notification wasn't even received. 1304 */ 1305 netif_carrier_off(net_dev); 1306 1307 err = dpni_enable(priv->mc_io, 0, priv->mc_token); 1308 if (err < 0) { 1309 netdev_err(net_dev, "dpni_enable() failed\n"); 1310 goto enable_err; 1311 } 1312 1313 /* If the DPMAC object has already processed the link up interrupt, 1314 * we have to learn the link state ourselves. 1315 */ 1316 err = link_state_update(priv); 1317 if (err < 0) { 1318 netdev_err(net_dev, "Can't update link state\n"); 1319 goto link_state_err; 1320 } 1321 1322 return 0; 1323 1324 link_state_err: 1325 enable_err: 1326 disable_ch_napi(priv); 1327 drain_pool(priv); 1328 return err; 1329 } 1330 1331 /* Total number of in-flight frames on ingress queues */ 1332 static u32 ingress_fq_count(struct dpaa2_eth_priv *priv) 1333 { 1334 struct dpaa2_eth_fq *fq; 1335 u32 fcnt = 0, bcnt = 0, total = 0; 1336 int i, err; 1337 1338 for (i = 0; i < priv->num_fqs; i++) { 1339 fq = &priv->fq[i]; 1340 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt); 1341 if (err) { 1342 netdev_warn(priv->net_dev, "query_fq_count failed"); 1343 break; 1344 } 1345 total += fcnt; 1346 } 1347 1348 return total; 1349 } 1350 1351 static void wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv) 1352 { 1353 int retries = 10; 1354 u32 pending; 1355 1356 do { 1357 pending = ingress_fq_count(priv); 1358 if (pending) 1359 msleep(100); 1360 } while (pending && --retries); 1361 } 1362 1363 #define DPNI_TX_PENDING_VER_MAJOR 7 1364 #define DPNI_TX_PENDING_VER_MINOR 13 1365 static void wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv) 1366 { 1367 union dpni_statistics stats; 1368 int retries = 10; 1369 int err; 1370 1371 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR, 1372 DPNI_TX_PENDING_VER_MINOR) < 0) 1373 goto out; 1374 1375 do { 1376 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6, 1377 &stats); 1378 if (err) 1379 goto out; 1380 if (stats.page_6.tx_pending_frames == 0) 1381 return; 1382 } while (--retries); 1383 1384 out: 1385 msleep(500); 1386 } 1387 1388 static int dpaa2_eth_stop(struct net_device *net_dev) 1389 { 1390 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1391 int dpni_enabled = 0; 1392 int retries = 10; 1393 1394 netif_tx_stop_all_queues(net_dev); 1395 netif_carrier_off(net_dev); 1396 1397 /* On dpni_disable(), the MC firmware will: 1398 * - stop MAC Rx and wait for all Rx frames to be enqueued to software 1399 * - cut off WRIOP dequeues from egress FQs and wait until transmission 1400 * of all in flight Tx frames is finished (and corresponding Tx conf 1401 * frames are enqueued back to software) 1402 * 1403 * Before calling dpni_disable(), we wait for all Tx frames to arrive 1404 * on WRIOP. After it finishes, wait until all remaining frames on Rx 1405 * and Tx conf queues are consumed on NAPI poll. 1406 */ 1407 wait_for_egress_fq_empty(priv); 1408 1409 do { 1410 dpni_disable(priv->mc_io, 0, priv->mc_token); 1411 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled); 1412 if (dpni_enabled) 1413 /* Allow the hardware some slack */ 1414 msleep(100); 1415 } while (dpni_enabled && --retries); 1416 if (!retries) { 1417 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n"); 1418 /* Must go on and disable NAPI nonetheless, so we don't crash at 1419 * the next "ifconfig up" 1420 */ 1421 } 1422 1423 wait_for_ingress_fq_empty(priv); 1424 disable_ch_napi(priv); 1425 1426 /* Empty the buffer pool */ 1427 drain_pool(priv); 1428 1429 return 0; 1430 } 1431 1432 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr) 1433 { 1434 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1435 struct device *dev = net_dev->dev.parent; 1436 int err; 1437 1438 err = eth_mac_addr(net_dev, addr); 1439 if (err < 0) { 1440 dev_err(dev, "eth_mac_addr() failed (%d)\n", err); 1441 return err; 1442 } 1443 1444 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token, 1445 net_dev->dev_addr); 1446 if (err) { 1447 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err); 1448 return err; 1449 } 1450 1451 return 0; 1452 } 1453 1454 /** Fill in counters maintained by the GPP driver. These may be different from 1455 * the hardware counters obtained by ethtool. 1456 */ 1457 static void dpaa2_eth_get_stats(struct net_device *net_dev, 1458 struct rtnl_link_stats64 *stats) 1459 { 1460 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1461 struct rtnl_link_stats64 *percpu_stats; 1462 u64 *cpustats; 1463 u64 *netstats = (u64 *)stats; 1464 int i, j; 1465 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64); 1466 1467 for_each_possible_cpu(i) { 1468 percpu_stats = per_cpu_ptr(priv->percpu_stats, i); 1469 cpustats = (u64 *)percpu_stats; 1470 for (j = 0; j < num; j++) 1471 netstats[j] += cpustats[j]; 1472 } 1473 } 1474 1475 /* Copy mac unicast addresses from @net_dev to @priv. 1476 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable. 1477 */ 1478 static void add_uc_hw_addr(const struct net_device *net_dev, 1479 struct dpaa2_eth_priv *priv) 1480 { 1481 struct netdev_hw_addr *ha; 1482 int err; 1483 1484 netdev_for_each_uc_addr(ha, net_dev) { 1485 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, 1486 ha->addr); 1487 if (err) 1488 netdev_warn(priv->net_dev, 1489 "Could not add ucast MAC %pM to the filtering table (err %d)\n", 1490 ha->addr, err); 1491 } 1492 } 1493 1494 /* Copy mac multicast addresses from @net_dev to @priv 1495 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable. 1496 */ 1497 static void add_mc_hw_addr(const struct net_device *net_dev, 1498 struct dpaa2_eth_priv *priv) 1499 { 1500 struct netdev_hw_addr *ha; 1501 int err; 1502 1503 netdev_for_each_mc_addr(ha, net_dev) { 1504 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, 1505 ha->addr); 1506 if (err) 1507 netdev_warn(priv->net_dev, 1508 "Could not add mcast MAC %pM to the filtering table (err %d)\n", 1509 ha->addr, err); 1510 } 1511 } 1512 1513 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev) 1514 { 1515 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1516 int uc_count = netdev_uc_count(net_dev); 1517 int mc_count = netdev_mc_count(net_dev); 1518 u8 max_mac = priv->dpni_attrs.mac_filter_entries; 1519 u32 options = priv->dpni_attrs.options; 1520 u16 mc_token = priv->mc_token; 1521 struct fsl_mc_io *mc_io = priv->mc_io; 1522 int err; 1523 1524 /* Basic sanity checks; these probably indicate a misconfiguration */ 1525 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0) 1526 netdev_info(net_dev, 1527 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n", 1528 max_mac); 1529 1530 /* Force promiscuous if the uc or mc counts exceed our capabilities. */ 1531 if (uc_count > max_mac) { 1532 netdev_info(net_dev, 1533 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n", 1534 uc_count, max_mac); 1535 goto force_promisc; 1536 } 1537 if (mc_count + uc_count > max_mac) { 1538 netdev_info(net_dev, 1539 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n", 1540 uc_count + mc_count, max_mac); 1541 goto force_mc_promisc; 1542 } 1543 1544 /* Adjust promisc settings due to flag combinations */ 1545 if (net_dev->flags & IFF_PROMISC) 1546 goto force_promisc; 1547 if (net_dev->flags & IFF_ALLMULTI) { 1548 /* First, rebuild unicast filtering table. This should be done 1549 * in promisc mode, in order to avoid frame loss while we 1550 * progressively add entries to the table. 1551 * We don't know whether we had been in promisc already, and 1552 * making an MC call to find out is expensive; so set uc promisc 1553 * nonetheless. 1554 */ 1555 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); 1556 if (err) 1557 netdev_warn(net_dev, "Can't set uc promisc\n"); 1558 1559 /* Actual uc table reconstruction. */ 1560 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0); 1561 if (err) 1562 netdev_warn(net_dev, "Can't clear uc filters\n"); 1563 add_uc_hw_addr(net_dev, priv); 1564 1565 /* Finally, clear uc promisc and set mc promisc as requested. */ 1566 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0); 1567 if (err) 1568 netdev_warn(net_dev, "Can't clear uc promisc\n"); 1569 goto force_mc_promisc; 1570 } 1571 1572 /* Neither unicast, nor multicast promisc will be on... eventually. 1573 * For now, rebuild mac filtering tables while forcing both of them on. 1574 */ 1575 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); 1576 if (err) 1577 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err); 1578 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1); 1579 if (err) 1580 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err); 1581 1582 /* Actual mac filtering tables reconstruction */ 1583 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1); 1584 if (err) 1585 netdev_warn(net_dev, "Can't clear mac filters\n"); 1586 add_mc_hw_addr(net_dev, priv); 1587 add_uc_hw_addr(net_dev, priv); 1588 1589 /* Now we can clear both ucast and mcast promisc, without risking 1590 * to drop legitimate frames anymore. 1591 */ 1592 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0); 1593 if (err) 1594 netdev_warn(net_dev, "Can't clear ucast promisc\n"); 1595 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0); 1596 if (err) 1597 netdev_warn(net_dev, "Can't clear mcast promisc\n"); 1598 1599 return; 1600 1601 force_promisc: 1602 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); 1603 if (err) 1604 netdev_warn(net_dev, "Can't set ucast promisc\n"); 1605 force_mc_promisc: 1606 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1); 1607 if (err) 1608 netdev_warn(net_dev, "Can't set mcast promisc\n"); 1609 } 1610 1611 static int dpaa2_eth_set_features(struct net_device *net_dev, 1612 netdev_features_t features) 1613 { 1614 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1615 netdev_features_t changed = features ^ net_dev->features; 1616 bool enable; 1617 int err; 1618 1619 if (changed & NETIF_F_RXCSUM) { 1620 enable = !!(features & NETIF_F_RXCSUM); 1621 err = set_rx_csum(priv, enable); 1622 if (err) 1623 return err; 1624 } 1625 1626 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) { 1627 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)); 1628 err = set_tx_csum(priv, enable); 1629 if (err) 1630 return err; 1631 } 1632 1633 return 0; 1634 } 1635 1636 static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 1637 { 1638 struct dpaa2_eth_priv *priv = netdev_priv(dev); 1639 struct hwtstamp_config config; 1640 1641 if (copy_from_user(&config, rq->ifr_data, sizeof(config))) 1642 return -EFAULT; 1643 1644 switch (config.tx_type) { 1645 case HWTSTAMP_TX_OFF: 1646 priv->tx_tstamp = false; 1647 break; 1648 case HWTSTAMP_TX_ON: 1649 priv->tx_tstamp = true; 1650 break; 1651 default: 1652 return -ERANGE; 1653 } 1654 1655 if (config.rx_filter == HWTSTAMP_FILTER_NONE) { 1656 priv->rx_tstamp = false; 1657 } else { 1658 priv->rx_tstamp = true; 1659 /* TS is set for all frame types, not only those requested */ 1660 config.rx_filter = HWTSTAMP_FILTER_ALL; 1661 } 1662 1663 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ? 1664 -EFAULT : 0; 1665 } 1666 1667 static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 1668 { 1669 if (cmd == SIOCSHWTSTAMP) 1670 return dpaa2_eth_ts_ioctl(dev, rq, cmd); 1671 1672 return -EINVAL; 1673 } 1674 1675 static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu) 1676 { 1677 int mfl, linear_mfl; 1678 1679 mfl = DPAA2_ETH_L2_MAX_FRM(mtu); 1680 linear_mfl = DPAA2_ETH_RX_BUF_SIZE - DPAA2_ETH_RX_HWA_SIZE - 1681 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM; 1682 1683 if (mfl > linear_mfl) { 1684 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n", 1685 linear_mfl - VLAN_ETH_HLEN); 1686 return false; 1687 } 1688 1689 return true; 1690 } 1691 1692 static int set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp) 1693 { 1694 int mfl, err; 1695 1696 /* We enforce a maximum Rx frame length based on MTU only if we have 1697 * an XDP program attached (in order to avoid Rx S/G frames). 1698 * Otherwise, we accept all incoming frames as long as they are not 1699 * larger than maximum size supported in hardware 1700 */ 1701 if (has_xdp) 1702 mfl = DPAA2_ETH_L2_MAX_FRM(mtu); 1703 else 1704 mfl = DPAA2_ETH_MFL; 1705 1706 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl); 1707 if (err) { 1708 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n"); 1709 return err; 1710 } 1711 1712 return 0; 1713 } 1714 1715 static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu) 1716 { 1717 struct dpaa2_eth_priv *priv = netdev_priv(dev); 1718 int err; 1719 1720 if (!priv->xdp_prog) 1721 goto out; 1722 1723 if (!xdp_mtu_valid(priv, new_mtu)) 1724 return -EINVAL; 1725 1726 err = set_rx_mfl(priv, new_mtu, true); 1727 if (err) 1728 return err; 1729 1730 out: 1731 dev->mtu = new_mtu; 1732 return 0; 1733 } 1734 1735 static int update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp) 1736 { 1737 struct dpni_buffer_layout buf_layout = {0}; 1738 int err; 1739 1740 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token, 1741 DPNI_QUEUE_RX, &buf_layout); 1742 if (err) { 1743 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n"); 1744 return err; 1745 } 1746 1747 /* Reserve extra headroom for XDP header size changes */ 1748 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) + 1749 (has_xdp ? XDP_PACKET_HEADROOM : 0); 1750 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM; 1751 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 1752 DPNI_QUEUE_RX, &buf_layout); 1753 if (err) { 1754 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n"); 1755 return err; 1756 } 1757 1758 return 0; 1759 } 1760 1761 static int setup_xdp(struct net_device *dev, struct bpf_prog *prog) 1762 { 1763 struct dpaa2_eth_priv *priv = netdev_priv(dev); 1764 struct dpaa2_eth_channel *ch; 1765 struct bpf_prog *old; 1766 bool up, need_update; 1767 int i, err; 1768 1769 if (prog && !xdp_mtu_valid(priv, dev->mtu)) 1770 return -EINVAL; 1771 1772 if (prog) { 1773 prog = bpf_prog_add(prog, priv->num_channels); 1774 if (IS_ERR(prog)) 1775 return PTR_ERR(prog); 1776 } 1777 1778 up = netif_running(dev); 1779 need_update = (!!priv->xdp_prog != !!prog); 1780 1781 if (up) 1782 dpaa2_eth_stop(dev); 1783 1784 /* While in xdp mode, enforce a maximum Rx frame size based on MTU. 1785 * Also, when switching between xdp/non-xdp modes we need to reconfigure 1786 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop, 1787 * so we are sure no old format buffers will be used from now on. 1788 */ 1789 if (need_update) { 1790 err = set_rx_mfl(priv, dev->mtu, !!prog); 1791 if (err) 1792 goto out_err; 1793 err = update_rx_buffer_headroom(priv, !!prog); 1794 if (err) 1795 goto out_err; 1796 } 1797 1798 old = xchg(&priv->xdp_prog, prog); 1799 if (old) 1800 bpf_prog_put(old); 1801 1802 for (i = 0; i < priv->num_channels; i++) { 1803 ch = priv->channel[i]; 1804 old = xchg(&ch->xdp.prog, prog); 1805 if (old) 1806 bpf_prog_put(old); 1807 } 1808 1809 if (up) { 1810 err = dpaa2_eth_open(dev); 1811 if (err) 1812 return err; 1813 } 1814 1815 return 0; 1816 1817 out_err: 1818 if (prog) 1819 bpf_prog_sub(prog, priv->num_channels); 1820 if (up) 1821 dpaa2_eth_open(dev); 1822 1823 return err; 1824 } 1825 1826 static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp) 1827 { 1828 struct dpaa2_eth_priv *priv = netdev_priv(dev); 1829 1830 switch (xdp->command) { 1831 case XDP_SETUP_PROG: 1832 return setup_xdp(dev, xdp->prog); 1833 case XDP_QUERY_PROG: 1834 xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0; 1835 break; 1836 default: 1837 return -EINVAL; 1838 } 1839 1840 return 0; 1841 } 1842 1843 static int dpaa2_eth_xdp_xmit_frame(struct net_device *net_dev, 1844 struct xdp_frame *xdpf) 1845 { 1846 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1847 struct device *dev = net_dev->dev.parent; 1848 struct rtnl_link_stats64 *percpu_stats; 1849 struct dpaa2_eth_drv_stats *percpu_extras; 1850 unsigned int needed_headroom; 1851 struct dpaa2_eth_swa *swa; 1852 struct dpaa2_eth_fq *fq; 1853 struct dpaa2_fd fd; 1854 void *buffer_start, *aligned_start; 1855 dma_addr_t addr; 1856 int err, i; 1857 1858 /* We require a minimum headroom to be able to transmit the frame. 1859 * Otherwise return an error and let the original net_device handle it 1860 */ 1861 needed_headroom = dpaa2_eth_needed_headroom(priv, NULL); 1862 if (xdpf->headroom < needed_headroom) 1863 return -EINVAL; 1864 1865 percpu_stats = this_cpu_ptr(priv->percpu_stats); 1866 percpu_extras = this_cpu_ptr(priv->percpu_extras); 1867 1868 /* Setup the FD fields */ 1869 memset(&fd, 0, sizeof(fd)); 1870 1871 /* Align FD address, if possible */ 1872 buffer_start = xdpf->data - needed_headroom; 1873 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN, 1874 DPAA2_ETH_TX_BUF_ALIGN); 1875 if (aligned_start >= xdpf->data - xdpf->headroom) 1876 buffer_start = aligned_start; 1877 1878 swa = (struct dpaa2_eth_swa *)buffer_start; 1879 /* fill in necessary fields here */ 1880 swa->type = DPAA2_ETH_SWA_XDP; 1881 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start; 1882 swa->xdp.xdpf = xdpf; 1883 1884 addr = dma_map_single(dev, buffer_start, 1885 swa->xdp.dma_size, 1886 DMA_BIDIRECTIONAL); 1887 if (unlikely(dma_mapping_error(dev, addr))) { 1888 percpu_stats->tx_dropped++; 1889 return -ENOMEM; 1890 } 1891 1892 dpaa2_fd_set_addr(&fd, addr); 1893 dpaa2_fd_set_offset(&fd, xdpf->data - buffer_start); 1894 dpaa2_fd_set_len(&fd, xdpf->len); 1895 dpaa2_fd_set_format(&fd, dpaa2_fd_single); 1896 dpaa2_fd_set_ctrl(&fd, FD_CTRL_PTA); 1897 1898 fq = &priv->fq[smp_processor_id() % dpaa2_eth_queue_count(priv)]; 1899 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) { 1900 err = priv->enqueue(priv, fq, &fd, 0); 1901 if (err != -EBUSY) 1902 break; 1903 } 1904 percpu_extras->tx_portal_busy += i; 1905 if (unlikely(err < 0)) { 1906 percpu_stats->tx_errors++; 1907 /* let the Rx device handle the cleanup */ 1908 return err; 1909 } 1910 1911 percpu_stats->tx_packets++; 1912 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fd); 1913 1914 return 0; 1915 } 1916 1917 static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n, 1918 struct xdp_frame **frames, u32 flags) 1919 { 1920 int drops = 0; 1921 int i, err; 1922 1923 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 1924 return -EINVAL; 1925 1926 if (!netif_running(net_dev)) 1927 return -ENETDOWN; 1928 1929 for (i = 0; i < n; i++) { 1930 struct xdp_frame *xdpf = frames[i]; 1931 1932 err = dpaa2_eth_xdp_xmit_frame(net_dev, xdpf); 1933 if (err) { 1934 xdp_return_frame_rx_napi(xdpf); 1935 drops++; 1936 } 1937 } 1938 1939 return n - drops; 1940 } 1941 1942 static int update_xps(struct dpaa2_eth_priv *priv) 1943 { 1944 struct net_device *net_dev = priv->net_dev; 1945 struct cpumask xps_mask; 1946 struct dpaa2_eth_fq *fq; 1947 int i, num_queues, netdev_queues; 1948 int err = 0; 1949 1950 num_queues = dpaa2_eth_queue_count(priv); 1951 netdev_queues = (net_dev->num_tc ? : 1) * num_queues; 1952 1953 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf 1954 * queues, so only process those 1955 */ 1956 for (i = 0; i < netdev_queues; i++) { 1957 fq = &priv->fq[i % num_queues]; 1958 1959 cpumask_clear(&xps_mask); 1960 cpumask_set_cpu(fq->target_cpu, &xps_mask); 1961 1962 err = netif_set_xps_queue(net_dev, &xps_mask, i); 1963 if (err) { 1964 netdev_warn_once(net_dev, "Error setting XPS queue\n"); 1965 break; 1966 } 1967 } 1968 1969 return err; 1970 } 1971 1972 static int dpaa2_eth_setup_tc(struct net_device *net_dev, 1973 enum tc_setup_type type, void *type_data) 1974 { 1975 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1976 struct tc_mqprio_qopt *mqprio = type_data; 1977 u8 num_tc, num_queues; 1978 int i; 1979 1980 if (type != TC_SETUP_QDISC_MQPRIO) 1981 return -EINVAL; 1982 1983 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 1984 num_queues = dpaa2_eth_queue_count(priv); 1985 num_tc = mqprio->num_tc; 1986 1987 if (num_tc == net_dev->num_tc) 1988 return 0; 1989 1990 if (num_tc > dpaa2_eth_tc_count(priv)) { 1991 netdev_err(net_dev, "Max %d traffic classes supported\n", 1992 dpaa2_eth_tc_count(priv)); 1993 return -EINVAL; 1994 } 1995 1996 if (!num_tc) { 1997 netdev_reset_tc(net_dev); 1998 netif_set_real_num_tx_queues(net_dev, num_queues); 1999 goto out; 2000 } 2001 2002 netdev_set_num_tc(net_dev, num_tc); 2003 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues); 2004 2005 for (i = 0; i < num_tc; i++) 2006 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues); 2007 2008 out: 2009 update_xps(priv); 2010 2011 return 0; 2012 } 2013 2014 static const struct net_device_ops dpaa2_eth_ops = { 2015 .ndo_open = dpaa2_eth_open, 2016 .ndo_start_xmit = dpaa2_eth_tx, 2017 .ndo_stop = dpaa2_eth_stop, 2018 .ndo_set_mac_address = dpaa2_eth_set_addr, 2019 .ndo_get_stats64 = dpaa2_eth_get_stats, 2020 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode, 2021 .ndo_set_features = dpaa2_eth_set_features, 2022 .ndo_do_ioctl = dpaa2_eth_ioctl, 2023 .ndo_change_mtu = dpaa2_eth_change_mtu, 2024 .ndo_bpf = dpaa2_eth_xdp, 2025 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit, 2026 .ndo_setup_tc = dpaa2_eth_setup_tc, 2027 }; 2028 2029 static void cdan_cb(struct dpaa2_io_notification_ctx *ctx) 2030 { 2031 struct dpaa2_eth_channel *ch; 2032 2033 ch = container_of(ctx, struct dpaa2_eth_channel, nctx); 2034 2035 /* Update NAPI statistics */ 2036 ch->stats.cdan++; 2037 2038 napi_schedule_irqoff(&ch->napi); 2039 } 2040 2041 /* Allocate and configure a DPCON object */ 2042 static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv) 2043 { 2044 struct fsl_mc_device *dpcon; 2045 struct device *dev = priv->net_dev->dev.parent; 2046 struct dpcon_attr attrs; 2047 int err; 2048 2049 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), 2050 FSL_MC_POOL_DPCON, &dpcon); 2051 if (err) { 2052 if (err == -ENXIO) 2053 err = -EPROBE_DEFER; 2054 else 2055 dev_info(dev, "Not enough DPCONs, will go on as-is\n"); 2056 return ERR_PTR(err); 2057 } 2058 2059 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle); 2060 if (err) { 2061 dev_err(dev, "dpcon_open() failed\n"); 2062 goto free; 2063 } 2064 2065 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle); 2066 if (err) { 2067 dev_err(dev, "dpcon_reset() failed\n"); 2068 goto close; 2069 } 2070 2071 err = dpcon_get_attributes(priv->mc_io, 0, dpcon->mc_handle, &attrs); 2072 if (err) { 2073 dev_err(dev, "dpcon_get_attributes() failed\n"); 2074 goto close; 2075 } 2076 2077 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle); 2078 if (err) { 2079 dev_err(dev, "dpcon_enable() failed\n"); 2080 goto close; 2081 } 2082 2083 return dpcon; 2084 2085 close: 2086 dpcon_close(priv->mc_io, 0, dpcon->mc_handle); 2087 free: 2088 fsl_mc_object_free(dpcon); 2089 2090 return NULL; 2091 } 2092 2093 static void free_dpcon(struct dpaa2_eth_priv *priv, 2094 struct fsl_mc_device *dpcon) 2095 { 2096 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle); 2097 dpcon_close(priv->mc_io, 0, dpcon->mc_handle); 2098 fsl_mc_object_free(dpcon); 2099 } 2100 2101 static struct dpaa2_eth_channel * 2102 alloc_channel(struct dpaa2_eth_priv *priv) 2103 { 2104 struct dpaa2_eth_channel *channel; 2105 struct dpcon_attr attr; 2106 struct device *dev = priv->net_dev->dev.parent; 2107 int err; 2108 2109 channel = kzalloc(sizeof(*channel), GFP_KERNEL); 2110 if (!channel) 2111 return NULL; 2112 2113 channel->dpcon = setup_dpcon(priv); 2114 if (IS_ERR_OR_NULL(channel->dpcon)) { 2115 err = PTR_ERR_OR_ZERO(channel->dpcon); 2116 goto err_setup; 2117 } 2118 2119 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle, 2120 &attr); 2121 if (err) { 2122 dev_err(dev, "dpcon_get_attributes() failed\n"); 2123 goto err_get_attr; 2124 } 2125 2126 channel->dpcon_id = attr.id; 2127 channel->ch_id = attr.qbman_ch_id; 2128 channel->priv = priv; 2129 2130 return channel; 2131 2132 err_get_attr: 2133 free_dpcon(priv, channel->dpcon); 2134 err_setup: 2135 kfree(channel); 2136 return ERR_PTR(err); 2137 } 2138 2139 static void free_channel(struct dpaa2_eth_priv *priv, 2140 struct dpaa2_eth_channel *channel) 2141 { 2142 free_dpcon(priv, channel->dpcon); 2143 kfree(channel); 2144 } 2145 2146 /* DPIO setup: allocate and configure QBMan channels, setup core affinity 2147 * and register data availability notifications 2148 */ 2149 static int setup_dpio(struct dpaa2_eth_priv *priv) 2150 { 2151 struct dpaa2_io_notification_ctx *nctx; 2152 struct dpaa2_eth_channel *channel; 2153 struct dpcon_notification_cfg dpcon_notif_cfg; 2154 struct device *dev = priv->net_dev->dev.parent; 2155 int i, err; 2156 2157 /* We want the ability to spread ingress traffic (RX, TX conf) to as 2158 * many cores as possible, so we need one channel for each core 2159 * (unless there's fewer queues than cores, in which case the extra 2160 * channels would be wasted). 2161 * Allocate one channel per core and register it to the core's 2162 * affine DPIO. If not enough channels are available for all cores 2163 * or if some cores don't have an affine DPIO, there will be no 2164 * ingress frame processing on those cores. 2165 */ 2166 cpumask_clear(&priv->dpio_cpumask); 2167 for_each_online_cpu(i) { 2168 /* Try to allocate a channel */ 2169 channel = alloc_channel(priv); 2170 if (IS_ERR_OR_NULL(channel)) { 2171 err = PTR_ERR_OR_ZERO(channel); 2172 if (err != -EPROBE_DEFER) 2173 dev_info(dev, 2174 "No affine channel for cpu %d and above\n", i); 2175 goto err_alloc_ch; 2176 } 2177 2178 priv->channel[priv->num_channels] = channel; 2179 2180 nctx = &channel->nctx; 2181 nctx->is_cdan = 1; 2182 nctx->cb = cdan_cb; 2183 nctx->id = channel->ch_id; 2184 nctx->desired_cpu = i; 2185 2186 /* Register the new context */ 2187 channel->dpio = dpaa2_io_service_select(i); 2188 err = dpaa2_io_service_register(channel->dpio, nctx, dev); 2189 if (err) { 2190 dev_dbg(dev, "No affine DPIO for cpu %d\n", i); 2191 /* If no affine DPIO for this core, there's probably 2192 * none available for next cores either. Signal we want 2193 * to retry later, in case the DPIO devices weren't 2194 * probed yet. 2195 */ 2196 err = -EPROBE_DEFER; 2197 goto err_service_reg; 2198 } 2199 2200 /* Register DPCON notification with MC */ 2201 dpcon_notif_cfg.dpio_id = nctx->dpio_id; 2202 dpcon_notif_cfg.priority = 0; 2203 dpcon_notif_cfg.user_ctx = nctx->qman64; 2204 err = dpcon_set_notification(priv->mc_io, 0, 2205 channel->dpcon->mc_handle, 2206 &dpcon_notif_cfg); 2207 if (err) { 2208 dev_err(dev, "dpcon_set_notification failed()\n"); 2209 goto err_set_cdan; 2210 } 2211 2212 /* If we managed to allocate a channel and also found an affine 2213 * DPIO for this core, add it to the final mask 2214 */ 2215 cpumask_set_cpu(i, &priv->dpio_cpumask); 2216 priv->num_channels++; 2217 2218 /* Stop if we already have enough channels to accommodate all 2219 * RX and TX conf queues 2220 */ 2221 if (priv->num_channels == priv->dpni_attrs.num_queues) 2222 break; 2223 } 2224 2225 return 0; 2226 2227 err_set_cdan: 2228 dpaa2_io_service_deregister(channel->dpio, nctx, dev); 2229 err_service_reg: 2230 free_channel(priv, channel); 2231 err_alloc_ch: 2232 if (err == -EPROBE_DEFER) 2233 return err; 2234 2235 if (cpumask_empty(&priv->dpio_cpumask)) { 2236 dev_err(dev, "No cpu with an affine DPIO/DPCON\n"); 2237 return -ENODEV; 2238 } 2239 2240 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n", 2241 cpumask_pr_args(&priv->dpio_cpumask)); 2242 2243 return 0; 2244 } 2245 2246 static void free_dpio(struct dpaa2_eth_priv *priv) 2247 { 2248 struct device *dev = priv->net_dev->dev.parent; 2249 struct dpaa2_eth_channel *ch; 2250 int i; 2251 2252 /* deregister CDAN notifications and free channels */ 2253 for (i = 0; i < priv->num_channels; i++) { 2254 ch = priv->channel[i]; 2255 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev); 2256 free_channel(priv, ch); 2257 } 2258 } 2259 2260 static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv, 2261 int cpu) 2262 { 2263 struct device *dev = priv->net_dev->dev.parent; 2264 int i; 2265 2266 for (i = 0; i < priv->num_channels; i++) 2267 if (priv->channel[i]->nctx.desired_cpu == cpu) 2268 return priv->channel[i]; 2269 2270 /* We should never get here. Issue a warning and return 2271 * the first channel, because it's still better than nothing 2272 */ 2273 dev_warn(dev, "No affine channel found for cpu %d\n", cpu); 2274 2275 return priv->channel[0]; 2276 } 2277 2278 static void set_fq_affinity(struct dpaa2_eth_priv *priv) 2279 { 2280 struct device *dev = priv->net_dev->dev.parent; 2281 struct dpaa2_eth_fq *fq; 2282 int rx_cpu, txc_cpu; 2283 int i; 2284 2285 /* For each FQ, pick one channel/CPU to deliver frames to. 2286 * This may well change at runtime, either through irqbalance or 2287 * through direct user intervention. 2288 */ 2289 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask); 2290 2291 for (i = 0; i < priv->num_fqs; i++) { 2292 fq = &priv->fq[i]; 2293 switch (fq->type) { 2294 case DPAA2_RX_FQ: 2295 fq->target_cpu = rx_cpu; 2296 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask); 2297 if (rx_cpu >= nr_cpu_ids) 2298 rx_cpu = cpumask_first(&priv->dpio_cpumask); 2299 break; 2300 case DPAA2_TX_CONF_FQ: 2301 fq->target_cpu = txc_cpu; 2302 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask); 2303 if (txc_cpu >= nr_cpu_ids) 2304 txc_cpu = cpumask_first(&priv->dpio_cpumask); 2305 break; 2306 default: 2307 dev_err(dev, "Unknown FQ type: %d\n", fq->type); 2308 } 2309 fq->channel = get_affine_channel(priv, fq->target_cpu); 2310 } 2311 2312 update_xps(priv); 2313 } 2314 2315 static void setup_fqs(struct dpaa2_eth_priv *priv) 2316 { 2317 int i; 2318 2319 /* We have one TxConf FQ per Tx flow. 2320 * The number of Tx and Rx queues is the same. 2321 * Tx queues come first in the fq array. 2322 */ 2323 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) { 2324 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ; 2325 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf; 2326 priv->fq[priv->num_fqs++].flowid = (u16)i; 2327 } 2328 2329 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) { 2330 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ; 2331 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx; 2332 priv->fq[priv->num_fqs++].flowid = (u16)i; 2333 } 2334 2335 /* For each FQ, decide on which core to process incoming frames */ 2336 set_fq_affinity(priv); 2337 } 2338 2339 /* Allocate and configure one buffer pool for each interface */ 2340 static int setup_dpbp(struct dpaa2_eth_priv *priv) 2341 { 2342 int err; 2343 struct fsl_mc_device *dpbp_dev; 2344 struct device *dev = priv->net_dev->dev.parent; 2345 struct dpbp_attr dpbp_attrs; 2346 2347 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP, 2348 &dpbp_dev); 2349 if (err) { 2350 if (err == -ENXIO) 2351 err = -EPROBE_DEFER; 2352 else 2353 dev_err(dev, "DPBP device allocation failed\n"); 2354 return err; 2355 } 2356 2357 priv->dpbp_dev = dpbp_dev; 2358 2359 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id, 2360 &dpbp_dev->mc_handle); 2361 if (err) { 2362 dev_err(dev, "dpbp_open() failed\n"); 2363 goto err_open; 2364 } 2365 2366 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle); 2367 if (err) { 2368 dev_err(dev, "dpbp_reset() failed\n"); 2369 goto err_reset; 2370 } 2371 2372 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle); 2373 if (err) { 2374 dev_err(dev, "dpbp_enable() failed\n"); 2375 goto err_enable; 2376 } 2377 2378 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle, 2379 &dpbp_attrs); 2380 if (err) { 2381 dev_err(dev, "dpbp_get_attributes() failed\n"); 2382 goto err_get_attr; 2383 } 2384 priv->bpid = dpbp_attrs.bpid; 2385 2386 return 0; 2387 2388 err_get_attr: 2389 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle); 2390 err_enable: 2391 err_reset: 2392 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle); 2393 err_open: 2394 fsl_mc_object_free(dpbp_dev); 2395 2396 return err; 2397 } 2398 2399 static void free_dpbp(struct dpaa2_eth_priv *priv) 2400 { 2401 drain_pool(priv); 2402 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle); 2403 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle); 2404 fsl_mc_object_free(priv->dpbp_dev); 2405 } 2406 2407 static int set_buffer_layout(struct dpaa2_eth_priv *priv) 2408 { 2409 struct device *dev = priv->net_dev->dev.parent; 2410 struct dpni_buffer_layout buf_layout = {0}; 2411 u16 rx_buf_align; 2412 int err; 2413 2414 /* We need to check for WRIOP version 1.0.0, but depending on the MC 2415 * version, this number is not always provided correctly on rev1. 2416 * We need to check for both alternatives in this situation. 2417 */ 2418 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) || 2419 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0)) 2420 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1; 2421 else 2422 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN; 2423 2424 /* tx buffer */ 2425 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE; 2426 buf_layout.pass_timestamp = true; 2427 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE | 2428 DPNI_BUF_LAYOUT_OPT_TIMESTAMP; 2429 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 2430 DPNI_QUEUE_TX, &buf_layout); 2431 if (err) { 2432 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n"); 2433 return err; 2434 } 2435 2436 /* tx-confirm buffer */ 2437 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP; 2438 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 2439 DPNI_QUEUE_TX_CONFIRM, &buf_layout); 2440 if (err) { 2441 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n"); 2442 return err; 2443 } 2444 2445 /* Now that we've set our tx buffer layout, retrieve the minimum 2446 * required tx data offset. 2447 */ 2448 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token, 2449 &priv->tx_data_offset); 2450 if (err) { 2451 dev_err(dev, "dpni_get_tx_data_offset() failed\n"); 2452 return err; 2453 } 2454 2455 if ((priv->tx_data_offset % 64) != 0) 2456 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n", 2457 priv->tx_data_offset); 2458 2459 /* rx buffer */ 2460 buf_layout.pass_frame_status = true; 2461 buf_layout.pass_parser_result = true; 2462 buf_layout.data_align = rx_buf_align; 2463 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv); 2464 buf_layout.private_data_size = 0; 2465 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT | 2466 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS | 2467 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN | 2468 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM | 2469 DPNI_BUF_LAYOUT_OPT_TIMESTAMP; 2470 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 2471 DPNI_QUEUE_RX, &buf_layout); 2472 if (err) { 2473 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n"); 2474 return err; 2475 } 2476 2477 return 0; 2478 } 2479 2480 #define DPNI_ENQUEUE_FQID_VER_MAJOR 7 2481 #define DPNI_ENQUEUE_FQID_VER_MINOR 9 2482 2483 static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv, 2484 struct dpaa2_eth_fq *fq, 2485 struct dpaa2_fd *fd, u8 prio) 2486 { 2487 return dpaa2_io_service_enqueue_qd(fq->channel->dpio, 2488 priv->tx_qdid, prio, 2489 fq->tx_qdbin, fd); 2490 } 2491 2492 static inline int dpaa2_eth_enqueue_fq(struct dpaa2_eth_priv *priv, 2493 struct dpaa2_eth_fq *fq, 2494 struct dpaa2_fd *fd, u8 prio) 2495 { 2496 return dpaa2_io_service_enqueue_fq(fq->channel->dpio, 2497 fq->tx_fqid[prio], fd); 2498 } 2499 2500 static void set_enqueue_mode(struct dpaa2_eth_priv *priv) 2501 { 2502 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR, 2503 DPNI_ENQUEUE_FQID_VER_MINOR) < 0) 2504 priv->enqueue = dpaa2_eth_enqueue_qd; 2505 else 2506 priv->enqueue = dpaa2_eth_enqueue_fq; 2507 } 2508 2509 static int set_pause(struct dpaa2_eth_priv *priv) 2510 { 2511 struct device *dev = priv->net_dev->dev.parent; 2512 struct dpni_link_cfg link_cfg = {0}; 2513 int err; 2514 2515 /* Get the default link options so we don't override other flags */ 2516 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg); 2517 if (err) { 2518 dev_err(dev, "dpni_get_link_cfg() failed\n"); 2519 return err; 2520 } 2521 2522 /* By default, enable both Rx and Tx pause frames */ 2523 link_cfg.options |= DPNI_LINK_OPT_PAUSE; 2524 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE; 2525 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg); 2526 if (err) { 2527 dev_err(dev, "dpni_set_link_cfg() failed\n"); 2528 return err; 2529 } 2530 2531 priv->link_state.options = link_cfg.options; 2532 2533 return 0; 2534 } 2535 2536 /* Configure the DPNI object this interface is associated with */ 2537 static int setup_dpni(struct fsl_mc_device *ls_dev) 2538 { 2539 struct device *dev = &ls_dev->dev; 2540 struct dpaa2_eth_priv *priv; 2541 struct net_device *net_dev; 2542 int err; 2543 2544 net_dev = dev_get_drvdata(dev); 2545 priv = netdev_priv(net_dev); 2546 2547 /* get a handle for the DPNI object */ 2548 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token); 2549 if (err) { 2550 dev_err(dev, "dpni_open() failed\n"); 2551 return err; 2552 } 2553 2554 /* Check if we can work with this DPNI object */ 2555 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major, 2556 &priv->dpni_ver_minor); 2557 if (err) { 2558 dev_err(dev, "dpni_get_api_version() failed\n"); 2559 goto close; 2560 } 2561 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) { 2562 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n", 2563 priv->dpni_ver_major, priv->dpni_ver_minor, 2564 DPNI_VER_MAJOR, DPNI_VER_MINOR); 2565 err = -ENOTSUPP; 2566 goto close; 2567 } 2568 2569 ls_dev->mc_io = priv->mc_io; 2570 ls_dev->mc_handle = priv->mc_token; 2571 2572 err = dpni_reset(priv->mc_io, 0, priv->mc_token); 2573 if (err) { 2574 dev_err(dev, "dpni_reset() failed\n"); 2575 goto close; 2576 } 2577 2578 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token, 2579 &priv->dpni_attrs); 2580 if (err) { 2581 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err); 2582 goto close; 2583 } 2584 2585 err = set_buffer_layout(priv); 2586 if (err) 2587 goto close; 2588 2589 set_enqueue_mode(priv); 2590 2591 /* Enable pause frame support */ 2592 if (dpaa2_eth_has_pause_support(priv)) { 2593 err = set_pause(priv); 2594 if (err) 2595 goto close; 2596 } 2597 2598 priv->cls_rules = devm_kzalloc(dev, sizeof(struct dpaa2_eth_cls_rule) * 2599 dpaa2_eth_fs_count(priv), GFP_KERNEL); 2600 if (!priv->cls_rules) 2601 goto close; 2602 2603 return 0; 2604 2605 close: 2606 dpni_close(priv->mc_io, 0, priv->mc_token); 2607 2608 return err; 2609 } 2610 2611 static void free_dpni(struct dpaa2_eth_priv *priv) 2612 { 2613 int err; 2614 2615 err = dpni_reset(priv->mc_io, 0, priv->mc_token); 2616 if (err) 2617 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n", 2618 err); 2619 2620 dpni_close(priv->mc_io, 0, priv->mc_token); 2621 } 2622 2623 static int setup_rx_flow(struct dpaa2_eth_priv *priv, 2624 struct dpaa2_eth_fq *fq) 2625 { 2626 struct device *dev = priv->net_dev->dev.parent; 2627 struct dpni_queue queue; 2628 struct dpni_queue_id qid; 2629 int err; 2630 2631 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 2632 DPNI_QUEUE_RX, 0, fq->flowid, &queue, &qid); 2633 if (err) { 2634 dev_err(dev, "dpni_get_queue(RX) failed\n"); 2635 return err; 2636 } 2637 2638 fq->fqid = qid.fqid; 2639 2640 queue.destination.id = fq->channel->dpcon_id; 2641 queue.destination.type = DPNI_DEST_DPCON; 2642 queue.destination.priority = 1; 2643 queue.user_context = (u64)(uintptr_t)fq; 2644 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token, 2645 DPNI_QUEUE_RX, 0, fq->flowid, 2646 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST, 2647 &queue); 2648 if (err) { 2649 dev_err(dev, "dpni_set_queue(RX) failed\n"); 2650 return err; 2651 } 2652 2653 /* xdp_rxq setup */ 2654 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev, 2655 fq->flowid); 2656 if (err) { 2657 dev_err(dev, "xdp_rxq_info_reg failed\n"); 2658 return err; 2659 } 2660 2661 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq, 2662 MEM_TYPE_PAGE_ORDER0, NULL); 2663 if (err) { 2664 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n"); 2665 return err; 2666 } 2667 2668 return 0; 2669 } 2670 2671 static int setup_tx_flow(struct dpaa2_eth_priv *priv, 2672 struct dpaa2_eth_fq *fq) 2673 { 2674 struct device *dev = priv->net_dev->dev.parent; 2675 struct dpni_queue queue; 2676 struct dpni_queue_id qid; 2677 int i, err; 2678 2679 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) { 2680 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 2681 DPNI_QUEUE_TX, i, fq->flowid, 2682 &queue, &qid); 2683 if (err) { 2684 dev_err(dev, "dpni_get_queue(TX) failed\n"); 2685 return err; 2686 } 2687 fq->tx_fqid[i] = qid.fqid; 2688 } 2689 2690 /* All Tx queues belonging to the same flowid have the same qdbin */ 2691 fq->tx_qdbin = qid.qdbin; 2692 2693 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 2694 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid, 2695 &queue, &qid); 2696 if (err) { 2697 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n"); 2698 return err; 2699 } 2700 2701 fq->fqid = qid.fqid; 2702 2703 queue.destination.id = fq->channel->dpcon_id; 2704 queue.destination.type = DPNI_DEST_DPCON; 2705 queue.destination.priority = 0; 2706 queue.user_context = (u64)(uintptr_t)fq; 2707 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token, 2708 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid, 2709 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST, 2710 &queue); 2711 if (err) { 2712 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n"); 2713 return err; 2714 } 2715 2716 return 0; 2717 } 2718 2719 /* Supported header fields for Rx hash distribution key */ 2720 static const struct dpaa2_eth_dist_fields dist_fields[] = { 2721 { 2722 /* L2 header */ 2723 .rxnfc_field = RXH_L2DA, 2724 .cls_prot = NET_PROT_ETH, 2725 .cls_field = NH_FLD_ETH_DA, 2726 .id = DPAA2_ETH_DIST_ETHDST, 2727 .size = 6, 2728 }, { 2729 .cls_prot = NET_PROT_ETH, 2730 .cls_field = NH_FLD_ETH_SA, 2731 .id = DPAA2_ETH_DIST_ETHSRC, 2732 .size = 6, 2733 }, { 2734 /* This is the last ethertype field parsed: 2735 * depending on frame format, it can be the MAC ethertype 2736 * or the VLAN etype. 2737 */ 2738 .cls_prot = NET_PROT_ETH, 2739 .cls_field = NH_FLD_ETH_TYPE, 2740 .id = DPAA2_ETH_DIST_ETHTYPE, 2741 .size = 2, 2742 }, { 2743 /* VLAN header */ 2744 .rxnfc_field = RXH_VLAN, 2745 .cls_prot = NET_PROT_VLAN, 2746 .cls_field = NH_FLD_VLAN_TCI, 2747 .id = DPAA2_ETH_DIST_VLAN, 2748 .size = 2, 2749 }, { 2750 /* IP header */ 2751 .rxnfc_field = RXH_IP_SRC, 2752 .cls_prot = NET_PROT_IP, 2753 .cls_field = NH_FLD_IP_SRC, 2754 .id = DPAA2_ETH_DIST_IPSRC, 2755 .size = 4, 2756 }, { 2757 .rxnfc_field = RXH_IP_DST, 2758 .cls_prot = NET_PROT_IP, 2759 .cls_field = NH_FLD_IP_DST, 2760 .id = DPAA2_ETH_DIST_IPDST, 2761 .size = 4, 2762 }, { 2763 .rxnfc_field = RXH_L3_PROTO, 2764 .cls_prot = NET_PROT_IP, 2765 .cls_field = NH_FLD_IP_PROTO, 2766 .id = DPAA2_ETH_DIST_IPPROTO, 2767 .size = 1, 2768 }, { 2769 /* Using UDP ports, this is functionally equivalent to raw 2770 * byte pairs from L4 header. 2771 */ 2772 .rxnfc_field = RXH_L4_B_0_1, 2773 .cls_prot = NET_PROT_UDP, 2774 .cls_field = NH_FLD_UDP_PORT_SRC, 2775 .id = DPAA2_ETH_DIST_L4SRC, 2776 .size = 2, 2777 }, { 2778 .rxnfc_field = RXH_L4_B_2_3, 2779 .cls_prot = NET_PROT_UDP, 2780 .cls_field = NH_FLD_UDP_PORT_DST, 2781 .id = DPAA2_ETH_DIST_L4DST, 2782 .size = 2, 2783 }, 2784 }; 2785 2786 /* Configure the Rx hash key using the legacy API */ 2787 static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key) 2788 { 2789 struct device *dev = priv->net_dev->dev.parent; 2790 struct dpni_rx_tc_dist_cfg dist_cfg; 2791 int err; 2792 2793 memset(&dist_cfg, 0, sizeof(dist_cfg)); 2794 2795 dist_cfg.key_cfg_iova = key; 2796 dist_cfg.dist_size = dpaa2_eth_queue_count(priv); 2797 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH; 2798 2799 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg); 2800 if (err) 2801 dev_err(dev, "dpni_set_rx_tc_dist failed\n"); 2802 2803 return err; 2804 } 2805 2806 /* Configure the Rx hash key using the new API */ 2807 static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key) 2808 { 2809 struct device *dev = priv->net_dev->dev.parent; 2810 struct dpni_rx_dist_cfg dist_cfg; 2811 int err; 2812 2813 memset(&dist_cfg, 0, sizeof(dist_cfg)); 2814 2815 dist_cfg.key_cfg_iova = key; 2816 dist_cfg.dist_size = dpaa2_eth_queue_count(priv); 2817 dist_cfg.enable = 1; 2818 2819 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg); 2820 if (err) 2821 dev_err(dev, "dpni_set_rx_hash_dist failed\n"); 2822 2823 return err; 2824 } 2825 2826 /* Configure the Rx flow classification key */ 2827 static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key) 2828 { 2829 struct device *dev = priv->net_dev->dev.parent; 2830 struct dpni_rx_dist_cfg dist_cfg; 2831 int err; 2832 2833 memset(&dist_cfg, 0, sizeof(dist_cfg)); 2834 2835 dist_cfg.key_cfg_iova = key; 2836 dist_cfg.dist_size = dpaa2_eth_queue_count(priv); 2837 dist_cfg.enable = 1; 2838 2839 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg); 2840 if (err) 2841 dev_err(dev, "dpni_set_rx_fs_dist failed\n"); 2842 2843 return err; 2844 } 2845 2846 /* Size of the Rx flow classification key */ 2847 int dpaa2_eth_cls_key_size(u64 fields) 2848 { 2849 int i, size = 0; 2850 2851 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { 2852 if (!(fields & dist_fields[i].id)) 2853 continue; 2854 size += dist_fields[i].size; 2855 } 2856 2857 return size; 2858 } 2859 2860 /* Offset of header field in Rx classification key */ 2861 int dpaa2_eth_cls_fld_off(int prot, int field) 2862 { 2863 int i, off = 0; 2864 2865 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { 2866 if (dist_fields[i].cls_prot == prot && 2867 dist_fields[i].cls_field == field) 2868 return off; 2869 off += dist_fields[i].size; 2870 } 2871 2872 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n"); 2873 return 0; 2874 } 2875 2876 /* Prune unused fields from the classification rule. 2877 * Used when masking is not supported 2878 */ 2879 void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields) 2880 { 2881 int off = 0, new_off = 0; 2882 int i, size; 2883 2884 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { 2885 size = dist_fields[i].size; 2886 if (dist_fields[i].id & fields) { 2887 memcpy(key_mem + new_off, key_mem + off, size); 2888 new_off += size; 2889 } 2890 off += size; 2891 } 2892 } 2893 2894 /* Set Rx distribution (hash or flow classification) key 2895 * flags is a combination of RXH_ bits 2896 */ 2897 static int dpaa2_eth_set_dist_key(struct net_device *net_dev, 2898 enum dpaa2_eth_rx_dist type, u64 flags) 2899 { 2900 struct device *dev = net_dev->dev.parent; 2901 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2902 struct dpkg_profile_cfg cls_cfg; 2903 u32 rx_hash_fields = 0; 2904 dma_addr_t key_iova; 2905 u8 *dma_mem; 2906 int i; 2907 int err = 0; 2908 2909 memset(&cls_cfg, 0, sizeof(cls_cfg)); 2910 2911 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { 2912 struct dpkg_extract *key = 2913 &cls_cfg.extracts[cls_cfg.num_extracts]; 2914 2915 /* For both Rx hashing and classification keys 2916 * we set only the selected fields. 2917 */ 2918 if (!(flags & dist_fields[i].id)) 2919 continue; 2920 if (type == DPAA2_ETH_RX_DIST_HASH) 2921 rx_hash_fields |= dist_fields[i].rxnfc_field; 2922 2923 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) { 2924 dev_err(dev, "error adding key extraction rule, too many rules?\n"); 2925 return -E2BIG; 2926 } 2927 2928 key->type = DPKG_EXTRACT_FROM_HDR; 2929 key->extract.from_hdr.prot = dist_fields[i].cls_prot; 2930 key->extract.from_hdr.type = DPKG_FULL_FIELD; 2931 key->extract.from_hdr.field = dist_fields[i].cls_field; 2932 cls_cfg.num_extracts++; 2933 } 2934 2935 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL); 2936 if (!dma_mem) 2937 return -ENOMEM; 2938 2939 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem); 2940 if (err) { 2941 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err); 2942 goto free_key; 2943 } 2944 2945 /* Prepare for setting the rx dist */ 2946 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE, 2947 DMA_TO_DEVICE); 2948 if (dma_mapping_error(dev, key_iova)) { 2949 dev_err(dev, "DMA mapping failed\n"); 2950 err = -ENOMEM; 2951 goto free_key; 2952 } 2953 2954 if (type == DPAA2_ETH_RX_DIST_HASH) { 2955 if (dpaa2_eth_has_legacy_dist(priv)) 2956 err = config_legacy_hash_key(priv, key_iova); 2957 else 2958 err = config_hash_key(priv, key_iova); 2959 } else { 2960 err = config_cls_key(priv, key_iova); 2961 } 2962 2963 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE, 2964 DMA_TO_DEVICE); 2965 if (!err && type == DPAA2_ETH_RX_DIST_HASH) 2966 priv->rx_hash_fields = rx_hash_fields; 2967 2968 free_key: 2969 kfree(dma_mem); 2970 return err; 2971 } 2972 2973 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags) 2974 { 2975 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2976 u64 key = 0; 2977 int i; 2978 2979 if (!dpaa2_eth_hash_enabled(priv)) 2980 return -EOPNOTSUPP; 2981 2982 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) 2983 if (dist_fields[i].rxnfc_field & flags) 2984 key |= dist_fields[i].id; 2985 2986 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key); 2987 } 2988 2989 int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags) 2990 { 2991 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags); 2992 } 2993 2994 static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv) 2995 { 2996 struct device *dev = priv->net_dev->dev.parent; 2997 int err; 2998 2999 /* Check if we actually support Rx flow classification */ 3000 if (dpaa2_eth_has_legacy_dist(priv)) { 3001 dev_dbg(dev, "Rx cls not supported by current MC version\n"); 3002 return -EOPNOTSUPP; 3003 } 3004 3005 if (!dpaa2_eth_fs_enabled(priv)) { 3006 dev_dbg(dev, "Rx cls disabled in DPNI options\n"); 3007 return -EOPNOTSUPP; 3008 } 3009 3010 if (!dpaa2_eth_hash_enabled(priv)) { 3011 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n"); 3012 return -EOPNOTSUPP; 3013 } 3014 3015 /* If there is no support for masking in the classification table, 3016 * we don't set a default key, as it will depend on the rules 3017 * added by the user at runtime. 3018 */ 3019 if (!dpaa2_eth_fs_mask_enabled(priv)) 3020 goto out; 3021 3022 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL); 3023 if (err) 3024 return err; 3025 3026 out: 3027 priv->rx_cls_enabled = 1; 3028 3029 return 0; 3030 } 3031 3032 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs, 3033 * frame queues and channels 3034 */ 3035 static int bind_dpni(struct dpaa2_eth_priv *priv) 3036 { 3037 struct net_device *net_dev = priv->net_dev; 3038 struct device *dev = net_dev->dev.parent; 3039 struct dpni_pools_cfg pools_params; 3040 struct dpni_error_cfg err_cfg; 3041 int err = 0; 3042 int i; 3043 3044 pools_params.num_dpbp = 1; 3045 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id; 3046 pools_params.pools[0].backup_pool = 0; 3047 pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE; 3048 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params); 3049 if (err) { 3050 dev_err(dev, "dpni_set_pools() failed\n"); 3051 return err; 3052 } 3053 3054 /* have the interface implicitly distribute traffic based on 3055 * the default hash key 3056 */ 3057 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT); 3058 if (err && err != -EOPNOTSUPP) 3059 dev_err(dev, "Failed to configure hashing\n"); 3060 3061 /* Configure the flow classification key; it includes all 3062 * supported header fields and cannot be modified at runtime 3063 */ 3064 err = dpaa2_eth_set_default_cls(priv); 3065 if (err && err != -EOPNOTSUPP) 3066 dev_err(dev, "Failed to configure Rx classification key\n"); 3067 3068 /* Configure handling of error frames */ 3069 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK; 3070 err_cfg.set_frame_annotation = 1; 3071 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD; 3072 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token, 3073 &err_cfg); 3074 if (err) { 3075 dev_err(dev, "dpni_set_errors_behavior failed\n"); 3076 return err; 3077 } 3078 3079 /* Configure Rx and Tx conf queues to generate CDANs */ 3080 for (i = 0; i < priv->num_fqs; i++) { 3081 switch (priv->fq[i].type) { 3082 case DPAA2_RX_FQ: 3083 err = setup_rx_flow(priv, &priv->fq[i]); 3084 break; 3085 case DPAA2_TX_CONF_FQ: 3086 err = setup_tx_flow(priv, &priv->fq[i]); 3087 break; 3088 default: 3089 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type); 3090 return -EINVAL; 3091 } 3092 if (err) 3093 return err; 3094 } 3095 3096 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token, 3097 DPNI_QUEUE_TX, &priv->tx_qdid); 3098 if (err) { 3099 dev_err(dev, "dpni_get_qdid() failed\n"); 3100 return err; 3101 } 3102 3103 return 0; 3104 } 3105 3106 /* Allocate rings for storing incoming frame descriptors */ 3107 static int alloc_rings(struct dpaa2_eth_priv *priv) 3108 { 3109 struct net_device *net_dev = priv->net_dev; 3110 struct device *dev = net_dev->dev.parent; 3111 int i; 3112 3113 for (i = 0; i < priv->num_channels; i++) { 3114 priv->channel[i]->store = 3115 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev); 3116 if (!priv->channel[i]->store) { 3117 netdev_err(net_dev, "dpaa2_io_store_create() failed\n"); 3118 goto err_ring; 3119 } 3120 } 3121 3122 return 0; 3123 3124 err_ring: 3125 for (i = 0; i < priv->num_channels; i++) { 3126 if (!priv->channel[i]->store) 3127 break; 3128 dpaa2_io_store_destroy(priv->channel[i]->store); 3129 } 3130 3131 return -ENOMEM; 3132 } 3133 3134 static void free_rings(struct dpaa2_eth_priv *priv) 3135 { 3136 int i; 3137 3138 for (i = 0; i < priv->num_channels; i++) 3139 dpaa2_io_store_destroy(priv->channel[i]->store); 3140 } 3141 3142 static int set_mac_addr(struct dpaa2_eth_priv *priv) 3143 { 3144 struct net_device *net_dev = priv->net_dev; 3145 struct device *dev = net_dev->dev.parent; 3146 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN]; 3147 int err; 3148 3149 /* Get firmware address, if any */ 3150 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr); 3151 if (err) { 3152 dev_err(dev, "dpni_get_port_mac_addr() failed\n"); 3153 return err; 3154 } 3155 3156 /* Get DPNI attributes address, if any */ 3157 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token, 3158 dpni_mac_addr); 3159 if (err) { 3160 dev_err(dev, "dpni_get_primary_mac_addr() failed\n"); 3161 return err; 3162 } 3163 3164 /* First check if firmware has any address configured by bootloader */ 3165 if (!is_zero_ether_addr(mac_addr)) { 3166 /* If the DPMAC addr != DPNI addr, update it */ 3167 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) { 3168 err = dpni_set_primary_mac_addr(priv->mc_io, 0, 3169 priv->mc_token, 3170 mac_addr); 3171 if (err) { 3172 dev_err(dev, "dpni_set_primary_mac_addr() failed\n"); 3173 return err; 3174 } 3175 } 3176 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len); 3177 } else if (is_zero_ether_addr(dpni_mac_addr)) { 3178 /* No MAC address configured, fill in net_dev->dev_addr 3179 * with a random one 3180 */ 3181 eth_hw_addr_random(net_dev); 3182 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n"); 3183 3184 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token, 3185 net_dev->dev_addr); 3186 if (err) { 3187 dev_err(dev, "dpni_set_primary_mac_addr() failed\n"); 3188 return err; 3189 } 3190 3191 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all 3192 * practical purposes, this will be our "permanent" mac address, 3193 * at least until the next reboot. This move will also permit 3194 * register_netdevice() to properly fill up net_dev->perm_addr. 3195 */ 3196 net_dev->addr_assign_type = NET_ADDR_PERM; 3197 } else { 3198 /* NET_ADDR_PERM is default, all we have to do is 3199 * fill in the device addr. 3200 */ 3201 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len); 3202 } 3203 3204 return 0; 3205 } 3206 3207 static int netdev_init(struct net_device *net_dev) 3208 { 3209 struct device *dev = net_dev->dev.parent; 3210 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 3211 u32 options = priv->dpni_attrs.options; 3212 u64 supported = 0, not_supported = 0; 3213 u8 bcast_addr[ETH_ALEN]; 3214 u8 num_queues; 3215 int err; 3216 3217 net_dev->netdev_ops = &dpaa2_eth_ops; 3218 net_dev->ethtool_ops = &dpaa2_ethtool_ops; 3219 3220 err = set_mac_addr(priv); 3221 if (err) 3222 return err; 3223 3224 /* Explicitly add the broadcast address to the MAC filtering table */ 3225 eth_broadcast_addr(bcast_addr); 3226 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr); 3227 if (err) { 3228 dev_err(dev, "dpni_add_mac_addr() failed\n"); 3229 return err; 3230 } 3231 3232 /* Set MTU upper limit; lower limit is 68B (default value) */ 3233 net_dev->max_mtu = DPAA2_ETH_MAX_MTU; 3234 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, 3235 DPAA2_ETH_MFL); 3236 if (err) { 3237 dev_err(dev, "dpni_set_max_frame_length() failed\n"); 3238 return err; 3239 } 3240 3241 /* Set actual number of queues in the net device */ 3242 num_queues = dpaa2_eth_queue_count(priv); 3243 err = netif_set_real_num_tx_queues(net_dev, num_queues); 3244 if (err) { 3245 dev_err(dev, "netif_set_real_num_tx_queues() failed\n"); 3246 return err; 3247 } 3248 err = netif_set_real_num_rx_queues(net_dev, num_queues); 3249 if (err) { 3250 dev_err(dev, "netif_set_real_num_rx_queues() failed\n"); 3251 return err; 3252 } 3253 3254 /* Capabilities listing */ 3255 supported |= IFF_LIVE_ADDR_CHANGE; 3256 3257 if (options & DPNI_OPT_NO_MAC_FILTER) 3258 not_supported |= IFF_UNICAST_FLT; 3259 else 3260 supported |= IFF_UNICAST_FLT; 3261 3262 net_dev->priv_flags |= supported; 3263 net_dev->priv_flags &= ~not_supported; 3264 3265 /* Features */ 3266 net_dev->features = NETIF_F_RXCSUM | 3267 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 3268 NETIF_F_SG | NETIF_F_HIGHDMA | 3269 NETIF_F_LLTX; 3270 net_dev->hw_features = net_dev->features; 3271 3272 return 0; 3273 } 3274 3275 static int poll_link_state(void *arg) 3276 { 3277 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg; 3278 int err; 3279 3280 while (!kthread_should_stop()) { 3281 err = link_state_update(priv); 3282 if (unlikely(err)) 3283 return err; 3284 3285 msleep(DPAA2_ETH_LINK_STATE_REFRESH); 3286 } 3287 3288 return 0; 3289 } 3290 3291 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg) 3292 { 3293 u32 status = ~0; 3294 struct device *dev = (struct device *)arg; 3295 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev); 3296 struct net_device *net_dev = dev_get_drvdata(dev); 3297 int err; 3298 3299 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle, 3300 DPNI_IRQ_INDEX, &status); 3301 if (unlikely(err)) { 3302 netdev_err(net_dev, "Can't get irq status (err %d)\n", err); 3303 return IRQ_HANDLED; 3304 } 3305 3306 if (status & DPNI_IRQ_EVENT_LINK_CHANGED) 3307 link_state_update(netdev_priv(net_dev)); 3308 3309 return IRQ_HANDLED; 3310 } 3311 3312 static int setup_irqs(struct fsl_mc_device *ls_dev) 3313 { 3314 int err = 0; 3315 struct fsl_mc_device_irq *irq; 3316 3317 err = fsl_mc_allocate_irqs(ls_dev); 3318 if (err) { 3319 dev_err(&ls_dev->dev, "MC irqs allocation failed\n"); 3320 return err; 3321 } 3322 3323 irq = ls_dev->irqs[0]; 3324 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq, 3325 NULL, dpni_irq0_handler_thread, 3326 IRQF_NO_SUSPEND | IRQF_ONESHOT, 3327 dev_name(&ls_dev->dev), &ls_dev->dev); 3328 if (err < 0) { 3329 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err); 3330 goto free_mc_irq; 3331 } 3332 3333 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle, 3334 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED); 3335 if (err < 0) { 3336 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err); 3337 goto free_irq; 3338 } 3339 3340 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle, 3341 DPNI_IRQ_INDEX, 1); 3342 if (err < 0) { 3343 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err); 3344 goto free_irq; 3345 } 3346 3347 return 0; 3348 3349 free_irq: 3350 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev); 3351 free_mc_irq: 3352 fsl_mc_free_irqs(ls_dev); 3353 3354 return err; 3355 } 3356 3357 static void add_ch_napi(struct dpaa2_eth_priv *priv) 3358 { 3359 int i; 3360 struct dpaa2_eth_channel *ch; 3361 3362 for (i = 0; i < priv->num_channels; i++) { 3363 ch = priv->channel[i]; 3364 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */ 3365 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll, 3366 NAPI_POLL_WEIGHT); 3367 } 3368 } 3369 3370 static void del_ch_napi(struct dpaa2_eth_priv *priv) 3371 { 3372 int i; 3373 struct dpaa2_eth_channel *ch; 3374 3375 for (i = 0; i < priv->num_channels; i++) { 3376 ch = priv->channel[i]; 3377 netif_napi_del(&ch->napi); 3378 } 3379 } 3380 3381 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev) 3382 { 3383 struct device *dev; 3384 struct net_device *net_dev = NULL; 3385 struct dpaa2_eth_priv *priv = NULL; 3386 int err = 0; 3387 3388 dev = &dpni_dev->dev; 3389 3390 /* Net device */ 3391 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES); 3392 if (!net_dev) { 3393 dev_err(dev, "alloc_etherdev_mq() failed\n"); 3394 return -ENOMEM; 3395 } 3396 3397 SET_NETDEV_DEV(net_dev, dev); 3398 dev_set_drvdata(dev, net_dev); 3399 3400 priv = netdev_priv(net_dev); 3401 priv->net_dev = net_dev; 3402 3403 priv->iommu_domain = iommu_get_domain_for_dev(dev); 3404 3405 /* Obtain a MC portal */ 3406 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, 3407 &priv->mc_io); 3408 if (err) { 3409 if (err == -ENXIO) 3410 err = -EPROBE_DEFER; 3411 else 3412 dev_err(dev, "MC portal allocation failed\n"); 3413 goto err_portal_alloc; 3414 } 3415 3416 /* MC objects initialization and configuration */ 3417 err = setup_dpni(dpni_dev); 3418 if (err) 3419 goto err_dpni_setup; 3420 3421 err = setup_dpio(priv); 3422 if (err) 3423 goto err_dpio_setup; 3424 3425 setup_fqs(priv); 3426 3427 err = setup_dpbp(priv); 3428 if (err) 3429 goto err_dpbp_setup; 3430 3431 err = bind_dpni(priv); 3432 if (err) 3433 goto err_bind; 3434 3435 /* Add a NAPI context for each channel */ 3436 add_ch_napi(priv); 3437 3438 /* Percpu statistics */ 3439 priv->percpu_stats = alloc_percpu(*priv->percpu_stats); 3440 if (!priv->percpu_stats) { 3441 dev_err(dev, "alloc_percpu(percpu_stats) failed\n"); 3442 err = -ENOMEM; 3443 goto err_alloc_percpu_stats; 3444 } 3445 priv->percpu_extras = alloc_percpu(*priv->percpu_extras); 3446 if (!priv->percpu_extras) { 3447 dev_err(dev, "alloc_percpu(percpu_extras) failed\n"); 3448 err = -ENOMEM; 3449 goto err_alloc_percpu_extras; 3450 } 3451 3452 err = netdev_init(net_dev); 3453 if (err) 3454 goto err_netdev_init; 3455 3456 /* Configure checksum offload based on current interface flags */ 3457 err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM)); 3458 if (err) 3459 goto err_csum; 3460 3461 err = set_tx_csum(priv, !!(net_dev->features & 3462 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))); 3463 if (err) 3464 goto err_csum; 3465 3466 err = alloc_rings(priv); 3467 if (err) 3468 goto err_alloc_rings; 3469 3470 err = setup_irqs(dpni_dev); 3471 if (err) { 3472 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n"); 3473 priv->poll_thread = kthread_run(poll_link_state, priv, 3474 "%s_poll_link", net_dev->name); 3475 if (IS_ERR(priv->poll_thread)) { 3476 dev_err(dev, "Error starting polling thread\n"); 3477 goto err_poll_thread; 3478 } 3479 priv->do_link_poll = true; 3480 } 3481 3482 err = register_netdev(net_dev); 3483 if (err < 0) { 3484 dev_err(dev, "register_netdev() failed\n"); 3485 goto err_netdev_reg; 3486 } 3487 3488 #ifdef CONFIG_DEBUG_FS 3489 dpaa2_dbg_add(priv); 3490 #endif 3491 3492 dev_info(dev, "Probed interface %s\n", net_dev->name); 3493 return 0; 3494 3495 err_netdev_reg: 3496 if (priv->do_link_poll) 3497 kthread_stop(priv->poll_thread); 3498 else 3499 fsl_mc_free_irqs(dpni_dev); 3500 err_poll_thread: 3501 free_rings(priv); 3502 err_alloc_rings: 3503 err_csum: 3504 err_netdev_init: 3505 free_percpu(priv->percpu_extras); 3506 err_alloc_percpu_extras: 3507 free_percpu(priv->percpu_stats); 3508 err_alloc_percpu_stats: 3509 del_ch_napi(priv); 3510 err_bind: 3511 free_dpbp(priv); 3512 err_dpbp_setup: 3513 free_dpio(priv); 3514 err_dpio_setup: 3515 free_dpni(priv); 3516 err_dpni_setup: 3517 fsl_mc_portal_free(priv->mc_io); 3518 err_portal_alloc: 3519 dev_set_drvdata(dev, NULL); 3520 free_netdev(net_dev); 3521 3522 return err; 3523 } 3524 3525 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev) 3526 { 3527 struct device *dev; 3528 struct net_device *net_dev; 3529 struct dpaa2_eth_priv *priv; 3530 3531 dev = &ls_dev->dev; 3532 net_dev = dev_get_drvdata(dev); 3533 priv = netdev_priv(net_dev); 3534 3535 #ifdef CONFIG_DEBUG_FS 3536 dpaa2_dbg_remove(priv); 3537 #endif 3538 unregister_netdev(net_dev); 3539 3540 if (priv->do_link_poll) 3541 kthread_stop(priv->poll_thread); 3542 else 3543 fsl_mc_free_irqs(ls_dev); 3544 3545 free_rings(priv); 3546 free_percpu(priv->percpu_stats); 3547 free_percpu(priv->percpu_extras); 3548 3549 del_ch_napi(priv); 3550 free_dpbp(priv); 3551 free_dpio(priv); 3552 free_dpni(priv); 3553 3554 fsl_mc_portal_free(priv->mc_io); 3555 3556 free_netdev(net_dev); 3557 3558 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name); 3559 3560 return 0; 3561 } 3562 3563 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = { 3564 { 3565 .vendor = FSL_MC_VENDOR_FREESCALE, 3566 .obj_type = "dpni", 3567 }, 3568 { .vendor = 0x0 } 3569 }; 3570 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table); 3571 3572 static struct fsl_mc_driver dpaa2_eth_driver = { 3573 .driver = { 3574 .name = KBUILD_MODNAME, 3575 .owner = THIS_MODULE, 3576 }, 3577 .probe = dpaa2_eth_probe, 3578 .remove = dpaa2_eth_remove, 3579 .match_id_table = dpaa2_eth_match_id_table 3580 }; 3581 3582 static int __init dpaa2_eth_driver_init(void) 3583 { 3584 int err; 3585 3586 dpaa2_eth_dbg_init(); 3587 err = fsl_mc_driver_register(&dpaa2_eth_driver); 3588 if (err) { 3589 dpaa2_eth_dbg_exit(); 3590 return err; 3591 } 3592 3593 return 0; 3594 } 3595 3596 static void __exit dpaa2_eth_driver_exit(void) 3597 { 3598 dpaa2_eth_dbg_exit(); 3599 fsl_mc_driver_unregister(&dpaa2_eth_driver); 3600 } 3601 3602 module_init(dpaa2_eth_driver_init); 3603 module_exit(dpaa2_eth_driver_exit); 3604