rxmq.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. atomic_dec(&mvm->queue_sync_counter);
  468. }
  469. switch (internal_notif->type) {
  470. case IWL_MVM_RXQ_EMPTY:
  471. break;
  472. case IWL_MVM_RXQ_NOTIF_DEL_BA:
  473. iwl_mvm_del_ba(mvm, queue, (void *)internal_notif->data);
  474. break;
  475. default:
  476. WARN_ONCE(1, "Invalid identifier %d", internal_notif->type);
  477. }
  478. }
  479. /*
  480. * Returns true if the MPDU was buffered\dropped, false if it should be passed
  481. * to upper layer.
  482. */
  483. static bool iwl_mvm_reorder(struct iwl_mvm *mvm,
  484. struct napi_struct *napi,
  485. int queue,
  486. struct ieee80211_sta *sta,
  487. struct sk_buff *skb,
  488. struct iwl_rx_mpdu_desc *desc)
  489. {
  490. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  491. struct iwl_mvm_sta *mvm_sta;
  492. struct iwl_mvm_baid_data *baid_data;
  493. struct iwl_mvm_reorder_buffer *buffer;
  494. struct sk_buff *tail;
  495. u32 reorder = le32_to_cpu(desc->reorder_data);
  496. bool amsdu = desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU;
  497. bool last_subframe =
  498. desc->amsdu_info & IWL_RX_MPDU_AMSDU_LAST_SUBFRAME;
  499. u8 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  500. u8 sub_frame_idx = desc->amsdu_info &
  501. IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
  502. int index;
  503. u16 nssn, sn;
  504. u8 baid;
  505. baid = (reorder & IWL_RX_MPDU_REORDER_BAID_MASK) >>
  506. IWL_RX_MPDU_REORDER_BAID_SHIFT;
  507. /*
  508. * This also covers the case of receiving a Block Ack Request
  509. * outside a BA session; we'll pass it to mac80211 and that
  510. * then sends a delBA action frame.
  511. */
  512. if (baid == IWL_RX_REORDER_DATA_INVALID_BAID)
  513. return false;
  514. /* no sta yet */
  515. if (WARN_ON(IS_ERR_OR_NULL(sta)))
  516. return false;
  517. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  518. /* not a data packet or a bar */
  519. if (!ieee80211_is_back_req(hdr->frame_control) &&
  520. (!ieee80211_is_data_qos(hdr->frame_control) ||
  521. is_multicast_ether_addr(hdr->addr1)))
  522. return false;
  523. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  524. return false;
  525. baid_data = rcu_dereference(mvm->baid_map[baid]);
  526. if (WARN(!baid_data,
  527. "Received baid %d, but no data exists for this BAID\n", baid))
  528. return false;
  529. if (WARN(tid != baid_data->tid || mvm_sta->sta_id != baid_data->sta_id,
  530. "baid 0x%x is mapped to sta:%d tid:%d, but was received for sta:%d tid:%d\n",
  531. baid, baid_data->sta_id, baid_data->tid, mvm_sta->sta_id,
  532. tid))
  533. return false;
  534. nssn = reorder & IWL_RX_MPDU_REORDER_NSSN_MASK;
  535. sn = (reorder & IWL_RX_MPDU_REORDER_SN_MASK) >>
  536. IWL_RX_MPDU_REORDER_SN_SHIFT;
  537. buffer = &baid_data->reorder_buf[queue];
  538. spin_lock_bh(&buffer->lock);
  539. if (ieee80211_is_back_req(hdr->frame_control)) {
  540. iwl_mvm_release_frames(mvm, sta, napi, buffer, nssn);
  541. goto drop;
  542. }
  543. /*
  544. * If there was a significant jump in the nssn - adjust.
  545. * If the SN is smaller than the NSSN it might need to first go into
  546. * the reorder buffer, in which case we just release up to it and the
  547. * rest of the function will take of storing it and releasing up to the
  548. * nssn
  549. */
  550. if (!iwl_mvm_is_sn_less(nssn, buffer->head_sn + buffer->buf_size,
  551. buffer->buf_size)) {
  552. u16 min_sn = ieee80211_sn_less(sn, nssn) ? sn : nssn;
  553. iwl_mvm_release_frames(mvm, sta, napi, buffer, min_sn);
  554. }
  555. /* drop any oudated packets */
  556. if (ieee80211_sn_less(sn, buffer->head_sn))
  557. goto drop;
  558. /* release immediately if allowed by nssn and no stored frames */
  559. if (!buffer->num_stored && ieee80211_sn_less(sn, nssn)) {
  560. if (iwl_mvm_is_sn_less(buffer->head_sn, nssn,
  561. buffer->buf_size) &&
  562. (!amsdu || last_subframe))
  563. buffer->head_sn = nssn;
  564. /* No need to update AMSDU last SN - we are moving the head */
  565. spin_unlock_bh(&buffer->lock);
  566. return false;
  567. }
  568. index = sn % buffer->buf_size;
  569. /*
  570. * Check if we already stored this frame
  571. * As AMSDU is either received or not as whole, logic is simple:
  572. * If we have frames in that position in the buffer and the last frame
  573. * originated from AMSDU had a different SN then it is a retransmission.
  574. * If it is the same SN then if the subframe index is incrementing it
  575. * is the same AMSDU - otherwise it is a retransmission.
  576. */
  577. tail = skb_peek_tail(&buffer->entries[index]);
  578. if (tail && !amsdu)
  579. goto drop;
  580. else if (tail && (sn != buffer->last_amsdu ||
  581. buffer->last_sub_index >= sub_frame_idx))
  582. goto drop;
  583. /* put in reorder buffer */
  584. __skb_queue_tail(&buffer->entries[index], skb);
  585. buffer->num_stored++;
  586. buffer->reorder_time[index] = jiffies;
  587. if (amsdu) {
  588. buffer->last_amsdu = sn;
  589. buffer->last_sub_index = sub_frame_idx;
  590. }
  591. /*
  592. * We cannot trust NSSN for AMSDU sub-frames that are not the last.
  593. * The reason is that NSSN advances on the first sub-frame, and may
  594. * cause the reorder buffer to advance before all the sub-frames arrive.
  595. * Example: reorder buffer contains SN 0 & 2, and we receive AMSDU with
  596. * SN 1. NSSN for first sub frame will be 3 with the result of driver
  597. * releasing SN 0,1, 2. When sub-frame 1 arrives - reorder buffer is
  598. * already ahead and it will be dropped.
  599. * If the last sub-frame is not on this queue - we will get frame
  600. * release notification with up to date NSSN.
  601. */
  602. if (!amsdu || last_subframe)
  603. iwl_mvm_release_frames(mvm, sta, napi, buffer, nssn);
  604. spin_unlock_bh(&buffer->lock);
  605. return true;
  606. drop:
  607. kfree_skb(skb);
  608. spin_unlock_bh(&buffer->lock);
  609. return true;
  610. }
  611. static void iwl_mvm_agg_rx_received(struct iwl_mvm *mvm, u8 baid)
  612. {
  613. unsigned long now = jiffies;
  614. unsigned long timeout;
  615. struct iwl_mvm_baid_data *data;
  616. rcu_read_lock();
  617. data = rcu_dereference(mvm->baid_map[baid]);
  618. if (WARN_ON(!data))
  619. goto out;
  620. if (!data->timeout)
  621. goto out;
  622. timeout = data->timeout;
  623. /*
  624. * Do not update last rx all the time to avoid cache bouncing
  625. * between the rx queues.
  626. * Update it every timeout. Worst case is the session will
  627. * expire after ~ 2 * timeout, which doesn't matter that much.
  628. */
  629. if (time_before(data->last_rx + TU_TO_JIFFIES(timeout), now))
  630. /* Update is atomic */
  631. data->last_rx = now;
  632. out:
  633. rcu_read_unlock();
  634. }
  635. void iwl_mvm_rx_mpdu_mq(struct iwl_mvm *mvm, struct napi_struct *napi,
  636. struct iwl_rx_cmd_buffer *rxb, int queue)
  637. {
  638. struct ieee80211_rx_status *rx_status;
  639. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  640. struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
  641. struct ieee80211_hdr *hdr = (void *)(pkt->data + sizeof(*desc));
  642. u32 len = le16_to_cpu(desc->mpdu_len);
  643. u32 rate_n_flags = le32_to_cpu(desc->rate_n_flags);
  644. u16 phy_info = le16_to_cpu(desc->phy_info);
  645. struct ieee80211_sta *sta = NULL;
  646. struct sk_buff *skb;
  647. u8 crypt_len = 0;
  648. /* Dont use dev_alloc_skb(), we'll have enough headroom once
  649. * ieee80211_hdr pulled.
  650. */
  651. skb = alloc_skb(128, GFP_ATOMIC);
  652. if (!skb) {
  653. IWL_ERR(mvm, "alloc_skb failed\n");
  654. return;
  655. }
  656. rx_status = IEEE80211_SKB_RXCB(skb);
  657. if (iwl_mvm_rx_crypto(mvm, hdr, rx_status, desc, queue, &crypt_len)) {
  658. kfree_skb(skb);
  659. return;
  660. }
  661. /*
  662. * Keep packets with CRC errors (and with overrun) for monitor mode
  663. * (otherwise the firmware discards them) but mark them as bad.
  664. */
  665. if (!(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_CRC_OK)) ||
  666. !(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_OVERRUN_OK))) {
  667. IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n",
  668. le16_to_cpu(desc->status));
  669. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  670. }
  671. /* set the preamble flag if appropriate */
  672. if (phy_info & IWL_RX_MPDU_PHY_SHORT_PREAMBLE)
  673. rx_status->flag |= RX_FLAG_SHORTPRE;
  674. if (likely(!(phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD))) {
  675. rx_status->mactime = le64_to_cpu(desc->tsf_on_air_rise);
  676. /* TSF as indicated by the firmware is at INA time */
  677. rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
  678. }
  679. rx_status->device_timestamp = le32_to_cpu(desc->gp2_on_air_rise);
  680. rx_status->band = desc->channel > 14 ? NL80211_BAND_5GHZ :
  681. NL80211_BAND_2GHZ;
  682. rx_status->freq = ieee80211_channel_to_frequency(desc->channel,
  683. rx_status->band);
  684. iwl_mvm_get_signal_strength(mvm, desc, rx_status);
  685. /* update aggregation data for monitor sake on default queue */
  686. if (!queue && (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
  687. bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
  688. rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
  689. rx_status->ampdu_reference = mvm->ampdu_ref;
  690. /* toggle is switched whenever new aggregation starts */
  691. if (toggle_bit != mvm->ampdu_toggle) {
  692. mvm->ampdu_ref++;
  693. mvm->ampdu_toggle = toggle_bit;
  694. }
  695. }
  696. rcu_read_lock();
  697. if (le16_to_cpu(desc->status) & IWL_RX_MPDU_STATUS_SRC_STA_FOUND) {
  698. u8 id = desc->sta_id_flags & IWL_RX_MPDU_SIF_STA_ID_MASK;
  699. if (!WARN_ON_ONCE(id >= IWL_MVM_STATION_COUNT)) {
  700. sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
  701. if (IS_ERR(sta))
  702. sta = NULL;
  703. }
  704. } else if (!is_multicast_ether_addr(hdr->addr2)) {
  705. /*
  706. * This is fine since we prevent two stations with the same
  707. * address from being added.
  708. */
  709. sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
  710. }
  711. if (sta) {
  712. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  713. struct ieee80211_vif *tx_blocked_vif =
  714. rcu_dereference(mvm->csa_tx_blocked_vif);
  715. u8 baid = (u8)((le32_to_cpu(desc->reorder_data) &
  716. IWL_RX_MPDU_REORDER_BAID_MASK) >>
  717. IWL_RX_MPDU_REORDER_BAID_SHIFT);
  718. /*
  719. * We have tx blocked stations (with CS bit). If we heard
  720. * frames from a blocked station on a new channel we can
  721. * TX to it again.
  722. */
  723. if (unlikely(tx_blocked_vif) &&
  724. tx_blocked_vif == mvmsta->vif) {
  725. struct iwl_mvm_vif *mvmvif =
  726. iwl_mvm_vif_from_mac80211(tx_blocked_vif);
  727. if (mvmvif->csa_target_freq == rx_status->freq)
  728. iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
  729. false);
  730. }
  731. rs_update_last_rssi(mvm, &mvmsta->lq_sta, rx_status);
  732. if (iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_RSSI) &&
  733. ieee80211_is_beacon(hdr->frame_control)) {
  734. struct iwl_fw_dbg_trigger_tlv *trig;
  735. struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
  736. bool trig_check;
  737. s32 rssi;
  738. trig = iwl_fw_dbg_get_trigger(mvm->fw,
  739. FW_DBG_TRIGGER_RSSI);
  740. rssi_trig = (void *)trig->data;
  741. rssi = le32_to_cpu(rssi_trig->rssi);
  742. trig_check =
  743. iwl_fw_dbg_trigger_check_stop(mvm, mvmsta->vif,
  744. trig);
  745. if (trig_check && rx_status->signal < rssi)
  746. iwl_mvm_fw_dbg_collect_trig(mvm, trig, NULL);
  747. }
  748. if (ieee80211_is_data(hdr->frame_control))
  749. iwl_mvm_rx_csum(sta, skb, desc);
  750. if (iwl_mvm_is_nonagg_dup(sta, queue, rx_status, hdr, desc)) {
  751. kfree_skb(skb);
  752. rcu_read_unlock();
  753. return;
  754. }
  755. /*
  756. * Our hardware de-aggregates AMSDUs but copies the mac header
  757. * as it to the de-aggregated MPDUs. We need to turn off the
  758. * AMSDU bit in the QoS control ourselves.
  759. */
  760. if ((desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU) &&
  761. !WARN_ON(!ieee80211_is_data_qos(hdr->frame_control))) {
  762. u8 *qc = ieee80211_get_qos_ctl(hdr);
  763. *qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  764. if (!(desc->amsdu_info &
  765. IWL_RX_MPDU_AMSDU_LAST_SUBFRAME))
  766. rx_status->flag |= RX_FLAG_AMSDU_MORE;
  767. }
  768. if (baid != IWL_RX_REORDER_DATA_INVALID_BAID)
  769. iwl_mvm_agg_rx_received(mvm, baid);
  770. }
  771. /* Set up the HT phy flags */
  772. switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
  773. case RATE_MCS_CHAN_WIDTH_20:
  774. break;
  775. case RATE_MCS_CHAN_WIDTH_40:
  776. rx_status->flag |= RX_FLAG_40MHZ;
  777. break;
  778. case RATE_MCS_CHAN_WIDTH_80:
  779. rx_status->vht_flag |= RX_VHT_FLAG_80MHZ;
  780. break;
  781. case RATE_MCS_CHAN_WIDTH_160:
  782. rx_status->vht_flag |= RX_VHT_FLAG_160MHZ;
  783. break;
  784. }
  785. if (rate_n_flags & RATE_MCS_SGI_MSK)
  786. rx_status->flag |= RX_FLAG_SHORT_GI;
  787. if (rate_n_flags & RATE_HT_MCS_GF_MSK)
  788. rx_status->flag |= RX_FLAG_HT_GF;
  789. if (rate_n_flags & RATE_MCS_LDPC_MSK)
  790. rx_status->flag |= RX_FLAG_LDPC;
  791. if (rate_n_flags & RATE_MCS_HT_MSK) {
  792. u8 stbc = (rate_n_flags & RATE_MCS_HT_STBC_MSK) >>
  793. RATE_MCS_STBC_POS;
  794. rx_status->flag |= RX_FLAG_HT;
  795. rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
  796. rx_status->flag |= stbc << RX_FLAG_STBC_SHIFT;
  797. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  798. u8 stbc = (rate_n_flags & RATE_MCS_VHT_STBC_MSK) >>
  799. RATE_MCS_STBC_POS;
  800. rx_status->vht_nss =
  801. ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
  802. RATE_VHT_MCS_NSS_POS) + 1;
  803. rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
  804. rx_status->flag |= RX_FLAG_VHT;
  805. rx_status->flag |= stbc << RX_FLAG_STBC_SHIFT;
  806. if (rate_n_flags & RATE_MCS_BF_MSK)
  807. rx_status->vht_flag |= RX_VHT_FLAG_BF;
  808. } else {
  809. rx_status->rate_idx =
  810. iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
  811. rx_status->band);
  812. }
  813. /* management stuff on default queue */
  814. if (!queue) {
  815. if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
  816. ieee80211_is_probe_resp(hdr->frame_control)) &&
  817. mvm->sched_scan_pass_all ==
  818. SCHED_SCAN_PASS_ALL_ENABLED))
  819. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
  820. if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
  821. ieee80211_is_probe_resp(hdr->frame_control)))
  822. rx_status->boottime_ns = ktime_get_boot_ns();
  823. }
  824. iwl_mvm_create_skb(skb, hdr, len, crypt_len, rxb);
  825. if (!iwl_mvm_reorder(mvm, napi, queue, sta, skb, desc))
  826. iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, queue, sta);
  827. rcu_read_unlock();
  828. }
  829. void iwl_mvm_rx_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
  830. struct iwl_rx_cmd_buffer *rxb, int queue)
  831. {
  832. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  833. struct iwl_frame_release *release = (void *)pkt->data;
  834. struct ieee80211_sta *sta;
  835. struct iwl_mvm_reorder_buffer *reorder_buf;
  836. struct iwl_mvm_baid_data *ba_data;
  837. int baid = release->baid;
  838. IWL_DEBUG_HT(mvm, "Frame release notification for BAID %u, NSSN %d\n",
  839. release->baid, le16_to_cpu(release->nssn));
  840. if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
  841. return;
  842. rcu_read_lock();
  843. ba_data = rcu_dereference(mvm->baid_map[baid]);
  844. if (WARN_ON_ONCE(!ba_data))
  845. goto out;
  846. sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
  847. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
  848. goto out;
  849. reorder_buf = &ba_data->reorder_buf[queue];
  850. spin_lock_bh(&reorder_buf->lock);
  851. iwl_mvm_release_frames(mvm, sta, napi, reorder_buf,
  852. le16_to_cpu(release->nssn));
  853. spin_unlock_bh(&reorder_buf->lock);
  854. out:
  855. rcu_read_unlock();
  856. }