1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* Copyright (C) 2018 Microchip Technology Inc. */ 3 4 #include <linux/module.h> 5 #include <linux/pci.h> 6 #include <linux/netdevice.h> 7 #include <linux/etherdevice.h> 8 #include <linux/crc32.h> 9 #include <linux/microchipphy.h> 10 #include <linux/net_tstamp.h> 11 #include <linux/of_mdio.h> 12 #include <linux/of_net.h> 13 #include <linux/phy.h> 14 #include <linux/phy_fixed.h> 15 #include <linux/rtnetlink.h> 16 #include <linux/iopoll.h> 17 #include <linux/crc16.h> 18 #include "lan743x_main.h" 19 #include "lan743x_ethtool.h" 20 21 static void lan743x_pci_cleanup(struct lan743x_adapter *adapter) 22 { 23 pci_release_selected_regions(adapter->pdev, 24 pci_select_bars(adapter->pdev, 25 IORESOURCE_MEM)); 26 pci_disable_device(adapter->pdev); 27 } 28 29 static int lan743x_pci_init(struct lan743x_adapter *adapter, 30 struct pci_dev *pdev) 31 { 32 unsigned long bars = 0; 33 int ret; 34 35 adapter->pdev = pdev; 36 ret = pci_enable_device_mem(pdev); 37 if (ret) 38 goto return_error; 39 40 netif_info(adapter, probe, adapter->netdev, 41 "PCI: Vendor ID = 0x%04X, Device ID = 0x%04X\n", 42 pdev->vendor, pdev->device); 43 bars = pci_select_bars(pdev, IORESOURCE_MEM); 44 if (!test_bit(0, &bars)) 45 goto disable_device; 46 47 ret = pci_request_selected_regions(pdev, bars, DRIVER_NAME); 48 if (ret) 49 goto disable_device; 50 51 pci_set_master(pdev); 52 return 0; 53 54 disable_device: 55 pci_disable_device(adapter->pdev); 56 57 return_error: 58 return ret; 59 } 60 61 u32 lan743x_csr_read(struct lan743x_adapter *adapter, int offset) 62 { 63 return ioread32(&adapter->csr.csr_address[offset]); 64 } 65 66 void lan743x_csr_write(struct lan743x_adapter *adapter, int offset, 67 u32 data) 68 { 69 iowrite32(data, &adapter->csr.csr_address[offset]); 70 } 71 72 #define LAN743X_CSR_READ_OP(offset) lan743x_csr_read(adapter, offset) 73 74 static int lan743x_csr_light_reset(struct lan743x_adapter *adapter) 75 { 76 u32 data; 77 78 data = lan743x_csr_read(adapter, HW_CFG); 79 data |= HW_CFG_LRST_; 80 lan743x_csr_write(adapter, HW_CFG, data); 81 82 return readx_poll_timeout(LAN743X_CSR_READ_OP, HW_CFG, data, 83 !(data & HW_CFG_LRST_), 100000, 10000000); 84 } 85 86 static int lan743x_csr_wait_for_bit(struct lan743x_adapter *adapter, 87 int offset, u32 bit_mask, 88 int target_value, int usleep_min, 89 int usleep_max, int count) 90 { 91 u32 data; 92 93 return readx_poll_timeout(LAN743X_CSR_READ_OP, offset, data, 94 target_value == ((data & bit_mask) ? 1 : 0), 95 usleep_max, usleep_min * count); 96 } 97 98 static int lan743x_csr_init(struct lan743x_adapter *adapter) 99 { 100 struct lan743x_csr *csr = &adapter->csr; 101 resource_size_t bar_start, bar_length; 102 int result; 103 104 bar_start = pci_resource_start(adapter->pdev, 0); 105 bar_length = pci_resource_len(adapter->pdev, 0); 106 csr->csr_address = devm_ioremap(&adapter->pdev->dev, 107 bar_start, bar_length); 108 if (!csr->csr_address) { 109 result = -ENOMEM; 110 goto clean_up; 111 } 112 113 csr->id_rev = lan743x_csr_read(adapter, ID_REV); 114 csr->fpga_rev = lan743x_csr_read(adapter, FPGA_REV); 115 netif_info(adapter, probe, adapter->netdev, 116 "ID_REV = 0x%08X, FPGA_REV = %d.%d\n", 117 csr->id_rev, FPGA_REV_GET_MAJOR_(csr->fpga_rev), 118 FPGA_REV_GET_MINOR_(csr->fpga_rev)); 119 if (!ID_REV_IS_VALID_CHIP_ID_(csr->id_rev)) { 120 result = -ENODEV; 121 goto clean_up; 122 } 123 124 csr->flags = LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR; 125 switch (csr->id_rev & ID_REV_CHIP_REV_MASK_) { 126 case ID_REV_CHIP_REV_A0_: 127 csr->flags |= LAN743X_CSR_FLAG_IS_A0; 128 csr->flags &= ~LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR; 129 break; 130 case ID_REV_CHIP_REV_B0_: 131 csr->flags |= LAN743X_CSR_FLAG_IS_B0; 132 break; 133 } 134 135 result = lan743x_csr_light_reset(adapter); 136 if (result) 137 goto clean_up; 138 return 0; 139 clean_up: 140 return result; 141 } 142 143 static void lan743x_intr_software_isr(void *context) 144 { 145 struct lan743x_adapter *adapter = context; 146 struct lan743x_intr *intr = &adapter->intr; 147 u32 int_sts; 148 149 int_sts = lan743x_csr_read(adapter, INT_STS); 150 if (int_sts & INT_BIT_SW_GP_) { 151 lan743x_csr_write(adapter, INT_STS, INT_BIT_SW_GP_); 152 intr->software_isr_flag = 1; 153 } 154 } 155 156 static void lan743x_tx_isr(void *context, u32 int_sts, u32 flags) 157 { 158 struct lan743x_tx *tx = context; 159 struct lan743x_adapter *adapter = tx->adapter; 160 bool enable_flag = true; 161 162 lan743x_csr_read(adapter, INT_EN_SET); 163 if (flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR) { 164 lan743x_csr_write(adapter, INT_EN_CLR, 165 INT_BIT_DMA_TX_(tx->channel_number)); 166 } 167 168 if (int_sts & INT_BIT_DMA_TX_(tx->channel_number)) { 169 u32 ioc_bit = DMAC_INT_BIT_TX_IOC_(tx->channel_number); 170 u32 dmac_int_sts; 171 u32 dmac_int_en; 172 173 if (flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ) 174 dmac_int_sts = lan743x_csr_read(adapter, DMAC_INT_STS); 175 else 176 dmac_int_sts = ioc_bit; 177 if (flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK) 178 dmac_int_en = lan743x_csr_read(adapter, 179 DMAC_INT_EN_SET); 180 else 181 dmac_int_en = ioc_bit; 182 183 dmac_int_en &= ioc_bit; 184 dmac_int_sts &= dmac_int_en; 185 if (dmac_int_sts & ioc_bit) { 186 napi_schedule(&tx->napi); 187 enable_flag = false;/* poll func will enable later */ 188 } 189 } 190 191 if (enable_flag) 192 /* enable isr */ 193 lan743x_csr_write(adapter, INT_EN_SET, 194 INT_BIT_DMA_TX_(tx->channel_number)); 195 } 196 197 static void lan743x_rx_isr(void *context, u32 int_sts, u32 flags) 198 { 199 struct lan743x_rx *rx = context; 200 struct lan743x_adapter *adapter = rx->adapter; 201 bool enable_flag = true; 202 203 if (flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR) { 204 lan743x_csr_write(adapter, INT_EN_CLR, 205 INT_BIT_DMA_RX_(rx->channel_number)); 206 } 207 208 if (int_sts & INT_BIT_DMA_RX_(rx->channel_number)) { 209 u32 rx_frame_bit = DMAC_INT_BIT_RXFRM_(rx->channel_number); 210 u32 dmac_int_sts; 211 u32 dmac_int_en; 212 213 if (flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ) 214 dmac_int_sts = lan743x_csr_read(adapter, DMAC_INT_STS); 215 else 216 dmac_int_sts = rx_frame_bit; 217 if (flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK) 218 dmac_int_en = lan743x_csr_read(adapter, 219 DMAC_INT_EN_SET); 220 else 221 dmac_int_en = rx_frame_bit; 222 223 dmac_int_en &= rx_frame_bit; 224 dmac_int_sts &= dmac_int_en; 225 if (dmac_int_sts & rx_frame_bit) { 226 napi_schedule(&rx->napi); 227 enable_flag = false;/* poll funct will enable later */ 228 } 229 } 230 231 if (enable_flag) { 232 /* enable isr */ 233 lan743x_csr_write(adapter, INT_EN_SET, 234 INT_BIT_DMA_RX_(rx->channel_number)); 235 } 236 } 237 238 static void lan743x_intr_shared_isr(void *context, u32 int_sts, u32 flags) 239 { 240 struct lan743x_adapter *adapter = context; 241 unsigned int channel; 242 243 if (int_sts & INT_BIT_ALL_RX_) { 244 for (channel = 0; channel < LAN743X_USED_RX_CHANNELS; 245 channel++) { 246 u32 int_bit = INT_BIT_DMA_RX_(channel); 247 248 if (int_sts & int_bit) { 249 lan743x_rx_isr(&adapter->rx[channel], 250 int_bit, flags); 251 int_sts &= ~int_bit; 252 } 253 } 254 } 255 if (int_sts & INT_BIT_ALL_TX_) { 256 for (channel = 0; channel < LAN743X_USED_TX_CHANNELS; 257 channel++) { 258 u32 int_bit = INT_BIT_DMA_TX_(channel); 259 260 if (int_sts & int_bit) { 261 lan743x_tx_isr(&adapter->tx[channel], 262 int_bit, flags); 263 int_sts &= ~int_bit; 264 } 265 } 266 } 267 if (int_sts & INT_BIT_ALL_OTHER_) { 268 if (int_sts & INT_BIT_SW_GP_) { 269 lan743x_intr_software_isr(adapter); 270 int_sts &= ~INT_BIT_SW_GP_; 271 } 272 if (int_sts & INT_BIT_1588_) { 273 lan743x_ptp_isr(adapter); 274 int_sts &= ~INT_BIT_1588_; 275 } 276 } 277 if (int_sts) 278 lan743x_csr_write(adapter, INT_EN_CLR, int_sts); 279 } 280 281 static irqreturn_t lan743x_intr_entry_isr(int irq, void *ptr) 282 { 283 struct lan743x_vector *vector = ptr; 284 struct lan743x_adapter *adapter = vector->adapter; 285 irqreturn_t result = IRQ_NONE; 286 u32 int_enables; 287 u32 int_sts; 288 289 if (vector->flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ) { 290 int_sts = lan743x_csr_read(adapter, INT_STS); 291 } else if (vector->flags & 292 (LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C | 293 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C)) { 294 int_sts = lan743x_csr_read(adapter, INT_STS_R2C); 295 } else { 296 /* use mask as implied status */ 297 int_sts = vector->int_mask | INT_BIT_MAS_; 298 } 299 300 if (!(int_sts & INT_BIT_MAS_)) 301 goto irq_done; 302 303 if (vector->flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR) 304 /* disable vector interrupt */ 305 lan743x_csr_write(adapter, 306 INT_VEC_EN_CLR, 307 INT_VEC_EN_(vector->vector_index)); 308 309 if (vector->flags & LAN743X_VECTOR_FLAG_MASTER_ENABLE_CLEAR) 310 /* disable master interrupt */ 311 lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_MAS_); 312 313 if (vector->flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK) { 314 int_enables = lan743x_csr_read(adapter, INT_EN_SET); 315 } else { 316 /* use vector mask as implied enable mask */ 317 int_enables = vector->int_mask; 318 } 319 320 int_sts &= int_enables; 321 int_sts &= vector->int_mask; 322 if (int_sts) { 323 if (vector->handler) { 324 vector->handler(vector->context, 325 int_sts, vector->flags); 326 } else { 327 /* disable interrupts on this vector */ 328 lan743x_csr_write(adapter, INT_EN_CLR, 329 vector->int_mask); 330 } 331 result = IRQ_HANDLED; 332 } 333 334 if (vector->flags & LAN743X_VECTOR_FLAG_MASTER_ENABLE_SET) 335 /* enable master interrupt */ 336 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_MAS_); 337 338 if (vector->flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET) 339 /* enable vector interrupt */ 340 lan743x_csr_write(adapter, 341 INT_VEC_EN_SET, 342 INT_VEC_EN_(vector->vector_index)); 343 irq_done: 344 return result; 345 } 346 347 static int lan743x_intr_test_isr(struct lan743x_adapter *adapter) 348 { 349 struct lan743x_intr *intr = &adapter->intr; 350 int result = -ENODEV; 351 int timeout = 10; 352 353 intr->software_isr_flag = 0; 354 355 /* enable interrupt */ 356 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_SW_GP_); 357 358 /* activate interrupt here */ 359 lan743x_csr_write(adapter, INT_SET, INT_BIT_SW_GP_); 360 while ((timeout > 0) && (!(intr->software_isr_flag))) { 361 usleep_range(1000, 20000); 362 timeout--; 363 } 364 365 if (intr->software_isr_flag) 366 result = 0; 367 368 /* disable interrupts */ 369 lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_SW_GP_); 370 return result; 371 } 372 373 static int lan743x_intr_register_isr(struct lan743x_adapter *adapter, 374 int vector_index, u32 flags, 375 u32 int_mask, 376 lan743x_vector_handler handler, 377 void *context) 378 { 379 struct lan743x_vector *vector = &adapter->intr.vector_list 380 [vector_index]; 381 int ret; 382 383 vector->adapter = adapter; 384 vector->flags = flags; 385 vector->vector_index = vector_index; 386 vector->int_mask = int_mask; 387 vector->handler = handler; 388 vector->context = context; 389 390 ret = request_irq(vector->irq, 391 lan743x_intr_entry_isr, 392 (flags & LAN743X_VECTOR_FLAG_IRQ_SHARED) ? 393 IRQF_SHARED : 0, DRIVER_NAME, vector); 394 if (ret) { 395 vector->handler = NULL; 396 vector->context = NULL; 397 vector->int_mask = 0; 398 vector->flags = 0; 399 } 400 return ret; 401 } 402 403 static void lan743x_intr_unregister_isr(struct lan743x_adapter *adapter, 404 int vector_index) 405 { 406 struct lan743x_vector *vector = &adapter->intr.vector_list 407 [vector_index]; 408 409 free_irq(vector->irq, vector); 410 vector->handler = NULL; 411 vector->context = NULL; 412 vector->int_mask = 0; 413 vector->flags = 0; 414 } 415 416 static u32 lan743x_intr_get_vector_flags(struct lan743x_adapter *adapter, 417 u32 int_mask) 418 { 419 int index; 420 421 for (index = 0; index < LAN743X_MAX_VECTOR_COUNT; index++) { 422 if (adapter->intr.vector_list[index].int_mask & int_mask) 423 return adapter->intr.vector_list[index].flags; 424 } 425 return 0; 426 } 427 428 static void lan743x_intr_close(struct lan743x_adapter *adapter) 429 { 430 struct lan743x_intr *intr = &adapter->intr; 431 int index = 0; 432 433 lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_MAS_); 434 lan743x_csr_write(adapter, INT_VEC_EN_CLR, 0x000000FF); 435 436 for (index = 0; index < LAN743X_MAX_VECTOR_COUNT; index++) { 437 if (intr->flags & INTR_FLAG_IRQ_REQUESTED(index)) { 438 lan743x_intr_unregister_isr(adapter, index); 439 intr->flags &= ~INTR_FLAG_IRQ_REQUESTED(index); 440 } 441 } 442 443 if (intr->flags & INTR_FLAG_MSI_ENABLED) { 444 pci_disable_msi(adapter->pdev); 445 intr->flags &= ~INTR_FLAG_MSI_ENABLED; 446 } 447 448 if (intr->flags & INTR_FLAG_MSIX_ENABLED) { 449 pci_disable_msix(adapter->pdev); 450 intr->flags &= ~INTR_FLAG_MSIX_ENABLED; 451 } 452 } 453 454 static int lan743x_intr_open(struct lan743x_adapter *adapter) 455 { 456 struct msix_entry msix_entries[LAN743X_MAX_VECTOR_COUNT]; 457 struct lan743x_intr *intr = &adapter->intr; 458 u32 int_vec_en_auto_clr = 0; 459 u32 int_vec_map0 = 0; 460 u32 int_vec_map1 = 0; 461 int ret = -ENODEV; 462 int index = 0; 463 u32 flags = 0; 464 465 intr->number_of_vectors = 0; 466 467 /* Try to set up MSIX interrupts */ 468 memset(&msix_entries[0], 0, 469 sizeof(struct msix_entry) * LAN743X_MAX_VECTOR_COUNT); 470 for (index = 0; index < LAN743X_MAX_VECTOR_COUNT; index++) 471 msix_entries[index].entry = index; 472 ret = pci_enable_msix_range(adapter->pdev, 473 msix_entries, 1, 474 1 + LAN743X_USED_TX_CHANNELS + 475 LAN743X_USED_RX_CHANNELS); 476 477 if (ret > 0) { 478 intr->flags |= INTR_FLAG_MSIX_ENABLED; 479 intr->number_of_vectors = ret; 480 intr->using_vectors = true; 481 for (index = 0; index < intr->number_of_vectors; index++) 482 intr->vector_list[index].irq = msix_entries 483 [index].vector; 484 netif_info(adapter, ifup, adapter->netdev, 485 "using MSIX interrupts, number of vectors = %d\n", 486 intr->number_of_vectors); 487 } 488 489 /* If MSIX failed try to setup using MSI interrupts */ 490 if (!intr->number_of_vectors) { 491 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) { 492 if (!pci_enable_msi(adapter->pdev)) { 493 intr->flags |= INTR_FLAG_MSI_ENABLED; 494 intr->number_of_vectors = 1; 495 intr->using_vectors = true; 496 intr->vector_list[0].irq = 497 adapter->pdev->irq; 498 netif_info(adapter, ifup, adapter->netdev, 499 "using MSI interrupts, number of vectors = %d\n", 500 intr->number_of_vectors); 501 } 502 } 503 } 504 505 /* If MSIX, and MSI failed, setup using legacy interrupt */ 506 if (!intr->number_of_vectors) { 507 intr->number_of_vectors = 1; 508 intr->using_vectors = false; 509 intr->vector_list[0].irq = intr->irq; 510 netif_info(adapter, ifup, adapter->netdev, 511 "using legacy interrupts\n"); 512 } 513 514 /* At this point we must have at least one irq */ 515 lan743x_csr_write(adapter, INT_VEC_EN_CLR, 0xFFFFFFFF); 516 517 /* map all interrupts to vector 0 */ 518 lan743x_csr_write(adapter, INT_VEC_MAP0, 0x00000000); 519 lan743x_csr_write(adapter, INT_VEC_MAP1, 0x00000000); 520 lan743x_csr_write(adapter, INT_VEC_MAP2, 0x00000000); 521 flags = LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ | 522 LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C | 523 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK | 524 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR; 525 526 if (intr->using_vectors) { 527 flags |= LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR | 528 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET; 529 } else { 530 flags |= LAN743X_VECTOR_FLAG_MASTER_ENABLE_CLEAR | 531 LAN743X_VECTOR_FLAG_MASTER_ENABLE_SET | 532 LAN743X_VECTOR_FLAG_IRQ_SHARED; 533 } 534 535 if (adapter->csr.flags & LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR) { 536 flags &= ~LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ; 537 flags &= ~LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C; 538 flags &= ~LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR; 539 flags &= ~LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK; 540 flags |= LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C; 541 flags |= LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C; 542 } 543 544 ret = lan743x_intr_register_isr(adapter, 0, flags, 545 INT_BIT_ALL_RX_ | INT_BIT_ALL_TX_ | 546 INT_BIT_ALL_OTHER_, 547 lan743x_intr_shared_isr, adapter); 548 if (ret) 549 goto clean_up; 550 intr->flags |= INTR_FLAG_IRQ_REQUESTED(0); 551 552 if (intr->using_vectors) 553 lan743x_csr_write(adapter, INT_VEC_EN_SET, 554 INT_VEC_EN_(0)); 555 556 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) { 557 lan743x_csr_write(adapter, INT_MOD_CFG0, LAN743X_INT_MOD); 558 lan743x_csr_write(adapter, INT_MOD_CFG1, LAN743X_INT_MOD); 559 lan743x_csr_write(adapter, INT_MOD_CFG2, LAN743X_INT_MOD); 560 lan743x_csr_write(adapter, INT_MOD_CFG3, LAN743X_INT_MOD); 561 lan743x_csr_write(adapter, INT_MOD_CFG4, LAN743X_INT_MOD); 562 lan743x_csr_write(adapter, INT_MOD_CFG5, LAN743X_INT_MOD); 563 lan743x_csr_write(adapter, INT_MOD_CFG6, LAN743X_INT_MOD); 564 lan743x_csr_write(adapter, INT_MOD_CFG7, LAN743X_INT_MOD); 565 lan743x_csr_write(adapter, INT_MOD_MAP0, 0x00005432); 566 lan743x_csr_write(adapter, INT_MOD_MAP1, 0x00000001); 567 lan743x_csr_write(adapter, INT_MOD_MAP2, 0x00FFFFFF); 568 } 569 570 /* enable interrupts */ 571 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_MAS_); 572 ret = lan743x_intr_test_isr(adapter); 573 if (ret) 574 goto clean_up; 575 576 if (intr->number_of_vectors > 1) { 577 int number_of_tx_vectors = intr->number_of_vectors - 1; 578 579 if (number_of_tx_vectors > LAN743X_USED_TX_CHANNELS) 580 number_of_tx_vectors = LAN743X_USED_TX_CHANNELS; 581 flags = LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ | 582 LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C | 583 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK | 584 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR | 585 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR | 586 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET; 587 588 if (adapter->csr.flags & 589 LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR) { 590 flags = LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET | 591 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET | 592 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR | 593 LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR; 594 } 595 596 for (index = 0; index < number_of_tx_vectors; index++) { 597 u32 int_bit = INT_BIT_DMA_TX_(index); 598 int vector = index + 1; 599 600 /* map TX interrupt to vector */ 601 int_vec_map1 |= INT_VEC_MAP1_TX_VEC_(index, vector); 602 lan743x_csr_write(adapter, INT_VEC_MAP1, int_vec_map1); 603 604 /* Remove TX interrupt from shared mask */ 605 intr->vector_list[0].int_mask &= ~int_bit; 606 ret = lan743x_intr_register_isr(adapter, vector, flags, 607 int_bit, lan743x_tx_isr, 608 &adapter->tx[index]); 609 if (ret) 610 goto clean_up; 611 intr->flags |= INTR_FLAG_IRQ_REQUESTED(vector); 612 if (!(flags & 613 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET)) 614 lan743x_csr_write(adapter, INT_VEC_EN_SET, 615 INT_VEC_EN_(vector)); 616 } 617 } 618 if ((intr->number_of_vectors - LAN743X_USED_TX_CHANNELS) > 1) { 619 int number_of_rx_vectors = intr->number_of_vectors - 620 LAN743X_USED_TX_CHANNELS - 1; 621 622 if (number_of_rx_vectors > LAN743X_USED_RX_CHANNELS) 623 number_of_rx_vectors = LAN743X_USED_RX_CHANNELS; 624 625 flags = LAN743X_VECTOR_FLAG_SOURCE_STATUS_READ | 626 LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C | 627 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CHECK | 628 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_CLEAR | 629 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_CLEAR | 630 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_ISR_SET; 631 632 if (adapter->csr.flags & 633 LAN743X_CSR_FLAG_SUPPORTS_INTR_AUTO_SET_CLR) { 634 flags = LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_CLEAR | 635 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET | 636 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET | 637 LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR | 638 LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR; 639 } 640 for (index = 0; index < number_of_rx_vectors; index++) { 641 int vector = index + 1 + LAN743X_USED_TX_CHANNELS; 642 u32 int_bit = INT_BIT_DMA_RX_(index); 643 644 /* map RX interrupt to vector */ 645 int_vec_map0 |= INT_VEC_MAP0_RX_VEC_(index, vector); 646 lan743x_csr_write(adapter, INT_VEC_MAP0, int_vec_map0); 647 if (flags & 648 LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_CLEAR) { 649 int_vec_en_auto_clr |= INT_VEC_EN_(vector); 650 lan743x_csr_write(adapter, INT_VEC_EN_AUTO_CLR, 651 int_vec_en_auto_clr); 652 } 653 654 /* Remove RX interrupt from shared mask */ 655 intr->vector_list[0].int_mask &= ~int_bit; 656 ret = lan743x_intr_register_isr(adapter, vector, flags, 657 int_bit, lan743x_rx_isr, 658 &adapter->rx[index]); 659 if (ret) 660 goto clean_up; 661 intr->flags |= INTR_FLAG_IRQ_REQUESTED(vector); 662 663 lan743x_csr_write(adapter, INT_VEC_EN_SET, 664 INT_VEC_EN_(vector)); 665 } 666 } 667 return 0; 668 669 clean_up: 670 lan743x_intr_close(adapter); 671 return ret; 672 } 673 674 static int lan743x_dp_write(struct lan743x_adapter *adapter, 675 u32 select, u32 addr, u32 length, u32 *buf) 676 { 677 int ret = -EIO; 678 u32 dp_sel; 679 int i; 680 681 mutex_lock(&adapter->dp_lock); 682 if (lan743x_csr_wait_for_bit(adapter, DP_SEL, DP_SEL_DPRDY_, 683 1, 40, 100, 100)) 684 goto unlock; 685 dp_sel = lan743x_csr_read(adapter, DP_SEL); 686 dp_sel &= ~DP_SEL_MASK_; 687 dp_sel |= select; 688 lan743x_csr_write(adapter, DP_SEL, dp_sel); 689 690 for (i = 0; i < length; i++) { 691 lan743x_csr_write(adapter, DP_ADDR, addr + i); 692 lan743x_csr_write(adapter, DP_DATA_0, buf[i]); 693 lan743x_csr_write(adapter, DP_CMD, DP_CMD_WRITE_); 694 if (lan743x_csr_wait_for_bit(adapter, DP_SEL, DP_SEL_DPRDY_, 695 1, 40, 100, 100)) 696 goto unlock; 697 } 698 ret = 0; 699 700 unlock: 701 mutex_unlock(&adapter->dp_lock); 702 return ret; 703 } 704 705 static u32 lan743x_mac_mii_access(u16 id, u16 index, int read) 706 { 707 u32 ret; 708 709 ret = (id << MAC_MII_ACC_PHY_ADDR_SHIFT_) & 710 MAC_MII_ACC_PHY_ADDR_MASK_; 711 ret |= (index << MAC_MII_ACC_MIIRINDA_SHIFT_) & 712 MAC_MII_ACC_MIIRINDA_MASK_; 713 714 if (read) 715 ret |= MAC_MII_ACC_MII_READ_; 716 else 717 ret |= MAC_MII_ACC_MII_WRITE_; 718 ret |= MAC_MII_ACC_MII_BUSY_; 719 720 return ret; 721 } 722 723 static int lan743x_mac_mii_wait_till_not_busy(struct lan743x_adapter *adapter) 724 { 725 u32 data; 726 727 return readx_poll_timeout(LAN743X_CSR_READ_OP, MAC_MII_ACC, data, 728 !(data & MAC_MII_ACC_MII_BUSY_), 0, 1000000); 729 } 730 731 static int lan743x_mdiobus_read(struct mii_bus *bus, int phy_id, int index) 732 { 733 struct lan743x_adapter *adapter = bus->priv; 734 u32 val, mii_access; 735 int ret; 736 737 /* comfirm MII not busy */ 738 ret = lan743x_mac_mii_wait_till_not_busy(adapter); 739 if (ret < 0) 740 return ret; 741 742 /* set the address, index & direction (read from PHY) */ 743 mii_access = lan743x_mac_mii_access(phy_id, index, MAC_MII_READ); 744 lan743x_csr_write(adapter, MAC_MII_ACC, mii_access); 745 ret = lan743x_mac_mii_wait_till_not_busy(adapter); 746 if (ret < 0) 747 return ret; 748 749 val = lan743x_csr_read(adapter, MAC_MII_DATA); 750 return (int)(val & 0xFFFF); 751 } 752 753 static int lan743x_mdiobus_write(struct mii_bus *bus, 754 int phy_id, int index, u16 regval) 755 { 756 struct lan743x_adapter *adapter = bus->priv; 757 u32 val, mii_access; 758 int ret; 759 760 /* confirm MII not busy */ 761 ret = lan743x_mac_mii_wait_till_not_busy(adapter); 762 if (ret < 0) 763 return ret; 764 val = (u32)regval; 765 lan743x_csr_write(adapter, MAC_MII_DATA, val); 766 767 /* set the address, index & direction (write to PHY) */ 768 mii_access = lan743x_mac_mii_access(phy_id, index, MAC_MII_WRITE); 769 lan743x_csr_write(adapter, MAC_MII_ACC, mii_access); 770 ret = lan743x_mac_mii_wait_till_not_busy(adapter); 771 return ret; 772 } 773 774 static void lan743x_mac_set_address(struct lan743x_adapter *adapter, 775 u8 *addr) 776 { 777 u32 addr_lo, addr_hi; 778 779 addr_lo = addr[0] | 780 addr[1] << 8 | 781 addr[2] << 16 | 782 addr[3] << 24; 783 addr_hi = addr[4] | 784 addr[5] << 8; 785 lan743x_csr_write(adapter, MAC_RX_ADDRL, addr_lo); 786 lan743x_csr_write(adapter, MAC_RX_ADDRH, addr_hi); 787 788 ether_addr_copy(adapter->mac_address, addr); 789 netif_info(adapter, drv, adapter->netdev, 790 "MAC address set to %pM\n", addr); 791 } 792 793 static int lan743x_mac_init(struct lan743x_adapter *adapter) 794 { 795 bool mac_address_valid = true; 796 struct net_device *netdev; 797 u32 mac_addr_hi = 0; 798 u32 mac_addr_lo = 0; 799 u32 data; 800 801 netdev = adapter->netdev; 802 803 /* disable auto duplex, and speed detection. Phylib does that */ 804 data = lan743x_csr_read(adapter, MAC_CR); 805 data &= ~(MAC_CR_ADD_ | MAC_CR_ASD_); 806 data |= MAC_CR_CNTR_RST_; 807 lan743x_csr_write(adapter, MAC_CR, data); 808 809 if (!is_valid_ether_addr(adapter->mac_address)) { 810 mac_addr_hi = lan743x_csr_read(adapter, MAC_RX_ADDRH); 811 mac_addr_lo = lan743x_csr_read(adapter, MAC_RX_ADDRL); 812 adapter->mac_address[0] = mac_addr_lo & 0xFF; 813 adapter->mac_address[1] = (mac_addr_lo >> 8) & 0xFF; 814 adapter->mac_address[2] = (mac_addr_lo >> 16) & 0xFF; 815 adapter->mac_address[3] = (mac_addr_lo >> 24) & 0xFF; 816 adapter->mac_address[4] = mac_addr_hi & 0xFF; 817 adapter->mac_address[5] = (mac_addr_hi >> 8) & 0xFF; 818 819 if (((mac_addr_hi & 0x0000FFFF) == 0x0000FFFF) && 820 mac_addr_lo == 0xFFFFFFFF) { 821 mac_address_valid = false; 822 } else if (!is_valid_ether_addr(adapter->mac_address)) { 823 mac_address_valid = false; 824 } 825 826 if (!mac_address_valid) 827 eth_random_addr(adapter->mac_address); 828 } 829 lan743x_mac_set_address(adapter, adapter->mac_address); 830 ether_addr_copy(netdev->dev_addr, adapter->mac_address); 831 832 return 0; 833 } 834 835 static int lan743x_mac_open(struct lan743x_adapter *adapter) 836 { 837 int ret = 0; 838 u32 temp; 839 840 temp = lan743x_csr_read(adapter, MAC_RX); 841 lan743x_csr_write(adapter, MAC_RX, temp | MAC_RX_RXEN_); 842 temp = lan743x_csr_read(adapter, MAC_TX); 843 lan743x_csr_write(adapter, MAC_TX, temp | MAC_TX_TXEN_); 844 return ret; 845 } 846 847 static void lan743x_mac_close(struct lan743x_adapter *adapter) 848 { 849 u32 temp; 850 851 temp = lan743x_csr_read(adapter, MAC_TX); 852 temp &= ~MAC_TX_TXEN_; 853 lan743x_csr_write(adapter, MAC_TX, temp); 854 lan743x_csr_wait_for_bit(adapter, MAC_TX, MAC_TX_TXD_, 855 1, 1000, 20000, 100); 856 857 temp = lan743x_csr_read(adapter, MAC_RX); 858 temp &= ~MAC_RX_RXEN_; 859 lan743x_csr_write(adapter, MAC_RX, temp); 860 lan743x_csr_wait_for_bit(adapter, MAC_RX, MAC_RX_RXD_, 861 1, 1000, 20000, 100); 862 } 863 864 static void lan743x_mac_flow_ctrl_set_enables(struct lan743x_adapter *adapter, 865 bool tx_enable, bool rx_enable) 866 { 867 u32 flow_setting = 0; 868 869 /* set maximum pause time because when fifo space frees 870 * up a zero value pause frame will be sent to release the pause 871 */ 872 flow_setting = MAC_FLOW_CR_FCPT_MASK_; 873 if (tx_enable) 874 flow_setting |= MAC_FLOW_CR_TX_FCEN_; 875 if (rx_enable) 876 flow_setting |= MAC_FLOW_CR_RX_FCEN_; 877 lan743x_csr_write(adapter, MAC_FLOW, flow_setting); 878 } 879 880 static int lan743x_mac_set_mtu(struct lan743x_adapter *adapter, int new_mtu) 881 { 882 int enabled = 0; 883 u32 mac_rx = 0; 884 885 mac_rx = lan743x_csr_read(adapter, MAC_RX); 886 if (mac_rx & MAC_RX_RXEN_) { 887 enabled = 1; 888 if (mac_rx & MAC_RX_RXD_) { 889 lan743x_csr_write(adapter, MAC_RX, mac_rx); 890 mac_rx &= ~MAC_RX_RXD_; 891 } 892 mac_rx &= ~MAC_RX_RXEN_; 893 lan743x_csr_write(adapter, MAC_RX, mac_rx); 894 lan743x_csr_wait_for_bit(adapter, MAC_RX, MAC_RX_RXD_, 895 1, 1000, 20000, 100); 896 lan743x_csr_write(adapter, MAC_RX, mac_rx | MAC_RX_RXD_); 897 } 898 899 mac_rx &= ~(MAC_RX_MAX_SIZE_MASK_); 900 mac_rx |= (((new_mtu + ETH_HLEN + 4) << MAC_RX_MAX_SIZE_SHIFT_) & 901 MAC_RX_MAX_SIZE_MASK_); 902 lan743x_csr_write(adapter, MAC_RX, mac_rx); 903 904 if (enabled) { 905 mac_rx |= MAC_RX_RXEN_; 906 lan743x_csr_write(adapter, MAC_RX, mac_rx); 907 } 908 return 0; 909 } 910 911 /* PHY */ 912 static int lan743x_phy_reset(struct lan743x_adapter *adapter) 913 { 914 u32 data; 915 916 /* Only called with in probe, and before mdiobus_register */ 917 918 data = lan743x_csr_read(adapter, PMT_CTL); 919 data |= PMT_CTL_ETH_PHY_RST_; 920 lan743x_csr_write(adapter, PMT_CTL, data); 921 922 return readx_poll_timeout(LAN743X_CSR_READ_OP, PMT_CTL, data, 923 (!(data & PMT_CTL_ETH_PHY_RST_) && 924 (data & PMT_CTL_READY_)), 925 50000, 1000000); 926 } 927 928 static void lan743x_phy_update_flowcontrol(struct lan743x_adapter *adapter, 929 u8 duplex, u16 local_adv, 930 u16 remote_adv) 931 { 932 struct lan743x_phy *phy = &adapter->phy; 933 u8 cap; 934 935 if (phy->fc_autoneg) 936 cap = mii_resolve_flowctrl_fdx(local_adv, remote_adv); 937 else 938 cap = phy->fc_request_control; 939 940 lan743x_mac_flow_ctrl_set_enables(adapter, 941 cap & FLOW_CTRL_TX, 942 cap & FLOW_CTRL_RX); 943 } 944 945 static int lan743x_phy_init(struct lan743x_adapter *adapter) 946 { 947 return lan743x_phy_reset(adapter); 948 } 949 950 static void lan743x_phy_link_status_change(struct net_device *netdev) 951 { 952 struct lan743x_adapter *adapter = netdev_priv(netdev); 953 struct phy_device *phydev = netdev->phydev; 954 u32 data; 955 956 phy_print_status(phydev); 957 if (phydev->state == PHY_RUNNING) { 958 struct ethtool_link_ksettings ksettings; 959 int remote_advertisement = 0; 960 int local_advertisement = 0; 961 962 data = lan743x_csr_read(adapter, MAC_CR); 963 964 /* set interface mode */ 965 if (phy_interface_mode_is_rgmii(adapter->phy_mode)) 966 /* RGMII */ 967 data &= ~MAC_CR_MII_EN_; 968 else 969 /* GMII */ 970 data |= MAC_CR_MII_EN_; 971 972 /* set duplex mode */ 973 if (phydev->duplex) 974 data |= MAC_CR_DPX_; 975 else 976 data &= ~MAC_CR_DPX_; 977 978 /* set bus speed */ 979 switch (phydev->speed) { 980 case SPEED_10: 981 data &= ~MAC_CR_CFG_H_; 982 data &= ~MAC_CR_CFG_L_; 983 break; 984 case SPEED_100: 985 data &= ~MAC_CR_CFG_H_; 986 data |= MAC_CR_CFG_L_; 987 break; 988 case SPEED_1000: 989 data |= MAC_CR_CFG_H_; 990 data &= ~MAC_CR_CFG_L_; 991 break; 992 } 993 lan743x_csr_write(adapter, MAC_CR, data); 994 995 memset(&ksettings, 0, sizeof(ksettings)); 996 phy_ethtool_get_link_ksettings(netdev, &ksettings); 997 local_advertisement = 998 linkmode_adv_to_mii_adv_t(phydev->advertising); 999 remote_advertisement = 1000 linkmode_adv_to_mii_adv_t(phydev->lp_advertising); 1001 1002 lan743x_phy_update_flowcontrol(adapter, 1003 ksettings.base.duplex, 1004 local_advertisement, 1005 remote_advertisement); 1006 lan743x_ptp_update_latency(adapter, ksettings.base.speed); 1007 } 1008 } 1009 1010 static void lan743x_phy_close(struct lan743x_adapter *adapter) 1011 { 1012 struct net_device *netdev = adapter->netdev; 1013 1014 phy_stop(netdev->phydev); 1015 phy_disconnect(netdev->phydev); 1016 netdev->phydev = NULL; 1017 } 1018 1019 static int lan743x_phy_open(struct lan743x_adapter *adapter) 1020 { 1021 struct lan743x_phy *phy = &adapter->phy; 1022 struct device_node *phynode; 1023 struct phy_device *phydev; 1024 struct net_device *netdev; 1025 int ret = -EIO; 1026 1027 netdev = adapter->netdev; 1028 phynode = of_node_get(adapter->pdev->dev.of_node); 1029 adapter->phy_mode = PHY_INTERFACE_MODE_GMII; 1030 1031 if (phynode) { 1032 of_get_phy_mode(phynode, &adapter->phy_mode); 1033 1034 if (of_phy_is_fixed_link(phynode)) { 1035 ret = of_phy_register_fixed_link(phynode); 1036 if (ret) { 1037 netdev_err(netdev, 1038 "cannot register fixed PHY\n"); 1039 of_node_put(phynode); 1040 goto return_error; 1041 } 1042 } 1043 phydev = of_phy_connect(netdev, phynode, 1044 lan743x_phy_link_status_change, 0, 1045 adapter->phy_mode); 1046 of_node_put(phynode); 1047 if (!phydev) 1048 goto return_error; 1049 } else { 1050 phydev = phy_find_first(adapter->mdiobus); 1051 if (!phydev) 1052 goto return_error; 1053 1054 ret = phy_connect_direct(netdev, phydev, 1055 lan743x_phy_link_status_change, 1056 adapter->phy_mode); 1057 if (ret) 1058 goto return_error; 1059 } 1060 1061 /* MAC doesn't support 1000T Half */ 1062 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT); 1063 1064 /* support both flow controls */ 1065 phy_support_asym_pause(phydev); 1066 phy->fc_request_control = (FLOW_CTRL_RX | FLOW_CTRL_TX); 1067 phy->fc_autoneg = phydev->autoneg; 1068 1069 phy_start(phydev); 1070 phy_start_aneg(phydev); 1071 return 0; 1072 1073 return_error: 1074 return ret; 1075 } 1076 1077 static void lan743x_rfe_open(struct lan743x_adapter *adapter) 1078 { 1079 lan743x_csr_write(adapter, RFE_RSS_CFG, 1080 RFE_RSS_CFG_UDP_IPV6_EX_ | 1081 RFE_RSS_CFG_TCP_IPV6_EX_ | 1082 RFE_RSS_CFG_IPV6_EX_ | 1083 RFE_RSS_CFG_UDP_IPV6_ | 1084 RFE_RSS_CFG_TCP_IPV6_ | 1085 RFE_RSS_CFG_IPV6_ | 1086 RFE_RSS_CFG_UDP_IPV4_ | 1087 RFE_RSS_CFG_TCP_IPV4_ | 1088 RFE_RSS_CFG_IPV4_ | 1089 RFE_RSS_CFG_VALID_HASH_BITS_ | 1090 RFE_RSS_CFG_RSS_QUEUE_ENABLE_ | 1091 RFE_RSS_CFG_RSS_HASH_STORE_ | 1092 RFE_RSS_CFG_RSS_ENABLE_); 1093 } 1094 1095 static void lan743x_rfe_update_mac_address(struct lan743x_adapter *adapter) 1096 { 1097 u8 *mac_addr; 1098 u32 mac_addr_hi = 0; 1099 u32 mac_addr_lo = 0; 1100 1101 /* Add mac address to perfect Filter */ 1102 mac_addr = adapter->mac_address; 1103 mac_addr_lo = ((((u32)(mac_addr[0])) << 0) | 1104 (((u32)(mac_addr[1])) << 8) | 1105 (((u32)(mac_addr[2])) << 16) | 1106 (((u32)(mac_addr[3])) << 24)); 1107 mac_addr_hi = ((((u32)(mac_addr[4])) << 0) | 1108 (((u32)(mac_addr[5])) << 8)); 1109 1110 lan743x_csr_write(adapter, RFE_ADDR_FILT_LO(0), mac_addr_lo); 1111 lan743x_csr_write(adapter, RFE_ADDR_FILT_HI(0), 1112 mac_addr_hi | RFE_ADDR_FILT_HI_VALID_); 1113 } 1114 1115 static void lan743x_rfe_set_multicast(struct lan743x_adapter *adapter) 1116 { 1117 struct net_device *netdev = adapter->netdev; 1118 u32 hash_table[DP_SEL_VHF_HASH_LEN]; 1119 u32 rfctl; 1120 u32 data; 1121 1122 rfctl = lan743x_csr_read(adapter, RFE_CTL); 1123 rfctl &= ~(RFE_CTL_AU_ | RFE_CTL_AM_ | 1124 RFE_CTL_DA_PERFECT_ | RFE_CTL_MCAST_HASH_); 1125 rfctl |= RFE_CTL_AB_; 1126 if (netdev->flags & IFF_PROMISC) { 1127 rfctl |= RFE_CTL_AM_ | RFE_CTL_AU_; 1128 } else { 1129 if (netdev->flags & IFF_ALLMULTI) 1130 rfctl |= RFE_CTL_AM_; 1131 } 1132 1133 memset(hash_table, 0, DP_SEL_VHF_HASH_LEN * sizeof(u32)); 1134 if (netdev_mc_count(netdev)) { 1135 struct netdev_hw_addr *ha; 1136 int i; 1137 1138 rfctl |= RFE_CTL_DA_PERFECT_; 1139 i = 1; 1140 netdev_for_each_mc_addr(ha, netdev) { 1141 /* set first 32 into Perfect Filter */ 1142 if (i < 33) { 1143 lan743x_csr_write(adapter, 1144 RFE_ADDR_FILT_HI(i), 0); 1145 data = ha->addr[3]; 1146 data = ha->addr[2] | (data << 8); 1147 data = ha->addr[1] | (data << 8); 1148 data = ha->addr[0] | (data << 8); 1149 lan743x_csr_write(adapter, 1150 RFE_ADDR_FILT_LO(i), data); 1151 data = ha->addr[5]; 1152 data = ha->addr[4] | (data << 8); 1153 data |= RFE_ADDR_FILT_HI_VALID_; 1154 lan743x_csr_write(adapter, 1155 RFE_ADDR_FILT_HI(i), data); 1156 } else { 1157 u32 bitnum = (ether_crc(ETH_ALEN, ha->addr) >> 1158 23) & 0x1FF; 1159 hash_table[bitnum / 32] |= (1 << (bitnum % 32)); 1160 rfctl |= RFE_CTL_MCAST_HASH_; 1161 } 1162 i++; 1163 } 1164 } 1165 1166 lan743x_dp_write(adapter, DP_SEL_RFE_RAM, 1167 DP_SEL_VHF_VLAN_LEN, 1168 DP_SEL_VHF_HASH_LEN, hash_table); 1169 lan743x_csr_write(adapter, RFE_CTL, rfctl); 1170 } 1171 1172 static int lan743x_dmac_init(struct lan743x_adapter *adapter) 1173 { 1174 u32 data = 0; 1175 1176 lan743x_csr_write(adapter, DMAC_CMD, DMAC_CMD_SWR_); 1177 lan743x_csr_wait_for_bit(adapter, DMAC_CMD, DMAC_CMD_SWR_, 1178 0, 1000, 20000, 100); 1179 switch (DEFAULT_DMA_DESCRIPTOR_SPACING) { 1180 case DMA_DESCRIPTOR_SPACING_16: 1181 data = DMAC_CFG_MAX_DSPACE_16_; 1182 break; 1183 case DMA_DESCRIPTOR_SPACING_32: 1184 data = DMAC_CFG_MAX_DSPACE_32_; 1185 break; 1186 case DMA_DESCRIPTOR_SPACING_64: 1187 data = DMAC_CFG_MAX_DSPACE_64_; 1188 break; 1189 case DMA_DESCRIPTOR_SPACING_128: 1190 data = DMAC_CFG_MAX_DSPACE_128_; 1191 break; 1192 default: 1193 return -EPERM; 1194 } 1195 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) 1196 data |= DMAC_CFG_COAL_EN_; 1197 data |= DMAC_CFG_CH_ARB_SEL_RX_HIGH_; 1198 data |= DMAC_CFG_MAX_READ_REQ_SET_(6); 1199 lan743x_csr_write(adapter, DMAC_CFG, data); 1200 data = DMAC_COAL_CFG_TIMER_LIMIT_SET_(1); 1201 data |= DMAC_COAL_CFG_TIMER_TX_START_; 1202 data |= DMAC_COAL_CFG_FLUSH_INTS_; 1203 data |= DMAC_COAL_CFG_INT_EXIT_COAL_; 1204 data |= DMAC_COAL_CFG_CSR_EXIT_COAL_; 1205 data |= DMAC_COAL_CFG_TX_THRES_SET_(0x0A); 1206 data |= DMAC_COAL_CFG_RX_THRES_SET_(0x0C); 1207 lan743x_csr_write(adapter, DMAC_COAL_CFG, data); 1208 data = DMAC_OBFF_TX_THRES_SET_(0x08); 1209 data |= DMAC_OBFF_RX_THRES_SET_(0x0A); 1210 lan743x_csr_write(adapter, DMAC_OBFF_CFG, data); 1211 return 0; 1212 } 1213 1214 static int lan743x_dmac_tx_get_state(struct lan743x_adapter *adapter, 1215 int tx_channel) 1216 { 1217 u32 dmac_cmd = 0; 1218 1219 dmac_cmd = lan743x_csr_read(adapter, DMAC_CMD); 1220 return DMAC_CHANNEL_STATE_SET((dmac_cmd & 1221 DMAC_CMD_START_T_(tx_channel)), 1222 (dmac_cmd & 1223 DMAC_CMD_STOP_T_(tx_channel))); 1224 } 1225 1226 static int lan743x_dmac_tx_wait_till_stopped(struct lan743x_adapter *adapter, 1227 int tx_channel) 1228 { 1229 int timeout = 100; 1230 int result = 0; 1231 1232 while (timeout && 1233 ((result = lan743x_dmac_tx_get_state(adapter, tx_channel)) == 1234 DMAC_CHANNEL_STATE_STOP_PENDING)) { 1235 usleep_range(1000, 20000); 1236 timeout--; 1237 } 1238 if (result == DMAC_CHANNEL_STATE_STOP_PENDING) 1239 result = -ENODEV; 1240 return result; 1241 } 1242 1243 static int lan743x_dmac_rx_get_state(struct lan743x_adapter *adapter, 1244 int rx_channel) 1245 { 1246 u32 dmac_cmd = 0; 1247 1248 dmac_cmd = lan743x_csr_read(adapter, DMAC_CMD); 1249 return DMAC_CHANNEL_STATE_SET((dmac_cmd & 1250 DMAC_CMD_START_R_(rx_channel)), 1251 (dmac_cmd & 1252 DMAC_CMD_STOP_R_(rx_channel))); 1253 } 1254 1255 static int lan743x_dmac_rx_wait_till_stopped(struct lan743x_adapter *adapter, 1256 int rx_channel) 1257 { 1258 int timeout = 100; 1259 int result = 0; 1260 1261 while (timeout && 1262 ((result = lan743x_dmac_rx_get_state(adapter, rx_channel)) == 1263 DMAC_CHANNEL_STATE_STOP_PENDING)) { 1264 usleep_range(1000, 20000); 1265 timeout--; 1266 } 1267 if (result == DMAC_CHANNEL_STATE_STOP_PENDING) 1268 result = -ENODEV; 1269 return result; 1270 } 1271 1272 static void lan743x_tx_release_desc(struct lan743x_tx *tx, 1273 int descriptor_index, bool cleanup) 1274 { 1275 struct lan743x_tx_buffer_info *buffer_info = NULL; 1276 struct lan743x_tx_descriptor *descriptor = NULL; 1277 u32 descriptor_type = 0; 1278 bool ignore_sync; 1279 1280 descriptor = &tx->ring_cpu_ptr[descriptor_index]; 1281 buffer_info = &tx->buffer_info[descriptor_index]; 1282 if (!(buffer_info->flags & TX_BUFFER_INFO_FLAG_ACTIVE)) 1283 goto done; 1284 1285 descriptor_type = (descriptor->data0) & 1286 TX_DESC_DATA0_DTYPE_MASK_; 1287 if (descriptor_type == TX_DESC_DATA0_DTYPE_DATA_) 1288 goto clean_up_data_descriptor; 1289 else 1290 goto clear_active; 1291 1292 clean_up_data_descriptor: 1293 if (buffer_info->dma_ptr) { 1294 if (buffer_info->flags & 1295 TX_BUFFER_INFO_FLAG_SKB_FRAGMENT) { 1296 dma_unmap_page(&tx->adapter->pdev->dev, 1297 buffer_info->dma_ptr, 1298 buffer_info->buffer_length, 1299 DMA_TO_DEVICE); 1300 } else { 1301 dma_unmap_single(&tx->adapter->pdev->dev, 1302 buffer_info->dma_ptr, 1303 buffer_info->buffer_length, 1304 DMA_TO_DEVICE); 1305 } 1306 buffer_info->dma_ptr = 0; 1307 buffer_info->buffer_length = 0; 1308 } 1309 if (!buffer_info->skb) 1310 goto clear_active; 1311 1312 if (!(buffer_info->flags & TX_BUFFER_INFO_FLAG_TIMESTAMP_REQUESTED)) { 1313 dev_kfree_skb(buffer_info->skb); 1314 goto clear_skb; 1315 } 1316 1317 if (cleanup) { 1318 lan743x_ptp_unrequest_tx_timestamp(tx->adapter); 1319 dev_kfree_skb(buffer_info->skb); 1320 } else { 1321 ignore_sync = (buffer_info->flags & 1322 TX_BUFFER_INFO_FLAG_IGNORE_SYNC) != 0; 1323 lan743x_ptp_tx_timestamp_skb(tx->adapter, 1324 buffer_info->skb, ignore_sync); 1325 } 1326 1327 clear_skb: 1328 buffer_info->skb = NULL; 1329 1330 clear_active: 1331 buffer_info->flags &= ~TX_BUFFER_INFO_FLAG_ACTIVE; 1332 1333 done: 1334 memset(buffer_info, 0, sizeof(*buffer_info)); 1335 memset(descriptor, 0, sizeof(*descriptor)); 1336 } 1337 1338 static int lan743x_tx_next_index(struct lan743x_tx *tx, int index) 1339 { 1340 return ((++index) % tx->ring_size); 1341 } 1342 1343 static void lan743x_tx_release_completed_descriptors(struct lan743x_tx *tx) 1344 { 1345 while ((*tx->head_cpu_ptr) != (tx->last_head)) { 1346 lan743x_tx_release_desc(tx, tx->last_head, false); 1347 tx->last_head = lan743x_tx_next_index(tx, tx->last_head); 1348 } 1349 } 1350 1351 static void lan743x_tx_release_all_descriptors(struct lan743x_tx *tx) 1352 { 1353 u32 original_head = 0; 1354 1355 original_head = tx->last_head; 1356 do { 1357 lan743x_tx_release_desc(tx, tx->last_head, true); 1358 tx->last_head = lan743x_tx_next_index(tx, tx->last_head); 1359 } while (tx->last_head != original_head); 1360 memset(tx->ring_cpu_ptr, 0, 1361 sizeof(*tx->ring_cpu_ptr) * (tx->ring_size)); 1362 memset(tx->buffer_info, 0, 1363 sizeof(*tx->buffer_info) * (tx->ring_size)); 1364 } 1365 1366 static int lan743x_tx_get_desc_cnt(struct lan743x_tx *tx, 1367 struct sk_buff *skb) 1368 { 1369 int result = 1; /* 1 for the main skb buffer */ 1370 int nr_frags = 0; 1371 1372 if (skb_is_gso(skb)) 1373 result++; /* requires an extension descriptor */ 1374 nr_frags = skb_shinfo(skb)->nr_frags; 1375 result += nr_frags; /* 1 for each fragment buffer */ 1376 return result; 1377 } 1378 1379 static int lan743x_tx_get_avail_desc(struct lan743x_tx *tx) 1380 { 1381 int last_head = tx->last_head; 1382 int last_tail = tx->last_tail; 1383 1384 if (last_tail >= last_head) 1385 return tx->ring_size - last_tail + last_head - 1; 1386 else 1387 return last_head - last_tail - 1; 1388 } 1389 1390 void lan743x_tx_set_timestamping_mode(struct lan743x_tx *tx, 1391 bool enable_timestamping, 1392 bool enable_onestep_sync) 1393 { 1394 if (enable_timestamping) 1395 tx->ts_flags |= TX_TS_FLAG_TIMESTAMPING_ENABLED; 1396 else 1397 tx->ts_flags &= ~TX_TS_FLAG_TIMESTAMPING_ENABLED; 1398 if (enable_onestep_sync) 1399 tx->ts_flags |= TX_TS_FLAG_ONE_STEP_SYNC; 1400 else 1401 tx->ts_flags &= ~TX_TS_FLAG_ONE_STEP_SYNC; 1402 } 1403 1404 static int lan743x_tx_frame_start(struct lan743x_tx *tx, 1405 unsigned char *first_buffer, 1406 unsigned int first_buffer_length, 1407 unsigned int frame_length, 1408 bool time_stamp, 1409 bool check_sum) 1410 { 1411 /* called only from within lan743x_tx_xmit_frame. 1412 * assuming tx->ring_lock has already been acquired. 1413 */ 1414 struct lan743x_tx_descriptor *tx_descriptor = NULL; 1415 struct lan743x_tx_buffer_info *buffer_info = NULL; 1416 struct lan743x_adapter *adapter = tx->adapter; 1417 struct device *dev = &adapter->pdev->dev; 1418 dma_addr_t dma_ptr; 1419 1420 tx->frame_flags |= TX_FRAME_FLAG_IN_PROGRESS; 1421 tx->frame_first = tx->last_tail; 1422 tx->frame_tail = tx->frame_first; 1423 1424 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail]; 1425 buffer_info = &tx->buffer_info[tx->frame_tail]; 1426 dma_ptr = dma_map_single(dev, first_buffer, first_buffer_length, 1427 DMA_TO_DEVICE); 1428 if (dma_mapping_error(dev, dma_ptr)) 1429 return -ENOMEM; 1430 1431 tx_descriptor->data1 = DMA_ADDR_LOW32(dma_ptr); 1432 tx_descriptor->data2 = DMA_ADDR_HIGH32(dma_ptr); 1433 tx_descriptor->data3 = (frame_length << 16) & 1434 TX_DESC_DATA3_FRAME_LENGTH_MSS_MASK_; 1435 1436 buffer_info->skb = NULL; 1437 buffer_info->dma_ptr = dma_ptr; 1438 buffer_info->buffer_length = first_buffer_length; 1439 buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE; 1440 1441 tx->frame_data0 = (first_buffer_length & 1442 TX_DESC_DATA0_BUF_LENGTH_MASK_) | 1443 TX_DESC_DATA0_DTYPE_DATA_ | 1444 TX_DESC_DATA0_FS_ | 1445 TX_DESC_DATA0_FCS_; 1446 if (time_stamp) 1447 tx->frame_data0 |= TX_DESC_DATA0_TSE_; 1448 1449 if (check_sum) 1450 tx->frame_data0 |= TX_DESC_DATA0_ICE_ | 1451 TX_DESC_DATA0_IPE_ | 1452 TX_DESC_DATA0_TPE_; 1453 1454 /* data0 will be programmed in one of other frame assembler functions */ 1455 return 0; 1456 } 1457 1458 static void lan743x_tx_frame_add_lso(struct lan743x_tx *tx, 1459 unsigned int frame_length, 1460 int nr_frags) 1461 { 1462 /* called only from within lan743x_tx_xmit_frame. 1463 * assuming tx->ring_lock has already been acquired. 1464 */ 1465 struct lan743x_tx_descriptor *tx_descriptor = NULL; 1466 struct lan743x_tx_buffer_info *buffer_info = NULL; 1467 1468 /* wrap up previous descriptor */ 1469 tx->frame_data0 |= TX_DESC_DATA0_EXT_; 1470 if (nr_frags <= 0) { 1471 tx->frame_data0 |= TX_DESC_DATA0_LS_; 1472 tx->frame_data0 |= TX_DESC_DATA0_IOC_; 1473 } 1474 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail]; 1475 tx_descriptor->data0 = tx->frame_data0; 1476 1477 /* move to next descriptor */ 1478 tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail); 1479 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail]; 1480 buffer_info = &tx->buffer_info[tx->frame_tail]; 1481 1482 /* add extension descriptor */ 1483 tx_descriptor->data1 = 0; 1484 tx_descriptor->data2 = 0; 1485 tx_descriptor->data3 = 0; 1486 1487 buffer_info->skb = NULL; 1488 buffer_info->dma_ptr = 0; 1489 buffer_info->buffer_length = 0; 1490 buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE; 1491 1492 tx->frame_data0 = (frame_length & TX_DESC_DATA0_EXT_PAY_LENGTH_MASK_) | 1493 TX_DESC_DATA0_DTYPE_EXT_ | 1494 TX_DESC_DATA0_EXT_LSO_; 1495 1496 /* data0 will be programmed in one of other frame assembler functions */ 1497 } 1498 1499 static int lan743x_tx_frame_add_fragment(struct lan743x_tx *tx, 1500 const skb_frag_t *fragment, 1501 unsigned int frame_length) 1502 { 1503 /* called only from within lan743x_tx_xmit_frame 1504 * assuming tx->ring_lock has already been acquired 1505 */ 1506 struct lan743x_tx_descriptor *tx_descriptor = NULL; 1507 struct lan743x_tx_buffer_info *buffer_info = NULL; 1508 struct lan743x_adapter *adapter = tx->adapter; 1509 struct device *dev = &adapter->pdev->dev; 1510 unsigned int fragment_length = 0; 1511 dma_addr_t dma_ptr; 1512 1513 fragment_length = skb_frag_size(fragment); 1514 if (!fragment_length) 1515 return 0; 1516 1517 /* wrap up previous descriptor */ 1518 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail]; 1519 tx_descriptor->data0 = tx->frame_data0; 1520 1521 /* move to next descriptor */ 1522 tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail); 1523 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail]; 1524 buffer_info = &tx->buffer_info[tx->frame_tail]; 1525 dma_ptr = skb_frag_dma_map(dev, fragment, 1526 0, fragment_length, 1527 DMA_TO_DEVICE); 1528 if (dma_mapping_error(dev, dma_ptr)) { 1529 int desc_index; 1530 1531 /* cleanup all previously setup descriptors */ 1532 desc_index = tx->frame_first; 1533 while (desc_index != tx->frame_tail) { 1534 lan743x_tx_release_desc(tx, desc_index, true); 1535 desc_index = lan743x_tx_next_index(tx, desc_index); 1536 } 1537 dma_wmb(); 1538 tx->frame_flags &= ~TX_FRAME_FLAG_IN_PROGRESS; 1539 tx->frame_first = 0; 1540 tx->frame_data0 = 0; 1541 tx->frame_tail = 0; 1542 return -ENOMEM; 1543 } 1544 1545 tx_descriptor->data1 = DMA_ADDR_LOW32(dma_ptr); 1546 tx_descriptor->data2 = DMA_ADDR_HIGH32(dma_ptr); 1547 tx_descriptor->data3 = (frame_length << 16) & 1548 TX_DESC_DATA3_FRAME_LENGTH_MSS_MASK_; 1549 1550 buffer_info->skb = NULL; 1551 buffer_info->dma_ptr = dma_ptr; 1552 buffer_info->buffer_length = fragment_length; 1553 buffer_info->flags |= TX_BUFFER_INFO_FLAG_ACTIVE; 1554 buffer_info->flags |= TX_BUFFER_INFO_FLAG_SKB_FRAGMENT; 1555 1556 tx->frame_data0 = (fragment_length & TX_DESC_DATA0_BUF_LENGTH_MASK_) | 1557 TX_DESC_DATA0_DTYPE_DATA_ | 1558 TX_DESC_DATA0_FCS_; 1559 1560 /* data0 will be programmed in one of other frame assembler functions */ 1561 return 0; 1562 } 1563 1564 static void lan743x_tx_frame_end(struct lan743x_tx *tx, 1565 struct sk_buff *skb, 1566 bool time_stamp, 1567 bool ignore_sync) 1568 { 1569 /* called only from within lan743x_tx_xmit_frame 1570 * assuming tx->ring_lock has already been acquired 1571 */ 1572 struct lan743x_tx_descriptor *tx_descriptor = NULL; 1573 struct lan743x_tx_buffer_info *buffer_info = NULL; 1574 struct lan743x_adapter *adapter = tx->adapter; 1575 u32 tx_tail_flags = 0; 1576 1577 /* wrap up previous descriptor */ 1578 if ((tx->frame_data0 & TX_DESC_DATA0_DTYPE_MASK_) == 1579 TX_DESC_DATA0_DTYPE_DATA_) { 1580 tx->frame_data0 |= TX_DESC_DATA0_LS_; 1581 tx->frame_data0 |= TX_DESC_DATA0_IOC_; 1582 } 1583 1584 tx_descriptor = &tx->ring_cpu_ptr[tx->frame_tail]; 1585 buffer_info = &tx->buffer_info[tx->frame_tail]; 1586 buffer_info->skb = skb; 1587 if (time_stamp) 1588 buffer_info->flags |= TX_BUFFER_INFO_FLAG_TIMESTAMP_REQUESTED; 1589 if (ignore_sync) 1590 buffer_info->flags |= TX_BUFFER_INFO_FLAG_IGNORE_SYNC; 1591 1592 tx_descriptor->data0 = tx->frame_data0; 1593 tx->frame_tail = lan743x_tx_next_index(tx, tx->frame_tail); 1594 tx->last_tail = tx->frame_tail; 1595 1596 dma_wmb(); 1597 1598 if (tx->vector_flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET) 1599 tx_tail_flags |= TX_TAIL_SET_TOP_INT_VEC_EN_; 1600 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET) 1601 tx_tail_flags |= TX_TAIL_SET_DMAC_INT_EN_ | 1602 TX_TAIL_SET_TOP_INT_EN_; 1603 1604 lan743x_csr_write(adapter, TX_TAIL(tx->channel_number), 1605 tx_tail_flags | tx->frame_tail); 1606 tx->frame_flags &= ~TX_FRAME_FLAG_IN_PROGRESS; 1607 } 1608 1609 static netdev_tx_t lan743x_tx_xmit_frame(struct lan743x_tx *tx, 1610 struct sk_buff *skb) 1611 { 1612 int required_number_of_descriptors = 0; 1613 unsigned int start_frame_length = 0; 1614 unsigned int frame_length = 0; 1615 unsigned int head_length = 0; 1616 unsigned long irq_flags = 0; 1617 bool do_timestamp = false; 1618 bool ignore_sync = false; 1619 int nr_frags = 0; 1620 bool gso = false; 1621 int j; 1622 1623 required_number_of_descriptors = lan743x_tx_get_desc_cnt(tx, skb); 1624 1625 spin_lock_irqsave(&tx->ring_lock, irq_flags); 1626 if (required_number_of_descriptors > 1627 lan743x_tx_get_avail_desc(tx)) { 1628 if (required_number_of_descriptors > (tx->ring_size - 1)) { 1629 dev_kfree_skb(skb); 1630 } else { 1631 /* save to overflow buffer */ 1632 tx->overflow_skb = skb; 1633 netif_stop_queue(tx->adapter->netdev); 1634 } 1635 goto unlock; 1636 } 1637 1638 /* space available, transmit skb */ 1639 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 1640 (tx->ts_flags & TX_TS_FLAG_TIMESTAMPING_ENABLED) && 1641 (lan743x_ptp_request_tx_timestamp(tx->adapter))) { 1642 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 1643 do_timestamp = true; 1644 if (tx->ts_flags & TX_TS_FLAG_ONE_STEP_SYNC) 1645 ignore_sync = true; 1646 } 1647 head_length = skb_headlen(skb); 1648 frame_length = skb_pagelen(skb); 1649 nr_frags = skb_shinfo(skb)->nr_frags; 1650 start_frame_length = frame_length; 1651 gso = skb_is_gso(skb); 1652 if (gso) { 1653 start_frame_length = max(skb_shinfo(skb)->gso_size, 1654 (unsigned short)8); 1655 } 1656 1657 if (lan743x_tx_frame_start(tx, 1658 skb->data, head_length, 1659 start_frame_length, 1660 do_timestamp, 1661 skb->ip_summed == CHECKSUM_PARTIAL)) { 1662 dev_kfree_skb(skb); 1663 goto unlock; 1664 } 1665 1666 if (gso) 1667 lan743x_tx_frame_add_lso(tx, frame_length, nr_frags); 1668 1669 if (nr_frags <= 0) 1670 goto finish; 1671 1672 for (j = 0; j < nr_frags; j++) { 1673 const skb_frag_t *frag = &(skb_shinfo(skb)->frags[j]); 1674 1675 if (lan743x_tx_frame_add_fragment(tx, frag, frame_length)) { 1676 /* upon error no need to call 1677 * lan743x_tx_frame_end 1678 * frame assembler clean up was performed inside 1679 * lan743x_tx_frame_add_fragment 1680 */ 1681 dev_kfree_skb(skb); 1682 goto unlock; 1683 } 1684 } 1685 1686 finish: 1687 lan743x_tx_frame_end(tx, skb, do_timestamp, ignore_sync); 1688 1689 unlock: 1690 spin_unlock_irqrestore(&tx->ring_lock, irq_flags); 1691 return NETDEV_TX_OK; 1692 } 1693 1694 static int lan743x_tx_napi_poll(struct napi_struct *napi, int weight) 1695 { 1696 struct lan743x_tx *tx = container_of(napi, struct lan743x_tx, napi); 1697 struct lan743x_adapter *adapter = tx->adapter; 1698 bool start_transmitter = false; 1699 unsigned long irq_flags = 0; 1700 u32 ioc_bit = 0; 1701 1702 ioc_bit = DMAC_INT_BIT_TX_IOC_(tx->channel_number); 1703 lan743x_csr_read(adapter, DMAC_INT_STS); 1704 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C) 1705 lan743x_csr_write(adapter, DMAC_INT_STS, ioc_bit); 1706 spin_lock_irqsave(&tx->ring_lock, irq_flags); 1707 1708 /* clean up tx ring */ 1709 lan743x_tx_release_completed_descriptors(tx); 1710 if (netif_queue_stopped(adapter->netdev)) { 1711 if (tx->overflow_skb) { 1712 if (lan743x_tx_get_desc_cnt(tx, tx->overflow_skb) <= 1713 lan743x_tx_get_avail_desc(tx)) 1714 start_transmitter = true; 1715 } else { 1716 netif_wake_queue(adapter->netdev); 1717 } 1718 } 1719 spin_unlock_irqrestore(&tx->ring_lock, irq_flags); 1720 1721 if (start_transmitter) { 1722 /* space is now available, transmit overflow skb */ 1723 lan743x_tx_xmit_frame(tx, tx->overflow_skb); 1724 tx->overflow_skb = NULL; 1725 netif_wake_queue(adapter->netdev); 1726 } 1727 1728 if (!napi_complete(napi)) 1729 goto done; 1730 1731 /* enable isr */ 1732 lan743x_csr_write(adapter, INT_EN_SET, 1733 INT_BIT_DMA_TX_(tx->channel_number)); 1734 lan743x_csr_read(adapter, INT_STS); 1735 1736 done: 1737 return 0; 1738 } 1739 1740 static void lan743x_tx_ring_cleanup(struct lan743x_tx *tx) 1741 { 1742 if (tx->head_cpu_ptr) { 1743 dma_free_coherent(&tx->adapter->pdev->dev, 1744 sizeof(*tx->head_cpu_ptr), tx->head_cpu_ptr, 1745 tx->head_dma_ptr); 1746 tx->head_cpu_ptr = NULL; 1747 tx->head_dma_ptr = 0; 1748 } 1749 kfree(tx->buffer_info); 1750 tx->buffer_info = NULL; 1751 1752 if (tx->ring_cpu_ptr) { 1753 dma_free_coherent(&tx->adapter->pdev->dev, 1754 tx->ring_allocation_size, tx->ring_cpu_ptr, 1755 tx->ring_dma_ptr); 1756 tx->ring_allocation_size = 0; 1757 tx->ring_cpu_ptr = NULL; 1758 tx->ring_dma_ptr = 0; 1759 } 1760 tx->ring_size = 0; 1761 } 1762 1763 static int lan743x_tx_ring_init(struct lan743x_tx *tx) 1764 { 1765 size_t ring_allocation_size = 0; 1766 void *cpu_ptr = NULL; 1767 dma_addr_t dma_ptr; 1768 int ret = -ENOMEM; 1769 1770 tx->ring_size = LAN743X_TX_RING_SIZE; 1771 if (tx->ring_size & ~TX_CFG_B_TX_RING_LEN_MASK_) { 1772 ret = -EINVAL; 1773 goto cleanup; 1774 } 1775 ring_allocation_size = ALIGN(tx->ring_size * 1776 sizeof(struct lan743x_tx_descriptor), 1777 PAGE_SIZE); 1778 dma_ptr = 0; 1779 cpu_ptr = dma_alloc_coherent(&tx->adapter->pdev->dev, 1780 ring_allocation_size, &dma_ptr, GFP_KERNEL); 1781 if (!cpu_ptr) { 1782 ret = -ENOMEM; 1783 goto cleanup; 1784 } 1785 1786 tx->ring_allocation_size = ring_allocation_size; 1787 tx->ring_cpu_ptr = (struct lan743x_tx_descriptor *)cpu_ptr; 1788 tx->ring_dma_ptr = dma_ptr; 1789 1790 cpu_ptr = kcalloc(tx->ring_size, sizeof(*tx->buffer_info), GFP_KERNEL); 1791 if (!cpu_ptr) { 1792 ret = -ENOMEM; 1793 goto cleanup; 1794 } 1795 tx->buffer_info = (struct lan743x_tx_buffer_info *)cpu_ptr; 1796 dma_ptr = 0; 1797 cpu_ptr = dma_alloc_coherent(&tx->adapter->pdev->dev, 1798 sizeof(*tx->head_cpu_ptr), &dma_ptr, 1799 GFP_KERNEL); 1800 if (!cpu_ptr) { 1801 ret = -ENOMEM; 1802 goto cleanup; 1803 } 1804 1805 tx->head_cpu_ptr = cpu_ptr; 1806 tx->head_dma_ptr = dma_ptr; 1807 if (tx->head_dma_ptr & 0x3) { 1808 ret = -ENOMEM; 1809 goto cleanup; 1810 } 1811 1812 return 0; 1813 1814 cleanup: 1815 lan743x_tx_ring_cleanup(tx); 1816 return ret; 1817 } 1818 1819 static void lan743x_tx_close(struct lan743x_tx *tx) 1820 { 1821 struct lan743x_adapter *adapter = tx->adapter; 1822 1823 lan743x_csr_write(adapter, 1824 DMAC_CMD, 1825 DMAC_CMD_STOP_T_(tx->channel_number)); 1826 lan743x_dmac_tx_wait_till_stopped(adapter, tx->channel_number); 1827 1828 lan743x_csr_write(adapter, 1829 DMAC_INT_EN_CLR, 1830 DMAC_INT_BIT_TX_IOC_(tx->channel_number)); 1831 lan743x_csr_write(adapter, INT_EN_CLR, 1832 INT_BIT_DMA_TX_(tx->channel_number)); 1833 napi_disable(&tx->napi); 1834 netif_napi_del(&tx->napi); 1835 1836 lan743x_csr_write(adapter, FCT_TX_CTL, 1837 FCT_TX_CTL_DIS_(tx->channel_number)); 1838 lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL, 1839 FCT_TX_CTL_EN_(tx->channel_number), 1840 0, 1000, 20000, 100); 1841 1842 lan743x_tx_release_all_descriptors(tx); 1843 1844 if (tx->overflow_skb) { 1845 dev_kfree_skb(tx->overflow_skb); 1846 tx->overflow_skb = NULL; 1847 } 1848 1849 lan743x_tx_ring_cleanup(tx); 1850 } 1851 1852 static int lan743x_tx_open(struct lan743x_tx *tx) 1853 { 1854 struct lan743x_adapter *adapter = NULL; 1855 u32 data = 0; 1856 int ret; 1857 1858 adapter = tx->adapter; 1859 ret = lan743x_tx_ring_init(tx); 1860 if (ret) 1861 return ret; 1862 1863 /* initialize fifo */ 1864 lan743x_csr_write(adapter, FCT_TX_CTL, 1865 FCT_TX_CTL_RESET_(tx->channel_number)); 1866 lan743x_csr_wait_for_bit(adapter, FCT_TX_CTL, 1867 FCT_TX_CTL_RESET_(tx->channel_number), 1868 0, 1000, 20000, 100); 1869 1870 /* enable fifo */ 1871 lan743x_csr_write(adapter, FCT_TX_CTL, 1872 FCT_TX_CTL_EN_(tx->channel_number)); 1873 1874 /* reset tx channel */ 1875 lan743x_csr_write(adapter, DMAC_CMD, 1876 DMAC_CMD_TX_SWR_(tx->channel_number)); 1877 lan743x_csr_wait_for_bit(adapter, DMAC_CMD, 1878 DMAC_CMD_TX_SWR_(tx->channel_number), 1879 0, 1000, 20000, 100); 1880 1881 /* Write TX_BASE_ADDR */ 1882 lan743x_csr_write(adapter, 1883 TX_BASE_ADDRH(tx->channel_number), 1884 DMA_ADDR_HIGH32(tx->ring_dma_ptr)); 1885 lan743x_csr_write(adapter, 1886 TX_BASE_ADDRL(tx->channel_number), 1887 DMA_ADDR_LOW32(tx->ring_dma_ptr)); 1888 1889 /* Write TX_CFG_B */ 1890 data = lan743x_csr_read(adapter, TX_CFG_B(tx->channel_number)); 1891 data &= ~TX_CFG_B_TX_RING_LEN_MASK_; 1892 data |= ((tx->ring_size) & TX_CFG_B_TX_RING_LEN_MASK_); 1893 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) 1894 data |= TX_CFG_B_TDMABL_512_; 1895 lan743x_csr_write(adapter, TX_CFG_B(tx->channel_number), data); 1896 1897 /* Write TX_CFG_A */ 1898 data = TX_CFG_A_TX_TMR_HPWB_SEL_IOC_ | TX_CFG_A_TX_HP_WB_EN_; 1899 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) { 1900 data |= TX_CFG_A_TX_HP_WB_ON_INT_TMR_; 1901 data |= TX_CFG_A_TX_PF_THRES_SET_(0x10); 1902 data |= TX_CFG_A_TX_PF_PRI_THRES_SET_(0x04); 1903 data |= TX_CFG_A_TX_HP_WB_THRES_SET_(0x07); 1904 } 1905 lan743x_csr_write(adapter, TX_CFG_A(tx->channel_number), data); 1906 1907 /* Write TX_HEAD_WRITEBACK_ADDR */ 1908 lan743x_csr_write(adapter, 1909 TX_HEAD_WRITEBACK_ADDRH(tx->channel_number), 1910 DMA_ADDR_HIGH32(tx->head_dma_ptr)); 1911 lan743x_csr_write(adapter, 1912 TX_HEAD_WRITEBACK_ADDRL(tx->channel_number), 1913 DMA_ADDR_LOW32(tx->head_dma_ptr)); 1914 1915 /* set last head */ 1916 tx->last_head = lan743x_csr_read(adapter, TX_HEAD(tx->channel_number)); 1917 1918 /* write TX_TAIL */ 1919 tx->last_tail = 0; 1920 lan743x_csr_write(adapter, TX_TAIL(tx->channel_number), 1921 (u32)(tx->last_tail)); 1922 tx->vector_flags = lan743x_intr_get_vector_flags(adapter, 1923 INT_BIT_DMA_TX_ 1924 (tx->channel_number)); 1925 netif_tx_napi_add(adapter->netdev, 1926 &tx->napi, lan743x_tx_napi_poll, 1927 tx->ring_size - 1); 1928 napi_enable(&tx->napi); 1929 1930 data = 0; 1931 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR) 1932 data |= TX_CFG_C_TX_TOP_INT_EN_AUTO_CLR_; 1933 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR) 1934 data |= TX_CFG_C_TX_DMA_INT_STS_AUTO_CLR_; 1935 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C) 1936 data |= TX_CFG_C_TX_INT_STS_R2C_MODE_MASK_; 1937 if (tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C) 1938 data |= TX_CFG_C_TX_INT_EN_R2C_; 1939 lan743x_csr_write(adapter, TX_CFG_C(tx->channel_number), data); 1940 1941 if (!(tx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET)) 1942 lan743x_csr_write(adapter, INT_EN_SET, 1943 INT_BIT_DMA_TX_(tx->channel_number)); 1944 lan743x_csr_write(adapter, DMAC_INT_EN_SET, 1945 DMAC_INT_BIT_TX_IOC_(tx->channel_number)); 1946 1947 /* start dmac channel */ 1948 lan743x_csr_write(adapter, DMAC_CMD, 1949 DMAC_CMD_START_T_(tx->channel_number)); 1950 return 0; 1951 } 1952 1953 static int lan743x_rx_next_index(struct lan743x_rx *rx, int index) 1954 { 1955 return ((++index) % rx->ring_size); 1956 } 1957 1958 static struct sk_buff *lan743x_rx_allocate_skb(struct lan743x_rx *rx) 1959 { 1960 int length = 0; 1961 1962 length = (LAN743X_MAX_FRAME_SIZE + ETH_HLEN + 4 + RX_HEAD_PADDING); 1963 return __netdev_alloc_skb(rx->adapter->netdev, 1964 length, GFP_ATOMIC | GFP_DMA); 1965 } 1966 1967 static int lan743x_rx_init_ring_element(struct lan743x_rx *rx, int index, 1968 struct sk_buff *skb) 1969 { 1970 struct lan743x_rx_buffer_info *buffer_info; 1971 struct lan743x_rx_descriptor *descriptor; 1972 int length = 0; 1973 1974 length = (LAN743X_MAX_FRAME_SIZE + ETH_HLEN + 4 + RX_HEAD_PADDING); 1975 descriptor = &rx->ring_cpu_ptr[index]; 1976 buffer_info = &rx->buffer_info[index]; 1977 buffer_info->skb = skb; 1978 if (!(buffer_info->skb)) 1979 return -ENOMEM; 1980 buffer_info->dma_ptr = dma_map_single(&rx->adapter->pdev->dev, 1981 buffer_info->skb->data, 1982 length, 1983 DMA_FROM_DEVICE); 1984 if (dma_mapping_error(&rx->adapter->pdev->dev, 1985 buffer_info->dma_ptr)) { 1986 buffer_info->dma_ptr = 0; 1987 return -ENOMEM; 1988 } 1989 1990 buffer_info->buffer_length = length; 1991 descriptor->data1 = DMA_ADDR_LOW32(buffer_info->dma_ptr); 1992 descriptor->data2 = DMA_ADDR_HIGH32(buffer_info->dma_ptr); 1993 descriptor->data3 = 0; 1994 descriptor->data0 = (RX_DESC_DATA0_OWN_ | 1995 (length & RX_DESC_DATA0_BUF_LENGTH_MASK_)); 1996 skb_reserve(buffer_info->skb, RX_HEAD_PADDING); 1997 1998 return 0; 1999 } 2000 2001 static void lan743x_rx_reuse_ring_element(struct lan743x_rx *rx, int index) 2002 { 2003 struct lan743x_rx_buffer_info *buffer_info; 2004 struct lan743x_rx_descriptor *descriptor; 2005 2006 descriptor = &rx->ring_cpu_ptr[index]; 2007 buffer_info = &rx->buffer_info[index]; 2008 2009 descriptor->data1 = DMA_ADDR_LOW32(buffer_info->dma_ptr); 2010 descriptor->data2 = DMA_ADDR_HIGH32(buffer_info->dma_ptr); 2011 descriptor->data3 = 0; 2012 descriptor->data0 = (RX_DESC_DATA0_OWN_ | 2013 ((buffer_info->buffer_length) & 2014 RX_DESC_DATA0_BUF_LENGTH_MASK_)); 2015 } 2016 2017 static void lan743x_rx_release_ring_element(struct lan743x_rx *rx, int index) 2018 { 2019 struct lan743x_rx_buffer_info *buffer_info; 2020 struct lan743x_rx_descriptor *descriptor; 2021 2022 descriptor = &rx->ring_cpu_ptr[index]; 2023 buffer_info = &rx->buffer_info[index]; 2024 2025 memset(descriptor, 0, sizeof(*descriptor)); 2026 2027 if (buffer_info->dma_ptr) { 2028 dma_unmap_single(&rx->adapter->pdev->dev, 2029 buffer_info->dma_ptr, 2030 buffer_info->buffer_length, 2031 DMA_FROM_DEVICE); 2032 buffer_info->dma_ptr = 0; 2033 } 2034 2035 if (buffer_info->skb) { 2036 dev_kfree_skb(buffer_info->skb); 2037 buffer_info->skb = NULL; 2038 } 2039 2040 memset(buffer_info, 0, sizeof(*buffer_info)); 2041 } 2042 2043 static int lan743x_rx_process_packet(struct lan743x_rx *rx) 2044 { 2045 struct skb_shared_hwtstamps *hwtstamps = NULL; 2046 int result = RX_PROCESS_RESULT_NOTHING_TO_DO; 2047 int current_head_index = *rx->head_cpu_ptr; 2048 struct lan743x_rx_buffer_info *buffer_info; 2049 struct lan743x_rx_descriptor *descriptor; 2050 int extension_index = -1; 2051 int first_index = -1; 2052 int last_index = -1; 2053 2054 if (current_head_index < 0 || current_head_index >= rx->ring_size) 2055 goto done; 2056 2057 if (rx->last_head < 0 || rx->last_head >= rx->ring_size) 2058 goto done; 2059 2060 if (rx->last_head != current_head_index) { 2061 descriptor = &rx->ring_cpu_ptr[rx->last_head]; 2062 if (descriptor->data0 & RX_DESC_DATA0_OWN_) 2063 goto done; 2064 2065 if (!(descriptor->data0 & RX_DESC_DATA0_FS_)) 2066 goto done; 2067 2068 first_index = rx->last_head; 2069 if (descriptor->data0 & RX_DESC_DATA0_LS_) { 2070 last_index = rx->last_head; 2071 } else { 2072 int index; 2073 2074 index = lan743x_rx_next_index(rx, first_index); 2075 while (index != current_head_index) { 2076 descriptor = &rx->ring_cpu_ptr[index]; 2077 if (descriptor->data0 & RX_DESC_DATA0_OWN_) 2078 goto done; 2079 2080 if (descriptor->data0 & RX_DESC_DATA0_LS_) { 2081 last_index = index; 2082 break; 2083 } 2084 index = lan743x_rx_next_index(rx, index); 2085 } 2086 } 2087 if (last_index >= 0) { 2088 descriptor = &rx->ring_cpu_ptr[last_index]; 2089 if (descriptor->data0 & RX_DESC_DATA0_EXT_) { 2090 /* extension is expected to follow */ 2091 int index = lan743x_rx_next_index(rx, 2092 last_index); 2093 if (index != current_head_index) { 2094 descriptor = &rx->ring_cpu_ptr[index]; 2095 if (descriptor->data0 & 2096 RX_DESC_DATA0_OWN_) { 2097 goto done; 2098 } 2099 if (descriptor->data0 & 2100 RX_DESC_DATA0_EXT_) { 2101 extension_index = index; 2102 } else { 2103 goto done; 2104 } 2105 } else { 2106 /* extension is not yet available */ 2107 /* prevent processing of this packet */ 2108 first_index = -1; 2109 last_index = -1; 2110 } 2111 } 2112 } 2113 } 2114 if (first_index >= 0 && last_index >= 0) { 2115 int real_last_index = last_index; 2116 struct sk_buff *skb = NULL; 2117 u32 ts_sec = 0; 2118 u32 ts_nsec = 0; 2119 2120 /* packet is available */ 2121 if (first_index == last_index) { 2122 /* single buffer packet */ 2123 struct sk_buff *new_skb = NULL; 2124 int packet_length; 2125 2126 new_skb = lan743x_rx_allocate_skb(rx); 2127 if (!new_skb) { 2128 /* failed to allocate next skb. 2129 * Memory is very low. 2130 * Drop this packet and reuse buffer. 2131 */ 2132 lan743x_rx_reuse_ring_element(rx, first_index); 2133 goto process_extension; 2134 } 2135 2136 buffer_info = &rx->buffer_info[first_index]; 2137 skb = buffer_info->skb; 2138 descriptor = &rx->ring_cpu_ptr[first_index]; 2139 2140 /* unmap from dma */ 2141 if (buffer_info->dma_ptr) { 2142 dma_unmap_single(&rx->adapter->pdev->dev, 2143 buffer_info->dma_ptr, 2144 buffer_info->buffer_length, 2145 DMA_FROM_DEVICE); 2146 buffer_info->dma_ptr = 0; 2147 buffer_info->buffer_length = 0; 2148 } 2149 buffer_info->skb = NULL; 2150 packet_length = RX_DESC_DATA0_FRAME_LENGTH_GET_ 2151 (descriptor->data0); 2152 skb_put(skb, packet_length - 4); 2153 skb->protocol = eth_type_trans(skb, 2154 rx->adapter->netdev); 2155 lan743x_rx_init_ring_element(rx, first_index, new_skb); 2156 } else { 2157 int index = first_index; 2158 2159 /* multi buffer packet not supported */ 2160 /* this should not happen since 2161 * buffers are allocated to be at least jumbo size 2162 */ 2163 2164 /* clean up buffers */ 2165 if (first_index <= last_index) { 2166 while ((index >= first_index) && 2167 (index <= last_index)) { 2168 lan743x_rx_reuse_ring_element(rx, 2169 index); 2170 index = lan743x_rx_next_index(rx, 2171 index); 2172 } 2173 } else { 2174 while ((index >= first_index) || 2175 (index <= last_index)) { 2176 lan743x_rx_reuse_ring_element(rx, 2177 index); 2178 index = lan743x_rx_next_index(rx, 2179 index); 2180 } 2181 } 2182 } 2183 2184 process_extension: 2185 if (extension_index >= 0) { 2186 descriptor = &rx->ring_cpu_ptr[extension_index]; 2187 buffer_info = &rx->buffer_info[extension_index]; 2188 2189 ts_sec = descriptor->data1; 2190 ts_nsec = (descriptor->data2 & 2191 RX_DESC_DATA2_TS_NS_MASK_); 2192 lan743x_rx_reuse_ring_element(rx, extension_index); 2193 real_last_index = extension_index; 2194 } 2195 2196 if (!skb) { 2197 result = RX_PROCESS_RESULT_PACKET_DROPPED; 2198 goto move_forward; 2199 } 2200 2201 if (extension_index < 0) 2202 goto pass_packet_to_os; 2203 hwtstamps = skb_hwtstamps(skb); 2204 if (hwtstamps) 2205 hwtstamps->hwtstamp = ktime_set(ts_sec, ts_nsec); 2206 2207 pass_packet_to_os: 2208 /* pass packet to OS */ 2209 napi_gro_receive(&rx->napi, skb); 2210 result = RX_PROCESS_RESULT_PACKET_RECEIVED; 2211 2212 move_forward: 2213 /* push tail and head forward */ 2214 rx->last_tail = real_last_index; 2215 rx->last_head = lan743x_rx_next_index(rx, real_last_index); 2216 } 2217 done: 2218 return result; 2219 } 2220 2221 static int lan743x_rx_napi_poll(struct napi_struct *napi, int weight) 2222 { 2223 struct lan743x_rx *rx = container_of(napi, struct lan743x_rx, napi); 2224 struct lan743x_adapter *adapter = rx->adapter; 2225 u32 rx_tail_flags = 0; 2226 int count; 2227 2228 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_W2C) { 2229 /* clear int status bit before reading packet */ 2230 lan743x_csr_write(adapter, DMAC_INT_STS, 2231 DMAC_INT_BIT_RXFRM_(rx->channel_number)); 2232 } 2233 count = 0; 2234 while (count < weight) { 2235 int rx_process_result = lan743x_rx_process_packet(rx); 2236 2237 if (rx_process_result == RX_PROCESS_RESULT_PACKET_RECEIVED) { 2238 count++; 2239 } else if (rx_process_result == 2240 RX_PROCESS_RESULT_NOTHING_TO_DO) { 2241 break; 2242 } else if (rx_process_result == 2243 RX_PROCESS_RESULT_PACKET_DROPPED) { 2244 continue; 2245 } 2246 } 2247 rx->frame_count += count; 2248 if (count == weight) 2249 goto done; 2250 2251 if (!napi_complete_done(napi, count)) 2252 goto done; 2253 2254 if (rx->vector_flags & LAN743X_VECTOR_FLAG_VECTOR_ENABLE_AUTO_SET) 2255 rx_tail_flags |= RX_TAIL_SET_TOP_INT_VEC_EN_; 2256 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_SET) { 2257 rx_tail_flags |= RX_TAIL_SET_TOP_INT_EN_; 2258 } else { 2259 lan743x_csr_write(adapter, INT_EN_SET, 2260 INT_BIT_DMA_RX_(rx->channel_number)); 2261 } 2262 2263 /* update RX_TAIL */ 2264 lan743x_csr_write(adapter, RX_TAIL(rx->channel_number), 2265 rx_tail_flags | rx->last_tail); 2266 done: 2267 return count; 2268 } 2269 2270 static void lan743x_rx_ring_cleanup(struct lan743x_rx *rx) 2271 { 2272 if (rx->buffer_info && rx->ring_cpu_ptr) { 2273 int index; 2274 2275 for (index = 0; index < rx->ring_size; index++) 2276 lan743x_rx_release_ring_element(rx, index); 2277 } 2278 2279 if (rx->head_cpu_ptr) { 2280 dma_free_coherent(&rx->adapter->pdev->dev, 2281 sizeof(*rx->head_cpu_ptr), rx->head_cpu_ptr, 2282 rx->head_dma_ptr); 2283 rx->head_cpu_ptr = NULL; 2284 rx->head_dma_ptr = 0; 2285 } 2286 2287 kfree(rx->buffer_info); 2288 rx->buffer_info = NULL; 2289 2290 if (rx->ring_cpu_ptr) { 2291 dma_free_coherent(&rx->adapter->pdev->dev, 2292 rx->ring_allocation_size, rx->ring_cpu_ptr, 2293 rx->ring_dma_ptr); 2294 rx->ring_allocation_size = 0; 2295 rx->ring_cpu_ptr = NULL; 2296 rx->ring_dma_ptr = 0; 2297 } 2298 2299 rx->ring_size = 0; 2300 rx->last_head = 0; 2301 } 2302 2303 static int lan743x_rx_ring_init(struct lan743x_rx *rx) 2304 { 2305 size_t ring_allocation_size = 0; 2306 dma_addr_t dma_ptr = 0; 2307 void *cpu_ptr = NULL; 2308 int ret = -ENOMEM; 2309 int index = 0; 2310 2311 rx->ring_size = LAN743X_RX_RING_SIZE; 2312 if (rx->ring_size <= 1) { 2313 ret = -EINVAL; 2314 goto cleanup; 2315 } 2316 if (rx->ring_size & ~RX_CFG_B_RX_RING_LEN_MASK_) { 2317 ret = -EINVAL; 2318 goto cleanup; 2319 } 2320 ring_allocation_size = ALIGN(rx->ring_size * 2321 sizeof(struct lan743x_rx_descriptor), 2322 PAGE_SIZE); 2323 dma_ptr = 0; 2324 cpu_ptr = dma_alloc_coherent(&rx->adapter->pdev->dev, 2325 ring_allocation_size, &dma_ptr, GFP_KERNEL); 2326 if (!cpu_ptr) { 2327 ret = -ENOMEM; 2328 goto cleanup; 2329 } 2330 rx->ring_allocation_size = ring_allocation_size; 2331 rx->ring_cpu_ptr = (struct lan743x_rx_descriptor *)cpu_ptr; 2332 rx->ring_dma_ptr = dma_ptr; 2333 2334 cpu_ptr = kcalloc(rx->ring_size, sizeof(*rx->buffer_info), 2335 GFP_KERNEL); 2336 if (!cpu_ptr) { 2337 ret = -ENOMEM; 2338 goto cleanup; 2339 } 2340 rx->buffer_info = (struct lan743x_rx_buffer_info *)cpu_ptr; 2341 dma_ptr = 0; 2342 cpu_ptr = dma_alloc_coherent(&rx->adapter->pdev->dev, 2343 sizeof(*rx->head_cpu_ptr), &dma_ptr, 2344 GFP_KERNEL); 2345 if (!cpu_ptr) { 2346 ret = -ENOMEM; 2347 goto cleanup; 2348 } 2349 2350 rx->head_cpu_ptr = cpu_ptr; 2351 rx->head_dma_ptr = dma_ptr; 2352 if (rx->head_dma_ptr & 0x3) { 2353 ret = -ENOMEM; 2354 goto cleanup; 2355 } 2356 2357 rx->last_head = 0; 2358 for (index = 0; index < rx->ring_size; index++) { 2359 struct sk_buff *new_skb = lan743x_rx_allocate_skb(rx); 2360 2361 ret = lan743x_rx_init_ring_element(rx, index, new_skb); 2362 if (ret) 2363 goto cleanup; 2364 } 2365 return 0; 2366 2367 cleanup: 2368 lan743x_rx_ring_cleanup(rx); 2369 return ret; 2370 } 2371 2372 static void lan743x_rx_close(struct lan743x_rx *rx) 2373 { 2374 struct lan743x_adapter *adapter = rx->adapter; 2375 2376 lan743x_csr_write(adapter, FCT_RX_CTL, 2377 FCT_RX_CTL_DIS_(rx->channel_number)); 2378 lan743x_csr_wait_for_bit(adapter, FCT_RX_CTL, 2379 FCT_RX_CTL_EN_(rx->channel_number), 2380 0, 1000, 20000, 100); 2381 2382 lan743x_csr_write(adapter, DMAC_CMD, 2383 DMAC_CMD_STOP_R_(rx->channel_number)); 2384 lan743x_dmac_rx_wait_till_stopped(adapter, rx->channel_number); 2385 2386 lan743x_csr_write(adapter, DMAC_INT_EN_CLR, 2387 DMAC_INT_BIT_RXFRM_(rx->channel_number)); 2388 lan743x_csr_write(adapter, INT_EN_CLR, 2389 INT_BIT_DMA_RX_(rx->channel_number)); 2390 napi_disable(&rx->napi); 2391 2392 netif_napi_del(&rx->napi); 2393 2394 lan743x_rx_ring_cleanup(rx); 2395 } 2396 2397 static int lan743x_rx_open(struct lan743x_rx *rx) 2398 { 2399 struct lan743x_adapter *adapter = rx->adapter; 2400 u32 data = 0; 2401 int ret; 2402 2403 rx->frame_count = 0; 2404 ret = lan743x_rx_ring_init(rx); 2405 if (ret) 2406 goto return_error; 2407 2408 netif_napi_add(adapter->netdev, 2409 &rx->napi, lan743x_rx_napi_poll, 2410 rx->ring_size - 1); 2411 2412 lan743x_csr_write(adapter, DMAC_CMD, 2413 DMAC_CMD_RX_SWR_(rx->channel_number)); 2414 lan743x_csr_wait_for_bit(adapter, DMAC_CMD, 2415 DMAC_CMD_RX_SWR_(rx->channel_number), 2416 0, 1000, 20000, 100); 2417 2418 /* set ring base address */ 2419 lan743x_csr_write(adapter, 2420 RX_BASE_ADDRH(rx->channel_number), 2421 DMA_ADDR_HIGH32(rx->ring_dma_ptr)); 2422 lan743x_csr_write(adapter, 2423 RX_BASE_ADDRL(rx->channel_number), 2424 DMA_ADDR_LOW32(rx->ring_dma_ptr)); 2425 2426 /* set rx write back address */ 2427 lan743x_csr_write(adapter, 2428 RX_HEAD_WRITEBACK_ADDRH(rx->channel_number), 2429 DMA_ADDR_HIGH32(rx->head_dma_ptr)); 2430 lan743x_csr_write(adapter, 2431 RX_HEAD_WRITEBACK_ADDRL(rx->channel_number), 2432 DMA_ADDR_LOW32(rx->head_dma_ptr)); 2433 data = RX_CFG_A_RX_HP_WB_EN_; 2434 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) { 2435 data |= (RX_CFG_A_RX_WB_ON_INT_TMR_ | 2436 RX_CFG_A_RX_WB_THRES_SET_(0x7) | 2437 RX_CFG_A_RX_PF_THRES_SET_(16) | 2438 RX_CFG_A_RX_PF_PRI_THRES_SET_(4)); 2439 } 2440 2441 /* set RX_CFG_A */ 2442 lan743x_csr_write(adapter, 2443 RX_CFG_A(rx->channel_number), data); 2444 2445 /* set RX_CFG_B */ 2446 data = lan743x_csr_read(adapter, RX_CFG_B(rx->channel_number)); 2447 data &= ~RX_CFG_B_RX_PAD_MASK_; 2448 if (!RX_HEAD_PADDING) 2449 data |= RX_CFG_B_RX_PAD_0_; 2450 else 2451 data |= RX_CFG_B_RX_PAD_2_; 2452 data &= ~RX_CFG_B_RX_RING_LEN_MASK_; 2453 data |= ((rx->ring_size) & RX_CFG_B_RX_RING_LEN_MASK_); 2454 data |= RX_CFG_B_TS_ALL_RX_; 2455 if (!(adapter->csr.flags & LAN743X_CSR_FLAG_IS_A0)) 2456 data |= RX_CFG_B_RDMABL_512_; 2457 2458 lan743x_csr_write(adapter, RX_CFG_B(rx->channel_number), data); 2459 rx->vector_flags = lan743x_intr_get_vector_flags(adapter, 2460 INT_BIT_DMA_RX_ 2461 (rx->channel_number)); 2462 2463 /* set RX_CFG_C */ 2464 data = 0; 2465 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_AUTO_CLEAR) 2466 data |= RX_CFG_C_RX_TOP_INT_EN_AUTO_CLR_; 2467 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_AUTO_CLEAR) 2468 data |= RX_CFG_C_RX_DMA_INT_STS_AUTO_CLR_; 2469 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_STATUS_R2C) 2470 data |= RX_CFG_C_RX_INT_STS_R2C_MODE_MASK_; 2471 if (rx->vector_flags & LAN743X_VECTOR_FLAG_SOURCE_ENABLE_R2C) 2472 data |= RX_CFG_C_RX_INT_EN_R2C_; 2473 lan743x_csr_write(adapter, RX_CFG_C(rx->channel_number), data); 2474 2475 rx->last_tail = ((u32)(rx->ring_size - 1)); 2476 lan743x_csr_write(adapter, RX_TAIL(rx->channel_number), 2477 rx->last_tail); 2478 rx->last_head = lan743x_csr_read(adapter, RX_HEAD(rx->channel_number)); 2479 if (rx->last_head) { 2480 ret = -EIO; 2481 goto napi_delete; 2482 } 2483 2484 napi_enable(&rx->napi); 2485 2486 lan743x_csr_write(adapter, INT_EN_SET, 2487 INT_BIT_DMA_RX_(rx->channel_number)); 2488 lan743x_csr_write(adapter, DMAC_INT_STS, 2489 DMAC_INT_BIT_RXFRM_(rx->channel_number)); 2490 lan743x_csr_write(adapter, DMAC_INT_EN_SET, 2491 DMAC_INT_BIT_RXFRM_(rx->channel_number)); 2492 lan743x_csr_write(adapter, DMAC_CMD, 2493 DMAC_CMD_START_R_(rx->channel_number)); 2494 2495 /* initialize fifo */ 2496 lan743x_csr_write(adapter, FCT_RX_CTL, 2497 FCT_RX_CTL_RESET_(rx->channel_number)); 2498 lan743x_csr_wait_for_bit(adapter, FCT_RX_CTL, 2499 FCT_RX_CTL_RESET_(rx->channel_number), 2500 0, 1000, 20000, 100); 2501 lan743x_csr_write(adapter, FCT_FLOW(rx->channel_number), 2502 FCT_FLOW_CTL_REQ_EN_ | 2503 FCT_FLOW_CTL_ON_THRESHOLD_SET_(0x2A) | 2504 FCT_FLOW_CTL_OFF_THRESHOLD_SET_(0xA)); 2505 2506 /* enable fifo */ 2507 lan743x_csr_write(adapter, FCT_RX_CTL, 2508 FCT_RX_CTL_EN_(rx->channel_number)); 2509 return 0; 2510 2511 napi_delete: 2512 netif_napi_del(&rx->napi); 2513 lan743x_rx_ring_cleanup(rx); 2514 2515 return_error: 2516 return ret; 2517 } 2518 2519 static int lan743x_netdev_close(struct net_device *netdev) 2520 { 2521 struct lan743x_adapter *adapter = netdev_priv(netdev); 2522 int index; 2523 2524 lan743x_tx_close(&adapter->tx[0]); 2525 2526 for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) 2527 lan743x_rx_close(&adapter->rx[index]); 2528 2529 lan743x_ptp_close(adapter); 2530 2531 lan743x_phy_close(adapter); 2532 2533 lan743x_mac_close(adapter); 2534 2535 lan743x_intr_close(adapter); 2536 2537 return 0; 2538 } 2539 2540 static int lan743x_netdev_open(struct net_device *netdev) 2541 { 2542 struct lan743x_adapter *adapter = netdev_priv(netdev); 2543 int index; 2544 int ret; 2545 2546 ret = lan743x_intr_open(adapter); 2547 if (ret) 2548 goto return_error; 2549 2550 ret = lan743x_mac_open(adapter); 2551 if (ret) 2552 goto close_intr; 2553 2554 ret = lan743x_phy_open(adapter); 2555 if (ret) 2556 goto close_mac; 2557 2558 ret = lan743x_ptp_open(adapter); 2559 if (ret) 2560 goto close_phy; 2561 2562 lan743x_rfe_open(adapter); 2563 2564 for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) { 2565 ret = lan743x_rx_open(&adapter->rx[index]); 2566 if (ret) 2567 goto close_rx; 2568 } 2569 2570 ret = lan743x_tx_open(&adapter->tx[0]); 2571 if (ret) 2572 goto close_rx; 2573 2574 return 0; 2575 2576 close_rx: 2577 for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) { 2578 if (adapter->rx[index].ring_cpu_ptr) 2579 lan743x_rx_close(&adapter->rx[index]); 2580 } 2581 lan743x_ptp_close(adapter); 2582 2583 close_phy: 2584 lan743x_phy_close(adapter); 2585 2586 close_mac: 2587 lan743x_mac_close(adapter); 2588 2589 close_intr: 2590 lan743x_intr_close(adapter); 2591 2592 return_error: 2593 netif_warn(adapter, ifup, adapter->netdev, 2594 "Error opening LAN743x\n"); 2595 return ret; 2596 } 2597 2598 static netdev_tx_t lan743x_netdev_xmit_frame(struct sk_buff *skb, 2599 struct net_device *netdev) 2600 { 2601 struct lan743x_adapter *adapter = netdev_priv(netdev); 2602 2603 return lan743x_tx_xmit_frame(&adapter->tx[0], skb); 2604 } 2605 2606 static int lan743x_netdev_ioctl(struct net_device *netdev, 2607 struct ifreq *ifr, int cmd) 2608 { 2609 if (!netif_running(netdev)) 2610 return -EINVAL; 2611 if (cmd == SIOCSHWTSTAMP) 2612 return lan743x_ptp_ioctl(netdev, ifr, cmd); 2613 return phy_mii_ioctl(netdev->phydev, ifr, cmd); 2614 } 2615 2616 static void lan743x_netdev_set_multicast(struct net_device *netdev) 2617 { 2618 struct lan743x_adapter *adapter = netdev_priv(netdev); 2619 2620 lan743x_rfe_set_multicast(adapter); 2621 } 2622 2623 static int lan743x_netdev_change_mtu(struct net_device *netdev, int new_mtu) 2624 { 2625 struct lan743x_adapter *adapter = netdev_priv(netdev); 2626 int ret = 0; 2627 2628 ret = lan743x_mac_set_mtu(adapter, new_mtu); 2629 if (!ret) 2630 netdev->mtu = new_mtu; 2631 return ret; 2632 } 2633 2634 static void lan743x_netdev_get_stats64(struct net_device *netdev, 2635 struct rtnl_link_stats64 *stats) 2636 { 2637 struct lan743x_adapter *adapter = netdev_priv(netdev); 2638 2639 stats->rx_packets = lan743x_csr_read(adapter, STAT_RX_TOTAL_FRAMES); 2640 stats->tx_packets = lan743x_csr_read(adapter, STAT_TX_TOTAL_FRAMES); 2641 stats->rx_bytes = lan743x_csr_read(adapter, 2642 STAT_RX_UNICAST_BYTE_COUNT) + 2643 lan743x_csr_read(adapter, 2644 STAT_RX_BROADCAST_BYTE_COUNT) + 2645 lan743x_csr_read(adapter, 2646 STAT_RX_MULTICAST_BYTE_COUNT); 2647 stats->tx_bytes = lan743x_csr_read(adapter, 2648 STAT_TX_UNICAST_BYTE_COUNT) + 2649 lan743x_csr_read(adapter, 2650 STAT_TX_BROADCAST_BYTE_COUNT) + 2651 lan743x_csr_read(adapter, 2652 STAT_TX_MULTICAST_BYTE_COUNT); 2653 stats->rx_errors = lan743x_csr_read(adapter, STAT_RX_FCS_ERRORS) + 2654 lan743x_csr_read(adapter, 2655 STAT_RX_ALIGNMENT_ERRORS) + 2656 lan743x_csr_read(adapter, STAT_RX_JABBER_ERRORS) + 2657 lan743x_csr_read(adapter, 2658 STAT_RX_UNDERSIZE_FRAME_ERRORS) + 2659 lan743x_csr_read(adapter, 2660 STAT_RX_OVERSIZE_FRAME_ERRORS); 2661 stats->tx_errors = lan743x_csr_read(adapter, STAT_TX_FCS_ERRORS) + 2662 lan743x_csr_read(adapter, 2663 STAT_TX_EXCESS_DEFERRAL_ERRORS) + 2664 lan743x_csr_read(adapter, STAT_TX_CARRIER_ERRORS); 2665 stats->rx_dropped = lan743x_csr_read(adapter, 2666 STAT_RX_DROPPED_FRAMES); 2667 stats->tx_dropped = lan743x_csr_read(adapter, 2668 STAT_TX_EXCESSIVE_COLLISION); 2669 stats->multicast = lan743x_csr_read(adapter, 2670 STAT_RX_MULTICAST_FRAMES) + 2671 lan743x_csr_read(adapter, 2672 STAT_TX_MULTICAST_FRAMES); 2673 stats->collisions = lan743x_csr_read(adapter, 2674 STAT_TX_SINGLE_COLLISIONS) + 2675 lan743x_csr_read(adapter, 2676 STAT_TX_MULTIPLE_COLLISIONS) + 2677 lan743x_csr_read(adapter, 2678 STAT_TX_LATE_COLLISIONS); 2679 } 2680 2681 static int lan743x_netdev_set_mac_address(struct net_device *netdev, 2682 void *addr) 2683 { 2684 struct lan743x_adapter *adapter = netdev_priv(netdev); 2685 struct sockaddr *sock_addr = addr; 2686 int ret; 2687 2688 ret = eth_prepare_mac_addr_change(netdev, sock_addr); 2689 if (ret) 2690 return ret; 2691 ether_addr_copy(netdev->dev_addr, sock_addr->sa_data); 2692 lan743x_mac_set_address(adapter, sock_addr->sa_data); 2693 lan743x_rfe_update_mac_address(adapter); 2694 return 0; 2695 } 2696 2697 static const struct net_device_ops lan743x_netdev_ops = { 2698 .ndo_open = lan743x_netdev_open, 2699 .ndo_stop = lan743x_netdev_close, 2700 .ndo_start_xmit = lan743x_netdev_xmit_frame, 2701 .ndo_do_ioctl = lan743x_netdev_ioctl, 2702 .ndo_set_rx_mode = lan743x_netdev_set_multicast, 2703 .ndo_change_mtu = lan743x_netdev_change_mtu, 2704 .ndo_get_stats64 = lan743x_netdev_get_stats64, 2705 .ndo_set_mac_address = lan743x_netdev_set_mac_address, 2706 }; 2707 2708 static void lan743x_hardware_cleanup(struct lan743x_adapter *adapter) 2709 { 2710 lan743x_csr_write(adapter, INT_EN_CLR, 0xFFFFFFFF); 2711 } 2712 2713 static void lan743x_mdiobus_cleanup(struct lan743x_adapter *adapter) 2714 { 2715 mdiobus_unregister(adapter->mdiobus); 2716 } 2717 2718 static void lan743x_full_cleanup(struct lan743x_adapter *adapter) 2719 { 2720 unregister_netdev(adapter->netdev); 2721 2722 lan743x_mdiobus_cleanup(adapter); 2723 lan743x_hardware_cleanup(adapter); 2724 lan743x_pci_cleanup(adapter); 2725 } 2726 2727 static int lan743x_hardware_init(struct lan743x_adapter *adapter, 2728 struct pci_dev *pdev) 2729 { 2730 struct lan743x_tx *tx; 2731 int index; 2732 int ret; 2733 2734 adapter->intr.irq = adapter->pdev->irq; 2735 lan743x_csr_write(adapter, INT_EN_CLR, 0xFFFFFFFF); 2736 mutex_init(&adapter->dp_lock); 2737 2738 ret = lan743x_gpio_init(adapter); 2739 if (ret) 2740 return ret; 2741 2742 ret = lan743x_mac_init(adapter); 2743 if (ret) 2744 return ret; 2745 2746 ret = lan743x_phy_init(adapter); 2747 if (ret) 2748 return ret; 2749 2750 ret = lan743x_ptp_init(adapter); 2751 if (ret) 2752 return ret; 2753 2754 lan743x_rfe_update_mac_address(adapter); 2755 2756 ret = lan743x_dmac_init(adapter); 2757 if (ret) 2758 return ret; 2759 2760 for (index = 0; index < LAN743X_USED_RX_CHANNELS; index++) { 2761 adapter->rx[index].adapter = adapter; 2762 adapter->rx[index].channel_number = index; 2763 } 2764 2765 tx = &adapter->tx[0]; 2766 tx->adapter = adapter; 2767 tx->channel_number = 0; 2768 spin_lock_init(&tx->ring_lock); 2769 return 0; 2770 } 2771 2772 static int lan743x_mdiobus_init(struct lan743x_adapter *adapter) 2773 { 2774 int ret; 2775 2776 adapter->mdiobus = devm_mdiobus_alloc(&adapter->pdev->dev); 2777 if (!(adapter->mdiobus)) { 2778 ret = -ENOMEM; 2779 goto return_error; 2780 } 2781 2782 adapter->mdiobus->priv = (void *)adapter; 2783 adapter->mdiobus->read = lan743x_mdiobus_read; 2784 adapter->mdiobus->write = lan743x_mdiobus_write; 2785 adapter->mdiobus->name = "lan743x-mdiobus"; 2786 snprintf(adapter->mdiobus->id, MII_BUS_ID_SIZE, 2787 "pci-%s", pci_name(adapter->pdev)); 2788 2789 if ((adapter->csr.id_rev & ID_REV_ID_MASK_) == ID_REV_ID_LAN7430_) 2790 /* LAN7430 uses internal phy at address 1 */ 2791 adapter->mdiobus->phy_mask = ~(u32)BIT(1); 2792 2793 /* register mdiobus */ 2794 ret = mdiobus_register(adapter->mdiobus); 2795 if (ret < 0) 2796 goto return_error; 2797 return 0; 2798 2799 return_error: 2800 return ret; 2801 } 2802 2803 /* lan743x_pcidev_probe - Device Initialization Routine 2804 * @pdev: PCI device information struct 2805 * @id: entry in lan743x_pci_tbl 2806 * 2807 * Returns 0 on success, negative on failure 2808 * 2809 * initializes an adapter identified by a pci_dev structure. 2810 * The OS initialization, configuring of the adapter private structure, 2811 * and a hardware reset occur. 2812 **/ 2813 static int lan743x_pcidev_probe(struct pci_dev *pdev, 2814 const struct pci_device_id *id) 2815 { 2816 struct lan743x_adapter *adapter = NULL; 2817 struct net_device *netdev = NULL; 2818 const void *mac_addr; 2819 int ret = -ENODEV; 2820 2821 netdev = devm_alloc_etherdev(&pdev->dev, 2822 sizeof(struct lan743x_adapter)); 2823 if (!netdev) 2824 goto return_error; 2825 2826 SET_NETDEV_DEV(netdev, &pdev->dev); 2827 pci_set_drvdata(pdev, netdev); 2828 adapter = netdev_priv(netdev); 2829 adapter->netdev = netdev; 2830 adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE | 2831 NETIF_MSG_LINK | NETIF_MSG_IFUP | 2832 NETIF_MSG_IFDOWN | NETIF_MSG_TX_QUEUED; 2833 netdev->max_mtu = LAN743X_MAX_FRAME_SIZE; 2834 2835 mac_addr = of_get_mac_address(pdev->dev.of_node); 2836 if (!IS_ERR(mac_addr)) 2837 ether_addr_copy(adapter->mac_address, mac_addr); 2838 2839 ret = lan743x_pci_init(adapter, pdev); 2840 if (ret) 2841 goto return_error; 2842 2843 ret = lan743x_csr_init(adapter); 2844 if (ret) 2845 goto cleanup_pci; 2846 2847 ret = lan743x_hardware_init(adapter, pdev); 2848 if (ret) 2849 goto cleanup_pci; 2850 2851 ret = lan743x_mdiobus_init(adapter); 2852 if (ret) 2853 goto cleanup_hardware; 2854 2855 adapter->netdev->netdev_ops = &lan743x_netdev_ops; 2856 adapter->netdev->ethtool_ops = &lan743x_ethtool_ops; 2857 adapter->netdev->features = NETIF_F_SG | NETIF_F_TSO | NETIF_F_HW_CSUM; 2858 adapter->netdev->hw_features = adapter->netdev->features; 2859 2860 /* carrier off reporting is important to ethtool even BEFORE open */ 2861 netif_carrier_off(netdev); 2862 2863 ret = register_netdev(adapter->netdev); 2864 if (ret < 0) 2865 goto cleanup_mdiobus; 2866 return 0; 2867 2868 cleanup_mdiobus: 2869 lan743x_mdiobus_cleanup(adapter); 2870 2871 cleanup_hardware: 2872 lan743x_hardware_cleanup(adapter); 2873 2874 cleanup_pci: 2875 lan743x_pci_cleanup(adapter); 2876 2877 return_error: 2878 pr_warn("Initialization failed\n"); 2879 return ret; 2880 } 2881 2882 /** 2883 * lan743x_pcidev_remove - Device Removal Routine 2884 * @pdev: PCI device information struct 2885 * 2886 * this is called by the PCI subsystem to alert the driver 2887 * that it should release a PCI device. This could be caused by a 2888 * Hot-Plug event, or because the driver is going to be removed from 2889 * memory. 2890 **/ 2891 static void lan743x_pcidev_remove(struct pci_dev *pdev) 2892 { 2893 struct net_device *netdev = pci_get_drvdata(pdev); 2894 struct lan743x_adapter *adapter = netdev_priv(netdev); 2895 2896 lan743x_full_cleanup(adapter); 2897 } 2898 2899 static void lan743x_pcidev_shutdown(struct pci_dev *pdev) 2900 { 2901 struct net_device *netdev = pci_get_drvdata(pdev); 2902 struct lan743x_adapter *adapter = netdev_priv(netdev); 2903 2904 rtnl_lock(); 2905 netif_device_detach(netdev); 2906 2907 /* close netdev when netdev is at running state. 2908 * For instance, it is true when system goes to sleep by pm-suspend 2909 * However, it is false when system goes to sleep by suspend GUI menu 2910 */ 2911 if (netif_running(netdev)) 2912 lan743x_netdev_close(netdev); 2913 rtnl_unlock(); 2914 2915 #ifdef CONFIG_PM 2916 pci_save_state(pdev); 2917 #endif 2918 2919 /* clean up lan743x portion */ 2920 lan743x_hardware_cleanup(adapter); 2921 } 2922 2923 #ifdef CONFIG_PM_SLEEP 2924 static u16 lan743x_pm_wakeframe_crc16(const u8 *buf, int len) 2925 { 2926 return bitrev16(crc16(0xFFFF, buf, len)); 2927 } 2928 2929 static void lan743x_pm_set_wol(struct lan743x_adapter *adapter) 2930 { 2931 const u8 ipv4_multicast[3] = { 0x01, 0x00, 0x5E }; 2932 const u8 ipv6_multicast[3] = { 0x33, 0x33 }; 2933 const u8 arp_type[2] = { 0x08, 0x06 }; 2934 int mask_index; 2935 u32 pmtctl; 2936 u32 wucsr; 2937 u32 macrx; 2938 u16 crc; 2939 2940 for (mask_index = 0; mask_index < MAC_NUM_OF_WUF_CFG; mask_index++) 2941 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index), 0); 2942 2943 /* clear wake settings */ 2944 pmtctl = lan743x_csr_read(adapter, PMT_CTL); 2945 pmtctl |= PMT_CTL_WUPS_MASK_; 2946 pmtctl &= ~(PMT_CTL_GPIO_WAKEUP_EN_ | PMT_CTL_EEE_WAKEUP_EN_ | 2947 PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_ | 2948 PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_ | PMT_CTL_ETH_PHY_WAKE_EN_); 2949 2950 macrx = lan743x_csr_read(adapter, MAC_RX); 2951 2952 wucsr = 0; 2953 mask_index = 0; 2954 2955 pmtctl |= PMT_CTL_ETH_PHY_D3_COLD_OVR_ | PMT_CTL_ETH_PHY_D3_OVR_; 2956 2957 if (adapter->wolopts & WAKE_PHY) { 2958 pmtctl |= PMT_CTL_ETH_PHY_EDPD_PLL_CTL_; 2959 pmtctl |= PMT_CTL_ETH_PHY_WAKE_EN_; 2960 } 2961 if (adapter->wolopts & WAKE_MAGIC) { 2962 wucsr |= MAC_WUCSR_MPEN_; 2963 macrx |= MAC_RX_RXEN_; 2964 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_; 2965 } 2966 if (adapter->wolopts & WAKE_UCAST) { 2967 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_PFDA_EN_; 2968 macrx |= MAC_RX_RXEN_; 2969 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_; 2970 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_; 2971 } 2972 if (adapter->wolopts & WAKE_BCAST) { 2973 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_BCST_EN_; 2974 macrx |= MAC_RX_RXEN_; 2975 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_; 2976 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_; 2977 } 2978 if (adapter->wolopts & WAKE_MCAST) { 2979 /* IPv4 multicast */ 2980 crc = lan743x_pm_wakeframe_crc16(ipv4_multicast, 3); 2981 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index), 2982 MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_MCAST_ | 2983 (0 << MAC_WUF_CFG_OFFSET_SHIFT_) | 2984 (crc & MAC_WUF_CFG_CRC16_MASK_)); 2985 lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 7); 2986 lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0); 2987 lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0); 2988 lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0); 2989 mask_index++; 2990 2991 /* IPv6 multicast */ 2992 crc = lan743x_pm_wakeframe_crc16(ipv6_multicast, 2); 2993 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index), 2994 MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_MCAST_ | 2995 (0 << MAC_WUF_CFG_OFFSET_SHIFT_) | 2996 (crc & MAC_WUF_CFG_CRC16_MASK_)); 2997 lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 3); 2998 lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0); 2999 lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0); 3000 lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0); 3001 mask_index++; 3002 3003 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_WAKE_EN_; 3004 macrx |= MAC_RX_RXEN_; 3005 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_; 3006 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_; 3007 } 3008 if (adapter->wolopts & WAKE_ARP) { 3009 /* set MAC_WUF_CFG & WUF_MASK 3010 * for packettype (offset 12,13) = ARP (0x0806) 3011 */ 3012 crc = lan743x_pm_wakeframe_crc16(arp_type, 2); 3013 lan743x_csr_write(adapter, MAC_WUF_CFG(mask_index), 3014 MAC_WUF_CFG_EN_ | MAC_WUF_CFG_TYPE_ALL_ | 3015 (0 << MAC_WUF_CFG_OFFSET_SHIFT_) | 3016 (crc & MAC_WUF_CFG_CRC16_MASK_)); 3017 lan743x_csr_write(adapter, MAC_WUF_MASK0(mask_index), 0x3000); 3018 lan743x_csr_write(adapter, MAC_WUF_MASK1(mask_index), 0); 3019 lan743x_csr_write(adapter, MAC_WUF_MASK2(mask_index), 0); 3020 lan743x_csr_write(adapter, MAC_WUF_MASK3(mask_index), 0); 3021 mask_index++; 3022 3023 wucsr |= MAC_WUCSR_RFE_WAKE_EN_ | MAC_WUCSR_WAKE_EN_; 3024 macrx |= MAC_RX_RXEN_; 3025 pmtctl |= PMT_CTL_WOL_EN_ | PMT_CTL_MAC_D3_RX_CLK_OVR_; 3026 pmtctl |= PMT_CTL_RX_FCT_RFE_D3_CLK_OVR_; 3027 } 3028 3029 lan743x_csr_write(adapter, MAC_WUCSR, wucsr); 3030 lan743x_csr_write(adapter, PMT_CTL, pmtctl); 3031 lan743x_csr_write(adapter, MAC_RX, macrx); 3032 } 3033 3034 static int lan743x_pm_suspend(struct device *dev) 3035 { 3036 struct pci_dev *pdev = to_pci_dev(dev); 3037 struct net_device *netdev = pci_get_drvdata(pdev); 3038 struct lan743x_adapter *adapter = netdev_priv(netdev); 3039 3040 lan743x_pcidev_shutdown(pdev); 3041 3042 /* clear all wakes */ 3043 lan743x_csr_write(adapter, MAC_WUCSR, 0); 3044 lan743x_csr_write(adapter, MAC_WUCSR2, 0); 3045 lan743x_csr_write(adapter, MAC_WK_SRC, 0xFFFFFFFF); 3046 3047 if (adapter->wolopts) 3048 lan743x_pm_set_wol(adapter); 3049 3050 /* Host sets PME_En, put D3hot */ 3051 return pci_prepare_to_sleep(pdev);; 3052 } 3053 3054 static int lan743x_pm_resume(struct device *dev) 3055 { 3056 struct pci_dev *pdev = to_pci_dev(dev); 3057 struct net_device *netdev = pci_get_drvdata(pdev); 3058 struct lan743x_adapter *adapter = netdev_priv(netdev); 3059 int ret; 3060 3061 pci_set_power_state(pdev, PCI_D0); 3062 pci_restore_state(pdev); 3063 pci_save_state(pdev); 3064 3065 ret = lan743x_hardware_init(adapter, pdev); 3066 if (ret) { 3067 netif_err(adapter, probe, adapter->netdev, 3068 "lan743x_hardware_init returned %d\n", ret); 3069 } 3070 3071 /* open netdev when netdev is at running state while resume. 3072 * For instance, it is true when system wakesup after pm-suspend 3073 * However, it is false when system wakes up after suspend GUI menu 3074 */ 3075 if (netif_running(netdev)) 3076 lan743x_netdev_open(netdev); 3077 3078 netif_device_attach(netdev); 3079 3080 return 0; 3081 } 3082 3083 static const struct dev_pm_ops lan743x_pm_ops = { 3084 SET_SYSTEM_SLEEP_PM_OPS(lan743x_pm_suspend, lan743x_pm_resume) 3085 }; 3086 #endif /* CONFIG_PM_SLEEP */ 3087 3088 static const struct pci_device_id lan743x_pcidev_tbl[] = { 3089 { PCI_DEVICE(PCI_VENDOR_ID_SMSC, PCI_DEVICE_ID_SMSC_LAN7430) }, 3090 { PCI_DEVICE(PCI_VENDOR_ID_SMSC, PCI_DEVICE_ID_SMSC_LAN7431) }, 3091 { 0, } 3092 }; 3093 3094 MODULE_DEVICE_TABLE(pci, lan743x_pcidev_tbl); 3095 3096 static struct pci_driver lan743x_pcidev_driver = { 3097 .name = DRIVER_NAME, 3098 .id_table = lan743x_pcidev_tbl, 3099 .probe = lan743x_pcidev_probe, 3100 .remove = lan743x_pcidev_remove, 3101 #ifdef CONFIG_PM_SLEEP 3102 .driver.pm = &lan743x_pm_ops, 3103 #endif 3104 .shutdown = lan743x_pcidev_shutdown, 3105 }; 3106 3107 module_pci_driver(lan743x_pcidev_driver); 3108 3109 MODULE_AUTHOR(DRIVER_AUTHOR); 3110 MODULE_DESCRIPTION(DRIVER_DESC); 3111 MODULE_LICENSE("GPL"); 3112