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