iwmc3200wifi: SDIO disable race fix
[safe/jmp/linux-2.6] / 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_umac_wifi_if *hdr = (struct iwm_umac_wifi_if *)payload;
74         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
75         struct iwm_umac_cmd umac_cmd;
76         int ret;
77         u8 oid = hdr->oid;
78
79         umac_cmd.id = UMAC_CMD_OPCODE_WIFI_IF_WRAPPER;
80         umac_cmd.resp = resp;
81
82         ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
83                                     payload, payload_size);
84
85         if (resp) {
86                 ret = wait_event_interruptible_timeout(iwm->wifi_ntfy_queue,
87                                    test_and_clear_bit(oid, &iwm->wifi_ntfy[0]),
88                                    3 * HZ);
89
90                 return ret ? 0 : -EBUSY;
91         }
92
93         return ret;
94 }
95
96 static struct coex_event iwm_sta_xor_prio_tbl[COEX_EVENTS_NUM] =
97 {
98         {4, 3, 0, COEX_UNASSOC_IDLE_FLAGS},
99         {4, 3, 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
100         {4, 3, 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
101         {4, 3, 0, COEX_CALIBRATION_FLAGS},
102         {4, 3, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
103         {4, 3, 0, COEX_CONNECTION_ESTAB_FLAGS},
104         {4, 3, 0, COEX_ASSOCIATED_IDLE_FLAGS},
105         {4, 3, 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
106         {4, 3, 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
107         {4, 3, 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
108         {6, 3, 0, COEX_XOR_RF_ON_FLAGS},
109         {4, 3, 0, COEX_RF_OFF_FLAGS},
110         {6, 6, 0, COEX_STAND_ALONE_DEBUG_FLAGS},
111         {4, 3, 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
112         {4, 3, 0, COEX_RSRVD1_FLAGS},
113         {4, 3, 0, COEX_RSRVD2_FLAGS}
114 };
115
116 static struct coex_event iwm_sta_cm_prio_tbl[COEX_EVENTS_NUM] =
117 {
118         {1, 1, 0, COEX_UNASSOC_IDLE_FLAGS},
119         {4, 3, 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
120         {3, 3, 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
121         {5, 5, 0, COEX_CALIBRATION_FLAGS},
122         {3, 3, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
123         {5, 4, 0, COEX_CONNECTION_ESTAB_FLAGS},
124         {4, 4, 0, COEX_ASSOCIATED_IDLE_FLAGS},
125         {4, 4, 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
126         {4, 4, 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
127         {4, 4, 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
128         {1, 1, 0, COEX_RF_ON_FLAGS},
129         {1, 1, 0, COEX_RF_OFF_FLAGS},
130         {6, 6, 0, COEX_STAND_ALONE_DEBUG_FLAGS},
131         {5, 4, 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
132         {1, 1, 0, COEX_RSRVD1_FLAGS},
133         {1, 1, 0, COEX_RSRVD2_FLAGS}
134 };
135
136 int iwm_send_prio_table(struct iwm_priv *iwm)
137 {
138         struct iwm_coex_prio_table_cmd coex_table_cmd;
139         u32 coex_enabled, mode_enabled;
140
141         memset(&coex_table_cmd, 0, sizeof(struct iwm_coex_prio_table_cmd));
142
143         coex_table_cmd.flags = COEX_FLAGS_STA_TABLE_VALID_MSK;
144
145         switch (iwm->conf.coexist_mode) {
146         case COEX_MODE_XOR:
147         case COEX_MODE_CM:
148                 coex_enabled = 1;
149                 break;
150         default:
151                 coex_enabled = 0;
152                 break;
153         }
154
155         switch (iwm->conf.mode) {
156         case UMAC_MODE_BSS:
157         case UMAC_MODE_IBSS:
158                 mode_enabled = 1;
159                 break;
160         default:
161                 mode_enabled = 0;
162                 break;
163         }
164
165         if (coex_enabled && mode_enabled) {
166                 coex_table_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK |
167                                         COEX_FLAGS_ASSOC_WAKEUP_UMASK_MSK |
168                                         COEX_FLAGS_UNASSOC_WAKEUP_UMASK_MSK;
169
170                 switch (iwm->conf.coexist_mode) {
171                 case COEX_MODE_XOR:
172                         memcpy(coex_table_cmd.sta_prio, iwm_sta_xor_prio_tbl,
173                                sizeof(iwm_sta_xor_prio_tbl));
174                         break;
175                 case COEX_MODE_CM:
176                         memcpy(coex_table_cmd.sta_prio, iwm_sta_cm_prio_tbl,
177                                sizeof(iwm_sta_cm_prio_tbl));
178                         break;
179                 default:
180                         IWM_ERR(iwm, "Invalid coex_mode 0x%x\n",
181                                 iwm->conf.coexist_mode);
182                         break;
183                 }
184         } else
185                 IWM_WARN(iwm, "coexistense disabled\n");
186
187         return iwm_send_lmac_ptrough_cmd(iwm, COEX_PRIORITY_TABLE_CMD,
188                                 &coex_table_cmd,
189                                 sizeof(struct iwm_coex_prio_table_cmd), 1);
190 }
191
192 int iwm_send_init_calib_cfg(struct iwm_priv *iwm, u8 calib_requested)
193 {
194         struct iwm_lmac_cal_cfg_cmd cal_cfg_cmd;
195
196         memset(&cal_cfg_cmd, 0, sizeof(struct iwm_lmac_cal_cfg_cmd));
197
198         cal_cfg_cmd.ucode_cfg.init.enable = cpu_to_le32(calib_requested);
199         cal_cfg_cmd.ucode_cfg.init.start = cpu_to_le32(calib_requested);
200         cal_cfg_cmd.ucode_cfg.init.send_res = cpu_to_le32(calib_requested);
201         cal_cfg_cmd.ucode_cfg.flags =
202                 cpu_to_le32(CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_AFTER_MSK);
203
204         return iwm_send_lmac_ptrough_cmd(iwm, CALIBRATION_CFG_CMD, &cal_cfg_cmd,
205                                 sizeof(struct iwm_lmac_cal_cfg_cmd), 1);
206 }
207
208 int iwm_send_periodic_calib_cfg(struct iwm_priv *iwm, u8 calib_requested)
209 {
210         struct iwm_lmac_cal_cfg_cmd cal_cfg_cmd;
211
212         memset(&cal_cfg_cmd, 0, sizeof(struct iwm_lmac_cal_cfg_cmd));
213
214         cal_cfg_cmd.ucode_cfg.periodic.enable = cpu_to_le32(calib_requested);
215         cal_cfg_cmd.ucode_cfg.periodic.start = cpu_to_le32(calib_requested);
216
217         return iwm_send_lmac_ptrough_cmd(iwm, CALIBRATION_CFG_CMD, &cal_cfg_cmd,
218                                 sizeof(struct iwm_lmac_cal_cfg_cmd), 0);
219 }
220
221 int iwm_store_rxiq_calib_result(struct iwm_priv *iwm)
222 {
223         struct iwm_calib_rxiq *rxiq;
224         u8 *eeprom_rxiq = iwm_eeprom_access(iwm, IWM_EEPROM_CALIB_RXIQ);
225         int grplen = sizeof(struct iwm_calib_rxiq_group);
226
227         rxiq = kzalloc(sizeof(struct iwm_calib_rxiq), GFP_KERNEL);
228         if (!rxiq) {
229                 IWM_ERR(iwm, "Couldn't alloc memory for RX IQ\n");
230                 return -ENOMEM;
231         }
232
233         eeprom_rxiq = iwm_eeprom_access(iwm, IWM_EEPROM_CALIB_RXIQ);
234         if (IS_ERR(eeprom_rxiq)) {
235                 IWM_ERR(iwm, "Couldn't access EEPROM RX IQ entry\n");
236                 kfree(rxiq);
237                 return PTR_ERR(eeprom_rxiq);
238         }
239
240         iwm->calib_res[SHILOH_PHY_CALIBRATE_RX_IQ_CMD].buf = (u8 *)rxiq;
241         iwm->calib_res[SHILOH_PHY_CALIBRATE_RX_IQ_CMD].size = sizeof(*rxiq);
242
243         rxiq->hdr.opcode = SHILOH_PHY_CALIBRATE_RX_IQ_CMD;
244         rxiq->hdr.first_grp = 0;
245         rxiq->hdr.grp_num = 1;
246         rxiq->hdr.all_data_valid = 1;
247
248         memcpy(&rxiq->group[0], eeprom_rxiq, 4 * grplen);
249         memcpy(&rxiq->group[4], eeprom_rxiq + 6 * grplen, grplen);
250
251         return 0;
252 }
253
254 int iwm_send_calib_results(struct iwm_priv *iwm)
255 {
256         int i, ret = 0;
257
258         for (i = PHY_CALIBRATE_OPCODES_NUM; i < CALIBRATION_CMD_NUM; i++) {
259                 if (test_bit(i - PHY_CALIBRATE_OPCODES_NUM,
260                              &iwm->calib_done_map)) {
261                         IWM_DBG_CMD(iwm, DBG,
262                                     "Send calibration %d result\n", i);
263                         ret |= iwm_send_lmac_ptrough_cmd(iwm,
264                                         REPLY_PHY_CALIBRATION_CMD,
265                                         iwm->calib_res[i].buf,
266                                         iwm->calib_res[i].size, 0);
267
268                         kfree(iwm->calib_res[i].buf);
269                         iwm->calib_res[i].buf = NULL;
270                         iwm->calib_res[i].size = 0;
271                 }
272         }
273
274         return ret;
275 }
276
277 int iwm_send_ct_kill_cfg(struct iwm_priv *iwm, u8 entry, u8 exit)
278 {
279         struct iwm_ct_kill_cfg_cmd cmd;
280
281         cmd.entry_threshold = entry;
282         cmd.exit_threshold = exit;
283
284         return iwm_send_lmac_ptrough_cmd(iwm, REPLY_CT_KILL_CONFIG_CMD, &cmd,
285                                          sizeof(struct iwm_ct_kill_cfg_cmd), 0);
286 }
287
288 int iwm_send_umac_reset(struct iwm_priv *iwm, __le32 reset_flags, bool resp)
289 {
290         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
291         struct iwm_umac_cmd umac_cmd;
292         struct iwm_umac_cmd_reset reset;
293
294         reset.flags = reset_flags;
295
296         umac_cmd.id = UMAC_CMD_OPCODE_RESET;
297         umac_cmd.resp = resp;
298
299         return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &reset,
300                                      sizeof(struct iwm_umac_cmd_reset));
301 }
302
303 int iwm_umac_set_config_fix(struct iwm_priv *iwm, u16 tbl, u16 key, u32 value)
304 {
305         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
306         struct iwm_umac_cmd umac_cmd;
307         struct iwm_umac_cmd_set_param_fix param;
308
309         if ((tbl != UMAC_PARAM_TBL_CFG_FIX) &&
310             (tbl != UMAC_PARAM_TBL_FA_CFG_FIX))
311                 return -EINVAL;
312
313         umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_FIX;
314         umac_cmd.resp = 0;
315
316         param.tbl = cpu_to_le16(tbl);
317         param.key = cpu_to_le16(key);
318         param.value = cpu_to_le32(value);
319
320         return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &param,
321                                      sizeof(struct iwm_umac_cmd_set_param_fix));
322 }
323
324 int iwm_umac_set_config_var(struct iwm_priv *iwm, u16 key,
325                             void *payload, u16 payload_size)
326 {
327         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
328         struct iwm_umac_cmd umac_cmd;
329         struct iwm_umac_cmd_set_param_var *param_hdr;
330         u8 *param;
331         int ret;
332
333         param = kzalloc(payload_size +
334                         sizeof(struct iwm_umac_cmd_set_param_var), GFP_KERNEL);
335         if (!param) {
336                 IWM_ERR(iwm, "Couldn't allocate param\n");
337                 return -ENOMEM;
338         }
339
340         param_hdr = (struct iwm_umac_cmd_set_param_var *)param;
341
342         umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_VAR;
343         umac_cmd.resp = 0;
344
345         param_hdr->tbl = cpu_to_le16(UMAC_PARAM_TBL_CFG_VAR);
346         param_hdr->key = cpu_to_le16(key);
347         param_hdr->len = cpu_to_le16(payload_size);
348         memcpy(param + sizeof(struct iwm_umac_cmd_set_param_var),
349                payload, payload_size);
350
351         ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, param,
352                                     sizeof(struct iwm_umac_cmd_set_param_var) +
353                                     payload_size);
354         kfree(param);
355
356         return ret;
357 }
358
359 int iwm_send_umac_config(struct iwm_priv *iwm, __le32 reset_flags)
360 {
361         int ret;
362
363         /* Use UMAC default values */
364         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
365                                       CFG_POWER_INDEX, iwm->conf.power_index);
366         if (ret < 0)
367                 return ret;
368
369         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_FA_CFG_FIX,
370                                       CFG_FRAG_THRESHOLD,
371                                       iwm->conf.frag_threshold);
372         if (ret < 0)
373                 return ret;
374
375         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
376                                       CFG_RTS_THRESHOLD,
377                                       iwm->conf.rts_threshold);
378         if (ret < 0)
379                 return ret;
380
381         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
382                                       CFG_CTS_TO_SELF, iwm->conf.cts_to_self);
383         if (ret < 0)
384                 return ret;
385
386         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
387                                       CFG_WIRELESS_MODE,
388                                       iwm->conf.wireless_mode);
389         if (ret < 0)
390                 return ret;
391
392         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
393                                       CFG_COEX_MODE, iwm->conf.coexist_mode);
394         if (ret < 0)
395                 return ret;
396
397         /*
398         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
399                                       CFG_ASSOCIATION_TIMEOUT,
400                                       iwm->conf.assoc_timeout);
401         if (ret < 0)
402                 return ret;
403
404         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
405                                       CFG_ROAM_TIMEOUT,
406                                       iwm->conf.roam_timeout);
407         if (ret < 0)
408                 return ret;
409
410         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
411                                       CFG_WIRELESS_MODE,
412                                       WIRELESS_MODE_11A | WIRELESS_MODE_11G);
413         if (ret < 0)
414                 return ret;
415         */
416
417         ret = iwm_umac_set_config_var(iwm, CFG_NET_ADDR,
418                                       iwm_to_ndev(iwm)->dev_addr, ETH_ALEN);
419         if (ret < 0)
420                 return ret;
421
422         /* UMAC PM static configurations */
423         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
424                                       CFG_PM_LEGACY_RX_TIMEOUT, 0x12C);
425         if (ret < 0)
426                 return ret;
427
428         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
429                                       CFG_PM_LEGACY_TX_TIMEOUT, 0x15E);
430         if (ret < 0)
431                 return ret;
432
433         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
434                                       CFG_PM_CTRL_FLAGS, 0x1);
435         if (ret < 0)
436                 return ret;
437
438         ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
439                                       CFG_PM_KEEP_ALIVE_IN_BEACONS, 0x80);
440         if (ret < 0)
441                 return ret;
442
443         /* reset UMAC */
444         ret = iwm_send_umac_reset(iwm, reset_flags, 1);
445         if (ret < 0)
446                 return ret;
447
448         ret = iwm_notif_handle(iwm, UMAC_CMD_OPCODE_RESET, IWM_SRC_UMAC,
449                                WAIT_NOTIF_TIMEOUT);
450         if (ret) {
451                 IWM_ERR(iwm, "Wait for UMAC RESET timeout\n");
452                 return ret;
453         }
454
455         return ret;
456 }
457
458 int iwm_send_packet(struct iwm_priv *iwm, struct sk_buff *skb, int pool_id)
459 {
460         struct iwm_udma_wifi_cmd udma_cmd;
461         struct iwm_umac_cmd umac_cmd;
462         struct iwm_tx_info *tx_info = skb_to_tx_info(skb);
463
464         udma_cmd.eop = 1; /* always set eop for non-concatenated Tx */
465         udma_cmd.credit_group = pool_id;
466         udma_cmd.ra_tid = tx_info->sta << 4 | tx_info->tid;
467         udma_cmd.lmac_offset = 0;
468
469         umac_cmd.id = REPLY_TX;
470         umac_cmd.color = tx_info->color;
471         umac_cmd.resp = 0;
472
473         return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
474                                      skb->data, skb->len);
475 }
476
477 static int iwm_target_read(struct iwm_priv *iwm, __le32 address,
478                            u8 *response, u32 resp_size)
479 {
480         struct iwm_udma_nonwifi_cmd target_cmd;
481         struct iwm_nonwifi_cmd *cmd;
482         u16 seq_num;
483         int ret = 0;
484
485         target_cmd.opcode = UMAC_HDI_OUT_OPCODE_READ;
486         target_cmd.addr = address;
487         target_cmd.op1_sz = cpu_to_le32(resp_size);
488         target_cmd.op2 = 0;
489         target_cmd.handle_by_hw = 0;
490         target_cmd.resp = 1;
491         target_cmd.eop = 1;
492
493         ret = iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
494         if (ret < 0) {
495                 IWM_ERR(iwm, "Couldn't send READ command\n");
496                 return ret;
497         }
498
499         /* When succeding, the send_target routine returns the seq number */
500         seq_num = ret;
501
502         ret = wait_event_interruptible_timeout(iwm->nonwifi_queue,
503                 (cmd = iwm_get_pending_nonwifi_cmd(iwm, seq_num,
504                                           UMAC_HDI_OUT_OPCODE_READ)) != NULL,
505                                                2 * HZ);
506
507         if (!ret) {
508                 IWM_ERR(iwm, "Didn't receive a target READ answer\n");
509                 return ret;
510         }
511
512         memcpy(response, cmd->buf.hdr + sizeof(struct iwm_udma_in_hdr),
513                resp_size);
514
515         kfree(cmd);
516
517         return 0;
518 }
519
520 int iwm_read_mac(struct iwm_priv *iwm, u8 *mac)
521 {
522         int ret;
523         u8 mac_align[ALIGN(ETH_ALEN, 8)];
524
525         ret = iwm_target_read(iwm, cpu_to_le32(WICO_MAC_ADDRESS_ADDR),
526                               mac_align, sizeof(mac_align));
527         if (ret)
528                 return ret;
529
530         if (is_valid_ether_addr(mac_align))
531                 memcpy(mac, mac_align, ETH_ALEN);
532         else {
533                 IWM_ERR(iwm, "Invalid EEPROM MAC\n");
534                 memcpy(mac, iwm->conf.mac_addr, ETH_ALEN);
535                 get_random_bytes(&mac[3], 3);
536         }
537
538         return 0;
539 }
540
541 static int iwm_check_profile(struct iwm_priv *iwm)
542 {
543         if (!iwm->umac_profile_active)
544                 return -EAGAIN;
545
546         if (iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
547             iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
548             iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_TKIP &&
549             iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_CCMP) {
550                 IWM_ERR(iwm, "Wrong unicast cipher: 0x%x\n",
551                         iwm->umac_profile->sec.ucast_cipher);
552                 return -EAGAIN;
553         }
554
555         if (iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
556             iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
557             iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_TKIP &&
558             iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_CCMP) {
559                 IWM_ERR(iwm, "Wrong multicast cipher: 0x%x\n",
560                         iwm->umac_profile->sec.mcast_cipher);
561                 return -EAGAIN;
562         }
563
564         if ((iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_40 ||
565              iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_104) &&
566             (iwm->umac_profile->sec.ucast_cipher !=
567              iwm->umac_profile->sec.mcast_cipher)) {
568                 IWM_ERR(iwm, "Unicast and multicast ciphers differ for WEP\n");
569         }
570
571         return 0;
572 }
573
574 int iwm_set_tx_key(struct iwm_priv *iwm, u8 key_idx)
575 {
576         struct iwm_umac_tx_key_id tx_key_id;
577         int ret;
578
579         ret = iwm_check_profile(iwm);
580         if (ret < 0)
581                 return ret;
582
583         /* UMAC only allows to set default key for WEP and auth type is
584          * NOT 802.1X or RSNA. */
585         if ((iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
586              iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_104) ||
587             iwm->umac_profile->sec.auth_type == UMAC_AUTH_TYPE_8021X ||
588             iwm->umac_profile->sec.auth_type == UMAC_AUTH_TYPE_RSNA_PSK)
589                 return 0;
590
591         tx_key_id.hdr.oid = UMAC_WIFI_IF_CMD_GLOBAL_TX_KEY_ID;
592         tx_key_id.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_tx_key_id) -
593                                              sizeof(struct iwm_umac_wifi_if));
594
595         tx_key_id.key_idx = key_idx;
596
597         return iwm_send_wifi_if_cmd(iwm, &tx_key_id, sizeof(tx_key_id), 1);
598 }
599
600 int iwm_set_key(struct iwm_priv *iwm, bool remove, struct iwm_key *key)
601 {
602         int ret = 0;
603         u8 cmd[64], *sta_addr, *key_data, key_len;
604         s8 key_idx;
605         u16 cmd_size = 0;
606         struct iwm_umac_key_hdr *key_hdr = &key->hdr;
607         struct iwm_umac_key_wep40 *wep40 = (struct iwm_umac_key_wep40 *)cmd;
608         struct iwm_umac_key_wep104 *wep104 = (struct iwm_umac_key_wep104 *)cmd;
609         struct iwm_umac_key_tkip *tkip = (struct iwm_umac_key_tkip *)cmd;
610         struct iwm_umac_key_ccmp *ccmp = (struct iwm_umac_key_ccmp *)cmd;
611
612         if (!remove) {
613                 ret = iwm_check_profile(iwm);
614                 if (ret < 0)
615                         return ret;
616         }
617
618         sta_addr = key->hdr.mac;
619         key_data = key->key;
620         key_len = key->key_len;
621         key_idx = key->hdr.key_idx;
622
623         if (!remove) {
624                 u8 auth_type = iwm->umac_profile->sec.auth_type;
625
626                 IWM_DBG_WEXT(iwm, DBG, "key_idx:%d\n", key_idx);
627                 IWM_DBG_WEXT(iwm, DBG, "key_len:%d\n", key_len);
628                 IWM_DBG_WEXT(iwm, DBG, "MAC:%pM, idx:%d, multicast:%d\n",
629                        key_hdr->mac, key_hdr->key_idx, key_hdr->multicast);
630
631                 IWM_DBG_WEXT(iwm, DBG, "profile: mcast:0x%x, ucast:0x%x\n",
632                              iwm->umac_profile->sec.mcast_cipher,
633                              iwm->umac_profile->sec.ucast_cipher);
634                 IWM_DBG_WEXT(iwm, DBG, "profile: auth_type:0x%x, flags:0x%x\n",
635                              iwm->umac_profile->sec.auth_type,
636                              iwm->umac_profile->sec.flags);
637
638                 switch (key->cipher) {
639                 case WLAN_CIPHER_SUITE_WEP40:
640                         wep40->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP40_KEY;
641                         wep40->hdr.buf_size =
642                                 cpu_to_le16(sizeof(struct iwm_umac_key_wep40) -
643                                             sizeof(struct iwm_umac_wifi_if));
644
645                         memcpy(&wep40->key_hdr, key_hdr,
646                                sizeof(struct iwm_umac_key_hdr));
647                         memcpy(wep40->key, key_data, key_len);
648                         wep40->static_key =
649                                 !!((auth_type != UMAC_AUTH_TYPE_8021X) &&
650                                    (auth_type != UMAC_AUTH_TYPE_RSNA_PSK));
651
652                         cmd_size = sizeof(struct iwm_umac_key_wep40);
653                         break;
654
655                 case WLAN_CIPHER_SUITE_WEP104:
656                         wep104->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP104_KEY;
657                         wep104->hdr.buf_size =
658                                 cpu_to_le16(sizeof(struct iwm_umac_key_wep104) -
659                                             sizeof(struct iwm_umac_wifi_if));
660
661                         memcpy(&wep104->key_hdr, key_hdr,
662                                sizeof(struct iwm_umac_key_hdr));
663                         memcpy(wep104->key, key_data, key_len);
664                         wep104->static_key =
665                                 !!((auth_type != UMAC_AUTH_TYPE_8021X) &&
666                                    (auth_type != UMAC_AUTH_TYPE_RSNA_PSK));
667
668                         cmd_size = sizeof(struct iwm_umac_key_wep104);
669                         break;
670
671                 case WLAN_CIPHER_SUITE_CCMP:
672                         key_hdr->key_idx++;
673                         ccmp->hdr.oid = UMAC_WIFI_IF_CMD_ADD_CCMP_KEY;
674                         ccmp->hdr.buf_size =
675                                 cpu_to_le16(sizeof(struct iwm_umac_key_ccmp) -
676                                             sizeof(struct iwm_umac_wifi_if));
677
678                         memcpy(&ccmp->key_hdr, key_hdr,
679                                sizeof(struct iwm_umac_key_hdr));
680
681                         memcpy(ccmp->key, key_data, key_len);
682
683                         if (key->seq_len)
684                                 memcpy(ccmp->iv_count, key->seq, key->seq_len);
685
686                         cmd_size = sizeof(struct iwm_umac_key_ccmp);
687                         break;
688
689                 case WLAN_CIPHER_SUITE_TKIP:
690                         key_hdr->key_idx++;
691                         tkip->hdr.oid = UMAC_WIFI_IF_CMD_ADD_TKIP_KEY;
692                         tkip->hdr.buf_size =
693                                 cpu_to_le16(sizeof(struct iwm_umac_key_tkip) -
694                                             sizeof(struct iwm_umac_wifi_if));
695
696                         memcpy(&tkip->key_hdr, key_hdr,
697                                sizeof(struct iwm_umac_key_hdr));
698
699                         memcpy(tkip->tkip_key, key_data, IWM_TKIP_KEY_SIZE);
700                         memcpy(tkip->mic_tx_key, key_data + IWM_TKIP_KEY_SIZE,
701                                IWM_TKIP_MIC_SIZE);
702                         memcpy(tkip->mic_rx_key,
703                                key_data + IWM_TKIP_KEY_SIZE + IWM_TKIP_MIC_SIZE,
704                                IWM_TKIP_MIC_SIZE);
705
706                         if (key->seq_len)
707                                 memcpy(ccmp->iv_count, key->seq, key->seq_len);
708
709                         cmd_size = sizeof(struct iwm_umac_key_tkip);
710                         break;
711
712                 default:
713                         return -ENOTSUPP;
714                 }
715
716                 if ((key->cipher == WLAN_CIPHER_SUITE_TKIP) ||
717                     (key->cipher == WLAN_CIPHER_SUITE_CCMP))
718                         /*
719                          * UGLY_UGLY_UGLY
720                          * Copied HACK from the MWG driver.
721                          * Without it, the key is set before the second
722                          * EAPOL frame is sent, and the latter is thus
723                          * encrypted.
724                          */
725                         schedule_timeout_interruptible(usecs_to_jiffies(300));
726
727                 ret =  iwm_send_wifi_if_cmd(iwm, cmd, cmd_size, 1);
728         } else {
729                 struct iwm_umac_key_remove key_remove;
730
731                 IWM_DBG_WEXT(iwm, ERR, "Removing key_idx:%d\n", key_idx);
732
733                 key_remove.hdr.oid = UMAC_WIFI_IF_CMD_REMOVE_KEY;
734                 key_remove.hdr.buf_size =
735                         cpu_to_le16(sizeof(struct iwm_umac_key_remove) -
736                                     sizeof(struct iwm_umac_wifi_if));
737                 memcpy(&key_remove.key_hdr, key_hdr,
738                        sizeof(struct iwm_umac_key_hdr));
739
740                 ret =  iwm_send_wifi_if_cmd(iwm, &key_remove,
741                                             sizeof(struct iwm_umac_key_remove),
742                                             1);
743                 if (ret)
744                         return ret;
745
746                 iwm->keys[key_idx].key_len = 0;
747         }
748
749         return ret;
750 }
751
752
753 int iwm_send_mlme_profile(struct iwm_priv *iwm)
754 {
755         int ret;
756         struct iwm_umac_profile profile;
757
758         memcpy(&profile, iwm->umac_profile, sizeof(profile));
759
760         profile.hdr.oid = UMAC_WIFI_IF_CMD_SET_PROFILE;
761         profile.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_profile) -
762                                            sizeof(struct iwm_umac_wifi_if));
763
764         ret = iwm_send_wifi_if_cmd(iwm, &profile, sizeof(profile), 1);
765         if (ret) {
766                 IWM_ERR(iwm, "Send profile command failed\n");
767                 return ret;
768         }
769
770         set_bit(IWM_STATUS_SME_CONNECTING, &iwm->status);
771         return 0;
772 }
773
774 int iwm_invalidate_mlme_profile(struct iwm_priv *iwm)
775 {
776         struct iwm_umac_invalidate_profile invalid;
777         int ret;
778
779         invalid.hdr.oid = UMAC_WIFI_IF_CMD_INVALIDATE_PROFILE;
780         invalid.hdr.buf_size =
781                 cpu_to_le16(sizeof(struct iwm_umac_invalidate_profile) -
782                             sizeof(struct iwm_umac_wifi_if));
783
784         invalid.reason = WLAN_REASON_UNSPECIFIED;
785
786         ret = iwm_send_wifi_if_cmd(iwm, &invalid, sizeof(invalid), 1);
787         if (ret)
788                 return ret;
789
790         ret = wait_event_interruptible_timeout(iwm->mlme_queue,
791                                 (iwm->umac_profile_active == 0), 2 * HZ);
792
793         return ret ? 0 : -EBUSY;
794 }
795
796 int iwm_tx_power_trigger(struct iwm_priv *iwm)
797 {
798         struct iwm_umac_pwr_trigger pwr_trigger;
799
800         pwr_trigger.hdr.oid = UMAC_WIFI_IF_CMD_TX_PWR_TRIGGER;
801         pwr_trigger.hdr.buf_size =
802                 cpu_to_le16(sizeof(struct iwm_umac_pwr_trigger) -
803                             sizeof(struct iwm_umac_wifi_if));
804
805
806         return iwm_send_wifi_if_cmd(iwm, &pwr_trigger, sizeof(pwr_trigger), 1);
807 }
808
809 int iwm_send_umac_stats_req(struct iwm_priv *iwm, u32 flags)
810 {
811         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
812         struct iwm_umac_cmd umac_cmd;
813         struct iwm_umac_cmd_stats_req stats_req;
814
815         stats_req.flags = cpu_to_le32(flags);
816
817         umac_cmd.id = UMAC_CMD_OPCODE_STATISTIC_REQUEST;
818         umac_cmd.resp = 0;
819
820         return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &stats_req,
821                                      sizeof(struct iwm_umac_cmd_stats_req));
822 }
823
824 int iwm_send_umac_channel_list(struct iwm_priv *iwm)
825 {
826         struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
827         struct iwm_umac_cmd umac_cmd;
828         struct iwm_umac_cmd_get_channel_list *ch_list;
829         int size = sizeof(struct iwm_umac_cmd_get_channel_list) +
830                    sizeof(struct iwm_umac_channel_info) * 4;
831         int ret;
832
833         ch_list = kzalloc(size, GFP_KERNEL);
834         if (!ch_list) {
835                 IWM_ERR(iwm, "Couldn't allocate channel list cmd\n");
836                 return -ENOMEM;
837         }
838
839         ch_list->ch[0].band = UMAC_BAND_2GHZ;
840         ch_list->ch[0].type = UMAC_CHANNEL_WIDTH_20MHZ;
841         ch_list->ch[0].flags = UMAC_CHANNEL_FLAG_VALID;
842
843         ch_list->ch[1].band = UMAC_BAND_5GHZ;
844         ch_list->ch[1].type = UMAC_CHANNEL_WIDTH_20MHZ;
845         ch_list->ch[1].flags = UMAC_CHANNEL_FLAG_VALID;
846
847         ch_list->ch[2].band = UMAC_BAND_2GHZ;
848         ch_list->ch[2].type = UMAC_CHANNEL_WIDTH_20MHZ;
849         ch_list->ch[2].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
850
851         ch_list->ch[3].band = UMAC_BAND_5GHZ;
852         ch_list->ch[3].type = UMAC_CHANNEL_WIDTH_20MHZ;
853         ch_list->ch[3].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
854
855         ch_list->count = cpu_to_le16(4);
856
857         umac_cmd.id = UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST;
858         umac_cmd.resp = 1;
859
860         ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, ch_list, size);
861
862         kfree(ch_list);
863
864         return ret;
865 }
866
867 int iwm_scan_ssids(struct iwm_priv *iwm, struct cfg80211_ssid *ssids,
868                    int ssid_num)
869 {
870         struct iwm_umac_cmd_scan_request req;
871         int i, ret;
872
873         memset(&req, 0, sizeof(struct iwm_umac_cmd_scan_request));
874
875         req.hdr.oid = UMAC_WIFI_IF_CMD_SCAN_REQUEST;
876         req.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_cmd_scan_request)
877                                        - sizeof(struct iwm_umac_wifi_if));
878         req.type = UMAC_WIFI_IF_SCAN_TYPE_USER;
879         req.timeout = 2;
880         req.seq_num = iwm->scan_id;
881         req.ssid_num = min(ssid_num, UMAC_WIFI_IF_PROBE_OPTION_MAX);
882
883         for (i = 0; i < req.ssid_num; i++) {
884                 memcpy(req.ssids[i].ssid, ssids[i].ssid, ssids[i].ssid_len);
885                 req.ssids[i].ssid_len = ssids[i].ssid_len;
886         }
887
888         ret = iwm_send_wifi_if_cmd(iwm, &req, sizeof(req), 0);
889         if (ret) {
890                 IWM_ERR(iwm, "Couldn't send scan request\n");
891                 return ret;
892         }
893
894         iwm->scan_id = iwm->scan_id++ % IWM_SCAN_ID_MAX;
895
896         return 0;
897 }
898
899 int iwm_scan_one_ssid(struct iwm_priv *iwm, u8 *ssid, int ssid_len)
900 {
901         struct cfg80211_ssid one_ssid;
902
903         if (test_and_set_bit(IWM_STATUS_SCANNING, &iwm->status))
904                 return 0;
905
906         one_ssid.ssid_len = min(ssid_len, IEEE80211_MAX_SSID_LEN);
907         memcpy(&one_ssid.ssid, ssid, one_ssid.ssid_len);
908
909         return iwm_scan_ssids(iwm, &one_ssid, 1);
910 }
911
912 int iwm_target_reset(struct iwm_priv *iwm)
913 {
914         struct iwm_udma_nonwifi_cmd target_cmd;
915
916         target_cmd.opcode = UMAC_HDI_OUT_OPCODE_REBOOT;
917         target_cmd.addr = 0;
918         target_cmd.op1_sz = 0;
919         target_cmd.op2 = 0;
920         target_cmd.handle_by_hw = 0;
921         target_cmd.resp = 0;
922         target_cmd.eop = 1;
923
924         return iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
925 }