forked from fjballest/nix.markII
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathether8169.c
1238 lines (1083 loc) · 27.1 KB
/
ether8169.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
/*
* Realtek RTL8110S/8169S Gigabit Ethernet Controllers.
* Mostly there. There are some magic register values used
* which are not described in any datasheet or driver but seem
* to be necessary.
* No tuning has been done. Only tested on an RTL8110S, there
* are slight differences between the chips in the series so some
* tweaks may be needed.
*/
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "../port/error.h"
#include "../port/ethermii.h"
#include "../port/netif.h"
#include "etherif.h"
enum { /* registers */
Idr0 = 0x00, /* MAC address */
Mar0 = 0x08, /* Multicast address */
Dtccr = 0x10, /* Dump Tally Counter Command */
Tnpds = 0x20, /* Transmit Normal Priority Descriptors */
Thpds = 0x28, /* Transmit High Priority Descriptors */
Flash = 0x30, /* Flash Memory Read/Write */
Erbcr = 0x34, /* Early Receive Byte Count */
Ersr = 0x36, /* Early Receive Status */
Cr = 0x37, /* Command Register */
Tppoll = 0x38, /* Transmit Priority Polling */
Imr = 0x3C, /* Interrupt Mask */
Isr = 0x3E, /* Interrupt Status */
Tcr = 0x40, /* Transmit Configuration */
Rcr = 0x44, /* Receive Configuration */
Tctr = 0x48, /* Timer Count */
Mpc = 0x4C, /* Missed Packet Counter */
Cr9346 = 0x50, /* 9346 Command Register */
Config0 = 0x51, /* Configuration Register 0 */
Config1 = 0x52, /* Configuration Register 1 */
Config2 = 0x53, /* Configuration Register 2 */
Config3 = 0x54, /* Configuration Register 3 */
Config4 = 0x55, /* Configuration Register 4 */
Config5 = 0x56, /* Configuration Register 5 */
Timerint = 0x58, /* Timer Interrupt */
Mulint = 0x5C, /* Multiple Interrupt Select */
Phyar = 0x60, /* PHY Access */
Tbicsr0 = 0x64, /* TBI Control and Status */
Tbianar = 0x68, /* TBI Auto-Negotiation Advertisment */
Tbilpar = 0x6A, /* TBI Auto-Negotiation Link Partner */
Phystatus = 0x6C, /* PHY Status */
Rms = 0xDA, /* Receive Packet Maximum Size */
Cplusc = 0xE0, /* C+ Command */
Coal = 0xE2, /* Interrupt Mitigation (Coalesce) */
Rdsar = 0xE4, /* Receive Descriptor Start Address */
Etx = 0xEC, /* Early Transmit Threshold */
};
enum { /* Dtccr */
Cmd = 0x00000008, /* Command */
};
enum { /* Cr */
Te = 0x04, /* Transmitter Enable */
Re = 0x08, /* Receiver Enable */
Rst = 0x10, /* Software Reset */
};
enum { /* Tppoll */
Fswint = 0x01, /* Forced Software Interrupt */
Npq = 0x40, /* Normal Priority Queue polling */
Hpq = 0x80, /* High Priority Queue polling */
};
enum { /* Imr/Isr */
Rok = 0x0001, /* Receive OK */
Rer = 0x0002, /* Receive Error */
Tok = 0x0004, /* Transmit OK */
Ter = 0x0008, /* Transmit Error */
Rdu = 0x0010, /* Receive Descriptor Unavailable */
Punlc = 0x0020, /* Packet Underrun or Link Change */
Fovw = 0x0040, /* Receive FIFO Overflow */
Tdu = 0x0080, /* Transmit Descriptor Unavailable */
Swint = 0x0100, /* Software Interrupt */
Timeout = 0x4000, /* Timer */
Serr = 0x8000, /* System Error */
};
enum { /* Tcr */
MtxdmaSHIFT = 8, /* Max. DMA Burst Size */
MtxdmaMASK = 0x00000700,
Mtxdmaunlimited = 0x00000700,
Acrc = 0x00010000, /* Append CRC (not) */
Lbk0 = 0x00020000, /* Loopback Test 0 */
Lbk1 = 0x00040000, /* Loopback Test 1 */
Ifg2 = 0x00080000, /* Interframe Gap 2 */
HwveridSHIFT = 23, /* Hardware Version ID */
HwveridMASK = 0x7C800000,
Macv01 = 0x00000000, /* RTL8169 */
Macv02 = 0x00800000, /* RTL8169S/8110S */
Macv03 = 0x04000000, /* RTL8169S/8110S */
Macv04 = 0x10000000, /* RTL8169SB/8110SB */
Macv05 = 0x18000000, /* RTL8169SC/8110SC */
Macv07 = 0x24800000, /* RTL8102e */
Macv07a = 0x34800000, /* RTL8102e */
Macv11 = 0x30000000, /* RTL8168B/8111B */
Macv12 = 0x38000000, /* RTL8169B/8111B */
Macv12a = 0x3c000000, /* RTL8169C/8111C/8111C-GR (Macv19) */
Macv13 = 0x34000000, /* RTL8101E */
Macv14 = 0x30800000, /* RTL8100E */
Macv15 = 0x38800000, /* RTL8100E */
Macv25 = 0x28000000, /* RTL8168D */
Macv2c = 0x2c000000, /* RTL8168E */
Macv2ca = 0x2c800000, /* RTL8111E */
Ifg0 = 0x01000000, /* Interframe Gap 0 */
Ifg1 = 0x02000000, /* Interframe Gap 1 */
};
enum { /* Rcr */
Aap = 0x00000001, /* Accept All Packets */
Apm = 0x00000002, /* Accept Physical Match */
Am = 0x00000004, /* Accept Multicast */
Ab = 0x00000008, /* Accept Broadcast */
Ar = 0x00000010, /* Accept Runt */
Aer = 0x00000020, /* Accept Error */
Sel9356 = 0x00000040, /* 9356 EEPROM used */
MrxdmaSHIFT = 8, /* Max. DMA Burst Size */
MrxdmaMASK = 0x00000700,
Mrxdmaunlimited = 0x00000700,
RxfthSHIFT = 13, /* Receive Buffer Length */
RxfthMASK = 0x0000E000,
Rxfth256 = 0x00008000,
Rxfthnone = 0x0000E000,
Rer8 = 0x00010000, /* Accept Error Packets > 8 bytes */
MulERINT = 0x01000000, /* Multiple Early Interrupt Select */
};
enum { /* Cr9346 */
Eedo = 0x01, /* */
Eedi = 0x02, /* */
Eesk = 0x04, /* */
Eecs = 0x08, /* */
Eem0 = 0x40, /* Operating Mode */
Eem1 = 0x80,
};
enum { /* Phyar */
DataMASK = 0x0000FFFF, /* 16-bit GMII/MII Register Data */
DataSHIFT = 0,
RegaddrMASK = 0x001F0000, /* 5-bit GMII/MII Register Address */
RegaddrSHIFT = 16,
Flag = 0x80000000, /* */
};
enum { /* Phystatus */
Fd = 0x01, /* Full Duplex */
Linksts = 0x02, /* Link Status */
Speed10 = 0x04, /* */
Speed100 = 0x08, /* */
Speed1000 = 0x10, /* */
Rxflow = 0x20, /* */
Txflow = 0x40, /* */
Entbi = 0x80, /* */
};
enum { /* Cplusc */
Mulrw = 0x0008, /* PCI Multiple R/W Enable */
Dac = 0x0010, /* PCI Dual Address Cycle Enable */
Rxchksum = 0x0020, /* Receive Checksum Offload Enable */
Rxvlan = 0x0040, /* Receive VLAN De-tagging Enable */
Endian = 0x0200, /* Endian Mode */
};
typedef struct D D; /* Transmit/Receive Descriptor */
struct D {
u32int control;
u32int vlan;
u32int addrlo;
u32int addrhi;
};
enum { /* Transmit Descriptor control */
TxflMASK = 0x0000FFFF, /* Transmit Frame Length */
TxflSHIFT = 0,
Tcps = 0x00010000, /* TCP Checksum Offload */
Udpcs = 0x00020000, /* UDP Checksum Offload */
Ipcs = 0x00040000, /* IP Checksum Offload */
Lgsen = 0x08000000, /* Large Send */
};
enum { /* Receive Descriptor control */
RxflMASK = 0x00001FFF, /* Receive Frame Length */
RxflSHIFT = 0,
Tcpf = 0x00004000, /* TCP Checksum Failure */
Udpf = 0x00008000, /* UDP Checksum Failure */
Ipf = 0x00010000, /* IP Checksum Failure */
Pid0 = 0x00020000, /* Protocol ID0 */
Pid1 = 0x00040000, /* Protocol ID1 */
Crce = 0x00080000, /* CRC Error */
Runt = 0x00100000, /* Runt Packet */
Res = 0x00200000, /* Receive Error Summary */
Rwt = 0x00400000, /* Receive Watchdog Timer Expired */
Fovf = 0x00800000, /* FIFO Overflow */
Bovf = 0x01000000, /* Buffer Overflow */
Bar = 0x02000000, /* Broadcast Address Received */
Pam = 0x04000000, /* Physical Address Matched */
Mar = 0x08000000, /* Multicast Address Received */
};
enum { /* General Descriptor control */
Ls = 0x10000000, /* Last Segment Descriptor */
Fs = 0x20000000, /* First Segment Descriptor */
Eor = 0x40000000, /* End of Descriptor Ring */
Own = 0x80000000, /* Ownership */
};
/*
*/
enum { /* Ring sizes (<= 1024) */
Ntd = 32, /* Transmit Ring */
Nrd = 128, /* Receive Ring */
Mtu = ETHERMAXTU,
Mps = ROUNDUP(ETHERMAXTU+4, 128),
// Mps = Mtu + 8 + 14, /* if(mtu>ETHERMAXTU) */
};
typedef struct Dtcc Dtcc;
struct Dtcc {
u64int txok;
u64int rxok;
u64int txer;
u32int rxer;
u16int misspkt;
u16int fae;
u32int tx1col;
u32int txmcol;
u64int rxokph;
u64int rxokbrd;
u32int rxokmu;
u16int txabt;
u16int txundrn;
};
enum { /* Variants */
Rtl8100e = (0x8136<<16)|0x10EC, /* RTL810[01]E: pci -e */
Rtl8169c = (0x0116<<16)|0x16EC, /* RTL8169C+ (USR997902) */
Rtl8169sc = (0x8167<<16)|0x10EC, /* RTL8169SC */
Rtl8168b = (0x8168<<16)|0x10EC, /* RTL8168B: pci-e */
Rtl8169 = (0x8169<<16)|0x10EC, /* RTL8169 */
};
typedef struct Ctlr Ctlr;
typedef struct Ctlr {
int port;
Pcidev* pcidev;
Ctlr* next;
int active;
QLock alock; /* attach */
Lock ilock; /* init */
int init; /* */
int pciv; /* */
int macv; /* MAC version */
int phyv; /* PHY version */
int pcie; /* flag: pci-express device? */
uvlong mchash; /* multicast hash */
Mii* mii;
Lock tlock; /* transmit */
D* td; /* descriptor ring */
Block** tb; /* transmit buffers */
int ntd;
int tdh; /* head - producer index (host) */
int tdt; /* tail - consumer index (NIC) */
int ntdfree;
int ntq;
Lock rlock; /* receive */
D* rd; /* descriptor ring */
Block** rb; /* receive buffers */
int nrd;
int rdh; /* head - producer index (NIC) */
int rdt; /* tail - consumer index (host) */
int nrdfree;
int tcr; /* transmit configuration register */
int rcr; /* receive configuration register */
int imr;
QLock slock; /* statistics */
Dtcc* dtcc;
uint txdu;
uint tcpf;
uint udpf;
uint ipf;
uint fovf;
uint ierrs;
uint rer;
uint rdu;
uint punlc;
uint fovw;
uint mcast;
uint frag; /* partial packets; rb was too small */
} Ctlr;
static Ctlr* rtl8169ctlrhead;
static Ctlr* rtl8169ctlrtail;
#define csr8r(c, r) (inb((c)->port+(r)))
#define csr16r(c, r) (ins((c)->port+(r)))
#define csr32r(c, r) (inl((c)->port+(r)))
#define csr8w(c, r, b) (outb((c)->port+(r), (u8int)(b)))
#define csr16w(c, r, w) (outs((c)->port+(r), (u16int)(w)))
#define csr32w(c, r, l) (outl((c)->port+(r), (u32int)(l)))
static int
rtl8169miimir(Ctlr* ctlr, int pa, int ra)
{
uint r;
int timeo;
if(pa != 1)
return -1;
r = (ra<<16) & RegaddrMASK;
csr32w(ctlr, Phyar, r);
delay(1);
for(timeo = 0; timeo < 2000; timeo++){
if((r = csr32r(ctlr, Phyar)) & Flag)
break;
microdelay(100);
}
if(!(r & Flag))
return -1;
return (r & DataMASK)>>DataSHIFT;
}
static int
rtl8169miimiw(Ctlr* ctlr, int pa, int ra, int data)
{
uint r;
int timeo;
if(pa != 1)
return -1;
r = Flag|((ra<<16) & RegaddrMASK)|((data<<DataSHIFT) & DataMASK);
csr32w(ctlr, Phyar, r);
delay(1);
for(timeo = 0; timeo < 2000; timeo++){
if(!((r = csr32r(ctlr, Phyar)) & Flag))
break;
microdelay(100);
}
if(r & Flag)
return -1;
return 0;
}
static int
rtl8169miirw(Mii* mii, int write, int pa, int ra, int data)
{
if(write)
return rtl8169miimiw(mii->ctlr, pa, ra, data);
return rtl8169miimir(mii->ctlr, pa, ra);
}
static Mii*
rtl8169mii(Ctlr* ctlr)
{
Mii* mii;
MiiPhy *phy;
/*
* Link management.
*
* Get rev number out of Phyidr2 so can config properly.
* There's probably more special stuff for Macv0[234] needed here.
*/
ctlr->phyv = rtl8169miimir(ctlr, 1, Phyidr2) & 0x0F;
if(ctlr->macv == Macv02){
csr8w(ctlr, 0x82, 1); /* magic */
rtl8169miimiw(ctlr, 1, 0x0B, 0x0000); /* magic */
}
if((mii = miiattach(ctlr, (1<<1), rtl8169miirw)) == nil)
return nil;
phy = mii->curphy;
print("oui %#ux phyno %d, macv = %#8.8ux phyv = %#4.4ux\n",
phy->oui, phy->phyno, ctlr->macv, ctlr->phyv);
if(miistatus(mii) < 0){
miireset(mii);
miiane(mii, ~0, ~0, ~0);
}
return mii;
}
static void
rtl8169promiscuous(void* arg, int on)
{
Ether *edev;
Ctlr * ctlr;
edev = arg;
ctlr = edev->ctlr;
ilock(&ctlr->ilock);
if(on)
ctlr->rcr |= Aap;
else
ctlr->rcr &= ~Aap;
csr32w(ctlr, Rcr, ctlr->rcr);
iunlock(&ctlr->ilock);
}
enum {
/* everyone else uses 0x04c11db7, but they both produce the same crc */
Etherpolybe = 0x04c11db6,
Bytemask = (1<<8) - 1,
};
static ulong
ethercrcbe(uchar *addr, long len)
{
int i, j;
ulong c, crc, carry;
crc = ~0UL;
for (i = 0; i < len; i++) {
c = addr[i];
for (j = 0; j < 8; j++) {
carry = ((crc & (1UL << 31))? 1: 0) ^ (c & 1);
crc <<= 1;
c >>= 1;
if (carry)
crc = (crc ^ Etherpolybe) | carry;
}
}
return crc;
}
static ulong
swabl(ulong l)
{
return l>>24 | (l>>8) & (Bytemask<<8) |
(l<<8) & (Bytemask<<16) | l<<24;
}
static void
rtl8169multicast(void* ether, uchar *eaddr, int add)
{
Ether *edev;
Ctlr *ctlr;
if (!add)
return; /* ok to keep receiving on old mcast addrs */
edev = ether;
ctlr = edev->ctlr;
ilock(&ctlr->ilock);
ctlr->mchash |= 1ULL << (ethercrcbe(eaddr, Eaddrlen) >> 26);
ctlr->rcr |= Am;
csr32w(ctlr, Rcr, ctlr->rcr);
/* pci-e variants reverse the order of the hash byte registers */
if (ctlr->pcie) {
csr32w(ctlr, Mar0, swabl(ctlr->mchash>>32));
csr32w(ctlr, Mar0+4, swabl(ctlr->mchash));
} else {
csr32w(ctlr, Mar0, ctlr->mchash);
csr32w(ctlr, Mar0+4, ctlr->mchash>>32);
}
iunlock(&ctlr->ilock);
}
static long
rtl8169ifstat(Ether* edev, void* a, long n, ulong offset)
{
Ctlr *ctlr;
Dtcc *dtcc;
int timeo;
char *alloc, *e, *p;
ctlr = edev->ctlr;
qlock(&ctlr->slock);
alloc = nil;
if(waserror()){
qunlock(&ctlr->slock);
free(alloc);
nexterror();
}
csr32w(ctlr, Dtccr+4, 0);
csr32w(ctlr, Dtccr, PCIWADDR(ctlr->dtcc)|Cmd);
for(timeo = 0; timeo < 1000; timeo++){
if(!(csr32r(ctlr, Dtccr) & Cmd))
break;
delay(1);
}
if(csr32r(ctlr, Dtccr) & Cmd)
error(Eio);
dtcc = ctlr->dtcc;
edev->oerrs = dtcc->txer;
edev->crcs = dtcc->rxer;
edev->frames = dtcc->fae;
edev->buffs = dtcc->misspkt;
edev->overflows = ctlr->txdu+ctlr->rdu;
if(n == 0){
qunlock(&ctlr->slock);
poperror();
return 0;
}
if((alloc = malloc(READSTR)) == nil)
error(Enomem);
e = alloc+READSTR;
p = seprint(alloc, e, "TxOk: %llud\n", dtcc->txok);
p = seprint(p, e, "RxOk: %llud\n", dtcc->rxok);
p = seprint(p, e, "TxEr: %llud\n", dtcc->txer);
p = seprint(p, e, "RxEr: %ud\n", dtcc->rxer);
p = seprint(p, e, "MissPkt: %ud\n", dtcc->misspkt);
p = seprint(p, e, "FAE: %ud\n", dtcc->fae);
p = seprint(p, e, "Tx1Col: %ud\n", dtcc->tx1col);
p = seprint(p, e, "TxMCol: %ud\n", dtcc->txmcol);
p = seprint(p, e, "RxOkPh: %llud\n", dtcc->rxokph);
p = seprint(p, e, "RxOkBrd: %llud\n", dtcc->rxokbrd);
p = seprint(p, e, "RxOkMu: %ud\n", dtcc->rxokmu);
p = seprint(p, e, "TxAbt: %ud\n", dtcc->txabt);
p = seprint(p, e, "TxUndrn: %ud\n", dtcc->txundrn);
p = seprint(p, e, "txdu: %ud\n", ctlr->txdu);
p = seprint(p, e, "tcpf: %ud\n", ctlr->tcpf);
p = seprint(p, e, "udpf: %ud\n", ctlr->udpf);
p = seprint(p, e, "ipf: %ud\n", ctlr->ipf);
p = seprint(p, e, "fovf: %ud\n", ctlr->fovf);
p = seprint(p, e, "ierrs: %ud\n", ctlr->ierrs);
p = seprint(p, e, "rer: %ud\n", ctlr->rer);
p = seprint(p, e, "rdu: %ud\n", ctlr->rdu);
p = seprint(p, e, "punlc: %ud\n", ctlr->punlc);
p = seprint(p, e, "fovw: %ud\n", ctlr->fovw);
p = seprint(p, e, "tcr: %#8.8ux\n", ctlr->tcr);
p = seprint(p, e, "rcr: %#8.8ux\n", ctlr->rcr);
p = seprint(p, e, "multicast: %ud\n", ctlr->mcast);
if(ctlr->mii != nil && ctlr->mii->curphy != nil)
miidumpphy(ctlr->mii, p, e);
n = readstr(offset, a, n, alloc);
qunlock(&ctlr->slock);
poperror();
free(alloc);
return n;
}
static void
rtl8169halt(Ctlr* ctlr)
{
csr32w(ctlr, Timerint, 0);
csr8w(ctlr, Cr, 0);
csr16w(ctlr, Imr, 0);
csr16w(ctlr, Isr, ~0);
}
static int
rtl8169reset(Ctlr* ctlr)
{
u32int r;
int timeo;
/*
* Soft reset the controller.
*/
csr8w(ctlr, Cr, Rst);
for(r = timeo = 0; timeo < 1000; timeo++){
r = csr8r(ctlr, Cr);
if(!(r & Rst))
break;
delay(1);
}
rtl8169halt(ctlr);
if(r & Rst)
return -1;
return 0;
}
static void
rtl8169shutdown(Ether *ether)
{
rtl8169reset(ether->ctlr);
}
static void
rtl8169replenish(Ctlr* ctlr)
{
D *d;
int rdt;
Block *bp;
rdt = ctlr->rdt;
while(NEXT(rdt, ctlr->nrd) != ctlr->rdh){
d = &ctlr->rd[rdt];
if(ctlr->rb[rdt] == nil){
/*
* Simple allocation for now.
* This better be aligned on 8.
*/
bp = iallocb(Mps);
if(bp == nil){
iprint("no available buffers\n");
break;
}
ctlr->rb[rdt] = bp;
d->addrlo = PCIWADDR(bp->rp);
d->addrhi = 0;
coherence();
}
else
iprint("i8169: rx overrun\n");
d->control |= Own|Mps;
rdt = NEXT(rdt, ctlr->nrd);
ctlr->nrdfree++;
}
ctlr->rdt = rdt;
}
static int
rtl8169init(Ether* edev)
{
u32int r;
Ctlr *ctlr;
u8int cplusc;
ctlr = edev->ctlr;
ilock(&ctlr->ilock);
rtl8169reset(ctlr);
/*
* MAC Address is not settable on some (all?) chips.
* Must put chip into config register write enable mode.
*/
csr8w(ctlr, Cr9346, Eem1|Eem0);
/*
* Transmitter.
*/
memset(ctlr->td, 0, sizeof(D)*ctlr->ntd);
ctlr->tdh = ctlr->tdt = 0;
ctlr->td[ctlr->ntd-1].control = Eor;
/*
* Receiver.
* Need to do something here about the multicast filter.
*/
memset(ctlr->rd, 0, sizeof(D)*ctlr->nrd);
ctlr->nrdfree = ctlr->rdh = ctlr->rdt = 0;
ctlr->rd[ctlr->nrd-1].control = Eor;
rtl8169replenish(ctlr);
ctlr->rcr = Rxfthnone|Mrxdmaunlimited|Ab|Am|Apm;
/*
* Setting Mulrw in Cplusc disables the Tx/Rx DMA burst
* settings in Tcr/Rcr; the (1<<14) is magic.
*/
cplusc = csr16r(ctlr, Cplusc) & ~(1<<14);
cplusc |= /*Rxchksum|*/Mulrw;
switch(ctlr->macv){
default:
/*
* If it isn't recognised, assume it behaves
* like all the recent chips.
*/
print("rtl8169: unrecognised macv %#ux\n", ctlr->macv);
break;
case Macv01:
break;
case Macv02:
case Macv03:
cplusc |= (1<<14); /* magic */
break;
case Macv05:
/*
* This is interpreted from clearly bogus code
* in the manufacturer-supplied driver, it could
* be wrong. Untested.
*/
r = csr8r(ctlr, Config2) & 0x07;
if(r == 0x01) /* 66MHz PCI */
csr32w(ctlr, 0x7C, 0x0007FFFF); /* magic */
else
csr32w(ctlr, 0x7C, 0x0007FF00); /* magic */
pciclrmwi(ctlr->pcidev);
break;
case Macv13:
/*
* This is interpreted from clearly bogus code
* in the manufacturer-supplied driver, it could
* be wrong. Untested.
*/
pcicfgw8(ctlr->pcidev, 0x68, 0x00); /* magic */
pcicfgw8(ctlr->pcidev, 0x69, 0x08); /* magic */
break;
case Macv04:
case Macv07:
case Macv07a:
case Macv11:
case Macv12:
case Macv12a:
case Macv14:
case Macv15:
case Macv25:
case Macv2c:
case Macv2ca:
break;
}
/*
* Enable receiver/transmitter.
* Need to do this first or some of the settings below
* won't take.
*/
switch(ctlr->pciv){
default:
csr8w(ctlr, Cr, Te|Re);
csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
csr32w(ctlr, Rcr, ctlr->rcr);
csr32w(ctlr, Mar0, 0);
csr32w(ctlr, Mar0+4, 0);
ctlr->mchash = 0;
case Rtl8169sc:
case Rtl8168b:
break;
}
/*
* Interrupts.
* Disable Tdu|Tok for now, the transmit routine will tidy.
* Tdu means the NIC ran out of descriptors to send, so it
* doesn't really need to ever be on.
*/
csr32w(ctlr, Timerint, 0);
ctlr->imr = Serr|Timeout|Fovw|Punlc|Rdu|Ter|Rer|Rok;
csr16w(ctlr, Imr, ctlr->imr);
/*
* Clear missed-packet counter;
* clear early transmit threshold value;
* set the descriptor ring base addresses;
* set the maximum receive packet size;
* no early-receive interrupts.
*
* note: the maximum rx size is a filter. the size of the buffer
* in the descriptor ring is still honored. we will toss >Mtu
* packets because they've been fragmented into multiple
* rx buffers.
*/
csr32w(ctlr, Mpc, 0);
csr8w(ctlr, Etx, 0x3f); /* magic */
csr32w(ctlr, Tnpds+4, 0);
csr32w(ctlr, Tnpds, PCIWADDR(ctlr->td));
csr32w(ctlr, Rdsar+4, 0);
csr32w(ctlr, Rdsar, PCIWADDR(ctlr->rd));
csr16w(ctlr, Rms, 16383); /* was Mps; see above comment */
r = csr16r(ctlr, Mulint) & 0xF000; /* no early rx interrupts */
csr16w(ctlr, Mulint, r);
csr16w(ctlr, Cplusc, cplusc);
csr16w(ctlr, Coal, 0);
/*
* Set configuration.
*/
switch(ctlr->pciv){
default:
break;
case Rtl8169sc:
csr8w(ctlr, Cr, Te|Re);
csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
csr32w(ctlr, Rcr, ctlr->rcr);
break;
case Rtl8168b:
case Rtl8169c:
csr16w(ctlr, Cplusc, 0x2000); /* magic */
csr8w(ctlr, Cr, Te|Re);
csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
csr32w(ctlr, Rcr, ctlr->rcr);
break;
}
ctlr->tcr = csr32r(ctlr, Tcr);
csr8w(ctlr, Cr9346, 0);
iunlock(&ctlr->ilock);
// rtl8169mii(ctlr);
return 0;
}
static void
rtl8169attach(Ether* edev)
{
int timeo;
Ctlr *ctlr;
ctlr = edev->ctlr;
qlock(&ctlr->alock);
if(ctlr->init == 0){
ctlr->td = mallocalign(sizeof(D)*Ntd, 256, 0, 0);
ctlr->tb = malloc(Ntd*sizeof(Block*));
ctlr->ntd = Ntd;
ctlr->rd = mallocalign(sizeof(D)*Nrd, 256, 0, 0);
ctlr->rb = malloc(Nrd*sizeof(Block*));
ctlr->nrd = Nrd;
ctlr->dtcc = mallocalign(sizeof(Dtcc), 64, 0, 0);
if(ctlr->td == nil || ctlr->tb == nil || ctlr->rd == nil ||
ctlr->rb == nil || ctlr->dtcc == nil) {
free(ctlr->td);
free(ctlr->tb);
free(ctlr->rd);
free(ctlr->rb);
free(ctlr->dtcc);
qunlock(&ctlr->alock);
error(Enomem);
}
memset(ctlr->dtcc, 0, sizeof(Dtcc)); /* paranoia */
rtl8169init(edev);
ctlr->init = 1;
}
qunlock(&ctlr->alock);
/*
* Wait for link to be ready.
*/
for(timeo = 0; timeo < 350; timeo++){
if(miistatus(ctlr->mii) == 0)
break;
tsleep(&up->sleep, return0, 0, 10);
}
}
static void
rtl8169link(Ether* edev)
{
int limit;
Ctlr *ctlr;
MiiPhy *phy;
ctlr = edev->ctlr;
/*
* Maybe the link changed - do we care very much?
* Could stall transmits if no link, maybe?
*/
if(ctlr->mii == nil || ctlr->mii->curphy == nil)
return;
phy = ctlr->mii->curphy;
if(miistatus(ctlr->mii) < 0){
iprint("%slink n: speed %d fd %d link %d rfc %d tfc %d\n",
edev->name, phy->speed, phy->fd, phy->link,
phy->rfc, phy->tfc);
edev->link = 0;
return;
}
edev->link = 1;
limit = 256*1024;
if(phy->speed == 10){
edev->mbps = 10;
limit = 65*1024;
}
else if(phy->speed == 100)
edev->mbps = 100;
else if(phy->speed == 1000)
edev->mbps = 1000;
iprint("%slink y: speed %d fd %d link %d rfc %d tfc %d\n",
edev->name, phy->speed, phy->fd, phy->link,
phy->rfc, phy->tfc);
if(edev->oq != nil)
qsetlimit(edev->oq, limit);
}
static void
rtl8169transmit(Ether* edev)
{
D *d;
Block *bp;
Ctlr *ctlr;
int control, x;
ctlr = edev->ctlr;
ilock(&ctlr->tlock);
for(x = ctlr->tdh; ctlr->ntq > 0; x = NEXT(x, ctlr->ntd)){
d = &ctlr->td[x];
if((control = d->control) & Own)
break;
/*
* Check errors and log here.
*/
USED(control);
/*
* Free it up.
* Need to clean the descriptor here? Not really.
* Simple freeb for now (no chain and freeblist).
* Use ntq count for now.
*/
freeb(ctlr->tb[x]);
ctlr->tb[x] = nil;
d->control &= Eor;
ctlr->ntq--;
}
ctlr->tdh = x;
x = ctlr->tdt;
while(ctlr->ntq < (ctlr->ntd-1)){
if((bp = qget(edev->oq)) == nil)
break;
d = &ctlr->td[x];
d->addrlo = PCIWADDR(bp->rp);
d->addrhi = 0;
ctlr->tb[x] = bp;
coherence();
d->control |= Own|Fs|Ls|((BLEN(bp)<<TxflSHIFT) & TxflMASK);
x = NEXT(x, ctlr->ntd);
ctlr->ntq++;
}
if(x != ctlr->tdt){
ctlr->tdt = x;
csr8w(ctlr, Tppoll, Npq);
}
else if(ctlr->ntq >= (ctlr->ntd-1))
ctlr->txdu++;
iunlock(&ctlr->tlock);
}
static void
rtl8169receive(Ether* edev)
{
D *d;
int rdh;
Block *bp;
Ctlr *ctlr;
u32int control;
ctlr = edev->ctlr;
rdh = ctlr->rdh;
for(;;){
d = &ctlr->rd[rdh];
if(d->control & Own)
break;
control = d->control;
if((control & (Fs|Ls|Res)) == (Fs|Ls)){
bp = ctlr->rb[rdh];
ctlr->rb[rdh] = nil;
bp->wp = bp->rp + ((control & RxflMASK)>>RxflSHIFT)-4;
if(control & Fovf)
ctlr->fovf++;
if(control & Mar)
ctlr->mcast++;
switch(control & (Pid1|Pid0)){
default:
break;