xref: /netbsd-src/sys/dev/ic/awi.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: awi.c,v 1.98 2019/05/28 07:41:48 msaitoh Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999,2000,2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Bill Sommerfeld
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*
32  * Driver for AMD 802.11 firmware.
33  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
34  *
35  * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
36  */
37 
38 /*
39  * todo:
40  *	- flush tx queue on resynch.
41  *	- clear oactive on "down".
42  *	- rewrite copy-into-mbuf code
43  *	- mgmt state machine gets stuck retransmitting assoc requests.
44  *	- multicast filter.
45  *	- fix device reset so it's more likely to work
46  *	- show status goo through ifmedia.
47  *
48  * more todo:
49  *	- deal with more 802.11 frames.
50  *		- send reassoc request
51  *		- deal with reassoc response
52  *		- send/deal with disassociation
53  *	- deal with "full" access points (no room for me).
54  *	- power save mode
55  *
56  * later:
57  *	- SSID preferences
58  *	- need ioctls for poking at the MIBs
59  *	- implement ad-hoc mode (including bss creation).
60  *	- decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?)
61  *		(focus on inf. mode since that will be needed for ietf)
62  *	- deal with DH vs. FH versions of the card
63  *	- deal with faster cards (2mb/s)
64  *	- ?WEP goo (mmm, rc4) (it looks not particularly useful).
65  *	- ifmedia revision.
66  *	- common 802.11 mibish things.
67  *	- common 802.11 media layer.
68  */
69 
70 /*
71  * Driver for AMD 802.11 PCnetMobile firmware.
72  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
73  *
74  * The initial version of the driver was written by
75  * Bill Sommerfeld <sommerfeld@NetBSD.org>.
76  * Then the driver module completely rewritten to support cards with DS phy
77  * and to support adhoc mode by Atsushi Onoe <onoe@NetBSD.org>
78  */
79 
80 #include <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: awi.c,v 1.98 2019/05/28 07:41:48 msaitoh Exp $");
82 
83 #include "opt_inet.h"
84 
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/kernel.h>
88 #include <sys/mbuf.h>
89 #include <sys/malloc.h>
90 #include <sys/proc.h>
91 #include <sys/socket.h>
92 #include <sys/sockio.h>
93 #include <sys/errno.h>
94 #include <sys/endian.h>
95 #include <sys/device.h>
96 #include <sys/cpu.h>
97 #include <sys/bus.h>
98 
99 #include <net/if.h>
100 #include <net/if_dl.h>
101 #include <net/if_ether.h>
102 #include <net/if_media.h>
103 #include <net/if_llc.h>
104 #include <net/bpf.h>
105 
106 #include <net80211/ieee80211_netbsd.h>
107 #include <net80211/ieee80211_var.h>
108 
109 #include <dev/ic/am79c930reg.h>
110 #include <dev/ic/am79c930var.h>
111 #include <dev/ic/awireg.h>
112 #include <dev/ic/awivar.h>
113 
114 static void awi_softintr(void *);
115 static int  awi_init(struct ifnet *);
116 static void awi_stop(struct ifnet *, int);
117 static void awi_start(struct ifnet *);
118 static void awi_watchdog(struct ifnet *);
119 static int  awi_ioctl(struct ifnet *, u_long, void *);
120 static int  awi_media_change(struct ifnet *);
121 static void awi_media_status(struct ifnet *, struct ifmediareq *);
122 static int  awi_mode_init(struct awi_softc *);
123 static void awi_rx_int(struct awi_softc *);
124 static void awi_tx_int(struct awi_softc *);
125 static struct mbuf *awi_devget(struct awi_softc *, uint32_t, uint16_t);
126 static int  awi_hw_init(struct awi_softc *);
127 static int  awi_init_mibs(struct awi_softc *);
128 static int  awi_mib(struct awi_softc *, uint8_t, uint8_t, int);
129 static int  awi_cmd(struct awi_softc *, uint8_t, int);
130 static int  awi_cmd_wait(struct awi_softc *);
131 static void awi_cmd_done(struct awi_softc *);
132 static int  awi_next_txd(struct awi_softc *, int, uint32_t *, uint32_t *);
133 static int  awi_lock(struct awi_softc *);
134 static void awi_unlock(struct awi_softc *);
135 static int  awi_intr_lock(struct awi_softc *);
136 static void awi_intr_unlock(struct awi_softc *);
137 static int  awi_newstate(struct ieee80211com *, enum ieee80211_state, int);
138 static void awi_recv_mgmt(struct ieee80211com *, struct mbuf *,
139     struct ieee80211_node *, int, int, uint32_t);
140 static int  awi_send_mgmt(struct ieee80211com *, struct ieee80211_node *, int,
141     int);
142 static struct mbuf *awi_ether_encap(struct awi_softc *, struct mbuf *);
143 static struct mbuf *awi_ether_modcap(struct awi_softc *, struct mbuf *);
144 
145 /* Unaligned little endian access */
146 #define LE_READ_2(p)							\
147 	((((uint8_t *)(p))[0]      ) | (((uint8_t *)(p))[1] <<  8))
148 #define LE_READ_4(p)							\
149 	((((uint8_t *)(p))[0]      ) | (((uint8_t *)(p))[1] <<  8) |	\
150 	 (((uint8_t *)(p))[2] << 16) | (((uint8_t *)(p))[3] << 24))
151 #define LE_WRITE_2(p, v)						\
152 	((((uint8_t *)(p))[0] = (((uint32_t)(v)      ) & 0xff)),	\
153 	 (((uint8_t *)(p))[1] = (((uint32_t)(v) >>  8) & 0xff)))
154 #define LE_WRITE_4(p, v)						\
155 	((((uint8_t *)(p))[0] = (((uint32_t)(v)      ) & 0xff)),	\
156 	 (((uint8_t *)(p))[1] = (((uint32_t)(v) >>  8) & 0xff)),	\
157 	 (((uint8_t *)(p))[2] = (((uint32_t)(v) >> 16) & 0xff)),	\
158 	 (((uint8_t *)(p))[3] = (((uint32_t)(v) >> 24) & 0xff)))
159 
160 static const struct awi_chanset awi_chanset[] = {
161     /* PHY type        domain            min max def */
162     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_JP,  6, 17,  6 },
163     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_ES,  0, 26,  1 },
164     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_FR,  0, 32,  1 },
165     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_US,  0, 77,  1 },
166     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_CA,  0, 77,  1 },
167     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_EU,  0, 77,  1 },
168     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_JP, 14, 14, 14 },
169     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_ES, 10, 11, 10 },
170     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_FR, 10, 13, 10 },
171     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_US,  1, 11,  3 },
172     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_CA,  1, 11,  3 },
173     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_EU,  1, 13,  3 },
174     { 0, 0, 0, 0, 0 }
175 };
176 
177 #ifdef AWI_DEBUG
178 int awi_debug = 0;
179 
180 #define	DPRINTF(X)	if (awi_debug) printf X
181 #define	DPRINTF2(X)	if (awi_debug > 1) printf X
182 #else
183 #define	DPRINTF(X)
184 #define	DPRINTF2(X)
185 #endif
186 
187 int
188 awi_attach(struct awi_softc *sc)
189 {
190 	struct ieee80211com *ic = &sc->sc_ic;
191 	struct ifnet *ifp = &sc->sc_if;
192 	int s, i, error, nrate;
193 	int mword;
194 	enum ieee80211_phymode mode;
195 
196 	s = splnet();
197 	sc->sc_busy = 1;
198 	sc->sc_attached = 0;
199 	sc->sc_substate = AWI_ST_NONE;
200 	sc->sc_soft_ih = softint_establish(SOFTINT_NET, awi_softintr, sc);
201 	if (sc->sc_soft_ih == NULL) {
202 		config_deactivate(sc->sc_dev);
203 		splx(s);
204 		return ENOMEM;
205 	}
206 	if ((error = awi_hw_init(sc)) != 0) {
207 		config_deactivate(sc->sc_dev);
208 		splx(s);
209 		return error;
210 	}
211 	error = awi_init_mibs(sc);
212 	if (error != 0) {
213 		config_deactivate(sc->sc_dev);
214 		splx(s);
215 		return error;
216 	}
217 	ifp->if_softc = sc;
218 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
219 	ifp->if_ioctl = awi_ioctl;
220 	ifp->if_start = awi_start;
221 	ifp->if_watchdog = awi_watchdog;
222 	ifp->if_init = awi_init;
223 	ifp->if_stop = awi_stop;
224 	IFQ_SET_READY(&ifp->if_snd);
225 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
226 
227 	ic->ic_ifp = ifp;
228 	ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP;
229 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
230 		ic->ic_phytype = IEEE80211_T_FH;
231 		mode = IEEE80211_MODE_FH;
232 	} else {
233 		ic->ic_phytype = IEEE80211_T_DS;
234 		ic->ic_caps |= IEEE80211_C_AHDEMO;
235 		mode = IEEE80211_MODE_11B;
236 	}
237 	ic->ic_opmode = IEEE80211_M_STA;
238 	nrate = sc->sc_mib_phy.aSuprt_Data_Rates[1];
239 	memcpy(ic->ic_sup_rates[mode].rs_rates,
240 	    sc->sc_mib_phy.aSuprt_Data_Rates + 2, nrate);
241 	ic->ic_sup_rates[mode].rs_nrates = nrate;
242 	IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_mib_addr.aMAC_Address);
243 
244 	printf("%s: IEEE802.11 %s (firmware %s)\n", ifp->if_xname,
245 	    (ic->ic_phytype == IEEE80211_T_FH) ? "FH" : "DS", sc->sc_banner);
246 	printf("%s: 802.11 address: %s\n", ifp->if_xname,
247 	    ether_sprintf(ic->ic_myaddr));
248 
249 	if_attach(ifp);
250 	ieee80211_ifattach(ic);
251 
252 	sc->sc_newstate = ic->ic_newstate;
253 	ic->ic_newstate = awi_newstate;
254 
255 	sc->sc_recv_mgmt = ic->ic_recv_mgmt;
256 	ic->ic_recv_mgmt = awi_recv_mgmt;
257 
258 	sc->sc_send_mgmt = ic->ic_send_mgmt;
259 	ic->ic_send_mgmt = awi_send_mgmt;
260 
261 	ieee80211_media_init(ic, awi_media_change, awi_media_status);
262 
263 	/* Melco compatibility mode. */
264 #define	ADD(s, o)	ifmedia_add(&ic->ic_media, \
265 	IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
266 	ADD(IFM_AUTO, IFM_FLAG0);
267 
268 	for (i = 0; i < nrate; i++) {
269 		mword = ieee80211_rate2media(ic,
270 		    ic->ic_sup_rates[mode].rs_rates[i], mode);
271 		if (mword == 0)
272 			continue;
273 		ADD(mword, IFM_FLAG0);
274 	}
275 #undef	ADD
276 
277 	if ((sc->sc_sdhook = shutdownhook_establish(awi_shutdown, sc)) == NULL)
278 		printf("%s: WARNING: unable to establish shutdown hook\n",
279 		    ifp->if_xname);
280 	if ((sc->sc_powerhook =
281 	     powerhook_establish(ifp->if_xname, awi_power, sc)) == NULL)
282 		printf("%s: WARNING: unable to establish power hook\n",
283 		    ifp->if_xname);
284 	sc->sc_attached = 1;
285 	splx(s);
286 
287 	/* Ready to accept ioctl */
288 	awi_unlock(sc);
289 
290 	return 0;
291 }
292 
293 int
294 awi_detach(struct awi_softc *sc)
295 {
296 	struct ieee80211com *ic = &sc->sc_ic;
297 	struct ifnet *ifp = &sc->sc_if;
298 	int s;
299 
300 	if (!sc->sc_attached)
301 		return 0;
302 
303 	s = splnet();
304 	awi_stop(ifp, 1);
305 
306 	while (sc->sc_sleep_cnt > 0) {
307 		wakeup(sc);
308 		(void)tsleep(sc, PWAIT, "awidet", 1);
309 	}
310 	sc->sc_attached = 0;
311 	ieee80211_ifdetach(ic);
312 	if_detach(ifp);
313 	shutdownhook_disestablish(sc->sc_sdhook);
314 	powerhook_disestablish(sc->sc_powerhook);
315 	softint_disestablish(sc->sc_soft_ih);
316 	splx(s);
317 	return 0;
318 }
319 
320 int
321 awi_activate(device_t self, enum devact act)
322 {
323 	struct awi_softc *sc = device_private(self);
324 
325 	switch (act) {
326 	case DVACT_DEACTIVATE:
327 		if_deactivate(&sc->sc_if);
328 		return 0;
329 	default:
330 		return EOPNOTSUPP;
331 	}
332 }
333 
334 void
335 awi_power(int why, void *arg)
336 {
337 	struct awi_softc *sc = arg;
338 	struct ifnet *ifp = &sc->sc_if;
339 	int s;
340 	int ocansleep;
341 
342 	DPRINTF(("awi_power: %d\n", why));
343 	s = splnet();
344 	ocansleep = sc->sc_cansleep;
345 	sc->sc_cansleep = 0;
346 	switch (why) {
347 	case PWR_SUSPEND:
348 	case PWR_STANDBY:
349 		awi_stop(ifp, 1);
350 		break;
351 	case PWR_RESUME:
352 		if (ifp->if_flags & IFF_UP) {
353 			awi_init(ifp);
354 			awi_softintr(sc);	/* make sure */
355 		}
356 		break;
357 	case PWR_SOFTSUSPEND:
358 	case PWR_SOFTSTANDBY:
359 	case PWR_SOFTRESUME:
360 		break;
361 	}
362 	sc->sc_cansleep = ocansleep;
363 	splx(s);
364 }
365 
366 void
367 awi_shutdown(void *arg)
368 {
369 	struct awi_softc *sc = arg;
370 	struct ifnet *ifp = &sc->sc_if;
371 
372 	if (sc->sc_attached)
373 		awi_stop(ifp, 1);
374 }
375 
376 int
377 awi_intr(void *arg)
378 {
379 	struct awi_softc *sc = arg;
380 
381 	if (!sc->sc_enabled || !sc->sc_enab_intr ||
382 	    !device_is_active(sc->sc_dev)) {
383 		DPRINTF(("awi_intr: stray interrupt: "
384 		    "enabled %d enab_intr %d invalid %d\n",
385 		    sc->sc_enabled, sc->sc_enab_intr,
386 		    !device_is_active(sc->sc_dev)));
387 		return 0;
388 	}
389 
390 	softint_schedule(sc->sc_soft_ih);
391 	return 1;
392 }
393 
394 static void
395 awi_softintr(void *arg)
396 {
397 	struct awi_softc *sc = arg;
398 	uint16_t status;
399 	int ocansleep;
400 	int s;
401 #ifdef AWI_DEBUG
402 	static const char *intname[] = {
403 	    "CMD", "RX", "TX", "SCAN_CMPLT",
404 	    "CFP_START", "DTIM", "CFP_ENDING", "GROGGY",
405 	    "TXDATA", "TXBCAST", "TXPS", "TXCF",
406 	    "TXMGT", "#13", "RXDATA", "RXMGT"
407 	};
408 #endif
409 
410 	s = splnet();
411 	am79c930_gcr_setbits(&sc->sc_chip,
412 	    AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
413 	awi_write_1(sc, AWI_DIS_PWRDN, 1);
414 	ocansleep = sc->sc_cansleep;
415 	sc->sc_cansleep = 0;
416 
417 	for (;;) {
418 		if (awi_intr_lock(sc) != 0)
419 			break;
420 		status = awi_read_1(sc, AWI_INTSTAT);
421 		awi_write_1(sc, AWI_INTSTAT, 0);
422 		awi_write_1(sc, AWI_INTSTAT, 0);
423 		status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
424 		awi_write_1(sc, AWI_INTSTAT2, 0);
425 		DELAY(10);
426 		awi_intr_unlock(sc);
427 		if (!sc->sc_cmd_inprog)
428 			status &= ~AWI_INT_CMD;	/* make sure */
429 		if (status == 0)
430 			break;
431 #ifdef AWI_DEBUG
432 		if (awi_debug > 1) {
433 			int i;
434 
435 			printf("awi_intr: status 0x%04x", status);
436 			for (i = 0; i < sizeof(intname)/sizeof(intname[0]);
437 			    i++) {
438 				if (status & (1 << i))
439 					printf(" %s", intname[i]);
440 			}
441 			printf("\n");
442 		}
443 #endif
444 		if (status & AWI_INT_RX)
445 			awi_rx_int(sc);
446 		if (status & AWI_INT_TX)
447 			awi_tx_int(sc);
448 		if (status & AWI_INT_CMD)
449 			awi_cmd_done(sc);
450 		if (status & AWI_INT_SCAN_CMPLT) {
451 			if (sc->sc_ic.ic_state == IEEE80211_S_SCAN &&
452 			    sc->sc_substate == AWI_ST_NONE)
453 				ieee80211_next_scan(&sc->sc_ic);
454 		}
455 	}
456 
457 	sc->sc_cansleep = ocansleep;
458 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
459 	awi_write_1(sc, AWI_DIS_PWRDN, 0);
460 	splx(s);
461 }
462 
463 
464 static int
465 awi_init(struct ifnet *ifp)
466 {
467 	struct awi_softc *sc = ifp->if_softc;
468 	struct ieee80211com *ic = &sc->sc_ic;
469 	struct ieee80211_node *ni = ic->ic_bss;
470 	struct ieee80211_rateset *rs;
471 	int error, rate, i;
472 
473 	DPRINTF(("awi_init: enabled=%d\n", sc->sc_enabled));
474 	if (sc->sc_enabled) {
475 		awi_stop(ifp, 0);
476 	} else {
477 		if (sc->sc_enable)
478 			(*sc->sc_enable)(sc);
479 		sc->sc_enabled = 1;
480 		if ((error = awi_hw_init(sc)) != 0) {
481 			if (sc->sc_disable)
482 				(*sc->sc_disable)(sc);
483 			sc->sc_enabled = 0;
484 			return error;
485 		}
486 	}
487 	ic->ic_state = IEEE80211_S_INIT;
488 
489 	ic->ic_flags &= ~IEEE80211_F_IBSSON;
490 	switch (ic->ic_opmode) {
491 	case IEEE80211_M_STA:
492 		sc->sc_mib_local.Network_Mode = 1;
493 		sc->sc_mib_local.Acting_as_AP = 0;
494 		break;
495 	case IEEE80211_M_IBSS:
496 		ic->ic_flags |= IEEE80211_F_IBSSON;
497 		/* FALLTHRU */
498 	case IEEE80211_M_AHDEMO:
499 		sc->sc_mib_local.Network_Mode = 0;
500 		sc->sc_mib_local.Acting_as_AP = 0;
501 		break;
502 	case IEEE80211_M_HOSTAP:
503 		sc->sc_mib_local.Network_Mode = 1;
504 		sc->sc_mib_local.Acting_as_AP = 1;
505 		break;
506 	case IEEE80211_M_MONITOR:
507 		return ENODEV;
508 	}
509 #if 0
510 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
511 #endif
512 	memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
513 	sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
514 	sc->sc_mib_mac.aDesired_ESS_ID[1] = ic->ic_des_esslen;
515 	memcpy(&sc->sc_mib_mac.aDesired_ESS_ID[2], ic->ic_des_essid,
516 	    ic->ic_des_esslen);
517 
518 	/* Configure basic rate */
519 	if (ic->ic_phytype == IEEE80211_T_FH)
520 		rs = &ic->ic_sup_rates[IEEE80211_MODE_FH];
521 	else
522 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
523 	if (ic->ic_fixed_rate != -1) {
524 		rate = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
525 	} else {
526 		rate = 0;
527 		for (i = 0; i < rs->rs_nrates; i++) {
528 			if ((rs->rs_rates[i] & IEEE80211_RATE_BASIC) &&
529 			    rate < (rs->rs_rates[i] & IEEE80211_RATE_VAL))
530 				rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
531 		}
532 	}
533 	rate *= 5;
534 	LE_WRITE_2(&sc->sc_mib_mac.aStation_Basic_Rate, rate);
535 
536 	if ((error = awi_mode_init(sc)) != 0) {
537 		DPRINTF(("awi_init: awi_mode_init failed %d\n", error));
538 		awi_stop(ifp, 1);
539 		return error;
540 	}
541 
542 	/* Start transmitter */
543 	sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
544 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
545 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
546 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
547 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
548 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
549 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
550 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
551 	awi_write_4(sc, AWI_CA_TX_DATA, sc->sc_txbase);
552 	awi_write_4(sc, AWI_CA_TX_MGT, 0);
553 	awi_write_4(sc, AWI_CA_TX_BCAST, 0);
554 	awi_write_4(sc, AWI_CA_TX_PS, 0);
555 	awi_write_4(sc, AWI_CA_TX_CF, 0);
556 	if ((error = awi_cmd(sc, AWI_CMD_INIT_TX, AWI_WAIT)) != 0) {
557 		DPRINTF(("awi_init: failed to start transmitter: %d\n", error));
558 		awi_stop(ifp, 1);
559 		return error;
560 	}
561 
562 	/* Start receiver */
563 	if ((error = awi_cmd(sc, AWI_CMD_INIT_RX, AWI_WAIT)) != 0) {
564 		DPRINTF(("awi_init: failed to start receiver: %d\n", error));
565 		awi_stop(ifp, 1);
566 		return error;
567 	}
568 	sc->sc_rxdoff = awi_read_4(sc, AWI_CA_IRX_DATA_DESC);
569 	sc->sc_rxmoff = awi_read_4(sc, AWI_CA_IRX_PS_DESC);
570 
571 	ifp->if_flags |= IFF_RUNNING;
572 	ifp->if_flags &= ~IFF_OACTIVE;
573 	ic->ic_state = IEEE80211_S_INIT;
574 
575 	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
576 	    ic->ic_opmode == IEEE80211_M_HOSTAP) {
577 		ni->ni_chan = ic->ic_ibss_chan;
578 		ni->ni_intval = ic->ic_lintval;
579 		ni->ni_rssi = 0;
580 		ni->ni_rstamp = 0;
581 		memset(&ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
582 		ni->ni_rates =
583 		    ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
584 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
585 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
586 			IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
587 			ni->ni_esslen = ic->ic_des_esslen;
588 			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
589 			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
590 			if (ic->ic_phytype == IEEE80211_T_FH) {
591 				ni->ni_fhdwell = 200;	/* XXX */
592 				ni->ni_fhindex = 1;
593 			}
594 		} else {
595 			ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
596 			memset(ni->ni_bssid, 0, IEEE80211_ADDR_LEN);
597 			ni->ni_esslen = 0;
598 		}
599 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
600 			ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
601 		if (ic->ic_opmode != IEEE80211_M_AHDEMO)
602 			ic->ic_flags |= IEEE80211_F_SIBSS;
603 		ic->ic_state = IEEE80211_S_SCAN;	/*XXX*/
604 		sc->sc_substate = AWI_ST_NONE;
605 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
606 	} else {
607 		/* XXX check sc->sc_cur_chan */
608 		ni->ni_chan = &ic->ic_channels[sc->sc_cur_chan];
609 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
610 	}
611 	return 0;
612 }
613 
614 static void
615 awi_stop(struct ifnet *ifp, int disable)
616 {
617 	struct awi_softc *sc = ifp->if_softc;
618 
619 	if (!sc->sc_enabled)
620 		return;
621 
622 	DPRINTF(("awi_stop(%d)\n", disable));
623 
624 	ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
625 
626 	if (device_is_active(sc->sc_dev)) {
627 		if (sc->sc_cmd_inprog)
628 			(void)awi_cmd_wait(sc);
629 		(void)awi_cmd(sc, AWI_CMD_KILL_RX, AWI_WAIT);
630 		sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
631 		awi_write_1(sc, AWI_CA_FTX_DATA, 1);
632 		awi_write_1(sc, AWI_CA_FTX_MGT, 0);
633 		awi_write_1(sc, AWI_CA_FTX_BCAST, 0);
634 		awi_write_1(sc, AWI_CA_FTX_PS, 0);
635 		awi_write_1(sc, AWI_CA_FTX_CF, 0);
636 		(void)awi_cmd(sc, AWI_CMD_FLUSH_TX, AWI_WAIT);
637 	}
638 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
639 	ifp->if_timer = 0;
640 	sc->sc_tx_timer = sc->sc_rx_timer = 0;
641 	if (sc->sc_rxpend != NULL) {
642 		m_freem(sc->sc_rxpend);
643 		sc->sc_rxpend = NULL;
644 	}
645 	IFQ_PURGE(&ifp->if_snd);
646 
647 	if (disable) {
648 		if (device_is_active(sc->sc_dev))
649 			am79c930_gcr_setbits(&sc->sc_chip,
650 			    AM79C930_GCR_CORESET);
651 		if (sc->sc_disable)
652 			(*sc->sc_disable)(sc);
653 		sc->sc_enabled = 0;
654 	}
655 }
656 
657 static void
658 awi_start(struct ifnet *ifp)
659 {
660 	struct awi_softc *sc = ifp->if_softc;
661 	struct ieee80211com *ic = &sc->sc_ic;
662 	struct ether_header *eh;
663 	struct ieee80211_node *ni;
664 	struct ieee80211_frame *wh;
665 	struct mbuf *m, *m0;
666 	int len, dowep;
667 	uint32_t txd, frame, ntxd;
668 	uint8_t rate;
669 
670 	if (!sc->sc_enabled || !device_is_active(sc->sc_dev))
671 		return;
672 
673 	for (;;) {
674 		txd = sc->sc_txnext;
675 		IF_POLL(&ic->ic_mgtq, m0);
676 		dowep = 0;
677 		if (m0 != NULL) {
678 			len = m0->m_pkthdr.len;
679 			if (awi_next_txd(sc, len, &frame, &ntxd)) {
680 				ifp->if_flags |= IFF_OACTIVE;
681 				break;
682 			}
683 			IF_DEQUEUE(&ic->ic_mgtq, m0);
684 			ni = M_GETCTX(m0, struct ieee80211_node *);
685 		} else {
686 			if (ic->ic_state != IEEE80211_S_RUN)
687 				break;
688 			IFQ_POLL(&ifp->if_snd, m0);
689 			if (m0 == NULL)
690 				break;
691 			/*
692 			 * Need to calculate the real length to determine
693 			 * if the transmit buffer has a room for the packet.
694 			 */
695 			len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
696 			if (!(ifp->if_flags & IFF_LINK0) && !sc->sc_adhoc_ap)
697 				len += sizeof(struct llc) -
698 				    sizeof(struct ether_header);
699 			if (ic->ic_flags & IEEE80211_F_PRIVACY) {
700 				dowep = 1;
701 				len += IEEE80211_WEP_IVLEN +
702 				    IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
703 			}
704 			if (awi_next_txd(sc, len, &frame, &ntxd)) {
705 				ifp->if_flags |= IFF_OACTIVE;
706 				break;
707 			}
708 			IFQ_DEQUEUE(&ifp->if_snd, m0);
709 			ifp->if_opackets++;
710 			bpf_mtap(ifp, m0, BPF_D_OUT);
711 			eh = mtod(m0, struct ether_header *);
712 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
713 			if (ni == NULL) {
714 				ifp->if_oerrors++;
715 				continue;
716 			}
717 			if ((ifp->if_flags & IFF_LINK0) || sc->sc_adhoc_ap)
718 				m0 = awi_ether_encap(sc, m0);
719 			else {
720 				m0 = ieee80211_encap(ic, m0, ni);
721 			}
722 			if (m0 == NULL) {
723 				ieee80211_free_node(ni);
724 				ifp->if_oerrors++;
725 				continue;
726 			}
727 			wh = mtod(m0, struct ieee80211_frame *);
728 			if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
729 			    (ic->ic_opmode == IEEE80211_M_HOSTAP ||
730 			     ic->ic_opmode == IEEE80211_M_IBSS) &&
731 			    sc->sc_adhoc_ap == 0 &&
732 			    (ifp->if_flags & IFF_LINK0) == 0 &&
733 			    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
734 			    IEEE80211_FC0_TYPE_DATA) {
735 				m_freem(m0);
736 				ieee80211_free_node(ni);
737 				ifp->if_oerrors++;
738 				continue;
739 			}
740 		}
741 		bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
742 		if (dowep) {
743 			if ((ieee80211_crypto_encap(ic, ni, m0)) == NULL) {
744 				m_freem(m0);
745 				ieee80211_free_node(ni);
746 				ifp->if_oerrors++;
747 				continue;
748 			}
749 		}
750 		ieee80211_free_node(ni);
751 #ifdef DIAGNOSTIC
752 		if (m0->m_pkthdr.len != len) {
753 			printf("%s: length %d should be %d\n",
754 			    sc->sc_if.if_xname, m0->m_pkthdr.len, len);
755 			m_freem(m0);
756 			ifp->if_oerrors++;
757 			continue;
758 		}
759 #endif
760 
761 		if ((ifp->if_flags & IFF_DEBUG) && (ifp->if_flags & IFF_LINK2))
762 			ieee80211_dump_pkt(m0->m_data, m0->m_len,
763 			    ic->ic_bss->ni_rates.
764 				rs_rates[ic->ic_bss->ni_txrate] &
765 			    IEEE80211_RATE_VAL, -1);
766 
767 		for (m = m0, len = 0; m != NULL; m = m->m_next) {
768 			awi_write_bytes(sc, frame + len, mtod(m, uint8_t *),
769 			    m->m_len);
770 			len += m->m_len;
771 		}
772 		m_freem(m0);
773 		rate = (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
774 		    IEEE80211_RATE_VAL) * 5;
775 		awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
776 		awi_write_4(sc, txd + AWI_TXD_START, frame);
777 		awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
778 		awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
779 		awi_write_1(sc, txd + AWI_TXD_RATE, rate);
780 		awi_write_4(sc, txd + AWI_TXD_NDA, 0);
781 		awi_write_4(sc, txd + AWI_TXD_NRA, 0);
782 		awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
783 		sc->sc_txnext = ntxd;
784 
785 		sc->sc_tx_timer = 5;
786 		ifp->if_timer = 1;
787 	}
788 }
789 
790 static void
791 awi_watchdog(struct ifnet *ifp)
792 {
793 	struct awi_softc *sc = ifp->if_softc;
794 	uint32_t prevdone;
795 	int ocansleep;
796 
797 	ifp->if_timer = 0;
798 	if (!sc->sc_enabled || !device_is_active(sc->sc_dev))
799 		return;
800 
801 	ocansleep = sc->sc_cansleep;
802 	sc->sc_cansleep = 0;
803 	if (sc->sc_tx_timer) {
804 		if (--sc->sc_tx_timer == 0) {
805 			printf("%s: device timeout\n", ifp->if_xname);
806 			prevdone = sc->sc_txdone;
807 			awi_tx_int(sc);
808 			if (sc->sc_txdone == prevdone) {
809 				ifp->if_oerrors++;
810 				awi_init(ifp);
811 				goto out;
812 			}
813 		}
814 		ifp->if_timer = 1;
815 	}
816 	if (sc->sc_rx_timer) {
817 		if (--sc->sc_rx_timer == 0) {
818 			if (sc->sc_ic.ic_state == IEEE80211_S_RUN) {
819 				ieee80211_new_state(&sc->sc_ic,
820 				    IEEE80211_S_SCAN, -1);
821 				goto out;
822 			}
823 		} else
824 			ifp->if_timer = 1;
825 	}
826 	/* TODO: rate control */
827 	ieee80211_watchdog(&sc->sc_ic);
828   out:
829 	sc->sc_cansleep = ocansleep;
830 }
831 
832 static int
833 awi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
834 {
835 	struct awi_softc *sc = ifp->if_softc;
836 	struct ifreq *ifr = (struct ifreq *)data;
837 	int s, error;
838 
839 	s = splnet();
840 	/* Serialize ioctl, since we may sleep */
841 	if ((error = awi_lock(sc)) != 0)
842 		goto cantlock;
843 
844 	switch (cmd) {
845 	case SIOCSIFFLAGS:
846 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
847 			break;
848 		if (ifp->if_flags & IFF_UP) {
849 			if (sc->sc_enabled) {
850 				/*
851 				 * To avoid rescanning another access point,
852 				 * do not call awi_init() here.  Instead,
853 				 * only reflect promisc mode settings.
854 				 */
855 				error = awi_mode_init(sc);
856 			} else
857 				error = awi_init(ifp);
858 		} else if (sc->sc_enabled)
859 			awi_stop(ifp, 1);
860 		break;
861 	case SIOCSIFMEDIA:
862 	case SIOCGIFMEDIA:
863 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_ic.ic_media, cmd);
864 		break;
865 	case SIOCADDMULTI:
866 	case SIOCDELMULTI:
867 		error = ether_ioctl(ifp, cmd, data);
868 		if (error == ENETRESET) {
869 			/* Do not rescan */
870 			if (ifp->if_flags & IFF_RUNNING)
871 				error = awi_mode_init(sc);
872 			else
873 				error = 0;
874 		}
875 		break;
876 	default:
877 		error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
878 		if (error == ENETRESET) {
879 			if (sc->sc_enabled)
880 				error = awi_init(ifp);
881 			else
882 				error = 0;
883 		}
884 		break;
885 	}
886 	awi_unlock(sc);
887   cantlock:
888 	splx(s);
889 	return error;
890 }
891 
892 /*
893  * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
894  *
895  * TBD factor with ieee80211_media_change
896  */
897 static int
898 awi_media_change(struct ifnet *ifp)
899 {
900 	struct awi_softc *sc = ifp->if_softc;
901 	struct ieee80211com *ic = &sc->sc_ic;
902 	struct ifmedia_entry *ime;
903 	enum ieee80211_opmode newmode;
904 	int i, rate, newadhoc_ap, error = 0;
905 
906 	ime = ic->ic_media.ifm_cur;
907 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
908 		i = -1;
909 	} else {
910 		struct ieee80211_rateset *rs =
911 		    &ic->ic_sup_rates[(ic->ic_phytype == IEEE80211_T_FH)
912 		    ? IEEE80211_MODE_FH : IEEE80211_MODE_11B];
913 		rate = ieee80211_media2rate(ime->ifm_media);
914 		if (rate == 0)
915 			return EINVAL;
916 		for (i = 0; i < rs->rs_nrates; i++) {
917 			if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
918 				break;
919 		}
920 		if (i == rs->rs_nrates)
921 			return EINVAL;
922 	}
923 	if (ic->ic_fixed_rate != i) {
924 		ic->ic_fixed_rate = i;
925 		error = ENETRESET;
926 	}
927 
928 	/*
929 	 * Combination of mediaopt
930 	 *
931 	 * hostap adhoc flag0	opmode  adhoc_ap	comment
932 	 *   +      -     -	HOSTAP      0		HostAP
933 	 *   -      +     -	IBSS        0		IBSS
934 	 *   -      +     +	AHDEMO      0		WaveLAN adhoc
935 	 *   -      -     +	IBSS        1		Melco old Sta
936 	 *							also LINK0
937 	 *   -      -     -	STA         0		Infra Station
938 	 */
939 	newadhoc_ap = 0;
940 	if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
941 		newmode = IEEE80211_M_HOSTAP;
942 	else if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
943 		if (ic->ic_phytype == IEEE80211_T_DS &&
944 		    (ime->ifm_media & IFM_FLAG0))
945 			newmode = IEEE80211_M_AHDEMO;
946 		else
947 			newmode = IEEE80211_M_IBSS;
948 	} else if (ime->ifm_media & IFM_FLAG0) {
949 		newmode = IEEE80211_M_IBSS;
950 		newadhoc_ap = 1;
951 	} else
952 		newmode = IEEE80211_M_STA;
953 	if (ic->ic_opmode != newmode || sc->sc_adhoc_ap != newadhoc_ap) {
954 		ic->ic_opmode = newmode;
955 		sc->sc_adhoc_ap = newadhoc_ap;
956 		error = ENETRESET;
957 	}
958 
959 	if (error == ENETRESET) {
960 		if (sc->sc_enabled)
961 			error = awi_init(ifp);
962 		else
963 			error = 0;
964 	}
965 	return error;
966 }
967 
968 static void
969 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
970 {
971 	struct awi_softc *sc = ifp->if_softc;
972 	struct ieee80211com *ic = &sc->sc_ic;
973 	int rate;
974 	enum ieee80211_phymode mode;
975 
976 	imr->ifm_status = IFM_AVALID;
977 	if (ic->ic_state == IEEE80211_S_RUN)
978 		imr->ifm_status |= IFM_ACTIVE;
979 	imr->ifm_active = IFM_IEEE80211;
980 	if (ic->ic_phytype == IEEE80211_T_FH)
981 		mode = IEEE80211_MODE_FH;
982 	else
983 		mode = IEEE80211_MODE_11B;
984 	if (ic->ic_state == IEEE80211_S_RUN) {
985 		rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
986 		    IEEE80211_RATE_VAL;
987 	} else {
988 		if (ic->ic_fixed_rate == -1)
989 			rate = 0;
990 		else
991 			rate = ic->ic_sup_rates[mode].
992 			    rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
993 	}
994 	imr->ifm_active |= ieee80211_rate2media(ic, rate, mode);
995 	switch (ic->ic_opmode) {
996 	case IEEE80211_M_MONITOR: /* We should never reach here */
997 		break;
998 	case IEEE80211_M_STA:
999 		break;
1000 	case IEEE80211_M_IBSS:
1001 		if (sc->sc_adhoc_ap)
1002 			imr->ifm_active |= IFM_FLAG0;
1003 		else
1004 			imr->ifm_active |= IFM_IEEE80211_ADHOC;
1005 		break;
1006 	case IEEE80211_M_AHDEMO:
1007 		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1008 		break;
1009 	case IEEE80211_M_HOSTAP:
1010 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1011 		break;
1012 	}
1013 }
1014 
1015 static int
1016 awi_mode_init(struct awi_softc *sc)
1017 {
1018 	struct ethercom *ec = &sc->sc_ec;
1019 	struct ifnet *ifp = &sc->sc_if;
1020 	int n, error;
1021 	struct ether_multi *enm;
1022 	struct ether_multistep step;
1023 
1024 	/* Reinitialize muticast filter */
1025 	n = 0;
1026 	sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
1027 	if (sc->sc_ic.ic_opmode != IEEE80211_M_HOSTAP &&
1028 	    (ifp->if_flags & IFF_PROMISC)) {
1029 		sc->sc_mib_mac.aPromiscuous_Enable = 1;
1030 		goto set_mib;
1031 	}
1032 	sc->sc_mib_mac.aPromiscuous_Enable = 0;
1033 	ETHER_LOCK(ec);
1034 	ETHER_FIRST_MULTI(step, ec, enm);
1035 	while (enm != NULL) {
1036 		if (n == AWI_GROUP_ADDR_SIZE ||
1037 		    !IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi)) {
1038 			ETHER_UNLOCK(ec);
1039 			goto set_mib;
1040 		}
1041 		IEEE80211_ADDR_COPY(sc->sc_mib_addr.aGroup_Addresses[n],
1042 		    enm->enm_addrlo);
1043 		n++;
1044 		ETHER_NEXT_MULTI(step, enm);
1045 	}
1046 	ETHER_UNLOCK(ec);
1047 	for (; n < AWI_GROUP_ADDR_SIZE; n++)
1048 		memset(sc->sc_mib_addr.aGroup_Addresses[n], 0,
1049 		    IEEE80211_ADDR_LEN);
1050 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1051 
1052   set_mib:
1053 	if (sc->sc_mib_local.Accept_All_Multicast_Dis)
1054 		ifp->if_flags &= ~IFF_ALLMULTI;
1055 	else
1056 		ifp->if_flags |= IFF_ALLMULTI;
1057 	sc->sc_mib_mgt.Wep_Required =
1058 	    (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) ? AWI_WEP_ON : AWI_WEP_OFF;
1059 
1060 	if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1061 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1062 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1063 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1064 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1065 		DPRINTF(("awi_mode_init: MIB set failed: %d\n", error));
1066 		return error;
1067 	}
1068 	return 0;
1069 }
1070 
1071 static void
1072 awi_rx_int(struct awi_softc *sc)
1073 {
1074 	struct ieee80211com *ic = &sc->sc_ic;
1075 	struct ifnet *ifp = &sc->sc_if;
1076 	struct ieee80211_frame_min *wh;
1077 	struct ieee80211_node *ni;
1078 	uint8_t state, rate, rssi;
1079 	uint16_t len;
1080 	uint32_t frame, next, rstamp, rxoff;
1081 	struct mbuf *m;
1082 
1083 	rxoff = sc->sc_rxdoff;
1084 	for (;;) {
1085 		state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1086 		if (state & AWI_RXD_ST_OWN)
1087 			break;
1088 		if (!(state & AWI_RXD_ST_CONSUMED)) {
1089 			if (sc->sc_substate != AWI_ST_NONE)
1090 				goto rx_next;
1091 			if (state & AWI_RXD_ST_RXERROR) {
1092 				ifp->if_ierrors++;
1093 				goto rx_next;
1094 			}
1095 			len    = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1096 			rate   = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1097 			rssi   = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1098 			frame  = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) &
1099 			    0x7fff;
1100 			rstamp = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1101 			m = awi_devget(sc, frame, len);
1102 			if (m == NULL) {
1103 				ifp->if_ierrors++;
1104 				goto rx_next;
1105 			}
1106 			if (state & AWI_RXD_ST_LF) {
1107 				/* TODO check my bss */
1108 				if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) &&
1109 				    sc->sc_ic.ic_state == IEEE80211_S_RUN) {
1110 					sc->sc_rx_timer = 10;
1111 					ifp->if_timer = 1;
1112 				}
1113 				if ((ifp->if_flags & IFF_DEBUG) &&
1114 				    (ifp->if_flags & IFF_LINK2))
1115 					ieee80211_dump_pkt(m->m_data, m->m_len,
1116 					    rate / 5, rssi);
1117 				if ((ifp->if_flags & IFF_LINK0) ||
1118 				    sc->sc_adhoc_ap)
1119 					m = awi_ether_modcap(sc, m);
1120 				else
1121 					m = m_pullup(m, sizeof(*wh));
1122 				if (m == NULL) {
1123 					ifp->if_ierrors++;
1124 					goto rx_next;
1125 				}
1126 				wh = mtod(m, struct ieee80211_frame_min *);
1127 				ni = ieee80211_find_rxnode(ic, wh);
1128 				ieee80211_input(ic, m, ni, rssi, rstamp);
1129 				/*
1130 				 * The frame may have caused the
1131 				 * node to be marked for reclamation
1132 				 * (e.g. in response to a DEAUTH
1133 				 * message) so use release_node here
1134 				 * instead of unref_node.
1135 				 */
1136 				ieee80211_free_node(ni);
1137 			} else
1138 				sc->sc_rxpend = m;
1139   rx_next:
1140 			state |= AWI_RXD_ST_CONSUMED;
1141 			awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1142 		}
1143 		next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1144 		if (next & AWI_RXD_NEXT_LAST)
1145 			break;
1146 		/* Make sure the next pointer is correct */
1147 		if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1148 			break;
1149 		state |= AWI_RXD_ST_OWN;
1150 		awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1151 		rxoff = next & 0x7fff;
1152 	}
1153 	sc->sc_rxdoff = rxoff;
1154 }
1155 
1156 static void
1157 awi_tx_int(struct awi_softc *sc)
1158 {
1159 	struct ifnet *ifp = &sc->sc_if;
1160 	uint8_t flags;
1161 
1162 	while (sc->sc_txdone != sc->sc_txnext) {
1163 		flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1164 		if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1165 			break;
1166 		if (flags & AWI_TXD_ST_ERROR)
1167 			ifp->if_oerrors++;
1168 		sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1169 		    0x7fff;
1170 	}
1171 	DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1172 	    sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend));
1173 	sc->sc_tx_timer = 0;
1174 	ifp->if_flags &= ~IFF_OACTIVE;
1175 	awi_start(ifp); /* in softint */
1176 }
1177 
1178 static struct mbuf *
1179 awi_devget(struct awi_softc *sc, uint32_t off, uint16_t len)
1180 {
1181 	struct ifnet *ifp = &sc->sc_if;
1182 	struct mbuf *m;
1183 	struct mbuf *top, **mp;
1184 	u_int tlen;
1185 
1186 	top = sc->sc_rxpend;
1187 	mp = &top;
1188 	if (top != NULL) {
1189 		sc->sc_rxpend = NULL;
1190 		top->m_pkthdr.len += len;
1191 		m = top;
1192 		while (*mp != NULL) {
1193 			m = *mp;
1194 			mp = &m->m_next;
1195 		}
1196 		if (m->m_flags & M_EXT)
1197 			tlen = m->m_ext.ext_size;
1198 		else if (m->m_flags & M_PKTHDR)
1199 			tlen = MHLEN;
1200 		else
1201 			tlen = MLEN;
1202 		tlen -= m->m_len;
1203 		if (tlen > len)
1204 			tlen = len;
1205 		awi_read_bytes(sc, off, mtod(m, uint8_t *) + m->m_len, tlen);
1206 		off += tlen;
1207 		len -= tlen;
1208 	}
1209 
1210 	while (len > 0) {
1211 		if (top == NULL) {
1212 			MGETHDR(m, M_DONTWAIT, MT_DATA);
1213 			if (m == NULL)
1214 				return NULL;
1215 			m_set_rcvif(m, ifp);
1216 			m->m_pkthdr.len = len;
1217 			m->m_len = MHLEN;
1218 			m->m_flags |= M_HASFCS;
1219 		} else {
1220 			MGET(m, M_DONTWAIT, MT_DATA);
1221 			if (m == NULL) {
1222 				m_freem(top);
1223 				return NULL;
1224 			}
1225 			m->m_len = MLEN;
1226 		}
1227 		if (len >= MINCLSIZE) {
1228 			MCLGET(m, M_DONTWAIT);
1229 			if (m->m_flags & M_EXT)
1230 				m->m_len = m->m_ext.ext_size;
1231 		}
1232 		if (top == NULL) {
1233 			int hdrlen = sizeof(struct ieee80211_frame) +
1234 			    sizeof(struct llc);
1235 			char *newdata = (char *)
1236 			    ALIGN(m->m_data + hdrlen) - hdrlen;
1237 			m->m_len -= newdata - m->m_data;
1238 			m->m_data = newdata;
1239 		}
1240 		if (m->m_len > len)
1241 			m->m_len = len;
1242 		awi_read_bytes(sc, off, mtod(m, uint8_t *), m->m_len);
1243 		off += m->m_len;
1244 		len -= m->m_len;
1245 		*mp = m;
1246 		mp = &m->m_next;
1247 	}
1248 	return top;
1249 }
1250 
1251 /*
1252  * Initialize hardware and start firmware to accept commands.
1253  * Called everytime after power on firmware.
1254  */
1255 
1256 static int
1257 awi_hw_init(struct awi_softc *sc)
1258 {
1259 	uint8_t status;
1260 	uint16_t intmask;
1261 	int i, error;
1262 
1263 	sc->sc_enab_intr = 0;
1264 	awi_drvstate(sc, AWI_DRV_RESET);
1265 
1266 	/* Reset firmware */
1267 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1268 	DELAY(100);
1269 	awi_write_1(sc, AWI_SELFTEST, 0);
1270 	awi_write_1(sc, AWI_CMD, 0);
1271 	awi_write_1(sc, AWI_BANNER, 0);
1272 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1273 	DELAY(100);
1274 
1275 	/* Wait for selftest completion */
1276 	for (i = 0; ; i++) {
1277 		if (!device_is_active(sc->sc_dev))
1278 			return ENXIO;
1279 		if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1280 			printf("%s: failed to complete selftest (timeout)\n",
1281 			    sc->sc_if.if_xname);
1282 			return ENXIO;
1283 		}
1284 		status = awi_read_1(sc, AWI_SELFTEST);
1285 		if ((status & 0xf0) == 0xf0)
1286 			break;
1287 		if (sc->sc_cansleep) {
1288 			sc->sc_sleep_cnt++;
1289 			(void)tsleep(sc, PWAIT, "awitst", 1);
1290 			sc->sc_sleep_cnt--;
1291 		} else {
1292 			DELAY(1000*1000/hz);
1293 		}
1294 	}
1295 	if (status != AWI_SELFTEST_PASSED) {
1296 		printf("%s: failed to complete selftest (code %x)\n",
1297 		    sc->sc_if.if_xname, status);
1298 		return ENXIO;
1299 	}
1300 
1301 	/* Check banner to confirm firmware write it */
1302 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1303 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1304 		printf("%s: failed to complete selftest (bad banner)\n",
1305 		    sc->sc_if.if_xname);
1306 		for (i = 0; i < AWI_BANNER_LEN; i++)
1307 			printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1308 		printf("\n");
1309 		return ENXIO;
1310 	}
1311 
1312 	/* Initializing interrupt */
1313 	sc->sc_enab_intr = 1;
1314 	error = awi_intr_lock(sc);
1315 	if (error)
1316 		return error;
1317 	intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1318 	    AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1319 	awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1320 	awi_write_1(sc, AWI_INTMASK2, 0);
1321 	awi_write_1(sc, AWI_INTSTAT, 0);
1322 	awi_write_1(sc, AWI_INTSTAT2, 0);
1323 	awi_intr_unlock(sc);
1324 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1325 
1326 	/* Issuing interface test command */
1327 	error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT);
1328 	if (error) {
1329 		printf("%s: failed to complete selftest",
1330 		    sc->sc_if.if_xname);
1331 		if (error == ENXIO)
1332 			printf(" (no hardware)\n");
1333 		else if (error != EWOULDBLOCK)
1334 			printf(" (error %d)\n", error);
1335 		else if (sc->sc_cansleep)
1336 			printf(" (lost interrupt)\n");
1337 		else
1338 			printf(" (command timeout)\n");
1339 		return error;
1340 	}
1341 
1342 	/* Initialize VBM */
1343 	awi_write_1(sc, AWI_VBM_OFFSET, 0);
1344 	awi_write_1(sc, AWI_VBM_LENGTH, 1);
1345 	awi_write_1(sc, AWI_VBM_BITMAP, 0);
1346 	return 0;
1347 }
1348 
1349 /*
1350  * Extract the factory default MIB value from firmware and assign the driver
1351  * default value.
1352  * Called once at attaching the interface.
1353  */
1354 
1355 static int
1356 awi_init_mibs(struct awi_softc *sc)
1357 {
1358 	int chan, i, error;
1359 	struct ieee80211com *ic = &sc->sc_ic;
1360 	const struct awi_chanset *cs;
1361 
1362 	if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1363 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1364 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1365 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1366 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1367 		printf("%s: failed to get default mib value (error %d)\n",
1368 		    sc->sc_if.if_xname, error);
1369 		return error;
1370 	}
1371 
1372 	memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail));
1373 	for (cs = awi_chanset; ; cs++) {
1374 		if (cs->cs_type == 0) {
1375 			printf("%s: failed to set available channel\n",
1376 			    sc->sc_if.if_xname);
1377 			return ENXIO;
1378 		}
1379 		if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type &&
1380 		    cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain)
1381 			break;
1382 	}
1383 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1384 		for (i = cs->cs_min; i <= cs->cs_max; i++) {
1385 			chan = IEEE80211_FH_CHAN(i % 3 + 1, i);
1386 			setbit(sc->sc_ic.ic_chan_avail, chan);
1387 			/* XXX for FHSS, does frequency matter? */
1388 			ic->ic_channels[chan].ic_freq = 0;
1389 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
1390 			/*
1391 			 * According to the IEEE 802.11 specification,
1392 			 * hop pattern parameter for FH phy should be
1393 			 * incremented by 3 for given hop chanset, i.e.,
1394 			 * the chanset parameter is calculated for given
1395 			 * hop patter.  However, BayStack 650 Access Points
1396 			 * apparently use fixed hop chanset parameter value
1397 			 * 1 for any hop pattern.  So we also try this
1398 			 * combination of hop chanset and pattern.
1399 			 */
1400 			chan = IEEE80211_FH_CHAN(1, i);
1401 			setbit(sc->sc_ic.ic_chan_avail, chan);
1402 			ic->ic_channels[chan].ic_freq = 0; /* XXX */
1403 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_FHSS;
1404 		}
1405 	} else {
1406 		for (i = cs->cs_min; i <= cs->cs_max; i++) {
1407 			setbit(sc->sc_ic.ic_chan_avail, i);
1408 			ic->ic_channels[i].ic_freq =
1409 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
1410 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
1411 		}
1412 	}
1413 	sc->sc_cur_chan = cs->cs_def;
1414 	ic->ic_ibss_chan = &ic->ic_channels[cs->cs_def];
1415 
1416 	sc->sc_mib_local.Fragmentation_Dis = 1;
1417 	sc->sc_mib_local.Add_PLCP_Dis = 0;
1418 	sc->sc_mib_local.MAC_Hdr_Prsv = 0;
1419 	sc->sc_mib_local.Rx_Mgmt_Que_En = 0;
1420 	sc->sc_mib_local.Re_Assembly_Dis = 1;
1421 	sc->sc_mib_local.Strip_PLCP_Dis = 0;
1422 	sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1423 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1424 	sc->sc_mib_local.Check_Seq_Cntl_Dis = 0;
1425 	sc->sc_mib_local.Flush_CFP_Queue_On_CF_End = 0;
1426 	sc->sc_mib_local.Network_Mode = 1;
1427 	sc->sc_mib_local.PWD_Lvl = 0;
1428 	sc->sc_mib_local.CFP_Mode = 0;
1429 
1430 	/* Allocate buffers */
1431 	sc->sc_txbase = AWI_BUFFERS;
1432 	sc->sc_txend = sc->sc_txbase +
1433 	    (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1434 	    sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1435 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1436 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1437 	    sc->sc_txend - sc->sc_txbase);
1438 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1439 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1440 	    AWI_BUFFERS_END - sc->sc_txend);
1441 	sc->sc_mib_local.Acting_as_AP = 0;
1442 	sc->sc_mib_local.Fill_CFP = 0;
1443 
1444 	memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1445 	sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1446 
1447 	sc->sc_mib_mgt.aPower_Mgt_Mode = 0;
1448 	sc->sc_mib_mgt.aDTIM_Period = 1;
1449 	LE_WRITE_2(&sc->sc_mib_mgt.aATIM_Window, 0);
1450 	return 0;
1451 }
1452 
1453 static int
1454 awi_mib(struct awi_softc *sc, uint8_t cmd, uint8_t mib, int wflag)
1455 {
1456 	int error;
1457 	uint8_t size, *ptr;
1458 
1459 	switch (mib) {
1460 	case AWI_MIB_LOCAL:
1461 		ptr = (uint8_t *)&sc->sc_mib_local;
1462 		size = sizeof(sc->sc_mib_local);
1463 		break;
1464 	case AWI_MIB_ADDR:
1465 		ptr = (uint8_t *)&sc->sc_mib_addr;
1466 		size = sizeof(sc->sc_mib_addr);
1467 		break;
1468 	case AWI_MIB_MAC:
1469 		ptr = (uint8_t *)&sc->sc_mib_mac;
1470 		size = sizeof(sc->sc_mib_mac);
1471 		break;
1472 	case AWI_MIB_STAT:
1473 		ptr = (uint8_t *)&sc->sc_mib_stat;
1474 		size = sizeof(sc->sc_mib_stat);
1475 		break;
1476 	case AWI_MIB_MGT:
1477 		ptr = (uint8_t *)&sc->sc_mib_mgt;
1478 		size = sizeof(sc->sc_mib_mgt);
1479 		break;
1480 	case AWI_MIB_PHY:
1481 		ptr = (uint8_t *)&sc->sc_mib_phy;
1482 		size = sizeof(sc->sc_mib_phy);
1483 		break;
1484 	default:
1485 		return EINVAL;
1486 	}
1487 	if (sc->sc_cmd_inprog) {
1488 		if ((error = awi_cmd_wait(sc)) != 0) {
1489 			if (error == EWOULDBLOCK) {
1490 				DPRINTF(("awi_mib: cmd %d inprog",
1491 				    sc->sc_cmd_inprog));
1492 			}
1493 			return error;
1494 		}
1495 	}
1496 	sc->sc_cmd_inprog = cmd;
1497 	if (cmd == AWI_CMD_SET_MIB)
1498 		awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1499 	awi_write_1(sc, AWI_CA_MIB_TYPE, mib);
1500 	awi_write_1(sc, AWI_CA_MIB_SIZE, size);
1501 	awi_write_1(sc, AWI_CA_MIB_INDEX, 0);
1502 	if ((error = awi_cmd(sc, cmd, wflag)) != 0)
1503 		return error;
1504 	if (cmd == AWI_CMD_GET_MIB) {
1505 		awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1506 #ifdef AWI_DEBUG
1507 		if (awi_debug) {
1508 			int i;
1509 
1510 			printf("awi_mib: #%d:", mib);
1511 			for (i = 0; i < size; i++)
1512 				printf(" %02x", ptr[i]);
1513 			printf("\n");
1514 		}
1515 #endif
1516 	}
1517 	return 0;
1518 }
1519 
1520 static int
1521 awi_cmd(struct awi_softc *sc, uint8_t cmd, int wflag)
1522 {
1523 	uint8_t status;
1524 	int error = 0;
1525 #ifdef AWI_DEBUG
1526 	static const char *cmdname[] = {
1527 	    "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX",
1528 	    "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME"
1529 	};
1530 #endif
1531 
1532 #ifdef AWI_DEBUG
1533 	if (awi_debug > 1) {
1534 		if (cmd >= sizeof(cmdname)/sizeof(cmdname[0]))
1535 			printf("awi_cmd: #%d", cmd);
1536 		else
1537 			printf("awi_cmd: %s", cmdname[cmd]);
1538 		printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait");
1539 	}
1540 #endif
1541 	sc->sc_cmd_inprog = cmd;
1542 	awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
1543 	awi_write_1(sc, AWI_CMD, cmd);
1544 	if (wflag == AWI_NOWAIT)
1545 		return EINPROGRESS;
1546 	if ((error = awi_cmd_wait(sc)) != 0)
1547 		return error;
1548 	status = awi_read_1(sc, AWI_CMD_STATUS);
1549 	awi_write_1(sc, AWI_CMD, 0);
1550 	switch (status) {
1551 	case AWI_STAT_OK:
1552 		break;
1553 	case AWI_STAT_BADPARM:
1554 		return EINVAL;
1555 	default:
1556 		printf("%s: command %d failed %x\n",
1557 		    sc->sc_if.if_xname, cmd, status);
1558 		return ENXIO;
1559 	}
1560 	return 0;
1561 }
1562 
1563 static int
1564 awi_cmd_wait(struct awi_softc *sc)
1565 {
1566 	int i, error = 0;
1567 
1568 	i = 0;
1569 	while (sc->sc_cmd_inprog) {
1570 		if (!device_is_active(sc->sc_dev))
1571 			return ENXIO;
1572 		if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
1573 			printf("%s: failed to access hardware\n",
1574 			    sc->sc_if.if_xname);
1575 			config_deactivate(sc->sc_dev);
1576 			return ENXIO;
1577 		}
1578 		if (sc->sc_cansleep) {
1579 			sc->sc_sleep_cnt++;
1580 			error = tsleep(sc, PWAIT, "awicmd",
1581 			    AWI_CMD_TIMEOUT*hz/1000);
1582 			sc->sc_sleep_cnt--;
1583 		} else {
1584 			if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
1585 				awi_cmd_done(sc);
1586 				break;
1587 			}
1588 			if (i++ >= AWI_CMD_TIMEOUT*1000/10)
1589 				error = EWOULDBLOCK;
1590 			else
1591 				DELAY(10);
1592 		}
1593 		if (error)
1594 			break;
1595 	}
1596 	if (error) {
1597 		DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n",
1598 		    sc->sc_cmd_inprog, error));
1599 	}
1600 	return error;
1601 }
1602 
1603 static void
1604 awi_cmd_done(struct awi_softc *sc)
1605 {
1606 	uint8_t cmd, status;
1607 
1608 	status = awi_read_1(sc, AWI_CMD_STATUS);
1609 	if (status == AWI_STAT_IDLE)
1610 		return;		/* stray interrupt */
1611 
1612 	cmd = sc->sc_cmd_inprog;
1613 	sc->sc_cmd_inprog = 0;
1614 	wakeup(sc);
1615 	awi_write_1(sc, AWI_CMD, 0);
1616 
1617 	if (status != AWI_STAT_OK) {
1618 		printf("%s: command %d failed %x\n",
1619 		    sc->sc_if.if_xname, cmd, status);
1620 		sc->sc_substate = AWI_ST_NONE;
1621 		return;
1622 	}
1623 	if (sc->sc_substate != AWI_ST_NONE)
1624 		(void)ieee80211_new_state(&sc->sc_ic, sc->sc_nstate, -1);
1625 }
1626 
1627 static int
1628 awi_next_txd(struct awi_softc *sc, int len, uint32_t *framep, uint32_t *ntxdp)
1629 {
1630 	uint32_t txd, ntxd, frame;
1631 
1632 	txd = sc->sc_txnext;
1633 	frame = txd + AWI_TXD_SIZE;
1634 	if (frame + len > sc->sc_txend)
1635 		frame = sc->sc_txbase;
1636 	ntxd = frame + len;
1637 	if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
1638 		ntxd = sc->sc_txbase;
1639 	*framep = frame;
1640 	*ntxdp = ntxd;
1641 	/*
1642 	 * Determine if there are any room in ring buffer.
1643 	 *		--- send wait,  === new data,  +++ conflict (ENOBUFS)
1644 	 *   base........................end
1645 	 *	   done----txd=====ntxd		OK
1646 	 *	 --txd=====done++++ntxd--	full
1647 	 *	 --txd=====ntxd    done--	OK
1648 	 *	 ==ntxd    done----txd===	OK
1649 	 *	 ==done++++ntxd----txd===	full
1650 	 *	 ++ntxd    txd=====done++	full
1651 	 */
1652 	if (txd < ntxd) {
1653 		if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1654 			return ENOBUFS;
1655 	} else {
1656 		if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1657 			return ENOBUFS;
1658 	}
1659 	return 0;
1660 }
1661 
1662 static int
1663 awi_lock(struct awi_softc *sc)
1664 {
1665 	int error = 0;
1666 
1667 	if (curlwp == NULL) {
1668 		/*
1669 		 * XXX
1670 		 * Though driver ioctl should be called with context,
1671 		 * KAME ipv6 stack calls ioctl in interrupt for now.
1672 		 * We simply abort the request if there are other
1673 		 * ioctl requests in progress.
1674 		 */
1675 		if (sc->sc_busy) {
1676 			if (!device_is_active(sc->sc_dev))
1677 				return ENXIO;
1678 			return EWOULDBLOCK;
1679 		}
1680 		sc->sc_busy = 1;
1681 		sc->sc_cansleep = 0;
1682 		return 0;
1683 	}
1684 	while (sc->sc_busy) {
1685 		if (!device_is_active(sc->sc_dev))
1686 			return ENXIO;
1687 		sc->sc_sleep_cnt++;
1688 		error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
1689 		sc->sc_sleep_cnt--;
1690 		if (error)
1691 			return error;
1692 	}
1693 	sc->sc_busy = 1;
1694 	sc->sc_cansleep = 1;
1695 	return 0;
1696 }
1697 
1698 static void
1699 awi_unlock(struct awi_softc *sc)
1700 {
1701 	sc->sc_busy = 0;
1702 	sc->sc_cansleep = 0;
1703 	if (sc->sc_sleep_cnt)
1704 		wakeup(sc);
1705 }
1706 
1707 static int
1708 awi_intr_lock(struct awi_softc *sc)
1709 {
1710 	uint8_t status;
1711 	int i, retry;
1712 
1713 	status = 1;
1714 	for (retry = 0; retry < 10; retry++) {
1715 		for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
1716 			if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1717 				break;
1718 			DELAY(5);
1719 		}
1720 		if (status != 0)
1721 			break;
1722 		awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
1723 		if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1724 			break;
1725 		awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1726 	}
1727 	if (status != 0) {
1728 		printf("%s: failed to lock interrupt\n",
1729 		    sc->sc_if.if_xname);
1730 		return ENXIO;
1731 	}
1732 	return 0;
1733 }
1734 
1735 static void
1736 awi_intr_unlock(struct awi_softc *sc)
1737 {
1738 
1739 	awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1740 }
1741 
1742 static int
1743 awi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1744 {
1745 	struct ifnet *ifp = ic->ic_ifp;
1746 	struct awi_softc *sc = ifp->if_softc;
1747 	struct ieee80211_node *ni;
1748 	int error;
1749 	uint8_t newmode;
1750 	enum ieee80211_state ostate;
1751 #ifdef AWI_DEBUG
1752 	static const char *stname[] =
1753 	    { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
1754 	static const char *substname[] =
1755 	    { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD",
1756 	      "SUB_INIT", "SUB_SETSS", "SUB_SYNC" };
1757 #endif /* AWI_DEBUG */
1758 
1759 	ostate = ic->ic_state;
1760 	DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate],
1761 	    stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate]));
1762 
1763 	/* Set LED */
1764 	switch (nstate) {
1765 	case IEEE80211_S_INIT:
1766 		awi_drvstate(sc, AWI_DRV_RESET);
1767 		break;
1768 	case IEEE80211_S_SCAN:
1769 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1770 		    ic->ic_opmode == IEEE80211_M_AHDEMO)
1771 			awi_drvstate(sc, AWI_DRV_ADHSC);
1772 		else
1773 			awi_drvstate(sc, AWI_DRV_INFSY);
1774 		break;
1775 	case IEEE80211_S_AUTH:
1776 		awi_drvstate(sc, AWI_DRV_INFSY);
1777 		break;
1778 	case IEEE80211_S_ASSOC:
1779 		awi_drvstate(sc, AWI_DRV_INFAUTH);
1780 		break;
1781 	case IEEE80211_S_RUN:
1782 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
1783 		    ic->ic_opmode == IEEE80211_M_AHDEMO)
1784 			awi_drvstate(sc, AWI_DRV_ADHSY);
1785 		else
1786 			awi_drvstate(sc, AWI_DRV_INFASSOC);
1787 		break;
1788 	}
1789 
1790 	if (nstate == IEEE80211_S_INIT) {
1791 		sc->sc_substate = AWI_ST_NONE;
1792 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
1793 		return (*sc->sc_newstate)(ic, nstate, arg);
1794 	}
1795 
1796 	/* State transition */
1797 	if (nstate == IEEE80211_S_SCAN) {
1798 		/* SCAN substate */
1799 		if (sc->sc_substate == AWI_ST_NONE) {
1800 			sc->sc_nstate = nstate;	/* next state in transition */
1801 			sc->sc_substate = AWI_ST_SCAN_INIT;
1802 		}
1803 		switch (sc->sc_substate) {
1804 		case AWI_ST_SCAN_INIT:
1805 			sc->sc_substate = AWI_ST_SCAN_SETMIB;
1806 			switch (ostate) {
1807 			case IEEE80211_S_RUN:
1808 				/* Beacon miss */
1809 				if (ifp->if_flags & IFF_DEBUG)
1810 					printf("%s: no recent beacons from %s;"
1811 					    " rescanning\n",
1812 					    ifp->if_xname,
1813 					    ether_sprintf(ic->ic_bss->ni_bssid));
1814 				/* FALLTHRU */
1815 			case IEEE80211_S_AUTH:
1816 			case IEEE80211_S_ASSOC:
1817 			case IEEE80211_S_INIT:
1818 				ieee80211_begin_scan(ic, 1);
1819 				/* FALLTHRU */
1820 			case IEEE80211_S_SCAN:
1821 				/* Scan next */
1822 				break;
1823 			}
1824 			if (ic->ic_flags & IEEE80211_F_ASCAN)
1825 				newmode = AWI_SCAN_ACTIVE;
1826 			else
1827 				newmode = AWI_SCAN_PASSIVE;
1828 			if (sc->sc_mib_mgt.aScan_Mode != newmode) {
1829 				sc->sc_mib_mgt.aScan_Mode = newmode;
1830 				if ((error = awi_mib(sc, AWI_CMD_SET_MIB,
1831 				    AWI_MIB_MGT, AWI_NOWAIT)) != 0)
1832 					break;
1833 			}
1834 			/* FALLTHRU */
1835 		case AWI_ST_SCAN_SETMIB:
1836 			sc->sc_substate = AWI_ST_SCAN_SCCMD;
1837 			if (sc->sc_cmd_inprog) {
1838 				if ((error = awi_cmd_wait(sc)) != 0)
1839 					break;
1840 			}
1841 			sc->sc_cmd_inprog = AWI_CMD_SCAN;
1842 			ni = ic->ic_bss;
1843 			awi_write_2(sc, AWI_CA_SCAN_DURATION,
1844 			    (ic->ic_flags & IEEE80211_F_ASCAN) ?
1845 			    AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
1846 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1847 				awi_write_1(sc, AWI_CA_SCAN_SET,
1848 				    IEEE80211_FH_CHANSET(
1849 					ieee80211_chan2ieee(ic, ni->ni_chan)));
1850 				awi_write_1(sc, AWI_CA_SCAN_PATTERN,
1851 				    IEEE80211_FH_CHANPAT(
1852 					ieee80211_chan2ieee(ic, ni->ni_chan)));
1853 				awi_write_1(sc, AWI_CA_SCAN_IDX, 1);
1854 			} else {
1855 				awi_write_1(sc, AWI_CA_SCAN_SET,
1856 				    ieee80211_chan2ieee(ic, ni->ni_chan));
1857 				awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0);
1858 				awi_write_1(sc, AWI_CA_SCAN_IDX, 0);
1859 			}
1860 			awi_write_1(sc, AWI_CA_SCAN_SUSP, 0);
1861 			sc->sc_cur_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1862 			if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT))
1863 			    != 0)
1864 				break;
1865 			/* FALLTHRU */
1866 		case AWI_ST_SCAN_SCCMD:
1867 			ic->ic_state = nstate;
1868 			sc->sc_substate = AWI_ST_NONE;
1869 			error = EINPROGRESS;
1870 			break;
1871 		default:
1872 			DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1873 			    stname[nstate], substname[sc->sc_substate]));
1874 			sc->sc_substate = AWI_ST_NONE;
1875 			error = EIO;
1876 			break;
1877 		}
1878 		goto out;
1879 	}
1880 
1881 	if (ostate == IEEE80211_S_SCAN) {
1882 		/* Set SSID and channel */
1883 		/* substate */
1884 		if (sc->sc_substate == AWI_ST_NONE) {
1885 			sc->sc_nstate = nstate;	/* Next state in transition */
1886 			sc->sc_substate = AWI_ST_SUB_INIT;
1887 		}
1888 		ni = ic->ic_bss;
1889 		switch (sc->sc_substate) {
1890 		case AWI_ST_SUB_INIT:
1891 			sc->sc_substate = AWI_ST_SUB_SETSS;
1892 			IEEE80211_ADDR_COPY(&sc->sc_mib_mgt.aCurrent_BSS_ID,
1893 			    ni->ni_bssid);
1894 			memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0,
1895 			    AWI_ESS_ID_SIZE);
1896 			sc->sc_mib_mgt.aCurrent_ESS_ID[0] =
1897 			    IEEE80211_ELEMID_SSID;
1898 			sc->sc_mib_mgt.aCurrent_ESS_ID[1] = ni->ni_esslen;
1899 			memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2],
1900 			    ni->ni_essid, ni->ni_esslen);
1901 			LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period,
1902 			    ni->ni_intval);
1903 			if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT,
1904 			    AWI_NOWAIT)) != 0)
1905 				break;
1906 			/* FALLTHRU */
1907 		case AWI_ST_SUB_SETSS:
1908 			sc->sc_substate = AWI_ST_SUB_SYNC;
1909 			if (sc->sc_cmd_inprog) {
1910 				if ((error = awi_cmd_wait(sc)) != 0)
1911 					break;
1912 			}
1913 			sc->sc_cmd_inprog = AWI_CMD_SYNC;
1914 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1915 				awi_write_1(sc, AWI_CA_SYNC_SET,
1916 				    IEEE80211_FH_CHANSET(
1917 					ieee80211_chan2ieee(ic, ni->ni_chan)));
1918 				awi_write_1(sc, AWI_CA_SYNC_PATTERN,
1919 				    IEEE80211_FH_CHANPAT(
1920 					ieee80211_chan2ieee(ic, ni->ni_chan)));
1921 				awi_write_1(sc, AWI_CA_SYNC_IDX,
1922 				    ni->ni_fhindex);
1923 				awi_write_2(sc, AWI_CA_SYNC_DWELL,
1924 				    ni->ni_fhdwell);
1925 			} else {
1926 				awi_write_1(sc, AWI_CA_SYNC_SET,
1927 				    ieee80211_chan2ieee(ic, ni->ni_chan));
1928 				awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0);
1929 				awi_write_1(sc, AWI_CA_SYNC_IDX, 0);
1930 				awi_write_2(sc, AWI_CA_SYNC_DWELL, 0);
1931 			}
1932 			if (ic->ic_flags & IEEE80211_F_SIBSS) {
1933 				memset(&ni->ni_tstamp, 0,
1934 				    sizeof(ni->ni_tstamp));
1935 				ni->ni_rstamp = 0;
1936 				awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1);
1937 			} else
1938 				awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0);
1939 			awi_write_2(sc, AWI_CA_SYNC_MBZ, 0);
1940 			awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP,
1941 			    ni->ni_tstamp.data, sizeof(ni->ni_tstamp.data));
1942 			awi_write_4(sc, AWI_CA_SYNC_REFTIME, ni->ni_rstamp);
1943 			sc->sc_cur_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1944 			if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT))
1945 			    != 0)
1946 				break;
1947 			/* FALLTHRU */
1948 		case AWI_ST_SUB_SYNC:
1949 			sc->sc_substate = AWI_ST_NONE;
1950 			if (ic->ic_flags & IEEE80211_F_SIBSS) {
1951 				if ((error = awi_mib(sc, AWI_CMD_GET_MIB,
1952 				    AWI_MIB_MGT, AWI_WAIT)) != 0)
1953 					break;
1954 				IEEE80211_ADDR_COPY(ni->ni_bssid,
1955 				    &sc->sc_mib_mgt.aCurrent_BSS_ID);
1956 			} else {
1957 				if (nstate == IEEE80211_S_RUN) {
1958 					sc->sc_rx_timer = 10;
1959 					ifp->if_timer = 1;
1960 				}
1961 			}
1962 			error = 0;
1963 			break;
1964 		default:
1965 			DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1966 			    stname[nstate], substname[sc->sc_substate]));
1967 			sc->sc_substate = AWI_ST_NONE;
1968 			error = EIO;
1969 			break;
1970 		}
1971 		goto out;
1972 	}
1973 
1974 	sc->sc_substate = AWI_ST_NONE;
1975 
1976 	return (*sc->sc_newstate)(ic, nstate, arg);
1977 out:
1978 	if (error != 0) {
1979 		if (error == EINPROGRESS)
1980 			error = 0;
1981 		return error;
1982 	}
1983 	return (*sc->sc_newstate)(ic, nstate, arg);
1984 }
1985 
1986 static void
1987 awi_recv_mgmt(struct ieee80211com *ic, struct mbuf *m0,
1988 	struct ieee80211_node *ni,
1989 	int subtype, int rssi, uint32_t rstamp)
1990 {
1991 	struct awi_softc *sc = ic->ic_ifp->if_softc;
1992 
1993 	/* probe request is handled by hardware */
1994 	if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
1995 		return;
1996 	(*sc->sc_recv_mgmt)(ic, m0, ni, subtype, rssi, rstamp);
1997 }
1998 
1999 static int
2000 awi_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
2001 	int type, int arg)
2002 {
2003 	struct awi_softc *sc = ic->ic_ifp->if_softc;
2004 
2005 	/* Probe request is handled by hardware */
2006 	if (type == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
2007 		return 0;
2008 	return (*sc->sc_send_mgmt)(ic, ni, type, arg);
2009 }
2010 
2011 static struct mbuf *
2012 awi_ether_encap(struct awi_softc *sc, struct mbuf *m)
2013 {
2014 	struct ieee80211com *ic = &sc->sc_ic;
2015 	struct ieee80211_node *ni = ic->ic_bss;
2016 	struct ether_header *eh;
2017 	struct ieee80211_frame *wh;
2018 
2019 	if (m->m_len < sizeof(struct ether_header)) {
2020 		m = m_pullup(m, sizeof(struct ether_header));
2021 		if (m == NULL)
2022 			return NULL;
2023 	}
2024 	eh = mtod(m, struct ether_header *);
2025 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2026 	if (m == NULL)
2027 		return NULL;
2028 	wh = mtod(m, struct ieee80211_frame *);
2029 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
2030 	*(uint16_t *)wh->i_dur = 0;
2031 	*(uint16_t *)wh->i_seq =
2032 	    htole16(ni->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
2033 	ni->ni_txseqs[0]++;
2034 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2035 	    ic->ic_opmode == IEEE80211_M_AHDEMO) {
2036 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2037 		if (sc->sc_adhoc_ap)
2038 			IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
2039 		else
2040 			IEEE80211_ADDR_COPY(wh->i_addr1, eh->ether_dhost);
2041 		IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
2042 		IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
2043 	} else {
2044 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
2045 		IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
2046 		IEEE80211_ADDR_COPY(wh->i_addr2, eh->ether_shost);
2047 		IEEE80211_ADDR_COPY(wh->i_addr3, eh->ether_dhost);
2048 	}
2049 	return m;
2050 }
2051 
2052 static struct mbuf *
2053 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m)
2054 {
2055 	struct ieee80211com *ic = &sc->sc_ic;
2056 	struct ether_header eh;
2057 	struct ieee80211_frame wh;
2058 	struct llc *llc;
2059 
2060 	if (m->m_len < sizeof(wh) + sizeof(eh)) {
2061 		m = m_pullup(m, sizeof(wh) + sizeof(eh));
2062 		if (m == NULL)
2063 			return NULL;
2064 	}
2065 	memcpy(&wh, mtod(m, void *), sizeof(wh));
2066 	if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA))
2067 		return m;
2068 	memcpy(&eh, mtod(m, char *) + sizeof(wh), sizeof(eh));
2069 	m_adj(m, sizeof(eh) - sizeof(*llc));
2070 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2071 	    ic->ic_opmode == IEEE80211_M_AHDEMO)
2072 		IEEE80211_ADDR_COPY(wh.i_addr2, eh.ether_shost);
2073 	memcpy(mtod(m, void *), &wh, sizeof(wh));
2074 	llc = (struct llc *)(mtod(m, char *) + sizeof(wh));
2075 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
2076 	llc->llc_control = LLC_UI;
2077 	llc->llc_snap.org_code[0] = 0;
2078 	llc->llc_snap.org_code[1] = 0;
2079 	llc->llc_snap.org_code[2] = 0;
2080 	llc->llc_snap.ether_type = eh.ether_type;
2081 	return m;
2082 }
2083