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/if_var.h> 45 #include <net/ethernet.h> 46 #include <net/if_media.h> 47 48 #include <net80211/ieee80211_var.h> 49 #include <net80211/ieee80211_radiotap.h> 50 51 #include <dev/rtwn/if_rtwnreg.h> 52 #include <dev/rtwn/if_rtwnvar.h> 53 #include <dev/rtwn/if_rtwn_debug.h> 54 55 #include <dev/rtwn/pci/rtwn_pci_var.h> 56 #include <dev/rtwn/pci/rtwn_pci_tx.h> 57 58 #include <dev/rtwn/rtl8192c/pci/r92ce_reg.h> 59 60 61 static struct mbuf * 62 rtwn_mbuf_defrag(struct mbuf *m0, int how) 63 { 64 struct mbuf *m = NULL; 65 66 KASSERT(m0->m_flags & M_PKTHDR, 67 ("M_PKTHDR flag is absent (m %p)!", m0)); 68 69 /* NB: we need _exactly_ one mbuf (no less, no more). */ 70 if (m0->m_pkthdr.len > MJUMPAGESIZE) { 71 /* XXX MJUM9BYTES? */ 72 return (NULL); 73 } else if (m0->m_pkthdr.len > MCLBYTES) { 74 m = m_getjcl(how, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 75 if (m == NULL) 76 return (NULL); 77 78 if (m_dup_pkthdr(m, m0, how) == 0) { 79 m_freem(m); 80 return (NULL); 81 } 82 83 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t)); 84 m->m_len = m->m_pkthdr.len; 85 m_freem(m0); 86 87 return (m); 88 } else 89 return (m_defrag(m0, how)); 90 } 91 92 static int 93 rtwn_pci_tx_start_frame(struct rtwn_softc *sc, struct ieee80211_node *ni, 94 struct mbuf *m, uint8_t *tx_desc, uint8_t type) 95 { 96 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc); 97 struct rtwn_tx_ring *ring; 98 struct rtwn_tx_data *data; 99 struct rtwn_tx_desc_common *txd; 100 bus_dma_segment_t segs[1]; 101 uint8_t qid; 102 int nsegs, error; 103 104 RTWN_ASSERT_LOCKED(sc); 105 106 switch (type) { 107 case IEEE80211_FC0_TYPE_CTL: 108 case IEEE80211_FC0_TYPE_MGT: 109 qid = RTWN_PCI_MGNT_QUEUE; 110 break; 111 default: 112 qid = M_WME_GETAC(m); 113 break; 114 } 115 116 ring = &pc->tx_ring[qid]; 117 data = &ring->tx_data[ring->cur]; 118 if (data->m != NULL) { 119 RTWN_DPRINTF(sc, RTWN_DEBUG_XMIT, 120 "%s: ring #%u is full (m %p)\n", __func__, qid, data->m); 121 return (ENOBUFS); 122 } 123 124 txd = (struct rtwn_tx_desc_common *) 125 ((uint8_t *)ring->desc + sc->txdesc_len * ring->cur); 126 if (txd->flags0 & RTWN_FLAGS0_OWN) { 127 device_printf(sc->sc_dev, 128 "%s: OWN bit is set (tx desc %d, ring %u)!\n", 129 __func__, ring->cur, qid); 130 return (ENOBUFS); 131 } 132 133 /* Copy Tx descriptor. */ 134 rtwn_pci_copy_tx_desc(pc, txd, tx_desc); 135 txd->pktlen = htole16(m->m_pkthdr.len); 136 txd->offset = sc->txdesc_len; 137 138 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m, segs, 139 &nsegs, BUS_DMA_NOWAIT); 140 if (error != 0 && error != EFBIG) { 141 device_printf(sc->sc_dev, "can't map mbuf (error %d)\n", 142 error); 143 return (error); 144 } 145 if (error != 0) { 146 struct mbuf *mnew; 147 148 mnew = rtwn_mbuf_defrag(m, M_NOWAIT); 149 if (mnew == NULL) { 150 device_printf(sc->sc_dev, "can't defragment mbuf\n"); 151 return (ENOBUFS); 152 } 153 m = mnew; 154 155 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m, 156 segs, &nsegs, BUS_DMA_NOWAIT); 157 if (error != 0) { 158 device_printf(sc->sc_dev, 159 "can't map mbuf (error %d)\n", error); 160 if (ni != NULL) { 161 if_inc_counter(ni->ni_vap->iv_ifp, 162 IFCOUNTER_OERRORS, 1); 163 ieee80211_free_node(ni); 164 } 165 m_freem(m); 166 return (0); /* XXX */ 167 } 168 } 169 170 rtwn_pci_tx_postsetup(pc, txd, segs); 171 txd->flags0 |= RTWN_FLAGS0_OWN; 172 173 /* Dump Tx descriptor. */ 174 rtwn_dump_tx_desc(sc, txd); 175 176 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 177 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 178 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 179 180 data->m = m; 181 data->ni = ni; 182 183 ring->cur = (ring->cur + 1) % RTWN_PCI_TX_LIST_COUNT; 184 185 ring->queued++; 186 if (ring->queued >= (RTWN_PCI_TX_LIST_COUNT - 1)) 187 sc->qfullmsk |= (1 << qid); 188 189 #ifndef D4054 190 sc->sc_tx_timer = 5; 191 #endif 192 193 /* Kick TX. */ 194 rtwn_write_2(sc, R92C_PCIE_CTRL_REG, (1 << qid)); 195 196 return (0); 197 } 198 199 static int 200 rtwn_pci_tx_start_beacon(struct rtwn_softc *sc, struct mbuf *m, 201 uint8_t *tx_desc, int id) 202 { 203 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc); 204 struct rtwn_tx_ring *ring; 205 struct rtwn_tx_data *data; 206 struct rtwn_tx_desc_common *txd; 207 bus_dma_segment_t segs[1]; 208 int nsegs, error, own; 209 210 RTWN_ASSERT_LOCKED(sc); 211 212 KASSERT(id == 0 || id == 1, ("bogus vap id %d\n", id)); 213 214 ring = &pc->tx_ring[RTWN_PCI_BEACON_QUEUE]; 215 data = &ring->tx_data[id]; 216 txd = (struct rtwn_tx_desc_common *) 217 ((uint8_t *)ring->desc + id * sc->txdesc_len); 218 219 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 220 BUS_DMASYNC_POSTREAD); 221 own = !!(txd->flags0 & RTWN_FLAGS0_OWN); 222 error = 0; 223 if (!own || txd->pktlen != htole16(m->m_pkthdr.len)) { 224 if (!own) { 225 /* Copy Tx descriptor. */ 226 rtwn_pci_copy_tx_desc(pc, txd, tx_desc); 227 txd->offset = sc->txdesc_len; 228 } else { 229 /* Reload mbuf. */ 230 bus_dmamap_unload(ring->data_dmat, data->map); 231 } 232 233 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, 234 data->map, m, segs, &nsegs, BUS_DMA_NOWAIT); 235 if (error != 0) { 236 device_printf(sc->sc_dev, 237 "can't map beacon (error %d)\n", error); 238 txd->flags0 &= ~RTWN_FLAGS0_OWN; 239 goto end; 240 } 241 242 txd->pktlen = htole16(m->m_pkthdr.len); 243 rtwn_pci_tx_postsetup(pc, txd, segs); 244 txd->flags0 |= RTWN_FLAGS0_OWN; 245 end: 246 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 247 BUS_DMASYNC_PREWRITE); 248 } 249 250 /* Dump Tx descriptor. */ 251 rtwn_dump_tx_desc(sc, txd); 252 253 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 254 255 return (0); 256 } 257 258 int 259 rtwn_pci_tx_start(struct rtwn_softc *sc, struct ieee80211_node *ni, 260 struct mbuf *m, uint8_t *tx_desc, uint8_t type, int id) 261 { 262 int error = 0; 263 264 RTWN_ASSERT_LOCKED(sc); 265 266 if (ni == NULL) /* beacon frame */ 267 error = rtwn_pci_tx_start_beacon(sc, m, tx_desc, id); 268 else 269 error = rtwn_pci_tx_start_frame(sc, ni, m, tx_desc, type); 270 271 return (error); 272 } 273