tx.c 57 KB

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