tx.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. *
  43. * * Redistributions of source code must retain the above copyright
  44. * notice, this list of conditions and the following disclaimer.
  45. * * Redistributions in binary form must reproduce the above copyright
  46. * notice, this list of conditions and the following disclaimer in
  47. * the documentation and/or other materials provided with the
  48. * distribution.
  49. * * Neither the name Intel Corporation nor the names of its
  50. * contributors may be used to endorse or promote products derived
  51. * from this software without specific prior written permission.
  52. *
  53. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  54. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  55. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  56. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  57. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  58. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  59. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  60. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  61. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  62. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  63. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  64. *
  65. *****************************************************************************/
  66. #include <linux/ieee80211.h>
  67. #include <linux/etherdevice.h>
  68. #include <linux/tcp.h>
  69. #include <net/ip.h>
  70. #include "iwl-trans.h"
  71. #include "iwl-eeprom-parse.h"
  72. #include "mvm.h"
  73. #include "sta.h"
  74. #include "fw-dbg.h"
  75. static void
  76. iwl_mvm_bar_check_trigger(struct iwl_mvm *mvm, const u8 *addr,
  77. u16 tid, u16 ssn)
  78. {
  79. struct iwl_fw_dbg_trigger_tlv *trig;
  80. struct iwl_fw_dbg_trigger_ba *ba_trig;
  81. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
  82. return;
  83. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
  84. ba_trig = (void *)trig->data;
  85. if (!iwl_fw_dbg_trigger_check_stop(mvm, NULL, trig))
  86. return;
  87. if (!(le16_to_cpu(ba_trig->tx_bar) & BIT(tid)))
  88. return;
  89. iwl_mvm_fw_dbg_collect_trig(mvm, trig,
  90. "BAR sent to %pM, tid %d, ssn %d",
  91. addr, tid, ssn);
  92. }
  93. /*
  94. * Sets most of the Tx cmd's fields
  95. */
  96. void iwl_mvm_set_tx_cmd(struct iwl_mvm *mvm, struct sk_buff *skb,
  97. struct iwl_tx_cmd *tx_cmd,
  98. struct ieee80211_tx_info *info, u8 sta_id)
  99. {
  100. struct ieee80211_hdr *hdr = (void *)skb->data;
  101. __le16 fc = hdr->frame_control;
  102. u32 tx_flags = le32_to_cpu(tx_cmd->tx_flags);
  103. u32 len = skb->len + FCS_LEN;
  104. u8 ac;
  105. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  106. tx_flags |= TX_CMD_FLG_ACK;
  107. else
  108. tx_flags &= ~TX_CMD_FLG_ACK;
  109. if (ieee80211_is_probe_resp(fc))
  110. tx_flags |= TX_CMD_FLG_TSF;
  111. if (ieee80211_has_morefrags(fc))
  112. tx_flags |= TX_CMD_FLG_MORE_FRAG;
  113. if (ieee80211_is_data_qos(fc)) {
  114. u8 *qc = ieee80211_get_qos_ctl(hdr);
  115. tx_cmd->tid_tspec = qc[0] & 0xf;
  116. tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
  117. } else if (ieee80211_is_back_req(fc)) {
  118. struct ieee80211_bar *bar = (void *)skb->data;
  119. u16 control = le16_to_cpu(bar->control);
  120. u16 ssn = le16_to_cpu(bar->start_seq_num);
  121. tx_flags |= TX_CMD_FLG_ACK | TX_CMD_FLG_BAR;
  122. tx_cmd->tid_tspec = (control &
  123. IEEE80211_BAR_CTRL_TID_INFO_MASK) >>
  124. IEEE80211_BAR_CTRL_TID_INFO_SHIFT;
  125. WARN_ON_ONCE(tx_cmd->tid_tspec >= IWL_MAX_TID_COUNT);
  126. iwl_mvm_bar_check_trigger(mvm, bar->ra, tx_cmd->tid_tspec,
  127. ssn);
  128. } else {
  129. tx_cmd->tid_tspec = IWL_TID_NON_QOS;
  130. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  131. tx_flags |= TX_CMD_FLG_SEQ_CTL;
  132. else
  133. tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
  134. }
  135. /* Default to 0 (BE) when tid_spec is set to IWL_TID_NON_QOS */
  136. if (tx_cmd->tid_tspec < IWL_MAX_TID_COUNT)
  137. ac = tid_to_mac80211_ac[tx_cmd->tid_tspec];
  138. else
  139. ac = tid_to_mac80211_ac[0];
  140. tx_flags |= iwl_mvm_bt_coex_tx_prio(mvm, hdr, info, ac) <<
  141. TX_CMD_FLG_BT_PRIO_POS;
  142. if (ieee80211_is_mgmt(fc)) {
  143. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  144. tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_ASSOC);
  145. else if (ieee80211_is_action(fc))
  146. tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
  147. else
  148. tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
  149. /* The spec allows Action frames in A-MPDU, we don't support
  150. * it
  151. */
  152. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU);
  153. } else if (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) {
  154. tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
  155. } else {
  156. tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
  157. }
  158. if (ieee80211_is_data(fc) && len > mvm->rts_threshold &&
  159. !is_multicast_ether_addr(ieee80211_get_DA(hdr)))
  160. tx_flags |= TX_CMD_FLG_PROT_REQUIRE;
  161. if (fw_has_capa(&mvm->fw->ucode_capa,
  162. IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT) &&
  163. ieee80211_action_contains_tpc(skb))
  164. tx_flags |= TX_CMD_FLG_WRITE_TX_POWER;
  165. tx_cmd->tx_flags = cpu_to_le32(tx_flags);
  166. /* Total # bytes to be transmitted */
  167. tx_cmd->len = cpu_to_le16((u16)skb->len +
  168. (uintptr_t)info->driver_data[0]);
  169. tx_cmd->next_frame_len = 0;
  170. tx_cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  171. tx_cmd->sta_id = sta_id;
  172. }
  173. /*
  174. * Sets the fields in the Tx cmd that are rate related
  175. */
  176. void iwl_mvm_set_tx_cmd_rate(struct iwl_mvm *mvm, struct iwl_tx_cmd *tx_cmd,
  177. struct ieee80211_tx_info *info,
  178. struct ieee80211_sta *sta, __le16 fc)
  179. {
  180. u32 rate_flags;
  181. int rate_idx;
  182. u8 rate_plcp;
  183. /* Set retry limit on RTS packets */
  184. tx_cmd->rts_retry_limit = IWL_RTS_DFAULT_RETRY_LIMIT;
  185. /* Set retry limit on DATA packets and Probe Responses*/
  186. if (ieee80211_is_probe_resp(fc)) {
  187. tx_cmd->data_retry_limit = IWL_MGMT_DFAULT_RETRY_LIMIT;
  188. tx_cmd->rts_retry_limit =
  189. min(tx_cmd->data_retry_limit, tx_cmd->rts_retry_limit);
  190. } else if (ieee80211_is_back_req(fc)) {
  191. tx_cmd->data_retry_limit = IWL_BAR_DFAULT_RETRY_LIMIT;
  192. } else {
  193. tx_cmd->data_retry_limit = IWL_DEFAULT_TX_RETRY;
  194. }
  195. /*
  196. * for data packets, rate info comes from the table inside the fw. This
  197. * table is controlled by LINK_QUALITY commands
  198. */
  199. if (ieee80211_is_data(fc) && sta) {
  200. tx_cmd->initial_rate_index = 0;
  201. tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_STA_RATE);
  202. return;
  203. } else if (ieee80211_is_back_req(fc)) {
  204. tx_cmd->tx_flags |=
  205. cpu_to_le32(TX_CMD_FLG_ACK | TX_CMD_FLG_BAR);
  206. }
  207. /* HT rate doesn't make sense for a non data frame */
  208. WARN_ONCE(info->control.rates[0].flags & IEEE80211_TX_RC_MCS,
  209. "Got an HT rate (flags:0x%x/mcs:%d) for a non data frame (fc:0x%x)\n",
  210. info->control.rates[0].flags,
  211. info->control.rates[0].idx,
  212. le16_to_cpu(fc));
  213. rate_idx = info->control.rates[0].idx;
  214. /* if the rate isn't a well known legacy rate, take the lowest one */
  215. if (rate_idx < 0 || rate_idx > IWL_RATE_COUNT_LEGACY)
  216. rate_idx = rate_lowest_index(
  217. &mvm->nvm_data->bands[info->band], sta);
  218. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  219. if (info->band == IEEE80211_BAND_5GHZ)
  220. rate_idx += IWL_FIRST_OFDM_RATE;
  221. /* For 2.4 GHZ band, check that there is no need to remap */
  222. BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
  223. /* Get PLCP rate for tx_cmd->rate_n_flags */
  224. rate_plcp = iwl_mvm_mac80211_idx_to_hwrate(rate_idx);
  225. mvm->mgmt_last_antenna_idx =
  226. iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
  227. mvm->mgmt_last_antenna_idx);
  228. if (info->band == IEEE80211_BAND_2GHZ &&
  229. !iwl_mvm_bt_coex_is_shared_ant_avail(mvm))
  230. rate_flags = mvm->cfg->non_shared_ant << RATE_MCS_ANT_POS;
  231. else
  232. rate_flags =
  233. BIT(mvm->mgmt_last_antenna_idx) << RATE_MCS_ANT_POS;
  234. /* Set CCK flag as needed */
  235. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  236. rate_flags |= RATE_MCS_CCK_MSK;
  237. /* Set the rate in the TX cmd */
  238. tx_cmd->rate_n_flags = cpu_to_le32((u32)rate_plcp | rate_flags);
  239. }
  240. /*
  241. * Sets the fields in the Tx cmd that are crypto related
  242. */
  243. static void iwl_mvm_set_tx_cmd_crypto(struct iwl_mvm *mvm,
  244. struct ieee80211_tx_info *info,
  245. struct iwl_tx_cmd *tx_cmd,
  246. struct sk_buff *skb_frag,
  247. int hdrlen)
  248. {
  249. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  250. u8 *crypto_hdr = skb_frag->data + hdrlen;
  251. u64 pn;
  252. switch (keyconf->cipher) {
  253. case WLAN_CIPHER_SUITE_CCMP:
  254. case WLAN_CIPHER_SUITE_CCMP_256:
  255. iwl_mvm_set_tx_cmd_ccmp(info, tx_cmd);
  256. pn = atomic64_inc_return(&keyconf->tx_pn);
  257. crypto_hdr[0] = pn;
  258. crypto_hdr[2] = 0;
  259. crypto_hdr[3] = 0x20 | (keyconf->keyidx << 6);
  260. crypto_hdr[1] = pn >> 8;
  261. crypto_hdr[4] = pn >> 16;
  262. crypto_hdr[5] = pn >> 24;
  263. crypto_hdr[6] = pn >> 32;
  264. crypto_hdr[7] = pn >> 40;
  265. break;
  266. case WLAN_CIPHER_SUITE_TKIP:
  267. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  268. pn = atomic64_inc_return(&keyconf->tx_pn);
  269. ieee80211_tkip_add_iv(crypto_hdr, keyconf, pn);
  270. ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
  271. break;
  272. case WLAN_CIPHER_SUITE_WEP104:
  273. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  274. /* fall through */
  275. case WLAN_CIPHER_SUITE_WEP40:
  276. tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
  277. ((keyconf->keyidx << TX_CMD_SEC_WEP_KEY_IDX_POS) &
  278. TX_CMD_SEC_WEP_KEY_IDX_MSK);
  279. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  280. break;
  281. default:
  282. tx_cmd->sec_ctl |= TX_CMD_SEC_EXT;
  283. }
  284. }
  285. /*
  286. * Allocates and sets the Tx cmd the driver data pointers in the skb
  287. */
  288. static struct iwl_device_cmd *
  289. iwl_mvm_set_tx_params(struct iwl_mvm *mvm, struct sk_buff *skb,
  290. int hdrlen, struct ieee80211_sta *sta, u8 sta_id)
  291. {
  292. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  293. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  294. struct iwl_device_cmd *dev_cmd;
  295. struct iwl_tx_cmd *tx_cmd;
  296. dev_cmd = iwl_trans_alloc_tx_cmd(mvm->trans);
  297. if (unlikely(!dev_cmd))
  298. return NULL;
  299. memset(dev_cmd, 0, sizeof(*dev_cmd));
  300. dev_cmd->hdr.cmd = TX_CMD;
  301. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  302. if (info->control.hw_key)
  303. iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb, hdrlen);
  304. iwl_mvm_set_tx_cmd(mvm, skb, tx_cmd, info, sta_id);
  305. iwl_mvm_set_tx_cmd_rate(mvm, tx_cmd, info, sta, hdr->frame_control);
  306. memset(&info->status, 0, sizeof(info->status));
  307. memset(info->driver_data, 0, sizeof(info->driver_data));
  308. info->driver_data[1] = dev_cmd;
  309. return dev_cmd;
  310. }
  311. int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
  312. {
  313. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  314. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  315. struct iwl_device_cmd *dev_cmd;
  316. struct iwl_tx_cmd *tx_cmd;
  317. u8 sta_id;
  318. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  319. if (WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU))
  320. return -1;
  321. if (WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
  322. (!info->control.vif ||
  323. info->hw_queue != info->control.vif->cab_queue)))
  324. return -1;
  325. /* This holds the amsdu headers length */
  326. info->driver_data[0] = (void *)(uintptr_t)0;
  327. /*
  328. * IWL_MVM_OFFCHANNEL_QUEUE is used for ROC packets that can be used
  329. * in 2 different types of vifs, P2P & STATION. P2P uses the offchannel
  330. * queue. STATION (HS2.0) uses the auxiliary context of the FW,
  331. * and hence needs to be sent on the aux queue
  332. */
  333. if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
  334. info->control.vif->type == NL80211_IFTYPE_STATION)
  335. IEEE80211_SKB_CB(skb)->hw_queue = mvm->aux_queue;
  336. /*
  337. * If the interface on which the frame is sent is the P2P_DEVICE
  338. * or an AP/GO interface use the broadcast station associated
  339. * with it; otherwise if the interface is a managed interface
  340. * use the AP station associated with it for multicast traffic
  341. * (this is not possible for unicast packets as a TLDS discovery
  342. * response are sent without a station entry); otherwise use the
  343. * AUX station.
  344. */
  345. sta_id = mvm->aux_sta.sta_id;
  346. if (info->control.vif) {
  347. struct iwl_mvm_vif *mvmvif =
  348. iwl_mvm_vif_from_mac80211(info->control.vif);
  349. if (info->control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
  350. info->control.vif->type == NL80211_IFTYPE_AP)
  351. sta_id = mvmvif->bcast_sta.sta_id;
  352. else if (info->control.vif->type == NL80211_IFTYPE_STATION &&
  353. is_multicast_ether_addr(hdr->addr1)) {
  354. u8 ap_sta_id = ACCESS_ONCE(mvmvif->ap_sta_id);
  355. if (ap_sta_id != IWL_MVM_STATION_COUNT)
  356. sta_id = ap_sta_id;
  357. }
  358. }
  359. IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, info->hw_queue);
  360. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, hdrlen, NULL, sta_id);
  361. if (!dev_cmd)
  362. return -1;
  363. /* From now on, we cannot access info->control */
  364. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  365. /* Copy MAC header from skb into command buffer */
  366. memcpy(tx_cmd->hdr, hdr, hdrlen);
  367. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, info->hw_queue)) {
  368. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  369. return -1;
  370. }
  371. /*
  372. * Increase the pending frames counter, so that later when a reply comes
  373. * in and the counter is decreased - we don't start getting negative
  374. * values.
  375. * Note that we don't need to make sure it isn't agg'd, since we're
  376. * TXing non-sta
  377. */
  378. atomic_inc(&mvm->pending_frames[sta_id]);
  379. return 0;
  380. }
  381. #ifdef CONFIG_INET
  382. static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
  383. struct ieee80211_sta *sta,
  384. struct sk_buff_head *mpdus_skb)
  385. {
  386. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  387. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  388. struct ieee80211_hdr *hdr = (void *)skb->data;
  389. unsigned int mss = skb_shinfo(skb)->gso_size;
  390. struct sk_buff *tmp, *next;
  391. char cb[sizeof(skb->cb)];
  392. unsigned int num_subframes, tcp_payload_len, subf_len, max_amsdu_len;
  393. bool ipv4 = (skb->protocol == htons(ETH_P_IP));
  394. u16 ip_base_id = ipv4 ? ntohs(ip_hdr(skb)->id) : 0;
  395. u16 amsdu_add, snap_ip_tcp, pad, i = 0;
  396. unsigned int dbg_max_amsdu_len;
  397. u8 *qc, tid, txf;
  398. snap_ip_tcp = 8 + skb_transport_header(skb) - skb_network_header(skb) +
  399. tcp_hdrlen(skb);
  400. qc = ieee80211_get_qos_ctl(hdr);
  401. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  402. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  403. return -EINVAL;
  404. if (!sta->max_amsdu_len ||
  405. !ieee80211_is_data_qos(hdr->frame_control) ||
  406. !mvmsta->tlc_amsdu) {
  407. num_subframes = 1;
  408. pad = 0;
  409. goto segment;
  410. }
  411. /*
  412. * No need to lock amsdu_in_ampdu_allowed since it can't be modified
  413. * during an BA session.
  414. */
  415. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  416. !mvmsta->tid_data[tid].amsdu_in_ampdu_allowed) {
  417. num_subframes = 1;
  418. pad = 0;
  419. goto segment;
  420. }
  421. max_amsdu_len = sta->max_amsdu_len;
  422. dbg_max_amsdu_len = ACCESS_ONCE(mvm->max_amsdu_len);
  423. /* the Tx FIFO to which this A-MSDU will be routed */
  424. txf = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
  425. /*
  426. * Don't send an AMSDU that will be longer than the TXF.
  427. * Add a security margin of 256 for the TX command + headers.
  428. * We also want to have the start of the next packet inside the
  429. * fifo to be able to send bursts.
  430. */
  431. max_amsdu_len = min_t(unsigned int, max_amsdu_len,
  432. mvm->shared_mem_cfg.txfifo_size[txf] - 256);
  433. if (dbg_max_amsdu_len)
  434. max_amsdu_len = min_t(unsigned int, max_amsdu_len,
  435. dbg_max_amsdu_len);
  436. /*
  437. * Limit A-MSDU in A-MPDU to 4095 bytes when VHT is not
  438. * supported. This is a spec requirement (IEEE 802.11-2015
  439. * section 8.7.3 NOTE 3).
  440. */
  441. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  442. !sta->vht_cap.vht_supported)
  443. max_amsdu_len = min_t(unsigned int, max_amsdu_len, 4095);
  444. /* Sub frame header + SNAP + IP header + TCP header + MSS */
  445. subf_len = sizeof(struct ethhdr) + snap_ip_tcp + mss;
  446. pad = (4 - subf_len) & 0x3;
  447. /*
  448. * If we have N subframes in the A-MSDU, then the A-MSDU's size is
  449. * N * subf_len + (N - 1) * pad.
  450. */
  451. num_subframes = (max_amsdu_len + pad) / (subf_len + pad);
  452. if (num_subframes > 1)
  453. *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  454. tcp_payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
  455. tcp_hdrlen(skb) + skb->data_len;
  456. /*
  457. * Make sure we have enough TBs for the A-MSDU:
  458. * 2 for each subframe
  459. * 1 more for each fragment
  460. * 1 more for the potential data in the header
  461. */
  462. num_subframes =
  463. min_t(unsigned int, num_subframes,
  464. (mvm->trans->max_skb_frags - 1 -
  465. skb_shinfo(skb)->nr_frags) / 2);
  466. /* This skb fits in one single A-MSDU */
  467. if (num_subframes * mss >= tcp_payload_len) {
  468. /*
  469. * Compute the length of all the data added for the A-MSDU.
  470. * This will be used to compute the length to write in the TX
  471. * command. We have: SNAP + IP + TCP for n -1 subframes and
  472. * ETH header for n subframes. Note that the original skb
  473. * already had one set of SNAP / IP / TCP headers.
  474. */
  475. num_subframes = DIV_ROUND_UP(tcp_payload_len, mss);
  476. info = IEEE80211_SKB_CB(skb);
  477. amsdu_add = num_subframes * sizeof(struct ethhdr) +
  478. (num_subframes - 1) * (snap_ip_tcp + pad);
  479. /* This holds the amsdu headers length */
  480. info->driver_data[0] = (void *)(uintptr_t)amsdu_add;
  481. __skb_queue_tail(mpdus_skb, skb);
  482. return 0;
  483. }
  484. /*
  485. * Trick the segmentation function to make it
  486. * create SKBs that can fit into one A-MSDU.
  487. */
  488. segment:
  489. skb_shinfo(skb)->gso_size = num_subframes * mss;
  490. memcpy(cb, skb->cb, sizeof(cb));
  491. next = skb_gso_segment(skb, NETIF_F_CSUM_MASK | NETIF_F_SG);
  492. skb_shinfo(skb)->gso_size = mss;
  493. if (WARN_ON_ONCE(IS_ERR(next)))
  494. return -EINVAL;
  495. else if (next)
  496. consume_skb(skb);
  497. while (next) {
  498. tmp = next;
  499. next = tmp->next;
  500. memcpy(tmp->cb, cb, sizeof(tmp->cb));
  501. /*
  502. * Compute the length of all the data added for the A-MSDU.
  503. * This will be used to compute the length to write in the TX
  504. * command. We have: SNAP + IP + TCP for n -1 subframes and
  505. * ETH header for n subframes.
  506. */
  507. tcp_payload_len = skb_tail_pointer(tmp) -
  508. skb_transport_header(tmp) -
  509. tcp_hdrlen(tmp) + tmp->data_len;
  510. if (ipv4)
  511. ip_hdr(tmp)->id = htons(ip_base_id + i * num_subframes);
  512. if (tcp_payload_len > mss) {
  513. num_subframes = DIV_ROUND_UP(tcp_payload_len, mss);
  514. info = IEEE80211_SKB_CB(tmp);
  515. amsdu_add = num_subframes * sizeof(struct ethhdr) +
  516. (num_subframes - 1) * (snap_ip_tcp + pad);
  517. info->driver_data[0] = (void *)(uintptr_t)amsdu_add;
  518. skb_shinfo(tmp)->gso_size = mss;
  519. } else {
  520. qc = ieee80211_get_qos_ctl((void *)tmp->data);
  521. if (ipv4)
  522. ip_send_check(ip_hdr(tmp));
  523. *qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  524. skb_shinfo(tmp)->gso_size = 0;
  525. }
  526. tmp->prev = NULL;
  527. tmp->next = NULL;
  528. __skb_queue_tail(mpdus_skb, tmp);
  529. i++;
  530. }
  531. return 0;
  532. }
  533. #else /* CONFIG_INET */
  534. static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
  535. struct ieee80211_sta *sta,
  536. struct sk_buff_head *mpdus_skb)
  537. {
  538. /* Impossible to get TSO with CONFIG_INET */
  539. WARN_ON(1);
  540. return -1;
  541. }
  542. #endif
  543. /*
  544. * Sets the fields in the Tx cmd that are crypto related
  545. */
  546. static int iwl_mvm_tx_mpdu(struct iwl_mvm *mvm, struct sk_buff *skb,
  547. struct ieee80211_sta *sta)
  548. {
  549. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  550. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  551. struct iwl_mvm_sta *mvmsta;
  552. struct iwl_device_cmd *dev_cmd;
  553. struct iwl_tx_cmd *tx_cmd;
  554. __le16 fc;
  555. u16 seq_number = 0;
  556. u8 tid = IWL_MAX_TID_COUNT;
  557. u8 txq_id = info->hw_queue;
  558. bool is_data_qos = false, is_ampdu = false;
  559. int hdrlen;
  560. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  561. fc = hdr->frame_control;
  562. hdrlen = ieee80211_hdrlen(fc);
  563. if (WARN_ON_ONCE(!mvmsta))
  564. return -1;
  565. if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
  566. return -1;
  567. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, hdrlen, sta, mvmsta->sta_id);
  568. if (!dev_cmd)
  569. goto drop;
  570. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  571. /* From now on, we cannot access info->control */
  572. /*
  573. * we handle that entirely ourselves -- for uAPSD the firmware
  574. * will always send a notification, and for PS-Poll responses
  575. * we'll notify mac80211 when getting frame status
  576. */
  577. info->flags &= ~IEEE80211_TX_STATUS_EOSP;
  578. spin_lock(&mvmsta->lock);
  579. if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
  580. u8 *qc = NULL;
  581. qc = ieee80211_get_qos_ctl(hdr);
  582. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  583. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  584. goto drop_unlock_sta;
  585. seq_number = mvmsta->tid_data[tid].seq_number;
  586. seq_number &= IEEE80211_SCTL_SEQ;
  587. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  588. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  589. is_data_qos = true;
  590. is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
  591. }
  592. /* Copy MAC header from skb into command buffer */
  593. memcpy(tx_cmd->hdr, hdr, hdrlen);
  594. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
  595. if (sta->tdls) {
  596. /* default to TID 0 for non-QoS packets */
  597. u8 tdls_tid = tid == IWL_MAX_TID_COUNT ? 0 : tid;
  598. txq_id = mvmsta->hw_queue[tid_to_mac80211_ac[tdls_tid]];
  599. }
  600. if (is_ampdu) {
  601. if (WARN_ON_ONCE(mvmsta->tid_data[tid].state != IWL_AGG_ON))
  602. goto drop_unlock_sta;
  603. txq_id = mvmsta->tid_data[tid].txq_id;
  604. }
  605. IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x\n", mvmsta->sta_id,
  606. tid, txq_id, IEEE80211_SEQ_TO_SN(seq_number));
  607. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, txq_id))
  608. goto drop_unlock_sta;
  609. if (is_data_qos && !ieee80211_has_morefrags(fc))
  610. mvmsta->tid_data[tid].seq_number = seq_number + 0x10;
  611. spin_unlock(&mvmsta->lock);
  612. if (txq_id < mvm->first_agg_queue)
  613. atomic_inc(&mvm->pending_frames[mvmsta->sta_id]);
  614. return 0;
  615. drop_unlock_sta:
  616. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  617. spin_unlock(&mvmsta->lock);
  618. drop:
  619. return -1;
  620. }
  621. int iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
  622. struct ieee80211_sta *sta)
  623. {
  624. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  625. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  626. struct sk_buff_head mpdus_skbs;
  627. unsigned int payload_len;
  628. int ret;
  629. if (WARN_ON_ONCE(!mvmsta))
  630. return -1;
  631. if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
  632. return -1;
  633. /* This holds the amsdu headers length */
  634. info->driver_data[0] = (void *)(uintptr_t)0;
  635. if (!skb_is_gso(skb))
  636. return iwl_mvm_tx_mpdu(mvm, skb, sta);
  637. payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
  638. tcp_hdrlen(skb) + skb->data_len;
  639. if (payload_len <= skb_shinfo(skb)->gso_size)
  640. return iwl_mvm_tx_mpdu(mvm, skb, sta);
  641. __skb_queue_head_init(&mpdus_skbs);
  642. ret = iwl_mvm_tx_tso(mvm, skb, sta, &mpdus_skbs);
  643. if (ret)
  644. return ret;
  645. if (WARN_ON(skb_queue_empty(&mpdus_skbs)))
  646. return ret;
  647. while (!skb_queue_empty(&mpdus_skbs)) {
  648. skb = __skb_dequeue(&mpdus_skbs);
  649. ret = iwl_mvm_tx_mpdu(mvm, skb, sta);
  650. if (ret) {
  651. __skb_queue_purge(&mpdus_skbs);
  652. return ret;
  653. }
  654. }
  655. return 0;
  656. }
  657. static void iwl_mvm_check_ratid_empty(struct iwl_mvm *mvm,
  658. struct ieee80211_sta *sta, u8 tid)
  659. {
  660. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  661. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  662. struct ieee80211_vif *vif = mvmsta->vif;
  663. lockdep_assert_held(&mvmsta->lock);
  664. if ((tid_data->state == IWL_AGG_ON ||
  665. tid_data->state == IWL_EMPTYING_HW_QUEUE_DELBA) &&
  666. iwl_mvm_tid_queued(tid_data) == 0) {
  667. /*
  668. * Now that this aggregation queue is empty tell mac80211 so it
  669. * knows we no longer have frames buffered for the station on
  670. * this TID (for the TIM bitmap calculation.)
  671. */
  672. ieee80211_sta_set_buffered(sta, tid, false);
  673. }
  674. if (tid_data->ssn != tid_data->next_reclaimed)
  675. return;
  676. switch (tid_data->state) {
  677. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  678. IWL_DEBUG_TX_QUEUES(mvm,
  679. "Can continue addBA flow ssn = next_recl = %d\n",
  680. tid_data->next_reclaimed);
  681. tid_data->state = IWL_AGG_STARTING;
  682. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  683. break;
  684. case IWL_EMPTYING_HW_QUEUE_DELBA:
  685. IWL_DEBUG_TX_QUEUES(mvm,
  686. "Can continue DELBA flow ssn = next_recl = %d\n",
  687. tid_data->next_reclaimed);
  688. iwl_mvm_disable_txq(mvm, tid_data->txq_id,
  689. vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
  690. CMD_ASYNC);
  691. tid_data->state = IWL_AGG_OFF;
  692. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  693. break;
  694. default:
  695. break;
  696. }
  697. }
  698. #ifdef CONFIG_IWLWIFI_DEBUG
  699. const char *iwl_mvm_get_tx_fail_reason(u32 status)
  700. {
  701. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  702. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  703. switch (status & TX_STATUS_MSK) {
  704. case TX_STATUS_SUCCESS:
  705. return "SUCCESS";
  706. TX_STATUS_POSTPONE(DELAY);
  707. TX_STATUS_POSTPONE(FEW_BYTES);
  708. TX_STATUS_POSTPONE(BT_PRIO);
  709. TX_STATUS_POSTPONE(QUIET_PERIOD);
  710. TX_STATUS_POSTPONE(CALC_TTAK);
  711. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  712. TX_STATUS_FAIL(SHORT_LIMIT);
  713. TX_STATUS_FAIL(LONG_LIMIT);
  714. TX_STATUS_FAIL(UNDERRUN);
  715. TX_STATUS_FAIL(DRAIN_FLOW);
  716. TX_STATUS_FAIL(RFKILL_FLUSH);
  717. TX_STATUS_FAIL(LIFE_EXPIRE);
  718. TX_STATUS_FAIL(DEST_PS);
  719. TX_STATUS_FAIL(HOST_ABORTED);
  720. TX_STATUS_FAIL(BT_RETRY);
  721. TX_STATUS_FAIL(STA_INVALID);
  722. TX_STATUS_FAIL(FRAG_DROPPED);
  723. TX_STATUS_FAIL(TID_DISABLE);
  724. TX_STATUS_FAIL(FIFO_FLUSHED);
  725. TX_STATUS_FAIL(SMALL_CF_POLL);
  726. TX_STATUS_FAIL(FW_DROP);
  727. TX_STATUS_FAIL(STA_COLOR_MISMATCH);
  728. }
  729. return "UNKNOWN";
  730. #undef TX_STATUS_FAIL
  731. #undef TX_STATUS_POSTPONE
  732. }
  733. #endif /* CONFIG_IWLWIFI_DEBUG */
  734. void iwl_mvm_hwrate_to_tx_rate(u32 rate_n_flags,
  735. enum ieee80211_band band,
  736. struct ieee80211_tx_rate *r)
  737. {
  738. if (rate_n_flags & RATE_HT_MCS_GF_MSK)
  739. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  740. switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
  741. case RATE_MCS_CHAN_WIDTH_20:
  742. break;
  743. case RATE_MCS_CHAN_WIDTH_40:
  744. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  745. break;
  746. case RATE_MCS_CHAN_WIDTH_80:
  747. r->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
  748. break;
  749. case RATE_MCS_CHAN_WIDTH_160:
  750. r->flags |= IEEE80211_TX_RC_160_MHZ_WIDTH;
  751. break;
  752. }
  753. if (rate_n_flags & RATE_MCS_SGI_MSK)
  754. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  755. if (rate_n_flags & RATE_MCS_HT_MSK) {
  756. r->flags |= IEEE80211_TX_RC_MCS;
  757. r->idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
  758. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  759. ieee80211_rate_set_vht(
  760. r, rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK,
  761. ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
  762. RATE_VHT_MCS_NSS_POS) + 1);
  763. r->flags |= IEEE80211_TX_RC_VHT_MCS;
  764. } else {
  765. r->idx = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
  766. band);
  767. }
  768. }
  769. /**
  770. * translate ucode response to mac80211 tx status control values
  771. */
  772. static void iwl_mvm_hwrate_to_tx_status(u32 rate_n_flags,
  773. struct ieee80211_tx_info *info)
  774. {
  775. struct ieee80211_tx_rate *r = &info->status.rates[0];
  776. info->status.antenna =
  777. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  778. iwl_mvm_hwrate_to_tx_rate(rate_n_flags, info->band, r);
  779. }
  780. static void iwl_mvm_tx_status_check_trigger(struct iwl_mvm *mvm,
  781. u32 status)
  782. {
  783. struct iwl_fw_dbg_trigger_tlv *trig;
  784. struct iwl_fw_dbg_trigger_tx_status *status_trig;
  785. int i;
  786. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TX_STATUS))
  787. return;
  788. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TX_STATUS);
  789. status_trig = (void *)trig->data;
  790. if (!iwl_fw_dbg_trigger_check_stop(mvm, NULL, trig))
  791. return;
  792. for (i = 0; i < ARRAY_SIZE(status_trig->statuses); i++) {
  793. /* don't collect on status 0 */
  794. if (!status_trig->statuses[i].status)
  795. break;
  796. if (status_trig->statuses[i].status != (status & TX_STATUS_MSK))
  797. continue;
  798. iwl_mvm_fw_dbg_collect_trig(mvm, trig,
  799. "Tx status %d was received",
  800. status & TX_STATUS_MSK);
  801. break;
  802. }
  803. }
  804. static void iwl_mvm_rx_tx_cmd_single(struct iwl_mvm *mvm,
  805. struct iwl_rx_packet *pkt)
  806. {
  807. struct ieee80211_sta *sta;
  808. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  809. int txq_id = SEQ_TO_QUEUE(sequence);
  810. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  811. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  812. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  813. u32 status = le16_to_cpu(tx_resp->status.status);
  814. u16 ssn = iwl_mvm_get_scd_ssn(tx_resp);
  815. struct iwl_mvm_sta *mvmsta;
  816. struct sk_buff_head skbs;
  817. u8 skb_freed = 0;
  818. u16 next_reclaimed, seq_ctl;
  819. bool is_ndp = false;
  820. __skb_queue_head_init(&skbs);
  821. seq_ctl = le16_to_cpu(tx_resp->seq_ctl);
  822. /* we can free until ssn % q.n_bd not inclusive */
  823. iwl_trans_reclaim(mvm->trans, txq_id, ssn, &skbs);
  824. while (!skb_queue_empty(&skbs)) {
  825. struct sk_buff *skb = __skb_dequeue(&skbs);
  826. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  827. skb_freed++;
  828. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  829. memset(&info->status, 0, sizeof(info->status));
  830. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  831. /* inform mac80211 about what happened with the frame */
  832. switch (status & TX_STATUS_MSK) {
  833. case TX_STATUS_SUCCESS:
  834. case TX_STATUS_DIRECT_DONE:
  835. info->flags |= IEEE80211_TX_STAT_ACK;
  836. break;
  837. case TX_STATUS_FAIL_DEST_PS:
  838. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  839. break;
  840. default:
  841. break;
  842. }
  843. iwl_mvm_tx_status_check_trigger(mvm, status);
  844. info->status.rates[0].count = tx_resp->failure_frame + 1;
  845. iwl_mvm_hwrate_to_tx_status(le32_to_cpu(tx_resp->initial_rate),
  846. info);
  847. info->status.status_driver_data[1] =
  848. (void *)(uintptr_t)le32_to_cpu(tx_resp->initial_rate);
  849. /* Single frame failure in an AMPDU queue => send BAR */
  850. if (txq_id >= mvm->first_agg_queue &&
  851. !(info->flags & IEEE80211_TX_STAT_ACK) &&
  852. !(info->flags & IEEE80211_TX_STAT_TX_FILTERED))
  853. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  854. /* W/A FW bug: seq_ctl is wrong when the status isn't success */
  855. if (status != TX_STATUS_SUCCESS) {
  856. struct ieee80211_hdr *hdr = (void *)skb->data;
  857. seq_ctl = le16_to_cpu(hdr->seq_ctrl);
  858. }
  859. if (unlikely(!seq_ctl)) {
  860. struct ieee80211_hdr *hdr = (void *)skb->data;
  861. /*
  862. * If it is an NDP, we can't update next_reclaim since
  863. * its sequence control is 0. Note that for that same
  864. * reason, NDPs are never sent to A-MPDU'able queues
  865. * so that we can never have more than one freed frame
  866. * for a single Tx resonse (see WARN_ON below).
  867. */
  868. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  869. is_ndp = true;
  870. }
  871. /*
  872. * TODO: this is not accurate if we are freeing more than one
  873. * packet.
  874. */
  875. info->status.tx_time =
  876. le16_to_cpu(tx_resp->wireless_media_time);
  877. BUILD_BUG_ON(ARRAY_SIZE(info->status.status_driver_data) < 1);
  878. info->status.status_driver_data[0] =
  879. (void *)(uintptr_t)tx_resp->reduced_tpc;
  880. ieee80211_tx_status(mvm->hw, skb);
  881. }
  882. if (txq_id >= mvm->first_agg_queue) {
  883. /* If this is an aggregation queue, we use the ssn since:
  884. * ssn = wifi seq_num % 256.
  885. * The seq_ctl is the sequence control of the packet to which
  886. * this Tx response relates. But if there is a hole in the
  887. * bitmap of the BA we received, this Tx response may allow to
  888. * reclaim the hole and all the subsequent packets that were
  889. * already acked. In that case, seq_ctl != ssn, and the next
  890. * packet to be reclaimed will be ssn and not seq_ctl. In that
  891. * case, several packets will be reclaimed even if
  892. * frame_count = 1.
  893. *
  894. * The ssn is the index (% 256) of the latest packet that has
  895. * treated (acked / dropped) + 1.
  896. */
  897. next_reclaimed = ssn;
  898. } else {
  899. /* The next packet to be reclaimed is the one after this one */
  900. next_reclaimed = IEEE80211_SEQ_TO_SN(seq_ctl + 0x10);
  901. }
  902. IWL_DEBUG_TX_REPLY(mvm,
  903. "TXQ %d status %s (0x%08x)\n",
  904. txq_id, iwl_mvm_get_tx_fail_reason(status), status);
  905. IWL_DEBUG_TX_REPLY(mvm,
  906. "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d next_reclaimed=0x%x seq_ctl=0x%x\n",
  907. le32_to_cpu(tx_resp->initial_rate),
  908. tx_resp->failure_frame, SEQ_TO_INDEX(sequence),
  909. ssn, next_reclaimed, seq_ctl);
  910. rcu_read_lock();
  911. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  912. /*
  913. * sta can't be NULL otherwise it'd mean that the sta has been freed in
  914. * the firmware while we still have packets for it in the Tx queues.
  915. */
  916. if (WARN_ON_ONCE(!sta))
  917. goto out;
  918. if (!IS_ERR(sta)) {
  919. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  920. if (tid != IWL_TID_NON_QOS) {
  921. struct iwl_mvm_tid_data *tid_data =
  922. &mvmsta->tid_data[tid];
  923. bool send_eosp_ndp = false;
  924. spin_lock_bh(&mvmsta->lock);
  925. if (!is_ndp) {
  926. tid_data->next_reclaimed = next_reclaimed;
  927. IWL_DEBUG_TX_REPLY(mvm,
  928. "Next reclaimed packet:%d\n",
  929. next_reclaimed);
  930. } else {
  931. IWL_DEBUG_TX_REPLY(mvm,
  932. "NDP - don't update next_reclaimed\n");
  933. }
  934. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  935. if (mvmsta->sleep_tx_count) {
  936. mvmsta->sleep_tx_count--;
  937. if (mvmsta->sleep_tx_count &&
  938. !iwl_mvm_tid_queued(tid_data)) {
  939. /*
  940. * The number of frames in the queue
  941. * dropped to 0 even if we sent less
  942. * frames than we thought we had on the
  943. * Tx queue.
  944. * This means we had holes in the BA
  945. * window that we just filled, ask
  946. * mac80211 to send EOSP since the
  947. * firmware won't know how to do that.
  948. * Send NDP and the firmware will send
  949. * EOSP notification that will trigger
  950. * a call to ieee80211_sta_eosp().
  951. */
  952. send_eosp_ndp = true;
  953. }
  954. }
  955. spin_unlock_bh(&mvmsta->lock);
  956. if (send_eosp_ndp) {
  957. iwl_mvm_sta_modify_sleep_tx_count(mvm, sta,
  958. IEEE80211_FRAME_RELEASE_UAPSD,
  959. 1, tid, false, false);
  960. mvmsta->sleep_tx_count = 0;
  961. ieee80211_send_eosp_nullfunc(sta, tid);
  962. }
  963. }
  964. if (mvmsta->next_status_eosp) {
  965. mvmsta->next_status_eosp = false;
  966. ieee80211_sta_eosp(sta);
  967. }
  968. } else {
  969. mvmsta = NULL;
  970. }
  971. /*
  972. * If the txq is not an AMPDU queue, there is no chance we freed
  973. * several skbs. Check that out...
  974. */
  975. if (txq_id >= mvm->first_agg_queue)
  976. goto out;
  977. /* We can't free more than one frame at once on a shared queue */
  978. WARN_ON(skb_freed > 1);
  979. /* If we have still frames for this STA nothing to do here */
  980. if (!atomic_sub_and_test(skb_freed, &mvm->pending_frames[sta_id]))
  981. goto out;
  982. if (mvmsta && mvmsta->vif->type == NL80211_IFTYPE_AP) {
  983. /*
  984. * If there are no pending frames for this STA and
  985. * the tx to this station is not disabled, notify
  986. * mac80211 that this station can now wake up in its
  987. * STA table.
  988. * If mvmsta is not NULL, sta is valid.
  989. */
  990. spin_lock_bh(&mvmsta->lock);
  991. if (!mvmsta->disable_tx)
  992. ieee80211_sta_block_awake(mvm->hw, sta, false);
  993. spin_unlock_bh(&mvmsta->lock);
  994. }
  995. if (PTR_ERR(sta) == -EBUSY || PTR_ERR(sta) == -ENOENT) {
  996. /*
  997. * We are draining and this was the last packet - pre_rcu_remove
  998. * has been called already. We might be after the
  999. * synchronize_net already.
  1000. * Don't rely on iwl_mvm_rm_sta to see the empty Tx queues.
  1001. */
  1002. set_bit(sta_id, mvm->sta_drained);
  1003. schedule_work(&mvm->sta_drained_wk);
  1004. }
  1005. out:
  1006. rcu_read_unlock();
  1007. }
  1008. #ifdef CONFIG_IWLWIFI_DEBUG
  1009. #define AGG_TX_STATE_(x) case AGG_TX_STATE_ ## x: return #x
  1010. static const char *iwl_get_agg_tx_status(u16 status)
  1011. {
  1012. switch (status & AGG_TX_STATE_STATUS_MSK) {
  1013. AGG_TX_STATE_(TRANSMITTED);
  1014. AGG_TX_STATE_(UNDERRUN);
  1015. AGG_TX_STATE_(BT_PRIO);
  1016. AGG_TX_STATE_(FEW_BYTES);
  1017. AGG_TX_STATE_(ABORT);
  1018. AGG_TX_STATE_(LAST_SENT_TTL);
  1019. AGG_TX_STATE_(LAST_SENT_TRY_CNT);
  1020. AGG_TX_STATE_(LAST_SENT_BT_KILL);
  1021. AGG_TX_STATE_(SCD_QUERY);
  1022. AGG_TX_STATE_(TEST_BAD_CRC32);
  1023. AGG_TX_STATE_(RESPONSE);
  1024. AGG_TX_STATE_(DUMP_TX);
  1025. AGG_TX_STATE_(DELAY_TX);
  1026. }
  1027. return "UNKNOWN";
  1028. }
  1029. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  1030. struct iwl_rx_packet *pkt)
  1031. {
  1032. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1033. struct agg_tx_status *frame_status = &tx_resp->status;
  1034. int i;
  1035. for (i = 0; i < tx_resp->frame_count; i++) {
  1036. u16 fstatus = le16_to_cpu(frame_status[i].status);
  1037. IWL_DEBUG_TX_REPLY(mvm,
  1038. "status %s (0x%04x), try-count (%d) seq (0x%x)\n",
  1039. iwl_get_agg_tx_status(fstatus),
  1040. fstatus & AGG_TX_STATE_STATUS_MSK,
  1041. (fstatus & AGG_TX_STATE_TRY_CNT_MSK) >>
  1042. AGG_TX_STATE_TRY_CNT_POS,
  1043. le16_to_cpu(frame_status[i].sequence));
  1044. }
  1045. }
  1046. #else
  1047. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  1048. struct iwl_rx_packet *pkt)
  1049. {}
  1050. #endif /* CONFIG_IWLWIFI_DEBUG */
  1051. static void iwl_mvm_rx_tx_cmd_agg(struct iwl_mvm *mvm,
  1052. struct iwl_rx_packet *pkt)
  1053. {
  1054. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1055. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  1056. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  1057. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1058. struct ieee80211_sta *sta;
  1059. if (WARN_ON_ONCE(SEQ_TO_QUEUE(sequence) < mvm->first_agg_queue))
  1060. return;
  1061. if (WARN_ON_ONCE(tid == IWL_TID_NON_QOS))
  1062. return;
  1063. iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);
  1064. rcu_read_lock();
  1065. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1066. if (!WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  1067. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1068. mvmsta->tid_data[tid].rate_n_flags =
  1069. le32_to_cpu(tx_resp->initial_rate);
  1070. mvmsta->tid_data[tid].tx_time =
  1071. le16_to_cpu(tx_resp->wireless_media_time);
  1072. }
  1073. rcu_read_unlock();
  1074. }
  1075. void iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  1076. {
  1077. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1078. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1079. if (tx_resp->frame_count == 1)
  1080. iwl_mvm_rx_tx_cmd_single(mvm, pkt);
  1081. else
  1082. iwl_mvm_rx_tx_cmd_agg(mvm, pkt);
  1083. }
  1084. static void iwl_mvm_tx_info_from_ba_notif(struct ieee80211_tx_info *info,
  1085. struct iwl_mvm_ba_notif *ba_notif,
  1086. struct iwl_mvm_tid_data *tid_data)
  1087. {
  1088. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1089. info->status.ampdu_ack_len = ba_notif->txed_2_done;
  1090. info->status.ampdu_len = ba_notif->txed;
  1091. iwl_mvm_hwrate_to_tx_status(tid_data->rate_n_flags,
  1092. info);
  1093. /* TODO: not accounted if the whole A-MPDU failed */
  1094. info->status.tx_time = tid_data->tx_time;
  1095. info->status.status_driver_data[0] =
  1096. (void *)(uintptr_t)ba_notif->reduced_txp;
  1097. info->status.status_driver_data[1] =
  1098. (void *)(uintptr_t)tid_data->rate_n_flags;
  1099. }
  1100. void iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  1101. {
  1102. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1103. struct iwl_mvm_ba_notif *ba_notif = (void *)pkt->data;
  1104. struct sk_buff_head reclaimed_skbs;
  1105. struct iwl_mvm_tid_data *tid_data;
  1106. struct ieee80211_sta *sta;
  1107. struct iwl_mvm_sta *mvmsta;
  1108. struct sk_buff *skb;
  1109. int sta_id, tid, freed;
  1110. /* "flow" corresponds to Tx queue */
  1111. u16 scd_flow = le16_to_cpu(ba_notif->scd_flow);
  1112. /* "ssn" is start of block-ack Tx window, corresponds to index
  1113. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1114. u16 ba_resp_scd_ssn = le16_to_cpu(ba_notif->scd_ssn);
  1115. sta_id = ba_notif->sta_id;
  1116. tid = ba_notif->tid;
  1117. if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT ||
  1118. tid >= IWL_MAX_TID_COUNT,
  1119. "sta_id %d tid %d", sta_id, tid))
  1120. return;
  1121. rcu_read_lock();
  1122. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1123. /* Reclaiming frames for a station that has been deleted ? */
  1124. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  1125. rcu_read_unlock();
  1126. return;
  1127. }
  1128. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1129. tid_data = &mvmsta->tid_data[tid];
  1130. if (tid_data->txq_id != scd_flow) {
  1131. IWL_ERR(mvm,
  1132. "invalid BA notification: Q %d, tid %d, flow %d\n",
  1133. tid_data->txq_id, tid, scd_flow);
  1134. rcu_read_unlock();
  1135. return;
  1136. }
  1137. spin_lock_bh(&mvmsta->lock);
  1138. __skb_queue_head_init(&reclaimed_skbs);
  1139. /*
  1140. * Release all TFDs before the SSN, i.e. all TFDs in front of
  1141. * block-ack window (we assume that they've been successfully
  1142. * transmitted ... if not, it's too late anyway).
  1143. */
  1144. iwl_trans_reclaim(mvm->trans, scd_flow, ba_resp_scd_ssn,
  1145. &reclaimed_skbs);
  1146. IWL_DEBUG_TX_REPLY(mvm,
  1147. "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
  1148. (u8 *)&ba_notif->sta_addr_lo32,
  1149. ba_notif->sta_id);
  1150. IWL_DEBUG_TX_REPLY(mvm,
  1151. "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
  1152. ba_notif->tid, le16_to_cpu(ba_notif->seq_ctl),
  1153. (unsigned long long)le64_to_cpu(ba_notif->bitmap),
  1154. scd_flow, ba_resp_scd_ssn, ba_notif->txed,
  1155. ba_notif->txed_2_done);
  1156. IWL_DEBUG_TX_REPLY(mvm, "reduced txp from ba notif %d\n",
  1157. ba_notif->reduced_txp);
  1158. tid_data->next_reclaimed = ba_resp_scd_ssn;
  1159. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  1160. freed = 0;
  1161. skb_queue_walk(&reclaimed_skbs, skb) {
  1162. struct ieee80211_hdr *hdr = (void *)skb->data;
  1163. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1164. if (ieee80211_is_data_qos(hdr->frame_control))
  1165. freed++;
  1166. else
  1167. WARN_ON_ONCE(1);
  1168. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  1169. memset(&info->status, 0, sizeof(info->status));
  1170. /* Packet was transmitted successfully, failures come as single
  1171. * frames because before failing a frame the firmware transmits
  1172. * it without aggregation at least once.
  1173. */
  1174. info->flags |= IEEE80211_TX_STAT_ACK;
  1175. /* this is the first skb we deliver in this batch */
  1176. /* put the rate scaling data there */
  1177. if (freed == 1)
  1178. iwl_mvm_tx_info_from_ba_notif(info, ba_notif, tid_data);
  1179. }
  1180. spin_unlock_bh(&mvmsta->lock);
  1181. /* We got a BA notif with 0 acked or scd_ssn didn't progress which is
  1182. * possible (i.e. first MPDU in the aggregation wasn't acked)
  1183. * Still it's important to update RS about sent vs. acked.
  1184. */
  1185. if (skb_queue_empty(&reclaimed_skbs)) {
  1186. struct ieee80211_tx_info ba_info = {};
  1187. struct ieee80211_chanctx_conf *chanctx_conf = NULL;
  1188. if (mvmsta->vif)
  1189. chanctx_conf =
  1190. rcu_dereference(mvmsta->vif->chanctx_conf);
  1191. if (WARN_ON_ONCE(!chanctx_conf))
  1192. goto out;
  1193. ba_info.band = chanctx_conf->def.chan->band;
  1194. iwl_mvm_tx_info_from_ba_notif(&ba_info, ba_notif, tid_data);
  1195. IWL_DEBUG_TX_REPLY(mvm, "No reclaim. Update rs directly\n");
  1196. iwl_mvm_rs_tx_status(mvm, sta, tid, &ba_info);
  1197. }
  1198. out:
  1199. rcu_read_unlock();
  1200. while (!skb_queue_empty(&reclaimed_skbs)) {
  1201. skb = __skb_dequeue(&reclaimed_skbs);
  1202. ieee80211_tx_status(mvm->hw, skb);
  1203. }
  1204. }
  1205. /*
  1206. * Note that there are transports that buffer frames before they reach
  1207. * the firmware. This means that after flush_tx_path is called, the
  1208. * queue might not be empty. The race-free way to handle this is to:
  1209. * 1) set the station as draining
  1210. * 2) flush the Tx path
  1211. * 3) wait for the transport queues to be empty
  1212. */
  1213. int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk, u32 flags)
  1214. {
  1215. int ret;
  1216. struct iwl_tx_path_flush_cmd flush_cmd = {
  1217. .queues_ctl = cpu_to_le32(tfd_msk),
  1218. .flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
  1219. };
  1220. ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
  1221. sizeof(flush_cmd), &flush_cmd);
  1222. if (ret)
  1223. IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
  1224. return ret;
  1225. }