xref: /freebsd/sys/dev/rtwn/usb/rtwn_usb_rx.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 /*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6  * Copyright (c) 2015-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 #include "opt_wlan.h"
23 
24 #include <sys/param.h>
25 #include <sys/lock.h>
26 #include <sys/mutex.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/taskqueue.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 
37 #include <net/if.h>
38 #include <net/if_var.h>
39 #include <net/ethernet.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 
43 #include <net80211/ieee80211_var.h>
44 #include <net80211/ieee80211_radiotap.h>
45 #ifdef	IEEE80211_SUPPORT_SUPERG
46 #include <net80211/ieee80211_superg.h>
47 #endif
48 
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 
52 #include <dev/rtwn/if_rtwnreg.h>
53 #include <dev/rtwn/if_rtwnvar.h>
54 
55 #include <dev/rtwn/if_rtwn_debug.h>
56 #include <dev/rtwn/if_rtwn_ridx.h>
57 #include <dev/rtwn/if_rtwn_rx.h>
58 #include <dev/rtwn/if_rtwn_task.h>
59 #include <dev/rtwn/if_rtwn_tx.h>
60 
61 #include <dev/rtwn/usb/rtwn_usb_var.h>
62 #include <dev/rtwn/usb/rtwn_usb_rx.h>
63 
64 static struct mbuf *	rtwn_rxeof(struct rtwn_softc *, struct rtwn_data *,
65 			    uint8_t *, int);
66 
67 static int
68 rtwn_rx_check_pre_alloc(struct rtwn_softc *sc,
69     struct rtwn_rx_stat_common *stat)
70 {
71 	uint32_t rxdw0;
72 	int pktlen;
73 
74 	RTWN_ASSERT_LOCKED(sc);
75 
76 	/*
77 	 * don't pass packets to the ieee80211 framework if the driver isn't
78 	 * RUNNING.
79 	 */
80 	if (!(sc->sc_flags & RTWN_RUNNING))
81 		return (-1);
82 
83 	rxdw0 = le32toh(stat->rxdw0);
84 	if (__predict_false(rxdw0 & (RTWN_RXDW0_CRCERR | RTWN_RXDW0_ICVERR))) {
85 		/*
86 		 * This should not happen since we setup our Rx filter
87 		 * to not receive these frames.
88 		 */
89 		RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
90 		    "%s: RX flags error (%s)\n", __func__,
91 		    rxdw0 & RTWN_RXDW0_CRCERR ? "CRC" : "ICV");
92 		return (-1);
93 	}
94 
95 	pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN);
96 	if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack))) {
97 		/*
98 		 * Should not happen (because of Rx filter setup).
99 		 */
100 		RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
101 		    "%s: frame is too short: %d\n", __func__, pktlen);
102 		return (-1);
103 	}
104 
105 	return (0);
106 }
107 
108 static struct mbuf *
109 rtwn_rx_copy_to_mbuf(struct rtwn_softc *sc, struct rtwn_rx_stat_common *stat,
110     int totlen)
111 {
112 	struct ieee80211com *ic = &sc->sc_ic;
113 	struct mbuf *m;
114 
115 	RTWN_ASSERT_LOCKED(sc);
116 
117 	/* Dump Rx descriptor. */
118 	RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC,
119 	    "%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, tsfl %08X\n",
120 	    __func__, le32toh(stat->rxdw0), le32toh(stat->rxdw1),
121 	    le32toh(stat->rxdw2), le32toh(stat->rxdw3), le32toh(stat->rxdw4),
122 	    le32toh(stat->tsf_low));
123 
124 	if (rtwn_rx_check_pre_alloc(sc, stat) != 0)
125 		goto fail;
126 
127 	m = m_get2(totlen, M_NOWAIT, MT_DATA, M_PKTHDR);
128 	if (__predict_false(m == NULL)) {
129 		device_printf(sc->sc_dev, "%s: could not allocate RX mbuf\n",
130 		    __func__);
131 		goto fail;
132 	}
133 
134 	/* Finalize mbuf. */
135 	memcpy(mtod(m, uint8_t *), (uint8_t *)stat, totlen);
136 	m->m_pkthdr.len = m->m_len = totlen;
137 
138 	if (rtwn_check_frame(sc, m) != 0) {
139 		m_freem(m);
140 		goto fail;
141 	}
142 
143 	return (m);
144 fail:
145 	counter_u64_add(ic->ic_ierrors, 1);
146 	return (NULL);
147 }
148 
149 static struct mbuf *
150 rtwn_rxeof_fragmented(struct rtwn_usb_softc *uc, struct rtwn_data *data,
151     uint8_t *buf, int len)
152 {
153 	struct rtwn_softc *sc = &uc->uc_sc;
154 	struct ieee80211com *ic = &sc->sc_ic;
155 	struct rtwn_rx_stat_common *stat = &uc->uc_rx_stat;
156 	uint32_t rxdw0;
157 	int totlen, pktlen, infosz, min_len;
158 	int orig_len = len;
159 	int alloc_mbuf = 0;
160 
161 	/* Check if Rx descriptor is not truncated. */
162 	if (uc->uc_rx_stat_len < sizeof(*stat)) {
163 		min_len = min(sizeof(*stat) - uc->uc_rx_stat_len, len);
164 		memcpy((uint8_t *)stat + uc->uc_rx_stat_len, buf, min_len);
165 
166 		uc->uc_rx_stat_len += min_len;
167 		buf += min_len;
168 		len -= min_len;
169 
170 		if (uc->uc_rx_stat_len < sizeof(*stat))
171 			goto end;
172 
173 		KASSERT(data->m == NULL, ("%s: data->m != NULL!\n", __func__));
174 		alloc_mbuf = 1;
175 
176 		/* Dump Rx descriptor. */
177 		RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC,
178 		    "%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, "
179 		    "tsfl %08X\n", __func__, le32toh(stat->rxdw0),
180 		    le32toh(stat->rxdw1), le32toh(stat->rxdw2),
181 		    le32toh(stat->rxdw3), le32toh(stat->rxdw4),
182 		    le32toh(stat->tsf_low));
183 	}
184 
185 	rxdw0 = le32toh(stat->rxdw0);
186 	pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN);
187 	infosz = MS(rxdw0, RTWN_RXDW0_INFOSZ) * 8;
188 	totlen = sizeof(*stat) + infosz + pktlen;
189 	if (alloc_mbuf) {
190 		if (rtwn_rx_check_pre_alloc(sc, stat) == 0) {
191 			data->m = m_getm(NULL, totlen, M_NOWAIT, MT_DATA);
192 			if (data->m != NULL) {
193 				m_copyback(data->m, 0, uc->uc_rx_stat_len,
194 				    (caddr_t)stat);
195 
196 				if (rtwn_check_frame(sc, data->m) != 0) {
197 					m_freem(data->m);
198 					data->m = NULL;
199 					counter_u64_add(ic->ic_ierrors, 1);
200 				}
201 			} else
202 				counter_u64_add(ic->ic_ierrors, 1);
203 		} else
204 			counter_u64_add(ic->ic_ierrors, 1);
205 
206 		uc->uc_rx_off = sizeof(*stat);
207 	}
208 
209 	/* If mbuf allocation fails just discard the data. */
210 	min_len = min(totlen - uc->uc_rx_off, len);
211 	if (data->m != NULL)
212 		m_copyback(data->m, uc->uc_rx_off, min_len, buf);
213 
214 	uc->uc_rx_off += min_len;
215 	if (uc->uc_rx_off == totlen) {
216 		/* Align next frame. */
217 		min_len = rtwn_usb_align_rx(uc,
218 		    orig_len - len + min_len, orig_len);
219 		min_len -= (orig_len - len);
220 		KASSERT(len >= min_len, ("%s: len (%d) < min_len (%d)!\n",
221 		    __func__, len, min_len));
222 
223 		/* Clear mbuf stats. */
224 		uc->uc_rx_stat_len = 0;
225 		uc->uc_rx_off = 0;
226 	}
227 	len -= min_len;
228 	buf += min_len;
229 end:
230 	if (uc->uc_rx_stat_len == 0)
231 		return (rtwn_rxeof(sc, data, buf, len));
232 	else
233 		return (NULL);
234 }
235 
236 static struct mbuf *
237 rtwn_rxeof(struct rtwn_softc *sc, struct rtwn_data *data, uint8_t *buf,
238     int len)
239 {
240 	struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc);
241 	struct rtwn_rx_stat_common *stat;
242 	struct mbuf *m, *m0 = NULL;
243 	uint32_t rxdw0;
244 	int totlen, pktlen, infosz;
245 
246 	/* Prepend defragmented frame (if any). */
247 	if (data->m != NULL) {
248 		m0 = m = data->m;
249 		data->m = NULL;
250 	}
251 
252 	/* Process packets. */
253 	while (len >= sizeof(*stat)) {
254 		stat = (struct rtwn_rx_stat_common *)buf;
255 		rxdw0 = le32toh(stat->rxdw0);
256 
257 		pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN);
258 		if (__predict_false(pktlen == 0))
259 			break;
260 
261 		infosz = MS(rxdw0, RTWN_RXDW0_INFOSZ) * 8;
262 
263 		/* Make sure everything fits in xfer. */
264 		totlen = sizeof(*stat) + infosz + pktlen;
265 		if (totlen > len) {
266 			RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
267 			    "%s: frame is fragmented (totlen %d len %d)\n",
268 			    __func__, totlen, len);
269 			break;
270 		}
271 
272 		if (m0 == NULL)
273 			m0 = m = rtwn_rx_copy_to_mbuf(sc, stat, totlen);
274 		else {
275 			m->m_nextpkt = rtwn_rx_copy_to_mbuf(sc, stat, totlen);
276 			if (m->m_nextpkt != NULL)
277 				m = m->m_nextpkt;
278 		}
279 
280 		/* Align next frame. */
281 		totlen = rtwn_usb_align_rx(uc, totlen, len);
282 		buf += totlen;
283 		len -= totlen;
284 	}
285 
286 	if (len > 0)
287 		(void)rtwn_rxeof_fragmented(uc, data, buf, len);
288 
289 	return (m0);
290 }
291 
292 static struct mbuf *
293 rtwn_report_intr(struct rtwn_usb_softc *uc, struct usb_xfer *xfer,
294     struct rtwn_data *data)
295 {
296 	struct rtwn_softc *sc = &uc->uc_sc;
297 	struct ieee80211com *ic = &sc->sc_ic;
298 	uint8_t *buf;
299 	int len;
300 
301 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
302 
303 	if (__predict_false(len < sizeof(struct rtwn_rx_stat_common) &&
304 	    uc->uc_rx_stat_len == 0)) {
305 		counter_u64_add(ic->ic_ierrors, 1);
306 		return (NULL);
307 	}
308 
309 	buf = data->buf;
310 	if (uc->uc_rx_stat_len > 0)
311 		return (rtwn_rxeof_fragmented(uc, data, data->buf, len));
312 
313 	switch (rtwn_classify_intr(sc, buf, len)) {
314 	case RTWN_RX_DATA:
315 		return (rtwn_rxeof(sc, data, buf, len));
316 	case RTWN_RX_TX_REPORT:
317 		if (sc->sc_ratectl != RTWN_RATECTL_NET80211) {
318 			/* shouldn't happen */
319 			device_printf(sc->sc_dev,
320 			    "%s called while ratectl = %d!\n",
321 			    __func__, sc->sc_ratectl);
322 			break;
323 		}
324 
325 		RTWN_NT_LOCK(sc);
326 		rtwn_handle_tx_report(sc, buf, len);
327 		RTWN_NT_UNLOCK(sc);
328 
329 #ifdef IEEE80211_SUPPORT_SUPERG
330 		/*
331 		 * NB: this will executed only when 'report' bit is set.
332 		 */
333 		if (sc->sc_tx_n_active > 0 && --sc->sc_tx_n_active <= 1)
334 			rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
335 #endif
336 		break;
337 	case RTWN_RX_OTHER:
338 		rtwn_handle_c2h_report(sc, buf, len);
339 		break;
340 	default:
341 		/* NOTREACHED */
342 		KASSERT(0, ("unknown Rx classification code"));
343 		break;
344 	}
345 
346 	return (NULL);
347 }
348 
349 static struct ieee80211_node *
350 rtwn_rx_frame(struct rtwn_softc *sc, struct mbuf *m)
351 {
352 	struct rtwn_rx_stat_common stat;
353 
354 	/* Imitate PCIe layout. */
355 	m_copydata(m, 0, sizeof(stat), (caddr_t)&stat);
356 	m_adj(m, sizeof(stat));
357 
358 	return (rtwn_rx_common(sc, m, &stat));
359 }
360 
361 void
362 rtwn_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
363 {
364 	struct rtwn_usb_softc *uc = usbd_xfer_softc(xfer);
365 	struct rtwn_softc *sc = &uc->uc_sc;
366 	struct ieee80211com *ic = &sc->sc_ic;
367 	struct ieee80211_node *ni;
368 	struct mbuf *m0, *m = NULL, *next;
369 	struct rtwn_data *data;
370 
371 	RTWN_ASSERT_LOCKED(sc);
372 
373 	switch (USB_GET_STATE(xfer)) {
374 	case USB_ST_TRANSFERRED:
375 		data = STAILQ_FIRST(&uc->uc_rx_active);
376 		if (data == NULL)
377 			goto tr_setup;
378 		STAILQ_REMOVE_HEAD(&uc->uc_rx_active, next);
379 		m = rtwn_report_intr(uc, xfer, data);
380 		STAILQ_INSERT_TAIL(&uc->uc_rx_inactive, data, next);
381 		/* FALLTHROUGH */
382 	case USB_ST_SETUP:
383 tr_setup:
384 		data = STAILQ_FIRST(&uc->uc_rx_inactive);
385 		if (data == NULL) {
386 			KASSERT(m == NULL, ("mbuf isn't NULL"));
387 			goto finish;
388 		}
389 		STAILQ_REMOVE_HEAD(&uc->uc_rx_inactive, next);
390 		STAILQ_INSERT_TAIL(&uc->uc_rx_active, data, next);
391 		usbd_xfer_set_frame_data(xfer, 0, data->buf,
392 		    usbd_xfer_max_len(xfer));
393 		usbd_transfer_submit(xfer);
394 
395 		/*
396 		 * To avoid LOR we should unlock our private mutex here to call
397 		 * ieee80211_input() because here is at the end of a USB
398 		 * callback and safe to unlock.
399 		 */
400 		m0 = m;
401 		while (m != NULL) {
402 			M_ASSERTPKTHDR(m);
403 			m->m_pkthdr.PH_loc.ptr = rtwn_rx_frame(sc, m);
404 			m = m->m_nextpkt;
405 		}
406 		RTWN_UNLOCK(sc);
407 		m = m0;
408 		while (m != NULL) {
409 			next = m->m_nextpkt;
410 			m->m_nextpkt = NULL;
411 
412 			ni = m->m_pkthdr.PH_loc.ptr;
413 			m->m_pkthdr.PH_loc.ptr = NULL;
414 			if (ni != NULL) {
415 				(void)ieee80211_input_mimo(ni, m);
416 				ieee80211_free_node(ni);
417 			} else {
418 				(void)ieee80211_input_mimo_all(ic, m);
419 			}
420 			m = next;
421 		}
422 		RTWN_LOCK(sc);
423 		break;
424 	default:
425 		/* needs it to the inactive queue due to a error. */
426 		data = STAILQ_FIRST(&uc->uc_rx_active);
427 		if (data != NULL) {
428 			STAILQ_REMOVE_HEAD(&uc->uc_rx_active, next);
429 			STAILQ_INSERT_TAIL(&uc->uc_rx_inactive, data, next);
430 		}
431 		if (error != USB_ERR_CANCELLED) {
432 			/* XXX restart device if frame was fragmented? */
433 
434 			usbd_xfer_set_stall(xfer);
435 			counter_u64_add(ic->ic_ierrors, 1);
436 			goto tr_setup;
437 		}
438 		break;
439 	}
440 finish:
441 	/* Kick-start more transmit in case we stalled */
442 	rtwn_start(sc);
443 }
444