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 #include <dev/rtwn/rtl8192c/r92c_reg.h> /* for CAM_ALGO_NONE */ 67 #include <dev/rtwn/rtl8192c/r92c_rx_desc.h> 68 69 70 static struct mbuf * 71 rtwn_rx_copy_to_mbuf(struct rtwn_softc *sc, struct r92c_rx_stat *stat, 72 int totlen) 73 { 74 struct ieee80211com *ic = &sc->sc_ic; 75 struct mbuf *m; 76 uint32_t rxdw0; 77 int pktlen; 78 79 RTWN_ASSERT_LOCKED(sc); 80 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)); 87 88 /* 89 * don't pass packets to the ieee80211 framework if the driver isn't 90 * RUNNING. 91 */ 92 if (!(sc->sc_flags & RTWN_RUNNING)) 93 return (NULL); 94 95 rxdw0 = le32toh(stat->rxdw0); 96 if (__predict_false(rxdw0 & (R92C_RXDW0_CRCERR | R92C_RXDW0_ICVERR))) { 97 /* 98 * This should not happen since we setup our Rx filter 99 * to not receive these frames. 100 */ 101 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, 102 "%s: RX flags error (%s)\n", __func__, 103 rxdw0 & R92C_RXDW0_CRCERR ? "CRC" : "ICV"); 104 goto fail; 105 } 106 107 pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN); 108 if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack))) { 109 /* 110 * Should not happen (because of Rx filter setup). 111 */ 112 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, 113 "%s: frame is too short: %d\n", __func__, pktlen); 114 goto fail; 115 } 116 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", 120 __func__); 121 goto fail; 122 } 123 124 /* Finalize mbuf. */ 125 memcpy(mtod(m, uint8_t *), (uint8_t *)stat, totlen); 126 m->m_pkthdr.len = m->m_len = totlen; 127 128 if (rtwn_check_frame(sc, m) != 0) { 129 m_freem(m); 130 goto fail; 131 } 132 133 return (m); 134 fail: 135 counter_u64_add(ic->ic_ierrors, 1); 136 return (NULL); 137 } 138 139 static struct mbuf * 140 rtwn_rxeof(struct rtwn_softc *sc, uint8_t *buf, int len) 141 { 142 struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc); 143 struct r92c_rx_stat *stat; 144 struct mbuf *m, *m0 = NULL; 145 uint32_t rxdw0; 146 int totlen, pktlen, infosz; 147 148 /* Process packets. */ 149 while (len >= sizeof(*stat)) { 150 stat = (struct r92c_rx_stat *)buf; 151 rxdw0 = le32toh(stat->rxdw0); 152 153 pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN); 154 if (__predict_false(pktlen == 0)) 155 break; 156 157 infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8; 158 159 /* Make sure everything fits in xfer. */ 160 totlen = sizeof(*stat) + infosz + pktlen; 161 if (totlen > len) { 162 device_printf(sc->sc_dev, 163 "%s: totlen (%d) > len (%d)!\n", 164 __func__, totlen, len); 165 break; 166 } 167 168 if (m0 == NULL) 169 m0 = m = rtwn_rx_copy_to_mbuf(sc, stat, totlen); 170 else { 171 m->m_next = rtwn_rx_copy_to_mbuf(sc, stat, totlen); 172 if (m->m_next != NULL) 173 m = m->m_next; 174 } 175 176 /* Align next frame. */ 177 totlen = rtwn_usb_align_rx(uc, totlen, len); 178 buf += totlen; 179 len -= totlen; 180 } 181 182 return (m0); 183 } 184 185 static struct mbuf * 186 rtwn_report_intr(struct rtwn_usb_softc *uc, struct usb_xfer *xfer, 187 struct rtwn_data *data) 188 { 189 struct rtwn_softc *sc = &uc->uc_sc; 190 struct ieee80211com *ic = &sc->sc_ic; 191 uint8_t *buf; 192 int len; 193 194 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 195 196 if (__predict_false(len < sizeof(struct r92c_rx_stat))) { 197 counter_u64_add(ic->ic_ierrors, 1); 198 return (NULL); 199 } 200 201 buf = data->buf; 202 switch (rtwn_classify_intr(sc, buf, len)) { 203 case RTWN_RX_DATA: 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); 211 break; 212 } 213 214 RTWN_NT_LOCK(sc); 215 rtwn_handle_tx_report(sc, buf, len); 216 RTWN_NT_UNLOCK(sc); 217 218 #ifdef IEEE80211_SUPPORT_SUPERG 219 /* 220 * NB: this will executed only when 'report' bit is set. 221 */ 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); 224 #endif 225 break; 226 case RTWN_RX_OTHER: 227 rtwn_handle_c2h_report(sc, buf, len); 228 break; 229 default: 230 /* NOTREACHED */ 231 KASSERT(0, ("unknown Rx classification code")); 232 break; 233 } 234 235 return (NULL); 236 } 237 238 static struct ieee80211_node * 239 rtwn_rx_frame(struct rtwn_softc *sc, struct mbuf *m) 240 { 241 struct r92c_rx_stat stat; 242 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)); 246 247 return (rtwn_rx_common(sc, m, &stat)); 248 } 249 250 void 251 rtwn_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 252 { 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; 259 260 RTWN_ASSERT_LOCKED(sc); 261 262 switch (USB_GET_STATE(xfer)) { 263 case USB_ST_TRANSFERRED: 264 data = STAILQ_FIRST(&uc->uc_rx_active); 265 if (data == NULL) 266 goto tr_setup; 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); 270 /* FALLTHROUGH */ 271 case USB_ST_SETUP: 272 tr_setup: 273 data = STAILQ_FIRST(&uc->uc_rx_inactive); 274 if (data == NULL) { 275 KASSERT(m == NULL, ("mbuf isn't NULL")); 276 goto finish; 277 } 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); 283 284 /* 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. 288 */ 289 while (m != NULL) { 290 next = m->m_next; 291 m->m_next = NULL; 292 293 ni = rtwn_rx_frame(sc, m); 294 295 RTWN_UNLOCK(sc); 296 297 if (ni != NULL) { 298 (void)ieee80211_input_mimo(ni, m); 299 ieee80211_free_node(ni); 300 } else { 301 (void)ieee80211_input_mimo_all(ic, m); 302 } 303 RTWN_LOCK(sc); 304 m = next; 305 } 306 break; 307 default: 308 /* needs it to the inactive queue due to a error. */ 309 data = STAILQ_FIRST(&uc->uc_rx_active); 310 if (data != NULL) { 311 STAILQ_REMOVE_HEAD(&uc->uc_rx_active, next); 312 STAILQ_INSERT_TAIL(&uc->uc_rx_inactive, data, next); 313 } 314 if (error != USB_ERR_CANCELLED) { 315 usbd_xfer_set_stall(xfer); 316 counter_u64_add(ic->ic_ierrors, 1); 317 goto tr_setup; 318 } 319 break; 320 } 321 finish: 322 /* Finished receive; age anything left on the FF queue by a little bump */ 323 /* 324 * XXX TODO: just make this a callout timer schedule so we can 325 * flush the FF staging queue if we're approaching idle. 326 */ 327 #ifdef IEEE80211_SUPPORT_SUPERG 328 if (!(sc->sc_flags & RTWN_FW_LOADED) || 329 sc->sc_ratectl != RTWN_RATECTL_NET80211) 330 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all); 331 #endif 332 333 /* Kick-start more transmit in case we stalled */ 334 rtwn_start(sc); 335 } 336