1 // SPDX-License-Identifier: GPL-2.0+ 2 3 #include "lan966x_main.h" 4 5 static int lan966x_fdma_channel_active(struct lan966x *lan966x) 6 { 7 return lan_rd(lan966x, FDMA_CH_ACTIVE); 8 } 9 10 static struct page *lan966x_fdma_rx_alloc_page(struct lan966x_rx *rx, 11 struct lan966x_db *db) 12 { 13 struct lan966x *lan966x = rx->lan966x; 14 dma_addr_t dma_addr; 15 struct page *page; 16 17 page = dev_alloc_pages(rx->page_order); 18 if (unlikely(!page)) 19 return NULL; 20 21 dma_addr = dma_map_page(lan966x->dev, page, 0, 22 PAGE_SIZE << rx->page_order, 23 DMA_FROM_DEVICE); 24 if (unlikely(dma_mapping_error(lan966x->dev, dma_addr))) 25 goto free_page; 26 27 db->dataptr = dma_addr; 28 29 return page; 30 31 free_page: 32 __free_pages(page, rx->page_order); 33 return NULL; 34 } 35 36 static void lan966x_fdma_rx_free_pages(struct lan966x_rx *rx) 37 { 38 struct lan966x *lan966x = rx->lan966x; 39 struct lan966x_rx_dcb *dcb; 40 struct lan966x_db *db; 41 int i, j; 42 43 for (i = 0; i < FDMA_DCB_MAX; ++i) { 44 dcb = &rx->dcbs[i]; 45 46 for (j = 0; j < FDMA_RX_DCB_MAX_DBS; ++j) { 47 db = &dcb->db[j]; 48 dma_unmap_single(lan966x->dev, 49 (dma_addr_t)db->dataptr, 50 PAGE_SIZE << rx->page_order, 51 DMA_FROM_DEVICE); 52 __free_pages(rx->page[i][j], rx->page_order); 53 } 54 } 55 } 56 57 static void lan966x_fdma_rx_add_dcb(struct lan966x_rx *rx, 58 struct lan966x_rx_dcb *dcb, 59 u64 nextptr) 60 { 61 struct lan966x_db *db; 62 int i; 63 64 for (i = 0; i < FDMA_RX_DCB_MAX_DBS; ++i) { 65 db = &dcb->db[i]; 66 db->status = FDMA_DCB_STATUS_INTR; 67 } 68 69 dcb->nextptr = FDMA_DCB_INVALID_DATA; 70 dcb->info = FDMA_DCB_INFO_DATAL(PAGE_SIZE << rx->page_order); 71 72 rx->last_entry->nextptr = nextptr; 73 rx->last_entry = dcb; 74 } 75 76 static int lan966x_fdma_rx_alloc(struct lan966x_rx *rx) 77 { 78 struct lan966x *lan966x = rx->lan966x; 79 struct lan966x_rx_dcb *dcb; 80 struct lan966x_db *db; 81 struct page *page; 82 int i, j; 83 int size; 84 85 /* calculate how many pages are needed to allocate the dcbs */ 86 size = sizeof(struct lan966x_rx_dcb) * FDMA_DCB_MAX; 87 size = ALIGN(size, PAGE_SIZE); 88 89 rx->dcbs = dma_alloc_coherent(lan966x->dev, size, &rx->dma, GFP_KERNEL); 90 if (!rx->dcbs) 91 return -ENOMEM; 92 93 rx->last_entry = rx->dcbs; 94 rx->db_index = 0; 95 rx->dcb_index = 0; 96 97 /* Now for each dcb allocate the dbs */ 98 for (i = 0; i < FDMA_DCB_MAX; ++i) { 99 dcb = &rx->dcbs[i]; 100 dcb->info = 0; 101 102 /* For each db allocate a page and map it to the DB dataptr. */ 103 for (j = 0; j < FDMA_RX_DCB_MAX_DBS; ++j) { 104 db = &dcb->db[j]; 105 page = lan966x_fdma_rx_alloc_page(rx, db); 106 if (!page) 107 return -ENOMEM; 108 109 db->status = 0; 110 rx->page[i][j] = page; 111 } 112 113 lan966x_fdma_rx_add_dcb(rx, dcb, rx->dma + sizeof(*dcb) * i); 114 } 115 116 return 0; 117 } 118 119 static void lan966x_fdma_rx_free(struct lan966x_rx *rx) 120 { 121 struct lan966x *lan966x = rx->lan966x; 122 u32 size; 123 124 /* Now it is possible to do the cleanup of dcb */ 125 size = sizeof(struct lan966x_tx_dcb) * FDMA_DCB_MAX; 126 size = ALIGN(size, PAGE_SIZE); 127 dma_free_coherent(lan966x->dev, size, rx->dcbs, rx->dma); 128 } 129 130 static void lan966x_fdma_rx_start(struct lan966x_rx *rx) 131 { 132 struct lan966x *lan966x = rx->lan966x; 133 u32 mask; 134 135 /* When activating a channel, first is required to write the first DCB 136 * address and then to activate it 137 */ 138 lan_wr(lower_32_bits((u64)rx->dma), lan966x, 139 FDMA_DCB_LLP(rx->channel_id)); 140 lan_wr(upper_32_bits((u64)rx->dma), lan966x, 141 FDMA_DCB_LLP1(rx->channel_id)); 142 143 lan_wr(FDMA_CH_CFG_CH_DCB_DB_CNT_SET(FDMA_RX_DCB_MAX_DBS) | 144 FDMA_CH_CFG_CH_INTR_DB_EOF_ONLY_SET(1) | 145 FDMA_CH_CFG_CH_INJ_PORT_SET(0) | 146 FDMA_CH_CFG_CH_MEM_SET(1), 147 lan966x, FDMA_CH_CFG(rx->channel_id)); 148 149 /* Start fdma */ 150 lan_rmw(FDMA_PORT_CTRL_XTR_STOP_SET(0), 151 FDMA_PORT_CTRL_XTR_STOP, 152 lan966x, FDMA_PORT_CTRL(0)); 153 154 /* Enable interrupts */ 155 mask = lan_rd(lan966x, FDMA_INTR_DB_ENA); 156 mask = FDMA_INTR_DB_ENA_INTR_DB_ENA_GET(mask); 157 mask |= BIT(rx->channel_id); 158 lan_rmw(FDMA_INTR_DB_ENA_INTR_DB_ENA_SET(mask), 159 FDMA_INTR_DB_ENA_INTR_DB_ENA, 160 lan966x, FDMA_INTR_DB_ENA); 161 162 /* Activate the channel */ 163 lan_rmw(FDMA_CH_ACTIVATE_CH_ACTIVATE_SET(BIT(rx->channel_id)), 164 FDMA_CH_ACTIVATE_CH_ACTIVATE, 165 lan966x, FDMA_CH_ACTIVATE); 166 } 167 168 static void lan966x_fdma_rx_disable(struct lan966x_rx *rx) 169 { 170 struct lan966x *lan966x = rx->lan966x; 171 u32 val; 172 173 /* Disable the channel */ 174 lan_rmw(FDMA_CH_DISABLE_CH_DISABLE_SET(BIT(rx->channel_id)), 175 FDMA_CH_DISABLE_CH_DISABLE, 176 lan966x, FDMA_CH_DISABLE); 177 178 readx_poll_timeout_atomic(lan966x_fdma_channel_active, lan966x, 179 val, !(val & BIT(rx->channel_id)), 180 READL_SLEEP_US, READL_TIMEOUT_US); 181 182 lan_rmw(FDMA_CH_DB_DISCARD_DB_DISCARD_SET(BIT(rx->channel_id)), 183 FDMA_CH_DB_DISCARD_DB_DISCARD, 184 lan966x, FDMA_CH_DB_DISCARD); 185 } 186 187 static void lan966x_fdma_rx_reload(struct lan966x_rx *rx) 188 { 189 struct lan966x *lan966x = rx->lan966x; 190 191 lan_rmw(FDMA_CH_RELOAD_CH_RELOAD_SET(BIT(rx->channel_id)), 192 FDMA_CH_RELOAD_CH_RELOAD, 193 lan966x, FDMA_CH_RELOAD); 194 } 195 196 static void lan966x_fdma_tx_add_dcb(struct lan966x_tx *tx, 197 struct lan966x_tx_dcb *dcb) 198 { 199 dcb->nextptr = FDMA_DCB_INVALID_DATA; 200 dcb->info = 0; 201 } 202 203 static int lan966x_fdma_tx_alloc(struct lan966x_tx *tx) 204 { 205 struct lan966x *lan966x = tx->lan966x; 206 struct lan966x_tx_dcb *dcb; 207 struct lan966x_db *db; 208 int size; 209 int i, j; 210 211 tx->dcbs_buf = kcalloc(FDMA_DCB_MAX, sizeof(struct lan966x_tx_dcb_buf), 212 GFP_KERNEL); 213 if (!tx->dcbs_buf) 214 return -ENOMEM; 215 216 /* calculate how many pages are needed to allocate the dcbs */ 217 size = sizeof(struct lan966x_tx_dcb) * FDMA_DCB_MAX; 218 size = ALIGN(size, PAGE_SIZE); 219 tx->dcbs = dma_alloc_coherent(lan966x->dev, size, &tx->dma, GFP_KERNEL); 220 if (!tx->dcbs) 221 goto out; 222 223 /* Now for each dcb allocate the db */ 224 for (i = 0; i < FDMA_DCB_MAX; ++i) { 225 dcb = &tx->dcbs[i]; 226 227 for (j = 0; j < FDMA_TX_DCB_MAX_DBS; ++j) { 228 db = &dcb->db[j]; 229 db->dataptr = 0; 230 db->status = 0; 231 } 232 233 lan966x_fdma_tx_add_dcb(tx, dcb); 234 } 235 236 return 0; 237 238 out: 239 kfree(tx->dcbs_buf); 240 return -ENOMEM; 241 } 242 243 static void lan966x_fdma_tx_free(struct lan966x_tx *tx) 244 { 245 struct lan966x *lan966x = tx->lan966x; 246 int size; 247 248 kfree(tx->dcbs_buf); 249 250 size = sizeof(struct lan966x_tx_dcb) * FDMA_DCB_MAX; 251 size = ALIGN(size, PAGE_SIZE); 252 dma_free_coherent(lan966x->dev, size, tx->dcbs, tx->dma); 253 } 254 255 static void lan966x_fdma_tx_activate(struct lan966x_tx *tx) 256 { 257 struct lan966x *lan966x = tx->lan966x; 258 u32 mask; 259 260 /* When activating a channel, first is required to write the first DCB 261 * address and then to activate it 262 */ 263 lan_wr(lower_32_bits((u64)tx->dma), lan966x, 264 FDMA_DCB_LLP(tx->channel_id)); 265 lan_wr(upper_32_bits((u64)tx->dma), lan966x, 266 FDMA_DCB_LLP1(tx->channel_id)); 267 268 lan_wr(FDMA_CH_CFG_CH_DCB_DB_CNT_SET(FDMA_TX_DCB_MAX_DBS) | 269 FDMA_CH_CFG_CH_INTR_DB_EOF_ONLY_SET(1) | 270 FDMA_CH_CFG_CH_INJ_PORT_SET(0) | 271 FDMA_CH_CFG_CH_MEM_SET(1), 272 lan966x, FDMA_CH_CFG(tx->channel_id)); 273 274 /* Start fdma */ 275 lan_rmw(FDMA_PORT_CTRL_INJ_STOP_SET(0), 276 FDMA_PORT_CTRL_INJ_STOP, 277 lan966x, FDMA_PORT_CTRL(0)); 278 279 /* Enable interrupts */ 280 mask = lan_rd(lan966x, FDMA_INTR_DB_ENA); 281 mask = FDMA_INTR_DB_ENA_INTR_DB_ENA_GET(mask); 282 mask |= BIT(tx->channel_id); 283 lan_rmw(FDMA_INTR_DB_ENA_INTR_DB_ENA_SET(mask), 284 FDMA_INTR_DB_ENA_INTR_DB_ENA, 285 lan966x, FDMA_INTR_DB_ENA); 286 287 /* Activate the channel */ 288 lan_rmw(FDMA_CH_ACTIVATE_CH_ACTIVATE_SET(BIT(tx->channel_id)), 289 FDMA_CH_ACTIVATE_CH_ACTIVATE, 290 lan966x, FDMA_CH_ACTIVATE); 291 } 292 293 static void lan966x_fdma_tx_disable(struct lan966x_tx *tx) 294 { 295 struct lan966x *lan966x = tx->lan966x; 296 u32 val; 297 298 /* Disable the channel */ 299 lan_rmw(FDMA_CH_DISABLE_CH_DISABLE_SET(BIT(tx->channel_id)), 300 FDMA_CH_DISABLE_CH_DISABLE, 301 lan966x, FDMA_CH_DISABLE); 302 303 readx_poll_timeout_atomic(lan966x_fdma_channel_active, lan966x, 304 val, !(val & BIT(tx->channel_id)), 305 READL_SLEEP_US, READL_TIMEOUT_US); 306 307 lan_rmw(FDMA_CH_DB_DISCARD_DB_DISCARD_SET(BIT(tx->channel_id)), 308 FDMA_CH_DB_DISCARD_DB_DISCARD, 309 lan966x, FDMA_CH_DB_DISCARD); 310 311 tx->activated = false; 312 tx->last_in_use = -1; 313 } 314 315 static void lan966x_fdma_tx_reload(struct lan966x_tx *tx) 316 { 317 struct lan966x *lan966x = tx->lan966x; 318 319 /* Write the registers to reload the channel */ 320 lan_rmw(FDMA_CH_RELOAD_CH_RELOAD_SET(BIT(tx->channel_id)), 321 FDMA_CH_RELOAD_CH_RELOAD, 322 lan966x, FDMA_CH_RELOAD); 323 } 324 325 static void lan966x_fdma_wakeup_netdev(struct lan966x *lan966x) 326 { 327 struct lan966x_port *port; 328 int i; 329 330 for (i = 0; i < lan966x->num_phys_ports; ++i) { 331 port = lan966x->ports[i]; 332 if (!port) 333 continue; 334 335 if (netif_queue_stopped(port->dev)) 336 netif_wake_queue(port->dev); 337 } 338 } 339 340 static void lan966x_fdma_stop_netdev(struct lan966x *lan966x) 341 { 342 struct lan966x_port *port; 343 int i; 344 345 for (i = 0; i < lan966x->num_phys_ports; ++i) { 346 port = lan966x->ports[i]; 347 if (!port) 348 continue; 349 350 netif_stop_queue(port->dev); 351 } 352 } 353 354 static void lan966x_fdma_tx_clear_buf(struct lan966x *lan966x, int weight) 355 { 356 struct lan966x_tx *tx = &lan966x->tx; 357 struct lan966x_tx_dcb_buf *dcb_buf; 358 struct lan966x_db *db; 359 unsigned long flags; 360 bool clear = false; 361 int i; 362 363 spin_lock_irqsave(&lan966x->tx_lock, flags); 364 for (i = 0; i < FDMA_DCB_MAX; ++i) { 365 dcb_buf = &tx->dcbs_buf[i]; 366 367 if (!dcb_buf->used) 368 continue; 369 370 db = &tx->dcbs[i].db[0]; 371 if (!(db->status & FDMA_DCB_STATUS_DONE)) 372 continue; 373 374 dcb_buf->dev->stats.tx_packets++; 375 dcb_buf->dev->stats.tx_bytes += dcb_buf->skb->len; 376 377 dcb_buf->used = false; 378 dma_unmap_single(lan966x->dev, 379 dcb_buf->dma_addr, 380 dcb_buf->skb->len, 381 DMA_TO_DEVICE); 382 if (!dcb_buf->ptp) 383 dev_kfree_skb_any(dcb_buf->skb); 384 385 clear = true; 386 } 387 388 if (clear) 389 lan966x_fdma_wakeup_netdev(lan966x); 390 391 spin_unlock_irqrestore(&lan966x->tx_lock, flags); 392 } 393 394 static bool lan966x_fdma_rx_more_frames(struct lan966x_rx *rx) 395 { 396 struct lan966x_db *db; 397 398 /* Check if there is any data */ 399 db = &rx->dcbs[rx->dcb_index].db[rx->db_index]; 400 if (unlikely(!(db->status & FDMA_DCB_STATUS_DONE))) 401 return false; 402 403 return true; 404 } 405 406 static struct sk_buff *lan966x_fdma_rx_get_frame(struct lan966x_rx *rx) 407 { 408 struct lan966x *lan966x = rx->lan966x; 409 u64 src_port, timestamp; 410 struct lan966x_db *db; 411 struct sk_buff *skb; 412 struct page *page; 413 414 /* Get the received frame and unmap it */ 415 db = &rx->dcbs[rx->dcb_index].db[rx->db_index]; 416 page = rx->page[rx->dcb_index][rx->db_index]; 417 skb = build_skb(page_address(page), PAGE_SIZE << rx->page_order); 418 if (unlikely(!skb)) 419 goto unmap_page; 420 421 dma_unmap_single(lan966x->dev, (dma_addr_t)db->dataptr, 422 FDMA_DCB_STATUS_BLOCKL(db->status), 423 DMA_FROM_DEVICE); 424 skb_put(skb, FDMA_DCB_STATUS_BLOCKL(db->status)); 425 426 lan966x_ifh_get_src_port(skb->data, &src_port); 427 lan966x_ifh_get_timestamp(skb->data, ×tamp); 428 429 if (WARN_ON(src_port >= lan966x->num_phys_ports)) 430 goto free_skb; 431 432 skb->dev = lan966x->ports[src_port]->dev; 433 skb_pull(skb, IFH_LEN * sizeof(u32)); 434 435 if (likely(!(skb->dev->features & NETIF_F_RXFCS))) 436 skb_trim(skb, skb->len - ETH_FCS_LEN); 437 438 lan966x_ptp_rxtstamp(lan966x, skb, timestamp); 439 skb->protocol = eth_type_trans(skb, skb->dev); 440 441 if (lan966x->bridge_mask & BIT(src_port)) { 442 skb->offload_fwd_mark = 1; 443 444 skb_reset_network_header(skb); 445 if (!lan966x_hw_offload(lan966x, src_port, skb)) 446 skb->offload_fwd_mark = 0; 447 } 448 449 skb->dev->stats.rx_bytes += skb->len; 450 skb->dev->stats.rx_packets++; 451 452 return skb; 453 454 free_skb: 455 kfree_skb(skb); 456 unmap_page: 457 dma_unmap_page(lan966x->dev, (dma_addr_t)db->dataptr, 458 FDMA_DCB_STATUS_BLOCKL(db->status), 459 DMA_FROM_DEVICE); 460 __free_pages(page, rx->page_order); 461 462 return NULL; 463 } 464 465 static int lan966x_fdma_napi_poll(struct napi_struct *napi, int weight) 466 { 467 struct lan966x *lan966x = container_of(napi, struct lan966x, napi); 468 struct lan966x_rx *rx = &lan966x->rx; 469 int dcb_reload = rx->dcb_index; 470 struct lan966x_rx_dcb *old_dcb; 471 struct lan966x_db *db; 472 struct sk_buff *skb; 473 struct page *page; 474 int counter = 0; 475 u64 nextptr; 476 477 lan966x_fdma_tx_clear_buf(lan966x, weight); 478 479 /* Get all received skb */ 480 while (counter < weight) { 481 if (!lan966x_fdma_rx_more_frames(rx)) 482 break; 483 484 skb = lan966x_fdma_rx_get_frame(rx); 485 486 rx->page[rx->dcb_index][rx->db_index] = NULL; 487 rx->dcb_index++; 488 rx->dcb_index &= FDMA_DCB_MAX - 1; 489 490 if (!skb) 491 break; 492 493 napi_gro_receive(&lan966x->napi, skb); 494 counter++; 495 } 496 497 /* Allocate new pages and map them */ 498 while (dcb_reload != rx->dcb_index) { 499 db = &rx->dcbs[dcb_reload].db[rx->db_index]; 500 page = lan966x_fdma_rx_alloc_page(rx, db); 501 if (unlikely(!page)) 502 break; 503 rx->page[dcb_reload][rx->db_index] = page; 504 505 old_dcb = &rx->dcbs[dcb_reload]; 506 dcb_reload++; 507 dcb_reload &= FDMA_DCB_MAX - 1; 508 509 nextptr = rx->dma + ((unsigned long)old_dcb - 510 (unsigned long)rx->dcbs); 511 lan966x_fdma_rx_add_dcb(rx, old_dcb, nextptr); 512 lan966x_fdma_rx_reload(rx); 513 } 514 515 if (counter < weight && napi_complete_done(napi, counter)) 516 lan_wr(0xff, lan966x, FDMA_INTR_DB_ENA); 517 518 return counter; 519 } 520 521 irqreturn_t lan966x_fdma_irq_handler(int irq, void *args) 522 { 523 struct lan966x *lan966x = args; 524 u32 db, err, err_type; 525 526 db = lan_rd(lan966x, FDMA_INTR_DB); 527 err = lan_rd(lan966x, FDMA_INTR_ERR); 528 529 if (db) { 530 lan_wr(0, lan966x, FDMA_INTR_DB_ENA); 531 lan_wr(db, lan966x, FDMA_INTR_DB); 532 533 napi_schedule(&lan966x->napi); 534 } 535 536 if (err) { 537 err_type = lan_rd(lan966x, FDMA_ERRORS); 538 539 WARN(1, "Unexpected error: %d, error_type: %d\n", err, err_type); 540 541 lan_wr(err, lan966x, FDMA_INTR_ERR); 542 lan_wr(err_type, lan966x, FDMA_ERRORS); 543 } 544 545 return IRQ_HANDLED; 546 } 547 548 static int lan966x_fdma_get_next_dcb(struct lan966x_tx *tx) 549 { 550 struct lan966x_tx_dcb_buf *dcb_buf; 551 int i; 552 553 for (i = 0; i < FDMA_DCB_MAX; ++i) { 554 dcb_buf = &tx->dcbs_buf[i]; 555 if (!dcb_buf->used && i != tx->last_in_use) 556 return i; 557 } 558 559 return -1; 560 } 561 562 int lan966x_fdma_xmit(struct sk_buff *skb, __be32 *ifh, struct net_device *dev) 563 { 564 struct lan966x_port *port = netdev_priv(dev); 565 struct lan966x *lan966x = port->lan966x; 566 struct lan966x_tx_dcb_buf *next_dcb_buf; 567 struct lan966x_tx_dcb *next_dcb, *dcb; 568 struct lan966x_tx *tx = &lan966x->tx; 569 struct lan966x_db *next_db; 570 int needed_headroom; 571 int needed_tailroom; 572 dma_addr_t dma_addr; 573 int next_to_use; 574 int err; 575 576 /* Get next index */ 577 next_to_use = lan966x_fdma_get_next_dcb(tx); 578 if (next_to_use < 0) { 579 netif_stop_queue(dev); 580 return NETDEV_TX_BUSY; 581 } 582 583 if (skb_put_padto(skb, ETH_ZLEN)) { 584 dev->stats.tx_dropped++; 585 return NETDEV_TX_OK; 586 } 587 588 /* skb processing */ 589 needed_headroom = max_t(int, IFH_LEN * sizeof(u32) - skb_headroom(skb), 0); 590 needed_tailroom = max_t(int, ETH_FCS_LEN - skb_tailroom(skb), 0); 591 if (needed_headroom || needed_tailroom || skb_header_cloned(skb)) { 592 err = pskb_expand_head(skb, needed_headroom, needed_tailroom, 593 GFP_ATOMIC); 594 if (unlikely(err)) { 595 dev->stats.tx_dropped++; 596 err = NETDEV_TX_OK; 597 goto release; 598 } 599 } 600 601 skb_tx_timestamp(skb); 602 skb_push(skb, IFH_LEN * sizeof(u32)); 603 memcpy(skb->data, ifh, IFH_LEN * sizeof(u32)); 604 skb_put(skb, 4); 605 606 dma_addr = dma_map_single(lan966x->dev, skb->data, skb->len, 607 DMA_TO_DEVICE); 608 if (dma_mapping_error(lan966x->dev, dma_addr)) { 609 dev->stats.tx_dropped++; 610 err = NETDEV_TX_OK; 611 goto release; 612 } 613 614 /* Setup next dcb */ 615 next_dcb = &tx->dcbs[next_to_use]; 616 next_dcb->nextptr = FDMA_DCB_INVALID_DATA; 617 618 next_db = &next_dcb->db[0]; 619 next_db->dataptr = dma_addr; 620 next_db->status = FDMA_DCB_STATUS_SOF | 621 FDMA_DCB_STATUS_EOF | 622 FDMA_DCB_STATUS_INTR | 623 FDMA_DCB_STATUS_BLOCKO(0) | 624 FDMA_DCB_STATUS_BLOCKL(skb->len); 625 626 /* Fill up the buffer */ 627 next_dcb_buf = &tx->dcbs_buf[next_to_use]; 628 next_dcb_buf->skb = skb; 629 next_dcb_buf->dma_addr = dma_addr; 630 next_dcb_buf->used = true; 631 next_dcb_buf->ptp = false; 632 next_dcb_buf->dev = dev; 633 634 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 635 LAN966X_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP) 636 next_dcb_buf->ptp = true; 637 638 if (likely(lan966x->tx.activated)) { 639 /* Connect current dcb to the next db */ 640 dcb = &tx->dcbs[tx->last_in_use]; 641 dcb->nextptr = tx->dma + (next_to_use * 642 sizeof(struct lan966x_tx_dcb)); 643 644 lan966x_fdma_tx_reload(tx); 645 } else { 646 /* Because it is first time, then just activate */ 647 lan966x->tx.activated = true; 648 lan966x_fdma_tx_activate(tx); 649 } 650 651 /* Move to next dcb because this last in use */ 652 tx->last_in_use = next_to_use; 653 654 return NETDEV_TX_OK; 655 656 release: 657 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 658 LAN966X_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP) 659 lan966x_ptp_txtstamp_release(port, skb); 660 661 dev_kfree_skb_any(skb); 662 return err; 663 } 664 665 static int lan966x_fdma_get_max_mtu(struct lan966x *lan966x) 666 { 667 int max_mtu = 0; 668 int i; 669 670 for (i = 0; i < lan966x->num_phys_ports; ++i) { 671 int mtu; 672 673 if (!lan966x->ports[i]) 674 continue; 675 676 mtu = lan966x->ports[i]->dev->mtu; 677 if (mtu > max_mtu) 678 max_mtu = mtu; 679 } 680 681 return max_mtu; 682 } 683 684 static int lan966x_qsys_sw_status(struct lan966x *lan966x) 685 { 686 return lan_rd(lan966x, QSYS_SW_STATUS(CPU_PORT)); 687 } 688 689 static int lan966x_fdma_reload(struct lan966x *lan966x, int new_mtu) 690 { 691 dma_addr_t rx_dma; 692 void *rx_dcbs; 693 u32 size; 694 int err; 695 696 /* Store these for later to free them */ 697 rx_dma = lan966x->rx.dma; 698 rx_dcbs = lan966x->rx.dcbs; 699 700 napi_synchronize(&lan966x->napi); 701 napi_disable(&lan966x->napi); 702 lan966x_fdma_stop_netdev(lan966x); 703 704 lan966x_fdma_rx_disable(&lan966x->rx); 705 lan966x_fdma_rx_free_pages(&lan966x->rx); 706 lan966x->rx.page_order = round_up(new_mtu, PAGE_SIZE) / PAGE_SIZE - 1; 707 err = lan966x_fdma_rx_alloc(&lan966x->rx); 708 if (err) 709 goto restore; 710 lan966x_fdma_rx_start(&lan966x->rx); 711 712 size = sizeof(struct lan966x_rx_dcb) * FDMA_DCB_MAX; 713 size = ALIGN(size, PAGE_SIZE); 714 dma_free_coherent(lan966x->dev, size, rx_dcbs, rx_dma); 715 716 lan966x_fdma_wakeup_netdev(lan966x); 717 napi_enable(&lan966x->napi); 718 719 return err; 720 restore: 721 lan966x->rx.dma = rx_dma; 722 lan966x->rx.dcbs = rx_dcbs; 723 lan966x_fdma_rx_start(&lan966x->rx); 724 725 return err; 726 } 727 728 int lan966x_fdma_change_mtu(struct lan966x *lan966x) 729 { 730 int max_mtu; 731 int err; 732 u32 val; 733 734 max_mtu = lan966x_fdma_get_max_mtu(lan966x); 735 max_mtu += IFH_LEN * sizeof(u32); 736 737 if (round_up(max_mtu, PAGE_SIZE) / PAGE_SIZE - 1 == 738 lan966x->rx.page_order) 739 return 0; 740 741 /* Disable the CPU port */ 742 lan_rmw(QSYS_SW_PORT_MODE_PORT_ENA_SET(0), 743 QSYS_SW_PORT_MODE_PORT_ENA, 744 lan966x, QSYS_SW_PORT_MODE(CPU_PORT)); 745 746 /* Flush the CPU queues */ 747 readx_poll_timeout(lan966x_qsys_sw_status, lan966x, 748 val, !(QSYS_SW_STATUS_EQ_AVAIL_GET(val)), 749 READL_SLEEP_US, READL_TIMEOUT_US); 750 751 /* Add a sleep in case there are frames between the queues and the CPU 752 * port 753 */ 754 usleep_range(1000, 2000); 755 756 err = lan966x_fdma_reload(lan966x, max_mtu); 757 758 /* Enable back the CPU port */ 759 lan_rmw(QSYS_SW_PORT_MODE_PORT_ENA_SET(1), 760 QSYS_SW_PORT_MODE_PORT_ENA, 761 lan966x, QSYS_SW_PORT_MODE(CPU_PORT)); 762 763 return err; 764 } 765 766 void lan966x_fdma_netdev_init(struct lan966x *lan966x, struct net_device *dev) 767 { 768 if (lan966x->fdma_ndev) 769 return; 770 771 lan966x->fdma_ndev = dev; 772 netif_napi_add(dev, &lan966x->napi, lan966x_fdma_napi_poll); 773 napi_enable(&lan966x->napi); 774 } 775 776 void lan966x_fdma_netdev_deinit(struct lan966x *lan966x, struct net_device *dev) 777 { 778 if (lan966x->fdma_ndev == dev) { 779 netif_napi_del(&lan966x->napi); 780 lan966x->fdma_ndev = NULL; 781 } 782 } 783 784 int lan966x_fdma_init(struct lan966x *lan966x) 785 { 786 int err; 787 788 if (!lan966x->fdma) 789 return 0; 790 791 lan966x->rx.lan966x = lan966x; 792 lan966x->rx.channel_id = FDMA_XTR_CHANNEL; 793 lan966x->tx.lan966x = lan966x; 794 lan966x->tx.channel_id = FDMA_INJ_CHANNEL; 795 lan966x->tx.last_in_use = -1; 796 797 err = lan966x_fdma_rx_alloc(&lan966x->rx); 798 if (err) 799 return err; 800 801 err = lan966x_fdma_tx_alloc(&lan966x->tx); 802 if (err) { 803 lan966x_fdma_rx_free(&lan966x->rx); 804 return err; 805 } 806 807 lan966x_fdma_rx_start(&lan966x->rx); 808 809 return 0; 810 } 811 812 void lan966x_fdma_deinit(struct lan966x *lan966x) 813 { 814 if (!lan966x->fdma) 815 return; 816 817 lan966x_fdma_rx_disable(&lan966x->rx); 818 lan966x_fdma_tx_disable(&lan966x->tx); 819 820 napi_synchronize(&lan966x->napi); 821 napi_disable(&lan966x->napi); 822 823 lan966x_fdma_rx_free_pages(&lan966x->rx); 824 lan966x_fdma_rx_free(&lan966x->rx); 825 lan966x_fdma_tx_free(&lan966x->tx); 826 } 827