blob: d12e25d0c88060c5d8968ee504894b57f64f4f77 [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: commands and events
3 *
4 * Copyright (C) 2011, Marvell International Ltd.
5 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
18 */
19
20#include "decl.h"
21#include "ioctl.h"
22#include "util.h"
23#include "fw.h"
24#include "main.h"
25#include "wmm.h"
26#include "11n.h"
27
28/*
29 * This function initializes a command node.
30 *
31 * The actual allocation of the node is not done by this function. It only
32 * initiates a node by filling it with default parameters. Similarly,
33 * allocation of the different buffers used (IOCTL buffer, data buffer) are
34 * not done by this function either.
35 */
36static void
37mwifiex_init_cmd_node(struct mwifiex_private *priv,
38 struct cmd_ctrl_node *cmd_node,
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070039 u32 cmd_oid, void *data_buf)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070040{
41 cmd_node->priv = priv;
42 cmd_node->cmd_oid = cmd_oid;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070043 cmd_node->wait_q_enabled = priv->adapter->cmd_wait_q_required;
44 priv->adapter->cmd_wait_q_required = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070045 cmd_node->data_buf = data_buf;
46 cmd_node->cmd_skb = cmd_node->skb;
47}
48
49/*
50 * This function returns a command node from the free queue depending upon
51 * availability.
52 */
53static struct cmd_ctrl_node *
54mwifiex_get_cmd_node(struct mwifiex_adapter *adapter)
55{
56 struct cmd_ctrl_node *cmd_node;
57 unsigned long flags;
58
59 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
60 if (list_empty(&adapter->cmd_free_q)) {
61 dev_err(adapter->dev, "GET_CMD_NODE: cmd node not available\n");
62 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
63 return NULL;
64 }
65 cmd_node = list_first_entry(&adapter->cmd_free_q,
66 struct cmd_ctrl_node, list);
67 list_del(&cmd_node->list);
68 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
69
70 return cmd_node;
71}
72
73/*
74 * This function cleans up a command node.
75 *
76 * The function resets the fields including the buffer pointers.
77 * This function does not try to free the buffers. They must be
78 * freed before calling this function.
79 *
80 * This function will however call the receive completion callback
81 * in case a response buffer is still available before resetting
82 * the pointer.
83 */
84static void
85mwifiex_clean_cmd_node(struct mwifiex_adapter *adapter,
86 struct cmd_ctrl_node *cmd_node)
87{
88 cmd_node->cmd_oid = 0;
89 cmd_node->cmd_flag = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070090 cmd_node->data_buf = NULL;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -070091 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070092
Amitkumar Karwar5cf80992011-09-21 21:43:25 -070093 if (cmd_node->cmd_skb)
94 skb_trim(cmd_node->cmd_skb, 0);
95
Bing Zhao5e6e3a92011-03-21 18:00:50 -070096 if (cmd_node->resp_skb) {
Amitkumar Karwar406a39e2011-05-11 19:47:11 -070097 dev_kfree_skb_any(cmd_node->resp_skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070098 cmd_node->resp_skb = NULL;
99 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700100}
101
102/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700103 * This function sends a host command to the firmware.
104 *
105 * The function copies the host command into the driver command
106 * buffer, which will be transferred to the firmware later by the
107 * main thread.
108 */
109static int mwifiex_cmd_host_cmd(struct mwifiex_private *priv,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700110 struct host_cmd_ds_command *cmd,
111 struct mwifiex_ds_misc_cmd *pcmd_ptr)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700112{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700113 /* Copy the HOST command to command buffer */
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700114 memcpy(cmd, pcmd_ptr->cmd, pcmd_ptr->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700115 dev_dbg(priv->adapter->dev, "cmd: host cmd size = %d\n", pcmd_ptr->len);
116 return 0;
117}
118
119/*
120 * This function downloads a command to the firmware.
121 *
122 * The function performs sanity tests, sets the command sequence
123 * number and size, converts the header fields to CPU format before
124 * sending. Afterwards, it logs the command ID and action for debugging
125 * and sets up the command timeout timer.
126 */
127static int mwifiex_dnld_cmd_to_fw(struct mwifiex_private *priv,
128 struct cmd_ctrl_node *cmd_node)
129{
130
131 struct mwifiex_adapter *adapter = priv->adapter;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700132 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700133 struct host_cmd_ds_command *host_cmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700134 uint16_t cmd_code;
135 uint16_t cmd_size;
136 struct timeval tstamp;
137 unsigned long flags;
138
139 if (!adapter || !cmd_node)
140 return -1;
141
142 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700143
144 /* Sanity test */
145 if (host_cmd == NULL || host_cmd->size == 0) {
146 dev_err(adapter->dev, "DNLD_CMD: host_cmd is null"
147 " or cmd size is 0, not sending\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700148 if (cmd_node->wait_q_enabled)
149 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700150 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
151 return -1;
152 }
153
154 /* Set command sequence number */
155 adapter->seq_num++;
156 host_cmd->seq_num = cpu_to_le16(HostCmd_SET_SEQ_NO_BSS_INFO
157 (adapter->seq_num, cmd_node->priv->bss_num,
158 cmd_node->priv->bss_type));
159
160 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
161 adapter->curr_cmd = cmd_node;
162 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
163
164 cmd_code = le16_to_cpu(host_cmd->command);
165 cmd_size = le16_to_cpu(host_cmd->size);
166
167 skb_trim(cmd_node->cmd_skb, cmd_size);
168
169 do_gettimeofday(&tstamp);
170 dev_dbg(adapter->dev, "cmd: DNLD_CMD: (%lu.%lu): %#x, act %#x, len %d,"
171 " seqno %#x\n",
172 tstamp.tv_sec, tstamp.tv_usec, cmd_code,
173 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN)), cmd_size,
174 le16_to_cpu(host_cmd->seq_num));
175
176 skb_push(cmd_node->cmd_skb, INTF_HEADER_LEN);
177
178 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
179 cmd_node->cmd_skb->data,
180 cmd_node->cmd_skb->len, NULL);
181
Bing Zhao18bf9652011-04-06 16:46:55 -0700182 skb_pull(cmd_node->cmd_skb, INTF_HEADER_LEN);
183
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700184 if (ret == -1) {
185 dev_err(adapter->dev, "DNLD_CMD: host to card failed\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700186 if (cmd_node->wait_q_enabled)
187 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700188 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
189
190 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
191 adapter->curr_cmd = NULL;
192 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
193
194 adapter->dbg.num_cmd_host_to_card_failure++;
195 return -1;
196 }
197
198 /* Save the last command id and action to debug log */
199 adapter->dbg.last_cmd_index =
200 (adapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
201 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index] = cmd_code;
202 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index] =
203 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN));
204
205 /* Clear BSS_NO_BITS from HostCmd */
206 cmd_code &= HostCmd_CMD_ID_MASK;
207
208 /* Setup the timer after transmit command */
209 mod_timer(&adapter->cmd_timer,
210 jiffies + (MWIFIEX_TIMER_10S * HZ) / 1000);
211
212 return 0;
213}
214
215/*
216 * This function downloads a sleep confirm command to the firmware.
217 *
218 * The function performs sanity tests, sets the command sequence
219 * number and size, converts the header fields to CPU format before
220 * sending.
221 *
222 * No responses are needed for sleep confirm command.
223 */
224static int mwifiex_dnld_sleep_confirm_cmd(struct mwifiex_adapter *adapter)
225{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700226 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700227 struct mwifiex_private *priv;
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700228 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf =
229 (struct mwifiex_opt_sleep_confirm *)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700230 adapter->sleep_cfm->data;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700231 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
232
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700233 sleep_cfm_buf->seq_num =
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700234 cpu_to_le16((HostCmd_SET_SEQ_NO_BSS_INFO
235 (adapter->seq_num, priv->bss_num,
236 priv->bss_type)));
237 adapter->seq_num++;
238
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700239 skb_push(adapter->sleep_cfm, INTF_HEADER_LEN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700240 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
241 adapter->sleep_cfm->data,
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700242 adapter->sleep_cfm->len, NULL);
243 skb_pull(adapter->sleep_cfm, INTF_HEADER_LEN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700244
245 if (ret == -1) {
246 dev_err(adapter->dev, "SLEEP_CFM: failed\n");
247 adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure++;
248 return -1;
249 }
250 if (GET_BSS_ROLE(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY))
251 == MWIFIEX_BSS_ROLE_STA) {
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700252 if (!sleep_cfm_buf->resp_ctrl)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700253 /* Response is not needed for sleep
254 confirm command */
255 adapter->ps_state = PS_STATE_SLEEP;
256 else
257 adapter->ps_state = PS_STATE_SLEEP_CFM;
258
Amitkumar Karwar7cc5eb62011-05-09 19:00:18 -0700259 if (!sleep_cfm_buf->resp_ctrl
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700260 && (adapter->is_hs_configured
261 && !adapter->sleep_period.period)) {
262 adapter->pm_wakeup_card_req = true;
263 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
264 MWIFIEX_BSS_ROLE_STA), true);
265 }
266 }
267
268 return ret;
269}
270
271/*
272 * This function allocates the command buffers and links them to
273 * the command free queue.
274 *
275 * The driver uses a pre allocated number of command buffers, which
276 * are created at driver initializations and freed at driver cleanup.
277 * Every command needs to obtain a command buffer from this pool before
278 * it can be issued. The command free queue lists the command buffers
279 * currently free to use, while the command pending queue lists the
280 * command buffers already in use and awaiting handling. Command buffers
281 * are returned to the free queue after use.
282 */
283int mwifiex_alloc_cmd_buffer(struct mwifiex_adapter *adapter)
284{
285 struct cmd_ctrl_node *cmd_array;
286 u32 buf_size;
287 u32 i;
288
289 /* Allocate and initialize struct cmd_ctrl_node */
290 buf_size = sizeof(struct cmd_ctrl_node) * MWIFIEX_NUM_OF_CMD_BUFFER;
291 cmd_array = kzalloc(buf_size, GFP_KERNEL);
292 if (!cmd_array) {
293 dev_err(adapter->dev, "%s: failed to alloc cmd_array\n",
294 __func__);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200295 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700296 }
297
298 adapter->cmd_pool = cmd_array;
299 memset(adapter->cmd_pool, 0, buf_size);
300
301 /* Allocate and initialize command buffers */
302 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
303 cmd_array[i].skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
304 if (!cmd_array[i].skb) {
305 dev_err(adapter->dev, "ALLOC_CMD_BUF: out of memory\n");
306 return -1;
307 }
308 }
309
310 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++)
311 mwifiex_insert_cmd_to_free_q(adapter, &cmd_array[i]);
312
313 return 0;
314}
315
316/*
317 * This function frees the command buffers.
318 *
319 * The function calls the completion callback for all the command
320 * buffers that still have response buffers associated with them.
321 */
322int mwifiex_free_cmd_buffer(struct mwifiex_adapter *adapter)
323{
324 struct cmd_ctrl_node *cmd_array;
325 u32 i;
326
327 /* Need to check if cmd pool is allocated or not */
328 if (!adapter->cmd_pool) {
329 dev_dbg(adapter->dev, "info: FREE_CMD_BUF: cmd_pool is null\n");
330 return 0;
331 }
332
333 cmd_array = adapter->cmd_pool;
334
335 /* Release shared memory buffers */
336 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
337 if (cmd_array[i].skb) {
338 dev_dbg(adapter->dev, "cmd: free cmd buffer %d\n", i);
339 dev_kfree_skb_any(cmd_array[i].skb);
340 }
341 if (!cmd_array[i].resp_skb)
342 continue;
Amitkumar Karwar406a39e2011-05-11 19:47:11 -0700343 dev_kfree_skb_any(cmd_array[i].resp_skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700344 }
345 /* Release struct cmd_ctrl_node */
346 if (adapter->cmd_pool) {
347 dev_dbg(adapter->dev, "cmd: free cmd pool\n");
348 kfree(adapter->cmd_pool);
349 adapter->cmd_pool = NULL;
350 }
351
352 return 0;
353}
354
355/*
356 * This function handles events generated by firmware.
357 *
358 * Event body of events received from firmware are not used (though they are
359 * saved), only the event ID is used. Some events are re-invoked by
360 * the driver, with a new event body.
361 *
362 * After processing, the function calls the completion callback
363 * for cleanup.
364 */
365int mwifiex_process_event(struct mwifiex_adapter *adapter)
366{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700367 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700368 struct mwifiex_private *priv =
369 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
370 struct sk_buff *skb = adapter->event_skb;
371 u32 eventcause = adapter->event_cause;
372 struct timeval tstamp;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700373 struct mwifiex_rxinfo *rx_info;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700374
375 /* Save the last event to debug log */
376 adapter->dbg.last_event_index =
377 (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
378 adapter->dbg.last_event[adapter->dbg.last_event_index] =
379 (u16) eventcause;
380
381 /* Get BSS number and corresponding priv */
382 priv = mwifiex_get_priv_by_id(adapter, EVENT_GET_BSS_NUM(eventcause),
383 EVENT_GET_BSS_TYPE(eventcause));
384 if (!priv)
385 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
386 /* Clear BSS_NO_BITS from event */
387 eventcause &= EVENT_ID_MASK;
388 adapter->event_cause = eventcause;
389
390 if (skb) {
391 rx_info = MWIFIEX_SKB_RXCB(skb);
392 rx_info->bss_index = priv->bss_index;
393 }
394
395 if (eventcause != EVENT_PS_SLEEP && eventcause != EVENT_PS_AWAKE) {
396 do_gettimeofday(&tstamp);
397 dev_dbg(adapter->dev, "event: %lu.%lu: cause: %#x\n",
398 tstamp.tv_sec, tstamp.tv_usec, eventcause);
399 }
400
401 ret = mwifiex_process_sta_event(priv);
402
403 adapter->event_cause = 0;
404 adapter->event_skb = NULL;
405
Amitkumar Karwar406a39e2011-05-11 19:47:11 -0700406 dev_kfree_skb_any(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700407
408 return ret;
409}
410
411/*
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700412 * This function is used to send synchronous command to the firmware.
413 *
414 * it allocates a wait queue for the command and wait for the command
415 * response.
416 */
417int mwifiex_send_cmd_sync(struct mwifiex_private *priv, uint16_t cmd_no,
418 u16 cmd_action, u32 cmd_oid, void *data_buf)
419{
420 int ret = 0;
421 struct mwifiex_adapter *adapter = priv->adapter;
422
423 adapter->cmd_wait_q_required = true;
424 adapter->cmd_wait_q.condition = false;
425
426 ret = mwifiex_send_cmd_async(priv, cmd_no, cmd_action, cmd_oid,
427 data_buf);
428 if (!ret)
429 ret = mwifiex_wait_queue_complete(adapter);
430
431 return ret;
432}
433
434
435/*
436 * This function prepares a command and asynchronously send it to the firmware.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700437 *
438 * Preparation includes -
439 * - Sanity tests to make sure the card is still present or the FW
440 * is not reset
441 * - Getting a new command node from the command free queue
442 * - Initializing the command node for default parameters
443 * - Fill up the non-default parameters and buffer pointers
444 * - Add the command to pending queue
445 */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700446int mwifiex_send_cmd_async(struct mwifiex_private *priv, uint16_t cmd_no,
447 u16 cmd_action, u32 cmd_oid, void *data_buf)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700448{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700449 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700450 struct mwifiex_adapter *adapter = priv->adapter;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700451 struct cmd_ctrl_node *cmd_node;
452 struct host_cmd_ds_command *cmd_ptr;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700453
454 if (!adapter) {
455 pr_err("PREP_CMD: adapter is NULL\n");
456 return -1;
457 }
458
459 if (adapter->is_suspended) {
460 dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
461 return -1;
462 }
463
464 if (adapter->surprise_removed) {
465 dev_err(adapter->dev, "PREP_CMD: card is removed\n");
466 return -1;
467 }
468
469 if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
470 if (cmd_no != HostCmd_CMD_FUNC_INIT) {
471 dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
472 return -1;
473 }
474 }
475
476 /* Get a new command node */
477 cmd_node = mwifiex_get_cmd_node(adapter);
478
479 if (!cmd_node) {
480 dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
481 return -1;
482 }
483
484 /* Initialize the command node */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700485 mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700486
487 if (!cmd_node->cmd_skb) {
488 dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
489 return -1;
490 }
491
492 memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
493 0, sizeof(struct host_cmd_ds_command));
494
495 cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
496 cmd_ptr->command = cpu_to_le16(cmd_no);
497 cmd_ptr->result = 0;
498
499 /* Prepare command */
500 if (cmd_no) {
501 ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
502 cmd_oid, data_buf, cmd_ptr);
503 } else {
504 ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
505 cmd_node->cmd_flag |= CMD_F_HOSTCMD;
506 }
507
508 /* Return error, since the command preparation failed */
509 if (ret) {
510 dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
511 cmd_no);
512 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
513 return -1;
514 }
515
516 /* Send command */
517 if (cmd_no == HostCmd_CMD_802_11_SCAN)
518 mwifiex_queue_scan_cmd(priv, cmd_node);
519 else
520 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
521
522 return ret;
523}
524
525/*
526 * This function returns a command to the command free queue.
527 *
528 * The function also calls the completion callback if required, before
529 * cleaning the command node and re-inserting it into the free queue.
530 */
531void
532mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
533 struct cmd_ctrl_node *cmd_node)
534{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700535 unsigned long flags;
536
Yogesh Ashok Powar19a89862011-04-13 17:27:07 -0700537 if (!cmd_node)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700538 return;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700539
540 if (cmd_node->wait_q_enabled)
541 mwifiex_complete_cmd(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700542 /* Clean the node */
543 mwifiex_clean_cmd_node(adapter, cmd_node);
544
545 /* Insert node into cmd_free_q */
546 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
547 list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
548 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700549}
550
551/*
552 * This function queues a command to the command pending queue.
553 *
554 * This in effect adds the command to the command list to be executed.
555 * Exit PS command is handled specially, by placing it always to the
556 * front of the command queue.
557 */
558void
559mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
560 struct cmd_ctrl_node *cmd_node, u32 add_tail)
561{
562 struct host_cmd_ds_command *host_cmd = NULL;
563 u16 command;
564 unsigned long flags;
565
566 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
567 if (!host_cmd) {
568 dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
569 return;
570 }
571
572 command = le16_to_cpu(host_cmd->command);
573
574 /* Exit_PS command needs to be queued in the header always. */
575 if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
576 struct host_cmd_ds_802_11_ps_mode_enh *pm =
577 &host_cmd->params.psmode_enh;
578 if ((le16_to_cpu(pm->action) == DIS_PS)
579 || (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
580 if (adapter->ps_state != PS_STATE_AWAKE)
581 add_tail = false;
582 }
583 }
584
585 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
586 if (add_tail)
587 list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
588 else
589 list_add(&cmd_node->list, &adapter->cmd_pending_q);
590 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
591
592 dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x is queued\n", command);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700593}
594
595/*
596 * This function executes the next command in command pending queue.
597 *
598 * This function will fail if a command is already in processing stage,
599 * otherwise it will dequeue the first command from the command pending
600 * queue and send to the firmware.
601 *
602 * If the device is currently in host sleep mode, any commands, except the
603 * host sleep configuration command will de-activate the host sleep. For PS
604 * mode, the function will put the firmware back to sleep if applicable.
605 */
606int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
607{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700608 struct mwifiex_private *priv;
609 struct cmd_ctrl_node *cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700610 int ret = 0;
611 struct host_cmd_ds_command *host_cmd;
612 unsigned long cmd_flags;
613 unsigned long cmd_pending_q_flags;
614
615 /* Check if already in processing */
616 if (adapter->curr_cmd) {
617 dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
618 return -1;
619 }
620
621 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
622 /* Check if any command is pending */
623 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
624 if (list_empty(&adapter->cmd_pending_q)) {
625 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
626 cmd_pending_q_flags);
627 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
628 return 0;
629 }
630 cmd_node = list_first_entry(&adapter->cmd_pending_q,
631 struct cmd_ctrl_node, list);
632 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
633 cmd_pending_q_flags);
634
635 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
636 priv = cmd_node->priv;
637
638 if (adapter->ps_state != PS_STATE_AWAKE) {
639 dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
640 " this should not happen\n", __func__);
641 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
642 return ret;
643 }
644
645 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
646 list_del(&cmd_node->list);
647 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
648 cmd_pending_q_flags);
649
650 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
651 ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
652 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
653 /* Any command sent to the firmware when host is in sleep
654 * mode should de-configure host sleep. We should skip the
655 * host sleep configuration command itself though
656 */
657 if (priv && (host_cmd->command !=
658 cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
659 if (adapter->hs_activated) {
660 adapter->is_hs_configured = false;
661 mwifiex_hs_activated_event(priv, false);
662 }
663 }
664
665 return ret;
666}
667
668/*
669 * This function handles the command response.
670 *
671 * After processing, the function cleans the command node and puts
672 * it back to the command free queue.
673 */
674int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
675{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700676 struct host_cmd_ds_command *resp;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700677 struct mwifiex_private *priv =
678 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
679 int ret = 0;
680 uint16_t orig_cmdresp_no;
681 uint16_t cmdresp_no;
682 uint16_t cmdresp_result;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700683 struct timeval tstamp;
684 unsigned long flags;
685
686 /* Now we got response from FW, cancel the command timer */
687 del_timer(&adapter->cmd_timer);
688
689 if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
690 resp = (struct host_cmd_ds_command *) adapter->upld_buf;
691 dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
692 le16_to_cpu(resp->command));
693 return -1;
694 }
695
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700696 adapter->num_cmd_timeout = 0;
697
698 resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
699 if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
700 dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
701 le16_to_cpu(resp->command));
702 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
703 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
704 adapter->curr_cmd = NULL;
705 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
706 return -1;
707 }
708
709 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
710 /* Copy original response back to response buffer */
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700711 struct mwifiex_ds_misc_cmd *hostcmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700712 uint16_t size = le16_to_cpu(resp->size);
713 dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
714 size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
715 if (adapter->curr_cmd->data_buf) {
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700716 hostcmd = adapter->curr_cmd->data_buf;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700717 hostcmd->len = size;
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700718 memcpy(hostcmd->cmd, resp, size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700719 }
720 }
721 orig_cmdresp_no = le16_to_cpu(resp->command);
722
723 /* Get BSS number and corresponding priv */
724 priv = mwifiex_get_priv_by_id(adapter,
725 HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
726 HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
727 if (!priv)
728 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
729 /* Clear RET_BIT from HostCmd */
730 resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
731
732 cmdresp_no = le16_to_cpu(resp->command);
733 cmdresp_result = le16_to_cpu(resp->result);
734
735 /* Save the last command response to debug log */
736 adapter->dbg.last_cmd_resp_index =
737 (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
738 adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
739 orig_cmdresp_no;
740
741 do_gettimeofday(&tstamp);
742 dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
743 " len %d, seqno 0x%x\n",
744 tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
745 le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
746
747 if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
748 dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700749 if (adapter->curr_cmd->wait_q_enabled)
750 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700751
752 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
753 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
754 adapter->curr_cmd = NULL;
755 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
756 return -1;
757 }
758
759 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
760 adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
761 if ((cmdresp_result == HostCmd_RESULT_OK)
762 && (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
763 ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
764 } else {
765 /* handle response */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700766 ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700767 }
768
769 /* Check init command response */
770 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
771 if (ret == -1) {
772 dev_err(adapter->dev, "%s: cmd %#x failed during "
773 "initialization\n", __func__, cmdresp_no);
774 mwifiex_init_fw_complete(adapter);
775 return -1;
776 } else if (adapter->last_init_cmd == cmdresp_no)
777 adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
778 }
779
780 if (adapter->curr_cmd) {
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700781 if (adapter->curr_cmd->wait_q_enabled && (!ret))
782 adapter->cmd_wait_q.status = 0;
783 else if (adapter->curr_cmd->wait_q_enabled && (ret == -1))
784 adapter->cmd_wait_q.status = -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700785
786 /* Clean up and put current command back to cmd_free_q */
787 mwifiex_insert_cmd_to_free_q(adapter, adapter->curr_cmd);
788
789 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
790 adapter->curr_cmd = NULL;
791 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
792 }
793
794 return ret;
795}
796
797/*
798 * This function handles the timeout of command sending.
799 *
800 * It will re-send the same command again.
801 */
802void
803mwifiex_cmd_timeout_func(unsigned long function_context)
804{
805 struct mwifiex_adapter *adapter =
806 (struct mwifiex_adapter *) function_context;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700807 struct cmd_ctrl_node *cmd_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700808 struct timeval tstamp;
809
810 adapter->num_cmd_timeout++;
811 adapter->dbg.num_cmd_timeout++;
812 if (!adapter->curr_cmd) {
813 dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
814 return;
815 }
816 cmd_node = adapter->curr_cmd;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700817 if (cmd_node->wait_q_enabled)
818 adapter->cmd_wait_q.status = -ETIMEDOUT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700819
820 if (cmd_node) {
821 adapter->dbg.timeout_cmd_id =
822 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
823 adapter->dbg.timeout_cmd_act =
824 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
825 do_gettimeofday(&tstamp);
826 dev_err(adapter->dev, "%s: Timeout cmd id (%lu.%lu) = %#x,"
827 " act = %#x\n", __func__,
828 tstamp.tv_sec, tstamp.tv_usec,
829 adapter->dbg.timeout_cmd_id,
830 adapter->dbg.timeout_cmd_act);
831
832 dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
833 adapter->dbg.num_tx_host_to_card_failure);
834 dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
835 adapter->dbg.num_cmd_host_to_card_failure);
836
837 dev_err(adapter->dev, "num_cmd_timeout = %d\n",
838 adapter->dbg.num_cmd_timeout);
839 dev_err(adapter->dev, "num_tx_timeout = %d\n",
840 adapter->dbg.num_tx_timeout);
841
842 dev_err(adapter->dev, "last_cmd_index = %d\n",
843 adapter->dbg.last_cmd_index);
844 print_hex_dump_bytes("last_cmd_id: ", DUMP_PREFIX_OFFSET,
845 adapter->dbg.last_cmd_id, DBG_CMD_NUM);
846 print_hex_dump_bytes("last_cmd_act: ", DUMP_PREFIX_OFFSET,
847 adapter->dbg.last_cmd_act, DBG_CMD_NUM);
848
849 dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
850 adapter->dbg.last_cmd_resp_index);
851 print_hex_dump_bytes("last_cmd_resp_id: ", DUMP_PREFIX_OFFSET,
852 adapter->dbg.last_cmd_resp_id, DBG_CMD_NUM);
853
854 dev_err(adapter->dev, "last_event_index = %d\n",
855 adapter->dbg.last_event_index);
856 print_hex_dump_bytes("last_event: ", DUMP_PREFIX_OFFSET,
857 adapter->dbg.last_event, DBG_CMD_NUM);
858
859 dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
860 adapter->data_sent, adapter->cmd_sent);
861
862 dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
863 adapter->ps_mode, adapter->ps_state);
864 }
865 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
866 mwifiex_init_fw_complete(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700867}
868
869/*
870 * This function cancels all the pending commands.
871 *
872 * The current command, all commands in command pending queue and all scan
873 * commands in scan pending queue are cancelled. All the completion callbacks
874 * are called with failure status to ensure cleanup.
875 */
876void
877mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
878{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700879 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700880 unsigned long flags;
881
882 /* Cancel current cmd */
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700883 if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700884 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700885 adapter->curr_cmd->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700886 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700887 adapter->cmd_wait_q.status = -1;
888 mwifiex_complete_cmd(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700889 }
890 /* Cancel all pending command */
891 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
892 list_for_each_entry_safe(cmd_node, tmp_node,
893 &adapter->cmd_pending_q, list) {
894 list_del(&cmd_node->list);
895 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
896
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700897 if (cmd_node->wait_q_enabled) {
898 adapter->cmd_wait_q.status = -1;
899 mwifiex_complete_cmd(adapter);
900 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700901 }
902 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
903 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
904 }
905 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
906
907 /* Cancel all pending scan command */
908 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
909 list_for_each_entry_safe(cmd_node, tmp_node,
910 &adapter->scan_pending_q, list) {
911 list_del(&cmd_node->list);
912 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
913
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700914 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700915 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
916 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
917 }
918 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
919
920 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
921 adapter->scan_processing = false;
922 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
923}
924
925/*
926 * This function cancels all pending commands that matches with
927 * the given IOCTL request.
928 *
929 * Both the current command buffer and the pending command queue are
930 * searched for matching IOCTL request. The completion callback of
931 * the matched command is called with failure status to ensure cleanup.
932 * In case of scan commands, all pending commands in scan pending queue
933 * are cancelled.
934 */
935void
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700936mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700937{
938 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
939 unsigned long cmd_flags;
940 unsigned long cmd_pending_q_flags;
941 unsigned long scan_pending_q_flags;
942 uint16_t cancel_scan_cmd = false;
943
944 if ((adapter->curr_cmd) &&
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700945 (adapter->curr_cmd->wait_q_enabled)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700946 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
947 cmd_node = adapter->curr_cmd;
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700948 cmd_node->wait_q_enabled = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700949 cmd_node->cmd_flag |= CMD_F_CANCELED;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700950 spin_lock_irqsave(&adapter->cmd_pending_q_lock,
951 cmd_pending_q_flags);
952 list_del(&cmd_node->list);
953 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
954 cmd_pending_q_flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700955 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700956 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700957 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700958
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700959 /* Cancel all pending scan command */
960 spin_lock_irqsave(&adapter->scan_pending_q_lock,
961 scan_pending_q_flags);
962 list_for_each_entry_safe(cmd_node, tmp_node,
963 &adapter->scan_pending_q, list) {
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700964 list_del(&cmd_node->list);
965 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
966 scan_pending_q_flags);
967 cmd_node->wait_q_enabled = false;
968 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
969 spin_lock_irqsave(&adapter->scan_pending_q_lock,
970 scan_pending_q_flags);
971 cancel_scan_cmd = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700972 }
973 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
974 scan_pending_q_flags);
975
976 if (cancel_scan_cmd) {
977 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
978 adapter->scan_processing = false;
979 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
980 }
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700981 adapter->cmd_wait_q.status = -1;
982 mwifiex_complete_cmd(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700983}
984
985/*
986 * This function sends the sleep confirm command to firmware, if
987 * possible.
988 *
989 * The sleep confirm command cannot be issued if command response,
990 * data response or event response is awaiting handling, or if we
991 * are in the middle of sending a command, or expecting a command
992 * response.
993 */
994void
995mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
996{
997 if (!adapter->cmd_sent &&
998 !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
999 mwifiex_dnld_sleep_confirm_cmd(adapter);
1000 else
1001 dev_dbg(adapter->dev,
1002 "cmd: Delay Sleep Confirm (%s%s%s)\n",
1003 (adapter->cmd_sent) ? "D" : "",
1004 (adapter->curr_cmd) ? "C" : "",
1005 (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
1006}
1007
1008/*
1009 * This function sends a Host Sleep activated event to applications.
1010 *
1011 * This event is generated by the driver, with a blank event body.
1012 */
1013void
1014mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1015{
1016 if (activated) {
1017 if (priv->adapter->is_hs_configured) {
1018 priv->adapter->hs_activated = true;
1019 dev_dbg(priv->adapter->dev, "event: hs_activated\n");
1020 priv->adapter->hs_activate_wait_q_woken = true;
1021 wake_up_interruptible(
1022 &priv->adapter->hs_activate_wait_q);
1023 } else {
1024 dev_dbg(priv->adapter->dev, "event: HS not configured\n");
1025 }
1026 } else {
1027 dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
1028 priv->adapter->hs_activated = false;
1029 }
1030}
1031
1032/*
1033 * This function handles the command response of a Host Sleep configuration
1034 * command.
1035 *
1036 * Handling includes changing the header fields into CPU format
1037 * and setting the current host sleep activation status in driver.
1038 *
1039 * In case host sleep status change, the function generates an event to
1040 * notify the applications.
1041 */
1042int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1043 struct host_cmd_ds_command *resp)
1044{
1045 struct mwifiex_adapter *adapter = priv->adapter;
1046 struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1047 &resp->params.opt_hs_cfg;
1048 uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1049
1050 if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE)) {
1051 mwifiex_hs_activated_event(priv, true);
1052 return 0;
1053 } else {
1054 dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
1055 " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1056 resp->result, conditions,
1057 phs_cfg->params.hs_config.gpio,
1058 phs_cfg->params.hs_config.gap);
1059 }
1060 if (conditions != HOST_SLEEP_CFG_CANCEL) {
1061 adapter->is_hs_configured = true;
1062 } else {
1063 adapter->is_hs_configured = false;
1064 if (adapter->hs_activated)
1065 mwifiex_hs_activated_event(priv, false);
1066 }
1067
1068 return 0;
1069}
1070
1071/*
1072 * This function wakes up the adapter and generates a Host Sleep
1073 * cancel event on receiving the power up interrupt.
1074 */
1075void
1076mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1077{
1078 dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
1079 " since there is interrupt from the firmware\n", __func__);
1080
1081 adapter->if_ops.wakeup(adapter);
1082 adapter->hs_activated = false;
1083 adapter->is_hs_configured = false;
1084 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
1085 MWIFIEX_BSS_ROLE_ANY), false);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001086}
1087
1088/*
1089 * This function handles the command response of a sleep confirm command.
1090 *
1091 * The function sets the card state to SLEEP if the response indicates success.
1092 */
1093void
1094mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1095 u8 *pbuf, u32 upld_len)
1096{
1097 struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1098 struct mwifiex_private *priv =
1099 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1100 uint16_t result = le16_to_cpu(cmd->result);
1101 uint16_t command = le16_to_cpu(cmd->command);
1102 uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1103
1104 if (!upld_len) {
1105 dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
1106 return;
1107 }
1108
1109 /* Get BSS number and corresponding priv */
1110 priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1111 HostCmd_GET_BSS_TYPE(seq_num));
1112 if (!priv)
1113 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1114
1115 /* Update sequence number */
1116 seq_num = HostCmd_GET_SEQ_NO(seq_num);
1117 /* Clear RET_BIT from HostCmd */
1118 command &= HostCmd_CMD_ID_MASK;
1119
1120 if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
1121 dev_err(adapter->dev, "%s: received unexpected response for"
1122 " cmd %x, result = %x\n", __func__, command, result);
1123 return;
1124 }
1125
1126 if (result) {
1127 dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
1128 __func__);
1129 adapter->pm_wakeup_card_req = false;
1130 adapter->ps_state = PS_STATE_AWAKE;
1131 return;
1132 }
1133 adapter->pm_wakeup_card_req = true;
1134 if (adapter->is_hs_configured)
1135 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
1136 MWIFIEX_BSS_ROLE_ANY), true);
1137 adapter->ps_state = PS_STATE_SLEEP;
1138 cmd->command = cpu_to_le16(command);
1139 cmd->seq_num = cpu_to_le16(seq_num);
1140}
1141EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1142
1143/*
1144 * This function prepares an enhanced power mode command.
1145 *
1146 * This function can be used to disable power save or to configure
1147 * power save with auto PS or STA PS or auto deep sleep.
1148 *
1149 * Preparation includes -
1150 * - Setting command ID, action and proper size
1151 * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1152 * auto deep sleep TLV (as required)
1153 * - Ensuring correct endian-ness
1154 */
1155int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1156 struct host_cmd_ds_command *cmd,
1157 u16 cmd_action, uint16_t ps_bitmap,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001158 struct mwifiex_ds_auto_ds *auto_ds)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001159{
1160 struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1161 &cmd->params.psmode_enh;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001162 u8 *tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001163 u16 cmd_size = 0;
1164
1165 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1166 if (cmd_action == DIS_AUTO_PS) {
1167 psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1168 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001169 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
1170 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001171 } else if (cmd_action == GET_PS) {
1172 psmode_enh->action = cpu_to_le16(GET_PS);
1173 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001174 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
1175 sizeof(psmode_enh->params.ps_bitmap));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001176 } else if (cmd_action == EN_AUTO_PS) {
1177 psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001178 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1179 cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
1180 sizeof(psmode_enh->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001181 tlv = (u8 *) cmd + cmd_size;
1182 if (ps_bitmap & BITMAP_STA_PS) {
1183 struct mwifiex_adapter *adapter = priv->adapter;
1184 struct mwifiex_ie_types_ps_param *ps_tlv =
1185 (struct mwifiex_ie_types_ps_param *) tlv;
1186 struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1187 ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1188 ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1189 sizeof(struct mwifiex_ie_types_header));
1190 cmd_size += sizeof(*ps_tlv);
1191 tlv += sizeof(*ps_tlv);
1192 dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
1193 ps_mode->null_pkt_interval =
1194 cpu_to_le16(adapter->null_pkt_interval);
1195 ps_mode->multiple_dtims =
1196 cpu_to_le16(adapter->multiple_dtim);
1197 ps_mode->bcn_miss_timeout =
1198 cpu_to_le16(adapter->bcn_miss_time_out);
1199 ps_mode->local_listen_interval =
1200 cpu_to_le16(adapter->local_listen_interval);
1201 ps_mode->adhoc_wake_period =
1202 cpu_to_le16(adapter->adhoc_awake_period);
1203 ps_mode->delay_to_ps =
1204 cpu_to_le16(adapter->delay_to_ps);
1205 ps_mode->mode =
1206 cpu_to_le16(adapter->enhanced_ps_mode);
1207
1208 }
1209 if (ps_bitmap & BITMAP_AUTO_DS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001210 struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001211 (struct mwifiex_ie_types_auto_ds_param *) tlv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001212 u16 idletime = 0;
Marc Yang2b06bdb2011-03-30 18:12:44 -07001213
1214 auto_ds_tlv->header.type =
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001215 cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
Marc Yang2b06bdb2011-03-30 18:12:44 -07001216 auto_ds_tlv->header.len =
1217 cpu_to_le16(sizeof(*auto_ds_tlv) -
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001218 sizeof(struct mwifiex_ie_types_header));
Marc Yang2b06bdb2011-03-30 18:12:44 -07001219 cmd_size += sizeof(*auto_ds_tlv);
1220 tlv += sizeof(*auto_ds_tlv);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001221 if (auto_ds)
1222 idletime = auto_ds->idle_time;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001223 dev_dbg(priv->adapter->dev,
1224 "cmd: PS Command: Enter Auto Deep Sleep\n");
Marc Yang2b06bdb2011-03-30 18:12:44 -07001225 auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001226 }
1227 cmd->size = cpu_to_le16(cmd_size);
1228 }
1229 return 0;
1230}
1231
1232/*
1233 * This function handles the command response of an enhanced power mode
1234 * command.
1235 *
1236 * Handling includes changing the header fields into CPU format
1237 * and setting the current enhanced power mode in driver.
1238 */
1239int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1240 struct host_cmd_ds_command *resp,
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001241 struct mwifiex_ds_pm_cfg *pm_cfg)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001242{
1243 struct mwifiex_adapter *adapter = priv->adapter;
1244 struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1245 &resp->params.psmode_enh;
1246 uint16_t action = le16_to_cpu(ps_mode->action);
1247 uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1248 uint16_t auto_ps_bitmap =
Marc Yang2b06bdb2011-03-30 18:12:44 -07001249 le16_to_cpu(ps_mode->params.ps_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001250
1251 dev_dbg(adapter->dev, "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1252 __func__, resp->result, action);
1253 if (action == EN_AUTO_PS) {
1254 if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1255 dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
1256 priv->adapter->is_deep_sleep = true;
1257 }
1258 if (auto_ps_bitmap & BITMAP_STA_PS) {
1259 dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
1260 if (adapter->sleep_period.period)
1261 dev_dbg(adapter->dev, "cmd: set to uapsd/pps mode\n");
1262 }
1263 } else if (action == DIS_AUTO_PS) {
1264 if (ps_bitmap & BITMAP_AUTO_DS) {
1265 priv->adapter->is_deep_sleep = false;
1266 dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
1267 }
1268 if (ps_bitmap & BITMAP_STA_PS) {
1269 dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
1270 if (adapter->sleep_period.period) {
1271 adapter->delay_null_pkt = false;
1272 adapter->tx_lock_flag = false;
1273 adapter->pps_uapsd_mode = false;
1274 }
1275 }
1276 } else if (action == GET_PS) {
Marc Yang2b06bdb2011-03-30 18:12:44 -07001277 if (ps_bitmap & BITMAP_STA_PS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001278 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1279 else
1280 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1281
1282 dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
1283
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -07001284 if (pm_cfg) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001285 /* This section is for get power save mode */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001286 if (ps_bitmap & BITMAP_STA_PS)
1287 pm_cfg->param.ps_mode = 1;
1288 else
1289 pm_cfg->param.ps_mode = 0;
1290 }
1291 }
1292 return 0;
1293}
1294
1295/*
1296 * This function prepares command to get hardware specifications.
1297 *
1298 * Preparation includes -
1299 * - Setting command ID, action and proper size
1300 * - Setting permanent address parameter
1301 * - Ensuring correct endian-ness
1302 */
1303int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1304 struct host_cmd_ds_command *cmd)
1305{
1306 struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1307
1308 cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1309 cmd->size =
1310 cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1311 memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1312
1313 return 0;
1314}
1315
1316/*
1317 * This function handles the command response of get hardware
1318 * specifications.
1319 *
1320 * Handling includes changing the header fields into CPU format
1321 * and saving/updating the following parameters in driver -
1322 * - Firmware capability information
1323 * - Firmware band settings
1324 * - Ad-hoc start band and channel
1325 * - Ad-hoc 11n activation status
1326 * - Firmware release number
1327 * - Number of antennas
1328 * - Hardware address
1329 * - Hardware interface version
1330 * - Firmware version
1331 * - Region code
1332 * - 11n capabilities
1333 * - MCS support fields
1334 * - MP end port
1335 */
1336int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1337 struct host_cmd_ds_command *resp)
1338{
1339 struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1340 struct mwifiex_adapter *adapter = priv->adapter;
1341 int i;
1342
1343 adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1344
1345 if (IS_SUPPORT_MULTI_BANDS(adapter))
1346 adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1347 else
1348 adapter->fw_bands = BAND_B;
1349
1350 adapter->config_bands = adapter->fw_bands;
1351
1352 if (adapter->fw_bands & BAND_A) {
1353 if (adapter->fw_bands & BAND_GN) {
1354 adapter->config_bands |= BAND_AN;
1355 adapter->fw_bands |= BAND_AN;
1356 }
1357 if (adapter->fw_bands & BAND_AN) {
1358 adapter->adhoc_start_band = BAND_A | BAND_AN;
1359 adapter->adhoc_11n_enabled = true;
1360 } else {
1361 adapter->adhoc_start_band = BAND_A;
1362 }
1363 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1364 } else if (adapter->fw_bands & BAND_GN) {
1365 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1366 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1367 adapter->adhoc_11n_enabled = true;
1368 } else if (adapter->fw_bands & BAND_G) {
1369 adapter->adhoc_start_band = BAND_G | BAND_B;
1370 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1371 } else if (adapter->fw_bands & BAND_B) {
1372 adapter->adhoc_start_band = BAND_B;
1373 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1374 }
1375
1376 adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1377 adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1378
1379 dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
1380 adapter->fw_release_number);
1381 dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
1382 hw_spec->permanent_addr);
1383 dev_dbg(adapter->dev, "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
1384 le16_to_cpu(hw_spec->hw_if_version),
1385 le16_to_cpu(hw_spec->version));
1386
1387 if (priv->curr_addr[0] == 0xff)
1388 memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
1389
1390 adapter->region_code = le16_to_cpu(hw_spec->region_code);
1391
1392 for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1393 /* Use the region code to search for the index */
1394 if (adapter->region_code == region_code_index[i])
1395 break;
1396
1397 /* If it's unidentified region code, use the default (USA) */
1398 if (i >= MWIFIEX_MAX_REGION_CODE) {
1399 adapter->region_code = 0x10;
1400 dev_dbg(adapter->dev, "cmd: unknown region code, use default (USA)\n");
1401 }
1402
1403 adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001404 adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001405
1406 if (adapter->if_ops.update_mp_end_port)
1407 adapter->if_ops.update_mp_end_port(adapter,
1408 le16_to_cpu(hw_spec->mp_end_port));
1409
1410 return 0;
1411}