tx.c 58 KB

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