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