tx.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2013 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called COPYING.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/sched.h>
  33. #include <linux/ieee80211.h>
  34. #include "iwl-io.h"
  35. #include "iwl-trans.h"
  36. #include "iwl-agn-hw.h"
  37. #include "dev.h"
  38. #include "agn.h"
  39. static const u8 tid_to_ac[] = {
  40. IEEE80211_AC_BE,
  41. IEEE80211_AC_BK,
  42. IEEE80211_AC_BK,
  43. IEEE80211_AC_BE,
  44. IEEE80211_AC_VI,
  45. IEEE80211_AC_VI,
  46. IEEE80211_AC_VO,
  47. IEEE80211_AC_VO,
  48. };
  49. static void iwlagn_tx_cmd_protection(struct iwl_priv *priv,
  50. struct ieee80211_tx_info *info,
  51. __le16 fc, __le32 *tx_flags)
  52. {
  53. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS ||
  54. info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT ||
  55. info->flags & IEEE80211_TX_CTL_AMPDU)
  56. *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
  57. }
  58. /*
  59. * handle build REPLY_TX command notification.
  60. */
  61. static void iwlagn_tx_cmd_build_basic(struct iwl_priv *priv,
  62. struct sk_buff *skb,
  63. struct iwl_tx_cmd *tx_cmd,
  64. struct ieee80211_tx_info *info,
  65. struct ieee80211_hdr *hdr, u8 sta_id)
  66. {
  67. __le16 fc = hdr->frame_control;
  68. __le32 tx_flags = tx_cmd->tx_flags;
  69. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  70. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  71. tx_flags |= TX_CMD_FLG_ACK_MSK;
  72. else
  73. tx_flags &= ~TX_CMD_FLG_ACK_MSK;
  74. if (ieee80211_is_probe_resp(fc))
  75. tx_flags |= TX_CMD_FLG_TSF_MSK;
  76. else if (ieee80211_is_back_req(fc))
  77. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  78. else if (info->band == IEEE80211_BAND_2GHZ &&
  79. priv->lib->bt_params &&
  80. priv->lib->bt_params->advanced_bt_coexist &&
  81. (ieee80211_is_auth(fc) || ieee80211_is_assoc_req(fc) ||
  82. ieee80211_is_reassoc_req(fc) ||
  83. info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
  84. tx_flags |= TX_CMD_FLG_IGNORE_BT;
  85. tx_cmd->sta_id = sta_id;
  86. if (ieee80211_has_morefrags(fc))
  87. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  88. if (ieee80211_is_data_qos(fc)) {
  89. u8 *qc = ieee80211_get_qos_ctl(hdr);
  90. tx_cmd->tid_tspec = qc[0] & 0xf;
  91. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  92. } else {
  93. tx_cmd->tid_tspec = IWL_TID_NON_QOS;
  94. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  95. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  96. else
  97. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  98. }
  99. iwlagn_tx_cmd_protection(priv, info, fc, &tx_flags);
  100. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  101. if (ieee80211_is_mgmt(fc)) {
  102. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  103. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  104. else
  105. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  106. } else {
  107. tx_cmd->timeout.pm_frame_timeout = 0;
  108. }
  109. tx_cmd->driver_txop = 0;
  110. tx_cmd->tx_flags = tx_flags;
  111. tx_cmd->next_frame_len = 0;
  112. }
  113. static void iwlagn_tx_cmd_build_rate(struct iwl_priv *priv,
  114. struct iwl_tx_cmd *tx_cmd,
  115. struct ieee80211_tx_info *info,
  116. struct ieee80211_sta *sta,
  117. __le16 fc)
  118. {
  119. u32 rate_flags;
  120. int rate_idx;
  121. u8 rts_retry_limit;
  122. u8 data_retry_limit;
  123. u8 rate_plcp;
  124. if (priv->wowlan) {
  125. rts_retry_limit = IWLAGN_LOW_RETRY_LIMIT;
  126. data_retry_limit = IWLAGN_LOW_RETRY_LIMIT;
  127. } else {
  128. /* Set retry limit on RTS packets */
  129. rts_retry_limit = IWLAGN_RTS_DFAULT_RETRY_LIMIT;
  130. /* Set retry limit on DATA packets and Probe Responses*/
  131. if (ieee80211_is_probe_resp(fc)) {
  132. data_retry_limit = IWLAGN_MGMT_DFAULT_RETRY_LIMIT;
  133. rts_retry_limit =
  134. min(data_retry_limit, rts_retry_limit);
  135. } else if (ieee80211_is_back_req(fc))
  136. data_retry_limit = IWLAGN_BAR_DFAULT_RETRY_LIMIT;
  137. else
  138. data_retry_limit = IWLAGN_DEFAULT_TX_RETRY;
  139. }
  140. tx_cmd->data_retry_limit = data_retry_limit;
  141. tx_cmd->rts_retry_limit = rts_retry_limit;
  142. /* DATA packets will use the uCode station table for rate/antenna
  143. * selection */
  144. if (ieee80211_is_data(fc)) {
  145. tx_cmd->initial_rate_index = 0;
  146. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  147. return;
  148. } else if (ieee80211_is_back_req(fc))
  149. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  150. /**
  151. * If the current TX rate stored in mac80211 has the MCS bit set, it's
  152. * not really a TX rate. Thus, we use the lowest supported rate for
  153. * this band. Also use the lowest supported rate if the stored rate
  154. * index is invalid.
  155. */
  156. rate_idx = info->control.rates[0].idx;
  157. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS ||
  158. (rate_idx < 0) || (rate_idx > IWL_RATE_COUNT_LEGACY))
  159. rate_idx = rate_lowest_index(
  160. &priv->nvm_data->bands[info->band], sta);
  161. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  162. if (info->band == IEEE80211_BAND_5GHZ)
  163. rate_idx += IWL_FIRST_OFDM_RATE;
  164. /* Get PLCP rate for tx_cmd->rate_n_flags */
  165. rate_plcp = iwl_rates[rate_idx].plcp;
  166. /* Zero out flags for this packet */
  167. rate_flags = 0;
  168. /* Set CCK flag as needed */
  169. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  170. rate_flags |= RATE_MCS_CCK_MSK;
  171. /* Set up antennas */
  172. if (priv->lib->bt_params &&
  173. priv->lib->bt_params->advanced_bt_coexist &&
  174. priv->bt_full_concurrent) {
  175. /* operated as 1x1 in full concurrency mode */
  176. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  177. first_antenna(priv->nvm_data->valid_tx_ant));
  178. } else
  179. priv->mgmt_tx_ant = iwl_toggle_tx_ant(
  180. priv, priv->mgmt_tx_ant,
  181. priv->nvm_data->valid_tx_ant);
  182. rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
  183. /* Set the rate in the TX cmd */
  184. tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
  185. }
  186. static void iwlagn_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  187. struct ieee80211_tx_info *info,
  188. struct iwl_tx_cmd *tx_cmd,
  189. struct sk_buff *skb_frag)
  190. {
  191. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  192. switch (keyconf->cipher) {
  193. case WLAN_CIPHER_SUITE_CCMP:
  194. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  195. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  196. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  197. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  198. break;
  199. case WLAN_CIPHER_SUITE_TKIP:
  200. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  201. ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
  202. break;
  203. case WLAN_CIPHER_SUITE_WEP104:
  204. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  205. /* fall through */
  206. case WLAN_CIPHER_SUITE_WEP40:
  207. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  208. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  209. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  210. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  211. "with key %d\n", keyconf->keyidx);
  212. break;
  213. default:
  214. IWL_ERR(priv, "Unknown encode cipher %x\n", keyconf->cipher);
  215. break;
  216. }
  217. }
  218. /**
  219. * iwl_sta_id_or_broadcast - return sta_id or broadcast sta
  220. * @context: the current context
  221. * @sta: mac80211 station
  222. *
  223. * In certain circumstances mac80211 passes a station pointer
  224. * that may be %NULL, for example during TX or key setup. In
  225. * that case, we need to use the broadcast station, so this
  226. * inline wraps that pattern.
  227. */
  228. static int iwl_sta_id_or_broadcast(struct iwl_rxon_context *context,
  229. struct ieee80211_sta *sta)
  230. {
  231. int sta_id;
  232. if (!sta)
  233. return context->bcast_sta_id;
  234. sta_id = iwl_sta_id(sta);
  235. /*
  236. * mac80211 should not be passing a partially
  237. * initialised station!
  238. */
  239. WARN_ON(sta_id == IWL_INVALID_STATION);
  240. return sta_id;
  241. }
  242. /*
  243. * start REPLY_TX command process
  244. */
  245. int iwlagn_tx_skb(struct iwl_priv *priv,
  246. struct ieee80211_sta *sta,
  247. struct sk_buff *skb)
  248. {
  249. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  250. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  251. struct iwl_station_priv *sta_priv = NULL;
  252. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  253. struct iwl_device_cmd *dev_cmd;
  254. struct iwl_tx_cmd *tx_cmd;
  255. __le16 fc;
  256. u8 hdr_len;
  257. u16 len, seq_number = 0;
  258. u8 sta_id, tid = IWL_MAX_TID_COUNT;
  259. bool is_agg = false, is_data_qos = false;
  260. int txq_id;
  261. if (info->control.vif)
  262. ctx = iwl_rxon_ctx_from_vif(info->control.vif);
  263. if (iwl_is_rfkill(priv)) {
  264. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  265. goto drop_unlock_priv;
  266. }
  267. fc = hdr->frame_control;
  268. #ifdef CONFIG_IWLWIFI_DEBUG
  269. if (ieee80211_is_auth(fc))
  270. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  271. else if (ieee80211_is_assoc_req(fc))
  272. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  273. else if (ieee80211_is_reassoc_req(fc))
  274. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  275. #endif
  276. if (unlikely(ieee80211_is_probe_resp(fc))) {
  277. struct iwl_wipan_noa_data *noa_data =
  278. rcu_dereference(priv->noa_data);
  279. if (noa_data &&
  280. pskb_expand_head(skb, 0, noa_data->length,
  281. GFP_ATOMIC) == 0) {
  282. memcpy(skb_put(skb, noa_data->length),
  283. noa_data->data, noa_data->length);
  284. hdr = (struct ieee80211_hdr *)skb->data;
  285. }
  286. }
  287. hdr_len = ieee80211_hdrlen(fc);
  288. /* For management frames use broadcast id to do not break aggregation */
  289. if (!ieee80211_is_data(fc))
  290. sta_id = ctx->bcast_sta_id;
  291. else {
  292. /* Find index into station table for destination station */
  293. sta_id = iwl_sta_id_or_broadcast(ctx, sta);
  294. if (sta_id == IWL_INVALID_STATION) {
  295. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  296. hdr->addr1);
  297. goto drop_unlock_priv;
  298. }
  299. }
  300. if (sta)
  301. sta_priv = (void *)sta->drv_priv;
  302. if (sta_priv && sta_priv->asleep &&
  303. (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)) {
  304. /*
  305. * This sends an asynchronous command to the device,
  306. * but we can rely on it being processed before the
  307. * next frame is processed -- and the next frame to
  308. * this station is the one that will consume this
  309. * counter.
  310. * For now set the counter to just 1 since we do not
  311. * support uAPSD yet.
  312. *
  313. * FIXME: If we get two non-bufferable frames one
  314. * after the other, we might only send out one of
  315. * them because this is racy.
  316. */
  317. iwl_sta_modify_sleep_tx_count(priv, sta_id, 1);
  318. }
  319. dev_cmd = iwl_trans_alloc_tx_cmd(priv->trans);
  320. if (unlikely(!dev_cmd))
  321. goto drop_unlock_priv;
  322. memset(dev_cmd, 0, sizeof(*dev_cmd));
  323. tx_cmd = (struct iwl_tx_cmd *) dev_cmd->payload;
  324. /* Total # bytes to be transmitted */
  325. len = (u16)skb->len;
  326. tx_cmd->len = cpu_to_le16(len);
  327. if (info->control.hw_key)
  328. iwlagn_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb);
  329. /* TODO need this for burst mode later on */
  330. iwlagn_tx_cmd_build_basic(priv, skb, tx_cmd, info, hdr, sta_id);
  331. iwlagn_tx_cmd_build_rate(priv, tx_cmd, info, sta, fc);
  332. memset(&info->status, 0, sizeof(info->status));
  333. info->driver_data[0] = ctx;
  334. info->driver_data[1] = dev_cmd;
  335. /* From now on, we cannot access info->control */
  336. spin_lock(&priv->sta_lock);
  337. if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
  338. u8 *qc = NULL;
  339. struct iwl_tid_data *tid_data;
  340. qc = ieee80211_get_qos_ctl(hdr);
  341. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  342. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  343. goto drop_unlock_sta;
  344. tid_data = &priv->tid_data[sta_id][tid];
  345. /* aggregation is on for this <sta,tid> */
  346. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  347. tid_data->agg.state != IWL_AGG_ON) {
  348. IWL_ERR(priv, "TX_CTL_AMPDU while not in AGG:"
  349. " Tx flags = 0x%08x, agg.state = %d",
  350. info->flags, tid_data->agg.state);
  351. IWL_ERR(priv, "sta_id = %d, tid = %d seq_num = %d",
  352. sta_id, tid,
  353. IEEE80211_SEQ_TO_SN(tid_data->seq_number));
  354. goto drop_unlock_sta;
  355. }
  356. /* We can receive packets from the stack in IWL_AGG_{ON,OFF}
  357. * only. Check this here.
  358. */
  359. if (WARN_ONCE(tid_data->agg.state != IWL_AGG_ON &&
  360. tid_data->agg.state != IWL_AGG_OFF,
  361. "Tx while agg.state = %d", tid_data->agg.state))
  362. goto drop_unlock_sta;
  363. seq_number = tid_data->seq_number;
  364. seq_number &= IEEE80211_SCTL_SEQ;
  365. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  366. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  367. seq_number += 0x10;
  368. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  369. is_agg = true;
  370. is_data_qos = true;
  371. }
  372. /* Copy MAC header from skb into command buffer */
  373. memcpy(tx_cmd->hdr, hdr, hdr_len);
  374. if (is_agg)
  375. txq_id = priv->tid_data[sta_id][tid].agg.txq_id;
  376. else if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  377. /*
  378. * Send this frame after DTIM -- there's a special queue
  379. * reserved for this for contexts that support AP mode.
  380. */
  381. txq_id = ctx->mcast_queue;
  382. /*
  383. * The microcode will clear the more data
  384. * bit in the last frame it transmits.
  385. */
  386. hdr->frame_control |=
  387. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  388. } else if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
  389. txq_id = IWL_AUX_QUEUE;
  390. else
  391. txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
  392. WARN_ON_ONCE(!is_agg && txq_id != info->hw_queue);
  393. WARN_ON_ONCE(is_agg &&
  394. priv->queue_to_mac80211[txq_id] != info->hw_queue);
  395. IWL_DEBUG_TX(priv, "TX to [%d|%d] Q:%d - seq: 0x%x\n", sta_id, tid,
  396. txq_id, seq_number);
  397. if (iwl_trans_tx(priv->trans, skb, dev_cmd, txq_id))
  398. goto drop_unlock_sta;
  399. if (is_data_qos && !ieee80211_has_morefrags(fc))
  400. priv->tid_data[sta_id][tid].seq_number = seq_number;
  401. spin_unlock(&priv->sta_lock);
  402. /*
  403. * Avoid atomic ops if it isn't an associated client.
  404. * Also, if this is a packet for aggregation, don't
  405. * increase the counter because the ucode will stop
  406. * aggregation queues when their respective station
  407. * goes to sleep.
  408. */
  409. if (sta_priv && sta_priv->client && !is_agg)
  410. atomic_inc(&sta_priv->pending_frames);
  411. return 0;
  412. drop_unlock_sta:
  413. if (dev_cmd)
  414. iwl_trans_free_tx_cmd(priv->trans, dev_cmd);
  415. spin_unlock(&priv->sta_lock);
  416. drop_unlock_priv:
  417. return -1;
  418. }
  419. static int iwlagn_alloc_agg_txq(struct iwl_priv *priv, int mq)
  420. {
  421. int q;
  422. for (q = IWLAGN_FIRST_AMPDU_QUEUE;
  423. q < priv->cfg->base_params->num_of_queues; q++) {
  424. if (!test_and_set_bit(q, priv->agg_q_alloc)) {
  425. priv->queue_to_mac80211[q] = mq;
  426. return q;
  427. }
  428. }
  429. return -ENOSPC;
  430. }
  431. static void iwlagn_dealloc_agg_txq(struct iwl_priv *priv, int q)
  432. {
  433. clear_bit(q, priv->agg_q_alloc);
  434. priv->queue_to_mac80211[q] = IWL_INVALID_MAC80211_QUEUE;
  435. }
  436. int iwlagn_tx_agg_stop(struct iwl_priv *priv, struct ieee80211_vif *vif,
  437. struct ieee80211_sta *sta, u16 tid)
  438. {
  439. struct iwl_tid_data *tid_data;
  440. int sta_id, txq_id;
  441. enum iwl_agg_state agg_state;
  442. sta_id = iwl_sta_id(sta);
  443. if (sta_id == IWL_INVALID_STATION) {
  444. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  445. return -ENXIO;
  446. }
  447. spin_lock_bh(&priv->sta_lock);
  448. tid_data = &priv->tid_data[sta_id][tid];
  449. txq_id = tid_data->agg.txq_id;
  450. switch (tid_data->agg.state) {
  451. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  452. /*
  453. * This can happen if the peer stops aggregation
  454. * again before we've had a chance to drain the
  455. * queue we selected previously, i.e. before the
  456. * session was really started completely.
  457. */
  458. IWL_DEBUG_HT(priv, "AGG stop before setup done\n");
  459. goto turn_off;
  460. case IWL_AGG_STARTING:
  461. /*
  462. * This can happen when the session is stopped before
  463. * we receive ADDBA response
  464. */
  465. IWL_DEBUG_HT(priv, "AGG stop before AGG became operational\n");
  466. goto turn_off;
  467. case IWL_AGG_ON:
  468. break;
  469. default:
  470. IWL_WARN(priv,
  471. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  472. sta_id, tid, tid_data->agg.state);
  473. spin_unlock_bh(&priv->sta_lock);
  474. return 0;
  475. }
  476. tid_data->agg.ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  477. /* There are still packets for this RA / TID in the HW */
  478. if (!test_bit(txq_id, priv->agg_q_alloc)) {
  479. IWL_DEBUG_TX_QUEUES(priv,
  480. "stopping AGG on STA/TID %d/%d but hwq %d not used\n",
  481. sta_id, tid, txq_id);
  482. } else if (tid_data->agg.ssn != tid_data->next_reclaimed) {
  483. IWL_DEBUG_TX_QUEUES(priv,
  484. "Can't proceed: ssn %d, next_recl = %d\n",
  485. tid_data->agg.ssn,
  486. tid_data->next_reclaimed);
  487. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_DELBA;
  488. spin_unlock_bh(&priv->sta_lock);
  489. return 0;
  490. }
  491. IWL_DEBUG_TX_QUEUES(priv, "Can proceed: ssn = next_recl = %d\n",
  492. tid_data->agg.ssn);
  493. turn_off:
  494. agg_state = tid_data->agg.state;
  495. tid_data->agg.state = IWL_AGG_OFF;
  496. spin_unlock_bh(&priv->sta_lock);
  497. if (test_bit(txq_id, priv->agg_q_alloc)) {
  498. /*
  499. * If the transport didn't know that we wanted to start
  500. * agreggation, don't tell it that we want to stop them.
  501. * This can happen when we don't get the addBA response on
  502. * time, or we hadn't time to drain the AC queues.
  503. */
  504. if (agg_state == IWL_AGG_ON)
  505. iwl_trans_txq_disable(priv->trans, txq_id);
  506. else
  507. IWL_DEBUG_TX_QUEUES(priv, "Don't disable tx agg: %d\n",
  508. agg_state);
  509. iwlagn_dealloc_agg_txq(priv, txq_id);
  510. }
  511. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  512. return 0;
  513. }
  514. int iwlagn_tx_agg_start(struct iwl_priv *priv, struct ieee80211_vif *vif,
  515. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  516. {
  517. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  518. struct iwl_tid_data *tid_data;
  519. int sta_id, txq_id, ret;
  520. IWL_DEBUG_HT(priv, "TX AGG request on ra = %pM tid = %d\n",
  521. sta->addr, tid);
  522. sta_id = iwl_sta_id(sta);
  523. if (sta_id == IWL_INVALID_STATION) {
  524. IWL_ERR(priv, "Start AGG on invalid station\n");
  525. return -ENXIO;
  526. }
  527. if (unlikely(tid >= IWL_MAX_TID_COUNT))
  528. return -EINVAL;
  529. if (priv->tid_data[sta_id][tid].agg.state != IWL_AGG_OFF) {
  530. IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n");
  531. return -ENXIO;
  532. }
  533. txq_id = iwlagn_alloc_agg_txq(priv, ctx->ac_to_queue[tid_to_ac[tid]]);
  534. if (txq_id < 0) {
  535. IWL_DEBUG_TX_QUEUES(priv,
  536. "No free aggregation queue for %pM/%d\n",
  537. sta->addr, tid);
  538. return txq_id;
  539. }
  540. ret = iwl_sta_tx_modify_enable_tid(priv, sta_id, tid);
  541. if (ret)
  542. return ret;
  543. spin_lock_bh(&priv->sta_lock);
  544. tid_data = &priv->tid_data[sta_id][tid];
  545. tid_data->agg.ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  546. tid_data->agg.txq_id = txq_id;
  547. *ssn = tid_data->agg.ssn;
  548. if (*ssn == tid_data->next_reclaimed) {
  549. IWL_DEBUG_TX_QUEUES(priv, "Can proceed: ssn = next_recl = %d\n",
  550. tid_data->agg.ssn);
  551. tid_data->agg.state = IWL_AGG_STARTING;
  552. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  553. } else {
  554. IWL_DEBUG_TX_QUEUES(priv, "Can't proceed: ssn %d, "
  555. "next_reclaimed = %d\n",
  556. tid_data->agg.ssn,
  557. tid_data->next_reclaimed);
  558. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  559. }
  560. spin_unlock_bh(&priv->sta_lock);
  561. return ret;
  562. }
  563. int iwlagn_tx_agg_flush(struct iwl_priv *priv, struct ieee80211_vif *vif,
  564. struct ieee80211_sta *sta, u16 tid)
  565. {
  566. struct iwl_tid_data *tid_data;
  567. enum iwl_agg_state agg_state;
  568. int sta_id, txq_id;
  569. sta_id = iwl_sta_id(sta);
  570. /*
  571. * First set the agg state to OFF to avoid calling
  572. * ieee80211_stop_tx_ba_cb in iwlagn_check_ratid_empty.
  573. */
  574. spin_lock_bh(&priv->sta_lock);
  575. tid_data = &priv->tid_data[sta_id][tid];
  576. txq_id = tid_data->agg.txq_id;
  577. agg_state = tid_data->agg.state;
  578. IWL_DEBUG_TX_QUEUES(priv, "Flush AGG: sta %d tid %d q %d state %d\n",
  579. sta_id, tid, txq_id, tid_data->agg.state);
  580. tid_data->agg.state = IWL_AGG_OFF;
  581. spin_unlock_bh(&priv->sta_lock);
  582. if (iwlagn_txfifo_flush(priv, BIT(txq_id)))
  583. IWL_ERR(priv, "Couldn't flush the AGG queue\n");
  584. if (test_bit(txq_id, priv->agg_q_alloc)) {
  585. /*
  586. * If the transport didn't know that we wanted to start
  587. * agreggation, don't tell it that we want to stop them.
  588. * This can happen when we don't get the addBA response on
  589. * time, or we hadn't time to drain the AC queues.
  590. */
  591. if (agg_state == IWL_AGG_ON)
  592. iwl_trans_txq_disable(priv->trans, txq_id);
  593. else
  594. IWL_DEBUG_TX_QUEUES(priv, "Don't disable tx agg: %d\n",
  595. agg_state);
  596. iwlagn_dealloc_agg_txq(priv, txq_id);
  597. }
  598. return 0;
  599. }
  600. int iwlagn_tx_agg_oper(struct iwl_priv *priv, struct ieee80211_vif *vif,
  601. struct ieee80211_sta *sta, u16 tid, u8 buf_size)
  602. {
  603. struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
  604. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  605. int q, fifo;
  606. u16 ssn;
  607. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  608. spin_lock_bh(&priv->sta_lock);
  609. ssn = priv->tid_data[sta_priv->sta_id][tid].agg.ssn;
  610. q = priv->tid_data[sta_priv->sta_id][tid].agg.txq_id;
  611. priv->tid_data[sta_priv->sta_id][tid].agg.state = IWL_AGG_ON;
  612. spin_unlock_bh(&priv->sta_lock);
  613. fifo = ctx->ac_to_fifo[tid_to_ac[tid]];
  614. iwl_trans_txq_enable(priv->trans, q, fifo, sta_priv->sta_id, tid,
  615. buf_size, ssn);
  616. /*
  617. * If the limit is 0, then it wasn't initialised yet,
  618. * use the default. We can do that since we take the
  619. * minimum below, and we don't want to go above our
  620. * default due to hardware restrictions.
  621. */
  622. if (sta_priv->max_agg_bufsize == 0)
  623. sta_priv->max_agg_bufsize =
  624. LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  625. /*
  626. * Even though in theory the peer could have different
  627. * aggregation reorder buffer sizes for different sessions,
  628. * our ucode doesn't allow for that and has a global limit
  629. * for each station. Therefore, use the minimum of all the
  630. * aggregation sessions and our default value.
  631. */
  632. sta_priv->max_agg_bufsize =
  633. min(sta_priv->max_agg_bufsize, buf_size);
  634. if (priv->hw_params.use_rts_for_aggregation) {
  635. /*
  636. * switch to RTS/CTS if it is the prefer protection
  637. * method for HT traffic
  638. */
  639. sta_priv->lq_sta.lq.general_params.flags |=
  640. LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
  641. }
  642. priv->agg_tids_count++;
  643. IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
  644. priv->agg_tids_count);
  645. sta_priv->lq_sta.lq.agg_params.agg_frame_cnt_limit =
  646. sta_priv->max_agg_bufsize;
  647. IWL_DEBUG_HT(priv, "Tx aggregation enabled on ra = %pM tid = %d\n",
  648. sta->addr, tid);
  649. return iwl_send_lq_cmd(priv, ctx,
  650. &sta_priv->lq_sta.lq, CMD_ASYNC, false);
  651. }
  652. static void iwlagn_check_ratid_empty(struct iwl_priv *priv, int sta_id, u8 tid)
  653. {
  654. struct iwl_tid_data *tid_data = &priv->tid_data[sta_id][tid];
  655. enum iwl_rxon_context_id ctx;
  656. struct ieee80211_vif *vif;
  657. u8 *addr;
  658. lockdep_assert_held(&priv->sta_lock);
  659. addr = priv->stations[sta_id].sta.sta.addr;
  660. ctx = priv->stations[sta_id].ctxid;
  661. vif = priv->contexts[ctx].vif;
  662. switch (priv->tid_data[sta_id][tid].agg.state) {
  663. case IWL_EMPTYING_HW_QUEUE_DELBA:
  664. /* There are no packets for this RA / TID in the HW any more */
  665. if (tid_data->agg.ssn == tid_data->next_reclaimed) {
  666. IWL_DEBUG_TX_QUEUES(priv,
  667. "Can continue DELBA flow ssn = next_recl ="
  668. " %d", tid_data->next_reclaimed);
  669. iwl_trans_txq_disable(priv->trans,
  670. tid_data->agg.txq_id);
  671. iwlagn_dealloc_agg_txq(priv, tid_data->agg.txq_id);
  672. tid_data->agg.state = IWL_AGG_OFF;
  673. ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
  674. }
  675. break;
  676. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  677. /* There are no packets for this RA / TID in the HW any more */
  678. if (tid_data->agg.ssn == tid_data->next_reclaimed) {
  679. IWL_DEBUG_TX_QUEUES(priv,
  680. "Can continue ADDBA flow ssn = next_recl ="
  681. " %d", tid_data->next_reclaimed);
  682. tid_data->agg.state = IWL_AGG_STARTING;
  683. ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
  684. }
  685. break;
  686. default:
  687. break;
  688. }
  689. }
  690. static void iwlagn_non_agg_tx_status(struct iwl_priv *priv,
  691. struct iwl_rxon_context *ctx,
  692. const u8 *addr1)
  693. {
  694. struct ieee80211_sta *sta;
  695. struct iwl_station_priv *sta_priv;
  696. rcu_read_lock();
  697. sta = ieee80211_find_sta(ctx->vif, addr1);
  698. if (sta) {
  699. sta_priv = (void *)sta->drv_priv;
  700. /* avoid atomic ops if this isn't a client */
  701. if (sta_priv->client &&
  702. atomic_dec_return(&sta_priv->pending_frames) == 0)
  703. ieee80211_sta_block_awake(priv->hw, sta, false);
  704. }
  705. rcu_read_unlock();
  706. }
  707. /**
  708. * translate ucode response to mac80211 tx status control values
  709. */
  710. static void iwlagn_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
  711. struct ieee80211_tx_info *info)
  712. {
  713. struct ieee80211_tx_rate *r = &info->status.rates[0];
  714. info->status.antenna =
  715. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  716. if (rate_n_flags & RATE_MCS_HT_MSK)
  717. r->flags |= IEEE80211_TX_RC_MCS;
  718. if (rate_n_flags & RATE_MCS_GF_MSK)
  719. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  720. if (rate_n_flags & RATE_MCS_HT40_MSK)
  721. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  722. if (rate_n_flags & RATE_MCS_DUP_MSK)
  723. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  724. if (rate_n_flags & RATE_MCS_SGI_MSK)
  725. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  726. r->idx = iwlagn_hwrate_to_mac80211_idx(rate_n_flags, info->band);
  727. }
  728. #ifdef CONFIG_IWLWIFI_DEBUG
  729. const char *iwl_get_tx_fail_reason(u32 status)
  730. {
  731. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  732. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  733. switch (status & TX_STATUS_MSK) {
  734. case TX_STATUS_SUCCESS:
  735. return "SUCCESS";
  736. TX_STATUS_POSTPONE(DELAY);
  737. TX_STATUS_POSTPONE(FEW_BYTES);
  738. TX_STATUS_POSTPONE(BT_PRIO);
  739. TX_STATUS_POSTPONE(QUIET_PERIOD);
  740. TX_STATUS_POSTPONE(CALC_TTAK);
  741. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  742. TX_STATUS_FAIL(SHORT_LIMIT);
  743. TX_STATUS_FAIL(LONG_LIMIT);
  744. TX_STATUS_FAIL(FIFO_UNDERRUN);
  745. TX_STATUS_FAIL(DRAIN_FLOW);
  746. TX_STATUS_FAIL(RFKILL_FLUSH);
  747. TX_STATUS_FAIL(LIFE_EXPIRE);
  748. TX_STATUS_FAIL(DEST_PS);
  749. TX_STATUS_FAIL(HOST_ABORTED);
  750. TX_STATUS_FAIL(BT_RETRY);
  751. TX_STATUS_FAIL(STA_INVALID);
  752. TX_STATUS_FAIL(FRAG_DROPPED);
  753. TX_STATUS_FAIL(TID_DISABLE);
  754. TX_STATUS_FAIL(FIFO_FLUSHED);
  755. TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
  756. TX_STATUS_FAIL(PASSIVE_NO_RX);
  757. TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
  758. }
  759. return "UNKNOWN";
  760. #undef TX_STATUS_FAIL
  761. #undef TX_STATUS_POSTPONE
  762. }
  763. #endif /* CONFIG_IWLWIFI_DEBUG */
  764. static void iwlagn_count_agg_tx_err_status(struct iwl_priv *priv, u16 status)
  765. {
  766. status &= AGG_TX_STATUS_MSK;
  767. switch (status) {
  768. case AGG_TX_STATE_UNDERRUN_MSK:
  769. priv->reply_agg_tx_stats.underrun++;
  770. break;
  771. case AGG_TX_STATE_BT_PRIO_MSK:
  772. priv->reply_agg_tx_stats.bt_prio++;
  773. break;
  774. case AGG_TX_STATE_FEW_BYTES_MSK:
  775. priv->reply_agg_tx_stats.few_bytes++;
  776. break;
  777. case AGG_TX_STATE_ABORT_MSK:
  778. priv->reply_agg_tx_stats.abort++;
  779. break;
  780. case AGG_TX_STATE_LAST_SENT_TTL_MSK:
  781. priv->reply_agg_tx_stats.last_sent_ttl++;
  782. break;
  783. case AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK:
  784. priv->reply_agg_tx_stats.last_sent_try++;
  785. break;
  786. case AGG_TX_STATE_LAST_SENT_BT_KILL_MSK:
  787. priv->reply_agg_tx_stats.last_sent_bt_kill++;
  788. break;
  789. case AGG_TX_STATE_SCD_QUERY_MSK:
  790. priv->reply_agg_tx_stats.scd_query++;
  791. break;
  792. case AGG_TX_STATE_TEST_BAD_CRC32_MSK:
  793. priv->reply_agg_tx_stats.bad_crc32++;
  794. break;
  795. case AGG_TX_STATE_RESPONSE_MSK:
  796. priv->reply_agg_tx_stats.response++;
  797. break;
  798. case AGG_TX_STATE_DUMP_TX_MSK:
  799. priv->reply_agg_tx_stats.dump_tx++;
  800. break;
  801. case AGG_TX_STATE_DELAY_TX_MSK:
  802. priv->reply_agg_tx_stats.delay_tx++;
  803. break;
  804. default:
  805. priv->reply_agg_tx_stats.unknown++;
  806. break;
  807. }
  808. }
  809. static inline u32 iwlagn_get_scd_ssn(struct iwlagn_tx_resp *tx_resp)
  810. {
  811. return le32_to_cpup((__le32 *)&tx_resp->status +
  812. tx_resp->frame_count) & IEEE80211_MAX_SN;
  813. }
  814. static void iwl_rx_reply_tx_agg(struct iwl_priv *priv,
  815. struct iwlagn_tx_resp *tx_resp)
  816. {
  817. struct agg_tx_status *frame_status = &tx_resp->status;
  818. int tid = (tx_resp->ra_tid & IWLAGN_TX_RES_TID_MSK) >>
  819. IWLAGN_TX_RES_TID_POS;
  820. int sta_id = (tx_resp->ra_tid & IWLAGN_TX_RES_RA_MSK) >>
  821. IWLAGN_TX_RES_RA_POS;
  822. struct iwl_ht_agg *agg = &priv->tid_data[sta_id][tid].agg;
  823. u32 status = le16_to_cpu(tx_resp->status.status);
  824. int i;
  825. WARN_ON(tid == IWL_TID_NON_QOS);
  826. if (agg->wait_for_ba)
  827. IWL_DEBUG_TX_REPLY(priv,
  828. "got tx response w/o block-ack\n");
  829. agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  830. agg->wait_for_ba = (tx_resp->frame_count > 1);
  831. /*
  832. * If the BT kill count is non-zero, we'll get this
  833. * notification again.
  834. */
  835. if (tx_resp->bt_kill_count && tx_resp->frame_count == 1 &&
  836. priv->lib->bt_params &&
  837. priv->lib->bt_params->advanced_bt_coexist) {
  838. IWL_DEBUG_COEX(priv, "receive reply tx w/ bt_kill\n");
  839. }
  840. if (tx_resp->frame_count == 1)
  841. return;
  842. IWL_DEBUG_TX_REPLY(priv, "TXQ %d initial_rate 0x%x ssn %d frm_cnt %d\n",
  843. agg->txq_id,
  844. le32_to_cpu(tx_resp->rate_n_flags),
  845. iwlagn_get_scd_ssn(tx_resp), tx_resp->frame_count);
  846. /* Construct bit-map of pending frames within Tx window */
  847. for (i = 0; i < tx_resp->frame_count; i++) {
  848. u16 fstatus = le16_to_cpu(frame_status[i].status);
  849. u8 retry_cnt = (fstatus & AGG_TX_TRY_MSK) >> AGG_TX_TRY_POS;
  850. if (status & AGG_TX_STATUS_MSK)
  851. iwlagn_count_agg_tx_err_status(priv, fstatus);
  852. if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
  853. AGG_TX_STATE_ABORT_MSK))
  854. continue;
  855. if (status & AGG_TX_STATUS_MSK || retry_cnt > 1)
  856. IWL_DEBUG_TX_REPLY(priv,
  857. "%d: status %s (0x%04x), try-count (0x%01x)\n",
  858. i,
  859. iwl_get_agg_tx_fail_reason(fstatus),
  860. fstatus & AGG_TX_STATUS_MSK,
  861. retry_cnt);
  862. }
  863. }
  864. #ifdef CONFIG_IWLWIFI_DEBUG
  865. #define AGG_TX_STATE_FAIL(x) case AGG_TX_STATE_ ## x: return #x
  866. const char *iwl_get_agg_tx_fail_reason(u16 status)
  867. {
  868. status &= AGG_TX_STATUS_MSK;
  869. switch (status) {
  870. case AGG_TX_STATE_TRANSMITTED:
  871. return "SUCCESS";
  872. AGG_TX_STATE_FAIL(UNDERRUN_MSK);
  873. AGG_TX_STATE_FAIL(BT_PRIO_MSK);
  874. AGG_TX_STATE_FAIL(FEW_BYTES_MSK);
  875. AGG_TX_STATE_FAIL(ABORT_MSK);
  876. AGG_TX_STATE_FAIL(LAST_SENT_TTL_MSK);
  877. AGG_TX_STATE_FAIL(LAST_SENT_TRY_CNT_MSK);
  878. AGG_TX_STATE_FAIL(LAST_SENT_BT_KILL_MSK);
  879. AGG_TX_STATE_FAIL(SCD_QUERY_MSK);
  880. AGG_TX_STATE_FAIL(TEST_BAD_CRC32_MSK);
  881. AGG_TX_STATE_FAIL(RESPONSE_MSK);
  882. AGG_TX_STATE_FAIL(DUMP_TX_MSK);
  883. AGG_TX_STATE_FAIL(DELAY_TX_MSK);
  884. }
  885. return "UNKNOWN";
  886. }
  887. #endif /* CONFIG_IWLWIFI_DEBUG */
  888. static void iwlagn_count_tx_err_status(struct iwl_priv *priv, u16 status)
  889. {
  890. status &= TX_STATUS_MSK;
  891. switch (status) {
  892. case TX_STATUS_POSTPONE_DELAY:
  893. priv->reply_tx_stats.pp_delay++;
  894. break;
  895. case TX_STATUS_POSTPONE_FEW_BYTES:
  896. priv->reply_tx_stats.pp_few_bytes++;
  897. break;
  898. case TX_STATUS_POSTPONE_BT_PRIO:
  899. priv->reply_tx_stats.pp_bt_prio++;
  900. break;
  901. case TX_STATUS_POSTPONE_QUIET_PERIOD:
  902. priv->reply_tx_stats.pp_quiet_period++;
  903. break;
  904. case TX_STATUS_POSTPONE_CALC_TTAK:
  905. priv->reply_tx_stats.pp_calc_ttak++;
  906. break;
  907. case TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY:
  908. priv->reply_tx_stats.int_crossed_retry++;
  909. break;
  910. case TX_STATUS_FAIL_SHORT_LIMIT:
  911. priv->reply_tx_stats.short_limit++;
  912. break;
  913. case TX_STATUS_FAIL_LONG_LIMIT:
  914. priv->reply_tx_stats.long_limit++;
  915. break;
  916. case TX_STATUS_FAIL_FIFO_UNDERRUN:
  917. priv->reply_tx_stats.fifo_underrun++;
  918. break;
  919. case TX_STATUS_FAIL_DRAIN_FLOW:
  920. priv->reply_tx_stats.drain_flow++;
  921. break;
  922. case TX_STATUS_FAIL_RFKILL_FLUSH:
  923. priv->reply_tx_stats.rfkill_flush++;
  924. break;
  925. case TX_STATUS_FAIL_LIFE_EXPIRE:
  926. priv->reply_tx_stats.life_expire++;
  927. break;
  928. case TX_STATUS_FAIL_DEST_PS:
  929. priv->reply_tx_stats.dest_ps++;
  930. break;
  931. case TX_STATUS_FAIL_HOST_ABORTED:
  932. priv->reply_tx_stats.host_abort++;
  933. break;
  934. case TX_STATUS_FAIL_BT_RETRY:
  935. priv->reply_tx_stats.bt_retry++;
  936. break;
  937. case TX_STATUS_FAIL_STA_INVALID:
  938. priv->reply_tx_stats.sta_invalid++;
  939. break;
  940. case TX_STATUS_FAIL_FRAG_DROPPED:
  941. priv->reply_tx_stats.frag_drop++;
  942. break;
  943. case TX_STATUS_FAIL_TID_DISABLE:
  944. priv->reply_tx_stats.tid_disable++;
  945. break;
  946. case TX_STATUS_FAIL_FIFO_FLUSHED:
  947. priv->reply_tx_stats.fifo_flush++;
  948. break;
  949. case TX_STATUS_FAIL_INSUFFICIENT_CF_POLL:
  950. priv->reply_tx_stats.insuff_cf_poll++;
  951. break;
  952. case TX_STATUS_FAIL_PASSIVE_NO_RX:
  953. priv->reply_tx_stats.fail_hw_drop++;
  954. break;
  955. case TX_STATUS_FAIL_NO_BEACON_ON_RADAR:
  956. priv->reply_tx_stats.sta_color_mismatch++;
  957. break;
  958. default:
  959. priv->reply_tx_stats.unknown++;
  960. break;
  961. }
  962. }
  963. static void iwlagn_set_tx_status(struct iwl_priv *priv,
  964. struct ieee80211_tx_info *info,
  965. struct iwlagn_tx_resp *tx_resp)
  966. {
  967. u16 status = le16_to_cpu(tx_resp->status.status);
  968. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  969. info->status.rates[0].count = tx_resp->failure_frame + 1;
  970. info->flags |= iwl_tx_status_to_mac80211(status);
  971. iwlagn_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
  972. info);
  973. if (!iwl_is_tx_success(status))
  974. iwlagn_count_tx_err_status(priv, status);
  975. }
  976. static void iwl_check_abort_status(struct iwl_priv *priv,
  977. u8 frame_count, u32 status)
  978. {
  979. if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
  980. IWL_ERR(priv, "Tx flush command to flush out all frames\n");
  981. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  982. queue_work(priv->workqueue, &priv->tx_flush);
  983. }
  984. }
  985. int iwlagn_rx_reply_tx(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
  986. struct iwl_device_cmd *cmd)
  987. {
  988. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  989. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  990. int txq_id = SEQ_TO_QUEUE(sequence);
  991. int cmd_index __maybe_unused = SEQ_TO_INDEX(sequence);
  992. struct iwlagn_tx_resp *tx_resp = (void *)pkt->data;
  993. struct ieee80211_hdr *hdr;
  994. u32 status = le16_to_cpu(tx_resp->status.status);
  995. u16 ssn = iwlagn_get_scd_ssn(tx_resp);
  996. int tid;
  997. int sta_id;
  998. int freed;
  999. struct ieee80211_tx_info *info;
  1000. struct sk_buff_head skbs;
  1001. struct sk_buff *skb;
  1002. struct iwl_rxon_context *ctx;
  1003. bool is_agg = (txq_id >= IWLAGN_FIRST_AMPDU_QUEUE);
  1004. tid = (tx_resp->ra_tid & IWLAGN_TX_RES_TID_MSK) >>
  1005. IWLAGN_TX_RES_TID_POS;
  1006. sta_id = (tx_resp->ra_tid & IWLAGN_TX_RES_RA_MSK) >>
  1007. IWLAGN_TX_RES_RA_POS;
  1008. spin_lock_bh(&priv->sta_lock);
  1009. if (is_agg) {
  1010. WARN_ON_ONCE(sta_id >= IWLAGN_STATION_COUNT ||
  1011. tid >= IWL_MAX_TID_COUNT);
  1012. if (txq_id != priv->tid_data[sta_id][tid].agg.txq_id)
  1013. IWL_ERR(priv, "txq_id mismatch: %d %d\n", txq_id,
  1014. priv->tid_data[sta_id][tid].agg.txq_id);
  1015. iwl_rx_reply_tx_agg(priv, tx_resp);
  1016. }
  1017. __skb_queue_head_init(&skbs);
  1018. if (tx_resp->frame_count == 1) {
  1019. u16 next_reclaimed = le16_to_cpu(tx_resp->seq_ctl);
  1020. next_reclaimed = IEEE80211_SEQ_TO_SN(next_reclaimed + 0x10);
  1021. if (is_agg) {
  1022. /* If this is an aggregation queue, we can rely on the
  1023. * ssn since the wifi sequence number corresponds to
  1024. * the index in the TFD ring (%256).
  1025. * The seq_ctl is the sequence control of the packet
  1026. * to which this Tx response relates. But if there is a
  1027. * hole in the bitmap of the BA we received, this Tx
  1028. * response may allow to reclaim the hole and all the
  1029. * subsequent packets that were already acked.
  1030. * In that case, seq_ctl != ssn, and the next packet
  1031. * to be reclaimed will be ssn and not seq_ctl.
  1032. */
  1033. next_reclaimed = ssn;
  1034. }
  1035. if (tid != IWL_TID_NON_QOS) {
  1036. priv->tid_data[sta_id][tid].next_reclaimed =
  1037. next_reclaimed;
  1038. IWL_DEBUG_TX_REPLY(priv, "Next reclaimed packet:%d\n",
  1039. next_reclaimed);
  1040. }
  1041. iwl_trans_reclaim(priv->trans, txq_id, ssn, &skbs);
  1042. iwlagn_check_ratid_empty(priv, sta_id, tid);
  1043. freed = 0;
  1044. /* process frames */
  1045. skb_queue_walk(&skbs, skb) {
  1046. hdr = (struct ieee80211_hdr *)skb->data;
  1047. if (!ieee80211_is_data_qos(hdr->frame_control))
  1048. priv->last_seq_ctl = tx_resp->seq_ctl;
  1049. info = IEEE80211_SKB_CB(skb);
  1050. ctx = info->driver_data[0];
  1051. iwl_trans_free_tx_cmd(priv->trans,
  1052. info->driver_data[1]);
  1053. memset(&info->status, 0, sizeof(info->status));
  1054. if (status == TX_STATUS_FAIL_PASSIVE_NO_RX &&
  1055. ctx->vif &&
  1056. ctx->vif->type == NL80211_IFTYPE_STATION) {
  1057. /* block and stop all queues */
  1058. priv->passive_no_rx = true;
  1059. IWL_DEBUG_TX_QUEUES(priv, "stop all queues: "
  1060. "passive channel");
  1061. ieee80211_stop_queues(priv->hw);
  1062. IWL_DEBUG_TX_REPLY(priv,
  1063. "TXQ %d status %s (0x%08x) "
  1064. "rate_n_flags 0x%x retries %d\n",
  1065. txq_id,
  1066. iwl_get_tx_fail_reason(status),
  1067. status,
  1068. le32_to_cpu(tx_resp->rate_n_flags),
  1069. tx_resp->failure_frame);
  1070. IWL_DEBUG_TX_REPLY(priv,
  1071. "FrameCnt = %d, idx=%d\n",
  1072. tx_resp->frame_count, cmd_index);
  1073. }
  1074. /* check if BAR is needed */
  1075. if (is_agg && !iwl_is_tx_success(status))
  1076. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  1077. iwlagn_set_tx_status(priv, IEEE80211_SKB_CB(skb),
  1078. tx_resp);
  1079. if (!is_agg)
  1080. iwlagn_non_agg_tx_status(priv, ctx, hdr->addr1);
  1081. freed++;
  1082. }
  1083. if (tid != IWL_TID_NON_QOS) {
  1084. priv->tid_data[sta_id][tid].next_reclaimed =
  1085. next_reclaimed;
  1086. IWL_DEBUG_TX_REPLY(priv, "Next reclaimed packet:%d\n",
  1087. next_reclaimed);
  1088. }
  1089. if (!is_agg && freed != 1)
  1090. IWL_ERR(priv, "Q: %d, freed %d\n", txq_id, freed);
  1091. IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x)\n", txq_id,
  1092. iwl_get_tx_fail_reason(status), status);
  1093. IWL_DEBUG_TX_REPLY(priv,
  1094. "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d seq_ctl=0x%x\n",
  1095. le32_to_cpu(tx_resp->rate_n_flags),
  1096. tx_resp->failure_frame,
  1097. SEQ_TO_INDEX(sequence), ssn,
  1098. le16_to_cpu(tx_resp->seq_ctl));
  1099. }
  1100. iwl_check_abort_status(priv, tx_resp->frame_count, status);
  1101. spin_unlock_bh(&priv->sta_lock);
  1102. while (!skb_queue_empty(&skbs)) {
  1103. skb = __skb_dequeue(&skbs);
  1104. ieee80211_tx_status_ni(priv->hw, skb);
  1105. }
  1106. return 0;
  1107. }
  1108. /**
  1109. * iwlagn_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  1110. *
  1111. * Handles block-acknowledge notification from device, which reports success
  1112. * of frames sent via aggregation.
  1113. */
  1114. int iwlagn_rx_reply_compressed_ba(struct iwl_priv *priv,
  1115. struct iwl_rx_cmd_buffer *rxb,
  1116. struct iwl_device_cmd *cmd)
  1117. {
  1118. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1119. struct iwl_compressed_ba_resp *ba_resp = (void *)pkt->data;
  1120. struct iwl_ht_agg *agg;
  1121. struct sk_buff_head reclaimed_skbs;
  1122. struct ieee80211_tx_info *info;
  1123. struct ieee80211_hdr *hdr;
  1124. struct sk_buff *skb;
  1125. int sta_id;
  1126. int tid;
  1127. int freed;
  1128. /* "flow" corresponds to Tx queue */
  1129. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1130. /* "ssn" is start of block-ack Tx window, corresponds to index
  1131. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1132. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  1133. if (scd_flow >= priv->cfg->base_params->num_of_queues) {
  1134. IWL_ERR(priv,
  1135. "BUG_ON scd_flow is bigger than number of queues\n");
  1136. return 0;
  1137. }
  1138. sta_id = ba_resp->sta_id;
  1139. tid = ba_resp->tid;
  1140. agg = &priv->tid_data[sta_id][tid].agg;
  1141. spin_lock_bh(&priv->sta_lock);
  1142. if (unlikely(!agg->wait_for_ba)) {
  1143. if (unlikely(ba_resp->bitmap))
  1144. IWL_ERR(priv, "Received BA when not expected\n");
  1145. spin_unlock_bh(&priv->sta_lock);
  1146. return 0;
  1147. }
  1148. if (unlikely(scd_flow != agg->txq_id)) {
  1149. /*
  1150. * FIXME: this is a uCode bug which need to be addressed,
  1151. * log the information and return for now.
  1152. * Since it is can possibly happen very often and in order
  1153. * not to fill the syslog, don't use IWL_ERR or IWL_WARN
  1154. */
  1155. IWL_DEBUG_TX_QUEUES(priv,
  1156. "Bad queue mapping txq_id=%d, agg_txq[sta:%d,tid:%d]=%d\n",
  1157. scd_flow, sta_id, tid, agg->txq_id);
  1158. spin_unlock_bh(&priv->sta_lock);
  1159. return 0;
  1160. }
  1161. __skb_queue_head_init(&reclaimed_skbs);
  1162. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  1163. * block-ack window (we assume that they've been successfully
  1164. * transmitted ... if not, it's too late anyway). */
  1165. iwl_trans_reclaim(priv->trans, scd_flow, ba_resp_scd_ssn,
  1166. &reclaimed_skbs);
  1167. IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, "
  1168. "sta_id = %d\n",
  1169. agg->wait_for_ba,
  1170. (u8 *) &ba_resp->sta_addr_lo32,
  1171. ba_resp->sta_id);
  1172. IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx, "
  1173. "scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
  1174. ba_resp->tid, le16_to_cpu(ba_resp->seq_ctl),
  1175. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  1176. scd_flow, ba_resp_scd_ssn, ba_resp->txed,
  1177. ba_resp->txed_2_done);
  1178. /* Mark that the expected block-ack response arrived */
  1179. agg->wait_for_ba = false;
  1180. /* Sanity check values reported by uCode */
  1181. if (ba_resp->txed_2_done > ba_resp->txed) {
  1182. IWL_DEBUG_TX_REPLY(priv,
  1183. "bogus sent(%d) and ack(%d) count\n",
  1184. ba_resp->txed, ba_resp->txed_2_done);
  1185. /*
  1186. * set txed_2_done = txed,
  1187. * so it won't impact rate scale
  1188. */
  1189. ba_resp->txed = ba_resp->txed_2_done;
  1190. }
  1191. priv->tid_data[sta_id][tid].next_reclaimed = ba_resp_scd_ssn;
  1192. iwlagn_check_ratid_empty(priv, sta_id, tid);
  1193. freed = 0;
  1194. skb_queue_walk(&reclaimed_skbs, skb) {
  1195. hdr = (struct ieee80211_hdr *)skb->data;
  1196. if (ieee80211_is_data_qos(hdr->frame_control))
  1197. freed++;
  1198. else
  1199. WARN_ON_ONCE(1);
  1200. info = IEEE80211_SKB_CB(skb);
  1201. iwl_trans_free_tx_cmd(priv->trans, info->driver_data[1]);
  1202. if (freed == 1) {
  1203. /* this is the first skb we deliver in this batch */
  1204. /* put the rate scaling data there */
  1205. info = IEEE80211_SKB_CB(skb);
  1206. memset(&info->status, 0, sizeof(info->status));
  1207. info->flags |= IEEE80211_TX_STAT_ACK;
  1208. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1209. info->status.ampdu_ack_len = ba_resp->txed_2_done;
  1210. info->status.ampdu_len = ba_resp->txed;
  1211. iwlagn_hwrate_to_tx_control(priv, agg->rate_n_flags,
  1212. info);
  1213. }
  1214. }
  1215. spin_unlock_bh(&priv->sta_lock);
  1216. while (!skb_queue_empty(&reclaimed_skbs)) {
  1217. skb = __skb_dequeue(&reclaimed_skbs);
  1218. ieee80211_tx_status_ni(priv->hw, skb);
  1219. }
  1220. return 0;
  1221. }