Merge branch 'topic/cleanup' into for-linus
[pandora-kernel.git] / drivers / net / wireless / iwmc3200wifi / commands.c
1 /*
2  * Intel Wireless Multicomm 3200 WiFi driver
3  *
4  * Copyright (C) 2009 Intel Corporation. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *   * Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *   * Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in
14  *     the documentation and/or other materials provided with the
15  *     distribution.
16  *   * Neither the name of Intel Corporation nor the names of its
17  *     contributors may be used to endorse or promote products derived
18  *     from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *
33  * Intel Corporation <ilw@linux.intel.com>
34  * Samuel Ortiz <samuel.ortiz@intel.com>
35  * Zhu Yi <yi.zhu@intel.com>
36  *
37  */
38
39 #include <linux/kernel.h>
40 #include <linux/wireless.h>
41 #include <linux/etherdevice.h>
42 #include <linux/ieee80211.h>
43
44 #include "iwm.h"
45 #include "bus.h"
46 #include "hal.h"
47 #include "umac.h"
48 #include "commands.h"
49 #include "debug.h"
50
51 static int iwm_send_lmac_ptrough_cmd(struct iwm_priv *iwm,
52                                      u8 lmac_cmd_id,
53                                      const void *lmac_payload,
54                                      u16 lmac_payload_size,
55                                      u8 resp)
56 {
57         struct iwm_udma_wifi_cmd udma_cmd = UDMA_LMAC_INIT;
58         struct iwm_umac_cmd umac_cmd;
59         struct iwm_lmac_cmd lmac_cmd;
60
61         lmac_cmd.id = lmac_cmd_id;
62
63         umac_cmd.id = UMAC_CMD_OPCODE_WIFI_PASS_THROUGH;
64         umac_cmd.resp = resp;
65
66         return iwm_hal_send_host_cmd(iwm, &udma_cmd, &umac_cmd, &lmac_cmd,
67                                      lmac_payload, lmac_payload_size);
68 }
69
70 int iwm_send_wifi_if_cmd(struct iwm_priv *iwm, void *payload, u16 payload_size,
71                          bool resp)
72 {
73         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
74         struct iwm_umac_cmd umac_cmd;
75
76         umac_cmd.id = UMAC_CMD_OPCODE_WIFI_IF_WRAPPER;
77         umac_cmd.resp = resp;
78
79         return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
80                                      payload, payload_size);
81 }
82
83 static struct coex_event iwm_sta_xor_prio_tbl[COEX_EVENTS_NUM] =
84 {
85         {4, 3, 0, COEX_UNASSOC_IDLE_FLAGS},
86         {4, 3, 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
87         {4, 3, 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
88         {4, 3, 0, COEX_CALIBRATION_FLAGS},
89         {4, 3, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
90         {4, 3, 0, COEX_CONNECTION_ESTAB_FLAGS},
91         {4, 3, 0, COEX_ASSOCIATED_IDLE_FLAGS},
92         {4, 3, 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
93         {4, 3, 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
94         {4, 3, 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
95         {6, 3, 0, COEX_XOR_RF_ON_FLAGS},
96         {4, 3, 0, COEX_RF_OFF_FLAGS},
97         {6, 6, 0, COEX_STAND_ALONE_DEBUG_FLAGS},
98         {4, 3, 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
99         {4, 3, 0, COEX_RSRVD1_FLAGS},
100         {4, 3, 0, COEX_RSRVD2_FLAGS}
101 };
102
103 static struct coex_event iwm_sta_cm_prio_tbl[COEX_EVENTS_NUM] =
104 {
105         {1, 1, 0, COEX_UNASSOC_IDLE_FLAGS},
106         {4, 3, 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
107         {3, 3, 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
108         {5, 5, 0, COEX_CALIBRATION_FLAGS},
109         {4, 4, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
110         {5, 4, 0, COEX_CONNECTION_ESTAB_FLAGS},
111         {4, 4, 0, COEX_ASSOCIATED_IDLE_FLAGS},
112         {4, 4, 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
113         {4, 4, 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
114         {4, 4, 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
115         {1, 1, 0, COEX_RF_ON_FLAGS},
116         {1, 1, 0, COEX_RF_OFF_FLAGS},
117         {6, 6, 0, COEX_STAND_ALONE_DEBUG_FLAGS},
118         {5, 4, 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
119         {1, 1, 0, COEX_RSRVD1_FLAGS},
120         {1, 1, 0, COEX_RSRVD2_FLAGS}
121 };
122
123 int iwm_send_prio_table(struct iwm_priv *iwm)
124 {
125         struct iwm_coex_prio_table_cmd coex_table_cmd;
126         u32 coex_enabled, mode_enabled;
127
128         memset(&coex_table_cmd, 0, sizeof(struct iwm_coex_prio_table_cmd));
129
130         coex_table_cmd.flags = COEX_FLAGS_STA_TABLE_VALID_MSK;
131
132         switch (iwm->conf.coexist_mode) {
133         case COEX_MODE_XOR:
134         case COEX_MODE_CM:
135                 coex_enabled = 1;
136                 break;
137         default:
138                 coex_enabled = 0;
139                 break;
140         }
141
142         switch (iwm->conf.mode) {
143         case UMAC_MODE_BSS:
144         case UMAC_MODE_IBSS:
145                 mode_enabled = 1;
146                 break;
147         default:
148                 mode_enabled = 0;
149                 break;
150         }
151
152         if (coex_enabled && mode_enabled) {
153                 coex_table_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK |
154                                         COEX_FLAGS_ASSOC_WAKEUP_UMASK_MSK |
155                                         COEX_FLAGS_UNASSOC_WAKEUP_UMASK_MSK;
156
157                 switch (iwm->conf.coexist_mode) {
158                 case COEX_MODE_XOR:
159                         memcpy(coex_table_cmd.sta_prio, iwm_sta_xor_prio_tbl,
160                                sizeof(iwm_sta_xor_prio_tbl));
161                         break;
162                 case COEX_MODE_CM:
163                         memcpy(coex_table_cmd.sta_prio, iwm_sta_cm_prio_tbl,
164                                sizeof(iwm_sta_cm_prio_tbl));
165                         break;
166                 default:
167                         IWM_ERR(iwm, "Invalid coex_mode 0x%x\n",
168                                 iwm->conf.coexist_mode);
169                         break;
170                 }
171         } else
172                 IWM_WARN(iwm, "coexistense disabled\n");
173
174         return iwm_send_lmac_ptrough_cmd(iwm, COEX_PRIORITY_TABLE_CMD,
175                                 &coex_table_cmd,
176                                 sizeof(struct iwm_coex_prio_table_cmd), 1);
177 }
178
179 int iwm_send_init_calib_cfg(struct iwm_priv *iwm, u8 calib_requested)
180 {
181         struct iwm_lmac_cal_cfg_cmd cal_cfg_cmd;
182
183         memset(&cal_cfg_cmd, 0, sizeof(struct iwm_lmac_cal_cfg_cmd));
184
185         cal_cfg_cmd.ucode_cfg.init.enable = cpu_to_le32(calib_requested);
186         cal_cfg_cmd.ucode_cfg.init.start = cpu_to_le32(calib_requested);
187         cal_cfg_cmd.ucode_cfg.init.send_res = cpu_to_le32(calib_requested);
188         cal_cfg_cmd.ucode_cfg.flags =
189                 cpu_to_le32(CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_AFTER_MSK);
190
191         return iwm_send_lmac_ptrough_cmd(iwm, CALIBRATION_CFG_CMD, &cal_cfg_cmd,
192                                 sizeof(struct iwm_lmac_cal_cfg_cmd), 1);
193 }
194
195 int iwm_send_periodic_calib_cfg(struct iwm_priv *iwm, u8 calib_requested)
196 {
197         struct iwm_lmac_cal_cfg_cmd cal_cfg_cmd;
198
199         memset(&cal_cfg_cmd, 0, sizeof(struct iwm_lmac_cal_cfg_cmd));
200
201         cal_cfg_cmd.ucode_cfg.periodic.enable = cpu_to_le32(calib_requested);
202         cal_cfg_cmd.ucode_cfg.periodic.start = cpu_to_le32(calib_requested);
203
204         return iwm_send_lmac_ptrough_cmd(iwm, CALIBRATION_CFG_CMD, &cal_cfg_cmd,
205                                 sizeof(struct iwm_lmac_cal_cfg_cmd), 0);
206 }
207
208 int iwm_store_rxiq_calib_result(struct iwm_priv *iwm)
209 {
210         struct iwm_calib_rxiq *rxiq;
211         u8 *eeprom_rxiq = iwm_eeprom_access(iwm, IWM_EEPROM_CALIB_RXIQ);
212         int grplen = sizeof(struct iwm_calib_rxiq_group);
213
214         rxiq = kzalloc(sizeof(struct iwm_calib_rxiq), GFP_KERNEL);
215         if (!rxiq) {
216                 IWM_ERR(iwm, "Couldn't alloc memory for RX IQ\n");
217                 return -ENOMEM;
218         }
219
220         eeprom_rxiq = iwm_eeprom_access(iwm, IWM_EEPROM_CALIB_RXIQ);
221         if (IS_ERR(eeprom_rxiq)) {
222                 IWM_ERR(iwm, "Couldn't access EEPROM RX IQ entry\n");
223                 kfree(rxiq);
224                 return PTR_ERR(eeprom_rxiq);
225         }
226
227         iwm->calib_res[SHILOH_PHY_CALIBRATE_RX_IQ_CMD].buf = (u8 *)rxiq;
228         iwm->calib_res[SHILOH_PHY_CALIBRATE_RX_IQ_CMD].size = sizeof(*rxiq);
229
230         rxiq->hdr.opcode = SHILOH_PHY_CALIBRATE_RX_IQ_CMD;
231         rxiq->hdr.first_grp = 0;
232         rxiq->hdr.grp_num = 1;
233         rxiq->hdr.all_data_valid = 1;
234
235         memcpy(&rxiq->group[0], eeprom_rxiq, 4 * grplen);
236         memcpy(&rxiq->group[4], eeprom_rxiq + 6 * grplen, grplen);
237
238         return 0;
239 }
240
241 int iwm_send_calib_results(struct iwm_priv *iwm)
242 {
243         int i, ret = 0;
244
245         for (i = PHY_CALIBRATE_OPCODES_NUM; i < CALIBRATION_CMD_NUM; i++) {
246                 if (test_bit(i - PHY_CALIBRATE_OPCODES_NUM,
247                              &iwm->calib_done_map)) {
248                         IWM_DBG_CMD(iwm, DBG,
249                                     "Send calibration %d result\n", i);
250                         ret |= iwm_send_lmac_ptrough_cmd(iwm,
251                                         REPLY_PHY_CALIBRATION_CMD,
252                                         iwm->calib_res[i].buf,
253                                         iwm->calib_res[i].size, 0);
254
255                         kfree(iwm->calib_res[i].buf);
256                         iwm->calib_res[i].buf = NULL;
257                         iwm->calib_res[i].size = 0;
258                 }
259         }
260
261         return ret;
262 }
263
264 int iwm_send_umac_reset(struct iwm_priv *iwm, __le32 reset_flags, bool resp)
265 {
266         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
267         struct iwm_umac_cmd umac_cmd;
268         struct iwm_umac_cmd_reset reset;
269
270         reset.flags = reset_flags;
271
272         umac_cmd.id = UMAC_CMD_OPCODE_RESET;
273         umac_cmd.resp = resp;
274
275         return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &reset,
276                                      sizeof(struct iwm_umac_cmd_reset));
277 }
278
279 int iwm_umac_set_config_fix(struct iwm_priv *iwm, u16 tbl, u16 key, u32 value)
280 {
281         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
282         struct iwm_umac_cmd umac_cmd;
283         struct iwm_umac_cmd_set_param_fix param;
284
285         if ((tbl != UMAC_PARAM_TBL_CFG_FIX) &&
286             (tbl != UMAC_PARAM_TBL_FA_CFG_FIX))
287                 return -EINVAL;
288
289         umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_FIX;
290         umac_cmd.resp = 0;
291
292         param.tbl = cpu_to_le16(tbl);
293         param.key = cpu_to_le16(key);
294         param.value = cpu_to_le32(value);
295
296         return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &param,
297                                      sizeof(struct iwm_umac_cmd_set_param_fix));
298 }
299
300 int iwm_umac_set_config_var(struct iwm_priv *iwm, u16 key,
301                             void *payload, u16 payload_size)
302 {
303         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
304         struct iwm_umac_cmd umac_cmd;
305         struct iwm_umac_cmd_set_param_var *param_hdr;
306         u8 *param;
307         int ret;
308
309         param = kzalloc(payload_size +
310                         sizeof(struct iwm_umac_cmd_set_param_var), GFP_KERNEL);
311         if (!param) {
312                 IWM_ERR(iwm, "Couldn't allocate param\n");
313                 return -ENOMEM;
314         }
315
316         param_hdr = (struct iwm_umac_cmd_set_param_var *)param;
317
318         umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_VAR;
319         umac_cmd.resp = 0;
320
321         param_hdr->tbl = cpu_to_le16(UMAC_PARAM_TBL_CFG_VAR);
322         param_hdr->key = cpu_to_le16(key);
323         param_hdr->len = cpu_to_le16(payload_size);
324         memcpy(param + sizeof(struct iwm_umac_cmd_set_param_var),
325                payload, payload_size);
326
327         ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, param,
328                                     sizeof(struct iwm_umac_cmd_set_param_var) +
329                                     payload_size);
330         kfree(param);
331
332         return ret;
333 }
334
335 int iwm_send_umac_config(struct iwm_priv *iwm,
336                          __le32 reset_flags)
337 {
338         int ret;
339
340         /* Use UMAC default values */
341         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
342                                       CFG_POWER_INDEX, iwm->conf.power_index);
343         if (ret < 0)
344                 return ret;
345
346         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_FA_CFG_FIX,
347                                       CFG_FRAG_THRESHOLD,
348                                       iwm->conf.frag_threshold);
349         if (ret < 0)
350                 return ret;
351
352         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
353                                       CFG_RTS_THRESHOLD,
354                                       iwm->conf.rts_threshold);
355         if (ret < 0)
356                 return ret;
357
358         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
359                                       CFG_CTS_TO_SELF, iwm->conf.cts_to_self);
360         if (ret < 0)
361                 return ret;
362
363         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
364                                       CFG_COEX_MODE, iwm->conf.coexist_mode);
365         if (ret < 0)
366                 return ret;
367
368         /*
369         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
370                                       CFG_ASSOCIATION_TIMEOUT,
371                                       iwm->conf.assoc_timeout);
372         if (ret < 0)
373                 return ret;
374
375         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
376                                       CFG_ROAM_TIMEOUT,
377                                       iwm->conf.roam_timeout);
378         if (ret < 0)
379                 return ret;
380
381         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
382                                       CFG_WIRELESS_MODE,
383                                       WIRELESS_MODE_11A | WIRELESS_MODE_11G);
384         if (ret < 0)
385                 return ret;
386         */
387
388         ret = iwm_umac_set_config_var(iwm, CFG_NET_ADDR,
389                                       iwm_to_ndev(iwm)->dev_addr, ETH_ALEN);
390         if (ret < 0)
391                 return ret;
392
393         /* UMAC PM static configurations */
394         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
395                                       CFG_PM_LEGACY_RX_TIMEOUT, 0x12C);
396         if (ret < 0)
397                 return ret;
398
399         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
400                                       CFG_PM_LEGACY_TX_TIMEOUT, 0x15E);
401         if (ret < 0)
402                 return ret;
403
404         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
405                                       CFG_PM_CTRL_FLAGS, 0x30001);
406         if (ret < 0)
407                 return ret;
408
409         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
410                                       CFG_PM_KEEP_ALIVE_IN_BEACONS, 0x80);
411         if (ret < 0)
412                 return ret;
413
414         /* reset UMAC */
415         ret = iwm_send_umac_reset(iwm, reset_flags, 1);
416         if (ret < 0)
417                 return ret;
418
419         ret = iwm_notif_handle(iwm, UMAC_CMD_OPCODE_RESET, IWM_SRC_UMAC,
420                                WAIT_NOTIF_TIMEOUT);
421         if (ret) {
422                 IWM_ERR(iwm, "Wait for UMAC RESET timeout\n");
423                 return ret;
424         }
425
426         return ret;
427 }
428
429 int iwm_send_packet(struct iwm_priv *iwm, struct sk_buff *skb, int pool_id)
430 {
431         struct iwm_udma_wifi_cmd udma_cmd;
432         struct iwm_umac_cmd umac_cmd;
433         struct iwm_tx_info *tx_info = skb_to_tx_info(skb);
434
435         udma_cmd.eop = 1; /* always set eop for non-concatenated Tx */
436         udma_cmd.credit_group = pool_id;
437         udma_cmd.ra_tid = tx_info->sta << 4 | tx_info->tid;
438         udma_cmd.lmac_offset = 0;
439
440         umac_cmd.id = REPLY_TX;
441         umac_cmd.color = tx_info->color;
442         umac_cmd.resp = 0;
443
444         return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
445                                      skb->data, skb->len);
446 }
447
448 static int iwm_target_read(struct iwm_priv *iwm, __le32 address,
449                            u8 *response, u32 resp_size)
450 {
451         struct iwm_udma_nonwifi_cmd target_cmd;
452         struct iwm_nonwifi_cmd *cmd;
453         u16 seq_num;
454         int ret = 0;
455
456         target_cmd.opcode = UMAC_HDI_OUT_OPCODE_READ;
457         target_cmd.addr = address;
458         target_cmd.op1_sz = cpu_to_le32(resp_size);
459         target_cmd.op2 = 0;
460         target_cmd.handle_by_hw = 0;
461         target_cmd.resp = 1;
462         target_cmd.eop = 1;
463
464         ret = iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
465         if (ret < 0)
466                 IWM_ERR(iwm, "Couldn't send READ command\n");
467
468         /* When succeding, the send_target routine returns the seq number */
469         seq_num = ret;
470
471         ret = wait_event_interruptible_timeout(iwm->nonwifi_queue,
472                 (cmd = iwm_get_pending_nonwifi_cmd(iwm, seq_num,
473                                           UMAC_HDI_OUT_OPCODE_READ)) != NULL,
474                                                2 * HZ);
475
476         if (!ret) {
477                 IWM_ERR(iwm, "Didn't receive a target READ answer\n");
478                 return ret;
479         }
480
481         memcpy(response, cmd->buf.hdr + sizeof(struct iwm_udma_in_hdr),
482                resp_size);
483
484         kfree(cmd);
485
486         return ret;
487 }
488
489 int iwm_read_mac(struct iwm_priv *iwm, u8 *mac)
490 {
491         int ret;
492         u8 mac_align[ALIGN(ETH_ALEN, 8)];
493
494         ret = iwm_target_read(iwm, cpu_to_le32(WICO_MAC_ADDRESS_ADDR),
495                               mac_align, sizeof(mac_align));
496         if (ret < 0)
497                 return ret;
498
499         if (is_valid_ether_addr(mac_align))
500                 memcpy(mac, mac_align, ETH_ALEN);
501         else {
502                 IWM_ERR(iwm, "Invalid EEPROM MAC\n");
503                 memcpy(mac, iwm->conf.mac_addr, ETH_ALEN);
504                 get_random_bytes(&mac[3], 3);
505         }
506
507         return 0;
508 }
509
510 int iwm_set_tx_key(struct iwm_priv *iwm, u8 key_idx)
511 {
512         struct iwm_umac_tx_key_id tx_key_id;
513
514         if (!iwm->default_key || !iwm->default_key->in_use)
515                 return -EINVAL;
516
517         tx_key_id.hdr.oid = UMAC_WIFI_IF_CMD_GLOBAL_TX_KEY_ID;
518         tx_key_id.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_tx_key_id) -
519                                              sizeof(struct iwm_umac_wifi_if));
520
521         tx_key_id.key_idx = key_idx;
522
523         return iwm_send_wifi_if_cmd(iwm, &tx_key_id, sizeof(tx_key_id), 1);
524 }
525
526 static int iwm_check_profile(struct iwm_priv *iwm)
527 {
528         if (!iwm->umac_profile_active)
529                 return -EAGAIN;
530
531         if (iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
532             iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
533             iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_TKIP &&
534             iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_CCMP) {
535                 IWM_ERR(iwm, "Wrong unicast cipher: 0x%x\n",
536                         iwm->umac_profile->sec.ucast_cipher);
537                 return -EAGAIN;
538         }
539
540         if (iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
541             iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
542             iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_TKIP &&
543             iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_CCMP) {
544                 IWM_ERR(iwm, "Wrong multicast cipher: 0x%x\n",
545                         iwm->umac_profile->sec.mcast_cipher);
546                 return -EAGAIN;
547         }
548
549         if ((iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_40 ||
550              iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_104) &&
551             (iwm->umac_profile->sec.ucast_cipher !=
552              iwm->umac_profile->sec.mcast_cipher)) {
553                 IWM_ERR(iwm, "Unicast and multicast ciphers differ for WEP\n");
554         }
555
556         return 0;
557 }
558
559 int iwm_set_key(struct iwm_priv *iwm, bool remove, bool set_tx_key,
560                 struct iwm_key *key)
561 {
562         int ret;
563         u8 cmd[64], *sta_addr, *key_data, key_len;
564         s8 key_idx;
565         u16 cmd_size = 0;
566         struct iwm_umac_key_hdr *key_hdr = &key->hdr;
567         struct iwm_umac_key_wep40 *wep40 = (struct iwm_umac_key_wep40 *)cmd;
568         struct iwm_umac_key_wep104 *wep104 = (struct iwm_umac_key_wep104 *)cmd;
569         struct iwm_umac_key_tkip *tkip = (struct iwm_umac_key_tkip *)cmd;
570         struct iwm_umac_key_ccmp *ccmp = (struct iwm_umac_key_ccmp *)cmd;
571
572         if (set_tx_key)
573                 iwm->default_key = key;
574
575         /*
576          * We check if our current profile is valid.
577          * If not, we dont push the key, we just cache them,
578          * so that with the next siwsessid call, the keys
579          * will be actually pushed.
580          */
581         if (!remove) {
582                 ret = iwm_check_profile(iwm);
583                 if (ret < 0)
584                         return ret;
585         }
586
587         sta_addr = key->hdr.mac;
588         key_data = key->key;
589         key_len = key->key_len;
590         key_idx = key->hdr.key_idx;
591
592         if (!remove) {
593                 IWM_DBG_WEXT(iwm, DBG, "key_idx:%d set tx key:%d\n",
594                              key_idx, set_tx_key);
595                 IWM_DBG_WEXT(iwm, DBG, "key_len:%d\n", key_len);
596                 IWM_DBG_WEXT(iwm, DBG, "MAC:%pM, idx:%d, multicast:%d\n",
597                        key_hdr->mac, key_hdr->key_idx, key_hdr->multicast);
598
599                 IWM_DBG_WEXT(iwm, DBG, "profile: mcast:0x%x, ucast:0x%x\n",
600                              iwm->umac_profile->sec.mcast_cipher,
601                              iwm->umac_profile->sec.ucast_cipher);
602                 IWM_DBG_WEXT(iwm, DBG, "profile: auth_type:0x%x, flags:0x%x\n",
603                              iwm->umac_profile->sec.auth_type,
604                              iwm->umac_profile->sec.flags);
605
606                 switch (key->alg) {
607                 case UMAC_CIPHER_TYPE_WEP_40:
608                         wep40->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP40_KEY;
609                         wep40->hdr.buf_size =
610                                 cpu_to_le16(sizeof(struct iwm_umac_key_wep40) -
611                                             sizeof(struct iwm_umac_wifi_if));
612
613                         memcpy(&wep40->key_hdr, key_hdr,
614                                sizeof(struct iwm_umac_key_hdr));
615                         memcpy(wep40->key, key_data, key_len);
616                         wep40->static_key = 1;
617
618                         cmd_size = sizeof(struct iwm_umac_key_wep40);
619                         break;
620
621                 case UMAC_CIPHER_TYPE_WEP_104:
622                         wep104->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP104_KEY;
623                         wep104->hdr.buf_size =
624                                 cpu_to_le16(sizeof(struct iwm_umac_key_wep104) -
625                                             sizeof(struct iwm_umac_wifi_if));
626
627                         memcpy(&wep104->key_hdr, key_hdr,
628                                sizeof(struct iwm_umac_key_hdr));
629                         memcpy(wep104->key, key_data, key_len);
630                         wep104->static_key = 1;
631
632                         cmd_size = sizeof(struct iwm_umac_key_wep104);
633                         break;
634
635                 case UMAC_CIPHER_TYPE_CCMP:
636                         key_hdr->key_idx++;
637                         ccmp->hdr.oid = UMAC_WIFI_IF_CMD_ADD_CCMP_KEY;
638                         ccmp->hdr.buf_size =
639                                 cpu_to_le16(sizeof(struct iwm_umac_key_ccmp) -
640                                             sizeof(struct iwm_umac_wifi_if));
641
642                         memcpy(&ccmp->key_hdr, key_hdr,
643                                sizeof(struct iwm_umac_key_hdr));
644
645                         memcpy(ccmp->key, key_data, key_len);
646
647                         if (key->flags & IW_ENCODE_EXT_RX_SEQ_VALID)
648                                 memcpy(ccmp->iv_count, key->rx_seq, 6);
649
650                         cmd_size = sizeof(struct iwm_umac_key_ccmp);
651                         break;
652
653                 case UMAC_CIPHER_TYPE_TKIP:
654                         key_hdr->key_idx++;
655                         tkip->hdr.oid = UMAC_WIFI_IF_CMD_ADD_TKIP_KEY;
656                         tkip->hdr.buf_size =
657                                 cpu_to_le16(sizeof(struct iwm_umac_key_tkip) -
658                                             sizeof(struct iwm_umac_wifi_if));
659
660                         memcpy(&tkip->key_hdr, key_hdr,
661                                sizeof(struct iwm_umac_key_hdr));
662
663                         memcpy(tkip->tkip_key, key_data, IWM_TKIP_KEY_SIZE);
664                         memcpy(tkip->mic_tx_key, key_data + IWM_TKIP_KEY_SIZE,
665                                IWM_TKIP_MIC_SIZE);
666                         memcpy(tkip->mic_rx_key,
667                                key_data + IWM_TKIP_KEY_SIZE + IWM_TKIP_MIC_SIZE,
668                                IWM_TKIP_MIC_SIZE);
669
670                         if (key->flags & IW_ENCODE_EXT_RX_SEQ_VALID)
671                                 memcpy(ccmp->iv_count, key->rx_seq, 6);
672
673                         cmd_size = sizeof(struct iwm_umac_key_tkip);
674                         break;
675
676                 default:
677                         return -ENOTSUPP;
678                 }
679
680                 if ((key->alg == UMAC_CIPHER_TYPE_CCMP) ||
681                     (key->alg == UMAC_CIPHER_TYPE_TKIP))
682                         /*
683                          * UGLY_UGLY_UGLY
684                          * Copied HACK from the MWG driver.
685                          * Without it, the key is set before the second
686                          * EAPOL frame is sent, and the latter is thus
687                          * encrypted.
688                          */
689                         schedule_timeout_interruptible(usecs_to_jiffies(300));
690
691                 ret =  iwm_send_wifi_if_cmd(iwm, cmd, cmd_size, 1);
692                 if (ret < 0)
693                         goto err;
694
695                 /*
696                  * We need a default key only if it is set and
697                  * if we're doing WEP.
698                  */
699                 if (iwm->default_key == key &&
700                         ((key->alg == UMAC_CIPHER_TYPE_WEP_40) ||
701                          (key->alg == UMAC_CIPHER_TYPE_WEP_104))) {
702                         ret = iwm_set_tx_key(iwm, key_idx);
703                         if (ret < 0)
704                                 goto err;
705                 }
706         } else {
707                 struct iwm_umac_key_remove key_remove;
708
709                 key_remove.hdr.oid = UMAC_WIFI_IF_CMD_REMOVE_KEY;
710                 key_remove.hdr.buf_size =
711                         cpu_to_le16(sizeof(struct iwm_umac_key_remove) -
712                                     sizeof(struct iwm_umac_wifi_if));
713                 memcpy(&key_remove.key_hdr, key_hdr,
714                        sizeof(struct iwm_umac_key_hdr));
715
716                 ret =  iwm_send_wifi_if_cmd(iwm, &key_remove,
717                                             sizeof(struct iwm_umac_key_remove),
718                                             1);
719                 if (ret < 0)
720                         return ret;
721
722                 iwm->keys[key_idx].in_use = 0;
723         }
724
725         return 0;
726
727  err:
728         kfree(key);
729         return ret;
730 }
731
732
733 int iwm_send_mlme_profile(struct iwm_priv *iwm)
734 {
735         int ret, i;
736         struct iwm_umac_profile profile;
737
738         memcpy(&profile, iwm->umac_profile, sizeof(profile));
739
740         profile.hdr.oid = UMAC_WIFI_IF_CMD_SET_PROFILE;
741         profile.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_profile) -
742                                            sizeof(struct iwm_umac_wifi_if));
743
744         ret = iwm_send_wifi_if_cmd(iwm, &profile, sizeof(profile), 1);
745         if (ret < 0) {
746                 IWM_ERR(iwm, "Send profile command failed\n");
747                 return ret;
748         }
749
750         /* Wait for the profile to be active */
751         ret = wait_event_interruptible_timeout(iwm->mlme_queue,
752                                                iwm->umac_profile_active == 1,
753                                                3 * HZ);
754         if (!ret)
755                 return -EBUSY;
756
757
758         for (i = 0; i < IWM_NUM_KEYS; i++)
759                 if (iwm->keys[i].in_use) {
760                         int default_key = 0;
761                         struct iwm_key *key = &iwm->keys[i];
762
763                         if (key == iwm->default_key)
764                                 default_key = 1;
765
766                         /* Wait for the profile before sending the keys */
767                         wait_event_interruptible_timeout(iwm->mlme_queue,
768                              (test_bit(IWM_STATUS_ASSOCIATING, &iwm->status) ||
769                               test_bit(IWM_STATUS_ASSOCIATED, &iwm->status)),
770                                                          3 * HZ);
771
772                         ret = iwm_set_key(iwm, 0, default_key, key);
773                         if (ret < 0)
774                                 return ret;
775                 }
776
777         return 0;
778 }
779
780 int iwm_invalidate_mlme_profile(struct iwm_priv *iwm)
781 {
782         int ret;
783         struct iwm_umac_invalidate_profile invalid;
784
785         invalid.hdr.oid = UMAC_WIFI_IF_CMD_INVALIDATE_PROFILE;
786         invalid.hdr.buf_size =
787                 cpu_to_le16(sizeof(struct iwm_umac_invalidate_profile) -
788                             sizeof(struct iwm_umac_wifi_if));
789
790         invalid.reason = WLAN_REASON_UNSPECIFIED;
791
792         ret = iwm_send_wifi_if_cmd(iwm, &invalid, sizeof(invalid), 1);
793         if (ret < 0)
794                 return ret;
795
796         ret = wait_event_interruptible_timeout(iwm->mlme_queue,
797                                  (iwm->umac_profile_active == 0),
798                                                2 * HZ);
799         if (!ret)
800                 return -EBUSY;
801
802         return 0;
803 }
804
805 int iwm_send_umac_stats_req(struct iwm_priv *iwm, u32 flags)
806 {
807         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
808         struct iwm_umac_cmd umac_cmd;
809         struct iwm_umac_cmd_stats_req stats_req;
810
811         stats_req.flags = cpu_to_le32(flags);
812
813         umac_cmd.id = UMAC_CMD_OPCODE_STATISTIC_REQUEST;
814         umac_cmd.resp = 0;
815
816         return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &stats_req,
817                                      sizeof(struct iwm_umac_cmd_stats_req));
818 }
819
820 int iwm_send_umac_channel_list(struct iwm_priv *iwm)
821 {
822         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
823         struct iwm_umac_cmd umac_cmd;
824         struct iwm_umac_cmd_get_channel_list *ch_list;
825         int size = sizeof(struct iwm_umac_cmd_get_channel_list) +
826                    sizeof(struct iwm_umac_channel_info) * 4;
827         int ret;
828
829         ch_list = kzalloc(size, GFP_KERNEL);
830         if (!ch_list) {
831                 IWM_ERR(iwm, "Couldn't allocate channel list cmd\n");
832                 return -ENOMEM;
833         }
834
835         ch_list->ch[0].band = UMAC_BAND_2GHZ;
836         ch_list->ch[0].type = UMAC_CHANNEL_WIDTH_20MHZ;
837         ch_list->ch[0].flags = UMAC_CHANNEL_FLAG_VALID;
838
839         ch_list->ch[1].band = UMAC_BAND_5GHZ;
840         ch_list->ch[1].type = UMAC_CHANNEL_WIDTH_20MHZ;
841         ch_list->ch[1].flags = UMAC_CHANNEL_FLAG_VALID;
842
843         ch_list->ch[2].band = UMAC_BAND_2GHZ;
844         ch_list->ch[2].type = UMAC_CHANNEL_WIDTH_20MHZ;
845         ch_list->ch[2].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
846
847         ch_list->ch[3].band = UMAC_BAND_5GHZ;
848         ch_list->ch[3].type = UMAC_CHANNEL_WIDTH_20MHZ;
849         ch_list->ch[3].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
850
851         ch_list->count = cpu_to_le16(4);
852
853         umac_cmd.id = UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST;
854         umac_cmd.resp = 1;
855
856         ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, ch_list, size);
857
858         kfree(ch_list);
859
860         return ret;
861 }
862
863 int iwm_scan_ssids(struct iwm_priv *iwm, struct cfg80211_ssid *ssids,
864                    int ssid_num)
865 {
866         struct iwm_umac_cmd_scan_request req;
867         int i, ret;
868
869         memset(&req, 0, sizeof(struct iwm_umac_cmd_scan_request));
870
871         req.hdr.oid = UMAC_WIFI_IF_CMD_SCAN_REQUEST;
872         req.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_cmd_scan_request)
873                                        - sizeof(struct iwm_umac_wifi_if));
874         req.type = UMAC_WIFI_IF_SCAN_TYPE_USER;
875         req.timeout = 2;
876         req.seq_num = iwm->scan_id;
877         req.ssid_num = min(ssid_num, UMAC_WIFI_IF_PROBE_OPTION_MAX);
878
879         for (i = 0; i < req.ssid_num; i++) {
880                 memcpy(req.ssids[i].ssid, ssids[i].ssid, ssids[i].ssid_len);
881                 req.ssids[i].ssid_len = ssids[i].ssid_len;
882         }
883
884         ret = iwm_send_wifi_if_cmd(iwm, &req, sizeof(req), 0);
885         if (ret < 0) {
886                 IWM_ERR(iwm, "Couldn't send scan request\n");
887                 return ret;
888         }
889
890         iwm->scan_id = iwm->scan_id++ % IWM_SCAN_ID_MAX;
891
892         return 0;
893 }
894
895 int iwm_scan_one_ssid(struct iwm_priv *iwm, u8 *ssid, int ssid_len)
896 {
897         struct cfg80211_ssid one_ssid;
898
899         if (test_and_set_bit(IWM_STATUS_SCANNING, &iwm->status))
900                 return 0;
901
902         one_ssid.ssid_len = min(ssid_len, IEEE80211_MAX_SSID_LEN);
903         memcpy(&one_ssid.ssid, ssid, one_ssid.ssid_len);
904
905         return iwm_scan_ssids(iwm, &one_ssid, 1);
906 }
907
908 int iwm_target_reset(struct iwm_priv *iwm)
909 {
910         struct iwm_udma_nonwifi_cmd target_cmd;
911
912         target_cmd.opcode = UMAC_HDI_OUT_OPCODE_REBOOT;
913         target_cmd.addr = 0;
914         target_cmd.op1_sz = 0;
915         target_cmd.op2 = 0;
916         target_cmd.handle_by_hw = 0;
917         target_cmd.resp = 0;
918         target_cmd.eop = 1;
919
920         return iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
921 }