tx.c 58 KB

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