net/atlantic: fix xstats return
[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 int atl_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
52
53 /* VLAN stuff */
54 static int atl_vlan_filter_set(struct rte_eth_dev *dev,
55                 uint16_t vlan_id, int on);
56
57 static int atl_vlan_offload_set(struct rte_eth_dev *dev, int mask);
58
59 static void atl_vlan_strip_queue_set(struct rte_eth_dev *dev,
60                                      uint16_t queue_id, int on);
61
62 static int atl_vlan_tpid_set(struct rte_eth_dev *dev,
63                              enum rte_vlan_type vlan_type, uint16_t tpid);
64
65 /* EEPROM */
66 static int atl_dev_get_eeprom_length(struct rte_eth_dev *dev);
67 static int atl_dev_get_eeprom(struct rte_eth_dev *dev,
68                               struct rte_dev_eeprom_info *eeprom);
69 static int atl_dev_set_eeprom(struct rte_eth_dev *dev,
70                               struct rte_dev_eeprom_info *eeprom);
71
72 /* Regs */
73 static int atl_dev_get_regs(struct rte_eth_dev *dev,
74                             struct rte_dev_reg_info *regs);
75
76 /* Flow control */
77 static int atl_flow_ctrl_get(struct rte_eth_dev *dev,
78                                struct rte_eth_fc_conf *fc_conf);
79 static int atl_flow_ctrl_set(struct rte_eth_dev *dev,
80                                struct rte_eth_fc_conf *fc_conf);
81
82 static void atl_dev_link_status_print(struct rte_eth_dev *dev);
83
84 /* Interrupts */
85 static int atl_dev_rxq_interrupt_setup(struct rte_eth_dev *dev);
86 static int atl_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on);
87 static int atl_dev_interrupt_get_status(struct rte_eth_dev *dev);
88 static int atl_dev_interrupt_action(struct rte_eth_dev *dev,
89                                     struct rte_intr_handle *handle);
90 static void atl_dev_interrupt_handler(void *param);
91
92
93 static int atl_add_mac_addr(struct rte_eth_dev *dev,
94                             struct ether_addr *mac_addr,
95                             uint32_t index, uint32_t pool);
96 static void atl_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index);
97 static int atl_set_default_mac_addr(struct rte_eth_dev *dev,
98                                            struct ether_addr *mac_addr);
99
100 static int atl_dev_set_mc_addr_list(struct rte_eth_dev *dev,
101                                     struct ether_addr *mc_addr_set,
102                                     uint32_t nb_mc_addr);
103
104 /* RSS */
105 static int atl_reta_update(struct rte_eth_dev *dev,
106                              struct rte_eth_rss_reta_entry64 *reta_conf,
107                              uint16_t reta_size);
108 static int atl_reta_query(struct rte_eth_dev *dev,
109                             struct rte_eth_rss_reta_entry64 *reta_conf,
110                             uint16_t reta_size);
111 static int atl_rss_hash_update(struct rte_eth_dev *dev,
112                                  struct rte_eth_rss_conf *rss_conf);
113 static int atl_rss_hash_conf_get(struct rte_eth_dev *dev,
114                                    struct rte_eth_rss_conf *rss_conf);
115
116
117 static int eth_atl_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
118         struct rte_pci_device *pci_dev);
119 static int eth_atl_pci_remove(struct rte_pci_device *pci_dev);
120
121 static void atl_dev_info_get(struct rte_eth_dev *dev,
122                                 struct rte_eth_dev_info *dev_info);
123
124 int atl_logtype_init;
125 int atl_logtype_driver;
126
127 /*
128  * The set of PCI devices this driver supports
129  */
130 static const struct rte_pci_id pci_id_atl_map[] = {
131         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_0001) },
132         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D100) },
133         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D107) },
134         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D108) },
135         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_D109) },
136
137         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC100) },
138         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC107) },
139         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC108) },
140         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC109) },
141         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC111) },
142         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC112) },
143
144         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC100S) },
145         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC107S) },
146         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC108S) },
147         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC109S) },
148         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC111S) },
149         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC112S) },
150
151         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC111E) },
152         { RTE_PCI_DEVICE(PCI_VENDOR_ID_AQUANTIA, AQ_DEVICE_ID_AQC112E) },
153         { .vendor_id = 0, /* sentinel */ },
154 };
155
156 static struct rte_pci_driver rte_atl_pmd = {
157         .id_table = pci_id_atl_map,
158         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
159                      RTE_PCI_DRV_IOVA_AS_VA,
160         .probe = eth_atl_pci_probe,
161         .remove = eth_atl_pci_remove,
162 };
163
164 #define ATL_RX_OFFLOADS (DEV_RX_OFFLOAD_VLAN_STRIP \
165                         | DEV_RX_OFFLOAD_IPV4_CKSUM \
166                         | DEV_RX_OFFLOAD_UDP_CKSUM \
167                         | DEV_RX_OFFLOAD_TCP_CKSUM \
168                         | DEV_RX_OFFLOAD_JUMBO_FRAME \
169                         | DEV_RX_OFFLOAD_VLAN_FILTER)
170
171 #define ATL_TX_OFFLOADS (DEV_TX_OFFLOAD_VLAN_INSERT \
172                         | DEV_TX_OFFLOAD_IPV4_CKSUM \
173                         | DEV_TX_OFFLOAD_UDP_CKSUM \
174                         | DEV_TX_OFFLOAD_TCP_CKSUM \
175                         | DEV_TX_OFFLOAD_TCP_TSO \
176                         | DEV_TX_OFFLOAD_MULTI_SEGS)
177
178 static const struct rte_eth_desc_lim rx_desc_lim = {
179         .nb_max = ATL_MAX_RING_DESC,
180         .nb_min = ATL_MIN_RING_DESC,
181         .nb_align = ATL_RXD_ALIGN,
182 };
183
184 static const struct rte_eth_desc_lim tx_desc_lim = {
185         .nb_max = ATL_MAX_RING_DESC,
186         .nb_min = ATL_MIN_RING_DESC,
187         .nb_align = ATL_TXD_ALIGN,
188         .nb_seg_max = ATL_TX_MAX_SEG,
189         .nb_mtu_seg_max = ATL_TX_MAX_SEG,
190 };
191
192 #define ATL_XSTATS_FIELD(name) { \
193         #name, \
194         offsetof(struct aq_stats_s, name) \
195 }
196
197 struct atl_xstats_tbl_s {
198         const char *name;
199         unsigned int offset;
200 };
201
202 static struct atl_xstats_tbl_s atl_xstats_tbl[] = {
203         ATL_XSTATS_FIELD(uprc),
204         ATL_XSTATS_FIELD(mprc),
205         ATL_XSTATS_FIELD(bprc),
206         ATL_XSTATS_FIELD(erpt),
207         ATL_XSTATS_FIELD(uptc),
208         ATL_XSTATS_FIELD(mptc),
209         ATL_XSTATS_FIELD(bptc),
210         ATL_XSTATS_FIELD(erpr),
211         ATL_XSTATS_FIELD(ubrc),
212         ATL_XSTATS_FIELD(ubtc),
213         ATL_XSTATS_FIELD(mbrc),
214         ATL_XSTATS_FIELD(mbtc),
215         ATL_XSTATS_FIELD(bbrc),
216         ATL_XSTATS_FIELD(bbtc),
217 };
218
219 static const struct eth_dev_ops atl_eth_dev_ops = {
220         .dev_configure        = atl_dev_configure,
221         .dev_start            = atl_dev_start,
222         .dev_stop             = atl_dev_stop,
223         .dev_set_link_up      = atl_dev_set_link_up,
224         .dev_set_link_down    = atl_dev_set_link_down,
225         .dev_close            = atl_dev_close,
226         .dev_reset            = atl_dev_reset,
227
228         /* PROMISC */
229         .promiscuous_enable   = atl_dev_promiscuous_enable,
230         .promiscuous_disable  = atl_dev_promiscuous_disable,
231         .allmulticast_enable  = atl_dev_allmulticast_enable,
232         .allmulticast_disable = atl_dev_allmulticast_disable,
233
234         /* Link */
235         .link_update          = atl_dev_link_update,
236
237         .get_reg              = atl_dev_get_regs,
238
239         /* Stats */
240         .stats_get            = atl_dev_stats_get,
241         .xstats_get           = atl_dev_xstats_get,
242         .xstats_get_names     = atl_dev_xstats_get_names,
243         .stats_reset          = atl_dev_stats_reset,
244         .xstats_reset         = atl_dev_stats_reset,
245
246         .fw_version_get       = atl_fw_version_get,
247         .dev_infos_get        = atl_dev_info_get,
248         .dev_supported_ptypes_get = atl_dev_supported_ptypes_get,
249
250         .mtu_set              = atl_dev_mtu_set,
251
252         /* VLAN */
253         .vlan_filter_set      = atl_vlan_filter_set,
254         .vlan_offload_set     = atl_vlan_offload_set,
255         .vlan_tpid_set        = atl_vlan_tpid_set,
256         .vlan_strip_queue_set = atl_vlan_strip_queue_set,
257
258         /* Queue Control */
259         .rx_queue_start       = atl_rx_queue_start,
260         .rx_queue_stop        = atl_rx_queue_stop,
261         .rx_queue_setup       = atl_rx_queue_setup,
262         .rx_queue_release     = atl_rx_queue_release,
263
264         .tx_queue_start       = atl_tx_queue_start,
265         .tx_queue_stop        = atl_tx_queue_stop,
266         .tx_queue_setup       = atl_tx_queue_setup,
267         .tx_queue_release     = atl_tx_queue_release,
268
269         .rx_queue_intr_enable = atl_dev_rx_queue_intr_enable,
270         .rx_queue_intr_disable = atl_dev_rx_queue_intr_disable,
271
272         .rx_queue_count       = atl_rx_queue_count,
273         .rx_descriptor_status = atl_dev_rx_descriptor_status,
274         .tx_descriptor_status = atl_dev_tx_descriptor_status,
275
276         /* EEPROM */
277         .get_eeprom_length    = atl_dev_get_eeprom_length,
278         .get_eeprom           = atl_dev_get_eeprom,
279         .set_eeprom           = atl_dev_set_eeprom,
280
281         /* Flow Control */
282         .flow_ctrl_get        = atl_flow_ctrl_get,
283         .flow_ctrl_set        = atl_flow_ctrl_set,
284
285         /* MAC */
286         .mac_addr_add         = atl_add_mac_addr,
287         .mac_addr_remove      = atl_remove_mac_addr,
288         .mac_addr_set         = atl_set_default_mac_addr,
289         .set_mc_addr_list     = atl_dev_set_mc_addr_list,
290         .rxq_info_get         = atl_rxq_info_get,
291         .txq_info_get         = atl_txq_info_get,
292
293         .reta_update          = atl_reta_update,
294         .reta_query           = atl_reta_query,
295         .rss_hash_update      = atl_rss_hash_update,
296         .rss_hash_conf_get    = atl_rss_hash_conf_get,
297 };
298
299 static inline int32_t
300 atl_reset_hw(struct aq_hw_s *hw)
301 {
302         return hw_atl_b0_hw_reset(hw);
303 }
304
305 static inline void
306 atl_enable_intr(struct rte_eth_dev *dev)
307 {
308         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
309
310         hw_atl_itr_irq_msk_setlsw_set(hw, 0xffffffff);
311 }
312
313 static void
314 atl_disable_intr(struct aq_hw_s *hw)
315 {
316         PMD_INIT_FUNC_TRACE();
317         hw_atl_itr_irq_msk_clearlsw_set(hw, 0xffffffff);
318 }
319
320 static int
321 eth_atl_dev_init(struct rte_eth_dev *eth_dev)
322 {
323         struct atl_adapter *adapter =
324                 (struct atl_adapter *)eth_dev->data->dev_private;
325         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
326         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
327         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
328         int err = 0;
329
330         PMD_INIT_FUNC_TRACE();
331
332         eth_dev->dev_ops = &atl_eth_dev_ops;
333         eth_dev->rx_pkt_burst = &atl_recv_pkts;
334         eth_dev->tx_pkt_burst = &atl_xmit_pkts;
335         eth_dev->tx_pkt_prepare = &atl_prep_pkts;
336
337         /* For secondary processes, the primary process has done all the work */
338         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
339                 return 0;
340
341         /* Vendor and Device ID need to be set before init of shared code */
342         hw->device_id = pci_dev->id.device_id;
343         hw->vendor_id = pci_dev->id.vendor_id;
344         hw->mmio = (void *)pci_dev->mem_resource[0].addr;
345
346         /* Hardware configuration - hardcode */
347         adapter->hw_cfg.is_lro = false;
348         adapter->hw_cfg.wol = false;
349         adapter->hw_cfg.is_rss = false;
350         adapter->hw_cfg.num_rss_queues = HW_ATL_B0_RSS_MAX;
351
352         adapter->hw_cfg.link_speed_msk = AQ_NIC_RATE_10G |
353                           AQ_NIC_RATE_5G |
354                           AQ_NIC_RATE_2G5 |
355                           AQ_NIC_RATE_1G |
356                           AQ_NIC_RATE_100M;
357
358         adapter->hw_cfg.flow_control = (AQ_NIC_FC_RX | AQ_NIC_FC_TX);
359         adapter->hw_cfg.aq_rss.indirection_table_size =
360                 HW_ATL_B0_RSS_REDIRECTION_MAX;
361
362         hw->aq_nic_cfg = &adapter->hw_cfg;
363
364         /* disable interrupt */
365         atl_disable_intr(hw);
366
367         /* Allocate memory for storing MAC addresses */
368         eth_dev->data->mac_addrs = rte_zmalloc("atlantic", ETHER_ADDR_LEN, 0);
369         if (eth_dev->data->mac_addrs == NULL) {
370                 PMD_INIT_LOG(ERR, "MAC Malloc failed");
371                 return -ENOMEM;
372         }
373
374         err = hw_atl_utils_initfw(hw, &hw->aq_fw_ops);
375         if (err)
376                 return err;
377
378         /* Copy the permanent MAC address */
379         if (hw->aq_fw_ops->get_mac_permanent(hw,
380                         eth_dev->data->mac_addrs->addr_bytes) != 0)
381                 return -EINVAL;
382
383         /* Reset the hw statistics */
384         atl_dev_stats_reset(eth_dev);
385
386         rte_intr_callback_register(intr_handle,
387                                    atl_dev_interrupt_handler, eth_dev);
388
389         /* enable uio/vfio intr/eventfd mapping */
390         rte_intr_enable(intr_handle);
391
392         /* enable support intr */
393         atl_enable_intr(eth_dev);
394
395         return err;
396 }
397
398 static int
399 eth_atl_dev_uninit(struct rte_eth_dev *eth_dev)
400 {
401         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
402         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
403         struct aq_hw_s *hw;
404
405         PMD_INIT_FUNC_TRACE();
406
407         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
408                 return -EPERM;
409
410         hw = ATL_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
411
412         if (hw->adapter_stopped == 0)
413                 atl_dev_close(eth_dev);
414
415         eth_dev->dev_ops = NULL;
416         eth_dev->rx_pkt_burst = NULL;
417         eth_dev->tx_pkt_burst = NULL;
418
419         /* disable uio intr before callback unregister */
420         rte_intr_disable(intr_handle);
421         rte_intr_callback_unregister(intr_handle,
422                                      atl_dev_interrupt_handler, eth_dev);
423
424         rte_free(eth_dev->data->mac_addrs);
425         eth_dev->data->mac_addrs = NULL;
426
427         return 0;
428 }
429
430 static int
431 eth_atl_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
432         struct rte_pci_device *pci_dev)
433 {
434         return rte_eth_dev_pci_generic_probe(pci_dev,
435                 sizeof(struct atl_adapter), eth_atl_dev_init);
436 }
437
438 static int
439 eth_atl_pci_remove(struct rte_pci_device *pci_dev)
440 {
441         return rte_eth_dev_pci_generic_remove(pci_dev, eth_atl_dev_uninit);
442 }
443
444 static int
445 atl_dev_configure(struct rte_eth_dev *dev)
446 {
447         struct atl_interrupt *intr =
448                 ATL_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
449
450         PMD_INIT_FUNC_TRACE();
451
452         /* set flag to update link status after init */
453         intr->flags |= ATL_FLAG_NEED_LINK_UPDATE;
454
455         return 0;
456 }
457
458 /*
459  * Configure device link speed and setup link.
460  * It returns 0 on success.
461  */
462 static int
463 atl_dev_start(struct rte_eth_dev *dev)
464 {
465         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
466         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
467         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
468         uint32_t intr_vector = 0;
469         int status;
470         int err;
471
472         PMD_INIT_FUNC_TRACE();
473
474         /* set adapter started */
475         hw->adapter_stopped = 0;
476
477         if (dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED) {
478                 PMD_INIT_LOG(ERR,
479                 "Invalid link_speeds for port %u, fix speed not supported",
480                                 dev->data->port_id);
481                 return -EINVAL;
482         }
483
484         /* disable uio/vfio intr/eventfd mapping */
485         rte_intr_disable(intr_handle);
486
487         /* reinitialize adapter
488          * this calls reset and start
489          */
490         status = atl_reset_hw(hw);
491         if (status != 0)
492                 return -EIO;
493
494         err = hw_atl_b0_hw_init(hw, dev->data->mac_addrs->addr_bytes);
495
496         hw_atl_b0_hw_start(hw);
497         /* check and configure queue intr-vector mapping */
498         if ((rte_intr_cap_multiple(intr_handle) ||
499             !RTE_ETH_DEV_SRIOV(dev).active) &&
500             dev->data->dev_conf.intr_conf.rxq != 0) {
501                 intr_vector = dev->data->nb_rx_queues;
502                 if (intr_vector > ATL_MAX_INTR_QUEUE_NUM) {
503                         PMD_INIT_LOG(ERR, "At most %d intr queues supported",
504                                         ATL_MAX_INTR_QUEUE_NUM);
505                         return -ENOTSUP;
506                 }
507                 if (rte_intr_efd_enable(intr_handle, intr_vector)) {
508                         PMD_INIT_LOG(ERR, "rte_intr_efd_enable failed");
509                         return -1;
510                 }
511         }
512
513         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
514                 intr_handle->intr_vec = rte_zmalloc("intr_vec",
515                                     dev->data->nb_rx_queues * sizeof(int), 0);
516                 if (intr_handle->intr_vec == NULL) {
517                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
518                                      " intr_vec", dev->data->nb_rx_queues);
519                         return -ENOMEM;
520                 }
521         }
522
523         /* initialize transmission unit */
524         atl_tx_init(dev);
525
526         /* This can fail when allocating mbufs for descriptor rings */
527         err = atl_rx_init(dev);
528         if (err) {
529                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware");
530                 goto error;
531         }
532
533         PMD_INIT_LOG(DEBUG, "FW version: %u.%u.%u",
534                 hw->fw_ver_actual >> 24,
535                 (hw->fw_ver_actual >> 16) & 0xFF,
536                 hw->fw_ver_actual & 0xFFFF);
537         PMD_INIT_LOG(DEBUG, "Driver version: %s", ATL_PMD_DRIVER_VERSION);
538
539         err = atl_start_queues(dev);
540         if (err < 0) {
541                 PMD_INIT_LOG(ERR, "Unable to start rxtx queues");
542                 goto error;
543         }
544
545         err = atl_dev_set_link_up(dev);
546
547         err = hw->aq_fw_ops->update_link_status(hw);
548
549         if (err)
550                 goto error;
551
552         dev->data->dev_link.link_status = hw->aq_link_status.mbps != 0;
553
554         if (err)
555                 goto error;
556
557         if (rte_intr_allow_others(intr_handle)) {
558                 /* check if lsc interrupt is enabled */
559                 if (dev->data->dev_conf.intr_conf.lsc != 0)
560                         atl_dev_lsc_interrupt_setup(dev, true);
561                 else
562                         atl_dev_lsc_interrupt_setup(dev, false);
563         } else {
564                 rte_intr_callback_unregister(intr_handle,
565                                              atl_dev_interrupt_handler, dev);
566                 if (dev->data->dev_conf.intr_conf.lsc != 0)
567                         PMD_INIT_LOG(INFO, "lsc won't enable because of"
568                                      " no intr multiplex");
569         }
570
571         /* check if rxq interrupt is enabled */
572         if (dev->data->dev_conf.intr_conf.rxq != 0 &&
573             rte_intr_dp_is_en(intr_handle))
574                 atl_dev_rxq_interrupt_setup(dev);
575
576         /* enable uio/vfio intr/eventfd mapping */
577         rte_intr_enable(intr_handle);
578
579         /* resume enabled intr since hw reset */
580         atl_enable_intr(dev);
581
582         return 0;
583
584 error:
585         atl_stop_queues(dev);
586         return -EIO;
587 }
588
589 /*
590  * Stop device: disable rx and tx functions to allow for reconfiguring.
591  */
592 static void
593 atl_dev_stop(struct rte_eth_dev *dev)
594 {
595         struct rte_eth_link link;
596         struct aq_hw_s *hw =
597                 ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
598         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
599         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
600
601         PMD_INIT_FUNC_TRACE();
602
603         /* disable interrupts */
604         atl_disable_intr(hw);
605
606         /* reset the NIC */
607         atl_reset_hw(hw);
608         hw->adapter_stopped = 1;
609
610         atl_stop_queues(dev);
611
612         /* Clear stored conf */
613         dev->data->scattered_rx = 0;
614         dev->data->lro = 0;
615
616         /* Clear recorded link status */
617         memset(&link, 0, sizeof(link));
618         rte_eth_linkstatus_set(dev, &link);
619
620         if (!rte_intr_allow_others(intr_handle))
621                 /* resume to the default handler */
622                 rte_intr_callback_register(intr_handle,
623                                            atl_dev_interrupt_handler,
624                                            (void *)dev);
625
626         /* Clean datapath event and queue/vec mapping */
627         rte_intr_efd_disable(intr_handle);
628         if (intr_handle->intr_vec != NULL) {
629                 rte_free(intr_handle->intr_vec);
630                 intr_handle->intr_vec = NULL;
631         }
632 }
633
634 /*
635  * Set device link up: enable tx.
636  */
637 static int
638 atl_dev_set_link_up(struct rte_eth_dev *dev)
639 {
640         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
641         uint32_t link_speeds = dev->data->dev_conf.link_speeds;
642         uint32_t speed_mask = 0;
643
644         if (link_speeds == ETH_LINK_SPEED_AUTONEG) {
645                 speed_mask = hw->aq_nic_cfg->link_speed_msk;
646         } else {
647                 if (link_speeds & ETH_LINK_SPEED_10G)
648                         speed_mask |= AQ_NIC_RATE_10G;
649                 if (link_speeds & ETH_LINK_SPEED_5G)
650                         speed_mask |= AQ_NIC_RATE_5G;
651                 if (link_speeds & ETH_LINK_SPEED_1G)
652                         speed_mask |= AQ_NIC_RATE_1G;
653                 if (link_speeds & ETH_LINK_SPEED_2_5G)
654                         speed_mask |=  AQ_NIC_RATE_2G5;
655                 if (link_speeds & ETH_LINK_SPEED_100M)
656                         speed_mask |= AQ_NIC_RATE_100M;
657         }
658
659         return hw->aq_fw_ops->set_link_speed(hw, speed_mask);
660 }
661
662 /*
663  * Set device link down: disable tx.
664  */
665 static int
666 atl_dev_set_link_down(struct rte_eth_dev *dev)
667 {
668         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
669
670         return hw->aq_fw_ops->set_link_speed(hw, 0);
671 }
672
673 /*
674  * Reset and stop device.
675  */
676 static void
677 atl_dev_close(struct rte_eth_dev *dev)
678 {
679         PMD_INIT_FUNC_TRACE();
680
681         atl_dev_stop(dev);
682
683         atl_free_queues(dev);
684 }
685
686 static int
687 atl_dev_reset(struct rte_eth_dev *dev)
688 {
689         int ret;
690
691         ret = eth_atl_dev_uninit(dev);
692         if (ret)
693                 return ret;
694
695         ret = eth_atl_dev_init(dev);
696
697         return ret;
698 }
699
700
701 static int
702 atl_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
703 {
704         struct atl_adapter *adapter = ATL_DEV_TO_ADAPTER(dev);
705         struct aq_hw_s *hw = &adapter->hw;
706         struct atl_sw_stats *swstats = &adapter->sw_stats;
707         unsigned int i;
708
709         hw->aq_fw_ops->update_stats(hw);
710
711         /* Fill out the rte_eth_stats statistics structure */
712         stats->ipackets = hw->curr_stats.dma_pkt_rc;
713         stats->ibytes = hw->curr_stats.dma_oct_rc;
714         stats->imissed = hw->curr_stats.dpc;
715         stats->ierrors = hw->curr_stats.erpt;
716
717         stats->opackets = hw->curr_stats.dma_pkt_tc;
718         stats->obytes = hw->curr_stats.dma_oct_tc;
719         stats->oerrors = 0;
720
721         stats->rx_nombuf = swstats->rx_nombuf;
722
723         for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
724                 stats->q_ipackets[i] = swstats->q_ipackets[i];
725                 stats->q_opackets[i] = swstats->q_opackets[i];
726                 stats->q_ibytes[i] = swstats->q_ibytes[i];
727                 stats->q_obytes[i] = swstats->q_obytes[i];
728                 stats->q_errors[i] = swstats->q_errors[i];
729         }
730         return 0;
731 }
732
733 static void
734 atl_dev_stats_reset(struct rte_eth_dev *dev)
735 {
736         struct atl_adapter *adapter = ATL_DEV_TO_ADAPTER(dev);
737         struct aq_hw_s *hw = &adapter->hw;
738
739         hw->aq_fw_ops->update_stats(hw);
740
741         /* Reset software totals */
742         memset(&hw->curr_stats, 0, sizeof(hw->curr_stats));
743
744         memset(&adapter->sw_stats, 0, sizeof(adapter->sw_stats));
745 }
746
747 static int
748 atl_dev_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
749                          struct rte_eth_xstat_name *xstats_names,
750                          unsigned int size)
751 {
752         unsigned int i;
753
754         if (!xstats_names)
755                 return RTE_DIM(atl_xstats_tbl);
756
757         for (i = 0; i < size && i < RTE_DIM(atl_xstats_tbl); i++)
758                 snprintf(xstats_names[i].name, RTE_ETH_XSTATS_NAME_SIZE, "%s",
759                         atl_xstats_tbl[i].name);
760
761         return i;
762 }
763
764 static int
765 atl_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *stats,
766                    unsigned int n)
767 {
768         struct atl_adapter *adapter = ATL_DEV_TO_ADAPTER(dev);
769         struct aq_hw_s *hw = &adapter->hw;
770         unsigned int i;
771
772         if (!stats)
773                 return 0;
774
775         for (i = 0; i < n && i < RTE_DIM(atl_xstats_tbl); i++) {
776                 stats[i].id = i;
777                 stats[i].value = *(u64 *)((uint8_t *)&hw->curr_stats +
778                                         atl_xstats_tbl[i].offset);
779         }
780
781         return i;
782 }
783
784 static int
785 atl_fw_version_get(struct rte_eth_dev *dev, char *fw_version, size_t fw_size)
786 {
787         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
788         uint32_t fw_ver = 0;
789         unsigned int ret = 0;
790
791         ret = hw_atl_utils_get_fw_version(hw, &fw_ver);
792         if (ret)
793                 return -EIO;
794
795         ret = snprintf(fw_version, fw_size, "%u.%u.%u", fw_ver >> 24,
796                        (fw_ver >> 16) & 0xFFU, fw_ver & 0xFFFFU);
797
798         ret += 1; /* add string null-terminator */
799
800         if (fw_size < ret)
801                 return ret;
802
803         return 0;
804 }
805
806 static void
807 atl_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
808 {
809         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
810
811         dev_info->max_rx_queues = AQ_HW_MAX_RX_QUEUES;
812         dev_info->max_tx_queues = AQ_HW_MAX_TX_QUEUES;
813
814         dev_info->min_rx_bufsize = 1024;
815         dev_info->max_rx_pktlen = HW_ATL_B0_MTU_JUMBO;
816         dev_info->max_mac_addrs = HW_ATL_B0_MAC_MAX;
817         dev_info->max_vfs = pci_dev->max_vfs;
818
819         dev_info->max_hash_mac_addrs = 0;
820         dev_info->max_vmdq_pools = 0;
821         dev_info->vmdq_queue_num = 0;
822
823         dev_info->rx_offload_capa = ATL_RX_OFFLOADS;
824
825         dev_info->tx_offload_capa = ATL_TX_OFFLOADS;
826
827
828         dev_info->default_rxconf = (struct rte_eth_rxconf) {
829                 .rx_free_thresh = ATL_DEFAULT_RX_FREE_THRESH,
830         };
831
832         dev_info->default_txconf = (struct rte_eth_txconf) {
833                 .tx_free_thresh = ATL_DEFAULT_TX_FREE_THRESH,
834         };
835
836         dev_info->rx_desc_lim = rx_desc_lim;
837         dev_info->tx_desc_lim = tx_desc_lim;
838
839         dev_info->hash_key_size = HW_ATL_B0_RSS_HASHKEY_BITS / 8;
840         dev_info->reta_size = HW_ATL_B0_RSS_REDIRECTION_MAX;
841         dev_info->flow_type_rss_offloads = ATL_RSS_OFFLOAD_ALL;
842
843         dev_info->speed_capa = ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G;
844         dev_info->speed_capa |= ETH_LINK_SPEED_100M;
845         dev_info->speed_capa |= ETH_LINK_SPEED_2_5G;
846         dev_info->speed_capa |= ETH_LINK_SPEED_5G;
847 }
848
849 static const uint32_t *
850 atl_dev_supported_ptypes_get(struct rte_eth_dev *dev)
851 {
852         static const uint32_t ptypes[] = {
853                 RTE_PTYPE_L2_ETHER,
854                 RTE_PTYPE_L2_ETHER_ARP,
855                 RTE_PTYPE_L2_ETHER_VLAN,
856                 RTE_PTYPE_L3_IPV4,
857                 RTE_PTYPE_L3_IPV6,
858                 RTE_PTYPE_L4_TCP,
859                 RTE_PTYPE_L4_UDP,
860                 RTE_PTYPE_L4_SCTP,
861                 RTE_PTYPE_L4_ICMP,
862                 RTE_PTYPE_UNKNOWN
863         };
864
865         if (dev->rx_pkt_burst == atl_recv_pkts)
866                 return ptypes;
867
868         return NULL;
869 }
870
871 /* return 0 means link status changed, -1 means not changed */
872 static int
873 atl_dev_link_update(struct rte_eth_dev *dev, int wait __rte_unused)
874 {
875         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
876         struct atl_interrupt *intr =
877                 ATL_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
878         struct rte_eth_link link, old;
879         int err = 0;
880
881         link.link_status = ETH_LINK_DOWN;
882         link.link_speed = 0;
883         link.link_duplex = ETH_LINK_FULL_DUPLEX;
884         link.link_autoneg = hw->is_autoneg ? ETH_LINK_AUTONEG : ETH_LINK_FIXED;
885         memset(&old, 0, sizeof(old));
886
887         /* load old link status */
888         rte_eth_linkstatus_get(dev, &old);
889
890         /* read current link status */
891         err = hw->aq_fw_ops->update_link_status(hw);
892
893         if (err)
894                 return 0;
895
896         if (hw->aq_link_status.mbps == 0) {
897                 /* write default (down) link status */
898                 rte_eth_linkstatus_set(dev, &link);
899                 if (link.link_status == old.link_status)
900                         return -1;
901                 return 0;
902         }
903
904         intr->flags &= ~ATL_FLAG_NEED_LINK_CONFIG;
905
906         link.link_status = ETH_LINK_UP;
907         link.link_duplex = ETH_LINK_FULL_DUPLEX;
908         link.link_speed = hw->aq_link_status.mbps;
909
910         rte_eth_linkstatus_set(dev, &link);
911
912         if (link.link_status == old.link_status)
913                 return -1;
914
915         return 0;
916 }
917
918 static void
919 atl_dev_promiscuous_enable(struct rte_eth_dev *dev)
920 {
921         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
922
923         hw_atl_rpfl2promiscuous_mode_en_set(hw, true);
924 }
925
926 static void
927 atl_dev_promiscuous_disable(struct rte_eth_dev *dev)
928 {
929         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
930
931         hw_atl_rpfl2promiscuous_mode_en_set(hw, false);
932 }
933
934 static void
935 atl_dev_allmulticast_enable(struct rte_eth_dev *dev)
936 {
937         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
938
939         hw_atl_rpfl2_accept_all_mc_packets_set(hw, true);
940 }
941
942 static void
943 atl_dev_allmulticast_disable(struct rte_eth_dev *dev)
944 {
945         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
946
947         if (dev->data->promiscuous == 1)
948                 return; /* must remain in all_multicast mode */
949
950         hw_atl_rpfl2_accept_all_mc_packets_set(hw, false);
951 }
952
953 /**
954  * It clears the interrupt causes and enables the interrupt.
955  * It will be called once only during nic initialized.
956  *
957  * @param dev
958  *  Pointer to struct rte_eth_dev.
959  * @param on
960  *  Enable or Disable.
961  *
962  * @return
963  *  - On success, zero.
964  *  - On failure, a negative value.
965  */
966
967 static int
968 atl_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on __rte_unused)
969 {
970         atl_dev_link_status_print(dev);
971         return 0;
972 }
973
974 static int
975 atl_dev_rxq_interrupt_setup(struct rte_eth_dev *dev __rte_unused)
976 {
977         return 0;
978 }
979
980
981 static int
982 atl_dev_interrupt_get_status(struct rte_eth_dev *dev)
983 {
984         struct atl_interrupt *intr =
985                 ATL_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
986         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
987         u64 cause = 0;
988
989         hw_atl_b0_hw_irq_read(hw, &cause);
990
991         atl_disable_intr(hw);
992         intr->flags = cause & BIT(ATL_IRQ_CAUSE_LINK) ?
993                         ATL_FLAG_NEED_LINK_UPDATE : 0;
994
995         return 0;
996 }
997
998 /**
999  * It gets and then prints the link status.
1000  *
1001  * @param dev
1002  *  Pointer to struct rte_eth_dev.
1003  *
1004  * @return
1005  *  - On success, zero.
1006  *  - On failure, a negative value.
1007  */
1008 static void
1009 atl_dev_link_status_print(struct rte_eth_dev *dev)
1010 {
1011         struct rte_eth_link link;
1012
1013         memset(&link, 0, sizeof(link));
1014         rte_eth_linkstatus_get(dev, &link);
1015         if (link.link_status) {
1016                 PMD_DRV_LOG(INFO, "Port %d: Link Up - speed %u Mbps - %s",
1017                                         (int)(dev->data->port_id),
1018                                         (unsigned int)link.link_speed,
1019                         link.link_duplex == ETH_LINK_FULL_DUPLEX ?
1020                                         "full-duplex" : "half-duplex");
1021         } else {
1022                 PMD_DRV_LOG(INFO, " Port %d: Link Down",
1023                                 (int)(dev->data->port_id));
1024         }
1025
1026
1027 #ifdef DEBUG
1028 {
1029         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1030
1031         PMD_DRV_LOG(DEBUG, "PCI Address: " PCI_PRI_FMT,
1032                                 pci_dev->addr.domain,
1033                                 pci_dev->addr.bus,
1034                                 pci_dev->addr.devid,
1035                                 pci_dev->addr.function);
1036 }
1037 #endif
1038
1039         PMD_DRV_LOG(INFO, "Link speed:%d", link.link_speed);
1040 }
1041
1042 /*
1043  * It executes link_update after knowing an interrupt occurred.
1044  *
1045  * @param dev
1046  *  Pointer to struct rte_eth_dev.
1047  *
1048  * @return
1049  *  - On success, zero.
1050  *  - On failure, a negative value.
1051  */
1052 static int
1053 atl_dev_interrupt_action(struct rte_eth_dev *dev,
1054                            struct rte_intr_handle *intr_handle)
1055 {
1056         struct atl_interrupt *intr =
1057                 ATL_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
1058
1059         if (intr->flags & ATL_FLAG_NEED_LINK_UPDATE) {
1060                 atl_dev_link_update(dev, 0);
1061                 intr->flags &= ~ATL_FLAG_NEED_LINK_UPDATE;
1062                 atl_dev_link_status_print(dev);
1063                 _rte_eth_dev_callback_process(dev,
1064                         RTE_ETH_EVENT_INTR_LSC, NULL);
1065         }
1066
1067         atl_enable_intr(dev);
1068         rte_intr_enable(intr_handle);
1069
1070         return 0;
1071 }
1072
1073 /**
1074  * Interrupt handler triggered by NIC  for handling
1075  * specific interrupt.
1076  *
1077  * @param handle
1078  *  Pointer to interrupt handle.
1079  * @param param
1080  *  The address of parameter (struct rte_eth_dev *) regsitered before.
1081  *
1082  * @return
1083  *  void
1084  */
1085 static void
1086 atl_dev_interrupt_handler(void *param)
1087 {
1088         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1089
1090         atl_dev_interrupt_get_status(dev);
1091         atl_dev_interrupt_action(dev, dev->intr_handle);
1092 }
1093
1094 #define SFP_EEPROM_SIZE 0xff
1095
1096 static int
1097 atl_dev_get_eeprom_length(struct rte_eth_dev *dev __rte_unused)
1098 {
1099         return SFP_EEPROM_SIZE;
1100 }
1101
1102 int atl_dev_get_eeprom(struct rte_eth_dev *dev,
1103                        struct rte_dev_eeprom_info *eeprom)
1104 {
1105         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1106         uint32_t dev_addr = SMBUS_DEVICE_ID;
1107
1108         if (hw->aq_fw_ops->get_eeprom == NULL)
1109                 return -ENOTSUP;
1110
1111         if (eeprom->length + eeprom->offset > SFP_EEPROM_SIZE ||
1112             eeprom->data == NULL)
1113                 return -EINVAL;
1114
1115         if (eeprom->magic)
1116                 dev_addr = eeprom->magic;
1117
1118         return hw->aq_fw_ops->get_eeprom(hw, dev_addr, eeprom->data,
1119                                          eeprom->length, eeprom->offset);
1120 }
1121
1122 int atl_dev_set_eeprom(struct rte_eth_dev *dev,
1123                        struct rte_dev_eeprom_info *eeprom)
1124 {
1125         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1126         uint32_t dev_addr = SMBUS_DEVICE_ID;
1127
1128         if (hw->aq_fw_ops->set_eeprom == NULL)
1129                 return -ENOTSUP;
1130
1131         if (eeprom->length != SFP_EEPROM_SIZE || eeprom->data == NULL)
1132                 return -EINVAL;
1133
1134         if (eeprom->magic)
1135                 dev_addr = eeprom->magic;
1136
1137         return hw->aq_fw_ops->set_eeprom(hw, dev_addr,
1138                                          eeprom->data, eeprom->length);
1139 }
1140
1141 static int
1142 atl_dev_get_regs(struct rte_eth_dev *dev, struct rte_dev_reg_info *regs)
1143 {
1144         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1145         u32 mif_id;
1146         int err;
1147
1148         if (regs->data == NULL) {
1149                 regs->length = hw_atl_utils_hw_get_reg_length();
1150                 regs->width = sizeof(u32);
1151                 return 0;
1152         }
1153
1154         /* Only full register dump is supported */
1155         if (regs->length && regs->length != hw_atl_utils_hw_get_reg_length())
1156                 return -ENOTSUP;
1157
1158         err = hw_atl_utils_hw_get_regs(hw, regs->data);
1159
1160         /* Device version */
1161         mif_id = hw_atl_reg_glb_mif_id_get(hw);
1162         regs->version = mif_id & 0xFFU;
1163
1164         return err;
1165 }
1166
1167 static int
1168 atl_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1169 {
1170         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1171
1172         if (hw->aq_nic_cfg->flow_control == AQ_NIC_FC_OFF)
1173                 fc_conf->mode = RTE_FC_NONE;
1174         else if (hw->aq_nic_cfg->flow_control & (AQ_NIC_FC_RX | AQ_NIC_FC_TX))
1175                 fc_conf->mode = RTE_FC_FULL;
1176         else if (hw->aq_nic_cfg->flow_control & AQ_NIC_FC_RX)
1177                 fc_conf->mode = RTE_FC_RX_PAUSE;
1178         else if (hw->aq_nic_cfg->flow_control & AQ_NIC_FC_RX)
1179                 fc_conf->mode = RTE_FC_TX_PAUSE;
1180
1181         return 0;
1182 }
1183
1184 static int
1185 atl_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1186 {
1187         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1188         uint32_t old_flow_control = hw->aq_nic_cfg->flow_control;
1189
1190
1191         if (hw->aq_fw_ops->set_flow_control == NULL)
1192                 return -ENOTSUP;
1193
1194         if (fc_conf->mode == RTE_FC_NONE)
1195                 hw->aq_nic_cfg->flow_control = AQ_NIC_FC_OFF;
1196         else if (fc_conf->mode == RTE_FC_RX_PAUSE)
1197                 hw->aq_nic_cfg->flow_control = AQ_NIC_FC_RX;
1198         else if (fc_conf->mode == RTE_FC_TX_PAUSE)
1199                 hw->aq_nic_cfg->flow_control = AQ_NIC_FC_TX;
1200         else if (fc_conf->mode == RTE_FC_FULL)
1201                 hw->aq_nic_cfg->flow_control = (AQ_NIC_FC_RX | AQ_NIC_FC_TX);
1202
1203         if (old_flow_control != hw->aq_nic_cfg->flow_control)
1204                 return hw->aq_fw_ops->set_flow_control(hw);
1205
1206         return 0;
1207 }
1208
1209 static int
1210 atl_update_mac_addr(struct rte_eth_dev *dev, uint32_t index,
1211                     u8 *mac_addr, bool enable)
1212 {
1213         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1214         unsigned int h = 0U;
1215         unsigned int l = 0U;
1216         int err;
1217
1218         if (mac_addr) {
1219                 h = (mac_addr[0] << 8) | (mac_addr[1]);
1220                 l = (mac_addr[2] << 24) | (mac_addr[3] << 16) |
1221                         (mac_addr[4] << 8) | mac_addr[5];
1222         }
1223
1224         hw_atl_rpfl2_uc_flr_en_set(hw, 0U, index);
1225         hw_atl_rpfl2unicast_dest_addresslsw_set(hw, l, index);
1226         hw_atl_rpfl2unicast_dest_addressmsw_set(hw, h, index);
1227
1228         if (enable)
1229                 hw_atl_rpfl2_uc_flr_en_set(hw, 1U, index);
1230
1231         err = aq_hw_err_from_flags(hw);
1232
1233         return err;
1234 }
1235
1236 static int
1237 atl_add_mac_addr(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
1238                         uint32_t index __rte_unused, uint32_t pool __rte_unused)
1239 {
1240         if (is_zero_ether_addr(mac_addr)) {
1241                 PMD_DRV_LOG(ERR, "Invalid Ethernet Address");
1242                 return -EINVAL;
1243         }
1244
1245         return atl_update_mac_addr(dev, index, (u8 *)mac_addr, true);
1246 }
1247
1248 static void
1249 atl_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index)
1250 {
1251         atl_update_mac_addr(dev, index, NULL, false);
1252 }
1253
1254 static int
1255 atl_set_default_mac_addr(struct rte_eth_dev *dev, struct ether_addr *addr)
1256 {
1257         atl_remove_mac_addr(dev, 0);
1258         atl_add_mac_addr(dev, addr, 0, 0);
1259         return 0;
1260 }
1261
1262 static int
1263 atl_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1264 {
1265         struct rte_eth_dev_info dev_info;
1266         uint32_t frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1267
1268         atl_dev_info_get(dev, &dev_info);
1269
1270         if ((mtu < ETHER_MIN_MTU) || (frame_size > dev_info.max_rx_pktlen))
1271                 return -EINVAL;
1272
1273         /* update max frame size */
1274         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
1275
1276         return 0;
1277 }
1278
1279 static int
1280 atl_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1281 {
1282         struct aq_hw_cfg_s *cfg =
1283                 ATL_DEV_PRIVATE_TO_CFG(dev->data->dev_private);
1284         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1285         int err = 0;
1286         int i = 0;
1287
1288         PMD_INIT_FUNC_TRACE();
1289
1290         for (i = 0; i < HW_ATL_B0_MAX_VLAN_IDS; i++) {
1291                 if (cfg->vlan_filter[i] == vlan_id) {
1292                         if (!on) {
1293                                 /* Disable VLAN filter. */
1294                                 hw_atl_rpf_vlan_flr_en_set(hw, 0U, i);
1295
1296                                 /* Clear VLAN filter entry */
1297                                 cfg->vlan_filter[i] = 0;
1298                         }
1299                         break;
1300                 }
1301         }
1302
1303         /* VLAN_ID was not found. So, nothing to delete. */
1304         if (i == HW_ATL_B0_MAX_VLAN_IDS && !on)
1305                 goto exit;
1306
1307         /* VLAN_ID already exist, or already removed above. Nothing to do. */
1308         if (i != HW_ATL_B0_MAX_VLAN_IDS)
1309                 goto exit;
1310
1311         /* Try to found free VLAN filter to add new VLAN_ID */
1312         for (i = 0; i < HW_ATL_B0_MAX_VLAN_IDS; i++) {
1313                 if (cfg->vlan_filter[i] == 0)
1314                         break;
1315         }
1316
1317         if (i == HW_ATL_B0_MAX_VLAN_IDS) {
1318                 /* We have no free VLAN filter to add new VLAN_ID*/
1319                 err = -ENOMEM;
1320                 goto exit;
1321         }
1322
1323         cfg->vlan_filter[i] = vlan_id;
1324         hw_atl_rpf_vlan_flr_act_set(hw, 1U, i);
1325         hw_atl_rpf_vlan_id_flr_set(hw, vlan_id, i);
1326         hw_atl_rpf_vlan_flr_en_set(hw, 1U, i);
1327
1328 exit:
1329         /* Enable VLAN promisc mode if vlan_filter empty  */
1330         for (i = 0; i < HW_ATL_B0_MAX_VLAN_IDS; i++) {
1331                 if (cfg->vlan_filter[i] != 0)
1332                         break;
1333         }
1334
1335         hw_atl_rpf_vlan_prom_mode_en_set(hw, i == HW_ATL_B0_MAX_VLAN_IDS);
1336
1337         return err;
1338 }
1339
1340 static int
1341 atl_enable_vlan_filter(struct rte_eth_dev *dev, int en)
1342 {
1343         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1344         struct aq_hw_cfg_s *cfg =
1345                 ATL_DEV_PRIVATE_TO_CFG(dev->data->dev_private);
1346         int i;
1347
1348         PMD_INIT_FUNC_TRACE();
1349
1350         for (i = 0; i < HW_ATL_B0_MAX_VLAN_IDS; i++) {
1351                 if (cfg->vlan_filter[i])
1352                         hw_atl_rpf_vlan_flr_en_set(hw, en, i);
1353         }
1354         return 0;
1355 }
1356
1357 static int
1358 atl_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1359 {
1360         struct aq_hw_cfg_s *cfg =
1361                 ATL_DEV_PRIVATE_TO_CFG(dev->data->dev_private);
1362         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1363         int ret = 0;
1364         int i;
1365
1366         PMD_INIT_FUNC_TRACE();
1367
1368         ret = atl_enable_vlan_filter(dev, mask & ETH_VLAN_FILTER_MASK);
1369
1370         cfg->vlan_strip = !!(mask & ETH_VLAN_STRIP_MASK);
1371
1372         for (i = 0; i < dev->data->nb_rx_queues; i++)
1373                 hw_atl_rpo_rx_desc_vlan_stripping_set(hw, cfg->vlan_strip, i);
1374
1375         if (mask & ETH_VLAN_EXTEND_MASK)
1376                 ret = -ENOTSUP;
1377
1378         return ret;
1379 }
1380
1381 static int
1382 atl_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
1383                   uint16_t tpid)
1384 {
1385         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1386         int err = 0;
1387
1388         PMD_INIT_FUNC_TRACE();
1389
1390         switch (vlan_type) {
1391         case ETH_VLAN_TYPE_INNER:
1392                 hw_atl_rpf_vlan_inner_etht_set(hw, tpid);
1393                 break;
1394         case ETH_VLAN_TYPE_OUTER:
1395                 hw_atl_rpf_vlan_outer_etht_set(hw, tpid);
1396                 break;
1397         default:
1398                 PMD_DRV_LOG(ERR, "Unsupported VLAN type");
1399                 err = -ENOTSUP;
1400         }
1401
1402         return err;
1403 }
1404
1405 static void
1406 atl_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue_id, int on)
1407 {
1408         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1409
1410         PMD_INIT_FUNC_TRACE();
1411
1412         if (queue_id > dev->data->nb_rx_queues) {
1413                 PMD_DRV_LOG(ERR, "Invalid queue id");
1414                 return;
1415         }
1416
1417         hw_atl_rpo_rx_desc_vlan_stripping_set(hw, on, queue_id);
1418 }
1419
1420 static int
1421 atl_dev_set_mc_addr_list(struct rte_eth_dev *dev,
1422                           struct ether_addr *mc_addr_set,
1423                           uint32_t nb_mc_addr)
1424 {
1425         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1426         u32 i;
1427
1428         if (nb_mc_addr > AQ_HW_MULTICAST_ADDRESS_MAX - HW_ATL_B0_MAC_MIN)
1429                 return -EINVAL;
1430
1431         /* Update whole uc filters table */
1432         for (i = 0; i < AQ_HW_MULTICAST_ADDRESS_MAX - HW_ATL_B0_MAC_MIN; i++) {
1433                 u8 *mac_addr = NULL;
1434                 u32 l = 0, h = 0;
1435
1436                 if (i < nb_mc_addr) {
1437                         mac_addr = mc_addr_set[i].addr_bytes;
1438                         l = (mac_addr[2] << 24) | (mac_addr[3] << 16) |
1439                                 (mac_addr[4] << 8) | mac_addr[5];
1440                         h = (mac_addr[0] << 8) | mac_addr[1];
1441                 }
1442
1443                 hw_atl_rpfl2_uc_flr_en_set(hw, 0U, HW_ATL_B0_MAC_MIN + i);
1444                 hw_atl_rpfl2unicast_dest_addresslsw_set(hw, l,
1445                                                         HW_ATL_B0_MAC_MIN + i);
1446                 hw_atl_rpfl2unicast_dest_addressmsw_set(hw, h,
1447                                                         HW_ATL_B0_MAC_MIN + i);
1448                 hw_atl_rpfl2_uc_flr_en_set(hw, !!mac_addr,
1449                                            HW_ATL_B0_MAC_MIN + i);
1450         }
1451
1452         return 0;
1453 }
1454
1455 static int
1456 atl_reta_update(struct rte_eth_dev *dev,
1457                    struct rte_eth_rss_reta_entry64 *reta_conf,
1458                    uint16_t reta_size)
1459 {
1460         int i;
1461         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1462         struct aq_hw_cfg_s *cf = ATL_DEV_PRIVATE_TO_CFG(dev->data->dev_private);
1463
1464         for (i = 0; i < reta_size && i < cf->aq_rss.indirection_table_size; i++)
1465                 cf->aq_rss.indirection_table[i] = min(reta_conf->reta[i],
1466                                         dev->data->nb_rx_queues - 1);
1467
1468         hw_atl_b0_hw_rss_set(hw, &cf->aq_rss);
1469         return 0;
1470 }
1471
1472 static int
1473 atl_reta_query(struct rte_eth_dev *dev,
1474                     struct rte_eth_rss_reta_entry64 *reta_conf,
1475                     uint16_t reta_size)
1476 {
1477         int i;
1478         struct aq_hw_cfg_s *cf = ATL_DEV_PRIVATE_TO_CFG(dev->data->dev_private);
1479
1480         for (i = 0; i < reta_size && i < cf->aq_rss.indirection_table_size; i++)
1481                 reta_conf->reta[i] = cf->aq_rss.indirection_table[i];
1482         reta_conf->mask = ~0U;
1483         return 0;
1484 }
1485
1486 static int
1487 atl_rss_hash_update(struct rte_eth_dev *dev,
1488                                  struct rte_eth_rss_conf *rss_conf)
1489 {
1490         struct aq_hw_s *hw = ATL_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1491         struct aq_hw_cfg_s *cfg =
1492                 ATL_DEV_PRIVATE_TO_CFG(dev->data->dev_private);
1493         static u8 def_rss_key[40] = {
1494                 0x1e, 0xad, 0x71, 0x87, 0x65, 0xfc, 0x26, 0x7d,
1495                 0x0d, 0x45, 0x67, 0x74, 0xcd, 0x06, 0x1a, 0x18,
1496                 0xb6, 0xc1, 0xf0, 0xc7, 0xbb, 0x18, 0xbe, 0xf8,
1497                 0x19, 0x13, 0x4b, 0xa9, 0xd0, 0x3e, 0xfe, 0x70,
1498                 0x25, 0x03, 0xab, 0x50, 0x6a, 0x8b, 0x82, 0x0c
1499         };
1500
1501         cfg->is_rss = !!rss_conf->rss_hf;
1502         if (rss_conf->rss_key) {
1503                 memcpy(cfg->aq_rss.hash_secret_key, rss_conf->rss_key,
1504                        rss_conf->rss_key_len);
1505                 cfg->aq_rss.hash_secret_key_size = rss_conf->rss_key_len;
1506         } else {
1507                 memcpy(cfg->aq_rss.hash_secret_key, def_rss_key,
1508                        sizeof(def_rss_key));
1509                 cfg->aq_rss.hash_secret_key_size = sizeof(def_rss_key);
1510         }
1511
1512         hw_atl_b0_hw_rss_set(hw, &cfg->aq_rss);
1513         hw_atl_b0_hw_rss_hash_set(hw, &cfg->aq_rss);
1514         return 0;
1515 }
1516
1517 static int
1518 atl_rss_hash_conf_get(struct rte_eth_dev *dev,
1519                                  struct rte_eth_rss_conf *rss_conf)
1520 {
1521         struct aq_hw_cfg_s *cfg =
1522                 ATL_DEV_PRIVATE_TO_CFG(dev->data->dev_private);
1523
1524         rss_conf->rss_hf = cfg->is_rss ? ATL_RSS_OFFLOAD_ALL : 0;
1525         if (rss_conf->rss_key) {
1526                 rss_conf->rss_key_len = cfg->aq_rss.hash_secret_key_size;
1527                 memcpy(rss_conf->rss_key, cfg->aq_rss.hash_secret_key,
1528                        rss_conf->rss_key_len);
1529         }
1530
1531         return 0;
1532 }
1533
1534 RTE_PMD_REGISTER_PCI(net_atlantic, rte_atl_pmd);
1535 RTE_PMD_REGISTER_PCI_TABLE(net_atlantic, pci_id_atl_map);
1536 RTE_PMD_REGISTER_KMOD_DEP(net_atlantic, "* igb_uio | uio_pci_generic");
1537
1538 RTE_INIT(atl_init_log)
1539 {
1540         atl_logtype_init = rte_log_register("pmd.net.atlantic.init");
1541         if (atl_logtype_init >= 0)
1542                 rte_log_set_level(atl_logtype_init, RTE_LOG_NOTICE);
1543         atl_logtype_driver = rte_log_register("pmd.net.atlantic.driver");
1544         if (atl_logtype_driver >= 0)
1545                 rte_log_set_level(atl_logtype_driver, RTE_LOG_NOTICE);
1546 }
1547