summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/drivers/net/vmxnet3.c
blob: 8d4f4b84348787f18e54b6693c9ac33f32233918 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*
 * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <byteswap.h>
#include <ipxe/pci.h>
#include <ipxe/io.h>
#include <ipxe/malloc.h>
#include <ipxe/profile.h>
#include <ipxe/iobuf.h>
#include <ipxe/netdevice.h>
#include <ipxe/if_ether.h>
#include <ipxe/ethernet.h>
#include "vmxnet3.h"

/**
 * @file
 *
 * VMware vmxnet3 virtual NIC driver
 *
 */

/** VM command profiler */
static struct profiler vmxnet3_vm_command_profiler __profiler =
	{ .name = "vmxnet3.vm_command" };

/** VM transmit profiler */
static struct profiler vmxnet3_vm_tx_profiler __profiler =
	{ .name = "vmxnet3.vm_tx" };

/** VM receive refill profiler */
static struct profiler vmxnet3_vm_refill_profiler __profiler =
	{ .name = "vmxnet3.vm_refill" };

/** VM event profiler */
static struct profiler vmxnet3_vm_event_profiler __profiler =
	{ .name = "vmxnet3.vm_event" };

/**
 * Issue command
 *
 * @v vmxnet		vmxnet3 NIC
 * @v command		Command to issue
 * @ret result		Command result
 */
static inline uint32_t vmxnet3_command ( struct vmxnet3_nic *vmxnet,
					 uint32_t command ) {
	uint32_t result;

	/* Issue command */
	profile_start ( &vmxnet3_vm_command_profiler );
	writel ( command, ( vmxnet->vd + VMXNET3_VD_CMD ) );
	result = readl ( vmxnet->vd + VMXNET3_VD_CMD );
	profile_stop ( &vmxnet3_vm_command_profiler );
	profile_exclude ( &vmxnet3_vm_command_profiler );

	return result;
}

/**
 * Transmit packet
 *
 * @v netdev		Network device
 * @v iobuf		I/O buffer
 * @ret rc		Return status code
 */
static int vmxnet3_transmit ( struct net_device *netdev,
			      struct io_buffer *iobuf ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
	struct vmxnet3_tx_desc *tx_desc;
	unsigned int desc_idx;
	unsigned int generation;

	/* Check that we have a free transmit descriptor */
	desc_idx = ( vmxnet->count.tx_prod % VMXNET3_NUM_TX_DESC );
	generation = ( ( vmxnet->count.tx_prod & VMXNET3_NUM_TX_DESC ) ?
		       0 : cpu_to_le32 ( VMXNET3_TXF_GEN ) );
	if ( vmxnet->tx_iobuf[desc_idx] ) {
		DBGC ( vmxnet, "VMXNET3 %p out of transmit descriptors\n",
		       vmxnet );
		return -ENOBUFS;
	}

	/* Increment producer counter */
	vmxnet->count.tx_prod++;

	/* Store I/O buffer for later completion */
	vmxnet->tx_iobuf[desc_idx] = iobuf;

	/* Populate transmit descriptor */
	tx_desc = &vmxnet->dma->tx_desc[desc_idx];
	tx_desc->address = cpu_to_le64 ( virt_to_bus ( iobuf->data ) );
	tx_desc->flags[0] = ( generation | cpu_to_le32 ( iob_len ( iobuf ) ) );
	tx_desc->flags[1] = cpu_to_le32 ( VMXNET3_TXF_CQ | VMXNET3_TXF_EOP );

	/* Hand over descriptor to NIC */
	wmb();
	profile_start ( &vmxnet3_vm_tx_profiler );
	writel ( ( vmxnet->count.tx_prod % VMXNET3_NUM_TX_DESC ),
		 ( vmxnet->pt + VMXNET3_PT_TXPROD ) );
	profile_stop ( &vmxnet3_vm_tx_profiler );
	profile_exclude ( &vmxnet3_vm_tx_profiler );

	return 0;
}

/**
 * Poll for completed transmissions
 *
 * @v netdev		Network device
 */
static void vmxnet3_poll_tx ( struct net_device *netdev ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
	struct vmxnet3_tx_comp *tx_comp;
	struct io_buffer *iobuf;
	unsigned int comp_idx;
	unsigned int desc_idx;
	unsigned int generation;

	while ( 1 ) {

		/* Look for completed descriptors */
		comp_idx = ( vmxnet->count.tx_cons % VMXNET3_NUM_TX_COMP );
		generation = ( ( vmxnet->count.tx_cons & VMXNET3_NUM_TX_COMP ) ?
			       0 : cpu_to_le32 ( VMXNET3_TXCF_GEN ) );
		tx_comp = &vmxnet->dma->tx_comp[comp_idx];
		if ( generation != ( tx_comp->flags &
				     cpu_to_le32 ( VMXNET3_TXCF_GEN ) ) ) {
			break;
		}

		/* Increment consumer counter */
		vmxnet->count.tx_cons++;

		/* Locate corresponding transmit descriptor */
		desc_idx = ( le32_to_cpu ( tx_comp->index ) %
			     VMXNET3_NUM_TX_DESC );
		iobuf = vmxnet->tx_iobuf[desc_idx];
		if ( ! iobuf ) {
			DBGC ( vmxnet, "VMXNET3 %p completed on empty transmit "
			       "buffer %#x/%#x\n", vmxnet, comp_idx, desc_idx );
			netdev_tx_err ( netdev, NULL, -ENOTTY );
			continue;
		}

		/* Remove I/O buffer from transmit queue */
		vmxnet->tx_iobuf[desc_idx] = NULL;

		/* Report transmission completion to network layer */
		DBGC2 ( vmxnet, "VMXNET3 %p completed TX %#x/%#x (len %#zx)\n",
			vmxnet, comp_idx, desc_idx, iob_len ( iobuf ) );
		netdev_tx_complete ( netdev, iobuf );
	}
}

/**
 * Flush any uncompleted transmit buffers
 *
 * @v netdev		Network device
 */
static void vmxnet3_flush_tx ( struct net_device *netdev ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
	unsigned int i;

	for ( i = 0 ; i < VMXNET3_NUM_TX_DESC ; i++ ) {
		if ( vmxnet->tx_iobuf[i] ) {
			netdev_tx_complete_err ( netdev, vmxnet->tx_iobuf[i],
						 -ECANCELED );
			vmxnet->tx_iobuf[i] = NULL;
		}
	}
}

/**
 * Refill receive ring
 *
 * @v netdev		Network device
 */
static void vmxnet3_refill_rx ( struct net_device *netdev ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
	struct vmxnet3_rx_desc *rx_desc;
	struct io_buffer *iobuf;
	unsigned int orig_rx_prod = vmxnet->count.rx_prod;
	unsigned int desc_idx;
	unsigned int generation;

	/* Fill receive ring to specified fill level */
	while ( vmxnet->count.rx_fill < VMXNET3_RX_FILL ) {

		/* Locate receive descriptor */
		desc_idx = ( vmxnet->count.rx_prod % VMXNET3_NUM_RX_DESC );
		generation = ( ( vmxnet->count.rx_prod & VMXNET3_NUM_RX_DESC ) ?
			       0 : cpu_to_le32 ( VMXNET3_RXF_GEN ) );
		assert ( vmxnet->rx_iobuf[desc_idx] == NULL );

		/* Allocate I/O buffer */
		iobuf = alloc_iob ( VMXNET3_MTU + NET_IP_ALIGN );
		if ( ! iobuf ) {
			/* Non-fatal low memory condition */
			break;
		}
		iob_reserve ( iobuf, NET_IP_ALIGN );

		/* Increment producer counter and fill level */
		vmxnet->count.rx_prod++;
		vmxnet->count.rx_fill++;

		/* Store I/O buffer for later completion */
		vmxnet->rx_iobuf[desc_idx] = iobuf;

		/* Populate receive descriptor */
		rx_desc = &vmxnet->dma->rx_desc[desc_idx];
		rx_desc->address = cpu_to_le64 ( virt_to_bus ( iobuf->data ) );
		rx_desc->flags = ( generation | cpu_to_le32 ( VMXNET3_MTU ) );

	}

	/* Hand over any new descriptors to NIC */
	if ( vmxnet->count.rx_prod != orig_rx_prod ) {
		wmb();
		profile_start ( &vmxnet3_vm_refill_profiler );
		writel ( ( vmxnet->count.rx_prod % VMXNET3_NUM_RX_DESC ),
			 ( vmxnet->pt + VMXNET3_PT_RXPROD ) );
		profile_stop ( &vmxnet3_vm_refill_profiler );
		profile_exclude ( &vmxnet3_vm_refill_profiler );
	}
}

/**
 * Poll for received packets
 *
 * @v netdev		Network device
 */
static void vmxnet3_poll_rx ( struct net_device *netdev ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
	struct vmxnet3_rx_comp *rx_comp;
	struct io_buffer *iobuf;
	unsigned int comp_idx;
	unsigned int desc_idx;
	unsigned int generation;
	size_t len;

	while ( 1 ) {

		/* Look for completed descriptors */
		comp_idx = ( vmxnet->count.rx_cons % VMXNET3_NUM_RX_COMP );
		generation = ( ( vmxnet->count.rx_cons & VMXNET3_NUM_RX_COMP ) ?
			       0 : cpu_to_le32 ( VMXNET3_RXCF_GEN ) );
		rx_comp = &vmxnet->dma->rx_comp[comp_idx];
		if ( generation != ( rx_comp->flags &
				     cpu_to_le32 ( VMXNET3_RXCF_GEN ) ) ) {
			break;
		}

		/* Increment consumer counter */
		vmxnet->count.rx_cons++;

		/* Locate corresponding receive descriptor */
		desc_idx = ( le32_to_cpu ( rx_comp->index ) %
			     VMXNET3_NUM_RX_DESC );
		iobuf = vmxnet->rx_iobuf[desc_idx];
		if ( ! iobuf ) {
			DBGC ( vmxnet, "VMXNET3 %p completed on empty receive "
			       "buffer %#x/%#x\n", vmxnet, comp_idx, desc_idx );
			netdev_rx_err ( netdev, NULL, -ENOTTY );
			continue;
		}

		/* Remove I/O buffer from receive queue */
		vmxnet->rx_iobuf[desc_idx] = NULL;
		vmxnet->count.rx_fill--;

		/* Deliver packet to network layer */
		len = ( le32_to_cpu ( rx_comp->len ) &
			( VMXNET3_MAX_PACKET_LEN - 1 ) );
		DBGC2 ( vmxnet, "VMXNET3 %p completed RX %#x/%#x (len %#zx)\n",
			vmxnet, comp_idx, desc_idx, len );
		iob_put ( iobuf, len );
		netdev_rx ( netdev, iobuf );
	}
}

/**
 * Flush any uncompleted receive buffers
 *
 * @v netdev		Network device
 */
static void vmxnet3_flush_rx ( struct net_device *netdev ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
	struct io_buffer *iobuf;
	unsigned int i;

	for ( i = 0 ; i < VMXNET3_NUM_RX_DESC ; i++ ) {
		if ( ( iobuf = vmxnet->rx_iobuf[i] ) != NULL ) {
			netdev_rx_err ( netdev, iobuf, -ECANCELED );
			vmxnet->rx_iobuf[i] = NULL;
		}
	}
}

/**
 * Check link state
 *
 * @v netdev		Network device
 */
static void vmxnet3_check_link ( struct net_device *netdev ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
	uint32_t state;
	int link_up;
	unsigned int link_speed;

	/* Get link state */
	state = vmxnet3_command ( vmxnet, VMXNET3_CMD_GET_LINK );
	link_up = ( state & 1 );
	link_speed = ( state >> 16 );

	/* Report link state to network device */
	if ( link_up ) {
		DBGC ( vmxnet, "VMXNET3 %p link is up at %d Mbps\n",
		       vmxnet, link_speed );
		netdev_link_up ( netdev );
	} else {
		DBGC ( vmxnet, "VMXNET3 %p link is down\n", vmxnet );
		netdev_link_down ( netdev );
	}
}

/**
 * Poll for events
 *
 * @v netdev		Network device
 */
static void vmxnet3_poll_events ( struct net_device *netdev ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
	uint32_t events;

	/* Do nothing unless there are events to process */
	if ( ! vmxnet->dma->shared.ecr )
		return;
	events = le32_to_cpu ( vmxnet->dma->shared.ecr );

	/* Acknowledge these events */
	profile_start ( &vmxnet3_vm_event_profiler );
	writel ( events, ( vmxnet->vd + VMXNET3_VD_ECR ) );
	profile_stop ( &vmxnet3_vm_event_profiler );
	profile_exclude ( &vmxnet3_vm_event_profiler );

	/* Check for link state change */
	if ( events & VMXNET3_ECR_LINK ) {
		vmxnet3_check_link ( netdev );
		events &= ~VMXNET3_ECR_LINK;
	}

	/* Check for queue errors */
	if ( events & ( VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR ) ) {
		vmxnet3_command ( vmxnet, VMXNET3_CMD_GET_QUEUE_STATUS );
		DBGC ( vmxnet, "VMXNET3 %p queue error status (TX %08x, RX "
		       "%08x)\n", vmxnet,
		       le32_to_cpu ( vmxnet->dma->queues.tx.status.error ),
		       le32_to_cpu ( vmxnet->dma->queues.rx.status.error ) );
		/* Report errors to allow for visibility via "ifstat" */
		if ( events & VMXNET3_ECR_TQERR )
			netdev_tx_err ( netdev, NULL, -EPIPE );
		if ( events & VMXNET3_ECR_RQERR )
			netdev_rx_err ( netdev, NULL, -EPIPE );
		events &= ~( VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR );
	}

	/* Check for unknown events */
	if ( events ) {
		DBGC ( vmxnet, "VMXNET3 %p unknown events %08x\n",
		       vmxnet, events );
		/* Report error to allow for visibility via "ifstat" */
		netdev_rx_err ( netdev, NULL, -ENODEV );
	}
}

/**
 * Poll network device
 *
 * @v netdev		Network device
 */
static void vmxnet3_poll ( struct net_device *netdev ) {

	vmxnet3_poll_events ( netdev );
	vmxnet3_poll_tx ( netdev );
	vmxnet3_poll_rx ( netdev );
	vmxnet3_refill_rx ( netdev );
}

/**
 * Enable/disable interrupts
 *
 * @v netdev		Network device
 * @v enable		Interrupts should be enabled
 */
static void vmxnet3_irq ( struct net_device *netdev, int enable ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );

	DBGC ( vmxnet, "VMXNET3 %p %s IRQ not implemented\n",
	       vmxnet, ( enable ? "enable" : "disable" ) );
}

/**
 * Set MAC address
 *
 * @v vmxnet		vmxnet3 NIC
 * @v ll_addr		Link-layer address to set
 */
static void vmxnet3_set_ll_addr ( struct vmxnet3_nic *vmxnet,
				  const void *ll_addr ) {
	struct {
		uint32_t low;
		uint32_t high;
	} __attribute__ (( packed )) mac;

	memset ( &mac, 0, sizeof ( mac ) );
	memcpy ( &mac, ll_addr, ETH_ALEN );
	writel ( cpu_to_le32 ( mac.low ), ( vmxnet->vd + VMXNET3_VD_MACL ) );
	writel ( cpu_to_le32 ( mac.high ), ( vmxnet->vd + VMXNET3_VD_MACH ) );
}

/**
 * Open NIC
 *
 * @v netdev		Network device
 * @ret rc		Return status code
 */
static int vmxnet3_open ( struct net_device *netdev ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );
	struct vmxnet3_shared *shared;
	struct vmxnet3_queues *queues;
	uint64_t shared_bus;
	uint64_t queues_bus;
	uint32_t status;
	int rc;

	/* Allocate DMA areas */
	vmxnet->dma = malloc_dma ( sizeof ( *vmxnet->dma ), VMXNET3_DMA_ALIGN );
	if ( ! vmxnet->dma ) {
		DBGC ( vmxnet, "VMXNET3 %p could not allocate DMA area\n",
		       vmxnet );
		rc = -ENOMEM;
		goto err_alloc_dma;
	}
	memset ( vmxnet->dma, 0, sizeof ( *vmxnet->dma ) );

	/* Populate queue descriptors */
	queues = &vmxnet->dma->queues;
	queues->tx.cfg.desc_address =
		cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->tx_desc ) );
	queues->tx.cfg.comp_address =
		cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->tx_comp ) );
	queues->tx.cfg.num_desc = cpu_to_le32 ( VMXNET3_NUM_TX_DESC );
	queues->tx.cfg.num_comp = cpu_to_le32 ( VMXNET3_NUM_TX_COMP );
	queues->rx.cfg.desc_address[0] =
		cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->rx_desc ) );
	queues->rx.cfg.comp_address =
		cpu_to_le64 ( virt_to_bus ( &vmxnet->dma->rx_comp ) );
	queues->rx.cfg.num_desc[0] = cpu_to_le32 ( VMXNET3_NUM_RX_DESC );
	queues->rx.cfg.num_comp = cpu_to_le32 ( VMXNET3_NUM_RX_COMP );
	queues_bus = virt_to_bus ( queues );
	DBGC ( vmxnet, "VMXNET3 %p queue descriptors at %08llx+%zx\n",
	       vmxnet, queues_bus, sizeof ( *queues ) );

	/* Populate shared area */
	shared = &vmxnet->dma->shared;
	shared->magic = cpu_to_le32 ( VMXNET3_SHARED_MAGIC );
	shared->misc.version = cpu_to_le32 ( VMXNET3_VERSION_MAGIC );
	shared->misc.version_support = cpu_to_le32 ( VMXNET3_VERSION_SELECT );
	shared->misc.upt_version_support =
		cpu_to_le32 ( VMXNET3_UPT_VERSION_SELECT );
	shared->misc.queue_desc_address = cpu_to_le64 ( queues_bus );
	shared->misc.queue_desc_len = cpu_to_le32 ( sizeof ( *queues ) );
	shared->misc.mtu = cpu_to_le32 ( VMXNET3_MTU );
	shared->misc.num_tx_queues = 1;
	shared->misc.num_rx_queues = 1;
	shared->interrupt.num_intrs = 1;
	shared->interrupt.control = cpu_to_le32 ( VMXNET3_IC_DISABLE_ALL );
	shared->rx_filter.mode = cpu_to_le32 ( VMXNET3_RXM_UCAST |
					       VMXNET3_RXM_BCAST |
					       VMXNET3_RXM_ALL_MULTI );
	shared_bus = virt_to_bus ( shared );
	DBGC ( vmxnet, "VMXNET3 %p shared area at %08llx+%zx\n",
	       vmxnet, shared_bus, sizeof ( *shared ) );

	/* Zero counters */
	memset ( &vmxnet->count, 0, sizeof ( vmxnet->count ) );

	/* Set MAC address */
	vmxnet3_set_ll_addr ( vmxnet, &netdev->ll_addr );

	/* Pass shared area to device */
	writel ( ( shared_bus >> 0 ), ( vmxnet->vd + VMXNET3_VD_DSAL ) );
	writel ( ( shared_bus >> 32 ), ( vmxnet->vd + VMXNET3_VD_DSAH ) );

	/* Activate device */
	if ( ( status = vmxnet3_command ( vmxnet,
					  VMXNET3_CMD_ACTIVATE_DEV ) ) != 0 ) {
		DBGC ( vmxnet, "VMXNET3 %p could not activate (status %#x)\n",
		       vmxnet, status );
		rc = -EIO;
		goto err_activate;
	}

	/* Fill receive ring */
	vmxnet3_refill_rx ( netdev );

	return 0;

	vmxnet3_command ( vmxnet, VMXNET3_CMD_QUIESCE_DEV );
	vmxnet3_command ( vmxnet, VMXNET3_CMD_RESET_DEV );
 err_activate:
	vmxnet3_flush_tx ( netdev );
	vmxnet3_flush_rx ( netdev );
	free_dma ( vmxnet->dma, sizeof ( *vmxnet->dma ) );
 err_alloc_dma:
	return rc;
}

/**
 * Close NIC
 *
 * @v netdev		Network device
 */
static void vmxnet3_close ( struct net_device *netdev ) {
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );

	vmxnet3_command ( vmxnet, VMXNET3_CMD_QUIESCE_DEV );
	vmxnet3_command ( vmxnet, VMXNET3_CMD_RESET_DEV );
	vmxnet3_flush_tx ( netdev );
	vmxnet3_flush_rx ( netdev );
	free_dma ( vmxnet->dma, sizeof ( *vmxnet->dma ) );
}

/** vmxnet3 net device operations */
static struct net_device_operations vmxnet3_operations = {
	.open		= vmxnet3_open,
	.close		= vmxnet3_close,
	.transmit	= vmxnet3_transmit,
	.poll		= vmxnet3_poll,
	.irq		= vmxnet3_irq,
};

/**
 * Check version
 *
 * @v vmxnet		vmxnet3 NIC
 * @ret rc		Return status code
 */
static int vmxnet3_check_version ( struct vmxnet3_nic *vmxnet ) {
	uint32_t version;
	uint32_t upt_version;

	/* Read version */
	version = readl ( vmxnet->vd + VMXNET3_VD_VRRS );
	upt_version = readl ( vmxnet->vd + VMXNET3_VD_UVRS );
	DBGC ( vmxnet, "VMXNET3 %p is version %d (UPT version %d)\n",
	       vmxnet, version, upt_version );

	/* Inform NIC of driver version */
	writel ( VMXNET3_VERSION_SELECT, ( vmxnet->vd + VMXNET3_VD_VRRS ) );
	writel ( VMXNET3_UPT_VERSION_SELECT, ( vmxnet->vd + VMXNET3_VD_UVRS ) );

	return 0;
}

/**
 * Get permanent MAC address
 *
 * @v vmxnet		vmxnet3 NIC
 * @v hw_addr		Hardware address to fill in
 */
static void vmxnet3_get_hw_addr ( struct vmxnet3_nic *vmxnet, void *hw_addr ) {
	struct {
		uint32_t low;
		uint32_t high;
	} __attribute__ (( packed )) mac;

	mac.low = le32_to_cpu ( vmxnet3_command ( vmxnet,
					       VMXNET3_CMD_GET_PERM_MAC_LO ) );
	mac.high = le32_to_cpu ( vmxnet3_command ( vmxnet,
					       VMXNET3_CMD_GET_PERM_MAC_HI ) );
	memcpy ( hw_addr, &mac, ETH_ALEN );
}

/**
 * Probe PCI device
 *
 * @v pci		PCI device
 * @v id		PCI ID
 * @ret rc		Return status code
 */
static int vmxnet3_probe ( struct pci_device *pci ) {
	struct net_device *netdev;
	struct vmxnet3_nic *vmxnet;
	int rc;

	/* Allocate network device */
	netdev = alloc_etherdev ( sizeof ( *vmxnet ) );
	if ( ! netdev ) {
		rc = -ENOMEM;
		goto err_alloc_etherdev;
	}
	netdev_init ( netdev, &vmxnet3_operations );
	vmxnet = netdev_priv ( netdev );
	pci_set_drvdata ( pci, netdev );
	netdev->dev = &pci->dev;
	memset ( vmxnet, 0, sizeof ( *vmxnet ) );

	/* Fix up PCI device */
	adjust_pci_device ( pci );

	/* Map PCI BARs */
	vmxnet->pt = ioremap ( pci_bar_start ( pci, VMXNET3_PT_BAR ),
			       VMXNET3_PT_LEN );
	if ( ! vmxnet->pt ) {
		rc = -ENODEV;
		goto err_ioremap_pt;
	}
	vmxnet->vd = ioremap ( pci_bar_start ( pci, VMXNET3_VD_BAR ),
			       VMXNET3_VD_LEN );
	if ( ! vmxnet->vd ) {
		rc = -ENODEV;
		goto err_ioremap_vd;
	}

	/* Version check */
	if ( ( rc = vmxnet3_check_version ( vmxnet ) ) != 0 )
		goto err_check_version;

	/* Reset device */
	if ( ( rc = vmxnet3_command ( vmxnet, VMXNET3_CMD_RESET_DEV ) ) != 0 )
		goto err_reset;

	/* Read initial MAC address */
	vmxnet3_get_hw_addr ( vmxnet, &netdev->hw_addr );

	/* Register network device */
	if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
		DBGC ( vmxnet, "VMXNET3 %p could not register net device: "
		       "%s\n", vmxnet, strerror ( rc ) );
		goto err_register_netdev;
	}

	/* Get initial link state */
	vmxnet3_check_link ( netdev );

	return 0;

	unregister_netdev ( netdev );
 err_register_netdev:
 err_reset:
 err_check_version:
	iounmap ( vmxnet->vd );
 err_ioremap_vd:
	iounmap ( vmxnet->pt );
 err_ioremap_pt:
	netdev_nullify ( netdev );
	netdev_put ( netdev );
 err_alloc_etherdev:
	return rc;
}

/**
 * Remove PCI device
 *
 * @v pci		PCI device
 */
static void vmxnet3_remove ( struct pci_device *pci ) {
	struct net_device *netdev = pci_get_drvdata ( pci );
	struct vmxnet3_nic *vmxnet = netdev_priv ( netdev );

	unregister_netdev ( netdev );
	iounmap ( vmxnet->vd );
	iounmap ( vmxnet->pt );
	netdev_nullify ( netdev );
	netdev_put ( netdev );
}

/** vmxnet3 PCI IDs */
static struct pci_device_id vmxnet3_nics[] = {
	PCI_ROM ( 0x15ad, 0x07b0, "vmxnet3", "vmxnet3 virtual NIC", 0 ),
};

/** vmxnet3 PCI driver */
struct pci_driver vmxnet3_driver __pci_driver = {
	.ids = vmxnet3_nics,
	.id_count = ( sizeof ( vmxnet3_nics ) / sizeof ( vmxnet3_nics[0] ) ),
	.probe = vmxnet3_probe,
	.remove = vmxnet3_remove,
};