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$");
37 #include <sys/param.h>
38 #include <sys/endian.h>
39 #include <sys/kernel.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43 #include <sys/taskqueue.h>
45 #include <machine/bus.h>
49 #include <net/if_dl.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/ethernet.h>
54 #include <net80211/ieee80211_var.h>
55 #include <net80211/ieee80211_regdomain.h>
59 #include <dev/malo/if_malo.h>
61 SYSCTL_NODE(_hw, OID_AUTO, malo, CTLFLAG_RD, 0,
62 "Marvell 88w8335 driver parameters");
64 static int malo_txcoalesce = 8; /* # tx pkts to q before poking f/w*/
65 SYSCTL_INT(_hw_malo, OID_AUTO, txcoalesce, CTLFLAG_RW, &malo_txcoalesce,
66 0, "tx buffers to send at once");
67 TUNABLE_INT("hw.malo.txcoalesce", &malo_txcoalesce);
68 static int malo_rxbuf = MALO_RXBUF; /* # rx buffers to allocate */
69 SYSCTL_INT(_hw_malo, OID_AUTO, rxbuf, CTLFLAG_RW, &malo_rxbuf,
70 0, "rx buffers allocated");
71 TUNABLE_INT("hw.malo.rxbuf", &malo_rxbuf);
72 static int malo_rxquota = MALO_RXBUF; /* # max buffers to process */
73 SYSCTL_INT(_hw_malo, OID_AUTO, rxquota, CTLFLAG_RW, &malo_rxquota,
74 0, "max rx buffers to process per interrupt");
75 TUNABLE_INT("hw.malo.rxquota", &malo_rxquota);
76 static int malo_txbuf = MALO_TXBUF; /* # tx buffers to allocate */
77 SYSCTL_INT(_hw_malo, OID_AUTO, txbuf, CTLFLAG_RW, &malo_txbuf,
78 0, "tx buffers allocated");
79 TUNABLE_INT("hw.malo.txbuf", &malo_txbuf);
82 static int malo_debug = 0;
83 SYSCTL_INT(_hw_malo, OID_AUTO, debug, CTLFLAG_RW, &malo_debug,
84 0, "control debugging printfs");
85 TUNABLE_INT("hw.malo.debug", &malo_debug);
87 MALO_DEBUG_XMIT = 0x00000001, /* basic xmit operation */
88 MALO_DEBUG_XMIT_DESC = 0x00000002, /* xmit descriptors */
89 MALO_DEBUG_RECV = 0x00000004, /* basic recv operation */
90 MALO_DEBUG_RECV_DESC = 0x00000008, /* recv descriptors */
91 MALO_DEBUG_RESET = 0x00000010, /* reset processing */
92 MALO_DEBUG_INTR = 0x00000040, /* ISR */
93 MALO_DEBUG_TX_PROC = 0x00000080, /* tx ISR proc */
94 MALO_DEBUG_RX_PROC = 0x00000100, /* rx ISR proc */
95 MALO_DEBUG_STATE = 0x00000400, /* 802.11 state transitions */
96 MALO_DEBUG_NODE = 0x00000800, /* node management */
97 MALO_DEBUG_RECV_ALL = 0x00001000, /* trace all frames (beacons) */
98 MALO_DEBUG_FW = 0x00008000, /* firmware */
99 MALO_DEBUG_ANY = 0xffffffff
101 #define IS_BEACON(wh) \
102 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | \
103 IEEE80211_FC0_SUBTYPE_MASK)) == \
104 (IEEE80211_FC0_TYPE_MGT|IEEE80211_FC0_SUBTYPE_BEACON))
105 #define IFF_DUMPPKTS_RECV(sc, wh) \
106 (((sc->malo_debug & MALO_DEBUG_RECV) && \
107 ((sc->malo_debug & MALO_DEBUG_RECV_ALL) || !IS_BEACON(wh))) || \
108 (sc->malo_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == \
109 (IFF_DEBUG|IFF_LINK2))
110 #define IFF_DUMPPKTS_XMIT(sc) \
111 ((sc->malo_debug & MALO_DEBUG_XMIT) || \
112 (sc->malo_ifp->if_flags & (IFF_DEBUG | IFF_LINK2)) == \
113 (IFF_DEBUG | IFF_LINK2))
114 #define DPRINTF(sc, m, fmt, ...) do { \
115 if (sc->malo_debug & (m)) \
116 printf(fmt, __VA_ARGS__); \
119 #define DPRINTF(sc, m, fmt, ...) do { \
124 MALLOC_DEFINE(M_MALODEV, "malodev", "malo driver dma buffers");
126 static int malo_dma_setup(struct malo_softc *);
127 static int malo_setup_hwdma(struct malo_softc *);
128 static void malo_txq_init(struct malo_softc *, struct malo_txq *, int);
129 static void malo_tx_cleanupq(struct malo_softc *, struct malo_txq *);
130 static void malo_start(struct ifnet *);
131 static void malo_watchdog(struct ifnet *);
132 static int malo_ioctl(struct ifnet *, u_long, caddr_t);
133 static void malo_updateslot(struct ifnet *);
134 static int malo_newstate(struct ieee80211com *, enum ieee80211_state, int);
135 static void malo_scan_start(struct ieee80211com *);
136 static void malo_scan_end(struct ieee80211com *);
137 static void malo_set_channel(struct ieee80211com *);
138 static int malo_raw_xmit(struct ieee80211_node *, struct mbuf *,
139 const struct ieee80211_bpf_params *);
140 static int malo_media_change(struct ifnet *);
141 static void malo_bpfattach(struct malo_softc *);
142 static void malo_sysctlattach(struct malo_softc *);
143 static void malo_announce(struct malo_softc *);
144 static void malo_dma_cleanup(struct malo_softc *);
145 static void malo_stop_locked(struct ifnet *, int);
146 static int malo_chan_set(struct malo_softc *, struct ieee80211_channel *);
147 static int malo_mode_init(struct malo_softc *);
148 static void malo_tx_proc(void *, int);
149 static void malo_rx_proc(void *, int);
150 static void malo_init(void *);
153 * Read/Write shorthands for accesses to BAR 0. Note that all BAR 1
154 * operations are done in the "hal" except getting H/W MAC address at
155 * malo_attach and there should be no reference to them here.
158 malo_bar0_read4(struct malo_softc *sc, bus_size_t off)
160 return bus_space_read_4(sc->malo_io0t, sc->malo_io0h, off);
164 malo_bar0_write4(struct malo_softc *sc, bus_size_t off, uint32_t val)
166 DPRINTF(sc, MALO_DEBUG_FW, "%s: off 0x%x val 0x%x\n",
169 bus_space_write_4(sc->malo_io0t, sc->malo_io0h, off, val);
173 malo_bar1_read1(struct malo_softc *sc, bus_size_t off)
175 return bus_space_read_1(sc->malo_io1t, sc->malo_io1h, off);
179 malo_attach(uint16_t devid, struct malo_softc *sc)
182 struct ieee80211com *ic = &sc->malo_ic;
187 ifp = sc->malo_ifp = if_alloc(IFT_ETHER);
189 device_printf(sc->malo_dev, "can not if_alloc()\n");
195 /* set these up early for if_printf use */
196 if_initname(ifp, device_get_name(sc->malo_dev),
197 device_get_unit(sc->malo_dev));
200 * NB: get mac address from hardware directly here before we set DMAs
201 * for HAL because we don't want to disturb operations of HAL at BAR 1.
203 for (i = 0; i < IEEE80211_ADDR_LEN; i++) {
204 /* XXX remove a magic number but we don't have documents. */
205 ic->ic_myaddr[i] = malo_bar1_read1(sc, 0xa528 + i);
209 mh = malo_hal_attach(sc->malo_dev, devid,
210 sc->malo_io1h, sc->malo_io1t, sc->malo_dmat);
212 if_printf(ifp, "unable to attach HAL\n");
218 sc->malo_txantenna = 0x2; /* h/w default */
219 sc->malo_rxantenna = 0xffff; /* h/w default */
222 * Allocate tx + rx descriptors and populate the lists.
223 * We immediately push the information to the firmware
224 * as otherwise it gets upset.
226 error = malo_dma_setup(sc);
228 if_printf(ifp, "failed to setup descriptors: %d\n", error);
232 sc->malo_tq = taskqueue_create_fast("malo_taskq", M_NOWAIT,
233 taskqueue_thread_enqueue, &sc->malo_tq);
234 taskqueue_start_threads(&sc->malo_tq, 1, PI_NET,
235 "%s taskq", ifp->if_xname);
237 TASK_INIT(&sc->malo_rxtask, 0, malo_rx_proc, sc);
238 TASK_INIT(&sc->malo_txtask, 0, malo_tx_proc, sc);
241 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
242 ifp->if_start = malo_start;
243 ifp->if_watchdog = malo_watchdog;
244 ifp->if_ioctl = malo_ioctl;
245 ifp->if_init = malo_init;
246 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
247 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
248 IFQ_SET_READY(&ifp->if_snd);
250 /* NB: firmware looks that it does not export regdomain info API. */
252 setbit(&bands, IEEE80211_MODE_11B);
253 setbit(&bands, IEEE80211_MODE_11G);
254 ieee80211_init_channels(ic, 0, CTRY_DEFAULT, bands, 0, 1);
257 /* XXX not right but it's not used anywhere important */
258 ic->ic_phytype = IEEE80211_T_OFDM;
259 ic->ic_opmode = IEEE80211_M_STA;
261 IEEE80211_C_BGSCAN /* capable of bg scanning */
262 | IEEE80211_C_MONITOR /* monitor mode */
263 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
264 | IEEE80211_C_SHSLOT /* short slot time supported */
265 | IEEE80211_C_TXPMGT /* capable of txpow mgt */
266 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */
270 * Transmit requires space in the packet for a special format transmit
271 * record and optional padding between this record and the payload.
272 * Ask the net80211 layer to arrange this when encapsulating
273 * packets so we can add it efficiently.
275 ic->ic_headroom = sizeof(struct malo_txrec) -
276 sizeof(struct ieee80211_frame);
278 /* call MI attach routine. */
279 ieee80211_ifattach(ic);
280 /* override default methods */
281 ic->ic_updateslot = malo_updateslot;
282 ic->ic_raw_xmit = malo_raw_xmit;
284 sc->malo_newstate = ic->ic_newstate;
285 ic->ic_newstate = malo_newstate;
287 ic->ic_scan_start = malo_scan_start;
288 ic->ic_scan_end = malo_scan_end;
289 ic->ic_set_channel = malo_set_channel;
291 /* complete initialization */
292 ieee80211_media_init(ic, malo_media_change, ieee80211_media_status);
294 sc->malo_invalid = 0; /* ready to go, enable int handling */
299 * Setup dynamic sysctl's.
301 malo_sysctlattach(sc);
304 ieee80211_announce(ic);
311 sc->malo_invalid = 1;
319 struct malo_softc *sc = arg;
320 struct malo_hal *mh = sc->malo_mh;
323 if (sc->malo_invalid) {
325 * The hardware is not ready/present, don't touch anything.
326 * Note this can happen early on if the IRQ is shared.
328 DPRINTF(sc, MALO_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
329 return (FILTER_STRAY);
333 * Figure out the reason(s) for the interrupt.
335 malo_hal_getisr(mh, &status); /* NB: clears ISR too */
336 if (status == 0) /* must be a shared irq */
337 return (FILTER_STRAY);
339 DPRINTF(sc, MALO_DEBUG_INTR, "%s: status 0x%x imask 0x%x\n",
340 __func__, status, sc->malo_imask);
342 if (status & MALO_A2HRIC_BIT_RX_RDY)
343 taskqueue_enqueue_fast(sc->malo_tq, &sc->malo_rxtask);
344 if (status & MALO_A2HRIC_BIT_TX_DONE)
345 taskqueue_enqueue_fast(sc->malo_tq, &sc->malo_txtask);
346 if (status & MALO_A2HRIC_BIT_OPC_DONE)
347 malo_hal_cmddone(mh);
348 if (status & MALO_A2HRIC_BIT_MAC_EVENT)
350 if (status & MALO_A2HRIC_BIT_RX_PROBLEM)
352 if (status & MALO_A2HRIC_BIT_ICV_ERROR) {
354 sc->malo_stats.mst_rx_badtkipicv++;
358 if (((status | sc->malo_imask) ^ sc->malo_imask) != 0)
359 DPRINTF(sc, MALO_DEBUG_INTR,
360 "%s: can't handle interrupt status 0x%x\n",
364 return (FILTER_HANDLED);
368 malo_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
370 bus_addr_t *paddr = (bus_addr_t*) arg;
372 KASSERT(error == 0, ("error %u on bus_dma callback", error));
374 *paddr = segs->ds_addr;
378 malo_desc_setup(struct malo_softc *sc, const char *name,
379 struct malo_descdma *dd,
380 int nbuf, size_t bufsize, int ndesc, size_t descsize)
383 struct ifnet *ifp = sc->malo_ifp;
386 DPRINTF(sc, MALO_DEBUG_RESET,
387 "%s: %s DMA: %u bufs (%ju) %u desc/buf (%ju)\n",
388 __func__, name, nbuf, (uintmax_t) bufsize,
389 ndesc, (uintmax_t) descsize);
392 dd->dd_desc_len = nbuf * ndesc * descsize;
395 * Setup DMA descriptor area.
397 error = bus_dma_tag_create(bus_get_dma_tag(sc->malo_dev),/* parent */
398 PAGE_SIZE, 0, /* alignment, bounds */
399 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
400 BUS_SPACE_MAXADDR, /* highaddr */
401 NULL, NULL, /* filter, filterarg */
402 dd->dd_desc_len, /* maxsize */
404 dd->dd_desc_len, /* maxsegsize */
405 BUS_DMA_ALLOCNOW, /* flags */
410 if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
414 /* allocate descriptors */
415 error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap);
417 if_printf(ifp, "unable to create dmamap for %s descriptors, "
418 "error %u\n", dd->dd_name, error);
422 error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
423 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dd->dd_dmamap);
425 if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
426 "error %u\n", nbuf * ndesc, dd->dd_name, error);
430 error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
431 dd->dd_desc, dd->dd_desc_len,
432 malo_load_cb, &dd->dd_desc_paddr, BUS_DMA_NOWAIT);
434 if_printf(ifp, "unable to map %s descriptors, error %u\n",
440 memset(ds, 0, dd->dd_desc_len);
441 DPRINTF(sc, MALO_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
442 __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
443 (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
447 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
449 bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
451 bus_dma_tag_destroy(dd->dd_dmat);
452 memset(dd, 0, sizeof(*dd));
456 #define DS2PHYS(_dd, _ds) \
457 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
460 malo_rxdma_setup(struct malo_softc *sc)
462 struct ifnet *ifp = sc->malo_ifp;
464 struct malo_rxbuf *bf;
465 struct malo_rxdesc *ds;
467 error = malo_desc_setup(sc, "rx", &sc->malo_rxdma,
468 malo_rxbuf, sizeof(struct malo_rxbuf),
469 1, sizeof(struct malo_rxdesc));
474 * Allocate rx buffers and set them up.
476 bsize = malo_rxbuf * sizeof(struct malo_rxbuf);
477 bf = malloc(bsize, M_MALODEV, M_NOWAIT | M_ZERO);
479 if_printf(ifp, "malloc of %u rx buffers failed\n", bsize);
482 sc->malo_rxdma.dd_bufptr = bf;
484 STAILQ_INIT(&sc->malo_rxbuf);
485 ds = sc->malo_rxdma.dd_desc;
486 for (i = 0; i < malo_rxbuf; i++, bf++, ds++) {
488 bf->bf_daddr = DS2PHYS(&sc->malo_rxdma, ds);
489 error = bus_dmamap_create(sc->malo_dmat, BUS_DMA_NOWAIT,
492 if_printf(ifp, "%s: unable to dmamap for rx buffer, "
493 "error %d\n", __func__, error);
496 /* NB: tail is intentional to preserve descriptor order */
497 STAILQ_INSERT_TAIL(&sc->malo_rxbuf, bf, bf_list);
503 malo_txdma_setup(struct malo_softc *sc, struct malo_txq *txq)
505 struct ifnet *ifp = sc->malo_ifp;
507 struct malo_txbuf *bf;
508 struct malo_txdesc *ds;
510 error = malo_desc_setup(sc, "tx", &txq->dma,
511 malo_txbuf, sizeof(struct malo_txbuf),
512 MALO_TXDESC, sizeof(struct malo_txdesc));
516 /* allocate and setup tx buffers */
517 bsize = malo_txbuf * sizeof(struct malo_txbuf);
518 bf = malloc(bsize, M_MALODEV, M_NOWAIT | M_ZERO);
520 if_printf(ifp, "malloc of %u tx buffers failed\n",
524 txq->dma.dd_bufptr = bf;
526 STAILQ_INIT(&txq->free);
528 ds = txq->dma.dd_desc;
529 for (i = 0; i < malo_txbuf; i++, bf++, ds += MALO_TXDESC) {
531 bf->bf_daddr = DS2PHYS(&txq->dma, ds);
532 error = bus_dmamap_create(sc->malo_dmat, BUS_DMA_NOWAIT,
535 if_printf(ifp, "unable to create dmamap for tx "
536 "buffer %u, error %u\n", i, error);
539 STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
547 malo_desc_cleanup(struct malo_softc *sc, struct malo_descdma *dd)
549 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
550 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
551 bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
552 bus_dma_tag_destroy(dd->dd_dmat);
554 memset(dd, 0, sizeof(*dd));
558 malo_rxdma_cleanup(struct malo_softc *sc)
560 struct malo_rxbuf *bf;
562 STAILQ_FOREACH(bf, &sc->malo_rxbuf, bf_list) {
563 if (bf->bf_m != NULL) {
567 if (bf->bf_dmamap != NULL) {
568 bus_dmamap_destroy(sc->malo_dmat, bf->bf_dmamap);
569 bf->bf_dmamap = NULL;
572 STAILQ_INIT(&sc->malo_rxbuf);
573 if (sc->malo_rxdma.dd_bufptr != NULL) {
574 free(sc->malo_rxdma.dd_bufptr, M_MALODEV);
575 sc->malo_rxdma.dd_bufptr = NULL;
577 if (sc->malo_rxdma.dd_desc_len != 0)
578 malo_desc_cleanup(sc, &sc->malo_rxdma);
582 malo_txdma_cleanup(struct malo_softc *sc, struct malo_txq *txq)
584 struct malo_txbuf *bf;
585 struct ieee80211_node *ni;
587 STAILQ_FOREACH(bf, &txq->free, bf_list) {
588 if (bf->bf_m != NULL) {
596 * Reclaim node reference.
598 ieee80211_free_node(ni);
600 if (bf->bf_dmamap != NULL) {
601 bus_dmamap_destroy(sc->malo_dmat, bf->bf_dmamap);
602 bf->bf_dmamap = NULL;
605 STAILQ_INIT(&txq->free);
607 if (txq->dma.dd_bufptr != NULL) {
608 free(txq->dma.dd_bufptr, M_MALODEV);
609 txq->dma.dd_bufptr = NULL;
611 if (txq->dma.dd_desc_len != 0)
612 malo_desc_cleanup(sc, &txq->dma);
616 malo_dma_cleanup(struct malo_softc *sc)
620 for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
621 malo_txdma_cleanup(sc, &sc->malo_txq[i]);
623 malo_rxdma_cleanup(sc);
627 malo_dma_setup(struct malo_softc *sc)
631 /* rxdma initializing. */
632 error = malo_rxdma_setup(sc);
636 /* NB: we just have 1 tx queue now. */
637 for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
638 error = malo_txdma_setup(sc, &sc->malo_txq[i]);
640 malo_dma_cleanup(sc);
645 malo_txq_init(sc, &sc->malo_txq[i], i);
652 malo_hal_set_rxtxdma(struct malo_softc *sc)
656 malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_read,
657 sc->malo_hwdma.rxdesc_read);
658 malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_write,
659 sc->malo_hwdma.rxdesc_read);
661 for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
663 sc->malo_hwspecs.wcbbase[i], sc->malo_hwdma.wcbbase[i]);
668 * Inform firmware of our tx/rx dma setup. The BAR 0 writes below are
669 * for compatibility with older firmware. For current firmware we send
670 * this information with a cmd block via malo_hal_sethwdma.
673 malo_setup_hwdma(struct malo_softc *sc)
676 struct malo_txq *txq;
678 sc->malo_hwdma.rxdesc_read = sc->malo_rxdma.dd_desc_paddr;
680 for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
681 txq = &sc->malo_txq[i];
682 sc->malo_hwdma.wcbbase[i] = txq->dma.dd_desc_paddr;
684 sc->malo_hwdma.maxnum_txwcb = malo_txbuf;
685 sc->malo_hwdma.maxnum_wcb = MALO_NUM_TX_QUEUES;
687 malo_hal_set_rxtxdma(sc);
693 malo_txq_init(struct malo_softc *sc, struct malo_txq *txq, int qnum)
695 struct malo_txbuf *bf, *bn;
696 struct malo_txdesc *ds;
698 MALO_TXQ_LOCK_INIT(sc, txq);
700 txq->txpri = 0; /* XXX */
702 STAILQ_FOREACH(bf, &txq->free, bf_list) {
706 bn = STAILQ_NEXT(bf, bf_list);
708 bn = STAILQ_FIRST(&txq->free);
709 ds->physnext = htole32(bn->bf_daddr);
711 STAILQ_INIT(&txq->active);
715 * Reclaim resources for a setup queue.
718 malo_tx_cleanupq(struct malo_softc *sc, struct malo_txq *txq)
721 MALO_TXQ_LOCK_DESTROY(txq);
725 * Allocate a tx buffer for sending a frame.
727 static struct malo_txbuf *
728 malo_getbuf(struct malo_softc *sc, struct malo_txq *txq)
730 struct malo_txbuf *bf;
733 bf = STAILQ_FIRST(&txq->free);
735 STAILQ_REMOVE_HEAD(&txq->free, bf_list);
738 MALO_TXQ_UNLOCK(txq);
740 DPRINTF(sc, MALO_DEBUG_XMIT,
741 "%s: out of xmit buffers on q %d\n", __func__, txq->qnum);
742 sc->malo_stats.mst_tx_qstop++;
748 malo_tx_dmasetup(struct malo_softc *sc, struct malo_txbuf *bf, struct mbuf *m0)
754 * Load the DMA map so any coalescing is done. This also calculates
755 * the number of descriptors we need.
757 error = bus_dmamap_load_mbuf_sg(sc->malo_dmat, bf->bf_dmamap, m0,
758 bf->bf_segs, &bf->bf_nseg,
760 if (error == EFBIG) {
761 /* XXX packet requires too many descriptors */
762 bf->bf_nseg = MALO_TXDESC + 1;
763 } else if (error != 0) {
764 sc->malo_stats.mst_tx_busdma++;
769 * Discard null packets and check for packets that require too many
770 * TX descriptors. We try to convert the latter to a cluster.
772 if (error == EFBIG) { /* too many desc's, linearize */
773 sc->malo_stats.mst_tx_linear++;
774 m = m_defrag(m0, M_DONTWAIT);
777 sc->malo_stats.mst_tx_nombuf++;
781 error = bus_dmamap_load_mbuf_sg(sc->malo_dmat, bf->bf_dmamap, m0,
782 bf->bf_segs, &bf->bf_nseg,
785 sc->malo_stats.mst_tx_busdma++;
789 KASSERT(bf->bf_nseg <= MALO_TXDESC,
790 ("too many segments after defrag; nseg %u", bf->bf_nseg));
791 } else if (bf->bf_nseg == 0) { /* null packet, discard */
792 sc->malo_stats.mst_tx_nodata++;
796 DPRINTF(sc, MALO_DEBUG_XMIT, "%s: m %p len %u\n",
797 __func__, m0, m0->m_pkthdr.len);
798 bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
806 malo_printrxbuf(const struct malo_rxbuf *bf, u_int ix)
808 const struct malo_rxdesc *ds = bf->bf_desc;
809 uint32_t status = le32toh(ds->status);
811 printf("R[%2u] (DS.V:%p DS.P:%p) NEXT:%08x DATA:%08x RC:%02x%s\n"
812 " STAT:%02x LEN:%04x SNR:%02x NF:%02x CHAN:%02x"
813 " RATE:%02x QOS:%04x\n",
814 ix, ds, (const struct malo_desc *)bf->bf_daddr,
815 le32toh(ds->physnext), le32toh(ds->physbuffdata),
817 ds->rxcontrol != MALO_RXD_CTRL_DRIVER_OWN ?
818 "" : (status & MALO_RXD_STATUS_OK) ? " *" : " !",
819 ds->status, le16toh(ds->pktlen), ds->snr, ds->nf, ds->channel,
820 ds->rate, le16toh(ds->qosctrl));
824 malo_printtxbuf(const struct malo_txbuf *bf, u_int qnum, u_int ix)
826 const struct malo_txdesc *ds = bf->bf_desc;
827 uint32_t status = le32toh(ds->status);
829 printf("Q%u[%3u]", qnum, ix);
830 printf(" (DS.V:%p DS.P:%p)\n",
831 ds, (const struct malo_txdesc *)bf->bf_daddr);
832 printf(" NEXT:%08x DATA:%08x LEN:%04x STAT:%08x%s\n",
833 le32toh(ds->physnext),
834 le32toh(ds->pktptr), le16toh(ds->pktlen), status,
835 status & MALO_TXD_STATUS_USED ?
836 "" : (status & 3) != 0 ? " *" : " !");
837 printf(" RATE:%02x PRI:%x QOS:%04x SAP:%08x FORMAT:%04x\n",
838 ds->datarate, ds->txpriority, le16toh(ds->qosctrl),
839 le32toh(ds->sap_pktinfo), le16toh(ds->format));
842 const uint8_t *cp = (const uint8_t *) ds;
844 for (i = 0; i < sizeof(struct malo_txdesc); i++) {
845 printf("%02x ", cp[i]);
846 if (((i+1) % 16) == 0)
853 #endif /* MALO_DEBUG */
856 malo_updatetxrate(struct ieee80211_node *ni, int rix)
858 #define N(x) (sizeof(x)/sizeof(x[0]))
859 static const int ieeerates[] =
860 { 2, 4, 11, 22, 44, 12, 18, 24, 36, 48, 96, 108 };
861 if (rix < N(ieeerates))
862 ni->ni_txrate = ieeerates[rix];
867 malo_fix2rate(int fix_rate)
869 #define N(x) (sizeof(x)/sizeof(x[0]))
870 static const int rates[] =
871 { 2, 4, 11, 22, 12, 18, 24, 36, 48, 96, 108 };
872 return (fix_rate < N(rates) ? rates[fix_rate] : 0);
876 /* idiomatic shorthands: MS = mask+shift, SM = shift+mask */
877 #define MS(v,x) (((v) & x) >> x##_S)
878 #define SM(v,x) (((v) << x##_S) & x)
881 * Process completed xmit descriptors from the specified queue.
884 malo_tx_processq(struct malo_softc *sc, struct malo_txq *txq)
886 struct malo_txbuf *bf;
887 struct malo_txdesc *ds;
888 struct ieee80211_node *ni;
892 DPRINTF(sc, MALO_DEBUG_TX_PROC, "%s: tx queue %u\n",
893 __func__, txq->qnum);
894 for (nreaped = 0;; nreaped++) {
896 bf = STAILQ_FIRST(&txq->active);
898 MALO_TXQ_UNLOCK(txq);
902 MALO_TXDESC_SYNC(txq, ds,
903 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
904 if (ds->status & htole32(MALO_TXD_STATUS_FW_OWNED)) {
905 MALO_TXQ_UNLOCK(txq);
908 STAILQ_REMOVE_HEAD(&txq->active, bf_list);
909 MALO_TXQ_UNLOCK(txq);
912 if (sc->malo_debug & MALO_DEBUG_XMIT_DESC)
913 malo_printtxbuf(bf, txq->qnum, nreaped);
917 status = le32toh(ds->status);
918 if (status & MALO_TXD_STATUS_OK) {
919 uint16_t format = le16toh(ds->format);
920 uint8_t txant = MS(format, MALO_TXD_ANTENNA);
922 sc->malo_stats.mst_ant_tx[txant]++;
923 if (status & MALO_TXD_STATUS_OK_RETRY)
924 sc->malo_stats.mst_tx_retries++;
925 if (status & MALO_TXD_STATUS_OK_MORE_RETRY)
926 sc->malo_stats.mst_tx_mretries++;
927 malo_updatetxrate(ni, ds->datarate);
928 sc->malo_stats.mst_tx_rate = ds->datarate;
930 if (status & MALO_TXD_STATUS_FAILED_LINK_ERROR)
931 sc->malo_stats.mst_tx_linkerror++;
932 if (status & MALO_TXD_STATUS_FAILED_XRETRY)
933 sc->malo_stats.mst_tx_xretries++;
934 if (status & MALO_TXD_STATUS_FAILED_AGING)
935 sc->malo_stats.mst_tx_aging++;
938 * Do any tx complete callback. Note this must
939 * be done before releasing the node reference.
940 * XXX no way to figure out if frame was ACK'd
942 if (bf->bf_m->m_flags & M_TXCB) {
943 /* XXX strip fw len in case header inspected */
944 m_adj(bf->bf_m, sizeof(uint16_t));
945 ieee80211_process_callback(ni, bf->bf_m,
946 (status & MALO_TXD_STATUS_OK) == 0);
949 * Reclaim reference to node.
951 * NB: the node may be reclaimed here if, for example
952 * this is a DEAUTH message that was sent and the
953 * node was timed out due to inactivity.
955 ieee80211_free_node(ni);
957 ds->status = htole32(MALO_TXD_STATUS_IDLE);
958 ds->pktlen = htole32(0);
960 bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap,
961 BUS_DMASYNC_POSTWRITE);
962 bus_dmamap_unload(sc->malo_dmat, bf->bf_dmamap);
968 STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
970 MALO_TXQ_UNLOCK(txq);
976 * Deferred processing of transmit interrupt.
979 malo_tx_proc(void *arg, int npending)
981 struct malo_softc *sc = arg;
982 struct ifnet *ifp = sc->malo_ifp;
986 * Process each active queue.
989 for (i = 0; i < MALO_NUM_TX_QUEUES; i++) {
990 if (!STAILQ_EMPTY(&sc->malo_txq[i].active))
991 nreaped += malo_tx_processq(sc, &sc->malo_txq[i]);
995 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1002 malo_tx_start(struct malo_softc *sc, struct ieee80211_node *ni,
1003 struct malo_txbuf *bf, struct mbuf *m0)
1005 #define IEEE80211_DIR_DSTODS(wh) \
1006 ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
1007 #define IS_DATA_FRAME(wh) \
1008 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK)) == IEEE80211_FC0_TYPE_DATA)
1009 int error, ismcast, iswep;
1010 int copyhdrlen, hdrlen, pktlen;
1011 struct ieee80211_frame *wh;
1012 struct ieee80211com *ic = &sc->malo_ic;
1013 struct ifnet *ifp = sc->malo_ifp;
1014 struct malo_txdesc *ds;
1015 struct malo_txrec *tr;
1016 struct malo_txq *txq;
1019 wh = mtod(m0, struct ieee80211_frame *);
1020 iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
1021 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1022 copyhdrlen = hdrlen = ieee80211_anyhdrsize(wh);
1023 pktlen = m0->m_pkthdr.len;
1024 if (IEEE80211_QOS_HAS_SEQ(wh)) {
1025 if (IEEE80211_DIR_DSTODS(wh)) {
1027 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos);
1028 copyhdrlen -= sizeof(qos);
1031 (((struct ieee80211_qosframe *) wh)->i_qos);
1036 struct ieee80211_key *k;
1039 * Construct the 802.11 header+trailer for an encrypted
1040 * frame. The only reason this can fail is because of an
1041 * unknown or unsupported cipher/key type.
1043 * NB: we do this even though the firmware will ignore
1044 * what we've done for WEP and TKIP as we need the
1045 * ExtIV filled in for CCMP and this also adjusts
1046 * the headers which simplifies our work below.
1048 k = ieee80211_crypto_encap(ic, ni, m0);
1051 * This can happen when the key is yanked after the
1052 * frame was queued. Just discard the frame; the
1053 * 802.11 layer counts failures and provides
1054 * debugging/diagnostics.
1061 * Adjust the packet length for the crypto additions
1062 * done during encap and any other bits that the f/w
1063 * will add later on.
1065 pktlen = m0->m_pkthdr.len;
1067 /* packet header may have moved, reset our local pointer */
1068 wh = mtod(m0, struct ieee80211_frame *);
1071 if (bpf_peers_present(sc->malo_drvbpf)) {
1072 sc->malo_tx_th.wt_flags = 0; /* XXX */
1074 sc->malo_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1075 sc->malo_tx_th.wt_txpower = ni->ni_txpower;
1076 sc->malo_tx_th.wt_antenna = sc->malo_txantenna;
1078 bpf_mtap2(sc->malo_drvbpf,
1079 &sc->malo_tx_th, sc->malo_tx_th_len, m0);
1083 * Copy up/down the 802.11 header; the firmware requires
1084 * we present a 2-byte payload length followed by a
1085 * 4-address header (w/o QoS), followed (optionally) by
1086 * any WEP/ExtIV header (but only filled in for CCMP).
1087 * We are assured the mbuf has sufficient headroom to
1088 * prepend in-place by the setup of ic_headroom in
1091 if (hdrlen < sizeof(struct malo_txrec)) {
1092 const int space = sizeof(struct malo_txrec) - hdrlen;
1093 if (M_LEADINGSPACE(m0) < space) {
1094 /* NB: should never happen */
1095 device_printf(sc->malo_dev,
1096 "not enough headroom, need %d found %zd, "
1097 "m_flags 0x%x m_len %d\n",
1098 space, M_LEADINGSPACE(m0), m0->m_flags, m0->m_len);
1099 ieee80211_dump_pkt(ic,
1100 mtod(m0, const uint8_t *), m0->m_len, 0, -1);
1105 M_PREPEND(m0, space, M_NOWAIT);
1107 tr = mtod(m0, struct malo_txrec *);
1108 if (wh != (struct ieee80211_frame *) &tr->wh)
1109 ovbcopy(wh, &tr->wh, hdrlen);
1111 * Note: the "firmware length" is actually the length of the fully
1112 * formed "802.11 payload". That is, it's everything except for
1113 * the 802.11 header. In particular this includes all crypto
1114 * material including the MIC!
1116 tr->fwlen = htole16(pktlen - hdrlen);
1119 * Load the DMA map so any coalescing is done. This
1120 * also calculates the number of descriptors we need.
1122 error = malo_tx_dmasetup(sc, bf, m0);
1125 bf->bf_node = ni; /* NB: held reference */
1126 m0 = bf->bf_m; /* NB: may have changed */
1127 tr = mtod(m0, struct malo_txrec *);
1128 wh = (struct ieee80211_frame *)&tr->wh;
1131 * Formulate tx descriptor.
1136 ds->qosctrl = qos; /* NB: already little-endian */
1137 ds->pktptr = htole32(bf->bf_segs[0].ds_addr);
1138 ds->pktlen = htole16(bf->bf_segs[0].ds_len);
1139 /* NB: pPhysNext setup once, don't touch */
1140 ds->datarate = IS_DATA_FRAME(wh) ? 1 : 0;
1141 ds->sap_pktinfo = 0;
1145 * Select transmit rate.
1147 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1148 case IEEE80211_FC0_TYPE_MGT:
1149 sc->malo_stats.mst_tx_mgmt++;
1151 case IEEE80211_FC0_TYPE_CTL:
1154 case IEEE80211_FC0_TYPE_DATA:
1155 ds->txpriority = txq->qnum;
1158 if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1159 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1166 if (IFF_DUMPPKTS_XMIT(sc))
1167 ieee80211_dump_pkt(ic,
1168 mtod(m0, const uint8_t *)+sizeof(uint16_t),
1169 m0->m_len - sizeof(uint16_t), ds->datarate, -1);
1173 if (!IS_DATA_FRAME(wh))
1174 ds->status |= htole32(1);
1175 ds->status |= htole32(MALO_TXD_STATUS_FW_OWNED);
1176 STAILQ_INSERT_TAIL(&txq->active, bf, bf_list);
1177 MALO_TXDESC_SYNC(txq, ds, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1181 MALO_TXQ_UNLOCK(txq);
1183 #undef IEEE80211_DIR_DSTODS
1187 malo_start(struct ifnet *ifp)
1190 struct ether_header *eh;
1191 struct malo_softc *sc = ifp->if_softc;
1192 struct ieee80211_frame *wh;
1193 struct ieee80211_node *ni;
1194 struct ieee80211com *ic = &sc->malo_ic;
1195 struct malo_txbuf *bf = NULL;
1196 struct malo_txq *txq = NULL;
1199 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->malo_invalid)
1204 * Poll the management queue for frames; they
1205 * have priority over normal data frames.
1207 IF_DEQUEUE(&ic->ic_mgtq, m);
1210 * No data frames go out unless we're associated.
1212 if (ic->ic_state != IEEE80211_S_RUN) {
1213 DPRINTF(sc, MALO_DEBUG_XMIT,
1214 "%s: discard data packet, state %s\n",
1216 ieee80211_state_name[ic->ic_state]);
1217 sc->malo_stats.mst_tx_discard++;
1220 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1224 * Cancel any background scan.
1226 if (ic->ic_flags & IEEE80211_F_SCAN)
1227 ieee80211_cancel_scan(ic);
1230 * Find the node for the destination so we can do
1231 * things like power save and fast frames aggregation.
1233 if (m->m_len < sizeof(struct ether_header) &&
1234 (m = m_pullup(m, sizeof(struct ether_header))) ==
1236 ic->ic_stats.is_tx_nobuf++; /* XXX */
1240 eh = mtod(m, struct ether_header *);
1241 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1243 /* NB: ieee80211_find_txnode does stat+msg */
1247 /* calculate priority so we can find the tx queue */
1248 if (ieee80211_classify(ic, m, ni)) {
1249 DPRINTF(sc, MALO_DEBUG_XMIT,
1250 "%s: discard, classification failure\n",
1256 txq = &sc->malo_txq[0];
1258 bf = malo_getbuf(sc, txq);
1260 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1261 ieee80211_free_node(ni);
1263 /* XXX blocks other traffic */
1264 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1265 sc->malo_stats.mst_tx_qstop++;
1270 if (bpf_peers_present(ifp->if_bpf))
1271 bpf_mtap(ifp->if_bpf, m);
1274 * Encapsulate the packet in prep for transmission.
1276 m = ieee80211_encap(ic, m, ni);
1278 DPRINTF(sc, MALO_DEBUG_XMIT,
1279 "%s: encapsulation failure\n", __func__);
1280 sc->malo_stats.mst_tx_encap++;
1285 * Grab a TX buffer and associated resources.
1286 * Note that we depend on the classification
1287 * by the 802.11 layer to get to the right h/w
1288 * queue. Management frames must ALWAYS go on
1289 * queue 1 but we cannot just force that here
1290 * because we may receive non-mgt frames through
1291 * the ic_mgtq (e.g. null data frames).
1293 txq = &sc->malo_txq[0];
1294 bf = malo_getbuf(sc, txq);
1296 IF_PREPEND(&ic->ic_mgtq, m);
1302 * Hack! The referenced node pointer is in the
1303 * rcvif field of the packet header. This is
1304 * placed there by ieee80211_mgmt_output because
1305 * we need to hold the reference with the frame
1306 * and there's no other way (other than packet
1307 * tags which we consider too expensive to use)
1310 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1311 m->m_pkthdr.rcvif = NULL;
1313 wh = mtod(m, struct ieee80211_frame *);
1314 sc->malo_stats.mst_tx_mgmt++;
1316 if (bpf_peers_present(ic->ic_rawbpf))
1317 bpf_mtap(ic->ic_rawbpf, m);
1321 * Pass the frame to the h/w for transmission.
1323 if (malo_tx_start(sc, ni, bf, m)) {
1330 STAILQ_INSERT_HEAD(&txq->free, bf, bf_list);
1331 MALO_TXQ_UNLOCK(txq);
1333 ieee80211_free_node(ni);
1338 if (nqueued >= malo_txcoalesce) {
1340 * Poke the firmware to process queued frames;
1341 * see below about (lack of) locking.
1344 malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
1350 * NB: We don't need to lock against tx done because
1351 * this just prods the firmware to check the transmit
1352 * descriptors. The firmware will also start fetching
1353 * descriptors by itself if it notices new ones are
1354 * present when it goes to deliver a tx done interrupt
1355 * to the host. So if we race with tx done processing
1356 * it's ok. Delivering the kick here rather than in
1357 * malo_tx_start is an optimization to avoid poking the
1358 * firmware for each packet.
1360 * NB: the queue id isn't used so 0 is ok.
1362 malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
1367 malo_watchdog(struct ifnet *ifp)
1369 struct malo_softc *sc = ifp->if_softc;
1371 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) && !sc->malo_invalid) {
1372 if_printf(ifp, "watchdog timeout\n");
1374 /* XXX no way to reset h/w. now */
1377 sc->malo_stats.mst_watchdog++;
1382 malo_hal_reset(struct malo_softc *sc)
1384 static int first = 0;
1385 struct ieee80211com *ic = &sc->malo_ic;
1386 struct malo_hal *mh = sc->malo_mh;
1390 * NB: when the device firstly is initialized, sometimes
1391 * firmware could override rx/tx dma registers so we re-set
1392 * these values once.
1394 malo_hal_set_rxtxdma(sc);
1398 malo_hal_setantenna(mh, MHA_ANTENNATYPE_RX, sc->malo_rxantenna);
1399 malo_hal_setantenna(mh, MHA_ANTENNATYPE_TX, sc->malo_txantenna);
1400 malo_hal_setradio(mh, 1, MHP_AUTO_PREAMBLE);
1401 malo_chan_set(sc, ic->ic_curchan);
1403 /* XXX needs other stuffs? */
1408 static __inline struct mbuf *
1409 malo_getrxmbuf(struct malo_softc *sc, struct malo_rxbuf *bf)
1415 /* XXX don't need mbuf, just dma buffer */
1416 m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1418 sc->malo_stats.mst_rx_nombuf++; /* XXX */
1421 error = bus_dmamap_load(sc->malo_dmat, bf->bf_dmamap,
1422 mtod(m, caddr_t), MJUMPAGESIZE,
1423 malo_load_cb, &paddr, BUS_DMA_NOWAIT);
1425 if_printf(sc->malo_ifp,
1426 "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1430 bf->bf_data = paddr;
1431 bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
1437 malo_rxbuf_init(struct malo_softc *sc, struct malo_rxbuf *bf)
1439 struct malo_rxdesc *ds;
1442 if (bf->bf_m == NULL) {
1443 bf->bf_m = malo_getrxmbuf(sc, bf);
1444 if (bf->bf_m == NULL) {
1445 /* mark descriptor to be skipped */
1446 ds->rxcontrol = MALO_RXD_CTRL_OS_OWN;
1447 /* NB: don't need PREREAD */
1448 MALO_RXDESC_SYNC(sc, ds, BUS_DMASYNC_PREWRITE);
1458 ds->status = MALO_RXD_STATUS_IDLE;
1460 ds->pktlen = htole16(MALO_RXSIZE);
1462 ds->physbuffdata = htole32(bf->bf_data);
1463 /* NB: don't touch pPhysNext, set once */
1464 ds->rxcontrol = MALO_RXD_CTRL_DRIVER_OWN;
1465 MALO_RXDESC_SYNC(sc, ds, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1471 * Setup the rx data structures. This should only be done once or we may get
1472 * out of sync with the firmware.
1475 malo_startrecv(struct malo_softc *sc)
1477 struct malo_rxbuf *bf, *prev;
1478 struct malo_rxdesc *ds;
1480 if (sc->malo_recvsetup == 1) {
1481 malo_mode_init(sc); /* set filters, etc. */
1486 STAILQ_FOREACH(bf, &sc->malo_rxbuf, bf_list) {
1487 int error = malo_rxbuf_init(sc, bf);
1489 DPRINTF(sc, MALO_DEBUG_RECV,
1490 "%s: malo_rxbuf_init failed %d\n",
1496 ds->physnext = htole32(bf->bf_daddr);
1503 htole32(STAILQ_FIRST(&sc->malo_rxbuf)->bf_daddr);
1506 sc->malo_recvsetup = 1;
1508 malo_mode_init(sc); /* set filters, etc. */
1514 malo_init(void *arg)
1516 struct malo_softc *sc = (struct malo_softc *) arg;
1517 struct ieee80211com *ic = &sc->malo_ic;
1518 struct ifnet *ifp = sc->malo_ifp;
1519 struct malo_hal *mh = sc->malo_mh;
1522 DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags 0x%x\n",
1523 __func__, ifp->if_flags);
1525 if (!sc->malo_fw_loaded) {
1527 * Load firmware so we can get setup.
1529 error = malo_hal_fwload(mh, "malo8335-h", "malo8335-m");
1531 if_printf(ifp, "unable to setup firmware\n");
1534 /* XXX gethwspecs() extracts correct informations? not maybe! */
1535 error = malo_hal_gethwspecs(mh, &sc->malo_hwspecs);
1537 if_printf(ifp, "unable to fetch h/w specs\n");
1541 DPRINTF(sc, MALO_DEBUG_FW,
1542 "malo_hal_gethwspecs: hwversion 0x%x hostif 0x%x"
1543 "maxnum_wcb 0x%x maxnum_mcaddr 0x%x maxnum_tx_wcb 0x%x"
1544 "regioncode 0x%x num_antenna 0x%x fw_releasenum 0x%x"
1545 "wcbbase0 0x%x rxdesc_read 0x%x rxdesc_write 0x%x"
1546 "ul_fw_awakecookie 0x%x w[4] = %x %x %x %x",
1547 sc->malo_hwspecs.hwversion,
1548 sc->malo_hwspecs.hostinterface, sc->malo_hwspecs.maxnum_wcb,
1549 sc->malo_hwspecs.maxnum_mcaddr,
1550 sc->malo_hwspecs.maxnum_tx_wcb,
1551 sc->malo_hwspecs.regioncode, sc->malo_hwspecs.num_antenna,
1552 sc->malo_hwspecs.fw_releasenum, sc->malo_hwspecs.wcbbase0,
1553 sc->malo_hwspecs.rxdesc_read, sc->malo_hwspecs.rxdesc_write,
1554 sc->malo_hwspecs.ul_fw_awakecookie,
1555 sc->malo_hwspecs.wcbbase[0], sc->malo_hwspecs.wcbbase[1],
1556 sc->malo_hwspecs.wcbbase[2], sc->malo_hwspecs.wcbbase[3]);
1558 error = malo_setup_hwdma(sc); /* push to firmware */
1559 /* NB: malo_setupdma prints msg */
1561 if_printf(ifp, "%s: failed to set up h/w dma\n",
1566 /* set reddomain. */
1567 ic->ic_regdomain = sc->malo_hwspecs.regioncode;
1571 sc->malo_fw_loaded = 1;
1577 * Stop anything previously setup. This is safe whether this is
1578 * the first time through or not.
1580 malo_stop_locked(ifp, 0);
1583 * Push state to the firmware.
1585 if (!malo_hal_reset(sc)) {
1586 if_printf(ifp, "%s: unable to reset hardware\n", __func__);
1591 * Setup recv (once); transmit is already good to go.
1593 error = malo_startrecv(sc);
1595 if_printf(ifp, "%s: unable to start recv logic, error %d\n",
1601 * Enable interrupts.
1603 sc->malo_imask = MALO_A2HRIC_BIT_RX_RDY
1604 | MALO_A2HRIC_BIT_TX_DONE
1605 | MALO_A2HRIC_BIT_OPC_DONE
1606 | MALO_A2HRIC_BIT_MAC_EVENT
1607 | MALO_A2HRIC_BIT_RX_PROBLEM
1608 | MALO_A2HRIC_BIT_ICV_ERROR
1609 | MALO_A2HRIC_BIT_RADAR_DETECT
1610 | MALO_A2HRIC_BIT_CHAN_SWITCH;
1612 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1613 ic->ic_state = IEEE80211_S_INIT;
1614 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1616 malo_hal_intrset(mh, sc->malo_imask);
1619 * The hardware should be ready to go now so it's safe to kick
1620 * the 802.11 state machine as it's likely to immediately call back
1621 * to us to send mgmt frames.
1623 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1624 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1625 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1627 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1632 "error(%d) occurred during the initializing.\n", error);
1640 * Set the multicast filter contents into the hardware.
1643 malo_setmcastfilter(struct malo_softc *sc)
1645 struct ieee80211com *ic = &sc->malo_ic;
1646 struct ifmultiaddr *ifma;
1647 struct ifnet *ifp = sc->malo_ifp;
1648 uint8_t macs[IEEE80211_ADDR_LEN * MALO_HAL_MCAST_MAX];
1655 if (ic->ic_opmode == IEEE80211_M_MONITOR ||
1656 (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)))
1660 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1661 if (ifma->ifma_addr->sa_family != AF_LINK)
1664 if (nmc == MALO_HAL_MCAST_MAX) {
1665 ifp->if_flags |= IFF_ALLMULTI;
1666 IF_ADDR_UNLOCK(ifp);
1669 IEEE80211_ADDR_COPY(mp,
1670 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1672 mp += IEEE80211_ADDR_LEN, nmc++;
1674 IF_ADDR_UNLOCK(ifp);
1676 malo_hal_setmcast(sc->malo_mh, nmc, macs);
1680 * XXX we don't know how to set the f/w for supporting
1681 * IFF_ALLMULTI | IFF_PROMISC cases
1687 malo_mode_init(struct malo_softc *sc)
1689 struct ieee80211com *ic = &sc->malo_ic;
1690 struct ifnet *ifp = ic->ic_ifp;
1691 struct malo_hal *mh = sc->malo_mh;
1694 * Handle any link-level address change. Note that we only
1695 * need to force ic_myaddr; any other addresses are handled
1696 * as a byproduct of the ifnet code marking the interface
1699 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1702 * NB: Ignore promisc in hostap mode; it's set by the
1703 * bridge. This is wrong but we have no way to
1704 * identify internal requests (from the bridge)
1705 * versus external requests such as for tcpdump.
1707 malo_hal_setpromisc(mh, (ifp->if_flags & IFF_PROMISC) &&
1708 ic->ic_opmode != IEEE80211_M_HOSTAP);
1709 malo_setmcastfilter(sc);
1715 malo_tx_draintxq(struct malo_softc *sc, struct malo_txq *txq)
1717 struct ieee80211_node *ni;
1718 struct malo_txbuf *bf;
1722 * NB: this assumes output has been stopped and
1723 * we do not need to block malo_tx_tasklet
1725 for (ix = 0;; ix++) {
1727 bf = STAILQ_FIRST(&txq->active);
1729 MALO_TXQ_UNLOCK(txq);
1732 STAILQ_REMOVE_HEAD(&txq->active, bf_list);
1733 MALO_TXQ_UNLOCK(txq);
1735 if (sc->malo_debug & MALO_DEBUG_RESET) {
1736 const struct malo_txrec *tr =
1737 mtod(bf->bf_m, const struct malo_txrec *);
1738 malo_printtxbuf(bf, txq->qnum, ix);
1739 ieee80211_dump_pkt(&sc->malo_ic,
1740 (const uint8_t *)&tr->wh,
1741 bf->bf_m->m_len - sizeof(tr->fwlen), 0, -1);
1743 #endif /* MALO_DEBUG */
1744 bus_dmamap_unload(sc->malo_dmat, bf->bf_dmamap);
1749 * Reclaim node reference.
1751 ieee80211_free_node(ni);
1757 STAILQ_INSERT_TAIL(&txq->free, bf, bf_list);
1759 MALO_TXQ_UNLOCK(txq);
1764 malo_stop_locked(struct ifnet *ifp, int disable)
1767 struct malo_softc *sc = ifp->if_softc;
1768 struct ieee80211com *ic = &sc->malo_ic;
1769 struct malo_hal *mh = sc->malo_mh;
1771 DPRINTF(sc, MALO_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1772 __func__, sc->malo_invalid, ifp->if_flags);
1774 MALO_LOCK_ASSERT(sc);
1776 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1780 * Shutdown the hardware and driver:
1781 * reset 802.11 state machine
1783 * disable interrupts
1784 * turn off the radio
1785 * clear transmit machinery
1786 * clear receive machinery
1787 * drain and release tx queues
1788 * reclaim beacon resources
1789 * power down hardware
1791 * Note that some of this work is not possible if the hardware
1792 * is gone (invalid).
1794 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1795 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1797 if (sc->malo_fw_loaded == 1) {
1798 /* diable interrupt. */
1799 malo_hal_intrset(mh, 0);
1800 /* turn off the radio. */
1801 malo_hal_setradio(mh, 0, MHP_AUTO_PREAMBLE);
1804 /* drain and release tx queues. */
1805 for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
1806 malo_tx_draintxq(sc, &sc->malo_txq[i]);
1810 malo_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1812 #define MALO_IS_RUNNING(ifp) \
1813 ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
1814 struct malo_softc *sc = ifp->if_softc;
1815 struct ieee80211com *ic = &sc->malo_ic;
1822 if (MALO_IS_RUNNING(ifp)) {
1824 * To avoid rescanning another access point,
1825 * do not call malo_init() here. Instead,
1826 * only reflect promisc mode settings.
1829 } else if (ifp->if_flags & IFF_UP) {
1831 * Beware of being called during attach/detach
1832 * to reset promiscuous mode. In that case we
1833 * will still be marked UP but not RUNNING.
1834 * However trying to re-init the interface
1835 * is the wrong thing to do as we've already
1836 * torn down much of our state. There's
1837 * probably a better way to deal with this.
1839 if (!sc->malo_invalid)
1842 malo_stop_locked(ifp, 1);
1847 * The upper layer has already installed/removed
1848 * the multicast address(es), just recalculate the
1849 * multicast filter for the card.
1851 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1855 error = ieee80211_ioctl(ic, cmd, data);
1856 if (error == ENETRESET) {
1857 if (MALO_IS_RUNNING(ifp) &&
1858 ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1862 if (error == ERESTART) {
1863 /* XXX we need to reset the device here. */
1872 #undef MALO_IS_RUNNING
1876 * Callback from the 802.11 layer to update the slot time
1877 * based on the current setting. We use it to notify the
1878 * firmware of ERP changes and the f/w takes care of things
1879 * like slot time and preamble.
1882 malo_updateslot(struct ifnet *ifp)
1884 struct malo_softc *sc = ifp->if_softc;
1885 struct ieee80211com *ic = &sc->malo_ic;
1886 struct malo_hal *mh = sc->malo_mh;
1889 /* NB: can be called early; suppress needless cmds */
1890 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1893 DPRINTF(sc, MALO_DEBUG_RESET,
1894 "%s: chan %u MHz/flags 0x%x %s slot, (ic_flags 0x%x)\n",
1895 __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
1896 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", ic->ic_flags);
1898 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1899 error = malo_hal_set_slot(mh, 1);
1901 error = malo_hal_set_slot(mh, 0);
1904 device_printf(sc->malo_dev, "setting %s slot failed\n",
1905 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long");
1909 malo_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1911 struct ieee80211_node *ni = ic->ic_bss;
1912 struct ifnet *ifp = ic->ic_ifp;
1913 struct malo_softc *sc = ifp->if_softc;
1914 struct malo_hal *mh = sc->malo_mh;
1917 DPRINTF(sc, MALO_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1918 ieee80211_state_name[ic->ic_state],
1919 ieee80211_state_name[nstate]);
1922 * Carry out firmware actions per-state.
1925 case IEEE80211_S_INIT:
1926 case IEEE80211_S_SCAN:
1927 case IEEE80211_S_AUTH:
1928 /* NB: do nothing. */
1930 case IEEE80211_S_ASSOC:
1931 malo_hal_setradio(mh, 1,
1932 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ?
1933 MHP_SHORT_PREAMBLE : MHP_LONG_PREAMBLE);
1935 case IEEE80211_S_RUN:
1936 DPRINTF(sc, MALO_DEBUG_STATE,
1937 "%s: %s(RUN): ic_flags 0x%08x bintvl %d bssid %s "
1938 "capinfo 0x%04x chan %d\n",
1939 ifp->if_xname, __func__, ic->ic_flags,
1940 ni->ni_intval, ether_sprintf(ni->ni_bssid), ni->ni_capinfo,
1941 ieee80211_chan2ieee(ic, ic->ic_curchan));
1943 switch (ic->ic_opmode) {
1944 case IEEE80211_M_STA:
1945 DPRINTF(sc, MALO_DEBUG_STATE, "%s: %s: aid 0x%x\n",
1946 ic->ic_ifp->if_xname, __func__, ni->ni_associd);
1947 malo_hal_setassocid(sc->malo_mh,
1948 ni->ni_bssid, ni->ni_associd);
1950 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
1951 /* automatic rate adaption */
1952 malo_hal_set_rate(mh, ic->ic_curmode, 0);
1955 malo_hal_set_rate(mh, ic->ic_curmode,
1956 malo_fix2rate(ic->ic_fixed_rate));
1964 if_printf(ifp, "%s: can't handle state %s -> %s\n",
1965 __func__, ieee80211_state_name[ic->ic_state],
1966 ieee80211_state_name[nstate]);
1970 * Invoke the parent method to complete the work.
1972 error = sc->malo_newstate(ic, nstate, arg);
1978 malo_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1979 const struct ieee80211_bpf_params *params)
1981 struct ieee80211com *ic = ni->ni_ic;
1982 struct ifnet *ifp = ic->ic_ifp;
1983 struct malo_softc *sc = ifp->if_softc;
1984 struct malo_txbuf *bf;
1985 struct malo_txq *txq;
1987 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->malo_invalid) {
1988 ieee80211_free_node(ni);
1994 * Grab a TX buffer and associated resources. Note that we depend
1995 * on the classification by the 802.11 layer to get to the right h/w
1996 * queue. Management frames must ALWAYS go on queue 1 but we
1997 * cannot just force that here because we may receive non-mgt frames.
1999 txq = &sc->malo_txq[0];
2000 bf = malo_getbuf(sc, txq);
2002 /* XXX blocks other traffic */
2003 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2004 ieee80211_free_node(ni);
2010 * Pass the frame to the h/w for transmission.
2012 if (malo_tx_start(sc, ni, bf, m) != 0) {
2017 STAILQ_INSERT_HEAD(&txq->free, bf, bf_list);
2019 MALO_TXQ_UNLOCK(txq);
2021 ieee80211_free_node(ni);
2022 return EIO; /* XXX */
2026 * NB: We don't need to lock against tx done because this just
2027 * prods the firmware to check the transmit descriptors. The firmware
2028 * will also start fetching descriptors by itself if it notices
2029 * new ones are present when it goes to deliver a tx done interrupt
2030 * to the host. So if we race with tx done processing it's ok.
2031 * Delivering the kick here rather than in malo_tx_start is
2032 * an optimization to avoid poking the firmware for each packet.
2034 * NB: the queue id isn't used so 0 is ok.
2036 malo_hal_txstart(sc->malo_mh, 0/*XXX*/);
2042 malo_media_change(struct ifnet *ifp)
2044 #define IS_UP(ifp) \
2045 ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
2048 error = ieee80211_media_change(ifp);
2049 if (error == ENETRESET) {
2050 struct malo_softc *sc = ifp->if_softc;
2061 malo_bpfattach(struct malo_softc *sc)
2063 struct ifnet *ifp = sc->malo_ifp;
2065 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
2066 sizeof(struct ieee80211_frame) + sizeof(sc->malo_tx_th),
2070 * Initialize constant fields.
2071 * XXX make header lengths a multiple of 32-bits so subsequent
2072 * headers are properly aligned; this is a kludge to keep
2073 * certain applications happy.
2075 * NB: the channel is setup each time we transition to the
2076 * RUN state to avoid filling it in for each frame.
2078 sc->malo_tx_th_len = roundup(sizeof(sc->malo_tx_th), sizeof(uint32_t));
2079 sc->malo_tx_th.wt_ihdr.it_len = htole16(sc->malo_tx_th_len);
2080 sc->malo_tx_th.wt_ihdr.it_present = htole32(MALO_TX_RADIOTAP_PRESENT);
2082 sc->malo_rx_th_len = roundup(sizeof(sc->malo_rx_th), sizeof(uint32_t));
2083 sc->malo_rx_th.wr_ihdr.it_len = htole16(sc->malo_rx_th_len);
2084 sc->malo_rx_th.wr_ihdr.it_present = htole32(MALO_RX_RADIOTAP_PRESENT);
2088 malo_sysctlattach(struct malo_softc *sc)
2091 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->malo_dev);
2092 struct sysctl_oid *tree = device_get_sysctl_tree(sc->malo_dev);
2094 sc->malo_debug = malo_debug;
2095 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2096 "debug", CTLFLAG_RW, &sc->malo_debug, 0,
2097 "control debugging printfs");
2102 malo_announce(struct malo_softc *sc)
2104 struct ifnet *ifp = sc->malo_ifp;
2106 if_printf(ifp, "versions [hw %d fw %d.%d.%d.%d] (regioncode %d)\n",
2107 sc->malo_hwspecs.hwversion,
2108 (sc->malo_hwspecs.fw_releasenum >> 24) & 0xff,
2109 (sc->malo_hwspecs.fw_releasenum >> 16) & 0xff,
2110 (sc->malo_hwspecs.fw_releasenum >> 8) & 0xff,
2111 (sc->malo_hwspecs.fw_releasenum >> 0) & 0xff,
2112 sc->malo_hwspecs.regioncode);
2114 if (bootverbose || malo_rxbuf != MALO_RXBUF)
2115 if_printf(ifp, "using %u rx buffers\n", malo_rxbuf);
2116 if (bootverbose || malo_txbuf != MALO_TXBUF)
2117 if_printf(ifp, "using %u tx buffers\n", malo_txbuf);
2121 * Convert net80211 channel to a HAL channel.
2124 malo_mapchan(struct malo_hal_channel *hc, const struct ieee80211_channel *chan)
2126 hc->channel = chan->ic_ieee;
2128 *(uint32_t *)&hc->flags = 0;
2129 if (IEEE80211_IS_CHAN_2GHZ(chan))
2130 hc->flags.freqband = MALO_FREQ_BAND_2DOT4GHZ;
2134 * Set/change channels. If the channel is really being changed,
2135 * it's done by reseting the chip. To accomplish this we must
2136 * first cleanup any pending DMA, then restart stuff after a la
2140 malo_chan_set(struct malo_softc *sc, struct ieee80211_channel *chan)
2142 struct malo_hal *mh = sc->malo_mh;
2143 struct malo_hal_channel hchan;
2145 DPRINTF(sc, MALO_DEBUG_RESET, "%s: chan %u MHz/flags 0x%x\n",
2146 __func__, chan->ic_freq, chan->ic_flags);
2149 * Convert to a HAL channel description with the flags constrained
2150 * to reflect the current operating mode.
2152 malo_mapchan(&hchan, chan);
2153 malo_hal_intrset(mh, 0); /* disable interrupts */
2154 malo_hal_setchannel(mh, &hchan);
2155 malo_hal_settxpower(mh, &hchan);
2158 * Update internal state.
2160 sc->malo_tx_th.wt_chan_freq = htole16(chan->ic_freq);
2161 sc->malo_rx_th.wr_chan_freq = htole16(chan->ic_freq);
2162 if (IEEE80211_IS_CHAN_ANYG(chan)) {
2163 sc->malo_tx_th.wt_chan_flags = htole16(IEEE80211_CHAN_G);
2164 sc->malo_rx_th.wr_chan_flags = htole16(IEEE80211_CHAN_G);
2166 sc->malo_tx_th.wt_chan_flags = htole16(IEEE80211_CHAN_B);
2167 sc->malo_rx_th.wr_chan_flags = htole16(IEEE80211_CHAN_B);
2169 sc->malo_curchan = hchan;
2170 malo_hal_intrset(mh, sc->malo_imask);
2176 malo_scan_start(struct ieee80211com *ic)
2178 struct ifnet *ifp = ic->ic_ifp;
2179 struct malo_softc *sc = ifp->if_softc;
2181 DPRINTF(sc, MALO_DEBUG_STATE, "%s\n", __func__);
2185 malo_scan_end(struct ieee80211com *ic)
2187 struct ifnet *ifp = ic->ic_ifp;
2188 struct malo_softc *sc = ifp->if_softc;
2190 DPRINTF(sc, MALO_DEBUG_STATE, "%s\n", __func__);
2194 malo_set_channel(struct ieee80211com *ic)
2196 struct ifnet *ifp = ic->ic_ifp;
2197 struct malo_softc *sc = ifp->if_softc;
2199 (void) malo_chan_set(sc, ic->ic_curchan);
2203 malo_rx_proc(void *arg, int npending)
2205 #define IEEE80211_DIR_DSTODS(wh) \
2206 ((((const struct ieee80211_frame *)wh)->i_fc[1] & \
2207 IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
2208 struct malo_softc *sc = arg;
2209 struct malo_rxbuf *bf;
2210 struct ieee80211com *ic = &sc->malo_ic;
2211 struct ifnet *ifp = sc->malo_ifp;
2212 struct malo_rxdesc *ds;
2213 struct mbuf *m, *mnew;
2214 struct ieee80211_qosframe *wh;
2215 struct ieee80211_qosframe_addr4 *wh4;
2216 struct ieee80211_node *ni;
2217 int off, len, hdrlen, pktlen, rssi, ntodo;
2218 uint8_t *data, status;
2219 uint32_t readptr, writeptr;
2221 DPRINTF(sc, MALO_DEBUG_RX_PROC,
2222 "%s: pending %u rdptr(0x%x) 0x%x wrptr(0x%x) 0x%x\n",
2224 sc->malo_hwspecs.rxdesc_read,
2225 malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_read),
2226 sc->malo_hwspecs.rxdesc_write,
2227 malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_write));
2229 readptr = malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_read);
2230 writeptr = malo_bar0_read4(sc, sc->malo_hwspecs.rxdesc_write);
2231 if (readptr == writeptr)
2234 bf = sc->malo_rxnext;
2235 for (ntodo = malo_rxquota; ntodo > 0 && (readptr != writeptr);
2238 bf = STAILQ_FIRST(&sc->malo_rxbuf);
2242 if (bf->bf_m == NULL) {
2244 * If data allocation failed previously there
2245 * will be no buffer; try again to re-populate it.
2246 * Note the firmware will not advance to the next
2247 * descriptor with a dma buffer so we must mimic
2248 * this or we'll get out of sync.
2250 DPRINTF(sc, MALO_DEBUG_ANY,
2251 "%s: rx buf w/o dma memory\n", __func__);
2252 (void)malo_rxbuf_init(sc, bf);
2255 MALO_RXDESC_SYNC(sc, ds,
2256 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2257 if (ds->rxcontrol != MALO_RXD_CTRL_DMA_OWN)
2260 readptr = le32toh(ds->physnext);
2263 if (sc->malo_debug & MALO_DEBUG_RECV_DESC)
2264 malo_printrxbuf(bf, 0);
2266 status = ds->status;
2267 if (status & MALO_RXD_STATUS_DECRYPT_ERR_MASK) {
2272 * Sync the data buffer.
2274 len = le16toh(ds->pktlen);
2275 bus_dmamap_sync(sc->malo_dmat, bf->bf_dmamap,
2276 BUS_DMASYNC_POSTREAD);
2278 * The 802.11 header is provided all or in part at the front;
2279 * use it to calculate the true size of the header that we'll
2280 * construct below. We use this to figure out where to copy
2281 * payload prior to constructing the header.
2284 data = mtod(m, uint8_t *);
2285 hdrlen = ieee80211_anyhdrsize(data + sizeof(uint16_t));
2286 off = sizeof(uint16_t) + sizeof(struct ieee80211_frame_addr4);
2289 * Calculate RSSI. XXX wrong
2291 rssi = 2 * ((int) ds->snr - ds->nf); /* NB: .5 dBm */
2295 pktlen = hdrlen + (len - off);
2297 * NB: we know our frame is at least as large as
2298 * IEEE80211_MIN_LEN because there is a 4-address frame at
2299 * the front. Hence there's no need to vet the packet length.
2300 * If the frame in fact is too small it should be discarded
2301 * at the net80211 layer.
2304 /* XXX don't need mbuf, just dma buffer */
2305 mnew = malo_getrxmbuf(sc, bf);
2312 * Attach the dma buffer to the mbuf; malo_rxbuf_init will
2313 * re-setup the rx descriptor using the replacement dma
2314 * buffer we just installed above.
2317 m->m_data += off - hdrlen;
2318 m->m_pkthdr.len = m->m_len = pktlen;
2319 m->m_pkthdr.rcvif = ifp;
2322 * Piece 802.11 header together.
2324 wh = mtod(m, struct ieee80211_qosframe *);
2325 /* NB: don't need to do this sometimes but ... */
2326 /* XXX special case so we can memcpy after m_devget? */
2327 ovbcopy(data + sizeof(uint16_t), wh, hdrlen);
2328 if (IEEE80211_QOS_HAS_SEQ(wh)) {
2329 if (IEEE80211_DIR_DSTODS(wh)) {
2331 struct ieee80211_qosframe_addr4*);
2332 *(uint16_t *)wh4->i_qos = ds->qosctrl;
2334 *(uint16_t *)wh->i_qos = ds->qosctrl;
2337 if (sc->malo_drvbpf != NULL) {
2338 sc->malo_rx_th.wr_flags = 0;
2339 sc->malo_rx_th.wr_rate = ds->rate;
2340 sc->malo_rx_th.wr_antsignal = rssi;
2341 sc->malo_rx_th.wr_antnoise = ds->nf;
2343 bpf_mtap2(sc->malo_drvbpf,
2344 &sc->malo_rx_th, sc->malo_rx_th_len, m);
2347 if (IFF_DUMPPKTS_RECV(sc, wh)) {
2348 ieee80211_dump_pkt(ic, mtod(m, caddr_t),
2349 len, ds->rate, rssi);
2355 ni = ieee80211_find_rxnode(ic,
2356 (const struct ieee80211_frame_min *) wh);
2357 (void) ieee80211_input(ic, m, ni, rssi, ds->nf, 0/*XXX*/);
2358 ieee80211_free_node(ni);
2361 /* NB: ignore ENOMEM so we process more descriptors */
2362 (void) malo_rxbuf_init(sc, bf);
2363 bf = STAILQ_NEXT(bf, bf_list);
2366 malo_bar0_write4(sc, sc->malo_hwspecs.rxdesc_read, readptr);
2367 sc->malo_rxnext = bf;
2369 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
2370 !IFQ_IS_EMPTY(&ifp->if_snd))
2372 #undef IEEE80211_DIR_DSTODS
2376 malo_stop(struct ifnet *ifp, int disable)
2378 struct malo_softc *sc = ifp->if_softc;
2382 malo_stop_locked(ifp, disable);
2388 * Reclaim all tx queue resources.
2391 malo_tx_cleanup(struct malo_softc *sc)
2395 for (i = 0; i < MALO_NUM_TX_QUEUES; i++)
2396 malo_tx_cleanupq(sc, &sc->malo_txq[i]);
2400 malo_detach(struct malo_softc *sc)
2402 struct ifnet *ifp = sc->malo_ifp;
2404 DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags %x\n",
2405 __func__, ifp->if_flags);
2409 if (sc->malo_tq != NULL) {
2410 taskqueue_drain(sc->malo_tq, &sc->malo_rxtask);
2411 taskqueue_drain(sc->malo_tq, &sc->malo_txtask);
2412 taskqueue_free(sc->malo_tq);
2419 * NB: the order of these is important:
2420 * o call the 802.11 layer before detaching the hal to
2421 * insure callbacks into the driver to delete global
2422 * key cache entries can be handled
2423 * o reclaim the tx queue data structures after calling
2424 * the 802.11 layer as we'll get called back to reclaim
2425 * node state and potentially want to use them
2426 * o to cleanup the tx queues the hal is called, so detach
2428 * Other than that, it's straightforward...
2430 ieee80211_ifdetach(&sc->malo_ic);
2431 malo_dma_cleanup(sc);
2432 malo_tx_cleanup(sc);
2433 malo_hal_detach(sc->malo_mh);
2436 MALO_LOCK_DESTROY(sc);
2442 malo_shutdown(struct malo_softc *sc)
2445 malo_stop(sc->malo_ifp, 1);
2449 malo_suspend(struct malo_softc *sc)
2451 struct ifnet *ifp = sc->malo_ifp;
2453 DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags %x\n",
2454 __func__, ifp->if_flags);
2460 malo_resume(struct malo_softc *sc)
2462 struct ifnet *ifp = sc->malo_ifp;
2464 DPRINTF(sc, MALO_DEBUG_ANY, "%s: if_flags %x\n",
2465 __func__, ifp->if_flags);
2467 if (ifp->if_flags & IFF_UP) {
2469 if (ifp->if_drv_flags & IFF_DRV_RUNNING)