summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/rtwn/pci/rtwn_pci_tx.c
blob: 943be3727084ac0f0156da4969d68f493f8539d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <machine/rtems-bsd-kernel-space.h>

/*	$OpenBSD: if_rtwn.c,v 1.6 2015/08/28 00:03:53 deraadt Exp $	*/

/*-
 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
 * Copyright (c) 2015 Stefan Sperling <stsp@openbsd.org>
 * Copyright (c) 2016 Andriy Voskoboinyk <avos@FreeBSD.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <rtems/bsd/local/opt_wlan.h>

#include <sys/param.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/taskqueue.h>
#include <sys/bus.h>
#include <sys/endian.h>

#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>

#include <net/if.h>
#include <net/if_var.h>
#include <net/ethernet.h>
#include <net/if_media.h>

#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_radiotap.h>

#include <dev/rtwn/if_rtwnreg.h>
#include <dev/rtwn/if_rtwnvar.h>
#include <dev/rtwn/if_rtwn_debug.h>

#include <dev/rtwn/pci/rtwn_pci_var.h>
#include <dev/rtwn/pci/rtwn_pci_tx.h>

#include <dev/rtwn/rtl8192c/pci/r92ce_reg.h>


static struct mbuf *
rtwn_mbuf_defrag(struct mbuf *m0, int how)
{
	struct mbuf *m = NULL;

	KASSERT(m0->m_flags & M_PKTHDR,
	    ("M_PKTHDR flag is absent (m %p)!", m0));

	/* NB: we need _exactly_ one mbuf (no less, no more). */
	if (m0->m_pkthdr.len > MJUMPAGESIZE) {
		/* XXX MJUM9BYTES? */
		return (NULL);
	} else if (m0->m_pkthdr.len > MCLBYTES) {
		m = m_getjcl(how, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
		if (m == NULL)
			return (NULL);

		if (m_dup_pkthdr(m, m0, how) == 0) {
			m_freem(m);
			return (NULL);
		}

		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
		m->m_len = m->m_pkthdr.len;
		m_freem(m0);

		return (m);
	} else
		return (m_defrag(m0, how));
}

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)
{
	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
	struct rtwn_tx_ring *ring;
	struct rtwn_tx_data *data;
	struct rtwn_tx_desc_common *txd;
	bus_dma_segment_t segs[1];
	uint8_t qid;
	int nsegs, error;

	RTWN_ASSERT_LOCKED(sc);

	switch (type) {
	case IEEE80211_FC0_TYPE_CTL:
	case IEEE80211_FC0_TYPE_MGT:
		qid = RTWN_PCI_MGNT_QUEUE;
		break;
	default:
		qid = M_WME_GETAC(m);
		break;
	}

	ring = &pc->tx_ring[qid];
	data = &ring->tx_data[ring->cur];
	if (data->m != NULL) {
		RTWN_DPRINTF(sc, RTWN_DEBUG_XMIT,
		    "%s: ring #%u is full (m %p)\n", __func__, qid, data->m);
		return (ENOBUFS);
	}

	txd = (struct rtwn_tx_desc_common *)
	    ((uint8_t *)ring->desc + sc->txdesc_len * ring->cur);
	if (txd->flags0 & RTWN_FLAGS0_OWN) {
		device_printf(sc->sc_dev,
		    "%s: OWN bit is set (tx desc %d, ring %u)!\n",
		    __func__, ring->cur, qid);
		return (ENOBUFS);
	}

	/* Copy Tx descriptor. */
	rtwn_pci_copy_tx_desc(pc, txd, tx_desc);
	txd->pktlen = htole16(m->m_pkthdr.len);
	txd->offset = sc->txdesc_len;

	error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m, segs,
	    &nsegs, BUS_DMA_NOWAIT);
	if (error != 0 && error != EFBIG) {
		device_printf(sc->sc_dev, "can't map mbuf (error %d)\n",
		    error);
		return (error);
	}
	if (error != 0) {
		struct mbuf *mnew;

		mnew = rtwn_mbuf_defrag(m, M_NOWAIT);
		if (mnew == NULL) {
			device_printf(sc->sc_dev, "can't defragment mbuf\n");
			return (ENOBUFS);
		}
		m = mnew;

		error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m,
		    segs, &nsegs, BUS_DMA_NOWAIT);
		if (error != 0) {
			device_printf(sc->sc_dev,
			    "can't map mbuf (error %d)\n", error);
			if (ni != NULL) {
				if_inc_counter(ni->ni_vap->iv_ifp,
				    IFCOUNTER_OERRORS, 1);
				ieee80211_free_node(ni);
			}
			m_freem(m);
			return (0);	/* XXX */
		}
	}

	rtwn_pci_tx_postsetup(pc, txd, segs);
	txd->flags0 |= RTWN_FLAGS0_OWN;

	/* Dump Tx descriptor. */
	rtwn_dump_tx_desc(sc, txd);

	bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
	    BUS_DMASYNC_POSTWRITE);
	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE);

	data->m = m;
	data->ni = ni;

	ring->cur = (ring->cur + 1) % RTWN_PCI_TX_LIST_COUNT;

	ring->queued++;
	if (ring->queued >= (RTWN_PCI_TX_LIST_COUNT - 1))
		sc->qfullmsk |= (1 << qid);

#ifndef D4054
	sc->sc_tx_timer = 5;
#endif

	/* Kick TX. */
	rtwn_write_2(sc, R92C_PCIE_CTRL_REG, (1 << qid));

	return (0);
}

static int
rtwn_pci_tx_start_beacon(struct rtwn_softc *sc, struct mbuf *m,
    uint8_t *tx_desc, int id)
{
	struct rtwn_pci_softc *pc = RTWN_PCI_SOFTC(sc);
	struct rtwn_tx_ring *ring;
	struct rtwn_tx_data *data;
	struct rtwn_tx_desc_common *txd;
	bus_dma_segment_t segs[1];
	int nsegs, error, own;

	RTWN_ASSERT_LOCKED(sc);

	KASSERT(id == 0 || id == 1, ("bogus vap id %d\n", id));

	ring = &pc->tx_ring[RTWN_PCI_BEACON_QUEUE];
	data = &ring->tx_data[id];
	txd = (struct rtwn_tx_desc_common *)
	    ((uint8_t *)ring->desc + id * sc->txdesc_len);

	bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
	    BUS_DMASYNC_POSTREAD);
	own = !!(txd->flags0 & RTWN_FLAGS0_OWN);
	error = 0;
	if (!own || txd->pktlen != htole16(m->m_pkthdr.len)) {
		if (!own) {
			/* Copy Tx descriptor. */
			rtwn_pci_copy_tx_desc(pc, txd, tx_desc);
			txd->offset = sc->txdesc_len;
		} else {
			/* Reload mbuf. */
			bus_dmamap_unload(ring->data_dmat, data->map);
		}

		error = bus_dmamap_load_mbuf_sg(ring->data_dmat,
		    data->map, m, segs, &nsegs, BUS_DMA_NOWAIT);
		if (error != 0) {
			device_printf(sc->sc_dev,
			    "can't map beacon (error %d)\n", error);
			txd->flags0 &= ~RTWN_FLAGS0_OWN;
			goto end;
		}

		txd->pktlen = htole16(m->m_pkthdr.len);
		rtwn_pci_tx_postsetup(pc, txd, segs);
		txd->flags0 |= RTWN_FLAGS0_OWN;
end:
		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
		    BUS_DMASYNC_PREWRITE);
	}

	/* Dump Tx descriptor. */
	rtwn_dump_tx_desc(sc, txd);

	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);

	return (0);
}

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)
{
	int error = 0;

	RTWN_ASSERT_LOCKED(sc);

	if (ni == NULL)		/* beacon frame */
		error = rtwn_pci_tx_start_beacon(sc, m, tx_desc, id);
	else
		error = rtwn_pci_tx_start_frame(sc, ni, m, tx_desc, type);

	return (error);
}