a70a860f6084c3e933894ca6f337420147d005bf
[dpdk.git] / drivers / net / ngbe / ngbe_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2021 Beijing WangXun Technology Co., Ltd.
3  * Copyright(c) 2010-2017 Intel Corporation
4  */
5
6 #include <errno.h>
7 #include <rte_common.h>
8 #include <ethdev_pci.h>
9
10 #include <rte_alarm.h>
11
12 #include "ngbe_logs.h"
13 #include "ngbe.h"
14 #include "ngbe_ethdev.h"
15 #include "ngbe_rxtx.h"
16
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);
20
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);
29
30 /*
31  * The set of PCI devices this driver supports
32  */
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 */ },
47 };
48
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,
53 };
54
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,
61 };
62
63 static const struct eth_dev_ops ngbe_eth_dev_ops;
64
65 static inline int32_t
66 ngbe_pf_reset_hw(struct ngbe_hw *hw)
67 {
68         uint32_t ctrl_ext;
69         int32_t status;
70
71         status = hw->mac.reset_hw(hw);
72
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);
77         ngbe_flush(hw);
78
79         if (status == NGBE_ERR_SFP_NOT_PRESENT)
80                 status = 0;
81         return status;
82 }
83
84 static inline void
85 ngbe_enable_intr(struct rte_eth_dev *dev)
86 {
87         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
88         struct ngbe_hw *hw = ngbe_dev_hw(dev);
89
90         wr32(hw, NGBE_IENMISC, intr->mask_misc);
91         wr32(hw, NGBE_IMC(0), intr->mask & BIT_MASK32);
92         ngbe_flush(hw);
93 }
94
95 static void
96 ngbe_disable_intr(struct ngbe_hw *hw)
97 {
98         PMD_INIT_FUNC_TRACE();
99
100         wr32(hw, NGBE_IMS(0), NGBE_IMS_MASK);
101         ngbe_flush(hw);
102 }
103
104 /*
105  * Ensure that all locks are released before first NVM or PHY access
106  */
107 static void
108 ngbe_swfw_lock_reset(struct ngbe_hw *hw)
109 {
110         uint16_t mask;
111
112         /*
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
116          * semaphore.
117          */
118         mask = NGBE_MNGSEM_SWPHY |
119                NGBE_MNGSEM_SWMBX |
120                NGBE_MNGSEM_SWFLASH;
121         if (hw->mac.acquire_swfw_sync(hw, mask) < 0)
122                 PMD_DRV_LOG(DEBUG, "SWFW common locks released");
123
124         hw->mac.release_swfw_sync(hw, mask);
125 }
126
127 static int
128 eth_ngbe_dev_init(struct rte_eth_dev *eth_dev, void *init_params __rte_unused)
129 {
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;
134         uint32_t ctrl_ext;
135         int err;
136
137         PMD_INIT_FUNC_TRACE();
138
139         eth_dev->dev_ops = &ngbe_eth_dev_ops;
140
141         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
142                 return 0;
143
144         rte_eth_copy_pci_info(eth_dev, pci_dev);
145
146         /* Vendor and Device ID need to be set before init of shared code */
147         hw->device_id = pci_dev->id.device_id;
148         hw->vendor_id = pci_dev->id.vendor_id;
149         hw->sub_system_id = pci_dev->id.subsystem_device_id;
150         ngbe_map_device_id(hw);
151         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
152
153         /* Reserve memory for interrupt status block */
154         mz = rte_eth_dma_zone_reserve(eth_dev, "ngbe_driver", -1,
155                 NGBE_ISB_SIZE, NGBE_ALIGN, SOCKET_ID_ANY);
156         if (mz == NULL)
157                 return -ENOMEM;
158
159         hw->isb_dma = TMZ_PADDR(mz);
160         hw->isb_mem = TMZ_VADDR(mz);
161
162         /* Initialize the shared code (base driver) */
163         err = ngbe_init_shared_code(hw);
164         if (err != 0) {
165                 PMD_INIT_LOG(ERR, "Shared code init failed: %d", err);
166                 return -EIO;
167         }
168
169         /* Unlock any pending hardware semaphore */
170         ngbe_swfw_lock_reset(hw);
171
172         err = hw->rom.init_params(hw);
173         if (err != 0) {
174                 PMD_INIT_LOG(ERR, "The EEPROM init failed: %d", err);
175                 return -EIO;
176         }
177
178         /* Make sure we have a good EEPROM before we read from it */
179         err = hw->rom.validate_checksum(hw, NULL);
180         if (err != 0) {
181                 PMD_INIT_LOG(ERR, "The EEPROM checksum is not valid: %d", err);
182                 return -EIO;
183         }
184
185         err = hw->mac.init_hw(hw);
186         if (err != 0) {
187                 PMD_INIT_LOG(ERR, "Hardware Initialization Failure: %d", err);
188                 return -EIO;
189         }
190
191         /* disable interrupt */
192         ngbe_disable_intr(hw);
193
194         /* Allocate memory for storing MAC addresses */
195         eth_dev->data->mac_addrs = rte_zmalloc("ngbe", RTE_ETHER_ADDR_LEN *
196                                                hw->mac.num_rar_entries, 0);
197         if (eth_dev->data->mac_addrs == NULL) {
198                 PMD_INIT_LOG(ERR,
199                              "Failed to allocate %u bytes needed to store MAC addresses",
200                              RTE_ETHER_ADDR_LEN * hw->mac.num_rar_entries);
201                 return -ENOMEM;
202         }
203
204         /* Copy the permanent MAC address */
205         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.perm_addr,
206                         &eth_dev->data->mac_addrs[0]);
207
208         /* Allocate memory for storing hash filter MAC addresses */
209         eth_dev->data->hash_mac_addrs = rte_zmalloc("ngbe",
210                         RTE_ETHER_ADDR_LEN * NGBE_VMDQ_NUM_UC_MAC, 0);
211         if (eth_dev->data->hash_mac_addrs == NULL) {
212                 PMD_INIT_LOG(ERR,
213                              "Failed to allocate %d bytes needed to store MAC addresses",
214                              RTE_ETHER_ADDR_LEN * NGBE_VMDQ_NUM_UC_MAC);
215                 rte_free(eth_dev->data->mac_addrs);
216                 eth_dev->data->mac_addrs = NULL;
217                 return -ENOMEM;
218         }
219
220         ctrl_ext = rd32(hw, NGBE_PORTCTL);
221         /* let hardware know driver is loaded */
222         ctrl_ext |= NGBE_PORTCTL_DRVLOAD;
223         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
224         ctrl_ext |= NGBE_PORTCTL_RSTDONE;
225         wr32(hw, NGBE_PORTCTL, ctrl_ext);
226         ngbe_flush(hw);
227
228         PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d",
229                         (int)hw->mac.type, (int)hw->phy.type);
230
231         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
232                      eth_dev->data->port_id, pci_dev->id.vendor_id,
233                      pci_dev->id.device_id);
234
235         rte_intr_callback_register(intr_handle,
236                                    ngbe_dev_interrupt_handler, eth_dev);
237
238         /* enable uio/vfio intr/eventfd mapping */
239         rte_intr_enable(intr_handle);
240
241         /* enable support intr */
242         ngbe_enable_intr(eth_dev);
243
244         return 0;
245 }
246
247 static int
248 eth_ngbe_dev_uninit(struct rte_eth_dev *eth_dev)
249 {
250         PMD_INIT_FUNC_TRACE();
251
252         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
253                 return 0;
254
255         ngbe_dev_close(eth_dev);
256
257         return -EINVAL;
258 }
259
260 static int
261 eth_ngbe_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
262                 struct rte_pci_device *pci_dev)
263 {
264         return rte_eth_dev_create(&pci_dev->device, pci_dev->device.name,
265                         sizeof(struct ngbe_adapter),
266                         eth_dev_pci_specific_init, pci_dev,
267                         eth_ngbe_dev_init, NULL);
268 }
269
270 static int eth_ngbe_pci_remove(struct rte_pci_device *pci_dev)
271 {
272         struct rte_eth_dev *ethdev;
273
274         ethdev = rte_eth_dev_allocated(pci_dev->device.name);
275         if (ethdev == NULL)
276                 return 0;
277
278         return rte_eth_dev_destroy(ethdev, eth_ngbe_dev_uninit);
279 }
280
281 static struct rte_pci_driver rte_ngbe_pmd = {
282         .id_table = pci_id_ngbe_map,
283         .drv_flags = RTE_PCI_DRV_NEED_MAPPING |
284                      RTE_PCI_DRV_INTR_LSC,
285         .probe = eth_ngbe_pci_probe,
286         .remove = eth_ngbe_pci_remove,
287 };
288
289 static int
290 ngbe_dev_configure(struct rte_eth_dev *dev)
291 {
292         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
293         struct ngbe_adapter *adapter = ngbe_dev_adapter(dev);
294
295         PMD_INIT_FUNC_TRACE();
296
297         /* set flag to update link status after init */
298         intr->flags |= NGBE_FLAG_NEED_LINK_UPDATE;
299
300         /*
301          * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
302          * allocation Rx preconditions we will reset it.
303          */
304         adapter->rx_bulk_alloc_allowed = true;
305
306         return 0;
307 }
308
309 static void
310 ngbe_dev_phy_intr_setup(struct rte_eth_dev *dev)
311 {
312         struct ngbe_hw *hw = ngbe_dev_hw(dev);
313         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
314
315         wr32(hw, NGBE_GPIODIR, NGBE_GPIODIR_DDR(1));
316         wr32(hw, NGBE_GPIOINTEN, NGBE_GPIOINTEN_INT(3));
317         wr32(hw, NGBE_GPIOINTTYPE, NGBE_GPIOINTTYPE_LEVEL(0));
318         if (hw->phy.type == ngbe_phy_yt8521s_sfi)
319                 wr32(hw, NGBE_GPIOINTPOL, NGBE_GPIOINTPOL_ACT(0));
320         else
321                 wr32(hw, NGBE_GPIOINTPOL, NGBE_GPIOINTPOL_ACT(3));
322
323         intr->mask_misc |= NGBE_ICRMISC_GPIO;
324 }
325
326 /*
327  * Configure device link speed and setup link.
328  * It returns 0 on success.
329  */
330 static int
331 ngbe_dev_start(struct rte_eth_dev *dev)
332 {
333         struct ngbe_hw *hw = ngbe_dev_hw(dev);
334         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
335         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
336         uint32_t intr_vector = 0;
337         int err;
338         bool link_up = false, negotiate = false;
339         uint32_t speed = 0;
340         uint32_t allowed_speeds = 0;
341         int status;
342         uint32_t *link_speeds;
343
344         PMD_INIT_FUNC_TRACE();
345
346         /* disable uio/vfio intr/eventfd mapping */
347         rte_intr_disable(intr_handle);
348
349         /* stop adapter */
350         hw->adapter_stopped = 0;
351         ngbe_stop_hw(hw);
352
353         /* reinitialize adapter, this calls reset and start */
354         hw->nb_rx_queues = dev->data->nb_rx_queues;
355         hw->nb_tx_queues = dev->data->nb_tx_queues;
356         status = ngbe_pf_reset_hw(hw);
357         if (status != 0)
358                 return -1;
359         hw->mac.start_hw(hw);
360         hw->mac.get_link_status = true;
361
362         ngbe_dev_phy_intr_setup(dev);
363
364         /* check and configure queue intr-vector mapping */
365         if ((rte_intr_cap_multiple(intr_handle) ||
366              !RTE_ETH_DEV_SRIOV(dev).active) &&
367             dev->data->dev_conf.intr_conf.rxq != 0) {
368                 intr_vector = dev->data->nb_rx_queues;
369                 if (rte_intr_efd_enable(intr_handle, intr_vector))
370                         return -1;
371         }
372
373         if (rte_intr_dp_is_en(intr_handle) && intr_handle->intr_vec == NULL) {
374                 intr_handle->intr_vec =
375                         rte_zmalloc("intr_vec",
376                                     dev->data->nb_rx_queues * sizeof(int), 0);
377                 if (intr_handle->intr_vec == NULL) {
378                         PMD_INIT_LOG(ERR,
379                                      "Failed to allocate %d rx_queues intr_vec",
380                                      dev->data->nb_rx_queues);
381                         return -ENOMEM;
382                 }
383         }
384
385         /* confiugre MSI-X for sleep until Rx interrupt */
386         ngbe_configure_msix(dev);
387
388         /* initialize transmission unit */
389         ngbe_dev_tx_init(dev);
390
391         /* This can fail when allocating mbufs for descriptor rings */
392         err = ngbe_dev_rx_init(dev);
393         if (err != 0) {
394                 PMD_INIT_LOG(ERR, "Unable to initialize Rx hardware");
395                 goto error;
396         }
397
398         err = ngbe_dev_rxtx_start(dev);
399         if (err < 0) {
400                 PMD_INIT_LOG(ERR, "Unable to start rxtx queues");
401                 goto error;
402         }
403
404         err = hw->mac.check_link(hw, &speed, &link_up, 0);
405         if (err != 0)
406                 goto error;
407         dev->data->dev_link.link_status = link_up;
408
409         link_speeds = &dev->data->dev_conf.link_speeds;
410         if (*link_speeds == ETH_LINK_SPEED_AUTONEG)
411                 negotiate = true;
412
413         err = hw->mac.get_link_capabilities(hw, &speed, &negotiate);
414         if (err != 0)
415                 goto error;
416
417         allowed_speeds = 0;
418         if (hw->mac.default_speeds & NGBE_LINK_SPEED_1GB_FULL)
419                 allowed_speeds |= ETH_LINK_SPEED_1G;
420         if (hw->mac.default_speeds & NGBE_LINK_SPEED_100M_FULL)
421                 allowed_speeds |= ETH_LINK_SPEED_100M;
422         if (hw->mac.default_speeds & NGBE_LINK_SPEED_10M_FULL)
423                 allowed_speeds |= ETH_LINK_SPEED_10M;
424
425         if (*link_speeds & ~allowed_speeds) {
426                 PMD_INIT_LOG(ERR, "Invalid link setting");
427                 goto error;
428         }
429
430         speed = 0x0;
431         if (*link_speeds == ETH_LINK_SPEED_AUTONEG) {
432                 speed = hw->mac.default_speeds;
433         } else {
434                 if (*link_speeds & ETH_LINK_SPEED_1G)
435                         speed |= NGBE_LINK_SPEED_1GB_FULL;
436                 if (*link_speeds & ETH_LINK_SPEED_100M)
437                         speed |= NGBE_LINK_SPEED_100M_FULL;
438                 if (*link_speeds & ETH_LINK_SPEED_10M)
439                         speed |= NGBE_LINK_SPEED_10M_FULL;
440         }
441
442         hw->phy.init_hw(hw);
443         err = hw->mac.setup_link(hw, speed, link_up);
444         if (err != 0)
445                 goto error;
446
447         if (rte_intr_allow_others(intr_handle)) {
448                 ngbe_dev_misc_interrupt_setup(dev);
449                 /* check if lsc interrupt is enabled */
450                 if (dev->data->dev_conf.intr_conf.lsc != 0)
451                         ngbe_dev_lsc_interrupt_setup(dev, TRUE);
452                 else
453                         ngbe_dev_lsc_interrupt_setup(dev, FALSE);
454                 ngbe_dev_macsec_interrupt_setup(dev);
455                 ngbe_set_ivar_map(hw, -1, 1, NGBE_MISC_VEC_ID);
456         } else {
457                 rte_intr_callback_unregister(intr_handle,
458                                              ngbe_dev_interrupt_handler, dev);
459                 if (dev->data->dev_conf.intr_conf.lsc != 0)
460                         PMD_INIT_LOG(INFO,
461                                      "LSC won't enable because of no intr multiplex");
462         }
463
464         /* check if rxq interrupt is enabled */
465         if (dev->data->dev_conf.intr_conf.rxq != 0 &&
466             rte_intr_dp_is_en(intr_handle))
467                 ngbe_dev_rxq_interrupt_setup(dev);
468
469         /* enable UIO/VFIO intr/eventfd mapping */
470         rte_intr_enable(intr_handle);
471
472         /* resume enabled intr since HW reset */
473         ngbe_enable_intr(dev);
474
475         if ((hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_M88E1512_SFP ||
476                 (hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_YT8521S_SFP) {
477                 /* gpio0 is used to power on/off control*/
478                 wr32(hw, NGBE_GPIODATA, 0);
479         }
480
481         /*
482          * Update link status right before return, because it may
483          * start link configuration process in a separate thread.
484          */
485         ngbe_dev_link_update(dev, 0);
486
487         return 0;
488
489 error:
490         PMD_INIT_LOG(ERR, "failure in dev start: %d", err);
491         ngbe_dev_clear_queues(dev);
492         return -EIO;
493 }
494
495 /*
496  * Stop device: disable rx and tx functions to allow for reconfiguring.
497  */
498 static int
499 ngbe_dev_stop(struct rte_eth_dev *dev)
500 {
501         struct rte_eth_link link;
502         struct ngbe_hw *hw = ngbe_dev_hw(dev);
503         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
504         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
505
506         if (hw->adapter_stopped)
507                 return 0;
508
509         PMD_INIT_FUNC_TRACE();
510
511         if ((hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_M88E1512_SFP ||
512                 (hw->sub_system_id & NGBE_OEM_MASK) == NGBE_LY_YT8521S_SFP) {
513                 /* gpio0 is used to power on/off control*/
514                 wr32(hw, NGBE_GPIODATA, NGBE_GPIOBIT_0);
515         }
516
517         /* disable interrupts */
518         ngbe_disable_intr(hw);
519
520         /* reset the NIC */
521         ngbe_pf_reset_hw(hw);
522         hw->adapter_stopped = 0;
523
524         /* stop adapter */
525         ngbe_stop_hw(hw);
526
527         ngbe_dev_clear_queues(dev);
528
529         /* Clear recorded link status */
530         memset(&link, 0, sizeof(link));
531         rte_eth_linkstatus_set(dev, &link);
532
533         if (!rte_intr_allow_others(intr_handle))
534                 /* resume to the default handler */
535                 rte_intr_callback_register(intr_handle,
536                                            ngbe_dev_interrupt_handler,
537                                            (void *)dev);
538
539         /* Clean datapath event and queue/vec mapping */
540         rte_intr_efd_disable(intr_handle);
541         if (intr_handle->intr_vec != NULL) {
542                 rte_free(intr_handle->intr_vec);
543                 intr_handle->intr_vec = NULL;
544         }
545
546         hw->adapter_stopped = true;
547         dev->data->dev_started = 0;
548
549         return 0;
550 }
551
552 /*
553  * Reset and stop device.
554  */
555 static int
556 ngbe_dev_close(struct rte_eth_dev *dev)
557 {
558         PMD_INIT_FUNC_TRACE();
559
560         RTE_SET_USED(dev);
561
562         return -EINVAL;
563 }
564
565 static int
566 ngbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
567 {
568         struct ngbe_hw *hw = ngbe_dev_hw(dev);
569
570         dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
571         dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
572
573         dev_info->default_rxconf = (struct rte_eth_rxconf) {
574                 .rx_thresh = {
575                         .pthresh = NGBE_DEFAULT_RX_PTHRESH,
576                         .hthresh = NGBE_DEFAULT_RX_HTHRESH,
577                         .wthresh = NGBE_DEFAULT_RX_WTHRESH,
578                 },
579                 .rx_free_thresh = NGBE_DEFAULT_RX_FREE_THRESH,
580                 .rx_drop_en = 0,
581                 .offloads = 0,
582         };
583
584         dev_info->default_txconf = (struct rte_eth_txconf) {
585                 .tx_thresh = {
586                         .pthresh = NGBE_DEFAULT_TX_PTHRESH,
587                         .hthresh = NGBE_DEFAULT_TX_HTHRESH,
588                         .wthresh = NGBE_DEFAULT_TX_WTHRESH,
589                 },
590                 .tx_free_thresh = NGBE_DEFAULT_TX_FREE_THRESH,
591                 .offloads = 0,
592         };
593
594         dev_info->rx_desc_lim = rx_desc_lim;
595         dev_info->tx_desc_lim = tx_desc_lim;
596
597         dev_info->speed_capa = ETH_LINK_SPEED_1G | ETH_LINK_SPEED_100M |
598                                 ETH_LINK_SPEED_10M;
599
600         /* Driver-preferred Rx/Tx parameters */
601         dev_info->default_txportconf.burst_size = 32;
602         dev_info->default_rxportconf.nb_queues = 1;
603         dev_info->default_txportconf.nb_queues = 1;
604         dev_info->default_rxportconf.ring_size = 256;
605         dev_info->default_txportconf.ring_size = 256;
606
607         return 0;
608 }
609
610 /* return 0 means link status changed, -1 means not changed */
611 int
612 ngbe_dev_link_update_share(struct rte_eth_dev *dev,
613                             int wait_to_complete)
614 {
615         struct ngbe_hw *hw = ngbe_dev_hw(dev);
616         struct rte_eth_link link;
617         u32 link_speed = NGBE_LINK_SPEED_UNKNOWN;
618         u32 lan_speed = 0;
619         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
620         bool link_up;
621         int err;
622         int wait = 1;
623
624         memset(&link, 0, sizeof(link));
625         link.link_status = ETH_LINK_DOWN;
626         link.link_speed = ETH_SPEED_NUM_NONE;
627         link.link_duplex = ETH_LINK_HALF_DUPLEX;
628         link.link_autoneg = !(dev->data->dev_conf.link_speeds &
629                         ~ETH_LINK_SPEED_AUTONEG);
630
631         hw->mac.get_link_status = true;
632
633         if (intr->flags & NGBE_FLAG_NEED_LINK_CONFIG)
634                 return rte_eth_linkstatus_set(dev, &link);
635
636         /* check if it needs to wait to complete, if lsc interrupt is enabled */
637         if (wait_to_complete == 0 || dev->data->dev_conf.intr_conf.lsc != 0)
638                 wait = 0;
639
640         err = hw->mac.check_link(hw, &link_speed, &link_up, wait);
641         if (err != 0) {
642                 link.link_speed = ETH_SPEED_NUM_NONE;
643                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
644                 return rte_eth_linkstatus_set(dev, &link);
645         }
646
647         if (!link_up)
648                 return rte_eth_linkstatus_set(dev, &link);
649
650         intr->flags &= ~NGBE_FLAG_NEED_LINK_CONFIG;
651         link.link_status = ETH_LINK_UP;
652         link.link_duplex = ETH_LINK_FULL_DUPLEX;
653
654         switch (link_speed) {
655         default:
656         case NGBE_LINK_SPEED_UNKNOWN:
657                 link.link_speed = ETH_SPEED_NUM_NONE;
658                 break;
659
660         case NGBE_LINK_SPEED_10M_FULL:
661                 link.link_speed = ETH_SPEED_NUM_10M;
662                 lan_speed = 0;
663                 break;
664
665         case NGBE_LINK_SPEED_100M_FULL:
666                 link.link_speed = ETH_SPEED_NUM_100M;
667                 lan_speed = 1;
668                 break;
669
670         case NGBE_LINK_SPEED_1GB_FULL:
671                 link.link_speed = ETH_SPEED_NUM_1G;
672                 lan_speed = 2;
673                 break;
674         }
675
676         if (hw->is_pf) {
677                 wr32m(hw, NGBE_LAN_SPEED, NGBE_LAN_SPEED_MASK, lan_speed);
678                 if (link_speed & (NGBE_LINK_SPEED_1GB_FULL |
679                                 NGBE_LINK_SPEED_100M_FULL |
680                                 NGBE_LINK_SPEED_10M_FULL)) {
681                         wr32m(hw, NGBE_MACTXCFG, NGBE_MACTXCFG_SPEED_MASK,
682                                 NGBE_MACTXCFG_SPEED_1G | NGBE_MACTXCFG_TE);
683                 }
684         }
685
686         return rte_eth_linkstatus_set(dev, &link);
687 }
688
689 static int
690 ngbe_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
691 {
692         return ngbe_dev_link_update_share(dev, wait_to_complete);
693 }
694
695 /**
696  * It clears the interrupt causes and enables the interrupt.
697  * It will be called once only during NIC initialized.
698  *
699  * @param dev
700  *  Pointer to struct rte_eth_dev.
701  * @param on
702  *  Enable or Disable.
703  *
704  * @return
705  *  - On success, zero.
706  *  - On failure, a negative value.
707  */
708 static int
709 ngbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on)
710 {
711         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
712
713         ngbe_dev_link_status_print(dev);
714         if (on != 0) {
715                 intr->mask_misc |= NGBE_ICRMISC_PHY;
716                 intr->mask_misc |= NGBE_ICRMISC_GPIO;
717         } else {
718                 intr->mask_misc &= ~NGBE_ICRMISC_PHY;
719                 intr->mask_misc &= ~NGBE_ICRMISC_GPIO;
720         }
721
722         return 0;
723 }
724
725 /**
726  * It clears the interrupt causes and enables the interrupt.
727  * It will be called once only during NIC initialized.
728  *
729  * @param dev
730  *  Pointer to struct rte_eth_dev.
731  *
732  * @return
733  *  - On success, zero.
734  *  - On failure, a negative value.
735  */
736 static int
737 ngbe_dev_misc_interrupt_setup(struct rte_eth_dev *dev)
738 {
739         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
740         u64 mask;
741
742         mask = NGBE_ICR_MASK;
743         mask &= (1ULL << NGBE_MISC_VEC_ID);
744         intr->mask |= mask;
745         intr->mask_misc |= NGBE_ICRMISC_GPIO;
746
747         return 0;
748 }
749
750 /**
751  * It clears the interrupt causes and enables the interrupt.
752  * It will be called once only during NIC initialized.
753  *
754  * @param dev
755  *  Pointer to struct rte_eth_dev.
756  *
757  * @return
758  *  - On success, zero.
759  *  - On failure, a negative value.
760  */
761 static int
762 ngbe_dev_rxq_interrupt_setup(struct rte_eth_dev *dev)
763 {
764         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
765         u64 mask;
766
767         mask = NGBE_ICR_MASK;
768         mask &= ~((1ULL << NGBE_RX_VEC_START) - 1);
769         intr->mask |= mask;
770
771         return 0;
772 }
773
774 /**
775  * It clears the interrupt causes and enables the interrupt.
776  * It will be called once only during NIC initialized.
777  *
778  * @param dev
779  *  Pointer to struct rte_eth_dev.
780  *
781  * @return
782  *  - On success, zero.
783  *  - On failure, a negative value.
784  */
785 static int
786 ngbe_dev_macsec_interrupt_setup(struct rte_eth_dev *dev)
787 {
788         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
789
790         intr->mask_misc |= NGBE_ICRMISC_LNKSEC;
791
792         return 0;
793 }
794
795 /*
796  * It reads ICR and sets flag for the link_update.
797  *
798  * @param dev
799  *  Pointer to struct rte_eth_dev.
800  *
801  * @return
802  *  - On success, zero.
803  *  - On failure, a negative value.
804  */
805 static int
806 ngbe_dev_interrupt_get_status(struct rte_eth_dev *dev)
807 {
808         uint32_t eicr;
809         struct ngbe_hw *hw = ngbe_dev_hw(dev);
810         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
811
812         /* clear all cause mask */
813         ngbe_disable_intr(hw);
814
815         /* read-on-clear nic registers here */
816         eicr = ((u32 *)hw->isb_mem)[NGBE_ISB_MISC];
817         PMD_DRV_LOG(DEBUG, "eicr %x", eicr);
818
819         intr->flags = 0;
820
821         /* set flag for async link update */
822         if (eicr & NGBE_ICRMISC_PHY)
823                 intr->flags |= NGBE_FLAG_NEED_LINK_UPDATE;
824
825         if (eicr & NGBE_ICRMISC_VFMBX)
826                 intr->flags |= NGBE_FLAG_MAILBOX;
827
828         if (eicr & NGBE_ICRMISC_LNKSEC)
829                 intr->flags |= NGBE_FLAG_MACSEC;
830
831         if (eicr & NGBE_ICRMISC_GPIO)
832                 intr->flags |= NGBE_FLAG_NEED_LINK_UPDATE;
833
834         return 0;
835 }
836
837 /**
838  * It gets and then prints the link status.
839  *
840  * @param dev
841  *  Pointer to struct rte_eth_dev.
842  *
843  * @return
844  *  - On success, zero.
845  *  - On failure, a negative value.
846  */
847 static void
848 ngbe_dev_link_status_print(struct rte_eth_dev *dev)
849 {
850         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
851         struct rte_eth_link link;
852
853         rte_eth_linkstatus_get(dev, &link);
854
855         if (link.link_status == ETH_LINK_UP) {
856                 PMD_INIT_LOG(INFO, "Port %d: Link Up - speed %u Mbps - %s",
857                                         (int)(dev->data->port_id),
858                                         (unsigned int)link.link_speed,
859                         link.link_duplex == ETH_LINK_FULL_DUPLEX ?
860                                         "full-duplex" : "half-duplex");
861         } else {
862                 PMD_INIT_LOG(INFO, " Port %d: Link Down",
863                                 (int)(dev->data->port_id));
864         }
865         PMD_INIT_LOG(DEBUG, "PCI Address: " PCI_PRI_FMT,
866                                 pci_dev->addr.domain,
867                                 pci_dev->addr.bus,
868                                 pci_dev->addr.devid,
869                                 pci_dev->addr.function);
870 }
871
872 /*
873  * It executes link_update after knowing an interrupt occurred.
874  *
875  * @param dev
876  *  Pointer to struct rte_eth_dev.
877  *
878  * @return
879  *  - On success, zero.
880  *  - On failure, a negative value.
881  */
882 static int
883 ngbe_dev_interrupt_action(struct rte_eth_dev *dev)
884 {
885         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
886         int64_t timeout;
887
888         PMD_DRV_LOG(DEBUG, "intr action type %d", intr->flags);
889
890         if (intr->flags & NGBE_FLAG_NEED_LINK_UPDATE) {
891                 struct rte_eth_link link;
892
893                 /*get the link status before link update, for predicting later*/
894                 rte_eth_linkstatus_get(dev, &link);
895
896                 ngbe_dev_link_update(dev, 0);
897
898                 /* likely to up */
899                 if (link.link_status != ETH_LINK_UP)
900                         /* handle it 1 sec later, wait it being stable */
901                         timeout = NGBE_LINK_UP_CHECK_TIMEOUT;
902                 /* likely to down */
903                 else
904                         /* handle it 4 sec later, wait it being stable */
905                         timeout = NGBE_LINK_DOWN_CHECK_TIMEOUT;
906
907                 ngbe_dev_link_status_print(dev);
908                 if (rte_eal_alarm_set(timeout * 1000,
909                                       ngbe_dev_interrupt_delayed_handler,
910                                       (void *)dev) < 0) {
911                         PMD_DRV_LOG(ERR, "Error setting alarm");
912                 } else {
913                         /* remember original mask */
914                         intr->mask_misc_orig = intr->mask_misc;
915                         /* only disable lsc interrupt */
916                         intr->mask_misc &= ~NGBE_ICRMISC_PHY;
917
918                         intr->mask_orig = intr->mask;
919                         /* only disable all misc interrupts */
920                         intr->mask &= ~(1ULL << NGBE_MISC_VEC_ID);
921                 }
922         }
923
924         PMD_DRV_LOG(DEBUG, "enable intr immediately");
925         ngbe_enable_intr(dev);
926
927         return 0;
928 }
929
930 /**
931  * Interrupt handler which shall be registered for alarm callback for delayed
932  * handling specific interrupt to wait for the stable nic state. As the
933  * NIC interrupt state is not stable for ngbe after link is just down,
934  * it needs to wait 4 seconds to get the stable status.
935  *
936  * @param param
937  *  The address of parameter (struct rte_eth_dev *) registered before.
938  */
939 static void
940 ngbe_dev_interrupt_delayed_handler(void *param)
941 {
942         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
943         struct ngbe_interrupt *intr = ngbe_dev_intr(dev);
944         struct ngbe_hw *hw = ngbe_dev_hw(dev);
945         uint32_t eicr;
946
947         ngbe_disable_intr(hw);
948
949         eicr = ((u32 *)hw->isb_mem)[NGBE_ISB_MISC];
950
951         if (intr->flags & NGBE_FLAG_NEED_LINK_UPDATE) {
952                 ngbe_dev_link_update(dev, 0);
953                 intr->flags &= ~NGBE_FLAG_NEED_LINK_UPDATE;
954                 ngbe_dev_link_status_print(dev);
955                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC,
956                                               NULL);
957         }
958
959         if (intr->flags & NGBE_FLAG_MACSEC) {
960                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_MACSEC,
961                                               NULL);
962                 intr->flags &= ~NGBE_FLAG_MACSEC;
963         }
964
965         /* restore original mask */
966         intr->mask_misc = intr->mask_misc_orig;
967         intr->mask_misc_orig = 0;
968         intr->mask = intr->mask_orig;
969         intr->mask_orig = 0;
970
971         PMD_DRV_LOG(DEBUG, "enable intr in delayed handler S[%08x]", eicr);
972         ngbe_enable_intr(dev);
973 }
974
975 /**
976  * Interrupt handler triggered by NIC  for handling
977  * specific interrupt.
978  *
979  * @param param
980  *  The address of parameter (struct rte_eth_dev *) registered before.
981  */
982 static void
983 ngbe_dev_interrupt_handler(void *param)
984 {
985         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
986
987         ngbe_dev_interrupt_get_status(dev);
988         ngbe_dev_interrupt_action(dev);
989 }
990
991 /**
992  * Set the IVAR registers, mapping interrupt causes to vectors
993  * @param hw
994  *  pointer to ngbe_hw struct
995  * @direction
996  *  0 for Rx, 1 for Tx, -1 for other causes
997  * @queue
998  *  queue to map the corresponding interrupt to
999  * @msix_vector
1000  *  the vector to map to the corresponding queue
1001  */
1002 void
1003 ngbe_set_ivar_map(struct ngbe_hw *hw, int8_t direction,
1004                    uint8_t queue, uint8_t msix_vector)
1005 {
1006         uint32_t tmp, idx;
1007
1008         if (direction == -1) {
1009                 /* other causes */
1010                 msix_vector |= NGBE_IVARMISC_VLD;
1011                 idx = 0;
1012                 tmp = rd32(hw, NGBE_IVARMISC);
1013                 tmp &= ~(0xFF << idx);
1014                 tmp |= (msix_vector << idx);
1015                 wr32(hw, NGBE_IVARMISC, tmp);
1016         } else {
1017                 /* rx or tx causes */
1018                 /* Workround for ICR lost */
1019                 idx = ((16 * (queue & 1)) + (8 * direction));
1020                 tmp = rd32(hw, NGBE_IVAR(queue >> 1));
1021                 tmp &= ~(0xFF << idx);
1022                 tmp |= (msix_vector << idx);
1023                 wr32(hw, NGBE_IVAR(queue >> 1), tmp);
1024         }
1025 }
1026
1027 /**
1028  * Sets up the hardware to properly generate MSI-X interrupts
1029  * @hw
1030  *  board private structure
1031  */
1032 static void
1033 ngbe_configure_msix(struct rte_eth_dev *dev)
1034 {
1035         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1036         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1037         struct ngbe_hw *hw = ngbe_dev_hw(dev);
1038         uint32_t queue_id, base = NGBE_MISC_VEC_ID;
1039         uint32_t vec = NGBE_MISC_VEC_ID;
1040         uint32_t gpie;
1041
1042         /*
1043          * Won't configure MSI-X register if no mapping is done
1044          * between intr vector and event fd
1045          * but if MSI-X has been enabled already, need to configure
1046          * auto clean, auto mask and throttling.
1047          */
1048         gpie = rd32(hw, NGBE_GPIE);
1049         if (!rte_intr_dp_is_en(intr_handle) &&
1050             !(gpie & NGBE_GPIE_MSIX))
1051                 return;
1052
1053         if (rte_intr_allow_others(intr_handle)) {
1054                 base = NGBE_RX_VEC_START;
1055                 vec = base;
1056         }
1057
1058         /* setup GPIE for MSI-X mode */
1059         gpie = rd32(hw, NGBE_GPIE);
1060         gpie |= NGBE_GPIE_MSIX;
1061         wr32(hw, NGBE_GPIE, gpie);
1062
1063         /* Populate the IVAR table and set the ITR values to the
1064          * corresponding register.
1065          */
1066         if (rte_intr_dp_is_en(intr_handle)) {
1067                 for (queue_id = 0; queue_id < dev->data->nb_rx_queues;
1068                         queue_id++) {
1069                         /* by default, 1:1 mapping */
1070                         ngbe_set_ivar_map(hw, 0, queue_id, vec);
1071                         intr_handle->intr_vec[queue_id] = vec;
1072                         if (vec < base + intr_handle->nb_efd - 1)
1073                                 vec++;
1074                 }
1075
1076                 ngbe_set_ivar_map(hw, -1, 1, NGBE_MISC_VEC_ID);
1077         }
1078         wr32(hw, NGBE_ITR(NGBE_MISC_VEC_ID),
1079                         NGBE_ITR_IVAL_1G(NGBE_QUEUE_ITR_INTERVAL_DEFAULT)
1080                         | NGBE_ITR_WRDSA);
1081 }
1082
1083 static const struct eth_dev_ops ngbe_eth_dev_ops = {
1084         .dev_configure              = ngbe_dev_configure,
1085         .dev_infos_get              = ngbe_dev_info_get,
1086         .dev_start                  = ngbe_dev_start,
1087         .dev_stop                   = ngbe_dev_stop,
1088         .link_update                = ngbe_dev_link_update,
1089         .tx_queue_start             = ngbe_dev_tx_queue_start,
1090         .tx_queue_stop              = ngbe_dev_tx_queue_stop,
1091         .rx_queue_setup             = ngbe_dev_rx_queue_setup,
1092         .rx_queue_release           = ngbe_dev_rx_queue_release,
1093         .tx_queue_setup             = ngbe_dev_tx_queue_setup,
1094         .tx_queue_release           = ngbe_dev_tx_queue_release,
1095 };
1096
1097 RTE_PMD_REGISTER_PCI(net_ngbe, rte_ngbe_pmd);
1098 RTE_PMD_REGISTER_PCI_TABLE(net_ngbe, pci_id_ngbe_map);
1099 RTE_PMD_REGISTER_KMOD_DEP(net_ngbe, "* igb_uio | uio_pci_generic | vfio-pci");
1100
1101 RTE_LOG_REGISTER_SUFFIX(ngbe_logtype_init, init, NOTICE);
1102 RTE_LOG_REGISTER_SUFFIX(ngbe_logtype_driver, driver, NOTICE);
1103
1104 #ifdef RTE_ETHDEV_DEBUG_RX
1105         RTE_LOG_REGISTER_SUFFIX(ngbe_logtype_rx, rx, DEBUG);
1106 #endif
1107 #ifdef RTE_ETHDEV_DEBUG_TX
1108         RTE_LOG_REGISTER_SUFFIX(ngbe_logtype_tx, tx, DEBUG);
1109 #endif