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