summaryrefslogtreecommitdiffstats
path: root/src/drivers/net/exanic.c
blob: 287e14e8d887b65ffb3594ee0eeea178f491fc69 (plain) (blame)
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
/*
 * Copyright (C) 2017 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 (at your option) 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 <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/netdevice.h>
#include <ipxe/ethernet.h>
#include <ipxe/if_ether.h>
#include <ipxe/iobuf.h>
#include <ipxe/malloc.h>
#include <ipxe/umalloc.h>
#include <ipxe/pci.h>
#include "exanic.h"

/** @file
 *
 * Exablaze ExaNIC driver
 *
 */

/* Disambiguate the various error causes */
#define EIO_ABORTED __einfo_error ( EINFO_EIO_ABORTED )
#define EINFO_EIO_ABORTED \
	__einfo_uniqify ( EINFO_EIO, 0x01, "Frame aborted" )
#define EIO_CORRUPT __einfo_error ( EINFO_EIO_CORRUPT )
#define EINFO_EIO_CORRUPT \
	__einfo_uniqify ( EINFO_EIO, 0x02, "CRC incorrect" )
#define EIO_HWOVFL __einfo_error ( EINFO_EIO_HWOVFL )
#define EINFO_EIO_HWOVFL \
	__einfo_uniqify ( EINFO_EIO, 0x03, "Hardware overflow" )
#define EIO_STATUS( status ) \
	EUNIQ ( EINFO_EIO, ( (status) & EXANIC_STATUS_ERROR_MASK ), \
		EIO_ABORTED, EIO_CORRUPT, EIO_HWOVFL )

/**
 * Write DMA base address register
 *
 * @v addr		DMA base address
 * @v reg		Register
 */
static void exanic_write_base ( physaddr_t addr, void *reg ) {
	uint32_t lo;
	uint32_t hi;

	/* Write high and low registers, setting flags as appropriate */
	lo = addr;
	if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) {
		/* 64-bit build; may be a 32-bit or 64-bit address */
		hi = ( ( ( uint64_t ) addr ) >> 32 );
		if ( ! hi )
			lo |= EXANIC_DMA_32_BIT;
	} else {
		/* 32-bit build; always a 32-bit address */
		hi = 0;
		lo |= EXANIC_DMA_32_BIT;
	}
	writel ( hi, ( reg + 0 ) );
	writel ( lo, ( reg + 4 ) );
}

/**
 * Clear DMA base address register
 *
 * @v reg		Register
 */
static inline void exanic_clear_base ( void *reg ) {

	/* Clear both high and low registers */
	writel ( 0, ( reg + 0 ) );
	writel ( 0, ( reg + 4 ) );
}

/******************************************************************************
 *
 * Device reset
 *
 ******************************************************************************
 */

/**
 * Reset hardware
 *
 * @v exanic		ExaNIC device
 */
static void exanic_reset ( struct exanic *exanic ) {
	void *port_regs;
	unsigned int i;

	/* Disable all possible ports */
	for ( i = 0 ; i < EXANIC_MAX_PORTS ; i++ ) {
		port_regs = ( exanic->regs + EXANIC_PORT_REGS ( i ) );
		writel ( 0, ( port_regs + EXANIC_PORT_ENABLE ) );
		writel ( 0, ( port_regs + EXANIC_PORT_IRQ ) );
		exanic_clear_base ( port_regs + EXANIC_PORT_RX_BASE );
	}

	/* Disable transmit feedback */
	exanic_clear_base ( exanic->regs + EXANIC_TXF_BASE );
}

/******************************************************************************
 *
 * MAC address
 *
 ******************************************************************************
 */

/**
 * Read I2C line status
 *
 * @v basher		Bit-bashing interface
 * @v bit_id		Bit number
 * @ret zero		Input is a logic 0
 * @ret non-zero	Input is a logic 1
 */
static int exanic_i2c_read_bit ( struct bit_basher *basher,
				 unsigned int bit_id ) {
	struct exanic *exanic =
		container_of ( basher, struct exanic, basher.basher );
	unsigned int shift;
	uint32_t i2c;

	/* Identify bit */
	assert ( bit_id == I2C_BIT_SDA );
	shift = exanic->i2cfg.getsda;

	/* Read I2C register */
	DBG_DISABLE ( DBGLVL_IO );
	i2c = readl ( exanic->regs + EXANIC_I2C );
	DBG_ENABLE ( DBGLVL_IO );
	return ( ( i2c >> shift ) & 1 );
}

/**
 * Write I2C line status
 *
 * @v basher		Bit-bashing interface
 * @v bit_id		Bit number
 * @v data		Value to write
 */
static void exanic_i2c_write_bit ( struct bit_basher *basher,
				   unsigned int bit_id, unsigned long data ) {
	struct exanic *exanic =
		container_of ( basher, struct exanic, basher.basher );
	unsigned int shift;
	uint32_t mask;
	uint32_t i2c;

	/* Identify shift */
	assert ( ( bit_id == I2C_BIT_SCL ) || ( bit_id == I2C_BIT_SDA ) );
	shift = ( ( bit_id == I2C_BIT_SCL ) ?
		  exanic->i2cfg.setscl : exanic->i2cfg.setsda );
	mask = ( 1UL << shift );

	/* Modify I2C register */
	DBG_DISABLE ( DBGLVL_IO );
	i2c = readl ( exanic->regs + EXANIC_I2C );
	i2c &= ~mask;
	if ( ! data )
		i2c |= mask;
	writel ( i2c, ( exanic->regs + EXANIC_I2C ) );
	DBG_ENABLE ( DBGLVL_IO );
}

/** I2C bit-bashing interface operations */
static struct bit_basher_operations exanic_i2c_basher_ops = {
	.read = exanic_i2c_read_bit,
	.write = exanic_i2c_write_bit,
};

/** Possible I2C bus configurations */
static struct exanic_i2c_config exanic_i2cfgs[] = {
	/* X2/X10 */
	{ .setscl = 7, .setsda = 4, .getsda = 12 },
	/* X4 */
	{ .setscl = 7, .setsda = 5, .getsda = 13 },
};

/**
 * Initialise EEPROM
 *
 * @v exanic		ExaNIC device
 * @v i2cfg		I2C bus configuration
 * @ret rc		Return status code
 */
static int exanic_try_init_eeprom ( struct exanic *exanic,
				    struct exanic_i2c_config *i2cfg ) {
	int rc;

	/* Configure I2C bus */
	memcpy ( &exanic->i2cfg, i2cfg, sizeof ( exanic->i2cfg ) );

	/* Initialise I2C bus */
	if ( ( rc = init_i2c_bit_basher ( &exanic->basher,
					  &exanic_i2c_basher_ops ) ) != 0 ) {
		DBGC2 ( exanic, "EXANIC %p found no I2C bus via %d/%d/%d\n",
			exanic, exanic->i2cfg.setscl,
			exanic->i2cfg.setsda, exanic->i2cfg.getsda );
		return rc;
	}

	/* Check for EEPROM presence */
	init_i2c_eeprom ( &exanic->eeprom, EXANIC_EEPROM_ADDRESS );
	if ( ( rc = i2c_check_presence ( &exanic->basher.i2c,
					 &exanic->eeprom ) ) != 0 ) {
		DBGC2 ( exanic, "EXANIC %p found no EEPROM via %d/%d/%d\n",
			exanic, exanic->i2cfg.setscl,
			exanic->i2cfg.setsda, exanic->i2cfg.getsda );
		return rc;
	}

	DBGC ( exanic, "EXANIC %p found EEPROM via %d/%d/%d\n",
	       exanic, exanic->i2cfg.setscl,
	       exanic->i2cfg.setsda, exanic->i2cfg.getsda );
	return 0;
}

/**
 * Initialise EEPROM
 *
 * @v exanic		ExaNIC device
 * @ret rc		Return status code
 */
static int exanic_init_eeprom ( struct exanic *exanic ) {
	struct exanic_i2c_config *i2cfg;
	unsigned int i;
	int rc;

	/* Try all possible bus configurations */
	for ( i = 0 ; i < ( sizeof ( exanic_i2cfgs ) /
			    sizeof ( exanic_i2cfgs[0] ) ) ; i++ ) {
		i2cfg = &exanic_i2cfgs[i];
		if ( ( rc = exanic_try_init_eeprom ( exanic, i2cfg ) ) == 0 )
			return 0;
	}

	DBGC ( exanic, "EXANIC %p found no EEPROM\n", exanic );
	return -ENODEV;
}

/**
 * Fetch base MAC address
 *
 * @v exanic		ExaNIC device
 * @ret rc		Return status code
 */
static int exanic_fetch_mac ( struct exanic *exanic ) {
	struct i2c_interface *i2c = &exanic->basher.i2c;
	int rc;

	/* Initialise EEPROM */
	if ( ( rc = exanic_init_eeprom ( exanic ) ) != 0 )
		return rc;

	/* Fetch base MAC address */
	if ( ( rc = i2c->read ( i2c, &exanic->eeprom, 0, exanic->mac,
				sizeof ( exanic->mac ) ) ) != 0 ) {
		DBGC ( exanic, "EXANIC %p could not read MAC address: %s\n",
		       exanic, strerror ( rc ) );
		return rc;
	}

	return 0;
}

/******************************************************************************
 *
 * Link state
 *
 ******************************************************************************
 */

/**
 * Check link state
 *
 * @v netdev		Network device
 */
static void exanic_check_link ( struct net_device *netdev ) {
	struct exanic_port *port = netdev->priv;
	uint32_t status;
	uint32_t speed;

	/* Report port status changes */
	status = readl ( port->regs + EXANIC_PORT_STATUS );
	speed = readl ( port->regs + EXANIC_PORT_SPEED );
	if ( status != port->status ) {
		DBGC ( port, "EXANIC %s port status %#08x speed %dMbps\n",
		       netdev->name, status, speed );
		if ( status & EXANIC_PORT_STATUS_LINK ) {
			netdev_link_up ( netdev );
		} else {
			netdev_link_down ( netdev );
		}
		port->status = status;
	}
}

/**
 * Check link state periodically
 *
 * @v retry		Link state check timer
 * @v over		Failure indicator
 */
static void exanic_expired ( struct retry_timer *timer, int over __unused ) {
	struct exanic_port *port =
		container_of ( timer, struct exanic_port, timer );
	struct net_device *netdev = port->netdev;
	static const uint32_t speeds[] = {
		100, 1000, 10000, 40000, 100000,
	};
	unsigned int index;

	/* Restart timer */
	start_timer_fixed ( timer, EXANIC_LINK_INTERVAL );

	/* Check link state */
	exanic_check_link ( netdev );

	/* Do nothing further if link is already up */
	if ( netdev_link_ok ( netdev ) )
		return;

	/* Do nothing further unless we have a valid list of supported speeds */
	if ( ! port->speeds )
		return;

	/* Autonegotiation is not supported; try manually selecting
	 * the next supported link speed.
	 */
	do {
		if ( ! port->speed )
			port->speed = ( 8 * sizeof ( port->speeds ) );
		port->speed--;
	} while ( ! ( ( 1UL << port->speed ) & port->speeds ) );
	index = ( port->speed - ( ffs ( EXANIC_CAPS_SPEED_MASK ) - 1 ) );
	assert ( index < ( sizeof ( speeds ) / sizeof ( speeds[0] ) ) );

	/* Attempt the selected speed */
	DBGC ( netdev, "EXANIC %s attempting %dMbps\n",
	       netdev->name, speeds[index] );
	writel ( speeds[index], ( port->regs + EXANIC_PORT_SPEED ) );
}

/******************************************************************************
 *
 * Network device interface
 *
 ******************************************************************************
 */

/**
 * Open network device
 *
 * @v netdev		Network device
 * @ret rc		Return status code
 */
static int exanic_open ( struct net_device *netdev ) {
	struct exanic_port *port = netdev->priv;
	struct exanic_tx_chunk *tx;
	unsigned int i;

	/* Reset transmit region contents */
	for ( i = 0 ; i < port->tx_count ; i++ ) {
		tx = ( port->tx + ( i * sizeof ( *tx ) ) );
		writew ( port->txf_slot, &tx->desc.txf_slot );
		writeb ( EXANIC_TYPE_RAW, &tx->desc.type );
		writeb ( 0, &tx->desc.flags );
		writew ( 0, &tx->pad );
	}

	/* Reset receive region contents */
	memset_user ( port->rx, 0, 0xff, EXANIC_RX_LEN );

	/* Reset transmit feedback region */
	*(port->txf) = 0;

	/* Reset counters */
	port->tx_prod = 0;
	port->tx_cons = 0;
	port->rx_cons = 0;

	/* Map receive region */
	exanic_write_base ( phys_to_bus ( user_to_phys ( port->rx, 0 ) ),
			    ( port->regs + EXANIC_PORT_RX_BASE ) );

	/* Enable promiscuous mode */
	writel ( EXANIC_PORT_FLAGS_PROMISC,
		 ( port->regs + EXANIC_PORT_FLAGS ) );

	/* Reset to default speed and clear cached status */
	writel ( port->default_speed, ( port->regs + EXANIC_PORT_SPEED ) );
	port->speed = 0;
	port->status = 0;

	/* Enable port */
	wmb();
	writel ( EXANIC_PORT_ENABLE_ENABLED,
		 ( port->regs + EXANIC_PORT_ENABLE ) );

	/* Start link state timer */
	start_timer_fixed ( &port->timer, EXANIC_LINK_INTERVAL );

	return 0;
}

/**
 * Close network device
 *
 * @v netdev		Network device
 */
static void exanic_close ( struct net_device *netdev ) {
	struct exanic_port *port = netdev->priv;

	/* Stop link state timer */
	stop_timer ( &port->timer );

	/* Disable port */
	writel ( 0, ( port->regs + EXANIC_PORT_ENABLE ) );
	wmb();

	/* Clear receive region */
	exanic_clear_base ( port->regs + EXANIC_PORT_RX_BASE );

	/* Discard any in-progress receive */
	if ( port->rx_iobuf ) {
		netdev_rx_err ( netdev, port->rx_iobuf, -ECANCELED );
		port->rx_iobuf = NULL;
	}
}

/**
 * Transmit packet
 *
 * @v netdev		Network device
 * @v iobuf		I/O buffer
 * @ret rc		Return status code
 */
static int exanic_transmit ( struct net_device *netdev,
			     struct io_buffer *iobuf ) {
	struct exanic_port *port = netdev->priv;
	struct exanic_tx_chunk *tx;
	unsigned int tx_fill;
	unsigned int tx_index;
	size_t offset;
	size_t len;
	uint8_t *src;
	uint8_t *dst;

	/* Sanity check */
	len = iob_len ( iobuf );
	if ( len > sizeof ( tx->data ) ) {
		DBGC ( port, "EXANIC %s transmit too large\n", netdev->name );
		return -ENOTSUP;
	}

	/* Get next transmit descriptor */
	tx_fill = ( port->tx_prod - port->tx_cons );
	if ( tx_fill >= port->tx_count ) {
		DBGC ( port, "EXANIC %s out of transmit descriptors\n",
		       netdev->name );
		return -ENOBUFS;
	}
	tx_index = ( port->tx_prod & ( port->tx_count - 1 ) );
	offset = ( tx_index * sizeof ( *tx ) );
	tx = ( port->tx + offset );
	DBGC2 ( port, "EXANIC %s TX %04x at [%05zx,%05zx)\n",
		netdev->name, port->tx_prod, ( port->tx_offset + offset ),
		( port->tx_offset + offset +
		  offsetof ( typeof ( *tx ), data ) + len ) );
	port->tx_prod++;

	/* Populate transmit descriptor */
	writew ( port->tx_prod, &tx->desc.txf_id );
	writew ( ( sizeof ( tx->pad ) + len ), &tx->desc.len );

	/* Copy data to transmit region.  There is no DMA on the
	 * transmit data path.
	 */
	src = iobuf->data;
	dst = tx->data;
	while ( len-- )
		writeb ( *(src++), dst++ );

	/* Send transmit command */
	wmb();
	writel ( ( port->tx_offset + offset ),
		 ( port->regs + EXANIC_PORT_TX_COMMAND ) );

	return 0;
}

/**
 * Poll for completed packets
 *
 * @v netdev		Network device
 */
static void exanic_poll_tx ( struct net_device *netdev ) {
	struct exanic_port *port = netdev->priv;

	/* Report any completed packets */
	while ( port->tx_cons != *(port->txf) ) {
		DBGC2 ( port, "EXANIC %s TX %04x complete\n",
			netdev->name, port->tx_cons );
		netdev_tx_complete_next ( netdev );
		port->tx_cons++;
	}
}

/**
 * Poll for received packets
 *
 * @v netdev		Network device
 */
static void exanic_poll_rx ( struct net_device *netdev ) {
	struct exanic_port *port = netdev->priv;
	struct exanic_rx_chunk *rx;
	struct exanic_rx_descriptor desc;
	uint8_t current;
	uint8_t previous;
	size_t offset;
	size_t len;

	for ( ; ; port->rx_cons++ ) {

		/* Fetch descriptor */
		offset = ( ( port->rx_cons * sizeof ( *rx ) ) % EXANIC_RX_LEN );
		copy_from_user ( &desc, port->rx,
				 ( offset + offsetof ( typeof ( *rx ), desc ) ),
				 sizeof ( desc ) );

		/* Calculate generation */
		current = ( port->rx_cons / ( EXANIC_RX_LEN / sizeof ( *rx ) ));
		previous = ( current - 1 );

		/* Do nothing if no chunk is ready */
		if ( desc.generation == previous )
			break;

		/* Allocate I/O buffer if needed */
		if ( ! port->rx_iobuf ) {
			port->rx_iobuf = alloc_iob ( EXANIC_MAX_RX_LEN );
			if ( ! port->rx_iobuf ) {
				/* Wait for next poll */
				break;
			}
			port->rx_rc = 0;
		}

		/* Calculate chunk length */
		len = ( desc.len ? desc.len : sizeof ( rx->data ) );

		/* Append data to I/O buffer */
		if ( len <= iob_tailroom ( port->rx_iobuf ) ) {
			copy_from_user ( iob_put ( port->rx_iobuf, len ),
					 port->rx,
					 ( offset + offsetof ( typeof ( *rx ),
							       data ) ), len );
		} else {
			DBGC ( port, "EXANIC %s RX too large\n",
			       netdev->name );
			port->rx_rc = -ERANGE;
		}

		/* Check for overrun */
		rmb();
		copy_from_user ( &desc.generation, port->rx,
				 ( offset + offsetof ( typeof ( *rx ),
						       desc.generation ) ),
				 sizeof ( desc.generation ) );
		if ( desc.generation != current ) {
			DBGC ( port, "EXANIC %s RX overrun\n", netdev->name );
			port->rx_rc = -ENOBUFS;
			continue;
		}

		/* Wait for end of packet */
		if ( ! desc.len )
			continue;

		/* Check for receive errors */
		if ( desc.status & EXANIC_STATUS_ERROR_MASK ) {
			port->rx_rc = -EIO_STATUS ( desc.status );
			DBGC ( port, "EXANIC %s RX %04x error: %s\n",
			       netdev->name, port->rx_cons,
			       strerror ( port->rx_rc ) );
		} else {
			DBGC2 ( port, "EXANIC %s RX %04x\n",
				netdev->name, port->rx_cons );
		}

		/* Hand off to network stack */
		if ( port->rx_rc ) {
			netdev_rx_err ( netdev, port->rx_iobuf, port->rx_rc );
		} else {
			iob_unput ( port->rx_iobuf, 4 /* strip CRC */ );
			netdev_rx ( netdev, port->rx_iobuf );
		}
		port->rx_iobuf = NULL;
	}
}

/**
 * Poll for completed and received packets
 *
 * @v netdev		Network device
 */
static void exanic_poll ( struct net_device *netdev ) {

	/* Poll for completed packets */
	exanic_poll_tx ( netdev );

	/* Poll for received packets */
	exanic_poll_rx ( netdev );
}

/** ExaNIC network device operations */
static struct net_device_operations exanic_operations = {
	.open		= exanic_open,
	.close		= exanic_close,
	.transmit	= exanic_transmit,
	.poll		= exanic_poll,
};

/******************************************************************************
 *
 * PCI interface
 *
 ******************************************************************************
 */

/**
 * Probe port
 *
 * @v exanic		ExaNIC device
 * @v dev		Parent device
 * @v index		Port number
 * @ret rc		Return status code
 */
static int exanic_probe_port ( struct exanic *exanic, struct device *dev,
			       unsigned int index ) {
	struct net_device *netdev;
	struct exanic_port *port;
	void *port_regs;
	uint32_t status;
	size_t tx_len;
	int rc;

	/* Do nothing if port is not physically present */
	port_regs = ( exanic->regs + EXANIC_PORT_REGS ( index ) );
	status = readl ( port_regs + EXANIC_PORT_STATUS );
	tx_len = readl ( port_regs + EXANIC_PORT_TX_LEN );
	if ( ( status & EXANIC_PORT_STATUS_ABSENT ) || ( tx_len == 0 ) ) {
		rc = 0;
		goto absent;
	}

	/* Allocate network device */
	netdev = alloc_etherdev ( sizeof ( *port ) );
	if ( ! netdev ) {
		rc = -ENOMEM;
		goto err_alloc_netdev;
	}
	netdev_init ( netdev, &exanic_operations );
	netdev->dev = dev;
	port = netdev->priv;
	memset ( port, 0, sizeof ( *port ) );
	exanic->port[index] = port;
	port->netdev = netdev;
	port->regs = port_regs;
	timer_init ( &port->timer, exanic_expired, &netdev->refcnt );

	/* Identify transmit region */
	port->tx_offset = readl ( port->regs + EXANIC_PORT_TX_OFFSET );
	if ( tx_len > EXANIC_MAX_TX_LEN )
		tx_len = EXANIC_MAX_TX_LEN;
	assert ( ! ( tx_len & ( tx_len - 1 ) ) );
	port->tx = ( exanic->tx + port->tx_offset );
	port->tx_count = ( tx_len / sizeof ( struct exanic_tx_chunk ) );

	/* Identify transmit feedback region */
	port->txf_slot = EXANIC_TXF_SLOT ( index );
	port->txf = ( exanic->txf +
		      ( port->txf_slot * sizeof ( *(port->txf) ) ) );

	/* Allocate receive region (via umalloc()) */
	port->rx = umalloc ( EXANIC_RX_LEN );
	if ( ! port->rx ) {
		rc = -ENOMEM;
		goto err_alloc_rx;
	}

	/* Set MAC address */
	memcpy ( netdev->hw_addr, exanic->mac, ETH_ALEN );
	netdev->hw_addr[ ETH_ALEN - 1 ] += index;

	/* Record default link speed and supported speeds */
	port->default_speed = readl ( port->regs + EXANIC_PORT_SPEED );
	port->speeds = ( exanic->caps & EXANIC_CAPS_SPEED_MASK );

	/* Register network device */
	if ( ( rc = register_netdev ( netdev ) ) != 0 )
		goto err_register_netdev;
	DBGC ( port, "EXANIC %s port %d TX [%#05zx,%#05zx) TXF %#02x RX "
	       "[%#lx,%#lx)\n", netdev->name, index, port->tx_offset,
	       ( port->tx_offset + tx_len ), port->txf_slot,
	       user_to_phys ( port->rx, 0 ),
	       user_to_phys ( port->rx, EXANIC_RX_LEN ) );

	/* Set initial link state */
	exanic_check_link ( netdev );

	return 0;

	unregister_netdev ( netdev );
 err_register_netdev:
	ufree ( port->rx );
 err_alloc_rx:
	netdev_nullify ( netdev );
	netdev_put ( netdev );
 err_alloc_netdev:
 absent:
	return rc;
}

/**
 * Probe port
 *
 * @v exanic		ExaNIC device
 * @v index		Port number
 */
static void exanic_remove_port ( struct exanic *exanic, unsigned int index ) {
	struct exanic_port *port;

	/* Do nothing if port is not physically present */
	port = exanic->port[index];
	if ( ! port )
		return;

	/* Unregister network device */
	unregister_netdev ( port->netdev );

	/* Free receive region */
	ufree ( port->rx );

	/* Free network device */
	netdev_nullify ( port->netdev );
	netdev_put ( port->netdev );
}

/**
 * Probe PCI device
 *
 * @v pci		PCI device
 * @ret rc		Return status code
 */
static int exanic_probe ( struct pci_device *pci ) {
	struct exanic *exanic;
	unsigned long regs_bar_start;
	unsigned long tx_bar_start;
	size_t tx_bar_len;
	int i;
	int rc;

	/* Allocate and initialise structure */
	exanic = zalloc ( sizeof ( *exanic ) );
	if ( ! exanic ) {
		rc = -ENOMEM;
		goto err_alloc;
	}
	pci_set_drvdata ( pci, exanic );

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

	/* Map registers */
	regs_bar_start = pci_bar_start ( pci, EXANIC_REGS_BAR );
	exanic->regs = ioremap ( regs_bar_start, EXANIC_REGS_LEN );
	if ( ! exanic->regs ) {
		rc = -ENODEV;
		goto err_ioremap_regs;
	}

	/* Reset device */
	exanic_reset ( exanic );

	/* Read capabilities */
	exanic->caps = readl ( exanic->regs + EXANIC_CAPS );

	/* Power up PHYs */
	writel ( EXANIC_POWER_ON, ( exanic->regs + EXANIC_POWER ) );

	/* Fetch base MAC address */
	if ( ( rc = exanic_fetch_mac ( exanic ) ) != 0 )
		goto err_fetch_mac;
	DBGC ( exanic, "EXANIC %p capabilities %#08x base MAC %s\n",
	       exanic, exanic->caps, eth_ntoa ( exanic->mac ) );

	/* Map transmit region */
	tx_bar_start = pci_bar_start ( pci, EXANIC_TX_BAR );
	tx_bar_len = pci_bar_size ( pci, EXANIC_TX_BAR );
	exanic->tx = ioremap ( tx_bar_start, tx_bar_len );
	if ( ! exanic->tx ) {
		rc = -ENODEV;
		goto err_ioremap_tx;
	}

	/* Allocate transmit feedback region (shared between all ports) */
	exanic->txf = malloc_dma ( EXANIC_TXF_LEN, EXANIC_ALIGN );
	if ( ! exanic->txf ) {
		rc = -ENOMEM;
		goto err_alloc_txf;
	}
	memset ( exanic->txf, 0, EXANIC_TXF_LEN );
	exanic_write_base ( virt_to_bus ( exanic->txf ),
			    ( exanic->regs + EXANIC_TXF_BASE ) );

	/* Allocate and initialise per-port network devices */
	for ( i = 0 ; i < EXANIC_MAX_PORTS ; i++ ) {
		if ( ( rc = exanic_probe_port ( exanic, &pci->dev, i ) ) != 0 )
			goto err_probe_port;
	}

	return 0;

	i = EXANIC_MAX_PORTS;
 err_probe_port:
	for ( i-- ; i >= 0 ; i-- )
		exanic_remove_port ( exanic, i );
	exanic_reset ( exanic );
	free_dma ( exanic->txf, EXANIC_TXF_LEN );
 err_alloc_txf:
	iounmap ( exanic->tx );
 err_ioremap_tx:
	iounmap ( exanic->regs );
 err_fetch_mac:
 err_ioremap_regs:
	free ( exanic );
 err_alloc:
	return rc;
}

/**
 * Remove PCI device
 *
 * @v pci		PCI device
 */
static void exanic_remove ( struct pci_device *pci ) {
	struct exanic *exanic = pci_get_drvdata ( pci );
	unsigned int i;

	/* Remove all ports */
	for ( i = 0 ; i < EXANIC_MAX_PORTS ; i++ )
		exanic_remove_port ( exanic, i );

	/* Reset device */
	exanic_reset ( exanic );

	/* Free transmit feedback region */
	free_dma ( exanic->txf, EXANIC_TXF_LEN );

	/* Unmap transmit region */
	iounmap ( exanic->tx );

	/* Unmap registers */
	iounmap ( exanic->regs );

	/* Free device */
	free ( exanic );
}

/** ExaNIC PCI device IDs */
static struct pci_device_id exanic_ids[] = {
	PCI_ROM ( 0x10ee, 0x2b00, "exanic-old", "ExaNIC (old)", 0 ),
	PCI_ROM ( 0x1ce4, 0x0001, "exanic-x4", "ExaNIC X4", 0 ),
	PCI_ROM ( 0x1ce4, 0x0002, "exanic-x2", "ExaNIC X2", 0 ),
	PCI_ROM ( 0x1ce4, 0x0003, "exanic-x10", "ExaNIC X10", 0 ),
	PCI_ROM ( 0x1ce4, 0x0004, "exanic-x10gm", "ExaNIC X10 GM", 0 ),
	PCI_ROM ( 0x1ce4, 0x0005, "exanic-x40", "ExaNIC X40", 0 ),
	PCI_ROM ( 0x1ce4, 0x0006, "exanic-x10hpt", "ExaNIC X10 HPT", 0 ),
	PCI_ROM ( 0x1ce4, 0x0007, "exanic-x40g", "ExaNIC X40", 0 ),
};

/** ExaNIC PCI driver */
struct pci_driver exanic_driver __pci_driver = {
	.ids = exanic_ids,
	.id_count = ( sizeof ( exanic_ids ) / sizeof ( exanic_ids[0] ) ),
	.probe = exanic_probe,
	.remove = exanic_remove,
};