1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* Copyright 2014-2016 Freescale Semiconductor Inc. 3 * Copyright 2016-2017 NXP 4 */ 5 #include <linux/init.h> 6 #include <linux/module.h> 7 #include <linux/platform_device.h> 8 #include <linux/etherdevice.h> 9 #include <linux/of_net.h> 10 #include <linux/interrupt.h> 11 #include <linux/msi.h> 12 #include <linux/kthread.h> 13 #include <linux/iommu.h> 14 #include <linux/net_tstamp.h> 15 #include <linux/fsl/mc.h> 16 17 #include <net/sock.h> 18 19 #include "dpaa2-eth.h" 20 21 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files 22 * using trace events only need to #include <trace/events/sched.h> 23 */ 24 #define CREATE_TRACE_POINTS 25 #include "dpaa2-eth-trace.h" 26 27 MODULE_LICENSE("Dual BSD/GPL"); 28 MODULE_AUTHOR("Freescale Semiconductor, Inc"); 29 MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver"); 30 31 static void *dpaa2_iova_to_virt(struct iommu_domain *domain, 32 dma_addr_t iova_addr) 33 { 34 phys_addr_t phys_addr; 35 36 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr; 37 38 return phys_to_virt(phys_addr); 39 } 40 41 static void validate_rx_csum(struct dpaa2_eth_priv *priv, 42 u32 fd_status, 43 struct sk_buff *skb) 44 { 45 skb_checksum_none_assert(skb); 46 47 /* HW checksum validation is disabled, nothing to do here */ 48 if (!(priv->net_dev->features & NETIF_F_RXCSUM)) 49 return; 50 51 /* Read checksum validation bits */ 52 if (!((fd_status & DPAA2_FAS_L3CV) && 53 (fd_status & DPAA2_FAS_L4CV))) 54 return; 55 56 /* Inform the stack there's no need to compute L3/L4 csum anymore */ 57 skb->ip_summed = CHECKSUM_UNNECESSARY; 58 } 59 60 /* Free a received FD. 61 * Not to be used for Tx conf FDs or on any other paths. 62 */ 63 static void free_rx_fd(struct dpaa2_eth_priv *priv, 64 const struct dpaa2_fd *fd, 65 void *vaddr) 66 { 67 struct device *dev = priv->net_dev->dev.parent; 68 dma_addr_t addr = dpaa2_fd_get_addr(fd); 69 u8 fd_format = dpaa2_fd_get_format(fd); 70 struct dpaa2_sg_entry *sgt; 71 void *sg_vaddr; 72 int i; 73 74 /* If single buffer frame, just free the data buffer */ 75 if (fd_format == dpaa2_fd_single) 76 goto free_buf; 77 else if (fd_format != dpaa2_fd_sg) 78 /* We don't support any other format */ 79 return; 80 81 /* For S/G frames, we first need to free all SG entries 82 * except the first one, which was taken care of already 83 */ 84 sgt = vaddr + dpaa2_fd_get_offset(fd); 85 for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) { 86 addr = dpaa2_sg_get_addr(&sgt[i]); 87 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr); 88 dma_unmap_single(dev, addr, DPAA2_ETH_RX_BUF_SIZE, 89 DMA_FROM_DEVICE); 90 91 skb_free_frag(sg_vaddr); 92 if (dpaa2_sg_is_final(&sgt[i])) 93 break; 94 } 95 96 free_buf: 97 skb_free_frag(vaddr); 98 } 99 100 /* Build a linear skb based on a single-buffer frame descriptor */ 101 static struct sk_buff *build_linear_skb(struct dpaa2_eth_priv *priv, 102 struct dpaa2_eth_channel *ch, 103 const struct dpaa2_fd *fd, 104 void *fd_vaddr) 105 { 106 struct sk_buff *skb = NULL; 107 u16 fd_offset = dpaa2_fd_get_offset(fd); 108 u32 fd_length = dpaa2_fd_get_len(fd); 109 110 ch->buf_count--; 111 112 skb = build_skb(fd_vaddr, DPAA2_ETH_SKB_SIZE); 113 if (unlikely(!skb)) 114 return NULL; 115 116 skb_reserve(skb, fd_offset); 117 skb_put(skb, fd_length); 118 119 return skb; 120 } 121 122 /* Build a non linear (fragmented) skb based on a S/G table */ 123 static struct sk_buff *build_frag_skb(struct dpaa2_eth_priv *priv, 124 struct dpaa2_eth_channel *ch, 125 struct dpaa2_sg_entry *sgt) 126 { 127 struct sk_buff *skb = NULL; 128 struct device *dev = priv->net_dev->dev.parent; 129 void *sg_vaddr; 130 dma_addr_t sg_addr; 131 u16 sg_offset; 132 u32 sg_length; 133 struct page *page, *head_page; 134 int page_offset; 135 int i; 136 137 for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) { 138 struct dpaa2_sg_entry *sge = &sgt[i]; 139 140 /* NOTE: We only support SG entries in dpaa2_sg_single format, 141 * but this is the only format we may receive from HW anyway 142 */ 143 144 /* Get the address and length from the S/G entry */ 145 sg_addr = dpaa2_sg_get_addr(sge); 146 sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr); 147 dma_unmap_single(dev, sg_addr, DPAA2_ETH_RX_BUF_SIZE, 148 DMA_FROM_DEVICE); 149 150 sg_length = dpaa2_sg_get_len(sge); 151 152 if (i == 0) { 153 /* We build the skb around the first data buffer */ 154 skb = build_skb(sg_vaddr, DPAA2_ETH_SKB_SIZE); 155 if (unlikely(!skb)) { 156 /* Free the first SG entry now, since we already 157 * unmapped it and obtained the virtual address 158 */ 159 skb_free_frag(sg_vaddr); 160 161 /* We still need to subtract the buffers used 162 * by this FD from our software counter 163 */ 164 while (!dpaa2_sg_is_final(&sgt[i]) && 165 i < DPAA2_ETH_MAX_SG_ENTRIES) 166 i++; 167 break; 168 } 169 170 sg_offset = dpaa2_sg_get_offset(sge); 171 skb_reserve(skb, sg_offset); 172 skb_put(skb, sg_length); 173 } else { 174 /* Rest of the data buffers are stored as skb frags */ 175 page = virt_to_page(sg_vaddr); 176 head_page = virt_to_head_page(sg_vaddr); 177 178 /* Offset in page (which may be compound). 179 * Data in subsequent SG entries is stored from the 180 * beginning of the buffer, so we don't need to add the 181 * sg_offset. 182 */ 183 page_offset = ((unsigned long)sg_vaddr & 184 (PAGE_SIZE - 1)) + 185 (page_address(page) - page_address(head_page)); 186 187 skb_add_rx_frag(skb, i - 1, head_page, page_offset, 188 sg_length, DPAA2_ETH_RX_BUF_SIZE); 189 } 190 191 if (dpaa2_sg_is_final(sge)) 192 break; 193 } 194 195 WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT"); 196 197 /* Count all data buffers + SG table buffer */ 198 ch->buf_count -= i + 2; 199 200 return skb; 201 } 202 203 /* Main Rx frame processing routine */ 204 static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv, 205 struct dpaa2_eth_channel *ch, 206 const struct dpaa2_fd *fd, 207 struct napi_struct *napi, 208 u16 queue_id) 209 { 210 dma_addr_t addr = dpaa2_fd_get_addr(fd); 211 u8 fd_format = dpaa2_fd_get_format(fd); 212 void *vaddr; 213 struct sk_buff *skb; 214 struct rtnl_link_stats64 *percpu_stats; 215 struct dpaa2_eth_drv_stats *percpu_extras; 216 struct device *dev = priv->net_dev->dev.parent; 217 struct dpaa2_fas *fas; 218 void *buf_data; 219 u32 status = 0; 220 221 /* Tracing point */ 222 trace_dpaa2_rx_fd(priv->net_dev, fd); 223 224 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr); 225 dma_unmap_single(dev, addr, DPAA2_ETH_RX_BUF_SIZE, DMA_FROM_DEVICE); 226 227 fas = dpaa2_get_fas(vaddr, false); 228 prefetch(fas); 229 buf_data = vaddr + dpaa2_fd_get_offset(fd); 230 prefetch(buf_data); 231 232 percpu_stats = this_cpu_ptr(priv->percpu_stats); 233 percpu_extras = this_cpu_ptr(priv->percpu_extras); 234 235 if (fd_format == dpaa2_fd_single) { 236 skb = build_linear_skb(priv, ch, fd, vaddr); 237 } else if (fd_format == dpaa2_fd_sg) { 238 skb = build_frag_skb(priv, ch, buf_data); 239 skb_free_frag(vaddr); 240 percpu_extras->rx_sg_frames++; 241 percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd); 242 } else { 243 /* We don't support any other format */ 244 goto err_frame_format; 245 } 246 247 if (unlikely(!skb)) 248 goto err_build_skb; 249 250 prefetch(skb->data); 251 252 /* Get the timestamp value */ 253 if (priv->rx_tstamp) { 254 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); 255 __le64 *ts = dpaa2_get_ts(vaddr, false); 256 u64 ns; 257 258 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 259 260 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts); 261 shhwtstamps->hwtstamp = ns_to_ktime(ns); 262 } 263 264 /* Check if we need to validate the L4 csum */ 265 if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) { 266 status = le32_to_cpu(fas->status); 267 validate_rx_csum(priv, status, skb); 268 } 269 270 skb->protocol = eth_type_trans(skb, priv->net_dev); 271 skb_record_rx_queue(skb, queue_id); 272 273 percpu_stats->rx_packets++; 274 percpu_stats->rx_bytes += dpaa2_fd_get_len(fd); 275 276 napi_gro_receive(napi, skb); 277 278 return; 279 280 err_build_skb: 281 free_rx_fd(priv, fd, vaddr); 282 err_frame_format: 283 percpu_stats->rx_dropped++; 284 } 285 286 /* Consume all frames pull-dequeued into the store. This is the simplest way to 287 * make sure we don't accidentally issue another volatile dequeue which would 288 * overwrite (leak) frames already in the store. 289 * 290 * Observance of NAPI budget is not our concern, leaving that to the caller. 291 */ 292 static int consume_frames(struct dpaa2_eth_channel *ch, 293 enum dpaa2_eth_fq_type *type) 294 { 295 struct dpaa2_eth_priv *priv = ch->priv; 296 struct dpaa2_eth_fq *fq = NULL; 297 struct dpaa2_dq *dq; 298 const struct dpaa2_fd *fd; 299 int cleaned = 0; 300 int is_last; 301 302 do { 303 dq = dpaa2_io_store_next(ch->store, &is_last); 304 if (unlikely(!dq)) { 305 /* If we're here, we *must* have placed a 306 * volatile dequeue comnmand, so keep reading through 307 * the store until we get some sort of valid response 308 * token (either a valid frame or an "empty dequeue") 309 */ 310 continue; 311 } 312 313 fd = dpaa2_dq_fd(dq); 314 fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq); 315 316 fq->consume(priv, ch, fd, &ch->napi, fq->flowid); 317 cleaned++; 318 } while (!is_last); 319 320 if (!cleaned) 321 return 0; 322 323 fq->stats.frames += cleaned; 324 ch->stats.frames += cleaned; 325 326 /* A dequeue operation only pulls frames from a single queue 327 * into the store. Return the frame queue type as an out param. 328 */ 329 if (type) 330 *type = fq->type; 331 332 return cleaned; 333 } 334 335 /* Configure the egress frame annotation for timestamp update */ 336 static void enable_tx_tstamp(struct dpaa2_fd *fd, void *buf_start) 337 { 338 struct dpaa2_faead *faead; 339 u32 ctrl, frc; 340 341 /* Mark the egress frame annotation area as valid */ 342 frc = dpaa2_fd_get_frc(fd); 343 dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV); 344 345 /* Set hardware annotation size */ 346 ctrl = dpaa2_fd_get_ctrl(fd); 347 dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL); 348 349 /* enable UPD (update prepanded data) bit in FAEAD field of 350 * hardware frame annotation area 351 */ 352 ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD; 353 faead = dpaa2_get_faead(buf_start, true); 354 faead->ctrl = cpu_to_le32(ctrl); 355 } 356 357 /* Create a frame descriptor based on a fragmented skb */ 358 static int build_sg_fd(struct dpaa2_eth_priv *priv, 359 struct sk_buff *skb, 360 struct dpaa2_fd *fd) 361 { 362 struct device *dev = priv->net_dev->dev.parent; 363 void *sgt_buf = NULL; 364 dma_addr_t addr; 365 int nr_frags = skb_shinfo(skb)->nr_frags; 366 struct dpaa2_sg_entry *sgt; 367 int i, err; 368 int sgt_buf_size; 369 struct scatterlist *scl, *crt_scl; 370 int num_sg; 371 int num_dma_bufs; 372 struct dpaa2_eth_swa *swa; 373 374 /* Create and map scatterlist. 375 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have 376 * to go beyond nr_frags+1. 377 * Note: We don't support chained scatterlists 378 */ 379 if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1)) 380 return -EINVAL; 381 382 scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC); 383 if (unlikely(!scl)) 384 return -ENOMEM; 385 386 sg_init_table(scl, nr_frags + 1); 387 num_sg = skb_to_sgvec(skb, scl, 0, skb->len); 388 num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL); 389 if (unlikely(!num_dma_bufs)) { 390 err = -ENOMEM; 391 goto dma_map_sg_failed; 392 } 393 394 /* Prepare the HW SGT structure */ 395 sgt_buf_size = priv->tx_data_offset + 396 sizeof(struct dpaa2_sg_entry) * num_dma_bufs; 397 sgt_buf = netdev_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN); 398 if (unlikely(!sgt_buf)) { 399 err = -ENOMEM; 400 goto sgt_buf_alloc_failed; 401 } 402 sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN); 403 memset(sgt_buf, 0, sgt_buf_size); 404 405 sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset); 406 407 /* Fill in the HW SGT structure. 408 * 409 * sgt_buf is zeroed out, so the following fields are implicit 410 * in all sgt entries: 411 * - offset is 0 412 * - format is 'dpaa2_sg_single' 413 */ 414 for_each_sg(scl, crt_scl, num_dma_bufs, i) { 415 dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl)); 416 dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl)); 417 } 418 dpaa2_sg_set_final(&sgt[i - 1], true); 419 420 /* Store the skb backpointer in the SGT buffer. 421 * Fit the scatterlist and the number of buffers alongside the 422 * skb backpointer in the software annotation area. We'll need 423 * all of them on Tx Conf. 424 */ 425 swa = (struct dpaa2_eth_swa *)sgt_buf; 426 swa->skb = skb; 427 swa->scl = scl; 428 swa->num_sg = num_sg; 429 swa->sgt_size = sgt_buf_size; 430 431 /* Separately map the SGT buffer */ 432 addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL); 433 if (unlikely(dma_mapping_error(dev, addr))) { 434 err = -ENOMEM; 435 goto dma_map_single_failed; 436 } 437 dpaa2_fd_set_offset(fd, priv->tx_data_offset); 438 dpaa2_fd_set_format(fd, dpaa2_fd_sg); 439 dpaa2_fd_set_addr(fd, addr); 440 dpaa2_fd_set_len(fd, skb->len); 441 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA | FD_CTRL_PTV1); 442 443 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) 444 enable_tx_tstamp(fd, sgt_buf); 445 446 return 0; 447 448 dma_map_single_failed: 449 skb_free_frag(sgt_buf); 450 sgt_buf_alloc_failed: 451 dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL); 452 dma_map_sg_failed: 453 kfree(scl); 454 return err; 455 } 456 457 /* Create a frame descriptor based on a linear skb */ 458 static int build_single_fd(struct dpaa2_eth_priv *priv, 459 struct sk_buff *skb, 460 struct dpaa2_fd *fd) 461 { 462 struct device *dev = priv->net_dev->dev.parent; 463 u8 *buffer_start, *aligned_start; 464 struct sk_buff **skbh; 465 dma_addr_t addr; 466 467 buffer_start = skb->data - dpaa2_eth_needed_headroom(priv, skb); 468 469 /* If there's enough room to align the FD address, do it. 470 * It will help hardware optimize accesses. 471 */ 472 aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN, 473 DPAA2_ETH_TX_BUF_ALIGN); 474 if (aligned_start >= skb->head) 475 buffer_start = aligned_start; 476 477 /* Store a backpointer to the skb at the beginning of the buffer 478 * (in the private data area) such that we can release it 479 * on Tx confirm 480 */ 481 skbh = (struct sk_buff **)buffer_start; 482 *skbh = skb; 483 484 addr = dma_map_single(dev, buffer_start, 485 skb_tail_pointer(skb) - buffer_start, 486 DMA_BIDIRECTIONAL); 487 if (unlikely(dma_mapping_error(dev, addr))) 488 return -ENOMEM; 489 490 dpaa2_fd_set_addr(fd, addr); 491 dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start)); 492 dpaa2_fd_set_len(fd, skb->len); 493 dpaa2_fd_set_format(fd, dpaa2_fd_single); 494 dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA | FD_CTRL_PTV1); 495 496 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) 497 enable_tx_tstamp(fd, buffer_start); 498 499 return 0; 500 } 501 502 /* FD freeing routine on the Tx path 503 * 504 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb 505 * back-pointed to is also freed. 506 * This can be called either from dpaa2_eth_tx_conf() or on the error path of 507 * dpaa2_eth_tx(). 508 */ 509 static void free_tx_fd(const struct dpaa2_eth_priv *priv, 510 const struct dpaa2_fd *fd) 511 { 512 struct device *dev = priv->net_dev->dev.parent; 513 dma_addr_t fd_addr; 514 struct sk_buff **skbh, *skb; 515 unsigned char *buffer_start; 516 struct dpaa2_eth_swa *swa; 517 u8 fd_format = dpaa2_fd_get_format(fd); 518 519 fd_addr = dpaa2_fd_get_addr(fd); 520 skbh = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr); 521 522 if (fd_format == dpaa2_fd_single) { 523 skb = *skbh; 524 buffer_start = (unsigned char *)skbh; 525 /* Accessing the skb buffer is safe before dma unmap, because 526 * we didn't map the actual skb shell. 527 */ 528 dma_unmap_single(dev, fd_addr, 529 skb_tail_pointer(skb) - buffer_start, 530 DMA_BIDIRECTIONAL); 531 } else if (fd_format == dpaa2_fd_sg) { 532 swa = (struct dpaa2_eth_swa *)skbh; 533 skb = swa->skb; 534 535 /* Unmap the scatterlist */ 536 dma_unmap_sg(dev, swa->scl, swa->num_sg, DMA_BIDIRECTIONAL); 537 kfree(swa->scl); 538 539 /* Unmap the SGT buffer */ 540 dma_unmap_single(dev, fd_addr, swa->sgt_size, 541 DMA_BIDIRECTIONAL); 542 } else { 543 netdev_dbg(priv->net_dev, "Invalid FD format\n"); 544 return; 545 } 546 547 /* Get the timestamp value */ 548 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) { 549 struct skb_shared_hwtstamps shhwtstamps; 550 __le64 *ts = dpaa2_get_ts(skbh, true); 551 u64 ns; 552 553 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 554 555 ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts); 556 shhwtstamps.hwtstamp = ns_to_ktime(ns); 557 skb_tstamp_tx(skb, &shhwtstamps); 558 } 559 560 /* Free SGT buffer allocated on tx */ 561 if (fd_format != dpaa2_fd_single) 562 skb_free_frag(skbh); 563 564 /* Move on with skb release */ 565 dev_kfree_skb(skb); 566 } 567 568 static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev) 569 { 570 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 571 struct dpaa2_fd fd; 572 struct rtnl_link_stats64 *percpu_stats; 573 struct dpaa2_eth_drv_stats *percpu_extras; 574 struct dpaa2_eth_fq *fq; 575 u16 queue_mapping; 576 unsigned int needed_headroom; 577 int err, i; 578 579 percpu_stats = this_cpu_ptr(priv->percpu_stats); 580 percpu_extras = this_cpu_ptr(priv->percpu_extras); 581 582 needed_headroom = dpaa2_eth_needed_headroom(priv, skb); 583 if (skb_headroom(skb) < needed_headroom) { 584 struct sk_buff *ns; 585 586 ns = skb_realloc_headroom(skb, needed_headroom); 587 if (unlikely(!ns)) { 588 percpu_stats->tx_dropped++; 589 goto err_alloc_headroom; 590 } 591 percpu_extras->tx_reallocs++; 592 593 if (skb->sk) 594 skb_set_owner_w(ns, skb->sk); 595 596 dev_kfree_skb(skb); 597 skb = ns; 598 } 599 600 /* We'll be holding a back-reference to the skb until Tx Confirmation; 601 * we don't want that overwritten by a concurrent Tx with a cloned skb. 602 */ 603 skb = skb_unshare(skb, GFP_ATOMIC); 604 if (unlikely(!skb)) { 605 /* skb_unshare() has already freed the skb */ 606 percpu_stats->tx_dropped++; 607 return NETDEV_TX_OK; 608 } 609 610 /* Setup the FD fields */ 611 memset(&fd, 0, sizeof(fd)); 612 613 if (skb_is_nonlinear(skb)) { 614 err = build_sg_fd(priv, skb, &fd); 615 percpu_extras->tx_sg_frames++; 616 percpu_extras->tx_sg_bytes += skb->len; 617 } else { 618 err = build_single_fd(priv, skb, &fd); 619 } 620 621 if (unlikely(err)) { 622 percpu_stats->tx_dropped++; 623 goto err_build_fd; 624 } 625 626 /* Tracing point */ 627 trace_dpaa2_tx_fd(net_dev, &fd); 628 629 /* TxConf FQ selection relies on queue id from the stack. 630 * In case of a forwarded frame from another DPNI interface, we choose 631 * a queue affined to the same core that processed the Rx frame 632 */ 633 queue_mapping = skb_get_queue_mapping(skb); 634 fq = &priv->fq[queue_mapping]; 635 for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) { 636 err = dpaa2_io_service_enqueue_qd(fq->channel->dpio, 637 priv->tx_qdid, 0, 638 fq->tx_qdbin, &fd); 639 if (err != -EBUSY) 640 break; 641 } 642 percpu_extras->tx_portal_busy += i; 643 if (unlikely(err < 0)) { 644 percpu_stats->tx_errors++; 645 /* Clean up everything, including freeing the skb */ 646 free_tx_fd(priv, &fd); 647 } else { 648 percpu_stats->tx_packets++; 649 percpu_stats->tx_bytes += dpaa2_fd_get_len(&fd); 650 } 651 652 return NETDEV_TX_OK; 653 654 err_build_fd: 655 err_alloc_headroom: 656 dev_kfree_skb(skb); 657 658 return NETDEV_TX_OK; 659 } 660 661 /* Tx confirmation frame processing routine */ 662 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv, 663 struct dpaa2_eth_channel *ch, 664 const struct dpaa2_fd *fd, 665 struct napi_struct *napi __always_unused, 666 u16 queue_id __always_unused) 667 { 668 struct rtnl_link_stats64 *percpu_stats; 669 struct dpaa2_eth_drv_stats *percpu_extras; 670 u32 fd_errors; 671 672 /* Tracing point */ 673 trace_dpaa2_tx_conf_fd(priv->net_dev, fd); 674 675 percpu_extras = this_cpu_ptr(priv->percpu_extras); 676 percpu_extras->tx_conf_frames++; 677 percpu_extras->tx_conf_bytes += dpaa2_fd_get_len(fd); 678 679 /* Check frame errors in the FD field */ 680 fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK; 681 free_tx_fd(priv, fd); 682 683 if (likely(!fd_errors)) 684 return; 685 686 if (net_ratelimit()) 687 netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n", 688 fd_errors); 689 690 percpu_stats = this_cpu_ptr(priv->percpu_stats); 691 /* Tx-conf logically pertains to the egress path. */ 692 percpu_stats->tx_errors++; 693 } 694 695 static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable) 696 { 697 int err; 698 699 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 700 DPNI_OFF_RX_L3_CSUM, enable); 701 if (err) { 702 netdev_err(priv->net_dev, 703 "dpni_set_offload(RX_L3_CSUM) failed\n"); 704 return err; 705 } 706 707 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 708 DPNI_OFF_RX_L4_CSUM, enable); 709 if (err) { 710 netdev_err(priv->net_dev, 711 "dpni_set_offload(RX_L4_CSUM) failed\n"); 712 return err; 713 } 714 715 return 0; 716 } 717 718 static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable) 719 { 720 int err; 721 722 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 723 DPNI_OFF_TX_L3_CSUM, enable); 724 if (err) { 725 netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n"); 726 return err; 727 } 728 729 err = dpni_set_offload(priv->mc_io, 0, priv->mc_token, 730 DPNI_OFF_TX_L4_CSUM, enable); 731 if (err) { 732 netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n"); 733 return err; 734 } 735 736 return 0; 737 } 738 739 /* Free buffers acquired from the buffer pool or which were meant to 740 * be released in the pool 741 */ 742 static void free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array, int count) 743 { 744 struct device *dev = priv->net_dev->dev.parent; 745 void *vaddr; 746 int i; 747 748 for (i = 0; i < count; i++) { 749 vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]); 750 dma_unmap_single(dev, buf_array[i], DPAA2_ETH_RX_BUF_SIZE, 751 DMA_FROM_DEVICE); 752 skb_free_frag(vaddr); 753 } 754 } 755 756 /* Perform a single release command to add buffers 757 * to the specified buffer pool 758 */ 759 static int add_bufs(struct dpaa2_eth_priv *priv, 760 struct dpaa2_eth_channel *ch, u16 bpid) 761 { 762 struct device *dev = priv->net_dev->dev.parent; 763 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD]; 764 void *buf; 765 dma_addr_t addr; 766 int i, err; 767 768 for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) { 769 /* Allocate buffer visible to WRIOP + skb shared info + 770 * alignment padding 771 */ 772 buf = napi_alloc_frag(dpaa2_eth_buf_raw_size(priv)); 773 if (unlikely(!buf)) 774 goto err_alloc; 775 776 buf = PTR_ALIGN(buf, priv->rx_buf_align); 777 778 addr = dma_map_single(dev, buf, DPAA2_ETH_RX_BUF_SIZE, 779 DMA_FROM_DEVICE); 780 if (unlikely(dma_mapping_error(dev, addr))) 781 goto err_map; 782 783 buf_array[i] = addr; 784 785 /* tracing point */ 786 trace_dpaa2_eth_buf_seed(priv->net_dev, 787 buf, dpaa2_eth_buf_raw_size(priv), 788 addr, DPAA2_ETH_RX_BUF_SIZE, 789 bpid); 790 } 791 792 release_bufs: 793 /* In case the portal is busy, retry until successful */ 794 while ((err = dpaa2_io_service_release(ch->dpio, bpid, 795 buf_array, i)) == -EBUSY) 796 cpu_relax(); 797 798 /* If release command failed, clean up and bail out; 799 * not much else we can do about it 800 */ 801 if (err) { 802 free_bufs(priv, buf_array, i); 803 return 0; 804 } 805 806 return i; 807 808 err_map: 809 skb_free_frag(buf); 810 err_alloc: 811 /* If we managed to allocate at least some buffers, 812 * release them to hardware 813 */ 814 if (i) 815 goto release_bufs; 816 817 return 0; 818 } 819 820 static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid) 821 { 822 int i, j; 823 int new_count; 824 825 /* This is the lazy seeding of Rx buffer pools. 826 * dpaa2_add_bufs() is also used on the Rx hotpath and calls 827 * napi_alloc_frag(). The trouble with that is that it in turn ends up 828 * calling this_cpu_ptr(), which mandates execution in atomic context. 829 * Rather than splitting up the code, do a one-off preempt disable. 830 */ 831 preempt_disable(); 832 for (j = 0; j < priv->num_channels; j++) { 833 for (i = 0; i < DPAA2_ETH_NUM_BUFS; 834 i += DPAA2_ETH_BUFS_PER_CMD) { 835 new_count = add_bufs(priv, priv->channel[j], bpid); 836 priv->channel[j]->buf_count += new_count; 837 838 if (new_count < DPAA2_ETH_BUFS_PER_CMD) { 839 preempt_enable(); 840 return -ENOMEM; 841 } 842 } 843 } 844 preempt_enable(); 845 846 return 0; 847 } 848 849 /** 850 * Drain the specified number of buffers from the DPNI's private buffer pool. 851 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD 852 */ 853 static void drain_bufs(struct dpaa2_eth_priv *priv, int count) 854 { 855 u64 buf_array[DPAA2_ETH_BUFS_PER_CMD]; 856 int ret; 857 858 do { 859 ret = dpaa2_io_service_acquire(NULL, priv->bpid, 860 buf_array, count); 861 if (ret < 0) { 862 netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n"); 863 return; 864 } 865 free_bufs(priv, buf_array, ret); 866 } while (ret); 867 } 868 869 static void drain_pool(struct dpaa2_eth_priv *priv) 870 { 871 int i; 872 873 drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD); 874 drain_bufs(priv, 1); 875 876 for (i = 0; i < priv->num_channels; i++) 877 priv->channel[i]->buf_count = 0; 878 } 879 880 /* Function is called from softirq context only, so we don't need to guard 881 * the access to percpu count 882 */ 883 static int refill_pool(struct dpaa2_eth_priv *priv, 884 struct dpaa2_eth_channel *ch, 885 u16 bpid) 886 { 887 int new_count; 888 889 if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH)) 890 return 0; 891 892 do { 893 new_count = add_bufs(priv, ch, bpid); 894 if (unlikely(!new_count)) { 895 /* Out of memory; abort for now, we'll try later on */ 896 break; 897 } 898 ch->buf_count += new_count; 899 } while (ch->buf_count < DPAA2_ETH_NUM_BUFS); 900 901 if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS)) 902 return -ENOMEM; 903 904 return 0; 905 } 906 907 static int pull_channel(struct dpaa2_eth_channel *ch) 908 { 909 int err; 910 int dequeues = -1; 911 912 /* Retry while portal is busy */ 913 do { 914 err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id, 915 ch->store); 916 dequeues++; 917 cpu_relax(); 918 } while (err == -EBUSY); 919 920 ch->stats.dequeue_portal_busy += dequeues; 921 if (unlikely(err)) 922 ch->stats.pull_err++; 923 924 return err; 925 } 926 927 /* NAPI poll routine 928 * 929 * Frames are dequeued from the QMan channel associated with this NAPI context. 930 * Rx, Tx confirmation and (if configured) Rx error frames all count 931 * towards the NAPI budget. 932 */ 933 static int dpaa2_eth_poll(struct napi_struct *napi, int budget) 934 { 935 struct dpaa2_eth_channel *ch; 936 struct dpaa2_eth_priv *priv; 937 int rx_cleaned = 0, txconf_cleaned = 0; 938 enum dpaa2_eth_fq_type type = 0; 939 int store_cleaned; 940 int err; 941 942 ch = container_of(napi, struct dpaa2_eth_channel, napi); 943 priv = ch->priv; 944 945 do { 946 err = pull_channel(ch); 947 if (unlikely(err)) 948 break; 949 950 /* Refill pool if appropriate */ 951 refill_pool(priv, ch, priv->bpid); 952 953 store_cleaned = consume_frames(ch, &type); 954 if (type == DPAA2_RX_FQ) 955 rx_cleaned += store_cleaned; 956 else 957 txconf_cleaned += store_cleaned; 958 959 /* If we either consumed the whole NAPI budget with Rx frames 960 * or we reached the Tx confirmations threshold, we're done. 961 */ 962 if (rx_cleaned >= budget || 963 txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) 964 return budget; 965 } while (store_cleaned); 966 967 /* We didn't consume the entire budget, so finish napi and 968 * re-enable data availability notifications 969 */ 970 napi_complete_done(napi, rx_cleaned); 971 do { 972 err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx); 973 cpu_relax(); 974 } while (err == -EBUSY); 975 WARN_ONCE(err, "CDAN notifications rearm failed on core %d", 976 ch->nctx.desired_cpu); 977 978 return max(rx_cleaned, 1); 979 } 980 981 static void enable_ch_napi(struct dpaa2_eth_priv *priv) 982 { 983 struct dpaa2_eth_channel *ch; 984 int i; 985 986 for (i = 0; i < priv->num_channels; i++) { 987 ch = priv->channel[i]; 988 napi_enable(&ch->napi); 989 } 990 } 991 992 static void disable_ch_napi(struct dpaa2_eth_priv *priv) 993 { 994 struct dpaa2_eth_channel *ch; 995 int i; 996 997 for (i = 0; i < priv->num_channels; i++) { 998 ch = priv->channel[i]; 999 napi_disable(&ch->napi); 1000 } 1001 } 1002 1003 static int link_state_update(struct dpaa2_eth_priv *priv) 1004 { 1005 struct dpni_link_state state = {0}; 1006 int err; 1007 1008 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state); 1009 if (unlikely(err)) { 1010 netdev_err(priv->net_dev, 1011 "dpni_get_link_state() failed\n"); 1012 return err; 1013 } 1014 1015 /* Chech link state; speed / duplex changes are not treated yet */ 1016 if (priv->link_state.up == state.up) 1017 return 0; 1018 1019 priv->link_state = state; 1020 if (state.up) { 1021 netif_carrier_on(priv->net_dev); 1022 netif_tx_start_all_queues(priv->net_dev); 1023 } else { 1024 netif_tx_stop_all_queues(priv->net_dev); 1025 netif_carrier_off(priv->net_dev); 1026 } 1027 1028 netdev_info(priv->net_dev, "Link Event: state %s\n", 1029 state.up ? "up" : "down"); 1030 1031 return 0; 1032 } 1033 1034 static int dpaa2_eth_open(struct net_device *net_dev) 1035 { 1036 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1037 int err; 1038 1039 err = seed_pool(priv, priv->bpid); 1040 if (err) { 1041 /* Not much to do; the buffer pool, though not filled up, 1042 * may still contain some buffers which would enable us 1043 * to limp on. 1044 */ 1045 netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n", 1046 priv->dpbp_dev->obj_desc.id, priv->bpid); 1047 } 1048 1049 /* We'll only start the txqs when the link is actually ready; make sure 1050 * we don't race against the link up notification, which may come 1051 * immediately after dpni_enable(); 1052 */ 1053 netif_tx_stop_all_queues(net_dev); 1054 enable_ch_napi(priv); 1055 /* Also, explicitly set carrier off, otherwise netif_carrier_ok() will 1056 * return true and cause 'ip link show' to report the LOWER_UP flag, 1057 * even though the link notification wasn't even received. 1058 */ 1059 netif_carrier_off(net_dev); 1060 1061 err = dpni_enable(priv->mc_io, 0, priv->mc_token); 1062 if (err < 0) { 1063 netdev_err(net_dev, "dpni_enable() failed\n"); 1064 goto enable_err; 1065 } 1066 1067 /* If the DPMAC object has already processed the link up interrupt, 1068 * we have to learn the link state ourselves. 1069 */ 1070 err = link_state_update(priv); 1071 if (err < 0) { 1072 netdev_err(net_dev, "Can't update link state\n"); 1073 goto link_state_err; 1074 } 1075 1076 return 0; 1077 1078 link_state_err: 1079 enable_err: 1080 disable_ch_napi(priv); 1081 drain_pool(priv); 1082 return err; 1083 } 1084 1085 /* The DPIO store must be empty when we call this, 1086 * at the end of every NAPI cycle. 1087 */ 1088 static u32 drain_channel(struct dpaa2_eth_priv *priv, 1089 struct dpaa2_eth_channel *ch) 1090 { 1091 u32 drained = 0, total = 0; 1092 1093 do { 1094 pull_channel(ch); 1095 drained = consume_frames(ch, NULL); 1096 total += drained; 1097 } while (drained); 1098 1099 return total; 1100 } 1101 1102 static u32 drain_ingress_frames(struct dpaa2_eth_priv *priv) 1103 { 1104 struct dpaa2_eth_channel *ch; 1105 int i; 1106 u32 drained = 0; 1107 1108 for (i = 0; i < priv->num_channels; i++) { 1109 ch = priv->channel[i]; 1110 drained += drain_channel(priv, ch); 1111 } 1112 1113 return drained; 1114 } 1115 1116 static int dpaa2_eth_stop(struct net_device *net_dev) 1117 { 1118 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1119 int dpni_enabled = 0; 1120 int retries = 10; 1121 u32 drained; 1122 1123 netif_tx_stop_all_queues(net_dev); 1124 netif_carrier_off(net_dev); 1125 1126 /* Loop while dpni_disable() attempts to drain the egress FQs 1127 * and confirm them back to us. 1128 */ 1129 do { 1130 dpni_disable(priv->mc_io, 0, priv->mc_token); 1131 dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled); 1132 if (dpni_enabled) 1133 /* Allow the hardware some slack */ 1134 msleep(100); 1135 } while (dpni_enabled && --retries); 1136 if (!retries) { 1137 netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n"); 1138 /* Must go on and disable NAPI nonetheless, so we don't crash at 1139 * the next "ifconfig up" 1140 */ 1141 } 1142 1143 /* Wait for NAPI to complete on every core and disable it. 1144 * In particular, this will also prevent NAPI from being rescheduled if 1145 * a new CDAN is serviced, effectively discarding the CDAN. We therefore 1146 * don't even need to disarm the channels, except perhaps for the case 1147 * of a huge coalescing value. 1148 */ 1149 disable_ch_napi(priv); 1150 1151 /* Manually drain the Rx and TxConf queues */ 1152 drained = drain_ingress_frames(priv); 1153 if (drained) 1154 netdev_dbg(net_dev, "Drained %d frames.\n", drained); 1155 1156 /* Empty the buffer pool */ 1157 drain_pool(priv); 1158 1159 return 0; 1160 } 1161 1162 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr) 1163 { 1164 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1165 struct device *dev = net_dev->dev.parent; 1166 int err; 1167 1168 err = eth_mac_addr(net_dev, addr); 1169 if (err < 0) { 1170 dev_err(dev, "eth_mac_addr() failed (%d)\n", err); 1171 return err; 1172 } 1173 1174 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token, 1175 net_dev->dev_addr); 1176 if (err) { 1177 dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err); 1178 return err; 1179 } 1180 1181 return 0; 1182 } 1183 1184 /** Fill in counters maintained by the GPP driver. These may be different from 1185 * the hardware counters obtained by ethtool. 1186 */ 1187 static void dpaa2_eth_get_stats(struct net_device *net_dev, 1188 struct rtnl_link_stats64 *stats) 1189 { 1190 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1191 struct rtnl_link_stats64 *percpu_stats; 1192 u64 *cpustats; 1193 u64 *netstats = (u64 *)stats; 1194 int i, j; 1195 int num = sizeof(struct rtnl_link_stats64) / sizeof(u64); 1196 1197 for_each_possible_cpu(i) { 1198 percpu_stats = per_cpu_ptr(priv->percpu_stats, i); 1199 cpustats = (u64 *)percpu_stats; 1200 for (j = 0; j < num; j++) 1201 netstats[j] += cpustats[j]; 1202 } 1203 } 1204 1205 /* Copy mac unicast addresses from @net_dev to @priv. 1206 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable. 1207 */ 1208 static void add_uc_hw_addr(const struct net_device *net_dev, 1209 struct dpaa2_eth_priv *priv) 1210 { 1211 struct netdev_hw_addr *ha; 1212 int err; 1213 1214 netdev_for_each_uc_addr(ha, net_dev) { 1215 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, 1216 ha->addr); 1217 if (err) 1218 netdev_warn(priv->net_dev, 1219 "Could not add ucast MAC %pM to the filtering table (err %d)\n", 1220 ha->addr, err); 1221 } 1222 } 1223 1224 /* Copy mac multicast addresses from @net_dev to @priv 1225 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable. 1226 */ 1227 static void add_mc_hw_addr(const struct net_device *net_dev, 1228 struct dpaa2_eth_priv *priv) 1229 { 1230 struct netdev_hw_addr *ha; 1231 int err; 1232 1233 netdev_for_each_mc_addr(ha, net_dev) { 1234 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, 1235 ha->addr); 1236 if (err) 1237 netdev_warn(priv->net_dev, 1238 "Could not add mcast MAC %pM to the filtering table (err %d)\n", 1239 ha->addr, err); 1240 } 1241 } 1242 1243 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev) 1244 { 1245 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1246 int uc_count = netdev_uc_count(net_dev); 1247 int mc_count = netdev_mc_count(net_dev); 1248 u8 max_mac = priv->dpni_attrs.mac_filter_entries; 1249 u32 options = priv->dpni_attrs.options; 1250 u16 mc_token = priv->mc_token; 1251 struct fsl_mc_io *mc_io = priv->mc_io; 1252 int err; 1253 1254 /* Basic sanity checks; these probably indicate a misconfiguration */ 1255 if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0) 1256 netdev_info(net_dev, 1257 "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n", 1258 max_mac); 1259 1260 /* Force promiscuous if the uc or mc counts exceed our capabilities. */ 1261 if (uc_count > max_mac) { 1262 netdev_info(net_dev, 1263 "Unicast addr count reached %d, max allowed is %d; forcing promisc\n", 1264 uc_count, max_mac); 1265 goto force_promisc; 1266 } 1267 if (mc_count + uc_count > max_mac) { 1268 netdev_info(net_dev, 1269 "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n", 1270 uc_count + mc_count, max_mac); 1271 goto force_mc_promisc; 1272 } 1273 1274 /* Adjust promisc settings due to flag combinations */ 1275 if (net_dev->flags & IFF_PROMISC) 1276 goto force_promisc; 1277 if (net_dev->flags & IFF_ALLMULTI) { 1278 /* First, rebuild unicast filtering table. This should be done 1279 * in promisc mode, in order to avoid frame loss while we 1280 * progressively add entries to the table. 1281 * We don't know whether we had been in promisc already, and 1282 * making an MC call to find out is expensive; so set uc promisc 1283 * nonetheless. 1284 */ 1285 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); 1286 if (err) 1287 netdev_warn(net_dev, "Can't set uc promisc\n"); 1288 1289 /* Actual uc table reconstruction. */ 1290 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0); 1291 if (err) 1292 netdev_warn(net_dev, "Can't clear uc filters\n"); 1293 add_uc_hw_addr(net_dev, priv); 1294 1295 /* Finally, clear uc promisc and set mc promisc as requested. */ 1296 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0); 1297 if (err) 1298 netdev_warn(net_dev, "Can't clear uc promisc\n"); 1299 goto force_mc_promisc; 1300 } 1301 1302 /* Neither unicast, nor multicast promisc will be on... eventually. 1303 * For now, rebuild mac filtering tables while forcing both of them on. 1304 */ 1305 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); 1306 if (err) 1307 netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err); 1308 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1); 1309 if (err) 1310 netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err); 1311 1312 /* Actual mac filtering tables reconstruction */ 1313 err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1); 1314 if (err) 1315 netdev_warn(net_dev, "Can't clear mac filters\n"); 1316 add_mc_hw_addr(net_dev, priv); 1317 add_uc_hw_addr(net_dev, priv); 1318 1319 /* Now we can clear both ucast and mcast promisc, without risking 1320 * to drop legitimate frames anymore. 1321 */ 1322 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0); 1323 if (err) 1324 netdev_warn(net_dev, "Can't clear ucast promisc\n"); 1325 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0); 1326 if (err) 1327 netdev_warn(net_dev, "Can't clear mcast promisc\n"); 1328 1329 return; 1330 1331 force_promisc: 1332 err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); 1333 if (err) 1334 netdev_warn(net_dev, "Can't set ucast promisc\n"); 1335 force_mc_promisc: 1336 err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1); 1337 if (err) 1338 netdev_warn(net_dev, "Can't set mcast promisc\n"); 1339 } 1340 1341 static int dpaa2_eth_set_features(struct net_device *net_dev, 1342 netdev_features_t features) 1343 { 1344 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 1345 netdev_features_t changed = features ^ net_dev->features; 1346 bool enable; 1347 int err; 1348 1349 if (changed & NETIF_F_RXCSUM) { 1350 enable = !!(features & NETIF_F_RXCSUM); 1351 err = set_rx_csum(priv, enable); 1352 if (err) 1353 return err; 1354 } 1355 1356 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) { 1357 enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)); 1358 err = set_tx_csum(priv, enable); 1359 if (err) 1360 return err; 1361 } 1362 1363 return 0; 1364 } 1365 1366 static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 1367 { 1368 struct dpaa2_eth_priv *priv = netdev_priv(dev); 1369 struct hwtstamp_config config; 1370 1371 if (copy_from_user(&config, rq->ifr_data, sizeof(config))) 1372 return -EFAULT; 1373 1374 switch (config.tx_type) { 1375 case HWTSTAMP_TX_OFF: 1376 priv->tx_tstamp = false; 1377 break; 1378 case HWTSTAMP_TX_ON: 1379 priv->tx_tstamp = true; 1380 break; 1381 default: 1382 return -ERANGE; 1383 } 1384 1385 if (config.rx_filter == HWTSTAMP_FILTER_NONE) { 1386 priv->rx_tstamp = false; 1387 } else { 1388 priv->rx_tstamp = true; 1389 /* TS is set for all frame types, not only those requested */ 1390 config.rx_filter = HWTSTAMP_FILTER_ALL; 1391 } 1392 1393 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ? 1394 -EFAULT : 0; 1395 } 1396 1397 static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 1398 { 1399 if (cmd == SIOCSHWTSTAMP) 1400 return dpaa2_eth_ts_ioctl(dev, rq, cmd); 1401 1402 return -EINVAL; 1403 } 1404 1405 static const struct net_device_ops dpaa2_eth_ops = { 1406 .ndo_open = dpaa2_eth_open, 1407 .ndo_start_xmit = dpaa2_eth_tx, 1408 .ndo_stop = dpaa2_eth_stop, 1409 .ndo_set_mac_address = dpaa2_eth_set_addr, 1410 .ndo_get_stats64 = dpaa2_eth_get_stats, 1411 .ndo_set_rx_mode = dpaa2_eth_set_rx_mode, 1412 .ndo_set_features = dpaa2_eth_set_features, 1413 .ndo_do_ioctl = dpaa2_eth_ioctl, 1414 }; 1415 1416 static void cdan_cb(struct dpaa2_io_notification_ctx *ctx) 1417 { 1418 struct dpaa2_eth_channel *ch; 1419 1420 ch = container_of(ctx, struct dpaa2_eth_channel, nctx); 1421 1422 /* Update NAPI statistics */ 1423 ch->stats.cdan++; 1424 1425 napi_schedule_irqoff(&ch->napi); 1426 } 1427 1428 /* Allocate and configure a DPCON object */ 1429 static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv) 1430 { 1431 struct fsl_mc_device *dpcon; 1432 struct device *dev = priv->net_dev->dev.parent; 1433 struct dpcon_attr attrs; 1434 int err; 1435 1436 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), 1437 FSL_MC_POOL_DPCON, &dpcon); 1438 if (err) { 1439 dev_info(dev, "Not enough DPCONs, will go on as-is\n"); 1440 return NULL; 1441 } 1442 1443 err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle); 1444 if (err) { 1445 dev_err(dev, "dpcon_open() failed\n"); 1446 goto free; 1447 } 1448 1449 err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle); 1450 if (err) { 1451 dev_err(dev, "dpcon_reset() failed\n"); 1452 goto close; 1453 } 1454 1455 err = dpcon_get_attributes(priv->mc_io, 0, dpcon->mc_handle, &attrs); 1456 if (err) { 1457 dev_err(dev, "dpcon_get_attributes() failed\n"); 1458 goto close; 1459 } 1460 1461 err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle); 1462 if (err) { 1463 dev_err(dev, "dpcon_enable() failed\n"); 1464 goto close; 1465 } 1466 1467 return dpcon; 1468 1469 close: 1470 dpcon_close(priv->mc_io, 0, dpcon->mc_handle); 1471 free: 1472 fsl_mc_object_free(dpcon); 1473 1474 return NULL; 1475 } 1476 1477 static void free_dpcon(struct dpaa2_eth_priv *priv, 1478 struct fsl_mc_device *dpcon) 1479 { 1480 dpcon_disable(priv->mc_io, 0, dpcon->mc_handle); 1481 dpcon_close(priv->mc_io, 0, dpcon->mc_handle); 1482 fsl_mc_object_free(dpcon); 1483 } 1484 1485 static struct dpaa2_eth_channel * 1486 alloc_channel(struct dpaa2_eth_priv *priv) 1487 { 1488 struct dpaa2_eth_channel *channel; 1489 struct dpcon_attr attr; 1490 struct device *dev = priv->net_dev->dev.parent; 1491 int err; 1492 1493 channel = kzalloc(sizeof(*channel), GFP_KERNEL); 1494 if (!channel) 1495 return NULL; 1496 1497 channel->dpcon = setup_dpcon(priv); 1498 if (!channel->dpcon) 1499 goto err_setup; 1500 1501 err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle, 1502 &attr); 1503 if (err) { 1504 dev_err(dev, "dpcon_get_attributes() failed\n"); 1505 goto err_get_attr; 1506 } 1507 1508 channel->dpcon_id = attr.id; 1509 channel->ch_id = attr.qbman_ch_id; 1510 channel->priv = priv; 1511 1512 return channel; 1513 1514 err_get_attr: 1515 free_dpcon(priv, channel->dpcon); 1516 err_setup: 1517 kfree(channel); 1518 return NULL; 1519 } 1520 1521 static void free_channel(struct dpaa2_eth_priv *priv, 1522 struct dpaa2_eth_channel *channel) 1523 { 1524 free_dpcon(priv, channel->dpcon); 1525 kfree(channel); 1526 } 1527 1528 /* DPIO setup: allocate and configure QBMan channels, setup core affinity 1529 * and register data availability notifications 1530 */ 1531 static int setup_dpio(struct dpaa2_eth_priv *priv) 1532 { 1533 struct dpaa2_io_notification_ctx *nctx; 1534 struct dpaa2_eth_channel *channel; 1535 struct dpcon_notification_cfg dpcon_notif_cfg; 1536 struct device *dev = priv->net_dev->dev.parent; 1537 int i, err; 1538 1539 /* We want the ability to spread ingress traffic (RX, TX conf) to as 1540 * many cores as possible, so we need one channel for each core 1541 * (unless there's fewer queues than cores, in which case the extra 1542 * channels would be wasted). 1543 * Allocate one channel per core and register it to the core's 1544 * affine DPIO. If not enough channels are available for all cores 1545 * or if some cores don't have an affine DPIO, there will be no 1546 * ingress frame processing on those cores. 1547 */ 1548 cpumask_clear(&priv->dpio_cpumask); 1549 for_each_online_cpu(i) { 1550 /* Try to allocate a channel */ 1551 channel = alloc_channel(priv); 1552 if (!channel) { 1553 dev_info(dev, 1554 "No affine channel for cpu %d and above\n", i); 1555 err = -ENODEV; 1556 goto err_alloc_ch; 1557 } 1558 1559 priv->channel[priv->num_channels] = channel; 1560 1561 nctx = &channel->nctx; 1562 nctx->is_cdan = 1; 1563 nctx->cb = cdan_cb; 1564 nctx->id = channel->ch_id; 1565 nctx->desired_cpu = i; 1566 1567 /* Register the new context */ 1568 channel->dpio = dpaa2_io_service_select(i); 1569 err = dpaa2_io_service_register(channel->dpio, nctx); 1570 if (err) { 1571 dev_dbg(dev, "No affine DPIO for cpu %d\n", i); 1572 /* If no affine DPIO for this core, there's probably 1573 * none available for next cores either. Signal we want 1574 * to retry later, in case the DPIO devices weren't 1575 * probed yet. 1576 */ 1577 err = -EPROBE_DEFER; 1578 goto err_service_reg; 1579 } 1580 1581 /* Register DPCON notification with MC */ 1582 dpcon_notif_cfg.dpio_id = nctx->dpio_id; 1583 dpcon_notif_cfg.priority = 0; 1584 dpcon_notif_cfg.user_ctx = nctx->qman64; 1585 err = dpcon_set_notification(priv->mc_io, 0, 1586 channel->dpcon->mc_handle, 1587 &dpcon_notif_cfg); 1588 if (err) { 1589 dev_err(dev, "dpcon_set_notification failed()\n"); 1590 goto err_set_cdan; 1591 } 1592 1593 /* If we managed to allocate a channel and also found an affine 1594 * DPIO for this core, add it to the final mask 1595 */ 1596 cpumask_set_cpu(i, &priv->dpio_cpumask); 1597 priv->num_channels++; 1598 1599 /* Stop if we already have enough channels to accommodate all 1600 * RX and TX conf queues 1601 */ 1602 if (priv->num_channels == dpaa2_eth_queue_count(priv)) 1603 break; 1604 } 1605 1606 return 0; 1607 1608 err_set_cdan: 1609 dpaa2_io_service_deregister(channel->dpio, nctx); 1610 err_service_reg: 1611 free_channel(priv, channel); 1612 err_alloc_ch: 1613 if (cpumask_empty(&priv->dpio_cpumask)) { 1614 dev_err(dev, "No cpu with an affine DPIO/DPCON\n"); 1615 return err; 1616 } 1617 1618 dev_info(dev, "Cores %*pbl available for processing ingress traffic\n", 1619 cpumask_pr_args(&priv->dpio_cpumask)); 1620 1621 return 0; 1622 } 1623 1624 static void free_dpio(struct dpaa2_eth_priv *priv) 1625 { 1626 int i; 1627 struct dpaa2_eth_channel *ch; 1628 1629 /* deregister CDAN notifications and free channels */ 1630 for (i = 0; i < priv->num_channels; i++) { 1631 ch = priv->channel[i]; 1632 dpaa2_io_service_deregister(ch->dpio, &ch->nctx); 1633 free_channel(priv, ch); 1634 } 1635 } 1636 1637 static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv, 1638 int cpu) 1639 { 1640 struct device *dev = priv->net_dev->dev.parent; 1641 int i; 1642 1643 for (i = 0; i < priv->num_channels; i++) 1644 if (priv->channel[i]->nctx.desired_cpu == cpu) 1645 return priv->channel[i]; 1646 1647 /* We should never get here. Issue a warning and return 1648 * the first channel, because it's still better than nothing 1649 */ 1650 dev_warn(dev, "No affine channel found for cpu %d\n", cpu); 1651 1652 return priv->channel[0]; 1653 } 1654 1655 static void set_fq_affinity(struct dpaa2_eth_priv *priv) 1656 { 1657 struct device *dev = priv->net_dev->dev.parent; 1658 struct cpumask xps_mask; 1659 struct dpaa2_eth_fq *fq; 1660 int rx_cpu, txc_cpu; 1661 int i, err; 1662 1663 /* For each FQ, pick one channel/CPU to deliver frames to. 1664 * This may well change at runtime, either through irqbalance or 1665 * through direct user intervention. 1666 */ 1667 rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask); 1668 1669 for (i = 0; i < priv->num_fqs; i++) { 1670 fq = &priv->fq[i]; 1671 switch (fq->type) { 1672 case DPAA2_RX_FQ: 1673 fq->target_cpu = rx_cpu; 1674 rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask); 1675 if (rx_cpu >= nr_cpu_ids) 1676 rx_cpu = cpumask_first(&priv->dpio_cpumask); 1677 break; 1678 case DPAA2_TX_CONF_FQ: 1679 fq->target_cpu = txc_cpu; 1680 1681 /* Tell the stack to affine to txc_cpu the Tx queue 1682 * associated with the confirmation one 1683 */ 1684 cpumask_clear(&xps_mask); 1685 cpumask_set_cpu(txc_cpu, &xps_mask); 1686 err = netif_set_xps_queue(priv->net_dev, &xps_mask, 1687 fq->flowid); 1688 if (err) 1689 dev_err(dev, "Error setting XPS queue\n"); 1690 1691 txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask); 1692 if (txc_cpu >= nr_cpu_ids) 1693 txc_cpu = cpumask_first(&priv->dpio_cpumask); 1694 break; 1695 default: 1696 dev_err(dev, "Unknown FQ type: %d\n", fq->type); 1697 } 1698 fq->channel = get_affine_channel(priv, fq->target_cpu); 1699 } 1700 } 1701 1702 static void setup_fqs(struct dpaa2_eth_priv *priv) 1703 { 1704 int i; 1705 1706 /* We have one TxConf FQ per Tx flow. 1707 * The number of Tx and Rx queues is the same. 1708 * Tx queues come first in the fq array. 1709 */ 1710 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) { 1711 priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ; 1712 priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf; 1713 priv->fq[priv->num_fqs++].flowid = (u16)i; 1714 } 1715 1716 for (i = 0; i < dpaa2_eth_queue_count(priv); i++) { 1717 priv->fq[priv->num_fqs].type = DPAA2_RX_FQ; 1718 priv->fq[priv->num_fqs].consume = dpaa2_eth_rx; 1719 priv->fq[priv->num_fqs++].flowid = (u16)i; 1720 } 1721 1722 /* For each FQ, decide on which core to process incoming frames */ 1723 set_fq_affinity(priv); 1724 } 1725 1726 /* Allocate and configure one buffer pool for each interface */ 1727 static int setup_dpbp(struct dpaa2_eth_priv *priv) 1728 { 1729 int err; 1730 struct fsl_mc_device *dpbp_dev; 1731 struct device *dev = priv->net_dev->dev.parent; 1732 struct dpbp_attr dpbp_attrs; 1733 1734 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP, 1735 &dpbp_dev); 1736 if (err) { 1737 dev_err(dev, "DPBP device allocation failed\n"); 1738 return err; 1739 } 1740 1741 priv->dpbp_dev = dpbp_dev; 1742 1743 err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id, 1744 &dpbp_dev->mc_handle); 1745 if (err) { 1746 dev_err(dev, "dpbp_open() failed\n"); 1747 goto err_open; 1748 } 1749 1750 err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle); 1751 if (err) { 1752 dev_err(dev, "dpbp_reset() failed\n"); 1753 goto err_reset; 1754 } 1755 1756 err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle); 1757 if (err) { 1758 dev_err(dev, "dpbp_enable() failed\n"); 1759 goto err_enable; 1760 } 1761 1762 err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle, 1763 &dpbp_attrs); 1764 if (err) { 1765 dev_err(dev, "dpbp_get_attributes() failed\n"); 1766 goto err_get_attr; 1767 } 1768 priv->bpid = dpbp_attrs.bpid; 1769 1770 return 0; 1771 1772 err_get_attr: 1773 dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle); 1774 err_enable: 1775 err_reset: 1776 dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle); 1777 err_open: 1778 fsl_mc_object_free(dpbp_dev); 1779 1780 return err; 1781 } 1782 1783 static void free_dpbp(struct dpaa2_eth_priv *priv) 1784 { 1785 drain_pool(priv); 1786 dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle); 1787 dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle); 1788 fsl_mc_object_free(priv->dpbp_dev); 1789 } 1790 1791 static int set_buffer_layout(struct dpaa2_eth_priv *priv) 1792 { 1793 struct device *dev = priv->net_dev->dev.parent; 1794 struct dpni_buffer_layout buf_layout = {0}; 1795 int err; 1796 1797 /* We need to check for WRIOP version 1.0.0, but depending on the MC 1798 * version, this number is not always provided correctly on rev1. 1799 * We need to check for both alternatives in this situation. 1800 */ 1801 if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) || 1802 priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0)) 1803 priv->rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1; 1804 else 1805 priv->rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN; 1806 1807 /* tx buffer */ 1808 buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE; 1809 buf_layout.pass_timestamp = true; 1810 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE | 1811 DPNI_BUF_LAYOUT_OPT_TIMESTAMP; 1812 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 1813 DPNI_QUEUE_TX, &buf_layout); 1814 if (err) { 1815 dev_err(dev, "dpni_set_buffer_layout(TX) failed\n"); 1816 return err; 1817 } 1818 1819 /* tx-confirm buffer */ 1820 buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP; 1821 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 1822 DPNI_QUEUE_TX_CONFIRM, &buf_layout); 1823 if (err) { 1824 dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n"); 1825 return err; 1826 } 1827 1828 /* Now that we've set our tx buffer layout, retrieve the minimum 1829 * required tx data offset. 1830 */ 1831 err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token, 1832 &priv->tx_data_offset); 1833 if (err) { 1834 dev_err(dev, "dpni_get_tx_data_offset() failed\n"); 1835 return err; 1836 } 1837 1838 if ((priv->tx_data_offset % 64) != 0) 1839 dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n", 1840 priv->tx_data_offset); 1841 1842 /* rx buffer */ 1843 buf_layout.pass_frame_status = true; 1844 buf_layout.pass_parser_result = true; 1845 buf_layout.data_align = priv->rx_buf_align; 1846 buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv); 1847 buf_layout.private_data_size = 0; 1848 buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT | 1849 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS | 1850 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN | 1851 DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM | 1852 DPNI_BUF_LAYOUT_OPT_TIMESTAMP; 1853 err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token, 1854 DPNI_QUEUE_RX, &buf_layout); 1855 if (err) { 1856 dev_err(dev, "dpni_set_buffer_layout(RX) failed\n"); 1857 return err; 1858 } 1859 1860 return 0; 1861 } 1862 1863 /* Configure the DPNI object this interface is associated with */ 1864 static int setup_dpni(struct fsl_mc_device *ls_dev) 1865 { 1866 struct device *dev = &ls_dev->dev; 1867 struct dpaa2_eth_priv *priv; 1868 struct net_device *net_dev; 1869 int err; 1870 1871 net_dev = dev_get_drvdata(dev); 1872 priv = netdev_priv(net_dev); 1873 1874 /* get a handle for the DPNI object */ 1875 err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token); 1876 if (err) { 1877 dev_err(dev, "dpni_open() failed\n"); 1878 return err; 1879 } 1880 1881 /* Check if we can work with this DPNI object */ 1882 err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major, 1883 &priv->dpni_ver_minor); 1884 if (err) { 1885 dev_err(dev, "dpni_get_api_version() failed\n"); 1886 goto close; 1887 } 1888 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) { 1889 dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n", 1890 priv->dpni_ver_major, priv->dpni_ver_minor, 1891 DPNI_VER_MAJOR, DPNI_VER_MINOR); 1892 err = -ENOTSUPP; 1893 goto close; 1894 } 1895 1896 ls_dev->mc_io = priv->mc_io; 1897 ls_dev->mc_handle = priv->mc_token; 1898 1899 err = dpni_reset(priv->mc_io, 0, priv->mc_token); 1900 if (err) { 1901 dev_err(dev, "dpni_reset() failed\n"); 1902 goto close; 1903 } 1904 1905 err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token, 1906 &priv->dpni_attrs); 1907 if (err) { 1908 dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err); 1909 goto close; 1910 } 1911 1912 err = set_buffer_layout(priv); 1913 if (err) 1914 goto close; 1915 1916 priv->cls_rules = devm_kzalloc(dev, sizeof(struct dpaa2_eth_cls_rule) * 1917 dpaa2_eth_fs_count(priv), GFP_KERNEL); 1918 if (!priv->cls_rules) 1919 goto close; 1920 1921 return 0; 1922 1923 close: 1924 dpni_close(priv->mc_io, 0, priv->mc_token); 1925 1926 return err; 1927 } 1928 1929 static void free_dpni(struct dpaa2_eth_priv *priv) 1930 { 1931 int err; 1932 1933 err = dpni_reset(priv->mc_io, 0, priv->mc_token); 1934 if (err) 1935 netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n", 1936 err); 1937 1938 dpni_close(priv->mc_io, 0, priv->mc_token); 1939 } 1940 1941 static int setup_rx_flow(struct dpaa2_eth_priv *priv, 1942 struct dpaa2_eth_fq *fq) 1943 { 1944 struct device *dev = priv->net_dev->dev.parent; 1945 struct dpni_queue queue; 1946 struct dpni_queue_id qid; 1947 struct dpni_taildrop td; 1948 int err; 1949 1950 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 1951 DPNI_QUEUE_RX, 0, fq->flowid, &queue, &qid); 1952 if (err) { 1953 dev_err(dev, "dpni_get_queue(RX) failed\n"); 1954 return err; 1955 } 1956 1957 fq->fqid = qid.fqid; 1958 1959 queue.destination.id = fq->channel->dpcon_id; 1960 queue.destination.type = DPNI_DEST_DPCON; 1961 queue.destination.priority = 1; 1962 queue.user_context = (u64)(uintptr_t)fq; 1963 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token, 1964 DPNI_QUEUE_RX, 0, fq->flowid, 1965 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST, 1966 &queue); 1967 if (err) { 1968 dev_err(dev, "dpni_set_queue(RX) failed\n"); 1969 return err; 1970 } 1971 1972 td.enable = 1; 1973 td.threshold = DPAA2_ETH_TAILDROP_THRESH; 1974 err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, DPNI_CP_QUEUE, 1975 DPNI_QUEUE_RX, 0, fq->flowid, &td); 1976 if (err) { 1977 dev_err(dev, "dpni_set_threshold() failed\n"); 1978 return err; 1979 } 1980 1981 return 0; 1982 } 1983 1984 static int setup_tx_flow(struct dpaa2_eth_priv *priv, 1985 struct dpaa2_eth_fq *fq) 1986 { 1987 struct device *dev = priv->net_dev->dev.parent; 1988 struct dpni_queue queue; 1989 struct dpni_queue_id qid; 1990 int err; 1991 1992 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 1993 DPNI_QUEUE_TX, 0, fq->flowid, &queue, &qid); 1994 if (err) { 1995 dev_err(dev, "dpni_get_queue(TX) failed\n"); 1996 return err; 1997 } 1998 1999 fq->tx_qdbin = qid.qdbin; 2000 2001 err = dpni_get_queue(priv->mc_io, 0, priv->mc_token, 2002 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid, 2003 &queue, &qid); 2004 if (err) { 2005 dev_err(dev, "dpni_get_queue(TX_CONF) failed\n"); 2006 return err; 2007 } 2008 2009 fq->fqid = qid.fqid; 2010 2011 queue.destination.id = fq->channel->dpcon_id; 2012 queue.destination.type = DPNI_DEST_DPCON; 2013 queue.destination.priority = 0; 2014 queue.user_context = (u64)(uintptr_t)fq; 2015 err = dpni_set_queue(priv->mc_io, 0, priv->mc_token, 2016 DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid, 2017 DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST, 2018 &queue); 2019 if (err) { 2020 dev_err(dev, "dpni_set_queue(TX_CONF) failed\n"); 2021 return err; 2022 } 2023 2024 return 0; 2025 } 2026 2027 /* Supported header fields for Rx hash distribution key */ 2028 static const struct dpaa2_eth_dist_fields dist_fields[] = { 2029 { 2030 /* L2 header */ 2031 .rxnfc_field = RXH_L2DA, 2032 .cls_prot = NET_PROT_ETH, 2033 .cls_field = NH_FLD_ETH_DA, 2034 .size = 6, 2035 }, { 2036 .cls_prot = NET_PROT_ETH, 2037 .cls_field = NH_FLD_ETH_SA, 2038 .size = 6, 2039 }, { 2040 /* This is the last ethertype field parsed: 2041 * depending on frame format, it can be the MAC ethertype 2042 * or the VLAN etype. 2043 */ 2044 .cls_prot = NET_PROT_ETH, 2045 .cls_field = NH_FLD_ETH_TYPE, 2046 .size = 2, 2047 }, { 2048 /* VLAN header */ 2049 .rxnfc_field = RXH_VLAN, 2050 .cls_prot = NET_PROT_VLAN, 2051 .cls_field = NH_FLD_VLAN_TCI, 2052 .size = 2, 2053 }, { 2054 /* IP header */ 2055 .rxnfc_field = RXH_IP_SRC, 2056 .cls_prot = NET_PROT_IP, 2057 .cls_field = NH_FLD_IP_SRC, 2058 .size = 4, 2059 }, { 2060 .rxnfc_field = RXH_IP_DST, 2061 .cls_prot = NET_PROT_IP, 2062 .cls_field = NH_FLD_IP_DST, 2063 .size = 4, 2064 }, { 2065 .rxnfc_field = RXH_L3_PROTO, 2066 .cls_prot = NET_PROT_IP, 2067 .cls_field = NH_FLD_IP_PROTO, 2068 .size = 1, 2069 }, { 2070 /* Using UDP ports, this is functionally equivalent to raw 2071 * byte pairs from L4 header. 2072 */ 2073 .rxnfc_field = RXH_L4_B_0_1, 2074 .cls_prot = NET_PROT_UDP, 2075 .cls_field = NH_FLD_UDP_PORT_SRC, 2076 .size = 2, 2077 }, { 2078 .rxnfc_field = RXH_L4_B_2_3, 2079 .cls_prot = NET_PROT_UDP, 2080 .cls_field = NH_FLD_UDP_PORT_DST, 2081 .size = 2, 2082 }, 2083 }; 2084 2085 /* Configure the Rx hash key using the legacy API */ 2086 static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key) 2087 { 2088 struct device *dev = priv->net_dev->dev.parent; 2089 struct dpni_rx_tc_dist_cfg dist_cfg; 2090 int err; 2091 2092 memset(&dist_cfg, 0, sizeof(dist_cfg)); 2093 2094 dist_cfg.key_cfg_iova = key; 2095 dist_cfg.dist_size = dpaa2_eth_queue_count(priv); 2096 dist_cfg.dist_mode = DPNI_DIST_MODE_HASH; 2097 2098 err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg); 2099 if (err) 2100 dev_err(dev, "dpni_set_rx_tc_dist failed\n"); 2101 2102 return err; 2103 } 2104 2105 /* Configure the Rx hash key using the new API */ 2106 static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key) 2107 { 2108 struct device *dev = priv->net_dev->dev.parent; 2109 struct dpni_rx_dist_cfg dist_cfg; 2110 int err; 2111 2112 memset(&dist_cfg, 0, sizeof(dist_cfg)); 2113 2114 dist_cfg.key_cfg_iova = key; 2115 dist_cfg.dist_size = dpaa2_eth_queue_count(priv); 2116 dist_cfg.enable = 1; 2117 2118 err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg); 2119 if (err) 2120 dev_err(dev, "dpni_set_rx_hash_dist failed\n"); 2121 2122 return err; 2123 } 2124 2125 /* Configure the Rx flow classification key */ 2126 static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key) 2127 { 2128 struct device *dev = priv->net_dev->dev.parent; 2129 struct dpni_rx_dist_cfg dist_cfg; 2130 int err; 2131 2132 memset(&dist_cfg, 0, sizeof(dist_cfg)); 2133 2134 dist_cfg.key_cfg_iova = key; 2135 dist_cfg.dist_size = dpaa2_eth_queue_count(priv); 2136 dist_cfg.enable = 1; 2137 2138 err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg); 2139 if (err) 2140 dev_err(dev, "dpni_set_rx_fs_dist failed\n"); 2141 2142 return err; 2143 } 2144 2145 /* Size of the Rx flow classification key */ 2146 int dpaa2_eth_cls_key_size(void) 2147 { 2148 int i, size = 0; 2149 2150 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) 2151 size += dist_fields[i].size; 2152 2153 return size; 2154 } 2155 2156 /* Offset of header field in Rx classification key */ 2157 int dpaa2_eth_cls_fld_off(int prot, int field) 2158 { 2159 int i, off = 0; 2160 2161 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { 2162 if (dist_fields[i].cls_prot == prot && 2163 dist_fields[i].cls_field == field) 2164 return off; 2165 off += dist_fields[i].size; 2166 } 2167 2168 WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n"); 2169 return 0; 2170 } 2171 2172 /* Set Rx distribution (hash or flow classification) key 2173 * flags is a combination of RXH_ bits 2174 */ 2175 static int dpaa2_eth_set_dist_key(struct net_device *net_dev, 2176 enum dpaa2_eth_rx_dist type, u64 flags) 2177 { 2178 struct device *dev = net_dev->dev.parent; 2179 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2180 struct dpkg_profile_cfg cls_cfg; 2181 u32 rx_hash_fields = 0; 2182 dma_addr_t key_iova; 2183 u8 *dma_mem; 2184 int i; 2185 int err = 0; 2186 2187 memset(&cls_cfg, 0, sizeof(cls_cfg)); 2188 2189 for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { 2190 struct dpkg_extract *key = 2191 &cls_cfg.extracts[cls_cfg.num_extracts]; 2192 2193 /* For Rx hashing key we set only the selected fields. 2194 * For Rx flow classification key we set all supported fields 2195 */ 2196 if (type == DPAA2_ETH_RX_DIST_HASH) { 2197 if (!(flags & dist_fields[i].rxnfc_field)) 2198 continue; 2199 rx_hash_fields |= dist_fields[i].rxnfc_field; 2200 } 2201 2202 if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) { 2203 dev_err(dev, "error adding key extraction rule, too many rules?\n"); 2204 return -E2BIG; 2205 } 2206 2207 key->type = DPKG_EXTRACT_FROM_HDR; 2208 key->extract.from_hdr.prot = dist_fields[i].cls_prot; 2209 key->extract.from_hdr.type = DPKG_FULL_FIELD; 2210 key->extract.from_hdr.field = dist_fields[i].cls_field; 2211 cls_cfg.num_extracts++; 2212 } 2213 2214 dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL); 2215 if (!dma_mem) 2216 return -ENOMEM; 2217 2218 err = dpni_prepare_key_cfg(&cls_cfg, dma_mem); 2219 if (err) { 2220 dev_err(dev, "dpni_prepare_key_cfg error %d\n", err); 2221 goto free_key; 2222 } 2223 2224 /* Prepare for setting the rx dist */ 2225 key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE, 2226 DMA_TO_DEVICE); 2227 if (dma_mapping_error(dev, key_iova)) { 2228 dev_err(dev, "DMA mapping failed\n"); 2229 err = -ENOMEM; 2230 goto free_key; 2231 } 2232 2233 if (type == DPAA2_ETH_RX_DIST_HASH) { 2234 if (dpaa2_eth_has_legacy_dist(priv)) 2235 err = config_legacy_hash_key(priv, key_iova); 2236 else 2237 err = config_hash_key(priv, key_iova); 2238 } else { 2239 err = config_cls_key(priv, key_iova); 2240 } 2241 2242 dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE, 2243 DMA_TO_DEVICE); 2244 if (!err && type == DPAA2_ETH_RX_DIST_HASH) 2245 priv->rx_hash_fields = rx_hash_fields; 2246 2247 free_key: 2248 kfree(dma_mem); 2249 return err; 2250 } 2251 2252 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags) 2253 { 2254 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2255 2256 if (!dpaa2_eth_hash_enabled(priv)) 2257 return -EOPNOTSUPP; 2258 2259 return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, flags); 2260 } 2261 2262 static int dpaa2_eth_set_cls(struct dpaa2_eth_priv *priv) 2263 { 2264 struct device *dev = priv->net_dev->dev.parent; 2265 2266 /* Check if we actually support Rx flow classification */ 2267 if (dpaa2_eth_has_legacy_dist(priv)) { 2268 dev_dbg(dev, "Rx cls not supported by current MC version\n"); 2269 return -EOPNOTSUPP; 2270 } 2271 2272 if (priv->dpni_attrs.options & DPNI_OPT_NO_FS || 2273 !(priv->dpni_attrs.options & DPNI_OPT_HAS_KEY_MASKING)) { 2274 dev_dbg(dev, "Rx cls disabled in DPNI options\n"); 2275 return -EOPNOTSUPP; 2276 } 2277 2278 if (!dpaa2_eth_hash_enabled(priv)) { 2279 dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n"); 2280 return -EOPNOTSUPP; 2281 } 2282 2283 priv->rx_cls_enabled = 1; 2284 2285 return dpaa2_eth_set_dist_key(priv->net_dev, DPAA2_ETH_RX_DIST_CLS, 0); 2286 } 2287 2288 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs, 2289 * frame queues and channels 2290 */ 2291 static int bind_dpni(struct dpaa2_eth_priv *priv) 2292 { 2293 struct net_device *net_dev = priv->net_dev; 2294 struct device *dev = net_dev->dev.parent; 2295 struct dpni_pools_cfg pools_params; 2296 struct dpni_error_cfg err_cfg; 2297 int err = 0; 2298 int i; 2299 2300 pools_params.num_dpbp = 1; 2301 pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id; 2302 pools_params.pools[0].backup_pool = 0; 2303 pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE; 2304 err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params); 2305 if (err) { 2306 dev_err(dev, "dpni_set_pools() failed\n"); 2307 return err; 2308 } 2309 2310 /* have the interface implicitly distribute traffic based on 2311 * the default hash key 2312 */ 2313 err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT); 2314 if (err && err != -EOPNOTSUPP) 2315 dev_err(dev, "Failed to configure hashing\n"); 2316 2317 /* Configure the flow classification key; it includes all 2318 * supported header fields and cannot be modified at runtime 2319 */ 2320 err = dpaa2_eth_set_cls(priv); 2321 if (err && err != -EOPNOTSUPP) 2322 dev_err(dev, "Failed to configure Rx classification key\n"); 2323 2324 /* Configure handling of error frames */ 2325 err_cfg.errors = DPAA2_FAS_RX_ERR_MASK; 2326 err_cfg.set_frame_annotation = 1; 2327 err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD; 2328 err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token, 2329 &err_cfg); 2330 if (err) { 2331 dev_err(dev, "dpni_set_errors_behavior failed\n"); 2332 return err; 2333 } 2334 2335 /* Configure Rx and Tx conf queues to generate CDANs */ 2336 for (i = 0; i < priv->num_fqs; i++) { 2337 switch (priv->fq[i].type) { 2338 case DPAA2_RX_FQ: 2339 err = setup_rx_flow(priv, &priv->fq[i]); 2340 break; 2341 case DPAA2_TX_CONF_FQ: 2342 err = setup_tx_flow(priv, &priv->fq[i]); 2343 break; 2344 default: 2345 dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type); 2346 return -EINVAL; 2347 } 2348 if (err) 2349 return err; 2350 } 2351 2352 err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token, 2353 DPNI_QUEUE_TX, &priv->tx_qdid); 2354 if (err) { 2355 dev_err(dev, "dpni_get_qdid() failed\n"); 2356 return err; 2357 } 2358 2359 return 0; 2360 } 2361 2362 /* Allocate rings for storing incoming frame descriptors */ 2363 static int alloc_rings(struct dpaa2_eth_priv *priv) 2364 { 2365 struct net_device *net_dev = priv->net_dev; 2366 struct device *dev = net_dev->dev.parent; 2367 int i; 2368 2369 for (i = 0; i < priv->num_channels; i++) { 2370 priv->channel[i]->store = 2371 dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev); 2372 if (!priv->channel[i]->store) { 2373 netdev_err(net_dev, "dpaa2_io_store_create() failed\n"); 2374 goto err_ring; 2375 } 2376 } 2377 2378 return 0; 2379 2380 err_ring: 2381 for (i = 0; i < priv->num_channels; i++) { 2382 if (!priv->channel[i]->store) 2383 break; 2384 dpaa2_io_store_destroy(priv->channel[i]->store); 2385 } 2386 2387 return -ENOMEM; 2388 } 2389 2390 static void free_rings(struct dpaa2_eth_priv *priv) 2391 { 2392 int i; 2393 2394 for (i = 0; i < priv->num_channels; i++) 2395 dpaa2_io_store_destroy(priv->channel[i]->store); 2396 } 2397 2398 static int set_mac_addr(struct dpaa2_eth_priv *priv) 2399 { 2400 struct net_device *net_dev = priv->net_dev; 2401 struct device *dev = net_dev->dev.parent; 2402 u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN]; 2403 int err; 2404 2405 /* Get firmware address, if any */ 2406 err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr); 2407 if (err) { 2408 dev_err(dev, "dpni_get_port_mac_addr() failed\n"); 2409 return err; 2410 } 2411 2412 /* Get DPNI attributes address, if any */ 2413 err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token, 2414 dpni_mac_addr); 2415 if (err) { 2416 dev_err(dev, "dpni_get_primary_mac_addr() failed\n"); 2417 return err; 2418 } 2419 2420 /* First check if firmware has any address configured by bootloader */ 2421 if (!is_zero_ether_addr(mac_addr)) { 2422 /* If the DPMAC addr != DPNI addr, update it */ 2423 if (!ether_addr_equal(mac_addr, dpni_mac_addr)) { 2424 err = dpni_set_primary_mac_addr(priv->mc_io, 0, 2425 priv->mc_token, 2426 mac_addr); 2427 if (err) { 2428 dev_err(dev, "dpni_set_primary_mac_addr() failed\n"); 2429 return err; 2430 } 2431 } 2432 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len); 2433 } else if (is_zero_ether_addr(dpni_mac_addr)) { 2434 /* No MAC address configured, fill in net_dev->dev_addr 2435 * with a random one 2436 */ 2437 eth_hw_addr_random(net_dev); 2438 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n"); 2439 2440 err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token, 2441 net_dev->dev_addr); 2442 if (err) { 2443 dev_err(dev, "dpni_set_primary_mac_addr() failed\n"); 2444 return err; 2445 } 2446 2447 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all 2448 * practical purposes, this will be our "permanent" mac address, 2449 * at least until the next reboot. This move will also permit 2450 * register_netdevice() to properly fill up net_dev->perm_addr. 2451 */ 2452 net_dev->addr_assign_type = NET_ADDR_PERM; 2453 } else { 2454 /* NET_ADDR_PERM is default, all we have to do is 2455 * fill in the device addr. 2456 */ 2457 memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len); 2458 } 2459 2460 return 0; 2461 } 2462 2463 static int netdev_init(struct net_device *net_dev) 2464 { 2465 struct device *dev = net_dev->dev.parent; 2466 struct dpaa2_eth_priv *priv = netdev_priv(net_dev); 2467 u32 options = priv->dpni_attrs.options; 2468 u64 supported = 0, not_supported = 0; 2469 u8 bcast_addr[ETH_ALEN]; 2470 u8 num_queues; 2471 int err; 2472 2473 net_dev->netdev_ops = &dpaa2_eth_ops; 2474 net_dev->ethtool_ops = &dpaa2_ethtool_ops; 2475 2476 err = set_mac_addr(priv); 2477 if (err) 2478 return err; 2479 2480 /* Explicitly add the broadcast address to the MAC filtering table */ 2481 eth_broadcast_addr(bcast_addr); 2482 err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr); 2483 if (err) { 2484 dev_err(dev, "dpni_add_mac_addr() failed\n"); 2485 return err; 2486 } 2487 2488 /* Set MTU upper limit; lower limit is 68B (default value) */ 2489 net_dev->max_mtu = DPAA2_ETH_MAX_MTU; 2490 err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, 2491 DPAA2_ETH_MFL); 2492 if (err) { 2493 dev_err(dev, "dpni_set_max_frame_length() failed\n"); 2494 return err; 2495 } 2496 2497 /* Set actual number of queues in the net device */ 2498 num_queues = dpaa2_eth_queue_count(priv); 2499 err = netif_set_real_num_tx_queues(net_dev, num_queues); 2500 if (err) { 2501 dev_err(dev, "netif_set_real_num_tx_queues() failed\n"); 2502 return err; 2503 } 2504 err = netif_set_real_num_rx_queues(net_dev, num_queues); 2505 if (err) { 2506 dev_err(dev, "netif_set_real_num_rx_queues() failed\n"); 2507 return err; 2508 } 2509 2510 /* Capabilities listing */ 2511 supported |= IFF_LIVE_ADDR_CHANGE; 2512 2513 if (options & DPNI_OPT_NO_MAC_FILTER) 2514 not_supported |= IFF_UNICAST_FLT; 2515 else 2516 supported |= IFF_UNICAST_FLT; 2517 2518 net_dev->priv_flags |= supported; 2519 net_dev->priv_flags &= ~not_supported; 2520 2521 /* Features */ 2522 net_dev->features = NETIF_F_RXCSUM | 2523 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 2524 NETIF_F_SG | NETIF_F_HIGHDMA | 2525 NETIF_F_LLTX; 2526 net_dev->hw_features = net_dev->features; 2527 2528 return 0; 2529 } 2530 2531 static int poll_link_state(void *arg) 2532 { 2533 struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg; 2534 int err; 2535 2536 while (!kthread_should_stop()) { 2537 err = link_state_update(priv); 2538 if (unlikely(err)) 2539 return err; 2540 2541 msleep(DPAA2_ETH_LINK_STATE_REFRESH); 2542 } 2543 2544 return 0; 2545 } 2546 2547 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg) 2548 { 2549 u32 status = ~0; 2550 struct device *dev = (struct device *)arg; 2551 struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev); 2552 struct net_device *net_dev = dev_get_drvdata(dev); 2553 int err; 2554 2555 err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle, 2556 DPNI_IRQ_INDEX, &status); 2557 if (unlikely(err)) { 2558 netdev_err(net_dev, "Can't get irq status (err %d)\n", err); 2559 return IRQ_HANDLED; 2560 } 2561 2562 if (status & DPNI_IRQ_EVENT_LINK_CHANGED) 2563 link_state_update(netdev_priv(net_dev)); 2564 2565 return IRQ_HANDLED; 2566 } 2567 2568 static int setup_irqs(struct fsl_mc_device *ls_dev) 2569 { 2570 int err = 0; 2571 struct fsl_mc_device_irq *irq; 2572 2573 err = fsl_mc_allocate_irqs(ls_dev); 2574 if (err) { 2575 dev_err(&ls_dev->dev, "MC irqs allocation failed\n"); 2576 return err; 2577 } 2578 2579 irq = ls_dev->irqs[0]; 2580 err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq, 2581 NULL, dpni_irq0_handler_thread, 2582 IRQF_NO_SUSPEND | IRQF_ONESHOT, 2583 dev_name(&ls_dev->dev), &ls_dev->dev); 2584 if (err < 0) { 2585 dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err); 2586 goto free_mc_irq; 2587 } 2588 2589 err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle, 2590 DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED); 2591 if (err < 0) { 2592 dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err); 2593 goto free_irq; 2594 } 2595 2596 err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle, 2597 DPNI_IRQ_INDEX, 1); 2598 if (err < 0) { 2599 dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err); 2600 goto free_irq; 2601 } 2602 2603 return 0; 2604 2605 free_irq: 2606 devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev); 2607 free_mc_irq: 2608 fsl_mc_free_irqs(ls_dev); 2609 2610 return err; 2611 } 2612 2613 static void add_ch_napi(struct dpaa2_eth_priv *priv) 2614 { 2615 int i; 2616 struct dpaa2_eth_channel *ch; 2617 2618 for (i = 0; i < priv->num_channels; i++) { 2619 ch = priv->channel[i]; 2620 /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */ 2621 netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll, 2622 NAPI_POLL_WEIGHT); 2623 } 2624 } 2625 2626 static void del_ch_napi(struct dpaa2_eth_priv *priv) 2627 { 2628 int i; 2629 struct dpaa2_eth_channel *ch; 2630 2631 for (i = 0; i < priv->num_channels; i++) { 2632 ch = priv->channel[i]; 2633 netif_napi_del(&ch->napi); 2634 } 2635 } 2636 2637 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev) 2638 { 2639 struct device *dev; 2640 struct net_device *net_dev = NULL; 2641 struct dpaa2_eth_priv *priv = NULL; 2642 int err = 0; 2643 2644 dev = &dpni_dev->dev; 2645 2646 /* Net device */ 2647 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_TX_QUEUES); 2648 if (!net_dev) { 2649 dev_err(dev, "alloc_etherdev_mq() failed\n"); 2650 return -ENOMEM; 2651 } 2652 2653 SET_NETDEV_DEV(net_dev, dev); 2654 dev_set_drvdata(dev, net_dev); 2655 2656 priv = netdev_priv(net_dev); 2657 priv->net_dev = net_dev; 2658 2659 priv->iommu_domain = iommu_get_domain_for_dev(dev); 2660 2661 /* Obtain a MC portal */ 2662 err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, 2663 &priv->mc_io); 2664 if (err) { 2665 if (err == -ENXIO) 2666 err = -EPROBE_DEFER; 2667 else 2668 dev_err(dev, "MC portal allocation failed\n"); 2669 goto err_portal_alloc; 2670 } 2671 2672 /* MC objects initialization and configuration */ 2673 err = setup_dpni(dpni_dev); 2674 if (err) 2675 goto err_dpni_setup; 2676 2677 err = setup_dpio(priv); 2678 if (err) 2679 goto err_dpio_setup; 2680 2681 setup_fqs(priv); 2682 2683 err = setup_dpbp(priv); 2684 if (err) 2685 goto err_dpbp_setup; 2686 2687 err = bind_dpni(priv); 2688 if (err) 2689 goto err_bind; 2690 2691 /* Add a NAPI context for each channel */ 2692 add_ch_napi(priv); 2693 2694 /* Percpu statistics */ 2695 priv->percpu_stats = alloc_percpu(*priv->percpu_stats); 2696 if (!priv->percpu_stats) { 2697 dev_err(dev, "alloc_percpu(percpu_stats) failed\n"); 2698 err = -ENOMEM; 2699 goto err_alloc_percpu_stats; 2700 } 2701 priv->percpu_extras = alloc_percpu(*priv->percpu_extras); 2702 if (!priv->percpu_extras) { 2703 dev_err(dev, "alloc_percpu(percpu_extras) failed\n"); 2704 err = -ENOMEM; 2705 goto err_alloc_percpu_extras; 2706 } 2707 2708 err = netdev_init(net_dev); 2709 if (err) 2710 goto err_netdev_init; 2711 2712 /* Configure checksum offload based on current interface flags */ 2713 err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM)); 2714 if (err) 2715 goto err_csum; 2716 2717 err = set_tx_csum(priv, !!(net_dev->features & 2718 (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))); 2719 if (err) 2720 goto err_csum; 2721 2722 err = alloc_rings(priv); 2723 if (err) 2724 goto err_alloc_rings; 2725 2726 err = setup_irqs(dpni_dev); 2727 if (err) { 2728 netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n"); 2729 priv->poll_thread = kthread_run(poll_link_state, priv, 2730 "%s_poll_link", net_dev->name); 2731 if (IS_ERR(priv->poll_thread)) { 2732 dev_err(dev, "Error starting polling thread\n"); 2733 goto err_poll_thread; 2734 } 2735 priv->do_link_poll = true; 2736 } 2737 2738 err = register_netdev(net_dev); 2739 if (err < 0) { 2740 dev_err(dev, "register_netdev() failed\n"); 2741 goto err_netdev_reg; 2742 } 2743 2744 dev_info(dev, "Probed interface %s\n", net_dev->name); 2745 return 0; 2746 2747 err_netdev_reg: 2748 if (priv->do_link_poll) 2749 kthread_stop(priv->poll_thread); 2750 else 2751 fsl_mc_free_irqs(dpni_dev); 2752 err_poll_thread: 2753 free_rings(priv); 2754 err_alloc_rings: 2755 err_csum: 2756 err_netdev_init: 2757 free_percpu(priv->percpu_extras); 2758 err_alloc_percpu_extras: 2759 free_percpu(priv->percpu_stats); 2760 err_alloc_percpu_stats: 2761 del_ch_napi(priv); 2762 err_bind: 2763 free_dpbp(priv); 2764 err_dpbp_setup: 2765 free_dpio(priv); 2766 err_dpio_setup: 2767 free_dpni(priv); 2768 err_dpni_setup: 2769 fsl_mc_portal_free(priv->mc_io); 2770 err_portal_alloc: 2771 dev_set_drvdata(dev, NULL); 2772 free_netdev(net_dev); 2773 2774 return err; 2775 } 2776 2777 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev) 2778 { 2779 struct device *dev; 2780 struct net_device *net_dev; 2781 struct dpaa2_eth_priv *priv; 2782 2783 dev = &ls_dev->dev; 2784 net_dev = dev_get_drvdata(dev); 2785 priv = netdev_priv(net_dev); 2786 2787 unregister_netdev(net_dev); 2788 2789 if (priv->do_link_poll) 2790 kthread_stop(priv->poll_thread); 2791 else 2792 fsl_mc_free_irqs(ls_dev); 2793 2794 free_rings(priv); 2795 free_percpu(priv->percpu_stats); 2796 free_percpu(priv->percpu_extras); 2797 2798 del_ch_napi(priv); 2799 free_dpbp(priv); 2800 free_dpio(priv); 2801 free_dpni(priv); 2802 2803 fsl_mc_portal_free(priv->mc_io); 2804 2805 free_netdev(net_dev); 2806 2807 dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name); 2808 2809 return 0; 2810 } 2811 2812 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = { 2813 { 2814 .vendor = FSL_MC_VENDOR_FREESCALE, 2815 .obj_type = "dpni", 2816 }, 2817 { .vendor = 0x0 } 2818 }; 2819 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table); 2820 2821 static struct fsl_mc_driver dpaa2_eth_driver = { 2822 .driver = { 2823 .name = KBUILD_MODNAME, 2824 .owner = THIS_MODULE, 2825 }, 2826 .probe = dpaa2_eth_probe, 2827 .remove = dpaa2_eth_remove, 2828 .match_id_table = dpaa2_eth_match_id_table 2829 }; 2830 2831 module_fsl_mc_driver(dpaa2_eth_driver); 2832