rxmq.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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 - 2016 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. * The full GNU General Public License is included in this distribution
  22. * in the file called COPYING.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. * BSD LICENSE
  29. *
  30. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  31. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  32. * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
  33. * All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. *
  39. * * Redistributions of source code must retain the above copyright
  40. * notice, this list of conditions and the following disclaimer.
  41. * * Redistributions in binary form must reproduce the above copyright
  42. * notice, this list of conditions and the following disclaimer in
  43. * the documentation and/or other materials provided with the
  44. * distribution.
  45. * * Neither the name Intel Corporation nor the names of its
  46. * contributors may be used to endorse or promote products derived
  47. * from this software without specific prior written permission.
  48. *
  49. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  50. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  51. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  52. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  53. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  54. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  55. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  56. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  57. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  58. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  59. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. *****************************************************************************/
  61. #include <linux/etherdevice.h>
  62. #include <linux/skbuff.h>
  63. #include "iwl-trans.h"
  64. #include "mvm.h"
  65. #include "fw-api.h"
  66. #include "fw-dbg.h"
  67. static inline int iwl_mvm_check_pn(struct iwl_mvm *mvm, struct sk_buff *skb,
  68. int queue, struct ieee80211_sta *sta)
  69. {
  70. struct iwl_mvm_sta *mvmsta;
  71. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  72. struct ieee80211_rx_status *stats = IEEE80211_SKB_RXCB(skb);
  73. struct iwl_mvm_key_pn *ptk_pn;
  74. int res;
  75. u8 tid, keyidx;
  76. u8 pn[IEEE80211_CCMP_PN_LEN];
  77. u8 *extiv;
  78. /* do PN checking */
  79. /* multicast and non-data only arrives on default queue */
  80. if (!ieee80211_is_data(hdr->frame_control) ||
  81. is_multicast_ether_addr(hdr->addr1))
  82. return 0;
  83. /* do not check PN for open AP */
  84. if (!(stats->flag & RX_FLAG_DECRYPTED))
  85. return 0;
  86. /*
  87. * avoid checking for default queue - we don't want to replicate
  88. * all the logic that's necessary for checking the PN on fragmented
  89. * frames, leave that to mac80211
  90. */
  91. if (queue == 0)
  92. return 0;
  93. /* if we are here - this for sure is either CCMP or GCMP */
  94. if (IS_ERR_OR_NULL(sta)) {
  95. IWL_ERR(mvm,
  96. "expected hw-decrypted unicast frame for station\n");
  97. return -1;
  98. }
  99. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  100. extiv = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control);
  101. keyidx = extiv[3] >> 6;
  102. ptk_pn = rcu_dereference(mvmsta->ptk_pn[keyidx]);
  103. if (!ptk_pn)
  104. return -1;
  105. if (ieee80211_is_data_qos(hdr->frame_control))
  106. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  107. else
  108. tid = 0;
  109. /* we don't use HCCA/802.11 QoS TSPECs, so drop such frames */
  110. if (tid >= IWL_MAX_TID_COUNT)
  111. return -1;
  112. /* load pn */
  113. pn[0] = extiv[7];
  114. pn[1] = extiv[6];
  115. pn[2] = extiv[5];
  116. pn[3] = extiv[4];
  117. pn[4] = extiv[1];
  118. pn[5] = extiv[0];
  119. res = memcmp(pn, ptk_pn->q[queue].pn[tid], IEEE80211_CCMP_PN_LEN);
  120. if (res < 0)
  121. return -1;
  122. if (!res && !(stats->flag & RX_FLAG_ALLOW_SAME_PN))
  123. return -1;
  124. memcpy(ptk_pn->q[queue].pn[tid], pn, IEEE80211_CCMP_PN_LEN);
  125. stats->flag |= RX_FLAG_PN_VALIDATED;
  126. return 0;
  127. }
  128. /* iwl_mvm_create_skb Adds the rxb to a new skb */
  129. static void iwl_mvm_create_skb(struct sk_buff *skb, struct ieee80211_hdr *hdr,
  130. u16 len, u8 crypt_len,
  131. struct iwl_rx_cmd_buffer *rxb)
  132. {
  133. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  134. struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
  135. unsigned int headlen, fraglen, pad_len = 0;
  136. unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  137. if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD)
  138. pad_len = 2;
  139. len -= pad_len;
  140. /* If frame is small enough to fit in skb->head, pull it completely.
  141. * If not, only pull ieee80211_hdr (including crypto if present, and
  142. * an additional 8 bytes for SNAP/ethertype, see below) so that
  143. * splice() or TCP coalesce are more efficient.
  144. *
  145. * Since, in addition, ieee80211_data_to_8023() always pull in at
  146. * least 8 bytes (possibly more for mesh) we can do the same here
  147. * to save the cost of doing it later. That still doesn't pull in
  148. * the actual IP header since the typical case has a SNAP header.
  149. * If the latter changes (there are efforts in the standards group
  150. * to do so) we should revisit this and ieee80211_data_to_8023().
  151. */
  152. headlen = (len <= skb_tailroom(skb)) ? len :
  153. hdrlen + crypt_len + 8;
  154. /* The firmware may align the packet to DWORD.
  155. * The padding is inserted after the IV.
  156. * After copying the header + IV skip the padding if
  157. * present before copying packet data.
  158. */
  159. hdrlen += crypt_len;
  160. memcpy(skb_put(skb, hdrlen), hdr, hdrlen);
  161. memcpy(skb_put(skb, headlen - hdrlen), (u8 *)hdr + hdrlen + pad_len,
  162. headlen - hdrlen);
  163. fraglen = len - headlen;
  164. if (fraglen) {
  165. int offset = (void *)hdr + headlen + pad_len -
  166. rxb_addr(rxb) + rxb_offset(rxb);
  167. skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
  168. fraglen, rxb->truesize);
  169. }
  170. }
  171. /* iwl_mvm_pass_packet_to_mac80211 - passes the packet for mac80211 */
  172. static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
  173. struct napi_struct *napi,
  174. struct sk_buff *skb, int queue,
  175. struct ieee80211_sta *sta)
  176. {
  177. if (iwl_mvm_check_pn(mvm, skb, queue, sta))
  178. kfree_skb(skb);
  179. else
  180. ieee80211_rx_napi(mvm->hw, sta, skb, napi);
  181. }
  182. static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
  183. struct iwl_rx_mpdu_desc *desc,
  184. struct ieee80211_rx_status *rx_status)
  185. {
  186. int energy_a, energy_b, max_energy;
  187. energy_a = desc->energy_a;
  188. energy_a = energy_a ? -energy_a : S8_MIN;
  189. energy_b = desc->energy_b;
  190. energy_b = energy_b ? -energy_b : S8_MIN;
  191. max_energy = max(energy_a, energy_b);
  192. IWL_DEBUG_STATS(mvm, "energy In A %d B %d, and max %d\n",
  193. energy_a, energy_b, max_energy);
  194. rx_status->signal = max_energy;
  195. rx_status->chains = 0; /* TODO: phy info */
  196. rx_status->chain_signal[0] = energy_a;
  197. rx_status->chain_signal[1] = energy_b;
  198. rx_status->chain_signal[2] = S8_MIN;
  199. }
  200. static int iwl_mvm_rx_crypto(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
  201. struct ieee80211_rx_status *stats,
  202. struct iwl_rx_mpdu_desc *desc, int queue,
  203. u8 *crypt_len)
  204. {
  205. u16 status = le16_to_cpu(desc->status);
  206. if (!ieee80211_has_protected(hdr->frame_control) ||
  207. (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
  208. IWL_RX_MPDU_STATUS_SEC_NONE)
  209. return 0;
  210. /* TODO: handle packets encrypted with unknown alg */
  211. switch (status & IWL_RX_MPDU_STATUS_SEC_MASK) {
  212. case IWL_RX_MPDU_STATUS_SEC_CCM:
  213. case IWL_RX_MPDU_STATUS_SEC_GCM:
  214. BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN != IEEE80211_GCMP_PN_LEN);
  215. /* alg is CCM: check MIC only */
  216. if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
  217. return -1;
  218. stats->flag |= RX_FLAG_DECRYPTED;
  219. *crypt_len = IEEE80211_CCMP_HDR_LEN;
  220. return 0;
  221. case IWL_RX_MPDU_STATUS_SEC_TKIP:
  222. /* Don't drop the frame and decrypt it in SW */
  223. if (!(status & IWL_RX_MPDU_RES_STATUS_TTAK_OK))
  224. return 0;
  225. *crypt_len = IEEE80211_TKIP_IV_LEN;
  226. /* fall through if TTAK OK */
  227. case IWL_RX_MPDU_STATUS_SEC_WEP:
  228. if (!(status & IWL_RX_MPDU_STATUS_ICV_OK))
  229. return -1;
  230. stats->flag |= RX_FLAG_DECRYPTED;
  231. if ((status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
  232. IWL_RX_MPDU_STATUS_SEC_WEP)
  233. *crypt_len = IEEE80211_WEP_IV_LEN;
  234. return 0;
  235. case IWL_RX_MPDU_STATUS_SEC_EXT_ENC:
  236. if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
  237. return -1;
  238. stats->flag |= RX_FLAG_DECRYPTED;
  239. return 0;
  240. default:
  241. IWL_ERR(mvm, "Unhandled alg: 0x%x\n", status);
  242. }
  243. return 0;
  244. }
  245. static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
  246. struct sk_buff *skb,
  247. struct iwl_rx_mpdu_desc *desc)
  248. {
  249. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  250. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
  251. u16 flags = le16_to_cpu(desc->l3l4_flags);
  252. u8 l3_prot = (u8)((flags & IWL_RX_L3L4_L3_PROTO_MASK) >>
  253. IWL_RX_L3_PROTO_POS);
  254. if (mvmvif->features & NETIF_F_RXCSUM &&
  255. flags & IWL_RX_L3L4_TCP_UDP_CSUM_OK &&
  256. (flags & IWL_RX_L3L4_IP_HDR_CSUM_OK ||
  257. l3_prot == IWL_RX_L3_TYPE_IPV6 ||
  258. l3_prot == IWL_RX_L3_TYPE_IPV6_FRAG))
  259. skb->ip_summed = CHECKSUM_UNNECESSARY;
  260. }
  261. /*
  262. * returns true if a packet is a duplicate and should be dropped.
  263. * Updates AMSDU PN tracking info
  264. */
  265. static bool iwl_mvm_is_dup(struct ieee80211_sta *sta, int queue,
  266. struct ieee80211_rx_status *rx_status,
  267. struct ieee80211_hdr *hdr,
  268. struct iwl_rx_mpdu_desc *desc)
  269. {
  270. struct iwl_mvm_sta *mvm_sta;
  271. struct iwl_mvm_rxq_dup_data *dup_data;
  272. u8 tid, sub_frame_idx;
  273. if (WARN_ON(IS_ERR_OR_NULL(sta)))
  274. return false;
  275. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  276. dup_data = &mvm_sta->dup_data[queue];
  277. /*
  278. * Drop duplicate 802.11 retransmissions
  279. * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
  280. */
  281. if (ieee80211_is_ctl(hdr->frame_control) ||
  282. ieee80211_is_qos_nullfunc(hdr->frame_control) ||
  283. is_multicast_ether_addr(hdr->addr1)) {
  284. rx_status->flag |= RX_FLAG_DUP_VALIDATED;
  285. return false;
  286. }
  287. if (ieee80211_is_data_qos(hdr->frame_control))
  288. /* frame has qos control */
  289. tid = *ieee80211_get_qos_ctl(hdr) &
  290. IEEE80211_QOS_CTL_TID_MASK;
  291. else
  292. tid = IWL_MAX_TID_COUNT;
  293. /* If this wasn't a part of an A-MSDU the sub-frame index will be 0 */
  294. sub_frame_idx = desc->amsdu_info & IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
  295. if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
  296. dup_data->last_seq[tid] == hdr->seq_ctrl &&
  297. dup_data->last_sub_frame[tid] >= sub_frame_idx))
  298. return true;
  299. /* Allow same PN as the first subframe for following sub frames */
  300. if (dup_data->last_seq[tid] == hdr->seq_ctrl &&
  301. sub_frame_idx > dup_data->last_sub_frame[tid] &&
  302. desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU)
  303. rx_status->flag |= RX_FLAG_ALLOW_SAME_PN;
  304. dup_data->last_seq[tid] = hdr->seq_ctrl;
  305. dup_data->last_sub_frame[tid] = sub_frame_idx;
  306. rx_status->flag |= RX_FLAG_DUP_VALIDATED;
  307. return false;
  308. }
  309. int iwl_mvm_notify_rx_queue(struct iwl_mvm *mvm, u32 rxq_mask,
  310. const u8 *data, u32 count)
  311. {
  312. struct iwl_rxq_sync_cmd *cmd;
  313. u32 data_size = sizeof(*cmd) + count;
  314. int ret;
  315. /* should be DWORD aligned */
  316. if (WARN_ON(count & 3 || count > IWL_MULTI_QUEUE_SYNC_MSG_MAX_SIZE))
  317. return -EINVAL;
  318. cmd = kzalloc(data_size, GFP_KERNEL);
  319. if (!cmd)
  320. return -ENOMEM;
  321. cmd->rxq_mask = cpu_to_le32(rxq_mask);
  322. cmd->count = cpu_to_le32(count);
  323. cmd->flags = 0;
  324. memcpy(cmd->payload, data, count);
  325. ret = iwl_mvm_send_cmd_pdu(mvm,
  326. WIDE_ID(DATA_PATH_GROUP,
  327. TRIGGER_RX_QUEUES_NOTIF_CMD),
  328. 0, data_size, cmd);
  329. kfree(cmd);
  330. return ret;
  331. }
  332. /*
  333. * Returns true if sn2 - buffer_size < sn1 < sn2.
  334. * To be used only in order to compare reorder buffer head with NSSN.
  335. * We fully trust NSSN unless it is behind us due to reorder timeout.
  336. * Reorder timeout can only bring us up to buffer_size SNs ahead of NSSN.
  337. */
  338. static bool iwl_mvm_is_sn_less(u16 sn1, u16 sn2, u16 buffer_size)
  339. {
  340. return ieee80211_sn_less(sn1, sn2) &&
  341. !ieee80211_sn_less(sn1, sn2 - buffer_size);
  342. }
  343. #define RX_REORDER_BUF_TIMEOUT_MQ (HZ / 10)
  344. static void iwl_mvm_release_frames(struct iwl_mvm *mvm,
  345. struct ieee80211_sta *sta,
  346. struct napi_struct *napi,
  347. struct iwl_mvm_reorder_buffer *reorder_buf,
  348. u16 nssn)
  349. {
  350. u16 ssn = reorder_buf->head_sn;
  351. lockdep_assert_held(&reorder_buf->lock);
  352. /* ignore nssn smaller than head sn - this can happen due to timeout */
  353. if (iwl_mvm_is_sn_less(nssn, ssn, reorder_buf->buf_size))
  354. goto set_timer;
  355. while (iwl_mvm_is_sn_less(ssn, nssn, reorder_buf->buf_size)) {
  356. int index = ssn % reorder_buf->buf_size;
  357. struct sk_buff_head *skb_list = &reorder_buf->entries[index];
  358. struct sk_buff *skb;
  359. ssn = ieee80211_sn_inc(ssn);
  360. /*
  361. * Empty the list. Will have more than one frame for A-MSDU.
  362. * Empty list is valid as well since nssn indicates frames were
  363. * received.
  364. */
  365. while ((skb = __skb_dequeue(skb_list))) {
  366. iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb,
  367. reorder_buf->queue,
  368. sta);
  369. reorder_buf->num_stored--;
  370. }
  371. }
  372. reorder_buf->head_sn = nssn;
  373. set_timer:
  374. if (reorder_buf->num_stored && !reorder_buf->removed) {
  375. u16 index = reorder_buf->head_sn % reorder_buf->buf_size;
  376. while (skb_queue_empty(&reorder_buf->entries[index]))
  377. index = (index + 1) % reorder_buf->buf_size;
  378. /* modify timer to match next frame's expiration time */
  379. mod_timer(&reorder_buf->reorder_timer,
  380. reorder_buf->reorder_time[index] + 1 +
  381. RX_REORDER_BUF_TIMEOUT_MQ);
  382. } else {
  383. del_timer(&reorder_buf->reorder_timer);
  384. }
  385. }
  386. void iwl_mvm_reorder_timer_expired(unsigned long data)
  387. {
  388. struct iwl_mvm_reorder_buffer *buf = (void *)data;
  389. int i;
  390. u16 sn = 0, index = 0;
  391. bool expired = false;
  392. spin_lock(&buf->lock);
  393. if (!buf->num_stored || buf->removed) {
  394. spin_unlock(&buf->lock);
  395. return;
  396. }
  397. for (i = 0; i < buf->buf_size ; i++) {
  398. index = (buf->head_sn + i) % buf->buf_size;
  399. if (skb_queue_empty(&buf->entries[index]))
  400. continue;
  401. if (!time_after(jiffies, buf->reorder_time[index] +
  402. RX_REORDER_BUF_TIMEOUT_MQ))
  403. break;
  404. expired = true;
  405. sn = ieee80211_sn_add(buf->head_sn, i + 1);
  406. }
  407. if (expired) {
  408. struct ieee80211_sta *sta;
  409. rcu_read_lock();
  410. sta = rcu_dereference(buf->mvm->fw_id_to_mac_id[buf->sta_id]);
  411. /* SN is set to the last expired frame + 1 */
  412. IWL_DEBUG_HT(buf->mvm,
  413. "Releasing expired frames for sta %u, sn %d\n",
  414. buf->sta_id, sn);
  415. iwl_mvm_release_frames(buf->mvm, sta, NULL, buf, sn);
  416. rcu_read_unlock();
  417. } else if (buf->num_stored) {
  418. /*
  419. * If no frame expired and there are stored frames, index is now
  420. * pointing to the first unexpired frame - modify timer
  421. * accordingly to this frame.
  422. */
  423. mod_timer(&buf->reorder_timer,
  424. buf->reorder_time[index] +
  425. 1 + RX_REORDER_BUF_TIMEOUT_MQ);
  426. }
  427. spin_unlock(&buf->lock);
  428. }
  429. static void iwl_mvm_del_ba(struct iwl_mvm *mvm, int queue,
  430. struct iwl_mvm_delba_data *data)
  431. {
  432. struct iwl_mvm_baid_data *ba_data;
  433. struct ieee80211_sta *sta;
  434. struct iwl_mvm_reorder_buffer *reorder_buf;
  435. u8 baid = data->baid;
  436. if (WARN_ONCE(baid >= IWL_MAX_BAID, "invalid BAID: %x\n", baid))
  437. return;
  438. rcu_read_lock();
  439. ba_data = rcu_dereference(mvm->baid_map[baid]);
  440. if (WARN_ON_ONCE(!ba_data))
  441. goto out;
  442. sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
  443. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
  444. goto out;
  445. reorder_buf = &ba_data->reorder_buf[queue];
  446. /* release all frames that are in the reorder buffer to the stack */
  447. spin_lock_bh(&reorder_buf->lock);
  448. iwl_mvm_release_frames(mvm, sta, NULL, reorder_buf,
  449. ieee80211_sn_add(reorder_buf->head_sn,
  450. reorder_buf->buf_size));
  451. spin_unlock_bh(&reorder_buf->lock);
  452. del_timer_sync(&reorder_buf->reorder_timer);
  453. out:
  454. rcu_read_unlock();
  455. }
  456. void iwl_mvm_rx_queue_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  457. int queue)
  458. {
  459. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  460. struct iwl_rxq_sync_notification *notif;
  461. struct iwl_mvm_internal_rxq_notif *internal_notif;
  462. notif = (void *)pkt->data;
  463. internal_notif = (void *)notif->payload;
  464. if (internal_notif->sync) {
  465. if (mvm->queue_sync_cookie != internal_notif->cookie) {
  466. WARN_ONCE(1,
  467. "Received expired RX queue sync message\n");
  468. return;
  469. }
  470. if (!atomic_dec_return(&mvm->queue_sync_counter))
  471. wake_up(&mvm->rx_sync_waitq);
  472. }
  473. switch (internal_notif->type) {
  474. case IWL_MVM_RXQ_EMPTY:
  475. break;
  476. case IWL_MVM_RXQ_NOTIF_DEL_BA:
  477. iwl_mvm_del_ba(mvm, queue, (void *)internal_notif->data);
  478. break;
  479. default:
  480. WARN_ONCE(1, "Invalid identifier %d", internal_notif->type);
  481. }
  482. }
  483. /*
  484. * Returns true if the MPDU was buffered\dropped, false if it should be passed
  485. * to upper layer.
  486. */
  487. static bool iwl_mvm_reorder(struct iwl_mvm *mvm,
  488. struct napi_struct *napi,
  489. int queue,
  490. struct ieee80211_sta *sta,
  491. struct sk_buff *skb,
  492. struct iwl_rx_mpdu_desc *desc)
  493. {
  494. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  495. struct iwl_mvm_sta *mvm_sta;
  496. struct iwl_mvm_baid_data *baid_data;
  497. struct iwl_mvm_reorder_buffer *buffer;
  498. struct sk_buff *tail;
  499. u32 reorder = le32_to_cpu(desc->reorder_data);
  500. bool amsdu = desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU;
  501. bool last_subframe =
  502. desc->amsdu_info & IWL_RX_MPDU_AMSDU_LAST_SUBFRAME;
  503. u8 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  504. u8 sub_frame_idx = desc->amsdu_info &
  505. IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
  506. int index;
  507. u16 nssn, sn;
  508. u8 baid;
  509. baid = (reorder & IWL_RX_MPDU_REORDER_BAID_MASK) >>
  510. IWL_RX_MPDU_REORDER_BAID_SHIFT;
  511. /*
  512. * This also covers the case of receiving a Block Ack Request
  513. * outside a BA session; we'll pass it to mac80211 and that
  514. * then sends a delBA action frame.
  515. */
  516. if (baid == IWL_RX_REORDER_DATA_INVALID_BAID)
  517. return false;
  518. /* no sta yet */
  519. if (WARN_ON(IS_ERR_OR_NULL(sta)))
  520. return false;
  521. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  522. /* not a data packet or a bar */
  523. if (!ieee80211_is_back_req(hdr->frame_control) &&
  524. (!ieee80211_is_data_qos(hdr->frame_control) ||
  525. is_multicast_ether_addr(hdr->addr1)))
  526. return false;
  527. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  528. return false;
  529. baid_data = rcu_dereference(mvm->baid_map[baid]);
  530. if (WARN(!baid_data,
  531. "Received baid %d, but no data exists for this BAID\n", baid))
  532. return false;
  533. if (WARN(tid != baid_data->tid || mvm_sta->sta_id != baid_data->sta_id,
  534. "baid 0x%x is mapped to sta:%d tid:%d, but was received for sta:%d tid:%d\n",
  535. baid, baid_data->sta_id, baid_data->tid, mvm_sta->sta_id,
  536. tid))
  537. return false;
  538. nssn = reorder & IWL_RX_MPDU_REORDER_NSSN_MASK;
  539. sn = (reorder & IWL_RX_MPDU_REORDER_SN_MASK) >>
  540. IWL_RX_MPDU_REORDER_SN_SHIFT;
  541. buffer = &baid_data->reorder_buf[queue];
  542. spin_lock_bh(&buffer->lock);
  543. if (ieee80211_is_back_req(hdr->frame_control)) {
  544. iwl_mvm_release_frames(mvm, sta, napi, buffer, nssn);
  545. goto drop;
  546. }
  547. /*
  548. * If there was a significant jump in the nssn - adjust.
  549. * If the SN is smaller than the NSSN it might need to first go into
  550. * the reorder buffer, in which case we just release up to it and the
  551. * rest of the function will take of storing it and releasing up to the
  552. * nssn
  553. */
  554. if (!iwl_mvm_is_sn_less(nssn, buffer->head_sn + buffer->buf_size,
  555. buffer->buf_size)) {
  556. u16 min_sn = ieee80211_sn_less(sn, nssn) ? sn : nssn;
  557. iwl_mvm_release_frames(mvm, sta, napi, buffer, min_sn);
  558. }
  559. /* drop any oudated packets */
  560. if (ieee80211_sn_less(sn, buffer->head_sn))
  561. goto drop;
  562. /* release immediately if allowed by nssn and no stored frames */
  563. if (!buffer->num_stored && ieee80211_sn_less(sn, nssn)) {
  564. if (iwl_mvm_is_sn_less(buffer->head_sn, nssn,
  565. buffer->buf_size) &&
  566. (!amsdu || last_subframe))
  567. buffer->head_sn = nssn;
  568. /* No need to update AMSDU last SN - we are moving the head */
  569. spin_unlock_bh(&buffer->lock);
  570. return false;
  571. }
  572. index = sn % buffer->buf_size;
  573. /*
  574. * Check if we already stored this frame
  575. * As AMSDU is either received or not as whole, logic is simple:
  576. * If we have frames in that position in the buffer and the last frame
  577. * originated from AMSDU had a different SN then it is a retransmission.
  578. * If it is the same SN then if the subframe index is incrementing it
  579. * is the same AMSDU - otherwise it is a retransmission.
  580. */
  581. tail = skb_peek_tail(&buffer->entries[index]);
  582. if (tail && !amsdu)
  583. goto drop;
  584. else if (tail && (sn != buffer->last_amsdu ||
  585. buffer->last_sub_index >= sub_frame_idx))
  586. goto drop;
  587. /* put in reorder buffer */
  588. __skb_queue_tail(&buffer->entries[index], skb);
  589. buffer->num_stored++;
  590. buffer->reorder_time[index] = jiffies;
  591. if (amsdu) {
  592. buffer->last_amsdu = sn;
  593. buffer->last_sub_index = sub_frame_idx;
  594. }
  595. /*
  596. * We cannot trust NSSN for AMSDU sub-frames that are not the last.
  597. * The reason is that NSSN advances on the first sub-frame, and may
  598. * cause the reorder buffer to advance before all the sub-frames arrive.
  599. * Example: reorder buffer contains SN 0 & 2, and we receive AMSDU with
  600. * SN 1. NSSN for first sub frame will be 3 with the result of driver
  601. * releasing SN 0,1, 2. When sub-frame 1 arrives - reorder buffer is
  602. * already ahead and it will be dropped.
  603. * If the last sub-frame is not on this queue - we will get frame
  604. * release notification with up to date NSSN.
  605. */
  606. if (!amsdu || last_subframe)
  607. iwl_mvm_release_frames(mvm, sta, napi, buffer, nssn);
  608. spin_unlock_bh(&buffer->lock);
  609. return true;
  610. drop:
  611. kfree_skb(skb);
  612. spin_unlock_bh(&buffer->lock);
  613. return true;
  614. }
  615. static void iwl_mvm_agg_rx_received(struct iwl_mvm *mvm, u8 baid)
  616. {
  617. unsigned long now = jiffies;
  618. unsigned long timeout;
  619. struct iwl_mvm_baid_data *data;
  620. rcu_read_lock();
  621. data = rcu_dereference(mvm->baid_map[baid]);
  622. if (WARN_ON(!data))
  623. goto out;
  624. if (!data->timeout)
  625. goto out;
  626. timeout = data->timeout;
  627. /*
  628. * Do not update last rx all the time to avoid cache bouncing
  629. * between the rx queues.
  630. * Update it every timeout. Worst case is the session will
  631. * expire after ~ 2 * timeout, which doesn't matter that much.
  632. */
  633. if (time_before(data->last_rx + TU_TO_JIFFIES(timeout), now))
  634. /* Update is atomic */
  635. data->last_rx = now;
  636. out:
  637. rcu_read_unlock();
  638. }
  639. void iwl_mvm_rx_mpdu_mq(struct iwl_mvm *mvm, struct napi_struct *napi,
  640. struct iwl_rx_cmd_buffer *rxb, int queue)
  641. {
  642. struct ieee80211_rx_status *rx_status;
  643. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  644. struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
  645. struct ieee80211_hdr *hdr = (void *)(pkt->data + sizeof(*desc));
  646. u32 len = le16_to_cpu(desc->mpdu_len);
  647. u32 rate_n_flags = le32_to_cpu(desc->rate_n_flags);
  648. u16 phy_info = le16_to_cpu(desc->phy_info);
  649. struct ieee80211_sta *sta = NULL;
  650. struct sk_buff *skb;
  651. u8 crypt_len = 0;
  652. /* Dont use dev_alloc_skb(), we'll have enough headroom once
  653. * ieee80211_hdr pulled.
  654. */
  655. skb = alloc_skb(128, GFP_ATOMIC);
  656. if (!skb) {
  657. IWL_ERR(mvm, "alloc_skb failed\n");
  658. return;
  659. }
  660. rx_status = IEEE80211_SKB_RXCB(skb);
  661. if (iwl_mvm_rx_crypto(mvm, hdr, rx_status, desc, queue, &crypt_len)) {
  662. kfree_skb(skb);
  663. return;
  664. }
  665. /*
  666. * Keep packets with CRC errors (and with overrun) for monitor mode
  667. * (otherwise the firmware discards them) but mark them as bad.
  668. */
  669. if (!(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_CRC_OK)) ||
  670. !(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_OVERRUN_OK))) {
  671. IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n",
  672. le16_to_cpu(desc->status));
  673. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  674. }
  675. /* set the preamble flag if appropriate */
  676. if (phy_info & IWL_RX_MPDU_PHY_SHORT_PREAMBLE)
  677. rx_status->flag |= RX_FLAG_SHORTPRE;
  678. if (likely(!(phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD))) {
  679. rx_status->mactime = le64_to_cpu(desc->tsf_on_air_rise);
  680. /* TSF as indicated by the firmware is at INA time */
  681. rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
  682. }
  683. rx_status->device_timestamp = le32_to_cpu(desc->gp2_on_air_rise);
  684. rx_status->band = desc->channel > 14 ? NL80211_BAND_5GHZ :
  685. NL80211_BAND_2GHZ;
  686. rx_status->freq = ieee80211_channel_to_frequency(desc->channel,
  687. rx_status->band);
  688. iwl_mvm_get_signal_strength(mvm, desc, rx_status);
  689. /* update aggregation data for monitor sake on default queue */
  690. if (!queue && (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
  691. bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
  692. rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
  693. rx_status->ampdu_reference = mvm->ampdu_ref;
  694. /* toggle is switched whenever new aggregation starts */
  695. if (toggle_bit != mvm->ampdu_toggle) {
  696. mvm->ampdu_ref++;
  697. mvm->ampdu_toggle = toggle_bit;
  698. }
  699. }
  700. rcu_read_lock();
  701. if (le16_to_cpu(desc->status) & IWL_RX_MPDU_STATUS_SRC_STA_FOUND) {
  702. u8 id = desc->sta_id_flags & IWL_RX_MPDU_SIF_STA_ID_MASK;
  703. if (!WARN_ON_ONCE(id >= IWL_MVM_STATION_COUNT)) {
  704. sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
  705. if (IS_ERR(sta))
  706. sta = NULL;
  707. }
  708. } else if (!is_multicast_ether_addr(hdr->addr2)) {
  709. /*
  710. * This is fine since we prevent two stations with the same
  711. * address from being added.
  712. */
  713. sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
  714. }
  715. if (sta) {
  716. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  717. struct ieee80211_vif *tx_blocked_vif =
  718. rcu_dereference(mvm->csa_tx_blocked_vif);
  719. u8 baid = (u8)((le32_to_cpu(desc->reorder_data) &
  720. IWL_RX_MPDU_REORDER_BAID_MASK) >>
  721. IWL_RX_MPDU_REORDER_BAID_SHIFT);
  722. /*
  723. * We have tx blocked stations (with CS bit). If we heard
  724. * frames from a blocked station on a new channel we can
  725. * TX to it again.
  726. */
  727. if (unlikely(tx_blocked_vif) &&
  728. tx_blocked_vif == mvmsta->vif) {
  729. struct iwl_mvm_vif *mvmvif =
  730. iwl_mvm_vif_from_mac80211(tx_blocked_vif);
  731. if (mvmvif->csa_target_freq == rx_status->freq)
  732. iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
  733. false);
  734. }
  735. rs_update_last_rssi(mvm, &mvmsta->lq_sta, rx_status);
  736. if (iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_RSSI) &&
  737. ieee80211_is_beacon(hdr->frame_control)) {
  738. struct iwl_fw_dbg_trigger_tlv *trig;
  739. struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
  740. bool trig_check;
  741. s32 rssi;
  742. trig = iwl_fw_dbg_get_trigger(mvm->fw,
  743. FW_DBG_TRIGGER_RSSI);
  744. rssi_trig = (void *)trig->data;
  745. rssi = le32_to_cpu(rssi_trig->rssi);
  746. trig_check =
  747. iwl_fw_dbg_trigger_check_stop(mvm, mvmsta->vif,
  748. trig);
  749. if (trig_check && rx_status->signal < rssi)
  750. iwl_mvm_fw_dbg_collect_trig(mvm, trig, NULL);
  751. }
  752. if (ieee80211_is_data(hdr->frame_control))
  753. iwl_mvm_rx_csum(sta, skb, desc);
  754. if (iwl_mvm_is_dup(sta, queue, rx_status, hdr, desc)) {
  755. kfree_skb(skb);
  756. rcu_read_unlock();
  757. return;
  758. }
  759. /*
  760. * Our hardware de-aggregates AMSDUs but copies the mac header
  761. * as it to the de-aggregated MPDUs. We need to turn off the
  762. * AMSDU bit in the QoS control ourselves.
  763. */
  764. if ((desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU) &&
  765. !WARN_ON(!ieee80211_is_data_qos(hdr->frame_control))) {
  766. u8 *qc = ieee80211_get_qos_ctl(hdr);
  767. *qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  768. if (!(desc->amsdu_info &
  769. IWL_RX_MPDU_AMSDU_LAST_SUBFRAME))
  770. rx_status->flag |= RX_FLAG_AMSDU_MORE;
  771. }
  772. if (baid != IWL_RX_REORDER_DATA_INVALID_BAID)
  773. iwl_mvm_agg_rx_received(mvm, baid);
  774. }
  775. /* Set up the HT phy flags */
  776. switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
  777. case RATE_MCS_CHAN_WIDTH_20:
  778. break;
  779. case RATE_MCS_CHAN_WIDTH_40:
  780. rx_status->flag |= RX_FLAG_40MHZ;
  781. break;
  782. case RATE_MCS_CHAN_WIDTH_80:
  783. rx_status->vht_flag |= RX_VHT_FLAG_80MHZ;
  784. break;
  785. case RATE_MCS_CHAN_WIDTH_160:
  786. rx_status->vht_flag |= RX_VHT_FLAG_160MHZ;
  787. break;
  788. }
  789. if (rate_n_flags & RATE_MCS_SGI_MSK)
  790. rx_status->flag |= RX_FLAG_SHORT_GI;
  791. if (rate_n_flags & RATE_HT_MCS_GF_MSK)
  792. rx_status->flag |= RX_FLAG_HT_GF;
  793. if (rate_n_flags & RATE_MCS_LDPC_MSK)
  794. rx_status->flag |= RX_FLAG_LDPC;
  795. if (rate_n_flags & RATE_MCS_HT_MSK) {
  796. u8 stbc = (rate_n_flags & RATE_MCS_HT_STBC_MSK) >>
  797. RATE_MCS_STBC_POS;
  798. rx_status->flag |= RX_FLAG_HT;
  799. rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
  800. rx_status->flag |= stbc << RX_FLAG_STBC_SHIFT;
  801. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  802. u8 stbc = (rate_n_flags & RATE_MCS_VHT_STBC_MSK) >>
  803. RATE_MCS_STBC_POS;
  804. rx_status->vht_nss =
  805. ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
  806. RATE_VHT_MCS_NSS_POS) + 1;
  807. rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
  808. rx_status->flag |= RX_FLAG_VHT;
  809. rx_status->flag |= stbc << RX_FLAG_STBC_SHIFT;
  810. if (rate_n_flags & RATE_MCS_BF_MSK)
  811. rx_status->vht_flag |= RX_VHT_FLAG_BF;
  812. } else {
  813. rx_status->rate_idx =
  814. iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
  815. rx_status->band);
  816. }
  817. /* management stuff on default queue */
  818. if (!queue) {
  819. if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
  820. ieee80211_is_probe_resp(hdr->frame_control)) &&
  821. mvm->sched_scan_pass_all ==
  822. SCHED_SCAN_PASS_ALL_ENABLED))
  823. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
  824. if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
  825. ieee80211_is_probe_resp(hdr->frame_control)))
  826. rx_status->boottime_ns = ktime_get_boot_ns();
  827. }
  828. iwl_mvm_create_skb(skb, hdr, len, crypt_len, rxb);
  829. if (!iwl_mvm_reorder(mvm, napi, queue, sta, skb, desc))
  830. iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, queue, sta);
  831. rcu_read_unlock();
  832. }
  833. void iwl_mvm_rx_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
  834. struct iwl_rx_cmd_buffer *rxb, int queue)
  835. {
  836. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  837. struct iwl_frame_release *release = (void *)pkt->data;
  838. struct ieee80211_sta *sta;
  839. struct iwl_mvm_reorder_buffer *reorder_buf;
  840. struct iwl_mvm_baid_data *ba_data;
  841. int baid = release->baid;
  842. IWL_DEBUG_HT(mvm, "Frame release notification for BAID %u, NSSN %d\n",
  843. release->baid, le16_to_cpu(release->nssn));
  844. if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
  845. return;
  846. rcu_read_lock();
  847. ba_data = rcu_dereference(mvm->baid_map[baid]);
  848. if (WARN_ON_ONCE(!ba_data))
  849. goto out;
  850. sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
  851. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
  852. goto out;
  853. reorder_buf = &ba_data->reorder_buf[queue];
  854. spin_lock_bh(&reorder_buf->lock);
  855. iwl_mvm_release_frames(mvm, sta, napi, reorder_buf,
  856. le16_to_cpu(release->nssn));
  857. spin_unlock_bh(&reorder_buf->lock);
  858. out:
  859. rcu_read_unlock();
  860. }