rxmq.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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) 2015 - 2017 Intel Deutschland GmbH
  11. * Copyright(c) 2018 Intel Corporation
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of version 2 of the GNU General Public License as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * The full GNU General Public License is included in this distribution
  23. * in the file called COPYING.
  24. *
  25. * Contact Information:
  26. * Intel Linux Wireless <ilw@linux.intel.com>
  27. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  28. *
  29. * BSD LICENSE
  30. *
  31. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  32. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  33. * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  34. * Copyright(c) 2018 Intel Corporation
  35. * All rights reserved.
  36. *
  37. * Redistribution and use in source and binary forms, with or without
  38. * modification, are permitted provided that the following conditions
  39. * are met:
  40. *
  41. * * Redistributions of source code must retain the above copyright
  42. * notice, this list of conditions and the following disclaimer.
  43. * * Redistributions in binary form must reproduce the above copyright
  44. * notice, this list of conditions and the following disclaimer in
  45. * the documentation and/or other materials provided with the
  46. * distribution.
  47. * * Neither the name Intel Corporation nor the names of its
  48. * contributors may be used to endorse or promote products derived
  49. * from this software without specific prior written permission.
  50. *
  51. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  52. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  53. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  54. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  55. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  56. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  57. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  58. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  59. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  60. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  61. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  62. *****************************************************************************/
  63. #include <linux/etherdevice.h>
  64. #include <linux/skbuff.h>
  65. #include "iwl-trans.h"
  66. #include "mvm.h"
  67. #include "fw-api.h"
  68. static inline int iwl_mvm_check_pn(struct iwl_mvm *mvm, struct sk_buff *skb,
  69. int queue, struct ieee80211_sta *sta)
  70. {
  71. struct iwl_mvm_sta *mvmsta;
  72. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  73. struct ieee80211_rx_status *stats = IEEE80211_SKB_RXCB(skb);
  74. struct iwl_mvm_key_pn *ptk_pn;
  75. int res;
  76. u8 tid, keyidx;
  77. u8 pn[IEEE80211_CCMP_PN_LEN];
  78. u8 *extiv;
  79. /* do PN checking */
  80. /* multicast and non-data only arrives on default queue */
  81. if (!ieee80211_is_data(hdr->frame_control) ||
  82. is_multicast_ether_addr(hdr->addr1))
  83. return 0;
  84. /* do not check PN for open AP */
  85. if (!(stats->flag & RX_FLAG_DECRYPTED))
  86. return 0;
  87. /*
  88. * avoid checking for default queue - we don't want to replicate
  89. * all the logic that's necessary for checking the PN on fragmented
  90. * frames, leave that to mac80211
  91. */
  92. if (queue == 0)
  93. return 0;
  94. /* if we are here - this for sure is either CCMP or GCMP */
  95. if (IS_ERR_OR_NULL(sta)) {
  96. IWL_ERR(mvm,
  97. "expected hw-decrypted unicast frame for station\n");
  98. return -1;
  99. }
  100. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  101. extiv = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control);
  102. keyidx = extiv[3] >> 6;
  103. ptk_pn = rcu_dereference(mvmsta->ptk_pn[keyidx]);
  104. if (!ptk_pn)
  105. return -1;
  106. if (ieee80211_is_data_qos(hdr->frame_control))
  107. tid = ieee80211_get_tid(hdr);
  108. else
  109. tid = 0;
  110. /* we don't use HCCA/802.11 QoS TSPECs, so drop such frames */
  111. if (tid >= IWL_MAX_TID_COUNT)
  112. return -1;
  113. /* load pn */
  114. pn[0] = extiv[7];
  115. pn[1] = extiv[6];
  116. pn[2] = extiv[5];
  117. pn[3] = extiv[4];
  118. pn[4] = extiv[1];
  119. pn[5] = extiv[0];
  120. res = memcmp(pn, ptk_pn->q[queue].pn[tid], IEEE80211_CCMP_PN_LEN);
  121. if (res < 0)
  122. return -1;
  123. if (!res && !(stats->flag & RX_FLAG_ALLOW_SAME_PN))
  124. return -1;
  125. memcpy(ptk_pn->q[queue].pn[tid], pn, IEEE80211_CCMP_PN_LEN);
  126. stats->flag |= RX_FLAG_PN_VALIDATED;
  127. return 0;
  128. }
  129. /* iwl_mvm_create_skb Adds the rxb to a new skb */
  130. static void iwl_mvm_create_skb(struct sk_buff *skb, struct ieee80211_hdr *hdr,
  131. u16 len, u8 crypt_len,
  132. struct iwl_rx_cmd_buffer *rxb)
  133. {
  134. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  135. struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
  136. unsigned int headlen, fraglen, pad_len = 0;
  137. unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  138. if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
  139. len -= 2;
  140. pad_len = 2;
  141. }
  142. /* If frame is small enough to fit in skb->head, pull it completely.
  143. * If not, only pull ieee80211_hdr (including crypto if present, and
  144. * an additional 8 bytes for SNAP/ethertype, see below) so that
  145. * splice() or TCP coalesce are more efficient.
  146. *
  147. * Since, in addition, ieee80211_data_to_8023() always pull in at
  148. * least 8 bytes (possibly more for mesh) we can do the same here
  149. * to save the cost of doing it later. That still doesn't pull in
  150. * the actual IP header since the typical case has a SNAP header.
  151. * If the latter changes (there are efforts in the standards group
  152. * to do so) we should revisit this and ieee80211_data_to_8023().
  153. */
  154. headlen = (len <= skb_tailroom(skb)) ? len :
  155. hdrlen + crypt_len + 8;
  156. /* The firmware may align the packet to DWORD.
  157. * The padding is inserted after the IV.
  158. * After copying the header + IV skip the padding if
  159. * present before copying packet data.
  160. */
  161. hdrlen += crypt_len;
  162. skb_put_data(skb, hdr, hdrlen);
  163. skb_put_data(skb, (u8 *)hdr + hdrlen + pad_len, headlen - hdrlen);
  164. fraglen = len - headlen;
  165. if (fraglen) {
  166. int offset = (void *)hdr + headlen + pad_len -
  167. rxb_addr(rxb) + rxb_offset(rxb);
  168. skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
  169. fraglen, rxb->truesize);
  170. }
  171. }
  172. /* iwl_mvm_pass_packet_to_mac80211 - passes the packet for mac80211 */
  173. static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
  174. struct napi_struct *napi,
  175. struct sk_buff *skb, int queue,
  176. struct ieee80211_sta *sta)
  177. {
  178. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  179. if (iwl_mvm_check_pn(mvm, skb, queue, sta)) {
  180. kfree_skb(skb);
  181. } else {
  182. unsigned int radiotap_len = 0;
  183. if (rx_status->flag & RX_FLAG_RADIOTAP_HE)
  184. radiotap_len += sizeof(struct ieee80211_radiotap_he);
  185. if (rx_status->flag & RX_FLAG_RADIOTAP_HE_MU)
  186. radiotap_len += sizeof(struct ieee80211_radiotap_he_mu);
  187. __skb_push(skb, radiotap_len);
  188. ieee80211_rx_napi(mvm->hw, sta, skb, napi);
  189. }
  190. }
  191. static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
  192. struct ieee80211_rx_status *rx_status,
  193. u32 rate_n_flags, int energy_a,
  194. int energy_b)
  195. {
  196. int max_energy;
  197. u32 rate_flags = rate_n_flags;
  198. energy_a = energy_a ? -energy_a : S8_MIN;
  199. energy_b = energy_b ? -energy_b : S8_MIN;
  200. max_energy = max(energy_a, energy_b);
  201. IWL_DEBUG_STATS(mvm, "energy In A %d B %d, and max %d\n",
  202. energy_a, energy_b, max_energy);
  203. rx_status->signal = max_energy;
  204. rx_status->chains =
  205. (rate_flags & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS;
  206. rx_status->chain_signal[0] = energy_a;
  207. rx_status->chain_signal[1] = energy_b;
  208. rx_status->chain_signal[2] = S8_MIN;
  209. }
  210. static int iwl_mvm_rx_crypto(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
  211. struct ieee80211_rx_status *stats, u16 phy_info,
  212. struct iwl_rx_mpdu_desc *desc,
  213. u32 pkt_flags, int queue, u8 *crypt_len)
  214. {
  215. u16 status = le16_to_cpu(desc->status);
  216. /*
  217. * Drop UNKNOWN frames in aggregation, unless in monitor mode
  218. * (where we don't have the keys).
  219. * We limit this to aggregation because in TKIP this is a valid
  220. * scenario, since we may not have the (correct) TTAK (phase 1
  221. * key) in the firmware.
  222. */
  223. if (phy_info & IWL_RX_MPDU_PHY_AMPDU &&
  224. (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
  225. IWL_RX_MPDU_STATUS_SEC_UNKNOWN && !mvm->monitor_on)
  226. return -1;
  227. if (!ieee80211_has_protected(hdr->frame_control) ||
  228. (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
  229. IWL_RX_MPDU_STATUS_SEC_NONE)
  230. return 0;
  231. /* TODO: handle packets encrypted with unknown alg */
  232. switch (status & IWL_RX_MPDU_STATUS_SEC_MASK) {
  233. case IWL_RX_MPDU_STATUS_SEC_CCM:
  234. case IWL_RX_MPDU_STATUS_SEC_GCM:
  235. BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN != IEEE80211_GCMP_PN_LEN);
  236. /* alg is CCM: check MIC only */
  237. if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
  238. return -1;
  239. stats->flag |= RX_FLAG_DECRYPTED;
  240. if (pkt_flags & FH_RSCSR_RADA_EN)
  241. stats->flag |= RX_FLAG_MIC_STRIPPED;
  242. *crypt_len = IEEE80211_CCMP_HDR_LEN;
  243. return 0;
  244. case IWL_RX_MPDU_STATUS_SEC_TKIP:
  245. /* Don't drop the frame and decrypt it in SW */
  246. if (!fw_has_api(&mvm->fw->ucode_capa,
  247. IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
  248. !(status & IWL_RX_MPDU_RES_STATUS_TTAK_OK))
  249. return 0;
  250. *crypt_len = IEEE80211_TKIP_IV_LEN;
  251. /* fall through if TTAK OK */
  252. case IWL_RX_MPDU_STATUS_SEC_WEP:
  253. if (!(status & IWL_RX_MPDU_STATUS_ICV_OK))
  254. return -1;
  255. stats->flag |= RX_FLAG_DECRYPTED;
  256. if ((status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
  257. IWL_RX_MPDU_STATUS_SEC_WEP)
  258. *crypt_len = IEEE80211_WEP_IV_LEN;
  259. if (pkt_flags & FH_RSCSR_RADA_EN)
  260. stats->flag |= RX_FLAG_ICV_STRIPPED;
  261. return 0;
  262. case IWL_RX_MPDU_STATUS_SEC_EXT_ENC:
  263. if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
  264. return -1;
  265. stats->flag |= RX_FLAG_DECRYPTED;
  266. return 0;
  267. default:
  268. /* Expected in monitor (not having the keys) */
  269. if (!mvm->monitor_on)
  270. IWL_ERR(mvm, "Unhandled alg: 0x%x\n", status);
  271. }
  272. return 0;
  273. }
  274. static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
  275. struct sk_buff *skb,
  276. struct iwl_rx_mpdu_desc *desc)
  277. {
  278. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  279. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
  280. u16 flags = le16_to_cpu(desc->l3l4_flags);
  281. u8 l3_prot = (u8)((flags & IWL_RX_L3L4_L3_PROTO_MASK) >>
  282. IWL_RX_L3_PROTO_POS);
  283. if (mvmvif->features & NETIF_F_RXCSUM &&
  284. flags & IWL_RX_L3L4_TCP_UDP_CSUM_OK &&
  285. (flags & IWL_RX_L3L4_IP_HDR_CSUM_OK ||
  286. l3_prot == IWL_RX_L3_TYPE_IPV6 ||
  287. l3_prot == IWL_RX_L3_TYPE_IPV6_FRAG))
  288. skb->ip_summed = CHECKSUM_UNNECESSARY;
  289. }
  290. /*
  291. * returns true if a packet is a duplicate and should be dropped.
  292. * Updates AMSDU PN tracking info
  293. */
  294. static bool iwl_mvm_is_dup(struct ieee80211_sta *sta, int queue,
  295. struct ieee80211_rx_status *rx_status,
  296. struct ieee80211_hdr *hdr,
  297. struct iwl_rx_mpdu_desc *desc)
  298. {
  299. struct iwl_mvm_sta *mvm_sta;
  300. struct iwl_mvm_rxq_dup_data *dup_data;
  301. u8 tid, sub_frame_idx;
  302. if (WARN_ON(IS_ERR_OR_NULL(sta)))
  303. return false;
  304. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  305. dup_data = &mvm_sta->dup_data[queue];
  306. /*
  307. * Drop duplicate 802.11 retransmissions
  308. * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
  309. */
  310. if (ieee80211_is_ctl(hdr->frame_control) ||
  311. ieee80211_is_qos_nullfunc(hdr->frame_control) ||
  312. is_multicast_ether_addr(hdr->addr1)) {
  313. rx_status->flag |= RX_FLAG_DUP_VALIDATED;
  314. return false;
  315. }
  316. if (ieee80211_is_data_qos(hdr->frame_control))
  317. /* frame has qos control */
  318. tid = ieee80211_get_tid(hdr);
  319. else
  320. tid = IWL_MAX_TID_COUNT;
  321. /* If this wasn't a part of an A-MSDU the sub-frame index will be 0 */
  322. sub_frame_idx = desc->amsdu_info &
  323. IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
  324. if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
  325. dup_data->last_seq[tid] == hdr->seq_ctrl &&
  326. dup_data->last_sub_frame[tid] >= sub_frame_idx))
  327. return true;
  328. /* Allow same PN as the first subframe for following sub frames */
  329. if (dup_data->last_seq[tid] == hdr->seq_ctrl &&
  330. sub_frame_idx > dup_data->last_sub_frame[tid] &&
  331. desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU)
  332. rx_status->flag |= RX_FLAG_ALLOW_SAME_PN;
  333. dup_data->last_seq[tid] = hdr->seq_ctrl;
  334. dup_data->last_sub_frame[tid] = sub_frame_idx;
  335. rx_status->flag |= RX_FLAG_DUP_VALIDATED;
  336. return false;
  337. }
  338. int iwl_mvm_notify_rx_queue(struct iwl_mvm *mvm, u32 rxq_mask,
  339. const u8 *data, u32 count)
  340. {
  341. struct iwl_rxq_sync_cmd *cmd;
  342. u32 data_size = sizeof(*cmd) + count;
  343. int ret;
  344. /* should be DWORD aligned */
  345. if (WARN_ON(count & 3 || count > IWL_MULTI_QUEUE_SYNC_MSG_MAX_SIZE))
  346. return -EINVAL;
  347. cmd = kzalloc(data_size, GFP_KERNEL);
  348. if (!cmd)
  349. return -ENOMEM;
  350. cmd->rxq_mask = cpu_to_le32(rxq_mask);
  351. cmd->count = cpu_to_le32(count);
  352. cmd->flags = 0;
  353. memcpy(cmd->payload, data, count);
  354. ret = iwl_mvm_send_cmd_pdu(mvm,
  355. WIDE_ID(DATA_PATH_GROUP,
  356. TRIGGER_RX_QUEUES_NOTIF_CMD),
  357. 0, data_size, cmd);
  358. kfree(cmd);
  359. return ret;
  360. }
  361. /*
  362. * Returns true if sn2 - buffer_size < sn1 < sn2.
  363. * To be used only in order to compare reorder buffer head with NSSN.
  364. * We fully trust NSSN unless it is behind us due to reorder timeout.
  365. * Reorder timeout can only bring us up to buffer_size SNs ahead of NSSN.
  366. */
  367. static bool iwl_mvm_is_sn_less(u16 sn1, u16 sn2, u16 buffer_size)
  368. {
  369. return ieee80211_sn_less(sn1, sn2) &&
  370. !ieee80211_sn_less(sn1, sn2 - buffer_size);
  371. }
  372. #define RX_REORDER_BUF_TIMEOUT_MQ (HZ / 10)
  373. static void iwl_mvm_release_frames(struct iwl_mvm *mvm,
  374. struct ieee80211_sta *sta,
  375. struct napi_struct *napi,
  376. struct iwl_mvm_baid_data *baid_data,
  377. struct iwl_mvm_reorder_buffer *reorder_buf,
  378. u16 nssn)
  379. {
  380. struct iwl_mvm_reorder_buf_entry *entries =
  381. &baid_data->entries[reorder_buf->queue *
  382. baid_data->entries_per_queue];
  383. u16 ssn = reorder_buf->head_sn;
  384. lockdep_assert_held(&reorder_buf->lock);
  385. /* ignore nssn smaller than head sn - this can happen due to timeout */
  386. if (iwl_mvm_is_sn_less(nssn, ssn, reorder_buf->buf_size))
  387. goto set_timer;
  388. while (iwl_mvm_is_sn_less(ssn, nssn, reorder_buf->buf_size)) {
  389. int index = ssn % reorder_buf->buf_size;
  390. struct sk_buff_head *skb_list = &entries[index].e.frames;
  391. struct sk_buff *skb;
  392. ssn = ieee80211_sn_inc(ssn);
  393. /*
  394. * Empty the list. Will have more than one frame for A-MSDU.
  395. * Empty list is valid as well since nssn indicates frames were
  396. * received.
  397. */
  398. while ((skb = __skb_dequeue(skb_list))) {
  399. iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb,
  400. reorder_buf->queue,
  401. sta);
  402. reorder_buf->num_stored--;
  403. }
  404. }
  405. reorder_buf->head_sn = nssn;
  406. set_timer:
  407. if (reorder_buf->num_stored && !reorder_buf->removed) {
  408. u16 index = reorder_buf->head_sn % reorder_buf->buf_size;
  409. while (skb_queue_empty(&entries[index].e.frames))
  410. index = (index + 1) % reorder_buf->buf_size;
  411. /* modify timer to match next frame's expiration time */
  412. mod_timer(&reorder_buf->reorder_timer,
  413. entries[index].e.reorder_time + 1 +
  414. RX_REORDER_BUF_TIMEOUT_MQ);
  415. } else {
  416. del_timer(&reorder_buf->reorder_timer);
  417. }
  418. }
  419. void iwl_mvm_reorder_timer_expired(struct timer_list *t)
  420. {
  421. struct iwl_mvm_reorder_buffer *buf = from_timer(buf, t, reorder_timer);
  422. struct iwl_mvm_baid_data *baid_data =
  423. iwl_mvm_baid_data_from_reorder_buf(buf);
  424. struct iwl_mvm_reorder_buf_entry *entries =
  425. &baid_data->entries[buf->queue * baid_data->entries_per_queue];
  426. int i;
  427. u16 sn = 0, index = 0;
  428. bool expired = false;
  429. bool cont = false;
  430. spin_lock(&buf->lock);
  431. if (!buf->num_stored || buf->removed) {
  432. spin_unlock(&buf->lock);
  433. return;
  434. }
  435. for (i = 0; i < buf->buf_size ; i++) {
  436. index = (buf->head_sn + i) % buf->buf_size;
  437. if (skb_queue_empty(&entries[index].e.frames)) {
  438. /*
  439. * If there is a hole and the next frame didn't expire
  440. * we want to break and not advance SN
  441. */
  442. cont = false;
  443. continue;
  444. }
  445. if (!cont &&
  446. !time_after(jiffies, entries[index].e.reorder_time +
  447. RX_REORDER_BUF_TIMEOUT_MQ))
  448. break;
  449. expired = true;
  450. /* continue until next hole after this expired frames */
  451. cont = true;
  452. sn = ieee80211_sn_add(buf->head_sn, i + 1);
  453. }
  454. if (expired) {
  455. struct ieee80211_sta *sta;
  456. struct iwl_mvm_sta *mvmsta;
  457. u8 sta_id = baid_data->sta_id;
  458. rcu_read_lock();
  459. sta = rcu_dereference(buf->mvm->fw_id_to_mac_id[sta_id]);
  460. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  461. /* SN is set to the last expired frame + 1 */
  462. IWL_DEBUG_HT(buf->mvm,
  463. "Releasing expired frames for sta %u, sn %d\n",
  464. sta_id, sn);
  465. iwl_mvm_event_frame_timeout_callback(buf->mvm, mvmsta->vif,
  466. sta, baid_data->tid);
  467. iwl_mvm_release_frames(buf->mvm, sta, NULL, baid_data, buf, sn);
  468. rcu_read_unlock();
  469. } else {
  470. /*
  471. * If no frame expired and there are stored frames, index is now
  472. * pointing to the first unexpired frame - modify timer
  473. * accordingly to this frame.
  474. */
  475. mod_timer(&buf->reorder_timer,
  476. entries[index].e.reorder_time +
  477. 1 + RX_REORDER_BUF_TIMEOUT_MQ);
  478. }
  479. spin_unlock(&buf->lock);
  480. }
  481. static void iwl_mvm_del_ba(struct iwl_mvm *mvm, int queue,
  482. struct iwl_mvm_delba_data *data)
  483. {
  484. struct iwl_mvm_baid_data *ba_data;
  485. struct ieee80211_sta *sta;
  486. struct iwl_mvm_reorder_buffer *reorder_buf;
  487. u8 baid = data->baid;
  488. if (WARN_ONCE(baid >= IWL_MAX_BAID, "invalid BAID: %x\n", baid))
  489. return;
  490. rcu_read_lock();
  491. ba_data = rcu_dereference(mvm->baid_map[baid]);
  492. if (WARN_ON_ONCE(!ba_data))
  493. goto out;
  494. sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
  495. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
  496. goto out;
  497. reorder_buf = &ba_data->reorder_buf[queue];
  498. /* release all frames that are in the reorder buffer to the stack */
  499. spin_lock_bh(&reorder_buf->lock);
  500. iwl_mvm_release_frames(mvm, sta, NULL, ba_data, reorder_buf,
  501. ieee80211_sn_add(reorder_buf->head_sn,
  502. reorder_buf->buf_size));
  503. spin_unlock_bh(&reorder_buf->lock);
  504. del_timer_sync(&reorder_buf->reorder_timer);
  505. out:
  506. rcu_read_unlock();
  507. }
  508. void iwl_mvm_rx_queue_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  509. int queue)
  510. {
  511. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  512. struct iwl_rxq_sync_notification *notif;
  513. struct iwl_mvm_internal_rxq_notif *internal_notif;
  514. notif = (void *)pkt->data;
  515. internal_notif = (void *)notif->payload;
  516. if (internal_notif->sync &&
  517. mvm->queue_sync_cookie != internal_notif->cookie) {
  518. WARN_ONCE(1, "Received expired RX queue sync message\n");
  519. return;
  520. }
  521. switch (internal_notif->type) {
  522. case IWL_MVM_RXQ_EMPTY:
  523. break;
  524. case IWL_MVM_RXQ_NOTIF_DEL_BA:
  525. iwl_mvm_del_ba(mvm, queue, (void *)internal_notif->data);
  526. break;
  527. default:
  528. WARN_ONCE(1, "Invalid identifier %d", internal_notif->type);
  529. }
  530. if (internal_notif->sync &&
  531. !atomic_dec_return(&mvm->queue_sync_counter))
  532. wake_up(&mvm->rx_sync_waitq);
  533. }
  534. /*
  535. * Returns true if the MPDU was buffered\dropped, false if it should be passed
  536. * to upper layer.
  537. */
  538. static bool iwl_mvm_reorder(struct iwl_mvm *mvm,
  539. struct napi_struct *napi,
  540. int queue,
  541. struct ieee80211_sta *sta,
  542. struct sk_buff *skb,
  543. struct iwl_rx_mpdu_desc *desc)
  544. {
  545. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  546. struct iwl_mvm_sta *mvm_sta;
  547. struct iwl_mvm_baid_data *baid_data;
  548. struct iwl_mvm_reorder_buffer *buffer;
  549. struct sk_buff *tail;
  550. u32 reorder = le32_to_cpu(desc->reorder_data);
  551. bool amsdu = desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU;
  552. bool last_subframe =
  553. desc->amsdu_info & IWL_RX_MPDU_AMSDU_LAST_SUBFRAME;
  554. u8 tid = ieee80211_get_tid(hdr);
  555. u8 sub_frame_idx = desc->amsdu_info &
  556. IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
  557. struct iwl_mvm_reorder_buf_entry *entries;
  558. int index;
  559. u16 nssn, sn;
  560. u8 baid;
  561. baid = (reorder & IWL_RX_MPDU_REORDER_BAID_MASK) >>
  562. IWL_RX_MPDU_REORDER_BAID_SHIFT;
  563. /*
  564. * This also covers the case of receiving a Block Ack Request
  565. * outside a BA session; we'll pass it to mac80211 and that
  566. * then sends a delBA action frame.
  567. */
  568. if (baid == IWL_RX_REORDER_DATA_INVALID_BAID)
  569. return false;
  570. /* no sta yet */
  571. if (WARN_ONCE(IS_ERR_OR_NULL(sta),
  572. "Got valid BAID without a valid station assigned\n"))
  573. return false;
  574. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  575. /* not a data packet or a bar */
  576. if (!ieee80211_is_back_req(hdr->frame_control) &&
  577. (!ieee80211_is_data_qos(hdr->frame_control) ||
  578. is_multicast_ether_addr(hdr->addr1)))
  579. return false;
  580. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  581. return false;
  582. baid_data = rcu_dereference(mvm->baid_map[baid]);
  583. if (!baid_data) {
  584. IWL_DEBUG_RX(mvm,
  585. "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
  586. baid, reorder);
  587. return false;
  588. }
  589. if (WARN(tid != baid_data->tid || mvm_sta->sta_id != baid_data->sta_id,
  590. "baid 0x%x is mapped to sta:%d tid:%d, but was received for sta:%d tid:%d\n",
  591. baid, baid_data->sta_id, baid_data->tid, mvm_sta->sta_id,
  592. tid))
  593. return false;
  594. nssn = reorder & IWL_RX_MPDU_REORDER_NSSN_MASK;
  595. sn = (reorder & IWL_RX_MPDU_REORDER_SN_MASK) >>
  596. IWL_RX_MPDU_REORDER_SN_SHIFT;
  597. buffer = &baid_data->reorder_buf[queue];
  598. entries = &baid_data->entries[queue * baid_data->entries_per_queue];
  599. spin_lock_bh(&buffer->lock);
  600. if (!buffer->valid) {
  601. if (reorder & IWL_RX_MPDU_REORDER_BA_OLD_SN) {
  602. spin_unlock_bh(&buffer->lock);
  603. return false;
  604. }
  605. buffer->valid = true;
  606. }
  607. if (ieee80211_is_back_req(hdr->frame_control)) {
  608. iwl_mvm_release_frames(mvm, sta, napi, baid_data, buffer, nssn);
  609. goto drop;
  610. }
  611. /*
  612. * If there was a significant jump in the nssn - adjust.
  613. * If the SN is smaller than the NSSN it might need to first go into
  614. * the reorder buffer, in which case we just release up to it and the
  615. * rest of the function will take care of storing it and releasing up to
  616. * the nssn
  617. */
  618. if (!iwl_mvm_is_sn_less(nssn, buffer->head_sn + buffer->buf_size,
  619. buffer->buf_size) ||
  620. !ieee80211_sn_less(sn, buffer->head_sn + buffer->buf_size)) {
  621. u16 min_sn = ieee80211_sn_less(sn, nssn) ? sn : nssn;
  622. iwl_mvm_release_frames(mvm, sta, napi, baid_data, buffer,
  623. min_sn);
  624. }
  625. /* drop any oudated packets */
  626. if (ieee80211_sn_less(sn, buffer->head_sn))
  627. goto drop;
  628. /* release immediately if allowed by nssn and no stored frames */
  629. if (!buffer->num_stored && ieee80211_sn_less(sn, nssn)) {
  630. if (iwl_mvm_is_sn_less(buffer->head_sn, nssn,
  631. buffer->buf_size) &&
  632. (!amsdu || last_subframe))
  633. buffer->head_sn = nssn;
  634. /* No need to update AMSDU last SN - we are moving the head */
  635. spin_unlock_bh(&buffer->lock);
  636. return false;
  637. }
  638. /*
  639. * release immediately if there are no stored frames, and the sn is
  640. * equal to the head.
  641. * This can happen due to reorder timer, where NSSN is behind head_sn.
  642. * When we released everything, and we got the next frame in the
  643. * sequence, according to the NSSN we can't release immediately,
  644. * while technically there is no hole and we can move forward.
  645. */
  646. if (!buffer->num_stored && sn == buffer->head_sn) {
  647. if (!amsdu || last_subframe)
  648. buffer->head_sn = ieee80211_sn_inc(buffer->head_sn);
  649. /* No need to update AMSDU last SN - we are moving the head */
  650. spin_unlock_bh(&buffer->lock);
  651. return false;
  652. }
  653. index = sn % buffer->buf_size;
  654. /*
  655. * Check if we already stored this frame
  656. * As AMSDU is either received or not as whole, logic is simple:
  657. * If we have frames in that position in the buffer and the last frame
  658. * originated from AMSDU had a different SN then it is a retransmission.
  659. * If it is the same SN then if the subframe index is incrementing it
  660. * is the same AMSDU - otherwise it is a retransmission.
  661. */
  662. tail = skb_peek_tail(&entries[index].e.frames);
  663. if (tail && !amsdu)
  664. goto drop;
  665. else if (tail && (sn != buffer->last_amsdu ||
  666. buffer->last_sub_index >= sub_frame_idx))
  667. goto drop;
  668. /* put in reorder buffer */
  669. __skb_queue_tail(&entries[index].e.frames, skb);
  670. buffer->num_stored++;
  671. entries[index].e.reorder_time = jiffies;
  672. if (amsdu) {
  673. buffer->last_amsdu = sn;
  674. buffer->last_sub_index = sub_frame_idx;
  675. }
  676. /*
  677. * We cannot trust NSSN for AMSDU sub-frames that are not the last.
  678. * The reason is that NSSN advances on the first sub-frame, and may
  679. * cause the reorder buffer to advance before all the sub-frames arrive.
  680. * Example: reorder buffer contains SN 0 & 2, and we receive AMSDU with
  681. * SN 1. NSSN for first sub frame will be 3 with the result of driver
  682. * releasing SN 0,1, 2. When sub-frame 1 arrives - reorder buffer is
  683. * already ahead and it will be dropped.
  684. * If the last sub-frame is not on this queue - we will get frame
  685. * release notification with up to date NSSN.
  686. */
  687. if (!amsdu || last_subframe)
  688. iwl_mvm_release_frames(mvm, sta, napi, baid_data, buffer, nssn);
  689. spin_unlock_bh(&buffer->lock);
  690. return true;
  691. drop:
  692. kfree_skb(skb);
  693. spin_unlock_bh(&buffer->lock);
  694. return true;
  695. }
  696. static void iwl_mvm_agg_rx_received(struct iwl_mvm *mvm,
  697. u32 reorder_data, u8 baid)
  698. {
  699. unsigned long now = jiffies;
  700. unsigned long timeout;
  701. struct iwl_mvm_baid_data *data;
  702. rcu_read_lock();
  703. data = rcu_dereference(mvm->baid_map[baid]);
  704. if (!data) {
  705. IWL_DEBUG_RX(mvm,
  706. "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
  707. baid, reorder_data);
  708. goto out;
  709. }
  710. if (!data->timeout)
  711. goto out;
  712. timeout = data->timeout;
  713. /*
  714. * Do not update last rx all the time to avoid cache bouncing
  715. * between the rx queues.
  716. * Update it every timeout. Worst case is the session will
  717. * expire after ~ 2 * timeout, which doesn't matter that much.
  718. */
  719. if (time_before(data->last_rx + TU_TO_JIFFIES(timeout), now))
  720. /* Update is atomic */
  721. data->last_rx = now;
  722. out:
  723. rcu_read_unlock();
  724. }
  725. static void iwl_mvm_flip_address(u8 *addr)
  726. {
  727. int i;
  728. u8 mac_addr[ETH_ALEN];
  729. for (i = 0; i < ETH_ALEN; i++)
  730. mac_addr[i] = addr[ETH_ALEN - i - 1];
  731. ether_addr_copy(addr, mac_addr);
  732. }
  733. void iwl_mvm_rx_mpdu_mq(struct iwl_mvm *mvm, struct napi_struct *napi,
  734. struct iwl_rx_cmd_buffer *rxb, int queue)
  735. {
  736. struct ieee80211_rx_status *rx_status;
  737. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  738. struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
  739. struct ieee80211_hdr *hdr;
  740. u32 len = le16_to_cpu(desc->mpdu_len);
  741. u32 rate_n_flags, gp2_on_air_rise;
  742. u16 phy_info = le16_to_cpu(desc->phy_info);
  743. struct ieee80211_sta *sta = NULL;
  744. struct sk_buff *skb;
  745. u8 crypt_len = 0, channel, energy_a, energy_b;
  746. struct ieee80211_radiotap_he *he = NULL;
  747. struct ieee80211_radiotap_he_mu *he_mu = NULL;
  748. u32 he_type = 0xffffffff;
  749. /* this is invalid e.g. because puncture type doesn't allow 0b11 */
  750. #define HE_PHY_DATA_INVAL ((u64)-1)
  751. u64 he_phy_data = HE_PHY_DATA_INVAL;
  752. size_t desc_size;
  753. if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
  754. return;
  755. if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560) {
  756. rate_n_flags = le32_to_cpu(desc->v3.rate_n_flags);
  757. channel = desc->v3.channel;
  758. gp2_on_air_rise = le32_to_cpu(desc->v3.gp2_on_air_rise);
  759. energy_a = desc->v3.energy_a;
  760. energy_b = desc->v3.energy_b;
  761. desc_size = sizeof(*desc);
  762. } else {
  763. rate_n_flags = le32_to_cpu(desc->v1.rate_n_flags);
  764. channel = desc->v1.channel;
  765. gp2_on_air_rise = le32_to_cpu(desc->v1.gp2_on_air_rise);
  766. energy_a = desc->v1.energy_a;
  767. energy_b = desc->v1.energy_b;
  768. desc_size = IWL_RX_DESC_SIZE_V1;
  769. }
  770. hdr = (void *)(pkt->data + desc_size);
  771. /* Dont use dev_alloc_skb(), we'll have enough headroom once
  772. * ieee80211_hdr pulled.
  773. */
  774. skb = alloc_skb(128, GFP_ATOMIC);
  775. if (!skb) {
  776. IWL_ERR(mvm, "alloc_skb failed\n");
  777. return;
  778. }
  779. if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
  780. /*
  781. * If the device inserted padding it means that (it thought)
  782. * the 802.11 header wasn't a multiple of 4 bytes long. In
  783. * this case, reserve two bytes at the start of the SKB to
  784. * align the payload properly in case we end up copying it.
  785. */
  786. skb_reserve(skb, 2);
  787. }
  788. rx_status = IEEE80211_SKB_RXCB(skb);
  789. if (rate_n_flags & RATE_MCS_HE_MSK) {
  790. static const struct ieee80211_radiotap_he known = {
  791. .data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
  792. IEEE80211_RADIOTAP_HE_DATA1_DATA_DCM_KNOWN |
  793. IEEE80211_RADIOTAP_HE_DATA1_STBC_KNOWN |
  794. IEEE80211_RADIOTAP_HE_DATA1_CODING_KNOWN),
  795. .data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN |
  796. IEEE80211_RADIOTAP_HE_DATA2_TXBF_KNOWN),
  797. };
  798. static const struct ieee80211_radiotap_he_mu mu_known = {
  799. .flags1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS_KNOWN |
  800. IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM_KNOWN |
  801. IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_SYMS_USERS_KNOWN |
  802. IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_COMP_KNOWN),
  803. .flags2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW_KNOWN),
  804. };
  805. unsigned int radiotap_len = 0;
  806. he = skb_put_data(skb, &known, sizeof(known));
  807. radiotap_len += sizeof(known);
  808. rx_status->flag |= RX_FLAG_RADIOTAP_HE;
  809. he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK;
  810. if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD) {
  811. if (mvm->trans->cfg->device_family >=
  812. IWL_DEVICE_FAMILY_22560)
  813. he_phy_data = le64_to_cpu(desc->v3.he_phy_data);
  814. else
  815. he_phy_data = le64_to_cpu(desc->v1.he_phy_data);
  816. if (he_type == RATE_MCS_HE_TYPE_MU) {
  817. he_mu = skb_put_data(skb, &mu_known,
  818. sizeof(mu_known));
  819. radiotap_len += sizeof(mu_known);
  820. rx_status->flag |= RX_FLAG_RADIOTAP_HE_MU;
  821. }
  822. }
  823. /* temporarily hide the radiotap data */
  824. __skb_pull(skb, radiotap_len);
  825. }
  826. rx_status = IEEE80211_SKB_RXCB(skb);
  827. if (iwl_mvm_rx_crypto(mvm, hdr, rx_status, phy_info, desc,
  828. le32_to_cpu(pkt->len_n_flags), queue,
  829. &crypt_len)) {
  830. kfree_skb(skb);
  831. return;
  832. }
  833. /*
  834. * Keep packets with CRC errors (and with overrun) for monitor mode
  835. * (otherwise the firmware discards them) but mark them as bad.
  836. */
  837. if (!(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_CRC_OK)) ||
  838. !(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_OVERRUN_OK))) {
  839. IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n",
  840. le16_to_cpu(desc->status));
  841. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  842. }
  843. /* set the preamble flag if appropriate */
  844. if (phy_info & IWL_RX_MPDU_PHY_SHORT_PREAMBLE)
  845. rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
  846. if (likely(!(phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD))) {
  847. u64 tsf_on_air_rise;
  848. if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
  849. tsf_on_air_rise = le64_to_cpu(desc->v3.tsf_on_air_rise);
  850. else
  851. tsf_on_air_rise = le64_to_cpu(desc->v1.tsf_on_air_rise);
  852. rx_status->mactime = tsf_on_air_rise;
  853. /* TSF as indicated by the firmware is at INA time */
  854. rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
  855. } else if (he_type == RATE_MCS_HE_TYPE_SU) {
  856. u64 he_phy_data;
  857. if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
  858. he_phy_data = le64_to_cpu(desc->v3.he_phy_data);
  859. else
  860. he_phy_data = le64_to_cpu(desc->v1.he_phy_data);
  861. he->data1 |=
  862. cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_UL_DL_KNOWN);
  863. if (FIELD_GET(IWL_RX_HE_PHY_UPLINK,
  864. he_phy_data))
  865. he->data3 |=
  866. cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA3_UL_DL);
  867. if (!queue && !(phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
  868. rx_status->ampdu_reference = mvm->ampdu_ref;
  869. mvm->ampdu_ref++;
  870. rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
  871. rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
  872. if (FIELD_GET(IWL_RX_HE_PHY_DELIM_EOF,
  873. he_phy_data))
  874. rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
  875. }
  876. } else if (he_mu && he_phy_data != HE_PHY_DATA_INVAL) {
  877. he_mu->flags1 |=
  878. le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK,
  879. he_phy_data),
  880. IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_SYMS_USERS);
  881. he_mu->flags1 |=
  882. le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIGB_DCM,
  883. he_phy_data),
  884. IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM);
  885. he_mu->flags1 |=
  886. le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIGB_MCS_MASK,
  887. he_phy_data),
  888. IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS);
  889. he_mu->flags2 |=
  890. le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIGB_COMPRESSION,
  891. he_phy_data),
  892. IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_COMP);
  893. he_mu->flags2 |=
  894. le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_PREAMBLE_PUNC_TYPE_MASK,
  895. he_phy_data),
  896. IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW);
  897. }
  898. rx_status->device_timestamp = gp2_on_air_rise;
  899. rx_status->band = channel > 14 ? NL80211_BAND_5GHZ :
  900. NL80211_BAND_2GHZ;
  901. rx_status->freq = ieee80211_channel_to_frequency(channel,
  902. rx_status->band);
  903. iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags, energy_a,
  904. energy_b);
  905. /* update aggregation data for monitor sake on default queue */
  906. if (!queue && (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
  907. bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
  908. u64 he_phy_data;
  909. if (mvm->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
  910. he_phy_data = le64_to_cpu(desc->v3.he_phy_data);
  911. else
  912. he_phy_data = le64_to_cpu(desc->v1.he_phy_data);
  913. rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
  914. rx_status->ampdu_reference = mvm->ampdu_ref;
  915. /* toggle is switched whenever new aggregation starts */
  916. if (toggle_bit != mvm->ampdu_toggle) {
  917. mvm->ampdu_ref++;
  918. mvm->ampdu_toggle = toggle_bit;
  919. if (he_phy_data != HE_PHY_DATA_INVAL &&
  920. he_type == RATE_MCS_HE_TYPE_MU) {
  921. rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
  922. if (FIELD_GET(IWL_RX_HE_PHY_DELIM_EOF,
  923. he_phy_data))
  924. rx_status->flag |=
  925. RX_FLAG_AMPDU_EOF_BIT;
  926. }
  927. }
  928. }
  929. rcu_read_lock();
  930. if (desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_SRC_STA_FOUND)) {
  931. u8 id = desc->sta_id_flags & IWL_RX_MPDU_SIF_STA_ID_MASK;
  932. if (!WARN_ON_ONCE(id >= ARRAY_SIZE(mvm->fw_id_to_mac_id))) {
  933. sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
  934. if (IS_ERR(sta))
  935. sta = NULL;
  936. }
  937. } else if (!is_multicast_ether_addr(hdr->addr2)) {
  938. /*
  939. * This is fine since we prevent two stations with the same
  940. * address from being added.
  941. */
  942. sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
  943. }
  944. if (sta) {
  945. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  946. struct ieee80211_vif *tx_blocked_vif =
  947. rcu_dereference(mvm->csa_tx_blocked_vif);
  948. u8 baid = (u8)((le32_to_cpu(desc->reorder_data) &
  949. IWL_RX_MPDU_REORDER_BAID_MASK) >>
  950. IWL_RX_MPDU_REORDER_BAID_SHIFT);
  951. if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
  952. !is_multicast_ether_addr(hdr->addr1) &&
  953. ieee80211_is_data(hdr->frame_control) &&
  954. time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
  955. schedule_delayed_work(&mvm->tcm.work, 0);
  956. /*
  957. * We have tx blocked stations (with CS bit). If we heard
  958. * frames from a blocked station on a new channel we can
  959. * TX to it again.
  960. */
  961. if (unlikely(tx_blocked_vif) &&
  962. tx_blocked_vif == mvmsta->vif) {
  963. struct iwl_mvm_vif *mvmvif =
  964. iwl_mvm_vif_from_mac80211(tx_blocked_vif);
  965. if (mvmvif->csa_target_freq == rx_status->freq)
  966. iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
  967. false);
  968. }
  969. rs_update_last_rssi(mvm, mvmsta, rx_status);
  970. if (iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_RSSI) &&
  971. ieee80211_is_beacon(hdr->frame_control)) {
  972. struct iwl_fw_dbg_trigger_tlv *trig;
  973. struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
  974. bool trig_check;
  975. s32 rssi;
  976. trig = iwl_fw_dbg_get_trigger(mvm->fw,
  977. FW_DBG_TRIGGER_RSSI);
  978. rssi_trig = (void *)trig->data;
  979. rssi = le32_to_cpu(rssi_trig->rssi);
  980. trig_check =
  981. iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  982. ieee80211_vif_to_wdev(mvmsta->vif),
  983. trig);
  984. if (trig_check && rx_status->signal < rssi)
  985. iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
  986. NULL);
  987. }
  988. if (ieee80211_is_data(hdr->frame_control))
  989. iwl_mvm_rx_csum(sta, skb, desc);
  990. if (iwl_mvm_is_dup(sta, queue, rx_status, hdr, desc)) {
  991. kfree_skb(skb);
  992. goto out;
  993. }
  994. /*
  995. * Our hardware de-aggregates AMSDUs but copies the mac header
  996. * as it to the de-aggregated MPDUs. We need to turn off the
  997. * AMSDU bit in the QoS control ourselves.
  998. * In addition, HW reverses addr3 and addr4 - reverse it back.
  999. */
  1000. if ((desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU) &&
  1001. !WARN_ON(!ieee80211_is_data_qos(hdr->frame_control))) {
  1002. u8 *qc = ieee80211_get_qos_ctl(hdr);
  1003. *qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  1004. if (mvm->trans->cfg->device_family ==
  1005. IWL_DEVICE_FAMILY_9000) {
  1006. iwl_mvm_flip_address(hdr->addr3);
  1007. if (ieee80211_has_a4(hdr->frame_control))
  1008. iwl_mvm_flip_address(hdr->addr4);
  1009. }
  1010. }
  1011. if (baid != IWL_RX_REORDER_DATA_INVALID_BAID) {
  1012. u32 reorder_data = le32_to_cpu(desc->reorder_data);
  1013. iwl_mvm_agg_rx_received(mvm, reorder_data, baid);
  1014. }
  1015. }
  1016. switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
  1017. case RATE_MCS_CHAN_WIDTH_20:
  1018. break;
  1019. case RATE_MCS_CHAN_WIDTH_40:
  1020. rx_status->bw = RATE_INFO_BW_40;
  1021. break;
  1022. case RATE_MCS_CHAN_WIDTH_80:
  1023. rx_status->bw = RATE_INFO_BW_80;
  1024. break;
  1025. case RATE_MCS_CHAN_WIDTH_160:
  1026. rx_status->bw = RATE_INFO_BW_160;
  1027. break;
  1028. }
  1029. if (he_type == RATE_MCS_HE_TYPE_EXT_SU &&
  1030. rate_n_flags & RATE_MCS_HE_106T_MSK) {
  1031. rx_status->bw = RATE_INFO_BW_HE_RU;
  1032. rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
  1033. }
  1034. if (rate_n_flags & RATE_MCS_HE_MSK &&
  1035. phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD &&
  1036. he_type == RATE_MCS_HE_TYPE_MU) {
  1037. /*
  1038. * Unfortunately, we have to leave the mac80211 data
  1039. * incorrect for the case that we receive an HE-MU
  1040. * transmission and *don't* have the he_mu pointer,
  1041. * i.e. we don't have the phy data (due to the bits
  1042. * being used for TSF). This shouldn't happen though
  1043. * as management frames where we need the TSF/timers
  1044. * are not be transmitted in HE-MU, I think.
  1045. */
  1046. u8 ru = FIELD_GET(IWL_RX_HE_PHY_RU_ALLOC_MASK, he_phy_data);
  1047. u8 offs = 0;
  1048. rx_status->bw = RATE_INFO_BW_HE_RU;
  1049. switch (ru) {
  1050. case 0 ... 36:
  1051. rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_26;
  1052. offs = ru;
  1053. break;
  1054. case 37 ... 52:
  1055. rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_52;
  1056. offs = ru - 37;
  1057. break;
  1058. case 53 ... 60:
  1059. rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
  1060. offs = ru - 53;
  1061. break;
  1062. case 61 ... 64:
  1063. rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_242;
  1064. offs = ru - 61;
  1065. break;
  1066. case 65 ... 66:
  1067. rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_484;
  1068. offs = ru - 65;
  1069. break;
  1070. case 67:
  1071. rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_996;
  1072. break;
  1073. case 68:
  1074. rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
  1075. break;
  1076. }
  1077. he->data2 |=
  1078. le16_encode_bits(offs,
  1079. IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET);
  1080. he->data2 |=
  1081. cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_KNOWN);
  1082. if (he_phy_data & IWL_RX_HE_PHY_RU_ALLOC_SEC80)
  1083. he->data2 |=
  1084. cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_SEC);
  1085. } else if (he) {
  1086. he->data1 |=
  1087. cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
  1088. }
  1089. if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
  1090. rate_n_flags & RATE_MCS_SGI_MSK)
  1091. rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
  1092. if (rate_n_flags & RATE_HT_MCS_GF_MSK)
  1093. rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
  1094. if (rate_n_flags & RATE_MCS_LDPC_MSK)
  1095. rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
  1096. if (rate_n_flags & RATE_MCS_HT_MSK) {
  1097. u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
  1098. RATE_MCS_STBC_POS;
  1099. rx_status->encoding = RX_ENC_HT;
  1100. rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
  1101. rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
  1102. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  1103. u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
  1104. RATE_MCS_STBC_POS;
  1105. rx_status->nss =
  1106. ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
  1107. RATE_VHT_MCS_NSS_POS) + 1;
  1108. rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
  1109. rx_status->encoding = RX_ENC_VHT;
  1110. rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
  1111. if (rate_n_flags & RATE_MCS_BF_MSK)
  1112. rx_status->enc_flags |= RX_ENC_FLAG_BF;
  1113. } else if (he) {
  1114. u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
  1115. RATE_MCS_STBC_POS;
  1116. rx_status->nss =
  1117. ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
  1118. RATE_VHT_MCS_NSS_POS) + 1;
  1119. rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
  1120. rx_status->encoding = RX_ENC_HE;
  1121. rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
  1122. if (rate_n_flags & RATE_MCS_BF_MSK)
  1123. rx_status->enc_flags |= RX_ENC_FLAG_BF;
  1124. rx_status->he_dcm =
  1125. !!(rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK);
  1126. #define CHECK_TYPE(F) \
  1127. BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA1_FORMAT_ ## F != \
  1128. (RATE_MCS_HE_TYPE_ ## F >> RATE_MCS_HE_TYPE_POS))
  1129. CHECK_TYPE(SU);
  1130. CHECK_TYPE(EXT_SU);
  1131. CHECK_TYPE(MU);
  1132. CHECK_TYPE(TRIG);
  1133. he->data1 |= cpu_to_le16(he_type >> RATE_MCS_HE_TYPE_POS);
  1134. if (rate_n_flags & RATE_MCS_BF_POS)
  1135. he->data5 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA5_TXBF);
  1136. switch ((rate_n_flags & RATE_MCS_HE_GI_LTF_MSK) >>
  1137. RATE_MCS_HE_GI_LTF_POS) {
  1138. case 0:
  1139. rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
  1140. break;
  1141. case 1:
  1142. rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
  1143. break;
  1144. case 2:
  1145. rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
  1146. break;
  1147. case 3:
  1148. if (rate_n_flags & RATE_MCS_SGI_MSK)
  1149. rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
  1150. else
  1151. rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
  1152. break;
  1153. }
  1154. switch (he_type) {
  1155. case RATE_MCS_HE_TYPE_SU: {
  1156. u16 val;
  1157. /* LTF syms correspond to streams */
  1158. he->data2 |=
  1159. cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_NUM_LTF_SYMS_KNOWN);
  1160. switch (rx_status->nss) {
  1161. case 1:
  1162. val = 0;
  1163. break;
  1164. case 2:
  1165. val = 1;
  1166. break;
  1167. case 3:
  1168. case 4:
  1169. val = 2;
  1170. break;
  1171. case 5:
  1172. case 6:
  1173. val = 3;
  1174. break;
  1175. case 7:
  1176. case 8:
  1177. val = 4;
  1178. break;
  1179. default:
  1180. WARN_ONCE(1, "invalid nss: %d\n",
  1181. rx_status->nss);
  1182. val = 0;
  1183. }
  1184. he->data5 |=
  1185. le16_encode_bits(val,
  1186. IEEE80211_RADIOTAP_HE_DATA5_NUM_LTF_SYMS);
  1187. }
  1188. break;
  1189. case RATE_MCS_HE_TYPE_MU: {
  1190. u16 val;
  1191. u64 he_phy_data;
  1192. if (mvm->trans->cfg->device_family >=
  1193. IWL_DEVICE_FAMILY_22560)
  1194. he_phy_data = le64_to_cpu(desc->v3.he_phy_data);
  1195. else
  1196. he_phy_data = le64_to_cpu(desc->v1.he_phy_data);
  1197. if (he_phy_data == HE_PHY_DATA_INVAL)
  1198. break;
  1199. val = FIELD_GET(IWL_RX_HE_PHY_HE_LTF_NUM_MASK,
  1200. he_phy_data);
  1201. he->data2 |=
  1202. cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_NUM_LTF_SYMS_KNOWN);
  1203. he->data5 |=
  1204. cpu_to_le16(FIELD_PREP(
  1205. IEEE80211_RADIOTAP_HE_DATA5_NUM_LTF_SYMS,
  1206. val));
  1207. }
  1208. break;
  1209. case RATE_MCS_HE_TYPE_EXT_SU:
  1210. case RATE_MCS_HE_TYPE_TRIG:
  1211. /* not supported yet */
  1212. break;
  1213. }
  1214. } else {
  1215. int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
  1216. rx_status->band);
  1217. if (WARN(rate < 0 || rate > 0xFF,
  1218. "Invalid rate flags 0x%x, band %d,\n",
  1219. rate_n_flags, rx_status->band)) {
  1220. kfree_skb(skb);
  1221. goto out;
  1222. }
  1223. rx_status->rate_idx = rate;
  1224. }
  1225. /* management stuff on default queue */
  1226. if (!queue) {
  1227. if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
  1228. ieee80211_is_probe_resp(hdr->frame_control)) &&
  1229. mvm->sched_scan_pass_all ==
  1230. SCHED_SCAN_PASS_ALL_ENABLED))
  1231. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
  1232. if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
  1233. ieee80211_is_probe_resp(hdr->frame_control)))
  1234. rx_status->boottime_ns = ktime_get_boot_ns();
  1235. }
  1236. iwl_mvm_create_skb(skb, hdr, len, crypt_len, rxb);
  1237. if (!iwl_mvm_reorder(mvm, napi, queue, sta, skb, desc))
  1238. iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, queue, sta);
  1239. out:
  1240. rcu_read_unlock();
  1241. }
  1242. void iwl_mvm_rx_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
  1243. struct iwl_rx_cmd_buffer *rxb, int queue)
  1244. {
  1245. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1246. struct iwl_frame_release *release = (void *)pkt->data;
  1247. struct ieee80211_sta *sta;
  1248. struct iwl_mvm_reorder_buffer *reorder_buf;
  1249. struct iwl_mvm_baid_data *ba_data;
  1250. int baid = release->baid;
  1251. IWL_DEBUG_HT(mvm, "Frame release notification for BAID %u, NSSN %d\n",
  1252. release->baid, le16_to_cpu(release->nssn));
  1253. if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
  1254. return;
  1255. rcu_read_lock();
  1256. ba_data = rcu_dereference(mvm->baid_map[baid]);
  1257. if (WARN_ON_ONCE(!ba_data))
  1258. goto out;
  1259. sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
  1260. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
  1261. goto out;
  1262. reorder_buf = &ba_data->reorder_buf[queue];
  1263. spin_lock_bh(&reorder_buf->lock);
  1264. iwl_mvm_release_frames(mvm, sta, napi, ba_data, reorder_buf,
  1265. le16_to_cpu(release->nssn));
  1266. spin_unlock_bh(&reorder_buf->lock);
  1267. out:
  1268. rcu_read_unlock();
  1269. }