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