2 * Copyright (c) 2008 Weongyo Jeong <weongyo@freebsd.org>
3 * Copyright (c) 2007 Marvell Semiconductor, Inc.
4 * Copyright (c) 2007 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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>
47 #include <machine/bus.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>
57 #include <net80211/ieee80211_var.h>
58 #include <net80211/ieee80211_regdomain.h>
62 #include <dev/malo/if_malo.h>
64 SYSCTL_NODE(_hw, OID_AUTO, malo, CTLFLAG_RD, 0,
65 "Marvell 88w8335 driver parameters");
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");
81 static int malo_debug = 0;
82 SYSCTL_INT(_hw_malo, OID_AUTO, debug, CTLFLAG_RWTUN, &malo_debug,
83 0, "control debugging printfs");
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
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__); \
117 #define DPRINTF(sc, m, fmt, ...) do { \
122 static MALLOC_DEFINE(M_MALODEV, "malodev", "malo driver dma buffers");
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 *);
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.
159 malo_bar0_read4(struct malo_softc *sc, bus_size_t off)
161 return bus_space_read_4(sc->malo_io0t, sc->malo_io0h, off);
165 malo_bar0_write4(struct malo_softc *sc, bus_size_t off, uint32_t val)
167 DPRINTF(sc, MALO_DEBUG_FW, "%s: off 0x%jx val 0x%x\n",
168 __func__, (uintmax_t)off, val);
170 bus_space_write_4(sc->malo_io0t, sc->malo_io0h, off, val);
174 malo_attach(uint16_t devid, struct malo_softc *sc)
177 struct ieee80211com *ic;
182 ifp = sc->malo_ifp = if_alloc(IFT_IEEE80211);
184 device_printf(sc->malo_dev, "can not if_alloc()\n");
190 callout_init_mtx(&sc->malo_watchdog_timer, &sc->malo_mtx, 0);
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));
196 mh = malo_hal_attach(sc->malo_dev, devid,
197 sc->malo_io1h, sc->malo_io1t, sc->malo_dmat);
199 if_printf(ifp, "unable to attach HAL\n");
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.
210 error = malo_hal_fwload(mh, "malo8335-h", "malo8335-m");
212 if_printf(ifp, "unable to setup firmware\n");
215 /* XXX gethwspecs() extracts correct informations? not maybe! */
216 error = malo_hal_gethwspecs(mh, &sc->malo_hwspecs);
218 if_printf(ifp, "unable to fetch h/w specs\n");
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]);
238 /* NB: firmware looks that it does not export regdomain info API. */
240 setbit(&bands, IEEE80211_MODE_11B);
241 setbit(&bands, IEEE80211_MODE_11G);
242 ieee80211_init_channels(ic, NULL, &bands);
244 sc->malo_txantenna = 0x2; /* h/w default */
245 sc->malo_rxantenna = 0xffff; /* h/w default */
248 * Allocate tx + rx descriptors and populate the lists.
249 * We immediately push the information to the firmware
250 * as otherwise it gets upset.
252 error = malo_dma_setup(sc);
254 if_printf(ifp, "failed to setup descriptors: %d\n", error);
257 error = malo_setup_hwdma(sc); /* push to firmware */
258 if (error != 0) /* NB: malo_setupdma prints msg */
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);
266 TASK_INIT(&sc->malo_rxtask, 0, malo_rx_proc, sc);
267 TASK_INIT(&sc->malo_txtask, 0, malo_tx_proc, 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);
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;
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 */
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.
298 ic->ic_headroom = sizeof(struct malo_txrec) -
299 sizeof(struct ieee80211_frame);
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;
309 ic->ic_scan_start = malo_scan_start;
310 ic->ic_scan_end = malo_scan_end;
311 ic->ic_set_channel = malo_set_channel;
313 sc->malo_invalid = 0; /* ready to go, enable int handling */
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);
322 * Setup dynamic sysctl's.
324 malo_sysctlattach(sc);
327 ieee80211_announce(ic);
332 malo_dma_cleanup(sc);
337 sc->malo_invalid = 1;
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])
348 struct ifnet *ifp = ic->ic_ifp;
349 struct malo_vap *mvp;
350 struct ieee80211vap *vap;
352 if (!TAILQ_EMPTY(&ic->ic_vaps)) {
353 if_printf(ifp, "multiple vaps not supported\n");
357 case IEEE80211_M_STA:
358 if (opmode == IEEE80211_M_STA)
359 flags |= IEEE80211_CLONE_NOBEACONS;
361 case IEEE80211_M_MONITOR:
364 if_printf(ifp, "%s mode not supported\n",
365 ieee80211_opmode_name[opmode]);
366 return NULL; /* unsupported */
368 mvp = (struct malo_vap *) malloc(sizeof(struct malo_vap),
369 M_80211_VAP, M_NOWAIT | M_ZERO);
371 if_printf(ifp, "cannot allocate vap state block\n");
374 vap = &mvp->malo_vap;
375 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
377 /* override state transition machine */
378 mvp->malo_newstate = vap->iv_newstate;
379 vap->iv_newstate = malo_newstate;
382 ieee80211_vap_attach(vap,
383 ieee80211_media_change, ieee80211_media_status);
384 ic->ic_opmode = opmode;
389 malo_vap_delete(struct ieee80211vap *vap)
391 struct malo_vap *mvp = MALO_VAP(vap);
393 ieee80211_vap_detach(vap);
394 free(mvp, M_80211_VAP);
400 struct malo_softc *sc = arg;
401 struct malo_hal *mh = sc->malo_mh;
404 if (sc->malo_invalid) {
406 * The hardware is not ready/present, don't touch anything.
407 * Note this can happen early on if the IRQ is shared.
409 DPRINTF(sc, MALO_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
410 return (FILTER_STRAY);
414 * Figure out the reason(s) for the interrupt.
416 malo_hal_getisr(mh, &status); /* NB: clears ISR too */
417 if (status == 0) /* must be a shared irq */
418 return (FILTER_STRAY);
420 DPRINTF(sc, MALO_DEBUG_INTR, "%s: status 0x%x imask 0x%x\n",
421 __func__, status, sc->malo_imask);
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)
431 if (status & MALO_A2HRIC_BIT_RX_PROBLEM)
433 if (status & MALO_A2HRIC_BIT_ICV_ERROR) {
435 sc->malo_stats.mst_rx_badtkipicv++;
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",
443 return (FILTER_HANDLED);
447 malo_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
449 bus_addr_t *paddr = (bus_addr_t*) arg;
451 KASSERT(error == 0, ("error %u on bus_dma callback", error));
453 *paddr = segs->ds_addr;
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)
462 struct ifnet *ifp = sc->malo_ifp;
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);
471 dd->dd_desc_len = nbuf * ndesc * descsize;
474 * Setup DMA descriptor area.
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 */
483 dd->dd_desc_len, /* maxsegsize */
484 BUS_DMA_ALLOCNOW, /* flags */
489 if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
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);
497 if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
498 "error %u\n", nbuf * ndesc, dd->dd_name, error);
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);
506 if_printf(ifp, "unable to map %s descriptors, error %u\n",
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);
520 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
522 bus_dma_tag_destroy(dd->dd_dmat);
523 memset(dd, 0, sizeof(*dd));
527 #define DS2PHYS(_dd, _ds) \
528 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
531 malo_rxdma_setup(struct malo_softc *sc)
533 struct ifnet *ifp = sc->malo_ifp;
535 struct malo_rxbuf *bf;
536 struct malo_rxdesc *ds;
538 error = malo_desc_setup(sc, "rx", &sc->malo_rxdma,
539 malo_rxbuf, sizeof(struct malo_rxbuf),
540 1, sizeof(struct malo_rxdesc));
545 * Allocate rx buffers and set them up.
547 bsize = malo_rxbuf * sizeof(struct malo_rxbuf);
548 bf = malloc(bsize, M_MALODEV, M_NOWAIT | M_ZERO);
550 if_printf(ifp, "malloc of %u rx buffers failed\n", bsize);
553 sc->malo_rxdma.dd_bufptr = bf;
555 STAILQ_INIT(&sc->malo_rxbuf);
556 ds = sc->malo_rxdma.dd_desc;
557 for (i = 0; i < malo_rxbuf; i++, bf++, ds++) {
559 bf->bf_daddr = DS2PHYS(&sc->malo_rxdma, ds);
560 error = bus_dmamap_create(sc->malo_dmat, BUS_DMA_NOWAIT,
563 if_printf(ifp, "%s: unable to dmamap for rx buffer, "
564 "error %d\n", __func__, error);
567 /* NB: tail is intentional to preserve descriptor order */
568 STAILQ_INSERT_TAIL(&sc->malo_rxbuf, bf, bf_list);
574 malo_txdma_setup(struct malo_softc *sc, struct malo_txq *txq)
576 struct ifnet *ifp = sc->malo_ifp;
578 struct malo_txbuf *bf;
579 struct malo_txdesc *ds;
581 error = malo_desc_setup(sc, "tx", &txq->dma,
582 malo_txbuf, sizeof(struct malo_txbuf),
583 MALO_TXDESC, sizeof(struct malo_txdesc));
587 /* allocate and setup tx buffers */
588 bsize = malo_txbuf * sizeof(struct malo_txbuf);
589 bf = malloc(bsize, M_MALODEV, M_NOWAIT | M_ZERO);
591 if_printf(ifp, "malloc of %u tx buffers failed\n",
595 txq->dma.dd_bufptr = bf;
597 STAILQ_INIT(&txq->free);
599 ds = txq->dma.dd_desc;
600 for (i = 0; i < malo_txbuf; i++, bf++, ds += MALO_TXDESC) {
602 bf->bf_daddr = DS2PHYS(&txq->dma, ds);
603 error = bus_dmamap_create(sc->malo_dmat, BUS_DMA_NOWAIT,
606 if_printf(ifp, "unable to create dmamap for tx "
607 "buffer %u, error %u\n", i, error);
610 STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
618 malo_desc_cleanup(struct malo_softc *sc, struct malo_descdma *dd)
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);
624 memset(dd, 0, sizeof(*dd));
628 malo_rxdma_cleanup(struct malo_softc *sc)
630 struct malo_rxbuf *bf;
632 STAILQ_FOREACH(bf, &sc->malo_rxbuf, bf_list) {
633 if (bf->bf_m != NULL) {
637 if (bf->bf_dmamap != NULL) {
638 bus_dmamap_destroy(sc->malo_dmat, bf->bf_dmamap);
639 bf->bf_dmamap = NULL;
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;
647 if (sc->malo_rxdma.dd_desc_len != 0)
648 malo_desc_cleanup(sc, &sc->malo_rxdma);
652 malo_txdma_cleanup(struct malo_softc *sc, struct malo_txq *txq)
654 struct malo_txbuf *bf;
655 struct ieee80211_node *ni;
657 STAILQ_FOREACH(bf, &txq->free, bf_list) {
658 if (bf->bf_m != NULL) {
666 * Reclaim node reference.
668 ieee80211_free_node(ni);
670 if (bf->bf_dmamap != NULL) {
671 bus_dmamap_destroy(sc->malo_dmat, bf->bf_dmamap);
672 bf->bf_dmamap = NULL;
675 STAILQ_INIT(&txq->free);
677 if (txq->dma.dd_bufptr != NULL) {
678 free(txq->dma.dd_bufptr, M_MALODEV);
679 txq->dma.dd_bufptr = NULL;
681 if (txq->dma.dd_desc_len != 0)
682 malo_desc_cleanup(sc, &txq->dma);
686 malo_dma_cleanup(struct malo_softc *sc)
690 for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
691 malo_txdma_cleanup(sc, &sc->malo_txq[i]);
693 malo_rxdma_cleanup(sc);
697 malo_dma_setup(struct malo_softc *sc)
701 /* rxdma initializing. */
702 error = malo_rxdma_setup(sc);
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]);
710 malo_dma_cleanup(sc);
715 malo_txq_init(sc, &sc->malo_txq[i], i);
722 malo_hal_set_rxtxdma(struct malo_softc *sc)
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);
731 for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
733 sc->malo_hwspecs.wcbbase[i], sc->malo_hwdma.wcbbase[i]);
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.
743 malo_setup_hwdma(struct malo_softc *sc)
746 struct malo_txq *txq;
748 sc->malo_hwdma.rxdesc_read = sc->malo_rxdma.dd_desc_paddr;
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;
754 sc->malo_hwdma.maxnum_txwcb = malo_txbuf;
755 sc->malo_hwdma.maxnum_wcb = MALO_NUM_TX_QUEUES;
757 malo_hal_set_rxtxdma(sc);
763 malo_txq_init(struct malo_softc *sc, struct malo_txq *txq, int qnum)
765 struct malo_txbuf *bf, *bn;
766 struct malo_txdesc *ds;
768 MALO_TXQ_LOCK_INIT(sc, txq);
770 txq->txpri = 0; /* XXX */
772 STAILQ_FOREACH(bf, &txq->free, bf_list) {
776 bn = STAILQ_NEXT(bf, bf_list);
778 bn = STAILQ_FIRST(&txq->free);
779 ds->physnext = htole32(bn->bf_daddr);
781 STAILQ_INIT(&txq->active);
785 * Reclaim resources for a setup queue.
788 malo_tx_cleanupq(struct malo_softc *sc, struct malo_txq *txq)
791 MALO_TXQ_LOCK_DESTROY(txq);
795 * Allocate a tx buffer for sending a frame.
797 static struct malo_txbuf *
798 malo_getbuf(struct malo_softc *sc, struct malo_txq *txq)
800 struct malo_txbuf *bf;
803 bf = STAILQ_FIRST(&txq->free);
805 STAILQ_REMOVE_HEAD(&txq->free, bf_list);
808 MALO_TXQ_UNLOCK(txq);
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++;
818 malo_tx_dmasetup(struct malo_softc *sc, struct malo_txbuf *bf, struct mbuf *m0)
824 * Load the DMA map so any coalescing is done. This also calculates
825 * the number of descriptors we need.
827 error = bus_dmamap_load_mbuf_sg(sc->malo_dmat, bf->bf_dmamap, m0,
828 bf->bf_segs, &bf->bf_nseg,
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++;
839 * Discard null packets and check for packets that require too many
840 * TX descriptors. We try to convert the latter to a cluster.
842 if (error == EFBIG) { /* too many desc's, linearize */
843 sc->malo_stats.mst_tx_linear++;
844 m = m_defrag(m0, M_NOWAIT);
847 sc->malo_stats.mst_tx_nombuf++;
851 error = bus_dmamap_load_mbuf_sg(sc->malo_dmat, bf->bf_dmamap, m0,
852 bf->bf_segs, &bf->bf_nseg,
855 sc->malo_stats.mst_tx_busdma++;
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++;
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);
876 malo_printrxbuf(const struct malo_rxbuf *bf, u_int ix)
878 const struct malo_rxdesc *ds = bf->bf_desc;
879 uint32_t status = le32toh(ds->status);
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),
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));
893 malo_printtxbuf(const struct malo_txbuf *bf, u_int qnum, u_int ix)
895 const struct malo_txdesc *ds = bf->bf_desc;
896 uint32_t status = le32toh(ds->status);
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));
910 const uint8_t *cp = (const uint8_t *) ds;
912 for (i = 0; i < sizeof(struct malo_txdesc); i++) {
913 printf("%02x ", cp[i]);
914 if (((i+1) % 16) == 0)
921 #endif /* MALO_DEBUG */
924 malo_updatetxrate(struct ieee80211_node *ni, int rix)
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];
935 malo_fix2rate(int fix_rate)
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);
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)
949 * Process completed xmit descriptors from the specified queue.
952 malo_tx_processq(struct malo_softc *sc, struct malo_txq *txq)
954 struct malo_txbuf *bf;
955 struct malo_txdesc *ds;
956 struct ieee80211_node *ni;
960 DPRINTF(sc, MALO_DEBUG_TX_PROC, "%s: tx queue %u\n",
961 __func__, txq->qnum);
962 for (nreaped = 0;; nreaped++) {
964 bf = STAILQ_FIRST(&txq->active);
966 MALO_TXQ_UNLOCK(txq);
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);
976 STAILQ_REMOVE_HEAD(&txq->active, bf_list);
977 MALO_TXQ_UNLOCK(txq);
980 if (sc->malo_debug & MALO_DEBUG_XMIT_DESC)
981 malo_printtxbuf(bf, txq->qnum, nreaped);
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);
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;
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++;
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
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);
1017 * Reclaim reference to node.
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.
1023 ieee80211_free_node(ni);
1025 ds->status = htole32(MALO_TXD_STATUS_IDLE);
1026 ds->pktlen = htole32(0);
1028 bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap,
1029 BUS_DMASYNC_POSTWRITE);
1030 bus_dmamap_unload(sc->malo_dmat, bf->bf_dmamap);
1036 STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
1038 MALO_TXQ_UNLOCK(txq);
1044 * Deferred processing of transmit interrupt.
1047 malo_tx_proc(void *arg, int npending)
1049 struct malo_softc *sc = arg;
1050 struct ifnet *ifp = sc->malo_ifp;
1054 * Process each active queue.
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]);
1063 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1070 malo_tx_start(struct malo_softc *sc, struct ieee80211_node *ni,
1071 struct malo_txbuf *bf, struct mbuf *m0)
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;
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)) {
1096 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos);
1097 copyhdrlen -= sizeof(qos);
1100 (((struct ieee80211_qosframe *) wh)->i_qos);
1105 struct ieee80211_key *k;
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.
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.
1117 k = ieee80211_crypto_encap(ni, m0);
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.
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.
1134 pktlen = m0->m_pkthdr.len;
1136 /* packet header may have moved, reset our local pointer */
1137 wh = mtod(m0, struct ieee80211_frame *);
1140 if (ieee80211_radiotap_active_vap(vap)) {
1141 sc->malo_tx_th.wt_flags = 0; /* XXX */
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;
1147 ieee80211_radiotap_tx(vap, m0);
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
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);
1173 M_PREPEND(m0, space, M_NOWAIT);
1175 tr = mtod(m0, struct malo_txrec *);
1176 if (wh != (struct ieee80211_frame *) &tr->wh)
1177 ovbcopy(wh, &tr->wh, hdrlen);
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!
1184 tr->fwlen = htole16(pktlen - hdrlen);
1187 * Load the DMA map so any coalescing is done. This
1188 * also calculates the number of descriptors we need.
1190 error = malo_tx_dmasetup(sc, bf, m0);
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;
1199 * Formulate tx descriptor.
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;
1213 * Select transmit rate.
1215 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1216 case IEEE80211_FC0_TYPE_MGT:
1217 sc->malo_stats.mst_tx_mgmt++;
1219 case IEEE80211_FC0_TYPE_CTL:
1222 case IEEE80211_FC0_TYPE_DATA:
1223 ds->txpriority = txq->qnum;
1226 if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1227 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
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);
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);
1247 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1249 MALO_TXQ_UNLOCK(txq);
1251 #undef IEEE80211_DIR_DSTODS
1255 malo_start(struct ifnet *ifp)
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;
1264 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->malo_invalid)
1268 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1271 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1272 bf = malo_getbuf(sc, txq);
1274 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1276 /* XXX blocks other traffic */
1277 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1278 sc->malo_stats.mst_tx_qstop++;
1282 * Pass the frame to the h/w for transmission.
1284 if (malo_tx_start(sc, ni, bf, m)) {
1285 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1290 STAILQ_INSERT_HEAD(&txq->free, bf, bf_list);
1291 MALO_TXQ_UNLOCK(txq);
1293 ieee80211_free_node(ni);
1298 if (nqueued >= malo_txcoalesce) {
1300 * Poke the firmware to process queued frames;
1301 * see below about (lack of) locking.
1304 malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
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.
1320 * NB: the queue id isn't used so 0 is ok.
1322 malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
1327 malo_watchdog(void *arg)
1329 struct malo_softc *sc;
1333 callout_reset(&sc->malo_watchdog_timer, hz, malo_watchdog, sc);
1334 if (sc->malo_timer == 0 || --sc->malo_timer > 0)
1338 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) && !sc->malo_invalid) {
1339 if_printf(ifp, "watchdog timeout\n");
1341 /* XXX no way to reset h/w. now */
1343 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1344 sc->malo_stats.mst_watchdog++;
1349 malo_hal_reset(struct malo_softc *sc)
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;
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.
1362 malo_hal_set_rxtxdma(sc);
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);
1371 /* XXX needs other stuffs? */
1376 static __inline struct mbuf *
1377 malo_getrxmbuf(struct malo_softc *sc, struct malo_rxbuf *bf)
1383 /* XXX don't need mbuf, just dma buffer */
1384 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1386 sc->malo_stats.mst_rx_nombuf++; /* XXX */
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);
1393 if_printf(sc->malo_ifp,
1394 "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1398 bf->bf_data = paddr;
1399 bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
1405 malo_rxbuf_init(struct malo_softc *sc, struct malo_rxbuf *bf)
1407 struct malo_rxdesc *ds;
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);
1426 ds->status = MALO_RXD_STATUS_IDLE;
1428 ds->pktlen = htole16(MALO_RXSIZE);
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);
1439 * Setup the rx data structures. This should only be done once or we may get
1440 * out of sync with the firmware.
1443 malo_startrecv(struct malo_softc *sc)
1445 struct malo_rxbuf *bf, *prev;
1446 struct malo_rxdesc *ds;
1448 if (sc->malo_recvsetup == 1) {
1449 malo_mode_init(sc); /* set filters, etc. */
1454 STAILQ_FOREACH(bf, &sc->malo_rxbuf, bf_list) {
1455 int error = malo_rxbuf_init(sc, bf);
1457 DPRINTF(sc, MALO_DEBUG_RECV,
1458 "%s: malo_rxbuf_init failed %d\n",
1464 ds->physnext = htole32(bf->bf_daddr);
1471 htole32(STAILQ_FIRST(&sc->malo_rxbuf)->bf_daddr);
1474 sc->malo_recvsetup = 1;
1476 malo_mode_init(sc); /* set filters, etc. */
1482 malo_init_locked(struct malo_softc *sc)
1484 struct ifnet *ifp = sc->malo_ifp;
1485 struct malo_hal *mh = sc->malo_mh;
1488 DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags 0x%x\n",
1489 __func__, ifp->if_flags);
1491 MALO_LOCK_ASSERT(sc);
1494 * Stop anything previously setup. This is safe whether this is
1495 * the first time through or not.
1497 malo_stop_locked(ifp, 0);
1500 * Push state to the firmware.
1502 if (!malo_hal_reset(sc)) {
1503 if_printf(ifp, "%s: unable to reset hardware\n", __func__);
1508 * Setup recv (once); transmit is already good to go.
1510 error = malo_startrecv(sc);
1512 if_printf(ifp, "%s: unable to start recv logic, error %d\n",
1518 * Enable interrupts.
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;
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);
1535 malo_init(void *arg)
1537 struct malo_softc *sc = (struct malo_softc *) arg;
1538 struct ifnet *ifp = sc->malo_ifp;
1539 struct ieee80211com *ic = ifp->if_l2com;
1541 DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags 0x%x\n",
1542 __func__, ifp->if_flags);
1545 malo_init_locked(sc);
1549 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1550 ieee80211_start_all(ic); /* start all vap's */
1554 * Set the multicast filter contents into the hardware.
1557 malo_setmcastfilter(struct malo_softc *sc)
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];
1569 if (ic->ic_opmode == IEEE80211_M_MONITOR ||
1570 (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)))
1573 if_maddr_rlock(ifp);
1574 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1575 if (ifma->ifma_addr->sa_family != AF_LINK)
1578 if (nmc == MALO_HAL_MCAST_MAX) {
1579 ifp->if_flags |= IFF_ALLMULTI;
1580 if_maddr_runlock(ifp);
1583 IEEE80211_ADDR_COPY(mp,
1584 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1586 mp += IEEE80211_ADDR_LEN, nmc++;
1588 if_maddr_runlock(ifp);
1590 malo_hal_setmcast(sc->malo_mh, nmc, macs);
1594 * XXX we don't know how to set the f/w for supporting
1595 * IFF_ALLMULTI | IFF_PROMISC cases
1601 malo_mode_init(struct malo_softc *sc)
1603 struct ifnet *ifp = sc->malo_ifp;
1604 struct ieee80211com *ic = ifp->if_l2com;
1605 struct malo_hal *mh = sc->malo_mh;
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.
1613 malo_hal_setpromisc(mh, (ifp->if_flags & IFF_PROMISC) &&
1614 ic->ic_opmode != IEEE80211_M_HOSTAP);
1615 malo_setmcastfilter(sc);
1621 malo_tx_draintxq(struct malo_softc *sc, struct malo_txq *txq)
1623 struct ieee80211_node *ni;
1624 struct malo_txbuf *bf;
1628 * NB: this assumes output has been stopped and
1629 * we do not need to block malo_tx_tasklet
1631 for (ix = 0;; ix++) {
1633 bf = STAILQ_FIRST(&txq->active);
1635 MALO_TXQ_UNLOCK(txq);
1638 STAILQ_REMOVE_HEAD(&txq->active, bf_list);
1639 MALO_TXQ_UNLOCK(txq);
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);
1650 #endif /* MALO_DEBUG */
1651 bus_dmamap_unload(sc->malo_dmat, bf->bf_dmamap);
1656 * Reclaim node reference.
1658 ieee80211_free_node(ni);
1664 STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
1666 MALO_TXQ_UNLOCK(txq);
1671 malo_stop_locked(struct ifnet *ifp, int disable)
1673 struct malo_softc *sc = ifp->if_softc;
1674 struct malo_hal *mh = sc->malo_mh;
1677 DPRINTF(sc, MALO_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1678 __func__, sc->malo_invalid, ifp->if_flags);
1680 MALO_LOCK_ASSERT(sc);
1682 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1686 * Shutdown the hardware and driver:
1687 * disable interrupts
1688 * turn off the radio
1689 * drain and release tx queues
1691 * Note that some of this work is not possible if the hardware
1692 * is gone (invalid).
1694 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1695 callout_stop(&sc->malo_watchdog_timer);
1697 /* diable interrupt. */
1698 malo_hal_intrset(mh, 0);
1699 /* turn off the radio. */
1700 malo_hal_setradio(mh, 0, MHP_AUTO_PREAMBLE);
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]);
1708 malo_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
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;
1720 if (MALO_IS_RUNNING(ifp)) {
1722 * To avoid rescanning another access point,
1723 * do not call malo_init() here. Instead,
1724 * only reflect promisc mode settings.
1727 } else if (ifp->if_flags & IFF_UP) {
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.
1737 if (!sc->malo_invalid) {
1738 malo_init_locked(sc);
1742 malo_stop_locked(ifp, 1);
1746 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1749 error = ether_ioctl(ifp, cmd, data);
1755 ieee80211_start_all(ic);
1757 #undef MALO_IS_RUNNING
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.
1767 malo_updateslot(struct ifnet *ifp)
1769 struct malo_softc *sc = ifp->if_softc;
1770 struct ieee80211com *ic = ifp->if_l2com;
1771 struct malo_hal *mh = sc->malo_mh;
1774 /* NB: can be called early; suppress needless cmds */
1775 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
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);
1783 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1784 error = malo_hal_set_slot(mh, 1);
1786 error = malo_hal_set_slot(mh, 0);
1789 device_printf(sc->malo_dev, "setting %s slot failed\n",
1790 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long");
1794 malo_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
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;
1801 DPRINTF(sc, MALO_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1802 ieee80211_state_name[vap->iv_state],
1803 ieee80211_state_name[nstate]);
1806 * Invoke the net80211 layer first so iv_bss is setup.
1808 error = MALO_VAP(vap)->malo_newstate(vap, nstate, arg);
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];
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);
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));
1837 malo_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1838 const struct ieee80211_bpf_params *params)
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;
1846 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->malo_invalid) {
1847 ieee80211_free_node(ni);
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.
1858 txq = &sc->malo_txq[0];
1859 bf = malo_getbuf(sc, txq);
1861 /* XXX blocks other traffic */
1862 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1863 ieee80211_free_node(ni);
1869 * Pass the frame to the h/w for transmission.
1871 if (malo_tx_start(sc, ni, bf, m) != 0) {
1872 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1876 STAILQ_INSERT_HEAD(&txq->free, bf, bf_list);
1878 MALO_TXQ_UNLOCK(txq);
1880 ieee80211_free_node(ni);
1881 return EIO; /* XXX */
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.
1893 * NB: the queue id isn't used so 0 is ok.
1895 malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
1901 malo_sysctlattach(struct malo_softc *sc)
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);
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");
1915 malo_announce(struct malo_softc *sc)
1917 struct ifnet *ifp = sc->malo_ifp;
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);
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);
1934 * Convert net80211 channel to a HAL channel.
1937 malo_mapchan(struct malo_hal_channel *hc, const struct ieee80211_channel *chan)
1939 hc->channel = chan->ic_ieee;
1941 *(uint32_t *)&hc->flags = 0;
1942 if (IEEE80211_IS_CHAN_2GHZ(chan))
1943 hc->flags.freqband = MALO_FREQ_BAND_2DOT4GHZ;
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
1953 malo_chan_set(struct malo_softc *sc, struct ieee80211_channel *chan)
1955 struct malo_hal *mh = sc->malo_mh;
1956 struct malo_hal_channel hchan;
1958 DPRINTF(sc, MALO_DEBUG_RESET, "%s: chan %u MHz/flags 0x%x\n",
1959 __func__, chan->ic_freq, chan->ic_flags);
1962 * Convert to a HAL channel description with the flags constrained
1963 * to reflect the current operating mode.
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);
1971 * Update internal state.
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);
1979 sc->malo_tx_th.wt_chan_flags = htole16(IEEE80211_CHAN_B);
1980 sc->malo_rx_th.wr_chan_flags = htole16(IEEE80211_CHAN_B);
1982 sc->malo_curchan = hchan;
1983 malo_hal_intrset(mh, sc->malo_imask);
1989 malo_scan_start(struct ieee80211com *ic)
1991 struct ifnet *ifp = ic->ic_ifp;
1992 struct malo_softc *sc = ifp->if_softc;
1994 DPRINTF(sc, MALO_DEBUG_STATE, "%s\n", __func__);
1998 malo_scan_end(struct ieee80211com *ic)
2000 struct ifnet *ifp = ic->ic_ifp;
2001 struct malo_softc *sc = ifp->if_softc;
2003 DPRINTF(sc, MALO_DEBUG_STATE, "%s\n", __func__);
2007 malo_set_channel(struct ieee80211com *ic)
2009 struct ifnet *ifp = ic->ic_ifp;
2010 struct malo_softc *sc = ifp->if_softc;
2012 (void) malo_chan_set(sc, ic->ic_curchan);
2016 malo_rx_proc(void *arg, int npending)
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;
2034 DPRINTF(sc, MALO_DEBUG_RX_PROC,
2035 "%s: pending %u rdptr(0x%x) 0x%x wrptr(0x%x) 0x%x\n",
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));
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)
2047 bf = sc->malo_rxnext;
2048 for (ntodo = malo_rxquota; ntodo > 0 && readptr != writeptr; ntodo--) {
2050 bf = STAILQ_FIRST(&sc->malo_rxbuf);
2054 if (bf->bf_m == NULL) {
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.
2062 DPRINTF(sc, MALO_DEBUG_ANY,
2063 "%s: rx buf w/o dma memory\n", __func__);
2064 (void)malo_rxbuf_init(sc, bf);
2067 MALO_RXDESC_SYNC(sc, ds,
2068 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2069 if (ds->rxcontrol != MALO_RXD_CTRL_DMA_OWN)
2072 readptr = le32toh(ds->physnext);
2075 if (sc->malo_debug & MALO_DEBUG_RECV_DESC)
2076 malo_printrxbuf(bf, 0);
2078 status = ds->status;
2079 if (status & MALO_RXD_STATUS_DECRYPT_ERR_MASK) {
2080 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2084 * Sync the data buffer.
2086 len = le16toh(ds->pktlen);
2087 bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap,
2088 BUS_DMASYNC_POSTREAD);
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.
2096 data = mtod(m, uint8_t *);
2097 hdrlen = ieee80211_anyhdrsize(data + sizeof(uint16_t));
2098 off = sizeof(uint16_t) + sizeof(struct ieee80211_frame_addr4);
2101 * Calculate RSSI. XXX wrong
2103 rssi = 2 * ((int) ds->snr - ds->nf); /* NB: .5 dBm */
2107 pktlen = hdrlen + (len - off);
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.
2116 /* XXX don't need mbuf, just dma buffer */
2117 mnew = malo_getrxmbuf(sc, bf);
2119 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
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.
2128 m->m_data += off - hdrlen;
2129 m->m_pkthdr.len = m->m_len = pktlen;
2130 m->m_pkthdr.rcvif = ifp;
2133 * Piece 802.11 header together.
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)) {
2142 struct ieee80211_qosframe_addr4*);
2143 *(uint16_t *)wh4->i_qos = ds->qosctrl;
2145 *(uint16_t *)wh->i_qos = ds->qosctrl;
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;
2155 if (IFF_DUMPPKTS_RECV(sc, wh)) {
2156 ieee80211_dump_pkt(ic, mtod(m, caddr_t),
2157 len, ds->rate, rssi);
2160 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
2163 ni = ieee80211_find_rxnode(ic,
2164 (struct ieee80211_frame_min *)wh);
2166 (void) ieee80211_input(ni, m, rssi, ds->nf);
2167 ieee80211_free_node(ni);
2169 (void) ieee80211_input_all(ic, m, rssi, ds->nf);
2171 /* NB: ignore ENOMEM so we process more descriptors */
2172 (void) malo_rxbuf_init(sc, bf);
2173 bf = STAILQ_NEXT(bf, bf_list);
2176 malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_read, readptr);
2177 sc->malo_rxnext = bf;
2179 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2180 !IFQ_IS_EMPTY(&ifp->if_snd))
2182 #undef IEEE80211_DIR_DSTODS
2186 malo_stop(struct ifnet *ifp, int disable)
2188 struct malo_softc *sc = ifp->if_softc;
2191 malo_stop_locked(ifp, disable);
2196 * Reclaim all tx queue resources.
2199 malo_tx_cleanup(struct malo_softc *sc)
2203 for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
2204 malo_tx_cleanupq(sc, &sc->malo_txq[i]);
2208 malo_detach(struct malo_softc *sc)
2210 struct ifnet *ifp = sc->malo_ifp;
2211 struct ieee80211com *ic = ifp->if_l2com;
2213 DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags %x\n",
2214 __func__, ifp->if_flags);
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);
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
2235 * Other than that, it's straightforward...
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);
2244 MALO_LOCK_DESTROY(sc);
2250 malo_shutdown(struct malo_softc *sc)
2252 malo_stop(sc->malo_ifp, 1);
2256 malo_suspend(struct malo_softc *sc)
2258 struct ifnet *ifp = sc->malo_ifp;
2260 DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags %x\n",
2261 __func__, ifp->if_flags);
2267 malo_resume(struct malo_softc *sc)
2269 struct ifnet *ifp = sc->malo_ifp;
2271 DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags %x\n",
2272 __func__, ifp->if_flags);
2274 if (ifp->if_flags & IFF_UP)