blob: db4925db39aa4f07d716040306d6041a037ae0a3 [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
chunfan chen7d7f07d2016-01-13 01:26:54 -0800135 if (adapter->nd_info) {
136 for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
137 kfree(adapter->nd_info->matches[i]);
138 kfree(adapter->nd_info);
139 adapter->nd_info = NULL;
140 }
141
Avinash Patilbf354432014-10-31 16:08:26 +0530142 vfree(adapter->chan_stats);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700143 kfree(adapter);
144 return 0;
145}
146
Shengzhen Lib2713f62015-03-13 17:37:54 +0530147void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
148{
149 unsigned long flags;
150
151 spin_lock_irqsave(&adapter->main_proc_lock, flags);
152 if (adapter->mwifiex_processing) {
153 adapter->more_task_flag = true;
154 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
155 } else {
156 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
157 queue_work(adapter->workqueue, &adapter->main_work);
158 }
159}
160EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
161
162static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
163{
164 unsigned long flags;
165
166 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
167 if (adapter->rx_processing) {
168 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
169 } else {
170 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
171 queue_work(adapter->rx_workqueue, &adapter->rx_work);
172 }
173}
174
Avinash Patil6e251172014-09-12 20:08:59 +0530175static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
176{
177 unsigned long flags;
178 struct sk_buff *skb;
Zhaoyang Liu92263a82015-03-13 17:37:58 +0530179 struct mwifiex_rxinfo *rx_info;
Avinash Patil6e251172014-09-12 20:08:59 +0530180
181 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
182 if (adapter->rx_processing || adapter->rx_locked) {
183 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
184 goto exit_rx_proc;
185 } else {
186 adapter->rx_processing = true;
187 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
188 }
189
190 /* Check for Rx data */
191 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
192 atomic_dec(&adapter->rx_pending);
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800193 if ((adapter->delay_main_work ||
194 adapter->iface_type == MWIFIEX_USB) &&
Avinash Patilb43a0d92014-09-29 21:44:14 +0530195 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530196 if (adapter->if_ops.submit_rem_rx_urbs)
197 adapter->if_ops.submit_rem_rx_urbs(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530198 adapter->delay_main_work = false;
Shengzhen Lib2713f62015-03-13 17:37:54 +0530199 mwifiex_queue_main_work(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530200 }
Zhaoyang Liu92263a82015-03-13 17:37:58 +0530201 rx_info = MWIFIEX_SKB_RXCB(skb);
202 if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
203 if (adapter->if_ops.deaggr_pkt)
204 adapter->if_ops.deaggr_pkt(adapter, skb);
205 dev_kfree_skb_any(skb);
206 } else {
207 mwifiex_handle_rx_packet(adapter, skb);
208 }
Avinash Patil6e251172014-09-12 20:08:59 +0530209 }
210 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
211 adapter->rx_processing = false;
212 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
213
Avinash Patil6e251172014-09-12 20:08:59 +0530214exit_rx_proc:
215 return 0;
216}
217
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700218/*
219 * The main process.
220 *
221 * This function is the main procedure of the driver and handles various driver
222 * operations. It runs in a loop and provides the core functionalities.
223 *
224 * The main responsibilities of this function are -
225 * - Ensure concurrency control
226 * - Handle pending interrupts and call interrupt handlers
227 * - Wake up the card if required
228 * - Handle command responses and call response handlers
229 * - Handle events and call event handlers
230 * - Execute pending commands
231 * - Transmit pending data packets
232 */
233int mwifiex_main_process(struct mwifiex_adapter *adapter)
234{
235 int ret = 0;
236 unsigned long flags;
237
238 spin_lock_irqsave(&adapter->main_proc_lock, flags);
239
240 /* Check if already processing */
Avinash Patila9adbcb2015-03-13 17:37:51 +0530241 if (adapter->mwifiex_processing || adapter->main_locked) {
Shengzhen Li04c7b362015-02-11 23:12:24 +0530242 adapter->more_task_flag = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700243 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
244 goto exit_main_proc;
245 } else {
246 adapter->mwifiex_processing = true;
Cathy Luo91457ea2015-04-17 04:18:29 -0700247 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700248 }
249process_start:
250 do {
251 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
252 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
253 break;
254
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800255 /* For non-USB interfaces, If we process interrupts first, it
256 * would increase RX pending even further. Avoid this by
257 * checking if rx_pending has crossed high threshold and
258 * schedule rx work queue and then process interrupts.
259 * For USB interface, there are no interrupts. We already have
260 * HIGH_RX_PENDING check in usb.c
Avinash Patil6e251172014-09-12 20:08:59 +0530261 */
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800262 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
263 adapter->iface_type != MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +0530264 adapter->delay_main_work = true;
Shengzhen Lib2713f62015-03-13 17:37:54 +0530265 mwifiex_queue_rx_work(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530266 break;
267 }
268
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700269 /* Handle pending interrupt if any */
270 if (adapter->int_status) {
271 if (adapter->hs_activated)
272 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700273 if (adapter->if_ops.process_int_status)
274 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700275 }
276
Avinash Patil6e251172014-09-12 20:08:59 +0530277 if (adapter->rx_work_enabled && adapter->data_received)
Shengzhen Lib2713f62015-03-13 17:37:54 +0530278 mwifiex_queue_rx_work(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530279
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700280 /* Need to wake up the card ? */
281 if ((adapter->ps_state == PS_STATE_SLEEP) &&
282 (adapter->pm_wakeup_card_req &&
283 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700284 (is_command_pending(adapter) ||
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530285 !skb_queue_empty(&adapter->tx_data_q) ||
Avinash Patila1777322015-06-22 19:06:17 +0530286 !mwifiex_bypass_txlist_empty(adapter) ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700287 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700288 adapter->pm_wakeup_fw_try = true;
Amitkumar Karwar46361872014-12-31 02:36:41 -0800289 mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700290 adapter->if_ops.wakeup(adapter);
291 continue;
292 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700293
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700294 if (IS_CARD_RX_RCVD(adapter)) {
Avinash Patil6e251172014-09-12 20:08:59 +0530295 adapter->data_received = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700296 adapter->pm_wakeup_fw_try = false;
Amitkumar Karwar6e9344f2015-03-12 00:38:40 -0700297 del_timer(&adapter->wakeup_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700298 if (adapter->ps_state == PS_STATE_SLEEP)
299 adapter->ps_state = PS_STATE_AWAKE;
300 } else {
301 /* We have tried to wakeup the card already */
302 if (adapter->pm_wakeup_fw_try)
303 break;
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700304 if (adapter->ps_state != PS_STATE_AWAKE)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700305 break;
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700306 if (adapter->tx_lock_flag) {
307 if (adapter->iface_type == MWIFIEX_USB) {
308 if (!adapter->usb_mc_setup)
309 break;
310 } else
311 break;
312 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700313
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530314 if ((!adapter->scan_chan_gap_enabled &&
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530315 adapter->scan_processing) || adapter->data_sent ||
Xinming Huba101ad2015-06-22 19:06:10 +0530316 mwifiex_is_tdls_chan_switching
317 (mwifiex_get_priv(adapter,
318 MWIFIEX_BSS_ROLE_STA)) ||
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530319 (mwifiex_wmm_lists_empty(adapter) &&
Avinash Patila1777322015-06-22 19:06:17 +0530320 mwifiex_bypass_txlist_empty(adapter) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530321 skb_queue_empty(&adapter->tx_data_q))) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700322 if (adapter->cmd_sent || adapter->curr_cmd ||
Xinming Huba101ad2015-06-22 19:06:10 +0530323 !mwifiex_is_send_cmd_allowed
324 (mwifiex_get_priv(adapter,
325 MWIFIEX_BSS_ROLE_STA)) ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700326 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700327 break;
328 }
329 }
330
Amitkumar Karwar20474122014-04-14 15:31:05 -0700331 /* Check for event */
332 if (adapter->event_received) {
333 adapter->event_received = false;
334 mwifiex_process_event(adapter);
335 }
336
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700337 /* Check for Cmd Resp */
338 if (adapter->cmd_resp_received) {
339 adapter->cmd_resp_received = false;
340 mwifiex_process_cmdresp(adapter);
341
342 /* call mwifiex back when init_fw is done */
343 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
344 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
345 mwifiex_init_fw_complete(adapter);
346 }
347 }
348
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700349 /* Check if we need to confirm Sleep Request
350 received previously */
351 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
352 if (!adapter->cmd_sent && !adapter->curr_cmd)
353 mwifiex_check_ps_cond(adapter);
354 }
355
356 /* * The ps_state may have been changed during processing of
357 * Sleep Request event.
358 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700359 if ((adapter->ps_state == PS_STATE_SLEEP) ||
360 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700361 (adapter->ps_state == PS_STATE_SLEEP_CFM)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700362 continue;
Shengzhen Li04c7b362015-02-11 23:12:24 +0530363 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700364
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700365 if (adapter->tx_lock_flag) {
366 if (adapter->iface_type == MWIFIEX_USB) {
367 if (!adapter->usb_mc_setup)
368 continue;
369 } else
370 continue;
371 }
372
Xinming Huba101ad2015-06-22 19:06:10 +0530373 if (!adapter->cmd_sent && !adapter->curr_cmd &&
374 mwifiex_is_send_cmd_allowed
375 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700376 if (mwifiex_exec_next_cmd(adapter) == -1) {
377 ret = -1;
378 break;
379 }
380 }
381
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700382 /** If USB Multi channel setup ongoing,
383 * wait for ready to tx data.
384 */
385 if (adapter->iface_type == MWIFIEX_USB &&
386 adapter->usb_mc_setup)
387 continue;
388
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530389 if ((adapter->scan_chan_gap_enabled ||
Amitkumar Karward8d91252014-09-12 20:08:58 +0530390 !adapter->scan_processing) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530391 !adapter->data_sent &&
392 !skb_queue_empty(&adapter->tx_data_q)) {
393 mwifiex_process_tx_queue(adapter);
394 if (adapter->hs_activated) {
395 adapter->is_hs_configured = false;
396 mwifiex_hs_activated_event
397 (mwifiex_get_priv
398 (adapter, MWIFIEX_BSS_ROLE_ANY),
399 false);
400 }
401 }
402
403 if ((adapter->scan_chan_gap_enabled ||
404 !adapter->scan_processing) &&
Avinash Patila1777322015-06-22 19:06:17 +0530405 !adapter->data_sent &&
406 !mwifiex_bypass_txlist_empty(adapter) &&
407 !mwifiex_is_tdls_chan_switching
408 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
409 mwifiex_process_bypass_tx(adapter);
410 if (adapter->hs_activated) {
411 adapter->is_hs_configured = false;
412 mwifiex_hs_activated_event
413 (mwifiex_get_priv
414 (adapter, MWIFIEX_BSS_ROLE_ANY),
415 false);
416 }
417 }
418
419 if ((adapter->scan_chan_gap_enabled ||
420 !adapter->scan_processing) &&
Xinming Huba101ad2015-06-22 19:06:10 +0530421 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
422 !mwifiex_is_tdls_chan_switching
423 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700424 mwifiex_wmm_process_tx(adapter);
425 if (adapter->hs_activated) {
426 adapter->is_hs_configured = false;
427 mwifiex_hs_activated_event
428 (mwifiex_get_priv
429 (adapter, MWIFIEX_BSS_ROLE_ANY),
430 false);
431 }
432 }
433
434 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700435 !adapter->curr_cmd && !is_command_pending(adapter) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530436 (mwifiex_wmm_lists_empty(adapter) &&
Avinash Patila1777322015-06-22 19:06:17 +0530437 mwifiex_bypass_txlist_empty(adapter) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530438 skb_queue_empty(&adapter->tx_data_q))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700439 if (!mwifiex_send_null_packet
440 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
441 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
442 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
443 adapter->delay_null_pkt = false;
444 adapter->ps_state = PS_STATE_SLEEP;
445 }
446 break;
447 }
448 } while (true);
449
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700450 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Cathy Luo91457ea2015-04-17 04:18:29 -0700451 if (adapter->more_task_flag) {
452 adapter->more_task_flag = false;
453 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700454 goto process_start;
Cathy Luo91457ea2015-04-17 04:18:29 -0700455 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700456 adapter->mwifiex_processing = false;
457 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
458
459exit_main_proc:
460 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
461 mwifiex_shutdown_drv(adapter);
462 return ret;
463}
Bing Zhao601216e2012-11-05 16:59:15 -0800464EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700465
466/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700467 * This function frees the adapter structure.
468 *
469 * Additionally, this closes the netlink socket, frees the timers
470 * and private structures.
471 */
472static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
473{
474 if (!adapter) {
475 pr_err("%s: adapter is NULL\n", __func__);
476 return;
477 }
478
479 mwifiex_unregister(adapter);
480 pr_debug("info: %s: free adapter\n", __func__);
481}
482
483/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700484 * This function cancels all works in the queue and destroys
485 * the main workqueue.
486 */
487static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
488{
489 flush_workqueue(adapter->workqueue);
490 destroy_workqueue(adapter->workqueue);
491 adapter->workqueue = NULL;
Avinash Patil6e251172014-09-12 20:08:59 +0530492
493 if (adapter->rx_workqueue) {
494 flush_workqueue(adapter->rx_workqueue);
495 destroy_workqueue(adapter->rx_workqueue);
496 adapter->rx_workqueue = NULL;
497 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700498}
499
500/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700501 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700502 *
503 * The main initialization steps followed are -
504 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700505 * - Issue the init commands to firmware
506 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700507static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700508{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800509 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700510 char fmt[64];
511 struct mwifiex_private *priv;
512 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700513 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700514 struct semaphore *sem = adapter->card_sem;
515 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800516 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700517
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700518 if (!firmware) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530519 mwifiex_dbg(adapter, ERROR,
520 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700521 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700522 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700523
524 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
525 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700526 fw.fw_buf = (u8 *) adapter->firmware->data;
527 fw.fw_len = adapter->firmware->size;
528
Wei-Ning Huang65c71ef2016-05-19 11:53:43 +0800529 if (adapter->if_ops.dnld_fw) {
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700530 ret = adapter->if_ops.dnld_fw(adapter, &fw);
Wei-Ning Huang65c71ef2016-05-19 11:53:43 +0800531 } else {
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700532 ret = mwifiex_dnld_fw(adapter, &fw);
Wei-Ning Huang65c71ef2016-05-19 11:53:43 +0800533 }
534
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700535 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700536 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700537
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530538 mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700539
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700540 if (cal_data_cfg) {
541 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
542 adapter->dev)) < 0)
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530543 mwifiex_dbg(adapter, ERROR,
544 "Cal data request_firmware() failed\n");
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700545 }
546
Daniel Drake232fde02013-07-13 10:57:10 -0400547 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700548 if (adapter->if_ops.enable_int) {
549 if (adapter->if_ops.enable_int(adapter))
550 goto err_dnld_fw;
551 }
Daniel Drake232fde02013-07-13 10:57:10 -0400552
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700553 adapter->init_wait_q_woken = false;
554 ret = mwifiex_init_fw(adapter);
555 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700556 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700557 } else if (!ret) {
558 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
559 goto done;
560 }
561 /* Wait for mwifiex_init to complete */
562 wait_event_interruptible(adapter->init_wait_q,
563 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700564 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700565 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700566
Avinash Patild6bffe82012-05-08 18:30:15 -0700567 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
568 if (mwifiex_register_cfg80211(adapter)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530569 mwifiex_dbg(adapter, ERROR,
570 "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800571 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700572 }
573
Avinash Patilbf354432014-10-31 16:08:26 +0530574 if (mwifiex_init_channel_scan_gap(adapter)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530575 mwifiex_dbg(adapter, ERROR,
576 "could not init channel stats table\n");
Avinash Patilbf354432014-10-31 16:08:26 +0530577 goto err_init_fw;
578 }
579
Avinash Patil0013c7c2014-11-05 19:38:11 +0530580 if (driver_mode) {
581 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
582 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
583 }
584
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700585 rtnl_lock();
586 /* Create station interface by default */
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100587 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800588 NL80211_IFTYPE_STATION, NULL, NULL);
589 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530590 mwifiex_dbg(adapter, ERROR,
591 "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800592 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700593 goto err_add_intf;
594 }
Avinash Patil0013c7c2014-11-05 19:38:11 +0530595
596 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100597 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
Avinash Patil0013c7c2014-11-05 19:38:11 +0530598 NL80211_IFTYPE_AP, NULL, NULL);
599 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530600 mwifiex_dbg(adapter, ERROR,
601 "cannot create AP interface\n");
Avinash Patil0013c7c2014-11-05 19:38:11 +0530602 rtnl_unlock();
603 goto err_add_intf;
604 }
605 }
606
607 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100608 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
Avinash Patil0013c7c2014-11-05 19:38:11 +0530609 NL80211_IFTYPE_P2P_CLIENT, NULL,
610 NULL);
611 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530612 mwifiex_dbg(adapter, ERROR,
613 "cannot create p2p client interface\n");
Avinash Patil0013c7c2014-11-05 19:38:11 +0530614 rtnl_unlock();
615 goto err_add_intf;
616 }
617 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700618 rtnl_unlock();
619
620 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530621 mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700622 goto done;
623
624err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700625 wiphy_unregister(adapter->wiphy);
626 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700627err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400628 if (adapter->if_ops.disable_int)
629 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700630err_dnld_fw:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530631 mwifiex_dbg(adapter, ERROR,
632 "info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700633 if (adapter->if_ops.unregister_dev)
634 adapter->if_ops.unregister_dev(adapter);
635
Amitkumar Karwar71973252014-12-31 02:36:38 -0800636 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700637 pr_debug("info: %s: shutdown mwifiex\n", __func__);
638 adapter->init_wait_q_woken = false;
639
640 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
641 wait_event_interruptible(adapter->init_wait_q,
642 adapter->init_wait_q_woken);
643 }
644 adapter->surprise_removed = true;
645 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700646 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700647done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700648 if (adapter->cal_data) {
649 release_firmware(adapter->cal_data);
650 adapter->cal_data = NULL;
651 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700652 if (adapter->firmware) {
653 release_firmware(adapter->firmware);
654 adapter->firmware = NULL;
655 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700656 if (init_failed)
657 mwifiex_free_adapter(adapter);
658 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700659 return;
660}
661
662/*
663 * This function initializes the hardware and gets firmware.
664 */
665static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
666{
667 int ret;
668
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700669 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
670 adapter->dev, GFP_KERNEL, adapter,
671 mwifiex_fw_dpc);
672 if (ret < 0)
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530673 mwifiex_dbg(adapter, ERROR,
674 "request_firmware_nowait error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700675 return ret;
676}
677
678/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700679 * CFG802.11 network device handler for open.
680 *
681 * Starts the data queue.
682 */
683static int
684mwifiex_open(struct net_device *dev)
685{
Johannes Bergea2325b2015-01-19 15:54:36 +0530686 netif_carrier_off(dev);
687
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700688 return 0;
689}
690
691/*
692 * CFG802.11 network device handler for close.
693 */
694static int
695mwifiex_close(struct net_device *dev)
696{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700697 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
698
699 if (priv->scan_request) {
Avraham Stern1d762502016-07-05 17:10:13 +0300700 struct cfg80211_scan_info info = {
701 .aborted = true,
702 };
703
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530704 mwifiex_dbg(priv->adapter, INFO,
705 "aborting scan on ndo_stop\n");
Avraham Stern1d762502016-07-05 17:10:13 +0300706 cfg80211_scan_done(priv->scan_request, &info);
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700707 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700708 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700709 }
710
Xinming Hueaf46b52016-04-18 06:42:55 -0700711 if (priv->sched_scanning) {
712 mwifiex_dbg(priv->adapter, INFO,
713 "aborting bgscan on ndo_stop\n");
714 mwifiex_stop_bg_scan(priv);
715 cfg80211_sched_scan_stopped(priv->wdev.wiphy);
716 }
717
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700718 return 0;
719}
720
Avinash Patila1777322015-06-22 19:06:17 +0530721static bool
722mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
723 struct sk_buff *skb)
724{
725 struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
726
727 if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
728 mwifiex_is_skb_mgmt_frame(skb) ||
729 (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
730 ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
731 (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
732 mwifiex_dbg(priv->adapter, DATA,
733 "bypass txqueue; eth type %#x, mgmt %d\n",
734 ntohs(eth_hdr->h_proto),
735 mwifiex_is_skb_mgmt_frame(skb));
736 return true;
737 }
738
739 return false;
740}
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700741/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700742 * Add buffer into wmm tx queue and queue work to transmit it.
743 */
744int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
745{
Avinash Patil47411a02012-11-01 18:44:16 -0700746 struct netdev_queue *txq;
747 int index = mwifiex_1d_to_wmm_queue[skb->priority];
748
749 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
750 txq = netdev_get_tx_queue(priv->netdev, index);
751 if (!netif_tx_queue_stopped(txq)) {
752 netif_tx_stop_queue(txq);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530753 mwifiex_dbg(priv->adapter, DATA,
754 "stop queue: %d\n", index);
Avinash Patil47411a02012-11-01 18:44:16 -0700755 }
756 }
757
Avinash Patila1777322015-06-22 19:06:17 +0530758 if (mwifiex_bypass_tx_queue(priv, skb)) {
759 atomic_inc(&priv->adapter->tx_pending);
760 atomic_inc(&priv->adapter->bypass_tx_pending);
761 mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
762 } else {
763 atomic_inc(&priv->adapter->tx_pending);
764 mwifiex_wmm_add_buf_txqueue(priv, skb);
765 }
Stone Piaoe39faa72012-09-25 20:23:32 -0700766
Shengzhen Lib2713f62015-03-13 17:37:54 +0530767 mwifiex_queue_main_work(priv->adapter);
Stone Piaoe39faa72012-09-25 20:23:32 -0700768
769 return 0;
770}
771
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800772struct sk_buff *
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800773mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800774 struct sk_buff *skb, u8 flag, u64 *cookie)
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800775{
776 struct sk_buff *orig_skb = skb;
777 struct mwifiex_txinfo *tx_info, *orig_tx_info;
778
779 skb = skb_clone(skb, GFP_ATOMIC);
780 if (skb) {
781 unsigned long flags;
782 int id;
783
784 spin_lock_irqsave(&priv->ack_status_lock, flags);
785 id = idr_alloc(&priv->ack_status_frames, orig_skb,
Amitkumar Karwaree548d42015-12-31 06:24:15 -0800786 1, 0x10, GFP_ATOMIC);
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800787 spin_unlock_irqrestore(&priv->ack_status_lock, flags);
788
789 if (id >= 0) {
790 tx_info = MWIFIEX_SKB_TXCB(skb);
791 tx_info->ack_frame_id = id;
792 tx_info->flags |= flag;
793 orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
794 orig_tx_info->ack_frame_id = id;
795 orig_tx_info->flags |= flag;
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800796
797 if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
798 orig_tx_info->cookie = *cookie;
799
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800800 } else if (skb_shared(skb)) {
801 kfree_skb(orig_skb);
802 } else {
803 kfree_skb(skb);
804 skb = orig_skb;
805 }
806 } else {
807 /* couldn't clone -- lose tx status ... */
808 skb = orig_skb;
809 }
810
811 return skb;
812}
813
Stone Piaoe39faa72012-09-25 20:23:32 -0700814/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700815 * CFG802.11 network device handler for data transmission.
816 */
817static int
818mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
819{
820 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700821 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700822 struct mwifiex_txinfo *tx_info;
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800823 bool multicast;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700824
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530825 mwifiex_dbg(priv->adapter, DATA,
826 "data: %lu BSS(%d-%d): Data <= kernel\n",
827 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700828
829 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200830 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700831 priv->stats.tx_dropped++;
832 return 0;
833 }
834 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530835 mwifiex_dbg(priv->adapter, ERROR,
836 "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200837 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700838 priv->stats.tx_dropped++;
839 return 0;
840 }
841 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530842 mwifiex_dbg(priv->adapter, DATA,
843 "data: Tx: insufficient skb headroom %d\n",
844 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700845 /* Insufficient skb headroom - allocate a new skb */
846 new_skb =
847 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
848 if (unlikely(!new_skb)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530849 mwifiex_dbg(priv->adapter, ERROR,
850 "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200851 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700852 priv->stats.tx_dropped++;
853 return 0;
854 }
855 kfree_skb(skb);
856 skb = new_skb;
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530857 mwifiex_dbg(priv->adapter, INFO,
858 "info: new skb headroomd %d\n",
859 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700860 }
861
862 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700863 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800864 tx_info->bss_num = priv->bss_num;
865 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800866 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700867
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800868 multicast = is_multicast_ether_addr(skb->data);
869
870 if (unlikely(!multicast && skb->sk &&
871 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
872 priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
873 skb = mwifiex_clone_skb_for_tx_status(priv,
874 skb,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800875 MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800876
Avinash Patil47411a02012-11-01 18:44:16 -0700877 /* Record the current time the packet was queued; used to
878 * determine the amount of time the packet was queued in
879 * the driver before it was sent to the firmware.
880 * The delay is then sent along with the packet to the
881 * firmware for aggregate delay calculation for stats and
882 * MSDU lifetime expiry.
883 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100884 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700885
Avinash Patil9927baa2014-11-13 21:54:16 +0530886 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
887 priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
888 !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
889 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
890 mwifiex_tdls_check_tx(priv, skb);
891 }
892
Stone Piaoe39faa72012-09-25 20:23:32 -0700893 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700894
895 return 0;
896}
897
898/*
899 * CFG802.11 network device handler for setting MAC address.
900 */
901static int
902mwifiex_set_mac_address(struct net_device *dev, void *addr)
903{
904 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700905 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700906 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700907
908 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
909
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700910 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800911 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
912 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700913
914 if (!ret)
915 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
916 else
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530917 mwifiex_dbg(priv->adapter, ERROR,
918 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700919
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700920 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
921
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700922 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700923}
924
925/*
926 * CFG802.11 network device handler for setting multicast list.
927 */
928static void mwifiex_set_multicast_list(struct net_device *dev)
929{
930 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700931 struct mwifiex_multicast_list mcast_list;
932
933 if (dev->flags & IFF_PROMISC) {
934 mcast_list.mode = MWIFIEX_PROMISC_MODE;
935 } else if (dev->flags & IFF_ALLMULTI ||
936 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
937 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
938 } else {
939 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400940 mcast_list.num_multicast_addr =
941 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700942 }
943 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700944}
945
946/*
947 * CFG802.11 network device handler for transmission timeout.
948 */
949static void
950mwifiex_tx_timeout(struct net_device *dev)
951{
952 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
953
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700954 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800955 priv->tx_timeout_cnt++;
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530956 mwifiex_dbg(priv->adapter, ERROR,
957 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
958 jiffies, priv->tx_timeout_cnt, priv->bss_type,
959 priv->bss_num);
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800960 mwifiex_set_trans_start(dev);
961
962 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
963 priv->adapter->if_ops.card_reset) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530964 mwifiex_dbg(priv->adapter, ERROR,
965 "tx_timeout_cnt exceeds threshold.\t"
966 "Triggering card reset!\n");
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800967 priv->adapter->if_ops.card_reset(priv->adapter);
968 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700969}
970
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700971void mwifiex_multi_chan_resync(struct mwifiex_adapter *adapter)
972{
973 struct usb_card_rec *card = adapter->card;
974 struct mwifiex_private *priv;
975 u16 tx_buf_size;
976 int i, ret;
977
978 card->mc_resync_flag = true;
979 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
980 if (atomic_read(&card->port[i].tx_data_urb_pending)) {
981 mwifiex_dbg(adapter, WARN, "pending data urb in sys\n");
982 return;
983 }
984 }
985
986 card->mc_resync_flag = false;
987 tx_buf_size = 0xffff;
988 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
989 ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
990 HostCmd_ACT_GEN_SET, 0, &tx_buf_size, false);
991 if (ret)
992 mwifiex_dbg(adapter, ERROR,
993 "send reconfig tx buf size cmd err\n");
994}
995EXPORT_SYMBOL_GPL(mwifiex_multi_chan_resync);
996
Amitkumar Karwarfc697152015-05-26 06:34:31 -0700997void mwifiex_drv_info_dump(struct mwifiex_adapter *adapter)
Xinming Hu11cd07a2014-12-23 19:14:10 +0530998{
999 void *p;
1000 char drv_version[64];
1001 struct usb_card_rec *cardp;
1002 struct sdio_mmc_card *sdio_card;
1003 struct mwifiex_private *priv;
1004 int i, idx;
1005 struct netdev_queue *txq;
1006 struct mwifiex_debug_info *debug_info;
1007
1008 if (adapter->drv_info_dump) {
1009 vfree(adapter->drv_info_dump);
Amitkumar Karwar0769b272015-05-26 06:34:28 -07001010 adapter->drv_info_dump = NULL;
Xinming Hu11cd07a2014-12-23 19:14:10 +05301011 adapter->drv_info_size = 0;
1012 }
1013
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -07001014 mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +05301015
1016 adapter->drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
1017
1018 if (!adapter->drv_info_dump)
1019 return;
1020
1021 p = (char *)(adapter->drv_info_dump);
1022 p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
1023
1024 mwifiex_drv_get_driver_version(adapter, drv_version,
1025 sizeof(drv_version) - 1);
1026 p += sprintf(p, "driver_version = %s\n", drv_version);
1027
1028 if (adapter->iface_type == MWIFIEX_USB) {
1029 cardp = (struct usb_card_rec *)adapter->card;
1030 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
1031 atomic_read(&cardp->tx_cmd_urb_pending));
Zhaoyang Liu308fe292015-09-18 06:32:16 -07001032 p += sprintf(p, "tx_data_urb_pending_port_0 = %d\n",
1033 atomic_read(&cardp->port[0].tx_data_urb_pending));
1034 p += sprintf(p, "tx_data_urb_pending_port_1 = %d\n",
1035 atomic_read(&cardp->port[1].tx_data_urb_pending));
Xinming Hu11cd07a2014-12-23 19:14:10 +05301036 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
1037 atomic_read(&cardp->rx_cmd_urb_pending));
1038 p += sprintf(p, "rx_data_urb_pending = %d\n",
1039 atomic_read(&cardp->rx_data_urb_pending));
1040 }
1041
1042 p += sprintf(p, "tx_pending = %d\n",
1043 atomic_read(&adapter->tx_pending));
1044 p += sprintf(p, "rx_pending = %d\n",
1045 atomic_read(&adapter->rx_pending));
1046
1047 if (adapter->iface_type == MWIFIEX_SDIO) {
1048 sdio_card = (struct sdio_mmc_card *)adapter->card;
1049 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
1050 sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
1051 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
1052 sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
1053 }
1054
1055 for (i = 0; i < adapter->priv_num; i++) {
1056 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1057 continue;
1058 priv = adapter->priv[i];
1059 p += sprintf(p, "\n[interface : \"%s\"]\n",
1060 priv->netdev->name);
1061 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
1062 atomic_read(&priv->wmm_tx_pending[0]));
1063 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
1064 atomic_read(&priv->wmm_tx_pending[1]));
1065 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
1066 atomic_read(&priv->wmm_tx_pending[2]));
1067 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1068 atomic_read(&priv->wmm_tx_pending[3]));
1069 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1070 "Disconnected" : "Connected");
1071 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1072 ? "on" : "off"));
1073 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1074 txq = netdev_get_tx_queue(priv->netdev, idx);
1075 p += sprintf(p, "tx queue %d:%s ", idx,
1076 netif_tx_queue_stopped(txq) ?
1077 "stopped" : "started");
1078 }
1079 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1080 priv->netdev->name, priv->num_tx_timeout);
1081 }
1082
Xinming Hu46469682016-04-05 01:04:40 -07001083 if (adapter->iface_type == MWIFIEX_SDIO ||
1084 adapter->iface_type == MWIFIEX_PCIE) {
1085 p += sprintf(p, "\n=== %s register dump===\n",
1086 adapter->iface_type == MWIFIEX_SDIO ?
1087 "SDIO" : "PCIE");
Xinming Hu809c6ea2014-12-23 19:14:11 +05301088 if (adapter->if_ops.reg_dump)
1089 p += adapter->if_ops.reg_dump(adapter, p);
1090 }
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -07001091 p += sprintf(p, "\n=== more debug information\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +05301092 debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
1093 if (debug_info) {
1094 for (i = 0; i < adapter->priv_num; i++) {
1095 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1096 continue;
1097 priv = adapter->priv[i];
1098 mwifiex_get_debug_info(priv, debug_info);
1099 p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
1100 break;
1101 }
1102 kfree(debug_info);
1103 }
1104
1105 adapter->drv_info_size = p - adapter->drv_info_dump;
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -07001106 mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +05301107}
Amitkumar Karwarfc697152015-05-26 06:34:31 -07001108EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
Xinming Hu11cd07a2014-12-23 19:14:10 +05301109
Amitkumar Karwar57670ee2015-05-26 06:34:32 -07001110void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter)
1111{
1112 u8 idx, *dump_data, *fw_dump_ptr;
1113 u32 dump_len;
1114
1115 dump_len = (strlen("========Start dump driverinfo========\n") +
1116 adapter->drv_info_size +
1117 strlen("\n========End dump========\n"));
1118
1119 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1120 struct memory_type_mapping *entry =
1121 &adapter->mem_type_mapping_tbl[idx];
1122
1123 if (entry->mem_ptr) {
1124 dump_len += (strlen("========Start dump ") +
1125 strlen(entry->mem_name) +
1126 strlen("========\n") +
1127 (entry->mem_size + 1) +
1128 strlen("\n========End dump========\n"));
1129 }
1130 }
1131
1132 dump_data = vzalloc(dump_len + 1);
1133 if (!dump_data)
1134 goto done;
1135
1136 fw_dump_ptr = dump_data;
1137
1138 /* Dump all the memory data into single file, a userspace script will
1139 * be used to split all the memory data to multiple files
1140 */
1141 mwifiex_dbg(adapter, MSG,
1142 "== mwifiex dump information to /sys/class/devcoredump start");
1143
1144 strcpy(fw_dump_ptr, "========Start dump driverinfo========\n");
1145 fw_dump_ptr += strlen("========Start dump driverinfo========\n");
1146 memcpy(fw_dump_ptr, adapter->drv_info_dump, adapter->drv_info_size);
1147 fw_dump_ptr += adapter->drv_info_size;
1148 strcpy(fw_dump_ptr, "\n========End dump========\n");
1149 fw_dump_ptr += strlen("\n========End dump========\n");
1150
1151 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1152 struct memory_type_mapping *entry =
1153 &adapter->mem_type_mapping_tbl[idx];
1154
1155 if (entry->mem_ptr) {
1156 strcpy(fw_dump_ptr, "========Start dump ");
1157 fw_dump_ptr += strlen("========Start dump ");
1158
1159 strcpy(fw_dump_ptr, entry->mem_name);
1160 fw_dump_ptr += strlen(entry->mem_name);
1161
1162 strcpy(fw_dump_ptr, "========\n");
1163 fw_dump_ptr += strlen("========\n");
1164
1165 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1166 fw_dump_ptr += entry->mem_size;
1167
1168 strcpy(fw_dump_ptr, "\n========End dump========\n");
1169 fw_dump_ptr += strlen("\n========End dump========\n");
1170 }
1171 }
1172
1173 /* device dump data will be free in device coredump release function
1174 * after 5 min
1175 */
1176 dev_coredumpv(adapter->dev, dump_data, dump_len, GFP_KERNEL);
1177 mwifiex_dbg(adapter, MSG,
1178 "== mwifiex dump information to /sys/class/devcoredump end");
1179
1180done:
1181 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1182 struct memory_type_mapping *entry =
1183 &adapter->mem_type_mapping_tbl[idx];
1184
1185 if (entry->mem_ptr) {
1186 vfree(entry->mem_ptr);
1187 entry->mem_ptr = NULL;
1188 }
1189 entry->mem_size = 0;
1190 }
1191
1192 if (adapter->drv_info_dump) {
1193 vfree(adapter->drv_info_dump);
1194 adapter->drv_info_dump = NULL;
1195 adapter->drv_info_size = 0;
1196 }
1197}
1198EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1199
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001200/*
1201 * CFG802.11 network device handler for statistics retrieval.
1202 */
1203static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1204{
1205 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1206
1207 return &priv->stats;
1208}
1209
Avinash Patil47411a02012-11-01 18:44:16 -07001210static u16
Jason Wangf663dd92014-01-10 16:18:26 +08001211mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +01001212 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -07001213{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001214 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -07001215 return mwifiex_1d_to_wmm_queue[skb->priority];
1216}
1217
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001218/* Network device handlers */
1219static const struct net_device_ops mwifiex_netdev_ops = {
1220 .ndo_open = mwifiex_open,
1221 .ndo_stop = mwifiex_close,
1222 .ndo_start_xmit = mwifiex_hard_start_xmit,
1223 .ndo_set_mac_address = mwifiex_set_mac_address,
Amitkumar Karwarfe243722015-10-09 04:26:34 -07001224 .ndo_validate_addr = eth_validate_addr,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001225 .ndo_tx_timeout = mwifiex_tx_timeout,
1226 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001227 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -07001228 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001229};
1230
1231/*
1232 * This function initializes the private structure parameters.
1233 *
1234 * The following wait queues are initialized -
1235 * - IOCTL wait queue
1236 * - Command wait queue
1237 * - Statistics wait queue
1238 *
1239 * ...and the following default parameters are set -
1240 * - Current key index : Set to 0
1241 * - Rate index : Set to auto
1242 * - Media connected : Set to disconnected
1243 * - Adhoc link sensed : Set to false
1244 * - Nick name : Set to null
1245 * - Number of Tx timeout : Set to 0
1246 * - Device address : Set to current address
Xinming Hucbf6e052014-12-23 19:14:07 +05301247 * - Rx histogram statistc : Set to 0
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001248 *
1249 * In addition, the CFG80211 work queue is also created.
1250 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001251void mwifiex_init_priv_params(struct mwifiex_private *priv,
Avinash Patil047eaaf2015-01-28 15:42:05 +05301252 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001253{
1254 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -07001255 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001256 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001257 priv->current_key_index = 0;
1258 priv->media_connected = false;
Avinash Patilede98bf2012-05-08 18:30:28 -07001259 memset(priv->mgmt_ie, 0,
1260 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -07001261 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1262 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1263 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
Avinash Patil2ade5662015-01-28 15:54:20 +05301264 priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001265 priv->num_tx_timeout = 0;
Avinash Patil8d05eb22015-01-28 15:42:01 +05301266 ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001267 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
Xinming Hucbf6e052014-12-23 19:14:07 +05301268
1269 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1270 GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1271 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1272 if (priv->hist_data)
1273 mwifiex_hist_data_reset(priv);
1274 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001275}
1276
1277/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001278 * This function check if command is pending.
1279 */
1280int is_command_pending(struct mwifiex_adapter *adapter)
1281{
1282 unsigned long flags;
1283 int is_cmd_pend_q_empty;
1284
1285 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1286 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1287 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1288
1289 return !is_cmd_pend_q_empty;
1290}
1291
1292/*
Avinash Patil6e251172014-09-12 20:08:59 +05301293 * This is the RX work queue function.
1294 *
1295 * It handles the RX operations.
1296 */
1297static void mwifiex_rx_work_queue(struct work_struct *work)
1298{
1299 struct mwifiex_adapter *adapter =
1300 container_of(work, struct mwifiex_adapter, rx_work);
1301
1302 if (adapter->surprise_removed)
1303 return;
1304 mwifiex_process_rx(adapter);
1305}
1306
1307/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001308 * This is the main work queue function.
1309 *
1310 * It handles the main process, which in turn handles the complete
1311 * driver operations.
1312 */
1313static void mwifiex_main_work_queue(struct work_struct *work)
1314{
1315 struct mwifiex_adapter *adapter =
1316 container_of(work, struct mwifiex_adapter, main_work);
1317
1318 if (adapter->surprise_removed)
1319 return;
1320 mwifiex_main_process(adapter);
1321}
1322
1323/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001324 * This function adds the card.
1325 *
1326 * This function follows the following major steps to set up the device -
1327 * - Initialize software. This includes probing the card, registering
1328 * the interface operations table, and allocating/initializing the
1329 * adapter structure
1330 * - Set up the netlink socket
1331 * - Create and start the main work queue
1332 * - Register the device
1333 * - Initialize firmware and hardware
1334 * - Add logical interfaces
1335 */
1336int
1337mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001338 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001339{
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001340 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001341
1342 if (down_interruptible(sem))
1343 goto exit_sem_err;
1344
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001345 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001346 pr_err("%s: software init failed\n", __func__);
1347 goto err_init_sw;
1348 }
1349
Amitkumar Karward930fae2011-10-11 17:41:21 -07001350 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001351 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001352
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001353 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001354 adapter->surprise_removed = false;
1355 init_waitqueue_head(&adapter->init_wait_q);
1356 adapter->is_suspended = false;
1357 adapter->hs_activated = false;
1358 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001359 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001360 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -07001361 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001362
Avinash Patilec4a16b2014-11-05 17:04:27 +05301363 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +05301364 adapter->rx_work_enabled = true;
1365 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
1366 }
1367
Amitkumar Karwar448a71c2013-10-11 18:33:04 -07001368 adapter->workqueue =
1369 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1370 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001371 if (!adapter->workqueue)
1372 goto err_kmalloc;
1373
1374 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Avinash Patil6e251172014-09-12 20:08:59 +05301375
1376 if (adapter->rx_work_enabled) {
1377 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1378 WQ_HIGHPRI |
1379 WQ_MEM_RECLAIM |
1380 WQ_UNBOUND, 1);
1381 if (!adapter->rx_workqueue)
1382 goto err_kmalloc;
1383
1384 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1385 }
1386
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001387 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -04001388 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001389 if (adapter->if_ops.register_dev(adapter)) {
1390 pr_err("%s: failed to register mwifiex device\n", __func__);
1391 goto err_registerdev;
1392 }
1393
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001394 if (mwifiex_init_hw_fw(adapter)) {
1395 pr_err("%s: firmware init failed\n", __func__);
1396 goto err_init_fw;
1397 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001398
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001399 return 0;
1400
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001401err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001402 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001403 if (adapter->if_ops.unregister_dev)
1404 adapter->if_ops.unregister_dev(adapter);
Amitkumar Karwar71973252014-12-31 02:36:38 -08001405 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001406 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1407 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001408
1409 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001410 wait_event_interruptible(adapter->init_wait_q,
1411 adapter->init_wait_q_woken);
1412 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001413err_registerdev:
1414 adapter->surprise_removed = true;
1415 mwifiex_terminate_workqueue(adapter);
1416err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001417 mwifiex_free_adapter(adapter);
1418
1419err_init_sw:
1420 up(sem);
1421
1422exit_sem_err:
1423 return -1;
1424}
1425EXPORT_SYMBOL_GPL(mwifiex_add_card);
1426
1427/*
1428 * This function removes the card.
1429 *
1430 * This function follows the following major steps to remove the device -
1431 * - Stop data traffic
1432 * - Shutdown firmware
1433 * - Remove the logical interfaces
1434 * - Terminate the work queue
1435 * - Unregister the device
1436 * - Free the adapter structure
1437 */
1438int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
1439{
1440 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001441 int i;
1442
Xinming Huc3ec0ff2016-04-11 07:52:40 -07001443 if (down_trylock(sem))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001444 goto exit_sem_err;
1445
1446 if (!adapter)
1447 goto exit_remove;
1448
Daniel Drake232fde02013-07-13 10:57:10 -04001449 /* We can no longer handle interrupts once we start doing the teardown
1450 * below. */
1451 if (adapter->if_ops.disable_int)
1452 adapter->if_ops.disable_int(adapter);
1453
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001454 adapter->surprise_removed = true;
1455
Marc Yang9d2e85e2014-12-31 02:36:39 -08001456 mwifiex_terminate_workqueue(adapter);
1457
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001458 /* Stop data */
1459 for (i = 0; i < adapter->priv_num; i++) {
1460 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001461 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -07001462 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001463 if (netif_carrier_ok(priv->netdev))
1464 netif_carrier_off(priv->netdev);
1465 }
1466 }
1467
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301468 mwifiex_dbg(adapter, CMD,
1469 "cmd: calling mwifiex_shutdown_drv...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001470 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001471
1472 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001473 wait_event_interruptible(adapter->init_wait_q,
1474 adapter->init_wait_q_woken);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301475 mwifiex_dbg(adapter, CMD,
1476 "cmd: mwifiex_shutdown_drv done\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001477 if (atomic_read(&adapter->rx_pending) ||
1478 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001479 atomic_read(&adapter->cmd_pending)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301480 mwifiex_dbg(adapter, ERROR,
1481 "rx_pending=%d, tx_pending=%d,\t"
1482 "cmd_pending=%d\n",
1483 atomic_read(&adapter->rx_pending),
1484 atomic_read(&adapter->tx_pending),
1485 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001486 }
1487
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001488 for (i = 0; i < adapter->priv_num; i++) {
1489 priv = adapter->priv[i];
1490
1491 if (!priv)
1492 continue;
1493
1494 rtnl_lock();
Avinash Patil4facc342015-01-28 15:42:00 +05301495 if (priv->netdev &&
1496 priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1497 mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001498 rtnl_unlock();
1499 }
1500
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -08001501 wiphy_unregister(adapter->wiphy);
1502 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -07001503
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001504 /* Unregister device */
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301505 mwifiex_dbg(adapter, INFO,
1506 "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001507 if (adapter->if_ops.unregister_dev)
1508 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001509 /* Free adapter structure */
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301510 mwifiex_dbg(adapter, INFO,
1511 "info: free adapter\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001512 mwifiex_free_adapter(adapter);
1513
1514exit_remove:
1515 up(sem);
1516exit_sem_err:
1517 return 0;
1518}
1519EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1520
Joe Perches36925e52015-08-31 10:46:45 -07001521void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
1522 const char *fmt, ...)
1523{
1524 struct va_format vaf;
1525 va_list args;
1526
1527 if (!adapter->dev || !(adapter->debug_mask & mask))
1528 return;
1529
1530 va_start(args, fmt);
1531
1532 vaf.fmt = fmt;
1533 vaf.va = &args;
1534
1535 dev_info(adapter->dev, "%pV", &vaf);
1536
1537 va_end(args);
1538}
1539EXPORT_SYMBOL_GPL(_mwifiex_dbg);
1540
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001541/*
1542 * This function initializes the module.
1543 *
1544 * The debug FS is also initialized if configured.
1545 */
1546static int
1547mwifiex_init_module(void)
1548{
1549#ifdef CONFIG_DEBUG_FS
1550 mwifiex_debugfs_init();
1551#endif
1552 return 0;
1553}
1554
1555/*
1556 * This function cleans up the module.
1557 *
1558 * The debug FS is removed if available.
1559 */
1560static void
1561mwifiex_cleanup_module(void)
1562{
1563#ifdef CONFIG_DEBUG_FS
1564 mwifiex_debugfs_remove();
1565#endif
1566}
1567
1568module_init(mwifiex_init_module);
1569module_exit(mwifiex_cleanup_module);
1570
1571MODULE_AUTHOR("Marvell International Ltd.");
1572MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1573MODULE_VERSION(VERSION);
1574MODULE_LICENSE("GPL v2");