1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2021 Beijing WangXun Technology Co., Ltd.
3 * Copyright(c) 2010-2017 Intel Corporation
7 #include <rte_common.h>
8 #include <ethdev_pci.h>
10 #include <rte_alarm.h>
12 #include "ngbe_logs.h"
14 #include "ngbe_ethdev.h"
15 #include "ngbe_rxtx.h"
17 static int ngbe_dev_close(struct rte_eth_dev *dev);
18 static int ngbe_dev_link_update(struct rte_eth_dev *dev,
19 int wait_to_complete);
21 static void ngbe_dev_link_status_print(struct rte_eth_dev *dev);
22 static int ngbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on);
23 static int ngbe_dev_macsec_interrupt_setup(struct rte_eth_dev *dev);
24 static int ngbe_dev_misc_interrupt_setup(struct rte_eth_dev *dev);
25 static int ngbe_dev_rxq_interrupt_setup(struct rte_eth_dev *dev);
26 static void ngbe_dev_interrupt_handler(void *param);
27 static void ngbe_dev_interrupt_delayed_handler(void *param);
28 static void ngbe_configure_msix(struct rte_eth_dev *dev);
31 * The set of PCI devices this driver supports
33 static const struct rte_pci_id pci_id_ngbe_map[] = {
34 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860A2) },
35 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860A2S) },
36 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860A4) },
37 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860A4S) },
38 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860AL2) },
39 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860AL2S) },
40 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860AL4) },
41 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860AL4S) },
42 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860NCSI) },
43 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860A1) },
44 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860A1L) },
45 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, NGBE_DEV_ID_EM_WX1860AL_W) },
46 { .vendor_id = 0, /* sentinel */ },
49 static const struct rte_eth_desc_lim rx_desc_lim = {
50 .nb_max = NGBE_RING_DESC_MAX,
51 .nb_min = NGBE_RING_DESC_MIN,
52 .nb_align = NGBE_RXD_ALIGN,
55 static const struct rte_eth_desc_lim tx_desc_lim = {
56 .nb_max = NGBE_RING_DESC_MAX,
57 .nb_min = NGBE_RING_DESC_MIN,
58 .nb_align = NGBE_TXD_ALIGN,
59 .nb_seg_max = NGBE_TX_MAX_SEG,
60 .nb_mtu_seg_max = NGBE_TX_MAX_SEG,
63 static const struct eth_dev_ops ngbe_eth_dev_ops;
66 ngbe_pf_reset_hw(struct ngbe_hw *hw)
71 status = hw->mac.reset_hw(hw);
73 ctrl_ext = rd32(hw, NGBE_PORTCTL);
74 /* Set PF Reset Done bit so PF/VF Mail Ops can work */
75 ctrl_ext |= NGBE_PORTCTL_RSTDONE;
76 wr32(hw, NGBE_PORTCTL, ctrl_ext);
79 if (status == NGBE_ERR_SFP_NOT_PRESENT)
85 ngbe_enable_intr(struct rte_eth_dev *dev)
87 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
88 struct ngbe_hw *hw = ngbe_dev_hw(dev);
90 wr32(hw, NGBE_IENMISC, intr->mask_misc);
91 wr32(hw, NGBE_IMC(0), intr->mask & BIT_MASK32);
96 ngbe_disable_intr(struct ngbe_hw *hw)
98 PMD_INIT_FUNC_TRACE();
100 wr32(hw, NGBE_IMS(0), NGBE_IMS_MASK);
105 * Ensure that all locks are released before first NVM or PHY access
108 ngbe_swfw_lock_reset(struct ngbe_hw *hw)
113 * These ones are more tricky since they are common to all ports; but
114 * swfw_sync retries last long enough (1s) to be almost sure that if
115 * lock can not be taken it is due to an improper lock of the
118 mask = NGBE_MNGSEM_SWPHY |
121 if (hw->mac.acquire_swfw_sync(hw, mask) < 0)
122 PMD_DRV_LOG(DEBUG, "SWFW common locks released");
124 hw->mac.release_swfw_sync(hw, mask);
128 eth_ngbe_dev_init(struct rte_eth_dev *eth_dev, void *init_params __rte_unused)
130 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
131 struct ngbe_hw *hw = ngbe_dev_hw(eth_dev);
132 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
133 const struct rte_memzone *mz;
137 PMD_INIT_FUNC_TRACE();
139 eth_dev->dev_ops = &ngbe_eth_dev_ops;
140 eth_dev->rx_pkt_burst = &ngbe_recv_pkts;
142 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
145 rte_eth_copy_pci_info(eth_dev, pci_dev);
147 /* Vendor and Device ID need to be set before init of shared code */
148 hw->device_id = pci_dev->id.device_id;
149 hw->vendor_id = pci_dev->id.vendor_id;
150 hw->sub_system_id = pci_dev->id.subsystem_device_id;
151 ngbe_map_device_id(hw);
152 hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
154 /* Reserve memory for interrupt status block */
155 mz = rte_eth_dma_zone_reserve(eth_dev, "ngbe_driver", -1,
156 NGBE_ISB_SIZE, NGBE_ALIGN, SOCKET_ID_ANY);
160 hw->isb_dma = TMZ_PADDR(mz);
161 hw->isb_mem = TMZ_VADDR(mz);
163 /* Initialize the shared code (base driver) */
164 err = ngbe_init_shared_code(hw);
166 PMD_INIT_LOG(ERR, "Shared code init failed: %d", err);
170 /* Unlock any pending hardware semaphore */
171 ngbe_swfw_lock_reset(hw);
173 err = hw->rom.init_params(hw);
175 PMD_INIT_LOG(ERR, "The EEPROM init failed: %d", err);
179 /* Make sure we have a good EEPROM before we read from it */
180 err = hw->rom.validate_checksum(hw, NULL);
182 PMD_INIT_LOG(ERR, "The EEPROM checksum is not valid: %d", err);
186 err = hw->mac.init_hw(hw);
188 PMD_INIT_LOG(ERR, "Hardware Initialization Failure: %d", err);
192 /* disable interrupt */
193 ngbe_disable_intr(hw);
195 /* Allocate memory for storing MAC addresses */
196 eth_dev->data->mac_addrs = rte_zmalloc("ngbe", RTE_ETHER_ADDR_LEN *
197 hw->mac.num_rar_entries, 0);
198 if (eth_dev->data->mac_addrs == NULL) {
200 "Failed to allocate %u bytes needed to store MAC addresses",
201 RTE_ETHER_ADDR_LEN * hw->mac.num_rar_entries);
205 /* Copy the permanent MAC address */
206 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.perm_addr,
207 ð_dev->data->mac_addrs[0]);
209 /* Allocate memory for storing hash filter MAC addresses */
210 eth_dev->data->hash_mac_addrs = rte_zmalloc("ngbe",
211 RTE_ETHER_ADDR_LEN * NGBE_VMDQ_NUM_UC_MAC, 0);
212 if (eth_dev->data->hash_mac_addrs == NULL) {
214 "Failed to allocate %d bytes needed to store MAC addresses",
215 RTE_ETHER_ADDR_LEN * NGBE_VMDQ_NUM_UC_MAC);
216 rte_free(eth_dev->data->mac_addrs);
217 eth_dev->data->mac_addrs = NULL;
221 ctrl_ext = rd32(hw, NGBE_PORTCTL);
222 /* let hardware know driver is loaded */
223 ctrl_ext |= NGBE_PORTCTL_DRVLOAD;
224 /* Set PF Reset Done bit so PF/VF Mail Ops can work */
225 ctrl_ext |= NGBE_PORTCTL_RSTDONE;
226 wr32(hw, NGBE_PORTCTL, ctrl_ext);
229 PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d",
230 (int)hw->mac.type, (int)hw->phy.type);
232 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
233 eth_dev->data->port_id, pci_dev->id.vendor_id,
234 pci_dev->id.device_id);
236 rte_intr_callback_register(intr_handle,
237 ngbe_dev_interrupt_handler, eth_dev);
239 /* enable uio/vfio intr/eventfd mapping */
240 rte_intr_enable(intr_handle);
242 /* enable support intr */
243 ngbe_enable_intr(eth_dev);
249 eth_ngbe_dev_uninit(struct rte_eth_dev *eth_dev)
251 PMD_INIT_FUNC_TRACE();
253 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
256 ngbe_dev_close(eth_dev);
262 eth_ngbe_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
263 struct rte_pci_device *pci_dev)
265 return rte_eth_dev_create(&pci_dev->device, pci_dev->device.name,
266 sizeof(struct ngbe_adapter),
267 eth_dev_pci_specific_init, pci_dev,
268 eth_ngbe_dev_init, NULL);
271 static int eth_ngbe_pci_remove(struct rte_pci_device *pci_dev)
273 struct rte_eth_dev *ethdev;
275 ethdev = rte_eth_dev_allocated(pci_dev->device.name);
279 return rte_eth_dev_destroy(ethdev, eth_ngbe_dev_uninit);
282 static struct rte_pci_driver rte_ngbe_pmd = {
283 .id_table = pci_id_ngbe_map,
284 .drv_flags = RTE_PCI_DRV_NEED_MAPPING |
285 RTE_PCI_DRV_INTR_LSC,
286 .probe = eth_ngbe_pci_probe,
287 .remove = eth_ngbe_pci_remove,
291 ngbe_dev_configure(struct rte_eth_dev *dev)
293 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
294 struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
296 PMD_INIT_FUNC_TRACE();
298 /* set flag to update link status after init */
299 intr->flags |= NGBE_FLAG_NEED_LINK_UPDATE;
302 * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
303 * allocation Rx preconditions we will reset it.
305 adapter->rx_bulk_alloc_allowed = true;
311 ngbe_dev_phy_intr_setup(struct rte_eth_dev *dev)
313 struct ngbe_hw *hw = ngbe_dev_hw(dev);
314 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
316 wr32(hw, NGBE_GPIODIR, NGBE_GPIODIR_DDR(1));
317 wr32(hw, NGBE_GPIOINTEN, NGBE_GPIOINTEN_INT(3));
318 wr32(hw, NGBE_GPIOINTTYPE, NGBE_GPIOINTTYPE_LEVEL(0));
319 if (hw->phy.type == ngbe_phy_yt8521s_sfi)
320 wr32(hw, NGBE_GPIOINTPOL, NGBE_GPIOINTPOL_ACT(0));
322 wr32(hw, NGBE_GPIOINTPOL, NGBE_GPIOINTPOL_ACT(3));
324 intr->mask_misc |= NGBE_ICRMISC_GPIO;
328 * Configure device link speed and setup link.
329 * It returns 0 on success.
332 ngbe_dev_start(struct rte_eth_dev *dev)
334 struct ngbe_hw *hw = ngbe_dev_hw(dev);
335 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
336 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
337 uint32_t intr_vector = 0;
339 bool link_up = false, negotiate = false;
341 uint32_t allowed_speeds = 0;
343 uint32_t *link_speeds;
345 PMD_INIT_FUNC_TRACE();
347 /* disable uio/vfio intr/eventfd mapping */
348 rte_intr_disable(intr_handle);
351 hw->adapter_stopped = 0;
354 /* reinitialize adapter, this calls reset and start */
355 hw->nb_rx_queues = dev->data->nb_rx_queues;
356 hw->nb_tx_queues = dev->data->nb_tx_queues;
357 status = ngbe_pf_reset_hw(hw);
360 hw->mac.start_hw(hw);
361 hw->mac.get_link_status = true;
363 ngbe_dev_phy_intr_setup(dev);
365 /* check and configure queue intr-vector mapping */
366 if ((rte_intr_cap_multiple(intr_handle) ||
367 !RTE_ETH_DEV_SRIOV(dev).active) &&
368 dev->data->dev_conf.intr_conf.rxq != 0) {
369 intr_vector = dev->data->nb_rx_queues;
370 if (rte_intr_efd_enable(intr_handle, intr_vector))
374 if (rte_intr_dp_is_en(intr_handle) && intr_handle->intr_vec == NULL) {
375 intr_handle->intr_vec =
376 rte_zmalloc("intr_vec",
377 dev->data->nb_rx_queues * sizeof(int), 0);
378 if (intr_handle->intr_vec == NULL) {
380 "Failed to allocate %d rx_queues intr_vec",
381 dev->data->nb_rx_queues);
386 /* confiugre MSI-X for sleep until Rx interrupt */
387 ngbe_configure_msix(dev);
389 /* initialize transmission unit */
390 ngbe_dev_tx_init(dev);
392 /* This can fail when allocating mbufs for descriptor rings */
393 err = ngbe_dev_rx_init(dev);
395 PMD_INIT_LOG(ERR, "Unable to initialize Rx hardware");
399 err = ngbe_dev_rxtx_start(dev);
401 PMD_INIT_LOG(ERR, "Unable to start rxtx queues");
405 err = hw->mac.check_link(hw, &speed, &link_up, 0);
408 dev->data->dev_link.link_status = link_up;
410 link_speeds = &dev->data->dev_conf.link_speeds;
411 if (*link_speeds == ETH_LINK_SPEED_AUTONEG)
414 err = hw->mac.get_link_capabilities(hw, &speed, &negotiate);
419 if (hw->mac.default_speeds & NGBE_LINK_SPEED_1GB_FULL)
420 allowed_speeds |= ETH_LINK_SPEED_1G;
421 if (hw->mac.default_speeds & NGBE_LINK_SPEED_100M_FULL)
422 allowed_speeds |= ETH_LINK_SPEED_100M;
423 if (hw->mac.default_speeds & NGBE_LINK_SPEED_10M_FULL)
424 allowed_speeds |= ETH_LINK_SPEED_10M;
426 if (*link_speeds & ~allowed_speeds) {
427 PMD_INIT_LOG(ERR, "Invalid link setting");
432 if (*link_speeds == ETH_LINK_SPEED_AUTONEG) {
433 speed = hw->mac.default_speeds;
435 if (*link_speeds & ETH_LINK_SPEED_1G)
436 speed |= NGBE_LINK_SPEED_1GB_FULL;
437 if (*link_speeds & ETH_LINK_SPEED_100M)
438 speed |= NGBE_LINK_SPEED_100M_FULL;
439 if (*link_speeds & ETH_LINK_SPEED_10M)
440 speed |= NGBE_LINK_SPEED_10M_FULL;
444 err = hw->mac.setup_link(hw, speed, link_up);
448 if (rte_intr_allow_others(intr_handle)) {
449 ngbe_dev_misc_interrupt_setup(dev);
450 /* check if lsc interrupt is enabled */
451 if (dev->data->dev_conf.intr_conf.lsc != 0)
452 ngbe_dev_lsc_interrupt_setup(dev, TRUE);
454 ngbe_dev_lsc_interrupt_setup(dev, FALSE);
455 ngbe_dev_macsec_interrupt_setup(dev);
456 ngbe_set_ivar_map(hw, -1, 1, NGBE_MISC_VEC_ID);
458 rte_intr_callback_unregister(intr_handle,
459 ngbe_dev_interrupt_handler, dev);
460 if (dev->data->dev_conf.intr_conf.lsc != 0)
462 "LSC won't enable because of no intr multiplex");
465 /* check if rxq interrupt is enabled */
466 if (dev->data->dev_conf.intr_conf.rxq != 0 &&
467 rte_intr_dp_is_en(intr_handle))
468 ngbe_dev_rxq_interrupt_setup(dev);
470 /* enable UIO/VFIO intr/eventfd mapping */
471 rte_intr_enable(intr_handle);
473 /* resume enabled intr since HW reset */
474 ngbe_enable_intr(dev);
476 if ((hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_M88E1512_SFP ||
477 (hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_YT8521S_SFP) {
478 /* gpio0 is used to power on/off control*/
479 wr32(hw, NGBE_GPIODATA, 0);
483 * Update link status right before return, because it may
484 * start link configuration process in a separate thread.
486 ngbe_dev_link_update(dev, 0);
491 PMD_INIT_LOG(ERR, "failure in dev start: %d", err);
492 ngbe_dev_clear_queues(dev);
497 * Stop device: disable rx and tx functions to allow for reconfiguring.
500 ngbe_dev_stop(struct rte_eth_dev *dev)
502 struct rte_eth_link link;
503 struct ngbe_hw *hw = ngbe_dev_hw(dev);
504 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
505 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
507 if (hw->adapter_stopped)
510 PMD_INIT_FUNC_TRACE();
512 if ((hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_M88E1512_SFP ||
513 (hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_YT8521S_SFP) {
514 /* gpio0 is used to power on/off control*/
515 wr32(hw, NGBE_GPIODATA, NGBE_GPIOBIT_0);
518 /* disable interrupts */
519 ngbe_disable_intr(hw);
522 ngbe_pf_reset_hw(hw);
523 hw->adapter_stopped = 0;
528 ngbe_dev_clear_queues(dev);
530 /* Clear recorded link status */
531 memset(&link, 0, sizeof(link));
532 rte_eth_linkstatus_set(dev, &link);
534 if (!rte_intr_allow_others(intr_handle))
535 /* resume to the default handler */
536 rte_intr_callback_register(intr_handle,
537 ngbe_dev_interrupt_handler,
540 /* Clean datapath event and queue/vec mapping */
541 rte_intr_efd_disable(intr_handle);
542 if (intr_handle->intr_vec != NULL) {
543 rte_free(intr_handle->intr_vec);
544 intr_handle->intr_vec = NULL;
547 hw->adapter_stopped = true;
548 dev->data->dev_started = 0;
554 * Reset and stop device.
557 ngbe_dev_close(struct rte_eth_dev *dev)
559 PMD_INIT_FUNC_TRACE();
567 ngbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
569 struct ngbe_hw *hw = ngbe_dev_hw(dev);
571 dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
572 dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
573 dev_info->min_rx_bufsize = 1024;
574 dev_info->max_rx_pktlen = 15872;
576 dev_info->default_rxconf = (struct rte_eth_rxconf) {
578 .pthresh = NGBE_DEFAULT_RX_PTHRESH,
579 .hthresh = NGBE_DEFAULT_RX_HTHRESH,
580 .wthresh = NGBE_DEFAULT_RX_WTHRESH,
582 .rx_free_thresh = NGBE_DEFAULT_RX_FREE_THRESH,
587 dev_info->default_txconf = (struct rte_eth_txconf) {
589 .pthresh = NGBE_DEFAULT_TX_PTHRESH,
590 .hthresh = NGBE_DEFAULT_TX_HTHRESH,
591 .wthresh = NGBE_DEFAULT_TX_WTHRESH,
593 .tx_free_thresh = NGBE_DEFAULT_TX_FREE_THRESH,
597 dev_info->rx_desc_lim = rx_desc_lim;
598 dev_info->tx_desc_lim = tx_desc_lim;
600 dev_info->speed_capa = ETH_LINK_SPEED_1G | ETH_LINK_SPEED_100M |
603 /* Driver-preferred Rx/Tx parameters */
604 dev_info->default_rxportconf.burst_size = 32;
605 dev_info->default_txportconf.burst_size = 32;
606 dev_info->default_rxportconf.nb_queues = 1;
607 dev_info->default_txportconf.nb_queues = 1;
608 dev_info->default_rxportconf.ring_size = 256;
609 dev_info->default_txportconf.ring_size = 256;
614 /* return 0 means link status changed, -1 means not changed */
616 ngbe_dev_link_update_share(struct rte_eth_dev *dev,
617 int wait_to_complete)
619 struct ngbe_hw *hw = ngbe_dev_hw(dev);
620 struct rte_eth_link link;
621 u32 link_speed = NGBE_LINK_SPEED_UNKNOWN;
623 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
628 memset(&link, 0, sizeof(link));
629 link.link_status = ETH_LINK_DOWN;
630 link.link_speed = ETH_SPEED_NUM_NONE;
631 link.link_duplex = ETH_LINK_HALF_DUPLEX;
632 link.link_autoneg = !(dev->data->dev_conf.link_speeds &
633 ~ETH_LINK_SPEED_AUTONEG);
635 hw->mac.get_link_status = true;
637 if (intr->flags & NGBE_FLAG_NEED_LINK_CONFIG)
638 return rte_eth_linkstatus_set(dev, &link);
640 /* check if it needs to wait to complete, if lsc interrupt is enabled */
641 if (wait_to_complete == 0 || dev->data->dev_conf.intr_conf.lsc != 0)
644 err = hw->mac.check_link(hw, &link_speed, &link_up, wait);
646 link.link_speed = ETH_SPEED_NUM_NONE;
647 link.link_duplex = ETH_LINK_FULL_DUPLEX;
648 return rte_eth_linkstatus_set(dev, &link);
652 return rte_eth_linkstatus_set(dev, &link);
654 intr->flags &= ~NGBE_FLAG_NEED_LINK_CONFIG;
655 link.link_status = ETH_LINK_UP;
656 link.link_duplex = ETH_LINK_FULL_DUPLEX;
658 switch (link_speed) {
660 case NGBE_LINK_SPEED_UNKNOWN:
661 link.link_speed = ETH_SPEED_NUM_NONE;
664 case NGBE_LINK_SPEED_10M_FULL:
665 link.link_speed = ETH_SPEED_NUM_10M;
669 case NGBE_LINK_SPEED_100M_FULL:
670 link.link_speed = ETH_SPEED_NUM_100M;
674 case NGBE_LINK_SPEED_1GB_FULL:
675 link.link_speed = ETH_SPEED_NUM_1G;
681 wr32m(hw, NGBE_LAN_SPEED, NGBE_LAN_SPEED_MASK, lan_speed);
682 if (link_speed & (NGBE_LINK_SPEED_1GB_FULL |
683 NGBE_LINK_SPEED_100M_FULL |
684 NGBE_LINK_SPEED_10M_FULL)) {
685 wr32m(hw, NGBE_MACTXCFG, NGBE_MACTXCFG_SPEED_MASK,
686 NGBE_MACTXCFG_SPEED_1G | NGBE_MACTXCFG_TE);
690 return rte_eth_linkstatus_set(dev, &link);
694 ngbe_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
696 return ngbe_dev_link_update_share(dev, wait_to_complete);
700 * It clears the interrupt causes and enables the interrupt.
701 * It will be called once only during NIC initialized.
704 * Pointer to struct rte_eth_dev.
709 * - On success, zero.
710 * - On failure, a negative value.
713 ngbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on)
715 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
717 ngbe_dev_link_status_print(dev);
719 intr->mask_misc |= NGBE_ICRMISC_PHY;
720 intr->mask_misc |= NGBE_ICRMISC_GPIO;
722 intr->mask_misc &= ~NGBE_ICRMISC_PHY;
723 intr->mask_misc &= ~NGBE_ICRMISC_GPIO;
730 * It clears the interrupt causes and enables the interrupt.
731 * It will be called once only during NIC initialized.
734 * Pointer to struct rte_eth_dev.
737 * - On success, zero.
738 * - On failure, a negative value.
741 ngbe_dev_misc_interrupt_setup(struct rte_eth_dev *dev)
743 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
746 mask = NGBE_ICR_MASK;
747 mask &= (1ULL << NGBE_MISC_VEC_ID);
749 intr->mask_misc |= NGBE_ICRMISC_GPIO;
755 * It clears the interrupt causes and enables the interrupt.
756 * It will be called once only during NIC initialized.
759 * Pointer to struct rte_eth_dev.
762 * - On success, zero.
763 * - On failure, a negative value.
766 ngbe_dev_rxq_interrupt_setup(struct rte_eth_dev *dev)
768 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
771 mask = NGBE_ICR_MASK;
772 mask &= ~((1ULL << NGBE_RX_VEC_START) - 1);
779 * It clears the interrupt causes and enables the interrupt.
780 * It will be called once only during NIC initialized.
783 * Pointer to struct rte_eth_dev.
786 * - On success, zero.
787 * - On failure, a negative value.
790 ngbe_dev_macsec_interrupt_setup(struct rte_eth_dev *dev)
792 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
794 intr->mask_misc |= NGBE_ICRMISC_LNKSEC;
800 * It reads ICR and sets flag for the link_update.
803 * Pointer to struct rte_eth_dev.
806 * - On success, zero.
807 * - On failure, a negative value.
810 ngbe_dev_interrupt_get_status(struct rte_eth_dev *dev)
813 struct ngbe_hw *hw = ngbe_dev_hw(dev);
814 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
816 /* clear all cause mask */
817 ngbe_disable_intr(hw);
819 /* read-on-clear nic registers here */
820 eicr = ((u32 *)hw->isb_mem)[NGBE_ISB_MISC];
821 PMD_DRV_LOG(DEBUG, "eicr %x", eicr);
825 /* set flag for async link update */
826 if (eicr & NGBE_ICRMISC_PHY)
827 intr->flags |= NGBE_FLAG_NEED_LINK_UPDATE;
829 if (eicr & NGBE_ICRMISC_VFMBX)
830 intr->flags |= NGBE_FLAG_MAILBOX;
832 if (eicr & NGBE_ICRMISC_LNKSEC)
833 intr->flags |= NGBE_FLAG_MACSEC;
835 if (eicr & NGBE_ICRMISC_GPIO)
836 intr->flags |= NGBE_FLAG_NEED_LINK_UPDATE;
842 * It gets and then prints the link status.
845 * Pointer to struct rte_eth_dev.
848 * - On success, zero.
849 * - On failure, a negative value.
852 ngbe_dev_link_status_print(struct rte_eth_dev *dev)
854 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
855 struct rte_eth_link link;
857 rte_eth_linkstatus_get(dev, &link);
859 if (link.link_status == ETH_LINK_UP) {
860 PMD_INIT_LOG(INFO, "Port %d: Link Up - speed %u Mbps - %s",
861 (int)(dev->data->port_id),
862 (unsigned int)link.link_speed,
863 link.link_duplex == ETH_LINK_FULL_DUPLEX ?
864 "full-duplex" : "half-duplex");
866 PMD_INIT_LOG(INFO, " Port %d: Link Down",
867 (int)(dev->data->port_id));
869 PMD_INIT_LOG(DEBUG, "PCI Address: " PCI_PRI_FMT,
870 pci_dev->addr.domain,
873 pci_dev->addr.function);
877 * It executes link_update after knowing an interrupt occurred.
880 * Pointer to struct rte_eth_dev.
883 * - On success, zero.
884 * - On failure, a negative value.
887 ngbe_dev_interrupt_action(struct rte_eth_dev *dev)
889 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
892 PMD_DRV_LOG(DEBUG, "intr action type %d", intr->flags);
894 if (intr->flags & NGBE_FLAG_NEED_LINK_UPDATE) {
895 struct rte_eth_link link;
897 /*get the link status before link update, for predicting later*/
898 rte_eth_linkstatus_get(dev, &link);
900 ngbe_dev_link_update(dev, 0);
903 if (link.link_status != ETH_LINK_UP)
904 /* handle it 1 sec later, wait it being stable */
905 timeout = NGBE_LINK_UP_CHECK_TIMEOUT;
908 /* handle it 4 sec later, wait it being stable */
909 timeout = NGBE_LINK_DOWN_CHECK_TIMEOUT;
911 ngbe_dev_link_status_print(dev);
912 if (rte_eal_alarm_set(timeout * 1000,
913 ngbe_dev_interrupt_delayed_handler,
915 PMD_DRV_LOG(ERR, "Error setting alarm");
917 /* remember original mask */
918 intr->mask_misc_orig = intr->mask_misc;
919 /* only disable lsc interrupt */
920 intr->mask_misc &= ~NGBE_ICRMISC_PHY;
922 intr->mask_orig = intr->mask;
923 /* only disable all misc interrupts */
924 intr->mask &= ~(1ULL << NGBE_MISC_VEC_ID);
928 PMD_DRV_LOG(DEBUG, "enable intr immediately");
929 ngbe_enable_intr(dev);
935 * Interrupt handler which shall be registered for alarm callback for delayed
936 * handling specific interrupt to wait for the stable nic state. As the
937 * NIC interrupt state is not stable for ngbe after link is just down,
938 * it needs to wait 4 seconds to get the stable status.
941 * The address of parameter (struct rte_eth_dev *) registered before.
944 ngbe_dev_interrupt_delayed_handler(void *param)
946 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
947 struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
948 struct ngbe_hw *hw = ngbe_dev_hw(dev);
951 ngbe_disable_intr(hw);
953 eicr = ((u32 *)hw->isb_mem)[NGBE_ISB_MISC];
955 if (intr->flags & NGBE_FLAG_NEED_LINK_UPDATE) {
956 ngbe_dev_link_update(dev, 0);
957 intr->flags &= ~NGBE_FLAG_NEED_LINK_UPDATE;
958 ngbe_dev_link_status_print(dev);
959 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC,
963 if (intr->flags & NGBE_FLAG_MACSEC) {
964 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_MACSEC,
966 intr->flags &= ~NGBE_FLAG_MACSEC;
969 /* restore original mask */
970 intr->mask_misc = intr->mask_misc_orig;
971 intr->mask_misc_orig = 0;
972 intr->mask = intr->mask_orig;
975 PMD_DRV_LOG(DEBUG, "enable intr in delayed handler S[%08x]", eicr);
976 ngbe_enable_intr(dev);
980 * Interrupt handler triggered by NIC for handling
981 * specific interrupt.
984 * The address of parameter (struct rte_eth_dev *) registered before.
987 ngbe_dev_interrupt_handler(void *param)
989 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
991 ngbe_dev_interrupt_get_status(dev);
992 ngbe_dev_interrupt_action(dev);
996 * Set the IVAR registers, mapping interrupt causes to vectors
998 * pointer to ngbe_hw struct
1000 * 0 for Rx, 1 for Tx, -1 for other causes
1002 * queue to map the corresponding interrupt to
1004 * the vector to map to the corresponding queue
1007 ngbe_set_ivar_map(struct ngbe_hw *hw, int8_t direction,
1008 uint8_t queue, uint8_t msix_vector)
1012 if (direction == -1) {
1014 msix_vector |= NGBE_IVARMISC_VLD;
1016 tmp = rd32(hw, NGBE_IVARMISC);
1017 tmp &= ~(0xFF << idx);
1018 tmp |= (msix_vector << idx);
1019 wr32(hw, NGBE_IVARMISC, tmp);
1021 /* rx or tx causes */
1022 /* Workround for ICR lost */
1023 idx = ((16 * (queue & 1)) + (8 * direction));
1024 tmp = rd32(hw, NGBE_IVAR(queue >> 1));
1025 tmp &= ~(0xFF << idx);
1026 tmp |= (msix_vector << idx);
1027 wr32(hw, NGBE_IVAR(queue >> 1), tmp);
1032 * Sets up the hardware to properly generate MSI-X interrupts
1034 * board private structure
1037 ngbe_configure_msix(struct rte_eth_dev *dev)
1039 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1040 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1041 struct ngbe_hw *hw = ngbe_dev_hw(dev);
1042 uint32_t queue_id, base = NGBE_MISC_VEC_ID;
1043 uint32_t vec = NGBE_MISC_VEC_ID;
1047 * Won't configure MSI-X register if no mapping is done
1048 * between intr vector and event fd
1049 * but if MSI-X has been enabled already, need to configure
1050 * auto clean, auto mask and throttling.
1052 gpie = rd32(hw, NGBE_GPIE);
1053 if (!rte_intr_dp_is_en(intr_handle) &&
1054 !(gpie & NGBE_GPIE_MSIX))
1057 if (rte_intr_allow_others(intr_handle)) {
1058 base = NGBE_RX_VEC_START;
1062 /* setup GPIE for MSI-X mode */
1063 gpie = rd32(hw, NGBE_GPIE);
1064 gpie |= NGBE_GPIE_MSIX;
1065 wr32(hw, NGBE_GPIE, gpie);
1067 /* Populate the IVAR table and set the ITR values to the
1068 * corresponding register.
1070 if (rte_intr_dp_is_en(intr_handle)) {
1071 for (queue_id = 0; queue_id < dev->data->nb_rx_queues;
1073 /* by default, 1:1 mapping */
1074 ngbe_set_ivar_map(hw, 0, queue_id, vec);
1075 intr_handle->intr_vec[queue_id] = vec;
1076 if (vec < base + intr_handle->nb_efd - 1)
1080 ngbe_set_ivar_map(hw, -1, 1, NGBE_MISC_VEC_ID);
1082 wr32(hw, NGBE_ITR(NGBE_MISC_VEC_ID),
1083 NGBE_ITR_IVAL_1G(NGBE_QUEUE_ITR_INTERVAL_DEFAULT)
1087 static const struct eth_dev_ops ngbe_eth_dev_ops = {
1088 .dev_configure = ngbe_dev_configure,
1089 .dev_infos_get = ngbe_dev_info_get,
1090 .dev_start = ngbe_dev_start,
1091 .dev_stop = ngbe_dev_stop,
1092 .link_update = ngbe_dev_link_update,
1093 .rx_queue_start = ngbe_dev_rx_queue_start,
1094 .rx_queue_stop = ngbe_dev_rx_queue_stop,
1095 .tx_queue_start = ngbe_dev_tx_queue_start,
1096 .tx_queue_stop = ngbe_dev_tx_queue_stop,
1097 .rx_queue_setup = ngbe_dev_rx_queue_setup,
1098 .rx_queue_release = ngbe_dev_rx_queue_release,
1099 .tx_queue_setup = ngbe_dev_tx_queue_setup,
1100 .tx_queue_release = ngbe_dev_tx_queue_release,
1103 RTE_PMD_REGISTER_PCI(net_ngbe, rte_ngbe_pmd);
1104 RTE_PMD_REGISTER_PCI_TABLE(net_ngbe, pci_id_ngbe_map);
1105 RTE_PMD_REGISTER_KMOD_DEP(net_ngbe, "* igb_uio | uio_pci_generic | vfio-pci");
1107 RTE_LOG_REGISTER_SUFFIX(ngbe_logtype_init, init, NOTICE);
1108 RTE_LOG_REGISTER_SUFFIX(ngbe_logtype_driver, driver, NOTICE);
1110 #ifdef RTE_ETHDEV_DEBUG_RX
1111 RTE_LOG_REGISTER_SUFFIX(ngbe_logtype_rx, rx, DEBUG);
1113 #ifdef RTE_ETHDEV_DEBUG_TX
1114 RTE_LOG_REGISTER_SUFFIX(ngbe_logtype_tx, tx, DEBUG);