iPXE
vmxnet3.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <byteswap.h>
30 #include <ipxe/pci.h>
31 #include <ipxe/io.h>
32 #include <ipxe/malloc.h>
33 #include <ipxe/profile.h>
34 #include <ipxe/iobuf.h>
35 #include <ipxe/netdevice.h>
36 #include <ipxe/if_ether.h>
37 #include <ipxe/ethernet.h>
38 #include "vmxnet3.h"
39 
40 /**
41  * @file
42  *
43  * VMware vmxnet3 virtual NIC driver
44  *
45  */
46 
47 /** VM command profiler */
48 static struct profiler vmxnet3_vm_command_profiler __profiler =
49  { .name = "vmxnet3.vm_command" };
50 
51 /** VM transmit profiler */
52 static struct profiler vmxnet3_vm_tx_profiler __profiler =
53  { .name = "vmxnet3.vm_tx" };
54 
55 /** VM receive refill profiler */
56 static struct profiler vmxnet3_vm_refill_profiler __profiler =
57  { .name = "vmxnet3.vm_refill" };
58 
59 /** VM event profiler */
60 static struct profiler vmxnet3_vm_event_profiler __profiler =
61  { .name = "vmxnet3.vm_event" };
62 
63 /**
64  * Issue command
65  *
66  * @v vmxnet vmxnet3 NIC
67  * @v command Command to issue
68  * @ret result Command result
69  */
70 static inline uint32_t vmxnet3_command ( struct vmxnet3_nic *vmxnet,
71  uint32_t command ) {
73 
74  /* Issue command */
75  profile_start ( &vmxnet3_vm_command_profiler );
76  writel ( command, ( vmxnet->vd + VMXNET3_VD_CMD ) );
77  result = readl ( vmxnet->vd + VMXNET3_VD_CMD );
78  profile_stop ( &vmxnet3_vm_command_profiler );
79  profile_exclude ( &vmxnet3_vm_command_profiler );
80 
81  return result;
82 }
83 
84 /**
85  * Transmit packet
86  *
87  * @v netdev Network device
88  * @v iobuf I/O buffer
89  * @ret rc Return status code
90  */
91 static int vmxnet3_transmit ( struct net_device *netdev,
92  struct io_buffer *iobuf ) {
93  struct vmxnet3_nic *vmxnet = netdev->priv;
94  struct vmxnet3_tx_desc *tx_desc;
95  unsigned int fill;
96  unsigned int desc_idx;
97  unsigned int generation;
98 
99  /* Check that we have a free transmit descriptor */
100  fill = ( vmxnet->count.tx_prod - vmxnet->count.tx_cons );
101  if ( fill >= VMXNET3_TX_FILL ) {
102  DBGC ( vmxnet, "VMXNET3 %p out of transmit descriptors\n",
103  vmxnet );
104  return -ENOBUFS;
105  }
106 
107  /* Locate transmit descriptor */
108  desc_idx = ( vmxnet->count.tx_prod % VMXNET3_NUM_TX_DESC );
109  generation = ( ( vmxnet->count.tx_prod & VMXNET3_NUM_TX_DESC ) ?
110  0 : cpu_to_le32 ( VMXNET3_TXF_GEN ) );
111  assert ( vmxnet->tx_iobuf[desc_idx] == NULL );
112 
113  /* Increment producer counter */
114  vmxnet->count.tx_prod++;
115 
116  /* Store I/O buffer for later completion */
117  vmxnet->tx_iobuf[desc_idx] = iobuf;
118 
119  /* Populate transmit descriptor */
120  tx_desc = &vmxnet->dma->tx_desc[desc_idx];
121  tx_desc->address = cpu_to_le64 ( virt_to_bus ( iobuf->data ) );
122  tx_desc->flags[0] = ( generation | cpu_to_le32 ( iob_len ( iobuf ) ) );
124 
125  /* Hand over descriptor to NIC */
126  wmb();
127  profile_start ( &vmxnet3_vm_tx_profiler );
128  writel ( ( vmxnet->count.tx_prod % VMXNET3_NUM_TX_DESC ),
129  ( vmxnet->pt + VMXNET3_PT_TXPROD ) );
130  profile_stop ( &vmxnet3_vm_tx_profiler );
131  profile_exclude ( &vmxnet3_vm_tx_profiler );
132 
133  return 0;
134 }
135 
136 /**
137  * Poll for completed transmissions
138  *
139  * @v netdev Network device
140  */
141 static void vmxnet3_poll_tx ( struct net_device *netdev ) {
142  struct vmxnet3_nic *vmxnet = netdev->priv;
143  struct vmxnet3_tx_comp *tx_comp;
144  struct io_buffer *iobuf;
145  unsigned int comp_idx;
146  unsigned int desc_idx;
147  unsigned int generation;
148 
149  while ( 1 ) {
150 
151  /* Look for completed descriptors */
152  comp_idx = ( vmxnet->count.tx_cons % VMXNET3_NUM_TX_COMP );
153  generation = ( ( vmxnet->count.tx_cons & VMXNET3_NUM_TX_COMP ) ?
154  0 : cpu_to_le32 ( VMXNET3_TXCF_GEN ) );
155  tx_comp = &vmxnet->dma->tx_comp[comp_idx];
156  if ( generation != ( tx_comp->flags &
157  cpu_to_le32 ( VMXNET3_TXCF_GEN ) ) ) {
158  break;
159  }
160 
161  /* Increment consumer counter */
162  vmxnet->count.tx_cons++;
163 
164  /* Locate corresponding transmit descriptor */
165  desc_idx = ( le32_to_cpu ( tx_comp->index ) %
167  iobuf = vmxnet->tx_iobuf[desc_idx];
168  if ( ! iobuf ) {
169  DBGC ( vmxnet, "VMXNET3 %p completed on empty transmit "
170  "buffer %#x/%#x\n", vmxnet, comp_idx, desc_idx );
172  continue;
173  }
174 
175  /* Remove I/O buffer from transmit queue */
176  vmxnet->tx_iobuf[desc_idx] = NULL;
177 
178  /* Report transmission completion to network layer */
179  DBGC2 ( vmxnet, "VMXNET3 %p completed TX %#x/%#x (len %#zx)\n",
180  vmxnet, comp_idx, desc_idx, iob_len ( iobuf ) );
181  netdev_tx_complete ( netdev, iobuf );
182  }
183 }
184 
185 /**
186  * Flush any uncompleted transmit buffers
187  *
188  * @v netdev Network device
189  */
190 static void vmxnet3_flush_tx ( struct net_device *netdev ) {
191  struct vmxnet3_nic *vmxnet = netdev->priv;
192  unsigned int i;
193 
194  for ( i = 0 ; i < VMXNET3_NUM_TX_DESC ; i++ ) {
195  if ( vmxnet->tx_iobuf[i] ) {
196  netdev_tx_complete_err ( netdev, vmxnet->tx_iobuf[i],
197  -ECANCELED );
198  vmxnet->tx_iobuf[i] = NULL;
199  }
200  }
201 }
202 
203 /**
204  * Refill receive ring
205  *
206  * @v netdev Network device
207  */
208 static void vmxnet3_refill_rx ( struct net_device *netdev ) {
209  struct vmxnet3_nic *vmxnet = netdev->priv;
210  struct vmxnet3_rx_desc *rx_desc;
211  struct io_buffer *iobuf;
212  unsigned int orig_rx_prod = vmxnet->count.rx_prod;
213  unsigned int desc_idx;
214  unsigned int generation;
215 
216  /* Fill receive ring to specified fill level */
217  while ( vmxnet->count.rx_fill < VMXNET3_RX_FILL ) {
218 
219  /* Locate receive descriptor */
220  desc_idx = ( vmxnet->count.rx_prod % VMXNET3_NUM_RX_DESC );
221  generation = ( ( vmxnet->count.rx_prod & VMXNET3_NUM_RX_DESC ) ?
222  0 : cpu_to_le32 ( VMXNET3_RXF_GEN ) );
223  assert ( vmxnet->rx_iobuf[desc_idx] == NULL );
224 
225  /* Allocate I/O buffer */
226  iobuf = alloc_iob ( VMXNET3_MTU + NET_IP_ALIGN );
227  if ( ! iobuf ) {
228  /* Non-fatal low memory condition */
229  break;
230  }
231  iob_reserve ( iobuf, NET_IP_ALIGN );
232 
233  /* Increment producer counter and fill level */
234  vmxnet->count.rx_prod++;
235  vmxnet->count.rx_fill++;
236 
237  /* Store I/O buffer for later completion */
238  vmxnet->rx_iobuf[desc_idx] = iobuf;
239 
240  /* Populate receive descriptor */
241  rx_desc = &vmxnet->dma->rx_desc[desc_idx];
242  rx_desc->address = cpu_to_le64 ( virt_to_bus ( iobuf->data ) );
243  rx_desc->flags = ( generation | cpu_to_le32 ( VMXNET3_MTU ) );
244 
245  }
246 
247  /* Hand over any new descriptors to NIC */
248  if ( vmxnet->count.rx_prod != orig_rx_prod ) {
249  wmb();
250  profile_start ( &vmxnet3_vm_refill_profiler );
251  writel ( ( vmxnet->count.rx_prod % VMXNET3_NUM_RX_DESC ),
252  ( vmxnet->pt + VMXNET3_PT_RXPROD ) );
253  profile_stop ( &vmxnet3_vm_refill_profiler );
254  profile_exclude ( &vmxnet3_vm_refill_profiler );
255  }
256 }
257 
258 /**
259  * Poll for received packets
260  *
261  * @v netdev Network device
262  */
263 static void vmxnet3_poll_rx ( struct net_device *netdev ) {
264  struct vmxnet3_nic *vmxnet = netdev->priv;
265  struct vmxnet3_rx_comp *rx_comp;
266  struct io_buffer *iobuf;
267  unsigned int comp_idx;
268  unsigned int desc_idx;
269  unsigned int generation;
270  size_t len;
271 
272  while ( 1 ) {
273 
274  /* Look for completed descriptors */
275  comp_idx = ( vmxnet->count.rx_cons % VMXNET3_NUM_RX_COMP );
276  generation = ( ( vmxnet->count.rx_cons & VMXNET3_NUM_RX_COMP ) ?
277  0 : cpu_to_le32 ( VMXNET3_RXCF_GEN ) );
278  rx_comp = &vmxnet->dma->rx_comp[comp_idx];
279  if ( generation != ( rx_comp->flags &
280  cpu_to_le32 ( VMXNET3_RXCF_GEN ) ) ) {
281  break;
282  }
283 
284  /* Increment consumer counter */
285  vmxnet->count.rx_cons++;
286 
287  /* Locate corresponding receive descriptor */
288  desc_idx = ( le32_to_cpu ( rx_comp->index ) %
290  iobuf = vmxnet->rx_iobuf[desc_idx];
291  if ( ! iobuf ) {
292  DBGC ( vmxnet, "VMXNET3 %p completed on empty receive "
293  "buffer %#x/%#x\n", vmxnet, comp_idx, desc_idx );
295  continue;
296  }
297 
298  /* Remove I/O buffer from receive queue */
299  vmxnet->rx_iobuf[desc_idx] = NULL;
300  vmxnet->count.rx_fill--;
301 
302  /* Deliver packet to network layer */
303  len = ( le32_to_cpu ( rx_comp->len ) &
304  ( VMXNET3_MAX_PACKET_LEN - 1 ) );
305  DBGC2 ( vmxnet, "VMXNET3 %p completed RX %#x/%#x (len %#zx)\n",
306  vmxnet, comp_idx, desc_idx, len );
307  iob_put ( iobuf, len );
308  netdev_rx ( netdev, iobuf );
309  }
310 }
311 
312 /**
313  * Flush any uncompleted receive buffers
314  *
315  * @v netdev Network device
316  */
317 static void vmxnet3_flush_rx ( struct net_device *netdev ) {
318  struct vmxnet3_nic *vmxnet = netdev->priv;
319  struct io_buffer *iobuf;
320  unsigned int i;
321 
322  for ( i = 0 ; i < VMXNET3_NUM_RX_DESC ; i++ ) {
323  if ( ( iobuf = vmxnet->rx_iobuf[i] ) != NULL ) {
324  netdev_rx_err ( netdev, iobuf, -ECANCELED );
325  vmxnet->rx_iobuf[i] = NULL;
326  }
327  }
328 }
329 
330 /**
331  * Check link state
332  *
333  * @v netdev Network device
334  */
335 static void vmxnet3_check_link ( struct net_device *netdev ) {
336  struct vmxnet3_nic *vmxnet = netdev->priv;
337  uint32_t state;
338  int link_up;
339  unsigned int link_speed;
340 
341  /* Get link state */
343  link_up = ( state & 1 );
344  link_speed = ( state >> 16 );
345 
346  /* Report link state to network device */
347  if ( link_up ) {
348  DBGC ( vmxnet, "VMXNET3 %p link is up at %d Mbps\n",
349  vmxnet, link_speed );
351  } else {
352  DBGC ( vmxnet, "VMXNET3 %p link is down\n", vmxnet );
354  }
355 }
356 
357 /**
358  * Poll for events
359  *
360  * @v netdev Network device
361  */
362 static void vmxnet3_poll_events ( struct net_device *netdev ) {
363  struct vmxnet3_nic *vmxnet = netdev->priv;
364  uint32_t events;
365 
366  /* Do nothing unless there are events to process */
367  if ( ! vmxnet->dma->shared.ecr )
368  return;
369  events = le32_to_cpu ( vmxnet->dma->shared.ecr );
370 
371  /* Acknowledge these events */
372  profile_start ( &vmxnet3_vm_event_profiler );
373  writel ( events, ( vmxnet->vd + VMXNET3_VD_ECR ) );
374  profile_stop ( &vmxnet3_vm_event_profiler );
375  profile_exclude ( &vmxnet3_vm_event_profiler );
376 
377  /* Check for link state change */
378  if ( events & VMXNET3_ECR_LINK ) {
380  events &= ~VMXNET3_ECR_LINK;
381  }
382 
383  /* Check for queue errors */
384  if ( events & ( VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR ) ) {
386  DBGC ( vmxnet, "VMXNET3 %p queue error status (TX %08x, RX "
387  "%08x)\n", vmxnet,
388  le32_to_cpu ( vmxnet->dma->queues.tx.status.error ),
389  le32_to_cpu ( vmxnet->dma->queues.rx.status.error ) );
390  /* Report errors to allow for visibility via "ifstat" */
391  if ( events & VMXNET3_ECR_TQERR )
393  if ( events & VMXNET3_ECR_RQERR )
395  events &= ~( VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR );
396  }
397 
398  /* Check for unknown events */
399  if ( events ) {
400  DBGC ( vmxnet, "VMXNET3 %p unknown events %08x\n",
401  vmxnet, events );
402  /* Report error to allow for visibility via "ifstat" */
404  }
405 }
406 
407 /**
408  * Poll network device
409  *
410  * @v netdev Network device
411  */
412 static void vmxnet3_poll ( struct net_device *netdev ) {
413 
418 }
419 
420 /**
421  * Enable/disable interrupts
422  *
423  * @v netdev Network device
424  * @v enable Interrupts should be enabled
425  */
426 static void vmxnet3_irq ( struct net_device *netdev, int enable ) {
427  struct vmxnet3_nic *vmxnet = netdev->priv;
428 
429  DBGC ( vmxnet, "VMXNET3 %p %s IRQ not implemented\n",
430  vmxnet, ( enable ? "enable" : "disable" ) );
431 }
432 
433 /**
434  * Set MAC address
435  *
436  * @v vmxnet vmxnet3 NIC
437  * @v ll_addr Link-layer address to set
438  */
439 static void vmxnet3_set_ll_addr ( struct vmxnet3_nic *vmxnet,
440  const void *ll_addr ) {
441  struct {
442  uint32_t low;
443  uint32_t high;
444  } __attribute__ (( packed )) mac;
445 
446  memset ( &mac, 0, sizeof ( mac ) );
447  memcpy ( &mac, ll_addr, ETH_ALEN );
448  writel ( cpu_to_le32 ( mac.low ), ( vmxnet->vd + VMXNET3_VD_MACL ) );
449  writel ( cpu_to_le32 ( mac.high ), ( vmxnet->vd + VMXNET3_VD_MACH ) );
450 }
451 
452 /**
453  * Open NIC
454  *
455  * @v netdev Network device
456  * @ret rc Return status code
457  */
458 static int vmxnet3_open ( struct net_device *netdev ) {
459  struct vmxnet3_nic *vmxnet = netdev->priv;
460  struct vmxnet3_shared *shared;
461  struct vmxnet3_queues *queues;
462  uint64_t shared_bus;
463  uint64_t queues_bus;
465  int rc;
466 
467  /* Allocate DMA areas */
468  vmxnet->dma = malloc_phys ( sizeof ( *vmxnet->dma ),
470  if ( ! vmxnet->dma ) {
471  DBGC ( vmxnet, "VMXNET3 %p could not allocate DMA area\n",
472  vmxnet );
473  rc = -ENOMEM;
474  goto err_alloc_dma;
475  }
476  memset ( vmxnet->dma, 0, sizeof ( *vmxnet->dma ) );
477 
478  /* Populate queue descriptors */
479  queues = &vmxnet->dma->queues;
480  queues->tx.cfg.desc_address =
481  cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->tx_desc ) );
482  queues->tx.cfg.comp_address =
483  cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->tx_comp ) );
486  queues->rx.cfg.desc_address[0] =
487  cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->rx_desc ) );
488  queues->rx.cfg.comp_address =
489  cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->rx_comp ) );
490  queues->rx.cfg.num_desc[0] = cpu_to_le32 ( VMXNET3_NUM_RX_DESC );
492  queues_bus = virt_to_bus ( queues );
493  DBGC ( vmxnet, "VMXNET3 %p queue descriptors at %08llx+%zx\n",
494  vmxnet, queues_bus, sizeof ( *queues ) );
495 
496  /* Populate shared area */
497  shared = &vmxnet->dma->shared;
498  shared->magic = cpu_to_le32 ( VMXNET3_SHARED_MAGIC );
501  shared->misc.upt_version_support =
503  shared->misc.queue_desc_address = cpu_to_le64 ( queues_bus );
504  shared->misc.queue_desc_len = cpu_to_le32 ( sizeof ( *queues ) );
505  shared->misc.mtu = cpu_to_le32 ( VMXNET3_MTU );
506  shared->misc.num_tx_queues = 1;
507  shared->misc.num_rx_queues = 1;
508  shared->interrupt.num_intrs = 1;
513  shared_bus = virt_to_bus ( shared );
514  DBGC ( vmxnet, "VMXNET3 %p shared area at %08llx+%zx\n",
515  vmxnet, shared_bus, sizeof ( *shared ) );
516 
517  /* Zero counters */
518  memset ( &vmxnet->count, 0, sizeof ( vmxnet->count ) );
519 
520  /* Set MAC address */
521  vmxnet3_set_ll_addr ( vmxnet, &netdev->ll_addr );
522 
523  /* Pass shared area to device */
524  writel ( ( shared_bus >> 0 ), ( vmxnet->vd + VMXNET3_VD_DSAL ) );
525  writel ( ( shared_bus >> 32 ), ( vmxnet->vd + VMXNET3_VD_DSAH ) );
526 
527  /* Activate device */
528  if ( ( status = vmxnet3_command ( vmxnet,
529  VMXNET3_CMD_ACTIVATE_DEV ) ) != 0 ) {
530  DBGC ( vmxnet, "VMXNET3 %p could not activate (status %#x)\n",
531  vmxnet, status );
532  rc = -EIO;
533  goto err_activate;
534  }
535 
536  /* Fill receive ring */
538 
539  return 0;
540 
543  err_activate:
546  free_phys ( vmxnet->dma, sizeof ( *vmxnet->dma ) );
547  err_alloc_dma:
548  return rc;
549 }
550 
551 /**
552  * Close NIC
553  *
554  * @v netdev Network device
555  */
556 static void vmxnet3_close ( struct net_device *netdev ) {
557  struct vmxnet3_nic *vmxnet = netdev->priv;
558 
563  free_phys ( vmxnet->dma, sizeof ( *vmxnet->dma ) );
564 }
565 
566 /** vmxnet3 net device operations */
568  .open = vmxnet3_open,
569  .close = vmxnet3_close,
570  .transmit = vmxnet3_transmit,
571  .poll = vmxnet3_poll,
572  .irq = vmxnet3_irq,
573 };
574 
575 /**
576  * Check version
577  *
578  * @v vmxnet vmxnet3 NIC
579  * @ret rc Return status code
580  */
581 static int vmxnet3_check_version ( struct vmxnet3_nic *vmxnet ) {
583  uint32_t upt_version;
584 
585  /* Read version */
586  version = readl ( vmxnet->vd + VMXNET3_VD_VRRS );
587  upt_version = readl ( vmxnet->vd + VMXNET3_VD_UVRS );
588  DBGC ( vmxnet, "VMXNET3 %p is version %d (UPT version %d)\n",
589  vmxnet, version, upt_version );
590 
591  /* Inform NIC of driver version */
592  writel ( VMXNET3_VERSION_SELECT, ( vmxnet->vd + VMXNET3_VD_VRRS ) );
594 
595  return 0;
596 }
597 
598 /**
599  * Get permanent MAC address
600  *
601  * @v vmxnet vmxnet3 NIC
602  * @v hw_addr Hardware address to fill in
603  */
604 static void vmxnet3_get_hw_addr ( struct vmxnet3_nic *vmxnet, void *hw_addr ) {
605  struct {
606  uint32_t low;
607  uint32_t high;
608  } __attribute__ (( packed )) mac;
609 
610  mac.low = le32_to_cpu ( vmxnet3_command ( vmxnet,
612  mac.high = le32_to_cpu ( vmxnet3_command ( vmxnet,
614  memcpy ( hw_addr, &mac, ETH_ALEN );
615 }
616 
617 /**
618  * Probe PCI device
619  *
620  * @v pci PCI device
621  * @v id PCI ID
622  * @ret rc Return status code
623  */
624 static int vmxnet3_probe ( struct pci_device *pci ) {
625  struct net_device *netdev;
626  struct vmxnet3_nic *vmxnet;
627  int rc;
628 
629  /* Allocate network device */
630  netdev = alloc_etherdev ( sizeof ( *vmxnet ) );
631  if ( ! netdev ) {
632  rc = -ENOMEM;
633  goto err_alloc_etherdev;
634  }
636  vmxnet = netdev->priv;
637  pci_set_drvdata ( pci, netdev );
638  netdev->dev = &pci->dev;
639  memset ( vmxnet, 0, sizeof ( *vmxnet ) );
640 
641  /* Fix up PCI device */
642  adjust_pci_device ( pci );
643 
644  /* Map PCI BARs */
645  vmxnet->pt = pci_ioremap ( pci, pci_bar_start ( pci, VMXNET3_PT_BAR ),
646  VMXNET3_PT_LEN );
647  if ( ! vmxnet->pt ) {
648  rc = -ENODEV;
649  goto err_ioremap_pt;
650  }
651  vmxnet->vd = pci_ioremap ( pci, pci_bar_start ( pci, VMXNET3_VD_BAR ),
652  VMXNET3_VD_LEN );
653  if ( ! vmxnet->vd ) {
654  rc = -ENODEV;
655  goto err_ioremap_vd;
656  }
657 
658  /* Version check */
659  if ( ( rc = vmxnet3_check_version ( vmxnet ) ) != 0 )
660  goto err_check_version;
661 
662  /* Reset device */
663  if ( ( rc = vmxnet3_command ( vmxnet, VMXNET3_CMD_RESET_DEV ) ) != 0 )
664  goto err_reset;
665 
666  /* Read initial MAC address */
667  vmxnet3_get_hw_addr ( vmxnet, &netdev->hw_addr );
668 
669  /* Register network device */
670  if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
671  DBGC ( vmxnet, "VMXNET3 %p could not register net device: "
672  "%s\n", vmxnet, strerror ( rc ) );
673  goto err_register_netdev;
674  }
675 
676  /* Get initial link state */
678 
679  return 0;
680 
682  err_register_netdev:
683  err_reset:
684  err_check_version:
685  iounmap ( vmxnet->vd );
686  err_ioremap_vd:
687  iounmap ( vmxnet->pt );
688  err_ioremap_pt:
690  netdev_put ( netdev );
691  err_alloc_etherdev:
692  return rc;
693 }
694 
695 /**
696  * Remove PCI device
697  *
698  * @v pci PCI device
699  */
700 static void vmxnet3_remove ( struct pci_device *pci ) {
701  struct net_device *netdev = pci_get_drvdata ( pci );
702  struct vmxnet3_nic *vmxnet = netdev->priv;
703 
705  iounmap ( vmxnet->vd );
706  iounmap ( vmxnet->pt );
708  netdev_put ( netdev );
709 }
710 
711 /** vmxnet3 PCI IDs */
712 static struct pci_device_id vmxnet3_nics[] = {
713  PCI_ROM ( 0x15ad, 0x07b0, "vmxnet3", "vmxnet3 virtual NIC", 0 ),
714 };
715 
716 /** vmxnet3 PCI driver */
717 struct pci_driver vmxnet3_driver __pci_driver = {
718  .ids = vmxnet3_nics,
719  .id_count = ( sizeof ( vmxnet3_nics ) / sizeof ( vmxnet3_nics[0] ) ),
720  .probe = vmxnet3_probe,
722 };
static int vmxnet3_transmit(struct net_device *netdev, struct io_buffer *iobuf)
Transmit packet.
Definition: vmxnet3.c:91
#define NET_IP_ALIGN
Definition: atl1e.h:47
#define VMXNET3_RX_FILL
Receive ring maximum fill level.
Definition: vmxnet3.h:500
struct vmxnet3_tx_queue_config cfg
Definition: vmxnet3.h:387
struct vmxnet3_rx_queue_config cfg
Definition: vmxnet3.h:396
#define VMXNET3_DMA_ALIGN
DMA area alignment.
Definition: vmxnet3.h:454
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define __attribute__(x)
Definition: compiler.h:10
Driver shared area.
Definition: vmxnet3.h:216
iPXE I/O API
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static uint32_t vmxnet3_command(struct vmxnet3_nic *vmxnet, uint32_t command)
Issue command.
Definition: vmxnet3.c:70
static void netdev_tx_complete(struct net_device *netdev, struct io_buffer *iobuf)
Complete network transmission.
Definition: netdevice.h:752
wmb()
uint32_t low
Low 16 bits of address.
Definition: myson.h:19
#define VMXNET3_VD_DSAH
Driver Shared Address High.
Definition: vmxnet3.h:84
#define iob_put(iobuf, len)
Definition: iobuf.h:120
Broadcast only.
Definition: vmxnet3.h:203
static const void const void void * result
Definition: crypto.h:335
void netdev_rx_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Discard received packet.
Definition: netdevice.c:586
void * vd
"VD" register base address
Definition: vmxnet3.h:475
Transmit descriptor.
Definition: vmxnet3.h:246
A PCI driver.
Definition: pci.h:247
uint8_t state
State.
Definition: eth_slow.h:47
uint32_t version_support
Version supported.
Definition: vmxnet3.h:144
#define le32_to_cpu(value)
Definition: byteswap.h:113
int(* open)(struct net_device *netdev)
Open network device.
Definition: netdevice.h:222
static void vmxnet3_poll_events(struct net_device *netdev)
Poll for events.
Definition: vmxnet3.c:362
void netdev_tx_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Discard transmitted packet.
Definition: netdevice.c:440
uint64_t desc_address
Descriptor ring address.
Definition: vmxnet3.h:285
Error codes.
A command-line command.
Definition: command.h:9
#define VMXNET3_PT_BAR
"PT" PCI BAR address
Definition: vmxnet3.h:51
#define VMXNET3_VD_VRRS
vmxnet3 Revision Report Selection
Definition: vmxnet3.h:75
I/O buffers.
struct pci_device_id * ids
PCI ID table.
Definition: pci.h:249
struct vmxnet3_rx_comp rx_comp[VMXNET3_NUM_RX_COMP]
RX completion ring.
Definition: vmxnet3.h:446
Receive descriptor.
Definition: vmxnet3.h:315
#define VMXNET3_PT_LEN
"PT" PCI BAR size
Definition: vmxnet3.h:54
Unicast only.
Definition: vmxnet3.h:201
#define EPIPE
Broken pipe.
Definition: errno.h:619
#define VMXNET3_MTU
MTU size.
Definition: vmxnet3.h:494
uint32_t readl(volatile uint32_t *io_addr)
Read 32-bit dword from memory-mapped device.
uint8_t num_rx_queues
Number of RX queues.
Definition: vmxnet3.h:164
struct vmxnet3_interrupt_config interrupt
Interrupt configuration.
Definition: vmxnet3.h:224
#define DBGC(...)
Definition: compiler.h:505
unsigned int rx_fill
Receive fill level.
Definition: vmxnet3.h:465
#define VMXNET3_TX_FILL
Transmit ring maximum fill level.
Definition: vmxnet3.h:497
#define VMXNET3_NUM_TX_DESC
Number of TX descriptors.
Definition: vmxnet3.h:421
static int vmxnet3_check_version(struct vmxnet3_nic *vmxnet)
Check version.
Definition: vmxnet3.c:581
unsigned int tx_prod
Transmit producer counter.
Definition: vmxnet3.h:459
unsigned long long uint64_t
Definition: stdint.h:13
static void *__malloc malloc_phys(size_t size, size_t phys_align)
Allocate memory with specified physical alignment.
Definition: malloc.h:62
static void vmxnet3_poll_rx(struct net_device *netdev)
Poll for received packets.
Definition: vmxnet3.c:263
#define cpu_to_le64(value)
Definition: byteswap.h:108
struct vmxnet3_dma * dma
DMA area.
Definition: vmxnet3.h:478
void netdev_link_down(struct net_device *netdev)
Mark network device as having link down.
Definition: netdevice.c:230
uint8_t mac[ETH_ALEN]
MAC address.
Definition: ena.h:24
A data structure for storing profiling information.
Definition: profile.h:26
#define VMXNET3_RXCF_GEN
Receive completion generation flag.
Definition: vmxnet3.h:340
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:171
#define VMXNET3_VERSION_SELECT
vmxnet3 version that we support
Definition: vmxnet3.h:488
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition: pci.c:154
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:129
static void vmxnet3_flush_rx(struct net_device *netdev)
Flush any uncompleted receive buffers.
Definition: vmxnet3.c:317
uint32_t flags
Flags.
Definition: vmxnet3.h:269
struct device dev
Generic device.
Definition: pci.h:208
uint32_t num_desc
Number of descriptors.
Definition: vmxnet3.h:295
#define ECANCELED
Operation canceled.
Definition: errno.h:343
static void vmxnet3_poll_tx(struct net_device *netdev)
Poll for completed transmissions.
Definition: vmxnet3.c:141
uint32_t index
Index of the end-of-packet descriptor.
Definition: vmxnet3.h:265
Dynamic memory allocation.
#define VMXNET3_PT_RXPROD
Rx producer index for ring 1.
Definition: vmxnet3.h:63
uint8_t status
Status.
Definition: ena.h:16
static void netdev_init(struct net_device *netdev, struct net_device_operations *op)
Initialise a network device.
Definition: netdevice.h:515
static struct pci_device_id vmxnet3_nics[]
vmxnet3 PCI IDs
Definition: vmxnet3.c:712
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition: pci.h:359
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * memcpy(void *dest, const void *src, size_t len) __nonnull
const char * name
Name.
Definition: profile.h:28
static void vmxnet3_poll(struct net_device *netdev)
Poll network device.
Definition: vmxnet3.c:412
static __always_inline unsigned long virt_to_bus(volatile const void *addr)
Convert virtual address to a bus address.
Definition: io.h:183
uint32_t upt_version_support
UPT version supported.
Definition: vmxnet3.h:146
uint32_t mtu
Maximum transmission unit.
Definition: vmxnet3.h:158
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition: netdevice.h:572
Ethernet protocol.
#define VMXNET3_NUM_TX_COMP
Number of TX completion descriptors.
Definition: vmxnet3.h:424
static int vmxnet3_open(struct net_device *netdev)
Open NIC.
Definition: vmxnet3.c:458
void * priv
Driver private data.
Definition: netdevice.h:431
static void netdev_link_up(struct net_device *netdev)
Mark network device as having link up.
Definition: netdevice.h:774
void writel(uint32_t data, volatile uint32_t *io_addr)
Write 32-bit dword to memory-mapped device.
static void vmxnet3_get_hw_addr(struct vmxnet3_nic *vmxnet, void *hw_addr)
Get permanent MAC address.
Definition: vmxnet3.c:604
uint64_t queue_desc_address
Queue descriptors data address.
Definition: vmxnet3.h:152
uint32_t queue_desc_len
Queue descriptors data length.
Definition: vmxnet3.h:156
static struct net_device * netdev
Definition: gdbudp.c:52
struct pci_driver vmxnet3_driver __pci_driver
vmxnet3 PCI driver
Definition: vmxnet3.c:717
struct io_buffer * tx_iobuf[VMXNET3_NUM_TX_DESC]
Transmit I/O buffers.
Definition: vmxnet3.h:482
unsigned long pci_bar_start(struct pci_device *pci, unsigned int reg)
Find the start of a PCI BAR.
Definition: pci.c:96
#define VMXNET3_UPT_VERSION_SELECT
UPT version that we support.
Definition: vmxnet3.h:491
All multicast.
Definition: vmxnet3.h:204
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:158
#define VMXNET3_VD_MACL
MAC Address Low.
Definition: vmxnet3.h:90
struct vmxnet3_queues queues
Queue descriptors.
Definition: vmxnet3.h:448
Profiling.
#define VMXNET3_VD_UVRS
UPT Version Report Selection.
Definition: vmxnet3.h:78
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition: netdevice.c:941
uint32_t num_desc[2]
Number of descriptors.
Definition: vmxnet3.h:360
uint32_t mode
Receive filter mode.
Definition: vmxnet3.h:188
#define cpu_to_le32(value)
Definition: byteswap.h:107
static void vmxnet3_flush_tx(struct net_device *netdev)
Flush any uncompleted transmit buffers.
Definition: vmxnet3.c:190
#define VMXNET3_MAX_PACKET_LEN
Maximum packet size.
Definition: vmxnet3.h:48
struct vmxnet3_queue_status status
Definition: vmxnet3.h:397
uint32_t high
High 32 bits of address.
Definition: myson.h:20
vmxnet3_command
Commands.
Definition: vmxnet3.h:102
struct vmxnet3_rx_filter_config rx_filter
Receive filter configuration.
Definition: vmxnet3.h:226
#define VMXNET3_VERSION_MAGIC
Driver version magic.
Definition: vmxnet3.h:170
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define VMXNET3_TXF_CQ
Transmit completion request flag.
Definition: vmxnet3.h:260
#define VMXNET3_TXF_GEN
Transmit generation flag.
Definition: vmxnet3.h:254
#define VMXNET3_IC_DISABLE_ALL
Interrupt control - disable all interrupts.
Definition: vmxnet3.h:183
PCI bus.
A PCI device.
Definition: pci.h:206
int register_netdev(struct net_device *netdev)
Register network device.
Definition: netdevice.c:759
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:155
#define VMXNET3_VD_LEN
"VD" PCI BAR size
Definition: vmxnet3.h:72
A network device.
Definition: netdevice.h:352
#define ENODEV
No such device.
Definition: errno.h:509
struct vmxnet3_queue_status status
Definition: vmxnet3.h:388
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition: netdevice.h:528
uint32_t num_comp
Number of completion descriptors.
Definition: vmxnet3.h:299
static int vmxnet3_probe(struct pci_device *pci)
Probe PCI device.
Definition: vmxnet3.c:624
#define ETH_ALEN
Definition: if_ether.h:8
static void vmxnet3_refill_rx(struct net_device *netdev)
Refill receive ring.
Definition: vmxnet3.c:208
A PCI device ID list entry.
Definition: pci.h:170
static void vmxnet3_check_link(struct net_device *netdev)
Check link state.
Definition: vmxnet3.c:335
uint64_t comp_address
Completion ring address.
Definition: vmxnet3.h:289
unsigned int uint32_t
Definition: stdint.h:12
uint32_t flags
Flags.
Definition: vmxnet3.h:336
#define VMXNET3_NUM_RX_COMP
Number of RX completion descriptors.
Definition: vmxnet3.h:430
static struct xen_remove_from_physmap * remove
Definition: xenmem.h:39
u32 version
Driver version.
Definition: ath9k_hw.c:1983
Definition: dmfe.c:137
static void vmxnet3_irq(struct net_device *netdev, int enable)
Enable/disable interrupts.
Definition: vmxnet3.c:426
struct vmxnet3_rx_queue rx
Receive queue descriptor(s)
Definition: vmxnet3.h:411
Network device operations.
Definition: netdevice.h:213
uint64_t desc_address[2]
Descriptor ring addresses.
Definition: vmxnet3.h:352
void netdev_rx(struct net_device *netdev, struct io_buffer *iobuf)
Add packet to receive queue.
Definition: netdevice.c:548
struct device * dev
Underlying hardware device.
Definition: netdevice.h:364
#define VMXNET3_RXF_GEN
Receive generation flag.
Definition: vmxnet3.h:325
static struct profiler vmxnet3_vm_command_profiler __profiler
VM command profiler.
Definition: vmxnet3.c:48
#define VMXNET3_VD_ECR
Event Cause Register.
Definition: vmxnet3.h:99
unsigned int rx_cons
Receive consumer counter.
Definition: vmxnet3.h:467
void * pt
"PT" register base address
Definition: vmxnet3.h:473
Network device management.
static void * pci_get_drvdata(struct pci_device *pci)
Get PCI driver-private data.
Definition: pci.h:369
static struct net_device_operations vmxnet3_operations
vmxnet3 net device operations
Definition: vmxnet3.c:567
Queue descriptor set.
Definition: vmxnet3.h:407
struct io_buffer * rx_iobuf[VMXNET3_NUM_RX_DESC]
Receive I/O buffers.
Definition: vmxnet3.h:484
#define iob_reserve(iobuf, len)
Definition: iobuf.h:67
struct vmxnet3_rx_desc rx_desc[VMXNET3_NUM_RX_DESC]
RX descriptor ring.
Definition: vmxnet3.h:444
unsigned int rx_prod
Receive producer counter.
Definition: vmxnet3.h:463
#define VMXNET3_TXCF_GEN
Transmit completion generation flag.
Definition: vmxnet3.h:273
void netdev_tx_complete_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Complete network transmission.
Definition: netdevice.c:470
struct vmxnet3_tx_queue tx
Transmit queue descriptor(s)
Definition: vmxnet3.h:409
struct vmxnet3_misc_config misc
Miscellaneous configuration.
Definition: vmxnet3.h:222
uint32_t len
Length.
Definition: ena.h:14
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
#define DBGC2(...)
Definition: compiler.h:522
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:594
static void vmxnet3_close(struct net_device *netdev)
Close NIC.
Definition: vmxnet3.c:556
int(* probe)(struct pci_device *pci)
Probe device.
Definition: pci.h:260
#define VMXNET3_TXF_EOP
Transmit end-of-packet flag.
Definition: vmxnet3.h:257
uint32_t index
Descriptor index.
Definition: vmxnet3.h:330
Definition: dmfe.c:143
void * data
Start of data.
Definition: iobuf.h:48
#define EIO
Input/output error.
Definition: errno.h:433
#define VMXNET3_VD_MACH
MAC Address High.
Definition: vmxnet3.h:93
struct net_device * alloc_etherdev(size_t priv_size)
Allocate Ethernet device.
Definition: ethernet.c:264
#define VMXNET3_SHARED_MAGIC
Driver shared area magic.
Definition: vmxnet3.h:243
uint8_t fill
Length pair.
Definition: deflate.h:12
uint8_t num_tx_queues
Number of TX queues.
Definition: vmxnet3.h:162
unsigned int tx_cons
Transmit completion consumer counter.
Definition: vmxnet3.h:461
void iounmap(volatile const void *io_addr)
Unmap I/O address.
VMware vmxnet3 virtual NIC driver.
struct vmxnet3_tx_comp tx_comp[VMXNET3_NUM_TX_COMP]
TX completion ring.
Definition: vmxnet3.h:442
uint32_t magic
Magic signature.
Definition: vmxnet3.h:218
static void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
Definition: malloc.h:77
static void vmxnet3_set_ll_addr(struct vmxnet3_nic *vmxnet, const void *ll_addr)
Set MAC address.
Definition: vmxnet3.c:439
#define VMXNET3_VD_DSAL
Driver Shared Address Low.
Definition: vmxnet3.h:81
static void profile_exclude(struct profiler *profiler)
Exclude time from other ongoing profiling results.
Definition: profile.h:184
uint8_t ll_addr[MAX_LL_ADDR_LEN]
Link-layer address.
Definition: netdevice.h:387
Transmit completion descriptor.
Definition: vmxnet3.h:263
struct vmxnet3_tx_desc tx_desc[VMXNET3_NUM_TX_DESC]
TX descriptor ring.
Definition: vmxnet3.h:440
void * pci_ioremap(struct pci_device *pci, unsigned long bus_addr, size_t len)
Map PCI bus address as an I/O address.
struct vmxnet3_counters count
Producer and consumer counters.
Definition: vmxnet3.h:480
#define VMXNET3_NUM_RX_DESC
Number of RX descriptors.
Definition: vmxnet3.h:427
struct vmxnet3_shared shared
Shared area.
Definition: vmxnet3.h:450
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:381
#define VMXNET3_PT_TXPROD
Transmit producer index.
Definition: vmxnet3.h:60
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define PCI_ROM(_vendor, _device, _name, _description, _data)
Definition: pci.h:303
uint32_t ecr
Event notifications.
Definition: vmxnet3.h:234
A vmxnet3 NIC.
Definition: vmxnet3.h:471
static void vmxnet3_remove(struct pci_device *pci)
Remove PCI device.
Definition: vmxnet3.c:700
uint32_t len
Length.
Definition: vmxnet3.h:334
Receive completion descriptor.
Definition: vmxnet3.h:328
uint32_t num_comp
Number of completion descriptors.
Definition: vmxnet3.h:362
#define VMXNET3_VD_CMD
Command.
Definition: vmxnet3.h:87
void * memset(void *dest, int character, size_t len) __nonnull
uint32_t version
Driver version.
Definition: vmxnet3.h:140
uint64_t comp_address
Completion ring address.
Definition: vmxnet3.h:354
A persistent I/O buffer.
Definition: iobuf.h:33
#define VMXNET3_VD_BAR
"VD" PCI BAR address
Definition: vmxnet3.h:69