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