blob: 0d35afefb61cd87da0d74bac8453d1bfa552e26b [file] [log] [blame]
Zhu Yibb9f8692009-05-21 21:20:45 +08001/*
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
51static 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
70int iwm_send_wifi_if_cmd(struct iwm_priv *iwm, void *payload, u16 payload_size,
71 bool resp)
72{
Samuel Ortiza70742f2009-06-15 21:59:51 +020073 struct iwm_umac_wifi_if *hdr = (struct iwm_umac_wifi_if *)payload;
Zhu Yibb9f8692009-05-21 21:20:45 +080074 struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
75 struct iwm_umac_cmd umac_cmd;
Samuel Ortiza70742f2009-06-15 21:59:51 +020076 int ret;
77 u8 oid = hdr->oid;
Zhu Yibb9f8692009-05-21 21:20:45 +080078
79 umac_cmd.id = UMAC_CMD_OPCODE_WIFI_IF_WRAPPER;
80 umac_cmd.resp = resp;
81
Samuel Ortiza70742f2009-06-15 21:59:51 +020082 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 if (!ret)
91 ret = -EBUSY;
92 }
93
94 return ret;
Zhu Yibb9f8692009-05-21 21:20:45 +080095}
96
97static struct coex_event iwm_sta_xor_prio_tbl[COEX_EVENTS_NUM] =
98{
99 {4, 3, 0, COEX_UNASSOC_IDLE_FLAGS},
100 {4, 3, 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
101 {4, 3, 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
102 {4, 3, 0, COEX_CALIBRATION_FLAGS},
103 {4, 3, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
104 {4, 3, 0, COEX_CONNECTION_ESTAB_FLAGS},
105 {4, 3, 0, COEX_ASSOCIATED_IDLE_FLAGS},
106 {4, 3, 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
107 {4, 3, 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
108 {4, 3, 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
109 {6, 3, 0, COEX_XOR_RF_ON_FLAGS},
110 {4, 3, 0, COEX_RF_OFF_FLAGS},
111 {6, 6, 0, COEX_STAND_ALONE_DEBUG_FLAGS},
112 {4, 3, 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
113 {4, 3, 0, COEX_RSRVD1_FLAGS},
114 {4, 3, 0, COEX_RSRVD2_FLAGS}
115};
116
117static struct coex_event iwm_sta_cm_prio_tbl[COEX_EVENTS_NUM] =
118{
119 {1, 1, 0, COEX_UNASSOC_IDLE_FLAGS},
120 {4, 3, 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
121 {3, 3, 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
122 {5, 5, 0, COEX_CALIBRATION_FLAGS},
Zhu Yi191506e2009-06-15 21:59:53 +0200123 {3, 3, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
Zhu Yibb9f8692009-05-21 21:20:45 +0800124 {5, 4, 0, COEX_CONNECTION_ESTAB_FLAGS},
125 {4, 4, 0, COEX_ASSOCIATED_IDLE_FLAGS},
126 {4, 4, 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
127 {4, 4, 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
128 {4, 4, 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
129 {1, 1, 0, COEX_RF_ON_FLAGS},
130 {1, 1, 0, COEX_RF_OFF_FLAGS},
131 {6, 6, 0, COEX_STAND_ALONE_DEBUG_FLAGS},
132 {5, 4, 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
133 {1, 1, 0, COEX_RSRVD1_FLAGS},
134 {1, 1, 0, COEX_RSRVD2_FLAGS}
135};
136
137int iwm_send_prio_table(struct iwm_priv *iwm)
138{
139 struct iwm_coex_prio_table_cmd coex_table_cmd;
140 u32 coex_enabled, mode_enabled;
141
142 memset(&coex_table_cmd, 0, sizeof(struct iwm_coex_prio_table_cmd));
143
144 coex_table_cmd.flags = COEX_FLAGS_STA_TABLE_VALID_MSK;
145
146 switch (iwm->conf.coexist_mode) {
147 case COEX_MODE_XOR:
148 case COEX_MODE_CM:
149 coex_enabled = 1;
150 break;
151 default:
152 coex_enabled = 0;
153 break;
154 }
155
156 switch (iwm->conf.mode) {
157 case UMAC_MODE_BSS:
158 case UMAC_MODE_IBSS:
159 mode_enabled = 1;
160 break;
161 default:
162 mode_enabled = 0;
163 break;
164 }
165
166 if (coex_enabled && mode_enabled) {
167 coex_table_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK |
168 COEX_FLAGS_ASSOC_WAKEUP_UMASK_MSK |
169 COEX_FLAGS_UNASSOC_WAKEUP_UMASK_MSK;
170
171 switch (iwm->conf.coexist_mode) {
172 case COEX_MODE_XOR:
173 memcpy(coex_table_cmd.sta_prio, iwm_sta_xor_prio_tbl,
174 sizeof(iwm_sta_xor_prio_tbl));
175 break;
176 case COEX_MODE_CM:
177 memcpy(coex_table_cmd.sta_prio, iwm_sta_cm_prio_tbl,
178 sizeof(iwm_sta_cm_prio_tbl));
179 break;
180 default:
181 IWM_ERR(iwm, "Invalid coex_mode 0x%x\n",
182 iwm->conf.coexist_mode);
183 break;
184 }
185 } else
186 IWM_WARN(iwm, "coexistense disabled\n");
187
188 return iwm_send_lmac_ptrough_cmd(iwm, COEX_PRIORITY_TABLE_CMD,
189 &coex_table_cmd,
190 sizeof(struct iwm_coex_prio_table_cmd), 1);
191}
192
193int iwm_send_init_calib_cfg(struct iwm_priv *iwm, u8 calib_requested)
194{
195 struct iwm_lmac_cal_cfg_cmd cal_cfg_cmd;
196
197 memset(&cal_cfg_cmd, 0, sizeof(struct iwm_lmac_cal_cfg_cmd));
198
199 cal_cfg_cmd.ucode_cfg.init.enable = cpu_to_le32(calib_requested);
200 cal_cfg_cmd.ucode_cfg.init.start = cpu_to_le32(calib_requested);
201 cal_cfg_cmd.ucode_cfg.init.send_res = cpu_to_le32(calib_requested);
202 cal_cfg_cmd.ucode_cfg.flags =
203 cpu_to_le32(CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_AFTER_MSK);
204
205 return iwm_send_lmac_ptrough_cmd(iwm, CALIBRATION_CFG_CMD, &cal_cfg_cmd,
206 sizeof(struct iwm_lmac_cal_cfg_cmd), 1);
207}
208
209int iwm_send_periodic_calib_cfg(struct iwm_priv *iwm, u8 calib_requested)
210{
211 struct iwm_lmac_cal_cfg_cmd cal_cfg_cmd;
212
213 memset(&cal_cfg_cmd, 0, sizeof(struct iwm_lmac_cal_cfg_cmd));
214
215 cal_cfg_cmd.ucode_cfg.periodic.enable = cpu_to_le32(calib_requested);
216 cal_cfg_cmd.ucode_cfg.periodic.start = cpu_to_le32(calib_requested);
217
218 return iwm_send_lmac_ptrough_cmd(iwm, CALIBRATION_CFG_CMD, &cal_cfg_cmd,
219 sizeof(struct iwm_lmac_cal_cfg_cmd), 0);
220}
221
222int iwm_store_rxiq_calib_result(struct iwm_priv *iwm)
223{
224 struct iwm_calib_rxiq *rxiq;
225 u8 *eeprom_rxiq = iwm_eeprom_access(iwm, IWM_EEPROM_CALIB_RXIQ);
226 int grplen = sizeof(struct iwm_calib_rxiq_group);
227
228 rxiq = kzalloc(sizeof(struct iwm_calib_rxiq), GFP_KERNEL);
229 if (!rxiq) {
230 IWM_ERR(iwm, "Couldn't alloc memory for RX IQ\n");
231 return -ENOMEM;
232 }
233
234 eeprom_rxiq = iwm_eeprom_access(iwm, IWM_EEPROM_CALIB_RXIQ);
235 if (IS_ERR(eeprom_rxiq)) {
236 IWM_ERR(iwm, "Couldn't access EEPROM RX IQ entry\n");
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
254int 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
277int iwm_send_umac_reset(struct iwm_priv *iwm, __le32 reset_flags, bool resp)
278{
279 struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
280 struct iwm_umac_cmd umac_cmd;
281 struct iwm_umac_cmd_reset reset;
282
283 reset.flags = reset_flags;
284
285 umac_cmd.id = UMAC_CMD_OPCODE_RESET;
286 umac_cmd.resp = resp;
287
288 return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &reset,
289 sizeof(struct iwm_umac_cmd_reset));
290}
291
292int iwm_umac_set_config_fix(struct iwm_priv *iwm, u16 tbl, u16 key, u32 value)
293{
294 struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
295 struct iwm_umac_cmd umac_cmd;
296 struct iwm_umac_cmd_set_param_fix param;
297
298 if ((tbl != UMAC_PARAM_TBL_CFG_FIX) &&
299 (tbl != UMAC_PARAM_TBL_FA_CFG_FIX))
300 return -EINVAL;
301
302 umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_FIX;
303 umac_cmd.resp = 0;
304
305 param.tbl = cpu_to_le16(tbl);
306 param.key = cpu_to_le16(key);
307 param.value = cpu_to_le32(value);
308
309 return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &param,
310 sizeof(struct iwm_umac_cmd_set_param_fix));
311}
312
313int iwm_umac_set_config_var(struct iwm_priv *iwm, u16 key,
314 void *payload, u16 payload_size)
315{
316 struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
317 struct iwm_umac_cmd umac_cmd;
318 struct iwm_umac_cmd_set_param_var *param_hdr;
319 u8 *param;
320 int ret;
321
322 param = kzalloc(payload_size +
323 sizeof(struct iwm_umac_cmd_set_param_var), GFP_KERNEL);
324 if (!param) {
325 IWM_ERR(iwm, "Couldn't allocate param\n");
326 return -ENOMEM;
327 }
328
329 param_hdr = (struct iwm_umac_cmd_set_param_var *)param;
330
331 umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_VAR;
332 umac_cmd.resp = 0;
333
334 param_hdr->tbl = cpu_to_le16(UMAC_PARAM_TBL_CFG_VAR);
335 param_hdr->key = cpu_to_le16(key);
336 param_hdr->len = cpu_to_le16(payload_size);
337 memcpy(param + sizeof(struct iwm_umac_cmd_set_param_var),
338 payload, payload_size);
339
340 ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, param,
341 sizeof(struct iwm_umac_cmd_set_param_var) +
342 payload_size);
343 kfree(param);
344
345 return ret;
346}
347
Zhu Yi191506e2009-06-15 21:59:53 +0200348int iwm_send_umac_config(struct iwm_priv *iwm, __le32 reset_flags)
Zhu Yibb9f8692009-05-21 21:20:45 +0800349{
350 int ret;
351
352 /* Use UMAC default values */
353 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
354 CFG_POWER_INDEX, iwm->conf.power_index);
355 if (ret < 0)
356 return ret;
357
358 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_FA_CFG_FIX,
359 CFG_FRAG_THRESHOLD,
360 iwm->conf.frag_threshold);
361 if (ret < 0)
362 return ret;
363
364 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
365 CFG_RTS_THRESHOLD,
366 iwm->conf.rts_threshold);
367 if (ret < 0)
368 return ret;
369
370 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
371 CFG_CTS_TO_SELF, iwm->conf.cts_to_self);
372 if (ret < 0)
373 return ret;
374
375 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
Zhu Yi191506e2009-06-15 21:59:53 +0200376 CFG_WIRELESS_MODE,
377 iwm->conf.wireless_mode);
378 if (ret < 0)
379 return ret;
380
381 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
Zhu Yibb9f8692009-05-21 21:20:45 +0800382 CFG_COEX_MODE, iwm->conf.coexist_mode);
383 if (ret < 0)
384 return ret;
385
386 /*
387 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
388 CFG_ASSOCIATION_TIMEOUT,
389 iwm->conf.assoc_timeout);
390 if (ret < 0)
391 return ret;
392
393 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
394 CFG_ROAM_TIMEOUT,
395 iwm->conf.roam_timeout);
396 if (ret < 0)
397 return ret;
398
399 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
400 CFG_WIRELESS_MODE,
401 WIRELESS_MODE_11A | WIRELESS_MODE_11G);
402 if (ret < 0)
403 return ret;
404 */
405
406 ret = iwm_umac_set_config_var(iwm, CFG_NET_ADDR,
407 iwm_to_ndev(iwm)->dev_addr, ETH_ALEN);
408 if (ret < 0)
409 return ret;
410
411 /* UMAC PM static configurations */
412 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
413 CFG_PM_LEGACY_RX_TIMEOUT, 0x12C);
414 if (ret < 0)
415 return ret;
416
417 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
418 CFG_PM_LEGACY_TX_TIMEOUT, 0x15E);
419 if (ret < 0)
420 return ret;
421
422 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
Zhu Yi191506e2009-06-15 21:59:53 +0200423 CFG_PM_CTRL_FLAGS, 0x1);
Zhu Yibb9f8692009-05-21 21:20:45 +0800424 if (ret < 0)
425 return ret;
426
427 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
428 CFG_PM_KEEP_ALIVE_IN_BEACONS, 0x80);
429 if (ret < 0)
430 return ret;
431
432 /* reset UMAC */
433 ret = iwm_send_umac_reset(iwm, reset_flags, 1);
434 if (ret < 0)
435 return ret;
436
437 ret = iwm_notif_handle(iwm, UMAC_CMD_OPCODE_RESET, IWM_SRC_UMAC,
438 WAIT_NOTIF_TIMEOUT);
439 if (ret) {
440 IWM_ERR(iwm, "Wait for UMAC RESET timeout\n");
441 return ret;
442 }
443
444 return ret;
445}
446
447int iwm_send_packet(struct iwm_priv *iwm, struct sk_buff *skb, int pool_id)
448{
449 struct iwm_udma_wifi_cmd udma_cmd;
450 struct iwm_umac_cmd umac_cmd;
451 struct iwm_tx_info *tx_info = skb_to_tx_info(skb);
452
453 udma_cmd.eop = 1; /* always set eop for non-concatenated Tx */
454 udma_cmd.credit_group = pool_id;
455 udma_cmd.ra_tid = tx_info->sta << 4 | tx_info->tid;
456 udma_cmd.lmac_offset = 0;
457
458 umac_cmd.id = REPLY_TX;
459 umac_cmd.color = tx_info->color;
460 umac_cmd.resp = 0;
461
462 return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
463 skb->data, skb->len);
464}
465
466static int iwm_target_read(struct iwm_priv *iwm, __le32 address,
467 u8 *response, u32 resp_size)
468{
469 struct iwm_udma_nonwifi_cmd target_cmd;
470 struct iwm_nonwifi_cmd *cmd;
471 u16 seq_num;
472 int ret = 0;
473
474 target_cmd.opcode = UMAC_HDI_OUT_OPCODE_READ;
475 target_cmd.addr = address;
476 target_cmd.op1_sz = cpu_to_le32(resp_size);
477 target_cmd.op2 = 0;
478 target_cmd.handle_by_hw = 0;
479 target_cmd.resp = 1;
480 target_cmd.eop = 1;
481
482 ret = iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
483 if (ret < 0)
484 IWM_ERR(iwm, "Couldn't send READ command\n");
485
486 /* When succeding, the send_target routine returns the seq number */
487 seq_num = ret;
488
489 ret = wait_event_interruptible_timeout(iwm->nonwifi_queue,
490 (cmd = iwm_get_pending_nonwifi_cmd(iwm, seq_num,
491 UMAC_HDI_OUT_OPCODE_READ)) != NULL,
492 2 * HZ);
493
494 if (!ret) {
495 IWM_ERR(iwm, "Didn't receive a target READ answer\n");
496 return ret;
497 }
498
499 memcpy(response, cmd->buf.hdr + sizeof(struct iwm_udma_in_hdr),
500 resp_size);
501
502 kfree(cmd);
503
504 return ret;
505}
506
507int iwm_read_mac(struct iwm_priv *iwm, u8 *mac)
508{
509 int ret;
510 u8 mac_align[ALIGN(ETH_ALEN, 8)];
511
512 ret = iwm_target_read(iwm, cpu_to_le32(WICO_MAC_ADDRESS_ADDR),
513 mac_align, sizeof(mac_align));
514 if (ret < 0)
515 return ret;
516
517 if (is_valid_ether_addr(mac_align))
518 memcpy(mac, mac_align, ETH_ALEN);
519 else {
520 IWM_ERR(iwm, "Invalid EEPROM MAC\n");
521 memcpy(mac, iwm->conf.mac_addr, ETH_ALEN);
522 get_random_bytes(&mac[3], 3);
523 }
524
525 return 0;
526}
527
528int iwm_set_tx_key(struct iwm_priv *iwm, u8 key_idx)
529{
530 struct iwm_umac_tx_key_id tx_key_id;
531
Zhu Yibb9f8692009-05-21 21:20:45 +0800532 tx_key_id.hdr.oid = UMAC_WIFI_IF_CMD_GLOBAL_TX_KEY_ID;
533 tx_key_id.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_tx_key_id) -
534 sizeof(struct iwm_umac_wifi_if));
535
536 tx_key_id.key_idx = key_idx;
537
538 return iwm_send_wifi_if_cmd(iwm, &tx_key_id, sizeof(tx_key_id), 1);
539}
540
541static 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
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200574int iwm_set_key(struct iwm_priv *iwm, bool remove, struct iwm_key *key)
Zhu Yibb9f8692009-05-21 21:20:45 +0800575{
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200576 int ret = 0;
Zhu Yibb9f8692009-05-21 21:20:45 +0800577 u8 cmd[64], *sta_addr, *key_data, key_len;
578 s8 key_idx;
579 u16 cmd_size = 0;
580 struct iwm_umac_key_hdr *key_hdr = &key->hdr;
581 struct iwm_umac_key_wep40 *wep40 = (struct iwm_umac_key_wep40 *)cmd;
582 struct iwm_umac_key_wep104 *wep104 = (struct iwm_umac_key_wep104 *)cmd;
583 struct iwm_umac_key_tkip *tkip = (struct iwm_umac_key_tkip *)cmd;
584 struct iwm_umac_key_ccmp *ccmp = (struct iwm_umac_key_ccmp *)cmd;
585
Zhu Yibb9f8692009-05-21 21:20:45 +0800586 /*
587 * We check if our current profile is valid.
588 * If not, we dont push the key, we just cache them,
589 * so that with the next siwsessid call, the keys
590 * will be actually pushed.
591 */
592 if (!remove) {
593 ret = iwm_check_profile(iwm);
594 if (ret < 0)
595 return ret;
596 }
597
598 sta_addr = key->hdr.mac;
599 key_data = key->key;
600 key_len = key->key_len;
601 key_idx = key->hdr.key_idx;
602
603 if (!remove) {
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200604 IWM_DBG_WEXT(iwm, DBG, "key_idx:%d\n", key_idx);
Zhu Yibb9f8692009-05-21 21:20:45 +0800605 IWM_DBG_WEXT(iwm, DBG, "key_len:%d\n", key_len);
606 IWM_DBG_WEXT(iwm, DBG, "MAC:%pM, idx:%d, multicast:%d\n",
607 key_hdr->mac, key_hdr->key_idx, key_hdr->multicast);
608
609 IWM_DBG_WEXT(iwm, DBG, "profile: mcast:0x%x, ucast:0x%x\n",
610 iwm->umac_profile->sec.mcast_cipher,
611 iwm->umac_profile->sec.ucast_cipher);
612 IWM_DBG_WEXT(iwm, DBG, "profile: auth_type:0x%x, flags:0x%x\n",
613 iwm->umac_profile->sec.auth_type,
614 iwm->umac_profile->sec.flags);
615
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200616 switch (key->cipher) {
617 case WLAN_CIPHER_SUITE_WEP40:
Zhu Yibb9f8692009-05-21 21:20:45 +0800618 wep40->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP40_KEY;
619 wep40->hdr.buf_size =
620 cpu_to_le16(sizeof(struct iwm_umac_key_wep40) -
621 sizeof(struct iwm_umac_wifi_if));
622
623 memcpy(&wep40->key_hdr, key_hdr,
624 sizeof(struct iwm_umac_key_hdr));
625 memcpy(wep40->key, key_data, key_len);
626 wep40->static_key = 1;
627
628 cmd_size = sizeof(struct iwm_umac_key_wep40);
629 break;
630
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200631 case WLAN_CIPHER_SUITE_WEP104:
Zhu Yibb9f8692009-05-21 21:20:45 +0800632 wep104->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP104_KEY;
633 wep104->hdr.buf_size =
634 cpu_to_le16(sizeof(struct iwm_umac_key_wep104) -
635 sizeof(struct iwm_umac_wifi_if));
636
637 memcpy(&wep104->key_hdr, key_hdr,
638 sizeof(struct iwm_umac_key_hdr));
639 memcpy(wep104->key, key_data, key_len);
640 wep104->static_key = 1;
641
642 cmd_size = sizeof(struct iwm_umac_key_wep104);
643 break;
644
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200645 case WLAN_CIPHER_SUITE_CCMP:
Zhu Yibb9f8692009-05-21 21:20:45 +0800646 key_hdr->key_idx++;
647 ccmp->hdr.oid = UMAC_WIFI_IF_CMD_ADD_CCMP_KEY;
648 ccmp->hdr.buf_size =
649 cpu_to_le16(sizeof(struct iwm_umac_key_ccmp) -
650 sizeof(struct iwm_umac_wifi_if));
651
652 memcpy(&ccmp->key_hdr, key_hdr,
653 sizeof(struct iwm_umac_key_hdr));
654
655 memcpy(ccmp->key, key_data, key_len);
656
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200657 if (key->seq_len)
658 memcpy(ccmp->iv_count, key->seq, key->seq_len);
Zhu Yibb9f8692009-05-21 21:20:45 +0800659
660 cmd_size = sizeof(struct iwm_umac_key_ccmp);
661 break;
662
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200663 case WLAN_CIPHER_SUITE_TKIP:
Zhu Yibb9f8692009-05-21 21:20:45 +0800664 key_hdr->key_idx++;
665 tkip->hdr.oid = UMAC_WIFI_IF_CMD_ADD_TKIP_KEY;
666 tkip->hdr.buf_size =
667 cpu_to_le16(sizeof(struct iwm_umac_key_tkip) -
668 sizeof(struct iwm_umac_wifi_if));
669
670 memcpy(&tkip->key_hdr, key_hdr,
671 sizeof(struct iwm_umac_key_hdr));
672
673 memcpy(tkip->tkip_key, key_data, IWM_TKIP_KEY_SIZE);
674 memcpy(tkip->mic_tx_key, key_data + IWM_TKIP_KEY_SIZE,
675 IWM_TKIP_MIC_SIZE);
676 memcpy(tkip->mic_rx_key,
677 key_data + IWM_TKIP_KEY_SIZE + IWM_TKIP_MIC_SIZE,
678 IWM_TKIP_MIC_SIZE);
679
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200680 if (key->seq_len)
681 memcpy(ccmp->iv_count, key->seq, key->seq_len);
Zhu Yibb9f8692009-05-21 21:20:45 +0800682
683 cmd_size = sizeof(struct iwm_umac_key_tkip);
684 break;
685
686 default:
687 return -ENOTSUPP;
688 }
689
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200690 if ((key->cipher == WLAN_CIPHER_SUITE_TKIP) ||
691 (key->cipher == WLAN_CIPHER_SUITE_CCMP))
Zhu Yibb9f8692009-05-21 21:20:45 +0800692 /*
693 * UGLY_UGLY_UGLY
694 * Copied HACK from the MWG driver.
695 * Without it, the key is set before the second
696 * EAPOL frame is sent, and the latter is thus
697 * encrypted.
698 */
699 schedule_timeout_interruptible(usecs_to_jiffies(300));
700
701 ret = iwm_send_wifi_if_cmd(iwm, cmd, cmd_size, 1);
Zhu Yibb9f8692009-05-21 21:20:45 +0800702 } else {
703 struct iwm_umac_key_remove key_remove;
704
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200705 IWM_DBG_WEXT(iwm, ERR, "Removing key_idx:%d\n", key_idx);
706
Zhu Yibb9f8692009-05-21 21:20:45 +0800707 key_remove.hdr.oid = UMAC_WIFI_IF_CMD_REMOVE_KEY;
708 key_remove.hdr.buf_size =
709 cpu_to_le16(sizeof(struct iwm_umac_key_remove) -
710 sizeof(struct iwm_umac_wifi_if));
711 memcpy(&key_remove.key_hdr, key_hdr,
712 sizeof(struct iwm_umac_key_hdr));
713
714 ret = iwm_send_wifi_if_cmd(iwm, &key_remove,
715 sizeof(struct iwm_umac_key_remove),
716 1);
717 if (ret < 0)
718 return ret;
719
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200720 iwm->keys[key_idx].key_len = 0;
Zhu Yibb9f8692009-05-21 21:20:45 +0800721 }
722
Zhu Yibb9f8692009-05-21 21:20:45 +0800723 return ret;
724}
725
726
727int iwm_send_mlme_profile(struct iwm_priv *iwm)
728{
729 int ret, i;
730 struct iwm_umac_profile profile;
731
732 memcpy(&profile, iwm->umac_profile, sizeof(profile));
733
734 profile.hdr.oid = UMAC_WIFI_IF_CMD_SET_PROFILE;
735 profile.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_profile) -
736 sizeof(struct iwm_umac_wifi_if));
737
738 ret = iwm_send_wifi_if_cmd(iwm, &profile, sizeof(profile), 1);
739 if (ret < 0) {
740 IWM_ERR(iwm, "Send profile command failed\n");
741 return ret;
742 }
743
Zhu Yibb9f8692009-05-21 21:20:45 +0800744 for (i = 0; i < IWM_NUM_KEYS; i++)
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200745 if (iwm->keys[i].key_len) {
Zhu Yibb9f8692009-05-21 21:20:45 +0800746 struct iwm_key *key = &iwm->keys[i];
747
Zhu Yibb9f8692009-05-21 21:20:45 +0800748 /* Wait for the profile before sending the keys */
749 wait_event_interruptible_timeout(iwm->mlme_queue,
750 (test_bit(IWM_STATUS_ASSOCIATING, &iwm->status) ||
751 test_bit(IWM_STATUS_ASSOCIATED, &iwm->status)),
752 3 * HZ);
753
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200754 ret = iwm_set_key(iwm, 0, key);
Zhu Yibb9f8692009-05-21 21:20:45 +0800755 if (ret < 0)
756 return ret;
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200757
758 if (iwm->default_key == i) {
759 ret = iwm_set_tx_key(iwm, i);
760 if (ret < 0)
761 return ret;
762 }
Zhu Yibb9f8692009-05-21 21:20:45 +0800763 }
764
765 return 0;
766}
767
768int iwm_invalidate_mlme_profile(struct iwm_priv *iwm)
769{
Zhu Yibb9f8692009-05-21 21:20:45 +0800770 struct iwm_umac_invalidate_profile invalid;
Samuel Ortiza70742f2009-06-15 21:59:51 +0200771 int ret;
Zhu Yibb9f8692009-05-21 21:20:45 +0800772
773 invalid.hdr.oid = UMAC_WIFI_IF_CMD_INVALIDATE_PROFILE;
774 invalid.hdr.buf_size =
775 cpu_to_le16(sizeof(struct iwm_umac_invalidate_profile) -
776 sizeof(struct iwm_umac_wifi_if));
777
778 invalid.reason = WLAN_REASON_UNSPECIFIED;
779
780 ret = iwm_send_wifi_if_cmd(iwm, &invalid, sizeof(invalid), 1);
781 if (ret < 0)
782 return ret;
783
784 ret = wait_event_interruptible_timeout(iwm->mlme_queue,
Samuel Ortiza70742f2009-06-15 21:59:51 +0200785 (iwm->umac_profile_active == 0), 2 * HZ);
Zhu Yibb9f8692009-05-21 21:20:45 +0800786 if (!ret)
787 return -EBUSY;
788
789 return 0;
790}
791
792int iwm_send_umac_stats_req(struct iwm_priv *iwm, u32 flags)
793{
794 struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
795 struct iwm_umac_cmd umac_cmd;
796 struct iwm_umac_cmd_stats_req stats_req;
797
798 stats_req.flags = cpu_to_le32(flags);
799
800 umac_cmd.id = UMAC_CMD_OPCODE_STATISTIC_REQUEST;
801 umac_cmd.resp = 0;
802
803 return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &stats_req,
804 sizeof(struct iwm_umac_cmd_stats_req));
805}
806
807int iwm_send_umac_channel_list(struct iwm_priv *iwm)
808{
809 struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
810 struct iwm_umac_cmd umac_cmd;
811 struct iwm_umac_cmd_get_channel_list *ch_list;
812 int size = sizeof(struct iwm_umac_cmd_get_channel_list) +
813 sizeof(struct iwm_umac_channel_info) * 4;
814 int ret;
815
816 ch_list = kzalloc(size, GFP_KERNEL);
817 if (!ch_list) {
818 IWM_ERR(iwm, "Couldn't allocate channel list cmd\n");
819 return -ENOMEM;
820 }
821
822 ch_list->ch[0].band = UMAC_BAND_2GHZ;
823 ch_list->ch[0].type = UMAC_CHANNEL_WIDTH_20MHZ;
824 ch_list->ch[0].flags = UMAC_CHANNEL_FLAG_VALID;
825
826 ch_list->ch[1].band = UMAC_BAND_5GHZ;
827 ch_list->ch[1].type = UMAC_CHANNEL_WIDTH_20MHZ;
828 ch_list->ch[1].flags = UMAC_CHANNEL_FLAG_VALID;
829
830 ch_list->ch[2].band = UMAC_BAND_2GHZ;
831 ch_list->ch[2].type = UMAC_CHANNEL_WIDTH_20MHZ;
832 ch_list->ch[2].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
833
834 ch_list->ch[3].band = UMAC_BAND_5GHZ;
835 ch_list->ch[3].type = UMAC_CHANNEL_WIDTH_20MHZ;
836 ch_list->ch[3].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
837
838 ch_list->count = cpu_to_le16(4);
839
840 umac_cmd.id = UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST;
841 umac_cmd.resp = 1;
842
843 ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, ch_list, size);
844
845 kfree(ch_list);
846
847 return ret;
848}
849
850int iwm_scan_ssids(struct iwm_priv *iwm, struct cfg80211_ssid *ssids,
851 int ssid_num)
852{
853 struct iwm_umac_cmd_scan_request req;
854 int i, ret;
855
856 memset(&req, 0, sizeof(struct iwm_umac_cmd_scan_request));
857
858 req.hdr.oid = UMAC_WIFI_IF_CMD_SCAN_REQUEST;
859 req.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_cmd_scan_request)
860 - sizeof(struct iwm_umac_wifi_if));
861 req.type = UMAC_WIFI_IF_SCAN_TYPE_USER;
862 req.timeout = 2;
863 req.seq_num = iwm->scan_id;
864 req.ssid_num = min(ssid_num, UMAC_WIFI_IF_PROBE_OPTION_MAX);
865
866 for (i = 0; i < req.ssid_num; i++) {
867 memcpy(req.ssids[i].ssid, ssids[i].ssid, ssids[i].ssid_len);
868 req.ssids[i].ssid_len = ssids[i].ssid_len;
869 }
870
871 ret = iwm_send_wifi_if_cmd(iwm, &req, sizeof(req), 0);
872 if (ret < 0) {
873 IWM_ERR(iwm, "Couldn't send scan request\n");
874 return ret;
875 }
876
877 iwm->scan_id = iwm->scan_id++ % IWM_SCAN_ID_MAX;
878
879 return 0;
880}
881
882int iwm_scan_one_ssid(struct iwm_priv *iwm, u8 *ssid, int ssid_len)
883{
884 struct cfg80211_ssid one_ssid;
885
886 if (test_and_set_bit(IWM_STATUS_SCANNING, &iwm->status))
887 return 0;
888
889 one_ssid.ssid_len = min(ssid_len, IEEE80211_MAX_SSID_LEN);
890 memcpy(&one_ssid.ssid, ssid, one_ssid.ssid_len);
891
892 return iwm_scan_ssids(iwm, &one_ssid, 1);
893}
894
895int iwm_target_reset(struct iwm_priv *iwm)
896{
897 struct iwm_udma_nonwifi_cmd target_cmd;
898
899 target_cmd.opcode = UMAC_HDI_OUT_OPCODE_REBOOT;
900 target_cmd.addr = 0;
901 target_cmd.op1_sz = 0;
902 target_cmd.op2 = 0;
903 target_cmd.handle_by_hw = 0;
904 target_cmd.resp = 0;
905 target_cmd.eop = 1;
906
907 return iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
908}