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