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 int 62 rtwn_pci_tx_start_common(struct rtwn_softc *sc, struct ieee80211_node *ni, 63 struct mbuf *m, uint8_t *tx_desc, uint8_t type, int id) 64 { 65 struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc); 66 struct rtwn_tx_ring *ring; 67 struct rtwn_tx_data *data; 68 struct rtwn_tx_desc_common *txd; 69 bus_dma_segment_t segs[1]; 70 uint8_t qid; 71 int nsegs, error; 72 73 RTWN_ASSERT_LOCKED(sc); 74 75 switch (type) { 76 case IEEE80211_FC0_TYPE_CTL: 77 case IEEE80211_FC0_TYPE_MGT: 78 qid = RTWN_PCI_VO_QUEUE; 79 break; 80 default: 81 qid = M_WME_GETAC(m); 82 break; 83 } 84 85 if (ni == NULL) /* beacon frame */ 86 qid = RTWN_PCI_BEACON_QUEUE; 87 88 ring = &pc->tx_ring[qid]; 89 data = &ring->tx_data[ring->cur]; 90 if (data->m != NULL) { 91 RTWN_DPRINTF(sc, RTWN_DEBUG_XMIT, 92 "%s: ring #%u is full (m %p)\n", __func__, qid, data->m); 93 return (ENOBUFS); 94 } 95 96 txd = (struct rtwn_tx_desc_common *) 97 ((uint8_t *)ring->desc + sc->txdesc_len * ring->cur); 98 if (txd->flags0 & RTWN_FLAGS0_OWN) { 99 device_printf(sc->sc_dev, 100 "%s: OWN bit is set (tx desc %d, ring %u)!\n", 101 __func__, ring->cur, qid); 102 return (ENOBUFS); 103 } 104 105 /* Copy Tx descriptor. */ 106 rtwn_pci_copy_tx_desc(pc, txd, tx_desc); 107 txd->pktlen = htole16(m->m_pkthdr.len); 108 txd->offset = sc->txdesc_len; 109 110 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m, segs, 111 &nsegs, BUS_DMA_NOWAIT); 112 if (error != 0 && error != EFBIG) { 113 device_printf(sc->sc_dev, "can't map mbuf (error %d)\n", 114 error); 115 return (error); 116 } 117 if (error != 0) { 118 struct mbuf *mnew; 119 120 mnew = m_defrag(m, M_NOWAIT); 121 if (mnew == NULL) { 122 device_printf(sc->sc_dev, "can't defragment mbuf\n"); 123 return (ENOBUFS); 124 } 125 m = mnew; 126 127 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m, 128 segs, &nsegs, BUS_DMA_NOWAIT); 129 if (error != 0) { 130 device_printf(sc->sc_dev, 131 "can't map mbuf (error %d)\n", error); 132 if (ni != NULL) { 133 if_inc_counter(ni->ni_vap->iv_ifp, 134 IFCOUNTER_OERRORS, 1); 135 ieee80211_free_node(ni); 136 } 137 m_freem(m); 138 return (0); /* XXX */ 139 } 140 } 141 142 rtwn_pci_tx_postsetup(pc, txd, segs); 143 txd->flags0 |= RTWN_FLAGS0_OWN; 144 145 /* Dump Tx descriptor. */ 146 rtwn_dump_tx_desc(sc, txd); 147 148 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 149 BUS_DMASYNC_POSTWRITE); 150 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE); 151 152 data->m = m; 153 data->ni = ni; 154 data->id = id; 155 156 ring->cur = (ring->cur + 1) % RTWN_PCI_TX_LIST_COUNT; 157 158 if (qid != RTWN_PCI_BEACON_QUEUE) { 159 ring->queued++; 160 if (ring->queued >= (RTWN_PCI_TX_LIST_COUNT - 1)) 161 sc->qfullmsk |= (1 << qid); 162 163 #ifndef D4054 164 sc->sc_tx_timer = 5; 165 #endif 166 } 167 168 /* Kick TX. */ 169 rtwn_write_2(sc, R92C_PCIE_CTRL_REG, (1 << qid)); 170 171 return (0); 172 } 173 174 int 175 rtwn_pci_tx_start(struct rtwn_softc *sc, struct ieee80211_node *ni, 176 struct mbuf *m, uint8_t *tx_desc, uint8_t type, int id) 177 { 178 int error = 0; 179 180 if (ni == NULL) { /* beacon frame */ 181 m = m_dup(m, M_NOWAIT); 182 if (__predict_false(m == NULL)) { 183 device_printf(sc->sc_dev, 184 "%s: could not copy beacon frame\n", __func__); 185 return (ENOMEM); 186 } 187 188 error = rtwn_pci_tx_start_common(sc, ni, m, tx_desc, type, id); 189 if (error != 0) 190 m_freem(m); 191 } else 192 error = rtwn_pci_tx_start_common(sc, ni, m, tx_desc, type, id); 193 194 return (error); 195 } 196