tx.c 55 KB

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