b5ee99e236b13d32873fe2daaebc29fff4ac05fa
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / iwlwifi / iwl-agn-lib.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19  * USA
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29 #include <linux/etherdevice.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/sched.h>
34
35 #include "iwl-dev.h"
36 #include "iwl-core.h"
37 #include "iwl-io.h"
38 #include "iwl-agn-hw.h"
39 #include "iwl-agn.h"
40 #include "iwl-trans.h"
41 #include "iwl-shared.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 (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         }
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, CMD_SYNC,
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 struct iwl_mod_params iwlagn_mod_params = {
98         .amsdu_size_8K = 1,
99         .restart_fw = 1,
100         .plcp_check = true,
101         .bt_coex_active = true,
102         .no_sleep_autoadjust = true,
103         .power_level = IWL_POWER_INDEX_1,
104         .bt_ch_announce = true,
105         .wanted_ucode_alternative = 1,
106         .auto_agg = true,
107         /* the rest are 0 by default */
108 };
109
110 int 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
131 int iwlagn_manage_ibss_station(struct iwl_priv *priv,
132                                struct ieee80211_vif *vif, bool add)
133 {
134         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
135
136         if (add)
137                 return iwlagn_add_bssid_station(priv, vif_priv->ctx,
138                                                 vif->bss_conf.bssid,
139                                                 &vif_priv->ibss_bssid_sta_id);
140         return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
141                                   vif->bss_conf.bssid);
142 }
143
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  */
151 int 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,
156                 .len = { sizeof(struct iwl_txfifo_flush_cmd), },
157                 .flags = CMD_SYNC,
158                 .data = { &flush_cmd, },
159         };
160
161         might_sleep();
162
163         memset(&flush_cmd, 0, sizeof(flush_cmd));
164         if (flush_control & BIT(IWL_RXON_CTX_BSS))
165                 flush_cmd.fifo_control = IWL_SCD_VO_MSK | IWL_SCD_VI_MSK |
166                                  IWL_SCD_BE_MSK | IWL_SCD_BK_MSK |
167                                  IWL_SCD_MGMT_MSK;
168         if ((flush_control & BIT(IWL_RXON_CTX_PAN)) &&
169             (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS)))
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
175         if (priv->hw_params.sku & EEPROM_SKU_CAP_11N_ENABLE)
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
182         return iwl_dvm_send_cmd(priv, &cmd);
183 }
184
185 void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
186 {
187         mutex_lock(&priv->mutex);
188         ieee80211_stop_queues(priv->hw);
189         if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
190                 IWL_ERR(priv, "flush request fail\n");
191                 goto done;
192         }
193         IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
194         iwl_trans_wait_tx_queue_empty(trans(priv));
195 done:
196         ieee80211_wake_queues(priv->hw);
197         mutex_unlock(&priv->mutex);
198 }
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
272 static 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),
284         cpu_to_le32(0xf0005000),
285 };
286
287 static 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
302 void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
303 {
304         struct iwl_basic_bt_cmd basic = {
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         };
310         struct iwl6000_bt_cmd bt_cmd_6000;
311         struct iwl2000_bt_cmd bt_cmd_2000;
312         int ret;
313
314         BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
315                         sizeof(basic.bt3_lookup_table));
316
317         if (cfg(priv)->bt_params) {
318                 if (cfg(priv)->bt_params->bt_session_2) {
319                         bt_cmd_2000.prio_boost = cpu_to_le32(
320                                 cfg(priv)->bt_params->bt_prio_boost);
321                         bt_cmd_2000.tx_prio_boost = 0;
322                         bt_cmd_2000.rx_prio_boost = 0;
323                 } else {
324                         bt_cmd_6000.prio_boost =
325                                 cfg(priv)->bt_params->bt_prio_boost;
326                         bt_cmd_6000.tx_prio_boost = 0;
327                         bt_cmd_6000.rx_prio_boost = 0;
328                 }
329         } else {
330                 IWL_ERR(priv, "failed to construct BT Coex Config\n");
331                 return;
332         }
333
334         basic.kill_ack_mask = priv->kill_ack_mask;
335         basic.kill_cts_mask = priv->kill_cts_mask;
336         basic.valid = priv->bt_valid;
337
338         /*
339          * Configure BT coex mode to "no coexistence" when the
340          * user disabled BT coexistence, we have no interface
341          * (might be in monitor mode), or the interface is in
342          * IBSS mode (no proper uCode support for coex then).
343          */
344         if (!iwlagn_mod_params.bt_coex_active ||
345             priv->iw_mode == NL80211_IFTYPE_ADHOC) {
346                 basic.flags = IWLAGN_BT_FLAG_COEX_MODE_DISABLED;
347         } else {
348                 basic.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
349                                         IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
350
351                 if (!priv->bt_enable_pspoll)
352                         basic.flags |= IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
353                 else
354                         basic.flags &= ~IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
355
356                 if (priv->bt_ch_announce)
357                         basic.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
358                 IWL_DEBUG_COEX(priv, "BT coex flag: 0X%x\n", basic.flags);
359         }
360         priv->bt_enable_flag = basic.flags;
361         if (priv->bt_full_concurrent)
362                 memcpy(basic.bt3_lookup_table, iwlagn_concurrent_lookup,
363                         sizeof(iwlagn_concurrent_lookup));
364         else
365                 memcpy(basic.bt3_lookup_table, iwlagn_def_3w_lookup,
366                         sizeof(iwlagn_def_3w_lookup));
367
368         IWL_DEBUG_COEX(priv, "BT coex %s in %s mode\n",
369                        basic.flags ? "active" : "disabled",
370                        priv->bt_full_concurrent ?
371                        "full concurrency" : "3-wire");
372
373         if (cfg(priv)->bt_params->bt_session_2) {
374                 memcpy(&bt_cmd_2000.basic, &basic,
375                         sizeof(basic));
376                 ret = iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
377                         CMD_SYNC, sizeof(bt_cmd_2000), &bt_cmd_2000);
378         } else {
379                 memcpy(&bt_cmd_6000.basic, &basic,
380                         sizeof(basic));
381                 ret = iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
382                         CMD_SYNC, sizeof(bt_cmd_6000), &bt_cmd_6000);
383         }
384         if (ret)
385                 IWL_ERR(priv, "failed to send BT Coex Config\n");
386
387 }
388
389 void iwlagn_bt_adjust_rssi_monitor(struct iwl_priv *priv, bool rssi_ena)
390 {
391         struct iwl_rxon_context *ctx, *found_ctx = NULL;
392         bool found_ap = false;
393
394         lockdep_assert_held(&priv->mutex);
395
396         /* Check whether AP or GO mode is active. */
397         if (rssi_ena) {
398                 for_each_context(priv, ctx) {
399                         if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_AP &&
400                             iwl_is_associated_ctx(ctx)) {
401                                 found_ap = true;
402                                 break;
403                         }
404                 }
405         }
406
407         /*
408          * If disable was received or If GO/AP mode, disable RSSI
409          * measurements.
410          */
411         if (!rssi_ena || found_ap) {
412                 if (priv->cur_rssi_ctx) {
413                         ctx = priv->cur_rssi_ctx;
414                         ieee80211_disable_rssi_reports(ctx->vif);
415                         priv->cur_rssi_ctx = NULL;
416                 }
417                 return;
418         }
419
420         /*
421          * If rssi measurements need to be enabled, consider all cases now.
422          * Figure out how many contexts are active.
423          */
424         for_each_context(priv, ctx) {
425                 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
426                     iwl_is_associated_ctx(ctx)) {
427                         found_ctx = ctx;
428                         break;
429                 }
430         }
431
432         /*
433          * rssi monitor already enabled for the correct interface...nothing
434          * to do.
435          */
436         if (found_ctx == priv->cur_rssi_ctx)
437                 return;
438
439         /*
440          * Figure out if rssi monitor is currently enabled, and needs
441          * to be changed. If rssi monitor is already enabled, disable
442          * it first else just enable rssi measurements on the
443          * interface found above.
444          */
445         if (priv->cur_rssi_ctx) {
446                 ctx = priv->cur_rssi_ctx;
447                 if (ctx->vif)
448                         ieee80211_disable_rssi_reports(ctx->vif);
449         }
450
451         priv->cur_rssi_ctx = found_ctx;
452
453         if (!found_ctx)
454                 return;
455
456         ieee80211_enable_rssi_reports(found_ctx->vif,
457                         IWLAGN_BT_PSP_MIN_RSSI_THRESHOLD,
458                         IWLAGN_BT_PSP_MAX_RSSI_THRESHOLD);
459 }
460
461 static bool iwlagn_bt_traffic_is_sco(struct iwl_bt_uart_msg *uart_msg)
462 {
463         return BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3 >>
464                         BT_UART_MSG_FRAME3SCOESCO_POS;
465 }
466
467 static void iwlagn_bt_traffic_change_work(struct work_struct *work)
468 {
469         struct iwl_priv *priv =
470                 container_of(work, struct iwl_priv, bt_traffic_change_work);
471         struct iwl_rxon_context *ctx;
472         int smps_request = -1;
473
474         if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
475                 /* bt coex disabled */
476                 return;
477         }
478
479         /*
480          * Note: bt_traffic_load can be overridden by scan complete and
481          * coex profile notifications. Ignore that since only bad consequence
482          * can be not matching debug print with actual state.
483          */
484         IWL_DEBUG_COEX(priv, "BT traffic load changes: %d\n",
485                        priv->bt_traffic_load);
486
487         switch (priv->bt_traffic_load) {
488         case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
489                 if (priv->bt_status)
490                         smps_request = IEEE80211_SMPS_DYNAMIC;
491                 else
492                         smps_request = IEEE80211_SMPS_AUTOMATIC;
493                 break;
494         case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
495                 smps_request = IEEE80211_SMPS_DYNAMIC;
496                 break;
497         case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
498         case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
499                 smps_request = IEEE80211_SMPS_STATIC;
500                 break;
501         default:
502                 IWL_ERR(priv, "Invalid BT traffic load: %d\n",
503                         priv->bt_traffic_load);
504                 break;
505         }
506
507         mutex_lock(&priv->mutex);
508
509         /*
510          * We can not send command to firmware while scanning. When the scan
511          * complete we will schedule this work again. We do check with mutex
512          * locked to prevent new scan request to arrive. We do not check
513          * STATUS_SCANNING to avoid race when queue_work two times from
514          * different notifications, but quit and not perform any work at all.
515          */
516         if (test_bit(STATUS_SCAN_HW, &priv->status))
517                 goto out;
518
519         iwl_update_chain_flags(priv);
520
521         if (smps_request != -1) {
522                 priv->current_ht_config.smps = smps_request;
523                 for_each_context(priv, ctx) {
524                         if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
525                                 ieee80211_request_smps(ctx->vif, smps_request);
526                 }
527         }
528
529         /*
530          * Dynamic PS poll related functionality. Adjust RSSI measurements if
531          * necessary.
532          */
533         iwlagn_bt_coex_rssi_monitor(priv);
534 out:
535         mutex_unlock(&priv->mutex);
536 }
537
538 /*
539  * If BT sco traffic, and RSSI monitor is enabled, move measurements to the
540  * correct interface or disable it if this is the last interface to be
541  * removed.
542  */
543 void iwlagn_bt_coex_rssi_monitor(struct iwl_priv *priv)
544 {
545         if (priv->bt_is_sco &&
546             priv->bt_traffic_load == IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS)
547                 iwlagn_bt_adjust_rssi_monitor(priv, true);
548         else
549                 iwlagn_bt_adjust_rssi_monitor(priv, false);
550 }
551
552 static void iwlagn_print_uartmsg(struct iwl_priv *priv,
553                                 struct iwl_bt_uart_msg *uart_msg)
554 {
555         IWL_DEBUG_COEX(priv, "Message Type = 0x%X, SSN = 0x%X, "
556                         "Update Req = 0x%X\n",
557                 (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
558                         BT_UART_MSG_FRAME1MSGTYPE_POS,
559                 (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
560                         BT_UART_MSG_FRAME1SSN_POS,
561                 (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
562                         BT_UART_MSG_FRAME1UPDATEREQ_POS);
563
564         IWL_DEBUG_COEX(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
565                         "Chl_SeqN = 0x%X, In band = 0x%X\n",
566                 (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
567                         BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
568                 (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
569                         BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
570                 (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
571                         BT_UART_MSG_FRAME2CHLSEQN_POS,
572                 (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
573                         BT_UART_MSG_FRAME2INBAND_POS);
574
575         IWL_DEBUG_COEX(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
576                         "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X\n",
577                 (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
578                         BT_UART_MSG_FRAME3SCOESCO_POS,
579                 (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
580                         BT_UART_MSG_FRAME3SNIFF_POS,
581                 (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
582                         BT_UART_MSG_FRAME3A2DP_POS,
583                 (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
584                         BT_UART_MSG_FRAME3ACL_POS,
585                 (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
586                         BT_UART_MSG_FRAME3MASTER_POS,
587                 (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
588                         BT_UART_MSG_FRAME3OBEX_POS);
589
590         IWL_DEBUG_COEX(priv, "Idle duration = 0x%X\n",
591                 (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
592                         BT_UART_MSG_FRAME4IDLEDURATION_POS);
593
594         IWL_DEBUG_COEX(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
595                         "eSCO Retransmissions = 0x%X\n",
596                 (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
597                         BT_UART_MSG_FRAME5TXACTIVITY_POS,
598                 (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
599                         BT_UART_MSG_FRAME5RXACTIVITY_POS,
600                 (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
601                         BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
602
603         IWL_DEBUG_COEX(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X\n",
604                 (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
605                         BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
606                 (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
607                         BT_UART_MSG_FRAME6DISCOVERABLE_POS);
608
609         IWL_DEBUG_COEX(priv, "Sniff Activity = 0x%X, Page = "
610                         "0x%X, Inquiry = 0x%X, Connectable = 0x%X\n",
611                 (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
612                         BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
613                 (BT_UART_MSG_FRAME7PAGE_MSK & uart_msg->frame7) >>
614                         BT_UART_MSG_FRAME7PAGE_POS,
615                 (BT_UART_MSG_FRAME7INQUIRY_MSK & uart_msg->frame7) >>
616                         BT_UART_MSG_FRAME7INQUIRY_POS,
617                 (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
618                         BT_UART_MSG_FRAME7CONNECTABLE_POS);
619 }
620
621 static void iwlagn_set_kill_msk(struct iwl_priv *priv,
622                                 struct iwl_bt_uart_msg *uart_msg)
623 {
624         u8 kill_msk;
625         static const __le32 bt_kill_ack_msg[2] = {
626                 IWLAGN_BT_KILL_ACK_MASK_DEFAULT,
627                 IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
628         static const __le32 bt_kill_cts_msg[2] = {
629                 IWLAGN_BT_KILL_CTS_MASK_DEFAULT,
630                 IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
631
632         kill_msk = (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3)
633                 ? 1 : 0;
634         if (priv->kill_ack_mask != bt_kill_ack_msg[kill_msk] ||
635             priv->kill_cts_mask != bt_kill_cts_msg[kill_msk]) {
636                 priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
637                 priv->kill_ack_mask = bt_kill_ack_msg[kill_msk];
638                 priv->bt_valid |= IWLAGN_BT_VALID_KILL_CTS_MASK;
639                 priv->kill_cts_mask = bt_kill_cts_msg[kill_msk];
640
641                 /* schedule to send runtime bt_config */
642                 queue_work(priv->workqueue, &priv->bt_runtime_config);
643         }
644 }
645
646 int iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
647                                   struct iwl_rx_cmd_buffer *rxb,
648                                   struct iwl_device_cmd *cmd)
649 {
650         struct iwl_rx_packet *pkt = rxb_addr(rxb);
651         struct iwl_bt_coex_profile_notif *coex = (void *)pkt->data;
652         struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
653
654         if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
655                 /* bt coex disabled */
656                 return 0;
657         }
658
659         IWL_DEBUG_COEX(priv, "BT Coex notification:\n");
660         IWL_DEBUG_COEX(priv, "    status: %d\n", coex->bt_status);
661         IWL_DEBUG_COEX(priv, "    traffic load: %d\n", coex->bt_traffic_load);
662         IWL_DEBUG_COEX(priv, "    CI compliance: %d\n",
663                         coex->bt_ci_compliance);
664         iwlagn_print_uartmsg(priv, uart_msg);
665
666         priv->last_bt_traffic_load = priv->bt_traffic_load;
667         priv->bt_is_sco = iwlagn_bt_traffic_is_sco(uart_msg);
668
669         if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
670                 if (priv->bt_status != coex->bt_status ||
671                     priv->last_bt_traffic_load != coex->bt_traffic_load) {
672                         if (coex->bt_status) {
673                                 /* BT on */
674                                 if (!priv->bt_ch_announce)
675                                         priv->bt_traffic_load =
676                                                 IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
677                                 else
678                                         priv->bt_traffic_load =
679                                                 coex->bt_traffic_load;
680                         } else {
681                                 /* BT off */
682                                 priv->bt_traffic_load =
683                                         IWL_BT_COEX_TRAFFIC_LOAD_NONE;
684                         }
685                         priv->bt_status = coex->bt_status;
686                         queue_work(priv->workqueue,
687                                    &priv->bt_traffic_change_work);
688                 }
689         }
690
691         iwlagn_set_kill_msk(priv, uart_msg);
692
693         /* FIXME: based on notification, adjust the prio_boost */
694
695         priv->bt_ci_compliance = coex->bt_ci_compliance;
696         return 0;
697 }
698
699 void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
700 {
701         priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
702                 iwlagn_bt_coex_profile_notif;
703 }
704
705 void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
706 {
707         INIT_WORK(&priv->bt_traffic_change_work,
708                   iwlagn_bt_traffic_change_work);
709 }
710
711 void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
712 {
713         cancel_work_sync(&priv->bt_traffic_change_work);
714 }
715
716 static bool is_single_rx_stream(struct iwl_priv *priv)
717 {
718         return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
719                priv->current_ht_config.single_chain_sufficient;
720 }
721
722 #define IWL_NUM_RX_CHAINS_MULTIPLE      3
723 #define IWL_NUM_RX_CHAINS_SINGLE        2
724 #define IWL_NUM_IDLE_CHAINS_DUAL        2
725 #define IWL_NUM_IDLE_CHAINS_SINGLE      1
726
727 /*
728  * Determine how many receiver/antenna chains to use.
729  *
730  * More provides better reception via diversity.  Fewer saves power
731  * at the expense of throughput, but only when not in powersave to
732  * start with.
733  *
734  * MIMO (dual stream) requires at least 2, but works better with 3.
735  * This does not determine *which* chains to use, just how many.
736  */
737 static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
738 {
739         if (cfg(priv)->bt_params &&
740             cfg(priv)->bt_params->advanced_bt_coexist &&
741             (priv->bt_full_concurrent ||
742              priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
743                 /*
744                  * only use chain 'A' in bt high traffic load or
745                  * full concurrency mode
746                  */
747                 return IWL_NUM_RX_CHAINS_SINGLE;
748         }
749         /* # of Rx chains to use when expecting MIMO. */
750         if (is_single_rx_stream(priv))
751                 return IWL_NUM_RX_CHAINS_SINGLE;
752         else
753                 return IWL_NUM_RX_CHAINS_MULTIPLE;
754 }
755
756 /*
757  * When we are in power saving mode, unless device support spatial
758  * multiplexing power save, use the active count for rx chain count.
759  */
760 static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
761 {
762         /* # Rx chains when idling, depending on SMPS mode */
763         switch (priv->current_ht_config.smps) {
764         case IEEE80211_SMPS_STATIC:
765         case IEEE80211_SMPS_DYNAMIC:
766                 return IWL_NUM_IDLE_CHAINS_SINGLE;
767         case IEEE80211_SMPS_AUTOMATIC:
768         case IEEE80211_SMPS_OFF:
769                 return active_cnt;
770         default:
771                 WARN(1, "invalid SMPS mode %d",
772                      priv->current_ht_config.smps);
773                 return active_cnt;
774         }
775 }
776
777 /* up to 4 chains */
778 static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
779 {
780         u8 res;
781         res = (chain_bitmap & BIT(0)) >> 0;
782         res += (chain_bitmap & BIT(1)) >> 1;
783         res += (chain_bitmap & BIT(2)) >> 2;
784         res += (chain_bitmap & BIT(3)) >> 3;
785         return res;
786 }
787
788 /**
789  * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
790  *
791  * Selects how many and which Rx receivers/antennas/chains to use.
792  * This should not be used for scan command ... it puts data in wrong place.
793  */
794 void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
795 {
796         bool is_single = is_single_rx_stream(priv);
797         bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->status);
798         u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
799         u32 active_chains;
800         u16 rx_chain;
801
802         /* Tell uCode which antennas are actually connected.
803          * Before first association, we assume all antennas are connected.
804          * Just after first association, iwl_chain_noise_calibration()
805          *    checks which antennas actually *are* connected. */
806         if (priv->chain_noise_data.active_chains)
807                 active_chains = priv->chain_noise_data.active_chains;
808         else
809                 active_chains = priv->hw_params.valid_rx_ant;
810
811         if (cfg(priv)->bt_params &&
812             cfg(priv)->bt_params->advanced_bt_coexist &&
813             (priv->bt_full_concurrent ||
814              priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
815                 /*
816                  * only use chain 'A' in bt high traffic load or
817                  * full concurrency mode
818                  */
819                 active_chains = first_antenna(active_chains);
820         }
821
822         rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
823
824         /* How many receivers should we use? */
825         active_rx_cnt = iwl_get_active_rx_chain_count(priv);
826         idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
827
828
829         /* correct rx chain count according hw settings
830          * and chain noise calibration
831          */
832         valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
833         if (valid_rx_cnt < active_rx_cnt)
834                 active_rx_cnt = valid_rx_cnt;
835
836         if (valid_rx_cnt < idle_rx_cnt)
837                 idle_rx_cnt = valid_rx_cnt;
838
839         rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
840         rx_chain |= idle_rx_cnt  << RXON_RX_CHAIN_CNT_POS;
841
842         ctx->staging.rx_chain = cpu_to_le16(rx_chain);
843
844         if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
845                 ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
846         else
847                 ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
848
849         IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
850                         ctx->staging.rx_chain,
851                         active_rx_cnt, idle_rx_cnt);
852
853         WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
854                 active_rx_cnt < idle_rx_cnt);
855 }
856
857 u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
858 {
859         int i;
860         u8 ind = ant;
861
862         if (priv->band == IEEE80211_BAND_2GHZ &&
863             priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
864                 return 0;
865
866         for (i = 0; i < RATE_ANT_NUM - 1; i++) {
867                 ind = (ind + 1) < RATE_ANT_NUM ?  ind + 1 : 0;
868                 if (valid & BIT(ind))
869                         return ind;
870         }
871         return ant;
872 }
873
874 #ifdef CONFIG_PM_SLEEP
875 static void iwlagn_convert_p1k(u16 *p1k, __le16 *out)
876 {
877         int i;
878
879         for (i = 0; i < IWLAGN_P1K_SIZE; i++)
880                 out[i] = cpu_to_le16(p1k[i]);
881 }
882
883 struct wowlan_key_data {
884         struct iwl_rxon_context *ctx;
885         struct iwlagn_wowlan_rsc_tsc_params_cmd *rsc_tsc;
886         struct iwlagn_wowlan_tkip_params_cmd *tkip;
887         const u8 *bssid;
888         bool error, use_rsc_tsc, use_tkip;
889 };
890
891
892 static void iwlagn_wowlan_program_keys(struct ieee80211_hw *hw,
893                                struct ieee80211_vif *vif,
894                                struct ieee80211_sta *sta,
895                                struct ieee80211_key_conf *key,
896                                void *_data)
897 {
898         struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
899         struct wowlan_key_data *data = _data;
900         struct iwl_rxon_context *ctx = data->ctx;
901         struct aes_sc *aes_sc, *aes_tx_sc = NULL;
902         struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
903         struct iwlagn_p1k_cache *rx_p1ks;
904         u8 *rx_mic_key;
905         struct ieee80211_key_seq seq;
906         u32 cur_rx_iv32 = 0;
907         u16 p1k[IWLAGN_P1K_SIZE];
908         int ret, i;
909
910         mutex_lock(&priv->mutex);
911
912         if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
913              key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
914              !sta && !ctx->key_mapping_keys)
915                 ret = iwl_set_default_wep_key(priv, ctx, key);
916         else
917                 ret = iwl_set_dynamic_key(priv, ctx, key, sta);
918
919         if (ret) {
920                 IWL_ERR(priv, "Error setting key during suspend!\n");
921                 data->error = true;
922         }
923
924         switch (key->cipher) {
925         case WLAN_CIPHER_SUITE_TKIP:
926                 if (sta) {
927                         tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
928                         tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
929
930                         rx_p1ks = data->tkip->rx_uni;
931
932                         ieee80211_get_key_tx_seq(key, &seq);
933                         tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
934                         tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
935
936                         ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
937                         iwlagn_convert_p1k(p1k, data->tkip->tx.p1k);
938
939                         memcpy(data->tkip->mic_keys.tx,
940                                &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
941                                IWLAGN_MIC_KEY_SIZE);
942
943                         rx_mic_key = data->tkip->mic_keys.rx_unicast;
944                 } else {
945                         tkip_sc =
946                                 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
947                         rx_p1ks = data->tkip->rx_multi;
948                         rx_mic_key = data->tkip->mic_keys.rx_mcast;
949                 }
950
951                 /*
952                  * For non-QoS this relies on the fact that both the uCode and
953                  * mac80211 use TID 0 (as they need to to avoid replay attacks)
954                  * for checking the IV in the frames.
955                  */
956                 for (i = 0; i < IWLAGN_NUM_RSC; i++) {
957                         ieee80211_get_key_rx_seq(key, i, &seq);
958                         tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
959                         tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
960                         /* wrapping isn't allowed, AP must rekey */
961                         if (seq.tkip.iv32 > cur_rx_iv32)
962                                 cur_rx_iv32 = seq.tkip.iv32;
963                 }
964
965                 ieee80211_get_tkip_rx_p1k(key, data->bssid, cur_rx_iv32, p1k);
966                 iwlagn_convert_p1k(p1k, rx_p1ks[0].p1k);
967                 ieee80211_get_tkip_rx_p1k(key, data->bssid,
968                                           cur_rx_iv32 + 1, p1k);
969                 iwlagn_convert_p1k(p1k, rx_p1ks[1].p1k);
970
971                 memcpy(rx_mic_key,
972                        &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
973                        IWLAGN_MIC_KEY_SIZE);
974
975                 data->use_tkip = true;
976                 data->use_rsc_tsc = true;
977                 break;
978         case WLAN_CIPHER_SUITE_CCMP:
979                 if (sta) {
980                         u8 *pn = seq.ccmp.pn;
981
982                         aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
983                         aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
984
985                         ieee80211_get_key_tx_seq(key, &seq);
986                         aes_tx_sc->pn = cpu_to_le64(
987                                         (u64)pn[5] |
988                                         ((u64)pn[4] << 8) |
989                                         ((u64)pn[3] << 16) |
990                                         ((u64)pn[2] << 24) |
991                                         ((u64)pn[1] << 32) |
992                                         ((u64)pn[0] << 40));
993                 } else
994                         aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
995
996                 /*
997                  * For non-QoS this relies on the fact that both the uCode and
998                  * mac80211 use TID 0 for checking the IV in the frames.
999                  */
1000                 for (i = 0; i < IWLAGN_NUM_RSC; i++) {
1001                         u8 *pn = seq.ccmp.pn;
1002
1003                         ieee80211_get_key_rx_seq(key, i, &seq);
1004                         aes_sc->pn = cpu_to_le64(
1005                                         (u64)pn[5] |
1006                                         ((u64)pn[4] << 8) |
1007                                         ((u64)pn[3] << 16) |
1008                                         ((u64)pn[2] << 24) |
1009                                         ((u64)pn[1] << 32) |
1010                                         ((u64)pn[0] << 40));
1011                 }
1012                 data->use_rsc_tsc = true;
1013                 break;
1014         }
1015
1016         mutex_unlock(&priv->mutex);
1017 }
1018
1019 int iwlagn_send_patterns(struct iwl_priv *priv,
1020                         struct cfg80211_wowlan *wowlan)
1021 {
1022         struct iwlagn_wowlan_patterns_cmd *pattern_cmd;
1023         struct iwl_host_cmd cmd = {
1024                 .id = REPLY_WOWLAN_PATTERNS,
1025                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1026                 .flags = CMD_SYNC,
1027         };
1028         int i, err;
1029
1030         if (!wowlan->n_patterns)
1031                 return 0;
1032
1033         cmd.len[0] = sizeof(*pattern_cmd) +
1034                 wowlan->n_patterns * sizeof(struct iwlagn_wowlan_pattern);
1035
1036         pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
1037         if (!pattern_cmd)
1038                 return -ENOMEM;
1039
1040         pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
1041
1042         for (i = 0; i < wowlan->n_patterns; i++) {
1043                 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
1044
1045                 memcpy(&pattern_cmd->patterns[i].mask,
1046                         wowlan->patterns[i].mask, mask_len);
1047                 memcpy(&pattern_cmd->patterns[i].pattern,
1048                         wowlan->patterns[i].pattern,
1049                         wowlan->patterns[i].pattern_len);
1050                 pattern_cmd->patterns[i].mask_size = mask_len;
1051                 pattern_cmd->patterns[i].pattern_size =
1052                         wowlan->patterns[i].pattern_len;
1053         }
1054
1055         cmd.data[0] = pattern_cmd;
1056         err = iwl_dvm_send_cmd(priv, &cmd);
1057         kfree(pattern_cmd);
1058         return err;
1059 }
1060
1061 int iwlagn_suspend(struct iwl_priv *priv, struct cfg80211_wowlan *wowlan)
1062 {
1063         struct iwlagn_wowlan_wakeup_filter_cmd wakeup_filter_cmd;
1064         struct iwl_rxon_cmd rxon;
1065         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1066         struct iwlagn_wowlan_kek_kck_material_cmd kek_kck_cmd;
1067         struct iwlagn_wowlan_tkip_params_cmd tkip_cmd = {};
1068         struct iwlagn_d3_config_cmd d3_cfg_cmd = {};
1069         struct wowlan_key_data key_data = {
1070                 .ctx = ctx,
1071                 .bssid = ctx->active.bssid_addr,
1072                 .use_rsc_tsc = false,
1073                 .tkip = &tkip_cmd,
1074                 .use_tkip = false,
1075         };
1076         int ret, i;
1077         u16 seq;
1078
1079         key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
1080         if (!key_data.rsc_tsc)
1081                 return -ENOMEM;
1082
1083         memset(&wakeup_filter_cmd, 0, sizeof(wakeup_filter_cmd));
1084
1085         /*
1086          * We know the last used seqno, and the uCode expects to know that
1087          * one, it will increment before TX.
1088          */
1089         seq = le16_to_cpu(priv->last_seq_ctl) & IEEE80211_SCTL_SEQ;
1090         wakeup_filter_cmd.non_qos_seq = cpu_to_le16(seq);
1091
1092         /*
1093          * For QoS counters, we store the one to use next, so subtract 0x10
1094          * since the uCode will add 0x10 before using the value.
1095          */
1096         for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1097                 seq = priv->tid_data[IWL_AP_ID][i].seq_number;
1098                 seq -= 0x10;
1099                 wakeup_filter_cmd.qos_seq[i] = cpu_to_le16(seq);
1100         }
1101
1102         if (wowlan->disconnect)
1103                 wakeup_filter_cmd.enabled |=
1104                         cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
1105                                     IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE);
1106         if (wowlan->magic_pkt)
1107                 wakeup_filter_cmd.enabled |=
1108                         cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET);
1109         if (wowlan->gtk_rekey_failure)
1110                 wakeup_filter_cmd.enabled |=
1111                         cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
1112         if (wowlan->eap_identity_req)
1113                 wakeup_filter_cmd.enabled |=
1114                         cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ);
1115         if (wowlan->four_way_handshake)
1116                 wakeup_filter_cmd.enabled |=
1117                         cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
1118         if (wowlan->n_patterns)
1119                 wakeup_filter_cmd.enabled |=
1120                         cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH);
1121
1122         if (wowlan->rfkill_release)
1123                 d3_cfg_cmd.wakeup_flags |=
1124                         cpu_to_le32(IWLAGN_D3_WAKEUP_RFKILL);
1125
1126         iwl_scan_cancel_timeout(priv, 200);
1127
1128         memcpy(&rxon, &ctx->active, sizeof(rxon));
1129
1130         priv->ucode_loaded = false;
1131         iwl_trans_stop_device(trans(priv));
1132
1133         priv->wowlan = true;
1134
1135         ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_WOWLAN);
1136         if (ret)
1137                 goto out;
1138
1139         /* now configure WoWLAN ucode */
1140         ret = iwl_alive_start(priv);
1141         if (ret)
1142                 goto out;
1143
1144         memcpy(&ctx->staging, &rxon, sizeof(rxon));
1145         ret = iwlagn_commit_rxon(priv, ctx);
1146         if (ret)
1147                 goto out;
1148
1149         ret = iwl_power_update_mode(priv, true);
1150         if (ret)
1151                 goto out;
1152
1153         if (!iwlagn_mod_params.sw_crypto) {
1154                 /* mark all keys clear */
1155                 priv->ucode_key_table = 0;
1156                 ctx->key_mapping_keys = 0;
1157
1158                 /*
1159                  * This needs to be unlocked due to lock ordering
1160                  * constraints. Since we're in the suspend path
1161                  * that isn't really a problem though.
1162                  */
1163                 mutex_unlock(&priv->mutex);
1164                 ieee80211_iter_keys(priv->hw, ctx->vif,
1165                                     iwlagn_wowlan_program_keys,
1166                                     &key_data);
1167                 mutex_lock(&priv->mutex);
1168                 if (key_data.error) {
1169                         ret = -EIO;
1170                         goto out;
1171                 }
1172
1173                 if (key_data.use_rsc_tsc) {
1174                         struct iwl_host_cmd rsc_tsc_cmd = {
1175                                 .id = REPLY_WOWLAN_TSC_RSC_PARAMS,
1176                                 .flags = CMD_SYNC,
1177                                 .data[0] = key_data.rsc_tsc,
1178                                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1179                                 .len[0] = sizeof(*key_data.rsc_tsc),
1180                         };
1181
1182                         ret = iwl_dvm_send_cmd(priv, &rsc_tsc_cmd);
1183                         if (ret)
1184                                 goto out;
1185                 }
1186
1187                 if (key_data.use_tkip) {
1188                         ret = iwl_dvm_send_cmd_pdu(priv,
1189                                                  REPLY_WOWLAN_TKIP_PARAMS,
1190                                                  CMD_SYNC, sizeof(tkip_cmd),
1191                                                  &tkip_cmd);
1192                         if (ret)
1193                                 goto out;
1194                 }
1195
1196                 if (priv->have_rekey_data) {
1197                         memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
1198                         memcpy(kek_kck_cmd.kck, priv->kck, NL80211_KCK_LEN);
1199                         kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
1200                         memcpy(kek_kck_cmd.kek, priv->kek, NL80211_KEK_LEN);
1201                         kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1202                         kek_kck_cmd.replay_ctr = priv->replay_ctr;
1203
1204                         ret = iwl_dvm_send_cmd_pdu(priv,
1205                                                  REPLY_WOWLAN_KEK_KCK_MATERIAL,
1206                                                  CMD_SYNC, sizeof(kek_kck_cmd),
1207                                                  &kek_kck_cmd);
1208                         if (ret)
1209                                 goto out;
1210                 }
1211         }
1212
1213         ret = iwl_dvm_send_cmd_pdu(priv, REPLY_D3_CONFIG, CMD_SYNC,
1214                                      sizeof(d3_cfg_cmd), &d3_cfg_cmd);
1215         if (ret)
1216                 goto out;
1217
1218         ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WOWLAN_WAKEUP_FILTER,
1219                                  CMD_SYNC, sizeof(wakeup_filter_cmd),
1220                                  &wakeup_filter_cmd);
1221         if (ret)
1222                 goto out;
1223
1224         ret = iwlagn_send_patterns(priv, wowlan);
1225  out:
1226         kfree(key_data.rsc_tsc);
1227         return ret;
1228 }
1229 #endif
1230
1231 int iwl_dvm_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
1232 {
1233         if (iwl_is_rfkill(priv) || iwl_is_ctkill(priv)) {
1234                 IWL_WARN(priv, "Not sending command - %s KILL\n",
1235                          iwl_is_rfkill(priv) ? "RF" : "CT");
1236                 return -EIO;
1237         }
1238
1239         if (test_bit(STATUS_FW_ERROR, &priv->status)) {
1240                 IWL_ERR(priv, "Command %s failed: FW Error\n",
1241                         get_cmd_string(cmd->id));
1242                 return -EIO;
1243         }
1244
1245         /*
1246          * Synchronous commands from this op-mode must hold
1247          * the mutex, this ensures we don't try to send two
1248          * (or more) synchronous commands at a time.
1249          */
1250         if (cmd->flags & CMD_SYNC)
1251                 lockdep_assert_held(&priv->mutex);
1252
1253         if (priv->ucode_owner == IWL_OWNERSHIP_TM &&
1254             !(cmd->flags & CMD_ON_DEMAND)) {
1255                 IWL_DEBUG_HC(priv, "tm own the uCode, no regular hcmd send\n");
1256                 return -EIO;
1257         }
1258
1259         return iwl_trans_send_cmd(trans(priv), cmd);
1260 }
1261
1262 int iwl_dvm_send_cmd_pdu(struct iwl_priv *priv, u8 id,
1263                          u32 flags, u16 len, const void *data)
1264 {
1265         struct iwl_host_cmd cmd = {
1266                 .id = id,
1267                 .len = { len, },
1268                 .data = { data, },
1269                 .flags = flags,
1270         };
1271
1272         return iwl_dvm_send_cmd(priv, &cmd);
1273 }