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