iwlagn: clean up of transport layer
[pandora-kernel.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 #include "iwl-shared.h"
44
45 const char *get_cmd_string(u8 cmd)
46 {
47         switch (cmd) {
48                 IWL_CMD(REPLY_ALIVE);
49                 IWL_CMD(REPLY_ERROR);
50                 IWL_CMD(REPLY_RXON);
51                 IWL_CMD(REPLY_RXON_ASSOC);
52                 IWL_CMD(REPLY_QOS_PARAM);
53                 IWL_CMD(REPLY_RXON_TIMING);
54                 IWL_CMD(REPLY_ADD_STA);
55                 IWL_CMD(REPLY_REMOVE_STA);
56                 IWL_CMD(REPLY_REMOVE_ALL_STA);
57                 IWL_CMD(REPLY_TXFIFO_FLUSH);
58                 IWL_CMD(REPLY_WEPKEY);
59                 IWL_CMD(REPLY_TX);
60                 IWL_CMD(REPLY_LEDS_CMD);
61                 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
62                 IWL_CMD(COEX_PRIORITY_TABLE_CMD);
63                 IWL_CMD(COEX_MEDIUM_NOTIFICATION);
64                 IWL_CMD(COEX_EVENT_CMD);
65                 IWL_CMD(REPLY_QUIET_CMD);
66                 IWL_CMD(REPLY_CHANNEL_SWITCH);
67                 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
68                 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
69                 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
70                 IWL_CMD(POWER_TABLE_CMD);
71                 IWL_CMD(PM_SLEEP_NOTIFICATION);
72                 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
73                 IWL_CMD(REPLY_SCAN_CMD);
74                 IWL_CMD(REPLY_SCAN_ABORT_CMD);
75                 IWL_CMD(SCAN_START_NOTIFICATION);
76                 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
77                 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
78                 IWL_CMD(BEACON_NOTIFICATION);
79                 IWL_CMD(REPLY_TX_BEACON);
80                 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
81                 IWL_CMD(QUIET_NOTIFICATION);
82                 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
83                 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
84                 IWL_CMD(REPLY_BT_CONFIG);
85                 IWL_CMD(REPLY_STATISTICS_CMD);
86                 IWL_CMD(STATISTICS_NOTIFICATION);
87                 IWL_CMD(REPLY_CARD_STATE_CMD);
88                 IWL_CMD(CARD_STATE_NOTIFICATION);
89                 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
90                 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
91                 IWL_CMD(SENSITIVITY_CMD);
92                 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
93                 IWL_CMD(REPLY_RX_PHY_CMD);
94                 IWL_CMD(REPLY_RX_MPDU_CMD);
95                 IWL_CMD(REPLY_RX);
96                 IWL_CMD(REPLY_COMPRESSED_BA);
97                 IWL_CMD(CALIBRATION_CFG_CMD);
98                 IWL_CMD(CALIBRATION_RES_NOTIFICATION);
99                 IWL_CMD(CALIBRATION_COMPLETE_NOTIFICATION);
100                 IWL_CMD(REPLY_TX_POWER_DBM_CMD);
101                 IWL_CMD(TEMPERATURE_NOTIFICATION);
102                 IWL_CMD(TX_ANT_CONFIGURATION_CMD);
103                 IWL_CMD(REPLY_BT_COEX_PROFILE_NOTIF);
104                 IWL_CMD(REPLY_BT_COEX_PRIO_TABLE);
105                 IWL_CMD(REPLY_BT_COEX_PROT_ENV);
106                 IWL_CMD(REPLY_WIPAN_PARAMS);
107                 IWL_CMD(REPLY_WIPAN_RXON);
108                 IWL_CMD(REPLY_WIPAN_RXON_TIMING);
109                 IWL_CMD(REPLY_WIPAN_RXON_ASSOC);
110                 IWL_CMD(REPLY_WIPAN_QOS_PARAM);
111                 IWL_CMD(REPLY_WIPAN_WEPKEY);
112                 IWL_CMD(REPLY_WIPAN_P2P_CHANNEL_SWITCH);
113                 IWL_CMD(REPLY_WIPAN_NOA_NOTIFICATION);
114                 IWL_CMD(REPLY_WIPAN_DEACTIVATION_COMPLETE);
115                 IWL_CMD(REPLY_WOWLAN_PATTERNS);
116                 IWL_CMD(REPLY_WOWLAN_WAKEUP_FILTER);
117                 IWL_CMD(REPLY_WOWLAN_TSC_RSC_PARAMS);
118                 IWL_CMD(REPLY_WOWLAN_TKIP_PARAMS);
119                 IWL_CMD(REPLY_WOWLAN_KEK_KCK_MATERIAL);
120                 IWL_CMD(REPLY_WOWLAN_GET_STATUS);
121         default:
122                 return "UNKNOWN";
123
124         }
125 }
126
127 /******************************************************************************
128  *
129  * Generic RX handler implementations
130  *
131  ******************************************************************************/
132
133 static void iwl_rx_reply_error(struct iwl_priv *priv,
134                                struct iwl_rx_mem_buffer *rxb)
135 {
136         struct iwl_rx_packet *pkt = rxb_addr(rxb);
137
138         IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
139                 "seq 0x%04X ser 0x%08X\n",
140                 le32_to_cpu(pkt->u.err_resp.error_type),
141                 get_cmd_string(pkt->u.err_resp.cmd_id),
142                 pkt->u.err_resp.cmd_id,
143                 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
144                 le32_to_cpu(pkt->u.err_resp.error_info));
145 }
146
147 static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
148 {
149         struct iwl_rx_packet *pkt = rxb_addr(rxb);
150         struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
151         /*
152          * MULTI-FIXME
153          * See iwl_mac_channel_switch.
154          */
155         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
156         struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
157
158         if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
159                 return;
160
161         if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
162                 rxon->channel = csa->channel;
163                 ctx->staging.channel = csa->channel;
164                 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
165                               le16_to_cpu(csa->channel));
166                 iwl_chswitch_done(priv, true);
167         } else {
168                 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
169                         le16_to_cpu(csa->channel));
170                 iwl_chswitch_done(priv, false);
171         }
172 }
173
174
175 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
176                                           struct iwl_rx_mem_buffer *rxb)
177 {
178         struct iwl_rx_packet *pkt = rxb_addr(rxb);
179         struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
180
181         if (!report->state) {
182                 IWL_DEBUG_11H(priv,
183                         "Spectrum Measure Notification: Start\n");
184                 return;
185         }
186
187         memcpy(&priv->measure_report, report, sizeof(*report));
188         priv->measurement_status |= MEASUREMENT_READY;
189 }
190
191 static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
192                                   struct iwl_rx_mem_buffer *rxb)
193 {
194 #ifdef CONFIG_IWLWIFI_DEBUG
195         struct iwl_rx_packet *pkt = rxb_addr(rxb);
196         struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
197         IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
198                      sleep->pm_sleep_mode, sleep->pm_wakeup_src);
199 #endif
200 }
201
202 static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
203                                              struct iwl_rx_mem_buffer *rxb)
204 {
205         struct iwl_rx_packet *pkt = rxb_addr(rxb);
206         u32 __maybe_unused len =
207                 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
208         IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
209                         "notification for %s:\n", len,
210                         get_cmd_string(pkt->hdr.cmd));
211         iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
212 }
213
214 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
215                                 struct iwl_rx_mem_buffer *rxb)
216 {
217         struct iwl_rx_packet *pkt = rxb_addr(rxb);
218         struct iwlagn_beacon_notif *beacon = (void *)pkt->u.raw;
219 #ifdef CONFIG_IWLWIFI_DEBUG
220         u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
221         u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
222
223         IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
224                 "tsf:0x%.8x%.8x rate:%d\n",
225                 status & TX_STATUS_MSK,
226                 beacon->beacon_notify_hdr.failure_frame,
227                 le32_to_cpu(beacon->ibss_mgr_status),
228                 le32_to_cpu(beacon->high_tsf),
229                 le32_to_cpu(beacon->low_tsf), rate);
230 #endif
231
232         priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
233
234         if (!test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
235                 queue_work(priv->shrd->workqueue, &priv->beacon_update);
236 }
237
238 /* the threshold ratio of actual_ack_cnt to expected_ack_cnt in percent */
239 #define ACK_CNT_RATIO (50)
240 #define BA_TIMEOUT_CNT (5)
241 #define BA_TIMEOUT_MAX (16)
242
243 /**
244  * iwl_good_ack_health - checks for ACK count ratios, BA timeout retries.
245  *
246  * When the ACK count ratio is low and aggregated BA timeout retries exceeding
247  * the BA_TIMEOUT_MAX, reload firmware and bring system back to normal
248  * operation state.
249  */
250 static bool iwl_good_ack_health(struct iwl_priv *priv,
251                                 struct statistics_tx *cur)
252 {
253         int actual_delta, expected_delta, ba_timeout_delta;
254         struct statistics_tx *old;
255
256         if (priv->agg_tids_count)
257                 return true;
258
259         old = &priv->statistics.tx;
260
261         actual_delta = le32_to_cpu(cur->actual_ack_cnt) -
262                        le32_to_cpu(old->actual_ack_cnt);
263         expected_delta = le32_to_cpu(cur->expected_ack_cnt) -
264                          le32_to_cpu(old->expected_ack_cnt);
265
266         /* Values should not be negative, but we do not trust the firmware */
267         if (actual_delta <= 0 || expected_delta <= 0)
268                 return true;
269
270         ba_timeout_delta = le32_to_cpu(cur->agg.ba_timeout) -
271                            le32_to_cpu(old->agg.ba_timeout);
272
273         if ((actual_delta * 100 / expected_delta) < ACK_CNT_RATIO &&
274             ba_timeout_delta > BA_TIMEOUT_CNT) {
275                 IWL_DEBUG_RADIO(priv, "deltas: actual %d expected %d ba_timeout %d\n",
276                                 actual_delta, expected_delta, ba_timeout_delta);
277
278 #ifdef CONFIG_IWLWIFI_DEBUGFS
279                 /*
280                  * This is ifdef'ed on DEBUGFS because otherwise the
281                  * statistics aren't available. If DEBUGFS is set but
282                  * DEBUG is not, these will just compile out.
283                  */
284                 IWL_DEBUG_RADIO(priv, "rx_detected_cnt delta %d\n",
285                                 priv->delta_stats.tx.rx_detected_cnt);
286                 IWL_DEBUG_RADIO(priv,
287                                 "ack_or_ba_timeout_collision delta %d\n",
288                                 priv->delta_stats.tx.ack_or_ba_timeout_collision);
289 #endif
290
291                 if (ba_timeout_delta >= BA_TIMEOUT_MAX)
292                         return false;
293         }
294
295         return true;
296 }
297
298 /**
299  * iwl_good_plcp_health - checks for plcp error.
300  *
301  * When the plcp error is exceeding the thresholds, reset the radio
302  * to improve the throughput.
303  */
304 static bool iwl_good_plcp_health(struct iwl_priv *priv,
305                                  struct statistics_rx_phy *cur_ofdm,
306                                  struct statistics_rx_ht_phy *cur_ofdm_ht,
307                                  unsigned int msecs)
308 {
309         int delta;
310         int threshold = priv->cfg->base_params->plcp_delta_threshold;
311
312         if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
313                 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
314                 return true;
315         }
316
317         delta = le32_to_cpu(cur_ofdm->plcp_err) -
318                 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
319                 le32_to_cpu(cur_ofdm_ht->plcp_err) -
320                 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
321
322         /* Can be negative if firmware reset statistics */
323         if (delta <= 0)
324                 return true;
325
326         if ((delta * 100 / msecs) > threshold) {
327                 IWL_DEBUG_RADIO(priv,
328                                 "plcp health threshold %u delta %d msecs %u\n",
329                                 threshold, delta, msecs);
330                 return false;
331         }
332
333         return true;
334 }
335
336 static void iwl_recover_from_statistics(struct iwl_priv *priv,
337                                         struct statistics_rx_phy *cur_ofdm,
338                                         struct statistics_rx_ht_phy *cur_ofdm_ht,
339                                         struct statistics_tx *tx,
340                                         unsigned long stamp)
341 {
342         unsigned int msecs;
343
344         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
345                 return;
346
347         msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
348
349         /* Only gather statistics and update time stamp when not associated */
350         if (!iwl_is_any_associated(priv))
351                 return;
352
353         /* Do not check/recover when do not have enough statistics data */
354         if (msecs < 99)
355                 return;
356
357         if (iwlagn_mod_params.ack_check && !iwl_good_ack_health(priv, tx)) {
358                 IWL_ERR(priv, "low ack count detected, restart firmware\n");
359                 if (!iwl_force_reset(priv, IWL_FW_RESET, false))
360                         return;
361         }
362
363         if (iwlagn_mod_params.plcp_check &&
364             !iwl_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
365                 iwl_force_reset(priv, IWL_RF_RESET, false);
366 }
367
368 /* Calculate noise level, based on measurements during network silence just
369  *   before arriving beacon.  This measurement can be done only if we know
370  *   exactly when to expect beacons, therefore only when we're associated. */
371 static void iwl_rx_calc_noise(struct iwl_priv *priv)
372 {
373         struct statistics_rx_non_phy *rx_info;
374         int num_active_rx = 0;
375         int total_silence = 0;
376         int bcn_silence_a, bcn_silence_b, bcn_silence_c;
377         int last_rx_noise;
378
379         rx_info = &priv->statistics.rx_non_phy;
380
381         bcn_silence_a =
382                 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
383         bcn_silence_b =
384                 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
385         bcn_silence_c =
386                 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
387
388         if (bcn_silence_a) {
389                 total_silence += bcn_silence_a;
390                 num_active_rx++;
391         }
392         if (bcn_silence_b) {
393                 total_silence += bcn_silence_b;
394                 num_active_rx++;
395         }
396         if (bcn_silence_c) {
397                 total_silence += bcn_silence_c;
398                 num_active_rx++;
399         }
400
401         /* Average among active antennas */
402         if (num_active_rx)
403                 last_rx_noise = (total_silence / num_active_rx) - 107;
404         else
405                 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
406
407         IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
408                         bcn_silence_a, bcn_silence_b, bcn_silence_c,
409                         last_rx_noise);
410 }
411
412 #ifdef CONFIG_IWLWIFI_DEBUGFS
413 /*
414  *  based on the assumption of all statistics counter are in DWORD
415  *  FIXME: This function is for debugging, do not deal with
416  *  the case of counters roll-over.
417  */
418 static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
419                         __le32 *max_delta, __le32 *accum, int size)
420 {
421         int i;
422
423         for (i = 0;
424              i < size / sizeof(__le32);
425              i++, prev++, cur++, delta++, max_delta++, accum++) {
426                 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
427                         *delta = cpu_to_le32(
428                                 le32_to_cpu(*cur) - le32_to_cpu(*prev));
429                         le32_add_cpu(accum, le32_to_cpu(*delta));
430                         if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
431                                 *max_delta = *delta;
432                 }
433         }
434 }
435
436 static void
437 iwl_accumulative_statistics(struct iwl_priv *priv,
438                             struct statistics_general_common *common,
439                             struct statistics_rx_non_phy *rx_non_phy,
440                             struct statistics_rx_phy *rx_ofdm,
441                             struct statistics_rx_ht_phy *rx_ofdm_ht,
442                             struct statistics_rx_phy *rx_cck,
443                             struct statistics_tx *tx,
444                             struct statistics_bt_activity *bt_activity)
445 {
446 #define ACCUM(_name)    \
447         accum_stats((__le32 *)&priv->statistics._name,          \
448                     (__le32 *)_name,                            \
449                     (__le32 *)&priv->delta_stats._name,         \
450                     (__le32 *)&priv->max_delta_stats._name,     \
451                     (__le32 *)&priv->accum_stats._name,         \
452                     sizeof(*_name));
453
454         ACCUM(common);
455         ACCUM(rx_non_phy);
456         ACCUM(rx_ofdm);
457         ACCUM(rx_ofdm_ht);
458         ACCUM(rx_cck);
459         ACCUM(tx);
460         if (bt_activity)
461                 ACCUM(bt_activity);
462 #undef ACCUM
463 }
464 #else
465 static inline void
466 iwl_accumulative_statistics(struct iwl_priv *priv,
467                             struct statistics_general_common *common,
468                             struct statistics_rx_non_phy *rx_non_phy,
469                             struct statistics_rx_phy *rx_ofdm,
470                             struct statistics_rx_ht_phy *rx_ofdm_ht,
471                             struct statistics_rx_phy *rx_cck,
472                             struct statistics_tx *tx,
473                             struct statistics_bt_activity *bt_activity)
474 {
475 }
476 #endif
477
478 static void iwl_rx_statistics(struct iwl_priv *priv,
479                               struct iwl_rx_mem_buffer *rxb)
480 {
481         unsigned long stamp = jiffies;
482         const int reg_recalib_period = 60;
483         int change;
484         struct iwl_rx_packet *pkt = rxb_addr(rxb);
485         u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
486         __le32 *flag;
487         struct statistics_general_common *common;
488         struct statistics_rx_non_phy *rx_non_phy;
489         struct statistics_rx_phy *rx_ofdm;
490         struct statistics_rx_ht_phy *rx_ofdm_ht;
491         struct statistics_rx_phy *rx_cck;
492         struct statistics_tx *tx;
493         struct statistics_bt_activity *bt_activity;
494
495         len -= sizeof(struct iwl_cmd_header); /* skip header */
496
497         IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
498                      len);
499
500         if (len == sizeof(struct iwl_bt_notif_statistics)) {
501                 struct iwl_bt_notif_statistics *stats;
502                 stats = &pkt->u.stats_bt;
503                 flag = &stats->flag;
504                 common = &stats->general.common;
505                 rx_non_phy = &stats->rx.general.common;
506                 rx_ofdm = &stats->rx.ofdm;
507                 rx_ofdm_ht = &stats->rx.ofdm_ht;
508                 rx_cck = &stats->rx.cck;
509                 tx = &stats->tx;
510                 bt_activity = &stats->general.activity;
511
512 #ifdef CONFIG_IWLWIFI_DEBUGFS
513                 /* handle this exception directly */
514                 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
515                 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
516                              le32_to_cpu(stats->rx.general.num_bt_kills));
517 #endif
518         } else if (len == sizeof(struct iwl_notif_statistics)) {
519                 struct iwl_notif_statistics *stats;
520                 stats = &pkt->u.stats;
521                 flag = &stats->flag;
522                 common = &stats->general.common;
523                 rx_non_phy = &stats->rx.general;
524                 rx_ofdm = &stats->rx.ofdm;
525                 rx_ofdm_ht = &stats->rx.ofdm_ht;
526                 rx_cck = &stats->rx.cck;
527                 tx = &stats->tx;
528                 bt_activity = NULL;
529         } else {
530                 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
531                           len, sizeof(struct iwl_bt_notif_statistics),
532                           sizeof(struct iwl_notif_statistics));
533                 return;
534         }
535
536         change = common->temperature != priv->statistics.common.temperature ||
537                  (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
538                  (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
539
540         iwl_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
541                                     rx_ofdm_ht, rx_cck, tx, bt_activity);
542
543         iwl_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
544
545         priv->statistics.flag = *flag;
546         memcpy(&priv->statistics.common, common, sizeof(*common));
547         memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
548         memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
549         memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
550         memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
551         memcpy(&priv->statistics.tx, tx, sizeof(*tx));
552 #ifdef CONFIG_IWLWIFI_DEBUGFS
553         if (bt_activity)
554                 memcpy(&priv->statistics.bt_activity, bt_activity,
555                         sizeof(*bt_activity));
556 #endif
557
558         priv->rx_statistics_jiffies = stamp;
559
560         set_bit(STATUS_STATISTICS, &priv->shrd->status);
561
562         /* Reschedule the statistics timer to occur in
563          * reg_recalib_period seconds to ensure we get a
564          * thermal update even if the uCode doesn't give
565          * us one */
566         mod_timer(&priv->statistics_periodic, jiffies +
567                   msecs_to_jiffies(reg_recalib_period * 1000));
568
569         if (unlikely(!test_bit(STATUS_SCANNING, &priv->shrd->status)) &&
570             (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
571                 iwl_rx_calc_noise(priv);
572                 queue_work(priv->shrd->workqueue, &priv->run_time_calib_work);
573         }
574         if (priv->cfg->lib->temperature && change)
575                 priv->cfg->lib->temperature(priv);
576 }
577
578 static void iwl_rx_reply_statistics(struct iwl_priv *priv,
579                                     struct iwl_rx_mem_buffer *rxb)
580 {
581         struct iwl_rx_packet *pkt = rxb_addr(rxb);
582
583         if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
584 #ifdef CONFIG_IWLWIFI_DEBUGFS
585                 memset(&priv->accum_stats, 0,
586                         sizeof(priv->accum_stats));
587                 memset(&priv->delta_stats, 0,
588                         sizeof(priv->delta_stats));
589                 memset(&priv->max_delta_stats, 0,
590                         sizeof(priv->max_delta_stats));
591 #endif
592                 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
593         }
594         iwl_rx_statistics(priv, rxb);
595 }
596
597 /* Handle notification from uCode that card's power state is changing
598  * due to software, hardware, or critical temperature RFKILL */
599 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
600                                     struct iwl_rx_mem_buffer *rxb)
601 {
602         struct iwl_rx_packet *pkt = rxb_addr(rxb);
603         u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
604         unsigned long status = priv->shrd->status;
605
606         IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
607                           (flags & HW_CARD_DISABLED) ? "Kill" : "On",
608                           (flags & SW_CARD_DISABLED) ? "Kill" : "On",
609                           (flags & CT_CARD_DISABLED) ?
610                           "Reached" : "Not reached");
611
612         if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
613                      CT_CARD_DISABLED)) {
614
615                 iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_SET,
616                             CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
617
618                 iwl_write_direct32(bus(priv), HBUS_TARG_MBX_C,
619                                         HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
620
621                 if (!(flags & RXON_CARD_DISABLED)) {
622                         iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
623                                     CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
624                         iwl_write_direct32(bus(priv), HBUS_TARG_MBX_C,
625                                         HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
626                 }
627                 if (flags & CT_CARD_DISABLED)
628                         iwl_tt_enter_ct_kill(priv);
629         }
630         if (!(flags & CT_CARD_DISABLED))
631                 iwl_tt_exit_ct_kill(priv);
632
633         if (flags & HW_CARD_DISABLED)
634                 set_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
635         else
636                 clear_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
637
638
639         if (!(flags & RXON_CARD_DISABLED))
640                 iwl_scan_cancel(priv);
641
642         if ((test_bit(STATUS_RF_KILL_HW, &status) !=
643              test_bit(STATUS_RF_KILL_HW, &priv->shrd->status)))
644                 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
645                         test_bit(STATUS_RF_KILL_HW, &priv->shrd->status));
646         else
647                 wake_up_interruptible(&priv->shrd->wait_command_queue);
648 }
649
650 static void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
651                                        struct iwl_rx_mem_buffer *rxb)
652
653 {
654         struct iwl_rx_packet *pkt = rxb_addr(rxb);
655         struct iwl_missed_beacon_notif *missed_beacon;
656
657         missed_beacon = &pkt->u.missed_beacon;
658         if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
659             priv->missed_beacon_threshold) {
660                 IWL_DEBUG_CALIB(priv,
661                     "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
662                     le32_to_cpu(missed_beacon->consecutive_missed_beacons),
663                     le32_to_cpu(missed_beacon->total_missed_becons),
664                     le32_to_cpu(missed_beacon->num_recvd_beacons),
665                     le32_to_cpu(missed_beacon->num_expected_beacons));
666                 if (!test_bit(STATUS_SCANNING, &priv->shrd->status))
667                         iwl_init_sensitivity(priv);
668         }
669 }
670
671 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
672  * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
673 static void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
674                                 struct iwl_rx_mem_buffer *rxb)
675 {
676         struct iwl_rx_packet *pkt = rxb_addr(rxb);
677
678         priv->last_phy_res_valid = true;
679         memcpy(&priv->last_phy_res, pkt->u.raw,
680                sizeof(struct iwl_rx_phy_res));
681 }
682
683 /*
684  * returns non-zero if packet should be dropped
685  */
686 static int iwl_set_decrypted_flag(struct iwl_priv *priv,
687                                   struct ieee80211_hdr *hdr,
688                                   u32 decrypt_res,
689                                   struct ieee80211_rx_status *stats)
690 {
691         u16 fc = le16_to_cpu(hdr->frame_control);
692
693         /*
694          * All contexts have the same setting here due to it being
695          * a module parameter, so OK to check any context.
696          */
697         if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
698                                                 RXON_FILTER_DIS_DECRYPT_MSK)
699                 return 0;
700
701         if (!(fc & IEEE80211_FCTL_PROTECTED))
702                 return 0;
703
704         IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
705         switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
706         case RX_RES_STATUS_SEC_TYPE_TKIP:
707                 /* The uCode has got a bad phase 1 Key, pushes the packet.
708                  * Decryption will be done in SW. */
709                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
710                     RX_RES_STATUS_BAD_KEY_TTAK)
711                         break;
712
713         case RX_RES_STATUS_SEC_TYPE_WEP:
714                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
715                     RX_RES_STATUS_BAD_ICV_MIC) {
716                         /* bad ICV, the packet is destroyed since the
717                          * decryption is inplace, drop it */
718                         IWL_DEBUG_RX(priv, "Packet destroyed\n");
719                         return -1;
720                 }
721         case RX_RES_STATUS_SEC_TYPE_CCMP:
722                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
723                     RX_RES_STATUS_DECRYPT_OK) {
724                         IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
725                         stats->flag |= RX_FLAG_DECRYPTED;
726                 }
727                 break;
728
729         default:
730                 break;
731         }
732         return 0;
733 }
734
735 static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
736                                         struct ieee80211_hdr *hdr,
737                                         u16 len,
738                                         u32 ampdu_status,
739                                         struct iwl_rx_mem_buffer *rxb,
740                                         struct ieee80211_rx_status *stats)
741 {
742         struct sk_buff *skb;
743         __le16 fc = hdr->frame_control;
744         struct iwl_rxon_context *ctx;
745
746         /* We only process data packets if the interface is open */
747         if (unlikely(!priv->is_open)) {
748                 IWL_DEBUG_DROP_LIMIT(priv,
749                     "Dropping packet while interface is not open.\n");
750                 return;
751         }
752
753         /* In case of HW accelerated crypto and bad decryption, drop */
754         if (!iwlagn_mod_params.sw_crypto &&
755             iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
756                 return;
757
758         skb = dev_alloc_skb(128);
759         if (!skb) {
760                 IWL_ERR(priv, "dev_alloc_skb failed\n");
761                 return;
762         }
763
764         skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
765
766         iwl_update_stats(priv, false, fc, len);
767
768         /*
769         * Wake any queues that were stopped due to a passive channel tx
770         * failure. This can happen because the regulatory enforcement in
771         * the device waits for a beacon before allowing transmission,
772         * sometimes even after already having transmitted frames for the
773         * association because the new RXON may reset the information.
774         */
775         if (unlikely(ieee80211_is_beacon(fc))) {
776                 for_each_context(priv, ctx) {
777                         if (!ctx->last_tx_rejected)
778                                 continue;
779                         if (compare_ether_addr(hdr->addr3,
780                                                ctx->active.bssid_addr))
781                                 continue;
782                         ctx->last_tx_rejected = false;
783                         iwl_trans_wake_any_queue(trans(priv), ctx->ctxid);
784                 }
785         }
786
787         memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
788
789         ieee80211_rx(priv->hw, skb);
790         rxb->page = NULL;
791 }
792
793 static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
794 {
795         u32 decrypt_out = 0;
796
797         if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
798                                         RX_RES_STATUS_STATION_FOUND)
799                 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
800                                 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
801
802         decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
803
804         /* packet was not encrypted */
805         if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
806                                         RX_RES_STATUS_SEC_TYPE_NONE)
807                 return decrypt_out;
808
809         /* packet was encrypted with unknown alg */
810         if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
811                                         RX_RES_STATUS_SEC_TYPE_ERR)
812                 return decrypt_out;
813
814         /* decryption was not done in HW */
815         if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
816                                         RX_MPDU_RES_STATUS_DEC_DONE_MSK)
817                 return decrypt_out;
818
819         switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
820
821         case RX_RES_STATUS_SEC_TYPE_CCMP:
822                 /* alg is CCM: check MIC only */
823                 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
824                         /* Bad MIC */
825                         decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
826                 else
827                         decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
828
829                 break;
830
831         case RX_RES_STATUS_SEC_TYPE_TKIP:
832                 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
833                         /* Bad TTAK */
834                         decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
835                         break;
836                 }
837                 /* fall through if TTAK OK */
838         default:
839                 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
840                         decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
841                 else
842                         decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
843                 break;
844         }
845
846         IWL_DEBUG_RX(priv, "decrypt_in:0x%x  decrypt_out = 0x%x\n",
847                                         decrypt_in, decrypt_out);
848
849         return decrypt_out;
850 }
851
852 /* Calc max signal level (dBm) among 3 possible receivers */
853 static int iwlagn_calc_rssi(struct iwl_priv *priv,
854                              struct iwl_rx_phy_res *rx_resp)
855 {
856         /* data from PHY/DSP regarding signal strength, etc.,
857          *   contents are always there, not configurable by host
858          */
859         struct iwlagn_non_cfg_phy *ncphy =
860                 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
861         u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
862         u8 agc;
863
864         val  = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
865         agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
866
867         /* Find max rssi among 3 possible receivers.
868          * These values are measured by the digital signal processor (DSP).
869          * They should stay fairly constant even as the signal strength varies,
870          *   if the radio's automatic gain control (AGC) is working right.
871          * AGC value (see below) will provide the "interesting" info.
872          */
873         val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
874         rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
875                 IWLAGN_OFDM_RSSI_A_BIT_POS;
876         rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
877                 IWLAGN_OFDM_RSSI_B_BIT_POS;
878         val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
879         rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
880                 IWLAGN_OFDM_RSSI_C_BIT_POS;
881
882         max_rssi = max_t(u32, rssi_a, rssi_b);
883         max_rssi = max_t(u32, max_rssi, rssi_c);
884
885         IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
886                 rssi_a, rssi_b, rssi_c, max_rssi, agc);
887
888         /* dBm = max_rssi dB - agc dB - constant.
889          * Higher AGC (higher radio gain) means lower signal. */
890         return max_rssi - agc - IWLAGN_RSSI_OFFSET;
891 }
892
893 /* Called for REPLY_RX (legacy ABG frames), or
894  * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
895 static void iwl_rx_reply_rx(struct iwl_priv *priv,
896                             struct iwl_rx_mem_buffer *rxb)
897 {
898         struct ieee80211_hdr *header;
899         struct ieee80211_rx_status rx_status;
900         struct iwl_rx_packet *pkt = rxb_addr(rxb);
901         struct iwl_rx_phy_res *phy_res;
902         __le32 rx_pkt_status;
903         struct iwl_rx_mpdu_res_start *amsdu;
904         u32 len;
905         u32 ampdu_status;
906         u32 rate_n_flags;
907
908         /**
909          * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
910          *      REPLY_RX: physical layer info is in this buffer
911          *      REPLY_RX_MPDU_CMD: physical layer info was sent in separate
912          *              command and cached in priv->last_phy_res
913          *
914          * Here we set up local variables depending on which command is
915          * received.
916          */
917         if (pkt->hdr.cmd == REPLY_RX) {
918                 phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
919                 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
920                                 + phy_res->cfg_phy_cnt);
921
922                 len = le16_to_cpu(phy_res->byte_count);
923                 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
924                                 phy_res->cfg_phy_cnt + len);
925                 ampdu_status = le32_to_cpu(rx_pkt_status);
926         } else {
927                 if (!priv->last_phy_res_valid) {
928                         IWL_ERR(priv, "MPDU frame without cached PHY data\n");
929                         return;
930                 }
931                 phy_res = &priv->last_phy_res;
932                 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
933                 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
934                 len = le16_to_cpu(amsdu->byte_count);
935                 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
936                 ampdu_status = iwl_translate_rx_status(priv,
937                                                 le32_to_cpu(rx_pkt_status));
938         }
939
940         if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
941                 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
942                                 phy_res->cfg_phy_cnt);
943                 return;
944         }
945
946         if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
947             !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
948                 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
949                                 le32_to_cpu(rx_pkt_status));
950                 return;
951         }
952
953         /* This will be used in several places later */
954         rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
955
956         /* rx_status carries information about the packet to mac80211 */
957         rx_status.mactime = le64_to_cpu(phy_res->timestamp);
958         rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
959                                 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
960         rx_status.freq =
961                 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
962                                                rx_status.band);
963         rx_status.rate_idx =
964                 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
965         rx_status.flag = 0;
966
967         /* TSF isn't reliable. In order to allow smooth user experience,
968          * this W/A doesn't propagate it to the mac80211 */
969         /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
970
971         priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
972
973         /* Find max signal strength (dBm) among 3 antenna/receiver chains */
974         rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
975
976         iwl_dbg_log_rx_data_frame(priv, len, header);
977         IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
978                 rx_status.signal, (unsigned long long)rx_status.mactime);
979
980         /*
981          * "antenna number"
982          *
983          * It seems that the antenna field in the phy flags value
984          * is actually a bit field. This is undefined by radiotap,
985          * it wants an actual antenna number but I always get "7"
986          * for most legacy frames I receive indicating that the
987          * same frame was received on all three RX chains.
988          *
989          * I think this field should be removed in favor of a
990          * new 802.11n radiotap field "RX chains" that is defined
991          * as a bitmask.
992          */
993         rx_status.antenna =
994                 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
995                 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
996
997         /* set the preamble flag if appropriate */
998         if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
999                 rx_status.flag |= RX_FLAG_SHORTPRE;
1000
1001         /* Set up the HT phy flags */
1002         if (rate_n_flags & RATE_MCS_HT_MSK)
1003                 rx_status.flag |= RX_FLAG_HT;
1004         if (rate_n_flags & RATE_MCS_HT40_MSK)
1005                 rx_status.flag |= RX_FLAG_40MHZ;
1006         if (rate_n_flags & RATE_MCS_SGI_MSK)
1007                 rx_status.flag |= RX_FLAG_SHORT_GI;
1008
1009         iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1010                                     rxb, &rx_status);
1011 }
1012
1013 /**
1014  * iwl_setup_rx_handlers - Initialize Rx handler callbacks
1015  *
1016  * Setup the RX handlers for each of the reply types sent from the uCode
1017  * to the host.
1018  */
1019 void iwl_setup_rx_handlers(struct iwl_priv *priv)
1020 {
1021         void (**handlers)(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb);
1022
1023         handlers = priv->rx_handlers;
1024
1025         handlers[REPLY_ERROR]                   = iwl_rx_reply_error;
1026         handlers[CHANNEL_SWITCH_NOTIFICATION]   = iwl_rx_csa;
1027         handlers[SPECTRUM_MEASURE_NOTIFICATION] = iwl_rx_spectrum_measure_notif;
1028         handlers[PM_SLEEP_NOTIFICATION]         = iwl_rx_pm_sleep_notif;
1029         handlers[PM_DEBUG_STATISTIC_NOTIFIC]    = iwl_rx_pm_debug_statistics_notif;
1030         handlers[BEACON_NOTIFICATION]           = iwl_rx_beacon_notif;
1031
1032         /*
1033          * The same handler is used for both the REPLY to a discrete
1034          * statistics request from the host as well as for the periodic
1035          * statistics notifications (after received beacons) from the uCode.
1036          */
1037         handlers[REPLY_STATISTICS_CMD]          = iwl_rx_reply_statistics;
1038         handlers[STATISTICS_NOTIFICATION]       = iwl_rx_statistics;
1039
1040         iwl_setup_rx_scan_handlers(priv);
1041
1042         handlers[CARD_STATE_NOTIFICATION]       = iwl_rx_card_state_notif;
1043         handlers[MISSED_BEACONS_NOTIFICATION]   = iwl_rx_missed_beacon_notif;
1044
1045         /* Rx handlers */
1046         handlers[REPLY_RX_PHY_CMD]              = iwl_rx_reply_rx_phy;
1047         handlers[REPLY_RX_MPDU_CMD]             = iwl_rx_reply_rx;
1048
1049         /* block ack */
1050         handlers[REPLY_COMPRESSED_BA]           = iwlagn_rx_reply_compressed_ba;
1051
1052         /* init calibration handlers */
1053         priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
1054                                         iwlagn_rx_calib_result;
1055         priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
1056
1057         /* set up notification wait support */
1058         spin_lock_init(&priv->notif_wait_lock);
1059         INIT_LIST_HEAD(&priv->notif_waits);
1060         init_waitqueue_head(&priv->notif_waitq);
1061
1062         /* Set up BT Rx handlers */
1063         if (priv->cfg->lib->bt_rx_handler_setup)
1064                 priv->cfg->lib->bt_rx_handler_setup(priv);
1065
1066 }
1067
1068 void iwl_rx_dispatch(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
1069 {
1070         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1071
1072         /*
1073          * Do the notification wait before RX handlers so
1074          * even if the RX handler consumes the RXB we have
1075          * access to it in the notification wait entry.
1076          */
1077         if (!list_empty(&priv->notif_waits)) {
1078                 struct iwl_notification_wait *w;
1079
1080                 spin_lock(&priv->notif_wait_lock);
1081                 list_for_each_entry(w, &priv->notif_waits, list) {
1082                         if (w->cmd != pkt->hdr.cmd)
1083                                 continue;
1084                         IWL_DEBUG_RX(priv,
1085                                 "Notif: %s, 0x%02x - wake the callers up\n",
1086                                 get_cmd_string(pkt->hdr.cmd),
1087                                 pkt->hdr.cmd);
1088                         w->triggered = true;
1089                         if (w->fn)
1090                                 w->fn(priv, pkt, w->fn_data);
1091                 }
1092                 spin_unlock(&priv->notif_wait_lock);
1093
1094                 wake_up_all(&priv->notif_waitq);
1095         }
1096
1097         if (priv->pre_rx_handler)
1098                 priv->pre_rx_handler(priv, rxb);
1099
1100         /* Based on type of command response or notification,
1101          *   handle those that need handling via function in
1102          *   rx_handlers table.  See iwl_setup_rx_handlers() */
1103         if (priv->rx_handlers[pkt->hdr.cmd]) {
1104                 priv->rx_handlers_stats[pkt->hdr.cmd]++;
1105                 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1106         } else {
1107                 /* No handling needed */
1108                 IWL_DEBUG_RX(priv,
1109                         "No handler needed for %s, 0x%02x\n",
1110                         get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1111         }
1112 }