1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2011 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/etherdevice.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/sched.h>
34 
35 #include "iwl-wifi.h"
36 #include "iwl-dev.h"
37 #include "iwl-core.h"
38 #include "iwl-io.h"
39 #include "iwl-agn-hw.h"
40 #include "iwl-agn.h"
41 #include "iwl-trans.h"
42 #include "iwl-shared.h"
43 
iwlagn_hw_valid_rtc_data_addr(u32 addr)44 int iwlagn_hw_valid_rtc_data_addr(u32 addr)
45 {
46 	return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
47 		(addr < IWLAGN_RTC_DATA_UPPER_BOUND);
48 }
49 
iwlagn_send_tx_power(struct iwl_priv * priv)50 int iwlagn_send_tx_power(struct iwl_priv *priv)
51 {
52 	struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
53 	u8 tx_ant_cfg_cmd;
54 
55 	if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->shrd->status),
56 		      "TX Power requested while scanning!\n"))
57 		return -EAGAIN;
58 
59 	/* half dBm need to multiply */
60 	tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
61 
62 	if (priv->tx_power_lmt_in_half_dbm &&
63 	    priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
64 		/*
65 		 * For the newer devices which using enhanced/extend tx power
66 		 * table in EEPROM, the format is in half dBm. driver need to
67 		 * convert to dBm format before report to mac80211.
68 		 * By doing so, there is a possibility of 1/2 dBm resolution
69 		 * lost. driver will perform "round-up" operation before
70 		 * reporting, but it will cause 1/2 dBm tx power over the
71 		 * regulatory limit. Perform the checking here, if the
72 		 * "tx_power_user_lmt" is higher than EEPROM value (in
73 		 * half-dBm format), lower the tx power based on EEPROM
74 		 */
75 		tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
76 	}
77 	tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
78 	tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
79 
80 	if (IWL_UCODE_API(priv->ucode_ver) == 1)
81 		tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
82 	else
83 		tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
84 
85 	return iwl_trans_send_cmd_pdu(trans(priv), tx_ant_cfg_cmd, CMD_SYNC,
86 			sizeof(tx_power_cmd), &tx_power_cmd);
87 }
88 
iwlagn_temperature(struct iwl_priv * priv)89 void iwlagn_temperature(struct iwl_priv *priv)
90 {
91 	/* store temperature from correct statistics (in Celsius) */
92 	priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
93 	iwl_tt_handler(priv);
94 }
95 
iwl_eeprom_calib_version(struct iwl_shared * shrd)96 u16 iwl_eeprom_calib_version(struct iwl_shared *shrd)
97 {
98 	struct iwl_eeprom_calib_hdr *hdr;
99 
100 	hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(shrd,
101 							EEPROM_CALIB_ALL);
102 	return hdr->version;
103 
104 }
105 
106 /*
107  * EEPROM
108  */
eeprom_indirect_address(const struct iwl_shared * shrd,u32 address)109 static u32 eeprom_indirect_address(const struct iwl_shared *shrd, u32 address)
110 {
111 	u16 offset = 0;
112 
113 	if ((address & INDIRECT_ADDRESS) == 0)
114 		return address;
115 
116 	switch (address & INDIRECT_TYPE_MSK) {
117 	case INDIRECT_HOST:
118 		offset = iwl_eeprom_query16(shrd, EEPROM_LINK_HOST);
119 		break;
120 	case INDIRECT_GENERAL:
121 		offset = iwl_eeprom_query16(shrd, EEPROM_LINK_GENERAL);
122 		break;
123 	case INDIRECT_REGULATORY:
124 		offset = iwl_eeprom_query16(shrd, EEPROM_LINK_REGULATORY);
125 		break;
126 	case INDIRECT_TXP_LIMIT:
127 		offset = iwl_eeprom_query16(shrd, EEPROM_LINK_TXP_LIMIT);
128 		break;
129 	case INDIRECT_TXP_LIMIT_SIZE:
130 		offset = iwl_eeprom_query16(shrd, EEPROM_LINK_TXP_LIMIT_SIZE);
131 		break;
132 	case INDIRECT_CALIBRATION:
133 		offset = iwl_eeprom_query16(shrd, EEPROM_LINK_CALIBRATION);
134 		break;
135 	case INDIRECT_PROCESS_ADJST:
136 		offset = iwl_eeprom_query16(shrd, EEPROM_LINK_PROCESS_ADJST);
137 		break;
138 	case INDIRECT_OTHERS:
139 		offset = iwl_eeprom_query16(shrd, EEPROM_LINK_OTHERS);
140 		break;
141 	default:
142 		IWL_ERR(shrd->trans, "illegal indirect type: 0x%X\n",
143 		address & INDIRECT_TYPE_MSK);
144 		break;
145 	}
146 
147 	/* translate the offset from words to byte */
148 	return (address & ADDRESS_MSK) + (offset << 1);
149 }
150 
iwl_eeprom_query_addr(const struct iwl_shared * shrd,size_t offset)151 const u8 *iwl_eeprom_query_addr(const struct iwl_shared *shrd, size_t offset)
152 {
153 	u32 address = eeprom_indirect_address(shrd, offset);
154 	BUG_ON(address >= shrd->cfg->base_params->eeprom_size);
155 	return &shrd->eeprom[address];
156 }
157 
158 struct iwl_mod_params iwlagn_mod_params = {
159 	.amsdu_size_8K = 1,
160 	.restart_fw = 1,
161 	.plcp_check = true,
162 	.bt_coex_active = true,
163 	.no_sleep_autoadjust = true,
164 	.power_level = IWL_POWER_INDEX_1,
165 	.bt_ch_announce = true,
166 	.wanted_ucode_alternative = 1,
167 	.auto_agg = true,
168 	/* the rest are 0 by default */
169 };
170 
iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags,enum ieee80211_band band)171 int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
172 {
173 	int idx = 0;
174 	int band_offset = 0;
175 
176 	/* HT rate format: mac80211 wants an MCS number, which is just LSB */
177 	if (rate_n_flags & RATE_MCS_HT_MSK) {
178 		idx = (rate_n_flags & 0xff);
179 		return idx;
180 	/* Legacy rate format, search for match in table */
181 	} else {
182 		if (band == IEEE80211_BAND_5GHZ)
183 			band_offset = IWL_FIRST_OFDM_RATE;
184 		for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
185 			if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
186 				return idx - band_offset;
187 	}
188 
189 	return -1;
190 }
191 
iwlagn_manage_ibss_station(struct iwl_priv * priv,struct ieee80211_vif * vif,bool add)192 int iwlagn_manage_ibss_station(struct iwl_priv *priv,
193 			       struct ieee80211_vif *vif, bool add)
194 {
195 	struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
196 
197 	if (add)
198 		return iwlagn_add_bssid_station(priv, vif_priv->ctx,
199 						vif->bss_conf.bssid,
200 						&vif_priv->ibss_bssid_sta_id);
201 	return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
202 				  vif->bss_conf.bssid);
203 }
204 
205 /**
206  * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
207  *
208  * pre-requirements:
209  *  1. acquire mutex before calling
210  *  2. make sure rf is on and not in exit state
211  */
iwlagn_txfifo_flush(struct iwl_priv * priv,u16 flush_control)212 int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
213 {
214 	struct iwl_txfifo_flush_cmd flush_cmd;
215 	struct iwl_host_cmd cmd = {
216 		.id = REPLY_TXFIFO_FLUSH,
217 		.len = { sizeof(struct iwl_txfifo_flush_cmd), },
218 		.flags = CMD_SYNC,
219 		.data = { &flush_cmd, },
220 	};
221 
222 	might_sleep();
223 
224 	memset(&flush_cmd, 0, sizeof(flush_cmd));
225 	if (flush_control & BIT(IWL_RXON_CTX_BSS))
226 		flush_cmd.fifo_control = IWL_SCD_VO_MSK | IWL_SCD_VI_MSK |
227 				 IWL_SCD_BE_MSK | IWL_SCD_BK_MSK |
228 				 IWL_SCD_MGMT_MSK;
229 	if ((flush_control & BIT(IWL_RXON_CTX_PAN)) &&
230 	    (priv->shrd->valid_contexts != BIT(IWL_RXON_CTX_BSS)))
231 		flush_cmd.fifo_control |= IWL_PAN_SCD_VO_MSK |
232 				IWL_PAN_SCD_VI_MSK | IWL_PAN_SCD_BE_MSK |
233 				IWL_PAN_SCD_BK_MSK | IWL_PAN_SCD_MGMT_MSK |
234 				IWL_PAN_SCD_MULTICAST_MSK;
235 
236 	if (cfg(priv)->sku & EEPROM_SKU_CAP_11N_ENABLE)
237 		flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
238 
239 	IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
240 		       flush_cmd.fifo_control);
241 	flush_cmd.flush_control = cpu_to_le16(flush_control);
242 
243 	return iwl_trans_send_cmd(trans(priv), &cmd);
244 }
245 
iwlagn_dev_txfifo_flush(struct iwl_priv * priv,u16 flush_control)246 void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
247 {
248 	mutex_lock(&priv->shrd->mutex);
249 	ieee80211_stop_queues(priv->hw);
250 	if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
251 		IWL_ERR(priv, "flush request fail\n");
252 		goto done;
253 	}
254 	IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
255 	iwl_trans_wait_tx_queue_empty(trans(priv));
256 done:
257 	ieee80211_wake_queues(priv->hw);
258 	mutex_unlock(&priv->shrd->mutex);
259 }
260 
261 /*
262  * BT coex
263  */
264 /*
265  * Macros to access the lookup table.
266  *
267  * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
268 * wifi_prio, wifi_txrx and wifi_sh_ant_req.
269  *
270  * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
271  *
272  * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
273  * one after another in 32-bit registers, and "registers" 0 through 7 contain
274  * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
275  *
276  * These macros encode that format.
277  */
278 #define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
279 		  wifi_txrx, wifi_sh_ant_req) \
280 	(bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
281 	(wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
282 
283 #define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
284 	lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
285 #define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
286 				 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
287 	(!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
288 				   bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
289 				   wifi_sh_ant_req))))
290 #define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
291 				wifi_prio, wifi_txrx, wifi_sh_ant_req) \
292 	LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
293 			       bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
294 			       wifi_sh_ant_req))
295 #define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
296 				  wifi_req, wifi_prio, wifi_txrx, \
297 				  wifi_sh_ant_req) \
298 	LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
299 			       bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
300 			       wifi_sh_ant_req))
301 
302 #define LUT_WLAN_KILL_OP(lut, op, val) \
303 	lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
304 #define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
305 			   wifi_prio, wifi_txrx, wifi_sh_ant_req) \
306 	(!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
307 			     wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
308 #define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
309 			  wifi_prio, wifi_txrx, wifi_sh_ant_req) \
310 	LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
311 			 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
312 #define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
313 			    wifi_prio, wifi_txrx, wifi_sh_ant_req) \
314 	LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
315 			 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
316 
317 #define LUT_ANT_SWITCH_OP(lut, op, val) \
318 	lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
319 #define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
320 			    wifi_prio, wifi_txrx, wifi_sh_ant_req) \
321 	(!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
322 			      wifi_req, wifi_prio, wifi_txrx, \
323 			      wifi_sh_ant_req))))
324 #define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
325 			   wifi_prio, wifi_txrx, wifi_sh_ant_req) \
326 	LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
327 			  wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
328 #define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
329 			     wifi_prio, wifi_txrx, wifi_sh_ant_req) \
330 	LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
331 			  wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
332 
333 static const __le32 iwlagn_def_3w_lookup[12] = {
334 	cpu_to_le32(0xaaaaaaaa),
335 	cpu_to_le32(0xaaaaaaaa),
336 	cpu_to_le32(0xaeaaaaaa),
337 	cpu_to_le32(0xaaaaaaaa),
338 	cpu_to_le32(0xcc00ff28),
339 	cpu_to_le32(0x0000aaaa),
340 	cpu_to_le32(0xcc00aaaa),
341 	cpu_to_le32(0x0000aaaa),
342 	cpu_to_le32(0xc0004000),
343 	cpu_to_le32(0x00004000),
344 	cpu_to_le32(0xf0005000),
345 	cpu_to_le32(0xf0005000),
346 };
347 
348 static const __le32 iwlagn_concurrent_lookup[12] = {
349 	cpu_to_le32(0xaaaaaaaa),
350 	cpu_to_le32(0xaaaaaaaa),
351 	cpu_to_le32(0xaaaaaaaa),
352 	cpu_to_le32(0xaaaaaaaa),
353 	cpu_to_le32(0xaaaaaaaa),
354 	cpu_to_le32(0xaaaaaaaa),
355 	cpu_to_le32(0xaaaaaaaa),
356 	cpu_to_le32(0xaaaaaaaa),
357 	cpu_to_le32(0x00000000),
358 	cpu_to_le32(0x00000000),
359 	cpu_to_le32(0x00000000),
360 	cpu_to_le32(0x00000000),
361 };
362 
iwlagn_send_advance_bt_config(struct iwl_priv * priv)363 void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
364 {
365 	struct iwl_basic_bt_cmd basic = {
366 		.max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
367 		.bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
368 		.bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
369 		.bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
370 	};
371 	struct iwl6000_bt_cmd bt_cmd_6000;
372 	struct iwl2000_bt_cmd bt_cmd_2000;
373 	int ret;
374 
375 	BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
376 			sizeof(basic.bt3_lookup_table));
377 
378 	if (cfg(priv)->bt_params) {
379 		if (cfg(priv)->bt_params->bt_session_2) {
380 			bt_cmd_2000.prio_boost = cpu_to_le32(
381 				cfg(priv)->bt_params->bt_prio_boost);
382 			bt_cmd_2000.tx_prio_boost = 0;
383 			bt_cmd_2000.rx_prio_boost = 0;
384 		} else {
385 			bt_cmd_6000.prio_boost =
386 				cfg(priv)->bt_params->bt_prio_boost;
387 			bt_cmd_6000.tx_prio_boost = 0;
388 			bt_cmd_6000.rx_prio_boost = 0;
389 		}
390 	} else {
391 		IWL_ERR(priv, "failed to construct BT Coex Config\n");
392 		return;
393 	}
394 
395 	basic.kill_ack_mask = priv->kill_ack_mask;
396 	basic.kill_cts_mask = priv->kill_cts_mask;
397 	basic.valid = priv->bt_valid;
398 
399 	/*
400 	 * Configure BT coex mode to "no coexistence" when the
401 	 * user disabled BT coexistence, we have no interface
402 	 * (might be in monitor mode), or the interface is in
403 	 * IBSS mode (no proper uCode support for coex then).
404 	 */
405 	if (!iwlagn_mod_params.bt_coex_active ||
406 	    priv->iw_mode == NL80211_IFTYPE_ADHOC) {
407 		basic.flags = IWLAGN_BT_FLAG_COEX_MODE_DISABLED;
408 	} else {
409 		basic.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
410 					IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
411 
412 		if (!priv->bt_enable_pspoll)
413 			basic.flags |= IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
414 		else
415 			basic.flags &= ~IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
416 
417 		if (priv->bt_ch_announce)
418 			basic.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
419 		IWL_DEBUG_COEX(priv, "BT coex flag: 0X%x\n", basic.flags);
420 	}
421 	priv->bt_enable_flag = basic.flags;
422 	if (priv->bt_full_concurrent)
423 		memcpy(basic.bt3_lookup_table, iwlagn_concurrent_lookup,
424 			sizeof(iwlagn_concurrent_lookup));
425 	else
426 		memcpy(basic.bt3_lookup_table, iwlagn_def_3w_lookup,
427 			sizeof(iwlagn_def_3w_lookup));
428 
429 	IWL_DEBUG_COEX(priv, "BT coex %s in %s mode\n",
430 		       basic.flags ? "active" : "disabled",
431 		       priv->bt_full_concurrent ?
432 		       "full concurrency" : "3-wire");
433 
434 	if (cfg(priv)->bt_params->bt_session_2) {
435 		memcpy(&bt_cmd_2000.basic, &basic,
436 			sizeof(basic));
437 		ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
438 			CMD_SYNC, sizeof(bt_cmd_2000), &bt_cmd_2000);
439 	} else {
440 		memcpy(&bt_cmd_6000.basic, &basic,
441 			sizeof(basic));
442 		ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
443 			CMD_SYNC, sizeof(bt_cmd_6000), &bt_cmd_6000);
444 	}
445 	if (ret)
446 		IWL_ERR(priv, "failed to send BT Coex Config\n");
447 
448 }
449 
iwlagn_bt_adjust_rssi_monitor(struct iwl_priv * priv,bool rssi_ena)450 void iwlagn_bt_adjust_rssi_monitor(struct iwl_priv *priv, bool rssi_ena)
451 {
452 	struct iwl_rxon_context *ctx, *found_ctx = NULL;
453 	bool found_ap = false;
454 
455 	lockdep_assert_held(&priv->shrd->mutex);
456 
457 	/* Check whether AP or GO mode is active. */
458 	if (rssi_ena) {
459 		for_each_context(priv, ctx) {
460 			if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_AP &&
461 			    iwl_is_associated_ctx(ctx)) {
462 				found_ap = true;
463 				break;
464 			}
465 		}
466 	}
467 
468 	/*
469 	 * If disable was received or If GO/AP mode, disable RSSI
470 	 * measurements.
471 	 */
472 	if (!rssi_ena || found_ap) {
473 		if (priv->cur_rssi_ctx) {
474 			ctx = priv->cur_rssi_ctx;
475 			ieee80211_disable_rssi_reports(ctx->vif);
476 			priv->cur_rssi_ctx = NULL;
477 		}
478 		return;
479 	}
480 
481 	/*
482 	 * If rssi measurements need to be enabled, consider all cases now.
483 	 * Figure out how many contexts are active.
484 	 */
485 	for_each_context(priv, ctx) {
486 		if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
487 		    iwl_is_associated_ctx(ctx)) {
488 			found_ctx = ctx;
489 			break;
490 		}
491 	}
492 
493 	/*
494 	 * rssi monitor already enabled for the correct interface...nothing
495 	 * to do.
496 	 */
497 	if (found_ctx == priv->cur_rssi_ctx)
498 		return;
499 
500 	/*
501 	 * Figure out if rssi monitor is currently enabled, and needs
502 	 * to be changed. If rssi monitor is already enabled, disable
503 	 * it first else just enable rssi measurements on the
504 	 * interface found above.
505 	 */
506 	if (priv->cur_rssi_ctx) {
507 		ctx = priv->cur_rssi_ctx;
508 		if (ctx->vif)
509 			ieee80211_disable_rssi_reports(ctx->vif);
510 	}
511 
512 	priv->cur_rssi_ctx = found_ctx;
513 
514 	if (!found_ctx)
515 		return;
516 
517 	ieee80211_enable_rssi_reports(found_ctx->vif,
518 			IWLAGN_BT_PSP_MIN_RSSI_THRESHOLD,
519 			IWLAGN_BT_PSP_MAX_RSSI_THRESHOLD);
520 }
521 
iwlagn_bt_traffic_is_sco(struct iwl_bt_uart_msg * uart_msg)522 static bool iwlagn_bt_traffic_is_sco(struct iwl_bt_uart_msg *uart_msg)
523 {
524 	return BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3 >>
525 			BT_UART_MSG_FRAME3SCOESCO_POS;
526 }
527 
iwlagn_bt_traffic_change_work(struct work_struct * work)528 static void iwlagn_bt_traffic_change_work(struct work_struct *work)
529 {
530 	struct iwl_priv *priv =
531 		container_of(work, struct iwl_priv, bt_traffic_change_work);
532 	struct iwl_rxon_context *ctx;
533 	int smps_request = -1;
534 
535 	if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
536 		/* bt coex disabled */
537 		return;
538 	}
539 
540 	/*
541 	 * Note: bt_traffic_load can be overridden by scan complete and
542 	 * coex profile notifications. Ignore that since only bad consequence
543 	 * can be not matching debug print with actual state.
544 	 */
545 	IWL_DEBUG_COEX(priv, "BT traffic load changes: %d\n",
546 		       priv->bt_traffic_load);
547 
548 	switch (priv->bt_traffic_load) {
549 	case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
550 		if (priv->bt_status)
551 			smps_request = IEEE80211_SMPS_DYNAMIC;
552 		else
553 			smps_request = IEEE80211_SMPS_AUTOMATIC;
554 		break;
555 	case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
556 		smps_request = IEEE80211_SMPS_DYNAMIC;
557 		break;
558 	case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
559 	case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
560 		smps_request = IEEE80211_SMPS_STATIC;
561 		break;
562 	default:
563 		IWL_ERR(priv, "Invalid BT traffic load: %d\n",
564 			priv->bt_traffic_load);
565 		break;
566 	}
567 
568 	mutex_lock(&priv->shrd->mutex);
569 
570 	/*
571 	 * We can not send command to firmware while scanning. When the scan
572 	 * complete we will schedule this work again. We do check with mutex
573 	 * locked to prevent new scan request to arrive. We do not check
574 	 * STATUS_SCANNING to avoid race when queue_work two times from
575 	 * different notifications, but quit and not perform any work at all.
576 	 */
577 	if (test_bit(STATUS_SCAN_HW, &priv->shrd->status))
578 		goto out;
579 
580 	iwl_update_chain_flags(priv);
581 
582 	if (smps_request != -1) {
583 		priv->current_ht_config.smps = smps_request;
584 		for_each_context(priv, ctx) {
585 			if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
586 				ieee80211_request_smps(ctx->vif, smps_request);
587 		}
588 	}
589 
590 	/*
591 	 * Dynamic PS poll related functionality. Adjust RSSI measurements if
592 	 * necessary.
593 	 */
594 	iwlagn_bt_coex_rssi_monitor(priv);
595 out:
596 	mutex_unlock(&priv->shrd->mutex);
597 }
598 
599 /*
600  * If BT sco traffic, and RSSI monitor is enabled, move measurements to the
601  * correct interface or disable it if this is the last interface to be
602  * removed.
603  */
iwlagn_bt_coex_rssi_monitor(struct iwl_priv * priv)604 void iwlagn_bt_coex_rssi_monitor(struct iwl_priv *priv)
605 {
606 	if (priv->bt_is_sco &&
607 	    priv->bt_traffic_load == IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS)
608 		iwlagn_bt_adjust_rssi_monitor(priv, true);
609 	else
610 		iwlagn_bt_adjust_rssi_monitor(priv, false);
611 }
612 
iwlagn_print_uartmsg(struct iwl_priv * priv,struct iwl_bt_uart_msg * uart_msg)613 static void iwlagn_print_uartmsg(struct iwl_priv *priv,
614 				struct iwl_bt_uart_msg *uart_msg)
615 {
616 	IWL_DEBUG_COEX(priv, "Message Type = 0x%X, SSN = 0x%X, "
617 			"Update Req = 0x%X",
618 		(BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
619 			BT_UART_MSG_FRAME1MSGTYPE_POS,
620 		(BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
621 			BT_UART_MSG_FRAME1SSN_POS,
622 		(BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
623 			BT_UART_MSG_FRAME1UPDATEREQ_POS);
624 
625 	IWL_DEBUG_COEX(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
626 			"Chl_SeqN = 0x%X, In band = 0x%X",
627 		(BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
628 			BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
629 		(BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
630 			BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
631 		(BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
632 			BT_UART_MSG_FRAME2CHLSEQN_POS,
633 		(BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
634 			BT_UART_MSG_FRAME2INBAND_POS);
635 
636 	IWL_DEBUG_COEX(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
637 			"ACL = 0x%X, Master = 0x%X, OBEX = 0x%X",
638 		(BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
639 			BT_UART_MSG_FRAME3SCOESCO_POS,
640 		(BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
641 			BT_UART_MSG_FRAME3SNIFF_POS,
642 		(BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
643 			BT_UART_MSG_FRAME3A2DP_POS,
644 		(BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
645 			BT_UART_MSG_FRAME3ACL_POS,
646 		(BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
647 			BT_UART_MSG_FRAME3MASTER_POS,
648 		(BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
649 			BT_UART_MSG_FRAME3OBEX_POS);
650 
651 	IWL_DEBUG_COEX(priv, "Idle duration = 0x%X",
652 		(BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
653 			BT_UART_MSG_FRAME4IDLEDURATION_POS);
654 
655 	IWL_DEBUG_COEX(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
656 			"eSCO Retransmissions = 0x%X",
657 		(BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
658 			BT_UART_MSG_FRAME5TXACTIVITY_POS,
659 		(BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
660 			BT_UART_MSG_FRAME5RXACTIVITY_POS,
661 		(BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
662 			BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
663 
664 	IWL_DEBUG_COEX(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X",
665 		(BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
666 			BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
667 		(BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
668 			BT_UART_MSG_FRAME6DISCOVERABLE_POS);
669 
670 	IWL_DEBUG_COEX(priv, "Sniff Activity = 0x%X, Page = "
671 			"0x%X, Inquiry = 0x%X, Connectable = 0x%X",
672 		(BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
673 			BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
674 		(BT_UART_MSG_FRAME7PAGE_MSK & uart_msg->frame7) >>
675 			BT_UART_MSG_FRAME7PAGE_POS,
676 		(BT_UART_MSG_FRAME7INQUIRY_MSK & uart_msg->frame7) >>
677 			BT_UART_MSG_FRAME7INQUIRY_POS,
678 		(BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
679 			BT_UART_MSG_FRAME7CONNECTABLE_POS);
680 }
681 
iwlagn_set_kill_msk(struct iwl_priv * priv,struct iwl_bt_uart_msg * uart_msg)682 static void iwlagn_set_kill_msk(struct iwl_priv *priv,
683 				struct iwl_bt_uart_msg *uart_msg)
684 {
685 	u8 kill_msk;
686 	static const __le32 bt_kill_ack_msg[2] = {
687 		IWLAGN_BT_KILL_ACK_MASK_DEFAULT,
688 		IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
689 	static const __le32 bt_kill_cts_msg[2] = {
690 		IWLAGN_BT_KILL_CTS_MASK_DEFAULT,
691 		IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
692 
693 	kill_msk = (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3)
694 		? 1 : 0;
695 	if (priv->kill_ack_mask != bt_kill_ack_msg[kill_msk] ||
696 	    priv->kill_cts_mask != bt_kill_cts_msg[kill_msk]) {
697 		priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
698 		priv->kill_ack_mask = bt_kill_ack_msg[kill_msk];
699 		priv->bt_valid |= IWLAGN_BT_VALID_KILL_CTS_MASK;
700 		priv->kill_cts_mask = bt_kill_cts_msg[kill_msk];
701 
702 		/* schedule to send runtime bt_config */
703 		queue_work(priv->shrd->workqueue, &priv->bt_runtime_config);
704 	}
705 }
706 
iwlagn_bt_coex_profile_notif(struct iwl_priv * priv,struct iwl_rx_mem_buffer * rxb,struct iwl_device_cmd * cmd)707 int iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
708 				  struct iwl_rx_mem_buffer *rxb,
709 				  struct iwl_device_cmd *cmd)
710 {
711 	unsigned long flags;
712 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
713 	struct iwl_bt_coex_profile_notif *coex = &pkt->u.bt_coex_profile_notif;
714 	struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
715 
716 	if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
717 		/* bt coex disabled */
718 		return 0;
719 	}
720 
721 	IWL_DEBUG_COEX(priv, "BT Coex notification:\n");
722 	IWL_DEBUG_COEX(priv, "    status: %d\n", coex->bt_status);
723 	IWL_DEBUG_COEX(priv, "    traffic load: %d\n", coex->bt_traffic_load);
724 	IWL_DEBUG_COEX(priv, "    CI compliance: %d\n",
725 			coex->bt_ci_compliance);
726 	iwlagn_print_uartmsg(priv, uart_msg);
727 
728 	priv->last_bt_traffic_load = priv->bt_traffic_load;
729 	priv->bt_is_sco = iwlagn_bt_traffic_is_sco(uart_msg);
730 
731 	if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
732 		if (priv->bt_status != coex->bt_status ||
733 		    priv->last_bt_traffic_load != coex->bt_traffic_load) {
734 			if (coex->bt_status) {
735 				/* BT on */
736 				if (!priv->bt_ch_announce)
737 					priv->bt_traffic_load =
738 						IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
739 				else
740 					priv->bt_traffic_load =
741 						coex->bt_traffic_load;
742 			} else {
743 				/* BT off */
744 				priv->bt_traffic_load =
745 					IWL_BT_COEX_TRAFFIC_LOAD_NONE;
746 			}
747 			priv->bt_status = coex->bt_status;
748 			queue_work(priv->shrd->workqueue,
749 				   &priv->bt_traffic_change_work);
750 		}
751 	}
752 
753 	iwlagn_set_kill_msk(priv, uart_msg);
754 
755 	/* FIXME: based on notification, adjust the prio_boost */
756 
757 	spin_lock_irqsave(&priv->shrd->lock, flags);
758 	priv->bt_ci_compliance = coex->bt_ci_compliance;
759 	spin_unlock_irqrestore(&priv->shrd->lock, flags);
760 	return 0;
761 }
762 
iwlagn_bt_rx_handler_setup(struct iwl_priv * priv)763 void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
764 {
765 	priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
766 		iwlagn_bt_coex_profile_notif;
767 }
768 
iwlagn_bt_setup_deferred_work(struct iwl_priv * priv)769 void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
770 {
771 	INIT_WORK(&priv->bt_traffic_change_work,
772 		  iwlagn_bt_traffic_change_work);
773 }
774 
iwlagn_bt_cancel_deferred_work(struct iwl_priv * priv)775 void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
776 {
777 	cancel_work_sync(&priv->bt_traffic_change_work);
778 }
779 
is_single_rx_stream(struct iwl_priv * priv)780 static bool is_single_rx_stream(struct iwl_priv *priv)
781 {
782 	return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
783 	       priv->current_ht_config.single_chain_sufficient;
784 }
785 
786 #define IWL_NUM_RX_CHAINS_MULTIPLE	3
787 #define IWL_NUM_RX_CHAINS_SINGLE	2
788 #define IWL_NUM_IDLE_CHAINS_DUAL	2
789 #define IWL_NUM_IDLE_CHAINS_SINGLE	1
790 
791 /*
792  * Determine how many receiver/antenna chains to use.
793  *
794  * More provides better reception via diversity.  Fewer saves power
795  * at the expense of throughput, but only when not in powersave to
796  * start with.
797  *
798  * MIMO (dual stream) requires at least 2, but works better with 3.
799  * This does not determine *which* chains to use, just how many.
800  */
iwl_get_active_rx_chain_count(struct iwl_priv * priv)801 static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
802 {
803 	if (cfg(priv)->bt_params &&
804 	    cfg(priv)->bt_params->advanced_bt_coexist &&
805 	    (priv->bt_full_concurrent ||
806 	     priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
807 		/*
808 		 * only use chain 'A' in bt high traffic load or
809 		 * full concurrency mode
810 		 */
811 		return IWL_NUM_RX_CHAINS_SINGLE;
812 	}
813 	/* # of Rx chains to use when expecting MIMO. */
814 	if (is_single_rx_stream(priv))
815 		return IWL_NUM_RX_CHAINS_SINGLE;
816 	else
817 		return IWL_NUM_RX_CHAINS_MULTIPLE;
818 }
819 
820 /*
821  * When we are in power saving mode, unless device support spatial
822  * multiplexing power save, use the active count for rx chain count.
823  */
iwl_get_idle_rx_chain_count(struct iwl_priv * priv,int active_cnt)824 static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
825 {
826 	/* # Rx chains when idling, depending on SMPS mode */
827 	switch (priv->current_ht_config.smps) {
828 	case IEEE80211_SMPS_STATIC:
829 	case IEEE80211_SMPS_DYNAMIC:
830 		return IWL_NUM_IDLE_CHAINS_SINGLE;
831 	case IEEE80211_SMPS_AUTOMATIC:
832 	case IEEE80211_SMPS_OFF:
833 		return active_cnt;
834 	default:
835 		WARN(1, "invalid SMPS mode %d",
836 		     priv->current_ht_config.smps);
837 		return active_cnt;
838 	}
839 }
840 
841 /* up to 4 chains */
iwl_count_chain_bitmap(u32 chain_bitmap)842 static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
843 {
844 	u8 res;
845 	res = (chain_bitmap & BIT(0)) >> 0;
846 	res += (chain_bitmap & BIT(1)) >> 1;
847 	res += (chain_bitmap & BIT(2)) >> 2;
848 	res += (chain_bitmap & BIT(3)) >> 3;
849 	return res;
850 }
851 
852 /**
853  * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
854  *
855  * Selects how many and which Rx receivers/antennas/chains to use.
856  * This should not be used for scan command ... it puts data in wrong place.
857  */
iwlagn_set_rxon_chain(struct iwl_priv * priv,struct iwl_rxon_context * ctx)858 void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
859 {
860 	bool is_single = is_single_rx_stream(priv);
861 	bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->shrd->status);
862 	u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
863 	u32 active_chains;
864 	u16 rx_chain;
865 
866 	/* Tell uCode which antennas are actually connected.
867 	 * Before first association, we assume all antennas are connected.
868 	 * Just after first association, iwl_chain_noise_calibration()
869 	 *    checks which antennas actually *are* connected. */
870 	if (priv->chain_noise_data.active_chains)
871 		active_chains = priv->chain_noise_data.active_chains;
872 	else
873 		active_chains = hw_params(priv).valid_rx_ant;
874 
875 	if (cfg(priv)->bt_params &&
876 	    cfg(priv)->bt_params->advanced_bt_coexist &&
877 	    (priv->bt_full_concurrent ||
878 	     priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
879 		/*
880 		 * only use chain 'A' in bt high traffic load or
881 		 * full concurrency mode
882 		 */
883 		active_chains = first_antenna(active_chains);
884 	}
885 
886 	rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
887 
888 	/* How many receivers should we use? */
889 	active_rx_cnt = iwl_get_active_rx_chain_count(priv);
890 	idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
891 
892 
893 	/* correct rx chain count according hw settings
894 	 * and chain noise calibration
895 	 */
896 	valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
897 	if (valid_rx_cnt < active_rx_cnt)
898 		active_rx_cnt = valid_rx_cnt;
899 
900 	if (valid_rx_cnt < idle_rx_cnt)
901 		idle_rx_cnt = valid_rx_cnt;
902 
903 	rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
904 	rx_chain |= idle_rx_cnt  << RXON_RX_CHAIN_CNT_POS;
905 
906 	ctx->staging.rx_chain = cpu_to_le16(rx_chain);
907 
908 	if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
909 		ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
910 	else
911 		ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
912 
913 	IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
914 			ctx->staging.rx_chain,
915 			active_rx_cnt, idle_rx_cnt);
916 
917 	WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
918 		active_rx_cnt < idle_rx_cnt);
919 }
920 
iwl_toggle_tx_ant(struct iwl_priv * priv,u8 ant,u8 valid)921 u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
922 {
923 	int i;
924 	u8 ind = ant;
925 
926 	if (priv->band == IEEE80211_BAND_2GHZ &&
927 	    priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
928 		return 0;
929 
930 	for (i = 0; i < RATE_ANT_NUM - 1; i++) {
931 		ind = (ind + 1) < RATE_ANT_NUM ?  ind + 1 : 0;
932 		if (valid & BIT(ind))
933 			return ind;
934 	}
935 	return ant;
936 }
937 
938 #ifdef CONFIG_PM_SLEEP
iwlagn_convert_p1k(u16 * p1k,__le16 * out)939 static void iwlagn_convert_p1k(u16 *p1k, __le16 *out)
940 {
941 	int i;
942 
943 	for (i = 0; i < IWLAGN_P1K_SIZE; i++)
944 		out[i] = cpu_to_le16(p1k[i]);
945 }
946 
947 struct wowlan_key_data {
948 	struct iwl_rxon_context *ctx;
949 	struct iwlagn_wowlan_rsc_tsc_params_cmd *rsc_tsc;
950 	struct iwlagn_wowlan_tkip_params_cmd *tkip;
951 	const u8 *bssid;
952 	bool error, use_rsc_tsc, use_tkip;
953 };
954 
955 
iwlagn_wowlan_program_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)956 static void iwlagn_wowlan_program_keys(struct ieee80211_hw *hw,
957 			       struct ieee80211_vif *vif,
958 			       struct ieee80211_sta *sta,
959 			       struct ieee80211_key_conf *key,
960 			       void *_data)
961 {
962 	struct iwl_priv *priv = hw->priv;
963 	struct wowlan_key_data *data = _data;
964 	struct iwl_rxon_context *ctx = data->ctx;
965 	struct aes_sc *aes_sc, *aes_tx_sc = NULL;
966 	struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
967 	struct iwlagn_p1k_cache *rx_p1ks;
968 	u8 *rx_mic_key;
969 	struct ieee80211_key_seq seq;
970 	u32 cur_rx_iv32 = 0;
971 	u16 p1k[IWLAGN_P1K_SIZE];
972 	int ret, i;
973 
974 	mutex_lock(&priv->shrd->mutex);
975 
976 	if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
977 	     key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
978 	     !sta && !ctx->key_mapping_keys)
979 		ret = iwl_set_default_wep_key(priv, ctx, key);
980 	else
981 		ret = iwl_set_dynamic_key(priv, ctx, key, sta);
982 
983 	if (ret) {
984 		IWL_ERR(priv, "Error setting key during suspend!\n");
985 		data->error = true;
986 	}
987 
988 	switch (key->cipher) {
989 	case WLAN_CIPHER_SUITE_TKIP:
990 		if (sta) {
991 			tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
992 			tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
993 
994 			rx_p1ks = data->tkip->rx_uni;
995 
996 			ieee80211_get_key_tx_seq(key, &seq);
997 			tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
998 			tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
999 
1000 			ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
1001 			iwlagn_convert_p1k(p1k, data->tkip->tx.p1k);
1002 
1003 			memcpy(data->tkip->mic_keys.tx,
1004 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
1005 			       IWLAGN_MIC_KEY_SIZE);
1006 
1007 			rx_mic_key = data->tkip->mic_keys.rx_unicast;
1008 		} else {
1009 			tkip_sc =
1010 				data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
1011 			rx_p1ks = data->tkip->rx_multi;
1012 			rx_mic_key = data->tkip->mic_keys.rx_mcast;
1013 		}
1014 
1015 		/*
1016 		 * For non-QoS this relies on the fact that both the uCode and
1017 		 * mac80211 use TID 0 (as they need to to avoid replay attacks)
1018 		 * for checking the IV in the frames.
1019 		 */
1020 		for (i = 0; i < IWLAGN_NUM_RSC; i++) {
1021 			ieee80211_get_key_rx_seq(key, i, &seq);
1022 			tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
1023 			tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
1024 			/* wrapping isn't allowed, AP must rekey */
1025 			if (seq.tkip.iv32 > cur_rx_iv32)
1026 				cur_rx_iv32 = seq.tkip.iv32;
1027 		}
1028 
1029 		ieee80211_get_tkip_rx_p1k(key, data->bssid, cur_rx_iv32, p1k);
1030 		iwlagn_convert_p1k(p1k, rx_p1ks[0].p1k);
1031 		ieee80211_get_tkip_rx_p1k(key, data->bssid,
1032 					  cur_rx_iv32 + 1, p1k);
1033 		iwlagn_convert_p1k(p1k, rx_p1ks[1].p1k);
1034 
1035 		memcpy(rx_mic_key,
1036 		       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
1037 		       IWLAGN_MIC_KEY_SIZE);
1038 
1039 		data->use_tkip = true;
1040 		data->use_rsc_tsc = true;
1041 		break;
1042 	case WLAN_CIPHER_SUITE_CCMP:
1043 		if (sta) {
1044 			u8 *pn = seq.ccmp.pn;
1045 
1046 			aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
1047 			aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
1048 
1049 			ieee80211_get_key_tx_seq(key, &seq);
1050 			aes_tx_sc->pn = cpu_to_le64(
1051 					(u64)pn[5] |
1052 					((u64)pn[4] << 8) |
1053 					((u64)pn[3] << 16) |
1054 					((u64)pn[2] << 24) |
1055 					((u64)pn[1] << 32) |
1056 					((u64)pn[0] << 40));
1057 		} else
1058 			aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
1059 
1060 		/*
1061 		 * For non-QoS this relies on the fact that both the uCode and
1062 		 * mac80211 use TID 0 for checking the IV in the frames.
1063 		 */
1064 		for (i = 0; i < IWLAGN_NUM_RSC; i++) {
1065 			u8 *pn = seq.ccmp.pn;
1066 
1067 			ieee80211_get_key_rx_seq(key, i, &seq);
1068 			aes_sc->pn = cpu_to_le64(
1069 					(u64)pn[5] |
1070 					((u64)pn[4] << 8) |
1071 					((u64)pn[3] << 16) |
1072 					((u64)pn[2] << 24) |
1073 					((u64)pn[1] << 32) |
1074 					((u64)pn[0] << 40));
1075 		}
1076 		data->use_rsc_tsc = true;
1077 		break;
1078 	}
1079 
1080 	mutex_unlock(&priv->shrd->mutex);
1081 }
1082 
iwlagn_send_patterns(struct iwl_priv * priv,struct cfg80211_wowlan * wowlan)1083 int iwlagn_send_patterns(struct iwl_priv *priv,
1084 			struct cfg80211_wowlan *wowlan)
1085 {
1086 	struct iwlagn_wowlan_patterns_cmd *pattern_cmd;
1087 	struct iwl_host_cmd cmd = {
1088 		.id = REPLY_WOWLAN_PATTERNS,
1089 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1090 		.flags = CMD_SYNC,
1091 	};
1092 	int i, err;
1093 
1094 	if (!wowlan->n_patterns)
1095 		return 0;
1096 
1097 	cmd.len[0] = sizeof(*pattern_cmd) +
1098 		wowlan->n_patterns * sizeof(struct iwlagn_wowlan_pattern);
1099 
1100 	pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
1101 	if (!pattern_cmd)
1102 		return -ENOMEM;
1103 
1104 	pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
1105 
1106 	for (i = 0; i < wowlan->n_patterns; i++) {
1107 		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
1108 
1109 		memcpy(&pattern_cmd->patterns[i].mask,
1110 			wowlan->patterns[i].mask, mask_len);
1111 		memcpy(&pattern_cmd->patterns[i].pattern,
1112 			wowlan->patterns[i].pattern,
1113 			wowlan->patterns[i].pattern_len);
1114 		pattern_cmd->patterns[i].mask_size = mask_len;
1115 		pattern_cmd->patterns[i].pattern_size =
1116 			wowlan->patterns[i].pattern_len;
1117 	}
1118 
1119 	cmd.data[0] = pattern_cmd;
1120 	err = iwl_trans_send_cmd(trans(priv), &cmd);
1121 	kfree(pattern_cmd);
1122 	return err;
1123 }
1124 
iwlagn_suspend(struct iwl_priv * priv,struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1125 int iwlagn_suspend(struct iwl_priv *priv,
1126 		struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1127 {
1128 	struct iwlagn_wowlan_wakeup_filter_cmd wakeup_filter_cmd;
1129 	struct iwl_rxon_cmd rxon;
1130 	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1131 	struct iwlagn_wowlan_kek_kck_material_cmd kek_kck_cmd;
1132 	struct iwlagn_wowlan_tkip_params_cmd tkip_cmd = {};
1133 	struct iwlagn_d3_config_cmd d3_cfg_cmd = {};
1134 	struct wowlan_key_data key_data = {
1135 		.ctx = ctx,
1136 		.bssid = ctx->active.bssid_addr,
1137 		.use_rsc_tsc = false,
1138 		.tkip = &tkip_cmd,
1139 		.use_tkip = false,
1140 	};
1141 	int ret, i;
1142 	u16 seq;
1143 
1144 	key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
1145 	if (!key_data.rsc_tsc)
1146 		return -ENOMEM;
1147 
1148 	memset(&wakeup_filter_cmd, 0, sizeof(wakeup_filter_cmd));
1149 
1150 	/*
1151 	 * We know the last used seqno, and the uCode expects to know that
1152 	 * one, it will increment before TX.
1153 	 */
1154 	seq = le16_to_cpu(priv->last_seq_ctl) & IEEE80211_SCTL_SEQ;
1155 	wakeup_filter_cmd.non_qos_seq = cpu_to_le16(seq);
1156 
1157 	/*
1158 	 * For QoS counters, we store the one to use next, so subtract 0x10
1159 	 * since the uCode will add 0x10 before using the value.
1160 	 */
1161 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1162 		seq = priv->tid_data[IWL_AP_ID][i].seq_number;
1163 		seq -= 0x10;
1164 		wakeup_filter_cmd.qos_seq[i] = cpu_to_le16(seq);
1165 	}
1166 
1167 	if (wowlan->disconnect)
1168 		wakeup_filter_cmd.enabled |=
1169 			cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
1170 				    IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE);
1171 	if (wowlan->magic_pkt)
1172 		wakeup_filter_cmd.enabled |=
1173 			cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET);
1174 	if (wowlan->gtk_rekey_failure)
1175 		wakeup_filter_cmd.enabled |=
1176 			cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
1177 	if (wowlan->eap_identity_req)
1178 		wakeup_filter_cmd.enabled |=
1179 			cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ);
1180 	if (wowlan->four_way_handshake)
1181 		wakeup_filter_cmd.enabled |=
1182 			cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
1183 	if (wowlan->n_patterns)
1184 		wakeup_filter_cmd.enabled |=
1185 			cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH);
1186 
1187 	if (wowlan->rfkill_release)
1188 		d3_cfg_cmd.wakeup_flags |=
1189 			cpu_to_le32(IWLAGN_D3_WAKEUP_RFKILL);
1190 
1191 	iwl_scan_cancel_timeout(priv, 200);
1192 
1193 	memcpy(&rxon, &ctx->active, sizeof(rxon));
1194 
1195 	iwl_trans_stop_device(trans(priv));
1196 
1197 	priv->shrd->wowlan = true;
1198 
1199 	ret = iwl_load_ucode_wait_alive(trans(priv), IWL_UCODE_WOWLAN);
1200 	if (ret)
1201 		goto out;
1202 
1203 	/* now configure WoWLAN ucode */
1204 	ret = iwl_alive_start(priv);
1205 	if (ret)
1206 		goto out;
1207 
1208 	memcpy(&ctx->staging, &rxon, sizeof(rxon));
1209 	ret = iwlagn_commit_rxon(priv, ctx);
1210 	if (ret)
1211 		goto out;
1212 
1213 	ret = iwl_power_update_mode(priv, true);
1214 	if (ret)
1215 		goto out;
1216 
1217 	if (!iwlagn_mod_params.sw_crypto) {
1218 		/* mark all keys clear */
1219 		priv->ucode_key_table = 0;
1220 		ctx->key_mapping_keys = 0;
1221 
1222 		/*
1223 		 * This needs to be unlocked due to lock ordering
1224 		 * constraints. Since we're in the suspend path
1225 		 * that isn't really a problem though.
1226 		 */
1227 		mutex_unlock(&priv->shrd->mutex);
1228 		ieee80211_iter_keys(priv->hw, ctx->vif,
1229 				    iwlagn_wowlan_program_keys,
1230 				    &key_data);
1231 		mutex_lock(&priv->shrd->mutex);
1232 		if (key_data.error) {
1233 			ret = -EIO;
1234 			goto out;
1235 		}
1236 
1237 		if (key_data.use_rsc_tsc) {
1238 			struct iwl_host_cmd rsc_tsc_cmd = {
1239 				.id = REPLY_WOWLAN_TSC_RSC_PARAMS,
1240 				.flags = CMD_SYNC,
1241 				.data[0] = key_data.rsc_tsc,
1242 				.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1243 				.len[0] = sizeof(*key_data.rsc_tsc),
1244 			};
1245 
1246 			ret = iwl_trans_send_cmd(trans(priv), &rsc_tsc_cmd);
1247 			if (ret)
1248 				goto out;
1249 		}
1250 
1251 		if (key_data.use_tkip) {
1252 			ret = iwl_trans_send_cmd_pdu(trans(priv),
1253 						 REPLY_WOWLAN_TKIP_PARAMS,
1254 						 CMD_SYNC, sizeof(tkip_cmd),
1255 						 &tkip_cmd);
1256 			if (ret)
1257 				goto out;
1258 		}
1259 
1260 		if (priv->have_rekey_data) {
1261 			memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
1262 			memcpy(kek_kck_cmd.kck, priv->kck, NL80211_KCK_LEN);
1263 			kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
1264 			memcpy(kek_kck_cmd.kek, priv->kek, NL80211_KEK_LEN);
1265 			kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1266 			kek_kck_cmd.replay_ctr = priv->replay_ctr;
1267 
1268 			ret = iwl_trans_send_cmd_pdu(trans(priv),
1269 						 REPLY_WOWLAN_KEK_KCK_MATERIAL,
1270 						 CMD_SYNC, sizeof(kek_kck_cmd),
1271 						 &kek_kck_cmd);
1272 			if (ret)
1273 				goto out;
1274 		}
1275 	}
1276 
1277 	ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_D3_CONFIG, CMD_SYNC,
1278 				     sizeof(d3_cfg_cmd), &d3_cfg_cmd);
1279 	if (ret)
1280 		goto out;
1281 
1282 	ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_WOWLAN_WAKEUP_FILTER,
1283 				 CMD_SYNC, sizeof(wakeup_filter_cmd),
1284 				 &wakeup_filter_cmd);
1285 	if (ret)
1286 		goto out;
1287 
1288 	ret = iwlagn_send_patterns(priv, wowlan);
1289  out:
1290 	kfree(key_data.rsc_tsc);
1291 	return ret;
1292 }
1293 #endif
1294