wi.c revision 1.204 1 /* $NetBSD: wi.c,v 1.204 2005/07/06 23:58:14 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1997, 1998, 1999
41 * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by Bill Paul.
54 * 4. Neither the name of the author nor the names of any co-contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
68 * THE POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 /*
72 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD.
73 *
74 * Original FreeBSD driver written by Bill Paul <wpaul (at) ctr.columbia.edu>
75 * Electrical Engineering Department
76 * Columbia University, New York City
77 */
78
79 /*
80 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
81 * from Lucent. Unlike the older cards, the new ones are programmed
82 * entirely via a firmware-driven controller called the Hermes.
83 * Unfortunately, Lucent will not release the Hermes programming manual
84 * without an NDA (if at all). What they do release is an API library
85 * called the HCF (Hardware Control Functions) which is supposed to
86 * do the device-specific operations of a device driver for you. The
87 * publically available version of the HCF library (the 'HCF Light') is
88 * a) extremely gross, b) lacks certain features, particularly support
89 * for 802.11 frames, and c) is contaminated by the GNU Public License.
90 *
91 * This driver does not use the HCF or HCF Light at all. Instead, it
92 * programs the Hermes controller directly, using information gleaned
93 * from the HCF Light code and corresponding documentation.
94 *
95 * This driver supports both the PCMCIA and ISA versions of the
96 * WaveLAN/IEEE cards. Note however that the ISA card isn't really
97 * anything of the sort: it's actually a PCMCIA bridge adapter
98 * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
99 * inserted. Consequently, you need to use the pccard support for
100 * both the ISA and PCMCIA adapters.
101 */
102
103 /*
104 * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the
105 * Oslo IETF plenary meeting.
106 */
107
108 #include <sys/cdefs.h>
109 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.204 2005/07/06 23:58:14 dyoung Exp $");
110
111 #define WI_HERMES_AUTOINC_WAR /* Work around data write autoinc bug. */
112 #define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
113 #undef WI_HISTOGRAM
114 #undef WI_RING_DEBUG
115 #define STATIC static
116
117 #include "bpfilter.h"
118
119 #include <sys/param.h>
120 #include <sys/systm.h>
121 #include <sys/callout.h>
122 #include <sys/device.h>
123 #include <sys/socket.h>
124 #include <sys/mbuf.h>
125 #include <sys/ioctl.h>
126 #include <sys/kernel.h> /* for hz */
127 #include <sys/proc.h>
128
129 #include <net/if.h>
130 #include <net/if_dl.h>
131 #include <net/if_llc.h>
132 #include <net/if_media.h>
133 #include <net/if_ether.h>
134 #include <net/route.h>
135
136 #include <net80211/ieee80211_netbsd.h>
137 #include <net80211/ieee80211_var.h>
138 #include <net80211/ieee80211_ioctl.h>
139 #include <net80211/ieee80211_radiotap.h>
140 #include <net80211/ieee80211_rssadapt.h>
141
142 #if NBPFILTER > 0
143 #include <net/bpf.h>
144 #include <net/bpfdesc.h>
145 #endif
146
147 #include <machine/bus.h>
148
149 #include <dev/ic/wi_ieee.h>
150 #include <dev/ic/wireg.h>
151 #include <dev/ic/wivar.h>
152
153 STATIC int wi_init(struct ifnet *);
154 STATIC void wi_stop(struct ifnet *, int);
155 STATIC void wi_start(struct ifnet *);
156 STATIC int wi_reset(struct wi_softc *);
157 STATIC void wi_watchdog(struct ifnet *);
158 STATIC int wi_ioctl(struct ifnet *, u_long, caddr_t);
159 STATIC int wi_media_change(struct ifnet *);
160 STATIC void wi_media_status(struct ifnet *, struct ifmediareq *);
161
162 STATIC struct ieee80211_node *wi_node_alloc(struct ieee80211_node_table *);
163 STATIC void wi_node_free(struct ieee80211_node *);
164
165 STATIC void wi_raise_rate(struct ieee80211com *, struct ieee80211_rssdesc *);
166 STATIC void wi_lower_rate(struct ieee80211com *, struct ieee80211_rssdesc *);
167 STATIC int wi_choose_rate(struct ieee80211com *, struct ieee80211_node *,
168 struct ieee80211_frame *, u_int);
169 STATIC void wi_rssadapt_updatestats_cb(void *, struct ieee80211_node *);
170 STATIC void wi_rssadapt_updatestats(void *);
171 STATIC void wi_rssdescs_init(struct wi_rssdesc (*)[], wi_rssdescq_t *);
172 STATIC void wi_rssdescs_reset(struct ieee80211com *, struct wi_rssdesc (*)[],
173 wi_rssdescq_t *, u_int8_t (*)[]);
174 STATIC void wi_sync_bssid(struct wi_softc *, u_int8_t new_bssid[]);
175
176 STATIC void wi_rx_intr(struct wi_softc *);
177 STATIC void wi_txalloc_intr(struct wi_softc *);
178 STATIC void wi_cmd_intr(struct wi_softc *);
179 STATIC void wi_tx_intr(struct wi_softc *);
180 STATIC void wi_tx_ex_intr(struct wi_softc *);
181 STATIC void wi_info_intr(struct wi_softc *);
182
183 STATIC int wi_key_alloc(struct ieee80211com *, const struct ieee80211_key *);
184 STATIC int wi_key_delete(struct ieee80211com *, const struct ieee80211_key *);
185 STATIC int wi_key_set(struct ieee80211com *, const struct ieee80211_key *,
186 const u_int8_t[IEEE80211_ADDR_LEN]);
187 STATIC void wi_key_update_begin(struct ieee80211com *);
188 STATIC void wi_key_update_end(struct ieee80211com *);
189
190 STATIC void wi_push_packet(struct wi_softc *);
191 STATIC int wi_get_cfg(struct ifnet *, u_long, caddr_t);
192 STATIC int wi_set_cfg(struct ifnet *, u_long, caddr_t);
193 STATIC int wi_cfg_txrate(struct wi_softc *);
194 STATIC int wi_write_txrate(struct wi_softc *, int);
195 STATIC int wi_write_wep(struct wi_softc *);
196 STATIC int wi_write_multi(struct wi_softc *);
197 STATIC int wi_alloc_fid(struct wi_softc *, int, int *);
198 STATIC void wi_read_nicid(struct wi_softc *);
199 STATIC int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
200
201 STATIC int wi_cmd(struct wi_softc *, int, int, int, int);
202 STATIC int wi_cmd_start(struct wi_softc *, int, int, int, int);
203 STATIC int wi_cmd_wait(struct wi_softc *, int, int);
204 STATIC int wi_seek_bap(struct wi_softc *, int, int);
205 STATIC int wi_read_bap(struct wi_softc *, int, int, void *, int);
206 STATIC int wi_write_bap(struct wi_softc *, int, int, void *, int);
207 STATIC int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
208 STATIC int wi_read_rid(struct wi_softc *, int, void *, int *);
209 STATIC int wi_write_rid(struct wi_softc *, int, void *, int);
210
211 STATIC int wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
212 STATIC void wi_set_tim(struct ieee80211com *, struct ieee80211_node *, int);
213
214 STATIC int wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
215 STATIC void wi_scan_result(struct wi_softc *, int, int);
216
217 STATIC void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
218 STATIC void wi_mend_flags(struct wi_softc *);
219
220 static inline int
221 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
222 {
223
224 val = htole16(val);
225 return wi_write_rid(sc, rid, &val, sizeof(val));
226 }
227
228 static struct timeval lasttxerror; /* time of last tx error msg */
229 static int curtxeps = 0; /* current tx error msgs/sec */
230 static int wi_txerate = 0; /* tx error rate: max msgs/sec */
231
232 #ifdef WI_DEBUG
233 int wi_debug = 0;
234
235 #define DPRINTF(X) if (wi_debug) printf X
236 #define DPRINTF2(X) if (wi_debug > 1) printf X
237 #define IFF_DUMPPKTS(_ifp) \
238 (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
239 #else
240 #define DPRINTF(X)
241 #define DPRINTF2(X)
242 #define IFF_DUMPPKTS(_ifp) 0
243 #endif
244
245 #define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO | \
246 WI_EV_TX | WI_EV_TX_EXC | WI_EV_CMD)
247
248 struct wi_card_ident
249 wi_card_ident[] = {
250 /* CARD_ID CARD_NAME FIRM_TYPE */
251 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
252 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
253 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
254 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
255 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
256 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
257 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
258 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
259 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
260 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
261 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
262 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
263 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
264 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
265 { WI_NIC_3842_PCMCIA_ATM_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
266 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
267 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
268 { WI_NIC_3842_MINI_ATM_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
269 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
270 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
271 { WI_NIC_3842_PCI_ATM_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
272 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
273 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
274 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
275 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
276 { 0, NULL, 0 },
277 };
278
279 STATIC int
280 wi_read_xrid(struct wi_softc *sc, int rid, void *buf, int ebuflen)
281 {
282 int buflen, rc;
283
284 buflen = ebuflen;
285 if ((rc = wi_read_rid(sc, rid, buf, &buflen)) != 0)
286 return rc;
287
288 if (buflen < ebuflen) {
289 #ifdef WI_DEBUG
290 printf("%s: rid=%#04x read %d, expected %d\n", __func__,
291 rid, buflen, ebuflen);
292 #endif
293 return -1;
294 }
295 return 0;
296 }
297
298 int
299 wi_attach(struct wi_softc *sc, const u_int8_t *macaddr)
300 {
301 struct ieee80211com *ic = &sc->sc_ic;
302 struct ifnet *ifp = &sc->sc_if;
303 int chan, nrate, buflen;
304 u_int16_t val, chanavail;
305 struct {
306 u_int16_t nrates;
307 char rates[IEEE80211_RATE_SIZE];
308 } ratebuf;
309 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
310 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
311 };
312 int s;
313
314 s = splnet();
315
316 /* Make sure interrupts are disabled. */
317 CSR_WRITE_2(sc, WI_INT_EN, 0);
318 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
319
320 sc->sc_invalid = 0;
321
322 /* Reset the NIC. */
323 if (wi_reset(sc) != 0) {
324 sc->sc_invalid = 1;
325 splx(s);
326 return 1;
327 }
328
329 if (wi_read_xrid(sc, WI_RID_MAC_NODE, ic->ic_myaddr,
330 IEEE80211_ADDR_LEN) != 0 ||
331 IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
332 if (macaddr != NULL)
333 memcpy(ic->ic_myaddr, macaddr, IEEE80211_ADDR_LEN);
334 else {
335 printf(" could not get mac address, attach failed\n");
336 splx(s);
337 return 1;
338 }
339 }
340
341 printf(" 802.11 address %s\n", ether_sprintf(ic->ic_myaddr));
342
343 /* Read NIC identification */
344 wi_read_nicid(sc);
345
346 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
347 ifp->if_softc = sc;
348 ifp->if_start = wi_start;
349 ifp->if_ioctl = wi_ioctl;
350 ifp->if_watchdog = wi_watchdog;
351 ifp->if_init = wi_init;
352 ifp->if_stop = wi_stop;
353 ifp->if_flags =
354 IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
355 IFQ_SET_READY(&ifp->if_snd);
356
357 ic->ic_ifp = ifp;
358 ic->ic_phytype = IEEE80211_T_DS;
359 ic->ic_opmode = IEEE80211_M_STA;
360 ic->ic_caps = IEEE80211_C_AHDEMO;
361 ic->ic_state = IEEE80211_S_INIT;
362 ic->ic_max_aid = WI_MAX_AID;
363
364 /* Find available channel */
365 if (wi_read_xrid(sc, WI_RID_CHANNEL_LIST, &chanavail,
366 sizeof(chanavail)) != 0) {
367 aprint_normal("%s: using default channel list\n", sc->sc_dev.dv_xname);
368 chanavail = htole16(0x1fff); /* assume 1-13 */
369 }
370 for (chan = 16; chan > 0; chan--) {
371 if (!isset((u_int8_t*)&chanavail, chan - 1))
372 continue;
373 ic->ic_ibss_chan = &ic->ic_channels[chan];
374 ic->ic_channels[chan].ic_freq =
375 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
376 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B;
377 }
378
379 /* Find default IBSS channel */
380 if (wi_read_xrid(sc, WI_RID_OWN_CHNL, &val, sizeof(val)) == 0) {
381 chan = le16toh(val);
382 if (isset((u_int8_t*)&chanavail, chan - 1))
383 ic->ic_ibss_chan = &ic->ic_channels[chan];
384 }
385 if (ic->ic_ibss_chan == NULL) {
386 aprint_error("%s: no available channel\n", sc->sc_dev.dv_xname);
387 return 1;
388 }
389
390 if (sc->sc_firmware_type == WI_LUCENT) {
391 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
392 } else {
393 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
394 wi_read_xrid(sc, WI_RID_DBM_ADJUST, &val, sizeof(val)) == 0)
395 sc->sc_dbm_offset = le16toh(val);
396 else
397 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
398 }
399
400 sc->sc_flags |= WI_FLAGS_RSSADAPTSTA;
401
402 /*
403 * Set flags based on firmware version.
404 */
405 switch (sc->sc_firmware_type) {
406 case WI_LUCENT:
407 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
408 #ifdef WI_HERMES_AUTOINC_WAR
409 /* XXX: not confirmed, but never seen for recent firmware */
410 if (sc->sc_sta_firmware_ver < 40000) {
411 sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
412 }
413 #endif
414 if (sc->sc_sta_firmware_ver >= 60000)
415 sc->sc_flags |= WI_FLAGS_HAS_MOR;
416 if (sc->sc_sta_firmware_ver >= 60006) {
417 ic->ic_caps |= IEEE80211_C_IBSS;
418 ic->ic_caps |= IEEE80211_C_MONITOR;
419 }
420 ic->ic_caps |= IEEE80211_C_PMGT;
421 sc->sc_ibss_port = 1;
422 break;
423
424 case WI_INTERSIL:
425 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
426 sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
427 sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
428 if (sc->sc_sta_firmware_ver > 10101)
429 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
430 if (sc->sc_sta_firmware_ver >= 800) {
431 if (sc->sc_sta_firmware_ver != 10402)
432 ic->ic_caps |= IEEE80211_C_HOSTAP;
433 ic->ic_caps |= IEEE80211_C_IBSS;
434 ic->ic_caps |= IEEE80211_C_MONITOR;
435 }
436 ic->ic_caps |= IEEE80211_C_PMGT;
437 sc->sc_ibss_port = 0;
438 sc->sc_alt_retry = 2;
439 break;
440
441 case WI_SYMBOL:
442 sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
443 if (sc->sc_sta_firmware_ver >= 20000)
444 ic->ic_caps |= IEEE80211_C_IBSS;
445 sc->sc_ibss_port = 4;
446 break;
447 }
448
449 /*
450 * Find out if we support WEP on this card.
451 */
452 if (wi_read_xrid(sc, WI_RID_WEP_AVAIL, &val, sizeof(val)) == 0 &&
453 val != htole16(0))
454 ic->ic_caps |= IEEE80211_C_WEP;
455
456 /* Find supported rates. */
457 buflen = sizeof(ratebuf);
458 if (wi_read_rid(sc, WI_RID_DATA_RATES, &ratebuf, &buflen) == 0 &&
459 buflen > 2) {
460 nrate = le16toh(ratebuf.nrates);
461 if (nrate > IEEE80211_RATE_SIZE)
462 nrate = IEEE80211_RATE_SIZE;
463 memcpy(ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates,
464 &ratebuf.rates[0], nrate);
465 ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = nrate;
466 } else {
467 aprint_error("%s: no supported rate list\n", sc->sc_dev.dv_xname);
468 return 1;
469 }
470
471 sc->sc_max_datalen = 2304;
472 sc->sc_rts_thresh = 2347;
473 sc->sc_frag_thresh = 2346;
474 sc->sc_system_scale = 1;
475 sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
476 sc->sc_roaming_mode = 1;
477
478 callout_init(&sc->sc_rssadapt_ch);
479
480 /*
481 * Call MI attach routines.
482 */
483 if_attach(ifp);
484 ieee80211_ifattach(ic);
485
486 sc->sc_newstate = ic->ic_newstate;
487 sc->sc_set_tim = ic->ic_set_tim;
488 ic->ic_newstate = wi_newstate;
489 ic->ic_node_alloc = wi_node_alloc;
490 ic->ic_node_free = wi_node_free;
491 ic->ic_set_tim = wi_set_tim;
492
493 ic->ic_crypto.cs_key_alloc = wi_key_alloc;
494 ic->ic_crypto.cs_key_delete = wi_key_delete;
495 ic->ic_crypto.cs_key_set = wi_key_set;
496 ic->ic_crypto.cs_key_update_begin = wi_key_update_begin;
497 ic->ic_crypto.cs_key_update_end = wi_key_update_end;
498
499 ieee80211_media_init(ic, wi_media_change, wi_media_status);
500
501 #if NBPFILTER > 0
502 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
503 sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
504 #endif
505
506 memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu));
507 sc->sc_rxtap.wr_ihdr.it_len = sizeof(sc->sc_rxtapu);
508 sc->sc_rxtap.wr_ihdr.it_present = WI_RX_RADIOTAP_PRESENT;
509
510 memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu));
511 sc->sc_txtap.wt_ihdr.it_len = sizeof(sc->sc_txtapu);
512 sc->sc_txtap.wt_ihdr.it_present = WI_TX_RADIOTAP_PRESENT;
513
514 /* Attach is successful. */
515 sc->sc_attached = 1;
516
517 splx(s);
518 ieee80211_announce(ic);
519 return 0;
520 }
521
522 int
523 wi_detach(struct wi_softc *sc)
524 {
525 struct ifnet *ifp = &sc->sc_if;
526 int s;
527
528 if (!sc->sc_attached)
529 return 0;
530
531 sc->sc_invalid = 1;
532 s = splnet();
533
534 wi_stop(ifp, 1);
535
536 ieee80211_ifdetach(&sc->sc_ic);
537 if_detach(ifp);
538 splx(s);
539 return 0;
540 }
541
542 #ifdef __NetBSD__
543 int
544 wi_activate(struct device *self, enum devact act)
545 {
546 struct wi_softc *sc = (struct wi_softc *)self;
547 int rv = 0, s;
548
549 s = splnet();
550 switch (act) {
551 case DVACT_ACTIVATE:
552 rv = EOPNOTSUPP;
553 break;
554
555 case DVACT_DEACTIVATE:
556 if_deactivate(&sc->sc_if);
557 break;
558 }
559 splx(s);
560 return rv;
561 }
562
563 void
564 wi_power(struct wi_softc *sc, int why)
565 {
566 struct ifnet *ifp = &sc->sc_if;
567 int s;
568
569 s = splnet();
570 switch (why) {
571 case PWR_SUSPEND:
572 case PWR_STANDBY:
573 wi_stop(ifp, 1);
574 break;
575 case PWR_RESUME:
576 if (ifp->if_flags & IFF_UP) {
577 wi_init(ifp);
578 (void)wi_intr(sc);
579 }
580 break;
581 case PWR_SOFTSUSPEND:
582 case PWR_SOFTSTANDBY:
583 case PWR_SOFTRESUME:
584 break;
585 }
586 splx(s);
587 }
588 #endif /* __NetBSD__ */
589
590 void
591 wi_shutdown(struct wi_softc *sc)
592 {
593 struct ifnet *ifp = &sc->sc_if;
594
595 if (sc->sc_attached)
596 wi_stop(ifp, 1);
597 }
598
599 int
600 wi_intr(void *arg)
601 {
602 int i;
603 struct wi_softc *sc = arg;
604 struct ifnet *ifp = &sc->sc_if;
605 u_int16_t status;
606
607 if (sc->sc_enabled == 0 ||
608 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
609 (ifp->if_flags & IFF_RUNNING) == 0)
610 return 0;
611
612 if ((ifp->if_flags & IFF_UP) == 0) {
613 CSR_WRITE_2(sc, WI_INT_EN, 0);
614 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
615 return 1;
616 }
617
618 /* This is superfluous on Prism, but Lucent breaks if we
619 * do not disable interrupts.
620 */
621 CSR_WRITE_2(sc, WI_INT_EN, 0);
622
623 /* maximum 10 loops per interrupt */
624 for (i = 0; i < 10; i++) {
625 status = CSR_READ_2(sc, WI_EVENT_STAT);
626 #ifdef WI_DEBUG
627 if (wi_debug > 1) {
628 printf("%s: iter %d status %#04x\n", __func__, i,
629 status);
630 }
631 #endif /* WI_DEBUG */
632 if ((status & WI_INTRS) == 0)
633 break;
634
635 sc->sc_status = status;
636
637 if (status & WI_EV_RX)
638 wi_rx_intr(sc);
639
640 if (status & WI_EV_ALLOC)
641 wi_txalloc_intr(sc);
642
643 if (status & WI_EV_TX)
644 wi_tx_intr(sc);
645
646 if (status & WI_EV_TX_EXC)
647 wi_tx_ex_intr(sc);
648
649 if (status & WI_EV_INFO)
650 wi_info_intr(sc);
651
652 CSR_WRITE_2(sc, WI_EVENT_ACK, sc->sc_status);
653
654 if (sc->sc_status & WI_EV_CMD)
655 wi_cmd_intr(sc);
656
657 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
658 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
659 !IFQ_IS_EMPTY(&ifp->if_snd))
660 wi_start(ifp);
661
662 sc->sc_status = 0;
663 }
664
665 /* re-enable interrupts */
666 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
667
668 sc->sc_status = 0;
669
670 return 1;
671 }
672
673 #define arraylen(a) (sizeof(a) / sizeof((a)[0]))
674
675 STATIC void
676 wi_rssdescs_init(struct wi_rssdesc (*rssd)[WI_NTXRSS], wi_rssdescq_t *rssdfree)
677 {
678 int i;
679 SLIST_INIT(rssdfree);
680 for (i = 0; i < arraylen(*rssd); i++) {
681 SLIST_INSERT_HEAD(rssdfree, &(*rssd)[i], rd_next);
682 }
683 }
684
685 STATIC void
686 wi_rssdescs_reset(struct ieee80211com *ic, struct wi_rssdesc (*rssd)[WI_NTXRSS],
687 wi_rssdescq_t *rssdfree, u_int8_t (*txpending)[IEEE80211_RATE_MAXSIZE])
688 {
689 struct ieee80211_node *ni;
690 int i;
691 for (i = 0; i < arraylen(*rssd); i++) {
692 ni = (*rssd)[i].rd_desc.id_node;
693 (*rssd)[i].rd_desc.id_node = NULL;
694 if (ni != NULL && (ic->ic_ifp->if_flags & IFF_DEBUG) != 0)
695 printf("%s: cleaning outstanding rssadapt "
696 "descriptor for %s\n",
697 ic->ic_ifp->if_xname, ether_sprintf(ni->ni_macaddr));
698 if (ni != NULL)
699 ieee80211_free_node(ni);
700 }
701 memset(*txpending, 0, sizeof(*txpending));
702 wi_rssdescs_init(rssd, rssdfree);
703 }
704
705 STATIC int
706 wi_init(struct ifnet *ifp)
707 {
708 struct wi_softc *sc = ifp->if_softc;
709 struct ieee80211com *ic = &sc->sc_ic;
710 struct wi_joinreq join;
711 int i;
712 int error = 0, wasenabled;
713
714 DPRINTF(("wi_init: enabled %d\n", sc->sc_enabled));
715 wasenabled = sc->sc_enabled;
716 if (!sc->sc_enabled) {
717 if ((error = (*sc->sc_enable)(sc)) != 0)
718 goto out;
719 sc->sc_enabled = 1;
720 } else
721 wi_stop(ifp, 0);
722
723 /* Symbol firmware cannot be initialized more than once */
724 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled)
725 if ((error = wi_reset(sc)) != 0)
726 goto out;
727
728 /* common 802.11 configuration */
729 ic->ic_flags &= ~IEEE80211_F_IBSSON;
730 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
731 switch (ic->ic_opmode) {
732 case IEEE80211_M_STA:
733 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
734 break;
735 case IEEE80211_M_IBSS:
736 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
737 ic->ic_flags |= IEEE80211_F_IBSSON;
738 break;
739 case IEEE80211_M_AHDEMO:
740 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
741 break;
742 case IEEE80211_M_HOSTAP:
743 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
744 break;
745 case IEEE80211_M_MONITOR:
746 if (sc->sc_firmware_type == WI_LUCENT)
747 wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
748 wi_cmd(sc, WI_CMD_TEST | (WI_TEST_MONITOR << 8), 0, 0, 0);
749 break;
750 }
751
752 /* Intersil interprets this RID as joining ESS even in IBSS mode */
753 if (sc->sc_firmware_type == WI_LUCENT &&
754 (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
755 wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
756 else
757 wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
758 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
759 wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
760 ic->ic_des_esslen);
761 wi_write_val(sc, WI_RID_OWN_CHNL,
762 ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
763 wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
764 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
765 wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
766 if (ic->ic_caps & IEEE80211_C_PMGT)
767 wi_write_val(sc, WI_RID_PM_ENABLED,
768 (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
769
770 /* not yet common 802.11 configuration */
771 wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
772 wi_write_val(sc, WI_RID_RTS_THRESH, sc->sc_rts_thresh);
773 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
774 wi_write_val(sc, WI_RID_FRAG_THRESH, sc->sc_frag_thresh);
775
776 /* driver specific 802.11 configuration */
777 if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
778 wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
779 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
780 wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
781 if (sc->sc_flags & WI_FLAGS_HAS_MOR)
782 wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
783 wi_cfg_txrate(sc);
784 wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
785
786 #ifndef IEEE80211_NO_HOSTAP
787 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
788 sc->sc_firmware_type == WI_INTERSIL) {
789 wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
790 wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
791 }
792 #endif /* !IEEE80211_NO_HOSTAP */
793
794 if (sc->sc_firmware_type == WI_INTERSIL) {
795 struct ieee80211_rateset *rs =
796 &ic->ic_sup_rates[IEEE80211_MODE_11B];
797 u_int16_t basic = 0, supported = 0, rate;
798
799 for (i = 0; i < rs->rs_nrates; i++) {
800 switch (rs->rs_rates[i] & IEEE80211_RATE_VAL) {
801 case 2:
802 rate = 1;
803 break;
804 case 4:
805 rate = 2;
806 break;
807 case 11:
808 rate = 4;
809 break;
810 case 22:
811 rate = 8;
812 break;
813 default:
814 rate = 0;
815 break;
816 }
817 if (rs->rs_rates[i] & IEEE80211_RATE_BASIC)
818 basic |= rate;
819 supported |= rate;
820 }
821 wi_write_val(sc, WI_RID_BASIC_RATE, basic);
822 wi_write_val(sc, WI_RID_SUPPORT_RATE, supported);
823 wi_write_val(sc, WI_RID_ALT_RETRY_COUNT, sc->sc_alt_retry);
824 }
825
826 /*
827 * Initialize promisc mode.
828 * Being in Host-AP mode causes a great
829 * deal of pain if promiscuous mode is set.
830 * Therefore we avoid confusing the firmware
831 * and always reset promisc mode in Host-AP
832 * mode. Host-AP sees all the packets anyway.
833 */
834 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
835 (ifp->if_flags & IFF_PROMISC) != 0) {
836 wi_write_val(sc, WI_RID_PROMISC, 1);
837 } else {
838 wi_write_val(sc, WI_RID_PROMISC, 0);
839 }
840
841 /* Configure WEP. */
842 if (ic->ic_caps & IEEE80211_C_WEP) {
843 sc->sc_cnfauthmode = ic->ic_bss->ni_authmode;
844 wi_write_wep(sc);
845 }
846
847 /* Set multicast filter. */
848 wi_write_multi(sc);
849
850 sc->sc_txalloc = 0;
851 sc->sc_txalloced = 0;
852 sc->sc_txqueue = 0;
853 sc->sc_txqueued = 0;
854 sc->sc_txstart = 0;
855 sc->sc_txstarted = 0;
856
857 if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
858 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
859 if (sc->sc_firmware_type == WI_SYMBOL)
860 sc->sc_buflen = 1585; /* XXX */
861 for (i = 0; i < WI_NTXBUF; i++) {
862 error = wi_alloc_fid(sc, sc->sc_buflen,
863 &sc->sc_txd[i].d_fid);
864 if (error) {
865 printf("%s: tx buffer allocation failed\n",
866 sc->sc_dev.dv_xname);
867 goto out;
868 }
869 DPRINTF2(("wi_init: txbuf %d allocated %x\n", i,
870 sc->sc_txd[i].d_fid));
871 ++sc->sc_txalloced;
872 }
873 }
874
875 wi_rssdescs_init(&sc->sc_rssd, &sc->sc_rssdfree);
876
877 /* Enable desired port */
878 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
879 ifp->if_flags |= IFF_RUNNING;
880 ifp->if_flags &= ~IFF_OACTIVE;
881 ic->ic_state = IEEE80211_S_INIT;
882
883 if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
884 ic->ic_opmode == IEEE80211_M_IBSS ||
885 ic->ic_opmode == IEEE80211_M_MONITOR ||
886 ic->ic_opmode == IEEE80211_M_HOSTAP)
887 ieee80211_create_ibss(ic, ic->ic_ibss_chan);
888
889 /* Enable interrupts */
890 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
891
892 #ifndef IEEE80211_NO_HOSTAP
893 if (!wasenabled &&
894 ic->ic_opmode == IEEE80211_M_HOSTAP &&
895 sc->sc_firmware_type == WI_INTERSIL) {
896 /* XXX: some card need to be re-enabled for hostap */
897 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
898 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
899 }
900 #endif /* !IEEE80211_NO_HOSTAP */
901
902 if (ic->ic_opmode == IEEE80211_M_STA &&
903 ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
904 ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
905 memset(&join, 0, sizeof(join));
906 if (ic->ic_flags & IEEE80211_F_DESBSSID)
907 IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
908 if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
909 join.wi_chan =
910 htole16(ieee80211_chan2ieee(ic, ic->ic_des_chan));
911 /* Lucent firmware does not support the JOIN RID. */
912 if (sc->sc_firmware_type != WI_LUCENT)
913 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
914 }
915
916 out:
917 if (error) {
918 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
919 wi_stop(ifp, 0);
920 }
921 DPRINTF(("wi_init: return %d\n", error));
922 return error;
923 }
924
925 STATIC void
926 wi_txcmd_wait(struct wi_softc *sc)
927 {
928 KASSERT(sc->sc_txcmds == 1);
929 if (sc->sc_status & WI_EV_CMD) {
930 sc->sc_status &= ~WI_EV_CMD;
931 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
932 } else
933 (void)wi_cmd_wait(sc, WI_CMD_TX | WI_RECLAIM, 0);
934 }
935
936 STATIC void
937 wi_stop(struct ifnet *ifp, int disable)
938 {
939 struct wi_softc *sc = ifp->if_softc;
940 struct ieee80211com *ic = &sc->sc_ic;
941 int s;
942
943 if (!sc->sc_enabled)
944 return;
945
946 s = splnet();
947
948 DPRINTF(("wi_stop: disable %d\n", disable));
949
950 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
951
952 /* wait for tx command completion (deassoc, deauth) */
953 while (sc->sc_txcmds > 0) {
954 wi_txcmd_wait(sc);
955 wi_cmd_intr(sc);
956 }
957
958 /* TBD wait for deassoc, deauth tx completion? */
959
960 if (!sc->sc_invalid) {
961 CSR_WRITE_2(sc, WI_INT_EN, 0);
962 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
963 }
964
965 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
966 &sc->sc_txpending);
967
968 sc->sc_tx_timer = 0;
969 sc->sc_scan_timer = 0;
970 sc->sc_false_syns = 0;
971 sc->sc_naps = 0;
972 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
973 ifp->if_timer = 0;
974
975 if (disable) {
976 if (sc->sc_disable)
977 (*sc->sc_disable)(sc);
978 sc->sc_enabled = 0;
979 }
980 splx(s);
981 }
982
983 /*
984 * Choose a data rate for a packet len bytes long that suits the packet
985 * type and the wireless conditions.
986 *
987 * TBD Adapt fragmentation threshold.
988 */
989 STATIC int
990 wi_choose_rate(struct ieee80211com *ic, struct ieee80211_node *ni,
991 struct ieee80211_frame *wh, u_int len)
992 {
993 struct wi_softc *sc = ic->ic_ifp->if_softc;
994 struct wi_node *wn = (void*)ni;
995 struct ieee80211_rssadapt *ra = &wn->wn_rssadapt;
996 int do_not_adapt, i, rateidx, s;
997
998 do_not_adapt = (ic->ic_opmode != IEEE80211_M_HOSTAP) &&
999 (sc->sc_flags & WI_FLAGS_RSSADAPTSTA) == 0;
1000
1001 s = splnet();
1002
1003 rateidx = ieee80211_rssadapt_choose(ra, &ni->ni_rates, wh, len,
1004 ic->ic_fixed_rate,
1005 ((ic->ic_ifp->if_flags & IFF_DEBUG) == 0) ? NULL : ic->ic_ifp->if_xname,
1006 do_not_adapt);
1007
1008 ni->ni_txrate = rateidx;
1009
1010 if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
1011 /* choose the slowest pending rate so that we don't
1012 * accidentally send a packet on the MAC's queue
1013 * too fast. TBD find out if the MAC labels Tx
1014 * packets w/ rate when enqueued or dequeued.
1015 */
1016 for (i = 0; i < rateidx && sc->sc_txpending[i] == 0; i++);
1017 rateidx = i;
1018 }
1019
1020 splx(s);
1021 return (rateidx);
1022 }
1023
1024 STATIC void
1025 wi_raise_rate(struct ieee80211com *ic, struct ieee80211_rssdesc *id)
1026 {
1027 struct wi_node *wn;
1028 if (id->id_node == NULL)
1029 return;
1030
1031 wn = (void*)id->id_node;
1032 ieee80211_rssadapt_raise_rate(ic, &wn->wn_rssadapt, id);
1033 }
1034
1035 STATIC void
1036 wi_lower_rate(struct ieee80211com *ic, struct ieee80211_rssdesc *id)
1037 {
1038 struct ieee80211_node *ni;
1039 struct wi_node *wn;
1040 int s;
1041
1042 s = splnet();
1043
1044 if ((ni = id->id_node) == NULL) {
1045 DPRINTF(("wi_lower_rate: missing node\n"));
1046 goto out;
1047 }
1048
1049 wn = (void *)ni;
1050
1051 ieee80211_rssadapt_lower_rate(ic, ni, &wn->wn_rssadapt, id);
1052 out:
1053 splx(s);
1054 return;
1055 }
1056
1057 STATIC void
1058 wi_start(struct ifnet *ifp)
1059 {
1060 struct wi_softc *sc = ifp->if_softc;
1061 struct ieee80211com *ic = &sc->sc_ic;
1062 struct ether_header *eh;
1063 struct ieee80211_node *ni;
1064 struct ieee80211_frame *wh;
1065 struct ieee80211_rateset *rs;
1066 struct wi_rssdesc *rd;
1067 struct ieee80211_rssdesc *id;
1068 struct mbuf *m0;
1069 struct wi_frame frmhdr;
1070 int cur, fid, off, rateidx;
1071
1072 if (!sc->sc_enabled || sc->sc_invalid)
1073 return;
1074 if (sc->sc_flags & WI_FLAGS_OUTRANGE)
1075 return;
1076
1077 memset(&frmhdr, 0, sizeof(frmhdr));
1078 cur = sc->sc_txqueue;
1079 for (;;) {
1080 ni = ic->ic_bss;
1081 if (sc->sc_txalloced == 0 || SLIST_EMPTY(&sc->sc_rssdfree)) {
1082 ifp->if_flags |= IFF_OACTIVE;
1083 break;
1084 }
1085 if (!IF_IS_EMPTY(&ic->ic_mgtq)) {
1086 IF_DEQUEUE(&ic->ic_mgtq, m0);
1087 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
1088 (caddr_t)&frmhdr.wi_ehdr);
1089 frmhdr.wi_ehdr.ether_type = 0;
1090 wh = mtod(m0, struct ieee80211_frame *);
1091 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1092 m0->m_pkthdr.rcvif = NULL;
1093 } else if (ic->ic_state == IEEE80211_S_RUN) {
1094 IFQ_POLL(&ifp->if_snd, m0);
1095 if (m0 == NULL)
1096 break;
1097 IFQ_DEQUEUE(&ifp->if_snd, m0);
1098 ifp->if_opackets++;
1099 m_copydata(m0, 0, ETHER_HDR_LEN,
1100 (caddr_t)&frmhdr.wi_ehdr);
1101 #if NBPFILTER > 0
1102 if (ifp->if_bpf)
1103 bpf_mtap(ifp->if_bpf, m0);
1104 #endif
1105
1106 eh = mtod(m0, struct ether_header *);
1107 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1108 if (ni == NULL) {
1109 ifp->if_oerrors++;
1110 continue;
1111 }
1112 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1113 (m0->m_flags & M_PWR_SAV) == 0) {
1114 ieee80211_pwrsave(ic, ni, m0);
1115 goto next;
1116 }
1117 if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) {
1118 ieee80211_free_node(ni);
1119 ifp->if_oerrors++;
1120 continue;
1121 }
1122 wh = mtod(m0, struct ieee80211_frame *);
1123 } else
1124 break;
1125 #if NBPFILTER > 0
1126 if (ic->ic_rawbpf)
1127 bpf_mtap(ic->ic_rawbpf, m0);
1128 #endif
1129 frmhdr.wi_tx_ctl =
1130 htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX|WI_TXCNTL_TX_OK);
1131 #ifndef IEEE80211_NO_HOSTAP
1132 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1133 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
1134 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
1135 (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
1136 if (ieee80211_crypto_encap(ic, ni, m0) == NULL) {
1137 m_freem(m0);
1138 ifp->if_oerrors++;
1139 goto next;
1140 }
1141 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1142 }
1143 #endif /* !IEEE80211_NO_HOSTAP */
1144
1145 rateidx = wi_choose_rate(ic, ni, wh, m0->m_pkthdr.len);
1146 rs = &ni->ni_rates;
1147
1148 #if NBPFILTER > 0
1149 if (sc->sc_drvbpf) {
1150 struct wi_tx_radiotap_header *tap = &sc->sc_txtap;
1151
1152 tap->wt_rate = rs->rs_rates[rateidx];
1153 tap->wt_chan_freq =
1154 htole16(ic->ic_bss->ni_chan->ic_freq);
1155 tap->wt_chan_flags =
1156 htole16(ic->ic_bss->ni_chan->ic_flags);
1157 /* TBD tap->wt_flags */
1158
1159 bpf_mtap2(sc->sc_drvbpf, tap, tap->wt_ihdr.it_len, m0);
1160 }
1161 #endif
1162
1163 rd = SLIST_FIRST(&sc->sc_rssdfree);
1164 id = &rd->rd_desc;
1165 id->id_len = m0->m_pkthdr.len;
1166 id->id_rateidx = ni->ni_txrate;
1167 id->id_rssi = ni->ni_rssi;
1168
1169 frmhdr.wi_tx_idx = rd - sc->sc_rssd;
1170
1171 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1172 frmhdr.wi_tx_rate = 5 * (rs->rs_rates[rateidx] &
1173 IEEE80211_RATE_VAL);
1174 else if (sc->sc_flags & WI_FLAGS_RSSADAPTSTA)
1175 (void)wi_write_txrate(sc, rs->rs_rates[rateidx]);
1176
1177 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1178 (caddr_t)&frmhdr.wi_whdr);
1179 m_adj(m0, sizeof(struct ieee80211_frame));
1180 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1181 if (IFF_DUMPPKTS(ifp))
1182 wi_dump_pkt(&frmhdr, ni, -1);
1183 fid = sc->sc_txd[cur].d_fid;
1184 off = sizeof(frmhdr);
1185 if (wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0 ||
1186 wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0) {
1187 printf("%s: %s write fid %x failed\n",
1188 sc->sc_dev.dv_xname, __func__, fid);
1189 ifp->if_oerrors++;
1190 m_freem(m0);
1191 goto next;
1192 }
1193 m_freem(m0);
1194 sc->sc_txpending[ni->ni_txrate]++;
1195 --sc->sc_txalloced;
1196 if (sc->sc_txqueued++ == 0) {
1197 #ifdef DIAGNOSTIC
1198 if (cur != sc->sc_txstart)
1199 printf("%s: ring is desynchronized\n",
1200 sc->sc_dev.dv_xname);
1201 #endif
1202 wi_push_packet(sc);
1203 } else {
1204 #ifdef WI_RING_DEBUG
1205 printf("%s: queue %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
1206 sc->sc_dev.dv_xname, fid,
1207 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
1208 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
1209 #endif
1210 }
1211 sc->sc_txqueue = cur = (cur + 1) % WI_NTXBUF;
1212 SLIST_REMOVE_HEAD(&sc->sc_rssdfree, rd_next);
1213 id->id_node = ni;
1214 continue;
1215 next:
1216 if (ni != NULL)
1217 ieee80211_free_node(ni);
1218 }
1219 }
1220
1221
1222 STATIC int
1223 wi_reset(struct wi_softc *sc)
1224 {
1225 int i, error;
1226
1227 DPRINTF(("wi_reset\n"));
1228
1229 if (sc->sc_reset)
1230 (*sc->sc_reset)(sc);
1231
1232 error = 0;
1233 for (i = 0; i < 5; i++) {
1234 if (sc->sc_invalid)
1235 return ENXIO;
1236 DELAY(20*1000); /* XXX: way too long! */
1237 if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
1238 break;
1239 }
1240 if (error) {
1241 printf("%s: init failed\n", sc->sc_dev.dv_xname);
1242 return error;
1243 }
1244 CSR_WRITE_2(sc, WI_INT_EN, 0);
1245 CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
1246
1247 /* Calibrate timer. */
1248 wi_write_val(sc, WI_RID_TICK_TIME, 0);
1249 return 0;
1250 }
1251
1252 STATIC void
1253 wi_watchdog(struct ifnet *ifp)
1254 {
1255 struct wi_softc *sc = ifp->if_softc;
1256
1257 ifp->if_timer = 0;
1258 if (!sc->sc_enabled)
1259 return;
1260
1261 if (sc->sc_tx_timer) {
1262 if (--sc->sc_tx_timer == 0) {
1263 printf("%s: device timeout\n", ifp->if_xname);
1264 ifp->if_oerrors++;
1265 wi_init(ifp);
1266 return;
1267 }
1268 ifp->if_timer = 1;
1269 }
1270
1271 if (sc->sc_scan_timer) {
1272 if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
1273 sc->sc_firmware_type == WI_INTERSIL) {
1274 DPRINTF(("wi_watchdog: inquire scan\n"));
1275 wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
1276 }
1277 if (sc->sc_scan_timer)
1278 ifp->if_timer = 1;
1279 }
1280
1281 /* TODO: rate control */
1282 ieee80211_watchdog(&sc->sc_ic);
1283 }
1284
1285 STATIC int
1286 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1287 {
1288 struct wi_softc *sc = ifp->if_softc;
1289 struct ieee80211com *ic = &sc->sc_ic;
1290 struct ifreq *ifr = (struct ifreq *)data;
1291 int s, error = 0;
1292
1293 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1294 return ENXIO;
1295
1296 s = splnet();
1297
1298 switch (cmd) {
1299 case SIOCSIFFLAGS:
1300 /*
1301 * Can't do promisc and hostap at the same time. If all that's
1302 * changing is the promisc flag, try to short-circuit a call to
1303 * wi_init() by just setting PROMISC in the hardware.
1304 */
1305 if (ifp->if_flags & IFF_UP) {
1306 if (sc->sc_enabled) {
1307 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1308 (ifp->if_flags & IFF_PROMISC) != 0)
1309 wi_write_val(sc, WI_RID_PROMISC, 1);
1310 else
1311 wi_write_val(sc, WI_RID_PROMISC, 0);
1312 } else
1313 error = wi_init(ifp);
1314 } else if (sc->sc_enabled)
1315 wi_stop(ifp, 1);
1316 break;
1317 case SIOCSIFMEDIA:
1318 case SIOCGIFMEDIA:
1319 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1320 break;
1321 case SIOCADDMULTI:
1322 case SIOCDELMULTI:
1323 error = (cmd == SIOCADDMULTI) ?
1324 ether_addmulti(ifr, &sc->sc_ec) :
1325 ether_delmulti(ifr, &sc->sc_ec);
1326 if (error == ENETRESET) {
1327 if (ifp->if_flags & IFF_RUNNING) {
1328 /* do not rescan */
1329 error = wi_write_multi(sc);
1330 } else
1331 error = 0;
1332 }
1333 break;
1334 case SIOCGIFGENERIC:
1335 error = wi_get_cfg(ifp, cmd, data);
1336 break;
1337 case SIOCSIFGENERIC:
1338 error = suser(curproc->p_ucred, &curproc->p_acflag);
1339 if (error)
1340 break;
1341 error = wi_set_cfg(ifp, cmd, data);
1342 if (error == ENETRESET) {
1343 if (ifp->if_flags & IFF_RUNNING)
1344 error = wi_init(ifp);
1345 else
1346 error = 0;
1347 }
1348 break;
1349 case SIOCS80211BSSID:
1350 if (sc->sc_firmware_type == WI_LUCENT) {
1351 error = ENODEV;
1352 break;
1353 }
1354 /* fall through */
1355 default:
1356 ic->ic_flags = sc->sc_ic_flags;
1357 error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
1358 sc->sc_ic_flags = ic->ic_flags;
1359 wi_mend_flags(sc);
1360 if (error == ENETRESET) {
1361 if (sc->sc_enabled)
1362 error = wi_init(ifp);
1363 else
1364 error = 0;
1365 }
1366 break;
1367 }
1368 splx(s);
1369 return error;
1370 }
1371
1372 STATIC int
1373 wi_media_change(struct ifnet *ifp)
1374 {
1375 struct wi_softc *sc = ifp->if_softc;
1376 struct ieee80211com *ic = &sc->sc_ic;
1377 int error;
1378
1379 error = ieee80211_media_change(ifp);
1380 if (error == ENETRESET) {
1381 if (sc->sc_enabled)
1382 error = wi_init(ifp);
1383 else
1384 error = 0;
1385 }
1386 ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media);
1387
1388 return error;
1389 }
1390
1391 STATIC void
1392 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1393 {
1394 struct wi_softc *sc = ifp->if_softc;
1395 struct ieee80211com *ic = &sc->sc_ic;
1396 u_int16_t val;
1397 int rate;
1398
1399 if (sc->sc_enabled == 0) {
1400 imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1401 imr->ifm_status = 0;
1402 return;
1403 }
1404
1405 imr->ifm_status = IFM_AVALID;
1406 imr->ifm_active = IFM_IEEE80211;
1407 if (ic->ic_state == IEEE80211_S_RUN &&
1408 (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
1409 imr->ifm_status |= IFM_ACTIVE;
1410 if (wi_read_xrid(sc, WI_RID_CUR_TX_RATE, &val, sizeof(val)) == 0) {
1411 /* convert to 802.11 rate */
1412 val = le16toh(val);
1413 rate = val * 2;
1414 if (sc->sc_firmware_type == WI_LUCENT) {
1415 if (rate == 10)
1416 rate = 11; /* 5.5Mbps */
1417 } else {
1418 if (rate == 4*2)
1419 rate = 11; /* 5.5Mbps */
1420 else if (rate == 8*2)
1421 rate = 22; /* 11Mbps */
1422 }
1423 } else
1424 rate = 0;
1425 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1426 switch (ic->ic_opmode) {
1427 case IEEE80211_M_STA:
1428 break;
1429 case IEEE80211_M_IBSS:
1430 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1431 break;
1432 case IEEE80211_M_AHDEMO:
1433 imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1434 break;
1435 case IEEE80211_M_HOSTAP:
1436 imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1437 break;
1438 case IEEE80211_M_MONITOR:
1439 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1440 break;
1441 }
1442 }
1443
1444 STATIC struct ieee80211_node *
1445 wi_node_alloc(struct ieee80211_node_table *nt)
1446 {
1447 struct wi_node *wn =
1448 malloc(sizeof(struct wi_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1449 return wn ? &wn->wn_node : NULL;
1450 }
1451
1452 STATIC void
1453 wi_node_free(struct ieee80211_node *ni)
1454 {
1455 struct wi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
1456 int i;
1457
1458 for (i = 0; i < WI_NTXRSS; i++) {
1459 if (sc->sc_rssd[i].rd_desc.id_node == ni)
1460 sc->sc_rssd[i].rd_desc.id_node = NULL;
1461 }
1462 free(ni, M_DEVBUF);
1463 }
1464
1465 STATIC void
1466 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1467 {
1468 struct ieee80211com *ic = &sc->sc_ic;
1469 struct ieee80211_node *ni = ic->ic_bss;
1470 struct ifnet *ifp = &sc->sc_if;
1471
1472 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1473 return;
1474
1475 DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
1476 DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
1477
1478 /* In promiscuous mode, the BSSID field is not a reliable
1479 * indicator of the firmware's BSSID. Damp spurious
1480 * change-of-BSSID indications.
1481 */
1482 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1483 !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1484 WI_MAX_FALSE_SYNS))
1485 return;
1486
1487 sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1488 /*
1489 * XXX hack; we should create a new node with the new bssid
1490 * and replace the existing ic_bss with it but since we don't
1491 * process management frames to collect state we cheat by
1492 * reusing the existing node as we know wi_newstate will be
1493 * called and it will overwrite the node state.
1494 */
1495 ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1496 }
1497
1498 static __inline void
1499 wi_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
1500 struct ieee80211_frame *wh, int rssi)
1501 {
1502 struct wi_node *wn;
1503
1504 if (ni == NULL) {
1505 printf("%s: null node", __func__);
1506 return;
1507 }
1508
1509 wn = (void*)ni;
1510 ieee80211_rssadapt_input(ic, ni, &wn->wn_rssadapt, rssi);
1511 }
1512
1513 STATIC void
1514 wi_rx_intr(struct wi_softc *sc)
1515 {
1516 struct ieee80211com *ic = &sc->sc_ic;
1517 struct ifnet *ifp = &sc->sc_if;
1518 struct ieee80211_node *ni;
1519 struct wi_frame frmhdr;
1520 struct mbuf *m;
1521 struct ieee80211_frame *wh;
1522 int fid, len, off, rssi;
1523 u_int8_t dir;
1524 u_int16_t status;
1525 u_int32_t rstamp;
1526
1527 fid = CSR_READ_2(sc, WI_RX_FID);
1528
1529 /* First read in the frame header */
1530 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1531 printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname,
1532 __func__, fid);
1533 ifp->if_ierrors++;
1534 return;
1535 }
1536
1537 if (IFF_DUMPPKTS(ifp))
1538 wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
1539
1540 /*
1541 * Drop undecryptable or packets with receive errors here
1542 */
1543 status = le16toh(frmhdr.wi_status);
1544 if ((status & WI_STAT_ERRSTAT) != 0 &&
1545 ic->ic_opmode != IEEE80211_M_MONITOR) {
1546 ifp->if_ierrors++;
1547 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1548 return;
1549 }
1550 rssi = frmhdr.wi_rx_signal;
1551 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1552 le16toh(frmhdr.wi_rx_tstamp1);
1553
1554 len = le16toh(frmhdr.wi_dat_len);
1555 off = ALIGN(sizeof(struct ieee80211_frame));
1556
1557 /* Sometimes the PRISM2.x returns bogusly large frames. Except
1558 * in monitor mode, just throw them away.
1559 */
1560 if (off + len > MCLBYTES) {
1561 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1562 ifp->if_ierrors++;
1563 DPRINTF(("wi_rx_intr: oversized packet\n"));
1564 return;
1565 } else
1566 len = 0;
1567 }
1568
1569 MGETHDR(m, M_DONTWAIT, MT_DATA);
1570 if (m == NULL) {
1571 ifp->if_ierrors++;
1572 DPRINTF(("wi_rx_intr: MGET failed\n"));
1573 return;
1574 }
1575 if (off + len > MHLEN) {
1576 MCLGET(m, M_DONTWAIT);
1577 if ((m->m_flags & M_EXT) == 0) {
1578 m_freem(m);
1579 ifp->if_ierrors++;
1580 DPRINTF(("wi_rx_intr: MCLGET failed\n"));
1581 return;
1582 }
1583 }
1584
1585 m->m_data += off - sizeof(struct ieee80211_frame);
1586 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1587 wi_read_bap(sc, fid, sizeof(frmhdr),
1588 m->m_data + sizeof(struct ieee80211_frame), len);
1589 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1590 m->m_pkthdr.rcvif = ifp;
1591
1592 #if NBPFILTER > 0
1593 if (sc->sc_drvbpf) {
1594 struct wi_rx_radiotap_header *tap = &sc->sc_rxtap;
1595
1596 tap->wr_rate = frmhdr.wi_rx_rate / 5;
1597 tap->wr_antsignal = frmhdr.wi_rx_signal;
1598 tap->wr_antnoise = frmhdr.wi_rx_silence;
1599 tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1600 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1601 if (frmhdr.wi_status & WI_STAT_PCF)
1602 tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1603
1604 bpf_mtap2(sc->sc_drvbpf, tap, tap->wr_ihdr.it_len, m);
1605 }
1606 #endif
1607 wh = mtod(m, struct ieee80211_frame *);
1608 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1609 /*
1610 * WEP is decrypted by hardware. Clear WEP bit
1611 * header for ieee80211_input().
1612 */
1613 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1614 }
1615
1616 /* synchronize driver's BSSID with firmware's BSSID */
1617 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1618 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1619 wi_sync_bssid(sc, wh->i_addr3);
1620
1621 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1622
1623 ieee80211_input(ic, m, ni, rssi, rstamp);
1624
1625 wi_rssadapt_input(ic, ni, wh, rssi);
1626
1627 /*
1628 * The frame may have caused the node to be marked for
1629 * reclamation (e.g. in response to a DEAUTH message)
1630 * so use release_node here instead of unref_node.
1631 */
1632 ieee80211_free_node(ni);
1633 }
1634
1635 STATIC void
1636 wi_tx_ex_intr(struct wi_softc *sc)
1637 {
1638 struct ieee80211com *ic = &sc->sc_ic;
1639 struct ifnet *ifp = &sc->sc_if;
1640 struct ieee80211_node *ni;
1641 struct ieee80211_rssdesc *id;
1642 struct wi_rssdesc *rssd;
1643 struct wi_frame frmhdr;
1644 int fid;
1645 u_int16_t status;
1646
1647 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1648 /* Read in the frame header */
1649 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
1650 printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname,
1651 __func__, fid);
1652 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
1653 &sc->sc_txpending);
1654 goto out;
1655 }
1656
1657 if (frmhdr.wi_tx_idx >= WI_NTXRSS) {
1658 printf("%s: %s bad idx %02x\n",
1659 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
1660 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
1661 &sc->sc_txpending);
1662 goto out;
1663 }
1664
1665 status = le16toh(frmhdr.wi_status);
1666
1667 /*
1668 * Spontaneous station disconnects appear as xmit
1669 * errors. Don't announce them and/or count them
1670 * as an output error.
1671 */
1672 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1673 printf("%s: tx failed", sc->sc_dev.dv_xname);
1674 if (status & WI_TXSTAT_RET_ERR)
1675 printf(", retry limit exceeded");
1676 if (status & WI_TXSTAT_AGED_ERR)
1677 printf(", max transmit lifetime exceeded");
1678 if (status & WI_TXSTAT_DISCONNECT)
1679 printf(", port disconnected");
1680 if (status & WI_TXSTAT_FORM_ERR)
1681 printf(", invalid format (data len %u src %s)",
1682 le16toh(frmhdr.wi_dat_len),
1683 ether_sprintf(frmhdr.wi_ehdr.ether_shost));
1684 if (status & ~0xf)
1685 printf(", status=0x%x", status);
1686 printf("\n");
1687 }
1688 ifp->if_oerrors++;
1689 rssd = &sc->sc_rssd[frmhdr.wi_tx_idx];
1690 id = &rssd->rd_desc;
1691 if ((status & WI_TXSTAT_RET_ERR) != 0)
1692 wi_lower_rate(ic, id);
1693
1694 ni = id->id_node;
1695 id->id_node = NULL;
1696
1697 if (ni == NULL) {
1698 printf("%s: %s null node, rssdesc %02x\n",
1699 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
1700 goto out;
1701 }
1702
1703 if (sc->sc_txpending[id->id_rateidx]-- == 0) {
1704 printf("%s: %s txpending[%i] wraparound", sc->sc_dev.dv_xname,
1705 __func__, id->id_rateidx);
1706 sc->sc_txpending[id->id_rateidx] = 0;
1707 }
1708 if (ni != NULL)
1709 ieee80211_free_node(ni);
1710 SLIST_INSERT_HEAD(&sc->sc_rssdfree, rssd, rd_next);
1711 out:
1712 ifp->if_flags &= ~IFF_OACTIVE;
1713 }
1714
1715 STATIC void
1716 wi_txalloc_intr(struct wi_softc *sc)
1717 {
1718 int fid, cur;
1719
1720 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1721
1722 cur = sc->sc_txalloc;
1723 #ifdef DIAGNOSTIC
1724 if (sc->sc_txstarted == 0) {
1725 printf("%s: spurious alloc %x != %x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
1726 sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
1727 sc->sc_txqueue, sc->sc_txstart, sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
1728 return;
1729 }
1730 #endif
1731 --sc->sc_txstarted;
1732 ++sc->sc_txalloced;
1733 sc->sc_txd[cur].d_fid = fid;
1734 sc->sc_txalloc = (cur + 1) % WI_NTXBUF;
1735 #ifdef WI_RING_DEBUG
1736 printf("%s: alloc %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
1737 sc->sc_dev.dv_xname, fid,
1738 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
1739 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
1740 #endif
1741 }
1742
1743 STATIC void
1744 wi_cmd_intr(struct wi_softc *sc)
1745 {
1746 struct ifnet *ifp = &sc->sc_if;
1747
1748 if (sc->sc_invalid)
1749 return;
1750 #ifdef WI_DEBUG
1751 if (wi_debug)
1752 printf("%s: %d txcmds outstanding\n", __func__, sc->sc_txcmds);
1753 #endif
1754 KASSERT(sc->sc_txcmds > 0);
1755
1756 --sc->sc_txcmds;
1757
1758 if (--sc->sc_txqueued == 0) {
1759 sc->sc_tx_timer = 0;
1760 ifp->if_flags &= ~IFF_OACTIVE;
1761 #ifdef WI_RING_DEBUG
1762 printf("%s: cmd , alloc %d queue %d start %d alloced %d queued %d started %d\n",
1763 sc->sc_dev.dv_xname,
1764 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
1765 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
1766 #endif
1767 } else
1768 wi_push_packet(sc);
1769 }
1770
1771 STATIC void
1772 wi_push_packet(struct wi_softc *sc)
1773 {
1774 struct ifnet *ifp = &sc->sc_if;
1775 int cur, fid;
1776
1777 cur = sc->sc_txstart;
1778 fid = sc->sc_txd[cur].d_fid;
1779
1780 KASSERT(sc->sc_txcmds == 0);
1781
1782 if (wi_cmd_start(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1783 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1784 /* XXX ring might have a hole */
1785 }
1786
1787 if (sc->sc_txcmds++ > 0)
1788 printf("%s: %d tx cmds pending!!!\n", __func__, sc->sc_txcmds);
1789
1790 ++sc->sc_txstarted;
1791 #ifdef DIAGNOSTIC
1792 if (sc->sc_txstarted > WI_NTXBUF)
1793 printf("%s: too many buffers started\n", sc->sc_dev.dv_xname);
1794 #endif
1795 sc->sc_txstart = (cur + 1) % WI_NTXBUF;
1796 sc->sc_tx_timer = 5;
1797 ifp->if_timer = 1;
1798 #ifdef WI_RING_DEBUG
1799 printf("%s: push %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
1800 sc->sc_dev.dv_xname, fid,
1801 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
1802 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
1803 #endif
1804 }
1805
1806 STATIC void
1807 wi_tx_intr(struct wi_softc *sc)
1808 {
1809 struct ieee80211com *ic = &sc->sc_ic;
1810 struct ifnet *ifp = &sc->sc_if;
1811 struct ieee80211_node *ni;
1812 struct ieee80211_rssdesc *id;
1813 struct wi_rssdesc *rssd;
1814 struct wi_frame frmhdr;
1815 int fid;
1816
1817 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1818 /* Read in the frame header */
1819 if (wi_read_bap(sc, fid, offsetof(struct wi_frame, wi_tx_swsup2),
1820 &frmhdr.wi_tx_swsup2, 2) != 0) {
1821 printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname,
1822 __func__, fid);
1823 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
1824 &sc->sc_txpending);
1825 goto out;
1826 }
1827
1828 if (frmhdr.wi_tx_idx >= WI_NTXRSS) {
1829 printf("%s: %s bad idx %02x\n",
1830 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
1831 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
1832 &sc->sc_txpending);
1833 goto out;
1834 }
1835
1836 rssd = &sc->sc_rssd[frmhdr.wi_tx_idx];
1837 id = &rssd->rd_desc;
1838 wi_raise_rate(ic, id);
1839
1840 ni = id->id_node;
1841 id->id_node = NULL;
1842
1843 if (ni == NULL) {
1844 printf("%s: %s null node, rssdesc %02x\n",
1845 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
1846 goto out;
1847 }
1848
1849 if (sc->sc_txpending[id->id_rateidx]-- == 0) {
1850 printf("%s: %s txpending[%i] wraparound", sc->sc_dev.dv_xname,
1851 __func__, id->id_rateidx);
1852 sc->sc_txpending[id->id_rateidx] = 0;
1853 }
1854 if (ni != NULL)
1855 ieee80211_free_node(ni);
1856 SLIST_INSERT_HEAD(&sc->sc_rssdfree, rssd, rd_next);
1857 out:
1858 ifp->if_flags &= ~IFF_OACTIVE;
1859 }
1860
1861 STATIC void
1862 wi_info_intr(struct wi_softc *sc)
1863 {
1864 struct ieee80211com *ic = &sc->sc_ic;
1865 struct ifnet *ifp = &sc->sc_if;
1866 int i, fid, len, off;
1867 u_int16_t ltbuf[2];
1868 u_int16_t stat;
1869 u_int32_t *ptr;
1870
1871 fid = CSR_READ_2(sc, WI_INFO_FID);
1872 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1873
1874 switch (le16toh(ltbuf[1])) {
1875
1876 case WI_INFO_LINK_STAT:
1877 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1878 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1879 switch (le16toh(stat)) {
1880 case CONNECTED:
1881 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1882 if (ic->ic_state == IEEE80211_S_RUN &&
1883 ic->ic_opmode != IEEE80211_M_IBSS)
1884 break;
1885 /* FALLTHROUGH */
1886 case AP_CHANGE:
1887 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1888 break;
1889 case AP_IN_RANGE:
1890 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1891 break;
1892 case AP_OUT_OF_RANGE:
1893 if (sc->sc_firmware_type == WI_SYMBOL &&
1894 sc->sc_scan_timer > 0) {
1895 if (wi_cmd(sc, WI_CMD_INQUIRE,
1896 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1897 sc->sc_scan_timer = 0;
1898 break;
1899 }
1900 if (ic->ic_opmode == IEEE80211_M_STA)
1901 sc->sc_flags |= WI_FLAGS_OUTRANGE;
1902 break;
1903 case DISCONNECTED:
1904 case ASSOC_FAILED:
1905 if (ic->ic_opmode == IEEE80211_M_STA)
1906 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1907 break;
1908 }
1909 break;
1910
1911 case WI_INFO_COUNTERS:
1912 /* some card versions have a larger stats structure */
1913 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1914 ptr = (u_int32_t *)&sc->sc_stats;
1915 off = sizeof(ltbuf);
1916 for (i = 0; i < len; i++, off += 2, ptr++) {
1917 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1918 stat = le16toh(stat);
1919 #ifdef WI_HERMES_STATS_WAR
1920 if (stat & 0xf000)
1921 stat = ~stat;
1922 #endif
1923 *ptr += stat;
1924 }
1925 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1926 sc->sc_stats.wi_tx_multi_retries +
1927 sc->sc_stats.wi_tx_retry_limit;
1928 break;
1929
1930 case WI_INFO_SCAN_RESULTS:
1931 case WI_INFO_HOST_SCAN_RESULTS:
1932 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1933 break;
1934
1935 default:
1936 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1937 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1938 break;
1939 }
1940 }
1941
1942 STATIC int
1943 wi_write_multi(struct wi_softc *sc)
1944 {
1945 struct ifnet *ifp = &sc->sc_if;
1946 int n;
1947 struct wi_mcast mlist;
1948 struct ether_multi *enm;
1949 struct ether_multistep estep;
1950
1951 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1952 allmulti:
1953 ifp->if_flags |= IFF_ALLMULTI;
1954 memset(&mlist, 0, sizeof(mlist));
1955 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1956 sizeof(mlist));
1957 }
1958
1959 n = 0;
1960 ETHER_FIRST_MULTI(estep, &sc->sc_ec, enm);
1961 while (enm != NULL) {
1962 /* Punt on ranges or too many multicast addresses. */
1963 if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
1964 n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
1965 goto allmulti;
1966
1967 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
1968 n++;
1969 ETHER_NEXT_MULTI(estep, enm);
1970 }
1971 ifp->if_flags &= ~IFF_ALLMULTI;
1972 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1973 IEEE80211_ADDR_LEN * n);
1974 }
1975
1976
1977 STATIC void
1978 wi_read_nicid(struct wi_softc *sc)
1979 {
1980 struct wi_card_ident *id;
1981 char *p;
1982 int len;
1983 u_int16_t ver[4];
1984
1985 /* getting chip identity */
1986 memset(ver, 0, sizeof(ver));
1987 len = sizeof(ver);
1988 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1989 printf("%s: using ", sc->sc_dev.dv_xname);
1990 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
1991
1992 sc->sc_firmware_type = WI_NOTYPE;
1993 for (id = wi_card_ident; id->card_name != NULL; id++) {
1994 if (le16toh(ver[0]) == id->card_id) {
1995 printf("%s", id->card_name);
1996 sc->sc_firmware_type = id->firm_type;
1997 break;
1998 }
1999 }
2000 if (sc->sc_firmware_type == WI_NOTYPE) {
2001 if (le16toh(ver[0]) & 0x8000) {
2002 printf("Unknown PRISM2 chip");
2003 sc->sc_firmware_type = WI_INTERSIL;
2004 } else {
2005 printf("Unknown Lucent chip");
2006 sc->sc_firmware_type = WI_LUCENT;
2007 }
2008 }
2009
2010 /* get primary firmware version (Only Prism chips) */
2011 if (sc->sc_firmware_type != WI_LUCENT) {
2012 memset(ver, 0, sizeof(ver));
2013 len = sizeof(ver);
2014 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
2015 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
2016 le16toh(ver[3]) * 100 + le16toh(ver[1]);
2017 }
2018
2019 /* get station firmware version */
2020 memset(ver, 0, sizeof(ver));
2021 len = sizeof(ver);
2022 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
2023 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
2024 le16toh(ver[3]) * 100 + le16toh(ver[1]);
2025 if (sc->sc_firmware_type == WI_INTERSIL &&
2026 (sc->sc_sta_firmware_ver == 10102 ||
2027 sc->sc_sta_firmware_ver == 20102)) {
2028 char ident[12];
2029 memset(ident, 0, sizeof(ident));
2030 len = sizeof(ident);
2031 /* value should be the format like "V2.00-11" */
2032 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
2033 *(p = (char *)ident) >= 'A' &&
2034 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
2035 sc->sc_firmware_type = WI_SYMBOL;
2036 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
2037 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
2038 (p[6] - '0') * 10 + (p[7] - '0');
2039 }
2040 }
2041
2042 printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
2043 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
2044 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
2045 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
2046 printf("Primary (%u.%u.%u), ",
2047 sc->sc_pri_firmware_ver / 10000,
2048 (sc->sc_pri_firmware_ver % 10000) / 100,
2049 sc->sc_pri_firmware_ver % 100);
2050 printf("Station (%u.%u.%u)\n",
2051 sc->sc_sta_firmware_ver / 10000,
2052 (sc->sc_sta_firmware_ver % 10000) / 100,
2053 sc->sc_sta_firmware_ver % 100);
2054 }
2055
2056 STATIC int
2057 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
2058 {
2059 struct wi_ssid ssid;
2060
2061 if (buflen > IEEE80211_NWID_LEN)
2062 return ENOBUFS;
2063 memset(&ssid, 0, sizeof(ssid));
2064 ssid.wi_len = htole16(buflen);
2065 memcpy(ssid.wi_ssid, buf, buflen);
2066 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
2067 }
2068
2069 STATIC int
2070 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
2071 {
2072 struct wi_softc *sc = ifp->if_softc;
2073 struct ieee80211com *ic = &sc->sc_ic;
2074 struct ifreq *ifr = (struct ifreq *)data;
2075 struct wi_req wreq;
2076 int len, n, error;
2077
2078 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2079 if (error)
2080 return error;
2081 len = (wreq.wi_len - 1) * 2;
2082 if (len < sizeof(u_int16_t))
2083 return ENOSPC;
2084 if (len > sizeof(wreq.wi_val))
2085 len = sizeof(wreq.wi_val);
2086
2087 switch (wreq.wi_type) {
2088
2089 case WI_RID_IFACE_STATS:
2090 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
2091 if (len < sizeof(sc->sc_stats))
2092 error = ENOSPC;
2093 else
2094 len = sizeof(sc->sc_stats);
2095 break;
2096
2097 case WI_RID_ENCRYPTION:
2098 case WI_RID_TX_CRYPT_KEY:
2099 case WI_RID_DEFLT_CRYPT_KEYS:
2100 case WI_RID_TX_RATE:
2101 return ieee80211_cfgget(ic, cmd, data);
2102
2103 case WI_RID_MICROWAVE_OVEN:
2104 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
2105 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2106 &len);
2107 break;
2108 }
2109 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
2110 len = sizeof(u_int16_t);
2111 break;
2112
2113 case WI_RID_DBM_ADJUST:
2114 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
2115 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2116 &len);
2117 break;
2118 }
2119 wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
2120 len = sizeof(u_int16_t);
2121 break;
2122
2123 case WI_RID_ROAMING_MODE:
2124 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
2125 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2126 &len);
2127 break;
2128 }
2129 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
2130 len = sizeof(u_int16_t);
2131 break;
2132
2133 case WI_RID_SYSTEM_SCALE:
2134 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
2135 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2136 &len);
2137 break;
2138 }
2139 wreq.wi_val[0] = htole16(sc->sc_system_scale);
2140 len = sizeof(u_int16_t);
2141 break;
2142
2143 case WI_RID_FRAG_THRESH:
2144 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
2145 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2146 &len);
2147 break;
2148 }
2149 wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
2150 len = sizeof(u_int16_t);
2151 break;
2152
2153 case WI_RID_READ_APS:
2154 #ifndef IEEE80211_NO_HOSTAP
2155 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2156 return ieee80211_cfgget(ic, cmd, data);
2157 #endif /* !IEEE80211_NO_HOSTAP */
2158 if (sc->sc_scan_timer > 0) {
2159 error = EINPROGRESS;
2160 break;
2161 }
2162 n = sc->sc_naps;
2163 if (len < sizeof(n)) {
2164 error = ENOSPC;
2165 break;
2166 }
2167 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
2168 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
2169 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
2170 memcpy(wreq.wi_val, &n, sizeof(n));
2171 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
2172 sizeof(struct wi_apinfo) * n);
2173 break;
2174
2175 default:
2176 if (sc->sc_enabled) {
2177 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2178 &len);
2179 break;
2180 }
2181 switch (wreq.wi_type) {
2182 case WI_RID_MAX_DATALEN:
2183 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2184 len = sizeof(u_int16_t);
2185 break;
2186 case WI_RID_FRAG_THRESH:
2187 wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
2188 len = sizeof(u_int16_t);
2189 break;
2190 case WI_RID_RTS_THRESH:
2191 wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
2192 len = sizeof(u_int16_t);
2193 break;
2194 case WI_RID_CNFAUTHMODE:
2195 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2196 len = sizeof(u_int16_t);
2197 break;
2198 case WI_RID_NODENAME:
2199 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2200 error = ENOSPC;
2201 break;
2202 }
2203 len = sc->sc_nodelen + sizeof(u_int16_t);
2204 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2205 memcpy(&wreq.wi_val[1], sc->sc_nodename,
2206 sc->sc_nodelen);
2207 break;
2208 default:
2209 return ieee80211_cfgget(ic, cmd, data);
2210 }
2211 break;
2212 }
2213 if (error)
2214 return error;
2215 wreq.wi_len = (len + 1) / 2 + 1;
2216 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
2217 }
2218
2219 STATIC int
2220 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
2221 {
2222 struct wi_softc *sc = ifp->if_softc;
2223 struct ieee80211com *ic = &sc->sc_ic;
2224 struct ifreq *ifr = (struct ifreq *)data;
2225 struct ieee80211_rateset *rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2226 struct wi_req wreq;
2227 struct mbuf *m;
2228 int i, len, error;
2229
2230 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2231 if (error)
2232 return error;
2233 len = (wreq.wi_len - 1) * 2;
2234 switch (wreq.wi_type) {
2235 case WI_RID_DBM_ADJUST:
2236 return ENODEV;
2237
2238 case WI_RID_NODENAME:
2239 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2240 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2241 error = ENOSPC;
2242 break;
2243 }
2244 if (sc->sc_enabled) {
2245 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2246 len);
2247 if (error)
2248 break;
2249 }
2250 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2251 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2252 break;
2253
2254 case WI_RID_MICROWAVE_OVEN:
2255 case WI_RID_ROAMING_MODE:
2256 case WI_RID_SYSTEM_SCALE:
2257 case WI_RID_FRAG_THRESH:
2258 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2259 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2260 break;
2261 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2262 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2263 break;
2264 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2265 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2266 break;
2267 if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2268 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2269 break;
2270 /* FALLTHROUGH */
2271 case WI_RID_RTS_THRESH:
2272 case WI_RID_CNFAUTHMODE:
2273 case WI_RID_MAX_DATALEN:
2274 if (sc->sc_enabled) {
2275 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2276 sizeof(u_int16_t));
2277 if (error)
2278 break;
2279 }
2280 switch (wreq.wi_type) {
2281 case WI_RID_FRAG_THRESH:
2282 sc->sc_frag_thresh = le16toh(wreq.wi_val[0]);
2283 break;
2284 case WI_RID_RTS_THRESH:
2285 sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
2286 break;
2287 case WI_RID_MICROWAVE_OVEN:
2288 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2289 break;
2290 case WI_RID_ROAMING_MODE:
2291 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2292 break;
2293 case WI_RID_SYSTEM_SCALE:
2294 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2295 break;
2296 case WI_RID_CNFAUTHMODE:
2297 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2298 break;
2299 case WI_RID_MAX_DATALEN:
2300 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2301 break;
2302 }
2303 break;
2304
2305 case WI_RID_TX_RATE:
2306 switch (le16toh(wreq.wi_val[0])) {
2307 case 3:
2308 ic->ic_fixed_rate = -1;
2309 break;
2310 default:
2311 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
2312 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2313 / 2 == le16toh(wreq.wi_val[0]))
2314 break;
2315 }
2316 if (i == IEEE80211_RATE_SIZE)
2317 return EINVAL;
2318 ic->ic_fixed_rate = i;
2319 }
2320 if (sc->sc_enabled)
2321 error = wi_cfg_txrate(sc);
2322 break;
2323
2324 case WI_RID_SCAN_APS:
2325 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2326 error = wi_scan_ap(sc, 0x3fff, 0x000f);
2327 break;
2328
2329 case WI_RID_MGMT_XMIT:
2330 if (!sc->sc_enabled) {
2331 error = ENETDOWN;
2332 break;
2333 }
2334 if (ic->ic_mgtq.ifq_len > 5) {
2335 error = EAGAIN;
2336 break;
2337 }
2338 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
2339 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2340 if (m == NULL) {
2341 error = ENOMEM;
2342 break;
2343 }
2344 IF_ENQUEUE(&ic->ic_mgtq, m);
2345 break;
2346
2347 default:
2348 if (sc->sc_enabled) {
2349 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2350 len);
2351 if (error)
2352 break;
2353 }
2354 error = ieee80211_cfgset(ic, cmd, data);
2355 break;
2356 }
2357 return error;
2358 }
2359
2360 /* Rate is 0 for hardware auto-select, otherwise rate is
2361 * 2, 4, 11, or 22 (units of 500Kbps).
2362 */
2363 STATIC int
2364 wi_write_txrate(struct wi_softc *sc, int rate)
2365 {
2366 u_int16_t hwrate;
2367
2368 /* rate: 0, 2, 4, 11, 22 */
2369 switch (sc->sc_firmware_type) {
2370 case WI_LUCENT:
2371 switch (rate & IEEE80211_RATE_VAL) {
2372 case 2:
2373 hwrate = 1;
2374 break;
2375 case 4:
2376 hwrate = 2;
2377 break;
2378 default:
2379 hwrate = 3; /* auto */
2380 break;
2381 case 11:
2382 hwrate = 4;
2383 break;
2384 case 22:
2385 hwrate = 5;
2386 break;
2387 }
2388 break;
2389 default:
2390 switch (rate & IEEE80211_RATE_VAL) {
2391 case 2:
2392 hwrate = 1;
2393 break;
2394 case 4:
2395 hwrate = 2;
2396 break;
2397 case 11:
2398 hwrate = 4;
2399 break;
2400 case 22:
2401 hwrate = 8;
2402 break;
2403 default:
2404 hwrate = 15; /* auto */
2405 break;
2406 }
2407 break;
2408 }
2409
2410 if (sc->sc_tx_rate == hwrate)
2411 return 0;
2412
2413 if (sc->sc_if.if_flags & IFF_DEBUG)
2414 printf("%s: tx rate %d -> %d (%d)\n", __func__, sc->sc_tx_rate,
2415 hwrate, rate);
2416
2417 sc->sc_tx_rate = hwrate;
2418
2419 return wi_write_val(sc, WI_RID_TX_RATE, sc->sc_tx_rate);
2420 }
2421
2422 STATIC int
2423 wi_cfg_txrate(struct wi_softc *sc)
2424 {
2425 struct ieee80211com *ic = &sc->sc_ic;
2426 struct ieee80211_rateset *rs;
2427 int rate;
2428
2429 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2430
2431 sc->sc_tx_rate = 0; /* force write to RID */
2432
2433 if (ic->ic_fixed_rate < 0)
2434 rate = 0; /* auto */
2435 else
2436 rate = rs->rs_rates[ic->ic_fixed_rate];
2437
2438 return wi_write_txrate(sc, rate);
2439 }
2440
2441 STATIC int
2442 wi_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k)
2443 {
2444 int keyix;
2445
2446 if (&ic->ic_nw_keys[0] <= k && k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])
2447 keyix = k - ic->ic_nw_keys;
2448 else
2449 keyix = IEEE80211_KEYIX_NONE;
2450
2451 DPRINTF(("%s: alloc key %u\n", __func__, keyix));
2452
2453 return keyix;
2454 }
2455
2456 STATIC int
2457 wi_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
2458 {
2459 struct wi_softc *sc = ic->ic_ifp->if_softc;
2460 u_int keyix = k->wk_keyix;
2461
2462 DPRINTF(("%s: delete key %u\n", __func__, keyix));
2463
2464 if (keyix >= IEEE80211_WEP_NKID)
2465 return 0;
2466 if (k->wk_keylen != 0)
2467 sc->sc_flags &= ~WI_FLAGS_WEP_VALID;
2468
2469 return 1;
2470 }
2471
2472 static int
2473 wi_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
2474 const u_int8_t mac[IEEE80211_ADDR_LEN])
2475 {
2476 struct wi_softc *sc = ic->ic_ifp->if_softc;
2477
2478 DPRINTF(("%s: set key %u\n", __func__, k->wk_keyix));
2479
2480 if (k->wk_keyix >= IEEE80211_WEP_NKID)
2481 return 0;
2482
2483 sc->sc_flags &= ~WI_FLAGS_WEP_VALID;
2484
2485 return 1;
2486 }
2487
2488 STATIC void
2489 wi_key_update_begin(struct ieee80211com *ic)
2490 {
2491 DPRINTF(("%s:\n", __func__));
2492 }
2493
2494 STATIC void
2495 wi_key_update_end(struct ieee80211com *ic)
2496 {
2497 struct ifnet *ifp = ic->ic_ifp;
2498 struct wi_softc *sc = ifp->if_softc;
2499
2500 DPRINTF(("%s:\n", __func__));
2501
2502 if ((sc->sc_flags & WI_FLAGS_WEP_VALID) != 0)
2503 return;
2504 if ((ic->ic_caps & IEEE80211_C_WEP) != 0 && sc->sc_enabled &&
2505 !sc->sc_invalid)
2506 (void)wi_write_wep(sc);
2507 }
2508
2509 STATIC int
2510 wi_write_wep(struct wi_softc *sc)
2511 {
2512 struct ifnet *ifp = &sc->sc_if;
2513 struct ieee80211com *ic = &sc->sc_ic;
2514 int error = 0;
2515 int i, keylen;
2516 u_int16_t val;
2517 struct wi_key wkey[IEEE80211_WEP_NKID];
2518
2519 if ((ifp->if_flags & IFF_RUNNING) != 0)
2520 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
2521
2522 switch (sc->sc_firmware_type) {
2523 case WI_LUCENT:
2524 val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
2525 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2526 if (error)
2527 break;
2528 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey);
2529 if (error)
2530 break;
2531 memset(wkey, 0, sizeof(wkey));
2532 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2533 keylen = ic->ic_nw_keys[i].wk_keylen;
2534 wkey[i].wi_keylen = htole16(keylen);
2535 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2536 keylen);
2537 }
2538 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2539 wkey, sizeof(wkey));
2540 break;
2541
2542 case WI_INTERSIL:
2543 case WI_SYMBOL:
2544 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2545 /*
2546 * ONLY HWB3163 EVAL-CARD Firmware version
2547 * less than 0.8 variant2
2548 *
2549 * If promiscuous mode disable, Prism2 chip
2550 * does not work with WEP .
2551 * It is under investigation for details.
2552 * (ichiro (at) NetBSD.org)
2553 */
2554 if (sc->sc_firmware_type == WI_INTERSIL &&
2555 sc->sc_sta_firmware_ver < 802 ) {
2556 /* firm ver < 0.8 variant 2 */
2557 wi_write_val(sc, WI_RID_PROMISC, 1);
2558 }
2559 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2560 sc->sc_cnfauthmode);
2561 val = PRIVACY_INVOKED;
2562 if ((sc->sc_ic_flags & IEEE80211_F_DROPUNENC) != 0)
2563 val |= EXCLUDE_UNENCRYPTED;
2564 #ifndef IEEE80211_NO_HOSTAP
2565 /*
2566 * Encryption firmware has a bug for HostAP mode.
2567 */
2568 if (sc->sc_firmware_type == WI_INTERSIL &&
2569 ic->ic_opmode == IEEE80211_M_HOSTAP)
2570 val |= HOST_ENCRYPT;
2571 #endif /* !IEEE80211_NO_HOSTAP */
2572 } else {
2573 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2574 IEEE80211_AUTH_OPEN);
2575 val = HOST_ENCRYPT | HOST_DECRYPT;
2576 }
2577 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2578 if (error)
2579 break;
2580 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2581 ic->ic_def_txkey);
2582 if (error)
2583 break;
2584 /*
2585 * It seems that the firmware accept 104bit key only if
2586 * all the keys have 104bit length. We get the length of
2587 * the transmit key and use it for all other keys.
2588 * Perhaps we should use software WEP for such situation.
2589 */
2590 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
2591 IEEE80211_KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
2592 keylen = 13; /* No keys => 104bit ok */
2593 else
2594 keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen;
2595
2596 if (keylen > IEEE80211_WEP_KEYLEN)
2597 keylen = 13; /* 104bit keys */
2598 else
2599 keylen = IEEE80211_WEP_KEYLEN;
2600 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2601 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2602 ic->ic_nw_keys[i].wk_key, keylen);
2603 if (error)
2604 break;
2605 }
2606 break;
2607 }
2608 if ((ifp->if_flags & IFF_RUNNING) != 0)
2609 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
2610 if (error == 0)
2611 sc->sc_flags |= WI_FLAGS_WEP_VALID;
2612 return error;
2613 }
2614
2615 /* Must be called at proper protection level! */
2616 STATIC int
2617 wi_cmd_start(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2618 {
2619 #ifdef WI_HISTOGRAM
2620 static int hist1[11];
2621 static int hist1count;
2622 #endif
2623 int i;
2624
2625 /* wait for the busy bit to clear */
2626 for (i = 500; i > 0; i--) { /* 5s */
2627 if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
2628 break;
2629 if (sc->sc_invalid)
2630 return ENXIO;
2631 DELAY(1000); /* 1 m sec */
2632 }
2633 if (i == 0) {
2634 printf("%s: wi_cmd: busy bit won't clear.\n",
2635 sc->sc_dev.dv_xname);
2636 return(ETIMEDOUT);
2637 }
2638 #ifdef WI_HISTOGRAM
2639 if (i > 490)
2640 hist1[500 - i]++;
2641 else
2642 hist1[10]++;
2643 if (++hist1count == 1000) {
2644 hist1count = 0;
2645 printf("%s: hist1: %d %d %d %d %d %d %d %d %d %d %d\n",
2646 sc->sc_dev.dv_xname,
2647 hist1[0], hist1[1], hist1[2], hist1[3], hist1[4],
2648 hist1[5], hist1[6], hist1[7], hist1[8], hist1[9],
2649 hist1[10]);
2650 }
2651 #endif
2652 CSR_WRITE_2(sc, WI_PARAM0, val0);
2653 CSR_WRITE_2(sc, WI_PARAM1, val1);
2654 CSR_WRITE_2(sc, WI_PARAM2, val2);
2655 CSR_WRITE_2(sc, WI_COMMAND, cmd);
2656
2657 return 0;
2658 }
2659
2660 STATIC int
2661 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2662 {
2663 int rc;
2664
2665 #ifdef WI_DEBUG
2666 if (wi_debug) {
2667 printf("%s: [enter] %d txcmds outstanding\n", __func__,
2668 sc->sc_txcmds);
2669 }
2670 #endif
2671 if (sc->sc_txcmds > 0)
2672 wi_txcmd_wait(sc);
2673
2674 if ((rc = wi_cmd_start(sc, cmd, val0, val1, val2)) != 0)
2675 return rc;
2676
2677 if (cmd == WI_CMD_INI) {
2678 /* XXX: should sleep here. */
2679 if (sc->sc_invalid)
2680 return ENXIO;
2681 DELAY(100*1000);
2682 }
2683 rc = wi_cmd_wait(sc, cmd, val0);
2684
2685 #ifdef WI_DEBUG
2686 if (wi_debug) {
2687 printf("%s: [ ] %d txcmds outstanding\n", __func__,
2688 sc->sc_txcmds);
2689 }
2690 #endif
2691 if (sc->sc_txcmds > 0)
2692 wi_cmd_intr(sc);
2693
2694 #ifdef WI_DEBUG
2695 if (wi_debug) {
2696 printf("%s: [leave] %d txcmds outstanding\n", __func__,
2697 sc->sc_txcmds);
2698 }
2699 #endif
2700 return rc;
2701 }
2702
2703 STATIC int
2704 wi_cmd_wait(struct wi_softc *sc, int cmd, int val0)
2705 {
2706 #ifdef WI_HISTOGRAM
2707 static int hist2[11];
2708 static int hist2count;
2709 #endif
2710 int i, status;
2711 #ifdef WI_DEBUG
2712 if (wi_debug > 1)
2713 printf("%s: cmd=%#x, arg=%#x\n", __func__, cmd, val0);
2714 #endif /* WI_DEBUG */
2715
2716 /* wait for the cmd completed bit */
2717 for (i = 0; i < WI_TIMEOUT; i++) {
2718 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
2719 break;
2720 if (sc->sc_invalid)
2721 return ENXIO;
2722 DELAY(WI_DELAY);
2723 }
2724
2725 #ifdef WI_HISTOGRAM
2726 if (i < 100)
2727 hist2[i/10]++;
2728 else
2729 hist2[10]++;
2730 if (++hist2count == 1000) {
2731 hist2count = 0;
2732 printf("%s: hist2: %d %d %d %d %d %d %d %d %d %d %d\n",
2733 sc->sc_dev.dv_xname,
2734 hist2[0], hist2[1], hist2[2], hist2[3], hist2[4],
2735 hist2[5], hist2[6], hist2[7], hist2[8], hist2[9],
2736 hist2[10]);
2737 }
2738 #endif
2739
2740 status = CSR_READ_2(sc, WI_STATUS);
2741
2742 if (i == WI_TIMEOUT) {
2743 printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
2744 sc->sc_dev.dv_xname, cmd, val0);
2745 return ETIMEDOUT;
2746 }
2747
2748 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2749
2750 if (status & WI_STAT_CMD_RESULT) {
2751 printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
2752 sc->sc_dev.dv_xname, cmd, val0);
2753 return EIO;
2754 }
2755 return 0;
2756 }
2757
2758 STATIC int
2759 wi_seek_bap(struct wi_softc *sc, int id, int off)
2760 {
2761 #ifdef WI_HISTOGRAM
2762 static int hist4[11];
2763 static int hist4count;
2764 #endif
2765 int i, status;
2766
2767 CSR_WRITE_2(sc, WI_SEL0, id);
2768 CSR_WRITE_2(sc, WI_OFF0, off);
2769
2770 for (i = 0; ; i++) {
2771 status = CSR_READ_2(sc, WI_OFF0);
2772 if ((status & WI_OFF_BUSY) == 0)
2773 break;
2774 if (i == WI_TIMEOUT) {
2775 printf("%s: timeout in wi_seek to %x/%x\n",
2776 sc->sc_dev.dv_xname, id, off);
2777 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2778 return ETIMEDOUT;
2779 }
2780 if (sc->sc_invalid)
2781 return ENXIO;
2782 DELAY(2);
2783 }
2784 #ifdef WI_HISTOGRAM
2785 if (i < 100)
2786 hist4[i/10]++;
2787 else
2788 hist4[10]++;
2789 if (++hist4count == 2500) {
2790 hist4count = 0;
2791 printf("%s: hist4: %d %d %d %d %d %d %d %d %d %d %d\n",
2792 sc->sc_dev.dv_xname,
2793 hist4[0], hist4[1], hist4[2], hist4[3], hist4[4],
2794 hist4[5], hist4[6], hist4[7], hist4[8], hist4[9],
2795 hist4[10]);
2796 }
2797 #endif
2798 if (status & WI_OFF_ERR) {
2799 printf("%s: failed in wi_seek to %x/%x\n",
2800 sc->sc_dev.dv_xname, id, off);
2801 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2802 return EIO;
2803 }
2804 sc->sc_bap_id = id;
2805 sc->sc_bap_off = off;
2806 return 0;
2807 }
2808
2809 STATIC int
2810 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2811 {
2812 int error, cnt;
2813
2814 if (buflen == 0)
2815 return 0;
2816 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2817 if ((error = wi_seek_bap(sc, id, off)) != 0)
2818 return error;
2819 }
2820 cnt = (buflen + 1) / 2;
2821 CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
2822 sc->sc_bap_off += cnt * 2;
2823 return 0;
2824 }
2825
2826 STATIC int
2827 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2828 {
2829 int error, cnt;
2830
2831 if (buflen == 0)
2832 return 0;
2833
2834 #ifdef WI_HERMES_AUTOINC_WAR
2835 again:
2836 #endif
2837 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2838 if ((error = wi_seek_bap(sc, id, off)) != 0)
2839 return error;
2840 }
2841 cnt = (buflen + 1) / 2;
2842 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
2843 sc->sc_bap_off += cnt * 2;
2844
2845 #ifdef WI_HERMES_AUTOINC_WAR
2846 /*
2847 * According to the comments in the HCF Light code, there is a bug
2848 * in the Hermes (or possibly in certain Hermes firmware revisions)
2849 * where the chip's internal autoincrement counter gets thrown off
2850 * during data writes: the autoincrement is missed, causing one
2851 * data word to be overwritten and subsequent words to be written to
2852 * the wrong memory locations. The end result is that we could end
2853 * up transmitting bogus frames without realizing it. The workaround
2854 * for this is to write a couple of extra guard words after the end
2855 * of the transfer, then attempt to read then back. If we fail to
2856 * locate the guard words where we expect them, we preform the
2857 * transfer over again.
2858 */
2859 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2860 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2861 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2862 wi_seek_bap(sc, id, sc->sc_bap_off);
2863 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2864 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2865 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2866 printf("%s: detect auto increment bug, try again\n",
2867 sc->sc_dev.dv_xname);
2868 goto again;
2869 }
2870 }
2871 #endif
2872 return 0;
2873 }
2874
2875 STATIC int
2876 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2877 {
2878 int error, len;
2879 struct mbuf *m;
2880
2881 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2882 if (m->m_len == 0)
2883 continue;
2884
2885 len = min(m->m_len, totlen);
2886
2887 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2888 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2889 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2890 totlen);
2891 }
2892
2893 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2894 return error;
2895
2896 off += m->m_len;
2897 totlen -= len;
2898 }
2899 return 0;
2900 }
2901
2902 STATIC int
2903 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
2904 {
2905 int i;
2906
2907 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2908 printf("%s: failed to allocate %d bytes on NIC\n",
2909 sc->sc_dev.dv_xname, len);
2910 return ENOMEM;
2911 }
2912
2913 for (i = 0; i < WI_TIMEOUT; i++) {
2914 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2915 break;
2916 DELAY(1);
2917 }
2918 if (i == WI_TIMEOUT) {
2919 printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
2920 return ETIMEDOUT;
2921 }
2922 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
2923 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2924 return 0;
2925 }
2926
2927 STATIC int
2928 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2929 {
2930 int error, len;
2931 u_int16_t ltbuf[2];
2932
2933 /* Tell the NIC to enter record read mode. */
2934 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2935 if (error)
2936 return error;
2937
2938 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2939 if (error)
2940 return error;
2941
2942 if (le16toh(ltbuf[0]) == 0)
2943 return EOPNOTSUPP;
2944 if (le16toh(ltbuf[1]) != rid) {
2945 printf("%s: record read mismatch, rid=%x, got=%x\n",
2946 sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
2947 return EIO;
2948 }
2949 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
2950 if (*buflenp < len) {
2951 printf("%s: record buffer is too small, "
2952 "rid=%x, size=%d, len=%d\n",
2953 sc->sc_dev.dv_xname, rid, *buflenp, len);
2954 return ENOSPC;
2955 }
2956 *buflenp = len;
2957 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2958 }
2959
2960 STATIC int
2961 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2962 {
2963 int error;
2964 u_int16_t ltbuf[2];
2965
2966 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
2967 ltbuf[1] = htole16(rid);
2968
2969 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2970 if (error)
2971 return error;
2972 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2973 if (error)
2974 return error;
2975
2976 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2977 }
2978
2979 STATIC void
2980 wi_rssadapt_updatestats_cb(void *arg, struct ieee80211_node *ni)
2981 {
2982 struct wi_node *wn = (void*)ni;
2983 ieee80211_rssadapt_updatestats(&wn->wn_rssadapt);
2984 }
2985
2986 STATIC void
2987 wi_rssadapt_updatestats(void *arg)
2988 {
2989 struct wi_softc *sc = arg;
2990 struct ieee80211com *ic = &sc->sc_ic;
2991 ieee80211_iterate_nodes(&ic->ic_sta, wi_rssadapt_updatestats_cb, arg);
2992 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
2993 ic->ic_state == IEEE80211_S_RUN)
2994 callout_reset(&sc->sc_rssadapt_ch, hz / 10,
2995 wi_rssadapt_updatestats, arg);
2996 }
2997
2998 /*
2999 * In HOSTAP mode, restore IEEE80211_F_DROPUNENC when operating
3000 * with WEP enabled so that the AP drops unencoded frames at the
3001 * 802.11 layer.
3002 *
3003 * In all other modes, clear IEEE80211_F_DROPUNENC when operating
3004 * with WEP enabled so we don't drop unencoded frames at the 802.11
3005 * layer. This is necessary because we must strip the WEP bit from
3006 * the 802.11 header before passing frames to ieee80211_input
3007 * because the card has already stripped the WEP crypto header from
3008 * the packet.
3009 */
3010 STATIC void
3011 wi_mend_flags(struct wi_softc *sc)
3012 {
3013 struct ieee80211com *ic = &sc->sc_ic;
3014
3015 if (ic->ic_state == IEEE80211_S_RUN &&
3016 (ic->ic_flags & IEEE80211_F_PRIVACY) != 0 &&
3017 ic->ic_opmode != IEEE80211_M_HOSTAP)
3018 ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
3019 else
3020 ic->ic_flags = sc->sc_ic_flags;
3021 }
3022
3023 STATIC int
3024 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
3025 {
3026 struct ifnet *ifp = ic->ic_ifp;
3027 struct wi_softc *sc = ifp->if_softc;
3028 struct ieee80211_node *ni = ic->ic_bss;
3029 u_int16_t val;
3030 struct wi_ssid ssid;
3031 struct wi_macaddr bssid, old_bssid;
3032 enum ieee80211_state ostate;
3033 #ifdef WI_DEBUG
3034 static const char *stname[] =
3035 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
3036 #endif /* WI_DEBUG */
3037
3038 ostate = ic->ic_state;
3039 DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
3040
3041 switch (nstate) {
3042 case IEEE80211_S_INIT:
3043 if (ic->ic_opmode != IEEE80211_M_MONITOR)
3044 callout_stop(&sc->sc_rssadapt_ch);
3045 ic->ic_flags &= ~IEEE80211_F_SIBSS;
3046 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
3047 break;
3048
3049 case IEEE80211_S_SCAN:
3050 case IEEE80211_S_AUTH:
3051 case IEEE80211_S_ASSOC:
3052 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
3053 return 0;
3054
3055 case IEEE80211_S_RUN:
3056 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
3057 IEEE80211_ADDR_COPY(old_bssid.wi_mac_addr, ni->ni_bssid);
3058 wi_read_xrid(sc, WI_RID_CURRENT_BSSID, &bssid,
3059 IEEE80211_ADDR_LEN);
3060 IEEE80211_ADDR_COPY(ni->ni_bssid, &bssid);
3061 IEEE80211_ADDR_COPY(ni->ni_macaddr, &bssid);
3062 wi_read_xrid(sc, WI_RID_CURRENT_CHAN, &val, sizeof(val));
3063 if (!isset(ic->ic_chan_avail, le16toh(val)))
3064 panic("%s: invalid channel %d\n", sc->sc_dev.dv_xname,
3065 le16toh(val));
3066 ni->ni_chan = &ic->ic_channels[le16toh(val)];
3067
3068 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
3069 #ifndef IEEE80211_NO_HOSTAP
3070 ni->ni_esslen = ic->ic_des_esslen;
3071 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
3072 ni->ni_rates = ic->ic_sup_rates[
3073 ieee80211_chan2mode(ic, ni->ni_chan)];
3074 ni->ni_intval = ic->ic_lintval;
3075 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
3076 if (ic->ic_flags & IEEE80211_F_PRIVACY)
3077 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3078 #endif /* !IEEE80211_NO_HOSTAP */
3079 } else {
3080 wi_read_xrid(sc, WI_RID_CURRENT_SSID, &ssid,
3081 sizeof(ssid));
3082 ni->ni_esslen = le16toh(ssid.wi_len);
3083 if (ni->ni_esslen > IEEE80211_NWID_LEN)
3084 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
3085 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
3086 ni->ni_rates = ic->ic_sup_rates[
3087 ieee80211_chan2mode(ic, ni->ni_chan)]; /*XXX*/
3088 }
3089 wi_mend_flags(sc);
3090 if (ic->ic_opmode != IEEE80211_M_MONITOR)
3091 callout_reset(&sc->sc_rssadapt_ch, hz / 10,
3092 wi_rssadapt_updatestats, sc);
3093 /* Trigger routing socket messages. XXX Copied from
3094 * ieee80211_newstate.
3095 */
3096 if (ic->ic_opmode == IEEE80211_M_STA)
3097 ieee80211_notify_node_join(ic, ic->ic_bss,
3098 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
3099 break;
3100 }
3101 return (*sc->sc_newstate)(ic, nstate, arg);
3102 }
3103
3104 STATIC void
3105 wi_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set)
3106 {
3107 struct wi_softc *sc = ic->ic_ifp->if_softc;
3108
3109 (*sc->sc_set_tim)(ic, ni, set);
3110
3111 if ((ic->ic_flags & IEEE80211_F_TIMUPDATE) == 0)
3112 return;
3113
3114 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
3115
3116 (void)wi_write_val(sc, WI_RID_SET_TIM,
3117 IEEE80211_AID(ni->ni_associd) | (set ? 0x8000 : 0));
3118 }
3119
3120 STATIC int
3121 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
3122 {
3123 int error = 0;
3124 u_int16_t val[2];
3125
3126 if (!sc->sc_enabled)
3127 return ENXIO;
3128 switch (sc->sc_firmware_type) {
3129 case WI_LUCENT:
3130 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
3131 break;
3132 case WI_INTERSIL:
3133 val[0] = htole16(chanmask); /* channel */
3134 val[1] = htole16(txrate); /* tx rate */
3135 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
3136 break;
3137 case WI_SYMBOL:
3138 /*
3139 * XXX only supported on 3.x ?
3140 */
3141 val[0] = htole16(BSCAN_BCAST | BSCAN_ONETIME);
3142 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
3143 val, sizeof(val[0]));
3144 break;
3145 }
3146 if (error == 0) {
3147 sc->sc_scan_timer = WI_SCAN_WAIT;
3148 sc->sc_if.if_timer = 1;
3149 DPRINTF(("wi_scan_ap: start scanning, "
3150 "chanmask 0x%x txrate 0x%x\n", chanmask, txrate));
3151 }
3152 return error;
3153 }
3154
3155 STATIC void
3156 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
3157 {
3158 #define N(a) (sizeof (a) / sizeof (a[0]))
3159 int i, naps, off, szbuf;
3160 struct wi_scan_header ws_hdr; /* Prism2 header */
3161 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
3162 struct wi_apinfo *ap;
3163
3164 off = sizeof(u_int16_t) * 2;
3165 memset(&ws_hdr, 0, sizeof(ws_hdr));
3166 switch (sc->sc_firmware_type) {
3167 case WI_INTERSIL:
3168 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
3169 off += sizeof(ws_hdr);
3170 szbuf = sizeof(struct wi_scan_data_p2);
3171 break;
3172 case WI_SYMBOL:
3173 szbuf = sizeof(struct wi_scan_data_p2) + 6;
3174 break;
3175 case WI_LUCENT:
3176 szbuf = sizeof(struct wi_scan_data);
3177 break;
3178 default:
3179 printf("%s: wi_scan_result: unknown firmware type %u\n",
3180 sc->sc_dev.dv_xname, sc->sc_firmware_type);
3181 naps = 0;
3182 goto done;
3183 }
3184 naps = (cnt * 2 + 2 - off) / szbuf;
3185 if (naps > N(sc->sc_aps))
3186 naps = N(sc->sc_aps);
3187 sc->sc_naps = naps;
3188 /* Read Data */
3189 ap = sc->sc_aps;
3190 memset(&ws_dat, 0, sizeof(ws_dat));
3191 for (i = 0; i < naps; i++, ap++) {
3192 wi_read_bap(sc, fid, off, &ws_dat,
3193 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
3194 DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
3195 ether_sprintf(ws_dat.wi_bssid)));
3196 off += szbuf;
3197 ap->scanreason = le16toh(ws_hdr.wi_reason);
3198 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
3199 ap->channel = le16toh(ws_dat.wi_chid);
3200 ap->signal = le16toh(ws_dat.wi_signal);
3201 ap->noise = le16toh(ws_dat.wi_noise);
3202 ap->quality = ap->signal - ap->noise;
3203 ap->capinfo = le16toh(ws_dat.wi_capinfo);
3204 ap->interval = le16toh(ws_dat.wi_interval);
3205 ap->rate = le16toh(ws_dat.wi_rate);
3206 ap->namelen = le16toh(ws_dat.wi_namelen);
3207 if (ap->namelen > sizeof(ap->name))
3208 ap->namelen = sizeof(ap->name);
3209 memcpy(ap->name, ws_dat.wi_name, ap->namelen);
3210 }
3211 done:
3212 /* Done scanning */
3213 sc->sc_scan_timer = 0;
3214 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
3215 #undef N
3216 }
3217
3218 STATIC void
3219 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
3220 {
3221 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
3222 ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL
3223 : -1,
3224 rssi);
3225 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
3226 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
3227 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
3228 printf(" rx_signal %u rx_rate %u rx_flow %u\n",
3229 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
3230 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
3231 wh->wi_tx_rtry, wh->wi_tx_rate,
3232 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
3233 printf(" ehdr dst %s src %s type 0x%x\n",
3234 ether_sprintf(wh->wi_ehdr.ether_dhost),
3235 ether_sprintf(wh->wi_ehdr.ether_shost),
3236 wh->wi_ehdr.ether_type);
3237 }
3238