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