a685102b9392f7432d112720f22c5651ca77520f
[dpdk.git] / drivers / net / vmxnet3 / vmxnet3_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #include <sys/queue.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdarg.h>
12 #include <fcntl.h>
13 #include <inttypes.h>
14 #include <rte_byteorder.h>
15 #include <rte_common.h>
16 #include <rte_cycles.h>
17
18 #include <rte_interrupts.h>
19 #include <rte_log.h>
20 #include <rte_debug.h>
21 #include <rte_pci.h>
22 #include <rte_bus_pci.h>
23 #include <rte_branch_prediction.h>
24 #include <rte_memory.h>
25 #include <rte_memzone.h>
26 #include <rte_eal.h>
27 #include <rte_alarm.h>
28 #include <rte_ether.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_ethdev_pci.h>
31 #include <rte_string_fns.h>
32 #include <rte_malloc.h>
33 #include <rte_dev.h>
34
35 #include "base/vmxnet3_defs.h"
36
37 #include "vmxnet3_ring.h"
38 #include "vmxnet3_logs.h"
39 #include "vmxnet3_ethdev.h"
40
41 #define PROCESS_SYS_EVENTS 0
42
43 #define VMXNET3_TX_MAX_SEG      UINT8_MAX
44
45 #define VMXNET3_TX_OFFLOAD_CAP          \
46         (DEV_TX_OFFLOAD_VLAN_INSERT |   \
47          DEV_TX_OFFLOAD_TCP_CKSUM |     \
48          DEV_TX_OFFLOAD_UDP_CKSUM |     \
49          DEV_TX_OFFLOAD_TCP_TSO |       \
50          DEV_TX_OFFLOAD_MULTI_SEGS)
51
52 #define VMXNET3_RX_OFFLOAD_CAP          \
53         (DEV_RX_OFFLOAD_VLAN_STRIP |    \
54          DEV_RX_OFFLOAD_VLAN_FILTER |   \
55          DEV_RX_OFFLOAD_SCATTER |       \
56          DEV_RX_OFFLOAD_UDP_CKSUM |     \
57          DEV_RX_OFFLOAD_TCP_CKSUM |     \
58          DEV_RX_OFFLOAD_TCP_LRO |       \
59          DEV_RX_OFFLOAD_JUMBO_FRAME |   \
60          DEV_RX_OFFLOAD_RSS_HASH)
61
62 static int eth_vmxnet3_dev_init(struct rte_eth_dev *eth_dev);
63 static int eth_vmxnet3_dev_uninit(struct rte_eth_dev *eth_dev);
64 static int vmxnet3_dev_configure(struct rte_eth_dev *dev);
65 static int vmxnet3_dev_start(struct rte_eth_dev *dev);
66 static int vmxnet3_dev_stop(struct rte_eth_dev *dev);
67 static int vmxnet3_dev_close(struct rte_eth_dev *dev);
68 static void vmxnet3_dev_set_rxmode(struct vmxnet3_hw *hw, uint32_t feature, int set);
69 static int vmxnet3_dev_promiscuous_enable(struct rte_eth_dev *dev);
70 static int vmxnet3_dev_promiscuous_disable(struct rte_eth_dev *dev);
71 static int vmxnet3_dev_allmulticast_enable(struct rte_eth_dev *dev);
72 static int vmxnet3_dev_allmulticast_disable(struct rte_eth_dev *dev);
73 static int __vmxnet3_dev_link_update(struct rte_eth_dev *dev,
74                                      int wait_to_complete);
75 static int vmxnet3_dev_link_update(struct rte_eth_dev *dev,
76                                    int wait_to_complete);
77 static void vmxnet3_hw_stats_save(struct vmxnet3_hw *hw);
78 static int vmxnet3_dev_stats_get(struct rte_eth_dev *dev,
79                                   struct rte_eth_stats *stats);
80 static int vmxnet3_dev_stats_reset(struct rte_eth_dev *dev);
81 static int vmxnet3_dev_xstats_get_names(struct rte_eth_dev *dev,
82                                         struct rte_eth_xstat_name *xstats,
83                                         unsigned int n);
84 static int vmxnet3_dev_xstats_get(struct rte_eth_dev *dev,
85                                   struct rte_eth_xstat *xstats, unsigned int n);
86 static int vmxnet3_dev_info_get(struct rte_eth_dev *dev,
87                                 struct rte_eth_dev_info *dev_info);
88 static const uint32_t *
89 vmxnet3_dev_supported_ptypes_get(struct rte_eth_dev *dev);
90 static int vmxnet3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
91 static int vmxnet3_dev_vlan_filter_set(struct rte_eth_dev *dev,
92                                        uint16_t vid, int on);
93 static int vmxnet3_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
94 static int vmxnet3_mac_addr_set(struct rte_eth_dev *dev,
95                                  struct rte_ether_addr *mac_addr);
96 static void vmxnet3_interrupt_handler(void *param);
97
98 /*
99  * The set of PCI devices this driver supports
100  */
101 #define VMWARE_PCI_VENDOR_ID 0x15AD
102 #define VMWARE_DEV_ID_VMXNET3 0x07B0
103 static const struct rte_pci_id pci_id_vmxnet3_map[] = {
104         { RTE_PCI_DEVICE(VMWARE_PCI_VENDOR_ID, VMWARE_DEV_ID_VMXNET3) },
105         { .vendor_id = 0, /* sentinel */ },
106 };
107
108 static const struct eth_dev_ops vmxnet3_eth_dev_ops = {
109         .dev_configure        = vmxnet3_dev_configure,
110         .dev_start            = vmxnet3_dev_start,
111         .dev_stop             = vmxnet3_dev_stop,
112         .dev_close            = vmxnet3_dev_close,
113         .promiscuous_enable   = vmxnet3_dev_promiscuous_enable,
114         .promiscuous_disable  = vmxnet3_dev_promiscuous_disable,
115         .allmulticast_enable  = vmxnet3_dev_allmulticast_enable,
116         .allmulticast_disable = vmxnet3_dev_allmulticast_disable,
117         .link_update          = vmxnet3_dev_link_update,
118         .stats_get            = vmxnet3_dev_stats_get,
119         .xstats_get_names     = vmxnet3_dev_xstats_get_names,
120         .xstats_get           = vmxnet3_dev_xstats_get,
121         .stats_reset          = vmxnet3_dev_stats_reset,
122         .mac_addr_set         = vmxnet3_mac_addr_set,
123         .dev_infos_get        = vmxnet3_dev_info_get,
124         .dev_supported_ptypes_get = vmxnet3_dev_supported_ptypes_get,
125         .mtu_set              = vmxnet3_dev_mtu_set,
126         .vlan_filter_set      = vmxnet3_dev_vlan_filter_set,
127         .vlan_offload_set     = vmxnet3_dev_vlan_offload_set,
128         .rx_queue_setup       = vmxnet3_dev_rx_queue_setup,
129         .rx_queue_release     = vmxnet3_dev_rx_queue_release,
130         .tx_queue_setup       = vmxnet3_dev_tx_queue_setup,
131         .tx_queue_release     = vmxnet3_dev_tx_queue_release,
132 };
133
134 struct vmxnet3_xstats_name_off {
135         char name[RTE_ETH_XSTATS_NAME_SIZE];
136         unsigned int offset;
137 };
138
139 /* tx_qX_ is prepended to the name string here */
140 static const struct vmxnet3_xstats_name_off vmxnet3_txq_stat_strings[] = {
141         {"drop_total",         offsetof(struct vmxnet3_txq_stats, drop_total)},
142         {"drop_too_many_segs", offsetof(struct vmxnet3_txq_stats, drop_too_many_segs)},
143         {"drop_tso",           offsetof(struct vmxnet3_txq_stats, drop_tso)},
144         {"tx_ring_full",       offsetof(struct vmxnet3_txq_stats, tx_ring_full)},
145 };
146
147 /* rx_qX_ is prepended to the name string here */
148 static const struct vmxnet3_xstats_name_off vmxnet3_rxq_stat_strings[] = {
149         {"drop_total",           offsetof(struct vmxnet3_rxq_stats, drop_total)},
150         {"drop_err",             offsetof(struct vmxnet3_rxq_stats, drop_err)},
151         {"drop_fcs",             offsetof(struct vmxnet3_rxq_stats, drop_fcs)},
152         {"rx_buf_alloc_failure", offsetof(struct vmxnet3_rxq_stats, rx_buf_alloc_failure)},
153 };
154
155 static const struct rte_memzone *
156 gpa_zone_reserve(struct rte_eth_dev *dev, uint32_t size,
157                  const char *post_string, int socket_id,
158                  uint16_t align, bool reuse)
159 {
160         char z_name[RTE_MEMZONE_NAMESIZE];
161         const struct rte_memzone *mz;
162
163         snprintf(z_name, sizeof(z_name), "eth_p%d_%s",
164                         dev->data->port_id, post_string);
165
166         mz = rte_memzone_lookup(z_name);
167         if (!reuse) {
168                 if (mz)
169                         rte_memzone_free(mz);
170                 return rte_memzone_reserve_aligned(z_name, size, socket_id,
171                                 RTE_MEMZONE_IOVA_CONTIG, align);
172         }
173
174         if (mz)
175                 return mz;
176
177         return rte_memzone_reserve_aligned(z_name, size, socket_id,
178                         RTE_MEMZONE_IOVA_CONTIG, align);
179 }
180
181 /*
182  * This function is based on vmxnet3_disable_intr()
183  */
184 static void
185 vmxnet3_disable_intr(struct vmxnet3_hw *hw)
186 {
187         int i;
188
189         PMD_INIT_FUNC_TRACE();
190
191         hw->shared->devRead.intrConf.intrCtrl |= VMXNET3_IC_DISABLE_ALL;
192         for (i = 0; i < hw->num_intrs; i++)
193                 VMXNET3_WRITE_BAR0_REG(hw, VMXNET3_REG_IMR + i * 8, 1);
194 }
195
196 static void
197 vmxnet3_enable_intr(struct vmxnet3_hw *hw)
198 {
199         int i;
200
201         PMD_INIT_FUNC_TRACE();
202
203         hw->shared->devRead.intrConf.intrCtrl &= ~VMXNET3_IC_DISABLE_ALL;
204         for (i = 0; i < hw->num_intrs; i++)
205                 VMXNET3_WRITE_BAR0_REG(hw, VMXNET3_REG_IMR + i * 8, 0);
206 }
207
208 /*
209  * Gets tx data ring descriptor size.
210  */
211 static uint16_t
212 eth_vmxnet3_txdata_get(struct vmxnet3_hw *hw)
213 {
214         uint16 txdata_desc_size;
215
216         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD,
217                                VMXNET3_CMD_GET_TXDATA_DESC_SIZE);
218         txdata_desc_size = VMXNET3_READ_BAR1_REG(hw, VMXNET3_REG_CMD);
219
220         return (txdata_desc_size < VMXNET3_TXDATA_DESC_MIN_SIZE ||
221                 txdata_desc_size > VMXNET3_TXDATA_DESC_MAX_SIZE ||
222                 txdata_desc_size & VMXNET3_TXDATA_DESC_SIZE_MASK) ?
223                 sizeof(struct Vmxnet3_TxDataDesc) : txdata_desc_size;
224 }
225
226 /*
227  * It returns 0 on success.
228  */
229 static int
230 eth_vmxnet3_dev_init(struct rte_eth_dev *eth_dev)
231 {
232         struct rte_pci_device *pci_dev;
233         struct vmxnet3_hw *hw = eth_dev->data->dev_private;
234         uint32_t mac_hi, mac_lo, ver;
235         struct rte_eth_link link;
236
237         PMD_INIT_FUNC_TRACE();
238
239         eth_dev->dev_ops = &vmxnet3_eth_dev_ops;
240         eth_dev->rx_pkt_burst = &vmxnet3_recv_pkts;
241         eth_dev->tx_pkt_burst = &vmxnet3_xmit_pkts;
242         eth_dev->tx_pkt_prepare = vmxnet3_prep_pkts;
243         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
244
245         /*
246          * for secondary processes, we don't initialize any further as primary
247          * has already done this work.
248          */
249         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
250                 return 0;
251
252         rte_eth_copy_pci_info(eth_dev, pci_dev);
253
254         /* Vendor and Device ID need to be set before init of shared code */
255         hw->device_id = pci_dev->id.device_id;
256         hw->vendor_id = pci_dev->id.vendor_id;
257         hw->hw_addr0 = (void *)pci_dev->mem_resource[0].addr;
258         hw->hw_addr1 = (void *)pci_dev->mem_resource[1].addr;
259
260         hw->num_rx_queues = 1;
261         hw->num_tx_queues = 1;
262         hw->bufs_per_pkt = 1;
263
264         /* Check h/w version compatibility with driver. */
265         ver = VMXNET3_READ_BAR1_REG(hw, VMXNET3_REG_VRRS);
266         PMD_INIT_LOG(DEBUG, "Hardware version : %d", ver);
267
268         if (ver & (1 << VMXNET3_REV_4)) {
269                 VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_VRRS,
270                                        1 << VMXNET3_REV_4);
271                 hw->version = VMXNET3_REV_4 + 1;
272         } else if (ver & (1 << VMXNET3_REV_3)) {
273                 VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_VRRS,
274                                        1 << VMXNET3_REV_3);
275                 hw->version = VMXNET3_REV_3 + 1;
276         } else if (ver & (1 << VMXNET3_REV_2)) {
277                 VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_VRRS,
278                                        1 << VMXNET3_REV_2);
279                 hw->version = VMXNET3_REV_2 + 1;
280         } else if (ver & (1 << VMXNET3_REV_1)) {
281                 VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_VRRS,
282                                        1 << VMXNET3_REV_1);
283                 hw->version = VMXNET3_REV_1 + 1;
284         } else {
285                 PMD_INIT_LOG(ERR, "Incompatible hardware version: %d", ver);
286                 return -EIO;
287         }
288
289         PMD_INIT_LOG(DEBUG, "Using device version %d\n", hw->version);
290
291         /* Check UPT version compatibility with driver. */
292         ver = VMXNET3_READ_BAR1_REG(hw, VMXNET3_REG_UVRS);
293         PMD_INIT_LOG(DEBUG, "UPT hardware version : %d", ver);
294         if (ver & 0x1)
295                 VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_UVRS, 1);
296         else {
297                 PMD_INIT_LOG(ERR, "Incompatible UPT version.");
298                 return -EIO;
299         }
300
301         /* Getting MAC Address */
302         mac_lo = VMXNET3_READ_BAR1_REG(hw, VMXNET3_REG_MACL);
303         mac_hi = VMXNET3_READ_BAR1_REG(hw, VMXNET3_REG_MACH);
304         memcpy(hw->perm_addr, &mac_lo, 4);
305         memcpy(hw->perm_addr + 4, &mac_hi, 2);
306
307         /* Allocate memory for storing MAC addresses */
308         eth_dev->data->mac_addrs = rte_zmalloc("vmxnet3", RTE_ETHER_ADDR_LEN *
309                                                VMXNET3_MAX_MAC_ADDRS, 0);
310         if (eth_dev->data->mac_addrs == NULL) {
311                 PMD_INIT_LOG(ERR,
312                              "Failed to allocate %d bytes needed to store MAC addresses",
313                              RTE_ETHER_ADDR_LEN * VMXNET3_MAX_MAC_ADDRS);
314                 return -ENOMEM;
315         }
316         /* Copy the permanent MAC address */
317         rte_ether_addr_copy((struct rte_ether_addr *)hw->perm_addr,
318                         &eth_dev->data->mac_addrs[0]);
319
320         PMD_INIT_LOG(DEBUG, "MAC Address : %02x:%02x:%02x:%02x:%02x:%02x",
321                      hw->perm_addr[0], hw->perm_addr[1], hw->perm_addr[2],
322                      hw->perm_addr[3], hw->perm_addr[4], hw->perm_addr[5]);
323
324         /* Put device in Quiesce Mode */
325         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD, VMXNET3_CMD_QUIESCE_DEV);
326
327         /* allow untagged pkts */
328         VMXNET3_SET_VFTABLE_ENTRY(hw->shadow_vfta, 0);
329
330         hw->txdata_desc_size = VMXNET3_VERSION_GE_3(hw) ?
331                 eth_vmxnet3_txdata_get(hw) : sizeof(struct Vmxnet3_TxDataDesc);
332
333         hw->rxdata_desc_size = VMXNET3_VERSION_GE_3(hw) ?
334                 VMXNET3_DEF_RXDATA_DESC_SIZE : 0;
335         RTE_ASSERT((hw->rxdata_desc_size & ~VMXNET3_RXDATA_DESC_SIZE_MASK) ==
336                    hw->rxdata_desc_size);
337
338         /* clear shadow stats */
339         memset(hw->saved_tx_stats, 0, sizeof(hw->saved_tx_stats));
340         memset(hw->saved_rx_stats, 0, sizeof(hw->saved_rx_stats));
341
342         /* clear snapshot stats */
343         memset(hw->snapshot_tx_stats, 0, sizeof(hw->snapshot_tx_stats));
344         memset(hw->snapshot_rx_stats, 0, sizeof(hw->snapshot_rx_stats));
345
346         /* set the initial link status */
347         memset(&link, 0, sizeof(link));
348         link.link_duplex = ETH_LINK_FULL_DUPLEX;
349         link.link_speed = ETH_SPEED_NUM_10G;
350         link.link_autoneg = ETH_LINK_FIXED;
351         rte_eth_linkstatus_set(eth_dev, &link);
352
353         return 0;
354 }
355
356 static int
357 eth_vmxnet3_dev_uninit(struct rte_eth_dev *eth_dev)
358 {
359         struct vmxnet3_hw *hw = eth_dev->data->dev_private;
360
361         PMD_INIT_FUNC_TRACE();
362
363         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
364                 return 0;
365
366         if (hw->adapter_stopped == 0) {
367                 PMD_INIT_LOG(DEBUG, "Device has not been closed.");
368                 return -EBUSY;
369         }
370
371         return 0;
372 }
373
374 static int eth_vmxnet3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
375         struct rte_pci_device *pci_dev)
376 {
377         return rte_eth_dev_pci_generic_probe(pci_dev,
378                 sizeof(struct vmxnet3_hw), eth_vmxnet3_dev_init);
379 }
380
381 static int eth_vmxnet3_pci_remove(struct rte_pci_device *pci_dev)
382 {
383         return rte_eth_dev_pci_generic_remove(pci_dev, eth_vmxnet3_dev_uninit);
384 }
385
386 static struct rte_pci_driver rte_vmxnet3_pmd = {
387         .id_table = pci_id_vmxnet3_map,
388         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
389         .probe = eth_vmxnet3_pci_probe,
390         .remove = eth_vmxnet3_pci_remove,
391 };
392
393 static int
394 vmxnet3_dev_configure(struct rte_eth_dev *dev)
395 {
396         const struct rte_memzone *mz;
397         struct vmxnet3_hw *hw = dev->data->dev_private;
398         size_t size;
399
400         PMD_INIT_FUNC_TRACE();
401
402         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
403                 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
404
405         if (dev->data->nb_tx_queues > VMXNET3_MAX_TX_QUEUES ||
406             dev->data->nb_rx_queues > VMXNET3_MAX_RX_QUEUES) {
407                 PMD_INIT_LOG(ERR, "ERROR: Number of queues not supported");
408                 return -EINVAL;
409         }
410
411         if (!rte_is_power_of_2(dev->data->nb_rx_queues)) {
412                 PMD_INIT_LOG(ERR, "ERROR: Number of rx queues not power of 2");
413                 return -EINVAL;
414         }
415
416         size = dev->data->nb_rx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
417                 dev->data->nb_tx_queues * sizeof(struct Vmxnet3_RxQueueDesc);
418
419         if (size > UINT16_MAX)
420                 return -EINVAL;
421
422         hw->num_rx_queues = (uint8_t)dev->data->nb_rx_queues;
423         hw->num_tx_queues = (uint8_t)dev->data->nb_tx_queues;
424
425         /*
426          * Allocate a memzone for Vmxnet3_DriverShared - Vmxnet3_DSDevRead
427          * on current socket
428          */
429         mz = gpa_zone_reserve(dev, sizeof(struct Vmxnet3_DriverShared),
430                               "shared", rte_socket_id(), 8, 1);
431
432         if (mz == NULL) {
433                 PMD_INIT_LOG(ERR, "ERROR: Creating shared zone");
434                 return -ENOMEM;
435         }
436         memset(mz->addr, 0, mz->len);
437
438         hw->shared = mz->addr;
439         hw->sharedPA = mz->iova;
440
441         /*
442          * Allocate a memzone for Vmxnet3_RxQueueDesc - Vmxnet3_TxQueueDesc
443          * on current socket.
444          *
445          * We cannot reuse this memzone from previous allocation as its size
446          * depends on the number of tx and rx queues, which could be different
447          * from one config to another.
448          */
449         mz = gpa_zone_reserve(dev, size, "queuedesc", rte_socket_id(),
450                               VMXNET3_QUEUE_DESC_ALIGN, 0);
451         if (mz == NULL) {
452                 PMD_INIT_LOG(ERR, "ERROR: Creating queue descriptors zone");
453                 return -ENOMEM;
454         }
455         memset(mz->addr, 0, mz->len);
456
457         hw->tqd_start = (Vmxnet3_TxQueueDesc *)mz->addr;
458         hw->rqd_start = (Vmxnet3_RxQueueDesc *)(hw->tqd_start + hw->num_tx_queues);
459
460         hw->queueDescPA = mz->iova;
461         hw->queue_desc_len = (uint16_t)size;
462
463         if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_RSS) {
464                 /* Allocate memory structure for UPT1_RSSConf and configure */
465                 mz = gpa_zone_reserve(dev, sizeof(struct VMXNET3_RSSConf),
466                                       "rss_conf", rte_socket_id(),
467                                       RTE_CACHE_LINE_SIZE, 1);
468                 if (mz == NULL) {
469                         PMD_INIT_LOG(ERR,
470                                      "ERROR: Creating rss_conf structure zone");
471                         return -ENOMEM;
472                 }
473                 memset(mz->addr, 0, mz->len);
474
475                 hw->rss_conf = mz->addr;
476                 hw->rss_confPA = mz->iova;
477         }
478
479         return 0;
480 }
481
482 static void
483 vmxnet3_write_mac(struct vmxnet3_hw *hw, const uint8_t *addr)
484 {
485         uint32_t val;
486
487         PMD_INIT_LOG(DEBUG,
488                      "Writing MAC Address : %02x:%02x:%02x:%02x:%02x:%02x",
489                      addr[0], addr[1], addr[2],
490                      addr[3], addr[4], addr[5]);
491
492         memcpy(&val, addr, 4);
493         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_MACL, val);
494
495         memcpy(&val, addr + 4, 2);
496         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_MACH, val);
497 }
498
499 static int
500 vmxnet3_dev_setup_memreg(struct rte_eth_dev *dev)
501 {
502         struct vmxnet3_hw *hw = dev->data->dev_private;
503         Vmxnet3_DriverShared *shared = hw->shared;
504         Vmxnet3_CmdInfo *cmdInfo;
505         struct rte_mempool *mp[VMXNET3_MAX_RX_QUEUES];
506         uint8_t index[VMXNET3_MAX_RX_QUEUES + VMXNET3_MAX_TX_QUEUES];
507         uint32_t num, i, j, size;
508
509         if (hw->memRegsPA == 0) {
510                 const struct rte_memzone *mz;
511
512                 size = sizeof(Vmxnet3_MemRegs) +
513                         (VMXNET3_MAX_RX_QUEUES + VMXNET3_MAX_TX_QUEUES) *
514                         sizeof(Vmxnet3_MemoryRegion);
515
516                 mz = gpa_zone_reserve(dev, size, "memRegs", rte_socket_id(), 8,
517                                       1);
518                 if (mz == NULL) {
519                         PMD_INIT_LOG(ERR, "ERROR: Creating memRegs zone");
520                         return -ENOMEM;
521                 }
522                 memset(mz->addr, 0, mz->len);
523                 hw->memRegs = mz->addr;
524                 hw->memRegsPA = mz->iova;
525         }
526
527         num = hw->num_rx_queues;
528
529         for (i = 0; i < num; i++) {
530                 vmxnet3_rx_queue_t *rxq = dev->data->rx_queues[i];
531
532                 mp[i] = rxq->mp;
533                 index[i] = 1 << i;
534         }
535
536         /*
537          * The same mempool could be used by multiple queues. In such a case,
538          * remove duplicate mempool entries. Only one entry is kept with
539          * bitmask indicating queues that are using this mempool.
540          */
541         for (i = 1; i < num; i++) {
542                 for (j = 0; j < i; j++) {
543                         if (mp[i] == mp[j]) {
544                                 mp[i] = NULL;
545                                 index[j] |= 1 << i;
546                                 break;
547                         }
548                 }
549         }
550
551         j = 0;
552         for (i = 0; i < num; i++) {
553                 if (mp[i] == NULL)
554                         continue;
555
556                 Vmxnet3_MemoryRegion *mr = &hw->memRegs->memRegs[j];
557
558                 mr->startPA =
559                         (uintptr_t)STAILQ_FIRST(&mp[i]->mem_list)->iova;
560                 mr->length = STAILQ_FIRST(&mp[i]->mem_list)->len <= INT32_MAX ?
561                         STAILQ_FIRST(&mp[i]->mem_list)->len : INT32_MAX;
562                 mr->txQueueBits = index[i];
563                 mr->rxQueueBits = index[i];
564
565                 PMD_INIT_LOG(INFO,
566                              "index: %u startPA: %" PRIu64 " length: %u, "
567                              "rxBits: %x",
568                              j, mr->startPA, mr->length, mr->rxQueueBits);
569                 j++;
570         }
571         hw->memRegs->numRegs = j;
572         PMD_INIT_LOG(INFO, "numRegs: %u", j);
573
574         size = sizeof(Vmxnet3_MemRegs) +
575                 (j - 1) * sizeof(Vmxnet3_MemoryRegion);
576
577         cmdInfo = &shared->cu.cmdInfo;
578         cmdInfo->varConf.confVer = 1;
579         cmdInfo->varConf.confLen = size;
580         cmdInfo->varConf.confPA = hw->memRegsPA;
581
582         return 0;
583 }
584
585 static int
586 vmxnet3_setup_driver_shared(struct rte_eth_dev *dev)
587 {
588         struct rte_eth_conf port_conf = dev->data->dev_conf;
589         struct vmxnet3_hw *hw = dev->data->dev_private;
590         uint32_t mtu = dev->data->mtu;
591         Vmxnet3_DriverShared *shared = hw->shared;
592         Vmxnet3_DSDevRead *devRead = &shared->devRead;
593         uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
594         uint32_t i;
595         int ret;
596
597         hw->mtu = mtu;
598
599         shared->magic = VMXNET3_REV1_MAGIC;
600         devRead->misc.driverInfo.version = VMXNET3_DRIVER_VERSION_NUM;
601
602         /* Setting up Guest OS information */
603         devRead->misc.driverInfo.gos.gosBits   = sizeof(void *) == 4 ?
604                 VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64;
605         devRead->misc.driverInfo.gos.gosType   = VMXNET3_GOS_TYPE_LINUX;
606         devRead->misc.driverInfo.vmxnet3RevSpt = 1;
607         devRead->misc.driverInfo.uptVerSpt     = 1;
608
609         devRead->misc.mtu = rte_le_to_cpu_32(mtu);
610         devRead->misc.queueDescPA  = hw->queueDescPA;
611         devRead->misc.queueDescLen = hw->queue_desc_len;
612         devRead->misc.numTxQueues  = hw->num_tx_queues;
613         devRead->misc.numRxQueues  = hw->num_rx_queues;
614
615         /*
616          * Set number of interrupts to 1
617          * PMD by default disables all the interrupts but this is MUST
618          * to activate device. It needs at least one interrupt for
619          * link events to handle
620          */
621         hw->num_intrs = devRead->intrConf.numIntrs = 1;
622         devRead->intrConf.intrCtrl |= VMXNET3_IC_DISABLE_ALL;
623
624         for (i = 0; i < hw->num_tx_queues; i++) {
625                 Vmxnet3_TxQueueDesc *tqd = &hw->tqd_start[i];
626                 vmxnet3_tx_queue_t *txq  = dev->data->tx_queues[i];
627
628                 txq->shared = &hw->tqd_start[i];
629
630                 tqd->ctrl.txNumDeferred  = 0;
631                 tqd->ctrl.txThreshold    = 1;
632                 tqd->conf.txRingBasePA   = txq->cmd_ring.basePA;
633                 tqd->conf.compRingBasePA = txq->comp_ring.basePA;
634                 tqd->conf.dataRingBasePA = txq->data_ring.basePA;
635
636                 tqd->conf.txRingSize   = txq->cmd_ring.size;
637                 tqd->conf.compRingSize = txq->comp_ring.size;
638                 tqd->conf.dataRingSize = txq->data_ring.size;
639                 tqd->conf.txDataRingDescSize = txq->txdata_desc_size;
640                 tqd->conf.intrIdx      = txq->comp_ring.intr_idx;
641                 tqd->status.stopped    = TRUE;
642                 tqd->status.error      = 0;
643                 memset(&tqd->stats, 0, sizeof(tqd->stats));
644         }
645
646         for (i = 0; i < hw->num_rx_queues; i++) {
647                 Vmxnet3_RxQueueDesc *rqd  = &hw->rqd_start[i];
648                 vmxnet3_rx_queue_t *rxq   = dev->data->rx_queues[i];
649
650                 rxq->shared = &hw->rqd_start[i];
651
652                 rqd->conf.rxRingBasePA[0] = rxq->cmd_ring[0].basePA;
653                 rqd->conf.rxRingBasePA[1] = rxq->cmd_ring[1].basePA;
654                 rqd->conf.compRingBasePA  = rxq->comp_ring.basePA;
655
656                 rqd->conf.rxRingSize[0]   = rxq->cmd_ring[0].size;
657                 rqd->conf.rxRingSize[1]   = rxq->cmd_ring[1].size;
658                 rqd->conf.compRingSize    = rxq->comp_ring.size;
659                 rqd->conf.intrIdx         = rxq->comp_ring.intr_idx;
660                 if (VMXNET3_VERSION_GE_3(hw)) {
661                         rqd->conf.rxDataRingBasePA = rxq->data_ring.basePA;
662                         rqd->conf.rxDataRingDescSize = rxq->data_desc_size;
663                 }
664                 rqd->status.stopped       = TRUE;
665                 rqd->status.error         = 0;
666                 memset(&rqd->stats, 0, sizeof(rqd->stats));
667         }
668
669         /* RxMode set to 0 of VMXNET3_RXM_xxx */
670         devRead->rxFilterConf.rxMode = 0;
671
672         /* Setting up feature flags */
673         if (rx_offloads & DEV_RX_OFFLOAD_CHECKSUM)
674                 devRead->misc.uptFeatures |= VMXNET3_F_RXCSUM;
675
676         if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) {
677                 devRead->misc.uptFeatures |= VMXNET3_F_LRO;
678                 devRead->misc.maxNumRxSG = 0;
679         }
680
681         if (port_conf.rxmode.mq_mode == ETH_MQ_RX_RSS) {
682                 ret = vmxnet3_rss_configure(dev);
683                 if (ret != VMXNET3_SUCCESS)
684                         return ret;
685
686                 devRead->misc.uptFeatures |= VMXNET3_F_RSS;
687                 devRead->rssConfDesc.confVer = 1;
688                 devRead->rssConfDesc.confLen = sizeof(struct VMXNET3_RSSConf);
689                 devRead->rssConfDesc.confPA  = hw->rss_confPA;
690         }
691
692         ret = vmxnet3_dev_vlan_offload_set(dev,
693                         ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK);
694         if (ret)
695                 return ret;
696
697         vmxnet3_write_mac(hw, dev->data->mac_addrs->addr_bytes);
698
699         return VMXNET3_SUCCESS;
700 }
701
702 /*
703  * Configure device link speed and setup link.
704  * Must be called after eth_vmxnet3_dev_init. Other wise it might fail
705  * It returns 0 on success.
706  */
707 static int
708 vmxnet3_dev_start(struct rte_eth_dev *dev)
709 {
710         int ret;
711         struct vmxnet3_hw *hw = dev->data->dev_private;
712
713         PMD_INIT_FUNC_TRACE();
714
715         /* Save stats before it is reset by CMD_ACTIVATE */
716         vmxnet3_hw_stats_save(hw);
717
718         ret = vmxnet3_setup_driver_shared(dev);
719         if (ret != VMXNET3_SUCCESS)
720                 return ret;
721
722         /* check if lsc interrupt feature is enabled */
723         if (dev->data->dev_conf.intr_conf.lsc) {
724                 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev->device);
725
726                 /* Setup interrupt callback  */
727                 rte_intr_callback_register(&pci_dev->intr_handle,
728                                            vmxnet3_interrupt_handler, dev);
729
730                 if (rte_intr_enable(&pci_dev->intr_handle) < 0) {
731                         PMD_INIT_LOG(ERR, "interrupt enable failed");
732                         return -EIO;
733                 }
734         }
735
736         /* Exchange shared data with device */
737         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_DSAL,
738                                VMXNET3_GET_ADDR_LO(hw->sharedPA));
739         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_DSAH,
740                                VMXNET3_GET_ADDR_HI(hw->sharedPA));
741
742         /* Activate device by register write */
743         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD, VMXNET3_CMD_ACTIVATE_DEV);
744         ret = VMXNET3_READ_BAR1_REG(hw, VMXNET3_REG_CMD);
745
746         if (ret != 0) {
747                 PMD_INIT_LOG(ERR, "Device activation: UNSUCCESSFUL");
748                 return -EINVAL;
749         }
750
751         /* Setup memory region for rx buffers */
752         ret = vmxnet3_dev_setup_memreg(dev);
753         if (ret == 0) {
754                 VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD,
755                                        VMXNET3_CMD_REGISTER_MEMREGS);
756                 ret = VMXNET3_READ_BAR1_REG(hw, VMXNET3_REG_CMD);
757                 if (ret != 0)
758                         PMD_INIT_LOG(DEBUG,
759                                      "Failed in setup memory region cmd\n");
760                 ret = 0;
761         } else {
762                 PMD_INIT_LOG(DEBUG, "Failed to setup memory region\n");
763         }
764
765         if (VMXNET3_VERSION_GE_4(hw) &&
766             dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_RSS) {
767                 /* Check for additional RSS  */
768                 ret = vmxnet3_v4_rss_configure(dev);
769                 if (ret != VMXNET3_SUCCESS) {
770                         PMD_INIT_LOG(ERR, "Failed to configure v4 RSS");
771                         return ret;
772                 }
773         }
774
775         /* Disable interrupts */
776         vmxnet3_disable_intr(hw);
777
778         /*
779          * Load RX queues with blank mbufs and update next2fill index for device
780          * Update RxMode of the device
781          */
782         ret = vmxnet3_dev_rxtx_init(dev);
783         if (ret != VMXNET3_SUCCESS) {
784                 PMD_INIT_LOG(ERR, "Device queue init: UNSUCCESSFUL");
785                 return ret;
786         }
787
788         hw->adapter_stopped = FALSE;
789
790         /* Setting proper Rx Mode and issue Rx Mode Update command */
791         vmxnet3_dev_set_rxmode(hw, VMXNET3_RXM_UCAST | VMXNET3_RXM_BCAST, 1);
792
793         if (dev->data->dev_conf.intr_conf.lsc) {
794                 vmxnet3_enable_intr(hw);
795
796                 /*
797                  * Update link state from device since this won't be
798                  * done upon starting with lsc in use. This is done
799                  * only after enabling interrupts to avoid any race
800                  * where the link state could change without an
801                  * interrupt being fired.
802                  */
803                 __vmxnet3_dev_link_update(dev, 0);
804         }
805
806         return VMXNET3_SUCCESS;
807 }
808
809 /*
810  * Stop device: disable rx and tx functions to allow for reconfiguring.
811  */
812 static int
813 vmxnet3_dev_stop(struct rte_eth_dev *dev)
814 {
815         struct rte_eth_link link;
816         struct vmxnet3_hw *hw = dev->data->dev_private;
817
818         PMD_INIT_FUNC_TRACE();
819
820         if (hw->adapter_stopped == 1) {
821                 PMD_INIT_LOG(DEBUG, "Device already stopped.");
822                 return 0;
823         }
824
825         /* disable interrupts */
826         vmxnet3_disable_intr(hw);
827
828         if (dev->data->dev_conf.intr_conf.lsc) {
829                 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev->device);
830
831                 rte_intr_disable(&pci_dev->intr_handle);
832
833                 rte_intr_callback_unregister(&pci_dev->intr_handle,
834                                              vmxnet3_interrupt_handler, dev);
835         }
836
837         /* quiesce the device first */
838         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD, VMXNET3_CMD_QUIESCE_DEV);
839         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_DSAL, 0);
840         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_DSAH, 0);
841
842         /* reset the device */
843         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
844         PMD_INIT_LOG(DEBUG, "Device reset.");
845
846         vmxnet3_dev_clear_queues(dev);
847
848         /* Clear recorded link status */
849         memset(&link, 0, sizeof(link));
850         link.link_duplex = ETH_LINK_FULL_DUPLEX;
851         link.link_speed = ETH_SPEED_NUM_10G;
852         link.link_autoneg = ETH_LINK_FIXED;
853         rte_eth_linkstatus_set(dev, &link);
854
855         hw->adapter_stopped = 1;
856         dev->data->dev_started = 0;
857
858         return 0;
859 }
860
861 static void
862 vmxnet3_free_queues(struct rte_eth_dev *dev)
863 {
864         int i;
865
866         PMD_INIT_FUNC_TRACE();
867
868         for (i = 0; i < dev->data->nb_rx_queues; i++) {
869                 void *rxq = dev->data->rx_queues[i];
870
871                 vmxnet3_dev_rx_queue_release(rxq);
872         }
873         dev->data->nb_rx_queues = 0;
874
875         for (i = 0; i < dev->data->nb_tx_queues; i++) {
876                 void *txq = dev->data->tx_queues[i];
877
878                 vmxnet3_dev_tx_queue_release(txq);
879         }
880         dev->data->nb_tx_queues = 0;
881 }
882
883 /*
884  * Reset and stop device.
885  */
886 static int
887 vmxnet3_dev_close(struct rte_eth_dev *dev)
888 {
889         int ret;
890         PMD_INIT_FUNC_TRACE();
891         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
892                 return 0;
893
894         ret = vmxnet3_dev_stop(dev);
895         vmxnet3_free_queues(dev);
896
897         return ret;
898 }
899
900 static void
901 vmxnet3_hw_tx_stats_get(struct vmxnet3_hw *hw, unsigned int q,
902                         struct UPT1_TxStats *res)
903 {
904 #define VMXNET3_UPDATE_TX_STAT(h, i, f, r)              \
905                 ((r)->f = (h)->tqd_start[(i)].stats.f + \
906                         (h)->saved_tx_stats[(i)].f)
907
908         VMXNET3_UPDATE_TX_STAT(hw, q, ucastPktsTxOK, res);
909         VMXNET3_UPDATE_TX_STAT(hw, q, mcastPktsTxOK, res);
910         VMXNET3_UPDATE_TX_STAT(hw, q, bcastPktsTxOK, res);
911         VMXNET3_UPDATE_TX_STAT(hw, q, ucastBytesTxOK, res);
912         VMXNET3_UPDATE_TX_STAT(hw, q, mcastBytesTxOK, res);
913         VMXNET3_UPDATE_TX_STAT(hw, q, bcastBytesTxOK, res);
914         VMXNET3_UPDATE_TX_STAT(hw, q, pktsTxError, res);
915         VMXNET3_UPDATE_TX_STAT(hw, q, pktsTxDiscard, res);
916
917 #undef VMXNET3_UPDATE_TX_STAT
918 }
919
920 static void
921 vmxnet3_hw_rx_stats_get(struct vmxnet3_hw *hw, unsigned int q,
922                         struct UPT1_RxStats *res)
923 {
924 #define VMXNET3_UPDATE_RX_STAT(h, i, f, r)              \
925                 ((r)->f = (h)->rqd_start[(i)].stats.f + \
926                         (h)->saved_rx_stats[(i)].f)
927
928         VMXNET3_UPDATE_RX_STAT(hw, q, ucastPktsRxOK, res);
929         VMXNET3_UPDATE_RX_STAT(hw, q, mcastPktsRxOK, res);
930         VMXNET3_UPDATE_RX_STAT(hw, q, bcastPktsRxOK, res);
931         VMXNET3_UPDATE_RX_STAT(hw, q, ucastBytesRxOK, res);
932         VMXNET3_UPDATE_RX_STAT(hw, q, mcastBytesRxOK, res);
933         VMXNET3_UPDATE_RX_STAT(hw, q, bcastBytesRxOK, res);
934         VMXNET3_UPDATE_RX_STAT(hw, q, pktsRxError, res);
935         VMXNET3_UPDATE_RX_STAT(hw, q, pktsRxOutOfBuf, res);
936
937 #undef VMXNET3_UPDATE_RX_STAT
938 }
939
940 static void
941 vmxnet3_tx_stats_get(struct vmxnet3_hw *hw, unsigned int q,
942                                         struct UPT1_TxStats *res)
943 {
944                 vmxnet3_hw_tx_stats_get(hw, q, res);
945
946 #define VMXNET3_REDUCE_SNAPSHOT_TX_STAT(h, i, f, r)     \
947                 ((r)->f -= (h)->snapshot_tx_stats[(i)].f)
948
949         VMXNET3_REDUCE_SNAPSHOT_TX_STAT(hw, q, ucastPktsTxOK, res);
950         VMXNET3_REDUCE_SNAPSHOT_TX_STAT(hw, q, mcastPktsTxOK, res);
951         VMXNET3_REDUCE_SNAPSHOT_TX_STAT(hw, q, bcastPktsTxOK, res);
952         VMXNET3_REDUCE_SNAPSHOT_TX_STAT(hw, q, ucastBytesTxOK, res);
953         VMXNET3_REDUCE_SNAPSHOT_TX_STAT(hw, q, mcastBytesTxOK, res);
954         VMXNET3_REDUCE_SNAPSHOT_TX_STAT(hw, q, bcastBytesTxOK, res);
955         VMXNET3_REDUCE_SNAPSHOT_TX_STAT(hw, q, pktsTxError, res);
956         VMXNET3_REDUCE_SNAPSHOT_TX_STAT(hw, q, pktsTxDiscard, res);
957
958 #undef VMXNET3_REDUCE_SNAPSHOT_TX_STAT
959 }
960
961 static void
962 vmxnet3_rx_stats_get(struct vmxnet3_hw *hw, unsigned int q,
963                                         struct UPT1_RxStats *res)
964 {
965                 vmxnet3_hw_rx_stats_get(hw, q, res);
966
967 #define VMXNET3_REDUCE_SNAPSHOT_RX_STAT(h, i, f, r)     \
968                 ((r)->f -= (h)->snapshot_rx_stats[(i)].f)
969
970         VMXNET3_REDUCE_SNAPSHOT_RX_STAT(hw, q, ucastPktsRxOK, res);
971         VMXNET3_REDUCE_SNAPSHOT_RX_STAT(hw, q, mcastPktsRxOK, res);
972         VMXNET3_REDUCE_SNAPSHOT_RX_STAT(hw, q, bcastPktsRxOK, res);
973         VMXNET3_REDUCE_SNAPSHOT_RX_STAT(hw, q, ucastBytesRxOK, res);
974         VMXNET3_REDUCE_SNAPSHOT_RX_STAT(hw, q, mcastBytesRxOK, res);
975         VMXNET3_REDUCE_SNAPSHOT_RX_STAT(hw, q, bcastBytesRxOK, res);
976         VMXNET3_REDUCE_SNAPSHOT_RX_STAT(hw, q, pktsRxError, res);
977         VMXNET3_REDUCE_SNAPSHOT_RX_STAT(hw, q, pktsRxOutOfBuf, res);
978
979 #undef VMXNET3_REDUCE_SNAPSHOT_RX_STAT
980 }
981
982 static void
983 vmxnet3_hw_stats_save(struct vmxnet3_hw *hw)
984 {
985         unsigned int i;
986
987         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD, VMXNET3_CMD_GET_STATS);
988
989         RTE_BUILD_BUG_ON(RTE_ETHDEV_QUEUE_STAT_CNTRS < VMXNET3_MAX_TX_QUEUES);
990
991         for (i = 0; i < hw->num_tx_queues; i++)
992                 vmxnet3_hw_tx_stats_get(hw, i, &hw->saved_tx_stats[i]);
993         for (i = 0; i < hw->num_rx_queues; i++)
994                 vmxnet3_hw_rx_stats_get(hw, i, &hw->saved_rx_stats[i]);
995 }
996
997 static int
998 vmxnet3_dev_xstats_get_names(struct rte_eth_dev *dev,
999                              struct rte_eth_xstat_name *xstats_names,
1000                              unsigned int n)
1001 {
1002         unsigned int i, t, count = 0;
1003         unsigned int nstats =
1004                 dev->data->nb_tx_queues * RTE_DIM(vmxnet3_txq_stat_strings) +
1005                 dev->data->nb_rx_queues * RTE_DIM(vmxnet3_rxq_stat_strings);
1006
1007         if (!xstats_names || n < nstats)
1008                 return nstats;
1009
1010         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1011                 if (!dev->data->rx_queues[i])
1012                         continue;
1013
1014                 for (t = 0; t < RTE_DIM(vmxnet3_rxq_stat_strings); t++) {
1015                         snprintf(xstats_names[count].name,
1016                                  sizeof(xstats_names[count].name),
1017                                  "rx_q%u_%s", i,
1018                                  vmxnet3_rxq_stat_strings[t].name);
1019                         count++;
1020                 }
1021         }
1022
1023         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1024                 if (!dev->data->tx_queues[i])
1025                         continue;
1026
1027                 for (t = 0; t < RTE_DIM(vmxnet3_txq_stat_strings); t++) {
1028                         snprintf(xstats_names[count].name,
1029                                  sizeof(xstats_names[count].name),
1030                                  "tx_q%u_%s", i,
1031                                  vmxnet3_txq_stat_strings[t].name);
1032                         count++;
1033                 }
1034         }
1035
1036         return count;
1037 }
1038
1039 static int
1040 vmxnet3_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
1041                        unsigned int n)
1042 {
1043         unsigned int i, t, count = 0;
1044         unsigned int nstats =
1045                 dev->data->nb_tx_queues * RTE_DIM(vmxnet3_txq_stat_strings) +
1046                 dev->data->nb_rx_queues * RTE_DIM(vmxnet3_rxq_stat_strings);
1047
1048         if (n < nstats)
1049                 return nstats;
1050
1051         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1052                 struct vmxnet3_rx_queue *rxq = dev->data->rx_queues[i];
1053
1054                 if (rxq == NULL)
1055                         continue;
1056
1057                 for (t = 0; t < RTE_DIM(vmxnet3_rxq_stat_strings); t++) {
1058                         xstats[count].value = *(uint64_t *)(((char *)&rxq->stats) +
1059                                 vmxnet3_rxq_stat_strings[t].offset);
1060                         xstats[count].id = count;
1061                         count++;
1062                 }
1063         }
1064
1065         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1066                 struct vmxnet3_tx_queue *txq = dev->data->tx_queues[i];
1067
1068                 if (txq == NULL)
1069                         continue;
1070
1071                 for (t = 0; t < RTE_DIM(vmxnet3_txq_stat_strings); t++) {
1072                         xstats[count].value = *(uint64_t *)(((char *)&txq->stats) +
1073                                 vmxnet3_txq_stat_strings[t].offset);
1074                         xstats[count].id = count;
1075                         count++;
1076                 }
1077         }
1078
1079         return count;
1080 }
1081
1082 static int
1083 vmxnet3_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1084 {
1085         unsigned int i;
1086         struct vmxnet3_hw *hw = dev->data->dev_private;
1087         struct UPT1_TxStats txStats;
1088         struct UPT1_RxStats rxStats;
1089
1090         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD, VMXNET3_CMD_GET_STATS);
1091
1092         RTE_BUILD_BUG_ON(RTE_ETHDEV_QUEUE_STAT_CNTRS < VMXNET3_MAX_TX_QUEUES);
1093         for (i = 0; i < hw->num_tx_queues; i++) {
1094                 vmxnet3_tx_stats_get(hw, i, &txStats);
1095
1096                 stats->q_opackets[i] = txStats.ucastPktsTxOK +
1097                         txStats.mcastPktsTxOK +
1098                         txStats.bcastPktsTxOK;
1099
1100                 stats->q_obytes[i] = txStats.ucastBytesTxOK +
1101                         txStats.mcastBytesTxOK +
1102                         txStats.bcastBytesTxOK;
1103
1104                 stats->opackets += stats->q_opackets[i];
1105                 stats->obytes += stats->q_obytes[i];
1106                 stats->oerrors += txStats.pktsTxError + txStats.pktsTxDiscard;
1107         }
1108
1109         RTE_BUILD_BUG_ON(RTE_ETHDEV_QUEUE_STAT_CNTRS < VMXNET3_MAX_RX_QUEUES);
1110         for (i = 0; i < hw->num_rx_queues; i++) {
1111                 vmxnet3_rx_stats_get(hw, i, &rxStats);
1112
1113                 stats->q_ipackets[i] = rxStats.ucastPktsRxOK +
1114                         rxStats.mcastPktsRxOK +
1115                         rxStats.bcastPktsRxOK;
1116
1117                 stats->q_ibytes[i] = rxStats.ucastBytesRxOK +
1118                         rxStats.mcastBytesRxOK +
1119                         rxStats.bcastBytesRxOK;
1120
1121                 stats->ipackets += stats->q_ipackets[i];
1122                 stats->ibytes += stats->q_ibytes[i];
1123
1124                 stats->q_errors[i] = rxStats.pktsRxError;
1125                 stats->ierrors += rxStats.pktsRxError;
1126                 stats->imissed += rxStats.pktsRxOutOfBuf;
1127         }
1128
1129         return 0;
1130 }
1131
1132 static int
1133 vmxnet3_dev_stats_reset(struct rte_eth_dev *dev)
1134 {
1135         unsigned int i;
1136         struct vmxnet3_hw *hw = dev->data->dev_private;
1137         struct UPT1_TxStats txStats = {0};
1138         struct UPT1_RxStats rxStats = {0};
1139
1140         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD, VMXNET3_CMD_GET_STATS);
1141
1142         RTE_BUILD_BUG_ON(RTE_ETHDEV_QUEUE_STAT_CNTRS < VMXNET3_MAX_TX_QUEUES);
1143
1144         for (i = 0; i < hw->num_tx_queues; i++) {
1145                 vmxnet3_hw_tx_stats_get(hw, i, &txStats);
1146                 memcpy(&hw->snapshot_tx_stats[i], &txStats,
1147                         sizeof(hw->snapshot_tx_stats[0]));
1148         }
1149         for (i = 0; i < hw->num_rx_queues; i++) {
1150                 vmxnet3_hw_rx_stats_get(hw, i, &rxStats);
1151                 memcpy(&hw->snapshot_rx_stats[i], &rxStats,
1152                         sizeof(hw->snapshot_rx_stats[0]));
1153         }
1154
1155         return 0;
1156 }
1157
1158 static int
1159 vmxnet3_dev_info_get(struct rte_eth_dev *dev,
1160                      struct rte_eth_dev_info *dev_info)
1161 {
1162         struct vmxnet3_hw *hw = dev->data->dev_private;
1163
1164         dev_info->max_rx_queues = VMXNET3_MAX_RX_QUEUES;
1165         dev_info->max_tx_queues = VMXNET3_MAX_TX_QUEUES;
1166         dev_info->min_rx_bufsize = 1518 + RTE_PKTMBUF_HEADROOM;
1167         dev_info->max_rx_pktlen = 16384; /* includes CRC, cf MAXFRS register */
1168         dev_info->min_mtu = VMXNET3_MIN_MTU;
1169         dev_info->max_mtu = VMXNET3_MAX_MTU;
1170         dev_info->speed_capa = ETH_LINK_SPEED_10G;
1171         dev_info->max_mac_addrs = VMXNET3_MAX_MAC_ADDRS;
1172
1173         dev_info->flow_type_rss_offloads = VMXNET3_RSS_OFFLOAD_ALL;
1174
1175         if (VMXNET3_VERSION_GE_4(hw)) {
1176                 dev_info->flow_type_rss_offloads |= VMXNET3_V4_RSS_MASK;
1177         }
1178
1179         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
1180                 .nb_max = VMXNET3_RX_RING_MAX_SIZE,
1181                 .nb_min = VMXNET3_DEF_RX_RING_SIZE,
1182                 .nb_align = 1,
1183         };
1184
1185         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
1186                 .nb_max = VMXNET3_TX_RING_MAX_SIZE,
1187                 .nb_min = VMXNET3_DEF_TX_RING_SIZE,
1188                 .nb_align = 1,
1189                 .nb_seg_max = VMXNET3_TX_MAX_SEG,
1190                 .nb_mtu_seg_max = VMXNET3_MAX_TXD_PER_PKT,
1191         };
1192
1193         dev_info->rx_offload_capa = VMXNET3_RX_OFFLOAD_CAP;
1194         dev_info->rx_queue_offload_capa = 0;
1195         dev_info->tx_offload_capa = VMXNET3_TX_OFFLOAD_CAP;
1196         dev_info->tx_queue_offload_capa = 0;
1197
1198         return 0;
1199 }
1200
1201 static const uint32_t *
1202 vmxnet3_dev_supported_ptypes_get(struct rte_eth_dev *dev)
1203 {
1204         static const uint32_t ptypes[] = {
1205                 RTE_PTYPE_L3_IPV4_EXT,
1206                 RTE_PTYPE_L3_IPV4,
1207                 RTE_PTYPE_UNKNOWN
1208         };
1209
1210         if (dev->rx_pkt_burst == vmxnet3_recv_pkts)
1211                 return ptypes;
1212         return NULL;
1213 }
1214
1215 static int
1216 vmxnet3_dev_mtu_set(struct rte_eth_dev *dev, __rte_unused uint16_t mtu)
1217 {
1218         if (dev->data->dev_started) {
1219                 PMD_DRV_LOG(ERR, "Port %d must be stopped to configure MTU",
1220                             dev->data->port_id);
1221                 return -EBUSY;
1222         }
1223
1224         return 0;
1225 }
1226
1227 static int
1228 vmxnet3_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1229 {
1230         struct vmxnet3_hw *hw = dev->data->dev_private;
1231
1232         rte_ether_addr_copy(mac_addr, (struct rte_ether_addr *)(hw->perm_addr));
1233         vmxnet3_write_mac(hw, mac_addr->addr_bytes);
1234         return 0;
1235 }
1236
1237 /* return 0 means link status changed, -1 means not changed */
1238 static int
1239 __vmxnet3_dev_link_update(struct rte_eth_dev *dev,
1240                           __rte_unused int wait_to_complete)
1241 {
1242         struct vmxnet3_hw *hw = dev->data->dev_private;
1243         struct rte_eth_link link;
1244         uint32_t ret;
1245
1246         memset(&link, 0, sizeof(link));
1247
1248         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
1249         ret = VMXNET3_READ_BAR1_REG(hw, VMXNET3_REG_CMD);
1250
1251         if (ret & 0x1)
1252                 link.link_status = ETH_LINK_UP;
1253         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1254         link.link_speed = ETH_SPEED_NUM_10G;
1255         link.link_autoneg = ETH_LINK_FIXED;
1256
1257         return rte_eth_linkstatus_set(dev, &link);
1258 }
1259
1260 static int
1261 vmxnet3_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
1262 {
1263         /* Link status doesn't change for stopped dev */
1264         if (dev->data->dev_started == 0)
1265                 return -1;
1266
1267         return __vmxnet3_dev_link_update(dev, wait_to_complete);
1268 }
1269
1270 /* Updating rxmode through Vmxnet3_DriverShared structure in adapter */
1271 static void
1272 vmxnet3_dev_set_rxmode(struct vmxnet3_hw *hw, uint32_t feature, int set)
1273 {
1274         struct Vmxnet3_RxFilterConf *rxConf = &hw->shared->devRead.rxFilterConf;
1275
1276         if (set)
1277                 rxConf->rxMode = rxConf->rxMode | feature;
1278         else
1279                 rxConf->rxMode = rxConf->rxMode & (~feature);
1280
1281         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD, VMXNET3_CMD_UPDATE_RX_MODE);
1282 }
1283
1284 /* Promiscuous supported only if Vmxnet3_DriverShared is initialized in adapter */
1285 static int
1286 vmxnet3_dev_promiscuous_enable(struct rte_eth_dev *dev)
1287 {
1288         struct vmxnet3_hw *hw = dev->data->dev_private;
1289         uint32_t *vf_table = hw->shared->devRead.rxFilterConf.vfTable;
1290
1291         memset(vf_table, 0, VMXNET3_VFT_TABLE_SIZE);
1292         vmxnet3_dev_set_rxmode(hw, VMXNET3_RXM_PROMISC, 1);
1293
1294         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD,
1295                                VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1296
1297         return 0;
1298 }
1299
1300 /* Promiscuous supported only if Vmxnet3_DriverShared is initialized in adapter */
1301 static int
1302 vmxnet3_dev_promiscuous_disable(struct rte_eth_dev *dev)
1303 {
1304         struct vmxnet3_hw *hw = dev->data->dev_private;
1305         uint32_t *vf_table = hw->shared->devRead.rxFilterConf.vfTable;
1306         uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
1307
1308         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
1309                 memcpy(vf_table, hw->shadow_vfta, VMXNET3_VFT_TABLE_SIZE);
1310         else
1311                 memset(vf_table, 0xff, VMXNET3_VFT_TABLE_SIZE);
1312         vmxnet3_dev_set_rxmode(hw, VMXNET3_RXM_PROMISC, 0);
1313         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD,
1314                                VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1315
1316         return 0;
1317 }
1318
1319 /* Allmulticast supported only if Vmxnet3_DriverShared is initialized in adapter */
1320 static int
1321 vmxnet3_dev_allmulticast_enable(struct rte_eth_dev *dev)
1322 {
1323         struct vmxnet3_hw *hw = dev->data->dev_private;
1324
1325         vmxnet3_dev_set_rxmode(hw, VMXNET3_RXM_ALL_MULTI, 1);
1326
1327         return 0;
1328 }
1329
1330 /* Allmulticast supported only if Vmxnet3_DriverShared is initialized in adapter */
1331 static int
1332 vmxnet3_dev_allmulticast_disable(struct rte_eth_dev *dev)
1333 {
1334         struct vmxnet3_hw *hw = dev->data->dev_private;
1335
1336         vmxnet3_dev_set_rxmode(hw, VMXNET3_RXM_ALL_MULTI, 0);
1337
1338         return 0;
1339 }
1340
1341 /* Enable/disable filter on vlan */
1342 static int
1343 vmxnet3_dev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vid, int on)
1344 {
1345         struct vmxnet3_hw *hw = dev->data->dev_private;
1346         struct Vmxnet3_RxFilterConf *rxConf = &hw->shared->devRead.rxFilterConf;
1347         uint32_t *vf_table = rxConf->vfTable;
1348
1349         /* save state for restore */
1350         if (on)
1351                 VMXNET3_SET_VFTABLE_ENTRY(hw->shadow_vfta, vid);
1352         else
1353                 VMXNET3_CLEAR_VFTABLE_ENTRY(hw->shadow_vfta, vid);
1354
1355         /* don't change active filter if in promiscuous mode */
1356         if (rxConf->rxMode & VMXNET3_RXM_PROMISC)
1357                 return 0;
1358
1359         /* set in hardware */
1360         if (on)
1361                 VMXNET3_SET_VFTABLE_ENTRY(vf_table, vid);
1362         else
1363                 VMXNET3_CLEAR_VFTABLE_ENTRY(vf_table, vid);
1364
1365         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD,
1366                                VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1367         return 0;
1368 }
1369
1370 static int
1371 vmxnet3_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1372 {
1373         struct vmxnet3_hw *hw = dev->data->dev_private;
1374         Vmxnet3_DSDevRead *devRead = &hw->shared->devRead;
1375         uint32_t *vf_table = devRead->rxFilterConf.vfTable;
1376         uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
1377
1378         if (mask & ETH_VLAN_STRIP_MASK) {
1379                 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
1380                         devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
1381                 else
1382                         devRead->misc.uptFeatures &= ~UPT1_F_RXVLAN;
1383
1384                 VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD,
1385                                        VMXNET3_CMD_UPDATE_FEATURE);
1386         }
1387
1388         if (mask & ETH_VLAN_FILTER_MASK) {
1389                 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
1390                         memcpy(vf_table, hw->shadow_vfta, VMXNET3_VFT_TABLE_SIZE);
1391                 else
1392                         memset(vf_table, 0xff, VMXNET3_VFT_TABLE_SIZE);
1393
1394                 VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD,
1395                                        VMXNET3_CMD_UPDATE_VLAN_FILTERS);
1396         }
1397
1398         return 0;
1399 }
1400
1401 static void
1402 vmxnet3_process_events(struct rte_eth_dev *dev)
1403 {
1404         struct vmxnet3_hw *hw = dev->data->dev_private;
1405         uint32_t events = hw->shared->ecr;
1406
1407         if (!events)
1408                 return;
1409
1410         /*
1411          * ECR bits when written with 1b are cleared. Hence write
1412          * events back to ECR so that the bits which were set will be reset.
1413          */
1414         VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_ECR, events);
1415
1416         /* Check if link state has changed */
1417         if (events & VMXNET3_ECR_LINK) {
1418                 PMD_DRV_LOG(DEBUG, "Process events: VMXNET3_ECR_LINK event");
1419                 if (vmxnet3_dev_link_update(dev, 0) == 0)
1420                         rte_eth_dev_callback_process(dev,
1421                                                      RTE_ETH_EVENT_INTR_LSC,
1422                                                      NULL);
1423         }
1424
1425         /* Check if there is an error on xmit/recv queues */
1426         if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
1427                 VMXNET3_WRITE_BAR1_REG(hw, VMXNET3_REG_CMD,
1428                                        VMXNET3_CMD_GET_QUEUE_STATUS);
1429
1430                 if (hw->tqd_start->status.stopped)
1431                         PMD_DRV_LOG(ERR, "tq error 0x%x",
1432                                     hw->tqd_start->status.error);
1433
1434                 if (hw->rqd_start->status.stopped)
1435                         PMD_DRV_LOG(ERR, "rq error 0x%x",
1436                                      hw->rqd_start->status.error);
1437
1438                 /* Reset the device */
1439                 /* Have to reset the device */
1440         }
1441
1442         if (events & VMXNET3_ECR_DIC)
1443                 PMD_DRV_LOG(DEBUG, "Device implementation change event.");
1444
1445         if (events & VMXNET3_ECR_DEBUG)
1446                 PMD_DRV_LOG(DEBUG, "Debug event generated by device.");
1447 }
1448
1449 static void
1450 vmxnet3_interrupt_handler(void *param)
1451 {
1452         struct rte_eth_dev *dev = param;
1453         struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev->device);
1454
1455         vmxnet3_process_events(dev);
1456
1457         if (rte_intr_ack(&pci_dev->intr_handle) < 0)
1458                 PMD_DRV_LOG(ERR, "interrupt enable failed");
1459 }
1460
1461 RTE_PMD_REGISTER_PCI(net_vmxnet3, rte_vmxnet3_pmd);
1462 RTE_PMD_REGISTER_PCI_TABLE(net_vmxnet3, pci_id_vmxnet3_map);
1463 RTE_PMD_REGISTER_KMOD_DEP(net_vmxnet3, "* igb_uio | uio_pci_generic | vfio-pci");
1464 RTE_LOG_REGISTER(vmxnet3_logtype_init, pmd.net.vmxnet3.init, NOTICE);
1465 RTE_LOG_REGISTER(vmxnet3_logtype_driver, pmd.net.vmxnet3.driver, NOTICE);