1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2020
11 #include <ethdev_pci.h>
13 #include "txgbe_logs.h"
14 #include "base/txgbe.h"
15 #include "txgbe_ethdev.h"
16 #include "txgbe_rxtx.h"
18 static int txgbevf_dev_info_get(struct rte_eth_dev *dev,
19 struct rte_eth_dev_info *dev_info);
20 static int txgbevf_dev_link_update(struct rte_eth_dev *dev,
21 int wait_to_complete);
22 static int txgbevf_dev_close(struct rte_eth_dev *dev);
23 static void txgbevf_intr_disable(struct rte_eth_dev *dev);
24 static void txgbevf_intr_enable(struct rte_eth_dev *dev);
25 static void txgbevf_configure_msix(struct rte_eth_dev *dev);
26 static void txgbevf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index);
27 static void txgbevf_dev_interrupt_handler(void *param);
30 * The set of PCI devices this driver supports (for VF)
32 static const struct rte_pci_id pci_id_txgbevf_map[] = {
33 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, TXGBE_DEV_ID_RAPTOR_VF) },
34 { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, TXGBE_DEV_ID_RAPTOR_VF_HV) },
35 { .vendor_id = 0, /* sentinel */ },
38 static const struct rte_eth_desc_lim rx_desc_lim = {
39 .nb_max = TXGBE_RING_DESC_MAX,
40 .nb_min = TXGBE_RING_DESC_MIN,
41 .nb_align = TXGBE_RXD_ALIGN,
44 static const struct rte_eth_desc_lim tx_desc_lim = {
45 .nb_max = TXGBE_RING_DESC_MAX,
46 .nb_min = TXGBE_RING_DESC_MIN,
47 .nb_align = TXGBE_TXD_ALIGN,
48 .nb_seg_max = TXGBE_TX_MAX_SEG,
49 .nb_mtu_seg_max = TXGBE_TX_MAX_SEG,
52 static const struct eth_dev_ops txgbevf_eth_dev_ops;
55 * Negotiate mailbox API version with the PF.
56 * After reset API version is always set to the basic one (txgbe_mbox_api_10).
57 * Then we try to negotiate starting with the most recent one.
58 * If all negotiation attempts fail, then we will proceed with
59 * the default one (txgbe_mbox_api_10).
62 txgbevf_negotiate_api(struct txgbe_hw *hw)
66 /* start with highest supported, proceed down */
67 static const int sup_ver[] = {
74 for (i = 0; i < ARRAY_SIZE(sup_ver); i++) {
75 if (txgbevf_negotiate_api_version(hw, sup_ver[i]) == 0)
81 generate_random_mac_addr(struct rte_ether_addr *mac_addr)
85 /* Set Organizationally Unique Identifier (OUI) prefix. */
86 mac_addr->addr_bytes[0] = 0x00;
87 mac_addr->addr_bytes[1] = 0x09;
88 mac_addr->addr_bytes[2] = 0xC0;
89 /* Force indication of locally assigned MAC address. */
90 mac_addr->addr_bytes[0] |= RTE_ETHER_LOCAL_ADMIN_ADDR;
91 /* Generate the last 3 bytes of the MAC address with a random number. */
93 memcpy(&mac_addr->addr_bytes[3], &random, 3);
97 * Virtual Function device init
100 eth_txgbevf_dev_init(struct rte_eth_dev *eth_dev)
104 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
105 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
106 struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
107 struct rte_ether_addr *perm_addr =
108 (struct rte_ether_addr *)hw->mac.perm_addr;
110 PMD_INIT_FUNC_TRACE();
112 eth_dev->dev_ops = &txgbevf_eth_dev_ops;
114 /* for secondary processes, we don't initialise any further as primary
115 * has already done this work. Only check we don't need a different
118 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
119 struct txgbe_tx_queue *txq;
120 uint16_t nb_tx_queues = eth_dev->data->nb_tx_queues;
121 /* TX queue function in primary, set by last queue initialized
122 * Tx queue may not initialized by primary process
124 if (eth_dev->data->tx_queues) {
125 txq = eth_dev->data->tx_queues[nb_tx_queues - 1];
126 txgbe_set_tx_function(eth_dev, txq);
128 /* Use default TX function if we get here */
130 "No TX queues configured yet. Using default TX function.");
133 txgbe_set_rx_function(eth_dev);
138 rte_eth_copy_pci_info(eth_dev, pci_dev);
140 hw->device_id = pci_dev->id.device_id;
141 hw->vendor_id = pci_dev->id.vendor_id;
142 hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
143 hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
144 hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
146 /* Initialize the shared code (base driver) */
147 err = txgbe_init_shared_code(hw);
150 "Shared code init failed for txgbevf: %d", err);
154 /* init_mailbox_params */
155 hw->mbx.init_params(hw);
157 /* Disable the interrupts for VF */
158 txgbevf_intr_disable(eth_dev);
160 hw->mac.num_rar_entries = 128; /* The MAX of the underlying PF */
161 err = hw->mac.reset_hw(hw);
164 * The VF reset operation returns the TXGBE_ERR_INVALID_MAC_ADDR when
165 * the underlying PF driver has not assigned a MAC address to the VF.
166 * In this case, assign a random MAC address.
168 if (err != 0 && err != TXGBE_ERR_INVALID_MAC_ADDR) {
169 PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", err);
171 * This error code will be propagated to the app by
172 * rte_eth_dev_reset, so use a public error code rather than
173 * the internal-only TXGBE_ERR_RESET_FAILED
178 /* negotiate mailbox API version to use with the PF. */
179 txgbevf_negotiate_api(hw);
181 /* Get Rx/Tx queue count via mailbox, which is ready after reset_hw */
182 txgbevf_get_queues(hw, &tcs, &tc);
184 /* Allocate memory for storing MAC addresses */
185 eth_dev->data->mac_addrs = rte_zmalloc("txgbevf", RTE_ETHER_ADDR_LEN *
186 hw->mac.num_rar_entries, 0);
187 if (eth_dev->data->mac_addrs == NULL) {
189 "Failed to allocate %u bytes needed to store "
191 RTE_ETHER_ADDR_LEN * hw->mac.num_rar_entries);
195 /* Generate a random MAC address, if none was assigned by PF. */
196 if (rte_is_zero_ether_addr(perm_addr)) {
197 generate_random_mac_addr(perm_addr);
198 err = txgbe_set_rar_vf(hw, 1, perm_addr->addr_bytes, 0, 1);
200 rte_free(eth_dev->data->mac_addrs);
201 eth_dev->data->mac_addrs = NULL;
204 PMD_INIT_LOG(INFO, "\tVF MAC address not assigned by Host PF");
205 PMD_INIT_LOG(INFO, "\tAssign randomly generated MAC address "
206 "%02x:%02x:%02x:%02x:%02x:%02x",
207 perm_addr->addr_bytes[0],
208 perm_addr->addr_bytes[1],
209 perm_addr->addr_bytes[2],
210 perm_addr->addr_bytes[3],
211 perm_addr->addr_bytes[4],
212 perm_addr->addr_bytes[5]);
215 /* Copy the permanent MAC address */
216 rte_ether_addr_copy(perm_addr, ð_dev->data->mac_addrs[0]);
218 /* reset the hardware with the new settings */
219 err = hw->mac.start_hw(hw);
221 PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", err);
225 rte_intr_callback_register(intr_handle,
226 txgbevf_dev_interrupt_handler, eth_dev);
227 rte_intr_enable(intr_handle);
228 txgbevf_intr_enable(eth_dev);
230 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x mac.type=%s",
231 eth_dev->data->port_id, pci_dev->id.vendor_id,
232 pci_dev->id.device_id, "txgbe_mac_raptor_vf");
237 /* Virtual Function device uninit */
239 eth_txgbevf_dev_uninit(struct rte_eth_dev *eth_dev)
241 PMD_INIT_FUNC_TRACE();
243 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
246 txgbevf_dev_close(eth_dev);
251 static int eth_txgbevf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
252 struct rte_pci_device *pci_dev)
254 return rte_eth_dev_pci_generic_probe(pci_dev,
255 sizeof(struct txgbe_adapter), eth_txgbevf_dev_init);
258 static int eth_txgbevf_pci_remove(struct rte_pci_device *pci_dev)
260 return rte_eth_dev_pci_generic_remove(pci_dev, eth_txgbevf_dev_uninit);
264 * virtual function driver struct
266 static struct rte_pci_driver rte_txgbevf_pmd = {
267 .id_table = pci_id_txgbevf_map,
268 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
269 .probe = eth_txgbevf_pci_probe,
270 .remove = eth_txgbevf_pci_remove,
274 txgbevf_dev_info_get(struct rte_eth_dev *dev,
275 struct rte_eth_dev_info *dev_info)
277 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
278 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
280 dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
281 dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
282 dev_info->min_rx_bufsize = 1024;
283 dev_info->max_rx_pktlen = TXGBE_FRAME_SIZE_MAX;
284 dev_info->max_mac_addrs = hw->mac.num_rar_entries;
285 dev_info->max_hash_mac_addrs = TXGBE_VMDQ_NUM_UC_MAC;
286 dev_info->max_vfs = pci_dev->max_vfs;
287 dev_info->max_vmdq_pools = ETH_64_POOLS;
288 dev_info->rx_queue_offload_capa = txgbe_get_rx_queue_offloads(dev);
289 dev_info->rx_offload_capa = (txgbe_get_rx_port_offloads(dev) |
290 dev_info->rx_queue_offload_capa);
291 dev_info->tx_queue_offload_capa = txgbe_get_tx_queue_offloads(dev);
292 dev_info->tx_offload_capa = txgbe_get_tx_port_offloads(dev);
293 dev_info->hash_key_size = TXGBE_HKEY_MAX_INDEX * sizeof(uint32_t);
294 dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
295 dev_info->flow_type_rss_offloads = TXGBE_RSS_OFFLOAD_ALL;
297 dev_info->default_rxconf = (struct rte_eth_rxconf) {
299 .pthresh = TXGBE_DEFAULT_RX_PTHRESH,
300 .hthresh = TXGBE_DEFAULT_RX_HTHRESH,
301 .wthresh = TXGBE_DEFAULT_RX_WTHRESH,
303 .rx_free_thresh = TXGBE_DEFAULT_RX_FREE_THRESH,
308 dev_info->default_txconf = (struct rte_eth_txconf) {
310 .pthresh = TXGBE_DEFAULT_TX_PTHRESH,
311 .hthresh = TXGBE_DEFAULT_TX_HTHRESH,
312 .wthresh = TXGBE_DEFAULT_TX_WTHRESH,
314 .tx_free_thresh = TXGBE_DEFAULT_TX_FREE_THRESH,
318 dev_info->rx_desc_lim = rx_desc_lim;
319 dev_info->tx_desc_lim = tx_desc_lim;
325 txgbevf_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
327 return txgbe_dev_link_update_share(dev, wait_to_complete);
331 * Virtual Function operations
334 txgbevf_intr_disable(struct rte_eth_dev *dev)
336 struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
337 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
339 PMD_INIT_FUNC_TRACE();
341 /* Clear interrupt mask to stop from interrupts being generated */
342 wr32(hw, TXGBE_VFIMS, TXGBE_VFIMS_MASK);
346 /* Clear mask value. */
347 intr->mask_misc = TXGBE_VFIMS_MASK;
351 txgbevf_intr_enable(struct rte_eth_dev *dev)
353 struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
354 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
356 PMD_INIT_FUNC_TRACE();
358 /* VF enable interrupt autoclean */
359 wr32(hw, TXGBE_VFIMC, TXGBE_VFIMC_MASK);
367 txgbevf_dev_close(struct rte_eth_dev *dev)
369 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
370 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
371 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
372 PMD_INIT_FUNC_TRACE();
373 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
376 hw->mac.reset_hw(hw);
378 txgbe_dev_free_queues(dev);
381 * Remove the VF MAC address ro ensure
382 * that the VF traffic goes to the PF
383 * after stop, close and detach of the VF
385 txgbevf_remove_mac_addr(dev, 0);
387 /* Disable the interrupts for VF */
388 txgbevf_intr_disable(dev);
390 rte_free(dev->data->mac_addrs);
391 dev->data->mac_addrs = NULL;
393 rte_intr_disable(intr_handle);
394 rte_intr_callback_unregister(intr_handle,
395 txgbevf_dev_interrupt_handler, dev);
401 txgbevf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
403 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
404 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
405 struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
406 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
407 uint32_t vec = TXGBE_MISC_VEC_ID;
409 if (rte_intr_allow_others(intr_handle))
410 vec = TXGBE_RX_VEC_START;
411 intr->mask_misc &= ~(1 << vec);
412 RTE_SET_USED(queue_id);
413 wr32(hw, TXGBE_VFIMC, ~intr->mask_misc);
415 rte_intr_enable(intr_handle);
421 txgbevf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
423 struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
424 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
425 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
426 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
427 uint32_t vec = TXGBE_MISC_VEC_ID;
429 if (rte_intr_allow_others(intr_handle))
430 vec = TXGBE_RX_VEC_START;
431 intr->mask_misc |= (1 << vec);
432 RTE_SET_USED(queue_id);
433 wr32(hw, TXGBE_VFIMS, intr->mask_misc);
439 txgbevf_set_ivar_map(struct txgbe_hw *hw, int8_t direction,
440 uint8_t queue, uint8_t msix_vector)
444 if (direction == -1) {
446 msix_vector |= TXGBE_VFIVAR_VLD;
447 tmp = rd32(hw, TXGBE_VFIVARMISC);
450 wr32(hw, TXGBE_VFIVARMISC, tmp);
453 /* Workround for ICR lost */
454 idx = ((16 * (queue & 1)) + (8 * direction));
455 tmp = rd32(hw, TXGBE_VFIVAR(queue >> 1));
456 tmp &= ~(0xFF << idx);
457 tmp |= (msix_vector << idx);
458 wr32(hw, TXGBE_VFIVAR(queue >> 1), tmp);
463 txgbevf_configure_msix(struct rte_eth_dev *dev)
465 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
466 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
467 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
469 uint32_t vector_idx = TXGBE_MISC_VEC_ID;
470 uint32_t base = TXGBE_MISC_VEC_ID;
472 /* Configure VF other cause ivar */
473 txgbevf_set_ivar_map(hw, -1, 1, vector_idx);
475 /* won't configure msix register if no mapping is done
476 * between intr vector and event fd.
478 if (!rte_intr_dp_is_en(intr_handle))
481 if (rte_intr_allow_others(intr_handle)) {
482 base = TXGBE_RX_VEC_START;
483 vector_idx = TXGBE_RX_VEC_START;
486 /* Configure all RX queues of VF */
487 for (q_idx = 0; q_idx < dev->data->nb_rx_queues; q_idx++) {
488 /* Force all queue use vector 0,
489 * as TXGBE_VF_MAXMSIVECOTR = 1
491 txgbevf_set_ivar_map(hw, 0, q_idx, vector_idx);
492 intr_handle->intr_vec[q_idx] = vector_idx;
493 if (vector_idx < base + intr_handle->nb_efd - 1)
497 /* As RX queue setting above show, all queues use the vector 0.
498 * Set only the ITR value of TXGBE_MISC_VEC_ID.
500 wr32(hw, TXGBE_ITR(TXGBE_MISC_VEC_ID),
501 TXGBE_ITR_IVAL(TXGBE_QUEUE_ITR_INTERVAL_DEFAULT)
506 txgbevf_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
507 __rte_unused uint32_t index,
508 __rte_unused uint32_t pool)
510 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
514 * On a VF, adding again the same MAC addr is not an idempotent
515 * operation. Trap this case to avoid exhausting the [very limited]
516 * set of PF resources used to store VF MAC addresses.
518 if (memcmp(hw->mac.perm_addr, mac_addr,
519 sizeof(struct rte_ether_addr)) == 0)
521 err = txgbevf_set_uc_addr_vf(hw, 2, mac_addr->addr_bytes);
523 PMD_DRV_LOG(ERR, "Unable to add MAC address "
524 "%02x:%02x:%02x:%02x:%02x:%02x - err=%d",
525 mac_addr->addr_bytes[0],
526 mac_addr->addr_bytes[1],
527 mac_addr->addr_bytes[2],
528 mac_addr->addr_bytes[3],
529 mac_addr->addr_bytes[4],
530 mac_addr->addr_bytes[5],
536 txgbevf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index)
538 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
539 struct rte_ether_addr *perm_addr =
540 (struct rte_ether_addr *)hw->mac.perm_addr;
541 struct rte_ether_addr *mac_addr;
546 * The TXGBE_VF_SET_MACVLAN command of the txgbe-pf driver does
547 * not support the deletion of a given MAC address.
548 * Instead, it imposes to delete all MAC addresses, then to add again
549 * all MAC addresses with the exception of the one to be deleted.
551 (void)txgbevf_set_uc_addr_vf(hw, 0, NULL);
554 * Add again all MAC addresses, with the exception of the deleted one
555 * and of the permanent MAC address.
557 for (i = 0, mac_addr = dev->data->mac_addrs;
558 i < hw->mac.num_rar_entries; i++, mac_addr++) {
559 /* Skip the deleted MAC address */
562 /* Skip NULL MAC addresses */
563 if (rte_is_zero_ether_addr(mac_addr))
565 /* Skip the permanent MAC address */
566 if (memcmp(perm_addr, mac_addr,
567 sizeof(struct rte_ether_addr)) == 0)
569 err = txgbevf_set_uc_addr_vf(hw, 2, mac_addr->addr_bytes);
572 "Adding again MAC address "
573 "%02x:%02x:%02x:%02x:%02x:%02x failed "
575 mac_addr->addr_bytes[0],
576 mac_addr->addr_bytes[1],
577 mac_addr->addr_bytes[2],
578 mac_addr->addr_bytes[3],
579 mac_addr->addr_bytes[4],
580 mac_addr->addr_bytes[5],
586 txgbevf_set_default_mac_addr(struct rte_eth_dev *dev,
587 struct rte_ether_addr *addr)
589 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
591 hw->mac.set_rar(hw, 0, (void *)addr, 0, 0);
596 static void txgbevf_mbx_process(struct rte_eth_dev *dev)
598 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
601 /* peek the message first */
602 in_msg = rd32(hw, TXGBE_VFMBX);
604 /* PF reset VF event */
605 if (in_msg == TXGBE_PF_CONTROL_MSG) {
606 /* dummy mbx read to ack pf */
607 if (txgbe_read_mbx(hw, &in_msg, 1, 0))
609 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
615 txgbevf_dev_interrupt_get_status(struct rte_eth_dev *dev)
618 struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
619 struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
620 txgbevf_intr_disable(dev);
622 /* read-on-clear nic registers here */
623 eicr = rd32(hw, TXGBE_VFICR);
626 /* only one misc vector supported - mailbox */
627 eicr &= TXGBE_VFICR_MASK;
628 /* Workround for ICR lost */
629 intr->flags |= TXGBE_FLAG_MAILBOX;
635 txgbevf_dev_interrupt_action(struct rte_eth_dev *dev)
637 struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
639 if (intr->flags & TXGBE_FLAG_MAILBOX) {
640 txgbevf_mbx_process(dev);
641 intr->flags &= ~TXGBE_FLAG_MAILBOX;
644 txgbevf_intr_enable(dev);
650 txgbevf_dev_interrupt_handler(void *param)
652 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
654 txgbevf_dev_interrupt_get_status(dev);
655 txgbevf_dev_interrupt_action(dev);
659 * dev_ops for virtual function, bare necessities for basic vf
660 * operation have been implemented
662 static const struct eth_dev_ops txgbevf_eth_dev_ops = {
663 .link_update = txgbevf_dev_link_update,
664 .dev_infos_get = txgbevf_dev_info_get,
665 .rx_queue_intr_enable = txgbevf_dev_rx_queue_intr_enable,
666 .rx_queue_intr_disable = txgbevf_dev_rx_queue_intr_disable,
667 .mac_addr_add = txgbevf_add_mac_addr,
668 .mac_addr_remove = txgbevf_remove_mac_addr,
669 .rxq_info_get = txgbe_rxq_info_get,
670 .txq_info_get = txgbe_txq_info_get,
671 .mac_addr_set = txgbevf_set_default_mac_addr,
674 RTE_PMD_REGISTER_PCI(net_txgbe_vf, rte_txgbevf_pmd);
675 RTE_PMD_REGISTER_PCI_TABLE(net_txgbe_vf, pci_id_txgbevf_map);
676 RTE_PMD_REGISTER_KMOD_DEP(net_txgbe_vf, "* igb_uio | vfio-pci");