b1573100e0188c11d2552498d8273e192fddaff9
[dpdk.git] / drivers / net / txgbe / txgbe_ethdev_vf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
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 <rte_log.h>
11 #include <ethdev_pci.h>
12
13 #include "txgbe_logs.h"
14 #include "base/txgbe.h"
15 #include "txgbe_ethdev.h"
16 #include "txgbe_rxtx.h"
17
18 static int txgbevf_dev_xstats_get(struct rte_eth_dev *dev,
19                                   struct rte_eth_xstat *xstats, unsigned int n);
20 static int txgbevf_dev_info_get(struct rte_eth_dev *dev,
21                                  struct rte_eth_dev_info *dev_info);
22 static int  txgbevf_dev_configure(struct rte_eth_dev *dev);
23 static int txgbevf_dev_link_update(struct rte_eth_dev *dev,
24                                    int wait_to_complete);
25 static int txgbevf_dev_close(struct rte_eth_dev *dev);
26 static void txgbevf_intr_disable(struct rte_eth_dev *dev);
27 static void txgbevf_intr_enable(struct rte_eth_dev *dev);
28 static int txgbevf_dev_stats_reset(struct rte_eth_dev *dev);
29 static int txgbevf_vlan_offload_config(struct rte_eth_dev *dev, int mask);
30 static void txgbevf_set_vfta_all(struct rte_eth_dev *dev, bool on);
31 static void txgbevf_configure_msix(struct rte_eth_dev *dev);
32 static void txgbevf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index);
33 static void txgbevf_dev_interrupt_handler(void *param);
34
35 /*
36  * The set of PCI devices this driver supports (for VF)
37  */
38 static const struct rte_pci_id pci_id_txgbevf_map[] = {
39         { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, TXGBE_DEV_ID_RAPTOR_VF) },
40         { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, TXGBE_DEV_ID_RAPTOR_VF_HV) },
41         { .vendor_id = 0, /* sentinel */ },
42 };
43
44 static const struct rte_eth_desc_lim rx_desc_lim = {
45         .nb_max = TXGBE_RING_DESC_MAX,
46         .nb_min = TXGBE_RING_DESC_MIN,
47         .nb_align = TXGBE_RXD_ALIGN,
48 };
49
50 static const struct rte_eth_desc_lim tx_desc_lim = {
51         .nb_max = TXGBE_RING_DESC_MAX,
52         .nb_min = TXGBE_RING_DESC_MIN,
53         .nb_align = TXGBE_TXD_ALIGN,
54         .nb_seg_max = TXGBE_TX_MAX_SEG,
55         .nb_mtu_seg_max = TXGBE_TX_MAX_SEG,
56 };
57
58 static const struct eth_dev_ops txgbevf_eth_dev_ops;
59
60 static const struct rte_txgbe_xstats_name_off rte_txgbevf_stats_strings[] = {
61         {"rx_multicast_packets_0",
62                         offsetof(struct txgbevf_hw_stats, qp[0].vfmprc)},
63         {"rx_multicast_packets_1",
64                         offsetof(struct txgbevf_hw_stats, qp[1].vfmprc)},
65         {"rx_multicast_packets_2",
66                         offsetof(struct txgbevf_hw_stats, qp[2].vfmprc)},
67         {"rx_multicast_packets_3",
68                         offsetof(struct txgbevf_hw_stats, qp[3].vfmprc)},
69         {"rx_multicast_packets_4",
70                         offsetof(struct txgbevf_hw_stats, qp[4].vfmprc)},
71         {"rx_multicast_packets_5",
72                         offsetof(struct txgbevf_hw_stats, qp[5].vfmprc)},
73         {"rx_multicast_packets_6",
74                         offsetof(struct txgbevf_hw_stats, qp[6].vfmprc)},
75         {"rx_multicast_packets_7",
76                         offsetof(struct txgbevf_hw_stats, qp[7].vfmprc)}
77 };
78
79 #define TXGBEVF_NB_XSTATS (sizeof(rte_txgbevf_stats_strings) /  \
80                 sizeof(rte_txgbevf_stats_strings[0]))
81
82 /*
83  * Negotiate mailbox API version with the PF.
84  * After reset API version is always set to the basic one (txgbe_mbox_api_10).
85  * Then we try to negotiate starting with the most recent one.
86  * If all negotiation attempts fail, then we will proceed with
87  * the default one (txgbe_mbox_api_10).
88  */
89 static void
90 txgbevf_negotiate_api(struct txgbe_hw *hw)
91 {
92         int32_t i;
93
94         /* start with highest supported, proceed down */
95         static const int sup_ver[] = {
96                 txgbe_mbox_api_13,
97                 txgbe_mbox_api_12,
98                 txgbe_mbox_api_11,
99                 txgbe_mbox_api_10,
100         };
101
102         for (i = 0; i < ARRAY_SIZE(sup_ver); i++) {
103                 if (txgbevf_negotiate_api_version(hw, sup_ver[i]) == 0)
104                         break;
105         }
106 }
107
108 static void
109 generate_random_mac_addr(struct rte_ether_addr *mac_addr)
110 {
111         uint64_t random;
112
113         /* Set Organizationally Unique Identifier (OUI) prefix. */
114         mac_addr->addr_bytes[0] = 0x00;
115         mac_addr->addr_bytes[1] = 0x09;
116         mac_addr->addr_bytes[2] = 0xC0;
117         /* Force indication of locally assigned MAC address. */
118         mac_addr->addr_bytes[0] |= RTE_ETHER_LOCAL_ADMIN_ADDR;
119         /* Generate the last 3 bytes of the MAC address with a random number. */
120         random = rte_rand();
121         memcpy(&mac_addr->addr_bytes[3], &random, 3);
122 }
123
124 /*
125  * Virtual Function device init
126  */
127 static int
128 eth_txgbevf_dev_init(struct rte_eth_dev *eth_dev)
129 {
130         int err;
131         uint32_t tc, tcs;
132         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
133         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
134         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
135         struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(eth_dev);
136         struct txgbe_hwstrip *hwstrip = TXGBE_DEV_HWSTRIP(eth_dev);
137         struct rte_ether_addr *perm_addr =
138                         (struct rte_ether_addr *)hw->mac.perm_addr;
139
140         PMD_INIT_FUNC_TRACE();
141
142         eth_dev->dev_ops = &txgbevf_eth_dev_ops;
143         eth_dev->rx_descriptor_status = txgbe_dev_rx_descriptor_status;
144         eth_dev->tx_descriptor_status = txgbe_dev_tx_descriptor_status;
145         eth_dev->rx_pkt_burst = &txgbe_recv_pkts;
146         eth_dev->tx_pkt_burst = &txgbe_xmit_pkts;
147
148         /* for secondary processes, we don't initialise any further as primary
149          * has already done this work. Only check we don't need a different
150          * RX function
151          */
152         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
153                 struct txgbe_tx_queue *txq;
154                 uint16_t nb_tx_queues = eth_dev->data->nb_tx_queues;
155                 /* TX queue function in primary, set by last queue initialized
156                  * Tx queue may not initialized by primary process
157                  */
158                 if (eth_dev->data->tx_queues) {
159                         txq = eth_dev->data->tx_queues[nb_tx_queues - 1];
160                         txgbe_set_tx_function(eth_dev, txq);
161                 } else {
162                         /* Use default TX function if we get here */
163                         PMD_INIT_LOG(NOTICE,
164                                      "No TX queues configured yet. Using default TX function.");
165                 }
166
167                 txgbe_set_rx_function(eth_dev);
168
169                 return 0;
170         }
171
172         rte_eth_copy_pci_info(eth_dev, pci_dev);
173
174         hw->device_id = pci_dev->id.device_id;
175         hw->vendor_id = pci_dev->id.vendor_id;
176         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
177         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
178         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
179
180         /* initialize the vfta */
181         memset(shadow_vfta, 0, sizeof(*shadow_vfta));
182
183         /* initialize the hw strip bitmap*/
184         memset(hwstrip, 0, sizeof(*hwstrip));
185
186         /* Initialize the shared code (base driver) */
187         err = txgbe_init_shared_code(hw);
188         if (err != 0) {
189                 PMD_INIT_LOG(ERR,
190                         "Shared code init failed for txgbevf: %d", err);
191                 return -EIO;
192         }
193
194         /* init_mailbox_params */
195         hw->mbx.init_params(hw);
196
197         /* Reset the hw statistics */
198         txgbevf_dev_stats_reset(eth_dev);
199
200         /* Disable the interrupts for VF */
201         txgbevf_intr_disable(eth_dev);
202
203         hw->mac.num_rar_entries = 128; /* The MAX of the underlying PF */
204         err = hw->mac.reset_hw(hw);
205
206         /*
207          * The VF reset operation returns the TXGBE_ERR_INVALID_MAC_ADDR when
208          * the underlying PF driver has not assigned a MAC address to the VF.
209          * In this case, assign a random MAC address.
210          */
211         if (err != 0 && err != TXGBE_ERR_INVALID_MAC_ADDR) {
212                 PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", err);
213                 /*
214                  * This error code will be propagated to the app by
215                  * rte_eth_dev_reset, so use a public error code rather than
216                  * the internal-only TXGBE_ERR_RESET_FAILED
217                  */
218                 return -EAGAIN;
219         }
220
221         /* negotiate mailbox API version to use with the PF. */
222         txgbevf_negotiate_api(hw);
223
224         /* Get Rx/Tx queue count via mailbox, which is ready after reset_hw */
225         txgbevf_get_queues(hw, &tcs, &tc);
226
227         /* Allocate memory for storing MAC addresses */
228         eth_dev->data->mac_addrs = rte_zmalloc("txgbevf", RTE_ETHER_ADDR_LEN *
229                                                hw->mac.num_rar_entries, 0);
230         if (eth_dev->data->mac_addrs == NULL) {
231                 PMD_INIT_LOG(ERR,
232                              "Failed to allocate %u bytes needed to store "
233                              "MAC addresses",
234                              RTE_ETHER_ADDR_LEN * hw->mac.num_rar_entries);
235                 return -ENOMEM;
236         }
237
238         /* Generate a random MAC address, if none was assigned by PF. */
239         if (rte_is_zero_ether_addr(perm_addr)) {
240                 generate_random_mac_addr(perm_addr);
241                 err = txgbe_set_rar_vf(hw, 1, perm_addr->addr_bytes, 0, 1);
242                 if (err) {
243                         rte_free(eth_dev->data->mac_addrs);
244                         eth_dev->data->mac_addrs = NULL;
245                         return err;
246                 }
247                 PMD_INIT_LOG(INFO, "\tVF MAC address not assigned by Host PF");
248                 PMD_INIT_LOG(INFO, "\tAssign randomly generated MAC address "
249                              "%02x:%02x:%02x:%02x:%02x:%02x",
250                              perm_addr->addr_bytes[0],
251                              perm_addr->addr_bytes[1],
252                              perm_addr->addr_bytes[2],
253                              perm_addr->addr_bytes[3],
254                              perm_addr->addr_bytes[4],
255                              perm_addr->addr_bytes[5]);
256         }
257
258         /* Copy the permanent MAC address */
259         rte_ether_addr_copy(perm_addr, &eth_dev->data->mac_addrs[0]);
260
261         /* reset the hardware with the new settings */
262         err = hw->mac.start_hw(hw);
263         if (err) {
264                 PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", err);
265                 return -EIO;
266         }
267
268         rte_intr_callback_register(intr_handle,
269                                    txgbevf_dev_interrupt_handler, eth_dev);
270         rte_intr_enable(intr_handle);
271         txgbevf_intr_enable(eth_dev);
272
273         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x mac.type=%s",
274                      eth_dev->data->port_id, pci_dev->id.vendor_id,
275                      pci_dev->id.device_id, "txgbe_mac_raptor_vf");
276
277         return 0;
278 }
279
280 /* Virtual Function device uninit */
281 static int
282 eth_txgbevf_dev_uninit(struct rte_eth_dev *eth_dev)
283 {
284         PMD_INIT_FUNC_TRACE();
285
286         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
287                 return 0;
288
289         txgbevf_dev_close(eth_dev);
290
291         return 0;
292 }
293
294 static int eth_txgbevf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
295         struct rte_pci_device *pci_dev)
296 {
297         return rte_eth_dev_pci_generic_probe(pci_dev,
298                 sizeof(struct txgbe_adapter), eth_txgbevf_dev_init);
299 }
300
301 static int eth_txgbevf_pci_remove(struct rte_pci_device *pci_dev)
302 {
303         return rte_eth_dev_pci_generic_remove(pci_dev, eth_txgbevf_dev_uninit);
304 }
305
306 /*
307  * virtual function driver struct
308  */
309 static struct rte_pci_driver rte_txgbevf_pmd = {
310         .id_table = pci_id_txgbevf_map,
311         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
312         .probe = eth_txgbevf_pci_probe,
313         .remove = eth_txgbevf_pci_remove,
314 };
315
316 static int txgbevf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
317         struct rte_eth_xstat_name *xstats_names, unsigned int limit)
318 {
319         unsigned int i;
320
321         if (limit < TXGBEVF_NB_XSTATS && xstats_names != NULL)
322                 return -ENOMEM;
323
324         if (xstats_names != NULL)
325                 for (i = 0; i < TXGBEVF_NB_XSTATS; i++)
326                         snprintf(xstats_names[i].name,
327                                 sizeof(xstats_names[i].name),
328                                 "%s", rte_txgbevf_stats_strings[i].name);
329         return TXGBEVF_NB_XSTATS;
330 }
331
332 static void
333 txgbevf_update_stats(struct rte_eth_dev *dev)
334 {
335         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
336         struct txgbevf_hw_stats *hw_stats = (struct txgbevf_hw_stats *)
337                           TXGBE_DEV_STATS(dev);
338         unsigned int i;
339
340         for (i = 0; i < dev->data->nb_rx_queues; i++) {
341                 /* Good Rx packet, include VF loopback */
342                 TXGBE_UPDCNT32(TXGBE_QPRXPKT(i),
343                 hw_stats->qp[i].last_vfgprc, hw_stats->qp[i].vfgprc);
344
345                 /* Good Rx octets, include VF loopback */
346                 TXGBE_UPDCNT36(TXGBE_QPRXOCTL(i),
347                 hw_stats->qp[i].last_vfgorc, hw_stats->qp[i].vfgorc);
348
349                 /* Rx Multicst Packet */
350                 TXGBE_UPDCNT32(TXGBE_QPRXMPKT(i),
351                 hw_stats->qp[i].last_vfmprc, hw_stats->qp[i].vfmprc);
352         }
353         hw->rx_loaded = 0;
354
355         for (i = 0; i < dev->data->nb_tx_queues; i++) {
356                 /* Good Tx packet, include VF loopback */
357                 TXGBE_UPDCNT32(TXGBE_QPTXPKT(i),
358                 hw_stats->qp[i].last_vfgptc, hw_stats->qp[i].vfgptc);
359
360                 /* Good Tx octets, include VF loopback */
361                 TXGBE_UPDCNT36(TXGBE_QPTXOCTL(i),
362                 hw_stats->qp[i].last_vfgotc, hw_stats->qp[i].vfgotc);
363         }
364         hw->offset_loaded = 0;
365 }
366
367 static int
368 txgbevf_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
369                        unsigned int n)
370 {
371         struct txgbevf_hw_stats *hw_stats = (struct txgbevf_hw_stats *)
372                         TXGBE_DEV_STATS(dev);
373         unsigned int i;
374
375         if (n < TXGBEVF_NB_XSTATS)
376                 return TXGBEVF_NB_XSTATS;
377
378         txgbevf_update_stats(dev);
379
380         if (!xstats)
381                 return 0;
382
383         /* Extended stats */
384         for (i = 0; i < TXGBEVF_NB_XSTATS; i++) {
385                 xstats[i].id = i;
386                 xstats[i].value = *(uint64_t *)(((char *)hw_stats) +
387                         rte_txgbevf_stats_strings[i].offset);
388         }
389
390         return TXGBEVF_NB_XSTATS;
391 }
392
393 static int
394 txgbevf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
395 {
396         struct txgbevf_hw_stats *hw_stats = (struct txgbevf_hw_stats *)
397                           TXGBE_DEV_STATS(dev);
398         uint32_t i;
399
400         txgbevf_update_stats(dev);
401
402         if (stats == NULL)
403                 return -EINVAL;
404
405         stats->ipackets = 0;
406         stats->ibytes = 0;
407         stats->opackets = 0;
408         stats->obytes = 0;
409
410         for (i = 0; i < 8; i++) {
411                 stats->ipackets += hw_stats->qp[i].vfgprc;
412                 stats->ibytes += hw_stats->qp[i].vfgorc;
413                 stats->opackets += hw_stats->qp[i].vfgptc;
414                 stats->obytes += hw_stats->qp[i].vfgotc;
415         }
416
417         return 0;
418 }
419
420 static int
421 txgbevf_dev_stats_reset(struct rte_eth_dev *dev)
422 {
423         struct txgbevf_hw_stats *hw_stats = (struct txgbevf_hw_stats *)
424                         TXGBE_DEV_STATS(dev);
425         uint32_t i;
426
427         /* Sync HW register to the last stats */
428         txgbevf_dev_stats_get(dev, NULL);
429
430         /* reset HW current stats*/
431         for (i = 0; i < 8; i++) {
432                 hw_stats->qp[i].vfgprc = 0;
433                 hw_stats->qp[i].vfgorc = 0;
434                 hw_stats->qp[i].vfgptc = 0;
435                 hw_stats->qp[i].vfgotc = 0;
436         }
437
438         return 0;
439 }
440
441 static int
442 txgbevf_dev_info_get(struct rte_eth_dev *dev,
443                      struct rte_eth_dev_info *dev_info)
444 {
445         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
446         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
447
448         dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
449         dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
450         dev_info->min_rx_bufsize = 1024;
451         dev_info->max_rx_pktlen = TXGBE_FRAME_SIZE_MAX;
452         dev_info->max_mac_addrs = hw->mac.num_rar_entries;
453         dev_info->max_hash_mac_addrs = TXGBE_VMDQ_NUM_UC_MAC;
454         dev_info->max_vfs = pci_dev->max_vfs;
455         dev_info->max_vmdq_pools = ETH_64_POOLS;
456         dev_info->rx_queue_offload_capa = txgbe_get_rx_queue_offloads(dev);
457         dev_info->rx_offload_capa = (txgbe_get_rx_port_offloads(dev) |
458                                      dev_info->rx_queue_offload_capa);
459         dev_info->tx_queue_offload_capa = txgbe_get_tx_queue_offloads(dev);
460         dev_info->tx_offload_capa = txgbe_get_tx_port_offloads(dev);
461         dev_info->hash_key_size = TXGBE_HKEY_MAX_INDEX * sizeof(uint32_t);
462         dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
463         dev_info->flow_type_rss_offloads = TXGBE_RSS_OFFLOAD_ALL;
464
465         dev_info->default_rxconf = (struct rte_eth_rxconf) {
466                 .rx_thresh = {
467                         .pthresh = TXGBE_DEFAULT_RX_PTHRESH,
468                         .hthresh = TXGBE_DEFAULT_RX_HTHRESH,
469                         .wthresh = TXGBE_DEFAULT_RX_WTHRESH,
470                 },
471                 .rx_free_thresh = TXGBE_DEFAULT_RX_FREE_THRESH,
472                 .rx_drop_en = 0,
473                 .offloads = 0,
474         };
475
476         dev_info->default_txconf = (struct rte_eth_txconf) {
477                 .tx_thresh = {
478                         .pthresh = TXGBE_DEFAULT_TX_PTHRESH,
479                         .hthresh = TXGBE_DEFAULT_TX_HTHRESH,
480                         .wthresh = TXGBE_DEFAULT_TX_WTHRESH,
481                 },
482                 .tx_free_thresh = TXGBE_DEFAULT_TX_FREE_THRESH,
483                 .offloads = 0,
484         };
485
486         dev_info->rx_desc_lim = rx_desc_lim;
487         dev_info->tx_desc_lim = tx_desc_lim;
488
489         return 0;
490 }
491
492 static int
493 txgbevf_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
494 {
495         return txgbe_dev_link_update_share(dev, wait_to_complete);
496 }
497
498 /*
499  * Virtual Function operations
500  */
501 static void
502 txgbevf_intr_disable(struct rte_eth_dev *dev)
503 {
504         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
505         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
506
507         PMD_INIT_FUNC_TRACE();
508
509         /* Clear interrupt mask to stop from interrupts being generated */
510         wr32(hw, TXGBE_VFIMS, TXGBE_VFIMS_MASK);
511
512         txgbe_flush(hw);
513
514         /* Clear mask value. */
515         intr->mask_misc = TXGBE_VFIMS_MASK;
516 }
517
518 static void
519 txgbevf_intr_enable(struct rte_eth_dev *dev)
520 {
521         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
522         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
523
524         PMD_INIT_FUNC_TRACE();
525
526         /* VF enable interrupt autoclean */
527         wr32(hw, TXGBE_VFIMC, TXGBE_VFIMC_MASK);
528
529         txgbe_flush(hw);
530
531         intr->mask_misc = 0;
532 }
533
534 static int
535 txgbevf_dev_configure(struct rte_eth_dev *dev)
536 {
537         struct rte_eth_conf *conf = &dev->data->dev_conf;
538         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
539
540         PMD_INIT_LOG(DEBUG, "Configured Virtual Function port id: %d",
541                      dev->data->port_id);
542
543         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
544                 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
545
546         /*
547          * VF has no ability to enable/disable HW CRC
548          * Keep the persistent behavior the same as Host PF
549          */
550 #ifndef RTE_LIBRTE_TXGBE_PF_DISABLE_STRIP_CRC
551         if (conf->rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC) {
552                 PMD_INIT_LOG(NOTICE, "VF can't disable HW CRC Strip");
553                 conf->rxmode.offloads &= ~DEV_RX_OFFLOAD_KEEP_CRC;
554         }
555 #else
556         if (!(conf->rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)) {
557                 PMD_INIT_LOG(NOTICE, "VF can't enable HW CRC Strip");
558                 conf->rxmode.offloads |= DEV_RX_OFFLOAD_KEEP_CRC;
559         }
560 #endif
561
562         /*
563          * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
564          * allocation or vector Rx preconditions we will reset it.
565          */
566         adapter->rx_bulk_alloc_allowed = true;
567
568         return 0;
569 }
570
571 static int
572 txgbevf_dev_close(struct rte_eth_dev *dev)
573 {
574         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
575         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
576         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
577         PMD_INIT_FUNC_TRACE();
578         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
579                 return 0;
580
581         hw->mac.reset_hw(hw);
582
583         txgbe_dev_free_queues(dev);
584
585         /**
586          * Remove the VF MAC address ro ensure
587          * that the VF traffic goes to the PF
588          * after stop, close and detach of the VF
589          **/
590         txgbevf_remove_mac_addr(dev, 0);
591
592         dev->rx_pkt_burst = NULL;
593         dev->tx_pkt_burst = NULL;
594
595         /* Disable the interrupts for VF */
596         txgbevf_intr_disable(dev);
597
598         rte_free(dev->data->mac_addrs);
599         dev->data->mac_addrs = NULL;
600
601         rte_intr_disable(intr_handle);
602         rte_intr_callback_unregister(intr_handle,
603                                      txgbevf_dev_interrupt_handler, dev);
604
605         return 0;
606 }
607
608 static void txgbevf_set_vfta_all(struct rte_eth_dev *dev, bool on)
609 {
610         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
611         struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(dev);
612         int i = 0, j = 0, vfta = 0, mask = 1;
613
614         for (i = 0; i < TXGBE_VFTA_SIZE; i++) {
615                 vfta = shadow_vfta->vfta[i];
616                 if (vfta) {
617                         mask = 1;
618                         for (j = 0; j < 32; j++) {
619                                 if (vfta & mask)
620                                         txgbe_set_vfta(hw, (i << 5) + j, 0,
621                                                        on, false);
622                                 mask <<= 1;
623                         }
624                 }
625         }
626 }
627
628 static int
629 txgbevf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
630 {
631         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
632         struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(dev);
633         uint32_t vid_idx = 0;
634         uint32_t vid_bit = 0;
635         int ret = 0;
636
637         PMD_INIT_FUNC_TRACE();
638
639         /* vind is not used in VF driver, set to 0, check txgbe_set_vfta_vf */
640         ret = hw->mac.set_vfta(hw, vlan_id, 0, !!on, false);
641         if (ret) {
642                 PMD_INIT_LOG(ERR, "Unable to set VF vlan");
643                 return ret;
644         }
645         vid_idx = (uint32_t)((vlan_id >> 5) & 0x7F);
646         vid_bit = (uint32_t)(1 << (vlan_id & 0x1F));
647
648         /* Save what we set and restore it after device reset */
649         if (on)
650                 shadow_vfta->vfta[vid_idx] |= vid_bit;
651         else
652                 shadow_vfta->vfta[vid_idx] &= ~vid_bit;
653
654         return 0;
655 }
656
657 static void
658 txgbevf_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
659 {
660         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
661         uint32_t ctrl;
662
663         PMD_INIT_FUNC_TRACE();
664
665         if (queue >= hw->mac.max_rx_queues)
666                 return;
667
668         ctrl = rd32(hw, TXGBE_RXCFG(queue));
669         txgbe_dev_save_rx_queue(hw, queue);
670         if (on)
671                 ctrl |= TXGBE_RXCFG_VLAN;
672         else
673                 ctrl &= ~TXGBE_RXCFG_VLAN;
674         wr32(hw, TXGBE_RXCFG(queue), 0);
675         msec_delay(100);
676         txgbe_dev_store_rx_queue(hw, queue);
677         wr32m(hw, TXGBE_RXCFG(queue),
678                 TXGBE_RXCFG_VLAN | TXGBE_RXCFG_ENA, ctrl);
679
680         txgbe_vlan_hw_strip_bitmap_set(dev, queue, on);
681 }
682
683 static int
684 txgbevf_vlan_offload_config(struct rte_eth_dev *dev, int mask)
685 {
686         struct txgbe_rx_queue *rxq;
687         uint16_t i;
688         int on = 0;
689
690         /* VF function only support hw strip feature, others are not support */
691         if (mask & ETH_VLAN_STRIP_MASK) {
692                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
693                         rxq = dev->data->rx_queues[i];
694                         on = !!(rxq->offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
695                         txgbevf_vlan_strip_queue_set(dev, i, on);
696                 }
697         }
698
699         return 0;
700 }
701
702 static int
703 txgbevf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
704 {
705         txgbe_config_vlan_strip_on_all_queues(dev, mask);
706
707         txgbevf_vlan_offload_config(dev, mask);
708
709         return 0;
710 }
711
712 static int
713 txgbevf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
714 {
715         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
716         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
717         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
718         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
719         uint32_t vec = TXGBE_MISC_VEC_ID;
720
721         if (rte_intr_allow_others(intr_handle))
722                 vec = TXGBE_RX_VEC_START;
723         intr->mask_misc &= ~(1 << vec);
724         RTE_SET_USED(queue_id);
725         wr32(hw, TXGBE_VFIMC, ~intr->mask_misc);
726
727         rte_intr_enable(intr_handle);
728
729         return 0;
730 }
731
732 static int
733 txgbevf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
734 {
735         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
736         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
737         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
738         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
739         uint32_t vec = TXGBE_MISC_VEC_ID;
740
741         if (rte_intr_allow_others(intr_handle))
742                 vec = TXGBE_RX_VEC_START;
743         intr->mask_misc |= (1 << vec);
744         RTE_SET_USED(queue_id);
745         wr32(hw, TXGBE_VFIMS, intr->mask_misc);
746
747         return 0;
748 }
749
750 static void
751 txgbevf_set_ivar_map(struct txgbe_hw *hw, int8_t direction,
752                      uint8_t queue, uint8_t msix_vector)
753 {
754         uint32_t tmp, idx;
755
756         if (direction == -1) {
757                 /* other causes */
758                 msix_vector |= TXGBE_VFIVAR_VLD;
759                 tmp = rd32(hw, TXGBE_VFIVARMISC);
760                 tmp &= ~0xFF;
761                 tmp |= msix_vector;
762                 wr32(hw, TXGBE_VFIVARMISC, tmp);
763         } else {
764                 /* rx or tx cause */
765                 /* Workround for ICR lost */
766                 idx = ((16 * (queue & 1)) + (8 * direction));
767                 tmp = rd32(hw, TXGBE_VFIVAR(queue >> 1));
768                 tmp &= ~(0xFF << idx);
769                 tmp |= (msix_vector << idx);
770                 wr32(hw, TXGBE_VFIVAR(queue >> 1), tmp);
771         }
772 }
773
774 static void
775 txgbevf_configure_msix(struct rte_eth_dev *dev)
776 {
777         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
778         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
779         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
780         uint32_t q_idx;
781         uint32_t vector_idx = TXGBE_MISC_VEC_ID;
782         uint32_t base = TXGBE_MISC_VEC_ID;
783
784         /* Configure VF other cause ivar */
785         txgbevf_set_ivar_map(hw, -1, 1, vector_idx);
786
787         /* won't configure msix register if no mapping is done
788          * between intr vector and event fd.
789          */
790         if (!rte_intr_dp_is_en(intr_handle))
791                 return;
792
793         if (rte_intr_allow_others(intr_handle)) {
794                 base = TXGBE_RX_VEC_START;
795                 vector_idx = TXGBE_RX_VEC_START;
796         }
797
798         /* Configure all RX queues of VF */
799         for (q_idx = 0; q_idx < dev->data->nb_rx_queues; q_idx++) {
800                 /* Force all queue use vector 0,
801                  * as TXGBE_VF_MAXMSIVECOTR = 1
802                  */
803                 txgbevf_set_ivar_map(hw, 0, q_idx, vector_idx);
804                 intr_handle->intr_vec[q_idx] = vector_idx;
805                 if (vector_idx < base + intr_handle->nb_efd - 1)
806                         vector_idx++;
807         }
808
809         /* As RX queue setting above show, all queues use the vector 0.
810          * Set only the ITR value of TXGBE_MISC_VEC_ID.
811          */
812         wr32(hw, TXGBE_ITR(TXGBE_MISC_VEC_ID),
813                 TXGBE_ITR_IVAL(TXGBE_QUEUE_ITR_INTERVAL_DEFAULT)
814                 | TXGBE_ITR_WRDSA);
815 }
816
817 static int
818 txgbevf_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
819                      __rte_unused uint32_t index,
820                      __rte_unused uint32_t pool)
821 {
822         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
823         int err;
824
825         /*
826          * On a VF, adding again the same MAC addr is not an idempotent
827          * operation. Trap this case to avoid exhausting the [very limited]
828          * set of PF resources used to store VF MAC addresses.
829          */
830         if (memcmp(hw->mac.perm_addr, mac_addr,
831                         sizeof(struct rte_ether_addr)) == 0)
832                 return -1;
833         err = txgbevf_set_uc_addr_vf(hw, 2, mac_addr->addr_bytes);
834         if (err != 0)
835                 PMD_DRV_LOG(ERR, "Unable to add MAC address "
836                             "%02x:%02x:%02x:%02x:%02x:%02x - err=%d",
837                             mac_addr->addr_bytes[0],
838                             mac_addr->addr_bytes[1],
839                             mac_addr->addr_bytes[2],
840                             mac_addr->addr_bytes[3],
841                             mac_addr->addr_bytes[4],
842                             mac_addr->addr_bytes[5],
843                             err);
844         return err;
845 }
846
847 static void
848 txgbevf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index)
849 {
850         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
851         struct rte_ether_addr *perm_addr =
852                         (struct rte_ether_addr *)hw->mac.perm_addr;
853         struct rte_ether_addr *mac_addr;
854         uint32_t i;
855         int err;
856
857         /*
858          * The TXGBE_VF_SET_MACVLAN command of the txgbe-pf driver does
859          * not support the deletion of a given MAC address.
860          * Instead, it imposes to delete all MAC addresses, then to add again
861          * all MAC addresses with the exception of the one to be deleted.
862          */
863         (void)txgbevf_set_uc_addr_vf(hw, 0, NULL);
864
865         /*
866          * Add again all MAC addresses, with the exception of the deleted one
867          * and of the permanent MAC address.
868          */
869         for (i = 0, mac_addr = dev->data->mac_addrs;
870              i < hw->mac.num_rar_entries; i++, mac_addr++) {
871                 /* Skip the deleted MAC address */
872                 if (i == index)
873                         continue;
874                 /* Skip NULL MAC addresses */
875                 if (rte_is_zero_ether_addr(mac_addr))
876                         continue;
877                 /* Skip the permanent MAC address */
878                 if (memcmp(perm_addr, mac_addr,
879                                 sizeof(struct rte_ether_addr)) == 0)
880                         continue;
881                 err = txgbevf_set_uc_addr_vf(hw, 2, mac_addr->addr_bytes);
882                 if (err != 0)
883                         PMD_DRV_LOG(ERR,
884                                     "Adding again MAC address "
885                                     "%02x:%02x:%02x:%02x:%02x:%02x failed "
886                                     "err=%d",
887                                     mac_addr->addr_bytes[0],
888                                     mac_addr->addr_bytes[1],
889                                     mac_addr->addr_bytes[2],
890                                     mac_addr->addr_bytes[3],
891                                     mac_addr->addr_bytes[4],
892                                     mac_addr->addr_bytes[5],
893                                     err);
894         }
895 }
896
897 static int
898 txgbevf_set_default_mac_addr(struct rte_eth_dev *dev,
899                 struct rte_ether_addr *addr)
900 {
901         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
902
903         hw->mac.set_rar(hw, 0, (void *)addr, 0, 0);
904
905         return 0;
906 }
907
908 static void txgbevf_mbx_process(struct rte_eth_dev *dev)
909 {
910         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
911         u32 in_msg = 0;
912
913         /* peek the message first */
914         in_msg = rd32(hw, TXGBE_VFMBX);
915
916         /* PF reset VF event */
917         if (in_msg == TXGBE_PF_CONTROL_MSG) {
918                 /* dummy mbx read to ack pf */
919                 if (txgbe_read_mbx(hw, &in_msg, 1, 0))
920                         return;
921                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
922                                               NULL);
923         }
924 }
925
926 static int
927 txgbevf_dev_interrupt_get_status(struct rte_eth_dev *dev)
928 {
929         uint32_t eicr;
930         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
931         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
932         txgbevf_intr_disable(dev);
933
934         /* read-on-clear nic registers here */
935         eicr = rd32(hw, TXGBE_VFICR);
936         intr->flags = 0;
937
938         /* only one misc vector supported - mailbox */
939         eicr &= TXGBE_VFICR_MASK;
940         /* Workround for ICR lost */
941         intr->flags |= TXGBE_FLAG_MAILBOX;
942
943         return 0;
944 }
945
946 static int
947 txgbevf_dev_interrupt_action(struct rte_eth_dev *dev)
948 {
949         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
950
951         if (intr->flags & TXGBE_FLAG_MAILBOX) {
952                 txgbevf_mbx_process(dev);
953                 intr->flags &= ~TXGBE_FLAG_MAILBOX;
954         }
955
956         txgbevf_intr_enable(dev);
957
958         return 0;
959 }
960
961 static void
962 txgbevf_dev_interrupt_handler(void *param)
963 {
964         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
965
966         txgbevf_dev_interrupt_get_status(dev);
967         txgbevf_dev_interrupt_action(dev);
968 }
969
970 /*
971  * dev_ops for virtual function, bare necessities for basic vf
972  * operation have been implemented
973  */
974 static const struct eth_dev_ops txgbevf_eth_dev_ops = {
975         .dev_configure        = txgbevf_dev_configure,
976         .link_update          = txgbevf_dev_link_update,
977         .stats_get            = txgbevf_dev_stats_get,
978         .xstats_get           = txgbevf_dev_xstats_get,
979         .stats_reset          = txgbevf_dev_stats_reset,
980         .xstats_reset         = txgbevf_dev_stats_reset,
981         .xstats_get_names     = txgbevf_dev_xstats_get_names,
982         .dev_infos_get        = txgbevf_dev_info_get,
983         .vlan_filter_set      = txgbevf_vlan_filter_set,
984         .vlan_strip_queue_set = txgbevf_vlan_strip_queue_set,
985         .vlan_offload_set     = txgbevf_vlan_offload_set,
986         .rx_queue_intr_enable = txgbevf_dev_rx_queue_intr_enable,
987         .rx_queue_intr_disable = txgbevf_dev_rx_queue_intr_disable,
988         .mac_addr_add         = txgbevf_add_mac_addr,
989         .mac_addr_remove      = txgbevf_remove_mac_addr,
990         .rxq_info_get         = txgbe_rxq_info_get,
991         .txq_info_get         = txgbe_txq_info_get,
992         .mac_addr_set         = txgbevf_set_default_mac_addr,
993 };
994
995 RTE_PMD_REGISTER_PCI(net_txgbe_vf, rte_txgbevf_pmd);
996 RTE_PMD_REGISTER_PCI_TABLE(net_txgbe_vf, pci_id_txgbevf_map);
997 RTE_PMD_REGISTER_KMOD_DEP(net_txgbe_vf, "* igb_uio | vfio-pci");