Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6.git] / drivers / net / wireless / iwlwifi / iwl-rx.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are derived from the ipw3945 project, as well
6  * as portions of the ieee80211 subsystem header files.
7  *
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.
11  *
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.
16  *
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
20  *
21  * The full GNU General Public License is included in this distribution in the
22  * file called LICENSE.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29
30 #include <linux/etherdevice.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
33 #include <net/mac80211.h>
34 #include <asm/unaligned.h>
35 #include "iwl-eeprom.h"
36 #include "iwl-dev.h"
37 #include "iwl-core.h"
38 #include "iwl-sta.h"
39 #include "iwl-io.h"
40 #include "iwl-helpers.h"
41 #include "iwl-agn-calib.h"
42 #include "iwl-agn.h"
43
44 /******************************************************************************
45  *
46  * RX path functions
47  *
48  ******************************************************************************/
49
50 /*
51  * Rx theory of operation
52  *
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.
58  *
59  * Rx Queue Indexes
60  * The host/firmware share two index registers for managing the Rx buffers.
61  *
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.
66  *
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.
69  *
70  * The queue is empty (no good data) if WRITE = READ - 1, and is full if
71  * WRITE = READ.
72  *
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)
75  *
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.
80  *
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.
94  *
95  *
96  * Driver sequence:
97  *
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
105  *
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  * ...
113  *
114  */
115
116 /**
117  * iwl_rx_queue_space - Return number of free slots available in queue.
118  */
119 int iwl_rx_queue_space(const struct iwl_rx_queue *q)
120 {
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;
129 }
130
131 /**
132  * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
133  */
134 void iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
135 {
136         unsigned long flags;
137         u32 reg;
138
139         spin_lock_irqsave(&q->lock, flags);
140
141         if (q->need_update == 0)
142                 goto exit_unlock;
143
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, FH_RSCSR_CHNL0_WPTR, 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);
153
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;
161                         }
162
163                         q->write_actual = (q->write & ~0x7);
164                         iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
165                                         q->write_actual);
166
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, FH_RSCSR_CHNL0_WPTR,
172                                 q->write_actual);
173                 }
174         }
175         q->need_update = 0;
176
177  exit_unlock:
178         spin_unlock_irqrestore(&q->lock, flags);
179 }
180
181 /******************************************************************************
182  *
183  * Generic RX handler implementations
184  *
185  ******************************************************************************/
186
187 static void iwl_rx_reply_error(struct iwl_priv *priv,
188                                struct iwl_rx_mem_buffer *rxb)
189 {
190         struct iwl_rx_packet *pkt = rxb_addr(rxb);
191
192         IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
193                 "seq 0x%04X ser 0x%08X\n",
194                 le32_to_cpu(pkt->u.err_resp.error_type),
195                 get_cmd_string(pkt->u.err_resp.cmd_id),
196                 pkt->u.err_resp.cmd_id,
197                 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
198                 le32_to_cpu(pkt->u.err_resp.error_info));
199 }
200
201 static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
202 {
203         struct iwl_rx_packet *pkt = rxb_addr(rxb);
204         struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
205         /*
206          * MULTI-FIXME
207          * See iwl_mac_channel_switch.
208          */
209         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
210         struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
211
212         if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
213                 return;
214
215         if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
216                 rxon->channel = csa->channel;
217                 ctx->staging.channel = csa->channel;
218                 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
219                               le16_to_cpu(csa->channel));
220                 iwl_chswitch_done(priv, true);
221         } else {
222                 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
223                         le16_to_cpu(csa->channel));
224                 iwl_chswitch_done(priv, false);
225         }
226 }
227
228
229 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
230                                           struct iwl_rx_mem_buffer *rxb)
231 {
232         struct iwl_rx_packet *pkt = rxb_addr(rxb);
233         struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
234
235         if (!report->state) {
236                 IWL_DEBUG_11H(priv,
237                         "Spectrum Measure Notification: Start\n");
238                 return;
239         }
240
241         memcpy(&priv->measure_report, report, sizeof(*report));
242         priv->measurement_status |= MEASUREMENT_READY;
243 }
244
245 static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
246                                   struct iwl_rx_mem_buffer *rxb)
247 {
248 #ifdef CONFIG_IWLWIFI_DEBUG
249         struct iwl_rx_packet *pkt = rxb_addr(rxb);
250         struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
251         IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
252                      sleep->pm_sleep_mode, sleep->pm_wakeup_src);
253 #endif
254 }
255
256 static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
257                                              struct iwl_rx_mem_buffer *rxb)
258 {
259         struct iwl_rx_packet *pkt = rxb_addr(rxb);
260         u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
261         IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
262                         "notification for %s:\n", len,
263                         get_cmd_string(pkt->hdr.cmd));
264         iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
265 }
266
267 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
268                                 struct iwl_rx_mem_buffer *rxb)
269 {
270         struct iwl_rx_packet *pkt = rxb_addr(rxb);
271         struct iwlagn_beacon_notif *beacon = (void *)pkt->u.raw;
272 #ifdef CONFIG_IWLWIFI_DEBUG
273         u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
274         u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
275
276         IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
277                 "tsf:0x%.8x%.8x rate:%d\n",
278                 status & TX_STATUS_MSK,
279                 beacon->beacon_notify_hdr.failure_frame,
280                 le32_to_cpu(beacon->ibss_mgr_status),
281                 le32_to_cpu(beacon->high_tsf),
282                 le32_to_cpu(beacon->low_tsf), rate);
283 #endif
284
285         priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
286
287         if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
288                 queue_work(priv->workqueue, &priv->beacon_update);
289 }
290
291 /* the threshold ratio of actual_ack_cnt to expected_ack_cnt in percent */
292 #define ACK_CNT_RATIO (50)
293 #define BA_TIMEOUT_CNT (5)
294 #define BA_TIMEOUT_MAX (16)
295
296 /**
297  * iwl_good_ack_health - checks for ACK count ratios, BA timeout retries.
298  *
299  * When the ACK count ratio is low and aggregated BA timeout retries exceeding
300  * the BA_TIMEOUT_MAX, reload firmware and bring system back to normal
301  * operation state.
302  */
303 static bool iwl_good_ack_health(struct iwl_priv *priv,
304                                 struct statistics_tx *cur)
305 {
306         int actual_delta, expected_delta, ba_timeout_delta;
307         struct statistics_tx *old;
308
309         if (priv->_agn.agg_tids_count)
310                 return true;
311
312         old = &priv->statistics.tx;
313
314         actual_delta = le32_to_cpu(cur->actual_ack_cnt) -
315                        le32_to_cpu(old->actual_ack_cnt);
316         expected_delta = le32_to_cpu(cur->expected_ack_cnt) -
317                          le32_to_cpu(old->expected_ack_cnt);
318
319         /* Values should not be negative, but we do not trust the firmware */
320         if (actual_delta <= 0 || expected_delta <= 0)
321                 return true;
322
323         ba_timeout_delta = le32_to_cpu(cur->agg.ba_timeout) -
324                            le32_to_cpu(old->agg.ba_timeout);
325
326         if ((actual_delta * 100 / expected_delta) < ACK_CNT_RATIO &&
327             ba_timeout_delta > BA_TIMEOUT_CNT) {
328                 IWL_DEBUG_RADIO(priv, "deltas: actual %d expected %d ba_timeout %d\n",
329                                 actual_delta, expected_delta, ba_timeout_delta);
330
331 #ifdef CONFIG_IWLWIFI_DEBUGFS
332                 /*
333                  * This is ifdef'ed on DEBUGFS because otherwise the
334                  * statistics aren't available. If DEBUGFS is set but
335                  * DEBUG is not, these will just compile out.
336                  */
337                 IWL_DEBUG_RADIO(priv, "rx_detected_cnt delta %d\n",
338                                 priv->delta_stats.tx.rx_detected_cnt);
339                 IWL_DEBUG_RADIO(priv,
340                                 "ack_or_ba_timeout_collision delta %d\n",
341                                 priv->delta_stats.tx.ack_or_ba_timeout_collision);
342 #endif
343
344                 if (ba_timeout_delta >= BA_TIMEOUT_MAX)
345                         return false;
346         }
347
348         return true;
349 }
350
351 /**
352  * iwl_good_plcp_health - checks for plcp error.
353  *
354  * When the plcp error is exceeding the thresholds, reset the radio
355  * to improve the throughput.
356  */
357 static bool iwl_good_plcp_health(struct iwl_priv *priv,
358                                  struct statistics_rx_phy *cur_ofdm,
359                                  struct statistics_rx_ht_phy *cur_ofdm_ht,
360                                  unsigned int msecs)
361 {
362         int delta;
363         int threshold = priv->cfg->base_params->plcp_delta_threshold;
364
365         if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
366                 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
367                 return true;
368         }
369
370         delta = le32_to_cpu(cur_ofdm->plcp_err) -
371                 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
372                 le32_to_cpu(cur_ofdm_ht->plcp_err) -
373                 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
374
375         /* Can be negative if firmware reset statistics */
376         if (delta <= 0)
377                 return true;
378
379         if ((delta * 100 / msecs) > threshold) {
380                 IWL_DEBUG_RADIO(priv,
381                                 "plcp health threshold %u delta %d msecs %u\n",
382                                 threshold, delta, msecs);
383                 return false;
384         }
385
386         return true;
387 }
388
389 static void iwl_recover_from_statistics(struct iwl_priv *priv,
390                                         struct statistics_rx_phy *cur_ofdm,
391                                         struct statistics_rx_ht_phy *cur_ofdm_ht,
392                                         struct statistics_tx *tx,
393                                         unsigned long stamp)
394 {
395         unsigned int msecs;
396
397         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
398                 return;
399
400         msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
401
402         /* Only gather statistics and update time stamp when not associated */
403         if (!iwl_is_any_associated(priv))
404                 return;
405
406         /* Do not check/recover when do not have enough statistics data */
407         if (msecs < 99)
408                 return;
409
410         if (iwlagn_mod_params.ack_check && !iwl_good_ack_health(priv, tx)) {
411                 IWL_ERR(priv, "low ack count detected, restart firmware\n");
412                 if (!iwl_force_reset(priv, IWL_FW_RESET, false))
413                         return;
414         }
415
416         if (iwlagn_mod_params.plcp_check &&
417             !iwl_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
418                 iwl_force_reset(priv, IWL_RF_RESET, false);
419 }
420
421 /* Calculate noise level, based on measurements during network silence just
422  *   before arriving beacon.  This measurement can be done only if we know
423  *   exactly when to expect beacons, therefore only when we're associated. */
424 static void iwl_rx_calc_noise(struct iwl_priv *priv)
425 {
426         struct statistics_rx_non_phy *rx_info;
427         int num_active_rx = 0;
428         int total_silence = 0;
429         int bcn_silence_a, bcn_silence_b, bcn_silence_c;
430         int last_rx_noise;
431
432         rx_info = &priv->statistics.rx_non_phy;
433
434         bcn_silence_a =
435                 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
436         bcn_silence_b =
437                 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
438         bcn_silence_c =
439                 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
440
441         if (bcn_silence_a) {
442                 total_silence += bcn_silence_a;
443                 num_active_rx++;
444         }
445         if (bcn_silence_b) {
446                 total_silence += bcn_silence_b;
447                 num_active_rx++;
448         }
449         if (bcn_silence_c) {
450                 total_silence += bcn_silence_c;
451                 num_active_rx++;
452         }
453
454         /* Average among active antennas */
455         if (num_active_rx)
456                 last_rx_noise = (total_silence / num_active_rx) - 107;
457         else
458                 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
459
460         IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
461                         bcn_silence_a, bcn_silence_b, bcn_silence_c,
462                         last_rx_noise);
463 }
464
465 #ifdef CONFIG_IWLWIFI_DEBUGFS
466 /*
467  *  based on the assumption of all statistics counter are in DWORD
468  *  FIXME: This function is for debugging, do not deal with
469  *  the case of counters roll-over.
470  */
471 static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
472                         __le32 *max_delta, __le32 *accum, int size)
473 {
474         int i;
475
476         for (i = 0;
477              i < size / sizeof(__le32);
478              i++, prev++, cur++, delta++, max_delta++, accum++) {
479                 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
480                         *delta = cpu_to_le32(
481                                 le32_to_cpu(*cur) - le32_to_cpu(*prev));
482                         le32_add_cpu(accum, le32_to_cpu(*delta));
483                         if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
484                                 *max_delta = *delta;
485                 }
486         }
487 }
488
489 static void
490 iwl_accumulative_statistics(struct iwl_priv *priv,
491                             struct statistics_general_common *common,
492                             struct statistics_rx_non_phy *rx_non_phy,
493                             struct statistics_rx_phy *rx_ofdm,
494                             struct statistics_rx_ht_phy *rx_ofdm_ht,
495                             struct statistics_rx_phy *rx_cck,
496                             struct statistics_tx *tx,
497                             struct statistics_bt_activity *bt_activity)
498 {
499 #define ACCUM(_name)    \
500         accum_stats((__le32 *)&priv->statistics._name,          \
501                     (__le32 *)_name,                            \
502                     (__le32 *)&priv->delta_stats._name,         \
503                     (__le32 *)&priv->max_delta_stats._name,     \
504                     (__le32 *)&priv->accum_stats._name,         \
505                     sizeof(*_name));
506
507         ACCUM(common);
508         ACCUM(rx_non_phy);
509         ACCUM(rx_ofdm);
510         ACCUM(rx_ofdm_ht);
511         ACCUM(rx_cck);
512         ACCUM(tx);
513         if (bt_activity)
514                 ACCUM(bt_activity);
515 #undef ACCUM
516 }
517 #else
518 static inline void
519 iwl_accumulative_statistics(struct iwl_priv *priv,
520                             struct statistics_general_common *common,
521                             struct statistics_rx_non_phy *rx_non_phy,
522                             struct statistics_rx_phy *rx_ofdm,
523                             struct statistics_rx_ht_phy *rx_ofdm_ht,
524                             struct statistics_rx_phy *rx_cck,
525                             struct statistics_tx *tx,
526                             struct statistics_bt_activity *bt_activity)
527 {
528 }
529 #endif
530
531 static void iwl_rx_statistics(struct iwl_priv *priv,
532                               struct iwl_rx_mem_buffer *rxb)
533 {
534         unsigned long stamp = jiffies;
535         const int reg_recalib_period = 60;
536         int change;
537         struct iwl_rx_packet *pkt = rxb_addr(rxb);
538         u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
539         __le32 *flag;
540         struct statistics_general_common *common;
541         struct statistics_rx_non_phy *rx_non_phy;
542         struct statistics_rx_phy *rx_ofdm;
543         struct statistics_rx_ht_phy *rx_ofdm_ht;
544         struct statistics_rx_phy *rx_cck;
545         struct statistics_tx *tx;
546         struct statistics_bt_activity *bt_activity;
547
548         len -= sizeof(struct iwl_cmd_header); /* skip header */
549
550         IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
551                      len);
552
553         if (len == sizeof(struct iwl_bt_notif_statistics)) {
554                 struct iwl_bt_notif_statistics *stats;
555                 stats = &pkt->u.stats_bt;
556                 flag = &stats->flag;
557                 common = &stats->general.common;
558                 rx_non_phy = &stats->rx.general.common;
559                 rx_ofdm = &stats->rx.ofdm;
560                 rx_ofdm_ht = &stats->rx.ofdm_ht;
561                 rx_cck = &stats->rx.cck;
562                 tx = &stats->tx;
563                 bt_activity = &stats->general.activity;
564
565 #ifdef CONFIG_IWLWIFI_DEBUGFS
566                 /* handle this exception directly */
567                 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
568                 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
569                              le32_to_cpu(stats->rx.general.num_bt_kills));
570 #endif
571         } else if (len == sizeof(struct iwl_notif_statistics)) {
572                 struct iwl_notif_statistics *stats;
573                 stats = &pkt->u.stats;
574                 flag = &stats->flag;
575                 common = &stats->general.common;
576                 rx_non_phy = &stats->rx.general;
577                 rx_ofdm = &stats->rx.ofdm;
578                 rx_ofdm_ht = &stats->rx.ofdm_ht;
579                 rx_cck = &stats->rx.cck;
580                 tx = &stats->tx;
581                 bt_activity = NULL;
582         } else {
583                 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
584                           len, sizeof(struct iwl_bt_notif_statistics),
585                           sizeof(struct iwl_notif_statistics));
586                 return;
587         }
588
589         change = common->temperature != priv->statistics.common.temperature ||
590                  (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
591                  (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
592
593         iwl_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
594                                     rx_ofdm_ht, rx_cck, tx, bt_activity);
595
596         iwl_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
597
598         priv->statistics.flag = *flag;
599         memcpy(&priv->statistics.common, common, sizeof(*common));
600         memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
601         memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
602         memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
603         memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
604         memcpy(&priv->statistics.tx, tx, sizeof(*tx));
605 #ifdef CONFIG_IWLWIFI_DEBUGFS
606         if (bt_activity)
607                 memcpy(&priv->statistics.bt_activity, bt_activity,
608                         sizeof(*bt_activity));
609 #endif
610
611         priv->rx_statistics_jiffies = stamp;
612
613         set_bit(STATUS_STATISTICS, &priv->status);
614
615         /* Reschedule the statistics timer to occur in
616          * reg_recalib_period seconds to ensure we get a
617          * thermal update even if the uCode doesn't give
618          * us one */
619         mod_timer(&priv->statistics_periodic, jiffies +
620                   msecs_to_jiffies(reg_recalib_period * 1000));
621
622         if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
623             (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
624                 iwl_rx_calc_noise(priv);
625                 queue_work(priv->workqueue, &priv->run_time_calib_work);
626         }
627         if (priv->cfg->ops->lib->temp_ops.temperature && change)
628                 priv->cfg->ops->lib->temp_ops.temperature(priv);
629 }
630
631 static void iwl_rx_reply_statistics(struct iwl_priv *priv,
632                                     struct iwl_rx_mem_buffer *rxb)
633 {
634         struct iwl_rx_packet *pkt = rxb_addr(rxb);
635
636         if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
637 #ifdef CONFIG_IWLWIFI_DEBUGFS
638                 memset(&priv->accum_stats, 0,
639                         sizeof(priv->accum_stats));
640                 memset(&priv->delta_stats, 0,
641                         sizeof(priv->delta_stats));
642                 memset(&priv->max_delta_stats, 0,
643                         sizeof(priv->max_delta_stats));
644 #endif
645                 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
646         }
647         iwl_rx_statistics(priv, rxb);
648 }
649
650 /* Handle notification from uCode that card's power state is changing
651  * due to software, hardware, or critical temperature RFKILL */
652 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
653                                     struct iwl_rx_mem_buffer *rxb)
654 {
655         struct iwl_rx_packet *pkt = rxb_addr(rxb);
656         u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
657         unsigned long status = priv->status;
658
659         IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
660                           (flags & HW_CARD_DISABLED) ? "Kill" : "On",
661                           (flags & SW_CARD_DISABLED) ? "Kill" : "On",
662                           (flags & CT_CARD_DISABLED) ?
663                           "Reached" : "Not reached");
664
665         if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
666                      CT_CARD_DISABLED)) {
667
668                 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
669                             CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
670
671                 iwl_write_direct32(priv, HBUS_TARG_MBX_C,
672                                         HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
673
674                 if (!(flags & RXON_CARD_DISABLED)) {
675                         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
676                                     CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
677                         iwl_write_direct32(priv, HBUS_TARG_MBX_C,
678                                         HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
679                 }
680                 if (flags & CT_CARD_DISABLED)
681                         iwl_tt_enter_ct_kill(priv);
682         }
683         if (!(flags & CT_CARD_DISABLED))
684                 iwl_tt_exit_ct_kill(priv);
685
686         if (flags & HW_CARD_DISABLED)
687                 set_bit(STATUS_RF_KILL_HW, &priv->status);
688         else
689                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
690
691
692         if (!(flags & RXON_CARD_DISABLED))
693                 iwl_scan_cancel(priv);
694
695         if ((test_bit(STATUS_RF_KILL_HW, &status) !=
696              test_bit(STATUS_RF_KILL_HW, &priv->status)))
697                 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
698                         test_bit(STATUS_RF_KILL_HW, &priv->status));
699         else
700                 wake_up_interruptible(&priv->wait_command_queue);
701 }
702
703 static void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
704                                        struct iwl_rx_mem_buffer *rxb)
705
706 {
707         struct iwl_rx_packet *pkt = rxb_addr(rxb);
708         struct iwl_missed_beacon_notif *missed_beacon;
709
710         missed_beacon = &pkt->u.missed_beacon;
711         if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
712             priv->missed_beacon_threshold) {
713                 IWL_DEBUG_CALIB(priv,
714                     "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
715                     le32_to_cpu(missed_beacon->consecutive_missed_beacons),
716                     le32_to_cpu(missed_beacon->total_missed_becons),
717                     le32_to_cpu(missed_beacon->num_recvd_beacons),
718                     le32_to_cpu(missed_beacon->num_expected_beacons));
719                 if (!test_bit(STATUS_SCANNING, &priv->status))
720                         iwl_init_sensitivity(priv);
721         }
722 }
723
724 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
725  * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
726 static void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
727                                 struct iwl_rx_mem_buffer *rxb)
728 {
729         struct iwl_rx_packet *pkt = rxb_addr(rxb);
730
731         priv->_agn.last_phy_res_valid = true;
732         memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
733                sizeof(struct iwl_rx_phy_res));
734 }
735
736 /*
737  * returns non-zero if packet should be dropped
738  */
739 static int iwl_set_decrypted_flag(struct iwl_priv *priv,
740                                   struct ieee80211_hdr *hdr,
741                                   u32 decrypt_res,
742                                   struct ieee80211_rx_status *stats)
743 {
744         u16 fc = le16_to_cpu(hdr->frame_control);
745
746         /*
747          * All contexts have the same setting here due to it being
748          * a module parameter, so OK to check any context.
749          */
750         if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
751                                                 RXON_FILTER_DIS_DECRYPT_MSK)
752                 return 0;
753
754         if (!(fc & IEEE80211_FCTL_PROTECTED))
755                 return 0;
756
757         IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
758         switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
759         case RX_RES_STATUS_SEC_TYPE_TKIP:
760                 /* The uCode has got a bad phase 1 Key, pushes the packet.
761                  * Decryption will be done in SW. */
762                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
763                     RX_RES_STATUS_BAD_KEY_TTAK)
764                         break;
765
766         case RX_RES_STATUS_SEC_TYPE_WEP:
767                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
768                     RX_RES_STATUS_BAD_ICV_MIC) {
769                         /* bad ICV, the packet is destroyed since the
770                          * decryption is inplace, drop it */
771                         IWL_DEBUG_RX(priv, "Packet destroyed\n");
772                         return -1;
773                 }
774         case RX_RES_STATUS_SEC_TYPE_CCMP:
775                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
776                     RX_RES_STATUS_DECRYPT_OK) {
777                         IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
778                         stats->flag |= RX_FLAG_DECRYPTED;
779                 }
780                 break;
781
782         default:
783                 break;
784         }
785         return 0;
786 }
787
788 static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
789                                         struct ieee80211_hdr *hdr,
790                                         u16 len,
791                                         u32 ampdu_status,
792                                         struct iwl_rx_mem_buffer *rxb,
793                                         struct ieee80211_rx_status *stats)
794 {
795         struct sk_buff *skb;
796         __le16 fc = hdr->frame_control;
797         struct iwl_rxon_context *ctx;
798
799         /* We only process data packets if the interface is open */
800         if (unlikely(!priv->is_open)) {
801                 IWL_DEBUG_DROP_LIMIT(priv,
802                     "Dropping packet while interface is not open.\n");
803                 return;
804         }
805
806         /* In case of HW accelerated crypto and bad decryption, drop */
807         if (!iwlagn_mod_params.sw_crypto &&
808             iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
809                 return;
810
811         skb = dev_alloc_skb(128);
812         if (!skb) {
813                 IWL_ERR(priv, "dev_alloc_skb failed\n");
814                 return;
815         }
816
817         skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
818
819         iwl_update_stats(priv, false, fc, len);
820
821         /*
822         * Wake any queues that were stopped due to a passive channel tx
823         * failure. This can happen because the regulatory enforcement in
824         * the device waits for a beacon before allowing transmission,
825         * sometimes even after already having transmitted frames for the
826         * association because the new RXON may reset the information.
827         */
828         if (unlikely(ieee80211_is_beacon(fc))) {
829                 for_each_context(priv, ctx) {
830                         if (!ctx->last_tx_rejected)
831                                 continue;
832                         if (compare_ether_addr(hdr->addr3,
833                                                ctx->active.bssid_addr))
834                                 continue;
835                         ctx->last_tx_rejected = false;
836                         iwl_wake_any_queue(priv, ctx);
837                 }
838         }
839
840         memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
841
842         ieee80211_rx(priv->hw, skb);
843         rxb->page = NULL;
844 }
845
846 static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
847 {
848         u32 decrypt_out = 0;
849
850         if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
851                                         RX_RES_STATUS_STATION_FOUND)
852                 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
853                                 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
854
855         decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
856
857         /* packet was not encrypted */
858         if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
859                                         RX_RES_STATUS_SEC_TYPE_NONE)
860                 return decrypt_out;
861
862         /* packet was encrypted with unknown alg */
863         if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
864                                         RX_RES_STATUS_SEC_TYPE_ERR)
865                 return decrypt_out;
866
867         /* decryption was not done in HW */
868         if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
869                                         RX_MPDU_RES_STATUS_DEC_DONE_MSK)
870                 return decrypt_out;
871
872         switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
873
874         case RX_RES_STATUS_SEC_TYPE_CCMP:
875                 /* alg is CCM: check MIC only */
876                 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
877                         /* Bad MIC */
878                         decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
879                 else
880                         decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
881
882                 break;
883
884         case RX_RES_STATUS_SEC_TYPE_TKIP:
885                 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
886                         /* Bad TTAK */
887                         decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
888                         break;
889                 }
890                 /* fall through if TTAK OK */
891         default:
892                 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
893                         decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
894                 else
895                         decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
896                 break;
897         }
898
899         IWL_DEBUG_RX(priv, "decrypt_in:0x%x  decrypt_out = 0x%x\n",
900                                         decrypt_in, decrypt_out);
901
902         return decrypt_out;
903 }
904
905 /* Called for REPLY_RX (legacy ABG frames), or
906  * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
907 static void iwl_rx_reply_rx(struct iwl_priv *priv,
908                             struct iwl_rx_mem_buffer *rxb)
909 {
910         struct ieee80211_hdr *header;
911         struct ieee80211_rx_status rx_status;
912         struct iwl_rx_packet *pkt = rxb_addr(rxb);
913         struct iwl_rx_phy_res *phy_res;
914         __le32 rx_pkt_status;
915         struct iwl_rx_mpdu_res_start *amsdu;
916         u32 len;
917         u32 ampdu_status;
918         u32 rate_n_flags;
919
920         /**
921          * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
922          *      REPLY_RX: physical layer info is in this buffer
923          *      REPLY_RX_MPDU_CMD: physical layer info was sent in separate
924          *              command and cached in priv->last_phy_res
925          *
926          * Here we set up local variables depending on which command is
927          * received.
928          */
929         if (pkt->hdr.cmd == REPLY_RX) {
930                 phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
931                 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
932                                 + phy_res->cfg_phy_cnt);
933
934                 len = le16_to_cpu(phy_res->byte_count);
935                 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
936                                 phy_res->cfg_phy_cnt + len);
937                 ampdu_status = le32_to_cpu(rx_pkt_status);
938         } else {
939                 if (!priv->_agn.last_phy_res_valid) {
940                         IWL_ERR(priv, "MPDU frame without cached PHY data\n");
941                         return;
942                 }
943                 phy_res = &priv->_agn.last_phy_res;
944                 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
945                 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
946                 len = le16_to_cpu(amsdu->byte_count);
947                 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
948                 ampdu_status = iwl_translate_rx_status(priv,
949                                                 le32_to_cpu(rx_pkt_status));
950         }
951
952         if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
953                 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
954                                 phy_res->cfg_phy_cnt);
955                 return;
956         }
957
958         if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
959             !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
960                 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
961                                 le32_to_cpu(rx_pkt_status));
962                 return;
963         }
964
965         /* This will be used in several places later */
966         rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
967
968         /* rx_status carries information about the packet to mac80211 */
969         rx_status.mactime = le64_to_cpu(phy_res->timestamp);
970         rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
971                                 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
972         rx_status.freq =
973                 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
974                                                rx_status.band);
975         rx_status.rate_idx =
976                 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
977         rx_status.flag = 0;
978
979         /* TSF isn't reliable. In order to allow smooth user experience,
980          * this W/A doesn't propagate it to the mac80211 */
981         /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
982
983         priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
984
985         /* Find max signal strength (dBm) among 3 antenna/receiver chains */
986         rx_status.signal = priv->cfg->ops->utils->calc_rssi(priv, phy_res);
987
988         iwl_dbg_log_rx_data_frame(priv, len, header);
989         IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
990                 rx_status.signal, (unsigned long long)rx_status.mactime);
991
992         /*
993          * "antenna number"
994          *
995          * It seems that the antenna field in the phy flags value
996          * is actually a bit field. This is undefined by radiotap,
997          * it wants an actual antenna number but I always get "7"
998          * for most legacy frames I receive indicating that the
999          * same frame was received on all three RX chains.
1000          *
1001          * I think this field should be removed in favor of a
1002          * new 802.11n radiotap field "RX chains" that is defined
1003          * as a bitmask.
1004          */
1005         rx_status.antenna =
1006                 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
1007                 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
1008
1009         /* set the preamble flag if appropriate */
1010         if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
1011                 rx_status.flag |= RX_FLAG_SHORTPRE;
1012
1013         /* Set up the HT phy flags */
1014         if (rate_n_flags & RATE_MCS_HT_MSK)
1015                 rx_status.flag |= RX_FLAG_HT;
1016         if (rate_n_flags & RATE_MCS_HT40_MSK)
1017                 rx_status.flag |= RX_FLAG_40MHZ;
1018         if (rate_n_flags & RATE_MCS_SGI_MSK)
1019                 rx_status.flag |= RX_FLAG_SHORT_GI;
1020
1021         iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1022                                     rxb, &rx_status);
1023 }
1024
1025 /**
1026  * iwl_setup_rx_handlers - Initialize Rx handler callbacks
1027  *
1028  * Setup the RX handlers for each of the reply types sent from the uCode
1029  * to the host.
1030  */
1031 void iwl_setup_rx_handlers(struct iwl_priv *priv)
1032 {
1033         void (**handlers)(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb);
1034
1035         handlers = priv->rx_handlers;
1036
1037         handlers[REPLY_ERROR]                   = iwl_rx_reply_error;
1038         handlers[CHANNEL_SWITCH_NOTIFICATION]   = iwl_rx_csa;
1039         handlers[SPECTRUM_MEASURE_NOTIFICATION] = iwl_rx_spectrum_measure_notif;
1040         handlers[PM_SLEEP_NOTIFICATION]         = iwl_rx_pm_sleep_notif;
1041         handlers[PM_DEBUG_STATISTIC_NOTIFIC]    = iwl_rx_pm_debug_statistics_notif;
1042         handlers[BEACON_NOTIFICATION]           = iwl_rx_beacon_notif;
1043
1044         /*
1045          * The same handler is used for both the REPLY to a discrete
1046          * statistics request from the host as well as for the periodic
1047          * statistics notifications (after received beacons) from the uCode.
1048          */
1049         handlers[REPLY_STATISTICS_CMD]          = iwl_rx_reply_statistics;
1050         handlers[STATISTICS_NOTIFICATION]       = iwl_rx_statistics;
1051
1052         iwl_setup_rx_scan_handlers(priv);
1053
1054         handlers[CARD_STATE_NOTIFICATION]       = iwl_rx_card_state_notif;
1055         handlers[MISSED_BEACONS_NOTIFICATION]   = iwl_rx_missed_beacon_notif;
1056
1057         /* Rx handlers */
1058         handlers[REPLY_RX_PHY_CMD]              = iwl_rx_reply_rx_phy;
1059         handlers[REPLY_RX_MPDU_CMD]             = iwl_rx_reply_rx;
1060
1061         /* block ack */
1062         handlers[REPLY_COMPRESSED_BA]           = iwlagn_rx_reply_compressed_ba;
1063
1064         /* Set up hardware specific Rx handlers */
1065         priv->cfg->ops->lib->rx_handler_setup(priv);
1066 }