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