iwl-agn-tx.c 28 KB

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