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