686943c52d18541aa5c7e001f54af191cf8c023b
[deliverable/linux.git] / drivers / net / wireless / mwifiex / cmdevt.c
1 /*
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 #include "11ac.h"
28
29 /*
30 * This function initializes a command node.
31 *
32 * The actual allocation of the node is not done by this function. It only
33 * initiates a node by filling it with default parameters. Similarly,
34 * allocation of the different buffers used (IOCTL buffer, data buffer) are
35 * not done by this function either.
36 */
37 static void
38 mwifiex_init_cmd_node(struct mwifiex_private *priv,
39 struct cmd_ctrl_node *cmd_node,
40 u32 cmd_oid, void *data_buf, bool sync)
41 {
42 cmd_node->priv = priv;
43 cmd_node->cmd_oid = cmd_oid;
44 if (sync) {
45 cmd_node->wait_q_enabled = true;
46 cmd_node->cmd_wait_q_woken = false;
47 cmd_node->condition = &cmd_node->cmd_wait_q_woken;
48 }
49 cmd_node->data_buf = data_buf;
50 cmd_node->cmd_skb = cmd_node->skb;
51 }
52
53 /*
54 * This function returns a command node from the free queue depending upon
55 * availability.
56 */
57 static struct cmd_ctrl_node *
58 mwifiex_get_cmd_node(struct mwifiex_adapter *adapter)
59 {
60 struct cmd_ctrl_node *cmd_node;
61 unsigned long flags;
62
63 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
64 if (list_empty(&adapter->cmd_free_q)) {
65 dev_err(adapter->dev, "GET_CMD_NODE: cmd node not available\n");
66 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
67 return NULL;
68 }
69 cmd_node = list_first_entry(&adapter->cmd_free_q,
70 struct cmd_ctrl_node, list);
71 list_del(&cmd_node->list);
72 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
73
74 return cmd_node;
75 }
76
77 /*
78 * This function cleans up a command node.
79 *
80 * The function resets the fields including the buffer pointers.
81 * This function does not try to free the buffers. They must be
82 * freed before calling this function.
83 *
84 * This function will however call the receive completion callback
85 * in case a response buffer is still available before resetting
86 * the pointer.
87 */
88 static void
89 mwifiex_clean_cmd_node(struct mwifiex_adapter *adapter,
90 struct cmd_ctrl_node *cmd_node)
91 {
92 cmd_node->cmd_oid = 0;
93 cmd_node->cmd_flag = 0;
94 cmd_node->data_buf = NULL;
95 cmd_node->wait_q_enabled = false;
96
97 if (cmd_node->cmd_skb)
98 skb_trim(cmd_node->cmd_skb, 0);
99
100 if (cmd_node->resp_skb) {
101 adapter->if_ops.cmdrsp_complete(adapter, cmd_node->resp_skb);
102 cmd_node->resp_skb = NULL;
103 }
104 }
105
106 /*
107 * This function sends a host command to the firmware.
108 *
109 * The function copies the host command into the driver command
110 * buffer, which will be transferred to the firmware later by the
111 * main thread.
112 */
113 static int mwifiex_cmd_host_cmd(struct mwifiex_private *priv,
114 struct host_cmd_ds_command *cmd,
115 struct mwifiex_ds_misc_cmd *pcmd_ptr)
116 {
117 /* Copy the HOST command to command buffer */
118 memcpy(cmd, pcmd_ptr->cmd, pcmd_ptr->len);
119 dev_dbg(priv->adapter->dev, "cmd: host cmd size = %d\n", pcmd_ptr->len);
120 return 0;
121 }
122
123 /*
124 * This function downloads a command to the firmware.
125 *
126 * The function performs sanity tests, sets the command sequence
127 * number and size, converts the header fields to CPU format before
128 * sending. Afterwards, it logs the command ID and action for debugging
129 * and sets up the command timeout timer.
130 */
131 static int mwifiex_dnld_cmd_to_fw(struct mwifiex_private *priv,
132 struct cmd_ctrl_node *cmd_node)
133 {
134
135 struct mwifiex_adapter *adapter = priv->adapter;
136 int ret;
137 struct host_cmd_ds_command *host_cmd;
138 uint16_t cmd_code;
139 uint16_t cmd_size;
140 struct timeval tstamp;
141 unsigned long flags;
142 __le32 tmp;
143
144 if (!adapter || !cmd_node)
145 return -1;
146
147 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
148
149 /* Sanity test */
150 if (host_cmd == NULL || host_cmd->size == 0) {
151 dev_err(adapter->dev, "DNLD_CMD: host_cmd is null"
152 " or cmd size is 0, not sending\n");
153 if (cmd_node->wait_q_enabled)
154 adapter->cmd_wait_q.status = -1;
155 mwifiex_recycle_cmd_node(adapter, cmd_node);
156 return -1;
157 }
158
159 cmd_code = le16_to_cpu(host_cmd->command);
160 cmd_size = le16_to_cpu(host_cmd->size);
161
162 if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET &&
163 cmd_code != HostCmd_CMD_FUNC_SHUTDOWN &&
164 cmd_code != HostCmd_CMD_FUNC_INIT) {
165 dev_err(adapter->dev,
166 "DNLD_CMD: FW in reset state, ignore cmd %#x\n",
167 cmd_code);
168 if (cmd_node->wait_q_enabled)
169 mwifiex_complete_cmd(adapter, cmd_node);
170 mwifiex_recycle_cmd_node(adapter, cmd_node);
171 queue_work(adapter->workqueue, &adapter->main_work);
172 return -1;
173 }
174
175 /* Set command sequence number */
176 adapter->seq_num++;
177 host_cmd->seq_num = cpu_to_le16(HostCmd_SET_SEQ_NO_BSS_INFO
178 (adapter->seq_num,
179 cmd_node->priv->bss_num,
180 cmd_node->priv->bss_type));
181
182 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
183 adapter->curr_cmd = cmd_node;
184 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
185
186 /* Adjust skb length */
187 if (cmd_node->cmd_skb->len > cmd_size)
188 /*
189 * cmd_size is less than sizeof(struct host_cmd_ds_command).
190 * Trim off the unused portion.
191 */
192 skb_trim(cmd_node->cmd_skb, cmd_size);
193 else if (cmd_node->cmd_skb->len < cmd_size)
194 /*
195 * cmd_size is larger than sizeof(struct host_cmd_ds_command)
196 * because we have appended custom IE TLV. Increase skb length
197 * accordingly.
198 */
199 skb_put(cmd_node->cmd_skb, cmd_size - cmd_node->cmd_skb->len);
200
201 do_gettimeofday(&tstamp);
202 dev_dbg(adapter->dev, "cmd: DNLD_CMD: (%lu.%lu): %#x, act %#x, len %d,"
203 " seqno %#x\n",
204 tstamp.tv_sec, tstamp.tv_usec, cmd_code,
205 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN)), cmd_size,
206 le16_to_cpu(host_cmd->seq_num));
207
208 if (adapter->iface_type == MWIFIEX_USB) {
209 tmp = cpu_to_le32(MWIFIEX_USB_TYPE_CMD);
210 skb_push(cmd_node->cmd_skb, MWIFIEX_TYPE_LEN);
211 memcpy(cmd_node->cmd_skb->data, &tmp, MWIFIEX_TYPE_LEN);
212 adapter->cmd_sent = true;
213 ret = adapter->if_ops.host_to_card(adapter,
214 MWIFIEX_USB_EP_CMD_EVENT,
215 cmd_node->cmd_skb, NULL);
216 skb_pull(cmd_node->cmd_skb, MWIFIEX_TYPE_LEN);
217 if (ret == -EBUSY)
218 cmd_node->cmd_skb = NULL;
219 } else {
220 skb_push(cmd_node->cmd_skb, INTF_HEADER_LEN);
221 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
222 cmd_node->cmd_skb, NULL);
223 skb_pull(cmd_node->cmd_skb, INTF_HEADER_LEN);
224 }
225
226 if (ret == -1) {
227 dev_err(adapter->dev, "DNLD_CMD: host to card failed\n");
228 if (adapter->iface_type == MWIFIEX_USB)
229 adapter->cmd_sent = false;
230 if (cmd_node->wait_q_enabled)
231 adapter->cmd_wait_q.status = -1;
232 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
233
234 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
235 adapter->curr_cmd = NULL;
236 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
237
238 adapter->dbg.num_cmd_host_to_card_failure++;
239 return -1;
240 }
241
242 /* Save the last command id and action to debug log */
243 adapter->dbg.last_cmd_index =
244 (adapter->dbg.last_cmd_index + 1) % DBG_CMD_NUM;
245 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index] = cmd_code;
246 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index] =
247 le16_to_cpu(*(__le16 *) ((u8 *) host_cmd + S_DS_GEN));
248
249 /* Clear BSS_NO_BITS from HostCmd */
250 cmd_code &= HostCmd_CMD_ID_MASK;
251
252 /* Setup the timer after transmit command */
253 mod_timer(&adapter->cmd_timer,
254 jiffies + msecs_to_jiffies(MWIFIEX_TIMER_10S));
255
256 return 0;
257 }
258
259 /*
260 * This function downloads a sleep confirm command to the firmware.
261 *
262 * The function performs sanity tests, sets the command sequence
263 * number and size, converts the header fields to CPU format before
264 * sending.
265 *
266 * No responses are needed for sleep confirm command.
267 */
268 static int mwifiex_dnld_sleep_confirm_cmd(struct mwifiex_adapter *adapter)
269 {
270 int ret;
271 struct mwifiex_private *priv;
272 struct mwifiex_opt_sleep_confirm *sleep_cfm_buf =
273 (struct mwifiex_opt_sleep_confirm *)
274 adapter->sleep_cfm->data;
275 struct sk_buff *sleep_cfm_tmp;
276 __le32 tmp;
277
278 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
279
280 sleep_cfm_buf->seq_num =
281 cpu_to_le16((HostCmd_SET_SEQ_NO_BSS_INFO
282 (adapter->seq_num, priv->bss_num,
283 priv->bss_type)));
284 adapter->seq_num++;
285
286 if (adapter->iface_type == MWIFIEX_USB) {
287 sleep_cfm_tmp =
288 dev_alloc_skb(sizeof(struct mwifiex_opt_sleep_confirm)
289 + MWIFIEX_TYPE_LEN);
290 skb_put(sleep_cfm_tmp, sizeof(struct mwifiex_opt_sleep_confirm)
291 + MWIFIEX_TYPE_LEN);
292 tmp = cpu_to_le32(MWIFIEX_USB_TYPE_CMD);
293 memcpy(sleep_cfm_tmp->data, &tmp, MWIFIEX_TYPE_LEN);
294 memcpy(sleep_cfm_tmp->data + MWIFIEX_TYPE_LEN,
295 adapter->sleep_cfm->data,
296 sizeof(struct mwifiex_opt_sleep_confirm));
297 ret = adapter->if_ops.host_to_card(adapter,
298 MWIFIEX_USB_EP_CMD_EVENT,
299 sleep_cfm_tmp, NULL);
300 if (ret != -EBUSY)
301 dev_kfree_skb_any(sleep_cfm_tmp);
302 } else {
303 skb_push(adapter->sleep_cfm, INTF_HEADER_LEN);
304 ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_CMD,
305 adapter->sleep_cfm, NULL);
306 skb_pull(adapter->sleep_cfm, INTF_HEADER_LEN);
307 }
308
309 if (ret == -1) {
310 dev_err(adapter->dev, "SLEEP_CFM: failed\n");
311 adapter->dbg.num_cmd_sleep_cfm_host_to_card_failure++;
312 return -1;
313 }
314 if (GET_BSS_ROLE(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY))
315 == MWIFIEX_BSS_ROLE_STA) {
316 if (!le16_to_cpu(sleep_cfm_buf->resp_ctrl))
317 /* Response is not needed for sleep
318 confirm command */
319 adapter->ps_state = PS_STATE_SLEEP;
320 else
321 adapter->ps_state = PS_STATE_SLEEP_CFM;
322
323 if (!le16_to_cpu(sleep_cfm_buf->resp_ctrl) &&
324 (adapter->is_hs_configured &&
325 !adapter->sleep_period.period)) {
326 adapter->pm_wakeup_card_req = true;
327 mwifiex_hs_activated_event(mwifiex_get_priv
328 (adapter, MWIFIEX_BSS_ROLE_STA), true);
329 }
330 }
331
332 return ret;
333 }
334
335 /*
336 * This function allocates the command buffers and links them to
337 * the command free queue.
338 *
339 * The driver uses a pre allocated number of command buffers, which
340 * are created at driver initializations and freed at driver cleanup.
341 * Every command needs to obtain a command buffer from this pool before
342 * it can be issued. The command free queue lists the command buffers
343 * currently free to use, while the command pending queue lists the
344 * command buffers already in use and awaiting handling. Command buffers
345 * are returned to the free queue after use.
346 */
347 int mwifiex_alloc_cmd_buffer(struct mwifiex_adapter *adapter)
348 {
349 struct cmd_ctrl_node *cmd_array;
350 u32 i;
351
352 /* Allocate and initialize struct cmd_ctrl_node */
353 cmd_array = kcalloc(MWIFIEX_NUM_OF_CMD_BUFFER,
354 sizeof(struct cmd_ctrl_node), GFP_KERNEL);
355 if (!cmd_array)
356 return -ENOMEM;
357
358 adapter->cmd_pool = cmd_array;
359
360 /* Allocate and initialize command buffers */
361 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
362 cmd_array[i].skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
363 if (!cmd_array[i].skb) {
364 dev_err(adapter->dev, "ALLOC_CMD_BUF: out of memory\n");
365 return -1;
366 }
367 }
368
369 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++)
370 mwifiex_insert_cmd_to_free_q(adapter, &cmd_array[i]);
371
372 return 0;
373 }
374
375 /*
376 * This function frees the command buffers.
377 *
378 * The function calls the completion callback for all the command
379 * buffers that still have response buffers associated with them.
380 */
381 int mwifiex_free_cmd_buffer(struct mwifiex_adapter *adapter)
382 {
383 struct cmd_ctrl_node *cmd_array;
384 u32 i;
385
386 /* Need to check if cmd pool is allocated or not */
387 if (!adapter->cmd_pool) {
388 dev_dbg(adapter->dev, "info: FREE_CMD_BUF: cmd_pool is null\n");
389 return 0;
390 }
391
392 cmd_array = adapter->cmd_pool;
393
394 /* Release shared memory buffers */
395 for (i = 0; i < MWIFIEX_NUM_OF_CMD_BUFFER; i++) {
396 if (cmd_array[i].skb) {
397 dev_dbg(adapter->dev, "cmd: free cmd buffer %d\n", i);
398 dev_kfree_skb_any(cmd_array[i].skb);
399 }
400 if (!cmd_array[i].resp_skb)
401 continue;
402
403 if (adapter->iface_type == MWIFIEX_USB)
404 adapter->if_ops.cmdrsp_complete(adapter,
405 cmd_array[i].resp_skb);
406 else
407 dev_kfree_skb_any(cmd_array[i].resp_skb);
408 }
409 /* Release struct cmd_ctrl_node */
410 if (adapter->cmd_pool) {
411 dev_dbg(adapter->dev, "cmd: free cmd pool\n");
412 kfree(adapter->cmd_pool);
413 adapter->cmd_pool = NULL;
414 }
415
416 return 0;
417 }
418
419 /*
420 * This function handles events generated by firmware.
421 *
422 * Event body of events received from firmware are not used (though they are
423 * saved), only the event ID is used. Some events are re-invoked by
424 * the driver, with a new event body.
425 *
426 * After processing, the function calls the completion callback
427 * for cleanup.
428 */
429 int mwifiex_process_event(struct mwifiex_adapter *adapter)
430 {
431 int ret;
432 struct mwifiex_private *priv =
433 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
434 struct sk_buff *skb = adapter->event_skb;
435 u32 eventcause = adapter->event_cause;
436 struct timeval tstamp;
437 struct mwifiex_rxinfo *rx_info;
438
439 /* Save the last event to debug log */
440 adapter->dbg.last_event_index =
441 (adapter->dbg.last_event_index + 1) % DBG_CMD_NUM;
442 adapter->dbg.last_event[adapter->dbg.last_event_index] =
443 (u16) eventcause;
444
445 /* Get BSS number and corresponding priv */
446 priv = mwifiex_get_priv_by_id(adapter, EVENT_GET_BSS_NUM(eventcause),
447 EVENT_GET_BSS_TYPE(eventcause));
448 if (!priv)
449 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
450 /* Clear BSS_NO_BITS from event */
451 eventcause &= EVENT_ID_MASK;
452 adapter->event_cause = eventcause;
453
454 if (skb) {
455 rx_info = MWIFIEX_SKB_RXCB(skb);
456 rx_info->bss_num = priv->bss_num;
457 rx_info->bss_type = priv->bss_type;
458 }
459
460 if (eventcause != EVENT_PS_SLEEP && eventcause != EVENT_PS_AWAKE) {
461 do_gettimeofday(&tstamp);
462 dev_dbg(adapter->dev, "event: %lu.%lu: cause: %#x\n",
463 tstamp.tv_sec, tstamp.tv_usec, eventcause);
464 } else {
465 /* Handle PS_SLEEP/AWAKE events on STA */
466 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA);
467 if (!priv)
468 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
469 }
470
471 if (priv->bss_role == MWIFIEX_BSS_ROLE_UAP)
472 ret = mwifiex_process_uap_event(priv);
473 else
474 ret = mwifiex_process_sta_event(priv);
475
476 adapter->event_cause = 0;
477 adapter->event_skb = NULL;
478 adapter->if_ops.event_complete(adapter, skb);
479
480 return ret;
481 }
482
483 /*
484 * This function prepares a command and send it to the firmware.
485 *
486 * Preparation includes -
487 * - Sanity tests to make sure the card is still present or the FW
488 * is not reset
489 * - Getting a new command node from the command free queue
490 * - Initializing the command node for default parameters
491 * - Fill up the non-default parameters and buffer pointers
492 * - Add the command to pending queue
493 */
494 int mwifiex_send_cmd(struct mwifiex_private *priv, u16 cmd_no,
495 u16 cmd_action, u32 cmd_oid, void *data_buf, bool sync)
496 {
497 int ret;
498 struct mwifiex_adapter *adapter = priv->adapter;
499 struct cmd_ctrl_node *cmd_node;
500 struct host_cmd_ds_command *cmd_ptr;
501
502 if (!adapter) {
503 pr_err("PREP_CMD: adapter is NULL\n");
504 return -1;
505 }
506
507 if (adapter->is_suspended) {
508 dev_err(adapter->dev, "PREP_CMD: device in suspended state\n");
509 return -1;
510 }
511
512 if (adapter->surprise_removed) {
513 dev_err(adapter->dev, "PREP_CMD: card is removed\n");
514 return -1;
515 }
516
517 if (adapter->num_cmd_timeout) {
518 dev_err(adapter->dev, "PREP_CMD: FW is in bad state\n");
519 return -1;
520 }
521
522 if (adapter->hw_status == MWIFIEX_HW_STATUS_RESET) {
523 if (cmd_no != HostCmd_CMD_FUNC_INIT) {
524 dev_err(adapter->dev, "PREP_CMD: FW in reset state\n");
525 return -1;
526 }
527 }
528
529 /* Get a new command node */
530 cmd_node = mwifiex_get_cmd_node(adapter);
531
532 if (!cmd_node) {
533 dev_err(adapter->dev, "PREP_CMD: no free cmd node\n");
534 return -1;
535 }
536
537 /* Initialize the command node */
538 mwifiex_init_cmd_node(priv, cmd_node, cmd_oid, data_buf, sync);
539
540 if (!cmd_node->cmd_skb) {
541 dev_err(adapter->dev, "PREP_CMD: no free cmd buf\n");
542 return -1;
543 }
544
545 memset(skb_put(cmd_node->cmd_skb, sizeof(struct host_cmd_ds_command)),
546 0, sizeof(struct host_cmd_ds_command));
547
548 cmd_ptr = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
549 cmd_ptr->command = cpu_to_le16(cmd_no);
550 cmd_ptr->result = 0;
551
552 /* Prepare command */
553 if (cmd_no) {
554 switch (cmd_no) {
555 case HostCmd_CMD_UAP_SYS_CONFIG:
556 case HostCmd_CMD_UAP_BSS_START:
557 case HostCmd_CMD_UAP_BSS_STOP:
558 case HostCmd_CMD_UAP_STA_DEAUTH:
559 ret = mwifiex_uap_prepare_cmd(priv, cmd_no, cmd_action,
560 cmd_oid, data_buf,
561 cmd_ptr);
562 break;
563 default:
564 ret = mwifiex_sta_prepare_cmd(priv, cmd_no, cmd_action,
565 cmd_oid, data_buf,
566 cmd_ptr);
567 break;
568 }
569 } else {
570 ret = mwifiex_cmd_host_cmd(priv, cmd_ptr, data_buf);
571 cmd_node->cmd_flag |= CMD_F_HOSTCMD;
572 }
573
574 /* Return error, since the command preparation failed */
575 if (ret) {
576 dev_err(adapter->dev, "PREP_CMD: cmd %#x preparation failed\n",
577 cmd_no);
578 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
579 return -1;
580 }
581
582 /* Send command */
583 if (cmd_no == HostCmd_CMD_802_11_SCAN ||
584 cmd_no == HostCmd_CMD_802_11_SCAN_EXT) {
585 mwifiex_queue_scan_cmd(priv, cmd_node);
586 } else {
587 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
588 queue_work(adapter->workqueue, &adapter->main_work);
589 if (cmd_node->wait_q_enabled)
590 ret = mwifiex_wait_queue_complete(adapter, cmd_node);
591 }
592
593 return ret;
594 }
595
596 /*
597 * This function returns a command to the command free queue.
598 *
599 * The function also calls the completion callback if required, before
600 * cleaning the command node and re-inserting it into the free queue.
601 */
602 void
603 mwifiex_insert_cmd_to_free_q(struct mwifiex_adapter *adapter,
604 struct cmd_ctrl_node *cmd_node)
605 {
606 unsigned long flags;
607
608 if (!cmd_node)
609 return;
610
611 if (cmd_node->wait_q_enabled)
612 mwifiex_complete_cmd(adapter, cmd_node);
613 /* Clean the node */
614 mwifiex_clean_cmd_node(adapter, cmd_node);
615
616 /* Insert node into cmd_free_q */
617 spin_lock_irqsave(&adapter->cmd_free_q_lock, flags);
618 list_add_tail(&cmd_node->list, &adapter->cmd_free_q);
619 spin_unlock_irqrestore(&adapter->cmd_free_q_lock, flags);
620 }
621
622 /* This function reuses a command node. */
623 void mwifiex_recycle_cmd_node(struct mwifiex_adapter *adapter,
624 struct cmd_ctrl_node *cmd_node)
625 {
626 struct host_cmd_ds_command *host_cmd = (void *)cmd_node->cmd_skb->data;
627
628 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
629
630 atomic_dec(&adapter->cmd_pending);
631 dev_dbg(adapter->dev, "cmd: FREE_CMD: cmd=%#x, cmd_pending=%d\n",
632 le16_to_cpu(host_cmd->command),
633 atomic_read(&adapter->cmd_pending));
634 }
635
636 /*
637 * This function queues a command to the command pending queue.
638 *
639 * This in effect adds the command to the command list to be executed.
640 * Exit PS command is handled specially, by placing it always to the
641 * front of the command queue.
642 */
643 void
644 mwifiex_insert_cmd_to_pending_q(struct mwifiex_adapter *adapter,
645 struct cmd_ctrl_node *cmd_node, u32 add_tail)
646 {
647 struct host_cmd_ds_command *host_cmd = NULL;
648 u16 command;
649 unsigned long flags;
650
651 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
652 if (!host_cmd) {
653 dev_err(adapter->dev, "QUEUE_CMD: host_cmd is NULL\n");
654 return;
655 }
656
657 command = le16_to_cpu(host_cmd->command);
658
659 /* Exit_PS command needs to be queued in the header always. */
660 if (command == HostCmd_CMD_802_11_PS_MODE_ENH) {
661 struct host_cmd_ds_802_11_ps_mode_enh *pm =
662 &host_cmd->params.psmode_enh;
663 if ((le16_to_cpu(pm->action) == DIS_PS) ||
664 (le16_to_cpu(pm->action) == DIS_AUTO_PS)) {
665 if (adapter->ps_state != PS_STATE_AWAKE)
666 add_tail = false;
667 }
668 }
669
670 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
671 if (add_tail)
672 list_add_tail(&cmd_node->list, &adapter->cmd_pending_q);
673 else
674 list_add(&cmd_node->list, &adapter->cmd_pending_q);
675 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
676
677 atomic_inc(&adapter->cmd_pending);
678 dev_dbg(adapter->dev, "cmd: QUEUE_CMD: cmd=%#x, cmd_pending=%d\n",
679 command, atomic_read(&adapter->cmd_pending));
680 }
681
682 /*
683 * This function executes the next command in command pending queue.
684 *
685 * This function will fail if a command is already in processing stage,
686 * otherwise it will dequeue the first command from the command pending
687 * queue and send to the firmware.
688 *
689 * If the device is currently in host sleep mode, any commands, except the
690 * host sleep configuration command will de-activate the host sleep. For PS
691 * mode, the function will put the firmware back to sleep if applicable.
692 */
693 int mwifiex_exec_next_cmd(struct mwifiex_adapter *adapter)
694 {
695 struct mwifiex_private *priv;
696 struct cmd_ctrl_node *cmd_node;
697 int ret = 0;
698 struct host_cmd_ds_command *host_cmd;
699 unsigned long cmd_flags;
700 unsigned long cmd_pending_q_flags;
701
702 /* Check if already in processing */
703 if (adapter->curr_cmd) {
704 dev_err(adapter->dev, "EXEC_NEXT_CMD: cmd in processing\n");
705 return -1;
706 }
707
708 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
709 /* Check if any command is pending */
710 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
711 if (list_empty(&adapter->cmd_pending_q)) {
712 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
713 cmd_pending_q_flags);
714 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
715 return 0;
716 }
717 cmd_node = list_first_entry(&adapter->cmd_pending_q,
718 struct cmd_ctrl_node, list);
719 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
720 cmd_pending_q_flags);
721
722 host_cmd = (struct host_cmd_ds_command *) (cmd_node->cmd_skb->data);
723 priv = cmd_node->priv;
724
725 if (adapter->ps_state != PS_STATE_AWAKE) {
726 dev_err(adapter->dev, "%s: cannot send cmd in sleep state,"
727 " this should not happen\n", __func__);
728 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
729 return ret;
730 }
731
732 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_pending_q_flags);
733 list_del(&cmd_node->list);
734 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock,
735 cmd_pending_q_flags);
736
737 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
738 ret = mwifiex_dnld_cmd_to_fw(priv, cmd_node);
739 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
740 /* Any command sent to the firmware when host is in sleep
741 * mode should de-configure host sleep. We should skip the
742 * host sleep configuration command itself though
743 */
744 if (priv && (host_cmd->command !=
745 cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH))) {
746 if (adapter->hs_activated) {
747 adapter->is_hs_configured = false;
748 mwifiex_hs_activated_event(priv, false);
749 }
750 }
751
752 return ret;
753 }
754
755 /*
756 * This function handles the command response.
757 *
758 * After processing, the function cleans the command node and puts
759 * it back to the command free queue.
760 */
761 int mwifiex_process_cmdresp(struct mwifiex_adapter *adapter)
762 {
763 struct host_cmd_ds_command *resp;
764 struct mwifiex_private *priv =
765 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
766 int ret = 0;
767 uint16_t orig_cmdresp_no;
768 uint16_t cmdresp_no;
769 uint16_t cmdresp_result;
770 struct timeval tstamp;
771 unsigned long flags;
772
773 /* Now we got response from FW, cancel the command timer */
774 del_timer_sync(&adapter->cmd_timer);
775
776 if (!adapter->curr_cmd || !adapter->curr_cmd->resp_skb) {
777 resp = (struct host_cmd_ds_command *) adapter->upld_buf;
778 dev_err(adapter->dev, "CMD_RESP: NULL curr_cmd, %#x\n",
779 le16_to_cpu(resp->command));
780 return -1;
781 }
782
783 adapter->num_cmd_timeout = 0;
784
785 resp = (struct host_cmd_ds_command *) adapter->curr_cmd->resp_skb->data;
786 if (adapter->curr_cmd->cmd_flag & CMD_F_CANCELED) {
787 dev_err(adapter->dev, "CMD_RESP: %#x been canceled\n",
788 le16_to_cpu(resp->command));
789 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
790 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
791 adapter->curr_cmd = NULL;
792 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
793 return -1;
794 }
795
796 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
797 /* Copy original response back to response buffer */
798 struct mwifiex_ds_misc_cmd *hostcmd;
799 uint16_t size = le16_to_cpu(resp->size);
800 dev_dbg(adapter->dev, "info: host cmd resp size = %d\n", size);
801 size = min_t(u16, size, MWIFIEX_SIZE_OF_CMD_BUFFER);
802 if (adapter->curr_cmd->data_buf) {
803 hostcmd = adapter->curr_cmd->data_buf;
804 hostcmd->len = size;
805 memcpy(hostcmd->cmd, resp, size);
806 }
807 }
808 orig_cmdresp_no = le16_to_cpu(resp->command);
809
810 /* Get BSS number and corresponding priv */
811 priv = mwifiex_get_priv_by_id(adapter,
812 HostCmd_GET_BSS_NO(le16_to_cpu(resp->seq_num)),
813 HostCmd_GET_BSS_TYPE(le16_to_cpu(resp->seq_num)));
814 if (!priv)
815 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
816 /* Clear RET_BIT from HostCmd */
817 resp->command = cpu_to_le16(orig_cmdresp_no & HostCmd_CMD_ID_MASK);
818
819 cmdresp_no = le16_to_cpu(resp->command);
820 cmdresp_result = le16_to_cpu(resp->result);
821
822 /* Save the last command response to debug log */
823 adapter->dbg.last_cmd_resp_index =
824 (adapter->dbg.last_cmd_resp_index + 1) % DBG_CMD_NUM;
825 adapter->dbg.last_cmd_resp_id[adapter->dbg.last_cmd_resp_index] =
826 orig_cmdresp_no;
827
828 do_gettimeofday(&tstamp);
829 dev_dbg(adapter->dev, "cmd: CMD_RESP: (%lu.%lu): 0x%x, result %d,"
830 " len %d, seqno 0x%x\n",
831 tstamp.tv_sec, tstamp.tv_usec, orig_cmdresp_no, cmdresp_result,
832 le16_to_cpu(resp->size), le16_to_cpu(resp->seq_num));
833
834 if (!(orig_cmdresp_no & HostCmd_RET_BIT)) {
835 dev_err(adapter->dev, "CMD_RESP: invalid cmd resp\n");
836 if (adapter->curr_cmd->wait_q_enabled)
837 adapter->cmd_wait_q.status = -1;
838
839 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
840 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
841 adapter->curr_cmd = NULL;
842 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
843 return -1;
844 }
845
846 if (adapter->curr_cmd->cmd_flag & CMD_F_HOSTCMD) {
847 adapter->curr_cmd->cmd_flag &= ~CMD_F_HOSTCMD;
848 if ((cmdresp_result == HostCmd_RESULT_OK) &&
849 (cmdresp_no == HostCmd_CMD_802_11_HS_CFG_ENH))
850 ret = mwifiex_ret_802_11_hs_cfg(priv, resp);
851 } else {
852 /* handle response */
853 ret = mwifiex_process_sta_cmdresp(priv, cmdresp_no, resp);
854 }
855
856 /* Check init command response */
857 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING) {
858 if (ret) {
859 dev_err(adapter->dev, "%s: cmd %#x failed during "
860 "initialization\n", __func__, cmdresp_no);
861 mwifiex_init_fw_complete(adapter);
862 return -1;
863 } else if (adapter->last_init_cmd == cmdresp_no)
864 adapter->hw_status = MWIFIEX_HW_STATUS_INIT_DONE;
865 }
866
867 if (adapter->curr_cmd) {
868 if (adapter->curr_cmd->wait_q_enabled)
869 adapter->cmd_wait_q.status = ret;
870
871 mwifiex_recycle_cmd_node(adapter, adapter->curr_cmd);
872
873 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
874 adapter->curr_cmd = NULL;
875 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
876 }
877
878 return ret;
879 }
880
881 /*
882 * This function handles the timeout of command sending.
883 *
884 * It will re-send the same command again.
885 */
886 void
887 mwifiex_cmd_timeout_func(unsigned long function_context)
888 {
889 struct mwifiex_adapter *adapter =
890 (struct mwifiex_adapter *) function_context;
891 struct cmd_ctrl_node *cmd_node;
892 struct timeval tstamp;
893
894 adapter->num_cmd_timeout++;
895 if (!adapter->curr_cmd) {
896 dev_dbg(adapter->dev, "cmd: empty curr_cmd\n");
897 return;
898 }
899 cmd_node = adapter->curr_cmd;
900 if (cmd_node) {
901 adapter->dbg.timeout_cmd_id =
902 adapter->dbg.last_cmd_id[adapter->dbg.last_cmd_index];
903 adapter->dbg.timeout_cmd_act =
904 adapter->dbg.last_cmd_act[adapter->dbg.last_cmd_index];
905 do_gettimeofday(&tstamp);
906 dev_err(adapter->dev,
907 "%s: Timeout cmd id (%lu.%lu) = %#x, act = %#x\n",
908 __func__, tstamp.tv_sec, tstamp.tv_usec,
909 adapter->dbg.timeout_cmd_id,
910 adapter->dbg.timeout_cmd_act);
911
912 dev_err(adapter->dev, "num_data_h2c_failure = %d\n",
913 adapter->dbg.num_tx_host_to_card_failure);
914 dev_err(adapter->dev, "num_cmd_h2c_failure = %d\n",
915 adapter->dbg.num_cmd_host_to_card_failure);
916
917 dev_err(adapter->dev, "num_cmd_timeout = %d\n",
918 adapter->num_cmd_timeout);
919 dev_err(adapter->dev, "num_tx_timeout = %d\n",
920 adapter->dbg.num_tx_timeout);
921
922 dev_err(adapter->dev, "last_cmd_index = %d\n",
923 adapter->dbg.last_cmd_index);
924 dev_err(adapter->dev, "last_cmd_id: %*ph\n",
925 (int)sizeof(adapter->dbg.last_cmd_id),
926 adapter->dbg.last_cmd_id);
927 dev_err(adapter->dev, "last_cmd_act: %*ph\n",
928 (int)sizeof(adapter->dbg.last_cmd_act),
929 adapter->dbg.last_cmd_act);
930
931 dev_err(adapter->dev, "last_cmd_resp_index = %d\n",
932 adapter->dbg.last_cmd_resp_index);
933 dev_err(adapter->dev, "last_cmd_resp_id: %*ph\n",
934 (int)sizeof(adapter->dbg.last_cmd_resp_id),
935 adapter->dbg.last_cmd_resp_id);
936
937 dev_err(adapter->dev, "last_event_index = %d\n",
938 adapter->dbg.last_event_index);
939 dev_err(adapter->dev, "last_event: %*ph\n",
940 (int)sizeof(adapter->dbg.last_event),
941 adapter->dbg.last_event);
942
943 dev_err(adapter->dev, "data_sent=%d cmd_sent=%d\n",
944 adapter->data_sent, adapter->cmd_sent);
945
946 dev_err(adapter->dev, "ps_mode=%d ps_state=%d\n",
947 adapter->ps_mode, adapter->ps_state);
948
949 if (cmd_node->wait_q_enabled) {
950 adapter->cmd_wait_q.status = -ETIMEDOUT;
951 wake_up_interruptible(&adapter->cmd_wait_q.wait);
952 mwifiex_cancel_pending_ioctl(adapter);
953 /* reset cmd_sent flag to unblock new commands */
954 adapter->cmd_sent = false;
955 }
956 }
957 if (adapter->hw_status == MWIFIEX_HW_STATUS_INITIALIZING)
958 mwifiex_init_fw_complete(adapter);
959
960 if (adapter->if_ops.card_reset)
961 adapter->if_ops.card_reset(adapter);
962 }
963
964 /*
965 * This function cancels all the pending commands.
966 *
967 * The current command, all commands in command pending queue and all scan
968 * commands in scan pending queue are cancelled. All the completion callbacks
969 * are called with failure status to ensure cleanup.
970 */
971 void
972 mwifiex_cancel_all_pending_cmd(struct mwifiex_adapter *adapter)
973 {
974 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node;
975 unsigned long flags, cmd_flags;
976 struct mwifiex_private *priv;
977 int i;
978
979 /* Cancel current cmd */
980 if ((adapter->curr_cmd) && (adapter->curr_cmd->wait_q_enabled)) {
981 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
982 adapter->curr_cmd->wait_q_enabled = false;
983 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
984 adapter->cmd_wait_q.status = -1;
985 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
986 }
987 /* Cancel all pending command */
988 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
989 list_for_each_entry_safe(cmd_node, tmp_node,
990 &adapter->cmd_pending_q, list) {
991 list_del(&cmd_node->list);
992 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
993
994 if (cmd_node->wait_q_enabled) {
995 adapter->cmd_wait_q.status = -1;
996 mwifiex_complete_cmd(adapter, cmd_node);
997 cmd_node->wait_q_enabled = false;
998 }
999 mwifiex_recycle_cmd_node(adapter, cmd_node);
1000 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1001 }
1002 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1003
1004 /* Cancel all pending scan command */
1005 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
1006 list_for_each_entry_safe(cmd_node, tmp_node,
1007 &adapter->scan_pending_q, list) {
1008 list_del(&cmd_node->list);
1009 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
1010
1011 cmd_node->wait_q_enabled = false;
1012 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1013 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
1014 }
1015 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
1016
1017 if (adapter->scan_processing) {
1018 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1019 adapter->scan_processing = false;
1020 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1021 for (i = 0; i < adapter->priv_num; i++) {
1022 priv = adapter->priv[i];
1023 if (!priv)
1024 continue;
1025 if (priv->scan_request) {
1026 dev_dbg(adapter->dev, "info: aborting scan\n");
1027 cfg80211_scan_done(priv->scan_request, 1);
1028 priv->scan_request = NULL;
1029 }
1030 }
1031 }
1032 }
1033
1034 /*
1035 * This function cancels all pending commands that matches with
1036 * the given IOCTL request.
1037 *
1038 * Both the current command buffer and the pending command queue are
1039 * searched for matching IOCTL request. The completion callback of
1040 * the matched command is called with failure status to ensure cleanup.
1041 * In case of scan commands, all pending commands in scan pending queue
1042 * are cancelled.
1043 */
1044 void
1045 mwifiex_cancel_pending_ioctl(struct mwifiex_adapter *adapter)
1046 {
1047 struct cmd_ctrl_node *cmd_node = NULL, *tmp_node = NULL;
1048 unsigned long cmd_flags;
1049 unsigned long scan_pending_q_flags;
1050 struct mwifiex_private *priv;
1051 int i;
1052
1053 if ((adapter->curr_cmd) &&
1054 (adapter->curr_cmd->wait_q_enabled)) {
1055 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1056 cmd_node = adapter->curr_cmd;
1057 cmd_node->wait_q_enabled = false;
1058 cmd_node->cmd_flag |= CMD_F_CANCELED;
1059 mwifiex_recycle_cmd_node(adapter, cmd_node);
1060 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
1061 adapter->curr_cmd = NULL;
1062 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1063 }
1064
1065 /* Cancel all pending scan command */
1066 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1067 scan_pending_q_flags);
1068 list_for_each_entry_safe(cmd_node, tmp_node,
1069 &adapter->scan_pending_q, list) {
1070 list_del(&cmd_node->list);
1071 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1072 scan_pending_q_flags);
1073 cmd_node->wait_q_enabled = false;
1074 mwifiex_insert_cmd_to_free_q(adapter, cmd_node);
1075 spin_lock_irqsave(&adapter->scan_pending_q_lock,
1076 scan_pending_q_flags);
1077 }
1078 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1079 scan_pending_q_flags);
1080
1081 if (adapter->scan_processing) {
1082 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
1083 adapter->scan_processing = false;
1084 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
1085 for (i = 0; i < adapter->priv_num; i++) {
1086 priv = adapter->priv[i];
1087 if (!priv)
1088 continue;
1089 if (priv->scan_request) {
1090 dev_dbg(adapter->dev, "info: aborting scan\n");
1091 cfg80211_scan_done(priv->scan_request, 1);
1092 priv->scan_request = NULL;
1093 }
1094 }
1095 }
1096 adapter->cmd_wait_q.status = -1;
1097 }
1098
1099 /*
1100 * This function sends the sleep confirm command to firmware, if
1101 * possible.
1102 *
1103 * The sleep confirm command cannot be issued if command response,
1104 * data response or event response is awaiting handling, or if we
1105 * are in the middle of sending a command, or expecting a command
1106 * response.
1107 */
1108 void
1109 mwifiex_check_ps_cond(struct mwifiex_adapter *adapter)
1110 {
1111 if (!adapter->cmd_sent &&
1112 !adapter->curr_cmd && !IS_CARD_RX_RCVD(adapter))
1113 mwifiex_dnld_sleep_confirm_cmd(adapter);
1114 else
1115 dev_dbg(adapter->dev,
1116 "cmd: Delay Sleep Confirm (%s%s%s)\n",
1117 (adapter->cmd_sent) ? "D" : "",
1118 (adapter->curr_cmd) ? "C" : "",
1119 (IS_CARD_RX_RCVD(adapter)) ? "R" : "");
1120 }
1121
1122 /*
1123 * This function sends a Host Sleep activated event to applications.
1124 *
1125 * This event is generated by the driver, with a blank event body.
1126 */
1127 void
1128 mwifiex_hs_activated_event(struct mwifiex_private *priv, u8 activated)
1129 {
1130 if (activated) {
1131 if (priv->adapter->is_hs_configured) {
1132 priv->adapter->hs_activated = true;
1133 mwifiex_update_rxreor_flags(priv->adapter,
1134 RXREOR_FORCE_NO_DROP);
1135 dev_dbg(priv->adapter->dev, "event: hs_activated\n");
1136 priv->adapter->hs_activate_wait_q_woken = true;
1137 wake_up_interruptible(
1138 &priv->adapter->hs_activate_wait_q);
1139 } else {
1140 dev_dbg(priv->adapter->dev, "event: HS not configured\n");
1141 }
1142 } else {
1143 dev_dbg(priv->adapter->dev, "event: hs_deactivated\n");
1144 priv->adapter->hs_activated = false;
1145 }
1146 }
1147
1148 /*
1149 * This function handles the command response of a Host Sleep configuration
1150 * command.
1151 *
1152 * Handling includes changing the header fields into CPU format
1153 * and setting the current host sleep activation status in driver.
1154 *
1155 * In case host sleep status change, the function generates an event to
1156 * notify the applications.
1157 */
1158 int mwifiex_ret_802_11_hs_cfg(struct mwifiex_private *priv,
1159 struct host_cmd_ds_command *resp)
1160 {
1161 struct mwifiex_adapter *adapter = priv->adapter;
1162 struct host_cmd_ds_802_11_hs_cfg_enh *phs_cfg =
1163 &resp->params.opt_hs_cfg;
1164 uint32_t conditions = le32_to_cpu(phs_cfg->params.hs_config.conditions);
1165
1166 if (phs_cfg->action == cpu_to_le16(HS_ACTIVATE) &&
1167 adapter->iface_type != MWIFIEX_USB) {
1168 mwifiex_hs_activated_event(priv, true);
1169 return 0;
1170 } else {
1171 dev_dbg(adapter->dev, "cmd: CMD_RESP: HS_CFG cmd reply"
1172 " result=%#x, conditions=0x%x gpio=0x%x gap=0x%x\n",
1173 resp->result, conditions,
1174 phs_cfg->params.hs_config.gpio,
1175 phs_cfg->params.hs_config.gap);
1176 }
1177 if (conditions != HS_CFG_CANCEL) {
1178 adapter->is_hs_configured = true;
1179 if (adapter->iface_type == MWIFIEX_USB)
1180 mwifiex_hs_activated_event(priv, true);
1181 } else {
1182 adapter->is_hs_configured = false;
1183 if (adapter->hs_activated)
1184 mwifiex_hs_activated_event(priv, false);
1185 }
1186
1187 return 0;
1188 }
1189
1190 /*
1191 * This function wakes up the adapter and generates a Host Sleep
1192 * cancel event on receiving the power up interrupt.
1193 */
1194 void
1195 mwifiex_process_hs_config(struct mwifiex_adapter *adapter)
1196 {
1197 dev_dbg(adapter->dev, "info: %s: auto cancelling host sleep"
1198 " since there is interrupt from the firmware\n", __func__);
1199
1200 adapter->if_ops.wakeup(adapter);
1201 adapter->hs_activated = false;
1202 adapter->is_hs_configured = false;
1203 adapter->is_suspended = false;
1204 mwifiex_hs_activated_event(mwifiex_get_priv(adapter,
1205 MWIFIEX_BSS_ROLE_ANY),
1206 false);
1207 }
1208 EXPORT_SYMBOL_GPL(mwifiex_process_hs_config);
1209
1210 /*
1211 * This function handles the command response of a sleep confirm command.
1212 *
1213 * The function sets the card state to SLEEP if the response indicates success.
1214 */
1215 void
1216 mwifiex_process_sleep_confirm_resp(struct mwifiex_adapter *adapter,
1217 u8 *pbuf, u32 upld_len)
1218 {
1219 struct host_cmd_ds_command *cmd = (struct host_cmd_ds_command *) pbuf;
1220 struct mwifiex_private *priv =
1221 mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1222 uint16_t result = le16_to_cpu(cmd->result);
1223 uint16_t command = le16_to_cpu(cmd->command);
1224 uint16_t seq_num = le16_to_cpu(cmd->seq_num);
1225
1226 if (!upld_len) {
1227 dev_err(adapter->dev, "%s: cmd size is 0\n", __func__);
1228 return;
1229 }
1230
1231 /* Get BSS number and corresponding priv */
1232 priv = mwifiex_get_priv_by_id(adapter, HostCmd_GET_BSS_NO(seq_num),
1233 HostCmd_GET_BSS_TYPE(seq_num));
1234 if (!priv)
1235 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1236
1237 /* Update sequence number */
1238 seq_num = HostCmd_GET_SEQ_NO(seq_num);
1239 /* Clear RET_BIT from HostCmd */
1240 command &= HostCmd_CMD_ID_MASK;
1241
1242 if (command != HostCmd_CMD_802_11_PS_MODE_ENH) {
1243 dev_err(adapter->dev,
1244 "%s: rcvd unexpected resp for cmd %#x, result = %x\n",
1245 __func__, command, result);
1246 return;
1247 }
1248
1249 if (result) {
1250 dev_err(adapter->dev, "%s: sleep confirm cmd failed\n",
1251 __func__);
1252 adapter->pm_wakeup_card_req = false;
1253 adapter->ps_state = PS_STATE_AWAKE;
1254 return;
1255 }
1256 adapter->pm_wakeup_card_req = true;
1257 if (adapter->is_hs_configured)
1258 mwifiex_hs_activated_event(mwifiex_get_priv
1259 (adapter, MWIFIEX_BSS_ROLE_ANY),
1260 true);
1261 adapter->ps_state = PS_STATE_SLEEP;
1262 cmd->command = cpu_to_le16(command);
1263 cmd->seq_num = cpu_to_le16(seq_num);
1264 }
1265 EXPORT_SYMBOL_GPL(mwifiex_process_sleep_confirm_resp);
1266
1267 /*
1268 * This function prepares an enhanced power mode command.
1269 *
1270 * This function can be used to disable power save or to configure
1271 * power save with auto PS or STA PS or auto deep sleep.
1272 *
1273 * Preparation includes -
1274 * - Setting command ID, action and proper size
1275 * - Setting Power Save bitmap, PS parameters TLV, PS mode TLV,
1276 * auto deep sleep TLV (as required)
1277 * - Ensuring correct endian-ness
1278 */
1279 int mwifiex_cmd_enh_power_mode(struct mwifiex_private *priv,
1280 struct host_cmd_ds_command *cmd,
1281 u16 cmd_action, uint16_t ps_bitmap,
1282 struct mwifiex_ds_auto_ds *auto_ds)
1283 {
1284 struct host_cmd_ds_802_11_ps_mode_enh *psmode_enh =
1285 &cmd->params.psmode_enh;
1286 u8 *tlv;
1287 u16 cmd_size = 0;
1288
1289 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_PS_MODE_ENH);
1290 if (cmd_action == DIS_AUTO_PS) {
1291 psmode_enh->action = cpu_to_le16(DIS_AUTO_PS);
1292 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1293 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
1294 sizeof(psmode_enh->params.ps_bitmap));
1295 } else if (cmd_action == GET_PS) {
1296 psmode_enh->action = cpu_to_le16(GET_PS);
1297 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1298 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(psmode_enh->action) +
1299 sizeof(psmode_enh->params.ps_bitmap));
1300 } else if (cmd_action == EN_AUTO_PS) {
1301 psmode_enh->action = cpu_to_le16(EN_AUTO_PS);
1302 psmode_enh->params.ps_bitmap = cpu_to_le16(ps_bitmap);
1303 cmd_size = S_DS_GEN + sizeof(psmode_enh->action) +
1304 sizeof(psmode_enh->params.ps_bitmap);
1305 tlv = (u8 *) cmd + cmd_size;
1306 if (ps_bitmap & BITMAP_STA_PS) {
1307 struct mwifiex_adapter *adapter = priv->adapter;
1308 struct mwifiex_ie_types_ps_param *ps_tlv =
1309 (struct mwifiex_ie_types_ps_param *) tlv;
1310 struct mwifiex_ps_param *ps_mode = &ps_tlv->param;
1311 ps_tlv->header.type = cpu_to_le16(TLV_TYPE_PS_PARAM);
1312 ps_tlv->header.len = cpu_to_le16(sizeof(*ps_tlv) -
1313 sizeof(struct mwifiex_ie_types_header));
1314 cmd_size += sizeof(*ps_tlv);
1315 tlv += sizeof(*ps_tlv);
1316 dev_dbg(adapter->dev, "cmd: PS Command: Enter PS\n");
1317 ps_mode->null_pkt_interval =
1318 cpu_to_le16(adapter->null_pkt_interval);
1319 ps_mode->multiple_dtims =
1320 cpu_to_le16(adapter->multiple_dtim);
1321 ps_mode->bcn_miss_timeout =
1322 cpu_to_le16(adapter->bcn_miss_time_out);
1323 ps_mode->local_listen_interval =
1324 cpu_to_le16(adapter->local_listen_interval);
1325 ps_mode->adhoc_wake_period =
1326 cpu_to_le16(adapter->adhoc_awake_period);
1327 ps_mode->delay_to_ps =
1328 cpu_to_le16(adapter->delay_to_ps);
1329 ps_mode->mode = cpu_to_le16(adapter->enhanced_ps_mode);
1330
1331 }
1332 if (ps_bitmap & BITMAP_AUTO_DS) {
1333 struct mwifiex_ie_types_auto_ds_param *auto_ds_tlv =
1334 (struct mwifiex_ie_types_auto_ds_param *) tlv;
1335 u16 idletime = 0;
1336
1337 auto_ds_tlv->header.type =
1338 cpu_to_le16(TLV_TYPE_AUTO_DS_PARAM);
1339 auto_ds_tlv->header.len =
1340 cpu_to_le16(sizeof(*auto_ds_tlv) -
1341 sizeof(struct mwifiex_ie_types_header));
1342 cmd_size += sizeof(*auto_ds_tlv);
1343 tlv += sizeof(*auto_ds_tlv);
1344 if (auto_ds)
1345 idletime = auto_ds->idle_time;
1346 dev_dbg(priv->adapter->dev,
1347 "cmd: PS Command: Enter Auto Deep Sleep\n");
1348 auto_ds_tlv->deep_sleep_timeout = cpu_to_le16(idletime);
1349 }
1350 cmd->size = cpu_to_le16(cmd_size);
1351 }
1352 return 0;
1353 }
1354
1355 /*
1356 * This function handles the command response of an enhanced power mode
1357 * command.
1358 *
1359 * Handling includes changing the header fields into CPU format
1360 * and setting the current enhanced power mode in driver.
1361 */
1362 int mwifiex_ret_enh_power_mode(struct mwifiex_private *priv,
1363 struct host_cmd_ds_command *resp,
1364 struct mwifiex_ds_pm_cfg *pm_cfg)
1365 {
1366 struct mwifiex_adapter *adapter = priv->adapter;
1367 struct host_cmd_ds_802_11_ps_mode_enh *ps_mode =
1368 &resp->params.psmode_enh;
1369 uint16_t action = le16_to_cpu(ps_mode->action);
1370 uint16_t ps_bitmap = le16_to_cpu(ps_mode->params.ps_bitmap);
1371 uint16_t auto_ps_bitmap =
1372 le16_to_cpu(ps_mode->params.ps_bitmap);
1373
1374 dev_dbg(adapter->dev,
1375 "info: %s: PS_MODE cmd reply result=%#x action=%#X\n",
1376 __func__, resp->result, action);
1377 if (action == EN_AUTO_PS) {
1378 if (auto_ps_bitmap & BITMAP_AUTO_DS) {
1379 dev_dbg(adapter->dev, "cmd: Enabled auto deep sleep\n");
1380 priv->adapter->is_deep_sleep = true;
1381 }
1382 if (auto_ps_bitmap & BITMAP_STA_PS) {
1383 dev_dbg(adapter->dev, "cmd: Enabled STA power save\n");
1384 if (adapter->sleep_period.period)
1385 dev_dbg(adapter->dev,
1386 "cmd: set to uapsd/pps mode\n");
1387 }
1388 } else if (action == DIS_AUTO_PS) {
1389 if (ps_bitmap & BITMAP_AUTO_DS) {
1390 priv->adapter->is_deep_sleep = false;
1391 dev_dbg(adapter->dev, "cmd: Disabled auto deep sleep\n");
1392 }
1393 if (ps_bitmap & BITMAP_STA_PS) {
1394 dev_dbg(adapter->dev, "cmd: Disabled STA power save\n");
1395 if (adapter->sleep_period.period) {
1396 adapter->delay_null_pkt = false;
1397 adapter->tx_lock_flag = false;
1398 adapter->pps_uapsd_mode = false;
1399 }
1400 }
1401 } else if (action == GET_PS) {
1402 if (ps_bitmap & BITMAP_STA_PS)
1403 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
1404 else
1405 adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_CAM;
1406
1407 dev_dbg(adapter->dev, "cmd: ps_bitmap=%#x\n", ps_bitmap);
1408
1409 if (pm_cfg) {
1410 /* This section is for get power save mode */
1411 if (ps_bitmap & BITMAP_STA_PS)
1412 pm_cfg->param.ps_mode = 1;
1413 else
1414 pm_cfg->param.ps_mode = 0;
1415 }
1416 }
1417 return 0;
1418 }
1419
1420 /*
1421 * This function prepares command to get hardware specifications.
1422 *
1423 * Preparation includes -
1424 * - Setting command ID, action and proper size
1425 * - Setting permanent address parameter
1426 * - Ensuring correct endian-ness
1427 */
1428 int mwifiex_cmd_get_hw_spec(struct mwifiex_private *priv,
1429 struct host_cmd_ds_command *cmd)
1430 {
1431 struct host_cmd_ds_get_hw_spec *hw_spec = &cmd->params.hw_spec;
1432
1433 cmd->command = cpu_to_le16(HostCmd_CMD_GET_HW_SPEC);
1434 cmd->size =
1435 cpu_to_le16(sizeof(struct host_cmd_ds_get_hw_spec) + S_DS_GEN);
1436 memcpy(hw_spec->permanent_addr, priv->curr_addr, ETH_ALEN);
1437
1438 return 0;
1439 }
1440
1441 /*
1442 * This function handles the command response of get hardware
1443 * specifications.
1444 *
1445 * Handling includes changing the header fields into CPU format
1446 * and saving/updating the following parameters in driver -
1447 * - Firmware capability information
1448 * - Firmware band settings
1449 * - Ad-hoc start band and channel
1450 * - Ad-hoc 11n activation status
1451 * - Firmware release number
1452 * - Number of antennas
1453 * - Hardware address
1454 * - Hardware interface version
1455 * - Firmware version
1456 * - Region code
1457 * - 11n capabilities
1458 * - MCS support fields
1459 * - MP end port
1460 */
1461 int mwifiex_ret_get_hw_spec(struct mwifiex_private *priv,
1462 struct host_cmd_ds_command *resp)
1463 {
1464 struct host_cmd_ds_get_hw_spec *hw_spec = &resp->params.hw_spec;
1465 struct mwifiex_adapter *adapter = priv->adapter;
1466 struct mwifiex_ie_types_header *tlv;
1467 struct hw_spec_fw_api_rev *api_rev;
1468 u16 resp_size, api_id;
1469 int i, left_len, parsed_len = 0;
1470
1471 adapter->fw_cap_info = le32_to_cpu(hw_spec->fw_cap_info);
1472
1473 if (IS_SUPPORT_MULTI_BANDS(adapter))
1474 adapter->fw_bands = (u8) GET_FW_DEFAULT_BANDS(adapter);
1475 else
1476 adapter->fw_bands = BAND_B;
1477
1478 adapter->config_bands = adapter->fw_bands;
1479
1480 if (adapter->fw_bands & BAND_A) {
1481 if (adapter->fw_bands & BAND_GN) {
1482 adapter->config_bands |= BAND_AN;
1483 adapter->fw_bands |= BAND_AN;
1484 }
1485 if (adapter->fw_bands & BAND_AN) {
1486 adapter->adhoc_start_band = BAND_A | BAND_AN;
1487 adapter->adhoc_11n_enabled = true;
1488 } else {
1489 adapter->adhoc_start_band = BAND_A;
1490 }
1491 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL_A;
1492 } else if (adapter->fw_bands & BAND_GN) {
1493 adapter->adhoc_start_band = BAND_G | BAND_B | BAND_GN;
1494 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1495 adapter->adhoc_11n_enabled = true;
1496 } else if (adapter->fw_bands & BAND_G) {
1497 adapter->adhoc_start_band = BAND_G | BAND_B;
1498 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1499 } else if (adapter->fw_bands & BAND_B) {
1500 adapter->adhoc_start_band = BAND_B;
1501 priv->adhoc_channel = DEFAULT_AD_HOC_CHANNEL;
1502 }
1503
1504 adapter->fw_release_number = le32_to_cpu(hw_spec->fw_release_number);
1505 adapter->number_of_antenna = le16_to_cpu(hw_spec->number_of_antenna);
1506
1507 if (le32_to_cpu(hw_spec->dot_11ac_dev_cap)) {
1508 adapter->is_hw_11ac_capable = true;
1509
1510 /* Copy 11AC cap */
1511 adapter->hw_dot_11ac_dev_cap =
1512 le32_to_cpu(hw_spec->dot_11ac_dev_cap);
1513 adapter->usr_dot_11ac_dev_cap_bg = adapter->hw_dot_11ac_dev_cap
1514 & ~MWIFIEX_DEF_11AC_CAP_BF_RESET_MASK;
1515 adapter->usr_dot_11ac_dev_cap_a = adapter->hw_dot_11ac_dev_cap
1516 & ~MWIFIEX_DEF_11AC_CAP_BF_RESET_MASK;
1517
1518 /* Copy 11AC mcs */
1519 adapter->hw_dot_11ac_mcs_support =
1520 le32_to_cpu(hw_spec->dot_11ac_mcs_support);
1521 adapter->usr_dot_11ac_mcs_support =
1522 adapter->hw_dot_11ac_mcs_support;
1523 } else {
1524 adapter->is_hw_11ac_capable = false;
1525 }
1526
1527 resp_size = le16_to_cpu(resp->size) - S_DS_GEN;
1528 if (resp_size > sizeof(struct host_cmd_ds_get_hw_spec)) {
1529 /* we have variable HW SPEC information */
1530 left_len = resp_size - sizeof(struct host_cmd_ds_get_hw_spec);
1531 while (left_len > sizeof(struct mwifiex_ie_types_header)) {
1532 tlv = (void *)&hw_spec->tlvs + parsed_len;
1533 switch (le16_to_cpu(tlv->type)) {
1534 case TLV_TYPE_FW_API_REV:
1535 api_rev = (struct hw_spec_fw_api_rev *)tlv;
1536 api_id = le16_to_cpu(api_rev->api_id);
1537 switch (api_id) {
1538 case KEY_API_VER_ID:
1539 adapter->fw_key_api_major_ver =
1540 api_rev->major_ver;
1541 adapter->fw_key_api_minor_ver =
1542 api_rev->minor_ver;
1543 dev_dbg(adapter->dev,
1544 "fw_key_api v%d.%d\n",
1545 adapter->fw_key_api_major_ver,
1546 adapter->fw_key_api_minor_ver);
1547 break;
1548 default:
1549 dev_warn(adapter->dev,
1550 "Unknown FW api_id: %d\n",
1551 api_id);
1552 break;
1553 }
1554 break;
1555 default:
1556 dev_warn(adapter->dev,
1557 "Unknown GET_HW_SPEC TLV type: %#x\n",
1558 le16_to_cpu(tlv->type));
1559 break;
1560 }
1561 parsed_len += le16_to_cpu(tlv->len) +
1562 sizeof(struct mwifiex_ie_types_header);
1563 left_len -= parsed_len;
1564 }
1565 }
1566
1567 dev_dbg(adapter->dev, "info: GET_HW_SPEC: fw_release_number- %#x\n",
1568 adapter->fw_release_number);
1569 dev_dbg(adapter->dev, "info: GET_HW_SPEC: permanent addr: %pM\n",
1570 hw_spec->permanent_addr);
1571 dev_dbg(adapter->dev,
1572 "info: GET_HW_SPEC: hw_if_version=%#x version=%#x\n",
1573 le16_to_cpu(hw_spec->hw_if_version),
1574 le16_to_cpu(hw_spec->version));
1575
1576 if (priv->curr_addr[0] == 0xff)
1577 memmove(priv->curr_addr, hw_spec->permanent_addr, ETH_ALEN);
1578
1579 adapter->region_code = le16_to_cpu(hw_spec->region_code);
1580
1581 for (i = 0; i < MWIFIEX_MAX_REGION_CODE; i++)
1582 /* Use the region code to search for the index */
1583 if (adapter->region_code == region_code_index[i])
1584 break;
1585
1586 /* If it's unidentified region code, use the default (USA) */
1587 if (i >= MWIFIEX_MAX_REGION_CODE) {
1588 adapter->region_code = 0x10;
1589 dev_dbg(adapter->dev,
1590 "cmd: unknown region code, use default (USA)\n");
1591 }
1592
1593 adapter->hw_dot_11n_dev_cap = le32_to_cpu(hw_spec->dot_11n_dev_cap);
1594 adapter->hw_dev_mcs_support = hw_spec->dev_mcs_support;
1595
1596 if (adapter->if_ops.update_mp_end_port)
1597 adapter->if_ops.update_mp_end_port(adapter,
1598 le16_to_cpu(hw_spec->mp_end_port));
1599
1600 return 0;
1601 }
This page took 0.062258 seconds and 4 git commands to generate.