iwlagn: remove priv dereferences from the transport layer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwlwifi / iwl-rx.c
blobee8fabd0b4f3d0d2d33cbff4fcdaa6c158aa40d6
1 /******************************************************************************
3 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *****************************************************************************/
30 #include <linux/etherdevice.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
33 #include <net/mac80211.h>
34 #include <asm/unaligned.h>
35 #include "iwl-eeprom.h"
36 #include "iwl-dev.h"
37 #include "iwl-core.h"
38 #include "iwl-sta.h"
39 #include "iwl-io.h"
40 #include "iwl-helpers.h"
41 #include "iwl-agn-calib.h"
42 #include "iwl-agn.h"
43 #include "iwl-shared.h"
46 /******************************************************************************
48 * Generic RX handler implementations
50 ******************************************************************************/
52 static void iwl_rx_reply_error(struct iwl_priv *priv,
53 struct iwl_rx_mem_buffer *rxb)
55 struct iwl_rx_packet *pkt = rxb_addr(rxb);
57 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
58 "seq 0x%04X ser 0x%08X\n",
59 le32_to_cpu(pkt->u.err_resp.error_type),
60 get_cmd_string(pkt->u.err_resp.cmd_id),
61 pkt->u.err_resp.cmd_id,
62 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
63 le32_to_cpu(pkt->u.err_resp.error_info));
66 static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
68 struct iwl_rx_packet *pkt = rxb_addr(rxb);
69 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
71 * MULTI-FIXME
72 * See iwl_mac_channel_switch.
74 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
75 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
77 if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
78 return;
80 if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
81 rxon->channel = csa->channel;
82 ctx->staging.channel = csa->channel;
83 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
84 le16_to_cpu(csa->channel));
85 iwl_chswitch_done(priv, true);
86 } else {
87 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
88 le16_to_cpu(csa->channel));
89 iwl_chswitch_done(priv, false);
94 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
95 struct iwl_rx_mem_buffer *rxb)
97 struct iwl_rx_packet *pkt = rxb_addr(rxb);
98 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
100 if (!report->state) {
101 IWL_DEBUG_11H(priv,
102 "Spectrum Measure Notification: Start\n");
103 return;
106 memcpy(&priv->measure_report, report, sizeof(*report));
107 priv->measurement_status |= MEASUREMENT_READY;
110 static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
111 struct iwl_rx_mem_buffer *rxb)
113 #ifdef CONFIG_IWLWIFI_DEBUG
114 struct iwl_rx_packet *pkt = rxb_addr(rxb);
115 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
116 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
117 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
118 #endif
121 static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
122 struct iwl_rx_mem_buffer *rxb)
124 struct iwl_rx_packet *pkt = rxb_addr(rxb);
125 u32 __maybe_unused len =
126 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
127 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
128 "notification for %s:\n", len,
129 get_cmd_string(pkt->hdr.cmd));
130 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
133 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
134 struct iwl_rx_mem_buffer *rxb)
136 struct iwl_rx_packet *pkt = rxb_addr(rxb);
137 struct iwlagn_beacon_notif *beacon = (void *)pkt->u.raw;
138 #ifdef CONFIG_IWLWIFI_DEBUG
139 u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
140 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
142 IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
143 "tsf:0x%.8x%.8x rate:%d\n",
144 status & TX_STATUS_MSK,
145 beacon->beacon_notify_hdr.failure_frame,
146 le32_to_cpu(beacon->ibss_mgr_status),
147 le32_to_cpu(beacon->high_tsf),
148 le32_to_cpu(beacon->low_tsf), rate);
149 #endif
151 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
153 if (!test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
154 queue_work(priv->shrd->workqueue, &priv->beacon_update);
157 /* the threshold ratio of actual_ack_cnt to expected_ack_cnt in percent */
158 #define ACK_CNT_RATIO (50)
159 #define BA_TIMEOUT_CNT (5)
160 #define BA_TIMEOUT_MAX (16)
163 * iwl_good_ack_health - checks for ACK count ratios, BA timeout retries.
165 * When the ACK count ratio is low and aggregated BA timeout retries exceeding
166 * the BA_TIMEOUT_MAX, reload firmware and bring system back to normal
167 * operation state.
169 static bool iwl_good_ack_health(struct iwl_priv *priv,
170 struct statistics_tx *cur)
172 int actual_delta, expected_delta, ba_timeout_delta;
173 struct statistics_tx *old;
175 if (priv->agg_tids_count)
176 return true;
178 old = &priv->statistics.tx;
180 actual_delta = le32_to_cpu(cur->actual_ack_cnt) -
181 le32_to_cpu(old->actual_ack_cnt);
182 expected_delta = le32_to_cpu(cur->expected_ack_cnt) -
183 le32_to_cpu(old->expected_ack_cnt);
185 /* Values should not be negative, but we do not trust the firmware */
186 if (actual_delta <= 0 || expected_delta <= 0)
187 return true;
189 ba_timeout_delta = le32_to_cpu(cur->agg.ba_timeout) -
190 le32_to_cpu(old->agg.ba_timeout);
192 if ((actual_delta * 100 / expected_delta) < ACK_CNT_RATIO &&
193 ba_timeout_delta > BA_TIMEOUT_CNT) {
194 IWL_DEBUG_RADIO(priv, "deltas: actual %d expected %d ba_timeout %d\n",
195 actual_delta, expected_delta, ba_timeout_delta);
197 #ifdef CONFIG_IWLWIFI_DEBUGFS
199 * This is ifdef'ed on DEBUGFS because otherwise the
200 * statistics aren't available. If DEBUGFS is set but
201 * DEBUG is not, these will just compile out.
203 IWL_DEBUG_RADIO(priv, "rx_detected_cnt delta %d\n",
204 priv->delta_stats.tx.rx_detected_cnt);
205 IWL_DEBUG_RADIO(priv,
206 "ack_or_ba_timeout_collision delta %d\n",
207 priv->delta_stats.tx.ack_or_ba_timeout_collision);
208 #endif
210 if (ba_timeout_delta >= BA_TIMEOUT_MAX)
211 return false;
214 return true;
218 * iwl_good_plcp_health - checks for plcp error.
220 * When the plcp error is exceeding the thresholds, reset the radio
221 * to improve the throughput.
223 static bool iwl_good_plcp_health(struct iwl_priv *priv,
224 struct statistics_rx_phy *cur_ofdm,
225 struct statistics_rx_ht_phy *cur_ofdm_ht,
226 unsigned int msecs)
228 int delta;
229 int threshold = priv->cfg->base_params->plcp_delta_threshold;
231 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
232 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
233 return true;
236 delta = le32_to_cpu(cur_ofdm->plcp_err) -
237 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
238 le32_to_cpu(cur_ofdm_ht->plcp_err) -
239 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
241 /* Can be negative if firmware reset statistics */
242 if (delta <= 0)
243 return true;
245 if ((delta * 100 / msecs) > threshold) {
246 IWL_DEBUG_RADIO(priv,
247 "plcp health threshold %u delta %d msecs %u\n",
248 threshold, delta, msecs);
249 return false;
252 return true;
255 static void iwl_recover_from_statistics(struct iwl_priv *priv,
256 struct statistics_rx_phy *cur_ofdm,
257 struct statistics_rx_ht_phy *cur_ofdm_ht,
258 struct statistics_tx *tx,
259 unsigned long stamp)
261 unsigned int msecs;
263 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
264 return;
266 msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
268 /* Only gather statistics and update time stamp when not associated */
269 if (!iwl_is_any_associated(priv))
270 return;
272 /* Do not check/recover when do not have enough statistics data */
273 if (msecs < 99)
274 return;
276 if (iwlagn_mod_params.ack_check && !iwl_good_ack_health(priv, tx)) {
277 IWL_ERR(priv, "low ack count detected, restart firmware\n");
278 if (!iwl_force_reset(priv, IWL_FW_RESET, false))
279 return;
282 if (iwlagn_mod_params.plcp_check &&
283 !iwl_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
284 iwl_force_reset(priv, IWL_RF_RESET, false);
287 /* Calculate noise level, based on measurements during network silence just
288 * before arriving beacon. This measurement can be done only if we know
289 * exactly when to expect beacons, therefore only when we're associated. */
290 static void iwl_rx_calc_noise(struct iwl_priv *priv)
292 struct statistics_rx_non_phy *rx_info;
293 int num_active_rx = 0;
294 int total_silence = 0;
295 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
296 int last_rx_noise;
298 rx_info = &priv->statistics.rx_non_phy;
300 bcn_silence_a =
301 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
302 bcn_silence_b =
303 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
304 bcn_silence_c =
305 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
307 if (bcn_silence_a) {
308 total_silence += bcn_silence_a;
309 num_active_rx++;
311 if (bcn_silence_b) {
312 total_silence += bcn_silence_b;
313 num_active_rx++;
315 if (bcn_silence_c) {
316 total_silence += bcn_silence_c;
317 num_active_rx++;
320 /* Average among active antennas */
321 if (num_active_rx)
322 last_rx_noise = (total_silence / num_active_rx) - 107;
323 else
324 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
326 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
327 bcn_silence_a, bcn_silence_b, bcn_silence_c,
328 last_rx_noise);
331 #ifdef CONFIG_IWLWIFI_DEBUGFS
333 * based on the assumption of all statistics counter are in DWORD
334 * FIXME: This function is for debugging, do not deal with
335 * the case of counters roll-over.
337 static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
338 __le32 *max_delta, __le32 *accum, int size)
340 int i;
342 for (i = 0;
343 i < size / sizeof(__le32);
344 i++, prev++, cur++, delta++, max_delta++, accum++) {
345 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
346 *delta = cpu_to_le32(
347 le32_to_cpu(*cur) - le32_to_cpu(*prev));
348 le32_add_cpu(accum, le32_to_cpu(*delta));
349 if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
350 *max_delta = *delta;
355 static void
356 iwl_accumulative_statistics(struct iwl_priv *priv,
357 struct statistics_general_common *common,
358 struct statistics_rx_non_phy *rx_non_phy,
359 struct statistics_rx_phy *rx_ofdm,
360 struct statistics_rx_ht_phy *rx_ofdm_ht,
361 struct statistics_rx_phy *rx_cck,
362 struct statistics_tx *tx,
363 struct statistics_bt_activity *bt_activity)
365 #define ACCUM(_name) \
366 accum_stats((__le32 *)&priv->statistics._name, \
367 (__le32 *)_name, \
368 (__le32 *)&priv->delta_stats._name, \
369 (__le32 *)&priv->max_delta_stats._name, \
370 (__le32 *)&priv->accum_stats._name, \
371 sizeof(*_name));
373 ACCUM(common);
374 ACCUM(rx_non_phy);
375 ACCUM(rx_ofdm);
376 ACCUM(rx_ofdm_ht);
377 ACCUM(rx_cck);
378 ACCUM(tx);
379 if (bt_activity)
380 ACCUM(bt_activity);
381 #undef ACCUM
383 #else
384 static inline void
385 iwl_accumulative_statistics(struct iwl_priv *priv,
386 struct statistics_general_common *common,
387 struct statistics_rx_non_phy *rx_non_phy,
388 struct statistics_rx_phy *rx_ofdm,
389 struct statistics_rx_ht_phy *rx_ofdm_ht,
390 struct statistics_rx_phy *rx_cck,
391 struct statistics_tx *tx,
392 struct statistics_bt_activity *bt_activity)
395 #endif
397 static void iwl_rx_statistics(struct iwl_priv *priv,
398 struct iwl_rx_mem_buffer *rxb)
400 unsigned long stamp = jiffies;
401 const int reg_recalib_period = 60;
402 int change;
403 struct iwl_rx_packet *pkt = rxb_addr(rxb);
404 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
405 __le32 *flag;
406 struct statistics_general_common *common;
407 struct statistics_rx_non_phy *rx_non_phy;
408 struct statistics_rx_phy *rx_ofdm;
409 struct statistics_rx_ht_phy *rx_ofdm_ht;
410 struct statistics_rx_phy *rx_cck;
411 struct statistics_tx *tx;
412 struct statistics_bt_activity *bt_activity;
414 len -= sizeof(struct iwl_cmd_header); /* skip header */
416 IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
417 len);
419 if (len == sizeof(struct iwl_bt_notif_statistics)) {
420 struct iwl_bt_notif_statistics *stats;
421 stats = &pkt->u.stats_bt;
422 flag = &stats->flag;
423 common = &stats->general.common;
424 rx_non_phy = &stats->rx.general.common;
425 rx_ofdm = &stats->rx.ofdm;
426 rx_ofdm_ht = &stats->rx.ofdm_ht;
427 rx_cck = &stats->rx.cck;
428 tx = &stats->tx;
429 bt_activity = &stats->general.activity;
431 #ifdef CONFIG_IWLWIFI_DEBUGFS
432 /* handle this exception directly */
433 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
434 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
435 le32_to_cpu(stats->rx.general.num_bt_kills));
436 #endif
437 } else if (len == sizeof(struct iwl_notif_statistics)) {
438 struct iwl_notif_statistics *stats;
439 stats = &pkt->u.stats;
440 flag = &stats->flag;
441 common = &stats->general.common;
442 rx_non_phy = &stats->rx.general;
443 rx_ofdm = &stats->rx.ofdm;
444 rx_ofdm_ht = &stats->rx.ofdm_ht;
445 rx_cck = &stats->rx.cck;
446 tx = &stats->tx;
447 bt_activity = NULL;
448 } else {
449 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
450 len, sizeof(struct iwl_bt_notif_statistics),
451 sizeof(struct iwl_notif_statistics));
452 return;
455 change = common->temperature != priv->statistics.common.temperature ||
456 (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
457 (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
459 iwl_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
460 rx_ofdm_ht, rx_cck, tx, bt_activity);
462 iwl_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
464 priv->statistics.flag = *flag;
465 memcpy(&priv->statistics.common, common, sizeof(*common));
466 memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
467 memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
468 memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
469 memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
470 memcpy(&priv->statistics.tx, tx, sizeof(*tx));
471 #ifdef CONFIG_IWLWIFI_DEBUGFS
472 if (bt_activity)
473 memcpy(&priv->statistics.bt_activity, bt_activity,
474 sizeof(*bt_activity));
475 #endif
477 priv->rx_statistics_jiffies = stamp;
479 set_bit(STATUS_STATISTICS, &priv->shrd->status);
481 /* Reschedule the statistics timer to occur in
482 * reg_recalib_period seconds to ensure we get a
483 * thermal update even if the uCode doesn't give
484 * us one */
485 mod_timer(&priv->statistics_periodic, jiffies +
486 msecs_to_jiffies(reg_recalib_period * 1000));
488 if (unlikely(!test_bit(STATUS_SCANNING, &priv->shrd->status)) &&
489 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
490 iwl_rx_calc_noise(priv);
491 queue_work(priv->shrd->workqueue, &priv->run_time_calib_work);
493 if (priv->cfg->lib->temperature && change)
494 priv->cfg->lib->temperature(priv);
497 static void iwl_rx_reply_statistics(struct iwl_priv *priv,
498 struct iwl_rx_mem_buffer *rxb)
500 struct iwl_rx_packet *pkt = rxb_addr(rxb);
502 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
503 #ifdef CONFIG_IWLWIFI_DEBUGFS
504 memset(&priv->accum_stats, 0,
505 sizeof(priv->accum_stats));
506 memset(&priv->delta_stats, 0,
507 sizeof(priv->delta_stats));
508 memset(&priv->max_delta_stats, 0,
509 sizeof(priv->max_delta_stats));
510 #endif
511 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
513 iwl_rx_statistics(priv, rxb);
516 /* Handle notification from uCode that card's power state is changing
517 * due to software, hardware, or critical temperature RFKILL */
518 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
519 struct iwl_rx_mem_buffer *rxb)
521 struct iwl_rx_packet *pkt = rxb_addr(rxb);
522 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
523 unsigned long status = priv->shrd->status;
525 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
526 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
527 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
528 (flags & CT_CARD_DISABLED) ?
529 "Reached" : "Not reached");
531 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
532 CT_CARD_DISABLED)) {
534 iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_SET,
535 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
537 iwl_write_direct32(bus(priv), HBUS_TARG_MBX_C,
538 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
540 if (!(flags & RXON_CARD_DISABLED)) {
541 iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
542 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
543 iwl_write_direct32(bus(priv), HBUS_TARG_MBX_C,
544 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
546 if (flags & CT_CARD_DISABLED)
547 iwl_tt_enter_ct_kill(priv);
549 if (!(flags & CT_CARD_DISABLED))
550 iwl_tt_exit_ct_kill(priv);
552 if (flags & HW_CARD_DISABLED)
553 set_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
554 else
555 clear_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
558 if (!(flags & RXON_CARD_DISABLED))
559 iwl_scan_cancel(priv);
561 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
562 test_bit(STATUS_RF_KILL_HW, &priv->shrd->status)))
563 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
564 test_bit(STATUS_RF_KILL_HW, &priv->shrd->status));
565 else
566 wake_up_interruptible(&priv->shrd->wait_command_queue);
569 static void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
570 struct iwl_rx_mem_buffer *rxb)
573 struct iwl_rx_packet *pkt = rxb_addr(rxb);
574 struct iwl_missed_beacon_notif *missed_beacon;
576 missed_beacon = &pkt->u.missed_beacon;
577 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
578 priv->missed_beacon_threshold) {
579 IWL_DEBUG_CALIB(priv,
580 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
581 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
582 le32_to_cpu(missed_beacon->total_missed_becons),
583 le32_to_cpu(missed_beacon->num_recvd_beacons),
584 le32_to_cpu(missed_beacon->num_expected_beacons));
585 if (!test_bit(STATUS_SCANNING, &priv->shrd->status))
586 iwl_init_sensitivity(priv);
590 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
591 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
592 static void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
593 struct iwl_rx_mem_buffer *rxb)
595 struct iwl_rx_packet *pkt = rxb_addr(rxb);
597 priv->last_phy_res_valid = true;
598 memcpy(&priv->last_phy_res, pkt->u.raw,
599 sizeof(struct iwl_rx_phy_res));
603 * returns non-zero if packet should be dropped
605 static int iwl_set_decrypted_flag(struct iwl_priv *priv,
606 struct ieee80211_hdr *hdr,
607 u32 decrypt_res,
608 struct ieee80211_rx_status *stats)
610 u16 fc = le16_to_cpu(hdr->frame_control);
613 * All contexts have the same setting here due to it being
614 * a module parameter, so OK to check any context.
616 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
617 RXON_FILTER_DIS_DECRYPT_MSK)
618 return 0;
620 if (!(fc & IEEE80211_FCTL_PROTECTED))
621 return 0;
623 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
624 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
625 case RX_RES_STATUS_SEC_TYPE_TKIP:
626 /* The uCode has got a bad phase 1 Key, pushes the packet.
627 * Decryption will be done in SW. */
628 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
629 RX_RES_STATUS_BAD_KEY_TTAK)
630 break;
632 case RX_RES_STATUS_SEC_TYPE_WEP:
633 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
634 RX_RES_STATUS_BAD_ICV_MIC) {
635 /* bad ICV, the packet is destroyed since the
636 * decryption is inplace, drop it */
637 IWL_DEBUG_RX(priv, "Packet destroyed\n");
638 return -1;
640 case RX_RES_STATUS_SEC_TYPE_CCMP:
641 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
642 RX_RES_STATUS_DECRYPT_OK) {
643 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
644 stats->flag |= RX_FLAG_DECRYPTED;
646 break;
648 default:
649 break;
651 return 0;
654 static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
655 struct ieee80211_hdr *hdr,
656 u16 len,
657 u32 ampdu_status,
658 struct iwl_rx_mem_buffer *rxb,
659 struct ieee80211_rx_status *stats)
661 struct sk_buff *skb;
662 __le16 fc = hdr->frame_control;
663 struct iwl_rxon_context *ctx;
665 /* We only process data packets if the interface is open */
666 if (unlikely(!priv->is_open)) {
667 IWL_DEBUG_DROP_LIMIT(priv,
668 "Dropping packet while interface is not open.\n");
669 return;
672 /* In case of HW accelerated crypto and bad decryption, drop */
673 if (!iwlagn_mod_params.sw_crypto &&
674 iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
675 return;
677 skb = dev_alloc_skb(128);
678 if (!skb) {
679 IWL_ERR(priv, "dev_alloc_skb failed\n");
680 return;
683 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
685 iwl_update_stats(priv, false, fc, len);
688 * Wake any queues that were stopped due to a passive channel tx
689 * failure. This can happen because the regulatory enforcement in
690 * the device waits for a beacon before allowing transmission,
691 * sometimes even after already having transmitted frames for the
692 * association because the new RXON may reset the information.
694 if (unlikely(ieee80211_is_beacon(fc))) {
695 for_each_context(priv, ctx) {
696 if (!ctx->last_tx_rejected)
697 continue;
698 if (compare_ether_addr(hdr->addr3,
699 ctx->active.bssid_addr))
700 continue;
701 ctx->last_tx_rejected = false;
702 iwl_trans_wake_any_queue(trans(priv), ctx->ctxid);
706 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
708 ieee80211_rx(priv->hw, skb);
709 rxb->page = NULL;
712 static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
714 u32 decrypt_out = 0;
716 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
717 RX_RES_STATUS_STATION_FOUND)
718 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
719 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
721 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
723 /* packet was not encrypted */
724 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
725 RX_RES_STATUS_SEC_TYPE_NONE)
726 return decrypt_out;
728 /* packet was encrypted with unknown alg */
729 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
730 RX_RES_STATUS_SEC_TYPE_ERR)
731 return decrypt_out;
733 /* decryption was not done in HW */
734 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
735 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
736 return decrypt_out;
738 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
740 case RX_RES_STATUS_SEC_TYPE_CCMP:
741 /* alg is CCM: check MIC only */
742 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
743 /* Bad MIC */
744 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
745 else
746 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
748 break;
750 case RX_RES_STATUS_SEC_TYPE_TKIP:
751 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
752 /* Bad TTAK */
753 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
754 break;
756 /* fall through if TTAK OK */
757 default:
758 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
759 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
760 else
761 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
762 break;
765 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
766 decrypt_in, decrypt_out);
768 return decrypt_out;
771 /* Calc max signal level (dBm) among 3 possible receivers */
772 static int iwlagn_calc_rssi(struct iwl_priv *priv,
773 struct iwl_rx_phy_res *rx_resp)
775 /* data from PHY/DSP regarding signal strength, etc.,
776 * contents are always there, not configurable by host
778 struct iwlagn_non_cfg_phy *ncphy =
779 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
780 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
781 u8 agc;
783 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
784 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
786 /* Find max rssi among 3 possible receivers.
787 * These values are measured by the digital signal processor (DSP).
788 * They should stay fairly constant even as the signal strength varies,
789 * if the radio's automatic gain control (AGC) is working right.
790 * AGC value (see below) will provide the "interesting" info.
792 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
793 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
794 IWLAGN_OFDM_RSSI_A_BIT_POS;
795 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
796 IWLAGN_OFDM_RSSI_B_BIT_POS;
797 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
798 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
799 IWLAGN_OFDM_RSSI_C_BIT_POS;
801 max_rssi = max_t(u32, rssi_a, rssi_b);
802 max_rssi = max_t(u32, max_rssi, rssi_c);
804 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
805 rssi_a, rssi_b, rssi_c, max_rssi, agc);
807 /* dBm = max_rssi dB - agc dB - constant.
808 * Higher AGC (higher radio gain) means lower signal. */
809 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
812 /* Called for REPLY_RX (legacy ABG frames), or
813 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
814 static void iwl_rx_reply_rx(struct iwl_priv *priv,
815 struct iwl_rx_mem_buffer *rxb)
817 struct ieee80211_hdr *header;
818 struct ieee80211_rx_status rx_status;
819 struct iwl_rx_packet *pkt = rxb_addr(rxb);
820 struct iwl_rx_phy_res *phy_res;
821 __le32 rx_pkt_status;
822 struct iwl_rx_mpdu_res_start *amsdu;
823 u32 len;
824 u32 ampdu_status;
825 u32 rate_n_flags;
828 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
829 * REPLY_RX: physical layer info is in this buffer
830 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
831 * command and cached in priv->last_phy_res
833 * Here we set up local variables depending on which command is
834 * received.
836 if (pkt->hdr.cmd == REPLY_RX) {
837 phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
838 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
839 + phy_res->cfg_phy_cnt);
841 len = le16_to_cpu(phy_res->byte_count);
842 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
843 phy_res->cfg_phy_cnt + len);
844 ampdu_status = le32_to_cpu(rx_pkt_status);
845 } else {
846 if (!priv->last_phy_res_valid) {
847 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
848 return;
850 phy_res = &priv->last_phy_res;
851 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
852 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
853 len = le16_to_cpu(amsdu->byte_count);
854 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
855 ampdu_status = iwl_translate_rx_status(priv,
856 le32_to_cpu(rx_pkt_status));
859 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
860 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
861 phy_res->cfg_phy_cnt);
862 return;
865 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
866 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
867 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
868 le32_to_cpu(rx_pkt_status));
869 return;
872 /* This will be used in several places later */
873 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
875 /* rx_status carries information about the packet to mac80211 */
876 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
877 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
878 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
879 rx_status.freq =
880 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
881 rx_status.band);
882 rx_status.rate_idx =
883 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
884 rx_status.flag = 0;
886 /* TSF isn't reliable. In order to allow smooth user experience,
887 * this W/A doesn't propagate it to the mac80211 */
888 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
890 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
892 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
893 rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
895 iwl_dbg_log_rx_data_frame(priv, len, header);
896 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
897 rx_status.signal, (unsigned long long)rx_status.mactime);
900 * "antenna number"
902 * It seems that the antenna field in the phy flags value
903 * is actually a bit field. This is undefined by radiotap,
904 * it wants an actual antenna number but I always get "7"
905 * for most legacy frames I receive indicating that the
906 * same frame was received on all three RX chains.
908 * I think this field should be removed in favor of a
909 * new 802.11n radiotap field "RX chains" that is defined
910 * as a bitmask.
912 rx_status.antenna =
913 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
914 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
916 /* set the preamble flag if appropriate */
917 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
918 rx_status.flag |= RX_FLAG_SHORTPRE;
920 /* Set up the HT phy flags */
921 if (rate_n_flags & RATE_MCS_HT_MSK)
922 rx_status.flag |= RX_FLAG_HT;
923 if (rate_n_flags & RATE_MCS_HT40_MSK)
924 rx_status.flag |= RX_FLAG_40MHZ;
925 if (rate_n_flags & RATE_MCS_SGI_MSK)
926 rx_status.flag |= RX_FLAG_SHORT_GI;
928 iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
929 rxb, &rx_status);
933 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
935 * Setup the RX handlers for each of the reply types sent from the uCode
936 * to the host.
938 void iwl_setup_rx_handlers(struct iwl_priv *priv)
940 void (**handlers)(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb);
942 handlers = priv->rx_handlers;
944 handlers[REPLY_ERROR] = iwl_rx_reply_error;
945 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
946 handlers[SPECTRUM_MEASURE_NOTIFICATION] = iwl_rx_spectrum_measure_notif;
947 handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
948 handlers[PM_DEBUG_STATISTIC_NOTIFIC] = iwl_rx_pm_debug_statistics_notif;
949 handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
952 * The same handler is used for both the REPLY to a discrete
953 * statistics request from the host as well as for the periodic
954 * statistics notifications (after received beacons) from the uCode.
956 handlers[REPLY_STATISTICS_CMD] = iwl_rx_reply_statistics;
957 handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
959 iwl_setup_rx_scan_handlers(priv);
961 handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
962 handlers[MISSED_BEACONS_NOTIFICATION] = iwl_rx_missed_beacon_notif;
964 /* Rx handlers */
965 handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
966 handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
968 /* block ack */
969 handlers[REPLY_COMPRESSED_BA] = iwlagn_rx_reply_compressed_ba;
971 /* init calibration handlers */
972 priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
973 iwlagn_rx_calib_result;
974 priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
976 /* set up notification wait support */
977 spin_lock_init(&priv->notif_wait_lock);
978 INIT_LIST_HEAD(&priv->notif_waits);
979 init_waitqueue_head(&priv->notif_waitq);
981 /* Set up BT Rx handlers */
982 if (priv->cfg->lib->bt_rx_handler_setup)
983 priv->cfg->lib->bt_rx_handler_setup(priv);
987 void iwl_rx_dispatch(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
989 struct iwl_rx_packet *pkt = rxb_addr(rxb);
992 * Do the notification wait before RX handlers so
993 * even if the RX handler consumes the RXB we have
994 * access to it in the notification wait entry.
996 if (!list_empty(&priv->notif_waits)) {
997 struct iwl_notification_wait *w;
999 spin_lock(&priv->notif_wait_lock);
1000 list_for_each_entry(w, &priv->notif_waits, list) {
1001 if (w->cmd != pkt->hdr.cmd)
1002 continue;
1003 IWL_DEBUG_RX(priv,
1004 "Notif: %s, 0x%02x - wake the callers up\n",
1005 get_cmd_string(pkt->hdr.cmd),
1006 pkt->hdr.cmd);
1007 w->triggered = true;
1008 if (w->fn)
1009 w->fn(priv, pkt, w->fn_data);
1011 spin_unlock(&priv->notif_wait_lock);
1013 wake_up_all(&priv->notif_waitq);
1016 if (priv->pre_rx_handler)
1017 priv->pre_rx_handler(priv, rxb);
1019 /* Based on type of command response or notification,
1020 * handle those that need handling via function in
1021 * rx_handlers table. See iwl_setup_rx_handlers() */
1022 if (priv->rx_handlers[pkt->hdr.cmd]) {
1023 priv->rx_handlers_stats[pkt->hdr.cmd]++;
1024 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1025 } else {
1026 /* No handling needed */
1027 IWL_DEBUG_RX(priv,
1028 "No handler needed for %s, 0x%02x\n",
1029 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);