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 int infosz, pktlen, shift, error; 99 100 /* Dump Rx descriptor. */ 101 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC, 102 "%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, tsfl %08X, " 103 "addr: %08X (64: %08X)\n", 104 __func__, le32toh(rx_desc->rxdw0), le32toh(rx_desc->rxdw1), 105 le32toh(rx_desc->rxdw2), le32toh(rx_desc->rxdw3), 106 le32toh(rx_desc->rxdw4), le32toh(rx_desc->tsf_low), 107 le32toh(rx_desc->rxbufaddr), le32toh(rx_desc->rxbufaddr64)); 108 109 rxdw0 = le32toh(rx_desc->rxdw0); 110 if (__predict_false(rxdw0 & (R92C_RXDW0_CRCERR | R92C_RXDW0_ICVERR))) { 111 /* 112 * This should not happen since we setup our Rx filter 113 * to not receive these frames. 114 */ 115 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, 116 "%s: RX flags error (%s)\n", __func__, 117 rxdw0 & R92C_RXDW0_CRCERR ? "CRC" : "ICV"); 118 goto fail; 119 } 120 121 pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN); 122 if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack) || 123 pktlen > MJUMPAGESIZE)) { 124 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, 125 "%s: frame is too short/long: %d\n", __func__, pktlen); 126 goto fail; 127 } 128 129 infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8; 130 shift = MS(rxdw0, R92C_RXDW0_SHIFT); 131 132 m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 133 if (__predict_false(m1 == NULL)) { 134 device_printf(sc->sc_dev, "%s: could not allocate RX mbuf\n", 135 __func__); 136 goto fail; 137 } 138 bus_dmamap_sync(ring->data_dmat, rx_data->map, BUS_DMASYNC_POSTREAD); 139 bus_dmamap_unload(ring->data_dmat, rx_data->map); 140 141 error = bus_dmamap_load(ring->data_dmat, rx_data->map, mtod(m1, void *), 142 MJUMPAGESIZE, rtwn_pci_dma_map_addr, &rx_data->paddr, 0); 143 if (error != 0) { 144 m_freem(m1); 145 146 error = bus_dmamap_load(ring->data_dmat, rx_data->map, 147 mtod(rx_data->m, void *), MJUMPAGESIZE, 148 rtwn_pci_dma_map_addr, &rx_data->paddr, BUS_DMA_NOWAIT); 149 if (error != 0) 150 panic("%s: could not load old RX mbuf", 151 device_get_name(sc->sc_dev)); 152 153 /* Physical address may have changed. */ 154 rtwn_pci_setup_rx_desc(pc, rx_desc, rx_data->paddr, 155 MJUMPAGESIZE, desc_idx); 156 goto fail; 157 } 158 159 /* Finalize mbuf. */ 160 m = rx_data->m; 161 rx_data->m = m1; 162 m->m_pkthdr.len = m->m_len = pktlen + infosz + shift; 163 164 ni = rtwn_rx_common(sc, m, rx_desc); 165 166 RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, 167 "%s: Rx frame len %d, infosz %d, shift %d\n", 168 __func__, pktlen, infosz, shift); 169 170 /* Update RX descriptor. */ 171 rtwn_pci_setup_rx_desc(pc, rx_desc, rx_data->paddr, MJUMPAGESIZE, 172 desc_idx); 173 174 /* Send the frame to the 802.11 layer. */ 175 RTWN_UNLOCK(sc); 176 if (ni != NULL) { 177 (void)ieee80211_input_mimo(ni, m); 178 /* Node is no longer needed. */ 179 ieee80211_free_node(ni); 180 } else 181 (void)ieee80211_input_mimo_all(ic, m); 182 183 RTWN_LOCK(sc); 184 185 return; 186 187 fail: 188 counter_u64_add(ic->ic_ierrors, 1); 189 } 190 191 static void 192 rtwn_pci_tx_done(struct rtwn_softc *sc, int qid) 193 { 194 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc); 195 struct rtwn_tx_ring *ring = &pc->tx_ring[qid]; 196 struct rtwn_tx_desc_common *desc; 197 struct rtwn_tx_data *data; 198 199 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: qid %d, last %d, cur %d\n", 200 __func__, qid, ring->last, ring->cur); 201 202 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_POSTREAD); 203 204 while(ring->last != ring->cur) { 205 data = &ring->tx_data[ring->last]; 206 desc = (struct rtwn_tx_desc_common *) 207 ((uint8_t *)ring->desc + sc->txdesc_len * ring->last); 208 209 KASSERT(data->m != NULL, ("no mbuf")); 210 211 if (desc->flags0 & RTWN_FLAGS0_OWN) 212 break; 213 214 /* Unmap and free mbuf. */ 215 bus_dmamap_sync(ring->data_dmat, data->map, 216 BUS_DMASYNC_POSTWRITE); 217 bus_dmamap_unload(ring->data_dmat, data->map); 218 219 if (data->ni != NULL) { /* not a beacon frame */ 220 ieee80211_tx_complete(data->ni, data->m, 0); 221 222 data->ni = NULL; 223 ring->queued--; 224 KASSERT(ring->queued >= 0, 225 ("ring->queued (qid %d) underflow!\n", qid)); 226 } else 227 m_freem(data->m); 228 229 data->m = NULL; 230 ring->last = (ring->last + 1) % RTWN_PCI_TX_LIST_COUNT; 231 #ifndef D4054 232 if (ring->queued > 0) 233 sc->sc_tx_timer = 5; 234 else 235 sc->sc_tx_timer = 0; 236 #endif 237 } 238 239 if ((sc->qfullmsk & (1 << qid)) != 0 && 240 ring->queued < (RTWN_PCI_TX_LIST_COUNT - 1)) { 241 sc->qfullmsk &= ~(1 << qid); 242 rtwn_start(sc); 243 } 244 245 #ifdef IEEE80211_SUPPORT_SUPERG 246 /* 247 * If the TX active queue drops below a certain 248 * threshold, ensure we age fast-frames out so they're 249 * transmitted. 250 */ 251 if (sc->sc_ratectl != RTWN_RATECTL_NET80211 && ring->queued <= 1) { 252 /* 253 * XXX TODO: just make this a callout timer schedule 254 * so we can flush the FF staging queue if we're 255 * approaching idle. 256 */ 257 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all); 258 } 259 #endif 260 } 261 262 static void 263 rtwn_pci_rx_done(struct rtwn_softc *sc) 264 { 265 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc); 266 struct rtwn_rx_ring *ring = &pc->rx_ring; 267 268 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_POSTREAD); 269 270 for (;;) { 271 struct r92ce_rx_stat *rx_desc = &ring->desc[ring->cur]; 272 273 if (le32toh(rx_desc->rxdw0) & R92C_RXDW0_OWN) 274 break; 275 276 rtwn_pci_rx_frame(sc, rx_desc, ring->cur); 277 278 if (!(sc->sc_flags & RTWN_RUNNING)) 279 return; 280 281 ring->cur = (ring->cur + 1) % RTWN_PCI_RX_LIST_COUNT; 282 } 283 284 /* Finished receive; age anything left on the FF queue by a little bump */ 285 /* 286 * XXX TODO: just make this a callout timer schedule so we can 287 * flush the FF staging queue if we're approaching idle. 288 */ 289 #ifdef IEEE80211_SUPPORT_SUPERG 290 if (!(sc->sc_flags & RTWN_FW_LOADED) || 291 sc->sc_ratectl != RTWN_RATECTL_NET80211) 292 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all); 293 #endif 294 } 295 296 void 297 rtwn_pci_intr(void *arg) 298 { 299 struct rtwn_softc *sc = arg; 300 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc); 301 int i, status, tx_rings; 302 303 RTWN_LOCK(sc); 304 status = rtwn_classify_intr(sc, &tx_rings, 0); 305 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: status %08X, tx_rings %08X\n", 306 __func__, status, tx_rings); 307 if (status == 0 && tx_rings == 0) 308 goto unlock; 309 310 if (status & RTWN_PCI_INTR_RX) { 311 rtwn_pci_rx_done(sc); 312 if (!(sc->sc_flags & RTWN_RUNNING)) 313 goto unlock; 314 } 315 316 if (tx_rings != 0) 317 for (i = 0; i < RTWN_PCI_NTXQUEUES; i++) 318 if (tx_rings & (1 << i)) 319 rtwn_pci_tx_done(sc, i); 320 321 if (sc->sc_flags & RTWN_RUNNING) 322 rtwn_pci_enable_intr(pc); 323 unlock: 324 RTWN_UNLOCK(sc); 325 } 326