1 /* $OpenBSD: if_rtwn.c,v 1.6 2015/08/28 00:03:53 deraadt Exp $ */
4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2015 Stefan Sperling <stsp@openbsd.org>
6 * Copyright (c) 2016 Andriy Voskoboinyk <avos@FreeBSD.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
26 #include <sys/param.h>
28 #include <sys/mutex.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/queue.h>
35 #include <sys/taskqueue.h>
37 #include <sys/endian.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
44 #include <net/ethernet.h>
45 #include <net/if_media.h>
47 #include <net80211/ieee80211_var.h>
49 #include <dev/rtwn/if_rtwnreg.h>
50 #include <dev/rtwn/if_rtwnvar.h>
51 #include <dev/rtwn/if_rtwn_debug.h>
52 #include <dev/rtwn/if_rtwn_rx.h>
53 #include <dev/rtwn/if_rtwn_task.h>
54 #include <dev/rtwn/if_rtwn_tx.h>
56 #include <dev/rtwn/pci/rtwn_pci_var.h>
57 #include <dev/rtwn/pci/rtwn_pci_rx.h>
59 #include <dev/rtwn/rtl8192c/pci/r92ce_rx_desc.h>
63 rtwn_pci_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs,
69 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
70 *(bus_addr_t *)arg = segs[0].ds_addr;
74 rtwn_pci_setup_rx_desc(struct rtwn_pci_softc *pc, struct r92ce_rx_stat *desc,
75 bus_addr_t addr, size_t len, int idx)
78 memset(desc, 0, sizeof(*desc));
79 desc->rxdw0 = htole32(SM(R92C_RXDW0_PKTLEN, len) |
80 ((idx == RTWN_PCI_RX_LIST_COUNT - 1) ? R92C_RXDW0_EOR : 0));
81 desc->rxbufaddr = htole32(addr);
82 bus_space_barrier(pc->pc_st, pc->pc_sh, 0, pc->pc_mapsize,
83 BUS_SPACE_BARRIER_WRITE);
84 desc->rxdw0 |= htole32(R92C_RXDW0_OWN);
88 rtwn_pci_rx_frame(struct rtwn_softc *sc, struct r92ce_rx_stat *rx_desc,
91 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
92 struct rtwn_rx_ring *ring = &pc->rx_ring;
93 struct rtwn_rx_data *rx_data = &ring->rx_data[desc_idx];
94 struct ieee80211com *ic = &sc->sc_ic;
95 struct ieee80211_node *ni;
99 int infosz, pktlen, shift, error;
101 /* Dump Rx descriptor. */
102 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC,
103 "%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, tsfl %08X, "
104 "addr: %08X (64: %08X)\n",
105 __func__, le32toh(rx_desc->rxdw0), le32toh(rx_desc->rxdw1),
106 le32toh(rx_desc->rxdw2), le32toh(rx_desc->rxdw3),
107 le32toh(rx_desc->rxdw4), le32toh(rx_desc->tsf_low),
108 le32toh(rx_desc->rxbufaddr), le32toh(rx_desc->rxbufaddr64));
110 rxdw0 = le32toh(rx_desc->rxdw0);
111 if (__predict_false(rxdw0 & (R92C_RXDW0_CRCERR | R92C_RXDW0_ICVERR))) {
113 * This should not happen since we setup our Rx filter
114 * to not receive these frames.
116 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
117 "%s: RX flags error (%s)\n", __func__,
118 rxdw0 & R92C_RXDW0_CRCERR ? "CRC" : "ICV");
122 pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN);
123 if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack) ||
124 pktlen > MJUMPAGESIZE)) {
125 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
126 "%s: frame is too short/long: %d\n", __func__, pktlen);
130 infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8;
131 shift = MS(rxdw0, R92C_RXDW0_SHIFT);
133 m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
134 if (__predict_false(m1 == NULL)) {
135 device_printf(sc->sc_dev, "%s: could not allocate RX mbuf\n",
139 bus_dmamap_sync(ring->data_dmat, rx_data->map, BUS_DMASYNC_POSTREAD);
140 bus_dmamap_unload(ring->data_dmat, rx_data->map);
142 error = bus_dmamap_load(ring->data_dmat, rx_data->map, mtod(m1, void *),
143 MJUMPAGESIZE, rtwn_pci_dma_map_addr, &rx_data->paddr, 0);
147 error = bus_dmamap_load(ring->data_dmat, rx_data->map,
148 mtod(rx_data->m, void *), MJUMPAGESIZE,
149 rtwn_pci_dma_map_addr, &rx_data->paddr, BUS_DMA_NOWAIT);
151 panic("%s: could not load old RX mbuf",
152 device_get_name(sc->sc_dev));
154 /* Physical address may have changed. */
155 rtwn_pci_setup_rx_desc(pc, rx_desc, rx_data->paddr,
156 MJUMPAGESIZE, desc_idx);
163 m->m_pkthdr.len = m->m_len = pktlen + infosz + shift;
165 nf = RTWN_NOISE_FLOOR;
166 ni = rtwn_rx_common(sc, m, rx_desc, &rssi);
168 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
169 "%s: Rx frame len %d, infosz %d, shift %d, rssi %d\n",
170 __func__, pktlen, infosz, shift, rssi);
172 /* Update RX descriptor. */
173 rtwn_pci_setup_rx_desc(pc, rx_desc, rx_data->paddr, MJUMPAGESIZE,
176 /* Send the frame to the 802.11 layer. */
179 (void)ieee80211_input(ni, m, rssi - nf, nf);
180 /* Node is no longer needed. */
181 ieee80211_free_node(ni);
183 (void)ieee80211_input_all(ic, m, rssi - nf, nf);
190 counter_u64_add(ic->ic_ierrors, 1);
194 rtwn_pci_tx_done(struct rtwn_softc *sc, int qid)
196 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
197 struct rtwn_tx_ring *ring = &pc->tx_ring[qid];
198 struct rtwn_tx_desc_common *desc;
199 struct rtwn_tx_data *data;
201 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: qid %d, last %d, cur %d\n",
202 __func__, qid, ring->last, ring->cur);
204 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_POSTREAD);
206 while(ring->last != ring->cur) {
207 data = &ring->tx_data[ring->last];
208 desc = (struct rtwn_tx_desc_common *)
209 ((uint8_t *)ring->desc + sc->txdesc_len * ring->last);
211 KASSERT(data->m != NULL, ("no mbuf"));
213 if (desc->flags0 & RTWN_FLAGS0_OWN)
216 /* Unmap and free mbuf. */
217 bus_dmamap_sync(ring->data_dmat, data->map,
218 BUS_DMASYNC_POSTWRITE);
219 bus_dmamap_unload(ring->data_dmat, data->map);
221 if (data->ni != NULL) { /* not a beacon frame */
222 ieee80211_tx_complete(data->ni, data->m, 0);
226 KASSERT(ring->queued >= 0,
227 ("ring->queued (qid %d) underflow!\n", qid));
232 ring->last = (ring->last + 1) % RTWN_PCI_TX_LIST_COUNT;
234 if (ring->queued > 0)
241 if ((sc->qfullmsk & (1 << qid)) != 0 &&
242 ring->queued < (RTWN_PCI_TX_LIST_COUNT - 1)) {
243 sc->qfullmsk &= ~(1 << qid);
247 #ifdef IEEE80211_SUPPORT_SUPERG
249 * If the TX active queue drops below a certain
250 * threshold, ensure we age fast-frames out so they're
253 if (sc->sc_ratectl != RTWN_RATECTL_NET80211 && ring->queued <= 1) {
255 * XXX TODO: just make this a callout timer schedule
256 * so we can flush the FF staging queue if we're
259 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
265 rtwn_pci_rx_done(struct rtwn_softc *sc)
267 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
268 struct rtwn_rx_ring *ring = &pc->rx_ring;
270 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_POSTREAD);
273 struct r92ce_rx_stat *rx_desc = &ring->desc[ring->cur];
275 if (le32toh(rx_desc->rxdw0) & R92C_RXDW0_OWN)
278 rtwn_pci_rx_frame(sc, rx_desc, ring->cur);
280 if (!(sc->sc_flags & RTWN_RUNNING))
283 ring->cur = (ring->cur + 1) % RTWN_PCI_RX_LIST_COUNT;
286 /* Finished receive; age anything left on the FF queue by a little bump */
288 * XXX TODO: just make this a callout timer schedule so we can
289 * flush the FF staging queue if we're approaching idle.
291 #ifdef IEEE80211_SUPPORT_SUPERG
292 if (!(sc->sc_flags & RTWN_FW_LOADED) ||
293 sc->sc_ratectl != RTWN_RATECTL_NET80211)
294 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
299 rtwn_pci_intr(void *arg)
301 struct rtwn_softc *sc = arg;
302 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
303 int i, status, tx_rings;
306 status = rtwn_classify_intr(sc, &tx_rings, 0);
307 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: status %08X, tx_rings %08X\n",
308 __func__, status, tx_rings);
309 if (status == 0 && tx_rings == 0)
312 if (status & RTWN_PCI_INTR_RX) {
313 rtwn_pci_rx_done(sc);
314 if (!(sc->sc_flags & RTWN_RUNNING))
319 for (i = 0; i < RTWN_PCI_NTXQUEUES; i++)
320 if (tx_rings & (1 << i))
321 rtwn_pci_tx_done(sc, i);
323 if (sc->sc_flags & RTWN_RUNNING)
324 rtwn_pci_enable_intr(pc);