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