Path: blob/main/sys/contrib/dev/broadcom/brcm80211/brcmsmac/ampdu.c
178665 views
/*1* Copyright (c) 2010 Broadcom Corporation2*3* Permission to use, copy, modify, and/or distribute this software for any4* purpose with or without fee is hereby granted, provided that the above5* copyright notice and this permission notice appear in all copies.6*7* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES8* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF9* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY10* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES11* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION12* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN13* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.14*/15#include <net/mac80211.h>1617#include "rate.h"18#include "scb.h"19#include "phy/phy_hal.h"20#include "antsel.h"21#include "main.h"22#include "ampdu.h"23#include "debug.h"24#include "brcms_trace_events.h"2526/* max number of mpdus in an ampdu */27#define AMPDU_MAX_MPDU 3228/* max number of mpdus in an ampdu to a legacy */29#define AMPDU_NUM_MPDU_LEGACY 1630/* max Tx ba window size (in pdu) */31#define AMPDU_TX_BA_MAX_WSIZE 6432/* default Tx ba window size (in pdu) */33#define AMPDU_TX_BA_DEF_WSIZE 6434/* default Rx ba window size (in pdu) */35#define AMPDU_RX_BA_DEF_WSIZE 6436/* max Rx ba window size (in pdu) */37#define AMPDU_RX_BA_MAX_WSIZE 6438/* max dur of tx ampdu (in msec) */39#define AMPDU_MAX_DUR 540/* default tx retry limit */41#define AMPDU_DEF_RETRY_LIMIT 542/* default tx retry limit at reg rate */43#define AMPDU_DEF_RR_RETRY_LIMIT 244/* default ffpld reserved bytes */45#define AMPDU_DEF_FFPLD_RSVD 204846/* # of inis to be freed on detach */47#define AMPDU_INI_FREE 1048/* max # of mpdus released at a time */49#define AMPDU_SCB_MAX_RELEASE 205051#define NUM_FFPLD_FIFO 4 /* number of fifo concerned by pre-loading */52#define FFPLD_TX_MAX_UNFL 200 /* default value of the average number of ampdu53* without underflows54*/55#define FFPLD_MPDU_SIZE 1800 /* estimate of maximum mpdu size */56#define FFPLD_MAX_MCS 23 /* we don't deal with mcs 32 */57#define FFPLD_PLD_INCR 1000 /* increments in bytes */58#define FFPLD_MAX_AMPDU_CNT 5000 /* maximum number of ampdu we59* accumulate between resets.60*/6162#define AMPDU_DELIMITER_LEN 46364/* max allowed number of mpdus in an ampdu (2 streams) */65#define AMPDU_NUM_MPDU 166667#define TX_SEQ_TO_INDEX(seq) ((seq) % AMPDU_TX_BA_MAX_WSIZE)6869/* max possible overhead per mpdu in the ampdu; 3 is for roundup if needed */70#define AMPDU_MAX_MPDU_OVERHEAD (FCS_LEN + DOT11_ICV_AES_LEN +\71AMPDU_DELIMITER_LEN + 3\72+ DOT11_A4_HDR_LEN + DOT11_QOS_LEN + DOT11_IV_MAX_LEN)7374/* modulo add/sub, bound = 2^k */75#define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))76#define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))7778/* structure to hold tx fifo information and pre-loading state79* counters specific to tx underflows of ampdus80* some counters might be redundant with the ones in wlc or ampdu structures.81* This allows to maintain a specific state independently of82* how often and/or when the wlc counters are updated.83*84* ampdu_pld_size: number of bytes to be pre-loaded85* mcs2ampdu_table: per-mcs max # of mpdus in an ampdu86* prev_txfunfl: num of underflows last read from the HW macstats counter87* accum_txfunfl: num of underflows since we modified pld params88* accum_txampdu: num of tx ampdu since we modified pld params89* prev_txampdu: previous reading of tx ampdu90* dmaxferrate: estimated dma avg xfer rate in kbits/sec91*/92struct brcms_fifo_info {93u16 ampdu_pld_size;94u8 mcs2ampdu_table[FFPLD_MAX_MCS + 1];95u16 prev_txfunfl;96u32 accum_txfunfl;97u32 accum_txampdu;98u32 prev_txampdu;99u32 dmaxferrate;100};101102/* AMPDU module specific state103*104* wlc: pointer to main wlc structure105* scb_handle: scb cubby handle to retrieve data from scb106* ini_enable: per-tid initiator enable/disable of ampdu107* ba_tx_wsize: Tx ba window size (in pdu)108* ba_rx_wsize: Rx ba window size (in pdu)109* retry_limit: mpdu transmit retry limit110* rr_retry_limit: mpdu transmit retry limit at regular rate111* retry_limit_tid: per-tid mpdu transmit retry limit112* rr_retry_limit_tid: per-tid mpdu transmit retry limit at regular rate113* mpdu_density: min mpdu spacing (0-7) ==> 2^(x-1)/8 usec114* max_pdu: max pdus allowed in ampdu115* dur: max duration of an ampdu (in msec)116* rx_factor: maximum rx ampdu factor (0-3) ==> 2^(13+x) bytes117* ffpld_rsvd: number of bytes to reserve for preload118* max_txlen: max size of ampdu per mcs, bw and sgi119* mfbr: enable multiple fallback rate120* tx_max_funl: underflows should be kept such that121* (tx_max_funfl*underflows) < tx frames122* fifo_tb: table of fifo infos123*/124struct ampdu_info {125struct brcms_c_info *wlc;126int scb_handle;127u8 ini_enable[AMPDU_MAX_SCB_TID];128u8 ba_tx_wsize;129u8 ba_rx_wsize;130u8 retry_limit;131u8 rr_retry_limit;132u8 retry_limit_tid[AMPDU_MAX_SCB_TID];133u8 rr_retry_limit_tid[AMPDU_MAX_SCB_TID];134u8 mpdu_density;135s8 max_pdu;136u8 dur;137u8 rx_factor;138u32 ffpld_rsvd;139u32 max_txlen[MCS_TABLE_SIZE][2][2];140bool mfbr;141u32 tx_max_funl;142struct brcms_fifo_info fifo_tb[NUM_FFPLD_FIFO];143};144145static void brcms_c_scb_ampdu_update_max_txlen(struct ampdu_info *ampdu, u8 dur)146{147u32 rate, mcs;148149for (mcs = 0; mcs < MCS_TABLE_SIZE; mcs++) {150/* rate is in Kbps; dur is in msec ==> len = (rate * dur) / 8 */151/* 20MHz, No SGI */152rate = mcs_2_rate(mcs, false, false);153ampdu->max_txlen[mcs][0][0] = (rate * dur) >> 3;154/* 40 MHz, No SGI */155rate = mcs_2_rate(mcs, true, false);156ampdu->max_txlen[mcs][1][0] = (rate * dur) >> 3;157/* 20MHz, SGI */158rate = mcs_2_rate(mcs, false, true);159ampdu->max_txlen[mcs][0][1] = (rate * dur) >> 3;160/* 40 MHz, SGI */161rate = mcs_2_rate(mcs, true, true);162ampdu->max_txlen[mcs][1][1] = (rate * dur) >> 3;163}164}165166static bool brcms_c_ampdu_cap(struct ampdu_info *ampdu)167{168if (BRCMS_PHY_11N_CAP(ampdu->wlc->band))169return true;170else171return false;172}173174static int brcms_c_ampdu_set(struct ampdu_info *ampdu, bool on)175{176struct brcms_c_info *wlc = ampdu->wlc;177struct bcma_device *core = wlc->hw->d11core;178179wlc->pub->_ampdu = false;180181if (on) {182if (!(wlc->pub->_n_enab & SUPPORT_11N)) {183brcms_err(core, "wl%d: driver not nmode enabled\n",184wlc->pub->unit);185return -ENOTSUPP;186}187if (!brcms_c_ampdu_cap(ampdu)) {188brcms_err(core, "wl%d: device not ampdu capable\n",189wlc->pub->unit);190return -ENOTSUPP;191}192wlc->pub->_ampdu = on;193}194195return 0;196}197198static void brcms_c_ffpld_init(struct ampdu_info *ampdu)199{200int i, j;201struct brcms_fifo_info *fifo;202203for (j = 0; j < NUM_FFPLD_FIFO; j++) {204fifo = (ampdu->fifo_tb + j);205fifo->ampdu_pld_size = 0;206for (i = 0; i <= FFPLD_MAX_MCS; i++)207fifo->mcs2ampdu_table[i] = 255;208fifo->dmaxferrate = 0;209fifo->accum_txampdu = 0;210fifo->prev_txfunfl = 0;211fifo->accum_txfunfl = 0;212213}214}215216struct ampdu_info *brcms_c_ampdu_attach(struct brcms_c_info *wlc)217{218struct ampdu_info *ampdu;219int i;220221ampdu = kzalloc(sizeof(*ampdu), GFP_ATOMIC);222if (!ampdu)223return NULL;224225ampdu->wlc = wlc;226227for (i = 0; i < AMPDU_MAX_SCB_TID; i++)228ampdu->ini_enable[i] = true;229/* Disable ampdu for VO by default */230ampdu->ini_enable[PRIO_8021D_VO] = false;231ampdu->ini_enable[PRIO_8021D_NC] = false;232233/* Disable ampdu for BK by default since not enough fifo space */234ampdu->ini_enable[PRIO_8021D_NONE] = false;235ampdu->ini_enable[PRIO_8021D_BK] = false;236237ampdu->ba_tx_wsize = AMPDU_TX_BA_DEF_WSIZE;238ampdu->ba_rx_wsize = AMPDU_RX_BA_DEF_WSIZE;239ampdu->mpdu_density = AMPDU_DEF_MPDU_DENSITY;240ampdu->max_pdu = AUTO;241ampdu->dur = AMPDU_MAX_DUR;242243ampdu->ffpld_rsvd = AMPDU_DEF_FFPLD_RSVD;244/*245* bump max ampdu rcv size to 64k for all 11n246* devices except 4321A0 and 4321A1247*/248if (BRCMS_ISNPHY(wlc->band) && NREV_LT(wlc->band->phyrev, 2))249ampdu->rx_factor = IEEE80211_HT_MAX_AMPDU_32K;250else251ampdu->rx_factor = IEEE80211_HT_MAX_AMPDU_64K;252ampdu->retry_limit = AMPDU_DEF_RETRY_LIMIT;253ampdu->rr_retry_limit = AMPDU_DEF_RR_RETRY_LIMIT;254255for (i = 0; i < AMPDU_MAX_SCB_TID; i++) {256ampdu->retry_limit_tid[i] = ampdu->retry_limit;257ampdu->rr_retry_limit_tid[i] = ampdu->rr_retry_limit;258}259260brcms_c_scb_ampdu_update_max_txlen(ampdu, ampdu->dur);261ampdu->mfbr = false;262/* try to set ampdu to the default value */263brcms_c_ampdu_set(ampdu, wlc->pub->_ampdu);264265ampdu->tx_max_funl = FFPLD_TX_MAX_UNFL;266brcms_c_ffpld_init(ampdu);267268return ampdu;269}270271void brcms_c_ampdu_detach(struct ampdu_info *ampdu)272{273kfree(ampdu);274}275276static void brcms_c_scb_ampdu_update_config(struct ampdu_info *ampdu,277struct scb *scb)278{279struct scb_ampdu *scb_ampdu = &scb->scb_ampdu;280int i;281282scb_ampdu->max_pdu = AMPDU_NUM_MPDU;283284/* go back to legacy size if some preloading is occurring */285for (i = 0; i < NUM_FFPLD_FIFO; i++) {286if (ampdu->fifo_tb[i].ampdu_pld_size > FFPLD_PLD_INCR)287scb_ampdu->max_pdu = AMPDU_NUM_MPDU_LEGACY;288}289290/* apply user override */291if (ampdu->max_pdu != AUTO)292scb_ampdu->max_pdu = (u8) ampdu->max_pdu;293294scb_ampdu->release = min_t(u8, scb_ampdu->max_pdu,295AMPDU_SCB_MAX_RELEASE);296297if (scb_ampdu->max_rx_ampdu_bytes)298scb_ampdu->release = min_t(u8, scb_ampdu->release,299scb_ampdu->max_rx_ampdu_bytes / 1600);300301scb_ampdu->release = min(scb_ampdu->release,302ampdu->fifo_tb[TX_AC_BE_FIFO].303mcs2ampdu_table[FFPLD_MAX_MCS]);304}305306static void brcms_c_scb_ampdu_update_config_all(struct ampdu_info *ampdu)307{308brcms_c_scb_ampdu_update_config(ampdu, &du->wlc->pri_scb);309}310311static void brcms_c_ffpld_calc_mcs2ampdu_table(struct ampdu_info *ampdu, int f)312{313int i;314u32 phy_rate, dma_rate, tmp;315u8 max_mpdu;316struct brcms_fifo_info *fifo = (ampdu->fifo_tb + f);317318/* recompute the dma rate */319/* note : we divide/multiply by 100 to avoid integer overflows */320max_mpdu = min_t(u8, fifo->mcs2ampdu_table[FFPLD_MAX_MCS],321AMPDU_NUM_MPDU_LEGACY);322phy_rate = mcs_2_rate(FFPLD_MAX_MCS, true, false);323dma_rate =324(((phy_rate / 100) *325(max_mpdu * FFPLD_MPDU_SIZE - fifo->ampdu_pld_size))326/ (max_mpdu * FFPLD_MPDU_SIZE)) * 100;327fifo->dmaxferrate = dma_rate;328329/* fill up the mcs2ampdu table; do not recalc the last mcs */330dma_rate = dma_rate >> 7;331for (i = 0; i < FFPLD_MAX_MCS; i++) {332/* shifting to keep it within integer range */333phy_rate = mcs_2_rate(i, true, false) >> 7;334if (phy_rate > dma_rate) {335tmp = ((fifo->ampdu_pld_size * phy_rate) /336((phy_rate - dma_rate) * FFPLD_MPDU_SIZE)) + 1;337tmp = min_t(u32, tmp, 255);338fifo->mcs2ampdu_table[i] = (u8) tmp;339}340}341}342343/* evaluate the dma transfer rate using the tx underflows as feedback.344* If necessary, increase tx fifo preloading. If not enough,345* decrease maximum ampdu size for each mcs till underflows stop346* Return 1 if pre-loading not active, -1 if not an underflow event,347* 0 if pre-loading module took care of the event.348*/349static int brcms_c_ffpld_check_txfunfl(struct brcms_c_info *wlc, int fid)350{351struct ampdu_info *ampdu = wlc->ampdu;352u32 phy_rate = mcs_2_rate(FFPLD_MAX_MCS, true, false);353u8 max_mpdu;354u16 max_pld_size;355u32 new_txunfl;356struct brcms_fifo_info *fifo = (ampdu->fifo_tb + fid);357uint xmtfifo_sz;358u16 cur_txunfl;359360/* return if we got here for a different reason than underflows */361cur_txunfl = brcms_b_read_shm(wlc->hw,362M_UCODE_MACSTAT +363offsetof(struct macstat, txfunfl[fid]));364new_txunfl = (u16) (cur_txunfl - fifo->prev_txfunfl);365if (new_txunfl == 0) {366brcms_dbg_ht(wlc->hw->d11core,367"TX status FRAG set but no tx underflows\n");368return -1;369}370fifo->prev_txfunfl = cur_txunfl;371372if (!ampdu->tx_max_funl)373return 1;374375/* check if fifo is big enough */376if (brcms_b_xmtfifo_sz_get(wlc->hw, fid, &xmtfifo_sz))377return -1;378379if ((TXFIFO_SIZE_UNIT * (u32) xmtfifo_sz) <= ampdu->ffpld_rsvd)380return 1;381382max_pld_size = TXFIFO_SIZE_UNIT * xmtfifo_sz - ampdu->ffpld_rsvd;383fifo->accum_txfunfl += new_txunfl;384385/* we need to wait for at least 10 underflows */386if (fifo->accum_txfunfl < 10)387return 0;388389brcms_dbg_ht(wlc->hw->d11core, "tx_underflows %d\n", fifo->accum_txfunfl);390391max_mpdu = min_t(u8, fifo->mcs2ampdu_table[FFPLD_MAX_MCS],392AMPDU_NUM_MPDU_LEGACY);393394/* In case max value max_pdu is already lower than395the fifo depth, there is nothing more we can do.396*/397398if (fifo->ampdu_pld_size >= max_mpdu * FFPLD_MPDU_SIZE) {399fifo->accum_txfunfl = 0;400return 0;401}402403if (fifo->ampdu_pld_size < max_pld_size) {404405/* increment by TX_FIFO_PLD_INC bytes */406fifo->ampdu_pld_size += FFPLD_PLD_INCR;407if (fifo->ampdu_pld_size > max_pld_size)408fifo->ampdu_pld_size = max_pld_size;409410/* update scb release size */411brcms_c_scb_ampdu_update_config_all(ampdu);412413/*414* compute a new dma xfer rate for max_mpdu @ max mcs.415* This is the minimum dma rate that can achieve no416* underflow condition for the current mpdu size.417*418* note : we divide/multiply by 100 to avoid integer overflows419*/420fifo->dmaxferrate =421(((phy_rate / 100) *422(max_mpdu * FFPLD_MPDU_SIZE - fifo->ampdu_pld_size))423/ (max_mpdu * FFPLD_MPDU_SIZE)) * 100;424425brcms_dbg_ht(wlc->hw->d11core,426"DMA estimated transfer rate %d; "427"pre-load size %d\n",428fifo->dmaxferrate, fifo->ampdu_pld_size);429} else {430431/* decrease ampdu size */432if (fifo->mcs2ampdu_table[FFPLD_MAX_MCS] > 1) {433if (fifo->mcs2ampdu_table[FFPLD_MAX_MCS] == 255)434fifo->mcs2ampdu_table[FFPLD_MAX_MCS] =435AMPDU_NUM_MPDU_LEGACY - 1;436else437fifo->mcs2ampdu_table[FFPLD_MAX_MCS] -= 1;438439/* recompute the table */440brcms_c_ffpld_calc_mcs2ampdu_table(ampdu, fid);441442/* update scb release size */443brcms_c_scb_ampdu_update_config_all(ampdu);444}445}446fifo->accum_txfunfl = 0;447return 0;448}449450void451brcms_c_ampdu_tx_operational(struct brcms_c_info *wlc, u8 tid,452uint max_rx_ampdu_bytes) /* from ht_cap in beacon */453{454struct scb_ampdu *scb_ampdu;455struct ampdu_info *ampdu = wlc->ampdu;456struct scb *scb = &wlc->pri_scb;457scb_ampdu = &scb->scb_ampdu;458459if (!ampdu->ini_enable[tid]) {460brcms_err(wlc->hw->d11core, "%s: Rejecting tid %d\n",461__func__, tid);462return;463}464465scb_ampdu->max_rx_ampdu_bytes = max_rx_ampdu_bytes;466}467468void brcms_c_ampdu_reset_session(struct brcms_ampdu_session *session,469struct brcms_c_info *wlc)470{471session->wlc = wlc;472skb_queue_head_init(&session->skb_list);473session->max_ampdu_len = 0; /* determined from first MPDU */474session->max_ampdu_frames = 0; /* determined from first MPDU */475session->ampdu_len = 0;476session->dma_len = 0;477}478479/*480* Preps the given packet for AMPDU based on the session data. If the481* frame cannot be accommodated in the current session, -ENOSPC is482* returned.483*/484int brcms_c_ampdu_add_frame(struct brcms_ampdu_session *session,485struct sk_buff *p)486{487struct brcms_c_info *wlc = session->wlc;488struct ampdu_info *ampdu = wlc->ampdu;489struct scb *scb = &wlc->pri_scb;490struct scb_ampdu *scb_ampdu = &scb->scb_ampdu;491struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(p);492struct ieee80211_tx_rate *txrate = tx_info->status.rates;493struct d11txh *txh = (struct d11txh *)p->data;494unsigned ampdu_frames;495u8 ndelim, tid;496u8 *plcp;497uint len;498u16 mcl;499bool fbr_iscck;500bool rr;501502ndelim = txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM];503plcp = (u8 *)(txh + 1);504fbr_iscck = !(le16_to_cpu(txh->XtraFrameTypes) & 0x03);505len = fbr_iscck ? BRCMS_GET_CCK_PLCP_LEN(txh->FragPLCPFallback) :506BRCMS_GET_MIMO_PLCP_LEN(txh->FragPLCPFallback);507len = roundup(len, 4) + (ndelim + 1) * AMPDU_DELIMITER_LEN;508509ampdu_frames = skb_queue_len(&session->skb_list);510if (ampdu_frames != 0) {511struct sk_buff *first;512513if (ampdu_frames + 1 > session->max_ampdu_frames ||514session->ampdu_len + len > session->max_ampdu_len)515return -ENOSPC;516517/*518* We aren't really out of space if the new frame is of519* a different priority, but we want the same behaviour520* so return -ENOSPC anyway.521*522* XXX: The old AMPDU code did this, but is it really523* necessary?524*/525first = skb_peek(&session->skb_list);526if (p->priority != first->priority)527return -ENOSPC;528}529530/*531* Now that we're sure this frame can be accommodated, update the532* session information.533*/534session->ampdu_len += len;535session->dma_len += p->len;536537tid = (u8)p->priority;538539/* Handle retry limits */540if (txrate[0].count <= ampdu->rr_retry_limit_tid[tid]) {541txrate[0].count++;542rr = true;543} else {544txrate[1].count++;545rr = false;546}547548if (ampdu_frames == 0) {549u8 plcp0, plcp3, is40, sgi, mcs;550uint fifo = le16_to_cpu(txh->TxFrameID) & TXFID_QUEUE_MASK;551struct brcms_fifo_info *f = &du->fifo_tb[fifo];552553if (rr) {554plcp0 = plcp[0];555plcp3 = plcp[3];556} else {557plcp0 = txh->FragPLCPFallback[0];558plcp3 = txh->FragPLCPFallback[3];559560}561562/* Limit AMPDU size based on MCS */563is40 = (plcp0 & MIMO_PLCP_40MHZ) ? 1 : 0;564sgi = plcp3_issgi(plcp3) ? 1 : 0;565mcs = plcp0 & ~MIMO_PLCP_40MHZ;566session->max_ampdu_len = min(scb_ampdu->max_rx_ampdu_bytes,567ampdu->max_txlen[mcs][is40][sgi]);568569session->max_ampdu_frames = scb_ampdu->max_pdu;570if (mcs_2_rate(mcs, true, false) >= f->dmaxferrate) {571session->max_ampdu_frames =572min_t(u16, f->mcs2ampdu_table[mcs],573session->max_ampdu_frames);574}575}576577/*578* Treat all frames as "middle" frames of AMPDU here. First and579* last frames must be fixed up after all MPDUs have been prepped.580*/581mcl = le16_to_cpu(txh->MacTxControlLow);582mcl &= ~TXC_AMPDU_MASK;583mcl |= (TXC_AMPDU_MIDDLE << TXC_AMPDU_SHIFT);584mcl &= ~(TXC_STARTMSDU | TXC_SENDRTS | TXC_SENDCTS);585txh->MacTxControlLow = cpu_to_le16(mcl);586txh->PreloadSize = 0; /* always default to 0 */587588skb_queue_tail(&session->skb_list, p);589590return 0;591}592593void brcms_c_ampdu_finalize(struct brcms_ampdu_session *session)594{595struct brcms_c_info *wlc = session->wlc;596struct ampdu_info *ampdu = wlc->ampdu;597struct sk_buff *first, *last;598struct d11txh *txh;599struct ieee80211_tx_info *tx_info;600struct ieee80211_tx_rate *txrate;601u8 ndelim;602u8 *plcp;603uint len;604uint fifo;605struct brcms_fifo_info *f;606u16 mcl;607bool fbr;608bool fbr_iscck;609struct ieee80211_rts *rts;610bool use_rts = false, use_cts = false;611u16 dma_len = session->dma_len;612u16 mimo_ctlchbw = PHY_TXC1_BW_20MHZ;613u32 rspec = 0, rspec_fallback = 0;614u32 rts_rspec = 0, rts_rspec_fallback = 0;615u8 plcp0, is40, mcs;616u16 mch;617u8 preamble_type = BRCMS_GF_PREAMBLE;618u8 fbr_preamble_type = BRCMS_GF_PREAMBLE;619u8 rts_preamble_type = BRCMS_LONG_PREAMBLE;620u8 rts_fbr_preamble_type = BRCMS_LONG_PREAMBLE;621622if (skb_queue_empty(&session->skb_list))623return;624625first = skb_peek(&session->skb_list);626last = skb_peek_tail(&session->skb_list);627628/* Need to fix up last MPDU first to adjust AMPDU length */629txh = (struct d11txh *)last->data;630fifo = le16_to_cpu(txh->TxFrameID) & TXFID_QUEUE_MASK;631f = &du->fifo_tb[fifo];632633mcl = le16_to_cpu(txh->MacTxControlLow);634mcl &= ~TXC_AMPDU_MASK;635mcl |= (TXC_AMPDU_LAST << TXC_AMPDU_SHIFT);636txh->MacTxControlLow = cpu_to_le16(mcl);637638/* remove the null delimiter after last mpdu */639ndelim = txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM];640txh->RTSPLCPFallback[AMPDU_FBR_NULL_DELIM] = 0;641session->ampdu_len -= ndelim * AMPDU_DELIMITER_LEN;642643/* remove the pad len from last mpdu */644fbr_iscck = ((le16_to_cpu(txh->XtraFrameTypes) & 0x3) == 0);645len = fbr_iscck ? BRCMS_GET_CCK_PLCP_LEN(txh->FragPLCPFallback) :646BRCMS_GET_MIMO_PLCP_LEN(txh->FragPLCPFallback);647session->ampdu_len -= roundup(len, 4) - len;648649/* Now fix up the first MPDU */650tx_info = IEEE80211_SKB_CB(first);651txrate = tx_info->status.rates;652txh = (struct d11txh *)first->data;653plcp = (u8 *)(txh + 1);654rts = (struct ieee80211_rts *)&txh->rts_frame;655656mcl = le16_to_cpu(txh->MacTxControlLow);657/* If only one MPDU leave it marked as last */658if (first != last) {659mcl &= ~TXC_AMPDU_MASK;660mcl |= (TXC_AMPDU_FIRST << TXC_AMPDU_SHIFT);661}662mcl |= TXC_STARTMSDU;663if (ieee80211_is_rts(rts->frame_control)) {664mcl |= TXC_SENDRTS;665use_rts = true;666}667if (ieee80211_is_cts(rts->frame_control)) {668mcl |= TXC_SENDCTS;669use_cts = true;670}671txh->MacTxControlLow = cpu_to_le16(mcl);672673fbr = txrate[1].count > 0;674if (!fbr)675plcp0 = plcp[0];676else677plcp0 = txh->FragPLCPFallback[0];678679is40 = (plcp0 & MIMO_PLCP_40MHZ) ? 1 : 0;680mcs = plcp0 & ~MIMO_PLCP_40MHZ;681682if (is40) {683if (CHSPEC_SB_UPPER(wlc_phy_chanspec_get(wlc->band->pi)))684mimo_ctlchbw = PHY_TXC1_BW_20MHZ_UP;685else686mimo_ctlchbw = PHY_TXC1_BW_20MHZ;687}688689/* rebuild the rspec and rspec_fallback */690rspec = RSPEC_MIMORATE;691rspec |= plcp[0] & ~MIMO_PLCP_40MHZ;692if (plcp[0] & MIMO_PLCP_40MHZ)693rspec |= (PHY_TXC1_BW_40MHZ << RSPEC_BW_SHIFT);694695fbr_iscck = !(le16_to_cpu(txh->XtraFrameTypes) & 0x03);696if (fbr_iscck) {697rspec_fallback =698cck_rspec(cck_phy2mac_rate(txh->FragPLCPFallback[0]));699} else {700rspec_fallback = RSPEC_MIMORATE;701rspec_fallback |= txh->FragPLCPFallback[0] & ~MIMO_PLCP_40MHZ;702if (txh->FragPLCPFallback[0] & MIMO_PLCP_40MHZ)703rspec_fallback |= PHY_TXC1_BW_40MHZ << RSPEC_BW_SHIFT;704}705706if (use_rts || use_cts) {707rts_rspec =708brcms_c_rspec_to_rts_rspec(wlc, rspec,709false, mimo_ctlchbw);710rts_rspec_fallback =711brcms_c_rspec_to_rts_rspec(wlc, rspec_fallback,712false, mimo_ctlchbw);713}714715BRCMS_SET_MIMO_PLCP_LEN(plcp, session->ampdu_len);716/* mark plcp to indicate ampdu */717BRCMS_SET_MIMO_PLCP_AMPDU(plcp);718719/* reset the mixed mode header durations */720if (txh->MModeLen) {721u16 mmodelen = brcms_c_calc_lsig_len(wlc, rspec,722session->ampdu_len);723txh->MModeLen = cpu_to_le16(mmodelen);724preamble_type = BRCMS_MM_PREAMBLE;725}726if (txh->MModeFbrLen) {727u16 mmfbrlen = brcms_c_calc_lsig_len(wlc, rspec_fallback,728session->ampdu_len);729txh->MModeFbrLen = cpu_to_le16(mmfbrlen);730fbr_preamble_type = BRCMS_MM_PREAMBLE;731}732733/* set the preload length */734if (mcs_2_rate(mcs, true, false) >= f->dmaxferrate) {735dma_len = min(dma_len, f->ampdu_pld_size);736txh->PreloadSize = cpu_to_le16(dma_len);737} else {738txh->PreloadSize = 0;739}740741mch = le16_to_cpu(txh->MacTxControlHigh);742743/* update RTS dur fields */744if (use_rts || use_cts) {745u16 durid;746if ((mch & TXC_PREAMBLE_RTS_MAIN_SHORT) ==747TXC_PREAMBLE_RTS_MAIN_SHORT)748rts_preamble_type = BRCMS_SHORT_PREAMBLE;749750if ((mch & TXC_PREAMBLE_RTS_FB_SHORT) ==751TXC_PREAMBLE_RTS_FB_SHORT)752rts_fbr_preamble_type = BRCMS_SHORT_PREAMBLE;753754durid = brcms_c_compute_rtscts_dur(wlc, use_cts, rts_rspec,755rspec, rts_preamble_type,756preamble_type,757session->ampdu_len, true);758rts->duration = cpu_to_le16(durid);759durid = brcms_c_compute_rtscts_dur(wlc, use_cts,760rts_rspec_fallback,761rspec_fallback,762rts_fbr_preamble_type,763fbr_preamble_type,764session->ampdu_len, true);765txh->RTSDurFallback = cpu_to_le16(durid);766/* set TxFesTimeNormal */767txh->TxFesTimeNormal = rts->duration;768/* set fallback rate version of TxFesTimeNormal */769txh->TxFesTimeFallback = txh->RTSDurFallback;770}771772/* set flag and plcp for fallback rate */773if (fbr) {774mch |= TXC_AMPDU_FBR;775txh->MacTxControlHigh = cpu_to_le16(mch);776BRCMS_SET_MIMO_PLCP_AMPDU(plcp);777BRCMS_SET_MIMO_PLCP_AMPDU(txh->FragPLCPFallback);778}779780brcms_dbg_ht(wlc->hw->d11core, "wl%d: count %d ampdu_len %d\n",781wlc->pub->unit, skb_queue_len(&session->skb_list),782session->ampdu_len);783}784785static void786brcms_c_ampdu_rate_status(struct brcms_c_info *wlc,787struct ieee80211_tx_info *tx_info,788struct tx_status *txs, u8 mcs)789{790struct ieee80211_tx_rate *txrate = tx_info->status.rates;791int i;792793/* clear the rest of the rates */794for (i = 2; i < IEEE80211_TX_MAX_RATES; i++) {795txrate[i].idx = -1;796txrate[i].count = 0;797}798}799800static void801brcms_c_ampdu_dotxstatus_complete(struct ampdu_info *ampdu, struct scb *scb,802struct sk_buff *p, struct tx_status *txs,803u32 s1, u32 s2)804{805struct scb_ampdu *scb_ampdu;806struct brcms_c_info *wlc = ampdu->wlc;807struct scb_ampdu_tid_ini *ini;808u8 bitmap[8], queue, tid;809struct d11txh *txh;810u8 *plcp;811struct ieee80211_hdr *h;812u16 seq, start_seq = 0, bindex, index, mcl;813u8 mcs = 0;814bool ba_recd = false, ack_recd = false;815u8 tot_mpdu = 0;816uint supr_status;817bool retry = true;818u16 mimoantsel = 0;819u8 retry_limit;820struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(p);821822#ifdef DEBUG823u8 hole[AMPDU_MAX_MPDU];824memset(hole, 0, sizeof(hole));825#endif826827scb_ampdu = &scb->scb_ampdu;828tid = (u8) (p->priority);829830ini = &scb_ampdu->ini[tid];831retry_limit = ampdu->retry_limit_tid[tid];832memset(bitmap, 0, sizeof(bitmap));833queue = txs->frameid & TXFID_QUEUE_MASK;834supr_status = txs->status & TX_STATUS_SUPR_MASK;835836if (txs->status & TX_STATUS_ACK_RCV) {837WARN_ON(!(txs->status & TX_STATUS_INTERMEDIATE));838start_seq = txs->sequence >> SEQNUM_SHIFT;839bitmap[0] = (txs->status & TX_STATUS_BA_BMAP03_MASK) >>840TX_STATUS_BA_BMAP03_SHIFT;841842WARN_ON(s1 & TX_STATUS_INTERMEDIATE);843WARN_ON(!(s1 & TX_STATUS_AMPDU));844845bitmap[0] |=846(s1 & TX_STATUS_BA_BMAP47_MASK) <<847TX_STATUS_BA_BMAP47_SHIFT;848bitmap[1] = (s1 >> 8) & 0xff;849bitmap[2] = (s1 >> 16) & 0xff;850bitmap[3] = (s1 >> 24) & 0xff;851852bitmap[4] = s2 & 0xff;853bitmap[5] = (s2 >> 8) & 0xff;854bitmap[6] = (s2 >> 16) & 0xff;855bitmap[7] = (s2 >> 24) & 0xff;856857ba_recd = true;858} else {859if (supr_status) {860if (supr_status == TX_STATUS_SUPR_BADCH) {861brcms_dbg_ht(wlc->hw->d11core,862"%s: Pkt tx suppressed, illegal channel possibly %d\n",863__func__, CHSPEC_CHANNEL(864wlc->default_bss->chanspec));865} else {866if (supr_status != TX_STATUS_SUPR_FRAG)867brcms_err(wlc->hw->d11core,868"%s: supr_status 0x%x\n",869__func__, supr_status);870}871/* no need to retry for badch; will fail again */872if (supr_status == TX_STATUS_SUPR_BADCH ||873supr_status == TX_STATUS_SUPR_EXPTIME) {874retry = false;875} else if (supr_status == TX_STATUS_SUPR_EXPTIME) {876/* TX underflow:877* try tuning pre-loading or ampdu size878*/879} else if (supr_status == TX_STATUS_SUPR_FRAG) {880/*881* if there were underflows, but pre-loading882* is not active, notify rate adaptation.883*/884brcms_c_ffpld_check_txfunfl(wlc, queue);885}886} else if (txs->phyerr) {887brcms_dbg_ht(wlc->hw->d11core,888"%s: ampdu tx phy error (0x%x)\n",889__func__, txs->phyerr);890}891}892893/* loop through all pkts and retry if not acked */894while (p) {895tx_info = IEEE80211_SKB_CB(p);896txh = (struct d11txh *) p->data;897mcl = le16_to_cpu(txh->MacTxControlLow);898plcp = (u8 *) (txh + 1);899h = (struct ieee80211_hdr *)(plcp + D11_PHY_HDR_LEN);900seq = le16_to_cpu(h->seq_ctrl) >> SEQNUM_SHIFT;901902trace_brcms_txdesc(&wlc->hw->d11core->dev, txh, sizeof(*txh));903904if (tot_mpdu == 0) {905mcs = plcp[0] & MIMO_PLCP_MCS_MASK;906mimoantsel = le16_to_cpu(txh->ABI_MimoAntSel);907}908909index = TX_SEQ_TO_INDEX(seq);910ack_recd = false;911if (ba_recd) {912int block_acked;913914bindex = MODSUB_POW2(seq, start_seq, SEQNUM_MAX);915if (bindex < AMPDU_TX_BA_MAX_WSIZE)916block_acked = isset(bitmap, bindex);917else918block_acked = 0;919brcms_dbg_ht(wlc->hw->d11core,920"tid %d seq %d, start_seq %d, bindex %d set %d, index %d\n",921tid, seq, start_seq, bindex,922block_acked, index);923/* if acked then clear bit and free packet */924if (block_acked) {925ini->txretry[index] = 0;926927/*928* ampdu_ack_len:929* number of acked aggregated frames930*/931/* ampdu_len: number of aggregated frames */932brcms_c_ampdu_rate_status(wlc, tx_info, txs,933mcs);934tx_info->flags |= IEEE80211_TX_STAT_ACK;935tx_info->flags |= IEEE80211_TX_STAT_AMPDU;936tx_info->status.ampdu_ack_len =937tx_info->status.ampdu_len = 1;938939skb_pull(p, D11_PHY_HDR_LEN);940skb_pull(p, D11_TXH_LEN);941942ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw,943p);944ack_recd = true;945}946}947/* either retransmit or send bar if ack not recd */948if (!ack_recd) {949if (retry && (ini->txretry[index] < (int)retry_limit)) {950int ret;951ini->txretry[index]++;952ret = brcms_c_txfifo(wlc, queue, p);953/*954* We shouldn't be out of space in the DMA955* ring here since we're reinserting a frame956* that was just pulled out.957*/958WARN_ONCE(ret, "queue %d out of txds\n", queue);959} else {960/* Retry timeout */961ieee80211_tx_info_clear_status(tx_info);962tx_info->status.ampdu_ack_len = 0;963tx_info->status.ampdu_len = 1;964tx_info->flags |=965IEEE80211_TX_STAT_AMPDU_NO_BACK;966skb_pull(p, D11_PHY_HDR_LEN);967skb_pull(p, D11_TXH_LEN);968brcms_dbg_ht(wlc->hw->d11core,969"BA Timeout, seq %d\n",970seq);971ieee80211_tx_status_irqsafe(wlc->pub->ieee_hw,972p);973}974}975tot_mpdu++;976977/* break out if last packet of ampdu */978if (((mcl & TXC_AMPDU_MASK) >> TXC_AMPDU_SHIFT) ==979TXC_AMPDU_LAST)980break;981982p = dma_getnexttxp(wlc->hw->di[queue], DMA_RANGE_TRANSMITTED);983}984985/* update rate state */986brcms_c_antsel_antsel2id(wlc->asi, mimoantsel);987}988989void990brcms_c_ampdu_dotxstatus(struct ampdu_info *ampdu, struct scb *scb,991struct sk_buff *p, struct tx_status *txs)992{993struct brcms_c_info *wlc = ampdu->wlc;994u32 s1 = 0, s2 = 0;995996/* BMAC_NOTE: For the split driver, second level txstatus comes later997* So if the ACK was received then wait for the second level else just998* call the first one999*/1000if (txs->status & TX_STATUS_ACK_RCV) {1001u8 status_delay = 0;10021003/* wait till the next 8 bytes of txstatus is available */1004s1 = bcma_read32(wlc->hw->d11core, D11REGOFFS(frmtxstatus));1005while ((s1 & TXS_V) == 0) {1006udelay(1);1007status_delay++;1008if (status_delay > 10)1009return; /* error condition */1010s1 = bcma_read32(wlc->hw->d11core,1011D11REGOFFS(frmtxstatus));1012}10131014s2 = bcma_read32(wlc->hw->d11core, D11REGOFFS(frmtxstatus2));1015}10161017if (scb) {1018brcms_c_ampdu_dotxstatus_complete(ampdu, scb, p, txs, s1, s2);1019} else {1020/* loop through all pkts and free */1021u8 queue = txs->frameid & TXFID_QUEUE_MASK;1022struct d11txh *txh;1023u16 mcl;1024while (p) {1025txh = (struct d11txh *) p->data;1026trace_brcms_txdesc(&wlc->hw->d11core->dev, txh,1027sizeof(*txh));1028mcl = le16_to_cpu(txh->MacTxControlLow);1029brcmu_pkt_buf_free_skb(p);1030/* break out if last packet of ampdu */1031if (((mcl & TXC_AMPDU_MASK) >> TXC_AMPDU_SHIFT) ==1032TXC_AMPDU_LAST)1033break;1034p = dma_getnexttxp(wlc->hw->di[queue],1035DMA_RANGE_TRANSMITTED);1036}1037}1038}10391040void brcms_c_ampdu_macaddr_upd(struct brcms_c_info *wlc)1041{1042char template[T_RAM_ACCESS_SZ * 2];10431044/* driver needs to write the ta in the template; ta is at offset 16 */1045memset(template, 0, sizeof(template));1046memcpy(template, wlc->pub->cur_etheraddr, ETH_ALEN);1047brcms_b_write_template_ram(wlc->hw, (T_BA_TPL_BASE + 16),1048(T_RAM_ACCESS_SZ * 2),1049template);1050}10511052bool brcms_c_aggregatable(struct brcms_c_info *wlc, u8 tid)1053{1054return wlc->ampdu->ini_enable[tid];1055}10561057void brcms_c_ampdu_shm_upd(struct ampdu_info *ampdu)1058{1059struct brcms_c_info *wlc = ampdu->wlc;10601061/*1062* Extend ucode internal watchdog timer to1063* match larger received frames1064*/1065if ((ampdu->rx_factor & IEEE80211_HT_AMPDU_PARM_FACTOR) ==1066IEEE80211_HT_MAX_AMPDU_64K) {1067brcms_b_write_shm(wlc->hw, M_MIMO_MAXSYM, MIMO_MAXSYM_MAX);1068brcms_b_write_shm(wlc->hw, M_WATCHDOG_8TU, WATCHDOG_8TU_MAX);1069} else {1070brcms_b_write_shm(wlc->hw, M_MIMO_MAXSYM, MIMO_MAXSYM_DEF);1071brcms_b_write_shm(wlc->hw, M_WATCHDOG_8TU, WATCHDOG_8TU_DEF);1072}1073}10741075/*1076* callback function that helps invalidating ampdu packets in a DMA queue1077*/1078static void dma_cb_fn_ampdu(void *txi, void *arg_a)1079{1080struct ieee80211_sta *sta = arg_a;1081struct ieee80211_tx_info *tx_info = (struct ieee80211_tx_info *)txi;10821083if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) &&1084(tx_info->rate_driver_data[0] == sta || sta == NULL))1085tx_info->rate_driver_data[0] = NULL;1086}10871088/*1089* When a remote party is no longer available for ampdu communication, any1090* pending tx ampdu packets in the driver have to be flushed.1091*/1092void brcms_c_ampdu_flush(struct brcms_c_info *wlc,1093struct ieee80211_sta *sta, u16 tid)1094{1095brcms_c_inval_dma_pkts(wlc->hw, sta, dma_cb_fn_ampdu);1096}109710981099