2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vppinfra/types.h>
17 #include <vlib/vlib.h>
18 #include <vlib/pci/pci.h>
19 #include <vnet/ethernet/ethernet.h>
20 #include <vnet/plugin/plugin.h>
21 #include <vpp/app/version.h>
23 #include <vmxnet3/vmxnet3.h>
25 #define PCI_VENDOR_ID_VMWARE 0x15ad
26 #define PCI_DEVICE_ID_VMWARE_VMXNET3 0x07b0
28 vmxnet3_main_t vmxnet3_main;
30 static pci_device_id_t vmxnet3_pci_device_ids[] = {
32 .vendor_id = PCI_VENDOR_ID_VMWARE,
33 .device_id = PCI_DEVICE_ID_VMWARE_VMXNET3},
38 vmxnet3_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
41 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
42 vmxnet3_main_t *vmxm = &vmxnet3_main;
43 vmxnet3_device_t *vd = vec_elt_at_index (vmxm->devices, hi->dev_instance);
44 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
46 if (vd->flags & VMXNET3_DEVICE_F_ERROR)
47 return clib_error_return (0, "device is in error state");
51 vnet_hw_interface_set_flags (vnm, vd->hw_if_index,
52 VNET_HW_INTERFACE_FLAG_LINK_UP);
53 vd->flags |= VMXNET3_DEVICE_F_ADMIN_UP;
57 vnet_hw_interface_set_flags (vnm, vd->hw_if_index, 0);
58 vd->flags &= ~VMXNET3_DEVICE_F_ADMIN_UP;
64 vmxnet3_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
65 vnet_hw_interface_rx_mode mode)
67 vmxnet3_main_t *vmxm = &vmxnet3_main;
68 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
69 vmxnet3_device_t *vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
70 vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, qid);
72 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
81 vmxnet3_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
84 vmxnet3_main_t *vmxm = &vmxnet3_main;
85 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
86 vmxnet3_device_t *vd = pool_elt_at_index (vmxm->devices, hw->dev_instance);
88 /* Shut off redirection */
91 vd->per_interface_next_index = node_index;
95 vd->per_interface_next_index =
96 vlib_node_add_next (vlib_get_main (), vmxnet3_input_node.index,
100 static char *vmxnet3_tx_func_error_strings[] = {
102 foreach_vmxnet3_tx_func_error
107 VNET_DEVICE_CLASS (vmxnet3_device_class,) =
109 .name = "VMXNET3 interface",
110 .format_device = format_vmxnet3_device,
111 .format_device_name = format_vmxnet3_device_name,
112 .admin_up_down_function = vmxnet3_interface_admin_up_down,
113 .rx_mode_change_function = vmxnet3_interface_rx_mode_change,
114 .rx_redirect_to_node = vmxnet3_set_interface_next_node,
115 .tx_function_n_errors = VMXNET3_TX_N_ERROR,
116 .tx_function_error_strings = vmxnet3_tx_func_error_strings,
121 vmxnet3_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
127 vmxnet3_write_mac (vmxnet3_device_t * vd)
131 memcpy (&val, vd->mac_addr, 4);
132 vmxnet3_reg_write (vd, 1, VMXNET3_REG_MACL, val);
135 memcpy (&val, vd->mac_addr + 4, 2);
136 vmxnet3_reg_write (vd, 1, VMXNET3_REG_MACH, val);
139 static clib_error_t *
140 vmxnet3_provision_driver_shared (vlib_main_t * vm, vmxnet3_device_t * vd)
142 vmxnet3_shared *shared;
146 vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, qid);
147 vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, qid);
149 vd->dma = vlib_physmem_alloc_aligned (vm, sizeof (*vd->dma), 512);
151 return vlib_physmem_last_error (vm);
153 clib_memset (vd->dma, 0, sizeof (*vd->dma));
155 q = &vd->dma->queues;
156 q->tx.cfg.desc_address = vmxnet3_dma_addr (vm, vd, txq->tx_desc);
157 q->tx.cfg.comp_address = vmxnet3_dma_addr (vm, vd, txq->tx_comp);
158 q->tx.cfg.num_desc = txq->size;
159 q->tx.cfg.num_comp = txq->size;
160 for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
162 q->rx.cfg.desc_address[rid] = vmxnet3_dma_addr (vm, vd,
164 q->rx.cfg.num_desc[rid] = rxq->size;
166 q->rx.cfg.comp_address = vmxnet3_dma_addr (vm, vd, rxq->rx_comp);
167 q->rx.cfg.num_comp = rxq->size;
169 shared = &vd->dma->shared;
170 shared->magic = VMXNET3_SHARED_MAGIC;
171 shared->misc.version = VMXNET3_VERSION_MAGIC;
172 if (sizeof (void *) == 4)
173 shared->misc.guest_info = VMXNET3_GOS_BITS_32;
175 shared->misc.guest_info = VMXNET3_GOS_BITS_64;
176 shared->misc.guest_info |= VMXNET3_GOS_TYPE_LINUX;
177 shared->misc.version_support = VMXNET3_VERSION_SELECT;
178 shared->misc.upt_version_support = VMXNET3_UPT_VERSION_SELECT;
179 shared->misc.queue_desc_address = vmxnet3_dma_addr (vm, vd, q);
180 shared->misc.queue_desc_len = sizeof (*q);
181 shared->misc.mtu = VMXNET3_MTU;
182 shared->misc.num_tx_queues = vd->num_tx_queues;
183 shared->misc.num_rx_queues = vd->num_rx_queues;
184 shared->interrupt.num_intrs = vd->num_intrs;
185 shared->interrupt.event_intr_index = 1;
186 shared->interrupt.control = VMXNET3_IC_DISABLE_ALL;
187 shared->rx_filter.mode = VMXNET3_RXMODE_UCAST | VMXNET3_RXMODE_BCAST |
188 VMXNET3_RXMODE_ALL_MULTI | VMXNET3_RXMODE_PROMISC;
189 shared_dma = vmxnet3_dma_addr (vm, vd, shared);
191 vmxnet3_reg_write (vd, 1, VMXNET3_REG_DSAL, shared_dma);
192 vmxnet3_reg_write (vd, 1, VMXNET3_REG_DSAH, shared_dma >> 32);
198 vmxnet3_enable_interrupt (vmxnet3_device_t * vd)
201 vmxnet3_shared *shared = &vd->dma->shared;
203 shared->interrupt.control &= ~VMXNET3_IC_DISABLE_ALL;
204 for (i = 0; i < vd->num_intrs; i++)
205 vmxnet3_reg_write (vd, 0, VMXNET3_REG_IMR + i * 8, 0);
209 vmxnet3_disable_interrupt (vmxnet3_device_t * vd)
212 vmxnet3_shared *shared = &vd->dma->shared;
214 shared->interrupt.control |= VMXNET3_IC_DISABLE_ALL;
215 for (i = 0; i < vd->num_intrs; i++)
216 vmxnet3_reg_write (vd, 0, VMXNET3_REG_IMR + i * 8, 1);
219 static clib_error_t *
220 vmxnet3_rxq_init (vlib_main_t * vm, vmxnet3_device_t * vd, u16 qid, u16 qsz)
225 vec_validate_aligned (vd->rxqs, qid, CLIB_CACHE_LINE_BYTES);
226 rxq = vec_elt_at_index (vd->rxqs, qid);
227 clib_memset (rxq, 0, sizeof (*rxq));
229 for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
231 rxq->rx_desc[rid] = vlib_physmem_alloc_aligned
232 (vm, qsz * sizeof (*rxq->rx_desc[rid]), 512);
234 if (rxq->rx_desc[rid] == 0)
235 return vlib_physmem_last_error (vm);
237 clib_memset (rxq->rx_desc[rid], 0, qsz * sizeof (*rxq->rx_desc[rid]));
239 rxq->rx_comp = vlib_physmem_alloc_aligned (vm, qsz * sizeof (*rxq->rx_comp),
241 if (rxq->rx_comp == 0)
242 return vlib_physmem_last_error (vm);
244 clib_memset (rxq->rx_comp, 0, qsz * sizeof (*rxq->rx_comp));
245 for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
247 vmxnet3_rx_ring *ring;
249 ring = &rxq->rx_ring[rid];
250 ring->gen = VMXNET3_RXF_GEN;
252 vec_validate_aligned (ring->bufs, rxq->size, CLIB_CACHE_LINE_BYTES);
254 rxq->rx_comp_ring.gen = VMXNET3_RXCF_GEN;
259 static clib_error_t *
260 vmxnet3_txq_init (vlib_main_t * vm, vmxnet3_device_t * vd, u16 qid, u16 qsz)
264 if (qid >= vd->num_tx_queues)
266 qid = qid % vd->num_tx_queues;
267 txq = vec_elt_at_index (vd->txqs, qid);
269 clib_spinlock_init (&txq->lock);
270 vd->flags |= VMXNET3_DEVICE_F_SHARED_TXQ_LOCK;
274 vec_validate_aligned (vd->txqs, qid, CLIB_CACHE_LINE_BYTES);
275 txq = vec_elt_at_index (vd->txqs, qid);
276 clib_memset (txq, 0, sizeof (*txq));
278 txq->tx_desc = vlib_physmem_alloc_aligned (vm, qsz * sizeof (*txq->tx_desc),
280 if (txq->tx_desc == 0)
281 return vlib_physmem_last_error (vm);
283 memset (txq->tx_desc, 0, qsz * sizeof (*txq->tx_desc));
284 txq->tx_comp = vlib_physmem_alloc_aligned (vm, qsz * sizeof (*txq->tx_comp),
286 if (txq->tx_comp == 0)
287 return vlib_physmem_last_error (vm);
289 clib_memset (txq->tx_comp, 0, qsz * sizeof (*txq->tx_comp));
290 vec_validate_aligned (txq->tx_ring.bufs, txq->size, CLIB_CACHE_LINE_BYTES);
291 txq->tx_ring.gen = VMXNET3_TXF_GEN;
292 txq->tx_comp_ring.gen = VMXNET3_TXCF_GEN;
297 static clib_error_t *
298 vmxnet3_device_init (vlib_main_t * vm, vmxnet3_device_t * vd,
299 vmxnet3_create_if_args_t * args)
301 clib_error_t *error = 0;
303 vlib_thread_main_t *tm = vlib_get_thread_main ();
305 vd->num_tx_queues = 1;
306 vd->num_rx_queues = 1;
309 /* Quiesce the device */
310 vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_QUIESCE_DEV);
311 ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_CMD);
314 error = clib_error_return (0, "error on quisecing device rc (%u)", ret);
318 /* Reset the device */
319 vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
320 ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_CMD);
323 error = clib_error_return (0, "error on resetting device rc (%u)", ret);
327 ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_VRRS);
328 vd->version = count_leading_zeros (ret);
329 vd->version = uword_bits - vd->version;
331 if (vd->version == 0 || vd->version > 3)
333 error = clib_error_return (0, "unsupported hardware version %u",
338 vmxnet3_reg_write (vd, 1, VMXNET3_REG_VRRS, 1 << (vd->version - 1));
340 ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_UVRS);
342 vmxnet3_reg_write (vd, 1, VMXNET3_REG_UVRS, 1);
345 error = clib_error_return (0, "unsupported upt version %u", ret);
349 vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
350 ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_CMD);
353 vd->flags |= VMXNET3_DEVICE_F_LINK_UP;
354 vd->link_speed = ret >> 16;
358 vd->flags &= ~VMXNET3_DEVICE_F_LINK_UP;
361 /* Get the mac address */
362 ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_MACL);
363 clib_memcpy (vd->mac_addr, &ret, 4);
364 ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_MACH);
365 clib_memcpy (vd->mac_addr + 4, &ret, 2);
367 error = vmxnet3_rxq_init (vm, vd, 0, args->rxq_size);
371 for (i = 0; i < tm->n_vlib_mains; i++)
373 error = vmxnet3_txq_init (vm, vd, i, args->txq_size);
378 error = vmxnet3_provision_driver_shared (vm, vd);
382 vmxnet3_write_mac (vd);
384 /* Activate device */
385 vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_ACTIVATE_DEV);
386 ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_CMD);
390 clib_error_return (0, "error on activating device rc (%u)", ret);
394 /* Disable interrupts */
395 vmxnet3_disable_interrupt (vd);
397 vec_foreach_index (i, vd->rxqs)
399 vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, i);
401 vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
402 vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
404 vd->flags |= VMXNET3_DEVICE_F_INITIALIZED;
406 vmxnet3_enable_interrupt (vd);
412 vmxnet3_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
414 vnet_main_t *vnm = vnet_get_main ();
415 vmxnet3_main_t *vmxm = &vmxnet3_main;
416 uword pd = vlib_pci_get_private_data (vm, h);
417 vmxnet3_device_t *vd = pool_elt_at_index (vmxm->devices, pd);
420 if (vec_len (vd->rxqs) > qid && vd->rxqs[qid].int_mode != 0)
421 vnet_device_input_set_interrupt_pending (vnm, vd->hw_if_index, qid);
425 vmxnet3_irq_1_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
427 vnet_main_t *vnm = vnet_get_main ();
428 vmxnet3_main_t *vmxm = &vmxnet3_main;
429 uword pd = vlib_pci_get_private_data (vm, h);
430 vmxnet3_device_t *vd = pool_elt_at_index (vmxm->devices, pd);
433 vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
434 ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_CMD);
437 vd->flags |= VMXNET3_DEVICE_F_LINK_UP;
438 vd->link_speed = ret >> 16;
439 vnet_hw_interface_set_flags (vnm, vd->hw_if_index,
440 VNET_HW_INTERFACE_FLAG_LINK_UP);
444 vd->flags &= ~VMXNET3_DEVICE_F_LINK_UP;
445 vnet_hw_interface_set_flags (vnm, vd->hw_if_index, 0);
450 vmxnet3_queue_size_valid (u16 qsz)
452 if (qsz < 64 || qsz > 4096)
460 vmxnet3_create_if (vlib_main_t * vm, vmxnet3_create_if_args_t * args)
462 vnet_main_t *vnm = vnet_get_main ();
463 vmxnet3_main_t *vmxm = &vmxnet3_main;
464 vmxnet3_device_t *vd;
465 vlib_pci_dev_handle_t h;
466 clib_error_t *error = 0;
468 if (args->rxq_size == 0)
469 args->rxq_size = VMXNET3_NUM_RX_DESC;
470 if (args->txq_size == 0)
471 args->txq_size = VMXNET3_NUM_TX_DESC;
473 if (!vmxnet3_queue_size_valid (args->rxq_size) ||
474 !vmxnet3_queue_size_valid (args->txq_size))
476 args->rv = VNET_API_ERROR_INVALID_VALUE;
478 clib_error_return (error,
479 "queue size must be <= 4096, >= 64, "
480 "and multiples of 64");
485 pool_foreach (vd, vmxm->devices, ({
486 if (vd->pci_addr.as_u32 == args->addr.as_u32)
488 args->rv = VNET_API_ERROR_INVALID_VALUE;
490 clib_error_return (error, "PCI address in use");
496 pool_get (vmxm->devices, vd);
497 vd->dev_instance = vd - vmxm->devices;
498 vd->per_interface_next_index = ~0;
499 vd->pci_addr = args->addr;
501 if (args->enable_elog)
502 vd->flags |= VMXNET3_DEVICE_F_ELOG;
505 vlib_pci_device_open (vm, &args->addr, vmxnet3_pci_device_ids, &h)))
507 pool_put (vmxm->devices, vd);
508 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
510 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
514 vd->pci_dev_handle = h;
516 vlib_pci_set_private_data (vm, h, vd->dev_instance);
518 if ((error = vlib_pci_bus_master_enable (vm, h)))
521 if ((error = vlib_pci_map_region (vm, h, 0, (void **) &vd->bar[0])))
524 if ((error = vlib_pci_map_region (vm, h, 1, (void **) &vd->bar[1])))
527 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
528 &vmxnet3_irq_0_handler)))
531 if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
532 &vmxnet3_irq_1_handler)))
535 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
538 if ((error = vlib_pci_intr_enable (vm, h)))
541 if ((error = vmxnet3_device_init (vm, vd, args)))
544 /* create interface */
545 error = ethernet_register_interface (vnm, vmxnet3_device_class.index,
546 vd->dev_instance, vd->mac_addr,
547 &vd->hw_if_index, vmxnet3_flag_change);
552 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vd->hw_if_index);
553 vd->sw_if_index = sw->sw_if_index;
554 args->sw_if_index = sw->sw_if_index;
556 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vd->hw_if_index);
557 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
558 vnet_hw_interface_set_input_node (vnm, vd->hw_if_index,
559 vmxnet3_input_node.index);
560 vnet_hw_interface_assign_rx_thread (vnm, vd->hw_if_index, 0, ~0);
561 if (vd->flags & VMXNET3_DEVICE_F_LINK_UP)
562 vnet_hw_interface_set_flags (vnm, vd->hw_if_index,
563 VNET_HW_INTERFACE_FLAG_LINK_UP);
565 vnet_hw_interface_set_flags (vnm, vd->hw_if_index, 0);
569 vmxnet3_delete_if (vm, vd);
570 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
575 vmxnet3_delete_if (vlib_main_t * vm, vmxnet3_device_t * vd)
577 vnet_main_t *vnm = vnet_get_main ();
578 vmxnet3_main_t *vmxm = &vmxnet3_main;
582 /* Quiesce the device */
583 vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_QUIESCE_DEV);
585 /* Reset the device */
586 vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
590 vnet_hw_interface_set_flags (vnm, vd->hw_if_index, 0);
591 vnet_hw_interface_unassign_rx_thread (vnm, vd->hw_if_index, 0);
592 ethernet_delete_interface (vnm, vd->hw_if_index);
595 vlib_pci_device_close (vm, vd->pci_dev_handle);
598 vec_foreach_index (i, vd->rxqs)
600 vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, i);
601 u16 mask = rxq->size - 1;
604 for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
606 vmxnet3_rx_ring *ring;
608 ring = &rxq->rx_ring[rid];
609 desc_idx = (ring->consume + 1) & mask;
610 vlib_buffer_free_from_ring (vm, ring->bufs, desc_idx, rxq->size,
612 vec_free (ring->bufs);
613 vlib_physmem_free (vm, rxq->rx_desc[rid]);
615 vlib_physmem_free (vm, rxq->rx_comp);
621 vec_foreach_index (i, vd->txqs)
623 vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, i);
624 u16 mask = txq->size - 1;
627 desc_idx = txq->tx_ring.consume;
628 end_idx = txq->tx_ring.produce;
629 while (desc_idx != end_idx)
631 bi = txq->tx_ring.bufs[desc_idx];
632 vlib_buffer_free_no_next (vm, &bi, 1);
636 clib_spinlock_free (&txq->lock);
637 vec_free (txq->tx_ring.bufs);
638 vlib_physmem_free (vm, txq->tx_desc);
639 vlib_physmem_free (vm, txq->tx_comp);
644 vlib_physmem_free (vm, vd->dma);
646 clib_error_free (vd->error);
647 clib_memset (vd, 0, sizeof (*vd));
648 pool_put (vmxm->devices, vd);
652 * fd.io coding-style-patch-verification: ON
655 * eval: (c-set-style "gnu")