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
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
|
/* $NetBSD: if_ae.c,v 1.42 2022/09/29 07:00:46 skrll Exp $ */
/*-
* Copyright (c) 2006 Urbana-Champaign Independent Media Center.
* Copyright (c) 2006 Garrett D'Amore.
* All rights reserved.
*
* This code was written by Garrett D'Amore for the Champaign-Urbana
* Community Wireless Network Project.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgements:
* This product includes software developed by the Urbana-Champaign
* Independent Media Center.
* This product includes software developed by Garrett D'Amore.
* 4. Urbana-Champaign Independent Media Center's name and Garrett
* D'Amore's name may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
* MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
* MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*-
* Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center; and by Charles M. Hannum.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Device driver for the onboard ethernet MAC found on the AR5312
* chip's AHB bus.
*
* This device is very simliar to the tulip in most regards, and
* the code is directly derived from NetBSD's tulip.c. However, it
* is different enough that it did not seem to be a good idea to
* add further complexity to the tulip driver, so we have our own.
*
* Also tulip has a lot of complexity in it for various parts/options
* that we don't need, and on these little boxes with only ~8MB RAM, we
* don't want any extra bloat.
*/
/*
* TODO:
*
* 1) Find out about BUS_MODE_ALIGN16B. This chip can apparently align
* inbound packets on a half-word boundary, which would make life easier
* for TCP/IP. (Aligning IP headers on a word.)
*
* 2) There is stuff in original tulip to shut down the device when reacting
* to a change in link status. Is that needed.
*
* 3) Test with variety of 10/100 HDX/FDX scenarios.
*
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ae.c,v 1.42 2022/09/29 07:00:46 skrll Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/callout.h>
#include <sys/device.h>
#include <sys/endian.h>
#include <sys/errno.h>
#include <sys/intr.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <uvm/uvm_extern.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_ether.h>
#include <net/bpf.h>
#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#include <dev/mii/mii_bitbang.h>
#include <mips/atheros/include/arbusvar.h>
#include <mips/atheros/dev/aereg.h>
#include <mips/atheros/dev/aevar.h>
static const struct {
uint32_t txth_opmode; /* OPMODE bits */
const char *txth_name; /* name of mode */
} ae_txthresh[] = {
{ OPMODE_TR_32, "32 words" },
{ OPMODE_TR_64, "64 words" },
{ OPMODE_TR_128, "128 words" },
{ OPMODE_TR_256, "256 words" },
{ OPMODE_SF, "store and forward mode" },
{ 0, NULL },
};
static int ae_match(device_t, struct cfdata *, void *);
static void ae_attach(device_t, device_t, void *);
static int ae_detach(device_t, int);
static int ae_activate(device_t, enum devact);
static int ae_ifflags_cb(struct ethercom *);
static void ae_reset(struct ae_softc *);
static void ae_idle(struct ae_softc *, uint32_t);
static void ae_start(struct ifnet *);
static void ae_watchdog(struct ifnet *);
static int ae_ioctl(struct ifnet *, u_long, void *);
static int ae_init(struct ifnet *);
static void ae_stop(struct ifnet *, int);
static void ae_shutdown(void *);
static void ae_rxdrain(struct ae_softc *);
static int ae_add_rxbuf(struct ae_softc *, int);
static int ae_enable(struct ae_softc *);
static void ae_disable(struct ae_softc *);
static void ae_power(int, void *);
static void ae_filter_setup(struct ae_softc *);
static int ae_intr(void *);
static void ae_rxintr(struct ae_softc *);
static void ae_txintr(struct ae_softc *);
static void ae_mii_tick(void *);
static void ae_mii_statchg(struct ifnet *);
static int ae_mii_readreg(device_t, int, int, uint16_t *);
static int ae_mii_writereg(device_t, int, int, uint16_t);
#ifdef AE_DEBUG
#define DPRINTF(sc, x) if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
printf x
#else
#define DPRINTF(sc, x) /* nothing */
#endif
#ifdef AE_STATS
static void ae_print_stats(struct ae_softc *);
#endif
CFATTACH_DECL_NEW(ae, sizeof(struct ae_softc),
ae_match, ae_attach, ae_detach, ae_activate);
/*
* ae_match:
*
* Check for a device match.
*/
int
ae_match(device_t parent, struct cfdata *cf, void *aux)
{
struct arbus_attach_args *aa = aux;
if (strcmp(aa->aa_name, cf->cf_name) == 0)
return 1;
return 0;
}
/*
* ae_attach:
*
* Attach an ae interface to the system.
*/
void
ae_attach(device_t parent, device_t self, void *aux)
{
const uint8_t *enaddr;
prop_data_t ea;
struct ae_softc *sc = device_private(self);
struct arbus_attach_args *aa = aux;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct mii_data * const mii = &sc->sc_mii;
int i, error;
sc->sc_dev = self;
callout_init(&sc->sc_tick_callout, 0);
printf(": Atheros AR531X 10/100 Ethernet\n");
/*
* Try to get MAC address.
*/
ea = prop_dictionary_get(device_properties(sc->sc_dev), "mac-address");
if (ea == NULL) {
printf("%s: unable to get mac-addr property\n",
device_xname(sc->sc_dev));
return;
}
KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
enaddr = prop_data_data_nocopy(ea);
/* Announce ourselves. */
printf("%s: Ethernet address %s\n", device_xname(sc->sc_dev),
ether_sprintf(enaddr));
sc->sc_cirq = aa->aa_cirq;
sc->sc_mirq = aa->aa_mirq;
sc->sc_st = aa->aa_bst;
sc->sc_dmat = aa->aa_dmat;
SIMPLEQ_INIT(&sc->sc_txfreeq);
SIMPLEQ_INIT(&sc->sc_txdirtyq);
/*
* Map registers.
*/
sc->sc_size = aa->aa_size;
if ((error = bus_space_map(sc->sc_st, aa->aa_addr, sc->sc_size, 0,
&sc->sc_sh)) != 0) {
printf("%s: unable to map registers, error = %d\n",
device_xname(sc->sc_dev), error);
goto fail_0;
}
/*
* Allocate the control data structures, and create and load the
* DMA map for it.
*/
if ((error = bus_dmamem_alloc(sc->sc_dmat,
sizeof(struct ae_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
1, &sc->sc_cdnseg, 0)) != 0) {
printf("%s: unable to allocate control data, error = %d\n",
device_xname(sc->sc_dev), error);
goto fail_1;
}
if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg,
sizeof(struct ae_control_data), (void **)&sc->sc_control_data,
BUS_DMA_COHERENT)) != 0) {
printf("%s: unable to map control data, error = %d\n",
device_xname(sc->sc_dev), error);
goto fail_2;
}
if ((error = bus_dmamap_create(sc->sc_dmat,
sizeof(struct ae_control_data), 1,
sizeof(struct ae_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
printf("%s: unable to create control data DMA map, "
"error = %d\n", device_xname(sc->sc_dev), error);
goto fail_3;
}
if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
sc->sc_control_data, sizeof(struct ae_control_data), NULL,
0)) != 0) {
printf("%s: unable to load control data DMA map, error = %d\n",
device_xname(sc->sc_dev), error);
goto fail_4;
}
/*
* Create the transmit buffer DMA maps.
*/
for (i = 0; i < AE_TXQUEUELEN; i++) {
if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
AE_NTXSEGS, MCLBYTES, 0, 0,
&sc->sc_txsoft[i].txs_dmamap)) != 0) {
printf("%s: unable to create tx DMA map %d, "
"error = %d\n", device_xname(sc->sc_dev), i, error);
goto fail_5;
}
}
/*
* Create the receive buffer DMA maps.
*/
for (i = 0; i < AE_NRXDESC; i++) {
if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
printf("%s: unable to create rx DMA map %d, "
"error = %d\n", device_xname(sc->sc_dev), i, error);
goto fail_6;
}
sc->sc_rxsoft[i].rxs_mbuf = NULL;
}
/*
* Reset the chip to a known state.
*/
ae_reset(sc);
/*
* From this point forward, the attachment cannot fail. A failure
* before this point releases all resources that may have been
* allocated.
*/
sc->sc_flags |= AE_ATTACHED;
/*
* Initialize our media structures. This may probe the MII, if
* present.
*/
mii->mii_ifp = ifp;
mii->mii_readreg = ae_mii_readreg;
mii->mii_writereg = ae_mii_writereg;
mii->mii_statchg = ae_mii_statchg;
sc->sc_ethercom.ec_mii = mii;
ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
MII_OFFSET_ANY, 0);
if (LIST_FIRST(&mii->mii_phys) == NULL) {
ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
} else
ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
sc->sc_tick = ae_mii_tick;
strcpy(ifp->if_xname, device_xname(sc->sc_dev));
ifp->if_softc = sc;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
sc->sc_if_flags = ifp->if_flags;
ifp->if_ioctl = ae_ioctl;
ifp->if_start = ae_start;
ifp->if_watchdog = ae_watchdog;
ifp->if_init = ae_init;
ifp->if_stop = ae_stop;
IFQ_SET_READY(&ifp->if_snd);
/*
* We can support 802.1Q VLAN-sized frames.
*/
sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
/*
* Attach the interface.
*/
if_attach(ifp);
if_deferred_start_init(ifp, NULL);
ether_ifattach(ifp, enaddr);
ether_set_ifflags_cb(&sc->sc_ethercom, ae_ifflags_cb);
rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
RND_TYPE_NET, RND_FLAG_DEFAULT);
/*
* Make sure the interface is shutdown during reboot.
*/
sc->sc_sdhook = shutdownhook_establish(ae_shutdown, sc);
if (sc->sc_sdhook == NULL)
printf("%s: WARNING: unable to establish shutdown hook\n",
device_xname(sc->sc_dev));
/*
* Add a suspend hook to make sure we come back up after a
* resume.
*/
sc->sc_powerhook = powerhook_establish(device_xname(sc->sc_dev),
ae_power, sc);
if (sc->sc_powerhook == NULL)
printf("%s: WARNING: unable to establish power hook\n",
device_xname(sc->sc_dev));
return;
/*
* Free any resources we've allocated during the failed attach
* attempt. Do this in reverse order and fall through.
*/
fail_6:
for (i = 0; i < AE_NRXDESC; i++) {
if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
bus_dmamap_destroy(sc->sc_dmat,
sc->sc_rxsoft[i].rxs_dmamap);
}
fail_5:
for (i = 0; i < AE_TXQUEUELEN; i++) {
if (sc->sc_txsoft[i].txs_dmamap != NULL)
bus_dmamap_destroy(sc->sc_dmat,
sc->sc_txsoft[i].txs_dmamap);
}
bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
fail_4:
bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
fail_3:
bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
sizeof(struct ae_control_data));
fail_2:
bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
fail_1:
bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_size);
fail_0:
return;
}
/*
* ae_activate:
*
* Handle device activation/deactivation requests.
*/
int
ae_activate(device_t self, enum devact act)
{
struct ae_softc *sc = device_private(self);
switch (act) {
case DVACT_DEACTIVATE:
if_deactivate(&sc->sc_ethercom.ec_if);
return 0;
default:
return EOPNOTSUPP;
}
}
/*
* ae_detach:
*
* Detach a device interface.
*/
int
ae_detach(device_t self, int flags)
{
struct ae_softc *sc = device_private(self);
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct ae_rxsoft *rxs;
struct ae_txsoft *txs;
int i;
/*
* Succeed now if there isn't any work to do.
*/
if ((sc->sc_flags & AE_ATTACHED) == 0)
return (0);
/* Unhook our tick handler. */
if (sc->sc_tick)
callout_stop(&sc->sc_tick_callout);
/* Detach all PHYs */
mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
rnd_detach_source(&sc->sc_rnd_source);
ether_ifdetach(ifp);
if_detach(ifp);
/* Delete all remaining media. */
ifmedia_fini(&sc->sc_mii.mii_media);
for (i = 0; i < AE_NRXDESC; i++) {
rxs = &sc->sc_rxsoft[i];
if (rxs->rxs_mbuf != NULL) {
bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
m_freem(rxs->rxs_mbuf);
rxs->rxs_mbuf = NULL;
}
bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
}
for (i = 0; i < AE_TXQUEUELEN; i++) {
txs = &sc->sc_txsoft[i];
if (txs->txs_mbuf != NULL) {
bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
m_freem(txs->txs_mbuf);
txs->txs_mbuf = NULL;
}
bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
}
bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
sizeof(struct ae_control_data));
bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
shutdownhook_disestablish(sc->sc_sdhook);
powerhook_disestablish(sc->sc_powerhook);
bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_size);
return (0);
}
/*
* ae_shutdown:
*
* Make sure the interface is stopped at reboot time.
*/
static void
ae_shutdown(void *arg)
{
struct ae_softc *sc = arg;
ae_stop(&sc->sc_ethercom.ec_if, 1);
}
/*
* ae_start: [ifnet interface function]
*
* Start packet transmission on the interface.
*/
static void
ae_start(struct ifnet *ifp)
{
struct ae_softc *sc = ifp->if_softc;
struct mbuf *m0, *m;
struct ae_txsoft *txs;
bus_dmamap_t dmamap;
int error, firsttx, nexttx, lasttx = 1, ofree, seg;
DPRINTF(sc, ("%s: ae_start: sc_flags 0x%08x, if_flags 0x%08x\n",
device_xname(sc->sc_dev), sc->sc_flags, ifp->if_flags));
if ((ifp->if_flags & IFF_RUNNING) == 0)
return;
/*
* Remember the previous number of free descriptors and
* the first descriptor we'll use.
*/
ofree = sc->sc_txfree;
firsttx = sc->sc_txnext;
DPRINTF(sc, ("%s: ae_start: txfree %d, txnext %d\n",
device_xname(sc->sc_dev), ofree, firsttx));
/*
* Loop through the send queue, setting up transmit descriptors
* until we drain the queue, or use up all available transmit
* descriptors.
*/
while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
sc->sc_txfree != 0) {
/*
* Grab a packet off the queue.
*/
IFQ_POLL(&ifp->if_snd, m0);
if (m0 == NULL)
break;
m = NULL;
dmamap = txs->txs_dmamap;
/*
* Load the DMA map. If this fails, the packet either
* didn't fit in the alloted number of segments, or we were
* short on resources. In this case, we'll copy and try
* again.
*/
if (((mtod(m0, uintptr_t) & 3) != 0) ||
bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
printf("%s: unable to allocate Tx mbuf\n",
device_xname(sc->sc_dev));
break;
}
MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
if (m0->m_pkthdr.len > MHLEN) {
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
printf("%s: unable to allocate Tx "
"cluster\n", device_xname(sc->sc_dev));
m_freem(m);
break;
}
}
m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
if (error) {
printf("%s: unable to load Tx buffer, "
"error = %d\n", device_xname(sc->sc_dev),
error);
break;
}
}
/*
* Ensure we have enough descriptors free to describe
* the packet.
*/
if (dmamap->dm_nsegs > sc->sc_txfree) {
/*
* Not enough free descriptors to transmit this
* packet. We haven't committed to anything yet,
* so just unload the DMA map, put the packet
* back on the queue, and punt. Notify the upper
* layer that there are no more slots left.
*
* XXX We could allocate an mbuf and copy, but
* XXX it is worth it?
*/
bus_dmamap_unload(sc->sc_dmat, dmamap);
if (m != NULL)
m_freem(m);
break;
}
IFQ_DEQUEUE(&ifp->if_snd, m0);
if (m != NULL) {
m_freem(m0);
m0 = m;
}
/*
* WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
*/
/* Sync the DMA map. */
bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
BUS_DMASYNC_PREWRITE);
/*
* Initialize the transmit descriptors.
*/
for (nexttx = sc->sc_txnext, seg = 0;
seg < dmamap->dm_nsegs;
seg++, nexttx = AE_NEXTTX(nexttx)) {
/*
* If this is the first descriptor we're
* enqueueing, don't set the OWN bit just
* yet. That could cause a race condition.
* We'll do it below.
*/
sc->sc_txdescs[nexttx].ad_status =
(nexttx == firsttx) ? 0 : ADSTAT_OWN;
sc->sc_txdescs[nexttx].ad_bufaddr1 =
dmamap->dm_segs[seg].ds_addr;
sc->sc_txdescs[nexttx].ad_ctl =
(dmamap->dm_segs[seg].ds_len <<
ADCTL_SIZE1_SHIFT) |
(nexttx == (AE_NTXDESC - 1) ?
ADCTL_ER : 0);
lasttx = nexttx;
}
KASSERT(lasttx != -1);
/* Set `first segment' and `last segment' appropriately. */
sc->sc_txdescs[sc->sc_txnext].ad_ctl |= ADCTL_Tx_FS;
sc->sc_txdescs[lasttx].ad_ctl |= ADCTL_Tx_LS;
#ifdef AE_DEBUG
if (ifp->if_flags & IFF_DEBUG) {
printf(" txsoft %p transmit chain:\n", txs);
for (seg = sc->sc_txnext;; seg = AE_NEXTTX(seg)) {
printf(" descriptor %d:\n", seg);
printf(" ad_status: 0x%08x\n",
sc->sc_txdescs[seg].ad_status);
printf(" ad_ctl: 0x%08x\n",
sc->sc_txdescs[seg].ad_ctl);
printf(" ad_bufaddr1: 0x%08x\n",
sc->sc_txdescs[seg].ad_bufaddr1);
printf(" ad_bufaddr2: 0x%08x\n",
sc->sc_txdescs[seg].ad_bufaddr2);
if (seg == lasttx)
break;
}
}
#endif
/* Sync the descriptors we're using. */
AE_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
/*
* Store a pointer to the packet so we can free it later,
* and remember what txdirty will be once the packet is
* done.
*/
txs->txs_mbuf = m0;
txs->txs_firstdesc = sc->sc_txnext;
txs->txs_lastdesc = lasttx;
txs->txs_ndescs = dmamap->dm_nsegs;
/* Advance the tx pointer. */
sc->sc_txfree -= dmamap->dm_nsegs;
sc->sc_txnext = nexttx;
SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
/*
* Pass the packet to any BPF listeners.
*/
bpf_mtap(ifp, m0, BPF_D_OUT);
}
if (sc->sc_txfree != ofree) {
DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
device_xname(sc->sc_dev), lasttx, firsttx));
/*
* Cause a transmit interrupt to happen on the
* last packet we enqueued.
*/
sc->sc_txdescs[lasttx].ad_ctl |= ADCTL_Tx_IC;
AE_CDTXSYNC(sc, lasttx, 1,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
/*
* The entire packet chain is set up. Give the
* first descriptor to the chip now.
*/
sc->sc_txdescs[firsttx].ad_status |= ADSTAT_OWN;
AE_CDTXSYNC(sc, firsttx, 1,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
/* Wake up the transmitter. */
/* XXX USE AUTOPOLLING? */
AE_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
AE_BARRIER(sc);
/* Set a watchdog timer in case the chip flakes out. */
ifp->if_timer = 5;
}
}
/*
* ae_watchdog: [ifnet interface function]
*
* Watchdog timer handler.
*/
static void
ae_watchdog(struct ifnet *ifp)
{
struct ae_softc *sc = ifp->if_softc;
int doing_transmit;
doing_transmit = (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq));
if (doing_transmit) {
printf("%s: transmit timeout\n", device_xname(sc->sc_dev));
if_statinc(ifp, if_oerrors);
}
else
printf("%s: spurious watchdog timeout\n", device_xname(sc->sc_dev));
(void) ae_init(ifp);
/* Try to get more packets going. */
ae_start(ifp);
}
/* If the interface is up and running, only modify the receive
* filter when changing to/from promiscuous mode. Otherwise return
* ENETRESET so that ether_ioctl will reset the chip.
*/
static int
ae_ifflags_cb(struct ethercom *ec)
{
struct ifnet *ifp = &ec->ec_if;
struct ae_softc *sc = ifp->if_softc;
u_short change = ifp->if_flags ^ sc->sc_if_flags;
if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0)
return ENETRESET;
else if ((change & IFF_PROMISC) != 0)
ae_filter_setup(sc);
return 0;
}
/*
* ae_ioctl: [ifnet interface function]
*
* Handle control requests from the operator.
*/
static int
ae_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
struct ae_softc *sc = ifp->if_softc;
int s, error;
s = splnet();
error = ether_ioctl(ifp, cmd, data);
if (error == ENETRESET) {
if (ifp->if_flags & IFF_RUNNING) {
/*
* Multicast list has changed. Set the
* hardware filter accordingly.
*/
ae_filter_setup(sc);
}
error = 0;
}
/* Try to get more packets going. */
if (AE_IS_ENABLED(sc))
ae_start(ifp);
sc->sc_if_flags = ifp->if_flags;
splx(s);
return (error);
}
/*
* ae_intr:
*
* Interrupt service routine.
*/
int
ae_intr(void *arg)
{
struct ae_softc *sc = arg;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
uint32_t status, rxstatus, txstatus;
int handled = 0, txthresh;
DPRINTF(sc, ("%s: ae_intr\n", device_xname(sc->sc_dev)));
#ifdef DEBUG
if (AE_IS_ENABLED(sc) == 0)
panic("%s: ae_intr: not enabled", device_xname(sc->sc_dev));
#endif
/*
* If the interface isn't running, the interrupt couldn't
* possibly have come from us.
*/
if ((ifp->if_flags & IFF_RUNNING) == 0 ||
!device_is_active(sc->sc_dev)) {
printf("spurious?!?\n");
return (0);
}
for (;;) {
status = AE_READ(sc, CSR_STATUS);
if (status) {
AE_WRITE(sc, CSR_STATUS, status);
AE_BARRIER(sc);
}
if ((status & sc->sc_inten) == 0)
break;
handled = 1;
rxstatus = status & sc->sc_rxint_mask;
txstatus = status & sc->sc_txint_mask;
if (rxstatus) {
/* Grab new any new packets. */
ae_rxintr(sc);
if (rxstatus & STATUS_RU) {
printf("%s: receive ring overrun\n",
device_xname(sc->sc_dev));
/* Get the receive process going again. */
AE_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
AE_BARRIER(sc);
break;
}
}
if (txstatus) {
/* Sweep up transmit descriptors. */
ae_txintr(sc);
if (txstatus & STATUS_TJT)
printf("%s: transmit jabber timeout\n",
device_xname(sc->sc_dev));
if (txstatus & STATUS_UNF) {
/*
* Increase our transmit threshold if
* another is available.
*/
txthresh = sc->sc_txthresh + 1;
if (ae_txthresh[txthresh].txth_name != NULL) {
uint32_t opmode;
/* Idle the transmit process. */
opmode = AE_READ(sc, CSR_OPMODE);
ae_idle(sc, OPMODE_ST);
sc->sc_txthresh = txthresh;
opmode &= ~(OPMODE_TR | OPMODE_SF);
opmode |=
ae_txthresh[txthresh].txth_opmode;
printf("%s: transmit underrun; new "
"threshold: %s\n",
device_xname(sc->sc_dev),
ae_txthresh[txthresh].txth_name);
/*
* Set the new threshold and restart
* the transmit process.
*/
AE_WRITE(sc, CSR_OPMODE, opmode);
AE_BARRIER(sc);
}
/*
* XXX Log every Nth underrun from
* XXX now on?
*/
}
}
if (status & (STATUS_TPS | STATUS_RPS)) {
if (status & STATUS_TPS)
printf("%s: transmit process stopped\n",
device_xname(sc->sc_dev));
if (status & STATUS_RPS)
printf("%s: receive process stopped\n",
device_xname(sc->sc_dev));
(void) ae_init(ifp);
break;
}
if (status & STATUS_SE) {
const char *str;
if (status & STATUS_TX_ABORT)
str = "tx abort";
else if (status & STATUS_RX_ABORT)
str = "rx abort";
else
str = "unknown error";
printf("%s: fatal system error: %s\n",
device_xname(sc->sc_dev), str);
(void) ae_init(ifp);
break;
}
/*
* Not handled:
*
* Transmit buffer unavailable -- normal
* condition, nothing to do, really.
*
* General purpose timer experied -- we don't
* use the general purpose timer.
*
* Early receive interrupt -- not available on
* all chips, we just use RI. We also only
* use single-segment receive DMA, so this
* is mostly useless.
*/
}
/* Try to get more packets going. */
if_schedule_deferred_start(ifp);
if (handled)
rnd_add_uint32(&sc->sc_rnd_source, status);
return (handled);
}
/*
* ae_rxintr:
*
* Helper; handle receive interrupts.
*/
static void
ae_rxintr(struct ae_softc *sc)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct ae_rxsoft *rxs;
struct mbuf *m;
uint32_t rxstat;
int i, len;
for (i = sc->sc_rxptr;; i = AE_NEXTRX(i)) {
rxs = &sc->sc_rxsoft[i];
AE_CDRXSYNC(sc, i,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
rxstat = sc->sc_rxdescs[i].ad_status;
if (rxstat & ADSTAT_OWN) {
/*
* We have processed all of the receive buffers.
*/
break;
}
/*
* If any collisions were seen on the wire, count one.
*/
if (rxstat & ADSTAT_Rx_CS)
if_statinc(ifp, if_collisions);
/*
* If an error occurred, update stats, clear the status
* word, and leave the packet buffer in place. It will
* simply be reused the next time the ring comes around.
* If 802.1Q VLAN MTU is enabled, ignore the Frame Too Long
* error.
*/
if (rxstat & ADSTAT_ES &&
((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) == 0 ||
(rxstat & (ADSTAT_Rx_DE | ADSTAT_Rx_RF |
ADSTAT_Rx_DB | ADSTAT_Rx_CE)) != 0)) {
#define PRINTERR(bit, str) \
if (rxstat & (bit)) \
printf("%s: receive error: %s\n", \
device_xname(sc->sc_dev), str)
if_statinc(ifp, if_ierrors);
PRINTERR(ADSTAT_Rx_DE, "descriptor error");
PRINTERR(ADSTAT_Rx_RF, "runt frame");
PRINTERR(ADSTAT_Rx_TL, "frame too long");
PRINTERR(ADSTAT_Rx_RE, "MII error");
PRINTERR(ADSTAT_Rx_DB, "dribbling bit");
PRINTERR(ADSTAT_Rx_CE, "CRC error");
#undef PRINTERR
AE_INIT_RXDESC(sc, i);
continue;
}
bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
/*
* No errors; receive the packet. Note the chip
* includes the CRC with every packet.
*/
len = ADSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
/*
* XXX: the Atheros part can align on half words. what
* is the performance implication of this? Probably
* minimal, and we should use it...
*/
#ifdef __NO_STRICT_ALIGNMENT
/*
* Allocate a new mbuf cluster. If that fails, we are
* out of memory, and must drop the packet and recycle
* the buffer that's already attached to this descriptor.
*/
m = rxs->rxs_mbuf;
if (ae_add_rxbuf(sc, i) != 0) {
if_statinc(ifp, if_ierrors);
AE_INIT_RXDESC(sc, i);
bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
continue;
}
#else
/*
* The chip's receive buffers must be 4-byte aligned.
* But this means that the data after the Ethernet header
* is misaligned. We must allocate a new buffer and
* copy the data, shifted forward 2 bytes.
*/
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
dropit:
if_statinc(ifp, if_ierrors);
AE_INIT_RXDESC(sc, i);
bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
continue;
}
MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
if (len > (MHLEN - 2)) {
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
m_freem(m);
goto dropit;
}
}
m->m_data += 2;
/*
* Note that we use clusters for incoming frames, so the
* buffer is virtually contiguous.
*/
memcpy(mtod(m, void *), mtod(rxs->rxs_mbuf, void *), len);
/* Allow the receive descriptor to continue using its mbuf. */
AE_INIT_RXDESC(sc, i);
bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
#endif /* __NO_STRICT_ALIGNMENT */
m_set_rcvif(m, ifp);
m->m_pkthdr.len = m->m_len = len;
/* Pass it on. */
if_percpuq_enqueue(ifp->if_percpuq, m);
}
/* Update the receive pointer. */
sc->sc_rxptr = i;
}
/*
* ae_txintr:
*
* Helper; handle transmit interrupts.
*/
static void
ae_txintr(struct ae_softc *sc)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct ae_txsoft *txs;
uint32_t txstat;
DPRINTF(sc, ("%s: ae_txintr: sc_flags 0x%08x\n",
device_xname(sc->sc_dev), sc->sc_flags));
/*
* Go through our Tx list and free mbufs for those
* frames that have been transmitted.
*/
while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
AE_CDTXSYNC(sc, txs->txs_lastdesc,
txs->txs_ndescs,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
#ifdef AE_DEBUG
if (ifp->if_flags & IFF_DEBUG) {
int i;
printf(" txsoft %p transmit chain:\n", txs);
for (i = txs->txs_firstdesc;; i = AE_NEXTTX(i)) {
printf(" descriptor %d:\n", i);
printf(" ad_status: 0x%08x\n",
sc->sc_txdescs[i].ad_status);
printf(" ad_ctl: 0x%08x\n",
sc->sc_txdescs[i].ad_ctl);
printf(" ad_bufaddr1: 0x%08x\n",
sc->sc_txdescs[i].ad_bufaddr1);
printf(" ad_bufaddr2: 0x%08x\n",
sc->sc_txdescs[i].ad_bufaddr2);
if (i == txs->txs_lastdesc)
break;
}
}
#endif
txstat = sc->sc_txdescs[txs->txs_lastdesc].ad_status;
if (txstat & ADSTAT_OWN)
break;
SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
sc->sc_txfree += txs->txs_ndescs;
bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
0, txs->txs_dmamap->dm_mapsize,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
m_freem(txs->txs_mbuf);
txs->txs_mbuf = NULL;
SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
/*
* Check for errors and collisions.
*/
#ifdef AE_STATS
if (txstat & ADSTAT_Tx_UF)
sc->sc_stats.ts_tx_uf++;
if (txstat & ADSTAT_Tx_TO)
sc->sc_stats.ts_tx_to++;
if (txstat & ADSTAT_Tx_EC)
sc->sc_stats.ts_tx_ec++;
if (txstat & ADSTAT_Tx_LC)
sc->sc_stats.ts_tx_lc++;
#endif
net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
if (txstat & (ADSTAT_Tx_UF | ADSTAT_Tx_TO))
if_statinc_ref(nsr, if_oerrors);
if (txstat & ADSTAT_Tx_EC)
if_statadd_ref(nsr, if_collisions, 16);
else if (ADSTAT_Tx_COLLISIONS(txstat))
if_statadd_ref(nsr, if_collisions,
ADSTAT_Tx_COLLISIONS(txstat));
if (txstat & ADSTAT_Tx_LC)
if_statinc_ref(nsr, if_collisions);
if_statinc_ref(nsr, if_opackets);
IF_STAT_PUTREF(ifp);
}
/*
* If there are no more pending transmissions, cancel the watchdog
* timer.
*/
if (txs == NULL)
ifp->if_timer = 0;
}
#ifdef AE_STATS
void
ae_print_stats(struct ae_softc *sc)
{
printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
device_xname(sc->sc_dev),
sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
}
#endif
/*
* ae_reset:
*
* Perform a soft reset on the chip.
*/
void
ae_reset(struct ae_softc *sc)
{
int i;
AE_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
AE_BARRIER(sc);
/*
* The chip doesn't take itself out of reset automatically.
* We need to do so after 2us.
*/
delay(10);
AE_WRITE(sc, CSR_BUSMODE, 0);
AE_BARRIER(sc);
for (i = 0; i < 1000; i++) {
/*
* Wait a bit for the reset to complete before peeking
* at the chip again.
*/
delay(10);
if (AE_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
break;
}
if (AE_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
printf("%s: reset failed to complete\n", device_xname(sc->sc_dev));
delay(1000);
}
/*
* ae_init: [ ifnet interface function ]
*
* Initialize the interface. Must be called at splnet().
*/
static int
ae_init(struct ifnet *ifp)
{
struct ae_softc *sc = ifp->if_softc;
struct ae_txsoft *txs;
struct ae_rxsoft *rxs;
const uint8_t *enaddr;
int i, error = 0;
if ((error = ae_enable(sc)) != 0)
goto out;
/*
* Cancel any pending I/O.
*/
ae_stop(ifp, 0);
/*
* Reset the chip to a known state.
*/
ae_reset(sc);
/*
* Initialize the BUSMODE register.
*/
AE_WRITE(sc, CSR_BUSMODE,
/* XXX: not sure if this is a good thing or not... */
//BUSMODE_ALIGN_16B |
BUSMODE_BAR | BUSMODE_BLE | BUSMODE_PBL_4LW);
AE_BARRIER(sc);
/*
* Initialize the transmit descriptor ring.
*/
memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
for (i = 0; i < AE_NTXDESC; i++) {
sc->sc_txdescs[i].ad_ctl = 0;
sc->sc_txdescs[i].ad_bufaddr2 =
AE_CDTXADDR(sc, AE_NEXTTX(i));
}
sc->sc_txdescs[AE_NTXDESC - 1].ad_ctl |= ADCTL_ER;
AE_CDTXSYNC(sc, 0, AE_NTXDESC,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
sc->sc_txfree = AE_NTXDESC;
sc->sc_txnext = 0;
/*
* Initialize the transmit job descriptors.
*/
SIMPLEQ_INIT(&sc->sc_txfreeq);
SIMPLEQ_INIT(&sc->sc_txdirtyq);
for (i = 0; i < AE_TXQUEUELEN; i++) {
txs = &sc->sc_txsoft[i];
txs->txs_mbuf = NULL;
SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
}
/*
* Initialize the receive descriptor and receive job
* descriptor rings.
*/
for (i = 0; i < AE_NRXDESC; i++) {
rxs = &sc->sc_rxsoft[i];
if (rxs->rxs_mbuf == NULL) {
if ((error = ae_add_rxbuf(sc, i)) != 0) {
printf("%s: unable to allocate or map rx "
"buffer %d, error = %d\n",
device_xname(sc->sc_dev), i, error);
/*
* XXX Should attempt to run with fewer receive
* XXX buffers instead of just failing.
*/
ae_rxdrain(sc);
goto out;
}
} else
AE_INIT_RXDESC(sc, i);
}
sc->sc_rxptr = 0;
/*
* Initialize the interrupt mask and enable interrupts.
*/
/* normal interrupts */
sc->sc_inten = STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
/* abnormal interrupts */
sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
STATUS_RU | STATUS_RPS | STATUS_SE | STATUS_AIS;
sc->sc_rxint_mask = STATUS_RI | STATUS_RU;
sc->sc_txint_mask = STATUS_TI | STATUS_UNF | STATUS_TJT;
sc->sc_rxint_mask &= sc->sc_inten;
sc->sc_txint_mask &= sc->sc_inten;
AE_WRITE(sc, CSR_INTEN, sc->sc_inten);
AE_WRITE(sc, CSR_STATUS, 0xffffffff);
/*
* Give the transmit and receive rings to the chip.
*/
AE_WRITE(sc, CSR_TXLIST, AE_CDTXADDR(sc, sc->sc_txnext));
AE_WRITE(sc, CSR_RXLIST, AE_CDRXADDR(sc, sc->sc_rxptr));
AE_BARRIER(sc);
/*
* Set the station address.
*/
enaddr = CLLADDR(ifp->if_sadl);
AE_WRITE(sc, CSR_MACHI, enaddr[5] << 16 | enaddr[4]);
AE_WRITE(sc, CSR_MACLO, enaddr[3] << 24 | enaddr[2] << 16 |
enaddr[1] << 8 | enaddr[0]);
AE_BARRIER(sc);
/*
* Set the receive filter. This will start the transmit and
* receive processes.
*/
ae_filter_setup(sc);
/*
* Set the current media.
*/
if ((error = ether_mediachange(ifp)) != 0)
goto out;
/*
* Start the mac.
*/
AE_SET(sc, CSR_MACCTL, MACCTL_RE | MACCTL_TE);
AE_BARRIER(sc);
/*
* Write out the opmode.
*/
AE_WRITE(sc, CSR_OPMODE, OPMODE_SR | OPMODE_ST |
ae_txthresh[sc->sc_txthresh].txth_opmode);
/*
* Start the receive process.
*/
AE_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
AE_BARRIER(sc);
if (sc->sc_tick != NULL) {
/* Start the one second clock. */
callout_reset(&sc->sc_tick_callout, hz >> 3, sc->sc_tick, sc);
}
/*
* Note that the interface is now running.
*/
ifp->if_flags |= IFF_RUNNING;
sc->sc_if_flags = ifp->if_flags;
out:
if (error) {
ifp->if_flags &= ~IFF_RUNNING;
ifp->if_timer = 0;
printf("%s: interface not running\n", device_xname(sc->sc_dev));
}
return (error);
}
/*
* ae_enable:
*
* Enable the chip.
*/
static int
ae_enable(struct ae_softc *sc)
{
if (AE_IS_ENABLED(sc) == 0) {
sc->sc_ih = arbus_intr_establish(sc->sc_cirq, sc->sc_mirq,
ae_intr, sc);
if (sc->sc_ih == NULL) {
printf("%s: unable to establish interrupt\n",
device_xname(sc->sc_dev));
return (EIO);
}
sc->sc_flags |= AE_ENABLED;
}
return (0);
}
/*
* ae_disable:
*
* Disable the chip.
*/
static void
ae_disable(struct ae_softc *sc)
{
if (AE_IS_ENABLED(sc)) {
arbus_intr_disestablish(sc->sc_ih);
sc->sc_flags &= ~AE_ENABLED;
}
}
/*
* ae_power:
*
* Power management (suspend/resume) hook.
*/
static void
ae_power(int why, void *arg)
{
struct ae_softc *sc = arg;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
int s;
printf("power called: %d, %x\n", why, (uint32_t)arg);
s = splnet();
switch (why) {
case PWR_STANDBY:
/* do nothing! */
break;
case PWR_SUSPEND:
ae_stop(ifp, 0);
ae_disable(sc);
break;
case PWR_RESUME:
if (ifp->if_flags & IFF_UP) {
ae_enable(sc);
ae_init(ifp);
}
break;
case PWR_SOFTSUSPEND:
case PWR_SOFTSTANDBY:
case PWR_SOFTRESUME:
break;
}
splx(s);
}
/*
* ae_rxdrain:
*
* Drain the receive queue.
*/
static void
ae_rxdrain(struct ae_softc *sc)
{
struct ae_rxsoft *rxs;
int i;
for (i = 0; i < AE_NRXDESC; i++) {
rxs = &sc->sc_rxsoft[i];
if (rxs->rxs_mbuf != NULL) {
bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
m_freem(rxs->rxs_mbuf);
rxs->rxs_mbuf = NULL;
}
}
}
/*
* ae_stop: [ ifnet interface function ]
*
* Stop transmission on the interface.
*/
static void
ae_stop(struct ifnet *ifp, int disable)
{
struct ae_softc *sc = ifp->if_softc;
struct ae_txsoft *txs;
if (sc->sc_tick != NULL) {
/* Stop the one second clock. */
callout_stop(&sc->sc_tick_callout);
}
/* Down the MII. */
mii_down(&sc->sc_mii);
/* Disable interrupts. */
AE_WRITE(sc, CSR_INTEN, 0);
/* Stop the transmit and receive processes. */
AE_WRITE(sc, CSR_OPMODE, 0);
AE_WRITE(sc, CSR_RXLIST, 0);
AE_WRITE(sc, CSR_TXLIST, 0);
AE_CLR(sc, CSR_MACCTL, MACCTL_TE | MACCTL_RE);
AE_BARRIER(sc);
/*
* Release any queued transmit buffers.
*/
while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
if (txs->txs_mbuf != NULL) {
bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
m_freem(txs->txs_mbuf);
txs->txs_mbuf = NULL;
}
SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
}
/*
* Mark the interface down and cancel the watchdog timer.
*/
ifp->if_flags &= ~IFF_RUNNING;
sc->sc_if_flags = ifp->if_flags;
ifp->if_timer = 0;
if (disable) {
ae_rxdrain(sc);
ae_disable(sc);
}
/*
* Reset the chip (needed on some flavors to actually disable it).
*/
ae_reset(sc);
}
/*
* ae_add_rxbuf:
*
* Add a receive buffer to the indicated descriptor.
*/
static int
ae_add_rxbuf(struct ae_softc *sc, int idx)
{
struct ae_rxsoft *rxs = &sc->sc_rxsoft[idx];
struct mbuf *m;
int error;
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL)
return (ENOBUFS);
MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
m_freem(m);
return (ENOBUFS);
}
if (rxs->rxs_mbuf != NULL)
bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
rxs->rxs_mbuf = m;
error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
BUS_DMA_READ | BUS_DMA_NOWAIT);
if (error) {
printf("%s: can't load rx DMA map %d, error = %d\n",
device_xname(sc->sc_dev), idx, error);
panic("ae_add_rxbuf"); /* XXX */
}
bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
AE_INIT_RXDESC(sc, idx);
return (0);
}
/*
* ae_filter_setup:
*
* Set the chip's receive filter.
*/
static void
ae_filter_setup(struct ae_softc *sc)
{
struct ethercom *ec = &sc->sc_ethercom;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct ether_multi *enm;
struct ether_multistep step;
uint32_t hash, mchash[2];
uint32_t macctl = 0;
/*
* If the chip is running, we need to reset the interface,
* and will revisit here (with IFF_RUNNING) clear. The
* chip seems to really not like to have its multicast
* filter programmed without a reset.
*/
if (ifp->if_flags & IFF_RUNNING) {
(void) ae_init(ifp);
return;
}
DPRINTF(sc, ("%s: ae_filter_setup: sc_flags 0x%08x\n",
device_xname(sc->sc_dev), sc->sc_flags));
macctl = AE_READ(sc, CSR_MACCTL);
macctl &= ~(MACCTL_PR | MACCTL_PM);
macctl |= MACCTL_HASH;
macctl |= MACCTL_HBD;
macctl |= MACCTL_PR;
if (ifp->if_flags & IFF_PROMISC) {
macctl |= MACCTL_PR;
goto allmulti;
}
mchash[0] = mchash[1] = 0;
ETHER_LOCK(ec);
ETHER_FIRST_MULTI(step, ec, enm);
while (enm != NULL) {
if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
/*
* We must listen to a range of multicast addresses.
* For now, just accept all multicasts, rather than
* trying to set only those filter bits needed to match
* the range. (At this time, the only use of address
* ranges is for IP multicast routing, for which the
* range is big enough to require all bits set.)
*/
ETHER_UNLOCK(ec);
goto allmulti;
}
/* Verify whether we use big or little endian hashes */
hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) & 0x3f;
mchash[hash >> 5] |= 1 << (hash & 0x1f);
ETHER_NEXT_MULTI(step, enm);
}
ETHER_UNLOCK(ec);
ifp->if_flags &= ~IFF_ALLMULTI;
goto setit;
allmulti:
ifp->if_flags |= IFF_ALLMULTI;
mchash[0] = mchash[1] = 0xffffffff;
macctl |= MACCTL_PM;
setit:
AE_WRITE(sc, CSR_HTHI, mchash[0]);
AE_WRITE(sc, CSR_HTHI, mchash[1]);
AE_WRITE(sc, CSR_MACCTL, macctl);
AE_BARRIER(sc);
DPRINTF(sc, ("%s: ae_filter_setup: returning %x\n",
device_xname(sc->sc_dev), macctl));
}
/*
* ae_idle:
*
* Cause the transmit and/or receive processes to go idle.
*/
void
ae_idle(struct ae_softc *sc, uint32_t bits)
{
static const char * const txstate_names[] = {
"STOPPED",
"RUNNING - FETCH",
"RUNNING - WAIT",
"RUNNING - READING",
"-- RESERVED --",
"RUNNING - SETUP",
"SUSPENDED",
"RUNNING - CLOSE",
};
static const char * const rxstate_names[] = {
"STOPPED",
"RUNNING - FETCH",
"RUNNING - CHECK",
"RUNNING - WAIT",
"SUSPENDED",
"RUNNING - CLOSE",
"RUNNING - FLUSH",
"RUNNING - QUEUE",
};
uint32_t csr, ackmask = 0;
int i;
if (bits & OPMODE_ST)
ackmask |= STATUS_TPS;
if (bits & OPMODE_SR)
ackmask |= STATUS_RPS;
AE_CLR(sc, CSR_OPMODE, bits);
for (i = 0; i < 1000; i++) {
if (AE_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
break;
delay(10);
}
csr = AE_READ(sc, CSR_STATUS);
if ((csr & ackmask) != ackmask) {
if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
(csr & STATUS_TS) != STATUS_TS_STOPPED) {
printf("%s: transmit process failed to idle: "
"state %s\n", device_xname(sc->sc_dev),
txstate_names[(csr & STATUS_TS) >> 20]);
}
if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
(csr & STATUS_RS) != STATUS_RS_STOPPED) {
printf("%s: receive process failed to idle: "
"state %s\n", device_xname(sc->sc_dev),
rxstate_names[(csr & STATUS_RS) >> 17]);
}
}
}
/*****************************************************************************
* Support functions for MII-attached media.
*****************************************************************************/
/*
* ae_mii_tick:
*
* One second timer, used to tick the MII.
*/
static void
ae_mii_tick(void *arg)
{
struct ae_softc *sc = arg;
int s;
if (!device_is_active(sc->sc_dev))
return;
s = splnet();
mii_tick(&sc->sc_mii);
splx(s);
callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
}
/*
* ae_mii_statchg: [mii interface function]
*
* Callback from PHY when media changes.
*/
static void
ae_mii_statchg(struct ifnet *ifp)
{
struct ae_softc *sc = ifp->if_softc;
uint32_t macctl, flowc;
//opmode = AE_READ(sc, CSR_OPMODE);
macctl = AE_READ(sc, CSR_MACCTL);
/* XXX: do we need to do this? */
/* Idle the transmit and receive processes. */
//ae_idle(sc, OPMODE_ST | OPMODE_SR);
if (sc->sc_mii.mii_media_active & IFM_FDX) {
flowc = FLOWC_FCE;
macctl &= ~MACCTL_DRO;
macctl |= MACCTL_FDX;
} else {
flowc = 0; /* cannot do flow control in HDX */
macctl |= MACCTL_DRO;
macctl &= ~MACCTL_FDX;
}
AE_WRITE(sc, CSR_FLOWC, flowc);
AE_WRITE(sc, CSR_MACCTL, macctl);
/* restore operational mode */
//AE_WRITE(sc, CSR_OPMODE, opmode);
AE_BARRIER(sc);
}
/*
* ae_mii_readreg:
*
* Read a PHY register.
*/
static int
ae_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
{
struct ae_softc *sc = device_private(self);
uint32_t addr;
int i;
addr = (phy << MIIADDR_PHY_SHIFT) | (reg << MIIADDR_REG_SHIFT);
AE_WRITE(sc, CSR_MIIADDR, addr);
AE_BARRIER(sc);
for (i = 0; i < 100000000; i++) {
if ((AE_READ(sc, CSR_MIIADDR) & MIIADDR_BUSY) == 0)
break;
}
if (i >= 100000000)
return ETIMEDOUT;
*val = AE_READ(sc, CSR_MIIDATA) & 0xffff;
return 0;
}
/*
* ae_mii_writereg:
*
* Write a PHY register.
*/
static int
ae_mii_writereg(device_t self, int phy, int reg, uint16_t val)
{
struct ae_softc *sc = device_private(self);
uint32_t addr;
int i;
/* write the data register */
AE_WRITE(sc, CSR_MIIDATA, val);
/* write the address to latch it in */
addr = (phy << MIIADDR_PHY_SHIFT) | (reg << MIIADDR_REG_SHIFT) |
MIIADDR_WRITE;
AE_WRITE(sc, CSR_MIIADDR, addr);
AE_BARRIER(sc);
for (i = 0; i < 100000000; i++) {
if ((AE_READ(sc, CSR_MIIADDR) & MIIADDR_BUSY) == 0)
break;
}
if (i >= 100000000)
return ETIMEDOUT;
return 0;
}
|