tx.c 29 KB

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