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