Remove some emacs variable settings.
[dragonfly.git] / sys / dev / netif / wi / if_wi.c
CommitLineData
5d8e19b2 1/*-
984263bc
MD
2 * Copyright (c) 1997, 1998, 1999
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
1de703da 31 *
5d8e19b2 32 * $FreeBSD: head/sys/dev/wi/if_wi.c 196970 2009-09-08 13:19:05Z phk $
984263bc
MD
33 */
34
35/*
acffed4b 36 * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
984263bc 37 *
acffed4b 38 * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
984263bc
MD
39 * Electrical Engineering Department
40 * Columbia University, New York City
41 */
42
43/*
44 * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
45 * from Lucent. Unlike the older cards, the new ones are programmed
46 * entirely via a firmware-driven controller called the Hermes.
47 * Unfortunately, Lucent will not release the Hermes programming manual
48 * without an NDA (if at all). What they do release is an API library
49 * called the HCF (Hardware Control Functions) which is supposed to
50 * do the device-specific operations of a device driver for you. The
51 * publically available version of the HCF library (the 'HCF Light') is
52 * a) extremely gross, b) lacks certain features, particularly support
53 * for 802.11 frames, and c) is contaminated by the GNU Public License.
54 *
55 * This driver does not use the HCF or HCF Light at all. Instead, it
56 * programs the Hermes controller directly, using information gleaned
57 * from the HCF Light code and corresponding documentation.
58 *
59 * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
60 * WaveLan cards (based on the Hermes chipset), as well as the newer
61 * Prism 2 chipsets with firmware from Intersil and Symbol.
62 */
63
acffed4b 64
5d8e19b2 65#define WI_HERMES_STATS_WAR /* Work around stats counter bug. */
2b71c8f1 66
984263bc 67#include <sys/param.h>
acffed4b 68#include <sys/systm.h>
5d8e19b2 69#include <sys/endian.h>
984263bc
MD
70#include <sys/sockio.h>
71#include <sys/mbuf.h>
895c1f85 72#include <sys/priv.h>
5d8e19b2 73#include <sys/proc.h>
984263bc
MD
74#include <sys/kernel.h>
75#include <sys/socket.h>
76#include <sys/module.h>
77#include <sys/bus.h>
78#include <sys/random.h>
79#include <sys/syslog.h>
80#include <sys/sysctl.h>
81
acffed4b 82#include <machine/atomic.h>
5d8e19b2 83#include <sys/rman.h>
984263bc
MD
84
85#include <net/if.h>
86#include <net/if_arp.h>
87#include <net/ethernet.h>
88#include <net/if_dl.h>
5d8e19b2 89#include <net/if_llc.h>
984263bc
MD
90#include <net/if_media.h>
91#include <net/if_types.h>
4d723e5a 92#include <net/ifq_var.h>
acffed4b
JS
93
94#include <netproto/802_11/ieee80211_var.h>
3ee50d77 95#include <netproto/802_11/ieee80211_ioctl.h>
acffed4b 96#include <netproto/802_11/ieee80211_radiotap.h>
984263bc
MD
97
98#include <netinet/in.h>
99#include <netinet/in_systm.h>
100#include <netinet/in_var.h>
101#include <netinet/ip.h>
102#include <netinet/if_ether.h>
103
104#include <net/bpf.h>
105
5d8e19b2 106#include <dev/netif/wi/if_wavelan_ieee.h>
acffed4b
JS
107#include <dev/netif/wi/if_wireg.h>
108#include <dev/netif/wi/if_wivar.h>
984263bc 109
5d8e19b2
RP
110static struct ieee80211vap *wi_vap_create(struct ieee80211com *ic,
111 const char name[IFNAMSIZ], int unit, int opmode, int flags,
112 const uint8_t bssid[IEEE80211_ADDR_LEN],
113 const uint8_t mac[IEEE80211_ADDR_LEN]);
114static void wi_vap_delete(struct ieee80211vap *vap);
115static void wi_stop_locked(struct wi_softc *sc, int disable);
116static void wi_start_locked(struct ifnet *);
984263bc 117static void wi_start(struct ifnet *);
5d8e19b2
RP
118static int wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr,
119 struct mbuf *m0);
120static int wi_raw_xmit(struct ieee80211_node *, struct mbuf *,
121 const struct ieee80211_bpf_params *);
122static int wi_newstate_sta(struct ieee80211vap *, enum ieee80211_state, int);
123static int wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state,
124 int);
125static void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
126 int subtype, int rssi, int nf);
acffed4b 127static int wi_reset(struct wi_softc *);
e89c26f4 128static void wi_watchdog_callout(void *);
acffed4b 129static int wi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
984263bc
MD
130static void wi_media_status(struct ifnet *, struct ifmediareq *);
131
acffed4b
JS
132static void wi_rx_intr(struct wi_softc *);
133static void wi_tx_intr(struct wi_softc *);
134static void wi_tx_ex_intr(struct wi_softc *);
5d8e19b2 135
acffed4b
JS
136static void wi_info_intr(struct wi_softc *);
137
5d8e19b2
RP
138static int wi_write_txrate(struct wi_softc *, struct ieee80211vap *);
139static int wi_write_wep(struct wi_softc *, struct ieee80211vap *);
acffed4b 140static int wi_write_multi(struct wi_softc *);
5d8e19b2
RP
141static void wi_update_mcast(struct ifnet *);
142static void wi_update_promisc(struct ifnet *);
acffed4b
JS
143static int wi_alloc_fid(struct wi_softc *, int, int *);
144static void wi_read_nicid(struct wi_softc *);
145static int wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
146
147static int wi_cmd(struct wi_softc *, int, int, int, int);
148static int wi_seek_bap(struct wi_softc *, int, int);
149static int wi_read_bap(struct wi_softc *, int, int, void *, int);
150static int wi_write_bap(struct wi_softc *, int, int, void *, int);
151static int wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
152static int wi_read_rid(struct wi_softc *, int, void *, int *);
153static int wi_write_rid(struct wi_softc *, int, void *, int);
5d8e19b2 154static int wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *);
acffed4b 155
5d8e19b2
RP
156static void wi_scan_start(struct ieee80211com *);
157static void wi_scan_end(struct ieee80211com *);
158static void wi_set_channel(struct ieee80211com *);
159
acffed4b
JS
160static __inline int
161wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
162{
163
164 val = htole16(val);
165 return wi_write_rid(sc, rid, &val, sizeof(val));
166}
167
168SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
169
170static struct timeval lasttxerror; /* time of last tx error msg */
171static int curtxeps; /* current tx error msgs/sec */
172static int wi_txerate = 0; /* tx error rate: max msgs/sec */
173SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
174 0, "max tx error msgs/sec; 0 to disable msgs");
175
176#define WI_DEBUG
177#ifdef WI_DEBUG
178static int wi_debug = 0;
179SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
5d8e19b2
RP
180 0, "control debugging printfs");
181#define DPRINTF(X) if (wi_debug) kprintf X
acffed4b
JS
182#else
183#define DPRINTF(X)
acffed4b
JS
184#endif
185
186#define WI_INTRS (WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
984263bc
MD
187
188struct wi_card_ident wi_card_ident[] = {
189 /* CARD_ID CARD_NAME FIRM_TYPE */
190 { WI_NIC_LUCENT_ID, WI_NIC_LUCENT_STR, WI_LUCENT },
191 { WI_NIC_SONY_ID, WI_NIC_SONY_STR, WI_LUCENT },
192 { WI_NIC_LUCENT_EMB_ID, WI_NIC_LUCENT_EMB_STR, WI_LUCENT },
193 { WI_NIC_EVB2_ID, WI_NIC_EVB2_STR, WI_INTERSIL },
194 { WI_NIC_HWB3763_ID, WI_NIC_HWB3763_STR, WI_INTERSIL },
195 { WI_NIC_HWB3163_ID, WI_NIC_HWB3163_STR, WI_INTERSIL },
196 { WI_NIC_HWB3163B_ID, WI_NIC_HWB3163B_STR, WI_INTERSIL },
197 { WI_NIC_EVB3_ID, WI_NIC_EVB3_STR, WI_INTERSIL },
198 { WI_NIC_HWB1153_ID, WI_NIC_HWB1153_STR, WI_INTERSIL },
199 { WI_NIC_P2_SST_ID, WI_NIC_P2_SST_STR, WI_INTERSIL },
200 { WI_NIC_EVB2_SST_ID, WI_NIC_EVB2_SST_STR, WI_INTERSIL },
201 { WI_NIC_3842_EVA_ID, WI_NIC_3842_EVA_STR, WI_INTERSIL },
202 { WI_NIC_3842_PCMCIA_AMD_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
203 { WI_NIC_3842_PCMCIA_SST_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
acffed4b
JS
204 { WI_NIC_3842_PCMCIA_ATL_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
205 { WI_NIC_3842_PCMCIA_ATS_ID, WI_NIC_3842_PCMCIA_STR, WI_INTERSIL },
984263bc
MD
206 { WI_NIC_3842_MINI_AMD_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
207 { WI_NIC_3842_MINI_SST_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
acffed4b
JS
208 { WI_NIC_3842_MINI_ATL_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
209 { WI_NIC_3842_MINI_ATS_ID, WI_NIC_3842_MINI_STR, WI_INTERSIL },
984263bc
MD
210 { WI_NIC_3842_PCI_AMD_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
211 { WI_NIC_3842_PCI_SST_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
acffed4b
JS
212 { WI_NIC_3842_PCI_ATS_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
213 { WI_NIC_3842_PCI_ATL_ID, WI_NIC_3842_PCI_STR, WI_INTERSIL },
984263bc
MD
214 { WI_NIC_P3_PCMCIA_AMD_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
215 { WI_NIC_P3_PCMCIA_SST_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
acffed4b
JS
216 { WI_NIC_P3_PCMCIA_ATL_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
217 { WI_NIC_P3_PCMCIA_ATS_ID, WI_NIC_P3_PCMCIA_STR, WI_INTERSIL },
984263bc
MD
218 { WI_NIC_P3_MINI_AMD_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
219 { WI_NIC_P3_MINI_SST_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
acffed4b
JS
220 { WI_NIC_P3_MINI_ATL_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
221 { WI_NIC_P3_MINI_ATS_ID, WI_NIC_P3_MINI_STR, WI_INTERSIL },
984263bc
MD
222 { 0, NULL, 0 },
223};
224
5d8e19b2
RP
225static char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" };
226
acffed4b 227devclass_t wi_devclass;
984263bc
MD
228
229int
acffed4b 230wi_attach(device_t dev)
984263bc 231{
acffed4b 232 struct wi_softc *sc = device_get_softc(dev);
5d8e19b2
RP
233 struct ieee80211com *ic;
234 struct ifnet *ifp;
acffed4b
JS
235 int i, nrates, buflen;
236 u_int16_t val;
237 u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
238 struct ieee80211_rateset *rs;
5d8e19b2
RP
239 struct sysctl_ctx_list *sctx;
240 struct sysctl_oid *soid;
acffed4b
JS
241 static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
242 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
243 };
244 int error;
5d8e19b2 245 uint8_t macaddr[IEEE80211_ADDR_LEN];
acffed4b 246
5d8e19b2
RP
247 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
248 if (ifp == NULL) {
249 device_printf(dev, "can not if_alloc\n");
250 wi_free(dev);
251 return ENOSPC;
252 }
253 ic = ifp->if_l2com;
4c30eb3b 254
841ab66c 255 sc->sc_firmware_type = WI_NOTYPE;
acffed4b 256 sc->wi_cmd_count = 500;
984263bc 257 /* Reset the NIC. */
5d8e19b2
RP
258 if (wi_reset(sc) != 0) {
259 wi_free(dev);
260 return ENXIO; /* XXX */
261 }
262
263 /* Read NIC identification */
264 wi_read_nicid(sc);
265 switch (sc->sc_firmware_type) {
266 case WI_LUCENT:
267 if (sc->sc_sta_firmware_ver < 60006)
268 goto reject;
269 break;
270 case WI_INTERSIL:
271 if (sc->sc_sta_firmware_ver < 800)
272 goto reject;
273 break;
274 default:
275 reject:
276 device_printf(dev, "Sorry, this card is not supported "
277 "(type %d, firmware ver %d)\n",
278 sc->sc_firmware_type, sc->sc_sta_firmware_ver);
279 wi_free(dev);
280 return EOPNOTSUPP;
281 }
282
283 /* Export info about the device via sysctl */
284 sctx = &sc->sc_sysctl_ctx;
285 sysctl_ctx_init(sctx);
286 soid = SYSCTL_ADD_NODE(sctx, SYSCTL_STATIC_CHILDREN(_hw),
287 OID_AUTO,
288 device_get_nameunit(sc->sc_dev),
289 CTLFLAG_RD, 0, "");
290 if (soid == NULL) {
291 device_printf(sc->sc_dev, "can't add sysctl node\n");
292 return ENXIO;
293 }
294
295 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
296 "firmware_type", CTLFLAG_RD,
297 wi_firmware_names[sc->sc_firmware_type], 0,
298 "Firmware type string");
299 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "sta_version",
300 CTLFLAG_RD, &sc->sc_sta_firmware_ver, 0,
301 "Station Firmware version");
302 if (sc->sc_firmware_type == WI_INTERSIL)
303 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
304 "pri_version", CTLFLAG_RD, &sc->sc_pri_firmware_ver, 0,
305 "Primary Firmware version");
306 SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_id",
307 CTLFLAG_RD, &sc->sc_nic_id, 0, "NIC id");
308 SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_name",
309 CTLFLAG_RD, sc->sc_nic_name, 0, "NIC name");
310
5d8e19b2 311 callout_init(&sc->sc_watchdog);
984263bc
MD
312
313 /*
314 * Read the station address.
315 * And do it twice. I've seen PRISM-based cards that return
316 * an error when trying to read it the first time, which causes
317 * the probe to fail.
318 */
acffed4b 319 buflen = IEEE80211_ADDR_LEN;
5d8e19b2 320 error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
acffed4b
JS
321 if (error != 0) {
322 buflen = IEEE80211_ADDR_LEN;
5d8e19b2 323 error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
0d71884b 324 }
5d8e19b2
RP
325 if (error || IEEE80211_ADDR_EQ(macaddr, empty_macaddr)) {
326 if (error != 0)
327 device_printf(dev, "mac read failed %d\n", error);
328 else {
329 device_printf(dev, "mac read failed (all zeros)\n");
330 error = ENXIO;
331 }
332 wi_free(dev);
333 return (error);
984263bc 334 }
984263bc 335
5d8e19b2
RP
336 ifp->if_softc = sc;
337 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
984263bc
MD
338 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
339 ifp->if_ioctl = wi_ioctl;
984263bc 340 ifp->if_start = wi_start;
984263bc 341 ifp->if_init = wi_init;
4d723e5a
JS
342 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
343 ifq_set_ready(&ifp->if_snd);
984263bc 344
5d8e19b2 345 ic->ic_ifp = ifp;
acffed4b
JS
346 ic->ic_phytype = IEEE80211_T_DS;
347 ic->ic_opmode = IEEE80211_M_STA;
5d8e19b2
RP
348 ic->ic_caps = IEEE80211_C_STA
349 | IEEE80211_C_PMGT
350 | IEEE80211_C_MONITOR
351 ;
984263bc 352
acffed4b
JS
353 /*
354 * Query the card for available channels and setup the
355 * channel table. We assume these are all 11b channels.
356 */
357 buflen = sizeof(val);
358 if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
359 val = htole16(0x1fff); /* assume 1-11 */
360 KASSERT(val != 0, ("wi_attach: no available channels listed!"));
361
362 val <<= 1; /* shift for base 1 indices */
363 for (i = 1; i < 16; i++) {
5d8e19b2 364 struct ieee80211_channel *c;
984263bc 365
5d8e19b2
RP
366 if (!isset((u_int8_t*)&val, i))
367 continue;
368 c = &ic->ic_channels[ic->ic_nchans++];
369 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
370 c->ic_flags = IEEE80211_CHAN_B;
371 c->ic_ieee = i;
372 /* XXX txpowers? */
acffed4b 373 }
984263bc
MD
374
375 /*
376 * Set flags based on firmware version.
377 */
378 switch (sc->sc_firmware_type) {
379 case WI_LUCENT:
acffed4b 380 sc->sc_ntxbuf = 1;
5d8e19b2 381 ic->ic_caps |= IEEE80211_C_IBSS;
acffed4b 382
5d8e19b2
RP
383 sc->sc_ibss_port = WI_PORTTYPE_BSS;
384 sc->sc_monitor_port = WI_PORTTYPE_ADHOC;
acffed4b
JS
385 sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
386 sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
387 sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
984263bc
MD
388 break;
389 case WI_INTERSIL:
acffed4b 390 sc->sc_ntxbuf = WI_NTXBUF;
5d8e19b2
RP
391 sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR
392 | WI_FLAGS_HAS_ROAMING;
acffed4b
JS
393 /*
394 * Old firmware are slow, so give peace a chance.
395 */
396 if (sc->sc_sta_firmware_ver < 10000)
397 sc->wi_cmd_count = 5000;
398 if (sc->sc_sta_firmware_ver > 10101)
399 sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
5d8e19b2 400 ic->ic_caps |= IEEE80211_C_IBSS;
984263bc
MD
401 /*
402 * version 0.8.3 and newer are the only ones that are known
403 * to currently work. Earlier versions can be made to work,
5d8e19b2
RP
404 * at least according to the Linux driver but we require
405 * monitor mode so this is irrelevant.
984263bc 406 */
5d8e19b2
RP
407 ic->ic_caps |= IEEE80211_C_HOSTAP;
408 if (sc->sc_sta_firmware_ver >= 10603)
409 sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY;
410 if (sc->sc_sta_firmware_ver >= 10700) {
411 /*
412 * 1.7.0+ have the necessary support for sta mode WPA.
413 */
414 sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT;
415 ic->ic_caps |= IEEE80211_C_WPA;
416 }
acffed4b 417
5d8e19b2
RP
418 sc->sc_ibss_port = WI_PORTTYPE_IBSS;
419 sc->sc_monitor_port = WI_PORTTYPE_APSILENT;
acffed4b
JS
420 sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
421 sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
422 sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
984263bc
MD
423 break;
424 }
425
426 /*
427 * Find out if we support WEP on this card.
428 */
acffed4b
JS
429 buflen = sizeof(val);
430 if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
431 val != htole16(0))
5d8e19b2 432 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
acffed4b
JS
433
434 /* Find supported rates. */
435 buflen = sizeof(ratebuf);
436 rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
437 if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
438 nrates = le16toh(*(u_int16_t *)ratebuf);
439 if (nrates > IEEE80211_RATE_MAXSIZE)
440 nrates = IEEE80211_RATE_MAXSIZE;
441 rs->rs_nrates = 0;
442 for (i = 0; i < nrates; i++)
443 if (ratebuf[2+i])
444 rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
445 } else {
446 /* XXX fallback on error? */
acffed4b
JS
447 }
448
449 buflen = sizeof(val);
450 if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
451 wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
452 sc->sc_dbm_offset = le16toh(val);
453 }
984263bc 454
acffed4b 455 sc->sc_portnum = WI_DEFAULT_PORT;
acffed4b 456
5d8e19b2
RP
457 ieee80211_ifattach(ic, macaddr);
458 ic->ic_raw_xmit = wi_raw_xmit;
459 ic->ic_scan_start = wi_scan_start;
460 ic->ic_scan_end = wi_scan_end;
461 ic->ic_set_channel = wi_set_channel;
acffed4b 462
5d8e19b2
RP
463 ic->ic_vap_create = wi_vap_create;
464 ic->ic_vap_delete = wi_vap_delete;
465 ic->ic_update_mcast = wi_update_mcast;
466 ic->ic_update_promisc = wi_update_promisc;
984263bc 467
5d8e19b2
RP
468 ieee80211_radiotap_attach(ic,
469 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
470 WI_TX_RADIOTAP_PRESENT,
471 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
472 WI_RX_RADIOTAP_PRESENT);
984263bc 473
5d8e19b2
RP
474 if (bootverbose)
475 ieee80211_announce(ic);
1f8e62c9 476
ee61f228 477 error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE,
5d8e19b2 478 wi_intr, sc, &sc->wi_intrhand, NULL);
bf853d22 479 if (error) {
bf853d22 480 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
5d8e19b2
RP
481 ieee80211_ifdetach(ic);
482 if_free(sc->sc_ifp);
483 wi_free(dev);
484 return error;
bf853d22
JS
485 }
486
5d8e19b2 487 return (0);
984263bc
MD
488}
489
acffed4b
JS
490int
491wi_detach(device_t dev)
984263bc 492{
acffed4b 493 struct wi_softc *sc = device_get_softc(dev);
5d8e19b2
RP
494 struct ifnet *ifp = sc->sc_ifp;
495 struct ieee80211com *ic = ifp->if_l2com;
984263bc 496
acffed4b
JS
497 /* check if device was removed */
498 sc->wi_gone |= !bus_child_present(dev);
984263bc 499
5d8e19b2 500 wi_stop_locked(sc, 0);
5d8e19b2 501 ieee80211_ifdetach(ic);
c90f18fc 502
5d8e19b2
RP
503 bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
504 if_free(sc->sc_ifp);
cdf89432 505 wi_free(dev);
acffed4b
JS
506 return (0);
507}
984263bc 508
5d8e19b2
RP
509static struct ieee80211vap *
510wi_vap_create(struct ieee80211com *ic,
511 const char name[IFNAMSIZ], int unit, int opmode, int flags,
512 const uint8_t bssid[IEEE80211_ADDR_LEN],
513 const uint8_t mac[IEEE80211_ADDR_LEN])
acffed4b 514{
5d8e19b2
RP
515 struct wi_softc *sc = ic->ic_ifp->if_softc;
516 struct wi_vap *wvp;
517 struct ieee80211vap *vap;
acffed4b 518
5d8e19b2
RP
519 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
520 return NULL;
521 wvp = (struct wi_vap *) kmalloc(sizeof(struct wi_vap),
522 M_80211_VAP, M_NOWAIT | M_ZERO);
523 if (wvp == NULL)
524 return NULL;
984263bc 525
5d8e19b2
RP
526 vap = &wvp->wv_vap;
527 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
9c095379 528
5d8e19b2 529 vap->iv_max_aid = WI_MAX_AID;
f35cfef5 530
5d8e19b2
RP
531 switch (opmode) {
532 case IEEE80211_M_STA:
533 sc->sc_porttype = WI_PORTTYPE_BSS;
534 wvp->wv_newstate = vap->iv_newstate;
535 vap->iv_newstate = wi_newstate_sta;
536 /* need to filter mgt frames to avoid confusing state machine */
537 wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
538 vap->iv_recv_mgmt = wi_recv_mgmt;
539 break;
540 case IEEE80211_M_IBSS:
541 sc->sc_porttype = sc->sc_ibss_port;
542 wvp->wv_newstate = vap->iv_newstate;
543 vap->iv_newstate = wi_newstate_sta;
544 break;
545 case IEEE80211_M_AHDEMO:
546 sc->sc_porttype = WI_PORTTYPE_ADHOC;
547 break;
548 case IEEE80211_M_HOSTAP:
549 sc->sc_porttype = WI_PORTTYPE_HOSTAP;
550 wvp->wv_newstate = vap->iv_newstate;
551 vap->iv_newstate = wi_newstate_hostap;
9c095379 552 break;
5d8e19b2
RP
553 case IEEE80211_M_MONITOR:
554 sc->sc_porttype = sc->sc_monitor_port;
9c095379
MD
555 break;
556 default:
9c095379
MD
557 break;
558 }
5d8e19b2
RP
559
560 /* complete setup */
561 ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status);
562 ic->ic_opmode = opmode;
563 return vap;
564}
565
566static void
567wi_vap_delete(struct ieee80211vap *vap)
568{
569 struct wi_vap *wvp = WI_VAP(vap);
570
571 ieee80211_vap_detach(vap);
572 kfree(wvp, M_80211_VAP);
573}
574
575int
576wi_shutdown(device_t dev)
577{
578 struct wi_softc *sc = device_get_softc(dev);
579
580 wi_stop(sc, 1);
581 return (0);
f35cfef5 582}
f35cfef5 583
acffed4b
JS
584void
585wi_intr(void *arg)
984263bc 586{
acffed4b 587 struct wi_softc *sc = arg;
5d8e19b2 588 struct ifnet *ifp = sc->sc_ifp;
acffed4b 589 u_int16_t status;
984263bc 590
acffed4b
JS
591 if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
592 CSR_WRITE_2(sc, WI_INT_EN, 0);
593 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
acffed4b
JS
594 return;
595 }
984263bc 596
acffed4b
JS
597 /* Disable interrupts. */
598 CSR_WRITE_2(sc, WI_INT_EN, 0);
984263bc 599
acffed4b
JS
600 status = CSR_READ_2(sc, WI_EVENT_STAT);
601 if (status & WI_EV_RX)
602 wi_rx_intr(sc);
603 if (status & WI_EV_ALLOC)
604 wi_tx_intr(sc);
605 if (status & WI_EV_TX_EXC)
606 wi_tx_ex_intr(sc);
607 if (status & WI_EV_INFO)
608 wi_info_intr(sc);
609 if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
5d8e19b2
RP
610 !ifq_is_empty(&ifp->if_snd))
611 wi_start_locked(ifp);
984263bc 612
acffed4b
JS
613 /* Re-enable interrupts. */
614 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
984263bc 615
5d8e19b2
RP
616 return;
617}
984263bc 618
5d8e19b2
RP
619static void
620wi_enable(struct wi_softc *sc)
621{
622 /* Enable interrupts */
623 CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
acffed4b 624
5d8e19b2
RP
625 /* enable port */
626 wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
627 sc->sc_enabled = 1;
628}
acffed4b 629
5d8e19b2
RP
630static int
631wi_setup_locked(struct wi_softc *sc, int porttype, int mode,
632 uint8_t mac[IEEE80211_ADDR_LEN])
633{
634 int i;
acffed4b 635
5d8e19b2 636 wi_reset(sc);
acffed4b 637
5d8e19b2
RP
638 wi_write_val(sc, WI_RID_PORTTYPE, porttype);
639 wi_write_val(sc, WI_RID_CREATE_IBSS, mode);
640 wi_write_val(sc, WI_RID_MAX_DATALEN, 2304);
641 /* XXX IEEE80211_BPF_NOACK wants 0 */
642 wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2);
acffed4b 643 if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
5d8e19b2 644 wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */
984263bc 645
5d8e19b2 646 wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN);
acffed4b
JS
647
648 /* Allocate fids for the card */
5d8e19b2
RP
649 sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
650 for (i = 0; i < sc->sc_ntxbuf; i++) {
651 int error = wi_alloc_fid(sc, sc->sc_buflen,
652 &sc->sc_txd[i].d_fid);
653 if (error) {
654 device_printf(sc->sc_dev,
655 "tx buffer allocation failed (error %u)\n",
656 error);
657 return error;
984263bc 658 }
5d8e19b2 659 sc->sc_txd[i].d_len = 0;
acffed4b
JS
660 }
661 sc->sc_txcur = sc->sc_txnext = 0;
984263bc 662
5d8e19b2
RP
663 return 0;
664}
984263bc 665
5d8e19b2
RP
666static void
667wi_init_locked(struct wi_softc *sc)
668{
669 struct ifnet *ifp = sc->sc_ifp;
670 int wasenabled;
984263bc 671
5d8e19b2
RP
672 wasenabled = sc->sc_enabled;
673 if (wasenabled)
674 wi_stop_locked(sc, 1);
675
676 if (wi_setup_locked(sc, sc->sc_porttype, 3, IF_LLADDR(ifp)) != 0) {
acffed4b 677 if_printf(ifp, "interface not running\n");
5d8e19b2
RP
678 wi_stop_locked(sc, 1);
679 return;
acffed4b 680 }
c90f18fc 681
5d8e19b2
RP
682 ifp->if_flags |= IFF_RUNNING;
683 ifp->if_flags &= ~IFF_OACTIVE;
684
e89c26f4 685 callout_reset(&sc->sc_watchdog, hz, wi_watchdog_callout, sc);
5d8e19b2
RP
686
687 wi_enable(sc); /* Enable desired port */
acffed4b 688}
984263bc 689
acffed4b 690void
5d8e19b2 691wi_init(void *arg)
acffed4b 692{
5d8e19b2
RP
693 struct wi_softc *sc = arg;
694 struct ifnet *ifp = sc->sc_ifp;
695 struct ieee80211com *ic = ifp->if_l2com;
696
5d8e19b2 697 wi_init_locked(sc);
984263bc 698
5d8e19b2
RP
699 if (ifp->if_flags & IFF_RUNNING)
700 ieee80211_start_all(ic); /* start all vap's */
701}
702
703static void
704wi_stop_locked(struct wi_softc *sc, int disable)
705{
706 struct ifnet *ifp = sc->sc_ifp;
707
acffed4b
JS
708 if (sc->sc_enabled && !sc->wi_gone) {
709 CSR_WRITE_2(sc, WI_INT_EN, 0);
710 wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
5d8e19b2 711 if (disable)
acffed4b 712 sc->sc_enabled = 0;
acffed4b 713 } else if (sc->wi_gone && disable) /* gone --> not enabled */
5d8e19b2 714 sc->sc_enabled = 0;
acffed4b 715
5d8e19b2 716 callout_stop(&sc->sc_watchdog);
acffed4b 717 sc->sc_tx_timer = 0;
acffed4b 718 sc->sc_false_syns = 0;
5d8e19b2 719
841ab66c 720 ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
984263bc
MD
721}
722
5d8e19b2
RP
723void
724wi_stop(struct wi_softc *sc, int disable)
984263bc 725{
5d8e19b2 726 wi_stop_locked(sc, disable);
5d8e19b2 727}
984263bc 728
5d8e19b2
RP
729static void
730wi_set_channel(struct ieee80211com *ic)
731{
732 struct ifnet *ifp = ic->ic_ifp;
733 struct wi_softc *sc = ifp->if_softc;
984263bc 734
5d8e19b2
RP
735 DPRINTF(("%s: channel %d, %sscanning\n", __func__,
736 ieee80211_chan2ieee(ic, ic->ic_curchan),
737 ic->ic_flags & IEEE80211_F_SCAN ? "" : "!"));
984263bc 738
5d8e19b2
RP
739 wi_write_val(sc, WI_RID_OWN_CHNL,
740 ieee80211_chan2ieee(ic, ic->ic_curchan));
5d8e19b2 741}
841ab66c 742
5d8e19b2
RP
743static void
744wi_scan_start(struct ieee80211com *ic)
745{
746 struct ifnet *ifp = ic->ic_ifp;
747 struct wi_softc *sc = ifp->if_softc;
748 struct ieee80211_scan_state *ss = ic->ic_scan;
9db4b353 749
5d8e19b2 750 DPRINTF(("%s\n", __func__));
841ab66c 751
5d8e19b2
RP
752 /*
753 * Switch device to monitor mode.
754 */
755 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port);
756 if (sc->sc_firmware_type == WI_INTERSIL) {
757 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
758 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
759 }
760 /* force full dwell time to compensate for firmware overhead */
761 ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400);
9db4b353 762
5d8e19b2 763}
841ab66c 764
5d8e19b2
RP
765static void
766wi_scan_end(struct ieee80211com *ic)
767{
768 struct ifnet *ifp = ic->ic_ifp;
769 struct wi_softc *sc = ifp->if_softc;
841ab66c 770
5d8e19b2 771 DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype));
acffed4b 772
5d8e19b2
RP
773 wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype);
774 if (sc->sc_firmware_type == WI_INTERSIL) {
775 wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
776 wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
777 }
5d8e19b2
RP
778}
779
780static void
781wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
782 int subtype, int rssi, int nf)
783{
784 struct ieee80211vap *vap = ni->ni_vap;
785
786 switch (subtype) {
787 case IEEE80211_FC0_SUBTYPE_AUTH:
788 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
789 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
790 /* NB: filter frames that trigger state changes */
791 return;
792 }
793 WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rssi, nf);
794}
795
796static int
797wi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
798{
799 struct ieee80211com *ic = vap->iv_ic;
800 struct ifnet *ifp = ic->ic_ifp;
801 struct ieee80211_node *bss;
802 struct wi_softc *sc = ifp->if_softc;
803
804 DPRINTF(("%s: %s -> %s\n", __func__,
805 ieee80211_state_name[vap->iv_state],
806 ieee80211_state_name[nstate]));
807
808 if (nstate == IEEE80211_S_AUTH) {
5d8e19b2
RP
809 wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr);
810
811 if (vap->iv_flags & IEEE80211_F_PMGTON) {
812 wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
813 wi_write_val(sc, WI_RID_PM_ENABLED, 1);
814 }
815 wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
816 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
817 wi_write_val(sc, WI_RID_FRAG_THRESH,
818 vap->iv_fragthreshold);
819 wi_write_txrate(sc, vap);
820
821 bss = vap->iv_bss;
822 wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen);
823 wi_write_val(sc, WI_RID_OWN_CHNL,
824 ieee80211_chan2ieee(ic, bss->ni_chan));
825
826 /* Configure WEP. */
827 if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
828 wi_write_wep(sc, vap);
829 else
830 sc->sc_encryption = 0;
831
832 if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
833 (vap->iv_flags & IEEE80211_F_WPA)) {
834 wi_write_val(sc, WI_RID_WPA_HANDLING, 1);
835 if (vap->iv_appie_wpa != NULL)
836 wi_write_appie(sc, WI_RID_WPA_DATA,
837 vap->iv_appie_wpa);
838 }
839
840 wi_enable(sc); /* enable port */
841
842 /* Lucent firmware does not support the JOIN RID. */
843 if (sc->sc_firmware_type == WI_INTERSIL) {
844 struct wi_joinreq join;
845
846 memset(&join, 0, sizeof(join));
847 IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid);
848 join.wi_chan = htole16(
849 ieee80211_chan2ieee(ic, bss->ni_chan));
850 wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
984263bc 851 }
1f8e62c9 852
5d8e19b2
RP
853 /*
854 * NB: don't go through 802.11 layer, it'll send auth frame;
855 * instead we drive the state machine from the link status
856 * notification we get on association.
857 */
858 vap->iv_state = nstate;
859 return (0);
860 }
861 return WI_VAP(vap)->wv_newstate(vap, nstate, arg);
862}
863
864static int
865wi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
866{
867 struct ieee80211com *ic = vap->iv_ic;
868 struct ifnet *ifp = ic->ic_ifp;
869 struct ieee80211_node *bss;
870 struct wi_softc *sc = ifp->if_softc;
871 int error;
872
873 DPRINTF(("%s: %s -> %s\n", __func__,
874 ieee80211_state_name[vap->iv_state],
875 ieee80211_state_name[nstate]));
876
877 error = WI_VAP(vap)->wv_newstate(vap, nstate, arg);
878 if (error == 0 && nstate == IEEE80211_S_RUN) {
5d8e19b2
RP
879 wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr);
880
881 bss = vap->iv_bss;
882 wi_write_ssid(sc, WI_RID_OWN_SSID,
883 bss->ni_essid, bss->ni_esslen);
884 wi_write_val(sc, WI_RID_OWN_CHNL,
885 ieee80211_chan2ieee(ic, bss->ni_chan));
886 wi_write_val(sc, WI_RID_BASIC_RATE, 0x3);
887 wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf);
888 wi_write_txrate(sc, vap);
889
890 wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval);
891 wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period);
892
893 wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
894 if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
895 wi_write_val(sc, WI_RID_FRAG_THRESH,
896 vap->iv_fragthreshold);
897
898 if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) &&
899 (vap->iv_flags & IEEE80211_F_HIDESSID)) {
900 /*
901 * bit 0 means hide SSID in beacons,
902 * bit 1 means don't respond to bcast probe req
903 */
904 wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3);
905 }
1f8e62c9 906
5d8e19b2
RP
907 if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
908 (vap->iv_flags & IEEE80211_F_WPA) &&
909 vap->iv_appie_wpa != NULL)
910 wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa);
911
912 wi_write_val(sc, WI_RID_PROMISC, 0);
913
914 /* Configure WEP. */
915 if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
916 wi_write_wep(sc, vap);
917 else
918 sc->sc_encryption = 0;
919
920 wi_enable(sc); /* enable port */
5d8e19b2
RP
921 }
922 return error;
923}
924
925static void
926wi_start_locked(struct ifnet *ifp)
927{
928 struct wi_softc *sc = ifp->if_softc;
929 struct ieee80211_node *ni;
930 struct ieee80211_frame *wh;
931 struct mbuf *m0;
932 struct ieee80211_key *k;
933 struct wi_frame frmhdr;
934 const struct llc *llc;
935 int cur;
936
5d8e19b2
RP
937 if (sc->wi_gone)
938 return;
939
940 memset(&frmhdr, 0, sizeof(frmhdr));
941 cur = sc->sc_txnext;
942 for (;;) {
943 IF_DEQUEUE(&ifp->if_snd, m0);
944 if (m0 == NULL)
945 break;
946 if (sc->sc_txd[cur].d_len != 0) {
947 IF_PREPEND(&ifp->if_snd, m0);
948 ifp->if_flags |= IFF_OACTIVE;
949 break;
950 }
951 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
952
953 /* reconstruct 802.3 header */
954 wh = mtod(m0, struct ieee80211_frame *);
955 switch (wh->i_fc[1]) {
956 case IEEE80211_FC1_DIR_TODS:
957 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
958 wh->i_addr2);
959 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
960 wh->i_addr3);
961 break;
962 case IEEE80211_FC1_DIR_NODS:
963 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
964 wh->i_addr2);
965 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
966 wh->i_addr1);
967 break;
968 case IEEE80211_FC1_DIR_FROMDS:
969 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
970 wh->i_addr3);
971 IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
972 wh->i_addr1);
973 break;
974 }
975 llc = (const struct llc *)(
976 mtod(m0, const uint8_t *) + ieee80211_hdrsize(wh));
977 frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type;
acffed4b 978 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
5d8e19b2
RP
979 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
980 k = ieee80211_crypto_encap(ni, m0);
981 if (k == NULL) {
69fc3d2e 982 ieee80211_free_node(ni);
841ab66c 983 m_freem(m0);
acffed4b 984 continue;
984263bc 985 }
acffed4b 986 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
984263bc 987 }
1f8e62c9 988
5d8e19b2
RP
989 if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
990 sc->sc_tx_th.wt_rate = ni->ni_txrate;
991 ieee80211_radiotap_tx(ni->ni_vap, m0);
acffed4b 992 }
1f8e62c9 993
acffed4b
JS
994 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
995 (caddr_t)&frmhdr.wi_whdr);
996 m_adj(m0, sizeof(struct ieee80211_frame));
997 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
69fc3d2e 998 ieee80211_free_node(ni);
5d8e19b2 999 if (wi_start_tx(ifp, &frmhdr, m0))
acffed4b 1000 continue;
5d8e19b2
RP
1001
1002 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1003 ifp->if_opackets++;
1004 }
1005}
1006
1007static void
1008wi_start(struct ifnet *ifp)
1009{
5d8e19b2 1010 wi_start_locked(ifp);
5d8e19b2
RP
1011}
1012
1013static int
1014wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0)
1015{
1016 struct wi_softc *sc = ifp->if_softc;
1017 int cur = sc->sc_txnext;
1018 int fid, off, error;
1019
1020 fid = sc->sc_txd[cur].d_fid;
1021 off = sizeof(*frmhdr);
1022 error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0
1023 || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
1024 m_freem(m0);
1025 if (error) {
1026 ifp->if_oerrors++;
1027 return -1;
1028 }
1029 sc->sc_txd[cur].d_len = off;
1030 if (sc->sc_txcur == cur) {
1031 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1032 if_printf(ifp, "xmit failed\n");
1033 sc->sc_txd[cur].d_len = 0;
1034 return -1;
acffed4b 1035 }
5d8e19b2
RP
1036 sc->sc_tx_timer = 5;
1037 }
1038 return 0;
1039}
1040
1041static int
1042wi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0,
1043 const struct ieee80211_bpf_params *params)
1044{
1045 struct ieee80211com *ic = ni->ni_ic;
1046 struct ifnet *ifp = ic->ic_ifp;
1047 struct ieee80211vap *vap = ni->ni_vap;
1048 struct wi_softc *sc = ifp->if_softc;
1049 struct ieee80211_key *k;
1050 struct ieee80211_frame *wh;
1051 struct wi_frame frmhdr;
1052 int cur;
1053 int rc = 0;
1054
5d8e19b2
RP
1055 if (sc->wi_gone) {
1056 rc = ENETDOWN;
1057 goto out;
1058 }
1059 memset(&frmhdr, 0, sizeof(frmhdr));
1060 cur = sc->sc_txnext;
1061 if (sc->sc_txd[cur].d_len != 0) {
1062 ifp->if_flags |= IFF_OACTIVE;
1063 rc = ENOBUFS;
1064 goto out;
1065 }
1066 m0->m_pkthdr.rcvif = NULL;
1067
1068 m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
1069 (caddr_t)&frmhdr.wi_ehdr);
1070 frmhdr.wi_ehdr.ether_type = 0;
1071 wh = mtod(m0, struct ieee80211_frame *);
1072
1073 frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1074 if (params && (params->ibp_flags & IEEE80211_BPF_NOACK))
1075 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
1076 if ((wh->i_fc[1] & IEEE80211_FC1_WEP) &&
1077 (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) {
1078 k = ieee80211_crypto_encap(ni, m0);
1079 if (k == NULL) {
1080 rc = ENOMEM;
1081 goto out;
acffed4b 1082 }
5d8e19b2
RP
1083 frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1084 }
1085 if (ieee80211_radiotap_active_vap(vap)) {
1086 sc->sc_tx_th.wt_rate = ni->ni_txrate;
1087 ieee80211_radiotap_tx(vap, m0);
1088 }
1089 m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1090 (caddr_t)&frmhdr.wi_whdr);
1091 m_adj(m0, sizeof(struct ieee80211_frame));
1092 frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1093 if (wi_start_tx(ifp, &frmhdr, m0) < 0) {
1094 m0 = NULL;
1095 rc = EIO;
1096 goto out;
984263bc 1097 }
5d8e19b2
RP
1098 m0 = NULL;
1099
1100 sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1101out:
5d8e19b2
RP
1102
1103 if (m0 != NULL)
1104 m_freem(m0);
1105 ieee80211_free_node(ni);
1106 return rc;
984263bc
MD
1107}
1108
acffed4b
JS
1109static int
1110wi_reset(struct wi_softc *sc)
984263bc
MD
1111{
1112#define WI_INIT_TRIES 3
5d8e19b2 1113 int i, error = 0;
984263bc 1114
5d8e19b2
RP
1115 for (i = 0; i < WI_INIT_TRIES; i++) {
1116 error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0);
1117 if (error == 0)
984263bc
MD
1118 break;
1119 DELAY(WI_DELAY * 1000);
1120 }
acffed4b 1121 sc->sc_reset = 1;
5d8e19b2
RP
1122 if (i == WI_INIT_TRIES) {
1123 if_printf(sc->sc_ifp, "reset failed\n");
1124 return error;
984263bc
MD
1125 }
1126
1127 CSR_WRITE_2(sc, WI_INT_EN, 0);
1128 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1129
1130 /* Calibrate timer. */
acffed4b 1131 wi_write_val(sc, WI_RID_TICK_TIME, 8);
984263bc 1132
5d8e19b2 1133 return 0;
acffed4b 1134#undef WI_INIT_TRIES
984263bc
MD
1135}
1136
acffed4b 1137static void
e89c26f4 1138wi_watchdog_callout(void *arg)
984263bc 1139{
5d8e19b2
RP
1140 struct wi_softc *sc = arg;
1141 struct ifnet *ifp = sc->sc_ifp;
1142
acffed4b
JS
1143 if (!sc->sc_enabled)
1144 return;
1145
5d8e19b2
RP
1146 if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
1147 if_printf(ifp, "device timeout\n");
1148 ifp->if_oerrors++;
1149 wi_init_locked(ifp->if_softc);
1150 return;
acffed4b 1151 }
e89c26f4 1152 callout_reset(&sc->sc_watchdog, hz, wi_watchdog_callout, sc);
acffed4b 1153}
984263bc 1154
acffed4b 1155static int
5d8e19b2 1156wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *ucred)
acffed4b
JS
1157{
1158 struct wi_softc *sc = ifp->if_softc;
5d8e19b2
RP
1159 struct ieee80211com *ic = ifp->if_l2com;
1160 struct ifreq *ifr = (struct ifreq *) data;
1161 int error = 0, startall = 0;
acffed4b
JS
1162
1163 switch (cmd) {
1164 case SIOCSIFFLAGS:
984263bc 1165 /*
acffed4b
JS
1166 * Can't do promisc and hostap at the same time. If all that's
1167 * changing is the promisc flag, try to short-circuit a call to
1168 * wi_init() by just setting PROMISC in the hardware.
984263bc 1169 */
acffed4b
JS
1170 if (ifp->if_flags & IFF_UP) {
1171 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1172 ifp->if_flags & IFF_RUNNING) {
5d8e19b2
RP
1173 if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) {
1174 wi_write_val(sc, WI_RID_PROMISC,
1175 (ifp->if_flags & IFF_PROMISC) != 0);
acffed4b 1176 } else {
5d8e19b2
RP
1177 wi_init_locked(sc);
1178 startall = 1;
acffed4b
JS
1179 }
1180 } else {
5d8e19b2
RP
1181 wi_init_locked(sc);
1182 startall = 1;
984263bc 1183 }
acffed4b 1184 } else {
5d8e19b2
RP
1185 if (ifp->if_flags & IFF_RUNNING)
1186 wi_stop_locked(sc, 1);
acffed4b
JS
1187 sc->wi_gone = 0;
1188 }
1189 sc->sc_if_flags = ifp->if_flags;
5d8e19b2
RP
1190 if (startall)
1191 ieee80211_start_all(ic);
acffed4b 1192 break;
5d8e19b2
RP
1193 case SIOCGIFMEDIA:
1194 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
acffed4b 1195 break;
5d8e19b2
RP
1196 case SIOCGIFADDR:
1197 error = ether_ioctl(ifp, cmd, data);
f35cfef5 1198 break;
acffed4b 1199 default:
5d8e19b2 1200 error = EINVAL;
acffed4b
JS
1201 break;
1202 }
acffed4b
JS
1203 return error;
1204}
1205
1206static void
1207wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1208{
5d8e19b2
RP
1209 struct ieee80211vap *vap = ifp->if_softc;
1210 struct ieee80211com *ic = vap->iv_ic;
1211 struct wi_softc *sc = ic->ic_ifp->if_softc;
acffed4b
JS
1212 u_int16_t val;
1213 int rate, len;
1214
acffed4b 1215 len = sizeof(val);
5d8e19b2
RP
1216 if (sc->sc_enabled &&
1217 wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
841ab66c 1218 len == sizeof(val)) {
acffed4b 1219 /* convert to 802.11 rate */
841ab66c 1220 val = le16toh(val);
acffed4b
JS
1221 rate = val * 2;
1222 if (sc->sc_firmware_type == WI_LUCENT) {
841ab66c 1223 if (rate == 10)
acffed4b 1224 rate = 11; /* 5.5Mbps */
acffed4b
JS
1225 } else {
1226 if (rate == 4*2)
1227 rate = 11; /* 5.5Mbps */
1228 else if (rate == 8*2)
1229 rate = 22; /* 11Mbps */
984263bc 1230 }
5d8e19b2 1231 vap->iv_bss->ni_txrate = rate;
acffed4b 1232 }
5d8e19b2 1233 ieee80211_media_status(ifp, imr);
acffed4b 1234}
984263bc 1235
acffed4b
JS
1236static void
1237wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1238{
5d8e19b2
RP
1239 struct ifnet *ifp = sc->sc_ifp;
1240 struct ieee80211com *ic = ifp->if_l2com;
1241 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1242 struct ieee80211_node *ni = vap->iv_bss;
984263bc 1243
acffed4b
JS
1244 if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1245 return;
984263bc 1246
5d8e19b2
RP
1247 DPRINTF(("wi_sync_bssid: bssid %6D -> ", ni->ni_bssid, ":"));
1248 DPRINTF(("%6D ?\n", new_bssid, ":"));
984263bc 1249
acffed4b
JS
1250 /* In promiscuous mode, the BSSID field is not a reliable
1251 * indicator of the firmware's BSSID. Damp spurious
1252 * change-of-BSSID indications.
1253 */
1254 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
841ab66c
SZ
1255 !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1256 WI_MAX_FALSE_SYNS))
acffed4b 1257 return;
984263bc 1258
841ab66c 1259 sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
5d8e19b2 1260#if 0
841ab66c
SZ
1261 /*
1262 * XXX hack; we should create a new node with the new bssid
1263 * and replace the existing ic_bss with it but since we don't
1264 * process management frames to collect state we cheat by
1265 * reusing the existing node as we know wi_newstate will be
1266 * called and it will overwrite the node state.
1267 */
1268 ieee80211_sta_join(ic, ieee80211_ref_node(ni));
5d8e19b2 1269#endif
984263bc
MD
1270}
1271
5d8e19b2 1272static __noinline void
acffed4b 1273wi_rx_intr(struct wi_softc *sc)
984263bc 1274{
5d8e19b2
RP
1275 struct ifnet *ifp = sc->sc_ifp;
1276 struct ieee80211com *ic = ifp->if_l2com;
acffed4b
JS
1277 struct wi_frame frmhdr;
1278 struct mbuf *m;
1279 struct ieee80211_frame *wh;
1280 struct ieee80211_node *ni;
5d8e19b2 1281 int fid, len, off;
acffed4b
JS
1282 u_int8_t dir;
1283 u_int16_t status;
5d8e19b2 1284 int8_t rssi, nf;
acffed4b
JS
1285
1286 fid = CSR_READ_2(sc, WI_RX_FID);
1287
acffed4b
JS
1288 /* First read in the frame header */
1289 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1290 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1291 ifp->if_ierrors++;
5d8e19b2 1292 DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
acffed4b
JS
1293 return;
1294 }
984263bc 1295
acffed4b
JS
1296 /*
1297 * Drop undecryptable or packets with receive errors here
1298 */
1299 status = le16toh(frmhdr.wi_status);
1300 if (status & WI_STAT_ERRSTAT) {
1301 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1302 ifp->if_ierrors++;
5d8e19b2 1303 DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
acffed4b
JS
1304 return;
1305 }
984263bc 1306
acffed4b
JS
1307 len = le16toh(frmhdr.wi_dat_len);
1308 off = ALIGN(sizeof(struct ieee80211_frame));
984263bc 1309
acffed4b
JS
1310 /*
1311 * Sometimes the PRISM2.x returns bogusly large frames. Except
1312 * in monitor mode, just throw them away.
1313 */
1314 if (off + len > MCLBYTES) {
1315 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1316 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1317 ifp->if_ierrors++;
5d8e19b2 1318 DPRINTF(("wi_rx_intr: oversized packet\n"));
acffed4b
JS
1319 return;
1320 } else
1321 len = 0;
1322 }
984263bc 1323
5d8e19b2
RP
1324 if (off + len > MHLEN)
1325 m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1326 else
1327 m = m_gethdr(MB_DONTWAIT, MT_DATA);
acffed4b
JS
1328 if (m == NULL) {
1329 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1330 ifp->if_ierrors++;
5d8e19b2 1331 DPRINTF(("wi_rx_intr: MGET failed\n"));
acffed4b
JS
1332 return;
1333 }
acffed4b
JS
1334 m->m_data += off - sizeof(struct ieee80211_frame);
1335 memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1336 wi_read_bap(sc, fid, sizeof(frmhdr),
1337 m->m_data + sizeof(struct ieee80211_frame), len);
1338 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1339 m->m_pkthdr.rcvif = ifp;
984263bc 1340
acffed4b 1341 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
984263bc 1342
5d8e19b2
RP
1343 rssi = frmhdr.wi_rx_signal;
1344 nf = frmhdr.wi_rx_silence;
1345 if (ieee80211_radiotap_active(ic)) {
1346 struct wi_rx_radiotap_header *tap = &sc->sc_rx_th;
1347 uint32_t rstamp;
1348
1349 rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1350 le16toh(frmhdr.wi_rx_tstamp1);
1351 tap->wr_tsf = htole64((uint64_t)rstamp);
acffed4b 1352 /* XXX replace divide by table */
5d8e19b2
RP
1353 tap->wr_rate = frmhdr.wi_rx_rate / 5;
1354 tap->wr_flags = 0;
acffed4b 1355 if (frmhdr.wi_status & WI_STAT_PCF)
5d8e19b2
RP
1356 tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1357 if (m->m_flags & M_WEP)
1358 tap->wr_flags |= IEEE80211_RADIOTAP_F_WEP;
1359 tap->wr_antsignal = rssi;
1360 tap->wr_antnoise = nf;
984263bc 1361 }
1f8e62c9 1362
acffed4b 1363 /* synchronize driver's BSSID with firmware's BSSID */
5d8e19b2 1364 wh = mtod(m, struct ieee80211_frame *);
acffed4b
JS
1365 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1366 if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1367 wi_sync_bssid(sc, wh->i_addr3);
1368
5d8e19b2
RP
1369 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1370 if (ni != NULL) {
1371 (void) ieee80211_input(ni, m, rssi, nf);
1372 ieee80211_free_node(ni);
1373 } else
1374 (void) ieee80211_input_all(ic, m, rssi, nf);
1375
984263bc
MD
1376}
1377
5d8e19b2 1378static __noinline void
acffed4b 1379wi_tx_ex_intr(struct wi_softc *sc)
984263bc 1380{
5d8e19b2 1381 struct ifnet *ifp = sc->sc_ifp;
acffed4b
JS
1382 struct wi_frame frmhdr;
1383 int fid;
984263bc 1384
acffed4b
JS
1385 fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1386 /* Read in the frame header */
1387 if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1388 u_int16_t status = le16toh(frmhdr.wi_status);
acffed4b
JS
1389 /*
1390 * Spontaneous station disconnects appear as xmit
1391 * errors. Don't announce them and/or count them
1392 * as an output error.
1393 */
1394 if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1395 if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1396 if_printf(ifp, "tx failed");
1397 if (status & WI_TXSTAT_RET_ERR)
e3869ec7 1398 kprintf(", retry limit exceeded");
acffed4b 1399 if (status & WI_TXSTAT_AGED_ERR)
e3869ec7 1400 kprintf(", max transmit lifetime exceeded");
acffed4b 1401 if (status & WI_TXSTAT_DISCONNECT)
e3869ec7 1402 kprintf(", port disconnected");
acffed4b 1403 if (status & WI_TXSTAT_FORM_ERR)
e3869ec7 1404 kprintf(", invalid format (data len %u src %6D)",
acffed4b
JS
1405 le16toh(frmhdr.wi_dat_len),
1406 frmhdr.wi_ehdr.ether_shost, ":");
1407 if (status & ~0xf)
e3869ec7
SW
1408 kprintf(", status=0x%x", status);
1409 kprintf("\n");
acffed4b
JS
1410 }
1411 ifp->if_oerrors++;
1412 } else {
5d8e19b2 1413 DPRINTF(("port disconnected\n"));
acffed4b
JS
1414 ifp->if_collisions++; /* XXX */
1415 }
1416 } else
5d8e19b2 1417 DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
acffed4b
JS
1418 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1419}
984263bc 1420
5d8e19b2 1421static __noinline void
acffed4b
JS
1422wi_tx_intr(struct wi_softc *sc)
1423{
5d8e19b2 1424 struct ifnet *ifp = sc->sc_ifp;
acffed4b
JS
1425 int fid, cur;
1426
1427 if (sc->wi_gone)
1428 return;
984263bc 1429
acffed4b 1430 fid = CSR_READ_2(sc, WI_ALLOC_FID);
984263bc 1431 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
984263bc 1432
acffed4b
JS
1433 cur = sc->sc_txcur;
1434 if (sc->sc_txd[cur].d_fid != fid) {
1435 if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1436 fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1437 return;
1438 }
1439 sc->sc_tx_timer = 0;
1440 sc->sc_txd[cur].d_len = 0;
1441 sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1442 if (sc->sc_txd[cur].d_len == 0)
1443 ifp->if_flags &= ~IFF_OACTIVE;
1444 else {
1445 if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1446 0, 0)) {
1447 if_printf(ifp, "xmit failed\n");
1448 sc->sc_txd[cur].d_len = 0;
1449 } else {
1450 sc->sc_tx_timer = 5;
acffed4b 1451 }
984263bc 1452 }
984263bc
MD
1453}
1454
5d8e19b2 1455static __noinline void
acffed4b 1456wi_info_intr(struct wi_softc *sc)
984263bc 1457{
5d8e19b2
RP
1458 struct ifnet *ifp = sc->sc_ifp;
1459 struct ieee80211com *ic = ifp->if_l2com;
1460 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
acffed4b
JS
1461 int i, fid, len, off;
1462 u_int16_t ltbuf[2];
1463 u_int16_t stat;
1464 u_int32_t *ptr;
1465
1466 fid = CSR_READ_2(sc, WI_INFO_FID);
1467 wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1468
1469 switch (le16toh(ltbuf[1])) {
acffed4b
JS
1470 case WI_INFO_LINK_STAT:
1471 wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
5d8e19b2 1472 DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
acffed4b
JS
1473 switch (le16toh(stat)) {
1474 case WI_INFO_LINK_STAT_CONNECTED:
5d8e19b2
RP
1475 if (vap->iv_state == IEEE80211_S_RUN &&
1476 vap->iv_opmode != IEEE80211_M_IBSS)
acffed4b 1477 break;
5d8e19b2 1478 /* fall thru... */
acffed4b 1479 case WI_INFO_LINK_STAT_AP_CHG:
5d8e19b2
RP
1480 vap->iv_bss->ni_associd = 1 | 0xc000; /* NB: anything will do */
1481 ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
acffed4b
JS
1482 break;
1483 case WI_INFO_LINK_STAT_AP_INR:
5d8e19b2
RP
1484 break;
1485 case WI_INFO_LINK_STAT_DISCONNECTED:
1486 /* we dropped off the net; e.g. due to deauth/disassoc */
5d8e19b2
RP
1487 vap->iv_bss->ni_associd = 0;
1488 vap->iv_stats.is_rx_deauth++;
1489 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
acffed4b
JS
1490 break;
1491 case WI_INFO_LINK_STAT_AP_OOR:
5d8e19b2
RP
1492 /* XXX does this need to be per-vap? */
1493 ieee80211_beacon_miss(ic);
acffed4b 1494 break;
acffed4b 1495 case WI_INFO_LINK_STAT_ASSOC_FAILED:
5d8e19b2
RP
1496 if (vap->iv_opmode == IEEE80211_M_STA)
1497 ieee80211_new_state(vap, IEEE80211_S_SCAN,
1498 IEEE80211_SCAN_FAIL_TIMEOUT);
acffed4b
JS
1499 break;
1500 }
1501 break;
acffed4b
JS
1502 case WI_INFO_COUNTERS:
1503 /* some card versions have a larger stats structure */
1504 len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1505 ptr = (u_int32_t *)&sc->sc_stats;
1506 off = sizeof(ltbuf);
1507 for (i = 0; i < len; i++, off += 2, ptr++) {
1508 wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1509#ifdef WI_HERMES_STATS_WAR
1510 if (stat & 0xf000)
1511 stat = ~stat;
1512#endif
1513 *ptr += stat;
1514 }
1515 ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
1516 sc->sc_stats.wi_tx_multi_retries +
1517 sc->sc_stats.wi_tx_retry_limit;
1518 break;
acffed4b 1519 default:
5d8e19b2 1520 DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
acffed4b
JS
1521 le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1522 break;
1523 }
1524 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1525}
984263bc 1526
acffed4b
JS
1527static int
1528wi_write_multi(struct wi_softc *sc)
1529{
5d8e19b2 1530 struct ifnet *ifp = sc->sc_ifp;
acffed4b
JS
1531 int n;
1532 struct ifmultiaddr *ifma;
1533 struct wi_mcast mlist;
984263bc
MD
1534
1535 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
acffed4b
JS
1536allmulti:
1537 memset(&mlist, 0, sizeof(mlist));
1538 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1539 sizeof(mlist));
984263bc
MD
1540 }
1541
acffed4b 1542 n = 0;
5d8e19b2
RP
1543#ifdef __FreeBSD__
1544 if_maddr_rlock(ifp);
1545#endif
441d34b2 1546 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
984263bc
MD
1547 if (ifma->ifma_addr->sa_family != AF_LINK)
1548 continue;
acffed4b
JS
1549 if (n >= 16)
1550 goto allmulti;
1551 IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1552 (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1553 n++;
1554 }
5d8e19b2
RP
1555#ifdef __FreeBSD__
1556 if_maddr_runlock(ifp);
1557#endif
acffed4b
JS
1558 return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1559 IEEE80211_ADDR_LEN * n);
1560}
1561
5d8e19b2
RP
1562static void
1563wi_update_mcast(struct ifnet *ifp)
1564{
1565 wi_write_multi(ifp->if_softc);
1566}
1567
1568static void
1569wi_update_promisc(struct ifnet *ifp)
1570{
1571 struct wi_softc *sc = ifp->if_softc;
1572 struct ieee80211com *ic = ifp->if_l2com;
1573
5d8e19b2
RP
1574 /* XXX handle WEP special case handling? */
1575 wi_write_val(sc, WI_RID_PROMISC,
1576 (ic->ic_opmode == IEEE80211_M_MONITOR ||
1577 (ifp->if_flags & IFF_PROMISC)));
5d8e19b2
RP
1578}
1579
acffed4b
JS
1580static void
1581wi_read_nicid(struct wi_softc *sc)
1582{
1583 struct wi_card_ident *id;
1584 char *p;
1585 int len;
1586 u_int16_t ver[4];
1587
1588 /* getting chip identity */
1589 memset(ver, 0, sizeof(ver));
1590 len = sizeof(ver);
1591 wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
acffed4b
JS
1592
1593 sc->sc_firmware_type = WI_NOTYPE;
5d8e19b2 1594 sc->sc_nic_id = le16toh(ver[0]);
acffed4b 1595 for (id = wi_card_ident; id->card_name != NULL; id++) {
5d8e19b2
RP
1596 if (sc->sc_nic_id == id->card_id) {
1597 sc->sc_nic_name = id->card_name;
acffed4b 1598 sc->sc_firmware_type = id->firm_type;
984263bc
MD
1599 break;
1600 }
5d8e19b2
RP
1601 }
1602 if (sc->sc_firmware_type == WI_NOTYPE) {
1603 if (sc->sc_nic_id & 0x8000) {
1604 sc->sc_firmware_type = WI_INTERSIL;
1605 sc->sc_nic_name = "Unknown Prism chip";
1606 } else {
1607 sc->sc_firmware_type = WI_LUCENT;
1608 sc->sc_nic_name = "Unknown Lucent chip";
acffed4b 1609 }
5d8e19b2
RP
1610 }
1611 if (bootverbose)
1612 device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name);
acffed4b 1613
5d8e19b2
RP
1614 /* get primary firmware version (Only Prism chips) */
1615 if (sc->sc_firmware_type != WI_LUCENT) {
1616 memset(ver, 0, sizeof(ver));
1617 len = sizeof(ver);
1618 wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1619 sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1620 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1621 }
1622
1623 /* get station firmware version */
1624 memset(ver, 0, sizeof(ver));
1625 len = sizeof(ver);
1626 wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1627 sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1628 le16toh(ver[3]) * 100 + le16toh(ver[1]);
1629 if (sc->sc_firmware_type == WI_INTERSIL &&
1630 (sc->sc_sta_firmware_ver == 10102 ||
1631 sc->sc_sta_firmware_ver == 20102)) {
1632 char ident[12];
1633 memset(ident, 0, sizeof(ident));
1634 len = sizeof(ident);
1635 /* value should be the format like "V2.00-11" */
1636 if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1637 *(p = (char *)ident) >= 'A' &&
1638 p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1639 sc->sc_firmware_type = WI_SYMBOL;
1640 sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1641 (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1642 (p[6] - '0') * 10 + (p[7] - '0');
acffed4b 1643 }
acffed4b 1644 }
5d8e19b2
RP
1645 if (bootverbose) {
1646 device_printf(sc->sc_dev, "%s Firmware: ",
1647 wi_firmware_names[sc->sc_firmware_type]);
1648 if (sc->sc_firmware_type != WI_LUCENT) /* XXX */
1649 kprintf("Primary (%u.%u.%u), ",
1650 sc->sc_pri_firmware_ver / 10000,
1651 (sc->sc_pri_firmware_ver % 10000) / 100,
1652 sc->sc_pri_firmware_ver % 100);
1653 kprintf("Station (%u.%u.%u)\n",
1654 sc->sc_sta_firmware_ver / 10000,
1655 (sc->sc_sta_firmware_ver % 10000) / 100,
1656 sc->sc_sta_firmware_ver % 100);
1657 }
acffed4b
JS
1658}
1659
1660static int
5d8e19b2 1661wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
acffed4b 1662{
5d8e19b2 1663 struct wi_ssid ssid;
acffed4b 1664
5d8e19b2
RP
1665 if (buflen > IEEE80211_NWID_LEN)
1666 return ENOBUFS;
1667 memset(&ssid, 0, sizeof(ssid));
1668 ssid.wi_len = htole16(buflen);
1669 memcpy(ssid.wi_ssid, buf, buflen);
1670 return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
984263bc
MD
1671}
1672
841ab66c 1673static int
5d8e19b2 1674wi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap)
841ab66c 1675{
5d8e19b2
RP
1676 static const uint16_t lucent_rates[12] = {
1677 [ 0] = 3, /* auto */
1678 [ 1] = 1, /* 1Mb/s */
1679 [ 2] = 2, /* 2Mb/s */
1680 [ 5] = 4, /* 5.5Mb/s */
1681 [11] = 5 /* 11Mb/s */
1682 };
1683 static const uint16_t intersil_rates[12] = {
1684 [ 0] = 0xf, /* auto */
1685 [ 1] = 0, /* 1Mb/s */
1686 [ 2] = 1, /* 2Mb/s */
1687 [ 5] = 2, /* 5.5Mb/s */
1688 [11] = 3, /* 11Mb/s */
1689 };
1690 const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ?
1691 lucent_rates : intersil_rates;
1692 struct ieee80211com *ic = vap->iv_ic;
1693 const struct ieee80211_txparam *tp;
1694
1695 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1696 return wi_write_val(sc, WI_RID_TX_RATE,
1697 (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1698 rates[0] : rates[tp->ucastrate / 2]));
841ab66c
SZ
1699}
1700
acffed4b 1701static int
5d8e19b2 1702wi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap)
984263bc 1703{
acffed4b
JS
1704 int error = 0;
1705 int i, keylen;
1706 u_int16_t val;
1707 struct wi_key wkey[IEEE80211_WEP_NKID];
984263bc 1708
acffed4b
JS
1709 switch (sc->sc_firmware_type) {
1710 case WI_LUCENT:
5d8e19b2 1711 val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
acffed4b
JS
1712 error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1713 if (error)
1714 break;
5d8e19b2 1715 if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
841ab66c 1716 break;
5d8e19b2 1717 error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey);
acffed4b
JS
1718 if (error)
1719 break;
1720 memset(wkey, 0, sizeof(wkey));
1721 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
5d8e19b2 1722 keylen = vap->iv_nw_keys[i].wk_keylen;
acffed4b 1723 wkey[i].wi_keylen = htole16(keylen);
5d8e19b2 1724 memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key,
acffed4b
JS
1725 keylen);
1726 }
1727 error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1728 wkey, sizeof(wkey));
841ab66c 1729 sc->sc_encryption = 0;
acffed4b 1730 break;
984263bc 1731
acffed4b 1732 case WI_INTERSIL:
5d8e19b2
RP
1733 val = HOST_ENCRYPT | HOST_DECRYPT;
1734 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
984263bc
MD
1735 /*
1736 * ONLY HWB3163 EVAL-CARD Firmware version
1737 * less than 0.8 variant2
1738 *
acffed4b
JS
1739 * If promiscuous mode disable, Prism2 chip
1740 * does not work with WEP .
984263bc
MD
1741 * It is under investigation for details.
1742 * (ichiro@netbsd.org)
984263bc 1743 */
5d8e19b2 1744 if (sc->sc_sta_firmware_ver < 802 ) {
984263bc 1745 /* firm ver < 0.8 variant 2 */
acffed4b 1746 wi_write_val(sc, WI_RID_PROMISC, 1);
984263bc 1747 }
acffed4b 1748 wi_write_val(sc, WI_RID_CNFAUTHMODE,
5d8e19b2
RP
1749 vap->iv_bss->ni_authmode);
1750 val |= PRIVACY_INVOKED;
acffed4b 1751 } else {
5d8e19b2 1752 wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN);
acffed4b
JS
1753 }
1754 error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1755 if (error)
1756 break;
841ab66c
SZ
1757 sc->sc_encryption = val;
1758 if ((val & PRIVACY_INVOKED) == 0)
1759 break;
5d8e19b2 1760 error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey);
acffed4b 1761 break;
984263bc 1762 }
acffed4b
JS
1763 return error;
1764}
984263bc 1765
acffed4b
JS
1766static int
1767wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
1768{
5d8e19b2
RP
1769 int i, s = 0;
1770
acffed4b
JS
1771 if (sc->wi_gone)
1772 return (ENODEV);
984263bc 1773
acffed4b
JS
1774 /* wait for the busy bit to clear */
1775 for (i = sc->wi_cmd_count; i > 0; i--) { /* 500ms */
1776 if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
1777 break;
1778 DELAY(1*1000); /* 1ms */
1779 }
1780 if (i == 0) {
5d8e19b2
RP
1781 device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n",
1782 __func__, cmd);
acffed4b 1783 sc->wi_gone = 1;
acffed4b
JS
1784 return(ETIMEDOUT);
1785 }
984263bc 1786
acffed4b
JS
1787 CSR_WRITE_2(sc, WI_PARAM0, val0);
1788 CSR_WRITE_2(sc, WI_PARAM1, val1);
1789 CSR_WRITE_2(sc, WI_PARAM2, val2);
1790 CSR_WRITE_2(sc, WI_COMMAND, cmd);
984263bc 1791
acffed4b
JS
1792 if (cmd == WI_CMD_INI) {
1793 /* XXX: should sleep here. */
1794 DELAY(100*1000); /* 100ms delay for init */
1795 }
1796 for (i = 0; i < WI_TIMEOUT; i++) {
1797 /*
1798 * Wait for 'command complete' bit to be
1799 * set in the event status register.
1800 */
1801 s = CSR_READ_2(sc, WI_EVENT_STAT);
1802 if (s & WI_EV_CMD) {
1803 /* Ack the event and read result code. */
1804 s = CSR_READ_2(sc, WI_STATUS);
1805 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1806 if (s & WI_STAT_CMD_RESULT) {
acffed4b
JS
1807 return(EIO);
1808 }
1809 break;
1810 }
1811 DELAY(WI_DELAY);
1812 }
984263bc 1813
acffed4b 1814 if (i == WI_TIMEOUT) {
5d8e19b2
RP
1815 device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; "
1816 "event status 0x%04x\n", __func__, cmd, s);
acffed4b
JS
1817 if (s == 0xffff)
1818 sc->wi_gone = 1;
1819 return(ETIMEDOUT);
1820 }
1821 return (0);
1822}
984263bc 1823
acffed4b
JS
1824static int
1825wi_seek_bap(struct wi_softc *sc, int id, int off)
1826{
1827 int i, status;
984263bc 1828
acffed4b
JS
1829 CSR_WRITE_2(sc, WI_SEL0, id);
1830 CSR_WRITE_2(sc, WI_OFF0, off);
984263bc 1831
acffed4b
JS
1832 for (i = 0; ; i++) {
1833 status = CSR_READ_2(sc, WI_OFF0);
1834 if ((status & WI_OFF_BUSY) == 0)
1835 break;
1836 if (i == WI_TIMEOUT) {
5d8e19b2
RP
1837 device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n",
1838 __func__, id, off);
acffed4b
JS
1839 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1840 if (status == 0xffff)
1841 sc->wi_gone = 1;
1842 return ETIMEDOUT;
1843 }
1844 DELAY(1);
1845 }
1846 if (status & WI_OFF_ERR) {
5d8e19b2
RP
1847 device_printf(sc->sc_dev, "%s: error, id %x off %x\n",
1848 __func__, id, off);
acffed4b
JS
1849 sc->sc_bap_off = WI_OFF_ERR; /* invalidate */
1850 return EIO;
1851 }
1852 sc->sc_bap_id = id;
1853 sc->sc_bap_off = off;
1854 return 0;
984263bc
MD
1855}
1856
acffed4b
JS
1857static int
1858wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
984263bc 1859{
acffed4b
JS
1860 u_int16_t *ptr;
1861 int i, error, cnt;
1862
1863 if (buflen == 0)
1864 return 0;
1865 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1866 if ((error = wi_seek_bap(sc, id, off)) != 0)
1867 return error;
984263bc 1868 }
acffed4b
JS
1869 cnt = (buflen + 1) / 2;
1870 ptr = (u_int16_t *)buf;
1871 for (i = 0; i < cnt; i++)
1872 *ptr++ = CSR_READ_2(sc, WI_DATA0);
1873 sc->sc_bap_off += cnt * 2;
1874 return 0;
984263bc
MD
1875}
1876
acffed4b
JS
1877static int
1878wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
984263bc 1879{
acffed4b
JS
1880 u_int16_t *ptr;
1881 int i, error, cnt;
984263bc 1882
acffed4b
JS
1883 if (buflen == 0)
1884 return 0;
984263bc 1885
acffed4b
JS
1886 if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1887 if ((error = wi_seek_bap(sc, id, off)) != 0)
1888 return error;
984263bc 1889 }
acffed4b
JS
1890 cnt = (buflen + 1) / 2;
1891 ptr = (u_int16_t *)buf;
1892 for (i = 0; i < cnt; i++)
1893 CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
1894 sc->sc_bap_off += cnt * 2;
984263bc 1895
acffed4b
JS
1896 return 0;
1897}
984263bc 1898
acffed4b
JS
1899static int
1900wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
1901{
1902 int error, len;
1903 struct mbuf *m;
984263bc 1904
acffed4b
JS
1905 for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
1906 if (m->m_len == 0)
1907 continue;
984263bc 1908
acffed4b 1909 len = min(m->m_len, totlen);
984263bc 1910
acffed4b
JS
1911 if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
1912 m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
1913 return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
1914 totlen);
1915 }
984263bc 1916
acffed4b
JS
1917 if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
1918 return error;
984263bc 1919
acffed4b
JS
1920 off += m->m_len;
1921 totlen -= len;
1922 }
1923 return 0;
984263bc
MD
1924}
1925
acffed4b
JS
1926static int
1927wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
984263bc 1928{
acffed4b 1929 int i;
984263bc 1930
acffed4b 1931 if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
5d8e19b2
RP
1932 device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n",
1933 __func__, len);
acffed4b
JS
1934 return ENOMEM;
1935 }
1936
1937 for (i = 0; i < WI_TIMEOUT; i++) {
1938 if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1939 break;
acffed4b
JS
1940 DELAY(1);
1941 }
841ab66c 1942 if (i == WI_TIMEOUT) {
5d8e19b2 1943 device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__);
841ab66c
SZ
1944 return ETIMEDOUT;
1945 }
acffed4b
JS
1946 *idp = CSR_READ_2(sc, WI_ALLOC_FID);
1947 CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1948 return 0;
1949}
984263bc 1950
acffed4b
JS
1951static int
1952wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
1953{
1954 int error, len;
1955 u_int16_t ltbuf[2];
984263bc 1956
acffed4b
JS
1957 /* Tell the NIC to enter record read mode. */
1958 error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
1959 if (error)
1960 return error;
984263bc 1961
acffed4b
JS
1962 error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
1963 if (error)
1964 return error;
1965
1966 if (le16toh(ltbuf[1]) != rid) {
5d8e19b2
RP
1967 device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
1968 rid, le16toh(ltbuf[1]));
acffed4b
JS
1969 return EIO;
1970 }
1971 len = (le16toh(ltbuf[0]) - 1) * 2; /* already got rid */
1972 if (*buflenp < len) {
5d8e19b2
RP
1973 device_printf(sc->sc_dev, "record buffer is too small, "
1974 "rid=%x, size=%d, len=%d\n",
1975 rid, *buflenp, len);
acffed4b
JS
1976 return ENOSPC;
1977 }
1978 *buflenp = len;
1979 return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
1980}
984263bc 1981
acffed4b
JS
1982static int
1983wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
1984{
1985 int error;
1986 u_int16_t ltbuf[2];
984263bc 1987
acffed4b
JS
1988 ltbuf[0] = htole16((buflen + 1) / 2 + 1); /* includes rid */
1989 ltbuf[1] = htole16(rid);
984263bc 1990
acffed4b 1991 error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
5d8e19b2
RP
1992 if (error) {
1993 device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n",
1994 __func__, rid);
acffed4b 1995 return error;
5d8e19b2 1996 }
acffed4b 1997 error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
5d8e19b2
RP
1998 if (error) {
1999 device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n",
2000 __func__, rid);
acffed4b 2001 return error;
984263bc
MD
2002 }
2003
5d8e19b2 2004 return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
acffed4b 2005}
984263bc 2006
acffed4b 2007static int
5d8e19b2 2008wi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie)
acffed4b 2009{
5d8e19b2
RP
2010 /* NB: 42 bytes is probably ok to have on the stack */
2011 char buf[sizeof(uint16_t) + 40];
2012
2013 if (ie->ie_len > 40)
2014 return EINVAL;
2015 /* NB: firmware requires 16-bit ie length before ie data */
2016 *(uint16_t *) buf = htole16(ie->ie_len);
2017 memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len);
2018 return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t));
984263bc
MD
2019}
2020
2021int
acffed4b 2022wi_alloc(device_t dev, int rid)
984263bc 2023{
acffed4b 2024 struct wi_softc *sc = device_get_softc(dev);
984263bc
MD
2025
2026 if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2027 sc->iobase_rid = rid;
2028 sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2029 &sc->iobase_rid, 0, ~0, (1 << 6),
2030 rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
5d8e19b2 2031 if (sc->iobase == NULL) {
984263bc 2032 device_printf(dev, "No I/O space?!\n");
5d8e19b2 2033 return ENXIO;
984263bc
MD
2034 }
2035
2036 sc->wi_io_addr = rman_get_start(sc->iobase);
2037 sc->wi_btag = rman_get_bustag(sc->iobase);
2038 sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2039 } else {
2040 sc->mem_rid = rid;
acffed4b
JS
2041 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2042 &sc->mem_rid, RF_ACTIVE);
5d8e19b2 2043 if (sc->mem == NULL) {
984263bc 2044 device_printf(dev, "No Mem space on prism2.5?\n");
5d8e19b2 2045 return ENXIO;
984263bc
MD
2046 }
2047
2048 sc->wi_btag = rman_get_bustag(sc->mem);
2049 sc->wi_bhandle = rman_get_bushandle(sc->mem);
2050 }
2051
984263bc 2052 sc->irq_rid = 0;
acffed4b
JS
2053 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2054 RF_ACTIVE |
984263bc 2055 ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
5d8e19b2 2056 if (sc->irq == NULL) {
984263bc
MD
2057 wi_free(dev);
2058 device_printf(dev, "No irq?!\n");
5d8e19b2 2059 return ENXIO;
984263bc
MD
2060 }
2061
5d8e19b2
RP
2062 sc->sc_dev = dev;
2063 sc->sc_unit = device_get_unit(dev);
2064 return 0;
984263bc
MD
2065}
2066
2067void
acffed4b 2068wi_free(device_t dev)
984263bc 2069{
acffed4b 2070 struct wi_softc *sc = device_get_softc(dev);
984263bc
MD
2071
2072 if (sc->iobase != NULL) {
2073 bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2074 sc->iobase = NULL;
2075 }
2076 if (sc->irq != NULL) {
2077 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2078 sc->irq = NULL;
2079 }
2080 if (sc->mem != NULL) {
2081 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2082 sc->mem = NULL;
2083 }
984263bc 2084}