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>
61 rtwn_pci_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs,
67 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
68 *(bus_addr_t *)arg = segs[0].ds_addr;
72 rtwn_pci_setup_rx_desc(struct rtwn_pci_softc *pc,
73 struct rtwn_rx_stat_pci *desc, bus_addr_t addr, size_t len, int idx)
76 memset(desc, 0, sizeof(*desc));
77 desc->rxdw0 = htole32(SM(RTWN_RXDW0_PKTLEN, len) |
78 ((idx == RTWN_PCI_RX_LIST_COUNT - 1) ? RTWN_RXDW0_EOR : 0));
79 desc->rxbufaddr = htole32(addr);
80 bus_space_barrier(pc->pc_st, pc->pc_sh, 0, pc->pc_mapsize,
81 BUS_SPACE_BARRIER_WRITE);
82 desc->rxdw0 |= htole32(RTWN_RXDW0_OWN);
86 rtwn_pci_rx_frame(struct rtwn_pci_softc *pc)
88 struct rtwn_softc *sc = &pc->pc_sc;
89 struct rtwn_rx_ring *ring = &pc->rx_ring;
90 struct rtwn_rx_stat_pci *rx_desc = &ring->desc[ring->cur];
91 struct rtwn_rx_data *rx_data = &ring->rx_data[ring->cur];
92 struct ieee80211com *ic = &sc->sc_ic;
93 struct ieee80211_node *ni;
96 int infosz, pktlen, shift, error;
98 /* Dump Rx descriptor. */
99 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC,
100 "%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, tsfl %08X, "
101 "addr: %08X (64: %08X)\n",
102 __func__, le32toh(rx_desc->rxdw0), le32toh(rx_desc->rxdw1),
103 le32toh(rx_desc->rxdw2), le32toh(rx_desc->rxdw3),
104 le32toh(rx_desc->rxdw4), le32toh(rx_desc->tsf_low),
105 le32toh(rx_desc->rxbufaddr), le32toh(rx_desc->rxbufaddr64));
107 rxdw0 = le32toh(rx_desc->rxdw0);
108 if (__predict_false(rxdw0 & (RTWN_RXDW0_CRCERR | RTWN_RXDW0_ICVERR))) {
110 * This should not happen since we setup our Rx filter
111 * to not receive these frames.
113 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
114 "%s: RX flags error (%s)\n", __func__,
115 rxdw0 & RTWN_RXDW0_CRCERR ? "CRC" : "ICV");
119 pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN);
120 if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack) ||
121 pktlen > MJUMPAGESIZE)) {
122 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
123 "%s: frame is too short/long: %d\n", __func__, pktlen);
127 infosz = MS(rxdw0, RTWN_RXDW0_INFOSZ) * 8;
128 shift = MS(rxdw0, RTWN_RXDW0_SHIFT);
130 m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
131 if (__predict_false(m1 == NULL)) {
132 device_printf(sc->sc_dev, "%s: could not allocate RX mbuf\n",
136 bus_dmamap_sync(ring->data_dmat, rx_data->map, BUS_DMASYNC_POSTREAD);
137 bus_dmamap_unload(ring->data_dmat, rx_data->map);
139 error = bus_dmamap_load(ring->data_dmat, rx_data->map, mtod(m1, void *),
140 MJUMPAGESIZE, rtwn_pci_dma_map_addr, &rx_data->paddr, 0);
144 error = bus_dmamap_load(ring->data_dmat, rx_data->map,
145 mtod(rx_data->m, void *), MJUMPAGESIZE,
146 rtwn_pci_dma_map_addr, &rx_data->paddr, BUS_DMA_NOWAIT);
148 panic("%s: could not load old RX mbuf",
149 device_get_name(sc->sc_dev));
157 m->m_pkthdr.len = m->m_len = pktlen + infosz + shift;
159 ni = rtwn_rx_common(sc, m, rx_desc);
161 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
162 "%s: Rx frame len %d, infosz %d, shift %d\n",
163 __func__, pktlen, infosz, shift);
165 /* Send the frame to the 802.11 layer. */
168 (void)ieee80211_input_mimo(ni, m);
169 /* Node is no longer needed. */
170 ieee80211_free_node(ni);
172 (void)ieee80211_input_mimo_all(ic, m);
179 counter_u64_add(ic->ic_ierrors, 1);
183 rtwn_pci_rx_buf_copy(struct rtwn_pci_softc *pc)
185 struct rtwn_rx_ring *ring = &pc->rx_ring;
186 struct rtwn_rx_stat_pci *rx_desc = &ring->desc[ring->cur];
187 struct rtwn_rx_data *rx_data = &ring->rx_data[ring->cur];
189 int desc_size, pktlen;
192 * NB: tx_report() / c2h_report() expects to see USB Rx
193 * descriptor - same as for PCIe, but without rxbufaddr* fields.
195 desc_size = sizeof(struct rtwn_rx_stat_common);
196 KASSERT(sizeof(pc->pc_rx_buf) >= desc_size,
197 ("adjust size for PCIe Rx buffer!"));
199 memcpy(pc->pc_rx_buf, rx_desc, desc_size);
201 rxdw0 = le32toh(rx_desc->rxdw0);
202 pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN);
204 if (pktlen > sizeof(pc->pc_rx_buf) - desc_size)
206 /* Looks like an ordinary Rx frame. */
210 bus_dmamap_sync(ring->data_dmat, rx_data->map, BUS_DMASYNC_POSTREAD);
211 memcpy(pc->pc_rx_buf + desc_size, mtod(rx_data->m, void *), pktlen);
213 return (desc_size + pktlen);
217 rtwn_pci_tx_report(struct rtwn_pci_softc *pc, int len)
219 struct rtwn_softc *sc = &pc->pc_sc;
221 if (sc->sc_ratectl != RTWN_RATECTL_NET80211) {
222 /* shouldn't happen */
223 device_printf(sc->sc_dev,
224 "%s called while ratectl = %d!\n",
225 __func__, sc->sc_ratectl);
230 rtwn_handle_tx_report(sc, pc->pc_rx_buf, len);
233 #ifdef IEEE80211_SUPPORT_SUPERG
235 * NB: this will executed only when 'report' bit is set.
237 if (sc->sc_tx_n_active > 0 && --sc->sc_tx_n_active <= 1)
238 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
243 rtwn_pci_c2h_report(struct rtwn_pci_softc *pc, int len)
245 rtwn_handle_c2h_report(&pc->pc_sc, pc->pc_rx_buf, len);
249 rtwn_pci_tx_done(struct rtwn_softc *sc, int qid)
251 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
252 struct rtwn_tx_ring *ring = &pc->tx_ring[qid];
253 struct rtwn_tx_desc_common *desc;
254 struct rtwn_tx_data *data;
256 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: qid %d, last %d, cur %d\n",
257 __func__, qid, ring->last, ring->cur);
259 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
260 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
262 while(ring->last != ring->cur) {
263 data = &ring->tx_data[ring->last];
264 desc = (struct rtwn_tx_desc_common *)
265 ((uint8_t *)ring->desc + sc->txdesc_len * ring->last);
267 KASSERT(data->m != NULL, ("no mbuf"));
269 if (desc->flags0 & RTWN_FLAGS0_OWN)
272 /* Unmap and free mbuf. */
273 bus_dmamap_sync(ring->data_dmat, data->map,
274 BUS_DMASYNC_POSTWRITE);
275 bus_dmamap_unload(ring->data_dmat, data->map);
277 if (data->ni != NULL) { /* not a beacon frame */
278 ieee80211_tx_complete(data->ni, data->m, 0);
282 KASSERT(ring->queued >= 0,
283 ("ring->queued (qid %d) underflow!\n", qid));
288 ring->last = (ring->last + 1) % RTWN_PCI_TX_LIST_COUNT;
290 if (ring->queued > 0)
297 if ((sc->qfullmsk & (1 << qid)) != 0 &&
298 ring->queued < (RTWN_PCI_TX_LIST_COUNT - 1)) {
299 sc->qfullmsk &= ~(1 << qid);
303 #ifdef IEEE80211_SUPPORT_SUPERG
305 * If the TX active queue drops below a certain
306 * threshold, ensure we age fast-frames out so they're
309 if (sc->sc_ratectl != RTWN_RATECTL_NET80211 && ring->queued <= 1) {
311 * XXX TODO: just make this a callout timer schedule
312 * so we can flush the FF staging queue if we're
315 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
321 rtwn_pci_rx_done(struct rtwn_softc *sc)
323 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
324 struct rtwn_rx_ring *ring = &pc->rx_ring;
325 struct rtwn_rx_stat_pci *rx_desc;
326 struct rtwn_rx_data *rx_data;
329 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_POSTREAD);
332 rx_desc = &ring->desc[ring->cur];
333 rx_data = &ring->rx_data[ring->cur];
335 if (le32toh(rx_desc->rxdw0) & RTWN_RXDW0_OWN)
338 len = rtwn_pci_rx_buf_copy(pc);
340 switch (rtwn_classify_intr(sc, pc->pc_rx_buf, len)) {
342 rtwn_pci_rx_frame(pc);
344 case RTWN_RX_TX_REPORT:
345 rtwn_pci_tx_report(pc, len);
348 rtwn_pci_c2h_report(pc, len);
352 KASSERT(0, ("unknown Rx classification code"));
356 /* Update / reset RX descriptor (and set OWN bit). */
357 rtwn_pci_setup_rx_desc(pc, rx_desc, rx_data->paddr,
358 MJUMPAGESIZE, ring->cur);
360 if (!(sc->sc_flags & RTWN_RUNNING))
363 /* NB: device can reuse current descriptor. */
364 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
365 BUS_DMASYNC_POSTREAD);
367 if (le32toh(rx_desc->rxdw0) & RTWN_RXDW0_OWN)
368 ring->cur = (ring->cur + 1) % RTWN_PCI_RX_LIST_COUNT;
373 rtwn_pci_intr(void *arg)
375 struct rtwn_softc *sc = arg;
376 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
377 int i, status, tx_rings;
380 status = rtwn_pci_get_intr_status(pc, &tx_rings);
381 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: status %08X, tx_rings %08X\n",
382 __func__, status, tx_rings);
383 if (status == 0 && tx_rings == 0)
386 if (status & (RTWN_PCI_INTR_RX | RTWN_PCI_INTR_TX_REPORT)) {
387 rtwn_pci_rx_done(sc);
388 if (!(sc->sc_flags & RTWN_RUNNING))
393 for (i = 0; i < RTWN_PCI_NTXQUEUES; i++)
394 if (tx_rings & (1 << i))
395 rtwn_pci_tx_done(sc, i);
397 if (sc->sc_flags & RTWN_RUNNING)
398 rtwn_pci_enable_intr(pc);