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