timerfd: Allow timers to be cancelled when clock was set
[linux-2.6/linux-mips.git] / drivers / net / wireless / iwlwifi / iwl-rx.c
blob6f9a2fa04763ed619a51257dbad8977d06208f05
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 <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"
44 /******************************************************************************
46 * RX path functions
48 ******************************************************************************/
51 * Rx theory of operation
53 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
54 * each of which point to Receive Buffers to be filled by the NIC. These get
55 * used not only for Rx frames, but for any command response or notification
56 * from the NIC. The driver and NIC manage the Rx buffers by means
57 * of indexes into the circular buffer.
59 * Rx Queue Indexes
60 * The host/firmware share two index registers for managing the Rx buffers.
62 * The READ index maps to the first position that the firmware may be writing
63 * to -- the driver can read up to (but not including) this position and get
64 * good data.
65 * The READ index is managed by the firmware once the card is enabled.
67 * The WRITE index maps to the last position the driver has read from -- the
68 * position preceding WRITE is the last slot the firmware can place a packet.
70 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
71 * WRITE = READ.
73 * During initialization, the host sets up the READ queue position to the first
74 * INDEX position, and WRITE to the last (READ - 1 wrapped)
76 * When the firmware places a packet in a buffer, it will advance the READ index
77 * and fire the RX interrupt. The driver can then query the READ index and
78 * process as many packets as possible, moving the WRITE index forward as it
79 * resets the Rx queue buffers with new memory.
81 * The management in the driver is as follows:
82 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
83 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
84 * to replenish the iwl->rxq->rx_free.
85 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
86 * iwl->rxq is replenished and the READ INDEX is updated (updating the
87 * 'processed' and 'read' driver indexes as well)
88 * + A received packet is processed and handed to the kernel network stack,
89 * detached from the iwl->rxq. The driver 'processed' index is updated.
90 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
91 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
92 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
93 * were enough free buffers and RX_STALLED is set it is cleared.
96 * Driver sequence:
98 * iwl_rx_queue_alloc() Allocates rx_free
99 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
100 * iwl_rx_queue_restock
101 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
102 * queue, updates firmware pointers, and updates
103 * the WRITE index. If insufficient rx_free buffers
104 * are available, schedules iwl_rx_replenish
106 * -- enable interrupts --
107 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
108 * READ INDEX, detaching the SKB from the pool.
109 * Moves the packet buffer from queue to rx_used.
110 * Calls iwl_rx_queue_restock to refill any empty
111 * slots.
112 * ...
117 * iwl_rx_queue_space - Return number of free slots available in queue.
119 int iwl_rx_queue_space(const struct iwl_rx_queue *q)
121 int s = q->read - q->write;
122 if (s <= 0)
123 s += RX_QUEUE_SIZE;
124 /* keep some buffer to not confuse full and empty queue */
125 s -= 2;
126 if (s < 0)
127 s = 0;
128 return s;
132 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
134 void iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
136 unsigned long flags;
137 u32 rx_wrt_ptr_reg = priv->hw_params.rx_wrt_ptr_reg;
138 u32 reg;
140 spin_lock_irqsave(&q->lock, flags);
142 if (q->need_update == 0)
143 goto exit_unlock;
145 if (priv->cfg->base_params->shadow_reg_enable) {
146 /* shadow register enabled */
147 /* Device expects a multiple of 8 */
148 q->write_actual = (q->write & ~0x7);
149 iwl_write32(priv, rx_wrt_ptr_reg, q->write_actual);
150 } else {
151 /* If power-saving is in use, make sure device is awake */
152 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
153 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
155 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
156 IWL_DEBUG_INFO(priv,
157 "Rx queue requesting wakeup,"
158 " GP1 = 0x%x\n", reg);
159 iwl_set_bit(priv, CSR_GP_CNTRL,
160 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
161 goto exit_unlock;
164 q->write_actual = (q->write & ~0x7);
165 iwl_write_direct32(priv, rx_wrt_ptr_reg,
166 q->write_actual);
168 /* Else device is assumed to be awake */
169 } else {
170 /* Device expects a multiple of 8 */
171 q->write_actual = (q->write & ~0x7);
172 iwl_write_direct32(priv, rx_wrt_ptr_reg,
173 q->write_actual);
176 q->need_update = 0;
178 exit_unlock:
179 spin_unlock_irqrestore(&q->lock, flags);
182 int iwl_rx_queue_alloc(struct iwl_priv *priv)
184 struct iwl_rx_queue *rxq = &priv->rxq;
185 struct device *dev = &priv->pci_dev->dev;
186 int i;
188 spin_lock_init(&rxq->lock);
189 INIT_LIST_HEAD(&rxq->rx_free);
190 INIT_LIST_HEAD(&rxq->rx_used);
192 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
193 rxq->bd = dma_alloc_coherent(dev, 4 * RX_QUEUE_SIZE, &rxq->bd_dma,
194 GFP_KERNEL);
195 if (!rxq->bd)
196 goto err_bd;
198 rxq->rb_stts = dma_alloc_coherent(dev, sizeof(struct iwl_rb_status),
199 &rxq->rb_stts_dma, GFP_KERNEL);
200 if (!rxq->rb_stts)
201 goto err_rb;
203 /* Fill the rx_used queue with _all_ of the Rx buffers */
204 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
205 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
207 /* Set us so that we have processed and used all buffers, but have
208 * not restocked the Rx queue with fresh buffers */
209 rxq->read = rxq->write = 0;
210 rxq->write_actual = 0;
211 rxq->free_count = 0;
212 rxq->need_update = 0;
213 return 0;
215 err_rb:
216 dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
217 rxq->bd_dma);
218 err_bd:
219 return -ENOMEM;
222 /******************************************************************************
224 * Generic RX handler implementations
226 ******************************************************************************/
228 static void iwl_rx_reply_alive(struct iwl_priv *priv,
229 struct iwl_rx_mem_buffer *rxb)
231 struct iwl_rx_packet *pkt = rxb_addr(rxb);
232 struct iwl_alive_resp *palive;
233 struct delayed_work *pwork;
235 palive = &pkt->u.alive_frame;
237 IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
238 "0x%01X 0x%01X\n",
239 palive->is_valid, palive->ver_type,
240 palive->ver_subtype);
242 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
243 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
244 memcpy(&priv->card_alive_init,
245 &pkt->u.alive_frame,
246 sizeof(struct iwl_init_alive_resp));
247 pwork = &priv->init_alive_start;
248 } else {
249 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
250 memcpy(&priv->card_alive, &pkt->u.alive_frame,
251 sizeof(struct iwl_alive_resp));
252 pwork = &priv->alive_start;
255 /* We delay the ALIVE response by 5ms to
256 * give the HW RF Kill time to activate... */
257 if (palive->is_valid == UCODE_VALID_OK)
258 queue_delayed_work(priv->workqueue, pwork,
259 msecs_to_jiffies(5));
260 else {
261 IWL_WARN(priv, "%s uCode did not respond OK.\n",
262 (palive->ver_subtype == INITIALIZE_SUBTYPE) ?
263 "init" : "runtime");
265 * If fail to load init uCode,
266 * let's try to load the init uCode again.
267 * We should not get into this situation, but if it
268 * does happen, we should not move on and loading "runtime"
269 * without proper calibrate the device.
271 if (palive->ver_subtype == INITIALIZE_SUBTYPE)
272 priv->ucode_type = UCODE_NONE;
273 queue_work(priv->workqueue, &priv->restart);
277 static void iwl_rx_reply_error(struct iwl_priv *priv,
278 struct iwl_rx_mem_buffer *rxb)
280 struct iwl_rx_packet *pkt = rxb_addr(rxb);
282 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
283 "seq 0x%04X ser 0x%08X\n",
284 le32_to_cpu(pkt->u.err_resp.error_type),
285 get_cmd_string(pkt->u.err_resp.cmd_id),
286 pkt->u.err_resp.cmd_id,
287 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
288 le32_to_cpu(pkt->u.err_resp.error_info));
291 static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
293 struct iwl_rx_packet *pkt = rxb_addr(rxb);
294 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
296 * MULTI-FIXME
297 * See iwl_mac_channel_switch.
299 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
300 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
302 if (priv->switch_rxon.switch_in_progress) {
303 if (!le32_to_cpu(csa->status) &&
304 (csa->channel == priv->switch_rxon.channel)) {
305 rxon->channel = csa->channel;
306 ctx->staging.channel = csa->channel;
307 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
308 le16_to_cpu(csa->channel));
309 iwl_chswitch_done(priv, true);
310 } else {
311 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
312 le16_to_cpu(csa->channel));
313 iwl_chswitch_done(priv, false);
319 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
320 struct iwl_rx_mem_buffer *rxb)
322 struct iwl_rx_packet *pkt = rxb_addr(rxb);
323 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
325 if (!report->state) {
326 IWL_DEBUG_11H(priv,
327 "Spectrum Measure Notification: Start\n");
328 return;
331 memcpy(&priv->measure_report, report, sizeof(*report));
332 priv->measurement_status |= MEASUREMENT_READY;
335 static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
336 struct iwl_rx_mem_buffer *rxb)
338 #ifdef CONFIG_IWLWIFI_DEBUG
339 struct iwl_rx_packet *pkt = rxb_addr(rxb);
340 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
341 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
342 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
343 #endif
346 static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
347 struct iwl_rx_mem_buffer *rxb)
349 struct iwl_rx_packet *pkt = rxb_addr(rxb);
350 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
351 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
352 "notification for %s:\n", len,
353 get_cmd_string(pkt->hdr.cmd));
354 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
357 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
358 struct iwl_rx_mem_buffer *rxb)
360 struct iwl_rx_packet *pkt = rxb_addr(rxb);
361 struct iwlagn_beacon_notif *beacon = (void *)pkt->u.raw;
362 #ifdef CONFIG_IWLWIFI_DEBUG
363 u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
364 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
366 IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
367 "tsf:0x%.8x%.8x rate:%d\n",
368 status & TX_STATUS_MSK,
369 beacon->beacon_notify_hdr.failure_frame,
370 le32_to_cpu(beacon->ibss_mgr_status),
371 le32_to_cpu(beacon->high_tsf),
372 le32_to_cpu(beacon->low_tsf), rate);
373 #endif
375 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
377 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
378 queue_work(priv->workqueue, &priv->beacon_update);
381 /* the threshold ratio of actual_ack_cnt to expected_ack_cnt in percent */
382 #define ACK_CNT_RATIO (50)
383 #define BA_TIMEOUT_CNT (5)
384 #define BA_TIMEOUT_MAX (16)
387 * iwl_good_ack_health - checks for ACK count ratios, BA timeout retries.
389 * When the ACK count ratio is low and aggregated BA timeout retries exceeding
390 * the BA_TIMEOUT_MAX, reload firmware and bring system back to normal
391 * operation state.
393 static bool iwl_good_ack_health(struct iwl_priv *priv, struct iwl_rx_packet *pkt)
395 int actual_delta, expected_delta, ba_timeout_delta;
396 struct statistics_tx *cur, *old;
398 if (priv->_agn.agg_tids_count)
399 return true;
401 if (iwl_bt_statistics(priv)) {
402 cur = &pkt->u.stats_bt.tx;
403 old = &priv->_agn.statistics_bt.tx;
404 } else {
405 cur = &pkt->u.stats.tx;
406 old = &priv->_agn.statistics.tx;
409 actual_delta = le32_to_cpu(cur->actual_ack_cnt) -
410 le32_to_cpu(old->actual_ack_cnt);
411 expected_delta = le32_to_cpu(cur->expected_ack_cnt) -
412 le32_to_cpu(old->expected_ack_cnt);
414 /* Values should not be negative, but we do not trust the firmware */
415 if (actual_delta <= 0 || expected_delta <= 0)
416 return true;
418 ba_timeout_delta = le32_to_cpu(cur->agg.ba_timeout) -
419 le32_to_cpu(old->agg.ba_timeout);
421 if ((actual_delta * 100 / expected_delta) < ACK_CNT_RATIO &&
422 ba_timeout_delta > BA_TIMEOUT_CNT) {
423 IWL_DEBUG_RADIO(priv, "deltas: actual %d expected %d ba_timeout %d\n",
424 actual_delta, expected_delta, ba_timeout_delta);
426 #ifdef CONFIG_IWLWIFI_DEBUGFS
428 * This is ifdef'ed on DEBUGFS because otherwise the
429 * statistics aren't available. If DEBUGFS is set but
430 * DEBUG is not, these will just compile out.
432 IWL_DEBUG_RADIO(priv, "rx_detected_cnt delta %d\n",
433 priv->_agn.delta_statistics.tx.rx_detected_cnt);
434 IWL_DEBUG_RADIO(priv,
435 "ack_or_ba_timeout_collision delta %d\n",
436 priv->_agn.delta_statistics.tx.ack_or_ba_timeout_collision);
437 #endif
439 if (ba_timeout_delta >= BA_TIMEOUT_MAX)
440 return false;
443 return true;
447 * iwl_good_plcp_health - checks for plcp error.
449 * When the plcp error is exceeding the thresholds, reset the radio
450 * to improve the throughput.
452 static bool iwl_good_plcp_health(struct iwl_priv *priv,
453 struct iwl_rx_packet *pkt, unsigned int msecs)
455 int delta;
456 int threshold = priv->cfg->base_params->plcp_delta_threshold;
458 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
459 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
460 return true;
463 if (iwl_bt_statistics(priv)) {
464 struct statistics_rx_bt *cur, *old;
466 cur = &pkt->u.stats_bt.rx;
467 old = &priv->_agn.statistics_bt.rx;
469 delta = le32_to_cpu(cur->ofdm.plcp_err) -
470 le32_to_cpu(old->ofdm.plcp_err) +
471 le32_to_cpu(cur->ofdm_ht.plcp_err) -
472 le32_to_cpu(old->ofdm_ht.plcp_err);
473 } else {
474 struct statistics_rx *cur, *old;
476 cur = &pkt->u.stats.rx;
477 old = &priv->_agn.statistics.rx;
479 delta = le32_to_cpu(cur->ofdm.plcp_err) -
480 le32_to_cpu(old->ofdm.plcp_err) +
481 le32_to_cpu(cur->ofdm_ht.plcp_err) -
482 le32_to_cpu(old->ofdm_ht.plcp_err);
485 /* Can be negative if firmware reseted statistics */
486 if (delta <= 0)
487 return true;
489 if ((delta * 100 / msecs) > threshold) {
490 IWL_DEBUG_RADIO(priv,
491 "plcp health threshold %u delta %d msecs %u\n",
492 threshold, delta, msecs);
493 return false;
496 return true;
499 static void iwl_recover_from_statistics(struct iwl_priv *priv,
500 struct iwl_rx_packet *pkt)
502 const struct iwl_mod_params *mod_params = priv->cfg->mod_params;
503 unsigned int msecs;
504 unsigned long stamp;
506 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
507 return;
509 stamp = jiffies;
510 msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
512 /* Only gather statistics and update time stamp when not associated */
513 if (!iwl_is_any_associated(priv))
514 goto out;
516 /* Do not check/recover when do not have enough statistics data */
517 if (msecs < 99)
518 return;
520 if (mod_params->ack_check && !iwl_good_ack_health(priv, pkt)) {
521 IWL_ERR(priv, "low ack count detected, restart firmware\n");
522 if (!iwl_force_reset(priv, IWL_FW_RESET, false))
523 return;
526 if (mod_params->plcp_check && !iwl_good_plcp_health(priv, pkt, msecs))
527 iwl_force_reset(priv, IWL_RF_RESET, false);
529 out:
530 if (iwl_bt_statistics(priv))
531 memcpy(&priv->_agn.statistics_bt, &pkt->u.stats_bt,
532 sizeof(priv->_agn.statistics_bt));
533 else
534 memcpy(&priv->_agn.statistics, &pkt->u.stats,
535 sizeof(priv->_agn.statistics));
537 priv->rx_statistics_jiffies = stamp;
540 /* Calculate noise level, based on measurements during network silence just
541 * before arriving beacon. This measurement can be done only if we know
542 * exactly when to expect beacons, therefore only when we're associated. */
543 static void iwl_rx_calc_noise(struct iwl_priv *priv)
545 struct statistics_rx_non_phy *rx_info;
546 int num_active_rx = 0;
547 int total_silence = 0;
548 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
549 int last_rx_noise;
551 if (iwl_bt_statistics(priv))
552 rx_info = &(priv->_agn.statistics_bt.rx.general.common);
553 else
554 rx_info = &(priv->_agn.statistics.rx.general);
555 bcn_silence_a =
556 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
557 bcn_silence_b =
558 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
559 bcn_silence_c =
560 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
562 if (bcn_silence_a) {
563 total_silence += bcn_silence_a;
564 num_active_rx++;
566 if (bcn_silence_b) {
567 total_silence += bcn_silence_b;
568 num_active_rx++;
570 if (bcn_silence_c) {
571 total_silence += bcn_silence_c;
572 num_active_rx++;
575 /* Average among active antennas */
576 if (num_active_rx)
577 last_rx_noise = (total_silence / num_active_rx) - 107;
578 else
579 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
581 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
582 bcn_silence_a, bcn_silence_b, bcn_silence_c,
583 last_rx_noise);
587 * based on the assumption of all statistics counter are in DWORD
588 * FIXME: This function is for debugging, do not deal with
589 * the case of counters roll-over.
591 static void iwl_accumulative_statistics(struct iwl_priv *priv,
592 __le32 *stats)
594 #ifdef CONFIG_IWLWIFI_DEBUGFS
595 int i, size;
596 __le32 *prev_stats;
597 u32 *accum_stats;
598 u32 *delta, *max_delta;
599 struct statistics_general_common *general, *accum_general;
600 struct statistics_tx *tx, *accum_tx;
602 if (iwl_bt_statistics(priv)) {
603 prev_stats = (__le32 *)&priv->_agn.statistics_bt;
604 accum_stats = (u32 *)&priv->_agn.accum_statistics_bt;
605 size = sizeof(struct iwl_bt_notif_statistics);
606 general = &priv->_agn.statistics_bt.general.common;
607 accum_general = &priv->_agn.accum_statistics_bt.general.common;
608 tx = &priv->_agn.statistics_bt.tx;
609 accum_tx = &priv->_agn.accum_statistics_bt.tx;
610 delta = (u32 *)&priv->_agn.delta_statistics_bt;
611 max_delta = (u32 *)&priv->_agn.max_delta_bt;
612 } else {
613 prev_stats = (__le32 *)&priv->_agn.statistics;
614 accum_stats = (u32 *)&priv->_agn.accum_statistics;
615 size = sizeof(struct iwl_notif_statistics);
616 general = &priv->_agn.statistics.general.common;
617 accum_general = &priv->_agn.accum_statistics.general.common;
618 tx = &priv->_agn.statistics.tx;
619 accum_tx = &priv->_agn.accum_statistics.tx;
620 delta = (u32 *)&priv->_agn.delta_statistics;
621 max_delta = (u32 *)&priv->_agn.max_delta;
623 for (i = sizeof(__le32); i < size;
624 i += sizeof(__le32), stats++, prev_stats++, delta++,
625 max_delta++, accum_stats++) {
626 if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
627 *delta = (le32_to_cpu(*stats) -
628 le32_to_cpu(*prev_stats));
629 *accum_stats += *delta;
630 if (*delta > *max_delta)
631 *max_delta = *delta;
635 /* reset accumulative statistics for "no-counter" type statistics */
636 accum_general->temperature = general->temperature;
637 accum_general->temperature_m = general->temperature_m;
638 accum_general->ttl_timestamp = general->ttl_timestamp;
639 accum_tx->tx_power.ant_a = tx->tx_power.ant_a;
640 accum_tx->tx_power.ant_b = tx->tx_power.ant_b;
641 accum_tx->tx_power.ant_c = tx->tx_power.ant_c;
642 #endif
645 static void iwl_rx_statistics(struct iwl_priv *priv,
646 struct iwl_rx_mem_buffer *rxb)
648 const int reg_recalib_period = 60;
649 int change;
650 struct iwl_rx_packet *pkt = rxb_addr(rxb);
652 if (iwl_bt_statistics(priv)) {
653 IWL_DEBUG_RX(priv,
654 "Statistics notification received (%d vs %d).\n",
655 (int)sizeof(struct iwl_bt_notif_statistics),
656 le32_to_cpu(pkt->len_n_flags) &
657 FH_RSCSR_FRAME_SIZE_MSK);
659 change = ((priv->_agn.statistics_bt.general.common.temperature !=
660 pkt->u.stats_bt.general.common.temperature) ||
661 ((priv->_agn.statistics_bt.flag &
662 STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
663 (pkt->u.stats_bt.flag &
664 STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
666 iwl_accumulative_statistics(priv, (__le32 *)&pkt->u.stats_bt);
667 } else {
668 IWL_DEBUG_RX(priv,
669 "Statistics notification received (%d vs %d).\n",
670 (int)sizeof(struct iwl_notif_statistics),
671 le32_to_cpu(pkt->len_n_flags) &
672 FH_RSCSR_FRAME_SIZE_MSK);
674 change = ((priv->_agn.statistics.general.common.temperature !=
675 pkt->u.stats.general.common.temperature) ||
676 ((priv->_agn.statistics.flag &
677 STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
678 (pkt->u.stats.flag &
679 STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
681 iwl_accumulative_statistics(priv, (__le32 *)&pkt->u.stats);
684 iwl_recover_from_statistics(priv, pkt);
686 set_bit(STATUS_STATISTICS, &priv->status);
688 /* Reschedule the statistics timer to occur in
689 * reg_recalib_period seconds to ensure we get a
690 * thermal update even if the uCode doesn't give
691 * us one */
692 mod_timer(&priv->statistics_periodic, jiffies +
693 msecs_to_jiffies(reg_recalib_period * 1000));
695 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
696 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
697 iwl_rx_calc_noise(priv);
698 queue_work(priv->workqueue, &priv->run_time_calib_work);
700 if (priv->cfg->ops->lib->temp_ops.temperature && change)
701 priv->cfg->ops->lib->temp_ops.temperature(priv);
704 static void iwl_rx_reply_statistics(struct iwl_priv *priv,
705 struct iwl_rx_mem_buffer *rxb)
707 struct iwl_rx_packet *pkt = rxb_addr(rxb);
709 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
710 #ifdef CONFIG_IWLWIFI_DEBUGFS
711 memset(&priv->_agn.accum_statistics, 0,
712 sizeof(struct iwl_notif_statistics));
713 memset(&priv->_agn.delta_statistics, 0,
714 sizeof(struct iwl_notif_statistics));
715 memset(&priv->_agn.max_delta, 0,
716 sizeof(struct iwl_notif_statistics));
717 memset(&priv->_agn.accum_statistics_bt, 0,
718 sizeof(struct iwl_bt_notif_statistics));
719 memset(&priv->_agn.delta_statistics_bt, 0,
720 sizeof(struct iwl_bt_notif_statistics));
721 memset(&priv->_agn.max_delta_bt, 0,
722 sizeof(struct iwl_bt_notif_statistics));
723 #endif
724 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
726 iwl_rx_statistics(priv, rxb);
729 /* Handle notification from uCode that card's power state is changing
730 * due to software, hardware, or critical temperature RFKILL */
731 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
732 struct iwl_rx_mem_buffer *rxb)
734 struct iwl_rx_packet *pkt = rxb_addr(rxb);
735 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
736 unsigned long status = priv->status;
738 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
739 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
740 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
741 (flags & CT_CARD_DISABLED) ?
742 "Reached" : "Not reached");
744 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
745 CT_CARD_DISABLED)) {
747 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
748 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
750 iwl_write_direct32(priv, HBUS_TARG_MBX_C,
751 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
753 if (!(flags & RXON_CARD_DISABLED)) {
754 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
755 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
756 iwl_write_direct32(priv, HBUS_TARG_MBX_C,
757 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
759 if (flags & CT_CARD_DISABLED)
760 iwl_tt_enter_ct_kill(priv);
762 if (!(flags & CT_CARD_DISABLED))
763 iwl_tt_exit_ct_kill(priv);
765 if (flags & HW_CARD_DISABLED)
766 set_bit(STATUS_RF_KILL_HW, &priv->status);
767 else
768 clear_bit(STATUS_RF_KILL_HW, &priv->status);
771 if (!(flags & RXON_CARD_DISABLED))
772 iwl_scan_cancel(priv);
774 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
775 test_bit(STATUS_RF_KILL_HW, &priv->status)))
776 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
777 test_bit(STATUS_RF_KILL_HW, &priv->status));
778 else
779 wake_up_interruptible(&priv->wait_command_queue);
782 static void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
783 struct iwl_rx_mem_buffer *rxb)
786 struct iwl_rx_packet *pkt = rxb_addr(rxb);
787 struct iwl_missed_beacon_notif *missed_beacon;
789 missed_beacon = &pkt->u.missed_beacon;
790 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
791 priv->missed_beacon_threshold) {
792 IWL_DEBUG_CALIB(priv,
793 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
794 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
795 le32_to_cpu(missed_beacon->total_missed_becons),
796 le32_to_cpu(missed_beacon->num_recvd_beacons),
797 le32_to_cpu(missed_beacon->num_expected_beacons));
798 if (!test_bit(STATUS_SCANNING, &priv->status))
799 iwl_init_sensitivity(priv);
803 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
804 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
805 static void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
806 struct iwl_rx_mem_buffer *rxb)
808 struct iwl_rx_packet *pkt = rxb_addr(rxb);
810 priv->_agn.last_phy_res_valid = true;
811 memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
812 sizeof(struct iwl_rx_phy_res));
816 * returns non-zero if packet should be dropped
818 static int iwl_set_decrypted_flag(struct iwl_priv *priv,
819 struct ieee80211_hdr *hdr,
820 u32 decrypt_res,
821 struct ieee80211_rx_status *stats)
823 u16 fc = le16_to_cpu(hdr->frame_control);
826 * All contexts have the same setting here due to it being
827 * a module parameter, so OK to check any context.
829 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
830 RXON_FILTER_DIS_DECRYPT_MSK)
831 return 0;
833 if (!(fc & IEEE80211_FCTL_PROTECTED))
834 return 0;
836 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
837 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
838 case RX_RES_STATUS_SEC_TYPE_TKIP:
839 /* The uCode has got a bad phase 1 Key, pushes the packet.
840 * Decryption will be done in SW. */
841 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
842 RX_RES_STATUS_BAD_KEY_TTAK)
843 break;
845 case RX_RES_STATUS_SEC_TYPE_WEP:
846 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
847 RX_RES_STATUS_BAD_ICV_MIC) {
848 /* bad ICV, the packet is destroyed since the
849 * decryption is inplace, drop it */
850 IWL_DEBUG_RX(priv, "Packet destroyed\n");
851 return -1;
853 case RX_RES_STATUS_SEC_TYPE_CCMP:
854 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
855 RX_RES_STATUS_DECRYPT_OK) {
856 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
857 stats->flag |= RX_FLAG_DECRYPTED;
859 break;
861 default:
862 break;
864 return 0;
867 static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
868 struct ieee80211_hdr *hdr,
869 u16 len,
870 u32 ampdu_status,
871 struct iwl_rx_mem_buffer *rxb,
872 struct ieee80211_rx_status *stats)
874 struct sk_buff *skb;
875 __le16 fc = hdr->frame_control;
877 /* We only process data packets if the interface is open */
878 if (unlikely(!priv->is_open)) {
879 IWL_DEBUG_DROP_LIMIT(priv,
880 "Dropping packet while interface is not open.\n");
881 return;
884 /* In case of HW accelerated crypto and bad decryption, drop */
885 if (!priv->cfg->mod_params->sw_crypto &&
886 iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
887 return;
889 skb = dev_alloc_skb(128);
890 if (!skb) {
891 IWL_ERR(priv, "dev_alloc_skb failed\n");
892 return;
895 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
897 iwl_update_stats(priv, false, fc, len);
898 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
900 ieee80211_rx(priv->hw, skb);
901 priv->alloc_rxb_page--;
902 rxb->page = NULL;
905 static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
907 u32 decrypt_out = 0;
909 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
910 RX_RES_STATUS_STATION_FOUND)
911 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
912 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
914 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
916 /* packet was not encrypted */
917 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
918 RX_RES_STATUS_SEC_TYPE_NONE)
919 return decrypt_out;
921 /* packet was encrypted with unknown alg */
922 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
923 RX_RES_STATUS_SEC_TYPE_ERR)
924 return decrypt_out;
926 /* decryption was not done in HW */
927 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
928 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
929 return decrypt_out;
931 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
933 case RX_RES_STATUS_SEC_TYPE_CCMP:
934 /* alg is CCM: check MIC only */
935 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
936 /* Bad MIC */
937 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
938 else
939 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
941 break;
943 case RX_RES_STATUS_SEC_TYPE_TKIP:
944 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
945 /* Bad TTAK */
946 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
947 break;
949 /* fall through if TTAK OK */
950 default:
951 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
952 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
953 else
954 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
955 break;
958 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
959 decrypt_in, decrypt_out);
961 return decrypt_out;
964 /* Called for REPLY_RX (legacy ABG frames), or
965 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
966 static void iwl_rx_reply_rx(struct iwl_priv *priv,
967 struct iwl_rx_mem_buffer *rxb)
969 struct ieee80211_hdr *header;
970 struct ieee80211_rx_status rx_status;
971 struct iwl_rx_packet *pkt = rxb_addr(rxb);
972 struct iwl_rx_phy_res *phy_res;
973 __le32 rx_pkt_status;
974 struct iwl_rx_mpdu_res_start *amsdu;
975 u32 len;
976 u32 ampdu_status;
977 u32 rate_n_flags;
980 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
981 * REPLY_RX: physical layer info is in this buffer
982 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
983 * command and cached in priv->last_phy_res
985 * Here we set up local variables depending on which command is
986 * received.
988 if (pkt->hdr.cmd == REPLY_RX) {
989 phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
990 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
991 + phy_res->cfg_phy_cnt);
993 len = le16_to_cpu(phy_res->byte_count);
994 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
995 phy_res->cfg_phy_cnt + len);
996 ampdu_status = le32_to_cpu(rx_pkt_status);
997 } else {
998 if (!priv->_agn.last_phy_res_valid) {
999 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
1000 return;
1002 phy_res = &priv->_agn.last_phy_res;
1003 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
1004 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
1005 len = le16_to_cpu(amsdu->byte_count);
1006 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
1007 ampdu_status = iwl_translate_rx_status(priv,
1008 le32_to_cpu(rx_pkt_status));
1011 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
1012 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
1013 phy_res->cfg_phy_cnt);
1014 return;
1017 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
1018 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
1019 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
1020 le32_to_cpu(rx_pkt_status));
1021 return;
1024 /* This will be used in several places later */
1025 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
1027 /* rx_status carries information about the packet to mac80211 */
1028 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
1029 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
1030 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
1031 rx_status.freq =
1032 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
1033 rx_status.band);
1034 rx_status.rate_idx =
1035 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
1036 rx_status.flag = 0;
1038 /* TSF isn't reliable. In order to allow smooth user experience,
1039 * this W/A doesn't propagate it to the mac80211 */
1040 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
1042 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
1044 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
1045 rx_status.signal = priv->cfg->ops->utils->calc_rssi(priv, phy_res);
1047 iwl_dbg_log_rx_data_frame(priv, len, header);
1048 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
1049 rx_status.signal, (unsigned long long)rx_status.mactime);
1052 * "antenna number"
1054 * It seems that the antenna field in the phy flags value
1055 * is actually a bit field. This is undefined by radiotap,
1056 * it wants an actual antenna number but I always get "7"
1057 * for most legacy frames I receive indicating that the
1058 * same frame was received on all three RX chains.
1060 * I think this field should be removed in favor of a
1061 * new 802.11n radiotap field "RX chains" that is defined
1062 * as a bitmask.
1064 rx_status.antenna =
1065 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
1066 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
1068 /* set the preamble flag if appropriate */
1069 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
1070 rx_status.flag |= RX_FLAG_SHORTPRE;
1072 /* Set up the HT phy flags */
1073 if (rate_n_flags & RATE_MCS_HT_MSK)
1074 rx_status.flag |= RX_FLAG_HT;
1075 if (rate_n_flags & RATE_MCS_HT40_MSK)
1076 rx_status.flag |= RX_FLAG_40MHZ;
1077 if (rate_n_flags & RATE_MCS_SGI_MSK)
1078 rx_status.flag |= RX_FLAG_SHORT_GI;
1080 iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1081 rxb, &rx_status);
1085 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
1087 * Setup the RX handlers for each of the reply types sent from the uCode
1088 * to the host.
1090 void iwl_setup_rx_handlers(struct iwl_priv *priv)
1092 void (**handlers)(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb);
1094 handlers = priv->rx_handlers;
1096 handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
1097 handlers[REPLY_ERROR] = iwl_rx_reply_error;
1098 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
1099 handlers[SPECTRUM_MEASURE_NOTIFICATION] = iwl_rx_spectrum_measure_notif;
1100 handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
1101 handlers[PM_DEBUG_STATISTIC_NOTIFIC] = iwl_rx_pm_debug_statistics_notif;
1102 handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
1105 * The same handler is used for both the REPLY to a discrete
1106 * statistics request from the host as well as for the periodic
1107 * statistics notifications (after received beacons) from the uCode.
1109 handlers[REPLY_STATISTICS_CMD] = iwl_rx_reply_statistics;
1110 handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
1112 iwl_setup_rx_scan_handlers(priv);
1114 handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
1115 handlers[MISSED_BEACONS_NOTIFICATION] = iwl_rx_missed_beacon_notif;
1117 /* Rx handlers */
1118 handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
1119 handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
1121 /* block ack */
1122 handlers[REPLY_COMPRESSED_BA] = iwlagn_rx_reply_compressed_ba;
1124 /* Set up hardware specific Rx handlers */
1125 priv->cfg->ops->lib->rx_handler_setup(priv);