xref: /freebsd/sys/dev/rtwn/pci/rtwn_pci_rx.c (revision b65f813c)
1 /*	$OpenBSD: if_rtwn.c,v 1.6 2015/08/28 00:03:53 deraadt Exp $	*/
2 
3 /*-
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>
7  *
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.
11  *
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.
19  */
20 
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include "opt_wlan.h"
25 
26 #include <sys/param.h>
27 #include <sys/lock.h>
28 #include <sys/mutex.h>
29 #include <sys/mbuf.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>
36 #include <sys/bus.h>
37 #include <sys/endian.h>
38 #include <sys/epoch.h>
39 
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <sys/rman.h>
43 
44 #include <net/if.h>
45 #include <net/ethernet.h>
46 #include <net/if_media.h>
47 
48 #include <net80211/ieee80211_var.h>
49 
50 #include <dev/rtwn/if_rtwnreg.h>
51 #include <dev/rtwn/if_rtwnvar.h>
52 #include <dev/rtwn/if_rtwn_debug.h>
53 #include <dev/rtwn/if_rtwn_rx.h>
54 #include <dev/rtwn/if_rtwn_task.h>
55 #include <dev/rtwn/if_rtwn_tx.h>
56 
57 #include <dev/rtwn/pci/rtwn_pci_var.h>
58 #include <dev/rtwn/pci/rtwn_pci_rx.h>
59 
60 
61 void
62 rtwn_pci_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs,
63     int error)
64 {
65 
66 	if (error != 0)
67 		return;
68 	KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
69 	*(bus_addr_t *)arg = segs[0].ds_addr;
70 }
71 
72 void
73 rtwn_pci_setup_rx_desc(struct rtwn_pci_softc *pc,
74     struct rtwn_rx_stat_pci *desc, bus_addr_t addr, size_t len, int idx)
75 {
76 
77 	memset(desc, 0, sizeof(*desc));
78 	desc->rxdw0 = htole32(SM(RTWN_RXDW0_PKTLEN, len) |
79 		((idx == RTWN_PCI_RX_LIST_COUNT - 1) ? RTWN_RXDW0_EOR : 0));
80 	desc->rxbufaddr = htole32(addr);
81 	bus_space_barrier(pc->pc_st, pc->pc_sh, 0, pc->pc_mapsize,
82 	    BUS_SPACE_BARRIER_WRITE);
83 	desc->rxdw0 |= htole32(RTWN_RXDW0_OWN);
84 }
85 
86 static void
87 rtwn_pci_rx_frame(struct rtwn_pci_softc *pc)
88 {
89 	struct epoch_tracker et;
90 	struct rtwn_softc *sc = &pc->pc_sc;
91 	struct rtwn_rx_ring *ring = &pc->rx_ring;
92 	struct rtwn_rx_stat_pci *rx_desc = &ring->desc[ring->cur];
93 	struct rtwn_rx_data *rx_data = &ring->rx_data[ring->cur];
94 	struct ieee80211com *ic = &sc->sc_ic;
95 	struct ieee80211_node *ni;
96 	uint32_t rxdw0;
97 	struct mbuf *m, *m1;
98 	int infosz, pktlen, shift, error;
99 
100 	/* Dump Rx descriptor. */
101 	RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC,
102 	    "%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, tsfl %08X, "
103 	    "addr: %08X (64: %08X)\n",
104 	    __func__, le32toh(rx_desc->rxdw0), le32toh(rx_desc->rxdw1),
105 	    le32toh(rx_desc->rxdw2), le32toh(rx_desc->rxdw3),
106 	    le32toh(rx_desc->rxdw4), le32toh(rx_desc->tsf_low),
107 	    le32toh(rx_desc->rxbufaddr), le32toh(rx_desc->rxbufaddr64));
108 
109 	rxdw0 = le32toh(rx_desc->rxdw0);
110 	if (__predict_false(rxdw0 & (RTWN_RXDW0_CRCERR | RTWN_RXDW0_ICVERR))) {
111 		/*
112 		 * This should not happen since we setup our Rx filter
113 		 * to not receive these frames.
114 		 */
115 		RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
116 		    "%s: RX flags error (%s)\n", __func__,
117 		    rxdw0 & RTWN_RXDW0_CRCERR ? "CRC" : "ICV");
118 		goto fail;
119 	}
120 
121 	pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN);
122 	if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack) ||
123 	    pktlen > MJUMPAGESIZE)) {
124 		RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
125 		    "%s: frame is too short/long: %d\n", __func__, pktlen);
126 		goto fail;
127 	}
128 
129 	infosz = MS(rxdw0, RTWN_RXDW0_INFOSZ) * 8;
130 	shift = MS(rxdw0, RTWN_RXDW0_SHIFT);
131 
132 	m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
133 	if (__predict_false(m1 == NULL)) {
134 		device_printf(sc->sc_dev, "%s: could not allocate RX mbuf\n",
135 		    __func__);
136 		goto fail;
137 	}
138 	bus_dmamap_sync(ring->data_dmat, rx_data->map, BUS_DMASYNC_POSTREAD);
139 	bus_dmamap_unload(ring->data_dmat, rx_data->map);
140 
141 	error = bus_dmamap_load(ring->data_dmat, rx_data->map, mtod(m1, void *),
142 	    MJUMPAGESIZE, rtwn_pci_dma_map_addr, &rx_data->paddr, 0);
143 	if (error != 0) {
144 		m_freem(m1);
145 
146 		error = bus_dmamap_load(ring->data_dmat, rx_data->map,
147 		    mtod(rx_data->m, void *), MJUMPAGESIZE,
148 		    rtwn_pci_dma_map_addr, &rx_data->paddr, BUS_DMA_NOWAIT);
149 		if (error != 0)
150 			panic("%s: could not load old RX mbuf",
151 			    device_get_name(sc->sc_dev));
152 
153 		goto fail;
154 	}
155 
156 	/* Finalize mbuf. */
157 	m = rx_data->m;
158 	rx_data->m = m1;
159 	m->m_pkthdr.len = m->m_len = pktlen + infosz + shift;
160 
161 	ni = rtwn_rx_common(sc, m, rx_desc);
162 
163 	RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
164 	    "%s: Rx frame len %d, infosz %d, shift %d\n",
165 	    __func__, pktlen, infosz, shift);
166 
167 	/* Send the frame to the 802.11 layer. */
168 	RTWN_UNLOCK(sc);
169 
170 	NET_EPOCH_ENTER(et);
171 	if (ni != NULL) {
172 		(void)ieee80211_input_mimo(ni, m);
173 		/* Node is no longer needed. */
174 		ieee80211_free_node(ni);
175 	} else
176 		(void)ieee80211_input_mimo_all(ic, m);
177 	NET_EPOCH_EXIT(et);
178 
179 	RTWN_LOCK(sc);
180 
181 	return;
182 
183 fail:
184 	counter_u64_add(ic->ic_ierrors, 1);
185 }
186 
187 static int
188 rtwn_pci_rx_buf_copy(struct rtwn_pci_softc *pc)
189 {
190 	struct rtwn_rx_ring *ring = &pc->rx_ring;
191 	struct rtwn_rx_stat_pci *rx_desc = &ring->desc[ring->cur];
192 	struct rtwn_rx_data *rx_data = &ring->rx_data[ring->cur];
193 	uint32_t rxdw0;
194 	int desc_size, pktlen;
195 
196 	/*
197 	 * NB: tx_report() / c2h_report() expects to see USB Rx
198 	 * descriptor - same as for PCIe, but without rxbufaddr* fields.
199 	 */
200 	desc_size = sizeof(struct rtwn_rx_stat_common);
201 	KASSERT(sizeof(pc->pc_rx_buf) >= desc_size,
202 	    ("adjust size for PCIe Rx buffer!"));
203 
204 	memcpy(pc->pc_rx_buf, rx_desc, desc_size);
205 
206 	rxdw0 = le32toh(rx_desc->rxdw0);
207 	pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN);
208 
209 	if (pktlen > sizeof(pc->pc_rx_buf) - desc_size)
210 	{
211 		/* Looks like an ordinary Rx frame. */
212 		return (desc_size);
213 	}
214 
215 	bus_dmamap_sync(ring->data_dmat, rx_data->map, BUS_DMASYNC_POSTREAD);
216 	memcpy(pc->pc_rx_buf + desc_size, mtod(rx_data->m, void *), pktlen);
217 
218 	return (desc_size + pktlen);
219 }
220 
221 static void
222 rtwn_pci_tx_report(struct rtwn_pci_softc *pc, int len)
223 {
224 	struct rtwn_softc *sc = &pc->pc_sc;
225 
226 	if (sc->sc_ratectl != RTWN_RATECTL_NET80211) {
227 		/* shouldn't happen */
228 		device_printf(sc->sc_dev,
229 		    "%s called while ratectl = %d!\n",
230 		     __func__, sc->sc_ratectl);
231 		return;
232 	}
233 
234 	RTWN_NT_LOCK(sc);
235 	rtwn_handle_tx_report(sc, pc->pc_rx_buf, len);
236 	RTWN_NT_UNLOCK(sc);
237 
238 #ifdef IEEE80211_SUPPORT_SUPERG
239 	/*
240 	 * NB: this will executed only when 'report' bit is set.
241 	 */
242 	if (sc->sc_tx_n_active > 0 && --sc->sc_tx_n_active <= 1)
243 		rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
244 #endif
245 }
246 
247 static void
248 rtwn_pci_c2h_report(struct rtwn_pci_softc *pc, int len)
249 {
250 	rtwn_handle_c2h_report(&pc->pc_sc, pc->pc_rx_buf, len);
251 }
252 
253 static void
254 rtwn_pci_tx_done(struct rtwn_softc *sc, int qid)
255 {
256 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
257 	struct rtwn_tx_ring *ring = &pc->tx_ring[qid];
258 	struct rtwn_tx_desc_common *desc;
259 	struct rtwn_tx_data *data;
260 
261 	RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: qid %d, last %d, cur %d\n",
262 	    __func__, qid, ring->last, ring->cur);
263 
264 	bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
265 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
266 
267 	while(ring->last != ring->cur) {
268 		data = &ring->tx_data[ring->last];
269 		desc = (struct rtwn_tx_desc_common *)
270 		    ((uint8_t *)ring->desc + sc->txdesc_len * ring->last);
271 
272 		KASSERT(data->m != NULL, ("no mbuf"));
273 
274 		if (desc->flags0 & RTWN_FLAGS0_OWN)
275 			break;
276 
277 		/* Unmap and free mbuf. */
278 		bus_dmamap_sync(ring->data_dmat, data->map,
279 		    BUS_DMASYNC_POSTWRITE);
280 		bus_dmamap_unload(ring->data_dmat, data->map);
281 
282 		if (data->ni != NULL) {	/* not a beacon frame */
283 			ieee80211_tx_complete(data->ni, data->m, 0);
284 
285 			data->ni = NULL;
286 			ring->queued--;
287 			KASSERT(ring->queued >= 0,
288 			    ("ring->queued (qid %d) underflow!\n", qid));
289 		} else
290 			m_freem(data->m);
291 
292 		data->m = NULL;
293 		ring->last = (ring->last + 1) % RTWN_PCI_TX_LIST_COUNT;
294 #ifndef D4054
295 		if (ring->queued > 0)
296 			sc->sc_tx_timer = 5;
297 		else
298 			sc->sc_tx_timer = 0;
299 #endif
300 	}
301 
302 	if ((sc->qfullmsk & (1 << qid)) != 0 &&
303 	    ring->queued < (RTWN_PCI_TX_LIST_COUNT - 1)) {
304 		sc->qfullmsk &= ~(1 << qid);
305 		rtwn_start(sc);
306 	}
307 
308 #ifdef  IEEE80211_SUPPORT_SUPERG
309 	/*
310 	 * If the TX active queue drops below a certain
311 	 * threshold, ensure we age fast-frames out so they're
312 	 * transmitted.
313 	 */
314 	if (sc->sc_ratectl != RTWN_RATECTL_NET80211 && ring->queued <= 1) {
315 		/*
316 		 * XXX TODO: just make this a callout timer schedule
317 		 * so we can flush the FF staging queue if we're
318 		 * approaching idle.
319 		 */
320 		rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
321 	}
322 #endif
323 }
324 
325 static void
326 rtwn_pci_rx_done(struct rtwn_softc *sc)
327 {
328 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
329 	struct rtwn_rx_ring *ring = &pc->rx_ring;
330 	struct rtwn_rx_stat_pci *rx_desc;
331 	struct rtwn_rx_data *rx_data;
332 	int len;
333 
334 	bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_POSTREAD);
335 
336 	for (;;) {
337 		rx_desc = &ring->desc[ring->cur];
338 		rx_data = &ring->rx_data[ring->cur];
339 
340 		if (le32toh(rx_desc->rxdw0) & RTWN_RXDW0_OWN)
341 			break;
342 
343 		len = rtwn_pci_rx_buf_copy(pc);
344 
345 		switch (rtwn_classify_intr(sc, pc->pc_rx_buf, len)) {
346 		case RTWN_RX_DATA:
347 			rtwn_pci_rx_frame(pc);
348 			break;
349 		case RTWN_RX_TX_REPORT:
350 			rtwn_pci_tx_report(pc, len);
351 			break;
352 		case RTWN_RX_OTHER:
353 			rtwn_pci_c2h_report(pc, len);
354 			break;
355 		default:
356 			/* NOTREACHED */
357 			KASSERT(0, ("unknown Rx classification code"));
358 			break;
359 		}
360 
361 		/* Update / reset RX descriptor (and set OWN bit). */
362 		rtwn_pci_setup_rx_desc(pc, rx_desc, rx_data->paddr,
363 		    MJUMPAGESIZE, ring->cur);
364 
365 		if (!(sc->sc_flags & RTWN_RUNNING))
366 			return;
367 
368 		/* NB: device can reuse current descriptor. */
369 		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
370 		    BUS_DMASYNC_POSTREAD);
371 
372 		if (le32toh(rx_desc->rxdw0) & RTWN_RXDW0_OWN)
373 			ring->cur = (ring->cur + 1) % RTWN_PCI_RX_LIST_COUNT;
374 	}
375 }
376 
377 void
378 rtwn_pci_intr(void *arg)
379 {
380 	struct rtwn_softc *sc = arg;
381 	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
382 	int i, status, tx_rings;
383 
384 	RTWN_LOCK(sc);
385 	status = rtwn_pci_get_intr_status(pc, &tx_rings);
386 	RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: status %08X, tx_rings %08X\n",
387 	    __func__, status, tx_rings);
388 	if (status == 0 && tx_rings == 0)
389 		goto unlock;
390 
391 	if (status & (RTWN_PCI_INTR_RX | RTWN_PCI_INTR_TX_REPORT)) {
392 		rtwn_pci_rx_done(sc);
393 		if (!(sc->sc_flags & RTWN_RUNNING))
394 			goto unlock;
395 	}
396 
397 	if (tx_rings != 0)
398 		for (i = 0; i < RTWN_PCI_NTXQUEUES; i++)
399 			if (tx_rings & (1 << i))
400 				rtwn_pci_tx_done(sc, i);
401 
402 	if (sc->sc_flags & RTWN_RUNNING)
403 		rtwn_pci_enable_intr(pc);
404 unlock:
405 	RTWN_UNLOCK(sc);
406 }
407