Changeset View
Changeset View
Standalone View
Standalone View
head/sys/dev/rtwn/usb/rtwn_usb_rx.c
Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | |||||
#include <dev/rtwn/if_rtwn_ridx.h> | #include <dev/rtwn/if_rtwn_ridx.h> | ||||
#include <dev/rtwn/if_rtwn_rx.h> | #include <dev/rtwn/if_rtwn_rx.h> | ||||
#include <dev/rtwn/if_rtwn_task.h> | #include <dev/rtwn/if_rtwn_task.h> | ||||
#include <dev/rtwn/if_rtwn_tx.h> | #include <dev/rtwn/if_rtwn_tx.h> | ||||
#include <dev/rtwn/usb/rtwn_usb_var.h> | #include <dev/rtwn/usb/rtwn_usb_var.h> | ||||
#include <dev/rtwn/usb/rtwn_usb_rx.h> | #include <dev/rtwn/usb/rtwn_usb_rx.h> | ||||
static struct mbuf * rtwn_rxeof(struct rtwn_softc *, struct rtwn_data *, | |||||
uint8_t *, int); | |||||
static struct mbuf * | static int | ||||
rtwn_rx_copy_to_mbuf(struct rtwn_softc *sc, struct rtwn_rx_stat_common *stat, | rtwn_rx_check_pre_alloc(struct rtwn_softc *sc, | ||||
int totlen) | struct rtwn_rx_stat_common *stat) | ||||
{ | { | ||||
struct ieee80211com *ic = &sc->sc_ic; | |||||
struct mbuf *m; | |||||
uint32_t rxdw0; | uint32_t rxdw0; | ||||
int pktlen; | int pktlen; | ||||
RTWN_ASSERT_LOCKED(sc); | RTWN_ASSERT_LOCKED(sc); | ||||
/* Dump Rx descriptor. */ | |||||
RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC, | |||||
"%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, tsfl %08X\n", | |||||
__func__, le32toh(stat->rxdw0), le32toh(stat->rxdw1), | |||||
le32toh(stat->rxdw2), le32toh(stat->rxdw3), le32toh(stat->rxdw4), | |||||
le32toh(stat->tsf_low)); | |||||
/* | /* | ||||
* don't pass packets to the ieee80211 framework if the driver isn't | * don't pass packets to the ieee80211 framework if the driver isn't | ||||
* RUNNING. | * RUNNING. | ||||
*/ | */ | ||||
if (!(sc->sc_flags & RTWN_RUNNING)) | if (!(sc->sc_flags & RTWN_RUNNING)) | ||||
return (NULL); | return (-1); | ||||
rxdw0 = le32toh(stat->rxdw0); | rxdw0 = le32toh(stat->rxdw0); | ||||
if (__predict_false(rxdw0 & (RTWN_RXDW0_CRCERR | RTWN_RXDW0_ICVERR))) { | if (__predict_false(rxdw0 & (RTWN_RXDW0_CRCERR | RTWN_RXDW0_ICVERR))) { | ||||
/* | /* | ||||
* This should not happen since we setup our Rx filter | * This should not happen since we setup our Rx filter | ||||
* to not receive these frames. | * to not receive these frames. | ||||
*/ | */ | ||||
RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, | RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, | ||||
"%s: RX flags error (%s)\n", __func__, | "%s: RX flags error (%s)\n", __func__, | ||||
rxdw0 & RTWN_RXDW0_CRCERR ? "CRC" : "ICV"); | rxdw0 & RTWN_RXDW0_CRCERR ? "CRC" : "ICV"); | ||||
goto fail; | return (-1); | ||||
} | } | ||||
pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN); | pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN); | ||||
if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack))) { | if (__predict_false(pktlen < sizeof(struct ieee80211_frame_ack))) { | ||||
/* | /* | ||||
* Should not happen (because of Rx filter setup). | * Should not happen (because of Rx filter setup). | ||||
*/ | */ | ||||
RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, | RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, | ||||
"%s: frame is too short: %d\n", __func__, pktlen); | "%s: frame is too short: %d\n", __func__, pktlen); | ||||
goto fail; | return (-1); | ||||
} | } | ||||
return (0); | |||||
} | |||||
static struct mbuf * | |||||
rtwn_rx_copy_to_mbuf(struct rtwn_softc *sc, struct rtwn_rx_stat_common *stat, | |||||
int totlen) | |||||
{ | |||||
struct ieee80211com *ic = &sc->sc_ic; | |||||
struct mbuf *m; | |||||
RTWN_ASSERT_LOCKED(sc); | |||||
/* Dump Rx descriptor. */ | |||||
RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC, | |||||
"%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, tsfl %08X\n", | |||||
__func__, le32toh(stat->rxdw0), le32toh(stat->rxdw1), | |||||
le32toh(stat->rxdw2), le32toh(stat->rxdw3), le32toh(stat->rxdw4), | |||||
le32toh(stat->tsf_low)); | |||||
if (rtwn_rx_check_pre_alloc(sc, stat) != 0) | |||||
goto fail; | |||||
m = m_get2(totlen, M_NOWAIT, MT_DATA, M_PKTHDR); | m = m_get2(totlen, M_NOWAIT, MT_DATA, M_PKTHDR); | ||||
if (__predict_false(m == NULL)) { | if (__predict_false(m == NULL)) { | ||||
device_printf(sc->sc_dev, "%s: could not allocate RX mbuf\n", | device_printf(sc->sc_dev, "%s: could not allocate RX mbuf\n", | ||||
__func__); | __func__); | ||||
goto fail; | goto fail; | ||||
} | } | ||||
/* Finalize mbuf. */ | /* Finalize mbuf. */ | ||||
memcpy(mtod(m, uint8_t *), (uint8_t *)stat, totlen); | memcpy(mtod(m, uint8_t *), (uint8_t *)stat, totlen); | ||||
m->m_pkthdr.len = m->m_len = totlen; | m->m_pkthdr.len = m->m_len = totlen; | ||||
if (rtwn_check_frame(sc, m) != 0) { | if (rtwn_check_frame(sc, m) != 0) { | ||||
m_freem(m); | m_freem(m); | ||||
goto fail; | goto fail; | ||||
} | } | ||||
return (m); | return (m); | ||||
fail: | fail: | ||||
counter_u64_add(ic->ic_ierrors, 1); | counter_u64_add(ic->ic_ierrors, 1); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
static struct mbuf * | static struct mbuf * | ||||
rtwn_rxeof(struct rtwn_softc *sc, uint8_t *buf, int len) | rtwn_rxeof_fragmented(struct rtwn_usb_softc *uc, struct rtwn_data *data, | ||||
uint8_t *buf, int len) | |||||
{ | { | ||||
struct rtwn_softc *sc = &uc->uc_sc; | |||||
struct ieee80211com *ic = &sc->sc_ic; | |||||
struct rtwn_rx_stat_common *stat = &uc->uc_rx_stat; | |||||
uint32_t rxdw0; | |||||
int totlen, pktlen, infosz, min_len; | |||||
int orig_len = len; | |||||
int alloc_mbuf = 0; | |||||
/* Check if Rx descriptor is not truncated. */ | |||||
if (uc->uc_rx_stat_len < sizeof(*stat)) { | |||||
min_len = min(sizeof(*stat) - uc->uc_rx_stat_len, len); | |||||
memcpy((uint8_t *)stat + uc->uc_rx_stat_len, buf, min_len); | |||||
uc->uc_rx_stat_len += min_len; | |||||
buf += min_len; | |||||
len -= min_len; | |||||
if (uc->uc_rx_stat_len < sizeof(*stat)) | |||||
goto end; | |||||
KASSERT(data->m == NULL, ("%s: data->m != NULL!\n", __func__)); | |||||
alloc_mbuf = 1; | |||||
/* Dump Rx descriptor. */ | |||||
RTWN_DPRINTF(sc, RTWN_DEBUG_RECV_DESC, | |||||
"%s: dw: 0 %08X, 1 %08X, 2 %08X, 3 %08X, 4 %08X, " | |||||
"tsfl %08X\n", __func__, le32toh(stat->rxdw0), | |||||
le32toh(stat->rxdw1), le32toh(stat->rxdw2), | |||||
le32toh(stat->rxdw3), le32toh(stat->rxdw4), | |||||
le32toh(stat->tsf_low)); | |||||
} | |||||
rxdw0 = le32toh(stat->rxdw0); | |||||
pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN); | |||||
infosz = MS(rxdw0, RTWN_RXDW0_INFOSZ) * 8; | |||||
totlen = sizeof(*stat) + infosz + pktlen; | |||||
if (alloc_mbuf) { | |||||
if (rtwn_rx_check_pre_alloc(sc, stat) == 0) { | |||||
data->m = m_getm(NULL, totlen, M_NOWAIT, MT_DATA); | |||||
if (data->m != NULL) { | |||||
m_copyback(data->m, 0, uc->uc_rx_stat_len, | |||||
(caddr_t)stat); | |||||
if (rtwn_check_frame(sc, data->m) != 0) { | |||||
m_freem(data->m); | |||||
data->m = NULL; | |||||
counter_u64_add(ic->ic_ierrors, 1); | |||||
} | |||||
} else | |||||
counter_u64_add(ic->ic_ierrors, 1); | |||||
} else | |||||
counter_u64_add(ic->ic_ierrors, 1); | |||||
uc->uc_rx_off = sizeof(*stat); | |||||
} | |||||
/* If mbuf allocation fails just discard the data. */ | |||||
min_len = min(totlen - uc->uc_rx_off, len); | |||||
if (data->m != NULL) | |||||
m_copyback(data->m, uc->uc_rx_off, min_len, buf); | |||||
uc->uc_rx_off += min_len; | |||||
if (uc->uc_rx_off == totlen) { | |||||
/* Align next frame. */ | |||||
min_len = rtwn_usb_align_rx(uc, | |||||
orig_len - len + min_len, orig_len); | |||||
min_len -= (orig_len - len); | |||||
KASSERT(len >= min_len, ("%s: len (%d) < min_len (%d)!\n", | |||||
__func__, len, min_len)); | |||||
/* Clear mbuf stats. */ | |||||
uc->uc_rx_stat_len = 0; | |||||
uc->uc_rx_off = 0; | |||||
} | |||||
len -= min_len; | |||||
buf += min_len; | |||||
end: | |||||
if (uc->uc_rx_stat_len == 0) | |||||
return (rtwn_rxeof(sc, data, buf, len)); | |||||
else | |||||
return (NULL); | |||||
} | |||||
static struct mbuf * | |||||
rtwn_rxeof(struct rtwn_softc *sc, struct rtwn_data *data, uint8_t *buf, | |||||
int len) | |||||
{ | |||||
struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc); | struct rtwn_usb_softc *uc = RTWN_USB_SOFTC(sc); | ||||
struct rtwn_rx_stat_common *stat; | struct rtwn_rx_stat_common *stat; | ||||
struct mbuf *m, *m0 = NULL; | struct mbuf *m, *m0 = NULL; | ||||
uint32_t rxdw0; | uint32_t rxdw0; | ||||
int totlen, pktlen, infosz; | int totlen, pktlen, infosz; | ||||
/* Prepend defragmented frame (if any). */ | |||||
if (data->m != NULL) { | |||||
m0 = m = data->m; | |||||
data->m = NULL; | |||||
} | |||||
/* Process packets. */ | /* Process packets. */ | ||||
while (len >= sizeof(*stat)) { | while (len >= sizeof(*stat)) { | ||||
stat = (struct rtwn_rx_stat_common *)buf; | stat = (struct rtwn_rx_stat_common *)buf; | ||||
rxdw0 = le32toh(stat->rxdw0); | rxdw0 = le32toh(stat->rxdw0); | ||||
pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN); | pktlen = MS(rxdw0, RTWN_RXDW0_PKTLEN); | ||||
if (__predict_false(pktlen == 0)) | if (__predict_false(pktlen == 0)) | ||||
break; | break; | ||||
infosz = MS(rxdw0, RTWN_RXDW0_INFOSZ) * 8; | infosz = MS(rxdw0, RTWN_RXDW0_INFOSZ) * 8; | ||||
/* Make sure everything fits in xfer. */ | /* Make sure everything fits in xfer. */ | ||||
totlen = sizeof(*stat) + infosz + pktlen; | totlen = sizeof(*stat) + infosz + pktlen; | ||||
if (totlen > len) { | if (totlen > len) { | ||||
device_printf(sc->sc_dev, | RTWN_DPRINTF(sc, RTWN_DEBUG_RECV, | ||||
"%s: totlen (%d) > len (%d)!\n", | "%s: frame is fragmented (totlen %d len %d)\n", | ||||
__func__, totlen, len); | __func__, totlen, len); | ||||
break; | break; | ||||
} | } | ||||
if (m0 == NULL) | if (m0 == NULL) | ||||
m0 = m = rtwn_rx_copy_to_mbuf(sc, stat, totlen); | m0 = m = rtwn_rx_copy_to_mbuf(sc, stat, totlen); | ||||
else { | else { | ||||
m->m_next = rtwn_rx_copy_to_mbuf(sc, stat, totlen); | m->m_nextpkt = rtwn_rx_copy_to_mbuf(sc, stat, totlen); | ||||
if (m->m_next != NULL) | if (m->m_nextpkt != NULL) | ||||
m = m->m_next; | m = m->m_nextpkt; | ||||
} | } | ||||
/* Align next frame. */ | /* Align next frame. */ | ||||
totlen = rtwn_usb_align_rx(uc, totlen, len); | totlen = rtwn_usb_align_rx(uc, totlen, len); | ||||
buf += totlen; | buf += totlen; | ||||
len -= totlen; | len -= totlen; | ||||
} | } | ||||
if (len > 0) | |||||
(void)rtwn_rxeof_fragmented(uc, data, buf, len); | |||||
return (m0); | return (m0); | ||||
} | } | ||||
static struct mbuf * | static struct mbuf * | ||||
rtwn_report_intr(struct rtwn_usb_softc *uc, struct usb_xfer *xfer, | rtwn_report_intr(struct rtwn_usb_softc *uc, struct usb_xfer *xfer, | ||||
struct rtwn_data *data) | struct rtwn_data *data) | ||||
{ | { | ||||
struct rtwn_softc *sc = &uc->uc_sc; | struct rtwn_softc *sc = &uc->uc_sc; | ||||
struct ieee80211com *ic = &sc->sc_ic; | struct ieee80211com *ic = &sc->sc_ic; | ||||
uint8_t *buf; | uint8_t *buf; | ||||
int len; | int len; | ||||
usbd_xfer_status(xfer, &len, NULL, NULL, NULL); | usbd_xfer_status(xfer, &len, NULL, NULL, NULL); | ||||
if (__predict_false(len < sizeof(struct rtwn_rx_stat_common))) { | if (__predict_false(len < sizeof(struct rtwn_rx_stat_common) && | ||||
uc->uc_rx_stat_len == 0)) { | |||||
counter_u64_add(ic->ic_ierrors, 1); | counter_u64_add(ic->ic_ierrors, 1); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
buf = data->buf; | buf = data->buf; | ||||
if (uc->uc_rx_stat_len > 0) | |||||
return (rtwn_rxeof_fragmented(uc, data, data->buf, len)); | |||||
switch (rtwn_classify_intr(sc, buf, len)) { | switch (rtwn_classify_intr(sc, buf, len)) { | ||||
case RTWN_RX_DATA: | case RTWN_RX_DATA: | ||||
return (rtwn_rxeof(sc, buf, len)); | return (rtwn_rxeof(sc, data, buf, len)); | ||||
case RTWN_RX_TX_REPORT: | case RTWN_RX_TX_REPORT: | ||||
if (sc->sc_ratectl != RTWN_RATECTL_NET80211) { | if (sc->sc_ratectl != RTWN_RATECTL_NET80211) { | ||||
/* shouldn't happen */ | /* shouldn't happen */ | ||||
device_printf(sc->sc_dev, | device_printf(sc->sc_dev, | ||||
"%s called while ratectl = %d!\n", | "%s called while ratectl = %d!\n", | ||||
__func__, sc->sc_ratectl); | __func__, sc->sc_ratectl); | ||||
break; | break; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | tr_setup: | ||||
usbd_transfer_submit(xfer); | usbd_transfer_submit(xfer); | ||||
/* | /* | ||||
* To avoid LOR we should unlock our private mutex here to call | * To avoid LOR we should unlock our private mutex here to call | ||||
* ieee80211_input() because here is at the end of a USB | * ieee80211_input() because here is at the end of a USB | ||||
* callback and safe to unlock. | * callback and safe to unlock. | ||||
*/ | */ | ||||
while (m != NULL) { | while (m != NULL) { | ||||
next = m->m_next; | next = m->m_nextpkt; | ||||
m->m_next = NULL; | m->m_nextpkt = NULL; | ||||
ni = rtwn_rx_frame(sc, m); | ni = rtwn_rx_frame(sc, m); | ||||
RTWN_UNLOCK(sc); | RTWN_UNLOCK(sc); | ||||
if (ni != NULL) { | if (ni != NULL) { | ||||
(void)ieee80211_input_mimo(ni, m); | (void)ieee80211_input_mimo(ni, m); | ||||
ieee80211_free_node(ni); | ieee80211_free_node(ni); | ||||
} else { | } else { | ||||
(void)ieee80211_input_mimo_all(ic, m); | (void)ieee80211_input_mimo_all(ic, m); | ||||
} | } | ||||
RTWN_LOCK(sc); | RTWN_LOCK(sc); | ||||
m = next; | m = next; | ||||
} | } | ||||
break; | break; | ||||
default: | default: | ||||
/* needs it to the inactive queue due to a error. */ | /* needs it to the inactive queue due to a error. */ | ||||
data = STAILQ_FIRST(&uc->uc_rx_active); | data = STAILQ_FIRST(&uc->uc_rx_active); | ||||
if (data != NULL) { | if (data != NULL) { | ||||
STAILQ_REMOVE_HEAD(&uc->uc_rx_active, next); | STAILQ_REMOVE_HEAD(&uc->uc_rx_active, next); | ||||
STAILQ_INSERT_TAIL(&uc->uc_rx_inactive, data, next); | STAILQ_INSERT_TAIL(&uc->uc_rx_inactive, data, next); | ||||
} | } | ||||
if (error != USB_ERR_CANCELLED) { | if (error != USB_ERR_CANCELLED) { | ||||
/* XXX restart device if frame was fragmented? */ | |||||
usbd_xfer_set_stall(xfer); | usbd_xfer_set_stall(xfer); | ||||
counter_u64_add(ic->ic_ierrors, 1); | counter_u64_add(ic->ic_ierrors, 1); | ||||
goto tr_setup; | goto tr_setup; | ||||
} | } | ||||
break; | break; | ||||
} | } | ||||
finish: | finish: | ||||
/* Kick-start more transmit in case we stalled */ | /* Kick-start more transmit in case we stalled */ | ||||
rtwn_start(sc); | rtwn_start(sc); | ||||
} | } |