xref: /freebsd/sys/dev/malo/if_malo.c (revision 788ca347b816afd83b2885e0c79aeeb88649b2ab)
1 /*-
2  * Copyright (c) 2008 Weongyo Jeong <weongyo@freebsd.org>
3  * Copyright (c) 2007 Marvell Semiconductor, Inc.
4  * Copyright (c) 2007 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR 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
27  * IN 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
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifdef __FreeBSD__
34 __FBSDID("$FreeBSD$");
35 #endif
36 
37 #include "opt_malo.h"
38 
39 #include <sys/param.h>
40 #include <sys/endian.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/sysctl.h>
45 #include <sys/taskqueue.h>
46 
47 #include <machine/bus.h>
48 #include <sys/bus.h>
49 
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55 #include <net/ethernet.h>
56 
57 #include <net80211/ieee80211_var.h>
58 #include <net80211/ieee80211_regdomain.h>
59 
60 #include <net/bpf.h>
61 
62 #include <dev/malo/if_malo.h>
63 
64 SYSCTL_NODE(_hw, OID_AUTO, malo, CTLFLAG_RD, 0,
65     "Marvell 88w8335 driver parameters");
66 
67 static	int malo_txcoalesce = 8;	/* # tx pkts to q before poking f/w*/
68 SYSCTL_INT(_hw_malo, OID_AUTO, txcoalesce, CTLFLAG_RWTUN, &malo_txcoalesce,
69 	    0, "tx buffers to send at once");
70 static	int malo_rxbuf = MALO_RXBUF;		/* # rx buffers to allocate */
71 SYSCTL_INT(_hw_malo, OID_AUTO, rxbuf, CTLFLAG_RWTUN, &malo_rxbuf,
72 	    0, "rx buffers allocated");
73 static	int malo_rxquota = MALO_RXBUF;		/* # max buffers to process */
74 SYSCTL_INT(_hw_malo, OID_AUTO, rxquota, CTLFLAG_RWTUN, &malo_rxquota,
75 	    0, "max rx buffers to process per interrupt");
76 static	int malo_txbuf = MALO_TXBUF;		/* # tx buffers to allocate */
77 SYSCTL_INT(_hw_malo, OID_AUTO, txbuf, CTLFLAG_RWTUN, &malo_txbuf,
78 	    0, "tx buffers allocated");
79 
80 #ifdef MALO_DEBUG
81 static	int malo_debug = 0;
82 SYSCTL_INT(_hw_malo, OID_AUTO, debug, CTLFLAG_RWTUN, &malo_debug,
83 	    0, "control debugging printfs");
84 enum {
85 	MALO_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
86 	MALO_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
87 	MALO_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
88 	MALO_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
89 	MALO_DEBUG_RESET	= 0x00000010,	/* reset processing */
90 	MALO_DEBUG_INTR		= 0x00000040,	/* ISR */
91 	MALO_DEBUG_TX_PROC	= 0x00000080,	/* tx ISR proc */
92 	MALO_DEBUG_RX_PROC	= 0x00000100,	/* rx ISR proc */
93 	MALO_DEBUG_STATE	= 0x00000400,	/* 802.11 state transitions */
94 	MALO_DEBUG_NODE		= 0x00000800,	/* node management */
95 	MALO_DEBUG_RECV_ALL	= 0x00001000,	/* trace all frames (beacons) */
96 	MALO_DEBUG_FW		= 0x00008000,	/* firmware */
97 	MALO_DEBUG_ANY		= 0xffffffff
98 };
99 #define	IS_BEACON(wh)							\
100 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK |			\
101 		IEEE80211_FC0_SUBTYPE_MASK)) ==				\
102 	 (IEEE80211_FC0_TYPE_MGT|IEEE80211_FC0_SUBTYPE_BEACON))
103 #define	IFF_DUMPPKTS_RECV(sc, wh)					\
104 	(((sc->malo_debug & MALO_DEBUG_RECV) &&				\
105 	  ((sc->malo_debug & MALO_DEBUG_RECV_ALL) || !IS_BEACON(wh))) || \
106 	 (sc->malo_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) ==		\
107 	  (IFF_DEBUG|IFF_LINK2))
108 #define	IFF_DUMPPKTS_XMIT(sc)						\
109 	((sc->malo_debug & MALO_DEBUG_XMIT) ||				\
110 	 (sc->malo_ifp->if_flags & (IFF_DEBUG | IFF_LINK2)) ==		\
111 	     (IFF_DEBUG | IFF_LINK2))
112 #define	DPRINTF(sc, m, fmt, ...) do {				\
113 	if (sc->malo_debug & (m))				\
114 		printf(fmt, __VA_ARGS__);			\
115 } while (0)
116 #else
117 #define	DPRINTF(sc, m, fmt, ...) do {				\
118 	(void) sc;						\
119 } while (0)
120 #endif
121 
122 static MALLOC_DEFINE(M_MALODEV, "malodev", "malo driver dma buffers");
123 
124 static struct ieee80211vap *malo_vap_create(struct ieee80211com *,
125 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
126 		    const uint8_t [IEEE80211_ADDR_LEN],
127 		    const uint8_t [IEEE80211_ADDR_LEN]);
128 static  void	malo_vap_delete(struct ieee80211vap *);
129 static	int	malo_dma_setup(struct malo_softc *);
130 static	int	malo_setup_hwdma(struct malo_softc *);
131 static	void	malo_txq_init(struct malo_softc *, struct malo_txq *, int);
132 static	void	malo_tx_cleanupq(struct malo_softc *, struct malo_txq *);
133 static	void	malo_start(struct ifnet *);
134 static	void	malo_watchdog(void *);
135 static	int	malo_ioctl(struct ifnet *, u_long, caddr_t);
136 static	void	malo_updateslot(struct ifnet *);
137 static	int	malo_newstate(struct ieee80211vap *, enum ieee80211_state, int);
138 static	void	malo_scan_start(struct ieee80211com *);
139 static	void	malo_scan_end(struct ieee80211com *);
140 static	void	malo_set_channel(struct ieee80211com *);
141 static	int	malo_raw_xmit(struct ieee80211_node *, struct mbuf *,
142 		    const struct ieee80211_bpf_params *);
143 static	void	malo_sysctlattach(struct malo_softc *);
144 static	void	malo_announce(struct malo_softc *);
145 static	void	malo_dma_cleanup(struct malo_softc *);
146 static	void	malo_stop_locked(struct ifnet *, int);
147 static	int	malo_chan_set(struct malo_softc *, struct ieee80211_channel *);
148 static	int	malo_mode_init(struct malo_softc *);
149 static	void	malo_tx_proc(void *, int);
150 static	void	malo_rx_proc(void *, int);
151 static	void	malo_init(void *);
152 
153 /*
154  * Read/Write shorthands for accesses to BAR 0.  Note that all BAR 1
155  * operations are done in the "hal" except getting H/W MAC address at
156  * malo_attach and there should be no reference to them here.
157  */
158 static uint32_t
159 malo_bar0_read4(struct malo_softc *sc, bus_size_t off)
160 {
161 	return bus_space_read_4(sc->malo_io0t, sc->malo_io0h, off);
162 }
163 
164 static void
165 malo_bar0_write4(struct malo_softc *sc, bus_size_t off, uint32_t val)
166 {
167 	DPRINTF(sc, MALO_DEBUG_FW, "%s: off 0x%jx val 0x%x\n",
168 	    __func__, (uintmax_t)off, val);
169 
170 	bus_space_write_4(sc->malo_io0t, sc->malo_io0h, off, val);
171 }
172 
173 int
174 malo_attach(uint16_t devid, struct malo_softc *sc)
175 {
176 	int error;
177 	struct ieee80211com *ic;
178 	struct ifnet *ifp;
179 	struct malo_hal *mh;
180 	uint8_t bands;
181 
182 	ifp = sc->malo_ifp = if_alloc(IFT_IEEE80211);
183 	if (ifp == NULL) {
184 		device_printf(sc->malo_dev, "can not if_alloc()\n");
185 		return ENOSPC;
186 	}
187 	ic = ifp->if_l2com;
188 
189 	MALO_LOCK_INIT(sc);
190 	callout_init_mtx(&sc->malo_watchdog_timer, &sc->malo_mtx, 0);
191 
192 	/* set these up early for if_printf use */
193 	if_initname(ifp, device_get_name(sc->malo_dev),
194 	    device_get_unit(sc->malo_dev));
195 
196 	mh = malo_hal_attach(sc->malo_dev, devid,
197 	    sc->malo_io1h, sc->malo_io1t, sc->malo_dmat);
198 	if (mh == NULL) {
199 		if_printf(ifp, "unable to attach HAL\n");
200 		error = EIO;
201 		goto bad;
202 	}
203 	sc->malo_mh = mh;
204 
205 	/*
206 	 * Load firmware so we can get setup.  We arbitrarily pick station
207 	 * firmware; we'll re-load firmware as needed so setting up
208 	 * the wrong mode isn't a big deal.
209 	 */
210 	error = malo_hal_fwload(mh, "malo8335-h", "malo8335-m");
211 	if (error != 0) {
212 		if_printf(ifp, "unable to setup firmware\n");
213 		goto bad1;
214 	}
215 	/* XXX gethwspecs() extracts correct informations?  not maybe!  */
216 	error = malo_hal_gethwspecs(mh, &sc->malo_hwspecs);
217 	if (error != 0) {
218 		if_printf(ifp, "unable to fetch h/w specs\n");
219 		goto bad1;
220 	}
221 
222 	DPRINTF(sc, MALO_DEBUG_FW,
223 	    "malo_hal_gethwspecs: hwversion 0x%x hostif 0x%x"
224 	    "maxnum_wcb 0x%x maxnum_mcaddr 0x%x maxnum_tx_wcb 0x%x"
225 	    "regioncode 0x%x num_antenna 0x%x fw_releasenum 0x%x"
226 	    "wcbbase0 0x%x rxdesc_read 0x%x rxdesc_write 0x%x"
227 	    "ul_fw_awakecookie 0x%x w[4] = %x %x %x %x",
228 	    sc->malo_hwspecs.hwversion,
229 	    sc->malo_hwspecs.hostinterface, sc->malo_hwspecs.maxnum_wcb,
230 	    sc->malo_hwspecs.maxnum_mcaddr, sc->malo_hwspecs.maxnum_tx_wcb,
231 	    sc->malo_hwspecs.regioncode, sc->malo_hwspecs.num_antenna,
232 	    sc->malo_hwspecs.fw_releasenum, sc->malo_hwspecs.wcbbase0,
233 	    sc->malo_hwspecs.rxdesc_read, sc->malo_hwspecs.rxdesc_write,
234 	    sc->malo_hwspecs.ul_fw_awakecookie,
235 	    sc->malo_hwspecs.wcbbase[0], sc->malo_hwspecs.wcbbase[1],
236 	    sc->malo_hwspecs.wcbbase[2], sc->malo_hwspecs.wcbbase[3]);
237 
238 	/* NB: firmware looks that it does not export regdomain info API.  */
239 	bands = 0;
240 	setbit(&bands, IEEE80211_MODE_11B);
241 	setbit(&bands, IEEE80211_MODE_11G);
242 	ieee80211_init_channels(ic, NULL, &bands);
243 
244 	sc->malo_txantenna = 0x2;	/* h/w default */
245 	sc->malo_rxantenna = 0xffff;	/* h/w default */
246 
247 	/*
248 	 * Allocate tx + rx descriptors and populate the lists.
249 	 * We immediately push the information to the firmware
250 	 * as otherwise it gets upset.
251 	 */
252 	error = malo_dma_setup(sc);
253 	if (error != 0) {
254 		if_printf(ifp, "failed to setup descriptors: %d\n", error);
255 		goto bad1;
256 	}
257 	error = malo_setup_hwdma(sc);	/* push to firmware */
258 	if (error != 0)			/* NB: malo_setupdma prints msg */
259 		goto bad2;
260 
261 	sc->malo_tq = taskqueue_create_fast("malo_taskq", M_NOWAIT,
262 		taskqueue_thread_enqueue, &sc->malo_tq);
263 	taskqueue_start_threads(&sc->malo_tq, 1, PI_NET,
264 		"%s taskq", ifp->if_xname);
265 
266 	TASK_INIT(&sc->malo_rxtask, 0, malo_rx_proc, sc);
267 	TASK_INIT(&sc->malo_txtask, 0, malo_tx_proc, sc);
268 
269 	ifp->if_softc = sc;
270 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
271 	ifp->if_start = malo_start;
272 	ifp->if_ioctl = malo_ioctl;
273 	ifp->if_init = malo_init;
274 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
275 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
276 	IFQ_SET_READY(&ifp->if_snd);
277 
278 	ic->ic_ifp = ifp;
279 	/* XXX not right but it's not used anywhere important */
280 	ic->ic_phytype = IEEE80211_T_OFDM;
281 	ic->ic_opmode = IEEE80211_M_STA;
282 	ic->ic_caps =
283 	      IEEE80211_C_STA			/* station mode supported */
284 	    | IEEE80211_C_BGSCAN		/* capable of bg scanning */
285 	    | IEEE80211_C_MONITOR		/* monitor mode */
286 	    | IEEE80211_C_SHPREAMBLE		/* short preamble supported */
287 	    | IEEE80211_C_SHSLOT		/* short slot time supported */
288 	    | IEEE80211_C_TXPMGT		/* capable of txpow mgt */
289 	    | IEEE80211_C_WPA			/* capable of WPA1+WPA2 */
290 	    ;
291 
292 	/*
293 	 * Transmit requires space in the packet for a special format transmit
294 	 * record and optional padding between this record and the payload.
295 	 * Ask the net80211 layer to arrange this when encapsulating
296 	 * packets so we can add it efficiently.
297 	 */
298 	ic->ic_headroom = sizeof(struct malo_txrec) -
299 		sizeof(struct ieee80211_frame);
300 
301 	/* call MI attach routine. */
302 	ieee80211_ifattach(ic, sc->malo_hwspecs.macaddr);
303 	/* override default methods */
304 	ic->ic_vap_create = malo_vap_create;
305 	ic->ic_vap_delete = malo_vap_delete;
306 	ic->ic_raw_xmit = malo_raw_xmit;
307 	ic->ic_updateslot = malo_updateslot;
308 
309 	ic->ic_scan_start = malo_scan_start;
310 	ic->ic_scan_end = malo_scan_end;
311 	ic->ic_set_channel = malo_set_channel;
312 
313 	sc->malo_invalid = 0;		/* ready to go, enable int handling */
314 
315 	ieee80211_radiotap_attach(ic,
316 	    &sc->malo_tx_th.wt_ihdr, sizeof(sc->malo_tx_th),
317 		MALO_TX_RADIOTAP_PRESENT,
318 	    &sc->malo_rx_th.wr_ihdr, sizeof(sc->malo_rx_th),
319 		MALO_RX_RADIOTAP_PRESENT);
320 
321 	/*
322 	 * Setup dynamic sysctl's.
323 	 */
324 	malo_sysctlattach(sc);
325 
326 	if (bootverbose)
327 		ieee80211_announce(ic);
328 	malo_announce(sc);
329 
330 	return 0;
331 bad2:
332 	malo_dma_cleanup(sc);
333 bad1:
334 	malo_hal_detach(mh);
335 bad:
336 	if_free(ifp);
337 	sc->malo_invalid = 1;
338 
339 	return error;
340 }
341 
342 static struct ieee80211vap *
343 malo_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
344     enum ieee80211_opmode opmode, int flags,
345     const uint8_t bssid[IEEE80211_ADDR_LEN],
346     const uint8_t mac[IEEE80211_ADDR_LEN])
347 {
348 	struct ifnet *ifp = ic->ic_ifp;
349 	struct malo_vap *mvp;
350 	struct ieee80211vap *vap;
351 
352 	if (!TAILQ_EMPTY(&ic->ic_vaps)) {
353 		if_printf(ifp, "multiple vaps not supported\n");
354 		return NULL;
355 	}
356 	switch (opmode) {
357 	case IEEE80211_M_STA:
358 		if (opmode == IEEE80211_M_STA)
359 			flags |= IEEE80211_CLONE_NOBEACONS;
360 		/* fall thru... */
361 	case IEEE80211_M_MONITOR:
362 		break;
363 	default:
364 		if_printf(ifp, "%s mode not supported\n",
365 		    ieee80211_opmode_name[opmode]);
366 		return NULL;		/* unsupported */
367 	}
368 	mvp = (struct malo_vap *) malloc(sizeof(struct malo_vap),
369 	    M_80211_VAP, M_NOWAIT | M_ZERO);
370 	if (mvp == NULL) {
371 		if_printf(ifp, "cannot allocate vap state block\n");
372 		return NULL;
373 	}
374 	vap = &mvp->malo_vap;
375 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
376 
377 	/* override state transition machine */
378 	mvp->malo_newstate = vap->iv_newstate;
379 	vap->iv_newstate = malo_newstate;
380 
381 	/* complete setup */
382 	ieee80211_vap_attach(vap,
383 	    ieee80211_media_change, ieee80211_media_status);
384 	ic->ic_opmode = opmode;
385 	return vap;
386 }
387 
388 static void
389 malo_vap_delete(struct ieee80211vap *vap)
390 {
391 	struct malo_vap *mvp = MALO_VAP(vap);
392 
393 	ieee80211_vap_detach(vap);
394 	free(mvp, M_80211_VAP);
395 }
396 
397 int
398 malo_intr(void *arg)
399 {
400 	struct malo_softc *sc = arg;
401 	struct malo_hal *mh = sc->malo_mh;
402 	uint32_t status;
403 
404 	if (sc->malo_invalid) {
405 		/*
406 		 * The hardware is not ready/present, don't touch anything.
407 		 * Note this can happen early on if the IRQ is shared.
408 		 */
409 		DPRINTF(sc, MALO_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
410 		return (FILTER_STRAY);
411 	}
412 
413 	/*
414 	 * Figure out the reason(s) for the interrupt.
415 	 */
416 	malo_hal_getisr(mh, &status);		/* NB: clears ISR too */
417 	if (status == 0)			/* must be a shared irq */
418 		return (FILTER_STRAY);
419 
420 	DPRINTF(sc, MALO_DEBUG_INTR, "%s: status 0x%x imask 0x%x\n",
421 	    __func__, status, sc->malo_imask);
422 
423 	if (status & MALO_A2HRIC_BIT_RX_RDY)
424 		taskqueue_enqueue_fast(sc->malo_tq, &sc->malo_rxtask);
425 	if (status & MALO_A2HRIC_BIT_TX_DONE)
426 		taskqueue_enqueue_fast(sc->malo_tq, &sc->malo_txtask);
427 	if (status & MALO_A2HRIC_BIT_OPC_DONE)
428 		malo_hal_cmddone(mh);
429 	if (status & MALO_A2HRIC_BIT_MAC_EVENT)
430 		;
431 	if (status & MALO_A2HRIC_BIT_RX_PROBLEM)
432 		;
433 	if (status & MALO_A2HRIC_BIT_ICV_ERROR) {
434 		/* TKIP ICV error */
435 		sc->malo_stats.mst_rx_badtkipicv++;
436 	}
437 #ifdef MALO_DEBUG
438 	if (((status | sc->malo_imask) ^ sc->malo_imask) != 0)
439 		DPRINTF(sc, MALO_DEBUG_INTR,
440 		    "%s: can't handle interrupt status 0x%x\n",
441 		    __func__, status);
442 #endif
443 	return (FILTER_HANDLED);
444 }
445 
446 static void
447 malo_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
448 {
449 	bus_addr_t *paddr = (bus_addr_t*) arg;
450 
451 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
452 
453 	*paddr = segs->ds_addr;
454 }
455 
456 static int
457 malo_desc_setup(struct malo_softc *sc, const char *name,
458     struct malo_descdma *dd,
459     int nbuf, size_t bufsize, int ndesc, size_t descsize)
460 {
461 	int error;
462 	struct ifnet *ifp = sc->malo_ifp;
463 	uint8_t *ds;
464 
465 	DPRINTF(sc, MALO_DEBUG_RESET,
466 	    "%s: %s DMA: %u bufs (%ju) %u desc/buf (%ju)\n",
467 	    __func__, name, nbuf, (uintmax_t) bufsize,
468 	    ndesc, (uintmax_t) descsize);
469 
470 	dd->dd_name = name;
471 	dd->dd_desc_len = nbuf * ndesc * descsize;
472 
473 	/*
474 	 * Setup DMA descriptor area.
475 	 */
476 	error = bus_dma_tag_create(bus_get_dma_tag(sc->malo_dev),/* parent */
477 		       PAGE_SIZE, 0,		/* alignment, bounds */
478 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
479 		       BUS_SPACE_MAXADDR,	/* highaddr */
480 		       NULL, NULL,		/* filter, filterarg */
481 		       dd->dd_desc_len,		/* maxsize */
482 		       1,			/* nsegments */
483 		       dd->dd_desc_len,		/* maxsegsize */
484 		       BUS_DMA_ALLOCNOW,	/* flags */
485 		       NULL,			/* lockfunc */
486 		       NULL,			/* lockarg */
487 		       &dd->dd_dmat);
488 	if (error != 0) {
489 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
490 		return error;
491 	}
492 
493 	/* allocate descriptors */
494 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
495 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dd->dd_dmamap);
496 	if (error != 0) {
497 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
498 		    "error %u\n", nbuf * ndesc, dd->dd_name, error);
499 		goto fail1;
500 	}
501 
502 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
503 	    dd->dd_desc, dd->dd_desc_len,
504 	    malo_load_cb, &dd->dd_desc_paddr, BUS_DMA_NOWAIT);
505 	if (error != 0) {
506 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
507 		    dd->dd_name, error);
508 		goto fail2;
509 	}
510 
511 	ds = dd->dd_desc;
512 	memset(ds, 0, dd->dd_desc_len);
513 	DPRINTF(sc, MALO_DEBUG_RESET,
514 	    "%s: %s DMA map: %p (%lu) -> 0x%jx (%lu)\n",
515 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
516 	    (uintmax_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
517 
518 	return 0;
519 fail2:
520 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
521 fail1:
522 	bus_dma_tag_destroy(dd->dd_dmat);
523 	memset(dd, 0, sizeof(*dd));
524 	return error;
525 }
526 
527 #define	DS2PHYS(_dd, _ds) \
528 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
529 
530 static int
531 malo_rxdma_setup(struct malo_softc *sc)
532 {
533 	struct ifnet *ifp = sc->malo_ifp;
534 	int error, bsize, i;
535 	struct malo_rxbuf *bf;
536 	struct malo_rxdesc *ds;
537 
538 	error = malo_desc_setup(sc, "rx", &sc->malo_rxdma,
539 	    malo_rxbuf, sizeof(struct malo_rxbuf),
540 	    1, sizeof(struct malo_rxdesc));
541 	if (error != 0)
542 		return error;
543 
544 	/*
545 	 * Allocate rx buffers and set them up.
546 	 */
547 	bsize = malo_rxbuf * sizeof(struct malo_rxbuf);
548 	bf = malloc(bsize, M_MALODEV, M_NOWAIT | M_ZERO);
549 	if (bf == NULL) {
550 		if_printf(ifp, "malloc of %u rx buffers failed\n", bsize);
551 		return error;
552 	}
553 	sc->malo_rxdma.dd_bufptr = bf;
554 
555 	STAILQ_INIT(&sc->malo_rxbuf);
556 	ds = sc->malo_rxdma.dd_desc;
557 	for (i = 0; i < malo_rxbuf; i++, bf++, ds++) {
558 		bf->bf_desc = ds;
559 		bf->bf_daddr = DS2PHYS(&sc->malo_rxdma, ds);
560 		error = bus_dmamap_create(sc->malo_dmat, BUS_DMA_NOWAIT,
561 		    &bf->bf_dmamap);
562 		if (error != 0) {
563 			if_printf(ifp, "%s: unable to dmamap for rx buffer, "
564 			    "error %d\n", __func__, error);
565 			return error;
566 		}
567 		/* NB: tail is intentional to preserve descriptor order */
568 		STAILQ_INSERT_TAIL(&sc->malo_rxbuf, bf, bf_list);
569 	}
570 	return 0;
571 }
572 
573 static int
574 malo_txdma_setup(struct malo_softc *sc, struct malo_txq *txq)
575 {
576 	struct ifnet *ifp = sc->malo_ifp;
577 	int error, bsize, i;
578 	struct malo_txbuf *bf;
579 	struct malo_txdesc *ds;
580 
581 	error = malo_desc_setup(sc, "tx", &txq->dma,
582 	    malo_txbuf, sizeof(struct malo_txbuf),
583 	    MALO_TXDESC, sizeof(struct malo_txdesc));
584 	if (error != 0)
585 		return error;
586 
587 	/* allocate and setup tx buffers */
588 	bsize = malo_txbuf * sizeof(struct malo_txbuf);
589 	bf = malloc(bsize, M_MALODEV, M_NOWAIT | M_ZERO);
590 	if (bf == NULL) {
591 		if_printf(ifp, "malloc of %u tx buffers failed\n",
592 		    malo_txbuf);
593 		return ENOMEM;
594 	}
595 	txq->dma.dd_bufptr = bf;
596 
597 	STAILQ_INIT(&txq->free);
598 	txq->nfree = 0;
599 	ds = txq->dma.dd_desc;
600 	for (i = 0; i < malo_txbuf; i++, bf++, ds += MALO_TXDESC) {
601 		bf->bf_desc = ds;
602 		bf->bf_daddr = DS2PHYS(&txq->dma, ds);
603 		error = bus_dmamap_create(sc->malo_dmat, BUS_DMA_NOWAIT,
604 		    &bf->bf_dmamap);
605 		if (error != 0) {
606 			if_printf(ifp, "unable to create dmamap for tx "
607 			    "buffer %u, error %u\n", i, error);
608 			return error;
609 		}
610 		STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
611 		txq->nfree++;
612 	}
613 
614 	return 0;
615 }
616 
617 static void
618 malo_desc_cleanup(struct malo_softc *sc, struct malo_descdma *dd)
619 {
620 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
621 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
622 	bus_dma_tag_destroy(dd->dd_dmat);
623 
624 	memset(dd, 0, sizeof(*dd));
625 }
626 
627 static void
628 malo_rxdma_cleanup(struct malo_softc *sc)
629 {
630 	struct malo_rxbuf *bf;
631 
632 	STAILQ_FOREACH(bf, &sc->malo_rxbuf, bf_list) {
633 		if (bf->bf_m != NULL) {
634 			m_freem(bf->bf_m);
635 			bf->bf_m = NULL;
636 		}
637 		if (bf->bf_dmamap != NULL) {
638 			bus_dmamap_destroy(sc->malo_dmat, bf->bf_dmamap);
639 			bf->bf_dmamap = NULL;
640 		}
641 	}
642 	STAILQ_INIT(&sc->malo_rxbuf);
643 	if (sc->malo_rxdma.dd_bufptr != NULL) {
644 		free(sc->malo_rxdma.dd_bufptr, M_MALODEV);
645 		sc->malo_rxdma.dd_bufptr = NULL;
646 	}
647 	if (sc->malo_rxdma.dd_desc_len != 0)
648 		malo_desc_cleanup(sc, &sc->malo_rxdma);
649 }
650 
651 static void
652 malo_txdma_cleanup(struct malo_softc *sc, struct malo_txq *txq)
653 {
654 	struct malo_txbuf *bf;
655 	struct ieee80211_node *ni;
656 
657 	STAILQ_FOREACH(bf, &txq->free, bf_list) {
658 		if (bf->bf_m != NULL) {
659 			m_freem(bf->bf_m);
660 			bf->bf_m = NULL;
661 		}
662 		ni = bf->bf_node;
663 		bf->bf_node = NULL;
664 		if (ni != NULL) {
665 			/*
666 			 * Reclaim node reference.
667 			 */
668 			ieee80211_free_node(ni);
669 		}
670 		if (bf->bf_dmamap != NULL) {
671 			bus_dmamap_destroy(sc->malo_dmat, bf->bf_dmamap);
672 			bf->bf_dmamap = NULL;
673 		}
674 	}
675 	STAILQ_INIT(&txq->free);
676 	txq->nfree = 0;
677 	if (txq->dma.dd_bufptr != NULL) {
678 		free(txq->dma.dd_bufptr, M_MALODEV);
679 		txq->dma.dd_bufptr = NULL;
680 	}
681 	if (txq->dma.dd_desc_len != 0)
682 		malo_desc_cleanup(sc, &txq->dma);
683 }
684 
685 static void
686 malo_dma_cleanup(struct malo_softc *sc)
687 {
688 	int i;
689 
690 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
691 		malo_txdma_cleanup(sc, &sc->malo_txq[i]);
692 
693 	malo_rxdma_cleanup(sc);
694 }
695 
696 static int
697 malo_dma_setup(struct malo_softc *sc)
698 {
699 	int error, i;
700 
701 	/* rxdma initializing.  */
702 	error = malo_rxdma_setup(sc);
703 	if (error != 0)
704 		return error;
705 
706 	/* NB: we just have 1 tx queue now.  */
707 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
708 		error = malo_txdma_setup(sc, &sc->malo_txq[i]);
709 		if (error != 0) {
710 			malo_dma_cleanup(sc);
711 
712 			return error;
713 		}
714 
715 		malo_txq_init(sc, &sc->malo_txq[i], i);
716 	}
717 
718 	return 0;
719 }
720 
721 static void
722 malo_hal_set_rxtxdma(struct malo_softc *sc)
723 {
724 	int i;
725 
726 	malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_read,
727 	    sc->malo_hwdma.rxdesc_read);
728 	malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_write,
729 	    sc->malo_hwdma.rxdesc_read);
730 
731 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
732 		malo_bar0_write4(sc,
733 		    sc->malo_hwspecs.wcbbase[i], sc->malo_hwdma.wcbbase[i]);
734 	}
735 }
736 
737 /*
738  * Inform firmware of our tx/rx dma setup.  The BAR 0 writes below are
739  * for compatibility with older firmware.  For current firmware we send
740  * this information with a cmd block via malo_hal_sethwdma.
741  */
742 static int
743 malo_setup_hwdma(struct malo_softc *sc)
744 {
745 	int i;
746 	struct malo_txq *txq;
747 
748 	sc->malo_hwdma.rxdesc_read = sc->malo_rxdma.dd_desc_paddr;
749 
750 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
751 		txq = &sc->malo_txq[i];
752 		sc->malo_hwdma.wcbbase[i] = txq->dma.dd_desc_paddr;
753 	}
754 	sc->malo_hwdma.maxnum_txwcb = malo_txbuf;
755 	sc->malo_hwdma.maxnum_wcb = MALO_NUM_TX_QUEUES;
756 
757 	malo_hal_set_rxtxdma(sc);
758 
759 	return 0;
760 }
761 
762 static void
763 malo_txq_init(struct malo_softc *sc, struct malo_txq *txq, int qnum)
764 {
765 	struct malo_txbuf *bf, *bn;
766 	struct malo_txdesc *ds;
767 
768 	MALO_TXQ_LOCK_INIT(sc, txq);
769 	txq->qnum = qnum;
770 	txq->txpri = 0;	/* XXX */
771 
772 	STAILQ_FOREACH(bf, &txq->free, bf_list) {
773 		bf->bf_txq = txq;
774 
775 		ds = bf->bf_desc;
776 		bn = STAILQ_NEXT(bf, bf_list);
777 		if (bn == NULL)
778 			bn = STAILQ_FIRST(&txq->free);
779 		ds->physnext = htole32(bn->bf_daddr);
780 	}
781 	STAILQ_INIT(&txq->active);
782 }
783 
784 /*
785  * Reclaim resources for a setup queue.
786  */
787 static void
788 malo_tx_cleanupq(struct malo_softc *sc, struct malo_txq *txq)
789 {
790 	/* XXX hal work? */
791 	MALO_TXQ_LOCK_DESTROY(txq);
792 }
793 
794 /*
795  * Allocate a tx buffer for sending a frame.
796  */
797 static struct malo_txbuf *
798 malo_getbuf(struct malo_softc *sc, struct malo_txq *txq)
799 {
800 	struct malo_txbuf *bf;
801 
802 	MALO_TXQ_LOCK(txq);
803 	bf = STAILQ_FIRST(&txq->free);
804 	if (bf != NULL) {
805 		STAILQ_REMOVE_HEAD(&txq->free, bf_list);
806 		txq->nfree--;
807 	}
808 	MALO_TXQ_UNLOCK(txq);
809 	if (bf == NULL) {
810 		DPRINTF(sc, MALO_DEBUG_XMIT,
811 		    "%s: out of xmit buffers on q %d\n", __func__, txq->qnum);
812 		sc->malo_stats.mst_tx_qstop++;
813 	}
814 	return bf;
815 }
816 
817 static int
818 malo_tx_dmasetup(struct malo_softc *sc, struct malo_txbuf *bf, struct mbuf *m0)
819 {
820 	struct mbuf *m;
821 	int error;
822 
823 	/*
824 	 * Load the DMA map so any coalescing is done.  This also calculates
825 	 * the number of descriptors we need.
826 	 */
827 	error = bus_dmamap_load_mbuf_sg(sc->malo_dmat, bf->bf_dmamap, m0,
828 				     bf->bf_segs, &bf->bf_nseg,
829 				     BUS_DMA_NOWAIT);
830 	if (error == EFBIG) {
831 		/* XXX packet requires too many descriptors */
832 		bf->bf_nseg = MALO_TXDESC + 1;
833 	} else if (error != 0) {
834 		sc->malo_stats.mst_tx_busdma++;
835 		m_freem(m0);
836 		return error;
837 	}
838 	/*
839 	 * Discard null packets and check for packets that require too many
840 	 * TX descriptors.  We try to convert the latter to a cluster.
841 	 */
842 	if (error == EFBIG) {		/* too many desc's, linearize */
843 		sc->malo_stats.mst_tx_linear++;
844 		m = m_defrag(m0, M_NOWAIT);
845 		if (m == NULL) {
846 			m_freem(m0);
847 			sc->malo_stats.mst_tx_nombuf++;
848 			return ENOMEM;
849 		}
850 		m0 = m;
851 		error = bus_dmamap_load_mbuf_sg(sc->malo_dmat, bf->bf_dmamap, m0,
852 					     bf->bf_segs, &bf->bf_nseg,
853 					     BUS_DMA_NOWAIT);
854 		if (error != 0) {
855 			sc->malo_stats.mst_tx_busdma++;
856 			m_freem(m0);
857 			return error;
858 		}
859 		KASSERT(bf->bf_nseg <= MALO_TXDESC,
860 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
861 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
862 		sc->malo_stats.mst_tx_nodata++;
863 		m_freem(m0);
864 		return EIO;
865 	}
866 	DPRINTF(sc, MALO_DEBUG_XMIT, "%s: m %p len %u\n",
867 		__func__, m0, m0->m_pkthdr.len);
868 	bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
869 	bf->bf_m = m0;
870 
871 	return 0;
872 }
873 
874 #ifdef MALO_DEBUG
875 static void
876 malo_printrxbuf(const struct malo_rxbuf *bf, u_int ix)
877 {
878 	const struct malo_rxdesc *ds = bf->bf_desc;
879 	uint32_t status = le32toh(ds->status);
880 
881 	printf("R[%2u] (DS.V:%p DS.P:0x%jx) NEXT:%08x DATA:%08x RC:%02x%s\n"
882 	    "      STAT:%02x LEN:%04x SNR:%02x NF:%02x CHAN:%02x"
883 	    " RATE:%02x QOS:%04x\n", ix, ds, (uintmax_t)bf->bf_daddr,
884 	    le32toh(ds->physnext), le32toh(ds->physbuffdata),
885 	    ds->rxcontrol,
886 	    ds->rxcontrol != MALO_RXD_CTRL_DRIVER_OWN ?
887 	        "" : (status & MALO_RXD_STATUS_OK) ? " *" : " !",
888 	    ds->status, le16toh(ds->pktlen), ds->snr, ds->nf, ds->channel,
889 	    ds->rate, le16toh(ds->qosctrl));
890 }
891 
892 static void
893 malo_printtxbuf(const struct malo_txbuf *bf, u_int qnum, u_int ix)
894 {
895 	const struct malo_txdesc *ds = bf->bf_desc;
896 	uint32_t status = le32toh(ds->status);
897 
898 	printf("Q%u[%3u]", qnum, ix);
899 	printf(" (DS.V:%p DS.P:0x%jx)\n", ds, (uintmax_t)bf->bf_daddr);
900 	printf("    NEXT:%08x DATA:%08x LEN:%04x STAT:%08x%s\n",
901 	    le32toh(ds->physnext),
902 	    le32toh(ds->pktptr), le16toh(ds->pktlen), status,
903 	    status & MALO_TXD_STATUS_USED ?
904 	    "" : (status & 3) != 0 ? " *" : " !");
905 	printf("    RATE:%02x PRI:%x QOS:%04x SAP:%08x FORMAT:%04x\n",
906 	    ds->datarate, ds->txpriority, le16toh(ds->qosctrl),
907 	    le32toh(ds->sap_pktinfo), le16toh(ds->format));
908 #if 0
909 	{
910 		const uint8_t *cp = (const uint8_t *) ds;
911 		int i;
912 		for (i = 0; i < sizeof(struct malo_txdesc); i++) {
913 			printf("%02x ", cp[i]);
914 			if (((i+1) % 16) == 0)
915 				printf("\n");
916 		}
917 		printf("\n");
918 	}
919 #endif
920 }
921 #endif /* MALO_DEBUG */
922 
923 static __inline void
924 malo_updatetxrate(struct ieee80211_node *ni, int rix)
925 {
926 #define	N(x)	(sizeof(x)/sizeof(x[0]))
927 	static const int ieeerates[] =
928 	    { 2, 4, 11, 22, 44, 12, 18, 24, 36, 48, 96, 108 };
929 	if (rix < N(ieeerates))
930 		ni->ni_txrate = ieeerates[rix];
931 #undef N
932 }
933 
934 static int
935 malo_fix2rate(int fix_rate)
936 {
937 #define	N(x)	(sizeof(x)/sizeof(x[0]))
938 	static const int rates[] =
939 	    { 2, 4, 11, 22, 12, 18, 24, 36, 48, 96, 108 };
940 	return (fix_rate < N(rates) ? rates[fix_rate] : 0);
941 #undef N
942 }
943 
944 /* idiomatic shorthands: MS = mask+shift, SM = shift+mask */
945 #define	MS(v,x)			(((v) & x) >> x##_S)
946 #define	SM(v,x)			(((v) << x##_S) & x)
947 
948 /*
949  * Process completed xmit descriptors from the specified queue.
950  */
951 static int
952 malo_tx_processq(struct malo_softc *sc, struct malo_txq *txq)
953 {
954 	struct malo_txbuf *bf;
955 	struct malo_txdesc *ds;
956 	struct ieee80211_node *ni;
957 	int nreaped;
958 	uint32_t status;
959 
960 	DPRINTF(sc, MALO_DEBUG_TX_PROC, "%s: tx queue %u\n",
961 	    __func__, txq->qnum);
962 	for (nreaped = 0;; nreaped++) {
963 		MALO_TXQ_LOCK(txq);
964 		bf = STAILQ_FIRST(&txq->active);
965 		if (bf == NULL) {
966 			MALO_TXQ_UNLOCK(txq);
967 			break;
968 		}
969 		ds = bf->bf_desc;
970 		MALO_TXDESC_SYNC(txq, ds,
971 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
972 		if (ds->status & htole32(MALO_TXD_STATUS_FW_OWNED)) {
973 			MALO_TXQ_UNLOCK(txq);
974 			break;
975 		}
976 		STAILQ_REMOVE_HEAD(&txq->active, bf_list);
977 		MALO_TXQ_UNLOCK(txq);
978 
979 #ifdef MALO_DEBUG
980 		if (sc->malo_debug & MALO_DEBUG_XMIT_DESC)
981 			malo_printtxbuf(bf, txq->qnum, nreaped);
982 #endif
983 		ni = bf->bf_node;
984 		if (ni != NULL) {
985 			status = le32toh(ds->status);
986 			if (status & MALO_TXD_STATUS_OK) {
987 				uint16_t format = le16toh(ds->format);
988 				uint8_t txant = MS(format, MALO_TXD_ANTENNA);
989 
990 				sc->malo_stats.mst_ant_tx[txant]++;
991 				if (status & MALO_TXD_STATUS_OK_RETRY)
992 					sc->malo_stats.mst_tx_retries++;
993 				if (status & MALO_TXD_STATUS_OK_MORE_RETRY)
994 					sc->malo_stats.mst_tx_mretries++;
995 				malo_updatetxrate(ni, ds->datarate);
996 				sc->malo_stats.mst_tx_rate = ds->datarate;
997 			} else {
998 				if (status & MALO_TXD_STATUS_FAILED_LINK_ERROR)
999 					sc->malo_stats.mst_tx_linkerror++;
1000 				if (status & MALO_TXD_STATUS_FAILED_XRETRY)
1001 					sc->malo_stats.mst_tx_xretries++;
1002 				if (status & MALO_TXD_STATUS_FAILED_AGING)
1003 					sc->malo_stats.mst_tx_aging++;
1004 			}
1005 			/*
1006 			 * Do any tx complete callback.  Note this must
1007 			 * be done before releasing the node reference.
1008 			 * XXX no way to figure out if frame was ACK'd
1009 			 */
1010 			if (bf->bf_m->m_flags & M_TXCB) {
1011 				/* XXX strip fw len in case header inspected */
1012 				m_adj(bf->bf_m, sizeof(uint16_t));
1013 				ieee80211_process_callback(ni, bf->bf_m,
1014 					(status & MALO_TXD_STATUS_OK) == 0);
1015 			}
1016 			/*
1017 			 * Reclaim reference to node.
1018 			 *
1019 			 * NB: the node may be reclaimed here if, for example
1020 			 *     this is a DEAUTH message that was sent and the
1021 			 *     node was timed out due to inactivity.
1022 			 */
1023 			ieee80211_free_node(ni);
1024 		}
1025 		ds->status = htole32(MALO_TXD_STATUS_IDLE);
1026 		ds->pktlen = htole32(0);
1027 
1028 		bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap,
1029 		    BUS_DMASYNC_POSTWRITE);
1030 		bus_dmamap_unload(sc->malo_dmat, bf->bf_dmamap);
1031 		m_freem(bf->bf_m);
1032 		bf->bf_m = NULL;
1033 		bf->bf_node = NULL;
1034 
1035 		MALO_TXQ_LOCK(txq);
1036 		STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
1037 		txq->nfree++;
1038 		MALO_TXQ_UNLOCK(txq);
1039 	}
1040 	return nreaped;
1041 }
1042 
1043 /*
1044  * Deferred processing of transmit interrupt.
1045  */
1046 static void
1047 malo_tx_proc(void *arg, int npending)
1048 {
1049 	struct malo_softc *sc = arg;
1050 	struct ifnet *ifp = sc->malo_ifp;
1051 	int i, nreaped;
1052 
1053 	/*
1054 	 * Process each active queue.
1055 	 */
1056 	nreaped = 0;
1057 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
1058 		if (!STAILQ_EMPTY(&sc->malo_txq[i].active))
1059 			nreaped += malo_tx_processq(sc, &sc->malo_txq[i]);
1060 	}
1061 
1062 	if (nreaped != 0) {
1063 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1064 		sc->malo_timer = 0;
1065 		malo_start(ifp);
1066 	}
1067 }
1068 
1069 static int
1070 malo_tx_start(struct malo_softc *sc, struct ieee80211_node *ni,
1071     struct malo_txbuf *bf, struct mbuf *m0)
1072 {
1073 #define	IEEE80211_DIR_DSTODS(wh) \
1074 	((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
1075 #define	IS_DATA_FRAME(wh)						\
1076 	((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK)) == IEEE80211_FC0_TYPE_DATA)
1077 	int error, ismcast, iswep;
1078 	int copyhdrlen, hdrlen, pktlen;
1079 	struct ieee80211_frame *wh;
1080 	struct ifnet *ifp = sc->malo_ifp;
1081 	struct ieee80211com *ic = ifp->if_l2com;
1082 	struct ieee80211vap *vap = ni->ni_vap;
1083 	struct malo_txdesc *ds;
1084 	struct malo_txrec *tr;
1085 	struct malo_txq *txq;
1086 	uint16_t qos;
1087 
1088 	wh = mtod(m0, struct ieee80211_frame *);
1089 	iswep = wh->i_fc[1] & IEEE80211_FC1_PROTECTED;
1090 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1091 	copyhdrlen = hdrlen = ieee80211_anyhdrsize(wh);
1092 	pktlen = m0->m_pkthdr.len;
1093 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
1094 		if (IEEE80211_DIR_DSTODS(wh)) {
1095 			qos = *(uint16_t *)
1096 			    (((struct ieee80211_qosframe_addr4 *) wh)->i_qos);
1097 			copyhdrlen -= sizeof(qos);
1098 		} else
1099 			qos = *(uint16_t *)
1100 			    (((struct ieee80211_qosframe *) wh)->i_qos);
1101 	} else
1102 		qos = 0;
1103 
1104 	if (iswep) {
1105 		struct ieee80211_key *k;
1106 
1107 		/*
1108 		 * Construct the 802.11 header+trailer for an encrypted
1109 		 * frame. The only reason this can fail is because of an
1110 		 * unknown or unsupported cipher/key type.
1111 		 *
1112 		 * NB: we do this even though the firmware will ignore
1113 		 *     what we've done for WEP and TKIP as we need the
1114 		 *     ExtIV filled in for CCMP and this also adjusts
1115 		 *     the headers which simplifies our work below.
1116 		 */
1117 		k = ieee80211_crypto_encap(ni, m0);
1118 		if (k == NULL) {
1119 			/*
1120 			 * This can happen when the key is yanked after the
1121 			 * frame was queued.  Just discard the frame; the
1122 			 * 802.11 layer counts failures and provides
1123 			 * debugging/diagnostics.
1124 			 */
1125 			m_freem(m0);
1126 			return EIO;
1127 		}
1128 
1129 		/*
1130 		 * Adjust the packet length for the crypto additions
1131 		 * done during encap and any other bits that the f/w
1132 		 * will add later on.
1133 		 */
1134 		pktlen = m0->m_pkthdr.len;
1135 
1136 		/* packet header may have moved, reset our local pointer */
1137 		wh = mtod(m0, struct ieee80211_frame *);
1138 	}
1139 
1140 	if (ieee80211_radiotap_active_vap(vap)) {
1141 		sc->malo_tx_th.wt_flags = 0;	/* XXX */
1142 		if (iswep)
1143 			sc->malo_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1144 		sc->malo_tx_th.wt_txpower = ni->ni_txpower;
1145 		sc->malo_tx_th.wt_antenna = sc->malo_txantenna;
1146 
1147 		ieee80211_radiotap_tx(vap, m0);
1148 	}
1149 
1150 	/*
1151 	 * Copy up/down the 802.11 header; the firmware requires
1152 	 * we present a 2-byte payload length followed by a
1153 	 * 4-address header (w/o QoS), followed (optionally) by
1154 	 * any WEP/ExtIV header (but only filled in for CCMP).
1155 	 * We are assured the mbuf has sufficient headroom to
1156 	 * prepend in-place by the setup of ic_headroom in
1157 	 * malo_attach.
1158 	 */
1159 	if (hdrlen < sizeof(struct malo_txrec)) {
1160 		const int space = sizeof(struct malo_txrec) - hdrlen;
1161 		if (M_LEADINGSPACE(m0) < space) {
1162 			/* NB: should never happen */
1163 			device_printf(sc->malo_dev,
1164 			    "not enough headroom, need %d found %zd, "
1165 			    "m_flags 0x%x m_len %d\n",
1166 			    space, M_LEADINGSPACE(m0), m0->m_flags, m0->m_len);
1167 			ieee80211_dump_pkt(ic,
1168 			    mtod(m0, const uint8_t *), m0->m_len, 0, -1);
1169 			m_freem(m0);
1170 			/* XXX stat */
1171 			return EIO;
1172 		}
1173 		M_PREPEND(m0, space, M_NOWAIT);
1174 	}
1175 	tr = mtod(m0, struct malo_txrec *);
1176 	if (wh != (struct ieee80211_frame *) &tr->wh)
1177 		ovbcopy(wh, &tr->wh, hdrlen);
1178 	/*
1179 	 * Note: the "firmware length" is actually the length of the fully
1180 	 * formed "802.11 payload".  That is, it's everything except for
1181 	 * the 802.11 header.  In particular this includes all crypto
1182 	 * material including the MIC!
1183 	 */
1184 	tr->fwlen = htole16(pktlen - hdrlen);
1185 
1186 	/*
1187 	 * Load the DMA map so any coalescing is done.  This
1188 	 * also calculates the number of descriptors we need.
1189 	 */
1190 	error = malo_tx_dmasetup(sc, bf, m0);
1191 	if (error != 0)
1192 		return error;
1193 	bf->bf_node = ni;			/* NB: held reference */
1194 	m0 = bf->bf_m;				/* NB: may have changed */
1195 	tr = mtod(m0, struct malo_txrec *);
1196 	wh = (struct ieee80211_frame *)&tr->wh;
1197 
1198 	/*
1199 	 * Formulate tx descriptor.
1200 	 */
1201 	ds = bf->bf_desc;
1202 	txq = bf->bf_txq;
1203 
1204 	ds->qosctrl = qos;			/* NB: already little-endian */
1205 	ds->pktptr = htole32(bf->bf_segs[0].ds_addr);
1206 	ds->pktlen = htole16(bf->bf_segs[0].ds_len);
1207 	/* NB: pPhysNext setup once, don't touch */
1208 	ds->datarate = IS_DATA_FRAME(wh) ? 1 : 0;
1209 	ds->sap_pktinfo = 0;
1210 	ds->format = 0;
1211 
1212 	/*
1213 	 * Select transmit rate.
1214 	 */
1215 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1216 	case IEEE80211_FC0_TYPE_MGT:
1217 		sc->malo_stats.mst_tx_mgmt++;
1218 		/* fall thru... */
1219 	case IEEE80211_FC0_TYPE_CTL:
1220 		ds->txpriority = 1;
1221 		break;
1222 	case IEEE80211_FC0_TYPE_DATA:
1223 		ds->txpriority = txq->qnum;
1224 		break;
1225 	default:
1226 		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1227 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1228 		/* XXX statistic */
1229 		m_freem(m0);
1230 		return EIO;
1231 	}
1232 
1233 #ifdef MALO_DEBUG
1234 	if (IFF_DUMPPKTS_XMIT(sc))
1235 		ieee80211_dump_pkt(ic,
1236 		    mtod(m0, const uint8_t *)+sizeof(uint16_t),
1237 		    m0->m_len - sizeof(uint16_t), ds->datarate, -1);
1238 #endif
1239 
1240 	MALO_TXQ_LOCK(txq);
1241 	if (!IS_DATA_FRAME(wh))
1242 		ds->status |= htole32(1);
1243 	ds->status |= htole32(MALO_TXD_STATUS_FW_OWNED);
1244 	STAILQ_INSERT_TAIL(&txq->active, bf, bf_list);
1245 	MALO_TXDESC_SYNC(txq, ds, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1246 
1247 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1248 	sc->malo_timer = 5;
1249 	MALO_TXQ_UNLOCK(txq);
1250 	return 0;
1251 #undef IEEE80211_DIR_DSTODS
1252 }
1253 
1254 static void
1255 malo_start(struct ifnet *ifp)
1256 {
1257 	struct malo_softc *sc = ifp->if_softc;
1258 	struct ieee80211_node *ni;
1259 	struct malo_txq *txq = &sc->malo_txq[0];
1260 	struct malo_txbuf *bf = NULL;
1261 	struct mbuf *m;
1262 	int nqueued = 0;
1263 
1264 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->malo_invalid)
1265 		return;
1266 
1267 	for (;;) {
1268 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1269 		if (m == NULL)
1270 			break;
1271 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1272 		bf = malo_getbuf(sc, txq);
1273 		if (bf == NULL) {
1274 			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1275 
1276 			/* XXX blocks other traffic */
1277 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1278 			sc->malo_stats.mst_tx_qstop++;
1279 			break;
1280 		}
1281 		/*
1282 		 * Pass the frame to the h/w for transmission.
1283 		 */
1284 		if (malo_tx_start(sc, ni, bf, m)) {
1285 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1286 			if (bf != NULL) {
1287 				bf->bf_m = NULL;
1288 				bf->bf_node = NULL;
1289 				MALO_TXQ_LOCK(txq);
1290 				STAILQ_INSERT_HEAD(&txq->free, bf, bf_list);
1291 				MALO_TXQ_UNLOCK(txq);
1292 			}
1293 			ieee80211_free_node(ni);
1294 			continue;
1295 		}
1296 		nqueued++;
1297 
1298 		if (nqueued >= malo_txcoalesce) {
1299 			/*
1300 			 * Poke the firmware to process queued frames;
1301 			 * see below about (lack of) locking.
1302 			 */
1303 			nqueued = 0;
1304 			malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
1305 		}
1306 	}
1307 
1308 	if (nqueued) {
1309 		/*
1310 		 * NB: We don't need to lock against tx done because
1311 		 * this just prods the firmware to check the transmit
1312 		 * descriptors.  The firmware will also start fetching
1313 		 * descriptors by itself if it notices new ones are
1314 		 * present when it goes to deliver a tx done interrupt
1315 		 * to the host. So if we race with tx done processing
1316 		 * it's ok.  Delivering the kick here rather than in
1317 		 * malo_tx_start is an optimization to avoid poking the
1318 		 * firmware for each packet.
1319 		 *
1320 		 * NB: the queue id isn't used so 0 is ok.
1321 		 */
1322 		malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
1323 	}
1324 }
1325 
1326 static void
1327 malo_watchdog(void *arg)
1328 {
1329 	struct malo_softc *sc;
1330 	struct ifnet *ifp;
1331 
1332 	sc = arg;
1333 	callout_reset(&sc->malo_watchdog_timer, hz, malo_watchdog, sc);
1334 	if (sc->malo_timer == 0 || --sc->malo_timer > 0)
1335 		return;
1336 
1337 	ifp = sc->malo_ifp;
1338 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) && !sc->malo_invalid) {
1339 		if_printf(ifp, "watchdog timeout\n");
1340 
1341 		/* XXX no way to reset h/w. now  */
1342 
1343 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1344 		sc->malo_stats.mst_watchdog++;
1345 	}
1346 }
1347 
1348 static int
1349 malo_hal_reset(struct malo_softc *sc)
1350 {
1351 	static int first = 0;
1352 	struct ifnet *ifp = sc->malo_ifp;
1353 	struct ieee80211com *ic = ifp->if_l2com;
1354 	struct malo_hal *mh = sc->malo_mh;
1355 
1356 	if (first == 0) {
1357 		/*
1358 		 * NB: when the device firstly is initialized, sometimes
1359 		 * firmware could override rx/tx dma registers so we re-set
1360 		 * these values once.
1361 		 */
1362 		malo_hal_set_rxtxdma(sc);
1363 		first = 1;
1364 	}
1365 
1366 	malo_hal_setantenna(mh, MHA_ANTENNATYPE_RX, sc->malo_rxantenna);
1367 	malo_hal_setantenna(mh, MHA_ANTENNATYPE_TX, sc->malo_txantenna);
1368 	malo_hal_setradio(mh, 1, MHP_AUTO_PREAMBLE);
1369 	malo_chan_set(sc, ic->ic_curchan);
1370 
1371 	/* XXX needs other stuffs?  */
1372 
1373 	return 1;
1374 }
1375 
1376 static __inline struct mbuf *
1377 malo_getrxmbuf(struct malo_softc *sc, struct malo_rxbuf *bf)
1378 {
1379 	struct mbuf *m;
1380 	bus_addr_t paddr;
1381 	int error;
1382 
1383 	/* XXX don't need mbuf, just dma buffer */
1384 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1385 	if (m == NULL) {
1386 		sc->malo_stats.mst_rx_nombuf++;	/* XXX */
1387 		return NULL;
1388 	}
1389 	error = bus_dmamap_load(sc->malo_dmat, bf->bf_dmamap,
1390 	    mtod(m, caddr_t), MJUMPAGESIZE,
1391 	    malo_load_cb, &paddr, BUS_DMA_NOWAIT);
1392 	if (error != 0) {
1393 		if_printf(sc->malo_ifp,
1394 		    "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1395 		m_freem(m);
1396 		return NULL;
1397 	}
1398 	bf->bf_data = paddr;
1399 	bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
1400 
1401 	return m;
1402 }
1403 
1404 static int
1405 malo_rxbuf_init(struct malo_softc *sc, struct malo_rxbuf *bf)
1406 {
1407 	struct malo_rxdesc *ds;
1408 
1409 	ds = bf->bf_desc;
1410 	if (bf->bf_m == NULL) {
1411 		bf->bf_m = malo_getrxmbuf(sc, bf);
1412 		if (bf->bf_m == NULL) {
1413 			/* mark descriptor to be skipped */
1414 			ds->rxcontrol = MALO_RXD_CTRL_OS_OWN;
1415 			/* NB: don't need PREREAD */
1416 			MALO_RXDESC_SYNC(sc, ds, BUS_DMASYNC_PREWRITE);
1417 			return ENOMEM;
1418 		}
1419 	}
1420 
1421 	/*
1422 	 * Setup descriptor.
1423 	 */
1424 	ds->qosctrl = 0;
1425 	ds->snr = 0;
1426 	ds->status = MALO_RXD_STATUS_IDLE;
1427 	ds->channel = 0;
1428 	ds->pktlen = htole16(MALO_RXSIZE);
1429 	ds->nf = 0;
1430 	ds->physbuffdata = htole32(bf->bf_data);
1431 	/* NB: don't touch pPhysNext, set once */
1432 	ds->rxcontrol = MALO_RXD_CTRL_DRIVER_OWN;
1433 	MALO_RXDESC_SYNC(sc, ds, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1434 
1435 	return 0;
1436 }
1437 
1438 /*
1439  * Setup the rx data structures.  This should only be done once or we may get
1440  * out of sync with the firmware.
1441  */
1442 static int
1443 malo_startrecv(struct malo_softc *sc)
1444 {
1445 	struct malo_rxbuf *bf, *prev;
1446 	struct malo_rxdesc *ds;
1447 
1448 	if (sc->malo_recvsetup == 1) {
1449 		malo_mode_init(sc);		/* set filters, etc. */
1450 		return 0;
1451 	}
1452 
1453 	prev = NULL;
1454 	STAILQ_FOREACH(bf, &sc->malo_rxbuf, bf_list) {
1455 		int error = malo_rxbuf_init(sc, bf);
1456 		if (error != 0) {
1457 			DPRINTF(sc, MALO_DEBUG_RECV,
1458 			    "%s: malo_rxbuf_init failed %d\n",
1459 			    __func__, error);
1460 			return error;
1461 		}
1462 		if (prev != NULL) {
1463 			ds = prev->bf_desc;
1464 			ds->physnext = htole32(bf->bf_daddr);
1465 		}
1466 		prev = bf;
1467 	}
1468 	if (prev != NULL) {
1469 		ds = prev->bf_desc;
1470 		ds->physnext =
1471 		    htole32(STAILQ_FIRST(&sc->malo_rxbuf)->bf_daddr);
1472 	}
1473 
1474 	sc->malo_recvsetup = 1;
1475 
1476 	malo_mode_init(sc);		/* set filters, etc. */
1477 
1478 	return 0;
1479 }
1480 
1481 static void
1482 malo_init_locked(struct malo_softc *sc)
1483 {
1484 	struct ifnet *ifp = sc->malo_ifp;
1485 	struct malo_hal *mh = sc->malo_mh;
1486 	int error;
1487 
1488 	DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags 0x%x\n",
1489 	    __func__, ifp->if_flags);
1490 
1491 	MALO_LOCK_ASSERT(sc);
1492 
1493 	/*
1494 	 * Stop anything previously setup.  This is safe whether this is
1495 	 * the first time through or not.
1496 	 */
1497 	malo_stop_locked(ifp, 0);
1498 
1499 	/*
1500 	 * Push state to the firmware.
1501 	 */
1502 	if (!malo_hal_reset(sc)) {
1503 		if_printf(ifp, "%s: unable to reset hardware\n", __func__);
1504 		return;
1505 	}
1506 
1507 	/*
1508 	 * Setup recv (once); transmit is already good to go.
1509 	 */
1510 	error = malo_startrecv(sc);
1511 	if (error != 0) {
1512 		if_printf(ifp, "%s: unable to start recv logic, error %d\n",
1513 		    __func__, error);
1514 		return;
1515 	}
1516 
1517 	/*
1518 	 * Enable interrupts.
1519 	 */
1520 	sc->malo_imask = MALO_A2HRIC_BIT_RX_RDY
1521 	    | MALO_A2HRIC_BIT_TX_DONE
1522 	    | MALO_A2HRIC_BIT_OPC_DONE
1523 	    | MALO_A2HRIC_BIT_MAC_EVENT
1524 	    | MALO_A2HRIC_BIT_RX_PROBLEM
1525 	    | MALO_A2HRIC_BIT_ICV_ERROR
1526 	    | MALO_A2HRIC_BIT_RADAR_DETECT
1527 	    | MALO_A2HRIC_BIT_CHAN_SWITCH;
1528 
1529 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1530 	malo_hal_intrset(mh, sc->malo_imask);
1531 	callout_reset(&sc->malo_watchdog_timer, hz, malo_watchdog, sc);
1532 }
1533 
1534 static void
1535 malo_init(void *arg)
1536 {
1537 	struct malo_softc *sc = (struct malo_softc *) arg;
1538 	struct ifnet *ifp = sc->malo_ifp;
1539 	struct ieee80211com *ic = ifp->if_l2com;
1540 
1541 	DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags 0x%x\n",
1542 	    __func__, ifp->if_flags);
1543 
1544 	MALO_LOCK(sc);
1545 	malo_init_locked(sc);
1546 
1547 	MALO_UNLOCK(sc);
1548 
1549 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1550 		ieee80211_start_all(ic);	/* start all vap's */
1551 }
1552 
1553 /*
1554  * Set the multicast filter contents into the hardware.
1555  */
1556 static void
1557 malo_setmcastfilter(struct malo_softc *sc)
1558 {
1559 	struct ifnet *ifp = sc->malo_ifp;
1560 	struct ieee80211com *ic = ifp->if_l2com;
1561 	struct ifmultiaddr *ifma;
1562 	uint8_t macs[IEEE80211_ADDR_LEN * MALO_HAL_MCAST_MAX];
1563 	uint8_t *mp;
1564 	int nmc;
1565 
1566 	mp = macs;
1567 	nmc = 0;
1568 
1569 	if (ic->ic_opmode == IEEE80211_M_MONITOR ||
1570 	    (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)))
1571 		goto all;
1572 
1573 	if_maddr_rlock(ifp);
1574 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1575 		if (ifma->ifma_addr->sa_family != AF_LINK)
1576 			continue;
1577 
1578 		if (nmc == MALO_HAL_MCAST_MAX) {
1579 			ifp->if_flags |= IFF_ALLMULTI;
1580 			if_maddr_runlock(ifp);
1581 			goto all;
1582 		}
1583 		IEEE80211_ADDR_COPY(mp,
1584 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1585 
1586 		mp += IEEE80211_ADDR_LEN, nmc++;
1587 	}
1588 	if_maddr_runlock(ifp);
1589 
1590 	malo_hal_setmcast(sc->malo_mh, nmc, macs);
1591 
1592 all:
1593 	/*
1594 	 * XXX we don't know how to set the f/w for supporting
1595 	 * IFF_ALLMULTI | IFF_PROMISC cases
1596 	 */
1597 	return;
1598 }
1599 
1600 static int
1601 malo_mode_init(struct malo_softc *sc)
1602 {
1603 	struct ifnet *ifp = sc->malo_ifp;
1604 	struct ieee80211com *ic = ifp->if_l2com;
1605 	struct malo_hal *mh = sc->malo_mh;
1606 
1607 	/*
1608 	 * NB: Ignore promisc in hostap mode; it's set by the
1609 	 * bridge.  This is wrong but we have no way to
1610 	 * identify internal requests (from the bridge)
1611 	 * versus external requests such as for tcpdump.
1612 	 */
1613 	malo_hal_setpromisc(mh, (ifp->if_flags & IFF_PROMISC) &&
1614 	    ic->ic_opmode != IEEE80211_M_HOSTAP);
1615 	malo_setmcastfilter(sc);
1616 
1617 	return ENXIO;
1618 }
1619 
1620 static void
1621 malo_tx_draintxq(struct malo_softc *sc, struct malo_txq *txq)
1622 {
1623 	struct ieee80211_node *ni;
1624 	struct malo_txbuf *bf;
1625 	u_int ix;
1626 
1627 	/*
1628 	 * NB: this assumes output has been stopped and
1629 	 *     we do not need to block malo_tx_tasklet
1630 	 */
1631 	for (ix = 0;; ix++) {
1632 		MALO_TXQ_LOCK(txq);
1633 		bf = STAILQ_FIRST(&txq->active);
1634 		if (bf == NULL) {
1635 			MALO_TXQ_UNLOCK(txq);
1636 			break;
1637 		}
1638 		STAILQ_REMOVE_HEAD(&txq->active, bf_list);
1639 		MALO_TXQ_UNLOCK(txq);
1640 #ifdef MALO_DEBUG
1641 		if (sc->malo_debug & MALO_DEBUG_RESET) {
1642 			struct ifnet *ifp = sc->malo_ifp;
1643 			struct ieee80211com *ic = ifp->if_l2com;
1644 			const struct malo_txrec *tr =
1645 			    mtod(bf->bf_m, const struct malo_txrec *);
1646 			malo_printtxbuf(bf, txq->qnum, ix);
1647 			ieee80211_dump_pkt(ic, (const uint8_t *)&tr->wh,
1648 			    bf->bf_m->m_len - sizeof(tr->fwlen), 0, -1);
1649 		}
1650 #endif /* MALO_DEBUG */
1651 		bus_dmamap_unload(sc->malo_dmat, bf->bf_dmamap);
1652 		ni = bf->bf_node;
1653 		bf->bf_node = NULL;
1654 		if (ni != NULL) {
1655 			/*
1656 			 * Reclaim node reference.
1657 			 */
1658 			ieee80211_free_node(ni);
1659 		}
1660 		m_freem(bf->bf_m);
1661 		bf->bf_m = NULL;
1662 
1663 		MALO_TXQ_LOCK(txq);
1664 		STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
1665 		txq->nfree++;
1666 		MALO_TXQ_UNLOCK(txq);
1667 	}
1668 }
1669 
1670 static void
1671 malo_stop_locked(struct ifnet *ifp, int disable)
1672 {
1673 	struct malo_softc *sc = ifp->if_softc;
1674 	struct malo_hal *mh = sc->malo_mh;
1675 	int i;
1676 
1677 	DPRINTF(sc, MALO_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1678 	    __func__, sc->malo_invalid, ifp->if_flags);
1679 
1680 	MALO_LOCK_ASSERT(sc);
1681 
1682 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1683 		return;
1684 
1685 	/*
1686 	 * Shutdown the hardware and driver:
1687 	 *    disable interrupts
1688 	 *    turn off the radio
1689 	 *    drain and release tx queues
1690 	 *
1691 	 * Note that some of this work is not possible if the hardware
1692 	 * is gone (invalid).
1693 	 */
1694 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1695 	callout_stop(&sc->malo_watchdog_timer);
1696 	sc->malo_timer = 0;
1697 	/* diable interrupt.  */
1698 	malo_hal_intrset(mh, 0);
1699 	/* turn off the radio.  */
1700 	malo_hal_setradio(mh, 0, MHP_AUTO_PREAMBLE);
1701 
1702 	/* drain and release tx queues.  */
1703 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
1704 		malo_tx_draintxq(sc, &sc->malo_txq[i]);
1705 }
1706 
1707 static int
1708 malo_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1709 {
1710 #define	MALO_IS_RUNNING(ifp) \
1711 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
1712 	struct malo_softc *sc = ifp->if_softc;
1713 	struct ieee80211com *ic = ifp->if_l2com;
1714 	struct ifreq *ifr = (struct ifreq *) data;
1715 	int error = 0, startall = 0;
1716 
1717 	MALO_LOCK(sc);
1718 	switch (cmd) {
1719 	case SIOCSIFFLAGS:
1720 		if (MALO_IS_RUNNING(ifp)) {
1721 			/*
1722 			 * To avoid rescanning another access point,
1723 			 * do not call malo_init() here.  Instead,
1724 			 * only reflect promisc mode settings.
1725 			 */
1726 			malo_mode_init(sc);
1727 		} else if (ifp->if_flags & IFF_UP) {
1728 			/*
1729 			 * Beware of being called during attach/detach
1730 			 * to reset promiscuous mode.  In that case we
1731 			 * will still be marked UP but not RUNNING.
1732 			 * However trying to re-init the interface
1733 			 * is the wrong thing to do as we've already
1734 			 * torn down much of our state.  There's
1735 			 * probably a better way to deal with this.
1736 			 */
1737 			if (!sc->malo_invalid) {
1738 				malo_init_locked(sc);
1739 				startall = 1;
1740 			}
1741 		} else
1742 			malo_stop_locked(ifp, 1);
1743 		break;
1744 	case SIOCGIFMEDIA:
1745 	case SIOCSIFMEDIA:
1746 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1747 		break;
1748 	default:
1749 		error = ether_ioctl(ifp, cmd, data);
1750 		break;
1751 	}
1752 	MALO_UNLOCK(sc);
1753 
1754 	if (startall)
1755 		ieee80211_start_all(ic);
1756 	return error;
1757 #undef MALO_IS_RUNNING
1758 }
1759 
1760 /*
1761  * Callback from the 802.11 layer to update the slot time
1762  * based on the current setting.  We use it to notify the
1763  * firmware of ERP changes and the f/w takes care of things
1764  * like slot time and preamble.
1765  */
1766 static void
1767 malo_updateslot(struct ifnet *ifp)
1768 {
1769 	struct malo_softc *sc = ifp->if_softc;
1770 	struct ieee80211com *ic = ifp->if_l2com;
1771 	struct malo_hal *mh = sc->malo_mh;
1772 	int error;
1773 
1774 	/* NB: can be called early; suppress needless cmds */
1775 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1776 		return;
1777 
1778 	DPRINTF(sc, MALO_DEBUG_RESET,
1779 	    "%s: chan %u MHz/flags 0x%x %s slot, (ic_flags 0x%x)\n",
1780 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
1781 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", ic->ic_flags);
1782 
1783 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1784 		error = malo_hal_set_slot(mh, 1);
1785 	else
1786 		error = malo_hal_set_slot(mh, 0);
1787 
1788 	if (error != 0)
1789 		device_printf(sc->malo_dev, "setting %s slot failed\n",
1790 			ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long");
1791 }
1792 
1793 static int
1794 malo_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1795 {
1796 	struct ieee80211com *ic = vap->iv_ic;
1797 	struct malo_softc *sc = ic->ic_ifp->if_softc;
1798 	struct malo_hal *mh = sc->malo_mh;
1799 	int error;
1800 
1801 	DPRINTF(sc, MALO_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1802 	    ieee80211_state_name[vap->iv_state],
1803 	    ieee80211_state_name[nstate]);
1804 
1805 	/*
1806 	 * Invoke the net80211 layer first so iv_bss is setup.
1807 	 */
1808 	error = MALO_VAP(vap)->malo_newstate(vap, nstate, arg);
1809 	if (error != 0)
1810 		return error;
1811 
1812 	if (nstate == IEEE80211_S_RUN && vap->iv_state != IEEE80211_S_RUN) {
1813 		struct ieee80211_node *ni = vap->iv_bss;
1814 		enum ieee80211_phymode mode = ieee80211_chan2mode(ni->ni_chan);
1815 		const struct ieee80211_txparam *tp = &vap->iv_txparms[mode];
1816 
1817 		DPRINTF(sc, MALO_DEBUG_STATE,
1818 		    "%s: %s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
1819 		    "capinfo 0x%04x chan %d associd 0x%x mode %d rate %d\n",
1820 		    vap->iv_ifp->if_xname, __func__, vap->iv_flags,
1821 		    ni->ni_intval, ether_sprintf(ni->ni_bssid), ni->ni_capinfo,
1822 		    ieee80211_chan2ieee(ic, ic->ic_curchan),
1823 		    ni->ni_associd, mode, tp->ucastrate);
1824 
1825 		malo_hal_setradio(mh, 1,
1826 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ?
1827 			MHP_SHORT_PREAMBLE : MHP_LONG_PREAMBLE);
1828 		malo_hal_setassocid(sc->malo_mh, ni->ni_bssid, ni->ni_associd);
1829 		malo_hal_set_rate(mh, mode,
1830 		   tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1831 		       0 : malo_fix2rate(tp->ucastrate));
1832 	}
1833 	return 0;
1834 }
1835 
1836 static int
1837 malo_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1838 	const struct ieee80211_bpf_params *params)
1839 {
1840 	struct ieee80211com *ic = ni->ni_ic;
1841 	struct ifnet *ifp = ic->ic_ifp;
1842 	struct malo_softc *sc = ifp->if_softc;
1843 	struct malo_txbuf *bf;
1844 	struct malo_txq *txq;
1845 
1846 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->malo_invalid) {
1847 		ieee80211_free_node(ni);
1848 		m_freem(m);
1849 		return ENETDOWN;
1850 	}
1851 
1852 	/*
1853 	 * Grab a TX buffer and associated resources.  Note that we depend
1854 	 * on the classification by the 802.11 layer to get to the right h/w
1855 	 * queue.  Management frames must ALWAYS go on queue 1 but we
1856 	 * cannot just force that here because we may receive non-mgt frames.
1857 	 */
1858 	txq = &sc->malo_txq[0];
1859 	bf = malo_getbuf(sc, txq);
1860 	if (bf == NULL) {
1861 		/* XXX blocks other traffic */
1862 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1863 		ieee80211_free_node(ni);
1864 		m_freem(m);
1865 		return ENOBUFS;
1866 	}
1867 
1868 	/*
1869 	 * Pass the frame to the h/w for transmission.
1870 	 */
1871 	if (malo_tx_start(sc, ni, bf, m) != 0) {
1872 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1873 		bf->bf_m = NULL;
1874 		bf->bf_node = NULL;
1875 		MALO_TXQ_LOCK(txq);
1876 		STAILQ_INSERT_HEAD(&txq->free, bf, bf_list);
1877 		txq->nfree++;
1878 		MALO_TXQ_UNLOCK(txq);
1879 
1880 		ieee80211_free_node(ni);
1881 		return EIO;		/* XXX */
1882 	}
1883 
1884 	/*
1885 	 * NB: We don't need to lock against tx done because this just
1886 	 * prods the firmware to check the transmit descriptors.  The firmware
1887 	 * will also start fetching descriptors by itself if it notices
1888 	 * new ones are present when it goes to deliver a tx done interrupt
1889 	 * to the host. So if we race with tx done processing it's ok.
1890 	 * Delivering the kick here rather than in malo_tx_start is
1891 	 * an optimization to avoid poking the firmware for each packet.
1892 	 *
1893 	 * NB: the queue id isn't used so 0 is ok.
1894 	 */
1895 	malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
1896 
1897 	return 0;
1898 }
1899 
1900 static void
1901 malo_sysctlattach(struct malo_softc *sc)
1902 {
1903 #ifdef	MALO_DEBUG
1904 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->malo_dev);
1905 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->malo_dev);
1906 
1907 	sc->malo_debug = malo_debug;
1908 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1909 		"debug", CTLFLAG_RW, &sc->malo_debug, 0,
1910 		"control debugging printfs");
1911 #endif
1912 }
1913 
1914 static void
1915 malo_announce(struct malo_softc *sc)
1916 {
1917 	struct ifnet *ifp = sc->malo_ifp;
1918 
1919 	if_printf(ifp, "versions [hw %d fw %d.%d.%d.%d] (regioncode %d)\n",
1920 		sc->malo_hwspecs.hwversion,
1921 		(sc->malo_hwspecs.fw_releasenum >> 24) & 0xff,
1922 		(sc->malo_hwspecs.fw_releasenum >> 16) & 0xff,
1923 		(sc->malo_hwspecs.fw_releasenum >> 8) & 0xff,
1924 		(sc->malo_hwspecs.fw_releasenum >> 0) & 0xff,
1925 		sc->malo_hwspecs.regioncode);
1926 
1927 	if (bootverbose || malo_rxbuf != MALO_RXBUF)
1928 		if_printf(ifp, "using %u rx buffers\n", malo_rxbuf);
1929 	if (bootverbose || malo_txbuf != MALO_TXBUF)
1930 		if_printf(ifp, "using %u tx buffers\n", malo_txbuf);
1931 }
1932 
1933 /*
1934  * Convert net80211 channel to a HAL channel.
1935  */
1936 static void
1937 malo_mapchan(struct malo_hal_channel *hc, const struct ieee80211_channel *chan)
1938 {
1939 	hc->channel = chan->ic_ieee;
1940 
1941 	*(uint32_t *)&hc->flags = 0;
1942 	if (IEEE80211_IS_CHAN_2GHZ(chan))
1943 		hc->flags.freqband = MALO_FREQ_BAND_2DOT4GHZ;
1944 }
1945 
1946 /*
1947  * Set/change channels.  If the channel is really being changed,
1948  * it's done by reseting the chip.  To accomplish this we must
1949  * first cleanup any pending DMA, then restart stuff after a la
1950  * malo_init.
1951  */
1952 static int
1953 malo_chan_set(struct malo_softc *sc, struct ieee80211_channel *chan)
1954 {
1955 	struct malo_hal *mh = sc->malo_mh;
1956 	struct malo_hal_channel hchan;
1957 
1958 	DPRINTF(sc, MALO_DEBUG_RESET, "%s: chan %u MHz/flags 0x%x\n",
1959 	    __func__, chan->ic_freq, chan->ic_flags);
1960 
1961 	/*
1962 	 * Convert to a HAL channel description with the flags constrained
1963 	 * to reflect the current operating mode.
1964 	 */
1965 	malo_mapchan(&hchan, chan);
1966 	malo_hal_intrset(mh, 0);		/* disable interrupts */
1967 	malo_hal_setchannel(mh, &hchan);
1968 	malo_hal_settxpower(mh, &hchan);
1969 
1970 	/*
1971 	 * Update internal state.
1972 	 */
1973 	sc->malo_tx_th.wt_chan_freq = htole16(chan->ic_freq);
1974 	sc->malo_rx_th.wr_chan_freq = htole16(chan->ic_freq);
1975 	if (IEEE80211_IS_CHAN_ANYG(chan)) {
1976 		sc->malo_tx_th.wt_chan_flags = htole16(IEEE80211_CHAN_G);
1977 		sc->malo_rx_th.wr_chan_flags = htole16(IEEE80211_CHAN_G);
1978 	} else {
1979 		sc->malo_tx_th.wt_chan_flags = htole16(IEEE80211_CHAN_B);
1980 		sc->malo_rx_th.wr_chan_flags = htole16(IEEE80211_CHAN_B);
1981 	}
1982 	sc->malo_curchan = hchan;
1983 	malo_hal_intrset(mh, sc->malo_imask);
1984 
1985 	return 0;
1986 }
1987 
1988 static void
1989 malo_scan_start(struct ieee80211com *ic)
1990 {
1991 	struct ifnet *ifp = ic->ic_ifp;
1992 	struct malo_softc *sc = ifp->if_softc;
1993 
1994 	DPRINTF(sc, MALO_DEBUG_STATE, "%s\n", __func__);
1995 }
1996 
1997 static void
1998 malo_scan_end(struct ieee80211com *ic)
1999 {
2000 	struct ifnet *ifp = ic->ic_ifp;
2001 	struct malo_softc *sc = ifp->if_softc;
2002 
2003 	DPRINTF(sc, MALO_DEBUG_STATE, "%s\n", __func__);
2004 }
2005 
2006 static void
2007 malo_set_channel(struct ieee80211com *ic)
2008 {
2009 	struct ifnet *ifp = ic->ic_ifp;
2010 	struct malo_softc *sc = ifp->if_softc;
2011 
2012 	(void) malo_chan_set(sc, ic->ic_curchan);
2013 }
2014 
2015 static void
2016 malo_rx_proc(void *arg, int npending)
2017 {
2018 #define	IEEE80211_DIR_DSTODS(wh)					\
2019 	((((const struct ieee80211_frame *)wh)->i_fc[1] &		\
2020 	    IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
2021 	struct malo_softc *sc = arg;
2022 	struct ifnet *ifp = sc->malo_ifp;
2023 	struct ieee80211com *ic = ifp->if_l2com;
2024 	struct malo_rxbuf *bf;
2025 	struct malo_rxdesc *ds;
2026 	struct mbuf *m, *mnew;
2027 	struct ieee80211_qosframe *wh;
2028 	struct ieee80211_qosframe_addr4 *wh4;
2029 	struct ieee80211_node *ni;
2030 	int off, len, hdrlen, pktlen, rssi, ntodo;
2031 	uint8_t *data, status;
2032 	uint32_t readptr, writeptr;
2033 
2034 	DPRINTF(sc, MALO_DEBUG_RX_PROC,
2035 	    "%s: pending %u rdptr(0x%x) 0x%x wrptr(0x%x) 0x%x\n",
2036 	    __func__, npending,
2037 	    sc->malo_hwspecs.rxdesc_read,
2038 	    malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_read),
2039 	    sc->malo_hwspecs.rxdesc_write,
2040 	    malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_write));
2041 
2042 	readptr = malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_read);
2043 	writeptr = malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_write);
2044 	if (readptr == writeptr)
2045 		return;
2046 
2047 	bf = sc->malo_rxnext;
2048 	for (ntodo = malo_rxquota; ntodo > 0 && readptr != writeptr; ntodo--) {
2049 		if (bf == NULL) {
2050 			bf = STAILQ_FIRST(&sc->malo_rxbuf);
2051 			break;
2052 		}
2053 		ds = bf->bf_desc;
2054 		if (bf->bf_m == NULL) {
2055 			/*
2056 			 * If data allocation failed previously there
2057 			 * will be no buffer; try again to re-populate it.
2058 			 * Note the firmware will not advance to the next
2059 			 * descriptor with a dma buffer so we must mimic
2060 			 * this or we'll get out of sync.
2061 			 */
2062 			DPRINTF(sc, MALO_DEBUG_ANY,
2063 			    "%s: rx buf w/o dma memory\n", __func__);
2064 			(void)malo_rxbuf_init(sc, bf);
2065 			break;
2066 		}
2067 		MALO_RXDESC_SYNC(sc, ds,
2068 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2069 		if (ds->rxcontrol != MALO_RXD_CTRL_DMA_OWN)
2070 			break;
2071 
2072 		readptr = le32toh(ds->physnext);
2073 
2074 #ifdef MALO_DEBUG
2075 		if (sc->malo_debug & MALO_DEBUG_RECV_DESC)
2076 			malo_printrxbuf(bf, 0);
2077 #endif
2078 		status = ds->status;
2079 		if (status & MALO_RXD_STATUS_DECRYPT_ERR_MASK) {
2080 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2081 			goto rx_next;
2082 		}
2083 		/*
2084 		 * Sync the data buffer.
2085 		 */
2086 		len = le16toh(ds->pktlen);
2087 		bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap,
2088 		    BUS_DMASYNC_POSTREAD);
2089 		/*
2090 		 * The 802.11 header is provided all or in part at the front;
2091 		 * use it to calculate the true size of the header that we'll
2092 		 * construct below.  We use this to figure out where to copy
2093 		 * payload prior to constructing the header.
2094 		 */
2095 		m = bf->bf_m;
2096 		data = mtod(m, uint8_t *);
2097 		hdrlen = ieee80211_anyhdrsize(data + sizeof(uint16_t));
2098 		off = sizeof(uint16_t) + sizeof(struct ieee80211_frame_addr4);
2099 
2100 		/*
2101 		 * Calculate RSSI. XXX wrong
2102 		 */
2103 		rssi = 2 * ((int) ds->snr - ds->nf);	/* NB: .5 dBm  */
2104 		if (rssi > 100)
2105 			rssi = 100;
2106 
2107 		pktlen = hdrlen + (len - off);
2108 		/*
2109 		 * NB: we know our frame is at least as large as
2110 		 * IEEE80211_MIN_LEN because there is a 4-address frame at
2111 		 * the front.  Hence there's no need to vet the packet length.
2112 		 * If the frame in fact is too small it should be discarded
2113 		 * at the net80211 layer.
2114 		 */
2115 
2116 		/* XXX don't need mbuf, just dma buffer */
2117 		mnew = malo_getrxmbuf(sc, bf);
2118 		if (mnew == NULL) {
2119 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2120 			goto rx_next;
2121 		}
2122 		/*
2123 		 * Attach the dma buffer to the mbuf; malo_rxbuf_init will
2124 		 * re-setup the rx descriptor using the replacement dma
2125 		 * buffer we just installed above.
2126 		 */
2127 		bf->bf_m = mnew;
2128 		m->m_data += off - hdrlen;
2129 		m->m_pkthdr.len = m->m_len = pktlen;
2130 		m->m_pkthdr.rcvif = ifp;
2131 
2132 		/*
2133 		 * Piece 802.11 header together.
2134 		 */
2135 		wh = mtod(m, struct ieee80211_qosframe *);
2136 		/* NB: don't need to do this sometimes but ... */
2137 		/* XXX special case so we can memcpy after m_devget? */
2138 		ovbcopy(data + sizeof(uint16_t), wh, hdrlen);
2139 		if (IEEE80211_QOS_HAS_SEQ(wh)) {
2140 			if (IEEE80211_DIR_DSTODS(wh)) {
2141 				wh4 = mtod(m,
2142 				    struct ieee80211_qosframe_addr4*);
2143 				*(uint16_t *)wh4->i_qos = ds->qosctrl;
2144 			} else {
2145 				*(uint16_t *)wh->i_qos = ds->qosctrl;
2146 			}
2147 		}
2148 		if (ieee80211_radiotap_active(ic)) {
2149 			sc->malo_rx_th.wr_flags = 0;
2150 			sc->malo_rx_th.wr_rate = ds->rate;
2151 			sc->malo_rx_th.wr_antsignal = rssi;
2152 			sc->malo_rx_th.wr_antnoise = ds->nf;
2153 		}
2154 #ifdef MALO_DEBUG
2155 		if (IFF_DUMPPKTS_RECV(sc, wh)) {
2156 			ieee80211_dump_pkt(ic, mtod(m, caddr_t),
2157 			    len, ds->rate, rssi);
2158 		}
2159 #endif
2160 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
2161 
2162 		/* dispatch */
2163 		ni = ieee80211_find_rxnode(ic,
2164 		    (struct ieee80211_frame_min *)wh);
2165 		if (ni != NULL) {
2166 			(void) ieee80211_input(ni, m, rssi, ds->nf);
2167 			ieee80211_free_node(ni);
2168 		} else
2169 			(void) ieee80211_input_all(ic, m, rssi, ds->nf);
2170 rx_next:
2171 		/* NB: ignore ENOMEM so we process more descriptors */
2172 		(void) malo_rxbuf_init(sc, bf);
2173 		bf = STAILQ_NEXT(bf, bf_list);
2174 	}
2175 
2176 	malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_read, readptr);
2177 	sc->malo_rxnext = bf;
2178 
2179 	if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2180 	    !IFQ_IS_EMPTY(&ifp->if_snd))
2181 		malo_start(ifp);
2182 #undef IEEE80211_DIR_DSTODS
2183 }
2184 
2185 static void
2186 malo_stop(struct ifnet *ifp, int disable)
2187 {
2188 	struct malo_softc *sc = ifp->if_softc;
2189 
2190 	MALO_LOCK(sc);
2191 	malo_stop_locked(ifp, disable);
2192 	MALO_UNLOCK(sc);
2193 }
2194 
2195 /*
2196  * Reclaim all tx queue resources.
2197  */
2198 static void
2199 malo_tx_cleanup(struct malo_softc *sc)
2200 {
2201 	int i;
2202 
2203 	for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
2204 		malo_tx_cleanupq(sc, &sc->malo_txq[i]);
2205 }
2206 
2207 int
2208 malo_detach(struct malo_softc *sc)
2209 {
2210 	struct ifnet *ifp = sc->malo_ifp;
2211 	struct ieee80211com *ic = ifp->if_l2com;
2212 
2213 	DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags %x\n",
2214 		__func__, ifp->if_flags);
2215 
2216 	malo_stop(ifp, 1);
2217 
2218 	if (sc->malo_tq != NULL) {
2219 		taskqueue_drain(sc->malo_tq, &sc->malo_rxtask);
2220 		taskqueue_drain(sc->malo_tq, &sc->malo_txtask);
2221 		taskqueue_free(sc->malo_tq);
2222 		sc->malo_tq = NULL;
2223 	}
2224 
2225 	/*
2226 	 * NB: the order of these is important:
2227 	 * o call the 802.11 layer before detaching the hal to
2228 	 *   insure callbacks into the driver to delete global
2229 	 *   key cache entries can be handled
2230 	 * o reclaim the tx queue data structures after calling
2231 	 *   the 802.11 layer as we'll get called back to reclaim
2232 	 *   node state and potentially want to use them
2233 	 * o to cleanup the tx queues the hal is called, so detach
2234 	 *   it last
2235 	 * Other than that, it's straightforward...
2236 	 */
2237 	ieee80211_ifdetach(ic);
2238 	callout_drain(&sc->malo_watchdog_timer);
2239 	malo_dma_cleanup(sc);
2240 	malo_tx_cleanup(sc);
2241 	malo_hal_detach(sc->malo_mh);
2242 	if_free(ifp);
2243 
2244 	MALO_LOCK_DESTROY(sc);
2245 
2246 	return 0;
2247 }
2248 
2249 void
2250 malo_shutdown(struct malo_softc *sc)
2251 {
2252 	malo_stop(sc->malo_ifp, 1);
2253 }
2254 
2255 void
2256 malo_suspend(struct malo_softc *sc)
2257 {
2258 	struct ifnet *ifp = sc->malo_ifp;
2259 
2260 	DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags %x\n",
2261 		__func__, ifp->if_flags);
2262 
2263 	malo_stop(ifp, 1);
2264 }
2265 
2266 void
2267 malo_resume(struct malo_softc *sc)
2268 {
2269 	struct ifnet *ifp = sc->malo_ifp;
2270 
2271 	DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags %x\n",
2272 		__func__, ifp->if_flags);
2273 
2274 	if (ifp->if_flags & IFF_UP)
2275 		malo_init(sc);
2276 }
2277