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