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