iwl-agn-tx.c 29 KB

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