iwl-agn-tx.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 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 LICENSE.GPL.
  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 "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-sta.h"
  36. #include "iwl-io.h"
  37. #include "iwl-helpers.h"
  38. #include "iwl-agn-hw.h"
  39. #include "iwl-agn.h"
  40. #include "iwl-trans.h"
  41. /*
  42. * mac80211 queues, ACs, hardware queues, FIFOs.
  43. *
  44. * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
  45. *
  46. * Mac80211 uses the following numbers, which we get as from it
  47. * by way of skb_get_queue_mapping(skb):
  48. *
  49. * VO 0
  50. * VI 1
  51. * BE 2
  52. * BK 3
  53. *
  54. *
  55. * Regular (not A-MPDU) frames are put into hardware queues corresponding
  56. * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
  57. * own queue per aggregation session (RA/TID combination), such queues are
  58. * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
  59. * order to map frames to the right queue, we also need an AC->hw queue
  60. * mapping. This is implemented here.
  61. *
  62. * Due to the way hw queues are set up (by the hw specific modules like
  63. * iwl-4965.c, iwl-5000.c etc.), the AC->hw queue mapping is the identity
  64. * mapping.
  65. */
  66. static const u8 tid_to_ac[] = {
  67. IEEE80211_AC_BE,
  68. IEEE80211_AC_BK,
  69. IEEE80211_AC_BK,
  70. IEEE80211_AC_BE,
  71. IEEE80211_AC_VI,
  72. IEEE80211_AC_VI,
  73. IEEE80211_AC_VO,
  74. IEEE80211_AC_VO
  75. };
  76. static inline int get_ac_from_tid(u16 tid)
  77. {
  78. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  79. return tid_to_ac[tid];
  80. /* no support for TIDs 8-15 yet */
  81. return -EINVAL;
  82. }
  83. static inline int get_fifo_from_tid(struct iwl_rxon_context *ctx, u16 tid)
  84. {
  85. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  86. return ctx->ac_to_fifo[tid_to_ac[tid]];
  87. /* no support for TIDs 8-15 yet */
  88. return -EINVAL;
  89. }
  90. static int iwlagn_txq_agg_enable(struct iwl_priv *priv, int txq_id, int sta_id,
  91. int tid)
  92. {
  93. if ((IWLAGN_FIRST_AMPDU_QUEUE > txq_id) ||
  94. (IWLAGN_FIRST_AMPDU_QUEUE +
  95. priv->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  96. IWL_WARN(priv,
  97. "queue number out of range: %d, must be %d to %d\n",
  98. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  99. IWLAGN_FIRST_AMPDU_QUEUE +
  100. priv->cfg->base_params->num_of_ampdu_queues - 1);
  101. return -EINVAL;
  102. }
  103. /* Modify device's station table to Tx this TID */
  104. return iwl_sta_tx_modify_enable_tid(priv, sta_id, tid);
  105. }
  106. static void iwlagn_tx_cmd_protection(struct iwl_priv *priv,
  107. struct ieee80211_tx_info *info,
  108. __le16 fc, __le32 *tx_flags)
  109. {
  110. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS ||
  111. info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT ||
  112. info->flags & IEEE80211_TX_CTL_AMPDU)
  113. *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
  114. }
  115. /*
  116. * handle build REPLY_TX command notification.
  117. */
  118. static void iwlagn_tx_cmd_build_basic(struct iwl_priv *priv,
  119. struct sk_buff *skb,
  120. struct iwl_tx_cmd *tx_cmd,
  121. struct ieee80211_tx_info *info,
  122. struct ieee80211_hdr *hdr,
  123. u8 std_id)
  124. {
  125. __le16 fc = hdr->frame_control;
  126. __le32 tx_flags = tx_cmd->tx_flags;
  127. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  128. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  129. tx_flags |= TX_CMD_FLG_ACK_MSK;
  130. else
  131. tx_flags &= ~TX_CMD_FLG_ACK_MSK;
  132. if (ieee80211_is_probe_resp(fc))
  133. tx_flags |= TX_CMD_FLG_TSF_MSK;
  134. else if (ieee80211_is_back_req(fc))
  135. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  136. else if (info->band == IEEE80211_BAND_2GHZ &&
  137. priv->cfg->bt_params &&
  138. priv->cfg->bt_params->advanced_bt_coexist &&
  139. (ieee80211_is_auth(fc) || ieee80211_is_assoc_req(fc) ||
  140. ieee80211_is_reassoc_req(fc) ||
  141. skb->protocol == cpu_to_be16(ETH_P_PAE)))
  142. tx_flags |= TX_CMD_FLG_IGNORE_BT;
  143. tx_cmd->sta_id = std_id;
  144. if (ieee80211_has_morefrags(fc))
  145. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  146. if (ieee80211_is_data_qos(fc)) {
  147. u8 *qc = ieee80211_get_qos_ctl(hdr);
  148. tx_cmd->tid_tspec = qc[0] & 0xf;
  149. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  150. } else {
  151. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  152. }
  153. iwlagn_tx_cmd_protection(priv, info, fc, &tx_flags);
  154. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  155. if (ieee80211_is_mgmt(fc)) {
  156. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  157. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  158. else
  159. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  160. } else {
  161. tx_cmd->timeout.pm_frame_timeout = 0;
  162. }
  163. tx_cmd->driver_txop = 0;
  164. tx_cmd->tx_flags = tx_flags;
  165. tx_cmd->next_frame_len = 0;
  166. }
  167. #define RTS_DFAULT_RETRY_LIMIT 60
  168. static void iwlagn_tx_cmd_build_rate(struct iwl_priv *priv,
  169. struct iwl_tx_cmd *tx_cmd,
  170. struct ieee80211_tx_info *info,
  171. __le16 fc)
  172. {
  173. u32 rate_flags;
  174. int rate_idx;
  175. u8 rts_retry_limit;
  176. u8 data_retry_limit;
  177. u8 rate_plcp;
  178. /* Set retry limit on DATA packets and Probe Responses*/
  179. if (ieee80211_is_probe_resp(fc))
  180. data_retry_limit = 3;
  181. else
  182. data_retry_limit = IWLAGN_DEFAULT_TX_RETRY;
  183. tx_cmd->data_retry_limit = data_retry_limit;
  184. /* Set retry limit on RTS packets */
  185. rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
  186. if (data_retry_limit < rts_retry_limit)
  187. rts_retry_limit = data_retry_limit;
  188. tx_cmd->rts_retry_limit = rts_retry_limit;
  189. /* DATA packets will use the uCode station table for rate/antenna
  190. * selection */
  191. if (ieee80211_is_data(fc)) {
  192. tx_cmd->initial_rate_index = 0;
  193. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  194. if (priv->tm_fixed_rate) {
  195. /*
  196. * rate overwrite by testmode
  197. * we not only send lq command to change rate
  198. * we also re-enforce per data pkt base.
  199. */
  200. tx_cmd->tx_flags &= ~TX_CMD_FLG_STA_RATE_MSK;
  201. memcpy(&tx_cmd->rate_n_flags, &priv->tm_fixed_rate,
  202. sizeof(tx_cmd->rate_n_flags));
  203. }
  204. return;
  205. }
  206. /**
  207. * If the current TX rate stored in mac80211 has the MCS bit set, it's
  208. * not really a TX rate. Thus, we use the lowest supported rate for
  209. * this band. Also use the lowest supported rate if the stored rate
  210. * index is invalid.
  211. */
  212. rate_idx = info->control.rates[0].idx;
  213. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS ||
  214. (rate_idx < 0) || (rate_idx > IWL_RATE_COUNT_LEGACY))
  215. rate_idx = rate_lowest_index(&priv->bands[info->band],
  216. info->control.sta);
  217. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  218. if (info->band == IEEE80211_BAND_5GHZ)
  219. rate_idx += IWL_FIRST_OFDM_RATE;
  220. /* Get PLCP rate for tx_cmd->rate_n_flags */
  221. rate_plcp = iwl_rates[rate_idx].plcp;
  222. /* Zero out flags for this packet */
  223. rate_flags = 0;
  224. /* Set CCK flag as needed */
  225. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  226. rate_flags |= RATE_MCS_CCK_MSK;
  227. /* Set up antennas */
  228. if (priv->cfg->bt_params &&
  229. priv->cfg->bt_params->advanced_bt_coexist &&
  230. priv->bt_full_concurrent) {
  231. /* operated as 1x1 in full concurrency mode */
  232. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  233. first_antenna(priv->hw_params.valid_tx_ant));
  234. } else
  235. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  236. priv->hw_params.valid_tx_ant);
  237. rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
  238. /* Set the rate in the TX cmd */
  239. tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
  240. }
  241. static void iwlagn_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  242. struct ieee80211_tx_info *info,
  243. struct iwl_tx_cmd *tx_cmd,
  244. struct sk_buff *skb_frag,
  245. int sta_id)
  246. {
  247. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  248. switch (keyconf->cipher) {
  249. case WLAN_CIPHER_SUITE_CCMP:
  250. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  251. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  252. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  253. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  254. IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
  255. break;
  256. case WLAN_CIPHER_SUITE_TKIP:
  257. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  258. ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
  259. IWL_DEBUG_TX(priv, "tx_cmd with tkip hwcrypto\n");
  260. break;
  261. case WLAN_CIPHER_SUITE_WEP104:
  262. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  263. /* fall through */
  264. case WLAN_CIPHER_SUITE_WEP40:
  265. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  266. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  267. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  268. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  269. "with key %d\n", keyconf->keyidx);
  270. break;
  271. default:
  272. IWL_ERR(priv, "Unknown encode cipher %x\n", keyconf->cipher);
  273. break;
  274. }
  275. }
  276. /*
  277. * start REPLY_TX command process
  278. */
  279. int iwlagn_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  280. {
  281. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  282. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  283. struct iwl_station_priv *sta_priv = NULL;
  284. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  285. struct iwl_tx_cmd *tx_cmd;
  286. int txq_id;
  287. u16 seq_number = 0;
  288. __le16 fc;
  289. u8 hdr_len;
  290. u16 len;
  291. u8 sta_id;
  292. u8 tid = 0;
  293. unsigned long flags;
  294. bool is_agg = false;
  295. /*
  296. * If the frame needs to go out off-channel, then
  297. * we'll have put the PAN context to that channel,
  298. * so make the frame go out there.
  299. */
  300. if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
  301. ctx = &priv->contexts[IWL_RXON_CTX_PAN];
  302. else if (info->control.vif)
  303. ctx = iwl_rxon_ctx_from_vif(info->control.vif);
  304. spin_lock_irqsave(&priv->lock, flags);
  305. if (iwl_is_rfkill(priv)) {
  306. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  307. goto drop_unlock_priv;
  308. }
  309. fc = hdr->frame_control;
  310. #ifdef CONFIG_IWLWIFI_DEBUG
  311. if (ieee80211_is_auth(fc))
  312. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  313. else if (ieee80211_is_assoc_req(fc))
  314. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  315. else if (ieee80211_is_reassoc_req(fc))
  316. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  317. #endif
  318. hdr_len = ieee80211_hdrlen(fc);
  319. /* For management frames use broadcast id to do not break aggregation */
  320. if (!ieee80211_is_data(fc))
  321. sta_id = ctx->bcast_sta_id;
  322. else {
  323. /* Find index into station table for destination station */
  324. sta_id = iwl_sta_id_or_broadcast(priv, ctx, info->control.sta);
  325. if (sta_id == IWL_INVALID_STATION) {
  326. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  327. hdr->addr1);
  328. goto drop_unlock_priv;
  329. }
  330. }
  331. IWL_DEBUG_TX(priv, "station Id %d\n", sta_id);
  332. if (info->control.sta)
  333. sta_priv = (void *)info->control.sta->drv_priv;
  334. if (sta_priv && sta_priv->asleep &&
  335. (info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE)) {
  336. /*
  337. * This sends an asynchronous command to the device,
  338. * but we can rely on it being processed before the
  339. * next frame is processed -- and the next frame to
  340. * this station is the one that will consume this
  341. * counter.
  342. * For now set the counter to just 1 since we do not
  343. * support uAPSD yet.
  344. */
  345. iwl_sta_modify_sleep_tx_count(priv, sta_id, 1);
  346. }
  347. /*
  348. * Send this frame after DTIM -- there's a special queue
  349. * reserved for this for contexts that support AP mode.
  350. */
  351. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  352. txq_id = ctx->mcast_queue;
  353. /*
  354. * The microcode will clear the more data
  355. * bit in the last frame it transmits.
  356. */
  357. hdr->frame_control |=
  358. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  359. } else
  360. txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
  361. /* irqs already disabled/saved above when locking priv->lock */
  362. spin_lock(&priv->sta_lock);
  363. if (ieee80211_is_data_qos(fc)) {
  364. u8 *qc = NULL;
  365. qc = ieee80211_get_qos_ctl(hdr);
  366. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  367. if (WARN_ON_ONCE(tid >= MAX_TID_COUNT))
  368. goto drop_unlock_sta;
  369. seq_number = priv->stations[sta_id].tid[tid].seq_number;
  370. seq_number &= IEEE80211_SCTL_SEQ;
  371. hdr->seq_ctrl = hdr->seq_ctrl &
  372. cpu_to_le16(IEEE80211_SCTL_FRAG);
  373. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  374. seq_number += 0x10;
  375. /* aggregation is on for this <sta,tid> */
  376. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  377. priv->stations[sta_id].tid[tid].agg.state == IWL_AGG_ON) {
  378. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  379. is_agg = true;
  380. }
  381. }
  382. tx_cmd = trans_get_tx_cmd(&priv->trans, txq_id);
  383. if (unlikely(!tx_cmd))
  384. goto drop_unlock_sta;
  385. /* Copy MAC header from skb into command buffer */
  386. memcpy(tx_cmd->hdr, hdr, hdr_len);
  387. /* Total # bytes to be transmitted */
  388. len = (u16)skb->len;
  389. tx_cmd->len = cpu_to_le16(len);
  390. if (info->control.hw_key)
  391. iwlagn_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
  392. /* TODO need this for burst mode later on */
  393. iwlagn_tx_cmd_build_basic(priv, skb, tx_cmd, info, hdr, sta_id);
  394. iwl_dbg_log_tx_data_frame(priv, len, hdr);
  395. iwlagn_tx_cmd_build_rate(priv, tx_cmd, info, fc);
  396. iwl_update_stats(priv, true, fc, len);
  397. if (trans_tx(&priv->trans, skb, tx_cmd, txq_id, fc, is_agg, ctx))
  398. goto drop_unlock_sta;
  399. if (ieee80211_is_data_qos(fc)) {
  400. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  401. if (!ieee80211_has_morefrags(fc))
  402. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  403. }
  404. spin_unlock(&priv->sta_lock);
  405. spin_unlock_irqrestore(&priv->lock, flags);
  406. /*
  407. * Avoid atomic ops if it isn't an associated client.
  408. * Also, if this is a packet for aggregation, don't
  409. * increase the counter because the ucode will stop
  410. * aggregation queues when their respective station
  411. * goes to sleep.
  412. */
  413. if (sta_priv && sta_priv->client && !is_agg)
  414. atomic_inc(&sta_priv->pending_frames);
  415. return 0;
  416. drop_unlock_sta:
  417. spin_unlock(&priv->sta_lock);
  418. drop_unlock_priv:
  419. spin_unlock_irqrestore(&priv->lock, flags);
  420. return -1;
  421. }
  422. /*
  423. * Find first available (lowest unused) Tx Queue, mark it "active".
  424. * Called only when finding queue for aggregation.
  425. * Should never return anything < 7, because they should already
  426. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  427. */
  428. static int iwlagn_txq_ctx_activate_free(struct iwl_priv *priv)
  429. {
  430. int txq_id;
  431. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  432. if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
  433. return txq_id;
  434. return -1;
  435. }
  436. int iwlagn_tx_agg_start(struct iwl_priv *priv, struct ieee80211_vif *vif,
  437. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  438. {
  439. int sta_id;
  440. int tx_fifo;
  441. int txq_id;
  442. int ret;
  443. unsigned long flags;
  444. struct iwl_tid_data *tid_data;
  445. tx_fifo = get_fifo_from_tid(iwl_rxon_ctx_from_vif(vif), tid);
  446. if (unlikely(tx_fifo < 0))
  447. return tx_fifo;
  448. IWL_DEBUG_HT(priv, "TX AGG request on ra = %pM tid = %d\n",
  449. sta->addr, tid);
  450. sta_id = iwl_sta_id(sta);
  451. if (sta_id == IWL_INVALID_STATION) {
  452. IWL_ERR(priv, "Start AGG on invalid station\n");
  453. return -ENXIO;
  454. }
  455. if (unlikely(tid >= MAX_TID_COUNT))
  456. return -EINVAL;
  457. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
  458. IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n");
  459. return -ENXIO;
  460. }
  461. txq_id = iwlagn_txq_ctx_activate_free(priv);
  462. if (txq_id == -1) {
  463. IWL_ERR(priv, "No free aggregation queue available\n");
  464. return -ENXIO;
  465. }
  466. spin_lock_irqsave(&priv->sta_lock, flags);
  467. tid_data = &priv->stations[sta_id].tid[tid];
  468. *ssn = SEQ_TO_SN(tid_data->seq_number);
  469. tid_data->agg.txq_id = txq_id;
  470. tid_data->agg.tx_fifo = tx_fifo;
  471. iwl_set_swq_id(&priv->txq[txq_id], get_ac_from_tid(tid), txq_id);
  472. spin_unlock_irqrestore(&priv->sta_lock, flags);
  473. ret = iwlagn_txq_agg_enable(priv, txq_id, sta_id, tid);
  474. if (ret)
  475. return ret;
  476. spin_lock_irqsave(&priv->sta_lock, flags);
  477. tid_data = &priv->stations[sta_id].tid[tid];
  478. if (tid_data->tfds_in_queue == 0) {
  479. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  480. tid_data->agg.state = IWL_AGG_ON;
  481. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  482. } else {
  483. IWL_DEBUG_HT(priv, "HW queue is NOT empty: %d packets in HW queue\n",
  484. tid_data->tfds_in_queue);
  485. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  486. }
  487. spin_unlock_irqrestore(&priv->sta_lock, flags);
  488. return ret;
  489. }
  490. int iwlagn_tx_agg_stop(struct iwl_priv *priv, struct ieee80211_vif *vif,
  491. struct ieee80211_sta *sta, u16 tid)
  492. {
  493. int tx_fifo_id, txq_id, sta_id, ssn;
  494. struct iwl_tid_data *tid_data;
  495. int write_ptr, read_ptr;
  496. unsigned long flags;
  497. tx_fifo_id = get_fifo_from_tid(iwl_rxon_ctx_from_vif(vif), tid);
  498. if (unlikely(tx_fifo_id < 0))
  499. return tx_fifo_id;
  500. sta_id = iwl_sta_id(sta);
  501. if (sta_id == IWL_INVALID_STATION) {
  502. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  503. return -ENXIO;
  504. }
  505. spin_lock_irqsave(&priv->sta_lock, flags);
  506. tid_data = &priv->stations[sta_id].tid[tid];
  507. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  508. txq_id = tid_data->agg.txq_id;
  509. switch (priv->stations[sta_id].tid[tid].agg.state) {
  510. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  511. /*
  512. * This can happen if the peer stops aggregation
  513. * again before we've had a chance to drain the
  514. * queue we selected previously, i.e. before the
  515. * session was really started completely.
  516. */
  517. IWL_DEBUG_HT(priv, "AGG stop before setup done\n");
  518. goto turn_off;
  519. case IWL_AGG_ON:
  520. break;
  521. default:
  522. IWL_WARN(priv, "Stopping AGG while state not ON or starting\n");
  523. }
  524. write_ptr = priv->txq[txq_id].q.write_ptr;
  525. read_ptr = priv->txq[txq_id].q.read_ptr;
  526. /* The queue is not empty */
  527. if (write_ptr != read_ptr) {
  528. IWL_DEBUG_HT(priv, "Stopping a non empty AGG HW QUEUE\n");
  529. priv->stations[sta_id].tid[tid].agg.state =
  530. IWL_EMPTYING_HW_QUEUE_DELBA;
  531. spin_unlock_irqrestore(&priv->sta_lock, flags);
  532. return 0;
  533. }
  534. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  535. turn_off:
  536. priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
  537. /* do not restore/save irqs */
  538. spin_unlock(&priv->sta_lock);
  539. spin_lock(&priv->lock);
  540. /*
  541. * the only reason this call can fail is queue number out of range,
  542. * which can happen if uCode is reloaded and all the station
  543. * information are lost. if it is outside the range, there is no need
  544. * to deactivate the uCode queue, just return "success" to allow
  545. * mac80211 to clean up it own data.
  546. */
  547. trans_txq_agg_disable(&priv->trans, txq_id, ssn, tx_fifo_id);
  548. spin_unlock_irqrestore(&priv->lock, flags);
  549. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  550. return 0;
  551. }
  552. int iwlagn_txq_check_empty(struct iwl_priv *priv,
  553. int sta_id, u8 tid, int txq_id)
  554. {
  555. struct iwl_queue *q = &priv->txq[txq_id].q;
  556. u8 *addr = priv->stations[sta_id].sta.sta.addr;
  557. struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
  558. struct iwl_rxon_context *ctx;
  559. ctx = &priv->contexts[priv->stations[sta_id].ctxid];
  560. lockdep_assert_held(&priv->sta_lock);
  561. switch (priv->stations[sta_id].tid[tid].agg.state) {
  562. case IWL_EMPTYING_HW_QUEUE_DELBA:
  563. /* We are reclaiming the last packet of the */
  564. /* aggregated HW queue */
  565. if ((txq_id == tid_data->agg.txq_id) &&
  566. (q->read_ptr == q->write_ptr)) {
  567. u16 ssn = SEQ_TO_SN(tid_data->seq_number);
  568. int tx_fifo = get_fifo_from_tid(ctx, tid);
  569. IWL_DEBUG_HT(priv, "HW queue empty: continue DELBA flow\n");
  570. trans_txq_agg_disable(&priv->trans, txq_id,
  571. ssn, tx_fifo);
  572. tid_data->agg.state = IWL_AGG_OFF;
  573. ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  574. }
  575. break;
  576. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  577. /* We are reclaiming the last packet of the queue */
  578. if (tid_data->tfds_in_queue == 0) {
  579. IWL_DEBUG_HT(priv, "HW queue empty: continue ADDBA flow\n");
  580. tid_data->agg.state = IWL_AGG_ON;
  581. ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  582. }
  583. break;
  584. }
  585. return 0;
  586. }
  587. static void iwlagn_non_agg_tx_status(struct iwl_priv *priv,
  588. struct iwl_rxon_context *ctx,
  589. const u8 *addr1)
  590. {
  591. struct ieee80211_sta *sta;
  592. struct iwl_station_priv *sta_priv;
  593. rcu_read_lock();
  594. sta = ieee80211_find_sta(ctx->vif, addr1);
  595. if (sta) {
  596. sta_priv = (void *)sta->drv_priv;
  597. /* avoid atomic ops if this isn't a client */
  598. if (sta_priv->client &&
  599. atomic_dec_return(&sta_priv->pending_frames) == 0)
  600. ieee80211_sta_block_awake(priv->hw, sta, false);
  601. }
  602. rcu_read_unlock();
  603. }
  604. static void iwlagn_tx_status(struct iwl_priv *priv, struct iwl_tx_info *tx_info,
  605. bool is_agg)
  606. {
  607. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx_info->skb->data;
  608. if (!is_agg)
  609. iwlagn_non_agg_tx_status(priv, tx_info->ctx, hdr->addr1);
  610. ieee80211_tx_status_irqsafe(priv->hw, tx_info->skb);
  611. }
  612. int iwlagn_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  613. {
  614. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  615. struct iwl_queue *q = &txq->q;
  616. struct iwl_tx_info *tx_info;
  617. int nfreed = 0;
  618. struct ieee80211_hdr *hdr;
  619. if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
  620. IWL_ERR(priv, "%s: Read index for DMA queue txq id (%d), "
  621. "index %d is out of range [0-%d] %d %d.\n", __func__,
  622. txq_id, index, q->n_bd, q->write_ptr, q->read_ptr);
  623. return 0;
  624. }
  625. for (index = iwl_queue_inc_wrap(index, q->n_bd);
  626. q->read_ptr != index;
  627. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  628. tx_info = &txq->txb[txq->q.read_ptr];
  629. if (WARN_ON_ONCE(tx_info->skb == NULL))
  630. continue;
  631. hdr = (struct ieee80211_hdr *)tx_info->skb->data;
  632. if (ieee80211_is_data_qos(hdr->frame_control))
  633. nfreed++;
  634. iwlagn_tx_status(priv, tx_info,
  635. txq_id >= IWLAGN_FIRST_AMPDU_QUEUE);
  636. tx_info->skb = NULL;
  637. iwlagn_txq_inval_byte_cnt_tbl(priv, txq);
  638. iwlagn_txq_free_tfd(priv, txq, txq->q.read_ptr);
  639. }
  640. return nfreed;
  641. }
  642. /**
  643. * iwlagn_tx_status_reply_compressed_ba - Update tx status from block-ack
  644. *
  645. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  646. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  647. */
  648. static int iwlagn_tx_status_reply_compressed_ba(struct iwl_priv *priv,
  649. struct iwl_ht_agg *agg,
  650. struct iwl_compressed_ba_resp *ba_resp)
  651. {
  652. int sh;
  653. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  654. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  655. struct ieee80211_tx_info *info;
  656. u64 bitmap, sent_bitmap;
  657. if (unlikely(!agg->wait_for_ba)) {
  658. if (unlikely(ba_resp->bitmap))
  659. IWL_ERR(priv, "Received BA when not expected\n");
  660. return -EINVAL;
  661. }
  662. /* Mark that the expected block-ack response arrived */
  663. agg->wait_for_ba = 0;
  664. IWL_DEBUG_TX_REPLY(priv, "BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
  665. /* Calculate shift to align block-ack bits with our Tx window bits */
  666. sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl >> 4);
  667. if (sh < 0)
  668. sh += 0x100;
  669. /*
  670. * Check for success or failure according to the
  671. * transmitted bitmap and block-ack bitmap
  672. */
  673. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  674. sent_bitmap = bitmap & agg->bitmap;
  675. /* Sanity check values reported by uCode */
  676. if (ba_resp->txed_2_done > ba_resp->txed) {
  677. IWL_DEBUG_TX_REPLY(priv,
  678. "bogus sent(%d) and ack(%d) count\n",
  679. ba_resp->txed, ba_resp->txed_2_done);
  680. /*
  681. * set txed_2_done = txed,
  682. * so it won't impact rate scale
  683. */
  684. ba_resp->txed = ba_resp->txed_2_done;
  685. }
  686. IWL_DEBUG_HT(priv, "agg frames sent:%d, acked:%d\n",
  687. ba_resp->txed, ba_resp->txed_2_done);
  688. /* Find the first ACKed frame to store the TX status */
  689. while (sent_bitmap && !(sent_bitmap & 1)) {
  690. agg->start_idx = (agg->start_idx + 1) & 0xff;
  691. sent_bitmap >>= 1;
  692. }
  693. info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb);
  694. memset(&info->status, 0, sizeof(info->status));
  695. info->flags |= IEEE80211_TX_STAT_ACK;
  696. info->flags |= IEEE80211_TX_STAT_AMPDU;
  697. info->status.ampdu_ack_len = ba_resp->txed_2_done;
  698. info->status.ampdu_len = ba_resp->txed;
  699. iwlagn_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
  700. return 0;
  701. }
  702. /**
  703. * translate ucode response to mac80211 tx status control values
  704. */
  705. void iwlagn_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
  706. struct ieee80211_tx_info *info)
  707. {
  708. struct ieee80211_tx_rate *r = &info->control.rates[0];
  709. info->antenna_sel_tx =
  710. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  711. if (rate_n_flags & RATE_MCS_HT_MSK)
  712. r->flags |= IEEE80211_TX_RC_MCS;
  713. if (rate_n_flags & RATE_MCS_GF_MSK)
  714. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  715. if (rate_n_flags & RATE_MCS_HT40_MSK)
  716. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  717. if (rate_n_flags & RATE_MCS_DUP_MSK)
  718. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  719. if (rate_n_flags & RATE_MCS_SGI_MSK)
  720. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  721. r->idx = iwlagn_hwrate_to_mac80211_idx(rate_n_flags, info->band);
  722. }
  723. /**
  724. * iwlagn_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  725. *
  726. * Handles block-acknowledge notification from device, which reports success
  727. * of frames sent via aggregation.
  728. */
  729. void iwlagn_rx_reply_compressed_ba(struct iwl_priv *priv,
  730. struct iwl_rx_mem_buffer *rxb)
  731. {
  732. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  733. struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  734. struct iwl_tx_queue *txq = NULL;
  735. struct iwl_ht_agg *agg;
  736. int index;
  737. int sta_id;
  738. int tid;
  739. unsigned long flags;
  740. /* "flow" corresponds to Tx queue */
  741. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  742. /* "ssn" is start of block-ack Tx window, corresponds to index
  743. * (in Tx queue's circular buffer) of first TFD/frame in window */
  744. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  745. if (scd_flow >= priv->hw_params.max_txq_num) {
  746. IWL_ERR(priv,
  747. "BUG_ON scd_flow is bigger than number of queues\n");
  748. return;
  749. }
  750. txq = &priv->txq[scd_flow];
  751. sta_id = ba_resp->sta_id;
  752. tid = ba_resp->tid;
  753. agg = &priv->stations[sta_id].tid[tid].agg;
  754. if (unlikely(agg->txq_id != scd_flow)) {
  755. /*
  756. * FIXME: this is a uCode bug which need to be addressed,
  757. * log the information and return for now!
  758. * since it is possible happen very often and in order
  759. * not to fill the syslog, don't enable the logging by default
  760. */
  761. IWL_DEBUG_TX_REPLY(priv,
  762. "BA scd_flow %d does not match txq_id %d\n",
  763. scd_flow, agg->txq_id);
  764. return;
  765. }
  766. /* Find index just before block-ack window */
  767. index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  768. spin_lock_irqsave(&priv->sta_lock, flags);
  769. IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, "
  770. "sta_id = %d\n",
  771. agg->wait_for_ba,
  772. (u8 *) &ba_resp->sta_addr_lo32,
  773. ba_resp->sta_id);
  774. IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
  775. "%d, scd_ssn = %d\n",
  776. ba_resp->tid,
  777. ba_resp->seq_ctl,
  778. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  779. ba_resp->scd_flow,
  780. ba_resp->scd_ssn);
  781. IWL_DEBUG_TX_REPLY(priv, "DAT start_idx = %d, bitmap = 0x%llx\n",
  782. agg->start_idx,
  783. (unsigned long long)agg->bitmap);
  784. /* Update driver's record of ACK vs. not for each frame in window */
  785. iwlagn_tx_status_reply_compressed_ba(priv, agg, ba_resp);
  786. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  787. * block-ack window (we assume that they've been successfully
  788. * transmitted ... if not, it's too late anyway). */
  789. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  790. /* calculate mac80211 ampdu sw queue to wake */
  791. int freed = iwlagn_tx_queue_reclaim(priv, scd_flow, index);
  792. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  793. if ((iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  794. priv->mac80211_registered &&
  795. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA))
  796. iwl_wake_queue(priv, txq);
  797. iwlagn_txq_check_empty(priv, sta_id, tid, scd_flow);
  798. }
  799. spin_unlock_irqrestore(&priv->sta_lock, flags);
  800. }
  801. #ifdef CONFIG_IWLWIFI_DEBUG
  802. const char *iwl_get_tx_fail_reason(u32 status)
  803. {
  804. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  805. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  806. switch (status & TX_STATUS_MSK) {
  807. case TX_STATUS_SUCCESS:
  808. return "SUCCESS";
  809. TX_STATUS_POSTPONE(DELAY);
  810. TX_STATUS_POSTPONE(FEW_BYTES);
  811. TX_STATUS_POSTPONE(BT_PRIO);
  812. TX_STATUS_POSTPONE(QUIET_PERIOD);
  813. TX_STATUS_POSTPONE(CALC_TTAK);
  814. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  815. TX_STATUS_FAIL(SHORT_LIMIT);
  816. TX_STATUS_FAIL(LONG_LIMIT);
  817. TX_STATUS_FAIL(FIFO_UNDERRUN);
  818. TX_STATUS_FAIL(DRAIN_FLOW);
  819. TX_STATUS_FAIL(RFKILL_FLUSH);
  820. TX_STATUS_FAIL(LIFE_EXPIRE);
  821. TX_STATUS_FAIL(DEST_PS);
  822. TX_STATUS_FAIL(HOST_ABORTED);
  823. TX_STATUS_FAIL(BT_RETRY);
  824. TX_STATUS_FAIL(STA_INVALID);
  825. TX_STATUS_FAIL(FRAG_DROPPED);
  826. TX_STATUS_FAIL(TID_DISABLE);
  827. TX_STATUS_FAIL(FIFO_FLUSHED);
  828. TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
  829. TX_STATUS_FAIL(PASSIVE_NO_RX);
  830. TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
  831. }
  832. return "UNKNOWN";
  833. #undef TX_STATUS_FAIL
  834. #undef TX_STATUS_POSTPONE
  835. }
  836. #endif /* CONFIG_IWLWIFI_DEBUG */