iwlwifi: avoid too frequent recover from statistics
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwlwifi / iwl-rx.c
blob7dc2d39e5cd6ba1914b652fd22337aa82775fe56
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, unsigned int msecs)
454 int delta;
455 int threshold = priv->cfg->base_params->plcp_delta_threshold;
457 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
458 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
459 return true;
462 if (iwl_bt_statistics(priv)) {
463 struct statistics_rx_bt *cur, *old;
465 cur = &pkt->u.stats_bt.rx;
466 old = &priv->_agn.statistics_bt.rx;
468 delta = le32_to_cpu(cur->ofdm.plcp_err) -
469 le32_to_cpu(old->ofdm.plcp_err) +
470 le32_to_cpu(cur->ofdm_ht.plcp_err) -
471 le32_to_cpu(old->ofdm_ht.plcp_err);
472 } else {
473 struct statistics_rx *cur, *old;
475 cur = &pkt->u.stats.rx;
476 old = &priv->_agn.statistics.rx;
478 delta = le32_to_cpu(cur->ofdm.plcp_err) -
479 le32_to_cpu(old->ofdm.plcp_err) +
480 le32_to_cpu(cur->ofdm_ht.plcp_err) -
481 le32_to_cpu(old->ofdm_ht.plcp_err);
484 /* Can be negative if firmware reseted statistics */
485 if (delta <= 0)
486 return true;
488 if ((delta * 100 / msecs) > threshold) {
489 IWL_DEBUG_RADIO(priv,
490 "plcp health threshold %u delta %d msecs %u\n",
491 threshold, delta, msecs);
492 return false;
495 return true;
498 static void iwl_recover_from_statistics(struct iwl_priv *priv,
499 struct iwl_rx_packet *pkt)
501 const struct iwl_mod_params *mod_params = priv->cfg->mod_params;
502 unsigned int msecs;
503 unsigned long stamp;
505 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
506 return;
508 stamp = jiffies;
509 msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
511 /* Only gather statistics and update time stamp when not associated */
512 if (!iwl_is_any_associated(priv))
513 goto out;
515 /* Do not check/recover when do not have enough statistics data */
516 if (msecs < 99)
517 return;
519 if (mod_params->ack_check && !iwl_good_ack_health(priv, pkt)) {
520 IWL_ERR(priv, "low ack count detected, restart firmware\n");
521 if (!iwl_force_reset(priv, IWL_FW_RESET, false))
522 return;
525 if (mod_params->plcp_check && !iwl_good_plcp_health(priv, pkt, msecs))
526 iwl_force_reset(priv, IWL_RF_RESET, false);
528 out:
529 if (iwl_bt_statistics(priv))
530 memcpy(&priv->_agn.statistics_bt, &pkt->u.stats_bt,
531 sizeof(priv->_agn.statistics_bt));
532 else
533 memcpy(&priv->_agn.statistics, &pkt->u.stats,
534 sizeof(priv->_agn.statistics));
536 priv->rx_statistics_jiffies = stamp;
539 /* Calculate noise level, based on measurements during network silence just
540 * before arriving beacon. This measurement can be done only if we know
541 * exactly when to expect beacons, therefore only when we're associated. */
542 static void iwl_rx_calc_noise(struct iwl_priv *priv)
544 struct statistics_rx_non_phy *rx_info;
545 int num_active_rx = 0;
546 int total_silence = 0;
547 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
548 int last_rx_noise;
550 if (iwl_bt_statistics(priv))
551 rx_info = &(priv->_agn.statistics_bt.rx.general.common);
552 else
553 rx_info = &(priv->_agn.statistics.rx.general);
554 bcn_silence_a =
555 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
556 bcn_silence_b =
557 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
558 bcn_silence_c =
559 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
561 if (bcn_silence_a) {
562 total_silence += bcn_silence_a;
563 num_active_rx++;
565 if (bcn_silence_b) {
566 total_silence += bcn_silence_b;
567 num_active_rx++;
569 if (bcn_silence_c) {
570 total_silence += bcn_silence_c;
571 num_active_rx++;
574 /* Average among active antennas */
575 if (num_active_rx)
576 last_rx_noise = (total_silence / num_active_rx) - 107;
577 else
578 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
580 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
581 bcn_silence_a, bcn_silence_b, bcn_silence_c,
582 last_rx_noise);
586 * based on the assumption of all statistics counter are in DWORD
587 * FIXME: This function is for debugging, do not deal with
588 * the case of counters roll-over.
590 static void iwl_accumulative_statistics(struct iwl_priv *priv,
591 __le32 *stats)
593 #ifdef CONFIG_IWLWIFI_DEBUGFS
594 int i, size;
595 __le32 *prev_stats;
596 u32 *accum_stats;
597 u32 *delta, *max_delta;
598 struct statistics_general_common *general, *accum_general;
599 struct statistics_tx *tx, *accum_tx;
601 if (iwl_bt_statistics(priv)) {
602 prev_stats = (__le32 *)&priv->_agn.statistics_bt;
603 accum_stats = (u32 *)&priv->_agn.accum_statistics_bt;
604 size = sizeof(struct iwl_bt_notif_statistics);
605 general = &priv->_agn.statistics_bt.general.common;
606 accum_general = &priv->_agn.accum_statistics_bt.general.common;
607 tx = &priv->_agn.statistics_bt.tx;
608 accum_tx = &priv->_agn.accum_statistics_bt.tx;
609 delta = (u32 *)&priv->_agn.delta_statistics_bt;
610 max_delta = (u32 *)&priv->_agn.max_delta_bt;
611 } else {
612 prev_stats = (__le32 *)&priv->_agn.statistics;
613 accum_stats = (u32 *)&priv->_agn.accum_statistics;
614 size = sizeof(struct iwl_notif_statistics);
615 general = &priv->_agn.statistics.general.common;
616 accum_general = &priv->_agn.accum_statistics.general.common;
617 tx = &priv->_agn.statistics.tx;
618 accum_tx = &priv->_agn.accum_statistics.tx;
619 delta = (u32 *)&priv->_agn.delta_statistics;
620 max_delta = (u32 *)&priv->_agn.max_delta;
622 for (i = sizeof(__le32); i < size;
623 i += sizeof(__le32), stats++, prev_stats++, delta++,
624 max_delta++, accum_stats++) {
625 if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
626 *delta = (le32_to_cpu(*stats) -
627 le32_to_cpu(*prev_stats));
628 *accum_stats += *delta;
629 if (*delta > *max_delta)
630 *max_delta = *delta;
634 /* reset accumulative statistics for "no-counter" type statistics */
635 accum_general->temperature = general->temperature;
636 accum_general->temperature_m = general->temperature_m;
637 accum_general->ttl_timestamp = general->ttl_timestamp;
638 accum_tx->tx_power.ant_a = tx->tx_power.ant_a;
639 accum_tx->tx_power.ant_b = tx->tx_power.ant_b;
640 accum_tx->tx_power.ant_c = tx->tx_power.ant_c;
641 #endif
644 static void iwl_rx_statistics(struct iwl_priv *priv,
645 struct iwl_rx_mem_buffer *rxb)
647 const int reg_recalib_period = 60;
648 int change;
649 struct iwl_rx_packet *pkt = rxb_addr(rxb);
651 if (iwl_bt_statistics(priv)) {
652 IWL_DEBUG_RX(priv,
653 "Statistics notification received (%d vs %d).\n",
654 (int)sizeof(struct iwl_bt_notif_statistics),
655 le32_to_cpu(pkt->len_n_flags) &
656 FH_RSCSR_FRAME_SIZE_MSK);
658 change = ((priv->_agn.statistics_bt.general.common.temperature !=
659 pkt->u.stats_bt.general.common.temperature) ||
660 ((priv->_agn.statistics_bt.flag &
661 STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
662 (pkt->u.stats_bt.flag &
663 STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
665 iwl_accumulative_statistics(priv, (__le32 *)&pkt->u.stats_bt);
666 } else {
667 IWL_DEBUG_RX(priv,
668 "Statistics notification received (%d vs %d).\n",
669 (int)sizeof(struct iwl_notif_statistics),
670 le32_to_cpu(pkt->len_n_flags) &
671 FH_RSCSR_FRAME_SIZE_MSK);
673 change = ((priv->_agn.statistics.general.common.temperature !=
674 pkt->u.stats.general.common.temperature) ||
675 ((priv->_agn.statistics.flag &
676 STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
677 (pkt->u.stats.flag &
678 STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
680 iwl_accumulative_statistics(priv, (__le32 *)&pkt->u.stats);
683 iwl_recover_from_statistics(priv, pkt);
685 set_bit(STATUS_STATISTICS, &priv->status);
687 /* Reschedule the statistics timer to occur in
688 * reg_recalib_period seconds to ensure we get a
689 * thermal update even if the uCode doesn't give
690 * us one */
691 mod_timer(&priv->statistics_periodic, jiffies +
692 msecs_to_jiffies(reg_recalib_period * 1000));
694 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
695 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
696 iwl_rx_calc_noise(priv);
697 queue_work(priv->workqueue, &priv->run_time_calib_work);
699 if (priv->cfg->ops->lib->temp_ops.temperature && change)
700 priv->cfg->ops->lib->temp_ops.temperature(priv);
703 static void iwl_rx_reply_statistics(struct iwl_priv *priv,
704 struct iwl_rx_mem_buffer *rxb)
706 struct iwl_rx_packet *pkt = rxb_addr(rxb);
708 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
709 #ifdef CONFIG_IWLWIFI_DEBUGFS
710 memset(&priv->_agn.accum_statistics, 0,
711 sizeof(struct iwl_notif_statistics));
712 memset(&priv->_agn.delta_statistics, 0,
713 sizeof(struct iwl_notif_statistics));
714 memset(&priv->_agn.max_delta, 0,
715 sizeof(struct iwl_notif_statistics));
716 memset(&priv->_agn.accum_statistics_bt, 0,
717 sizeof(struct iwl_bt_notif_statistics));
718 memset(&priv->_agn.delta_statistics_bt, 0,
719 sizeof(struct iwl_bt_notif_statistics));
720 memset(&priv->_agn.max_delta_bt, 0,
721 sizeof(struct iwl_bt_notif_statistics));
722 #endif
723 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
725 iwl_rx_statistics(priv, rxb);
728 /* Handle notification from uCode that card's power state is changing
729 * due to software, hardware, or critical temperature RFKILL */
730 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
731 struct iwl_rx_mem_buffer *rxb)
733 struct iwl_rx_packet *pkt = rxb_addr(rxb);
734 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
735 unsigned long status = priv->status;
737 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
738 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
739 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
740 (flags & CT_CARD_DISABLED) ?
741 "Reached" : "Not reached");
743 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
744 CT_CARD_DISABLED)) {
746 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
747 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 & RXON_CARD_DISABLED)) {
753 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
754 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
755 iwl_write_direct32(priv, HBUS_TARG_MBX_C,
756 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
758 if (flags & CT_CARD_DISABLED)
759 iwl_tt_enter_ct_kill(priv);
761 if (!(flags & CT_CARD_DISABLED))
762 iwl_tt_exit_ct_kill(priv);
764 if (flags & HW_CARD_DISABLED)
765 set_bit(STATUS_RF_KILL_HW, &priv->status);
766 else
767 clear_bit(STATUS_RF_KILL_HW, &priv->status);
770 if (!(flags & RXON_CARD_DISABLED))
771 iwl_scan_cancel(priv);
773 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
774 test_bit(STATUS_RF_KILL_HW, &priv->status)))
775 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
776 test_bit(STATUS_RF_KILL_HW, &priv->status));
777 else
778 wake_up_interruptible(&priv->wait_command_queue);
781 static void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
782 struct iwl_rx_mem_buffer *rxb)
785 struct iwl_rx_packet *pkt = rxb_addr(rxb);
786 struct iwl_missed_beacon_notif *missed_beacon;
788 missed_beacon = &pkt->u.missed_beacon;
789 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
790 priv->missed_beacon_threshold) {
791 IWL_DEBUG_CALIB(priv,
792 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
793 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
794 le32_to_cpu(missed_beacon->total_missed_becons),
795 le32_to_cpu(missed_beacon->num_recvd_beacons),
796 le32_to_cpu(missed_beacon->num_expected_beacons));
797 if (!test_bit(STATUS_SCANNING, &priv->status))
798 iwl_init_sensitivity(priv);
802 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
803 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
804 static void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
805 struct iwl_rx_mem_buffer *rxb)
807 struct iwl_rx_packet *pkt = rxb_addr(rxb);
809 priv->_agn.last_phy_res_valid = true;
810 memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
811 sizeof(struct iwl_rx_phy_res));
815 * returns non-zero if packet should be dropped
817 static int iwl_set_decrypted_flag(struct iwl_priv *priv,
818 struct ieee80211_hdr *hdr,
819 u32 decrypt_res,
820 struct ieee80211_rx_status *stats)
822 u16 fc = le16_to_cpu(hdr->frame_control);
825 * All contexts have the same setting here due to it being
826 * a module parameter, so OK to check any context.
828 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
829 RXON_FILTER_DIS_DECRYPT_MSK)
830 return 0;
832 if (!(fc & IEEE80211_FCTL_PROTECTED))
833 return 0;
835 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
836 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
837 case RX_RES_STATUS_SEC_TYPE_TKIP:
838 /* The uCode has got a bad phase 1 Key, pushes the packet.
839 * Decryption will be done in SW. */
840 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
841 RX_RES_STATUS_BAD_KEY_TTAK)
842 break;
844 case RX_RES_STATUS_SEC_TYPE_WEP:
845 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
846 RX_RES_STATUS_BAD_ICV_MIC) {
847 /* bad ICV, the packet is destroyed since the
848 * decryption is inplace, drop it */
849 IWL_DEBUG_RX(priv, "Packet destroyed\n");
850 return -1;
852 case RX_RES_STATUS_SEC_TYPE_CCMP:
853 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
854 RX_RES_STATUS_DECRYPT_OK) {
855 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
856 stats->flag |= RX_FLAG_DECRYPTED;
858 break;
860 default:
861 break;
863 return 0;
866 static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
867 struct ieee80211_hdr *hdr,
868 u16 len,
869 u32 ampdu_status,
870 struct iwl_rx_mem_buffer *rxb,
871 struct ieee80211_rx_status *stats)
873 struct sk_buff *skb;
874 __le16 fc = hdr->frame_control;
876 /* We only process data packets if the interface is open */
877 if (unlikely(!priv->is_open)) {
878 IWL_DEBUG_DROP_LIMIT(priv,
879 "Dropping packet while interface is not open.\n");
880 return;
883 /* In case of HW accelerated crypto and bad decryption, drop */
884 if (!priv->cfg->mod_params->sw_crypto &&
885 iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
886 return;
888 skb = dev_alloc_skb(128);
889 if (!skb) {
890 IWL_ERR(priv, "dev_alloc_skb failed\n");
891 return;
894 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
896 iwl_update_stats(priv, false, fc, len);
897 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
899 ieee80211_rx(priv->hw, skb);
900 priv->alloc_rxb_page--;
901 rxb->page = NULL;
904 static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
906 u32 decrypt_out = 0;
908 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
909 RX_RES_STATUS_STATION_FOUND)
910 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
911 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
913 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
915 /* packet was not encrypted */
916 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
917 RX_RES_STATUS_SEC_TYPE_NONE)
918 return decrypt_out;
920 /* packet was encrypted with unknown alg */
921 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
922 RX_RES_STATUS_SEC_TYPE_ERR)
923 return decrypt_out;
925 /* decryption was not done in HW */
926 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
927 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
928 return decrypt_out;
930 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
932 case RX_RES_STATUS_SEC_TYPE_CCMP:
933 /* alg is CCM: check MIC only */
934 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
935 /* Bad MIC */
936 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
937 else
938 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
940 break;
942 case RX_RES_STATUS_SEC_TYPE_TKIP:
943 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
944 /* Bad TTAK */
945 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
946 break;
948 /* fall through if TTAK OK */
949 default:
950 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
951 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
952 else
953 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
954 break;
957 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
958 decrypt_in, decrypt_out);
960 return decrypt_out;
963 /* Called for REPLY_RX (legacy ABG frames), or
964 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
965 static void iwl_rx_reply_rx(struct iwl_priv *priv,
966 struct iwl_rx_mem_buffer *rxb)
968 struct ieee80211_hdr *header;
969 struct ieee80211_rx_status rx_status;
970 struct iwl_rx_packet *pkt = rxb_addr(rxb);
971 struct iwl_rx_phy_res *phy_res;
972 __le32 rx_pkt_status;
973 struct iwl_rx_mpdu_res_start *amsdu;
974 u32 len;
975 u32 ampdu_status;
976 u32 rate_n_flags;
979 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
980 * REPLY_RX: physical layer info is in this buffer
981 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
982 * command and cached in priv->last_phy_res
984 * Here we set up local variables depending on which command is
985 * received.
987 if (pkt->hdr.cmd == REPLY_RX) {
988 phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
989 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
990 + phy_res->cfg_phy_cnt);
992 len = le16_to_cpu(phy_res->byte_count);
993 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
994 phy_res->cfg_phy_cnt + len);
995 ampdu_status = le32_to_cpu(rx_pkt_status);
996 } else {
997 if (!priv->_agn.last_phy_res_valid) {
998 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
999 return;
1001 phy_res = &priv->_agn.last_phy_res;
1002 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
1003 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
1004 len = le16_to_cpu(amsdu->byte_count);
1005 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
1006 ampdu_status = iwl_translate_rx_status(priv,
1007 le32_to_cpu(rx_pkt_status));
1010 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
1011 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
1012 phy_res->cfg_phy_cnt);
1013 return;
1016 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
1017 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
1018 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
1019 le32_to_cpu(rx_pkt_status));
1020 return;
1023 /* This will be used in several places later */
1024 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
1026 /* rx_status carries information about the packet to mac80211 */
1027 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
1028 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
1029 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
1030 rx_status.freq =
1031 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
1032 rx_status.band);
1033 rx_status.rate_idx =
1034 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
1035 rx_status.flag = 0;
1037 /* TSF isn't reliable. In order to allow smooth user experience,
1038 * this W/A doesn't propagate it to the mac80211 */
1039 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
1041 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
1043 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
1044 rx_status.signal = priv->cfg->ops->utils->calc_rssi(priv, phy_res);
1046 iwl_dbg_log_rx_data_frame(priv, len, header);
1047 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
1048 rx_status.signal, (unsigned long long)rx_status.mactime);
1051 * "antenna number"
1053 * It seems that the antenna field in the phy flags value
1054 * is actually a bit field. This is undefined by radiotap,
1055 * it wants an actual antenna number but I always get "7"
1056 * for most legacy frames I receive indicating that the
1057 * same frame was received on all three RX chains.
1059 * I think this field should be removed in favor of a
1060 * new 802.11n radiotap field "RX chains" that is defined
1061 * as a bitmask.
1063 rx_status.antenna =
1064 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
1065 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
1067 /* set the preamble flag if appropriate */
1068 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
1069 rx_status.flag |= RX_FLAG_SHORTPRE;
1071 /* Set up the HT phy flags */
1072 if (rate_n_flags & RATE_MCS_HT_MSK)
1073 rx_status.flag |= RX_FLAG_HT;
1074 if (rate_n_flags & RATE_MCS_HT40_MSK)
1075 rx_status.flag |= RX_FLAG_40MHZ;
1076 if (rate_n_flags & RATE_MCS_SGI_MSK)
1077 rx_status.flag |= RX_FLAG_SHORT_GI;
1079 iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1080 rxb, &rx_status);
1084 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
1086 * Setup the RX handlers for each of the reply types sent from the uCode
1087 * to the host.
1089 void iwl_setup_rx_handlers(struct iwl_priv *priv)
1091 void (**handlers)(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb);
1093 handlers = priv->rx_handlers;
1095 handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
1096 handlers[REPLY_ERROR] = iwl_rx_reply_error;
1097 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
1098 handlers[SPECTRUM_MEASURE_NOTIFICATION] = iwl_rx_spectrum_measure_notif;
1099 handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
1100 handlers[PM_DEBUG_STATISTIC_NOTIFIC] = iwl_rx_pm_debug_statistics_notif;
1101 handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
1104 * The same handler is used for both the REPLY to a discrete
1105 * statistics request from the host as well as for the periodic
1106 * statistics notifications (after received beacons) from the uCode.
1108 handlers[REPLY_STATISTICS_CMD] = iwl_rx_reply_statistics;
1109 handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
1111 iwl_setup_rx_scan_handlers(priv);
1113 handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
1114 handlers[MISSED_BEACONS_NOTIFICATION] = iwl_rx_missed_beacon_notif;
1116 /* Rx handlers */
1117 handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
1118 handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
1120 /* block ack */
1121 handlers[REPLY_COMPRESSED_BA] = iwlagn_rx_reply_compressed_ba;
1123 /* Set up hardware specific Rx handlers */
1124 priv->cfg->ops->lib->rx_handler_setup(priv);