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