1 /* $OpenBSD: if_rtwn.c,v 1.6 2015/08/28 00:03:53 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2015 Stefan Sperling <stsp@openbsd.org> 6 * Copyright (c) 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 <machine/bus.h> 40 #include <machine/resource.h> 41 #include <sys/rman.h> 42 43 #include <net/if.h> 44 #include <net/ethernet.h> 45 #include <net/if_media.h> 46 47 #include <net80211/ieee80211_var.h> 48 49 #include <dev/rtwn/if_rtwnreg.h> 50 #include <dev/rtwn/if_rtwnvar.h> 51 #include <dev/rtwn/if_rtwn_debug.h> 52 #include <dev/rtwn/if_rtwn_rx.h> 53 #include <dev/rtwn/if_rtwn_task.h> 54 #include <dev/rtwn/if_rtwn_tx.h> 55 56 #include <dev/rtwn/pci/rtwn_pci_var.h> 57 #include <dev/rtwn/pci/rtwn_pci_rx.h> 58 59 #include <dev/rtwn/rtl8192c/pci/r92ce_rx_desc.h> 60 61 62 void 63 rtwn_pci_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, 64 int error) 65 { 66 67 if (error != 0) 68 return; 69 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs)); 70 *(bus_addr_t *)arg = segs[0].ds_addr; 71 } 72 73 void 74 rtwn_pci_setup_rx_desc(struct rtwn_pci_softc *pc, struct r92ce_rx_stat *desc, 75 bus_addr_t addr, size_t len, int idx) 76 { 77 78 memset(desc, 0, sizeof(*desc)); 79 desc->rxdw0 = htole32(SM(R92C_RXDW0_PKTLEN, len) | 80 ((idx == RTWN_PCI_RX_LIST_COUNT - 1) ? R92C_RXDW0_EOR : 0)); 81 desc->rxbufaddr = htole32(addr); 82 bus_space_barrier(pc->pc_st, pc->pc_sh, 0, pc->pc_mapsize, 83 BUS_SPACE_BARRIER_WRITE); 84 desc->rxdw0 |= htole32(R92C_RXDW0_OWN); 85 } 86 87 static void 88 rtwn_pci_rx_frame(struct rtwn_softc *sc, struct r92ce_rx_stat *rx_desc, 89 int desc_idx) 90 { 91 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc); 92 struct rtwn_rx_ring *ring = &pc->rx_ring; 93 struct rtwn_rx_data *rx_data = &ring->rx_data[desc_idx]; 94 struct ieee80211com *ic = &sc->sc_ic; 95 struct ieee80211_node *ni; 96 uint32_t rxdw0; 97 struct mbuf *m, *m1; 98 int8_t rssi = 0, nf; 99 int infosz, pktlen, shift, error; 100 101 /* Dump Rx descriptor. */ 102 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC, 103 "%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, tsfl %08X, " 104 "addr: %08X (64: %08X)\n", 105 __func__, le32toh(rx_desc->rxdw0), le32toh(rx_desc->rxdw1), 106 le32toh(rx_desc->rxdw2), le32toh(rx_desc->rxdw3), 107 le32toh(rx_desc->rxdw4), le32toh(rx_desc->tsf_low), 108 le32toh(rx_desc->rxbufaddr), le32toh(rx_desc->rxbufaddr64)); 109 110 rxdw0 = le32toh(rx_desc->rxdw0); 111 if (__predict_false(rxdw0 & (R92C_RXDW0_CRCERR | R92C_RXDW0_ICVERR))) { 112 /* 113 * This should not happen since we setup our Rx filter 114 * to not receive these frames. 115 */ 116 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, 117 "%s: RX flags error (%s)\n", __func__, 118 rxdw0 & R92C_RXDW0_CRCERR ? "CRC" : "ICV"); 119 goto fail; 120 } 121 122 pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN); 123 if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack) || 124 pktlen > MJUMPAGESIZE)) { 125 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, 126 "%s: frame is too short/long: %d\n", __func__, pktlen); 127 goto fail; 128 } 129 130 infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8; 131 shift = MS(rxdw0, R92C_RXDW0_SHIFT); 132 133 m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 134 if (__predict_false(m1 == NULL)) { 135 device_printf(sc->sc_dev, "%s: could not allocate RX mbuf\n", 136 __func__); 137 goto fail; 138 } 139 bus_dmamap_sync(ring->data_dmat, rx_data->map, BUS_DMASYNC_POSTREAD); 140 bus_dmamap_unload(ring->data_dmat, rx_data->map); 141 142 error = bus_dmamap_load(ring->data_dmat, rx_data->map, mtod(m1, void *), 143 MJUMPAGESIZE, rtwn_pci_dma_map_addr, &rx_data->paddr, 0); 144 if (error != 0) { 145 m_freem(m1); 146 147 error = bus_dmamap_load(ring->data_dmat, rx_data->map, 148 mtod(rx_data->m, void *), MJUMPAGESIZE, 149 rtwn_pci_dma_map_addr, &rx_data->paddr, BUS_DMA_NOWAIT); 150 if (error != 0) 151 panic("%s: could not load old RX mbuf", 152 device_get_name(sc->sc_dev)); 153 154 /* Physical address may have changed. */ 155 rtwn_pci_setup_rx_desc(pc, rx_desc, rx_data->paddr, 156 MJUMPAGESIZE, desc_idx); 157 goto fail; 158 } 159 160 /* Finalize mbuf. */ 161 m = rx_data->m; 162 rx_data->m = m1; 163 m->m_pkthdr.len = m->m_len = pktlen + infosz + shift; 164 165 nf = RTWN_NOISE_FLOOR; 166 ni = rtwn_rx_common(sc, m, rx_desc, &rssi); 167 168 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, 169 "%s: Rx frame len %d, infosz %d, shift %d, rssi %d\n", 170 __func__, pktlen, infosz, shift, rssi); 171 172 /* Update RX descriptor. */ 173 rtwn_pci_setup_rx_desc(pc, rx_desc, rx_data->paddr, MJUMPAGESIZE, 174 desc_idx); 175 176 /* Send the frame to the 802.11 layer. */ 177 RTWN_UNLOCK(sc); 178 if (ni != NULL) { 179 (void)ieee80211_input(ni, m, rssi - nf, nf); 180 /* Node is no longer needed. */ 181 ieee80211_free_node(ni); 182 } else 183 (void)ieee80211_input_all(ic, m, rssi - nf, nf); 184 185 RTWN_LOCK(sc); 186 187 return; 188 189 fail: 190 counter_u64_add(ic->ic_ierrors, 1); 191 } 192 193 static void 194 rtwn_pci_tx_done(struct rtwn_softc *sc, int qid) 195 { 196 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc); 197 struct rtwn_tx_ring *ring = &pc->tx_ring[qid]; 198 struct rtwn_tx_desc_common *desc; 199 struct rtwn_tx_data *data; 200 201 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: qid %d, last %d, cur %d\n", 202 __func__, qid, ring->last, ring->cur); 203 204 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_POSTREAD); 205 206 while(ring->last != ring->cur) { 207 data = &ring->tx_data[ring->last]; 208 desc = (struct rtwn_tx_desc_common *) 209 ((uint8_t *)ring->desc + sc->txdesc_len * ring->last); 210 211 KASSERT(data->m != NULL, ("no mbuf")); 212 213 if (desc->flags0 & RTWN_FLAGS0_OWN) 214 break; 215 216 /* Unmap and free mbuf. */ 217 bus_dmamap_sync(ring->data_dmat, data->map, 218 BUS_DMASYNC_POSTWRITE); 219 bus_dmamap_unload(ring->data_dmat, data->map); 220 221 if (data->ni != NULL) { /* not a beacon frame */ 222 ieee80211_tx_complete(data->ni, data->m, 0); 223 224 data->ni = NULL; 225 ring->queued--; 226 KASSERT(ring->queued >= 0, 227 ("ring->queued (qid %d) underflow!\n", qid)); 228 } else 229 m_freem(data->m); 230 231 data->m = NULL; 232 ring->last = (ring->last + 1) % RTWN_PCI_TX_LIST_COUNT; 233 #ifndef D4054 234 if (ring->queued > 0) 235 sc->sc_tx_timer = 5; 236 else 237 sc->sc_tx_timer = 0; 238 #endif 239 } 240 241 if ((sc->qfullmsk & (1 << qid)) != 0 && 242 ring->queued < (RTWN_PCI_TX_LIST_COUNT - 1)) { 243 sc->qfullmsk &= ~(1 << qid); 244 rtwn_start(sc); 245 } 246 247 #ifdef IEEE80211_SUPPORT_SUPERG 248 /* 249 * If the TX active queue drops below a certain 250 * threshold, ensure we age fast-frames out so they're 251 * transmitted. 252 */ 253 if (sc->sc_ratectl != RTWN_RATECTL_NET80211 && ring->queued <= 1) { 254 /* 255 * XXX TODO: just make this a callout timer schedule 256 * so we can flush the FF staging queue if we're 257 * approaching idle. 258 */ 259 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all); 260 } 261 #endif 262 } 263 264 static void 265 rtwn_pci_rx_done(struct rtwn_softc *sc) 266 { 267 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc); 268 struct rtwn_rx_ring *ring = &pc->rx_ring; 269 270 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_POSTREAD); 271 272 for (;;) { 273 struct r92ce_rx_stat *rx_desc = &ring->desc[ring->cur]; 274 275 if (le32toh(rx_desc->rxdw0) & R92C_RXDW0_OWN) 276 break; 277 278 rtwn_pci_rx_frame(sc, rx_desc, ring->cur); 279 280 if (!(sc->sc_flags & RTWN_RUNNING)) 281 return; 282 283 ring->cur = (ring->cur + 1) % RTWN_PCI_RX_LIST_COUNT; 284 } 285 286 /* Finished receive; age anything left on the FF queue by a little bump */ 287 /* 288 * XXX TODO: just make this a callout timer schedule so we can 289 * flush the FF staging queue if we're approaching idle. 290 */ 291 #ifdef IEEE80211_SUPPORT_SUPERG 292 if (!(sc->sc_flags & RTWN_FW_LOADED) || 293 sc->sc_ratectl != RTWN_RATECTL_NET80211) 294 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all); 295 #endif 296 } 297 298 void 299 rtwn_pci_intr(void *arg) 300 { 301 struct rtwn_softc *sc = arg; 302 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc); 303 int i, status, tx_rings; 304 305 RTWN_LOCK(sc); 306 status = rtwn_classify_intr(sc, &tx_rings, 0); 307 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: status %08X, tx_rings %08X\n", 308 __func__, status, tx_rings); 309 if (status == 0 && tx_rings == 0) 310 goto unlock; 311 312 if (status & RTWN_PCI_INTR_RX) { 313 rtwn_pci_rx_done(sc); 314 if (!(sc->sc_flags & RTWN_RUNNING)) 315 goto unlock; 316 } 317 318 if (tx_rings != 0) 319 for (i = 0; i < RTWN_PCI_NTXQUEUES; i++) 320 if (tx_rings & (1 << i)) 321 rtwn_pci_tx_done(sc, i); 322 323 if (sc->sc_flags & RTWN_RUNNING) 324 rtwn_pci_enable_intr(pc); 325 unlock: 326 RTWN_UNLOCK(sc); 327 } 328