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