tx.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/ieee80211.h>
  64. #include <linux/etherdevice.h>
  65. #include "iwl-trans.h"
  66. #include "iwl-eeprom-parse.h"
  67. #include "mvm.h"
  68. #include "sta.h"
  69. /*
  70. * Sets most of the Tx cmd's fields
  71. */
  72. static void iwl_mvm_set_tx_cmd(struct iwl_mvm *mvm, struct sk_buff *skb,
  73. struct iwl_tx_cmd *tx_cmd,
  74. struct ieee80211_tx_info *info, u8 sta_id)
  75. {
  76. struct ieee80211_hdr *hdr = (void *)skb->data;
  77. __le16 fc = hdr->frame_control;
  78. u32 tx_flags = le32_to_cpu(tx_cmd->tx_flags);
  79. u32 len = skb->len + FCS_LEN;
  80. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  81. tx_flags |= TX_CMD_FLG_ACK;
  82. else
  83. tx_flags &= ~TX_CMD_FLG_ACK;
  84. if (ieee80211_is_probe_resp(fc))
  85. tx_flags |= TX_CMD_FLG_TSF;
  86. else if (ieee80211_is_back_req(fc))
  87. tx_flags |= TX_CMD_FLG_ACK | TX_CMD_FLG_BAR;
  88. /* High prio packet (wrt. BT coex) if it is EAPOL, MCAST or MGMT */
  89. if (info->band == IEEE80211_BAND_2GHZ &&
  90. (skb->protocol == cpu_to_be16(ETH_P_PAE) ||
  91. is_multicast_ether_addr(hdr->addr1) ||
  92. ieee80211_is_back_req(fc) ||
  93. ieee80211_is_mgmt(fc)))
  94. tx_flags |= TX_CMD_FLG_BT_DIS;
  95. if (ieee80211_has_morefrags(fc))
  96. tx_flags |= TX_CMD_FLG_MORE_FRAG;
  97. if (ieee80211_is_data_qos(fc)) {
  98. u8 *qc = ieee80211_get_qos_ctl(hdr);
  99. tx_cmd->tid_tspec = qc[0] & 0xf;
  100. tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
  101. } else {
  102. tx_cmd->tid_tspec = IWL_TID_NON_QOS;
  103. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  104. tx_flags |= TX_CMD_FLG_SEQ_CTL;
  105. else
  106. tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
  107. }
  108. if (ieee80211_is_mgmt(fc)) {
  109. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  110. tx_cmd->pm_frame_timeout = cpu_to_le16(3);
  111. else
  112. tx_cmd->pm_frame_timeout = cpu_to_le16(2);
  113. /* The spec allows Action frames in A-MPDU, we don't support
  114. * it
  115. */
  116. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU);
  117. } else {
  118. tx_cmd->pm_frame_timeout = 0;
  119. }
  120. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  121. tx_flags |= TX_CMD_FLG_PROT_REQUIRE;
  122. if (ieee80211_is_data(fc) && len > mvm->rts_threshold &&
  123. !is_multicast_ether_addr(ieee80211_get_DA(hdr)))
  124. tx_flags |= TX_CMD_FLG_PROT_REQUIRE;
  125. tx_cmd->driver_txop = 0;
  126. tx_cmd->tx_flags = cpu_to_le32(tx_flags);
  127. /* Total # bytes to be transmitted */
  128. tx_cmd->len = cpu_to_le16((u16)skb->len);
  129. tx_cmd->next_frame_len = 0;
  130. tx_cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  131. tx_cmd->sta_id = sta_id;
  132. }
  133. /*
  134. * Sets the fields in the Tx cmd that are rate related
  135. */
  136. static void iwl_mvm_set_tx_cmd_rate(struct iwl_mvm *mvm,
  137. struct iwl_tx_cmd *tx_cmd,
  138. struct ieee80211_tx_info *info,
  139. struct ieee80211_sta *sta,
  140. __le16 fc)
  141. {
  142. u32 rate_flags;
  143. int rate_idx;
  144. u8 rate_plcp;
  145. /* Set retry limit on RTS packets */
  146. tx_cmd->rts_retry_limit = IWL_RTS_DFAULT_RETRY_LIMIT;
  147. /* Set retry limit on DATA packets and Probe Responses*/
  148. if (ieee80211_is_probe_resp(fc)) {
  149. tx_cmd->data_retry_limit = IWL_MGMT_DFAULT_RETRY_LIMIT;
  150. tx_cmd->rts_retry_limit =
  151. min(tx_cmd->data_retry_limit, tx_cmd->rts_retry_limit);
  152. } else if (ieee80211_is_back_req(fc)) {
  153. tx_cmd->data_retry_limit = IWL_BAR_DFAULT_RETRY_LIMIT;
  154. } else {
  155. tx_cmd->data_retry_limit = IWL_DEFAULT_TX_RETRY;
  156. }
  157. /*
  158. * for data packets, rate info comes from the table inside he fw. This
  159. * table is controlled by LINK_QUALITY commands
  160. */
  161. if (ieee80211_is_data(fc)) {
  162. tx_cmd->initial_rate_index = 0;
  163. tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_STA_RATE);
  164. return;
  165. } else if (ieee80211_is_back_req(fc)) {
  166. tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_STA_RATE);
  167. }
  168. /* HT rate doesn't make sense for a non data frame */
  169. WARN_ONCE(info->control.rates[0].flags & IEEE80211_TX_RC_MCS,
  170. "Got an HT rate for a non data frame 0x%x\n",
  171. info->control.rates[0].flags);
  172. rate_idx = info->control.rates[0].idx;
  173. /* if the rate isn't a well known legacy rate, take the lowest one */
  174. if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT_LEGACY)
  175. rate_idx = rate_lowest_index(
  176. &mvm->nvm_data->bands[info->band], sta);
  177. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  178. if (info->band == IEEE80211_BAND_5GHZ)
  179. rate_idx += IWL_FIRST_OFDM_RATE;
  180. /* For 2.4 GHZ band, check that there is no need to remap */
  181. BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
  182. /* Get PLCP rate for tx_cmd->rate_n_flags */
  183. rate_plcp = iwl_mvm_mac80211_idx_to_hwrate(rate_idx);
  184. mvm->mgmt_last_antenna_idx =
  185. iwl_mvm_next_antenna(mvm, mvm->nvm_data->valid_tx_ant,
  186. mvm->mgmt_last_antenna_idx);
  187. rate_flags = BIT(mvm->mgmt_last_antenna_idx) << RATE_MCS_ANT_POS;
  188. /* Set CCK flag as needed */
  189. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  190. rate_flags |= RATE_MCS_CCK_MSK;
  191. /* Set the rate in the TX cmd */
  192. tx_cmd->rate_n_flags = cpu_to_le32((u32)rate_plcp | rate_flags);
  193. }
  194. /*
  195. * Sets the fields in the Tx cmd that are crypto related
  196. */
  197. static void iwl_mvm_set_tx_cmd_crypto(struct iwl_mvm *mvm,
  198. struct ieee80211_tx_info *info,
  199. struct iwl_tx_cmd *tx_cmd,
  200. struct sk_buff *skb_frag)
  201. {
  202. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  203. switch (keyconf->cipher) {
  204. case WLAN_CIPHER_SUITE_CCMP:
  205. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  206. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  207. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  208. tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_CCMP_AGG);
  209. break;
  210. case WLAN_CIPHER_SUITE_TKIP:
  211. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  212. ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
  213. break;
  214. case WLAN_CIPHER_SUITE_WEP104:
  215. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  216. /* fall through */
  217. case WLAN_CIPHER_SUITE_WEP40:
  218. tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
  219. ((keyconf->keyidx << TX_CMD_SEC_WEP_KEY_IDX_POS) &
  220. TX_CMD_SEC_WEP_KEY_IDX_MSK);
  221. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  222. break;
  223. default:
  224. IWL_ERR(mvm, "Unknown encode cipher %x\n", keyconf->cipher);
  225. break;
  226. }
  227. }
  228. /*
  229. * Allocates and sets the Tx cmd the driver data pointers in the skb
  230. */
  231. static struct iwl_device_cmd *
  232. iwl_mvm_set_tx_params(struct iwl_mvm *mvm, struct sk_buff *skb,
  233. struct ieee80211_sta *sta, u8 sta_id)
  234. {
  235. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  236. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  237. struct iwl_device_cmd *dev_cmd;
  238. struct iwl_tx_cmd *tx_cmd;
  239. dev_cmd = iwl_trans_alloc_tx_cmd(mvm->trans);
  240. if (unlikely(!dev_cmd))
  241. return NULL;
  242. memset(dev_cmd, 0, sizeof(*dev_cmd));
  243. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  244. if (info->control.hw_key)
  245. iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb);
  246. iwl_mvm_set_tx_cmd(mvm, skb, tx_cmd, info, sta_id);
  247. iwl_mvm_set_tx_cmd_rate(mvm, tx_cmd, info, sta, hdr->frame_control);
  248. memset(&info->status, 0, sizeof(info->status));
  249. info->driver_data[0] = NULL;
  250. info->driver_data[1] = dev_cmd;
  251. return dev_cmd;
  252. }
  253. int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
  254. {
  255. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  256. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  257. struct iwl_device_cmd *dev_cmd;
  258. struct iwl_tx_cmd *tx_cmd;
  259. u8 sta_id;
  260. if (WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU))
  261. return -1;
  262. if (WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
  263. (!info->control.vif ||
  264. info->hw_queue != info->control.vif->cab_queue)))
  265. return -1;
  266. /*
  267. * If the interface on which frame is sent is the P2P_DEVICE
  268. * or an AP/GO interface use the broadcast station associated
  269. * with it; otherwise use the AUX station.
  270. */
  271. if (info->control.vif &&
  272. (info->control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
  273. info->control.vif->type == NL80211_IFTYPE_AP)) {
  274. struct iwl_mvm_vif *mvmvif =
  275. iwl_mvm_vif_from_mac80211(info->control.vif);
  276. sta_id = mvmvif->bcast_sta.sta_id;
  277. } else {
  278. sta_id = mvm->aux_sta.sta_id;
  279. }
  280. IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, info->hw_queue);
  281. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, NULL, sta_id);
  282. if (!dev_cmd)
  283. return -1;
  284. /* From now on, we cannot access info->control */
  285. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  286. /* Copy MAC header from skb into command buffer */
  287. memcpy(tx_cmd->hdr, hdr, ieee80211_hdrlen(hdr->frame_control));
  288. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, info->hw_queue)) {
  289. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  290. return -1;
  291. }
  292. return 0;
  293. }
  294. /*
  295. * Sets the fields in the Tx cmd that are crypto related
  296. */
  297. int iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
  298. struct ieee80211_sta *sta)
  299. {
  300. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  301. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  302. struct iwl_mvm_sta *mvmsta;
  303. struct iwl_device_cmd *dev_cmd;
  304. struct iwl_tx_cmd *tx_cmd;
  305. __le16 fc;
  306. u16 seq_number = 0;
  307. u8 tid = IWL_MAX_TID_COUNT;
  308. u8 txq_id = info->hw_queue;
  309. bool is_data_qos = false, is_ampdu = false;
  310. mvmsta = (void *)sta->drv_priv;
  311. fc = hdr->frame_control;
  312. if (WARN_ON_ONCE(!mvmsta))
  313. return -1;
  314. if (WARN_ON_ONCE(mvmsta->sta_id == IWL_INVALID_STATION))
  315. return -1;
  316. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, sta, mvmsta->sta_id);
  317. if (!dev_cmd)
  318. goto drop;
  319. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  320. /* From now on, we cannot access info->control */
  321. spin_lock(&mvmsta->lock);
  322. if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
  323. u8 *qc = NULL;
  324. qc = ieee80211_get_qos_ctl(hdr);
  325. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  326. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  327. goto drop_unlock_sta;
  328. seq_number = mvmsta->tid_data[tid].seq_number;
  329. seq_number &= IEEE80211_SCTL_SEQ;
  330. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  331. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  332. seq_number += 0x10;
  333. is_data_qos = true;
  334. is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
  335. }
  336. /* Copy MAC header from skb into command buffer */
  337. memcpy(tx_cmd->hdr, hdr, ieee80211_hdrlen(fc));
  338. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
  339. if (is_ampdu) {
  340. if (WARN_ON_ONCE(mvmsta->tid_data[tid].state != IWL_AGG_ON))
  341. goto drop_unlock_sta;
  342. txq_id = mvmsta->tid_data[tid].txq_id;
  343. }
  344. IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x\n", mvmsta->sta_id,
  345. tid, txq_id, seq_number);
  346. /* NOTE: aggregation will need changes here (for txq id) */
  347. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, txq_id))
  348. goto drop_unlock_sta;
  349. if (is_data_qos && !ieee80211_has_morefrags(fc))
  350. mvmsta->tid_data[tid].seq_number = seq_number;
  351. spin_unlock(&mvmsta->lock);
  352. if (mvmsta->vif->type == NL80211_IFTYPE_AP &&
  353. txq_id < IWL_FIRST_AMPDU_QUEUE)
  354. atomic_inc(&mvmsta->pending_frames);
  355. return 0;
  356. drop_unlock_sta:
  357. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  358. spin_unlock(&mvmsta->lock);
  359. drop:
  360. return -1;
  361. }
  362. static void iwl_mvm_check_ratid_empty(struct iwl_mvm *mvm,
  363. struct ieee80211_sta *sta, u8 tid)
  364. {
  365. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  366. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  367. struct ieee80211_vif *vif = mvmsta->vif;
  368. lockdep_assert_held(&mvmsta->lock);
  369. if (tid_data->ssn != tid_data->next_reclaimed)
  370. return;
  371. switch (tid_data->state) {
  372. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  373. IWL_DEBUG_TX_QUEUES(mvm,
  374. "Can continue addBA flow ssn = next_recl = %d\n",
  375. tid_data->next_reclaimed);
  376. tid_data->state = IWL_AGG_STARTING;
  377. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  378. break;
  379. case IWL_EMPTYING_HW_QUEUE_DELBA:
  380. IWL_DEBUG_TX_QUEUES(mvm,
  381. "Can continue DELBA flow ssn = next_recl = %d\n",
  382. tid_data->next_reclaimed);
  383. iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
  384. tid_data->state = IWL_AGG_OFF;
  385. /*
  386. * we can't hold the mutex - but since we are after a sequence
  387. * point (call to iwl_trans_txq_disable), so we don't even need
  388. * a memory barrier.
  389. */
  390. mvm->queue_to_mac80211[tid_data->txq_id] =
  391. IWL_INVALID_MAC80211_QUEUE;
  392. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  393. break;
  394. default:
  395. break;
  396. }
  397. }
  398. #ifdef CONFIG_IWLWIFI_DEBUG
  399. const char *iwl_mvm_get_tx_fail_reason(u32 status)
  400. {
  401. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  402. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  403. switch (status & TX_STATUS_MSK) {
  404. case TX_STATUS_SUCCESS:
  405. return "SUCCESS";
  406. TX_STATUS_POSTPONE(DELAY);
  407. TX_STATUS_POSTPONE(FEW_BYTES);
  408. TX_STATUS_POSTPONE(BT_PRIO);
  409. TX_STATUS_POSTPONE(QUIET_PERIOD);
  410. TX_STATUS_POSTPONE(CALC_TTAK);
  411. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  412. TX_STATUS_FAIL(SHORT_LIMIT);
  413. TX_STATUS_FAIL(LONG_LIMIT);
  414. TX_STATUS_FAIL(UNDERRUN);
  415. TX_STATUS_FAIL(DRAIN_FLOW);
  416. TX_STATUS_FAIL(RFKILL_FLUSH);
  417. TX_STATUS_FAIL(LIFE_EXPIRE);
  418. TX_STATUS_FAIL(DEST_PS);
  419. TX_STATUS_FAIL(HOST_ABORTED);
  420. TX_STATUS_FAIL(BT_RETRY);
  421. TX_STATUS_FAIL(STA_INVALID);
  422. TX_STATUS_FAIL(FRAG_DROPPED);
  423. TX_STATUS_FAIL(TID_DISABLE);
  424. TX_STATUS_FAIL(FIFO_FLUSHED);
  425. TX_STATUS_FAIL(SMALL_CF_POLL);
  426. TX_STATUS_FAIL(FW_DROP);
  427. TX_STATUS_FAIL(STA_COLOR_MISMATCH);
  428. }
  429. return "UNKNOWN";
  430. #undef TX_STATUS_FAIL
  431. #undef TX_STATUS_POSTPONE
  432. }
  433. #endif /* CONFIG_IWLWIFI_DEBUG */
  434. /**
  435. * translate ucode response to mac80211 tx status control values
  436. */
  437. static void iwl_mvm_hwrate_to_tx_control(u32 rate_n_flags,
  438. struct ieee80211_tx_info *info)
  439. {
  440. struct ieee80211_tx_rate *r = &info->status.rates[0];
  441. info->status.antenna =
  442. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  443. if (rate_n_flags & RATE_HT_MCS_GF_MSK)
  444. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  445. switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
  446. case RATE_MCS_CHAN_WIDTH_20:
  447. break;
  448. case RATE_MCS_CHAN_WIDTH_40:
  449. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  450. break;
  451. case RATE_MCS_CHAN_WIDTH_80:
  452. r->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
  453. break;
  454. case RATE_MCS_CHAN_WIDTH_160:
  455. r->flags |= IEEE80211_TX_RC_160_MHZ_WIDTH;
  456. break;
  457. }
  458. if (rate_n_flags & RATE_MCS_SGI_MSK)
  459. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  460. if (rate_n_flags & RATE_MCS_HT_MSK) {
  461. r->flags |= IEEE80211_TX_RC_MCS;
  462. r->idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
  463. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  464. ieee80211_rate_set_vht(
  465. r, rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK,
  466. ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
  467. RATE_VHT_MCS_NSS_POS) + 1);
  468. r->flags |= IEEE80211_TX_RC_VHT_MCS;
  469. } else {
  470. r->idx = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
  471. info->band);
  472. }
  473. }
  474. static void iwl_mvm_rx_tx_cmd_single(struct iwl_mvm *mvm,
  475. struct iwl_rx_packet *pkt)
  476. {
  477. struct ieee80211_sta *sta;
  478. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  479. int txq_id = SEQ_TO_QUEUE(sequence);
  480. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  481. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  482. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  483. u32 status = le16_to_cpu(tx_resp->status.status);
  484. u16 ssn = iwl_mvm_get_scd_ssn(tx_resp);
  485. struct iwl_mvm_sta *mvmsta;
  486. struct sk_buff_head skbs;
  487. u8 skb_freed = 0;
  488. u16 next_reclaimed, seq_ctl;
  489. __skb_queue_head_init(&skbs);
  490. seq_ctl = le16_to_cpu(tx_resp->seq_ctl);
  491. /* we can free until ssn % q.n_bd not inclusive */
  492. iwl_trans_reclaim(mvm->trans, txq_id, ssn, &skbs);
  493. while (!skb_queue_empty(&skbs)) {
  494. struct sk_buff *skb = __skb_dequeue(&skbs);
  495. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  496. skb_freed++;
  497. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  498. memset(&info->status, 0, sizeof(info->status));
  499. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  500. /* inform mac80211 about what happened with the frame */
  501. switch (status & TX_STATUS_MSK) {
  502. case TX_STATUS_SUCCESS:
  503. case TX_STATUS_DIRECT_DONE:
  504. info->flags |= IEEE80211_TX_STAT_ACK;
  505. break;
  506. case TX_STATUS_FAIL_DEST_PS:
  507. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  508. break;
  509. default:
  510. break;
  511. }
  512. info->status.rates[0].count = tx_resp->failure_frame + 1;
  513. iwl_mvm_hwrate_to_tx_control(le32_to_cpu(tx_resp->initial_rate),
  514. info);
  515. /* Single frame failure in an AMPDU queue => send BAR */
  516. if (txq_id >= IWL_FIRST_AMPDU_QUEUE &&
  517. !(info->flags & IEEE80211_TX_STAT_ACK))
  518. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  519. /* W/A FW bug: seq_ctl is wrong when the queue is flushed */
  520. if (status == TX_STATUS_FAIL_FIFO_FLUSHED) {
  521. struct ieee80211_hdr *hdr = (void *)skb->data;
  522. seq_ctl = le16_to_cpu(hdr->seq_ctrl);
  523. }
  524. ieee80211_tx_status_ni(mvm->hw, skb);
  525. }
  526. if (txq_id >= IWL_FIRST_AMPDU_QUEUE) {
  527. /* If this is an aggregation queue, we use the ssn since:
  528. * ssn = wifi seq_num % 256.
  529. * The seq_ctl is the sequence control of the packet to which
  530. * this Tx response relates. But if there is a hole in the
  531. * bitmap of the BA we received, this Tx response may allow to
  532. * reclaim the hole and all the subsequent packets that were
  533. * already acked. In that case, seq_ctl != ssn, and the next
  534. * packet to be reclaimed will be ssn and not seq_ctl. In that
  535. * case, several packets will be reclaimed even if
  536. * frame_count = 1.
  537. *
  538. * The ssn is the index (% 256) of the latest packet that has
  539. * treated (acked / dropped) + 1.
  540. */
  541. next_reclaimed = ssn;
  542. } else {
  543. /* The next packet to be reclaimed is the one after this one */
  544. next_reclaimed = SEQ_TO_SN(seq_ctl + 0x10);
  545. }
  546. IWL_DEBUG_TX_REPLY(mvm,
  547. "TXQ %d status %s (0x%08x)\n\t\t\t\tinitial_rate 0x%x "
  548. "retries %d, idx=%d ssn=%d next_reclaimed=0x%x seq_ctl=0x%x\n",
  549. txq_id, iwl_mvm_get_tx_fail_reason(status),
  550. status, le32_to_cpu(tx_resp->initial_rate),
  551. tx_resp->failure_frame, SEQ_TO_INDEX(sequence),
  552. ssn, next_reclaimed, seq_ctl);
  553. rcu_read_lock();
  554. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  555. if (!IS_ERR_OR_NULL(sta)) {
  556. mvmsta = (void *)sta->drv_priv;
  557. if (tid != IWL_TID_NON_QOS) {
  558. struct iwl_mvm_tid_data *tid_data =
  559. &mvmsta->tid_data[tid];
  560. spin_lock_bh(&mvmsta->lock);
  561. tid_data->next_reclaimed = next_reclaimed;
  562. IWL_DEBUG_TX_REPLY(mvm, "Next reclaimed packet:%d\n",
  563. next_reclaimed);
  564. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  565. spin_unlock_bh(&mvmsta->lock);
  566. }
  567. #ifdef CONFIG_PM_SLEEP
  568. mvmsta->last_seq_ctl = seq_ctl;
  569. #endif
  570. } else {
  571. sta = NULL;
  572. mvmsta = NULL;
  573. }
  574. /*
  575. * If the txq is not an AMPDU queue, there is no chance we freed
  576. * several skbs. Check that out...
  577. * If there are no pending frames for this STA, notify mac80211 that
  578. * this station can go to sleep in its STA table.
  579. */
  580. if (txq_id < IWL_FIRST_AMPDU_QUEUE && mvmsta &&
  581. !WARN_ON(skb_freed > 1) &&
  582. mvmsta->vif->type == NL80211_IFTYPE_AP &&
  583. atomic_sub_and_test(skb_freed, &mvmsta->pending_frames)) {
  584. ieee80211_sta_block_awake(mvm->hw, sta, false);
  585. set_bit(sta_id, mvm->sta_drained);
  586. schedule_work(&mvm->sta_drained_wk);
  587. }
  588. rcu_read_unlock();
  589. }
  590. #ifdef CONFIG_IWLWIFI_DEBUG
  591. #define AGG_TX_STATE_(x) case AGG_TX_STATE_ ## x: return #x
  592. static const char *iwl_get_agg_tx_status(u16 status)
  593. {
  594. switch (status & AGG_TX_STATE_STATUS_MSK) {
  595. AGG_TX_STATE_(TRANSMITTED);
  596. AGG_TX_STATE_(UNDERRUN);
  597. AGG_TX_STATE_(BT_PRIO);
  598. AGG_TX_STATE_(FEW_BYTES);
  599. AGG_TX_STATE_(ABORT);
  600. AGG_TX_STATE_(LAST_SENT_TTL);
  601. AGG_TX_STATE_(LAST_SENT_TRY_CNT);
  602. AGG_TX_STATE_(LAST_SENT_BT_KILL);
  603. AGG_TX_STATE_(SCD_QUERY);
  604. AGG_TX_STATE_(TEST_BAD_CRC32);
  605. AGG_TX_STATE_(RESPONSE);
  606. AGG_TX_STATE_(DUMP_TX);
  607. AGG_TX_STATE_(DELAY_TX);
  608. }
  609. return "UNKNOWN";
  610. }
  611. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  612. struct iwl_rx_packet *pkt)
  613. {
  614. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  615. struct agg_tx_status *frame_status = &tx_resp->status;
  616. int i;
  617. for (i = 0; i < tx_resp->frame_count; i++) {
  618. u16 fstatus = le16_to_cpu(frame_status[i].status);
  619. IWL_DEBUG_TX_REPLY(mvm,
  620. "status %s (0x%04x), try-count (%d) seq (0x%x)\n",
  621. iwl_get_agg_tx_status(fstatus),
  622. fstatus & AGG_TX_STATE_STATUS_MSK,
  623. (fstatus & AGG_TX_STATE_TRY_CNT_MSK) >>
  624. AGG_TX_STATE_TRY_CNT_POS,
  625. le16_to_cpu(frame_status[i].sequence));
  626. }
  627. }
  628. #else
  629. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  630. struct iwl_rx_packet *pkt)
  631. {}
  632. #endif /* CONFIG_IWLWIFI_DEBUG */
  633. static void iwl_mvm_rx_tx_cmd_agg(struct iwl_mvm *mvm,
  634. struct iwl_rx_packet *pkt)
  635. {
  636. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  637. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  638. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  639. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  640. struct ieee80211_sta *sta;
  641. if (WARN_ON_ONCE(SEQ_TO_QUEUE(sequence) < IWL_FIRST_AMPDU_QUEUE))
  642. return;
  643. if (WARN_ON_ONCE(tid == IWL_TID_NON_QOS))
  644. return;
  645. iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);
  646. rcu_read_lock();
  647. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  648. if (!WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  649. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  650. mvmsta->tid_data[tid].rate_n_flags =
  651. le32_to_cpu(tx_resp->initial_rate);
  652. }
  653. rcu_read_unlock();
  654. }
  655. int iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  656. struct iwl_device_cmd *cmd)
  657. {
  658. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  659. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  660. if (tx_resp->frame_count == 1)
  661. iwl_mvm_rx_tx_cmd_single(mvm, pkt);
  662. else
  663. iwl_mvm_rx_tx_cmd_agg(mvm, pkt);
  664. return 0;
  665. }
  666. int iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  667. struct iwl_device_cmd *cmd)
  668. {
  669. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  670. struct iwl_mvm_ba_notif *ba_notif = (void *)pkt->data;
  671. struct sk_buff_head reclaimed_skbs;
  672. struct iwl_mvm_tid_data *tid_data;
  673. struct ieee80211_tx_info *info;
  674. struct ieee80211_sta *sta;
  675. struct iwl_mvm_sta *mvmsta;
  676. struct ieee80211_hdr *hdr;
  677. struct sk_buff *skb;
  678. int sta_id, tid, freed;
  679. /* "flow" corresponds to Tx queue */
  680. u16 scd_flow = le16_to_cpu(ba_notif->scd_flow);
  681. /* "ssn" is start of block-ack Tx window, corresponds to index
  682. * (in Tx queue's circular buffer) of first TFD/frame in window */
  683. u16 ba_resp_scd_ssn = le16_to_cpu(ba_notif->scd_ssn);
  684. sta_id = ba_notif->sta_id;
  685. tid = ba_notif->tid;
  686. rcu_read_lock();
  687. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  688. /* Reclaiming frames for a station that has been deleted ? */
  689. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  690. rcu_read_unlock();
  691. return 0;
  692. }
  693. mvmsta = (void *)sta->drv_priv;
  694. tid_data = &mvmsta->tid_data[tid];
  695. if (WARN_ONCE(tid_data->txq_id != scd_flow, "Q %d, tid %d, flow %d",
  696. tid_data->txq_id, tid, scd_flow)) {
  697. rcu_read_unlock();
  698. return 0;
  699. }
  700. spin_lock_bh(&mvmsta->lock);
  701. __skb_queue_head_init(&reclaimed_skbs);
  702. /*
  703. * Release all TFDs before the SSN, i.e. all TFDs in front of
  704. * block-ack window (we assume that they've been successfully
  705. * transmitted ... if not, it's too late anyway).
  706. */
  707. iwl_trans_reclaim(mvm->trans, scd_flow, ba_resp_scd_ssn,
  708. &reclaimed_skbs);
  709. IWL_DEBUG_TX_REPLY(mvm,
  710. "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
  711. (u8 *)&ba_notif->sta_addr_lo32,
  712. ba_notif->sta_id);
  713. IWL_DEBUG_TX_REPLY(mvm,
  714. "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
  715. ba_notif->tid, le16_to_cpu(ba_notif->seq_ctl),
  716. (unsigned long long)le64_to_cpu(ba_notif->bitmap),
  717. scd_flow, ba_resp_scd_ssn, ba_notif->txed,
  718. ba_notif->txed_2_done);
  719. tid_data->next_reclaimed = ba_resp_scd_ssn;
  720. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  721. freed = 0;
  722. skb_queue_walk(&reclaimed_skbs, skb) {
  723. hdr = (struct ieee80211_hdr *)skb->data;
  724. if (ieee80211_is_data_qos(hdr->frame_control))
  725. freed++;
  726. else
  727. WARN_ON_ONCE(1);
  728. info = IEEE80211_SKB_CB(skb);
  729. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  730. if (freed == 1) {
  731. /* this is the first skb we deliver in this batch */
  732. /* put the rate scaling data there */
  733. info = IEEE80211_SKB_CB(skb);
  734. memset(&info->status, 0, sizeof(info->status));
  735. info->flags |= IEEE80211_TX_STAT_ACK;
  736. info->flags |= IEEE80211_TX_STAT_AMPDU;
  737. info->status.ampdu_ack_len = ba_notif->txed_2_done;
  738. info->status.ampdu_len = ba_notif->txed;
  739. iwl_mvm_hwrate_to_tx_control(tid_data->rate_n_flags,
  740. info);
  741. }
  742. }
  743. spin_unlock_bh(&mvmsta->lock);
  744. rcu_read_unlock();
  745. while (!skb_queue_empty(&reclaimed_skbs)) {
  746. skb = __skb_dequeue(&reclaimed_skbs);
  747. ieee80211_tx_status_ni(mvm->hw, skb);
  748. }
  749. return 0;
  750. }
  751. int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk, bool sync)
  752. {
  753. int ret;
  754. struct iwl_tx_path_flush_cmd flush_cmd = {
  755. .queues_ctl = cpu_to_le32(tfd_msk),
  756. .flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
  757. };
  758. u32 flags = sync ? CMD_SYNC : CMD_ASYNC;
  759. ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
  760. sizeof(flush_cmd), &flush_cmd);
  761. if (ret)
  762. IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
  763. return ret;
  764. }