iwlwifi: cleanup iwl_good_plcp_health
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwlwifi / iwl-rx.c
bloba70f1eb08e5cf66f5c7f5690d532bd5fb15f9ca5
1 /******************************************************************************
3 * Copyright(c) 2003 - 2010 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 <net/mac80211.h>
33 #include <asm/unaligned.h>
34 #include "iwl-eeprom.h"
35 #include "iwl-dev.h"
36 #include "iwl-core.h"
37 #include "iwl-sta.h"
38 #include "iwl-io.h"
39 #include "iwl-helpers.h"
40 #include "iwl-agn-calib.h"
41 #include "iwl-agn.h"
43 /******************************************************************************
45 * RX path functions
47 ******************************************************************************/
50 * Rx theory of operation
52 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
53 * each of which point to Receive Buffers to be filled by the NIC. These get
54 * used not only for Rx frames, but for any command response or notification
55 * from the NIC. The driver and NIC manage the Rx buffers by means
56 * of indexes into the circular buffer.
58 * Rx Queue Indexes
59 * The host/firmware share two index registers for managing the Rx buffers.
61 * The READ index maps to the first position that the firmware may be writing
62 * to -- the driver can read up to (but not including) this position and get
63 * good data.
64 * The READ index is managed by the firmware once the card is enabled.
66 * The WRITE index maps to the last position the driver has read from -- the
67 * position preceding WRITE is the last slot the firmware can place a packet.
69 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
70 * WRITE = READ.
72 * During initialization, the host sets up the READ queue position to the first
73 * INDEX position, and WRITE to the last (READ - 1 wrapped)
75 * When the firmware places a packet in a buffer, it will advance the READ index
76 * and fire the RX interrupt. The driver can then query the READ index and
77 * process as many packets as possible, moving the WRITE index forward as it
78 * resets the Rx queue buffers with new memory.
80 * The management in the driver is as follows:
81 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
82 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
83 * to replenish the iwl->rxq->rx_free.
84 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
85 * iwl->rxq is replenished and the READ INDEX is updated (updating the
86 * 'processed' and 'read' driver indexes as well)
87 * + A received packet is processed and handed to the kernel network stack,
88 * detached from the iwl->rxq. The driver 'processed' index is updated.
89 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
90 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
91 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
92 * were enough free buffers and RX_STALLED is set it is cleared.
95 * Driver sequence:
97 * iwl_rx_queue_alloc() Allocates rx_free
98 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
99 * iwl_rx_queue_restock
100 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
101 * queue, updates firmware pointers, and updates
102 * the WRITE index. If insufficient rx_free buffers
103 * are available, schedules iwl_rx_replenish
105 * -- enable interrupts --
106 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
107 * READ INDEX, detaching the SKB from the pool.
108 * Moves the packet buffer from queue to rx_used.
109 * Calls iwl_rx_queue_restock to refill any empty
110 * slots.
111 * ...
116 * iwl_rx_queue_space - Return number of free slots available in queue.
118 int iwl_rx_queue_space(const struct iwl_rx_queue *q)
120 int s = q->read - q->write;
121 if (s <= 0)
122 s += RX_QUEUE_SIZE;
123 /* keep some buffer to not confuse full and empty queue */
124 s -= 2;
125 if (s < 0)
126 s = 0;
127 return s;
131 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
133 void iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
135 unsigned long flags;
136 u32 rx_wrt_ptr_reg = priv->hw_params.rx_wrt_ptr_reg;
137 u32 reg;
139 spin_lock_irqsave(&q->lock, flags);
141 if (q->need_update == 0)
142 goto exit_unlock;
144 if (priv->cfg->base_params->shadow_reg_enable) {
145 /* shadow register enabled */
146 /* Device expects a multiple of 8 */
147 q->write_actual = (q->write & ~0x7);
148 iwl_write32(priv, rx_wrt_ptr_reg, q->write_actual);
149 } else {
150 /* If power-saving is in use, make sure device is awake */
151 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
152 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
154 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
155 IWL_DEBUG_INFO(priv,
156 "Rx queue requesting wakeup,"
157 " GP1 = 0x%x\n", reg);
158 iwl_set_bit(priv, CSR_GP_CNTRL,
159 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
160 goto exit_unlock;
163 q->write_actual = (q->write & ~0x7);
164 iwl_write_direct32(priv, rx_wrt_ptr_reg,
165 q->write_actual);
167 /* Else device is assumed to be awake */
168 } else {
169 /* Device expects a multiple of 8 */
170 q->write_actual = (q->write & ~0x7);
171 iwl_write_direct32(priv, rx_wrt_ptr_reg,
172 q->write_actual);
175 q->need_update = 0;
177 exit_unlock:
178 spin_unlock_irqrestore(&q->lock, flags);
181 int iwl_rx_queue_alloc(struct iwl_priv *priv)
183 struct iwl_rx_queue *rxq = &priv->rxq;
184 struct device *dev = &priv->pci_dev->dev;
185 int i;
187 spin_lock_init(&rxq->lock);
188 INIT_LIST_HEAD(&rxq->rx_free);
189 INIT_LIST_HEAD(&rxq->rx_used);
191 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
192 rxq->bd = dma_alloc_coherent(dev, 4 * RX_QUEUE_SIZE, &rxq->bd_dma,
193 GFP_KERNEL);
194 if (!rxq->bd)
195 goto err_bd;
197 rxq->rb_stts = dma_alloc_coherent(dev, sizeof(struct iwl_rb_status),
198 &rxq->rb_stts_dma, GFP_KERNEL);
199 if (!rxq->rb_stts)
200 goto err_rb;
202 /* Fill the rx_used queue with _all_ of the Rx buffers */
203 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
204 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
206 /* Set us so that we have processed and used all buffers, but have
207 * not restocked the Rx queue with fresh buffers */
208 rxq->read = rxq->write = 0;
209 rxq->write_actual = 0;
210 rxq->free_count = 0;
211 rxq->need_update = 0;
212 return 0;
214 err_rb:
215 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
216 rxq->bd_dma);
217 err_bd:
218 return -ENOMEM;
221 /******************************************************************************
223 * Generic RX handler implementations
225 ******************************************************************************/
227 static void iwl_rx_reply_alive(struct iwl_priv *priv,
228 struct iwl_rx_mem_buffer *rxb)
230 struct iwl_rx_packet *pkt = rxb_addr(rxb);
231 struct iwl_alive_resp *palive;
232 struct delayed_work *pwork;
234 palive = &pkt->u.alive_frame;
236 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
237 "0x%01X 0x%01X\n",
238 palive->is_valid, palive->ver_type,
239 palive->ver_subtype);
241 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
242 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
243 memcpy(&priv->card_alive_init,
244 &pkt->u.alive_frame,
245 sizeof(struct iwl_init_alive_resp));
246 pwork = &priv->init_alive_start;
247 } else {
248 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
249 memcpy(&priv->card_alive, &pkt->u.alive_frame,
250 sizeof(struct iwl_alive_resp));
251 pwork = &priv->alive_start;
254 /* We delay the ALIVE response by 5ms to
255 * give the HW RF Kill time to activate... */
256 if (palive->is_valid == UCODE_VALID_OK)
257 queue_delayed_work(priv->workqueue, pwork,
258 msecs_to_jiffies(5));
259 else {
260 IWL_WARN(priv, "%s uCode did not respond OK.\n",
261 (palive->ver_subtype == INITIALIZE_SUBTYPE) ?
262 "init" : "runtime");
264 * If fail to load init uCode,
265 * let's try to load the init uCode again.
266 * We should not get into this situation, but if it
267 * does happen, we should not move on and loading "runtime"
268 * without proper calibrate the device.
270 if (palive->ver_subtype == INITIALIZE_SUBTYPE)
271 priv->ucode_type = UCODE_NONE;
272 queue_work(priv->workqueue, &priv->restart);
276 static void iwl_rx_reply_error(struct iwl_priv *priv,
277 struct iwl_rx_mem_buffer *rxb)
279 struct iwl_rx_packet *pkt = rxb_addr(rxb);
281 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
282 "seq 0x%04X ser 0x%08X\n",
283 le32_to_cpu(pkt->u.err_resp.error_type),
284 get_cmd_string(pkt->u.err_resp.cmd_id),
285 pkt->u.err_resp.cmd_id,
286 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
287 le32_to_cpu(pkt->u.err_resp.error_info));
290 static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
292 struct iwl_rx_packet *pkt = rxb_addr(rxb);
293 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
295 * MULTI-FIXME
296 * See iwl_mac_channel_switch.
298 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
299 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
301 if (priv->switch_rxon.switch_in_progress) {
302 if (!le32_to_cpu(csa->status) &&
303 (csa->channel == priv->switch_rxon.channel)) {
304 rxon->channel = csa->channel;
305 ctx->staging.channel = csa->channel;
306 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
307 le16_to_cpu(csa->channel));
308 iwl_chswitch_done(priv, true);
309 } else {
310 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
311 le16_to_cpu(csa->channel));
312 iwl_chswitch_done(priv, false);
318 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
319 struct iwl_rx_mem_buffer *rxb)
321 struct iwl_rx_packet *pkt = rxb_addr(rxb);
322 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
324 if (!report->state) {
325 IWL_DEBUG_11H(priv,
326 "Spectrum Measure Notification: Start\n");
327 return;
330 memcpy(&priv->measure_report, report, sizeof(*report));
331 priv->measurement_status |= MEASUREMENT_READY;
334 static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
335 struct iwl_rx_mem_buffer *rxb)
337 #ifdef CONFIG_IWLWIFI_DEBUG
338 struct iwl_rx_packet *pkt = rxb_addr(rxb);
339 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
340 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
341 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
342 #endif
345 static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
346 struct iwl_rx_mem_buffer *rxb)
348 struct iwl_rx_packet *pkt = rxb_addr(rxb);
349 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
350 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
351 "notification for %s:\n", len,
352 get_cmd_string(pkt->hdr.cmd));
353 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
356 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
357 struct iwl_rx_mem_buffer *rxb)
359 struct iwl_rx_packet *pkt = rxb_addr(rxb);
360 struct iwlagn_beacon_notif *beacon = (void *)pkt->u.raw;
361 #ifdef CONFIG_IWLWIFI_DEBUG
362 u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
363 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
365 IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
366 "tsf:0x%.8x%.8x rate:%d\n",
367 status & TX_STATUS_MSK,
368 beacon->beacon_notify_hdr.failure_frame,
369 le32_to_cpu(beacon->ibss_mgr_status),
370 le32_to_cpu(beacon->high_tsf),
371 le32_to_cpu(beacon->low_tsf), rate);
372 #endif
374 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
376 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
377 queue_work(priv->workqueue, &priv->beacon_update);
380 /* the threshold ratio of actual_ack_cnt to expected_ack_cnt in percent */
381 #define ACK_CNT_RATIO (50)
382 #define BA_TIMEOUT_CNT (5)
383 #define BA_TIMEOUT_MAX (16)
386 * iwl_good_ack_health - checks for ACK count ratios, BA timeout retries.
388 * When the ACK count ratio is low and aggregated BA timeout retries exceeding
389 * the BA_TIMEOUT_MAX, reload firmware and bring system back to normal
390 * operation state.
392 static bool iwl_good_ack_health(struct iwl_priv *priv, struct iwl_rx_packet *pkt)
394 int actual_delta, expected_delta, ba_timeout_delta;
395 struct statistics_tx *cur, *old;
397 if (priv->_agn.agg_tids_count)
398 return true;
400 if (iwl_bt_statistics(priv)) {
401 cur = &pkt->u.stats_bt.tx;
402 old = &priv->_agn.statistics_bt.tx;
403 } else {
404 cur = &pkt->u.stats.tx;
405 old = &priv->_agn.statistics.tx;
408 actual_delta = le32_to_cpu(cur->actual_ack_cnt) -
409 le32_to_cpu(old->actual_ack_cnt);
410 expected_delta = le32_to_cpu(cur->expected_ack_cnt) -
411 le32_to_cpu(old->expected_ack_cnt);
413 /* Values should not be negative, but we do not trust the firmware */
414 if (actual_delta <= 0 || expected_delta <= 0)
415 return true;
417 ba_timeout_delta = le32_to_cpu(cur->agg.ba_timeout) -
418 le32_to_cpu(old->agg.ba_timeout);
420 if ((actual_delta * 100 / expected_delta) < ACK_CNT_RATIO &&
421 ba_timeout_delta > BA_TIMEOUT_CNT) {
422 IWL_DEBUG_RADIO(priv, "deltas: actual %d expected %d ba_timeout %d\n",
423 actual_delta, expected_delta, ba_timeout_delta);
425 #ifdef CONFIG_IWLWIFI_DEBUGFS
427 * This is ifdef'ed on DEBUGFS because otherwise the
428 * statistics aren't available. If DEBUGFS is set but
429 * DEBUG is not, these will just compile out.
431 IWL_DEBUG_RADIO(priv, "rx_detected_cnt delta %d\n",
432 priv->_agn.delta_statistics.tx.rx_detected_cnt);
433 IWL_DEBUG_RADIO(priv,
434 "ack_or_ba_timeout_collision delta %d\n",
435 priv->_agn.delta_statistics.tx.ack_or_ba_timeout_collision);
436 #endif
438 if (ba_timeout_delta >= BA_TIMEOUT_MAX)
439 return false;
442 return true;
446 * iwl_good_plcp_health - checks for plcp error.
448 * When the plcp error is exceeding the thresholds, reset the radio
449 * to improve the throughput.
451 static bool iwl_good_plcp_health(struct iwl_priv *priv,
452 struct iwl_rx_packet *pkt)
454 unsigned int msecs;
455 unsigned long stamp;
456 int delta;
457 int threshold = priv->cfg->base_params->plcp_delta_threshold;
459 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
460 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
461 return true;
464 stamp = jiffies;
465 msecs = jiffies_to_msecs(stamp - priv->plcp_jiffies);
466 priv->plcp_jiffies = stamp;
468 if (msecs == 0)
469 return true;
471 if (iwl_bt_statistics(priv)) {
472 struct statistics_rx_bt *cur, *old;
474 cur = &pkt->u.stats_bt.rx;
475 old = &priv->_agn.statistics_bt.rx;
477 delta = le32_to_cpu(cur->ofdm.plcp_err) -
478 le32_to_cpu(old->ofdm.plcp_err) +
479 le32_to_cpu(cur->ofdm_ht.plcp_err) -
480 le32_to_cpu(old->ofdm_ht.plcp_err);
481 } else {
482 struct statistics_rx *cur, *old;
484 cur = &pkt->u.stats.rx;
485 old = &priv->_agn.statistics.rx;
487 delta = le32_to_cpu(cur->ofdm.plcp_err) -
488 le32_to_cpu(old->ofdm.plcp_err) +
489 le32_to_cpu(cur->ofdm_ht.plcp_err) -
490 le32_to_cpu(old->ofdm_ht.plcp_err);
493 /* Can be negative if firmware reseted statistics */
494 if (delta <= 0)
495 return true;
497 if ((delta * 100 / msecs) > threshold) {
498 IWL_DEBUG_RADIO(priv,
499 "plcp health threshold %u delta %d msecs %u\n",
500 threshold, delta, msecs);
501 return false;
504 return true;
507 static void iwl_recover_from_statistics(struct iwl_priv *priv,
508 struct iwl_rx_packet *pkt)
510 const struct iwl_mod_params *mod_params = priv->cfg->mod_params;
512 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
513 !iwl_is_any_associated(priv))
514 return;
516 if (mod_params->ack_check && !iwl_good_ack_health(priv, pkt)) {
517 IWL_ERR(priv, "low ack count detected, restart firmware\n");
518 if (!iwl_force_reset(priv, IWL_FW_RESET, false))
519 return;
522 if (mod_params->plcp_check && !iwl_good_plcp_health(priv, pkt))
523 iwl_force_reset(priv, IWL_RF_RESET, false);
526 /* Calculate noise level, based on measurements during network silence just
527 * before arriving beacon. This measurement can be done only if we know
528 * exactly when to expect beacons, therefore only when we're associated. */
529 static void iwl_rx_calc_noise(struct iwl_priv *priv)
531 struct statistics_rx_non_phy *rx_info;
532 int num_active_rx = 0;
533 int total_silence = 0;
534 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
535 int last_rx_noise;
537 if (iwl_bt_statistics(priv))
538 rx_info = &(priv->_agn.statistics_bt.rx.general.common);
539 else
540 rx_info = &(priv->_agn.statistics.rx.general);
541 bcn_silence_a =
542 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
543 bcn_silence_b =
544 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
545 bcn_silence_c =
546 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
548 if (bcn_silence_a) {
549 total_silence += bcn_silence_a;
550 num_active_rx++;
552 if (bcn_silence_b) {
553 total_silence += bcn_silence_b;
554 num_active_rx++;
556 if (bcn_silence_c) {
557 total_silence += bcn_silence_c;
558 num_active_rx++;
561 /* Average among active antennas */
562 if (num_active_rx)
563 last_rx_noise = (total_silence / num_active_rx) - 107;
564 else
565 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
567 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
568 bcn_silence_a, bcn_silence_b, bcn_silence_c,
569 last_rx_noise);
573 * based on the assumption of all statistics counter are in DWORD
574 * FIXME: This function is for debugging, do not deal with
575 * the case of counters roll-over.
577 static void iwl_accumulative_statistics(struct iwl_priv *priv,
578 __le32 *stats)
580 #ifdef CONFIG_IWLWIFI_DEBUGFS
581 int i, size;
582 __le32 *prev_stats;
583 u32 *accum_stats;
584 u32 *delta, *max_delta;
585 struct statistics_general_common *general, *accum_general;
586 struct statistics_tx *tx, *accum_tx;
588 if (iwl_bt_statistics(priv)) {
589 prev_stats = (__le32 *)&priv->_agn.statistics_bt;
590 accum_stats = (u32 *)&priv->_agn.accum_statistics_bt;
591 size = sizeof(struct iwl_bt_notif_statistics);
592 general = &priv->_agn.statistics_bt.general.common;
593 accum_general = &priv->_agn.accum_statistics_bt.general.common;
594 tx = &priv->_agn.statistics_bt.tx;
595 accum_tx = &priv->_agn.accum_statistics_bt.tx;
596 delta = (u32 *)&priv->_agn.delta_statistics_bt;
597 max_delta = (u32 *)&priv->_agn.max_delta_bt;
598 } else {
599 prev_stats = (__le32 *)&priv->_agn.statistics;
600 accum_stats = (u32 *)&priv->_agn.accum_statistics;
601 size = sizeof(struct iwl_notif_statistics);
602 general = &priv->_agn.statistics.general.common;
603 accum_general = &priv->_agn.accum_statistics.general.common;
604 tx = &priv->_agn.statistics.tx;
605 accum_tx = &priv->_agn.accum_statistics.tx;
606 delta = (u32 *)&priv->_agn.delta_statistics;
607 max_delta = (u32 *)&priv->_agn.max_delta;
609 for (i = sizeof(__le32); i < size;
610 i += sizeof(__le32), stats++, prev_stats++, delta++,
611 max_delta++, accum_stats++) {
612 if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
613 *delta = (le32_to_cpu(*stats) -
614 le32_to_cpu(*prev_stats));
615 *accum_stats += *delta;
616 if (*delta > *max_delta)
617 *max_delta = *delta;
621 /* reset accumulative statistics for "no-counter" type statistics */
622 accum_general->temperature = general->temperature;
623 accum_general->temperature_m = general->temperature_m;
624 accum_general->ttl_timestamp = general->ttl_timestamp;
625 accum_tx->tx_power.ant_a = tx->tx_power.ant_a;
626 accum_tx->tx_power.ant_b = tx->tx_power.ant_b;
627 accum_tx->tx_power.ant_c = tx->tx_power.ant_c;
628 #endif
631 static void iwl_rx_statistics(struct iwl_priv *priv,
632 struct iwl_rx_mem_buffer *rxb)
634 const int reg_recalib_period = 60;
635 int change;
636 struct iwl_rx_packet *pkt = rxb_addr(rxb);
638 if (iwl_bt_statistics(priv)) {
639 IWL_DEBUG_RX(priv,
640 "Statistics notification received (%d vs %d).\n",
641 (int)sizeof(struct iwl_bt_notif_statistics),
642 le32_to_cpu(pkt->len_n_flags) &
643 FH_RSCSR_FRAME_SIZE_MSK);
645 change = ((priv->_agn.statistics_bt.general.common.temperature !=
646 pkt->u.stats_bt.general.common.temperature) ||
647 ((priv->_agn.statistics_bt.flag &
648 STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
649 (pkt->u.stats_bt.flag &
650 STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
652 iwl_accumulative_statistics(priv, (__le32 *)&pkt->u.stats_bt);
653 } else {
654 IWL_DEBUG_RX(priv,
655 "Statistics notification received (%d vs %d).\n",
656 (int)sizeof(struct iwl_notif_statistics),
657 le32_to_cpu(pkt->len_n_flags) &
658 FH_RSCSR_FRAME_SIZE_MSK);
660 change = ((priv->_agn.statistics.general.common.temperature !=
661 pkt->u.stats.general.common.temperature) ||
662 ((priv->_agn.statistics.flag &
663 STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
664 (pkt->u.stats.flag &
665 STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
667 iwl_accumulative_statistics(priv, (__le32 *)&pkt->u.stats);
670 iwl_recover_from_statistics(priv, pkt);
672 if (iwl_bt_statistics(priv))
673 memcpy(&priv->_agn.statistics_bt, &pkt->u.stats_bt,
674 sizeof(priv->_agn.statistics_bt));
675 else
676 memcpy(&priv->_agn.statistics, &pkt->u.stats,
677 sizeof(priv->_agn.statistics));
679 set_bit(STATUS_STATISTICS, &priv->status);
681 /* Reschedule the statistics timer to occur in
682 * reg_recalib_period seconds to ensure we get a
683 * thermal update even if the uCode doesn't give
684 * us one */
685 mod_timer(&priv->statistics_periodic, jiffies +
686 msecs_to_jiffies(reg_recalib_period * 1000));
688 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
689 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
690 iwl_rx_calc_noise(priv);
691 queue_work(priv->workqueue, &priv->run_time_calib_work);
693 if (priv->cfg->ops->lib->temp_ops.temperature && change)
694 priv->cfg->ops->lib->temp_ops.temperature(priv);
697 static void iwl_rx_reply_statistics(struct iwl_priv *priv,
698 struct iwl_rx_mem_buffer *rxb)
700 struct iwl_rx_packet *pkt = rxb_addr(rxb);
702 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
703 #ifdef CONFIG_IWLWIFI_DEBUGFS
704 memset(&priv->_agn.accum_statistics, 0,
705 sizeof(struct iwl_notif_statistics));
706 memset(&priv->_agn.delta_statistics, 0,
707 sizeof(struct iwl_notif_statistics));
708 memset(&priv->_agn.max_delta, 0,
709 sizeof(struct iwl_notif_statistics));
710 memset(&priv->_agn.accum_statistics_bt, 0,
711 sizeof(struct iwl_bt_notif_statistics));
712 memset(&priv->_agn.delta_statistics_bt, 0,
713 sizeof(struct iwl_bt_notif_statistics));
714 memset(&priv->_agn.max_delta_bt, 0,
715 sizeof(struct iwl_bt_notif_statistics));
716 #endif
717 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
719 iwl_rx_statistics(priv, rxb);
722 /* Handle notification from uCode that card's power state is changing
723 * due to software, hardware, or critical temperature RFKILL */
724 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
725 struct iwl_rx_mem_buffer *rxb)
727 struct iwl_rx_packet *pkt = rxb_addr(rxb);
728 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
729 unsigned long status = priv->status;
731 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
732 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
733 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
734 (flags & CT_CARD_DISABLED) ?
735 "Reached" : "Not reached");
737 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
738 CT_CARD_DISABLED)) {
740 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
741 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
743 iwl_write_direct32(priv, HBUS_TARG_MBX_C,
744 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
746 if (!(flags & RXON_CARD_DISABLED)) {
747 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
748 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
749 iwl_write_direct32(priv, HBUS_TARG_MBX_C,
750 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
752 if (flags & CT_CARD_DISABLED)
753 iwl_tt_enter_ct_kill(priv);
755 if (!(flags & CT_CARD_DISABLED))
756 iwl_tt_exit_ct_kill(priv);
758 if (flags & HW_CARD_DISABLED)
759 set_bit(STATUS_RF_KILL_HW, &priv->status);
760 else
761 clear_bit(STATUS_RF_KILL_HW, &priv->status);
764 if (!(flags & RXON_CARD_DISABLED))
765 iwl_scan_cancel(priv);
767 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
768 test_bit(STATUS_RF_KILL_HW, &priv->status)))
769 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
770 test_bit(STATUS_RF_KILL_HW, &priv->status));
771 else
772 wake_up_interruptible(&priv->wait_command_queue);
775 static void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
776 struct iwl_rx_mem_buffer *rxb)
779 struct iwl_rx_packet *pkt = rxb_addr(rxb);
780 struct iwl_missed_beacon_notif *missed_beacon;
782 missed_beacon = &pkt->u.missed_beacon;
783 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
784 priv->missed_beacon_threshold) {
785 IWL_DEBUG_CALIB(priv,
786 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
787 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
788 le32_to_cpu(missed_beacon->total_missed_becons),
789 le32_to_cpu(missed_beacon->num_recvd_beacons),
790 le32_to_cpu(missed_beacon->num_expected_beacons));
791 if (!test_bit(STATUS_SCANNING, &priv->status))
792 iwl_init_sensitivity(priv);
796 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
797 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
798 static void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
799 struct iwl_rx_mem_buffer *rxb)
801 struct iwl_rx_packet *pkt = rxb_addr(rxb);
803 priv->_agn.last_phy_res_valid = true;
804 memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
805 sizeof(struct iwl_rx_phy_res));
809 * returns non-zero if packet should be dropped
811 static int iwl_set_decrypted_flag(struct iwl_priv *priv,
812 struct ieee80211_hdr *hdr,
813 u32 decrypt_res,
814 struct ieee80211_rx_status *stats)
816 u16 fc = le16_to_cpu(hdr->frame_control);
819 * All contexts have the same setting here due to it being
820 * a module parameter, so OK to check any context.
822 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
823 RXON_FILTER_DIS_DECRYPT_MSK)
824 return 0;
826 if (!(fc & IEEE80211_FCTL_PROTECTED))
827 return 0;
829 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
830 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
831 case RX_RES_STATUS_SEC_TYPE_TKIP:
832 /* The uCode has got a bad phase 1 Key, pushes the packet.
833 * Decryption will be done in SW. */
834 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
835 RX_RES_STATUS_BAD_KEY_TTAK)
836 break;
838 case RX_RES_STATUS_SEC_TYPE_WEP:
839 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
840 RX_RES_STATUS_BAD_ICV_MIC) {
841 /* bad ICV, the packet is destroyed since the
842 * decryption is inplace, drop it */
843 IWL_DEBUG_RX(priv, "Packet destroyed\n");
844 return -1;
846 case RX_RES_STATUS_SEC_TYPE_CCMP:
847 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
848 RX_RES_STATUS_DECRYPT_OK) {
849 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
850 stats->flag |= RX_FLAG_DECRYPTED;
852 break;
854 default:
855 break;
857 return 0;
860 static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
861 struct ieee80211_hdr *hdr,
862 u16 len,
863 u32 ampdu_status,
864 struct iwl_rx_mem_buffer *rxb,
865 struct ieee80211_rx_status *stats)
867 struct sk_buff *skb;
868 __le16 fc = hdr->frame_control;
870 /* We only process data packets if the interface is open */
871 if (unlikely(!priv->is_open)) {
872 IWL_DEBUG_DROP_LIMIT(priv,
873 "Dropping packet while interface is not open.\n");
874 return;
877 /* In case of HW accelerated crypto and bad decryption, drop */
878 if (!priv->cfg->mod_params->sw_crypto &&
879 iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
880 return;
882 skb = dev_alloc_skb(128);
883 if (!skb) {
884 IWL_ERR(priv, "dev_alloc_skb failed\n");
885 return;
888 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
890 iwl_update_stats(priv, false, fc, len);
891 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
893 ieee80211_rx(priv->hw, skb);
894 priv->alloc_rxb_page--;
895 rxb->page = NULL;
898 static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
900 u32 decrypt_out = 0;
902 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
903 RX_RES_STATUS_STATION_FOUND)
904 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
905 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
907 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
909 /* packet was not encrypted */
910 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
911 RX_RES_STATUS_SEC_TYPE_NONE)
912 return decrypt_out;
914 /* packet was encrypted with unknown alg */
915 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
916 RX_RES_STATUS_SEC_TYPE_ERR)
917 return decrypt_out;
919 /* decryption was not done in HW */
920 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
921 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
922 return decrypt_out;
924 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
926 case RX_RES_STATUS_SEC_TYPE_CCMP:
927 /* alg is CCM: check MIC only */
928 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
929 /* Bad MIC */
930 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
931 else
932 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
934 break;
936 case RX_RES_STATUS_SEC_TYPE_TKIP:
937 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
938 /* Bad TTAK */
939 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
940 break;
942 /* fall through if TTAK OK */
943 default:
944 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
945 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
946 else
947 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
948 break;
951 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
952 decrypt_in, decrypt_out);
954 return decrypt_out;
957 /* Called for REPLY_RX (legacy ABG frames), or
958 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
959 static void iwl_rx_reply_rx(struct iwl_priv *priv,
960 struct iwl_rx_mem_buffer *rxb)
962 struct ieee80211_hdr *header;
963 struct ieee80211_rx_status rx_status;
964 struct iwl_rx_packet *pkt = rxb_addr(rxb);
965 struct iwl_rx_phy_res *phy_res;
966 __le32 rx_pkt_status;
967 struct iwl_rx_mpdu_res_start *amsdu;
968 u32 len;
969 u32 ampdu_status;
970 u32 rate_n_flags;
973 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
974 * REPLY_RX: physical layer info is in this buffer
975 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
976 * command and cached in priv->last_phy_res
978 * Here we set up local variables depending on which command is
979 * received.
981 if (pkt->hdr.cmd == REPLY_RX) {
982 phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
983 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
984 + phy_res->cfg_phy_cnt);
986 len = le16_to_cpu(phy_res->byte_count);
987 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
988 phy_res->cfg_phy_cnt + len);
989 ampdu_status = le32_to_cpu(rx_pkt_status);
990 } else {
991 if (!priv->_agn.last_phy_res_valid) {
992 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
993 return;
995 phy_res = &priv->_agn.last_phy_res;
996 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
997 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
998 len = le16_to_cpu(amsdu->byte_count);
999 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
1000 ampdu_status = iwl_translate_rx_status(priv,
1001 le32_to_cpu(rx_pkt_status));
1004 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
1005 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
1006 phy_res->cfg_phy_cnt);
1007 return;
1010 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
1011 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
1012 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
1013 le32_to_cpu(rx_pkt_status));
1014 return;
1017 /* This will be used in several places later */
1018 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
1020 /* rx_status carries information about the packet to mac80211 */
1021 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
1022 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
1023 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
1024 rx_status.freq =
1025 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
1026 rx_status.band);
1027 rx_status.rate_idx =
1028 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
1029 rx_status.flag = 0;
1031 /* TSF isn't reliable. In order to allow smooth user experience,
1032 * this W/A doesn't propagate it to the mac80211 */
1033 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
1035 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
1037 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
1038 rx_status.signal = priv->cfg->ops->utils->calc_rssi(priv, phy_res);
1040 iwl_dbg_log_rx_data_frame(priv, len, header);
1041 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
1042 rx_status.signal, (unsigned long long)rx_status.mactime);
1045 * "antenna number"
1047 * It seems that the antenna field in the phy flags value
1048 * is actually a bit field. This is undefined by radiotap,
1049 * it wants an actual antenna number but I always get "7"
1050 * for most legacy frames I receive indicating that the
1051 * same frame was received on all three RX chains.
1053 * I think this field should be removed in favor of a
1054 * new 802.11n radiotap field "RX chains" that is defined
1055 * as a bitmask.
1057 rx_status.antenna =
1058 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
1059 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
1061 /* set the preamble flag if appropriate */
1062 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
1063 rx_status.flag |= RX_FLAG_SHORTPRE;
1065 /* Set up the HT phy flags */
1066 if (rate_n_flags & RATE_MCS_HT_MSK)
1067 rx_status.flag |= RX_FLAG_HT;
1068 if (rate_n_flags & RATE_MCS_HT40_MSK)
1069 rx_status.flag |= RX_FLAG_40MHZ;
1070 if (rate_n_flags & RATE_MCS_SGI_MSK)
1071 rx_status.flag |= RX_FLAG_SHORT_GI;
1073 iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1074 rxb, &rx_status);
1078 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
1080 * Setup the RX handlers for each of the reply types sent from the uCode
1081 * to the host.
1083 void iwl_setup_rx_handlers(struct iwl_priv *priv)
1085 void (**handlers)(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb);
1087 handlers = priv->rx_handlers;
1089 handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
1090 handlers[REPLY_ERROR] = iwl_rx_reply_error;
1091 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
1092 handlers[SPECTRUM_MEASURE_NOTIFICATION] = iwl_rx_spectrum_measure_notif;
1093 handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
1094 handlers[PM_DEBUG_STATISTIC_NOTIFIC] = iwl_rx_pm_debug_statistics_notif;
1095 handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
1098 * The same handler is used for both the REPLY to a discrete
1099 * statistics request from the host as well as for the periodic
1100 * statistics notifications (after received beacons) from the uCode.
1102 handlers[REPLY_STATISTICS_CMD] = iwl_rx_reply_statistics;
1103 handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
1105 iwl_setup_rx_scan_handlers(priv);
1107 handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
1108 handlers[MISSED_BEACONS_NOTIFICATION] = iwl_rx_missed_beacon_notif;
1110 /* Rx handlers */
1111 handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
1112 handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
1114 /* block ack */
1115 handlers[REPLY_COMPRESSED_BA] = iwlagn_rx_reply_compressed_ba;
1117 /* Set up hardware specific Rx handlers */
1118 priv->cfg->ops->lib->rx_handler_setup(priv);