1 // SPDX-License-Identifier: GPL-2.0 2 /* Marvell Octeon EP (EndPoint) Ethernet Driver 3 * 4 * Copyright (C) 2020 Marvell. 5 * 6 */ 7 8 #include <linux/types.h> 9 #include <linux/module.h> 10 #include <linux/pci.h> 11 #include <linux/aer.h> 12 #include <linux/netdevice.h> 13 #include <linux/etherdevice.h> 14 #include <linux/rtnetlink.h> 15 #include <linux/vmalloc.h> 16 17 #include "octep_config.h" 18 #include "octep_main.h" 19 #include "octep_ctrl_net.h" 20 21 struct workqueue_struct *octep_wq; 22 23 /* Supported Devices */ 24 static const struct pci_device_id octep_pci_id_tbl[] = { 25 {PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_PCI_DEVICE_ID_CN93_PF)}, 26 {0, }, 27 }; 28 MODULE_DEVICE_TABLE(pci, octep_pci_id_tbl); 29 30 MODULE_AUTHOR("Veerasenareddy Burru <vburru@marvell.com>"); 31 MODULE_DESCRIPTION(OCTEP_DRV_STRING); 32 MODULE_LICENSE("GPL"); 33 34 /** 35 * octep_alloc_ioq_vectors() - Allocate Tx/Rx Queue interrupt info. 36 * 37 * @oct: Octeon device private data structure. 38 * 39 * Allocate resources to hold per Tx/Rx queue interrupt info. 40 * This is the information passed to interrupt handler, from which napi poll 41 * is scheduled and includes quick access to private data of Tx/Rx queue 42 * corresponding to the interrupt being handled. 43 * 44 * Return: 0, on successful allocation of resources for all queue interrupts. 45 * -1, if failed to allocate any resource. 46 */ 47 static int octep_alloc_ioq_vectors(struct octep_device *oct) 48 { 49 int i; 50 struct octep_ioq_vector *ioq_vector; 51 52 for (i = 0; i < oct->num_oqs; i++) { 53 oct->ioq_vector[i] = vzalloc(sizeof(*oct->ioq_vector[i])); 54 if (!oct->ioq_vector[i]) 55 goto free_ioq_vector; 56 57 ioq_vector = oct->ioq_vector[i]; 58 ioq_vector->iq = oct->iq[i]; 59 ioq_vector->oq = oct->oq[i]; 60 ioq_vector->octep_dev = oct; 61 } 62 63 dev_info(&oct->pdev->dev, "Allocated %d IOQ vectors\n", oct->num_oqs); 64 return 0; 65 66 free_ioq_vector: 67 while (i) { 68 i--; 69 vfree(oct->ioq_vector[i]); 70 oct->ioq_vector[i] = NULL; 71 } 72 return -1; 73 } 74 75 /** 76 * octep_free_ioq_vectors() - Free Tx/Rx Queue interrupt vector info. 77 * 78 * @oct: Octeon device private data structure. 79 */ 80 static void octep_free_ioq_vectors(struct octep_device *oct) 81 { 82 int i; 83 84 for (i = 0; i < oct->num_oqs; i++) { 85 if (oct->ioq_vector[i]) { 86 vfree(oct->ioq_vector[i]); 87 oct->ioq_vector[i] = NULL; 88 } 89 } 90 netdev_info(oct->netdev, "Freed IOQ Vectors\n"); 91 } 92 93 /** 94 * octep_enable_msix_range() - enable MSI-x interrupts. 95 * 96 * @oct: Octeon device private data structure. 97 * 98 * Allocate and enable all MSI-x interrupts (queue and non-queue interrupts) 99 * for the Octeon device. 100 * 101 * Return: 0, on successfully enabling all MSI-x interrupts. 102 * -1, if failed to enable any MSI-x interrupt. 103 */ 104 static int octep_enable_msix_range(struct octep_device *oct) 105 { 106 int num_msix, msix_allocated; 107 int i; 108 109 /* Generic interrupts apart from input/output queues */ 110 num_msix = oct->num_oqs + CFG_GET_NON_IOQ_MSIX(oct->conf); 111 oct->msix_entries = kcalloc(num_msix, 112 sizeof(struct msix_entry), GFP_KERNEL); 113 if (!oct->msix_entries) 114 goto msix_alloc_err; 115 116 for (i = 0; i < num_msix; i++) 117 oct->msix_entries[i].entry = i; 118 119 msix_allocated = pci_enable_msix_range(oct->pdev, oct->msix_entries, 120 num_msix, num_msix); 121 if (msix_allocated != num_msix) { 122 dev_err(&oct->pdev->dev, 123 "Failed to enable %d msix irqs; got only %d\n", 124 num_msix, msix_allocated); 125 goto enable_msix_err; 126 } 127 oct->num_irqs = msix_allocated; 128 dev_info(&oct->pdev->dev, "MSI-X enabled successfully\n"); 129 130 return 0; 131 132 enable_msix_err: 133 if (msix_allocated > 0) 134 pci_disable_msix(oct->pdev); 135 kfree(oct->msix_entries); 136 oct->msix_entries = NULL; 137 msix_alloc_err: 138 return -1; 139 } 140 141 /** 142 * octep_disable_msix() - disable MSI-x interrupts. 143 * 144 * @oct: Octeon device private data structure. 145 * 146 * Disable MSI-x on the Octeon device. 147 */ 148 static void octep_disable_msix(struct octep_device *oct) 149 { 150 pci_disable_msix(oct->pdev); 151 kfree(oct->msix_entries); 152 oct->msix_entries = NULL; 153 dev_info(&oct->pdev->dev, "Disabled MSI-X\n"); 154 } 155 156 /** 157 * octep_non_ioq_intr_handler() - common handler for all generic interrupts. 158 * 159 * @irq: Interrupt number. 160 * @data: interrupt data. 161 * 162 * this is common handler for all non-queue (generic) interrupts. 163 */ 164 static irqreturn_t octep_non_ioq_intr_handler(int irq, void *data) 165 { 166 struct octep_device *oct = data; 167 168 return oct->hw_ops.non_ioq_intr_handler(oct); 169 } 170 171 /** 172 * octep_ioq_intr_handler() - handler for all Tx/Rx queue interrupts. 173 * 174 * @irq: Interrupt number. 175 * @data: interrupt data contains pointers to Tx/Rx queue private data 176 * and correspong NAPI context. 177 * 178 * this is common handler for all non-queue (generic) interrupts. 179 */ 180 static irqreturn_t octep_ioq_intr_handler(int irq, void *data) 181 { 182 struct octep_ioq_vector *ioq_vector = data; 183 struct octep_device *oct = ioq_vector->octep_dev; 184 185 return oct->hw_ops.ioq_intr_handler(ioq_vector); 186 } 187 188 /** 189 * octep_request_irqs() - Register interrupt handlers. 190 * 191 * @oct: Octeon device private data structure. 192 * 193 * Register handlers for all queue and non-queue interrupts. 194 * 195 * Return: 0, on successful registration of all interrupt handlers. 196 * -1, on any error. 197 */ 198 static int octep_request_irqs(struct octep_device *oct) 199 { 200 struct net_device *netdev = oct->netdev; 201 struct octep_ioq_vector *ioq_vector; 202 struct msix_entry *msix_entry; 203 char **non_ioq_msix_names; 204 int num_non_ioq_msix; 205 int ret, i; 206 207 num_non_ioq_msix = CFG_GET_NON_IOQ_MSIX(oct->conf); 208 non_ioq_msix_names = CFG_GET_NON_IOQ_MSIX_NAMES(oct->conf); 209 210 oct->non_ioq_irq_names = kcalloc(num_non_ioq_msix, 211 OCTEP_MSIX_NAME_SIZE, GFP_KERNEL); 212 if (!oct->non_ioq_irq_names) 213 goto alloc_err; 214 215 /* First few MSI-X interrupts are non-queue interrupts */ 216 for (i = 0; i < num_non_ioq_msix; i++) { 217 char *irq_name; 218 219 irq_name = &oct->non_ioq_irq_names[i * OCTEP_MSIX_NAME_SIZE]; 220 msix_entry = &oct->msix_entries[i]; 221 222 snprintf(irq_name, OCTEP_MSIX_NAME_SIZE, 223 "%s-%s", netdev->name, non_ioq_msix_names[i]); 224 ret = request_irq(msix_entry->vector, 225 octep_non_ioq_intr_handler, 0, 226 irq_name, oct); 227 if (ret) { 228 netdev_err(netdev, 229 "request_irq failed for %s; err=%d", 230 irq_name, ret); 231 goto non_ioq_irq_err; 232 } 233 } 234 235 /* Request IRQs for Tx/Rx queues */ 236 for (i = 0; i < oct->num_oqs; i++) { 237 ioq_vector = oct->ioq_vector[i]; 238 msix_entry = &oct->msix_entries[i + num_non_ioq_msix]; 239 240 snprintf(ioq_vector->name, sizeof(ioq_vector->name), 241 "%s-q%d", netdev->name, i); 242 ret = request_irq(msix_entry->vector, 243 octep_ioq_intr_handler, 0, 244 ioq_vector->name, ioq_vector); 245 if (ret) { 246 netdev_err(netdev, 247 "request_irq failed for Q-%d; err=%d", 248 i, ret); 249 goto ioq_irq_err; 250 } 251 252 cpumask_set_cpu(i % num_online_cpus(), 253 &ioq_vector->affinity_mask); 254 irq_set_affinity_hint(msix_entry->vector, 255 &ioq_vector->affinity_mask); 256 } 257 258 return 0; 259 ioq_irq_err: 260 while (i > num_non_ioq_msix) { 261 --i; 262 irq_set_affinity_hint(oct->msix_entries[i].vector, NULL); 263 free_irq(oct->msix_entries[i].vector, oct->ioq_vector[i]); 264 } 265 non_ioq_irq_err: 266 while (i) { 267 --i; 268 free_irq(oct->msix_entries[i].vector, oct); 269 } 270 alloc_err: 271 return -1; 272 } 273 274 /** 275 * octep_free_irqs() - free all registered interrupts. 276 * 277 * @oct: Octeon device private data structure. 278 * 279 * Free all queue and non-queue interrupts of the Octeon device. 280 */ 281 static void octep_free_irqs(struct octep_device *oct) 282 { 283 int i; 284 285 /* First few MSI-X interrupts are non queue interrupts; free them */ 286 for (i = 0; i < CFG_GET_NON_IOQ_MSIX(oct->conf); i++) 287 free_irq(oct->msix_entries[i].vector, oct); 288 kfree(oct->non_ioq_irq_names); 289 290 /* Free IRQs for Input/Output (Tx/Rx) queues */ 291 for (i = CFG_GET_NON_IOQ_MSIX(oct->conf); i < oct->num_irqs; i++) { 292 irq_set_affinity_hint(oct->msix_entries[i].vector, NULL); 293 free_irq(oct->msix_entries[i].vector, 294 oct->ioq_vector[i - CFG_GET_NON_IOQ_MSIX(oct->conf)]); 295 } 296 netdev_info(oct->netdev, "IRQs freed\n"); 297 } 298 299 /** 300 * octep_setup_irqs() - setup interrupts for the Octeon device. 301 * 302 * @oct: Octeon device private data structure. 303 * 304 * Allocate data structures to hold per interrupt information, allocate/enable 305 * MSI-x interrupt and register interrupt handlers. 306 * 307 * Return: 0, on successful allocation and registration of all interrupts. 308 * -1, on any error. 309 */ 310 static int octep_setup_irqs(struct octep_device *oct) 311 { 312 if (octep_alloc_ioq_vectors(oct)) 313 goto ioq_vector_err; 314 315 if (octep_enable_msix_range(oct)) 316 goto enable_msix_err; 317 318 if (octep_request_irqs(oct)) 319 goto request_irq_err; 320 321 return 0; 322 323 request_irq_err: 324 octep_disable_msix(oct); 325 enable_msix_err: 326 octep_free_ioq_vectors(oct); 327 ioq_vector_err: 328 return -1; 329 } 330 331 /** 332 * octep_clean_irqs() - free all interrupts and its resources. 333 * 334 * @oct: Octeon device private data structure. 335 */ 336 static void octep_clean_irqs(struct octep_device *oct) 337 { 338 octep_free_irqs(oct); 339 octep_disable_msix(oct); 340 octep_free_ioq_vectors(oct); 341 } 342 343 /** 344 * octep_enable_ioq_irq() - Enable MSI-x interrupt of a Tx/Rx queue. 345 * 346 * @iq: Octeon Tx queue data structure. 347 * @oq: Octeon Rx queue data structure. 348 */ 349 static void octep_enable_ioq_irq(struct octep_iq *iq, struct octep_oq *oq) 350 { 351 u32 pkts_pend = oq->pkts_pending; 352 353 netdev_dbg(iq->netdev, "enabling intr for Q-%u\n", iq->q_no); 354 if (iq->pkts_processed) { 355 writel(iq->pkts_processed, iq->inst_cnt_reg); 356 iq->pkt_in_done -= iq->pkts_processed; 357 iq->pkts_processed = 0; 358 } 359 if (oq->last_pkt_count - pkts_pend) { 360 writel(oq->last_pkt_count - pkts_pend, oq->pkts_sent_reg); 361 oq->last_pkt_count = pkts_pend; 362 } 363 364 /* Flush the previous wrties before writing to RESEND bit */ 365 wmb(); 366 writeq(1UL << OCTEP_OQ_INTR_RESEND_BIT, oq->pkts_sent_reg); 367 writeq(1UL << OCTEP_IQ_INTR_RESEND_BIT, iq->inst_cnt_reg); 368 } 369 370 /** 371 * octep_napi_poll() - NAPI poll function for Tx/Rx. 372 * 373 * @napi: pointer to napi context. 374 * @budget: max number of packets to be processed in single invocation. 375 */ 376 static int octep_napi_poll(struct napi_struct *napi, int budget) 377 { 378 struct octep_ioq_vector *ioq_vector = 379 container_of(napi, struct octep_ioq_vector, napi); 380 u32 tx_pending, rx_done; 381 382 tx_pending = octep_iq_process_completions(ioq_vector->iq, budget); 383 rx_done = octep_oq_process_rx(ioq_vector->oq, budget); 384 385 /* need more polling if tx completion processing is still pending or 386 * processed at least 'budget' number of rx packets. 387 */ 388 if (tx_pending || rx_done >= budget) 389 return budget; 390 391 napi_complete(napi); 392 octep_enable_ioq_irq(ioq_vector->iq, ioq_vector->oq); 393 return rx_done; 394 } 395 396 /** 397 * octep_napi_add() - Add NAPI poll for all Tx/Rx queues. 398 * 399 * @oct: Octeon device private data structure. 400 */ 401 static void octep_napi_add(struct octep_device *oct) 402 { 403 int i; 404 405 for (i = 0; i < oct->num_oqs; i++) { 406 netdev_dbg(oct->netdev, "Adding NAPI on Q-%d\n", i); 407 netif_napi_add(oct->netdev, &oct->ioq_vector[i]->napi, 408 octep_napi_poll, 64); 409 oct->oq[i]->napi = &oct->ioq_vector[i]->napi; 410 } 411 } 412 413 /** 414 * octep_napi_delete() - delete NAPI poll callback for all Tx/Rx queues. 415 * 416 * @oct: Octeon device private data structure. 417 */ 418 static void octep_napi_delete(struct octep_device *oct) 419 { 420 int i; 421 422 for (i = 0; i < oct->num_oqs; i++) { 423 netdev_dbg(oct->netdev, "Deleting NAPI on Q-%d\n", i); 424 netif_napi_del(&oct->ioq_vector[i]->napi); 425 oct->oq[i]->napi = NULL; 426 } 427 } 428 429 /** 430 * octep_napi_enable() - enable NAPI for all Tx/Rx queues. 431 * 432 * @oct: Octeon device private data structure. 433 */ 434 static void octep_napi_enable(struct octep_device *oct) 435 { 436 int i; 437 438 for (i = 0; i < oct->num_oqs; i++) { 439 netdev_dbg(oct->netdev, "Enabling NAPI on Q-%d\n", i); 440 napi_enable(&oct->ioq_vector[i]->napi); 441 } 442 } 443 444 /** 445 * octep_napi_disable() - disable NAPI for all Tx/Rx queues. 446 * 447 * @oct: Octeon device private data structure. 448 */ 449 static void octep_napi_disable(struct octep_device *oct) 450 { 451 int i; 452 453 for (i = 0; i < oct->num_oqs; i++) { 454 netdev_dbg(oct->netdev, "Disabling NAPI on Q-%d\n", i); 455 napi_disable(&oct->ioq_vector[i]->napi); 456 } 457 } 458 459 static void octep_link_up(struct net_device *netdev) 460 { 461 netif_carrier_on(netdev); 462 netif_tx_start_all_queues(netdev); 463 } 464 465 /** 466 * octep_open() - start the octeon network device. 467 * 468 * @netdev: pointer to kernel network device. 469 * 470 * setup Tx/Rx queues, interrupts and enable hardware operation of Tx/Rx queues 471 * and interrupts.. 472 * 473 * Return: 0, on successfully setting up device and bring it up. 474 * -1, on any error. 475 */ 476 static int octep_open(struct net_device *netdev) 477 { 478 struct octep_device *oct = netdev_priv(netdev); 479 int err, ret; 480 481 netdev_info(netdev, "Starting netdev ...\n"); 482 netif_carrier_off(netdev); 483 484 oct->hw_ops.reset_io_queues(oct); 485 486 if (octep_setup_iqs(oct)) 487 goto setup_iq_err; 488 if (octep_setup_oqs(oct)) 489 goto setup_oq_err; 490 if (octep_setup_irqs(oct)) 491 goto setup_irq_err; 492 493 err = netif_set_real_num_tx_queues(netdev, oct->num_oqs); 494 if (err) 495 goto set_queues_err; 496 err = netif_set_real_num_rx_queues(netdev, oct->num_iqs); 497 if (err) 498 goto set_queues_err; 499 500 octep_napi_add(oct); 501 octep_napi_enable(oct); 502 503 oct->link_info.admin_up = 1; 504 octep_set_rx_state(oct, true); 505 506 ret = octep_get_link_status(oct); 507 if (!ret) 508 octep_set_link_status(oct, true); 509 510 /* Enable the input and output queues for this Octeon device */ 511 oct->hw_ops.enable_io_queues(oct); 512 513 /* Enable Octeon device interrupts */ 514 oct->hw_ops.enable_interrupts(oct); 515 516 octep_oq_dbell_init(oct); 517 518 ret = octep_get_link_status(oct); 519 if (ret) 520 octep_link_up(netdev); 521 522 return 0; 523 524 set_queues_err: 525 octep_napi_disable(oct); 526 octep_napi_delete(oct); 527 octep_clean_irqs(oct); 528 setup_irq_err: 529 octep_free_oqs(oct); 530 setup_oq_err: 531 octep_free_iqs(oct); 532 setup_iq_err: 533 return -1; 534 } 535 536 /** 537 * octep_stop() - stop the octeon network device. 538 * 539 * @netdev: pointer to kernel network device. 540 * 541 * stop the device Tx/Rx operations, bring down the link and 542 * free up all resources allocated for Tx/Rx queues and interrupts. 543 */ 544 static int octep_stop(struct net_device *netdev) 545 { 546 struct octep_device *oct = netdev_priv(netdev); 547 548 netdev_info(netdev, "Stopping the device ...\n"); 549 550 /* Stop Tx from stack */ 551 netif_tx_stop_all_queues(netdev); 552 netif_carrier_off(netdev); 553 netif_tx_disable(netdev); 554 555 octep_set_link_status(oct, false); 556 octep_set_rx_state(oct, false); 557 558 oct->link_info.admin_up = 0; 559 oct->link_info.oper_up = 0; 560 561 oct->hw_ops.disable_interrupts(oct); 562 octep_napi_disable(oct); 563 octep_napi_delete(oct); 564 565 octep_clean_irqs(oct); 566 octep_clean_iqs(oct); 567 568 oct->hw_ops.disable_io_queues(oct); 569 oct->hw_ops.reset_io_queues(oct); 570 octep_free_oqs(oct); 571 octep_free_iqs(oct); 572 netdev_info(netdev, "Device stopped !!\n"); 573 return 0; 574 } 575 576 /** 577 * octep_iq_full_check() - check if a Tx queue is full. 578 * 579 * @iq: Octeon Tx queue data structure. 580 * 581 * Return: 0, if the Tx queue is not full. 582 * 1, if the Tx queue is full. 583 */ 584 static inline int octep_iq_full_check(struct octep_iq *iq) 585 { 586 if (likely((iq->max_count - atomic_read(&iq->instr_pending)) >= 587 OCTEP_WAKE_QUEUE_THRESHOLD)) 588 return 0; 589 590 /* Stop the queue if unable to send */ 591 netif_stop_subqueue(iq->netdev, iq->q_no); 592 593 /* check again and restart the queue, in case NAPI has just freed 594 * enough Tx ring entries. 595 */ 596 if (unlikely((iq->max_count - atomic_read(&iq->instr_pending)) >= 597 OCTEP_WAKE_QUEUE_THRESHOLD)) { 598 netif_start_subqueue(iq->netdev, iq->q_no); 599 iq->stats.restart_cnt++; 600 return 0; 601 } 602 603 return 1; 604 } 605 606 /** 607 * octep_start_xmit() - Enqueue packet to Octoen hardware Tx Queue. 608 * 609 * @skb: packet skbuff pointer. 610 * @netdev: kernel network device. 611 * 612 * Return: NETDEV_TX_BUSY, if Tx Queue is full. 613 * NETDEV_TX_OK, if successfully enqueued to hardware Tx queue. 614 */ 615 static netdev_tx_t octep_start_xmit(struct sk_buff *skb, 616 struct net_device *netdev) 617 { 618 struct octep_device *oct = netdev_priv(netdev); 619 struct octep_tx_sglist_desc *sglist; 620 struct octep_tx_buffer *tx_buffer; 621 struct octep_tx_desc_hw *hw_desc; 622 struct skb_shared_info *shinfo; 623 struct octep_instr_hdr *ih; 624 struct octep_iq *iq; 625 skb_frag_t *frag; 626 u16 nr_frags, si; 627 u16 q_no, wi; 628 629 q_no = skb_get_queue_mapping(skb); 630 if (q_no >= oct->num_iqs) { 631 netdev_err(netdev, "Invalid Tx skb->queue_mapping=%d\n", q_no); 632 q_no = q_no % oct->num_iqs; 633 } 634 635 iq = oct->iq[q_no]; 636 if (octep_iq_full_check(iq)) { 637 iq->stats.tx_busy++; 638 return NETDEV_TX_BUSY; 639 } 640 641 shinfo = skb_shinfo(skb); 642 nr_frags = shinfo->nr_frags; 643 644 wi = iq->host_write_index; 645 hw_desc = &iq->desc_ring[wi]; 646 hw_desc->ih64 = 0; 647 648 tx_buffer = iq->buff_info + wi; 649 tx_buffer->skb = skb; 650 651 ih = &hw_desc->ih; 652 ih->tlen = skb->len; 653 ih->pkind = oct->pkind; 654 655 if (!nr_frags) { 656 tx_buffer->gather = 0; 657 tx_buffer->dma = dma_map_single(iq->dev, skb->data, 658 skb->len, DMA_TO_DEVICE); 659 if (dma_mapping_error(iq->dev, tx_buffer->dma)) 660 goto dma_map_err; 661 hw_desc->dptr = tx_buffer->dma; 662 } else { 663 /* Scatter/Gather */ 664 dma_addr_t dma; 665 u16 len; 666 667 sglist = tx_buffer->sglist; 668 669 ih->gsz = nr_frags + 1; 670 ih->gather = 1; 671 tx_buffer->gather = 1; 672 673 len = skb_headlen(skb); 674 dma = dma_map_single(iq->dev, skb->data, len, DMA_TO_DEVICE); 675 if (dma_mapping_error(iq->dev, dma)) 676 goto dma_map_err; 677 678 dma_sync_single_for_cpu(iq->dev, tx_buffer->sglist_dma, 679 OCTEP_SGLIST_SIZE_PER_PKT, 680 DMA_TO_DEVICE); 681 memset(sglist, 0, OCTEP_SGLIST_SIZE_PER_PKT); 682 sglist[0].len[3] = len; 683 sglist[0].dma_ptr[0] = dma; 684 685 si = 1; /* entry 0 is main skb, mapped above */ 686 frag = &shinfo->frags[0]; 687 while (nr_frags--) { 688 len = skb_frag_size(frag); 689 dma = skb_frag_dma_map(iq->dev, frag, 0, 690 len, DMA_TO_DEVICE); 691 if (dma_mapping_error(iq->dev, dma)) 692 goto dma_map_sg_err; 693 694 sglist[si >> 2].len[3 - (si & 3)] = len; 695 sglist[si >> 2].dma_ptr[si & 3] = dma; 696 697 frag++; 698 si++; 699 } 700 dma_sync_single_for_device(iq->dev, tx_buffer->sglist_dma, 701 OCTEP_SGLIST_SIZE_PER_PKT, 702 DMA_TO_DEVICE); 703 704 hw_desc->dptr = tx_buffer->sglist_dma; 705 } 706 707 /* Flush the hw descriptor before writing to doorbell */ 708 wmb(); 709 710 /* Ring Doorbell to notify the NIC there is a new packet */ 711 writel(1, iq->doorbell_reg); 712 atomic_inc(&iq->instr_pending); 713 wi++; 714 if (wi == iq->max_count) 715 wi = 0; 716 iq->host_write_index = wi; 717 718 netdev_tx_sent_queue(iq->netdev_q, skb->len); 719 iq->stats.instr_posted++; 720 skb_tx_timestamp(skb); 721 return NETDEV_TX_OK; 722 723 dma_map_sg_err: 724 if (si > 0) { 725 dma_unmap_single(iq->dev, sglist[0].dma_ptr[0], 726 sglist[0].len[0], DMA_TO_DEVICE); 727 sglist[0].len[0] = 0; 728 } 729 while (si > 1) { 730 dma_unmap_page(iq->dev, sglist[si >> 2].dma_ptr[si & 3], 731 sglist[si >> 2].len[si & 3], DMA_TO_DEVICE); 732 sglist[si >> 2].len[si & 3] = 0; 733 si--; 734 } 735 tx_buffer->gather = 0; 736 dma_map_err: 737 dev_kfree_skb_any(skb); 738 return NETDEV_TX_OK; 739 } 740 741 /** 742 * octep_get_stats64() - Get Octeon network device statistics. 743 * 744 * @netdev: kernel network device. 745 * @stats: pointer to stats structure to be filled in. 746 */ 747 static void octep_get_stats64(struct net_device *netdev, 748 struct rtnl_link_stats64 *stats) 749 { 750 u64 tx_packets, tx_bytes, rx_packets, rx_bytes; 751 struct octep_device *oct = netdev_priv(netdev); 752 int q; 753 754 octep_get_if_stats(oct); 755 tx_packets = 0; 756 tx_bytes = 0; 757 rx_packets = 0; 758 rx_bytes = 0; 759 for (q = 0; q < oct->num_oqs; q++) { 760 struct octep_iq *iq = oct->iq[q]; 761 struct octep_oq *oq = oct->oq[q]; 762 763 tx_packets += iq->stats.instr_completed; 764 tx_bytes += iq->stats.bytes_sent; 765 rx_packets += oq->stats.packets; 766 rx_bytes += oq->stats.bytes; 767 } 768 stats->tx_packets = tx_packets; 769 stats->tx_bytes = tx_bytes; 770 stats->rx_packets = rx_packets; 771 stats->rx_bytes = rx_bytes; 772 stats->multicast = oct->iface_rx_stats.mcast_pkts; 773 stats->rx_errors = oct->iface_rx_stats.err_pkts; 774 stats->collisions = oct->iface_tx_stats.xscol; 775 stats->tx_fifo_errors = oct->iface_tx_stats.undflw; 776 } 777 778 /** 779 * octep_tx_timeout_task - work queue task to Handle Tx queue timeout. 780 * 781 * @work: pointer to Tx queue timeout work_struct 782 * 783 * Stop and start the device so that it frees up all queue resources 784 * and restarts the queues, that potentially clears a Tx queue timeout 785 * condition. 786 **/ 787 static void octep_tx_timeout_task(struct work_struct *work) 788 { 789 struct octep_device *oct = container_of(work, struct octep_device, 790 tx_timeout_task); 791 struct net_device *netdev = oct->netdev; 792 793 rtnl_lock(); 794 if (netif_running(netdev)) { 795 octep_stop(netdev); 796 octep_open(netdev); 797 } 798 rtnl_unlock(); 799 } 800 801 /** 802 * octep_tx_timeout() - Handle Tx Queue timeout. 803 * 804 * @netdev: pointer to kernel network device. 805 * @txqueue: Timed out Tx queue number. 806 * 807 * Schedule a work to handle Tx queue timeout. 808 */ 809 static void octep_tx_timeout(struct net_device *netdev, unsigned int txqueue) 810 { 811 struct octep_device *oct = netdev_priv(netdev); 812 813 queue_work(octep_wq, &oct->tx_timeout_task); 814 } 815 816 static int octep_set_mac(struct net_device *netdev, void *p) 817 { 818 struct octep_device *oct = netdev_priv(netdev); 819 struct sockaddr *addr = (struct sockaddr *)p; 820 int err; 821 822 if (!is_valid_ether_addr(addr->sa_data)) 823 return -EADDRNOTAVAIL; 824 825 err = octep_set_mac_addr(oct, addr->sa_data); 826 if (err) 827 return err; 828 829 memcpy(oct->mac_addr, addr->sa_data, ETH_ALEN); 830 eth_hw_addr_set(netdev, addr->sa_data); 831 832 return 0; 833 } 834 835 static int octep_change_mtu(struct net_device *netdev, int new_mtu) 836 { 837 struct octep_device *oct = netdev_priv(netdev); 838 struct octep_iface_link_info *link_info; 839 int err = 0; 840 841 link_info = &oct->link_info; 842 if (link_info->mtu == new_mtu) 843 return 0; 844 845 err = octep_set_mtu(oct, new_mtu); 846 if (!err) { 847 oct->link_info.mtu = new_mtu; 848 netdev->mtu = new_mtu; 849 } 850 851 return err; 852 } 853 854 static const struct net_device_ops octep_netdev_ops = { 855 .ndo_open = octep_open, 856 .ndo_stop = octep_stop, 857 .ndo_start_xmit = octep_start_xmit, 858 .ndo_get_stats64 = octep_get_stats64, 859 .ndo_tx_timeout = octep_tx_timeout, 860 .ndo_set_mac_address = octep_set_mac, 861 .ndo_change_mtu = octep_change_mtu, 862 }; 863 864 /** 865 * octep_ctrl_mbox_task - work queue task to handle ctrl mbox messages. 866 * 867 * @work: pointer to ctrl mbox work_struct 868 * 869 * Poll ctrl mbox message queue and handle control messages from firmware. 870 **/ 871 static void octep_ctrl_mbox_task(struct work_struct *work) 872 { 873 struct octep_device *oct = container_of(work, struct octep_device, 874 ctrl_mbox_task); 875 struct net_device *netdev = oct->netdev; 876 struct octep_ctrl_net_f2h_req req = {}; 877 struct octep_ctrl_mbox_msg msg; 878 int ret = 0; 879 880 msg.msg = &req; 881 while (true) { 882 ret = octep_ctrl_mbox_recv(&oct->ctrl_mbox, &msg); 883 if (ret) 884 break; 885 886 switch (req.hdr.cmd) { 887 case OCTEP_CTRL_NET_F2H_CMD_LINK_STATUS: 888 if (netif_running(netdev)) { 889 if (req.link.state) { 890 dev_info(&oct->pdev->dev, "netif_carrier_on\n"); 891 netif_carrier_on(netdev); 892 } else { 893 dev_info(&oct->pdev->dev, "netif_carrier_off\n"); 894 netif_carrier_off(netdev); 895 } 896 } 897 break; 898 default: 899 pr_info("Unknown mbox req : %u\n", req.hdr.cmd); 900 break; 901 } 902 } 903 } 904 905 /** 906 * octep_device_setup() - Setup Octeon Device. 907 * 908 * @oct: Octeon device private data structure. 909 * 910 * Setup Octeon device hardware operations, configuration, etc ... 911 */ 912 int octep_device_setup(struct octep_device *oct) 913 { 914 struct octep_ctrl_mbox *ctrl_mbox; 915 struct pci_dev *pdev = oct->pdev; 916 int i, ret; 917 918 /* allocate memory for oct->conf */ 919 oct->conf = kzalloc(sizeof(*oct->conf), GFP_KERNEL); 920 if (!oct->conf) 921 return -ENOMEM; 922 923 /* Map BAR regions */ 924 for (i = 0; i < OCTEP_MMIO_REGIONS; i++) { 925 oct->mmio[i].hw_addr = 926 ioremap(pci_resource_start(oct->pdev, i * 2), 927 pci_resource_len(oct->pdev, i * 2)); 928 oct->mmio[i].mapped = 1; 929 } 930 931 oct->chip_id = pdev->device; 932 oct->rev_id = pdev->revision; 933 dev_info(&pdev->dev, "chip_id = 0x%x\n", pdev->device); 934 935 switch (oct->chip_id) { 936 case OCTEP_PCI_DEVICE_ID_CN93_PF: 937 dev_info(&pdev->dev, 938 "Setting up OCTEON CN93XX PF PASS%d.%d\n", 939 OCTEP_MAJOR_REV(oct), OCTEP_MINOR_REV(oct)); 940 octep_device_setup_cn93_pf(oct); 941 break; 942 default: 943 dev_err(&pdev->dev, 944 "%s: unsupported device\n", __func__); 945 goto unsupported_dev; 946 } 947 948 oct->pkind = CFG_GET_IQ_PKIND(oct->conf); 949 950 /* Initialize control mbox */ 951 ctrl_mbox = &oct->ctrl_mbox; 952 ctrl_mbox->barmem = CFG_GET_CTRL_MBOX_MEM_ADDR(oct->conf); 953 ret = octep_ctrl_mbox_init(ctrl_mbox); 954 if (ret) { 955 dev_err(&pdev->dev, "Failed to initialize control mbox\n"); 956 return -1; 957 } 958 oct->ctrl_mbox_ifstats_offset = OCTEP_CTRL_MBOX_SZ(ctrl_mbox->h2fq.elem_sz, 959 ctrl_mbox->h2fq.elem_cnt, 960 ctrl_mbox->f2hq.elem_sz, 961 ctrl_mbox->f2hq.elem_cnt); 962 963 return 0; 964 965 unsupported_dev: 966 return -1; 967 } 968 969 /** 970 * octep_device_cleanup() - Cleanup Octeon Device. 971 * 972 * @oct: Octeon device private data structure. 973 * 974 * Cleanup Octeon device allocated resources. 975 */ 976 static void octep_device_cleanup(struct octep_device *oct) 977 { 978 int i; 979 980 dev_info(&oct->pdev->dev, "Cleaning up Octeon Device ...\n"); 981 982 for (i = 0; i < OCTEP_MAX_VF; i++) { 983 if (oct->mbox[i]) 984 vfree(oct->mbox[i]); 985 oct->mbox[i] = NULL; 986 } 987 988 octep_ctrl_mbox_uninit(&oct->ctrl_mbox); 989 990 oct->hw_ops.soft_reset(oct); 991 for (i = 0; i < OCTEP_MMIO_REGIONS; i++) { 992 if (oct->mmio[i].mapped) 993 iounmap(oct->mmio[i].hw_addr); 994 } 995 996 kfree(oct->conf); 997 oct->conf = NULL; 998 } 999 1000 /** 1001 * octep_probe() - Octeon PCI device probe handler. 1002 * 1003 * @pdev: PCI device structure. 1004 * @ent: entry in Octeon PCI device ID table. 1005 * 1006 * Initializes and enables the Octeon PCI device for network operations. 1007 * Initializes Octeon private data structure and registers a network device. 1008 */ 1009 static int octep_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 1010 { 1011 struct octep_device *octep_dev = NULL; 1012 struct net_device *netdev; 1013 int err; 1014 1015 err = pci_enable_device(pdev); 1016 if (err) { 1017 dev_err(&pdev->dev, "Failed to enable PCI device\n"); 1018 return err; 1019 } 1020 1021 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 1022 if (err) { 1023 dev_err(&pdev->dev, "Failed to set DMA mask !!\n"); 1024 goto err_dma_mask; 1025 } 1026 1027 err = pci_request_mem_regions(pdev, OCTEP_DRV_NAME); 1028 if (err) { 1029 dev_err(&pdev->dev, "Failed to map PCI memory regions\n"); 1030 goto err_pci_regions; 1031 } 1032 1033 pci_enable_pcie_error_reporting(pdev); 1034 pci_set_master(pdev); 1035 1036 netdev = alloc_etherdev_mq(sizeof(struct octep_device), 1037 OCTEP_MAX_QUEUES); 1038 if (!netdev) { 1039 dev_err(&pdev->dev, "Failed to allocate netdev\n"); 1040 err = -ENOMEM; 1041 goto err_alloc_netdev; 1042 } 1043 SET_NETDEV_DEV(netdev, &pdev->dev); 1044 1045 octep_dev = netdev_priv(netdev); 1046 octep_dev->netdev = netdev; 1047 octep_dev->pdev = pdev; 1048 octep_dev->dev = &pdev->dev; 1049 pci_set_drvdata(pdev, octep_dev); 1050 1051 err = octep_device_setup(octep_dev); 1052 if (err) { 1053 dev_err(&pdev->dev, "Device setup failed\n"); 1054 goto err_octep_config; 1055 } 1056 INIT_WORK(&octep_dev->tx_timeout_task, octep_tx_timeout_task); 1057 INIT_WORK(&octep_dev->ctrl_mbox_task, octep_ctrl_mbox_task); 1058 1059 netdev->netdev_ops = &octep_netdev_ops; 1060 octep_set_ethtool_ops(netdev); 1061 netif_carrier_off(netdev); 1062 1063 netdev->hw_features = NETIF_F_SG; 1064 netdev->features |= netdev->hw_features; 1065 netdev->min_mtu = OCTEP_MIN_MTU; 1066 netdev->max_mtu = OCTEP_MAX_MTU; 1067 netdev->mtu = OCTEP_DEFAULT_MTU; 1068 1069 octep_get_mac_addr(octep_dev, octep_dev->mac_addr); 1070 eth_hw_addr_set(netdev, octep_dev->mac_addr); 1071 1072 err = register_netdev(netdev); 1073 if (err) { 1074 dev_err(&pdev->dev, "Failed to register netdev\n"); 1075 goto register_dev_err; 1076 } 1077 dev_info(&pdev->dev, "Device probe successful\n"); 1078 return 0; 1079 1080 register_dev_err: 1081 octep_device_cleanup(octep_dev); 1082 err_octep_config: 1083 free_netdev(netdev); 1084 err_alloc_netdev: 1085 pci_disable_pcie_error_reporting(pdev); 1086 pci_release_mem_regions(pdev); 1087 err_pci_regions: 1088 err_dma_mask: 1089 pci_disable_device(pdev); 1090 return err; 1091 } 1092 1093 /** 1094 * octep_remove() - Remove Octeon PCI device from driver control. 1095 * 1096 * @pdev: PCI device structure of the Octeon device. 1097 * 1098 * Cleanup all resources allocated for the Octeon device. 1099 * Unregister from network device and disable the PCI device. 1100 */ 1101 static void octep_remove(struct pci_dev *pdev) 1102 { 1103 struct octep_device *oct = pci_get_drvdata(pdev); 1104 struct net_device *netdev; 1105 1106 if (!oct) 1107 return; 1108 1109 cancel_work_sync(&oct->tx_timeout_task); 1110 cancel_work_sync(&oct->ctrl_mbox_task); 1111 netdev = oct->netdev; 1112 if (netdev->reg_state == NETREG_REGISTERED) 1113 unregister_netdev(netdev); 1114 1115 octep_device_cleanup(oct); 1116 pci_release_mem_regions(pdev); 1117 free_netdev(netdev); 1118 pci_disable_pcie_error_reporting(pdev); 1119 pci_disable_device(pdev); 1120 } 1121 1122 static struct pci_driver octep_driver = { 1123 .name = OCTEP_DRV_NAME, 1124 .id_table = octep_pci_id_tbl, 1125 .probe = octep_probe, 1126 .remove = octep_remove, 1127 }; 1128 1129 /** 1130 * octep_init_module() - Module initialiation. 1131 * 1132 * create common resource for the driver and register PCI driver. 1133 */ 1134 static int __init octep_init_module(void) 1135 { 1136 int ret; 1137 1138 pr_info("%s: Loading %s ...\n", OCTEP_DRV_NAME, OCTEP_DRV_STRING); 1139 1140 /* work queue for all deferred tasks */ 1141 octep_wq = create_singlethread_workqueue(OCTEP_DRV_NAME); 1142 if (!octep_wq) { 1143 pr_err("%s: Failed to create common workqueue\n", 1144 OCTEP_DRV_NAME); 1145 return -ENOMEM; 1146 } 1147 1148 ret = pci_register_driver(&octep_driver); 1149 if (ret < 0) { 1150 pr_err("%s: Failed to register PCI driver; err=%d\n", 1151 OCTEP_DRV_NAME, ret); 1152 return ret; 1153 } 1154 1155 pr_info("%s: Loaded successfully !\n", OCTEP_DRV_NAME); 1156 1157 return ret; 1158 } 1159 1160 /** 1161 * octep_exit_module() - Module exit routine. 1162 * 1163 * unregister the driver with PCI subsystem and cleanup common resources. 1164 */ 1165 static void __exit octep_exit_module(void) 1166 { 1167 pr_info("%s: Unloading ...\n", OCTEP_DRV_NAME); 1168 1169 pci_unregister_driver(&octep_driver); 1170 destroy_workqueue(octep_wq); 1171 1172 pr_info("%s: Unloading complete\n", OCTEP_DRV_NAME); 1173 } 1174 1175 module_init(octep_init_module); 1176 module_exit(octep_exit_module); 1177