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