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