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