blob: 6b3626013fd2118db174ff7605da50f78c336306 [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
Zhu Yib6c32172009-07-16 17:34:12 +080090 return ret ? 0 : -EBUSY;
Samuel Ortiza70742f2009-06-15 21:59:51 +020091 }
92
93 return ret;
Zhu Yibb9f8692009-05-21 21:20:45 +080094}
95
96static 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
116static 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},
Zhu Yi191506e2009-06-15 21:59:53 +0200122 {3, 3, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
Zhu Yibb9f8692009-05-21 21:20:45 +0800123 {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
136int 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
192int 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
208int 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
221int 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 return PTR_ERR(eeprom_rxiq);
237 }
238
239 iwm->calib_res[SHILOH_PHY_CALIBRATE_RX_IQ_CMD].buf = (u8 *)rxiq;
240 iwm->calib_res[SHILOH_PHY_CALIBRATE_RX_IQ_CMD].size = sizeof(*rxiq);
241
242 rxiq->hdr.opcode = SHILOH_PHY_CALIBRATE_RX_IQ_CMD;
243 rxiq->hdr.first_grp = 0;
244 rxiq->hdr.grp_num = 1;
245 rxiq->hdr.all_data_valid = 1;
246
247 memcpy(&rxiq->group[0], eeprom_rxiq, 4 * grplen);
248 memcpy(&rxiq->group[4], eeprom_rxiq + 6 * grplen, grplen);
249
250 return 0;
251}
252
253int iwm_send_calib_results(struct iwm_priv *iwm)
254{
255 int i, ret = 0;
256
257 for (i = PHY_CALIBRATE_OPCODES_NUM; i < CALIBRATION_CMD_NUM; i++) {
258 if (test_bit(i - PHY_CALIBRATE_OPCODES_NUM,
259 &iwm->calib_done_map)) {
260 IWM_DBG_CMD(iwm, DBG,
261 "Send calibration %d result\n", i);
262 ret |= iwm_send_lmac_ptrough_cmd(iwm,
263 REPLY_PHY_CALIBRATION_CMD,
264 iwm->calib_res[i].buf,
265 iwm->calib_res[i].size, 0);
266
267 kfree(iwm->calib_res[i].buf);
268 iwm->calib_res[i].buf = NULL;
269 iwm->calib_res[i].size = 0;
270 }
271 }
272
273 return ret;
274}
275
276int iwm_send_umac_reset(struct iwm_priv *iwm, __le32 reset_flags, bool resp)
277{
278 struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
279 struct iwm_umac_cmd umac_cmd;
280 struct iwm_umac_cmd_reset reset;
281
282 reset.flags = reset_flags;
283
284 umac_cmd.id = UMAC_CMD_OPCODE_RESET;
285 umac_cmd.resp = resp;
286
287 return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &reset,
288 sizeof(struct iwm_umac_cmd_reset));
289}
290
291int iwm_umac_set_config_fix(struct iwm_priv *iwm, u16 tbl, u16 key, u32 value)
292{
293 struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
294 struct iwm_umac_cmd umac_cmd;
295 struct iwm_umac_cmd_set_param_fix param;
296
297 if ((tbl != UMAC_PARAM_TBL_CFG_FIX) &&
298 (tbl != UMAC_PARAM_TBL_FA_CFG_FIX))
299 return -EINVAL;
300
301 umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_FIX;
302 umac_cmd.resp = 0;
303
304 param.tbl = cpu_to_le16(tbl);
305 param.key = cpu_to_le16(key);
306 param.value = cpu_to_le32(value);
307
308 return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &param,
309 sizeof(struct iwm_umac_cmd_set_param_fix));
310}
311
312int iwm_umac_set_config_var(struct iwm_priv *iwm, u16 key,
313 void *payload, u16 payload_size)
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_var *param_hdr;
318 u8 *param;
319 int ret;
320
321 param = kzalloc(payload_size +
322 sizeof(struct iwm_umac_cmd_set_param_var), GFP_KERNEL);
323 if (!param) {
324 IWM_ERR(iwm, "Couldn't allocate param\n");
325 return -ENOMEM;
326 }
327
328 param_hdr = (struct iwm_umac_cmd_set_param_var *)param;
329
330 umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_VAR;
331 umac_cmd.resp = 0;
332
333 param_hdr->tbl = cpu_to_le16(UMAC_PARAM_TBL_CFG_VAR);
334 param_hdr->key = cpu_to_le16(key);
335 param_hdr->len = cpu_to_le16(payload_size);
336 memcpy(param + sizeof(struct iwm_umac_cmd_set_param_var),
337 payload, payload_size);
338
339 ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, param,
340 sizeof(struct iwm_umac_cmd_set_param_var) +
341 payload_size);
342 kfree(param);
343
344 return ret;
345}
346
Zhu Yi191506e2009-06-15 21:59:53 +0200347int iwm_send_umac_config(struct iwm_priv *iwm, __le32 reset_flags)
Zhu Yibb9f8692009-05-21 21:20:45 +0800348{
349 int ret;
350
351 /* Use UMAC default values */
352 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
353 CFG_POWER_INDEX, iwm->conf.power_index);
354 if (ret < 0)
355 return ret;
356
357 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_FA_CFG_FIX,
358 CFG_FRAG_THRESHOLD,
359 iwm->conf.frag_threshold);
360 if (ret < 0)
361 return ret;
362
363 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
364 CFG_RTS_THRESHOLD,
365 iwm->conf.rts_threshold);
366 if (ret < 0)
367 return ret;
368
369 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
370 CFG_CTS_TO_SELF, iwm->conf.cts_to_self);
371 if (ret < 0)
372 return ret;
373
374 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
Zhu Yi191506e2009-06-15 21:59:53 +0200375 CFG_WIRELESS_MODE,
376 iwm->conf.wireless_mode);
377 if (ret < 0)
378 return ret;
379
380 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
Zhu Yibb9f8692009-05-21 21:20:45 +0800381 CFG_COEX_MODE, iwm->conf.coexist_mode);
382 if (ret < 0)
383 return ret;
384
385 /*
386 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
387 CFG_ASSOCIATION_TIMEOUT,
388 iwm->conf.assoc_timeout);
389 if (ret < 0)
390 return ret;
391
392 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
393 CFG_ROAM_TIMEOUT,
394 iwm->conf.roam_timeout);
395 if (ret < 0)
396 return ret;
397
398 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
399 CFG_WIRELESS_MODE,
400 WIRELESS_MODE_11A | WIRELESS_MODE_11G);
401 if (ret < 0)
402 return ret;
403 */
404
405 ret = iwm_umac_set_config_var(iwm, CFG_NET_ADDR,
406 iwm_to_ndev(iwm)->dev_addr, ETH_ALEN);
407 if (ret < 0)
408 return ret;
409
410 /* UMAC PM static configurations */
411 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
412 CFG_PM_LEGACY_RX_TIMEOUT, 0x12C);
413 if (ret < 0)
414 return ret;
415
416 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
417 CFG_PM_LEGACY_TX_TIMEOUT, 0x15E);
418 if (ret < 0)
419 return ret;
420
421 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
Zhu Yi191506e2009-06-15 21:59:53 +0200422 CFG_PM_CTRL_FLAGS, 0x1);
Zhu Yibb9f8692009-05-21 21:20:45 +0800423 if (ret < 0)
424 return ret;
425
426 ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
427 CFG_PM_KEEP_ALIVE_IN_BEACONS, 0x80);
428 if (ret < 0)
429 return ret;
430
431 /* reset UMAC */
432 ret = iwm_send_umac_reset(iwm, reset_flags, 1);
433 if (ret < 0)
434 return ret;
435
436 ret = iwm_notif_handle(iwm, UMAC_CMD_OPCODE_RESET, IWM_SRC_UMAC,
437 WAIT_NOTIF_TIMEOUT);
438 if (ret) {
439 IWM_ERR(iwm, "Wait for UMAC RESET timeout\n");
440 return ret;
441 }
442
443 return ret;
444}
445
446int iwm_send_packet(struct iwm_priv *iwm, struct sk_buff *skb, int pool_id)
447{
448 struct iwm_udma_wifi_cmd udma_cmd;
449 struct iwm_umac_cmd umac_cmd;
450 struct iwm_tx_info *tx_info = skb_to_tx_info(skb);
451
452 udma_cmd.eop = 1; /* always set eop for non-concatenated Tx */
453 udma_cmd.credit_group = pool_id;
454 udma_cmd.ra_tid = tx_info->sta << 4 | tx_info->tid;
455 udma_cmd.lmac_offset = 0;
456
457 umac_cmd.id = REPLY_TX;
458 umac_cmd.color = tx_info->color;
459 umac_cmd.resp = 0;
460
461 return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
462 skb->data, skb->len);
463}
464
465static int iwm_target_read(struct iwm_priv *iwm, __le32 address,
466 u8 *response, u32 resp_size)
467{
468 struct iwm_udma_nonwifi_cmd target_cmd;
469 struct iwm_nonwifi_cmd *cmd;
470 u16 seq_num;
471 int ret = 0;
472
473 target_cmd.opcode = UMAC_HDI_OUT_OPCODE_READ;
474 target_cmd.addr = address;
475 target_cmd.op1_sz = cpu_to_le32(resp_size);
476 target_cmd.op2 = 0;
477 target_cmd.handle_by_hw = 0;
478 target_cmd.resp = 1;
479 target_cmd.eop = 1;
480
481 ret = iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
Zhu Yib6c32172009-07-16 17:34:12 +0800482 if (ret < 0) {
Zhu Yibb9f8692009-05-21 21:20:45 +0800483 IWM_ERR(iwm, "Couldn't send READ command\n");
Zhu Yib6c32172009-07-16 17:34:12 +0800484 return ret;
485 }
Zhu Yibb9f8692009-05-21 21:20:45 +0800486
487 /* When succeding, the send_target routine returns the seq number */
488 seq_num = ret;
489
490 ret = wait_event_interruptible_timeout(iwm->nonwifi_queue,
491 (cmd = iwm_get_pending_nonwifi_cmd(iwm, seq_num,
492 UMAC_HDI_OUT_OPCODE_READ)) != NULL,
493 2 * HZ);
494
495 if (!ret) {
496 IWM_ERR(iwm, "Didn't receive a target READ answer\n");
497 return ret;
498 }
499
500 memcpy(response, cmd->buf.hdr + sizeof(struct iwm_udma_in_hdr),
501 resp_size);
502
503 kfree(cmd);
504
Zhu Yib6c32172009-07-16 17:34:12 +0800505 return 0;
Zhu Yibb9f8692009-05-21 21:20:45 +0800506}
507
508int iwm_read_mac(struct iwm_priv *iwm, u8 *mac)
509{
510 int ret;
511 u8 mac_align[ALIGN(ETH_ALEN, 8)];
512
513 ret = iwm_target_read(iwm, cpu_to_le32(WICO_MAC_ADDRESS_ADDR),
514 mac_align, sizeof(mac_align));
Zhu Yib6c32172009-07-16 17:34:12 +0800515 if (ret)
Zhu Yibb9f8692009-05-21 21:20:45 +0800516 return ret;
517
518 if (is_valid_ether_addr(mac_align))
519 memcpy(mac, mac_align, ETH_ALEN);
520 else {
521 IWM_ERR(iwm, "Invalid EEPROM MAC\n");
522 memcpy(mac, iwm->conf.mac_addr, ETH_ALEN);
523 get_random_bytes(&mac[3], 3);
524 }
525
526 return 0;
527}
528
529int iwm_set_tx_key(struct iwm_priv *iwm, u8 key_idx)
530{
531 struct iwm_umac_tx_key_id tx_key_id;
532
Zhu Yibb9f8692009-05-21 21:20:45 +0800533 tx_key_id.hdr.oid = UMAC_WIFI_IF_CMD_GLOBAL_TX_KEY_ID;
534 tx_key_id.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_tx_key_id) -
535 sizeof(struct iwm_umac_wifi_if));
536
537 tx_key_id.key_idx = key_idx;
538
539 return iwm_send_wifi_if_cmd(iwm, &tx_key_id, sizeof(tx_key_id), 1);
540}
541
542static int iwm_check_profile(struct iwm_priv *iwm)
543{
544 if (!iwm->umac_profile_active)
545 return -EAGAIN;
546
547 if (iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
548 iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
549 iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_TKIP &&
550 iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_CCMP) {
551 IWM_ERR(iwm, "Wrong unicast cipher: 0x%x\n",
552 iwm->umac_profile->sec.ucast_cipher);
553 return -EAGAIN;
554 }
555
556 if (iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
557 iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
558 iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_TKIP &&
559 iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_CCMP) {
560 IWM_ERR(iwm, "Wrong multicast cipher: 0x%x\n",
561 iwm->umac_profile->sec.mcast_cipher);
562 return -EAGAIN;
563 }
564
565 if ((iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_40 ||
566 iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_104) &&
567 (iwm->umac_profile->sec.ucast_cipher !=
568 iwm->umac_profile->sec.mcast_cipher)) {
569 IWM_ERR(iwm, "Unicast and multicast ciphers differ for WEP\n");
570 }
571
572 return 0;
573}
574
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200575int iwm_set_key(struct iwm_priv *iwm, bool remove, struct iwm_key *key)
Zhu Yibb9f8692009-05-21 21:20:45 +0800576{
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200577 int ret = 0;
Zhu Yibb9f8692009-05-21 21:20:45 +0800578 u8 cmd[64], *sta_addr, *key_data, key_len;
579 s8 key_idx;
580 u16 cmd_size = 0;
581 struct iwm_umac_key_hdr *key_hdr = &key->hdr;
582 struct iwm_umac_key_wep40 *wep40 = (struct iwm_umac_key_wep40 *)cmd;
583 struct iwm_umac_key_wep104 *wep104 = (struct iwm_umac_key_wep104 *)cmd;
584 struct iwm_umac_key_tkip *tkip = (struct iwm_umac_key_tkip *)cmd;
585 struct iwm_umac_key_ccmp *ccmp = (struct iwm_umac_key_ccmp *)cmd;
586
Zhu Yibb9f8692009-05-21 21:20:45 +0800587 if (!remove) {
588 ret = iwm_check_profile(iwm);
589 if (ret < 0)
590 return ret;
591 }
592
593 sta_addr = key->hdr.mac;
594 key_data = key->key;
595 key_len = key->key_len;
596 key_idx = key->hdr.key_idx;
597
598 if (!remove) {
Zhu Yibeda2782009-08-03 14:37:02 +0800599 u8 auth_type = iwm->umac_profile->sec.auth_type;
600
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200601 IWM_DBG_WEXT(iwm, DBG, "key_idx:%d\n", key_idx);
Zhu Yibb9f8692009-05-21 21:20:45 +0800602 IWM_DBG_WEXT(iwm, DBG, "key_len:%d\n", key_len);
603 IWM_DBG_WEXT(iwm, DBG, "MAC:%pM, idx:%d, multicast:%d\n",
604 key_hdr->mac, key_hdr->key_idx, key_hdr->multicast);
605
606 IWM_DBG_WEXT(iwm, DBG, "profile: mcast:0x%x, ucast:0x%x\n",
607 iwm->umac_profile->sec.mcast_cipher,
608 iwm->umac_profile->sec.ucast_cipher);
609 IWM_DBG_WEXT(iwm, DBG, "profile: auth_type:0x%x, flags:0x%x\n",
610 iwm->umac_profile->sec.auth_type,
611 iwm->umac_profile->sec.flags);
612
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200613 switch (key->cipher) {
614 case WLAN_CIPHER_SUITE_WEP40:
Zhu Yibb9f8692009-05-21 21:20:45 +0800615 wep40->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP40_KEY;
616 wep40->hdr.buf_size =
617 cpu_to_le16(sizeof(struct iwm_umac_key_wep40) -
618 sizeof(struct iwm_umac_wifi_if));
619
620 memcpy(&wep40->key_hdr, key_hdr,
621 sizeof(struct iwm_umac_key_hdr));
622 memcpy(wep40->key, key_data, key_len);
Zhu Yibeda2782009-08-03 14:37:02 +0800623 wep40->static_key =
624 !!((auth_type != UMAC_AUTH_TYPE_8021X) &&
625 (auth_type != UMAC_AUTH_TYPE_RSNA_PSK));
Zhu Yibb9f8692009-05-21 21:20:45 +0800626
627 cmd_size = sizeof(struct iwm_umac_key_wep40);
628 break;
629
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200630 case WLAN_CIPHER_SUITE_WEP104:
Zhu Yibb9f8692009-05-21 21:20:45 +0800631 wep104->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP104_KEY;
632 wep104->hdr.buf_size =
633 cpu_to_le16(sizeof(struct iwm_umac_key_wep104) -
634 sizeof(struct iwm_umac_wifi_if));
635
636 memcpy(&wep104->key_hdr, key_hdr,
637 sizeof(struct iwm_umac_key_hdr));
638 memcpy(wep104->key, key_data, key_len);
Zhu Yibeda2782009-08-03 14:37:02 +0800639 wep104->static_key =
640 !!((auth_type != UMAC_AUTH_TYPE_8021X) &&
641 (auth_type != UMAC_AUTH_TYPE_RSNA_PSK));
Zhu Yibb9f8692009-05-21 21:20:45 +0800642
643 cmd_size = sizeof(struct iwm_umac_key_wep104);
644 break;
645
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200646 case WLAN_CIPHER_SUITE_CCMP:
Zhu Yibb9f8692009-05-21 21:20:45 +0800647 key_hdr->key_idx++;
648 ccmp->hdr.oid = UMAC_WIFI_IF_CMD_ADD_CCMP_KEY;
649 ccmp->hdr.buf_size =
650 cpu_to_le16(sizeof(struct iwm_umac_key_ccmp) -
651 sizeof(struct iwm_umac_wifi_if));
652
653 memcpy(&ccmp->key_hdr, key_hdr,
654 sizeof(struct iwm_umac_key_hdr));
655
656 memcpy(ccmp->key, key_data, key_len);
657
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200658 if (key->seq_len)
659 memcpy(ccmp->iv_count, key->seq, key->seq_len);
Zhu Yibb9f8692009-05-21 21:20:45 +0800660
661 cmd_size = sizeof(struct iwm_umac_key_ccmp);
662 break;
663
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200664 case WLAN_CIPHER_SUITE_TKIP:
Zhu Yibb9f8692009-05-21 21:20:45 +0800665 key_hdr->key_idx++;
666 tkip->hdr.oid = UMAC_WIFI_IF_CMD_ADD_TKIP_KEY;
667 tkip->hdr.buf_size =
668 cpu_to_le16(sizeof(struct iwm_umac_key_tkip) -
669 sizeof(struct iwm_umac_wifi_if));
670
671 memcpy(&tkip->key_hdr, key_hdr,
672 sizeof(struct iwm_umac_key_hdr));
673
674 memcpy(tkip->tkip_key, key_data, IWM_TKIP_KEY_SIZE);
675 memcpy(tkip->mic_tx_key, key_data + IWM_TKIP_KEY_SIZE,
676 IWM_TKIP_MIC_SIZE);
677 memcpy(tkip->mic_rx_key,
678 key_data + IWM_TKIP_KEY_SIZE + IWM_TKIP_MIC_SIZE,
679 IWM_TKIP_MIC_SIZE);
680
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200681 if (key->seq_len)
682 memcpy(ccmp->iv_count, key->seq, key->seq_len);
Zhu Yibb9f8692009-05-21 21:20:45 +0800683
684 cmd_size = sizeof(struct iwm_umac_key_tkip);
685 break;
686
687 default:
688 return -ENOTSUPP;
689 }
690
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200691 if ((key->cipher == WLAN_CIPHER_SUITE_TKIP) ||
692 (key->cipher == WLAN_CIPHER_SUITE_CCMP))
Zhu Yibb9f8692009-05-21 21:20:45 +0800693 /*
694 * UGLY_UGLY_UGLY
695 * Copied HACK from the MWG driver.
696 * Without it, the key is set before the second
697 * EAPOL frame is sent, and the latter is thus
698 * encrypted.
699 */
700 schedule_timeout_interruptible(usecs_to_jiffies(300));
701
702 ret = iwm_send_wifi_if_cmd(iwm, cmd, cmd_size, 1);
Zhu Yibb9f8692009-05-21 21:20:45 +0800703 } else {
704 struct iwm_umac_key_remove key_remove;
705
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200706 IWM_DBG_WEXT(iwm, ERR, "Removing key_idx:%d\n", key_idx);
707
Zhu Yibb9f8692009-05-21 21:20:45 +0800708 key_remove.hdr.oid = UMAC_WIFI_IF_CMD_REMOVE_KEY;
709 key_remove.hdr.buf_size =
710 cpu_to_le16(sizeof(struct iwm_umac_key_remove) -
711 sizeof(struct iwm_umac_wifi_if));
712 memcpy(&key_remove.key_hdr, key_hdr,
713 sizeof(struct iwm_umac_key_hdr));
714
715 ret = iwm_send_wifi_if_cmd(iwm, &key_remove,
716 sizeof(struct iwm_umac_key_remove),
717 1);
Zhu Yib6c32172009-07-16 17:34:12 +0800718 if (ret)
Zhu Yibb9f8692009-05-21 21:20:45 +0800719 return ret;
720
Samuel Ortiz13e0fe702009-06-15 21:59:52 +0200721 iwm->keys[key_idx].key_len = 0;
Zhu Yibb9f8692009-05-21 21:20:45 +0800722 }
723
Zhu Yibb9f8692009-05-21 21:20:45 +0800724 return ret;
725}
726
727
728int iwm_send_mlme_profile(struct iwm_priv *iwm)
729{
Zhu Yi6e5db0a2009-07-16 17:34:13 +0800730 int ret;
Zhu Yibb9f8692009-05-21 21:20:45 +0800731 struct iwm_umac_profile profile;
732
733 memcpy(&profile, iwm->umac_profile, sizeof(profile));
734
735 profile.hdr.oid = UMAC_WIFI_IF_CMD_SET_PROFILE;
736 profile.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_profile) -
737 sizeof(struct iwm_umac_wifi_if));
738
739 ret = iwm_send_wifi_if_cmd(iwm, &profile, sizeof(profile), 1);
Zhu Yib6c32172009-07-16 17:34:12 +0800740 if (ret) {
Zhu Yibb9f8692009-05-21 21:20:45 +0800741 IWM_ERR(iwm, "Send profile command failed\n");
742 return ret;
743 }
744
Zhu Yibb9f8692009-05-21 21:20:45 +0800745 return 0;
746}
747
748int iwm_invalidate_mlme_profile(struct iwm_priv *iwm)
749{
Zhu Yibb9f8692009-05-21 21:20:45 +0800750 struct iwm_umac_invalidate_profile invalid;
Samuel Ortiza70742f2009-06-15 21:59:51 +0200751 int ret;
Zhu Yibb9f8692009-05-21 21:20:45 +0800752
753 invalid.hdr.oid = UMAC_WIFI_IF_CMD_INVALIDATE_PROFILE;
754 invalid.hdr.buf_size =
755 cpu_to_le16(sizeof(struct iwm_umac_invalidate_profile) -
756 sizeof(struct iwm_umac_wifi_if));
757
758 invalid.reason = WLAN_REASON_UNSPECIFIED;
759
760 ret = iwm_send_wifi_if_cmd(iwm, &invalid, sizeof(invalid), 1);
Zhu Yib6c32172009-07-16 17:34:12 +0800761 if (ret)
Zhu Yibb9f8692009-05-21 21:20:45 +0800762 return ret;
763
764 ret = wait_event_interruptible_timeout(iwm->mlme_queue,
Samuel Ortiza70742f2009-06-15 21:59:51 +0200765 (iwm->umac_profile_active == 0), 2 * HZ);
Zhu Yibb9f8692009-05-21 21:20:45 +0800766
Zhu Yib6c32172009-07-16 17:34:12 +0800767 return ret ? 0 : -EBUSY;
Zhu Yibb9f8692009-05-21 21:20:45 +0800768}
769
770int iwm_send_umac_stats_req(struct iwm_priv *iwm, u32 flags)
771{
772 struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
773 struct iwm_umac_cmd umac_cmd;
774 struct iwm_umac_cmd_stats_req stats_req;
775
776 stats_req.flags = cpu_to_le32(flags);
777
778 umac_cmd.id = UMAC_CMD_OPCODE_STATISTIC_REQUEST;
779 umac_cmd.resp = 0;
780
781 return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &stats_req,
782 sizeof(struct iwm_umac_cmd_stats_req));
783}
784
785int iwm_send_umac_channel_list(struct iwm_priv *iwm)
786{
787 struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
788 struct iwm_umac_cmd umac_cmd;
789 struct iwm_umac_cmd_get_channel_list *ch_list;
790 int size = sizeof(struct iwm_umac_cmd_get_channel_list) +
791 sizeof(struct iwm_umac_channel_info) * 4;
792 int ret;
793
794 ch_list = kzalloc(size, GFP_KERNEL);
795 if (!ch_list) {
796 IWM_ERR(iwm, "Couldn't allocate channel list cmd\n");
797 return -ENOMEM;
798 }
799
800 ch_list->ch[0].band = UMAC_BAND_2GHZ;
801 ch_list->ch[0].type = UMAC_CHANNEL_WIDTH_20MHZ;
802 ch_list->ch[0].flags = UMAC_CHANNEL_FLAG_VALID;
803
804 ch_list->ch[1].band = UMAC_BAND_5GHZ;
805 ch_list->ch[1].type = UMAC_CHANNEL_WIDTH_20MHZ;
806 ch_list->ch[1].flags = UMAC_CHANNEL_FLAG_VALID;
807
808 ch_list->ch[2].band = UMAC_BAND_2GHZ;
809 ch_list->ch[2].type = UMAC_CHANNEL_WIDTH_20MHZ;
810 ch_list->ch[2].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
811
812 ch_list->ch[3].band = UMAC_BAND_5GHZ;
813 ch_list->ch[3].type = UMAC_CHANNEL_WIDTH_20MHZ;
814 ch_list->ch[3].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
815
816 ch_list->count = cpu_to_le16(4);
817
818 umac_cmd.id = UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST;
819 umac_cmd.resp = 1;
820
821 ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, ch_list, size);
822
823 kfree(ch_list);
824
825 return ret;
826}
827
828int iwm_scan_ssids(struct iwm_priv *iwm, struct cfg80211_ssid *ssids,
829 int ssid_num)
830{
831 struct iwm_umac_cmd_scan_request req;
832 int i, ret;
833
834 memset(&req, 0, sizeof(struct iwm_umac_cmd_scan_request));
835
836 req.hdr.oid = UMAC_WIFI_IF_CMD_SCAN_REQUEST;
837 req.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_cmd_scan_request)
838 - sizeof(struct iwm_umac_wifi_if));
839 req.type = UMAC_WIFI_IF_SCAN_TYPE_USER;
840 req.timeout = 2;
841 req.seq_num = iwm->scan_id;
842 req.ssid_num = min(ssid_num, UMAC_WIFI_IF_PROBE_OPTION_MAX);
843
844 for (i = 0; i < req.ssid_num; i++) {
845 memcpy(req.ssids[i].ssid, ssids[i].ssid, ssids[i].ssid_len);
846 req.ssids[i].ssid_len = ssids[i].ssid_len;
847 }
848
849 ret = iwm_send_wifi_if_cmd(iwm, &req, sizeof(req), 0);
Zhu Yib6c32172009-07-16 17:34:12 +0800850 if (ret) {
Zhu Yibb9f8692009-05-21 21:20:45 +0800851 IWM_ERR(iwm, "Couldn't send scan request\n");
852 return ret;
853 }
854
855 iwm->scan_id = iwm->scan_id++ % IWM_SCAN_ID_MAX;
856
857 return 0;
858}
859
860int iwm_scan_one_ssid(struct iwm_priv *iwm, u8 *ssid, int ssid_len)
861{
862 struct cfg80211_ssid one_ssid;
863
864 if (test_and_set_bit(IWM_STATUS_SCANNING, &iwm->status))
865 return 0;
866
867 one_ssid.ssid_len = min(ssid_len, IEEE80211_MAX_SSID_LEN);
868 memcpy(&one_ssid.ssid, ssid, one_ssid.ssid_len);
869
870 return iwm_scan_ssids(iwm, &one_ssid, 1);
871}
872
873int iwm_target_reset(struct iwm_priv *iwm)
874{
875 struct iwm_udma_nonwifi_cmd target_cmd;
876
877 target_cmd.opcode = UMAC_HDI_OUT_OPCODE_REBOOT;
878 target_cmd.addr = 0;
879 target_cmd.op1_sz = 0;
880 target_cmd.op2 = 0;
881 target_cmd.handle_by_hw = 0;
882 target_cmd.resp = 0;
883 target_cmd.eop = 1;
884
885 return iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
886}