1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
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>
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>
40 #include <net/if_var.h>
41 #include <net/ethernet.h>
42 #include <net/if_dl.h>
43 #include <net/if_media.h>
45 #include <net80211/ieee80211_var.h>
46 #include <net80211/ieee80211_radiotap.h>
47 #ifdef IEEE80211_SUPPORT_SUPERG
48 #include <net80211/ieee80211_superg.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
54 #include <dev/rtwn/if_rtwnreg.h>
55 #include <dev/rtwn/if_rtwnvar.h>
57 #include <dev/rtwn/if_rtwn_debug.h>
58 #include <dev/rtwn/if_rtwn_ridx.h>
59 #include <dev/rtwn/if_rtwn_rx.h>
60 #include <dev/rtwn/if_rtwn_task.h>
61 #include <dev/rtwn/if_rtwn_tx.h>
63 #include <dev/rtwn/usb/rtwn_usb_var.h>
64 #include <dev/rtwn/usb/rtwn_usb_rx.h>
66 #include <dev/rtwn/rtl8192c/r92c_reg.h> /* for CAM_ALGO_NONE */
67 #include <dev/rtwn/rtl8192c/r92c_rx_desc.h>
71 rtwn_rx_copy_to_mbuf(struct rtwn_softc *sc, struct r92c_rx_stat *stat,
74 struct ieee80211com *ic = &sc->sc_ic;
79 RTWN_ASSERT_LOCKED(sc);
81 /* Dump Rx descriptor. */
82 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC,
83 "%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, tsfl %08X\n",
84 __func__, le32toh(stat->rxdw0), le32toh(stat->rxdw1),
85 le32toh(stat->rxdw2), le32toh(stat->rxdw3), le32toh(stat->rxdw4),
86 le32toh(stat->tsf_low));
89 * don't pass packets to the ieee80211 framework if the driver isn't
92 if (!(sc->sc_flags & RTWN_RUNNING))
95 rxdw0 = le32toh(stat->rxdw0);
96 if (__predict_false(rxdw0 & (R92C_RXDW0_CRCERR | R92C_RXDW0_ICVERR))) {
98 * This should not happen since we setup our Rx filter
99 * to not receive these frames.
101 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
102 "%s: RX flags error (%s)\n", __func__,
103 rxdw0 & R92C_RXDW0_CRCERR ? "CRC" : "ICV");
107 pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN);
108 if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack))) {
110 * Should not happen (because of Rx filter setup).
112 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV,
113 "%s: frame is too short: %d\n", __func__, pktlen);
117 m = m_get2(totlen, M_NOWAIT, MT_DATA, M_PKTHDR);
118 if (__predict_false(m == NULL)) {
119 device_printf(sc->sc_dev, "%s: could not allocate RX mbuf\n",
125 memcpy(mtod(m, uint8_t *), (uint8_t *)stat, totlen);
126 m->m_pkthdr.len = m->m_len = totlen;
128 if (rtwn_check_frame(sc, m) != 0) {
135 counter_u64_add(ic->ic_ierrors, 1);
140 rtwn_rxeof(struct rtwn_softc *sc, uint8_t *buf, int len)
142 struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc);
143 struct r92c_rx_stat *stat;
144 struct mbuf *m, *m0 = NULL;
146 int totlen, pktlen, infosz;
148 /* Process packets. */
149 while (len >= sizeof(*stat)) {
150 stat = (struct r92c_rx_stat *)buf;
151 rxdw0 = le32toh(stat->rxdw0);
153 pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN);
154 if (__predict_false(pktlen == 0))
157 infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8;
159 /* Make sure everything fits in xfer. */
160 totlen = sizeof(*stat) + infosz + pktlen;
162 device_printf(sc->sc_dev,
163 "%s: totlen (%d) > len (%d)!\n",
164 __func__, totlen, len);
169 m0 = m = rtwn_rx_copy_to_mbuf(sc, stat, totlen);
171 m->m_next = rtwn_rx_copy_to_mbuf(sc, stat, totlen);
172 if (m->m_next != NULL)
176 /* Align next frame. */
177 totlen = rtwn_usb_align_rx(uc, totlen, len);
186 rtwn_report_intr(struct rtwn_usb_softc *uc, struct usb_xfer *xfer,
187 struct rtwn_data *data)
189 struct rtwn_softc *sc = &uc->uc_sc;
190 struct ieee80211com *ic = &sc->sc_ic;
194 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
196 if (__predict_false(len < sizeof(struct r92c_rx_stat))) {
197 counter_u64_add(ic->ic_ierrors, 1);
202 switch (rtwn_classify_intr(sc, buf, len)) {
204 return (rtwn_rxeof(sc, buf, len));
205 case RTWN_RX_TX_REPORT:
206 if (sc->sc_ratectl != RTWN_RATECTL_NET80211) {
207 /* shouldn't happen */
208 device_printf(sc->sc_dev,
209 "%s called while ratectl = %d!\n",
210 __func__, sc->sc_ratectl);
215 rtwn_handle_tx_report(sc, buf, len);
218 #ifdef IEEE80211_SUPPORT_SUPERG
220 * NB: this will executed only when 'report' bit is set.
222 if (sc->sc_tx_n_active > 0 && --sc->sc_tx_n_active <= 1)
223 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
227 rtwn_handle_c2h_report(sc, buf, len);
231 KASSERT(0, ("unknown Rx classification code"));
238 static struct ieee80211_node *
239 rtwn_rx_frame(struct rtwn_softc *sc, struct mbuf *m)
241 struct r92c_rx_stat stat;
243 /* Imitate PCIe layout. */
244 m_copydata(m, 0, sizeof(struct r92c_rx_stat), (caddr_t)&stat);
245 m_adj(m, sizeof(struct r92c_rx_stat));
247 return (rtwn_rx_common(sc, m, &stat));
251 rtwn_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
253 struct rtwn_usb_softc *uc = usbd_xfer_softc(xfer);
254 struct rtwn_softc *sc = &uc->uc_sc;
255 struct ieee80211com *ic = &sc->sc_ic;
256 struct ieee80211_node *ni;
257 struct mbuf *m = NULL, *next;
258 struct rtwn_data *data;
260 RTWN_ASSERT_LOCKED(sc);
262 switch (USB_GET_STATE(xfer)) {
263 case USB_ST_TRANSFERRED:
264 data = STAILQ_FIRST(&uc->uc_rx_active);
267 STAILQ_REMOVE_HEAD(&uc->uc_rx_active, next);
268 m = rtwn_report_intr(uc, xfer, data);
269 STAILQ_INSERT_TAIL(&uc->uc_rx_inactive, data, next);
273 data = STAILQ_FIRST(&uc->uc_rx_inactive);
275 KASSERT(m == NULL, ("mbuf isn't NULL"));
278 STAILQ_REMOVE_HEAD(&uc->uc_rx_inactive, next);
279 STAILQ_INSERT_TAIL(&uc->uc_rx_active, data, next);
280 usbd_xfer_set_frame_data(xfer, 0, data->buf,
281 usbd_xfer_max_len(xfer));
282 usbd_transfer_submit(xfer);
285 * To avoid LOR we should unlock our private mutex here to call
286 * ieee80211_input() because here is at the end of a USB
287 * callback and safe to unlock.
293 ni = rtwn_rx_frame(sc, m);
298 (void)ieee80211_input_mimo(ni, m);
299 ieee80211_free_node(ni);
301 (void)ieee80211_input_mimo_all(ic, m);
308 /* needs it to the inactive queue due to a error. */
309 data = STAILQ_FIRST(&uc->uc_rx_active);
311 STAILQ_REMOVE_HEAD(&uc->uc_rx_active, next);
312 STAILQ_INSERT_TAIL(&uc->uc_rx_inactive, data, next);
314 if (error != USB_ERR_CANCELLED) {
315 usbd_xfer_set_stall(xfer);
316 counter_u64_add(ic->ic_ierrors, 1);
322 /* Kick-start more transmit in case we stalled */