blob: 2de8a6a846205e2cf8b2252d09a4924c172a839f [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: major functions
3 *
Xinming Hu65da33f2014-06-19 21:38:57 -07004 * Copyright (C) 2011-2014, Marvell International Ltd.
Bing Zhao5e6e3a92011-03-21 18:00:50 -07005 *
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) ";
Amitkumar Karwar388ec382013-05-17 17:50:25 -070028static char *cal_data_cfg;
29module_param(cal_data_cfg, charp, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070030
Avinash Patil0013c7c2014-11-05 19:38:11 +053031static unsigned short driver_mode;
32module_param(driver_mode, ushort, 0);
33MODULE_PARM_DESC(driver_mode,
34 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
35
Bing Zhao5e6e3a92011-03-21 18:00:50 -070036/*
37 * This function registers the device and performs all the necessary
38 * initializations.
39 *
40 * The following initialization operations are performed -
41 * - Allocate adapter structure
42 * - Save interface specific operations table in adapter
43 * - Call interface specific initialization routine
44 * - Allocate private structures
45 * - Set default adapter structure parameters
46 * - Initialize locks
47 *
48 * In case of any errors during inittialization, this function also ensures
49 * proper cleanup before exiting.
50 */
51static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
Amitkumar Karwar287546d2011-06-08 20:39:20 +053052 void **padapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070053{
Amitkumar Karwar2be78592011-04-15 20:50:42 -070054 struct mwifiex_adapter *adapter;
55 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070056
57 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070058 if (!adapter)
Christoph Fritzb53575e2011-05-08 22:50:09 +020059 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070060
Amitkumar Karwar287546d2011-06-08 20:39:20 +053061 *padapter = adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070062 adapter->card = card;
63
64 /* Save interface specific operations in adapter */
65 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
66
67 /* card specific initialization has been deferred until now .. */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070068 if (adapter->if_ops.init_if)
69 if (adapter->if_ops.init_if(adapter))
70 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070071
72 adapter->priv_num = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070073
Avinash Patil64b05e22012-05-08 18:30:13 -070074 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
75 /* Allocate memory for private structure */
76 adapter->priv[i] =
77 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
78 if (!adapter->priv[i])
79 goto error;
80
81 adapter->priv[i]->adapter = adapter;
Avinash Patil64b05e22012-05-08 18:30:13 -070082 adapter->priv_num++;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070083 }
Amitkumar Karwar44b815c2011-09-29 20:43:41 -070084 mwifiex_init_lock_list(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070085
86 init_timer(&adapter->cmd_timer);
87 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
88 adapter->cmd_timer.data = (unsigned long) adapter;
89
Bing Zhao5e6e3a92011-03-21 18:00:50 -070090 return 0;
91
92error:
93 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
94
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -070095 for (i = 0; i < adapter->priv_num; i++)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070096 kfree(adapter->priv[i]);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -070097
Bing Zhao5e6e3a92011-03-21 18:00:50 -070098 kfree(adapter);
99
100 return -1;
101}
102
103/*
104 * This function unregisters the device and performs all the necessary
105 * cleanups.
106 *
107 * The following cleanup operations are performed -
108 * - Free the timers
109 * - Free beacon buffers
110 * - Free private structures
111 * - Free adapter structure
112 */
113static int mwifiex_unregister(struct mwifiex_adapter *adapter)
114{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700115 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700116
Amitkumar Karwar4cfda672013-07-22 19:17:50 -0700117 if (adapter->if_ops.cleanup_if)
118 adapter->if_ops.cleanup_if(adapter);
119
Avinash Patil629873f2014-02-18 15:47:55 -0800120 del_timer_sync(&adapter->cmd_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700121
122 /* Free private structures */
123 for (i = 0; i < adapter->priv_num; i++) {
124 if (adapter->priv[i]) {
125 mwifiex_free_curr_bcn(adapter->priv[i]);
126 kfree(adapter->priv[i]);
127 }
128 }
129
Avinash Patilbf354432014-10-31 16:08:26 +0530130 vfree(adapter->chan_stats);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700131 kfree(adapter);
132 return 0;
133}
134
Avinash Patil6e251172014-09-12 20:08:59 +0530135static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
136{
137 unsigned long flags;
138 struct sk_buff *skb;
Avinash Patil6e251172014-09-12 20:08:59 +0530139
140 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
141 if (adapter->rx_processing || adapter->rx_locked) {
142 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
143 goto exit_rx_proc;
144 } else {
145 adapter->rx_processing = true;
146 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
147 }
148
149 /* Check for Rx data */
150 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
151 atomic_dec(&adapter->rx_pending);
152 if (adapter->delay_main_work &&
Avinash Patilb43a0d92014-09-29 21:44:14 +0530153 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530154 if (adapter->if_ops.submit_rem_rx_urbs)
155 adapter->if_ops.submit_rem_rx_urbs(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530156 adapter->delay_main_work = false;
Avinash Patilb43a0d92014-09-29 21:44:14 +0530157 queue_work(adapter->workqueue, &adapter->main_work);
Avinash Patil6e251172014-09-12 20:08:59 +0530158 }
159 mwifiex_handle_rx_packet(adapter, skb);
160 }
161 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
162 adapter->rx_processing = false;
163 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
164
Avinash Patil6e251172014-09-12 20:08:59 +0530165exit_rx_proc:
166 return 0;
167}
168
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700169/*
170 * The main process.
171 *
172 * This function is the main procedure of the driver and handles various driver
173 * operations. It runs in a loop and provides the core functionalities.
174 *
175 * The main responsibilities of this function are -
176 * - Ensure concurrency control
177 * - Handle pending interrupts and call interrupt handlers
178 * - Wake up the card if required
179 * - Handle command responses and call response handlers
180 * - Handle events and call event handlers
181 * - Execute pending commands
182 * - Transmit pending data packets
183 */
184int mwifiex_main_process(struct mwifiex_adapter *adapter)
185{
186 int ret = 0;
187 unsigned long flags;
188
189 spin_lock_irqsave(&adapter->main_proc_lock, flags);
190
191 /* Check if already processing */
192 if (adapter->mwifiex_processing) {
193 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
194 goto exit_main_proc;
195 } else {
196 adapter->mwifiex_processing = true;
197 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
198 }
199process_start:
200 do {
201 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
202 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
203 break;
204
Avinash Patil6e251172014-09-12 20:08:59 +0530205 /* If we process interrupts first, it would increase RX pending
206 * even further. Avoid this by checking if rx_pending has
207 * crossed high threshold and schedule rx work queue
208 * and then process interrupts
209 */
210 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING) {
211 adapter->delay_main_work = true;
212 if (!adapter->rx_processing)
213 queue_work(adapter->rx_workqueue,
214 &adapter->rx_work);
215 break;
216 }
217
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700218 /* Handle pending interrupt if any */
219 if (adapter->int_status) {
220 if (adapter->hs_activated)
221 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700222 if (adapter->if_ops.process_int_status)
223 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700224 }
225
Avinash Patil6e251172014-09-12 20:08:59 +0530226 if (adapter->rx_work_enabled && adapter->data_received)
227 queue_work(adapter->rx_workqueue, &adapter->rx_work);
228
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700229 /* Need to wake up the card ? */
230 if ((adapter->ps_state == PS_STATE_SLEEP) &&
231 (adapter->pm_wakeup_card_req &&
232 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700233 (is_command_pending(adapter) ||
234 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700235 adapter->pm_wakeup_fw_try = true;
236 adapter->if_ops.wakeup(adapter);
237 continue;
238 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700239
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700240 if (IS_CARD_RX_RCVD(adapter)) {
Avinash Patil6e251172014-09-12 20:08:59 +0530241 adapter->data_received = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700242 adapter->pm_wakeup_fw_try = false;
243 if (adapter->ps_state == PS_STATE_SLEEP)
244 adapter->ps_state = PS_STATE_AWAKE;
245 } else {
246 /* We have tried to wakeup the card already */
247 if (adapter->pm_wakeup_fw_try)
248 break;
249 if (adapter->ps_state != PS_STATE_AWAKE ||
250 adapter->tx_lock_flag)
251 break;
252
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530253 if ((!adapter->scan_chan_gap_enabled &&
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530254 adapter->scan_processing) || adapter->data_sent ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700255 mwifiex_wmm_lists_empty(adapter)) {
256 if (adapter->cmd_sent || adapter->curr_cmd ||
257 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700258 break;
259 }
260 }
261
Amitkumar Karwar20474122014-04-14 15:31:05 -0700262 /* Check for event */
263 if (adapter->event_received) {
264 adapter->event_received = false;
265 mwifiex_process_event(adapter);
266 }
267
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700268 /* Check for Cmd Resp */
269 if (adapter->cmd_resp_received) {
270 adapter->cmd_resp_received = false;
271 mwifiex_process_cmdresp(adapter);
272
273 /* call mwifiex back when init_fw is done */
274 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
275 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
276 mwifiex_init_fw_complete(adapter);
277 }
278 }
279
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700280 /* Check if we need to confirm Sleep Request
281 received previously */
282 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
283 if (!adapter->cmd_sent && !adapter->curr_cmd)
284 mwifiex_check_ps_cond(adapter);
285 }
286
287 /* * The ps_state may have been changed during processing of
288 * Sleep Request event.
289 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700290 if ((adapter->ps_state == PS_STATE_SLEEP) ||
291 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
292 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
293 adapter->tx_lock_flag)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700294 continue;
295
296 if (!adapter->cmd_sent && !adapter->curr_cmd) {
297 if (mwifiex_exec_next_cmd(adapter) == -1) {
298 ret = -1;
299 break;
300 }
301 }
302
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530303 if ((adapter->scan_chan_gap_enabled ||
Amitkumar Karward8d91252014-09-12 20:08:58 +0530304 !adapter->scan_processing) &&
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700305 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700306 mwifiex_wmm_process_tx(adapter);
307 if (adapter->hs_activated) {
308 adapter->is_hs_configured = false;
309 mwifiex_hs_activated_event
310 (mwifiex_get_priv
311 (adapter, MWIFIEX_BSS_ROLE_ANY),
312 false);
313 }
314 }
315
316 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700317 !adapter->curr_cmd && !is_command_pending(adapter) &&
318 mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700319 if (!mwifiex_send_null_packet
320 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
321 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
322 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
323 adapter->delay_null_pkt = false;
324 adapter->ps_state = PS_STATE_SLEEP;
325 }
326 break;
327 }
328 } while (true);
329
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700330 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Avinash Patilf73e5572014-09-29 21:44:13 +0530331 if (!adapter->delay_main_work &&
332 (adapter->int_status || IS_CARD_RX_RCVD(adapter))) {
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700333 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
334 goto process_start;
335 }
336
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700337 adapter->mwifiex_processing = false;
338 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
339
340exit_main_proc:
341 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
342 mwifiex_shutdown_drv(adapter);
343 return ret;
344}
Bing Zhao601216e2012-11-05 16:59:15 -0800345EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700346
347/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700348 * This function frees the adapter structure.
349 *
350 * Additionally, this closes the netlink socket, frees the timers
351 * and private structures.
352 */
353static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
354{
355 if (!adapter) {
356 pr_err("%s: adapter is NULL\n", __func__);
357 return;
358 }
359
360 mwifiex_unregister(adapter);
361 pr_debug("info: %s: free adapter\n", __func__);
362}
363
364/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700365 * This function cancels all works in the queue and destroys
366 * the main workqueue.
367 */
368static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
369{
370 flush_workqueue(adapter->workqueue);
371 destroy_workqueue(adapter->workqueue);
372 adapter->workqueue = NULL;
Avinash Patil6e251172014-09-12 20:08:59 +0530373
374 if (adapter->rx_workqueue) {
375 flush_workqueue(adapter->rx_workqueue);
376 destroy_workqueue(adapter->rx_workqueue);
377 adapter->rx_workqueue = NULL;
378 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700379}
380
381/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700382 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700383 *
384 * The main initialization steps followed are -
385 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700386 * - Issue the init commands to firmware
387 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700388static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700389{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800390 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700391 char fmt[64];
392 struct mwifiex_private *priv;
393 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700394 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700395 struct semaphore *sem = adapter->card_sem;
396 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800397 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700398
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700399 if (!firmware) {
400 dev_err(adapter->dev,
401 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700402 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700403 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700404
405 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
406 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700407 fw.fw_buf = (u8 *) adapter->firmware->data;
408 fw.fw_len = adapter->firmware->size;
409
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700410 if (adapter->if_ops.dnld_fw)
411 ret = adapter->if_ops.dnld_fw(adapter, &fw);
412 else
413 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700414 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700415 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700416
417 dev_notice(adapter->dev, "WLAN FW is active\n");
418
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700419 if (cal_data_cfg) {
420 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
421 adapter->dev)) < 0)
422 dev_err(adapter->dev,
423 "Cal data request_firmware() failed\n");
424 }
425
Daniel Drake232fde02013-07-13 10:57:10 -0400426 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700427 if (adapter->if_ops.enable_int) {
428 if (adapter->if_ops.enable_int(adapter))
429 goto err_dnld_fw;
430 }
Daniel Drake232fde02013-07-13 10:57:10 -0400431
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700432 adapter->init_wait_q_woken = false;
433 ret = mwifiex_init_fw(adapter);
434 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700435 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700436 } else if (!ret) {
437 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
438 goto done;
439 }
440 /* Wait for mwifiex_init to complete */
441 wait_event_interruptible(adapter->init_wait_q,
442 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700443 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700444 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700445
Avinash Patild6bffe82012-05-08 18:30:15 -0700446 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
447 if (mwifiex_register_cfg80211(adapter)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700448 dev_err(adapter->dev, "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800449 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700450 }
451
Avinash Patilbf354432014-10-31 16:08:26 +0530452 if (mwifiex_init_channel_scan_gap(adapter)) {
453 dev_err(adapter->dev, "could not init channel stats table\n");
454 goto err_init_fw;
455 }
456
Avinash Patil0013c7c2014-11-05 19:38:11 +0530457 if (driver_mode) {
458 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
459 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
460 }
461
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700462 rtnl_lock();
463 /* Create station interface by default */
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800464 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
465 NL80211_IFTYPE_STATION, NULL, NULL);
466 if (IS_ERR(wdev)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700467 dev_err(adapter->dev, "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800468 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700469 goto err_add_intf;
470 }
Avinash Patil0013c7c2014-11-05 19:38:11 +0530471
472 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
473 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d",
474 NL80211_IFTYPE_AP, NULL, NULL);
475 if (IS_ERR(wdev)) {
476 dev_err(adapter->dev, "cannot create AP interface\n");
477 rtnl_unlock();
478 goto err_add_intf;
479 }
480 }
481
482 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
483 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d",
484 NL80211_IFTYPE_P2P_CLIENT, NULL,
485 NULL);
486 if (IS_ERR(wdev)) {
487 dev_err(adapter->dev,
488 "cannot create p2p client interface\n");
489 rtnl_unlock();
490 goto err_add_intf;
491 }
492 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700493 rtnl_unlock();
494
495 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
496 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
497 goto done;
498
499err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700500 wiphy_unregister(adapter->wiphy);
501 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700502err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400503 if (adapter->if_ops.disable_int)
504 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700505err_dnld_fw:
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700506 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700507 if (adapter->if_ops.unregister_dev)
508 adapter->if_ops.unregister_dev(adapter);
509
510 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
511 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
512 pr_debug("info: %s: shutdown mwifiex\n", __func__);
513 adapter->init_wait_q_woken = false;
514
515 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
516 wait_event_interruptible(adapter->init_wait_q,
517 adapter->init_wait_q_woken);
518 }
519 adapter->surprise_removed = true;
520 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700521 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700522done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700523 if (adapter->cal_data) {
524 release_firmware(adapter->cal_data);
525 adapter->cal_data = NULL;
526 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700527 if (adapter->firmware) {
528 release_firmware(adapter->firmware);
529 adapter->firmware = NULL;
530 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700531 if (init_failed)
532 mwifiex_free_adapter(adapter);
533 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700534 return;
535}
536
537/*
538 * This function initializes the hardware and gets firmware.
539 */
540static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
541{
542 int ret;
543
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700544 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
545 adapter->dev, GFP_KERNEL, adapter,
546 mwifiex_fw_dpc);
547 if (ret < 0)
548 dev_err(adapter->dev,
549 "request_firmware_nowait() returned error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700550 return ret;
551}
552
553/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700554 * CFG802.11 network device handler for open.
555 *
556 * Starts the data queue.
557 */
558static int
559mwifiex_open(struct net_device *dev)
560{
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800561 netif_tx_start_all_queues(dev);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700562 return 0;
563}
564
565/*
566 * CFG802.11 network device handler for close.
567 */
568static int
569mwifiex_close(struct net_device *dev)
570{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700571 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
572
573 if (priv->scan_request) {
574 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
575 cfg80211_scan_done(priv->scan_request, 1);
576 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700577 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700578 }
579
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700580 return 0;
581}
582
583/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700584 * Add buffer into wmm tx queue and queue work to transmit it.
585 */
586int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
587{
Avinash Patil47411a02012-11-01 18:44:16 -0700588 struct netdev_queue *txq;
589 int index = mwifiex_1d_to_wmm_queue[skb->priority];
590
591 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
592 txq = netdev_get_tx_queue(priv->netdev, index);
593 if (!netif_tx_queue_stopped(txq)) {
594 netif_tx_stop_queue(txq);
595 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
596 }
597 }
598
Stone Piaoe39faa72012-09-25 20:23:32 -0700599 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700600 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700601
Stone Piaoe39faa72012-09-25 20:23:32 -0700602 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
603
604 return 0;
605}
606
607/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700608 * CFG802.11 network device handler for data transmission.
609 */
610static int
611mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
612{
613 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700614 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700615 struct mwifiex_txinfo *tx_info;
616
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800617 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700618 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700619
620 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200621 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700622 priv->stats.tx_dropped++;
623 return 0;
624 }
625 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
626 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200627 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700628 priv->stats.tx_dropped++;
629 return 0;
630 }
631 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
632 dev_dbg(priv->adapter->dev,
633 "data: Tx: insufficient skb headroom %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700634 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700635 /* Insufficient skb headroom - allocate a new skb */
636 new_skb =
637 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
638 if (unlikely(!new_skb)) {
639 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200640 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700641 priv->stats.tx_dropped++;
642 return 0;
643 }
644 kfree_skb(skb);
645 skb = new_skb;
646 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700647 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700648 }
649
650 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700651 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800652 tx_info->bss_num = priv->bss_num;
653 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800654 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700655
656 /* Record the current time the packet was queued; used to
657 * determine the amount of time the packet was queued in
658 * the driver before it was sent to the firmware.
659 * The delay is then sent along with the packet to the
660 * firmware for aggregate delay calculation for stats and
661 * MSDU lifetime expiry.
662 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100663 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700664
Stone Piaoe39faa72012-09-25 20:23:32 -0700665 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700666
667 return 0;
668}
669
670/*
671 * CFG802.11 network device handler for setting MAC address.
672 */
673static int
674mwifiex_set_mac_address(struct net_device *dev, void *addr)
675{
676 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700677 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700678 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700679
680 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
681
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700682 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800683 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
684 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700685
686 if (!ret)
687 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
688 else
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700689 dev_err(priv->adapter->dev,
690 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700691
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700692 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
693
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700694 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700695}
696
697/*
698 * CFG802.11 network device handler for setting multicast list.
699 */
700static void mwifiex_set_multicast_list(struct net_device *dev)
701{
702 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700703 struct mwifiex_multicast_list mcast_list;
704
705 if (dev->flags & IFF_PROMISC) {
706 mcast_list.mode = MWIFIEX_PROMISC_MODE;
707 } else if (dev->flags & IFF_ALLMULTI ||
708 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
709 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
710 } else {
711 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400712 mcast_list.num_multicast_addr =
713 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700714 }
715 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700716}
717
718/*
719 * CFG802.11 network device handler for transmission timeout.
720 */
721static void
722mwifiex_tx_timeout(struct net_device *dev)
723{
724 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
725
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700726 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800727 priv->tx_timeout_cnt++;
728 dev_err(priv->adapter->dev,
729 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
730 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
731 mwifiex_set_trans_start(dev);
732
733 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
734 priv->adapter->if_ops.card_reset) {
735 dev_err(priv->adapter->dev,
736 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
737 priv->adapter->if_ops.card_reset(priv->adapter);
738 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700739}
740
741/*
742 * CFG802.11 network device handler for statistics retrieval.
743 */
744static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
745{
746 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
747
748 return &priv->stats;
749}
750
Avinash Patil47411a02012-11-01 18:44:16 -0700751static u16
Jason Wangf663dd92014-01-10 16:18:26 +0800752mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100753 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -0700754{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800755 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -0700756 return mwifiex_1d_to_wmm_queue[skb->priority];
757}
758
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700759/* Network device handlers */
760static const struct net_device_ops mwifiex_netdev_ops = {
761 .ndo_open = mwifiex_open,
762 .ndo_stop = mwifiex_close,
763 .ndo_start_xmit = mwifiex_hard_start_xmit,
764 .ndo_set_mac_address = mwifiex_set_mac_address,
765 .ndo_tx_timeout = mwifiex_tx_timeout,
766 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000767 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -0700768 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700769};
770
771/*
772 * This function initializes the private structure parameters.
773 *
774 * The following wait queues are initialized -
775 * - IOCTL wait queue
776 * - Command wait queue
777 * - Statistics wait queue
778 *
779 * ...and the following default parameters are set -
780 * - Current key index : Set to 0
781 * - Rate index : Set to auto
782 * - Media connected : Set to disconnected
783 * - Adhoc link sensed : Set to false
784 * - Nick name : Set to null
785 * - Number of Tx timeout : Set to 0
786 * - Device address : Set to current address
787 *
788 * In addition, the CFG80211 work queue is also created.
789 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700790void mwifiex_init_priv_params(struct mwifiex_private *priv,
791 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700792{
793 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -0700794 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700795 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700796 priv->current_key_index = 0;
797 priv->media_connected = false;
798 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
Avinash Patilede98bf2012-05-08 18:30:28 -0700799 memset(priv->mgmt_ie, 0,
800 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700801 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
802 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
803 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
804 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700805 priv->num_tx_timeout = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700806 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
807}
808
809/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700810 * This function check if command is pending.
811 */
812int is_command_pending(struct mwifiex_adapter *adapter)
813{
814 unsigned long flags;
815 int is_cmd_pend_q_empty;
816
817 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
818 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
819 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
820
821 return !is_cmd_pend_q_empty;
822}
823
824/*
Avinash Patil6e251172014-09-12 20:08:59 +0530825 * This is the RX work queue function.
826 *
827 * It handles the RX operations.
828 */
829static void mwifiex_rx_work_queue(struct work_struct *work)
830{
831 struct mwifiex_adapter *adapter =
832 container_of(work, struct mwifiex_adapter, rx_work);
833
834 if (adapter->surprise_removed)
835 return;
836 mwifiex_process_rx(adapter);
837}
838
839/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700840 * This is the main work queue function.
841 *
842 * It handles the main process, which in turn handles the complete
843 * driver operations.
844 */
845static void mwifiex_main_work_queue(struct work_struct *work)
846{
847 struct mwifiex_adapter *adapter =
848 container_of(work, struct mwifiex_adapter, main_work);
849
850 if (adapter->surprise_removed)
851 return;
852 mwifiex_main_process(adapter);
853}
854
855/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700856 * This function adds the card.
857 *
858 * This function follows the following major steps to set up the device -
859 * - Initialize software. This includes probing the card, registering
860 * the interface operations table, and allocating/initializing the
861 * adapter structure
862 * - Set up the netlink socket
863 * - Create and start the main work queue
864 * - Register the device
865 * - Initialize firmware and hardware
866 * - Add logical interfaces
867 */
868int
869mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700870 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700871{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700872 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700873
874 if (down_interruptible(sem))
875 goto exit_sem_err;
876
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700877 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700878 pr_err("%s: software init failed\n", __func__);
879 goto err_init_sw;
880 }
881
Amitkumar Karward930fae2011-10-11 17:41:21 -0700882 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700883 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700884
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700885 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700886 adapter->surprise_removed = false;
887 init_waitqueue_head(&adapter->init_wait_q);
888 adapter->is_suspended = false;
889 adapter->hs_activated = false;
890 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700891 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700892 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700893 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700894
Avinash Patilec4a16b2014-11-05 17:04:27 +0530895 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +0530896 adapter->rx_work_enabled = true;
897 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
898 }
899
Amitkumar Karwar448a71c2013-10-11 18:33:04 -0700900 adapter->workqueue =
901 alloc_workqueue("MWIFIEX_WORK_QUEUE",
902 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700903 if (!adapter->workqueue)
904 goto err_kmalloc;
905
906 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Avinash Patil6e251172014-09-12 20:08:59 +0530907
908 if (adapter->rx_work_enabled) {
909 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
910 WQ_HIGHPRI |
911 WQ_MEM_RECLAIM |
912 WQ_UNBOUND, 1);
913 if (!adapter->rx_workqueue)
914 goto err_kmalloc;
915
916 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
917 }
918
Amitkumar Karwar92c25382014-06-19 21:38:52 -0700919 if (adapter->if_ops.iface_work)
920 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700921
922 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -0400923 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700924 if (adapter->if_ops.register_dev(adapter)) {
925 pr_err("%s: failed to register mwifiex device\n", __func__);
926 goto err_registerdev;
927 }
928
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700929 if (mwifiex_init_hw_fw(adapter)) {
930 pr_err("%s: firmware init failed\n", __func__);
931 goto err_init_fw;
932 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700933
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700934 return 0;
935
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700936err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700937 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700938 if (adapter->if_ops.unregister_dev)
939 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700940 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
941 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
942 pr_debug("info: %s: shutdown mwifiex\n", __func__);
943 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700944
945 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700946 wait_event_interruptible(adapter->init_wait_q,
947 adapter->init_wait_q_woken);
948 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700949err_registerdev:
950 adapter->surprise_removed = true;
951 mwifiex_terminate_workqueue(adapter);
952err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700953 mwifiex_free_adapter(adapter);
954
955err_init_sw:
956 up(sem);
957
958exit_sem_err:
959 return -1;
960}
961EXPORT_SYMBOL_GPL(mwifiex_add_card);
962
963/*
964 * This function removes the card.
965 *
966 * This function follows the following major steps to remove the device -
967 * - Stop data traffic
968 * - Shutdown firmware
969 * - Remove the logical interfaces
970 * - Terminate the work queue
971 * - Unregister the device
972 * - Free the adapter structure
973 */
974int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
975{
976 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700977 int i;
978
979 if (down_interruptible(sem))
980 goto exit_sem_err;
981
982 if (!adapter)
983 goto exit_remove;
984
Daniel Drake232fde02013-07-13 10:57:10 -0400985 /* We can no longer handle interrupts once we start doing the teardown
986 * below. */
987 if (adapter->if_ops.disable_int)
988 adapter->if_ops.disable_int(adapter);
989
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700990 adapter->surprise_removed = true;
991
992 /* Stop data */
993 for (i = 0; i < adapter->priv_num; i++) {
994 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700995 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -0700996 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700997 if (netif_carrier_ok(priv->netdev))
998 netif_carrier_off(priv->netdev);
999 }
1000 }
1001
1002 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
1003 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001004
1005 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001006 wait_event_interruptible(adapter->init_wait_q,
1007 adapter->init_wait_q_woken);
1008 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
1009 if (atomic_read(&adapter->rx_pending) ||
1010 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001011 atomic_read(&adapter->cmd_pending)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001012 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001013 "cmd_pending=%d\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001014 atomic_read(&adapter->rx_pending),
1015 atomic_read(&adapter->tx_pending),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001016 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001017 }
1018
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001019 for (i = 0; i < adapter->priv_num; i++) {
1020 priv = adapter->priv[i];
1021
1022 if (!priv)
1023 continue;
1024
1025 rtnl_lock();
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -08001026 if (priv->wdev && priv->netdev)
Johannes Berg84efbb82012-06-16 00:00:26 +02001027 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001028 rtnl_unlock();
1029 }
1030
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -08001031 wiphy_unregister(adapter->wiphy);
1032 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -07001033
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001034 mwifiex_terminate_workqueue(adapter);
1035
1036 /* Unregister device */
1037 dev_dbg(adapter->dev, "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001038 if (adapter->if_ops.unregister_dev)
1039 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001040 /* Free adapter structure */
1041 dev_dbg(adapter->dev, "info: free adapter\n");
1042 mwifiex_free_adapter(adapter);
1043
1044exit_remove:
1045 up(sem);
1046exit_sem_err:
1047 return 0;
1048}
1049EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1050
1051/*
1052 * This function initializes the module.
1053 *
1054 * The debug FS is also initialized if configured.
1055 */
1056static int
1057mwifiex_init_module(void)
1058{
1059#ifdef CONFIG_DEBUG_FS
1060 mwifiex_debugfs_init();
1061#endif
1062 return 0;
1063}
1064
1065/*
1066 * This function cleans up the module.
1067 *
1068 * The debug FS is removed if available.
1069 */
1070static void
1071mwifiex_cleanup_module(void)
1072{
1073#ifdef CONFIG_DEBUG_FS
1074 mwifiex_debugfs_remove();
1075#endif
1076}
1077
1078module_init(mwifiex_init_module);
1079module_exit(mwifiex_cleanup_module);
1080
1081MODULE_AUTHOR("Marvell International Ltd.");
1082MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1083MODULE_VERSION(VERSION);
1084MODULE_LICENSE("GPL v2");