1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) 2021 Gerhard Engleder <gerhard@engleder-embedded.com> */ 3 4 /* TSN endpoint Ethernet MAC driver 5 * 6 * The TSN endpoint Ethernet MAC is a FPGA based network device for real-time 7 * communication. It is designed for endpoints within TSN (Time Sensitive 8 * Networking) networks; e.g., for PLCs in the industrial automation case. 9 * 10 * It supports multiple TX/RX queue pairs. The first TX/RX queue pair is used 11 * by the driver. 12 * 13 * More information can be found here: 14 * - www.embedded-experts.at/tsn 15 * - www.engleder-embedded.com 16 */ 17 18 #include "tsnep.h" 19 #include "tsnep_hw.h" 20 21 #include <linux/module.h> 22 #include <linux/of.h> 23 #include <linux/of_net.h> 24 #include <linux/of_mdio.h> 25 #include <linux/interrupt.h> 26 #include <linux/etherdevice.h> 27 #include <linux/phy.h> 28 #include <linux/iopoll.h> 29 30 #define TSNEP_SKB_PAD (NET_SKB_PAD + NET_IP_ALIGN) 31 #define TSNEP_HEADROOM ALIGN(TSNEP_SKB_PAD, 4) 32 #define TSNEP_MAX_RX_BUF_SIZE (PAGE_SIZE - TSNEP_HEADROOM - \ 33 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) 34 35 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 36 #define DMA_ADDR_HIGH(dma_addr) ((u32)(((dma_addr) >> 32) & 0xFFFFFFFF)) 37 #else 38 #define DMA_ADDR_HIGH(dma_addr) ((u32)(0)) 39 #endif 40 #define DMA_ADDR_LOW(dma_addr) ((u32)((dma_addr) & 0xFFFFFFFF)) 41 42 static void tsnep_enable_irq(struct tsnep_adapter *adapter, u32 mask) 43 { 44 iowrite32(mask, adapter->addr + ECM_INT_ENABLE); 45 } 46 47 static void tsnep_disable_irq(struct tsnep_adapter *adapter, u32 mask) 48 { 49 mask |= ECM_INT_DISABLE; 50 iowrite32(mask, adapter->addr + ECM_INT_ENABLE); 51 } 52 53 static irqreturn_t tsnep_irq(int irq, void *arg) 54 { 55 struct tsnep_adapter *adapter = arg; 56 u32 active = ioread32(adapter->addr + ECM_INT_ACTIVE); 57 58 /* acknowledge interrupt */ 59 if (active != 0) 60 iowrite32(active, adapter->addr + ECM_INT_ACKNOWLEDGE); 61 62 /* handle link interrupt */ 63 if ((active & ECM_INT_LINK) != 0) 64 phy_mac_interrupt(adapter->netdev->phydev); 65 66 /* handle TX/RX queue 0 interrupt */ 67 if ((active & adapter->queue[0].irq_mask) != 0) { 68 tsnep_disable_irq(adapter, adapter->queue[0].irq_mask); 69 napi_schedule(&adapter->queue[0].napi); 70 } 71 72 return IRQ_HANDLED; 73 } 74 75 static irqreturn_t tsnep_irq_txrx(int irq, void *arg) 76 { 77 struct tsnep_queue *queue = arg; 78 79 /* handle TX/RX queue interrupt */ 80 tsnep_disable_irq(queue->adapter, queue->irq_mask); 81 napi_schedule(&queue->napi); 82 83 return IRQ_HANDLED; 84 } 85 86 static int tsnep_mdiobus_read(struct mii_bus *bus, int addr, int regnum) 87 { 88 struct tsnep_adapter *adapter = bus->priv; 89 u32 md; 90 int retval; 91 92 if (regnum & MII_ADDR_C45) 93 return -EOPNOTSUPP; 94 95 md = ECM_MD_READ; 96 if (!adapter->suppress_preamble) 97 md |= ECM_MD_PREAMBLE; 98 md |= (regnum << ECM_MD_ADDR_SHIFT) & ECM_MD_ADDR_MASK; 99 md |= (addr << ECM_MD_PHY_ADDR_SHIFT) & ECM_MD_PHY_ADDR_MASK; 100 iowrite32(md, adapter->addr + ECM_MD_CONTROL); 101 retval = readl_poll_timeout_atomic(adapter->addr + ECM_MD_STATUS, md, 102 !(md & ECM_MD_BUSY), 16, 1000); 103 if (retval != 0) 104 return retval; 105 106 return (md & ECM_MD_DATA_MASK) >> ECM_MD_DATA_SHIFT; 107 } 108 109 static int tsnep_mdiobus_write(struct mii_bus *bus, int addr, int regnum, 110 u16 val) 111 { 112 struct tsnep_adapter *adapter = bus->priv; 113 u32 md; 114 int retval; 115 116 if (regnum & MII_ADDR_C45) 117 return -EOPNOTSUPP; 118 119 md = ECM_MD_WRITE; 120 if (!adapter->suppress_preamble) 121 md |= ECM_MD_PREAMBLE; 122 md |= (regnum << ECM_MD_ADDR_SHIFT) & ECM_MD_ADDR_MASK; 123 md |= (addr << ECM_MD_PHY_ADDR_SHIFT) & ECM_MD_PHY_ADDR_MASK; 124 md |= ((u32)val << ECM_MD_DATA_SHIFT) & ECM_MD_DATA_MASK; 125 iowrite32(md, adapter->addr + ECM_MD_CONTROL); 126 retval = readl_poll_timeout_atomic(adapter->addr + ECM_MD_STATUS, md, 127 !(md & ECM_MD_BUSY), 16, 1000); 128 if (retval != 0) 129 return retval; 130 131 return 0; 132 } 133 134 static void tsnep_set_link_mode(struct tsnep_adapter *adapter) 135 { 136 u32 mode; 137 138 switch (adapter->phydev->speed) { 139 case SPEED_100: 140 mode = ECM_LINK_MODE_100; 141 break; 142 case SPEED_1000: 143 mode = ECM_LINK_MODE_1000; 144 break; 145 default: 146 mode = ECM_LINK_MODE_OFF; 147 break; 148 } 149 iowrite32(mode, adapter->addr + ECM_STATUS); 150 } 151 152 static void tsnep_phy_link_status_change(struct net_device *netdev) 153 { 154 struct tsnep_adapter *adapter = netdev_priv(netdev); 155 struct phy_device *phydev = netdev->phydev; 156 157 if (phydev->link) 158 tsnep_set_link_mode(adapter); 159 160 phy_print_status(netdev->phydev); 161 } 162 163 static int tsnep_phy_loopback(struct tsnep_adapter *adapter, bool enable) 164 { 165 int retval; 166 167 retval = phy_loopback(adapter->phydev, enable); 168 169 /* PHY link state change is not signaled if loopback is enabled, it 170 * would delay a working loopback anyway, let's ensure that loopback 171 * is working immediately by setting link mode directly 172 */ 173 if (!retval && enable) 174 tsnep_set_link_mode(adapter); 175 176 return retval; 177 } 178 179 static int tsnep_phy_open(struct tsnep_adapter *adapter) 180 { 181 struct phy_device *phydev; 182 struct ethtool_eee ethtool_eee; 183 int retval; 184 185 retval = phy_connect_direct(adapter->netdev, adapter->phydev, 186 tsnep_phy_link_status_change, 187 adapter->phy_mode); 188 if (retval) 189 return retval; 190 phydev = adapter->netdev->phydev; 191 192 /* MAC supports only 100Mbps|1000Mbps full duplex 193 * SPE (Single Pair Ethernet) is also an option but not implemented yet 194 */ 195 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT); 196 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Full_BIT); 197 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT); 198 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT); 199 200 /* disable EEE autoneg, EEE not supported by TSNEP */ 201 memset(ðtool_eee, 0, sizeof(ethtool_eee)); 202 phy_ethtool_set_eee(adapter->phydev, ðtool_eee); 203 204 adapter->phydev->irq = PHY_MAC_INTERRUPT; 205 phy_start(adapter->phydev); 206 207 return 0; 208 } 209 210 static void tsnep_phy_close(struct tsnep_adapter *adapter) 211 { 212 phy_stop(adapter->netdev->phydev); 213 phy_disconnect(adapter->netdev->phydev); 214 adapter->netdev->phydev = NULL; 215 } 216 217 static void tsnep_tx_ring_cleanup(struct tsnep_tx *tx) 218 { 219 struct device *dmadev = tx->adapter->dmadev; 220 int i; 221 222 memset(tx->entry, 0, sizeof(tx->entry)); 223 224 for (i = 0; i < TSNEP_RING_PAGE_COUNT; i++) { 225 if (tx->page[i]) { 226 dma_free_coherent(dmadev, PAGE_SIZE, tx->page[i], 227 tx->page_dma[i]); 228 tx->page[i] = NULL; 229 tx->page_dma[i] = 0; 230 } 231 } 232 } 233 234 static int tsnep_tx_ring_init(struct tsnep_tx *tx) 235 { 236 struct device *dmadev = tx->adapter->dmadev; 237 struct tsnep_tx_entry *entry; 238 struct tsnep_tx_entry *next_entry; 239 int i, j; 240 int retval; 241 242 for (i = 0; i < TSNEP_RING_PAGE_COUNT; i++) { 243 tx->page[i] = 244 dma_alloc_coherent(dmadev, PAGE_SIZE, &tx->page_dma[i], 245 GFP_KERNEL); 246 if (!tx->page[i]) { 247 retval = -ENOMEM; 248 goto alloc_failed; 249 } 250 for (j = 0; j < TSNEP_RING_ENTRIES_PER_PAGE; j++) { 251 entry = &tx->entry[TSNEP_RING_ENTRIES_PER_PAGE * i + j]; 252 entry->desc_wb = (struct tsnep_tx_desc_wb *) 253 (((u8 *)tx->page[i]) + TSNEP_DESC_SIZE * j); 254 entry->desc = (struct tsnep_tx_desc *) 255 (((u8 *)entry->desc_wb) + TSNEP_DESC_OFFSET); 256 entry->desc_dma = tx->page_dma[i] + TSNEP_DESC_SIZE * j; 257 } 258 } 259 for (i = 0; i < TSNEP_RING_SIZE; i++) { 260 entry = &tx->entry[i]; 261 next_entry = &tx->entry[(i + 1) % TSNEP_RING_SIZE]; 262 entry->desc->next = __cpu_to_le64(next_entry->desc_dma); 263 } 264 265 return 0; 266 267 alloc_failed: 268 tsnep_tx_ring_cleanup(tx); 269 return retval; 270 } 271 272 static void tsnep_tx_activate(struct tsnep_tx *tx, int index, int length, 273 bool last) 274 { 275 struct tsnep_tx_entry *entry = &tx->entry[index]; 276 277 entry->properties = 0; 278 if (entry->skb) { 279 entry->properties = length & TSNEP_DESC_LENGTH_MASK; 280 entry->properties |= TSNEP_DESC_INTERRUPT_FLAG; 281 if (skb_shinfo(entry->skb)->tx_flags & SKBTX_IN_PROGRESS) 282 entry->properties |= TSNEP_DESC_EXTENDED_WRITEBACK_FLAG; 283 284 /* toggle user flag to prevent false acknowledge 285 * 286 * Only the first fragment is acknowledged. For all other 287 * fragments no acknowledge is done and the last written owner 288 * counter stays in the writeback descriptor. Therefore, it is 289 * possible that the last written owner counter is identical to 290 * the new incremented owner counter and a false acknowledge is 291 * detected before the real acknowledge has been done by 292 * hardware. 293 * 294 * The user flag is used to prevent this situation. The user 295 * flag is copied to the writeback descriptor by the hardware 296 * and is used as additional acknowledge data. By toggeling the 297 * user flag only for the first fragment (which is 298 * acknowledged), it is guaranteed that the last acknowledge 299 * done for this descriptor has used a different user flag and 300 * cannot be detected as false acknowledge. 301 */ 302 entry->owner_user_flag = !entry->owner_user_flag; 303 } 304 if (last) 305 entry->properties |= TSNEP_TX_DESC_LAST_FRAGMENT_FLAG; 306 if (index == tx->increment_owner_counter) { 307 tx->owner_counter++; 308 if (tx->owner_counter == 4) 309 tx->owner_counter = 1; 310 tx->increment_owner_counter--; 311 if (tx->increment_owner_counter < 0) 312 tx->increment_owner_counter = TSNEP_RING_SIZE - 1; 313 } 314 entry->properties |= 315 (tx->owner_counter << TSNEP_DESC_OWNER_COUNTER_SHIFT) & 316 TSNEP_DESC_OWNER_COUNTER_MASK; 317 if (entry->owner_user_flag) 318 entry->properties |= TSNEP_TX_DESC_OWNER_USER_FLAG; 319 entry->desc->more_properties = 320 __cpu_to_le32(entry->len & TSNEP_DESC_LENGTH_MASK); 321 322 /* descriptor properties shall be written last, because valid data is 323 * signaled there 324 */ 325 dma_wmb(); 326 327 entry->desc->properties = __cpu_to_le32(entry->properties); 328 } 329 330 static int tsnep_tx_desc_available(struct tsnep_tx *tx) 331 { 332 if (tx->read <= tx->write) 333 return TSNEP_RING_SIZE - tx->write + tx->read - 1; 334 else 335 return tx->read - tx->write - 1; 336 } 337 338 static int tsnep_tx_map(struct sk_buff *skb, struct tsnep_tx *tx, int count) 339 { 340 struct device *dmadev = tx->adapter->dmadev; 341 struct tsnep_tx_entry *entry; 342 unsigned int len; 343 dma_addr_t dma; 344 int map_len = 0; 345 int i; 346 347 for (i = 0; i < count; i++) { 348 entry = &tx->entry[(tx->write + i) % TSNEP_RING_SIZE]; 349 350 if (i == 0) { 351 len = skb_headlen(skb); 352 dma = dma_map_single(dmadev, skb->data, len, 353 DMA_TO_DEVICE); 354 } else { 355 len = skb_frag_size(&skb_shinfo(skb)->frags[i - 1]); 356 dma = skb_frag_dma_map(dmadev, 357 &skb_shinfo(skb)->frags[i - 1], 358 0, len, DMA_TO_DEVICE); 359 } 360 if (dma_mapping_error(dmadev, dma)) 361 return -ENOMEM; 362 363 entry->len = len; 364 dma_unmap_addr_set(entry, dma, dma); 365 366 entry->desc->tx = __cpu_to_le64(dma); 367 368 map_len += len; 369 } 370 371 return map_len; 372 } 373 374 static int tsnep_tx_unmap(struct tsnep_tx *tx, int index, int count) 375 { 376 struct device *dmadev = tx->adapter->dmadev; 377 struct tsnep_tx_entry *entry; 378 int map_len = 0; 379 int i; 380 381 for (i = 0; i < count; i++) { 382 entry = &tx->entry[(index + i) % TSNEP_RING_SIZE]; 383 384 if (entry->len) { 385 if (i == 0) 386 dma_unmap_single(dmadev, 387 dma_unmap_addr(entry, dma), 388 dma_unmap_len(entry, len), 389 DMA_TO_DEVICE); 390 else 391 dma_unmap_page(dmadev, 392 dma_unmap_addr(entry, dma), 393 dma_unmap_len(entry, len), 394 DMA_TO_DEVICE); 395 map_len += entry->len; 396 entry->len = 0; 397 } 398 } 399 400 return map_len; 401 } 402 403 static netdev_tx_t tsnep_xmit_frame_ring(struct sk_buff *skb, 404 struct tsnep_tx *tx) 405 { 406 unsigned long flags; 407 int count = 1; 408 struct tsnep_tx_entry *entry; 409 int length; 410 int i; 411 int retval; 412 413 if (skb_shinfo(skb)->nr_frags > 0) 414 count += skb_shinfo(skb)->nr_frags; 415 416 spin_lock_irqsave(&tx->lock, flags); 417 418 if (tsnep_tx_desc_available(tx) < count) { 419 /* ring full, shall not happen because queue is stopped if full 420 * below 421 */ 422 netif_stop_queue(tx->adapter->netdev); 423 424 spin_unlock_irqrestore(&tx->lock, flags); 425 426 return NETDEV_TX_BUSY; 427 } 428 429 entry = &tx->entry[tx->write]; 430 entry->skb = skb; 431 432 retval = tsnep_tx_map(skb, tx, count); 433 if (retval < 0) { 434 tsnep_tx_unmap(tx, tx->write, count); 435 dev_kfree_skb_any(entry->skb); 436 entry->skb = NULL; 437 438 tx->dropped++; 439 440 spin_unlock_irqrestore(&tx->lock, flags); 441 442 netdev_err(tx->adapter->netdev, "TX DMA map failed\n"); 443 444 return NETDEV_TX_OK; 445 } 446 length = retval; 447 448 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) 449 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 450 451 for (i = 0; i < count; i++) 452 tsnep_tx_activate(tx, (tx->write + i) % TSNEP_RING_SIZE, length, 453 i == (count - 1)); 454 tx->write = (tx->write + count) % TSNEP_RING_SIZE; 455 456 skb_tx_timestamp(skb); 457 458 /* descriptor properties shall be valid before hardware is notified */ 459 dma_wmb(); 460 461 iowrite32(TSNEP_CONTROL_TX_ENABLE, tx->addr + TSNEP_CONTROL); 462 463 if (tsnep_tx_desc_available(tx) < (MAX_SKB_FRAGS + 1)) { 464 /* ring can get full with next frame */ 465 netif_stop_queue(tx->adapter->netdev); 466 } 467 468 spin_unlock_irqrestore(&tx->lock, flags); 469 470 return NETDEV_TX_OK; 471 } 472 473 static bool tsnep_tx_poll(struct tsnep_tx *tx, int napi_budget) 474 { 475 unsigned long flags; 476 int budget = 128; 477 struct tsnep_tx_entry *entry; 478 int count; 479 int length; 480 481 spin_lock_irqsave(&tx->lock, flags); 482 483 do { 484 if (tx->read == tx->write) 485 break; 486 487 entry = &tx->entry[tx->read]; 488 if ((__le32_to_cpu(entry->desc_wb->properties) & 489 TSNEP_TX_DESC_OWNER_MASK) != 490 (entry->properties & TSNEP_TX_DESC_OWNER_MASK)) 491 break; 492 493 /* descriptor properties shall be read first, because valid data 494 * is signaled there 495 */ 496 dma_rmb(); 497 498 count = 1; 499 if (skb_shinfo(entry->skb)->nr_frags > 0) 500 count += skb_shinfo(entry->skb)->nr_frags; 501 502 length = tsnep_tx_unmap(tx, tx->read, count); 503 504 if ((skb_shinfo(entry->skb)->tx_flags & SKBTX_IN_PROGRESS) && 505 (__le32_to_cpu(entry->desc_wb->properties) & 506 TSNEP_DESC_EXTENDED_WRITEBACK_FLAG)) { 507 struct skb_shared_hwtstamps hwtstamps; 508 u64 timestamp; 509 510 if (skb_shinfo(entry->skb)->tx_flags & 511 SKBTX_HW_TSTAMP_USE_CYCLES) 512 timestamp = 513 __le64_to_cpu(entry->desc_wb->counter); 514 else 515 timestamp = 516 __le64_to_cpu(entry->desc_wb->timestamp); 517 518 memset(&hwtstamps, 0, sizeof(hwtstamps)); 519 hwtstamps.hwtstamp = ns_to_ktime(timestamp); 520 521 skb_tstamp_tx(entry->skb, &hwtstamps); 522 } 523 524 napi_consume_skb(entry->skb, budget); 525 entry->skb = NULL; 526 527 tx->read = (tx->read + count) % TSNEP_RING_SIZE; 528 529 tx->packets++; 530 tx->bytes += length + ETH_FCS_LEN; 531 532 budget--; 533 } while (likely(budget)); 534 535 if ((tsnep_tx_desc_available(tx) >= ((MAX_SKB_FRAGS + 1) * 2)) && 536 netif_queue_stopped(tx->adapter->netdev)) { 537 netif_wake_queue(tx->adapter->netdev); 538 } 539 540 spin_unlock_irqrestore(&tx->lock, flags); 541 542 return (budget != 0); 543 } 544 545 static int tsnep_tx_open(struct tsnep_adapter *adapter, void __iomem *addr, 546 int queue_index, struct tsnep_tx *tx) 547 { 548 dma_addr_t dma; 549 int retval; 550 551 memset(tx, 0, sizeof(*tx)); 552 tx->adapter = adapter; 553 tx->addr = addr; 554 tx->queue_index = queue_index; 555 556 retval = tsnep_tx_ring_init(tx); 557 if (retval) 558 return retval; 559 560 dma = tx->entry[0].desc_dma | TSNEP_RESET_OWNER_COUNTER; 561 iowrite32(DMA_ADDR_LOW(dma), tx->addr + TSNEP_TX_DESC_ADDR_LOW); 562 iowrite32(DMA_ADDR_HIGH(dma), tx->addr + TSNEP_TX_DESC_ADDR_HIGH); 563 tx->owner_counter = 1; 564 tx->increment_owner_counter = TSNEP_RING_SIZE - 1; 565 566 spin_lock_init(&tx->lock); 567 568 return 0; 569 } 570 571 static void tsnep_tx_close(struct tsnep_tx *tx) 572 { 573 u32 val; 574 575 readx_poll_timeout(ioread32, tx->addr + TSNEP_CONTROL, val, 576 ((val & TSNEP_CONTROL_TX_ENABLE) == 0), 10000, 577 1000000); 578 579 tsnep_tx_ring_cleanup(tx); 580 } 581 582 static void tsnep_rx_ring_cleanup(struct tsnep_rx *rx) 583 { 584 struct device *dmadev = rx->adapter->dmadev; 585 struct tsnep_rx_entry *entry; 586 int i; 587 588 for (i = 0; i < TSNEP_RING_SIZE; i++) { 589 entry = &rx->entry[i]; 590 if (entry->page) 591 page_pool_put_full_page(rx->page_pool, entry->page, 592 false); 593 entry->page = NULL; 594 } 595 596 if (rx->page_pool) 597 page_pool_destroy(rx->page_pool); 598 599 memset(rx->entry, 0, sizeof(rx->entry)); 600 601 for (i = 0; i < TSNEP_RING_PAGE_COUNT; i++) { 602 if (rx->page[i]) { 603 dma_free_coherent(dmadev, PAGE_SIZE, rx->page[i], 604 rx->page_dma[i]); 605 rx->page[i] = NULL; 606 rx->page_dma[i] = 0; 607 } 608 } 609 } 610 611 static int tsnep_rx_alloc_buffer(struct tsnep_rx *rx, 612 struct tsnep_rx_entry *entry) 613 { 614 struct page *page; 615 616 page = page_pool_dev_alloc_pages(rx->page_pool); 617 if (unlikely(!page)) 618 return -ENOMEM; 619 620 entry->page = page; 621 entry->len = TSNEP_MAX_RX_BUF_SIZE; 622 entry->dma = page_pool_get_dma_addr(entry->page); 623 entry->desc->rx = __cpu_to_le64(entry->dma + TSNEP_SKB_PAD); 624 625 return 0; 626 } 627 628 static int tsnep_rx_ring_init(struct tsnep_rx *rx) 629 { 630 struct device *dmadev = rx->adapter->dmadev; 631 struct tsnep_rx_entry *entry; 632 struct page_pool_params pp_params = { 0 }; 633 struct tsnep_rx_entry *next_entry; 634 int i, j; 635 int retval; 636 637 for (i = 0; i < TSNEP_RING_PAGE_COUNT; i++) { 638 rx->page[i] = 639 dma_alloc_coherent(dmadev, PAGE_SIZE, &rx->page_dma[i], 640 GFP_KERNEL); 641 if (!rx->page[i]) { 642 retval = -ENOMEM; 643 goto failed; 644 } 645 for (j = 0; j < TSNEP_RING_ENTRIES_PER_PAGE; j++) { 646 entry = &rx->entry[TSNEP_RING_ENTRIES_PER_PAGE * i + j]; 647 entry->desc_wb = (struct tsnep_rx_desc_wb *) 648 (((u8 *)rx->page[i]) + TSNEP_DESC_SIZE * j); 649 entry->desc = (struct tsnep_rx_desc *) 650 (((u8 *)entry->desc_wb) + TSNEP_DESC_OFFSET); 651 entry->desc_dma = rx->page_dma[i] + TSNEP_DESC_SIZE * j; 652 } 653 } 654 655 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 656 pp_params.order = 0; 657 pp_params.pool_size = TSNEP_RING_SIZE; 658 pp_params.nid = dev_to_node(dmadev); 659 pp_params.dev = dmadev; 660 pp_params.dma_dir = DMA_FROM_DEVICE; 661 pp_params.max_len = TSNEP_MAX_RX_BUF_SIZE; 662 pp_params.offset = TSNEP_SKB_PAD; 663 rx->page_pool = page_pool_create(&pp_params); 664 if (IS_ERR(rx->page_pool)) { 665 retval = PTR_ERR(rx->page_pool); 666 rx->page_pool = NULL; 667 goto failed; 668 } 669 670 for (i = 0; i < TSNEP_RING_SIZE; i++) { 671 entry = &rx->entry[i]; 672 next_entry = &rx->entry[(i + 1) % TSNEP_RING_SIZE]; 673 entry->desc->next = __cpu_to_le64(next_entry->desc_dma); 674 675 retval = tsnep_rx_alloc_buffer(rx, entry); 676 if (retval) 677 goto failed; 678 } 679 680 return 0; 681 682 failed: 683 tsnep_rx_ring_cleanup(rx); 684 return retval; 685 } 686 687 static void tsnep_rx_activate(struct tsnep_rx *rx, int index) 688 { 689 struct tsnep_rx_entry *entry = &rx->entry[index]; 690 691 /* TSNEP_MAX_RX_BUF_SIZE is a multiple of 4 */ 692 entry->properties = entry->len & TSNEP_DESC_LENGTH_MASK; 693 entry->properties |= TSNEP_DESC_INTERRUPT_FLAG; 694 if (index == rx->increment_owner_counter) { 695 rx->owner_counter++; 696 if (rx->owner_counter == 4) 697 rx->owner_counter = 1; 698 rx->increment_owner_counter--; 699 if (rx->increment_owner_counter < 0) 700 rx->increment_owner_counter = TSNEP_RING_SIZE - 1; 701 } 702 entry->properties |= 703 (rx->owner_counter << TSNEP_DESC_OWNER_COUNTER_SHIFT) & 704 TSNEP_DESC_OWNER_COUNTER_MASK; 705 706 /* descriptor properties shall be written last, because valid data is 707 * signaled there 708 */ 709 dma_wmb(); 710 711 entry->desc->properties = __cpu_to_le32(entry->properties); 712 } 713 714 static struct sk_buff *tsnep_build_skb(struct tsnep_rx *rx, struct page *page, 715 int length) 716 { 717 struct sk_buff *skb; 718 719 skb = napi_build_skb(page_address(page), PAGE_SIZE); 720 if (unlikely(!skb)) 721 return NULL; 722 723 /* update pointers within the skb to store the data */ 724 skb_reserve(skb, TSNEP_SKB_PAD + TSNEP_RX_INLINE_METADATA_SIZE); 725 __skb_put(skb, length - TSNEP_RX_INLINE_METADATA_SIZE - ETH_FCS_LEN); 726 727 if (rx->adapter->hwtstamp_config.rx_filter == HWTSTAMP_FILTER_ALL) { 728 struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb); 729 struct tsnep_rx_inline *rx_inline = 730 (struct tsnep_rx_inline *)(page_address(page) + 731 TSNEP_SKB_PAD); 732 733 skb_shinfo(skb)->tx_flags |= 734 SKBTX_HW_TSTAMP_NETDEV; 735 memset(hwtstamps, 0, sizeof(*hwtstamps)); 736 hwtstamps->netdev_data = rx_inline; 737 } 738 739 skb_record_rx_queue(skb, rx->queue_index); 740 skb->protocol = eth_type_trans(skb, rx->adapter->netdev); 741 742 return skb; 743 } 744 745 static int tsnep_rx_poll(struct tsnep_rx *rx, struct napi_struct *napi, 746 int budget) 747 { 748 struct device *dmadev = rx->adapter->dmadev; 749 int done = 0; 750 enum dma_data_direction dma_dir; 751 struct tsnep_rx_entry *entry; 752 struct page *page; 753 struct sk_buff *skb; 754 int length; 755 bool enable = false; 756 int retval; 757 758 dma_dir = page_pool_get_dma_dir(rx->page_pool); 759 760 while (likely(done < budget)) { 761 entry = &rx->entry[rx->read]; 762 if ((__le32_to_cpu(entry->desc_wb->properties) & 763 TSNEP_DESC_OWNER_COUNTER_MASK) != 764 (entry->properties & TSNEP_DESC_OWNER_COUNTER_MASK)) 765 break; 766 767 /* descriptor properties shall be read first, because valid data 768 * is signaled there 769 */ 770 dma_rmb(); 771 772 prefetch(page_address(entry->page) + TSNEP_SKB_PAD); 773 length = __le32_to_cpu(entry->desc_wb->properties) & 774 TSNEP_DESC_LENGTH_MASK; 775 dma_sync_single_range_for_cpu(dmadev, entry->dma, TSNEP_SKB_PAD, 776 length, dma_dir); 777 page = entry->page; 778 779 /* forward skb only if allocation is successful, otherwise 780 * page is reused and frame dropped 781 */ 782 retval = tsnep_rx_alloc_buffer(rx, entry); 783 if (!retval) { 784 skb = tsnep_build_skb(rx, page, length); 785 if (skb) { 786 page_pool_release_page(rx->page_pool, page); 787 788 rx->packets++; 789 rx->bytes += length - 790 TSNEP_RX_INLINE_METADATA_SIZE; 791 if (skb->pkt_type == PACKET_MULTICAST) 792 rx->multicast++; 793 794 napi_gro_receive(napi, skb); 795 } else { 796 page_pool_recycle_direct(rx->page_pool, page); 797 798 rx->dropped++; 799 } 800 done++; 801 } else { 802 rx->dropped++; 803 } 804 805 tsnep_rx_activate(rx, rx->read); 806 807 enable = true; 808 809 rx->read = (rx->read + 1) % TSNEP_RING_SIZE; 810 } 811 812 if (enable) { 813 /* descriptor properties shall be valid before hardware is 814 * notified 815 */ 816 dma_wmb(); 817 818 iowrite32(TSNEP_CONTROL_RX_ENABLE, rx->addr + TSNEP_CONTROL); 819 } 820 821 return done; 822 } 823 824 static int tsnep_rx_open(struct tsnep_adapter *adapter, void __iomem *addr, 825 int queue_index, struct tsnep_rx *rx) 826 { 827 dma_addr_t dma; 828 int i; 829 int retval; 830 831 memset(rx, 0, sizeof(*rx)); 832 rx->adapter = adapter; 833 rx->addr = addr; 834 rx->queue_index = queue_index; 835 836 retval = tsnep_rx_ring_init(rx); 837 if (retval) 838 return retval; 839 840 dma = rx->entry[0].desc_dma | TSNEP_RESET_OWNER_COUNTER; 841 iowrite32(DMA_ADDR_LOW(dma), rx->addr + TSNEP_RX_DESC_ADDR_LOW); 842 iowrite32(DMA_ADDR_HIGH(dma), rx->addr + TSNEP_RX_DESC_ADDR_HIGH); 843 rx->owner_counter = 1; 844 rx->increment_owner_counter = TSNEP_RING_SIZE - 1; 845 846 for (i = 0; i < TSNEP_RING_SIZE; i++) 847 tsnep_rx_activate(rx, i); 848 849 /* descriptor properties shall be valid before hardware is notified */ 850 dma_wmb(); 851 852 iowrite32(TSNEP_CONTROL_RX_ENABLE, rx->addr + TSNEP_CONTROL); 853 854 return 0; 855 } 856 857 static void tsnep_rx_close(struct tsnep_rx *rx) 858 { 859 u32 val; 860 861 iowrite32(TSNEP_CONTROL_RX_DISABLE, rx->addr + TSNEP_CONTROL); 862 readx_poll_timeout(ioread32, rx->addr + TSNEP_CONTROL, val, 863 ((val & TSNEP_CONTROL_RX_ENABLE) == 0), 10000, 864 1000000); 865 866 tsnep_rx_ring_cleanup(rx); 867 } 868 869 static int tsnep_poll(struct napi_struct *napi, int budget) 870 { 871 struct tsnep_queue *queue = container_of(napi, struct tsnep_queue, 872 napi); 873 bool complete = true; 874 int done = 0; 875 876 if (queue->tx) 877 complete = tsnep_tx_poll(queue->tx, budget); 878 879 if (queue->rx) { 880 done = tsnep_rx_poll(queue->rx, napi, budget); 881 if (done >= budget) 882 complete = false; 883 } 884 885 /* if all work not completed, return budget and keep polling */ 886 if (!complete) 887 return budget; 888 889 if (likely(napi_complete_done(napi, done))) 890 tsnep_enable_irq(queue->adapter, queue->irq_mask); 891 892 return min(done, budget - 1); 893 } 894 895 static int tsnep_request_irq(struct tsnep_queue *queue, bool first) 896 { 897 const char *name = netdev_name(queue->adapter->netdev); 898 irq_handler_t handler; 899 void *dev; 900 int retval; 901 902 if (first) { 903 sprintf(queue->name, "%s-mac", name); 904 handler = tsnep_irq; 905 dev = queue->adapter; 906 } else { 907 if (queue->tx && queue->rx) 908 sprintf(queue->name, "%s-txrx-%d", name, 909 queue->rx->queue_index); 910 else if (queue->tx) 911 sprintf(queue->name, "%s-tx-%d", name, 912 queue->tx->queue_index); 913 else 914 sprintf(queue->name, "%s-rx-%d", name, 915 queue->rx->queue_index); 916 handler = tsnep_irq_txrx; 917 dev = queue; 918 } 919 920 retval = request_irq(queue->irq, handler, 0, queue->name, dev); 921 if (retval) { 922 /* if name is empty, then interrupt won't be freed */ 923 memset(queue->name, 0, sizeof(queue->name)); 924 } 925 926 return retval; 927 } 928 929 static void tsnep_free_irq(struct tsnep_queue *queue, bool first) 930 { 931 void *dev; 932 933 if (!strlen(queue->name)) 934 return; 935 936 if (first) 937 dev = queue->adapter; 938 else 939 dev = queue; 940 941 free_irq(queue->irq, dev); 942 memset(queue->name, 0, sizeof(queue->name)); 943 } 944 945 static int tsnep_netdev_open(struct net_device *netdev) 946 { 947 struct tsnep_adapter *adapter = netdev_priv(netdev); 948 int i; 949 void __iomem *addr; 950 int tx_queue_index = 0; 951 int rx_queue_index = 0; 952 int retval; 953 954 for (i = 0; i < adapter->num_queues; i++) { 955 adapter->queue[i].adapter = adapter; 956 if (adapter->queue[i].tx) { 957 addr = adapter->addr + TSNEP_QUEUE(tx_queue_index); 958 retval = tsnep_tx_open(adapter, addr, tx_queue_index, 959 adapter->queue[i].tx); 960 if (retval) 961 goto failed; 962 tx_queue_index++; 963 } 964 if (adapter->queue[i].rx) { 965 addr = adapter->addr + TSNEP_QUEUE(rx_queue_index); 966 retval = tsnep_rx_open(adapter, addr, 967 rx_queue_index, 968 adapter->queue[i].rx); 969 if (retval) 970 goto failed; 971 rx_queue_index++; 972 } 973 974 retval = tsnep_request_irq(&adapter->queue[i], i == 0); 975 if (retval) { 976 netif_err(adapter, drv, adapter->netdev, 977 "can't get assigned irq %d.\n", 978 adapter->queue[i].irq); 979 goto failed; 980 } 981 } 982 983 retval = netif_set_real_num_tx_queues(adapter->netdev, 984 adapter->num_tx_queues); 985 if (retval) 986 goto failed; 987 retval = netif_set_real_num_rx_queues(adapter->netdev, 988 adapter->num_rx_queues); 989 if (retval) 990 goto failed; 991 992 tsnep_enable_irq(adapter, ECM_INT_LINK); 993 retval = tsnep_phy_open(adapter); 994 if (retval) 995 goto phy_failed; 996 997 for (i = 0; i < adapter->num_queues; i++) { 998 netif_napi_add(adapter->netdev, &adapter->queue[i].napi, 999 tsnep_poll); 1000 napi_enable(&adapter->queue[i].napi); 1001 1002 tsnep_enable_irq(adapter, adapter->queue[i].irq_mask); 1003 } 1004 1005 return 0; 1006 1007 phy_failed: 1008 tsnep_disable_irq(adapter, ECM_INT_LINK); 1009 tsnep_phy_close(adapter); 1010 failed: 1011 for (i = 0; i < adapter->num_queues; i++) { 1012 tsnep_free_irq(&adapter->queue[i], i == 0); 1013 1014 if (adapter->queue[i].rx) 1015 tsnep_rx_close(adapter->queue[i].rx); 1016 if (adapter->queue[i].tx) 1017 tsnep_tx_close(adapter->queue[i].tx); 1018 } 1019 return retval; 1020 } 1021 1022 static int tsnep_netdev_close(struct net_device *netdev) 1023 { 1024 struct tsnep_adapter *adapter = netdev_priv(netdev); 1025 int i; 1026 1027 tsnep_disable_irq(adapter, ECM_INT_LINK); 1028 tsnep_phy_close(adapter); 1029 1030 for (i = 0; i < adapter->num_queues; i++) { 1031 tsnep_disable_irq(adapter, adapter->queue[i].irq_mask); 1032 1033 napi_disable(&adapter->queue[i].napi); 1034 netif_napi_del(&adapter->queue[i].napi); 1035 1036 tsnep_free_irq(&adapter->queue[i], i == 0); 1037 1038 if (adapter->queue[i].rx) 1039 tsnep_rx_close(adapter->queue[i].rx); 1040 if (adapter->queue[i].tx) 1041 tsnep_tx_close(adapter->queue[i].tx); 1042 } 1043 1044 return 0; 1045 } 1046 1047 static netdev_tx_t tsnep_netdev_xmit_frame(struct sk_buff *skb, 1048 struct net_device *netdev) 1049 { 1050 struct tsnep_adapter *adapter = netdev_priv(netdev); 1051 u16 queue_mapping = skb_get_queue_mapping(skb); 1052 1053 if (queue_mapping >= adapter->num_tx_queues) 1054 queue_mapping = 0; 1055 1056 return tsnep_xmit_frame_ring(skb, &adapter->tx[queue_mapping]); 1057 } 1058 1059 static int tsnep_netdev_ioctl(struct net_device *netdev, struct ifreq *ifr, 1060 int cmd) 1061 { 1062 if (!netif_running(netdev)) 1063 return -EINVAL; 1064 if (cmd == SIOCSHWTSTAMP || cmd == SIOCGHWTSTAMP) 1065 return tsnep_ptp_ioctl(netdev, ifr, cmd); 1066 return phy_mii_ioctl(netdev->phydev, ifr, cmd); 1067 } 1068 1069 static void tsnep_netdev_set_multicast(struct net_device *netdev) 1070 { 1071 struct tsnep_adapter *adapter = netdev_priv(netdev); 1072 1073 u16 rx_filter = 0; 1074 1075 /* configured MAC address and broadcasts are never filtered */ 1076 if (netdev->flags & IFF_PROMISC) { 1077 rx_filter |= TSNEP_RX_FILTER_ACCEPT_ALL_MULTICASTS; 1078 rx_filter |= TSNEP_RX_FILTER_ACCEPT_ALL_UNICASTS; 1079 } else if (!netdev_mc_empty(netdev) || (netdev->flags & IFF_ALLMULTI)) { 1080 rx_filter |= TSNEP_RX_FILTER_ACCEPT_ALL_MULTICASTS; 1081 } 1082 iowrite16(rx_filter, adapter->addr + TSNEP_RX_FILTER); 1083 } 1084 1085 static void tsnep_netdev_get_stats64(struct net_device *netdev, 1086 struct rtnl_link_stats64 *stats) 1087 { 1088 struct tsnep_adapter *adapter = netdev_priv(netdev); 1089 u32 reg; 1090 u32 val; 1091 int i; 1092 1093 for (i = 0; i < adapter->num_tx_queues; i++) { 1094 stats->tx_packets += adapter->tx[i].packets; 1095 stats->tx_bytes += adapter->tx[i].bytes; 1096 stats->tx_dropped += adapter->tx[i].dropped; 1097 } 1098 for (i = 0; i < adapter->num_rx_queues; i++) { 1099 stats->rx_packets += adapter->rx[i].packets; 1100 stats->rx_bytes += adapter->rx[i].bytes; 1101 stats->rx_dropped += adapter->rx[i].dropped; 1102 stats->multicast += adapter->rx[i].multicast; 1103 1104 reg = ioread32(adapter->addr + TSNEP_QUEUE(i) + 1105 TSNEP_RX_STATISTIC); 1106 val = (reg & TSNEP_RX_STATISTIC_NO_DESC_MASK) >> 1107 TSNEP_RX_STATISTIC_NO_DESC_SHIFT; 1108 stats->rx_dropped += val; 1109 val = (reg & TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_MASK) >> 1110 TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_SHIFT; 1111 stats->rx_dropped += val; 1112 val = (reg & TSNEP_RX_STATISTIC_FIFO_OVERFLOW_MASK) >> 1113 TSNEP_RX_STATISTIC_FIFO_OVERFLOW_SHIFT; 1114 stats->rx_errors += val; 1115 stats->rx_fifo_errors += val; 1116 val = (reg & TSNEP_RX_STATISTIC_INVALID_FRAME_MASK) >> 1117 TSNEP_RX_STATISTIC_INVALID_FRAME_SHIFT; 1118 stats->rx_errors += val; 1119 stats->rx_frame_errors += val; 1120 } 1121 1122 reg = ioread32(adapter->addr + ECM_STAT); 1123 val = (reg & ECM_STAT_RX_ERR_MASK) >> ECM_STAT_RX_ERR_SHIFT; 1124 stats->rx_errors += val; 1125 val = (reg & ECM_STAT_INV_FRM_MASK) >> ECM_STAT_INV_FRM_SHIFT; 1126 stats->rx_errors += val; 1127 stats->rx_crc_errors += val; 1128 val = (reg & ECM_STAT_FWD_RX_ERR_MASK) >> ECM_STAT_FWD_RX_ERR_SHIFT; 1129 stats->rx_errors += val; 1130 } 1131 1132 static void tsnep_mac_set_address(struct tsnep_adapter *adapter, u8 *addr) 1133 { 1134 iowrite32(*(u32 *)addr, adapter->addr + TSNEP_MAC_ADDRESS_LOW); 1135 iowrite16(*(u16 *)(addr + sizeof(u32)), 1136 adapter->addr + TSNEP_MAC_ADDRESS_HIGH); 1137 1138 ether_addr_copy(adapter->mac_address, addr); 1139 netif_info(adapter, drv, adapter->netdev, "MAC address set to %pM\n", 1140 addr); 1141 } 1142 1143 static int tsnep_netdev_set_mac_address(struct net_device *netdev, void *addr) 1144 { 1145 struct tsnep_adapter *adapter = netdev_priv(netdev); 1146 struct sockaddr *sock_addr = addr; 1147 int retval; 1148 1149 retval = eth_prepare_mac_addr_change(netdev, sock_addr); 1150 if (retval) 1151 return retval; 1152 eth_hw_addr_set(netdev, sock_addr->sa_data); 1153 tsnep_mac_set_address(adapter, sock_addr->sa_data); 1154 1155 return 0; 1156 } 1157 1158 static int tsnep_netdev_set_features(struct net_device *netdev, 1159 netdev_features_t features) 1160 { 1161 struct tsnep_adapter *adapter = netdev_priv(netdev); 1162 netdev_features_t changed = netdev->features ^ features; 1163 bool enable; 1164 int retval = 0; 1165 1166 if (changed & NETIF_F_LOOPBACK) { 1167 enable = !!(features & NETIF_F_LOOPBACK); 1168 retval = tsnep_phy_loopback(adapter, enable); 1169 } 1170 1171 return retval; 1172 } 1173 1174 static ktime_t tsnep_netdev_get_tstamp(struct net_device *netdev, 1175 const struct skb_shared_hwtstamps *hwtstamps, 1176 bool cycles) 1177 { 1178 struct tsnep_rx_inline *rx_inline = hwtstamps->netdev_data; 1179 u64 timestamp; 1180 1181 if (cycles) 1182 timestamp = __le64_to_cpu(rx_inline->counter); 1183 else 1184 timestamp = __le64_to_cpu(rx_inline->timestamp); 1185 1186 return ns_to_ktime(timestamp); 1187 } 1188 1189 static const struct net_device_ops tsnep_netdev_ops = { 1190 .ndo_open = tsnep_netdev_open, 1191 .ndo_stop = tsnep_netdev_close, 1192 .ndo_start_xmit = tsnep_netdev_xmit_frame, 1193 .ndo_eth_ioctl = tsnep_netdev_ioctl, 1194 .ndo_set_rx_mode = tsnep_netdev_set_multicast, 1195 .ndo_get_stats64 = tsnep_netdev_get_stats64, 1196 .ndo_set_mac_address = tsnep_netdev_set_mac_address, 1197 .ndo_set_features = tsnep_netdev_set_features, 1198 .ndo_get_tstamp = tsnep_netdev_get_tstamp, 1199 .ndo_setup_tc = tsnep_tc_setup, 1200 }; 1201 1202 static int tsnep_mac_init(struct tsnep_adapter *adapter) 1203 { 1204 int retval; 1205 1206 /* initialize RX filtering, at least configured MAC address and 1207 * broadcast are not filtered 1208 */ 1209 iowrite16(0, adapter->addr + TSNEP_RX_FILTER); 1210 1211 /* try to get MAC address in the following order: 1212 * - device tree 1213 * - valid MAC address already set 1214 * - MAC address register if valid 1215 * - random MAC address 1216 */ 1217 retval = of_get_mac_address(adapter->pdev->dev.of_node, 1218 adapter->mac_address); 1219 if (retval == -EPROBE_DEFER) 1220 return retval; 1221 if (retval && !is_valid_ether_addr(adapter->mac_address)) { 1222 *(u32 *)adapter->mac_address = 1223 ioread32(adapter->addr + TSNEP_MAC_ADDRESS_LOW); 1224 *(u16 *)(adapter->mac_address + sizeof(u32)) = 1225 ioread16(adapter->addr + TSNEP_MAC_ADDRESS_HIGH); 1226 if (!is_valid_ether_addr(adapter->mac_address)) 1227 eth_random_addr(adapter->mac_address); 1228 } 1229 1230 tsnep_mac_set_address(adapter, adapter->mac_address); 1231 eth_hw_addr_set(adapter->netdev, adapter->mac_address); 1232 1233 return 0; 1234 } 1235 1236 static int tsnep_mdio_init(struct tsnep_adapter *adapter) 1237 { 1238 struct device_node *np = adapter->pdev->dev.of_node; 1239 int retval; 1240 1241 if (np) { 1242 np = of_get_child_by_name(np, "mdio"); 1243 if (!np) 1244 return 0; 1245 1246 adapter->suppress_preamble = 1247 of_property_read_bool(np, "suppress-preamble"); 1248 } 1249 1250 adapter->mdiobus = devm_mdiobus_alloc(&adapter->pdev->dev); 1251 if (!adapter->mdiobus) { 1252 retval = -ENOMEM; 1253 1254 goto out; 1255 } 1256 1257 adapter->mdiobus->priv = (void *)adapter; 1258 adapter->mdiobus->parent = &adapter->pdev->dev; 1259 adapter->mdiobus->read = tsnep_mdiobus_read; 1260 adapter->mdiobus->write = tsnep_mdiobus_write; 1261 adapter->mdiobus->name = TSNEP "-mdiobus"; 1262 snprintf(adapter->mdiobus->id, MII_BUS_ID_SIZE, "%s", 1263 adapter->pdev->name); 1264 1265 /* do not scan broadcast address */ 1266 adapter->mdiobus->phy_mask = 0x0000001; 1267 1268 retval = of_mdiobus_register(adapter->mdiobus, np); 1269 1270 out: 1271 of_node_put(np); 1272 1273 return retval; 1274 } 1275 1276 static int tsnep_phy_init(struct tsnep_adapter *adapter) 1277 { 1278 struct device_node *phy_node; 1279 int retval; 1280 1281 retval = of_get_phy_mode(adapter->pdev->dev.of_node, 1282 &adapter->phy_mode); 1283 if (retval) 1284 adapter->phy_mode = PHY_INTERFACE_MODE_GMII; 1285 1286 phy_node = of_parse_phandle(adapter->pdev->dev.of_node, "phy-handle", 1287 0); 1288 adapter->phydev = of_phy_find_device(phy_node); 1289 of_node_put(phy_node); 1290 if (!adapter->phydev && adapter->mdiobus) 1291 adapter->phydev = phy_find_first(adapter->mdiobus); 1292 if (!adapter->phydev) 1293 return -EIO; 1294 1295 return 0; 1296 } 1297 1298 static int tsnep_queue_init(struct tsnep_adapter *adapter, int queue_count) 1299 { 1300 u32 irq_mask = ECM_INT_TX_0 | ECM_INT_RX_0; 1301 char name[8]; 1302 int i; 1303 int retval; 1304 1305 /* one TX/RX queue pair for netdev is mandatory */ 1306 if (platform_irq_count(adapter->pdev) == 1) 1307 retval = platform_get_irq(adapter->pdev, 0); 1308 else 1309 retval = platform_get_irq_byname(adapter->pdev, "mac"); 1310 if (retval < 0) 1311 return retval; 1312 adapter->num_tx_queues = 1; 1313 adapter->num_rx_queues = 1; 1314 adapter->num_queues = 1; 1315 adapter->queue[0].irq = retval; 1316 adapter->queue[0].tx = &adapter->tx[0]; 1317 adapter->queue[0].rx = &adapter->rx[0]; 1318 adapter->queue[0].irq_mask = irq_mask; 1319 1320 adapter->netdev->irq = adapter->queue[0].irq; 1321 1322 /* add additional TX/RX queue pairs only if dedicated interrupt is 1323 * available 1324 */ 1325 for (i = 1; i < queue_count; i++) { 1326 sprintf(name, "txrx-%d", i); 1327 retval = platform_get_irq_byname_optional(adapter->pdev, name); 1328 if (retval < 0) 1329 break; 1330 1331 adapter->num_tx_queues++; 1332 adapter->num_rx_queues++; 1333 adapter->num_queues++; 1334 adapter->queue[i].irq = retval; 1335 adapter->queue[i].tx = &adapter->tx[i]; 1336 adapter->queue[i].rx = &adapter->rx[i]; 1337 adapter->queue[i].irq_mask = 1338 irq_mask << (ECM_INT_TXRX_SHIFT * i); 1339 } 1340 1341 return 0; 1342 } 1343 1344 static int tsnep_probe(struct platform_device *pdev) 1345 { 1346 struct tsnep_adapter *adapter; 1347 struct net_device *netdev; 1348 struct resource *io; 1349 u32 type; 1350 int revision; 1351 int version; 1352 int queue_count; 1353 int retval; 1354 1355 netdev = devm_alloc_etherdev_mqs(&pdev->dev, 1356 sizeof(struct tsnep_adapter), 1357 TSNEP_MAX_QUEUES, TSNEP_MAX_QUEUES); 1358 if (!netdev) 1359 return -ENODEV; 1360 SET_NETDEV_DEV(netdev, &pdev->dev); 1361 adapter = netdev_priv(netdev); 1362 platform_set_drvdata(pdev, adapter); 1363 adapter->pdev = pdev; 1364 adapter->dmadev = &pdev->dev; 1365 adapter->netdev = netdev; 1366 adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE | 1367 NETIF_MSG_LINK | NETIF_MSG_IFUP | 1368 NETIF_MSG_IFDOWN | NETIF_MSG_TX_QUEUED; 1369 1370 netdev->min_mtu = ETH_MIN_MTU; 1371 netdev->max_mtu = TSNEP_MAX_FRAME_SIZE; 1372 1373 mutex_init(&adapter->gate_control_lock); 1374 mutex_init(&adapter->rxnfc_lock); 1375 INIT_LIST_HEAD(&adapter->rxnfc_rules); 1376 1377 io = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1378 adapter->addr = devm_ioremap_resource(&pdev->dev, io); 1379 if (IS_ERR(adapter->addr)) 1380 return PTR_ERR(adapter->addr); 1381 netdev->mem_start = io->start; 1382 netdev->mem_end = io->end; 1383 1384 type = ioread32(adapter->addr + ECM_TYPE); 1385 revision = (type & ECM_REVISION_MASK) >> ECM_REVISION_SHIFT; 1386 version = (type & ECM_VERSION_MASK) >> ECM_VERSION_SHIFT; 1387 queue_count = (type & ECM_QUEUE_COUNT_MASK) >> ECM_QUEUE_COUNT_SHIFT; 1388 adapter->gate_control = type & ECM_GATE_CONTROL; 1389 adapter->rxnfc_max = TSNEP_RX_ASSIGN_ETHER_TYPE_COUNT; 1390 1391 tsnep_disable_irq(adapter, ECM_INT_ALL); 1392 1393 retval = tsnep_queue_init(adapter, queue_count); 1394 if (retval) 1395 return retval; 1396 1397 retval = dma_set_mask_and_coherent(&adapter->pdev->dev, 1398 DMA_BIT_MASK(64)); 1399 if (retval) { 1400 dev_err(&adapter->pdev->dev, "no usable DMA configuration.\n"); 1401 return retval; 1402 } 1403 1404 retval = tsnep_mac_init(adapter); 1405 if (retval) 1406 return retval; 1407 1408 retval = tsnep_mdio_init(adapter); 1409 if (retval) 1410 goto mdio_init_failed; 1411 1412 retval = tsnep_phy_init(adapter); 1413 if (retval) 1414 goto phy_init_failed; 1415 1416 retval = tsnep_ptp_init(adapter); 1417 if (retval) 1418 goto ptp_init_failed; 1419 1420 retval = tsnep_tc_init(adapter); 1421 if (retval) 1422 goto tc_init_failed; 1423 1424 retval = tsnep_rxnfc_init(adapter); 1425 if (retval) 1426 goto rxnfc_init_failed; 1427 1428 netdev->netdev_ops = &tsnep_netdev_ops; 1429 netdev->ethtool_ops = &tsnep_ethtool_ops; 1430 netdev->features = NETIF_F_SG; 1431 netdev->hw_features = netdev->features | NETIF_F_LOOPBACK; 1432 1433 /* carrier off reporting is important to ethtool even BEFORE open */ 1434 netif_carrier_off(netdev); 1435 1436 retval = register_netdev(netdev); 1437 if (retval) 1438 goto register_failed; 1439 1440 dev_info(&adapter->pdev->dev, "device version %d.%02d\n", version, 1441 revision); 1442 if (adapter->gate_control) 1443 dev_info(&adapter->pdev->dev, "gate control detected\n"); 1444 1445 return 0; 1446 1447 register_failed: 1448 tsnep_rxnfc_cleanup(adapter); 1449 rxnfc_init_failed: 1450 tsnep_tc_cleanup(adapter); 1451 tc_init_failed: 1452 tsnep_ptp_cleanup(adapter); 1453 ptp_init_failed: 1454 phy_init_failed: 1455 if (adapter->mdiobus) 1456 mdiobus_unregister(adapter->mdiobus); 1457 mdio_init_failed: 1458 return retval; 1459 } 1460 1461 static int tsnep_remove(struct platform_device *pdev) 1462 { 1463 struct tsnep_adapter *adapter = platform_get_drvdata(pdev); 1464 1465 unregister_netdev(adapter->netdev); 1466 1467 tsnep_rxnfc_cleanup(adapter); 1468 1469 tsnep_tc_cleanup(adapter); 1470 1471 tsnep_ptp_cleanup(adapter); 1472 1473 if (adapter->mdiobus) 1474 mdiobus_unregister(adapter->mdiobus); 1475 1476 tsnep_disable_irq(adapter, ECM_INT_ALL); 1477 1478 return 0; 1479 } 1480 1481 static const struct of_device_id tsnep_of_match[] = { 1482 { .compatible = "engleder,tsnep", }, 1483 { }, 1484 }; 1485 MODULE_DEVICE_TABLE(of, tsnep_of_match); 1486 1487 static struct platform_driver tsnep_driver = { 1488 .driver = { 1489 .name = TSNEP, 1490 .of_match_table = tsnep_of_match, 1491 }, 1492 .probe = tsnep_probe, 1493 .remove = tsnep_remove, 1494 }; 1495 module_platform_driver(tsnep_driver); 1496 1497 MODULE_AUTHOR("Gerhard Engleder <gerhard@engleder-embedded.com>"); 1498 MODULE_DESCRIPTION("TSN endpoint Ethernet MAC driver"); 1499 MODULE_LICENSE("GPL"); 1500