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 __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 39 #include <net/if.h> 40 #include <net/if_var.h> 41 #include <net/ethernet.h> 42 #include <net/if_dl.h> 43 #include <net/if_media.h> 44 45 #include <net80211/ieee80211_var.h> 46 #include <net80211/ieee80211_radiotap.h> 47 #ifdef IEEE80211_SUPPORT_SUPERG 48 #include <net80211/ieee80211_superg.h> 49 #endif 50 51 #include <dev/usb/usb.h> 52 #include <dev/usb/usbdi.h> 53 54 #include <dev/rtwn/if_rtwnreg.h> 55 #include <dev/rtwn/if_rtwnvar.h> 56 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> 62 63 #include <dev/rtwn/usb/rtwn_usb_var.h> 64 #include <dev/rtwn/usb/rtwn_usb_rx.h> 65 66 67 static struct mbuf * 68 rtwn_rx_copy_to_mbuf(struct rtwn_softc *sc, struct rtwn_rx_stat_common *stat, 69 int totlen) 70 { 71 struct ieee80211com *ic = &sc->sc_ic; 72 struct mbuf *m; 73 uint32_t rxdw0; 74 int pktlen; 75 76 RTWN_ASSERT_LOCKED(sc); 77 78 /* Dump Rx descriptor. */ 79 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC, 80 "%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, tsfl %08X\n", 81 __func__, le32toh(stat->rxdw0), le32toh(stat->rxdw1), 82 le32toh(stat->rxdw2), le32toh(stat->rxdw3), le32toh(stat->rxdw4), 83 le32toh(stat->tsf_low)); 84 85 /* 86 * don't pass packets to the ieee80211 framework if the driver isn't 87 * RUNNING. 88 */ 89 if (!(sc->sc_flags & RTWN_RUNNING)) 90 return (NULL); 91 92 rxdw0 = le32toh(stat->rxdw0); 93 if (__predict_false(rxdw0 & (RTWN_RXDW0_CRCERR | RTWN_RXDW0_ICVERR))) { 94 /* 95 * This should not happen since we setup our Rx filter 96 * to not receive these frames. 97 */ 98 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, 99 "%s: RX flags error (%s)\n", __func__, 100 rxdw0 & RTWN_RXDW0_CRCERR ? "CRC" : "ICV"); 101 goto fail; 102 } 103 104 pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN); 105 if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack))) { 106 /* 107 * Should not happen (because of Rx filter setup). 108 */ 109 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, 110 "%s: frame is too short: %d\n", __func__, pktlen); 111 goto fail; 112 } 113 114 m = m_get2(totlen, M_NOWAIT, MT_DATA, M_PKTHDR); 115 if (__predict_false(m == NULL)) { 116 device_printf(sc->sc_dev, "%s: could not allocate RX mbuf\n", 117 __func__); 118 goto fail; 119 } 120 121 /* Finalize mbuf. */ 122 memcpy(mtod(m, uint8_t *), (uint8_t *)stat, totlen); 123 m->m_pkthdr.len = m->m_len = totlen; 124 125 if (rtwn_check_frame(sc, m) != 0) { 126 m_freem(m); 127 goto fail; 128 } 129 130 return (m); 131 fail: 132 counter_u64_add(ic->ic_ierrors, 1); 133 return (NULL); 134 } 135 136 static struct mbuf * 137 rtwn_rxeof(struct rtwn_softc *sc, uint8_t *buf, int len) 138 { 139 struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc); 140 struct rtwn_rx_stat_common *stat; 141 struct mbuf *m, *m0 = NULL; 142 uint32_t rxdw0; 143 int totlen, pktlen, infosz; 144 145 /* Process packets. */ 146 while (len >= sizeof(*stat)) { 147 stat = (struct rtwn_rx_stat_common *)buf; 148 rxdw0 = le32toh(stat->rxdw0); 149 150 pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN); 151 if (__predict_false(pktlen == 0)) 152 break; 153 154 infosz = MS(rxdw0, RTWN_RXDW0_INFOSZ) * 8; 155 156 /* Make sure everything fits in xfer. */ 157 totlen = sizeof(*stat) + infosz + pktlen; 158 if (totlen > len) { 159 device_printf(sc->sc_dev, 160 "%s: totlen (%d) > len (%d)!\n", 161 __func__, totlen, len); 162 break; 163 } 164 165 if (m0 == NULL) 166 m0 = m = rtwn_rx_copy_to_mbuf(sc, stat, totlen); 167 else { 168 m->m_next = rtwn_rx_copy_to_mbuf(sc, stat, totlen); 169 if (m->m_next != NULL) 170 m = m->m_next; 171 } 172 173 /* Align next frame. */ 174 totlen = rtwn_usb_align_rx(uc, totlen, len); 175 buf += totlen; 176 len -= totlen; 177 } 178 179 return (m0); 180 } 181 182 static struct mbuf * 183 rtwn_report_intr(struct rtwn_usb_softc *uc, struct usb_xfer *xfer, 184 struct rtwn_data *data) 185 { 186 struct rtwn_softc *sc = &uc->uc_sc; 187 struct ieee80211com *ic = &sc->sc_ic; 188 uint8_t *buf; 189 int len; 190 191 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 192 193 if (__predict_false(len < sizeof(struct rtwn_rx_stat_common))) { 194 counter_u64_add(ic->ic_ierrors, 1); 195 return (NULL); 196 } 197 198 buf = data->buf; 199 switch (rtwn_classify_intr(sc, buf, len)) { 200 case RTWN_RX_DATA: 201 return (rtwn_rxeof(sc, buf, len)); 202 case RTWN_RX_TX_REPORT: 203 if (sc->sc_ratectl != RTWN_RATECTL_NET80211) { 204 /* shouldn't happen */ 205 device_printf(sc->sc_dev, 206 "%s called while ratectl = %d!\n", 207 __func__, sc->sc_ratectl); 208 break; 209 } 210 211 RTWN_NT_LOCK(sc); 212 rtwn_handle_tx_report(sc, buf, len); 213 RTWN_NT_UNLOCK(sc); 214 215 #ifdef IEEE80211_SUPPORT_SUPERG 216 /* 217 * NB: this will executed only when 'report' bit is set. 218 */ 219 if (sc->sc_tx_n_active > 0 && --sc->sc_tx_n_active <= 1) 220 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all); 221 #endif 222 break; 223 case RTWN_RX_OTHER: 224 rtwn_handle_c2h_report(sc, buf, len); 225 break; 226 default: 227 /* NOTREACHED */ 228 KASSERT(0, ("unknown Rx classification code")); 229 break; 230 } 231 232 return (NULL); 233 } 234 235 static struct ieee80211_node * 236 rtwn_rx_frame(struct rtwn_softc *sc, struct mbuf *m) 237 { 238 struct rtwn_rx_stat_common stat; 239 240 /* Imitate PCIe layout. */ 241 m_copydata(m, 0, sizeof(stat), (caddr_t)&stat); 242 m_adj(m, sizeof(stat)); 243 244 return (rtwn_rx_common(sc, m, &stat)); 245 } 246 247 void 248 rtwn_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 249 { 250 struct rtwn_usb_softc *uc = usbd_xfer_softc(xfer); 251 struct rtwn_softc *sc = &uc->uc_sc; 252 struct ieee80211com *ic = &sc->sc_ic; 253 struct ieee80211_node *ni; 254 struct mbuf *m = NULL, *next; 255 struct rtwn_data *data; 256 257 RTWN_ASSERT_LOCKED(sc); 258 259 switch (USB_GET_STATE(xfer)) { 260 case USB_ST_TRANSFERRED: 261 data = STAILQ_FIRST(&uc->uc_rx_active); 262 if (data == NULL) 263 goto tr_setup; 264 STAILQ_REMOVE_HEAD(&uc->uc_rx_active, next); 265 m = rtwn_report_intr(uc, xfer, data); 266 STAILQ_INSERT_TAIL(&uc->uc_rx_inactive, data, next); 267 /* FALLTHROUGH */ 268 case USB_ST_SETUP: 269 tr_setup: 270 data = STAILQ_FIRST(&uc->uc_rx_inactive); 271 if (data == NULL) { 272 KASSERT(m == NULL, ("mbuf isn't NULL")); 273 goto finish; 274 } 275 STAILQ_REMOVE_HEAD(&uc->uc_rx_inactive, next); 276 STAILQ_INSERT_TAIL(&uc->uc_rx_active, data, next); 277 usbd_xfer_set_frame_data(xfer, 0, data->buf, 278 usbd_xfer_max_len(xfer)); 279 usbd_transfer_submit(xfer); 280 281 /* 282 * To avoid LOR we should unlock our private mutex here to call 283 * ieee80211_input() because here is at the end of a USB 284 * callback and safe to unlock. 285 */ 286 while (m != NULL) { 287 next = m->m_next; 288 m->m_next = NULL; 289 290 ni = rtwn_rx_frame(sc, m); 291 292 RTWN_UNLOCK(sc); 293 294 if (ni != NULL) { 295 (void)ieee80211_input_mimo(ni, m); 296 ieee80211_free_node(ni); 297 } else { 298 (void)ieee80211_input_mimo_all(ic, m); 299 } 300 RTWN_LOCK(sc); 301 m = next; 302 } 303 break; 304 default: 305 /* needs it to the inactive queue due to a error. */ 306 data = STAILQ_FIRST(&uc->uc_rx_active); 307 if (data != NULL) { 308 STAILQ_REMOVE_HEAD(&uc->uc_rx_active, next); 309 STAILQ_INSERT_TAIL(&uc->uc_rx_inactive, data, next); 310 } 311 if (error != USB_ERR_CANCELLED) { 312 usbd_xfer_set_stall(xfer); 313 counter_u64_add(ic->ic_ierrors, 1); 314 goto tr_setup; 315 } 316 break; 317 } 318 finish: 319 /* Kick-start more transmit in case we stalled */ 320 rtwn_start(sc); 321 } 322