tx.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  246. if (info->control.hw_key)
  247. iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb);
  248. iwl_mvm_set_tx_cmd(mvm, skb, tx_cmd, info, sta_id);
  249. iwl_mvm_set_tx_cmd_rate(mvm, tx_cmd, info, sta, hdr->frame_control);
  250. memset(&info->status, 0, sizeof(info->status));
  251. info->driver_data[0] = NULL;
  252. info->driver_data[1] = dev_cmd;
  253. return dev_cmd;
  254. }
  255. int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
  256. {
  257. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  258. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  259. struct iwl_device_cmd *dev_cmd;
  260. struct iwl_tx_cmd *tx_cmd;
  261. u8 sta_id;
  262. if (WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU))
  263. return -1;
  264. if (WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
  265. (!info->control.vif ||
  266. info->hw_queue != info->control.vif->cab_queue)))
  267. return -1;
  268. /*
  269. * If the interface on which frame is sent is the P2P_DEVICE
  270. * or an AP/GO interface use the broadcast station associated
  271. * with it; otherwise use the AUX station.
  272. */
  273. if (info->control.vif &&
  274. (info->control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
  275. info->control.vif->type == NL80211_IFTYPE_AP)) {
  276. struct iwl_mvm_vif *mvmvif =
  277. iwl_mvm_vif_from_mac80211(info->control.vif);
  278. sta_id = mvmvif->bcast_sta.sta_id;
  279. } else {
  280. sta_id = mvm->aux_sta.sta_id;
  281. }
  282. IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, info->hw_queue);
  283. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, NULL, sta_id);
  284. if (!dev_cmd)
  285. return -1;
  286. /* From now on, we cannot access info->control */
  287. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  288. /* Copy MAC header from skb into command buffer */
  289. memcpy(tx_cmd->hdr, hdr, ieee80211_hdrlen(hdr->frame_control));
  290. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, info->hw_queue)) {
  291. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  292. return -1;
  293. }
  294. return 0;
  295. }
  296. /*
  297. * Sets the fields in the Tx cmd that are crypto related
  298. */
  299. int iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
  300. struct ieee80211_sta *sta)
  301. {
  302. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  303. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  304. struct iwl_mvm_sta *mvmsta;
  305. struct iwl_device_cmd *dev_cmd;
  306. struct iwl_tx_cmd *tx_cmd;
  307. __le16 fc;
  308. u16 seq_number = 0;
  309. u8 tid = IWL_MAX_TID_COUNT;
  310. u8 txq_id = info->hw_queue;
  311. bool is_data_qos = false, is_ampdu = false;
  312. mvmsta = (void *)sta->drv_priv;
  313. fc = hdr->frame_control;
  314. if (WARN_ON_ONCE(!mvmsta))
  315. return -1;
  316. if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
  317. return -1;
  318. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, sta, mvmsta->sta_id);
  319. if (!dev_cmd)
  320. goto drop;
  321. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  322. /* From now on, we cannot access info->control */
  323. spin_lock(&mvmsta->lock);
  324. if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
  325. u8 *qc = NULL;
  326. qc = ieee80211_get_qos_ctl(hdr);
  327. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  328. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  329. goto drop_unlock_sta;
  330. seq_number = mvmsta->tid_data[tid].seq_number;
  331. seq_number &= IEEE80211_SCTL_SEQ;
  332. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  333. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  334. seq_number += 0x10;
  335. is_data_qos = true;
  336. is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
  337. }
  338. /* Copy MAC header from skb into command buffer */
  339. memcpy(tx_cmd->hdr, hdr, ieee80211_hdrlen(fc));
  340. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
  341. if (is_ampdu) {
  342. if (WARN_ON_ONCE(mvmsta->tid_data[tid].state != IWL_AGG_ON))
  343. goto drop_unlock_sta;
  344. txq_id = mvmsta->tid_data[tid].txq_id;
  345. }
  346. IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x\n", mvmsta->sta_id,
  347. tid, txq_id, seq_number);
  348. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, txq_id))
  349. goto drop_unlock_sta;
  350. if (is_data_qos && !ieee80211_has_morefrags(fc))
  351. mvmsta->tid_data[tid].seq_number = seq_number;
  352. spin_unlock(&mvmsta->lock);
  353. if (txq_id < IWL_MVM_FIRST_AGG_QUEUE)
  354. atomic_inc(&mvm->pending_frames[mvmsta->sta_id]);
  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_MVM_FIRST_AGG_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 status isn't success */
  520. if (status != TX_STATUS_SUCCESS) {
  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_MVM_FIRST_AGG_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 = IEEE80211_SEQ_TO_SN(seq_ctl + 0x10);
  545. }
  546. IWL_DEBUG_TX_REPLY(mvm,
  547. "TXQ %d status %s (0x%08x)\n",
  548. txq_id, iwl_mvm_get_tx_fail_reason(status), status);
  549. IWL_DEBUG_TX_REPLY(mvm,
  550. "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d next_reclaimed=0x%x seq_ctl=0x%x\n",
  551. le32_to_cpu(tx_resp->initial_rate),
  552. tx_resp->failure_frame, SEQ_TO_INDEX(sequence),
  553. ssn, next_reclaimed, seq_ctl);
  554. rcu_read_lock();
  555. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  556. if (!IS_ERR_OR_NULL(sta)) {
  557. mvmsta = (void *)sta->drv_priv;
  558. if (tid != IWL_TID_NON_QOS) {
  559. struct iwl_mvm_tid_data *tid_data =
  560. &mvmsta->tid_data[tid];
  561. spin_lock_bh(&mvmsta->lock);
  562. tid_data->next_reclaimed = next_reclaimed;
  563. IWL_DEBUG_TX_REPLY(mvm, "Next reclaimed packet:%d\n",
  564. next_reclaimed);
  565. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  566. spin_unlock_bh(&mvmsta->lock);
  567. }
  568. #ifdef CONFIG_PM_SLEEP
  569. mvmsta->last_seq_ctl = seq_ctl;
  570. #endif
  571. } else {
  572. sta = NULL;
  573. mvmsta = NULL;
  574. }
  575. /*
  576. * If the txq is not an AMPDU queue, there is no chance we freed
  577. * several skbs. Check that out...
  578. */
  579. if (txq_id < IWL_MVM_FIRST_AGG_QUEUE && !WARN_ON(skb_freed > 1) &&
  580. atomic_sub_and_test(skb_freed, &mvm->pending_frames[sta_id])) {
  581. if (mvmsta) {
  582. /*
  583. * If there are no pending frames for this STA, notify
  584. * mac80211 that this station can go to sleep in its
  585. * STA table.
  586. */
  587. if (mvmsta->vif->type == NL80211_IFTYPE_AP)
  588. ieee80211_sta_block_awake(mvm->hw, sta, false);
  589. /*
  590. * We might very well have taken mvmsta pointer while
  591. * the station was being removed. The remove flow might
  592. * have seen a pending_frame (because we didn't take
  593. * the lock) even if now the queues are drained. So make
  594. * really sure now that this the station is not being
  595. * removed. If it is, run the drain worker to remove it.
  596. */
  597. spin_lock_bh(&mvmsta->lock);
  598. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  599. if (IS_ERR_OR_NULL(sta)) {
  600. /*
  601. * Station disappeared in the meantime:
  602. * so we are draining.
  603. */
  604. set_bit(sta_id, mvm->sta_drained);
  605. schedule_work(&mvm->sta_drained_wk);
  606. }
  607. spin_unlock_bh(&mvmsta->lock);
  608. } else if (!mvmsta) {
  609. /* Tx response without STA, so we are draining */
  610. set_bit(sta_id, mvm->sta_drained);
  611. schedule_work(&mvm->sta_drained_wk);
  612. }
  613. }
  614. rcu_read_unlock();
  615. }
  616. #ifdef CONFIG_IWLWIFI_DEBUG
  617. #define AGG_TX_STATE_(x) case AGG_TX_STATE_ ## x: return #x
  618. static const char *iwl_get_agg_tx_status(u16 status)
  619. {
  620. switch (status & AGG_TX_STATE_STATUS_MSK) {
  621. AGG_TX_STATE_(TRANSMITTED);
  622. AGG_TX_STATE_(UNDERRUN);
  623. AGG_TX_STATE_(BT_PRIO);
  624. AGG_TX_STATE_(FEW_BYTES);
  625. AGG_TX_STATE_(ABORT);
  626. AGG_TX_STATE_(LAST_SENT_TTL);
  627. AGG_TX_STATE_(LAST_SENT_TRY_CNT);
  628. AGG_TX_STATE_(LAST_SENT_BT_KILL);
  629. AGG_TX_STATE_(SCD_QUERY);
  630. AGG_TX_STATE_(TEST_BAD_CRC32);
  631. AGG_TX_STATE_(RESPONSE);
  632. AGG_TX_STATE_(DUMP_TX);
  633. AGG_TX_STATE_(DELAY_TX);
  634. }
  635. return "UNKNOWN";
  636. }
  637. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  638. struct iwl_rx_packet *pkt)
  639. {
  640. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  641. struct agg_tx_status *frame_status = &tx_resp->status;
  642. int i;
  643. for (i = 0; i < tx_resp->frame_count; i++) {
  644. u16 fstatus = le16_to_cpu(frame_status[i].status);
  645. IWL_DEBUG_TX_REPLY(mvm,
  646. "status %s (0x%04x), try-count (%d) seq (0x%x)\n",
  647. iwl_get_agg_tx_status(fstatus),
  648. fstatus & AGG_TX_STATE_STATUS_MSK,
  649. (fstatus & AGG_TX_STATE_TRY_CNT_MSK) >>
  650. AGG_TX_STATE_TRY_CNT_POS,
  651. le16_to_cpu(frame_status[i].sequence));
  652. }
  653. }
  654. #else
  655. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  656. struct iwl_rx_packet *pkt)
  657. {}
  658. #endif /* CONFIG_IWLWIFI_DEBUG */
  659. static void iwl_mvm_rx_tx_cmd_agg(struct iwl_mvm *mvm,
  660. struct iwl_rx_packet *pkt)
  661. {
  662. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  663. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  664. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  665. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  666. struct ieee80211_sta *sta;
  667. if (WARN_ON_ONCE(SEQ_TO_QUEUE(sequence) < IWL_MVM_FIRST_AGG_QUEUE))
  668. return;
  669. if (WARN_ON_ONCE(tid == IWL_TID_NON_QOS))
  670. return;
  671. iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);
  672. rcu_read_lock();
  673. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  674. if (!WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  675. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  676. mvmsta->tid_data[tid].rate_n_flags =
  677. le32_to_cpu(tx_resp->initial_rate);
  678. }
  679. rcu_read_unlock();
  680. }
  681. int iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  682. struct iwl_device_cmd *cmd)
  683. {
  684. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  685. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  686. if (tx_resp->frame_count == 1)
  687. iwl_mvm_rx_tx_cmd_single(mvm, pkt);
  688. else
  689. iwl_mvm_rx_tx_cmd_agg(mvm, pkt);
  690. return 0;
  691. }
  692. int iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  693. struct iwl_device_cmd *cmd)
  694. {
  695. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  696. struct iwl_mvm_ba_notif *ba_notif = (void *)pkt->data;
  697. struct sk_buff_head reclaimed_skbs;
  698. struct iwl_mvm_tid_data *tid_data;
  699. struct ieee80211_tx_info *info;
  700. struct ieee80211_sta *sta;
  701. struct iwl_mvm_sta *mvmsta;
  702. struct ieee80211_hdr *hdr;
  703. struct sk_buff *skb;
  704. int sta_id, tid, freed;
  705. /* "flow" corresponds to Tx queue */
  706. u16 scd_flow = le16_to_cpu(ba_notif->scd_flow);
  707. /* "ssn" is start of block-ack Tx window, corresponds to index
  708. * (in Tx queue's circular buffer) of first TFD/frame in window */
  709. u16 ba_resp_scd_ssn = le16_to_cpu(ba_notif->scd_ssn);
  710. sta_id = ba_notif->sta_id;
  711. tid = ba_notif->tid;
  712. rcu_read_lock();
  713. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  714. /* Reclaiming frames for a station that has been deleted ? */
  715. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  716. rcu_read_unlock();
  717. return 0;
  718. }
  719. mvmsta = (void *)sta->drv_priv;
  720. tid_data = &mvmsta->tid_data[tid];
  721. if (WARN_ONCE(tid_data->txq_id != scd_flow, "Q %d, tid %d, flow %d",
  722. tid_data->txq_id, tid, scd_flow)) {
  723. rcu_read_unlock();
  724. return 0;
  725. }
  726. spin_lock_bh(&mvmsta->lock);
  727. __skb_queue_head_init(&reclaimed_skbs);
  728. /*
  729. * Release all TFDs before the SSN, i.e. all TFDs in front of
  730. * block-ack window (we assume that they've been successfully
  731. * transmitted ... if not, it's too late anyway).
  732. */
  733. iwl_trans_reclaim(mvm->trans, scd_flow, ba_resp_scd_ssn,
  734. &reclaimed_skbs);
  735. IWL_DEBUG_TX_REPLY(mvm,
  736. "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
  737. (u8 *)&ba_notif->sta_addr_lo32,
  738. ba_notif->sta_id);
  739. IWL_DEBUG_TX_REPLY(mvm,
  740. "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
  741. ba_notif->tid, le16_to_cpu(ba_notif->seq_ctl),
  742. (unsigned long long)le64_to_cpu(ba_notif->bitmap),
  743. scd_flow, ba_resp_scd_ssn, ba_notif->txed,
  744. ba_notif->txed_2_done);
  745. tid_data->next_reclaimed = ba_resp_scd_ssn;
  746. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  747. freed = 0;
  748. skb_queue_walk(&reclaimed_skbs, skb) {
  749. hdr = (struct ieee80211_hdr *)skb->data;
  750. if (ieee80211_is_data_qos(hdr->frame_control))
  751. freed++;
  752. else
  753. WARN_ON_ONCE(1);
  754. info = IEEE80211_SKB_CB(skb);
  755. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  756. if (freed == 1) {
  757. /* this is the first skb we deliver in this batch */
  758. /* put the rate scaling data there */
  759. info = IEEE80211_SKB_CB(skb);
  760. memset(&info->status, 0, sizeof(info->status));
  761. info->flags |= IEEE80211_TX_STAT_ACK;
  762. info->flags |= IEEE80211_TX_STAT_AMPDU;
  763. info->status.ampdu_ack_len = ba_notif->txed_2_done;
  764. info->status.ampdu_len = ba_notif->txed;
  765. iwl_mvm_hwrate_to_tx_control(tid_data->rate_n_flags,
  766. info);
  767. }
  768. }
  769. spin_unlock_bh(&mvmsta->lock);
  770. rcu_read_unlock();
  771. while (!skb_queue_empty(&reclaimed_skbs)) {
  772. skb = __skb_dequeue(&reclaimed_skbs);
  773. ieee80211_tx_status_ni(mvm->hw, skb);
  774. }
  775. return 0;
  776. }
  777. int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk, bool sync)
  778. {
  779. int ret;
  780. struct iwl_tx_path_flush_cmd flush_cmd = {
  781. .queues_ctl = cpu_to_le32(tfd_msk),
  782. .flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
  783. };
  784. u32 flags = sync ? CMD_SYNC : CMD_ASYNC;
  785. ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
  786. sizeof(flush_cmd), &flush_cmd);
  787. if (ret)
  788. IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
  789. return ret;
  790. }