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