iwlwifi: remove all occurrences of the FSF address paragraph
[linux-2.6-microblaze.git] / drivers / net / wireless / intel / iwlwifi / dvm / rx.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
4  * Copyright(c) 2015 Intel Deutschland GmbH
5  *
6  * Portions of this file are derived from the ipw3945 project, as well
7  * as portionhelp of the ieee80211 subsystem header files.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  *  Intel Linux Wireless <linuxwifi@intel.com>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  *****************************************************************************/
26
27 #include <linux/etherdevice.h>
28 #include <linux/slab.h>
29 #include <linux/sched.h>
30 #include <net/mac80211.h>
31 #include <asm/unaligned.h>
32
33 #include "iwl-trans.h"
34 #include "iwl-io.h"
35 #include "dev.h"
36 #include "calib.h"
37 #include "agn.h"
38
39 /******************************************************************************
40  *
41  * Generic RX handler implementations
42  *
43  ******************************************************************************/
44
45 static void iwlagn_rx_reply_error(struct iwl_priv *priv,
46                                   struct iwl_rx_cmd_buffer *rxb)
47 {
48         struct iwl_rx_packet *pkt = rxb_addr(rxb);
49         struct iwl_error_resp *err_resp = (void *)pkt->data;
50
51         IWL_ERR(priv, "Error Reply type 0x%08X cmd REPLY_ERROR (0x%02X) "
52                 "seq 0x%04X ser 0x%08X\n",
53                 le32_to_cpu(err_resp->error_type),
54                 err_resp->cmd_id,
55                 le16_to_cpu(err_resp->bad_cmd_seq_num),
56                 le32_to_cpu(err_resp->error_info));
57 }
58
59 static void iwlagn_rx_csa(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb)
60 {
61         struct iwl_rx_packet *pkt = rxb_addr(rxb);
62         struct iwl_csa_notification *csa = (void *)pkt->data;
63         /*
64          * MULTI-FIXME
65          * See iwlagn_mac_channel_switch.
66          */
67         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
68         struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
69
70         if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
71                 return;
72
73         if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
74                 rxon->channel = csa->channel;
75                 ctx->staging.channel = csa->channel;
76                 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
77                               le16_to_cpu(csa->channel));
78                 iwl_chswitch_done(priv, true);
79         } else {
80                 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
81                         le16_to_cpu(csa->channel));
82                 iwl_chswitch_done(priv, false);
83         }
84 }
85
86
87 static void iwlagn_rx_spectrum_measure_notif(struct iwl_priv *priv,
88                                              struct iwl_rx_cmd_buffer *rxb)
89 {
90         struct iwl_rx_packet *pkt = rxb_addr(rxb);
91         struct iwl_spectrum_notification *report = (void *)pkt->data;
92
93         if (!report->state) {
94                 IWL_DEBUG_11H(priv,
95                         "Spectrum Measure Notification: Start\n");
96                 return;
97         }
98
99         memcpy(&priv->measure_report, report, sizeof(*report));
100         priv->measurement_status |= MEASUREMENT_READY;
101 }
102
103 static void iwlagn_rx_pm_sleep_notif(struct iwl_priv *priv,
104                                      struct iwl_rx_cmd_buffer *rxb)
105 {
106 #ifdef CONFIG_IWLWIFI_DEBUG
107         struct iwl_rx_packet *pkt = rxb_addr(rxb);
108         struct iwl_sleep_notification *sleep = (void *)pkt->data;
109         IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
110                      sleep->pm_sleep_mode, sleep->pm_wakeup_src);
111 #endif
112 }
113
114 static void iwlagn_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
115                                                 struct iwl_rx_cmd_buffer *rxb)
116 {
117         struct iwl_rx_packet *pkt = rxb_addr(rxb);
118         u32 __maybe_unused len = iwl_rx_packet_len(pkt);
119         IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
120                         "notification for PM_DEBUG_STATISTIC_NOTIFIC:\n", len);
121         iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->data, len);
122 }
123
124 static void iwlagn_rx_beacon_notif(struct iwl_priv *priv,
125                                    struct iwl_rx_cmd_buffer *rxb)
126 {
127         struct iwl_rx_packet *pkt = rxb_addr(rxb);
128         struct iwlagn_beacon_notif *beacon = (void *)pkt->data;
129 #ifdef CONFIG_IWLWIFI_DEBUG
130         u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
131         u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
132
133         IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
134                 "tsf:0x%.8x%.8x rate:%d\n",
135                 status & TX_STATUS_MSK,
136                 beacon->beacon_notify_hdr.failure_frame,
137                 le32_to_cpu(beacon->ibss_mgr_status),
138                 le32_to_cpu(beacon->high_tsf),
139                 le32_to_cpu(beacon->low_tsf), rate);
140 #endif
141
142         priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
143 }
144
145 /**
146  * iwl_good_plcp_health - checks for plcp error.
147  *
148  * When the plcp error is exceeding the thresholds, reset the radio
149  * to improve the throughput.
150  */
151 static bool iwlagn_good_plcp_health(struct iwl_priv *priv,
152                                  struct statistics_rx_phy *cur_ofdm,
153                                  struct statistics_rx_ht_phy *cur_ofdm_ht,
154                                  unsigned int msecs)
155 {
156         int delta;
157         int threshold = priv->plcp_delta_threshold;
158
159         if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
160                 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
161                 return true;
162         }
163
164         delta = le32_to_cpu(cur_ofdm->plcp_err) -
165                 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
166                 le32_to_cpu(cur_ofdm_ht->plcp_err) -
167                 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
168
169         /* Can be negative if firmware reset statistics */
170         if (delta <= 0)
171                 return true;
172
173         if ((delta * 100 / msecs) > threshold) {
174                 IWL_DEBUG_RADIO(priv,
175                                 "plcp health threshold %u delta %d msecs %u\n",
176                                 threshold, delta, msecs);
177                 return false;
178         }
179
180         return true;
181 }
182
183 int iwl_force_rf_reset(struct iwl_priv *priv, bool external)
184 {
185         struct iwl_rf_reset *rf_reset;
186
187         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
188                 return -EAGAIN;
189
190         if (!iwl_is_any_associated(priv)) {
191                 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
192                 return -ENOLINK;
193         }
194
195         rf_reset = &priv->rf_reset;
196         rf_reset->reset_request_count++;
197         if (!external && rf_reset->last_reset_jiffies &&
198             time_after(rf_reset->last_reset_jiffies +
199                        IWL_DELAY_NEXT_FORCE_RF_RESET, jiffies)) {
200                 IWL_DEBUG_INFO(priv, "RF reset rejected\n");
201                 rf_reset->reset_reject_count++;
202                 return -EAGAIN;
203         }
204         rf_reset->reset_success_count++;
205         rf_reset->last_reset_jiffies = jiffies;
206
207         /*
208          * There is no easy and better way to force reset the radio,
209          * the only known method is switching channel which will force to
210          * reset and tune the radio.
211          * Use internal short scan (single channel) operation to should
212          * achieve this objective.
213          * Driver should reset the radio when number of consecutive missed
214          * beacon, or any other uCode error condition detected.
215          */
216         IWL_DEBUG_INFO(priv, "perform radio reset.\n");
217         iwl_internal_short_hw_scan(priv);
218         return 0;
219 }
220
221
222 static void iwlagn_recover_from_statistics(struct iwl_priv *priv,
223                                 struct statistics_rx_phy *cur_ofdm,
224                                 struct statistics_rx_ht_phy *cur_ofdm_ht,
225                                 struct statistics_tx *tx,
226                                 unsigned long stamp)
227 {
228         unsigned int msecs;
229
230         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
231                 return;
232
233         msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
234
235         /* Only gather statistics and update time stamp when not associated */
236         if (!iwl_is_any_associated(priv))
237                 return;
238
239         /* Do not check/recover when do not have enough statistics data */
240         if (msecs < 99)
241                 return;
242
243         if (!iwlagn_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
244                 iwl_force_rf_reset(priv, false);
245 }
246
247 /* Calculate noise level, based on measurements during network silence just
248  *   before arriving beacon.  This measurement can be done only if we know
249  *   exactly when to expect beacons, therefore only when we're associated. */
250 static void iwlagn_rx_calc_noise(struct iwl_priv *priv)
251 {
252         struct statistics_rx_non_phy *rx_info;
253         int num_active_rx = 0;
254         int total_silence = 0;
255         int bcn_silence_a, bcn_silence_b, bcn_silence_c;
256         int last_rx_noise;
257
258         rx_info = &priv->statistics.rx_non_phy;
259
260         bcn_silence_a =
261                 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
262         bcn_silence_b =
263                 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
264         bcn_silence_c =
265                 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
266
267         if (bcn_silence_a) {
268                 total_silence += bcn_silence_a;
269                 num_active_rx++;
270         }
271         if (bcn_silence_b) {
272                 total_silence += bcn_silence_b;
273                 num_active_rx++;
274         }
275         if (bcn_silence_c) {
276                 total_silence += bcn_silence_c;
277                 num_active_rx++;
278         }
279
280         /* Average among active antennas */
281         if (num_active_rx)
282                 last_rx_noise = (total_silence / num_active_rx) - 107;
283         else
284                 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
285
286         IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
287                         bcn_silence_a, bcn_silence_b, bcn_silence_c,
288                         last_rx_noise);
289 }
290
291 #ifdef CONFIG_IWLWIFI_DEBUGFS
292 /*
293  *  based on the assumption of all statistics counter are in DWORD
294  *  FIXME: This function is for debugging, do not deal with
295  *  the case of counters roll-over.
296  */
297 static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
298                         __le32 *max_delta, __le32 *accum, int size)
299 {
300         int i;
301
302         for (i = 0;
303              i < size / sizeof(__le32);
304              i++, prev++, cur++, delta++, max_delta++, accum++) {
305                 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
306                         *delta = cpu_to_le32(
307                                 le32_to_cpu(*cur) - le32_to_cpu(*prev));
308                         le32_add_cpu(accum, le32_to_cpu(*delta));
309                         if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
310                                 *max_delta = *delta;
311                 }
312         }
313 }
314
315 static void
316 iwlagn_accumulative_statistics(struct iwl_priv *priv,
317                             struct statistics_general_common *common,
318                             struct statistics_rx_non_phy *rx_non_phy,
319                             struct statistics_rx_phy *rx_ofdm,
320                             struct statistics_rx_ht_phy *rx_ofdm_ht,
321                             struct statistics_rx_phy *rx_cck,
322                             struct statistics_tx *tx,
323                             struct statistics_bt_activity *bt_activity)
324 {
325 #define ACCUM(_name)    \
326         accum_stats((__le32 *)&priv->statistics._name,          \
327                     (__le32 *)_name,                            \
328                     (__le32 *)&priv->delta_stats._name,         \
329                     (__le32 *)&priv->max_delta_stats._name,     \
330                     (__le32 *)&priv->accum_stats._name,         \
331                     sizeof(*_name));
332
333         ACCUM(common);
334         ACCUM(rx_non_phy);
335         ACCUM(rx_ofdm);
336         ACCUM(rx_ofdm_ht);
337         ACCUM(rx_cck);
338         ACCUM(tx);
339         if (bt_activity)
340                 ACCUM(bt_activity);
341 #undef ACCUM
342 }
343 #else
344 static inline void
345 iwlagn_accumulative_statistics(struct iwl_priv *priv,
346                             struct statistics_general_common *common,
347                             struct statistics_rx_non_phy *rx_non_phy,
348                             struct statistics_rx_phy *rx_ofdm,
349                             struct statistics_rx_ht_phy *rx_ofdm_ht,
350                             struct statistics_rx_phy *rx_cck,
351                             struct statistics_tx *tx,
352                             struct statistics_bt_activity *bt_activity)
353 {
354 }
355 #endif
356
357 static void iwlagn_rx_statistics(struct iwl_priv *priv,
358                                  struct iwl_rx_cmd_buffer *rxb)
359 {
360         unsigned long stamp = jiffies;
361         const int reg_recalib_period = 60;
362         int change;
363         struct iwl_rx_packet *pkt = rxb_addr(rxb);
364         u32 len = iwl_rx_packet_payload_len(pkt);
365         __le32 *flag;
366         struct statistics_general_common *common;
367         struct statistics_rx_non_phy *rx_non_phy;
368         struct statistics_rx_phy *rx_ofdm;
369         struct statistics_rx_ht_phy *rx_ofdm_ht;
370         struct statistics_rx_phy *rx_cck;
371         struct statistics_tx *tx;
372         struct statistics_bt_activity *bt_activity;
373
374         IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
375                      len);
376
377         spin_lock(&priv->statistics.lock);
378
379         if (len == sizeof(struct iwl_bt_notif_statistics)) {
380                 struct iwl_bt_notif_statistics *stats;
381                 stats = (void *)&pkt->data;
382                 flag = &stats->flag;
383                 common = &stats->general.common;
384                 rx_non_phy = &stats->rx.general.common;
385                 rx_ofdm = &stats->rx.ofdm;
386                 rx_ofdm_ht = &stats->rx.ofdm_ht;
387                 rx_cck = &stats->rx.cck;
388                 tx = &stats->tx;
389                 bt_activity = &stats->general.activity;
390
391 #ifdef CONFIG_IWLWIFI_DEBUGFS
392                 /* handle this exception directly */
393                 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
394                 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
395                              le32_to_cpu(stats->rx.general.num_bt_kills));
396 #endif
397         } else if (len == sizeof(struct iwl_notif_statistics)) {
398                 struct iwl_notif_statistics *stats;
399                 stats = (void *)&pkt->data;
400                 flag = &stats->flag;
401                 common = &stats->general.common;
402                 rx_non_phy = &stats->rx.general;
403                 rx_ofdm = &stats->rx.ofdm;
404                 rx_ofdm_ht = &stats->rx.ofdm_ht;
405                 rx_cck = &stats->rx.cck;
406                 tx = &stats->tx;
407                 bt_activity = NULL;
408         } else {
409                 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
410                           len, sizeof(struct iwl_bt_notif_statistics),
411                           sizeof(struct iwl_notif_statistics));
412                 spin_unlock(&priv->statistics.lock);
413                 return;
414         }
415
416         change = common->temperature != priv->statistics.common.temperature ||
417                  (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
418                  (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
419
420         iwlagn_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
421                                     rx_ofdm_ht, rx_cck, tx, bt_activity);
422
423         iwlagn_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
424
425         priv->statistics.flag = *flag;
426         memcpy(&priv->statistics.common, common, sizeof(*common));
427         memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
428         memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
429         memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
430         memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
431         memcpy(&priv->statistics.tx, tx, sizeof(*tx));
432 #ifdef CONFIG_IWLWIFI_DEBUGFS
433         if (bt_activity)
434                 memcpy(&priv->statistics.bt_activity, bt_activity,
435                         sizeof(*bt_activity));
436 #endif
437
438         priv->rx_statistics_jiffies = stamp;
439
440         set_bit(STATUS_STATISTICS, &priv->status);
441
442         /* Reschedule the statistics timer to occur in
443          * reg_recalib_period seconds to ensure we get a
444          * thermal update even if the uCode doesn't give
445          * us one */
446         mod_timer(&priv->statistics_periodic, jiffies +
447                   msecs_to_jiffies(reg_recalib_period * 1000));
448
449         if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
450             (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
451                 iwlagn_rx_calc_noise(priv);
452                 queue_work(priv->workqueue, &priv->run_time_calib_work);
453         }
454         if (priv->lib->temperature && change)
455                 priv->lib->temperature(priv);
456
457         spin_unlock(&priv->statistics.lock);
458 }
459
460 static void iwlagn_rx_reply_statistics(struct iwl_priv *priv,
461                                        struct iwl_rx_cmd_buffer *rxb)
462 {
463         struct iwl_rx_packet *pkt = rxb_addr(rxb);
464         struct iwl_notif_statistics *stats = (void *)pkt->data;
465
466         if (le32_to_cpu(stats->flag) & UCODE_STATISTICS_CLEAR_MSK) {
467 #ifdef CONFIG_IWLWIFI_DEBUGFS
468                 memset(&priv->accum_stats, 0,
469                         sizeof(priv->accum_stats));
470                 memset(&priv->delta_stats, 0,
471                         sizeof(priv->delta_stats));
472                 memset(&priv->max_delta_stats, 0,
473                         sizeof(priv->max_delta_stats));
474 #endif
475                 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
476         }
477
478         iwlagn_rx_statistics(priv, rxb);
479 }
480
481 /* Handle notification from uCode that card's power state is changing
482  * due to software, hardware, or critical temperature RFKILL */
483 static void iwlagn_rx_card_state_notif(struct iwl_priv *priv,
484                                        struct iwl_rx_cmd_buffer *rxb)
485 {
486         struct iwl_rx_packet *pkt = rxb_addr(rxb);
487         struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
488         u32 flags = le32_to_cpu(card_state_notif->flags);
489         unsigned long status = priv->status;
490
491         IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
492                           (flags & HW_CARD_DISABLED) ? "Kill" : "On",
493                           (flags & SW_CARD_DISABLED) ? "Kill" : "On",
494                           (flags & CT_CARD_DISABLED) ?
495                           "Reached" : "Not reached");
496
497         if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
498                      CT_CARD_DISABLED)) {
499
500                 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
501                             CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
502
503                 iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
504                                         HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
505
506                 if (!(flags & RXON_CARD_DISABLED)) {
507                         iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
508                                     CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
509                         iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
510                                         HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
511                 }
512                 if (flags & CT_CARD_DISABLED)
513                         iwl_tt_enter_ct_kill(priv);
514         }
515         if (!(flags & CT_CARD_DISABLED))
516                 iwl_tt_exit_ct_kill(priv);
517
518         if (flags & HW_CARD_DISABLED)
519                 set_bit(STATUS_RF_KILL_HW, &priv->status);
520         else
521                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
522
523
524         if (!(flags & RXON_CARD_DISABLED))
525                 iwl_scan_cancel(priv);
526
527         if ((test_bit(STATUS_RF_KILL_HW, &status) !=
528              test_bit(STATUS_RF_KILL_HW, &priv->status)))
529                 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
530                         test_bit(STATUS_RF_KILL_HW, &priv->status));
531 }
532
533 static void iwlagn_rx_missed_beacon_notif(struct iwl_priv *priv,
534                                           struct iwl_rx_cmd_buffer *rxb)
535
536 {
537         struct iwl_rx_packet *pkt = rxb_addr(rxb);
538         struct iwl_missed_beacon_notif *missed_beacon = (void *)pkt->data;
539
540         if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
541             priv->missed_beacon_threshold) {
542                 IWL_DEBUG_CALIB(priv,
543                     "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
544                     le32_to_cpu(missed_beacon->consecutive_missed_beacons),
545                     le32_to_cpu(missed_beacon->total_missed_becons),
546                     le32_to_cpu(missed_beacon->num_recvd_beacons),
547                     le32_to_cpu(missed_beacon->num_expected_beacons));
548                 if (!test_bit(STATUS_SCANNING, &priv->status))
549                         iwl_init_sensitivity(priv);
550         }
551 }
552
553 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
554  * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
555 static void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
556                                    struct iwl_rx_cmd_buffer *rxb)
557 {
558         struct iwl_rx_packet *pkt = rxb_addr(rxb);
559
560         priv->last_phy_res_valid = true;
561         priv->ampdu_ref++;
562         memcpy(&priv->last_phy_res, pkt->data,
563                sizeof(struct iwl_rx_phy_res));
564 }
565
566 /*
567  * returns non-zero if packet should be dropped
568  */
569 static int iwlagn_set_decrypted_flag(struct iwl_priv *priv,
570                                   struct ieee80211_hdr *hdr,
571                                   u32 decrypt_res,
572                                   struct ieee80211_rx_status *stats)
573 {
574         u16 fc = le16_to_cpu(hdr->frame_control);
575
576         /*
577          * All contexts have the same setting here due to it being
578          * a module parameter, so OK to check any context.
579          */
580         if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
581                                                 RXON_FILTER_DIS_DECRYPT_MSK)
582                 return 0;
583
584         if (!(fc & IEEE80211_FCTL_PROTECTED))
585                 return 0;
586
587         IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
588         switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
589         case RX_RES_STATUS_SEC_TYPE_TKIP:
590                 /* The uCode has got a bad phase 1 Key, pushes the packet.
591                  * Decryption will be done in SW. */
592                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
593                     RX_RES_STATUS_BAD_KEY_TTAK)
594                         break;
595
596         case RX_RES_STATUS_SEC_TYPE_WEP:
597                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
598                     RX_RES_STATUS_BAD_ICV_MIC) {
599                         /* bad ICV, the packet is destroyed since the
600                          * decryption is inplace, drop it */
601                         IWL_DEBUG_RX(priv, "Packet destroyed\n");
602                         return -1;
603                 }
604         case RX_RES_STATUS_SEC_TYPE_CCMP:
605                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
606                     RX_RES_STATUS_DECRYPT_OK) {
607                         IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
608                         stats->flag |= RX_FLAG_DECRYPTED;
609                 }
610                 break;
611
612         default:
613                 break;
614         }
615         return 0;
616 }
617
618 static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
619                                         struct ieee80211_hdr *hdr,
620                                         u16 len,
621                                         u32 ampdu_status,
622                                         struct iwl_rx_cmd_buffer *rxb,
623                                         struct ieee80211_rx_status *stats)
624 {
625         struct sk_buff *skb;
626         __le16 fc = hdr->frame_control;
627         struct iwl_rxon_context *ctx;
628         unsigned int hdrlen, fraglen;
629
630         /* We only process data packets if the interface is open */
631         if (unlikely(!priv->is_open)) {
632                 IWL_DEBUG_DROP_LIMIT(priv,
633                     "Dropping packet while interface is not open.\n");
634                 return;
635         }
636
637         /* In case of HW accelerated crypto and bad decryption, drop */
638         if (!iwlwifi_mod_params.swcrypto &&
639             iwlagn_set_decrypted_flag(priv, hdr, ampdu_status, stats))
640                 return;
641
642         /* Dont use dev_alloc_skb(), we'll have enough headroom once
643          * ieee80211_hdr pulled.
644          */
645         skb = alloc_skb(128, GFP_ATOMIC);
646         if (!skb) {
647                 IWL_ERR(priv, "alloc_skb failed\n");
648                 return;
649         }
650         /* If frame is small enough to fit in skb->head, pull it completely.
651          * If not, only pull ieee80211_hdr so that splice() or TCP coalesce
652          * are more efficient.
653          */
654         hdrlen = (len <= skb_tailroom(skb)) ? len : sizeof(*hdr);
655
656         skb_put_data(skb, hdr, hdrlen);
657         fraglen = len - hdrlen;
658
659         if (fraglen) {
660                 int offset = (void *)hdr + hdrlen -
661                              rxb_addr(rxb) + rxb_offset(rxb);
662
663                 skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
664                                 fraglen, rxb->truesize);
665         }
666
667         /*
668         * Wake any queues that were stopped due to a passive channel tx
669         * failure. This can happen because the regulatory enforcement in
670         * the device waits for a beacon before allowing transmission,
671         * sometimes even after already having transmitted frames for the
672         * association because the new RXON may reset the information.
673         */
674         if (unlikely(ieee80211_is_beacon(fc) && priv->passive_no_rx)) {
675                 for_each_context(priv, ctx) {
676                         if (!ether_addr_equal(hdr->addr3,
677                                               ctx->active.bssid_addr))
678                                 continue;
679                         iwlagn_lift_passive_no_rx(priv);
680                 }
681         }
682
683         memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
684
685         ieee80211_rx_napi(priv->hw, NULL, skb, priv->napi);
686 }
687
688 static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
689 {
690         u32 decrypt_out = 0;
691
692         if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
693                                         RX_RES_STATUS_STATION_FOUND)
694                 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
695                                 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
696
697         decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
698
699         /* packet was not encrypted */
700         if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
701                                         RX_RES_STATUS_SEC_TYPE_NONE)
702                 return decrypt_out;
703
704         /* packet was encrypted with unknown alg */
705         if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
706                                         RX_RES_STATUS_SEC_TYPE_ERR)
707                 return decrypt_out;
708
709         /* decryption was not done in HW */
710         if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
711                                         RX_MPDU_RES_STATUS_DEC_DONE_MSK)
712                 return decrypt_out;
713
714         switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
715
716         case RX_RES_STATUS_SEC_TYPE_CCMP:
717                 /* alg is CCM: check MIC only */
718                 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
719                         /* Bad MIC */
720                         decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
721                 else
722                         decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
723
724                 break;
725
726         case RX_RES_STATUS_SEC_TYPE_TKIP:
727                 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
728                         /* Bad TTAK */
729                         decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
730                         break;
731                 }
732                 /* fall through if TTAK OK */
733         default:
734                 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
735                         decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
736                 else
737                         decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
738                 break;
739         }
740
741         IWL_DEBUG_RX(priv, "decrypt_in:0x%x  decrypt_out = 0x%x\n",
742                                         decrypt_in, decrypt_out);
743
744         return decrypt_out;
745 }
746
747 /* Calc max signal level (dBm) among 3 possible receivers */
748 static int iwlagn_calc_rssi(struct iwl_priv *priv,
749                              struct iwl_rx_phy_res *rx_resp)
750 {
751         /* data from PHY/DSP regarding signal strength, etc.,
752          *   contents are always there, not configurable by host
753          */
754         struct iwlagn_non_cfg_phy *ncphy =
755                 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
756         u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
757         u8 agc;
758
759         val  = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
760         agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
761
762         /* Find max rssi among 3 possible receivers.
763          * These values are measured by the digital signal processor (DSP).
764          * They should stay fairly constant even as the signal strength varies,
765          *   if the radio's automatic gain control (AGC) is working right.
766          * AGC value (see below) will provide the "interesting" info.
767          */
768         val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
769         rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
770                 IWLAGN_OFDM_RSSI_A_BIT_POS;
771         rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
772                 IWLAGN_OFDM_RSSI_B_BIT_POS;
773         val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
774         rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
775                 IWLAGN_OFDM_RSSI_C_BIT_POS;
776
777         max_rssi = max_t(u32, rssi_a, rssi_b);
778         max_rssi = max_t(u32, max_rssi, rssi_c);
779
780         IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
781                 rssi_a, rssi_b, rssi_c, max_rssi, agc);
782
783         /* dBm = max_rssi dB - agc dB - constant.
784          * Higher AGC (higher radio gain) means lower signal. */
785         return max_rssi - agc - IWLAGN_RSSI_OFFSET;
786 }
787
788 /* Called for REPLY_RX_MPDU_CMD */
789 static void iwlagn_rx_reply_rx(struct iwl_priv *priv,
790                                struct iwl_rx_cmd_buffer *rxb)
791 {
792         struct ieee80211_hdr *header;
793         struct ieee80211_rx_status rx_status = {};
794         struct iwl_rx_packet *pkt = rxb_addr(rxb);
795         struct iwl_rx_phy_res *phy_res;
796         __le32 rx_pkt_status;
797         struct iwl_rx_mpdu_res_start *amsdu;
798         u32 len;
799         u32 ampdu_status;
800         u32 rate_n_flags;
801
802         if (!priv->last_phy_res_valid) {
803                 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
804                 return;
805         }
806         phy_res = &priv->last_phy_res;
807         amsdu = (struct iwl_rx_mpdu_res_start *)pkt->data;
808         header = (struct ieee80211_hdr *)(pkt->data + sizeof(*amsdu));
809         len = le16_to_cpu(amsdu->byte_count);
810         rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*amsdu) + len);
811         ampdu_status = iwlagn_translate_rx_status(priv,
812                                                   le32_to_cpu(rx_pkt_status));
813
814         if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
815                 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d\n",
816                                 phy_res->cfg_phy_cnt);
817                 return;
818         }
819
820         if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
821             !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
822                 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
823                                 le32_to_cpu(rx_pkt_status));
824                 return;
825         }
826
827         /* This will be used in several places later */
828         rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
829
830         /* rx_status carries information about the packet to mac80211 */
831         rx_status.mactime = le64_to_cpu(phy_res->timestamp);
832         rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
833                                 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
834         rx_status.freq =
835                 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
836                                                rx_status.band);
837         rx_status.rate_idx =
838                 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
839         rx_status.flag = 0;
840
841         /* TSF isn't reliable. In order to allow smooth user experience,
842          * this W/A doesn't propagate it to the mac80211 */
843         /*rx_status.flag |= RX_FLAG_MACTIME_START;*/
844
845         priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
846
847         /* Find max signal strength (dBm) among 3 antenna/receiver chains */
848         rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
849
850         IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
851                 rx_status.signal, (unsigned long long)rx_status.mactime);
852
853         /*
854          * "antenna number"
855          *
856          * It seems that the antenna field in the phy flags value
857          * is actually a bit field. This is undefined by radiotap,
858          * it wants an actual antenna number but I always get "7"
859          * for most legacy frames I receive indicating that the
860          * same frame was received on all three RX chains.
861          *
862          * I think this field should be removed in favor of a
863          * new 802.11n radiotap field "RX chains" that is defined
864          * as a bitmask.
865          */
866         rx_status.antenna =
867                 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
868                 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
869
870         /* set the preamble flag if appropriate */
871         if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
872                 rx_status.enc_flags |= RX_ENC_FLAG_SHORTPRE;
873
874         if (phy_res->phy_flags & RX_RES_PHY_FLAGS_AGG_MSK) {
875                 /*
876                  * We know which subframes of an A-MPDU belong
877                  * together since we get a single PHY response
878                  * from the firmware for all of them
879                  */
880                 rx_status.flag |= RX_FLAG_AMPDU_DETAILS;
881                 rx_status.ampdu_reference = priv->ampdu_ref;
882         }
883
884         /* Set up the HT phy flags */
885         if (rate_n_flags & RATE_MCS_HT_MSK)
886                 rx_status.encoding = RX_ENC_HT;
887         if (rate_n_flags & RATE_MCS_HT40_MSK)
888                 rx_status.bw = RATE_INFO_BW_40;
889         else
890                 rx_status.bw = RATE_INFO_BW_20;
891         if (rate_n_flags & RATE_MCS_SGI_MSK)
892                 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
893         if (rate_n_flags & RATE_MCS_GF_MSK)
894                 rx_status.enc_flags |= RX_ENC_FLAG_HT_GF;
895
896         iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
897                                     rxb, &rx_status);
898 }
899
900 static void iwlagn_rx_noa_notification(struct iwl_priv *priv,
901                                        struct iwl_rx_cmd_buffer *rxb)
902 {
903         struct iwl_wipan_noa_data *new_data, *old_data;
904         struct iwl_rx_packet *pkt = rxb_addr(rxb);
905         struct iwl_wipan_noa_notification *noa_notif = (void *)pkt->data;
906
907         /* no condition -- we're in softirq */
908         old_data = rcu_dereference_protected(priv->noa_data, true);
909
910         if (noa_notif->noa_active) {
911                 u32 len = le16_to_cpu(noa_notif->noa_attribute.length);
912                 u32 copylen = len;
913
914                 /* EID, len, OUI, subtype */
915                 len += 1 + 1 + 3 + 1;
916                 /* P2P id, P2P length */
917                 len += 1 + 2;
918                 copylen += 1 + 2;
919
920                 new_data = kmalloc(sizeof(*new_data) + len, GFP_ATOMIC);
921                 if (new_data) {
922                         new_data->length = len;
923                         new_data->data[0] = WLAN_EID_VENDOR_SPECIFIC;
924                         new_data->data[1] = len - 2; /* not counting EID, len */
925                         new_data->data[2] = (WLAN_OUI_WFA >> 16) & 0xff;
926                         new_data->data[3] = (WLAN_OUI_WFA >> 8) & 0xff;
927                         new_data->data[4] = (WLAN_OUI_WFA >> 0) & 0xff;
928                         new_data->data[5] = WLAN_OUI_TYPE_WFA_P2P;
929                         memcpy(&new_data->data[6], &noa_notif->noa_attribute,
930                                copylen);
931                 }
932         } else
933                 new_data = NULL;
934
935         rcu_assign_pointer(priv->noa_data, new_data);
936
937         if (old_data)
938                 kfree_rcu(old_data, rcu_head);
939 }
940
941 /**
942  * iwl_setup_rx_handlers - Initialize Rx handler callbacks
943  *
944  * Setup the RX handlers for each of the reply types sent from the uCode
945  * to the host.
946  */
947 void iwl_setup_rx_handlers(struct iwl_priv *priv)
948 {
949         void (**handlers)(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb);
950
951         handlers = priv->rx_handlers;
952
953         handlers[REPLY_ERROR]                   = iwlagn_rx_reply_error;
954         handlers[CHANNEL_SWITCH_NOTIFICATION]   = iwlagn_rx_csa;
955         handlers[SPECTRUM_MEASURE_NOTIFICATION] =
956                 iwlagn_rx_spectrum_measure_notif;
957         handlers[PM_SLEEP_NOTIFICATION]         = iwlagn_rx_pm_sleep_notif;
958         handlers[PM_DEBUG_STATISTIC_NOTIFIC]    =
959                 iwlagn_rx_pm_debug_statistics_notif;
960         handlers[BEACON_NOTIFICATION]           = iwlagn_rx_beacon_notif;
961         handlers[REPLY_ADD_STA]                 = iwl_add_sta_callback;
962
963         handlers[REPLY_WIPAN_NOA_NOTIFICATION]  = iwlagn_rx_noa_notification;
964
965         /*
966          * The same handler is used for both the REPLY to a discrete
967          * statistics request from the host as well as for the periodic
968          * statistics notifications (after received beacons) from the uCode.
969          */
970         handlers[REPLY_STATISTICS_CMD]          = iwlagn_rx_reply_statistics;
971         handlers[STATISTICS_NOTIFICATION]       = iwlagn_rx_statistics;
972
973         iwl_setup_rx_scan_handlers(priv);
974
975         handlers[CARD_STATE_NOTIFICATION]       = iwlagn_rx_card_state_notif;
976         handlers[MISSED_BEACONS_NOTIFICATION]   =
977                 iwlagn_rx_missed_beacon_notif;
978
979         /* Rx handlers */
980         handlers[REPLY_RX_PHY_CMD]              = iwlagn_rx_reply_rx_phy;
981         handlers[REPLY_RX_MPDU_CMD]             = iwlagn_rx_reply_rx;
982
983         /* block ack */
984         handlers[REPLY_COMPRESSED_BA]           =
985                 iwlagn_rx_reply_compressed_ba;
986
987         priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
988
989         /* set up notification wait support */
990         iwl_notification_wait_init(&priv->notif_wait);
991
992         /* Set up BT Rx handlers */
993         if (priv->lib->bt_params)
994                 iwlagn_bt_rx_handler_setup(priv);
995 }
996
997 void iwl_rx_dispatch(struct iwl_op_mode *op_mode, struct napi_struct *napi,
998                      struct iwl_rx_cmd_buffer *rxb)
999 {
1000         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1001         struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1002
1003         /*
1004          * Do the notification wait before RX handlers so
1005          * even if the RX handler consumes the RXB we have
1006          * access to it in the notification wait entry.
1007          */
1008         iwl_notification_wait_notify(&priv->notif_wait, pkt);
1009
1010         /* Based on type of command response or notification,
1011          *   handle those that need handling via function in
1012          *   rx_handlers table.  See iwl_setup_rx_handlers() */
1013         if (priv->rx_handlers[pkt->hdr.cmd]) {
1014                 priv->rx_handlers_stats[pkt->hdr.cmd]++;
1015                 priv->rx_handlers[pkt->hdr.cmd](priv, rxb);
1016         } else {
1017                 /* No handling needed */
1018                 IWL_DEBUG_RX(priv, "No handler needed for %s, 0x%02x\n",
1019                              iwl_get_cmd_string(priv->trans,
1020                                                 iwl_cmd_id(pkt->hdr.cmd,
1021                                                            0, 0)),
1022                              pkt->hdr.cmd);
1023         }
1024 }