tx.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 COPYING.
  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) && sta) {
  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 |=
  167. cpu_to_le32(TX_CMD_FLG_ACK | TX_CMD_FLG_BAR);
  168. }
  169. /* HT rate doesn't make sense for a non data frame */
  170. WARN_ONCE(info->control.rates[0].flags & IEEE80211_TX_RC_MCS,
  171. "Got an HT rate for a non data frame 0x%x\n",
  172. info->control.rates[0].flags);
  173. rate_idx = info->control.rates[0].idx;
  174. /* if the rate isn't a well known legacy rate, take the lowest one */
  175. if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT_LEGACY)
  176. rate_idx = rate_lowest_index(
  177. &mvm->nvm_data->bands[info->band], sta);
  178. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  179. if (info->band == IEEE80211_BAND_5GHZ)
  180. rate_idx += IWL_FIRST_OFDM_RATE;
  181. /* For 2.4 GHZ band, check that there is no need to remap */
  182. BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
  183. /* Get PLCP rate for tx_cmd->rate_n_flags */
  184. rate_plcp = iwl_mvm_mac80211_idx_to_hwrate(rate_idx);
  185. mvm->mgmt_last_antenna_idx =
  186. iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
  187. mvm->mgmt_last_antenna_idx);
  188. rate_flags = BIT(mvm->mgmt_last_antenna_idx) << RATE_MCS_ANT_POS;
  189. /* Set CCK flag as needed */
  190. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  191. rate_flags |= RATE_MCS_CCK_MSK;
  192. /* Set the rate in the TX cmd */
  193. tx_cmd->rate_n_flags = cpu_to_le32((u32)rate_plcp | rate_flags);
  194. }
  195. /*
  196. * Sets the fields in the Tx cmd that are crypto related
  197. */
  198. static void iwl_mvm_set_tx_cmd_crypto(struct iwl_mvm *mvm,
  199. struct ieee80211_tx_info *info,
  200. struct iwl_tx_cmd *tx_cmd,
  201. struct sk_buff *skb_frag)
  202. {
  203. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  204. switch (keyconf->cipher) {
  205. case WLAN_CIPHER_SUITE_CCMP:
  206. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  207. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  208. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  209. tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_CCMP_AGG);
  210. break;
  211. case WLAN_CIPHER_SUITE_TKIP:
  212. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  213. ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
  214. break;
  215. case WLAN_CIPHER_SUITE_WEP104:
  216. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  217. /* fall through */
  218. case WLAN_CIPHER_SUITE_WEP40:
  219. tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
  220. ((keyconf->keyidx << TX_CMD_SEC_WEP_KEY_IDX_POS) &
  221. TX_CMD_SEC_WEP_KEY_IDX_MSK);
  222. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  223. break;
  224. default:
  225. IWL_ERR(mvm, "Unknown encode cipher %x\n", keyconf->cipher);
  226. break;
  227. }
  228. }
  229. /*
  230. * Allocates and sets the Tx cmd the driver data pointers in the skb
  231. */
  232. static struct iwl_device_cmd *
  233. iwl_mvm_set_tx_params(struct iwl_mvm *mvm, struct sk_buff *skb,
  234. struct ieee80211_sta *sta, u8 sta_id)
  235. {
  236. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  237. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  238. struct iwl_device_cmd *dev_cmd;
  239. struct iwl_tx_cmd *tx_cmd;
  240. dev_cmd = iwl_trans_alloc_tx_cmd(mvm->trans);
  241. if (unlikely(!dev_cmd))
  242. return NULL;
  243. memset(dev_cmd, 0, sizeof(*dev_cmd));
  244. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  245. if (info->control.hw_key)
  246. iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb);
  247. iwl_mvm_set_tx_cmd(mvm, skb, tx_cmd, info, sta_id);
  248. iwl_mvm_set_tx_cmd_rate(mvm, tx_cmd, info, sta, hdr->frame_control);
  249. memset(&info->status, 0, sizeof(info->status));
  250. info->driver_data[0] = NULL;
  251. info->driver_data[1] = dev_cmd;
  252. return dev_cmd;
  253. }
  254. int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
  255. {
  256. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  257. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  258. struct iwl_device_cmd *dev_cmd;
  259. struct iwl_tx_cmd *tx_cmd;
  260. u8 sta_id;
  261. if (WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU))
  262. return -1;
  263. if (WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
  264. (!info->control.vif ||
  265. info->hw_queue != info->control.vif->cab_queue)))
  266. return -1;
  267. /*
  268. * If the interface on which frame is sent is the P2P_DEVICE
  269. * or an AP/GO interface use the broadcast station associated
  270. * with it; otherwise use the AUX station.
  271. */
  272. if (info->control.vif &&
  273. (info->control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
  274. info->control.vif->type == NL80211_IFTYPE_AP)) {
  275. struct iwl_mvm_vif *mvmvif =
  276. iwl_mvm_vif_from_mac80211(info->control.vif);
  277. sta_id = mvmvif->bcast_sta.sta_id;
  278. } else {
  279. sta_id = mvm->aux_sta.sta_id;
  280. }
  281. IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, info->hw_queue);
  282. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, NULL, sta_id);
  283. if (!dev_cmd)
  284. return -1;
  285. /* From now on, we cannot access info->control */
  286. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  287. /* Copy MAC header from skb into command buffer */
  288. memcpy(tx_cmd->hdr, hdr, ieee80211_hdrlen(hdr->frame_control));
  289. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, info->hw_queue)) {
  290. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  291. return -1;
  292. }
  293. return 0;
  294. }
  295. /*
  296. * Sets the fields in the Tx cmd that are crypto related
  297. */
  298. int iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
  299. struct ieee80211_sta *sta)
  300. {
  301. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  302. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  303. struct iwl_mvm_sta *mvmsta;
  304. struct iwl_device_cmd *dev_cmd;
  305. struct iwl_tx_cmd *tx_cmd;
  306. __le16 fc;
  307. u16 seq_number = 0;
  308. u8 tid = IWL_MAX_TID_COUNT;
  309. u8 txq_id = info->hw_queue;
  310. bool is_data_qos = false, is_ampdu = false;
  311. mvmsta = (void *)sta->drv_priv;
  312. fc = hdr->frame_control;
  313. if (WARN_ON_ONCE(!mvmsta))
  314. return -1;
  315. if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
  316. return -1;
  317. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, sta, mvmsta->sta_id);
  318. if (!dev_cmd)
  319. goto drop;
  320. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  321. /* From now on, we cannot access info->control */
  322. spin_lock(&mvmsta->lock);
  323. if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
  324. u8 *qc = NULL;
  325. qc = ieee80211_get_qos_ctl(hdr);
  326. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  327. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  328. goto drop_unlock_sta;
  329. seq_number = mvmsta->tid_data[tid].seq_number;
  330. seq_number &= IEEE80211_SCTL_SEQ;
  331. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  332. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  333. seq_number += 0x10;
  334. is_data_qos = true;
  335. is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
  336. }
  337. /* Copy MAC header from skb into command buffer */
  338. memcpy(tx_cmd->hdr, hdr, ieee80211_hdrlen(fc));
  339. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
  340. if (is_ampdu) {
  341. if (WARN_ON_ONCE(mvmsta->tid_data[tid].state != IWL_AGG_ON))
  342. goto drop_unlock_sta;
  343. txq_id = mvmsta->tid_data[tid].txq_id;
  344. }
  345. IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x\n", mvmsta->sta_id,
  346. tid, txq_id, seq_number);
  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 (txq_id < IWL_MVM_FIRST_AGG_QUEUE)
  353. atomic_inc(&mvm->pending_frames[mvmsta->sta_id]);
  354. return 0;
  355. drop_unlock_sta:
  356. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  357. spin_unlock(&mvmsta->lock);
  358. drop:
  359. return -1;
  360. }
  361. static void iwl_mvm_check_ratid_empty(struct iwl_mvm *mvm,
  362. struct ieee80211_sta *sta, u8 tid)
  363. {
  364. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  365. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  366. struct ieee80211_vif *vif = mvmsta->vif;
  367. lockdep_assert_held(&mvmsta->lock);
  368. if (tid_data->ssn != tid_data->next_reclaimed)
  369. return;
  370. switch (tid_data->state) {
  371. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  372. IWL_DEBUG_TX_QUEUES(mvm,
  373. "Can continue addBA flow ssn = next_recl = %d\n",
  374. tid_data->next_reclaimed);
  375. tid_data->state = IWL_AGG_STARTING;
  376. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  377. break;
  378. case IWL_EMPTYING_HW_QUEUE_DELBA:
  379. IWL_DEBUG_TX_QUEUES(mvm,
  380. "Can continue DELBA flow ssn = next_recl = %d\n",
  381. tid_data->next_reclaimed);
  382. iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
  383. tid_data->state = IWL_AGG_OFF;
  384. /*
  385. * we can't hold the mutex - but since we are after a sequence
  386. * point (call to iwl_trans_txq_disable), so we don't even need
  387. * a memory barrier.
  388. */
  389. mvm->queue_to_mac80211[tid_data->txq_id] =
  390. IWL_INVALID_MAC80211_QUEUE;
  391. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  392. break;
  393. default:
  394. break;
  395. }
  396. }
  397. #ifdef CONFIG_IWLWIFI_DEBUG
  398. const char *iwl_mvm_get_tx_fail_reason(u32 status)
  399. {
  400. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  401. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  402. switch (status & TX_STATUS_MSK) {
  403. case TX_STATUS_SUCCESS:
  404. return "SUCCESS";
  405. TX_STATUS_POSTPONE(DELAY);
  406. TX_STATUS_POSTPONE(FEW_BYTES);
  407. TX_STATUS_POSTPONE(BT_PRIO);
  408. TX_STATUS_POSTPONE(QUIET_PERIOD);
  409. TX_STATUS_POSTPONE(CALC_TTAK);
  410. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  411. TX_STATUS_FAIL(SHORT_LIMIT);
  412. TX_STATUS_FAIL(LONG_LIMIT);
  413. TX_STATUS_FAIL(UNDERRUN);
  414. TX_STATUS_FAIL(DRAIN_FLOW);
  415. TX_STATUS_FAIL(RFKILL_FLUSH);
  416. TX_STATUS_FAIL(LIFE_EXPIRE);
  417. TX_STATUS_FAIL(DEST_PS);
  418. TX_STATUS_FAIL(HOST_ABORTED);
  419. TX_STATUS_FAIL(BT_RETRY);
  420. TX_STATUS_FAIL(STA_INVALID);
  421. TX_STATUS_FAIL(FRAG_DROPPED);
  422. TX_STATUS_FAIL(TID_DISABLE);
  423. TX_STATUS_FAIL(FIFO_FLUSHED);
  424. TX_STATUS_FAIL(SMALL_CF_POLL);
  425. TX_STATUS_FAIL(FW_DROP);
  426. TX_STATUS_FAIL(STA_COLOR_MISMATCH);
  427. }
  428. return "UNKNOWN";
  429. #undef TX_STATUS_FAIL
  430. #undef TX_STATUS_POSTPONE
  431. }
  432. #endif /* CONFIG_IWLWIFI_DEBUG */
  433. /**
  434. * translate ucode response to mac80211 tx status control values
  435. */
  436. static void iwl_mvm_hwrate_to_tx_control(u32 rate_n_flags,
  437. struct ieee80211_tx_info *info)
  438. {
  439. struct ieee80211_tx_rate *r = &info->status.rates[0];
  440. info->status.antenna =
  441. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  442. if (rate_n_flags & RATE_HT_MCS_GF_MSK)
  443. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  444. switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
  445. case RATE_MCS_CHAN_WIDTH_20:
  446. break;
  447. case RATE_MCS_CHAN_WIDTH_40:
  448. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  449. break;
  450. case RATE_MCS_CHAN_WIDTH_80:
  451. r->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
  452. break;
  453. case RATE_MCS_CHAN_WIDTH_160:
  454. r->flags |= IEEE80211_TX_RC_160_MHZ_WIDTH;
  455. break;
  456. }
  457. if (rate_n_flags & RATE_MCS_SGI_MSK)
  458. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  459. if (rate_n_flags & RATE_MCS_HT_MSK) {
  460. r->flags |= IEEE80211_TX_RC_MCS;
  461. r->idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
  462. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  463. ieee80211_rate_set_vht(
  464. r, rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK,
  465. ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
  466. RATE_VHT_MCS_NSS_POS) + 1);
  467. r->flags |= IEEE80211_TX_RC_VHT_MCS;
  468. } else {
  469. r->idx = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
  470. info->band);
  471. }
  472. }
  473. static void iwl_mvm_rx_tx_cmd_single(struct iwl_mvm *mvm,
  474. struct iwl_rx_packet *pkt)
  475. {
  476. struct ieee80211_sta *sta;
  477. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  478. int txq_id = SEQ_TO_QUEUE(sequence);
  479. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  480. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  481. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  482. u32 status = le16_to_cpu(tx_resp->status.status);
  483. u16 ssn = iwl_mvm_get_scd_ssn(tx_resp);
  484. struct iwl_mvm_sta *mvmsta;
  485. struct sk_buff_head skbs;
  486. u8 skb_freed = 0;
  487. u16 next_reclaimed, seq_ctl;
  488. __skb_queue_head_init(&skbs);
  489. seq_ctl = le16_to_cpu(tx_resp->seq_ctl);
  490. /* we can free until ssn % q.n_bd not inclusive */
  491. iwl_trans_reclaim(mvm->trans, txq_id, ssn, &skbs);
  492. while (!skb_queue_empty(&skbs)) {
  493. struct sk_buff *skb = __skb_dequeue(&skbs);
  494. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  495. skb_freed++;
  496. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  497. memset(&info->status, 0, sizeof(info->status));
  498. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  499. /* inform mac80211 about what happened with the frame */
  500. switch (status & TX_STATUS_MSK) {
  501. case TX_STATUS_SUCCESS:
  502. case TX_STATUS_DIRECT_DONE:
  503. info->flags |= IEEE80211_TX_STAT_ACK;
  504. break;
  505. case TX_STATUS_FAIL_DEST_PS:
  506. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  507. break;
  508. default:
  509. break;
  510. }
  511. info->status.rates[0].count = tx_resp->failure_frame + 1;
  512. iwl_mvm_hwrate_to_tx_control(le32_to_cpu(tx_resp->initial_rate),
  513. info);
  514. /* Single frame failure in an AMPDU queue => send BAR */
  515. if (txq_id >= IWL_MVM_FIRST_AGG_QUEUE &&
  516. !(info->flags & IEEE80211_TX_STAT_ACK))
  517. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  518. /* W/A FW bug: seq_ctl is wrong when the status isn't success */
  519. if (status != TX_STATUS_SUCCESS) {
  520. struct ieee80211_hdr *hdr = (void *)skb->data;
  521. seq_ctl = le16_to_cpu(hdr->seq_ctrl);
  522. }
  523. ieee80211_tx_status_ni(mvm->hw, skb);
  524. }
  525. if (txq_id >= IWL_MVM_FIRST_AGG_QUEUE) {
  526. /* If this is an aggregation queue, we use the ssn since:
  527. * ssn = wifi seq_num % 256.
  528. * The seq_ctl is the sequence control of the packet to which
  529. * this Tx response relates. But if there is a hole in the
  530. * bitmap of the BA we received, this Tx response may allow to
  531. * reclaim the hole and all the subsequent packets that were
  532. * already acked. In that case, seq_ctl != ssn, and the next
  533. * packet to be reclaimed will be ssn and not seq_ctl. In that
  534. * case, several packets will be reclaimed even if
  535. * frame_count = 1.
  536. *
  537. * The ssn is the index (% 256) of the latest packet that has
  538. * treated (acked / dropped) + 1.
  539. */
  540. next_reclaimed = ssn;
  541. } else {
  542. /* The next packet to be reclaimed is the one after this one */
  543. next_reclaimed = IEEE80211_SEQ_TO_SN(seq_ctl + 0x10);
  544. }
  545. IWL_DEBUG_TX_REPLY(mvm,
  546. "TXQ %d status %s (0x%08x)\n",
  547. txq_id, iwl_mvm_get_tx_fail_reason(status), status);
  548. IWL_DEBUG_TX_REPLY(mvm,
  549. "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d next_reclaimed=0x%x seq_ctl=0x%x\n",
  550. 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. */
  578. if (txq_id < IWL_MVM_FIRST_AGG_QUEUE && !WARN_ON(skb_freed > 1) &&
  579. atomic_sub_and_test(skb_freed, &mvm->pending_frames[sta_id])) {
  580. if (mvmsta) {
  581. /*
  582. * If there are no pending frames for this STA, notify
  583. * mac80211 that this station can go to sleep in its
  584. * STA table.
  585. */
  586. if (mvmsta->vif->type == NL80211_IFTYPE_AP)
  587. ieee80211_sta_block_awake(mvm->hw, sta, false);
  588. /*
  589. * We might very well have taken mvmsta pointer while
  590. * the station was being removed. The remove flow might
  591. * have seen a pending_frame (because we didn't take
  592. * the lock) even if now the queues are drained. So make
  593. * really sure now that this the station is not being
  594. * removed. If it is, run the drain worker to remove it.
  595. */
  596. spin_lock_bh(&mvmsta->lock);
  597. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  598. if (IS_ERR_OR_NULL(sta)) {
  599. /*
  600. * Station disappeared in the meantime:
  601. * so we are draining.
  602. */
  603. set_bit(sta_id, mvm->sta_drained);
  604. schedule_work(&mvm->sta_drained_wk);
  605. }
  606. spin_unlock_bh(&mvmsta->lock);
  607. } else if (!mvmsta) {
  608. /* Tx response without STA, so we are draining */
  609. set_bit(sta_id, mvm->sta_drained);
  610. schedule_work(&mvm->sta_drained_wk);
  611. }
  612. }
  613. rcu_read_unlock();
  614. }
  615. #ifdef CONFIG_IWLWIFI_DEBUG
  616. #define AGG_TX_STATE_(x) case AGG_TX_STATE_ ## x: return #x
  617. static const char *iwl_get_agg_tx_status(u16 status)
  618. {
  619. switch (status & AGG_TX_STATE_STATUS_MSK) {
  620. AGG_TX_STATE_(TRANSMITTED);
  621. AGG_TX_STATE_(UNDERRUN);
  622. AGG_TX_STATE_(BT_PRIO);
  623. AGG_TX_STATE_(FEW_BYTES);
  624. AGG_TX_STATE_(ABORT);
  625. AGG_TX_STATE_(LAST_SENT_TTL);
  626. AGG_TX_STATE_(LAST_SENT_TRY_CNT);
  627. AGG_TX_STATE_(LAST_SENT_BT_KILL);
  628. AGG_TX_STATE_(SCD_QUERY);
  629. AGG_TX_STATE_(TEST_BAD_CRC32);
  630. AGG_TX_STATE_(RESPONSE);
  631. AGG_TX_STATE_(DUMP_TX);
  632. AGG_TX_STATE_(DELAY_TX);
  633. }
  634. return "UNKNOWN";
  635. }
  636. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  637. struct iwl_rx_packet *pkt)
  638. {
  639. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  640. struct agg_tx_status *frame_status = &tx_resp->status;
  641. int i;
  642. for (i = 0; i < tx_resp->frame_count; i++) {
  643. u16 fstatus = le16_to_cpu(frame_status[i].status);
  644. IWL_DEBUG_TX_REPLY(mvm,
  645. "status %s (0x%04x), try-count (%d) seq (0x%x)\n",
  646. iwl_get_agg_tx_status(fstatus),
  647. fstatus & AGG_TX_STATE_STATUS_MSK,
  648. (fstatus & AGG_TX_STATE_TRY_CNT_MSK) >>
  649. AGG_TX_STATE_TRY_CNT_POS,
  650. le16_to_cpu(frame_status[i].sequence));
  651. }
  652. }
  653. #else
  654. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  655. struct iwl_rx_packet *pkt)
  656. {}
  657. #endif /* CONFIG_IWLWIFI_DEBUG */
  658. static void iwl_mvm_rx_tx_cmd_agg(struct iwl_mvm *mvm,
  659. struct iwl_rx_packet *pkt)
  660. {
  661. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  662. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  663. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  664. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  665. struct ieee80211_sta *sta;
  666. if (WARN_ON_ONCE(SEQ_TO_QUEUE(sequence) < IWL_MVM_FIRST_AGG_QUEUE))
  667. return;
  668. if (WARN_ON_ONCE(tid == IWL_TID_NON_QOS))
  669. return;
  670. iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);
  671. rcu_read_lock();
  672. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  673. if (!WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  674. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  675. mvmsta->tid_data[tid].rate_n_flags =
  676. le32_to_cpu(tx_resp->initial_rate);
  677. }
  678. rcu_read_unlock();
  679. }
  680. int iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  681. struct iwl_device_cmd *cmd)
  682. {
  683. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  684. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  685. if (tx_resp->frame_count == 1)
  686. iwl_mvm_rx_tx_cmd_single(mvm, pkt);
  687. else
  688. iwl_mvm_rx_tx_cmd_agg(mvm, pkt);
  689. return 0;
  690. }
  691. int iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  692. struct iwl_device_cmd *cmd)
  693. {
  694. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  695. struct iwl_mvm_ba_notif *ba_notif = (void *)pkt->data;
  696. struct sk_buff_head reclaimed_skbs;
  697. struct iwl_mvm_tid_data *tid_data;
  698. struct ieee80211_tx_info *info;
  699. struct ieee80211_sta *sta;
  700. struct iwl_mvm_sta *mvmsta;
  701. struct ieee80211_hdr *hdr;
  702. struct sk_buff *skb;
  703. int sta_id, tid, freed;
  704. /* "flow" corresponds to Tx queue */
  705. u16 scd_flow = le16_to_cpu(ba_notif->scd_flow);
  706. /* "ssn" is start of block-ack Tx window, corresponds to index
  707. * (in Tx queue's circular buffer) of first TFD/frame in window */
  708. u16 ba_resp_scd_ssn = le16_to_cpu(ba_notif->scd_ssn);
  709. sta_id = ba_notif->sta_id;
  710. tid = ba_notif->tid;
  711. rcu_read_lock();
  712. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  713. /* Reclaiming frames for a station that has been deleted ? */
  714. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  715. rcu_read_unlock();
  716. return 0;
  717. }
  718. mvmsta = (void *)sta->drv_priv;
  719. tid_data = &mvmsta->tid_data[tid];
  720. if (WARN_ONCE(tid_data->txq_id != scd_flow, "Q %d, tid %d, flow %d",
  721. tid_data->txq_id, tid, scd_flow)) {
  722. rcu_read_unlock();
  723. return 0;
  724. }
  725. spin_lock_bh(&mvmsta->lock);
  726. __skb_queue_head_init(&reclaimed_skbs);
  727. /*
  728. * Release all TFDs before the SSN, i.e. all TFDs in front of
  729. * block-ack window (we assume that they've been successfully
  730. * transmitted ... if not, it's too late anyway).
  731. */
  732. iwl_trans_reclaim(mvm->trans, scd_flow, ba_resp_scd_ssn,
  733. &reclaimed_skbs);
  734. IWL_DEBUG_TX_REPLY(mvm,
  735. "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
  736. (u8 *)&ba_notif->sta_addr_lo32,
  737. ba_notif->sta_id);
  738. IWL_DEBUG_TX_REPLY(mvm,
  739. "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
  740. ba_notif->tid, le16_to_cpu(ba_notif->seq_ctl),
  741. (unsigned long long)le64_to_cpu(ba_notif->bitmap),
  742. scd_flow, ba_resp_scd_ssn, ba_notif->txed,
  743. ba_notif->txed_2_done);
  744. tid_data->next_reclaimed = ba_resp_scd_ssn;
  745. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  746. freed = 0;
  747. skb_queue_walk(&reclaimed_skbs, skb) {
  748. hdr = (struct ieee80211_hdr *)skb->data;
  749. if (ieee80211_is_data_qos(hdr->frame_control))
  750. freed++;
  751. else
  752. WARN_ON_ONCE(1);
  753. info = IEEE80211_SKB_CB(skb);
  754. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  755. if (freed == 1) {
  756. /* this is the first skb we deliver in this batch */
  757. /* put the rate scaling data there */
  758. info = IEEE80211_SKB_CB(skb);
  759. memset(&info->status, 0, sizeof(info->status));
  760. info->flags |= IEEE80211_TX_STAT_ACK;
  761. info->flags |= IEEE80211_TX_STAT_AMPDU;
  762. info->status.ampdu_ack_len = ba_notif->txed_2_done;
  763. info->status.ampdu_len = ba_notif->txed;
  764. iwl_mvm_hwrate_to_tx_control(tid_data->rate_n_flags,
  765. info);
  766. }
  767. }
  768. spin_unlock_bh(&mvmsta->lock);
  769. rcu_read_unlock();
  770. while (!skb_queue_empty(&reclaimed_skbs)) {
  771. skb = __skb_dequeue(&reclaimed_skbs);
  772. ieee80211_tx_status_ni(mvm->hw, skb);
  773. }
  774. return 0;
  775. }
  776. int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk, bool sync)
  777. {
  778. int ret;
  779. struct iwl_tx_path_flush_cmd flush_cmd = {
  780. .queues_ctl = cpu_to_le32(tfd_msk),
  781. .flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
  782. };
  783. u32 flags = sync ? CMD_SYNC : CMD_ASYNC;
  784. ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
  785. sizeof(flush_cmd), &flush_cmd);
  786. if (ret)
  787. IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
  788. return ret;
  789. }