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