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
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
|
/*-
* Copyright (c) 2011, Aleksandr Rybalko
* based on hard work
* by Alexander Egorenkov <egorenar@gmail.com>
* and by Damien Bergamini <damien.bergamini@free.fr>
* All rights reserved.
*
* 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 unmodified, 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 AUTHOR 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 AUTHOR 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "if_rtvar.h"
#include "if_rtreg.h"
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <net/if_vlan_var.h>
#include <net/bpf.h>
#include <machine/bus.h>
#include <machine/cache.h>
#include <machine/cpufunc.h>
#include <machine/resource.h>
#include <vm/vm_param.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <machine/pmap.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#include <mips/rt305x/rt305x_sysctlvar.h>
#include <mips/rt305x/rt305xreg.h>
#ifdef IF_RT_PHY_SUPPORT
#include "miibus_if.h"
#endif
/*
* Defines and macros
*/
#define RT_MAX_AGG_SIZE 3840
#define RT_TX_DATA_SEG0_SIZE MJUMPAGESIZE
#define RT_MS(_v, _f) (((_v) & _f) >> _f##_S)
#define RT_SM(_v, _f) (((_v) << _f##_S) & _f)
#define RT_TX_WATCHDOG_TIMEOUT 5
/*
* Static function prototypes
*/
static int rt_probe(device_t dev);
static int rt_attach(device_t dev);
static int rt_detach(device_t dev);
static int rt_shutdown(device_t dev);
static int rt_suspend(device_t dev);
static int rt_resume(device_t dev);
static void rt_init_locked(void *priv);
static void rt_init(void *priv);
static void rt_stop_locked(void *priv);
static void rt_stop(void *priv);
static void rt_start(struct ifnet *ifp);
static int rt_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
static void rt_periodic(void *arg);
static void rt_tx_watchdog(void *arg);
static void rt_intr(void *arg);
static void rt_tx_coherent_intr(struct rt_softc *sc);
static void rt_rx_coherent_intr(struct rt_softc *sc);
static void rt_rx_delay_intr(struct rt_softc *sc);
static void rt_tx_delay_intr(struct rt_softc *sc);
static void rt_rx_intr(struct rt_softc *sc);
static void rt_tx_intr(struct rt_softc *sc, int qid);
static void rt_rx_done_task(void *context, int pending);
static void rt_tx_done_task(void *context, int pending);
static void rt_periodic_task(void *context, int pending);
static int rt_rx_eof(struct rt_softc *sc, int limit);
static void rt_tx_eof(struct rt_softc *sc,
struct rt_softc_tx_ring *ring);
static void rt_update_stats(struct rt_softc *sc);
static void rt_watchdog(struct rt_softc *sc);
static void rt_update_raw_counters(struct rt_softc *sc);
static void rt_intr_enable(struct rt_softc *sc, uint32_t intr_mask);
static void rt_intr_disable(struct rt_softc *sc, uint32_t intr_mask);
static int rt_txrx_enable(struct rt_softc *sc);
static int rt_alloc_rx_ring(struct rt_softc *sc,
struct rt_softc_rx_ring *ring);
static void rt_reset_rx_ring(struct rt_softc *sc,
struct rt_softc_rx_ring *ring);
static void rt_free_rx_ring(struct rt_softc *sc,
struct rt_softc_rx_ring *ring);
static int rt_alloc_tx_ring(struct rt_softc *sc,
struct rt_softc_tx_ring *ring, int qid);
static void rt_reset_tx_ring(struct rt_softc *sc,
struct rt_softc_tx_ring *ring);
static void rt_free_tx_ring(struct rt_softc *sc,
struct rt_softc_tx_ring *ring);
static void rt_dma_map_addr(void *arg, bus_dma_segment_t *segs,
int nseg, int error);
static void rt_sysctl_attach(struct rt_softc *sc);
#ifdef IF_RT_PHY_SUPPORT
void rt_miibus_statchg(device_t);
static int rt_miibus_readreg(device_t, int, int);
static int rt_miibus_writereg(device_t, int, int, int);
#endif
static int rt_ifmedia_upd(struct ifnet *);
static void rt_ifmedia_sts(struct ifnet *, struct ifmediareq *);
static SYSCTL_NODE(_hw, OID_AUTO, rt, CTLFLAG_RD, 0, "RT driver parameters");
#ifdef IF_RT_DEBUG
static int rt_debug = 0;
SYSCTL_INT(_hw_rt, OID_AUTO, debug, CTLFLAG_RW, &rt_debug, 0,
"RT debug level");
TUNABLE_INT("hw.rt.debug", &rt_debug);
#endif
static int
rt_probe(device_t dev)
{
device_set_desc(dev, "Ralink RT305XF onChip Ethernet MAC");
return (BUS_PROBE_NOWILDCARD);
}
/*
* macaddr_atoi - translate string MAC address to uint8_t array
*/
static int
macaddr_atoi(const char *str, uint8_t *mac)
{
int count, i;
unsigned int amac[ETHER_ADDR_LEN]; /* Aligned version */
count = sscanf(str, "%x%*c%x%*c%x%*c%x%*c%x%*c%x",
&amac[0], &amac[1], &amac[2],
&amac[3], &amac[4], &amac[5]);
if (count < ETHER_ADDR_LEN) {
memset(mac, 0, ETHER_ADDR_LEN);
return (1);
}
/* Copy aligned to result */
for (i = 0; i < ETHER_ADDR_LEN; i ++)
mac[i] = (amac[i] & 0xff);
return (0);
}
#ifdef USE_GENERATED_MAC_ADDRESS
static char *
kernenv_next(char *cp)
{
if (cp != NULL) {
while (*cp != 0)
cp++;
cp++;
if (*cp == 0)
cp = NULL;
}
return (cp);
}
/*
* generate_mac(uin8_t *mac)
* This is MAC address generator for cases when real device MAC address
* unknown or not yet accessible.
* Use 'b','s','d' signature and 3 octets from CRC32 on kenv.
* MAC = 'b', 's', 'd', CRC[3]^CRC[2], CRC[1], CRC[0]
*
* Output - MAC address, that do not change between reboots, if hints or
* bootloader info unchange.
*/
static void
generate_mac(uint8_t *mac)
{
unsigned char *cp;
int i = 0;
uint32_t crc = 0xffffffff;
/* Generate CRC32 on kenv */
if (dynamic_kenv) {
for (cp = kenvp[0]; cp != NULL; cp = kenvp[++i]) {
crc = calculate_crc32c(crc, cp, strlen(cp) + 1);
}
} else {
for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
crc = calculate_crc32c(crc, cp, strlen(cp) + 1);
}
}
crc = ~crc;
mac[0] = 'b';
mac[1] = 's';
mac[2] = 'd';
mac[3] = (crc >> 24) ^ ((crc >> 16) & 0xff);
mac[4] = (crc >> 8) & 0xff;
mac[5] = crc & 0xff;
}
#endif
/*
* ether_request_mac - try to find usable MAC address.
*/
static int
ether_request_mac(device_t dev, uint8_t *mac)
{
char *var;
/*
* "ethaddr" is passed via envp on RedBoot platforms
* "kmac" is passed via argv on RouterBOOT platforms
*/
#if defined(__U_BOOT__) || defined(__REDBOOT__) || defined(__ROUTERBOOT__)
if ((var = getenv("ethaddr")) != NULL ||
(var = getenv("kmac")) != NULL ) {
if(!macaddr_atoi(var, mac)) {
printf("%s: use %s macaddr from KENV\n",
device_get_nameunit(dev), var);
freeenv(var);
return (0);
}
freeenv(var);
}
#endif
/*
* Try from hints
* hint.[dev].[unit].macaddr
*/
if (!resource_string_value(device_get_name(dev),
device_get_unit(dev), "macaddr", (const char **)&var)) {
if(!macaddr_atoi(var, mac)) {
printf("%s: use %s macaddr from hints\n",
device_get_nameunit(dev), var);
return (0);
}
}
#ifdef USE_GENERATED_MAC_ADDRESS
generate_mac(mac);
device_printf(dev, "use generated %02x:%02x:%02x:%02x:%02x:%02x "
"macaddr\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
#else
/* Hardcoded */
mac[0] = 0x00;
mac[1] = 0x18;
mac[2] = 0xe7;
mac[3] = 0xd5;
mac[4] = 0x83;
mac[5] = 0x90;
device_printf(dev, "use hardcoded 00:18:e7:d5:83:90 macaddr\n");
#endif
return (0);
}
static int
rt_attach(device_t dev)
{
struct rt_softc *sc;
struct ifnet *ifp;
int error, i;
sc = device_get_softc(dev);
sc->dev = dev;
mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
MTX_DEF | MTX_RECURSE);
sc->mem_rid = 0;
sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
RF_ACTIVE);
if (sc->mem == NULL) {
device_printf(dev, "could not allocate memory resource\n");
error = ENXIO;
goto fail;
}
sc->bst = rman_get_bustag(sc->mem);
sc->bsh = rman_get_bushandle(sc->mem);
sc->irq_rid = 0;
sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
RF_ACTIVE);
if (sc->irq == NULL) {
device_printf(dev,
"could not allocate interrupt resource\n");
error = ENXIO;
goto fail;
}
#ifdef IF_RT_DEBUG
sc->debug = rt_debug;
SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
"debug", CTLFLAG_RW, &sc->debug, 0, "rt debug level");
#endif
device_printf(dev, "RT305XF Ethernet MAC (rev 0x%08x)\n",
sc->mac_rev);
/* Reset hardware */
RT_WRITE(sc, GE_PORT_BASE + FE_RST_GLO, PSE_RESET);
RT_WRITE(sc, GDMA1_BASE + GDMA_FWD_CFG,
(
GDM_ICS_EN | /* Enable IP Csum */
GDM_TCS_EN | /* Enable TCP Csum */
GDM_UCS_EN | /* Enable UDP Csum */
GDM_STRPCRC | /* Strip CRC from packet */
GDM_DST_PORT_CPU << GDM_UFRC_P_SHIFT | /* Forward UCast to CPU */
GDM_DST_PORT_CPU << GDM_BFRC_P_SHIFT | /* Forward BCast to CPU */
GDM_DST_PORT_CPU << GDM_MFRC_P_SHIFT | /* Forward MCast to CPU */
GDM_DST_PORT_CPU << GDM_OFRC_P_SHIFT /* Forward Other to CPU */
));
/* allocate Tx and Rx rings */
for (i = 0; i < RT_SOFTC_TX_RING_COUNT; i++) {
error = rt_alloc_tx_ring(sc, &sc->tx_ring[i], i);
if (error != 0) {
device_printf(dev, "could not allocate Tx ring #%d\n",
i);
goto fail;
}
}
sc->tx_ring_mgtqid = 5;
error = rt_alloc_rx_ring(sc, &sc->rx_ring);
if (error != 0) {
device_printf(dev, "could not allocate Rx ring\n");
goto fail;
}
callout_init(&sc->periodic_ch, 0);
callout_init_mtx(&sc->tx_watchdog_ch, &sc->lock, 0);
ifp = sc->ifp = if_alloc(IFT_ETHER);
if (ifp == NULL) {
device_printf(dev, "could not if_alloc()\n");
error = ENOMEM;
goto fail;
}
ifp->if_softc = sc;
if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_init = rt_init;
ifp->if_ioctl = rt_ioctl;
ifp->if_start = rt_start;
#define RT_TX_QLEN 256
IFQ_SET_MAXLEN(&ifp->if_snd, RT_TX_QLEN);
ifp->if_snd.ifq_drv_maxlen = RT_TX_QLEN;
IFQ_SET_READY(&ifp->if_snd);
#ifdef IF_RT_PHY_SUPPORT
error = mii_attach(dev, &sc->rt_miibus, ifp, rt_ifmedia_upd,
rt_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
if (error != 0) {
device_printf(dev, "attaching PHYs failed\n");
error = ENXIO;
goto fail;
}
#else
ifmedia_init(&sc->rt_ifmedia, 0, rt_ifmedia_upd, rt_ifmedia_sts);
ifmedia_add(&sc->rt_ifmedia, IFM_ETHER | IFM_100_TX | IFM_FDX, 0,
NULL);
ifmedia_set(&sc->rt_ifmedia, IFM_ETHER | IFM_100_TX | IFM_FDX);
#endif /* IF_RT_PHY_SUPPORT */
ether_request_mac(dev, sc->mac_addr);
ether_ifattach(ifp, sc->mac_addr);
/*
* Tell the upper layer(s) we support long frames.
*/
ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
ifp->if_capabilities |= IFCAP_VLAN_MTU;
ifp->if_capenable |= IFCAP_VLAN_MTU;
ifp->if_capabilities |= IFCAP_RXCSUM|IFCAP_TXCSUM;
ifp->if_capenable |= IFCAP_RXCSUM|IFCAP_TXCSUM;
/* init task queue */
TASK_INIT(&sc->rx_done_task, 0, rt_rx_done_task, sc);
TASK_INIT(&sc->tx_done_task, 0, rt_tx_done_task, sc);
TASK_INIT(&sc->periodic_task, 0, rt_periodic_task, sc);
sc->rx_process_limit = 100;
sc->taskqueue = taskqueue_create("rt_taskq", M_NOWAIT,
taskqueue_thread_enqueue, &sc->taskqueue);
taskqueue_start_threads(&sc->taskqueue, 1, PI_NET, "%s taskq",
device_get_nameunit(sc->dev));
rt_sysctl_attach(sc);
/* set up interrupt */
error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
NULL, rt_intr, sc, &sc->irqh);
if (error != 0) {
printf("%s: could not set up interrupt\n",
device_get_nameunit(dev));
goto fail;
}
#ifdef IF_RT_DEBUG
device_printf(dev, "debug var at %#08x\n", (u_int)&(sc->debug));
#endif
return (0);
fail:
/* free Tx and Rx rings */
for (i = 0; i < RT_SOFTC_TX_RING_COUNT; i++)
rt_free_tx_ring(sc, &sc->tx_ring[i]);
rt_free_rx_ring(sc, &sc->rx_ring);
mtx_destroy(&sc->lock);
if (sc->mem != NULL)
bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid,
sc->mem);
if (sc->irq != NULL)
bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
sc->irq);
return (error);
}
/*
* Set media options.
*/
static int
rt_ifmedia_upd(struct ifnet *ifp)
{
struct rt_softc *sc;
#ifdef IF_RT_PHY_SUPPORT
struct mii_data *mii;
struct mii_softc *miisc;
int error = 0;
sc = ifp->if_softc;
RT_SOFTC_LOCK(sc);
mii = device_get_softc(sc->rt_miibus);
LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
PHY_RESET(miisc);
error = mii_mediachg(mii);
RT_SOFTC_UNLOCK(sc);
return (error);
#else /* !IF_RT_PHY_SUPPORT */
struct ifmedia *ifm;
struct ifmedia_entry *ife;
sc = ifp->if_softc;
ifm = &sc->rt_ifmedia;
ife = ifm->ifm_cur;
if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
return (EINVAL);
if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
device_printf(sc->dev,
"AUTO is not supported for multiphy MAC");
return (EINVAL);
}
/*
* Ignore everything
*/
return (0);
#endif /* IF_RT_PHY_SUPPORT */
}
/*
* Report current media status.
*/
static void
rt_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
{
#ifdef IF_RT_PHY_SUPPORT
struct rt_softc *sc;
struct mii_data *mii;
sc = ifp->if_softc;
RT_SOFTC_LOCK(sc);
mii = device_get_softc(sc->rt_miibus);
mii_pollstat(mii);
ifmr->ifm_active = mii->mii_media_active;
ifmr->ifm_status = mii->mii_media_status;
ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
RT_SOFTC_UNLOCK(sc);
#else /* !IF_RT_PHY_SUPPORT */
ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
#endif /* IF_RT_PHY_SUPPORT */
}
static int
rt_detach(device_t dev)
{
struct rt_softc *sc;
struct ifnet *ifp;
int i;
sc = device_get_softc(dev);
ifp = sc->ifp;
RT_DPRINTF(sc, RT_DEBUG_ANY, "detaching\n");
RT_SOFTC_LOCK(sc);
ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
callout_stop(&sc->periodic_ch);
callout_stop(&sc->tx_watchdog_ch);
taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
taskqueue_drain(sc->taskqueue, &sc->periodic_task);
/* free Tx and Rx rings */
for (i = 0; i < RT_SOFTC_TX_RING_COUNT; i++)
rt_free_tx_ring(sc, &sc->tx_ring[i]);
rt_free_rx_ring(sc, &sc->rx_ring);
RT_SOFTC_UNLOCK(sc);
#ifdef IF_RT_PHY_SUPPORT
if (sc->rt_miibus != NULL)
device_delete_child(dev, sc->rt_miibus);
#endif
ether_ifdetach(ifp);
if_free(ifp);
taskqueue_free(sc->taskqueue);
mtx_destroy(&sc->lock);
bus_generic_detach(dev);
bus_teardown_intr(dev, sc->irq, sc->irqh);
bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
return (0);
}
static int
rt_shutdown(device_t dev)
{
struct rt_softc *sc;
sc = device_get_softc(dev);
RT_DPRINTF(sc, RT_DEBUG_ANY, "shutting down\n");
rt_stop(sc);
return (0);
}
static int
rt_suspend(device_t dev)
{
struct rt_softc *sc;
sc = device_get_softc(dev);
RT_DPRINTF(sc, RT_DEBUG_ANY, "suspending\n");
rt_stop(sc);
return (0);
}
static int
rt_resume(device_t dev)
{
struct rt_softc *sc;
struct ifnet *ifp;
sc = device_get_softc(dev);
ifp = sc->ifp;
RT_DPRINTF(sc, RT_DEBUG_ANY, "resuming\n");
if (ifp->if_flags & IFF_UP)
rt_init(sc);
return (0);
}
/*
* rt_init_locked - Run initialization process having locked mtx.
*/
static void
rt_init_locked(void *priv)
{
struct rt_softc *sc;
struct ifnet *ifp;
#ifdef IF_RT_PHY_SUPPORT
struct mii_data *mii;
#endif
int i, ntries;
uint32_t tmp;
sc = priv;
ifp = sc->ifp;
#ifdef IF_RT_PHY_SUPPORT
mii = device_get_softc(sc->rt_miibus);
#endif
RT_DPRINTF(sc, RT_DEBUG_ANY, "initializing\n");
RT_SOFTC_ASSERT_LOCKED(sc);
/* hardware reset */
RT_WRITE(sc, GE_PORT_BASE + FE_RST_GLO, PSE_RESET);
rt305x_sysctl_set(SYSCTL_RSTCTRL, SYSCTL_RSTCTRL_FRENG);
/* Fwd to CPU (uni|broad|multi)cast and Unknown */
RT_WRITE(sc, GDMA1_BASE + GDMA_FWD_CFG,
(
GDM_ICS_EN | /* Enable IP Csum */
GDM_TCS_EN | /* Enable TCP Csum */
GDM_UCS_EN | /* Enable UDP Csum */
GDM_STRPCRC | /* Strip CRC from packet */
GDM_DST_PORT_CPU << GDM_UFRC_P_SHIFT | /* Forward UCast to CPU */
GDM_DST_PORT_CPU << GDM_BFRC_P_SHIFT | /* Forward BCast to CPU */
GDM_DST_PORT_CPU << GDM_MFRC_P_SHIFT | /* Forward MCast to CPU */
GDM_DST_PORT_CPU << GDM_OFRC_P_SHIFT /* Forward Other to CPU */
));
/* disable DMA engine */
RT_WRITE(sc, PDMA_BASE + PDMA_GLO_CFG, 0);
RT_WRITE(sc, PDMA_BASE + PDMA_RST_IDX, 0xffffffff);
/* wait while DMA engine is busy */
for (ntries = 0; ntries < 100; ntries++) {
tmp = RT_READ(sc, PDMA_BASE + PDMA_GLO_CFG);
if (!(tmp & (FE_TX_DMA_BUSY | FE_RX_DMA_BUSY)))
break;
DELAY(1000);
}
if (ntries == 100) {
device_printf(sc->dev, "timeout waiting for DMA engine\n");
goto fail;
}
/* reset Rx and Tx rings */
tmp = FE_RST_DRX_IDX0 |
FE_RST_DTX_IDX3 |
FE_RST_DTX_IDX2 |
FE_RST_DTX_IDX1 |
FE_RST_DTX_IDX0;
RT_WRITE(sc, PDMA_BASE + PDMA_RST_IDX, tmp);
/* XXX switch set mac address */
for (i = 0; i < RT_SOFTC_TX_RING_COUNT; i++)
rt_reset_tx_ring(sc, &sc->tx_ring[i]);
for (i = 0; i < RT_SOFTC_TX_RING_COUNT; i++) {
/* update TX_BASE_PTRx */
RT_WRITE(sc, PDMA_BASE + TX_BASE_PTR(i),
sc->tx_ring[i].desc_phys_addr);
RT_WRITE(sc, PDMA_BASE + TX_MAX_CNT(i),
RT_SOFTC_TX_RING_DESC_COUNT);
RT_WRITE(sc, PDMA_BASE + TX_CTX_IDX(i), 0);
}
/* init Rx ring */
rt_reset_rx_ring(sc, &sc->rx_ring);
/* update RX_BASE_PTR0 */
RT_WRITE(sc, PDMA_BASE + RX_BASE_PTR0,
sc->rx_ring.desc_phys_addr);
RT_WRITE(sc, PDMA_BASE + RX_MAX_CNT0,
RT_SOFTC_RX_RING_DATA_COUNT);
RT_WRITE(sc, PDMA_BASE + RX_CALC_IDX0,
RT_SOFTC_RX_RING_DATA_COUNT - 1);
/* write back DDONE, 16byte burst enable RX/TX DMA */
RT_WRITE(sc, PDMA_BASE + PDMA_GLO_CFG,
FE_TX_WB_DDONE | FE_DMA_BT_SIZE16 | FE_RX_DMA_EN | FE_TX_DMA_EN);
/* disable interrupts mitigation */
RT_WRITE(sc, PDMA_BASE + DELAY_INT_CFG, 0);
/* clear pending interrupts */
RT_WRITE(sc, GE_PORT_BASE + FE_INT_STATUS, 0xffffffff);
/* enable interrupts */
tmp = CNT_PPE_AF |
CNT_GDM_AF |
PSE_P2_FC |
GDM_CRC_DROP |
PSE_BUF_DROP |
GDM_OTHER_DROP |
PSE_P1_FC |
PSE_P0_FC |
PSE_FQ_EMPTY |
INT_TX_COHERENT |
INT_RX_COHERENT |
INT_TXQ3_DONE |
INT_TXQ2_DONE |
INT_TXQ1_DONE |
INT_TXQ0_DONE |
INT_RX_DONE;
sc->intr_enable_mask = tmp;
RT_WRITE(sc, GE_PORT_BASE + FE_INT_ENABLE, tmp);
if (rt_txrx_enable(sc) != 0)
goto fail;
#ifdef IF_RT_PHY_SUPPORT
if (mii) mii_mediachg(mii);
#endif /* IF_RT_PHY_SUPPORT */
ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
ifp->if_drv_flags |= IFF_DRV_RUNNING;
sc->periodic_round = 0;
callout_reset(&sc->periodic_ch, hz / 10, rt_periodic, sc);
return;
fail:
rt_stop_locked(sc);
}
/*
* rt_init - lock and initialize device.
*/
static void
rt_init(void *priv)
{
struct rt_softc *sc;
sc = priv;
RT_SOFTC_LOCK(sc);
rt_init_locked(sc);
RT_SOFTC_UNLOCK(sc);
}
/*
* rt_stop_locked - stop TX/RX w/ lock
*/
static void
rt_stop_locked(void *priv)
{
struct rt_softc *sc;
struct ifnet *ifp;
sc = priv;
ifp = sc->ifp;
RT_DPRINTF(sc, RT_DEBUG_ANY, "stopping\n");
RT_SOFTC_ASSERT_LOCKED(sc);
sc->tx_timer = 0;
ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
callout_stop(&sc->periodic_ch);
callout_stop(&sc->tx_watchdog_ch);
RT_SOFTC_UNLOCK(sc);
taskqueue_block(sc->taskqueue);
/*
* Sometime rt_stop_locked called from isr and we get panic
* When found, I fix it
*/
#ifdef notyet
taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
taskqueue_drain(sc->taskqueue, &sc->periodic_task);
#endif
RT_SOFTC_LOCK(sc);
/* disable interrupts */
RT_WRITE(sc, GE_PORT_BASE + FE_INT_ENABLE, 0);
/* reset adapter */
RT_WRITE(sc, GE_PORT_BASE + FE_RST_GLO, PSE_RESET);
RT_WRITE(sc, GDMA1_BASE + GDMA_FWD_CFG,
(
GDM_ICS_EN | /* Enable IP Csum */
GDM_TCS_EN | /* Enable TCP Csum */
GDM_UCS_EN | /* Enable UDP Csum */
GDM_STRPCRC | /* Strip CRC from packet */
GDM_DST_PORT_CPU << GDM_UFRC_P_SHIFT | /* Forward UCast to CPU */
GDM_DST_PORT_CPU << GDM_BFRC_P_SHIFT | /* Forward BCast to CPU */
GDM_DST_PORT_CPU << GDM_MFRC_P_SHIFT | /* Forward MCast to CPU */
GDM_DST_PORT_CPU << GDM_OFRC_P_SHIFT /* Forward Other to CPU */
));
}
static void
rt_stop(void *priv)
{
struct rt_softc *sc;
sc = priv;
RT_SOFTC_LOCK(sc);
rt_stop_locked(sc);
RT_SOFTC_UNLOCK(sc);
}
/*
* rt_tx_data - transmit packet.
*/
static int
rt_tx_data(struct rt_softc *sc, struct mbuf *m, int qid)
{
struct ifnet *ifp;
struct rt_softc_tx_ring *ring;
struct rt_softc_tx_data *data;
struct rt_txdesc *desc;
struct mbuf *m_d;
bus_dma_segment_t dma_seg[RT_SOFTC_MAX_SCATTER];
int error, ndmasegs, ndescs, i;
KASSERT(qid >= 0 && qid < RT_SOFTC_TX_RING_COUNT,
("%s: Tx data: invalid qid=%d\n",
device_get_nameunit(sc->dev), qid));
RT_SOFTC_TX_RING_ASSERT_LOCKED(&sc->tx_ring[qid]);
ifp = sc->ifp;
ring = &sc->tx_ring[qid];
desc = &ring->desc[ring->desc_cur];
data = &ring->data[ring->data_cur];
error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
dma_seg, &ndmasegs, 0);
if (error != 0) {
/* too many fragments, linearize */
RT_DPRINTF(sc, RT_DEBUG_TX,
"could not load mbuf DMA map, trying to linearize "
"mbuf: ndmasegs=%d, len=%d, error=%d\n",
ndmasegs, m->m_pkthdr.len, error);
m_d = m_collapse(m, M_NOWAIT, 16);
if (m_d == NULL) {
m_freem(m);
m = NULL;
return (ENOMEM);
}
m = m_d;
sc->tx_defrag_packets++;
error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag,
data->dma_map, m, dma_seg, &ndmasegs, 0);
if (error != 0) {
device_printf(sc->dev, "could not load mbuf DMA map: "
"ndmasegs=%d, len=%d, error=%d\n",
ndmasegs, m->m_pkthdr.len, error);
m_freem(m);
return (error);
}
}
if (m->m_pkthdr.len == 0)
ndmasegs = 0;
/* determine how many Tx descs are required */
ndescs = 1 + ndmasegs / 2;
if ((ring->desc_queued + ndescs) >
(RT_SOFTC_TX_RING_DESC_COUNT - 2)) {
RT_DPRINTF(sc, RT_DEBUG_TX,
"there are not enough Tx descs\n");
sc->no_tx_desc_avail++;
bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
m_freem(m);
return (EFBIG);
}
data->m = m;
/* set up Tx descs */
for (i = 0; i < ndmasegs; i += 2) {
/* Set destenation */
desc->dst = (TXDSCR_DST_PORT_GDMA1);
if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
desc->dst |= (TXDSCR_IP_CSUM_GEN|TXDSCR_UDP_CSUM_GEN|
TXDSCR_TCP_CSUM_GEN);
/* Set queue id */
desc->qn = qid;
/* No PPPoE */
desc->pppoe = 0;
/* No VLAN */
desc->vid = 0;
desc->sdp0 = htole32(dma_seg[i].ds_addr);
desc->sdl0 = htole16(dma_seg[i].ds_len |
( ((i+1) == ndmasegs )?RT_TXDESC_SDL0_LASTSEG:0 ));
if ((i+1) < ndmasegs) {
desc->sdp1 = htole32(dma_seg[i+1].ds_addr);
desc->sdl1 = htole16(dma_seg[i+1].ds_len |
( ((i+2) == ndmasegs )?RT_TXDESC_SDL1_LASTSEG:0 ));
} else {
desc->sdp1 = 0;
desc->sdl1 = 0;
}
if ((i+2) < ndmasegs) {
ring->desc_queued++;
ring->desc_cur = (ring->desc_cur + 1) %
RT_SOFTC_TX_RING_DESC_COUNT;
}
desc = &ring->desc[ring->desc_cur];
}
RT_DPRINTF(sc, RT_DEBUG_TX, "sending data: len=%d, ndmasegs=%d, "
"DMA ds_len=%d/%d/%d/%d/%d\n",
m->m_pkthdr.len, ndmasegs,
(int) dma_seg[0].ds_len,
(int) dma_seg[1].ds_len,
(int) dma_seg[2].ds_len,
(int) dma_seg[3].ds_len,
(int) dma_seg[4].ds_len);
bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
BUS_DMASYNC_PREWRITE);
bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
BUS_DMASYNC_PREWRITE);
bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
BUS_DMASYNC_PREWRITE);
ring->desc_queued++;
ring->desc_cur = (ring->desc_cur + 1) % RT_SOFTC_TX_RING_DESC_COUNT;
ring->data_queued++;
ring->data_cur = (ring->data_cur + 1) % RT_SOFTC_TX_RING_DATA_COUNT;
/* kick Tx */
RT_WRITE(sc, PDMA_BASE + TX_CTX_IDX(qid), ring->desc_cur);
return (0);
}
/*
* rt_start - start Transmit/Receive
*/
static void
rt_start(struct ifnet *ifp)
{
struct rt_softc *sc;
struct mbuf *m;
int qid = 0 /* XXX must check QoS priority */;
sc = ifp->if_softc;
if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
return;
for (;;) {
IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
if (m == NULL)
break;
m->m_pkthdr.rcvif = NULL;
RT_SOFTC_TX_RING_LOCK(&sc->tx_ring[qid]);
if (sc->tx_ring[qid].data_queued >=
RT_SOFTC_TX_RING_DATA_COUNT) {
RT_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[qid]);
RT_DPRINTF(sc, RT_DEBUG_TX,
"if_start: Tx ring with qid=%d is full\n", qid);
m_freem(m);
ifp->if_drv_flags |= IFF_DRV_OACTIVE;
ifp->if_oerrors++;
sc->tx_data_queue_full[qid]++;
break;
}
if (rt_tx_data(sc, m, qid) != 0) {
RT_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[qid]);
ifp->if_oerrors++;
break;
}
RT_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[qid]);
sc->tx_timer = RT_TX_WATCHDOG_TIMEOUT;
callout_reset(&sc->tx_watchdog_ch, hz, rt_tx_watchdog, sc);
}
}
/*
* rt_update_promisc - set/clear promiscuous mode. Unused yet, because
* filtering done by attached Ethernet switch.
*/
static void
rt_update_promisc(struct ifnet *ifp)
{
struct rt_softc *sc;
sc = ifp->if_softc;
printf("%s: %s promiscuous mode\n",
device_get_nameunit(sc->dev),
(ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
}
/*
* rt_ioctl - ioctl handler.
*/
static int
rt_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
{
struct rt_softc *sc;
struct ifreq *ifr;
#ifdef IF_RT_PHY_SUPPORT
struct mii_data *mii;
#endif /* IF_RT_PHY_SUPPORT */
int error, startall;
sc = ifp->if_softc;
ifr = (struct ifreq *) data;
error = 0;
switch (cmd) {
case SIOCSIFFLAGS:
startall = 0;
RT_SOFTC_LOCK(sc);
if (ifp->if_flags & IFF_UP) {
if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
if ((ifp->if_flags ^ sc->if_flags) &
IFF_PROMISC)
rt_update_promisc(ifp);
} else {
rt_init_locked(sc);
startall = 1;
}
} else {
if (ifp->if_drv_flags & IFF_DRV_RUNNING)
rt_stop_locked(sc);
}
sc->if_flags = ifp->if_flags;
RT_SOFTC_UNLOCK(sc);
break;
case SIOCGIFMEDIA:
case SIOCSIFMEDIA:
#ifdef IF_RT_PHY_SUPPORT
mii = device_get_softc(sc->rt_miibus);
error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
#else
error = ifmedia_ioctl(ifp, ifr, &sc->rt_ifmedia, cmd);
#endif /* IF_RT_PHY_SUPPORT */
break;
default:
error = ether_ioctl(ifp, cmd, data);
break;
}
return (error);
}
/*
* rt_periodic - Handler of PERIODIC interrupt
*/
static void
rt_periodic(void *arg)
{
struct rt_softc *sc;
sc = arg;
RT_DPRINTF(sc, RT_DEBUG_PERIODIC, "periodic\n");
taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
}
/*
* rt_tx_watchdog - Handler of TX Watchdog
*/
static void
rt_tx_watchdog(void *arg)
{
struct rt_softc *sc;
struct ifnet *ifp;
sc = arg;
ifp = sc->ifp;
if (sc->tx_timer == 0)
return;
if (--sc->tx_timer == 0) {
device_printf(sc->dev, "Tx watchdog timeout: resetting\n");
#ifdef notyet
/*
* XXX: Commented out, because reset break input.
*/
rt_stop_locked(sc);
rt_init_locked(sc);
#endif
ifp->if_oerrors++;
sc->tx_watchdog_timeouts++;
}
callout_reset(&sc->tx_watchdog_ch, hz, rt_tx_watchdog, sc);
}
/*
* rt_cnt_ppe_af - Handler of PPE Counter Table Almost Full interrupt
*/
static void
rt_cnt_ppe_af(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR, "PPE Counter Table Almost Full\n");
}
/*
* rt_cnt_gdm_af - Handler of GDMA 1 & 2 Counter Table Almost Full interrupt
*/
static void
rt_cnt_gdm_af(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR,
"GDMA 1 & 2 Counter Table Almost Full\n");
}
/*
* rt_pse_p2_fc - Handler of PSE port2 (GDMA 2) flow control interrupt
*/
static void
rt_pse_p2_fc(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR,
"PSE port2 (GDMA 2) flow control asserted.\n");
}
/*
* rt_gdm_crc_drop - Handler of GDMA 1/2 discard a packet due to CRC error
* interrupt
*/
static void
rt_gdm_crc_drop(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR,
"GDMA 1 & 2 discard a packet due to CRC error\n");
}
/*
* rt_pse_buf_drop - Handler of buffer sharing limitation interrupt
*/
static void
rt_pse_buf_drop(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR,
"PSE discards a packet due to buffer sharing limitation\n");
}
/*
* rt_gdm_other_drop - Handler of discard on other reason interrupt
*/
static void
rt_gdm_other_drop(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR,
"GDMA 1 & 2 discard a packet due to other reason\n");
}
/*
* rt_pse_p1_fc - Handler of PSE port1 (GDMA 1) flow control interrupt
*/
static void
rt_pse_p1_fc(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR,
"PSE port1 (GDMA 1) flow control asserted.\n");
}
/*
* rt_pse_p0_fc - Handler of PSE port0 (CDMA) flow control interrupt
*/
static void
rt_pse_p0_fc(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR,
"PSE port0 (CDMA) flow control asserted.\n");
}
/*
* rt_pse_fq_empty - Handler of PSE free Q empty threshold reached interrupt
*/
static void
rt_pse_fq_empty(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR,
"PSE free Q empty threshold reached & forced drop "
"condition occurred.\n");
}
/*
* rt_intr - main ISR
*/
static void
rt_intr(void *arg)
{
struct rt_softc *sc;
struct ifnet *ifp;
uint32_t status;
sc = arg;
ifp = sc->ifp;
/* acknowledge interrupts */
status = RT_READ(sc, GE_PORT_BASE + FE_INT_STATUS);
RT_WRITE(sc, GE_PORT_BASE + FE_INT_STATUS, status);
RT_DPRINTF(sc, RT_DEBUG_INTR, "interrupt: status=0x%08x\n", status);
if (status == 0xffffffff || /* device likely went away */
status == 0) /* not for us */
return;
sc->interrupts++;
if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
return;
if (status & CNT_PPE_AF)
rt_cnt_ppe_af(sc);
if (status & CNT_GDM_AF)
rt_cnt_gdm_af(sc);
if (status & PSE_P2_FC)
rt_pse_p2_fc(sc);
if (status & GDM_CRC_DROP)
rt_gdm_crc_drop(sc);
if (status & PSE_BUF_DROP)
rt_pse_buf_drop(sc);
if (status & GDM_OTHER_DROP)
rt_gdm_other_drop(sc);
if (status & PSE_P1_FC)
rt_pse_p1_fc(sc);
if (status & PSE_P0_FC)
rt_pse_p0_fc(sc);
if (status & PSE_FQ_EMPTY)
rt_pse_fq_empty(sc);
if (status & INT_TX_COHERENT)
rt_tx_coherent_intr(sc);
if (status & INT_RX_COHERENT)
rt_rx_coherent_intr(sc);
if (status & RX_DLY_INT)
rt_rx_delay_intr(sc);
if (status & TX_DLY_INT)
rt_tx_delay_intr(sc);
if (status & INT_RX_DONE)
rt_rx_intr(sc);
if (status & INT_TXQ3_DONE)
rt_tx_intr(sc, 3);
if (status & INT_TXQ2_DONE)
rt_tx_intr(sc, 2);
if (status & INT_TXQ1_DONE)
rt_tx_intr(sc, 1);
if (status & INT_TXQ0_DONE)
rt_tx_intr(sc, 0);
}
static void
rt_tx_coherent_intr(struct rt_softc *sc)
{
uint32_t tmp;
int i;
RT_DPRINTF(sc, RT_DEBUG_INTR, "Tx coherent interrupt\n");
sc->tx_coherent_interrupts++;
/* restart DMA engine */
tmp = RT_READ(sc, PDMA_BASE + PDMA_GLO_CFG);
tmp &= ~(FE_TX_WB_DDONE | FE_TX_DMA_EN);
RT_WRITE(sc, PDMA_BASE + PDMA_GLO_CFG, tmp);
for (i = 0; i < RT_SOFTC_TX_RING_COUNT; i++)
rt_reset_tx_ring(sc, &sc->tx_ring[i]);
for (i = 0; i < RT_SOFTC_TX_RING_COUNT; i++) {
RT_WRITE(sc, PDMA_BASE + TX_BASE_PTR(i),
sc->tx_ring[i].desc_phys_addr);
RT_WRITE(sc, PDMA_BASE + TX_MAX_CNT(i),
RT_SOFTC_TX_RING_DESC_COUNT);
RT_WRITE(sc, PDMA_BASE + TX_CTX_IDX(i), 0);
}
rt_txrx_enable(sc);
}
/*
* rt_rx_coherent_intr
*/
static void
rt_rx_coherent_intr(struct rt_softc *sc)
{
uint32_t tmp;
RT_DPRINTF(sc, RT_DEBUG_INTR, "Rx coherent interrupt\n");
sc->rx_coherent_interrupts++;
/* restart DMA engine */
tmp = RT_READ(sc, PDMA_BASE + PDMA_GLO_CFG);
tmp &= ~(FE_RX_DMA_EN);
RT_WRITE(sc, PDMA_BASE + PDMA_GLO_CFG, tmp);
/* init Rx ring */
rt_reset_rx_ring(sc, &sc->rx_ring);
RT_WRITE(sc, PDMA_BASE + RX_BASE_PTR0,
sc->rx_ring.desc_phys_addr);
RT_WRITE(sc, PDMA_BASE + RX_MAX_CNT0,
RT_SOFTC_RX_RING_DATA_COUNT);
RT_WRITE(sc, PDMA_BASE + RX_CALC_IDX0,
RT_SOFTC_RX_RING_DATA_COUNT - 1);
rt_txrx_enable(sc);
}
/*
* rt_rx_intr - a packet received
*/
static void
rt_rx_intr(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR, "Rx interrupt\n");
sc->rx_interrupts++;
RT_SOFTC_LOCK(sc);
if (!(sc->intr_disable_mask & INT_RX_DONE)) {
rt_intr_disable(sc, INT_RX_DONE);
taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
}
sc->intr_pending_mask |= INT_RX_DONE;
RT_SOFTC_UNLOCK(sc);
}
static void
rt_rx_delay_intr(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR, "Rx delay interrupt\n");
sc->rx_delay_interrupts++;
}
static void
rt_tx_delay_intr(struct rt_softc *sc)
{
RT_DPRINTF(sc, RT_DEBUG_INTR, "Tx delay interrupt\n");
sc->tx_delay_interrupts++;
}
/*
* rt_tx_intr - Transsmition of packet done
*/
static void
rt_tx_intr(struct rt_softc *sc, int qid)
{
KASSERT(qid >= 0 && qid < RT_SOFTC_TX_RING_COUNT,
("%s: Tx interrupt: invalid qid=%d\n",
device_get_nameunit(sc->dev), qid));
RT_DPRINTF(sc, RT_DEBUG_INTR, "Tx interrupt: qid=%d\n", qid);
sc->tx_interrupts[qid]++;
RT_SOFTC_LOCK(sc);
if (!(sc->intr_disable_mask & (INT_TXQ0_DONE << qid))) {
rt_intr_disable(sc, (INT_TXQ0_DONE << qid));
taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
}
sc->intr_pending_mask |= (INT_TXQ0_DONE << qid);
RT_SOFTC_UNLOCK(sc);
}
/*
* rt_rx_done_task - run RX task
*/
static void
rt_rx_done_task(void *context, int pending)
{
struct rt_softc *sc;
struct ifnet *ifp;
int again;
sc = context;
ifp = sc->ifp;
RT_DPRINTF(sc, RT_DEBUG_RX, "Rx done task\n");
if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
return;
sc->intr_pending_mask &= ~INT_RX_DONE;
again = rt_rx_eof(sc, sc->rx_process_limit);
RT_SOFTC_LOCK(sc);
if ((sc->intr_pending_mask & INT_RX_DONE) || again) {
RT_DPRINTF(sc, RT_DEBUG_RX,
"Rx done task: scheduling again\n");
taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
} else {
rt_intr_enable(sc, INT_RX_DONE);
}
RT_SOFTC_UNLOCK(sc);
}
/*
* rt_tx_done_task - check for pending TX task in all queues
*/
static void
rt_tx_done_task(void *context, int pending)
{
struct rt_softc *sc;
struct ifnet *ifp;
uint32_t intr_mask;
int i;
sc = context;
ifp = sc->ifp;
RT_DPRINTF(sc, RT_DEBUG_TX, "Tx done task\n");
if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
return;
for (i = RT_SOFTC_TX_RING_COUNT - 1; i >= 0; i--) {
if (sc->intr_pending_mask & (INT_TXQ0_DONE << i)) {
sc->intr_pending_mask &= ~(INT_TXQ0_DONE << i);
rt_tx_eof(sc, &sc->tx_ring[i]);
}
}
sc->tx_timer = 0;
ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
intr_mask = (
INT_TXQ3_DONE |
INT_TXQ2_DONE |
INT_TXQ1_DONE |
INT_TXQ0_DONE);
RT_SOFTC_LOCK(sc);
rt_intr_enable(sc, ~sc->intr_pending_mask &
(sc->intr_disable_mask & intr_mask));
if (sc->intr_pending_mask & intr_mask) {
RT_DPRINTF(sc, RT_DEBUG_TX,
"Tx done task: scheduling again\n");
taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
}
RT_SOFTC_UNLOCK(sc);
if (!IFQ_IS_EMPTY(&ifp->if_snd))
rt_start(ifp);
}
/*
* rt_periodic_task - run periodic task
*/
static void
rt_periodic_task(void *context, int pending)
{
struct rt_softc *sc;
struct ifnet *ifp;
sc = context;
ifp = sc->ifp;
RT_DPRINTF(sc, RT_DEBUG_PERIODIC, "periodic task: round=%lu\n",
sc->periodic_round);
if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
return;
RT_SOFTC_LOCK(sc);
sc->periodic_round++;
rt_update_stats(sc);
if ((sc->periodic_round % 10) == 0) {
rt_update_raw_counters(sc);
rt_watchdog(sc);
}
RT_SOFTC_UNLOCK(sc);
callout_reset(&sc->periodic_ch, hz / 10, rt_periodic, sc);
}
/*
* rt_rx_eof - check for frames that done by DMA engine and pass it into
* network subsystem.
*/
static int
rt_rx_eof(struct rt_softc *sc, int limit)
{
struct ifnet *ifp;
struct rt_softc_rx_ring *ring;
struct rt_rxdesc *desc;
struct rt_softc_rx_data *data;
struct mbuf *m, *mnew;
bus_dma_segment_t segs[1];
bus_dmamap_t dma_map;
uint32_t index, desc_flags;
int error, nsegs, len, nframes;
ifp = sc->ifp;
ring = &sc->rx_ring;
nframes = 0;
while (limit != 0) {
index = RT_READ(sc, PDMA_BASE + RX_DRX_IDX0);
if (ring->cur == index)
break;
desc = &ring->desc[ring->cur];
data = &ring->data[ring->cur];
bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
#ifdef IF_RT_DEBUG
if ( sc->debug & RT_DEBUG_RX ) {
printf("\nRX Descriptor[%#08x] dump:\n", (u_int)desc);
hexdump(desc, 16, 0, 0);
printf("-----------------------------------\n");
}
#endif
/* XXX Sometime device don`t set DDONE bit */
#ifdef DDONE_FIXED
if (!(desc->sdl0 & htole16(RT_RXDESC_SDL0_DDONE))) {
RT_DPRINTF(sc, RT_DEBUG_RX, "DDONE=0, try next\n");
break;
}
#endif
len = le16toh(desc->sdl0) & 0x3fff;
RT_DPRINTF(sc, RT_DEBUG_RX, "new frame len=%d\n", len);
nframes++;
mnew = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
MJUMPAGESIZE);
if (mnew == NULL) {
sc->rx_mbuf_alloc_errors++;
ifp->if_ierrors++;
goto skip;
}
mnew->m_len = mnew->m_pkthdr.len = MJUMPAGESIZE;
error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag,
ring->spare_dma_map, mnew, segs, &nsegs, BUS_DMA_NOWAIT);
if (error != 0) {
RT_DPRINTF(sc, RT_DEBUG_RX,
"could not load Rx mbuf DMA map: "
"error=%d, nsegs=%d\n",
error, nsegs);
m_freem(mnew);
sc->rx_mbuf_dmamap_errors++;
ifp->if_ierrors++;
goto skip;
}
KASSERT(nsegs == 1, ("%s: too many DMA segments",
device_get_nameunit(sc->dev)));
bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
dma_map = data->dma_map;
data->dma_map = ring->spare_dma_map;
ring->spare_dma_map = dma_map;
bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
BUS_DMASYNC_PREREAD);
m = data->m;
desc_flags = desc->src;
data->m = mnew;
/* Add 2 for proper align of RX IP header */
desc->sdp0 = htole32(segs[0].ds_addr+2);
desc->sdl0 = htole32(segs[0].ds_len-2);
desc->src = 0;
desc->ai = 0;
desc->foe = 0;
RT_DPRINTF(sc, RT_DEBUG_RX,
"Rx frame: rxdesc flags=0x%08x\n", desc_flags);
m->m_pkthdr.rcvif = ifp;
/* Add 2 to fix data align, after sdp0 = addr + 2 */
m->m_data += 2;
m->m_pkthdr.len = m->m_len = len;
/* check for crc errors */
if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
/*check for valid checksum*/
if (desc_flags & (RXDSXR_SRC_IP_CSUM_FAIL|
RXDSXR_SRC_L4_CSUM_FAIL)) {
RT_DPRINTF(sc, RT_DEBUG_RX,
"rxdesc: crc error\n");
ifp->if_ierrors++;
if (!(ifp->if_flags & IFF_PROMISC)) {
m_freem(m);
goto skip;
}
}
if ((desc_flags & RXDSXR_SRC_IP_CSUM_FAIL) != 0) {
m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
m->m_pkthdr.csum_data = 0xffff;
}
m->m_flags &= ~M_HASFCS;
}
(*ifp->if_input)(ifp, m);
skip:
desc->sdl0 &= ~htole16(RT_RXDESC_SDL0_DDONE);
bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
ring->cur = (ring->cur + 1) % RT_SOFTC_RX_RING_DATA_COUNT;
limit--;
}
if (ring->cur == 0)
RT_WRITE(sc, PDMA_BASE + RX_CALC_IDX0,
RT_SOFTC_RX_RING_DATA_COUNT - 1);
else
RT_WRITE(sc, PDMA_BASE + RX_CALC_IDX0,
ring->cur - 1);
RT_DPRINTF(sc, RT_DEBUG_RX, "Rx eof: nframes=%d\n", nframes);
sc->rx_packets += nframes;
return (limit == 0);
}
/*
* rt_tx_eof - check for successful transmitted frames and mark their
* descriptor as free.
*/
static void
rt_tx_eof(struct rt_softc *sc, struct rt_softc_tx_ring *ring)
{
struct ifnet *ifp;
struct rt_txdesc *desc;
struct rt_softc_tx_data *data;
uint32_t index;
int ndescs, nframes;
ifp = sc->ifp;
ndescs = 0;
nframes = 0;
for (;;) {
index = RT_READ(sc, PDMA_BASE + TX_DTX_IDX(ring->qid));
if (ring->desc_next == index)
break;
ndescs++;
desc = &ring->desc[ring->desc_next];
bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
if (desc->sdl0 & htole16(RT_TXDESC_SDL0_LASTSEG) ||
desc->sdl1 & htole16(RT_TXDESC_SDL1_LASTSEG)) {
nframes++;
data = &ring->data[ring->data_next];
bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
m_freem(data->m);
data->m = NULL;
ifp->if_opackets++;
RT_SOFTC_TX_RING_LOCK(ring);
ring->data_queued--;
ring->data_next = (ring->data_next + 1) %
RT_SOFTC_TX_RING_DATA_COUNT;
RT_SOFTC_TX_RING_UNLOCK(ring);
}
desc->sdl0 &= ~htole16(RT_TXDESC_SDL0_DDONE);
bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
RT_SOFTC_TX_RING_LOCK(ring);
ring->desc_queued--;
ring->desc_next = (ring->desc_next + 1) %
RT_SOFTC_TX_RING_DESC_COUNT;
RT_SOFTC_TX_RING_UNLOCK(ring);
}
RT_DPRINTF(sc, RT_DEBUG_TX,
"Tx eof: qid=%d, ndescs=%d, nframes=%d\n", ring->qid, ndescs,
nframes);
}
/*
* rt_update_stats - query statistics counters and update related variables.
*/
static void
rt_update_stats(struct rt_softc *sc)
{
struct ifnet *ifp;
ifp = sc->ifp;
RT_DPRINTF(sc, RT_DEBUG_STATS, "update statistic: \n");
/* XXX do update stats here */
}
/*
* rt_watchdog - reinit device on watchdog event.
*/
static void
rt_watchdog(struct rt_softc *sc)
{
uint32_t tmp;
#ifdef notyet
int ntries;
#endif
tmp = RT_READ(sc, PSE_BASE + CDMA_OQ_STA);
RT_DPRINTF(sc, RT_DEBUG_WATCHDOG, "watchdog: PSE_IQ_STA=0x%08x\n",
tmp);
/* XXX: do not reset */
#ifdef notyet
if (((tmp >> P0_IQ_PCNT_SHIFT) & 0xff) != 0) {
sc->tx_queue_not_empty[0]++;
for (ntries = 0; ntries < 10; ntries++) {
tmp = RT_READ(sc, PSE_BASE + PSE_IQ_STA);
if (((tmp >> P0_IQ_PCNT_SHIFT) & 0xff) == 0)
break;
DELAY(1);
}
}
if (((tmp >> P1_IQ_PCNT_SHIFT) & 0xff) != 0) {
sc->tx_queue_not_empty[1]++;
for (ntries = 0; ntries < 10; ntries++) {
tmp = RT_READ(sc, PSE_BASE + PSE_IQ_STA);
if (((tmp >> P1_IQ_PCNT_SHIFT) & 0xff) == 0)
break;
DELAY(1);
}
}
#endif
}
/*
* rt_update_raw_counters - update counters.
*/
static void
rt_update_raw_counters(struct rt_softc *sc)
{
sc->tx_bytes += RT_READ(sc, CNTR_BASE + GDMA_TX_GBCNT0);
sc->tx_packets += RT_READ(sc, CNTR_BASE + GDMA_TX_GPCNT0);
sc->tx_skip += RT_READ(sc, CNTR_BASE + GDMA_TX_SKIPCNT0);
sc->tx_collision+= RT_READ(sc, CNTR_BASE + GDMA_TX_COLCNT0);
sc->rx_bytes += RT_READ(sc, CNTR_BASE + GDMA_RX_GBCNT0);
sc->rx_packets += RT_READ(sc, CNTR_BASE + GDMA_RX_GPCNT0);
sc->rx_crc_err += RT_READ(sc, CNTR_BASE + GDMA_RX_CSUM_ERCNT0);
sc->rx_short_err+= RT_READ(sc, CNTR_BASE + GDMA_RX_SHORT_ERCNT0);
sc->rx_long_err += RT_READ(sc, CNTR_BASE + GDMA_RX_LONG_ERCNT0);
sc->rx_phy_err += RT_READ(sc, CNTR_BASE + GDMA_RX_FERCNT0);
sc->rx_fifo_overflows+= RT_READ(sc, CNTR_BASE + GDMA_RX_OERCNT0);
}
static void
rt_intr_enable(struct rt_softc *sc, uint32_t intr_mask)
{
uint32_t tmp;
sc->intr_disable_mask &= ~intr_mask;
tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
RT_WRITE(sc, GE_PORT_BASE + FE_INT_ENABLE, tmp);
}
static void
rt_intr_disable(struct rt_softc *sc, uint32_t intr_mask)
{
uint32_t tmp;
sc->intr_disable_mask |= intr_mask;
tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
RT_WRITE(sc, GE_PORT_BASE + FE_INT_ENABLE, tmp);
}
/*
* rt_txrx_enable - enable TX/RX DMA
*/
static int
rt_txrx_enable(struct rt_softc *sc)
{
struct ifnet *ifp;
uint32_t tmp;
int ntries;
ifp = sc->ifp;
/* enable Tx/Rx DMA engine */
for (ntries = 0; ntries < 200; ntries++) {
tmp = RT_READ(sc, PDMA_BASE + PDMA_GLO_CFG);
if (!(tmp & (FE_TX_DMA_BUSY | FE_RX_DMA_BUSY)))
break;
DELAY(1000);
}
if (ntries == 200) {
device_printf(sc->dev, "timeout waiting for DMA engine\n");
return (-1);
}
DELAY(50);
tmp |= FE_TX_WB_DDONE | FE_RX_DMA_EN | FE_TX_DMA_EN;
RT_WRITE(sc, PDMA_BASE + PDMA_GLO_CFG, tmp);
/* XXX set Rx filter */
return (0);
}
/*
* rt_alloc_rx_ring - allocate RX DMA ring buffer
*/
static int
rt_alloc_rx_ring(struct rt_softc *sc, struct rt_softc_rx_ring *ring)
{
struct rt_rxdesc *desc;
struct rt_softc_rx_data *data;
bus_dma_segment_t segs[1];
int i, nsegs, error;
error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
RT_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt_rxdesc), 1,
RT_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt_rxdesc),
0, NULL, NULL, &ring->desc_dma_tag);
if (error != 0) {
device_printf(sc->dev,
"could not create Rx desc DMA tag\n");
goto fail;
}
error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
if (error != 0) {
device_printf(sc->dev,
"could not allocate Rx desc DMA memory\n");
goto fail;
}
error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
ring->desc,
RT_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt_rxdesc),
rt_dma_map_addr, &ring->desc_phys_addr, 0);
if (error != 0) {
device_printf(sc->dev, "could not load Rx desc DMA map\n");
goto fail;
}
error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL,
&ring->data_dma_tag);
if (error != 0) {
device_printf(sc->dev,
"could not create Rx data DMA tag\n");
goto fail;
}
for (i = 0; i < RT_SOFTC_RX_RING_DATA_COUNT; i++) {
desc = &ring->desc[i];
data = &ring->data[i];
error = bus_dmamap_create(ring->data_dma_tag, 0,
&data->dma_map);
if (error != 0) {
device_printf(sc->dev, "could not create Rx data DMA "
"map\n");
goto fail;
}
data->m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
MJUMPAGESIZE);
if (data->m == NULL) {
device_printf(sc->dev, "could not allocate Rx mbuf\n");
error = ENOMEM;
goto fail;
}
data->m->m_len = data->m->m_pkthdr.len = MJUMPAGESIZE;
error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag,
data->dma_map, data->m, segs, &nsegs, BUS_DMA_NOWAIT);
if (error != 0) {
device_printf(sc->dev,
"could not load Rx mbuf DMA map\n");
goto fail;
}
KASSERT(nsegs == 1, ("%s: too many DMA segments",
device_get_nameunit(sc->dev)));
/* Add 2 for proper align of RX IP header */
desc->sdp0 = htole32(segs[0].ds_addr+2);
desc->sdl0 = htole32(segs[0].ds_len-2);
}
error = bus_dmamap_create(ring->data_dma_tag, 0,
&ring->spare_dma_map);
if (error != 0) {
device_printf(sc->dev,
"could not create Rx spare DMA map\n");
goto fail;
}
bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
return (0);
fail:
rt_free_rx_ring(sc, ring);
return (error);
}
/*
* rt_reset_rx_ring - reset RX ring buffer
*/
static void
rt_reset_rx_ring(struct rt_softc *sc, struct rt_softc_rx_ring *ring)
{
struct rt_rxdesc *desc;
int i;
for (i = 0; i < RT_SOFTC_RX_RING_DATA_COUNT; i++) {
desc = &ring->desc[i];
desc->sdl0 &= ~htole16(RT_RXDESC_SDL0_DDONE);
}
bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
ring->cur = 0;
}
/*
* rt_free_rx_ring - free memory used by RX ring buffer
*/
static void
rt_free_rx_ring(struct rt_softc *sc, struct rt_softc_rx_ring *ring)
{
struct rt_softc_rx_data *data;
int i;
if (ring->desc != NULL) {
bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
bus_dmamem_free(ring->desc_dma_tag, ring->desc,
ring->desc_dma_map);
}
if (ring->desc_dma_tag != NULL)
bus_dma_tag_destroy(ring->desc_dma_tag);
for (i = 0; i < RT_SOFTC_RX_RING_DATA_COUNT; i++) {
data = &ring->data[i];
if (data->m != NULL) {
bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
m_freem(data->m);
}
if (data->dma_map != NULL)
bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
}
if (ring->spare_dma_map != NULL)
bus_dmamap_destroy(ring->data_dma_tag, ring->spare_dma_map);
if (ring->data_dma_tag != NULL)
bus_dma_tag_destroy(ring->data_dma_tag);
}
/*
* rt_alloc_tx_ring - allocate TX ring buffer
*/
static int
rt_alloc_tx_ring(struct rt_softc *sc, struct rt_softc_tx_ring *ring, int qid)
{
struct rt_softc_tx_data *data;
int error, i;
mtx_init(&ring->lock, device_get_nameunit(sc->dev), NULL, MTX_DEF);
error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
RT_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt_txdesc), 1,
RT_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt_txdesc),
0, NULL, NULL, &ring->desc_dma_tag);
if (error != 0) {
device_printf(sc->dev,
"could not create Tx desc DMA tag\n");
goto fail;
}
error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
if (error != 0) {
device_printf(sc->dev,
"could not allocate Tx desc DMA memory\n");
goto fail;
}
error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
ring->desc, (RT_SOFTC_TX_RING_DESC_COUNT *
sizeof(struct rt_txdesc)), rt_dma_map_addr,
&ring->desc_phys_addr, 0);
if (error != 0) {
device_printf(sc->dev, "could not load Tx desc DMA map\n");
goto fail;
}
ring->desc_queued = 0;
ring->desc_cur = 0;
ring->desc_next = 0;
error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
RT_SOFTC_TX_RING_DATA_COUNT * RT_TX_DATA_SEG0_SIZE, 1,
RT_SOFTC_TX_RING_DATA_COUNT * RT_TX_DATA_SEG0_SIZE,
0, NULL, NULL, &ring->seg0_dma_tag);
if (error != 0) {
device_printf(sc->dev,
"could not create Tx seg0 DMA tag\n");
goto fail;
}
error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
if (error != 0) {
device_printf(sc->dev,
"could not allocate Tx seg0 DMA memory\n");
goto fail;
}
error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
ring->seg0,
RT_SOFTC_TX_RING_DATA_COUNT * RT_TX_DATA_SEG0_SIZE,
rt_dma_map_addr, &ring->seg0_phys_addr, 0);
if (error != 0) {
device_printf(sc->dev, "could not load Tx seg0 DMA map\n");
goto fail;
}
error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
MJUMPAGESIZE, RT_SOFTC_MAX_SCATTER, MJUMPAGESIZE, 0, NULL, NULL,
&ring->data_dma_tag);
if (error != 0) {
device_printf(sc->dev,
"could not create Tx data DMA tag\n");
goto fail;
}
for (i = 0; i < RT_SOFTC_TX_RING_DATA_COUNT; i++) {
data = &ring->data[i];
error = bus_dmamap_create(ring->data_dma_tag, 0,
&data->dma_map);
if (error != 0) {
device_printf(sc->dev, "could not create Tx data DMA "
"map\n");
goto fail;
}
}
ring->data_queued = 0;
ring->data_cur = 0;
ring->data_next = 0;
ring->qid = qid;
return (0);
fail:
rt_free_tx_ring(sc, ring);
return (error);
}
/*
* rt_reset_tx_ring - reset TX ring buffer to empty state
*/
static void
rt_reset_tx_ring(struct rt_softc *sc, struct rt_softc_tx_ring *ring)
{
struct rt_softc_tx_data *data;
struct rt_txdesc *desc;
int i;
for (i = 0; i < RT_SOFTC_TX_RING_DESC_COUNT; i++) {
desc = &ring->desc[i];
desc->sdl0 = 0;
desc->sdl1 = 0;
}
ring->desc_queued = 0;
ring->desc_cur = 0;
ring->desc_next = 0;
bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
BUS_DMASYNC_PREWRITE);
bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
BUS_DMASYNC_PREWRITE);
for (i = 0; i < RT_SOFTC_TX_RING_DATA_COUNT; i++) {
data = &ring->data[i];
if (data->m != NULL) {
bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
m_freem(data->m);
data->m = NULL;
}
}
ring->data_queued = 0;
ring->data_cur = 0;
ring->data_next = 0;
}
/*
* rt_free_tx_ring - free RX ring buffer
*/
static void
rt_free_tx_ring(struct rt_softc *sc, struct rt_softc_tx_ring *ring)
{
struct rt_softc_tx_data *data;
int i;
if (ring->desc != NULL) {
bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
bus_dmamem_free(ring->desc_dma_tag, ring->desc,
ring->desc_dma_map);
}
if (ring->desc_dma_tag != NULL)
bus_dma_tag_destroy(ring->desc_dma_tag);
if (ring->seg0 != NULL) {
bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
ring->seg0_dma_map);
}
if (ring->seg0_dma_tag != NULL)
bus_dma_tag_destroy(ring->seg0_dma_tag);
for (i = 0; i < RT_SOFTC_TX_RING_DATA_COUNT; i++) {
data = &ring->data[i];
if (data->m != NULL) {
bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
m_freem(data->m);
}
if (data->dma_map != NULL)
bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
}
if (ring->data_dma_tag != NULL)
bus_dma_tag_destroy(ring->data_dma_tag);
mtx_destroy(&ring->lock);
}
/*
* rt_dma_map_addr - get address of busdma segment
*/
static void
rt_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
{
if (error != 0)
return;
KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
*(bus_addr_t *) arg = segs[0].ds_addr;
}
/*
* rt_sysctl_attach - attach sysctl nodes for NIC counters.
*/
static void
rt_sysctl_attach(struct rt_softc *sc)
{
struct sysctl_ctx_list *ctx;
struct sysctl_oid *tree;
struct sysctl_oid *stats;
ctx = device_get_sysctl_ctx(sc->dev);
tree = device_get_sysctl_tree(sc->dev);
/* statistic counters */
stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
"stats", CTLFLAG_RD, 0, "statistic");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"interrupts", CTLFLAG_RD, &sc->interrupts, 0,
"all interrupts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts,
0, "Tx coherent interrupts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts,
0, "Rx coherent interrupts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
"Rx interrupts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
"Rx delay interrupts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
"Tx AC3 interrupts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
"Tx AC2 interrupts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
"Tx AC1 interrupts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
"Tx AC0 interrupts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts,
0, "Tx delay interrupts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued,
0, "Tx AC3 descriptors queued");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued,
0, "Tx AC3 data queued");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued,
0, "Tx AC2 descriptors queued");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued,
0, "Tx AC2 data queued");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued,
0, "Tx AC1 descriptors queued");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued,
0, "Tx AC1 data queued");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued,
0, "Tx AC0 descriptors queued");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued,
0, "Tx AC0 data queued");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ3_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[3],
0, "Tx AC3 data queue full");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ2_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[2],
0, "Tx AC2 data queue full");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ1_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[1],
0, "Tx AC1 data queue full");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"TXQ0_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[0],
0, "Tx AC0 data queue full");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts,
0, "Tx watchdog timeouts");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
"Tx defragmented packets");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
"no Tx descriptors available");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors,
0, "Rx mbuf allocation errors");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_mbuf_dmamap_errors", CTLFLAG_RD, &sc->rx_mbuf_dmamap_errors,
0, "Rx mbuf DMA mapping errors");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0],
0, "Tx queue 0 not empty");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1],
0, "Tx queue 1 not empty");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
"Rx packets");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_err, 0,
"Rx CRC errors");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_err, 0,
"Rx PHY errors");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
"Rx duplicate packets");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
"Rx FIFO overflows");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_bytes", CTLFLAG_RD, &sc->rx_bytes, 0,
"Rx bytes");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_long_err", CTLFLAG_RD, &sc->rx_long_err, 0,
"Rx too long frame errors");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"rx_short_err", CTLFLAG_RD, &sc->rx_short_err, 0,
"Rx too short frame errors");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"tx_bytes", CTLFLAG_RD, &sc->tx_bytes, 0,
"Tx bytes");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"tx_packets", CTLFLAG_RD, &sc->tx_packets, 0,
"Tx packets");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"tx_skip", CTLFLAG_RD, &sc->tx_skip, 0,
"Tx skip count for GDMA ports");
SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
"tx_collision", CTLFLAG_RD, &sc->tx_collision, 0,
"Tx collision count for GDMA ports");
}
#ifdef IF_RT_PHY_SUPPORT
static int
rt_miibus_readreg(device_t dev, int phy, int reg)
{
struct rt_softc *sc = device_get_softc(dev);
/*
* PSEUDO_PHYAD is a special value for indicate switch attached.
* No one PHY use PSEUDO_PHYAD (0x1e) address.
*/
if (phy == 31) {
/* Fake PHY ID for bfeswitch attach */
switch (reg) {
case MII_BMSR:
return (BMSR_EXTSTAT|BMSR_MEDIAMASK);
case MII_PHYIDR1:
return (0x40); /* As result of faking */
case MII_PHYIDR2: /* PHY will detect as */
return (0x6250); /* bfeswitch */
}
}
/* Wait prev command done if any */
while (RT_READ(sc, MDIO_ACCESS) & MDIO_CMD_ONGO);
RT_WRITE(sc, MDIO_ACCESS,
MDIO_CMD_ONGO ||
((phy << MDIO_PHY_ADDR_SHIFT) & MDIO_PHY_ADDR_MASK) ||
((reg << MDIO_PHYREG_ADDR_SHIFT) & MDIO_PHYREG_ADDR_MASK));
while (RT_READ(sc, MDIO_ACCESS) & MDIO_CMD_ONGO);
return (RT_READ(sc, MDIO_ACCESS) & MDIO_PHY_DATA_MASK);
}
static int
rt_miibus_writereg(device_t dev, int phy, int reg, int val)
{
struct rt_softc *sc = device_get_softc(dev);
/* Wait prev command done if any */
while (RT_READ(sc, MDIO_ACCESS) & MDIO_CMD_ONGO);
RT_WRITE(sc, MDIO_ACCESS,
MDIO_CMD_ONGO || MDIO_CMD_WR ||
((phy << MDIO_PHY_ADDR_SHIFT) & MDIO_PHY_ADDR_MASK) ||
((reg << MDIO_PHYREG_ADDR_SHIFT) & MDIO_PHYREG_ADDR_MASK) ||
(val & MDIO_PHY_DATA_MASK));
while (RT_READ(sc, MDIO_ACCESS) & MDIO_CMD_ONGO);
return (0);
}
void
rt_miibus_statchg(device_t dev)
{
struct rt_softc *sc = device_get_softc(dev);
struct mii_data *mii;
mii = device_get_softc(sc->rt_miibus);
if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
(IFM_ACTIVE | IFM_AVALID)) {
switch (IFM_SUBTYPE(mii->mii_media_active)) {
case IFM_10_T:
case IFM_100_TX:
/* XXX check link here */
sc->flags |= 1;
break;
default:
break;
}
}
}
#endif /* IF_RT_PHY_SUPPORT */
static device_method_t rt_dev_methods[] =
{
DEVMETHOD(device_probe, rt_probe),
DEVMETHOD(device_attach, rt_attach),
DEVMETHOD(device_detach, rt_detach),
DEVMETHOD(device_shutdown, rt_shutdown),
DEVMETHOD(device_suspend, rt_suspend),
DEVMETHOD(device_resume, rt_resume),
#ifdef IF_RT_PHY_SUPPORT
/* MII interface */
DEVMETHOD(miibus_readreg, rt_miibus_readreg),
DEVMETHOD(miibus_writereg, rt_miibus_writereg),
DEVMETHOD(miibus_statchg, rt_miibus_statchg),
#endif
DEVMETHOD_END
};
static driver_t rt_driver =
{
"rt",
rt_dev_methods,
sizeof(struct rt_softc)
};
static devclass_t rt_dev_class;
DRIVER_MODULE(rt, nexus, rt_driver, rt_dev_class, 0, 0);
MODULE_DEPEND(rt, ether, 1, 1, 1);
MODULE_DEPEND(rt, miibus, 1, 1, 1);
|