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 break; 163 164 if (m0 == NULL) 165 m0 = m = rtwn_rx_copy_to_mbuf(sc, stat, totlen); 166 else { 167 m->m_next = rtwn_rx_copy_to_mbuf(sc, stat, totlen); 168 if (m->m_next != NULL) 169 m = m->m_next; 170 } 171 172 /* Align next frame. */ 173 totlen = rtwn_usb_align_rx(uc, totlen, len); 174 buf += totlen; 175 len -= totlen; 176 } 177 178 return (m0); 179 } 180 181 static struct mbuf * 182 rtwn_report_intr(struct rtwn_usb_softc *uc, struct usb_xfer *xfer, 183 struct rtwn_data *data) 184 { 185 struct rtwn_softc *sc = &uc->uc_sc; 186 struct ieee80211com *ic = &sc->sc_ic; 187 uint8_t *buf; 188 int len; 189 190 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 191 192 if (__predict_false(len < sizeof(struct r92c_rx_stat))) { 193 counter_u64_add(ic->ic_ierrors, 1); 194 return (NULL); 195 } 196 197 buf = data->buf; 198 switch (rtwn_classify_intr(sc, buf, len)) { 199 case RTWN_RX_DATA: 200 return (rtwn_rxeof(sc, buf, len)); 201 case RTWN_RX_TX_REPORT: 202 if (sc->sc_ratectl != RTWN_RATECTL_NET80211) { 203 /* shouldn't happen */ 204 device_printf(sc->sc_dev, 205 "%s called while ratectl = %d!\n", 206 __func__, sc->sc_ratectl); 207 break; 208 } 209 210 RTWN_NT_LOCK(sc); 211 rtwn_handle_tx_report(sc, buf, len); 212 RTWN_NT_UNLOCK(sc); 213 214 #ifdef IEEE80211_SUPPORT_SUPERG 215 /* 216 * NB: this will executed only when 'report' bit is set. 217 */ 218 if (sc->sc_tx_n_active > 0 && --sc->sc_tx_n_active <= 1) 219 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all); 220 #endif 221 break; 222 case RTWN_RX_OTHER: 223 rtwn_handle_c2h_report(sc, buf, len); 224 break; 225 default: 226 /* NOTREACHED */ 227 KASSERT(0, ("unknown Rx classification code")); 228 break; 229 } 230 231 return (NULL); 232 } 233 234 static struct ieee80211_node * 235 rtwn_rx_frame(struct rtwn_softc *sc, struct mbuf *m, int8_t *rssi) 236 { 237 struct r92c_rx_stat stat; 238 239 /* Imitate PCIe layout. */ 240 m_copydata(m, 0, sizeof(struct r92c_rx_stat), (caddr_t)&stat); 241 m_adj(m, sizeof(struct r92c_rx_stat)); 242 243 return (rtwn_rx_common(sc, m, &stat, rssi)); 244 } 245 246 void 247 rtwn_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 248 { 249 struct rtwn_usb_softc *uc = usbd_xfer_softc(xfer); 250 struct rtwn_softc *sc = &uc->uc_sc; 251 struct ieee80211com *ic = &sc->sc_ic; 252 struct ieee80211_node *ni; 253 struct mbuf *m = NULL, *next; 254 struct rtwn_data *data; 255 int8_t nf, rssi; 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, &rssi); 291 292 RTWN_UNLOCK(sc); 293 294 nf = RTWN_NOISE_FLOOR; 295 if (ni != NULL) { 296 if (ni->ni_flags & IEEE80211_NODE_HT) 297 m->m_flags |= M_AMPDU; 298 (void)ieee80211_input(ni, m, rssi - nf, nf); 299 ieee80211_free_node(ni); 300 } else { 301 (void)ieee80211_input_all(ic, m, 302 rssi - nf, nf); 303 } 304 RTWN_LOCK(sc); 305 m = next; 306 } 307 break; 308 default: 309 /* needs it to the inactive queue due to a error. */ 310 data = STAILQ_FIRST(&uc->uc_rx_active); 311 if (data != NULL) { 312 STAILQ_REMOVE_HEAD(&uc->uc_rx_active, next); 313 STAILQ_INSERT_TAIL(&uc->uc_rx_inactive, data, next); 314 } 315 if (error != USB_ERR_CANCELLED) { 316 usbd_xfer_set_stall(xfer); 317 counter_u64_add(ic->ic_ierrors, 1); 318 goto tr_setup; 319 } 320 break; 321 } 322 finish: 323 /* Finished receive; age anything left on the FF queue by a little bump */ 324 /* 325 * XXX TODO: just make this a callout timer schedule so we can 326 * flush the FF staging queue if we're approaching idle. 327 */ 328 #ifdef IEEE80211_SUPPORT_SUPERG 329 if (!(sc->sc_flags & RTWN_FW_LOADED)) 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