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
|
/* $Id: if_ipw.c,v 1.15 2004/10/27 21:23:45 damien Exp $ */
/*-
* Copyright (c) 2004
* Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*-
* Intel(R) PRO/Wireless 2100 MiniPCI driver
* www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
*/
#include "bpfilter.h"
#include <sys/param.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/conf.h>
#include <machine/bus.h>
#include <machine/endian.h>
#include <machine/intr.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_radiotap.h>
#include <dev/pci/if_ipwreg.h>
#include <dev/pci/if_ipwvar.h>
static const struct ieee80211_rateset ipw_rateset_11b =
{ 4, { 2, 4, 11, 22 } };
int ipw_match(struct device *, void *, void *);
void ipw_attach(struct device *, struct device *, void *);
int ipw_detach(struct device *, int);
int ipw_media_change(struct ifnet *);
void ipw_media_status(struct ifnet *, struct ifmediareq *);
int ipw_newstate(struct ieee80211com *, enum ieee80211_state, int);
u_int16_t ipw_read_prom_word(struct ipw_softc *, u_int8_t);
void ipw_command_intr(struct ipw_softc *, struct ipw_soft_buf *);
void ipw_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
void ipw_data_intr(struct ipw_softc *, struct ipw_status *,
struct ipw_soft_bd *, struct ipw_soft_buf *);
void ipw_notification_intr(struct ipw_softc *, struct ipw_soft_buf *);
void ipw_rx_intr(struct ipw_softc *);
void ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
void ipw_tx_intr(struct ipw_softc *);
int ipw_intr(void *);
int ipw_cmd(struct ipw_softc *, u_int32_t, void *, u_int32_t);
int ipw_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *);
void ipw_start(struct ifnet *);
void ipw_watchdog(struct ifnet *);
int ipw_get_table1(struct ipw_softc *, u_int32_t *);
int ipw_get_radio(struct ipw_softc *, int *);
int ipw_ioctl(struct ifnet *, u_long, caddr_t);
u_int32_t ipw_read_table1(struct ipw_softc *, u_int32_t);
void ipw_write_table1(struct ipw_softc *, u_int32_t, u_int32_t);
int ipw_read_table2(struct ipw_softc *, u_int32_t, void *, u_int32_t *);
int ipw_tx_init(struct ipw_softc *);
void ipw_tx_stop(struct ipw_softc *);
int ipw_rx_init(struct ipw_softc *);
void ipw_rx_stop(struct ipw_softc *);
void ipw_stop_master(struct ipw_softc *);
int ipw_reset(struct ipw_softc *);
int ipw_load_ucode(struct ipw_softc *, u_char *, int);
int ipw_load_firmware(struct ipw_softc *, u_char *, int);
int ipw_cache_firmware(struct ipw_softc *, void *);
void ipw_free_firmware(struct ipw_softc *);
int ipw_config(struct ipw_softc *);
int ipw_init(struct ifnet *);
void ipw_stop(struct ifnet *, int);
void ipw_read_mem_1(struct ipw_softc *, bus_size_t, u_int8_t *, bus_size_t);
void ipw_write_mem_1(struct ipw_softc *, bus_size_t, u_int8_t *, bus_size_t);
static __inline u_int8_t MEM_READ_1(struct ipw_softc *sc, u_int32_t addr)
{
CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
return CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA);
}
static __inline u_int32_t MEM_READ_4(struct ipw_softc *sc, u_int32_t addr)
{
CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
return CSR_READ_4(sc, IPW_CSR_INDIRECT_DATA);
}
#ifdef IPW_DEBUG
#define DPRINTF(x) if (ipw_debug > 0) printf x
#define DPRINTFN(n, x) if (ipw_debug >= (n)) printf x
int ipw_debug = 0;
#else
#define DPRINTF(x)
#define DPRINTFN(n, x)
#endif
struct cfattach ipw_ca = {
sizeof (struct ipw_softc), ipw_match, ipw_attach, ipw_detach
};
int
ipw_match(struct device *parent, void *match, void *aux)
{
struct pci_attach_args *pa = aux;
if (PCI_VENDOR (pa->pa_id) == PCI_VENDOR_INTEL &&
PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_2100_3B)
return 1;
return 0;
}
/* Base Address Register */
#define IPW_PCI_BAR0 0x10
void
ipw_attach(struct device *parent, struct device *self, void *aux)
{
struct ipw_softc *sc = (struct ipw_softc *)self;
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct pci_attach_args *pa = aux;
const char *intrstr;
bus_space_tag_t memt;
bus_space_handle_t memh;
bus_addr_t base;
pci_intr_handle_t ih;
pcireg_t data;
u_int16_t val;
int error, i;
sc->sc_pct = pa->pa_pc;
data = pci_conf_read(sc->sc_pct, pa->pa_tag, 0x40);
data &= ~0x00ff0000;
pci_conf_write(sc->sc_pct, pa->pa_tag, 0x40, data);
/* enable bus-mastering */
data = pci_conf_read(sc->sc_pct, pa->pa_tag, PCI_COMMAND_STATUS_REG);
data |= PCI_COMMAND_MASTER_ENABLE;
pci_conf_write(sc->sc_pct, pa->pa_tag, PCI_COMMAND_STATUS_REG, data);
/* map the register window */
error = pci_mapreg_map(pa, IPW_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz, 0);
if (error != 0) {
printf(": could not map memory space\n");
return;
}
sc->sc_st = memt;
sc->sc_sh = memh;
sc->sc_dmat = pa->pa_dmat;
/* disable interrupts */
CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
if (pci_intr_map(pa, &ih) != 0) {
printf(": could not map interrupt\n");
return;
}
intrstr = pci_intr_string(sc->sc_pct, ih);
sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, ipw_intr, sc,
sc->sc_dev.dv_xname);
if (sc->sc_ih == NULL) {
printf(": could not establish interrupt");
if (intrstr != NULL)
printf(" at %s", intrstr);
printf("\n");
return;
}
printf(": %s\n", intrstr);
if (ipw_reset(sc) != 0) {
printf(": could not reset adapter\n");
return;
}
ic->ic_phytype = IEEE80211_T_DS;
ic->ic_opmode = IEEE80211_M_STA;
ic->ic_state = IEEE80211_S_INIT;
/* set device capabilities */
ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_MONITOR |
IEEE80211_C_PMGT | IEEE80211_C_TXPMGT | IEEE80211_C_WEP;
/* read MAC address from EEPROM */
val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
ic->ic_myaddr[0] = val >> 8;
ic->ic_myaddr[1] = val & 0xff;
val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
ic->ic_myaddr[2] = val >> 8;
ic->ic_myaddr[3] = val & 0xff;
val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
ic->ic_myaddr[4] = val >> 8;
ic->ic_myaddr[5] = val & 0xff;
/* set supported .11b rates */
ic->ic_sup_rates[IEEE80211_MODE_11B] = ipw_rateset_11b;
/* set supported .11b channels (1 through 14) */
for (i = 1; i <= 14; i++) {
ic->ic_channels[i].ic_freq =
ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
}
/* default to authmode OPEN */
sc->authmode = IEEE80211_AUTH_OPEN;
/* IBSS channel undefined for now */
ic->ic_ibss_chan = &ic->ic_channels[0];
ifp->if_softc = sc;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_init = ipw_init;
ifp->if_ioctl = ipw_ioctl;
ifp->if_start = ipw_start;
ifp->if_watchdog = ipw_watchdog;
IFQ_SET_READY(&ifp->if_snd);
bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
if_attach(ifp);
ieee80211_ifattach(ifp);
/* override state transition machine */
sc->sc_newstate = ic->ic_newstate;
ic->ic_newstate = ipw_newstate;
ieee80211_media_init(ifp, ipw_media_change, ipw_media_status);
#if NBPFILTER > 0
bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
sizeof (struct ieee80211_frame) + 64);
sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
sc->sc_txtap_len = sizeof sc->sc_txtapu;
sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
#endif
}
int
ipw_detach(struct device* self, int flags)
{
struct ipw_softc *sc = (struct ipw_softc *)self;
struct ifnet *ifp = &sc->sc_ic.ic_if;
ipw_stop(ifp, 1);
if (sc->flags & IPW_FLAG_FW_CACHED)
ipw_free_firmware(sc);
#if NBPFILTER > 0
bpfdetach(ifp);
#endif
ieee80211_ifdetach(ifp);
if_detach(ifp);
if (sc->sc_ih != NULL) {
pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
sc->sc_ih = NULL;
}
bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
return 0;
}
int
ipw_media_change(struct ifnet *ifp)
{
int error;
error = ieee80211_media_change(ifp);
if (error != ENETRESET)
return error;
if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
ipw_init(ifp);
return 0;
}
void
ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
{
struct ipw_softc *sc = ifp->if_softc;
struct ieee80211com *ic = &sc->sc_ic;
#define N(a) (sizeof (a) / sizeof (a[0]))
static const struct {
u_int32_t val;
int rate;
} rates[] = {
{ IPW_RATE_DS1, 2 },
{ IPW_RATE_DS2, 4 },
{ IPW_RATE_DS5, 11 },
{ IPW_RATE_DS11, 22 },
};
u_int32_t val;
int rate, i;
imr->ifm_status = IFM_AVALID;
imr->ifm_active = IFM_IEEE80211;
if (ic->ic_state == IEEE80211_S_RUN)
imr->ifm_status |= IFM_ACTIVE;
/* read current transmission rate from adapter */
val = ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE);
val &= 0xf;
/* convert rate to 802.11 rate */
for (i = 0; i < N(rates) && rates[i].val != val; i++);
rate = (i < N(rates)) ? rates[i].rate : 0;
imr->ifm_active |= IFM_IEEE80211_11B;
imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
switch (ic->ic_opmode) {
case IEEE80211_M_STA:
break;
case IEEE80211_M_IBSS:
imr->ifm_active |= IFM_IEEE80211_ADHOC;
break;
case IEEE80211_M_MONITOR:
imr->ifm_active |= IFM_IEEE80211_MONITOR;
break;
case IEEE80211_M_AHDEMO:
case IEEE80211_M_HOSTAP:
/* should not get there */
break;
}
#undef N
}
int
ipw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
{
struct ipw_softc *sc = ic->ic_softc;
struct ieee80211_node *ni = ic->ic_bss;
u_int32_t val, len;
switch (nstate) {
case IEEE80211_S_RUN:
len = IEEE80211_NWID_LEN;
ipw_read_table2(sc, IPW_INFO_CURRENT_SSID, ni->ni_essid, &len);
ni->ni_esslen = len;
val = ipw_read_table1(sc, IPW_INFO_CURRENT_CHANNEL);
ni->ni_chan = &ic->ic_channels[val];
DELAY(100); /* firmware needs a short delay here */
len = IEEE80211_ADDR_LEN;
ipw_read_table2(sc, IPW_INFO_CURRENT_BSSID, ni->ni_bssid, &len);
break;
case IEEE80211_S_INIT:
case IEEE80211_S_SCAN:
case IEEE80211_S_AUTH:
case IEEE80211_S_ASSOC:
break;
}
ic->ic_state = nstate;
return 0;
}
/*
* Read 16 bits at address 'addr' from the Microwire EEPROM.
* DON'T PLAY WITH THIS CODE UNLESS YOU KNOW *EXACTLY* WHAT YOU'RE DOING!
*/
u_int16_t
ipw_read_prom_word(struct ipw_softc *sc, u_int8_t addr)
{
u_int32_t tmp;
u_int16_t val;
int n;
/* Clock C once before the first command */
IPW_EEPROM_CTL(sc, 0);
IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
/* Write start bit (1) */
IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
/* Write READ opcode (10) */
IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
/* Write address A7-A0 */
for (n = 7; n >= 0; n--) {
IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
(((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
(((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
}
IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
/* Read data Q15-Q0 */
val = 0;
for (n = 15; n >= 0; n--) {
IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
}
IPW_EEPROM_CTL(sc, 0);
/* Clear Chip Select and clock C */
IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
IPW_EEPROM_CTL(sc, 0);
IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
return letoh16(val);
}
void
ipw_command_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
{
struct ipw_cmd *cmd;
bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof (struct ipw_cmd),
BUS_DMASYNC_POSTREAD);
cmd = mtod(sbuf->m, struct ipw_cmd *);
DPRINTFN(2, ("RX!CMD!%u!%u!%u!%u!%u\n",
letoh32(cmd->type), letoh32(cmd->subtype), letoh32(cmd->seq),
letoh32(cmd->len), letoh32(cmd->status)));
wakeup(sc);
}
void
ipw_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
{
struct ieee80211com *ic = &sc->sc_ic;
u_int32_t state;
bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof state,
BUS_DMASYNC_POSTREAD);
state = letoh32(*mtod(sbuf->m, u_int32_t *));
DPRINTFN(2, ("RX!NEWSTATE!%u\n", state));
switch (state) {
case IPW_STATE_ASSOCIATED:
ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
break;
case IPW_STATE_SCANNING:
ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
break;
case IPW_STATE_ASSOCIATION_LOST:
ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
break;
case IPW_STATE_RADIO_DISABLED:
/* XXX should turn the interface down */
break;
}
}
void
ipw_data_intr(struct ipw_softc *sc, struct ipw_status *status,
struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct mbuf *m;
struct ieee80211_frame *wh;
struct ieee80211_node *ni;
int error;
DPRINTFN(5, ("RX!DATA!%u!%u\n", letoh32(status->len), status->rssi));
bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, letoh32(status->len),
BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(sc->sc_dmat, sbuf->map);
/* Finalize mbuf */
m = sbuf->m;
m->m_pkthdr.rcvif = ifp;
m->m_pkthdr.len = m->m_len = letoh32(status->len);
#if NBPFILTER > 0
if (sc->sc_drvbpf != NULL) {
struct mbuf mb;
struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
tap->wr_flags = 0;
tap->wr_antsignal = status->rssi;
tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
M_DUP_PKTHDR(&mb, m);
mb.m_data = (caddr_t)tap;
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
wh = mtod(m, struct ieee80211_frame *);
ni = ieee80211_find_rxnode(ic, wh);
/* Send it up to the upper layer */
ieee80211_input(ifp, m, ni, status->rssi, 0);
if (ni == ic->ic_bss)
ieee80211_unref_node(&ni);
else
ieee80211_free_node(ic, ni);
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
printf("%s: could not allocate rx mbuf\n",
sc->sc_dev.dv_xname);
return;
}
MCLGET(m, M_DONTWAIT);
if (!(m->m_flags & M_EXT)) {
m_freem(m);
printf("%s: could not allocate rx mbuf cluster\n",
sc->sc_dev.dv_xname);
return;
}
error = bus_dmamap_load(sc->sc_dmat, sbuf->map, mtod(m, void *),
MCLBYTES, NULL, BUS_DMA_NOWAIT);
if (error != 0) {
printf("%s: could not map rxbuf dma memory\n",
sc->sc_dev.dv_xname);
m_freem(m);
return;
}
sbuf->m = m;
sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
}
void
ipw_notification_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
{
DPRINTFN(2, ("RX!NOTIFICATION\n"));
}
void
ipw_rx_intr(struct ipw_softc *sc)
{
struct ipw_status *status;
struct ipw_soft_bd *sbd;
struct ipw_soft_buf *sbuf;
u_int32_t r, i;
r = CSR_READ_4(sc, IPW_CSR_RX_READ_INDEX);
for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
BUS_DMASYNC_POSTREAD);
bus_dmamap_sync(sc->sc_dmat, sc->status_map,
i * sizeof (struct ipw_status), sizeof (struct ipw_status),
BUS_DMASYNC_POSTREAD);
status = &sc->status_list[i];
sbd = &sc->srbd_list[i];
sbuf = sbd->priv;
switch (letoh16(status->code) & 0xf) {
case IPW_STATUS_CODE_COMMAND:
ipw_command_intr(sc, sbuf);
break;
case IPW_STATUS_CODE_NEWSTATE:
ipw_newstate_intr(sc, sbuf);
break;
case IPW_STATUS_CODE_DATA_802_3:
case IPW_STATUS_CODE_DATA_802_11:
ipw_data_intr(sc, status, sbd, sbuf);
break;
case IPW_STATUS_CODE_NOTIFICATION:
ipw_notification_intr(sc, sbuf);
break;
default:
printf("%s: unknown status code %u\n",
sc->sc_dev.dv_xname, letoh16(status->code));
}
sbd->bd->flags = 0;
bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
BUS_DMASYNC_PREWRITE);
}
/* Tell the firmware what we have processed */
sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
CSR_WRITE_4(sc, IPW_CSR_RX_WRITE_INDEX, sc->rxcur);
}
void
ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
{
struct ieee80211com *ic;
struct ipw_soft_hdr *shdr;
struct ipw_soft_buf *sbuf;
switch (sbd->type) {
case IPW_SBD_TYPE_COMMAND:
bus_dmamap_unload(sc->sc_dmat, sc->cmd_map);
break;
case IPW_SBD_TYPE_HEADER:
shdr = sbd->priv;
bus_dmamap_unload(sc->sc_dmat, shdr->map);
TAILQ_INSERT_TAIL(&sc->sc_free_shdr, shdr, next);
break;
case IPW_SBD_TYPE_DATA:
ic = &sc->sc_ic;
sbuf = sbd->priv;
bus_dmamap_unload(sc->sc_dmat, sbuf->map);
m_freem(sbuf->m);
if (sbuf->ni != NULL && sbuf->ni != ic->ic_bss)
ieee80211_free_node(ic, sbuf->ni);
/* kill watchdog timer */
sc->sc_tx_timer = 0;
TAILQ_INSERT_TAIL(&sc->sc_free_sbuf, sbuf, next);
break;
}
sbd->type = IPW_SBD_TYPE_NOASSOC;
}
void
ipw_tx_intr(struct ipw_softc *sc)
{
struct ifnet *ifp = &sc->sc_ic.ic_if;
u_int32_t r, i;
r = CSR_READ_4(sc, IPW_CSR_TX_READ_INDEX);
for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD)
ipw_release_sbd(sc, &sc->stbd_list[i]);
/* Remember what the firmware has processed */
sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
/* Call start() since some buffer descriptors have been released */
ifp->if_flags &= ~IFF_OACTIVE;
(*ifp->if_start)(ifp);
}
int
ipw_intr(void *arg)
{
struct ipw_softc *sc = arg;
u_int32_t r;
if ((r = CSR_READ_4(sc, IPW_CSR_INTR)) == 0)
return 0;
/* Disable interrupts */
CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
DPRINTFN(8, ("INTR!0x%08x\n", r));
if (r & IPW_INTR_RX_TRANSFER)
ipw_rx_intr(sc);
if (r & IPW_INTR_TX_TRANSFER)
ipw_tx_intr(sc);
if (r & IPW_INTR_FW_INIT_DONE) {
if (!(r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)))
wakeup(sc);
}
if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
printf("%s: fatal error\n", sc->sc_dev.dv_xname);
ipw_stop(&sc->sc_ic.ic_if, 1);
}
/* Acknowledge interrupts */
CSR_WRITE_4(sc, IPW_CSR_INTR, r);
/* Re-enable interrupts */
CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
return 0;
}
int
ipw_cmd(struct ipw_softc *sc, u_int32_t type, void *data, u_int32_t len)
{
struct ipw_soft_bd *sbd;
int error;
sbd = &sc->stbd_list[sc->txcur];
error = bus_dmamap_load(sc->sc_dmat, sc->cmd_map, sc->cmd,
sizeof (struct ipw_cmd), NULL, BUS_DMA_NOWAIT);
if (error != 0) {
printf("%s: could not map cmd dma memory\n",
sc->sc_dev.dv_xname);
return error;
}
sc->cmd->type = htole32(type);
sc->cmd->subtype = htole32(0);
sc->cmd->len = htole32(len);
sc->cmd->seq = htole32(0);
if (data != NULL)
bcopy(data, sc->cmd->data, len);
sbd->type = IPW_SBD_TYPE_COMMAND;
sbd->bd->physaddr = htole32(sc->cmd_map->dm_segs[0].ds_addr);
sbd->bd->len = htole32(sizeof (struct ipw_cmd));
sbd->bd->nfrag = 1;
sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
IPW_BD_FLAG_TX_LAST_FRAGMENT;
bus_dmamap_sync(sc->sc_dmat, sc->cmd_map, 0, sizeof (struct ipw_cmd),
BUS_DMASYNC_PREWRITE);
bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
sc->txcur * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
BUS_DMASYNC_PREWRITE);
sc->txcur = (sc->txcur + 1) % IPW_NTBD;
CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
DPRINTFN(2, ("TX!CMD!%u!%u!%u!%u\n", type, 0, 0, len));
/* Wait at most one second for command to complete */
return tsleep(sc, 0, "ipwcmd", hz);
}
int
ipw_tx_start(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node *ni)
{
struct ipw_softc *sc = ifp->if_softc;
struct ieee80211com *ic = &sc->sc_ic;
struct ieee80211_frame *wh;
struct ipw_soft_bd *sbd;
struct ipw_soft_hdr *shdr;
struct ipw_soft_buf *sbuf;
int error, i;
if (ic->ic_flags & IEEE80211_F_WEPON) {
m = ieee80211_wep_crypt(ifp, m, 1);
if (m == NULL)
return ENOBUFS;
}
#if NBPFILTER > 0
if (sc->sc_drvbpf != NULL) {
struct mbuf mb;
struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
tap->wt_flags = 0;
tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
M_DUP_PKTHDR(&mb, m);
mb.m_data = (caddr_t)tap;
mb.m_len = sc->sc_txtap_len;
mb.m_next = m;
mb.m_pkthdr.len += mb.m_len;
bpf_mtap(sc->sc_drvbpf, &mb);
}
#endif
wh = mtod(m, struct ieee80211_frame *);
shdr = TAILQ_FIRST(&sc->sc_free_shdr);
sbuf = TAILQ_FIRST(&sc->sc_free_sbuf);
shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
shdr->hdr.subtype = htole32(0);
shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
shdr->hdr.encrypt = 0;
shdr->hdr.keyidx = 0;
shdr->hdr.keysz = 0;
shdr->hdr.fragmentsz = htole16(0);
IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
if (ic->ic_opmode == IEEE80211_M_STA)
IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
else
IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
/* trim IEEE802.11 header */
m_adj(m, sizeof (struct ieee80211_frame));
/*
* We need to map the mbuf first to know how many buffer descriptors
* are needed for this transfer.
*/
error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, m, BUS_DMA_NOWAIT);
if (error != 0) {
printf("%s: could not map mbuf (error %d)\n",
sc->sc_dev.dv_xname, error);
m_freem(m);
return error;
}
error = bus_dmamap_load(sc->sc_dmat, shdr->map, &shdr->hdr,
sizeof (struct ipw_hdr), NULL, BUS_DMA_NOWAIT);
if (error != 0) {
printf("%s: could not map header (error %d)\n",
sc->sc_dev.dv_xname, error);
bus_dmamap_unload(sc->sc_dmat, sbuf->map);
m_freem(m);
return error;
}
TAILQ_REMOVE(&sc->sc_free_sbuf, sbuf, next);
TAILQ_REMOVE(&sc->sc_free_shdr, shdr, next);
sbd = &sc->stbd_list[sc->txcur];
sbd->type = IPW_SBD_TYPE_HEADER;
sbd->priv = shdr;
sbd->bd->physaddr = htole32(shdr->map->dm_segs[0].ds_addr);
sbd->bd->len = htole32(sizeof (struct ipw_hdr));
sbd->bd->nfrag = 1 + sbuf->map->dm_nsegs;
sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
DPRINTFN(5, ("TX!HDR!%u!%u!%u!%u", shdr->hdr.type, shdr->hdr.subtype,
shdr->hdr.encrypted, shdr->hdr.encrypt));
DPRINTFN(5, ("!%s", ether_sprintf(shdr->hdr.src_addr)));
DPRINTFN(5, ("!%s\n", ether_sprintf(shdr->hdr.dst_addr)));
bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
sc->txcur * sizeof (struct ipw_bd),
sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
sc->txcur = (sc->txcur + 1) % IPW_NTBD;
sbuf->m = m;
sbuf->ni = ni;
for (i = 0; i < sbuf->map->dm_nsegs; i++) {
sbd = &sc->stbd_list[sc->txcur];
sbd->bd->physaddr = htole32(sbuf->map->dm_segs[i].ds_addr);
sbd->bd->len = htole32(sbuf->map->dm_segs[i].ds_len);
sbd->bd->nfrag = 0; /* used only in first bd */
sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
if (i == sbuf->map->dm_nsegs - 1) {
sbd->type = IPW_SBD_TYPE_DATA;
sbd->priv = sbuf;
sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
} else {
sbd->type = IPW_SBD_TYPE_NOASSOC;
sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
}
DPRINTFN(5, ("TX!FRAG!%d!%d\n", i,
sbuf->map->dm_segs[i].ds_len));
bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
sc->txcur * sizeof (struct ipw_bd),
sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
sc->txcur = (sc->txcur + 1) % IPW_NTBD;
}
bus_dmamap_sync(sc->sc_dmat, shdr->map, 0, sizeof (struct ipw_hdr),
BUS_DMASYNC_PREWRITE);
bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, MCLBYTES,
BUS_DMASYNC_PREWRITE);
/* Inform firmware about this new packet */
CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
return 0;
}
void
ipw_start(struct ifnet *ifp)
{
struct ipw_softc *sc = ifp->if_softc;
struct ieee80211com *ic = &sc->sc_ic;
struct mbuf *m;
struct ieee80211_node *ni;
if (ic->ic_state != IEEE80211_S_RUN)
return;
for (;;) {
IF_DEQUEUE(&ifp->if_snd, m);
if (m == NULL)
break;
#if NBPFILTER > 0
if (ifp->if_bpf != NULL)
bpf_mtap(ifp->if_bpf, m);
#endif
m = ieee80211_encap(ifp, m, &ni);
if (m == NULL)
continue;
#if NBPFILTER > 0
if (ic->ic_rawbpf != NULL)
bpf_mtap(ic->ic_rawbpf, m);
#endif
if (ipw_tx_start(ifp, m, ni) != 0) {
if (ni != NULL && ni != ic->ic_bss)
ieee80211_free_node(ic, ni);
break;
}
/* start watchdog timer */
sc->sc_tx_timer = 5;
ifp->if_timer = 1;
}
}
void
ipw_watchdog(struct ifnet *ifp)
{
struct ipw_softc *sc = ifp->if_softc;
ifp->if_timer = 0;
if (sc->sc_tx_timer > 0) {
if (--sc->sc_tx_timer == 0) {
printf("%s: device timeout\n", sc->sc_dev.dv_xname);
ipw_stop(ifp, 1);
return;
}
ifp->if_timer = 1;
}
ieee80211_watchdog(ifp);
}
int
ipw_get_table1(struct ipw_softc *sc, u_int32_t *tbl)
{
u_int32_t i, size, buf[256];
if (!(sc->flags & IPW_FLAG_FW_INITED)) {
bzero(buf, sizeof buf);
return copyout(buf, tbl, sizeof buf);
}
CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
size = CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA);
for (i = 1; i < size; i++)
buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
return copyout(buf, tbl, size * sizeof (u_int32_t));
}
int
ipw_get_radio(struct ipw_softc *sc, int *ret)
{
int val;
val = (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED) ? 0 : 1;
return copyout(&val, ret, sizeof val);
}
int
ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
{
struct ipw_softc *sc = ifp->if_softc;
struct ifreq *ifr;
struct ifaddr *ifa;
int s, error = 0;
s = splnet();
switch (cmd) {
case SIOCSIFADDR:
ifa = (struct ifaddr *) data;
ifp->if_flags |= IFF_UP;
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
arp_ifinit(&sc->sc_ic.ic_ac, ifa);
ipw_init(ifp);
break;
#endif
default:
ipw_init(ifp);
}
break;
case SIOCSIFFLAGS:
if (ifp->if_flags & IFF_UP) {
if (!(ifp->if_flags & IFF_RUNNING))
ipw_init(ifp);
} else {
if (ifp->if_flags & IFF_RUNNING)
ipw_stop(ifp, 1);
}
break;
case SIOCGTABLE1:
ifr = (struct ifreq *)data;
error = ipw_get_table1(sc, (u_int32_t *)ifr->ifr_data);
break;
case SIOCGRADIO:
ifr = (struct ifreq *)data;
error = ipw_get_radio(sc, (int *)ifr->ifr_data);
break;
case SIOCSLOADFW:
/* only super-user can do that! */
if ((error = suser(curproc, 0)) != 0)
break;
ifr = (struct ifreq *)data;
error = ipw_cache_firmware(sc, ifr->ifr_data);
break;
case SIOCSKILLFW:
/* only super-user can do that! */
if ((error = suser(curproc, 0)) != 0)
break;
ipw_stop(ifp, 1);
ipw_free_firmware(sc);
break;
case SIOCG80211AUTH:
((struct ieee80211_auth *)data)->i_authtype = sc->authmode;
break;
case SIOCS80211AUTH:
/* only super-user can do that! */
if ((error = suser(curproc, 0)) != 0)
break;
sc->authmode = ((struct ieee80211_auth *)data)->i_authtype;
break;
default:
error = ieee80211_ioctl(ifp, cmd, data);
}
if (error == ENETRESET && cmd != SIOCADDMULTI) {
if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
(IFF_UP | IFF_RUNNING))
ipw_init(ifp);
error = 0;
}
splx(s);
return error;
}
u_int32_t
ipw_read_table1(struct ipw_softc *sc, u_int32_t off)
{
return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
}
void
ipw_write_table1(struct ipw_softc *sc, u_int32_t off, u_int32_t info)
{
MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
}
int
ipw_read_table2(struct ipw_softc *sc, u_int32_t off, void *buf, u_int32_t *len)
{
u_int32_t addr, info;
u_int16_t count, size;
u_int32_t total;
/* addr[4] + count[2] + size[2] */
addr = MEM_READ_4(sc, sc->table2_base + off);
info = MEM_READ_4(sc, sc->table2_base + off + 4);
count = info >> 16;
size = info & 0xffff;
total = count * size;
if (total > *len) {
*len = total;
return EINVAL;
}
*len = total;
ipw_read_mem_1(sc, addr, buf, total);
return 0;
}
int
ipw_tx_init(struct ipw_softc *sc)
{
char *errmsg;
struct ipw_bd *bd;
struct ipw_soft_bd *sbd;
struct ipw_soft_hdr *shdr;
struct ipw_soft_buf *sbuf;
int error, i, nsegs;
/* Allocate transmission buffer descriptors */
error = bus_dmamap_create(sc->sc_dmat, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0,
BUS_DMA_NOWAIT, &sc->tbd_map);
if (error != 0) {
errmsg = "could not create tbd dma map";
goto fail;
}
error = bus_dmamem_alloc(sc->sc_dmat, IPW_TBD_SZ, PAGE_SIZE, 0,
&sc->tbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not allocate tbd dma memory";
goto fail;
}
error = bus_dmamem_map(sc->sc_dmat, &sc->tbd_seg, nsegs, IPW_TBD_SZ,
(caddr_t *)&sc->tbd_list, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not map tbd dma memory";
goto fail;
}
error = bus_dmamap_load(sc->sc_dmat, sc->tbd_map, sc->tbd_list,
IPW_TBD_SZ, NULL, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not load tbd dma memory";
goto fail;
}
sc->stbd_list = malloc(IPW_NTBD * sizeof (struct ipw_soft_bd),
M_DEVBUF, M_NOWAIT);
if (sc->stbd_list == NULL) {
errmsg = "could not allocate soft tbd";
error = ENOMEM;
goto fail;
}
sbd = sc->stbd_list;
bd = sc->tbd_list;
for (i = 0; i < IPW_NTBD; i++, sbd++, bd++) {
sbd->type = IPW_SBD_TYPE_NOASSOC;
sbd->bd = bd;
}
CSR_WRITE_4(sc, IPW_CSR_TX_BD_BASE, sc->tbd_map->dm_segs[0].ds_addr);
CSR_WRITE_4(sc, IPW_CSR_TX_BD_SIZE, IPW_NTBD);
CSR_WRITE_4(sc, IPW_CSR_TX_READ_INDEX, 0);
CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, 0);
sc->txold = IPW_NTBD - 1; /* latest bd index ack'ed by firmware */
sc->txcur = 0; /* bd index to write to */
/* Allocate a DMA-able command */
error = bus_dmamap_create(sc->sc_dmat, sizeof (struct ipw_cmd), 1,
sizeof (struct ipw_cmd), 0, BUS_DMA_NOWAIT, &sc->cmd_map);
if (error != 0) {
errmsg = "could not create cmd dma map";
goto fail;
}
error = bus_dmamem_alloc(sc->sc_dmat, sizeof (struct ipw_cmd),
PAGE_SIZE, 0, &sc->cmd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not allocate cmd dma memory";
goto fail;
}
error = bus_dmamem_map(sc->sc_dmat, &sc->cmd_seg, nsegs,
sizeof (struct ipw_cmd), (caddr_t *)&sc->cmd, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not map cmd dma memory";
goto fail;
}
/* Allocate a pool of DMA-able headers */
sc->shdr_list = malloc(IPW_NDATA * sizeof (struct ipw_soft_hdr),
M_DEVBUF, M_NOWAIT);
if (sc->shdr_list == NULL) {
errmsg = "could not allocate soft hdr";
error = ENOMEM;
goto fail;
}
TAILQ_INIT(&sc->sc_free_shdr);
for (i = 0, shdr = sc->shdr_list; i < IPW_NDATA; i++, shdr++) {
error = bus_dmamap_create(sc->sc_dmat,
sizeof (struct ipw_soft_hdr), 1,
sizeof (struct ipw_soft_hdr), 0, BUS_DMA_NOWAIT,
&shdr->map);
if (error != 0) {
errmsg = "could not create hdr dma map";
goto fail;
}
TAILQ_INSERT_TAIL(&sc->sc_free_shdr, shdr, next);
}
/* Allocate a pool of DMA-able buffers */
sc->tx_sbuf_list = malloc(IPW_NDATA * sizeof (struct ipw_soft_buf),
M_DEVBUF, M_NOWAIT);
if (sc->tx_sbuf_list == NULL) {
errmsg = "could not allocate soft txbuf";
error = ENOMEM;
goto fail;
}
TAILQ_INIT(&sc->sc_free_sbuf);
for (i = 0, sbuf = sc->tx_sbuf_list; i < IPW_NDATA; i++, sbuf++) {
error = bus_dmamap_create(sc->sc_dmat, IPW_NDATA * MCLBYTES,
IPW_NDATA, MCLBYTES, 0, BUS_DMA_NOWAIT, &sbuf->map);
if (error != 0) {
errmsg = "could not create txbuf dma map";
goto fail;
}
TAILQ_INSERT_TAIL(&sc->sc_free_sbuf, sbuf, next);
}
return 0;
fail: printf("%s: %s\n", sc->sc_dev.dv_xname, errmsg);
ipw_tx_stop(sc);
return error;
}
void
ipw_tx_stop(struct ipw_softc *sc)
{
struct ipw_soft_hdr *shdr;
struct ipw_soft_buf *sbuf;
int i;
if (sc->tbd_map != NULL) {
if (sc->tbd_list != NULL) {
bus_dmamap_unload(sc->sc_dmat, sc->tbd_map);
bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->tbd_list,
IPW_TBD_SZ);
bus_dmamem_free(sc->sc_dmat, &sc->tbd_seg, 1);
sc->tbd_list = NULL;
}
bus_dmamap_destroy(sc->sc_dmat, sc->tbd_map);
sc->tbd_map = NULL;
}
if (sc->stbd_list != NULL) {
for (i = 0; i < IPW_NTBD; i++)
ipw_release_sbd(sc, &sc->stbd_list[i]);
free(sc->stbd_list, M_DEVBUF);
sc->stbd_list = NULL;
}
if (sc->cmd_map != NULL) {
if (sc->cmd != NULL) {
bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->cmd,
sizeof (struct ipw_cmd));
bus_dmamem_free(sc->sc_dmat, &sc->cmd_seg, 1);
sc->cmd = NULL;
}
bus_dmamap_destroy(sc->sc_dmat, sc->cmd_map);
sc->cmd_map = NULL;
}
if (sc->shdr_list != NULL) {
TAILQ_FOREACH(shdr, &sc->sc_free_shdr, next)
bus_dmamap_destroy(sc->sc_dmat, shdr->map);
free(sc->shdr_list, M_DEVBUF);
sc->shdr_list = NULL;
}
if (sc->tx_sbuf_list != NULL) {
TAILQ_FOREACH(sbuf, &sc->sc_free_sbuf, next)
bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
free(sc->tx_sbuf_list, M_DEVBUF);
sc->tx_sbuf_list = NULL;
}
}
int
ipw_rx_init(struct ipw_softc *sc)
{
char *errmsg;
struct ipw_bd *bd;
struct ipw_soft_bd *sbd;
struct ipw_soft_buf *sbuf;
int error, i, nsegs;
/* Allocate reception buffer descriptors */
error = bus_dmamap_create(sc->sc_dmat, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0,
BUS_DMA_NOWAIT, &sc->rbd_map);
if (error != 0) {
errmsg = "could not create rbd dma map";
goto fail;
}
error = bus_dmamem_alloc(sc->sc_dmat, IPW_RBD_SZ, PAGE_SIZE, 0,
&sc->rbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not allocate rbd dma memory";
goto fail;
}
error = bus_dmamem_map(sc->sc_dmat, &sc->rbd_seg, nsegs, IPW_RBD_SZ,
(caddr_t *)&sc->rbd_list, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not map rbd dma memory";
goto fail;
}
error = bus_dmamap_load(sc->sc_dmat, sc->rbd_map, sc->rbd_list,
IPW_RBD_SZ, NULL, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not load rbd dma memory";
goto fail;
}
sc->srbd_list = malloc(IPW_NRBD * sizeof (struct ipw_soft_bd),
M_DEVBUF, M_NOWAIT);
if (sc->srbd_list == NULL) {
errmsg = "could not allocate soft rbd";
error = ENOMEM;
goto fail;
}
sbd = sc->srbd_list;
bd = sc->rbd_list;
for (i = 0; i < IPW_NRBD; i++, sbd++, bd++) {
sbd->type = IPW_SBD_TYPE_NOASSOC;
sbd->bd = bd;
}
CSR_WRITE_4(sc, IPW_CSR_RX_BD_BASE, sc->rbd_map->dm_segs[0].ds_addr);
CSR_WRITE_4(sc, IPW_CSR_RX_BD_SIZE, IPW_NRBD);
CSR_WRITE_4(sc, IPW_CSR_RX_READ_INDEX, 0);
CSR_WRITE_4(sc, IPW_CSR_RX_WRITE_INDEX, IPW_NRBD - 1);
sc->rxcur = IPW_NRBD - 1; /* latest bd index I've read */
/* Allocate status descriptors */
error = bus_dmamap_create(sc->sc_dmat, IPW_STATUS_SZ, 1, IPW_STATUS_SZ,
0, BUS_DMA_NOWAIT, &sc->status_map);
if (error != 0) {
errmsg = "could not create status dma map";
goto fail;
}
error = bus_dmamem_alloc(sc->sc_dmat, IPW_STATUS_SZ, PAGE_SIZE, 0,
&sc->status_seg, 1, &nsegs, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not allocate status dma memory";
goto fail;
}
error = bus_dmamem_map(sc->sc_dmat, &sc->status_seg, nsegs,
IPW_STATUS_SZ, (caddr_t *)&sc->status_list, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not map status dma memory";
goto fail;
}
error = bus_dmamap_load(sc->sc_dmat, sc->status_map, sc->status_list,
IPW_STATUS_SZ, NULL, BUS_DMA_NOWAIT);
if (error != 0) {
errmsg = "could not load status dma memory";
goto fail;
}
CSR_WRITE_4(sc, IPW_CSR_RX_STATUS_BASE,
sc->status_map->dm_segs[0].ds_addr);
sc->rx_sbuf_list = malloc(IPW_NRBD * sizeof (struct ipw_soft_buf),
M_DEVBUF, M_NOWAIT);
if (sc->rx_sbuf_list == NULL) {
errmsg = "could not allocate soft rxbuf";
error = ENOMEM;
goto fail;
}
sbuf = sc->rx_sbuf_list;
sbd = sc->srbd_list;
for (i = 0; i < IPW_NRBD; i++, sbuf++, sbd++) {
MGETHDR(sbuf->m, M_DONTWAIT, MT_DATA);
if (sbuf->m == NULL) {
errmsg = "could not allocate rx mbuf";
error = ENOMEM;
goto fail;
}
MCLGET(sbuf->m, M_DONTWAIT);
if (!(sbuf->m->m_flags & M_EXT)) {
m_freem(sbuf->m);
errmsg = "could not allocate rx mbuf cluster";
error = ENOMEM;
goto fail;
}
error = bus_dmamap_create(sc->sc_dmat, IPW_NRBD * MCLBYTES,
IPW_NRBD, MCLBYTES, 0, BUS_DMA_NOWAIT, &sbuf->map);
if (error != 0) {
m_freem(sbuf->m);
errmsg = "could not create rxbuf dma map";
goto fail;
}
error = bus_dmamap_load(sc->sc_dmat, sbuf->map,
mtod(sbuf->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
if (error != 0) {
bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
m_freem(sbuf->m);
errmsg = "could not map rxbuf dma memory";
goto fail;
}
sbd->type = IPW_SBD_TYPE_DATA;
sbd->priv = sbuf;
sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
sbd->bd->len = htole32(MCLBYTES);
}
return 0;
fail: printf("%s: %s\n", sc->sc_dev.dv_xname, errmsg);
ipw_rx_stop(sc);
return error;
}
void
ipw_rx_stop(struct ipw_softc *sc)
{
struct ipw_soft_bd *sbd;
struct ipw_soft_buf *sbuf;
int i;
if (sc->rbd_map != NULL) {
if (sc->rbd_list != NULL) {
bus_dmamap_unload(sc->sc_dmat, sc->rbd_map);
bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rbd_list,
IPW_RBD_SZ);
bus_dmamem_free(sc->sc_dmat, &sc->rbd_seg, 1);
sc->rbd_list = NULL;
}
bus_dmamap_destroy(sc->sc_dmat, sc->rbd_map);
sc->rbd_map = NULL;
}
if (sc->status_map != NULL) {
if (sc->status_list != NULL) {
bus_dmamap_unload(sc->sc_dmat, sc->status_map);
bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->status_list,
IPW_STATUS_SZ);
bus_dmamem_free(sc->sc_dmat, &sc->status_seg, 1);
sc->status_list = NULL;
}
bus_dmamap_destroy(sc->sc_dmat, sc->status_map);
sc->status_map = NULL;
}
if (sc->srbd_list != NULL) {
for (i = 0, sbd = sc->srbd_list; i < IPW_NRBD; i++, sbd++) {
if (sbd->type == IPW_SBD_TYPE_NOASSOC)
continue;
sbuf = sbd->priv;
bus_dmamap_unload(sc->sc_dmat, sbuf->map);
bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
m_freem(sbuf->m);
}
free(sc->srbd_list, M_DEVBUF);
sc->srbd_list = NULL;
}
if (sc->rx_sbuf_list != NULL) {
free(sc->rx_sbuf_list, M_DEVBUF);
sc->rx_sbuf_list = NULL;
}
}
void
ipw_stop_master(struct ipw_softc *sc)
{
int ntries;
/* Disable interrupts */
CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
for (ntries = 0; ntries < 5; ntries++) {
if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
break;
DELAY(10);
}
if (ntries == 5)
printf("%s: timeout waiting for master\n",
sc->sc_dev.dv_xname);
CSR_WRITE_4(sc, IPW_CSR_RST, CSR_READ_4(sc, IPW_CSR_RST) |
IPW_RST_PRINCETON_RESET);
sc->flags &= ~IPW_FLAG_FW_INITED;
}
int
ipw_reset(struct ipw_softc *sc)
{
int ntries;
ipw_stop_master(sc);
/* Move adapter to D0 state */
CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
IPW_CTL_INIT);
/* Wait for clock stabilization */
for (ntries = 0; ntries < 1000; ntries++) {
if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
break;
DELAY(200);
}
if (ntries == 1000)
return EIO;
CSR_WRITE_4(sc, IPW_CSR_RST, CSR_READ_4(sc, IPW_CSR_RST) |
IPW_RST_SW_RESET);
DELAY(10);
CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
IPW_CTL_INIT);
return 0;
}
int
ipw_load_ucode(struct ipw_softc *sc, u_char *uc, int size)
{
int ntries;
MEM_WRITE_4(sc, 0x003000e0, 0x80000000);
CSR_WRITE_4(sc, IPW_CSR_RST, 0);
MEM_WRITE_2(sc, 0x220000, 0x0703);
MEM_WRITE_2(sc, 0x220000, 0x0707);
MEM_WRITE_1(sc, 0x210014, 0x72);
MEM_WRITE_1(sc, 0x210014, 0x72);
MEM_WRITE_1(sc, 0x210000, 0x40);
MEM_WRITE_1(sc, 0x210000, 0x00);
MEM_WRITE_1(sc, 0x210000, 0x40);
MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
MEM_WRITE_1(sc, 0x210000, 0x00);
MEM_WRITE_1(sc, 0x210000, 0x00);
MEM_WRITE_1(sc, 0x210000, 0x80);
MEM_WRITE_2(sc, 0x220000, 0x0703);
MEM_WRITE_2(sc, 0x220000, 0x0707);
MEM_WRITE_1(sc, 0x210014, 0x72);
MEM_WRITE_1(sc, 0x210014, 0x72);
MEM_WRITE_1(sc, 0x210000, 0x00);
MEM_WRITE_1(sc, 0x210000, 0x80);
for (ntries = 0; ntries < 10; ntries++) {
if (MEM_READ_1(sc, 0x210000) & 1)
break;
DELAY(10);
}
if (ntries == 10)
return EIO;
MEM_WRITE_4(sc, 0x003000e0, 0);
return 0;
}
/* set of macros to handle unaligned little endian data in firmware image */
#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
#define GETLE16(p) ((p)[0] | (p)[1] << 8)
int
ipw_load_firmware(struct ipw_softc *sc, u_char *fw, int size)
{
u_char *p, *end;
u_int32_t dst;
u_int16_t len;
int error;
p = fw;
end = fw + size;
while (p < end) {
if (p + 6 > end)
return EINVAL;
dst = GETLE32(p); p += 4;
len = GETLE16(p); p += 2;
if (p + len > end)
return EINVAL;
ipw_write_mem_1(sc, dst, p, len);
p += len;
}
CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
IPW_IO_LED_OFF);
/* Allow interrupts so we know when the firmware is inited */
CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
/* Tell the adapter to initialize the firmware */
CSR_WRITE_4(sc, IPW_CSR_RST, 0);
CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
IPW_CTL_ALLOW_STANDBY);
/* Wait at most one second for firmware initialization to complete */
if ((error = tsleep(sc, 0, "ipwinit", hz)) != 0) {
printf("%s: timeout waiting for firmware initialization to "
"complete\n", sc->sc_dev.dv_xname);
return error;
}
CSR_WRITE_4(sc, IPW_CSR_IO, CSR_READ_4(sc, IPW_CSR_IO) |
IPW_IO_GPIO1_MASK | IPW_IO_GPIO3_MASK);
return 0;
}
/*
* Store firmware into kernel memory so we can download it when we need to,
* e.g when the adapter wakes up from suspend mode.
*/
int
ipw_cache_firmware(struct ipw_softc *sc, void *data)
{
struct ipw_firmware *fw = &sc->fw;
struct ipw_firmware_hdr hdr;
u_char *p = data;
int error;
if (sc->flags & IPW_FLAG_FW_CACHED)
ipw_free_firmware(sc);
if ((error = copyin(data, &hdr, sizeof hdr)) != 0)
goto fail1;
fw->main_size = letoh32(hdr.main_size);
fw->ucode_size = letoh32(hdr.ucode_size);
p += sizeof hdr;
fw->main = malloc(fw->main_size, M_DEVBUF, M_NOWAIT);
if (fw->main == NULL) {
error = ENOMEM;
goto fail1;
}
fw->ucode = malloc(fw->ucode_size, M_DEVBUF, M_NOWAIT);
if (fw->ucode == NULL) {
error = ENOMEM;
goto fail2;
}
if ((error = copyin(p, fw->main, fw->main_size)) != 0)
goto fail3;
p += fw->main_size;
if ((error = copyin(p, fw->ucode, fw->ucode_size)) != 0)
goto fail3;
DPRINTF(("Firmware cached: main %u, ucode %u\n", fw->main_size,
fw->ucode_size));
sc->flags |= IPW_FLAG_FW_CACHED;
return 0;
fail3: free(fw->ucode, M_DEVBUF);
fail2: free(fw->main, M_DEVBUF);
fail1: return error;
}
void
ipw_free_firmware(struct ipw_softc *sc)
{
free(sc->fw.main, M_DEVBUF);
free(sc->fw.ucode, M_DEVBUF);
sc->flags &= ~IPW_FLAG_FW_CACHED;
}
int
ipw_config(struct ipw_softc *sc)
{
struct ieee80211com *ic = &sc->sc_ic;
struct ifnet *ifp = &ic->ic_if;
struct ipw_security security;
struct ieee80211_wepkey *k;
struct ipw_wep_key wepkey;
struct ipw_scan_options options;
struct ipw_configuration config;
u_int32_t data;
int error, i;
switch (ic->ic_opmode) {
case IEEE80211_M_STA:
case IEEE80211_M_HOSTAP:
data = htole32(IPW_MODE_BSS);
break;
case IEEE80211_M_IBSS:
case IEEE80211_M_AHDEMO:
data = htole32(IPW_MODE_IBSS);
break;
case IEEE80211_M_MONITOR:
data = htole32(IPW_MODE_MONITOR);
break;
}
DPRINTF(("Setting adapter mode to %u\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
if (error != 0)
return error;
if (ic->ic_opmode == IEEE80211_M_IBSS ||
ic->ic_opmode == IEEE80211_M_MONITOR) {
data = htole32(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
DPRINTF(("Setting adapter channel to %u\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
if (error != 0)
return error;
}
if (ic->ic_opmode == IEEE80211_M_MONITOR) {
DPRINTF(("Enabling adapter\n"));
return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
}
DPRINTF(("Setting adapter MAC to %s\n", ether_sprintf(ic->ic_myaddr)));
IEEE80211_ADDR_COPY(((struct arpcom *)ifp)->ac_enaddr, ic->ic_myaddr);
IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), ic->ic_myaddr);
error = ipw_cmd(sc, IPW_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
IEEE80211_ADDR_LEN);
if (error != 0)
return error;
config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
IPW_CFG_PREAMBLE_LEN | IPW_CFG_802_1x_ENABLE);
if (ic->ic_opmode == IEEE80211_M_IBSS)
config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
if (ifp->if_flags & IFF_PROMISC)
config.flags |= htole32(IPW_CFG_PROMISCUOUS);
config.channels = htole32(0x3fff); /* channels 1-14 */
config.ibss_chan = htole32(0x7ff);
DPRINTF(("Setting adapter configuration 0x%08x\n", config.flags));
error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
if (error != 0)
return error;
data = htole32(0x3); /* 1, 2 */
DPRINTF(("Setting adapter basic tx rates to 0x%x\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
if (error != 0)
return error;
data = htole32(0xf); /* 1, 2, 5.5, 11 */
DPRINTF(("Setting adapter tx rates to 0x%x\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
if (error != 0)
return error;
data = htole32(IPW_POWER_MODE_CAM);
DPRINTF(("Setting adapter power mode to %u\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
if (error != 0)
return error;
if (ic->ic_opmode == IEEE80211_M_IBSS) {
data = htole32(ic->ic_txpower);
DPRINTF(("Setting adapter tx power index to %u\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
sizeof data);
if (error != 0)
return error;
}
data = htole32(ic->ic_rtsthreshold);
DPRINTF(("Setting adapter RTS threshold to %u\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
if (error != 0)
return error;
data = htole32(ic->ic_fragthreshold);
DPRINTF(("Setting adapter frag threshold to %u\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
if (error != 0)
return error;
#ifdef IPW_DEBUG
if (ipw_debug > 0) {
printf("Setting adapter ESSID to ");
ieee80211_print_essid(ic->ic_des_essid, ic->ic_des_esslen);
printf("\n");
}
#endif
error = ipw_cmd(sc, IPW_CMD_SET_ESSID, ic->ic_des_essid,
ic->ic_des_esslen);
if (error != 0)
return error;
/* no mandatory BSSID */
error = ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
if (error != 0)
return error;
if (ic->ic_flags & IEEE80211_F_DESBSSID) {
DPRINTF(("Setting adapter desired BSSID to %s\n",
ether_sprintf(ic->ic_des_bssid)));
error = ipw_cmd(sc, IPW_CMD_SET_DESIRED_BSSID,
ic->ic_des_bssid, IEEE80211_ADDR_LEN);
if (error != 0)
return error;
}
security.authmode = (sc->authmode == IEEE80211_AUTH_SHARED) ?
IPW_AUTH_SHARED : IPW_AUTH_OPEN;
security.ciphers = htole32(IPW_CIPHER_NONE);
security.version = htole16(0);
security.replay_counters_number = 0;
security.unicast_using_group = 0;
DPRINTF(("Setting adapter authmode to %u\n", security.authmode));
error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFORMATION, &security,
sizeof security);
if (error != 0)
return error;
if (ic->ic_flags & IEEE80211_F_WEPON) {
k = ic->ic_nw_keys;
for (i = 0; i < IEEE80211_WEP_NKID; i++, k++) {
if (k->wk_len == 0)
continue;
wepkey.idx = i;
wepkey.len = k->wk_len;
bzero(wepkey.key, sizeof wepkey.key);
bcopy(k->wk_key, wepkey.key, k->wk_len);
DPRINTF(("Setting wep key index %d len %d\n",
wepkey.idx, wepkey.len));
error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
sizeof wepkey);
if (error != 0)
return error;
}
data = htole32(ic->ic_wep_txkey);
DPRINTF(("Setting adapter tx key index to %u\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
sizeof data);
if (error != 0)
return error;
}
data = htole32((sc->sc_ic.ic_flags & IEEE80211_F_WEPON) ? 0x8 : 0);
DPRINTF(("Setting adapter wep flags to 0x%x\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
if (error != 0)
return error;
if (ic->ic_opmode == IEEE80211_M_IBSS ||
ic->ic_opmode == IEEE80211_M_HOSTAP) {
data = htole32(ic->ic_lintval);
DPRINTF(("Setting adapter beacon interval to %u\n", data));
error = ipw_cmd(sc, IPW_CMD_SET_BEACON_INTERVAL, &data,
sizeof data);
if (error != 0)
return error;
}
options.flags = htole32(0);
options.channels = htole32(0x3fff); /* scan channels 1-14 */
error = ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &options, sizeof options);
if (error != 0)
return error;
/* finally, enable adapter (start scanning for an access point) */
DPRINTF(("Enabling adapter\n"));
return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
}
int
ipw_init(struct ifnet *ifp)
{
struct ipw_softc *sc = ifp->if_softc;
struct ipw_firmware *fw = &sc->fw;
int error;
/* exit immediately if firmware has not been ioctl'd */
if (!(sc->flags & IPW_FLAG_FW_CACHED)) {
ifp->if_flags &= ~IFF_UP;
return EIO;
}
if ((error = ipw_reset(sc)) != 0) {
printf("%s: could not reset adapter\n", sc->sc_dev.dv_xname);
goto fail;
}
if ((error = ipw_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) {
printf("%s: could not load microcode\n", sc->sc_dev.dv_xname);
goto fail;
}
ipw_stop_master(sc);
if ((error = ipw_rx_init(sc)) != 0) {
printf("%s: could not initialize rx queue\n",
sc->sc_dev.dv_xname);
goto fail;
}
if ((error = ipw_tx_init(sc)) != 0) {
printf("%s: could not initialize tx queue\n",
sc->sc_dev.dv_xname);
goto fail;
}
if ((error = ipw_load_firmware(sc, fw->main, fw->main_size)) != 0) {
printf("%s: could not load firmware\n", sc->sc_dev.dv_xname);
goto fail;
}
sc->flags |= IPW_FLAG_FW_INITED;
/* Retrieve information tables base addresses */
sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
ipw_write_table1(sc, IPW_INFO_LOCK, 0);
if ((error = ipw_config(sc)) != 0) {
printf("%s: device configuration failed\n",
sc->sc_dev.dv_xname);
goto fail;
}
ifp->if_flags &= ~IFF_OACTIVE;
ifp->if_flags |= IFF_RUNNING;
return 0;
fail: ipw_stop(ifp, 0);
return error;
}
void
ipw_stop(struct ifnet *ifp, int disable)
{
struct ipw_softc *sc = ifp->if_softc;
struct ieee80211com *ic = &sc->sc_ic;
ipw_stop_master(sc);
CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
ipw_tx_stop(sc);
ipw_rx_stop(sc);
ifp->if_timer = 0;
ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
}
void
ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, u_int8_t *datap,
bus_size_t count)
{
for (; count > 0; offset++, datap++, count--) {
CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
}
}
void
ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, u_int8_t *datap,
bus_size_t count)
{
for (; count > 0; offset++, datap++, count--) {
CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
}
}
struct cfdriver ipw_cd = {
0, "ipw", DV_IFNET
};
|