1 /******************************************************************************
3 * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
4 * Copyright(c) 2015 Intel Deutschland GmbH
6 * Portions of this file are derived from the ipw3945 project, as well
7 * as portionhelp of the ieee80211 subsystem header files.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * Intel Linux Wireless <linuxwifi@intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 *****************************************************************************/
27 #include <linux/etherdevice.h>
28 #include <linux/slab.h>
29 #include <linux/sched.h>
30 #include <net/mac80211.h>
31 #include <asm/unaligned.h>
33 #include "iwl-trans.h"
39 /******************************************************************************
41 * Generic RX handler implementations
43 ******************************************************************************/
45 static void iwlagn_rx_reply_error(struct iwl_priv *priv,
46 struct iwl_rx_cmd_buffer *rxb)
48 struct iwl_rx_packet *pkt = rxb_addr(rxb);
49 struct iwl_error_resp *err_resp = (void *)pkt->data;
51 IWL_ERR(priv, "Error Reply type 0x%08X cmd REPLY_ERROR (0x%02X) "
52 "seq 0x%04X ser 0x%08X\n",
53 le32_to_cpu(err_resp->error_type),
55 le16_to_cpu(err_resp->bad_cmd_seq_num),
56 le32_to_cpu(err_resp->error_info));
59 static void iwlagn_rx_csa(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb)
61 struct iwl_rx_packet *pkt = rxb_addr(rxb);
62 struct iwl_csa_notification *csa = (void *)pkt->data;
65 * See iwlagn_mac_channel_switch.
67 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
68 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
70 if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
73 if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
74 rxon->channel = csa->channel;
75 ctx->staging.channel = csa->channel;
76 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
77 le16_to_cpu(csa->channel));
78 iwl_chswitch_done(priv, true);
80 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
81 le16_to_cpu(csa->channel));
82 iwl_chswitch_done(priv, false);
87 static void iwlagn_rx_spectrum_measure_notif(struct iwl_priv *priv,
88 struct iwl_rx_cmd_buffer *rxb)
90 struct iwl_rx_packet *pkt = rxb_addr(rxb);
91 struct iwl_spectrum_notification *report = (void *)pkt->data;
95 "Spectrum Measure Notification: Start\n");
99 memcpy(&priv->measure_report, report, sizeof(*report));
100 priv->measurement_status |= MEASUREMENT_READY;
103 static void iwlagn_rx_pm_sleep_notif(struct iwl_priv *priv,
104 struct iwl_rx_cmd_buffer *rxb)
106 #ifdef CONFIG_IWLWIFI_DEBUG
107 struct iwl_rx_packet *pkt = rxb_addr(rxb);
108 struct iwl_sleep_notification *sleep = (void *)pkt->data;
109 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
110 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
114 static void iwlagn_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
115 struct iwl_rx_cmd_buffer *rxb)
117 struct iwl_rx_packet *pkt = rxb_addr(rxb);
118 u32 __maybe_unused len = iwl_rx_packet_len(pkt);
119 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
120 "notification for PM_DEBUG_STATISTIC_NOTIFIC:\n", len);
121 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->data, len);
124 static void iwlagn_rx_beacon_notif(struct iwl_priv *priv,
125 struct iwl_rx_cmd_buffer *rxb)
127 struct iwl_rx_packet *pkt = rxb_addr(rxb);
128 struct iwlagn_beacon_notif *beacon = (void *)pkt->data;
129 #ifdef CONFIG_IWLWIFI_DEBUG
130 u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
131 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
133 IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
134 "tsf:0x%.8x%.8x rate:%d\n",
135 status & TX_STATUS_MSK,
136 beacon->beacon_notify_hdr.failure_frame,
137 le32_to_cpu(beacon->ibss_mgr_status),
138 le32_to_cpu(beacon->high_tsf),
139 le32_to_cpu(beacon->low_tsf), rate);
142 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
146 * iwl_good_plcp_health - checks for plcp error.
148 * When the plcp error is exceeding the thresholds, reset the radio
149 * to improve the throughput.
151 static bool iwlagn_good_plcp_health(struct iwl_priv *priv,
152 struct statistics_rx_phy *cur_ofdm,
153 struct statistics_rx_ht_phy *cur_ofdm_ht,
157 int threshold = priv->plcp_delta_threshold;
159 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
160 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
164 delta = le32_to_cpu(cur_ofdm->plcp_err) -
165 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
166 le32_to_cpu(cur_ofdm_ht->plcp_err) -
167 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
169 /* Can be negative if firmware reset statistics */
173 if ((delta * 100 / msecs) > threshold) {
174 IWL_DEBUG_RADIO(priv,
175 "plcp health threshold %u delta %d msecs %u\n",
176 threshold, delta, msecs);
183 int iwl_force_rf_reset(struct iwl_priv *priv, bool external)
185 struct iwl_rf_reset *rf_reset;
187 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
190 if (!iwl_is_any_associated(priv)) {
191 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
195 rf_reset = &priv->rf_reset;
196 rf_reset->reset_request_count++;
197 if (!external && rf_reset->last_reset_jiffies &&
198 time_after(rf_reset->last_reset_jiffies +
199 IWL_DELAY_NEXT_FORCE_RF_RESET, jiffies)) {
200 IWL_DEBUG_INFO(priv, "RF reset rejected\n");
201 rf_reset->reset_reject_count++;
204 rf_reset->reset_success_count++;
205 rf_reset->last_reset_jiffies = jiffies;
208 * There is no easy and better way to force reset the radio,
209 * the only known method is switching channel which will force to
210 * reset and tune the radio.
211 * Use internal short scan (single channel) operation to should
212 * achieve this objective.
213 * Driver should reset the radio when number of consecutive missed
214 * beacon, or any other uCode error condition detected.
216 IWL_DEBUG_INFO(priv, "perform radio reset.\n");
217 iwl_internal_short_hw_scan(priv);
222 static void iwlagn_recover_from_statistics(struct iwl_priv *priv,
223 struct statistics_rx_phy *cur_ofdm,
224 struct statistics_rx_ht_phy *cur_ofdm_ht,
225 struct statistics_tx *tx,
230 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
233 msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
235 /* Only gather statistics and update time stamp when not associated */
236 if (!iwl_is_any_associated(priv))
239 /* Do not check/recover when do not have enough statistics data */
243 if (!iwlagn_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
244 iwl_force_rf_reset(priv, false);
247 /* Calculate noise level, based on measurements during network silence just
248 * before arriving beacon. This measurement can be done only if we know
249 * exactly when to expect beacons, therefore only when we're associated. */
250 static void iwlagn_rx_calc_noise(struct iwl_priv *priv)
252 struct statistics_rx_non_phy *rx_info;
253 int num_active_rx = 0;
254 int total_silence = 0;
255 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
258 rx_info = &priv->statistics.rx_non_phy;
261 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
263 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
265 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
268 total_silence += bcn_silence_a;
272 total_silence += bcn_silence_b;
276 total_silence += bcn_silence_c;
280 /* Average among active antennas */
282 last_rx_noise = (total_silence / num_active_rx) - 107;
284 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
286 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
287 bcn_silence_a, bcn_silence_b, bcn_silence_c,
291 #ifdef CONFIG_IWLWIFI_DEBUGFS
293 * based on the assumption of all statistics counter are in DWORD
294 * FIXME: This function is for debugging, do not deal with
295 * the case of counters roll-over.
297 static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
298 __le32 *max_delta, __le32 *accum, int size)
303 i < size / sizeof(__le32);
304 i++, prev++, cur++, delta++, max_delta++, accum++) {
305 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
306 *delta = cpu_to_le32(
307 le32_to_cpu(*cur) - le32_to_cpu(*prev));
308 le32_add_cpu(accum, le32_to_cpu(*delta));
309 if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
316 iwlagn_accumulative_statistics(struct iwl_priv *priv,
317 struct statistics_general_common *common,
318 struct statistics_rx_non_phy *rx_non_phy,
319 struct statistics_rx_phy *rx_ofdm,
320 struct statistics_rx_ht_phy *rx_ofdm_ht,
321 struct statistics_rx_phy *rx_cck,
322 struct statistics_tx *tx,
323 struct statistics_bt_activity *bt_activity)
325 #define ACCUM(_name) \
326 accum_stats((__le32 *)&priv->statistics._name, \
328 (__le32 *)&priv->delta_stats._name, \
329 (__le32 *)&priv->max_delta_stats._name, \
330 (__le32 *)&priv->accum_stats._name, \
345 iwlagn_accumulative_statistics(struct iwl_priv *priv,
346 struct statistics_general_common *common,
347 struct statistics_rx_non_phy *rx_non_phy,
348 struct statistics_rx_phy *rx_ofdm,
349 struct statistics_rx_ht_phy *rx_ofdm_ht,
350 struct statistics_rx_phy *rx_cck,
351 struct statistics_tx *tx,
352 struct statistics_bt_activity *bt_activity)
357 static void iwlagn_rx_statistics(struct iwl_priv *priv,
358 struct iwl_rx_cmd_buffer *rxb)
360 unsigned long stamp = jiffies;
361 const int reg_recalib_period = 60;
363 struct iwl_rx_packet *pkt = rxb_addr(rxb);
364 u32 len = iwl_rx_packet_payload_len(pkt);
366 struct statistics_general_common *common;
367 struct statistics_rx_non_phy *rx_non_phy;
368 struct statistics_rx_phy *rx_ofdm;
369 struct statistics_rx_ht_phy *rx_ofdm_ht;
370 struct statistics_rx_phy *rx_cck;
371 struct statistics_tx *tx;
372 struct statistics_bt_activity *bt_activity;
374 IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
377 spin_lock(&priv->statistics.lock);
379 if (len == sizeof(struct iwl_bt_notif_statistics)) {
380 struct iwl_bt_notif_statistics *stats;
381 stats = (void *)&pkt->data;
383 common = &stats->general.common;
384 rx_non_phy = &stats->rx.general.common;
385 rx_ofdm = &stats->rx.ofdm;
386 rx_ofdm_ht = &stats->rx.ofdm_ht;
387 rx_cck = &stats->rx.cck;
389 bt_activity = &stats->general.activity;
391 #ifdef CONFIG_IWLWIFI_DEBUGFS
392 /* handle this exception directly */
393 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
394 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
395 le32_to_cpu(stats->rx.general.num_bt_kills));
397 } else if (len == sizeof(struct iwl_notif_statistics)) {
398 struct iwl_notif_statistics *stats;
399 stats = (void *)&pkt->data;
401 common = &stats->general.common;
402 rx_non_phy = &stats->rx.general;
403 rx_ofdm = &stats->rx.ofdm;
404 rx_ofdm_ht = &stats->rx.ofdm_ht;
405 rx_cck = &stats->rx.cck;
409 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
410 len, sizeof(struct iwl_bt_notif_statistics),
411 sizeof(struct iwl_notif_statistics));
412 spin_unlock(&priv->statistics.lock);
416 change = common->temperature != priv->statistics.common.temperature ||
417 (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
418 (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
420 iwlagn_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
421 rx_ofdm_ht, rx_cck, tx, bt_activity);
423 iwlagn_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
425 priv->statistics.flag = *flag;
426 memcpy(&priv->statistics.common, common, sizeof(*common));
427 memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
428 memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
429 memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
430 memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
431 memcpy(&priv->statistics.tx, tx, sizeof(*tx));
432 #ifdef CONFIG_IWLWIFI_DEBUGFS
434 memcpy(&priv->statistics.bt_activity, bt_activity,
435 sizeof(*bt_activity));
438 priv->rx_statistics_jiffies = stamp;
440 set_bit(STATUS_STATISTICS, &priv->status);
442 /* Reschedule the statistics timer to occur in
443 * reg_recalib_period seconds to ensure we get a
444 * thermal update even if the uCode doesn't give
446 mod_timer(&priv->statistics_periodic, jiffies +
447 msecs_to_jiffies(reg_recalib_period * 1000));
449 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
450 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
451 iwlagn_rx_calc_noise(priv);
452 queue_work(priv->workqueue, &priv->run_time_calib_work);
454 if (priv->lib->temperature && change)
455 priv->lib->temperature(priv);
457 spin_unlock(&priv->statistics.lock);
460 static void iwlagn_rx_reply_statistics(struct iwl_priv *priv,
461 struct iwl_rx_cmd_buffer *rxb)
463 struct iwl_rx_packet *pkt = rxb_addr(rxb);
464 struct iwl_notif_statistics *stats = (void *)pkt->data;
466 if (le32_to_cpu(stats->flag) & UCODE_STATISTICS_CLEAR_MSK) {
467 #ifdef CONFIG_IWLWIFI_DEBUGFS
468 memset(&priv->accum_stats, 0,
469 sizeof(priv->accum_stats));
470 memset(&priv->delta_stats, 0,
471 sizeof(priv->delta_stats));
472 memset(&priv->max_delta_stats, 0,
473 sizeof(priv->max_delta_stats));
475 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
478 iwlagn_rx_statistics(priv, rxb);
481 /* Handle notification from uCode that card's power state is changing
482 * due to software, hardware, or critical temperature RFKILL */
483 static void iwlagn_rx_card_state_notif(struct iwl_priv *priv,
484 struct iwl_rx_cmd_buffer *rxb)
486 struct iwl_rx_packet *pkt = rxb_addr(rxb);
487 struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
488 u32 flags = le32_to_cpu(card_state_notif->flags);
489 unsigned long status = priv->status;
491 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
492 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
493 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
494 (flags & CT_CARD_DISABLED) ?
495 "Reached" : "Not reached");
497 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
500 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
501 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
503 iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
504 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
506 if (!(flags & RXON_CARD_DISABLED)) {
507 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
508 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
509 iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
510 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
512 if (flags & CT_CARD_DISABLED)
513 iwl_tt_enter_ct_kill(priv);
515 if (!(flags & CT_CARD_DISABLED))
516 iwl_tt_exit_ct_kill(priv);
518 if (flags & HW_CARD_DISABLED)
519 set_bit(STATUS_RF_KILL_HW, &priv->status);
521 clear_bit(STATUS_RF_KILL_HW, &priv->status);
524 if (!(flags & RXON_CARD_DISABLED))
525 iwl_scan_cancel(priv);
527 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
528 test_bit(STATUS_RF_KILL_HW, &priv->status)))
529 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
530 test_bit(STATUS_RF_KILL_HW, &priv->status));
533 static void iwlagn_rx_missed_beacon_notif(struct iwl_priv *priv,
534 struct iwl_rx_cmd_buffer *rxb)
537 struct iwl_rx_packet *pkt = rxb_addr(rxb);
538 struct iwl_missed_beacon_notif *missed_beacon = (void *)pkt->data;
540 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
541 priv->missed_beacon_threshold) {
542 IWL_DEBUG_CALIB(priv,
543 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
544 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
545 le32_to_cpu(missed_beacon->total_missed_becons),
546 le32_to_cpu(missed_beacon->num_recvd_beacons),
547 le32_to_cpu(missed_beacon->num_expected_beacons));
548 if (!test_bit(STATUS_SCANNING, &priv->status))
549 iwl_init_sensitivity(priv);
553 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
554 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
555 static void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
556 struct iwl_rx_cmd_buffer *rxb)
558 struct iwl_rx_packet *pkt = rxb_addr(rxb);
560 priv->last_phy_res_valid = true;
562 memcpy(&priv->last_phy_res, pkt->data,
563 sizeof(struct iwl_rx_phy_res));
567 * returns non-zero if packet should be dropped
569 static int iwlagn_set_decrypted_flag(struct iwl_priv *priv,
570 struct ieee80211_hdr *hdr,
572 struct ieee80211_rx_status *stats)
574 u16 fc = le16_to_cpu(hdr->frame_control);
577 * All contexts have the same setting here due to it being
578 * a module parameter, so OK to check any context.
580 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
581 RXON_FILTER_DIS_DECRYPT_MSK)
584 if (!(fc & IEEE80211_FCTL_PROTECTED))
587 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
588 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
589 case RX_RES_STATUS_SEC_TYPE_TKIP:
590 /* The uCode has got a bad phase 1 Key, pushes the packet.
591 * Decryption will be done in SW. */
592 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
593 RX_RES_STATUS_BAD_KEY_TTAK)
596 case RX_RES_STATUS_SEC_TYPE_WEP:
597 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
598 RX_RES_STATUS_BAD_ICV_MIC) {
599 /* bad ICV, the packet is destroyed since the
600 * decryption is inplace, drop it */
601 IWL_DEBUG_RX(priv, "Packet destroyed\n");
604 case RX_RES_STATUS_SEC_TYPE_CCMP:
605 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
606 RX_RES_STATUS_DECRYPT_OK) {
607 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
608 stats->flag |= RX_FLAG_DECRYPTED;
618 static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
619 struct ieee80211_hdr *hdr,
622 struct iwl_rx_cmd_buffer *rxb,
623 struct ieee80211_rx_status *stats)
626 __le16 fc = hdr->frame_control;
627 struct iwl_rxon_context *ctx;
628 unsigned int hdrlen, fraglen;
630 /* We only process data packets if the interface is open */
631 if (unlikely(!priv->is_open)) {
632 IWL_DEBUG_DROP_LIMIT(priv,
633 "Dropping packet while interface is not open.\n");
637 /* In case of HW accelerated crypto and bad decryption, drop */
638 if (!iwlwifi_mod_params.swcrypto &&
639 iwlagn_set_decrypted_flag(priv, hdr, ampdu_status, stats))
642 /* Dont use dev_alloc_skb(), we'll have enough headroom once
643 * ieee80211_hdr pulled.
645 skb = alloc_skb(128, GFP_ATOMIC);
647 IWL_ERR(priv, "alloc_skb failed\n");
650 /* If frame is small enough to fit in skb->head, pull it completely.
651 * If not, only pull ieee80211_hdr so that splice() or TCP coalesce
652 * are more efficient.
654 hdrlen = (len <= skb_tailroom(skb)) ? len : sizeof(*hdr);
656 skb_put_data(skb, hdr, hdrlen);
657 fraglen = len - hdrlen;
660 int offset = (void *)hdr + hdrlen -
661 rxb_addr(rxb) + rxb_offset(rxb);
663 skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
664 fraglen, rxb->truesize);
668 * Wake any queues that were stopped due to a passive channel tx
669 * failure. This can happen because the regulatory enforcement in
670 * the device waits for a beacon before allowing transmission,
671 * sometimes even after already having transmitted frames for the
672 * association because the new RXON may reset the information.
674 if (unlikely(ieee80211_is_beacon(fc) && priv->passive_no_rx)) {
675 for_each_context(priv, ctx) {
676 if (!ether_addr_equal(hdr->addr3,
677 ctx->active.bssid_addr))
679 iwlagn_lift_passive_no_rx(priv);
683 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
685 ieee80211_rx_napi(priv->hw, NULL, skb, priv->napi);
688 static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
692 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
693 RX_RES_STATUS_STATION_FOUND)
694 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
695 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
697 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
699 /* packet was not encrypted */
700 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
701 RX_RES_STATUS_SEC_TYPE_NONE)
704 /* packet was encrypted with unknown alg */
705 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
706 RX_RES_STATUS_SEC_TYPE_ERR)
709 /* decryption was not done in HW */
710 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
711 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
714 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
716 case RX_RES_STATUS_SEC_TYPE_CCMP:
717 /* alg is CCM: check MIC only */
718 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
720 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
722 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
726 case RX_RES_STATUS_SEC_TYPE_TKIP:
727 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
729 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
732 /* fall through if TTAK OK */
734 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
735 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
737 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
741 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
742 decrypt_in, decrypt_out);
747 /* Calc max signal level (dBm) among 3 possible receivers */
748 static int iwlagn_calc_rssi(struct iwl_priv *priv,
749 struct iwl_rx_phy_res *rx_resp)
751 /* data from PHY/DSP regarding signal strength, etc.,
752 * contents are always there, not configurable by host
754 struct iwlagn_non_cfg_phy *ncphy =
755 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
756 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
759 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
760 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
762 /* Find max rssi among 3 possible receivers.
763 * These values are measured by the digital signal processor (DSP).
764 * They should stay fairly constant even as the signal strength varies,
765 * if the radio's automatic gain control (AGC) is working right.
766 * AGC value (see below) will provide the "interesting" info.
768 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
769 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
770 IWLAGN_OFDM_RSSI_A_BIT_POS;
771 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
772 IWLAGN_OFDM_RSSI_B_BIT_POS;
773 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
774 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
775 IWLAGN_OFDM_RSSI_C_BIT_POS;
777 max_rssi = max_t(u32, rssi_a, rssi_b);
778 max_rssi = max_t(u32, max_rssi, rssi_c);
780 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
781 rssi_a, rssi_b, rssi_c, max_rssi, agc);
783 /* dBm = max_rssi dB - agc dB - constant.
784 * Higher AGC (higher radio gain) means lower signal. */
785 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
788 /* Called for REPLY_RX_MPDU_CMD */
789 static void iwlagn_rx_reply_rx(struct iwl_priv *priv,
790 struct iwl_rx_cmd_buffer *rxb)
792 struct ieee80211_hdr *header;
793 struct ieee80211_rx_status rx_status = {};
794 struct iwl_rx_packet *pkt = rxb_addr(rxb);
795 struct iwl_rx_phy_res *phy_res;
796 __le32 rx_pkt_status;
797 struct iwl_rx_mpdu_res_start *amsdu;
802 if (!priv->last_phy_res_valid) {
803 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
806 phy_res = &priv->last_phy_res;
807 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->data;
808 header = (struct ieee80211_hdr *)(pkt->data + sizeof(*amsdu));
809 len = le16_to_cpu(amsdu->byte_count);
810 rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*amsdu) + len);
811 ampdu_status = iwlagn_translate_rx_status(priv,
812 le32_to_cpu(rx_pkt_status));
814 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
815 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d\n",
816 phy_res->cfg_phy_cnt);
820 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
821 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
822 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
823 le32_to_cpu(rx_pkt_status));
827 /* This will be used in several places later */
828 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
830 /* rx_status carries information about the packet to mac80211 */
831 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
832 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
833 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
835 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
838 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
841 /* TSF isn't reliable. In order to allow smooth user experience,
842 * this W/A doesn't propagate it to the mac80211 */
843 /*rx_status.flag |= RX_FLAG_MACTIME_START;*/
845 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
847 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
848 rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
850 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
851 rx_status.signal, (unsigned long long)rx_status.mactime);
856 * It seems that the antenna field in the phy flags value
857 * is actually a bit field. This is undefined by radiotap,
858 * it wants an actual antenna number but I always get "7"
859 * for most legacy frames I receive indicating that the
860 * same frame was received on all three RX chains.
862 * I think this field should be removed in favor of a
863 * new 802.11n radiotap field "RX chains" that is defined
867 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
868 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
870 /* set the preamble flag if appropriate */
871 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
872 rx_status.enc_flags |= RX_ENC_FLAG_SHORTPRE;
874 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_AGG_MSK) {
876 * We know which subframes of an A-MPDU belong
877 * together since we get a single PHY response
878 * from the firmware for all of them
880 rx_status.flag |= RX_FLAG_AMPDU_DETAILS;
881 rx_status.ampdu_reference = priv->ampdu_ref;
884 /* Set up the HT phy flags */
885 if (rate_n_flags & RATE_MCS_HT_MSK)
886 rx_status.encoding = RX_ENC_HT;
887 if (rate_n_flags & RATE_MCS_HT40_MSK)
888 rx_status.bw = RATE_INFO_BW_40;
890 rx_status.bw = RATE_INFO_BW_20;
891 if (rate_n_flags & RATE_MCS_SGI_MSK)
892 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
893 if (rate_n_flags & RATE_MCS_GF_MSK)
894 rx_status.enc_flags |= RX_ENC_FLAG_HT_GF;
896 iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
900 static void iwlagn_rx_noa_notification(struct iwl_priv *priv,
901 struct iwl_rx_cmd_buffer *rxb)
903 struct iwl_wipan_noa_data *new_data, *old_data;
904 struct iwl_rx_packet *pkt = rxb_addr(rxb);
905 struct iwl_wipan_noa_notification *noa_notif = (void *)pkt->data;
907 /* no condition -- we're in softirq */
908 old_data = rcu_dereference_protected(priv->noa_data, true);
910 if (noa_notif->noa_active) {
911 u32 len = le16_to_cpu(noa_notif->noa_attribute.length);
914 /* EID, len, OUI, subtype */
915 len += 1 + 1 + 3 + 1;
916 /* P2P id, P2P length */
920 new_data = kmalloc(sizeof(*new_data) + len, GFP_ATOMIC);
922 new_data->length = len;
923 new_data->data[0] = WLAN_EID_VENDOR_SPECIFIC;
924 new_data->data[1] = len - 2; /* not counting EID, len */
925 new_data->data[2] = (WLAN_OUI_WFA >> 16) & 0xff;
926 new_data->data[3] = (WLAN_OUI_WFA >> 8) & 0xff;
927 new_data->data[4] = (WLAN_OUI_WFA >> 0) & 0xff;
928 new_data->data[5] = WLAN_OUI_TYPE_WFA_P2P;
929 memcpy(&new_data->data[6], &noa_notif->noa_attribute,
935 rcu_assign_pointer(priv->noa_data, new_data);
938 kfree_rcu(old_data, rcu_head);
942 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
944 * Setup the RX handlers for each of the reply types sent from the uCode
947 void iwl_setup_rx_handlers(struct iwl_priv *priv)
949 void (**handlers)(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb);
951 handlers = priv->rx_handlers;
953 handlers[REPLY_ERROR] = iwlagn_rx_reply_error;
954 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwlagn_rx_csa;
955 handlers[SPECTRUM_MEASURE_NOTIFICATION] =
956 iwlagn_rx_spectrum_measure_notif;
957 handlers[PM_SLEEP_NOTIFICATION] = iwlagn_rx_pm_sleep_notif;
958 handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
959 iwlagn_rx_pm_debug_statistics_notif;
960 handlers[BEACON_NOTIFICATION] = iwlagn_rx_beacon_notif;
961 handlers[REPLY_ADD_STA] = iwl_add_sta_callback;
963 handlers[REPLY_WIPAN_NOA_NOTIFICATION] = iwlagn_rx_noa_notification;
966 * The same handler is used for both the REPLY to a discrete
967 * statistics request from the host as well as for the periodic
968 * statistics notifications (after received beacons) from the uCode.
970 handlers[REPLY_STATISTICS_CMD] = iwlagn_rx_reply_statistics;
971 handlers[STATISTICS_NOTIFICATION] = iwlagn_rx_statistics;
973 iwl_setup_rx_scan_handlers(priv);
975 handlers[CARD_STATE_NOTIFICATION] = iwlagn_rx_card_state_notif;
976 handlers[MISSED_BEACONS_NOTIFICATION] =
977 iwlagn_rx_missed_beacon_notif;
980 handlers[REPLY_RX_PHY_CMD] = iwlagn_rx_reply_rx_phy;
981 handlers[REPLY_RX_MPDU_CMD] = iwlagn_rx_reply_rx;
984 handlers[REPLY_COMPRESSED_BA] =
985 iwlagn_rx_reply_compressed_ba;
987 priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
989 /* set up notification wait support */
990 iwl_notification_wait_init(&priv->notif_wait);
992 /* Set up BT Rx handlers */
993 if (priv->lib->bt_params)
994 iwlagn_bt_rx_handler_setup(priv);
997 void iwl_rx_dispatch(struct iwl_op_mode *op_mode, struct napi_struct *napi,
998 struct iwl_rx_cmd_buffer *rxb)
1000 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1001 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1004 * Do the notification wait before RX handlers so
1005 * even if the RX handler consumes the RXB we have
1006 * access to it in the notification wait entry.
1008 iwl_notification_wait_notify(&priv->notif_wait, pkt);
1010 /* Based on type of command response or notification,
1011 * handle those that need handling via function in
1012 * rx_handlers table. See iwl_setup_rx_handlers() */
1013 if (priv->rx_handlers[pkt->hdr.cmd]) {
1014 priv->rx_handlers_stats[pkt->hdr.cmd]++;
1015 priv->rx_handlers[pkt->hdr.cmd](priv, rxb);
1017 /* No handling needed */
1018 IWL_DEBUG_RX(priv, "No handler needed for %s, 0x%02x\n",
1019 iwl_get_cmd_string(priv->trans,
1020 iwl_cmd_id(pkt->hdr.cmd,