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
|
/* $OpenBSD: ath.c,v 1.23 2005/04/18 18:42:55 reyk Exp $ */
/* $NetBSD: ath.c,v 1.37 2004/08/18 21:59:39 dyoung Exp $ */
/*-
* Copyright (c) 2002-2004 Sam Leffler, Errno Consulting
* 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,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
* redistribution must be conditioned upon including a substantially
* similar Disclaimer requirement for further binary redistribution.
* 3. Neither the names of the above-listed copyright holders nor the names
* of any contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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 DAMAGES.
*/
/*
* Driver for the Atheros Wireless LAN controller.
*
* This software is derived from work of Atsushi Onoe; his contribution
* is greatly appreciated. It has been modified for OpenBSD to use an
* open source HAL instead of the original binary-only HAL.
*/
#include "bpfilter.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/lock.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/timeout.h>
#include <sys/gpio.h>
#include <machine/endian.h>
#include <machine/bus.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_arp.h>
#include <net/if_llc.h>
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_ether.h>
#endif
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_compat.h>
#include <dev/gpio/gpiovar.h>
#include <dev/ic/athvar.h>
int ath_init(struct ifnet *);
int ath_init1(struct ath_softc *);
int ath_intr1(struct ath_softc *);
void ath_stop(struct ifnet *);
void ath_start(struct ifnet *);
void ath_reset(struct ath_softc *);
int ath_media_change(struct ifnet *);
void ath_watchdog(struct ifnet *);
int ath_ioctl(struct ifnet *, u_long, caddr_t);
void ath_fatal_proc(void *, int);
void ath_rxorn_proc(void *, int);
void ath_bmiss_proc(void *, int);
u_int ath_chan2flags(struct ieee80211com *, struct ieee80211_channel *);
int ath_initkeytable(struct ath_softc *);
void ath_mcastfilter_accum(caddr_t, u_int32_t (*)[2]);
void ath_mcastfilter_compute(struct ath_softc *, u_int32_t (*)[2]);
u_int32_t ath_calcrxfilter(struct ath_softc *);
void ath_mode_init(struct ath_softc *);
int ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
void ath_beacon_proc(void *, int);
void ath_beacon_free(struct ath_softc *);
void ath_beacon_config(struct ath_softc *);
int ath_desc_alloc(struct ath_softc *);
void ath_desc_free(struct ath_softc *);
struct ieee80211_node *ath_node_alloc(struct ieee80211com *);
struct mbuf *ath_getmbuf(int, int, u_int);
void ath_node_free(struct ieee80211com *, struct ieee80211_node *);
void ath_node_copy(struct ieee80211com *,
struct ieee80211_node *, const struct ieee80211_node *);
u_int8_t ath_node_getrssi(struct ieee80211com *,
struct ieee80211_node *);
int ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
void ath_rx_proc(void *, int);
int ath_tx_start(struct ath_softc *, struct ieee80211_node *,
struct ath_buf *, struct mbuf *);
void ath_tx_proc(void *, int);
int ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
void ath_draintxq(struct ath_softc *);
void ath_stoprecv(struct ath_softc *);
int ath_startrecv(struct ath_softc *);
void ath_next_scan(void *);
void ath_calibrate(void *);
HAL_LED_STATE ath_state_to_led(enum ieee80211_state);
int ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
void ath_newassoc(struct ieee80211com *,
struct ieee80211_node *, int);
int ath_getchannels(struct ath_softc *, u_int cc, HAL_BOOL outdoor,
HAL_BOOL xchanmode);
int ath_rate_setup(struct ath_softc *sc, u_int mode);
void ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
void ath_rate_ctl_reset(struct ath_softc *, enum ieee80211_state);
void ath_rate_ctl(void *, struct ieee80211_node *);
void ath_recv_mgmt(struct ieee80211com *, struct mbuf *,
struct ieee80211_node *, int, int, u_int32_t);
int ath_enable(struct ath_softc *);
void ath_disable(struct ath_softc *);
void ath_power(int, void *);
#if NGPIO > 0
int ath_gpio_attach(struct ath_softc *);
int ath_gpio_pin_read(void *, int);
void ath_gpio_pin_write(void *, int, int);
void ath_gpio_pin_ctl(void *, int, int);
#endif
#ifdef AR_DEBUG
void ath_printrxbuf(struct ath_buf *, int);
void ath_printtxbuf(struct ath_buf *, int);
int ath_debug = ATH_DEBUG_ANY;
#endif
int ath_dwelltime = 200; /* 5 channels/second */
int ath_calinterval = 30; /* calibrate every 30 secs */
int ath_outdoor = AH_TRUE; /* outdoor operation */
int ath_xchanmode = AH_TRUE; /* enable extended channels */
int ath_countrycode = CTRY_DEFAULT; /* country code */
int ath_regdomain = DMN_DEFAULT; /* regulatory domain */
struct cfdriver ath_cd = {
NULL, "ath", DV_IFNET
};
#if 0
int
ath_activate(struct device *self, enum devact act)
{
struct ath_softc *sc = (struct ath_softc *)self;
int rv = 0, s;
s = splnet();
switch (act) {
case DVACT_ACTIVATE:
rv = EOPNOTSUPP;
break;
case DVACT_DEACTIVATE:
if_deactivate(&sc->sc_ic.ic_if);
break;
}
splx(s);
return rv;
}
#endif
int
ath_enable(struct ath_softc *sc)
{
if (ATH_IS_ENABLED(sc) == 0) {
if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
printf("%s: device enable failed\n",
sc->sc_dev.dv_xname);
return (EIO);
}
sc->sc_flags |= ATH_ENABLED;
}
return (0);
}
void
ath_disable(struct ath_softc *sc)
{
if (!ATH_IS_ENABLED(sc))
return;
if (sc->sc_disable != NULL)
(*sc->sc_disable)(sc);
sc->sc_flags &= ~ATH_ENABLED;
}
int
ath_attach(u_int16_t devid, struct ath_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct ath_hal *ah;
HAL_STATUS status;
HAL_TXQ_INFO qinfo;
int error = 0, i = 0;
DPRINTF(ATH_DEBUG_ANY, ("%s: devid 0x%x\n", __func__, devid));
bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
if (ah == NULL) {
if_printf(ifp, "unable to attach hardware; HAL status %d\n",
status);
error = ENXIO;
goto bad;
}
if (ah->ah_abi != HAL_ABI_VERSION) {
if_printf(ifp, "HAL ABI mismatch detected (0x%x != 0x%x)\n",
ah->ah_abi, HAL_ABI_VERSION);
error = ENXIO;
goto bad;
}
if_printf(ifp, "mac %d.%d phy %d.%d",
ah->ah_macVersion, ah->ah_macRev,
ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4,
ah->ah_analog5GhzRev & 0xf);
if (ah->ah_analog2GhzRev != 0) {
printf(" %d.%d", ah->ah_analog2GhzRev >> 4,
ah->ah_analog2GhzRev & 0xf);
}
#define ah_mode(_m) { \
if (i++) { \
printf("/%s", #_m); \
} else { \
printf(", 802.11%s", #_m); \
} \
} \
if (ah->ah_modes & HAL_MODE_11A)
ah_mode(a);
if (ah->ah_modes & HAL_MODE_11B)
ah_mode(b);
if (ah->ah_modes & HAL_MODE_11G)
ah_mode(g);
#undef ah_mode
sc->sc_ah = ah;
sc->sc_invalid = 0; /* ready to go, enable interrupt handling */
/*
* Collect the channel list using the default country
* code and including outdoor channels. The 802.11 layer
* is resposible for filtering this list based on settings
* like the phy mode.
*/
error = ath_getchannels(sc, ath_countrycode, ath_outdoor,
ath_xchanmode);
if (error != 0)
goto bad;
/*
* Copy these back; they are set as a side effect
* of constructing the channel list.
*/
ath_hal_get_regdomain(ah, &ath_regdomain);
ath_hal_getcountrycode(ah, &ath_countrycode);
/*
* Setup rate tables for all potential media types.
*/
ath_rate_setup(sc, IEEE80211_MODE_11A);
ath_rate_setup(sc, IEEE80211_MODE_11B);
ath_rate_setup(sc, IEEE80211_MODE_11G);
ath_rate_setup(sc, IEEE80211_MODE_TURBO);
error = ath_desc_alloc(sc);
if (error != 0) {
if_printf(ifp, "failed to allocate descriptors: %d\n", error);
goto bad;
}
timeout_set(&sc->sc_scan_to, ath_next_scan, sc);
timeout_set(&sc->sc_cal_to, ath_calibrate, sc);
#ifdef __FreeBSD__
ATH_TXBUF_LOCK_INIT(sc);
ATH_TXQ_LOCK_INIT(sc);
#endif
ATH_TASK_INIT(&sc->sc_txtask, ath_tx_proc, sc);
ATH_TASK_INIT(&sc->sc_rxtask, ath_rx_proc, sc);
ATH_TASK_INIT(&sc->sc_rxorntask, ath_rxorn_proc, sc);
ATH_TASK_INIT(&sc->sc_fataltask, ath_fatal_proc, sc);
ATH_TASK_INIT(&sc->sc_bmisstask, ath_bmiss_proc, sc);
ATH_TASK_INIT(&sc->sc_swbatask, ath_beacon_proc, sc);
/*
* For now just pre-allocate one data queue and one
* beacon queue. Note that the HAL handles resetting
* them at the needed time. Eventually we'll want to
* allocate more tx queues for splitting management
* frames and for QOS support.
*/
sc->sc_bhalq = ath_hal_setup_tx_queue(ah,HAL_TX_QUEUE_BEACON,NULL);
if (sc->sc_bhalq == (u_int) -1) {
if_printf(ifp, "unable to setup a beacon xmit queue!\n");
goto bad2;
}
bzero(&qinfo, sizeof(qinfo));
qinfo.tqi_subtype = HAL_WME_AC_BE;
sc->sc_txhalq = ath_hal_setup_tx_queue(ah, HAL_TX_QUEUE_DATA, &qinfo);
if (sc->sc_txhalq == (u_int) -1) {
if_printf(ifp, "unable to setup a data xmit queue!\n");
goto bad2;
}
ifp->if_softc = sc;
ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST
| IFF_NOTRAILERS;
ifp->if_start = ath_start;
ifp->if_watchdog = ath_watchdog;
ifp->if_ioctl = ath_ioctl;
#ifndef __OpenBSD__
ifp->if_init = ath_init;
ifp->if_stop = ath_stop; /* XXX */
#endif
IFQ_SET_MAXLEN(&ifp->if_snd, ATH_TXBUF * ATH_TXDESC);
IFQ_SET_READY(&ifp->if_snd);
ic->ic_softc = sc;
ic->ic_newassoc = ath_newassoc;
/* XXX not right but it's not used anywhere important */
ic->ic_phytype = IEEE80211_T_OFDM;
ic->ic_opmode = IEEE80211_M_STA;
ic->ic_caps = IEEE80211_C_WEP /* wep supported */
| IEEE80211_C_PMGT /* power management */
| IEEE80211_C_IBSS /* ibss, nee adhoc, mode */
| IEEE80211_C_HOSTAP /* hostap mode */
| IEEE80211_C_MONITOR /* monitor mode */
| IEEE80211_C_SHPREAMBLE; /* short preamble supported */
/*
* Not all chips have the VEOL support we want to use with
* IBSS beacon; check here for it.
*/
sc->sc_has_veol = ath_hal_has_veol(ah);
/* get mac address from hardware */
ath_hal_get_lladdr(ah, ic->ic_myaddr);
if_attach(ifp);
/* call MI attach routine. */
ieee80211_ifattach(ifp);
/* override default methods */
ic->ic_node_alloc = ath_node_alloc;
sc->sc_node_free = ic->ic_node_free;
ic->ic_node_free = ath_node_free;
sc->sc_node_copy = ic->ic_node_copy;
ic->ic_node_copy = ath_node_copy;
ic->ic_node_getrssi = ath_node_getrssi;
sc->sc_newstate = ic->ic_newstate;
ic->ic_newstate = ath_newstate;
sc->sc_recv_mgmt = ic->ic_recv_mgmt;
ic->ic_recv_mgmt = ath_recv_mgmt;
bcopy(etherbroadcastaddr, sc->sc_broadcast_addr, IEEE80211_ADDR_LEN);
/* complete initialization */
ieee80211_media_init(ifp, ath_media_change, ieee80211_media_status);
#if NBPFILTER > 0
bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
sc->sc_rxtap_len = sizeof(sc->sc_rxtapu);
bzero(&sc->sc_rxtapu, sc->sc_rxtap_len);
sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
sc->sc_rxtap.wr_ihdr.it_present = htole32(ATH_RX_RADIOTAP_PRESENT);
sc->sc_txtap_len = sizeof(sc->sc_txtapu);
bzero(&sc->sc_txtapu, sc->sc_txtap_len);
sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
sc->sc_txtap.wt_ihdr.it_present = htole32(ATH_TX_RADIOTAP_PRESENT);
#endif
sc->sc_flags |= ATH_ATTACHED;
/*
* Make sure the interface is shutdown during reboot.
*/
sc->sc_sdhook = shutdownhook_establish(ath_shutdown, sc);
if (sc->sc_sdhook == NULL) {
printf("%s: WARNING: unable to establish shutdown hook\n",
sc->sc_dev.dv_xname);
}
sc->sc_powerhook = powerhook_establish(ath_power, sc);
if (sc->sc_powerhook == NULL) {
printf("%s: WARNING: unable to establish power hook\n",
sc->sc_dev.dv_xname);
}
printf(", %s, address %s\n", ieee80211_regdomain2name(ath_regdomain),
ether_sprintf(ic->ic_myaddr));
#if NGPIO > 0
if (ath_gpio_attach(sc) == 0)
sc->sc_flags |= ATH_GPIO;
#endif
return 0;
bad2:
ath_desc_free(sc);
bad:
if (ah)
ath_hal_detach(ah);
sc->sc_invalid = 1;
return error;
}
int
ath_detach(struct ath_softc *sc, int flags)
{
struct ifnet *ifp = &sc->sc_ic.ic_if;
int s;
config_detach_children(&sc->sc_dev, flags);
if ((sc->sc_flags & ATH_ATTACHED) == 0)
return (0);
DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags %x\n", __func__, ifp->if_flags));
s = splnet();
ath_stop(ifp);
#if NBPFILTER > 0
bpfdetach(ifp);
#endif
ath_desc_free(sc);
ath_hal_detach(sc->sc_ah);
ieee80211_ifdetach(ifp);
if_detach(ifp);
splx(s);
powerhook_disestablish(sc->sc_powerhook);
shutdownhook_disestablish(sc->sc_sdhook);
#ifdef __FreeBSD__
ATH_TXBUF_LOCK_DESTROY(sc);
ATH_TXQ_LOCK_DESTROY(sc);
#endif
return 0;
}
void
ath_power(int why, void *arg)
{
struct ath_softc *sc = arg;
int s;
DPRINTF(ATH_DEBUG_ANY, ("ath_power(%d)\n", why));
s = splnet();
switch (why) {
case PWR_SUSPEND:
case PWR_STANDBY:
ath_suspend(sc, why);
break;
case PWR_RESUME:
ath_resume(sc, why);
break;
#if !defined(__OpenBSD__)
case PWR_SOFTSUSPEND:
case PWR_SOFTSTANDBY:
case PWR_SOFTRESUME:
break;
#endif
}
splx(s);
}
void
ath_suspend(struct ath_softc *sc, int why)
{
struct ifnet *ifp = &sc->sc_ic.ic_if;
DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags %x\n", __func__, ifp->if_flags));
ath_stop(ifp);
if (sc->sc_power != NULL)
(*sc->sc_power)(sc, why);
}
void
ath_resume(struct ath_softc *sc, int why)
{
struct ifnet *ifp = &sc->sc_ic.ic_if;
DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags %x\n", __func__, ifp->if_flags));
if (ifp->if_flags & IFF_UP) {
ath_init(ifp);
#if 0
(void)ath_intr(sc);
#endif
if (sc->sc_power != NULL)
(*sc->sc_power)(sc, why);
if (ifp->if_flags & IFF_RUNNING)
ath_start(ifp);
}
}
void
ath_shutdown(void *arg)
{
struct ath_softc *sc = arg;
ath_stop(&sc->sc_ic.ic_if);
}
int
ath_intr(void *arg)
{
return ath_intr1((struct ath_softc *)arg);
}
int
ath_intr1(struct ath_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct ath_hal *ah = sc->sc_ah;
HAL_INT status;
if (sc->sc_invalid) {
/*
* The hardware is not ready/present, don't touch anything.
* Note this can happen early on if the IRQ is shared.
*/
DPRINTF(ATH_DEBUG_ANY, ("%s: invalid; ignored\n", __func__));
return 0;
}
if (!ath_hal_is_intr_pending(ah)) /* shared irq, not for us */
return 0;
if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags 0x%x\n",
__func__, ifp->if_flags));
ath_hal_get_isr(ah, &status); /* clear ISR */
ath_hal_set_intr(ah, 0); /* disable further intr's */
return 1; /* XXX */
}
ath_hal_get_isr(ah, &status); /* NB: clears ISR too */
DPRINTF(ATH_DEBUG_INTR, ("%s: status 0x%x\n", __func__, status));
status &= sc->sc_imask; /* discard unasked for bits */
if (status & HAL_INT_FATAL) {
sc->sc_stats.ast_hardware++;
ath_hal_set_intr(ah, 0); /* disable intr's until reset */
ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_fataltask);
} else if (status & HAL_INT_RXORN) {
sc->sc_stats.ast_rxorn++;
ath_hal_set_intr(ah, 0); /* disable intr's until reset */
ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_rxorntask);
} else {
if (status & HAL_INT_RXEOL) {
/*
* NB: the hardware should re-read the link when
* RXE bit is written, but it doesn't work at
* least on older hardware revs.
*/
sc->sc_stats.ast_rxeol++;
sc->sc_rxlink = NULL;
}
if (status & HAL_INT_TXURN) {
sc->sc_stats.ast_txurn++;
/* bump tx trigger level */
ath_hal_update_tx_triglevel(ah, AH_TRUE);
}
if (status & HAL_INT_RX)
ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_rxtask);
if (status & HAL_INT_TX)
ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_txtask);
if (status & HAL_INT_SWBA)
ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_swbatask);
if (status & HAL_INT_BMISS) {
sc->sc_stats.ast_bmiss++;
ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_bmisstask);
}
}
return 1;
}
void
ath_fatal_proc(void *arg, int pending)
{
struct ath_softc *sc = arg;
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
if_printf(ifp, "hardware error; resetting\n");
ath_reset(sc);
}
void
ath_rxorn_proc(void *arg, int pending)
{
struct ath_softc *sc = arg;
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
if_printf(ifp, "rx FIFO overrun; resetting\n");
ath_reset(sc);
}
void
ath_bmiss_proc(void *arg, int pending)
{
struct ath_softc *sc = arg;
struct ieee80211com *ic = &sc->sc_ic;
DPRINTF(ATH_DEBUG_ANY, ("%s: pending %u\n", __func__, pending));
if (ic->ic_opmode != IEEE80211_M_STA)
return;
if (ic->ic_state == IEEE80211_S_RUN) {
/*
* Rather than go directly to scan state, try to
* reassociate first. If that fails then the state
* machine will drop us into scanning after timing
* out waiting for a probe response.
*/
ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
}
}
u_int
ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan)
{
enum ieee80211_phymode mode = ieee80211_chan2mode(ic, chan);
switch (mode) {
case IEEE80211_MODE_AUTO:
return 0;
case IEEE80211_MODE_11A:
return CHANNEL_A;
case IEEE80211_MODE_11B:
return CHANNEL_B;
case IEEE80211_MODE_11G:
return CHANNEL_PUREG;
case IEEE80211_MODE_TURBO:
return CHANNEL_T;
default:
panic("%s: unsupported mode %d\n", __func__, mode);
return 0;
}
}
int
ath_init(struct ifnet *ifp)
{
return ath_init1((struct ath_softc *)ifp->if_softc);
}
int
ath_init1(struct ath_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct ieee80211_node *ni;
enum ieee80211_phymode mode;
struct ath_hal *ah = sc->sc_ah;
HAL_STATUS status;
HAL_CHANNEL hchan;
int error = 0, s;
DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags 0x%x\n",
__func__, ifp->if_flags));
if ((error = ath_enable(sc)) != 0)
return error;
s = splnet();
/*
* Stop anything previously setup. This is safe
* whether this is the first time through or not.
*/
ath_stop(ifp);
/*
* The basic interface to setting the hardware in a good
* state is ``reset''. On return the hardware is known to
* be powered up and with interrupts disabled. This must
* be followed by initialization of the appropriate bits
* and then setup of the interrupt mask.
*/
hchan.channel = ic->ic_ibss_chan->ic_freq;
hchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan);
if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_FALSE, &status)) {
if_printf(ifp, "unable to reset hardware; hal status %u\n",
status);
error = EIO;
goto done;
}
/*
* Setup the hardware after reset: the key cache
* is filled as needed and the receive engine is
* set going. Frame transmit is handled entirely
* in the frame output path; there's nothing to do
* here except setup the interrupt mask.
*/
if (ic->ic_flags & IEEE80211_F_WEPON) {
if ((error = ath_initkeytable(sc)) != 0) {
if_printf(ifp, "unable to initialize the key cache\n");
goto done;
}
}
if ((error = ath_startrecv(sc)) != 0) {
if_printf(ifp, "unable to start recv logic\n");
goto done;
}
/*
* Enable interrupts.
*/
sc->sc_imask = HAL_INT_RX | HAL_INT_TX
| HAL_INT_RXEOL | HAL_INT_RXORN
| HAL_INT_FATAL | HAL_INT_GLOBAL;
ath_hal_set_intr(ah, sc->sc_imask);
ifp->if_flags |= IFF_RUNNING;
ic->ic_state = IEEE80211_S_INIT;
/*
* The hardware should be ready to go now so it's safe
* to kick the 802.11 state machine as it's likely to
* immediately call back to us to send mgmt frames.
*/
ni = ic->ic_bss;
ni->ni_chan = ic->ic_ibss_chan;
mode = ieee80211_chan2mode(ic, ni->ni_chan);
if (mode != sc->sc_curmode)
ath_setcurmode(sc, mode);
if (ic->ic_opmode != IEEE80211_M_MONITOR) {
ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
} else {
ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
}
done:
splx(s);
return error;
}
void
ath_stop(struct ifnet *ifp)
{
struct ieee80211com *ic = (struct ieee80211com *) ifp;
struct ath_softc *sc = ifp->if_softc;
struct ath_hal *ah = sc->sc_ah;
int s;
DPRINTF(ATH_DEBUG_ANY, ("%s: invalid %u if_flags 0x%x\n",
__func__, sc->sc_invalid, ifp->if_flags));
s = splnet();
if (ifp->if_flags & IFF_RUNNING) {
/*
* Shutdown the hardware and driver:
* disable interrupts
* turn off timers
* clear transmit machinery
* clear receive machinery
* drain and release tx queues
* reclaim beacon resources
* reset 802.11 state machine
* power down hardware
*
* Note that some of this work is not possible if the
* hardware is gone (invalid).
*/
ifp->if_flags &= ~IFF_RUNNING;
ifp->if_timer = 0;
if (!sc->sc_invalid)
ath_hal_set_intr(ah, 0);
ath_draintxq(sc);
if (!sc->sc_invalid) {
ath_stoprecv(sc);
} else {
sc->sc_rxlink = NULL;
}
IFQ_PURGE(&ifp->if_snd);
ath_beacon_free(sc);
ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
if (!sc->sc_invalid) {
ath_hal_set_power(ah, HAL_PM_FULL_SLEEP, 0);
}
ath_disable(sc);
}
splx(s);
}
/*
* Reset the hardware w/o losing operational state. This is
* basically a more efficient way of doing ath_stop, ath_init,
* followed by state transitions to the current 802.11
* operational state. Used to recover from errors rx overrun
* and to reset the hardware when rf gain settings must be reset.
*/
void
ath_reset(struct ath_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct ath_hal *ah = sc->sc_ah;
struct ieee80211_channel *c;
HAL_STATUS status;
HAL_CHANNEL hchan;
/*
* Convert to a HAL channel description with the flags
* constrained to reflect the current operating mode.
*/
c = ic->ic_ibss_chan;
hchan.channel = c->ic_freq;
hchan.channelFlags = ath_chan2flags(ic, c);
ath_hal_set_intr(ah, 0); /* disable interrupts */
ath_draintxq(sc); /* stop xmit side */
ath_stoprecv(sc); /* stop recv side */
/* NB: indicate channel change so we do a full reset */
if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) {
if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
__func__, status);
}
/* In case channel changed, save as a node channel */
ic->ic_bss->ni_chan = ic->ic_ibss_chan;
ath_hal_set_intr(ah, sc->sc_imask);
if (ath_startrecv(sc) != 0) /* restart recv */
if_printf(ifp, "%s: unable to start recv logic\n", __func__);
ath_start(ifp); /* restart xmit */
if (ic->ic_state == IEEE80211_S_RUN)
ath_beacon_config(sc); /* restart beacons */
}
void
ath_start(struct ifnet *ifp)
{
struct ath_softc *sc = ifp->if_softc;
struct ath_hal *ah = sc->sc_ah;
struct ieee80211com *ic = &sc->sc_ic;
struct ieee80211_node *ni;
struct ath_buf *bf;
struct mbuf *m;
struct ieee80211_frame *wh;
int s;
if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING ||
sc->sc_invalid)
return;
for (;;) {
/*
* Grab a TX buffer and associated resources.
*/
s = splnet();
bf = TAILQ_FIRST(&sc->sc_txbuf);
if (bf != NULL)
TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
splx(s);
if (bf == NULL) {
DPRINTF(ATH_DEBUG_ANY, ("%s: out of xmit buffers\n",
__func__));
sc->sc_stats.ast_tx_qstop++;
ifp->if_flags |= IFF_OACTIVE;
break;
}
/*
* Poll the management queue for frames; they
* have priority over normal data frames.
*/
IF_DEQUEUE(&ic->ic_mgtq, m);
if (m == NULL) {
/*
* No data frames go out unless we're associated.
*/
if (ic->ic_state != IEEE80211_S_RUN) {
DPRINTF(ATH_DEBUG_ANY,
("%s: ignore data packet, state %u\n",
__func__, ic->ic_state));
sc->sc_stats.ast_tx_discard++;
s = splnet();
TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
splx(s);
break;
}
IFQ_DEQUEUE(&ifp->if_snd, m);
if (m == NULL) {
s = splnet();
TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
splx(s);
break;
}
ifp->if_opackets++;
#if NBPFILTER > 0
if (ifp->if_bpf)
bpf_mtap(ifp->if_bpf, m);
#endif
/*
* Encapsulate the packet in prep for transmission.
*/
m = ieee80211_encap(ifp, m, &ni);
if (m == NULL) {
DPRINTF(ATH_DEBUG_ANY,
("%s: encapsulation failure\n",
__func__));
sc->sc_stats.ast_tx_encap++;
goto bad;
}
wh = mtod(m, struct ieee80211_frame *);
} else {
/*
* Hack! The referenced node pointer is in the
* rcvif field of the packet header. This is
* placed there by ieee80211_mgmt_output because
* we need to hold the reference with the frame
* and there's no other way (other than packet
* tags which we consider too expensive to use)
* to pass it along.
*/
ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
m->m_pkthdr.rcvif = NULL;
wh = mtod(m, struct ieee80211_frame *);
if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
/* fill time stamp */
u_int64_t tsf;
u_int32_t *tstamp;
tsf = ath_hal_get_tsf64(ah);
/* XXX: adjust 100us delay to xmit */
tsf += 100;
tstamp = (u_int32_t *)&wh[1];
tstamp[0] = htole32(tsf & 0xffffffff);
tstamp[1] = htole32(tsf >> 32);
}
sc->sc_stats.ast_tx_mgmt++;
}
if (ath_tx_start(sc, ni, bf, m)) {
bad:
s = splnet();
TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
splx(s);
ifp->if_oerrors++;
if (ni != NULL)
ieee80211_release_node(ic, ni);
continue;
}
sc->sc_tx_timer = 5;
ifp->if_timer = 1;
}
}
int
ath_media_change(struct ifnet *ifp)
{
int error;
error = ieee80211_media_change(ifp);
if (error == ENETRESET) {
if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
(IFF_RUNNING|IFF_UP))
ath_init(ifp); /* XXX lose error */
error = 0;
}
return error;
}
void
ath_watchdog(struct ifnet *ifp)
{
struct ath_softc *sc = ifp->if_softc;
struct ieee80211com *ic = &sc->sc_ic;
ifp->if_timer = 0;
if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
return;
if (sc->sc_tx_timer) {
if (--sc->sc_tx_timer == 0) {
if_printf(ifp, "device timeout\n");
ath_reset(sc);
ifp->if_oerrors++;
sc->sc_stats.ast_watchdog++;
return;
}
ifp->if_timer = 1;
}
if (ic->ic_fixed_rate == -1) {
/*
* Run the rate control algorithm if we're not
* locked at a fixed rate.
*/
if (ic->ic_opmode == IEEE80211_M_STA) {
ath_rate_ctl(sc, ic->ic_bss);
} else {
ieee80211_iterate_nodes(ic, ath_rate_ctl, sc);
}
}
ieee80211_watchdog(ifp);
}
int
ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
{
struct ath_softc *sc = ifp->if_softc;
struct ieee80211com *ic = &sc->sc_ic;
struct ifreq *ifr = (struct ifreq *)data;
struct ifaddr *ifa = (struct ifaddr *)data;
int error = 0, s;
s = splnet();
switch (cmd) {
case SIOCSIFADDR:
ifp->if_flags |= IFF_UP;
#ifdef INET
if (ifa->ifa_addr->sa_family == AF_INET) {
arp_ifinit(&ic->ic_ac, ifa);
}
#endif /* INET */
/* FALLTHROUGH */
case SIOCSIFFLAGS:
if (ifp->if_flags & IFF_UP) {
if (ifp->if_flags & IFF_RUNNING) {
/*
* To avoid rescanning another access point,
* do not call ath_init() here. Instead,
* only reflect promisc mode settings.
*/
ath_mode_init(sc);
} else {
/*
* Beware of being called during detach to
* reset promiscuous mode. In that case we
* will still be marked UP but not RUNNING.
* However trying to re-init the interface
* is the wrong thing to do as we've already
* torn down much of our state. There's
* probably a better way to deal with this.
*/
if (!sc->sc_invalid)
ath_init(ifp); /* XXX lose error */
}
} else
ath_stop(ifp);
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
#ifdef __FreeBSD__
/*
* The upper layer has already installed/removed
* the multicast address(es), just recalculate the
* multicast filter for the card.
*/
if (ifp->if_flags & IFF_RUNNING)
ath_mode_init(sc);
#endif
error = (cmd == SIOCADDMULTI) ?
ether_addmulti(ifr, &sc->sc_ic.ic_ac) :
ether_delmulti(ifr, &sc->sc_ic.ic_ac);
if (error == ENETRESET) {
if (ifp->if_flags & IFF_RUNNING)
ath_mode_init(sc);
error = 0;
}
break;
case SIOCGATHSTATS:
error = copyout(&sc->sc_stats,
ifr->ifr_data, sizeof (sc->sc_stats));
break;
default:
error = ieee80211_ioctl(ifp, cmd, data);
if (error == ENETRESET) {
if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
(IFF_RUNNING|IFF_UP)) {
struct ieee80211com *ic = &sc->sc_ic;
if (ic->ic_opmode != IEEE80211_M_MONITOR)
ath_init(ifp); /* XXX lose error */
else
ath_reset(sc);
}
error = 0;
}
break;
}
splx(s);
return error;
}
/*
* Fill the hardware key cache with key entries.
*/
int
ath_initkeytable(struct ath_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ath_hal *ah = sc->sc_ah;
int i;
/* XXX maybe should reset all keys when !WEPON */
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
struct ieee80211_wepkey *k = &ic->ic_nw_keys[i];
if (k->wk_len == 0)
ath_hal_reset_key(ah, i);
else {
HAL_KEYVAL hk;
bzero(&hk, sizeof(hk));
bcopy(k->wk_key, hk.wk_key, k->wk_len);
/*
* Pad the key to a supported key length. It
* is always a good idea to use full-length
* keys without padded zeros but this seems
* to be the default behaviour used by many
* implementations.
*/
if (k->wk_len <= AR5K_KEYVAL_LENGTH_40)
hk.wk_len = AR5K_KEYVAL_LENGTH_40;
else if (k->wk_len <= AR5K_KEYVAL_LENGTH_104)
hk.wk_len = AR5K_KEYVAL_LENGTH_104;
else if (k->wk_len <= AR5K_KEYVAL_LENGTH_128)
hk.wk_len = AR5K_KEYVAL_LENGTH_128;
else
return (EINVAL);
if (ath_hal_set_key(ah, i, &hk) != AH_TRUE)
return (EINVAL);
}
}
return (0);
}
void
ath_mcastfilter_accum(caddr_t dl, u_int32_t (*mfilt)[2])
{
u_int32_t val;
u_int8_t pos;
val = LE_READ_4(dl + 0);
pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
val = LE_READ_4(dl + 3);
pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
pos &= 0x3f;
(*mfilt)[pos / 32] |= (1 << (pos % 32));
}
void
ath_mcastfilter_compute(struct ath_softc *sc, u_int32_t (*mfilt)[2])
{
struct ifnet *ifp = &sc->sc_ic.ic_if;
struct ether_multi *enm;
struct ether_multistep estep;
ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ac, enm);
while (enm != NULL) {
/* XXX Punt on ranges. */
if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi)) {
(*mfilt)[0] = (*mfilt)[1] = ~((u_int32_t)0);
ifp->if_flags |= IFF_ALLMULTI;
return;
}
ath_mcastfilter_accum(enm->enm_addrlo, mfilt);
ETHER_NEXT_MULTI(estep, enm);
}
ifp->if_flags &= ~IFF_ALLMULTI;
}
/*
* Calculate the receive filter according to the
* operating mode and state:
*
* o always accept unicast, broadcast, and multicast traffic
* o maintain current state of phy error reception
* o probe request frames are accepted only when operating in
* hostap, adhoc, or monitor modes
* o enable promiscuous mode according to the interface state
* o accept beacons:
* - when operating in adhoc mode so the 802.11 layer creates
* node table entries for peers,
* - when operating in station mode for collecting rssi data when
* the station is otherwise quiet, or
* - when scanning
*/
u_int32_t
ath_calcrxfilter(struct ath_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ath_hal *ah = sc->sc_ah;
struct ifnet *ifp = &ic->ic_if;
u_int32_t rfilt;
rfilt = (ath_hal_get_rx_filter(ah) & HAL_RX_FILTER_PHYERR)
| HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
if (ic->ic_opmode != IEEE80211_M_STA)
rfilt |= HAL_RX_FILTER_PROBEREQ;
if (ic->ic_opmode != IEEE80211_M_AHDEMO)
rfilt |= HAL_RX_FILTER_BEACON;
if (ifp->if_flags & IFF_PROMISC)
rfilt |= HAL_RX_FILTER_PROM;
return rfilt;
}
void
ath_mode_init(struct ath_softc *sc)
{
struct ath_hal *ah = sc->sc_ah;
u_int32_t rfilt, mfilt[2];
/* configure rx filter */
rfilt = ath_calcrxfilter(sc);
ath_hal_set_rx_filter(ah, rfilt);
/* configure operational mode */
ath_hal_set_opmode(ah);
/* calculate and install multicast filter */
mfilt[0] = mfilt[1] = 0;
ath_mcastfilter_compute(sc, &mfilt);
ath_hal_set_mcast_filter(ah, mfilt[0], mfilt[1]);
DPRINTF(ATH_DEBUG_MODE, ("%s: RX filter 0x%x, MC filter %08x:%08x\n",
__func__, rfilt, mfilt[0], mfilt[1]));
}
struct mbuf *
ath_getmbuf(int flags, int type, u_int pktlen)
{
struct mbuf *m;
KASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
#ifdef __FreeBSD__
if (pktlen <= MHLEN) {
MGETHDR(m, flags, type);
} else {
m = m_getcl(flags, type, M_PKTHDR);
}
#else
MGETHDR(m, flags, type);
if (m != NULL && pktlen > MHLEN) {
MCLGET(m, flags);
if ((m->m_flags & M_EXT) == 0) {
m_free(m);
m = NULL;
}
}
#endif
return m;
}
int
ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ath_hal *ah = sc->sc_ah;
struct ath_buf *bf;
struct ath_desc *ds;
struct mbuf *m;
int error;
u_int8_t rate;
const HAL_RATE_TABLE *rt;
u_int flags;
bf = sc->sc_bcbuf;
if (bf->bf_m != NULL) {
bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
m_freem(bf->bf_m);
bf->bf_m = NULL;
bf->bf_node = NULL;
}
/*
* NB: the beacon data buffer must be 32-bit aligned;
* we assume the mbuf routines will return us something
* with this alignment (perhaps should assert).
*/
m = ieee80211_beacon_alloc(ic, ni);
if (m == NULL) {
DPRINTF(ATH_DEBUG_BEACON, ("%s: cannot get mbuf/cluster\n",
__func__));
sc->sc_stats.ast_be_nombuf++;
return ENOMEM;
}
DPRINTF(ATH_DEBUG_BEACON, ("%s: m %p len %u\n", __func__, m, m->m_len));
error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
BUS_DMA_NOWAIT);
if (error != 0) {
m_freem(m);
return error;
}
KASSERT(bf->bf_nseg == 1,
("%s: multi-segment packet; nseg %u", __func__, bf->bf_nseg));
bf->bf_m = m;
/* setup descriptors */
ds = bf->bf_desc;
bzero(ds, sizeof(struct ath_desc));
if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_has_veol) {
ds->ds_link = bf->bf_daddr; /* link to self */
flags |= HAL_TXDESC_VEOL;
} else {
ds->ds_link = 0;
}
ds->ds_data = bf->bf_segs[0].ds_addr;
DPRINTF(ATH_DEBUG_ANY, ("%s: segaddr %p seglen %u\n", __func__,
(caddr_t)bf->bf_segs[0].ds_addr, (u_int)bf->bf_segs[0].ds_len));
/*
* Calculate rate code.
* XXX everything at min xmit rate
*/
rt = sc->sc_currates;
KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) {
rate = rt->info[0].rateCode | rt->info[0].shortPreamble;
} else {
rate = rt->info[0].rateCode;
}
flags = HAL_TXDESC_NOACK;
if (ic->ic_opmode == IEEE80211_M_IBSS)
flags |= HAL_TXDESC_VEOL;
if (!ath_hal_setup_tx_desc(ah, ds
, m->m_pkthdr.len + IEEE80211_CRC_LEN /* packet length */
, sizeof(struct ieee80211_frame) /* header length */
, HAL_PKT_TYPE_BEACON /* Atheros packet type */
, 60 /* txpower XXX */
, rate, 1 /* series 0 rate/tries */
, HAL_TXKEYIX_INVALID /* no encryption */
, 0 /* antenna mode */
, flags /* no ack for beacons */
, 0 /* rts/cts rate */
, 0 /* rts/cts duration */
)) {
printf("%s: ath_hal_setup_tx_desc failed\n", __func__);
return -1;
}
/* NB: beacon's BufLen must be a multiple of 4 bytes */
/* XXX verify mbuf data area covers this roundup */
if (!ath_hal_fill_tx_desc(ah, ds
, roundup(bf->bf_segs[0].ds_len, 4) /* buffer length */
, AH_TRUE /* first segment */
, AH_TRUE /* last segment */
)) {
printf("%s: ath_hal_fill_tx_desc failed\n", __func__);
return -1;
}
/* XXX it is not appropriate to bus_dmamap_sync? -dcy */
return 0;
}
void
ath_beacon_proc(void *arg, int pending)
{
struct ath_softc *sc = arg;
struct ieee80211com *ic = &sc->sc_ic;
struct ath_buf *bf = sc->sc_bcbuf;
struct ath_hal *ah = sc->sc_ah;
DPRINTF(ATH_DEBUG_BEACON_PROC, ("%s: pending %u\n", __func__, pending));
if (ic->ic_opmode == IEEE80211_M_STA ||
bf == NULL || bf->bf_m == NULL) {
DPRINTF(ATH_DEBUG_ANY, ("%s: ic_flags=%x bf=%p bf_m=%p\n",
__func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL));
return;
}
/* TODO: update beacon to reflect PS poll state */
if (!ath_hal_stop_tx_dma(ah, sc->sc_bhalq)) {
DPRINTF(ATH_DEBUG_ANY, ("%s: beacon queue %u did not stop?\n",
__func__, sc->sc_bhalq));
}
bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 0,
bf->bf_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
ath_hal_put_tx_buf(ah, sc->sc_bhalq, bf->bf_daddr);
ath_hal_tx_start(ah, sc->sc_bhalq);
DPRINTF(ATH_DEBUG_BEACON_PROC,
("%s: TXDP%u = %p (%p)\n", __func__,
sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc));
}
void
ath_beacon_free(struct ath_softc *sc)
{
struct ath_buf *bf = sc->sc_bcbuf;
if (bf->bf_m != NULL) {
bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
m_freem(bf->bf_m);
bf->bf_m = NULL;
bf->bf_node = NULL;
}
}
/*
* Configure the beacon and sleep timers.
*
* When operating as an AP this resets the TSF and sets
* up the hardware to notify us when we need to issue beacons.
*
* When operating in station mode this sets up the beacon
* timers according to the timestamp of the last received
* beacon and the current TSF, configures PCF and DTIM
* handling, programs the sleep registers so the hardware
* will wakeup in time to receive beacons, and configures
* the beacon miss handling so we'll receive a BMISS
* interrupt when we stop seeing beacons from the AP
* we've associated with.
*/
void
ath_beacon_config(struct ath_softc *sc)
{
#define MS_TO_TU(x) (((x) * 1000) / 1024)
struct ath_hal *ah = sc->sc_ah;
struct ieee80211com *ic = &sc->sc_ic;
struct ieee80211_node *ni = ic->ic_bss;
u_int32_t nexttbtt, intval;
nexttbtt = (LE_READ_4(ni->ni_tstamp + 4) << 22) |
(LE_READ_4(ni->ni_tstamp) >> 10);
intval = MS_TO_TU(ni->ni_intval) & HAL_BEACON_PERIOD;
if (nexttbtt == 0) { /* e.g. for ap mode */
nexttbtt = intval;
} else if (intval) {
nexttbtt = roundup(nexttbtt, intval);
}
DPRINTF(ATH_DEBUG_BEACON, ("%s: intval %u nexttbtt %u\n",
__func__, ni->ni_intval, nexttbtt));
if (ic->ic_opmode == IEEE80211_M_STA) {
HAL_BEACON_STATE bs;
u_int32_t bmisstime;
/* NB: no PCF support right now */
bzero(&bs, sizeof(bs));
bs.bs_intval = intval;
bs.bs_nexttbtt = nexttbtt;
bs.bs_dtimperiod = bs.bs_intval;
bs.bs_nextdtim = nexttbtt;
/*
* Calculate the number of consecutive beacons to miss
* before taking a BMISS interrupt. The configuration
* is specified in ms, so we need to convert that to
* TU's and then calculate based on the beacon interval.
* Note that we clamp the result to at most 10 beacons.
*/
bmisstime = MS_TO_TU(ic->ic_bmisstimeout);
bs.bs_bmissthreshold = howmany(bmisstime, intval);
if (bs.bs_bmissthreshold > 7) {
bs.bs_bmissthreshold = 7;
} else if (bs.bs_bmissthreshold <= 0) {
bs.bs_bmissthreshold = 1;
}
/*
* Calculate sleep duration. The configuration is
* given in ms. We insure a multiple of the beacon
* period is used. Also, if the sleep duration is
* greater than the DTIM period then it makes senses
* to make it a multiple of that.
*
* XXX fixed at 100ms
*/
bs.bs_sleepduration =
roundup(MS_TO_TU(100), bs.bs_intval);
if (bs.bs_sleepduration > bs.bs_dtimperiod) {
bs.bs_sleepduration =
roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
}
DPRINTF(ATH_DEBUG_BEACON,
("%s: intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u"
" sleep %u\n"
, __func__
, bs.bs_intval
, bs.bs_nexttbtt
, bs.bs_dtimperiod
, bs.bs_nextdtim
, bs.bs_bmissthreshold
, bs.bs_sleepduration
));
ath_hal_set_intr(ah, 0);
ath_hal_set_beacon_timers(ah, &bs, 0/*XXX*/, 0, 0);
sc->sc_imask |= HAL_INT_BMISS;
ath_hal_set_intr(ah, sc->sc_imask);
} else {
ath_hal_set_intr(ah, 0);
if (nexttbtt == intval)
intval |= HAL_BEACON_RESET_TSF;
if (ic->ic_opmode == IEEE80211_M_IBSS) {
/*
* In IBSS mode enable the beacon timers but ony
* enable SWBA interrupts if we need to moanually
* prepare beacon frames. Otherwise we use a
* self-linked tx descriptor and let the hardware
* deal with things.
*/
intval |= HAL_BEACON_ENA;
if (!sc->sc_has_veol)
sc->sc_imask |= HAL_INT_SWBA;
} else if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
/*
* In AP mode we enable the beacon timers and
* SWBA interrupts to prepare beacon frames.
*/
intval |= HAL_BEACON_ENA;
sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */
}
ath_hal_init_beacon(ah, nexttbtt, intval);
ath_hal_set_intr(ah, sc->sc_imask);
/*
* When using a self-linked beacon descriptor in IBBS
* mode load it once here.
*/
if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_has_veol)
ath_beacon_proc(sc, 0);
}
}
int
ath_desc_alloc(struct ath_softc *sc)
{
int i, bsize, error = -1;
struct ath_desc *ds;
struct ath_buf *bf;
/* allocate descriptors */
sc->sc_desc_len = sizeof(struct ath_desc) *
(ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1);
if ((error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_desc_len, PAGE_SIZE,
0, &sc->sc_dseg, 1, &sc->sc_dnseg, 0)) != 0) {
printf("%s: unable to allocate control data, error = %d\n",
sc->sc_dev.dv_xname, error);
goto fail0;
}
if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg,
sc->sc_desc_len, (caddr_t *)&sc->sc_desc, BUS_DMA_COHERENT)) != 0) {
printf("%s: unable to map control data, error = %d\n",
sc->sc_dev.dv_xname, error);
goto fail1;
}
if ((error = bus_dmamap_create(sc->sc_dmat, sc->sc_desc_len, 1,
sc->sc_desc_len, 0, 0, &sc->sc_ddmamap)) != 0) {
printf("%s: unable to create control data DMA map, "
"error = %d\n", sc->sc_dev.dv_xname, error);
goto fail2;
}
if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap, sc->sc_desc,
sc->sc_desc_len, NULL, 0)) != 0) {
printf("%s: unable to load control data DMA map, error = %d\n",
sc->sc_dev.dv_xname, error);
goto fail3;
}
ds = sc->sc_desc;
sc->sc_desc_paddr = sc->sc_ddmamap->dm_segs[0].ds_addr;
DPRINTF(ATH_DEBUG_XMIT_DESC|ATH_DEBUG_RECV_DESC,
("ath_desc_alloc: DMA map: %p (%lu) -> %p (%lu)\n",
ds, (u_long)sc->sc_desc_len,
(caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len));
/* allocate buffers */
bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1);
bf = malloc(bsize, M_DEVBUF, M_NOWAIT);
if (bf == NULL) {
printf("%s: unable to allocate Tx/Rx buffers\n",
sc->sc_dev.dv_xname);
error = ENOMEM;
goto fail3;
}
bzero(bf, bsize);
sc->sc_bufptr = bf;
TAILQ_INIT(&sc->sc_rxbuf);
for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
bf->bf_desc = ds;
bf->bf_daddr = sc->sc_desc_paddr +
((caddr_t)ds - (caddr_t)sc->sc_desc);
if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
MCLBYTES, 0, 0, &bf->bf_dmamap)) != 0) {
printf("%s: unable to create Rx dmamap, error = %d\n",
sc->sc_dev.dv_xname, error);
goto fail4;
}
TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
}
TAILQ_INIT(&sc->sc_txbuf);
for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) {
bf->bf_desc = ds;
bf->bf_daddr = sc->sc_desc_paddr +
((caddr_t)ds - (caddr_t)sc->sc_desc);
if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
ATH_TXDESC, MCLBYTES, 0, 0, &bf->bf_dmamap)) != 0) {
printf("%s: unable to create Tx dmamap, error = %d\n",
sc->sc_dev.dv_xname, error);
goto fail5;
}
TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
}
TAILQ_INIT(&sc->sc_txq);
/* beacon buffer */
bf->bf_desc = ds;
bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc);
if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0, 0,
&bf->bf_dmamap)) != 0) {
printf("%s: unable to create beacon dmamap, error = %d\n",
sc->sc_dev.dv_xname, error);
goto fail5;
}
sc->sc_bcbuf = bf;
return 0;
fail5:
for (i = ATH_RXBUF; i < ATH_RXBUF + ATH_TXBUF; i++) {
if (sc->sc_bufptr[i].bf_dmamap == NULL)
continue;
bus_dmamap_destroy(sc->sc_dmat, sc->sc_bufptr[i].bf_dmamap);
}
fail4:
for (i = 0; i < ATH_RXBUF; i++) {
if (sc->sc_bufptr[i].bf_dmamap == NULL)
continue;
bus_dmamap_destroy(sc->sc_dmat, sc->sc_bufptr[i].bf_dmamap);
}
fail3:
bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
fail2:
bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
sc->sc_ddmamap = NULL;
fail1:
bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_desc, sc->sc_desc_len);
fail0:
bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
return error;
}
void
ath_desc_free(struct ath_softc *sc)
{
struct ath_buf *bf;
bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
m_freem(bf->bf_m);
}
TAILQ_FOREACH(bf, &sc->sc_txbuf, bf_list)
bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
if (bf->bf_m) {
bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
m_freem(bf->bf_m);
bf->bf_m = NULL;
}
}
if (sc->sc_bcbuf != NULL) {
bus_dmamap_unload(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
bus_dmamap_destroy(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
sc->sc_bcbuf = NULL;
}
TAILQ_INIT(&sc->sc_rxbuf);
TAILQ_INIT(&sc->sc_txbuf);
TAILQ_INIT(&sc->sc_txq);
free(sc->sc_bufptr, M_DEVBUF);
sc->sc_bufptr = NULL;
}
struct ieee80211_node *
ath_node_alloc(struct ieee80211com *ic)
{
struct ath_node *an =
malloc(sizeof(struct ath_node), M_DEVBUF, M_NOWAIT);
if (an) {
int i;
bzero(an, sizeof(struct ath_node));
for (i = 0; i < ATH_RHIST_SIZE; i++)
an->an_rx_hist[i].arh_ticks = ATH_RHIST_NOTIME;
an->an_rx_hist_next = ATH_RHIST_SIZE-1;
return &an->an_node;
} else
return NULL;
}
void
ath_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
{
struct ath_softc *sc = ic->ic_if.if_softc;
struct ath_buf *bf;
TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
if (bf->bf_node == ni)
bf->bf_node = NULL;
}
(*sc->sc_node_free)(ic, ni);
}
void
ath_node_copy(struct ieee80211com *ic,
struct ieee80211_node *dst, const struct ieee80211_node *src)
{
struct ath_softc *sc = ic->ic_if.if_softc;
bcopy(&src[1], &dst[1],
sizeof(struct ath_node) - sizeof(struct ieee80211_node));
(*sc->sc_node_copy)(ic, dst, src);
}
u_int8_t
ath_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni)
{
struct ath_node *an = ATH_NODE(ni);
int i, now, nsamples, rssi;
/*
* Calculate the average over the last second of sampled data.
*/
now = ATH_TICKS();
nsamples = 0;
rssi = 0;
i = an->an_rx_hist_next;
do {
struct ath_recv_hist *rh = &an->an_rx_hist[i];
if (rh->arh_ticks == ATH_RHIST_NOTIME)
goto done;
if (now - rh->arh_ticks > hz)
goto done;
rssi += rh->arh_rssi;
nsamples++;
if (i == 0) {
i = ATH_RHIST_SIZE-1;
} else {
i--;
}
} while (i != an->an_rx_hist_next);
done:
/*
* Return either the average or the last known
* value if there is no recent data.
*/
return (nsamples ? rssi / nsamples : an->an_rx_hist[i].arh_rssi);
}
int
ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
{
struct ath_hal *ah = sc->sc_ah;
int error;
struct mbuf *m;
struct ath_desc *ds;
m = bf->bf_m;
if (m == NULL) {
/*
* NB: by assigning a page to the rx dma buffer we
* implicitly satisfy the Atheros requirement that
* this buffer be cache-line-aligned and sized to be
* multiple of the cache line size. Not doing this
* causes weird stuff to happen (for the 5210 at least).
*/
m = ath_getmbuf(M_DONTWAIT, MT_DATA, MCLBYTES);
if (m == NULL) {
DPRINTF(ATH_DEBUG_ANY,
("%s: no mbuf/cluster\n", __func__));
sc->sc_stats.ast_rx_nombuf++;
return ENOMEM;
}
bf->bf_m = m;
m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
BUS_DMA_NOWAIT);
if (error != 0) {
DPRINTF(ATH_DEBUG_ANY,
("%s: ath_bus_dmamap_load_mbuf failed;"
" error %d\n", __func__, error));
sc->sc_stats.ast_rx_busdma++;
return error;
}
KASSERT(bf->bf_nseg == 1,
("ath_rxbuf_init: multi-segment packet; nseg %u",
bf->bf_nseg));
}
bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 0,
bf->bf_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
/*
* Setup descriptors. For receive we always terminate
* the descriptor list with a self-linked entry so we'll
* not get overrun under high load (as can happen with a
* 5212 when ANI processing enables PHY errors).
*
* To insure the last descriptor is self-linked we create
* each descriptor as self-linked and add it to the end. As
* each additional descriptor is added the previous self-linked
* entry is ``fixed'' naturally. This should be safe even
* if DMA is happening. When processing RX interrupts we
* never remove/process the last, self-linked, entry on the
* descriptor list. This insures the hardware always has
* someplace to write a new frame.
*/
ds = bf->bf_desc;
bzero(ds, sizeof(struct ath_desc));
ds->ds_link = bf->bf_daddr; /* link to self */
ds->ds_data = bf->bf_segs[0].ds_addr;
ath_hal_setup_rx_desc(ah, ds
, m->m_len /* buffer size */
, 0
);
if (sc->sc_rxlink != NULL)
*sc->sc_rxlink = bf->bf_daddr;
sc->sc_rxlink = &ds->ds_link;
return 0;
}
void
ath_rx_proc(void *arg, int npending)
{
#define PA2DESC(_sc, _pa) \
((struct ath_desc *)((caddr_t)(_sc)->sc_desc + \
((_pa) - (_sc)->sc_desc_paddr)))
struct ath_softc *sc = arg;
struct ath_buf *bf;
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct ath_hal *ah = sc->sc_ah;
struct ath_desc *ds;
struct mbuf *m;
struct ieee80211_frame *wh, whbuf;
struct ieee80211_node *ni;
struct ath_node *an;
struct ath_recv_hist *rh;
int len;
u_int phyerr;
HAL_STATUS status;
DPRINTF(ATH_DEBUG_RX_PROC, ("%s: pending %u\n", __func__, npending));
do {
bf = TAILQ_FIRST(&sc->sc_rxbuf);
if (bf == NULL) { /* NB: shouldn't happen */
if_printf(ifp, "ath_rx_proc: no buffer!\n");
break;
}
ds = bf->bf_desc;
if (ds->ds_link == bf->bf_daddr) {
/* NB: never process the self-linked entry at the end */
break;
}
m = bf->bf_m;
if (m == NULL) { /* NB: shouldn't happen */
if_printf(ifp, "ath_rx_proc: no mbuf!\n");
continue;
}
/* XXX sync descriptor memory */
/*
* Must provide the virtual address of the current
* descriptor, the physical address, and the virtual
* address of the next descriptor in the h/w chain.
* This allows the HAL to look ahead to see if the
* hardware is done with a descriptor by checking the
* done bit in the following descriptor and the address
* of the current descriptor the DMA engine is working
* on. All this is necessary because of our use of
* a self-linked list to avoid rx overruns.
*/
status = ath_hal_proc_rx_desc(ah, ds,
bf->bf_daddr, PA2DESC(sc, ds->ds_link));
#ifdef AR_DEBUG
if (ath_debug & ATH_DEBUG_RECV_DESC)
ath_printrxbuf(bf, status == HAL_OK);
#endif
if (status == HAL_EINPROGRESS)
break;
TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
if (ds->ds_rxstat.rs_more) {
/*
* Frame spans multiple descriptors; this
* cannot happen yet as we don't support
* jumbograms. If not in monitor mode,
* discard the frame.
*/
/*
* Enable this if you want to see error
* frames in Monitor mode.
*/
#ifdef ERROR_FRAMES
if (ic->ic_opmode != IEEE80211_M_MONITOR) {
/* XXX statistic */
goto rx_next;
}
#endif
/* fall thru for monitor mode handling... */
} else if (ds->ds_rxstat.rs_status != 0) {
if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
sc->sc_stats.ast_rx_crcerr++;
if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
sc->sc_stats.ast_rx_fifoerr++;
if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT)
sc->sc_stats.ast_rx_badcrypt++;
if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
sc->sc_stats.ast_rx_phyerr++;
phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
sc->sc_stats.ast_rx_phy[phyerr]++;
}
/*
* reject error frames, we normally don't want
* to see them in monitor mode.
*/
if ((ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT ) ||
(ds->ds_rxstat.rs_status & HAL_RXERR_PHY))
goto rx_next;
/*
* In monitor mode, allow through packets that
* cannot be decrypted
*/
if ((ds->ds_rxstat.rs_status & ~HAL_RXERR_DECRYPT) ||
sc->sc_ic.ic_opmode != IEEE80211_M_MONITOR)
goto rx_next;
}
len = ds->ds_rxstat.rs_datalen;
if (len < IEEE80211_MIN_LEN) {
DPRINTF(ATH_DEBUG_RECV, ("%s: short packet %d\n",
__func__, len));
sc->sc_stats.ast_rx_tooshort++;
goto rx_next;
}
bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 0,
bf->bf_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
bf->bf_m = NULL;
m->m_pkthdr.rcvif = ifp;
m->m_pkthdr.len = m->m_len = len;
#if NBPFILTER > 0
if (sc->sc_drvbpf) {
struct mbuf mb;
sc->sc_rxtap.wr_rate =
sc->sc_hwmap[ds->ds_rxstat.rs_rate] &
IEEE80211_RATE_VAL;
sc->sc_rxtap.wr_antsignal = ds->ds_rxstat.rs_rssi;
sc->sc_rxtap.wr_antenna = ds->ds_rxstat.rs_antenna;
M_DUP_PKTHDR(&mb, m);
mb.m_data = (caddr_t)&sc->sc_rxtap;
mb.m_len = sc->sc_rxtap_len;
mb.m_next = m;
mb.m_pkthdr.len += mb.m_len;
bpf_mtap(sc->sc_drvbpf, &mb);
}
#endif
m_adj(m, -IEEE80211_CRC_LEN);
wh = mtod(m, struct ieee80211_frame *);
if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
/*
* WEP is decrypted by hardware. Clear WEP bit
* and trim WEP header for ieee80211_input().
*/
wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
bcopy(wh, &whbuf, sizeof(whbuf));
m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
wh = mtod(m, struct ieee80211_frame *);
bcopy(&whbuf, wh, sizeof(whbuf));
/*
* Also trim WEP ICV from the tail.
*/
m_adj(m, -IEEE80211_WEP_CRCLEN);
/*
* The header has probably moved.
*/
wh = mtod(m, struct ieee80211_frame *);
}
/*
* Locate the node for sender, track state, and
* then pass this node (referenced) up to the 802.11
* layer for its use.
*/
ni = ieee80211_find_rxnode(ic, wh);
/*
* Record driver-specific state.
*/
an = ATH_NODE(ni);
if (++(an->an_rx_hist_next) == ATH_RHIST_SIZE)
an->an_rx_hist_next = 0;
rh = &an->an_rx_hist[an->an_rx_hist_next];
rh->arh_ticks = ATH_TICKS();
rh->arh_rssi = ds->ds_rxstat.rs_rssi;
rh->arh_antenna = ds->ds_rxstat.rs_antenna;
/*
* Send frame up for processing.
*/
ieee80211_input(ifp, m, ni,
ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp);
/*
* The frame may have caused the node to be marked for
* reclamation (e.g. in response to a DEAUTH message)
* so use release_node here instead of unref_node.
*/
ieee80211_release_node(ic, ni);
rx_next:
TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
} while (ath_rxbuf_init(sc, bf) == 0);
ath_hal_set_rx_monitor(ah); /* rx signal state monitoring */
ath_hal_start_rx(ah); /* in case of RXEOL */
if ((ifp->if_flags & IFF_OACTIVE) == 0 && !IFQ_IS_EMPTY(&ifp->if_snd))
ath_start(ifp);
#undef PA2DESC
}
/*
* XXX Size of an ACK control frame in bytes.
*/
#define IEEE80211_ACK_SIZE (2+2+IEEE80211_ADDR_LEN+4)
int
ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
struct ath_buf *bf, struct mbuf *m0)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ath_hal *ah = sc->sc_ah;
struct ifnet *ifp = &sc->sc_ic.ic_if;
int i, error, iswep, hdrlen, pktlen, len, s;
u_int8_t rix, cix, txrate, ctsrate;
struct ath_desc *ds;
struct mbuf *m;
struct ieee80211_frame *wh;
u_int32_t iv;
u_int8_t *ivp;
u_int8_t hdrbuf[sizeof(struct ieee80211_frame) +
IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN];
u_int subtype, flags, ctsduration, antenna;
HAL_PKT_TYPE atype;
const HAL_RATE_TABLE *rt;
HAL_BOOL shortPreamble;
struct ath_node *an;
wh = mtod(m0, struct ieee80211_frame *);
iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
hdrlen = sizeof(struct ieee80211_frame);
pktlen = m0->m_pkthdr.len;
if (iswep) {
bcopy(mtod(m0, caddr_t), hdrbuf, hdrlen);
m_adj(m0, hdrlen);
M_PREPEND(m0, sizeof(hdrbuf), M_DONTWAIT);
if (m0 == NULL) {
sc->sc_stats.ast_tx_nombuf++;
return ENOMEM;
}
ivp = hdrbuf + hdrlen;
wh = mtod(m0, struct ieee80211_frame *);
/*
* XXX
* IV must not duplicate during the lifetime of the key.
* But no mechanism to renew keys is defined in IEEE 802.11
* for WEP. And the IV may be duplicated at other stations
* because the session key itself is shared. So we use a
* pseudo random IV for now, though it is not the right way.
*
* NB: Rather than use a strictly random IV we select a
* random one to start and then increment the value for
* each frame. This is an explicit tradeoff between
* overhead and security. Given the basic insecurity of
* WEP this seems worthwhile.
*/
/*
* Skip 'bad' IVs from Fluhrer/Mantin/Shamir:
* (B, 255, N) with 3 <= B < 16 and 0 <= N <= 255
*/
iv = ic->ic_iv;
if ((iv & 0xff00) == 0xff00) {
int B = (iv & 0xff0000) >> 16;
if (3 <= B && B < 16)
iv = (B+1) << 16;
}
ic->ic_iv = iv + 1;
/*
* NB: Preserve byte order of IV for packet
* sniffers; it doesn't matter otherwise.
*/
#if AH_BYTE_ORDER == AH_BIG_ENDIAN
ivp[0] = iv >> 0;
ivp[1] = iv >> 8;
ivp[2] = iv >> 16;
#else
ivp[2] = iv >> 0;
ivp[1] = iv >> 8;
ivp[0] = iv >> 16;
#endif
ivp[3] = ic->ic_wep_txkey << 6; /* Key ID and pad */
bcopy(hdrbuf, mtod(m0, caddr_t), sizeof(hdrbuf));
/*
* The length of hdrlen and pktlen must be increased for WEP
*/
len = IEEE80211_WEP_IVLEN +
IEEE80211_WEP_KIDLEN +
IEEE80211_WEP_CRCLEN;
hdrlen += len;
pktlen += len;
}
pktlen += IEEE80211_CRC_LEN;
/*
* Load the DMA map so any coalescing is done. This
* also calculates the number of descriptors we need.
*/
error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
BUS_DMA_NOWAIT);
/*
* Discard null packets and check for packets that
* require too many TX descriptors. We try to convert
* the latter to a cluster.
*/
if (error == EFBIG) { /* too many desc's, linearize */
sc->sc_stats.ast_tx_linear++;
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
sc->sc_stats.ast_tx_nombuf++;
m_freem(m0);
return ENOMEM;
}
M_DUP_PKTHDR(m, m0);
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
sc->sc_stats.ast_tx_nomcl++;
m_freem(m0);
m_free(m);
return ENOMEM;
}
m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
m_freem(m0);
m->m_len = m->m_pkthdr.len;
m0 = m;
error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
BUS_DMA_NOWAIT);
if (error != 0) {
sc->sc_stats.ast_tx_busdma++;
m_freem(m0);
return error;
}
KASSERT(bf->bf_nseg == 1,
("ath_tx_start: packet not one segment; nseg %u",
bf->bf_nseg));
} else if (error != 0) {
sc->sc_stats.ast_tx_busdma++;
m_freem(m0);
return error;
} else if (bf->bf_nseg == 0) { /* null packet, discard */
sc->sc_stats.ast_tx_nodata++;
m_freem(m0);
return EIO;
}
DPRINTF(ATH_DEBUG_XMIT, ("%s: m %p len %u\n", __func__, m0, pktlen));
bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 0,
bf->bf_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
bf->bf_m = m0;
bf->bf_node = ni; /* NB: held reference */
/* setup descriptors */
ds = bf->bf_desc;
rt = sc->sc_currates;
KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
/*
* Calculate Atheros packet type from IEEE80211 packet header
* and setup for rate calculations.
*/
atype = HAL_PKT_TYPE_NORMAL; /* default */
switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
case IEEE80211_FC0_TYPE_MGT:
subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
atype = HAL_PKT_TYPE_BEACON;
} else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
atype = HAL_PKT_TYPE_PROBE_RESP;
} else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM) {
atype = HAL_PKT_TYPE_ATIM;
}
rix = 0; /* XXX lowest rate */
break;
case IEEE80211_FC0_TYPE_CTL:
subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
if (subtype == IEEE80211_FC0_SUBTYPE_PS_POLL)
atype = HAL_PKT_TYPE_PSPOLL;
rix = 0; /* XXX lowest rate */
break;
default:
rix = sc->sc_rixmap[ni->ni_rates.rs_rates[ni->ni_txrate] &
IEEE80211_RATE_VAL];
if (rix == 0xff) {
if_printf(ifp, "bogus xmit rate 0x%x\n",
ni->ni_rates.rs_rates[ni->ni_txrate]);
sc->sc_stats.ast_tx_badrate++;
m_freem(m0);
return EIO;
}
break;
}
/*
* NB: the 802.11 layer marks whether or not we should
* use short preamble based on the current mode and
* negotiated parameters.
*/
if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
txrate = rt->info[rix].rateCode | rt->info[rix].shortPreamble;
shortPreamble = AH_TRUE;
sc->sc_stats.ast_tx_shortpre++;
} else {
txrate = rt->info[rix].rateCode;
shortPreamble = AH_FALSE;
}
/*
* Calculate miscellaneous flags.
*/
flags = HAL_TXDESC_CLRDMASK; /* XXX needed for wep errors */
if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
flags |= HAL_TXDESC_NOACK; /* no ack on broad/multicast */
sc->sc_stats.ast_tx_noack++;
} else if (pktlen > ic->ic_rtsthreshold) {
flags |= HAL_TXDESC_RTSENA; /* RTS based on frame length */
sc->sc_stats.ast_tx_rts++;
}
/*
* Calculate duration. This logically belongs in the 802.11
* layer but it lacks sufficient information to calculate it.
*/
if ((flags & HAL_TXDESC_NOACK) == 0 &&
(wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
u_int16_t dur;
/*
* XXX not right with fragmentation.
*/
dur = ath_hal_computetxtime(ah, rt, IEEE80211_ACK_SIZE,
rix, shortPreamble);
*((u_int16_t*) wh->i_dur) = htole16(dur);
}
/*
* Calculate RTS/CTS rate and duration if needed.
*/
ctsduration = 0;
if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
/*
* CTS transmit rate is derived from the transmit rate
* by looking in the h/w rate table. We must also factor
* in whether or not a short preamble is to be used.
*/
cix = rt->info[rix].controlRate;
ctsrate = rt->info[cix].rateCode;
if (shortPreamble)
ctsrate |= rt->info[cix].shortPreamble;
/*
* Compute the transmit duration based on the size
* of an ACK frame. We call into the HAL to do the
* computation since it depends on the characteristics
* of the actual PHY being used.
*/
if (flags & HAL_TXDESC_RTSENA) { /* SIFS + CTS */
ctsduration += ath_hal_computetxtime(ah,
rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
}
/* SIFS + data */
ctsduration += ath_hal_computetxtime(ah,
rt, pktlen, rix, shortPreamble);
if ((flags & HAL_TXDESC_NOACK) == 0) { /* SIFS + ACK */
ctsduration += ath_hal_computetxtime(ah,
rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
}
} else
ctsrate = 0;
/*
* For now use the antenna on which the last good
* frame was received on. We assume this field is
* initialized to 0 which gives us ``auto'' or the
* ``default'' antenna.
*/
an = (struct ath_node *) ni;
if (an->an_tx_antenna) {
antenna = an->an_tx_antenna;
} else {
antenna = an->an_rx_hist[an->an_rx_hist_next].arh_antenna;
}
#if NBPFILTER > 0
if (ic->ic_rawbpf)
bpf_mtap(ic->ic_rawbpf, m0);
if (sc->sc_drvbpf) {
struct mbuf mb;
sc->sc_txtap.wt_flags = 0;
if (shortPreamble)
sc->sc_txtap.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
if (iswep)
sc->sc_txtap.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
sc->sc_txtap.wt_rate = ni->ni_rates.rs_rates[ni->ni_txrate] &
IEEE80211_RATE_VAL;
sc->sc_txtap.wt_txpower = 30;
sc->sc_txtap.wt_antenna = antenna;
M_DUP_PKTHDR(&mb, m0);
mb.m_data = (caddr_t)&sc->sc_txtap;
mb.m_len = sc->sc_txtap_len;
mb.m_next = m0;
mb.m_pkthdr.len += mb.m_len;
bpf_mtap(sc->sc_drvbpf, &mb);
}
#endif
/*
* Formulate first tx descriptor with tx controls.
*/
/* XXX check return value? */
ath_hal_setup_tx_desc(ah, ds
, pktlen /* packet length */
, hdrlen /* header length */
, atype /* Atheros packet type */
, 60 /* txpower XXX */
, txrate, 1+10 /* series 0 rate/tries */
, iswep ? sc->sc_ic.ic_wep_txkey : HAL_TXKEYIX_INVALID
, antenna /* antenna mode */
, flags /* flags */
, ctsrate /* rts/cts rate */
, ctsduration /* rts/cts duration */
);
#ifdef notyet
ath_hal_setup_xtx_desc(ah, ds
, AH_FALSE /* short preamble */
, 0, 0 /* series 1 rate/tries */
, 0, 0 /* series 2 rate/tries */
, 0, 0 /* series 3 rate/tries */
);
#endif
/*
* Fillin the remainder of the descriptor info.
*/
for (i = 0; i < bf->bf_nseg; i++, ds++) {
ds->ds_data = bf->bf_segs[i].ds_addr;
if (i == bf->bf_nseg - 1) {
ds->ds_link = 0;
} else {
ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
}
ath_hal_fill_tx_desc(ah, ds
, bf->bf_segs[i].ds_len /* segment length */
, i == 0 /* first segment */
, i == bf->bf_nseg - 1 /* last segment */
);
DPRINTF(ATH_DEBUG_XMIT,
("%s: %d: %08x %08x %08x %08x %08x %08x\n",
__func__, i, ds->ds_link, ds->ds_data,
ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]));
}
/*
* Insert the frame on the outbound list and
* pass it on to the hardware.
*/
s = splnet();
TAILQ_INSERT_TAIL(&sc->sc_txq, bf, bf_list);
if (sc->sc_txlink == NULL) {
ath_hal_put_tx_buf(ah, sc->sc_txhalq, bf->bf_daddr);
DPRINTF(ATH_DEBUG_XMIT, ("%s: TXDP0 = %p (%p)\n", __func__,
(caddr_t)bf->bf_daddr, bf->bf_desc));
} else {
*sc->sc_txlink = bf->bf_daddr;
DPRINTF(ATH_DEBUG_XMIT, ("%s: link(%p)=%p (%p)\n", __func__,
sc->sc_txlink, (caddr_t)bf->bf_daddr, bf->bf_desc));
}
sc->sc_txlink = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
splx(s);
ath_hal_tx_start(ah, sc->sc_txhalq);
return 0;
}
void
ath_tx_proc(void *arg, int npending)
{
struct ath_softc *sc = arg;
struct ath_hal *ah = sc->sc_ah;
struct ath_buf *bf;
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct ath_desc *ds;
struct ieee80211_node *ni;
struct ath_node *an;
int sr, lr, s;
HAL_STATUS status;
DPRINTF(ATH_DEBUG_TX_PROC, ("%s: pending %u tx queue %p, "
"link %p\n", __func__, npending,
(caddr_t)(u_intptr_t) ath_hal_get_tx_buf(sc->sc_ah,
sc->sc_txhalq), sc->sc_txlink));
for (;;) {
s = splnet();
bf = TAILQ_FIRST(&sc->sc_txq);
if (bf == NULL) {
sc->sc_txlink = NULL;
splx(s);
break;
}
/* only the last descriptor is needed */
ds = &bf->bf_desc[bf->bf_nseg - 1];
status = ath_hal_proc_tx_desc(ah, ds);
#ifdef AR_DEBUG
if (ath_debug & ATH_DEBUG_XMIT_DESC)
ath_printtxbuf(bf, status == HAL_OK);
#endif
if (status == HAL_EINPROGRESS) {
splx(s);
break;
}
TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
splx(s);
ni = bf->bf_node;
if (ni != NULL) {
an = (struct ath_node *) ni;
if (ds->ds_txstat.ts_status == 0) {
an->an_tx_ok++;
an->an_tx_antenna = ds->ds_txstat.ts_antenna;
} else {
an->an_tx_err++;
ifp->if_oerrors++;
if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
sc->sc_stats.ast_tx_xretries++;
if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
sc->sc_stats.ast_tx_fifoerr++;
if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
sc->sc_stats.ast_tx_filtered++;
an->an_tx_antenna = 0; /* invalidate */
}
sr = ds->ds_txstat.ts_shortretry;
lr = ds->ds_txstat.ts_longretry;
sc->sc_stats.ast_tx_shortretry += sr;
sc->sc_stats.ast_tx_longretry += lr;
if (sr + lr)
an->an_tx_retr++;
/*
* Reclaim reference to node.
*
* NB: the node may be reclaimed here if, for example
* this is a DEAUTH message that was sent and the
* node was timed out due to inactivity.
*/
ieee80211_release_node(ic, ni);
}
bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 0,
bf->bf_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
m_freem(bf->bf_m);
bf->bf_m = NULL;
bf->bf_node = NULL;
s = splnet();
TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
splx(s);
}
ifp->if_flags &= ~IFF_OACTIVE;
sc->sc_tx_timer = 0;
ath_start(ifp);
}
/*
* Drain the transmit queue and reclaim resources.
*/
void
ath_draintxq(struct ath_softc *sc)
{
struct ath_hal *ah = sc->sc_ah;
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct ieee80211_node *ni;
struct ath_buf *bf;
int s;
/* XXX return value */
if (!sc->sc_invalid) {
/* don't touch the hardware if marked invalid */
(void) ath_hal_stop_tx_dma(ah, sc->sc_txhalq);
DPRINTF(ATH_DEBUG_RESET,
("%s: tx queue %p, link %p\n", __func__,
(caddr_t)(u_intptr_t) ath_hal_get_tx_buf(ah, sc->sc_txhalq),
sc->sc_txlink));
(void) ath_hal_stop_tx_dma(ah, sc->sc_bhalq);
DPRINTF(ATH_DEBUG_RESET,
("%s: beacon queue %p\n", __func__,
(caddr_t)(u_intptr_t)ath_hal_get_tx_buf(ah, sc->sc_bhalq)));
}
for (;;) {
s = splnet();
bf = TAILQ_FIRST(&sc->sc_txq);
if (bf == NULL) {
sc->sc_txlink = NULL;
splx(s);
break;
}
TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
splx(s);
#ifdef AR_DEBUG
if (ath_debug & ATH_DEBUG_RESET) {
ath_printtxbuf(bf,
ath_hal_proc_tx_desc(ah, bf->bf_desc) == HAL_OK);
}
#endif /* AR_DEBUG */
bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
m_freem(bf->bf_m);
bf->bf_m = NULL;
ni = bf->bf_node;
bf->bf_node = NULL;
s = splnet();
if (ni != NULL) {
/*
* Reclaim node reference.
*/
ieee80211_release_node(ic, ni);
}
TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
splx(s);
}
ifp->if_flags &= ~IFF_OACTIVE;
sc->sc_tx_timer = 0;
}
/*
* Disable the receive h/w in preparation for a reset.
*/
void
ath_stoprecv(struct ath_softc *sc)
{
#define PA2DESC(_sc, _pa) \
((struct ath_desc *)((caddr_t)(_sc)->sc_desc + \
((_pa) - (_sc)->sc_desc_paddr)))
struct ath_hal *ah = sc->sc_ah;
ath_hal_stop_pcu_recv(ah); /* disable PCU */
ath_hal_set_rx_filter(ah, 0); /* clear recv filter */
ath_hal_stop_rx_dma(ah); /* disable DMA engine */
DELAY(3000); /* long enough for 1 frame */
#ifdef AR_DEBUG
if (ath_debug & ATH_DEBUG_RESET) {
struct ath_buf *bf;
printf("%s: rx queue %p, link %p\n", __func__,
(caddr_t)(u_intptr_t)ath_hal_get_rx_buf(ah), sc->sc_rxlink);
TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
struct ath_desc *ds = bf->bf_desc;
if (ath_hal_proc_rx_desc(ah, ds, bf->bf_daddr,
PA2DESC(sc, ds->ds_link)) == HAL_OK)
ath_printrxbuf(bf, 1);
}
}
#endif
sc->sc_rxlink = NULL; /* just in case */
#undef PA2DESC
}
/*
* Enable the receive h/w following a reset.
*/
int
ath_startrecv(struct ath_softc *sc)
{
struct ath_hal *ah = sc->sc_ah;
struct ath_buf *bf;
sc->sc_rxlink = NULL;
TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
int error = ath_rxbuf_init(sc, bf);
if (error != 0) {
DPRINTF(ATH_DEBUG_RECV,
("%s: ath_rxbuf_init failed %d\n",
__func__, error));
return error;
}
}
bf = TAILQ_FIRST(&sc->sc_rxbuf);
ath_hal_put_rx_buf(ah, bf->bf_daddr);
ath_hal_start_rx(ah); /* enable recv descriptors */
ath_mode_init(sc); /* set filters, etc. */
ath_hal_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
return 0;
}
/*
* Set/change channels. If the channel is really being changed,
* it's done by resetting the chip. To accomplish this we must
* first cleanup any pending DMA, then restart stuff after a la
* ath_init.
*/
int
ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
{
struct ath_hal *ah = sc->sc_ah;
struct ieee80211com *ic = &sc->sc_ic;
DPRINTF(ATH_DEBUG_ANY, ("%s: %u (%u MHz) -> %u (%u MHz)\n", __func__,
ieee80211_chan2ieee(ic, ic->ic_ibss_chan),
ic->ic_ibss_chan->ic_freq,
ieee80211_chan2ieee(ic, chan), chan->ic_freq));
if (chan != ic->ic_ibss_chan) {
HAL_STATUS status;
HAL_CHANNEL hchan;
enum ieee80211_phymode mode;
/*
* To switch channels clear any pending DMA operations;
* wait long enough for the RX fifo to drain, reset the
* hardware at the new frequency, and then re-enable
* the relevant bits of the h/w.
*/
ath_hal_set_intr(ah, 0); /* disable interrupts */
ath_draintxq(sc); /* clear pending tx frames */
ath_stoprecv(sc); /* turn off frame recv */
/*
* Convert to a HAL channel description with
* the flags constrained to reflect the current
* operating mode.
*/
hchan.channel = chan->ic_freq;
hchan.channelFlags = ath_chan2flags(ic, chan);
if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE,
&status)) {
if_printf(&ic->ic_if, "ath_chan_set: unable to reset "
"channel %u (%u Mhz)\n",
ieee80211_chan2ieee(ic, chan), chan->ic_freq);
return EIO;
}
/*
* Re-enable rx framework.
*/
if (ath_startrecv(sc) != 0) {
if_printf(&ic->ic_if,
"ath_chan_set: unable to restart recv logic\n");
return EIO;
}
/*
* Update BPF state.
*/
sc->sc_txtap.wt_chan_freq = sc->sc_rxtap.wr_chan_freq =
htole16(chan->ic_freq);
sc->sc_txtap.wt_chan_flags = sc->sc_rxtap.wr_chan_flags =
htole16(chan->ic_flags);
/*
* Change channels and update the h/w rate map
* if we're switching; e.g. 11a to 11b/g.
*/
ic->ic_ibss_chan = chan;
mode = ieee80211_chan2mode(ic, chan);
if (mode != sc->sc_curmode)
ath_setcurmode(sc, mode);
/*
* Re-enable interrupts.
*/
ath_hal_set_intr(ah, sc->sc_imask);
}
return 0;
}
void
ath_next_scan(void *arg)
{
struct ath_softc *sc = arg;
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
int s;
/* don't call ath_start w/o network interrupts blocked */
s = splnet();
if (ic->ic_state == IEEE80211_S_SCAN)
ieee80211_next_scan(ifp);
splx(s);
}
/*
* Periodically recalibrate the PHY to account
* for temperature/environment changes.
*/
void
ath_calibrate(void *arg)
{
struct ath_softc *sc = arg;
struct ath_hal *ah = sc->sc_ah;
struct ieee80211com *ic = &sc->sc_ic;
struct ieee80211_channel *c;
HAL_CHANNEL hchan;
sc->sc_stats.ast_per_cal++;
/*
* Convert to a HAL channel description with the flags
* constrained to reflect the current operating mode.
*/
c = ic->ic_ibss_chan;
hchan.channel = c->ic_freq;
hchan.channelFlags = ath_chan2flags(ic, c);
DPRINTF(ATH_DEBUG_CALIBRATE,
("%s: channel %u/%x\n", __func__, c->ic_freq, c->ic_flags));
if (ath_hal_get_rf_gain(ah) == HAL_RFGAIN_NEED_CHANGE) {
/*
* Rfgain is out of bounds, reset the chip
* to load new gain values.
*/
sc->sc_stats.ast_per_rfgain++;
ath_reset(sc);
}
if (!ath_hal_calibrate(ah, &hchan)) {
DPRINTF(ATH_DEBUG_ANY,
("%s: calibration of channel %u failed\n",
__func__, c->ic_freq));
sc->sc_stats.ast_per_calfail++;
}
timeout_add(&sc->sc_cal_to, hz * ath_calinterval);
}
HAL_LED_STATE
ath_state_to_led(enum ieee80211_state state)
{
switch (state) {
case IEEE80211_S_INIT:
return HAL_LED_INIT;
case IEEE80211_S_SCAN:
return HAL_LED_SCAN;
case IEEE80211_S_AUTH:
return HAL_LED_AUTH;
case IEEE80211_S_ASSOC:
return HAL_LED_ASSOC;
case IEEE80211_S_RUN:
return HAL_LED_RUN;
default:
panic("%s: unknown 802.11 state %d\n", __func__, state);
return HAL_LED_INIT;
}
}
int
ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
{
struct ifnet *ifp = &ic->ic_if;
struct ath_softc *sc = ifp->if_softc;
struct ath_hal *ah = sc->sc_ah;
struct ieee80211_node *ni;
const u_int8_t *bssid;
int i, error;
u_int32_t rfilt;
DPRINTF(ATH_DEBUG_ANY, ("%s: %s -> %s\n", __func__,
ieee80211_state_name[ic->ic_state],
ieee80211_state_name[nstate]));
timeout_del(&sc->sc_scan_to);
timeout_del(&sc->sc_cal_to);
ath_hal_set_ledstate(ah, ath_state_to_led(nstate)); /* set LED */
if (nstate == IEEE80211_S_INIT) {
sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
ath_hal_set_intr(ah, sc->sc_imask);
return (*sc->sc_newstate)(ic, nstate, arg);
}
ni = ic->ic_bss;
error = ath_chan_set(sc, ni->ni_chan);
if (error != 0)
goto bad;
rfilt = ath_calcrxfilter(sc);
if (nstate == IEEE80211_S_SCAN) {
bssid = sc->sc_broadcast_addr;
} else {
bssid = ni->ni_bssid;
}
ath_hal_set_rx_filter(ah, rfilt);
DPRINTF(ATH_DEBUG_ANY, ("%s: RX filter 0x%x bssid %s\n",
__func__, rfilt, ether_sprintf((u_char*)bssid)));
if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA) {
ath_hal_set_associd(ah, bssid, ni->ni_associd);
} else {
ath_hal_set_associd(ah, bssid, 0);
}
if (ic->ic_flags & IEEE80211_F_WEPON) {
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
if (ath_hal_is_key_valid(ah, i))
ath_hal_set_key_lladdr(ah, i, bssid);
}
}
if (ic->ic_opmode == IEEE80211_M_MONITOR) {
/* nothing to do */
} else if (nstate == IEEE80211_S_RUN) {
DPRINTF(ATH_DEBUG_ANY, ("%s(RUN): "
"ic_flags=0x%08x iv=%d bssid=%s "
"capinfo=0x%04x chan=%d\n",
__func__,
ic->ic_flags,
ni->ni_intval,
ether_sprintf(ni->ni_bssid),
ni->ni_capinfo,
ieee80211_chan2ieee(ic, ni->ni_chan)));
/*
* Allocate and setup the beacon frame for AP or adhoc mode.
*/
if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
ic->ic_opmode == IEEE80211_M_IBSS) {
error = ath_beacon_alloc(sc, ni);
if (error != 0)
goto bad;
}
/*
* Configure the beacon and sleep timers.
*/
ath_beacon_config(sc);
} else {
sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
ath_hal_set_intr(ah, sc->sc_imask);
}
/*
* Reset the rate control state.
*/
ath_rate_ctl_reset(sc, nstate);
/*
* Invoke the parent method to complete the work.
*/
error = (*sc->sc_newstate)(ic, nstate, arg);
if (nstate == IEEE80211_S_RUN) {
/* start periodic recalibration timer */
timeout_add(&sc->sc_cal_to, hz * ath_calinterval);
} else if (nstate == IEEE80211_S_SCAN) {
/* start ap/neighbor scan timer */
timeout_add(&sc->sc_scan_to, (hz * ath_dwelltime) / 1000);
}
bad:
return error;
}
void
ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
struct ieee80211_node *ni, int subtype, int rssi, u_int32_t rstamp)
{
struct ath_softc *sc = (struct ath_softc*)ic->ic_softc;
struct ath_hal *ah = sc->sc_ah;
(*sc->sc_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
switch (subtype) {
case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
case IEEE80211_FC0_SUBTYPE_BEACON:
if (ic->ic_opmode != IEEE80211_M_IBSS ||
ic->ic_state != IEEE80211_S_RUN)
break;
if (ieee80211_ibss_merge(ic, ni, ath_hal_get_tsf64(ah)) ==
ENETRESET)
ath_hal_set_associd(ah, ic->ic_bss->ni_bssid, 0);
break;
default:
break;
}
return;
}
/*
* Setup driver-specific state for a newly associated node.
* Note that we're called also on a re-associate, the isnew
* param tells us if this is the first time or not.
*/
void
ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
{
if (isnew) {
struct ath_node *an = (struct ath_node *) ni;
an->an_tx_ok = an->an_tx_err =
an->an_tx_retr = an->an_tx_upper = 0;
/* start with highest negotiated rate */
/*
* XXX should do otherwise but only when
* the rate control algorithm is better.
*/
KASSERT(ni->ni_rates.rs_nrates > 0,
("new association w/ no rates!"));
ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
}
}
int
ath_getchannels(struct ath_softc *sc, u_int cc, HAL_BOOL outdoor,
HAL_BOOL xchanmode)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct ath_hal *ah = sc->sc_ah;
HAL_CHANNEL *chans;
int i, ix, nchan;
sc->sc_nchan = 0;
chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
M_TEMP, M_NOWAIT);
if (chans == NULL) {
if_printf(ifp, "unable to allocate channel table\n");
return ENOMEM;
}
if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
cc, HAL_MODE_ALL, outdoor, xchanmode)) {
if_printf(ifp, "unable to collect channel list from hal\n");
free(chans, M_TEMP);
return EINVAL;
}
/*
* Convert HAL channels to ieee80211 ones and insert
* them in the table according to their channel number.
*/
for (i = 0; i < nchan; i++) {
HAL_CHANNEL *c = &chans[i];
ix = ath_hal_mhz2ieee(c->channel, c->channelFlags);
if (ix > IEEE80211_CHAN_MAX) {
if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n",
ix, c->channel, c->channelFlags);
continue;
}
DPRINTF(ATH_DEBUG_ANY,
("%s: HAL channel %d/%d freq %d flags %#04x idx %d\n",
sc->sc_dev.dv_xname, i, nchan, c->channel, c->channelFlags,
ix));
/* NB: flags are known to be compatible */
if (ic->ic_channels[ix].ic_freq == 0) {
ic->ic_channels[ix].ic_freq = c->channel;
ic->ic_channels[ix].ic_flags = c->channelFlags;
} else {
/* channels overlap; e.g. 11g and 11b */
ic->ic_channels[ix].ic_flags |= c->channelFlags;
}
/* count valid channels */
sc->sc_nchan++;
}
free(chans, M_TEMP);
if (sc->sc_nchan < 1) {
if_printf(ifp, "no valid channels for regdomain %s(%u)\n",
ieee80211_regdomain2name(ath_regdomain),
sc->sc_ah->ah_capabilities.cap_eeprom.ee_regdomain);
return ENOENT;
}
/* set an initial channel */
ic->ic_ibss_chan = &ic->ic_channels[0];
return 0;
}
int
ath_rate_setup(struct ath_softc *sc, u_int mode)
{
struct ath_hal *ah = sc->sc_ah;
struct ieee80211com *ic = &sc->sc_ic;
const HAL_RATE_TABLE *rt;
struct ieee80211_rateset *rs;
int i, maxrates;
switch (mode) {
case IEEE80211_MODE_11A:
sc->sc_rates[mode] = ath_hal_get_rate_table(ah, HAL_MODE_11A);
break;
case IEEE80211_MODE_11B:
sc->sc_rates[mode] = ath_hal_get_rate_table(ah, HAL_MODE_11B);
break;
case IEEE80211_MODE_11G:
sc->sc_rates[mode] = ath_hal_get_rate_table(ah, HAL_MODE_11G);
break;
case IEEE80211_MODE_TURBO:
sc->sc_rates[mode] = ath_hal_get_rate_table(ah, HAL_MODE_TURBO);
break;
default:
DPRINTF(ATH_DEBUG_ANY,
("%s: invalid mode %u\n", __func__, mode));
return 0;
}
rt = sc->sc_rates[mode];
if (rt == NULL)
return 0;
if (rt->rateCount > IEEE80211_RATE_MAXSIZE) {
DPRINTF(ATH_DEBUG_ANY,
("%s: rate table too small (%u > %u)\n",
__func__, rt->rateCount, IEEE80211_RATE_MAXSIZE));
maxrates = IEEE80211_RATE_MAXSIZE;
} else {
maxrates = rt->rateCount;
}
rs = &ic->ic_sup_rates[mode];
for (i = 0; i < maxrates; i++)
rs->rs_rates[i] = rt->info[i].dot11Rate;
rs->rs_nrates = maxrates;
return 1;
}
void
ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
{
const HAL_RATE_TABLE *rt;
int i;
memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
rt = sc->sc_rates[mode];
KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
for (i = 0; i < rt->rateCount; i++)
sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
bzero(sc->sc_hwmap, sizeof(sc->sc_hwmap));
for (i = 0; i < 32; i++)
sc->sc_hwmap[i] = rt->info[rt->rateCodeToIndex[i]].dot11Rate;
sc->sc_currates = rt;
sc->sc_curmode = mode;
}
/*
* Reset the rate control state for each 802.11 state transition.
*/
void
ath_rate_ctl_reset(struct ath_softc *sc, enum ieee80211_state state)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ieee80211_node *ni;
struct ath_node *an;
if (ic->ic_opmode != IEEE80211_M_STA) {
/*
* When operating as a station the node table holds
* the AP's that were discovered during scanning.
* For any other operating mode we want to reset the
* tx rate state of each node.
*/
TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
ni->ni_txrate = 0; /* use lowest rate */
an = (struct ath_node *) ni;
an->an_tx_ok = an->an_tx_err = an->an_tx_retr =
an->an_tx_upper = 0;
}
}
/*
* Reset local xmit state; this is really only meaningful
* when operating in station or adhoc mode.
*/
ni = ic->ic_bss;
an = (struct ath_node *) ni;
an->an_tx_ok = an->an_tx_err = an->an_tx_retr = an->an_tx_upper = 0;
if (state == IEEE80211_S_RUN) {
/* start with highest negotiated rate */
KASSERT(ni->ni_rates.rs_nrates > 0,
("transition to RUN state w/ no rates!"));
ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
} else {
/* use lowest rate */
ni->ni_txrate = 0;
}
}
/*
* Examine and potentially adjust the transmit rate.
*/
void
ath_rate_ctl(void *arg, struct ieee80211_node *ni)
{
struct ath_softc *sc = arg;
struct ath_node *an = (struct ath_node *) ni;
struct ieee80211_rateset *rs = &ni->ni_rates;
int mod = 0, orate, enough;
/*
* Rate control
* XXX: very primitive version.
*/
sc->sc_stats.ast_rate_calls++;
enough = (an->an_tx_ok + an->an_tx_err >= 10);
/* no packet reached -> down */
if (an->an_tx_err > 0 && an->an_tx_ok == 0)
mod = -1;
/* all packets needs retry in average -> down */
if (enough && an->an_tx_ok < an->an_tx_retr)
mod = -1;
/* no error and less than 10% of packets needs retry -> up */
if (enough && an->an_tx_err == 0 && an->an_tx_ok > an->an_tx_retr * 10)
mod = 1;
orate = ni->ni_txrate;
switch (mod) {
case 0:
if (enough && an->an_tx_upper > 0)
an->an_tx_upper--;
break;
case -1:
if (ni->ni_txrate > 0) {
ni->ni_txrate--;
sc->sc_stats.ast_rate_drop++;
}
an->an_tx_upper = 0;
break;
case 1:
if (++an->an_tx_upper < 2)
break;
an->an_tx_upper = 0;
if (ni->ni_txrate + 1 < rs->rs_nrates) {
ni->ni_txrate++;
sc->sc_stats.ast_rate_raise++;
}
break;
}
if (ni->ni_txrate != orate) {
DPRINTF(ATH_DEBUG_RATE,
("%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
__func__,
(rs->rs_rates[orate] & IEEE80211_RATE_VAL) / 2,
(rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
an->an_tx_ok, an->an_tx_err, an->an_tx_retr));
}
if (ni->ni_txrate != orate || enough)
an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 0;
}
#ifdef AR_DEBUG
void
ath_printrxbuf(struct ath_buf *bf, int done)
{
struct ath_desc *ds;
int i;
for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n",
i, ds, (struct ath_desc *)bf->bf_daddr + i,
ds->ds_link, ds->ds_data,
ds->ds_ctl0, ds->ds_ctl1,
ds->ds_hw[0], ds->ds_hw[1],
!done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!');
}
}
void
ath_printtxbuf(struct ath_buf *bf, int done)
{
struct ath_desc *ds;
int i;
for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
printf("T%d (%p %p) "
"%08x %08x %08x %08x %08x %08x %08x %08x %c\n",
i, ds, (struct ath_desc *)bf->bf_daddr + i,
ds->ds_link, ds->ds_data,
ds->ds_ctl0, ds->ds_ctl1,
ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
!done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!');
}
}
#endif /* AR_DEBUG */
#if NGPIO > 0
int
ath_gpio_attach(struct ath_softc *sc)
{
struct ath_hal *ah = sc->sc_ah;
struct gpiobus_attach_args gba;
int i;
if (ah->ah_gpio_npins < 1)
return 0;
/* Initialize gpio pins array */
for (i = 0; i < ah->ah_gpio_npins; i++) {
sc->sc_gpio_pins[i].pin_num = i;
sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
GPIO_PIN_OUTPUT;
/* Set pin mode to input */
ath_hal_set_gpio_input(ah, i);
sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT;
/* Get pin input */
sc->sc_gpio_pins[i].pin_state = ath_hal_get_gpio(ah, i) ?
GPIO_PIN_HIGH : GPIO_PIN_LOW;
}
/* Create gpio controller tag */
sc->sc_gpio_gc.gp_cookie = sc;
sc->sc_gpio_gc.gp_pin_read = ath_gpio_pin_read;
sc->sc_gpio_gc.gp_pin_write = ath_gpio_pin_write;
sc->sc_gpio_gc.gp_pin_ctl = ath_gpio_pin_ctl;
gba.gba_name = "gpio";
gba.gba_gc = &sc->sc_gpio_gc;
gba.gba_pins = sc->sc_gpio_pins;
gba.gba_npins = ah->ah_gpio_npins;
if (config_found(&sc->sc_dev, &gba, gpiobus_print) == NULL)
return (ENODEV);
return (0);
}
int
ath_gpio_pin_read(void *arg, int pin)
{
struct ath_softc *sc = arg;
struct ath_hal *ah = sc->sc_ah;
return (ath_hal_get_gpio(ah, pin) ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
}
void
ath_gpio_pin_write(void *arg, int pin, int value)
{
struct ath_softc *sc = arg;
struct ath_hal *ah = sc->sc_ah;
ath_hal_set_gpio(ah, pin, value ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
}
void
ath_gpio_pin_ctl(void *arg, int pin, int flags)
{
struct ath_softc *sc = arg;
struct ath_hal *ah = sc->sc_ah;
if (flags & GPIO_PIN_INPUT) {
ath_hal_set_gpio_input(ah, pin);
} else if (flags & GPIO_PIN_OUTPUT) {
ath_hal_set_gpio_output(ah, pin);
}
}
#endif /* NGPIO */
|