vmxnet3: add support for "non-default" next node
[vpp.git] / src / plugins / vmxnet3 / vmxnet3.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
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>
22
23 #include <vmxnet3/vmxnet3.h>
24
25 #define PCI_VENDOR_ID_VMWARE                            0x15ad
26 #define PCI_DEVICE_ID_VMWARE_VMXNET3                    0x07b0
27
28 vmxnet3_main_t vmxnet3_main;
29
30 static pci_device_id_t vmxnet3_pci_device_ids[] = {
31   {
32    .vendor_id = PCI_VENDOR_ID_VMWARE,
33    .device_id = PCI_DEVICE_ID_VMWARE_VMXNET3},
34   {0},
35 };
36
37 static clib_error_t *
38 vmxnet3_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
39                                  u32 flags)
40 {
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;
45
46   if (vd->flags & VMXNET3_DEVICE_F_ERROR)
47     return clib_error_return (0, "device is in error state");
48
49   if (is_up)
50     {
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;
54     }
55   else
56     {
57       vnet_hw_interface_set_flags (vnm, vd->hw_if_index, 0);
58       vd->flags &= ~VMXNET3_DEVICE_F_ADMIN_UP;
59     }
60   return 0;
61 }
62
63 static clib_error_t *
64 vmxnet3_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
65                                   vnet_hw_interface_rx_mode mode)
66 {
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);
71
72   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
73     rxq->int_mode = 0;
74   else
75     rxq->int_mode = 1;
76
77   return 0;
78 }
79
80 static void
81 vmxnet3_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
82                                  u32 node_index)
83 {
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);
87
88   /* Shut off redirection */
89   if (node_index == ~0)
90     {
91       vd->per_interface_next_index = node_index;
92       return;
93     }
94
95   vd->per_interface_next_index =
96     vlib_node_add_next (vlib_get_main (), vmxnet3_input_node.index,
97                         node_index);
98 }
99
100 static char *vmxnet3_tx_func_error_strings[] = {
101 #define _(n,s) s,
102   foreach_vmxnet3_tx_func_error
103 #undef _
104 };
105
106 /* *INDENT-OFF* */
107 VNET_DEVICE_CLASS (vmxnet3_device_class,) =
108 {
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,
117 };
118 /* *INDENT-ON* */
119
120 static u32
121 vmxnet3_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
122 {
123   return 0;
124 }
125
126 static void
127 vmxnet3_write_mac (vmxnet3_device_t * vd)
128 {
129   u32 val;
130
131   memcpy (&val, vd->mac_addr, 4);
132   vmxnet3_reg_write (vd, 1, VMXNET3_REG_MACL, val);
133
134   val = 0;
135   memcpy (&val, vd->mac_addr + 4, 2);
136   vmxnet3_reg_write (vd, 1, VMXNET3_REG_MACH, val);
137 }
138
139 static clib_error_t *
140 vmxnet3_provision_driver_shared (vlib_main_t * vm, vmxnet3_device_t * vd)
141 {
142   vmxnet3_main_t *vmxm = &vmxnet3_main;
143   vmxnet3_shared *shared;
144   vmxnet3_queues *q;
145   u64 shared_dma;
146   clib_error_t *error;
147   u16 qid = 0, rid;
148   vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, qid);
149   vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, qid);
150
151   vd->dma = vlib_physmem_alloc_aligned (vm, vmxm->physmem_region, &error,
152                                         sizeof (*vd->dma), 512);
153   if (error)
154     return error;
155
156   memset (vd->dma, 0, sizeof (*vd->dma));
157
158   q = &vd->dma->queues;
159   q->tx.cfg.desc_address = vmxnet3_dma_addr (vm, vd, txq->tx_desc);
160   q->tx.cfg.comp_address = vmxnet3_dma_addr (vm, vd, txq->tx_comp);
161   q->tx.cfg.num_desc = txq->size;
162   q->tx.cfg.num_comp = txq->size;
163   for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
164     {
165       q->rx.cfg.desc_address[rid] = vmxnet3_dma_addr (vm, vd,
166                                                       rxq->rx_desc[rid]);
167       q->rx.cfg.num_desc[rid] = rxq->size;
168     }
169   q->rx.cfg.comp_address = vmxnet3_dma_addr (vm, vd, rxq->rx_comp);
170   q->rx.cfg.num_comp = rxq->size;
171
172   shared = &vd->dma->shared;
173   shared->magic = VMXNET3_SHARED_MAGIC;
174   shared->misc.version = VMXNET3_VERSION_MAGIC;
175   if (sizeof (void *) == 4)
176     shared->misc.guest_info = VMXNET3_GOS_BITS_32;
177   else
178     shared->misc.guest_info = VMXNET3_GOS_BITS_64;
179   shared->misc.guest_info |= VMXNET3_GOS_TYPE_LINUX;
180   shared->misc.version_support = VMXNET3_VERSION_SELECT;
181   shared->misc.upt_version_support = VMXNET3_UPT_VERSION_SELECT;
182   shared->misc.queue_desc_address = vmxnet3_dma_addr (vm, vd, q);
183   shared->misc.queue_desc_len = sizeof (*q);
184   shared->misc.mtu = VMXNET3_MTU;
185   shared->misc.num_tx_queues = vd->num_tx_queues;
186   shared->misc.num_rx_queues = vd->num_rx_queues;
187   shared->interrupt.num_intrs = vd->num_intrs;
188   shared->interrupt.control = VMXNET3_IC_DISABLE_ALL;
189   shared->rx_filter.mode = VMXNET3_RXMODE_UCAST | VMXNET3_RXMODE_BCAST |
190     VMXNET3_RXMODE_ALL_MULTI;
191   shared_dma = vmxnet3_dma_addr (vm, vd, shared);
192
193   vmxnet3_reg_write (vd, 1, VMXNET3_REG_DSAL, shared_dma);
194   vmxnet3_reg_write (vd, 1, VMXNET3_REG_DSAH, shared_dma >> 32);
195
196   return 0;
197 }
198
199 static inline void
200 vmxnet3_enable_interrupt (vmxnet3_device_t * vd)
201 {
202   int i;
203   vmxnet3_shared *shared = &vd->dma->shared;
204
205   shared->interrupt.control &= ~VMXNET3_IC_DISABLE_ALL;
206   for (i = 0; i < vd->num_intrs; i++)
207     vmxnet3_reg_write (vd, 0, VMXNET3_REG_IMR + i * 8, 0);
208 }
209
210 static inline void
211 vmxnet3_disable_interrupt (vmxnet3_device_t * vd)
212 {
213   int i;
214   vmxnet3_shared *shared = &vd->dma->shared;
215
216   shared->interrupt.control |= VMXNET3_IC_DISABLE_ALL;
217   for (i = 0; i < vd->num_intrs; i++)
218     vmxnet3_reg_write (vd, 0, VMXNET3_REG_IMR + i * 8, 1);
219 }
220
221 static clib_error_t *
222 vmxnet3_rxq_init (vlib_main_t * vm, vmxnet3_device_t * vd, u16 qid, u16 qsz)
223 {
224   vmxnet3_main_t *vmxm = &vmxnet3_main;
225   vmxnet3_rxq_t *rxq;
226   clib_error_t *error;
227   u16 rid;
228
229   vec_validate_aligned (vd->rxqs, qid, CLIB_CACHE_LINE_BYTES);
230   rxq = vec_elt_at_index (vd->rxqs, qid);
231   memset (rxq, 0, sizeof (*rxq));
232   rxq->size = qsz;
233   for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
234     {
235       rxq->rx_desc[rid] =
236         vlib_physmem_alloc_aligned (vm, vmxm->physmem_region,
237                                     &error, qsz * sizeof (*rxq->rx_desc[rid]),
238                                     512);
239       if (error)
240         return error;
241       memset (rxq->rx_desc[rid], 0, qsz * sizeof (*rxq->rx_desc[rid]));
242     }
243   rxq->rx_comp = vlib_physmem_alloc_aligned (vm, vmxm->physmem_region, &error,
244                                              qsz * sizeof (*rxq->rx_comp),
245                                              512);
246   if (error)
247     return error;
248   memset (rxq->rx_comp, 0, qsz * sizeof (*rxq->rx_comp));
249   for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
250     {
251       vmxnet3_rx_ring *ring;
252
253       ring = &rxq->rx_ring[rid];
254       ring->gen = VMXNET3_RXF_GEN;
255       vec_validate_aligned (ring->bufs, rxq->size, CLIB_CACHE_LINE_BYTES);
256     }
257   rxq->rx_comp_ring.gen = VMXNET3_RXCF_GEN;
258
259   return 0;
260 }
261
262 static clib_error_t *
263 vmxnet3_txq_init (vlib_main_t * vm, vmxnet3_device_t * vd, u16 qid, u16 qsz)
264 {
265   vmxnet3_main_t *vmxm = &vmxnet3_main;
266   vmxnet3_txq_t *txq;
267   clib_error_t *error;
268
269   if (qid >= vd->num_tx_queues)
270     {
271       qid = qid % vd->num_tx_queues;
272       txq = vec_elt_at_index (vd->txqs, qid);
273       if (txq->lock == 0)
274         clib_spinlock_init (&txq->lock);
275       vd->flags |= VMXNET3_DEVICE_F_SHARED_TXQ_LOCK;
276     }
277
278   vec_validate_aligned (vd->txqs, qid, CLIB_CACHE_LINE_BYTES);
279   txq = vec_elt_at_index (vd->txqs, qid);
280   memset (txq, 0, sizeof (*txq));
281   txq->size = qsz;
282   txq->tx_desc = vlib_physmem_alloc_aligned (vm, vmxm->physmem_region, &error,
283                                              qsz * sizeof (*txq->tx_desc),
284                                              512);
285   if (error)
286     return error;
287   memset (txq->tx_desc, 0, qsz * sizeof (*txq->tx_desc));
288   txq->tx_comp = vlib_physmem_alloc_aligned (vm, vmxm->physmem_region, &error,
289                                              qsz * sizeof (*txq->tx_comp),
290                                              512);
291   if (error)
292     return error;
293   memset (txq->tx_comp, 0, qsz * sizeof (*txq->tx_comp));
294   vec_validate_aligned (txq->tx_ring.bufs, txq->size, CLIB_CACHE_LINE_BYTES);
295   txq->tx_ring.gen = VMXNET3_TXF_GEN;
296   txq->tx_comp_ring.gen = VMXNET3_TXCF_GEN;
297
298   return 0;
299 }
300
301 static clib_error_t *
302 vmxnet3_device_init (vlib_main_t * vm, vmxnet3_device_t * vd,
303                      vmxnet3_create_if_args_t * args)
304 {
305   clib_error_t *error = 0;
306   u32 ret, i;
307   vmxnet3_main_t *vmxm = &vmxnet3_main;
308   vlib_thread_main_t *tm = vlib_get_thread_main ();
309
310   vd->num_tx_queues = 1;
311   vd->num_rx_queues = 1;
312   vd->num_intrs = 1;
313
314   /* Quiesce the device */
315   vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_QUIESCE_DEV);
316   ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_CMD);
317   if (ret != 0)
318     {
319       error = clib_error_return (0, "error on quisecing device rc (%u)", ret);
320       return error;
321     }
322
323   /* Reset the device */
324   vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
325   ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_CMD);
326   if (ret != 0)
327     {
328       error = clib_error_return (0, "error on resetting device rc (%u)", ret);
329       return error;
330     }
331
332   ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_VRRS);
333   vd->version = count_leading_zeros (ret);
334   vd->version = uword_bits - vd->version;
335
336   if (vd->version == 0 || vd->version > 3)
337     {
338       error = clib_error_return (0, "unsupported hardware version %u",
339                                  vd->version);
340       return error;
341     }
342
343   vmxnet3_reg_write (vd, 1, VMXNET3_REG_VRRS, 1 << (vd->version - 1));
344
345   ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_UVRS);
346   if (ret & 1)
347     vmxnet3_reg_write (vd, 1, VMXNET3_REG_UVRS, 1);
348   else
349     {
350       error = clib_error_return (0, "unsupported upt version %u", ret);
351       return error;
352     }
353
354   /* Get the mac address */
355   ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_MACL);
356   clib_memcpy (vd->mac_addr, &ret, 4);
357   ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_MACH);
358   clib_memcpy (vd->mac_addr + 4, &ret, 2);
359
360   if (vmxm->physmem_region_alloc == 0)
361     {
362       u32 flags = VLIB_PHYSMEM_F_INIT_MHEAP | VLIB_PHYSMEM_F_HUGETLB;
363       error =
364         vlib_physmem_region_alloc (vm, "vmxnet3 descriptors", 4 << 20, 0,
365                                    flags, &vmxm->physmem_region);
366       if (error)
367         return error;
368       vmxm->physmem_region_alloc = 1;
369     }
370
371   error = vmxnet3_rxq_init (vm, vd, 0, args->rxq_size);
372   if (error)
373     return error;
374
375   for (i = 0; i < tm->n_vlib_mains; i++)
376     {
377       error = vmxnet3_txq_init (vm, vd, i, args->txq_size);
378       if (error)
379         return error;
380     }
381
382   error = vmxnet3_provision_driver_shared (vm, vd);
383   if (error)
384     return error;
385
386   vmxnet3_write_mac (vd);
387
388   /* Activate device */
389   vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_ACTIVATE_DEV);
390   ret = vmxnet3_reg_read (vd, 1, VMXNET3_REG_CMD);
391   if (ret != 0)
392     {
393       error =
394         clib_error_return (0, "error on activating device rc (%u)", ret);
395       return error;
396     }
397
398   /* Disable interrupts */
399   vmxnet3_disable_interrupt (vd);
400
401   vec_foreach_index (i, vd->rxqs)
402   {
403     vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, i);
404
405     vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
406     vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
407   }
408   vd->flags |= VMXNET3_DEVICE_F_INITIALIZED;
409
410   vmxnet3_enable_interrupt (vd);
411
412   return error;
413 }
414
415 static void
416 vmxnet3_irq_handler (vlib_pci_dev_handle_t h, u16 line)
417 {
418   vnet_main_t *vnm = vnet_get_main ();
419   vmxnet3_main_t *vmxm = &vmxnet3_main;
420   uword pd = vlib_pci_get_private_data (h);
421   vmxnet3_device_t *vd = pool_elt_at_index (vmxm->devices, pd);
422   u16 qid = line;
423
424   if (vec_len (vd->rxqs) > qid && vd->rxqs[qid].int_mode != 0)
425     vnet_device_input_set_interrupt_pending (vnm, vd->hw_if_index, qid);
426 }
427
428 static u8
429 vmxnet3_queue_size_valid (u16 qsz)
430 {
431   if (qsz < 64 || qsz > 4096)
432     return 0;
433   if ((qsz % 64) != 0)
434     return 0;
435   return 1;
436 }
437
438 void
439 vmxnet3_create_if (vlib_main_t * vm, vmxnet3_create_if_args_t * args)
440 {
441   vnet_main_t *vnm = vnet_get_main ();
442   vmxnet3_main_t *vmxm = &vmxnet3_main;
443   vmxnet3_device_t *vd;
444   vlib_pci_dev_handle_t h;
445   clib_error_t *error = 0;
446
447   if (args->rxq_size == 0)
448     args->rxq_size = VMXNET3_NUM_RX_DESC;
449   if (args->txq_size == 0)
450     args->txq_size = VMXNET3_NUM_TX_DESC;
451
452   if (!vmxnet3_queue_size_valid (args->rxq_size) ||
453       !vmxnet3_queue_size_valid (args->txq_size))
454     {
455       args->rv = VNET_API_ERROR_INVALID_VALUE;
456       args->error =
457         clib_error_return (error,
458                            "queue size must be <= 4096, >= 64, "
459                            "and multiples of 64");
460       return;
461     }
462
463   /* *INDENT-OFF* */
464   pool_foreach (vd, vmxm->devices, ({
465     if (vd->pci_addr.as_u32 == args->addr.as_u32)
466       {
467         args->rv = VNET_API_ERROR_INVALID_VALUE;
468         args->error =
469           clib_error_return (error, "PCI address in use");
470         return;
471       }
472   }));
473   /* *INDENT-ON* */
474
475   pool_get (vmxm->devices, vd);
476   vd->dev_instance = vd - vmxm->devices;
477   vd->per_interface_next_index = ~0;
478   vd->pci_addr = args->addr;
479
480   if (args->enable_elog)
481     vd->flags |= VMXNET3_DEVICE_F_ELOG;
482
483   if ((error =
484        vlib_pci_device_open (&args->addr, vmxnet3_pci_device_ids, &h)))
485     {
486       pool_put (vmxm->devices, vd);
487       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
488       args->error =
489         clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
490                            &args->addr);
491       return;
492     }
493   vd->pci_dev_handle = h;
494
495   vlib_pci_set_private_data (h, vd->dev_instance);
496
497   if ((error = vlib_pci_bus_master_enable (h)))
498     goto error;
499
500   if ((error = vlib_pci_map_region (h, 0, (void **) &vd->bar[0])))
501     goto error;
502
503   if ((error = vlib_pci_map_region (h, 1, (void **) &vd->bar[1])))
504     goto error;
505
506   if ((error = vlib_pci_register_msix_handler (h, 0, 1,
507                                                &vmxnet3_irq_handler)))
508     goto error;
509
510   if ((error = vlib_pci_enable_msix_irq (h, 0, 1)))
511     goto error;
512
513   if ((error = vlib_pci_intr_enable (h)))
514     goto error;
515
516   if ((error = vmxnet3_device_init (vm, vd, args)))
517     goto error;
518
519   /* create interface */
520   error = ethernet_register_interface (vnm, vmxnet3_device_class.index,
521                                        vd->dev_instance, vd->mac_addr,
522                                        &vd->hw_if_index, vmxnet3_flag_change);
523
524   if (error)
525     goto error;
526
527   vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vd->hw_if_index);
528   vd->sw_if_index = sw->sw_if_index;
529   args->sw_if_index = sw->sw_if_index;
530
531   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vd->hw_if_index);
532   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
533   vnet_hw_interface_set_input_node (vnm, vd->hw_if_index,
534                                     vmxnet3_input_node.index);
535   vnet_hw_interface_assign_rx_thread (vnm, vd->hw_if_index, 0, ~0);
536   return;
537
538 error:
539   vmxnet3_delete_if (vm, vd);
540   args->rv = VNET_API_ERROR_INVALID_INTERFACE;
541   args->error = error;
542 }
543
544 void
545 vmxnet3_delete_if (vlib_main_t * vm, vmxnet3_device_t * vd)
546 {
547   vnet_main_t *vnm = vnet_get_main ();
548   vmxnet3_main_t *vmxm = &vmxnet3_main;
549   u32 i, bi;
550   u16 desc_idx;
551
552   /* Quiesce the device */
553   vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_QUIESCE_DEV);
554
555   /* Reset the device */
556   vmxnet3_reg_write (vd, 1, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
557
558   if (vd->hw_if_index)
559     {
560       vnet_hw_interface_set_flags (vnm, vd->hw_if_index, 0);
561       vnet_hw_interface_unassign_rx_thread (vnm, vd->hw_if_index, 0);
562       ethernet_delete_interface (vnm, vd->hw_if_index);
563     }
564
565   vlib_pci_device_close (vd->pci_dev_handle);
566
567   /* *INDENT-OFF* */
568   vec_foreach_index (i, vd->rxqs)
569     {
570       vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, i);
571       u16 mask = rxq->size - 1;
572       u16 rid;
573
574       for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++)
575         {
576           vmxnet3_rx_ring *ring;
577
578           ring = &rxq->rx_ring[rid];
579           desc_idx = ring->consume;
580           while (ring->fill)
581             {
582               desc_idx &= mask;
583               bi = ring->bufs[desc_idx];
584               vlib_buffer_free_no_next (vm, &bi, 1);
585               ring->fill--;
586               desc_idx++;
587             }
588           vec_free (ring->bufs);
589           vlib_physmem_free (vm, vmxm->physmem_region, rxq->rx_desc[rid]);
590         }
591       vlib_physmem_free (vm, vmxm->physmem_region, rxq->rx_comp);
592     }
593   /* *INDENT-ON* */
594   vec_free (vd->rxqs);
595
596   /* *INDENT-OFF* */
597   vec_foreach_index (i, vd->txqs)
598     {
599       vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, i);
600       u16 mask = txq->size - 1;
601       u16 end_idx;
602
603       desc_idx = txq->tx_ring.consume;
604       end_idx = txq->tx_ring.produce;
605       while (desc_idx != end_idx)
606         {
607           bi = txq->tx_ring.bufs[desc_idx];
608           vlib_buffer_free_no_next (vm, &bi, 1);
609           desc_idx++;
610           desc_idx &= mask;
611         }
612       clib_spinlock_free (&txq->lock);
613       vec_free (txq->tx_ring.bufs);
614       vlib_physmem_free (vm, vmxm->physmem_region, txq->tx_desc);
615       vlib_physmem_free (vm, vmxm->physmem_region, txq->tx_comp);
616     }
617   /* *INDENT-ON* */
618   vec_free (vd->txqs);
619
620   vlib_physmem_free (vm, vmxm->physmem_region, vd->dma);
621
622   clib_error_free (vd->error);
623   memset (vd, 0, sizeof (*vd));
624   pool_put (vmxm->devices, vd);
625 }
626
627 /*
628  * fd.io coding-style-patch-verification: ON
629  *
630  * Local Variables:
631  * eval: (c-set-style "gnu")
632  * End:
633  */