1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* Copyright 2014-2016 Freescale Semiconductor Inc. 3 * Copyright 2016-2020 NXP 4 */ 5 #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/fsl/mc.h> 15 #include <linux/bpf.h> 16 #include <linux/bpf_trace.h> 17 #include <linux/fsl/ptp_qoriq.h> 18 #include <linux/ptp_classify.h> 19 #include <net/pkt_cls.h> 20 #include <net/sock.h> 21 22 #include "dpaa2-eth.h" 23 24 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files 25 * using trace events only need to #include <trace/events/sched.h> 26 */ 27 #define CREATE_TRACE_POINTS 28 #include "dpaa2-eth-trace.h" 29 30 MODULE_LICENSE("Dual BSD/GPL"); 31 MODULE_AUTHOR("Freescale Semiconductor, Inc"); 32 MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver"); 33 34 struct ptp_qoriq *dpaa2_ptp; 35 EXPORT_SYMBOL(dpaa2_ptp); 36 37 static void *dpaa2_iova_to_virt(struct iommu_domain *domain, 38 dma_addr_t iova_addr) 39 { 40 phys_addr_t phys_addr; 41 42 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr; 43 44 return phys_to_virt(phys_addr); 45 } 46 47 static void dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv *priv, 48 u32 fd_status, 49 struct sk_buff *skb) 50 { 51 skb_checksum_none_assert(skb); 52 53 /* HW checksum validation is disabled, nothing to do here */ 54 if (!(priv->net_dev->features & NETIF_F_RXCSUM)) 55 return; 56 57 /* Read checksum validation bits */ 58 if (!((fd_status & DPAA2_FAS_L3CV) && 59 (fd_status & DPAA2_FAS_L4CV))) 60 return; 61 62 /* Inform the stack there's no need to compute L3/L4 csum anymore */ 63 skb->ip_summed = CHECKSUM_UNNECESSARY; 64 } 65 66 /* Free a received FD. 67 * Not to be used for Tx conf FDs or on any other paths. 68 */ 69 static void dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv, 70 const struct dpaa2_fd *fd, 71 void *vaddr) 72 { 73 struct device *dev = priv->net_dev->dev.parent; 74 dma_addr_t addr = dpaa2_fd_get_addr(fd); 75 u8 fd_format = dpaa2_fd_get_format(fd); 76 struct dpaa2_sg_entry *sgt; 77 void *sg_vaddr; 78 int i; 79 80 /* If single buffer frame, just free the data buffer */ 81 if (fd_format == dpaa2_fd_single) 82 goto free_buf; 83 else if (fd_format != dpaa2_fd_sg) 84 /* We don't support any other format */ 85 return; 86 87 /* For S/G frames, we first need to free all SG entries 88 * except the first one, which was taken care of already 89 */ 90 sgt = vaddr + dpaa2_fd_get_offset(fd); 91 for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) { 92 addr = dpaa2_sg_get_addr(&sgt[i]); 93 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr); 94 dma_unmap_page(dev, addr, priv->rx_buf_size, 95 DMA_BIDIRECTIONAL); 96 97 free_pages((unsigned long)sg_vaddr, 0); 98 if (dpaa2_sg_is_final(&sgt[i])) 99 break; 100 } 101 102 free_buf: 103 free_pages((unsigned long)vaddr, 0); 104 } 105 106 /* Build a linear skb based on a single-buffer frame descriptor */ 107 static struct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch, 108 const struct dpaa2_fd *fd, 109 void *fd_vaddr) 110 { 111 struct sk_buff *skb = NULL; 112 u16 fd_offset = dpaa2_fd_get_offset(fd); 113 u32 fd_length = dpaa2_fd_get_len(fd); 114 115 ch->buf_count--; 116 117 skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE); 118 if (unlikely(!skb)) 119 return NULL; 120 121 skb_reserve(skb, fd_offset); 122 skb_put(skb, fd_length); 123 124 return skb; 125 } 126 127 /* Build a non linear (fragmented) skb based on a S/G table */ 128 static struct sk_buff *dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv *priv, 129 struct dpaa2_eth_channel *ch, 130 struct dpaa2_sg_entry *sgt) 131 { 132 struct sk_buff *skb = NULL; 133 struct device *dev = priv->net_dev->dev.parent; 134 void *sg_vaddr; 135 dma_addr_t sg_addr; 136 u16 sg_offset; 137 u32 sg_length; 138 struct page *page, *head_page; 139 int page_offset; 140 int i; 141 142 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) { 143 struct dpaa2_sg_entry *sge = &sgt[i]; 144 145 /* NOTE: We only support SG entries in dpaa2_sg_single format, 146 * but this is the only format we may receive from HW anyway 147 */ 148 149 /* Get the address and length from the S/G entry */ 150 sg_addr = dpaa2_sg_get_addr(sge); 151 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr); 152 dma_unmap_page(dev, sg_addr, priv->rx_buf_size, 153 DMA_BIDIRECTIONAL); 154 155 sg_length = dpaa2_sg_get_len(sge); 156 157 if (i == 0) { 158 /* We build the skb around the first data buffer */ 159 skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE); 160 if (unlikely(!skb)) { 161 /* Free the first SG entry now, since we already 162 * unmapped it and obtained the virtual address 163 */ 164 free_pages((unsigned long)sg_vaddr, 0); 165 166 /* We still need to subtract the buffers used 167 * by this FD from our software counter 168 */ 169 while (!dpaa2_sg_is_final(&sgt[i]) && 170 i < DPAA2_ETH_MAX_SG_ENTRIES) 171 i++; 172 break; 173 } 174 175 sg_offset = dpaa2_sg_get_offset(sge); 176 skb_reserve(skb, sg_offset); 177 skb_put(skb, sg_length); 178 } else { 179 /* Rest of the data buffers are stored as skb frags */ 180 page = virt_to_page(sg_vaddr); 181 head_page = virt_to_head_page(sg_vaddr); 182 183 /* Offset in page (which may be compound). 184 * Data in subsequent SG entries is stored from the 185 * beginning of the buffer, so we don't need to add the 186 * sg_offset. 187 */ 188 page_offset = ((unsigned long)sg_vaddr & 189 (PAGE_SIZE - 1)) + 190 (page_address(page) - page_address(head_page)); 191 192 skb_add_rx_frag(skb, i - 1, head_page, page_offset, 193 sg_length, priv->rx_buf_size); 194 } 195 196 if (dpaa2_sg_is_final(sge)) 197 break; 198 } 199 200 WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT"); 201 202 /* Count all data buffers + SG table buffer */ 203 ch->buf_count -= i + 2; 204 205 return skb; 206 } 207 208 /* Free buffers acquired from the buffer pool or which were meant to 209 * be released in the pool 210 */ 211 static void dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array, 212 int count) 213 { 214 struct device *dev = priv->net_dev->dev.parent; 215 void *vaddr; 216 int i; 217 218 for (i = 0; i < count; i++) { 219 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]); 220 dma_unmap_page(dev, buf_array[i], priv->rx_buf_size, 221 DMA_BIDIRECTIONAL); 222 free_pages((unsigned long)vaddr, 0); 223 } 224 } 225 226 static void dpaa2_eth_recycle_buf(struct dpaa2_eth_priv *priv, 227 struct dpaa2_eth_channel *ch, 228 dma_addr_t addr) 229 { 230 int retries = 0; 231 int err; 232 233 ch->recycled_bufs[ch->recycled_bufs_cnt++] = addr; 234 if (ch->recycled_bufs_cnt < DPAA2_ETH_BUFS_PER_CMD) 235 return; 236 237 while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid, 238 ch->recycled_bufs, 239 ch->recycled_bufs_cnt)) == -EBUSY) { 240 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) 241 break; 242 cpu_relax(); 243 } 244 245 if (err) { 246 dpaa2_eth_free_bufs(priv, ch->recycled_bufs, ch->recycled_bufs_cnt); 247 ch->buf_count -= ch->recycled_bufs_cnt; 248 } 249 250 ch->recycled_bufs_cnt = 0; 251 } 252 253 static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv, 254 struct dpaa2_eth_fq *fq, 255 struct dpaa2_eth_xdp_fds *xdp_fds) 256 { 257 int total_enqueued = 0, retries = 0, enqueued; 258 struct dpaa2_eth_drv_stats *percpu_extras; 259 int num_fds, err, max_retries; 260 struct dpaa2_fd *fds; 261 262 percpu_extras = this_cpu_ptr(priv->percpu_extras); 263 264 /* try to enqueue all the FDs until the max number of retries is hit */ 265 fds = xdp_fds->fds; 266 num_fds = xdp_fds->num; 267 max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES; 268 while (total_enqueued < num_fds && retries < max_retries) { 269 err = priv->enqueue(priv, fq, &fds[total_enqueued], 270 0, num_fds - total_enqueued, &enqueued); 271 if (err == -EBUSY) { 272 percpu_extras->tx_portal_busy += ++retries; 273 continue; 274 } 275 total_enqueued += enqueued; 276 } 277 xdp_fds->num = 0; 278 279 return total_enqueued; 280 } 281 282 static void dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv *priv, 283 struct dpaa2_eth_channel *ch, 284 struct dpaa2_eth_fq *fq) 285 { 286 struct rtnl_link_stats64 *percpu_stats; 287 struct dpaa2_fd *fds; 288 int enqueued, i; 289 290 percpu_stats = this_cpu_ptr(priv->percpu_stats); 291 292 // enqueue the array of XDP_TX frames 293 enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds); 294 295 /* update statistics */ 296 percpu_stats->tx_packets += enqueued; 297 fds = fq->xdp_tx_fds.fds; 298 for (i = 0; i < enqueued; i++) { 299 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]); 300 ch->stats.xdp_tx++; 301 } 302 for (i = enqueued; i < fq->xdp_tx_fds.num; i++) { 303 dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(&fds[i])); 304 percpu_stats->tx_errors++; 305 ch->stats.xdp_tx_err++; 306 } 307 fq->xdp_tx_fds.num = 0; 308 } 309 310 static void dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv *priv, 311 struct dpaa2_eth_channel *ch, 312 struct dpaa2_fd *fd, 313 void *buf_start, u16 queue_id) 314 { 315 struct dpaa2_faead *faead; 316 struct dpaa2_fd *dest_fd; 317 struct dpaa2_eth_fq *fq; 318 u32 ctrl, frc; 319 320 /* Mark the egress frame hardware annotation area as valid */ 321 frc = dpaa2_fd_get_frc(fd); 322 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV); 323 dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL); 324 325 /* Instruct hardware to release the FD buffer directly into 326 * the buffer pool once transmission is completed, instead of 327 * sending a Tx confirmation frame to us 328 */ 329 ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV; 330 faead = dpaa2_get_faead(buf_start, false); 331 faead->ctrl = cpu_to_le32(ctrl); 332 faead->conf_fqid = 0; 333 334 fq = &priv->fq[queue_id]; 335 dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++]; 336 memcpy(dest_fd, fd, sizeof(*dest_fd)); 337 338 if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE) 339 return; 340 341 dpaa2_eth_xdp_tx_flush(priv, ch, fq); 342 } 343 344 static u32 dpaa2_eth_run_xdp(struct dpaa2_eth_priv *priv, 345 struct dpaa2_eth_channel *ch, 346 struct dpaa2_eth_fq *rx_fq, 347 struct dpaa2_fd *fd, void *vaddr) 348 { 349 dma_addr_t addr = dpaa2_fd_get_addr(fd); 350 struct bpf_prog *xdp_prog; 351 struct xdp_buff xdp; 352 u32 xdp_act = XDP_PASS; 353 int err, offset; 354 355 xdp_prog = READ_ONCE(ch->xdp.prog); 356 if (!xdp_prog) 357 goto out; 358 359 offset = dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM; 360 xdp_init_buff(&xdp, DPAA2_ETH_RX_BUF_RAW_SIZE - offset, &ch->xdp_rxq); 361 xdp_prepare_buff(&xdp, vaddr + offset, XDP_PACKET_HEADROOM, 362 dpaa2_fd_get_len(fd), false); 363 364 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp); 365 366 /* xdp.data pointer may have changed */ 367 dpaa2_fd_set_offset(fd, xdp.data - vaddr); 368 dpaa2_fd_set_len(fd, xdp.data_end - xdp.data); 369 370 switch (xdp_act) { 371 case XDP_PASS: 372 break; 373 case XDP_TX: 374 dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid); 375 break; 376 default: 377 bpf_warn_invalid_xdp_action(priv->net_dev, xdp_prog, xdp_act); 378 fallthrough; 379 case XDP_ABORTED: 380 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act); 381 fallthrough; 382 case XDP_DROP: 383 dpaa2_eth_recycle_buf(priv, ch, addr); 384 ch->stats.xdp_drop++; 385 break; 386 case XDP_REDIRECT: 387 dma_unmap_page(priv->net_dev->dev.parent, addr, 388 priv->rx_buf_size, DMA_BIDIRECTIONAL); 389 ch->buf_count--; 390 391 /* Allow redirect use of full headroom */ 392 xdp.data_hard_start = vaddr; 393 xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE; 394 395 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog); 396 if (unlikely(err)) { 397 addr = dma_map_page(priv->net_dev->dev.parent, 398 virt_to_page(vaddr), 0, 399 priv->rx_buf_size, DMA_BIDIRECTIONAL); 400 if (unlikely(dma_mapping_error(priv->net_dev->dev.parent, addr))) { 401 free_pages((unsigned long)vaddr, 0); 402 } else { 403 ch->buf_count++; 404 dpaa2_eth_recycle_buf(priv, ch, addr); 405 } 406 ch->stats.xdp_drop++; 407 } else { 408 ch->stats.xdp_redirect++; 409 } 410 break; 411 } 412 413 ch->xdp.res |= xdp_act; 414 out: 415 return xdp_act; 416 } 417 418 static struct sk_buff *dpaa2_eth_copybreak(struct dpaa2_eth_channel *ch, 419 const struct dpaa2_fd *fd, 420 void *fd_vaddr) 421 { 422 u16 fd_offset = dpaa2_fd_get_offset(fd); 423 struct dpaa2_eth_priv *priv = ch->priv; 424 u32 fd_length = dpaa2_fd_get_len(fd); 425 struct sk_buff *skb = NULL; 426 unsigned int skb_len; 427 428 if (fd_length > priv->rx_copybreak) 429 return NULL; 430 431 skb_len = fd_length + dpaa2_eth_needed_headroom(NULL); 432 433 skb = napi_alloc_skb(&ch->napi, skb_len); 434 if (!skb) 435 return NULL; 436 437 skb_reserve(skb, dpaa2_eth_needed_headroom(NULL)); 438 skb_put(skb, fd_length); 439 440 memcpy(skb->data, fd_vaddr + fd_offset, fd_length); 441 442 dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(fd)); 443 444 return skb; 445 } 446 447 /* Main Rx frame processing routine */ 448 static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv, 449 struct dpaa2_eth_channel *ch, 450 const struct dpaa2_fd *fd, 451 struct dpaa2_eth_fq *fq) 452 { 453 dma_addr_t addr = dpaa2_fd_get_addr(fd); 454 u8 fd_format = dpaa2_fd_get_format(fd); 455 void *vaddr; 456 struct sk_buff *skb; 457 struct rtnl_link_stats64 *percpu_stats; 458 struct dpaa2_eth_drv_stats *percpu_extras; 459 struct device *dev = priv->net_dev->dev.parent; 460 struct dpaa2_fas *fas; 461 void *buf_data; 462 u32 status = 0; 463 u32 xdp_act; 464 465 /* Tracing point */ 466 trace_dpaa2_rx_fd(priv->net_dev, fd); 467 468 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr); 469 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size, 470 DMA_BIDIRECTIONAL); 471 472 fas = dpaa2_get_fas(vaddr, false); 473 prefetch(fas); 474 buf_data = vaddr + dpaa2_fd_get_offset(fd); 475 prefetch(buf_data); 476 477 percpu_stats = this_cpu_ptr(priv->percpu_stats); 478 percpu_extras = this_cpu_ptr(priv->percpu_extras); 479 480 if (fd_format == dpaa2_fd_single) { 481 xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr); 482 if (xdp_act != XDP_PASS) { 483 percpu_stats->rx_packets++; 484 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd); 485 return; 486 } 487 488 skb = dpaa2_eth_copybreak(ch, fd, vaddr); 489 if (!skb) { 490 dma_unmap_page(dev, addr, priv->rx_buf_size, 491 DMA_BIDIRECTIONAL); 492 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr); 493 } 494 } else if (fd_format == dpaa2_fd_sg) { 495 WARN_ON(priv->xdp_prog); 496 497 dma_unmap_page(dev, addr, priv->rx_buf_size, 498 DMA_BIDIRECTIONAL); 499 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data); 500 free_pages((unsigned long)vaddr, 0); 501 percpu_extras->rx_sg_frames++; 502 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd); 503 } else { 504 /* We don't support any other format */ 505 goto err_frame_format; 506 } 507 508 if (unlikely(!skb)) 509 goto err_build_skb; 510 511 prefetch(skb->data); 512 513 /* Get the timestamp value */ 514 if (priv->rx_tstamp) { 515 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 516 __le64 *ts = dpaa2_get_ts(vaddr, false); 517 u64 ns; 518 519 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 520 521 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts); 522 shhwtstamps->hwtstamp = ns_to_ktime(ns); 523 } 524 525 /* Check if we need to validate the L4 csum */ 526 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) { 527 status = le32_to_cpu(fas->status); 528 dpaa2_eth_validate_rx_csum(priv, status, skb); 529 } 530 531 skb->protocol = eth_type_trans(skb, priv->net_dev); 532 skb_record_rx_queue(skb, fq->flowid); 533 534 percpu_stats->rx_packets++; 535 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd); 536 ch->stats.bytes_per_cdan += dpaa2_fd_get_len(fd); 537 538 list_add_tail(&skb->list, ch->rx_list); 539 540 return; 541 542 err_build_skb: 543 dpaa2_eth_free_rx_fd(priv, fd, vaddr); 544 err_frame_format: 545 percpu_stats->rx_dropped++; 546 } 547 548 /* Processing of Rx frames received on the error FQ 549 * We check and print the error bits and then free the frame 550 */ 551 static void dpaa2_eth_rx_err(struct dpaa2_eth_priv *priv, 552 struct dpaa2_eth_channel *ch, 553 const struct dpaa2_fd *fd, 554 struct dpaa2_eth_fq *fq __always_unused) 555 { 556 struct device *dev = priv->net_dev->dev.parent; 557 dma_addr_t addr = dpaa2_fd_get_addr(fd); 558 u8 fd_format = dpaa2_fd_get_format(fd); 559 struct rtnl_link_stats64 *percpu_stats; 560 struct dpaa2_eth_trap_item *trap_item; 561 struct dpaa2_fapr *fapr; 562 struct sk_buff *skb; 563 void *buf_data; 564 void *vaddr; 565 566 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr); 567 dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size, 568 DMA_BIDIRECTIONAL); 569 570 buf_data = vaddr + dpaa2_fd_get_offset(fd); 571 572 if (fd_format == dpaa2_fd_single) { 573 dma_unmap_page(dev, addr, priv->rx_buf_size, 574 DMA_BIDIRECTIONAL); 575 skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr); 576 } else if (fd_format == dpaa2_fd_sg) { 577 dma_unmap_page(dev, addr, priv->rx_buf_size, 578 DMA_BIDIRECTIONAL); 579 skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data); 580 free_pages((unsigned long)vaddr, 0); 581 } else { 582 /* We don't support any other format */ 583 dpaa2_eth_free_rx_fd(priv, fd, vaddr); 584 goto err_frame_format; 585 } 586 587 fapr = dpaa2_get_fapr(vaddr, false); 588 trap_item = dpaa2_eth_dl_get_trap(priv, fapr); 589 if (trap_item) 590 devlink_trap_report(priv->devlink, skb, trap_item->trap_ctx, 591 &priv->devlink_port, NULL); 592 consume_skb(skb); 593 594 err_frame_format: 595 percpu_stats = this_cpu_ptr(priv->percpu_stats); 596 percpu_stats->rx_errors++; 597 ch->buf_count--; 598 } 599 600 /* Consume all frames pull-dequeued into the store. This is the simplest way to 601 * make sure we don't accidentally issue another volatile dequeue which would 602 * overwrite (leak) frames already in the store. 603 * 604 * Observance of NAPI budget is not our concern, leaving that to the caller. 605 */ 606 static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch, 607 struct dpaa2_eth_fq **src) 608 { 609 struct dpaa2_eth_priv *priv = ch->priv; 610 struct dpaa2_eth_fq *fq = NULL; 611 struct dpaa2_dq *dq; 612 const struct dpaa2_fd *fd; 613 int cleaned = 0, retries = 0; 614 int is_last; 615 616 do { 617 dq = dpaa2_io_store_next(ch->store, &is_last); 618 if (unlikely(!dq)) { 619 /* If we're here, we *must* have placed a 620 * volatile dequeue comnmand, so keep reading through 621 * the store until we get some sort of valid response 622 * token (either a valid frame or an "empty dequeue") 623 */ 624 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) { 625 netdev_err_once(priv->net_dev, 626 "Unable to read a valid dequeue response\n"); 627 return -ETIMEDOUT; 628 } 629 continue; 630 } 631 632 fd = dpaa2_dq_fd(dq); 633 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq); 634 635 fq->consume(priv, ch, fd, fq); 636 cleaned++; 637 retries = 0; 638 } while (!is_last); 639 640 if (!cleaned) 641 return 0; 642 643 fq->stats.frames += cleaned; 644 ch->stats.frames += cleaned; 645 ch->stats.frames_per_cdan += cleaned; 646 647 /* A dequeue operation only pulls frames from a single queue 648 * into the store. Return the frame queue as an out param. 649 */ 650 if (src) 651 *src = fq; 652 653 return cleaned; 654 } 655 656 static int dpaa2_eth_ptp_parse(struct sk_buff *skb, 657 u8 *msgtype, u8 *twostep, u8 *udp, 658 u16 *correction_offset, 659 u16 *origintimestamp_offset) 660 { 661 unsigned int ptp_class; 662 struct ptp_header *hdr; 663 unsigned int type; 664 u8 *base; 665 666 ptp_class = ptp_classify_raw(skb); 667 if (ptp_class == PTP_CLASS_NONE) 668 return -EINVAL; 669 670 hdr = ptp_parse_header(skb, ptp_class); 671 if (!hdr) 672 return -EINVAL; 673 674 *msgtype = ptp_get_msgtype(hdr, ptp_class); 675 *twostep = hdr->flag_field[0] & 0x2; 676 677 type = ptp_class & PTP_CLASS_PMASK; 678 if (type == PTP_CLASS_IPV4 || 679 type == PTP_CLASS_IPV6) 680 *udp = 1; 681 else 682 *udp = 0; 683 684 base = skb_mac_header(skb); 685 *correction_offset = (u8 *)&hdr->correction - base; 686 *origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base; 687 688 return 0; 689 } 690 691 /* Configure the egress frame annotation for timestamp update */ 692 static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv, 693 struct dpaa2_fd *fd, 694 void *buf_start, 695 struct sk_buff *skb) 696 { 697 struct ptp_tstamp origin_timestamp; 698 struct dpni_single_step_cfg cfg; 699 u8 msgtype, twostep, udp; 700 struct dpaa2_faead *faead; 701 struct dpaa2_fas *fas; 702 struct timespec64 ts; 703 u16 offset1, offset2; 704 u32 ctrl, frc; 705 __le64 *ns; 706 u8 *data; 707 708 /* Mark the egress frame annotation area as valid */ 709 frc = dpaa2_fd_get_frc(fd); 710 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV); 711 712 /* Set hardware annotation size */ 713 ctrl = dpaa2_fd_get_ctrl(fd); 714 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL); 715 716 /* enable UPD (update prepanded data) bit in FAEAD field of 717 * hardware frame annotation area 718 */ 719 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD; 720 faead = dpaa2_get_faead(buf_start, true); 721 faead->ctrl = cpu_to_le32(ctrl); 722 723 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) { 724 if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp, 725 &offset1, &offset2) || 726 msgtype != PTP_MSGTYPE_SYNC || twostep) { 727 WARN_ONCE(1, "Bad packet for one-step timestamping\n"); 728 return; 729 } 730 731 /* Mark the frame annotation status as valid */ 732 frc = dpaa2_fd_get_frc(fd); 733 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV); 734 735 /* Mark the PTP flag for one step timestamping */ 736 fas = dpaa2_get_fas(buf_start, true); 737 fas->status = cpu_to_le32(DPAA2_FAS_PTP); 738 739 dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts); 740 ns = dpaa2_get_ts(buf_start, true); 741 *ns = cpu_to_le64(timespec64_to_ns(&ts) / 742 DPAA2_PTP_CLK_PERIOD_NS); 743 744 /* Update current time to PTP message originTimestamp field */ 745 ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns)); 746 data = skb_mac_header(skb); 747 *(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb); 748 *(__be32 *)(data + offset2 + 2) = 749 htonl(origin_timestamp.sec_lsb); 750 *(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec); 751 752 cfg.en = 1; 753 cfg.ch_update = udp; 754 cfg.offset = offset1; 755 cfg.peer_delay = 0; 756 757 if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token, 758 &cfg)) 759 WARN_ONCE(1, "Failed to set single step register"); 760 } 761 } 762 763 /* Create a frame descriptor based on a fragmented skb */ 764 static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv, 765 struct sk_buff *skb, 766 struct dpaa2_fd *fd, 767 void **swa_addr) 768 { 769 struct device *dev = priv->net_dev->dev.parent; 770 void *sgt_buf = NULL; 771 dma_addr_t addr; 772 int nr_frags = skb_shinfo(skb)->nr_frags; 773 struct dpaa2_sg_entry *sgt; 774 int i, err; 775 int sgt_buf_size; 776 struct scatterlist *scl, *crt_scl; 777 int num_sg; 778 int num_dma_bufs; 779 struct dpaa2_eth_swa *swa; 780 781 /* Create and map scatterlist. 782 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have 783 * to go beyond nr_frags+1. 784 * Note: We don't support chained scatterlists 785 */ 786 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1)) 787 return -EINVAL; 788 789 scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC); 790 if (unlikely(!scl)) 791 return -ENOMEM; 792 793 sg_init_table(scl, nr_frags + 1); 794 num_sg = skb_to_sgvec(skb, scl, 0, skb->len); 795 if (unlikely(num_sg < 0)) { 796 err = -ENOMEM; 797 goto dma_map_sg_failed; 798 } 799 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL); 800 if (unlikely(!num_dma_bufs)) { 801 err = -ENOMEM; 802 goto dma_map_sg_failed; 803 } 804 805 /* Prepare the HW SGT structure */ 806 sgt_buf_size = priv->tx_data_offset + 807 sizeof(struct dpaa2_sg_entry) * num_dma_bufs; 808 sgt_buf = napi_alloc_frag_align(sgt_buf_size, DPAA2_ETH_TX_BUF_ALIGN); 809 if (unlikely(!sgt_buf)) { 810 err = -ENOMEM; 811 goto sgt_buf_alloc_failed; 812 } 813 memset(sgt_buf, 0, sgt_buf_size); 814 815 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset); 816 817 /* Fill in the HW SGT structure. 818 * 819 * sgt_buf is zeroed out, so the following fields are implicit 820 * in all sgt entries: 821 * - offset is 0 822 * - format is 'dpaa2_sg_single' 823 */ 824 for_each_sg(scl, crt_scl, num_dma_bufs, i) { 825 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl)); 826 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl)); 827 } 828 dpaa2_sg_set_final(&sgt[i - 1], true); 829 830 /* Store the skb backpointer in the SGT buffer. 831 * Fit the scatterlist and the number of buffers alongside the 832 * skb backpointer in the software annotation area. We'll need 833 * all of them on Tx Conf. 834 */ 835 *swa_addr = (void *)sgt_buf; 836 swa = (struct dpaa2_eth_swa *)sgt_buf; 837 swa->type = DPAA2_ETH_SWA_SG; 838 swa->sg.skb = skb; 839 swa->sg.scl = scl; 840 swa->sg.num_sg = num_sg; 841 swa->sg.sgt_size = sgt_buf_size; 842 843 /* Separately map the SGT buffer */ 844 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL); 845 if (unlikely(dma_mapping_error(dev, addr))) { 846 err = -ENOMEM; 847 goto dma_map_single_failed; 848 } 849 dpaa2_fd_set_offset(fd, priv->tx_data_offset); 850 dpaa2_fd_set_format(fd, dpaa2_fd_sg); 851 dpaa2_fd_set_addr(fd, addr); 852 dpaa2_fd_set_len(fd, skb->len); 853 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA); 854 855 return 0; 856 857 dma_map_single_failed: 858 skb_free_frag(sgt_buf); 859 sgt_buf_alloc_failed: 860 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL); 861 dma_map_sg_failed: 862 kfree(scl); 863 return err; 864 } 865 866 /* Create a SG frame descriptor based on a linear skb. 867 * 868 * This function is used on the Tx path when the skb headroom is not large 869 * enough for the HW requirements, thus instead of realloc-ing the skb we 870 * create a SG frame descriptor with only one entry. 871 */ 872 static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv, 873 struct sk_buff *skb, 874 struct dpaa2_fd *fd, 875 void **swa_addr) 876 { 877 struct device *dev = priv->net_dev->dev.parent; 878 struct dpaa2_eth_sgt_cache *sgt_cache; 879 struct dpaa2_sg_entry *sgt; 880 struct dpaa2_eth_swa *swa; 881 dma_addr_t addr, sgt_addr; 882 void *sgt_buf = NULL; 883 int sgt_buf_size; 884 int err; 885 886 /* Prepare the HW SGT structure */ 887 sgt_cache = this_cpu_ptr(priv->sgt_cache); 888 sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry); 889 890 if (sgt_cache->count == 0) 891 sgt_buf = napi_alloc_frag_align(sgt_buf_size, DPAA2_ETH_TX_BUF_ALIGN); 892 else 893 sgt_buf = sgt_cache->buf[--sgt_cache->count]; 894 if (unlikely(!sgt_buf)) 895 return -ENOMEM; 896 memset(sgt_buf, 0, sgt_buf_size); 897 898 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset); 899 900 addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL); 901 if (unlikely(dma_mapping_error(dev, addr))) { 902 err = -ENOMEM; 903 goto data_map_failed; 904 } 905 906 /* Fill in the HW SGT structure */ 907 dpaa2_sg_set_addr(sgt, addr); 908 dpaa2_sg_set_len(sgt, skb->len); 909 dpaa2_sg_set_final(sgt, true); 910 911 /* Store the skb backpointer in the SGT buffer */ 912 *swa_addr = (void *)sgt_buf; 913 swa = (struct dpaa2_eth_swa *)sgt_buf; 914 swa->type = DPAA2_ETH_SWA_SINGLE; 915 swa->single.skb = skb; 916 swa->single.sgt_size = sgt_buf_size; 917 918 /* Separately map the SGT buffer */ 919 sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL); 920 if (unlikely(dma_mapping_error(dev, sgt_addr))) { 921 err = -ENOMEM; 922 goto sgt_map_failed; 923 } 924 925 dpaa2_fd_set_offset(fd, priv->tx_data_offset); 926 dpaa2_fd_set_format(fd, dpaa2_fd_sg); 927 dpaa2_fd_set_addr(fd, sgt_addr); 928 dpaa2_fd_set_len(fd, skb->len); 929 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA); 930 931 return 0; 932 933 sgt_map_failed: 934 dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL); 935 data_map_failed: 936 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE) 937 skb_free_frag(sgt_buf); 938 else 939 sgt_cache->buf[sgt_cache->count++] = sgt_buf; 940 941 return err; 942 } 943 944 /* Create a frame descriptor based on a linear skb */ 945 static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv, 946 struct sk_buff *skb, 947 struct dpaa2_fd *fd, 948 void **swa_addr) 949 { 950 struct device *dev = priv->net_dev->dev.parent; 951 u8 *buffer_start, *aligned_start; 952 struct dpaa2_eth_swa *swa; 953 dma_addr_t addr; 954 955 buffer_start = skb->data - dpaa2_eth_needed_headroom(skb); 956 957 /* If there's enough room to align the FD address, do it. 958 * It will help hardware optimize accesses. 959 */ 960 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN, 961 DPAA2_ETH_TX_BUF_ALIGN); 962 if (aligned_start >= skb->head) 963 buffer_start = aligned_start; 964 965 /* Store a backpointer to the skb at the beginning of the buffer 966 * (in the private data area) such that we can release it 967 * on Tx confirm 968 */ 969 *swa_addr = (void *)buffer_start; 970 swa = (struct dpaa2_eth_swa *)buffer_start; 971 swa->type = DPAA2_ETH_SWA_SINGLE; 972 swa->single.skb = skb; 973 974 addr = dma_map_single(dev, buffer_start, 975 skb_tail_pointer(skb) - buffer_start, 976 DMA_BIDIRECTIONAL); 977 if (unlikely(dma_mapping_error(dev, addr))) 978 return -ENOMEM; 979 980 dpaa2_fd_set_addr(fd, addr); 981 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start)); 982 dpaa2_fd_set_len(fd, skb->len); 983 dpaa2_fd_set_format(fd, dpaa2_fd_single); 984 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA); 985 986 return 0; 987 } 988 989 /* FD freeing routine on the Tx path 990 * 991 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb 992 * back-pointed to is also freed. 993 * This can be called either from dpaa2_eth_tx_conf() or on the error path of 994 * dpaa2_eth_tx(). 995 */ 996 static void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv, 997 struct dpaa2_eth_fq *fq, 998 const struct dpaa2_fd *fd, bool in_napi) 999 { 1000 struct device *dev = priv->net_dev->dev.parent; 1001 dma_addr_t fd_addr, sg_addr; 1002 struct sk_buff *skb = NULL; 1003 unsigned char *buffer_start; 1004 struct dpaa2_eth_swa *swa; 1005 u8 fd_format = dpaa2_fd_get_format(fd); 1006 u32 fd_len = dpaa2_fd_get_len(fd); 1007 1008 struct dpaa2_eth_sgt_cache *sgt_cache; 1009 struct dpaa2_sg_entry *sgt; 1010 1011 fd_addr = dpaa2_fd_get_addr(fd); 1012 buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr); 1013 swa = (struct dpaa2_eth_swa *)buffer_start; 1014 1015 if (fd_format == dpaa2_fd_single) { 1016 if (swa->type == DPAA2_ETH_SWA_SINGLE) { 1017 skb = swa->single.skb; 1018 /* Accessing the skb buffer is safe before dma unmap, 1019 * because we didn't map the actual skb shell. 1020 */ 1021 dma_unmap_single(dev, fd_addr, 1022 skb_tail_pointer(skb) - buffer_start, 1023 DMA_BIDIRECTIONAL); 1024 } else { 1025 WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type"); 1026 dma_unmap_single(dev, fd_addr, swa->xdp.dma_size, 1027 DMA_BIDIRECTIONAL); 1028 } 1029 } else if (fd_format == dpaa2_fd_sg) { 1030 if (swa->type == DPAA2_ETH_SWA_SG) { 1031 skb = swa->sg.skb; 1032 1033 /* Unmap the scatterlist */ 1034 dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg, 1035 DMA_BIDIRECTIONAL); 1036 kfree(swa->sg.scl); 1037 1038 /* Unmap the SGT buffer */ 1039 dma_unmap_single(dev, fd_addr, swa->sg.sgt_size, 1040 DMA_BIDIRECTIONAL); 1041 } else { 1042 skb = swa->single.skb; 1043 1044 /* Unmap the SGT Buffer */ 1045 dma_unmap_single(dev, fd_addr, swa->single.sgt_size, 1046 DMA_BIDIRECTIONAL); 1047 1048 sgt = (struct dpaa2_sg_entry *)(buffer_start + 1049 priv->tx_data_offset); 1050 sg_addr = dpaa2_sg_get_addr(sgt); 1051 dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL); 1052 } 1053 } else { 1054 netdev_dbg(priv->net_dev, "Invalid FD format\n"); 1055 return; 1056 } 1057 1058 if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) { 1059 fq->dq_frames++; 1060 fq->dq_bytes += fd_len; 1061 } 1062 1063 if (swa->type == DPAA2_ETH_SWA_XDP) { 1064 xdp_return_frame(swa->xdp.xdpf); 1065 return; 1066 } 1067 1068 /* Get the timestamp value */ 1069 if (skb->cb[0] == TX_TSTAMP) { 1070 struct skb_shared_hwtstamps shhwtstamps; 1071 __le64 *ts = dpaa2_get_ts(buffer_start, true); 1072 u64 ns; 1073 1074 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 1075 1076 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts); 1077 shhwtstamps.hwtstamp = ns_to_ktime(ns); 1078 skb_tstamp_tx(skb, &shhwtstamps); 1079 } else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) { 1080 mutex_unlock(&priv->onestep_tstamp_lock); 1081 } 1082 1083 /* Free SGT buffer allocated on tx */ 1084 if (fd_format != dpaa2_fd_single) { 1085 sgt_cache = this_cpu_ptr(priv->sgt_cache); 1086 if (swa->type == DPAA2_ETH_SWA_SG) { 1087 skb_free_frag(buffer_start); 1088 } else { 1089 if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE) 1090 skb_free_frag(buffer_start); 1091 else 1092 sgt_cache->buf[sgt_cache->count++] = buffer_start; 1093 } 1094 } 1095 1096 /* Move on with skb release */ 1097 napi_consume_skb(skb, in_napi); 1098 } 1099 1100 static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb, 1101 struct net_device *net_dev) 1102 { 1103 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1104 struct dpaa2_eth_drv_stats *percpu_extras; 1105 struct rtnl_link_stats64 *percpu_stats; 1106 unsigned int needed_headroom; 1107 struct dpaa2_eth_fq *fq; 1108 struct netdev_queue *nq; 1109 struct dpaa2_fd fd; 1110 u16 queue_mapping; 1111 u8 prio = 0; 1112 int err, i; 1113 u32 fd_len; 1114 void *swa; 1115 1116 percpu_stats = this_cpu_ptr(priv->percpu_stats); 1117 percpu_extras = this_cpu_ptr(priv->percpu_extras); 1118 1119 needed_headroom = dpaa2_eth_needed_headroom(skb); 1120 1121 /* We'll be holding a back-reference to the skb until Tx Confirmation; 1122 * we don't want that overwritten by a concurrent Tx with a cloned skb. 1123 */ 1124 skb = skb_unshare(skb, GFP_ATOMIC); 1125 if (unlikely(!skb)) { 1126 /* skb_unshare() has already freed the skb */ 1127 percpu_stats->tx_dropped++; 1128 return NETDEV_TX_OK; 1129 } 1130 1131 /* Setup the FD fields */ 1132 memset(&fd, 0, sizeof(fd)); 1133 1134 if (skb_is_nonlinear(skb)) { 1135 err = dpaa2_eth_build_sg_fd(priv, skb, &fd, &swa); 1136 percpu_extras->tx_sg_frames++; 1137 percpu_extras->tx_sg_bytes += skb->len; 1138 } else if (skb_headroom(skb) < needed_headroom) { 1139 err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, &fd, &swa); 1140 percpu_extras->tx_sg_frames++; 1141 percpu_extras->tx_sg_bytes += skb->len; 1142 percpu_extras->tx_converted_sg_frames++; 1143 percpu_extras->tx_converted_sg_bytes += skb->len; 1144 } else { 1145 err = dpaa2_eth_build_single_fd(priv, skb, &fd, &swa); 1146 } 1147 1148 if (unlikely(err)) { 1149 percpu_stats->tx_dropped++; 1150 goto err_build_fd; 1151 } 1152 1153 if (skb->cb[0]) 1154 dpaa2_eth_enable_tx_tstamp(priv, &fd, swa, skb); 1155 1156 /* Tracing point */ 1157 trace_dpaa2_tx_fd(net_dev, &fd); 1158 1159 /* TxConf FQ selection relies on queue id from the stack. 1160 * In case of a forwarded frame from another DPNI interface, we choose 1161 * a queue affined to the same core that processed the Rx frame 1162 */ 1163 queue_mapping = skb_get_queue_mapping(skb); 1164 1165 if (net_dev->num_tc) { 1166 prio = netdev_txq_to_tc(net_dev, queue_mapping); 1167 /* Hardware interprets priority level 0 as being the highest, 1168 * so we need to do a reverse mapping to the netdev tc index 1169 */ 1170 prio = net_dev->num_tc - prio - 1; 1171 /* We have only one FQ array entry for all Tx hardware queues 1172 * with the same flow id (but different priority levels) 1173 */ 1174 queue_mapping %= dpaa2_eth_queue_count(priv); 1175 } 1176 fq = &priv->fq[queue_mapping]; 1177 1178 fd_len = dpaa2_fd_get_len(&fd); 1179 nq = netdev_get_tx_queue(net_dev, queue_mapping); 1180 netdev_tx_sent_queue(nq, fd_len); 1181 1182 /* Everything that happens after this enqueues might race with 1183 * the Tx confirmation callback for this frame 1184 */ 1185 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) { 1186 err = priv->enqueue(priv, fq, &fd, prio, 1, NULL); 1187 if (err != -EBUSY) 1188 break; 1189 } 1190 percpu_extras->tx_portal_busy += i; 1191 if (unlikely(err < 0)) { 1192 percpu_stats->tx_errors++; 1193 /* Clean up everything, including freeing the skb */ 1194 dpaa2_eth_free_tx_fd(priv, fq, &fd, false); 1195 netdev_tx_completed_queue(nq, 1, fd_len); 1196 } else { 1197 percpu_stats->tx_packets++; 1198 percpu_stats->tx_bytes += fd_len; 1199 } 1200 1201 return NETDEV_TX_OK; 1202 1203 err_build_fd: 1204 dev_kfree_skb(skb); 1205 1206 return NETDEV_TX_OK; 1207 } 1208 1209 static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work) 1210 { 1211 struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv, 1212 tx_onestep_tstamp); 1213 struct sk_buff *skb; 1214 1215 while (true) { 1216 skb = skb_dequeue(&priv->tx_skbs); 1217 if (!skb) 1218 return; 1219 1220 /* Lock just before TX one-step timestamping packet, 1221 * and release the lock in dpaa2_eth_free_tx_fd when 1222 * confirm the packet has been sent on hardware, or 1223 * when clean up during transmit failure. 1224 */ 1225 mutex_lock(&priv->onestep_tstamp_lock); 1226 __dpaa2_eth_tx(skb, priv->net_dev); 1227 } 1228 } 1229 1230 static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev) 1231 { 1232 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1233 u8 msgtype, twostep, udp; 1234 u16 offset1, offset2; 1235 1236 /* Utilize skb->cb[0] for timestamping request per skb */ 1237 skb->cb[0] = 0; 1238 1239 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) { 1240 if (priv->tx_tstamp_type == HWTSTAMP_TX_ON) 1241 skb->cb[0] = TX_TSTAMP; 1242 else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC) 1243 skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC; 1244 } 1245 1246 /* TX for one-step timestamping PTP Sync packet */ 1247 if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) { 1248 if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp, 1249 &offset1, &offset2)) 1250 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) { 1251 skb_queue_tail(&priv->tx_skbs, skb); 1252 queue_work(priv->dpaa2_ptp_wq, 1253 &priv->tx_onestep_tstamp); 1254 return NETDEV_TX_OK; 1255 } 1256 /* Use two-step timestamping if not one-step timestamping 1257 * PTP Sync packet 1258 */ 1259 skb->cb[0] = TX_TSTAMP; 1260 } 1261 1262 /* TX for other packets */ 1263 return __dpaa2_eth_tx(skb, net_dev); 1264 } 1265 1266 /* Tx confirmation frame processing routine */ 1267 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv, 1268 struct dpaa2_eth_channel *ch, 1269 const struct dpaa2_fd *fd, 1270 struct dpaa2_eth_fq *fq) 1271 { 1272 struct rtnl_link_stats64 *percpu_stats; 1273 struct dpaa2_eth_drv_stats *percpu_extras; 1274 u32 fd_len = dpaa2_fd_get_len(fd); 1275 u32 fd_errors; 1276 1277 /* Tracing point */ 1278 trace_dpaa2_tx_conf_fd(priv->net_dev, fd); 1279 1280 percpu_extras = this_cpu_ptr(priv->percpu_extras); 1281 percpu_extras->tx_conf_frames++; 1282 percpu_extras->tx_conf_bytes += fd_len; 1283 ch->stats.bytes_per_cdan += fd_len; 1284 1285 /* Check frame errors in the FD field */ 1286 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK; 1287 dpaa2_eth_free_tx_fd(priv, fq, fd, true); 1288 1289 if (likely(!fd_errors)) 1290 return; 1291 1292 if (net_ratelimit()) 1293 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n", 1294 fd_errors); 1295 1296 percpu_stats = this_cpu_ptr(priv->percpu_stats); 1297 /* Tx-conf logically pertains to the egress path. */ 1298 percpu_stats->tx_errors++; 1299 } 1300 1301 static int dpaa2_eth_set_rx_vlan_filtering(struct dpaa2_eth_priv *priv, 1302 bool enable) 1303 { 1304 int err; 1305 1306 err = dpni_enable_vlan_filter(priv->mc_io, 0, priv->mc_token, enable); 1307 1308 if (err) { 1309 netdev_err(priv->net_dev, 1310 "dpni_enable_vlan_filter failed\n"); 1311 return err; 1312 } 1313 1314 return 0; 1315 } 1316 1317 static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable) 1318 { 1319 int err; 1320 1321 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 1322 DPNI_OFF_RX_L3_CSUM, enable); 1323 if (err) { 1324 netdev_err(priv->net_dev, 1325 "dpni_set_offload(RX_L3_CSUM) failed\n"); 1326 return err; 1327 } 1328 1329 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 1330 DPNI_OFF_RX_L4_CSUM, enable); 1331 if (err) { 1332 netdev_err(priv->net_dev, 1333 "dpni_set_offload(RX_L4_CSUM) failed\n"); 1334 return err; 1335 } 1336 1337 return 0; 1338 } 1339 1340 static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable) 1341 { 1342 int err; 1343 1344 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 1345 DPNI_OFF_TX_L3_CSUM, enable); 1346 if (err) { 1347 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n"); 1348 return err; 1349 } 1350 1351 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 1352 DPNI_OFF_TX_L4_CSUM, enable); 1353 if (err) { 1354 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n"); 1355 return err; 1356 } 1357 1358 return 0; 1359 } 1360 1361 /* Perform a single release command to add buffers 1362 * to the specified buffer pool 1363 */ 1364 static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv, 1365 struct dpaa2_eth_channel *ch, u16 bpid) 1366 { 1367 struct device *dev = priv->net_dev->dev.parent; 1368 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD]; 1369 struct page *page; 1370 dma_addr_t addr; 1371 int retries = 0; 1372 int i, err; 1373 1374 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) { 1375 /* Allocate buffer visible to WRIOP + skb shared info + 1376 * alignment padding 1377 */ 1378 /* allocate one page for each Rx buffer. WRIOP sees 1379 * the entire page except for a tailroom reserved for 1380 * skb shared info 1381 */ 1382 page = dev_alloc_pages(0); 1383 if (!page) 1384 goto err_alloc; 1385 1386 addr = dma_map_page(dev, page, 0, priv->rx_buf_size, 1387 DMA_BIDIRECTIONAL); 1388 if (unlikely(dma_mapping_error(dev, addr))) 1389 goto err_map; 1390 1391 buf_array[i] = addr; 1392 1393 /* tracing point */ 1394 trace_dpaa2_eth_buf_seed(priv->net_dev, 1395 page, DPAA2_ETH_RX_BUF_RAW_SIZE, 1396 addr, priv->rx_buf_size, 1397 bpid); 1398 } 1399 1400 release_bufs: 1401 /* In case the portal is busy, retry until successful */ 1402 while ((err = dpaa2_io_service_release(ch->dpio, bpid, 1403 buf_array, i)) == -EBUSY) { 1404 if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) 1405 break; 1406 cpu_relax(); 1407 } 1408 1409 /* If release command failed, clean up and bail out; 1410 * not much else we can do about it 1411 */ 1412 if (err) { 1413 dpaa2_eth_free_bufs(priv, buf_array, i); 1414 return 0; 1415 } 1416 1417 return i; 1418 1419 err_map: 1420 __free_pages(page, 0); 1421 err_alloc: 1422 /* If we managed to allocate at least some buffers, 1423 * release them to hardware 1424 */ 1425 if (i) 1426 goto release_bufs; 1427 1428 return 0; 1429 } 1430 1431 static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid) 1432 { 1433 int i, j; 1434 int new_count; 1435 1436 for (j = 0; j < priv->num_channels; j++) { 1437 for (i = 0; i < DPAA2_ETH_NUM_BUFS; 1438 i += DPAA2_ETH_BUFS_PER_CMD) { 1439 new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid); 1440 priv->channel[j]->buf_count += new_count; 1441 1442 if (new_count < DPAA2_ETH_BUFS_PER_CMD) { 1443 return -ENOMEM; 1444 } 1445 } 1446 } 1447 1448 return 0; 1449 } 1450 1451 /* 1452 * Drain the specified number of buffers from the DPNI's private buffer pool. 1453 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD 1454 */ 1455 static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count) 1456 { 1457 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD]; 1458 int retries = 0; 1459 int ret; 1460 1461 do { 1462 ret = dpaa2_io_service_acquire(NULL, priv->bpid, 1463 buf_array, count); 1464 if (ret < 0) { 1465 if (ret == -EBUSY && 1466 retries++ < DPAA2_ETH_SWP_BUSY_RETRIES) 1467 continue; 1468 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n"); 1469 return; 1470 } 1471 dpaa2_eth_free_bufs(priv, buf_array, ret); 1472 retries = 0; 1473 } while (ret); 1474 } 1475 1476 static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv) 1477 { 1478 int i; 1479 1480 dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD); 1481 dpaa2_eth_drain_bufs(priv, 1); 1482 1483 for (i = 0; i < priv->num_channels; i++) 1484 priv->channel[i]->buf_count = 0; 1485 } 1486 1487 /* Function is called from softirq context only, so we don't need to guard 1488 * the access to percpu count 1489 */ 1490 static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv, 1491 struct dpaa2_eth_channel *ch, 1492 u16 bpid) 1493 { 1494 int new_count; 1495 1496 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH)) 1497 return 0; 1498 1499 do { 1500 new_count = dpaa2_eth_add_bufs(priv, ch, bpid); 1501 if (unlikely(!new_count)) { 1502 /* Out of memory; abort for now, we'll try later on */ 1503 break; 1504 } 1505 ch->buf_count += new_count; 1506 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS); 1507 1508 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS)) 1509 return -ENOMEM; 1510 1511 return 0; 1512 } 1513 1514 static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv) 1515 { 1516 struct dpaa2_eth_sgt_cache *sgt_cache; 1517 u16 count; 1518 int k, i; 1519 1520 for_each_possible_cpu(k) { 1521 sgt_cache = per_cpu_ptr(priv->sgt_cache, k); 1522 count = sgt_cache->count; 1523 1524 for (i = 0; i < count; i++) 1525 skb_free_frag(sgt_cache->buf[i]); 1526 sgt_cache->count = 0; 1527 } 1528 } 1529 1530 static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch) 1531 { 1532 int err; 1533 int dequeues = -1; 1534 1535 /* Retry while portal is busy */ 1536 do { 1537 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id, 1538 ch->store); 1539 dequeues++; 1540 cpu_relax(); 1541 } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES); 1542 1543 ch->stats.dequeue_portal_busy += dequeues; 1544 if (unlikely(err)) 1545 ch->stats.pull_err++; 1546 1547 return err; 1548 } 1549 1550 /* NAPI poll routine 1551 * 1552 * Frames are dequeued from the QMan channel associated with this NAPI context. 1553 * Rx, Tx confirmation and (if configured) Rx error frames all count 1554 * towards the NAPI budget. 1555 */ 1556 static int dpaa2_eth_poll(struct napi_struct *napi, int budget) 1557 { 1558 struct dpaa2_eth_channel *ch; 1559 struct dpaa2_eth_priv *priv; 1560 int rx_cleaned = 0, txconf_cleaned = 0; 1561 struct dpaa2_eth_fq *fq, *txc_fq = NULL; 1562 struct netdev_queue *nq; 1563 int store_cleaned, work_done; 1564 struct list_head rx_list; 1565 int retries = 0; 1566 u16 flowid; 1567 int err; 1568 1569 ch = container_of(napi, struct dpaa2_eth_channel, napi); 1570 ch->xdp.res = 0; 1571 priv = ch->priv; 1572 1573 INIT_LIST_HEAD(&rx_list); 1574 ch->rx_list = &rx_list; 1575 1576 do { 1577 err = dpaa2_eth_pull_channel(ch); 1578 if (unlikely(err)) 1579 break; 1580 1581 /* Refill pool if appropriate */ 1582 dpaa2_eth_refill_pool(priv, ch, priv->bpid); 1583 1584 store_cleaned = dpaa2_eth_consume_frames(ch, &fq); 1585 if (store_cleaned <= 0) 1586 break; 1587 if (fq->type == DPAA2_RX_FQ) { 1588 rx_cleaned += store_cleaned; 1589 flowid = fq->flowid; 1590 } else { 1591 txconf_cleaned += store_cleaned; 1592 /* We have a single Tx conf FQ on this channel */ 1593 txc_fq = fq; 1594 } 1595 1596 /* If we either consumed the whole NAPI budget with Rx frames 1597 * or we reached the Tx confirmations threshold, we're done. 1598 */ 1599 if (rx_cleaned >= budget || 1600 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) { 1601 work_done = budget; 1602 goto out; 1603 } 1604 } while (store_cleaned); 1605 1606 /* Update NET DIM with the values for this CDAN */ 1607 dpaa2_io_update_net_dim(ch->dpio, ch->stats.frames_per_cdan, 1608 ch->stats.bytes_per_cdan); 1609 ch->stats.frames_per_cdan = 0; 1610 ch->stats.bytes_per_cdan = 0; 1611 1612 /* We didn't consume the entire budget, so finish napi and 1613 * re-enable data availability notifications 1614 */ 1615 napi_complete_done(napi, rx_cleaned); 1616 do { 1617 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx); 1618 cpu_relax(); 1619 } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES); 1620 WARN_ONCE(err, "CDAN notifications rearm failed on core %d", 1621 ch->nctx.desired_cpu); 1622 1623 work_done = max(rx_cleaned, 1); 1624 1625 out: 1626 netif_receive_skb_list(ch->rx_list); 1627 1628 if (txc_fq && txc_fq->dq_frames) { 1629 nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid); 1630 netdev_tx_completed_queue(nq, txc_fq->dq_frames, 1631 txc_fq->dq_bytes); 1632 txc_fq->dq_frames = 0; 1633 txc_fq->dq_bytes = 0; 1634 } 1635 1636 if (ch->xdp.res & XDP_REDIRECT) 1637 xdp_do_flush_map(); 1638 else if (rx_cleaned && ch->xdp.res & XDP_TX) 1639 dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]); 1640 1641 return work_done; 1642 } 1643 1644 static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv) 1645 { 1646 struct dpaa2_eth_channel *ch; 1647 int i; 1648 1649 for (i = 0; i < priv->num_channels; i++) { 1650 ch = priv->channel[i]; 1651 napi_enable(&ch->napi); 1652 } 1653 } 1654 1655 static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv) 1656 { 1657 struct dpaa2_eth_channel *ch; 1658 int i; 1659 1660 for (i = 0; i < priv->num_channels; i++) { 1661 ch = priv->channel[i]; 1662 napi_disable(&ch->napi); 1663 } 1664 } 1665 1666 void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv, 1667 bool tx_pause, bool pfc) 1668 { 1669 struct dpni_taildrop td = {0}; 1670 struct dpaa2_eth_fq *fq; 1671 int i, err; 1672 1673 /* FQ taildrop: threshold is in bytes, per frame queue. Enabled if 1674 * flow control is disabled (as it might interfere with either the 1675 * buffer pool depletion trigger for pause frames or with the group 1676 * congestion trigger for PFC frames) 1677 */ 1678 td.enable = !tx_pause; 1679 if (priv->rx_fqtd_enabled == td.enable) 1680 goto set_cgtd; 1681 1682 td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH; 1683 td.units = DPNI_CONGESTION_UNIT_BYTES; 1684 1685 for (i = 0; i < priv->num_fqs; i++) { 1686 fq = &priv->fq[i]; 1687 if (fq->type != DPAA2_RX_FQ) 1688 continue; 1689 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, 1690 DPNI_CP_QUEUE, DPNI_QUEUE_RX, 1691 fq->tc, fq->flowid, &td); 1692 if (err) { 1693 netdev_err(priv->net_dev, 1694 "dpni_set_taildrop(FQ) failed\n"); 1695 return; 1696 } 1697 } 1698 1699 priv->rx_fqtd_enabled = td.enable; 1700 1701 set_cgtd: 1702 /* Congestion group taildrop: threshold is in frames, per group 1703 * of FQs belonging to the same traffic class 1704 * Enabled if general Tx pause disabled or if PFCs are enabled 1705 * (congestion group threhsold for PFC generation is lower than the 1706 * CG taildrop threshold, so it won't interfere with it; we also 1707 * want frames in non-PFC enabled traffic classes to be kept in check) 1708 */ 1709 td.enable = !tx_pause || pfc; 1710 if (priv->rx_cgtd_enabled == td.enable) 1711 return; 1712 1713 td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv); 1714 td.units = DPNI_CONGESTION_UNIT_FRAMES; 1715 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) { 1716 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, 1717 DPNI_CP_GROUP, DPNI_QUEUE_RX, 1718 i, 0, &td); 1719 if (err) { 1720 netdev_err(priv->net_dev, 1721 "dpni_set_taildrop(CG) failed\n"); 1722 return; 1723 } 1724 } 1725 1726 priv->rx_cgtd_enabled = td.enable; 1727 } 1728 1729 static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv) 1730 { 1731 struct dpni_link_state state = {0}; 1732 bool tx_pause; 1733 int err; 1734 1735 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state); 1736 if (unlikely(err)) { 1737 netdev_err(priv->net_dev, 1738 "dpni_get_link_state() failed\n"); 1739 return err; 1740 } 1741 1742 /* If Tx pause frame settings have changed, we need to update 1743 * Rx FQ taildrop configuration as well. We configure taildrop 1744 * only when pause frame generation is disabled. 1745 */ 1746 tx_pause = dpaa2_eth_tx_pause_enabled(state.options); 1747 dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled); 1748 1749 /* When we manage the MAC/PHY using phylink there is no need 1750 * to manually update the netif_carrier. 1751 */ 1752 if (dpaa2_eth_is_type_phy(priv)) 1753 goto out; 1754 1755 /* Chech link state; speed / duplex changes are not treated yet */ 1756 if (priv->link_state.up == state.up) 1757 goto out; 1758 1759 if (state.up) { 1760 netif_carrier_on(priv->net_dev); 1761 netif_tx_start_all_queues(priv->net_dev); 1762 } else { 1763 netif_tx_stop_all_queues(priv->net_dev); 1764 netif_carrier_off(priv->net_dev); 1765 } 1766 1767 netdev_info(priv->net_dev, "Link Event: state %s\n", 1768 state.up ? "up" : "down"); 1769 1770 out: 1771 priv->link_state = state; 1772 1773 return 0; 1774 } 1775 1776 static int dpaa2_eth_open(struct net_device *net_dev) 1777 { 1778 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1779 int err; 1780 1781 err = dpaa2_eth_seed_pool(priv, priv->bpid); 1782 if (err) { 1783 /* Not much to do; the buffer pool, though not filled up, 1784 * may still contain some buffers which would enable us 1785 * to limp on. 1786 */ 1787 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n", 1788 priv->dpbp_dev->obj_desc.id, priv->bpid); 1789 } 1790 1791 if (!dpaa2_eth_is_type_phy(priv)) { 1792 /* We'll only start the txqs when the link is actually ready; 1793 * make sure we don't race against the link up notification, 1794 * which may come immediately after dpni_enable(); 1795 */ 1796 netif_tx_stop_all_queues(net_dev); 1797 1798 /* Also, explicitly set carrier off, otherwise 1799 * netif_carrier_ok() will return true and cause 'ip link show' 1800 * to report the LOWER_UP flag, even though the link 1801 * notification wasn't even received. 1802 */ 1803 netif_carrier_off(net_dev); 1804 } 1805 dpaa2_eth_enable_ch_napi(priv); 1806 1807 err = dpni_enable(priv->mc_io, 0, priv->mc_token); 1808 if (err < 0) { 1809 netdev_err(net_dev, "dpni_enable() failed\n"); 1810 goto enable_err; 1811 } 1812 1813 if (dpaa2_eth_is_type_phy(priv)) 1814 phylink_start(priv->mac->phylink); 1815 1816 return 0; 1817 1818 enable_err: 1819 dpaa2_eth_disable_ch_napi(priv); 1820 dpaa2_eth_drain_pool(priv); 1821 return err; 1822 } 1823 1824 /* Total number of in-flight frames on ingress queues */ 1825 static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv) 1826 { 1827 struct dpaa2_eth_fq *fq; 1828 u32 fcnt = 0, bcnt = 0, total = 0; 1829 int i, err; 1830 1831 for (i = 0; i < priv->num_fqs; i++) { 1832 fq = &priv->fq[i]; 1833 err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt); 1834 if (err) { 1835 netdev_warn(priv->net_dev, "query_fq_count failed"); 1836 break; 1837 } 1838 total += fcnt; 1839 } 1840 1841 return total; 1842 } 1843 1844 static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv) 1845 { 1846 int retries = 10; 1847 u32 pending; 1848 1849 do { 1850 pending = dpaa2_eth_ingress_fq_count(priv); 1851 if (pending) 1852 msleep(100); 1853 } while (pending && --retries); 1854 } 1855 1856 #define DPNI_TX_PENDING_VER_MAJOR 7 1857 #define DPNI_TX_PENDING_VER_MINOR 13 1858 static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv) 1859 { 1860 union dpni_statistics stats; 1861 int retries = 10; 1862 int err; 1863 1864 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR, 1865 DPNI_TX_PENDING_VER_MINOR) < 0) 1866 goto out; 1867 1868 do { 1869 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6, 1870 &stats); 1871 if (err) 1872 goto out; 1873 if (stats.page_6.tx_pending_frames == 0) 1874 return; 1875 } while (--retries); 1876 1877 out: 1878 msleep(500); 1879 } 1880 1881 static int dpaa2_eth_stop(struct net_device *net_dev) 1882 { 1883 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1884 int dpni_enabled = 0; 1885 int retries = 10; 1886 1887 if (dpaa2_eth_is_type_phy(priv)) { 1888 phylink_stop(priv->mac->phylink); 1889 } else { 1890 netif_tx_stop_all_queues(net_dev); 1891 netif_carrier_off(net_dev); 1892 } 1893 1894 /* On dpni_disable(), the MC firmware will: 1895 * - stop MAC Rx and wait for all Rx frames to be enqueued to software 1896 * - cut off WRIOP dequeues from egress FQs and wait until transmission 1897 * of all in flight Tx frames is finished (and corresponding Tx conf 1898 * frames are enqueued back to software) 1899 * 1900 * Before calling dpni_disable(), we wait for all Tx frames to arrive 1901 * on WRIOP. After it finishes, wait until all remaining frames on Rx 1902 * and Tx conf queues are consumed on NAPI poll. 1903 */ 1904 dpaa2_eth_wait_for_egress_fq_empty(priv); 1905 1906 do { 1907 dpni_disable(priv->mc_io, 0, priv->mc_token); 1908 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled); 1909 if (dpni_enabled) 1910 /* Allow the hardware some slack */ 1911 msleep(100); 1912 } while (dpni_enabled && --retries); 1913 if (!retries) { 1914 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n"); 1915 /* Must go on and disable NAPI nonetheless, so we don't crash at 1916 * the next "ifconfig up" 1917 */ 1918 } 1919 1920 dpaa2_eth_wait_for_ingress_fq_empty(priv); 1921 dpaa2_eth_disable_ch_napi(priv); 1922 1923 /* Empty the buffer pool */ 1924 dpaa2_eth_drain_pool(priv); 1925 1926 /* Empty the Scatter-Gather Buffer cache */ 1927 dpaa2_eth_sgt_cache_drain(priv); 1928 1929 return 0; 1930 } 1931 1932 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr) 1933 { 1934 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1935 struct device *dev = net_dev->dev.parent; 1936 int err; 1937 1938 err = eth_mac_addr(net_dev, addr); 1939 if (err < 0) { 1940 dev_err(dev, "eth_mac_addr() failed (%d)\n", err); 1941 return err; 1942 } 1943 1944 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token, 1945 net_dev->dev_addr); 1946 if (err) { 1947 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err); 1948 return err; 1949 } 1950 1951 return 0; 1952 } 1953 1954 /** Fill in counters maintained by the GPP driver. These may be different from 1955 * the hardware counters obtained by ethtool. 1956 */ 1957 static void dpaa2_eth_get_stats(struct net_device *net_dev, 1958 struct rtnl_link_stats64 *stats) 1959 { 1960 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1961 struct rtnl_link_stats64 *percpu_stats; 1962 u64 *cpustats; 1963 u64 *netstats = (u64 *)stats; 1964 int i, j; 1965 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64); 1966 1967 for_each_possible_cpu(i) { 1968 percpu_stats = per_cpu_ptr(priv->percpu_stats, i); 1969 cpustats = (u64 *)percpu_stats; 1970 for (j = 0; j < num; j++) 1971 netstats[j] += cpustats[j]; 1972 } 1973 } 1974 1975 /* Copy mac unicast addresses from @net_dev to @priv. 1976 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable. 1977 */ 1978 static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev, 1979 struct dpaa2_eth_priv *priv) 1980 { 1981 struct netdev_hw_addr *ha; 1982 int err; 1983 1984 netdev_for_each_uc_addr(ha, net_dev) { 1985 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, 1986 ha->addr); 1987 if (err) 1988 netdev_warn(priv->net_dev, 1989 "Could not add ucast MAC %pM to the filtering table (err %d)\n", 1990 ha->addr, err); 1991 } 1992 } 1993 1994 /* Copy mac multicast addresses from @net_dev to @priv 1995 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable. 1996 */ 1997 static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev, 1998 struct dpaa2_eth_priv *priv) 1999 { 2000 struct netdev_hw_addr *ha; 2001 int err; 2002 2003 netdev_for_each_mc_addr(ha, net_dev) { 2004 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, 2005 ha->addr); 2006 if (err) 2007 netdev_warn(priv->net_dev, 2008 "Could not add mcast MAC %pM to the filtering table (err %d)\n", 2009 ha->addr, err); 2010 } 2011 } 2012 2013 static int dpaa2_eth_rx_add_vid(struct net_device *net_dev, 2014 __be16 vlan_proto, u16 vid) 2015 { 2016 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2017 int err; 2018 2019 err = dpni_add_vlan_id(priv->mc_io, 0, priv->mc_token, 2020 vid, 0, 0, 0); 2021 2022 if (err) { 2023 netdev_warn(priv->net_dev, 2024 "Could not add the vlan id %u\n", 2025 vid); 2026 return err; 2027 } 2028 2029 return 0; 2030 } 2031 2032 static int dpaa2_eth_rx_kill_vid(struct net_device *net_dev, 2033 __be16 vlan_proto, u16 vid) 2034 { 2035 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2036 int err; 2037 2038 err = dpni_remove_vlan_id(priv->mc_io, 0, priv->mc_token, vid); 2039 2040 if (err) { 2041 netdev_warn(priv->net_dev, 2042 "Could not remove the vlan id %u\n", 2043 vid); 2044 return err; 2045 } 2046 2047 return 0; 2048 } 2049 2050 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev) 2051 { 2052 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2053 int uc_count = netdev_uc_count(net_dev); 2054 int mc_count = netdev_mc_count(net_dev); 2055 u8 max_mac = priv->dpni_attrs.mac_filter_entries; 2056 u32 options = priv->dpni_attrs.options; 2057 u16 mc_token = priv->mc_token; 2058 struct fsl_mc_io *mc_io = priv->mc_io; 2059 int err; 2060 2061 /* Basic sanity checks; these probably indicate a misconfiguration */ 2062 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0) 2063 netdev_info(net_dev, 2064 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n", 2065 max_mac); 2066 2067 /* Force promiscuous if the uc or mc counts exceed our capabilities. */ 2068 if (uc_count > max_mac) { 2069 netdev_info(net_dev, 2070 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n", 2071 uc_count, max_mac); 2072 goto force_promisc; 2073 } 2074 if (mc_count + uc_count > max_mac) { 2075 netdev_info(net_dev, 2076 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n", 2077 uc_count + mc_count, max_mac); 2078 goto force_mc_promisc; 2079 } 2080 2081 /* Adjust promisc settings due to flag combinations */ 2082 if (net_dev->flags & IFF_PROMISC) 2083 goto force_promisc; 2084 if (net_dev->flags & IFF_ALLMULTI) { 2085 /* First, rebuild unicast filtering table. This should be done 2086 * in promisc mode, in order to avoid frame loss while we 2087 * progressively add entries to the table. 2088 * We don't know whether we had been in promisc already, and 2089 * making an MC call to find out is expensive; so set uc promisc 2090 * nonetheless. 2091 */ 2092 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); 2093 if (err) 2094 netdev_warn(net_dev, "Can't set uc promisc\n"); 2095 2096 /* Actual uc table reconstruction. */ 2097 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0); 2098 if (err) 2099 netdev_warn(net_dev, "Can't clear uc filters\n"); 2100 dpaa2_eth_add_uc_hw_addr(net_dev, priv); 2101 2102 /* Finally, clear uc promisc and set mc promisc as requested. */ 2103 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0); 2104 if (err) 2105 netdev_warn(net_dev, "Can't clear uc promisc\n"); 2106 goto force_mc_promisc; 2107 } 2108 2109 /* Neither unicast, nor multicast promisc will be on... eventually. 2110 * For now, rebuild mac filtering tables while forcing both of them on. 2111 */ 2112 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); 2113 if (err) 2114 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err); 2115 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1); 2116 if (err) 2117 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err); 2118 2119 /* Actual mac filtering tables reconstruction */ 2120 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1); 2121 if (err) 2122 netdev_warn(net_dev, "Can't clear mac filters\n"); 2123 dpaa2_eth_add_mc_hw_addr(net_dev, priv); 2124 dpaa2_eth_add_uc_hw_addr(net_dev, priv); 2125 2126 /* Now we can clear both ucast and mcast promisc, without risking 2127 * to drop legitimate frames anymore. 2128 */ 2129 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0); 2130 if (err) 2131 netdev_warn(net_dev, "Can't clear ucast promisc\n"); 2132 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0); 2133 if (err) 2134 netdev_warn(net_dev, "Can't clear mcast promisc\n"); 2135 2136 return; 2137 2138 force_promisc: 2139 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); 2140 if (err) 2141 netdev_warn(net_dev, "Can't set ucast promisc\n"); 2142 force_mc_promisc: 2143 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1); 2144 if (err) 2145 netdev_warn(net_dev, "Can't set mcast promisc\n"); 2146 } 2147 2148 static int dpaa2_eth_set_features(struct net_device *net_dev, 2149 netdev_features_t features) 2150 { 2151 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2152 netdev_features_t changed = features ^ net_dev->features; 2153 bool enable; 2154 int err; 2155 2156 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) { 2157 enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER); 2158 err = dpaa2_eth_set_rx_vlan_filtering(priv, enable); 2159 if (err) 2160 return err; 2161 } 2162 2163 if (changed & NETIF_F_RXCSUM) { 2164 enable = !!(features & NETIF_F_RXCSUM); 2165 err = dpaa2_eth_set_rx_csum(priv, enable); 2166 if (err) 2167 return err; 2168 } 2169 2170 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) { 2171 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)); 2172 err = dpaa2_eth_set_tx_csum(priv, enable); 2173 if (err) 2174 return err; 2175 } 2176 2177 return 0; 2178 } 2179 2180 static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 2181 { 2182 struct dpaa2_eth_priv *priv = netdev_priv(dev); 2183 struct hwtstamp_config config; 2184 2185 if (!dpaa2_ptp) 2186 return -EINVAL; 2187 2188 if (copy_from_user(&config, rq->ifr_data, sizeof(config))) 2189 return -EFAULT; 2190 2191 switch (config.tx_type) { 2192 case HWTSTAMP_TX_OFF: 2193 case HWTSTAMP_TX_ON: 2194 case HWTSTAMP_TX_ONESTEP_SYNC: 2195 priv->tx_tstamp_type = config.tx_type; 2196 break; 2197 default: 2198 return -ERANGE; 2199 } 2200 2201 if (config.rx_filter == HWTSTAMP_FILTER_NONE) { 2202 priv->rx_tstamp = false; 2203 } else { 2204 priv->rx_tstamp = true; 2205 /* TS is set for all frame types, not only those requested */ 2206 config.rx_filter = HWTSTAMP_FILTER_ALL; 2207 } 2208 2209 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ? 2210 -EFAULT : 0; 2211 } 2212 2213 static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 2214 { 2215 struct dpaa2_eth_priv *priv = netdev_priv(dev); 2216 2217 if (cmd == SIOCSHWTSTAMP) 2218 return dpaa2_eth_ts_ioctl(dev, rq, cmd); 2219 2220 if (dpaa2_eth_is_type_phy(priv)) 2221 return phylink_mii_ioctl(priv->mac->phylink, rq, cmd); 2222 2223 return -EOPNOTSUPP; 2224 } 2225 2226 static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu) 2227 { 2228 int mfl, linear_mfl; 2229 2230 mfl = DPAA2_ETH_L2_MAX_FRM(mtu); 2231 linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE - 2232 dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM; 2233 2234 if (mfl > linear_mfl) { 2235 netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n", 2236 linear_mfl - VLAN_ETH_HLEN); 2237 return false; 2238 } 2239 2240 return true; 2241 } 2242 2243 static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp) 2244 { 2245 int mfl, err; 2246 2247 /* We enforce a maximum Rx frame length based on MTU only if we have 2248 * an XDP program attached (in order to avoid Rx S/G frames). 2249 * Otherwise, we accept all incoming frames as long as they are not 2250 * larger than maximum size supported in hardware 2251 */ 2252 if (has_xdp) 2253 mfl = DPAA2_ETH_L2_MAX_FRM(mtu); 2254 else 2255 mfl = DPAA2_ETH_MFL; 2256 2257 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl); 2258 if (err) { 2259 netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n"); 2260 return err; 2261 } 2262 2263 return 0; 2264 } 2265 2266 static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu) 2267 { 2268 struct dpaa2_eth_priv *priv = netdev_priv(dev); 2269 int err; 2270 2271 if (!priv->xdp_prog) 2272 goto out; 2273 2274 if (!xdp_mtu_valid(priv, new_mtu)) 2275 return -EINVAL; 2276 2277 err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true); 2278 if (err) 2279 return err; 2280 2281 out: 2282 dev->mtu = new_mtu; 2283 return 0; 2284 } 2285 2286 static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp) 2287 { 2288 struct dpni_buffer_layout buf_layout = {0}; 2289 int err; 2290 2291 err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token, 2292 DPNI_QUEUE_RX, &buf_layout); 2293 if (err) { 2294 netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n"); 2295 return err; 2296 } 2297 2298 /* Reserve extra headroom for XDP header size changes */ 2299 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) + 2300 (has_xdp ? XDP_PACKET_HEADROOM : 0); 2301 buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM; 2302 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 2303 DPNI_QUEUE_RX, &buf_layout); 2304 if (err) { 2305 netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n"); 2306 return err; 2307 } 2308 2309 return 0; 2310 } 2311 2312 static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog) 2313 { 2314 struct dpaa2_eth_priv *priv = netdev_priv(dev); 2315 struct dpaa2_eth_channel *ch; 2316 struct bpf_prog *old; 2317 bool up, need_update; 2318 int i, err; 2319 2320 if (prog && !xdp_mtu_valid(priv, dev->mtu)) 2321 return -EINVAL; 2322 2323 if (prog) 2324 bpf_prog_add(prog, priv->num_channels); 2325 2326 up = netif_running(dev); 2327 need_update = (!!priv->xdp_prog != !!prog); 2328 2329 if (up) 2330 dpaa2_eth_stop(dev); 2331 2332 /* While in xdp mode, enforce a maximum Rx frame size based on MTU. 2333 * Also, when switching between xdp/non-xdp modes we need to reconfigure 2334 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop, 2335 * so we are sure no old format buffers will be used from now on. 2336 */ 2337 if (need_update) { 2338 err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog); 2339 if (err) 2340 goto out_err; 2341 err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog); 2342 if (err) 2343 goto out_err; 2344 } 2345 2346 old = xchg(&priv->xdp_prog, prog); 2347 if (old) 2348 bpf_prog_put(old); 2349 2350 for (i = 0; i < priv->num_channels; i++) { 2351 ch = priv->channel[i]; 2352 old = xchg(&ch->xdp.prog, prog); 2353 if (old) 2354 bpf_prog_put(old); 2355 } 2356 2357 if (up) { 2358 err = dpaa2_eth_open(dev); 2359 if (err) 2360 return err; 2361 } 2362 2363 return 0; 2364 2365 out_err: 2366 if (prog) 2367 bpf_prog_sub(prog, priv->num_channels); 2368 if (up) 2369 dpaa2_eth_open(dev); 2370 2371 return err; 2372 } 2373 2374 static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp) 2375 { 2376 switch (xdp->command) { 2377 case XDP_SETUP_PROG: 2378 return dpaa2_eth_setup_xdp(dev, xdp->prog); 2379 default: 2380 return -EINVAL; 2381 } 2382 2383 return 0; 2384 } 2385 2386 static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev, 2387 struct xdp_frame *xdpf, 2388 struct dpaa2_fd *fd) 2389 { 2390 struct device *dev = net_dev->dev.parent; 2391 unsigned int needed_headroom; 2392 struct dpaa2_eth_swa *swa; 2393 void *buffer_start, *aligned_start; 2394 dma_addr_t addr; 2395 2396 /* We require a minimum headroom to be able to transmit the frame. 2397 * Otherwise return an error and let the original net_device handle it 2398 */ 2399 needed_headroom = dpaa2_eth_needed_headroom(NULL); 2400 if (xdpf->headroom < needed_headroom) 2401 return -EINVAL; 2402 2403 /* Setup the FD fields */ 2404 memset(fd, 0, sizeof(*fd)); 2405 2406 /* Align FD address, if possible */ 2407 buffer_start = xdpf->data - needed_headroom; 2408 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN, 2409 DPAA2_ETH_TX_BUF_ALIGN); 2410 if (aligned_start >= xdpf->data - xdpf->headroom) 2411 buffer_start = aligned_start; 2412 2413 swa = (struct dpaa2_eth_swa *)buffer_start; 2414 /* fill in necessary fields here */ 2415 swa->type = DPAA2_ETH_SWA_XDP; 2416 swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start; 2417 swa->xdp.xdpf = xdpf; 2418 2419 addr = dma_map_single(dev, buffer_start, 2420 swa->xdp.dma_size, 2421 DMA_BIDIRECTIONAL); 2422 if (unlikely(dma_mapping_error(dev, addr))) 2423 return -ENOMEM; 2424 2425 dpaa2_fd_set_addr(fd, addr); 2426 dpaa2_fd_set_offset(fd, xdpf->data - buffer_start); 2427 dpaa2_fd_set_len(fd, xdpf->len); 2428 dpaa2_fd_set_format(fd, dpaa2_fd_single); 2429 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA); 2430 2431 return 0; 2432 } 2433 2434 static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n, 2435 struct xdp_frame **frames, u32 flags) 2436 { 2437 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2438 struct dpaa2_eth_xdp_fds *xdp_redirect_fds; 2439 struct rtnl_link_stats64 *percpu_stats; 2440 struct dpaa2_eth_fq *fq; 2441 struct dpaa2_fd *fds; 2442 int enqueued, i, err; 2443 2444 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 2445 return -EINVAL; 2446 2447 if (!netif_running(net_dev)) 2448 return -ENETDOWN; 2449 2450 fq = &priv->fq[smp_processor_id()]; 2451 xdp_redirect_fds = &fq->xdp_redirect_fds; 2452 fds = xdp_redirect_fds->fds; 2453 2454 percpu_stats = this_cpu_ptr(priv->percpu_stats); 2455 2456 /* create a FD for each xdp_frame in the list received */ 2457 for (i = 0; i < n; i++) { 2458 err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]); 2459 if (err) 2460 break; 2461 } 2462 xdp_redirect_fds->num = i; 2463 2464 /* enqueue all the frame descriptors */ 2465 enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds); 2466 2467 /* update statistics */ 2468 percpu_stats->tx_packets += enqueued; 2469 for (i = 0; i < enqueued; i++) 2470 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]); 2471 2472 return enqueued; 2473 } 2474 2475 static int update_xps(struct dpaa2_eth_priv *priv) 2476 { 2477 struct net_device *net_dev = priv->net_dev; 2478 struct cpumask xps_mask; 2479 struct dpaa2_eth_fq *fq; 2480 int i, num_queues, netdev_queues; 2481 int err = 0; 2482 2483 num_queues = dpaa2_eth_queue_count(priv); 2484 netdev_queues = (net_dev->num_tc ? : 1) * num_queues; 2485 2486 /* The first <num_queues> entries in priv->fq array are Tx/Tx conf 2487 * queues, so only process those 2488 */ 2489 for (i = 0; i < netdev_queues; i++) { 2490 fq = &priv->fq[i % num_queues]; 2491 2492 cpumask_clear(&xps_mask); 2493 cpumask_set_cpu(fq->target_cpu, &xps_mask); 2494 2495 err = netif_set_xps_queue(net_dev, &xps_mask, i); 2496 if (err) { 2497 netdev_warn_once(net_dev, "Error setting XPS queue\n"); 2498 break; 2499 } 2500 } 2501 2502 return err; 2503 } 2504 2505 static int dpaa2_eth_setup_mqprio(struct net_device *net_dev, 2506 struct tc_mqprio_qopt *mqprio) 2507 { 2508 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2509 u8 num_tc, num_queues; 2510 int i; 2511 2512 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 2513 num_queues = dpaa2_eth_queue_count(priv); 2514 num_tc = mqprio->num_tc; 2515 2516 if (num_tc == net_dev->num_tc) 2517 return 0; 2518 2519 if (num_tc > dpaa2_eth_tc_count(priv)) { 2520 netdev_err(net_dev, "Max %d traffic classes supported\n", 2521 dpaa2_eth_tc_count(priv)); 2522 return -EOPNOTSUPP; 2523 } 2524 2525 if (!num_tc) { 2526 netdev_reset_tc(net_dev); 2527 netif_set_real_num_tx_queues(net_dev, num_queues); 2528 goto out; 2529 } 2530 2531 netdev_set_num_tc(net_dev, num_tc); 2532 netif_set_real_num_tx_queues(net_dev, num_tc * num_queues); 2533 2534 for (i = 0; i < num_tc; i++) 2535 netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues); 2536 2537 out: 2538 update_xps(priv); 2539 2540 return 0; 2541 } 2542 2543 #define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8) 2544 2545 static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p) 2546 { 2547 struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params; 2548 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2549 struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 }; 2550 struct dpni_tx_shaping_cfg tx_er_shaper = { 0 }; 2551 int err; 2552 2553 if (p->command == TC_TBF_STATS) 2554 return -EOPNOTSUPP; 2555 2556 /* Only per port Tx shaping */ 2557 if (p->parent != TC_H_ROOT) 2558 return -EOPNOTSUPP; 2559 2560 if (p->command == TC_TBF_REPLACE) { 2561 if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) { 2562 netdev_err(net_dev, "burst size cannot be greater than %d\n", 2563 DPAA2_ETH_MAX_BURST_SIZE); 2564 return -EINVAL; 2565 } 2566 2567 tx_cr_shaper.max_burst_size = cfg->max_size; 2568 /* The TBF interface is in bytes/s, whereas DPAA2 expects the 2569 * rate in Mbits/s 2570 */ 2571 tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps); 2572 } 2573 2574 err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper, 2575 &tx_er_shaper, 0); 2576 if (err) { 2577 netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err); 2578 return err; 2579 } 2580 2581 return 0; 2582 } 2583 2584 static int dpaa2_eth_setup_tc(struct net_device *net_dev, 2585 enum tc_setup_type type, void *type_data) 2586 { 2587 switch (type) { 2588 case TC_SETUP_QDISC_MQPRIO: 2589 return dpaa2_eth_setup_mqprio(net_dev, type_data); 2590 case TC_SETUP_QDISC_TBF: 2591 return dpaa2_eth_setup_tbf(net_dev, type_data); 2592 default: 2593 return -EOPNOTSUPP; 2594 } 2595 } 2596 2597 static const struct net_device_ops dpaa2_eth_ops = { 2598 .ndo_open = dpaa2_eth_open, 2599 .ndo_start_xmit = dpaa2_eth_tx, 2600 .ndo_stop = dpaa2_eth_stop, 2601 .ndo_set_mac_address = dpaa2_eth_set_addr, 2602 .ndo_get_stats64 = dpaa2_eth_get_stats, 2603 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode, 2604 .ndo_set_features = dpaa2_eth_set_features, 2605 .ndo_eth_ioctl = dpaa2_eth_ioctl, 2606 .ndo_change_mtu = dpaa2_eth_change_mtu, 2607 .ndo_bpf = dpaa2_eth_xdp, 2608 .ndo_xdp_xmit = dpaa2_eth_xdp_xmit, 2609 .ndo_setup_tc = dpaa2_eth_setup_tc, 2610 .ndo_vlan_rx_add_vid = dpaa2_eth_rx_add_vid, 2611 .ndo_vlan_rx_kill_vid = dpaa2_eth_rx_kill_vid 2612 }; 2613 2614 static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx) 2615 { 2616 struct dpaa2_eth_channel *ch; 2617 2618 ch = container_of(ctx, struct dpaa2_eth_channel, nctx); 2619 2620 /* Update NAPI statistics */ 2621 ch->stats.cdan++; 2622 2623 napi_schedule(&ch->napi); 2624 } 2625 2626 /* Allocate and configure a DPCON object */ 2627 static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv) 2628 { 2629 struct fsl_mc_device *dpcon; 2630 struct device *dev = priv->net_dev->dev.parent; 2631 int err; 2632 2633 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), 2634 FSL_MC_POOL_DPCON, &dpcon); 2635 if (err) { 2636 if (err == -ENXIO) 2637 err = -EPROBE_DEFER; 2638 else 2639 dev_info(dev, "Not enough DPCONs, will go on as-is\n"); 2640 return ERR_PTR(err); 2641 } 2642 2643 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle); 2644 if (err) { 2645 dev_err(dev, "dpcon_open() failed\n"); 2646 goto free; 2647 } 2648 2649 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle); 2650 if (err) { 2651 dev_err(dev, "dpcon_reset() failed\n"); 2652 goto close; 2653 } 2654 2655 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle); 2656 if (err) { 2657 dev_err(dev, "dpcon_enable() failed\n"); 2658 goto close; 2659 } 2660 2661 return dpcon; 2662 2663 close: 2664 dpcon_close(priv->mc_io, 0, dpcon->mc_handle); 2665 free: 2666 fsl_mc_object_free(dpcon); 2667 2668 return ERR_PTR(err); 2669 } 2670 2671 static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv, 2672 struct fsl_mc_device *dpcon) 2673 { 2674 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle); 2675 dpcon_close(priv->mc_io, 0, dpcon->mc_handle); 2676 fsl_mc_object_free(dpcon); 2677 } 2678 2679 static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv) 2680 { 2681 struct dpaa2_eth_channel *channel; 2682 struct dpcon_attr attr; 2683 struct device *dev = priv->net_dev->dev.parent; 2684 int err; 2685 2686 channel = kzalloc(sizeof(*channel), GFP_KERNEL); 2687 if (!channel) 2688 return NULL; 2689 2690 channel->dpcon = dpaa2_eth_setup_dpcon(priv); 2691 if (IS_ERR(channel->dpcon)) { 2692 err = PTR_ERR(channel->dpcon); 2693 goto err_setup; 2694 } 2695 2696 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle, 2697 &attr); 2698 if (err) { 2699 dev_err(dev, "dpcon_get_attributes() failed\n"); 2700 goto err_get_attr; 2701 } 2702 2703 channel->dpcon_id = attr.id; 2704 channel->ch_id = attr.qbman_ch_id; 2705 channel->priv = priv; 2706 2707 return channel; 2708 2709 err_get_attr: 2710 dpaa2_eth_free_dpcon(priv, channel->dpcon); 2711 err_setup: 2712 kfree(channel); 2713 return ERR_PTR(err); 2714 } 2715 2716 static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv, 2717 struct dpaa2_eth_channel *channel) 2718 { 2719 dpaa2_eth_free_dpcon(priv, channel->dpcon); 2720 kfree(channel); 2721 } 2722 2723 /* DPIO setup: allocate and configure QBMan channels, setup core affinity 2724 * and register data availability notifications 2725 */ 2726 static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv) 2727 { 2728 struct dpaa2_io_notification_ctx *nctx; 2729 struct dpaa2_eth_channel *channel; 2730 struct dpcon_notification_cfg dpcon_notif_cfg; 2731 struct device *dev = priv->net_dev->dev.parent; 2732 int i, err; 2733 2734 /* We want the ability to spread ingress traffic (RX, TX conf) to as 2735 * many cores as possible, so we need one channel for each core 2736 * (unless there's fewer queues than cores, in which case the extra 2737 * channels would be wasted). 2738 * Allocate one channel per core and register it to the core's 2739 * affine DPIO. If not enough channels are available for all cores 2740 * or if some cores don't have an affine DPIO, there will be no 2741 * ingress frame processing on those cores. 2742 */ 2743 cpumask_clear(&priv->dpio_cpumask); 2744 for_each_online_cpu(i) { 2745 /* Try to allocate a channel */ 2746 channel = dpaa2_eth_alloc_channel(priv); 2747 if (IS_ERR_OR_NULL(channel)) { 2748 err = PTR_ERR_OR_ZERO(channel); 2749 if (err != -EPROBE_DEFER) 2750 dev_info(dev, 2751 "No affine channel for cpu %d and above\n", i); 2752 goto err_alloc_ch; 2753 } 2754 2755 priv->channel[priv->num_channels] = channel; 2756 2757 nctx = &channel->nctx; 2758 nctx->is_cdan = 1; 2759 nctx->cb = dpaa2_eth_cdan_cb; 2760 nctx->id = channel->ch_id; 2761 nctx->desired_cpu = i; 2762 2763 /* Register the new context */ 2764 channel->dpio = dpaa2_io_service_select(i); 2765 err = dpaa2_io_service_register(channel->dpio, nctx, dev); 2766 if (err) { 2767 dev_dbg(dev, "No affine DPIO for cpu %d\n", i); 2768 /* If no affine DPIO for this core, there's probably 2769 * none available for next cores either. Signal we want 2770 * to retry later, in case the DPIO devices weren't 2771 * probed yet. 2772 */ 2773 err = -EPROBE_DEFER; 2774 goto err_service_reg; 2775 } 2776 2777 /* Register DPCON notification with MC */ 2778 dpcon_notif_cfg.dpio_id = nctx->dpio_id; 2779 dpcon_notif_cfg.priority = 0; 2780 dpcon_notif_cfg.user_ctx = nctx->qman64; 2781 err = dpcon_set_notification(priv->mc_io, 0, 2782 channel->dpcon->mc_handle, 2783 &dpcon_notif_cfg); 2784 if (err) { 2785 dev_err(dev, "dpcon_set_notification failed()\n"); 2786 goto err_set_cdan; 2787 } 2788 2789 /* If we managed to allocate a channel and also found an affine 2790 * DPIO for this core, add it to the final mask 2791 */ 2792 cpumask_set_cpu(i, &priv->dpio_cpumask); 2793 priv->num_channels++; 2794 2795 /* Stop if we already have enough channels to accommodate all 2796 * RX and TX conf queues 2797 */ 2798 if (priv->num_channels == priv->dpni_attrs.num_queues) 2799 break; 2800 } 2801 2802 return 0; 2803 2804 err_set_cdan: 2805 dpaa2_io_service_deregister(channel->dpio, nctx, dev); 2806 err_service_reg: 2807 dpaa2_eth_free_channel(priv, channel); 2808 err_alloc_ch: 2809 if (err == -EPROBE_DEFER) { 2810 for (i = 0; i < priv->num_channels; i++) { 2811 channel = priv->channel[i]; 2812 nctx = &channel->nctx; 2813 dpaa2_io_service_deregister(channel->dpio, nctx, dev); 2814 dpaa2_eth_free_channel(priv, channel); 2815 } 2816 priv->num_channels = 0; 2817 return err; 2818 } 2819 2820 if (cpumask_empty(&priv->dpio_cpumask)) { 2821 dev_err(dev, "No cpu with an affine DPIO/DPCON\n"); 2822 return -ENODEV; 2823 } 2824 2825 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n", 2826 cpumask_pr_args(&priv->dpio_cpumask)); 2827 2828 return 0; 2829 } 2830 2831 static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv) 2832 { 2833 struct device *dev = priv->net_dev->dev.parent; 2834 struct dpaa2_eth_channel *ch; 2835 int i; 2836 2837 /* deregister CDAN notifications and free channels */ 2838 for (i = 0; i < priv->num_channels; i++) { 2839 ch = priv->channel[i]; 2840 dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev); 2841 dpaa2_eth_free_channel(priv, ch); 2842 } 2843 } 2844 2845 static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv, 2846 int cpu) 2847 { 2848 struct device *dev = priv->net_dev->dev.parent; 2849 int i; 2850 2851 for (i = 0; i < priv->num_channels; i++) 2852 if (priv->channel[i]->nctx.desired_cpu == cpu) 2853 return priv->channel[i]; 2854 2855 /* We should never get here. Issue a warning and return 2856 * the first channel, because it's still better than nothing 2857 */ 2858 dev_warn(dev, "No affine channel found for cpu %d\n", cpu); 2859 2860 return priv->channel[0]; 2861 } 2862 2863 static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv) 2864 { 2865 struct device *dev = priv->net_dev->dev.parent; 2866 struct dpaa2_eth_fq *fq; 2867 int rx_cpu, txc_cpu; 2868 int i; 2869 2870 /* For each FQ, pick one channel/CPU to deliver frames to. 2871 * This may well change at runtime, either through irqbalance or 2872 * through direct user intervention. 2873 */ 2874 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask); 2875 2876 for (i = 0; i < priv->num_fqs; i++) { 2877 fq = &priv->fq[i]; 2878 switch (fq->type) { 2879 case DPAA2_RX_FQ: 2880 case DPAA2_RX_ERR_FQ: 2881 fq->target_cpu = rx_cpu; 2882 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask); 2883 if (rx_cpu >= nr_cpu_ids) 2884 rx_cpu = cpumask_first(&priv->dpio_cpumask); 2885 break; 2886 case DPAA2_TX_CONF_FQ: 2887 fq->target_cpu = txc_cpu; 2888 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask); 2889 if (txc_cpu >= nr_cpu_ids) 2890 txc_cpu = cpumask_first(&priv->dpio_cpumask); 2891 break; 2892 default: 2893 dev_err(dev, "Unknown FQ type: %d\n", fq->type); 2894 } 2895 fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu); 2896 } 2897 2898 update_xps(priv); 2899 } 2900 2901 static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv) 2902 { 2903 int i, j; 2904 2905 /* We have one TxConf FQ per Tx flow. 2906 * The number of Tx and Rx queues is the same. 2907 * Tx queues come first in the fq array. 2908 */ 2909 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) { 2910 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ; 2911 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf; 2912 priv->fq[priv->num_fqs++].flowid = (u16)i; 2913 } 2914 2915 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) { 2916 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) { 2917 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ; 2918 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx; 2919 priv->fq[priv->num_fqs].tc = (u8)j; 2920 priv->fq[priv->num_fqs++].flowid = (u16)i; 2921 } 2922 } 2923 2924 /* We have exactly one Rx error queue per DPNI */ 2925 priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ; 2926 priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err; 2927 2928 /* For each FQ, decide on which core to process incoming frames */ 2929 dpaa2_eth_set_fq_affinity(priv); 2930 } 2931 2932 /* Allocate and configure one buffer pool for each interface */ 2933 static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv) 2934 { 2935 int err; 2936 struct fsl_mc_device *dpbp_dev; 2937 struct device *dev = priv->net_dev->dev.parent; 2938 struct dpbp_attr dpbp_attrs; 2939 2940 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP, 2941 &dpbp_dev); 2942 if (err) { 2943 if (err == -ENXIO) 2944 err = -EPROBE_DEFER; 2945 else 2946 dev_err(dev, "DPBP device allocation failed\n"); 2947 return err; 2948 } 2949 2950 priv->dpbp_dev = dpbp_dev; 2951 2952 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id, 2953 &dpbp_dev->mc_handle); 2954 if (err) { 2955 dev_err(dev, "dpbp_open() failed\n"); 2956 goto err_open; 2957 } 2958 2959 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle); 2960 if (err) { 2961 dev_err(dev, "dpbp_reset() failed\n"); 2962 goto err_reset; 2963 } 2964 2965 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle); 2966 if (err) { 2967 dev_err(dev, "dpbp_enable() failed\n"); 2968 goto err_enable; 2969 } 2970 2971 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle, 2972 &dpbp_attrs); 2973 if (err) { 2974 dev_err(dev, "dpbp_get_attributes() failed\n"); 2975 goto err_get_attr; 2976 } 2977 priv->bpid = dpbp_attrs.bpid; 2978 2979 return 0; 2980 2981 err_get_attr: 2982 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle); 2983 err_enable: 2984 err_reset: 2985 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle); 2986 err_open: 2987 fsl_mc_object_free(dpbp_dev); 2988 2989 return err; 2990 } 2991 2992 static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv) 2993 { 2994 dpaa2_eth_drain_pool(priv); 2995 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle); 2996 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle); 2997 fsl_mc_object_free(priv->dpbp_dev); 2998 } 2999 3000 static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv) 3001 { 3002 struct device *dev = priv->net_dev->dev.parent; 3003 struct dpni_buffer_layout buf_layout = {0}; 3004 u16 rx_buf_align; 3005 int err; 3006 3007 /* We need to check for WRIOP version 1.0.0, but depending on the MC 3008 * version, this number is not always provided correctly on rev1. 3009 * We need to check for both alternatives in this situation. 3010 */ 3011 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) || 3012 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0)) 3013 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1; 3014 else 3015 rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN; 3016 3017 /* We need to ensure that the buffer size seen by WRIOP is a multiple 3018 * of 64 or 256 bytes depending on the WRIOP version. 3019 */ 3020 priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align); 3021 3022 /* tx buffer */ 3023 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE; 3024 buf_layout.pass_timestamp = true; 3025 buf_layout.pass_frame_status = true; 3026 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE | 3027 DPNI_BUF_LAYOUT_OPT_TIMESTAMP | 3028 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS; 3029 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 3030 DPNI_QUEUE_TX, &buf_layout); 3031 if (err) { 3032 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n"); 3033 return err; 3034 } 3035 3036 /* tx-confirm buffer */ 3037 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP | 3038 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS; 3039 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 3040 DPNI_QUEUE_TX_CONFIRM, &buf_layout); 3041 if (err) { 3042 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n"); 3043 return err; 3044 } 3045 3046 /* Now that we've set our tx buffer layout, retrieve the minimum 3047 * required tx data offset. 3048 */ 3049 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token, 3050 &priv->tx_data_offset); 3051 if (err) { 3052 dev_err(dev, "dpni_get_tx_data_offset() failed\n"); 3053 return err; 3054 } 3055 3056 if ((priv->tx_data_offset % 64) != 0) 3057 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n", 3058 priv->tx_data_offset); 3059 3060 /* rx buffer */ 3061 buf_layout.pass_frame_status = true; 3062 buf_layout.pass_parser_result = true; 3063 buf_layout.data_align = rx_buf_align; 3064 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv); 3065 buf_layout.private_data_size = 0; 3066 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT | 3067 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS | 3068 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN | 3069 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM | 3070 DPNI_BUF_LAYOUT_OPT_TIMESTAMP; 3071 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 3072 DPNI_QUEUE_RX, &buf_layout); 3073 if (err) { 3074 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n"); 3075 return err; 3076 } 3077 3078 return 0; 3079 } 3080 3081 #define DPNI_ENQUEUE_FQID_VER_MAJOR 7 3082 #define DPNI_ENQUEUE_FQID_VER_MINOR 9 3083 3084 static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv, 3085 struct dpaa2_eth_fq *fq, 3086 struct dpaa2_fd *fd, u8 prio, 3087 u32 num_frames __always_unused, 3088 int *frames_enqueued) 3089 { 3090 int err; 3091 3092 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio, 3093 priv->tx_qdid, prio, 3094 fq->tx_qdbin, fd); 3095 if (!err && frames_enqueued) 3096 *frames_enqueued = 1; 3097 return err; 3098 } 3099 3100 static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv, 3101 struct dpaa2_eth_fq *fq, 3102 struct dpaa2_fd *fd, 3103 u8 prio, u32 num_frames, 3104 int *frames_enqueued) 3105 { 3106 int err; 3107 3108 err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio, 3109 fq->tx_fqid[prio], 3110 fd, num_frames); 3111 3112 if (err == 0) 3113 return -EBUSY; 3114 3115 if (frames_enqueued) 3116 *frames_enqueued = err; 3117 return 0; 3118 } 3119 3120 static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv) 3121 { 3122 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR, 3123 DPNI_ENQUEUE_FQID_VER_MINOR) < 0) 3124 priv->enqueue = dpaa2_eth_enqueue_qd; 3125 else 3126 priv->enqueue = dpaa2_eth_enqueue_fq_multiple; 3127 } 3128 3129 static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv) 3130 { 3131 struct device *dev = priv->net_dev->dev.parent; 3132 struct dpni_link_cfg link_cfg = {0}; 3133 int err; 3134 3135 /* Get the default link options so we don't override other flags */ 3136 err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg); 3137 if (err) { 3138 dev_err(dev, "dpni_get_link_cfg() failed\n"); 3139 return err; 3140 } 3141 3142 /* By default, enable both Rx and Tx pause frames */ 3143 link_cfg.options |= DPNI_LINK_OPT_PAUSE; 3144 link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE; 3145 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg); 3146 if (err) { 3147 dev_err(dev, "dpni_set_link_cfg() failed\n"); 3148 return err; 3149 } 3150 3151 priv->link_state.options = link_cfg.options; 3152 3153 return 0; 3154 } 3155 3156 static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv) 3157 { 3158 struct dpni_queue_id qid = {0}; 3159 struct dpaa2_eth_fq *fq; 3160 struct dpni_queue queue; 3161 int i, j, err; 3162 3163 /* We only use Tx FQIDs for FQID-based enqueue, so check 3164 * if DPNI version supports it before updating FQIDs 3165 */ 3166 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR, 3167 DPNI_ENQUEUE_FQID_VER_MINOR) < 0) 3168 return; 3169 3170 for (i = 0; i < priv->num_fqs; i++) { 3171 fq = &priv->fq[i]; 3172 if (fq->type != DPAA2_TX_CONF_FQ) 3173 continue; 3174 for (j = 0; j < dpaa2_eth_tc_count(priv); j++) { 3175 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 3176 DPNI_QUEUE_TX, j, fq->flowid, 3177 &queue, &qid); 3178 if (err) 3179 goto out_err; 3180 3181 fq->tx_fqid[j] = qid.fqid; 3182 if (fq->tx_fqid[j] == 0) 3183 goto out_err; 3184 } 3185 } 3186 3187 priv->enqueue = dpaa2_eth_enqueue_fq_multiple; 3188 3189 return; 3190 3191 out_err: 3192 netdev_info(priv->net_dev, 3193 "Error reading Tx FQID, fallback to QDID-based enqueue\n"); 3194 priv->enqueue = dpaa2_eth_enqueue_qd; 3195 } 3196 3197 /* Configure ingress classification based on VLAN PCP */ 3198 static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv) 3199 { 3200 struct device *dev = priv->net_dev->dev.parent; 3201 struct dpkg_profile_cfg kg_cfg = {0}; 3202 struct dpni_qos_tbl_cfg qos_cfg = {0}; 3203 struct dpni_rule_cfg key_params; 3204 void *dma_mem, *key, *mask; 3205 u8 key_size = 2; /* VLAN TCI field */ 3206 int i, pcp, err; 3207 3208 /* VLAN-based classification only makes sense if we have multiple 3209 * traffic classes. 3210 * Also, we need to extract just the 3-bit PCP field from the VLAN 3211 * header and we can only do that by using a mask 3212 */ 3213 if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) { 3214 dev_dbg(dev, "VLAN-based QoS classification not supported\n"); 3215 return -EOPNOTSUPP; 3216 } 3217 3218 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL); 3219 if (!dma_mem) 3220 return -ENOMEM; 3221 3222 kg_cfg.num_extracts = 1; 3223 kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR; 3224 kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN; 3225 kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD; 3226 kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI; 3227 3228 err = dpni_prepare_key_cfg(&kg_cfg, dma_mem); 3229 if (err) { 3230 dev_err(dev, "dpni_prepare_key_cfg failed\n"); 3231 goto out_free_tbl; 3232 } 3233 3234 /* set QoS table */ 3235 qos_cfg.default_tc = 0; 3236 qos_cfg.discard_on_miss = 0; 3237 qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem, 3238 DPAA2_CLASSIFIER_DMA_SIZE, 3239 DMA_TO_DEVICE); 3240 if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) { 3241 dev_err(dev, "QoS table DMA mapping failed\n"); 3242 err = -ENOMEM; 3243 goto out_free_tbl; 3244 } 3245 3246 err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg); 3247 if (err) { 3248 dev_err(dev, "dpni_set_qos_table failed\n"); 3249 goto out_unmap_tbl; 3250 } 3251 3252 /* Add QoS table entries */ 3253 key = kzalloc(key_size * 2, GFP_KERNEL); 3254 if (!key) { 3255 err = -ENOMEM; 3256 goto out_unmap_tbl; 3257 } 3258 mask = key + key_size; 3259 *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK); 3260 3261 key_params.key_iova = dma_map_single(dev, key, key_size * 2, 3262 DMA_TO_DEVICE); 3263 if (dma_mapping_error(dev, key_params.key_iova)) { 3264 dev_err(dev, "Qos table entry DMA mapping failed\n"); 3265 err = -ENOMEM; 3266 goto out_free_key; 3267 } 3268 3269 key_params.mask_iova = key_params.key_iova + key_size; 3270 key_params.key_size = key_size; 3271 3272 /* We add rules for PCP-based distribution starting with highest 3273 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic 3274 * classes to accommodate all priority levels, the lowest ones end up 3275 * on TC 0 which was configured as default 3276 */ 3277 for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) { 3278 *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT); 3279 dma_sync_single_for_device(dev, key_params.key_iova, 3280 key_size * 2, DMA_TO_DEVICE); 3281 3282 err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token, 3283 &key_params, i, i); 3284 if (err) { 3285 dev_err(dev, "dpni_add_qos_entry failed\n"); 3286 dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token); 3287 goto out_unmap_key; 3288 } 3289 } 3290 3291 priv->vlan_cls_enabled = true; 3292 3293 /* Table and key memory is not persistent, clean everything up after 3294 * configuration is finished 3295 */ 3296 out_unmap_key: 3297 dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE); 3298 out_free_key: 3299 kfree(key); 3300 out_unmap_tbl: 3301 dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE, 3302 DMA_TO_DEVICE); 3303 out_free_tbl: 3304 kfree(dma_mem); 3305 3306 return err; 3307 } 3308 3309 /* Configure the DPNI object this interface is associated with */ 3310 static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev) 3311 { 3312 struct device *dev = &ls_dev->dev; 3313 struct dpaa2_eth_priv *priv; 3314 struct net_device *net_dev; 3315 int err; 3316 3317 net_dev = dev_get_drvdata(dev); 3318 priv = netdev_priv(net_dev); 3319 3320 /* get a handle for the DPNI object */ 3321 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token); 3322 if (err) { 3323 dev_err(dev, "dpni_open() failed\n"); 3324 return err; 3325 } 3326 3327 /* Check if we can work with this DPNI object */ 3328 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major, 3329 &priv->dpni_ver_minor); 3330 if (err) { 3331 dev_err(dev, "dpni_get_api_version() failed\n"); 3332 goto close; 3333 } 3334 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) { 3335 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n", 3336 priv->dpni_ver_major, priv->dpni_ver_minor, 3337 DPNI_VER_MAJOR, DPNI_VER_MINOR); 3338 err = -ENOTSUPP; 3339 goto close; 3340 } 3341 3342 ls_dev->mc_io = priv->mc_io; 3343 ls_dev->mc_handle = priv->mc_token; 3344 3345 err = dpni_reset(priv->mc_io, 0, priv->mc_token); 3346 if (err) { 3347 dev_err(dev, "dpni_reset() failed\n"); 3348 goto close; 3349 } 3350 3351 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token, 3352 &priv->dpni_attrs); 3353 if (err) { 3354 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err); 3355 goto close; 3356 } 3357 3358 err = dpaa2_eth_set_buffer_layout(priv); 3359 if (err) 3360 goto close; 3361 3362 dpaa2_eth_set_enqueue_mode(priv); 3363 3364 /* Enable pause frame support */ 3365 if (dpaa2_eth_has_pause_support(priv)) { 3366 err = dpaa2_eth_set_pause(priv); 3367 if (err) 3368 goto close; 3369 } 3370 3371 err = dpaa2_eth_set_vlan_qos(priv); 3372 if (err && err != -EOPNOTSUPP) 3373 goto close; 3374 3375 priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv), 3376 sizeof(struct dpaa2_eth_cls_rule), 3377 GFP_KERNEL); 3378 if (!priv->cls_rules) { 3379 err = -ENOMEM; 3380 goto close; 3381 } 3382 3383 return 0; 3384 3385 close: 3386 dpni_close(priv->mc_io, 0, priv->mc_token); 3387 3388 return err; 3389 } 3390 3391 static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv) 3392 { 3393 int err; 3394 3395 err = dpni_reset(priv->mc_io, 0, priv->mc_token); 3396 if (err) 3397 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n", 3398 err); 3399 3400 dpni_close(priv->mc_io, 0, priv->mc_token); 3401 } 3402 3403 static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv, 3404 struct dpaa2_eth_fq *fq) 3405 { 3406 struct device *dev = priv->net_dev->dev.parent; 3407 struct dpni_queue queue; 3408 struct dpni_queue_id qid; 3409 int err; 3410 3411 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 3412 DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid); 3413 if (err) { 3414 dev_err(dev, "dpni_get_queue(RX) failed\n"); 3415 return err; 3416 } 3417 3418 fq->fqid = qid.fqid; 3419 3420 queue.destination.id = fq->channel->dpcon_id; 3421 queue.destination.type = DPNI_DEST_DPCON; 3422 queue.destination.priority = 1; 3423 queue.user_context = (u64)(uintptr_t)fq; 3424 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token, 3425 DPNI_QUEUE_RX, fq->tc, fq->flowid, 3426 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST, 3427 &queue); 3428 if (err) { 3429 dev_err(dev, "dpni_set_queue(RX) failed\n"); 3430 return err; 3431 } 3432 3433 /* xdp_rxq setup */ 3434 /* only once for each channel */ 3435 if (fq->tc > 0) 3436 return 0; 3437 3438 err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev, 3439 fq->flowid, 0); 3440 if (err) { 3441 dev_err(dev, "xdp_rxq_info_reg failed\n"); 3442 return err; 3443 } 3444 3445 err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq, 3446 MEM_TYPE_PAGE_ORDER0, NULL); 3447 if (err) { 3448 dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n"); 3449 return err; 3450 } 3451 3452 return 0; 3453 } 3454 3455 static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv, 3456 struct dpaa2_eth_fq *fq) 3457 { 3458 struct device *dev = priv->net_dev->dev.parent; 3459 struct dpni_queue queue; 3460 struct dpni_queue_id qid; 3461 int i, err; 3462 3463 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) { 3464 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 3465 DPNI_QUEUE_TX, i, fq->flowid, 3466 &queue, &qid); 3467 if (err) { 3468 dev_err(dev, "dpni_get_queue(TX) failed\n"); 3469 return err; 3470 } 3471 fq->tx_fqid[i] = qid.fqid; 3472 } 3473 3474 /* All Tx queues belonging to the same flowid have the same qdbin */ 3475 fq->tx_qdbin = qid.qdbin; 3476 3477 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 3478 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid, 3479 &queue, &qid); 3480 if (err) { 3481 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n"); 3482 return err; 3483 } 3484 3485 fq->fqid = qid.fqid; 3486 3487 queue.destination.id = fq->channel->dpcon_id; 3488 queue.destination.type = DPNI_DEST_DPCON; 3489 queue.destination.priority = 0; 3490 queue.user_context = (u64)(uintptr_t)fq; 3491 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token, 3492 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid, 3493 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST, 3494 &queue); 3495 if (err) { 3496 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n"); 3497 return err; 3498 } 3499 3500 return 0; 3501 } 3502 3503 static int setup_rx_err_flow(struct dpaa2_eth_priv *priv, 3504 struct dpaa2_eth_fq *fq) 3505 { 3506 struct device *dev = priv->net_dev->dev.parent; 3507 struct dpni_queue q = { { 0 } }; 3508 struct dpni_queue_id qid; 3509 u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST; 3510 int err; 3511 3512 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 3513 DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid); 3514 if (err) { 3515 dev_err(dev, "dpni_get_queue() failed (%d)\n", err); 3516 return err; 3517 } 3518 3519 fq->fqid = qid.fqid; 3520 3521 q.destination.id = fq->channel->dpcon_id; 3522 q.destination.type = DPNI_DEST_DPCON; 3523 q.destination.priority = 1; 3524 q.user_context = (u64)(uintptr_t)fq; 3525 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token, 3526 DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q); 3527 if (err) { 3528 dev_err(dev, "dpni_set_queue() failed (%d)\n", err); 3529 return err; 3530 } 3531 3532 return 0; 3533 } 3534 3535 /* Supported header fields for Rx hash distribution key */ 3536 static const struct dpaa2_eth_dist_fields dist_fields[] = { 3537 { 3538 /* L2 header */ 3539 .rxnfc_field = RXH_L2DA, 3540 .cls_prot = NET_PROT_ETH, 3541 .cls_field = NH_FLD_ETH_DA, 3542 .id = DPAA2_ETH_DIST_ETHDST, 3543 .size = 6, 3544 }, { 3545 .cls_prot = NET_PROT_ETH, 3546 .cls_field = NH_FLD_ETH_SA, 3547 .id = DPAA2_ETH_DIST_ETHSRC, 3548 .size = 6, 3549 }, { 3550 /* This is the last ethertype field parsed: 3551 * depending on frame format, it can be the MAC ethertype 3552 * or the VLAN etype. 3553 */ 3554 .cls_prot = NET_PROT_ETH, 3555 .cls_field = NH_FLD_ETH_TYPE, 3556 .id = DPAA2_ETH_DIST_ETHTYPE, 3557 .size = 2, 3558 }, { 3559 /* VLAN header */ 3560 .rxnfc_field = RXH_VLAN, 3561 .cls_prot = NET_PROT_VLAN, 3562 .cls_field = NH_FLD_VLAN_TCI, 3563 .id = DPAA2_ETH_DIST_VLAN, 3564 .size = 2, 3565 }, { 3566 /* IP header */ 3567 .rxnfc_field = RXH_IP_SRC, 3568 .cls_prot = NET_PROT_IP, 3569 .cls_field = NH_FLD_IP_SRC, 3570 .id = DPAA2_ETH_DIST_IPSRC, 3571 .size = 4, 3572 }, { 3573 .rxnfc_field = RXH_IP_DST, 3574 .cls_prot = NET_PROT_IP, 3575 .cls_field = NH_FLD_IP_DST, 3576 .id = DPAA2_ETH_DIST_IPDST, 3577 .size = 4, 3578 }, { 3579 .rxnfc_field = RXH_L3_PROTO, 3580 .cls_prot = NET_PROT_IP, 3581 .cls_field = NH_FLD_IP_PROTO, 3582 .id = DPAA2_ETH_DIST_IPPROTO, 3583 .size = 1, 3584 }, { 3585 /* Using UDP ports, this is functionally equivalent to raw 3586 * byte pairs from L4 header. 3587 */ 3588 .rxnfc_field = RXH_L4_B_0_1, 3589 .cls_prot = NET_PROT_UDP, 3590 .cls_field = NH_FLD_UDP_PORT_SRC, 3591 .id = DPAA2_ETH_DIST_L4SRC, 3592 .size = 2, 3593 }, { 3594 .rxnfc_field = RXH_L4_B_2_3, 3595 .cls_prot = NET_PROT_UDP, 3596 .cls_field = NH_FLD_UDP_PORT_DST, 3597 .id = DPAA2_ETH_DIST_L4DST, 3598 .size = 2, 3599 }, 3600 }; 3601 3602 /* Configure the Rx hash key using the legacy API */ 3603 static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key) 3604 { 3605 struct device *dev = priv->net_dev->dev.parent; 3606 struct dpni_rx_tc_dist_cfg dist_cfg; 3607 int i, err = 0; 3608 3609 memset(&dist_cfg, 0, sizeof(dist_cfg)); 3610 3611 dist_cfg.key_cfg_iova = key; 3612 dist_cfg.dist_size = dpaa2_eth_queue_count(priv); 3613 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH; 3614 3615 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) { 3616 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 3617 i, &dist_cfg); 3618 if (err) { 3619 dev_err(dev, "dpni_set_rx_tc_dist failed\n"); 3620 break; 3621 } 3622 } 3623 3624 return err; 3625 } 3626 3627 /* Configure the Rx hash key using the new API */ 3628 static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key) 3629 { 3630 struct device *dev = priv->net_dev->dev.parent; 3631 struct dpni_rx_dist_cfg dist_cfg; 3632 int i, err = 0; 3633 3634 memset(&dist_cfg, 0, sizeof(dist_cfg)); 3635 3636 dist_cfg.key_cfg_iova = key; 3637 dist_cfg.dist_size = dpaa2_eth_queue_count(priv); 3638 dist_cfg.enable = 1; 3639 3640 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) { 3641 dist_cfg.tc = i; 3642 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, 3643 &dist_cfg); 3644 if (err) { 3645 dev_err(dev, "dpni_set_rx_hash_dist failed\n"); 3646 break; 3647 } 3648 3649 /* If the flow steering / hashing key is shared between all 3650 * traffic classes, install it just once 3651 */ 3652 if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS) 3653 break; 3654 } 3655 3656 return err; 3657 } 3658 3659 /* Configure the Rx flow classification key */ 3660 static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key) 3661 { 3662 struct device *dev = priv->net_dev->dev.parent; 3663 struct dpni_rx_dist_cfg dist_cfg; 3664 int i, err = 0; 3665 3666 memset(&dist_cfg, 0, sizeof(dist_cfg)); 3667 3668 dist_cfg.key_cfg_iova = key; 3669 dist_cfg.dist_size = dpaa2_eth_queue_count(priv); 3670 dist_cfg.enable = 1; 3671 3672 for (i = 0; i < dpaa2_eth_tc_count(priv); i++) { 3673 dist_cfg.tc = i; 3674 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, 3675 &dist_cfg); 3676 if (err) { 3677 dev_err(dev, "dpni_set_rx_fs_dist failed\n"); 3678 break; 3679 } 3680 3681 /* If the flow steering / hashing key is shared between all 3682 * traffic classes, install it just once 3683 */ 3684 if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS) 3685 break; 3686 } 3687 3688 return err; 3689 } 3690 3691 /* Size of the Rx flow classification key */ 3692 int dpaa2_eth_cls_key_size(u64 fields) 3693 { 3694 int i, size = 0; 3695 3696 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { 3697 if (!(fields & dist_fields[i].id)) 3698 continue; 3699 size += dist_fields[i].size; 3700 } 3701 3702 return size; 3703 } 3704 3705 /* Offset of header field in Rx classification key */ 3706 int dpaa2_eth_cls_fld_off(int prot, int field) 3707 { 3708 int i, off = 0; 3709 3710 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { 3711 if (dist_fields[i].cls_prot == prot && 3712 dist_fields[i].cls_field == field) 3713 return off; 3714 off += dist_fields[i].size; 3715 } 3716 3717 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n"); 3718 return 0; 3719 } 3720 3721 /* Prune unused fields from the classification rule. 3722 * Used when masking is not supported 3723 */ 3724 void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields) 3725 { 3726 int off = 0, new_off = 0; 3727 int i, size; 3728 3729 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { 3730 size = dist_fields[i].size; 3731 if (dist_fields[i].id & fields) { 3732 memcpy(key_mem + new_off, key_mem + off, size); 3733 new_off += size; 3734 } 3735 off += size; 3736 } 3737 } 3738 3739 /* Set Rx distribution (hash or flow classification) key 3740 * flags is a combination of RXH_ bits 3741 */ 3742 static int dpaa2_eth_set_dist_key(struct net_device *net_dev, 3743 enum dpaa2_eth_rx_dist type, u64 flags) 3744 { 3745 struct device *dev = net_dev->dev.parent; 3746 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 3747 struct dpkg_profile_cfg cls_cfg; 3748 u32 rx_hash_fields = 0; 3749 dma_addr_t key_iova; 3750 u8 *dma_mem; 3751 int i; 3752 int err = 0; 3753 3754 memset(&cls_cfg, 0, sizeof(cls_cfg)); 3755 3756 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { 3757 struct dpkg_extract *key = 3758 &cls_cfg.extracts[cls_cfg.num_extracts]; 3759 3760 /* For both Rx hashing and classification keys 3761 * we set only the selected fields. 3762 */ 3763 if (!(flags & dist_fields[i].id)) 3764 continue; 3765 if (type == DPAA2_ETH_RX_DIST_HASH) 3766 rx_hash_fields |= dist_fields[i].rxnfc_field; 3767 3768 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) { 3769 dev_err(dev, "error adding key extraction rule, too many rules?\n"); 3770 return -E2BIG; 3771 } 3772 3773 key->type = DPKG_EXTRACT_FROM_HDR; 3774 key->extract.from_hdr.prot = dist_fields[i].cls_prot; 3775 key->extract.from_hdr.type = DPKG_FULL_FIELD; 3776 key->extract.from_hdr.field = dist_fields[i].cls_field; 3777 cls_cfg.num_extracts++; 3778 } 3779 3780 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL); 3781 if (!dma_mem) 3782 return -ENOMEM; 3783 3784 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem); 3785 if (err) { 3786 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err); 3787 goto free_key; 3788 } 3789 3790 /* Prepare for setting the rx dist */ 3791 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE, 3792 DMA_TO_DEVICE); 3793 if (dma_mapping_error(dev, key_iova)) { 3794 dev_err(dev, "DMA mapping failed\n"); 3795 err = -ENOMEM; 3796 goto free_key; 3797 } 3798 3799 if (type == DPAA2_ETH_RX_DIST_HASH) { 3800 if (dpaa2_eth_has_legacy_dist(priv)) 3801 err = dpaa2_eth_config_legacy_hash_key(priv, key_iova); 3802 else 3803 err = dpaa2_eth_config_hash_key(priv, key_iova); 3804 } else { 3805 err = dpaa2_eth_config_cls_key(priv, key_iova); 3806 } 3807 3808 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE, 3809 DMA_TO_DEVICE); 3810 if (!err && type == DPAA2_ETH_RX_DIST_HASH) 3811 priv->rx_hash_fields = rx_hash_fields; 3812 3813 free_key: 3814 kfree(dma_mem); 3815 return err; 3816 } 3817 3818 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags) 3819 { 3820 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 3821 u64 key = 0; 3822 int i; 3823 3824 if (!dpaa2_eth_hash_enabled(priv)) 3825 return -EOPNOTSUPP; 3826 3827 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) 3828 if (dist_fields[i].rxnfc_field & flags) 3829 key |= dist_fields[i].id; 3830 3831 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key); 3832 } 3833 3834 int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags) 3835 { 3836 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags); 3837 } 3838 3839 static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv) 3840 { 3841 struct device *dev = priv->net_dev->dev.parent; 3842 int err; 3843 3844 /* Check if we actually support Rx flow classification */ 3845 if (dpaa2_eth_has_legacy_dist(priv)) { 3846 dev_dbg(dev, "Rx cls not supported by current MC version\n"); 3847 return -EOPNOTSUPP; 3848 } 3849 3850 if (!dpaa2_eth_fs_enabled(priv)) { 3851 dev_dbg(dev, "Rx cls disabled in DPNI options\n"); 3852 return -EOPNOTSUPP; 3853 } 3854 3855 if (!dpaa2_eth_hash_enabled(priv)) { 3856 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n"); 3857 return -EOPNOTSUPP; 3858 } 3859 3860 /* If there is no support for masking in the classification table, 3861 * we don't set a default key, as it will depend on the rules 3862 * added by the user at runtime. 3863 */ 3864 if (!dpaa2_eth_fs_mask_enabled(priv)) 3865 goto out; 3866 3867 err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL); 3868 if (err) 3869 return err; 3870 3871 out: 3872 priv->rx_cls_enabled = 1; 3873 3874 return 0; 3875 } 3876 3877 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs, 3878 * frame queues and channels 3879 */ 3880 static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv) 3881 { 3882 struct net_device *net_dev = priv->net_dev; 3883 struct device *dev = net_dev->dev.parent; 3884 struct dpni_pools_cfg pools_params; 3885 struct dpni_error_cfg err_cfg; 3886 int err = 0; 3887 int i; 3888 3889 pools_params.num_dpbp = 1; 3890 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id; 3891 pools_params.pools[0].backup_pool = 0; 3892 pools_params.pools[0].buffer_size = priv->rx_buf_size; 3893 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params); 3894 if (err) { 3895 dev_err(dev, "dpni_set_pools() failed\n"); 3896 return err; 3897 } 3898 3899 /* have the interface implicitly distribute traffic based on 3900 * the default hash key 3901 */ 3902 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT); 3903 if (err && err != -EOPNOTSUPP) 3904 dev_err(dev, "Failed to configure hashing\n"); 3905 3906 /* Configure the flow classification key; it includes all 3907 * supported header fields and cannot be modified at runtime 3908 */ 3909 err = dpaa2_eth_set_default_cls(priv); 3910 if (err && err != -EOPNOTSUPP) 3911 dev_err(dev, "Failed to configure Rx classification key\n"); 3912 3913 /* Configure handling of error frames */ 3914 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK; 3915 err_cfg.set_frame_annotation = 1; 3916 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD; 3917 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token, 3918 &err_cfg); 3919 if (err) { 3920 dev_err(dev, "dpni_set_errors_behavior failed\n"); 3921 return err; 3922 } 3923 3924 /* Configure Rx and Tx conf queues to generate CDANs */ 3925 for (i = 0; i < priv->num_fqs; i++) { 3926 switch (priv->fq[i].type) { 3927 case DPAA2_RX_FQ: 3928 err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]); 3929 break; 3930 case DPAA2_TX_CONF_FQ: 3931 err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]); 3932 break; 3933 case DPAA2_RX_ERR_FQ: 3934 err = setup_rx_err_flow(priv, &priv->fq[i]); 3935 break; 3936 default: 3937 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type); 3938 return -EINVAL; 3939 } 3940 if (err) 3941 return err; 3942 } 3943 3944 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token, 3945 DPNI_QUEUE_TX, &priv->tx_qdid); 3946 if (err) { 3947 dev_err(dev, "dpni_get_qdid() failed\n"); 3948 return err; 3949 } 3950 3951 return 0; 3952 } 3953 3954 /* Allocate rings for storing incoming frame descriptors */ 3955 static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv) 3956 { 3957 struct net_device *net_dev = priv->net_dev; 3958 struct device *dev = net_dev->dev.parent; 3959 int i; 3960 3961 for (i = 0; i < priv->num_channels; i++) { 3962 priv->channel[i]->store = 3963 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev); 3964 if (!priv->channel[i]->store) { 3965 netdev_err(net_dev, "dpaa2_io_store_create() failed\n"); 3966 goto err_ring; 3967 } 3968 } 3969 3970 return 0; 3971 3972 err_ring: 3973 for (i = 0; i < priv->num_channels; i++) { 3974 if (!priv->channel[i]->store) 3975 break; 3976 dpaa2_io_store_destroy(priv->channel[i]->store); 3977 } 3978 3979 return -ENOMEM; 3980 } 3981 3982 static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv) 3983 { 3984 int i; 3985 3986 for (i = 0; i < priv->num_channels; i++) 3987 dpaa2_io_store_destroy(priv->channel[i]->store); 3988 } 3989 3990 static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv) 3991 { 3992 struct net_device *net_dev = priv->net_dev; 3993 struct device *dev = net_dev->dev.parent; 3994 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN]; 3995 int err; 3996 3997 /* Get firmware address, if any */ 3998 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr); 3999 if (err) { 4000 dev_err(dev, "dpni_get_port_mac_addr() failed\n"); 4001 return err; 4002 } 4003 4004 /* Get DPNI attributes address, if any */ 4005 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token, 4006 dpni_mac_addr); 4007 if (err) { 4008 dev_err(dev, "dpni_get_primary_mac_addr() failed\n"); 4009 return err; 4010 } 4011 4012 /* First check if firmware has any address configured by bootloader */ 4013 if (!is_zero_ether_addr(mac_addr)) { 4014 /* If the DPMAC addr != DPNI addr, update it */ 4015 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) { 4016 err = dpni_set_primary_mac_addr(priv->mc_io, 0, 4017 priv->mc_token, 4018 mac_addr); 4019 if (err) { 4020 dev_err(dev, "dpni_set_primary_mac_addr() failed\n"); 4021 return err; 4022 } 4023 } 4024 eth_hw_addr_set(net_dev, mac_addr); 4025 } else if (is_zero_ether_addr(dpni_mac_addr)) { 4026 /* No MAC address configured, fill in net_dev->dev_addr 4027 * with a random one 4028 */ 4029 eth_hw_addr_random(net_dev); 4030 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n"); 4031 4032 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token, 4033 net_dev->dev_addr); 4034 if (err) { 4035 dev_err(dev, "dpni_set_primary_mac_addr() failed\n"); 4036 return err; 4037 } 4038 4039 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all 4040 * practical purposes, this will be our "permanent" mac address, 4041 * at least until the next reboot. This move will also permit 4042 * register_netdevice() to properly fill up net_dev->perm_addr. 4043 */ 4044 net_dev->addr_assign_type = NET_ADDR_PERM; 4045 } else { 4046 /* NET_ADDR_PERM is default, all we have to do is 4047 * fill in the device addr. 4048 */ 4049 eth_hw_addr_set(net_dev, dpni_mac_addr); 4050 } 4051 4052 return 0; 4053 } 4054 4055 static int dpaa2_eth_netdev_init(struct net_device *net_dev) 4056 { 4057 struct device *dev = net_dev->dev.parent; 4058 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 4059 u32 options = priv->dpni_attrs.options; 4060 u64 supported = 0, not_supported = 0; 4061 u8 bcast_addr[ETH_ALEN]; 4062 u8 num_queues; 4063 int err; 4064 4065 net_dev->netdev_ops = &dpaa2_eth_ops; 4066 net_dev->ethtool_ops = &dpaa2_ethtool_ops; 4067 4068 err = dpaa2_eth_set_mac_addr(priv); 4069 if (err) 4070 return err; 4071 4072 /* Explicitly add the broadcast address to the MAC filtering table */ 4073 eth_broadcast_addr(bcast_addr); 4074 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr); 4075 if (err) { 4076 dev_err(dev, "dpni_add_mac_addr() failed\n"); 4077 return err; 4078 } 4079 4080 /* Set MTU upper limit; lower limit is 68B (default value) */ 4081 net_dev->max_mtu = DPAA2_ETH_MAX_MTU; 4082 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, 4083 DPAA2_ETH_MFL); 4084 if (err) { 4085 dev_err(dev, "dpni_set_max_frame_length() failed\n"); 4086 return err; 4087 } 4088 4089 /* Set actual number of queues in the net device */ 4090 num_queues = dpaa2_eth_queue_count(priv); 4091 err = netif_set_real_num_tx_queues(net_dev, num_queues); 4092 if (err) { 4093 dev_err(dev, "netif_set_real_num_tx_queues() failed\n"); 4094 return err; 4095 } 4096 err = netif_set_real_num_rx_queues(net_dev, num_queues); 4097 if (err) { 4098 dev_err(dev, "netif_set_real_num_rx_queues() failed\n"); 4099 return err; 4100 } 4101 4102 /* Capabilities listing */ 4103 supported |= IFF_LIVE_ADDR_CHANGE; 4104 4105 if (options & DPNI_OPT_NO_MAC_FILTER) 4106 not_supported |= IFF_UNICAST_FLT; 4107 else 4108 supported |= IFF_UNICAST_FLT; 4109 4110 net_dev->priv_flags |= supported; 4111 net_dev->priv_flags &= ~not_supported; 4112 4113 /* Features */ 4114 net_dev->features = NETIF_F_RXCSUM | 4115 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 4116 NETIF_F_SG | NETIF_F_HIGHDMA | 4117 NETIF_F_LLTX | NETIF_F_HW_TC; 4118 net_dev->hw_features = net_dev->features; 4119 4120 if (priv->dpni_attrs.vlan_filter_entries) 4121 net_dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER; 4122 4123 return 0; 4124 } 4125 4126 static int dpaa2_eth_poll_link_state(void *arg) 4127 { 4128 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg; 4129 int err; 4130 4131 while (!kthread_should_stop()) { 4132 err = dpaa2_eth_link_state_update(priv); 4133 if (unlikely(err)) 4134 return err; 4135 4136 msleep(DPAA2_ETH_LINK_STATE_REFRESH); 4137 } 4138 4139 return 0; 4140 } 4141 4142 static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv) 4143 { 4144 struct fsl_mc_device *dpni_dev, *dpmac_dev; 4145 struct dpaa2_mac *mac; 4146 int err; 4147 4148 dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent); 4149 dpmac_dev = fsl_mc_get_endpoint(dpni_dev, 0); 4150 4151 if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER) 4152 return PTR_ERR(dpmac_dev); 4153 4154 if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type) 4155 return 0; 4156 4157 mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL); 4158 if (!mac) 4159 return -ENOMEM; 4160 4161 mac->mc_dev = dpmac_dev; 4162 mac->mc_io = priv->mc_io; 4163 mac->net_dev = priv->net_dev; 4164 4165 err = dpaa2_mac_open(mac); 4166 if (err) 4167 goto err_free_mac; 4168 priv->mac = mac; 4169 4170 if (dpaa2_eth_is_type_phy(priv)) { 4171 err = dpaa2_mac_connect(mac); 4172 if (err && err != -EPROBE_DEFER) 4173 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint: %pe", 4174 ERR_PTR(err)); 4175 if (err) 4176 goto err_close_mac; 4177 } 4178 4179 return 0; 4180 4181 err_close_mac: 4182 dpaa2_mac_close(mac); 4183 priv->mac = NULL; 4184 err_free_mac: 4185 kfree(mac); 4186 return err; 4187 } 4188 4189 static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv) 4190 { 4191 if (dpaa2_eth_is_type_phy(priv)) 4192 dpaa2_mac_disconnect(priv->mac); 4193 4194 if (!dpaa2_eth_has_mac(priv)) 4195 return; 4196 4197 dpaa2_mac_close(priv->mac); 4198 kfree(priv->mac); 4199 priv->mac = NULL; 4200 } 4201 4202 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg) 4203 { 4204 u32 status = ~0; 4205 struct device *dev = (struct device *)arg; 4206 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev); 4207 struct net_device *net_dev = dev_get_drvdata(dev); 4208 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 4209 int err; 4210 4211 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle, 4212 DPNI_IRQ_INDEX, &status); 4213 if (unlikely(err)) { 4214 netdev_err(net_dev, "Can't get irq status (err %d)\n", err); 4215 return IRQ_HANDLED; 4216 } 4217 4218 if (status & DPNI_IRQ_EVENT_LINK_CHANGED) 4219 dpaa2_eth_link_state_update(netdev_priv(net_dev)); 4220 4221 if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) { 4222 dpaa2_eth_set_mac_addr(netdev_priv(net_dev)); 4223 dpaa2_eth_update_tx_fqids(priv); 4224 4225 rtnl_lock(); 4226 if (dpaa2_eth_has_mac(priv)) 4227 dpaa2_eth_disconnect_mac(priv); 4228 else 4229 dpaa2_eth_connect_mac(priv); 4230 rtnl_unlock(); 4231 } 4232 4233 return IRQ_HANDLED; 4234 } 4235 4236 static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev) 4237 { 4238 int err = 0; 4239 struct fsl_mc_device_irq *irq; 4240 4241 err = fsl_mc_allocate_irqs(ls_dev); 4242 if (err) { 4243 dev_err(&ls_dev->dev, "MC irqs allocation failed\n"); 4244 return err; 4245 } 4246 4247 irq = ls_dev->irqs[0]; 4248 err = devm_request_threaded_irq(&ls_dev->dev, irq->virq, 4249 NULL, dpni_irq0_handler_thread, 4250 IRQF_NO_SUSPEND | IRQF_ONESHOT, 4251 dev_name(&ls_dev->dev), &ls_dev->dev); 4252 if (err < 0) { 4253 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err); 4254 goto free_mc_irq; 4255 } 4256 4257 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle, 4258 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED | 4259 DPNI_IRQ_EVENT_ENDPOINT_CHANGED); 4260 if (err < 0) { 4261 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err); 4262 goto free_irq; 4263 } 4264 4265 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle, 4266 DPNI_IRQ_INDEX, 1); 4267 if (err < 0) { 4268 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err); 4269 goto free_irq; 4270 } 4271 4272 return 0; 4273 4274 free_irq: 4275 devm_free_irq(&ls_dev->dev, irq->virq, &ls_dev->dev); 4276 free_mc_irq: 4277 fsl_mc_free_irqs(ls_dev); 4278 4279 return err; 4280 } 4281 4282 static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv) 4283 { 4284 int i; 4285 struct dpaa2_eth_channel *ch; 4286 4287 for (i = 0; i < priv->num_channels; i++) { 4288 ch = priv->channel[i]; 4289 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */ 4290 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll, 4291 NAPI_POLL_WEIGHT); 4292 } 4293 } 4294 4295 static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv) 4296 { 4297 int i; 4298 struct dpaa2_eth_channel *ch; 4299 4300 for (i = 0; i < priv->num_channels; i++) { 4301 ch = priv->channel[i]; 4302 netif_napi_del(&ch->napi); 4303 } 4304 } 4305 4306 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev) 4307 { 4308 struct device *dev; 4309 struct net_device *net_dev = NULL; 4310 struct dpaa2_eth_priv *priv = NULL; 4311 int err = 0; 4312 4313 dev = &dpni_dev->dev; 4314 4315 /* Net device */ 4316 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES); 4317 if (!net_dev) { 4318 dev_err(dev, "alloc_etherdev_mq() failed\n"); 4319 return -ENOMEM; 4320 } 4321 4322 SET_NETDEV_DEV(net_dev, dev); 4323 dev_set_drvdata(dev, net_dev); 4324 4325 priv = netdev_priv(net_dev); 4326 priv->net_dev = net_dev; 4327 4328 priv->iommu_domain = iommu_get_domain_for_dev(dev); 4329 4330 priv->tx_tstamp_type = HWTSTAMP_TX_OFF; 4331 priv->rx_tstamp = false; 4332 4333 priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0); 4334 if (!priv->dpaa2_ptp_wq) { 4335 err = -ENOMEM; 4336 goto err_wq_alloc; 4337 } 4338 4339 INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp); 4340 4341 skb_queue_head_init(&priv->tx_skbs); 4342 4343 priv->rx_copybreak = DPAA2_ETH_DEFAULT_COPYBREAK; 4344 4345 /* Obtain a MC portal */ 4346 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, 4347 &priv->mc_io); 4348 if (err) { 4349 if (err == -ENXIO) 4350 err = -EPROBE_DEFER; 4351 else 4352 dev_err(dev, "MC portal allocation failed\n"); 4353 goto err_portal_alloc; 4354 } 4355 4356 /* MC objects initialization and configuration */ 4357 err = dpaa2_eth_setup_dpni(dpni_dev); 4358 if (err) 4359 goto err_dpni_setup; 4360 4361 err = dpaa2_eth_setup_dpio(priv); 4362 if (err) 4363 goto err_dpio_setup; 4364 4365 dpaa2_eth_setup_fqs(priv); 4366 4367 err = dpaa2_eth_setup_dpbp(priv); 4368 if (err) 4369 goto err_dpbp_setup; 4370 4371 err = dpaa2_eth_bind_dpni(priv); 4372 if (err) 4373 goto err_bind; 4374 4375 /* Add a NAPI context for each channel */ 4376 dpaa2_eth_add_ch_napi(priv); 4377 4378 /* Percpu statistics */ 4379 priv->percpu_stats = alloc_percpu(*priv->percpu_stats); 4380 if (!priv->percpu_stats) { 4381 dev_err(dev, "alloc_percpu(percpu_stats) failed\n"); 4382 err = -ENOMEM; 4383 goto err_alloc_percpu_stats; 4384 } 4385 priv->percpu_extras = alloc_percpu(*priv->percpu_extras); 4386 if (!priv->percpu_extras) { 4387 dev_err(dev, "alloc_percpu(percpu_extras) failed\n"); 4388 err = -ENOMEM; 4389 goto err_alloc_percpu_extras; 4390 } 4391 4392 priv->sgt_cache = alloc_percpu(*priv->sgt_cache); 4393 if (!priv->sgt_cache) { 4394 dev_err(dev, "alloc_percpu(sgt_cache) failed\n"); 4395 err = -ENOMEM; 4396 goto err_alloc_sgt_cache; 4397 } 4398 4399 err = dpaa2_eth_netdev_init(net_dev); 4400 if (err) 4401 goto err_netdev_init; 4402 4403 /* Configure checksum offload based on current interface flags */ 4404 err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM)); 4405 if (err) 4406 goto err_csum; 4407 4408 err = dpaa2_eth_set_tx_csum(priv, 4409 !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))); 4410 if (err) 4411 goto err_csum; 4412 4413 err = dpaa2_eth_alloc_rings(priv); 4414 if (err) 4415 goto err_alloc_rings; 4416 4417 #ifdef CONFIG_FSL_DPAA2_ETH_DCB 4418 if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) { 4419 priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE; 4420 net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops; 4421 } else { 4422 dev_dbg(dev, "PFC not supported\n"); 4423 } 4424 #endif 4425 4426 err = dpaa2_eth_setup_irqs(dpni_dev); 4427 if (err) { 4428 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n"); 4429 priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv, 4430 "%s_poll_link", net_dev->name); 4431 if (IS_ERR(priv->poll_thread)) { 4432 dev_err(dev, "Error starting polling thread\n"); 4433 goto err_poll_thread; 4434 } 4435 priv->do_link_poll = true; 4436 } 4437 4438 err = dpaa2_eth_connect_mac(priv); 4439 if (err) 4440 goto err_connect_mac; 4441 4442 err = dpaa2_eth_dl_alloc(priv); 4443 if (err) 4444 goto err_dl_register; 4445 4446 err = dpaa2_eth_dl_traps_register(priv); 4447 if (err) 4448 goto err_dl_trap_register; 4449 4450 err = dpaa2_eth_dl_port_add(priv); 4451 if (err) 4452 goto err_dl_port_add; 4453 4454 err = register_netdev(net_dev); 4455 if (err < 0) { 4456 dev_err(dev, "register_netdev() failed\n"); 4457 goto err_netdev_reg; 4458 } 4459 4460 #ifdef CONFIG_DEBUG_FS 4461 dpaa2_dbg_add(priv); 4462 #endif 4463 4464 dpaa2_eth_dl_register(priv); 4465 dev_info(dev, "Probed interface %s\n", net_dev->name); 4466 return 0; 4467 4468 err_netdev_reg: 4469 dpaa2_eth_dl_port_del(priv); 4470 err_dl_port_add: 4471 dpaa2_eth_dl_traps_unregister(priv); 4472 err_dl_trap_register: 4473 dpaa2_eth_dl_free(priv); 4474 err_dl_register: 4475 dpaa2_eth_disconnect_mac(priv); 4476 err_connect_mac: 4477 if (priv->do_link_poll) 4478 kthread_stop(priv->poll_thread); 4479 else 4480 fsl_mc_free_irqs(dpni_dev); 4481 err_poll_thread: 4482 dpaa2_eth_free_rings(priv); 4483 err_alloc_rings: 4484 err_csum: 4485 err_netdev_init: 4486 free_percpu(priv->sgt_cache); 4487 err_alloc_sgt_cache: 4488 free_percpu(priv->percpu_extras); 4489 err_alloc_percpu_extras: 4490 free_percpu(priv->percpu_stats); 4491 err_alloc_percpu_stats: 4492 dpaa2_eth_del_ch_napi(priv); 4493 err_bind: 4494 dpaa2_eth_free_dpbp(priv); 4495 err_dpbp_setup: 4496 dpaa2_eth_free_dpio(priv); 4497 err_dpio_setup: 4498 dpaa2_eth_free_dpni(priv); 4499 err_dpni_setup: 4500 fsl_mc_portal_free(priv->mc_io); 4501 err_portal_alloc: 4502 destroy_workqueue(priv->dpaa2_ptp_wq); 4503 err_wq_alloc: 4504 dev_set_drvdata(dev, NULL); 4505 free_netdev(net_dev); 4506 4507 return err; 4508 } 4509 4510 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev) 4511 { 4512 struct device *dev; 4513 struct net_device *net_dev; 4514 struct dpaa2_eth_priv *priv; 4515 4516 dev = &ls_dev->dev; 4517 net_dev = dev_get_drvdata(dev); 4518 priv = netdev_priv(net_dev); 4519 4520 dpaa2_eth_dl_unregister(priv); 4521 4522 #ifdef CONFIG_DEBUG_FS 4523 dpaa2_dbg_remove(priv); 4524 #endif 4525 rtnl_lock(); 4526 dpaa2_eth_disconnect_mac(priv); 4527 rtnl_unlock(); 4528 4529 unregister_netdev(net_dev); 4530 4531 dpaa2_eth_dl_port_del(priv); 4532 dpaa2_eth_dl_traps_unregister(priv); 4533 dpaa2_eth_dl_free(priv); 4534 4535 if (priv->do_link_poll) 4536 kthread_stop(priv->poll_thread); 4537 else 4538 fsl_mc_free_irqs(ls_dev); 4539 4540 dpaa2_eth_free_rings(priv); 4541 free_percpu(priv->sgt_cache); 4542 free_percpu(priv->percpu_stats); 4543 free_percpu(priv->percpu_extras); 4544 4545 dpaa2_eth_del_ch_napi(priv); 4546 dpaa2_eth_free_dpbp(priv); 4547 dpaa2_eth_free_dpio(priv); 4548 dpaa2_eth_free_dpni(priv); 4549 4550 fsl_mc_portal_free(priv->mc_io); 4551 4552 destroy_workqueue(priv->dpaa2_ptp_wq); 4553 4554 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name); 4555 4556 free_netdev(net_dev); 4557 4558 return 0; 4559 } 4560 4561 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = { 4562 { 4563 .vendor = FSL_MC_VENDOR_FREESCALE, 4564 .obj_type = "dpni", 4565 }, 4566 { .vendor = 0x0 } 4567 }; 4568 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table); 4569 4570 static struct fsl_mc_driver dpaa2_eth_driver = { 4571 .driver = { 4572 .name = KBUILD_MODNAME, 4573 .owner = THIS_MODULE, 4574 }, 4575 .probe = dpaa2_eth_probe, 4576 .remove = dpaa2_eth_remove, 4577 .match_id_table = dpaa2_eth_match_id_table 4578 }; 4579 4580 static int __init dpaa2_eth_driver_init(void) 4581 { 4582 int err; 4583 4584 dpaa2_eth_dbg_init(); 4585 err = fsl_mc_driver_register(&dpaa2_eth_driver); 4586 if (err) { 4587 dpaa2_eth_dbg_exit(); 4588 return err; 4589 } 4590 4591 return 0; 4592 } 4593 4594 static void __exit dpaa2_eth_driver_exit(void) 4595 { 4596 dpaa2_eth_dbg_exit(); 4597 fsl_mc_driver_unregister(&dpaa2_eth_driver); 4598 } 4599 4600 module_init(dpaa2_eth_driver_init); 4601 module_exit(dpaa2_eth_driver_exit); 4602