rxmq.c 30 KB

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