tx.c 54 KB

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