tx.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  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. memset(dev_cmd, 0, sizeof(dev_cmd->hdr) + sizeof(*tx_cmd));
  425. dev_cmd->hdr.cmd = TX_CMD;
  426. if (iwl_mvm_has_new_tx_api(mvm)) {
  427. struct iwl_tx_cmd_gen2 *cmd = (void *)dev_cmd->payload;
  428. u16 offload_assist = 0;
  429. if (ieee80211_is_data_qos(hdr->frame_control)) {
  430. u8 *qc = ieee80211_get_qos_ctl(hdr);
  431. if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
  432. offload_assist |= BIT(TX_CMD_OFFLD_AMSDU);
  433. }
  434. offload_assist = iwl_mvm_tx_csum(mvm, skb, hdr, info,
  435. offload_assist);
  436. /* padding is inserted later in transport */
  437. if (ieee80211_hdrlen(hdr->frame_control) % 4 &&
  438. !(offload_assist & BIT(TX_CMD_OFFLD_AMSDU)))
  439. offload_assist |= BIT(TX_CMD_OFFLD_PAD);
  440. cmd->offload_assist |= cpu_to_le16(offload_assist);
  441. /* Total # bytes to be transmitted */
  442. cmd->len = cpu_to_le16((u16)skb->len);
  443. /* Copy MAC header from skb into command buffer */
  444. memcpy(cmd->hdr, hdr, hdrlen);
  445. if (!info->control.hw_key)
  446. cmd->flags |= cpu_to_le32(IWL_TX_FLAGS_ENCRYPT_DIS);
  447. /* For data packets rate info comes from the fw */
  448. if (ieee80211_is_data(hdr->frame_control) && sta)
  449. goto out;
  450. cmd->flags |= cpu_to_le32(IWL_TX_FLAGS_CMD_RATE);
  451. cmd->rate_n_flags =
  452. cpu_to_le32(iwl_mvm_get_tx_rate(mvm, info, sta));
  453. goto out;
  454. }
  455. tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  456. if (info->control.hw_key)
  457. iwl_mvm_set_tx_cmd_crypto(mvm, info, tx_cmd, skb, hdrlen);
  458. iwl_mvm_set_tx_cmd(mvm, skb, tx_cmd, info, sta_id);
  459. iwl_mvm_set_tx_cmd_rate(mvm, tx_cmd, info, sta, hdr->frame_control);
  460. /* Copy MAC header from skb into command buffer */
  461. memcpy(tx_cmd->hdr, hdr, hdrlen);
  462. out:
  463. return dev_cmd;
  464. }
  465. static void iwl_mvm_skb_prepare_status(struct sk_buff *skb,
  466. struct iwl_device_cmd *cmd)
  467. {
  468. struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
  469. memset(&skb_info->status, 0, sizeof(skb_info->status));
  470. memset(skb_info->driver_data, 0, sizeof(skb_info->driver_data));
  471. skb_info->driver_data[1] = cmd;
  472. }
  473. static int iwl_mvm_get_ctrl_vif_queue(struct iwl_mvm *mvm,
  474. struct ieee80211_tx_info *info, __le16 fc)
  475. {
  476. struct iwl_mvm_vif *mvmvif;
  477. mvmvif = iwl_mvm_vif_from_mac80211(info->control.vif);
  478. switch (info->control.vif->type) {
  479. case NL80211_IFTYPE_AP:
  480. case NL80211_IFTYPE_ADHOC:
  481. /*
  482. * Non-bufferable frames use the broadcast station, thus they
  483. * use the probe queue.
  484. * Also take care of the case where we send a deauth to a
  485. * station that we don't have, or similarly an association
  486. * response (with non-success status) for a station we can't
  487. * accept.
  488. * Also, disassociate frames might happen, particular with
  489. * reason 7 ("Class 3 frame received from nonassociated STA").
  490. */
  491. if (ieee80211_is_mgmt(fc) &&
  492. (!ieee80211_is_bufferable_mmpdu(fc) ||
  493. ieee80211_is_deauth(fc) || ieee80211_is_disassoc(fc)))
  494. return mvm->probe_queue;
  495. if (info->hw_queue == info->control.vif->cab_queue)
  496. return mvmvif->cab_queue;
  497. WARN_ONCE(info->control.vif->type != NL80211_IFTYPE_ADHOC,
  498. "fc=0x%02x", le16_to_cpu(fc));
  499. return mvm->probe_queue;
  500. case NL80211_IFTYPE_P2P_DEVICE:
  501. if (ieee80211_is_mgmt(fc))
  502. return mvm->p2p_dev_queue;
  503. if (info->hw_queue == info->control.vif->cab_queue)
  504. return mvmvif->cab_queue;
  505. WARN_ON_ONCE(1);
  506. return mvm->p2p_dev_queue;
  507. default:
  508. WARN_ONCE(1, "Not a ctrl vif, no available queue\n");
  509. return -1;
  510. }
  511. }
  512. int iwl_mvm_tx_skb_non_sta(struct iwl_mvm *mvm, struct sk_buff *skb)
  513. {
  514. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  515. struct ieee80211_tx_info *skb_info = IEEE80211_SKB_CB(skb);
  516. struct ieee80211_tx_info info;
  517. struct iwl_device_cmd *dev_cmd;
  518. u8 sta_id;
  519. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  520. int queue;
  521. /* IWL_MVM_OFFCHANNEL_QUEUE is used for ROC packets that can be used
  522. * in 2 different types of vifs, P2P & STATION. P2P uses the offchannel
  523. * queue. STATION (HS2.0) uses the auxiliary context of the FW,
  524. * and hence needs to be sent on the aux queue
  525. */
  526. if (skb_info->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
  527. skb_info->control.vif->type == NL80211_IFTYPE_STATION)
  528. skb_info->hw_queue = mvm->aux_queue;
  529. memcpy(&info, skb->cb, sizeof(info));
  530. if (WARN_ON_ONCE(info.flags & IEEE80211_TX_CTL_AMPDU))
  531. return -1;
  532. if (WARN_ON_ONCE(info.flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
  533. (!info.control.vif ||
  534. info.hw_queue != info.control.vif->cab_queue)))
  535. return -1;
  536. queue = info.hw_queue;
  537. /*
  538. * If the interface on which the frame is sent is the P2P_DEVICE
  539. * or an AP/GO interface use the broadcast station associated
  540. * with it; otherwise if the interface is a managed interface
  541. * use the AP station associated with it for multicast traffic
  542. * (this is not possible for unicast packets as a TLDS discovery
  543. * response are sent without a station entry); otherwise use the
  544. * AUX station.
  545. */
  546. sta_id = mvm->aux_sta.sta_id;
  547. if (info.control.vif) {
  548. struct iwl_mvm_vif *mvmvif =
  549. iwl_mvm_vif_from_mac80211(info.control.vif);
  550. if (info.control.vif->type == NL80211_IFTYPE_P2P_DEVICE ||
  551. info.control.vif->type == NL80211_IFTYPE_AP ||
  552. info.control.vif->type == NL80211_IFTYPE_ADHOC) {
  553. if (info.control.vif->type == NL80211_IFTYPE_P2P_DEVICE)
  554. sta_id = mvmvif->bcast_sta.sta_id;
  555. else
  556. sta_id = mvmvif->mcast_sta.sta_id;
  557. queue = iwl_mvm_get_ctrl_vif_queue(mvm, &info,
  558. hdr->frame_control);
  559. if (queue < 0)
  560. return -1;
  561. } else if (info.control.vif->type == NL80211_IFTYPE_STATION &&
  562. is_multicast_ether_addr(hdr->addr1)) {
  563. u8 ap_sta_id = READ_ONCE(mvmvif->ap_sta_id);
  564. if (ap_sta_id != IWL_MVM_INVALID_STA)
  565. sta_id = ap_sta_id;
  566. } else if (info.control.vif->type == NL80211_IFTYPE_MONITOR) {
  567. queue = mvm->snif_queue;
  568. sta_id = mvm->snif_sta.sta_id;
  569. }
  570. }
  571. IWL_DEBUG_TX(mvm, "station Id %d, queue=%d\n", sta_id, queue);
  572. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, &info, hdrlen, NULL, sta_id);
  573. if (!dev_cmd)
  574. return -1;
  575. /* From now on, we cannot access info->control */
  576. iwl_mvm_skb_prepare_status(skb, dev_cmd);
  577. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, queue)) {
  578. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  579. return -1;
  580. }
  581. return 0;
  582. }
  583. #ifdef CONFIG_INET
  584. static int
  585. iwl_mvm_tx_tso_segment(struct sk_buff *skb, unsigned int num_subframes,
  586. netdev_features_t netdev_flags,
  587. struct sk_buff_head *mpdus_skb)
  588. {
  589. struct sk_buff *tmp, *next;
  590. struct ieee80211_hdr *hdr = (void *)skb->data;
  591. char cb[sizeof(skb->cb)];
  592. u16 i = 0;
  593. unsigned int tcp_payload_len;
  594. unsigned int mss = skb_shinfo(skb)->gso_size;
  595. bool ipv4 = (skb->protocol == htons(ETH_P_IP));
  596. u16 ip_base_id = ipv4 ? ntohs(ip_hdr(skb)->id) : 0;
  597. skb_shinfo(skb)->gso_size = num_subframes * mss;
  598. memcpy(cb, skb->cb, sizeof(cb));
  599. next = skb_gso_segment(skb, netdev_flags);
  600. skb_shinfo(skb)->gso_size = mss;
  601. if (WARN_ON_ONCE(IS_ERR(next)))
  602. return -EINVAL;
  603. else if (next)
  604. consume_skb(skb);
  605. while (next) {
  606. tmp = next;
  607. next = tmp->next;
  608. memcpy(tmp->cb, cb, sizeof(tmp->cb));
  609. /*
  610. * Compute the length of all the data added for the A-MSDU.
  611. * This will be used to compute the length to write in the TX
  612. * command. We have: SNAP + IP + TCP for n -1 subframes and
  613. * ETH header for n subframes.
  614. */
  615. tcp_payload_len = skb_tail_pointer(tmp) -
  616. skb_transport_header(tmp) -
  617. tcp_hdrlen(tmp) + tmp->data_len;
  618. if (ipv4)
  619. ip_hdr(tmp)->id = htons(ip_base_id + i * num_subframes);
  620. if (tcp_payload_len > mss) {
  621. skb_shinfo(tmp)->gso_size = mss;
  622. } else {
  623. if (ieee80211_is_data_qos(hdr->frame_control)) {
  624. u8 *qc;
  625. if (ipv4)
  626. ip_send_check(ip_hdr(tmp));
  627. qc = ieee80211_get_qos_ctl((void *)tmp->data);
  628. *qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  629. }
  630. skb_shinfo(tmp)->gso_size = 0;
  631. }
  632. tmp->prev = NULL;
  633. tmp->next = NULL;
  634. __skb_queue_tail(mpdus_skb, tmp);
  635. i++;
  636. }
  637. return 0;
  638. }
  639. static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
  640. struct ieee80211_tx_info *info,
  641. struct ieee80211_sta *sta,
  642. struct sk_buff_head *mpdus_skb)
  643. {
  644. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  645. struct ieee80211_hdr *hdr = (void *)skb->data;
  646. unsigned int mss = skb_shinfo(skb)->gso_size;
  647. unsigned int num_subframes, tcp_payload_len, subf_len, max_amsdu_len;
  648. u16 snap_ip_tcp, pad;
  649. unsigned int dbg_max_amsdu_len;
  650. netdev_features_t netdev_flags = NETIF_F_CSUM_MASK | NETIF_F_SG;
  651. u8 *qc, tid, txf;
  652. snap_ip_tcp = 8 + skb_transport_header(skb) - skb_network_header(skb) +
  653. tcp_hdrlen(skb);
  654. dbg_max_amsdu_len = READ_ONCE(mvm->max_amsdu_len);
  655. if (!sta->max_amsdu_len ||
  656. !ieee80211_is_data_qos(hdr->frame_control) ||
  657. (!mvmsta->tlc_amsdu && !dbg_max_amsdu_len))
  658. return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);
  659. /*
  660. * Do not build AMSDU for IPv6 with extension headers.
  661. * ask stack to segment and checkum the generated MPDUs for us.
  662. */
  663. if (skb->protocol == htons(ETH_P_IPV6) &&
  664. ((struct ipv6hdr *)skb_network_header(skb))->nexthdr !=
  665. IPPROTO_TCP) {
  666. netdev_flags &= ~NETIF_F_CSUM_MASK;
  667. return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);
  668. }
  669. qc = ieee80211_get_qos_ctl(hdr);
  670. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  671. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  672. return -EINVAL;
  673. /*
  674. * No need to lock amsdu_in_ampdu_allowed since it can't be modified
  675. * during an BA session.
  676. */
  677. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  678. !mvmsta->tid_data[tid].amsdu_in_ampdu_allowed)
  679. return iwl_mvm_tx_tso_segment(skb, 1, netdev_flags, mpdus_skb);
  680. max_amsdu_len = sta->max_amsdu_len;
  681. /* the Tx FIFO to which this A-MSDU will be routed */
  682. txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, tid_to_mac80211_ac[tid]);
  683. /*
  684. * Don't send an AMSDU that will be longer than the TXF.
  685. * Add a security margin of 256 for the TX command + headers.
  686. * We also want to have the start of the next packet inside the
  687. * fifo to be able to send bursts.
  688. */
  689. max_amsdu_len = min_t(unsigned int, max_amsdu_len,
  690. mvm->fwrt.smem_cfg.lmac[0].txfifo_size[txf] -
  691. 256);
  692. if (unlikely(dbg_max_amsdu_len))
  693. max_amsdu_len = min_t(unsigned int, max_amsdu_len,
  694. dbg_max_amsdu_len);
  695. /*
  696. * Limit A-MSDU in A-MPDU to 4095 bytes when VHT is not
  697. * supported. This is a spec requirement (IEEE 802.11-2015
  698. * section 8.7.3 NOTE 3).
  699. */
  700. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  701. !sta->vht_cap.vht_supported)
  702. max_amsdu_len = min_t(unsigned int, max_amsdu_len, 4095);
  703. /* Sub frame header + SNAP + IP header + TCP header + MSS */
  704. subf_len = sizeof(struct ethhdr) + snap_ip_tcp + mss;
  705. pad = (4 - subf_len) & 0x3;
  706. /*
  707. * If we have N subframes in the A-MSDU, then the A-MSDU's size is
  708. * N * subf_len + (N - 1) * pad.
  709. */
  710. num_subframes = (max_amsdu_len + pad) / (subf_len + pad);
  711. if (num_subframes > 1)
  712. *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  713. tcp_payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
  714. tcp_hdrlen(skb) + skb->data_len;
  715. /*
  716. * Make sure we have enough TBs for the A-MSDU:
  717. * 2 for each subframe
  718. * 1 more for each fragment
  719. * 1 more for the potential data in the header
  720. */
  721. num_subframes =
  722. min_t(unsigned int, num_subframes,
  723. (mvm->trans->max_skb_frags - 1 -
  724. skb_shinfo(skb)->nr_frags) / 2);
  725. /* This skb fits in one single A-MSDU */
  726. if (num_subframes * mss >= tcp_payload_len) {
  727. __skb_queue_tail(mpdus_skb, skb);
  728. return 0;
  729. }
  730. /*
  731. * Trick the segmentation function to make it
  732. * create SKBs that can fit into one A-MSDU.
  733. */
  734. return iwl_mvm_tx_tso_segment(skb, num_subframes, netdev_flags,
  735. mpdus_skb);
  736. }
  737. #else /* CONFIG_INET */
  738. static int iwl_mvm_tx_tso(struct iwl_mvm *mvm, struct sk_buff *skb,
  739. struct ieee80211_tx_info *info,
  740. struct ieee80211_sta *sta,
  741. struct sk_buff_head *mpdus_skb)
  742. {
  743. /* Impossible to get TSO with CONFIG_INET */
  744. WARN_ON(1);
  745. return -1;
  746. }
  747. #endif
  748. static void iwl_mvm_tx_add_stream(struct iwl_mvm *mvm,
  749. struct iwl_mvm_sta *mvm_sta, u8 tid,
  750. struct sk_buff *skb)
  751. {
  752. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  753. u8 mac_queue = info->hw_queue;
  754. struct sk_buff_head *deferred_tx_frames;
  755. lockdep_assert_held(&mvm_sta->lock);
  756. mvm_sta->deferred_traffic_tid_map |= BIT(tid);
  757. set_bit(mvm_sta->sta_id, mvm->sta_deferred_frames);
  758. deferred_tx_frames = &mvm_sta->tid_data[tid].deferred_tx_frames;
  759. skb_queue_tail(deferred_tx_frames, skb);
  760. /*
  761. * The first deferred frame should've stopped the MAC queues, so we
  762. * should never get a second deferred frame for the RA/TID.
  763. * In case of GSO the first packet may have been split, so don't warn.
  764. */
  765. if (skb_queue_len(deferred_tx_frames) == 1) {
  766. iwl_mvm_stop_mac_queues(mvm, BIT(mac_queue));
  767. schedule_work(&mvm->add_stream_wk);
  768. }
  769. }
  770. /* Check if there are any timed-out TIDs on a given shared TXQ */
  771. static bool iwl_mvm_txq_should_update(struct iwl_mvm *mvm, int txq_id)
  772. {
  773. unsigned long queue_tid_bitmap = mvm->queue_info[txq_id].tid_bitmap;
  774. unsigned long now = jiffies;
  775. int tid;
  776. if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
  777. return false;
  778. for_each_set_bit(tid, &queue_tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  779. if (time_before(mvm->queue_info[txq_id].last_frame_time[tid] +
  780. IWL_MVM_DQA_QUEUE_TIMEOUT, now))
  781. return true;
  782. }
  783. return false;
  784. }
  785. /*
  786. * Sets the fields in the Tx cmd that are crypto related
  787. */
  788. static int iwl_mvm_tx_mpdu(struct iwl_mvm *mvm, struct sk_buff *skb,
  789. struct ieee80211_tx_info *info,
  790. struct ieee80211_sta *sta)
  791. {
  792. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  793. struct iwl_mvm_sta *mvmsta;
  794. struct iwl_device_cmd *dev_cmd;
  795. __le16 fc;
  796. u16 seq_number = 0;
  797. u8 tid = IWL_MAX_TID_COUNT;
  798. u16 txq_id = info->hw_queue;
  799. bool is_ampdu = false;
  800. int hdrlen;
  801. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  802. fc = hdr->frame_control;
  803. hdrlen = ieee80211_hdrlen(fc);
  804. if (WARN_ON_ONCE(!mvmsta))
  805. return -1;
  806. if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_INVALID_STA))
  807. return -1;
  808. dev_cmd = iwl_mvm_set_tx_params(mvm, skb, info, hdrlen,
  809. sta, mvmsta->sta_id);
  810. if (!dev_cmd)
  811. goto drop;
  812. /*
  813. * we handle that entirely ourselves -- for uAPSD the firmware
  814. * will always send a notification, and for PS-Poll responses
  815. * we'll notify mac80211 when getting frame status
  816. */
  817. info->flags &= ~IEEE80211_TX_STATUS_EOSP;
  818. spin_lock(&mvmsta->lock);
  819. /* nullfunc frames should go to the MGMT queue regardless of QOS,
  820. * the condition of !ieee80211_is_qos_nullfunc(fc) keeps the default
  821. * assignment of MGMT TID
  822. */
  823. if (ieee80211_is_data_qos(fc) && !ieee80211_is_qos_nullfunc(fc)) {
  824. u8 *qc = NULL;
  825. qc = ieee80211_get_qos_ctl(hdr);
  826. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  827. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  828. goto drop_unlock_sta;
  829. is_ampdu = info->flags & IEEE80211_TX_CTL_AMPDU;
  830. if (WARN_ON_ONCE(is_ampdu &&
  831. mvmsta->tid_data[tid].state != IWL_AGG_ON))
  832. goto drop_unlock_sta;
  833. seq_number = mvmsta->tid_data[tid].seq_number;
  834. seq_number &= IEEE80211_SCTL_SEQ;
  835. if (!iwl_mvm_has_new_tx_api(mvm)) {
  836. struct iwl_tx_cmd *tx_cmd = (void *)dev_cmd->payload;
  837. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  838. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  839. /* update the tx_cmd hdr as it was already copied */
  840. tx_cmd->hdr->seq_ctrl = hdr->seq_ctrl;
  841. }
  842. }
  843. txq_id = mvmsta->tid_data[tid].txq_id;
  844. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
  845. /* Check if TXQ needs to be allocated or re-activated */
  846. if (unlikely(txq_id == IWL_MVM_INVALID_QUEUE ||
  847. !mvmsta->tid_data[tid].is_tid_active)) {
  848. /* If TXQ needs to be allocated... */
  849. if (txq_id == IWL_MVM_INVALID_QUEUE) {
  850. iwl_mvm_tx_add_stream(mvm, mvmsta, tid, skb);
  851. /*
  852. * The frame is now deferred, and the worker scheduled
  853. * will re-allocate it, so we can free it for now.
  854. */
  855. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  856. spin_unlock(&mvmsta->lock);
  857. return 0;
  858. }
  859. /* queue should always be active in new TX path */
  860. WARN_ON(iwl_mvm_has_new_tx_api(mvm));
  861. /* If we are here - TXQ exists and needs to be re-activated */
  862. spin_lock(&mvm->queue_info_lock);
  863. mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
  864. mvmsta->tid_data[tid].is_tid_active = true;
  865. spin_unlock(&mvm->queue_info_lock);
  866. IWL_DEBUG_TX_QUEUES(mvm, "Re-activating queue %d for TX\n",
  867. txq_id);
  868. }
  869. if (!iwl_mvm_has_new_tx_api(mvm)) {
  870. /* Keep track of the time of the last frame for this RA/TID */
  871. mvm->queue_info[txq_id].last_frame_time[tid] = jiffies;
  872. /*
  873. * If we have timed-out TIDs - schedule the worker that will
  874. * reconfig the queues and update them
  875. *
  876. * Note that the mvm->queue_info_lock isn't being taken here in
  877. * order to not serialize the TX flow. This isn't dangerous
  878. * because scheduling mvm->add_stream_wk can't ruin the state,
  879. * and if we DON'T schedule it due to some race condition then
  880. * next TX we get here we will.
  881. */
  882. if (unlikely(mvm->queue_info[txq_id].status ==
  883. IWL_MVM_QUEUE_SHARED &&
  884. iwl_mvm_txq_should_update(mvm, txq_id)))
  885. schedule_work(&mvm->add_stream_wk);
  886. }
  887. IWL_DEBUG_TX(mvm, "TX to [%d|%d] Q:%d - seq: 0x%x\n", mvmsta->sta_id,
  888. tid, txq_id, IEEE80211_SEQ_TO_SN(seq_number));
  889. /* From now on, we cannot access info->control */
  890. iwl_mvm_skb_prepare_status(skb, dev_cmd);
  891. if (iwl_trans_tx(mvm->trans, skb, dev_cmd, txq_id))
  892. goto drop_unlock_sta;
  893. if (tid < IWL_MAX_TID_COUNT && !ieee80211_has_morefrags(fc))
  894. mvmsta->tid_data[tid].seq_number = seq_number + 0x10;
  895. spin_unlock(&mvmsta->lock);
  896. return 0;
  897. drop_unlock_sta:
  898. iwl_trans_free_tx_cmd(mvm->trans, dev_cmd);
  899. spin_unlock(&mvmsta->lock);
  900. drop:
  901. return -1;
  902. }
  903. int iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
  904. struct ieee80211_sta *sta)
  905. {
  906. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  907. struct ieee80211_tx_info info;
  908. struct sk_buff_head mpdus_skbs;
  909. unsigned int payload_len;
  910. int ret;
  911. if (WARN_ON_ONCE(!mvmsta))
  912. return -1;
  913. if (WARN_ON_ONCE(mvmsta->sta_id == IWL_MVM_INVALID_STA))
  914. return -1;
  915. memcpy(&info, skb->cb, sizeof(info));
  916. if (!skb_is_gso(skb))
  917. return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
  918. payload_len = skb_tail_pointer(skb) - skb_transport_header(skb) -
  919. tcp_hdrlen(skb) + skb->data_len;
  920. if (payload_len <= skb_shinfo(skb)->gso_size)
  921. return iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
  922. __skb_queue_head_init(&mpdus_skbs);
  923. ret = iwl_mvm_tx_tso(mvm, skb, &info, sta, &mpdus_skbs);
  924. if (ret)
  925. return ret;
  926. if (WARN_ON(skb_queue_empty(&mpdus_skbs)))
  927. return ret;
  928. while (!skb_queue_empty(&mpdus_skbs)) {
  929. skb = __skb_dequeue(&mpdus_skbs);
  930. ret = iwl_mvm_tx_mpdu(mvm, skb, &info, sta);
  931. if (ret) {
  932. __skb_queue_purge(&mpdus_skbs);
  933. return ret;
  934. }
  935. }
  936. return 0;
  937. }
  938. static void iwl_mvm_check_ratid_empty(struct iwl_mvm *mvm,
  939. struct ieee80211_sta *sta, u8 tid)
  940. {
  941. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  942. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  943. struct ieee80211_vif *vif = mvmsta->vif;
  944. u16 normalized_ssn;
  945. lockdep_assert_held(&mvmsta->lock);
  946. if ((tid_data->state == IWL_AGG_ON ||
  947. tid_data->state == IWL_EMPTYING_HW_QUEUE_DELBA) &&
  948. iwl_mvm_tid_queued(mvm, tid_data) == 0) {
  949. /*
  950. * Now that this aggregation or DQA queue is empty tell
  951. * mac80211 so it knows we no longer have frames buffered for
  952. * the station on this TID (for the TIM bitmap calculation.)
  953. */
  954. ieee80211_sta_set_buffered(sta, tid, false);
  955. }
  956. /*
  957. * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
  958. * to align the wrap around of ssn so we compare relevant values.
  959. */
  960. normalized_ssn = tid_data->ssn;
  961. if (mvm->trans->cfg->gen2)
  962. normalized_ssn &= 0xff;
  963. if (normalized_ssn != tid_data->next_reclaimed)
  964. return;
  965. switch (tid_data->state) {
  966. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  967. IWL_DEBUG_TX_QUEUES(mvm,
  968. "Can continue addBA flow ssn = next_recl = %d\n",
  969. tid_data->next_reclaimed);
  970. tid_data->state = IWL_AGG_STARTING;
  971. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  972. break;
  973. case IWL_EMPTYING_HW_QUEUE_DELBA:
  974. IWL_DEBUG_TX_QUEUES(mvm,
  975. "Can continue DELBA flow ssn = next_recl = %d\n",
  976. tid_data->next_reclaimed);
  977. tid_data->state = IWL_AGG_OFF;
  978. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  979. break;
  980. default:
  981. break;
  982. }
  983. }
  984. #ifdef CONFIG_IWLWIFI_DEBUG
  985. const char *iwl_mvm_get_tx_fail_reason(u32 status)
  986. {
  987. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  988. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  989. switch (status & TX_STATUS_MSK) {
  990. case TX_STATUS_SUCCESS:
  991. return "SUCCESS";
  992. TX_STATUS_POSTPONE(DELAY);
  993. TX_STATUS_POSTPONE(FEW_BYTES);
  994. TX_STATUS_POSTPONE(BT_PRIO);
  995. TX_STATUS_POSTPONE(QUIET_PERIOD);
  996. TX_STATUS_POSTPONE(CALC_TTAK);
  997. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  998. TX_STATUS_FAIL(SHORT_LIMIT);
  999. TX_STATUS_FAIL(LONG_LIMIT);
  1000. TX_STATUS_FAIL(UNDERRUN);
  1001. TX_STATUS_FAIL(DRAIN_FLOW);
  1002. TX_STATUS_FAIL(RFKILL_FLUSH);
  1003. TX_STATUS_FAIL(LIFE_EXPIRE);
  1004. TX_STATUS_FAIL(DEST_PS);
  1005. TX_STATUS_FAIL(HOST_ABORTED);
  1006. TX_STATUS_FAIL(BT_RETRY);
  1007. TX_STATUS_FAIL(STA_INVALID);
  1008. TX_STATUS_FAIL(FRAG_DROPPED);
  1009. TX_STATUS_FAIL(TID_DISABLE);
  1010. TX_STATUS_FAIL(FIFO_FLUSHED);
  1011. TX_STATUS_FAIL(SMALL_CF_POLL);
  1012. TX_STATUS_FAIL(FW_DROP);
  1013. TX_STATUS_FAIL(STA_COLOR_MISMATCH);
  1014. }
  1015. return "UNKNOWN";
  1016. #undef TX_STATUS_FAIL
  1017. #undef TX_STATUS_POSTPONE
  1018. }
  1019. #endif /* CONFIG_IWLWIFI_DEBUG */
  1020. void iwl_mvm_hwrate_to_tx_rate(u32 rate_n_flags,
  1021. enum nl80211_band band,
  1022. struct ieee80211_tx_rate *r)
  1023. {
  1024. if (rate_n_flags & RATE_HT_MCS_GF_MSK)
  1025. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  1026. switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
  1027. case RATE_MCS_CHAN_WIDTH_20:
  1028. break;
  1029. case RATE_MCS_CHAN_WIDTH_40:
  1030. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1031. break;
  1032. case RATE_MCS_CHAN_WIDTH_80:
  1033. r->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
  1034. break;
  1035. case RATE_MCS_CHAN_WIDTH_160:
  1036. r->flags |= IEEE80211_TX_RC_160_MHZ_WIDTH;
  1037. break;
  1038. }
  1039. if (rate_n_flags & RATE_MCS_SGI_MSK)
  1040. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  1041. if (rate_n_flags & RATE_MCS_HT_MSK) {
  1042. r->flags |= IEEE80211_TX_RC_MCS;
  1043. r->idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
  1044. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  1045. ieee80211_rate_set_vht(
  1046. r, rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK,
  1047. ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
  1048. RATE_VHT_MCS_NSS_POS) + 1);
  1049. r->flags |= IEEE80211_TX_RC_VHT_MCS;
  1050. } else {
  1051. r->idx = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
  1052. band);
  1053. }
  1054. }
  1055. /**
  1056. * translate ucode response to mac80211 tx status control values
  1057. */
  1058. static void iwl_mvm_hwrate_to_tx_status(u32 rate_n_flags,
  1059. struct ieee80211_tx_info *info)
  1060. {
  1061. struct ieee80211_tx_rate *r = &info->status.rates[0];
  1062. info->status.antenna =
  1063. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  1064. iwl_mvm_hwrate_to_tx_rate(rate_n_flags, info->band, r);
  1065. }
  1066. static void iwl_mvm_tx_status_check_trigger(struct iwl_mvm *mvm,
  1067. u32 status)
  1068. {
  1069. struct iwl_fw_dbg_trigger_tlv *trig;
  1070. struct iwl_fw_dbg_trigger_tx_status *status_trig;
  1071. int i;
  1072. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TX_STATUS))
  1073. return;
  1074. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TX_STATUS);
  1075. status_trig = (void *)trig->data;
  1076. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt, NULL, trig))
  1077. return;
  1078. for (i = 0; i < ARRAY_SIZE(status_trig->statuses); i++) {
  1079. /* don't collect on status 0 */
  1080. if (!status_trig->statuses[i].status)
  1081. break;
  1082. if (status_trig->statuses[i].status != (status & TX_STATUS_MSK))
  1083. continue;
  1084. iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
  1085. "Tx status %d was received",
  1086. status & TX_STATUS_MSK);
  1087. break;
  1088. }
  1089. }
  1090. /**
  1091. * iwl_mvm_get_scd_ssn - returns the SSN of the SCD
  1092. * @tx_resp: the Tx response from the fw (agg or non-agg)
  1093. *
  1094. * When the fw sends an AMPDU, it fetches the MPDUs one after the other. Since
  1095. * it can't know that everything will go well until the end of the AMPDU, it
  1096. * can't know in advance the number of MPDUs that will be sent in the current
  1097. * batch. This is why it writes the agg Tx response while it fetches the MPDUs.
  1098. * Hence, it can't know in advance what the SSN of the SCD will be at the end
  1099. * of the batch. This is why the SSN of the SCD is written at the end of the
  1100. * whole struct at a variable offset. This function knows how to cope with the
  1101. * variable offset and returns the SSN of the SCD.
  1102. */
  1103. static inline u32 iwl_mvm_get_scd_ssn(struct iwl_mvm *mvm,
  1104. struct iwl_mvm_tx_resp *tx_resp)
  1105. {
  1106. return le32_to_cpup((__le32 *)iwl_mvm_get_agg_status(mvm, tx_resp) +
  1107. tx_resp->frame_count) & 0xfff;
  1108. }
  1109. static void iwl_mvm_rx_tx_cmd_single(struct iwl_mvm *mvm,
  1110. struct iwl_rx_packet *pkt)
  1111. {
  1112. struct ieee80211_sta *sta;
  1113. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1114. int txq_id = SEQ_TO_QUEUE(sequence);
  1115. /* struct iwl_mvm_tx_resp_v3 is almost the same */
  1116. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1117. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  1118. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  1119. struct agg_tx_status *agg_status =
  1120. iwl_mvm_get_agg_status(mvm, tx_resp);
  1121. u32 status = le16_to_cpu(agg_status->status);
  1122. u16 ssn = iwl_mvm_get_scd_ssn(mvm, tx_resp);
  1123. struct iwl_mvm_sta *mvmsta;
  1124. struct sk_buff_head skbs;
  1125. u8 skb_freed = 0;
  1126. u8 lq_color;
  1127. u16 next_reclaimed, seq_ctl;
  1128. bool is_ndp = false;
  1129. __skb_queue_head_init(&skbs);
  1130. if (iwl_mvm_has_new_tx_api(mvm))
  1131. txq_id = le16_to_cpu(tx_resp->tx_queue);
  1132. seq_ctl = le16_to_cpu(tx_resp->seq_ctl);
  1133. /* we can free until ssn % q.n_bd not inclusive */
  1134. iwl_trans_reclaim(mvm->trans, txq_id, ssn, &skbs);
  1135. while (!skb_queue_empty(&skbs)) {
  1136. struct sk_buff *skb = __skb_dequeue(&skbs);
  1137. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1138. bool flushed = false;
  1139. skb_freed++;
  1140. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  1141. memset(&info->status, 0, sizeof(info->status));
  1142. /* inform mac80211 about what happened with the frame */
  1143. switch (status & TX_STATUS_MSK) {
  1144. case TX_STATUS_SUCCESS:
  1145. case TX_STATUS_DIRECT_DONE:
  1146. info->flags |= IEEE80211_TX_STAT_ACK;
  1147. break;
  1148. case TX_STATUS_FAIL_FIFO_FLUSHED:
  1149. case TX_STATUS_FAIL_DRAIN_FLOW:
  1150. flushed = true;
  1151. break;
  1152. case TX_STATUS_FAIL_DEST_PS:
  1153. /* the FW should have stopped the queue and not
  1154. * return this status
  1155. */
  1156. WARN_ON(1);
  1157. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1158. break;
  1159. default:
  1160. break;
  1161. }
  1162. iwl_mvm_tx_status_check_trigger(mvm, status);
  1163. info->status.rates[0].count = tx_resp->failure_frame + 1;
  1164. iwl_mvm_hwrate_to_tx_status(le32_to_cpu(tx_resp->initial_rate),
  1165. info);
  1166. info->status.status_driver_data[1] =
  1167. (void *)(uintptr_t)le32_to_cpu(tx_resp->initial_rate);
  1168. /* Single frame failure in an AMPDU queue => send BAR */
  1169. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  1170. !(info->flags & IEEE80211_TX_STAT_ACK) &&
  1171. !(info->flags & IEEE80211_TX_STAT_TX_FILTERED) && !flushed)
  1172. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  1173. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  1174. /* W/A FW bug: seq_ctl is wrong when the status isn't success */
  1175. if (status != TX_STATUS_SUCCESS) {
  1176. struct ieee80211_hdr *hdr = (void *)skb->data;
  1177. seq_ctl = le16_to_cpu(hdr->seq_ctrl);
  1178. }
  1179. if (unlikely(!seq_ctl)) {
  1180. struct ieee80211_hdr *hdr = (void *)skb->data;
  1181. /*
  1182. * If it is an NDP, we can't update next_reclaim since
  1183. * its sequence control is 0. Note that for that same
  1184. * reason, NDPs are never sent to A-MPDU'able queues
  1185. * so that we can never have more than one freed frame
  1186. * for a single Tx resonse (see WARN_ON below).
  1187. */
  1188. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  1189. is_ndp = true;
  1190. }
  1191. /*
  1192. * TODO: this is not accurate if we are freeing more than one
  1193. * packet.
  1194. */
  1195. info->status.tx_time =
  1196. le16_to_cpu(tx_resp->wireless_media_time);
  1197. BUILD_BUG_ON(ARRAY_SIZE(info->status.status_driver_data) < 1);
  1198. lq_color = TX_RES_RATE_TABLE_COL_GET(tx_resp->tlc_info);
  1199. info->status.status_driver_data[0] =
  1200. RS_DRV_DATA_PACK(lq_color, tx_resp->reduced_tpc);
  1201. ieee80211_tx_status(mvm->hw, skb);
  1202. }
  1203. /* This is an aggregation queue or might become one, so we use
  1204. * the ssn since: ssn = wifi seq_num % 256.
  1205. * The seq_ctl is the sequence control of the packet to which
  1206. * this Tx response relates. But if there is a hole in the
  1207. * bitmap of the BA we received, this Tx response may allow to
  1208. * reclaim the hole and all the subsequent packets that were
  1209. * already acked. In that case, seq_ctl != ssn, and the next
  1210. * packet to be reclaimed will be ssn and not seq_ctl. In that
  1211. * case, several packets will be reclaimed even if
  1212. * frame_count = 1.
  1213. *
  1214. * The ssn is the index (% 256) of the latest packet that has
  1215. * treated (acked / dropped) + 1.
  1216. */
  1217. next_reclaimed = ssn;
  1218. IWL_DEBUG_TX_REPLY(mvm,
  1219. "TXQ %d status %s (0x%08x)\n",
  1220. txq_id, iwl_mvm_get_tx_fail_reason(status), status);
  1221. IWL_DEBUG_TX_REPLY(mvm,
  1222. "\t\t\t\tinitial_rate 0x%x retries %d, idx=%d ssn=%d next_reclaimed=0x%x seq_ctl=0x%x\n",
  1223. le32_to_cpu(tx_resp->initial_rate),
  1224. tx_resp->failure_frame, SEQ_TO_INDEX(sequence),
  1225. ssn, next_reclaimed, seq_ctl);
  1226. rcu_read_lock();
  1227. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1228. /*
  1229. * sta can't be NULL otherwise it'd mean that the sta has been freed in
  1230. * the firmware while we still have packets for it in the Tx queues.
  1231. */
  1232. if (WARN_ON_ONCE(!sta))
  1233. goto out;
  1234. if (!IS_ERR(sta)) {
  1235. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1236. if (tid != IWL_TID_NON_QOS && tid != IWL_MGMT_TID) {
  1237. struct iwl_mvm_tid_data *tid_data =
  1238. &mvmsta->tid_data[tid];
  1239. bool send_eosp_ndp = false;
  1240. spin_lock_bh(&mvmsta->lock);
  1241. if (!is_ndp) {
  1242. tid_data->next_reclaimed = next_reclaimed;
  1243. IWL_DEBUG_TX_REPLY(mvm,
  1244. "Next reclaimed packet:%d\n",
  1245. next_reclaimed);
  1246. } else {
  1247. IWL_DEBUG_TX_REPLY(mvm,
  1248. "NDP - don't update next_reclaimed\n");
  1249. }
  1250. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  1251. if (mvmsta->sleep_tx_count) {
  1252. mvmsta->sleep_tx_count--;
  1253. if (mvmsta->sleep_tx_count &&
  1254. !iwl_mvm_tid_queued(mvm, tid_data)) {
  1255. /*
  1256. * The number of frames in the queue
  1257. * dropped to 0 even if we sent less
  1258. * frames than we thought we had on the
  1259. * Tx queue.
  1260. * This means we had holes in the BA
  1261. * window that we just filled, ask
  1262. * mac80211 to send EOSP since the
  1263. * firmware won't know how to do that.
  1264. * Send NDP and the firmware will send
  1265. * EOSP notification that will trigger
  1266. * a call to ieee80211_sta_eosp().
  1267. */
  1268. send_eosp_ndp = true;
  1269. }
  1270. }
  1271. spin_unlock_bh(&mvmsta->lock);
  1272. if (send_eosp_ndp) {
  1273. iwl_mvm_sta_modify_sleep_tx_count(mvm, sta,
  1274. IEEE80211_FRAME_RELEASE_UAPSD,
  1275. 1, tid, false, false);
  1276. mvmsta->sleep_tx_count = 0;
  1277. ieee80211_send_eosp_nullfunc(sta, tid);
  1278. }
  1279. }
  1280. if (mvmsta->next_status_eosp) {
  1281. mvmsta->next_status_eosp = false;
  1282. ieee80211_sta_eosp(sta);
  1283. }
  1284. } else {
  1285. mvmsta = NULL;
  1286. }
  1287. out:
  1288. rcu_read_unlock();
  1289. }
  1290. #ifdef CONFIG_IWLWIFI_DEBUG
  1291. #define AGG_TX_STATE_(x) case AGG_TX_STATE_ ## x: return #x
  1292. static const char *iwl_get_agg_tx_status(u16 status)
  1293. {
  1294. switch (status & AGG_TX_STATE_STATUS_MSK) {
  1295. AGG_TX_STATE_(TRANSMITTED);
  1296. AGG_TX_STATE_(UNDERRUN);
  1297. AGG_TX_STATE_(BT_PRIO);
  1298. AGG_TX_STATE_(FEW_BYTES);
  1299. AGG_TX_STATE_(ABORT);
  1300. AGG_TX_STATE_(TX_ON_AIR_DROP);
  1301. AGG_TX_STATE_(LAST_SENT_TRY_CNT);
  1302. AGG_TX_STATE_(LAST_SENT_BT_KILL);
  1303. AGG_TX_STATE_(SCD_QUERY);
  1304. AGG_TX_STATE_(TEST_BAD_CRC32);
  1305. AGG_TX_STATE_(RESPONSE);
  1306. AGG_TX_STATE_(DUMP_TX);
  1307. AGG_TX_STATE_(DELAY_TX);
  1308. }
  1309. return "UNKNOWN";
  1310. }
  1311. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  1312. struct iwl_rx_packet *pkt)
  1313. {
  1314. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1315. struct agg_tx_status *frame_status =
  1316. iwl_mvm_get_agg_status(mvm, tx_resp);
  1317. int i;
  1318. for (i = 0; i < tx_resp->frame_count; i++) {
  1319. u16 fstatus = le16_to_cpu(frame_status[i].status);
  1320. IWL_DEBUG_TX_REPLY(mvm,
  1321. "status %s (0x%04x), try-count (%d) seq (0x%x)\n",
  1322. iwl_get_agg_tx_status(fstatus),
  1323. fstatus & AGG_TX_STATE_STATUS_MSK,
  1324. (fstatus & AGG_TX_STATE_TRY_CNT_MSK) >>
  1325. AGG_TX_STATE_TRY_CNT_POS,
  1326. le16_to_cpu(frame_status[i].sequence));
  1327. }
  1328. }
  1329. #else
  1330. static void iwl_mvm_rx_tx_cmd_agg_dbg(struct iwl_mvm *mvm,
  1331. struct iwl_rx_packet *pkt)
  1332. {}
  1333. #endif /* CONFIG_IWLWIFI_DEBUG */
  1334. static void iwl_mvm_rx_tx_cmd_agg(struct iwl_mvm *mvm,
  1335. struct iwl_rx_packet *pkt)
  1336. {
  1337. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1338. int sta_id = IWL_MVM_TX_RES_GET_RA(tx_resp->ra_tid);
  1339. int tid = IWL_MVM_TX_RES_GET_TID(tx_resp->ra_tid);
  1340. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1341. struct iwl_mvm_sta *mvmsta;
  1342. int queue = SEQ_TO_QUEUE(sequence);
  1343. if (WARN_ON_ONCE(queue < IWL_MVM_DQA_MIN_DATA_QUEUE &&
  1344. (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)))
  1345. return;
  1346. if (WARN_ON_ONCE(tid == IWL_TID_NON_QOS))
  1347. return;
  1348. iwl_mvm_rx_tx_cmd_agg_dbg(mvm, pkt);
  1349. rcu_read_lock();
  1350. mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
  1351. if (!WARN_ON_ONCE(!mvmsta)) {
  1352. mvmsta->tid_data[tid].rate_n_flags =
  1353. le32_to_cpu(tx_resp->initial_rate);
  1354. mvmsta->tid_data[tid].tx_time =
  1355. le16_to_cpu(tx_resp->wireless_media_time);
  1356. mvmsta->tid_data[tid].lq_color =
  1357. TX_RES_RATE_TABLE_COL_GET(tx_resp->tlc_info);
  1358. }
  1359. rcu_read_unlock();
  1360. }
  1361. void iwl_mvm_rx_tx_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  1362. {
  1363. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1364. struct iwl_mvm_tx_resp *tx_resp = (void *)pkt->data;
  1365. if (tx_resp->frame_count == 1)
  1366. iwl_mvm_rx_tx_cmd_single(mvm, pkt);
  1367. else
  1368. iwl_mvm_rx_tx_cmd_agg(mvm, pkt);
  1369. }
  1370. static void iwl_mvm_tx_reclaim(struct iwl_mvm *mvm, int sta_id, int tid,
  1371. int txq, int index,
  1372. struct ieee80211_tx_info *ba_info, u32 rate)
  1373. {
  1374. struct sk_buff_head reclaimed_skbs;
  1375. struct iwl_mvm_tid_data *tid_data;
  1376. struct ieee80211_sta *sta;
  1377. struct iwl_mvm_sta *mvmsta;
  1378. struct sk_buff *skb;
  1379. int freed;
  1380. if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT ||
  1381. tid > IWL_MAX_TID_COUNT,
  1382. "sta_id %d tid %d", sta_id, tid))
  1383. return;
  1384. rcu_read_lock();
  1385. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1386. /* Reclaiming frames for a station that has been deleted ? */
  1387. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  1388. rcu_read_unlock();
  1389. return;
  1390. }
  1391. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1392. tid_data = &mvmsta->tid_data[tid];
  1393. if (tid_data->txq_id != txq) {
  1394. IWL_ERR(mvm,
  1395. "invalid BA notification: Q %d, tid %d\n",
  1396. tid_data->txq_id, tid);
  1397. rcu_read_unlock();
  1398. return;
  1399. }
  1400. spin_lock_bh(&mvmsta->lock);
  1401. __skb_queue_head_init(&reclaimed_skbs);
  1402. /*
  1403. * Release all TFDs before the SSN, i.e. all TFDs in front of
  1404. * block-ack window (we assume that they've been successfully
  1405. * transmitted ... if not, it's too late anyway).
  1406. */
  1407. iwl_trans_reclaim(mvm->trans, txq, index, &reclaimed_skbs);
  1408. tid_data->next_reclaimed = index;
  1409. iwl_mvm_check_ratid_empty(mvm, sta, tid);
  1410. freed = 0;
  1411. /* pack lq color from tid_data along the reduced txp */
  1412. ba_info->status.status_driver_data[0] =
  1413. RS_DRV_DATA_PACK(tid_data->lq_color,
  1414. ba_info->status.status_driver_data[0]);
  1415. ba_info->status.status_driver_data[1] = (void *)(uintptr_t)rate;
  1416. skb_queue_walk(&reclaimed_skbs, skb) {
  1417. struct ieee80211_hdr *hdr = (void *)skb->data;
  1418. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1419. if (ieee80211_is_data_qos(hdr->frame_control))
  1420. freed++;
  1421. else
  1422. WARN_ON_ONCE(tid != IWL_MAX_TID_COUNT);
  1423. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  1424. memset(&info->status, 0, sizeof(info->status));
  1425. /* Packet was transmitted successfully, failures come as single
  1426. * frames because before failing a frame the firmware transmits
  1427. * it without aggregation at least once.
  1428. */
  1429. info->flags |= IEEE80211_TX_STAT_ACK;
  1430. /* this is the first skb we deliver in this batch */
  1431. /* put the rate scaling data there */
  1432. if (freed == 1) {
  1433. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1434. memcpy(&info->status, &ba_info->status,
  1435. sizeof(ba_info->status));
  1436. iwl_mvm_hwrate_to_tx_status(rate, info);
  1437. }
  1438. }
  1439. spin_unlock_bh(&mvmsta->lock);
  1440. /* We got a BA notif with 0 acked or scd_ssn didn't progress which is
  1441. * possible (i.e. first MPDU in the aggregation wasn't acked)
  1442. * Still it's important to update RS about sent vs. acked.
  1443. */
  1444. if (skb_queue_empty(&reclaimed_skbs)) {
  1445. struct ieee80211_chanctx_conf *chanctx_conf = NULL;
  1446. if (mvmsta->vif)
  1447. chanctx_conf =
  1448. rcu_dereference(mvmsta->vif->chanctx_conf);
  1449. if (WARN_ON_ONCE(!chanctx_conf))
  1450. goto out;
  1451. ba_info->band = chanctx_conf->def.chan->band;
  1452. iwl_mvm_hwrate_to_tx_status(rate, ba_info);
  1453. if (!iwl_mvm_has_tlc_offload(mvm)) {
  1454. IWL_DEBUG_TX_REPLY(mvm,
  1455. "No reclaim. Update rs directly\n");
  1456. iwl_mvm_rs_tx_status(mvm, sta, tid, ba_info, false);
  1457. }
  1458. }
  1459. out:
  1460. rcu_read_unlock();
  1461. while (!skb_queue_empty(&reclaimed_skbs)) {
  1462. skb = __skb_dequeue(&reclaimed_skbs);
  1463. ieee80211_tx_status(mvm->hw, skb);
  1464. }
  1465. }
  1466. void iwl_mvm_rx_ba_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  1467. {
  1468. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1469. int sta_id, tid, txq, index;
  1470. struct ieee80211_tx_info ba_info = {};
  1471. struct iwl_mvm_ba_notif *ba_notif;
  1472. struct iwl_mvm_tid_data *tid_data;
  1473. struct iwl_mvm_sta *mvmsta;
  1474. ba_info.flags = IEEE80211_TX_STAT_AMPDU;
  1475. if (iwl_mvm_has_new_tx_api(mvm)) {
  1476. struct iwl_mvm_compressed_ba_notif *ba_res =
  1477. (void *)pkt->data;
  1478. u8 lq_color = TX_RES_RATE_TABLE_COL_GET(ba_res->tlc_rate_info);
  1479. int i;
  1480. sta_id = ba_res->sta_id;
  1481. ba_info.status.ampdu_ack_len = (u8)le16_to_cpu(ba_res->done);
  1482. ba_info.status.ampdu_len = (u8)le16_to_cpu(ba_res->txed);
  1483. ba_info.status.tx_time =
  1484. (u16)le32_to_cpu(ba_res->wireless_time);
  1485. ba_info.status.status_driver_data[0] =
  1486. (void *)(uintptr_t)ba_res->reduced_txp;
  1487. if (!le16_to_cpu(ba_res->tfd_cnt))
  1488. goto out;
  1489. rcu_read_lock();
  1490. mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
  1491. if (!mvmsta)
  1492. goto out_unlock;
  1493. /* Free per TID */
  1494. for (i = 0; i < le16_to_cpu(ba_res->tfd_cnt); i++) {
  1495. struct iwl_mvm_compressed_ba_tfd *ba_tfd =
  1496. &ba_res->tfd[i];
  1497. tid = ba_tfd->tid;
  1498. if (tid == IWL_MGMT_TID)
  1499. tid = IWL_MAX_TID_COUNT;
  1500. mvmsta->tid_data[i].lq_color = lq_color;
  1501. iwl_mvm_tx_reclaim(mvm, sta_id, tid,
  1502. (int)(le16_to_cpu(ba_tfd->q_num)),
  1503. le16_to_cpu(ba_tfd->tfd_index),
  1504. &ba_info,
  1505. le32_to_cpu(ba_res->tx_rate));
  1506. }
  1507. out_unlock:
  1508. rcu_read_unlock();
  1509. out:
  1510. IWL_DEBUG_TX_REPLY(mvm,
  1511. "BA_NOTIFICATION Received from sta_id = %d, flags %x, sent:%d, acked:%d\n",
  1512. sta_id, le32_to_cpu(ba_res->flags),
  1513. le16_to_cpu(ba_res->txed),
  1514. le16_to_cpu(ba_res->done));
  1515. return;
  1516. }
  1517. ba_notif = (void *)pkt->data;
  1518. sta_id = ba_notif->sta_id;
  1519. tid = ba_notif->tid;
  1520. /* "flow" corresponds to Tx queue */
  1521. txq = le16_to_cpu(ba_notif->scd_flow);
  1522. /* "ssn" is start of block-ack Tx window, corresponds to index
  1523. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1524. index = le16_to_cpu(ba_notif->scd_ssn);
  1525. rcu_read_lock();
  1526. mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, sta_id);
  1527. if (WARN_ON_ONCE(!mvmsta)) {
  1528. rcu_read_unlock();
  1529. return;
  1530. }
  1531. tid_data = &mvmsta->tid_data[tid];
  1532. ba_info.status.ampdu_ack_len = ba_notif->txed_2_done;
  1533. ba_info.status.ampdu_len = ba_notif->txed;
  1534. ba_info.status.tx_time = tid_data->tx_time;
  1535. ba_info.status.status_driver_data[0] =
  1536. (void *)(uintptr_t)ba_notif->reduced_txp;
  1537. rcu_read_unlock();
  1538. iwl_mvm_tx_reclaim(mvm, sta_id, tid, txq, index, &ba_info,
  1539. tid_data->rate_n_flags);
  1540. IWL_DEBUG_TX_REPLY(mvm,
  1541. "BA_NOTIFICATION Received from %pM, sta_id = %d\n",
  1542. ba_notif->sta_addr, ba_notif->sta_id);
  1543. IWL_DEBUG_TX_REPLY(mvm,
  1544. "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = %d, scd_ssn = %d sent:%d, acked:%d\n",
  1545. ba_notif->tid, le16_to_cpu(ba_notif->seq_ctl),
  1546. le64_to_cpu(ba_notif->bitmap), txq, index,
  1547. ba_notif->txed, ba_notif->txed_2_done);
  1548. IWL_DEBUG_TX_REPLY(mvm, "reduced txp from ba notif %d\n",
  1549. ba_notif->reduced_txp);
  1550. }
  1551. /*
  1552. * Note that there are transports that buffer frames before they reach
  1553. * the firmware. This means that after flush_tx_path is called, the
  1554. * queue might not be empty. The race-free way to handle this is to:
  1555. * 1) set the station as draining
  1556. * 2) flush the Tx path
  1557. * 3) wait for the transport queues to be empty
  1558. */
  1559. int iwl_mvm_flush_tx_path(struct iwl_mvm *mvm, u32 tfd_msk, u32 flags)
  1560. {
  1561. int ret;
  1562. struct iwl_tx_path_flush_cmd_v1 flush_cmd = {
  1563. .queues_ctl = cpu_to_le32(tfd_msk),
  1564. .flush_ctl = cpu_to_le16(DUMP_TX_FIFO_FLUSH),
  1565. };
  1566. WARN_ON(iwl_mvm_has_new_tx_api(mvm));
  1567. ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
  1568. sizeof(flush_cmd), &flush_cmd);
  1569. if (ret)
  1570. IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
  1571. return ret;
  1572. }
  1573. int iwl_mvm_flush_sta_tids(struct iwl_mvm *mvm, u32 sta_id,
  1574. u16 tids, u32 flags)
  1575. {
  1576. int ret;
  1577. struct iwl_tx_path_flush_cmd flush_cmd = {
  1578. .sta_id = cpu_to_le32(sta_id),
  1579. .tid_mask = cpu_to_le16(tids),
  1580. };
  1581. WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
  1582. ret = iwl_mvm_send_cmd_pdu(mvm, TXPATH_FLUSH, flags,
  1583. sizeof(flush_cmd), &flush_cmd);
  1584. if (ret)
  1585. IWL_ERR(mvm, "Failed to send flush command (%d)\n", ret);
  1586. return ret;
  1587. }
  1588. int iwl_mvm_flush_sta(struct iwl_mvm *mvm, void *sta, bool internal, u32 flags)
  1589. {
  1590. struct iwl_mvm_int_sta *int_sta = sta;
  1591. struct iwl_mvm_sta *mvm_sta = sta;
  1592. BUILD_BUG_ON(offsetof(struct iwl_mvm_int_sta, sta_id) !=
  1593. offsetof(struct iwl_mvm_sta, sta_id));
  1594. if (iwl_mvm_has_new_tx_api(mvm))
  1595. return iwl_mvm_flush_sta_tids(mvm, mvm_sta->sta_id,
  1596. 0xff | BIT(IWL_MGMT_TID), flags);
  1597. if (internal)
  1598. return iwl_mvm_flush_tx_path(mvm, int_sta->tfd_queue_msk,
  1599. flags);
  1600. return iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, flags);
  1601. }