rxmq.c 30 KB

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