-
Notifications
You must be signed in to change notification settings - Fork 649
/
intel.c
1204 lines (1051 loc) · 37.3 KB
/
intel.c
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2012 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/dma.h>
#include <ipxe/pci.h>
#include <ipxe/profile.h>
#include "intel.h"
/** @file
*
* Intel 10/100/1000 network card driver
*
*/
/** VM transmit profiler */
static struct profiler intel_vm_tx_profiler __profiler =
{ .name = "intel.vm_tx" };
/** VM receive refill profiler */
static struct profiler intel_vm_refill_profiler __profiler =
{ .name = "intel.vm_refill" };
/** VM poll profiler */
static struct profiler intel_vm_poll_profiler __profiler =
{ .name = "intel.vm_poll" };
/******************************************************************************
*
* EEPROM interface
*
******************************************************************************
*/
/**
* Read data from EEPROM
*
* @v nvs NVS device
* @v address Address from which to read
* @v data Data buffer
* @v len Length of data buffer
* @ret rc Return status code
*/
static int intel_read_eeprom ( struct nvs_device *nvs, unsigned int address,
void *data, size_t len ) {
struct intel_nic *intel =
container_of ( nvs, struct intel_nic, eeprom );
unsigned int i;
uint32_t value;
uint16_t *data_word = data;
/* Sanity check. We advertise a blocksize of one word, so
* should only ever receive single-word requests.
*/
assert ( len == sizeof ( *data_word ) );
/* Initiate read */
writel ( ( INTEL_EERD_START | ( address << intel->eerd_addr_shift ) ),
intel->regs + INTEL_EERD );
/* Wait for read to complete */
for ( i = 0 ; i < INTEL_EEPROM_MAX_WAIT_MS ; i++ ) {
/* If read is not complete, delay 1ms and retry */
value = readl ( intel->regs + INTEL_EERD );
if ( ! ( value & intel->eerd_done ) ) {
mdelay ( 1 );
continue;
}
/* Extract data */
*data_word = cpu_to_le16 ( INTEL_EERD_DATA ( value ) );
return 0;
}
DBGC ( intel, "INTEL %p timed out waiting for EEPROM read\n", intel );
return -ETIMEDOUT;
}
/**
* Write data to EEPROM
*
* @v nvs NVS device
* @v address Address to which to write
* @v data Data buffer
* @v len Length of data buffer
* @ret rc Return status code
*/
static int intel_write_eeprom ( struct nvs_device *nvs,
unsigned int address __unused,
const void *data __unused,
size_t len __unused ) {
struct intel_nic *intel =
container_of ( nvs, struct intel_nic, eeprom );
DBGC ( intel, "INTEL %p EEPROM write not supported\n", intel );
return -ENOTSUP;
}
/**
* Initialise EEPROM
*
* @v intel Intel device
* @ret rc Return status code
*/
static int intel_init_eeprom ( struct intel_nic *intel ) {
unsigned int i;
uint32_t value;
/* The NIC automatically detects the type of attached EEPROM.
* The EERD register provides access to only a single word at
* a time, so we pretend to have a single-word block size.
*
* The EEPROM size may be larger than the minimum size, but
* this doesn't matter to us since we access only the first
* few words.
*/
intel->eeprom.word_len_log2 = INTEL_EEPROM_WORD_LEN_LOG2;
intel->eeprom.size = INTEL_EEPROM_MIN_SIZE_WORDS;
intel->eeprom.block_size = 1;
intel->eeprom.read = intel_read_eeprom;
intel->eeprom.write = intel_write_eeprom;
/* The layout of the EERD register was changed at some point
* to accommodate larger EEPROMs. Read from address zero (for
* which the request layouts are compatible) to determine
* which type of register we have.
*/
writel ( INTEL_EERD_START, intel->regs + INTEL_EERD );
for ( i = 0 ; i < INTEL_EEPROM_MAX_WAIT_MS ; i++ ) {
value = readl ( intel->regs + INTEL_EERD );
if ( value & INTEL_EERD_DONE_LARGE ) {
DBGC ( intel, "INTEL %p has large-format EERD\n",
intel );
intel->eerd_done = INTEL_EERD_DONE_LARGE;
intel->eerd_addr_shift = INTEL_EERD_ADDR_SHIFT_LARGE;
return 0;
}
if ( value & INTEL_EERD_DONE_SMALL ) {
DBGC ( intel, "INTEL %p has small-format EERD\n",
intel );
intel->eerd_done = INTEL_EERD_DONE_SMALL;
intel->eerd_addr_shift = INTEL_EERD_ADDR_SHIFT_SMALL;
return 0;
}
mdelay ( 1 );
}
DBGC ( intel, "INTEL %p timed out waiting for initial EEPROM read "
"(value %08x)\n", intel, value );
return -ETIMEDOUT;
}
/******************************************************************************
*
* MAC address
*
******************************************************************************
*/
/**
* Fetch initial MAC address from EEPROM
*
* @v intel Intel device
* @v hw_addr Hardware address to fill in
* @ret rc Return status code
*/
static int intel_fetch_mac_eeprom ( struct intel_nic *intel,
uint8_t *hw_addr ) {
int rc;
/* Initialise EEPROM */
if ( ( rc = intel_init_eeprom ( intel ) ) != 0 )
return rc;
/* Read base MAC address from EEPROM */
if ( ( rc = nvs_read ( &intel->eeprom, INTEL_EEPROM_MAC,
hw_addr, ETH_ALEN ) ) != 0 ) {
DBGC ( intel, "INTEL %p could not read EEPROM base MAC "
"address: %s\n", intel, strerror ( rc ) );
return rc;
}
/* Adjust MAC address for multi-port devices */
hw_addr[ETH_ALEN-1] ^= intel->port;
DBGC ( intel, "INTEL %p has EEPROM MAC address %s (port %d)\n",
intel, eth_ntoa ( hw_addr ), intel->port );
return 0;
}
/**
* Fetch initial MAC address
*
* @v intel Intel device
* @v hw_addr Hardware address to fill in
* @ret rc Return status code
*/
static int intel_fetch_mac ( struct intel_nic *intel, uint8_t *hw_addr ) {
union intel_receive_address mac;
int rc;
/* Read current address from RAL0/RAH0 */
mac.reg.low = cpu_to_le32 ( readl ( intel->regs + INTEL_RAL0 ) );
mac.reg.high = cpu_to_le32 ( readl ( intel->regs + INTEL_RAH0 ) );
DBGC ( intel, "INTEL %p has autoloaded MAC address %s\n",
intel, eth_ntoa ( mac.raw ) );
/* Use current address if valid */
if ( is_valid_ether_addr ( mac.raw ) ) {
memcpy ( hw_addr, mac.raw, ETH_ALEN );
return 0;
}
/* Otherwise, try to read address from EEPROM */
if ( ( rc = intel_fetch_mac_eeprom ( intel, hw_addr ) ) == 0 )
return 0;
DBGC ( intel, "INTEL %p has no MAC address to use\n", intel );
return -ENOENT;
}
/******************************************************************************
*
* Device reset
*
******************************************************************************
*/
/**
* Reset hardware
*
* @v intel Intel device
* @ret rc Return status code
*/
static int intel_reset ( struct intel_nic *intel ) {
uint32_t pbs;
uint32_t pba;
uint32_t ctrl;
uint32_t status;
uint32_t orig_ctrl;
uint32_t orig_status;
/* Record initial control and status register values */
orig_ctrl = ctrl = readl ( intel->regs + INTEL_CTRL );
orig_status = readl ( intel->regs + INTEL_STATUS );
/* Force RX and TX packet buffer allocation, to work around an
* errata in ICH devices.
*/
if ( intel->flags & INTEL_PBS_ERRATA ) {
DBGC ( intel, "INTEL %p WARNING: applying ICH PBS/PBA errata\n",
intel );
pbs = readl ( intel->regs + INTEL_PBS );
pba = readl ( intel->regs + INTEL_PBA );
writel ( 0x08, intel->regs + INTEL_PBA );
writel ( 0x10, intel->regs + INTEL_PBS );
DBGC ( intel, "INTEL %p PBS %#08x->%#08x PBA %#08x->%#08x\n",
intel, pbs, readl ( intel->regs + INTEL_PBS ),
pba, readl ( intel->regs + INTEL_PBA ) );
}
/* The Intel I210's packet buffer size registers reset only on
* power up. If an operating system changes these but then
* the computer recieves a reset signal without losing power,
* the registers will stay the same (but be incompatible with
* other register defaults), thus making the device unable to
* pass traffic.
*/
if ( intel->flags & INTEL_PBSIZE_RST ) {
writel ( INTEL_RXPBS_I210, intel->regs + INTEL_RXPBS );
writel ( INTEL_TXPBS_I210, intel->regs + INTEL_TXPBS );
}
/* Always reset MAC. Required to reset the TX and RX rings. */
writel ( ( ctrl | INTEL_CTRL_RST ), intel->regs + INTEL_CTRL );
mdelay ( INTEL_RESET_DELAY_MS );
/* Set a sensible default configuration */
if ( ! ( intel->flags & INTEL_NO_ASDE ) )
ctrl |= INTEL_CTRL_ASDE;
ctrl |= INTEL_CTRL_SLU;
ctrl &= ~( INTEL_CTRL_LRST | INTEL_CTRL_FRCSPD | INTEL_CTRL_FRCDPLX );
writel ( ctrl, intel->regs + INTEL_CTRL );
mdelay ( INTEL_RESET_DELAY_MS );
/* On some models (notably ICH), the PHY reset mechanism
* appears to be broken. In particular, the PHY_CTRL register
* will be correctly loaded from NVM but the values will not
* be propagated to the "OEM bits" PHY register. This
* typically has the effect of dropping the link speed to
* 10Mbps.
*
* Work around this problem by skipping the PHY reset if
* either (a) the link is already up, or (b) this particular
* NIC is known to be broken.
*/
status = readl ( intel->regs + INTEL_STATUS );
if ( ( intel->flags & INTEL_NO_PHY_RST ) ||
( status & INTEL_STATUS_LU ) ) {
DBGC ( intel, "INTEL %p %sMAC reset (%08x/%08x was "
"%08x/%08x)\n", intel,
( ( intel->flags & INTEL_NO_PHY_RST ) ? "forced " : "" ),
ctrl, status, orig_ctrl, orig_status );
return 0;
}
/* Reset PHY and MAC simultaneously */
writel ( ( ctrl | INTEL_CTRL_RST | INTEL_CTRL_PHY_RST ),
intel->regs + INTEL_CTRL );
mdelay ( INTEL_RESET_DELAY_MS );
/* PHY reset is not self-clearing on all models */
writel ( ctrl, intel->regs + INTEL_CTRL );
mdelay ( INTEL_RESET_DELAY_MS );
status = readl ( intel->regs + INTEL_STATUS );
DBGC ( intel, "INTEL %p MAC+PHY reset (%08x/%08x was %08x/%08x)\n",
intel, ctrl, status, orig_ctrl, orig_status );
return 0;
}
/******************************************************************************
*
* Link state
*
******************************************************************************
*/
/**
* Check link state
*
* @v netdev Network device
*/
static void intel_check_link ( struct net_device *netdev ) {
struct intel_nic *intel = netdev->priv;
uint32_t status;
/* Read link status */
status = readl ( intel->regs + INTEL_STATUS );
DBGC ( intel, "INTEL %p link status is %08x\n", intel, status );
/* Update network device */
if ( status & INTEL_STATUS_LU ) {
netdev_link_up ( netdev );
} else {
netdev_link_down ( netdev );
}
}
/******************************************************************************
*
* Descriptors
*
******************************************************************************
*/
/**
* Populate transmit descriptor
*
* @v tx Transmit descriptor
* @v addr Data buffer address
* @v len Length of data
*/
void intel_describe_tx ( struct intel_descriptor *tx, physaddr_t addr,
size_t len ) {
/* Populate transmit descriptor */
tx->address = cpu_to_le64 ( addr );
tx->length = cpu_to_le16 ( len );
tx->flags = 0;
tx->command = ( INTEL_DESC_CMD_RS | INTEL_DESC_CMD_IFCS |
INTEL_DESC_CMD_EOP );
tx->status = 0;
}
/**
* Populate advanced transmit descriptor
*
* @v tx Transmit descriptor
* @v addr Data buffer address
* @v len Length of data
*/
void intel_describe_tx_adv ( struct intel_descriptor *tx, physaddr_t addr,
size_t len ) {
/* Populate advanced transmit descriptor */
tx->address = cpu_to_le64 ( addr );
tx->length = cpu_to_le16 ( len );
tx->flags = INTEL_DESC_FL_DTYP_DATA;
tx->command = ( INTEL_DESC_CMD_DEXT | INTEL_DESC_CMD_RS |
INTEL_DESC_CMD_IFCS | INTEL_DESC_CMD_EOP );
tx->status = cpu_to_le32 ( INTEL_DESC_STATUS_PAYLEN ( len ) );
}
/**
* Populate receive descriptor
*
* @v rx Receive descriptor
* @v addr Data buffer address
* @v len Length of data
*/
void intel_describe_rx ( struct intel_descriptor *rx, physaddr_t addr,
size_t len __unused ) {
/* Populate transmit descriptor */
rx->address = cpu_to_le64 ( addr );
rx->length = 0;
rx->status = 0;
}
/******************************************************************************
*
* Network device interface
*
******************************************************************************
*/
/**
* Disable descriptor ring
*
* @v intel Intel device
* @v reg Register block
* @ret rc Return status code
*/
static int intel_disable_ring ( struct intel_nic *intel, unsigned int reg ) {
uint32_t dctl;
unsigned int i;
/* Disable ring */
writel ( 0, ( intel->regs + reg + INTEL_xDCTL ) );
/* Wait for disable to complete */
for ( i = 0 ; i < INTEL_DISABLE_MAX_WAIT_MS ; i++ ) {
/* Check if ring is disabled */
dctl = readl ( intel->regs + reg + INTEL_xDCTL );
if ( ! ( dctl & INTEL_xDCTL_ENABLE ) )
return 0;
/* Delay */
mdelay ( 1 );
}
DBGC ( intel, "INTEL %p ring %05x timed out waiting for disable "
"(dctl %08x)\n", intel, reg, dctl );
return -ETIMEDOUT;
}
/**
* Reset descriptor ring
*
* @v intel Intel device
* @v reg Register block
* @ret rc Return status code
*/
void intel_reset_ring ( struct intel_nic *intel, unsigned int reg ) {
/* Disable ring. Ignore errors and continue to reset the ring anyway */
intel_disable_ring ( intel, reg );
/* Clear ring length */
writel ( 0, ( intel->regs + reg + INTEL_xDLEN ) );
/* Clear ring address */
writel ( 0, ( intel->regs + reg + INTEL_xDBAH ) );
writel ( 0, ( intel->regs + reg + INTEL_xDBAL ) );
/* Reset head and tail pointers */
writel ( 0, ( intel->regs + reg + INTEL_xDH ) );
writel ( 0, ( intel->regs + reg + INTEL_xDT ) );
}
/**
* Create descriptor ring
*
* @v intel Intel device
* @v ring Descriptor ring
* @ret rc Return status code
*/
int intel_create_ring ( struct intel_nic *intel, struct intel_ring *ring ) {
physaddr_t address;
uint32_t dctl;
/* Allocate descriptor ring. Align ring on its own size to
* prevent any possible page-crossing errors due to hardware
* errata.
*/
ring->desc = dma_alloc ( intel->dma, &ring->map, ring->len,
ring->len );
if ( ! ring->desc )
return -ENOMEM;
/* Initialise descriptor ring */
memset ( ring->desc, 0, ring->len );
/* Program ring address */
address = dma ( &ring->map, ring->desc );
writel ( ( address & 0xffffffffUL ),
( intel->regs + ring->reg + INTEL_xDBAL ) );
if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) {
writel ( ( ( ( uint64_t ) address ) >> 32 ),
( intel->regs + ring->reg + INTEL_xDBAH ) );
} else {
writel ( 0, intel->regs + ring->reg + INTEL_xDBAH );
}
/* Program ring length */
writel ( ring->len, ( intel->regs + ring->reg + INTEL_xDLEN ) );
/* Reset head and tail pointers */
writel ( 0, ( intel->regs + ring->reg + INTEL_xDH ) );
writel ( 0, ( intel->regs + ring->reg + INTEL_xDT ) );
/* Enable ring */
dctl = readl ( intel->regs + ring->reg + INTEL_xDCTL );
dctl |= INTEL_xDCTL_ENABLE;
writel ( dctl, intel->regs + ring->reg + INTEL_xDCTL );
DBGC ( intel, "INTEL %p ring %05x is at [%08lx,%08lx)\n",
intel, ring->reg, virt_to_phys ( ring->desc ),
( virt_to_phys ( ring->desc ) + ring->len ) );
return 0;
}
/**
* Destroy descriptor ring
*
* @v intel Intel device
* @v ring Descriptor ring
*/
void intel_destroy_ring ( struct intel_nic *intel, struct intel_ring *ring ) {
/* Reset ring */
intel_reset_ring ( intel, ring->reg );
/* Free descriptor ring */
dma_free ( &ring->map, ring->desc, ring->len );
ring->desc = NULL;
ring->prod = 0;
ring->cons = 0;
}
/**
* Refill receive descriptor ring
*
* @v intel Intel device
*/
void intel_refill_rx ( struct intel_nic *intel ) {
struct intel_descriptor *rx;
struct io_buffer *iobuf;
unsigned int rx_idx;
unsigned int rx_tail;
unsigned int refilled = 0;
/* Refill ring */
while ( ( intel->rx.prod - intel->rx.cons ) < INTEL_RX_FILL ) {
/* Allocate I/O buffer */
iobuf = alloc_rx_iob ( INTEL_RX_MAX_LEN, intel->dma );
if ( ! iobuf ) {
/* Wait for next refill */
break;
}
/* Get next receive descriptor */
rx_idx = ( intel->rx.prod++ % INTEL_NUM_RX_DESC );
rx = &intel->rx.desc[rx_idx];
/* Populate receive descriptor */
intel->rx.describe ( rx, iob_dma ( iobuf ), 0 );
/* Record I/O buffer */
assert ( intel->rx_iobuf[rx_idx] == NULL );
intel->rx_iobuf[rx_idx] = iobuf;
DBGC2 ( intel, "INTEL %p RX %d is [%lx,%lx)\n",
intel, rx_idx, virt_to_phys ( iobuf->data ),
( virt_to_phys ( iobuf->data ) + INTEL_RX_MAX_LEN ) );
refilled++;
}
/* Push descriptors to card, if applicable */
if ( refilled ) {
wmb();
rx_tail = ( intel->rx.prod % INTEL_NUM_RX_DESC );
profile_start ( &intel_vm_refill_profiler );
writel ( rx_tail, intel->regs + intel->rx.reg + INTEL_xDT );
profile_stop ( &intel_vm_refill_profiler );
profile_exclude ( &intel_vm_refill_profiler );
}
}
/**
* Discard unused receive I/O buffers
*
* @v intel Intel device
*/
void intel_empty_rx ( struct intel_nic *intel ) {
unsigned int i;
/* Discard unused receive buffers */
for ( i = 0 ; i < INTEL_NUM_RX_DESC ; i++ ) {
if ( intel->rx_iobuf[i] )
free_rx_iob ( intel->rx_iobuf[i] );
intel->rx_iobuf[i] = NULL;
}
}
/**
* Open network device
*
* @v netdev Network device
* @ret rc Return status code
*/
static int intel_open ( struct net_device *netdev ) {
struct intel_nic *intel = netdev->priv;
union intel_receive_address mac;
uint32_t fextnvm11;
uint32_t tctl;
uint32_t rctl;
int rc;
/* Set undocumented bit in FEXTNVM11 to work around an errata
* in i219 devices that will otherwise cause a complete
* datapath hang at the next device reset.
*/
if ( intel->flags & INTEL_RST_HANG ) {
DBGC ( intel, "INTEL %p WARNING: applying reset hang "
"workaround\n", intel );
fextnvm11 = readl ( intel->regs + INTEL_FEXTNVM11 );
fextnvm11 |= INTEL_FEXTNVM11_WTF;
writel ( fextnvm11, intel->regs + INTEL_FEXTNVM11 );
}
/* Create transmit descriptor ring */
if ( ( rc = intel_create_ring ( intel, &intel->tx ) ) != 0 )
goto err_create_tx;
/* Create receive descriptor ring */
if ( ( rc = intel_create_ring ( intel, &intel->rx ) ) != 0 )
goto err_create_rx;
/* Program MAC address */
memset ( &mac, 0, sizeof ( mac ) );
memcpy ( mac.raw, netdev->ll_addr, sizeof ( mac.raw ) );
writel ( le32_to_cpu ( mac.reg.low ), intel->regs + INTEL_RAL0 );
writel ( ( le32_to_cpu ( mac.reg.high ) | INTEL_RAH0_AV ),
intel->regs + INTEL_RAH0 );
/* Enable transmitter */
tctl = readl ( intel->regs + INTEL_TCTL );
tctl &= ~( INTEL_TCTL_CT_MASK | INTEL_TCTL_COLD_MASK );
tctl |= ( INTEL_TCTL_EN | INTEL_TCTL_PSP | INTEL_TCTL_CT_DEFAULT |
INTEL_TCTL_COLD_DEFAULT );
writel ( tctl, intel->regs + INTEL_TCTL );
/* Enable receiver */
rctl = readl ( intel->regs + INTEL_RCTL );
rctl &= ~( INTEL_RCTL_BSIZE_BSEX_MASK );
rctl |= ( INTEL_RCTL_EN | INTEL_RCTL_UPE | INTEL_RCTL_MPE |
INTEL_RCTL_BAM | INTEL_RCTL_BSIZE_2048 | INTEL_RCTL_SECRC );
writel ( rctl, intel->regs + INTEL_RCTL );
/* Fill receive ring */
intel_refill_rx ( intel );
/* Update link state */
intel_check_link ( netdev );
/* Apply required errata */
if ( intel->flags & INTEL_VMWARE ) {
DBGC ( intel, "INTEL %p applying VMware errata workaround\n",
intel );
intel->force_icr = INTEL_IRQ_RXT0;
}
return 0;
intel_destroy_ring ( intel, &intel->rx );
err_create_rx:
intel_destroy_ring ( intel, &intel->tx );
err_create_tx:
return rc;
}
/**
* Close network device
*
* @v netdev Network device
*/
static void intel_close ( struct net_device *netdev ) {
struct intel_nic *intel = netdev->priv;
/* Disable receiver */
writel ( 0, intel->regs + INTEL_RCTL );
/* Disable transmitter */
writel ( 0, intel->regs + INTEL_TCTL );
/* Destroy receive descriptor ring */
intel_destroy_ring ( intel, &intel->rx );
/* Discard any unused receive buffers */
intel_empty_rx ( intel );
/* Destroy transmit descriptor ring */
intel_destroy_ring ( intel, &intel->tx );
/* Reset the NIC, to flush the transmit and receive FIFOs */
intel_reset ( intel );
}
/**
* Transmit packet
*
* @v netdev Network device
* @v iobuf I/O buffer
* @ret rc Return status code
*/
int intel_transmit ( struct net_device *netdev, struct io_buffer *iobuf ) {
struct intel_nic *intel = netdev->priv;
struct intel_descriptor *tx;
unsigned int tx_idx;
unsigned int tx_tail;
size_t len;
/* Get next transmit descriptor */
if ( ( intel->tx.prod - intel->tx.cons ) >= INTEL_TX_FILL ) {
DBGC ( intel, "INTEL %p out of transmit descriptors\n", intel );
return -ENOBUFS;
}
tx_idx = ( intel->tx.prod++ % INTEL_NUM_TX_DESC );
tx_tail = ( intel->tx.prod % INTEL_NUM_TX_DESC );
tx = &intel->tx.desc[tx_idx];
/* Populate transmit descriptor */
len = iob_len ( iobuf );
intel->tx.describe ( tx, iob_dma ( iobuf ), len );
wmb();
/* Notify card that there are packets ready to transmit */
profile_start ( &intel_vm_tx_profiler );
writel ( tx_tail, intel->regs + intel->tx.reg + INTEL_xDT );
profile_stop ( &intel_vm_tx_profiler );
profile_exclude ( &intel_vm_tx_profiler );
DBGC2 ( intel, "INTEL %p TX %d is [%lx,%lx)\n",
intel, tx_idx, virt_to_phys ( iobuf->data ),
( virt_to_phys ( iobuf->data ) + len ) );
return 0;
}
/**
* Poll for completed packets
*
* @v netdev Network device
*/
void intel_poll_tx ( struct net_device *netdev ) {
struct intel_nic *intel = netdev->priv;
struct intel_descriptor *tx;
unsigned int tx_idx;
/* Check for completed packets */
while ( intel->tx.cons != intel->tx.prod ) {
/* Get next transmit descriptor */
tx_idx = ( intel->tx.cons % INTEL_NUM_TX_DESC );
tx = &intel->tx.desc[tx_idx];
/* Stop if descriptor is still in use */
if ( ! ( tx->status & cpu_to_le32 ( INTEL_DESC_STATUS_DD ) ) )
return;
DBGC2 ( intel, "INTEL %p TX %d complete\n", intel, tx_idx );
/* Complete TX descriptor */
netdev_tx_complete_next ( netdev );
intel->tx.cons++;
}
}
/**
* Poll for received packets
*
* @v netdev Network device
*/
void intel_poll_rx ( struct net_device *netdev ) {
struct intel_nic *intel = netdev->priv;
struct intel_descriptor *rx;
struct io_buffer *iobuf;
unsigned int rx_idx;
size_t len;
/* Check for received packets */
while ( intel->rx.cons != intel->rx.prod ) {
/* Get next receive descriptor */
rx_idx = ( intel->rx.cons % INTEL_NUM_RX_DESC );
rx = &intel->rx.desc[rx_idx];
/* Stop if descriptor is still in use */
if ( ! ( rx->status & cpu_to_le32 ( INTEL_DESC_STATUS_DD ) ) )
return;
/* Populate I/O buffer */
iobuf = intel->rx_iobuf[rx_idx];
intel->rx_iobuf[rx_idx] = NULL;
len = le16_to_cpu ( rx->length );
iob_put ( iobuf, len );
/* Hand off to network stack */
if ( rx->status & cpu_to_le32 ( INTEL_DESC_STATUS_RXE ) ) {
DBGC ( intel, "INTEL %p RX %d error (length %zd, "
"status %08x)\n", intel, rx_idx, len,
le32_to_cpu ( rx->status ) );
netdev_rx_err ( netdev, iobuf, -EIO );
} else {
DBGC2 ( intel, "INTEL %p RX %d complete (length %zd)\n",
intel, rx_idx, len );
netdev_rx ( netdev, iobuf );
}
intel->rx.cons++;
}
}
/**
* Poll for completed and received packets
*
* @v netdev Network device
*/
static void intel_poll ( struct net_device *netdev ) {
struct intel_nic *intel = netdev->priv;
uint32_t icr;
/* Check for and acknowledge interrupts */
profile_start ( &intel_vm_poll_profiler );
icr = readl ( intel->regs + INTEL_ICR );
profile_stop ( &intel_vm_poll_profiler );
profile_exclude ( &intel_vm_poll_profiler );
icr |= intel->force_icr;
if ( ! icr )
return;
/* Poll for TX completions, if applicable */
if ( icr & INTEL_IRQ_TXDW )
intel_poll_tx ( netdev );
/* Poll for RX completions, if applicable */
if ( icr & ( INTEL_IRQ_RXT0 | INTEL_IRQ_RXO ) )
intel_poll_rx ( netdev );
/* Report receive overruns */
if ( icr & INTEL_IRQ_RXO )
netdev_rx_err ( netdev, NULL, -ENOBUFS );
/* Check link state, if applicable */
if ( icr & INTEL_IRQ_LSC )
intel_check_link ( netdev );
/* Check for unexpected interrupts */
if ( icr & ~( INTEL_IRQ_TXDW | INTEL_IRQ_TXQE | INTEL_IRQ_LSC |
INTEL_IRQ_RXDMT0 | INTEL_IRQ_RXT0 | INTEL_IRQ_RXO ) ) {
DBGC ( intel, "INTEL %p unexpected ICR %08x\n", intel, icr );
/* Report as a TX error */
netdev_tx_err ( netdev, NULL, -ENOTSUP );
}
/* Refill RX ring */
intel_refill_rx ( intel );
}
/**
* Enable or disable interrupts
*
* @v netdev Network device
* @v enable Interrupts should be enabled
*/
static void intel_irq ( struct net_device *netdev, int enable ) {
struct intel_nic *intel = netdev->priv;
uint32_t mask;
mask = ( INTEL_IRQ_TXDW | INTEL_IRQ_LSC | INTEL_IRQ_RXT0 );
if ( enable ) {
writel ( mask, intel->regs + INTEL_IMS );
} else {
writel ( mask, intel->regs + INTEL_IMC );
}
}
/** Intel network device operations */
static struct net_device_operations intel_operations = {
.open = intel_open,
.close = intel_close,
.transmit = intel_transmit,
.poll = intel_poll,
.irq = intel_irq,
};
/******************************************************************************
*
* PCI interface
*
******************************************************************************
*/
/**
* Probe PCI device
*
* @v pci PCI device
* @ret rc Return status code
*/
static int intel_probe ( struct pci_device *pci ) {
struct net_device *netdev;
struct intel_nic *intel;
int rc;
/* Allocate and initialise net device */
netdev = alloc_etherdev ( sizeof ( *intel ) );
if ( ! netdev ) {
rc = -ENOMEM;
goto err_alloc;
}
netdev_init ( netdev, &intel_operations );
intel = netdev->priv;
pci_set_drvdata ( pci, netdev );
netdev->dev = &pci->dev;
memset ( intel, 0, sizeof ( *intel ) );
intel->port = PCI_FUNC ( pci->busdevfn );
intel->flags = pci->id->driver_data;
intel_init_ring ( &intel->tx, INTEL_NUM_TX_DESC, INTEL_TD,
intel_describe_tx );
intel_init_ring ( &intel->rx, INTEL_NUM_RX_DESC, INTEL_RD,
intel_describe_rx );
/* Fix up PCI device */
adjust_pci_device ( pci );
/* Map registers */
intel->regs = pci_ioremap ( pci, pci->membase, INTEL_BAR_SIZE );
if ( ! intel->regs ) {
rc = -ENODEV;
goto err_ioremap;
}
/* Configure DMA */
intel->dma = &pci->dma;
dma_set_mask_64bit ( intel->dma );
netdev->dma = intel->dma;
/* Reset the NIC */
if ( ( rc = intel_reset ( intel ) ) != 0 )
goto err_reset;
/* Fetch MAC address */
if ( ( rc = intel_fetch_mac ( intel, netdev->hw_addr ) ) != 0 )
goto err_fetch_mac;
/* Register network device */
if ( ( rc = register_netdev ( netdev ) ) != 0 )
goto err_register_netdev;
/* Set initial link state */
intel_check_link ( netdev );
return 0;
unregister_netdev ( netdev );