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