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
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
|
/* $NetBSD: if_sip.c,v 1.190 2023/06/02 08:51:47 andvar Exp $ */
/*-
* Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* 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.
*/
/*-
* Copyright (c) 1999 Network Computer, Inc.
* 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, 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. Neither the name of Network Computer, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY NETWORK COMPUTER, 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 Silicon Integrated Systems SiS 900,
* SiS 7016 10/100, National Semiconductor DP83815 10/100, and
* National Semiconductor DP83820 10/100/1000 PCI Ethernet
* controllers.
*
* Originally written to support the SiS 900 by Jason R. Thorpe for
* Network Computer, Inc.
*
* TODO:
*
* - Reduce the Rx interrupt load.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_sip.c,v 1.190 2023/06/02 08:51:47 andvar Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/queue.h>
#include <sys/rndsource.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 <sys/bus.h>
#include <sys/intr.h>
#include <machine/endian.h>
#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#include <dev/mii/mii_bitbang.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/if_sipreg.h>
/*
* Transmit descriptor list size. This is arbitrary, but allocate
* enough descriptors for 128 pending transmissions, and 8 segments
* per packet (64 for DP83820 for jumbo frames).
*
* This MUST work out to a power of 2.
*/
#define GSIP_NTXSEGS_ALLOC 16
#define SIP_NTXSEGS_ALLOC 8
#define SIP_TXQUEUELEN 256
#define MAX_SIP_NTXDESC \
(SIP_TXQUEUELEN * MAX(SIP_NTXSEGS_ALLOC, GSIP_NTXSEGS_ALLOC))
/*
* Receive descriptor list size. We have one Rx buffer per incoming
* packet, so this logic is a little simpler.
*
* Actually, on the DP83820, we allow the packet to consume more than
* one buffer, in order to support jumbo Ethernet frames. In that
* case, a packet may consume up to 5 buffers (assuming a 2048 byte
* mbuf cluster). 256 receive buffers is only 51 maximum size packets,
* so we'd better be quick about handling receive interrupts.
*/
#define GSIP_NRXDESC 256
#define SIP_NRXDESC 128
#define MAX_SIP_NRXDESC MAX(GSIP_NRXDESC, SIP_NRXDESC)
/*
* Set this to 1 to force-disable using the 64-bit data path
* on DP83820.
*/
static int gsip_disable_data64 = 0;
/*
* Control structures are DMA'd to the SiS900 chip. We allocate them in
* a single clump that maps to a single DMA segment to make several things
* easier.
*/
struct sip_control_data {
/*
* The transmit descriptors.
*/
struct sip_desc scd_txdescs[MAX_SIP_NTXDESC];
/*
* The receive descriptors.
*/
struct sip_desc scd_rxdescs[MAX_SIP_NRXDESC];
};
#define SIP_CDOFF(x) offsetof(struct sip_control_data, x)
#define SIP_CDTXOFF(x) SIP_CDOFF(scd_txdescs[(x)])
#define SIP_CDRXOFF(x) SIP_CDOFF(scd_rxdescs[(x)])
/*
* Software state for transmit jobs.
*/
struct sip_txsoft {
struct mbuf *txs_mbuf; /* head of our mbuf chain */
bus_dmamap_t txs_dmamap; /* our DMA map */
int txs_firstdesc; /* first descriptor in packet */
int txs_lastdesc; /* last descriptor in packet */
SIMPLEQ_ENTRY(sip_txsoft) txs_q;
};
SIMPLEQ_HEAD(sip_txsq, sip_txsoft);
/*
* Software state for receive jobs.
*/
struct sip_rxsoft {
struct mbuf *rxs_mbuf; /* head of our mbuf chain */
bus_dmamap_t rxs_dmamap; /* our DMA map */
};
enum sip_attach_stage {
SIP_ATTACH_FIN = 0
, SIP_ATTACH_CREATE_RXMAP
, SIP_ATTACH_CREATE_TXMAP
, SIP_ATTACH_LOAD_MAP
, SIP_ATTACH_CREATE_MAP
, SIP_ATTACH_MAP_MEM
, SIP_ATTACH_ALLOC_MEM
, SIP_ATTACH_INTR
, SIP_ATTACH_MAP
};
/*
* Software state per device.
*/
struct sip_softc {
device_t sc_dev; /* generic device information */
device_suspensor_t sc_suspensor;
pmf_qual_t sc_qual;
bus_space_tag_t sc_st; /* bus space tag */
bus_space_handle_t sc_sh; /* bus space handle */
bus_size_t sc_sz; /* bus space size */
bus_dma_tag_t sc_dmat; /* bus DMA tag */
pci_chipset_tag_t sc_pc;
bus_dma_segment_t sc_seg;
struct ethercom sc_ethercom; /* ethernet common data */
const struct sip_product *sc_model; /* which model are we? */
bool sc_gigabit; /* 1: 83820, 0: other */
bool sc_dma64; /* using 64-bit DMA addresses */
int sc_rev; /* chip revision */
unsigned int sc_bufptr_idx;
unsigned int sc_cmdsts_idx;
unsigned int sc_extsts_idx; /* DP83820 only */
void *sc_ih; /* interrupt cookie */
struct mii_data sc_mii; /* MII/media information */
callout_t sc_tick_ch; /* tick callout */
bus_dmamap_t sc_cddmamap; /* control data DMA map */
#define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
/*
* Software state for transmit and receive descriptors.
*/
struct sip_txsoft sc_txsoft[SIP_TXQUEUELEN];
struct sip_rxsoft sc_rxsoft[MAX_SIP_NRXDESC];
/*
* Control data structures.
*/
struct sip_control_data *sc_control_data;
#define sc_txdescs sc_control_data->scd_txdescs
#define sc_rxdescs sc_control_data->scd_rxdescs
#ifdef SIP_EVENT_COUNTERS
/*
* Event counters.
*/
struct evcnt sc_ev_txdstall; /* Tx stalled due to no txd */
struct evcnt sc_ev_txforceintr; /* Tx interrupts forced */
struct evcnt sc_ev_txdintr; /* Tx descriptor interrupts */
struct evcnt sc_ev_txiintr; /* Tx idle interrupts */
struct evcnt sc_ev_rxintr; /* Rx interrupts */
struct evcnt sc_ev_hiberr; /* HIBERR interrupts */
struct evcnt sc_ev_rxpause; /* PAUSE received */
/* DP83820 only */
struct evcnt sc_ev_txpause; /* PAUSE transmitted */
struct evcnt sc_ev_rxipsum; /* IP checksums checked in-bound */
struct evcnt sc_ev_rxtcpsum; /* TCP checksums checked in-bound */
struct evcnt sc_ev_rxudpsum; /* UDP checksums checked in-bound */
struct evcnt sc_ev_txipsum; /* IP checksums comp. out-bound */
struct evcnt sc_ev_txtcpsum; /* TCP checksums comp. out-bound */
struct evcnt sc_ev_txudpsum; /* UDP checksums comp. out-bound */
#endif /* SIP_EVENT_COUNTERS */
uint32_t sc_txcfg; /* prototype TXCFG register */
uint32_t sc_rxcfg; /* prototype RXCFG register */
uint32_t sc_imr; /* prototype IMR register */
uint32_t sc_rfcr; /* prototype RFCR register */
uint32_t sc_cfg; /* prototype CFG register */
uint32_t sc_gpior; /* prototype GPIOR register */
uint32_t sc_tx_fill_thresh; /* transmit fill threshold */
uint32_t sc_tx_drain_thresh; /* transmit drain threshold */
uint32_t sc_rx_drain_thresh; /* receive drain threshold */
int sc_flowflags; /* 802.3x flow control flags */
int sc_rx_flow_thresh; /* Rx FIFO threshold for flow control */
int sc_paused; /* paused indication */
int sc_txfree; /* number of free Tx descriptors */
int sc_txnext; /* next ready Tx descriptor */
int sc_txwin; /* Tx descriptors since last intr */
struct sip_txsq sc_txfreeq; /* free Tx descsofts */
struct sip_txsq sc_txdirtyq; /* dirty Tx descsofts */
/* values of interface state at last init */
struct {
/* if_capenable */
uint64_t if_capenable;
/* ec_capenable */
int ec_capenable;
/* VLAN_ATTACHED */
int is_vlan;
} sc_prev;
u_short sc_if_flags;
int sc_rxptr; /* next ready Rx descriptor/descsoft */
int sc_rxdiscard;
int sc_rxlen;
struct mbuf *sc_rxhead;
struct mbuf *sc_rxtail;
struct mbuf **sc_rxtailp;
int sc_ntxdesc;
int sc_ntxdesc_mask;
int sc_nrxdesc_mask;
const struct sip_parm {
const struct sip_regs {
int r_rxcfg;
int r_txcfg;
} p_regs;
const struct sip_bits {
uint32_t b_txcfg_mxdma_8;
uint32_t b_txcfg_mxdma_16;
uint32_t b_txcfg_mxdma_32;
uint32_t b_txcfg_mxdma_64;
uint32_t b_txcfg_mxdma_128;
uint32_t b_txcfg_mxdma_256;
uint32_t b_txcfg_mxdma_512;
uint32_t b_txcfg_flth_mask;
uint32_t b_txcfg_drth_mask;
uint32_t b_rxcfg_mxdma_8;
uint32_t b_rxcfg_mxdma_16;
uint32_t b_rxcfg_mxdma_32;
uint32_t b_rxcfg_mxdma_64;
uint32_t b_rxcfg_mxdma_128;
uint32_t b_rxcfg_mxdma_256;
uint32_t b_rxcfg_mxdma_512;
uint32_t b_isr_txrcmp;
uint32_t b_isr_rxrcmp;
uint32_t b_isr_dperr;
uint32_t b_isr_sserr;
uint32_t b_isr_rmabt;
uint32_t b_isr_rtabt;
uint32_t b_cmdsts_size_mask;
} p_bits;
int p_filtmem;
int p_rxbuf_len;
bus_size_t p_tx_dmamap_size;
int p_ntxsegs;
int p_ntxsegs_alloc;
int p_nrxdesc;
} *sc_parm;
void (*sc_rxintr)(struct sip_softc *);
krndsource_t rnd_source; /* random source */
};
#define sc_bits sc_parm->p_bits
#define sc_regs sc_parm->p_regs
static const struct sip_parm sip_parm = {
.p_filtmem = OTHER_RFCR_NS_RFADDR_FILTMEM
, .p_rxbuf_len = MCLBYTES - 1 /* field width */
, .p_tx_dmamap_size = MCLBYTES
, .p_ntxsegs = 16
, .p_ntxsegs_alloc = SIP_NTXSEGS_ALLOC
, .p_nrxdesc = SIP_NRXDESC
, .p_bits = {
.b_txcfg_mxdma_8 = 0x00200000 /* 8 bytes */
, .b_txcfg_mxdma_16 = 0x00300000 /* 16 bytes */
, .b_txcfg_mxdma_32 = 0x00400000 /* 32 bytes */
, .b_txcfg_mxdma_64 = 0x00500000 /* 64 bytes */
, .b_txcfg_mxdma_128 = 0x00600000 /* 128 bytes */
, .b_txcfg_mxdma_256 = 0x00700000 /* 256 bytes */
, .b_txcfg_mxdma_512 = 0x00000000 /* 512 bytes */
, .b_txcfg_flth_mask = 0x00003f00 /* Tx fill threshold */
, .b_txcfg_drth_mask = 0x0000003f /* Tx drain threshold */
, .b_rxcfg_mxdma_8 = 0x00200000 /* 8 bytes */
, .b_rxcfg_mxdma_16 = 0x00300000 /* 16 bytes */
, .b_rxcfg_mxdma_32 = 0x00400000 /* 32 bytes */
, .b_rxcfg_mxdma_64 = 0x00500000 /* 64 bytes */
, .b_rxcfg_mxdma_128 = 0x00600000 /* 128 bytes */
, .b_rxcfg_mxdma_256 = 0x00700000 /* 256 bytes */
, .b_rxcfg_mxdma_512 = 0x00000000 /* 512 bytes */
, .b_isr_txrcmp = 0x02000000 /* transmit reset complete */
, .b_isr_rxrcmp = 0x01000000 /* receive reset complete */
, .b_isr_dperr = 0x00800000 /* detected parity error */
, .b_isr_sserr = 0x00400000 /* signalled system error */
, .b_isr_rmabt = 0x00200000 /* received master abort */
, .b_isr_rtabt = 0x00100000 /* received target abort */
, .b_cmdsts_size_mask = OTHER_CMDSTS_SIZE_MASK
}
, .p_regs = {
.r_rxcfg = OTHER_SIP_RXCFG,
.r_txcfg = OTHER_SIP_TXCFG
}
}, gsip_parm = {
.p_filtmem = DP83820_RFCR_NS_RFADDR_FILTMEM
, .p_rxbuf_len = MCLBYTES - 8
, .p_tx_dmamap_size = ETHER_MAX_LEN_JUMBO
, .p_ntxsegs = 64
, .p_ntxsegs_alloc = GSIP_NTXSEGS_ALLOC
, .p_nrxdesc = GSIP_NRXDESC
, .p_bits = {
.b_txcfg_mxdma_8 = 0x00100000 /* 8 bytes */
, .b_txcfg_mxdma_16 = 0x00200000 /* 16 bytes */
, .b_txcfg_mxdma_32 = 0x00300000 /* 32 bytes */
, .b_txcfg_mxdma_64 = 0x00400000 /* 64 bytes */
, .b_txcfg_mxdma_128 = 0x00500000 /* 128 bytes */
, .b_txcfg_mxdma_256 = 0x00600000 /* 256 bytes */
, .b_txcfg_mxdma_512 = 0x00700000 /* 512 bytes */
, .b_txcfg_flth_mask = 0x0000ff00 /* Fx fill threshold */
, .b_txcfg_drth_mask = 0x000000ff /* Tx drain threshold */
, .b_rxcfg_mxdma_8 = 0x00100000 /* 8 bytes */
, .b_rxcfg_mxdma_16 = 0x00200000 /* 16 bytes */
, .b_rxcfg_mxdma_32 = 0x00300000 /* 32 bytes */
, .b_rxcfg_mxdma_64 = 0x00400000 /* 64 bytes */
, .b_rxcfg_mxdma_128 = 0x00500000 /* 128 bytes */
, .b_rxcfg_mxdma_256 = 0x00600000 /* 256 bytes */
, .b_rxcfg_mxdma_512 = 0x00700000 /* 512 bytes */
, .b_isr_txrcmp = 0x00400000 /* transmit reset complete */
, .b_isr_rxrcmp = 0x00200000 /* receive reset complete */
, .b_isr_dperr = 0x00100000 /* detected parity error */
, .b_isr_sserr = 0x00080000 /* signalled system error */
, .b_isr_rmabt = 0x00040000 /* received master abort */
, .b_isr_rtabt = 0x00020000 /* received target abort */
, .b_cmdsts_size_mask = DP83820_CMDSTS_SIZE_MASK
}
, .p_regs = {
.r_rxcfg = DP83820_SIP_RXCFG,
.r_txcfg = DP83820_SIP_TXCFG
}
};
static inline int
sip_nexttx(const struct sip_softc *sc, int x)
{
return (x + 1) & sc->sc_ntxdesc_mask;
}
static inline int
sip_nextrx(const struct sip_softc *sc, int x)
{
return (x + 1) & sc->sc_nrxdesc_mask;
}
/* 83820 only */
static inline void
sip_rxchain_reset(struct sip_softc *sc)
{
sc->sc_rxtailp = &sc->sc_rxhead;
*sc->sc_rxtailp = NULL;
sc->sc_rxlen = 0;
}
/* 83820 only */
static inline void
sip_rxchain_link(struct sip_softc *sc, struct mbuf *m)
{
*sc->sc_rxtailp = sc->sc_rxtail = m;
sc->sc_rxtailp = &m->m_next;
}
#ifdef SIP_EVENT_COUNTERS
#define SIP_EVCNT_INCR(ev) (ev)->ev_count++
#else
#define SIP_EVCNT_INCR(ev) /* nothing */
#endif
#define SIP_CDTXADDR(sc, x) ((sc)->sc_cddma + SIP_CDTXOFF((x)))
#define SIP_CDRXADDR(sc, x) ((sc)->sc_cddma + SIP_CDRXOFF((x)))
static inline void
sip_set_rxdp(struct sip_softc *sc, bus_addr_t addr)
{
if (sc->sc_gigabit)
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXDP_HI,
BUS_ADDR_HI32(addr));
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXDP, BUS_ADDR_LO32(addr));
}
static inline void
sip_set_txdp(struct sip_softc *sc, bus_addr_t addr)
{
if (sc->sc_gigabit)
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXDP_HI,
BUS_ADDR_HI32(addr));
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXDP, BUS_ADDR_LO32(addr));
}
static inline void
sip_cdtxsync(struct sip_softc *sc, const int x0, const int n0, const int ops)
{
int x, n;
x = x0;
n = n0;
/* If it will wrap around, sync to the end of the ring. */
if (x + n > sc->sc_ntxdesc) {
bus_dmamap_sync(sc->sc_dmat, sc->sc_cddmamap,
SIP_CDTXOFF(x), sizeof(struct sip_desc) *
(sc->sc_ntxdesc - x), ops);
n -= (sc->sc_ntxdesc - x);
x = 0;
}
/* Now sync whatever is left. */
bus_dmamap_sync(sc->sc_dmat, sc->sc_cddmamap,
SIP_CDTXOFF(x), sizeof(struct sip_desc) * n, ops);
}
static inline void
sip_cdrxsync(struct sip_softc *sc, int x, int ops)
{
bus_dmamap_sync(sc->sc_dmat, sc->sc_cddmamap,
SIP_CDRXOFF(x), sizeof(struct sip_desc), ops);
}
static void
sip_init_txring(struct sip_softc *sc)
{
struct sip_desc *sipd;
bus_addr_t next_desc;
int i;
memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
for (i = 0; i < sc->sc_ntxdesc; i++) {
sipd = &sc->sc_txdescs[i];
next_desc = SIP_CDTXADDR(sc, sip_nexttx(sc, i));
if (sc->sc_dma64) {
sipd->sipd_words[GSIP64_DESC_LINK_LO] =
htole32(BUS_ADDR_LO32(next_desc));
sipd->sipd_words[GSIP64_DESC_LINK_HI] =
htole32(BUS_ADDR_HI32(next_desc));
} else {
/* SIP_DESC_LINK == GSIP_DESC_LINK */
sipd->sipd_words[SIP_DESC_LINK] = htole32(next_desc);
}
}
sip_cdtxsync(sc, 0, sc->sc_ntxdesc,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
sc->sc_txfree = sc->sc_ntxdesc;
sc->sc_txnext = 0;
sc->sc_txwin = 0;
}
static inline void
sip_init_txdesc(struct sip_softc *sc, int x, bus_addr_t bufptr, uint32_t cmdsts)
{
struct sip_desc *sipd = &sc->sc_txdescs[x];
if (sc->sc_dma64) {
sipd->sipd_words[GSIP64_DESC_BUFPTR_LO] =
htole32(BUS_ADDR_LO32(bufptr));
sipd->sipd_words[GSIP64_DESC_BUFPTR_HI] =
htole32(BUS_ADDR_HI32(bufptr));
} else {
sipd->sipd_words[sc->sc_bufptr_idx] = htole32(bufptr);
}
sipd->sipd_words[sc->sc_extsts_idx] = 0;
sipd->sipd_words[sc->sc_cmdsts_idx] = htole32(cmdsts);
/* sip_cdtxsync() will be done later. */
}
static inline void
sip_init_rxdesc(struct sip_softc *sc, int x)
{
struct sip_rxsoft *rxs = &sc->sc_rxsoft[x];
struct sip_desc *sipd = &sc->sc_rxdescs[x];
const bus_addr_t next_desc = SIP_CDRXADDR(sc, sip_nextrx(sc, x));
if (sc->sc_dma64) {
sipd->sipd_words[GSIP64_DESC_LINK_LO] =
htole32(BUS_ADDR_LO32(next_desc));
sipd->sipd_words[GSIP64_DESC_LINK_HI] =
htole32(BUS_ADDR_HI32(next_desc));
sipd->sipd_words[GSIP64_DESC_BUFPTR_LO] =
htole32(BUS_ADDR_LO32(rxs->rxs_dmamap->dm_segs[0].ds_addr));
sipd->sipd_words[GSIP64_DESC_BUFPTR_HI] =
htole32(BUS_ADDR_HI32(rxs->rxs_dmamap->dm_segs[0].ds_addr));
} else {
sipd->sipd_words[SIP_DESC_LINK] = htole32(next_desc);
sipd->sipd_words[sc->sc_bufptr_idx] =
htole32(rxs->rxs_dmamap->dm_segs[0].ds_addr);
}
sipd->sipd_words[sc->sc_extsts_idx] = 0;
sip_cdrxsync(sc, x, BUS_DMASYNC_PREWRITE);
sipd->sipd_words[sc->sc_cmdsts_idx] =
htole32(CMDSTS_INTR | (sc->sc_parm->p_rxbuf_len &
sc->sc_bits.b_cmdsts_size_mask));
sip_cdrxsync(sc, x, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
}
#define SIP_CHIP_VERS(sc, v, p, r) \
((sc)->sc_model->sip_vendor == (v) && \
(sc)->sc_model->sip_product == (p) && \
(sc)->sc_rev == (r))
#define SIP_CHIP_MODEL(sc, v, p) \
((sc)->sc_model->sip_vendor == (v) && \
(sc)->sc_model->sip_product == (p))
#define SIP_SIS900_REV(sc, rev) \
SIP_CHIP_VERS((sc), PCI_VENDOR_SIS, PCI_PRODUCT_SIS_900, (rev))
#define SIP_TIMEOUT 1000
static int sip_ifflags_cb(struct ethercom *);
static void sipcom_start(struct ifnet *);
static void sipcom_watchdog(struct ifnet *);
static int sipcom_ioctl(struct ifnet *, u_long, void *);
static int sipcom_init(struct ifnet *);
static void sipcom_stop(struct ifnet *, int);
static bool sipcom_reset(struct sip_softc *);
static void sipcom_rxdrain(struct sip_softc *);
static int sipcom_add_rxbuf(struct sip_softc *, int);
static void sipcom_read_eeprom(struct sip_softc *, int, int,
uint16_t *);
static void sipcom_tick(void *);
static void sipcom_sis900_set_filter(struct sip_softc *);
static void sipcom_dp83815_set_filter(struct sip_softc *);
static void sipcom_dp83820_read_macaddr(struct sip_softc *,
const struct pci_attach_args *, uint8_t *);
static void sipcom_sis900_eeprom_delay(struct sip_softc *sc);
static void sipcom_sis900_read_macaddr(struct sip_softc *,
const struct pci_attach_args *, uint8_t *);
static void sipcom_dp83815_read_macaddr(struct sip_softc *,
const struct pci_attach_args *, uint8_t *);
static int sipcom_intr(void *);
static void sipcom_txintr(struct sip_softc *);
static void sip_rxintr(struct sip_softc *);
static void gsip_rxintr(struct sip_softc *);
static int sipcom_dp83820_mii_readreg(device_t, int, int, uint16_t *);
static int sipcom_dp83820_mii_writereg(device_t, int, int, uint16_t);
static void sipcom_dp83820_mii_statchg(struct ifnet *);
static int sipcom_sis900_mii_readreg(device_t, int, int, uint16_t *);
static int sipcom_sis900_mii_writereg(device_t, int, int, uint16_t);
static void sipcom_sis900_mii_statchg(struct ifnet *);
static int sipcom_dp83815_mii_readreg(device_t, int, int, uint16_t *);
static int sipcom_dp83815_mii_writereg(device_t, int, int, uint16_t);
static void sipcom_dp83815_mii_statchg(struct ifnet *);
static void sipcom_mediastatus(struct ifnet *, struct ifmediareq *);
static int sipcom_match(device_t, cfdata_t, void *);
static void sipcom_attach(device_t, device_t, void *);
static void sipcom_do_detach(device_t, enum sip_attach_stage);
static int sipcom_detach(device_t, int);
static bool sipcom_resume(device_t, const pmf_qual_t *);
static bool sipcom_suspend(device_t, const pmf_qual_t *);
int gsip_copy_small = 0;
int sip_copy_small = 0;
CFATTACH_DECL3_NEW(gsip, sizeof(struct sip_softc),
sipcom_match, sipcom_attach, sipcom_detach, NULL, NULL, NULL,
DVF_DETACH_SHUTDOWN);
CFATTACH_DECL3_NEW(sip, sizeof(struct sip_softc),
sipcom_match, sipcom_attach, sipcom_detach, NULL, NULL, NULL,
DVF_DETACH_SHUTDOWN);
/*
* Descriptions of the variants of the SiS900.
*/
struct sip_variant {
int (*sipv_mii_readreg)(device_t, int, int, uint16_t *);
int (*sipv_mii_writereg)(device_t, int, int, uint16_t);
void (*sipv_mii_statchg)(struct ifnet *);
void (*sipv_set_filter)(struct sip_softc *);
void (*sipv_read_macaddr)(struct sip_softc *,
const struct pci_attach_args *, uint8_t *);
};
static uint32_t sipcom_mii_bitbang_read(device_t);
static void sipcom_mii_bitbang_write(device_t, uint32_t);
static const struct mii_bitbang_ops sipcom_mii_bitbang_ops = {
sipcom_mii_bitbang_read,
sipcom_mii_bitbang_write,
{
EROMAR_MDIO, /* MII_BIT_MDO */
EROMAR_MDIO, /* MII_BIT_MDI */
EROMAR_MDC, /* MII_BIT_MDC */
EROMAR_MDDIR, /* MII_BIT_DIR_HOST_PHY */
0, /* MII_BIT_DIR_PHY_HOST */
}
};
static const struct sip_variant sipcom_variant_dp83820 = {
sipcom_dp83820_mii_readreg,
sipcom_dp83820_mii_writereg,
sipcom_dp83820_mii_statchg,
sipcom_dp83815_set_filter,
sipcom_dp83820_read_macaddr,
};
static const struct sip_variant sipcom_variant_sis900 = {
sipcom_sis900_mii_readreg,
sipcom_sis900_mii_writereg,
sipcom_sis900_mii_statchg,
sipcom_sis900_set_filter,
sipcom_sis900_read_macaddr,
};
static const struct sip_variant sipcom_variant_dp83815 = {
sipcom_dp83815_mii_readreg,
sipcom_dp83815_mii_writereg,
sipcom_dp83815_mii_statchg,
sipcom_dp83815_set_filter,
sipcom_dp83815_read_macaddr,
};
/*
* Devices supported by this driver.
*/
static const struct sip_product {
pci_vendor_id_t sip_vendor;
pci_product_id_t sip_product;
const char *sip_name;
const struct sip_variant *sip_variant;
bool sip_gigabit;
} sipcom_products[] = {
{ PCI_VENDOR_NS, PCI_PRODUCT_NS_DP83820,
"NatSemi DP83820 Gigabit Ethernet",
&sipcom_variant_dp83820, true },
{ PCI_VENDOR_SIS, PCI_PRODUCT_SIS_900,
"SiS 900 10/100 Ethernet",
&sipcom_variant_sis900, false },
{ PCI_VENDOR_SIS, PCI_PRODUCT_SIS_7016,
"SiS 7016 10/100 Ethernet",
&sipcom_variant_sis900, false },
{ PCI_VENDOR_NS, PCI_PRODUCT_NS_DP83815,
"NatSemi DP83815 10/100 Ethernet",
&sipcom_variant_dp83815, false },
{ 0, 0,
NULL,
NULL, false },
};
static const struct sip_product *
sipcom_lookup(const struct pci_attach_args *pa, bool gigabit)
{
const struct sip_product *sip;
for (sip = sipcom_products; sip->sip_name != NULL; sip++) {
if (PCI_VENDOR(pa->pa_id) == sip->sip_vendor &&
PCI_PRODUCT(pa->pa_id) == sip->sip_product &&
sip->sip_gigabit == gigabit)
return sip;
}
return NULL;
}
/*
* I really hate stupid hardware vendors. There's a bit in the EEPROM
* which indicates if the card can do 64-bit data transfers. Unfortunately,
* several vendors of 32-bit cards fail to clear this bit in the EEPROM,
* which means we try to use 64-bit data transfers on those cards if we
* happen to be plugged into a 32-bit slot.
*
* What we do is use this table of cards known to be 64-bit cards. If
* you have a 64-bit card who's subsystem ID is not listed in this table,
* send the output of "pcictl dump ..." of the device to me so that your
* card will use the 64-bit data path when plugged into a 64-bit slot.
*
* -- Jason R. Thorpe <thorpej@NetBSD.org>
* June 30, 2002
*/
static int
sipcom_check_64bit(const struct pci_attach_args *pa)
{
static const struct {
pci_vendor_id_t c64_vendor;
pci_product_id_t c64_product;
} card64[] = {
/* Asante GigaNIX */
{ 0x128a, 0x0002 },
/* Accton EN1407-T, Planex GN-1000TE */
{ 0x1113, 0x1407 },
/* Netgear GA621 */
{ 0x1385, 0x621a },
/* Netgear GA622 */
{ 0x1385, 0x622a },
/* SMC EZ Card 1000 (9462TX) */
{ 0x10b8, 0x9462 },
{ 0, 0}
};
pcireg_t subsys;
int i;
subsys = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
for (i = 0; card64[i].c64_vendor != 0; i++) {
if (PCI_VENDOR(subsys) == card64[i].c64_vendor &&
PCI_PRODUCT(subsys) == card64[i].c64_product)
return 1;
}
return 0;
}
static int
sipcom_match(device_t parent, cfdata_t cf, void *aux)
{
struct pci_attach_args *pa = aux;
if (sipcom_lookup(pa, strcmp(cf->cf_name, "gsip") == 0) != NULL)
return 1;
return 0;
}
static void
sipcom_dp83820_attach(struct sip_softc *sc, struct pci_attach_args *pa)
{
uint32_t reg;
int i;
/*
* Cause the chip to load configuration data from the EEPROM.
*/
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_PTSCR, PTSCR_EELOAD_EN);
for (i = 0; i < 10000; i++) {
delay(10);
if ((bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_PTSCR) &
PTSCR_EELOAD_EN) == 0)
break;
}
if (bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_PTSCR) &
PTSCR_EELOAD_EN) {
printf("%s: timeout loading configuration from EEPROM\n",
device_xname(sc->sc_dev));
return;
}
sc->sc_gpior = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_GPIOR);
reg = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_CFG);
if (reg & CFG_PCI64_DET) {
const char *using64 = NULL;
if (reg & CFG_DATA64_EN) {
/*
* Check to see if this card is 64-bit. If so,
* enable 64-bit data transfers.
*
* We can't trust the DATA64_EN bit in the EEPROM,
* because vendors of 32-bit cards fail to clear
* that bit in many cases (yet the card still detects
* that it's in a 64-bit slot because I guess they
* wired up ACK64# and REQ64#).
*/
if (gsip_disable_data64)
using64 = "force-disabled";
else if (sipcom_check_64bit(pa)) {
sc->sc_cfg |= CFG_DATA64_EN;
using64 = "enabled";
} else
using64 = "disabled (32-bit card)";
} else {
using64 = "disabled in EEPROM";
}
printf("%s: 64-bit slot detected, 64-bit transfers %s\n",
device_xname(sc->sc_dev), using64);
}
/*
* The T64ADDR bit is loaded by the chip from the EEPROM and
* is read-only.
*/
if (reg & CFG_T64ADDR)
sc->sc_cfg |= CFG_T64ADDR;
/*
* We can use 64-bit DMA addressing regardless of what
* sort of slot we're in.
*/
if (pci_dma64_available(pa)) {
sc->sc_dmat = pa->pa_dmat64;
sc->sc_cfg |= CFG_M64ADDR;
sc->sc_dma64 = true;
}
if (reg & (CFG_TBI_EN | CFG_EXT_125)) {
const char *sep = "";
printf("%s: using ", device_xname(sc->sc_dev));
if (reg & CFG_EXT_125) {
sc->sc_cfg |= CFG_EXT_125;
printf("%sexternal 125MHz clock", sep);
sep = ", ";
}
if (reg & CFG_TBI_EN) {
sc->sc_cfg |= CFG_TBI_EN;
printf("%sten-bit interface", sep);
sep = ", ";
}
printf("\n");
}
if ((pa->pa_flags & PCI_FLAGS_MRM_OKAY) == 0 ||
(reg & CFG_MRM_DIS) != 0)
sc->sc_cfg |= CFG_MRM_DIS;
if ((pa->pa_flags & PCI_FLAGS_MWI_OKAY) == 0 ||
(reg & CFG_MWI_DIS) != 0)
sc->sc_cfg |= CFG_MWI_DIS;
/*
* Use the extended descriptor format on the DP83820. This
* gives us an interface to VLAN tagging and IPv4/TCP/UDP
* checksumming.
*/
sc->sc_cfg |= CFG_EXTSTS_EN;
}
static int
sipcom_detach(device_t self, int flags)
{
int s;
s = splnet();
sipcom_do_detach(self, SIP_ATTACH_FIN);
splx(s);
return 0;
}
static void
sipcom_do_detach(device_t self, enum sip_attach_stage stage)
{
int i;
struct sip_softc *sc = device_private(self);
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
/*
* Free any resources we've allocated during attach.
* Do this in reverse order and fall through.
*/
switch (stage) {
case SIP_ATTACH_FIN:
sipcom_stop(ifp, 1);
pmf_device_deregister(self);
#ifdef SIP_EVENT_COUNTERS
/*
* Attach event counters.
*/
evcnt_detach(&sc->sc_ev_txforceintr);
evcnt_detach(&sc->sc_ev_txdstall);
evcnt_detach(&sc->sc_ev_hiberr);
evcnt_detach(&sc->sc_ev_rxintr);
evcnt_detach(&sc->sc_ev_txiintr);
evcnt_detach(&sc->sc_ev_txdintr);
if (!sc->sc_gigabit) {
evcnt_detach(&sc->sc_ev_rxpause);
} else {
evcnt_detach(&sc->sc_ev_txudpsum);
evcnt_detach(&sc->sc_ev_txtcpsum);
evcnt_detach(&sc->sc_ev_txipsum);
evcnt_detach(&sc->sc_ev_rxudpsum);
evcnt_detach(&sc->sc_ev_rxtcpsum);
evcnt_detach(&sc->sc_ev_rxipsum);
evcnt_detach(&sc->sc_ev_txpause);
evcnt_detach(&sc->sc_ev_rxpause);
}
#endif /* SIP_EVENT_COUNTERS */
rnd_detach_source(&sc->rnd_source);
ether_ifdetach(ifp);
if_detach(ifp);
mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
ifmedia_fini(&sc->sc_mii.mii_media);
/*FALLTHROUGH*/
case SIP_ATTACH_CREATE_RXMAP:
for (i = 0; i < sc->sc_parm->p_nrxdesc; i++) {
if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
bus_dmamap_destroy(sc->sc_dmat,
sc->sc_rxsoft[i].rxs_dmamap);
}
/*FALLTHROUGH*/
case SIP_ATTACH_CREATE_TXMAP:
for (i = 0; i < SIP_TXQUEUELEN; i++) {
if (sc->sc_txsoft[i].txs_dmamap != NULL)
bus_dmamap_destroy(sc->sc_dmat,
sc->sc_txsoft[i].txs_dmamap);
}
/*FALLTHROUGH*/
case SIP_ATTACH_LOAD_MAP:
bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
/*FALLTHROUGH*/
case SIP_ATTACH_CREATE_MAP:
bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
/*FALLTHROUGH*/
case SIP_ATTACH_MAP_MEM:
bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
sizeof(struct sip_control_data));
/*FALLTHROUGH*/
case SIP_ATTACH_ALLOC_MEM:
bus_dmamem_free(sc->sc_dmat, &sc->sc_seg, 1);
/* FALLTHROUGH*/
case SIP_ATTACH_INTR:
pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
/* FALLTHROUGH*/
case SIP_ATTACH_MAP:
bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
break;
default:
break;
}
return;
}
static bool
sipcom_resume(device_t self, const pmf_qual_t *qual)
{
struct sip_softc *sc = device_private(self);
return sipcom_reset(sc);
}
static bool
sipcom_suspend(device_t self, const pmf_qual_t *qual)
{
struct sip_softc *sc = device_private(self);
sipcom_rxdrain(sc);
return true;
}
static void
sipcom_attach(device_t parent, device_t self, void *aux)
{
struct sip_softc *sc = device_private(self);
struct pci_attach_args *pa = aux;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct mii_data * const mii = &sc->sc_mii;
pci_chipset_tag_t pc = pa->pa_pc;
pci_intr_handle_t ih;
const char *intrstr = NULL;
bus_space_tag_t iot, memt;
bus_space_handle_t ioh, memh;
bus_size_t iosz, memsz;
int ioh_valid, memh_valid;
int i, rseg, error;
const struct sip_product *sip;
uint8_t enaddr[ETHER_ADDR_LEN];
pcireg_t csr;
pcireg_t memtype;
bus_size_t tx_dmamap_size;
int ntxsegs_alloc;
cfdata_t cf = device_cfdata(self);
char intrbuf[PCI_INTRSTR_LEN];
callout_init(&sc->sc_tick_ch, 0);
callout_setfunc(&sc->sc_tick_ch, sipcom_tick, sc);
sip = sipcom_lookup(pa, strcmp(cf->cf_name, "gsip") == 0);
if (sip == NULL) {
aprint_error("\n");
panic("%s: impossible", __func__);
}
sc->sc_dev = self;
sc->sc_gigabit = sip->sip_gigabit;
sc->sc_dma64 = false;
pmf_self_suspensor_init(self, &sc->sc_suspensor, &sc->sc_qual);
sc->sc_pc = pc;
if (sc->sc_gigabit) {
if (sc->sc_dma64) {
sc->sc_bufptr_idx = GSIP64_DESC_BUFPTR_LO;
sc->sc_cmdsts_idx = GSIP64_DESC_CMDSTS;
sc->sc_extsts_idx = GSIP64_DESC_EXTSTS;
} else {
sc->sc_bufptr_idx = GSIP_DESC_BUFPTR;
sc->sc_cmdsts_idx = GSIP_DESC_CMDSTS;
sc->sc_extsts_idx = GSIP_DESC_EXTSTS;
}
sc->sc_rxintr = gsip_rxintr;
sc->sc_parm = &gsip_parm;
} else {
sc->sc_rxintr = sip_rxintr;
sc->sc_parm = &sip_parm;
sc->sc_bufptr_idx = SIP_DESC_BUFPTR;
sc->sc_cmdsts_idx = SIP_DESC_CMDSTS;
/*
* EXTSTS doesn't really exist on non-GigE parts,
* but we initialize the index for simplicity later.
*/
sc->sc_extsts_idx = GSIP_DESC_EXTSTS;
}
tx_dmamap_size = sc->sc_parm->p_tx_dmamap_size;
ntxsegs_alloc = sc->sc_parm->p_ntxsegs_alloc;
sc->sc_ntxdesc = SIP_TXQUEUELEN * ntxsegs_alloc;
sc->sc_ntxdesc_mask = sc->sc_ntxdesc - 1;
sc->sc_nrxdesc_mask = sc->sc_parm->p_nrxdesc - 1;
sc->sc_rev = PCI_REVISION(pa->pa_class);
aprint_naive("\n");
aprint_normal(": %s, rev %#02x\n", sip->sip_name, sc->sc_rev);
sc->sc_model = sip;
/*
* XXX Work-around broken PXE firmware on some boards.
*
* The DP83815 shares an address decoder with the MEM BAR
* and the ROM BAR. Make sure the ROM BAR is disabled,
* so that memory mapped access works.
*/
pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM) &
~PCI_MAPREG_ROM_ENABLE);
/*
* Map the device.
*/
ioh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGIOA,
PCI_MAPREG_TYPE_IO, 0,
&iot, &ioh, NULL, &iosz) == 0);
if (sc->sc_gigabit) {
memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, SIP_PCI_CFGMA);
switch (memtype) {
case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
memh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGMA,
memtype, 0, &memt, &memh, NULL, &memsz) == 0);
break;
default:
memh_valid = 0;
}
} else {
memh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGMA,
PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
&memt, &memh, NULL, &memsz) == 0);
}
if (memh_valid) {
sc->sc_st = memt;
sc->sc_sh = memh;
sc->sc_sz = memsz;
} else if (ioh_valid) {
sc->sc_st = iot;
sc->sc_sh = ioh;
sc->sc_sz = iosz;
} else {
aprint_error_dev(self, "unable to map device registers\n");
return;
}
sc->sc_dmat = pa->pa_dmat;
/*
* Make sure bus mastering is enabled. Also make sure
* Write/Invalidate is enabled if we're allowed to use it.
*/
csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
if (pa->pa_flags & PCI_FLAGS_MWI_OKAY)
csr |= PCI_COMMAND_INVALIDATE_ENABLE;
pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
csr | PCI_COMMAND_MASTER_ENABLE);
/* Power up chip */
error = pci_activate(pa->pa_pc, pa->pa_tag, self, pci_activate_null);
if (error != 0 && error != EOPNOTSUPP) {
aprint_error_dev(sc->sc_dev, "cannot activate %d\n", error);
return;
}
/*
* Map and establish our interrupt.
*/
if (pci_intr_map(pa, &ih)) {
aprint_error_dev(sc->sc_dev, "unable to map interrupt\n");
return;
}
intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_NET, sipcom_intr, sc,
device_xname(self));
if (sc->sc_ih == NULL) {
aprint_error_dev(sc->sc_dev, "unable to establish interrupt");
if (intrstr != NULL)
aprint_error(" at %s", intrstr);
aprint_error("\n");
sipcom_do_detach(self, SIP_ATTACH_MAP);
return;
}
aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
SIMPLEQ_INIT(&sc->sc_txfreeq);
SIMPLEQ_INIT(&sc->sc_txdirtyq);
/*
* Allocate the control data structures, and create and load the
* DMA map for it.
*/
if ((error = bus_dmamem_alloc(sc->sc_dmat,
sizeof(struct sip_control_data), PAGE_SIZE, 0, &sc->sc_seg, 1,
&rseg, 0)) != 0) {
aprint_error_dev(sc->sc_dev,
"unable to allocate control data, error = %d\n", error);
sipcom_do_detach(self, SIP_ATTACH_INTR);
return;
}
if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_seg, rseg,
sizeof(struct sip_control_data), (void **)&sc->sc_control_data,
BUS_DMA_COHERENT)) != 0) {
aprint_error_dev(sc->sc_dev,
"unable to map control data, error = %d\n", error);
sipcom_do_detach(self, SIP_ATTACH_ALLOC_MEM);
}
if ((error = bus_dmamap_create(sc->sc_dmat,
sizeof(struct sip_control_data), 1,
sizeof(struct sip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
aprint_error_dev(self, "unable to create control data DMA map"
", error = %d\n", error);
sipcom_do_detach(self, SIP_ATTACH_MAP_MEM);
}
if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
sc->sc_control_data, sizeof(struct sip_control_data), NULL,
0)) != 0) {
aprint_error_dev(self, "unable to load control data DMA map"
", error = %d\n", error);
sipcom_do_detach(self, SIP_ATTACH_CREATE_MAP);
}
/*
* Create the transmit buffer DMA maps.
*/
for (i = 0; i < SIP_TXQUEUELEN; i++) {
if ((error = bus_dmamap_create(sc->sc_dmat, tx_dmamap_size,
sc->sc_parm->p_ntxsegs, MCLBYTES, 0, 0,
&sc->sc_txsoft[i].txs_dmamap)) != 0) {
aprint_error_dev(self, "unable to create tx DMA map %d"
", error = %d\n", i, error);
sipcom_do_detach(self, SIP_ATTACH_CREATE_TXMAP);
}
}
/*
* Create the receive buffer DMA maps.
*/
for (i = 0; i < sc->sc_parm->p_nrxdesc; i++) {
if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
aprint_error_dev(self, "unable to create rx DMA map %d"
", error = %d\n", i, error);
sipcom_do_detach(self, SIP_ATTACH_CREATE_RXMAP);
}
sc->sc_rxsoft[i].rxs_mbuf = NULL;
}
/*
* Reset the chip to a known state.
*/
sipcom_reset(sc);
/*
* Read the Ethernet address from the EEPROM. This might
* also fetch other stuff from the EEPROM and stash it
* in the softc.
*/
sc->sc_cfg = 0;
if (!sc->sc_gigabit) {
if (SIP_SIS900_REV(sc, SIS_REV_635) ||
SIP_SIS900_REV(sc, SIS_REV_900B))
sc->sc_cfg |= (CFG_PESEL | CFG_RNDCNT);
if (SIP_SIS900_REV(sc, SIS_REV_635) ||
SIP_SIS900_REV(sc, SIS_REV_960) ||
SIP_SIS900_REV(sc, SIS_REV_900B))
sc->sc_cfg |=
(bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_CFG) &
CFG_EDBMASTEN);
}
(*sip->sip_variant->sipv_read_macaddr)(sc, pa, enaddr);
aprint_normal_dev(self, "Ethernet address %s\n",ether_sprintf(enaddr));
/*
* Initialize the configuration register: aggressive PCI
* bus request algorithm, default backoff, default OW timer,
* default parity error detection.
*
* NOTE: "Big endian mode" is useless on the SiS900 and
* friends -- it affects packet data, not descriptors.
*/
if (sc->sc_gigabit)
sipcom_dp83820_attach(sc, pa);
/*
* Initialize our media structures and probe the MII.
*/
mii->mii_ifp = ifp;
mii->mii_readreg = sip->sip_variant->sipv_mii_readreg;
mii->mii_writereg = sip->sip_variant->sipv_mii_writereg;
mii->mii_statchg = sip->sip_variant->sipv_mii_statchg;
sc->sc_ethercom.ec_mii = mii;
ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
sipcom_mediastatus);
/*
* XXX We cannot handle flow control on the DP83815.
*/
if (SIP_CHIP_MODEL(sc, PCI_VENDOR_NS, PCI_PRODUCT_NS_DP83815))
mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
MII_OFFSET_ANY, 0);
else
mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
MII_OFFSET_ANY, MIIF_DOPAUSE);
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);
ifp = &sc->sc_ethercom.ec_if;
strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
ifp->if_softc = sc;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
sc->sc_if_flags = ifp->if_flags;
ifp->if_ioctl = sipcom_ioctl;
ifp->if_start = sipcom_start;
ifp->if_watchdog = sipcom_watchdog;
ifp->if_init = sipcom_init;
ifp->if_stop = sipcom_stop;
IFQ_SET_READY(&ifp->if_snd);
/*
* We can support 802.1Q VLAN-sized frames.
*/
sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
if (sc->sc_gigabit) {
/*
* And the DP83820 can do VLAN tagging in hardware, and
* support the jumbo Ethernet MTU.
*/
sc->sc_ethercom.ec_capabilities |=
ETHERCAP_VLAN_HWTAGGING | ETHERCAP_JUMBO_MTU;
sc->sc_ethercom.ec_capenable |= ETHERCAP_VLAN_HWTAGGING;
/*
* The DP83820 can do IPv4, TCPv4, and UDPv4 checksums
* in hardware.
*/
ifp->if_capabilities |=
IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
}
/*
* Attach the interface.
*/
if_attach(ifp);
if_deferred_start_init(ifp, NULL);
ether_ifattach(ifp, enaddr);
ether_set_ifflags_cb(&sc->sc_ethercom, sip_ifflags_cb);
sc->sc_prev.ec_capenable = sc->sc_ethercom.ec_capenable;
sc->sc_prev.is_vlan = VLAN_ATTACHED(&(sc)->sc_ethercom);
sc->sc_prev.if_capenable = ifp->if_capenable;
rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
RND_TYPE_NET, RND_FLAG_DEFAULT);
/*
* The number of bytes that must be available in
* the Tx FIFO before the bus master can DMA more
* data into the FIFO.
*/
sc->sc_tx_fill_thresh = 64 / 32;
/*
* Start at a drain threshold of 512 bytes. We will
* increase it if a DMA underrun occurs.
*
* XXX The minimum value of this variable should be
* tuned. We may be able to improve performance
* by starting with a lower value. That, however,
* may trash the first few outgoing packets if the
* PCI bus is saturated.
*/
if (sc->sc_gigabit)
sc->sc_tx_drain_thresh = 6400 / 32; /* from FreeBSD nge(4) */
else
sc->sc_tx_drain_thresh = 1504 / 32;
/*
* Initialize the Rx FIFO drain threshold.
*
* This is in units of 8 bytes.
*
* We should never set this value lower than 2; 14 bytes are
* required to filter the packet.
*/
sc->sc_rx_drain_thresh = 128 / 8;
#ifdef SIP_EVENT_COUNTERS
/*
* Attach event counters.
*/
evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC,
NULL, device_xname(sc->sc_dev), "txdstall");
evcnt_attach_dynamic(&sc->sc_ev_txforceintr, EVCNT_TYPE_INTR,
NULL, device_xname(sc->sc_dev), "txforceintr");
evcnt_attach_dynamic(&sc->sc_ev_txdintr, EVCNT_TYPE_INTR,
NULL, device_xname(sc->sc_dev), "txdintr");
evcnt_attach_dynamic(&sc->sc_ev_txiintr, EVCNT_TYPE_INTR,
NULL, device_xname(sc->sc_dev), "txiintr");
evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
NULL, device_xname(sc->sc_dev), "rxintr");
evcnt_attach_dynamic(&sc->sc_ev_hiberr, EVCNT_TYPE_INTR,
NULL, device_xname(sc->sc_dev), "hiberr");
if (!sc->sc_gigabit) {
evcnt_attach_dynamic(&sc->sc_ev_rxpause, EVCNT_TYPE_INTR,
NULL, device_xname(sc->sc_dev), "rxpause");
} else {
evcnt_attach_dynamic(&sc->sc_ev_rxpause, EVCNT_TYPE_MISC,
NULL, device_xname(sc->sc_dev), "rxpause");
evcnt_attach_dynamic(&sc->sc_ev_txpause, EVCNT_TYPE_MISC,
NULL, device_xname(sc->sc_dev), "txpause");
evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
NULL, device_xname(sc->sc_dev), "rxipsum");
evcnt_attach_dynamic(&sc->sc_ev_rxtcpsum, EVCNT_TYPE_MISC,
NULL, device_xname(sc->sc_dev), "rxtcpsum");
evcnt_attach_dynamic(&sc->sc_ev_rxudpsum, EVCNT_TYPE_MISC,
NULL, device_xname(sc->sc_dev), "rxudpsum");
evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
NULL, device_xname(sc->sc_dev), "txipsum");
evcnt_attach_dynamic(&sc->sc_ev_txtcpsum, EVCNT_TYPE_MISC,
NULL, device_xname(sc->sc_dev), "txtcpsum");
evcnt_attach_dynamic(&sc->sc_ev_txudpsum, EVCNT_TYPE_MISC,
NULL, device_xname(sc->sc_dev), "txudpsum");
}
#endif /* SIP_EVENT_COUNTERS */
if (pmf_device_register(self, sipcom_suspend, sipcom_resume))
pmf_class_network_register(self, ifp);
else
aprint_error_dev(self, "couldn't establish power handler\n");
}
static inline void
sipcom_set_extsts(struct sip_softc *sc, int lasttx, struct mbuf *m0,
uint64_t capenable)
{
uint32_t extsts = 0;
#ifdef DEBUG
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
#endif
/*
* If VLANs are enabled and the packet has a VLAN tag, set
* up the descriptor to encapsulate the packet for us.
*
* This apparently has to be on the last descriptor of
* the packet.
*/
/*
* Byte swapping is tricky. We need to provide the tag
* in a network byte order. On a big-endian machine,
* the byteorder is correct, but we need to swap it
* anyway, because this will be undone by the outside
* htole32(). That's why there must be an
* unconditional swap instead of htons() inside.
*/
if (vlan_has_tag(m0)) {
sc->sc_txdescs[lasttx].sipd_words[sc->sc_extsts_idx] |=
htole32(EXTSTS_VPKT |
(bswap16(vlan_get_tag(m0)) &
EXTSTS_VTCI));
}
/*
* If the upper-layer has requested IPv4/TCPv4/UDPv4
* checksumming, set up the descriptor to do this work
* for us.
*
* This apparently has to be on the first descriptor of
* the packet.
*
* Byte-swap constants so the compiler can optimize.
*/
if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
KDASSERT(ifp->if_capenable & IFCAP_CSUM_IPv4_Tx);
SIP_EVCNT_INCR(&sc->sc_ev_txipsum);
extsts |= htole32(EXTSTS_IPPKT);
}
if (m0->m_pkthdr.csum_flags & M_CSUM_TCPv4) {
KDASSERT(ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx);
SIP_EVCNT_INCR(&sc->sc_ev_txtcpsum);
extsts |= htole32(EXTSTS_TCPPKT);
} else if (m0->m_pkthdr.csum_flags & M_CSUM_UDPv4) {
KDASSERT(ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx);
SIP_EVCNT_INCR(&sc->sc_ev_txudpsum);
extsts |= htole32(EXTSTS_UDPPKT);
}
sc->sc_txdescs[sc->sc_txnext].sipd_words[sc->sc_extsts_idx] |= extsts;
}
/*
* sip_start: [ifnet interface function]
*
* Start packet transmission on the interface.
*/
static void
sipcom_start(struct ifnet *ifp)
{
struct sip_softc *sc = ifp->if_softc;
struct mbuf *m0;
struct mbuf *m;
struct sip_txsoft *txs;
bus_dmamap_t dmamap;
int error, nexttx, lasttx, seg;
int ofree = sc->sc_txfree;
uint32_t cmdsts;
#if 0
int firsttx = sc->sc_txnext;
#endif
/*
* If we've been told to pause, don't transmit any more packets.
*/
if (!sc->sc_gigabit && sc->sc_paused)
return;
if ((ifp->if_flags & IFF_RUNNING) != IFF_RUNNING)
return;
/*
* 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) {
/*
* 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.
*/
error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
BUS_DMA_WRITE | BUS_DMA_NOWAIT);
/* In the non-gigabit case, we'll copy and try again. */
if (error != 0 && !sc->sc_gigabit) {
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;
}
} else if (error == EFBIG) {
/*
* For the too-many-segments case, we simply
* report an error and drop the packet,
* since we can't sanely copy a jumbo packet
* to a single buffer.
*/
printf("%s: Tx packet consumes too many DMA segments, "
"dropping...\n", device_xname(sc->sc_dev));
IFQ_DEQUEUE(&ifp->if_snd, m0);
m_freem(m0);
continue;
} else if (error != 0) {
/*
* Short on resources, just stop for now.
*/
break;
}
/*
* Ensure we have enough descriptors free to describe
* the packet. Note, we always reserve one descriptor
* at the end of the ring as a termination point, to
* prevent wrap-around.
*/
if (dmamap->dm_nsegs > (sc->sc_txfree - 1)) {
/*
* Not enough free descriptors to transmit this
* packet.
*/
bus_dmamap_unload(sc->sc_dmat, dmamap);
if (m != NULL)
m_freem(m);
SIP_EVCNT_INCR(&sc->sc_ev_txdstall);
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 = lasttx = sc->sc_txnext, seg = 0;
seg < dmamap->dm_nsegs;
seg++, nexttx = sip_nexttx(sc, 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.
*/
cmdsts = dmamap->dm_segs[seg].ds_len;
if (nexttx != sc->sc_txnext)
cmdsts |= CMDSTS_OWN;
if (seg < dmamap->dm_nsegs - 1)
cmdsts |= CMDSTS_MORE;
sip_init_txdesc(sc, nexttx,
dmamap->dm_segs[seg].ds_addr, cmdsts);
lasttx = nexttx;
}
/*
* If we're in the interrupt delay window, delay the
* interrupt.
*/
if (++sc->sc_txwin >= (SIP_TXQUEUELEN * 2 / 3)) {
SIP_EVCNT_INCR(&sc->sc_ev_txforceintr);
sc->sc_txdescs[lasttx].sipd_words[sc->sc_cmdsts_idx] |=
htole32(CMDSTS_INTR);
sc->sc_txwin = 0;
}
if (sc->sc_gigabit)
sipcom_set_extsts(sc, lasttx, m0, ifp->if_capenable);
/* Sync the descriptors we're using. */
sip_cdtxsync(sc, sc->sc_txnext, dmamap->dm_nsegs,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
/*
* The entire packet is set up. Give the first descriptor
* to the chip now.
*/
sc->sc_txdescs[sc->sc_txnext].sipd_words[sc->sc_cmdsts_idx] |=
htole32(CMDSTS_OWN);
sip_cdtxsync(sc, sc->sc_txnext, 1,
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;
/* 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) {
/*
* Start the transmit process. Note, the manual says
* that if there are no pending transmissions in the
* chip's internal queue (indicated by TXE being clear),
* then the driver software must set the TXDP to the
* first descriptor to be transmitted. However, if we
* do this, it causes serious performance degradation on
* the DP83820 under load, not setting TXDP doesn't seem
* to adversely affect the SiS 900 or DP83815.
*
* Well, I guess it wouldn't be the first time a manual
* has lied -- and they could be speaking of the NULL-
* terminated descriptor list case, rather than OWN-
* terminated rings.
*/
#if 0
if ((bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_CR) &
CR_TXE) == 0) {
sip_set_txdp(sc, SIP_CDTXADDR(sc, firsttx));
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CR, CR_TXE);
}
#else
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CR, CR_TXE);
#endif
/* Set a watchdog timer in case the chip flakes out. */
/* Gigabit autonegotiation takes 5 seconds. */
ifp->if_timer = (sc->sc_gigabit) ? 10 : 5;
}
}
/*
* sip_watchdog: [ifnet interface function]
*
* Watchdog timer handler.
*/
static void
sipcom_watchdog(struct ifnet *ifp)
{
struct sip_softc *sc = ifp->if_softc;
/*
* The chip seems to ignore the CMDSTS_INTR bit sometimes!
* If we get a timeout, try and sweep up transmit descriptors.
* If we manage to sweep them all up, ignore the lack of
* interrupt.
*/
sipcom_txintr(sc);
if (sc->sc_txfree != sc->sc_ntxdesc) {
printf("%s: device timeout\n", device_xname(sc->sc_dev));
if_statinc(ifp, if_oerrors);
/* Reset the interface. */
(void) sipcom_init(ifp);
} else if (ifp->if_flags & IFF_DEBUG)
printf("%s: recovered from device timeout\n",
device_xname(sc->sc_dev));
/* Try to get more packets going. */
sipcom_start(ifp);
}
/* If the interface is up and running, only modify the receive
* filter when setting promiscuous or debug mode. Otherwise fall
* through to ether_ioctl, which will reset the chip.
*/
static int
sip_ifflags_cb(struct ethercom *ec)
{
#define COMPARE_EC(sc) (((sc)->sc_prev.ec_capenable \
== (sc)->sc_ethercom.ec_capenable) \
&& ((sc)->sc_prev.is_vlan == \
VLAN_ATTACHED(&(sc)->sc_ethercom) ))
#define COMPARE_IC(sc, ifp) ((sc)->sc_prev.if_capenable == (ifp)->if_capenable)
struct ifnet *ifp = &ec->ec_if;
struct sip_softc *sc = ifp->if_softc;
u_short change = ifp->if_flags ^ sc->sc_if_flags;
if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0 || !COMPARE_EC(sc) ||
!COMPARE_IC(sc, ifp))
return ENETRESET;
/* Set up the receive filter. */
(*sc->sc_model->sip_variant->sipv_set_filter)(sc);
return 0;
}
/*
* sip_ioctl: [ifnet interface function]
*
* Handle control requests from the operator.
*/
static int
sipcom_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
struct sip_softc *sc = ifp->if_softc;
struct ifreq *ifr = (struct ifreq *)data;
int s, error;
s = splnet();
switch (cmd) {
case SIOCSIFMEDIA:
/* Flow control requires full-duplex mode. */
if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
(ifr->ifr_media & IFM_FDX) == 0)
ifr->ifr_media &= ~IFM_ETH_FMASK;
/* XXX */
if (SIP_CHIP_MODEL(sc, PCI_VENDOR_NS, PCI_PRODUCT_NS_DP83815))
ifr->ifr_media &= ~IFM_ETH_FMASK;
if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
if (sc->sc_gigabit &&
(ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
/* We can do both TXPAUSE and RXPAUSE. */
ifr->ifr_media |=
IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
} else if (ifr->ifr_media & IFM_FLOW) {
/*
* Both TXPAUSE and RXPAUSE must be set.
* (SiS900 and DP83815 don't have PAUSE_ASYM
* feature.)
*
* XXX Can SiS900 and DP83815 send PAUSE?
*/
ifr->ifr_media |=
IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
}
sc->sc_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
}
/*FALLTHROUGH*/
default:
if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
break;
error = 0;
if (cmd == SIOCSIFCAP)
error = if_init(ifp);
else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
;
else if (ifp->if_flags & IFF_RUNNING) {
/*
* Multicast list has changed; set the hardware filter
* accordingly.
*/
(*sc->sc_model->sip_variant->sipv_set_filter)(sc);
}
break;
}
/* Try to get more packets going. */
sipcom_start(ifp);
sc->sc_if_flags = ifp->if_flags;
splx(s);
return error;
}
/*
* sip_intr:
*
* Interrupt service routine.
*/
static int
sipcom_intr(void *arg)
{
struct sip_softc *sc = arg;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
uint32_t isr;
int handled = 0;
if (!device_activation(sc->sc_dev, DEVACT_LEVEL_DRIVER))
return 0;
/* Disable interrupts. */
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_IER, 0);
for (;;) {
/* Reading clears interrupt. */
isr = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ISR);
if ((isr & sc->sc_imr) == 0)
break;
rnd_add_uint32(&sc->rnd_source, isr);
handled = 1;
if ((ifp->if_flags & IFF_RUNNING) == 0)
break;
if (isr & (ISR_RXORN | ISR_RXIDLE | ISR_RXDESC)) {
SIP_EVCNT_INCR(&sc->sc_ev_rxintr);
/* Grab any new packets. */
(*sc->sc_rxintr)(sc);
if (isr & ISR_RXORN) {
printf("%s: receive FIFO overrun\n",
device_xname(sc->sc_dev));
/* XXX adjust rx_drain_thresh? */
}
if (isr & ISR_RXIDLE) {
printf("%s: receive ring overrun\n",
device_xname(sc->sc_dev));
/* Get the receive process going again. */
sip_set_rxdp(sc,
SIP_CDRXADDR(sc, sc->sc_rxptr));
bus_space_write_4(sc->sc_st, sc->sc_sh,
SIP_CR, CR_RXE);
}
}
if (isr & (ISR_TXURN | ISR_TXDESC | ISR_TXIDLE)) {
#ifdef SIP_EVENT_COUNTERS
if (isr & ISR_TXDESC)
SIP_EVCNT_INCR(&sc->sc_ev_txdintr);
else if (isr & ISR_TXIDLE)
SIP_EVCNT_INCR(&sc->sc_ev_txiintr);
#endif
/* Sweep up transmit descriptors. */
sipcom_txintr(sc);
if (isr & ISR_TXURN) {
uint32_t thresh;
int txfifo_size = (sc->sc_gigabit)
? DP83820_SIP_TXFIFO_SIZE
: OTHER_SIP_TXFIFO_SIZE;
printf("%s: transmit FIFO underrun",
device_xname(sc->sc_dev));
thresh = sc->sc_tx_drain_thresh + 1;
if (thresh <= __SHIFTOUT_MASK(sc->sc_bits.b_txcfg_drth_mask)
&& (thresh * 32) <= (txfifo_size -
(sc->sc_tx_fill_thresh * 32))) {
printf("; increasing Tx drain "
"threshold to %u bytes\n",
thresh * 32);
sc->sc_tx_drain_thresh = thresh;
(void) sipcom_init(ifp);
} else {
(void) sipcom_init(ifp);
printf("\n");
}
}
}
if (sc->sc_imr & (ISR_PAUSE_END | ISR_PAUSE_ST)) {
if (isr & ISR_PAUSE_ST) {
sc->sc_paused = 1;
SIP_EVCNT_INCR(&sc->sc_ev_rxpause);
}
if (isr & ISR_PAUSE_END) {
sc->sc_paused = 0;
}
}
if (isr & ISR_HIBERR) {
int want_init = 0;
SIP_EVCNT_INCR(&sc->sc_ev_hiberr);
#define PRINTERR(bit, str) \
do { \
if ((isr & (bit)) != 0) { \
if ((ifp->if_flags & IFF_DEBUG) != 0) \
printf("%s: %s\n", \
device_xname(sc->sc_dev), str); \
want_init = 1; \
} \
} while (/*CONSTCOND*/0)
PRINTERR(sc->sc_bits.b_isr_dperr, "parity error");
PRINTERR(sc->sc_bits.b_isr_sserr, "system error");
PRINTERR(sc->sc_bits.b_isr_rmabt, "master abort");
PRINTERR(sc->sc_bits.b_isr_rtabt, "target abort");
PRINTERR(ISR_RXSOVR, "receive status FIFO overrun");
/*
* Ignore:
* Tx reset complete
* Rx reset complete
*/
if (want_init)
(void) sipcom_init(ifp);
#undef PRINTERR
}
}
/* Re-enable interrupts. */
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_IER, IER_IE);
/* Try to get more packets going. */
if_schedule_deferred_start(ifp);
return handled;
}
/*
* sip_txintr:
*
* Helper; handle transmit interrupts.
*/
static void
sipcom_txintr(struct sip_softc *sc)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct sip_txsoft *txs;
uint32_t cmdsts;
/*
* Go through our Tx list and free mbufs for those
* frames which have been transmitted.
*/
while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
sip_cdtxsync(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
cmdsts = le32toh(sc->sc_txdescs[
txs->txs_lastdesc].sipd_words[sc->sc_cmdsts_idx]);
if (cmdsts & CMDSTS_OWN)
break;
SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
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. */
net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
if (cmdsts & (CMDSTS_Tx_TXA | CMDSTS_Tx_TFU | CMDSTS_Tx_ED |
CMDSTS_Tx_EC)) {
if_statinc_ref(nsr, if_oerrors);
if (cmdsts & CMDSTS_Tx_EC)
if_statadd_ref(nsr, if_collisions, 16);
if (ifp->if_flags & IFF_DEBUG) {
if (cmdsts & CMDSTS_Tx_ED)
printf("%s: excessive deferral\n",
device_xname(sc->sc_dev));
if (cmdsts & CMDSTS_Tx_EC)
printf("%s: excessive collisions\n",
device_xname(sc->sc_dev));
}
} else {
/* Packet was transmitted successfully. */
if_statinc_ref(nsr, if_opackets);
if (CMDSTS_COLLISIONS(cmdsts))
if_statadd_ref(nsr, if_collisions,
CMDSTS_COLLISIONS(cmdsts));
}
IF_STAT_PUTREF(ifp);
}
/*
* If there are no more pending transmissions, cancel the watchdog
* timer.
*/
if (txs == NULL) {
ifp->if_timer = 0;
sc->sc_txwin = 0;
}
}
/*
* gsip_rxintr:
*
* Helper; handle receive interrupts on gigabit parts.
*/
static void
gsip_rxintr(struct sip_softc *sc)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct sip_rxsoft *rxs;
struct mbuf *m;
uint32_t cmdsts, extsts;
int i, len;
for (i = sc->sc_rxptr;; i = sip_nextrx(sc, i)) {
rxs = &sc->sc_rxsoft[i];
sip_cdrxsync(sc, i,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
cmdsts =
le32toh(sc->sc_rxdescs[i].sipd_words[sc->sc_cmdsts_idx]);
/*
* NOTE: OWN is set if owned by _consumer_. We're the
* consumer of the receive ring, so if the bit is clear,
* we have processed all of the packets.
*/
if ((cmdsts & CMDSTS_OWN) == 0) {
/*
* We have processed all of the receive buffers.
*/
break;
}
sip_cdrxsync(sc, i, BUS_DMASYNC_POSTREAD);
extsts =
le32toh(sc->sc_rxdescs[i].sipd_words[sc->sc_extsts_idx]);
len = CMDSTS_SIZE(sc, cmdsts);
if (__predict_false(sc->sc_rxdiscard)) {
sip_init_rxdesc(sc, i);
if ((cmdsts & CMDSTS_MORE) == 0) {
/* Reset our state. */
sc->sc_rxdiscard = 0;
}
continue;
}
bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
m = rxs->rxs_mbuf;
/*
* Add a new receive buffer to the ring.
*/
if (sipcom_add_rxbuf(sc, i) != 0) {
/*
* Failed, throw away what we've done so
* far, and discard the rest of the packet.
*/
if_statinc(ifp, if_ierrors);
bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
sip_init_rxdesc(sc, i);
if (cmdsts & CMDSTS_MORE)
sc->sc_rxdiscard = 1;
if (sc->sc_rxhead != NULL)
m_freem(sc->sc_rxhead);
sip_rxchain_reset(sc);
continue;
}
sip_rxchain_link(sc, m);
m->m_len = len;
/*
* If this is not the end of the packet, keep
* looking.
*/
if (cmdsts & CMDSTS_MORE) {
sc->sc_rxlen += len;
continue;
}
/*
* Okay, we have the entire packet now. The chip includes
* the FCS, so we need to trim it.
*/
m->m_len -= ETHER_CRC_LEN;
*sc->sc_rxtailp = NULL;
len = m->m_len + sc->sc_rxlen;
m = sc->sc_rxhead;
sip_rxchain_reset(sc);
/* If an error occurred, update stats and drop the packet. */
if (cmdsts & (CMDSTS_Rx_RXA | CMDSTS_Rx_LONG | CMDSTS_Rx_RUNT |
CMDSTS_Rx_ISE | CMDSTS_Rx_CRCE | CMDSTS_Rx_FAE)) {
if_statinc(ifp, if_ierrors);
if ((cmdsts & CMDSTS_Rx_RXA) != 0 &&
(cmdsts & CMDSTS_Rx_RXO) == 0) {
/* Receive overrun handled elsewhere. */
printf("%s: receive descriptor error\n",
device_xname(sc->sc_dev));
}
#define PRINTERR(bit, str) \
if ((ifp->if_flags & IFF_DEBUG) != 0 && \
(cmdsts & (bit)) != 0) \
printf("%s: %s\n", device_xname(sc->sc_dev), str)
PRINTERR(CMDSTS_Rx_LONG, "Too long packet");
PRINTERR(CMDSTS_Rx_RUNT, "runt packet");
PRINTERR(CMDSTS_Rx_ISE, "invalid symbol error");
PRINTERR(CMDSTS_Rx_CRCE, "CRC error");
PRINTERR(CMDSTS_Rx_FAE, "frame alignment error");
#undef PRINTERR
m_freem(m);
continue;
}
/*
* If the packet is small enough to fit in a
* single header mbuf, allocate one and copy
* the data into it. This greatly reduces
* memory consumption when we receive lots
* of small packets.
*/
if (gsip_copy_small != 0 && len <= (MHLEN - 2)) {
struct mbuf *nm;
MGETHDR(nm, M_DONTWAIT, MT_DATA);
if (nm == NULL) {
if_statinc(ifp, if_ierrors);
m_freem(m);
continue;
}
MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
nm->m_data += 2;
nm->m_pkthdr.len = nm->m_len = len;
m_copydata(m, 0, len, mtod(nm, void *));
m_freem(m);
m = nm;
}
#ifndef __NO_STRICT_ALIGNMENT
else {
/*
* The DP83820's receive buffers must be 4-byte
* aligned. But this means that the data after
* the Ethernet header is misaligned. To compensate,
* we have artificially shortened the buffer size
* in the descriptor, and we do an overlapping copy
* of the data two bytes further in (in the first
* buffer of the chain only).
*/
memmove(mtod(m, char *) + 2, mtod(m, void *),
m->m_len);
m->m_data += 2;
}
#endif /* ! __NO_STRICT_ALIGNMENT */
/*
* If VLANs are enabled, VLAN packets have been unwrapped
* for us. Associate the tag with the packet.
*/
/*
* Again, byte swapping is tricky. Hardware provided
* the tag in the network byte order, but extsts was
* passed through le32toh() in the meantime. On a
* big-endian machine, we need to swap it again. On a
* little-endian machine, we need to convert from the
* network to host byte order. This means that we must
* swap it in any case, so unconditional swap instead
* of htons() is used.
*/
if ((extsts & EXTSTS_VPKT) != 0) {
vlan_set_tag(m, bswap16(extsts & EXTSTS_VTCI));
}
/*
* Set the incoming checksum information for the
* packet.
*/
if ((extsts & EXTSTS_IPPKT) != 0) {
SIP_EVCNT_INCR(&sc->sc_ev_rxipsum);
m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
if (extsts & EXTSTS_Rx_IPERR)
m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
if (extsts & EXTSTS_TCPPKT) {
SIP_EVCNT_INCR(&sc->sc_ev_rxtcpsum);
m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
if (extsts & EXTSTS_Rx_TCPERR)
m->m_pkthdr.csum_flags |=
M_CSUM_TCP_UDP_BAD;
} else if (extsts & EXTSTS_UDPPKT) {
SIP_EVCNT_INCR(&sc->sc_ev_rxudpsum);
m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
if (extsts & EXTSTS_Rx_UDPERR)
m->m_pkthdr.csum_flags |=
M_CSUM_TCP_UDP_BAD;
}
}
m_set_rcvif(m, ifp);
m->m_pkthdr.len = len;
/* Pass it on. */
if_percpuq_enqueue(ifp->if_percpuq, m);
}
/* Update the receive pointer. */
sc->sc_rxptr = i;
}
/*
* sip_rxintr:
*
* Helper; handle receive interrupts on 10/100 parts.
*/
static void
sip_rxintr(struct sip_softc *sc)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct sip_rxsoft *rxs;
struct mbuf *m;
uint32_t cmdsts;
int i, len;
for (i = sc->sc_rxptr;; i = sip_nextrx(sc, i)) {
rxs = &sc->sc_rxsoft[i];
sip_cdrxsync(sc, i,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
cmdsts =
le32toh(sc->sc_rxdescs[i].sipd_words[sc->sc_cmdsts_idx]);
/*
* NOTE: OWN is set if owned by _consumer_. We're the
* consumer of the receive ring, so if the bit is clear,
* we have processed all of the packets.
*/
if ((cmdsts & CMDSTS_OWN) == 0) {
/*
* We have processed all of the receive buffers.
*/
break;
}
/* If any collisions were seen on the wire, count one. */
if (cmdsts & CMDSTS_Rx_COL)
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 (cmdsts & (CMDSTS_Rx_RXA | CMDSTS_Rx_LONG | CMDSTS_Rx_RUNT |
CMDSTS_Rx_ISE | CMDSTS_Rx_CRCE | CMDSTS_Rx_FAE)) {
if_statinc(ifp, if_ierrors);
if ((cmdsts & CMDSTS_Rx_RXA) != 0 &&
(cmdsts & CMDSTS_Rx_RXO) == 0) {
/* Receive overrun handled elsewhere. */
printf("%s: receive descriptor error\n",
device_xname(sc->sc_dev));
}
#define PRINTERR(bit, str) \
if ((ifp->if_flags & IFF_DEBUG) != 0 && \
(cmdsts & (bit)) != 0) \
printf("%s: %s\n", device_xname(sc->sc_dev), str)
PRINTERR(CMDSTS_Rx_LONG, "Too long packet");
PRINTERR(CMDSTS_Rx_RUNT, "runt packet");
PRINTERR(CMDSTS_Rx_ISE, "invalid symbol error");
PRINTERR(CMDSTS_Rx_CRCE, "CRC error");
PRINTERR(CMDSTS_Rx_FAE, "frame alignment error");
#undef PRINTERR
sip_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 SiS 900
* includes the CRC with every packet.
*/
len = CMDSTS_SIZE(sc, cmdsts) - ETHER_CRC_LEN;
#ifdef __NO_STRICT_ALIGNMENT
/*
* If the packet is small enough to fit in a
* single header mbuf, allocate one and copy
* the data into it. This greatly reduces
* memory consumption when we receive lots
* of small packets.
*
* Otherwise, we add a new buffer to the receive
* chain. If this fails, we drop the packet and
* recycle the old buffer.
*/
if (sip_copy_small != 0 && len <= MHLEN) {
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL)
goto dropit;
MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
memcpy(mtod(m, void *),
mtod(rxs->rxs_mbuf, void *), len);
sip_init_rxdesc(sc, i);
bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
rxs->rxs_dmamap->dm_mapsize,
BUS_DMASYNC_PREREAD);
} else {
m = rxs->rxs_mbuf;
if (sipcom_add_rxbuf(sc, i) != 0) {
dropit:
if_statinc(ifp, if_ierrors);
sip_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 SiS 900'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);
sip_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. */
sip_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;
}
/*
* sip_tick:
*
* One second timer, used to tick the MII.
*/
static void
sipcom_tick(void *arg)
{
struct sip_softc *sc = arg;
int s;
s = splnet();
#ifdef SIP_EVENT_COUNTERS
if (sc->sc_gigabit) {
/* Read PAUSE related counts from MIB registers. */
sc->sc_ev_rxpause.ev_count +=
bus_space_read_4(sc->sc_st, sc->sc_sh,
SIP_NS_MIB(MIB_RXPauseFrames)) & 0xffff;
sc->sc_ev_txpause.ev_count +=
bus_space_read_4(sc->sc_st, sc->sc_sh,
SIP_NS_MIB(MIB_TXPauseFrames)) & 0xffff;
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_NS_MIBC, MIBC_ACLR);
}
#endif /* SIP_EVENT_COUNTERS */
mii_tick(&sc->sc_mii);
splx(s);
callout_schedule(&sc->sc_tick_ch, hz);
}
/*
* sip_reset:
*
* Perform a soft reset on the SiS 900.
*/
static bool
sipcom_reset(struct sip_softc *sc)
{
bus_space_tag_t st = sc->sc_st;
bus_space_handle_t sh = sc->sc_sh;
int i;
bus_space_write_4(st, sh, SIP_IER, 0);
bus_space_write_4(st, sh, SIP_IMR, 0);
bus_space_write_4(st, sh, SIP_RFCR, 0);
bus_space_write_4(st, sh, SIP_CR, CR_RST);
for (i = 0; i < SIP_TIMEOUT; i++) {
if ((bus_space_read_4(st, sh, SIP_CR) & CR_RST) == 0)
break;
delay(2);
}
if (i == SIP_TIMEOUT) {
printf("%s: reset failed to complete\n",
device_xname(sc->sc_dev));
return false;
}
delay(1000);
if (sc->sc_gigabit) {
/*
* Set the general purpose I/O bits. Do it here in case we
* need to have GPIO set up to talk to the media interface.
*/
bus_space_write_4(st, sh, SIP_GPIOR, sc->sc_gpior);
delay(1000);
}
return true;
}
static void
sipcom_dp83820_init(struct sip_softc *sc, uint64_t capenable)
{
uint32_t reg;
bus_space_tag_t st = sc->sc_st;
bus_space_handle_t sh = sc->sc_sh;
/*
* Initialize the VLAN/IP receive control register.
* We enable checksum computation on all incoming
* packets, and do not reject packets w/ bad checksums.
*/
reg = 0;
if (capenable &
(IFCAP_CSUM_IPv4_Rx | IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
reg |= VRCR_IPEN;
if (VLAN_ATTACHED(&sc->sc_ethercom))
reg |= VRCR_VTDEN | VRCR_VTREN;
bus_space_write_4(st, sh, SIP_VRCR, reg);
/*
* Initialize the VLAN/IP transmit control register.
* We enable outgoing checksum computation on a
* per-packet basis.
*/
reg = 0;
if (capenable &
(IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx))
reg |= VTCR_PPCHK;
if (VLAN_ATTACHED(&sc->sc_ethercom))
reg |= VTCR_VPPTI;
bus_space_write_4(st, sh, SIP_VTCR, reg);
/*
* If we're using VLANs, initialize the VLAN data register.
* To understand why we bswap the VLAN Ethertype, see section
* 4.2.36 of the DP83820 manual.
*/
if (VLAN_ATTACHED(&sc->sc_ethercom))
bus_space_write_4(st, sh, SIP_VDR, bswap16(ETHERTYPE_VLAN));
}
/*
* sip_init: [ ifnet interface function ]
*
* Initialize the interface. Must be called at splnet().
*/
static int
sipcom_init(struct ifnet *ifp)
{
struct sip_softc *sc = ifp->if_softc;
bus_space_tag_t st = sc->sc_st;
bus_space_handle_t sh = sc->sc_sh;
struct sip_txsoft *txs;
struct sip_rxsoft *rxs;
int i, error = 0;
if (device_is_active(sc->sc_dev)) {
/*
* Cancel any pending I/O.
*/
sipcom_stop(ifp, 0);
} else if (!pmf_device_subtree_resume(sc->sc_dev, &sc->sc_qual) ||
!device_is_active(sc->sc_dev))
return 0;
/*
* Reset the chip to a known state.
*/
if (!sipcom_reset(sc))
return EBUSY;
if (SIP_CHIP_MODEL(sc, PCI_VENDOR_NS, PCI_PRODUCT_NS_DP83815)) {
/*
* DP83815 manual, page 78:
* 4.4 Recommended Registers Configuration
* For optimum performance of the DP83815, version noted
* as DP83815CVNG (SRR = 203h), the listed register
* modifications must be followed in sequence...
*
* It's not clear if this should be 302h or 203h because that
* chip name is listed as SRR 302h in the description of the
* SRR register. However, my revision 302h DP83815 on the
* Netgear FA311 purchased in 02/2001 needs these settings
* to avoid tons of errors in AcceptPerfectMatch (non-
* IFF_PROMISC) mode. I do not know if other revisions need
* this set or not. [briggs -- 09 March 2001]
*
* Note that only the low-order 12 bits of 0xe4 are documented
* and that this sets reserved bits in that register.
*/
bus_space_write_4(st, sh, 0x00cc, 0x0001);
bus_space_write_4(st, sh, 0x00e4, 0x189C);
bus_space_write_4(st, sh, 0x00fc, 0x0000);
bus_space_write_4(st, sh, 0x00f4, 0x5040);
bus_space_write_4(st, sh, 0x00f8, 0x008c);
bus_space_write_4(st, sh, 0x00cc, 0x0000);
}
/* Initialize the transmit descriptor ring. */
sip_init_txring(sc);
/*
* Initialize the transmit job descriptors.
*/
SIMPLEQ_INIT(&sc->sc_txfreeq);
SIMPLEQ_INIT(&sc->sc_txdirtyq);
for (i = 0; i < SIP_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 < sc->sc_parm->p_nrxdesc; i++) {
rxs = &sc->sc_rxsoft[i];
if (rxs->rxs_mbuf == NULL) {
if ((error = sipcom_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.
*/
sipcom_rxdrain(sc);
goto out;
}
} else
sip_init_rxdesc(sc, i);
}
sc->sc_rxptr = 0;
sc->sc_rxdiscard = 0;
sip_rxchain_reset(sc);
/*
* Set the configuration register; it's already initialized
* in sip_attach().
*/
bus_space_write_4(st, sh, SIP_CFG, sc->sc_cfg);
/*
* Initialize the prototype TXCFG register.
*/
if (sc->sc_gigabit) {
sc->sc_txcfg = sc->sc_bits.b_txcfg_mxdma_512;
sc->sc_rxcfg = sc->sc_bits.b_rxcfg_mxdma_512;
} else if ((SIP_SIS900_REV(sc, SIS_REV_635) ||
SIP_SIS900_REV(sc, SIS_REV_960) ||
SIP_SIS900_REV(sc, SIS_REV_900B)) &&
(sc->sc_cfg & CFG_EDBMASTEN)) {
sc->sc_txcfg = sc->sc_bits.b_txcfg_mxdma_64;
sc->sc_rxcfg = sc->sc_bits.b_rxcfg_mxdma_64;
} else {
sc->sc_txcfg = sc->sc_bits.b_txcfg_mxdma_512;
sc->sc_rxcfg = sc->sc_bits.b_rxcfg_mxdma_512;
}
sc->sc_txcfg |= TXCFG_ATP |
__SHIFTIN(sc->sc_tx_fill_thresh, sc->sc_bits.b_txcfg_flth_mask) |
sc->sc_tx_drain_thresh;
bus_space_write_4(st, sh, sc->sc_regs.r_txcfg, sc->sc_txcfg);
/*
* Initialize the receive drain threshold if we have never
* done so.
*/
if (sc->sc_rx_drain_thresh == 0) {
/*
* XXX This value should be tuned. This is set to the
* maximum of 248 bytes, and we may be able to improve
* performance by decreasing it (although we should never
* set this value lower than 2; 14 bytes are required to
* filter the packet).
*/
sc->sc_rx_drain_thresh = __SHIFTOUT_MASK(RXCFG_DRTH_MASK);
}
/*
* Initialize the prototype RXCFG register.
*/
sc->sc_rxcfg |= __SHIFTIN(sc->sc_rx_drain_thresh, RXCFG_DRTH_MASK);
/*
* Accept long packets (including FCS) so we can handle
* 802.1q-tagged frames and jumbo frames properly.
*/
if ((sc->sc_gigabit && ifp->if_mtu > ETHERMTU) ||
(sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU))
sc->sc_rxcfg |= RXCFG_ALP;
/*
* Checksum offloading is disabled if the user selects an MTU
* larger than 8109. (FreeBSD says 8152, but there is empirical
* evidence that >8109 does not work on some boards, such as the
* Planex GN-1000TE).
*/
if (sc->sc_gigabit && ifp->if_mtu > 8109 &&
(ifp->if_capenable &
(IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx))) {
printf("%s: Checksum offloading does not work if MTU > 8109 - "
"disabled.\n", device_xname(sc->sc_dev));
ifp->if_capenable &=
~(IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx);
ifp->if_csum_flags_tx = 0;
ifp->if_csum_flags_rx = 0;
}
bus_space_write_4(st, sh, sc->sc_regs.r_rxcfg, sc->sc_rxcfg);
if (sc->sc_gigabit)
sipcom_dp83820_init(sc, ifp->if_capenable);
/*
* Give the transmit and receive rings to the chip.
*/
sip_set_txdp(sc, SIP_CDTXADDR(sc, sc->sc_txnext));
sip_set_rxdp(sc, SIP_CDRXADDR(sc, sc->sc_rxptr));
/*
* Initialize the interrupt mask.
*/
sc->sc_imr = sc->sc_bits.b_isr_dperr |
sc->sc_bits.b_isr_sserr |
sc->sc_bits.b_isr_rmabt |
sc->sc_bits.b_isr_rtabt |
ISR_RXSOVR | ISR_TXURN | ISR_TXDESC | ISR_TXIDLE | ISR_RXORN |
ISR_RXIDLE | ISR_RXDESC;
bus_space_write_4(st, sh, SIP_IMR, sc->sc_imr);
/* Set up the receive filter. */
(*sc->sc_model->sip_variant->sipv_set_filter)(sc);
/*
* Tune sc_rx_flow_thresh.
* XXX "More than 8KB" is too short for jumbo frames.
* XXX TODO: Threshold value should be user-settable.
*/
sc->sc_rx_flow_thresh = (PCR_PS_STHI_8 | PCR_PS_STLO_4 |
PCR_PS_FFHI_8 | PCR_PS_FFLO_4 |
(PCR_PAUSE_CNT & PCR_PAUSE_CNT_MASK));
/*
* Set the current media. Do this after initializing the prototype
* IMR, since sip_mii_statchg() modifies the IMR for 802.3x flow
* control.
*/
if ((error = ether_mediachange(ifp)) != 0)
goto out;
/*
* Set the interrupt hold-off timer to 100us.
*/
if (sc->sc_gigabit)
bus_space_write_4(st, sh, SIP_IHR, 0x01);
/*
* Enable interrupts.
*/
bus_space_write_4(st, sh, SIP_IER, IER_IE);
/*
* Start the transmit and receive processes.
*/
bus_space_write_4(st, sh, SIP_CR, CR_RXE | CR_TXE);
/*
* Start the one second MII clock.
*/
callout_schedule(&sc->sc_tick_ch, hz);
/*
* ...all done!
*/
ifp->if_flags |= IFF_RUNNING;
sc->sc_if_flags = ifp->if_flags;
sc->sc_prev.ec_capenable = sc->sc_ethercom.ec_capenable;
sc->sc_prev.is_vlan = VLAN_ATTACHED(&(sc)->sc_ethercom);
sc->sc_prev.if_capenable = ifp->if_capenable;
out:
if (error)
printf("%s: interface not running\n", device_xname(sc->sc_dev));
return error;
}
/*
* sip_drain:
*
* Drain the receive queue.
*/
static void
sipcom_rxdrain(struct sip_softc *sc)
{
struct sip_rxsoft *rxs;
int i;
for (i = 0; i < sc->sc_parm->p_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;
}
}
}
/*
* sip_stop: [ ifnet interface function ]
*
* Stop transmission on the interface.
*/
static void
sipcom_stop(struct ifnet *ifp, int disable)
{
struct sip_softc *sc = ifp->if_softc;
bus_space_tag_t st = sc->sc_st;
bus_space_handle_t sh = sc->sc_sh;
struct sip_txsoft *txs;
uint32_t cmdsts = 0; /* DEBUG */
/*
* Stop the one second clock.
*/
callout_stop(&sc->sc_tick_ch);
/* Down the MII. */
mii_down(&sc->sc_mii);
if (device_is_active(sc->sc_dev)) {
/*
* Disable interrupts.
*/
bus_space_write_4(st, sh, SIP_IER, 0);
/*
* Stop receiver and transmitter.
*/
bus_space_write_4(st, sh, SIP_CR, CR_RXD | CR_TXD);
}
/*
* Release any queued transmit buffers.
*/
while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
if ((ifp->if_flags & IFF_DEBUG) != 0 &&
SIMPLEQ_NEXT(txs, txs_q) == NULL &&
(sc->sc_txdescs[
txs->txs_lastdesc].sipd_words[
sc->sc_cmdsts_idx] & htole32(CMDSTS_INTR)) == 0)
printf("%s: sip_stop: last descriptor does not "
"have INTR bit set\n", device_xname(sc->sc_dev));
SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
#ifdef DIAGNOSTIC
if (txs->txs_mbuf == NULL) {
printf("%s: dirty txsoft with no mbuf chain\n",
device_xname(sc->sc_dev));
panic("sip_stop");
}
#endif
cmdsts |= /* DEBUG */
le32toh(sc->sc_txdescs[
txs->txs_lastdesc].sipd_words[sc->sc_cmdsts_idx]);
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;
ifp->if_timer = 0;
if (disable)
pmf_device_recursive_suspend(sc->sc_dev, &sc->sc_qual);
if ((ifp->if_flags & IFF_DEBUG) != 0 &&
(cmdsts & CMDSTS_INTR) == 0 && sc->sc_txfree != sc->sc_ntxdesc)
printf("%s: sip_stop: no INTR bits set in dirty tx "
"descriptors\n", device_xname(sc->sc_dev));
}
/*
* sip_read_eeprom:
*
* Read data from the serial EEPROM.
*/
static void
sipcom_read_eeprom(struct sip_softc *sc, int word, int wordcnt,
uint16_t *data)
{
bus_space_tag_t st = sc->sc_st;
bus_space_handle_t sh = sc->sc_sh;
uint16_t reg;
int i, x;
for (i = 0; i < wordcnt; i++) {
/* Send CHIP SELECT. */
reg = EROMAR_EECS;
bus_space_write_4(st, sh, SIP_EROMAR, reg);
/* Shift in the READ opcode. */
for (x = 3; x > 0; x--) {
if (SIP_EEPROM_OPC_READ & (1 << (x - 1)))
reg |= EROMAR_EEDI;
else
reg &= ~EROMAR_EEDI;
bus_space_write_4(st, sh, SIP_EROMAR, reg);
bus_space_write_4(st, sh, SIP_EROMAR,
reg | EROMAR_EESK);
delay(4);
bus_space_write_4(st, sh, SIP_EROMAR, reg);
delay(4);
}
/* Shift in address. */
for (x = 6; x > 0; x--) {
if ((word + i) & (1 << (x - 1)))
reg |= EROMAR_EEDI;
else
reg &= ~EROMAR_EEDI;
bus_space_write_4(st, sh, SIP_EROMAR, reg);
bus_space_write_4(st, sh, SIP_EROMAR,
reg | EROMAR_EESK);
delay(4);
bus_space_write_4(st, sh, SIP_EROMAR, reg);
delay(4);
}
/* Shift out data. */
reg = EROMAR_EECS;
data[i] = 0;
for (x = 16; x > 0; x--) {
bus_space_write_4(st, sh, SIP_EROMAR,
reg | EROMAR_EESK);
delay(4);
if (bus_space_read_4(st, sh, SIP_EROMAR) & EROMAR_EEDO)
data[i] |= (1 << (x - 1));
bus_space_write_4(st, sh, SIP_EROMAR, reg);
delay(4);
}
/* Clear CHIP SELECT. */
bus_space_write_4(st, sh, SIP_EROMAR, 0);
delay(4);
}
}
/*
* sipcom_add_rxbuf:
*
* Add a receive buffer to the indicated descriptor.
*/
static int
sipcom_add_rxbuf(struct sip_softc *sc, int idx)
{
struct sip_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;
}
/* XXX I don't believe this is necessary. --dyoung */
if (sc->sc_gigabit)
m->m_len = sc->sc_parm->p_rxbuf_len;
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("%s", __func__); /* XXX */
}
bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
sip_init_rxdesc(sc, idx);
return 0;
}
/*
* sip_sis900_set_filter:
*
* Set up the receive filter.
*/
static void
sipcom_sis900_set_filter(struct sip_softc *sc)
{
bus_space_tag_t st = sc->sc_st;
bus_space_handle_t sh = sc->sc_sh;
struct ethercom *ec = &sc->sc_ethercom;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct ether_multi *enm;
const uint8_t *cp;
struct ether_multistep step;
uint32_t crc, mchash[16];
/*
* Initialize the prototype RFCR.
*/
sc->sc_rfcr = RFCR_RFEN;
if (ifp->if_flags & IFF_BROADCAST)
sc->sc_rfcr |= RFCR_AAB;
if (ifp->if_flags & IFF_PROMISC) {
sc->sc_rfcr |= RFCR_AAP;
goto allmulti;
}
/*
* Set up the multicast address filter by passing all multicast
* addresses through a CRC generator, and then using the high-order
* 6 bits as an index into the 128 bit multicast hash table (only
* the lower 16 bits of each 32 bit multicast hash register are
* valid). The high order bits select the register, while the
* rest of the bits select the bit within the register.
*/
memset(mchash, 0, sizeof(mchash));
/*
* SiS900 (at least SiS963) requires us to register the address of
* the PAUSE packet (01:80:c2:00:00:01) into the address filter.
*/
crc = 0x0ed423f9;
if (SIP_SIS900_REV(sc, SIS_REV_635) ||
SIP_SIS900_REV(sc, SIS_REV_960) ||
SIP_SIS900_REV(sc, SIS_REV_900B)) {
/* Just want the 8 most significant bits. */
crc >>= 24;
} else {
/* Just want the 7 most significant bits. */
crc >>= 25;
}
/* Set the corresponding bit in the hash table. */
mchash[crc >> 4] |= 1 << (crc & 0xf);
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;
}
crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
if (SIP_SIS900_REV(sc, SIS_REV_635) ||
SIP_SIS900_REV(sc, SIS_REV_960) ||
SIP_SIS900_REV(sc, SIS_REV_900B)) {
/* Just want the 8 most significant bits. */
crc >>= 24;
} else {
/* Just want the 7 most significant bits. */
crc >>= 25;
}
/* Set the corresponding bit in the hash table. */
mchash[crc >> 4] |= 1 << (crc & 0xf);
ETHER_NEXT_MULTI(step, enm);
}
ETHER_UNLOCK(ec);
ifp->if_flags &= ~IFF_ALLMULTI;
goto setit;
allmulti:
ifp->if_flags |= IFF_ALLMULTI;
sc->sc_rfcr |= RFCR_AAM;
setit:
#define FILTER_EMIT(addr, data) \
bus_space_write_4(st, sh, SIP_RFCR, (addr)); \
delay(1); \
bus_space_write_4(st, sh, SIP_RFDR, (data)); \
delay(1)
/*
* Disable receive filter, and program the node address.
*/
cp = CLLADDR(ifp->if_sadl);
FILTER_EMIT(RFCR_RFADDR_NODE0, (cp[1] << 8) | cp[0]);
FILTER_EMIT(RFCR_RFADDR_NODE2, (cp[3] << 8) | cp[2]);
FILTER_EMIT(RFCR_RFADDR_NODE4, (cp[5] << 8) | cp[4]);
if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
/*
* Program the multicast hash table.
*/
FILTER_EMIT(RFCR_RFADDR_MC0, mchash[0]);
FILTER_EMIT(RFCR_RFADDR_MC1, mchash[1]);
FILTER_EMIT(RFCR_RFADDR_MC2, mchash[2]);
FILTER_EMIT(RFCR_RFADDR_MC3, mchash[3]);
FILTER_EMIT(RFCR_RFADDR_MC4, mchash[4]);
FILTER_EMIT(RFCR_RFADDR_MC5, mchash[5]);
FILTER_EMIT(RFCR_RFADDR_MC6, mchash[6]);
FILTER_EMIT(RFCR_RFADDR_MC7, mchash[7]);
if (SIP_SIS900_REV(sc, SIS_REV_635) ||
SIP_SIS900_REV(sc, SIS_REV_960) ||
SIP_SIS900_REV(sc, SIS_REV_900B)) {
FILTER_EMIT(RFCR_RFADDR_MC8, mchash[8]);
FILTER_EMIT(RFCR_RFADDR_MC9, mchash[9]);
FILTER_EMIT(RFCR_RFADDR_MC10, mchash[10]);
FILTER_EMIT(RFCR_RFADDR_MC11, mchash[11]);
FILTER_EMIT(RFCR_RFADDR_MC12, mchash[12]);
FILTER_EMIT(RFCR_RFADDR_MC13, mchash[13]);
FILTER_EMIT(RFCR_RFADDR_MC14, mchash[14]);
FILTER_EMIT(RFCR_RFADDR_MC15, mchash[15]);
}
}
#undef FILTER_EMIT
/*
* Re-enable the receiver filter.
*/
bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
}
/*
* sip_dp83815_set_filter:
*
* Set up the receive filter.
*/
static void
sipcom_dp83815_set_filter(struct sip_softc *sc)
{
bus_space_tag_t st = sc->sc_st;
bus_space_handle_t sh = sc->sc_sh;
struct ethercom *ec = &sc->sc_ethercom;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct ether_multi *enm;
const uint8_t *cp;
struct ether_multistep step;
uint32_t crc, hash, slot, bit;
#define MCHASH_NWORDS_83820 128
#define MCHASH_NWORDS_83815 32
#define MCHASH_NWORDS MAX(MCHASH_NWORDS_83820, MCHASH_NWORDS_83815)
uint16_t mchash[MCHASH_NWORDS];
int i;
/*
* Initialize the prototype RFCR.
* Enable the receive filter, and accept on
* Perfect (destination address) Match
* If IFF_BROADCAST, also accept all broadcast packets.
* If IFF_PROMISC, accept all unicast packets (and later, set
* IFF_ALLMULTI and accept all multicast, too).
*/
sc->sc_rfcr = RFCR_RFEN | RFCR_APM;
if (ifp->if_flags & IFF_BROADCAST)
sc->sc_rfcr |= RFCR_AAB;
if (ifp->if_flags & IFF_PROMISC) {
sc->sc_rfcr |= RFCR_AAP;
goto allmulti;
}
/*
* Set up the DP83820/DP83815 multicast address filter by
* passing all multicast addresses through a CRC generator,
* and then using the high-order 11/9 bits as an index into
* the 2048/512 bit multicast hash table. The high-order
* 7/5 bits select the slot, while the low-order 4 bits
* select the bit within the slot. Note that only the low
* 16-bits of each filter word are used, and there are
* 128/32 filter words.
*/
memset(mchash, 0, sizeof(mchash));
ifp->if_flags &= ~IFF_ALLMULTI;
ETHER_FIRST_MULTI(step, ec, enm);
if (enm == NULL)
goto setit;
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.)
*/
goto allmulti;
}
crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
if (sc->sc_gigabit) {
/* Just want the 11 most significant bits. */
hash = crc >> 21;
} else {
/* Just want the 9 most significant bits. */
hash = crc >> 23;
}
slot = hash >> 4;
bit = hash & 0xf;
/* Set the corresponding bit in the hash table. */
mchash[slot] |= 1 << bit;
ETHER_NEXT_MULTI(step, enm);
}
sc->sc_rfcr |= RFCR_MHEN;
goto setit;
allmulti:
ifp->if_flags |= IFF_ALLMULTI;
sc->sc_rfcr |= RFCR_AAM;
setit:
#define FILTER_EMIT(addr, data) \
bus_space_write_4(st, sh, SIP_RFCR, (addr)); \
delay(1); \
bus_space_write_4(st, sh, SIP_RFDR, (data)); \
delay(1)
/*
* Disable receive filter, and program the node address.
*/
cp = CLLADDR(ifp->if_sadl);
FILTER_EMIT(RFCR_NS_RFADDR_PMATCH0, (cp[1] << 8) | cp[0]);
FILTER_EMIT(RFCR_NS_RFADDR_PMATCH2, (cp[3] << 8) | cp[2]);
FILTER_EMIT(RFCR_NS_RFADDR_PMATCH4, (cp[5] << 8) | cp[4]);
if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
int nwords =
sc->sc_gigabit ? MCHASH_NWORDS_83820 : MCHASH_NWORDS_83815;
/*
* Program the multicast hash table.
*/
for (i = 0; i < nwords; i++) {
FILTER_EMIT(sc->sc_parm->p_filtmem + (i * 2), mchash[i]);
}
}
#undef FILTER_EMIT
#undef MCHASH_NWORDS
#undef MCHASH_NWORDS_83815
#undef MCHASH_NWORDS_83820
/*
* Re-enable the receiver filter.
*/
bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
}
/*
* sip_dp83820_mii_readreg: [mii interface function]
*
* Read a PHY register on the MII of the DP83820.
*/
static int
sipcom_dp83820_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
{
struct sip_softc *sc = device_private(self);
if (sc->sc_cfg & CFG_TBI_EN) {
bus_addr_t tbireg;
if (phy != 0)
return -1;
switch (reg) {
case MII_BMCR: tbireg = SIP_TBICR; break;
case MII_BMSR: tbireg = SIP_TBISR; break;
case MII_ANAR: tbireg = SIP_TANAR; break;
case MII_ANLPAR: tbireg = SIP_TANLPAR; break;
case MII_ANER: tbireg = SIP_TANER; break;
case MII_EXTSR:
/*
* Don't even bother reading the TESR register.
* The manual documents that the device has
* 1000baseX full/half capability, but the
* register itself seems read back 0 on some
* boards. Just hard-code the result.
*/
*val = (EXTSR_1000XFDX | EXTSR_1000XHDX);
return 0;
default:
return 0;
}
*val = bus_space_read_4(sc->sc_st, sc->sc_sh, tbireg) & 0xffff;
if (tbireg == SIP_TBISR) {
/* LINK and ACOMP are switched! */
int sr = *val;
*val = 0;
if (sr & TBISR_MR_LINK_STATUS)
*val |= BMSR_LINK;
if (sr & TBISR_MR_AN_COMPLETE)
*val |= BMSR_ACOMP;
/*
* The manual claims this register reads back 0
* on hard and soft reset. But we want to let
* the gentbi driver know that we support auto-
* negotiation, so hard-code this bit in the
* result.
*/
*val |= BMSR_ANEG | BMSR_EXTSTAT;
}
return 0;
}
return mii_bitbang_readreg(self, &sipcom_mii_bitbang_ops, phy, reg,
val);
}
/*
* sip_dp83820_mii_writereg: [mii interface function]
*
* Write a PHY register on the MII of the DP83820.
*/
static int
sipcom_dp83820_mii_writereg(device_t self, int phy, int reg, uint16_t val)
{
struct sip_softc *sc = device_private(self);
if (sc->sc_cfg & CFG_TBI_EN) {
bus_addr_t tbireg;
if (phy != 0)
return -1;
switch (reg) {
case MII_BMCR: tbireg = SIP_TBICR; break;
case MII_ANAR: tbireg = SIP_TANAR; break;
case MII_ANLPAR: tbireg = SIP_TANLPAR; break;
default:
return 0;
}
bus_space_write_4(sc->sc_st, sc->sc_sh, tbireg, val);
return 0;
}
return mii_bitbang_writereg(self, &sipcom_mii_bitbang_ops, phy, reg,
val);
}
/*
* sip_dp83820_mii_statchg: [mii interface function]
*
* Callback from MII layer when media changes.
*/
static void
sipcom_dp83820_mii_statchg(struct ifnet *ifp)
{
struct sip_softc *sc = ifp->if_softc;
struct mii_data *mii = &sc->sc_mii;
uint32_t cfg, pcr;
/*
* Get flow control negotiation result.
*/
if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO &&
(mii->mii_media_active & IFM_ETH_FMASK) != sc->sc_flowflags) {
sc->sc_flowflags = mii->mii_media_active & IFM_ETH_FMASK;
mii->mii_media_active &= ~IFM_ETH_FMASK;
}
/*
* Update TXCFG for full-duplex operation.
*/
if ((mii->mii_media_active & IFM_FDX) != 0)
sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
else
sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
/*
* Update RXCFG for full-duplex or loopback.
*/
if ((mii->mii_media_active & IFM_FDX) != 0 ||
IFM_SUBTYPE(mii->mii_media_active) == IFM_LOOP)
sc->sc_rxcfg |= RXCFG_ATX;
else
sc->sc_rxcfg &= ~RXCFG_ATX;
/*
* Update CFG for MII/GMII.
*/
if (sc->sc_ethercom.ec_if.if_baudrate == IF_Mbps(1000))
cfg = sc->sc_cfg | CFG_MODE_1000;
else
cfg = sc->sc_cfg;
/*
* 802.3x flow control.
*/
pcr = 0;
if (sc->sc_flowflags & IFM_FLOW) {
if (sc->sc_flowflags & IFM_ETH_TXPAUSE)
pcr |= sc->sc_rx_flow_thresh;
if (sc->sc_flowflags & IFM_ETH_RXPAUSE)
pcr |= PCR_PSEN | PCR_PS_MCAST;
}
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CFG, cfg);
bus_space_write_4(sc->sc_st, sc->sc_sh, sc->sc_regs.r_txcfg,
sc->sc_txcfg);
bus_space_write_4(sc->sc_st, sc->sc_sh, sc->sc_regs.r_rxcfg,
sc->sc_rxcfg);
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_NS_PCR, pcr);
}
/*
* sip_mii_bitbang_read: [mii bit-bang interface function]
*
* Read the MII serial port for the MII bit-bang module.
*/
static uint32_t
sipcom_mii_bitbang_read(device_t self)
{
struct sip_softc *sc = device_private(self);
return (bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_EROMAR));
}
/*
* sip_mii_bitbang_write: [mii big-bang interface function]
*
* Write the MII serial port for the MII bit-bang module.
*/
static void
sipcom_mii_bitbang_write(device_t self, uint32_t val)
{
struct sip_softc *sc = device_private(self);
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_EROMAR, val);
}
/*
* sip_sis900_mii_readreg: [mii interface function]
*
* Read a PHY register on the MII.
*/
static int
sipcom_sis900_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
{
struct sip_softc *sc = device_private(self);
uint32_t enphy;
/*
* The PHY of recent SiS chipsets is accessed through bitbang
* operations.
*/
if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900)
return mii_bitbang_readreg(self, &sipcom_mii_bitbang_ops,
phy, reg, val);
#ifndef SIS900_MII_RESTRICT
/*
* The SiS 900 has only an internal PHY on the MII. Only allow
* MII address 0.
*/
if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900 && phy != 0)
return -1;
#endif
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
(phy << ENPHY_PHYADDR_SHIFT) | (reg << ENPHY_REGADDR_SHIFT) |
ENPHY_RWCMD | ENPHY_ACCESS);
do {
enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
} while (enphy & ENPHY_ACCESS);
*val = (enphy & ENPHY_PHYDATA) >> ENPHY_DATA_SHIFT;
return 0;
}
/*
* sip_sis900_mii_writereg: [mii interface function]
*
* Write a PHY register on the MII.
*/
static int
sipcom_sis900_mii_writereg(device_t self, int phy, int reg, uint16_t val)
{
struct sip_softc *sc = device_private(self);
uint32_t enphy;
if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900) {
return mii_bitbang_writereg(self, &sipcom_mii_bitbang_ops,
phy, reg, val);
}
#ifndef SIS900_MII_RESTRICT
/*
* The SiS 900 has only an internal PHY on the MII. Only allow
* MII address 0.
*/
if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900 && phy != 0)
return -1;
#endif
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
(val << ENPHY_DATA_SHIFT) | (phy << ENPHY_PHYADDR_SHIFT) |
(reg << ENPHY_REGADDR_SHIFT) | ENPHY_ACCESS);
do {
enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
} while (enphy & ENPHY_ACCESS);
return 0;
}
/*
* sip_sis900_mii_statchg: [mii interface function]
*
* Callback from MII layer when media changes.
*/
static void
sipcom_sis900_mii_statchg(struct ifnet *ifp)
{
struct sip_softc *sc = ifp->if_softc;
struct mii_data *mii = &sc->sc_mii;
uint32_t flowctl;
/*
* Get flow control negotiation result.
*/
if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO &&
(mii->mii_media_active & IFM_ETH_FMASK) != sc->sc_flowflags) {
sc->sc_flowflags = mii->mii_media_active & IFM_ETH_FMASK;
mii->mii_media_active &= ~IFM_ETH_FMASK;
}
/*
* Update TXCFG for full-duplex operation.
*/
if ((mii->mii_media_active & IFM_FDX) != 0)
sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
else
sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
/*
* Update RXCFG for full-duplex or loopback.
*/
if ((mii->mii_media_active & IFM_FDX) != 0 ||
IFM_SUBTYPE(mii->mii_media_active) == IFM_LOOP)
sc->sc_rxcfg |= RXCFG_ATX;
else
sc->sc_rxcfg &= ~RXCFG_ATX;
/*
* Update IMR for use of 802.3x flow control.
*/
if (sc->sc_flowflags & IFM_FLOW) {
sc->sc_imr |= (ISR_PAUSE_END | ISR_PAUSE_ST);
flowctl = FLOWCTL_FLOWEN;
} else {
sc->sc_imr &= ~(ISR_PAUSE_END | ISR_PAUSE_ST);
flowctl = 0;
}
bus_space_write_4(sc->sc_st, sc->sc_sh, sc->sc_regs.r_txcfg,
sc->sc_txcfg);
bus_space_write_4(sc->sc_st, sc->sc_sh, sc->sc_regs.r_rxcfg,
sc->sc_rxcfg);
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_IMR, sc->sc_imr);
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_FLOWCTL, flowctl);
}
/*
* sip_dp83815_mii_readreg: [mii interface function]
*
* Read a PHY register on the MII.
*/
static int
sipcom_dp83815_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
{
struct sip_softc *sc = device_private(self);
uint32_t data;
/*
* The DP83815 only has an internal PHY. Only allow
* MII address 0.
*/
if (phy != 0)
return -1;
/*
* Apparently, after a reset, the DP83815 can take a while
* to respond. During this recovery period, the BMSR returns
* a value of 0. Catch this -- it's not supposed to happen
* (the BMSR has some hardcoded-to-1 bits), and wait for the
* PHY to come back to life.
*
* This works out because the BMSR is the first register
* read during the PHY probe process.
*/
do {
data = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_NS_PHY(reg));
} while (reg == MII_BMSR && data == 0);
*val = data & 0xffff;
return 0;
}
/*
* sip_dp83815_mii_writereg: [mii interface function]
*
* Write a PHY register to the MII.
*/
static int
sipcom_dp83815_mii_writereg(device_t self, int phy, int reg, uint16_t val)
{
struct sip_softc *sc = device_private(self);
/*
* The DP83815 only has an internal PHY. Only allow
* MII address 0.
*/
if (phy != 0)
return -1;
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_NS_PHY(reg), val);
return 0;
}
/*
* sip_dp83815_mii_statchg: [mii interface function]
*
* Callback from MII layer when media changes.
*/
static void
sipcom_dp83815_mii_statchg(struct ifnet *ifp)
{
struct sip_softc *sc = ifp->if_softc;
/*
* Update TXCFG for full-duplex operation.
*/
if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
else
sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
/*
* Update RXCFG for full-duplex or loopback.
*/
if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
sc->sc_rxcfg |= RXCFG_ATX;
else
sc->sc_rxcfg &= ~RXCFG_ATX;
/*
* XXX 802.3x flow control.
*/
bus_space_write_4(sc->sc_st, sc->sc_sh, sc->sc_regs.r_txcfg,
sc->sc_txcfg);
bus_space_write_4(sc->sc_st, sc->sc_sh, sc->sc_regs.r_rxcfg,
sc->sc_rxcfg);
/*
* Some DP83815s experience problems when used with short
* (< 30m/100ft) Ethernet cables in 100BaseTX mode. This
* sequence adjusts the DSP's signal attenuation to fix the
* problem.
*/
if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX) {
uint32_t reg;
bus_space_write_4(sc->sc_st, sc->sc_sh, 0x00cc, 0x0001);
reg = bus_space_read_4(sc->sc_st, sc->sc_sh, 0x00f4);
reg &= 0x0fff;
bus_space_write_4(sc->sc_st, sc->sc_sh, 0x00f4, reg | 0x1000);
delay(100);
reg = bus_space_read_4(sc->sc_st, sc->sc_sh, 0x00fc);
reg &= 0x00ff;
if ((reg & 0x0080) == 0 || (reg >= 0x00d8)) {
bus_space_write_4(sc->sc_st, sc->sc_sh, 0x00fc,
0x00e8);
reg = bus_space_read_4(sc->sc_st, sc->sc_sh, 0x00f4);
bus_space_write_4(sc->sc_st, sc->sc_sh, 0x00f4,
reg | 0x20);
}
bus_space_write_4(sc->sc_st, sc->sc_sh, 0x00cc, 0);
}
}
static void
sipcom_dp83820_read_macaddr(struct sip_softc *sc,
const struct pci_attach_args *pa, uint8_t *enaddr)
{
uint16_t eeprom_data[SIP_DP83820_EEPROM_LENGTH / 2];
uint8_t cksum, *e, match;
int i;
/*
* EEPROM data format for the DP83820 can be found in
* the DP83820 manual, section 4.2.4.
*/
sipcom_read_eeprom(sc, 0, __arraycount(eeprom_data), eeprom_data);
match = eeprom_data[SIP_DP83820_EEPROM_CHECKSUM / 2] >> 8;
match = ~(match - 1);
cksum = 0x55;
e = (uint8_t *)eeprom_data;
for (i = 0; i < SIP_DP83820_EEPROM_CHECKSUM; i++)
cksum += *e++;
if (cksum != match)
printf("%s: Checksum (%x) mismatch (%x)",
device_xname(sc->sc_dev), cksum, match);
enaddr[0] = eeprom_data[SIP_DP83820_EEPROM_PMATCH2 / 2] & 0xff;
enaddr[1] = eeprom_data[SIP_DP83820_EEPROM_PMATCH2 / 2] >> 8;
enaddr[2] = eeprom_data[SIP_DP83820_EEPROM_PMATCH1 / 2] & 0xff;
enaddr[3] = eeprom_data[SIP_DP83820_EEPROM_PMATCH1 / 2] >> 8;
enaddr[4] = eeprom_data[SIP_DP83820_EEPROM_PMATCH0 / 2] & 0xff;
enaddr[5] = eeprom_data[SIP_DP83820_EEPROM_PMATCH0 / 2] >> 8;
}
static void
sipcom_sis900_eeprom_delay(struct sip_softc *sc)
{
int i;
/*
* FreeBSD goes from (300/33)+1 [10] to 0. There must be
* a reason, but I don't know it.
*/
for (i = 0; i < 10; i++)
bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_CR);
}
static void
sipcom_sis900_read_macaddr(struct sip_softc *sc,
const struct pci_attach_args *pa, uint8_t *enaddr)
{
uint16_t myea[ETHER_ADDR_LEN / 2];
switch (sc->sc_rev) {
case SIS_REV_630S:
case SIS_REV_630E:
case SIS_REV_630EA1:
case SIS_REV_630ET:
case SIS_REV_635:
/*
* The MAC address for the on-board Ethernet of
* the SiS 630 chipset is in the NVRAM. Kick
* the chip into re-loading it from NVRAM, and
* read the MAC address out of the filter registers.
*/
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CR, CR_RLD);
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RFCR,
RFCR_RFADDR_NODE0);
myea[0] = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_RFDR) &
0xffff;
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RFCR,
RFCR_RFADDR_NODE2);
myea[1] = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_RFDR) &
0xffff;
bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RFCR,
RFCR_RFADDR_NODE4);
myea[2] = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_RFDR) &
0xffff;
break;
case SIS_REV_960:
{
#define SIS_SET_EROMAR(x, y) \
bus_space_write_4(x->sc_st, x->sc_sh, SIP_EROMAR, \
bus_space_read_4(x->sc_st, x->sc_sh, SIP_EROMAR) | (y))
#define SIS_CLR_EROMAR(x, y) \
bus_space_write_4(x->sc_st, x->sc_sh, SIP_EROMAR, \
bus_space_read_4(x->sc_st, x->sc_sh, SIP_EROMAR) & ~(y))
int waittime, i;
/* Allow to read EEPROM from LAN. It is shared
* between a 1394 controller and the NIC and each
* time we access it, we need to set SIS_EECMD_REQ.
*/
SIS_SET_EROMAR(sc, EROMAR_REQ);
for (waittime = 0; waittime < 1000; waittime++) { /* 1 ms max */
/* Force EEPROM to idle state. */
/*
* XXX-cube This is ugly.
* I'll look for docs about it.
*/
SIS_SET_EROMAR(sc, EROMAR_EECS);
sipcom_sis900_eeprom_delay(sc);
for (i = 0; i <= 25; i++) { /* Yes, 26 times. */
SIS_SET_EROMAR(sc, EROMAR_EESK);
sipcom_sis900_eeprom_delay(sc);
SIS_CLR_EROMAR(sc, EROMAR_EESK);
sipcom_sis900_eeprom_delay(sc);
}
SIS_CLR_EROMAR(sc, EROMAR_EECS);
sipcom_sis900_eeprom_delay(sc);
bus_space_write_4(sc->sc_st, sc->sc_sh,
SIP_EROMAR, 0);
if (bus_space_read_4(sc->sc_st, sc->sc_sh,
SIP_EROMAR) & EROMAR_GNT) {
sipcom_read_eeprom(sc,
SIP_EEPROM_ETHERNET_ID0 >> 1,
sizeof(myea) / sizeof(myea[0]),
myea);
break;
}
DELAY(1);
}
/*
* Set SIS_EECTL_CLK to high, so a other master
* can operate on the i2c bus.
*/
SIS_SET_EROMAR(sc, EROMAR_EESK);
/* Refuse EEPROM access by LAN */
SIS_SET_EROMAR(sc, EROMAR_DONE);
} break;
default:
sipcom_read_eeprom(sc, SIP_EEPROM_ETHERNET_ID0 >> 1,
sizeof(myea) / sizeof(myea[0]), myea);
}
enaddr[0] = myea[0] & 0xff;
enaddr[1] = myea[0] >> 8;
enaddr[2] = myea[1] & 0xff;
enaddr[3] = myea[1] >> 8;
enaddr[4] = myea[2] & 0xff;
enaddr[5] = myea[2] >> 8;
}
/* Table and macro to bit-reverse an octet. */
static const uint8_t bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
#define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
static void
sipcom_dp83815_read_macaddr(struct sip_softc *sc,
const struct pci_attach_args *pa, uint8_t *enaddr)
{
uint16_t eeprom_data[SIP_DP83815_EEPROM_LENGTH / 2], *ea;
uint8_t cksum, *e, match;
int i;
sipcom_read_eeprom(sc, 0, sizeof(eeprom_data) /
sizeof(eeprom_data[0]), eeprom_data);
match = eeprom_data[SIP_DP83815_EEPROM_CHECKSUM/2] >> 8;
match = ~(match - 1);
cksum = 0x55;
e = (uint8_t *)eeprom_data;
for (i = 0; i < SIP_DP83815_EEPROM_CHECKSUM; i++)
cksum += *e++;
if (cksum != match)
printf("%s: Checksum (%x) mismatch (%x)",
device_xname(sc->sc_dev), cksum, match);
/*
* Unrolled because it makes slightly more sense this way.
* The DP83815 stores the MAC address in bit 0 of word 6
* through bit 15 of word 8.
*/
ea = &eeprom_data[6];
enaddr[0] = ((*ea & 0x1) << 7);
ea++;
enaddr[0] |= ((*ea & 0xFE00) >> 9);
enaddr[1] = ((*ea & 0x1FE) >> 1);
enaddr[2] = ((*ea & 0x1) << 7);
ea++;
enaddr[2] |= ((*ea & 0xFE00) >> 9);
enaddr[3] = ((*ea & 0x1FE) >> 1);
enaddr[4] = ((*ea & 0x1) << 7);
ea++;
enaddr[4] |= ((*ea & 0xFE00) >> 9);
enaddr[5] = ((*ea & 0x1FE) >> 1);
/*
* In case that's not weird enough, we also need to reverse
* the bits in each byte. This all actually makes more sense
* if you think about the EEPROM storage as an array of bits
* being shifted into bytes, but that's not how we're looking
* at it here...
*/
for (i = 0; i < 6 ;i++)
enaddr[i] = bbr(enaddr[i]);
}
/*
* sip_mediastatus: [ifmedia interface function]
*
* Get the current interface media status.
*/
static void
sipcom_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
{
struct sip_softc *sc = ifp->if_softc;
if (!device_is_active(sc->sc_dev)) {
ifmr->ifm_active = IFM_ETHER | IFM_NONE;
ifmr->ifm_status = 0;
return;
}
ether_mediastatus(ifp, ifmr);
ifmr->ifm_active = (ifmr->ifm_active & ~IFM_ETH_FMASK) |
sc->sc_flowflags;
}
|