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