summaryrefslogtreecommitdiffstats
path: root/src/drivers/net/ice.c
blob: b5d66f1bbd81afc1e55df81e905930c17f1e1ade (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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/*
 * Copyright (C) 2022 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 <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/pci.h>
#include "ice.h"

/** @file
 *
 * Intel 100 Gigabit Ethernet network card driver
 *
 */

/**
 * Magic MAC address
 *
 * Used as the source address and promiscuous unicast destination
 * address in the "add switch rules" command.
 */
static uint8_t ice_magic_mac[ETH_HLEN] = { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };

/******************************************************************************
 *
 * Admin queue
 *
 ******************************************************************************
 */

/**
 * Get firmware version
 *
 * @v intelxl		Intel device
 * @ret rc		Return status code
 */
static int ice_admin_version ( struct intelxl_nic *intelxl ) {
	struct ice_admin_descriptor *cmd;
	struct ice_admin_version_params *version;
	unsigned int api;
	int rc;

	/* Populate descriptor */
	cmd = ice_admin_command_descriptor ( intelxl );
	cmd->opcode = cpu_to_le16 ( INTELXL_ADMIN_VERSION );
	version = &cmd->params.version;

	/* Issue command */
	if ( ( rc = intelxl_admin_command ( intelxl ) ) != 0 )
		return rc;
	api = version->api.major;
	DBGC ( intelxl, "ICE %p firmware v%d/%d.%d.%d API v%d/%d.%d.%d\n",
	       intelxl, version->firmware.branch, version->firmware.major,
	       version->firmware.minor, version->firmware.patch,
	       version->api.branch, version->api.major, version->api.minor,
	       version->api.patch );

	/* Check for API compatibility */
	if ( api > INTELXL_ADMIN_API_MAJOR ) {
		DBGC ( intelxl, "ICE %p unsupported API v%d\n", intelxl, api );
		return -ENOTSUP;
	}

	return 0;
}

/**
 * Get MAC address
 *
 * @v netdev		Network device
 * @ret rc		Return status code
 */
static int ice_admin_mac_read ( struct net_device *netdev ) {
	struct intelxl_nic *intelxl = netdev->priv;
	struct ice_admin_descriptor *cmd;
	struct ice_admin_mac_read_params *read;
	struct ice_admin_mac_read_address *mac;
	union ice_admin_buffer *buf;
	unsigned int i;
	int rc;

	/* Populate descriptor */
	cmd = ice_admin_command_descriptor ( intelxl );
	cmd->opcode = cpu_to_le16 ( INTELXL_ADMIN_MAC_READ );
	cmd->flags = cpu_to_le16 ( INTELXL_ADMIN_FL_BUF );
	cmd->len = cpu_to_le16 ( sizeof ( buf->mac_read ) );
	read = &cmd->params.mac_read;
	buf = ice_admin_command_buffer ( intelxl );

	/* Issue command */
	if ( ( rc = intelxl_admin_command ( intelxl ) ) != 0 )
		return rc;

	/* Check that MAC address is present in response */
	if ( ! ( read->valid & INTELXL_ADMIN_MAC_READ_VALID_LAN ) ) {
		DBGC ( intelxl, "ICE %p has no MAC address\n", intelxl );
		return -ENOENT;
	}

	/* Identify MAC address */
	for ( i = 0 ; i < read->count ; i++ ) {

		/* Check for a LAN MAC address */
		mac = &buf->mac_read.mac[i];
		if ( mac->type != ICE_ADMIN_MAC_READ_TYPE_LAN )
			continue;

		/* Check that address is valid */
		if ( ! is_valid_ether_addr ( mac->mac ) ) {
			DBGC ( intelxl, "ICE %p has invalid MAC address "
			       "(%s)\n", intelxl, eth_ntoa ( mac->mac ) );
			return -EINVAL;
		}

		/* Copy MAC address */
		DBGC ( intelxl, "ICE %p has MAC address %s\n",
		       intelxl, eth_ntoa ( mac->mac ) );
		memcpy ( netdev->hw_addr, mac->mac, ETH_ALEN );

		return 0;
	}

	/* Missing LAN MAC address */
	DBGC ( intelxl, "ICE %p has no LAN MAC address\n",
	       intelxl );
	return -ENOENT;
}

/**
 * Set MAC address
 *
 * @v netdev		Network device
 * @ret rc		Return status code
 */
static int ice_admin_mac_write ( struct net_device *netdev ) {
	struct intelxl_nic *intelxl = netdev->priv;
	struct ice_admin_descriptor *cmd;
	struct ice_admin_mac_write_params *write;
	int rc;

	/* Populate descriptor */
	cmd = ice_admin_command_descriptor ( intelxl );
	cmd->opcode = cpu_to_le16 ( INTELXL_ADMIN_MAC_WRITE );
	write = &cmd->params.mac_write;
	memcpy ( write->mac, netdev->ll_addr, ETH_ALEN );

	/* Issue command */
	if ( ( rc = intelxl_admin_command ( intelxl ) ) != 0 )
		return rc;

	return 0;
}

/**
 * Get switch configuration
 *
 * @v intelxl		Intel device
 * @ret rc		Return status code
 */
static int ice_admin_switch ( struct intelxl_nic *intelxl ) {
	struct ice_admin_descriptor *cmd;
	struct ice_admin_switch_params *sw;
	union ice_admin_buffer *buf;
	uint16_t next = 0;
	uint16_t seid;
	uint16_t type;
	int rc;

	/* Get each configuration in turn */
	do {
		/* Populate descriptor */
		cmd = ice_admin_command_descriptor ( intelxl );
		cmd->opcode = cpu_to_le16 ( INTELXL_ADMIN_SWITCH );
		cmd->flags = cpu_to_le16 ( INTELXL_ADMIN_FL_BUF );
		cmd->len = cpu_to_le16 ( sizeof ( buf->sw ) );
		sw = &cmd->params.sw;
		sw->next = next;
		buf = ice_admin_command_buffer ( intelxl );

		/* Issue command */
		if ( ( rc = intelxl_admin_command ( intelxl ) ) != 0 )
			return rc;
		seid = le16_to_cpu ( buf->sw.cfg[0].seid );

		/* Dump raw configuration */
		DBGC2 ( intelxl, "ICE %p SEID %#04x:\n", intelxl, seid );
		DBGC2_HDA ( intelxl, 0, &buf->sw.cfg[0],
			    sizeof ( buf->sw.cfg[0] ) );

		/* Parse response */
		type = ( seid & ICE_ADMIN_SWITCH_TYPE_MASK );
		if ( type == ICE_ADMIN_SWITCH_TYPE_VSI ) {
			intelxl->vsi = ( seid & ~ICE_ADMIN_SWITCH_TYPE_MASK );
			DBGC ( intelxl, "ICE %p VSI %#04x uplink %#04x func "
			       "%#04x\n", intelxl, intelxl->vsi,
			       le16_to_cpu ( buf->sw.cfg[0].uplink ),
			       le16_to_cpu ( buf->sw.cfg[0].func ) );
		}

	} while ( ( next = sw->next ) );

	/* Check that we found a VSI */
	if ( ! intelxl->vsi ) {
		DBGC ( intelxl, "ICE %p has no VSI\n", intelxl );
		return -ENOENT;
	}

	return 0;
}

/**
 * Add switch rules
 *
 * @v intelxl		Intel device
 * @v mac		MAC address
 * @ret rc		Return status code
 */
static int ice_admin_rules ( struct intelxl_nic *intelxl, uint8_t *mac ) {
	struct ice_admin_descriptor *cmd;
	struct ice_admin_rules_params *rules;
	union ice_admin_buffer *buf;
	int rc;

	/* Populate descriptor */
	cmd = ice_admin_command_descriptor ( intelxl );
	cmd->opcode = cpu_to_le16 ( ICE_ADMIN_ADD_RULES );
	cmd->flags = cpu_to_le16 ( INTELXL_ADMIN_FL_BUF | INTELXL_ADMIN_FL_RD );
	cmd->len = cpu_to_le16 ( sizeof ( buf->rules ) );
	rules = &cmd->params.rules;
	rules->count = cpu_to_le16 ( 1 );
	buf = ice_admin_command_buffer ( intelxl );
	buf->rules.recipe = cpu_to_le16 ( ICE_ADMIN_RULES_RECIPE_PROMISC );
	buf->rules.port = cpu_to_le16 ( intelxl->port );
	buf->rules.action =
		cpu_to_le32 ( ICE_ADMIN_RULES_ACTION_VALID |
			      ICE_ADMIN_RULES_ACTION_VSI ( intelxl->vsi ) );
	buf->rules.len = cpu_to_le16 ( sizeof ( buf->rules.hdr ) );
	memcpy ( buf->rules.hdr.eth.h_dest, mac, ETH_ALEN );
	memcpy ( buf->rules.hdr.eth.h_source, ice_magic_mac, ETH_ALEN );
	buf->rules.hdr.eth.h_protocol = htons ( ETH_P_8021Q );

	/* Issue command */
	if ( ( rc = intelxl_admin_command ( intelxl ) ) != 0 )
		return rc;

	return 0;
}

/**
 * Check if scheduler node is a parent (i.e. non-leaf) node
 *
 * @v branch		Scheduler topology branch
 * @v node		Scheduler topology node
 * @ret child		Any child node, or NULL if not found
 */
static struct ice_admin_schedule_node *
ice_admin_schedule_is_parent ( struct ice_admin_schedule_branch *branch,
			       struct ice_admin_schedule_node *node ) {
	unsigned int count = le16_to_cpu ( branch->count );
	struct ice_admin_schedule_node *child;
	unsigned int i;

	/* Find a child element, if any */
	for ( i = 0 ; i < count ; i++ ) {
		child = &branch->node[i];
		if ( child->parent == node->teid )
			return child;
	}

	return NULL;
}

/**
 * Query default scheduling tree topology
 *
 * @v intelxl		Intel device
 * @ret rc		Return status code
 */
static int ice_admin_schedule ( struct intelxl_nic *intelxl ) {
	struct ice_admin_descriptor *cmd;
	struct ice_admin_schedule_params *sched;
	struct ice_admin_schedule_branch *branch;
	struct ice_admin_schedule_node *node;
	union ice_admin_buffer *buf;
	int i;
	int rc;

	/* Populate descriptor */
	cmd = ice_admin_command_descriptor ( intelxl );
	cmd->opcode = cpu_to_le16 ( ICE_ADMIN_SCHEDULE );
	cmd->flags = cpu_to_le16 ( INTELXL_ADMIN_FL_BUF );
	cmd->len = cpu_to_le16 ( sizeof ( buf->sched ) );
	sched = &cmd->params.sched;
	buf = ice_admin_command_buffer ( intelxl );

	/* Issue command */
	if ( ( rc = intelxl_admin_command ( intelxl ) ) != 0 )
		return rc;

	/* Sanity checks */
	if ( ! sched->branches ) {
		DBGC ( intelxl, "ICE %p topology has no branches\n", intelxl );
		return -EINVAL;
	}
	branch = buf->sched.branch;

	/* Identify leaf node */
	for ( i = ( le16_to_cpu ( branch->count ) - 1 ) ; i >= 0 ; i-- ) {
		node = &branch->node[i];
		if ( ! ice_admin_schedule_is_parent ( branch, node ) ) {
			intelxl->teid = le32_to_cpu ( node->teid );
			DBGC2 ( intelxl, "ICE %p TEID %#08x type %d\n",
				intelxl, intelxl->teid, node->config.type );
			break;
		}
	}
	if ( ! intelxl->teid ) {
		DBGC ( intelxl, "ICE %p found no leaf TEID\n", intelxl );
		return -EINVAL;
	}

	return 0;
}

/**
 * Restart autonegotiation
 *
 * @v intelxl		Intel device
 * @ret rc		Return status code
 */
static int ice_admin_autoneg ( struct intelxl_nic *intelxl ) {
	struct ice_admin_descriptor *cmd;
	struct ice_admin_autoneg_params *autoneg;
	int rc;

	/* Populate descriptor */
	cmd = ice_admin_command_descriptor ( intelxl );
	cmd->opcode = cpu_to_le16 ( INTELXL_ADMIN_AUTONEG );
	autoneg = &cmd->params.autoneg;
	autoneg->flags = ( INTELXL_ADMIN_AUTONEG_FL_RESTART |
			   INTELXL_ADMIN_AUTONEG_FL_ENABLE );

	/* Issue command */
	if ( ( rc = intelxl_admin_command ( intelxl ) ) != 0 )
		return rc;

	return 0;
}

/**
 * Get link status
 *
 * @v netdev		Network device
 * @ret rc		Return status code
 */
static int ice_admin_link ( struct net_device *netdev ) {
	struct intelxl_nic *intelxl = netdev->priv;
	struct ice_admin_descriptor *cmd;
	struct ice_admin_link_params *link;
	union ice_admin_buffer *buf;
	int rc;

	/* Populate descriptor */
	cmd = ice_admin_command_descriptor ( intelxl );
	cmd->opcode = cpu_to_le16 ( INTELXL_ADMIN_LINK );
	cmd->flags = cpu_to_le16 ( INTELXL_ADMIN_FL_BUF );
	cmd->len = cpu_to_le16 ( sizeof ( buf->link ) );
	link = &cmd->params.link;
	link->notify = INTELXL_ADMIN_LINK_NOTIFY;
	buf = ice_admin_command_buffer ( intelxl );

	/* Issue command */
	if ( ( rc = intelxl_admin_command ( intelxl ) ) != 0 )
		return rc;
	DBGC ( intelxl, "ICE %p speed %#02x status %#02x\n",
	       intelxl, le16_to_cpu ( buf->link.speed ), buf->link.status );

	/* Update network device */
	if ( buf->link.status & INTELXL_ADMIN_LINK_UP ) {
		netdev_link_up ( netdev );
	} else {
		netdev_link_down ( netdev );
	}

	return 0;
}

/**
 * Handle admin event
 *
 * @v netdev		Network device
 * @v xlevt		Event descriptor
 * @v xlbuf		Data buffer
 */
static void ice_admin_event ( struct net_device *netdev,
			      struct intelxl_admin_descriptor *xlevt,
			      union intelxl_admin_buffer *xlbuf __unused ) {
	struct intelxl_nic *intelxl = netdev->priv;
	struct ice_admin_descriptor *evt =
		container_of ( xlevt, struct ice_admin_descriptor, xl );

	/* Ignore unrecognised events */
	if ( evt->opcode != cpu_to_le16 ( INTELXL_ADMIN_LINK ) ) {
		DBGC ( intelxl, "INTELXL %p unrecognised event opcode "
		       "%#04x\n", intelxl, le16_to_cpu ( evt->opcode ) );
		return;
	}

	/* Update link status */
	ice_admin_link ( netdev );
}

/**
 * Add transmit queue
 *
 * @v intelxl		Intel device
 * @v ring		Descriptor ring
 * @ret rc		Return status code
 */
static int ice_admin_add_txq ( struct intelxl_nic *intelxl,
			       struct intelxl_ring *ring ) {
	struct ice_admin_descriptor *cmd;
	struct ice_admin_add_txq_params *add_txq;
	union ice_admin_buffer *buf;
	struct ice_context_tx *ctx;
	struct ice_schedule_tx *sched;
	physaddr_t address;
	int rc;

	/* Populate descriptor */
	cmd = ice_admin_command_descriptor ( intelxl );
	cmd->opcode = cpu_to_le16 ( ICE_ADMIN_ADD_TXQ );
	cmd->flags = cpu_to_le16 ( INTELXL_ADMIN_FL_RD | INTELXL_ADMIN_FL_BUF );
	cmd->len = cpu_to_le16 ( sizeof ( buf->add_txq ) );
	add_txq = &cmd->params.add_txq;
	add_txq->count = 1;
	buf = ice_admin_command_buffer ( intelxl );
	buf->add_txq.parent = cpu_to_le32 ( intelxl->teid );
	buf->add_txq.count = 1;
	ctx = &buf->add_txq.ctx;
	address = dma ( &ring->map, ring->desc.raw );
	ctx->base_port =
		cpu_to_le64 ( ICE_TXQ_BASE_PORT ( address, intelxl->port ) );
	ctx->pf_type = cpu_to_le16 ( ICE_TXQ_PF_TYPE ( intelxl->pf ) );
	ctx->vsi = cpu_to_le16 ( intelxl->vsi );
	ctx->len = cpu_to_le16 ( ICE_TXQ_LEN ( INTELXL_TX_NUM_DESC ) );
	ctx->flags = cpu_to_le16 ( ICE_TXQ_FL_TSO | ICE_TXQ_FL_LEGACY );
	sched = &buf->add_txq.sched;
	sched->sections = ( ICE_SCHEDULE_GENERIC | ICE_SCHEDULE_COMMIT |
			    ICE_SCHEDULE_EXCESS );
	sched->commit_weight = cpu_to_le16 ( ICE_SCHEDULE_WEIGHT );
	sched->excess_weight = cpu_to_le16 ( ICE_SCHEDULE_WEIGHT );

	/* Issue command */
	if ( ( rc = intelxl_admin_command ( intelxl ) ) != 0 )
		return rc;
	DBGC ( intelxl, "ICE %p added TEID %#04x\n",
	       intelxl, le32_to_cpu ( buf->add_txq.teid ) );

	return 0;
}

/**
 * Disable transmit queue
 *
 * @v intelxl		Intel device
 * @v ring		Descriptor ring
 * @ret rc		Return status code
 */
static int ice_admin_disable_txq ( struct intelxl_nic *intelxl ) {
	struct ice_admin_descriptor *cmd;
	struct ice_admin_disable_txq_params *disable_txq;
	union ice_admin_buffer *buf;
	int rc;

	/* Populate descriptor */
	cmd = ice_admin_command_descriptor ( intelxl );
	cmd->opcode = cpu_to_le16 ( ICE_ADMIN_DISABLE_TXQ );
	cmd->flags = cpu_to_le16 ( INTELXL_ADMIN_FL_RD | INTELXL_ADMIN_FL_BUF );
	cmd->len = cpu_to_le16 ( sizeof ( buf->disable_txq ) );
	disable_txq = &cmd->params.disable_txq;
	disable_txq->flags = ICE_TXQ_FL_FLUSH;
	disable_txq->count = 1;
	disable_txq->timeout = ICE_TXQ_TIMEOUT;
	buf = ice_admin_command_buffer ( intelxl );
	buf->disable_txq.parent = cpu_to_le32 ( intelxl->teid );
	buf->disable_txq.count = 1;

	/* Issue command */
	if ( ( rc = intelxl_admin_command ( intelxl ) ) != 0 )
		return rc;

	return 0;
}

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

/**
 * Dump transmit queue context (for debugging)
 *
 * @v intelxl		Intel device
 */
static void ice_dump_tx ( struct intelxl_nic *intelxl ) {
	uint32_t ctx[ sizeof ( struct ice_context_tx ) / sizeof ( uint32_t ) ];
	uint32_t stat;
	unsigned int i;

	/* Do nothing unless debug output is enabled */
	if ( ! DBG_EXTRA )
		return;

	/* Trigger reading of transmit context */
	writel ( ( ICE_GLCOMM_QTX_CNTX_CTL_CMD_READ |
		   ICE_GLCOMM_QTX_CNTX_CTL_EXEC ),
		 intelxl->regs + ICE_GLCOMM_QTX_CNTX_CTL );

	/* Wait for operation to complete */
	for ( i = 0 ; i < INTELXL_CTX_MAX_WAIT_MS ; i++ ) {

		/* Check if operation is complete */
		stat = readl ( intelxl->regs + ICE_GLCOMM_QTX_CNTX_STAT );
		if ( ! ( stat & ICE_GLCOMM_QTX_CNTX_BUSY ) )
			break;

		/* Delay */
		mdelay ( 1 );
	}

	/* Read context registers */
	for ( i = 0 ; i < ( sizeof ( ctx ) / sizeof ( ctx[0] ) ) ; i++ ) {
		ctx[i] = cpu_to_le32 ( readl ( intelxl->regs +
					       ICE_GLCOMM_QTX_CNTX_DATA ( i )));
	}

	/* Dump context */
	DBGC2 ( intelxl, "ICE %p TX context:\n", intelxl );
	DBGC2_HDA ( intelxl, 0, ctx, sizeof ( ctx ) );
}

/**
 * Dump receive queue context (for debugging)
 *
 * @v intelxl		Intel device
 */
static void ice_dump_rx ( struct intelxl_nic *intelxl ) {
	uint32_t ctx[ sizeof ( struct intelxl_context_rx ) /
		      sizeof ( uint32_t ) ];
	unsigned int i;

	/* Do nothing unless debug output is enabled */
	if ( ! DBG_EXTRA )
		return;

	/* Read context registers */
	for ( i = 0 ; i < ( sizeof ( ctx ) / sizeof ( ctx[0] ) ) ; i++ ) {
		ctx[i] = cpu_to_le32 ( readl ( intelxl->regs +
					       ICE_QRX_CONTEXT ( i ) ) );
	}

	/* Dump context */
	DBGC2 ( intelxl, "ICE %p RX context:\n", intelxl );
	DBGC2_HDA ( intelxl, 0, ctx, sizeof ( ctx ) );
}

/**
 * Create transmit queue
 *
 * @v intelxl		Intel device
 * @v ring		Descriptor ring
 * @ret rc		Return status code
 */
static int ice_create_tx ( struct intelxl_nic *intelxl,
			   struct intelxl_ring *ring ) {
	int rc;

	/* Allocate descriptor ring */
	if ( ( rc = intelxl_alloc_ring ( intelxl, ring ) ) != 0 )
		goto err_alloc;

	/* Add transmit queue */
	if ( ( rc = ice_admin_add_txq ( intelxl, ring ) ) != 0 )
		goto err_add_txq;

	return 0;

 err_add_txq:
	intelxl_free_ring ( intelxl, ring );
 err_alloc:
	return rc;
}

/**
 * Destroy transmit queue
 *
 * @v intelxl		Intel device
 * @v ring		Descriptor ring
 * @ret rc		Return status code
 */
static void ice_destroy_tx ( struct intelxl_nic *intelxl,
			     struct intelxl_ring *ring ) {
	int rc;

	/* Disable transmit queue */
	if ( ( rc = ice_admin_disable_txq ( intelxl ) ) != 0 ) {
		/* Leak memory; there's nothing else we can do */
		return;
	}

	/* Free descriptor ring */
	intelxl_free_ring ( intelxl, ring );
}

/**
 * Program receive queue context
 *
 * @v intelxl		Intel device
 * @v address		Descriptor ring base address
 * @ret rc		Return status code
 */
static int ice_context_rx ( struct intelxl_nic *intelxl,
			    physaddr_t address ) {
	union {
		struct intelxl_context_rx rx;
		uint32_t raw[ sizeof ( struct intelxl_context_rx ) /
			      sizeof ( uint32_t ) ];
	} ctx;
	uint64_t base_count;
	unsigned int i;

	/* Initialise context */
	memset ( &ctx, 0, sizeof ( ctx ) );
	base_count = INTELXL_CTX_RX_BASE_COUNT ( address, INTELXL_RX_NUM_DESC );
	ctx.rx.base_count = cpu_to_le64 ( base_count );
	ctx.rx.len = cpu_to_le16 ( INTELXL_CTX_RX_LEN ( intelxl->mfs ) );
	ctx.rx.flags = ( INTELXL_CTX_RX_FL_DSIZE | INTELXL_CTX_RX_FL_CRCSTRIP );
	ctx.rx.mfs = cpu_to_le16 ( INTELXL_CTX_RX_MFS ( intelxl->mfs ) );

	/* Write context registers */
	for ( i = 0 ; i < ( sizeof ( ctx ) / sizeof ( ctx.raw[0] ) ) ; i++ ) {
		writel ( le32_to_cpu ( ctx.raw[i] ),
			 ( intelxl->regs + ICE_QRX_CONTEXT ( i ) ) );
	}

	return 0;
}

/**
 * Open network device
 *
 * @v netdev		Network device
 * @ret rc		Return status code
 */
static int ice_open ( struct net_device *netdev ) {
	struct intelxl_nic *intelxl = netdev->priv;
	int rc;

	/* Calculate maximum frame size */
	intelxl->mfs = ( ( ETH_HLEN + netdev->mtu + 4 /* CRC */ +
			   INTELXL_ALIGN - 1 ) & ~( INTELXL_ALIGN - 1 ) );

	/* Set MAC address */
	if ( ( rc = ice_admin_mac_write ( netdev ) ) != 0 )
		goto err_mac_write;

	/* Set maximum frame size */
	if ( ( rc = intelxl_admin_mac_config ( intelxl ) ) != 0 )
		goto err_mac_config;

	/* Create receive descriptor ring */
	if ( ( rc = intelxl_create_ring ( intelxl, &intelxl->rx ) ) != 0 )
		goto err_create_rx;

	/* Create transmit descriptor ring */
	if ( ( rc = ice_create_tx ( intelxl, &intelxl->tx ) ) != 0 )
		goto err_create_tx;

	/* Restart autonegotiation */
	ice_admin_autoneg ( intelxl );

	/* Update link state */
	ice_admin_link ( netdev );

	return 0;

	ice_destroy_tx ( intelxl, &intelxl->tx );
 err_create_tx:
	intelxl_destroy_ring ( intelxl, &intelxl->rx );
 err_create_rx:
 err_mac_config:
 err_mac_write:
	return rc;
}

/**
 * Close network device
 *
 * @v netdev		Network device
 */
static void ice_close ( struct net_device *netdev ) {
	struct intelxl_nic *intelxl = netdev->priv;

	/* Dump contexts (for debugging) */
	ice_dump_tx ( intelxl );
	ice_dump_rx ( intelxl );

	/* Destroy transmit descriptor ring */
	ice_destroy_tx ( intelxl, &intelxl->tx );

	/* Destroy receive descriptor ring */
	intelxl_destroy_ring ( intelxl, &intelxl->rx );

	/* Discard any unused receive buffers */
	intelxl_empty_rx ( intelxl );
}

/** Network device operations */
static struct net_device_operations ice_operations = {
	.open		= ice_open,
	.close		= ice_close,
	.transmit	= intelxl_transmit,
	.poll		= intelxl_poll,
};

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

/**
 * Probe PCI device
 *
 * @v pci		PCI device
 * @ret rc		Return status code
 */
static int ice_probe ( struct pci_device *pci ) {
	struct net_device *netdev;
	struct intelxl_nic *intelxl;
	uint32_t pffunc_rid;
	uint32_t pfgen_portnum;
	int rc;

	/* Allocate and initialise net device */
	netdev = alloc_etherdev ( sizeof ( *intelxl ) );
	if ( ! netdev ) {
		rc = -ENOMEM;
		goto err_alloc;
	}
	netdev_init ( netdev, &ice_operations );
	netdev->max_pkt_len = INTELXL_MAX_PKT_LEN;
	intelxl = netdev->priv;
	pci_set_drvdata ( pci, netdev );
	netdev->dev = &pci->dev;
	memset ( intelxl, 0, sizeof ( *intelxl ) );
	intelxl->intr = ICE_GLINT_DYN_CTL;
	intelxl->handle = ice_admin_event;
	intelxl_init_admin ( &intelxl->command, INTELXL_ADMIN_CMD,
			     &intelxl_admin_offsets );
	intelxl_init_admin ( &intelxl->event, INTELXL_ADMIN_EVT,
			     &intelxl_admin_offsets );
	intelxl_init_ring ( &intelxl->tx, INTELXL_TX_NUM_DESC,
			    sizeof ( intelxl->tx.desc.tx[0] ), NULL );
	intelxl_init_ring ( &intelxl->rx, INTELXL_RX_NUM_DESC,
			    sizeof ( intelxl->rx.desc.rx[0] ),
			    ice_context_rx );

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

	/* Map registers */
	intelxl->regs = pci_ioremap ( pci, pci->membase, ICE_BAR_SIZE );
	if ( ! intelxl->regs ) {
		rc = -ENODEV;
		goto err_ioremap;
	}

	/* Configure DMA */
	intelxl->dma = &pci->dma;
	dma_set_mask_64bit ( intelxl->dma );
	netdev->dma = intelxl->dma;

	/* Locate PCI Express capability */
	intelxl->exp = pci_find_capability ( pci, PCI_CAP_ID_EXP );
	if ( ! intelxl->exp ) {
		DBGC ( intelxl, "ICE %p missing PCIe capability\n",
		       intelxl );
		rc = -ENXIO;
		goto err_exp;
	}

	/* Reset the function via PCIe FLR */
	pci_reset ( pci, intelxl->exp );

	/* Get function and port number */
	pffunc_rid = readl ( intelxl->regs + ICE_PFFUNC_RID );
	intelxl->pf = ICE_PFFUNC_RID_FUNC_NUM ( pffunc_rid );
	pfgen_portnum = readl ( intelxl->regs + ICE_PFGEN_PORTNUM );
	intelxl->port = ICE_PFGEN_PORTNUM_PORT_NUM ( pfgen_portnum );
	DBGC ( intelxl, "ICE %p PF %d using port %d\n",
	       intelxl, intelxl->pf, intelxl->port );

	/* Enable MSI-X dummy interrupt */
	if ( ( rc = intelxl_msix_enable ( intelxl, pci,
					  INTELXL_MSIX_VECTOR ) ) != 0 )
		goto err_msix;

	/* Open admin queues */
	if ( ( rc = intelxl_open_admin ( intelxl ) ) != 0 )
		goto err_open_admin;

	/* Get firmware version */
	if ( ( rc = ice_admin_version ( intelxl ) ) != 0 )
		goto err_admin_version;

	/* Clear PXE mode */
	if ( ( rc = intelxl_admin_clear_pxe ( intelxl ) ) != 0 )
		goto err_admin_clear_pxe;

	/* Get switch configuration */
	if ( ( rc = ice_admin_switch ( intelxl ) ) != 0 )
		goto err_admin_switch;

	/* Add broadcast address */
	if ( ( rc = ice_admin_rules ( intelxl, eth_broadcast ) ) != 0 )
		goto err_admin_rules_broadcast;

	/* Add promiscuous unicast address */
	if ( ( rc = ice_admin_rules ( intelxl, ice_magic_mac ) ) != 0 )
		goto err_admin_rules_magic;

	/* Query scheduler topology */
	if ( ( rc = ice_admin_schedule ( intelxl ) ) != 0 )
		goto err_admin_schedule;

	/* Get MAC address */
	if ( ( rc = ice_admin_mac_read ( netdev ) ) != 0 )
		goto err_admin_mac_read;

	/* Configure queue register addresses */
	intelxl->tx.tail = ICE_QTX_COMM_DBELL;
	intelxl->rx.reg = ICE_QRX_CTRL;
	intelxl->rx.tail = ICE_QRX_TAIL;

	/* Configure interrupt causes */
	writel ( ( ICE_QINT_TQCTL_ITR_INDX_NONE | ICE_QINT_TQCTL_CAUSE_ENA ),
		 intelxl->regs + ICE_QINT_TQCTL );
	writel ( ( ICE_QINT_RQCTL_ITR_INDX_NONE | ICE_QINT_RQCTL_CAUSE_ENA ),
		 intelxl->regs + ICE_QINT_RQCTL );

	/* Set a default value for the queue context flex extension,
	 * since this register erroneously retains its value across at
	 * least a PCIe FLR.
	 */
	writel ( ( ICE_QRX_FLXP_CNTXT_RXDID_IDX_LEGACY_32 |
		   ICE_QRX_FLXP_CNTXT_RXDID_PRIO_MAX ),
		 intelxl->regs + ICE_QRX_FLXP_CNTXT );

	/* Register network device */
	if ( ( rc = register_netdev ( netdev ) ) != 0 )
		goto err_register_netdev;

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

	return 0;

	unregister_netdev ( netdev );
 err_register_netdev:
 err_admin_mac_read:
 err_admin_schedule:
 err_admin_rules_magic:
 err_admin_rules_broadcast:
 err_admin_switch:
 err_admin_clear_pxe:
 err_admin_version:
	intelxl_close_admin ( intelxl );
 err_open_admin:
	intelxl_msix_disable ( intelxl, pci, INTELXL_MSIX_VECTOR );
 err_msix:
	pci_reset ( pci, intelxl->exp );
 err_exp:
	iounmap ( intelxl->regs );
 err_ioremap:
	netdev_nullify ( netdev );
	netdev_put ( netdev );
 err_alloc:
	return rc;
}

/**
 * Remove PCI device
 *
 * @v pci		PCI device
 */
static void ice_remove ( struct pci_device *pci ) {
	struct net_device *netdev = pci_get_drvdata ( pci );
	struct intelxl_nic *intelxl = netdev->priv;

	/* Unregister network device */
	unregister_netdev ( netdev );

	/* Close admin queues */
	intelxl_close_admin ( intelxl );

	/* Disable MSI-X dummy interrupt */
	intelxl_msix_disable ( intelxl, pci, INTELXL_MSIX_VECTOR );

	/* Reset the NIC */
	pci_reset ( pci, intelxl->exp );

	/* Free network device */
	iounmap ( intelxl->regs );
	netdev_nullify ( netdev );
	netdev_put ( netdev );
}

/** PCI device IDs */
static struct pci_device_id ice_nics[] = {
	PCI_ROM ( 0x8086, 0x124c, "e823l-bp", "E823-L backplane", 0 ),
	PCI_ROM ( 0x8086, 0x124d, "e823l-sfp", "E823-L SFP", 0 ),
	PCI_ROM ( 0x8086, 0x124e, "e823l-10gt", "E823-L 10GBASE-T", 0 ),
	PCI_ROM ( 0x8086, 0x124f, "e823l-1g", "E823-L 1GbE", 0 ),
	PCI_ROM ( 0x8086, 0x151d, "e823l-qsfp", "E823-L QSFP", 0 ),
	PCI_ROM ( 0x8086, 0x1591, "e810c-bp", "E810-C backplane", 0 ),
	PCI_ROM ( 0x8086, 0x1592, "e810c-qsfp", "E810-C QSFP", 0 ),
	PCI_ROM ( 0x8086, 0x1593, "e810c-sfp", "E810-C SFP", 0 ),
	PCI_ROM ( 0x8086, 0x1599, "e810-xxv-bp", "E810-XXV backplane", 0 ),
	PCI_ROM ( 0x8086, 0x159a, "e810-xxv-qsfp", "E810-XXV QSFP", 0 ),
	PCI_ROM ( 0x8086, 0x159b, "e810-xxv-sfp", "E810-XXV SFP", 0 ),
	PCI_ROM ( 0x8086, 0x188a, "e823c-bp", "E823-C backplane", 0 ),
	PCI_ROM ( 0x8086, 0x188b, "e823c-qsfp", "E823-C QSFP", 0 ),
	PCI_ROM ( 0x8086, 0x188c, "e823c-sfp", "E823-C SFP", 0 ),
	PCI_ROM ( 0x8086, 0x188d, "e823c-10gt", "E823-C 10GBASE-T", 0 ),
	PCI_ROM ( 0x8086, 0x188e, "e823c-1g", "E823-C 1GbE", 0 ),
	PCI_ROM ( 0x8086, 0x1890, "e822c-bp", "E822-C backplane", 0 ),
	PCI_ROM ( 0x8086, 0x1891, "e822c-qsfp", "E822-C QSFP", 0 ),
	PCI_ROM ( 0x8086, 0x1892, "e822c-sfp", "E822-C SFP", 0 ),
	PCI_ROM ( 0x8086, 0x1893, "e822c-10gt", "E822-C 10GBASE-T", 0 ),
	PCI_ROM ( 0x8086, 0x1894, "e822c-1g", "E822-C 1GbE", 0 ),
	PCI_ROM ( 0x8086, 0x1897, "e822l-bp", "E822-L backplane", 0 ),
	PCI_ROM ( 0x8086, 0x1898, "e822l-sfp", "E822-L SFP", 0 ),
	PCI_ROM ( 0x8086, 0x1899, "e822l-10gt", "E822-L 10GBASE-T", 0 ),
	PCI_ROM ( 0x8086, 0x189a, "e822l-1g", "E822-L 1GbE", 0 ),
};

/** PCI driver */
struct pci_driver ice_driver __pci_driver = {
	.ids = ice_nics,
	.id_count = ( sizeof ( ice_nics ) / sizeof ( ice_nics[0] ) ),
	.probe = ice_probe,
	.remove = ice_remove,
};