tx.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  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 <net/ipv6.h>
  71. #include "iwl-trans.h"
  72. #include "iwl-eeprom-parse.h"
  73. #include "mvm.h"
  74. #include "sta.h"
  75. #include "fw-dbg.h"
  76. static void
  77. iwl_mvm_bar_check_trigger(struct iwl_mvm *mvm, const u8 *addr,
  78. u16 tid, u16 ssn)
  79. {
  80. struct iwl_fw_dbg_trigger_tlv *trig;
  81. struct iwl_fw_dbg_trigger_ba *ba_trig;
  82. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
  83. return;
  84. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
  85. ba_trig = (void *)trig->data;
  86. if (!iwl_fw_dbg_trigger_check_stop(mvm, NULL, trig))
  87. return;
  88. if (!(le16_to_cpu(ba_trig->tx_bar) & BIT(tid)))
  89. return;
  90. iwl_mvm_fw_dbg_collect_trig(mvm, trig,
  91. "BAR sent to %pM, tid %d, ssn %d",
  92. addr, tid, ssn);
  93. }
  94. #define OPT_HDR(type, skb, off) \
  95. (type *)(skb_network_header(skb) + (off))
  96. static void iwl_mvm_tx_csum(struct iwl_mvm *mvm, struct sk_buff *skb,
  97. struct ieee80211_hdr *hdr,
  98. struct ieee80211_tx_info *info,
  99. struct iwl_tx_cmd *tx_cmd)
  100. {
  101. #if IS_ENABLED(CONFIG_INET)
  102. u16 mh_len = ieee80211_hdrlen(hdr->frame_control);
  103. u16 offload_assist = le16_to_cpu(tx_cmd->offload_assist);
  104. u8 protocol = 0;
  105. /*
  106. * Do not compute checksum if already computed or if transport will
  107. * compute it
  108. */
  109. if (skb->ip_summed != CHECKSUM_PARTIAL || IWL_MVM_SW_TX_CSUM_OFFLOAD)
  110. return;
  111. /* We do not expect to be requested to csum stuff we do not support */
  112. if (WARN_ONCE(!(mvm->hw->netdev_features & IWL_TX_CSUM_NETIF_FLAGS) ||
  113. (skb->protocol != htons(ETH_P_IP) &&
  114. skb->protocol != htons(ETH_P_IPV6)),
  115. "No support for requested checksum\n")) {
  116. skb_checksum_help(skb);
  117. return;
  118. }
  119. if (skb->protocol == htons(ETH_P_IP)) {
  120. protocol = ip_hdr(skb)->protocol;
  121. } else {
  122. #if IS_ENABLED(CONFIG_IPV6)
  123. struct ipv6hdr *ipv6h =
  124. (struct ipv6hdr *)skb_network_header(skb);
  125. unsigned int off = sizeof(*ipv6h);
  126. protocol = ipv6h->nexthdr;
  127. while (protocol != NEXTHDR_NONE && ipv6_ext_hdr(protocol)) {
  128. struct ipv6_opt_hdr *hp;
  129. /* only supported extension headers */
  130. if (protocol != NEXTHDR_ROUTING &&
  131. protocol != NEXTHDR_HOP &&
  132. protocol != NEXTHDR_DEST) {
  133. skb_checksum_help(skb);
  134. return;
  135. }
  136. hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
  137. protocol = hp->nexthdr;
  138. off += ipv6_optlen(hp);
  139. }
  140. /* if we get here - protocol now should be TCP/UDP */
  141. #endif
  142. }
  143. if (protocol != IPPROTO_TCP && protocol != IPPROTO_UDP) {
  144. WARN_ON_ONCE(1);
  145. skb_checksum_help(skb);
  146. return;
  147. }
  148. /* enable L4 csum */
  149. offload_assist |= BIT(TX_CMD_OFFLD_L4_EN);
  150. /*
  151. * Set offset to IP header (snap).
  152. * We don't support tunneling so no need to take care of inner header.
  153. * Size is in words.
  154. */
  155. offload_assist |= (4 << TX_CMD_OFFLD_IP_HDR);
  156. /* Do IPv4 csum for AMSDU only (no IP csum for Ipv6) */
  157. if (skb->protocol == htons(ETH_P_IP) &&
  158. (offload_assist & BIT(TX_CMD_OFFLD_AMSDU))) {
  159. ip_hdr(skb)->check = 0;
  160. offload_assist |= BIT(TX_CMD_OFFLD_L3_EN);
  161. }
  162. /* reset UDP/TCP header csum */
  163. if (protocol == IPPROTO_TCP)
  164. tcp_hdr(skb)->check = 0;
  165. else
  166. udp_hdr(skb)->check = 0;
  167. /* mac header len should include IV, size is in words */
  168. if (info->control.hw_key)
  169. mh_len += info->control.hw_key->iv_len;
  170. mh_len /= 2;
  171. offload_assist |= mh_len << TX_CMD_OFFLD_MH_SIZE;
  172. tx_cmd->offload_assist = cpu_to_le16(offload_assist);
  173. #endif
  174. }
  175. /*
  176. * Sets most of the Tx cmd's fields
  177. */
  178. void iwl_mvm_set_tx_cmd(struct iwl_mvm *mvm, struct sk_buff *skb,
  179. struct iwl_tx_cmd *tx_cmd,
  180. struct ieee80211_tx_info *info, u8 sta_id)
  181. {
  182. struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
  183. struct ieee80211_hdr *hdr = (void *)skb->data;
  184. __le16 fc = hdr->frame_control;
  185. u32 tx_flags = le32_to_cpu(tx_cmd->tx_flags);
  186. u32 len = skb->len + FCS_LEN;
  187. u8 ac;
  188. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  189. tx_flags |= TX_CMD_FLG_ACK;
  190. else
  191. tx_flags &= ~TX_CMD_FLG_ACK;
  192. if (ieee80211_is_probe_resp(fc))
  193. tx_flags |= TX_CMD_FLG_TSF;
  194. if (ieee80211_has_morefrags(fc))
  195. tx_flags |= TX_CMD_FLG_MORE_FRAG;
  196. if (ieee80211_is_data_qos(fc)) {
  197. u8 *qc = ieee80211_get_qos_ctl(hdr);
  198. tx_cmd->tid_tspec = qc[0] & 0xf;
  199. tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
  200. if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
  201. tx_cmd->offload_assist |=
  202. cpu_to_le16(BIT(TX_CMD_OFFLD_AMSDU));
  203. } else if (ieee80211_is_back_req(fc)) {
  204. struct ieee80211_bar *bar = (void *)skb->data;
  205. u16 control = le16_to_cpu(bar->control);
  206. u16 ssn = le16_to_cpu(bar->start_seq_num);
  207. tx_flags |= TX_CMD_FLG_ACK | TX_CMD_FLG_BAR;
  208. tx_cmd->tid_tspec = (control &
  209. IEEE80211_BAR_CTRL_TID_INFO_MASK) >>
  210. IEEE80211_BAR_CTRL_TID_INFO_SHIFT;
  211. WARN_ON_ONCE(tx_cmd->tid_tspec >= IWL_MAX_TID_COUNT);
  212. iwl_mvm_bar_check_trigger(mvm, bar->ra, tx_cmd->tid_tspec,
  213. ssn);
  214. } else {
  215. tx_cmd->tid_tspec = IWL_TID_NON_QOS;
  216. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
  217. tx_flags |= TX_CMD_FLG_SEQ_CTL;
  218. else
  219. tx_flags &= ~TX_CMD_FLG_SEQ_CTL;
  220. }
  221. /* Default to 0 (BE) when tid_spec is set to IWL_TID_NON_QOS */
  222. if (tx_cmd->tid_tspec < IWL_MAX_TID_COUNT)
  223. ac = tid_to_mac80211_ac[tx_cmd->tid_tspec];
  224. else
  225. ac = tid_to_mac80211_ac[0];
  226. tx_flags |= iwl_mvm_bt_coex_tx_prio(mvm, hdr, info, ac) <<
  227. TX_CMD_FLG_BT_PRIO_POS;
  228. if (ieee80211_is_mgmt(fc)) {
  229. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  230. tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_ASSOC);
  231. else if (ieee80211_is_action(fc))
  232. tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
  233. else
  234. tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
  235. /* The spec allows Action frames in A-MPDU, we don't support
  236. * it
  237. */
  238. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_AMPDU);
  239. } else if (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO) {
  240. tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_MGMT);
  241. } else {
  242. tx_cmd->pm_frame_timeout = cpu_to_le16(PM_FRAME_NONE);
  243. }
  244. if (ieee80211_is_data(fc) && len > mvm->rts_threshold &&
  245. !is_multicast_ether_addr(ieee80211_get_DA(hdr)))
  246. tx_flags |= TX_CMD_FLG_PROT_REQUIRE;
  247. if (fw_has_capa(&mvm->fw->ucode_capa,
  248. IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT) &&
  249. ieee80211_action_contains_tpc(skb))
  250. tx_flags |= TX_CMD_FLG_WRITE_TX_POWER;
  251. tx_cmd->tx_flags = cpu_to_le32(tx_flags);
  252. /* Total # bytes to be transmitted */
  253. tx_cmd->len = cpu_to_le16((u16)skb->len +
  254. (uintptr_t)skb_info->driver_data[0]);
  255. tx_cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  256. tx_cmd->sta_id = sta_id;
  257. /* padding is inserted later in transport */
  258. if (ieee80211_hdrlen(fc) % 4 &&
  259. !(tx_cmd->offload_assist & cpu_to_le16(BIT(TX_CMD_OFFLD_AMSDU))))
  260. tx_cmd->offload_assist |= cpu_to_le16(BIT(TX_CMD_OFFLD_PAD));
  261. iwl_mvm_tx_csum(mvm, skb, hdr, info, tx_cmd);
  262. }
  263. /*
  264. * Sets the fields in the Tx cmd that are rate related
  265. */
  266. void iwl_mvm_set_tx_cmd_rate(struct iwl_mvm *mvm, struct iwl_tx_cmd *tx_cmd,
  267. struct ieee80211_tx_info *info,
  268. struct ieee80211_sta *sta, __le16 fc)
  269. {
  270. u32 rate_flags;
  271. int rate_idx;
  272. u8 rate_plcp;
  273. /* Set retry limit on RTS packets */
  274. tx_cmd->rts_retry_limit = IWL_RTS_DFAULT_RETRY_LIMIT;
  275. /* Set retry limit on DATA packets and Probe Responses*/
  276. if (ieee80211_is_probe_resp(fc)) {
  277. tx_cmd->data_retry_limit = IWL_MGMT_DFAULT_RETRY_LIMIT;
  278. tx_cmd->rts_retry_limit =
  279. min(tx_cmd->data_retry_limit, tx_cmd->rts_retry_limit);
  280. } else if (ieee80211_is_back_req(fc)) {
  281. tx_cmd->data_retry_limit = IWL_BAR_DFAULT_RETRY_LIMIT;
  282. } else {
  283. tx_cmd->data_retry_limit = IWL_DEFAULT_TX_RETRY;
  284. }
  285. /*
  286. * for data packets, rate info comes from the table inside the fw. This
  287. * table is controlled by LINK_QUALITY commands
  288. */
  289. if (ieee80211_is_data(fc) && sta) {
  290. tx_cmd->initial_rate_index = 0;
  291. tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_STA_RATE);
  292. return;
  293. } else if (ieee80211_is_back_req(fc)) {
  294. tx_cmd->tx_flags |=
  295. cpu_to_le32(TX_CMD_FLG_ACK | TX_CMD_FLG_BAR);
  296. }
  297. /* HT rate doesn't make sense for a non data frame */
  298. WARN_ONCE(info->control.rates[0].flags & IEEE80211_TX_RC_MCS,
  299. "Got an HT rate (flags:0x%x/mcs:%d) for a non data frame (fc:0x%x)\n",
  300. info->control.rates[0].flags,
  301. info->control.rates[0].idx,
  302. le16_to_cpu(fc));
  303. rate_idx = info->control.rates[0].idx;
  304. /* if the rate isn't a well known legacy rate, take the lowest one */
  305. if (rate_idx < 0 || rate_idx >= IWL_RATE_COUNT_LEGACY)
  306. rate_idx = rate_lowest_index(
  307. &mvm->nvm_data->bands[info->band], sta);
  308. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  309. if (info->band == NL80211_BAND_5GHZ)
  310. rate_idx += IWL_FIRST_OFDM_RATE;
  311. /* For 2.4 GHZ band, check that there is no need to remap */
  312. BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
  313. /* Get PLCP rate for tx_cmd->rate_n_flags */
  314. rate_plcp = iwl_mvm_mac80211_idx_to_hwrate(rate_idx);
  315. mvm->mgmt_last_antenna_idx =
  316. iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
  317. mvm->mgmt_last_antenna_idx);
  318. if (info->band == NL80211_BAND_2GHZ &&
  319. !iwl_mvm_bt_coex_is_shared_ant_avail(mvm))
  320. rate_flags = mvm->cfg->non_shared_ant << RATE_MCS_ANT_POS;
  321. else
  322. rate_flags =
  323. BIT(mvm->mgmt_last_antenna_idx) << RATE_MCS_ANT_POS;
  324. /* Set CCK flag as needed */
  325. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  326. rate_flags |= RATE_MCS_CCK_MSK;
  327. /* Set the rate in the TX cmd */
  328. tx_cmd->rate_n_flags = cpu_to_le32((u32)rate_plcp | rate_flags);
  329. }
  330. static inline void iwl_mvm_set_tx_cmd_pn(struct ieee80211_tx_info *info,
  331. u8 *crypto_hdr)
  332. {
  333. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  334. u64 pn;
  335. pn = atomic64_inc_return(&keyconf->tx_pn);
  336. crypto_hdr[0] = pn;
  337. crypto_hdr[2] = 0;
  338. crypto_hdr[3] = 0x20 | (keyconf->keyidx << 6);
  339. crypto_hdr[1] = pn >> 8;
  340. crypto_hdr[4] = pn >> 16;
  341. crypto_hdr[5] = pn >> 24;
  342. crypto_hdr[6] = pn >> 32;
  343. crypto_hdr[7] = pn >> 40;
  344. }
  345. /*
  346. * Sets the fields in the Tx cmd that are crypto related
  347. */
  348. static void iwl_mvm_set_tx_cmd_crypto(struct iwl_mvm *mvm,
  349. struct ieee80211_tx_info *info,
  350. struct iwl_tx_cmd *tx_cmd,
  351. struct sk_buff *skb_frag,
  352. int hdrlen)
  353. {
  354. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  355. u8 *crypto_hdr = skb_frag->data + hdrlen;
  356. u64 pn;
  357. switch (keyconf->cipher) {
  358. case WLAN_CIPHER_SUITE_CCMP:
  359. case WLAN_CIPHER_SUITE_CCMP_256:
  360. iwl_mvm_set_tx_cmd_ccmp(info, tx_cmd);
  361. iwl_mvm_set_tx_cmd_pn(info, crypto_hdr);
  362. break;
  363. case WLAN_CIPHER_SUITE_TKIP:
  364. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  365. pn = atomic64_inc_return(&keyconf->tx_pn);
  366. ieee80211_tkip_add_iv(crypto_hdr, keyconf, pn);
  367. ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
  368. break;
  369. case WLAN_CIPHER_SUITE_WEP104:
  370. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  371. /* fall through */
  372. case WLAN_CIPHER_SUITE_WEP40:
  373. tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
  374. ((keyconf->keyidx << TX_CMD_SEC_WEP_KEY_IDX_POS) &
  375. TX_CMD_SEC_WEP_KEY_IDX_MSK);
  376. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  377. break;
  378. case WLAN_CIPHER_SUITE_GCMP:
  379. case WLAN_CIPHER_SUITE_GCMP_256:
  380. /* TODO: Taking the key from the table might introduce a race
  381. * when PTK rekeying is done, having an old packets with a PN
  382. * based on the old key but the message encrypted with a new
  383. * one.
  384. * Need to handle this.
  385. */
  386. tx_cmd->sec_ctl |= TX_CMD_SEC_GCMP | TX_CMD_SEC_KEY_FROM_TABLE;
  387. tx_cmd->key[0] = keyconf->hw_key_idx;
  388. iwl_mvm_set_tx_cmd_pn(info, crypto_hdr);
  389. break;
  390. default:
  391. tx_cmd->sec_ctl |= TX_CMD_SEC_EXT;
  392. }
  393. }
  394. /*
  395. * Allocates and sets the Tx cmd the driver data pointers in the skb
  396. */
  397. static struct iwl_device_cmd *
  398. iwl_mvm_set_tx_params(struct iwl_mvm *mvm, struct sk_buff *skb,
  399. struct ieee80211_tx_info *info, int hdrlen,
  400. struct ieee80211_sta *sta, u8 sta_id)
  401. {
  402. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  403. struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
  404. struct iwl_device_cmd *dev_cmd;
  405. struct iwl_tx_cmd *tx_cmd;
  406. dev_cmd = iwl_trans_alloc_tx_cmd(mvm->trans);
  407. if (unlikely(!dev_cmd))
  408. return NULL;
  409. memset(dev_cmd, 0, sizeof(*dev_cmd));
  410. dev_cmd->hdr.cmd = TX_CMD;
  411. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  412. if (info->control.hw_key)
  413. iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb, hdrlen);
  414. iwl_mvm_set_tx_cmd(mvm, skb, tx_cmd, info, sta_id);
  415. iwl_mvm_set_tx_cmd_rate(mvm, tx_cmd, info, sta, hdr->frame_control);
  416. memset(&skb_info->status, 0, sizeof(skb_info->status));
  417. memset(skb_info->driver_data, 0, sizeof(skb_info->driver_data));
  418. skb_info->driver_data[1] = dev_cmd;
  419. return dev_cmd;
  420. }
  421. static int iwl_mvm_get_ctrl_vif_queue(struct iwl_mvm *mvm,
  422. struct ieee80211_tx_info *info, __le16 fc)
  423. {
  424. if (!iwl_mvm_is_dqa_supported(mvm))
  425. return info->hw_queue;
  426. switch (info->control.vif->type) {
  427. case NL80211_IFTYPE_AP:
  428. /*
  429. * handle legacy hostapd as well, where station may be added
  430. * only after assoc.
  431. */
  432. if (ieee80211_is_probe_resp(fc) || ieee80211_is_auth(fc))
  433. return IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
  434. if (info->hw_queue == info->control.vif->cab_queue)
  435. return info->hw_queue;
  436. WARN_ON_ONCE(1);
  437. return IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
  438. case NL80211_IFTYPE_P2P_DEVICE:
  439. if (ieee80211_is_mgmt(fc))
  440. return IWL_MVM_DQA_P2P_DEVICE_QUEUE;
  441. if (info->hw_queue == info->control.vif->cab_queue)
  442. return info->hw_queue;
  443. WARN_ON_ONCE(1);
  444. return IWL_MVM_DQA_P2P_DEVICE_QUEUE;
  445. default:
  446. WARN_ONCE(1, "Not a ctrl vif, no available queue\n");
  447. return -1;
  448. }
  449. }
  450. int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
  451. {
  452. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  453. struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
  454. struct ieee80211_tx_info info;
  455. struct iwl_device_cmd *dev_cmd;
  456. struct iwl_tx_cmd *tx_cmd;
  457. u8 sta_id;
  458. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  459. int queue;
  460. /* IWL_MVM_OFFCHANNEL_QUEUE is used for ROC packets that can be used
  461. * in 2 different types of vifs, P2P & STATION. P2P uses the offchannel
  462. * queue. STATION (HS2.0) uses the auxiliary context of the FW,
  463. * and hence needs to be sent on the aux queue
  464. */
  465. if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
  466. skb_info->control.vif->type == NL80211_IFTYPE_STATION)
  467. IEEE80211_SKB_CB(skb)->hw_queue = mvm->aux_queue;
  468. memcpy(&info, skb->cb, sizeof(info));
  469. if (WARN_ON_ONCE(info.flags & IEEE80211_TX_CTL_AMPDU))
  470. return -1;
  471. if (WARN_ON_ONCE(info.flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
  472. (!info.control.vif ||
  473. info.hw_queue != info.control.vif->cab_queue)))
  474. return -1;
  475. /* This holds the amsdu headers length */
  476. skb_info->driver_data[0] = (void *)(uintptr_t)0;
  477. queue = info.hw_queue;
  478. /*
  479. * If the interface on which the frame is sent is the P2P_DEVICE
  480. * or an AP/GO interface use the broadcast station associated
  481. * with it; otherwise if the interface is a managed interface
  482. * use the AP station associated with it for multicast traffic
  483. * (this is not possible for unicast packets as a TLDS discovery
  484. * response are sent without a station entry); otherwise use the
  485. * AUX station.
  486. * In DQA mode, if vif is of type STATION and frames are not multicast,
  487. * they should be sent from the BSS queue. For example, TDLS setup
  488. * frames should be sent on this queue, as they go through the AP.
  489. */
  490. sta_id = mvm->aux_sta.sta_id;
  491. if (info.control.vif) {
  492. struct iwl_mvm_vif *mvmvif =
  493. iwl_mvm_vif_from_mac80211(info.control.vif);
  494. if (info.control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
  495. info.control.vif->type == NL80211_IFTYPE_AP) {
  496. sta_id = mvmvif->bcast_sta.sta_id;
  497. queue = iwl_mvm_get_ctrl_vif_queue(mvm, &info,
  498. hdr->frame_control);
  499. if (queue < 0)
  500. return -1;
  501. } else if (info.control.vif->type == NL80211_IFTYPE_STATION &&
  502. is_multicast_ether_addr(hdr->addr1)) {
  503. u8 ap_sta_id = ACCESS_ONCE(mvmvif->ap_sta_id);
  504. if (ap_sta_id != IWL_MVM_STATION_COUNT)
  505. sta_id = ap_sta_id;
  506. } else if (iwl_mvm_is_dqa_supported(mvm) &&
  507. info.control.vif->type == NL80211_IFTYPE_STATION) {
  508. queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
  509. }
  510. }
  511. IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, queue);
  512. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, &info, hdrlen, NULL, sta_id);
  513. if (!dev_cmd)
  514. return -1;
  515. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  516. /* Copy MAC header from skb into command buffer */
  517. memcpy(tx_cmd->hdr, hdr, hdrlen);
  518. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, queue)) {
  519. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  520. return -1;
  521. }
  522. /*
  523. * Increase the pending frames counter, so that later when a reply comes
  524. * in and the counter is decreased - we don't start getting negative
  525. * values.
  526. * Note that we don't need to make sure it isn't agg'd, since we're
  527. * TXing non-sta
  528. */
  529. atomic_inc(&mvm->pending_frames[sta_id]);
  530. return 0;
  531. }
  532. #ifdef CONFIG_INET
  533. static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
  534. struct ieee80211_tx_info *info,
  535. struct ieee80211_sta *sta,
  536. struct sk_buff_head *mpdus_skb)
  537. {
  538. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  539. struct ieee80211_hdr *hdr = (void *)skb->data;
  540. unsigned int mss = skb_shinfo(skb)->gso_size;
  541. struct sk_buff *tmp, *next;
  542. char cb[sizeof(skb->cb)];
  543. unsigned int num_subframes, tcp_payload_len, subf_len, max_amsdu_len;
  544. bool ipv4 = (skb->protocol == htons(ETH_P_IP));
  545. u16 ip_base_id = ipv4 ? ntohs(ip_hdr(skb)->id) : 0;
  546. u16 amsdu_add, snap_ip_tcp, pad, i = 0;
  547. unsigned int dbg_max_amsdu_len;
  548. netdev_features_t netdev_features = NETIF_F_CSUM_MASK | NETIF_F_SG;
  549. u8 *qc, tid, txf;
  550. snap_ip_tcp = 8 + skb_transport_header(skb) - skb_network_header(skb) +
  551. tcp_hdrlen(skb);
  552. qc = ieee80211_get_qos_ctl(hdr);
  553. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  554. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  555. return -EINVAL;
  556. dbg_max_amsdu_len = ACCESS_ONCE(mvm->max_amsdu_len);
  557. if (!sta->max_amsdu_len ||
  558. !ieee80211_is_data_qos(hdr->frame_control) ||
  559. (!mvmsta->tlc_amsdu && !dbg_max_amsdu_len)) {
  560. num_subframes = 1;
  561. pad = 0;
  562. goto segment;
  563. }
  564. /*
  565. * Do not build AMSDU for IPv6 with extension headers.
  566. * ask stack to segment and checkum the generated MPDUs for us.
  567. */
  568. if (skb->protocol == htons(ETH_P_IPV6) &&
  569. ((struct ipv6hdr *)skb_network_header(skb))->nexthdr !=
  570. IPPROTO_TCP) {
  571. num_subframes = 1;
  572. pad = 0;
  573. netdev_features &= ~NETIF_F_CSUM_MASK;
  574. goto segment;
  575. }
  576. /*
  577. * No need to lock amsdu_in_ampdu_allowed since it can't be modified
  578. * during an BA session.
  579. */
  580. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  581. !mvmsta->tid_data[tid].amsdu_in_ampdu_allowed) {
  582. num_subframes = 1;
  583. pad = 0;
  584. goto segment;
  585. }
  586. max_amsdu_len = sta->max_amsdu_len;
  587. /* the Tx FIFO to which this A-MSDU will be routed */
  588. txf = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
  589. /*
  590. * Don't send an AMSDU that will be longer than the TXF.
  591. * Add a security margin of 256 for the TX command + headers.
  592. * We also want to have the start of the next packet inside the
  593. * fifo to be able to send bursts.
  594. */
  595. max_amsdu_len = min_t(unsigned int, max_amsdu_len,
  596. mvm->shared_mem_cfg.txfifo_size[txf] - 256);
  597. if (unlikely(dbg_max_amsdu_len))
  598. max_amsdu_len = min_t(unsigned int, max_amsdu_len,
  599. dbg_max_amsdu_len);
  600. /*
  601. * Limit A-MSDU in A-MPDU to 4095 bytes when VHT is not
  602. * supported. This is a spec requirement (IEEE 802.11-2015
  603. * section 8.7.3 NOTE 3).
  604. */
  605. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  606. !sta->vht_cap.vht_supported)
  607. max_amsdu_len = min_t(unsigned int, max_amsdu_len, 4095);
  608. /* Sub frame header + SNAP + IP header + TCP header + MSS */
  609. subf_len = sizeof(struct ethhdr) + snap_ip_tcp + mss;
  610. pad = (4 - subf_len) & 0x3;
  611. /*
  612. * If we have N subframes in the A-MSDU, then the A-MSDU's size is
  613. * N * subf_len + (N - 1) * pad.
  614. */
  615. num_subframes = (max_amsdu_len + pad) / (subf_len + pad);
  616. if (num_subframes > 1)
  617. *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  618. tcp_payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
  619. tcp_hdrlen(skb) + skb->data_len;
  620. /*
  621. * Make sure we have enough TBs for the A-MSDU:
  622. * 2 for each subframe
  623. * 1 more for each fragment
  624. * 1 more for the potential data in the header
  625. */
  626. num_subframes =
  627. min_t(unsigned int, num_subframes,
  628. (mvm->trans->max_skb_frags - 1 -
  629. skb_shinfo(skb)->nr_frags) / 2);
  630. /* This skb fits in one single A-MSDU */
  631. if (num_subframes * mss >= tcp_payload_len) {
  632. struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
  633. /*
  634. * Compute the length of all the data added for the A-MSDU.
  635. * This will be used to compute the length to write in the TX
  636. * command. We have: SNAP + IP + TCP for n -1 subframes and
  637. * ETH header for n subframes. Note that the original skb
  638. * already had one set of SNAP / IP / TCP headers.
  639. */
  640. num_subframes = DIV_ROUND_UP(tcp_payload_len, mss);
  641. amsdu_add = num_subframes * sizeof(struct ethhdr) +
  642. (num_subframes - 1) * (snap_ip_tcp + pad);
  643. /* This holds the amsdu headers length */
  644. skb_info->driver_data[0] = (void *)(uintptr_t)amsdu_add;
  645. __skb_queue_tail(mpdus_skb, skb);
  646. return 0;
  647. }
  648. /*
  649. * Trick the segmentation function to make it
  650. * create SKBs that can fit into one A-MSDU.
  651. */
  652. segment:
  653. skb_shinfo(skb)->gso_size = num_subframes * mss;
  654. memcpy(cb, skb->cb, sizeof(cb));
  655. next = skb_gso_segment(skb, netdev_features);
  656. skb_shinfo(skb)->gso_size = mss;
  657. if (WARN_ON_ONCE(IS_ERR(next)))
  658. return -EINVAL;
  659. else if (next)
  660. consume_skb(skb);
  661. while (next) {
  662. tmp = next;
  663. next = tmp->next;
  664. memcpy(tmp->cb, cb, sizeof(tmp->cb));
  665. /*
  666. * Compute the length of all the data added for the A-MSDU.
  667. * This will be used to compute the length to write in the TX
  668. * command. We have: SNAP + IP + TCP for n -1 subframes and
  669. * ETH header for n subframes.
  670. */
  671. tcp_payload_len = skb_tail_pointer(tmp) -
  672. skb_transport_header(tmp) -
  673. tcp_hdrlen(tmp) + tmp->data_len;
  674. if (ipv4)
  675. ip_hdr(tmp)->id = htons(ip_base_id + i * num_subframes);
  676. if (tcp_payload_len > mss) {
  677. struct ieee80211_tx_info *skb_info =
  678. IEEE80211_SKB_CB(tmp);
  679. num_subframes = DIV_ROUND_UP(tcp_payload_len, mss);
  680. amsdu_add = num_subframes * sizeof(struct ethhdr) +
  681. (num_subframes - 1) * (snap_ip_tcp + pad);
  682. skb_info->driver_data[0] =
  683. (void *)(uintptr_t)amsdu_add;
  684. skb_shinfo(tmp)->gso_size = mss;
  685. } else {
  686. qc = ieee80211_get_qos_ctl((void *)tmp->data);
  687. if (ipv4)
  688. ip_send_check(ip_hdr(tmp));
  689. *qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  690. skb_shinfo(tmp)->gso_size = 0;
  691. }
  692. tmp->prev = NULL;
  693. tmp->next = NULL;
  694. __skb_queue_tail(mpdus_skb, tmp);
  695. i++;
  696. }
  697. return 0;
  698. }
  699. #else /* CONFIG_INET */
  700. static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
  701. struct ieee80211_tx_info *info,
  702. struct ieee80211_sta *sta,
  703. struct sk_buff_head *mpdus_skb)
  704. {
  705. /* Impossible to get TSO with CONFIG_INET */
  706. WARN_ON(1);
  707. return -1;
  708. }
  709. #endif
  710. static void iwl_mvm_tx_add_stream(struct iwl_mvm *mvm,
  711. struct iwl_mvm_sta *mvm_sta, u8 tid,
  712. struct sk_buff *skb)
  713. {
  714. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  715. u8 mac_queue = info->hw_queue;
  716. struct sk_buff_head *deferred_tx_frames;
  717. lockdep_assert_held(&mvm_sta->lock);
  718. mvm_sta->deferred_traffic_tid_map |= BIT(tid);
  719. set_bit(mvm_sta->sta_id, mvm->sta_deferred_frames);
  720. deferred_tx_frames = &mvm_sta->tid_data[tid].deferred_tx_frames;
  721. skb_queue_tail(deferred_tx_frames, skb);
  722. /*
  723. * The first deferred frame should've stopped the MAC queues, so we
  724. * should never get a second deferred frame for the RA/TID.
  725. */
  726. if (!WARN(skb_queue_len(deferred_tx_frames) != 1,
  727. "RATID %d/%d has %d deferred frames\n", mvm_sta->sta_id, tid,
  728. skb_queue_len(deferred_tx_frames))) {
  729. iwl_mvm_stop_mac_queues(mvm, BIT(mac_queue));
  730. schedule_work(&mvm->add_stream_wk);
  731. }
  732. }
  733. /* Check if there are any timed-out TIDs on a given shared TXQ */
  734. static bool iwl_mvm_txq_should_update(struct iwl_mvm *mvm, int txq_id)
  735. {
  736. unsigned long queue_tid_bitmap = mvm->queue_info[txq_id].tid_bitmap;
  737. unsigned long now = jiffies;
  738. int tid;
  739. for_each_set_bit(tid, &queue_tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  740. if (time_before(mvm->queue_info[txq_id].last_frame_time[tid] +
  741. IWL_MVM_DQA_QUEUE_TIMEOUT, now))
  742. return true;
  743. }
  744. return false;
  745. }
  746. /*
  747. * Sets the fields in the Tx cmd that are crypto related
  748. */
  749. static int iwl_mvm_tx_mpdu(struct iwl_mvm *mvm, struct sk_buff *skb,
  750. struct ieee80211_tx_info *info,
  751. struct ieee80211_sta *sta)
  752. {
  753. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  754. struct iwl_mvm_sta *mvmsta;
  755. struct iwl_device_cmd *dev_cmd;
  756. struct iwl_tx_cmd *tx_cmd;
  757. __le16 fc;
  758. u16 seq_number = 0;
  759. u8 tid = IWL_MAX_TID_COUNT;
  760. u8 txq_id = info->hw_queue;
  761. bool is_ampdu = false;
  762. int hdrlen;
  763. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  764. fc = hdr->frame_control;
  765. hdrlen = ieee80211_hdrlen(fc);
  766. if (WARN_ON_ONCE(!mvmsta))
  767. return -1;
  768. if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
  769. return -1;
  770. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, info, hdrlen,
  771. sta, mvmsta->sta_id);
  772. if (!dev_cmd)
  773. goto drop;
  774. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  775. /* From now on, we cannot access info->control */
  776. /*
  777. * we handle that entirely ourselves -- for uAPSD the firmware
  778. * will always send a notification, and for PS-Poll responses
  779. * we'll notify mac80211 when getting frame status
  780. */
  781. info->flags &= ~IEEE80211_TX_STATUS_EOSP;
  782. spin_lock(&mvmsta->lock);
  783. if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
  784. u8 *qc = NULL;
  785. qc = ieee80211_get_qos_ctl(hdr);
  786. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  787. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  788. goto drop_unlock_sta;
  789. seq_number = mvmsta->tid_data[tid].seq_number;
  790. seq_number &= IEEE80211_SCTL_SEQ;
  791. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  792. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  793. is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
  794. } else if (iwl_mvm_is_dqa_supported(mvm) &&
  795. (ieee80211_is_qos_nullfunc(fc) ||
  796. ieee80211_is_nullfunc(fc))) {
  797. /*
  798. * nullfunc frames should go to the MGMT queue regardless of QOS
  799. */
  800. tid = IWL_MAX_TID_COUNT;
  801. }
  802. if (iwl_mvm_is_dqa_supported(mvm)) {
  803. txq_id = mvmsta->tid_data[tid].txq_id;
  804. if (ieee80211_is_mgmt(fc))
  805. tx_cmd->tid_tspec = IWL_TID_NON_QOS;
  806. }
  807. /* Copy MAC header from skb into command buffer */
  808. memcpy(tx_cmd->hdr, hdr, hdrlen);
  809. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
  810. if (sta->tdls && !iwl_mvm_is_dqa_supported(mvm)) {
  811. /* default to TID 0 for non-QoS packets */
  812. u8 tdls_tid = tid == IWL_MAX_TID_COUNT ? 0 : tid;
  813. txq_id = mvmsta->hw_queue[tid_to_mac80211_ac[tdls_tid]];
  814. }
  815. if (is_ampdu) {
  816. if (WARN_ON_ONCE(mvmsta->tid_data[tid].state != IWL_AGG_ON))
  817. goto drop_unlock_sta;
  818. txq_id = mvmsta->tid_data[tid].txq_id;
  819. }
  820. /* Check if TXQ needs to be allocated or re-activated */
  821. if (unlikely(txq_id == IEEE80211_INVAL_HW_QUEUE ||
  822. !mvmsta->tid_data[tid].is_tid_active) &&
  823. iwl_mvm_is_dqa_supported(mvm)) {
  824. /* If TXQ needs to be allocated... */
  825. if (txq_id == IEEE80211_INVAL_HW_QUEUE) {
  826. iwl_mvm_tx_add_stream(mvm, mvmsta, tid, skb);
  827. /*
  828. * The frame is now deferred, and the worker scheduled
  829. * will re-allocate it, so we can free it for now.
  830. */
  831. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  832. spin_unlock(&mvmsta->lock);
  833. return 0;
  834. }
  835. /* If we are here - TXQ exists and needs to be re-activated */
  836. spin_lock(&mvm->queue_info_lock);
  837. mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
  838. mvmsta->tid_data[tid].is_tid_active = true;
  839. spin_unlock(&mvm->queue_info_lock);
  840. IWL_DEBUG_TX_QUEUES(mvm, "Re-activating queue %d for TX\n",
  841. txq_id);
  842. }
  843. if (iwl_mvm_is_dqa_supported(mvm)) {
  844. /* Keep track of the time of the last frame for this RA/TID */
  845. mvm->queue_info[txq_id].last_frame_time[tid] = jiffies;
  846. /*
  847. * If we have timed-out TIDs - schedule the worker that will
  848. * reconfig the queues and update them
  849. *
  850. * Note that the mvm->queue_info_lock isn't being taken here in
  851. * order to not serialize the TX flow. This isn't dangerous
  852. * because scheduling mvm->add_stream_wk can't ruin the state,
  853. * and if we DON'T schedule it due to some race condition then
  854. * next TX we get here we will.
  855. */
  856. if (unlikely(mvm->queue_info[txq_id].status ==
  857. IWL_MVM_QUEUE_SHARED &&
  858. iwl_mvm_txq_should_update(mvm, txq_id)))
  859. schedule_work(&mvm->add_stream_wk);
  860. }
  861. IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x\n", mvmsta->sta_id,
  862. tid, txq_id, IEEE80211_SEQ_TO_SN(seq_number));
  863. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, txq_id))
  864. goto drop_unlock_sta;
  865. if (tid < IWL_MAX_TID_COUNT && !ieee80211_has_morefrags(fc))
  866. mvmsta->tid_data[tid].seq_number = seq_number + 0x10;
  867. spin_unlock(&mvmsta->lock);
  868. /* Increase pending frames count if this isn't AMPDU */
  869. if (!is_ampdu)
  870. atomic_inc(&mvm->pending_frames[mvmsta->sta_id]);
  871. return 0;
  872. drop_unlock_sta:
  873. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  874. spin_unlock(&mvmsta->lock);
  875. drop:
  876. return -1;
  877. }
  878. int iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
  879. struct ieee80211_sta *sta)
  880. {
  881. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  882. struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
  883. struct ieee80211_tx_info info;
  884. struct sk_buff_head mpdus_skbs;
  885. unsigned int payload_len;
  886. int ret;
  887. if (WARN_ON_ONCE(!mvmsta))
  888. return -1;
  889. if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
  890. return -1;
  891. memcpy(&info, skb->cb, sizeof(info));
  892. /* This holds the amsdu headers length */
  893. skb_info->driver_data[0] = (void *)(uintptr_t)0;
  894. if (!skb_is_gso(skb))
  895. return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
  896. payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
  897. tcp_hdrlen(skb) + skb->data_len;
  898. if (payload_len <= skb_shinfo(skb)->gso_size)
  899. return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
  900. __skb_queue_head_init(&mpdus_skbs);
  901. ret = iwl_mvm_tx_tso(mvm, skb, &info, sta, &mpdus_skbs);
  902. if (ret)
  903. return ret;
  904. if (WARN_ON(skb_queue_empty(&mpdus_skbs)))
  905. return ret;
  906. while (!skb_queue_empty(&mpdus_skbs)) {
  907. skb = __skb_dequeue(&mpdus_skbs);
  908. ret = iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
  909. if (ret) {
  910. __skb_queue_purge(&mpdus_skbs);
  911. return ret;
  912. }
  913. }
  914. return 0;
  915. }
  916. static void iwl_mvm_check_ratid_empty(struct iwl_mvm *mvm,
  917. struct ieee80211_sta *sta, u8 tid)
  918. {
  919. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  920. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  921. struct ieee80211_vif *vif = mvmsta->vif;
  922. lockdep_assert_held(&mvmsta->lock);
  923. if ((tid_data->state == IWL_AGG_ON ||
  924. tid_data->state == IWL_EMPTYING_HW_QUEUE_DELBA) &&
  925. iwl_mvm_tid_queued(tid_data) == 0) {
  926. /*
  927. * Now that this aggregation queue is empty tell mac80211 so it
  928. * knows we no longer have frames buffered for the station on
  929. * this TID (for the TIM bitmap calculation.)
  930. */
  931. ieee80211_sta_set_buffered(sta, tid, false);
  932. }
  933. if (tid_data->ssn != tid_data->next_reclaimed)
  934. return;
  935. switch (tid_data->state) {
  936. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  937. IWL_DEBUG_TX_QUEUES(mvm,
  938. "Can continue addBA flow ssn = next_recl = %d\n",
  939. tid_data->next_reclaimed);
  940. tid_data->state = IWL_AGG_STARTING;
  941. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  942. break;
  943. case IWL_EMPTYING_HW_QUEUE_DELBA:
  944. IWL_DEBUG_TX_QUEUES(mvm,
  945. "Can continue DELBA flow ssn = next_recl = %d\n",
  946. tid_data->next_reclaimed);
  947. if (!iwl_mvm_is_dqa_supported(mvm)) {
  948. u8 mac80211_ac = tid_to_mac80211_ac[tid];
  949. iwl_mvm_disable_txq(mvm, tid_data->txq_id,
  950. vif->hw_queue[mac80211_ac], tid,
  951. CMD_ASYNC);
  952. }
  953. tid_data->state = IWL_AGG_OFF;
  954. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  955. break;
  956. default:
  957. break;
  958. }
  959. }
  960. #ifdef CONFIG_IWLWIFI_DEBUG
  961. const char *iwl_mvm_get_tx_fail_reason(u32 status)
  962. {
  963. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  964. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  965. switch (status & TX_STATUS_MSK) {
  966. case TX_STATUS_SUCCESS:
  967. return "SUCCESS";
  968. TX_STATUS_POSTPONE(DELAY);
  969. TX_STATUS_POSTPONE(FEW_BYTES);
  970. TX_STATUS_POSTPONE(BT_PRIO);
  971. TX_STATUS_POSTPONE(QUIET_PERIOD);
  972. TX_STATUS_POSTPONE(CALC_TTAK);
  973. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  974. TX_STATUS_FAIL(SHORT_LIMIT);
  975. TX_STATUS_FAIL(LONG_LIMIT);
  976. TX_STATUS_FAIL(UNDERRUN);
  977. TX_STATUS_FAIL(DRAIN_FLOW);
  978. TX_STATUS_FAIL(RFKILL_FLUSH);
  979. TX_STATUS_FAIL(LIFE_EXPIRE);
  980. TX_STATUS_FAIL(DEST_PS);
  981. TX_STATUS_FAIL(HOST_ABORTED);
  982. TX_STATUS_FAIL(BT_RETRY);
  983. TX_STATUS_FAIL(STA_INVALID);
  984. TX_STATUS_FAIL(FRAG_DROPPED);
  985. TX_STATUS_FAIL(TID_DISABLE);
  986. TX_STATUS_FAIL(FIFO_FLUSHED);
  987. TX_STATUS_FAIL(SMALL_CF_POLL);
  988. TX_STATUS_FAIL(FW_DROP);
  989. TX_STATUS_FAIL(STA_COLOR_MISMATCH);
  990. }
  991. return "UNKNOWN";
  992. #undef TX_STATUS_FAIL
  993. #undef TX_STATUS_POSTPONE
  994. }
  995. #endif /* CONFIG_IWLWIFI_DEBUG */
  996. void iwl_mvm_hwrate_to_tx_rate(u32 rate_n_flags,
  997. enum nl80211_band band,
  998. struct ieee80211_tx_rate *r)
  999. {
  1000. if (rate_n_flags & RATE_HT_MCS_GF_MSK)
  1001. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  1002. switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
  1003. case RATE_MCS_CHAN_WIDTH_20:
  1004. break;
  1005. case RATE_MCS_CHAN_WIDTH_40:
  1006. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1007. break;
  1008. case RATE_MCS_CHAN_WIDTH_80:
  1009. r->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
  1010. break;
  1011. case RATE_MCS_CHAN_WIDTH_160:
  1012. r->flags |= IEEE80211_TX_RC_160_MHZ_WIDTH;
  1013. break;
  1014. }
  1015. if (rate_n_flags & RATE_MCS_SGI_MSK)
  1016. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  1017. if (rate_n_flags & RATE_MCS_HT_MSK) {
  1018. r->flags |= IEEE80211_TX_RC_MCS;
  1019. r->idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
  1020. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  1021. ieee80211_rate_set_vht(
  1022. r, rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK,
  1023. ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
  1024. RATE_VHT_MCS_NSS_POS) + 1);
  1025. r->flags |= IEEE80211_TX_RC_VHT_MCS;
  1026. } else {
  1027. r->idx = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
  1028. band);
  1029. }
  1030. }
  1031. /**
  1032. * translate ucode response to mac80211 tx status control values
  1033. */
  1034. static void iwl_mvm_hwrate_to_tx_status(u32 rate_n_flags,
  1035. struct ieee80211_tx_info *info)
  1036. {
  1037. struct ieee80211_tx_rate *r = &info->status.rates[0];
  1038. info->status.antenna =
  1039. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  1040. iwl_mvm_hwrate_to_tx_rate(rate_n_flags, info->band, r);
  1041. }
  1042. static void iwl_mvm_tx_status_check_trigger(struct iwl_mvm *mvm,
  1043. u32 status)
  1044. {
  1045. struct iwl_fw_dbg_trigger_tlv *trig;
  1046. struct iwl_fw_dbg_trigger_tx_status *status_trig;
  1047. int i;
  1048. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TX_STATUS))
  1049. return;
  1050. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TX_STATUS);
  1051. status_trig = (void *)trig->data;
  1052. if (!iwl_fw_dbg_trigger_check_stop(mvm, NULL, trig))
  1053. return;
  1054. for (i = 0; i < ARRAY_SIZE(status_trig->statuses); i++) {
  1055. /* don't collect on status 0 */
  1056. if (!status_trig->statuses[i].status)
  1057. break;
  1058. if (status_trig->statuses[i].status != (status & TX_STATUS_MSK))
  1059. continue;
  1060. iwl_mvm_fw_dbg_collect_trig(mvm, trig,
  1061. "Tx status %d was received",
  1062. status & TX_STATUS_MSK);
  1063. break;
  1064. }
  1065. }
  1066. static void iwl_mvm_rx_tx_cmd_single(struct iwl_mvm *mvm,
  1067. struct iwl_rx_packet *pkt)
  1068. {
  1069. struct ieee80211_sta *sta;
  1070. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1071. int txq_id = SEQ_TO_QUEUE(sequence);
  1072. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1073. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  1074. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  1075. u32 status = le16_to_cpu(tx_resp->status.status);
  1076. u16 ssn = iwl_mvm_get_scd_ssn(tx_resp);
  1077. struct iwl_mvm_sta *mvmsta;
  1078. struct sk_buff_head skbs;
  1079. u8 skb_freed = 0;
  1080. u16 next_reclaimed, seq_ctl;
  1081. bool is_ndp = false;
  1082. bool txq_agg = false; /* Is this TXQ aggregated */
  1083. __skb_queue_head_init(&skbs);
  1084. seq_ctl = le16_to_cpu(tx_resp->seq_ctl);
  1085. /* we can free until ssn % q.n_bd not inclusive */
  1086. iwl_trans_reclaim(mvm->trans, txq_id, ssn, &skbs);
  1087. while (!skb_queue_empty(&skbs)) {
  1088. struct sk_buff *skb = __skb_dequeue(&skbs);
  1089. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1090. skb_freed++;
  1091. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  1092. memset(&info->status, 0, sizeof(info->status));
  1093. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  1094. /* inform mac80211 about what happened with the frame */
  1095. switch (status & TX_STATUS_MSK) {
  1096. case TX_STATUS_SUCCESS:
  1097. case TX_STATUS_DIRECT_DONE:
  1098. info->flags |= IEEE80211_TX_STAT_ACK;
  1099. break;
  1100. case TX_STATUS_FAIL_DEST_PS:
  1101. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1102. break;
  1103. default:
  1104. break;
  1105. }
  1106. iwl_mvm_tx_status_check_trigger(mvm, status);
  1107. info->status.rates[0].count = tx_resp->failure_frame + 1;
  1108. iwl_mvm_hwrate_to_tx_status(le32_to_cpu(tx_resp->initial_rate),
  1109. info);
  1110. info->status.status_driver_data[1] =
  1111. (void *)(uintptr_t)le32_to_cpu(tx_resp->initial_rate);
  1112. /* Single frame failure in an AMPDU queue => send BAR */
  1113. if (txq_id >= mvm->first_agg_queue &&
  1114. !(info->flags & IEEE80211_TX_STAT_ACK) &&
  1115. !(info->flags & IEEE80211_TX_STAT_TX_FILTERED))
  1116. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  1117. /* W/A FW bug: seq_ctl is wrong when the status isn't success */
  1118. if (status != TX_STATUS_SUCCESS) {
  1119. struct ieee80211_hdr *hdr = (void *)skb->data;
  1120. seq_ctl = le16_to_cpu(hdr->seq_ctrl);
  1121. }
  1122. if (unlikely(!seq_ctl)) {
  1123. struct ieee80211_hdr *hdr = (void *)skb->data;
  1124. /*
  1125. * If it is an NDP, we can't update next_reclaim since
  1126. * its sequence control is 0. Note that for that same
  1127. * reason, NDPs are never sent to A-MPDU'able queues
  1128. * so that we can never have more than one freed frame
  1129. * for a single Tx resonse (see WARN_ON below).
  1130. */
  1131. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  1132. is_ndp = true;
  1133. }
  1134. /*
  1135. * TODO: this is not accurate if we are freeing more than one
  1136. * packet.
  1137. */
  1138. info->status.tx_time =
  1139. le16_to_cpu(tx_resp->wireless_media_time);
  1140. BUILD_BUG_ON(ARRAY_SIZE(info->status.status_driver_data) < 1);
  1141. info->status.status_driver_data[0] =
  1142. (void *)(uintptr_t)tx_resp->reduced_tpc;
  1143. ieee80211_tx_status(mvm->hw, skb);
  1144. }
  1145. if (txq_id >= mvm->first_agg_queue) {
  1146. /* If this is an aggregation queue, we use the ssn since:
  1147. * ssn = wifi seq_num % 256.
  1148. * The seq_ctl is the sequence control of the packet to which
  1149. * this Tx response relates. But if there is a hole in the
  1150. * bitmap of the BA we received, this Tx response may allow to
  1151. * reclaim the hole and all the subsequent packets that were
  1152. * already acked. In that case, seq_ctl != ssn, and the next
  1153. * packet to be reclaimed will be ssn and not seq_ctl. In that
  1154. * case, several packets will be reclaimed even if
  1155. * frame_count = 1.
  1156. *
  1157. * The ssn is the index (% 256) of the latest packet that has
  1158. * treated (acked / dropped) + 1.
  1159. */
  1160. next_reclaimed = ssn;
  1161. } else {
  1162. /* The next packet to be reclaimed is the one after this one */
  1163. next_reclaimed = IEEE80211_SEQ_TO_SN(seq_ctl + 0x10);
  1164. }
  1165. IWL_DEBUG_TX_REPLY(mvm,
  1166. "TXQ %d status %s (0x%08x)\n",
  1167. txq_id, iwl_mvm_get_tx_fail_reason(status), status);
  1168. IWL_DEBUG_TX_REPLY(mvm,
  1169. "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d next_reclaimed=0x%x seq_ctl=0x%x\n",
  1170. le32_to_cpu(tx_resp->initial_rate),
  1171. tx_resp->failure_frame, SEQ_TO_INDEX(sequence),
  1172. ssn, next_reclaimed, seq_ctl);
  1173. rcu_read_lock();
  1174. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1175. /*
  1176. * sta can't be NULL otherwise it'd mean that the sta has been freed in
  1177. * the firmware while we still have packets for it in the Tx queues.
  1178. */
  1179. if (WARN_ON_ONCE(!sta))
  1180. goto out;
  1181. if (!IS_ERR(sta)) {
  1182. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1183. if (tid != IWL_TID_NON_QOS) {
  1184. struct iwl_mvm_tid_data *tid_data =
  1185. &mvmsta->tid_data[tid];
  1186. bool send_eosp_ndp = false;
  1187. spin_lock_bh(&mvmsta->lock);
  1188. if (iwl_mvm_is_dqa_supported(mvm)) {
  1189. enum iwl_mvm_agg_state state;
  1190. state = mvmsta->tid_data[tid].state;
  1191. txq_agg = (state == IWL_AGG_ON ||
  1192. state == IWL_EMPTYING_HW_QUEUE_DELBA);
  1193. } else {
  1194. txq_agg = txq_id >= mvm->first_agg_queue;
  1195. }
  1196. if (!is_ndp) {
  1197. tid_data->next_reclaimed = next_reclaimed;
  1198. IWL_DEBUG_TX_REPLY(mvm,
  1199. "Next reclaimed packet:%d\n",
  1200. next_reclaimed);
  1201. } else {
  1202. IWL_DEBUG_TX_REPLY(mvm,
  1203. "NDP - don't update next_reclaimed\n");
  1204. }
  1205. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  1206. if (mvmsta->sleep_tx_count) {
  1207. mvmsta->sleep_tx_count--;
  1208. if (mvmsta->sleep_tx_count &&
  1209. !iwl_mvm_tid_queued(tid_data)) {
  1210. /*
  1211. * The number of frames in the queue
  1212. * dropped to 0 even if we sent less
  1213. * frames than we thought we had on the
  1214. * Tx queue.
  1215. * This means we had holes in the BA
  1216. * window that we just filled, ask
  1217. * mac80211 to send EOSP since the
  1218. * firmware won't know how to do that.
  1219. * Send NDP and the firmware will send
  1220. * EOSP notification that will trigger
  1221. * a call to ieee80211_sta_eosp().
  1222. */
  1223. send_eosp_ndp = true;
  1224. }
  1225. }
  1226. spin_unlock_bh(&mvmsta->lock);
  1227. if (send_eosp_ndp) {
  1228. iwl_mvm_sta_modify_sleep_tx_count(mvm, sta,
  1229. IEEE80211_FRAME_RELEASE_UAPSD,
  1230. 1, tid, false, false);
  1231. mvmsta->sleep_tx_count = 0;
  1232. ieee80211_send_eosp_nullfunc(sta, tid);
  1233. }
  1234. }
  1235. if (mvmsta->next_status_eosp) {
  1236. mvmsta->next_status_eosp = false;
  1237. ieee80211_sta_eosp(sta);
  1238. }
  1239. } else {
  1240. mvmsta = NULL;
  1241. }
  1242. /*
  1243. * If the txq is not an AMPDU queue, there is no chance we freed
  1244. * several skbs. Check that out...
  1245. */
  1246. if (txq_agg)
  1247. goto out;
  1248. /* We can't free more than one frame at once on a shared queue */
  1249. WARN_ON(!iwl_mvm_is_dqa_supported(mvm) && (skb_freed > 1));
  1250. /* If we have still frames for this STA nothing to do here */
  1251. if (!atomic_sub_and_test(skb_freed, &mvm->pending_frames[sta_id]))
  1252. goto out;
  1253. if (mvmsta && mvmsta->vif->type == NL80211_IFTYPE_AP) {
  1254. /*
  1255. * If there are no pending frames for this STA and
  1256. * the tx to this station is not disabled, notify
  1257. * mac80211 that this station can now wake up in its
  1258. * STA table.
  1259. * If mvmsta is not NULL, sta is valid.
  1260. */
  1261. spin_lock_bh(&mvmsta->lock);
  1262. if (!mvmsta->disable_tx)
  1263. ieee80211_sta_block_awake(mvm->hw, sta, false);
  1264. spin_unlock_bh(&mvmsta->lock);
  1265. }
  1266. if (PTR_ERR(sta) == -EBUSY || PTR_ERR(sta) == -ENOENT) {
  1267. /*
  1268. * We are draining and this was the last packet - pre_rcu_remove
  1269. * has been called already. We might be after the
  1270. * synchronize_net already.
  1271. * Don't rely on iwl_mvm_rm_sta to see the empty Tx queues.
  1272. */
  1273. set_bit(sta_id, mvm->sta_drained);
  1274. schedule_work(&mvm->sta_drained_wk);
  1275. }
  1276. out:
  1277. rcu_read_unlock();
  1278. }
  1279. #ifdef CONFIG_IWLWIFI_DEBUG
  1280. #define AGG_TX_STATE_(x) case AGG_TX_STATE_ ## x: return #x
  1281. static const char *iwl_get_agg_tx_status(u16 status)
  1282. {
  1283. switch (status & AGG_TX_STATE_STATUS_MSK) {
  1284. AGG_TX_STATE_(TRANSMITTED);
  1285. AGG_TX_STATE_(UNDERRUN);
  1286. AGG_TX_STATE_(BT_PRIO);
  1287. AGG_TX_STATE_(FEW_BYTES);
  1288. AGG_TX_STATE_(ABORT);
  1289. AGG_TX_STATE_(LAST_SENT_TTL);
  1290. AGG_TX_STATE_(LAST_SENT_TRY_CNT);
  1291. AGG_TX_STATE_(LAST_SENT_BT_KILL);
  1292. AGG_TX_STATE_(SCD_QUERY);
  1293. AGG_TX_STATE_(TEST_BAD_CRC32);
  1294. AGG_TX_STATE_(RESPONSE);
  1295. AGG_TX_STATE_(DUMP_TX);
  1296. AGG_TX_STATE_(DELAY_TX);
  1297. }
  1298. return "UNKNOWN";
  1299. }
  1300. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  1301. struct iwl_rx_packet *pkt)
  1302. {
  1303. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1304. struct agg_tx_status *frame_status = &tx_resp->status;
  1305. int i;
  1306. for (i = 0; i < tx_resp->frame_count; i++) {
  1307. u16 fstatus = le16_to_cpu(frame_status[i].status);
  1308. IWL_DEBUG_TX_REPLY(mvm,
  1309. "status %s (0x%04x), try-count (%d) seq (0x%x)\n",
  1310. iwl_get_agg_tx_status(fstatus),
  1311. fstatus & AGG_TX_STATE_STATUS_MSK,
  1312. (fstatus & AGG_TX_STATE_TRY_CNT_MSK) >>
  1313. AGG_TX_STATE_TRY_CNT_POS,
  1314. le16_to_cpu(frame_status[i].sequence));
  1315. }
  1316. }
  1317. #else
  1318. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  1319. struct iwl_rx_packet *pkt)
  1320. {}
  1321. #endif /* CONFIG_IWLWIFI_DEBUG */
  1322. static void iwl_mvm_rx_tx_cmd_agg(struct iwl_mvm *mvm,
  1323. struct iwl_rx_packet *pkt)
  1324. {
  1325. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1326. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  1327. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  1328. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1329. struct iwl_mvm_sta *mvmsta;
  1330. int queue = SEQ_TO_QUEUE(sequence);
  1331. if (WARN_ON_ONCE(queue < mvm->first_agg_queue &&
  1332. (!iwl_mvm_is_dqa_supported(mvm) ||
  1333. (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE))))
  1334. return;
  1335. if (WARN_ON_ONCE(tid == IWL_TID_NON_QOS))
  1336. return;
  1337. iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);
  1338. rcu_read_lock();
  1339. mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
  1340. if (!WARN_ON_ONCE(!mvmsta)) {
  1341. mvmsta->tid_data[tid].rate_n_flags =
  1342. le32_to_cpu(tx_resp->initial_rate);
  1343. mvmsta->tid_data[tid].tx_time =
  1344. le16_to_cpu(tx_resp->wireless_media_time);
  1345. }
  1346. rcu_read_unlock();
  1347. }
  1348. void iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  1349. {
  1350. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1351. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1352. if (tx_resp->frame_count == 1)
  1353. iwl_mvm_rx_tx_cmd_single(mvm, pkt);
  1354. else
  1355. iwl_mvm_rx_tx_cmd_agg(mvm, pkt);
  1356. }
  1357. static void iwl_mvm_tx_reclaim(struct iwl_mvm *mvm, int sta_id, int tid,
  1358. int txq, int index,
  1359. struct ieee80211_tx_info *ba_info, u32 rate)
  1360. {
  1361. struct sk_buff_head reclaimed_skbs;
  1362. struct iwl_mvm_tid_data *tid_data;
  1363. struct ieee80211_sta *sta;
  1364. struct iwl_mvm_sta *mvmsta;
  1365. struct sk_buff *skb;
  1366. int freed;
  1367. if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT ||
  1368. tid >= IWL_MAX_TID_COUNT,
  1369. "sta_id %d tid %d", sta_id, tid))
  1370. return;
  1371. rcu_read_lock();
  1372. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1373. /* Reclaiming frames for a station that has been deleted ? */
  1374. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  1375. rcu_read_unlock();
  1376. return;
  1377. }
  1378. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1379. tid_data = &mvmsta->tid_data[tid];
  1380. if (tid_data->txq_id != txq) {
  1381. IWL_ERR(mvm,
  1382. "invalid BA notification: Q %d, tid %d\n",
  1383. tid_data->txq_id, tid);
  1384. rcu_read_unlock();
  1385. return;
  1386. }
  1387. spin_lock_bh(&mvmsta->lock);
  1388. __skb_queue_head_init(&reclaimed_skbs);
  1389. /*
  1390. * Release all TFDs before the SSN, i.e. all TFDs in front of
  1391. * block-ack window (we assume that they've been successfully
  1392. * transmitted ... if not, it's too late anyway).
  1393. */
  1394. iwl_trans_reclaim(mvm->trans, txq, index, &reclaimed_skbs);
  1395. tid_data->next_reclaimed = index;
  1396. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  1397. freed = 0;
  1398. ba_info->status.status_driver_data[1] = (void *)(uintptr_t)rate;
  1399. skb_queue_walk(&reclaimed_skbs, skb) {
  1400. struct ieee80211_hdr *hdr = (void *)skb->data;
  1401. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1402. if (ieee80211_is_data_qos(hdr->frame_control))
  1403. freed++;
  1404. else
  1405. WARN_ON_ONCE(1);
  1406. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  1407. memset(&info->status, 0, sizeof(info->status));
  1408. /* Packet was transmitted successfully, failures come as single
  1409. * frames because before failing a frame the firmware transmits
  1410. * it without aggregation at least once.
  1411. */
  1412. info->flags |= IEEE80211_TX_STAT_ACK;
  1413. /* this is the first skb we deliver in this batch */
  1414. /* put the rate scaling data there */
  1415. if (freed == 1) {
  1416. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1417. memcpy(&info->status, &ba_info->status,
  1418. sizeof(ba_info->status));
  1419. iwl_mvm_hwrate_to_tx_status(rate, info);
  1420. }
  1421. }
  1422. spin_unlock_bh(&mvmsta->lock);
  1423. /* We got a BA notif with 0 acked or scd_ssn didn't progress which is
  1424. * possible (i.e. first MPDU in the aggregation wasn't acked)
  1425. * Still it's important to update RS about sent vs. acked.
  1426. */
  1427. if (skb_queue_empty(&reclaimed_skbs)) {
  1428. struct ieee80211_chanctx_conf *chanctx_conf = NULL;
  1429. if (mvmsta->vif)
  1430. chanctx_conf =
  1431. rcu_dereference(mvmsta->vif->chanctx_conf);
  1432. if (WARN_ON_ONCE(!chanctx_conf))
  1433. goto out;
  1434. ba_info->band = chanctx_conf->def.chan->band;
  1435. iwl_mvm_hwrate_to_tx_status(rate, ba_info);
  1436. IWL_DEBUG_TX_REPLY(mvm, "No reclaim. Update rs directly\n");
  1437. iwl_mvm_rs_tx_status(mvm, sta, tid, ba_info, false);
  1438. }
  1439. out:
  1440. rcu_read_unlock();
  1441. while (!skb_queue_empty(&reclaimed_skbs)) {
  1442. skb = __skb_dequeue(&reclaimed_skbs);
  1443. ieee80211_tx_status(mvm->hw, skb);
  1444. }
  1445. }
  1446. void iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  1447. {
  1448. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1449. int sta_id, tid, txq, index;
  1450. struct ieee80211_tx_info ba_info = {};
  1451. struct iwl_mvm_ba_notif *ba_notif;
  1452. struct iwl_mvm_tid_data *tid_data;
  1453. struct iwl_mvm_sta *mvmsta;
  1454. if (iwl_mvm_has_new_tx_api(mvm)) {
  1455. struct iwl_mvm_compressed_ba_notif *ba_res =
  1456. (void *)pkt->data;
  1457. sta_id = ba_res->sta_id;
  1458. ba_info.status.ampdu_ack_len = (u8)le16_to_cpu(ba_res->done);
  1459. ba_info.status.ampdu_len = (u8)le16_to_cpu(ba_res->txed);
  1460. ba_info.status.tx_time =
  1461. (u16)le32_to_cpu(ba_res->wireless_time);
  1462. ba_info.status.status_driver_data[0] =
  1463. (void *)(uintptr_t)ba_res->reduced_txp;
  1464. /*
  1465. * TODO:
  1466. * When supporting multi TID aggregations - we need to move
  1467. * next_reclaimed to be per TXQ and not per TID or handle it
  1468. * in a different way.
  1469. * This will go together with SN and AddBA offload and cannot
  1470. * be handled properly for now.
  1471. */
  1472. WARN_ON(le16_to_cpu(ba_res->tfd_cnt) != 1);
  1473. iwl_mvm_tx_reclaim(mvm, sta_id, ba_res->ra_tid[0].tid,
  1474. (int)ba_res->tfd[0].q_num,
  1475. le16_to_cpu(ba_res->tfd[0].tfd_index),
  1476. &ba_info, le32_to_cpu(ba_res->tx_rate));
  1477. IWL_DEBUG_TX_REPLY(mvm,
  1478. "BA_NOTIFICATION Received from sta_id = %d, flags %x, sent:%d, acked:%d\n",
  1479. sta_id, le32_to_cpu(ba_res->flags),
  1480. le16_to_cpu(ba_res->txed),
  1481. le16_to_cpu(ba_res->done));
  1482. return;
  1483. }
  1484. ba_notif = (void *)pkt->data;
  1485. sta_id = ba_notif->sta_id;
  1486. tid = ba_notif->tid;
  1487. /* "flow" corresponds to Tx queue */
  1488. txq = le16_to_cpu(ba_notif->scd_flow);
  1489. /* "ssn" is start of block-ack Tx window, corresponds to index
  1490. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1491. index = le16_to_cpu(ba_notif->scd_ssn);
  1492. rcu_read_lock();
  1493. mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
  1494. if (WARN_ON_ONCE(!mvmsta)) {
  1495. rcu_read_unlock();
  1496. return;
  1497. }
  1498. tid_data = &mvmsta->tid_data[tid];
  1499. ba_info.status.ampdu_ack_len = ba_notif->txed_2_done;
  1500. ba_info.status.ampdu_len = ba_notif->txed;
  1501. ba_info.status.tx_time = tid_data->tx_time;
  1502. ba_info.status.status_driver_data[0] =
  1503. (void *)(uintptr_t)ba_notif->reduced_txp;
  1504. rcu_read_unlock();
  1505. iwl_mvm_tx_reclaim(mvm, sta_id, tid, txq, index, &ba_info,
  1506. tid_data->rate_n_flags);
  1507. IWL_DEBUG_TX_REPLY(mvm,
  1508. "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
  1509. (u8 *)&ba_notif->sta_addr_lo32, ba_notif->sta_id);
  1510. IWL_DEBUG_TX_REPLY(mvm,
  1511. "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
  1512. ba_notif->tid, le16_to_cpu(ba_notif->seq_ctl),
  1513. le64_to_cpu(ba_notif->bitmap), txq, index,
  1514. ba_notif->txed, ba_notif->txed_2_done);
  1515. IWL_DEBUG_TX_REPLY(mvm, "reduced txp from ba notif %d\n",
  1516. ba_notif->reduced_txp);
  1517. }
  1518. /*
  1519. * Note that there are transports that buffer frames before they reach
  1520. * the firmware. This means that after flush_tx_path is called, the
  1521. * queue might not be empty. The race-free way to handle this is to:
  1522. * 1) set the station as draining
  1523. * 2) flush the Tx path
  1524. * 3) wait for the transport queues to be empty
  1525. */
  1526. int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk, u32 flags)
  1527. {
  1528. int ret;
  1529. struct iwl_tx_path_flush_cmd flush_cmd = {
  1530. .queues_ctl = cpu_to_le32(tfd_msk),
  1531. .flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
  1532. };
  1533. ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
  1534. sizeof(flush_cmd), &flush_cmd);
  1535. if (ret)
  1536. IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
  1537. return ret;
  1538. }