tx.c 55 KB

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