1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19  * USA
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
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 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/etherdevice.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <net/mac80211.h>
35 
36 #include "iwl-eeprom.h"
37 #include "iwl-debug.h"
38 #include "iwl-core.h"
39 #include "iwl-io.h"
40 #include "iwl-power.h"
41 #include "iwl-shared.h"
42 #include "iwl-agn.h"
43 #include "iwl-trans.h"
44 
45 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
46 
47 #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
48 #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
iwl_init_ht_hw_capab(const struct iwl_priv * priv,struct ieee80211_sta_ht_cap * ht_info,enum ieee80211_band band)49 static void iwl_init_ht_hw_capab(const struct iwl_priv *priv,
50 			      struct ieee80211_sta_ht_cap *ht_info,
51 			      enum ieee80211_band band)
52 {
53 	u16 max_bit_rate = 0;
54 	u8 rx_chains_num = hw_params(priv).rx_chains_num;
55 	u8 tx_chains_num = hw_params(priv).tx_chains_num;
56 
57 	ht_info->cap = 0;
58 	memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
59 
60 	ht_info->ht_supported = true;
61 
62 	if (cfg(priv)->ht_params &&
63 	    cfg(priv)->ht_params->ht_greenfield_support)
64 		ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
65 	ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
66 	max_bit_rate = MAX_BIT_RATE_20_MHZ;
67 	if (hw_params(priv).ht40_channel & BIT(band)) {
68 		ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
69 		ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
70 		ht_info->mcs.rx_mask[4] = 0x01;
71 		max_bit_rate = MAX_BIT_RATE_40_MHZ;
72 	}
73 
74 	if (iwlagn_mod_params.amsdu_size_8K)
75 		ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
76 
77 	ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
78 	ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
79 
80 	ht_info->mcs.rx_mask[0] = 0xFF;
81 	if (rx_chains_num >= 2)
82 		ht_info->mcs.rx_mask[1] = 0xFF;
83 	if (rx_chains_num >= 3)
84 		ht_info->mcs.rx_mask[2] = 0xFF;
85 
86 	/* Highest supported Rx data rate */
87 	max_bit_rate *= rx_chains_num;
88 	WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
89 	ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
90 
91 	/* Tx MCS capabilities */
92 	ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
93 	if (tx_chains_num != rx_chains_num) {
94 		ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
95 		ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
96 				IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
97 	}
98 }
99 
100 /**
101  * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
102  */
iwl_init_geos(struct iwl_priv * priv)103 int iwl_init_geos(struct iwl_priv *priv)
104 {
105 	struct iwl_channel_info *ch;
106 	struct ieee80211_supported_band *sband;
107 	struct ieee80211_channel *channels;
108 	struct ieee80211_channel *geo_ch;
109 	struct ieee80211_rate *rates;
110 	int i = 0;
111 	s8 max_tx_power = IWLAGN_TX_POWER_TARGET_POWER_MIN;
112 
113 	if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
114 	    priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
115 		IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
116 		set_bit(STATUS_GEO_CONFIGURED, &priv->status);
117 		return 0;
118 	}
119 
120 	channels = kcalloc(priv->channel_count,
121 			   sizeof(struct ieee80211_channel), GFP_KERNEL);
122 	if (!channels)
123 		return -ENOMEM;
124 
125 	rates = kcalloc(IWL_RATE_COUNT_LEGACY, sizeof(struct ieee80211_rate),
126 			GFP_KERNEL);
127 	if (!rates) {
128 		kfree(channels);
129 		return -ENOMEM;
130 	}
131 
132 	/* 5.2GHz channels start after the 2.4GHz channels */
133 	sband = &priv->bands[IEEE80211_BAND_5GHZ];
134 	sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
135 	/* just OFDM */
136 	sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
137 	sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
138 
139 	if (hw_params(priv).sku & EEPROM_SKU_CAP_11N_ENABLE)
140 		iwl_init_ht_hw_capab(priv, &sband->ht_cap,
141 					 IEEE80211_BAND_5GHZ);
142 
143 	sband = &priv->bands[IEEE80211_BAND_2GHZ];
144 	sband->channels = channels;
145 	/* OFDM & CCK */
146 	sband->bitrates = rates;
147 	sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
148 
149 	if (hw_params(priv).sku & EEPROM_SKU_CAP_11N_ENABLE)
150 		iwl_init_ht_hw_capab(priv, &sband->ht_cap,
151 					 IEEE80211_BAND_2GHZ);
152 
153 	priv->ieee_channels = channels;
154 	priv->ieee_rates = rates;
155 
156 	for (i = 0;  i < priv->channel_count; i++) {
157 		ch = &priv->channel_info[i];
158 
159 		/* FIXME: might be removed if scan is OK */
160 		if (!is_channel_valid(ch))
161 			continue;
162 
163 		sband =  &priv->bands[ch->band];
164 
165 		geo_ch = &sband->channels[sband->n_channels++];
166 
167 		geo_ch->center_freq =
168 			ieee80211_channel_to_frequency(ch->channel, ch->band);
169 		geo_ch->max_power = ch->max_power_avg;
170 		geo_ch->max_antenna_gain = 0xff;
171 		geo_ch->hw_value = ch->channel;
172 
173 		if (is_channel_valid(ch)) {
174 			if (!(ch->flags & EEPROM_CHANNEL_IBSS))
175 				geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
176 
177 			if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
178 				geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
179 
180 			if (ch->flags & EEPROM_CHANNEL_RADAR)
181 				geo_ch->flags |= IEEE80211_CHAN_RADAR;
182 
183 			geo_ch->flags |= ch->ht40_extension_channel;
184 
185 			if (ch->max_power_avg > max_tx_power)
186 				max_tx_power = ch->max_power_avg;
187 		} else {
188 			geo_ch->flags |= IEEE80211_CHAN_DISABLED;
189 		}
190 
191 		IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
192 				ch->channel, geo_ch->center_freq,
193 				is_channel_a_band(ch) ?  "5.2" : "2.4",
194 				geo_ch->flags & IEEE80211_CHAN_DISABLED ?
195 				"restricted" : "valid",
196 				 geo_ch->flags);
197 	}
198 
199 	priv->tx_power_device_lmt = max_tx_power;
200 	priv->tx_power_user_lmt = max_tx_power;
201 	priv->tx_power_next = max_tx_power;
202 
203 	if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
204 	     hw_params(priv).sku & EEPROM_SKU_CAP_BAND_52GHZ) {
205 		IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
206 			"Please send your %s to maintainer.\n",
207 			trans(priv)->hw_id_str);
208 		hw_params(priv).sku &= ~EEPROM_SKU_CAP_BAND_52GHZ;
209 	}
210 
211 	IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
212 		   priv->bands[IEEE80211_BAND_2GHZ].n_channels,
213 		   priv->bands[IEEE80211_BAND_5GHZ].n_channels);
214 
215 	set_bit(STATUS_GEO_CONFIGURED, &priv->status);
216 
217 	return 0;
218 }
219 
220 /*
221  * iwl_free_geos - undo allocations in iwl_init_geos
222  */
iwl_free_geos(struct iwl_priv * priv)223 void iwl_free_geos(struct iwl_priv *priv)
224 {
225 	kfree(priv->ieee_channels);
226 	kfree(priv->ieee_rates);
227 	clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
228 }
229 
iwl_is_channel_extension(struct iwl_priv * priv,enum ieee80211_band band,u16 channel,u8 extension_chan_offset)230 static bool iwl_is_channel_extension(struct iwl_priv *priv,
231 				     enum ieee80211_band band,
232 				     u16 channel, u8 extension_chan_offset)
233 {
234 	const struct iwl_channel_info *ch_info;
235 
236 	ch_info = iwl_get_channel_info(priv, band, channel);
237 	if (!is_channel_valid(ch_info))
238 		return false;
239 
240 	if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
241 		return !(ch_info->ht40_extension_channel &
242 					IEEE80211_CHAN_NO_HT40PLUS);
243 	else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
244 		return !(ch_info->ht40_extension_channel &
245 					IEEE80211_CHAN_NO_HT40MINUS);
246 
247 	return false;
248 }
249 
iwl_is_ht40_tx_allowed(struct iwl_priv * priv,struct iwl_rxon_context * ctx,struct ieee80211_sta_ht_cap * ht_cap)250 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
251 			    struct iwl_rxon_context *ctx,
252 			    struct ieee80211_sta_ht_cap *ht_cap)
253 {
254 	if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
255 		return false;
256 
257 	/*
258 	 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
259 	 * the bit will not set if it is pure 40MHz case
260 	 */
261 	if (ht_cap && !ht_cap->ht_supported)
262 		return false;
263 
264 #ifdef CONFIG_IWLWIFI_DEBUGFS
265 	if (priv->disable_ht40)
266 		return false;
267 #endif
268 
269 	return iwl_is_channel_extension(priv, priv->band,
270 			le16_to_cpu(ctx->staging.channel),
271 			ctx->ht.extension_chan_offset);
272 }
273 
iwl_adjust_beacon_interval(u16 beacon_val,u16 max_beacon_val)274 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
275 {
276 	u16 new_val;
277 	u16 beacon_factor;
278 
279 	/*
280 	 * If mac80211 hasn't given us a beacon interval, program
281 	 * the default into the device (not checking this here
282 	 * would cause the adjustment below to return the maximum
283 	 * value, which may break PAN.)
284 	 */
285 	if (!beacon_val)
286 		return DEFAULT_BEACON_INTERVAL;
287 
288 	/*
289 	 * If the beacon interval we obtained from the peer
290 	 * is too large, we'll have to wake up more often
291 	 * (and in IBSS case, we'll beacon too much)
292 	 *
293 	 * For example, if max_beacon_val is 4096, and the
294 	 * requested beacon interval is 7000, we'll have to
295 	 * use 3500 to be able to wake up on the beacons.
296 	 *
297 	 * This could badly influence beacon detection stats.
298 	 */
299 
300 	beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
301 	new_val = beacon_val / beacon_factor;
302 
303 	if (!new_val)
304 		new_val = max_beacon_val;
305 
306 	return new_val;
307 }
308 
iwl_send_rxon_timing(struct iwl_priv * priv,struct iwl_rxon_context * ctx)309 int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
310 {
311 	u64 tsf;
312 	s32 interval_tm, rem;
313 	struct ieee80211_conf *conf = NULL;
314 	u16 beacon_int;
315 	struct ieee80211_vif *vif = ctx->vif;
316 
317 	conf = &priv->hw->conf;
318 
319 	lockdep_assert_held(&priv->mutex);
320 
321 	memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
322 
323 	ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
324 	ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
325 
326 	beacon_int = vif ? vif->bss_conf.beacon_int : 0;
327 
328 	/*
329 	 * TODO: For IBSS we need to get atim_window from mac80211,
330 	 *	 for now just always use 0
331 	 */
332 	ctx->timing.atim_window = 0;
333 
334 	if (ctx->ctxid == IWL_RXON_CTX_PAN &&
335 	    (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
336 	    iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
337 	    priv->contexts[IWL_RXON_CTX_BSS].vif &&
338 	    priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
339 		ctx->timing.beacon_interval =
340 			priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
341 		beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
342 	} else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
343 		   iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
344 		   priv->contexts[IWL_RXON_CTX_PAN].vif &&
345 		   priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
346 		   (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
347 		    !ctx->vif->bss_conf.beacon_int)) {
348 		ctx->timing.beacon_interval =
349 			priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
350 		beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
351 	} else {
352 		beacon_int = iwl_adjust_beacon_interval(beacon_int,
353 			IWL_MAX_UCODE_BEACON_INTERVAL * TIME_UNIT);
354 		ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
355 	}
356 
357 	ctx->beacon_int = beacon_int;
358 
359 	tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
360 	interval_tm = beacon_int * TIME_UNIT;
361 	rem = do_div(tsf, interval_tm);
362 	ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
363 
364 	ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
365 
366 	IWL_DEBUG_ASSOC(priv,
367 			"beacon interval %d beacon timer %d beacon tim %d\n",
368 			le16_to_cpu(ctx->timing.beacon_interval),
369 			le32_to_cpu(ctx->timing.beacon_init_val),
370 			le16_to_cpu(ctx->timing.atim_window));
371 
372 	return iwl_dvm_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
373 				CMD_SYNC, sizeof(ctx->timing), &ctx->timing);
374 }
375 
iwl_set_rxon_hwcrypto(struct iwl_priv * priv,struct iwl_rxon_context * ctx,int hw_decrypt)376 void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
377 			   int hw_decrypt)
378 {
379 	struct iwl_rxon_cmd *rxon = &ctx->staging;
380 
381 	if (hw_decrypt)
382 		rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
383 	else
384 		rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
385 
386 }
387 
388 /* validate RXON structure is valid */
iwl_check_rxon_cmd(struct iwl_priv * priv,struct iwl_rxon_context * ctx)389 int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
390 {
391 	struct iwl_rxon_cmd *rxon = &ctx->staging;
392 	u32 errors = 0;
393 
394 	if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
395 		if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
396 			IWL_WARN(priv, "check 2.4G: wrong narrow\n");
397 			errors |= BIT(0);
398 		}
399 		if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
400 			IWL_WARN(priv, "check 2.4G: wrong radar\n");
401 			errors |= BIT(1);
402 		}
403 	} else {
404 		if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
405 			IWL_WARN(priv, "check 5.2G: not short slot!\n");
406 			errors |= BIT(2);
407 		}
408 		if (rxon->flags & RXON_FLG_CCK_MSK) {
409 			IWL_WARN(priv, "check 5.2G: CCK!\n");
410 			errors |= BIT(3);
411 		}
412 	}
413 	if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
414 		IWL_WARN(priv, "mac/bssid mcast!\n");
415 		errors |= BIT(4);
416 	}
417 
418 	/* make sure basic rates 6Mbps and 1Mbps are supported */
419 	if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
420 	    (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
421 		IWL_WARN(priv, "neither 1 nor 6 are basic\n");
422 		errors |= BIT(5);
423 	}
424 
425 	if (le16_to_cpu(rxon->assoc_id) > 2007) {
426 		IWL_WARN(priv, "aid > 2007\n");
427 		errors |= BIT(6);
428 	}
429 
430 	if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
431 			== (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
432 		IWL_WARN(priv, "CCK and short slot\n");
433 		errors |= BIT(7);
434 	}
435 
436 	if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
437 			== (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
438 		IWL_WARN(priv, "CCK and auto detect");
439 		errors |= BIT(8);
440 	}
441 
442 	if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
443 			    RXON_FLG_TGG_PROTECT_MSK)) ==
444 			    RXON_FLG_TGG_PROTECT_MSK) {
445 		IWL_WARN(priv, "TGg but no auto-detect\n");
446 		errors |= BIT(9);
447 	}
448 
449 	if (rxon->channel == 0) {
450 		IWL_WARN(priv, "zero channel is invalid\n");
451 		errors |= BIT(10);
452 	}
453 
454 	WARN(errors, "Invalid RXON (%#x), channel %d",
455 	     errors, le16_to_cpu(rxon->channel));
456 
457 	return errors ? -EINVAL : 0;
458 }
459 
460 /**
461  * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
462  * @priv: staging_rxon is compared to active_rxon
463  *
464  * If the RXON structure is changing enough to require a new tune,
465  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
466  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
467  */
iwl_full_rxon_required(struct iwl_priv * priv,struct iwl_rxon_context * ctx)468 int iwl_full_rxon_required(struct iwl_priv *priv,
469 			   struct iwl_rxon_context *ctx)
470 {
471 	const struct iwl_rxon_cmd *staging = &ctx->staging;
472 	const struct iwl_rxon_cmd *active = &ctx->active;
473 
474 #define CHK(cond)							\
475 	if ((cond)) {							\
476 		IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");	\
477 		return 1;						\
478 	}
479 
480 #define CHK_NEQ(c1, c2)						\
481 	if ((c1) != (c2)) {					\
482 		IWL_DEBUG_INFO(priv, "need full RXON - "	\
483 			       #c1 " != " #c2 " - %d != %d\n",	\
484 			       (c1), (c2));			\
485 		return 1;					\
486 	}
487 
488 	/* These items are only settable from the full RXON command */
489 	CHK(!iwl_is_associated_ctx(ctx));
490 	CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
491 	CHK(compare_ether_addr(staging->node_addr, active->node_addr));
492 	CHK(compare_ether_addr(staging->wlap_bssid_addr,
493 				active->wlap_bssid_addr));
494 	CHK_NEQ(staging->dev_type, active->dev_type);
495 	CHK_NEQ(staging->channel, active->channel);
496 	CHK_NEQ(staging->air_propagation, active->air_propagation);
497 	CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
498 		active->ofdm_ht_single_stream_basic_rates);
499 	CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
500 		active->ofdm_ht_dual_stream_basic_rates);
501 	CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
502 		active->ofdm_ht_triple_stream_basic_rates);
503 	CHK_NEQ(staging->assoc_id, active->assoc_id);
504 
505 	/* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
506 	 * be updated with the RXON_ASSOC command -- however only some
507 	 * flag transitions are allowed using RXON_ASSOC */
508 
509 	/* Check if we are not switching bands */
510 	CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
511 		active->flags & RXON_FLG_BAND_24G_MSK);
512 
513 	/* Check if we are switching association toggle */
514 	CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
515 		active->filter_flags & RXON_FILTER_ASSOC_MSK);
516 
517 #undef CHK
518 #undef CHK_NEQ
519 
520 	return 0;
521 }
522 
_iwl_set_rxon_ht(struct iwl_priv * priv,struct iwl_ht_config * ht_conf,struct iwl_rxon_context * ctx)523 static void _iwl_set_rxon_ht(struct iwl_priv *priv,
524 			     struct iwl_ht_config *ht_conf,
525 			     struct iwl_rxon_context *ctx)
526 {
527 	struct iwl_rxon_cmd *rxon = &ctx->staging;
528 
529 	if (!ctx->ht.enabled) {
530 		rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
531 			RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
532 			RXON_FLG_HT40_PROT_MSK |
533 			RXON_FLG_HT_PROT_MSK);
534 		return;
535 	}
536 
537 	/* FIXME: if the definition of ht.protection changed, the "translation"
538 	 * will be needed for rxon->flags
539 	 */
540 	rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
541 
542 	/* Set up channel bandwidth:
543 	 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
544 	/* clear the HT channel mode before set the mode */
545 	rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
546 			 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
547 	if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
548 		/* pure ht40 */
549 		if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
550 			rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
551 			/* Note: control channel is opposite of extension channel */
552 			switch (ctx->ht.extension_chan_offset) {
553 			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
554 				rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
555 				break;
556 			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
557 				rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
558 				break;
559 			}
560 		} else {
561 			/* Note: control channel is opposite of extension channel */
562 			switch (ctx->ht.extension_chan_offset) {
563 			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
564 				rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
565 				rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
566 				break;
567 			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
568 				rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
569 				rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
570 				break;
571 			case IEEE80211_HT_PARAM_CHA_SEC_NONE:
572 			default:
573 				/* channel location only valid if in Mixed mode */
574 				IWL_ERR(priv, "invalid extension channel offset\n");
575 				break;
576 			}
577 		}
578 	} else {
579 		rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
580 	}
581 
582 	iwlagn_set_rxon_chain(priv, ctx);
583 
584 	IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
585 			"extension channel offset 0x%x\n",
586 			le32_to_cpu(rxon->flags), ctx->ht.protection,
587 			ctx->ht.extension_chan_offset);
588 }
589 
iwl_set_rxon_ht(struct iwl_priv * priv,struct iwl_ht_config * ht_conf)590 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
591 {
592 	struct iwl_rxon_context *ctx;
593 
594 	for_each_context(priv, ctx)
595 		_iwl_set_rxon_ht(priv, ht_conf, ctx);
596 }
597 
598 /* Return valid, unused, channel for a passive scan to reset the RF */
iwl_get_single_channel_number(struct iwl_priv * priv,enum ieee80211_band band)599 u8 iwl_get_single_channel_number(struct iwl_priv *priv,
600 				 enum ieee80211_band band)
601 {
602 	const struct iwl_channel_info *ch_info;
603 	int i;
604 	u8 channel = 0;
605 	u8 min, max;
606 	struct iwl_rxon_context *ctx;
607 
608 	if (band == IEEE80211_BAND_5GHZ) {
609 		min = 14;
610 		max = priv->channel_count;
611 	} else {
612 		min = 0;
613 		max = 14;
614 	}
615 
616 	for (i = min; i < max; i++) {
617 		bool busy = false;
618 
619 		for_each_context(priv, ctx) {
620 			busy = priv->channel_info[i].channel ==
621 				le16_to_cpu(ctx->staging.channel);
622 			if (busy)
623 				break;
624 		}
625 
626 		if (busy)
627 			continue;
628 
629 		channel = priv->channel_info[i].channel;
630 		ch_info = iwl_get_channel_info(priv, band, channel);
631 		if (is_channel_valid(ch_info))
632 			break;
633 	}
634 
635 	return channel;
636 }
637 
638 /**
639  * iwl_set_rxon_channel - Set the band and channel values in staging RXON
640  * @ch: requested channel as a pointer to struct ieee80211_channel
641 
642  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
643  * in the staging RXON flag structure based on the ch->band
644  */
iwl_set_rxon_channel(struct iwl_priv * priv,struct ieee80211_channel * ch,struct iwl_rxon_context * ctx)645 void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
646 			 struct iwl_rxon_context *ctx)
647 {
648 	enum ieee80211_band band = ch->band;
649 	u16 channel = ch->hw_value;
650 
651 	if ((le16_to_cpu(ctx->staging.channel) == channel) &&
652 	    (priv->band == band))
653 		return;
654 
655 	ctx->staging.channel = cpu_to_le16(channel);
656 	if (band == IEEE80211_BAND_5GHZ)
657 		ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
658 	else
659 		ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
660 
661 	priv->band = band;
662 
663 	IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
664 
665 }
666 
iwl_set_flags_for_band(struct iwl_priv * priv,struct iwl_rxon_context * ctx,enum ieee80211_band band,struct ieee80211_vif * vif)667 void iwl_set_flags_for_band(struct iwl_priv *priv,
668 			    struct iwl_rxon_context *ctx,
669 			    enum ieee80211_band band,
670 			    struct ieee80211_vif *vif)
671 {
672 	if (band == IEEE80211_BAND_5GHZ) {
673 		ctx->staging.flags &=
674 		    ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
675 		      | RXON_FLG_CCK_MSK);
676 		ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
677 	} else {
678 		/* Copied from iwl_post_associate() */
679 		if (vif && vif->bss_conf.use_short_slot)
680 			ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
681 		else
682 			ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
683 
684 		ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
685 		ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
686 		ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
687 	}
688 }
689 
690 /*
691  * initialize rxon structure with default values from eeprom
692  */
iwl_connection_init_rx_config(struct iwl_priv * priv,struct iwl_rxon_context * ctx)693 void iwl_connection_init_rx_config(struct iwl_priv *priv,
694 				   struct iwl_rxon_context *ctx)
695 {
696 	const struct iwl_channel_info *ch_info;
697 
698 	memset(&ctx->staging, 0, sizeof(ctx->staging));
699 
700 	if (!ctx->vif) {
701 		ctx->staging.dev_type = ctx->unused_devtype;
702 	} else switch (ctx->vif->type) {
703 	case NL80211_IFTYPE_AP:
704 		ctx->staging.dev_type = ctx->ap_devtype;
705 		break;
706 
707 	case NL80211_IFTYPE_STATION:
708 		ctx->staging.dev_type = ctx->station_devtype;
709 		ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
710 		break;
711 
712 	case NL80211_IFTYPE_ADHOC:
713 		ctx->staging.dev_type = ctx->ibss_devtype;
714 		ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
715 		ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
716 						  RXON_FILTER_ACCEPT_GRP_MSK;
717 		break;
718 
719 	default:
720 		IWL_ERR(priv, "Unsupported interface type %d\n",
721 			ctx->vif->type);
722 		break;
723 	}
724 
725 #if 0
726 	/* TODO:  Figure out when short_preamble would be set and cache from
727 	 * that */
728 	if (!hw_to_local(priv->hw)->short_preamble)
729 		ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
730 	else
731 		ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
732 #endif
733 
734 	ch_info = iwl_get_channel_info(priv, priv->band,
735 				       le16_to_cpu(ctx->active.channel));
736 
737 	if (!ch_info)
738 		ch_info = &priv->channel_info[0];
739 
740 	ctx->staging.channel = cpu_to_le16(ch_info->channel);
741 	priv->band = ch_info->band;
742 
743 	iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
744 
745 	ctx->staging.ofdm_basic_rates =
746 	    (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
747 	ctx->staging.cck_basic_rates =
748 	    (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
749 
750 	/* clear both MIX and PURE40 mode flag */
751 	ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
752 					RXON_FLG_CHANNEL_MODE_PURE_40);
753 	if (ctx->vif)
754 		memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
755 
756 	ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
757 	ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
758 	ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
759 }
760 
iwl_set_rate(struct iwl_priv * priv)761 void iwl_set_rate(struct iwl_priv *priv)
762 {
763 	const struct ieee80211_supported_band *hw = NULL;
764 	struct ieee80211_rate *rate;
765 	struct iwl_rxon_context *ctx;
766 	int i;
767 
768 	hw = iwl_get_hw_mode(priv, priv->band);
769 	if (!hw) {
770 		IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
771 		return;
772 	}
773 
774 	priv->active_rate = 0;
775 
776 	for (i = 0; i < hw->n_bitrates; i++) {
777 		rate = &(hw->bitrates[i]);
778 		if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
779 			priv->active_rate |= (1 << rate->hw_value);
780 	}
781 
782 	IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
783 
784 	for_each_context(priv, ctx) {
785 		ctx->staging.cck_basic_rates =
786 		    (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
787 
788 		ctx->staging.ofdm_basic_rates =
789 		   (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
790 	}
791 }
792 
iwl_chswitch_done(struct iwl_priv * priv,bool is_success)793 void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
794 {
795 	/*
796 	 * MULTI-FIXME
797 	 * See iwlagn_mac_channel_switch.
798 	 */
799 	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
800 
801 	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
802 		return;
803 
804 	if (!test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
805 		return;
806 
807 	if (ctx->vif)
808 		ieee80211_chswitch_done(ctx->vif, is_success);
809 }
810 
811 #ifdef CONFIG_IWLWIFI_DEBUG
iwl_print_rx_config_cmd(struct iwl_priv * priv,enum iwl_rxon_context_id ctxid)812 void iwl_print_rx_config_cmd(struct iwl_priv *priv,
813 			     enum iwl_rxon_context_id ctxid)
814 {
815 	struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
816 	struct iwl_rxon_cmd *rxon = &ctx->staging;
817 
818 	IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
819 	iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
820 	IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
821 	IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
822 	IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
823 			le32_to_cpu(rxon->filter_flags));
824 	IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
825 	IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
826 			rxon->ofdm_basic_rates);
827 	IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
828 	IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
829 	IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
830 	IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
831 }
832 #endif
833 
iwlagn_fw_error(struct iwl_priv * priv,bool ondemand)834 static void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
835 {
836 	unsigned int reload_msec;
837 	unsigned long reload_jiffies;
838 
839 #ifdef CONFIG_IWLWIFI_DEBUG
840 	if (iwl_have_debug_level(IWL_DL_FW_ERRORS))
841 		iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
842 #endif
843 
844 	/* uCode is no longer loaded. */
845 	priv->ucode_loaded = false;
846 
847 	/* Set the FW error flag -- cleared on iwl_down */
848 	set_bit(STATUS_FW_ERROR, &priv->shrd->status);
849 
850 	/* Cancel currently queued command. */
851 	clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
852 
853 	iwl_abort_notification_waits(&priv->notif_wait);
854 
855 	/* Keep the restart process from trying to send host
856 	 * commands by clearing the ready bit */
857 	clear_bit(STATUS_READY, &priv->status);
858 
859 	wake_up(&trans(priv)->wait_command_queue);
860 
861 	if (!ondemand) {
862 		/*
863 		 * If firmware keep reloading, then it indicate something
864 		 * serious wrong and firmware having problem to recover
865 		 * from it. Instead of keep trying which will fill the syslog
866 		 * and hang the system, let's just stop it
867 		 */
868 		reload_jiffies = jiffies;
869 		reload_msec = jiffies_to_msecs((long) reload_jiffies -
870 					(long) priv->reload_jiffies);
871 		priv->reload_jiffies = reload_jiffies;
872 		if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
873 			priv->reload_count++;
874 			if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
875 				IWL_ERR(priv, "BUG_ON, Stop restarting\n");
876 				return;
877 			}
878 		} else
879 			priv->reload_count = 0;
880 	}
881 
882 	if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
883 		if (iwlagn_mod_params.restart_fw) {
884 			IWL_DEBUG_FW_ERRORS(priv,
885 				  "Restarting adapter due to uCode error.\n");
886 			queue_work(priv->workqueue, &priv->restart);
887 		} else
888 			IWL_DEBUG_FW_ERRORS(priv,
889 				  "Detected FW error, but not restarting\n");
890 	}
891 }
892 
iwl_set_tx_power(struct iwl_priv * priv,s8 tx_power,bool force)893 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
894 {
895 	int ret;
896 	s8 prev_tx_power;
897 	bool defer;
898 	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
899 
900 	lockdep_assert_held(&priv->mutex);
901 
902 	if (priv->tx_power_user_lmt == tx_power && !force)
903 		return 0;
904 
905 	if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
906 		IWL_WARN(priv,
907 			 "Requested user TXPOWER %d below lower limit %d.\n",
908 			 tx_power,
909 			 IWLAGN_TX_POWER_TARGET_POWER_MIN);
910 		return -EINVAL;
911 	}
912 
913 	if (tx_power > priv->tx_power_device_lmt) {
914 		IWL_WARN(priv,
915 			"Requested user TXPOWER %d above upper limit %d.\n",
916 			 tx_power, priv->tx_power_device_lmt);
917 		return -EINVAL;
918 	}
919 
920 	if (!iwl_is_ready_rf(priv))
921 		return -EIO;
922 
923 	/* scan complete and commit_rxon use tx_power_next value,
924 	 * it always need to be updated for newest request */
925 	priv->tx_power_next = tx_power;
926 
927 	/* do not set tx power when scanning or channel changing */
928 	defer = test_bit(STATUS_SCANNING, &priv->status) ||
929 		memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
930 	if (defer && !force) {
931 		IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
932 		return 0;
933 	}
934 
935 	prev_tx_power = priv->tx_power_user_lmt;
936 	priv->tx_power_user_lmt = tx_power;
937 
938 	ret = iwlagn_send_tx_power(priv);
939 
940 	/* if fail to set tx_power, restore the orig. tx power */
941 	if (ret) {
942 		priv->tx_power_user_lmt = prev_tx_power;
943 		priv->tx_power_next = prev_tx_power;
944 	}
945 	return ret;
946 }
947 
iwl_send_bt_config(struct iwl_priv * priv)948 void iwl_send_bt_config(struct iwl_priv *priv)
949 {
950 	struct iwl_bt_cmd bt_cmd = {
951 		.lead_time = BT_LEAD_TIME_DEF,
952 		.max_kill = BT_MAX_KILL_DEF,
953 		.kill_ack_mask = 0,
954 		.kill_cts_mask = 0,
955 	};
956 
957 	if (!iwlagn_mod_params.bt_coex_active)
958 		bt_cmd.flags = BT_COEX_DISABLE;
959 	else
960 		bt_cmd.flags = BT_COEX_ENABLE;
961 
962 	priv->bt_enable_flag = bt_cmd.flags;
963 	IWL_DEBUG_INFO(priv, "BT coex %s\n",
964 		(bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
965 
966 	if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
967 			     CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
968 		IWL_ERR(priv, "failed to send BT Coex Config\n");
969 }
970 
iwl_send_statistics_request(struct iwl_priv * priv,u8 flags,bool clear)971 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
972 {
973 	struct iwl_statistics_cmd statistics_cmd = {
974 		.configuration_flags =
975 			clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
976 	};
977 
978 	if (flags & CMD_ASYNC)
979 		return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
980 					      CMD_ASYNC,
981 					       sizeof(struct iwl_statistics_cmd),
982 					       &statistics_cmd);
983 	else
984 		return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
985 					CMD_SYNC,
986 					sizeof(struct iwl_statistics_cmd),
987 					&statistics_cmd);
988 }
989 
990 
991 
992 
993 #ifdef CONFIG_IWLWIFI_DEBUGFS
994 
995 #define IWL_TRAFFIC_DUMP_SIZE	(IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
996 
iwl_reset_traffic_log(struct iwl_priv * priv)997 void iwl_reset_traffic_log(struct iwl_priv *priv)
998 {
999 	priv->tx_traffic_idx = 0;
1000 	priv->rx_traffic_idx = 0;
1001 	if (priv->tx_traffic)
1002 		memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1003 	if (priv->rx_traffic)
1004 		memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1005 }
1006 
iwl_alloc_traffic_mem(struct iwl_priv * priv)1007 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
1008 {
1009 	u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
1010 
1011 	if (iwl_have_debug_level(IWL_DL_TX)) {
1012 		if (!priv->tx_traffic) {
1013 			priv->tx_traffic =
1014 				kzalloc(traffic_size, GFP_KERNEL);
1015 			if (!priv->tx_traffic)
1016 				return -ENOMEM;
1017 		}
1018 	}
1019 	if (iwl_have_debug_level(IWL_DL_RX)) {
1020 		if (!priv->rx_traffic) {
1021 			priv->rx_traffic =
1022 				kzalloc(traffic_size, GFP_KERNEL);
1023 			if (!priv->rx_traffic)
1024 				return -ENOMEM;
1025 		}
1026 	}
1027 	iwl_reset_traffic_log(priv);
1028 	return 0;
1029 }
1030 
iwl_free_traffic_mem(struct iwl_priv * priv)1031 void iwl_free_traffic_mem(struct iwl_priv *priv)
1032 {
1033 	kfree(priv->tx_traffic);
1034 	priv->tx_traffic = NULL;
1035 
1036 	kfree(priv->rx_traffic);
1037 	priv->rx_traffic = NULL;
1038 }
1039 
iwl_dbg_log_tx_data_frame(struct iwl_priv * priv,u16 length,struct ieee80211_hdr * header)1040 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
1041 		      u16 length, struct ieee80211_hdr *header)
1042 {
1043 	__le16 fc;
1044 	u16 len;
1045 
1046 	if (likely(!iwl_have_debug_level(IWL_DL_TX)))
1047 		return;
1048 
1049 	if (!priv->tx_traffic)
1050 		return;
1051 
1052 	fc = header->frame_control;
1053 	if (ieee80211_is_data(fc)) {
1054 		len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1055 		       ? IWL_TRAFFIC_ENTRY_SIZE : length;
1056 		memcpy((priv->tx_traffic +
1057 		       (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1058 		       header, len);
1059 		priv->tx_traffic_idx =
1060 			(priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1061 	}
1062 }
1063 
iwl_dbg_log_rx_data_frame(struct iwl_priv * priv,u16 length,struct ieee80211_hdr * header)1064 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
1065 		      u16 length, struct ieee80211_hdr *header)
1066 {
1067 	__le16 fc;
1068 	u16 len;
1069 
1070 	if (likely(!iwl_have_debug_level(IWL_DL_RX)))
1071 		return;
1072 
1073 	if (!priv->rx_traffic)
1074 		return;
1075 
1076 	fc = header->frame_control;
1077 	if (ieee80211_is_data(fc)) {
1078 		len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1079 		       ? IWL_TRAFFIC_ENTRY_SIZE : length;
1080 		memcpy((priv->rx_traffic +
1081 		       (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1082 		       header, len);
1083 		priv->rx_traffic_idx =
1084 			(priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1085 	}
1086 }
1087 
get_mgmt_string(int cmd)1088 const char *get_mgmt_string(int cmd)
1089 {
1090 	switch (cmd) {
1091 		IWL_CMD(MANAGEMENT_ASSOC_REQ);
1092 		IWL_CMD(MANAGEMENT_ASSOC_RESP);
1093 		IWL_CMD(MANAGEMENT_REASSOC_REQ);
1094 		IWL_CMD(MANAGEMENT_REASSOC_RESP);
1095 		IWL_CMD(MANAGEMENT_PROBE_REQ);
1096 		IWL_CMD(MANAGEMENT_PROBE_RESP);
1097 		IWL_CMD(MANAGEMENT_BEACON);
1098 		IWL_CMD(MANAGEMENT_ATIM);
1099 		IWL_CMD(MANAGEMENT_DISASSOC);
1100 		IWL_CMD(MANAGEMENT_AUTH);
1101 		IWL_CMD(MANAGEMENT_DEAUTH);
1102 		IWL_CMD(MANAGEMENT_ACTION);
1103 	default:
1104 		return "UNKNOWN";
1105 
1106 	}
1107 }
1108 
get_ctrl_string(int cmd)1109 const char *get_ctrl_string(int cmd)
1110 {
1111 	switch (cmd) {
1112 		IWL_CMD(CONTROL_BACK_REQ);
1113 		IWL_CMD(CONTROL_BACK);
1114 		IWL_CMD(CONTROL_PSPOLL);
1115 		IWL_CMD(CONTROL_RTS);
1116 		IWL_CMD(CONTROL_CTS);
1117 		IWL_CMD(CONTROL_ACK);
1118 		IWL_CMD(CONTROL_CFEND);
1119 		IWL_CMD(CONTROL_CFENDACK);
1120 	default:
1121 		return "UNKNOWN";
1122 
1123 	}
1124 }
1125 
iwl_clear_traffic_stats(struct iwl_priv * priv)1126 void iwl_clear_traffic_stats(struct iwl_priv *priv)
1127 {
1128 	memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
1129 	memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
1130 }
1131 
1132 /*
1133  * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
1134  * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
1135  * Use debugFs to display the rx/rx_statistics
1136  * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
1137  * information will be recorded, but DATA pkt still will be recorded
1138  * for the reason of iwl_led.c need to control the led blinking based on
1139  * number of tx and rx data.
1140  *
1141  */
iwl_update_stats(struct iwl_priv * priv,bool is_tx,__le16 fc,u16 len)1142 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
1143 {
1144 	struct traffic_stats	*stats;
1145 
1146 	if (is_tx)
1147 		stats = &priv->tx_stats;
1148 	else
1149 		stats = &priv->rx_stats;
1150 
1151 	if (ieee80211_is_mgmt(fc)) {
1152 		switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1153 		case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
1154 			stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
1155 			break;
1156 		case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
1157 			stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
1158 			break;
1159 		case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
1160 			stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
1161 			break;
1162 		case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
1163 			stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
1164 			break;
1165 		case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
1166 			stats->mgmt[MANAGEMENT_PROBE_REQ]++;
1167 			break;
1168 		case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
1169 			stats->mgmt[MANAGEMENT_PROBE_RESP]++;
1170 			break;
1171 		case cpu_to_le16(IEEE80211_STYPE_BEACON):
1172 			stats->mgmt[MANAGEMENT_BEACON]++;
1173 			break;
1174 		case cpu_to_le16(IEEE80211_STYPE_ATIM):
1175 			stats->mgmt[MANAGEMENT_ATIM]++;
1176 			break;
1177 		case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
1178 			stats->mgmt[MANAGEMENT_DISASSOC]++;
1179 			break;
1180 		case cpu_to_le16(IEEE80211_STYPE_AUTH):
1181 			stats->mgmt[MANAGEMENT_AUTH]++;
1182 			break;
1183 		case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
1184 			stats->mgmt[MANAGEMENT_DEAUTH]++;
1185 			break;
1186 		case cpu_to_le16(IEEE80211_STYPE_ACTION):
1187 			stats->mgmt[MANAGEMENT_ACTION]++;
1188 			break;
1189 		}
1190 	} else if (ieee80211_is_ctl(fc)) {
1191 		switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1192 		case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
1193 			stats->ctrl[CONTROL_BACK_REQ]++;
1194 			break;
1195 		case cpu_to_le16(IEEE80211_STYPE_BACK):
1196 			stats->ctrl[CONTROL_BACK]++;
1197 			break;
1198 		case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
1199 			stats->ctrl[CONTROL_PSPOLL]++;
1200 			break;
1201 		case cpu_to_le16(IEEE80211_STYPE_RTS):
1202 			stats->ctrl[CONTROL_RTS]++;
1203 			break;
1204 		case cpu_to_le16(IEEE80211_STYPE_CTS):
1205 			stats->ctrl[CONTROL_CTS]++;
1206 			break;
1207 		case cpu_to_le16(IEEE80211_STYPE_ACK):
1208 			stats->ctrl[CONTROL_ACK]++;
1209 			break;
1210 		case cpu_to_le16(IEEE80211_STYPE_CFEND):
1211 			stats->ctrl[CONTROL_CFEND]++;
1212 			break;
1213 		case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
1214 			stats->ctrl[CONTROL_CFENDACK]++;
1215 			break;
1216 		}
1217 	} else {
1218 		/* data */
1219 		stats->data_cnt++;
1220 		stats->data_bytes += len;
1221 	}
1222 }
1223 #endif
1224 
iwl_force_rf_reset(struct iwl_priv * priv)1225 static void iwl_force_rf_reset(struct iwl_priv *priv)
1226 {
1227 	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1228 		return;
1229 
1230 	if (!iwl_is_any_associated(priv)) {
1231 		IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
1232 		return;
1233 	}
1234 	/*
1235 	 * There is no easy and better way to force reset the radio,
1236 	 * the only known method is switching channel which will force to
1237 	 * reset and tune the radio.
1238 	 * Use internal short scan (single channel) operation to should
1239 	 * achieve this objective.
1240 	 * Driver should reset the radio when number of consecutive missed
1241 	 * beacon, or any other uCode error condition detected.
1242 	 */
1243 	IWL_DEBUG_INFO(priv, "perform radio reset.\n");
1244 	iwl_internal_short_hw_scan(priv);
1245 }
1246 
1247 
iwl_force_reset(struct iwl_priv * priv,int mode,bool external)1248 int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
1249 {
1250 	struct iwl_force_reset *force_reset;
1251 
1252 	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1253 		return -EINVAL;
1254 
1255 	if (mode >= IWL_MAX_FORCE_RESET) {
1256 		IWL_DEBUG_INFO(priv, "invalid reset request.\n");
1257 		return -EINVAL;
1258 	}
1259 	force_reset = &priv->force_reset[mode];
1260 	force_reset->reset_request_count++;
1261 	if (!external) {
1262 		if (force_reset->last_force_reset_jiffies &&
1263 		    time_after(force_reset->last_force_reset_jiffies +
1264 		    force_reset->reset_duration, jiffies)) {
1265 			IWL_DEBUG_INFO(priv, "force reset rejected\n");
1266 			force_reset->reset_reject_count++;
1267 			return -EAGAIN;
1268 		}
1269 	}
1270 	force_reset->reset_success_count++;
1271 	force_reset->last_force_reset_jiffies = jiffies;
1272 	IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
1273 	switch (mode) {
1274 	case IWL_RF_RESET:
1275 		iwl_force_rf_reset(priv);
1276 		break;
1277 	case IWL_FW_RESET:
1278 		/*
1279 		 * if the request is from external(ex: debugfs),
1280 		 * then always perform the request in regardless the module
1281 		 * parameter setting
1282 		 * if the request is from internal (uCode error or driver
1283 		 * detect failure), then fw_restart module parameter
1284 		 * need to be check before performing firmware reload
1285 		 */
1286 		if (!external && !iwlagn_mod_params.restart_fw) {
1287 			IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
1288 				       "module parameter setting\n");
1289 			break;
1290 		}
1291 		IWL_ERR(priv, "On demand firmware reload\n");
1292 		iwlagn_fw_error(priv, true);
1293 		break;
1294 	}
1295 	return 0;
1296 }
1297 
1298 
iwl_cmd_echo_test(struct iwl_priv * priv)1299 int iwl_cmd_echo_test(struct iwl_priv *priv)
1300 {
1301 	int ret;
1302 	struct iwl_host_cmd cmd = {
1303 		.id = REPLY_ECHO,
1304 		.len = { 0 },
1305 		.flags = CMD_SYNC,
1306 	};
1307 
1308 	ret = iwl_dvm_send_cmd(priv, &cmd);
1309 	if (ret)
1310 		IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
1311 	else
1312 		IWL_DEBUG_INFO(priv, "echo testing pass\n");
1313 	return ret;
1314 }
1315 
iwl_check_stuck_queue(struct iwl_priv * priv,int txq)1316 static inline int iwl_check_stuck_queue(struct iwl_priv *priv, int txq)
1317 {
1318 	if (iwl_trans_check_stuck_queue(trans(priv), txq)) {
1319 		int ret;
1320 		ret = iwl_force_reset(priv, IWL_FW_RESET, false);
1321 		return (ret == -EAGAIN) ? 0 : 1;
1322 	}
1323 	return 0;
1324 }
1325 
1326 /*
1327  * Making watchdog tick be a quarter of timeout assure we will
1328  * discover the queue hung between timeout and 1.25*timeout
1329  */
1330 #define IWL_WD_TICK(timeout) ((timeout) / 4)
1331 
1332 /*
1333  * Watchdog timer callback, we check each tx queue for stuck, if if hung
1334  * we reset the firmware. If everything is fine just rearm the timer.
1335  */
iwl_bg_watchdog(unsigned long data)1336 void iwl_bg_watchdog(unsigned long data)
1337 {
1338 	struct iwl_priv *priv = (struct iwl_priv *)data;
1339 	int cnt;
1340 	unsigned long timeout;
1341 
1342 	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1343 		return;
1344 
1345 	if (iwl_is_rfkill(priv))
1346 		return;
1347 
1348 	timeout = hw_params(priv).wd_timeout;
1349 	if (timeout == 0)
1350 		return;
1351 
1352 	/* monitor and check for stuck queues */
1353 	for (cnt = 0; cnt < cfg(priv)->base_params->num_of_queues; cnt++)
1354 		if (iwl_check_stuck_queue(priv, cnt))
1355 			return;
1356 
1357 	mod_timer(&priv->watchdog, jiffies +
1358 		  msecs_to_jiffies(IWL_WD_TICK(timeout)));
1359 }
1360 
iwl_setup_watchdog(struct iwl_priv * priv)1361 void iwl_setup_watchdog(struct iwl_priv *priv)
1362 {
1363 	unsigned int timeout = hw_params(priv).wd_timeout;
1364 
1365 	if (!iwlagn_mod_params.wd_disable) {
1366 		/* use system default */
1367 		if (timeout && !cfg(priv)->base_params->wd_disable)
1368 			mod_timer(&priv->watchdog,
1369 				jiffies +
1370 				msecs_to_jiffies(IWL_WD_TICK(timeout)));
1371 		else
1372 			del_timer(&priv->watchdog);
1373 	} else {
1374 		/* module parameter overwrite default configuration */
1375 		if (timeout && iwlagn_mod_params.wd_disable == 2)
1376 			mod_timer(&priv->watchdog,
1377 				jiffies +
1378 				msecs_to_jiffies(IWL_WD_TICK(timeout)));
1379 		else
1380 			del_timer(&priv->watchdog);
1381 	}
1382 }
1383 
1384 /**
1385  * iwl_beacon_time_mask_low - mask of lower 32 bit of beacon time
1386  * @priv -- pointer to iwl_priv data structure
1387  * @tsf_bits -- number of bits need to shift for masking)
1388  */
iwl_beacon_time_mask_low(struct iwl_priv * priv,u16 tsf_bits)1389 static inline u32 iwl_beacon_time_mask_low(struct iwl_priv *priv,
1390 					   u16 tsf_bits)
1391 {
1392 	return (1 << tsf_bits) - 1;
1393 }
1394 
1395 /**
1396  * iwl_beacon_time_mask_high - mask of higher 32 bit of beacon time
1397  * @priv -- pointer to iwl_priv data structure
1398  * @tsf_bits -- number of bits need to shift for masking)
1399  */
iwl_beacon_time_mask_high(struct iwl_priv * priv,u16 tsf_bits)1400 static inline u32 iwl_beacon_time_mask_high(struct iwl_priv *priv,
1401 					    u16 tsf_bits)
1402 {
1403 	return ((1 << (32 - tsf_bits)) - 1) << tsf_bits;
1404 }
1405 
1406 /*
1407  * extended beacon time format
1408  * time in usec will be changed into a 32-bit value in extended:internal format
1409  * the extended part is the beacon counts
1410  * the internal part is the time in usec within one beacon interval
1411  */
iwl_usecs_to_beacons(struct iwl_priv * priv,u32 usec,u32 beacon_interval)1412 u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
1413 {
1414 	u32 quot;
1415 	u32 rem;
1416 	u32 interval = beacon_interval * TIME_UNIT;
1417 
1418 	if (!interval || !usec)
1419 		return 0;
1420 
1421 	quot = (usec / interval) &
1422 		(iwl_beacon_time_mask_high(priv, IWLAGN_EXT_BEACON_TIME_POS) >>
1423 		IWLAGN_EXT_BEACON_TIME_POS);
1424 	rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
1425 				   IWLAGN_EXT_BEACON_TIME_POS);
1426 
1427 	return (quot << IWLAGN_EXT_BEACON_TIME_POS) + rem;
1428 }
1429 
1430 /* base is usually what we get from ucode with each received frame,
1431  * the same as HW timer counter counting down
1432  */
iwl_add_beacon_time(struct iwl_priv * priv,u32 base,u32 addon,u32 beacon_interval)1433 __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
1434 			   u32 addon, u32 beacon_interval)
1435 {
1436 	u32 base_low = base & iwl_beacon_time_mask_low(priv,
1437 				IWLAGN_EXT_BEACON_TIME_POS);
1438 	u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
1439 				IWLAGN_EXT_BEACON_TIME_POS);
1440 	u32 interval = beacon_interval * TIME_UNIT;
1441 	u32 res = (base & iwl_beacon_time_mask_high(priv,
1442 				IWLAGN_EXT_BEACON_TIME_POS)) +
1443 				(addon & iwl_beacon_time_mask_high(priv,
1444 				IWLAGN_EXT_BEACON_TIME_POS));
1445 
1446 	if (base_low > addon_low)
1447 		res += base_low - addon_low;
1448 	else if (base_low < addon_low) {
1449 		res += interval + base_low - addon_low;
1450 		res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
1451 	} else
1452 		res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
1453 
1454 	return cpu_to_le32(res);
1455 }
1456 
iwl_nic_error(struct iwl_op_mode * op_mode)1457 void iwl_nic_error(struct iwl_op_mode *op_mode)
1458 {
1459 	struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1460 
1461 	iwlagn_fw_error(priv, false);
1462 }
1463 
iwl_set_hw_rfkill_state(struct iwl_op_mode * op_mode,bool state)1464 void iwl_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1465 {
1466 	struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1467 
1468 	if (state)
1469 		set_bit(STATUS_RF_KILL_HW, &priv->status);
1470 	else
1471 		clear_bit(STATUS_RF_KILL_HW, &priv->status);
1472 
1473 	wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
1474 }
1475 
iwl_free_skb(struct iwl_op_mode * op_mode,struct sk_buff * skb)1476 void iwl_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1477 {
1478 	struct ieee80211_tx_info *info;
1479 
1480 	info = IEEE80211_SKB_CB(skb);
1481 	kmem_cache_free(iwl_tx_cmd_pool, (info->driver_data[1]));
1482 	dev_kfree_skb_any(skb);
1483 }
1484