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