5223b57ebd839ec77617d14d695104e9a461beb7
[dpdk.git] / drivers / net / atlantic / atl_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Aquantia Corporation
3  */
4
5 #include <rte_ethdev_pci.h>
6
7 #include "atl_ethdev.h"
8 #include "atl_common.h"
9 #include "atl_hw_regs.h"
10 #include "atl_logs.h"
11 #include "hw_atl/hw_atl_llh.h"
12 #include "hw_atl/hw_atl_b0.h"
13 #include "hw_atl/hw_atl_b0_internal.h"
14
15 static int eth_atl_dev_init(struct rte_eth_dev *eth_dev);
16 static int eth_atl_dev_uninit(struct rte_eth_dev *eth_dev);
17
18 static int  atl_dev_configure(struct rte_eth_dev *dev);
19 static int  atl_dev_start(struct rte_eth_dev *dev);
20 static void atl_dev_stop(struct rte_eth_dev *dev);
21 static int  atl_dev_set_link_up(struct rte_eth_dev *dev);
22 static int  atl_dev_set_link_down(struct rte_eth_dev *dev);
23 static void atl_dev_close(struct rte_eth_dev *dev);
24 static int  atl_dev_reset(struct rte_eth_dev *dev);
25 static void atl_dev_promiscuous_enable(struct rte_eth_dev *dev);
26 static void atl_dev_promiscuous_disable(struct rte_eth_dev *dev);
27 static void atl_dev_allmulticast_enable(struct rte_eth_dev *dev);
28 static void atl_dev_allmulticast_disable(struct rte_eth_dev *dev);
29 static int  atl_dev_link_update(struct rte_eth_dev *dev, int wait);
30
31 static int atl_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
32                                     struct rte_eth_xstat_name *xstats_names,
33                                     unsigned int size);
34
35 static int atl_dev_stats_get(struct rte_eth_dev *dev,
36                                 struct rte_eth_stats *stats);
37
38 static int atl_dev_xstats_get(struct rte_eth_dev *dev,
39                               struct rte_eth_xstat *stats, unsigned int n);
40
41 static void atl_dev_stats_reset(struct rte_eth_dev *dev);
42
43 static int atl_fw_version_get(struct rte_eth_dev *dev, char *fw_version,
44                               size_t fw_size);
45
46 static void atl_dev_info_get(struct rte_eth_dev *dev,
47                                struct rte_eth_dev_info *dev_info);
48
49 static const uint32_t *atl_dev_supported_ptypes_get(struct rte_eth_dev *dev);
50
51 static void atl_dev_link_status_print(struct rte_eth_dev *dev);
52
53 /* Interrupts */
54 static int atl_dev_rxq_interrupt_setup(struct rte_eth_dev *dev);
55 static int atl_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on);
56 static int atl_dev_interrupt_get_status(struct rte_eth_dev *dev);
57 static int atl_dev_interrupt_action(struct rte_eth_dev *dev,
58                                     struct rte_intr_handle *handle);
59 static void atl_dev_interrupt_handler(void *param);
60
61 static int eth_atl_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
62         struct rte_pci_device *pci_dev);
63 static int eth_atl_pci_remove(struct rte_pci_device *pci_dev);
64
65 static void atl_dev_info_get(struct rte_eth_dev *dev,
66                                 struct rte_eth_dev_info *dev_info);
67
68 int atl_logtype_init;
69 int atl_logtype_driver;
70
71 /*
72  * The set of PCI devices this driver supports
73  */
74 static const struct rte_pci_id pci_id_atl_map[] = {
75         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_0001) },
76         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D100) },
77         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D107) },
78         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D108) },
79         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D109) },
80
81         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC100) },
82         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC107) },
83         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC108) },
84         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC109) },
85         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC111) },
86         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC112) },
87
88         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC100S) },
89         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC107S) },
90         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC108S) },
91         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC109S) },
92         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC111S) },
93         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC112S) },
94
95         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC111E) },
96         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC112E) },
97         { .vendor_id = 0, /* sentinel */ },
98 };
99
100 static struct rte_pci_driver rte_atl_pmd = {
101         .id_table = pci_id_atl_map,
102         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
103                      RTE_PCI_DRV_IOVA_AS_VA,
104         .probe = eth_atl_pci_probe,
105         .remove = eth_atl_pci_remove,
106 };
107
108 #define ATL_RX_OFFLOADS (DEV_RX_OFFLOAD_VLAN_STRIP \
109                         | DEV_RX_OFFLOAD_IPV4_CKSUM \
110                         | DEV_RX_OFFLOAD_UDP_CKSUM \
111                         | DEV_RX_OFFLOAD_TCP_CKSUM \
112                         | DEV_RX_OFFLOAD_JUMBO_FRAME)
113
114 #define ATL_TX_OFFLOADS (DEV_TX_OFFLOAD_VLAN_INSERT \
115                         | DEV_TX_OFFLOAD_IPV4_CKSUM \
116                         | DEV_TX_OFFLOAD_UDP_CKSUM \
117                         | DEV_TX_OFFLOAD_TCP_CKSUM \
118                         | DEV_TX_OFFLOAD_TCP_TSO \
119                         | DEV_TX_OFFLOAD_MULTI_SEGS)
120
121 static const struct rte_eth_desc_lim rx_desc_lim = {
122         .nb_max = ATL_MAX_RING_DESC,
123         .nb_min = ATL_MIN_RING_DESC,
124         .nb_align = ATL_RXD_ALIGN,
125 };
126
127 static const struct rte_eth_desc_lim tx_desc_lim = {
128         .nb_max = ATL_MAX_RING_DESC,
129         .nb_min = ATL_MIN_RING_DESC,
130         .nb_align = ATL_TXD_ALIGN,
131         .nb_seg_max = ATL_TX_MAX_SEG,
132         .nb_mtu_seg_max = ATL_TX_MAX_SEG,
133 };
134
135 #define ATL_XSTATS_FIELD(name) { \
136         #name, \
137         offsetof(struct aq_stats_s, name) \
138 }
139
140 struct atl_xstats_tbl_s {
141         const char *name;
142         unsigned int offset;
143 };
144
145 static struct atl_xstats_tbl_s atl_xstats_tbl[] = {
146         ATL_XSTATS_FIELD(uprc),
147         ATL_XSTATS_FIELD(mprc),
148         ATL_XSTATS_FIELD(bprc),
149         ATL_XSTATS_FIELD(erpt),
150         ATL_XSTATS_FIELD(uptc),
151         ATL_XSTATS_FIELD(mptc),
152         ATL_XSTATS_FIELD(bptc),
153         ATL_XSTATS_FIELD(erpr),
154         ATL_XSTATS_FIELD(ubrc),
155         ATL_XSTATS_FIELD(ubtc),
156         ATL_XSTATS_FIELD(mbrc),
157         ATL_XSTATS_FIELD(mbtc),
158         ATL_XSTATS_FIELD(bbrc),
159         ATL_XSTATS_FIELD(bbtc),
160 };
161
162 static const struct eth_dev_ops atl_eth_dev_ops = {
163         .dev_configure        = atl_dev_configure,
164         .dev_start            = atl_dev_start,
165         .dev_stop             = atl_dev_stop,
166         .dev_set_link_up      = atl_dev_set_link_up,
167         .dev_set_link_down    = atl_dev_set_link_down,
168         .dev_close            = atl_dev_close,
169         .dev_reset            = atl_dev_reset,
170
171         /* PROMISC */
172         .promiscuous_enable   = atl_dev_promiscuous_enable,
173         .promiscuous_disable  = atl_dev_promiscuous_disable,
174         .allmulticast_enable  = atl_dev_allmulticast_enable,
175         .allmulticast_disable = atl_dev_allmulticast_disable,
176
177         /* Link */
178         .link_update          = atl_dev_link_update,
179
180         /* Stats */
181         .stats_get            = atl_dev_stats_get,
182         .xstats_get           = atl_dev_xstats_get,
183         .xstats_get_names     = atl_dev_xstats_get_names,
184         .stats_reset          = atl_dev_stats_reset,
185         .xstats_reset         = atl_dev_stats_reset,
186
187         .fw_version_get       = atl_fw_version_get,
188         .dev_infos_get        = atl_dev_info_get,
189         .dev_supported_ptypes_get = atl_dev_supported_ptypes_get,
190
191         /* Queue Control */
192         .rx_queue_start       = atl_rx_queue_start,
193         .rx_queue_stop        = atl_rx_queue_stop,
194         .rx_queue_setup       = atl_rx_queue_setup,
195         .rx_queue_release     = atl_rx_queue_release,
196
197         .tx_queue_start       = atl_tx_queue_start,
198         .tx_queue_stop        = atl_tx_queue_stop,
199         .tx_queue_setup       = atl_tx_queue_setup,
200         .tx_queue_release     = atl_tx_queue_release,
201
202         .rx_queue_intr_enable = atl_dev_rx_queue_intr_enable,
203         .rx_queue_intr_disable = atl_dev_rx_queue_intr_disable,
204
205         .rx_queue_count       = atl_rx_queue_count,
206         .rx_descriptor_status = atl_dev_rx_descriptor_status,
207         .tx_descriptor_status = atl_dev_tx_descriptor_status,
208
209         .rxq_info_get         = atl_rxq_info_get,
210         .txq_info_get         = atl_txq_info_get,
211 };
212
213 static inline int32_t
214 atl_reset_hw(struct aq_hw_s *hw)
215 {
216         return hw_atl_b0_hw_reset(hw);
217 }
218
219 static inline void
220 atl_enable_intr(struct rte_eth_dev *dev)
221 {
222         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
223
224         hw_atl_itr_irq_msk_setlsw_set(hw, 0xffffffff);
225 }
226
227 static void
228 atl_disable_intr(struct aq_hw_s *hw)
229 {
230         PMD_INIT_FUNC_TRACE();
231         hw_atl_itr_irq_msk_clearlsw_set(hw, 0xffffffff);
232 }
233
234 static int
235 eth_atl_dev_init(struct rte_eth_dev *eth_dev)
236 {
237         struct atl_adapter *adapter =
238                 (struct atl_adapter *)eth_dev->data->dev_private;
239         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
240         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
241         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
242         int err = 0;
243
244         PMD_INIT_FUNC_TRACE();
245
246         eth_dev->dev_ops = &atl_eth_dev_ops;
247         eth_dev->rx_pkt_burst = &atl_recv_pkts;
248         eth_dev->tx_pkt_burst = &atl_xmit_pkts;
249         eth_dev->tx_pkt_prepare = &atl_prep_pkts;
250
251         /* For secondary processes, the primary process has done all the work */
252         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
253                 return 0;
254
255         /* Vendor and Device ID need to be set before init of shared code */
256         hw->device_id = pci_dev->id.device_id;
257         hw->vendor_id = pci_dev->id.vendor_id;
258         hw->mmio = (void *)pci_dev->mem_resource[0].addr;
259
260         /* Hardware configuration - hardcode */
261         adapter->hw_cfg.is_lro = false;
262         adapter->hw_cfg.wol = false;
263         adapter->hw_cfg.link_speed_msk = AQ_NIC_RATE_10G |
264                           AQ_NIC_RATE_5G |
265                           AQ_NIC_RATE_2G5 |
266                           AQ_NIC_RATE_1G |
267                           AQ_NIC_RATE_100M;
268
269         hw->aq_nic_cfg = &adapter->hw_cfg;
270
271         /* disable interrupt */
272         atl_disable_intr(hw);
273
274         /* Allocate memory for storing MAC addresses */
275         eth_dev->data->mac_addrs = rte_zmalloc("atlantic", ETHER_ADDR_LEN, 0);
276         if (eth_dev->data->mac_addrs == NULL) {
277                 PMD_INIT_LOG(ERR, "MAC Malloc failed");
278                 return -ENOMEM;
279         }
280
281         err = hw_atl_utils_initfw(hw, &hw->aq_fw_ops);
282         if (err)
283                 return err;
284
285         /* Copy the permanent MAC address */
286         if (hw->aq_fw_ops->get_mac_permanent(hw,
287                         eth_dev->data->mac_addrs->addr_bytes) != 0)
288                 return -EINVAL;
289
290         /* Reset the hw statistics */
291         atl_dev_stats_reset(eth_dev);
292
293         rte_intr_callback_register(intr_handle,
294                                    atl_dev_interrupt_handler, eth_dev);
295
296         /* enable uio/vfio intr/eventfd mapping */
297         rte_intr_enable(intr_handle);
298
299         /* enable support intr */
300         atl_enable_intr(eth_dev);
301
302         return err;
303 }
304
305 static int
306 eth_atl_dev_uninit(struct rte_eth_dev *eth_dev)
307 {
308         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
309         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
310         struct aq_hw_s *hw;
311
312         PMD_INIT_FUNC_TRACE();
313
314         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
315                 return -EPERM;
316
317         hw = ATL_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
318
319         if (hw->adapter_stopped == 0)
320                 atl_dev_close(eth_dev);
321
322         eth_dev->dev_ops = NULL;
323         eth_dev->rx_pkt_burst = NULL;
324         eth_dev->tx_pkt_burst = NULL;
325
326         /* disable uio intr before callback unregister */
327         rte_intr_disable(intr_handle);
328         rte_intr_callback_unregister(intr_handle,
329                                      atl_dev_interrupt_handler, eth_dev);
330
331         rte_free(eth_dev->data->mac_addrs);
332         eth_dev->data->mac_addrs = NULL;
333
334         return 0;
335 }
336
337 static int
338 eth_atl_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
339         struct rte_pci_device *pci_dev)
340 {
341         return rte_eth_dev_pci_generic_probe(pci_dev,
342                 sizeof(struct atl_adapter), eth_atl_dev_init);
343 }
344
345 static int
346 eth_atl_pci_remove(struct rte_pci_device *pci_dev)
347 {
348         return rte_eth_dev_pci_generic_remove(pci_dev, eth_atl_dev_uninit);
349 }
350
351 static int
352 atl_dev_configure(struct rte_eth_dev *dev)
353 {
354         struct atl_interrupt *intr =
355                 ATL_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
356
357         PMD_INIT_FUNC_TRACE();
358
359         /* set flag to update link status after init */
360         intr->flags |= ATL_FLAG_NEED_LINK_UPDATE;
361
362         return 0;
363 }
364
365 /*
366  * Configure device link speed and setup link.
367  * It returns 0 on success.
368  */
369 static int
370 atl_dev_start(struct rte_eth_dev *dev)
371 {
372         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
373         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
374         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
375         uint32_t intr_vector = 0;
376         uint32_t *link_speeds;
377         uint32_t speed = 0;
378         int status;
379         int err;
380
381         PMD_INIT_FUNC_TRACE();
382
383         /* set adapter started */
384         hw->adapter_stopped = 0;
385
386         if (dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED) {
387                 PMD_INIT_LOG(ERR,
388                 "Invalid link_speeds for port %u, fix speed not supported",
389                                 dev->data->port_id);
390                 return -EINVAL;
391         }
392
393         /* disable uio/vfio intr/eventfd mapping */
394         rte_intr_disable(intr_handle);
395
396         /* reinitialize adapter
397          * this calls reset and start
398          */
399         status = atl_reset_hw(hw);
400         if (status != 0)
401                 return -EIO;
402
403         err = hw_atl_b0_hw_init(hw, dev->data->mac_addrs->addr_bytes);
404
405         hw_atl_b0_hw_start(hw);
406         /* check and configure queue intr-vector mapping */
407         if ((rte_intr_cap_multiple(intr_handle) ||
408             !RTE_ETH_DEV_SRIOV(dev).active) &&
409             dev->data->dev_conf.intr_conf.rxq != 0) {
410                 intr_vector = dev->data->nb_rx_queues;
411                 if (intr_vector > ATL_MAX_INTR_QUEUE_NUM) {
412                         PMD_INIT_LOG(ERR, "At most %d intr queues supported",
413                                         ATL_MAX_INTR_QUEUE_NUM);
414                         return -ENOTSUP;
415                 }
416                 if (rte_intr_efd_enable(intr_handle, intr_vector)) {
417                         PMD_INIT_LOG(ERR, "rte_intr_efd_enable failed");
418                         return -1;
419                 }
420         }
421
422         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
423                 intr_handle->intr_vec = rte_zmalloc("intr_vec",
424                                     dev->data->nb_rx_queues * sizeof(int), 0);
425                 if (intr_handle->intr_vec == NULL) {
426                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
427                                      " intr_vec", dev->data->nb_rx_queues);
428                         return -ENOMEM;
429                 }
430         }
431
432         /* initialize transmission unit */
433         atl_tx_init(dev);
434
435         /* This can fail when allocating mbufs for descriptor rings */
436         err = atl_rx_init(dev);
437         if (err) {
438                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware");
439                 goto error;
440         }
441
442         PMD_INIT_LOG(DEBUG, "FW version: %u.%u.%u",
443                 hw->fw_ver_actual >> 24,
444                 (hw->fw_ver_actual >> 16) & 0xFF,
445                 hw->fw_ver_actual & 0xFFFF);
446         PMD_INIT_LOG(DEBUG, "Driver version: %s", ATL_PMD_DRIVER_VERSION);
447
448         err = atl_start_queues(dev);
449         if (err < 0) {
450                 PMD_INIT_LOG(ERR, "Unable to start rxtx queues");
451                 goto error;
452         }
453
454         err = hw->aq_fw_ops->update_link_status(hw);
455
456         if (err)
457                 goto error;
458
459         dev->data->dev_link.link_status = hw->aq_link_status.mbps != 0;
460
461         link_speeds = &dev->data->dev_conf.link_speeds;
462
463         speed = 0x0;
464
465         if (*link_speeds == ETH_LINK_SPEED_AUTONEG) {
466                 speed = hw->aq_nic_cfg->link_speed_msk;
467         } else {
468                 if (*link_speeds & ETH_LINK_SPEED_10G)
469                         speed |= AQ_NIC_RATE_10G;
470                 if (*link_speeds & ETH_LINK_SPEED_5G)
471                         speed |= AQ_NIC_RATE_5G;
472                 if (*link_speeds & ETH_LINK_SPEED_1G)
473                         speed |= AQ_NIC_RATE_1G;
474                 if (*link_speeds & ETH_LINK_SPEED_2_5G)
475                         speed |=  AQ_NIC_RATE_2G5;
476                 if (*link_speeds & ETH_LINK_SPEED_100M)
477                         speed |= AQ_NIC_RATE_100M;
478         }
479
480         err = hw->aq_fw_ops->set_link_speed(hw, speed);
481         if (err)
482                 goto error;
483
484         if (rte_intr_allow_others(intr_handle)) {
485                 /* check if lsc interrupt is enabled */
486                 if (dev->data->dev_conf.intr_conf.lsc != 0)
487                         atl_dev_lsc_interrupt_setup(dev, true);
488                 else
489                         atl_dev_lsc_interrupt_setup(dev, false);
490         } else {
491                 rte_intr_callback_unregister(intr_handle,
492                                              atl_dev_interrupt_handler, dev);
493                 if (dev->data->dev_conf.intr_conf.lsc != 0)
494                         PMD_INIT_LOG(INFO, "lsc won't enable because of"
495                                      " no intr multiplex");
496         }
497
498         /* check if rxq interrupt is enabled */
499         if (dev->data->dev_conf.intr_conf.rxq != 0 &&
500             rte_intr_dp_is_en(intr_handle))
501                 atl_dev_rxq_interrupt_setup(dev);
502
503         /* enable uio/vfio intr/eventfd mapping */
504         rte_intr_enable(intr_handle);
505
506         /* resume enabled intr since hw reset */
507         atl_enable_intr(dev);
508
509         return 0;
510
511 error:
512         atl_stop_queues(dev);
513         return -EIO;
514 }
515
516 /*
517  * Stop device: disable rx and tx functions to allow for reconfiguring.
518  */
519 static void
520 atl_dev_stop(struct rte_eth_dev *dev)
521 {
522         struct rte_eth_link link;
523         struct aq_hw_s *hw =
524                 ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
525         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
526         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
527
528         PMD_INIT_FUNC_TRACE();
529
530         /* disable interrupts */
531         atl_disable_intr(hw);
532
533         /* reset the NIC */
534         atl_reset_hw(hw);
535         hw->adapter_stopped = 1;
536
537         atl_stop_queues(dev);
538
539         /* Clear stored conf */
540         dev->data->scattered_rx = 0;
541         dev->data->lro = 0;
542
543         /* Clear recorded link status */
544         memset(&link, 0, sizeof(link));
545         rte_eth_linkstatus_set(dev, &link);
546
547         if (!rte_intr_allow_others(intr_handle))
548                 /* resume to the default handler */
549                 rte_intr_callback_register(intr_handle,
550                                            atl_dev_interrupt_handler,
551                                            (void *)dev);
552
553         /* Clean datapath event and queue/vec mapping */
554         rte_intr_efd_disable(intr_handle);
555         if (intr_handle->intr_vec != NULL) {
556                 rte_free(intr_handle->intr_vec);
557                 intr_handle->intr_vec = NULL;
558         }
559 }
560
561 /*
562  * Set device link up: enable tx.
563  */
564 static int
565 atl_dev_set_link_up(struct rte_eth_dev *dev)
566 {
567         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
568
569         return hw->aq_fw_ops->set_link_speed(hw,
570                         hw->aq_nic_cfg->link_speed_msk);
571 }
572
573 /*
574  * Set device link down: disable tx.
575  */
576 static int
577 atl_dev_set_link_down(struct rte_eth_dev *dev)
578 {
579         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
580
581         return hw->aq_fw_ops->set_link_speed(hw, 0);
582 }
583
584 /*
585  * Reset and stop device.
586  */
587 static void
588 atl_dev_close(struct rte_eth_dev *dev)
589 {
590         PMD_INIT_FUNC_TRACE();
591
592         atl_dev_stop(dev);
593
594         atl_free_queues(dev);
595 }
596
597 static int
598 atl_dev_reset(struct rte_eth_dev *dev)
599 {
600         int ret;
601
602         ret = eth_atl_dev_uninit(dev);
603         if (ret)
604                 return ret;
605
606         ret = eth_atl_dev_init(dev);
607
608         return ret;
609 }
610
611
612 static int
613 atl_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
614 {
615         struct atl_adapter *adapter = ATL_DEV_TO_ADAPTER(dev);
616         struct aq_hw_s *hw = &adapter->hw;
617         struct atl_sw_stats *swstats = &adapter->sw_stats;
618         unsigned int i;
619
620         hw->aq_fw_ops->update_stats(hw);
621
622         /* Fill out the rte_eth_stats statistics structure */
623         stats->ipackets = hw->curr_stats.dma_pkt_rc;
624         stats->ibytes = hw->curr_stats.dma_oct_rc;
625         stats->imissed = hw->curr_stats.dpc;
626         stats->ierrors = hw->curr_stats.erpt;
627
628         stats->opackets = hw->curr_stats.dma_pkt_tc;
629         stats->obytes = hw->curr_stats.dma_oct_tc;
630         stats->oerrors = 0;
631
632         stats->rx_nombuf = swstats->rx_nombuf;
633
634         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
635                 stats->q_ipackets[i] = swstats->q_ipackets[i];
636                 stats->q_opackets[i] = swstats->q_opackets[i];
637                 stats->q_ibytes[i] = swstats->q_ibytes[i];
638                 stats->q_obytes[i] = swstats->q_obytes[i];
639                 stats->q_errors[i] = swstats->q_errors[i];
640         }
641         return 0;
642 }
643
644 static void
645 atl_dev_stats_reset(struct rte_eth_dev *dev)
646 {
647         struct atl_adapter *adapter = ATL_DEV_TO_ADAPTER(dev);
648         struct aq_hw_s *hw = &adapter->hw;
649
650         hw->aq_fw_ops->update_stats(hw);
651
652         /* Reset software totals */
653         memset(&hw->curr_stats, 0, sizeof(hw->curr_stats));
654
655         memset(&adapter->sw_stats, 0, sizeof(adapter->sw_stats));
656 }
657
658 static int
659 atl_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
660                          struct rte_eth_xstat_name *xstats_names,
661                          unsigned int size)
662 {
663         unsigned int i;
664
665         if (!xstats_names)
666                 return RTE_DIM(atl_xstats_tbl);
667
668         for (i = 0; i < size && i < RTE_DIM(atl_xstats_tbl); i++)
669                 snprintf(xstats_names[i].name, RTE_ETH_XSTATS_NAME_SIZE, "%s",
670                         atl_xstats_tbl[i].name);
671
672         return size;
673 }
674
675 static int
676 atl_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *stats,
677                    unsigned int n)
678 {
679         struct atl_adapter *adapter = ATL_DEV_TO_ADAPTER(dev);
680         struct aq_hw_s *hw = &adapter->hw;
681         unsigned int i;
682
683         if (!stats)
684                 return 0;
685
686         for (i = 0; i < n && i < RTE_DIM(atl_xstats_tbl); i++) {
687                 stats[i].id = i;
688                 stats[i].value = *(u64 *)((uint8_t *)&hw->curr_stats +
689                                         atl_xstats_tbl[i].offset);
690         }
691
692         return n;
693 }
694
695 static int
696 atl_fw_version_get(struct rte_eth_dev *dev, char *fw_version, size_t fw_size)
697 {
698         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
699         uint32_t fw_ver = 0;
700         unsigned int ret = 0;
701
702         ret = hw_atl_utils_get_fw_version(hw, &fw_ver);
703         if (ret)
704                 return -EIO;
705
706         ret = snprintf(fw_version, fw_size, "%u.%u.%u", fw_ver >> 24,
707                        (fw_ver >> 16) & 0xFFU, fw_ver & 0xFFFFU);
708
709         ret += 1; /* add string null-terminator */
710
711         if (fw_size < ret)
712                 return ret;
713
714         return 0;
715 }
716
717 static void
718 atl_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
719 {
720         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
721
722         dev_info->max_rx_queues = AQ_HW_MAX_RX_QUEUES;
723         dev_info->max_tx_queues = AQ_HW_MAX_TX_QUEUES;
724
725         dev_info->min_rx_bufsize = 1024;
726         dev_info->max_rx_pktlen = HW_ATL_B0_MTU_JUMBO;
727         dev_info->max_mac_addrs = HW_ATL_B0_MAC_MAX;
728         dev_info->max_vfs = pci_dev->max_vfs;
729
730         dev_info->max_hash_mac_addrs = 0;
731         dev_info->max_vmdq_pools = 0;
732         dev_info->vmdq_queue_num = 0;
733
734         dev_info->rx_offload_capa = ATL_RX_OFFLOADS;
735
736         dev_info->tx_offload_capa = ATL_TX_OFFLOADS;
737
738
739         dev_info->default_rxconf = (struct rte_eth_rxconf) {
740                 .rx_free_thresh = ATL_DEFAULT_RX_FREE_THRESH,
741         };
742
743         dev_info->default_txconf = (struct rte_eth_txconf) {
744                 .tx_free_thresh = ATL_DEFAULT_TX_FREE_THRESH,
745         };
746
747         dev_info->rx_desc_lim = rx_desc_lim;
748         dev_info->tx_desc_lim = tx_desc_lim;
749
750         dev_info->speed_capa = ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G;
751         dev_info->speed_capa |= ETH_LINK_SPEED_100M;
752         dev_info->speed_capa |= ETH_LINK_SPEED_2_5G;
753         dev_info->speed_capa |= ETH_LINK_SPEED_5G;
754 }
755
756 static const uint32_t *
757 atl_dev_supported_ptypes_get(struct rte_eth_dev *dev)
758 {
759         static const uint32_t ptypes[] = {
760                 RTE_PTYPE_L2_ETHER,
761                 RTE_PTYPE_L2_ETHER_ARP,
762                 RTE_PTYPE_L2_ETHER_VLAN,
763                 RTE_PTYPE_L3_IPV4,
764                 RTE_PTYPE_L3_IPV6,
765                 RTE_PTYPE_L4_TCP,
766                 RTE_PTYPE_L4_UDP,
767                 RTE_PTYPE_L4_SCTP,
768                 RTE_PTYPE_L4_ICMP,
769                 RTE_PTYPE_UNKNOWN
770         };
771
772         if (dev->rx_pkt_burst == atl_recv_pkts)
773                 return ptypes;
774
775         return NULL;
776 }
777
778 /* return 0 means link status changed, -1 means not changed */
779 static int
780 atl_dev_link_update(struct rte_eth_dev *dev, int wait __rte_unused)
781 {
782         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
783         struct atl_interrupt *intr =
784                 ATL_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
785         struct rte_eth_link link, old;
786         int err = 0;
787
788         link.link_status = ETH_LINK_DOWN;
789         link.link_speed = 0;
790         link.link_duplex = ETH_LINK_FULL_DUPLEX;
791         link.link_autoneg = hw->is_autoneg ? ETH_LINK_AUTONEG : ETH_LINK_FIXED;
792         memset(&old, 0, sizeof(old));
793
794         /* load old link status */
795         rte_eth_linkstatus_get(dev, &old);
796
797         /* read current link status */
798         err = hw->aq_fw_ops->update_link_status(hw);
799
800         if (err)
801                 return 0;
802
803         if (hw->aq_link_status.mbps == 0) {
804                 /* write default (down) link status */
805                 rte_eth_linkstatus_set(dev, &link);
806                 if (link.link_status == old.link_status)
807                         return -1;
808                 return 0;
809         }
810
811         intr->flags &= ~ATL_FLAG_NEED_LINK_CONFIG;
812
813         link.link_status = ETH_LINK_UP;
814         link.link_duplex = ETH_LINK_FULL_DUPLEX;
815         link.link_speed = hw->aq_link_status.mbps;
816
817         rte_eth_linkstatus_set(dev, &link);
818
819         if (link.link_status == old.link_status)
820                 return -1;
821
822         return 0;
823 }
824
825 static void
826 atl_dev_promiscuous_enable(struct rte_eth_dev *dev)
827 {
828         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
829
830         hw_atl_rpfl2promiscuous_mode_en_set(hw, true);
831 }
832
833 static void
834 atl_dev_promiscuous_disable(struct rte_eth_dev *dev)
835 {
836         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
837
838         hw_atl_rpfl2promiscuous_mode_en_set(hw, false);
839 }
840
841 static void
842 atl_dev_allmulticast_enable(struct rte_eth_dev *dev)
843 {
844         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
845
846         hw_atl_rpfl2_accept_all_mc_packets_set(hw, true);
847 }
848
849 static void
850 atl_dev_allmulticast_disable(struct rte_eth_dev *dev)
851 {
852         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
853
854         if (dev->data->promiscuous == 1)
855                 return; /* must remain in all_multicast mode */
856
857         hw_atl_rpfl2_accept_all_mc_packets_set(hw, false);
858 }
859
860 /**
861  * It clears the interrupt causes and enables the interrupt.
862  * It will be called once only during nic initialized.
863  *
864  * @param dev
865  *  Pointer to struct rte_eth_dev.
866  * @param on
867  *  Enable or Disable.
868  *
869  * @return
870  *  - On success, zero.
871  *  - On failure, a negative value.
872  */
873
874 static int
875 atl_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on __rte_unused)
876 {
877         atl_dev_link_status_print(dev);
878         return 0;
879 }
880
881 static int
882 atl_dev_rxq_interrupt_setup(struct rte_eth_dev *dev __rte_unused)
883 {
884         return 0;
885 }
886
887
888 static int
889 atl_dev_interrupt_get_status(struct rte_eth_dev *dev)
890 {
891         struct atl_interrupt *intr =
892                 ATL_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
893         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
894         u64 cause = 0;
895
896         hw_atl_b0_hw_irq_read(hw, &cause);
897
898         atl_disable_intr(hw);
899         intr->flags = cause & BIT(ATL_IRQ_CAUSE_LINK) ?
900                         ATL_FLAG_NEED_LINK_UPDATE : 0;
901
902         return 0;
903 }
904
905 /**
906  * It gets and then prints the link status.
907  *
908  * @param dev
909  *  Pointer to struct rte_eth_dev.
910  *
911  * @return
912  *  - On success, zero.
913  *  - On failure, a negative value.
914  */
915 static void
916 atl_dev_link_status_print(struct rte_eth_dev *dev)
917 {
918         struct rte_eth_link link;
919
920         memset(&link, 0, sizeof(link));
921         rte_eth_linkstatus_get(dev, &link);
922         if (link.link_status) {
923                 PMD_DRV_LOG(INFO, "Port %d: Link Up - speed %u Mbps - %s",
924                                         (int)(dev->data->port_id),
925                                         (unsigned int)link.link_speed,
926                         link.link_duplex == ETH_LINK_FULL_DUPLEX ?
927                                         "full-duplex" : "half-duplex");
928         } else {
929                 PMD_DRV_LOG(INFO, " Port %d: Link Down",
930                                 (int)(dev->data->port_id));
931         }
932
933
934 #ifdef DEBUG
935 {
936         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
937
938         PMD_DRV_LOG(DEBUG, "PCI Address: " PCI_PRI_FMT,
939                                 pci_dev->addr.domain,
940                                 pci_dev->addr.bus,
941                                 pci_dev->addr.devid,
942                                 pci_dev->addr.function);
943 }
944 #endif
945
946         PMD_DRV_LOG(INFO, "Link speed:%d", link.link_speed);
947 }
948
949 /*
950  * It executes link_update after knowing an interrupt occurred.
951  *
952  * @param dev
953  *  Pointer to struct rte_eth_dev.
954  *
955  * @return
956  *  - On success, zero.
957  *  - On failure, a negative value.
958  */
959 static int
960 atl_dev_interrupt_action(struct rte_eth_dev *dev,
961                            struct rte_intr_handle *intr_handle)
962 {
963         struct atl_interrupt *intr =
964                 ATL_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
965
966         if (intr->flags & ATL_FLAG_NEED_LINK_UPDATE) {
967                 atl_dev_link_update(dev, 0);
968                 intr->flags &= ~ATL_FLAG_NEED_LINK_UPDATE;
969                 atl_dev_link_status_print(dev);
970                 _rte_eth_dev_callback_process(dev,
971                         RTE_ETH_EVENT_INTR_LSC, NULL);
972         }
973
974         atl_enable_intr(dev);
975         rte_intr_enable(intr_handle);
976
977         return 0;
978 }
979
980 /**
981  * Interrupt handler triggered by NIC  for handling
982  * specific interrupt.
983  *
984  * @param handle
985  *  Pointer to interrupt handle.
986  * @param param
987  *  The address of parameter (struct rte_eth_dev *) regsitered before.
988  *
989  * @return
990  *  void
991  */
992 static void
993 atl_dev_interrupt_handler(void *param)
994 {
995         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
996
997         atl_dev_interrupt_get_status(dev);
998         atl_dev_interrupt_action(dev, dev->intr_handle);
999 }
1000
1001 RTE_PMD_REGISTER_PCI(net_atlantic, rte_atl_pmd);
1002 RTE_PMD_REGISTER_PCI_TABLE(net_atlantic, pci_id_atl_map);
1003 RTE_PMD_REGISTER_KMOD_DEP(net_atlantic, "* igb_uio | uio_pci_generic");
1004
1005 RTE_INIT(atl_init_log)
1006 {
1007         atl_logtype_init = rte_log_register("pmd.net.atlantic.init");
1008         if (atl_logtype_init >= 0)
1009                 rte_log_set_level(atl_logtype_init, RTE_LOG_NOTICE);
1010         atl_logtype_driver = rte_log_register("pmd.net.atlantic.driver");
1011         if (atl_logtype_driver >= 0)
1012                 rte_log_set_level(atl_logtype_driver, RTE_LOG_NOTICE);
1013 }
1014