1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
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.
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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
26 #include <sys/param.h>
28 #include <sys/mutex.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>
37 #include <sys/endian.h>
40 #include <net/if_var.h>
41 #include <net/ethernet.h>
42 #include <net/if_media.h>
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_radiotap.h>
46 #include <net80211/ieee80211_ratectl.h>
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
51 #include <dev/rtwn/if_rtwnreg.h>
52 #include <dev/rtwn/if_rtwnvar.h>
54 #include <dev/rtwn/if_rtwn_beacon.h>
55 #include <dev/rtwn/if_rtwn_debug.h>
56 #include <dev/rtwn/if_rtwn_ridx.h>
57 #include <dev/rtwn/if_rtwn_task.h>
58 #include <dev/rtwn/if_rtwn_tx.h>
60 #include <dev/rtwn/usb/rtwn_usb_var.h>
62 #include <dev/rtwn/usb/rtwn_usb_reg.h>
63 #include <dev/rtwn/usb/rtwn_usb_tx.h>
65 static struct rtwn_data * _rtwn_usb_getbuf(struct rtwn_usb_softc *);
66 static struct rtwn_data * rtwn_usb_getbuf(struct rtwn_usb_softc *);
67 static void rtwn_usb_txeof(struct rtwn_usb_softc *,
68 struct rtwn_data *, int);
71 static const uint8_t wme2qid[] =
72 { RTWN_BULK_TX_BE, RTWN_BULK_TX_BK,
73 RTWN_BULK_TX_VI, RTWN_BULK_TX_VO };
76 static struct rtwn_data *
77 _rtwn_usb_getbuf(struct rtwn_usb_softc *uc)
79 struct rtwn_softc *sc = &uc->uc_sc;
82 bf = STAILQ_FIRST(&uc->uc_tx_inactive);
84 STAILQ_REMOVE_HEAD(&uc->uc_tx_inactive, next);
86 RTWN_DPRINTF(sc, RTWN_DEBUG_XMIT,
87 "%s: out of xmit buffers\n", __func__);
92 static struct rtwn_data *
93 rtwn_usb_getbuf(struct rtwn_usb_softc *uc)
95 struct rtwn_softc *sc = &uc->uc_sc;
98 RTWN_ASSERT_LOCKED(sc);
100 bf = _rtwn_usb_getbuf(uc);
102 RTWN_DPRINTF(sc, RTWN_DEBUG_XMIT, "%s: stop queue\n",
109 rtwn_usb_txeof(struct rtwn_usb_softc *uc, struct rtwn_data *data, int status)
111 struct rtwn_softc *sc = &uc->uc_sc;
113 RTWN_ASSERT_LOCKED(sc);
115 if (data->ni != NULL) /* not a beacon frame */
116 ieee80211_tx_complete(data->ni, data->m, status);
118 if (sc->sc_ratectl != RTWN_RATECTL_NET80211)
119 if (sc->sc_tx_n_active > 0)
120 sc->sc_tx_n_active--;
125 STAILQ_INSERT_TAIL(&uc->uc_tx_inactive, data, next);
128 if (STAILQ_EMPTY(&uc->uc_tx_active) && STAILQ_EMPTY(&uc->uc_tx_pending))
136 rtwn_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error)
138 struct rtwn_usb_softc *uc = usbd_xfer_softc(xfer);
139 struct rtwn_softc *sc = &uc->uc_sc;
140 struct rtwn_data *data;
142 RTWN_ASSERT_LOCKED(sc);
144 switch (USB_GET_STATE(xfer)){
145 case USB_ST_TRANSFERRED:
146 data = STAILQ_FIRST(&uc->uc_tx_active);
149 STAILQ_REMOVE_HEAD(&uc->uc_tx_active, next);
150 rtwn_usb_txeof(uc, data, 0);
154 data = STAILQ_FIRST(&uc->uc_tx_pending);
156 RTWN_DPRINTF(sc, RTWN_DEBUG_XMIT,
157 "%s: empty pending queue\n", __func__);
158 sc->sc_tx_n_active = 0;
161 STAILQ_REMOVE_HEAD(&uc->uc_tx_pending, next);
162 STAILQ_INSERT_TAIL(&uc->uc_tx_active, data, next);
165 * Note: if this is a beacon frame, ensure that it will go
166 * into appropriate queue.
168 if (data->ni == NULL && RTWN_CHIP_HAS_BCNQ1(sc))
169 rtwn_switch_bcnq(sc, data->id);
170 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
171 usbd_transfer_submit(xfer);
172 if (sc->sc_ratectl != RTWN_RATECTL_NET80211)
173 sc->sc_tx_n_active++;
176 data = STAILQ_FIRST(&uc->uc_tx_active);
179 STAILQ_REMOVE_HEAD(&uc->uc_tx_active, next);
180 rtwn_usb_txeof(uc, data, 1);
181 if (error != USB_ERR_CANCELLED) {
182 usbd_xfer_set_stall(xfer);
188 #ifdef IEEE80211_SUPPORT_SUPERG
190 * If the TX active queue drops below a certain
191 * threshold, ensure we age fast-frames out so they're
194 if (sc->sc_ratectl != RTWN_RATECTL_NET80211 &&
195 sc->sc_tx_n_active <= 1) {
196 /* XXX ew - net80211 should defer this for us! */
199 * Note: this sc_tx_n_active currently tracks
200 * the number of pending transmit submissions
201 * and not the actual depth of the TX frames
202 * pending to the hardware. That means that
203 * we're going to end up with some sub-optimal
204 * aggregation behaviour.
207 * XXX TODO: just make this a callout timer schedule so we can
208 * flush the FF staging queue if we're approaching idle.
210 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
213 /* Kick-start more transmit */
218 rtwn_usb_tx_checksum(struct rtwn_tx_desc_common *txd)
220 txd->txdw7.usb_checksum = 0;
221 txd->txdw7.usb_checksum = rtwn_usb_calc_tx_checksum(txd);
225 rtwn_usb_tx_start(struct rtwn_softc *sc, struct ieee80211_node *ni,
226 struct mbuf *m, uint8_t *tx_desc, uint8_t type, int id)
228 struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc);
229 struct rtwn_tx_desc_common *txd;
230 struct rtwn_data *data;
231 struct usb_xfer *xfer;
234 RTWN_ASSERT_LOCKED(sc);
236 if (m->m_pkthdr.len + sc->txdesc_len > RTWN_USB_TXBUFSZ)
239 data = rtwn_usb_getbuf(uc);
246 case IEEE80211_FC0_TYPE_CTL:
247 case IEEE80211_FC0_TYPE_MGT:
248 xfer = uc->uc_xfer[RTWN_BULK_TX_VO];
251 xfer = uc->uc_xfer[wme2qid[ac]];
255 txd = (struct rtwn_tx_desc_common *)tx_desc;
256 txd->pktlen = htole16(m->m_pkthdr.len);
257 txd->offset = sc->txdesc_len;
258 txd->flags0 |= RTWN_FLAGS0_OWN;
259 rtwn_usb_tx_checksum(txd);
261 /* Dump Tx descriptor. */
262 rtwn_dump_tx_desc(sc, tx_desc);
264 memcpy(data->buf, tx_desc, sc->txdesc_len);
265 m_copydata(m, 0, m->m_pkthdr.len,
266 (caddr_t)(data->buf + sc->txdesc_len));
268 data->buflen = m->m_pkthdr.len + sc->txdesc_len;
271 if (data->ni != NULL) {
278 STAILQ_INSERT_TAIL(&uc->uc_tx_pending, data, next);
279 if (STAILQ_EMPTY(&uc->uc_tx_inactive))
282 usbd_transfer_start(xfer);