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