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