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