blob: 2a2e5dbab8ddd5a58ffad048595cb0eeb2e568de [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
Zhaoyang Liuc687a002015-05-12 00:48:18 +053027static unsigned int debug_mask = MWIFIEX_DEFAULT_DEBUG_MASK;
28module_param(debug_mask, uint, 0);
29MODULE_PARM_DESC(debug_mask, "bitmap for debug flags");
30
Bing Zhao5e6e3a92011-03-21 18:00:50 -070031const char driver_version[] = "mwifiex " VERSION " (%s) ";
Amitkumar Karwar388ec382013-05-17 17:50:25 -070032static char *cal_data_cfg;
33module_param(cal_data_cfg, charp, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070034
Avinash Patil0013c7c2014-11-05 19:38:11 +053035static unsigned short driver_mode;
36module_param(driver_mode, ushort, 0);
37MODULE_PARM_DESC(driver_mode,
38 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
39
Bing Zhao5e6e3a92011-03-21 18:00:50 -070040/*
41 * This function registers the device and performs all the necessary
42 * initializations.
43 *
44 * The following initialization operations are performed -
45 * - Allocate adapter structure
46 * - Save interface specific operations table in adapter
47 * - Call interface specific initialization routine
48 * - Allocate private structures
49 * - Set default adapter structure parameters
50 * - Initialize locks
51 *
52 * In case of any errors during inittialization, this function also ensures
53 * proper cleanup before exiting.
54 */
55static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
Amitkumar Karwar287546d2011-06-08 20:39:20 +053056 void **padapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070057{
Amitkumar Karwar2be78592011-04-15 20:50:42 -070058 struct mwifiex_adapter *adapter;
59 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070060
61 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070062 if (!adapter)
Christoph Fritzb53575e2011-05-08 22:50:09 +020063 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070064
Amitkumar Karwar287546d2011-06-08 20:39:20 +053065 *padapter = adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070066 adapter->card = card;
67
68 /* Save interface specific operations in adapter */
69 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
Zhaoyang Liuc687a002015-05-12 00:48:18 +053070 adapter->debug_mask = debug_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070071
72 /* card specific initialization has been deferred until now .. */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070073 if (adapter->if_ops.init_if)
74 if (adapter->if_ops.init_if(adapter))
75 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070076
77 adapter->priv_num = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070078
Avinash Patil64b05e22012-05-08 18:30:13 -070079 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
80 /* Allocate memory for private structure */
81 adapter->priv[i] =
82 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
83 if (!adapter->priv[i])
84 goto error;
85
86 adapter->priv[i]->adapter = adapter;
Avinash Patil64b05e22012-05-08 18:30:13 -070087 adapter->priv_num++;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070088 }
Amitkumar Karwar44b815c2011-09-29 20:43:41 -070089 mwifiex_init_lock_list(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070090
Julia Lawallc6c33e72014-12-26 15:35:56 +010091 setup_timer(&adapter->cmd_timer, mwifiex_cmd_timeout_func,
92 (unsigned long)adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070093
Bing Zhao5e6e3a92011-03-21 18:00:50 -070094 return 0;
95
96error:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +053097 mwifiex_dbg(adapter, ERROR,
98 "info: leave mwifiex_register with error\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -070099
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700100 for (i = 0; i < adapter->priv_num; i++)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700101 kfree(adapter->priv[i]);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700102
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700103 kfree(adapter);
104
105 return -1;
106}
107
108/*
109 * This function unregisters the device and performs all the necessary
110 * cleanups.
111 *
112 * The following cleanup operations are performed -
113 * - Free the timers
114 * - Free beacon buffers
115 * - Free private structures
116 * - Free adapter structure
117 */
118static int mwifiex_unregister(struct mwifiex_adapter *adapter)
119{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700120 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700121
Amitkumar Karwar4cfda672013-07-22 19:17:50 -0700122 if (adapter->if_ops.cleanup_if)
123 adapter->if_ops.cleanup_if(adapter);
124
Avinash Patil629873f2014-02-18 15:47:55 -0800125 del_timer_sync(&adapter->cmd_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700126
127 /* Free private structures */
128 for (i = 0; i < adapter->priv_num; i++) {
129 if (adapter->priv[i]) {
130 mwifiex_free_curr_bcn(adapter->priv[i]);
131 kfree(adapter->priv[i]);
132 }
133 }
134
Avinash Patilbf354432014-10-31 16:08:26 +0530135 vfree(adapter->chan_stats);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700136 kfree(adapter);
137 return 0;
138}
139
Shengzhen Lib2713f62015-03-13 17:37:54 +0530140void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
141{
142 unsigned long flags;
143
144 spin_lock_irqsave(&adapter->main_proc_lock, flags);
145 if (adapter->mwifiex_processing) {
146 adapter->more_task_flag = true;
147 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
148 } else {
149 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
150 queue_work(adapter->workqueue, &adapter->main_work);
151 }
152}
153EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
154
155static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
156{
157 unsigned long flags;
158
159 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
160 if (adapter->rx_processing) {
161 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
162 } else {
163 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
164 queue_work(adapter->rx_workqueue, &adapter->rx_work);
165 }
166}
167
Avinash Patil6e251172014-09-12 20:08:59 +0530168static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
169{
170 unsigned long flags;
171 struct sk_buff *skb;
Zhaoyang Liu92263a82015-03-13 17:37:58 +0530172 struct mwifiex_rxinfo *rx_info;
Avinash Patil6e251172014-09-12 20:08:59 +0530173
174 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
175 if (adapter->rx_processing || adapter->rx_locked) {
176 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
177 goto exit_rx_proc;
178 } else {
179 adapter->rx_processing = true;
180 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
181 }
182
183 /* Check for Rx data */
184 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
185 atomic_dec(&adapter->rx_pending);
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800186 if ((adapter->delay_main_work ||
187 adapter->iface_type == MWIFIEX_USB) &&
Avinash Patilb43a0d92014-09-29 21:44:14 +0530188 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530189 if (adapter->if_ops.submit_rem_rx_urbs)
190 adapter->if_ops.submit_rem_rx_urbs(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530191 adapter->delay_main_work = false;
Shengzhen Lib2713f62015-03-13 17:37:54 +0530192 mwifiex_queue_main_work(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530193 }
Zhaoyang Liu92263a82015-03-13 17:37:58 +0530194 rx_info = MWIFIEX_SKB_RXCB(skb);
195 if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
196 if (adapter->if_ops.deaggr_pkt)
197 adapter->if_ops.deaggr_pkt(adapter, skb);
198 dev_kfree_skb_any(skb);
199 } else {
200 mwifiex_handle_rx_packet(adapter, skb);
201 }
Avinash Patil6e251172014-09-12 20:08:59 +0530202 }
203 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
204 adapter->rx_processing = false;
205 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
206
Avinash Patil6e251172014-09-12 20:08:59 +0530207exit_rx_proc:
208 return 0;
209}
210
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700211/*
212 * The main process.
213 *
214 * This function is the main procedure of the driver and handles various driver
215 * operations. It runs in a loop and provides the core functionalities.
216 *
217 * The main responsibilities of this function are -
218 * - Ensure concurrency control
219 * - Handle pending interrupts and call interrupt handlers
220 * - Wake up the card if required
221 * - Handle command responses and call response handlers
222 * - Handle events and call event handlers
223 * - Execute pending commands
224 * - Transmit pending data packets
225 */
226int mwifiex_main_process(struct mwifiex_adapter *adapter)
227{
228 int ret = 0;
229 unsigned long flags;
230
231 spin_lock_irqsave(&adapter->main_proc_lock, flags);
232
233 /* Check if already processing */
Avinash Patila9adbcb2015-03-13 17:37:51 +0530234 if (adapter->mwifiex_processing || adapter->main_locked) {
Shengzhen Li04c7b362015-02-11 23:12:24 +0530235 adapter->more_task_flag = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700236 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
237 goto exit_main_proc;
238 } else {
239 adapter->mwifiex_processing = true;
Cathy Luo91457ea2015-04-17 04:18:29 -0700240 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700241 }
242process_start:
243 do {
244 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
245 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
246 break;
247
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800248 /* For non-USB interfaces, If we process interrupts first, it
249 * would increase RX pending even further. Avoid this by
250 * checking if rx_pending has crossed high threshold and
251 * schedule rx work queue and then process interrupts.
252 * For USB interface, there are no interrupts. We already have
253 * HIGH_RX_PENDING check in usb.c
Avinash Patil6e251172014-09-12 20:08:59 +0530254 */
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800255 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
256 adapter->iface_type != MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +0530257 adapter->delay_main_work = true;
Shengzhen Lib2713f62015-03-13 17:37:54 +0530258 mwifiex_queue_rx_work(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530259 break;
260 }
261
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700262 /* Handle pending interrupt if any */
263 if (adapter->int_status) {
264 if (adapter->hs_activated)
265 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700266 if (adapter->if_ops.process_int_status)
267 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700268 }
269
Avinash Patil6e251172014-09-12 20:08:59 +0530270 if (adapter->rx_work_enabled && adapter->data_received)
Shengzhen Lib2713f62015-03-13 17:37:54 +0530271 mwifiex_queue_rx_work(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530272
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700273 /* Need to wake up the card ? */
274 if ((adapter->ps_state == PS_STATE_SLEEP) &&
275 (adapter->pm_wakeup_card_req &&
276 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700277 (is_command_pending(adapter) ||
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530278 !skb_queue_empty(&adapter->tx_data_q) ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700279 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700280 adapter->pm_wakeup_fw_try = true;
Amitkumar Karwar46361872014-12-31 02:36:41 -0800281 mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700282 adapter->if_ops.wakeup(adapter);
283 continue;
284 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700285
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700286 if (IS_CARD_RX_RCVD(adapter)) {
Avinash Patil6e251172014-09-12 20:08:59 +0530287 adapter->data_received = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700288 adapter->pm_wakeup_fw_try = false;
Amitkumar Karwar6e9344f2015-03-12 00:38:40 -0700289 del_timer(&adapter->wakeup_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700290 if (adapter->ps_state == PS_STATE_SLEEP)
291 adapter->ps_state = PS_STATE_AWAKE;
292 } else {
293 /* We have tried to wakeup the card already */
294 if (adapter->pm_wakeup_fw_try)
295 break;
296 if (adapter->ps_state != PS_STATE_AWAKE ||
297 adapter->tx_lock_flag)
298 break;
299
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530300 if ((!adapter->scan_chan_gap_enabled &&
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530301 adapter->scan_processing) || adapter->data_sent ||
Xinming Huba101ad2015-06-22 19:06:10 +0530302 mwifiex_is_tdls_chan_switching
303 (mwifiex_get_priv(adapter,
304 MWIFIEX_BSS_ROLE_STA)) ||
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530305 (mwifiex_wmm_lists_empty(adapter) &&
306 skb_queue_empty(&adapter->tx_data_q))) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700307 if (adapter->cmd_sent || adapter->curr_cmd ||
Xinming Huba101ad2015-06-22 19:06:10 +0530308 !mwifiex_is_send_cmd_allowed
309 (mwifiex_get_priv(adapter,
310 MWIFIEX_BSS_ROLE_STA)) ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700311 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700312 break;
313 }
314 }
315
Amitkumar Karwar20474122014-04-14 15:31:05 -0700316 /* Check for event */
317 if (adapter->event_received) {
318 adapter->event_received = false;
319 mwifiex_process_event(adapter);
320 }
321
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700322 /* Check for Cmd Resp */
323 if (adapter->cmd_resp_received) {
324 adapter->cmd_resp_received = false;
325 mwifiex_process_cmdresp(adapter);
326
327 /* call mwifiex back when init_fw is done */
328 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
329 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
330 mwifiex_init_fw_complete(adapter);
331 }
332 }
333
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700334 /* Check if we need to confirm Sleep Request
335 received previously */
336 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
337 if (!adapter->cmd_sent && !adapter->curr_cmd)
338 mwifiex_check_ps_cond(adapter);
339 }
340
341 /* * The ps_state may have been changed during processing of
342 * Sleep Request event.
343 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700344 if ((adapter->ps_state == PS_STATE_SLEEP) ||
345 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
346 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
Shengzhen Li04c7b362015-02-11 23:12:24 +0530347 adapter->tx_lock_flag){
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700348 continue;
Shengzhen Li04c7b362015-02-11 23:12:24 +0530349 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700350
Xinming Huba101ad2015-06-22 19:06:10 +0530351 if (!adapter->cmd_sent && !adapter->curr_cmd &&
352 mwifiex_is_send_cmd_allowed
353 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700354 if (mwifiex_exec_next_cmd(adapter) == -1) {
355 ret = -1;
356 break;
357 }
358 }
359
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530360 if ((adapter->scan_chan_gap_enabled ||
Amitkumar Karward8d91252014-09-12 20:08:58 +0530361 !adapter->scan_processing) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530362 !adapter->data_sent &&
363 !skb_queue_empty(&adapter->tx_data_q)) {
364 mwifiex_process_tx_queue(adapter);
365 if (adapter->hs_activated) {
366 adapter->is_hs_configured = false;
367 mwifiex_hs_activated_event
368 (mwifiex_get_priv
369 (adapter, MWIFIEX_BSS_ROLE_ANY),
370 false);
371 }
372 }
373
374 if ((adapter->scan_chan_gap_enabled ||
375 !adapter->scan_processing) &&
Xinming Huba101ad2015-06-22 19:06:10 +0530376 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
377 !mwifiex_is_tdls_chan_switching
378 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700379 mwifiex_wmm_process_tx(adapter);
380 if (adapter->hs_activated) {
381 adapter->is_hs_configured = false;
382 mwifiex_hs_activated_event
383 (mwifiex_get_priv
384 (adapter, MWIFIEX_BSS_ROLE_ANY),
385 false);
386 }
387 }
388
389 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700390 !adapter->curr_cmd && !is_command_pending(adapter) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530391 (mwifiex_wmm_lists_empty(adapter) &&
392 skb_queue_empty(&adapter->tx_data_q))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700393 if (!mwifiex_send_null_packet
394 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
395 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
396 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
397 adapter->delay_null_pkt = false;
398 adapter->ps_state = PS_STATE_SLEEP;
399 }
400 break;
401 }
402 } while (true);
403
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700404 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Cathy Luo91457ea2015-04-17 04:18:29 -0700405 if (adapter->more_task_flag) {
406 adapter->more_task_flag = false;
407 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700408 goto process_start;
Cathy Luo91457ea2015-04-17 04:18:29 -0700409 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700410 adapter->mwifiex_processing = false;
411 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
412
413exit_main_proc:
414 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
415 mwifiex_shutdown_drv(adapter);
416 return ret;
417}
Bing Zhao601216e2012-11-05 16:59:15 -0800418EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700419
420/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700421 * This function frees the adapter structure.
422 *
423 * Additionally, this closes the netlink socket, frees the timers
424 * and private structures.
425 */
426static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
427{
428 if (!adapter) {
429 pr_err("%s: adapter is NULL\n", __func__);
430 return;
431 }
432
433 mwifiex_unregister(adapter);
434 pr_debug("info: %s: free adapter\n", __func__);
435}
436
437/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700438 * This function cancels all works in the queue and destroys
439 * the main workqueue.
440 */
441static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
442{
443 flush_workqueue(adapter->workqueue);
444 destroy_workqueue(adapter->workqueue);
445 adapter->workqueue = NULL;
Avinash Patil6e251172014-09-12 20:08:59 +0530446
447 if (adapter->rx_workqueue) {
448 flush_workqueue(adapter->rx_workqueue);
449 destroy_workqueue(adapter->rx_workqueue);
450 adapter->rx_workqueue = NULL;
451 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700452}
453
454/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700455 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700456 *
457 * The main initialization steps followed are -
458 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700459 * - Issue the init commands to firmware
460 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700461static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700462{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800463 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700464 char fmt[64];
465 struct mwifiex_private *priv;
466 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700467 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700468 struct semaphore *sem = adapter->card_sem;
469 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800470 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700471
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700472 if (!firmware) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530473 mwifiex_dbg(adapter, ERROR,
474 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700475 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700476 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700477
478 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
479 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700480 fw.fw_buf = (u8 *) adapter->firmware->data;
481 fw.fw_len = adapter->firmware->size;
482
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700483 if (adapter->if_ops.dnld_fw)
484 ret = adapter->if_ops.dnld_fw(adapter, &fw);
485 else
486 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700487 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700488 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700489
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530490 mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700491
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700492 if (cal_data_cfg) {
493 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
494 adapter->dev)) < 0)
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530495 mwifiex_dbg(adapter, ERROR,
496 "Cal data request_firmware() failed\n");
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700497 }
498
Daniel Drake232fde02013-07-13 10:57:10 -0400499 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700500 if (adapter->if_ops.enable_int) {
501 if (adapter->if_ops.enable_int(adapter))
502 goto err_dnld_fw;
503 }
Daniel Drake232fde02013-07-13 10:57:10 -0400504
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700505 adapter->init_wait_q_woken = false;
506 ret = mwifiex_init_fw(adapter);
507 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700508 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700509 } else if (!ret) {
510 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
511 goto done;
512 }
513 /* Wait for mwifiex_init to complete */
514 wait_event_interruptible(adapter->init_wait_q,
515 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700516 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700517 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700518
Avinash Patild6bffe82012-05-08 18:30:15 -0700519 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
520 if (mwifiex_register_cfg80211(adapter)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530521 mwifiex_dbg(adapter, ERROR,
522 "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800523 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700524 }
525
Avinash Patilbf354432014-10-31 16:08:26 +0530526 if (mwifiex_init_channel_scan_gap(adapter)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530527 mwifiex_dbg(adapter, ERROR,
528 "could not init channel stats table\n");
Avinash Patilbf354432014-10-31 16:08:26 +0530529 goto err_init_fw;
530 }
531
Avinash Patil0013c7c2014-11-05 19:38:11 +0530532 if (driver_mode) {
533 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
534 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
535 }
536
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700537 rtnl_lock();
538 /* Create station interface by default */
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100539 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800540 NL80211_IFTYPE_STATION, NULL, NULL);
541 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530542 mwifiex_dbg(adapter, ERROR,
543 "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800544 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700545 goto err_add_intf;
546 }
Avinash Patil0013c7c2014-11-05 19:38:11 +0530547
548 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100549 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
Avinash Patil0013c7c2014-11-05 19:38:11 +0530550 NL80211_IFTYPE_AP, NULL, NULL);
551 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530552 mwifiex_dbg(adapter, ERROR,
553 "cannot create AP interface\n");
Avinash Patil0013c7c2014-11-05 19:38:11 +0530554 rtnl_unlock();
555 goto err_add_intf;
556 }
557 }
558
559 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100560 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
Avinash Patil0013c7c2014-11-05 19:38:11 +0530561 NL80211_IFTYPE_P2P_CLIENT, NULL,
562 NULL);
563 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530564 mwifiex_dbg(adapter, ERROR,
565 "cannot create p2p client interface\n");
Avinash Patil0013c7c2014-11-05 19:38:11 +0530566 rtnl_unlock();
567 goto err_add_intf;
568 }
569 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700570 rtnl_unlock();
571
572 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530573 mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700574 goto done;
575
576err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700577 wiphy_unregister(adapter->wiphy);
578 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700579err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400580 if (adapter->if_ops.disable_int)
581 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700582err_dnld_fw:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530583 mwifiex_dbg(adapter, ERROR,
584 "info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700585 if (adapter->if_ops.unregister_dev)
586 adapter->if_ops.unregister_dev(adapter);
587
Amitkumar Karwar71973252014-12-31 02:36:38 -0800588 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700589 pr_debug("info: %s: shutdown mwifiex\n", __func__);
590 adapter->init_wait_q_woken = false;
591
592 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
593 wait_event_interruptible(adapter->init_wait_q,
594 adapter->init_wait_q_woken);
595 }
596 adapter->surprise_removed = true;
597 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700598 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700599done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700600 if (adapter->cal_data) {
601 release_firmware(adapter->cal_data);
602 adapter->cal_data = NULL;
603 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700604 if (adapter->firmware) {
605 release_firmware(adapter->firmware);
606 adapter->firmware = NULL;
607 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700608 if (init_failed)
609 mwifiex_free_adapter(adapter);
610 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700611 return;
612}
613
614/*
615 * This function initializes the hardware and gets firmware.
616 */
617static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
618{
619 int ret;
620
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700621 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
622 adapter->dev, GFP_KERNEL, adapter,
623 mwifiex_fw_dpc);
624 if (ret < 0)
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530625 mwifiex_dbg(adapter, ERROR,
626 "request_firmware_nowait error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700627 return ret;
628}
629
630/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700631 * CFG802.11 network device handler for open.
632 *
633 * Starts the data queue.
634 */
635static int
636mwifiex_open(struct net_device *dev)
637{
Johannes Bergea2325b2015-01-19 15:54:36 +0530638 netif_carrier_off(dev);
639
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700640 return 0;
641}
642
643/*
644 * CFG802.11 network device handler for close.
645 */
646static int
647mwifiex_close(struct net_device *dev)
648{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700649 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
650
651 if (priv->scan_request) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530652 mwifiex_dbg(priv->adapter, INFO,
653 "aborting scan on ndo_stop\n");
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700654 cfg80211_scan_done(priv->scan_request, 1);
655 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700656 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700657 }
658
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700659 return 0;
660}
661
662/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700663 * Add buffer into wmm tx queue and queue work to transmit it.
664 */
665int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
666{
Avinash Patil47411a02012-11-01 18:44:16 -0700667 struct netdev_queue *txq;
668 int index = mwifiex_1d_to_wmm_queue[skb->priority];
669
670 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
671 txq = netdev_get_tx_queue(priv->netdev, index);
672 if (!netif_tx_queue_stopped(txq)) {
673 netif_tx_stop_queue(txq);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530674 mwifiex_dbg(priv->adapter, DATA,
675 "stop queue: %d\n", index);
Avinash Patil47411a02012-11-01 18:44:16 -0700676 }
677 }
678
Stone Piaoe39faa72012-09-25 20:23:32 -0700679 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700680 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700681
Shengzhen Lib2713f62015-03-13 17:37:54 +0530682 mwifiex_queue_main_work(priv->adapter);
Stone Piaoe39faa72012-09-25 20:23:32 -0700683
684 return 0;
685}
686
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800687struct sk_buff *
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800688mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800689 struct sk_buff *skb, u8 flag, u64 *cookie)
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800690{
691 struct sk_buff *orig_skb = skb;
692 struct mwifiex_txinfo *tx_info, *orig_tx_info;
693
694 skb = skb_clone(skb, GFP_ATOMIC);
695 if (skb) {
696 unsigned long flags;
697 int id;
698
699 spin_lock_irqsave(&priv->ack_status_lock, flags);
700 id = idr_alloc(&priv->ack_status_frames, orig_skb,
701 1, 0xff, GFP_ATOMIC);
702 spin_unlock_irqrestore(&priv->ack_status_lock, flags);
703
704 if (id >= 0) {
705 tx_info = MWIFIEX_SKB_TXCB(skb);
706 tx_info->ack_frame_id = id;
707 tx_info->flags |= flag;
708 orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
709 orig_tx_info->ack_frame_id = id;
710 orig_tx_info->flags |= flag;
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800711
712 if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
713 orig_tx_info->cookie = *cookie;
714
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800715 } else if (skb_shared(skb)) {
716 kfree_skb(orig_skb);
717 } else {
718 kfree_skb(skb);
719 skb = orig_skb;
720 }
721 } else {
722 /* couldn't clone -- lose tx status ... */
723 skb = orig_skb;
724 }
725
726 return skb;
727}
728
Stone Piaoe39faa72012-09-25 20:23:32 -0700729/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700730 * CFG802.11 network device handler for data transmission.
731 */
732static int
733mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
734{
735 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700736 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700737 struct mwifiex_txinfo *tx_info;
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800738 bool multicast;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700739
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530740 mwifiex_dbg(priv->adapter, DATA,
741 "data: %lu BSS(%d-%d): Data <= kernel\n",
742 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700743
744 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200745 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700746 priv->stats.tx_dropped++;
747 return 0;
748 }
749 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530750 mwifiex_dbg(priv->adapter, ERROR,
751 "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200752 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700753 priv->stats.tx_dropped++;
754 return 0;
755 }
756 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530757 mwifiex_dbg(priv->adapter, DATA,
758 "data: Tx: insufficient skb headroom %d\n",
759 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700760 /* Insufficient skb headroom - allocate a new skb */
761 new_skb =
762 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
763 if (unlikely(!new_skb)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530764 mwifiex_dbg(priv->adapter, ERROR,
765 "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200766 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700767 priv->stats.tx_dropped++;
768 return 0;
769 }
770 kfree_skb(skb);
771 skb = new_skb;
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530772 mwifiex_dbg(priv->adapter, INFO,
773 "info: new skb headroomd %d\n",
774 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700775 }
776
777 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700778 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800779 tx_info->bss_num = priv->bss_num;
780 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800781 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700782
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800783 multicast = is_multicast_ether_addr(skb->data);
784
785 if (unlikely(!multicast && skb->sk &&
786 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
787 priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
788 skb = mwifiex_clone_skb_for_tx_status(priv,
789 skb,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800790 MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800791
Avinash Patil47411a02012-11-01 18:44:16 -0700792 /* Record the current time the packet was queued; used to
793 * determine the amount of time the packet was queued in
794 * the driver before it was sent to the firmware.
795 * The delay is then sent along with the packet to the
796 * firmware for aggregate delay calculation for stats and
797 * MSDU lifetime expiry.
798 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100799 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700800
Avinash Patil9927baa2014-11-13 21:54:16 +0530801 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
802 priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
803 !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
804 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
805 mwifiex_tdls_check_tx(priv, skb);
806 }
807
Stone Piaoe39faa72012-09-25 20:23:32 -0700808 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700809
810 return 0;
811}
812
813/*
814 * CFG802.11 network device handler for setting MAC address.
815 */
816static int
817mwifiex_set_mac_address(struct net_device *dev, void *addr)
818{
819 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700820 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700821 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700822
823 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
824
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700825 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800826 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
827 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700828
829 if (!ret)
830 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
831 else
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530832 mwifiex_dbg(priv->adapter, ERROR,
833 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700834
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700835 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
836
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700837 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700838}
839
840/*
841 * CFG802.11 network device handler for setting multicast list.
842 */
843static void mwifiex_set_multicast_list(struct net_device *dev)
844{
845 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700846 struct mwifiex_multicast_list mcast_list;
847
848 if (dev->flags & IFF_PROMISC) {
849 mcast_list.mode = MWIFIEX_PROMISC_MODE;
850 } else if (dev->flags & IFF_ALLMULTI ||
851 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
852 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
853 } else {
854 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400855 mcast_list.num_multicast_addr =
856 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700857 }
858 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700859}
860
861/*
862 * CFG802.11 network device handler for transmission timeout.
863 */
864static void
865mwifiex_tx_timeout(struct net_device *dev)
866{
867 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
868
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700869 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800870 priv->tx_timeout_cnt++;
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530871 mwifiex_dbg(priv->adapter, ERROR,
872 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
873 jiffies, priv->tx_timeout_cnt, priv->bss_type,
874 priv->bss_num);
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800875 mwifiex_set_trans_start(dev);
876
877 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
878 priv->adapter->if_ops.card_reset) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530879 mwifiex_dbg(priv->adapter, ERROR,
880 "tx_timeout_cnt exceeds threshold.\t"
881 "Triggering card reset!\n");
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800882 priv->adapter->if_ops.card_reset(priv->adapter);
883 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700884}
885
Amitkumar Karwarfc697152015-05-26 06:34:31 -0700886void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
Xinming Hu11cd07a2014-12-23 19:14:10 +0530887{
888 void *p;
889 char drv_version[64];
890 struct usb_card_rec *cardp;
891 struct sdio_mmc_card *sdio_card;
892 struct mwifiex_private *priv;
893 int i, idx;
894 struct netdev_queue *txq;
895 struct mwifiex_debug_info *debug_info;
896
897 if (adapter->drv_info_dump) {
898 vfree(adapter->drv_info_dump);
Amitkumar Karwar0769b272015-05-26 06:34:28 -0700899 adapter->drv_info_dump = NULL;
Xinming Hu11cd07a2014-12-23 19:14:10 +0530900 adapter->drv_info_size = 0;
901 }
902
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -0700903 mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +0530904
905 adapter->drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
906
907 if (!adapter->drv_info_dump)
908 return;
909
910 p = (char *)(adapter->drv_info_dump);
911 p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
912
913 mwifiex_drv_get_driver_version(adapter, drv_version,
914 sizeof(drv_version) - 1);
915 p += sprintf(p, "driver_version = %s\n", drv_version);
916
917 if (adapter->iface_type == MWIFIEX_USB) {
918 cardp = (struct usb_card_rec *)adapter->card;
919 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
920 atomic_read(&cardp->tx_cmd_urb_pending));
921 p += sprintf(p, "tx_data_urb_pending = %d\n",
922 atomic_read(&cardp->tx_data_urb_pending));
923 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
924 atomic_read(&cardp->rx_cmd_urb_pending));
925 p += sprintf(p, "rx_data_urb_pending = %d\n",
926 atomic_read(&cardp->rx_data_urb_pending));
927 }
928
929 p += sprintf(p, "tx_pending = %d\n",
930 atomic_read(&adapter->tx_pending));
931 p += sprintf(p, "rx_pending = %d\n",
932 atomic_read(&adapter->rx_pending));
933
934 if (adapter->iface_type == MWIFIEX_SDIO) {
935 sdio_card = (struct sdio_mmc_card *)adapter->card;
936 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
937 sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
938 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
939 sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
940 }
941
942 for (i = 0; i < adapter->priv_num; i++) {
943 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
944 continue;
945 priv = adapter->priv[i];
946 p += sprintf(p, "\n[interface : \"%s\"]\n",
947 priv->netdev->name);
948 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
949 atomic_read(&priv->wmm_tx_pending[0]));
950 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
951 atomic_read(&priv->wmm_tx_pending[1]));
952 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
953 atomic_read(&priv->wmm_tx_pending[2]));
954 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
955 atomic_read(&priv->wmm_tx_pending[3]));
956 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
957 "Disconnected" : "Connected");
958 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
959 ? "on" : "off"));
960 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
961 txq = netdev_get_tx_queue(priv->netdev, idx);
962 p += sprintf(p, "tx queue %d:%s ", idx,
963 netif_tx_queue_stopped(txq) ?
964 "stopped" : "started");
965 }
966 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
967 priv->netdev->name, priv->num_tx_timeout);
968 }
969
Xinming Hu809c6ea2014-12-23 19:14:11 +0530970 if (adapter->iface_type == MWIFIEX_SDIO) {
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -0700971 p += sprintf(p, "\n=== SDIO register dump===\n");
Xinming Hu809c6ea2014-12-23 19:14:11 +0530972 if (adapter->if_ops.reg_dump)
973 p += adapter->if_ops.reg_dump(adapter, p);
974 }
975
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -0700976 p += sprintf(p, "\n=== more debug information\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +0530977 debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
978 if (debug_info) {
979 for (i = 0; i < adapter->priv_num; i++) {
980 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
981 continue;
982 priv = adapter->priv[i];
983 mwifiex_get_debug_info(priv, debug_info);
984 p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
985 break;
986 }
987 kfree(debug_info);
988 }
989
990 adapter->drv_info_size = p - adapter->drv_info_dump;
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -0700991 mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +0530992}
Amitkumar Karwarfc697152015-05-26 06:34:31 -0700993EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
Xinming Hu11cd07a2014-12-23 19:14:10 +0530994
Amitkumar Karwar57670ee2015-05-26 06:34:32 -0700995void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
996{
997 u8 idx, *dump_data, *fw_dump_ptr;
998 u32 dump_len;
999
1000 dump_len = (strlen("========Start dump driverinfo========\n") +
1001 adapter->drv_info_size +
1002 strlen("\n========End dump========\n"));
1003
1004 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1005 struct memory_type_mapping *entry =
1006 &adapter->mem_type_mapping_tbl[idx];
1007
1008 if (entry->mem_ptr) {
1009 dump_len += (strlen("========Start dump ") +
1010 strlen(entry->mem_name) +
1011 strlen("========\n") +
1012 (entry->mem_size + 1) +
1013 strlen("\n========End dump========\n"));
1014 }
1015 }
1016
1017 dump_data = vzalloc(dump_len + 1);
1018 if (!dump_data)
1019 goto done;
1020
1021 fw_dump_ptr = dump_data;
1022
1023 /* Dump all the memory data into single file, a userspace script will
1024 * be used to split all the memory data to multiple files
1025 */
1026 mwifiex_dbg(adapter, MSG,
1027 "== mwifiex dump information to /sys/class/devcoredump start");
1028
1029 strcpy(fw_dump_ptr, "========Start dump driverinfo========\n");
1030 fw_dump_ptr += strlen("========Start dump driverinfo========\n");
1031 memcpy(fw_dump_ptr, adapter->drv_info_dump, adapter->drv_info_size);
1032 fw_dump_ptr += adapter->drv_info_size;
1033 strcpy(fw_dump_ptr, "\n========End dump========\n");
1034 fw_dump_ptr += strlen("\n========End dump========\n");
1035
1036 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1037 struct memory_type_mapping *entry =
1038 &adapter->mem_type_mapping_tbl[idx];
1039
1040 if (entry->mem_ptr) {
1041 strcpy(fw_dump_ptr, "========Start dump ");
1042 fw_dump_ptr += strlen("========Start dump ");
1043
1044 strcpy(fw_dump_ptr, entry->mem_name);
1045 fw_dump_ptr += strlen(entry->mem_name);
1046
1047 strcpy(fw_dump_ptr, "========\n");
1048 fw_dump_ptr += strlen("========\n");
1049
1050 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1051 fw_dump_ptr += entry->mem_size;
1052
1053 strcpy(fw_dump_ptr, "\n========End dump========\n");
1054 fw_dump_ptr += strlen("\n========End dump========\n");
1055 }
1056 }
1057
1058 /* device dump data will be free in device coredump release function
1059 * after 5 min
1060 */
1061 dev_coredumpv(adapter->dev, dump_data, dump_len, GFP_KERNEL);
1062 mwifiex_dbg(adapter, MSG,
1063 "== mwifiex dump information to /sys/class/devcoredump end");
1064
1065done:
1066 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1067 struct memory_type_mapping *entry =
1068 &adapter->mem_type_mapping_tbl[idx];
1069
1070 if (entry->mem_ptr) {
1071 vfree(entry->mem_ptr);
1072 entry->mem_ptr = NULL;
1073 }
1074 entry->mem_size = 0;
1075 }
1076
1077 if (adapter->drv_info_dump) {
1078 vfree(adapter->drv_info_dump);
1079 adapter->drv_info_dump = NULL;
1080 adapter->drv_info_size = 0;
1081 }
1082}
1083EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1084
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001085/*
1086 * CFG802.11 network device handler for statistics retrieval.
1087 */
1088static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1089{
1090 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1091
1092 return &priv->stats;
1093}
1094
Avinash Patil47411a02012-11-01 18:44:16 -07001095static u16
Jason Wangf663dd92014-01-10 16:18:26 +08001096mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +01001097 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -07001098{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001099 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -07001100 return mwifiex_1d_to_wmm_queue[skb->priority];
1101}
1102
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001103/* Network device handlers */
1104static const struct net_device_ops mwifiex_netdev_ops = {
1105 .ndo_open = mwifiex_open,
1106 .ndo_stop = mwifiex_close,
1107 .ndo_start_xmit = mwifiex_hard_start_xmit,
1108 .ndo_set_mac_address = mwifiex_set_mac_address,
1109 .ndo_tx_timeout = mwifiex_tx_timeout,
1110 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001111 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -07001112 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001113};
1114
1115/*
1116 * This function initializes the private structure parameters.
1117 *
1118 * The following wait queues are initialized -
1119 * - IOCTL wait queue
1120 * - Command wait queue
1121 * - Statistics wait queue
1122 *
1123 * ...and the following default parameters are set -
1124 * - Current key index : Set to 0
1125 * - Rate index : Set to auto
1126 * - Media connected : Set to disconnected
1127 * - Adhoc link sensed : Set to false
1128 * - Nick name : Set to null
1129 * - Number of Tx timeout : Set to 0
1130 * - Device address : Set to current address
Xinming Hucbf6e052014-12-23 19:14:07 +05301131 * - Rx histogram statistc : Set to 0
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001132 *
1133 * In addition, the CFG80211 work queue is also created.
1134 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001135void mwifiex_init_priv_params(struct mwifiex_private *priv,
Avinash Patil047eaaf2015-01-28 15:42:05 +05301136 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001137{
1138 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -07001139 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001140 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001141 priv->current_key_index = 0;
1142 priv->media_connected = false;
Avinash Patilede98bf2012-05-08 18:30:28 -07001143 memset(priv->mgmt_ie, 0,
1144 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -07001145 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1146 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1147 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
Avinash Patil2ade5662015-01-28 15:54:20 +05301148 priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001149 priv->num_tx_timeout = 0;
Avinash Patil8d05eb22015-01-28 15:42:01 +05301150 ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001151 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
Xinming Hucbf6e052014-12-23 19:14:07 +05301152
1153 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1154 GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1155 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1156 if (priv->hist_data)
1157 mwifiex_hist_data_reset(priv);
1158 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001159}
1160
1161/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001162 * This function check if command is pending.
1163 */
1164int is_command_pending(struct mwifiex_adapter *adapter)
1165{
1166 unsigned long flags;
1167 int is_cmd_pend_q_empty;
1168
1169 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1170 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1171 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1172
1173 return !is_cmd_pend_q_empty;
1174}
1175
1176/*
Avinash Patil6e251172014-09-12 20:08:59 +05301177 * This is the RX work queue function.
1178 *
1179 * It handles the RX operations.
1180 */
1181static void mwifiex_rx_work_queue(struct work_struct *work)
1182{
1183 struct mwifiex_adapter *adapter =
1184 container_of(work, struct mwifiex_adapter, rx_work);
1185
1186 if (adapter->surprise_removed)
1187 return;
1188 mwifiex_process_rx(adapter);
1189}
1190
1191/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001192 * This is the main work queue function.
1193 *
1194 * It handles the main process, which in turn handles the complete
1195 * driver operations.
1196 */
1197static void mwifiex_main_work_queue(struct work_struct *work)
1198{
1199 struct mwifiex_adapter *adapter =
1200 container_of(work, struct mwifiex_adapter, main_work);
1201
1202 if (adapter->surprise_removed)
1203 return;
1204 mwifiex_main_process(adapter);
1205}
1206
1207/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001208 * This function adds the card.
1209 *
1210 * This function follows the following major steps to set up the device -
1211 * - Initialize software. This includes probing the card, registering
1212 * the interface operations table, and allocating/initializing the
1213 * adapter structure
1214 * - Set up the netlink socket
1215 * - Create and start the main work queue
1216 * - Register the device
1217 * - Initialize firmware and hardware
1218 * - Add logical interfaces
1219 */
1220int
1221mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001222 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001223{
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001224 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001225
1226 if (down_interruptible(sem))
1227 goto exit_sem_err;
1228
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001229 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001230 pr_err("%s: software init failed\n", __func__);
1231 goto err_init_sw;
1232 }
1233
Amitkumar Karward930fae2011-10-11 17:41:21 -07001234 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001235 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001236
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001237 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001238 adapter->surprise_removed = false;
1239 init_waitqueue_head(&adapter->init_wait_q);
1240 adapter->is_suspended = false;
1241 adapter->hs_activated = false;
1242 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001243 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001244 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -07001245 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001246
Avinash Patilec4a16b2014-11-05 17:04:27 +05301247 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +05301248 adapter->rx_work_enabled = true;
1249 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
1250 }
1251
Amitkumar Karwar448a71c2013-10-11 18:33:04 -07001252 adapter->workqueue =
1253 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1254 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001255 if (!adapter->workqueue)
1256 goto err_kmalloc;
1257
1258 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Avinash Patil6e251172014-09-12 20:08:59 +05301259
1260 if (adapter->rx_work_enabled) {
1261 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1262 WQ_HIGHPRI |
1263 WQ_MEM_RECLAIM |
1264 WQ_UNBOUND, 1);
1265 if (!adapter->rx_workqueue)
1266 goto err_kmalloc;
1267
1268 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1269 }
1270
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001271 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -04001272 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001273 if (adapter->if_ops.register_dev(adapter)) {
1274 pr_err("%s: failed to register mwifiex device\n", __func__);
1275 goto err_registerdev;
1276 }
1277
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001278 if (mwifiex_init_hw_fw(adapter)) {
1279 pr_err("%s: firmware init failed\n", __func__);
1280 goto err_init_fw;
1281 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001282
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001283 return 0;
1284
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001285err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001286 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001287 if (adapter->if_ops.unregister_dev)
1288 adapter->if_ops.unregister_dev(adapter);
Amitkumar Karwar71973252014-12-31 02:36:38 -08001289 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001290 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1291 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001292
1293 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001294 wait_event_interruptible(adapter->init_wait_q,
1295 adapter->init_wait_q_woken);
1296 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001297err_registerdev:
1298 adapter->surprise_removed = true;
1299 mwifiex_terminate_workqueue(adapter);
1300err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001301 mwifiex_free_adapter(adapter);
1302
1303err_init_sw:
1304 up(sem);
1305
1306exit_sem_err:
1307 return -1;
1308}
1309EXPORT_SYMBOL_GPL(mwifiex_add_card);
1310
1311/*
1312 * This function removes the card.
1313 *
1314 * This function follows the following major steps to remove the device -
1315 * - Stop data traffic
1316 * - Shutdown firmware
1317 * - Remove the logical interfaces
1318 * - Terminate the work queue
1319 * - Unregister the device
1320 * - Free the adapter structure
1321 */
1322int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
1323{
1324 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001325 int i;
1326
1327 if (down_interruptible(sem))
1328 goto exit_sem_err;
1329
1330 if (!adapter)
1331 goto exit_remove;
1332
Daniel Drake232fde02013-07-13 10:57:10 -04001333 /* We can no longer handle interrupts once we start doing the teardown
1334 * below. */
1335 if (adapter->if_ops.disable_int)
1336 adapter->if_ops.disable_int(adapter);
1337
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001338 adapter->surprise_removed = true;
1339
Marc Yang9d2e85e2014-12-31 02:36:39 -08001340 mwifiex_terminate_workqueue(adapter);
1341
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001342 /* Stop data */
1343 for (i = 0; i < adapter->priv_num; i++) {
1344 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001345 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -07001346 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001347 if (netif_carrier_ok(priv->netdev))
1348 netif_carrier_off(priv->netdev);
1349 }
1350 }
1351
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301352 mwifiex_dbg(adapter, CMD,
1353 "cmd: calling mwifiex_shutdown_drv...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001354 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001355
1356 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001357 wait_event_interruptible(adapter->init_wait_q,
1358 adapter->init_wait_q_woken);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301359 mwifiex_dbg(adapter, CMD,
1360 "cmd: mwifiex_shutdown_drv done\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001361 if (atomic_read(&adapter->rx_pending) ||
1362 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001363 atomic_read(&adapter->cmd_pending)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301364 mwifiex_dbg(adapter, ERROR,
1365 "rx_pending=%d, tx_pending=%d,\t"
1366 "cmd_pending=%d\n",
1367 atomic_read(&adapter->rx_pending),
1368 atomic_read(&adapter->tx_pending),
1369 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001370 }
1371
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001372 for (i = 0; i < adapter->priv_num; i++) {
1373 priv = adapter->priv[i];
1374
1375 if (!priv)
1376 continue;
1377
1378 rtnl_lock();
Avinash Patil4facc342015-01-28 15:42:00 +05301379 if (priv->netdev &&
1380 priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1381 mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001382 rtnl_unlock();
1383 }
1384
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -08001385 wiphy_unregister(adapter->wiphy);
1386 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -07001387
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001388 /* Unregister device */
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301389 mwifiex_dbg(adapter, INFO,
1390 "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001391 if (adapter->if_ops.unregister_dev)
1392 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001393 /* Free adapter structure */
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301394 mwifiex_dbg(adapter, INFO,
1395 "info: free adapter\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001396 mwifiex_free_adapter(adapter);
1397
1398exit_remove:
1399 up(sem);
1400exit_sem_err:
1401 return 0;
1402}
1403EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1404
1405/*
1406 * This function initializes the module.
1407 *
1408 * The debug FS is also initialized if configured.
1409 */
1410static int
1411mwifiex_init_module(void)
1412{
1413#ifdef CONFIG_DEBUG_FS
1414 mwifiex_debugfs_init();
1415#endif
1416 return 0;
1417}
1418
1419/*
1420 * This function cleans up the module.
1421 *
1422 * The debug FS is removed if available.
1423 */
1424static void
1425mwifiex_cleanup_module(void)
1426{
1427#ifdef CONFIG_DEBUG_FS
1428 mwifiex_debugfs_remove();
1429#endif
1430}
1431
1432module_init(mwifiex_init_module);
1433module_exit(mwifiex_cleanup_module);
1434
1435MODULE_AUTHOR("Marvell International Ltd.");
1436MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1437MODULE_VERSION(VERSION);
1438MODULE_LICENSE("GPL v2");