ath9k: add support for reporting tx power to mac80211
[deliverable/linux.git] / drivers / net / wireless / mwifiex / main.c
CommitLineData
5e6e3a92
BZ
1/*
2 * Marvell Wireless LAN device driver: major functions
3 *
65da33f5 4 * Copyright (C) 2011-2014, Marvell International Ltd.
5e6e3a92
BZ
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 "main.h"
21#include "wmm.h"
22#include "cfg80211.h"
23#include "11n.h"
24
25#define VERSION "1.0"
26
27const char driver_version[] = "mwifiex " VERSION " (%s) ";
388ec385
AK
28static char *cal_data_cfg;
29module_param(cal_data_cfg, charp, 0);
5e6e3a92 30
5e6e3a92
BZ
31/*
32 * This function registers the device and performs all the necessary
33 * initializations.
34 *
35 * The following initialization operations are performed -
36 * - Allocate adapter structure
37 * - Save interface specific operations table in adapter
38 * - Call interface specific initialization routine
39 * - Allocate private structures
40 * - Set default adapter structure parameters
41 * - Initialize locks
42 *
43 * In case of any errors during inittialization, this function also ensures
44 * proper cleanup before exiting.
45 */
46static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
287546df 47 void **padapter)
5e6e3a92 48{
2be7859f
AK
49 struct mwifiex_adapter *adapter;
50 int i;
5e6e3a92
BZ
51
52 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
5e6e3a92 53 if (!adapter)
b53575ec 54 return -ENOMEM;
5e6e3a92 55
287546df 56 *padapter = adapter;
5e6e3a92
BZ
57 adapter->card = card;
58
59 /* Save interface specific operations in adapter */
60 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
61
62 /* card specific initialization has been deferred until now .. */
4daffe35
AK
63 if (adapter->if_ops.init_if)
64 if (adapter->if_ops.init_if(adapter))
65 goto error;
5e6e3a92
BZ
66
67 adapter->priv_num = 0;
5e6e3a92 68
64b05e2f
AP
69 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
70 /* Allocate memory for private structure */
71 adapter->priv[i] =
72 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
73 if (!adapter->priv[i])
74 goto error;
93a1df48 75
64b05e2f 76 adapter->priv[i]->adapter = adapter;
64b05e2f
AP
77 adapter->priv_num++;
78 }
44b815c6 79 mwifiex_init_lock_list(adapter);
5e6e3a92
BZ
80
81 init_timer(&adapter->cmd_timer);
82 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
83 adapter->cmd_timer.data = (unsigned long) adapter;
84
5e6e3a92
BZ
85 return 0;
86
87error:
88 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
89
93a1df48 90 for (i = 0; i < adapter->priv_num; i++)
5e6e3a92 91 kfree(adapter->priv[i]);
93a1df48 92
5e6e3a92
BZ
93 kfree(adapter);
94
95 return -1;
96}
97
98/*
99 * This function unregisters the device and performs all the necessary
100 * cleanups.
101 *
102 * The following cleanup operations are performed -
103 * - Free the timers
104 * - Free beacon buffers
105 * - Free private structures
106 * - Free adapter structure
107 */
108static int mwifiex_unregister(struct mwifiex_adapter *adapter)
109{
270e58e8 110 s32 i;
5e6e3a92 111
4cfda67d
AK
112 if (adapter->if_ops.cleanup_if)
113 adapter->if_ops.cleanup_if(adapter);
114
629873f2 115 del_timer_sync(&adapter->cmd_timer);
5e6e3a92
BZ
116
117 /* Free private structures */
118 for (i = 0; i < adapter->priv_num; i++) {
119 if (adapter->priv[i]) {
120 mwifiex_free_curr_bcn(adapter->priv[i]);
121 kfree(adapter->priv[i]);
122 }
123 }
124
bf354433 125 vfree(adapter->chan_stats);
5e6e3a92
BZ
126 kfree(adapter);
127 return 0;
128}
129
6e251174
AP
130static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
131{
132 unsigned long flags;
133 struct sk_buff *skb;
6e251174
AP
134
135 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
136 if (adapter->rx_processing || adapter->rx_locked) {
137 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
138 goto exit_rx_proc;
139 } else {
140 adapter->rx_processing = true;
141 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
142 }
143
144 /* Check for Rx data */
145 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
146 atomic_dec(&adapter->rx_pending);
147 if (adapter->delay_main_work &&
b43a0d9d 148 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
6e251174 149 adapter->delay_main_work = false;
b43a0d9d 150 queue_work(adapter->workqueue, &adapter->main_work);
6e251174
AP
151 }
152 mwifiex_handle_rx_packet(adapter, skb);
153 }
154 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
155 adapter->rx_processing = false;
156 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
157
6e251174
AP
158exit_rx_proc:
159 return 0;
160}
161
5e6e3a92
BZ
162/*
163 * The main process.
164 *
165 * This function is the main procedure of the driver and handles various driver
166 * operations. It runs in a loop and provides the core functionalities.
167 *
168 * The main responsibilities of this function are -
169 * - Ensure concurrency control
170 * - Handle pending interrupts and call interrupt handlers
171 * - Wake up the card if required
172 * - Handle command responses and call response handlers
173 * - Handle events and call event handlers
174 * - Execute pending commands
175 * - Transmit pending data packets
176 */
177int mwifiex_main_process(struct mwifiex_adapter *adapter)
178{
179 int ret = 0;
180 unsigned long flags;
4daffe35 181 struct sk_buff *skb;
5e6e3a92
BZ
182
183 spin_lock_irqsave(&adapter->main_proc_lock, flags);
184
185 /* Check if already processing */
186 if (adapter->mwifiex_processing) {
187 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
188 goto exit_main_proc;
189 } else {
190 adapter->mwifiex_processing = true;
191 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
192 }
193process_start:
194 do {
195 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
196 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
197 break;
198
6e251174
AP
199 /* If we process interrupts first, it would increase RX pending
200 * even further. Avoid this by checking if rx_pending has
201 * crossed high threshold and schedule rx work queue
202 * and then process interrupts
203 */
204 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING) {
205 adapter->delay_main_work = true;
206 if (!adapter->rx_processing)
207 queue_work(adapter->rx_workqueue,
208 &adapter->rx_work);
209 break;
210 }
211
5e6e3a92
BZ
212 /* Handle pending interrupt if any */
213 if (adapter->int_status) {
214 if (adapter->hs_activated)
215 mwifiex_process_hs_config(adapter);
4daffe35
AK
216 if (adapter->if_ops.process_int_status)
217 adapter->if_ops.process_int_status(adapter);
5e6e3a92
BZ
218 }
219
6e251174
AP
220 if (adapter->rx_work_enabled && adapter->data_received)
221 queue_work(adapter->rx_workqueue, &adapter->rx_work);
222
5e6e3a92
BZ
223 /* Need to wake up the card ? */
224 if ((adapter->ps_state == PS_STATE_SLEEP) &&
225 (adapter->pm_wakeup_card_req &&
226 !adapter->pm_wakeup_fw_try) &&
f57c1edc
YAP
227 (is_command_pending(adapter) ||
228 !mwifiex_wmm_lists_empty(adapter))) {
5e6e3a92
BZ
229 adapter->pm_wakeup_fw_try = true;
230 adapter->if_ops.wakeup(adapter);
231 continue;
232 }
4daffe35 233
5e6e3a92 234 if (IS_CARD_RX_RCVD(adapter)) {
6e251174 235 adapter->data_received = false;
5e6e3a92
BZ
236 adapter->pm_wakeup_fw_try = false;
237 if (adapter->ps_state == PS_STATE_SLEEP)
238 adapter->ps_state = PS_STATE_AWAKE;
239 } else {
240 /* We have tried to wakeup the card already */
241 if (adapter->pm_wakeup_fw_try)
242 break;
243 if (adapter->ps_state != PS_STATE_AWAKE ||
244 adapter->tx_lock_flag)
245 break;
246
5ec39efa 247 if ((!adapter->scan_chan_gap_enabled &&
5ec39efa 248 adapter->scan_processing) || adapter->data_sent ||
f57c1edc
YAP
249 mwifiex_wmm_lists_empty(adapter)) {
250 if (adapter->cmd_sent || adapter->curr_cmd ||
251 (!is_command_pending(adapter)))
5e6e3a92
BZ
252 break;
253 }
254 }
255
4daffe35
AK
256 /* Check Rx data for USB */
257 if (adapter->iface_type == MWIFIEX_USB)
258 while ((skb = skb_dequeue(&adapter->usb_rx_data_q)))
259 mwifiex_handle_rx_packet(adapter, skb);
260
20474129
AK
261 /* Check for event */
262 if (adapter->event_received) {
263 adapter->event_received = false;
264 mwifiex_process_event(adapter);
265 }
266
5e6e3a92
BZ
267 /* Check for Cmd Resp */
268 if (adapter->cmd_resp_received) {
269 adapter->cmd_resp_received = false;
270 mwifiex_process_cmdresp(adapter);
271
272 /* call mwifiex back when init_fw is done */
273 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
274 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
275 mwifiex_init_fw_complete(adapter);
276 }
277 }
278
5e6e3a92
BZ
279 /* Check if we need to confirm Sleep Request
280 received previously */
281 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
282 if (!adapter->cmd_sent && !adapter->curr_cmd)
283 mwifiex_check_ps_cond(adapter);
284 }
285
286 /* * The ps_state may have been changed during processing of
287 * Sleep Request event.
288 */
f57c1edc
YAP
289 if ((adapter->ps_state == PS_STATE_SLEEP) ||
290 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
291 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
292 adapter->tx_lock_flag)
5e6e3a92
BZ
293 continue;
294
295 if (!adapter->cmd_sent && !adapter->curr_cmd) {
296 if (mwifiex_exec_next_cmd(adapter) == -1) {
297 ret = -1;
298 break;
299 }
300 }
301
5ec39efa 302 if ((adapter->scan_chan_gap_enabled ||
d8d91253 303 !adapter->scan_processing) &&
3249ba73 304 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
5e6e3a92
BZ
305 mwifiex_wmm_process_tx(adapter);
306 if (adapter->hs_activated) {
307 adapter->is_hs_configured = false;
308 mwifiex_hs_activated_event
309 (mwifiex_get_priv
310 (adapter, MWIFIEX_BSS_ROLE_ANY),
311 false);
312 }
313 }
314
315 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
f57c1edc
YAP
316 !adapter->curr_cmd && !is_command_pending(adapter) &&
317 mwifiex_wmm_lists_empty(adapter)) {
5e6e3a92
BZ
318 if (!mwifiex_send_null_packet
319 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
320 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
321 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
322 adapter->delay_null_pkt = false;
323 adapter->ps_state = PS_STATE_SLEEP;
324 }
325 break;
326 }
327 } while (true);
328
453b0c3f 329 spin_lock_irqsave(&adapter->main_proc_lock, flags);
f73e557b
AP
330 if (!adapter->delay_main_work &&
331 (adapter->int_status || IS_CARD_RX_RCVD(adapter))) {
453b0c3f 332 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
5e6e3a92 333 goto process_start;
453b0c3f 334 }
5e6e3a92 335
5e6e3a92
BZ
336 adapter->mwifiex_processing = false;
337 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
338
339exit_main_proc:
340 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
341 mwifiex_shutdown_drv(adapter);
342 return ret;
343}
601216e1 344EXPORT_SYMBOL_GPL(mwifiex_main_process);
5e6e3a92 345
5e6e3a92
BZ
346/*
347 * This function frees the adapter structure.
348 *
349 * Additionally, this closes the netlink socket, frees the timers
350 * and private structures.
351 */
352static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
353{
354 if (!adapter) {
355 pr_err("%s: adapter is NULL\n", __func__);
356 return;
357 }
358
359 mwifiex_unregister(adapter);
360 pr_debug("info: %s: free adapter\n", __func__);
361}
362
6b41f941
AK
363/*
364 * This function cancels all works in the queue and destroys
365 * the main workqueue.
366 */
367static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
368{
369 flush_workqueue(adapter->workqueue);
370 destroy_workqueue(adapter->workqueue);
371 adapter->workqueue = NULL;
6e251174
AP
372
373 if (adapter->rx_workqueue) {
374 flush_workqueue(adapter->rx_workqueue);
375 destroy_workqueue(adapter->rx_workqueue);
376 adapter->rx_workqueue = NULL;
377 }
6b41f941
AK
378}
379
5e6e3a92 380/*
59a4cc25 381 * This function gets firmware and initializes it.
5e6e3a92
BZ
382 *
383 * The main initialization steps followed are -
384 * - Download the correct firmware to card
5e6e3a92
BZ
385 * - Issue the init commands to firmware
386 */
59a4cc25 387static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
5e6e3a92 388{
bec568ff 389 int ret;
59a4cc25
AK
390 char fmt[64];
391 struct mwifiex_private *priv;
392 struct mwifiex_adapter *adapter = context;
5e6e3a92 393 struct mwifiex_fw_image fw;
c3afd99f
AK
394 struct semaphore *sem = adapter->card_sem;
395 bool init_failed = false;
68b76e99 396 struct wireless_dev *wdev;
5e6e3a92 397
59a4cc25
AK
398 if (!firmware) {
399 dev_err(adapter->dev,
400 "Failed to get firmware %s\n", adapter->fw_name);
6b41f941 401 goto err_dnld_fw;
5e6e3a92 402 }
59a4cc25
AK
403
404 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
405 adapter->firmware = firmware;
5e6e3a92
BZ
406 fw.fw_buf = (u8 *) adapter->firmware->data;
407 fw.fw_len = adapter->firmware->size;
408
4daffe35
AK
409 if (adapter->if_ops.dnld_fw)
410 ret = adapter->if_ops.dnld_fw(adapter, &fw);
411 else
412 ret = mwifiex_dnld_fw(adapter, &fw);
5e6e3a92 413 if (ret == -1)
6b41f941 414 goto err_dnld_fw;
5e6e3a92
BZ
415
416 dev_notice(adapter->dev, "WLAN FW is active\n");
417
388ec385
AK
418 if (cal_data_cfg) {
419 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
420 adapter->dev)) < 0)
421 dev_err(adapter->dev,
422 "Cal data request_firmware() failed\n");
423 }
424
232fde06 425 /* enable host interrupt after fw dnld is successful */
6b41f941
AK
426 if (adapter->if_ops.enable_int) {
427 if (adapter->if_ops.enable_int(adapter))
428 goto err_dnld_fw;
429 }
232fde06 430
5e6e3a92
BZ
431 adapter->init_wait_q_woken = false;
432 ret = mwifiex_init_fw(adapter);
433 if (ret == -1) {
6b41f941 434 goto err_init_fw;
5e6e3a92
BZ
435 } else if (!ret) {
436 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
437 goto done;
438 }
439 /* Wait for mwifiex_init to complete */
440 wait_event_interruptible(adapter->init_wait_q,
441 adapter->init_wait_q_woken);
59a4cc25 442 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
6b41f941 443 goto err_init_fw;
59a4cc25 444
d6bffe8b
AP
445 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
446 if (mwifiex_register_cfg80211(adapter)) {
59a4cc25 447 dev_err(adapter->dev, "cannot register with cfg80211\n");
d1af2943 448 goto err_init_fw;
59a4cc25
AK
449 }
450
bf354433
AP
451 if (mwifiex_init_channel_scan_gap(adapter)) {
452 dev_err(adapter->dev, "could not init channel stats table\n");
453 goto err_init_fw;
454 }
455
59a4cc25
AK
456 rtnl_lock();
457 /* Create station interface by default */
68b76e99
AK
458 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
459 NL80211_IFTYPE_STATION, NULL, NULL);
460 if (IS_ERR(wdev)) {
59a4cc25 461 dev_err(adapter->dev, "cannot create default STA interface\n");
bec568ff 462 rtnl_unlock();
59a4cc25 463 goto err_add_intf;
5e6e3a92 464 }
59a4cc25
AK
465 rtnl_unlock();
466
467 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
468 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
469 goto done;
5e6e3a92 470
59a4cc25 471err_add_intf:
6b41f941
AK
472 wiphy_unregister(adapter->wiphy);
473 wiphy_free(adapter->wiphy);
59a4cc25 474err_init_fw:
232fde06
DD
475 if (adapter->if_ops.disable_int)
476 adapter->if_ops.disable_int(adapter);
6b41f941 477err_dnld_fw:
59a4cc25 478 pr_debug("info: %s: unregister device\n", __func__);
6b41f941
AK
479 if (adapter->if_ops.unregister_dev)
480 adapter->if_ops.unregister_dev(adapter);
481
482 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
483 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
484 pr_debug("info: %s: shutdown mwifiex\n", __func__);
485 adapter->init_wait_q_woken = false;
486
487 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
488 wait_event_interruptible(adapter->init_wait_q,
489 adapter->init_wait_q_woken);
490 }
491 adapter->surprise_removed = true;
492 mwifiex_terminate_workqueue(adapter);
c3afd99f 493 init_failed = true;
5e6e3a92 494done:
388ec385
AK
495 if (adapter->cal_data) {
496 release_firmware(adapter->cal_data);
497 adapter->cal_data = NULL;
498 }
c3afd99f
AK
499 if (adapter->firmware) {
500 release_firmware(adapter->firmware);
501 adapter->firmware = NULL;
502 }
c3afd99f
AK
503 if (init_failed)
504 mwifiex_free_adapter(adapter);
505 up(sem);
59a4cc25
AK
506 return;
507}
508
509/*
510 * This function initializes the hardware and gets firmware.
511 */
512static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
513{
514 int ret;
515
59a4cc25
AK
516 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
517 adapter->dev, GFP_KERNEL, adapter,
518 mwifiex_fw_dpc);
519 if (ret < 0)
520 dev_err(adapter->dev,
521 "request_firmware_nowait() returned error %d\n", ret);
5e6e3a92
BZ
522 return ret;
523}
524
5e6e3a92
BZ
525/*
526 * CFG802.11 network device handler for open.
527 *
528 * Starts the data queue.
529 */
530static int
531mwifiex_open(struct net_device *dev)
532{
bbea3bc4 533 netif_tx_start_all_queues(dev);
5e6e3a92
BZ
534 return 0;
535}
536
537/*
538 * CFG802.11 network device handler for close.
539 */
540static int
541mwifiex_close(struct net_device *dev)
542{
f162cac8
AK
543 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
544
545 if (priv->scan_request) {
546 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
547 cfg80211_scan_done(priv->scan_request, 1);
548 priv->scan_request = NULL;
75ab753d 549 priv->scan_aborting = true;
f162cac8
AK
550 }
551
5e6e3a92
BZ
552 return 0;
553}
554
e39faa73
SP
555/*
556 * Add buffer into wmm tx queue and queue work to transmit it.
557 */
558int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
559{
47411a06
AP
560 struct netdev_queue *txq;
561 int index = mwifiex_1d_to_wmm_queue[skb->priority];
562
563 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
564 txq = netdev_get_tx_queue(priv->netdev, index);
565 if (!netif_tx_queue_stopped(txq)) {
566 netif_tx_stop_queue(txq);
567 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
568 }
569 }
570
e39faa73 571 atomic_inc(&priv->adapter->tx_pending);
47411a06 572 mwifiex_wmm_add_buf_txqueue(priv, skb);
e39faa73 573
e39faa73
SP
574 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
575
576 return 0;
577}
578
5e6e3a92
BZ
579/*
580 * CFG802.11 network device handler for data transmission.
581 */
582static int
583mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
584{
585 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
270e58e8 586 struct sk_buff *new_skb;
5e6e3a92
BZ
587 struct mwifiex_txinfo *tx_info;
588
9da9a3b2 589 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
f57c1edc 590 jiffies, priv->bss_type, priv->bss_num);
5e6e3a92
BZ
591
592 if (priv->adapter->surprise_removed) {
b53575ec 593 kfree_skb(skb);
5e6e3a92
BZ
594 priv->stats.tx_dropped++;
595 return 0;
596 }
597 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
598 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
b53575ec 599 kfree_skb(skb);
5e6e3a92
BZ
600 priv->stats.tx_dropped++;
601 return 0;
602 }
603 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
604 dev_dbg(priv->adapter->dev,
605 "data: Tx: insufficient skb headroom %d\n",
f57c1edc 606 skb_headroom(skb));
5e6e3a92
BZ
607 /* Insufficient skb headroom - allocate a new skb */
608 new_skb =
609 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
610 if (unlikely(!new_skb)) {
611 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
b53575ec 612 kfree_skb(skb);
5e6e3a92
BZ
613 priv->stats.tx_dropped++;
614 return 0;
615 }
616 kfree_skb(skb);
617 skb = new_skb;
618 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
f57c1edc 619 skb_headroom(skb));
5e6e3a92
BZ
620 }
621
622 tx_info = MWIFIEX_SKB_TXCB(skb);
d76744a9 623 memset(tx_info, 0, sizeof(*tx_info));
9da9a3b2
YAP
624 tx_info->bss_num = priv->bss_num;
625 tx_info->bss_type = priv->bss_type;
a1ed4849 626 tx_info->pkt_len = skb->len;
47411a06
AP
627
628 /* Record the current time the packet was queued; used to
629 * determine the amount of time the packet was queued in
630 * the driver before it was sent to the firmware.
631 * The delay is then sent along with the packet to the
632 * firmware for aggregate delay calculation for stats and
633 * MSDU lifetime expiry.
634 */
c64800e7 635 __net_timestamp(skb);
5e6e3a92 636
e39faa73 637 mwifiex_queue_tx_pkt(priv, skb);
5e6e3a92
BZ
638
639 return 0;
640}
641
642/*
643 * CFG802.11 network device handler for setting MAC address.
644 */
645static int
646mwifiex_set_mac_address(struct net_device *dev, void *addr)
647{
648 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
a5ffddb7 649 struct sockaddr *hw_addr = addr;
270e58e8 650 int ret;
5e6e3a92
BZ
651
652 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
653
600f5d90 654 /* Send request to firmware */
fa0ecbb9
BZ
655 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
656 HostCmd_ACT_GEN_SET, 0, NULL, true);
600f5d90
AK
657
658 if (!ret)
659 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
660 else
f57c1edc
YAP
661 dev_err(priv->adapter->dev,
662 "set mac address failed: ret=%d\n", ret);
600f5d90 663
5e6e3a92
BZ
664 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
665
600f5d90 666 return ret;
5e6e3a92
BZ
667}
668
669/*
670 * CFG802.11 network device handler for setting multicast list.
671 */
672static void mwifiex_set_multicast_list(struct net_device *dev)
673{
674 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
600f5d90
AK
675 struct mwifiex_multicast_list mcast_list;
676
677 if (dev->flags & IFF_PROMISC) {
678 mcast_list.mode = MWIFIEX_PROMISC_MODE;
679 } else if (dev->flags & IFF_ALLMULTI ||
680 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
681 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
682 } else {
683 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
6390d885
DD
684 mcast_list.num_multicast_addr =
685 mwifiex_copy_mcast_addr(&mcast_list, dev);
600f5d90
AK
686 }
687 mwifiex_request_set_multicast_list(priv, &mcast_list);
5e6e3a92
BZ
688}
689
690/*
691 * CFG802.11 network device handler for transmission timeout.
692 */
693static void
694mwifiex_tx_timeout(struct net_device *dev)
695{
696 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
697
5e6e3a92 698 priv->num_tx_timeout++;
8908c7d5
AN
699 priv->tx_timeout_cnt++;
700 dev_err(priv->adapter->dev,
701 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
702 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
703 mwifiex_set_trans_start(dev);
704
705 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
706 priv->adapter->if_ops.card_reset) {
707 dev_err(priv->adapter->dev,
708 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
709 priv->adapter->if_ops.card_reset(priv->adapter);
710 }
5e6e3a92
BZ
711}
712
713/*
714 * CFG802.11 network device handler for statistics retrieval.
715 */
716static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
717{
718 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
719
720 return &priv->stats;
721}
722
47411a06 723static u16
f663dd9a 724mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
99932d4f 725 void *accel_priv, select_queue_fallback_t fallback)
47411a06 726{
fa9ffc74 727 skb->priority = cfg80211_classify8021d(skb, NULL);
47411a06
AP
728 return mwifiex_1d_to_wmm_queue[skb->priority];
729}
730
5e6e3a92
BZ
731/* Network device handlers */
732static const struct net_device_ops mwifiex_netdev_ops = {
733 .ndo_open = mwifiex_open,
734 .ndo_stop = mwifiex_close,
735 .ndo_start_xmit = mwifiex_hard_start_xmit,
736 .ndo_set_mac_address = mwifiex_set_mac_address,
737 .ndo_tx_timeout = mwifiex_tx_timeout,
738 .ndo_get_stats = mwifiex_get_stats,
afc4b13d 739 .ndo_set_rx_mode = mwifiex_set_multicast_list,
47411a06 740 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
5e6e3a92
BZ
741};
742
743/*
744 * This function initializes the private structure parameters.
745 *
746 * The following wait queues are initialized -
747 * - IOCTL wait queue
748 * - Command wait queue
749 * - Statistics wait queue
750 *
751 * ...and the following default parameters are set -
752 * - Current key index : Set to 0
753 * - Rate index : Set to auto
754 * - Media connected : Set to disconnected
755 * - Adhoc link sensed : Set to false
756 * - Nick name : Set to null
757 * - Number of Tx timeout : Set to 0
758 * - Device address : Set to current address
759 *
760 * In addition, the CFG80211 work queue is also created.
761 */
93a1df48
YAP
762void mwifiex_init_priv_params(struct mwifiex_private *priv,
763 struct net_device *dev)
5e6e3a92
BZ
764{
765 dev->netdev_ops = &mwifiex_netdev_ops;
f16fdc9d 766 dev->destructor = free_netdev;
5e6e3a92 767 /* Initialize private structure */
5e6e3a92
BZ
768 priv->current_key_index = 0;
769 priv->media_connected = false;
770 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
ede98bfa
AP
771 memset(priv->mgmt_ie, 0,
772 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
f31acabe
AP
773 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
774 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
775 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
776 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
5e6e3a92 777 priv->num_tx_timeout = 0;
5e6e3a92
BZ
778 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
779}
780
5e6e3a92
BZ
781/*
782 * This function check if command is pending.
783 */
784int is_command_pending(struct mwifiex_adapter *adapter)
785{
786 unsigned long flags;
787 int is_cmd_pend_q_empty;
788
789 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
790 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
791 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
792
793 return !is_cmd_pend_q_empty;
794}
795
6e251174
AP
796/*
797 * This is the RX work queue function.
798 *
799 * It handles the RX operations.
800 */
801static void mwifiex_rx_work_queue(struct work_struct *work)
802{
803 struct mwifiex_adapter *adapter =
804 container_of(work, struct mwifiex_adapter, rx_work);
805
806 if (adapter->surprise_removed)
807 return;
808 mwifiex_process_rx(adapter);
809}
810
5e6e3a92
BZ
811/*
812 * This is the main work queue function.
813 *
814 * It handles the main process, which in turn handles the complete
815 * driver operations.
816 */
817static void mwifiex_main_work_queue(struct work_struct *work)
818{
819 struct mwifiex_adapter *adapter =
820 container_of(work, struct mwifiex_adapter, main_work);
821
822 if (adapter->surprise_removed)
823 return;
824 mwifiex_main_process(adapter);
825}
826
5e6e3a92
BZ
827/*
828 * This function adds the card.
829 *
830 * This function follows the following major steps to set up the device -
831 * - Initialize software. This includes probing the card, registering
832 * the interface operations table, and allocating/initializing the
833 * adapter structure
834 * - Set up the netlink socket
835 * - Create and start the main work queue
836 * - Register the device
837 * - Initialize firmware and hardware
838 * - Add logical interfaces
839 */
840int
841mwifiex_add_card(void *card, struct semaphore *sem,
d930faee 842 struct mwifiex_if_ops *if_ops, u8 iface_type)
5e6e3a92 843{
2be7859f 844 struct mwifiex_adapter *adapter;
5e6e3a92
BZ
845
846 if (down_interruptible(sem))
847 goto exit_sem_err;
848
93a1df48 849 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
5e6e3a92
BZ
850 pr_err("%s: software init failed\n", __func__);
851 goto err_init_sw;
852 }
853
d930faee 854 adapter->iface_type = iface_type;
6b41f941 855 adapter->card_sem = sem;
d930faee 856
5e6e3a92 857 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
5e6e3a92
BZ
858 adapter->surprise_removed = false;
859 init_waitqueue_head(&adapter->init_wait_q);
860 adapter->is_suspended = false;
861 adapter->hs_activated = false;
862 init_waitqueue_head(&adapter->hs_activate_wait_q);
600f5d90 863 init_waitqueue_head(&adapter->cmd_wait_q.wait);
600f5d90 864 adapter->cmd_wait_q.status = 0;
efaaa8b8 865 adapter->scan_wait_q_woken = false;
5e6e3a92 866
6e251174
AP
867 if (num_possible_cpus() > 1) {
868 adapter->rx_work_enabled = true;
869 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
870 }
871
448a71cc
AK
872 adapter->workqueue =
873 alloc_workqueue("MWIFIEX_WORK_QUEUE",
874 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
5e6e3a92
BZ
875 if (!adapter->workqueue)
876 goto err_kmalloc;
877
878 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
6e251174
AP
879
880 if (adapter->rx_work_enabled) {
881 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
882 WQ_HIGHPRI |
883 WQ_MEM_RECLAIM |
884 WQ_UNBOUND, 1);
885 if (!adapter->rx_workqueue)
886 goto err_kmalloc;
887
888 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
889 }
890
92c2538f
AK
891 if (adapter->if_ops.iface_work)
892 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
5e6e3a92
BZ
893
894 /* Register the device. Fill up the private data structure with relevant
232fde06 895 information from the card. */
5e6e3a92
BZ
896 if (adapter->if_ops.register_dev(adapter)) {
897 pr_err("%s: failed to register mwifiex device\n", __func__);
898 goto err_registerdev;
899 }
900
5e6e3a92
BZ
901 if (mwifiex_init_hw_fw(adapter)) {
902 pr_err("%s: firmware init failed\n", __func__);
903 goto err_init_fw;
904 }
2be7859f 905
5e6e3a92
BZ
906 return 0;
907
5e6e3a92 908err_init_fw:
5e6e3a92 909 pr_debug("info: %s: unregister device\n", __func__);
4daffe35
AK
910 if (adapter->if_ops.unregister_dev)
911 adapter->if_ops.unregister_dev(adapter);
5e6e3a92
BZ
912 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
913 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
914 pr_debug("info: %s: shutdown mwifiex\n", __func__);
915 adapter->init_wait_q_woken = false;
636c4598
YAP
916
917 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
5e6e3a92
BZ
918 wait_event_interruptible(adapter->init_wait_q,
919 adapter->init_wait_q_woken);
920 }
6b41f941
AK
921err_registerdev:
922 adapter->surprise_removed = true;
923 mwifiex_terminate_workqueue(adapter);
924err_kmalloc:
5e6e3a92
BZ
925 mwifiex_free_adapter(adapter);
926
927err_init_sw:
928 up(sem);
929
930exit_sem_err:
931 return -1;
932}
933EXPORT_SYMBOL_GPL(mwifiex_add_card);
934
935/*
936 * This function removes the card.
937 *
938 * This function follows the following major steps to remove the device -
939 * - Stop data traffic
940 * - Shutdown firmware
941 * - Remove the logical interfaces
942 * - Terminate the work queue
943 * - Unregister the device
944 * - Free the adapter structure
945 */
946int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
947{
948 struct mwifiex_private *priv = NULL;
5e6e3a92
BZ
949 int i;
950
951 if (down_interruptible(sem))
952 goto exit_sem_err;
953
954 if (!adapter)
955 goto exit_remove;
956
232fde06
DD
957 /* We can no longer handle interrupts once we start doing the teardown
958 * below. */
959 if (adapter->if_ops.disable_int)
960 adapter->if_ops.disable_int(adapter);
961
5e6e3a92
BZ
962 adapter->surprise_removed = true;
963
964 /* Stop data */
965 for (i = 0; i < adapter->priv_num; i++) {
966 priv = adapter->priv[i];
93a1df48 967 if (priv && priv->netdev) {
47411a06 968 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
5e6e3a92
BZ
969 if (netif_carrier_ok(priv->netdev))
970 netif_carrier_off(priv->netdev);
971 }
972 }
973
974 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
975 adapter->init_wait_q_woken = false;
636c4598
YAP
976
977 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
5e6e3a92
BZ
978 wait_event_interruptible(adapter->init_wait_q,
979 adapter->init_wait_q_woken);
980 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
981 if (atomic_read(&adapter->rx_pending) ||
982 atomic_read(&adapter->tx_pending) ||
600f5d90 983 atomic_read(&adapter->cmd_pending)) {
5e6e3a92 984 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
600f5d90 985 "cmd_pending=%d\n",
5e6e3a92
BZ
986 atomic_read(&adapter->rx_pending),
987 atomic_read(&adapter->tx_pending),
600f5d90 988 atomic_read(&adapter->cmd_pending));
5e6e3a92
BZ
989 }
990
93a1df48
YAP
991 for (i = 0; i < adapter->priv_num; i++) {
992 priv = adapter->priv[i];
993
994 if (!priv)
995 continue;
996
997 rtnl_lock();
2da8cbf8 998 if (priv->wdev && priv->netdev)
84efbb84 999 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
93a1df48
YAP
1000 rtnl_unlock();
1001 }
1002
c2868ade
AK
1003 wiphy_unregister(adapter->wiphy);
1004 wiphy_free(adapter->wiphy);
64b05e2f 1005
5e6e3a92
BZ
1006 mwifiex_terminate_workqueue(adapter);
1007
1008 /* Unregister device */
1009 dev_dbg(adapter->dev, "info: unregister device\n");
4daffe35
AK
1010 if (adapter->if_ops.unregister_dev)
1011 adapter->if_ops.unregister_dev(adapter);
5e6e3a92
BZ
1012 /* Free adapter structure */
1013 dev_dbg(adapter->dev, "info: free adapter\n");
1014 mwifiex_free_adapter(adapter);
1015
1016exit_remove:
1017 up(sem);
1018exit_sem_err:
1019 return 0;
1020}
1021EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1022
1023/*
1024 * This function initializes the module.
1025 *
1026 * The debug FS is also initialized if configured.
1027 */
1028static int
1029mwifiex_init_module(void)
1030{
1031#ifdef CONFIG_DEBUG_FS
1032 mwifiex_debugfs_init();
1033#endif
1034 return 0;
1035}
1036
1037/*
1038 * This function cleans up the module.
1039 *
1040 * The debug FS is removed if available.
1041 */
1042static void
1043mwifiex_cleanup_module(void)
1044{
1045#ifdef CONFIG_DEBUG_FS
1046 mwifiex_debugfs_remove();
1047#endif
1048}
1049
1050module_init(mwifiex_init_module);
1051module_exit(mwifiex_cleanup_module);
1052
1053MODULE_AUTHOR("Marvell International Ltd.");
1054MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1055MODULE_VERSION(VERSION);
1056MODULE_LICENSE("GPL v2");
This page took 0.426905 seconds and 5 git commands to generate.