#include <sys/param.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/stream.h>
#include <sys/termio.h>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/cmn_err.h>
#include <sys/stropts.h>
#include <sys/strsubr.h>
#include <sys/strtty.h>
#include <sys/kbio.h>
#include <sys/cred.h>
#include <sys/stat.h>
#include <sys/consdev.h>
#include <sys/kmem.h>
#include <sys/modctl.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/pci.h>
#include <sys/errno.h>
#include <sys/mac_provider.h>
#include <sys/dlpi.h>
#include <sys/ethernet.h>
#include <sys/list.h>
#include <sys/byteorder.h>
#include <sys/strsun.h>
#include <sys/policy.h>
#include <inet/common.h>
#include <inet/nd.h>
#include <inet/mi.h>
#include <inet/wifi_ioctl.h>
#include <sys/mac_wifi.h>
#include "arn_core.h"
#define BITS_PER_BYTE 8
#define OFDM_PLCP_BITS 22
#define HT_RC_2_MCS(_rc) ((_rc) & 0x0f)
#define HT_RC_2_STREAMS(_rc) ((((_rc) & 0x78) >> 3) + 1)
#define L_STF 8
#define L_LTF 8
#define L_SIG 4
#define HT_SIG 8
#define HT_STF 4
#define HT_LTF(_ns) (4 * (_ns))
#define SYMBOL_TIME(_ns) ((_ns) << 2)
#define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5)
#define NUM_SYMBOLS_PER_USEC(_usec) (_usec >> 2)
#define NUM_SYMBOLS_PER_USEC_HALFGI(_usec) (((_usec*5)-4)/18)
#define OFDM_SIFS_TIME 16
static uint32_t bits_per_symbol[][2] = {
{ 26, 54 },
{ 52, 108 },
{ 78, 162 },
{ 104, 216 },
{ 156, 324 },
{ 208, 432 },
{ 234, 486 },
{ 260, 540 },
{ 52, 108 },
{ 104, 216 },
{ 156, 324 },
{ 208, 432 },
{ 312, 648 },
{ 416, 864 },
{ 468, 972 },
{ 520, 1080 },
};
#define IS_HT_RATE(_rate) ((_rate) & 0x80)
#ifdef ARN_TX_AGGREGRATION
static void arn_tx_send_ht_normal(struct arn_softc *sc, struct ath_txq *txq,
struct ath_atx_tid *tid, list_t *bf_list);
static void arn_tx_complete_buf(struct arn_softc *sc, struct ath_buf *bf,
list_t *bf_q, int txok, int sendbar);
static void arn_tx_txqaddbuf(struct arn_softc *sc, struct ath_txq *txq,
list_t *buf_list);
static void arn_buf_set_rate(struct arn_softc *sc, struct ath_buf *bf);
static int arn_tx_num_badfrms(struct arn_softc *sc,
struct ath_buf *bf, int txok);
static void arn_tx_rc_status(struct ath_buf *bf, struct ath_desc *ds,
int nbad, int txok, boolean_t update_rc);
#endif
static void
arn_get_beaconconfig(struct arn_softc *sc, struct ath_beacon_config *conf)
{
ieee80211com_t *ic = (ieee80211com_t *)sc;
struct ieee80211_node *in = ic->ic_bss;
conf->beacon_interval = in->in_intval ?
in->in_intval : ATH_DEFAULT_BINTVAL;
conf->listen_interval = 100;
conf->dtim_count = 1;
conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
}
#ifdef ARN_TX_AGGREGATION
static int
arn_aggr_query(struct arn_softc *sc, struct ath_node *an, uint8_t tidno)
{
struct ath_atx_tid *tid;
tid = ATH_AN_2_TID(an, tidno);
if (tid->state & AGGR_ADDBA_COMPLETE ||
tid->state & AGGR_ADDBA_PROGRESS)
return (1);
else
return (0);
}
static void
arn_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
{
struct ath_atx_ac *ac = tid->ac;
if (tid->paused)
return;
if (tid->sched)
return;
tid->sched = B_TRUE;
list_insert_tail(&ac->tid_q, &tid->list);
if (ac->sched)
return;
ac->sched = B_TRUE;
list_insert_tail(&txq->axq_acq, &ac->list);
}
static void
arn_tx_pause_tid(struct arn_softc *sc, struct ath_atx_tid *tid)
{
struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
mutex_enter(&txq->axq_lock);
tid->paused++;
mutex_exit(&txq->axq_lock);
}
void
arn_tx_resume_tid(struct arn_softc *sc, struct ath_atx_tid *tid)
{
struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
ASSERT(tid->paused > 0);
mutex_enter(&txq->axq_lock);
tid->paused--;
if (tid->paused > 0)
goto unlock;
if (list_empty(&tid->buf_q))
goto unlock;
arn_tx_queue_tid(txq, tid);
arn_txq_schedule(sc, txq);
unlock:
mutex_exit(&txq->axq_lock);
}
static void
arn_tx_flush_tid(struct arn_softc *sc, struct ath_atx_tid *tid)
{
struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
struct ath_buf *bf;
list_t list;
list_create(&list, sizeof (struct ath_buf),
offsetof(struct ath_buf, bf_node));
ASSERT(tid->paused > 0);
mutex_enter(&txq->axq_lock);
tid->paused--;
if (tid->paused > 0) {
mutex_exit(&txq->axq_lock);
return;
}
while (!list_empty(&tid->buf_q)) {
bf = list_head(&tid->buf_q);
ASSERT(!bf_isretried(bf));
list_remove(&tid->buf_q, bf);
list_insert_tail(&list, bf);
arn_tx_send_ht_normal(sc, txq, tid, &list);
}
mutex_exit(&txq->axq_lock);
}
static void
arn_tx_update_baw(struct arn_softc *sc, struct ath_atx_tid *tid, int seqno)
{
int index, cindex;
index = ATH_BA_INDEX(tid->seq_start, seqno);
cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
tid->tx_buf[cindex] = NULL;
while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
INCR(tid->seq_start, IEEE80211_SEQ_MAX);
INCR(tid->baw_head, ATH_TID_MAX_BUFS);
}
}
static void
arn_tx_addto_baw(struct arn_softc *sc, struct ath_atx_tid *tid,
struct ath_buf *bf)
{
int index, cindex;
if (bf_isretried(bf))
return;
index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
ASSERT(tid->tx_buf[cindex] == NULL);
tid->tx_buf[cindex] = bf;
if (index >= ((tid->baw_tail - tid->baw_head) &
(ATH_TID_MAX_BUFS - 1))) {
tid->baw_tail = cindex;
INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
}
}
static void
arn_tid_drain(struct arn_softc *sc,
struct ath_txq *txq,
struct ath_atx_tid *tid)
{
struct ath_buf *bf;
list_t list;
list_create(&list, sizeof (struct ath_buf),
offsetof(struct ath_buf, bf_node));
for (;;) {
if (list_empty(&tid->buf_q))
break;
bf = list_head(&tid->buf_q);
list_remove(&tid->buf_q, bf);
list_insert_tail(&list, bf);
if (bf_isretried(bf))
arn_tx_update_baw(sc, tid, bf->bf_seqno);
mutex_enter(&txq->axq_lock);
arn_tx_complete_buf(sc, bf, &list, 0, 0);
mutex_exit(&txq->axq_lock);
}
tid->seq_next = tid->seq_start;
tid->baw_tail = tid->baw_head;
}
static void
arn_tx_set_retry(struct arn_softc *sc, struct ath_buf *bf)
{
struct ieee80211_frame *wh;
wh = (struct ieee80211_frame *)bf->bf_dma.mem_va;
bf->bf_state.bf_type |= BUF_RETRY;
bf->bf_retries++;
*(uint16_t *)&wh->i_seq[0] |= LE_16(0x0800);
}
static struct ath_buf *
arn_clone_txbuf(struct arn_softc *sc, struct ath_buf *bf)
{
struct ath_buf *tbf;
mutex_enter(&sc->sc_txbuflock);
ASSERT(!list_empty((&sc->sc_txbuf_list)));
tbf = list_head(&sc->sc_txbuf_list);
list_remove(&sc->sc_txbuf_list, tbf);
mutex_exit(&sc->sc_txbuflock);
ATH_TXBUF_RESET(tbf);
tbf->bf_daddr = bf->bf_daddr;
tbf->bf_dma = bf->bf_dma;
*(tbf->bf_desc) = *(bf->bf_desc);
tbf->bf_state = bf->bf_state;
return (tbf);
}
static void
arn_tx_complete_aggr(struct arn_softc *sc, struct ath_txq *txq,
struct ath_buf *bf, list_t *bf_q, int txok)
{
struct ieee80211_node *in;
struct ath_node *an = NULL;
struct ath_atx_tid *tid = NULL;
struct ath_buf *bf_next, *bf_last = bf->bf_lastbf;
struct ath_desc *ds = bf_last->bf_desc;
list_t list, list_pending;
uint16_t seq_st = 0, acked_cnt = 0, txfail_cnt = 0;
uint32_t ba[WME_BA_BMP_SIZE >> 5];
int isaggr, txfail, txpending, sendbar = 0, needreset = 0, nbad = 0;
boolean_t rc_update = B_TRUE;
an = ATH_NODE(in);
tid = ATH_AN_2_TID(an, bf->bf_tidno);
isaggr = bf_isaggr(bf);
memset(ba, 0, WME_BA_BMP_SIZE >> 3);
if (isaggr && txok) {
if (ATH_DS_TX_BA(ds)) {
seq_st = ATH_DS_BA_SEQ(ds);
memcpy(ba, ATH_DS_BA_BITMAP(ds),
WME_BA_BMP_SIZE >> 3);
} else {
if (sc->sc_ah->ah_opmode == ATH9K_M_STA)
needreset = 1;
}
}
list_create(&list_pending, sizeof (struct ath_buf),
offsetof(struct ath_buf, bf_node));
list_create(&list, sizeof (struct ath_buf),
offsetof(struct ath_buf, bf_node));
nbad = arn_tx_num_badfrms(sc, bf, txok);
while (bf) {
txfail = txpending = 0;
bf_next = bf->bf_next;
if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
acked_cnt++;
} else if (!isaggr && txok) {
acked_cnt++;
} else {
if (!(tid->state & AGGR_CLEANUP) &&
ds->ds_txstat.ts_flags != ATH9K_TX_SW_ABORTED) {
if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
arn_tx_set_retry(sc, bf);
txpending = 1;
} else {
bf->bf_state.bf_type |= BUF_XRETRY;
txfail = 1;
sendbar = 1;
txfail_cnt++;
}
} else {
txfail = 1;
}
}
if (bf_next == NULL) {
list_create(&list, sizeof (struct ath_buf),
offsetof(struct ath_buf, bf_node));
} else {
ASSERT(!list_empty(bf_q));
list_remove(bf_q, bf);
list_insert_tail(&list, bf);
}
if (!txpending) {
mutex_enter(&txq->axq_lock);
arn_tx_update_baw(sc, tid, bf->bf_seqno);
mutex_exit(&txq->axq_lock);
if (rc_update && (acked_cnt == 1 || txfail_cnt == 1)) {
ath_tx_rc_status(bf, ds, nbad, txok, B_TRUE);
rc_update = B_FALSE;
} else {
ath_tx_rc_status(bf, ds, nbad, txok, B_FALSE);
}
ath_tx_complete_buf(sc, bf, list, !txfail, sendbar);
} else {
if (bf->bf_next == NULL &&
bf_last->bf_status & ATH_BUFSTATUS_STALE) {
struct ath_buf *tbf;
tbf = arn_clone_txbuf(sc, bf_last);
ath9k_hw_cleartxdesc(sc->sc_ah, tbf->bf_desc);
list_insert_tail(&list, tbf);
} else {
ath9k_hw_cleartxdesc(sc->sc_ah, bf->bf_desc);
}
list_splice_tail_init(&list, &list_pending);
list_move_tail(&list_pending, &list);
}
bf = bf_next;
}
if (tid->state & AGGR_CLEANUP) {
if (tid->baw_head == tid->baw_tail) {
tid->state &= ~AGGR_ADDBA_COMPLETE;
tid->addba_exchangeattempts = 0;
tid->state &= ~AGGR_CLEANUP;
arn_tx_flush_tid(sc, tid);
}
return;
}
if (!list_empty(&list_pending)) {
mutex_enter(&txq->axq_lock);
list_move_tail(&list_pending, &tid->buf_q);
arn_tx_queue_tid(txq, tid);
mutex_exit(&txq->axq_lock);
}
}
static uint32_t
arn_lookup_rate(struct arn_softc *sc, struct ath_buf *bf,
struct ath_atx_tid *tid)
{
struct ath_rate_table *rate_table = sc->sc_currates;
struct ath9k_tx_rate *rates;
struct ath_tx_info_priv *tx_info_priv;
uint32_t max_4ms_framelen, frmlen;
uint16_t aggr_limit, legacy = 0, maxampdu;
int i;
rates = (struct ath9k_tx_rate *)bf->rates;
tx_info_priv = (struct ath_tx_info_priv *)&bf->tx_info_priv;
max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
for (i = 0; i < 4; i++) {
if (rates[i].count) {
if (!WLAN_RC_PHY_HT
(rate_table->info[rates[i].idx].phy)) {
legacy = 1;
break;
}
frmlen =
rate_table->info[rates[i].idx].max_4ms_framelen;
max_4ms_framelen = min(max_4ms_framelen, frmlen);
}
}
if (legacy)
return (0);
aggr_limit = min(max_4ms_framelen, (uint32_t)ATH_AMPDU_LIMIT_DEFAULT);
maxampdu = tid->an->maxampdu;
if (maxampdu)
aggr_limit = min(aggr_limit, maxampdu);
return (aggr_limit);
}
static int
arn_compute_num_delims(struct arn_softc *sc, struct ath_atx_tid *tid,
struct ath_buf *bf, uint16_t frmlen)
{
struct ath_rate_table *rt = sc->sc_currates;
struct ath9k_tx_rate *rates = (struct ath9k_tx_rate *)bf->rates;
uint32_t nsymbits, nsymbols, mpdudensity;
uint16_t minlen;
uint8_t rc, flags, rix;
int width, half_gi, ndelim, mindelim;
ndelim = ATH_AGGR_GET_NDELIM(frmlen);
if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
ndelim += ATH_AGGR_ENCRYPTDELIM;
mpdudensity = tid->an->mpdudensity;
if (mpdudensity == 0)
return (ndelim);
rix = rates[0].idx;
flags = rates[0].flags;
rc = rt->info[rix].ratecode;
width = (flags & ATH9K_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
half_gi = (flags & ATH9K_TX_RC_SHORT_GI) ? 1 : 0;
if (half_gi)
nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
else
nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
if (nsymbols == 0)
nsymbols = 1;
nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
if (frmlen < minlen) {
mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
ndelim = max(mindelim, ndelim);
}
return (ndelim);
}
static enum ATH_AGGR_STATUS
arn_tx_form_aggr(struct arn_softc *sc, struct ath_atx_tid *tid,
list_t *bf_q)
{
#define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
struct ath_buf *bf, *bf_first, *bf_prev = NULL;
int rl = 0, nframes = 0, ndelim, prev_al = 0;
uint16_t aggr_limit = 0, al = 0, bpad = 0,
al_delta, h_baw = tid->baw_size / 2;
enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
bf_first = list_head(&tid->buf_q);
do {
bf = list_head(&tid->buf_q);
if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
status = ATH_AGGR_BAW_CLOSED;
break;
}
if (!rl) {
aggr_limit = arn_lookup_rate(sc, bf, tid);
rl = 1;
}
al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
if (nframes &&
(aggr_limit < (al + bpad + al_delta + prev_al))) {
status = ATH_AGGR_LIMITED;
break;
}
if (nframes >= min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
status = ATH_AGGR_LIMITED;
break;
}
nframes++;
al += bpad + al_delta;
ndelim =
arn_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
bpad = PADBYTES(al_delta) + (ndelim << 2);
bf->bf_next = NULL;
bf->bf_desc->ds_link = 0;
arn_tx_addto_baw(sc, tid, bf);
ath9k_hw_set11n_aggr_middle(sc->sc_ah, bf->bf_desc, ndelim);
list_remove(&tid->buf_q, bf);
list_insert_tail(bf_q, bf);
if (bf_prev) {
bf_prev->bf_next = bf;
bf_prev->bf_desc->ds_link = bf->bf_daddr;
}
bf_prev = bf;
} while (!list_empty(&tid->buf_q));
bf_first->bf_al = al;
bf_first->bf_nframes = nframes;
return (status);
#undef PADBYTES
}
static void
arn_tx_sched_aggr(struct arn_softc *sc, struct ath_txq *txq,
struct ath_atx_tid *tid)
{
struct ath_buf *bf;
enum ATH_AGGR_STATUS status;
list_t bf_q;
do {
if (list_empty(&tid->buf_q))
return;
list_create(&bf_q, sizeof (struct ath_buf),
offsetof(struct ath_buf, bf_node));
status = arn_tx_form_aggr(sc, tid, &bf_q);
if (list_empty(&bf_q))
break;
bf = list_head(&bf_q);
bf->bf_lastbf = list_object(&bf_q, bf->bf_node.list_prev);
if (bf->bf_nframes == 1) {
bf->bf_state.bf_type &= ~BUF_AGGR;
ath9k_hw_clr11n_aggr(sc->sc_ah, bf->bf_desc);
ath_buf_set_rate(sc, bf);
arn_tx_txqaddbuf(sc, txq, &bf_q);
continue;
}
bf->bf_state.bf_type |= BUF_AGGR;
ath_buf_set_rate(sc, bf);
ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
ath9k_hw_set11n_aggr_last(sc->sc_ah, bf->bf_lastbf->bf_desc);
txq->axq_aggr_depth++;
arn_tx_txqaddbuf(sc, txq, &bf_q);
} while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
status != ATH_AGGR_BAW_CLOSED);
}
int
arn_tx_aggr_start(struct arn_softc *sc, struct ieee80211_node *in,
uint16_t tid, uint16_t *ssn)
{
struct ath_atx_tid *txtid;
struct ath_node *an;
an = ATH_NODE(in);
if (sc->sc_flags & SC_OP_TXAGGR) {
txtid = ATH_AN_2_TID(an, tid);
txtid->state |= AGGR_ADDBA_PROGRESS;
arn_tx_pause_tid(sc, txtid);
*ssn = txtid->seq_start;
}
return (0);
}
int
arn_tx_aggr_stop(struct arn_softc *sc, struct ieee80211_node *in, uint16_t tid)
{
struct ath_node *an = ATH_NODE(in);
struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
struct ath_txq *txq = &sc->sc_txq[txtid->ac->qnum];
struct ath_buf *bf;
list_t list;
list_create(&list, sizeof (struct ath_buf),
offsetof(struct ath_buf, bf_node));
if (txtid->state & AGGR_CLEANUP)
return (0);
if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
txtid->addba_exchangeattempts = 0;
return (0);
}
arn_tx_pause_tid(sc, txtid);
mutex_enter(&txq->axq_lock);
while (!list_empty(&txtid->buf_q)) {
bf = list_head(&txtid->buf_q);
if (!bf_isretried(bf)) {
break;
}
list_remove(&txtid->buf_q, bf);
list_insert_tail(&list, bf);
arn_tx_update_baw(sc, txtid, bf->bf_seqno);
}
mutex_exit(&txq->axq_lock);
if (txtid->baw_head != txtid->baw_tail) {
txtid->state |= AGGR_CLEANUP;
} else {
txtid->state &= ~AGGR_ADDBA_COMPLETE;
txtid->addba_exchangeattempts = 0;
arn_tx_flush_tid(sc, txtid);
}
return (0);
}
void
arn_tx_aggr_resume(struct arn_softc *sc,
struct ieee80211_node *in,
uint16_t tid)
{
struct ath_atx_tid *txtid;
struct ath_node *an;
an = ATH_NODE(in);
if (sc->sc_flags & SC_OP_TXAGGR) {
txtid = ATH_AN_2_TID(an, tid);
txtid->baw_size = (0x8) << sc->sc_ht_conf.ampdu_factor;
txtid->state |= AGGR_ADDBA_COMPLETE;
txtid->state &= ~AGGR_ADDBA_PROGRESS;
arn_tx_resume_tid(sc, txtid);
}
}
boolean_t
arn_tx_aggr_check(struct arn_softc *sc, struct ath_node *an, uint8_t tidno)
{
struct ath_atx_tid *txtid;
if (!(sc->sc_flags & SC_OP_TXAGGR))
return (B_FALSE);
txtid = ATH_AN_2_TID(an, tidno);
if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
if (!(txtid->state & AGGR_ADDBA_PROGRESS) &&
(txtid->addba_exchangeattempts < ADDBA_EXCHANGE_ATTEMPTS)) {
txtid->addba_exchangeattempts++;
return (B_TRUE);
}
}
return (B_FALSE);
}
static void
arn_txq_drain_pending_buffers(struct arn_softc *sc, struct ath_txq *txq)
{
struct ath_atx_ac *ac, *ac_tmp;
struct ath_atx_tid *tid, *tid_tmp;
list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq) {
list_remove(&txq->axq_acq, ac);
ac->sched = B_FALSE;
list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q) {
list_remove(&ac->tid_q, tid);
tid->sched = B_FALSE;
arn_tid_drain(sc, txq, tid);
}
}
}
int
arn_tx_get_qnum(struct arn_softc *sc, int qtype, int haltype)
{
int qnum;
switch (qtype) {
case ATH9K_TX_QUEUE_DATA:
if (haltype >= ARRAY_SIZE(sc->sc_haltype2q)) {
ARN_DBG((ARN_DBG_FATAL, "arn: arn_tx_get_qnum(): "
"HAL AC %u out of range, max %zu!\n",
haltype, ARRAY_SIZE(sc->sc_haltype2q)));
return (-1);
}
qnum = sc->sc_haltype2q[haltype];
break;
case ATH9K_TX_QUEUE_BEACON:
qnum = sc->sc_beaconq;
break;
case ATH9K_TX_QUEUE_CAB:
qnum = sc->sc_cabq->axq_qnum;
break;
default:
qnum = -1;
}
return (qnum);
}
struct ath_txq *
arn_test_get_txq(struct arn_softc *sc, struct ieee80211_node *in,
struct ieee80211_frame *wh, uint8_t type)
{
struct ieee80211_qosframe *qwh = NULL;
struct ath_txq *txq = NULL;
int tid = -1;
int qos_ac;
int qnum;
if (in->in_flags & IEEE80211_NODE_QOS) {
if ((type & IEEE80211_FC0_TYPE_MASK) ==
IEEE80211_FC0_TYPE_DATA) {
if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
qwh = (struct ieee80211_qosframe *)wh;
tid = qwh->i_qos[0] & IEEE80211_QOS_TID;
switch (tid) {
case 1:
case 2:
qos_ac = WME_AC_BK;
case 0:
case 3:
qos_ac = WME_AC_BE;
case 4:
case 5:
qos_ac = WME_AC_VI;
case 6:
case 7:
qos_ac = WME_AC_VO;
}
}
} else {
qos_ac = WME_AC_VO;
}
} else if ((type & IEEE80211_FC0_TYPE_MASK) ==
IEEE80211_FC0_TYPE_MGT) {
qos_ac = WME_AC_VO;
} else if ((type & IEEE80211_FC0_TYPE_MASK) ==
IEEE80211_FC0_TYPE_CTL) {
qos_ac = WME_AC_VO;
} else {
qos_ac = WME_AC_BK;
}
qnum = arn_get_hal_qnum(qos_ac, sc);
txq = &sc->sc_txq[qnum];
mutex_enter(&txq->axq_lock);
if (txq->axq_depth >= (ATH_TXBUF - 20)) {
ARN_DBG((ARN_DBG_XMIT,
"TX queue: %d is full, depth: %d\n",
qnum, txq->axq_depth));
sc->sc_resched_needed = B_TRUE;
txq->stopped = 1;
mutex_exit(&txq->axq_lock);
return (NULL);
}
mutex_exit(&txq->axq_lock);
return (txq);
}
static void
assign_aggr_tid_seqno(struct arn_softc *sc,
struct ath_buf *bf,
struct ieee80211_frame *wh)
{
struct ath_node *an;
struct ath_atx_tid *tid;
struct ieee80211_node *in;
struct ieee80211_qosframe *qwh = NULL;
ieee80211com_t *ic = (ieee80211com_t *)sc;
in = ieee80211_find_txnode(ic, wh->i_addr1);
if (in == NULL) {
arn_problem("assign_aggr_tid_seqno():"
"failed to find tx node\n");
return;
}
an = ATH_NODE(in);
if (in->in_flags & IEEE80211_NODE_QOS) {
if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
qwh = (struct ieee80211_qosframe *)wh;
bf->bf_tidno = qwh->i_qos[0] & IEEE80211_QOS_TID;
}
}
tid = ATH_AN_2_TID(an, bf->bf_tidno);
*(uint16_t *)&wh->i_seq[0] =
LE_16(tid->seq_next << IEEE80211_SEQ_SEQ_SHIFT);
bf->bf_seqno = tid->seq_next;
INCR(tid->seq_next, IEEE80211_SEQ_MAX);
ieee80211_free_node(in);
}
static int
arn_tx_num_badfrms(struct arn_softc *sc, struct ath_buf *bf, int txok)
{
struct ath_buf *bf_last = bf->bf_lastbf;
struct ath_desc *ds = bf_last->bf_desc;
uint16_t seq_st = 0;
uint32_t ba[WME_BA_BMP_SIZE >> 5];
int ba_index;
int nbad = 0;
int isaggr = 0;
if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
return (0);
isaggr = bf_isaggr(bf);
if (isaggr) {
seq_st = ATH_DS_BA_SEQ(ds);
memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
}
while (bf) {
ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
nbad++;
bf = bf->bf_next;
}
return (nbad);
}
static void
arn_tx_send_ht_normal(struct arn_softc *sc,
struct ath_txq *txq,
struct ath_atx_tid *tid,
list_t *list)
{
struct ath_buf *bf;
bf = list_head(list);
bf->bf_state.bf_type &= ~BUF_AMPDU;
INCR(tid->seq_start, IEEE80211_SEQ_MAX);
bf->bf_nframes = 1;
bf->bf_lastbf = bf;
ath_buf_set_rate(sc, bf);
arn_tx_txqaddbuf(sc, txq, list);
}
static void
arn_tx_txqaddbuf(struct arn_softc *sc,
struct ath_txq *txq,
list_t *list)
{
struct ath_buf *bf;
if (list_empty(list))
return;
bf = list_head(list);
list_splice_tail_init(list, &txq->axq_q);
txq->axq_depth++;
txq->axq_totalqueued++;
txq->axq_linkbuf = list_object(list, txq->axq_q.prev);
ARN_DBG((ARN_DBG_QUEUE,
"qnum: %d, txq depth: %d\n", txq->axq_qnum, txq->axq_depth));
if (txq->axq_link == NULL) {
ath9k_hw_puttxbuf(sc->sc_ah, txq->axq_qnum, bf->bf_daddr);
ARN_DBG((ARN_DBG_XMIT,
"TXDP[%u] = %llx (%p)\n",
txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc));
} else {
*txq->axq_link = bf->bf_daddr;
ARN_DBG((ARN_DBG_XMIT, "link[%u] (%p)=%llx (%p)\n",
txq->axq_qnum, txq->axq_link,
ito64(bf->bf_daddr), bf->bf_desc));
}
txq->axq_link = &(bf->bf_lastbf->bf_desc->ds_link);
ath9k_hw_txstart(sc->sc_ah, txq->axq_qnum);
}
#endif
static struct ath_buf *
arn_tx_get_buffer(struct arn_softc *sc)
{
struct ath_buf *bf = NULL;
mutex_enter(&sc->sc_txbuflock);
bf = list_head(&sc->sc_txbuf_list);
if (bf != NULL)
list_remove(&sc->sc_txbuf_list, bf);
if (list_empty(&sc->sc_txbuf_list)) {
ARN_DBG((ARN_DBG_XMIT, "arn: arn_tx(): "
"stop queue\n"));
sc->sc_stats.ast_tx_qstop++;
}
mutex_exit(&sc->sc_txbuflock);
return (bf);
}
static uint32_t
setup_tx_flags(struct arn_softc *sc,
struct ieee80211_frame *wh,
uint32_t pktlen)
{
int flags = 0;
ieee80211com_t *ic = (ieee80211com_t *)sc;
flags |= ATH9K_TXDESC_CLRDMASK;
flags |= ATH9K_TXDESC_INTREQ;
if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
flags |= ATH9K_TXDESC_NOACK;
sc->sc_stats.ast_tx_noack++;
}
if (pktlen > ic->ic_rtsthreshold) {
flags |= ATH9K_TXDESC_RTSENA;
sc->sc_stats.ast_tx_rts++;
}
return (flags);
}
static void
ath_tx_setup_buffer(struct arn_softc *sc, struct ath_buf *bf,
struct ieee80211_node *in, struct ieee80211_frame *wh,
uint32_t pktlen, uint32_t keytype)
{
ieee80211com_t *ic = (ieee80211com_t *)sc;
int i;
ATH_TXBUF_RESET(bf);
for (i = 0; i < 4; i++) {
bf->rates[i].idx = -1;
bf->rates[i].flags = 0;
bf->rates[i].count = 1;
}
bf->bf_in = in;
bf->bf_frmlen = pktlen;
IEEE80211_IS_DATA(wh) ?
(bf->bf_state.bf_type |= BUF_DATA) :
(bf->bf_state.bf_type &= ~BUF_DATA);
IEEE80211_IS_BACK_REQ(wh) ?
(bf->bf_state.bf_type |= BUF_BAR) :
(bf->bf_state.bf_type &= ~BUF_BAR);
IEEE80211_IS_PSPOLL(wh) ?
(bf->bf_state.bf_type |= BUF_PSPOLL) :
(bf->bf_state.bf_type &= ~BUF_PSPOLL);
((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
(in->in_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) ?
(bf->bf_state.bf_type |= BUF_SHORT_PREAMBLE) :
(bf->bf_state.bf_type &= ~BUF_SHORT_PREAMBLE);
bf->bf_flags = setup_tx_flags(sc, wh, pktlen);
bf->bf_keytype = keytype;
#ifdef ARN_TX_AGGREGATION
if (ieee80211_is_data_qos(wh) && (sc->sc_flags & SC_OP_TXAGGR))
assign_aggr_tid_seqno(sc, bf, wh);
#endif
}
static uint32_t
ath_pkt_duration(struct arn_softc *sc, uint8_t rix, struct ath_buf *bf,
int width, int half_gi, boolean_t shortPreamble)
{
struct ath_rate_table *rate_table = sc->sc_currates;
uint32_t nbits, nsymbits, duration, nsymbols;
uint8_t rc;
int streams, pktlen;
pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
rc = rate_table->info[rix].ratecode;
if (!IS_HT_RATE(rc))
return (ath9k_hw_computetxtime(sc->sc_ah, rate_table, pktlen,
rix, shortPreamble));
nbits = (pktlen << 3) + OFDM_PLCP_BITS;
nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
nsymbols = (nbits + nsymbits - 1) / nsymbits;
if (!half_gi)
duration = SYMBOL_TIME(nsymbols);
else
duration = SYMBOL_TIME_HALFGI(nsymbols);
streams = HT_RC_2_STREAMS(rc);
duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
return (duration);
}
static void
ath_buf_set_rate(struct arn_softc *sc,
struct ath_buf *bf,
struct ieee80211_frame *wh)
{
struct ath_hal *ah = sc->sc_ah;
struct ath_rate_table *rt;
struct ath_desc *ds = bf->bf_desc;
struct ath_desc *lastds = bf->bf_desc;
struct ath9k_11n_rate_series series[4];
struct ath9k_tx_rate *rates;
int i, flags, rtsctsena = 0;
uint32_t ctsduration = 0;
uint8_t rix = 0, cix, ctsrate = 0;
(void) memset(series, 0, sizeof (struct ath9k_11n_rate_series) * 4);
rates = bf->rates;
if (IEEE80211_HAS_MOREFRAGS(wh) ||
wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK) {
rates[1].count = rates[2].count = rates[3].count = 0;
rates[1].idx = rates[2].idx = rates[3].idx = 0;
rates[0].count = ATH_TXMAXTRY;
}
rt = sc->sc_currates;
for (i = 3; i >= 0; i--) {
if (rates[i].count && (rates[i].idx >= 0)) {
rix = rates[i].idx;
break;
}
}
flags = (bf->bf_flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA));
cix = rt->info[rix].ctrl_rate;
if (sc->sc_protmode != PROT_M_NONE &&
!(bf->bf_flags & ATH9K_TXDESC_NOACK) &&
(rt->info[rix].phy == WLAN_RC_PHY_OFDM ||
WLAN_RC_PHY_HT(rt->info[rix].phy))) {
if (sc->sc_protmode == PROT_M_RTSCTS)
flags = ATH9K_TXDESC_RTSENA;
else if (sc->sc_protmode == PROT_M_CTSONLY)
flags = ATH9K_TXDESC_CTSENA;
cix = rt->info[sc->sc_protrix].ctrl_rate;
rtsctsena = 1;
}
if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) && bf_isdata(bf)) {
if (!rtsctsena)
flags = ATH9K_TXDESC_RTSENA;
}
if (sc->sc_config.ath_aggr_prot &&
(!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
flags = ATH9K_TXDESC_RTSENA;
cix = rt->info[sc->sc_protrix].ctrl_rate;
rtsctsena = 1;
}
if (bf_isaggr(bf) && (bf->bf_al > ah->ah_caps.rts_aggr_limit))
flags &= ~(ATH9K_TXDESC_RTSENA);
ctsrate = rt->info[cix].ratecode |
(bf_isshpreamble(bf) ? rt->info[cix].short_preamble : 0);
for (i = 0; i < 4; i++) {
if (!rates[i].count || (rates[i].idx < 0))
continue;
rix = rates[i].idx;
series[i].Rate = rt->info[rix].ratecode |
(bf_isshpreamble(bf) ?
rt->info[rix].short_preamble : 0);
series[i].Tries = rates[i].count;
series[i].RateFlags =
((rates[i].flags & ATH9K_TX_RC_USE_RTS_CTS) ?
ATH9K_RATESERIES_RTS_CTS : 0) |
((rates[i].flags & ATH9K_TX_RC_40_MHZ_WIDTH) ?
ATH9K_RATESERIES_2040 : 0) |
((rates[i].flags & ATH9K_TX_RC_SHORT_GI) ?
ATH9K_RATESERIES_HALFGI : 0);
series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
(rates[i].flags & ATH9K_TX_RC_40_MHZ_WIDTH) != 0,
(rates[i].flags & ATH9K_TX_RC_SHORT_GI),
bf_isshpreamble(bf));
series[i].ChSel = sc->sc_tx_chainmask;
if (rtsctsena)
series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
ARN_DBG((ARN_DBG_RATE,
"series[%d]--flags & ATH9K_TX_RC_USE_RTS_CTS = %08x"
"--flags & ATH9K_TX_RC_40_MHZ_WIDTH = %08x"
"--flags & ATH9K_TX_RC_SHORT_GI = %08x\n",
rates[i].flags & ATH9K_TX_RC_USE_RTS_CTS,
rates[i].flags & ATH9K_TX_RC_40_MHZ_WIDTH,
rates[i].flags & ATH9K_TX_RC_SHORT_GI));
ARN_DBG((ARN_DBG_RATE,
"series[%d]:"
"dot11rate:%d"
"index:%d"
"retry count:%d\n",
i,
(rt->info[rates[i].idx].ratekbps)/1000,
rates[i].idx,
rates[i].count));
}
ath9k_hw_set11n_ratescenario(ah, ds, lastds, !bf_ispspoll(bf),
ctsrate, ctsduration,
series, 4, flags);
if (sc->sc_config.ath_aggr_prot && flags)
ath9k_hw_set11n_burstduration(ah, ds, 8192);
}
static void
ath_tx_complete(struct arn_softc *sc, struct ath_buf *bf,
struct ath_xmit_status *tx_status)
{
boolean_t is_data = bf_isdata(bf);
ARN_DBG((ARN_DBG_XMIT, "TX complete\n"));
if (tx_status->flags & ATH_TX_BAR)
tx_status->flags &= ~ATH_TX_BAR;
bf->rates[0].count = tx_status->retries + 1;
arn_tx_status(sc, bf, is_data);
}
static void
ath_tx_complete_buf(struct arn_softc *sc, struct ath_buf *bf,
int txok, int sendbar)
{
struct ath_xmit_status tx_status;
tx_status.retries = bf->bf_retries;
tx_status.flags = 0;
if (sendbar)
tx_status.flags = ATH_TX_BAR;
if (!txok) {
tx_status.flags |= ATH_TX_ERROR;
if (bf_isxretried(bf))
tx_status.flags |= ATH_TX_XRETRY;
}
ath_tx_complete(sc, bf, &tx_status);
}
static void
arn_tx_stopdma(struct arn_softc *sc, struct ath_txq *txq)
{
struct ath_hal *ah = sc->sc_ah;
(void) ath9k_hw_stoptxdma(ah, txq->axq_qnum);
ARN_DBG((ARN_DBG_XMIT, "arn: arn_drain_txdataq(): "
"tx queue [%u] %x, link %p\n",
txq->axq_qnum,
ath9k_hw_gettxbuf(ah, txq->axq_qnum), txq->axq_link));
}
static void
arn_drain_txdataq(struct arn_softc *sc, boolean_t retry_tx)
{
struct ath_hal *ah = sc->sc_ah;
int i, status, npend = 0;
if (!(sc->sc_flags & SC_OP_INVALID)) {
for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
if (ARN_TXQ_SETUP(sc, i)) {
arn_tx_stopdma(sc, &sc->sc_txq[i]);
npend += ath9k_hw_numtxpending(ah,
sc->sc_txq[i].axq_qnum);
}
}
}
if (npend) {
ARN_DBG((ARN_DBG_XMIT, "arn: arn_drain_txdataq(): "
"Unable to stop TxDMA. Reset HAL!\n"));
if (!ath9k_hw_reset(ah,
sc->sc_ah->ah_curchan,
sc->tx_chan_width,
sc->sc_tx_chainmask, sc->sc_rx_chainmask,
sc->sc_ht_extprotspacing, B_TRUE, &status)) {
ARN_DBG((ARN_DBG_FATAL, "arn: arn_drain_txdataq(): "
"unable to reset hardware; hal status %u\n",
status));
}
}
for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
if (ARN_TXQ_SETUP(sc, i))
arn_tx_draintxq(sc, &sc->sc_txq[i]);
}
}
struct ath_txq *
arn_txq_setup(struct arn_softc *sc, int qtype, int subtype)
{
struct ath_hal *ah = sc->sc_ah;
struct ath9k_tx_queue_info qi;
int qnum;
(void) memset(&qi, 0, sizeof (qi));
qi.tqi_subtype = subtype;
qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
qi.tqi_physCompBuf = 0;
if (qtype == ATH9K_TX_QUEUE_UAPSD)
qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
else
qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
TXQ_FLAG_TXDESCINT_ENABLE;
qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
if (qnum == -1) {
return (NULL);
}
if (qnum >= ARRAY_SIZE(sc->sc_txq)) {
ARN_DBG((ARN_DBG_FATAL, "arn: arn_txq_setup(): "
"hal qnum %u out of range, max %u!\n",
qnum, (unsigned int)ARRAY_SIZE(sc->sc_txq)));
(void) ath9k_hw_releasetxqueue(ah, qnum);
return (NULL);
}
if (!ARN_TXQ_SETUP(sc, qnum)) {
struct ath_txq *txq = &sc->sc_txq[qnum];
txq->axq_qnum = qnum;
txq->axq_intrcnt = 0;
txq->axq_link = NULL;
list_create(&txq->axq_list, sizeof (struct ath_buf),
offsetof(struct ath_buf, bf_node));
list_create(&txq->axq_acq, sizeof (struct ath_buf),
offsetof(struct ath_buf, bf_node));
mutex_init(&txq->axq_lock, NULL, MUTEX_DRIVER, NULL);
txq->axq_depth = 0;
txq->axq_aggr_depth = 0;
txq->axq_totalqueued = 0;
txq->axq_linkbuf = NULL;
sc->sc_txqsetup |= 1<<qnum;
}
return (&sc->sc_txq[qnum]);
}
void
arn_tx_cleanupq(struct arn_softc *sc, struct ath_txq *txq)
{
(void) ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
}
int
arn_tx_setup(struct arn_softc *sc, int haltype)
{
struct ath_txq *txq;
if (haltype >= ARRAY_SIZE(sc->sc_haltype2q)) {
ARN_DBG((ARN_DBG_FATAL, "arn: arn_tx_setup(): "
"HAL AC %u out of range, max %zu!\n",
haltype, ARRAY_SIZE(sc->sc_haltype2q)));
return (0);
}
txq = arn_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
if (txq != NULL) {
sc->sc_haltype2q[haltype] = txq->axq_qnum;
return (1);
} else
return (0);
}
void
arn_tx_draintxq(struct arn_softc *sc, struct ath_txq *txq)
{
struct ath_buf *bf;
for (;;) {
mutex_enter(&txq->axq_lock);
bf = list_head(&txq->axq_list);
if (bf == NULL) {
txq->axq_link = NULL;
mutex_exit(&txq->axq_lock);
break;
}
list_remove(&txq->axq_list, bf);
mutex_exit(&txq->axq_lock);
bf->bf_in = NULL;
mutex_enter(&sc->sc_txbuflock);
list_insert_tail(&sc->sc_txbuf_list, bf);
mutex_exit(&sc->sc_txbuflock);
}
}
void
arn_draintxq(struct arn_softc *sc, boolean_t retry_tx)
{
if (!(sc->sc_flags & SC_OP_INVALID)) {
(void) ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_beaconq);
ARN_DBG((ARN_DBG_XMIT, "arn: arn_draintxq(): "
"beacon queue %x\n",
ath9k_hw_gettxbuf(sc->sc_ah, sc->sc_beaconq)));
}
arn_drain_txdataq(sc, retry_tx);
}
uint32_t
arn_txq_depth(struct arn_softc *sc, int qnum)
{
return (sc->sc_txq[qnum].axq_depth);
}
uint32_t
arn_txq_aggr_depth(struct arn_softc *sc, int qnum)
{
return (sc->sc_txq[qnum].axq_aggr_depth);
}
int
arn_txq_update(struct arn_softc *sc, int qnum,
struct ath9k_tx_queue_info *qinfo)
{
struct ath_hal *ah = sc->sc_ah;
int error = 0;
struct ath9k_tx_queue_info qi;
if (qnum == sc->sc_beaconq) {
sc->sc_beacon_qi = *qinfo;
return (0);
}
ASSERT(sc->sc_txq[qnum].axq_qnum == qnum);
(void) ath9k_hw_get_txq_props(ah, qnum, &qi);
qi.tqi_aifs = qinfo->tqi_aifs;
qi.tqi_cwmin = qinfo->tqi_cwmin;
qi.tqi_cwmax = qinfo->tqi_cwmax;
qi.tqi_burstTime = qinfo->tqi_burstTime;
qi.tqi_readyTime = qinfo->tqi_readyTime;
if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
ARN_DBG((ARN_DBG_FATAL,
"Unable to update hardware queue %u!\n", qnum));
error = -EIO;
} else {
(void) ath9k_hw_resettxqueue(ah, qnum);
}
return (error);
}
int
ath_cabq_update(struct arn_softc *sc)
{
struct ath9k_tx_queue_info qi;
int qnum = sc->sc_cabq->axq_qnum;
struct ath_beacon_config conf;
(void) ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
if (sc->sc_config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
sc->sc_config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
else if (sc->sc_config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
sc->sc_config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
arn_get_beaconconfig(sc, &conf);
qi.tqi_readyTime =
(conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
(void) arn_txq_update(sc, qnum, &qi);
return (0);
}
static uint32_t
arn_tx_get_keytype(const struct ieee80211_cipher *cip)
{
uint32_t index;
static const uint8_t ciphermap[] = {
ATH9K_CIPHER_WEP,
ATH9K_CIPHER_TKIP,
ATH9K_CIPHER_AES_OCB,
ATH9K_CIPHER_AES_CCM,
ATH9K_CIPHER_CKIP,
ATH9K_CIPHER_CLR,
};
ASSERT(cip->ic_cipher < ARRAY_SIZE(ciphermap));
index = cip->ic_cipher;
if (ciphermap[index] == ATH9K_CIPHER_WEP)
return (ATH9K_KEY_TYPE_WEP);
else if (ciphermap[index] == ATH9K_CIPHER_TKIP)
return (ATH9K_KEY_TYPE_TKIP);
else if (ciphermap[index] == ATH9K_CIPHER_AES_CCM)
return (ATH9K_KEY_TYPE_AES);
return (ATH9K_KEY_TYPE_CLEAR);
}
void
arn_dump_line(unsigned char *p, uint32_t len, boolean_t isaddress,
uint32_t group)
{
char *pnumeric = "0123456789ABCDEF";
char hex[((2 + 1) * 16) + 1];
char *phex = hex;
char ascii[16 + 1];
char *pascii = ascii;
uint32_t grouped = 0;
if (isaddress) {
arn_problem("arn: %08x: ", p);
} else {
arn_problem("arn: ");
}
while (len) {
*phex++ = pnumeric[((uint8_t)*p) / 16];
*phex++ = pnumeric[((uint8_t)*p) % 16];
if (++grouped >= group) {
*phex++ = ' ';
grouped = 0;
}
*pascii++ = (*p >= 32 && *p < 128) ? *p : '.';
++p;
--len;
}
*phex = '\0';
*pascii = '\0';
arn_problem("%-*s|%-*s|\n", (2 * 16) +
(16 / group), hex, 16, ascii);
}
void
arn_dump_pkg(unsigned char *p, uint32_t len, boolean_t isaddress,
uint32_t group)
{
uint32_t perline;
while (len) {
perline = (len < 16) ? len : 16;
arn_dump_line(p, perline, isaddress, group);
len -= perline;
p += perline;
}
}
static int32_t
arn_tx_start(struct arn_softc *sc, struct ieee80211_node *in,
struct ath_buf *bf, mblk_t *mp)
{
ieee80211com_t *ic = (ieee80211com_t *)sc;
struct ieee80211_frame *wh = (struct ieee80211_frame *)mp->b_rptr;
struct ath_hal *ah = sc->sc_ah;
struct ath_node *an;
struct ath_desc *ds;
struct ath_txq *txq;
struct ath_rate_table *rt;
enum ath9k_pkt_type atype;
boolean_t shortPreamble, is_padding = B_FALSE;
uint32_t subtype, keytype = ATH9K_KEY_TYPE_CLEAR;
int32_t keyix, iswep, hdrlen, pktlen, mblen, mbslen;
caddr_t dest;
pktlen = IEEE80211_CRC_LEN;
iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
keyix = ATH9K_TXKEYIX_INVALID;
hdrlen = ieee80211_hdrspace(ic, mp->b_rptr);
if (hdrlen == 28)
is_padding = B_TRUE;
if (iswep != 0) {
const struct ieee80211_cipher *cip;
struct ieee80211_key *k;
k = ieee80211_crypto_encap(ic, mp);
if (k == NULL) {
ARN_DBG((ARN_DBG_XMIT, "arn: arn_tx_start "
"crypto_encap failed\n"));
return (EIO);
}
cip = k->wk_cipher;
keytype = arn_tx_get_keytype(cip);
hdrlen += cip->ic_header;
pktlen += cip->ic_trailer;
if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0)
pktlen += cip->ic_miclen;
keyix = k->wk_keyix;
wh = (struct ieee80211_frame *)mp->b_rptr;
}
dest = bf->bf_dma.mem_va;
for (; mp != NULL; mp = mp->b_cont) {
mblen = MBLKL(mp);
bcopy(mp->b_rptr, dest, mblen);
dest += mblen;
}
mbslen = (uintptr_t)dest - (uintptr_t)bf->bf_dma.mem_va;
pktlen += mbslen;
if (is_padding && (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
IEEE80211_FC0_TYPE_DATA)
pktlen -= 2;
ath_tx_setup_buffer(sc, bf, in, wh, pktlen, keytype);
ds = bf->bf_desc;
rt = sc->sc_currates;
ASSERT(rt != NULL);
arn_get_rate(sc, bf, wh);
an = (struct ath_node *)(in);
switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
case IEEE80211_FC0_TYPE_MGT:
subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
atype = ATH9K_PKT_TYPE_BEACON;
else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
atype = ATH9K_PKT_TYPE_PROBE_RESP;
else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
atype = ATH9K_PKT_TYPE_ATIM;
else
atype = ATH9K_PKT_TYPE_NORMAL;
txq = &sc->sc_txq[arn_get_hal_qnum(WME_AC_VO, sc)];
break;
case IEEE80211_FC0_TYPE_CTL:
atype = ATH9K_PKT_TYPE_PSPOLL;
subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
txq = &sc->sc_txq[arn_get_hal_qnum(WME_AC_VO, sc)];
break;
case IEEE80211_FC0_TYPE_DATA:
atype = ATH9K_PKT_TYPE_NORMAL;
txq = &sc->sc_txq[arn_get_hal_qnum(WME_AC_BE, sc)];
break;
default:
sc->sc_stats.ast_tx_invalid++;
return (1);
}
ds->ds_link = 0;
ds->ds_data = bf->bf_dma.cookie.dmac_address;
ath9k_hw_set11n_txdesc(ah, ds,
(pktlen),
atype,
MAX_RATE_POWER ,
keyix ,
keytype ,
bf->bf_flags );
ARN_DBG((ARN_DBG_XMIT, "arn: arn_tx_start(): to %s totlen=%d "
"an->an_tx_rate1sp=%d tx_rate2sp=%d tx_rate3sp=%d "
"qnum=%d sht=%d dur = %d\n",
ieee80211_macaddr_sprintf(wh->i_addr1), mbslen, an->an_tx_rate1sp,
an->an_tx_rate2sp, an->an_tx_rate3sp,
txq->axq_qnum, shortPreamble, *(uint16_t *)wh->i_dur));
(void) ath9k_hw_filltxdesc(ah, ds,
mbslen,
B_TRUE,
B_TRUE,
ds);
ath_buf_set_rate(sc, bf, wh);
ARN_DMA_SYNC(bf->bf_dma, DDI_DMA_SYNC_FORDEV);
mutex_enter(&txq->axq_lock);
list_insert_tail(&txq->axq_list, bf);
if (txq->axq_link == NULL) {
(void) ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
} else {
*txq->axq_link = bf->bf_daddr;
}
txq->axq_link = &ds->ds_link;
mutex_exit(&txq->axq_lock);
(void) ath9k_hw_txstart(ah, txq->axq_qnum);
ic->ic_stats.is_tx_frags++;
ic->ic_stats.is_tx_bytes += pktlen;
return (0);
}
int
arn_tx(ieee80211com_t *ic, mblk_t *mp, uint8_t type)
{
struct arn_softc *sc = (struct arn_softc *)ic;
struct ath_hal *ah = sc->sc_ah;
struct ieee80211_node *in = NULL;
struct ath_buf *bf = NULL;
struct ieee80211_frame *wh;
int error = 0;
ASSERT(mp->b_next == NULL);
if (sc->sc_flags & SC_OP_INVALID) {
if ((type & IEEE80211_FC0_TYPE_MASK) !=
IEEE80211_FC0_TYPE_DATA) {
freemsg(mp);
}
return (ENXIO);
}
bf = arn_tx_get_buffer(sc);
if (bf == NULL) {
ARN_DBG((ARN_DBG_XMIT, "arn: arn_tx(): discard, "
"no xmit buf\n"));
ic->ic_stats.is_tx_nobuf++;
if ((type & IEEE80211_FC0_TYPE_MASK) ==
IEEE80211_FC0_TYPE_DATA) {
sc->sc_stats.ast_tx_nobuf++;
mutex_enter(&sc->sc_resched_lock);
sc->sc_resched_needed = B_TRUE;
mutex_exit(&sc->sc_resched_lock);
} else {
sc->sc_stats.ast_tx_nobufmgt++;
freemsg(mp);
}
return (ENOMEM);
}
wh = (struct ieee80211_frame *)mp->b_rptr;
in = ieee80211_find_txnode(ic, wh->i_addr1);
if (in == NULL) {
error = EIO;
goto bad;
}
in->in_inact = 0;
switch (type & IEEE80211_FC0_TYPE_MASK) {
case IEEE80211_FC0_TYPE_DATA:
(void) ieee80211_encap(ic, mp, in);
break;
default:
if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
uint64_t tsf;
uint32_t *tstamp;
tsf = ath9k_hw_gettsf64(ah);
tsf += 100;
tstamp = (uint32_t *)&wh[1];
tstamp[0] = LE_32(tsf & 0xffffffff);
tstamp[1] = LE_32(tsf >> 32);
}
sc->sc_stats.ast_tx_mgmt++;
break;
}
error = arn_tx_start(sc, in, bf, mp);
if (error != 0) {
bad:
ic->ic_stats.is_tx_failed++;
if (bf != NULL) {
mutex_enter(&sc->sc_txbuflock);
list_insert_tail(&sc->sc_txbuf_list, bf);
mutex_exit(&sc->sc_txbuflock);
}
}
if (in != NULL)
ieee80211_free_node(in);
if ((type & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_DATA ||
error == 0) {
freemsg(mp);
}
return (error);
}
static void
arn_printtxbuf(struct ath_buf *bf, int done)
{
struct ath_desc *ds = bf->bf_desc;
const struct ath_tx_status *ts = &ds->ds_txstat;
ARN_DBG((ARN_DBG_XMIT, "arn: T(%p %p) %08x %08x %08x %08x %08x"
" %08x %08x %08x %c\n",
ds, bf->bf_daddr,
ds->ds_link, ds->ds_data,
ds->ds_ctl0, ds->ds_ctl1,
ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
!done ? ' ' : (ts->ts_status == 0) ? '*' : '!'));
}
static void
ath_tx_rc_status(struct ath_buf *bf,
struct ath_desc *ds,
int nbad,
int txok,
boolean_t update_rc)
{
struct ath_tx_info_priv *tx_info_priv =
(struct ath_tx_info_priv *)&bf->tx_info_priv;
tx_info_priv->update_rc = B_FALSE;
if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
(bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
if (bf_isdata(bf)) {
(void) memcpy(&tx_info_priv->tx, &ds->ds_txstat,
sizeof (tx_info_priv->tx));
tx_info_priv->n_frames = bf->bf_nframes;
tx_info_priv->n_bad_frames = nbad;
tx_info_priv->update_rc = B_TRUE;
}
}
}
static int
arn_tx_processq(struct arn_softc *sc, struct ath_txq *txq)
{
ieee80211com_t *ic = (ieee80211com_t *)sc;
struct ath_hal *ah = sc->sc_ah;
struct ath_buf *bf;
struct ath_desc *ds;
struct ieee80211_node *in;
struct ath_tx_status *ts;
struct ath_node *an;
int32_t sr, lr, nacked = 0;
int txok, nbad = 0;
int status;
for (;;) {
mutex_enter(&txq->axq_lock);
bf = list_head(&txq->axq_list);
if (bf == NULL) {
txq->axq_link = NULL;
mutex_exit(&txq->axq_lock);
break;
}
ds = bf->bf_desc;
ts = &ds->ds_txstat;
status = ath9k_hw_txprocdesc(ah, ds);
#ifdef DEBUG
arn_printtxbuf(bf, status == 0);
#endif
if (status == EINPROGRESS) {
mutex_exit(&txq->axq_lock);
break;
}
list_remove(&txq->axq_list, bf);
mutex_exit(&txq->axq_lock);
in = bf->bf_in;
if (in != NULL) {
an = ATH_NODE(in);
if (ts->ts_status == 0) {
an->an_tx_ok++;
an->an_tx_antenna = ts->ts_antenna;
sc->sc_stats.ast_tx_rssidelta =
ts->ts_rssi - sc->sc_stats.ast_tx_rssi;
sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
} else {
an->an_tx_err++;
if (ts->ts_status & ATH9K_TXERR_XRETRY) {
sc->sc_stats.ast_tx_xretries++;
}
if (ts->ts_status & ATH9K_TXERR_FIFO) {
sc->sc_stats.ast_tx_fifoerr++;
}
if (ts->ts_status & ATH9K_TXERR_FILT) {
sc->sc_stats.ast_tx_filtered++;
}
an->an_tx_antenna = 0;
}
sr = ts->ts_shortretry;
lr = ts->ts_longretry;
sc->sc_stats.ast_tx_shortretry += sr;
sc->sc_stats.ast_tx_longretry += lr;
if ((ts->ts_status & ATH9K_TXERR_FILT) == 0 &&
(bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
if (ts->ts_status == 0) {
nacked++;
an->an_tx_ok++;
} else {
an->an_tx_err++;
}
an->an_tx_retr += sr + lr;
}
}
txok = (ds->ds_txstat.ts_status == 0);
if (!bf_isampdu(bf)) {
bf->bf_retries = ds->ds_txstat.ts_longretry;
if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
bf->bf_state.bf_type |= BUF_XRETRY;
nbad = 0;
}
ath_tx_rc_status(bf, ds, nbad, B_TRUE, txok);
ath_tx_complete_buf(sc, bf, txok, 0);
bf->bf_in = NULL;
mutex_enter(&sc->sc_txbuflock);
list_insert_tail(&sc->sc_txbuf_list, bf);
mutex_exit(&sc->sc_txbuflock);
mutex_enter(&sc->sc_resched_lock);
if (sc->sc_resched_needed) {
sc->sc_resched_needed = B_FALSE;
mac_tx_update(ic->ic_mach);
}
mutex_exit(&sc->sc_resched_lock);
}
return (nacked);
}
static void
arn_tx_handler(struct arn_softc *sc)
{
int i;
int nacked = 0;
uint32_t qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
if (ARN_TXQ_SETUP(sc, i) && (qcumask & (1 << i))) {
nacked += arn_tx_processq(sc, &sc->sc_txq[i]);
}
}
if (nacked)
sc->sc_lastrx = ath9k_hw_gettsf64(sc->sc_ah);
}
void
arn_tx_int_proc(void *arg)
{
struct arn_softc *sc = arg;
arn_tx_handler(sc);
}
#ifdef ARN_TX_AGGREGATION
void
arn_tx_node_init(struct arn_softc *sc, struct ath_node *an)
{
struct ath_atx_tid *tid;
struct ath_atx_ac *ac;
int tidno, acno;
for (tidno = 0, tid = &an->tid[tidno]; tidno < WME_NUM_TID;
tidno++, tid++) {
tid->an = an;
tid->tidno = tidno;
tid->seq_start = tid->seq_next = 0;
tid->baw_size = WME_MAX_BA;
tid->baw_head = tid->baw_tail = 0;
tid->sched = B_FALSE;
tid->paused = B_FALSE;
tid->state &= ~AGGR_CLEANUP;
list_create(&tid->buf_q, sizeof (struct ath_buf),
offsetof(struct ath_buf, bf_node));
acno = TID_TO_WME_AC(tidno);
tid->ac = &an->ac[acno];
tid->state &= ~AGGR_ADDBA_COMPLETE;
tid->state &= ~AGGR_ADDBA_PROGRESS;
tid->addba_exchangeattempts = 0;
}
for (acno = 0, ac = &an->ac[acno]; acno < WME_NUM_AC; acno++, ac++) {
ac->sched = B_FALSE;
list_create(&ac->tid_q, sizeof (struct ath_atx_tid),
offsetof(struct ath_atx_tid, list));
switch (acno) {
case WME_AC_BE:
ac->qnum = arn_tx_get_qnum(sc,
ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
break;
case WME_AC_BK:
ac->qnum = arn_tx_get_qnum(sc,
ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
break;
case WME_AC_VI:
ac->qnum = arn_tx_get_qnum(sc,
ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
break;
case WME_AC_VO:
ac->qnum = arn_tx_get_qnum(sc,
ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
break;
}
}
}
void
arn_tx_node_cleanup(struct arn_softc *sc, struct ieee80211_node *in)
{
int i;
struct ath_atx_ac *ac, *ac_tmp;
struct ath_atx_tid *tid, *tid_tmp;
struct ath_txq *txq;
struct ath_node *an = ATH_NODE(in);
for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
if (ARN_TXQ_SETUP(sc, i)) {
txq = &sc->sc_txq[i];
mutex_enter(&txq->axq_lock);
list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq) {
tid = list_head(&ac->tid_q);
if (tid && tid->an != an)
continue;
list_remove(&txq->axq_acq, ac);
ac->sched = B_FALSE;
list_for_each_entry_safe(tid, tid_tmp,
&ac->tid_q) {
list_remove(&ac->tid_q, tid);
bf = list_head(&tid->buf_q);
while (bf != NULL) {
if (bf->bf_in == in)
bf->bf_in = NULL;
}
bf = list_next(&txq->axq_list, bf);
tid->sched = B_FALSE;
arn_tid_drain(sc, txq, tid);
tid->state &= ~AGGR_ADDBA_COMPLETE;
tid->addba_exchangeattempts = 0;
tid->state &= ~AGGR_CLEANUP;
}
}
mutex_exit(&txq->axq_lock);
}
}
}
#endif