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