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