blob: 4fa8ca33937dd2b75c1847d607c32d3df9594912 [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) ||
Avinash Patila1777322015-06-22 19:06:17 +0530279 !mwifiex_bypass_txlist_empty(adapter) ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700280 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700281 adapter->pm_wakeup_fw_try = true;
Amitkumar Karwar46361872014-12-31 02:36:41 -0800282 mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700283 adapter->if_ops.wakeup(adapter);
284 continue;
285 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700286
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700287 if (IS_CARD_RX_RCVD(adapter)) {
Avinash Patil6e251172014-09-12 20:08:59 +0530288 adapter->data_received = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700289 adapter->pm_wakeup_fw_try = false;
Amitkumar Karwar6e9344f2015-03-12 00:38:40 -0700290 del_timer(&adapter->wakeup_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700291 if (adapter->ps_state == PS_STATE_SLEEP)
292 adapter->ps_state = PS_STATE_AWAKE;
293 } else {
294 /* We have tried to wakeup the card already */
295 if (adapter->pm_wakeup_fw_try)
296 break;
297 if (adapter->ps_state != PS_STATE_AWAKE ||
298 adapter->tx_lock_flag)
299 break;
300
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530301 if ((!adapter->scan_chan_gap_enabled &&
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530302 adapter->scan_processing) || adapter->data_sent ||
Xinming Huba101ad2015-06-22 19:06:10 +0530303 mwifiex_is_tdls_chan_switching
304 (mwifiex_get_priv(adapter,
305 MWIFIEX_BSS_ROLE_STA)) ||
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530306 (mwifiex_wmm_lists_empty(adapter) &&
Avinash Patila1777322015-06-22 19:06:17 +0530307 mwifiex_bypass_txlist_empty(adapter) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530308 skb_queue_empty(&adapter->tx_data_q))) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700309 if (adapter->cmd_sent || adapter->curr_cmd ||
Xinming Huba101ad2015-06-22 19:06:10 +0530310 !mwifiex_is_send_cmd_allowed
311 (mwifiex_get_priv(adapter,
312 MWIFIEX_BSS_ROLE_STA)) ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700313 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700314 break;
315 }
316 }
317
Amitkumar Karwar20474122014-04-14 15:31:05 -0700318 /* Check for event */
319 if (adapter->event_received) {
320 adapter->event_received = false;
321 mwifiex_process_event(adapter);
322 }
323
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700324 /* Check for Cmd Resp */
325 if (adapter->cmd_resp_received) {
326 adapter->cmd_resp_received = false;
327 mwifiex_process_cmdresp(adapter);
328
329 /* call mwifiex back when init_fw is done */
330 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
331 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
332 mwifiex_init_fw_complete(adapter);
333 }
334 }
335
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700336 /* Check if we need to confirm Sleep Request
337 received previously */
338 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
339 if (!adapter->cmd_sent && !adapter->curr_cmd)
340 mwifiex_check_ps_cond(adapter);
341 }
342
343 /* * The ps_state may have been changed during processing of
344 * Sleep Request event.
345 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700346 if ((adapter->ps_state == PS_STATE_SLEEP) ||
347 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
348 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
Shengzhen Li04c7b362015-02-11 23:12:24 +0530349 adapter->tx_lock_flag){
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700350 continue;
Shengzhen Li04c7b362015-02-11 23:12:24 +0530351 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700352
Xinming Huba101ad2015-06-22 19:06:10 +0530353 if (!adapter->cmd_sent && !adapter->curr_cmd &&
354 mwifiex_is_send_cmd_allowed
355 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700356 if (mwifiex_exec_next_cmd(adapter) == -1) {
357 ret = -1;
358 break;
359 }
360 }
361
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530362 if ((adapter->scan_chan_gap_enabled ||
Amitkumar Karward8d91252014-09-12 20:08:58 +0530363 !adapter->scan_processing) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530364 !adapter->data_sent &&
365 !skb_queue_empty(&adapter->tx_data_q)) {
366 mwifiex_process_tx_queue(adapter);
367 if (adapter->hs_activated) {
368 adapter->is_hs_configured = false;
369 mwifiex_hs_activated_event
370 (mwifiex_get_priv
371 (adapter, MWIFIEX_BSS_ROLE_ANY),
372 false);
373 }
374 }
375
376 if ((adapter->scan_chan_gap_enabled ||
377 !adapter->scan_processing) &&
Avinash Patila1777322015-06-22 19:06:17 +0530378 !adapter->data_sent &&
379 !mwifiex_bypass_txlist_empty(adapter) &&
380 !mwifiex_is_tdls_chan_switching
381 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
382 mwifiex_process_bypass_tx(adapter);
383 if (adapter->hs_activated) {
384 adapter->is_hs_configured = false;
385 mwifiex_hs_activated_event
386 (mwifiex_get_priv
387 (adapter, MWIFIEX_BSS_ROLE_ANY),
388 false);
389 }
390 }
391
392 if ((adapter->scan_chan_gap_enabled ||
393 !adapter->scan_processing) &&
Xinming Huba101ad2015-06-22 19:06:10 +0530394 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
395 !mwifiex_is_tdls_chan_switching
396 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700397 mwifiex_wmm_process_tx(adapter);
398 if (adapter->hs_activated) {
399 adapter->is_hs_configured = false;
400 mwifiex_hs_activated_event
401 (mwifiex_get_priv
402 (adapter, MWIFIEX_BSS_ROLE_ANY),
403 false);
404 }
405 }
406
407 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700408 !adapter->curr_cmd && !is_command_pending(adapter) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530409 (mwifiex_wmm_lists_empty(adapter) &&
Avinash Patila1777322015-06-22 19:06:17 +0530410 mwifiex_bypass_txlist_empty(adapter) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530411 skb_queue_empty(&adapter->tx_data_q))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700412 if (!mwifiex_send_null_packet
413 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
414 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
415 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
416 adapter->delay_null_pkt = false;
417 adapter->ps_state = PS_STATE_SLEEP;
418 }
419 break;
420 }
421 } while (true);
422
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700423 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Cathy Luo91457ea2015-04-17 04:18:29 -0700424 if (adapter->more_task_flag) {
425 adapter->more_task_flag = false;
426 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700427 goto process_start;
Cathy Luo91457ea2015-04-17 04:18:29 -0700428 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700429 adapter->mwifiex_processing = false;
430 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
431
432exit_main_proc:
433 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
434 mwifiex_shutdown_drv(adapter);
435 return ret;
436}
Bing Zhao601216e2012-11-05 16:59:15 -0800437EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700438
439/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700440 * This function frees the adapter structure.
441 *
442 * Additionally, this closes the netlink socket, frees the timers
443 * and private structures.
444 */
445static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
446{
447 if (!adapter) {
448 pr_err("%s: adapter is NULL\n", __func__);
449 return;
450 }
451
452 mwifiex_unregister(adapter);
453 pr_debug("info: %s: free adapter\n", __func__);
454}
455
456/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700457 * This function cancels all works in the queue and destroys
458 * the main workqueue.
459 */
460static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
461{
462 flush_workqueue(adapter->workqueue);
463 destroy_workqueue(adapter->workqueue);
464 adapter->workqueue = NULL;
Avinash Patil6e251172014-09-12 20:08:59 +0530465
466 if (adapter->rx_workqueue) {
467 flush_workqueue(adapter->rx_workqueue);
468 destroy_workqueue(adapter->rx_workqueue);
469 adapter->rx_workqueue = NULL;
470 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700471}
472
473/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700474 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700475 *
476 * The main initialization steps followed are -
477 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700478 * - Issue the init commands to firmware
479 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700480static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700481{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800482 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700483 char fmt[64];
484 struct mwifiex_private *priv;
485 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700486 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700487 struct semaphore *sem = adapter->card_sem;
488 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800489 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700490
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700491 if (!firmware) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530492 mwifiex_dbg(adapter, ERROR,
493 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700494 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700495 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700496
497 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
498 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700499 fw.fw_buf = (u8 *) adapter->firmware->data;
500 fw.fw_len = adapter->firmware->size;
501
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700502 if (adapter->if_ops.dnld_fw)
503 ret = adapter->if_ops.dnld_fw(adapter, &fw);
504 else
505 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700506 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700507 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700508
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530509 mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700510
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700511 if (cal_data_cfg) {
512 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
513 adapter->dev)) < 0)
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530514 mwifiex_dbg(adapter, ERROR,
515 "Cal data request_firmware() failed\n");
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700516 }
517
Daniel Drake232fde02013-07-13 10:57:10 -0400518 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700519 if (adapter->if_ops.enable_int) {
520 if (adapter->if_ops.enable_int(adapter))
521 goto err_dnld_fw;
522 }
Daniel Drake232fde02013-07-13 10:57:10 -0400523
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700524 adapter->init_wait_q_woken = false;
525 ret = mwifiex_init_fw(adapter);
526 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700527 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700528 } else if (!ret) {
529 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
530 goto done;
531 }
532 /* Wait for mwifiex_init to complete */
533 wait_event_interruptible(adapter->init_wait_q,
534 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700535 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700536 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700537
Avinash Patild6bffe82012-05-08 18:30:15 -0700538 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
539 if (mwifiex_register_cfg80211(adapter)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530540 mwifiex_dbg(adapter, ERROR,
541 "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800542 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700543 }
544
Avinash Patilbf354432014-10-31 16:08:26 +0530545 if (mwifiex_init_channel_scan_gap(adapter)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530546 mwifiex_dbg(adapter, ERROR,
547 "could not init channel stats table\n");
Avinash Patilbf354432014-10-31 16:08:26 +0530548 goto err_init_fw;
549 }
550
Avinash Patil0013c7c2014-11-05 19:38:11 +0530551 if (driver_mode) {
552 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
553 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
554 }
555
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700556 rtnl_lock();
557 /* Create station interface by default */
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100558 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800559 NL80211_IFTYPE_STATION, NULL, NULL);
560 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530561 mwifiex_dbg(adapter, ERROR,
562 "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800563 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700564 goto err_add_intf;
565 }
Avinash Patil0013c7c2014-11-05 19:38:11 +0530566
567 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100568 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
Avinash Patil0013c7c2014-11-05 19:38:11 +0530569 NL80211_IFTYPE_AP, NULL, NULL);
570 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530571 mwifiex_dbg(adapter, ERROR,
572 "cannot create AP interface\n");
Avinash Patil0013c7c2014-11-05 19:38:11 +0530573 rtnl_unlock();
574 goto err_add_intf;
575 }
576 }
577
578 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100579 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
Avinash Patil0013c7c2014-11-05 19:38:11 +0530580 NL80211_IFTYPE_P2P_CLIENT, NULL,
581 NULL);
582 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530583 mwifiex_dbg(adapter, ERROR,
584 "cannot create p2p client interface\n");
Avinash Patil0013c7c2014-11-05 19:38:11 +0530585 rtnl_unlock();
586 goto err_add_intf;
587 }
588 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700589 rtnl_unlock();
590
591 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530592 mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700593 goto done;
594
595err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700596 wiphy_unregister(adapter->wiphy);
597 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700598err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400599 if (adapter->if_ops.disable_int)
600 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700601err_dnld_fw:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530602 mwifiex_dbg(adapter, ERROR,
603 "info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700604 if (adapter->if_ops.unregister_dev)
605 adapter->if_ops.unregister_dev(adapter);
606
Amitkumar Karwar71973252014-12-31 02:36:38 -0800607 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700608 pr_debug("info: %s: shutdown mwifiex\n", __func__);
609 adapter->init_wait_q_woken = false;
610
611 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
612 wait_event_interruptible(adapter->init_wait_q,
613 adapter->init_wait_q_woken);
614 }
615 adapter->surprise_removed = true;
616 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700617 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700618done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700619 if (adapter->cal_data) {
620 release_firmware(adapter->cal_data);
621 adapter->cal_data = NULL;
622 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700623 if (adapter->firmware) {
624 release_firmware(adapter->firmware);
625 adapter->firmware = NULL;
626 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700627 if (init_failed)
628 mwifiex_free_adapter(adapter);
629 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700630 return;
631}
632
633/*
634 * This function initializes the hardware and gets firmware.
635 */
636static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
637{
638 int ret;
639
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700640 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
641 adapter->dev, GFP_KERNEL, adapter,
642 mwifiex_fw_dpc);
643 if (ret < 0)
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530644 mwifiex_dbg(adapter, ERROR,
645 "request_firmware_nowait error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700646 return ret;
647}
648
649/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700650 * CFG802.11 network device handler for open.
651 *
652 * Starts the data queue.
653 */
654static int
655mwifiex_open(struct net_device *dev)
656{
Johannes Bergea2325b2015-01-19 15:54:36 +0530657 netif_carrier_off(dev);
658
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700659 return 0;
660}
661
662/*
663 * CFG802.11 network device handler for close.
664 */
665static int
666mwifiex_close(struct net_device *dev)
667{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700668 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
669
670 if (priv->scan_request) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530671 mwifiex_dbg(priv->adapter, INFO,
672 "aborting scan on ndo_stop\n");
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700673 cfg80211_scan_done(priv->scan_request, 1);
674 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700675 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700676 }
677
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700678 return 0;
679}
680
Avinash Patila1777322015-06-22 19:06:17 +0530681static bool
682mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
683 struct sk_buff *skb)
684{
685 struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
686
687 if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
688 mwifiex_is_skb_mgmt_frame(skb) ||
689 (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
690 ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
691 (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
692 mwifiex_dbg(priv->adapter, DATA,
693 "bypass txqueue; eth type %#x, mgmt %d\n",
694 ntohs(eth_hdr->h_proto),
695 mwifiex_is_skb_mgmt_frame(skb));
696 return true;
697 }
698
699 return false;
700}
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700701/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700702 * Add buffer into wmm tx queue and queue work to transmit it.
703 */
704int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
705{
Avinash Patil47411a02012-11-01 18:44:16 -0700706 struct netdev_queue *txq;
707 int index = mwifiex_1d_to_wmm_queue[skb->priority];
708
709 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
710 txq = netdev_get_tx_queue(priv->netdev, index);
711 if (!netif_tx_queue_stopped(txq)) {
712 netif_tx_stop_queue(txq);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530713 mwifiex_dbg(priv->adapter, DATA,
714 "stop queue: %d\n", index);
Avinash Patil47411a02012-11-01 18:44:16 -0700715 }
716 }
717
Avinash Patila1777322015-06-22 19:06:17 +0530718 if (mwifiex_bypass_tx_queue(priv, skb)) {
719 atomic_inc(&priv->adapter->tx_pending);
720 atomic_inc(&priv->adapter->bypass_tx_pending);
721 mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
722 } else {
723 atomic_inc(&priv->adapter->tx_pending);
724 mwifiex_wmm_add_buf_txqueue(priv, skb);
725 }
Stone Piaoe39faa72012-09-25 20:23:32 -0700726
Shengzhen Lib2713f62015-03-13 17:37:54 +0530727 mwifiex_queue_main_work(priv->adapter);
Stone Piaoe39faa72012-09-25 20:23:32 -0700728
729 return 0;
730}
731
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800732struct sk_buff *
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800733mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800734 struct sk_buff *skb, u8 flag, u64 *cookie)
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800735{
736 struct sk_buff *orig_skb = skb;
737 struct mwifiex_txinfo *tx_info, *orig_tx_info;
738
739 skb = skb_clone(skb, GFP_ATOMIC);
740 if (skb) {
741 unsigned long flags;
742 int id;
743
744 spin_lock_irqsave(&priv->ack_status_lock, flags);
745 id = idr_alloc(&priv->ack_status_frames, orig_skb,
746 1, 0xff, GFP_ATOMIC);
747 spin_unlock_irqrestore(&priv->ack_status_lock, flags);
748
749 if (id >= 0) {
750 tx_info = MWIFIEX_SKB_TXCB(skb);
751 tx_info->ack_frame_id = id;
752 tx_info->flags |= flag;
753 orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
754 orig_tx_info->ack_frame_id = id;
755 orig_tx_info->flags |= flag;
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800756
757 if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
758 orig_tx_info->cookie = *cookie;
759
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800760 } else if (skb_shared(skb)) {
761 kfree_skb(orig_skb);
762 } else {
763 kfree_skb(skb);
764 skb = orig_skb;
765 }
766 } else {
767 /* couldn't clone -- lose tx status ... */
768 skb = orig_skb;
769 }
770
771 return skb;
772}
773
Stone Piaoe39faa72012-09-25 20:23:32 -0700774/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700775 * CFG802.11 network device handler for data transmission.
776 */
777static int
778mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
779{
780 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700781 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700782 struct mwifiex_txinfo *tx_info;
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800783 bool multicast;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700784
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530785 mwifiex_dbg(priv->adapter, DATA,
786 "data: %lu BSS(%d-%d): Data <= kernel\n",
787 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700788
789 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200790 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700791 priv->stats.tx_dropped++;
792 return 0;
793 }
794 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530795 mwifiex_dbg(priv->adapter, ERROR,
796 "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200797 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700798 priv->stats.tx_dropped++;
799 return 0;
800 }
801 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530802 mwifiex_dbg(priv->adapter, DATA,
803 "data: Tx: insufficient skb headroom %d\n",
804 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700805 /* Insufficient skb headroom - allocate a new skb */
806 new_skb =
807 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
808 if (unlikely(!new_skb)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530809 mwifiex_dbg(priv->adapter, ERROR,
810 "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200811 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700812 priv->stats.tx_dropped++;
813 return 0;
814 }
815 kfree_skb(skb);
816 skb = new_skb;
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530817 mwifiex_dbg(priv->adapter, INFO,
818 "info: new skb headroomd %d\n",
819 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700820 }
821
822 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700823 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800824 tx_info->bss_num = priv->bss_num;
825 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800826 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700827
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800828 multicast = is_multicast_ether_addr(skb->data);
829
830 if (unlikely(!multicast && skb->sk &&
831 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
832 priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
833 skb = mwifiex_clone_skb_for_tx_status(priv,
834 skb,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800835 MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800836
Avinash Patil47411a02012-11-01 18:44:16 -0700837 /* Record the current time the packet was queued; used to
838 * determine the amount of time the packet was queued in
839 * the driver before it was sent to the firmware.
840 * The delay is then sent along with the packet to the
841 * firmware for aggregate delay calculation for stats and
842 * MSDU lifetime expiry.
843 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100844 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700845
Avinash Patil9927baa2014-11-13 21:54:16 +0530846 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
847 priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
848 !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
849 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
850 mwifiex_tdls_check_tx(priv, skb);
851 }
852
Stone Piaoe39faa72012-09-25 20:23:32 -0700853 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700854
855 return 0;
856}
857
858/*
859 * CFG802.11 network device handler for setting MAC address.
860 */
861static int
862mwifiex_set_mac_address(struct net_device *dev, void *addr)
863{
864 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700865 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700866 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700867
868 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
869
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700870 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800871 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
872 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700873
874 if (!ret)
875 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
876 else
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530877 mwifiex_dbg(priv->adapter, ERROR,
878 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700879
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700880 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
881
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700882 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700883}
884
885/*
886 * CFG802.11 network device handler for setting multicast list.
887 */
888static void mwifiex_set_multicast_list(struct net_device *dev)
889{
890 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700891 struct mwifiex_multicast_list mcast_list;
892
893 if (dev->flags & IFF_PROMISC) {
894 mcast_list.mode = MWIFIEX_PROMISC_MODE;
895 } else if (dev->flags & IFF_ALLMULTI ||
896 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
897 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
898 } else {
899 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400900 mcast_list.num_multicast_addr =
901 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700902 }
903 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700904}
905
906/*
907 * CFG802.11 network device handler for transmission timeout.
908 */
909static void
910mwifiex_tx_timeout(struct net_device *dev)
911{
912 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
913
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700914 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800915 priv->tx_timeout_cnt++;
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530916 mwifiex_dbg(priv->adapter, ERROR,
917 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
918 jiffies, priv->tx_timeout_cnt, priv->bss_type,
919 priv->bss_num);
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800920 mwifiex_set_trans_start(dev);
921
922 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
923 priv->adapter->if_ops.card_reset) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530924 mwifiex_dbg(priv->adapter, ERROR,
925 "tx_timeout_cnt exceeds threshold.\t"
926 "Triggering card reset!\n");
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800927 priv->adapter->if_ops.card_reset(priv->adapter);
928 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700929}
930
Amitkumar Karwarfc697152015-05-26 06:34:31 -0700931void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
Xinming Hu11cd07a2014-12-23 19:14:10 +0530932{
933 void *p;
934 char drv_version[64];
935 struct usb_card_rec *cardp;
936 struct sdio_mmc_card *sdio_card;
937 struct mwifiex_private *priv;
938 int i, idx;
939 struct netdev_queue *txq;
940 struct mwifiex_debug_info *debug_info;
941
942 if (adapter->drv_info_dump) {
943 vfree(adapter->drv_info_dump);
Amitkumar Karwar0769b272015-05-26 06:34:28 -0700944 adapter->drv_info_dump = NULL;
Xinming Hu11cd07a2014-12-23 19:14:10 +0530945 adapter->drv_info_size = 0;
946 }
947
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -0700948 mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +0530949
950 adapter->drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
951
952 if (!adapter->drv_info_dump)
953 return;
954
955 p = (char *)(adapter->drv_info_dump);
956 p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
957
958 mwifiex_drv_get_driver_version(adapter, drv_version,
959 sizeof(drv_version) - 1);
960 p += sprintf(p, "driver_version = %s\n", drv_version);
961
962 if (adapter->iface_type == MWIFIEX_USB) {
963 cardp = (struct usb_card_rec *)adapter->card;
964 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
965 atomic_read(&cardp->tx_cmd_urb_pending));
966 p += sprintf(p, "tx_data_urb_pending = %d\n",
967 atomic_read(&cardp->tx_data_urb_pending));
968 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
969 atomic_read(&cardp->rx_cmd_urb_pending));
970 p += sprintf(p, "rx_data_urb_pending = %d\n",
971 atomic_read(&cardp->rx_data_urb_pending));
972 }
973
974 p += sprintf(p, "tx_pending = %d\n",
975 atomic_read(&adapter->tx_pending));
976 p += sprintf(p, "rx_pending = %d\n",
977 atomic_read(&adapter->rx_pending));
978
979 if (adapter->iface_type == MWIFIEX_SDIO) {
980 sdio_card = (struct sdio_mmc_card *)adapter->card;
981 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
982 sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
983 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
984 sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
985 }
986
987 for (i = 0; i < adapter->priv_num; i++) {
988 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
989 continue;
990 priv = adapter->priv[i];
991 p += sprintf(p, "\n[interface : \"%s\"]\n",
992 priv->netdev->name);
993 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
994 atomic_read(&priv->wmm_tx_pending[0]));
995 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
996 atomic_read(&priv->wmm_tx_pending[1]));
997 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
998 atomic_read(&priv->wmm_tx_pending[2]));
999 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1000 atomic_read(&priv->wmm_tx_pending[3]));
1001 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1002 "Disconnected" : "Connected");
1003 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1004 ? "on" : "off"));
1005 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1006 txq = netdev_get_tx_queue(priv->netdev, idx);
1007 p += sprintf(p, "tx queue %d:%s ", idx,
1008 netif_tx_queue_stopped(txq) ?
1009 "stopped" : "started");
1010 }
1011 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1012 priv->netdev->name, priv->num_tx_timeout);
1013 }
1014
Xinming Hu809c6ea2014-12-23 19:14:11 +05301015 if (adapter->iface_type == MWIFIEX_SDIO) {
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -07001016 p += sprintf(p, "\n=== SDIO register dump===\n");
Xinming Hu809c6ea2014-12-23 19:14:11 +05301017 if (adapter->if_ops.reg_dump)
1018 p += adapter->if_ops.reg_dump(adapter, p);
1019 }
1020
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -07001021 p += sprintf(p, "\n=== more debug information\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +05301022 debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
1023 if (debug_info) {
1024 for (i = 0; i < adapter->priv_num; i++) {
1025 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1026 continue;
1027 priv = adapter->priv[i];
1028 mwifiex_get_debug_info(priv, debug_info);
1029 p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
1030 break;
1031 }
1032 kfree(debug_info);
1033 }
1034
1035 adapter->drv_info_size = p - adapter->drv_info_dump;
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -07001036 mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +05301037}
Amitkumar Karwarfc697152015-05-26 06:34:31 -07001038EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
Xinming Hu11cd07a2014-12-23 19:14:10 +05301039
Amitkumar Karwar57670ee2015-05-26 06:34:32 -07001040void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
1041{
1042 u8 idx, *dump_data, *fw_dump_ptr;
1043 u32 dump_len;
1044
1045 dump_len = (strlen("========Start dump driverinfo========\n") +
1046 adapter->drv_info_size +
1047 strlen("\n========End dump========\n"));
1048
1049 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1050 struct memory_type_mapping *entry =
1051 &adapter->mem_type_mapping_tbl[idx];
1052
1053 if (entry->mem_ptr) {
1054 dump_len += (strlen("========Start dump ") +
1055 strlen(entry->mem_name) +
1056 strlen("========\n") +
1057 (entry->mem_size + 1) +
1058 strlen("\n========End dump========\n"));
1059 }
1060 }
1061
1062 dump_data = vzalloc(dump_len + 1);
1063 if (!dump_data)
1064 goto done;
1065
1066 fw_dump_ptr = dump_data;
1067
1068 /* Dump all the memory data into single file, a userspace script will
1069 * be used to split all the memory data to multiple files
1070 */
1071 mwifiex_dbg(adapter, MSG,
1072 "== mwifiex dump information to /sys/class/devcoredump start");
1073
1074 strcpy(fw_dump_ptr, "========Start dump driverinfo========\n");
1075 fw_dump_ptr += strlen("========Start dump driverinfo========\n");
1076 memcpy(fw_dump_ptr, adapter->drv_info_dump, adapter->drv_info_size);
1077 fw_dump_ptr += adapter->drv_info_size;
1078 strcpy(fw_dump_ptr, "\n========End dump========\n");
1079 fw_dump_ptr += strlen("\n========End dump========\n");
1080
1081 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1082 struct memory_type_mapping *entry =
1083 &adapter->mem_type_mapping_tbl[idx];
1084
1085 if (entry->mem_ptr) {
1086 strcpy(fw_dump_ptr, "========Start dump ");
1087 fw_dump_ptr += strlen("========Start dump ");
1088
1089 strcpy(fw_dump_ptr, entry->mem_name);
1090 fw_dump_ptr += strlen(entry->mem_name);
1091
1092 strcpy(fw_dump_ptr, "========\n");
1093 fw_dump_ptr += strlen("========\n");
1094
1095 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1096 fw_dump_ptr += entry->mem_size;
1097
1098 strcpy(fw_dump_ptr, "\n========End dump========\n");
1099 fw_dump_ptr += strlen("\n========End dump========\n");
1100 }
1101 }
1102
1103 /* device dump data will be free in device coredump release function
1104 * after 5 min
1105 */
1106 dev_coredumpv(adapter->dev, dump_data, dump_len, GFP_KERNEL);
1107 mwifiex_dbg(adapter, MSG,
1108 "== mwifiex dump information to /sys/class/devcoredump end");
1109
1110done:
1111 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1112 struct memory_type_mapping *entry =
1113 &adapter->mem_type_mapping_tbl[idx];
1114
1115 if (entry->mem_ptr) {
1116 vfree(entry->mem_ptr);
1117 entry->mem_ptr = NULL;
1118 }
1119 entry->mem_size = 0;
1120 }
1121
1122 if (adapter->drv_info_dump) {
1123 vfree(adapter->drv_info_dump);
1124 adapter->drv_info_dump = NULL;
1125 adapter->drv_info_size = 0;
1126 }
1127}
1128EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1129
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001130/*
1131 * CFG802.11 network device handler for statistics retrieval.
1132 */
1133static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1134{
1135 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1136
1137 return &priv->stats;
1138}
1139
Avinash Patil47411a02012-11-01 18:44:16 -07001140static u16
Jason Wangf663dd92014-01-10 16:18:26 +08001141mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +01001142 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -07001143{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001144 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -07001145 return mwifiex_1d_to_wmm_queue[skb->priority];
1146}
1147
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001148/* Network device handlers */
1149static const struct net_device_ops mwifiex_netdev_ops = {
1150 .ndo_open = mwifiex_open,
1151 .ndo_stop = mwifiex_close,
1152 .ndo_start_xmit = mwifiex_hard_start_xmit,
1153 .ndo_set_mac_address = mwifiex_set_mac_address,
1154 .ndo_tx_timeout = mwifiex_tx_timeout,
1155 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001156 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -07001157 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001158};
1159
1160/*
1161 * This function initializes the private structure parameters.
1162 *
1163 * The following wait queues are initialized -
1164 * - IOCTL wait queue
1165 * - Command wait queue
1166 * - Statistics wait queue
1167 *
1168 * ...and the following default parameters are set -
1169 * - Current key index : Set to 0
1170 * - Rate index : Set to auto
1171 * - Media connected : Set to disconnected
1172 * - Adhoc link sensed : Set to false
1173 * - Nick name : Set to null
1174 * - Number of Tx timeout : Set to 0
1175 * - Device address : Set to current address
Xinming Hucbf6e052014-12-23 19:14:07 +05301176 * - Rx histogram statistc : Set to 0
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001177 *
1178 * In addition, the CFG80211 work queue is also created.
1179 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001180void mwifiex_init_priv_params(struct mwifiex_private *priv,
Avinash Patil047eaaf2015-01-28 15:42:05 +05301181 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001182{
1183 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -07001184 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001185 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001186 priv->current_key_index = 0;
1187 priv->media_connected = false;
Avinash Patilede98bf2012-05-08 18:30:28 -07001188 memset(priv->mgmt_ie, 0,
1189 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -07001190 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1191 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1192 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
Avinash Patil2ade5662015-01-28 15:54:20 +05301193 priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001194 priv->num_tx_timeout = 0;
Avinash Patil8d05eb22015-01-28 15:42:01 +05301195 ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001196 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
Xinming Hucbf6e052014-12-23 19:14:07 +05301197
1198 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1199 GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1200 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1201 if (priv->hist_data)
1202 mwifiex_hist_data_reset(priv);
1203 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001204}
1205
1206/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001207 * This function check if command is pending.
1208 */
1209int is_command_pending(struct mwifiex_adapter *adapter)
1210{
1211 unsigned long flags;
1212 int is_cmd_pend_q_empty;
1213
1214 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1215 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1216 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1217
1218 return !is_cmd_pend_q_empty;
1219}
1220
1221/*
Avinash Patil6e251172014-09-12 20:08:59 +05301222 * This is the RX work queue function.
1223 *
1224 * It handles the RX operations.
1225 */
1226static void mwifiex_rx_work_queue(struct work_struct *work)
1227{
1228 struct mwifiex_adapter *adapter =
1229 container_of(work, struct mwifiex_adapter, rx_work);
1230
1231 if (adapter->surprise_removed)
1232 return;
1233 mwifiex_process_rx(adapter);
1234}
1235
1236/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001237 * This is the main work queue function.
1238 *
1239 * It handles the main process, which in turn handles the complete
1240 * driver operations.
1241 */
1242static void mwifiex_main_work_queue(struct work_struct *work)
1243{
1244 struct mwifiex_adapter *adapter =
1245 container_of(work, struct mwifiex_adapter, main_work);
1246
1247 if (adapter->surprise_removed)
1248 return;
1249 mwifiex_main_process(adapter);
1250}
1251
1252/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001253 * This function adds the card.
1254 *
1255 * This function follows the following major steps to set up the device -
1256 * - Initialize software. This includes probing the card, registering
1257 * the interface operations table, and allocating/initializing the
1258 * adapter structure
1259 * - Set up the netlink socket
1260 * - Create and start the main work queue
1261 * - Register the device
1262 * - Initialize firmware and hardware
1263 * - Add logical interfaces
1264 */
1265int
1266mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001267 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001268{
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001269 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001270
1271 if (down_interruptible(sem))
1272 goto exit_sem_err;
1273
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001274 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001275 pr_err("%s: software init failed\n", __func__);
1276 goto err_init_sw;
1277 }
1278
Amitkumar Karward930fae2011-10-11 17:41:21 -07001279 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001280 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001281
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001282 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001283 adapter->surprise_removed = false;
1284 init_waitqueue_head(&adapter->init_wait_q);
1285 adapter->is_suspended = false;
1286 adapter->hs_activated = false;
1287 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001288 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001289 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -07001290 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001291
Avinash Patilec4a16b2014-11-05 17:04:27 +05301292 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +05301293 adapter->rx_work_enabled = true;
1294 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
1295 }
1296
Amitkumar Karwar448a71c2013-10-11 18:33:04 -07001297 adapter->workqueue =
1298 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1299 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001300 if (!adapter->workqueue)
1301 goto err_kmalloc;
1302
1303 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Avinash Patil6e251172014-09-12 20:08:59 +05301304
1305 if (adapter->rx_work_enabled) {
1306 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1307 WQ_HIGHPRI |
1308 WQ_MEM_RECLAIM |
1309 WQ_UNBOUND, 1);
1310 if (!adapter->rx_workqueue)
1311 goto err_kmalloc;
1312
1313 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1314 }
1315
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001316 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -04001317 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001318 if (adapter->if_ops.register_dev(adapter)) {
1319 pr_err("%s: failed to register mwifiex device\n", __func__);
1320 goto err_registerdev;
1321 }
1322
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001323 if (mwifiex_init_hw_fw(adapter)) {
1324 pr_err("%s: firmware init failed\n", __func__);
1325 goto err_init_fw;
1326 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001327
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001328 return 0;
1329
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001330err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001331 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001332 if (adapter->if_ops.unregister_dev)
1333 adapter->if_ops.unregister_dev(adapter);
Amitkumar Karwar71973252014-12-31 02:36:38 -08001334 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001335 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1336 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001337
1338 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001339 wait_event_interruptible(adapter->init_wait_q,
1340 adapter->init_wait_q_woken);
1341 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001342err_registerdev:
1343 adapter->surprise_removed = true;
1344 mwifiex_terminate_workqueue(adapter);
1345err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001346 mwifiex_free_adapter(adapter);
1347
1348err_init_sw:
1349 up(sem);
1350
1351exit_sem_err:
1352 return -1;
1353}
1354EXPORT_SYMBOL_GPL(mwifiex_add_card);
1355
1356/*
1357 * This function removes the card.
1358 *
1359 * This function follows the following major steps to remove the device -
1360 * - Stop data traffic
1361 * - Shutdown firmware
1362 * - Remove the logical interfaces
1363 * - Terminate the work queue
1364 * - Unregister the device
1365 * - Free the adapter structure
1366 */
1367int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
1368{
1369 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001370 int i;
1371
1372 if (down_interruptible(sem))
1373 goto exit_sem_err;
1374
1375 if (!adapter)
1376 goto exit_remove;
1377
Daniel Drake232fde02013-07-13 10:57:10 -04001378 /* We can no longer handle interrupts once we start doing the teardown
1379 * below. */
1380 if (adapter->if_ops.disable_int)
1381 adapter->if_ops.disable_int(adapter);
1382
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001383 adapter->surprise_removed = true;
1384
Marc Yang9d2e85e2014-12-31 02:36:39 -08001385 mwifiex_terminate_workqueue(adapter);
1386
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001387 /* Stop data */
1388 for (i = 0; i < adapter->priv_num; i++) {
1389 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001390 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -07001391 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001392 if (netif_carrier_ok(priv->netdev))
1393 netif_carrier_off(priv->netdev);
1394 }
1395 }
1396
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301397 mwifiex_dbg(adapter, CMD,
1398 "cmd: calling mwifiex_shutdown_drv...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001399 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001400
1401 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001402 wait_event_interruptible(adapter->init_wait_q,
1403 adapter->init_wait_q_woken);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301404 mwifiex_dbg(adapter, CMD,
1405 "cmd: mwifiex_shutdown_drv done\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001406 if (atomic_read(&adapter->rx_pending) ||
1407 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001408 atomic_read(&adapter->cmd_pending)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301409 mwifiex_dbg(adapter, ERROR,
1410 "rx_pending=%d, tx_pending=%d,\t"
1411 "cmd_pending=%d\n",
1412 atomic_read(&adapter->rx_pending),
1413 atomic_read(&adapter->tx_pending),
1414 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001415 }
1416
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001417 for (i = 0; i < adapter->priv_num; i++) {
1418 priv = adapter->priv[i];
1419
1420 if (!priv)
1421 continue;
1422
1423 rtnl_lock();
Avinash Patil4facc342015-01-28 15:42:00 +05301424 if (priv->netdev &&
1425 priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1426 mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001427 rtnl_unlock();
1428 }
1429
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -08001430 wiphy_unregister(adapter->wiphy);
1431 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -07001432
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001433 /* Unregister device */
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301434 mwifiex_dbg(adapter, INFO,
1435 "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001436 if (adapter->if_ops.unregister_dev)
1437 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001438 /* Free adapter structure */
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301439 mwifiex_dbg(adapter, INFO,
1440 "info: free adapter\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001441 mwifiex_free_adapter(adapter);
1442
1443exit_remove:
1444 up(sem);
1445exit_sem_err:
1446 return 0;
1447}
1448EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1449
Joe Perches36925e52015-08-31 10:46:45 -07001450void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
1451 const char *fmt, ...)
1452{
1453 struct va_format vaf;
1454 va_list args;
1455
1456 if (!adapter->dev || !(adapter->debug_mask & mask))
1457 return;
1458
1459 va_start(args, fmt);
1460
1461 vaf.fmt = fmt;
1462 vaf.va = &args;
1463
1464 dev_info(adapter->dev, "%pV", &vaf);
1465
1466 va_end(args);
1467}
1468EXPORT_SYMBOL_GPL(_mwifiex_dbg);
1469
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001470/*
1471 * This function initializes the module.
1472 *
1473 * The debug FS is also initialized if configured.
1474 */
1475static int
1476mwifiex_init_module(void)
1477{
1478#ifdef CONFIG_DEBUG_FS
1479 mwifiex_debugfs_init();
1480#endif
1481 return 0;
1482}
1483
1484/*
1485 * This function cleans up the module.
1486 *
1487 * The debug FS is removed if available.
1488 */
1489static void
1490mwifiex_cleanup_module(void)
1491{
1492#ifdef CONFIG_DEBUG_FS
1493 mwifiex_debugfs_remove();
1494#endif
1495}
1496
1497module_init(mwifiex_init_module);
1498module_exit(mwifiex_cleanup_module);
1499
1500MODULE_AUTHOR("Marvell International Ltd.");
1501MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1502MODULE_VERSION(VERSION);
1503MODULE_LICENSE("GPL v2");