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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
|
/*
* marvell kirkwood gigabit ethernet (88e1116 and 88e1121) driver
* (as found in the sheevaplug, openrd and guruplug).
* the main difference is the flavour of phy kludgery necessary.
*
* from /public/doc/marvell/88f61xx.kirkwood.pdf,
* /public/doc/marvell/88e1116.pdf, and
* /public/doc/marvell/88e1121r.pdf.
*/
#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/netif.h"
#include "etherif.h"
#include "ethermii.h"
#include "../ip/ip.h"
#define MIIDBG if(0)iprint
#define WINATTR(v) (((v) & MASK(8)) << 8)
#define WINSIZE(v) (((v)/(64*1024) - 1) << 16)
enum {
Nrx = 512,
Ntx = 32,
Nrxblks = 1024,
Rxblklen = 2+1522, /* ifc. supplies first 2 bytes as padding */
Maxrxintrsec = 20*1000, /* max. rx intrs. / sec */
Etherstuck = 70, /* must send or receive a packet in this many sec.s */
Descralign = 16,
Bufalign = 8,
Pass = 1, /* accept packets */
Qno = 0, /* do everything on queue zero */
};
typedef struct Ctlr Ctlr;
typedef struct Gbereg Gbereg;
typedef struct Mibstats Mibstats;
typedef struct Rx Rx;
typedef struct Tx Tx;
static struct {
Lock;
Block *head;
} freeblocks;
/* hardware receive buffer descriptor */
struct Rx {
ulong cs;
ulong countsize; /* bytes, buffer size */
ulong buf; /* phys. addr. of packet buffer */
ulong next; /* phys. addr. of next Rx */
};
/* hardware transmit buffer descriptor */
struct Tx {
ulong cs;
ulong countchk; /* bytes, checksum */
ulong buf; /* phys. addr. of packet buffer */
ulong next; /* phys. addr. of next Tx */
};
/* fixed by hw; part of Gberegs */
struct Mibstats {
union {
uvlong rxby; /* good bytes rcv'd */
struct {
ulong rxbylo;
ulong rxbyhi;
};
};
ulong badrxby; /* bad bytes rcv'd */
ulong mactxerr; /* tx err pkts */
ulong rxpkt; /* good pkts rcv'd */
ulong badrxpkt; /* bad pkts rcv'd */
ulong rxbcastpkt; /* b'cast pkts rcv'd */
ulong rxmcastpkt; /* m'cast pkts rcv'd */
ulong rx64; /* pkts <= 64 bytes */
ulong rx65_127; /* pkts 65—127 bytes */
ulong rx128_255; /* pkts 128—255 bytes */
ulong rx256_511; /* pkts 256—511 bytes */
ulong rx512_1023; /* pkts 512—1023 bytes */
ulong rx1024_max; /* pkts >= 1024 bytes */
union {
uvlong txby; /* good bytes sent */
struct {
ulong txbylo;
ulong txbyhi;
};
};
ulong txpkt; /* good pkts sent */
/* half-duplex: pkts dropped due to excessive collisions */
ulong txcollpktdrop;
ulong txmcastpkt; /* m'cast pkts sent */
ulong txbcastpkt; /* b'cast pkts sent */
ulong badmacctlpkts; /* bad mac ctl pkts */
ulong txflctl; /* flow-control pkts sent */
ulong rxflctl; /* good flow-control pkts rcv'd */
ulong badrxflctl; /* bad flow-control pkts rcv'd */
ulong rxundersized; /* runts */
ulong rxfrags; /* fragments rcv'd */
ulong rxtoobig; /* oversized pkts rcv'd */
ulong rxjabber; /* jabber pkts rcv'd */
ulong rxerr; /* rx error events */
ulong crcerr; /* crc error events */
ulong collisions; /* collision events */
ulong latecoll; /* late collisions */
};
struct Ctlr {
Lock;
Ether *ether;
Gbereg *reg;
Lock initlock;
int init;
Rx *rx; /* receive descriptors */
Block *rxb[Nrx]; /* blocks belonging to the descriptors */
int rxhead; /* descr ethernet will write to next */
int rxtail; /* next descr that might need a buffer */
Rendez rrendez; /* interrupt wakes up read process */
int haveinput;
Tx *tx;
Block *txb[Ntx];
int txhead; /* next descr we can use for new packet */
int txtail; /* next descr to reclaim on tx complete */
Mii *mii;
int port;
/* stats */
ulong intrs;
ulong newintrs;
ulong txunderrun;
ulong txringfull;
ulong rxdiscard;
ulong rxoverrun;
ulong nofirstlast;
Mibstats;
};
#define Rxqon(q) (1<<(q))
#define Txqon(q) (1<<(q))
enum {
/* euc bits */
Portreset = 1 << 20,
/* sdma config, sdc bits */
Burst1 = 0,
Burst2,
Burst4,
Burst8,
Burst16,
SDCrifb = 1<<0, /* rx intr on pkt boundaries */
#define SDCrxburst(v) ((v)<<1)
SDCrxnobyteswap = 1<<4,
SDCtxnobyteswap = 1<<5,
SDCswap64byte = 1<<6,
#define SDCtxburst(v) ((v)<<22)
/* rx intr ipg (inter packet gap) */
#define SDCipgintrx(v) ((((v)>>15) & 1)<<25) | (((v) & MASK(15))<<7)
/* portcfg bits */
PCFGupromisc = 1<<0, /* unicast promiscuous mode */
#define Rxqdefault(q) ((q)<<1)
#define Rxqarp(q) ((q)<<4)
PCFGbcrejectnoiparp = 1<<7,
PCFGbcrejectip = 1<<8,
PCFGbcrejectarp = 1<<9,
PCFGamnotxes = 1<<12, /* auto mode, no summary update on tx */
PCFGtcpq = 1<<14, /* capture tcp frames to tcpq */
PCFGudpq = 1<<15, /* capture udp frames to udpq */
#define Rxqtcp(q) ((q)<<16)
#define Rxqudp(q) ((q)<<19)
#define Rxqbpdu(q) ((q)<<22)
PCFGrxcs = 1<<25, /* rx tcp checksum mode with header */
/* portcfgx bits */
PCFGXspanq = 1<<1,
PCFGXcrcoff = 1<<2, /* no ethernet crc */
/* port serial control0, psc0 bits */
PSC0porton = 1<<0,
PSC0forcelinkup = 1<<1,
PSC0an_dplxoff = 1<<2, /* an_ = auto. negotiate */
PSC0an_flctloff = 1<<3,
PSC0an_pauseadv = 1<<4,
PSC0nofrclinkdown = 1<<10,
PSC0an_spdoff = 1<<13,
PSC0dteadv = 1<<14, /* dte advertise */
/* max. input pkt size */
#define PSC0mru(v) ((v)<<17)
PSC0mrumask = PSC0mru(MASK(3)),
PSC0mru1518 = 0, /* 1500+2* 6(addrs) +2 + 4(crc) */
PSC0mru1522, /* 1518 + 4(vlan tags) */
PSC0mru1552, /* `baby giant' */
PSC0mru9022, /* `jumbo' */
PSC0mru9192, /* bigger jumbo */
PSC0mru9700, /* still bigger jumbo */
PSC0fd_frc = 1<<21, /* force full duplex */
PSC0flctlfrc = 1<<22,
PSC0gmiispd_gbfrc = 1<<23,
PSC0miispdfrc100mbps = 1<<24,
/* port status 0, ps0 bits */
PS0linkup = 1<<1,
PS0fd = 1<<2, /* full duplex */
PS0flctl = 1<<3,
PS0gmii_gb = 1<<4,
PS0mii100mbps = 1<<5,
PS0txbusy = 1<<7,
PS0txfifoempty = 1<<10,
PS0rxfifo1empty = 1<<11,
PS0rxfifo2empty = 1<<12,
/* port serial control 1, psc1 bits */
PSC1loopback = 1<<1,
PSC1mii = 0<<2,
PSC1rgmii = 1<<3, /* enable RGMII */
PSC1portreset = 1<<4,
PSC1clockbypass = 1<<5,
PSC1iban = 1<<6,
PSC1iban_bypass = 1<<7,
PSC1iban_restart= 1<<8,
PSC1_gbonly = 1<<11,
PSC1encolonbp = 1<<15, /* "collision during back-pressure mib counting" */
PSC1coldomlimmask= MASK(6)<<16,
#define PSC1coldomlim(v) (((v) & MASK(6))<<16)
PSC1miiallowoddpreamble = 1<<22,
/* port status 1, ps1 bits */
PS1rxpause = 1<<0,
PS1txpause = 1<<1,
PS1pressure = 1<<2,
PS1syncfail10ms = 1<<3,
PS1an_done = 1<<4,
PS1inbandan_bypassed = 1<<5,
PS1serdesplllocked = 1<<6,
PS1syncok = 1<<7,
PS1nosquelch = 1<<8,
/* irq bits */
/* rx buf returned to cpu ownership, or frame reception finished */
Irx = 1<<0,
Iextend = 1<<1, /* IEsum of irqe set */
#define Irxbufferq(q) (1<<((q)+2)) /* rx buf returned to cpu ownership */
Irxerr = 1<<10, /* input ring full, usually */
#define Irxerrq(q) (1<<((q)+11))
#define Itxendq(q) (1<<((q)+19)) /* tx dma stopped for q */
Isum = 1<<31,
/* irq extended, irqe bits */
#define IEtxbufferq(q) (1<<((q)+0)) /* tx buf returned to cpu ownership */
#define IEtxerrq(q) (1<<((q)+8))
IEphystschg = 1<<16,
IEptp = 1<<17,
IErxoverrun = 1<<18,
IEtxunderrun = 1<<19,
IElinkchg = 1<<20,
IEintaddrerr = 1<<23,
IEprbserr = 1<<25,
IEsum = 1<<31,
/* tx fifo urgent threshold (tx interrupt coalescing), pxtfut */
#define TFUTipginttx(v) (((v) & MASK(16))<<4);
/* minimal frame size, mfs */
MFS40by = 10<<2,
MFS44by = 11<<2,
MFS48by = 12<<2,
MFS52by = 13<<2,
MFS56by = 14<<2,
MFS60by = 15<<2,
MFS64by = 16<<2,
/* receive descriptor status */
RCSmacerr = 1<<0,
RCSmacmask = 3<<1,
RCSmacce = 0<<1,
RCSmacor = 1<<1,
RCSmacmf = 2<<1,
RCSl4chkshift = 3,
RCSl4chkmask = MASK(16),
RCSvlan = 1<<17,
RCSbpdu = 1<<18,
RCSl4mask = 3<<21,
RCSl4tcp4 = 0<<21,
RCSl4udp4 = 1<<21,
RCSl4other = 2<<21,
RCSl4rsvd = 3<<21,
RCSl2ev2 = 1<<23,
RCSl3ip4 = 1<<24,
RCSip4headok = 1<<25,
RCSlast = 1<<26,
RCSfirst = 1<<27,
RCSunknownaddr = 1<<28,
RCSenableintr = 1<<29,
RCSl4chkok = 1<<30,
RCSdmaown = 1<<31,
/* transmit descriptor status */
TCSmacerr = 1<<0,
TCSmacmask = 3<<1,
TCSmaclc = 0<<1,
TCSmacur = 1<<1,
TCSmacrl = 2<<1,
TCSllc = 1<<9,
TCSl4chkmode = 1<<10,
TCSipv4hdlenshift= 11,
TCSvlan = 1<<15,
TCSl4type = 1<<16,
TCSgl4chk = 1<<17,
TCSgip4chk = 1<<18,
TCSpadding = 1<<19,
TCSlast = 1<<20,
TCSfirst = 1<<21,
TCSenableintr = 1<<23,
TCSautomode = 1<<30,
TCSdmaown = 1<<31,
};
enum {
/* SMI regs */
PhysmiTimeout = 10000, /* what units? in ms. */
Physmidataoff = 0, /* Data */
Physmidatamask = 0xffff<<Physmidataoff,
Physmiaddroff = 16, /* PHY device addr */
Physmiaddrmask = 0x1f << Physmiaddroff,
Physmiop = 26,
Physmiopmask = 3<<Physmiop,
PhysmiopWr = 0<<Physmiop,
PhysmiopRd = 1<<Physmiop,
PhysmiReadok = 1<<27,
PhysmiBusy = 1<<28,
SmiRegaddroff = 21, /* PHY device register addr */
SmiRegaddrmask = 0x1f << SmiRegaddroff,
};
struct Gbereg {
ulong phy; /* PHY address */
ulong smi; /* serial mgmt. interface */
ulong euda; /* ether default address */
ulong eudid; /* ether default id */
uchar _pad0[0x80-0x10];
/* dma stuff */
ulong euirq; /* interrupt cause */
ulong euirqmask; /* interrupt mask */
uchar _pad1[0x94-0x88];
ulong euea; /* error address */
ulong euiae; /* internal error address */
uchar _pad2[0xb0-0x9c];
ulong euc; /* control */
uchar _pad3[0x200-0xb4];
struct {
ulong base; /* window base */
ulong size; /* window size */
} base[6];
uchar _pad4[0x280-0x230];
ulong harr[4]; /* high address remap */
ulong bare; /* base address enable */
ulong epap; /* port access protect */
uchar _pad5[0x400-0x298];
ulong portcfg; /* port configuration */
ulong portcfgx; /* port config. extend */
ulong mii; /* mii serial parameters */
ulong _pad6;
ulong evlane; /* vlan ether type */
ulong macal; /* mac address low */
ulong macah; /* mac address high */
ulong sdc; /* sdma config. */
ulong dscp[7]; /* ip diff. serv. code point -> pri */
ulong psc0; /* port serial control 0 */
ulong vpt2p; /* vlan priority tag -> pri */
ulong ps0; /* ether port status 0 */
ulong tqc; /* transmit queue command */
ulong psc1; /* port serial control 1 */
ulong ps1; /* ether port status 1 */
ulong mvhdr; /* marvell header */
ulong _pad8[2];
/* interrupts */
ulong irq; /* interrupt cause; some rw0c bits */
ulong irqe; /* " " extended; some rw0c bits */
ulong irqmask; /* interrupt mask (actually enable) */
ulong irqemask; /* " " extended */
ulong _pad9;
ulong pxtfut; /* port tx fifo urgent threshold */
ulong _pad10;
ulong pxmfs; /* port rx minimum frame size */
ulong _pad11;
/*
* # of input frames discarded by addr filtering or lack of resources;
* zeroed upon read.
*/
ulong pxdfc; /* port rx discard frame counter */
ulong pxofc; /* port overrun frame counter */
ulong _pad12[2];
ulong piae; /* port internal address error */
uchar _pad13[0x4bc-0x498];
ulong etherprio; /* ether type priority */
uchar _pad14[0x4dc-0x4c0];
ulong tqfpc; /* tx queue fixed priority config. */
ulong pttbrc; /* port tx token-bucket rate config. */
ulong tqc1; /* tx queue command 1 */
ulong pmtu; /* port maximum transmit unit */
ulong pmtbs; /* port maximum token bucket size */
uchar _pad15[0x600-0x4f0];
struct {
ulong _pad[3];
ulong r; /* phys. addr.: cur. rx desc. ptrs */
} crdp[8];
ulong rqc; /* rx queue command */
ulong tcsdp; /* phys. addr.: cur. tx desc. ptr */
uchar _pad16[0x6c0-0x688];
ulong tcqdp[8]; /* phys. addr.: cur. tx q. desc. ptr */
uchar _pad17[0x700-0x6e0];
struct {
ulong tbctr; /* queue tx token-bucket counter */
ulong tbcfg; /* tx queue token-bucket config. */
ulong acfg; /* tx queue arbiter config. */
ulong _pad;
} tq[8];
ulong pttbc; /* port tx token-bucket counter */
uchar _pad18[0x7a8-0x784];
ulong ipg2; /* tx queue ipg */
ulong _pad19[3];
ulong ipg3;
ulong _pad20;
ulong htlp; /* high token in low packet */
ulong htap; /* high token in async packet */
ulong ltap; /* low token in async packet */
ulong _pad21;
ulong ts; /* tx speed */
uchar _pad22[0x1000-0x7d4];
/* mac mib counters: statistics */
Mibstats;
uchar _pad23[0x1400-0x1080];
/* multicast filtering; each byte: Qno<<1 | Pass */
ulong dfsmt[64]; /* dest addr filter special m'cast table */
ulong dfomt[64]; /* dest addr filter other m'cast table */
/* unicast filtering */
ulong dfut[4]; /* dest addr filter unicast table */
};
static Ctlr *ctlrs[MaxEther];
static uchar zeroea[Eaddrlen];
static void getmibstats(Ctlr *);
static void
rxfreeb(Block *b)
{
b->wp = b->rp =
(uchar*)((uintptr)(b->lim - Rxblklen) & ~(Bufalign - 1));
assert(((uintptr)b->rp & (Bufalign - 1)) == 0);
b->free = rxfreeb;
ilock(&freeblocks);
b->next = freeblocks.head;
freeblocks.head = b;
iunlock(&freeblocks);
}
static Block *
rxallocb(void)
{
Block *b;
ilock(&freeblocks);
b = freeblocks.head;
if(b != nil) {
freeblocks.head = b->next;
b->next = nil;
b->free = rxfreeb;
}
iunlock(&freeblocks);
return b;
}
static void
rxkick(Ctlr *ctlr)
{
Gbereg *reg = ctlr->reg;
if (reg->crdp[Qno].r == 0)
reg->crdp[Qno].r = PADDR(&ctlr->rx[ctlr->rxhead]);
if ((reg->rqc & 0xff) == 0) /* all queues are stopped? */
reg->rqc = Rxqon(Qno); /* restart */
coherence();
}
static void
txkick(Ctlr *ctlr)
{
Gbereg *reg = ctlr->reg;
if (reg->tcqdp[Qno] == 0)
reg->tcqdp[Qno] = PADDR(&ctlr->tx[ctlr->txhead]);
if ((reg->tqc & 0xff) == 0) /* all q's stopped? */
reg->tqc = Txqon(Qno); /* restart */
coherence();
}
static void
rxreplenish(Ctlr *ctlr)
{
Rx *r;
Block *b;
while(ctlr->rxb[ctlr->rxtail] == nil) {
b = rxallocb();
if(b == nil) {
iprint("#l%d: rxreplenish out of buffers\n",
ctlr->ether->ctlrno);
break;
}
ctlr->rxb[ctlr->rxtail] = b;
/* set up uncached receive descriptor */
r = &ctlr->rx[ctlr->rxtail];
assert(((uintptr)r & (Descralign - 1)) == 0);
r->countsize = ROUNDUP(Rxblklen, 8);
r->buf = PADDR(b->rp);
coherence();
/* and fire */
r->cs = RCSdmaown | RCSenableintr;
coherence();
ctlr->rxtail = NEXT(ctlr->rxtail, Nrx);
}
}
static void
dump(uchar *bp, long max)
{
if (max > 64)
max = 64;
for (; max > 0; max--, bp++)
iprint("%02.2ux ", *bp);
print("...\n");
}
static void
etheractive(Ether *ether)
{
ether->starttime = TK2MS(MACHP(0)->ticks)/1000;
}
static void
ethercheck(Ether *ether)
{
if (ether->starttime != 0 &&
TK2MS(MACHP(0)->ticks)/1000 - ether->starttime > Etherstuck) {
etheractive(ether);
if (ether->ctlrno == 0) /* only complain about main ether */
iprint("#l%d: ethernet stuck\n", ether->ctlrno);
}
}
static void
receive(Ether *ether)
{
int i;
ulong n;
Block *b;
Ctlr *ctlr = ether->ctlr;
Rx *r;
ethercheck(ether);
for (i = Nrx-2; i > 0; i--) {
r = &ctlr->rx[ctlr->rxhead]; /* *r is uncached */
assert(((uintptr)r & (Descralign - 1)) == 0);
if(r->cs & RCSdmaown) /* descriptor busy? */
break;
b = ctlr->rxb[ctlr->rxhead]; /* got input buffer? */
if (b == nil)
panic("ether1116: nil ctlr->rxb[ctlr->rxhead] "
"in receive");
ctlr->rxb[ctlr->rxhead] = nil;
ctlr->rxhead = NEXT(ctlr->rxhead, Nrx);
if((r->cs & (RCSfirst|RCSlast)) != (RCSfirst|RCSlast)) {
ctlr->nofirstlast++; /* partial packet */
freeb(b);
continue;
}
if(r->cs & RCSmacerr) {
freeb(b);
continue;
}
n = r->countsize >> 16; /* TODO includes 2 pad bytes? */
assert(n >= 2 && n < 2048);
/* clear any cached packet or part thereof */
l2cacheuinvse(b->rp, n+2);
cachedinvse(b->rp, n+2);
b->wp = b->rp + n;
/*
* skip hardware padding intended to align ipv4 address
* in memory (mv-s104860-u0 §8.3.4.1)
*/
b->rp += 2;
etheriq(ether, b, 1);
etheractive(ether);
if (i % (Nrx / 2) == 0) {
rxreplenish(ctlr);
rxkick(ctlr);
}
}
rxreplenish(ctlr);
rxkick(ctlr);
}
static void
txreplenish(Ether *ether) /* free transmitted packets */
{
Ctlr *ctlr;
ctlr = ether->ctlr;
while(ctlr->txtail != ctlr->txhead) {
/* ctlr->tx is uncached */
if(ctlr->tx[ctlr->txtail].cs & TCSdmaown)
break;
if(ctlr->txb[ctlr->txtail] == nil)
panic("no block for sent packet?!");
freeb(ctlr->txb[ctlr->txtail]);
ctlr->txb[ctlr->txtail] = nil;
ctlr->txtail = NEXT(ctlr->txtail, Ntx);
etheractive(ether);
}
}
/*
* transmit strategy: fill the output ring as far as possible,
* perhaps leaving a few spare; kick off the output and take
* an interrupt only when the transmit queue is empty.
*/
static void
transmit(Ether *ether)
{
int i, kick, len;
Block *b;
Ctlr *ctlr = ether->ctlr;
Gbereg *reg = ctlr->reg;
Tx *t;
ethercheck(ether);
ilock(ctlr);
txreplenish(ether); /* reap old packets */
/* queue new packets; use at most half the tx descs to avoid livelock */
kick = 0;
for (i = Ntx/2 - 2; i > 0; i--) {
t = &ctlr->tx[ctlr->txhead]; /* *t is uncached */
assert(((uintptr)t & (Descralign - 1)) == 0);
if(t->cs & TCSdmaown) { /* descriptor busy? */
ctlr->txringfull++;
break;
}
b = qget(ether->oq); /* outgoing packet? */
if (b == nil)
break;
len = BLEN(b);
if(len < ether->minmtu || len > ether->maxmtu) {
freeb(b);
continue;
}
ctlr->txb[ctlr->txhead] = b;
/* make sure the whole packet is in memory */
cachedwbse(b->rp, len);
l2cacheuwbse(b->rp, len);
/* set up the transmit descriptor */
t->buf = PADDR(b->rp);
t->countchk = len << 16;
coherence();
/* and fire */
t->cs = TCSpadding | TCSfirst | TCSlast | TCSdmaown |
TCSenableintr;
coherence();
kick++;
ctlr->txhead = NEXT(ctlr->txhead, Ntx);
}
if (kick) {
txkick(ctlr);
reg->irqmask |= Itxendq(Qno);
reg->irqemask |= IEtxerrq(Qno) | IEtxunderrun;
}
iunlock(ctlr);
}
static void
dumprxdescs(Ctlr *ctlr)
{
int i;
Gbereg *reg = ctlr->reg;
iprint("\nrxhead %d rxtail %d; txcdp %#p rxcdp %#p\n",
ctlr->rxhead, ctlr->rxtail, reg->tcqdp[Qno], reg->crdp[Qno].r);
for (i = 0; i < Nrx; i++) {
iprint("rxb %d @ %#p: %#p\n", i, &ctlr->rxb[i], ctlr->rxb[i]);
delay(50);
}
for (i = 0; i < Nrx; i++) {
iprint("rx %d @ %#p: cs %#lux countsize %lud buf %#lux next %#lux\n",
i, &ctlr->rx[i], ctlr->rx[i].cs,
ctlr->rx[i].countsize >> 3, ctlr->rx[i].buf,
ctlr->rx[i].next);
delay(50);
}
delay(1000);
}
static int
gotinput(void* ctlr)
{
return ((Ctlr*)ctlr)->haveinput != 0;
}
/*
* process any packets in the input ring.
* also sum mib stats frequently to avoid the overflow
* mentioned in the errata.
*/
static void
rcvproc(void* arg)
{
Ctlr *ctlr;
Ether *ether;
ether = arg;
ctlr = ether->ctlr;
while(waserror())
;
for(;;){
tsleep(&ctlr->rrendez, gotinput, ctlr, 10*1000);
ilock(ctlr);
getmibstats(ctlr);
if (ctlr->haveinput) {
ctlr->haveinput = 0;
iunlock(ctlr);
receive(ether);
} else
iunlock(ctlr);
}
}
static void
interrupt(Ureg*, void *arg)
{
ulong irq, irqe, handled;
Ether *ether = arg;
Ctlr *ctlr = ether->ctlr;
Gbereg *reg = ctlr->reg;
handled = 0;
irq = reg->irq;
irqe = reg->irqe;
reg->irqe = 0; /* extinguish intr causes */
reg->irq = 0; /* extinguish intr causes */
ethercheck(ether);
if(irq & (Irx | Irxbufferq(Qno))) {
/*
* letting a kproc process the input takes far less real time
* than doing it all at interrupt level.
*/
ctlr->haveinput = 1;
wakeup(&ctlr->rrendez);
irq &= ~(Irx | Irxbufferq(Qno));
handled++;
} else
rxkick(ctlr);
if(irq & Itxendq(Qno)) { /* transmit ring empty? */
reg->irqmask &= ~Itxendq(Qno); /* prevent more interrupts */
reg->irqemask &= ~(IEtxerrq(Qno) | IEtxunderrun);
transmit(ether);
irq &= ~Itxendq(Qno);
handled++;
}
if(irqe & IEsum) {
/*
* IElinkchg appears to only be set when unplugging.
* autonegotiation is likely not done yet, so linkup not valid,
* thus we note the link change here, and check for
* that and autonegotiation done below.
*/
if(irqe & IEphystschg) {
ether->link = (reg->ps0 & PS0linkup) != 0;
ether->linkchg = 1;
}
if(irqe & IEtxerrq(Qno))
ether->oerrs++;
if(irqe & IErxoverrun)
ether->overflows++;
if(irqe & IEtxunderrun)
ctlr->txunderrun++;
if(irqe & (IEphystschg | IEtxerrq(Qno) | IErxoverrun |
IEtxunderrun))
handled++;
}
if (irq & Isum) {
if (irq & Irxerr) { /* nil desc. ptr. or desc. owned by cpu */
ether->buffs++; /* approx. error */
/* if the input ring is full, drain it */
ctlr->haveinput = 1;
wakeup(&ctlr->rrendez);
}
if(irq & (Irxerr | Irxerrq(Qno)))
handled++;
irq &= ~(Irxerr | Irxerrq(Qno));
}
if(ether->linkchg && (reg->ps1 & PS1an_done)) {
handled++;
ether->link = (reg->ps0 & PS0linkup) != 0;
ether->linkchg = 0;
}
ctlr->newintrs++;
if (!handled) {
irq &= ~Isum;
irqe &= ~IEtxbufferq(Qno);
if (irq == 0 && irqe == 0) {
/* seems to be triggered by continuous output */
// iprint("ether1116: spurious interrupt\n");
} else
iprint("ether1116: interrupt cause unknown; "
"irq %#lux irqe %#lux\n", irq, irqe);
}
intrclear(Irqlo, ether->irq);
}
void
promiscuous(void *arg, int on)
{
Ether *ether = arg;
Ctlr *ctlr = ether->ctlr;
Gbereg *reg = ctlr->reg;
ilock(ctlr);
ether->prom = on;
if(on)
reg->portcfg |= PCFGupromisc;
else
reg->portcfg &= ~PCFGupromisc;
iunlock(ctlr);
}
void
multicast(void *, uchar *, int)
{
/* nothing to do; we always accept multicast */
}
static void quiesce(Gbereg *reg);
static void
shutdown(Ether *ether)
{
int i;
Ctlr *ctlr = ether->ctlr;
Gbereg *reg = ctlr->reg;
ilock(ctlr);
quiesce(reg);
reg->euc |= Portreset;
coherence();
iunlock(ctlr);
delay(100);
ilock(ctlr);
reg->euc &= ~Portreset;
coherence();
delay(20);
reg->psc0 = 0; /* no PSC0porton */
reg->psc1 |= PSC1portreset;
coherence();
delay(50);
reg->psc1 &= ~PSC1portreset;
coherence();
for (i = 0; i < nelem(reg->tcqdp); i++)
reg->tcqdp[i] = 0;
for (i = 0; i < nelem(reg->crdp); i++)
reg->crdp[i].r = 0;
coherence();
iunlock(ctlr);
}
enum {
CMjumbo,
};
static Cmdtab ctlmsg[] = {
CMjumbo, "jumbo", 2,
};
long
ctl(Ether *e, void *p, long n)
{
Cmdbuf *cb;
Cmdtab *ct;
Ctlr *ctlr = e->ctlr;
Gbereg *reg = ctlr->reg;
cb = parsecmd(p, n);
if(waserror()) {
free(cb);
nexterror();
}
ct = lookupcmd(cb, ctlmsg, nelem(ctlmsg));
switch(ct->index) {
case CMjumbo:
if(strcmp(cb->f[1], "on") == 0) {
/* incoming packet queue doesn't expect jumbo frames */
error("jumbo disabled");
reg->psc0 = (reg->psc0 & ~PSC0mrumask) |
PSC0mru(PSC0mru9022);
e->maxmtu = 9022;
} else if(strcmp(cb->f[1], "off") == 0) {
reg->psc0 = (reg->psc0 & ~PSC0mrumask) |
PSC0mru(PSC0mru1522);
e->maxmtu = ETHERMAXTU;
} else
error(Ebadctl);
break;
default:
error(Ebadctl);
break;
}
free(cb);
poperror();
return n;
}
/*
* phy/mii goo
*/
static int
smibusywait(Gbereg *reg, ulong waitbit)
{
ulong timeout, smi_reg;
timeout = PhysmiTimeout;
/* wait till the SMI is not busy */
do {
/* read smi register */
smi_reg = reg->smi;
if (timeout-- == 0) {
MIIDBG("SMI busy timeout\n");
return -1;
}
// delay(1);
} while (smi_reg & waitbit);
return 0;
}
static int
miird(Mii *mii, int pa, int ra)
{
ulong smi_reg, timeout;
Gbereg *reg;
reg = ((Ctlr*)mii->ctlr)->reg;
/* check params */
if ((pa<<Physmiaddroff) & ~Physmiaddrmask ||
(ra<<SmiRegaddroff) & ~SmiRegaddrmask)
return -1;
smibusywait(reg, PhysmiBusy);
/* fill the phy address and register offset and read opcode */
reg->smi = pa << Physmiaddroff | ra << SmiRegaddroff | PhysmiopRd;
coherence();
/* wait til read value is ready */
timeout = PhysmiTimeout;
do {
smi_reg = reg->smi;
if (timeout-- == 0) {
MIIDBG("SMI read-valid timeout\n");
return -1;
}
} while (!(smi_reg & PhysmiReadok));
/* Wait for the data to update in the SMI register */
for (timeout = 0; timeout < PhysmiTimeout; timeout++)
;
return reg->smi & Physmidatamask;
}
static int
miiwr(Mii *mii, int pa, int ra, int v)
{
Gbereg *reg;
ulong smi_reg;
reg = ((Ctlr*)mii->ctlr)->reg;
/* check params */
if (((pa<<Physmiaddroff) & ~Physmiaddrmask) ||
((ra<<SmiRegaddroff) & ~SmiRegaddrmask))
return -1;
smibusywait(reg, PhysmiBusy);
/* fill the phy address and register offset and read opcode */
smi_reg = v << Physmidataoff | pa << Physmiaddroff | ra << SmiRegaddroff;
reg->smi = smi_reg & ~PhysmiopRd;
coherence();
return 0;
}
#define MIIMODEL(idr2) (((idr2) >> 4) & MASK(6))
enum {
Hacknone,
Hackdual,
Ouimarvell = 0x005043,
/* idr2 mii/phy model numbers */
Phy1000 = 0x00, /* 88E1000 Gb */
Phy1011 = 0x02, /* 88E1011 Gb */
Phy1000_3 = 0x03, /* 88E1000 Gb */
Phy1000s = 0x04, /* 88E1000S Gb */
Phy1000_5 = 0x05, /* 88E1000 Gb */
Phy1000_6 = 0x06, /* 88E1000 Gb */
Phy3082 = 0x08, /* 88E3082 10/100 */
Phy1112 = 0x09, /* 88E1112 Gb */
Phy1121r = 0x0b, /* says the 1121r manual */
Phy1149 = 0x0b, /* 88E1149 Gb */
Phy1111 = 0x0c, /* 88E1111 Gb */
Phy1116 = 0x21, /* 88E1116 Gb */
Phy1116r = 0x24, /* 88E1116R Gb */
Phy1118 = 0x22, /* 88E1118 Gb */
Phy3016 = 0x26, /* 88E3016 10/100 */
};
static int hackflavour;
/*
* on openrd, ether0's phy has address 8, ether1's is ether0's 24.
* on guruplug, ether0's is phy 0 and ether1's is ether0's phy 1.
*/
int
mymii(Mii* mii, int mask)
{
Ctlr *ctlr;
MiiPhy *miiphy;
int bit, ctlrno, oui, model, phyno, r, rmask;
static int dualport, phyidx;
static int phynos[NMiiPhy];
ctlr = mii->ctlr;
ctlrno = ctlr->ether->ctlrno;
/* first pass: figure out what kind of phy(s) we have. */
dualport = 0;
if (ctlrno == 0) {
for(phyno = 0; phyno < NMiiPhy; phyno++){
bit = 1<<phyno;
if(!(mask & bit) || mii->mask & bit)
continue;
if(mii->mir(mii, phyno, Bmsr) == -1)
continue;
r = mii->mir(mii, phyno, Phyidr1);
oui = (r & 0x3FFF)<<6;
r = mii->mir(mii, phyno, Phyidr2);
oui |= r>>10;
model = MIIMODEL(r);
if (oui == 0xfffff && model == 0x3f)
continue;
MIIDBG("ctlrno %d phy %d oui %#ux model %#ux\n",
ctlrno, phyno, oui, model);
if (oui == Ouimarvell &&
(model == Phy1121r || model == Phy1116r))
++dualport;
phynos[phyidx++] = phyno;
}
hackflavour = dualport == 2 && phyidx == 2? Hackdual: Hacknone;
MIIDBG("ether1116: %s-port phy\n",
hackflavour == Hackdual? "dual": "single");
}
/*
* Probe through mii for PHYs in mask;
* return the mask of those found in the current probe.
* If the PHY has not already been probed, update
* the Mii information.
*/
rmask = 0;
if (hackflavour == Hackdual && ctlrno < phyidx) {
/*
* openrd, guruplug or the like: use ether0's phys.
* this is a nasty hack, but so is the hardware.
*/
MIIDBG("ctlrno %d using ctlrno 0's phyno %d\n",
ctlrno, phynos[ctlrno]);
ctlr->mii = mii = ctlrs[0]->mii;
mask = 1 << phynos[ctlrno];
mii->mask = ~mask;
}
for(phyno = 0; phyno < NMiiPhy; phyno++){
bit = 1<<phyno;
if(!(mask & bit))
continue;
if(mii->mask & bit){
rmask |= bit;
continue;
}
if(mii->mir(mii, phyno, Bmsr) == -1)
continue;
r = mii->mir(mii, phyno, Phyidr1);
oui = (r & 0x3FFF)<<6;
r = mii->mir(mii, phyno, Phyidr2);
oui |= r>>10;
if(oui == 0xFFFFF || oui == 0)
continue;
if((miiphy = malloc(sizeof(MiiPhy))) == nil)
continue;
miiphy->mii = mii;
miiphy->oui = oui;
miiphy->phyno = phyno;
miiphy->anar = ~0;
miiphy->fc = ~0;
miiphy->mscr = ~0;
mii->phy[phyno] = miiphy;
if(ctlrno == 0 || hackflavour != Hackdual && mii->curphy == nil)
mii->curphy = miiphy;
mii->mask |= bit;
mii->nphy++;
rmask |= bit;
}
return rmask;
}
static int
kirkwoodmii(Ether *ether)
{
int i;
Ctlr *ctlr;
MiiPhy *phy;
MIIDBG("mii\n");
ctlr = ether->ctlr;
if((ctlr->mii = malloc(sizeof(Mii))) == nil)
return -1;
ctlr->mii->ctlr = ctlr;
ctlr->mii->mir = miird;
ctlr->mii->miw = miiwr;
if(mymii(ctlr->mii, ~0) == 0 || (phy = ctlr->mii->curphy) == nil){
print("#l%d: ether1116: init mii failure\n", ether->ctlrno);
free(ctlr->mii);
ctlr->mii = nil;
return -1;
}
/* oui 005043 is marvell */
MIIDBG("oui %#X phyno %d\n", phy->oui, phy->phyno);
// TODO: does this make sense? shouldn't each phy be initialised?
if((ctlr->ether->ctlrno == 0 || hackflavour != Hackdual) &&
miistatus(ctlr->mii) < 0){
miireset(ctlr->mii);
MIIDBG("miireset\n");
if(miiane(ctlr->mii, ~0, 0, ~0) < 0){
iprint("miiane failed\n");
return -1;
}
MIIDBG("miistatus\n");
miistatus(ctlr->mii);
if(miird(ctlr->mii, phy->phyno, Bmsr) & BmsrLs){
for(i = 0; ; i++){
if(i > 600){
iprint("ether1116: autonegotiation failed\n");
break;
}
if(miird(ctlr->mii, phy->phyno, Bmsr) & BmsrAnc)
break;
delay(10);
}
if(miistatus(ctlr->mii) < 0)
iprint("miistatus failed\n");
}else{
iprint("ether1116: no link\n");
phy->speed = 10; /* simple default */
}
}
ether->mbps = phy->speed;
MIIDBG("#l%d: kirkwoodmii: fd %d speed %d tfc %d rfc %d\n",
ctlr->port, phy->fd, phy->speed, phy->tfc, phy->rfc);
MIIDBG("mii done\n");
return 0;
}
enum { /* PHY register pages */
Pagcopper,
Pagfiber,
Pagrgmii,
Pagled,
Pagrsvd1,
Pagvct,
Pagtest,
Pagrsvd2,
Pagfactest,
};
static void
miiregpage(Mii *mii, ulong dev, ulong page)
{
miiwr(mii, dev, Eadr, page);
}
static int
miiphyinit(Mii *mii)
{
ulong dev;
Ctlr *ctlr;
Gbereg *reg;
ctlr = (Ctlr*)mii->ctlr;
reg = ctlr->reg;
dev = reg->phy;
MIIDBG("phy dev addr %lux\n", dev);
/* leds link & activity */
miiregpage(mii, dev, Pagled);
/* low 4 bits == 1: on - link, blink - activity, off - no link */
miiwr(mii, dev, Scr, (miird(mii, dev, Scr) & ~0xf) | 1);
miiregpage(mii, dev, Pagrgmii);
miiwr(mii, dev, Scr, miird(mii, dev, Scr) | Rgmiipwrup);
/* must now do a software reset, says the manual */
miireset(ctlr->mii);
/* enable RGMII delay on Tx and Rx for CPU port */
miiwr(mii, dev, Recr, miird(mii, dev, Recr) | Rxtiming | Rxtiming);
/* must now do a software reset, says the manual */
miireset(ctlr->mii);
miiregpage(mii, dev, Pagcopper);
miiwr(mii, dev, Scr,
(miird(mii, dev, Scr) & ~(Pwrdown|Endetect)) | Mdix);
return 0;
}
/*
* initialisation
*/
static void
quiesce(Gbereg *reg)
{
ulong v;
v = reg->tqc;
if (v & 0xFF)
reg->tqc = v << 8; /* stop active channels */
v = reg->rqc;
if (v & 0xFF)
reg->rqc = v << 8; /* stop active channels */
/* wait for all queues to stop */
while (reg->tqc & 0xFF || reg->rqc & 0xFF)
;
}
static void
p16(uchar *p, ulong v) /* convert big-endian short to bytes */
{
*p++ = v>>8;
*p = v;
}
static void
p32(uchar *p, ulong v) /* convert big-endian long to bytes */
{
*p++ = v>>24;
*p++ = v>>16;
*p++ = v>>8;
*p = v;
}
/*
* set ether->ea from hw mac address,
* configure unicast filtering to accept it.
*/
void
archetheraddr(Ether *ether, Gbereg *reg, int rxqno)
{
uchar *ea;
ulong nibble, ucreg, tbloff, regoff;
ea = ether->ea;
p32(ea, reg->macah);
p16(ea+4, reg->macal);
if (memcmp(ea, zeroea, sizeof zeroea) == 0 && ether->ctlrno > 0) {
/* hack: use ctlr[0]'s + ctlrno */
memmove(ea, ctlrs[0]->ether->ea, Eaddrlen);
ea[Eaddrlen-1] += ether->ctlrno;
reg->macah = ea[0] << 24 | ea[1] << 16 | ea[2] << 8 | ea[3];
reg->macal = ea[4] << 8 | ea[5];
coherence();
}
/* accept frames on ea */
nibble = ea[5] & 0xf;
tbloff = nibble / 4;
regoff = nibble % 4;
regoff *= 8;
ucreg = reg->dfut[tbloff] & (0xff << regoff);
ucreg |= (rxqno << 1 | Pass) << regoff;
reg->dfut[tbloff] = ucreg;
/* accept all multicast too. set up special & other tables. */
memset(reg->dfsmt, Qno<<1 | Pass, sizeof reg->dfsmt);
memset(reg->dfomt, Qno<<1 | Pass, sizeof reg->dfomt);
coherence();
}
static void
cfgdramacc(Gbereg *reg)
{
memset(reg->harr, 0, sizeof reg->harr);
memset(reg->base, 0, sizeof reg->base);
reg->bare = MASK(6) - MASK(2); /* disable wins 2-5 */
/* this doesn't make any sense, but it's required */
reg->epap = 3 << 2 | 3; /* full access for wins 0 & 1 */
// reg->epap = 0; /* no access on access violation for all wins */
coherence();
reg->base[0].base = PHYSDRAM | WINATTR(Attrcs0) | Targdram;
reg->base[0].size = WINSIZE(256*MB);
reg->base[1].base = (PHYSDRAM + 256*MB) | WINATTR(Attrcs1) | Targdram;
reg->base[1].size = WINSIZE(256*MB);
coherence();
}
static void
ctlralloc(Ctlr *ctlr)
{
int i;
Block *b;
Rx *r;
Tx *t;
ilock(&freeblocks);
for(i = 0; i < Nrxblks; i++) {
b = iallocb(Rxblklen+Bufalign-1);
if(b == nil) {
iprint("ether1116: no memory for rx buffers\n");
break;
}
b->wp = b->rp = (uchar*)
((uintptr)(b->lim - Rxblklen) & ~(Bufalign - 1));
assert(((uintptr)b->rp & (Bufalign - 1)) == 0);
b->free = rxfreeb;
b->next = freeblocks.head;
freeblocks.head = b;
}
iunlock(&freeblocks);
/*
* allocate uncached rx ring descriptors because rings are shared
* with the ethernet controller and more than one fits in a cache line.
*/
ctlr->rx = ucallocalign(Nrx * sizeof(Rx), Descralign, 0);
if(ctlr->rx == nil)
panic("ether1116: no memory for rx ring");
for(i = 0; i < Nrx; i++) {
r = &ctlr->rx[i];
assert(((uintptr)r & (Descralign - 1)) == 0);
r->cs = 0; /* owned by software until r->buf is non-nil */
r->buf = 0;
r->next = PADDR(&ctlr->rx[NEXT(i, Nrx)]);
ctlr->rxb[i] = nil;
}
ctlr->rxtail = ctlr->rxhead = 0;
rxreplenish(ctlr);
/* allocate uncached tx ring descriptors */
ctlr->tx = ucallocalign(Ntx * sizeof(Tx), Descralign, 0);
if(ctlr->tx == nil)
panic("ether1116: no memory for tx ring");
for(i = 0; i < Ntx; i++) {
t = &ctlr->tx[i];
assert(((uintptr)t & (Descralign - 1)) == 0);
t->cs = 0;
t->buf = 0;
t->next = PADDR(&ctlr->tx[NEXT(i, Ntx)]);
ctlr->txb[i] = nil;
}
ctlr->txtail = ctlr->txhead = 0;
}
static void
ctlrinit(Ether *ether)
{
int i;
Ctlr *ctlr = ether->ctlr;
Gbereg *reg = ctlr->reg;
static char name[KNAMELEN];
static Ctlr fakectlr; /* bigger than 4K; keep off the stack */
for (i = 0; i < nelem(reg->tcqdp); i++)
reg->tcqdp[i] = 0;
for (i = 0; i < nelem(reg->crdp); i++)
reg->crdp[i].r = 0;
coherence();
cfgdramacc(reg);
ctlralloc(ctlr);
reg->tcqdp[Qno] = PADDR(&ctlr->tx[ctlr->txhead]);
reg->crdp[Qno].r = PADDR(&ctlr->rx[ctlr->rxhead]);
coherence();
// dumprxdescs(ctlr);
/* clear stats by reading them into fake ctlr */
getmibstats(&fakectlr);
reg->pxmfs = MFS40by; /* allow runts in */
/*
* ipg's (inter packet gaps) for interrupt coalescing,
* values in units of 64 clock cycles. A full-sized
* packet (1514 bytes) takes just over 12µs to transmit.
*/
if (CLOCKFREQ/(Maxrxintrsec*64) >= (1<<16))
panic("rx coalescing value %d too big for short",
CLOCKFREQ/(Maxrxintrsec*64));
reg->sdc = SDCrifb | SDCrxburst(Burst16) | SDCtxburst(Burst16) |
SDCrxnobyteswap | SDCtxnobyteswap |
SDCipgintrx(CLOCKFREQ/(Maxrxintrsec*64));
reg->pxtfut = 0; /* TFUTipginttx(CLOCKFREQ/(Maxrxintrsec*64)) */
/* allow just these interrupts */
/* guruplug generates Irxerr interrupts continually */
reg->irqmask = Isum | Irx | Irxbufferq(Qno) | Irxerr | Itxendq(Qno);
reg->irqemask = IEsum | IEtxerrq(Qno) | IEphystschg | IErxoverrun |
IEtxunderrun;
reg->irqe = 0;
reg->euirqmask = 0;
coherence();
reg->irq = 0;
reg->euirq = 0;
/* send errors to end of memory */
// reg->euda = PHYSDRAM + 512*MB - 8*1024;
reg->euda = 0;
reg->eudid = Attrcs1 << 4 | Targdram;
// archetheraddr(ether, ctlr->reg, Qno); /* 2nd location */
reg->portcfg = Rxqdefault(Qno) | Rxqarp(Qno);
reg->portcfgx = 0;
coherence();
/*
* start the controller running.
* turn the port on, kick the receiver.
*/
reg->psc1 = PSC1rgmii | PSC1encolonbp | PSC1coldomlim(0x23);
/* do this only when the controller is quiescent */
reg->psc0 = PSC0porton | PSC0an_flctloff |
PSC0an_pauseadv | PSC0nofrclinkdown | PSC0mru(PSC0mru1522);
coherence();
for (i = 0; i < 4000; i++) /* magic delay */
;
ether->link = (reg->ps0 & PS0linkup) != 0;
/* set ethernet MTU for leaky bucket mechanism to 0 (disabled) */
reg->pmtu = 0;
etheractive(ether);
snprint(name, sizeof name, "#l%drproc", ether->ctlrno);
kproc(name, rcvproc, ether);
reg->rqc = Rxqon(Qno);
coherence();
}
static void
attach(Ether* ether)
{
Ctlr *ctlr = ether->ctlr;
lock(&ctlr->initlock);
if(ctlr->init == 0) {
ctlrinit(ether);
ctlr->init = 1;
}
unlock(&ctlr->initlock);
}
/*
* statistics goo.
* mib registers clear on read.
*/
static void
getmibstats(Ctlr *ctlr)
{
Gbereg *reg = ctlr->reg;
/*
* Marvell 88f6281 errata FE-ETH-120: high long of rxby and txby
* can't be read correctly, so read the low long frequently
* (every 30 seconds or less), thus avoiding overflow into high long.
*/
ctlr->rxby += reg->rxbylo;
ctlr->txby += reg->txbylo;
ctlr->badrxby += reg->badrxby;
ctlr->mactxerr += reg->mactxerr;
ctlr->rxpkt += reg->rxpkt;
ctlr->badrxpkt += reg->badrxpkt;
ctlr->rxbcastpkt+= reg->rxbcastpkt;
ctlr->rxmcastpkt+= reg->rxmcastpkt;
ctlr->rx64 += reg->rx64;
ctlr->rx65_127 += reg->rx65_127;
ctlr->rx128_255 += reg->rx128_255;
ctlr->rx256_511 += reg->rx256_511;
ctlr->rx512_1023+= reg->rx512_1023;
ctlr->rx1024_max+= reg->rx1024_max;
ctlr->txpkt += reg->txpkt;
ctlr->txcollpktdrop+= reg->txcollpktdrop;
ctlr->txmcastpkt+= reg->txmcastpkt;
ctlr->txbcastpkt+= reg->txbcastpkt;
ctlr->badmacctlpkts+= reg->badmacctlpkts;
ctlr->txflctl += reg->txflctl;
ctlr->rxflctl += reg->rxflctl;
ctlr->badrxflctl+= reg->badrxflctl;
ctlr->rxundersized+= reg->rxundersized;
ctlr->rxfrags += reg->rxfrags;
ctlr->rxtoobig += reg->rxtoobig;
ctlr->rxjabber += reg->rxjabber;
ctlr->rxerr += reg->rxerr;
ctlr->crcerr += reg->crcerr;
ctlr->collisions+= reg->collisions;
ctlr->latecoll += reg->latecoll;
}
long
ifstat(Ether *ether, void *a, long n, ulong off)
{
Ctlr *ctlr = ether->ctlr;
Gbereg *reg = ctlr->reg;
char *buf, *p, *e;
buf = p = malloc(READSTR);
e = p + READSTR;
ilock(ctlr);
getmibstats(ctlr);
ctlr->intrs += ctlr->newintrs;
p = seprint(p, e, "interrupts: %lud\n", ctlr->intrs);
p = seprint(p, e, "new interrupts: %lud\n", ctlr->newintrs);
ctlr->newintrs = 0;
p = seprint(p, e, "tx underrun: %lud\n", ctlr->txunderrun);
p = seprint(p, e, "tx ring full: %lud\n", ctlr->txringfull);
ctlr->rxdiscard += reg->pxdfc;
ctlr->rxoverrun += reg->pxofc;
p = seprint(p, e, "rx discarded frames: %lud\n", ctlr->rxdiscard);
p = seprint(p, e, "rx overrun frames: %lud\n", ctlr->rxoverrun);
p = seprint(p, e, "no first+last flag: %lud\n", ctlr->nofirstlast);
p = seprint(p, e, "duplex: %s\n", (reg->ps0 & PS0fd)? "full": "half");
p = seprint(p, e, "flow control: %s\n", (reg->ps0 & PS0flctl)? "on": "off");
/* p = seprint(p, e, "speed: %d mbps\n", ); */
p = seprint(p, e, "received bytes: %llud\n", ctlr->rxby);
p = seprint(p, e, "bad received bytes: %lud\n", ctlr->badrxby);
p = seprint(p, e, "internal mac transmit errors: %lud\n", ctlr->mactxerr);
p = seprint(p, e, "total received frames: %lud\n", ctlr->rxpkt);
p = seprint(p, e, "received broadcast frames: %lud\n", ctlr->rxbcastpkt);
p = seprint(p, e, "received multicast frames: %lud\n", ctlr->rxmcastpkt);
p = seprint(p, e, "bad received frames: %lud\n", ctlr->badrxpkt);
p = seprint(p, e, "received frames 0-64: %lud\n", ctlr->rx64);
p = seprint(p, e, "received frames 65-127: %lud\n", ctlr->rx65_127);
p = seprint(p, e, "received frames 128-255: %lud\n", ctlr->rx128_255);
p = seprint(p, e, "received frames 256-511: %lud\n", ctlr->rx256_511);
p = seprint(p, e, "received frames 512-1023: %lud\n", ctlr->rx512_1023);
p = seprint(p, e, "received frames 1024-max: %lud\n", ctlr->rx1024_max);
p = seprint(p, e, "transmitted bytes: %llud\n", ctlr->txby);
p = seprint(p, e, "total transmitted frames: %lud\n", ctlr->txpkt);
p = seprint(p, e, "transmitted broadcast frames: %lud\n", ctlr->txbcastpkt);
p = seprint(p, e, "transmitted multicast frames: %lud\n", ctlr->txmcastpkt);
p = seprint(p, e, "transmit frames dropped by collision: %lud\n", ctlr->txcollpktdrop);
p = seprint(p, e, "misaligned buffers: %lud\n", ether->pktsmisaligned);
p = seprint(p, e, "bad mac control frames: %lud\n", ctlr->badmacctlpkts);
p = seprint(p, e, "transmitted flow control messages: %lud\n", ctlr->txflctl);
p = seprint(p, e, "received flow control messages: %lud\n", ctlr->rxflctl);
p = seprint(p, e, "bad received flow control messages: %lud\n", ctlr->badrxflctl);
p = seprint(p, e, "received undersized packets: %lud\n", ctlr->rxundersized);
p = seprint(p, e, "received fragments: %lud\n", ctlr->rxfrags);
p = seprint(p, e, "received oversized packets: %lud\n", ctlr->rxtoobig);
p = seprint(p, e, "received jabber packets: %lud\n", ctlr->rxjabber);
p = seprint(p, e, "mac receive errors: %lud\n", ctlr->rxerr);
p = seprint(p, e, "crc errors: %lud\n", ctlr->crcerr);
p = seprint(p, e, "collisions: %lud\n", ctlr->collisions);
p = seprint(p, e, "late collisions: %lud\n", ctlr->latecoll);
USED(p);
iunlock(ctlr);
n = readstr(off, a, n, buf);
free(buf);
return n;
}
static int
reset(Ether *ether)
{
Ctlr *ctlr;
ether->ctlr = ctlr = malloc(sizeof *ctlr);
switch(ether->ctlrno) {
case 0:
ether->irq = IRQ0gbe0sum;
break;
case 1:
ether->irq = IRQ0gbe1sum;
break;
default:
panic("ether1116: bad ether ctlr #%d", ether->ctlrno);
}
ctlr->reg = (Gbereg*)soc.ether[ether->ctlrno];
/* need this for guruplug, at least */
*(ulong *)soc.iocfg |= 1 << 7 | 1 << 15; /* io cfg 0: 1.8v gbe */
coherence();
ctlr->ether = ether;
ctlrs[ether->ctlrno] = ctlr;
shutdown(ether);
/* ensure that both interfaces are set to RGMII before calling mii */
((Gbereg*)soc.ether[0])->psc1 |= PSC1rgmii;
((Gbereg*)soc.ether[1])->psc1 |= PSC1rgmii;
coherence();
/* Set phy address of the port */
ctlr->port = ether->ctlrno;
ctlr->reg->phy = ether->ctlrno;
coherence();
ether->port = (uintptr)ctlr->reg;
if(kirkwoodmii(ether) < 0){
free(ctlr);
ether->ctlr = nil;
return -1;
}
miiphyinit(ctlr->mii);
archetheraddr(ether, ctlr->reg, Qno); /* original location */
if (memcmp(ether->ea, zeroea, sizeof zeroea) == 0){
iprint("ether1116: reset: zero ether->ea\n");
free(ctlr);
ether->ctlr = nil;
return -1; /* no rj45 for this ether */
}
ether->attach = attach;
ether->transmit = transmit;
ether->interrupt = interrupt;
ether->ifstat = ifstat;
ether->shutdown = shutdown;
ether->ctl = ctl;
ether->arg = ether;
ether->promiscuous = promiscuous;
ether->multicast = multicast;
return 0;
}
void
ether1116link(void)
{
addethercard("88e1116", reset);
}
|