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