wi.c revision 1.205 1 /* $NetBSD: wi.c,v 1.205 2005/07/14 00:28:51 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.205 2005/07/14 00:28:51 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 *, enum ieee80211_state);
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 & IEEE80211_F_DROPUNENC;
1359 if (error == ENETRESET) {
1360 if (sc->sc_enabled)
1361 error = wi_init(ifp);
1362 else
1363 error = 0;
1364 }
1365 break;
1366 }
1367 wi_mend_flags(sc, ic->ic_state);
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 wh = mtod(m, struct ieee80211_frame *);
1593 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1594 /*
1595 * WEP is decrypted by hardware. Clear WEP bit
1596 * header for ieee80211_input().
1597 */
1598 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1599 }
1600 #if NBPFILTER > 0
1601 if (sc->sc_drvbpf) {
1602 struct wi_rx_radiotap_header *tap = &sc->sc_rxtap;
1603
1604 tap->wr_rate = frmhdr.wi_rx_rate / 5;
1605 tap->wr_antsignal = frmhdr.wi_rx_signal;
1606 tap->wr_antnoise = frmhdr.wi_rx_silence;
1607 tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1608 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1609 if (frmhdr.wi_status & WI_STAT_PCF)
1610 tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1611
1612 /* XXX IEEE80211_RADIOTAP_F_WEP */
1613 bpf_mtap2(sc->sc_drvbpf, tap, tap->wr_ihdr.it_len, m);
1614 }
1615 #endif
1616
1617 /* synchronize driver's BSSID with firmware's BSSID */
1618 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1619 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1620 wi_sync_bssid(sc, wh->i_addr3);
1621
1622 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1623
1624 ieee80211_input(ic, m, ni, rssi, rstamp);
1625
1626 wi_rssadapt_input(ic, ni, wh, rssi);
1627
1628 /*
1629 * The frame may have caused the node to be marked for
1630 * reclamation (e.g. in response to a DEAUTH message)
1631 * so use release_node here instead of unref_node.
1632 */
1633 ieee80211_free_node(ni);
1634 }
1635
1636 STATIC void
1637 wi_tx_ex_intr(struct wi_softc *sc)
1638 {
1639 struct ieee80211com *ic = &sc->sc_ic;
1640 struct ifnet *ifp = &sc->sc_if;
1641 struct ieee80211_node *ni;
1642 struct ieee80211_rssdesc *id;
1643 struct wi_rssdesc *rssd;
1644 struct wi_frame frmhdr;
1645 int fid;
1646 u_int16_t status;
1647
1648 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1649 /* Read in the frame header */
1650 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
1651 printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname,
1652 __func__, fid);
1653 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
1654 &sc->sc_txpending);
1655 goto out;
1656 }
1657
1658 if (frmhdr.wi_tx_idx >= WI_NTXRSS) {
1659 printf("%s: %s bad idx %02x\n",
1660 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
1661 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
1662 &sc->sc_txpending);
1663 goto out;
1664 }
1665
1666 status = le16toh(frmhdr.wi_status);
1667
1668 /*
1669 * Spontaneous station disconnects appear as xmit
1670 * errors. Don't announce them and/or count them
1671 * as an output error.
1672 */
1673 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1674 printf("%s: tx failed", sc->sc_dev.dv_xname);
1675 if (status & WI_TXSTAT_RET_ERR)
1676 printf(", retry limit exceeded");
1677 if (status & WI_TXSTAT_AGED_ERR)
1678 printf(", max transmit lifetime exceeded");
1679 if (status & WI_TXSTAT_DISCONNECT)
1680 printf(", port disconnected");
1681 if (status & WI_TXSTAT_FORM_ERR)
1682 printf(", invalid format (data len %u src %s)",
1683 le16toh(frmhdr.wi_dat_len),
1684 ether_sprintf(frmhdr.wi_ehdr.ether_shost));
1685 if (status & ~0xf)
1686 printf(", status=0x%x", status);
1687 printf("\n");
1688 }
1689 ifp->if_oerrors++;
1690 rssd = &sc->sc_rssd[frmhdr.wi_tx_idx];
1691 id = &rssd->rd_desc;
1692 if ((status & WI_TXSTAT_RET_ERR) != 0)
1693 wi_lower_rate(ic, id);
1694
1695 ni = id->id_node;
1696 id->id_node = NULL;
1697
1698 if (ni == NULL) {
1699 printf("%s: %s null node, rssdesc %02x\n",
1700 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
1701 goto out;
1702 }
1703
1704 if (sc->sc_txpending[id->id_rateidx]-- == 0) {
1705 printf("%s: %s txpending[%i] wraparound", sc->sc_dev.dv_xname,
1706 __func__, id->id_rateidx);
1707 sc->sc_txpending[id->id_rateidx] = 0;
1708 }
1709 if (ni != NULL)
1710 ieee80211_free_node(ni);
1711 SLIST_INSERT_HEAD(&sc->sc_rssdfree, rssd, rd_next);
1712 out:
1713 ifp->if_flags &= ~IFF_OACTIVE;
1714 }
1715
1716 STATIC void
1717 wi_txalloc_intr(struct wi_softc *sc)
1718 {
1719 int fid, cur;
1720
1721 fid = CSR_READ_2(sc, WI_ALLOC_FID);
1722
1723 cur = sc->sc_txalloc;
1724 #ifdef DIAGNOSTIC
1725 if (sc->sc_txstarted == 0) {
1726 printf("%s: spurious alloc %x != %x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
1727 sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
1728 sc->sc_txqueue, sc->sc_txstart, sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
1729 return;
1730 }
1731 #endif
1732 --sc->sc_txstarted;
1733 ++sc->sc_txalloced;
1734 sc->sc_txd[cur].d_fid = fid;
1735 sc->sc_txalloc = (cur + 1) % WI_NTXBUF;
1736 #ifdef WI_RING_DEBUG
1737 printf("%s: alloc %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
1738 sc->sc_dev.dv_xname, fid,
1739 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
1740 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
1741 #endif
1742 }
1743
1744 STATIC void
1745 wi_cmd_intr(struct wi_softc *sc)
1746 {
1747 struct ifnet *ifp = &sc->sc_if;
1748
1749 if (sc->sc_invalid)
1750 return;
1751 #ifdef WI_DEBUG
1752 if (wi_debug)
1753 printf("%s: %d txcmds outstanding\n", __func__, sc->sc_txcmds);
1754 #endif
1755 KASSERT(sc->sc_txcmds > 0);
1756
1757 --sc->sc_txcmds;
1758
1759 if (--sc->sc_txqueued == 0) {
1760 sc->sc_tx_timer = 0;
1761 ifp->if_flags &= ~IFF_OACTIVE;
1762 #ifdef WI_RING_DEBUG
1763 printf("%s: cmd , alloc %d queue %d start %d alloced %d queued %d started %d\n",
1764 sc->sc_dev.dv_xname,
1765 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
1766 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
1767 #endif
1768 } else
1769 wi_push_packet(sc);
1770 }
1771
1772 STATIC void
1773 wi_push_packet(struct wi_softc *sc)
1774 {
1775 struct ifnet *ifp = &sc->sc_if;
1776 int cur, fid;
1777
1778 cur = sc->sc_txstart;
1779 fid = sc->sc_txd[cur].d_fid;
1780
1781 KASSERT(sc->sc_txcmds == 0);
1782
1783 if (wi_cmd_start(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1784 printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
1785 /* XXX ring might have a hole */
1786 }
1787
1788 if (sc->sc_txcmds++ > 0)
1789 printf("%s: %d tx cmds pending!!!\n", __func__, sc->sc_txcmds);
1790
1791 ++sc->sc_txstarted;
1792 #ifdef DIAGNOSTIC
1793 if (sc->sc_txstarted > WI_NTXBUF)
1794 printf("%s: too many buffers started\n", sc->sc_dev.dv_xname);
1795 #endif
1796 sc->sc_txstart = (cur + 1) % WI_NTXBUF;
1797 sc->sc_tx_timer = 5;
1798 ifp->if_timer = 1;
1799 #ifdef WI_RING_DEBUG
1800 printf("%s: push %04x, alloc %d queue %d start %d alloced %d queued %d started %d\n",
1801 sc->sc_dev.dv_xname, fid,
1802 sc->sc_txalloc, sc->sc_txqueue, sc->sc_txstart,
1803 sc->sc_txalloced, sc->sc_txqueued, sc->sc_txstarted);
1804 #endif
1805 }
1806
1807 STATIC void
1808 wi_tx_intr(struct wi_softc *sc)
1809 {
1810 struct ieee80211com *ic = &sc->sc_ic;
1811 struct ifnet *ifp = &sc->sc_if;
1812 struct ieee80211_node *ni;
1813 struct ieee80211_rssdesc *id;
1814 struct wi_rssdesc *rssd;
1815 struct wi_frame frmhdr;
1816 int fid;
1817
1818 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1819 /* Read in the frame header */
1820 if (wi_read_bap(sc, fid, offsetof(struct wi_frame, wi_tx_swsup2),
1821 &frmhdr.wi_tx_swsup2, 2) != 0) {
1822 printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname,
1823 __func__, fid);
1824 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
1825 &sc->sc_txpending);
1826 goto out;
1827 }
1828
1829 if (frmhdr.wi_tx_idx >= WI_NTXRSS) {
1830 printf("%s: %s bad idx %02x\n",
1831 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
1832 wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
1833 &sc->sc_txpending);
1834 goto out;
1835 }
1836
1837 rssd = &sc->sc_rssd[frmhdr.wi_tx_idx];
1838 id = &rssd->rd_desc;
1839 wi_raise_rate(ic, id);
1840
1841 ni = id->id_node;
1842 id->id_node = NULL;
1843
1844 if (ni == NULL) {
1845 printf("%s: %s null node, rssdesc %02x\n",
1846 sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
1847 goto out;
1848 }
1849
1850 if (sc->sc_txpending[id->id_rateidx]-- == 0) {
1851 printf("%s: %s txpending[%i] wraparound", sc->sc_dev.dv_xname,
1852 __func__, id->id_rateidx);
1853 sc->sc_txpending[id->id_rateidx] = 0;
1854 }
1855 if (ni != NULL)
1856 ieee80211_free_node(ni);
1857 SLIST_INSERT_HEAD(&sc->sc_rssdfree, rssd, rd_next);
1858 out:
1859 ifp->if_flags &= ~IFF_OACTIVE;
1860 }
1861
1862 STATIC void
1863 wi_info_intr(struct wi_softc *sc)
1864 {
1865 struct ieee80211com *ic = &sc->sc_ic;
1866 struct ifnet *ifp = &sc->sc_if;
1867 int i, fid, len, off;
1868 u_int16_t ltbuf[2];
1869 u_int16_t stat;
1870 u_int32_t *ptr;
1871
1872 fid = CSR_READ_2(sc, WI_INFO_FID);
1873 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1874
1875 switch (le16toh(ltbuf[1])) {
1876
1877 case WI_INFO_LINK_STAT:
1878 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1879 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1880 switch (le16toh(stat)) {
1881 case CONNECTED:
1882 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1883 if (ic->ic_state == IEEE80211_S_RUN &&
1884 ic->ic_opmode != IEEE80211_M_IBSS)
1885 break;
1886 /* FALLTHROUGH */
1887 case AP_CHANGE:
1888 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1889 break;
1890 case AP_IN_RANGE:
1891 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
1892 break;
1893 case AP_OUT_OF_RANGE:
1894 if (sc->sc_firmware_type == WI_SYMBOL &&
1895 sc->sc_scan_timer > 0) {
1896 if (wi_cmd(sc, WI_CMD_INQUIRE,
1897 WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
1898 sc->sc_scan_timer = 0;
1899 break;
1900 }
1901 if (ic->ic_opmode == IEEE80211_M_STA)
1902 sc->sc_flags |= WI_FLAGS_OUTRANGE;
1903 break;
1904 case DISCONNECTED:
1905 case ASSOC_FAILED:
1906 if (ic->ic_opmode == IEEE80211_M_STA)
1907 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1908 break;
1909 }
1910 break;
1911
1912 case WI_INFO_COUNTERS:
1913 /* some card versions have a larger stats structure */
1914 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1915 ptr = (u_int32_t *)&sc->sc_stats;
1916 off = sizeof(ltbuf);
1917 for (i = 0; i < len; i++, off += 2, ptr++) {
1918 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1919 stat = le16toh(stat);
1920 #ifdef WI_HERMES_STATS_WAR
1921 if (stat & 0xf000)
1922 stat = ~stat;
1923 #endif
1924 *ptr += stat;
1925 }
1926 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1927 sc->sc_stats.wi_tx_multi_retries +
1928 sc->sc_stats.wi_tx_retry_limit;
1929 break;
1930
1931 case WI_INFO_SCAN_RESULTS:
1932 case WI_INFO_HOST_SCAN_RESULTS:
1933 wi_scan_result(sc, fid, le16toh(ltbuf[0]));
1934 break;
1935
1936 default:
1937 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1938 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1939 break;
1940 }
1941 }
1942
1943 STATIC int
1944 wi_write_multi(struct wi_softc *sc)
1945 {
1946 struct ifnet *ifp = &sc->sc_if;
1947 int n;
1948 struct wi_mcast mlist;
1949 struct ether_multi *enm;
1950 struct ether_multistep estep;
1951
1952 if ((ifp->if_flags & IFF_PROMISC) != 0) {
1953 allmulti:
1954 ifp->if_flags |= IFF_ALLMULTI;
1955 memset(&mlist, 0, sizeof(mlist));
1956 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1957 sizeof(mlist));
1958 }
1959
1960 n = 0;
1961 ETHER_FIRST_MULTI(estep, &sc->sc_ec, enm);
1962 while (enm != NULL) {
1963 /* Punt on ranges or too many multicast addresses. */
1964 if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
1965 n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
1966 goto allmulti;
1967
1968 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
1969 n++;
1970 ETHER_NEXT_MULTI(estep, enm);
1971 }
1972 ifp->if_flags &= ~IFF_ALLMULTI;
1973 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1974 IEEE80211_ADDR_LEN * n);
1975 }
1976
1977
1978 STATIC void
1979 wi_read_nicid(struct wi_softc *sc)
1980 {
1981 struct wi_card_ident *id;
1982 char *p;
1983 int len;
1984 u_int16_t ver[4];
1985
1986 /* getting chip identity */
1987 memset(ver, 0, sizeof(ver));
1988 len = sizeof(ver);
1989 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1990 printf("%s: using ", sc->sc_dev.dv_xname);
1991 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
1992
1993 sc->sc_firmware_type = WI_NOTYPE;
1994 for (id = wi_card_ident; id->card_name != NULL; id++) {
1995 if (le16toh(ver[0]) == id->card_id) {
1996 printf("%s", id->card_name);
1997 sc->sc_firmware_type = id->firm_type;
1998 break;
1999 }
2000 }
2001 if (sc->sc_firmware_type == WI_NOTYPE) {
2002 if (le16toh(ver[0]) & 0x8000) {
2003 printf("Unknown PRISM2 chip");
2004 sc->sc_firmware_type = WI_INTERSIL;
2005 } else {
2006 printf("Unknown Lucent chip");
2007 sc->sc_firmware_type = WI_LUCENT;
2008 }
2009 }
2010
2011 /* get primary firmware version (Only Prism chips) */
2012 if (sc->sc_firmware_type != WI_LUCENT) {
2013 memset(ver, 0, sizeof(ver));
2014 len = sizeof(ver);
2015 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
2016 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
2017 le16toh(ver[3]) * 100 + le16toh(ver[1]);
2018 }
2019
2020 /* get station firmware version */
2021 memset(ver, 0, sizeof(ver));
2022 len = sizeof(ver);
2023 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
2024 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
2025 le16toh(ver[3]) * 100 + le16toh(ver[1]);
2026 if (sc->sc_firmware_type == WI_INTERSIL &&
2027 (sc->sc_sta_firmware_ver == 10102 ||
2028 sc->sc_sta_firmware_ver == 20102)) {
2029 char ident[12];
2030 memset(ident, 0, sizeof(ident));
2031 len = sizeof(ident);
2032 /* value should be the format like "V2.00-11" */
2033 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
2034 *(p = (char *)ident) >= 'A' &&
2035 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
2036 sc->sc_firmware_type = WI_SYMBOL;
2037 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
2038 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
2039 (p[6] - '0') * 10 + (p[7] - '0');
2040 }
2041 }
2042
2043 printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
2044 sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
2045 (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
2046 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
2047 printf("Primary (%u.%u.%u), ",
2048 sc->sc_pri_firmware_ver / 10000,
2049 (sc->sc_pri_firmware_ver % 10000) / 100,
2050 sc->sc_pri_firmware_ver % 100);
2051 printf("Station (%u.%u.%u)\n",
2052 sc->sc_sta_firmware_ver / 10000,
2053 (sc->sc_sta_firmware_ver % 10000) / 100,
2054 sc->sc_sta_firmware_ver % 100);
2055 }
2056
2057 STATIC int
2058 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
2059 {
2060 struct wi_ssid ssid;
2061
2062 if (buflen > IEEE80211_NWID_LEN)
2063 return ENOBUFS;
2064 memset(&ssid, 0, sizeof(ssid));
2065 ssid.wi_len = htole16(buflen);
2066 memcpy(ssid.wi_ssid, buf, buflen);
2067 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
2068 }
2069
2070 STATIC int
2071 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
2072 {
2073 struct wi_softc *sc = ifp->if_softc;
2074 struct ieee80211com *ic = &sc->sc_ic;
2075 struct ifreq *ifr = (struct ifreq *)data;
2076 struct wi_req wreq;
2077 int len, n, error;
2078
2079 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2080 if (error)
2081 return error;
2082 len = (wreq.wi_len - 1) * 2;
2083 if (len < sizeof(u_int16_t))
2084 return ENOSPC;
2085 if (len > sizeof(wreq.wi_val))
2086 len = sizeof(wreq.wi_val);
2087
2088 switch (wreq.wi_type) {
2089
2090 case WI_RID_IFACE_STATS:
2091 memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
2092 if (len < sizeof(sc->sc_stats))
2093 error = ENOSPC;
2094 else
2095 len = sizeof(sc->sc_stats);
2096 break;
2097
2098 case WI_RID_ENCRYPTION:
2099 case WI_RID_TX_CRYPT_KEY:
2100 case WI_RID_DEFLT_CRYPT_KEYS:
2101 case WI_RID_TX_RATE:
2102 return ieee80211_cfgget(ic, cmd, data);
2103
2104 case WI_RID_MICROWAVE_OVEN:
2105 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
2106 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2107 &len);
2108 break;
2109 }
2110 wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
2111 len = sizeof(u_int16_t);
2112 break;
2113
2114 case WI_RID_DBM_ADJUST:
2115 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
2116 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2117 &len);
2118 break;
2119 }
2120 wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
2121 len = sizeof(u_int16_t);
2122 break;
2123
2124 case WI_RID_ROAMING_MODE:
2125 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
2126 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2127 &len);
2128 break;
2129 }
2130 wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
2131 len = sizeof(u_int16_t);
2132 break;
2133
2134 case WI_RID_SYSTEM_SCALE:
2135 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
2136 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2137 &len);
2138 break;
2139 }
2140 wreq.wi_val[0] = htole16(sc->sc_system_scale);
2141 len = sizeof(u_int16_t);
2142 break;
2143
2144 case WI_RID_FRAG_THRESH:
2145 if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
2146 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2147 &len);
2148 break;
2149 }
2150 wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
2151 len = sizeof(u_int16_t);
2152 break;
2153
2154 case WI_RID_READ_APS:
2155 #ifndef IEEE80211_NO_HOSTAP
2156 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2157 return ieee80211_cfgget(ic, cmd, data);
2158 #endif /* !IEEE80211_NO_HOSTAP */
2159 if (sc->sc_scan_timer > 0) {
2160 error = EINPROGRESS;
2161 break;
2162 }
2163 n = sc->sc_naps;
2164 if (len < sizeof(n)) {
2165 error = ENOSPC;
2166 break;
2167 }
2168 if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
2169 n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
2170 len = sizeof(n) + sizeof(struct wi_apinfo) * n;
2171 memcpy(wreq.wi_val, &n, sizeof(n));
2172 memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
2173 sizeof(struct wi_apinfo) * n);
2174 break;
2175
2176 default:
2177 if (sc->sc_enabled) {
2178 error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
2179 &len);
2180 break;
2181 }
2182 switch (wreq.wi_type) {
2183 case WI_RID_MAX_DATALEN:
2184 wreq.wi_val[0] = htole16(sc->sc_max_datalen);
2185 len = sizeof(u_int16_t);
2186 break;
2187 case WI_RID_FRAG_THRESH:
2188 wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
2189 len = sizeof(u_int16_t);
2190 break;
2191 case WI_RID_RTS_THRESH:
2192 wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
2193 len = sizeof(u_int16_t);
2194 break;
2195 case WI_RID_CNFAUTHMODE:
2196 wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
2197 len = sizeof(u_int16_t);
2198 break;
2199 case WI_RID_NODENAME:
2200 if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
2201 error = ENOSPC;
2202 break;
2203 }
2204 len = sc->sc_nodelen + sizeof(u_int16_t);
2205 wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
2206 memcpy(&wreq.wi_val[1], sc->sc_nodename,
2207 sc->sc_nodelen);
2208 break;
2209 default:
2210 return ieee80211_cfgget(ic, cmd, data);
2211 }
2212 break;
2213 }
2214 if (error)
2215 return error;
2216 wreq.wi_len = (len + 1) / 2 + 1;
2217 return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
2218 }
2219
2220 STATIC int
2221 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
2222 {
2223 struct wi_softc *sc = ifp->if_softc;
2224 struct ieee80211com *ic = &sc->sc_ic;
2225 struct ifreq *ifr = (struct ifreq *)data;
2226 struct ieee80211_rateset *rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2227 struct wi_req wreq;
2228 struct mbuf *m;
2229 int i, len, error;
2230
2231 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
2232 if (error)
2233 return error;
2234 len = (wreq.wi_len - 1) * 2;
2235 switch (wreq.wi_type) {
2236 case WI_RID_DBM_ADJUST:
2237 return ENODEV;
2238
2239 case WI_RID_NODENAME:
2240 if (le16toh(wreq.wi_val[0]) * 2 > len ||
2241 le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
2242 error = ENOSPC;
2243 break;
2244 }
2245 if (sc->sc_enabled) {
2246 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2247 len);
2248 if (error)
2249 break;
2250 }
2251 sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
2252 memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
2253 break;
2254
2255 case WI_RID_MICROWAVE_OVEN:
2256 case WI_RID_ROAMING_MODE:
2257 case WI_RID_SYSTEM_SCALE:
2258 case WI_RID_FRAG_THRESH:
2259 if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
2260 (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
2261 break;
2262 if (wreq.wi_type == WI_RID_ROAMING_MODE &&
2263 (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
2264 break;
2265 if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
2266 (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
2267 break;
2268 if (wreq.wi_type == WI_RID_FRAG_THRESH &&
2269 (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
2270 break;
2271 /* FALLTHROUGH */
2272 case WI_RID_RTS_THRESH:
2273 case WI_RID_CNFAUTHMODE:
2274 case WI_RID_MAX_DATALEN:
2275 if (sc->sc_enabled) {
2276 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2277 sizeof(u_int16_t));
2278 if (error)
2279 break;
2280 }
2281 switch (wreq.wi_type) {
2282 case WI_RID_FRAG_THRESH:
2283 sc->sc_frag_thresh = le16toh(wreq.wi_val[0]);
2284 break;
2285 case WI_RID_RTS_THRESH:
2286 sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
2287 break;
2288 case WI_RID_MICROWAVE_OVEN:
2289 sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
2290 break;
2291 case WI_RID_ROAMING_MODE:
2292 sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
2293 break;
2294 case WI_RID_SYSTEM_SCALE:
2295 sc->sc_system_scale = le16toh(wreq.wi_val[0]);
2296 break;
2297 case WI_RID_CNFAUTHMODE:
2298 sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
2299 break;
2300 case WI_RID_MAX_DATALEN:
2301 sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
2302 break;
2303 }
2304 break;
2305
2306 case WI_RID_TX_RATE:
2307 switch (le16toh(wreq.wi_val[0])) {
2308 case 3:
2309 ic->ic_fixed_rate = -1;
2310 break;
2311 default:
2312 for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
2313 if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
2314 / 2 == le16toh(wreq.wi_val[0]))
2315 break;
2316 }
2317 if (i == IEEE80211_RATE_SIZE)
2318 return EINVAL;
2319 ic->ic_fixed_rate = i;
2320 }
2321 if (sc->sc_enabled)
2322 error = wi_cfg_txrate(sc);
2323 break;
2324
2325 case WI_RID_SCAN_APS:
2326 if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
2327 error = wi_scan_ap(sc, 0x3fff, 0x000f);
2328 break;
2329
2330 case WI_RID_MGMT_XMIT:
2331 if (!sc->sc_enabled) {
2332 error = ENETDOWN;
2333 break;
2334 }
2335 if (ic->ic_mgtq.ifq_len > 5) {
2336 error = EAGAIN;
2337 break;
2338 }
2339 /* XXX wi_len looks in u_int8_t, not in u_int16_t */
2340 m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
2341 if (m == NULL) {
2342 error = ENOMEM;
2343 break;
2344 }
2345 IF_ENQUEUE(&ic->ic_mgtq, m);
2346 break;
2347
2348 default:
2349 if (sc->sc_enabled) {
2350 error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
2351 len);
2352 if (error)
2353 break;
2354 }
2355 error = ieee80211_cfgset(ic, cmd, data);
2356 break;
2357 }
2358 return error;
2359 }
2360
2361 /* Rate is 0 for hardware auto-select, otherwise rate is
2362 * 2, 4, 11, or 22 (units of 500Kbps).
2363 */
2364 STATIC int
2365 wi_write_txrate(struct wi_softc *sc, int rate)
2366 {
2367 u_int16_t hwrate;
2368
2369 /* rate: 0, 2, 4, 11, 22 */
2370 switch (sc->sc_firmware_type) {
2371 case WI_LUCENT:
2372 switch (rate & IEEE80211_RATE_VAL) {
2373 case 2:
2374 hwrate = 1;
2375 break;
2376 case 4:
2377 hwrate = 2;
2378 break;
2379 default:
2380 hwrate = 3; /* auto */
2381 break;
2382 case 11:
2383 hwrate = 4;
2384 break;
2385 case 22:
2386 hwrate = 5;
2387 break;
2388 }
2389 break;
2390 default:
2391 switch (rate & IEEE80211_RATE_VAL) {
2392 case 2:
2393 hwrate = 1;
2394 break;
2395 case 4:
2396 hwrate = 2;
2397 break;
2398 case 11:
2399 hwrate = 4;
2400 break;
2401 case 22:
2402 hwrate = 8;
2403 break;
2404 default:
2405 hwrate = 15; /* auto */
2406 break;
2407 }
2408 break;
2409 }
2410
2411 if (sc->sc_tx_rate == hwrate)
2412 return 0;
2413
2414 if (sc->sc_if.if_flags & IFF_DEBUG)
2415 printf("%s: tx rate %d -> %d (%d)\n", __func__, sc->sc_tx_rate,
2416 hwrate, rate);
2417
2418 sc->sc_tx_rate = hwrate;
2419
2420 return wi_write_val(sc, WI_RID_TX_RATE, sc->sc_tx_rate);
2421 }
2422
2423 STATIC int
2424 wi_cfg_txrate(struct wi_softc *sc)
2425 {
2426 struct ieee80211com *ic = &sc->sc_ic;
2427 struct ieee80211_rateset *rs;
2428 int rate;
2429
2430 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
2431
2432 sc->sc_tx_rate = 0; /* force write to RID */
2433
2434 if (ic->ic_fixed_rate < 0)
2435 rate = 0; /* auto */
2436 else
2437 rate = rs->rs_rates[ic->ic_fixed_rate];
2438
2439 return wi_write_txrate(sc, rate);
2440 }
2441
2442 STATIC int
2443 wi_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k)
2444 {
2445 int keyix;
2446
2447 if (&ic->ic_nw_keys[0] <= k && k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])
2448 keyix = k - ic->ic_nw_keys;
2449 else
2450 keyix = IEEE80211_KEYIX_NONE;
2451
2452 DPRINTF(("%s: alloc key %u\n", __func__, keyix));
2453
2454 return keyix;
2455 }
2456
2457 STATIC int
2458 wi_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
2459 {
2460 struct wi_softc *sc = ic->ic_ifp->if_softc;
2461 u_int keyix = k->wk_keyix;
2462
2463 DPRINTF(("%s: delete key %u\n", __func__, keyix));
2464
2465 if (keyix >= IEEE80211_WEP_NKID)
2466 return 0;
2467 if (k->wk_keylen != 0)
2468 sc->sc_flags &= ~WI_FLAGS_WEP_VALID;
2469
2470 return 1;
2471 }
2472
2473 static int
2474 wi_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
2475 const u_int8_t mac[IEEE80211_ADDR_LEN])
2476 {
2477 struct wi_softc *sc = ic->ic_ifp->if_softc;
2478
2479 DPRINTF(("%s: set key %u\n", __func__, k->wk_keyix));
2480
2481 if (k->wk_keyix >= IEEE80211_WEP_NKID)
2482 return 0;
2483
2484 sc->sc_flags &= ~WI_FLAGS_WEP_VALID;
2485
2486 return 1;
2487 }
2488
2489 STATIC void
2490 wi_key_update_begin(struct ieee80211com *ic)
2491 {
2492 DPRINTF(("%s:\n", __func__));
2493 }
2494
2495 STATIC void
2496 wi_key_update_end(struct ieee80211com *ic)
2497 {
2498 struct ifnet *ifp = ic->ic_ifp;
2499 struct wi_softc *sc = ifp->if_softc;
2500
2501 DPRINTF(("%s:\n", __func__));
2502
2503 if ((sc->sc_flags & WI_FLAGS_WEP_VALID) != 0)
2504 return;
2505 if ((ic->ic_caps & IEEE80211_C_WEP) != 0 && sc->sc_enabled &&
2506 !sc->sc_invalid)
2507 (void)wi_write_wep(sc);
2508 }
2509
2510 STATIC int
2511 wi_write_wep(struct wi_softc *sc)
2512 {
2513 struct ifnet *ifp = &sc->sc_if;
2514 struct ieee80211com *ic = &sc->sc_ic;
2515 int error = 0;
2516 int i, keylen;
2517 u_int16_t val;
2518 struct wi_key wkey[IEEE80211_WEP_NKID];
2519
2520 if ((ifp->if_flags & IFF_RUNNING) != 0)
2521 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
2522
2523 switch (sc->sc_firmware_type) {
2524 case WI_LUCENT:
2525 val = (ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
2526 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
2527 if (error)
2528 break;
2529 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_def_txkey);
2530 if (error)
2531 break;
2532 memset(wkey, 0, sizeof(wkey));
2533 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2534 keylen = ic->ic_nw_keys[i].wk_keylen;
2535 wkey[i].wi_keylen = htole16(keylen);
2536 memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
2537 keylen);
2538 }
2539 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
2540 wkey, sizeof(wkey));
2541 break;
2542
2543 case WI_INTERSIL:
2544 case WI_SYMBOL:
2545 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2546 /*
2547 * ONLY HWB3163 EVAL-CARD Firmware version
2548 * less than 0.8 variant2
2549 *
2550 * If promiscuous mode disable, Prism2 chip
2551 * does not work with WEP .
2552 * It is under investigation for details.
2553 * (ichiro (at) NetBSD.org)
2554 */
2555 if (sc->sc_firmware_type == WI_INTERSIL &&
2556 sc->sc_sta_firmware_ver < 802 ) {
2557 /* firm ver < 0.8 variant 2 */
2558 wi_write_val(sc, WI_RID_PROMISC, 1);
2559 }
2560 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2561 sc->sc_cnfauthmode);
2562 val = PRIVACY_INVOKED;
2563 if ((sc->sc_ic_flags & IEEE80211_F_DROPUNENC) != 0)
2564 val |= EXCLUDE_UNENCRYPTED;
2565 #ifndef IEEE80211_NO_HOSTAP
2566 /*
2567 * Encryption firmware has a bug for HostAP mode.
2568 */
2569 if (sc->sc_firmware_type == WI_INTERSIL &&
2570 ic->ic_opmode == IEEE80211_M_HOSTAP)
2571 val |= HOST_ENCRYPT;
2572 #endif /* !IEEE80211_NO_HOSTAP */
2573 } else {
2574 wi_write_val(sc, WI_RID_CNFAUTHMODE,
2575 IEEE80211_AUTH_OPEN);
2576 val = HOST_ENCRYPT | HOST_DECRYPT;
2577 }
2578 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
2579 if (error)
2580 break;
2581 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
2582 ic->ic_def_txkey);
2583 if (error)
2584 break;
2585 /*
2586 * It seems that the firmware accept 104bit key only if
2587 * all the keys have 104bit length. We get the length of
2588 * the transmit key and use it for all other keys.
2589 * Perhaps we should use software WEP for such situation.
2590 */
2591 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE ||
2592 IEEE80211_KEY_UNDEFINED(ic->ic_nw_keys[ic->ic_def_txkey]))
2593 keylen = 13; /* No keys => 104bit ok */
2594 else
2595 keylen = ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen;
2596
2597 if (keylen > IEEE80211_WEP_KEYLEN)
2598 keylen = 13; /* 104bit keys */
2599 else
2600 keylen = IEEE80211_WEP_KEYLEN;
2601 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2602 error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
2603 ic->ic_nw_keys[i].wk_key, keylen);
2604 if (error)
2605 break;
2606 }
2607 break;
2608 }
2609 if ((ifp->if_flags & IFF_RUNNING) != 0)
2610 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
2611 if (error == 0)
2612 sc->sc_flags |= WI_FLAGS_WEP_VALID;
2613 return error;
2614 }
2615
2616 /* Must be called at proper protection level! */
2617 STATIC int
2618 wi_cmd_start(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2619 {
2620 #ifdef WI_HISTOGRAM
2621 static int hist1[11];
2622 static int hist1count;
2623 #endif
2624 int i;
2625
2626 /* wait for the busy bit to clear */
2627 for (i = 500; i > 0; i--) { /* 5s */
2628 if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
2629 break;
2630 if (sc->sc_invalid)
2631 return ENXIO;
2632 DELAY(1000); /* 1 m sec */
2633 }
2634 if (i == 0) {
2635 printf("%s: wi_cmd: busy bit won't clear.\n",
2636 sc->sc_dev.dv_xname);
2637 return(ETIMEDOUT);
2638 }
2639 #ifdef WI_HISTOGRAM
2640 if (i > 490)
2641 hist1[500 - i]++;
2642 else
2643 hist1[10]++;
2644 if (++hist1count == 1000) {
2645 hist1count = 0;
2646 printf("%s: hist1: %d %d %d %d %d %d %d %d %d %d %d\n",
2647 sc->sc_dev.dv_xname,
2648 hist1[0], hist1[1], hist1[2], hist1[3], hist1[4],
2649 hist1[5], hist1[6], hist1[7], hist1[8], hist1[9],
2650 hist1[10]);
2651 }
2652 #endif
2653 CSR_WRITE_2(sc, WI_PARAM0, val0);
2654 CSR_WRITE_2(sc, WI_PARAM1, val1);
2655 CSR_WRITE_2(sc, WI_PARAM2, val2);
2656 CSR_WRITE_2(sc, WI_COMMAND, cmd);
2657
2658 return 0;
2659 }
2660
2661 STATIC int
2662 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
2663 {
2664 int rc;
2665
2666 #ifdef WI_DEBUG
2667 if (wi_debug) {
2668 printf("%s: [enter] %d txcmds outstanding\n", __func__,
2669 sc->sc_txcmds);
2670 }
2671 #endif
2672 if (sc->sc_txcmds > 0)
2673 wi_txcmd_wait(sc);
2674
2675 if ((rc = wi_cmd_start(sc, cmd, val0, val1, val2)) != 0)
2676 return rc;
2677
2678 if (cmd == WI_CMD_INI) {
2679 /* XXX: should sleep here. */
2680 if (sc->sc_invalid)
2681 return ENXIO;
2682 DELAY(100*1000);
2683 }
2684 rc = wi_cmd_wait(sc, cmd, val0);
2685
2686 #ifdef WI_DEBUG
2687 if (wi_debug) {
2688 printf("%s: [ ] %d txcmds outstanding\n", __func__,
2689 sc->sc_txcmds);
2690 }
2691 #endif
2692 if (sc->sc_txcmds > 0)
2693 wi_cmd_intr(sc);
2694
2695 #ifdef WI_DEBUG
2696 if (wi_debug) {
2697 printf("%s: [leave] %d txcmds outstanding\n", __func__,
2698 sc->sc_txcmds);
2699 }
2700 #endif
2701 return rc;
2702 }
2703
2704 STATIC int
2705 wi_cmd_wait(struct wi_softc *sc, int cmd, int val0)
2706 {
2707 #ifdef WI_HISTOGRAM
2708 static int hist2[11];
2709 static int hist2count;
2710 #endif
2711 int i, status;
2712 #ifdef WI_DEBUG
2713 if (wi_debug > 1)
2714 printf("%s: cmd=%#x, arg=%#x\n", __func__, cmd, val0);
2715 #endif /* WI_DEBUG */
2716
2717 /* wait for the cmd completed bit */
2718 for (i = 0; i < WI_TIMEOUT; i++) {
2719 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
2720 break;
2721 if (sc->sc_invalid)
2722 return ENXIO;
2723 DELAY(WI_DELAY);
2724 }
2725
2726 #ifdef WI_HISTOGRAM
2727 if (i < 100)
2728 hist2[i/10]++;
2729 else
2730 hist2[10]++;
2731 if (++hist2count == 1000) {
2732 hist2count = 0;
2733 printf("%s: hist2: %d %d %d %d %d %d %d %d %d %d %d\n",
2734 sc->sc_dev.dv_xname,
2735 hist2[0], hist2[1], hist2[2], hist2[3], hist2[4],
2736 hist2[5], hist2[6], hist2[7], hist2[8], hist2[9],
2737 hist2[10]);
2738 }
2739 #endif
2740
2741 status = CSR_READ_2(sc, WI_STATUS);
2742
2743 if (i == WI_TIMEOUT) {
2744 printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
2745 sc->sc_dev.dv_xname, cmd, val0);
2746 return ETIMEDOUT;
2747 }
2748
2749 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
2750
2751 if (status & WI_STAT_CMD_RESULT) {
2752 printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
2753 sc->sc_dev.dv_xname, cmd, val0);
2754 return EIO;
2755 }
2756 return 0;
2757 }
2758
2759 STATIC int
2760 wi_seek_bap(struct wi_softc *sc, int id, int off)
2761 {
2762 #ifdef WI_HISTOGRAM
2763 static int hist4[11];
2764 static int hist4count;
2765 #endif
2766 int i, status;
2767
2768 CSR_WRITE_2(sc, WI_SEL0, id);
2769 CSR_WRITE_2(sc, WI_OFF0, off);
2770
2771 for (i = 0; ; i++) {
2772 status = CSR_READ_2(sc, WI_OFF0);
2773 if ((status & WI_OFF_BUSY) == 0)
2774 break;
2775 if (i == WI_TIMEOUT) {
2776 printf("%s: timeout in wi_seek to %x/%x\n",
2777 sc->sc_dev.dv_xname, id, off);
2778 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2779 return ETIMEDOUT;
2780 }
2781 if (sc->sc_invalid)
2782 return ENXIO;
2783 DELAY(2);
2784 }
2785 #ifdef WI_HISTOGRAM
2786 if (i < 100)
2787 hist4[i/10]++;
2788 else
2789 hist4[10]++;
2790 if (++hist4count == 2500) {
2791 hist4count = 0;
2792 printf("%s: hist4: %d %d %d %d %d %d %d %d %d %d %d\n",
2793 sc->sc_dev.dv_xname,
2794 hist4[0], hist4[1], hist4[2], hist4[3], hist4[4],
2795 hist4[5], hist4[6], hist4[7], hist4[8], hist4[9],
2796 hist4[10]);
2797 }
2798 #endif
2799 if (status & WI_OFF_ERR) {
2800 printf("%s: failed in wi_seek to %x/%x\n",
2801 sc->sc_dev.dv_xname, id, off);
2802 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2803 return EIO;
2804 }
2805 sc->sc_bap_id = id;
2806 sc->sc_bap_off = off;
2807 return 0;
2808 }
2809
2810 STATIC int
2811 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2812 {
2813 int error, cnt;
2814
2815 if (buflen == 0)
2816 return 0;
2817 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2818 if ((error = wi_seek_bap(sc, id, off)) != 0)
2819 return error;
2820 }
2821 cnt = (buflen + 1) / 2;
2822 CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
2823 sc->sc_bap_off += cnt * 2;
2824 return 0;
2825 }
2826
2827 STATIC int
2828 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
2829 {
2830 int error, cnt;
2831
2832 if (buflen == 0)
2833 return 0;
2834
2835 #ifdef WI_HERMES_AUTOINC_WAR
2836 again:
2837 #endif
2838 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
2839 if ((error = wi_seek_bap(sc, id, off)) != 0)
2840 return error;
2841 }
2842 cnt = (buflen + 1) / 2;
2843 CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
2844 sc->sc_bap_off += cnt * 2;
2845
2846 #ifdef WI_HERMES_AUTOINC_WAR
2847 /*
2848 * According to the comments in the HCF Light code, there is a bug
2849 * in the Hermes (or possibly in certain Hermes firmware revisions)
2850 * where the chip's internal autoincrement counter gets thrown off
2851 * during data writes: the autoincrement is missed, causing one
2852 * data word to be overwritten and subsequent words to be written to
2853 * the wrong memory locations. The end result is that we could end
2854 * up transmitting bogus frames without realizing it. The workaround
2855 * for this is to write a couple of extra guard words after the end
2856 * of the transfer, then attempt to read then back. If we fail to
2857 * locate the guard words where we expect them, we preform the
2858 * transfer over again.
2859 */
2860 if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
2861 CSR_WRITE_2(sc, WI_DATA0, 0x1234);
2862 CSR_WRITE_2(sc, WI_DATA0, 0x5678);
2863 wi_seek_bap(sc, id, sc->sc_bap_off);
2864 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
2865 if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
2866 CSR_READ_2(sc, WI_DATA0) != 0x5678) {
2867 printf("%s: detect auto increment bug, try again\n",
2868 sc->sc_dev.dv_xname);
2869 goto again;
2870 }
2871 }
2872 #endif
2873 return 0;
2874 }
2875
2876 STATIC int
2877 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
2878 {
2879 int error, len;
2880 struct mbuf *m;
2881
2882 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
2883 if (m->m_len == 0)
2884 continue;
2885
2886 len = min(m->m_len, totlen);
2887
2888 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
2889 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
2890 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
2891 totlen);
2892 }
2893
2894 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
2895 return error;
2896
2897 off += m->m_len;
2898 totlen -= len;
2899 }
2900 return 0;
2901 }
2902
2903 STATIC int
2904 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
2905 {
2906 int i;
2907
2908 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
2909 printf("%s: failed to allocate %d bytes on NIC\n",
2910 sc->sc_dev.dv_xname, len);
2911 return ENOMEM;
2912 }
2913
2914 for (i = 0; i < WI_TIMEOUT; i++) {
2915 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
2916 break;
2917 DELAY(1);
2918 }
2919 if (i == WI_TIMEOUT) {
2920 printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
2921 return ETIMEDOUT;
2922 }
2923 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
2924 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
2925 return 0;
2926 }
2927
2928 STATIC int
2929 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
2930 {
2931 int error, len;
2932 u_int16_t ltbuf[2];
2933
2934 /* Tell the NIC to enter record read mode. */
2935 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
2936 if (error)
2937 return error;
2938
2939 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2940 if (error)
2941 return error;
2942
2943 if (le16toh(ltbuf[0]) == 0)
2944 return EOPNOTSUPP;
2945 if (le16toh(ltbuf[1]) != rid) {
2946 printf("%s: record read mismatch, rid=%x, got=%x\n",
2947 sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
2948 return EIO;
2949 }
2950 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
2951 if (*buflenp < len) {
2952 printf("%s: record buffer is too small, "
2953 "rid=%x, size=%d, len=%d\n",
2954 sc->sc_dev.dv_xname, rid, *buflenp, len);
2955 return ENOSPC;
2956 }
2957 *buflenp = len;
2958 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
2959 }
2960
2961 STATIC int
2962 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
2963 {
2964 int error;
2965 u_int16_t ltbuf[2];
2966
2967 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
2968 ltbuf[1] = htole16(rid);
2969
2970 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
2971 if (error)
2972 return error;
2973 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2974 if (error)
2975 return error;
2976
2977 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2978 }
2979
2980 STATIC void
2981 wi_rssadapt_updatestats_cb(void *arg, struct ieee80211_node *ni)
2982 {
2983 struct wi_node *wn = (void*)ni;
2984 ieee80211_rssadapt_updatestats(&wn->wn_rssadapt);
2985 }
2986
2987 STATIC void
2988 wi_rssadapt_updatestats(void *arg)
2989 {
2990 struct wi_softc *sc = arg;
2991 struct ieee80211com *ic = &sc->sc_ic;
2992 ieee80211_iterate_nodes(&ic->ic_sta, wi_rssadapt_updatestats_cb, arg);
2993 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
2994 ic->ic_state == IEEE80211_S_RUN)
2995 callout_reset(&sc->sc_rssadapt_ch, hz / 10,
2996 wi_rssadapt_updatestats, arg);
2997 }
2998
2999 /*
3000 * In HOSTAP mode, restore IEEE80211_F_DROPUNENC when operating
3001 * with WEP enabled so that the AP drops unencoded frames at the
3002 * 802.11 layer.
3003 *
3004 * In all other modes, clear IEEE80211_F_DROPUNENC when operating
3005 * with WEP enabled so we don't drop unencoded frames at the 802.11
3006 * layer. This is necessary because we must strip the WEP bit from
3007 * the 802.11 header before passing frames to ieee80211_input
3008 * because the card has already stripped the WEP crypto header from
3009 * the packet.
3010 */
3011 STATIC void
3012 wi_mend_flags(struct wi_softc *sc, enum ieee80211_state nstate)
3013 {
3014 struct ieee80211com *ic = &sc->sc_ic;
3015
3016 if (nstate == IEEE80211_S_RUN &&
3017 (ic->ic_flags & IEEE80211_F_PRIVACY) != 0 &&
3018 ic->ic_opmode != IEEE80211_M_HOSTAP)
3019 ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
3020 else
3021 ic->ic_flags |= sc->sc_ic_flags;
3022
3023 DPRINTF(("%s: state %d, "
3024 "ic->ic_flags & IEEE80211_F_DROPUNENC = %#" PRIx32 ", "
3025 "sc->sc_ic_flags & IEEE80211_F_DROPUNENC = %#" PRIx32 "\n",
3026 __func__, nstate,
3027 ic->ic_flags & IEEE80211_F_DROPUNENC,
3028 sc->sc_ic_flags & IEEE80211_F_DROPUNENC));
3029 }
3030
3031 STATIC int
3032 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
3033 {
3034 struct ifnet *ifp = ic->ic_ifp;
3035 struct wi_softc *sc = ifp->if_softc;
3036 struct ieee80211_node *ni = ic->ic_bss;
3037 u_int16_t val;
3038 struct wi_ssid ssid;
3039 struct wi_macaddr bssid, old_bssid;
3040 enum ieee80211_state ostate;
3041 #ifdef WI_DEBUG
3042 static const char *stname[] =
3043 { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
3044 #endif /* WI_DEBUG */
3045
3046 ostate = ic->ic_state;
3047 DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
3048
3049 switch (nstate) {
3050 case IEEE80211_S_INIT:
3051 if (ic->ic_opmode != IEEE80211_M_MONITOR)
3052 callout_stop(&sc->sc_rssadapt_ch);
3053 ic->ic_flags &= ~IEEE80211_F_SIBSS;
3054 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
3055 break;
3056
3057 case IEEE80211_S_SCAN:
3058 case IEEE80211_S_AUTH:
3059 case IEEE80211_S_ASSOC:
3060 ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
3061 wi_mend_flags(sc, nstate);
3062 return 0;
3063
3064 case IEEE80211_S_RUN:
3065 sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
3066 IEEE80211_ADDR_COPY(old_bssid.wi_mac_addr, ni->ni_bssid);
3067 wi_read_xrid(sc, WI_RID_CURRENT_BSSID, &bssid,
3068 IEEE80211_ADDR_LEN);
3069 IEEE80211_ADDR_COPY(ni->ni_bssid, &bssid);
3070 IEEE80211_ADDR_COPY(ni->ni_macaddr, &bssid);
3071 wi_read_xrid(sc, WI_RID_CURRENT_CHAN, &val, sizeof(val));
3072 if (!isset(ic->ic_chan_avail, le16toh(val)))
3073 panic("%s: invalid channel %d\n", sc->sc_dev.dv_xname,
3074 le16toh(val));
3075 ni->ni_chan = &ic->ic_channels[le16toh(val)];
3076
3077 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
3078 #ifndef IEEE80211_NO_HOSTAP
3079 ni->ni_esslen = ic->ic_des_esslen;
3080 memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
3081 ni->ni_rates = ic->ic_sup_rates[
3082 ieee80211_chan2mode(ic, ni->ni_chan)];
3083 ni->ni_intval = ic->ic_lintval;
3084 ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
3085 if (ic->ic_flags & IEEE80211_F_PRIVACY)
3086 ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
3087 #endif /* !IEEE80211_NO_HOSTAP */
3088 } else {
3089 wi_read_xrid(sc, WI_RID_CURRENT_SSID, &ssid,
3090 sizeof(ssid));
3091 ni->ni_esslen = le16toh(ssid.wi_len);
3092 if (ni->ni_esslen > IEEE80211_NWID_LEN)
3093 ni->ni_esslen = IEEE80211_NWID_LEN; /*XXX*/
3094 memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
3095 ni->ni_rates = ic->ic_sup_rates[
3096 ieee80211_chan2mode(ic, ni->ni_chan)]; /*XXX*/
3097 }
3098 if (ic->ic_opmode != IEEE80211_M_MONITOR)
3099 callout_reset(&sc->sc_rssadapt_ch, hz / 10,
3100 wi_rssadapt_updatestats, sc);
3101 /* Trigger routing socket messages. XXX Copied from
3102 * ieee80211_newstate.
3103 */
3104 if (ic->ic_opmode == IEEE80211_M_STA)
3105 ieee80211_notify_node_join(ic, ic->ic_bss,
3106 arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
3107 break;
3108 }
3109 wi_mend_flags(sc, nstate);
3110 return (*sc->sc_newstate)(ic, nstate, arg);
3111 }
3112
3113 STATIC void
3114 wi_set_tim(struct ieee80211com *ic, struct ieee80211_node *ni, int set)
3115 {
3116 struct wi_softc *sc = ic->ic_ifp->if_softc;
3117
3118 (*sc->sc_set_tim)(ic, ni, set);
3119
3120 if ((ic->ic_flags & IEEE80211_F_TIMUPDATE) == 0)
3121 return;
3122
3123 ic->ic_flags &= ~IEEE80211_F_TIMUPDATE;
3124
3125 (void)wi_write_val(sc, WI_RID_SET_TIM,
3126 IEEE80211_AID(ni->ni_associd) | (set ? 0x8000 : 0));
3127 }
3128
3129 STATIC int
3130 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
3131 {
3132 int error = 0;
3133 u_int16_t val[2];
3134
3135 if (!sc->sc_enabled)
3136 return ENXIO;
3137 switch (sc->sc_firmware_type) {
3138 case WI_LUCENT:
3139 (void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
3140 break;
3141 case WI_INTERSIL:
3142 val[0] = htole16(chanmask); /* channel */
3143 val[1] = htole16(txrate); /* tx rate */
3144 error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
3145 break;
3146 case WI_SYMBOL:
3147 /*
3148 * XXX only supported on 3.x ?
3149 */
3150 val[0] = htole16(BSCAN_BCAST | BSCAN_ONETIME);
3151 error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
3152 val, sizeof(val[0]));
3153 break;
3154 }
3155 if (error == 0) {
3156 sc->sc_scan_timer = WI_SCAN_WAIT;
3157 sc->sc_if.if_timer = 1;
3158 DPRINTF(("wi_scan_ap: start scanning, "
3159 "chanmask 0x%x txrate 0x%x\n", chanmask, txrate));
3160 }
3161 return error;
3162 }
3163
3164 STATIC void
3165 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
3166 {
3167 #define N(a) (sizeof (a) / sizeof (a[0]))
3168 int i, naps, off, szbuf;
3169 struct wi_scan_header ws_hdr; /* Prism2 header */
3170 struct wi_scan_data_p2 ws_dat; /* Prism2 scantable*/
3171 struct wi_apinfo *ap;
3172
3173 off = sizeof(u_int16_t) * 2;
3174 memset(&ws_hdr, 0, sizeof(ws_hdr));
3175 switch (sc->sc_firmware_type) {
3176 case WI_INTERSIL:
3177 wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
3178 off += sizeof(ws_hdr);
3179 szbuf = sizeof(struct wi_scan_data_p2);
3180 break;
3181 case WI_SYMBOL:
3182 szbuf = sizeof(struct wi_scan_data_p2) + 6;
3183 break;
3184 case WI_LUCENT:
3185 szbuf = sizeof(struct wi_scan_data);
3186 break;
3187 default:
3188 printf("%s: wi_scan_result: unknown firmware type %u\n",
3189 sc->sc_dev.dv_xname, sc->sc_firmware_type);
3190 naps = 0;
3191 goto done;
3192 }
3193 naps = (cnt * 2 + 2 - off) / szbuf;
3194 if (naps > N(sc->sc_aps))
3195 naps = N(sc->sc_aps);
3196 sc->sc_naps = naps;
3197 /* Read Data */
3198 ap = sc->sc_aps;
3199 memset(&ws_dat, 0, sizeof(ws_dat));
3200 for (i = 0; i < naps; i++, ap++) {
3201 wi_read_bap(sc, fid, off, &ws_dat,
3202 (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
3203 DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
3204 ether_sprintf(ws_dat.wi_bssid)));
3205 off += szbuf;
3206 ap->scanreason = le16toh(ws_hdr.wi_reason);
3207 memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
3208 ap->channel = le16toh(ws_dat.wi_chid);
3209 ap->signal = le16toh(ws_dat.wi_signal);
3210 ap->noise = le16toh(ws_dat.wi_noise);
3211 ap->quality = ap->signal - ap->noise;
3212 ap->capinfo = le16toh(ws_dat.wi_capinfo);
3213 ap->interval = le16toh(ws_dat.wi_interval);
3214 ap->rate = le16toh(ws_dat.wi_rate);
3215 ap->namelen = le16toh(ws_dat.wi_namelen);
3216 if (ap->namelen > sizeof(ap->name))
3217 ap->namelen = sizeof(ap->name);
3218 memcpy(ap->name, ws_dat.wi_name, ap->namelen);
3219 }
3220 done:
3221 /* Done scanning */
3222 sc->sc_scan_timer = 0;
3223 DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
3224 #undef N
3225 }
3226
3227 STATIC void
3228 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
3229 {
3230 ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
3231 ni ? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL
3232 : -1,
3233 rssi);
3234 printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
3235 le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
3236 le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
3237 printf(" rx_signal %u rx_rate %u rx_flow %u\n",
3238 wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
3239 printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
3240 wh->wi_tx_rtry, wh->wi_tx_rate,
3241 le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
3242 printf(" ehdr dst %s src %s type 0x%x\n",
3243 ether_sprintf(wh->wi_ehdr.ether_dhost),
3244 ether_sprintf(wh->wi_ehdr.ether_shost),
3245 wh->wi_ehdr.ether_type);
3246 }
3247