tx.c 28 KB

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