blob: 39b6b5e3f6e0e4e9ec6eb81e458250485e94c487 [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
Jeffy Chenef7e0712017-02-24 14:24:31 +080020#include <linux/suspend.h>
21
Bing Zhao5e6e3a92011-03-21 18:00:50 -070022#include "main.h"
23#include "wmm.h"
24#include "cfg80211.h"
25#include "11n.h"
26
27#define VERSION "1.0"
Xinming Hucf5383b2016-09-02 13:05:06 +053028#define MFG_FIRMWARE "mwifiex_mfg.bin"
Bing Zhao5e6e3a92011-03-21 18:00:50 -070029
Zhaoyang Liuc687a002015-05-12 00:48:18 +053030static unsigned int debug_mask = MWIFIEX_DEFAULT_DEBUG_MASK;
31module_param(debug_mask, uint, 0);
32MODULE_PARM_DESC(debug_mask, "bitmap for debug flags");
33
Bing Zhao5e6e3a92011-03-21 18:00:50 -070034const char driver_version[] = "mwifiex " VERSION " (%s) ";
Amitkumar Karwar388ec382013-05-17 17:50:25 -070035static char *cal_data_cfg;
36module_param(cal_data_cfg, charp, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070037
Avinash Patil0013c7c2014-11-05 19:38:11 +053038static unsigned short driver_mode;
39module_param(driver_mode, ushort, 0);
40MODULE_PARM_DESC(driver_mode,
41 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
42
Xinming Hucf5383b2016-09-02 13:05:06 +053043bool mfg_mode;
44module_param(mfg_mode, bool, 0);
45MODULE_PARM_DESC(mfg_mode, "manufacturing mode enable:1, disable:0");
46
Bing Zhao5e6e3a92011-03-21 18:00:50 -070047/*
48 * This function registers the device and performs all the necessary
49 * initializations.
50 *
51 * The following initialization operations are performed -
52 * - Allocate adapter structure
53 * - Save interface specific operations table in adapter
54 * - Call interface specific initialization routine
55 * - Allocate private structures
56 * - Set default adapter structure parameters
57 * - Initialize locks
58 *
59 * In case of any errors during inittialization, this function also ensures
60 * proper cleanup before exiting.
61 */
Brian Norrisba1c7e42017-03-10 17:39:22 -080062static int mwifiex_register(void *card, struct device *dev,
63 struct mwifiex_if_ops *if_ops, void **padapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070064{
Amitkumar Karwar2be78592011-04-15 20:50:42 -070065 struct mwifiex_adapter *adapter;
66 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070067
68 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070069 if (!adapter)
Christoph Fritzb53575e2011-05-08 22:50:09 +020070 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070071
Amitkumar Karwar287546d2011-06-08 20:39:20 +053072 *padapter = adapter;
Brian Norrisba1c7e42017-03-10 17:39:22 -080073 adapter->dev = dev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070074 adapter->card = card;
75
76 /* Save interface specific operations in adapter */
77 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
Zhaoyang Liuc687a002015-05-12 00:48:18 +053078 adapter->debug_mask = debug_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070079
80 /* card specific initialization has been deferred until now .. */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070081 if (adapter->if_ops.init_if)
82 if (adapter->if_ops.init_if(adapter))
83 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070084
85 adapter->priv_num = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070086
Avinash Patil64b05e22012-05-08 18:30:13 -070087 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
88 /* Allocate memory for private structure */
89 adapter->priv[i] =
90 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
91 if (!adapter->priv[i])
92 goto error;
93
94 adapter->priv[i]->adapter = adapter;
Avinash Patil64b05e22012-05-08 18:30:13 -070095 adapter->priv_num++;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070096 }
Amitkumar Karwar44b815c2011-09-29 20:43:41 -070097 mwifiex_init_lock_list(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070098
Julia Lawallc6c33e72014-12-26 15:35:56 +010099 setup_timer(&adapter->cmd_timer, mwifiex_cmd_timeout_func,
100 (unsigned long)adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700101
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700102 return 0;
103
104error:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530105 mwifiex_dbg(adapter, ERROR,
106 "info: leave mwifiex_register with error\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700107
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700108 for (i = 0; i < adapter->priv_num; i++)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700109 kfree(adapter->priv[i]);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700110
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700111 kfree(adapter);
112
113 return -1;
114}
115
116/*
117 * This function unregisters the device and performs all the necessary
118 * cleanups.
119 *
120 * The following cleanup operations are performed -
121 * - Free the timers
122 * - Free beacon buffers
123 * - Free private structures
124 * - Free adapter structure
125 */
126static int mwifiex_unregister(struct mwifiex_adapter *adapter)
127{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700128 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700129
Amitkumar Karwar4cfda672013-07-22 19:17:50 -0700130 if (adapter->if_ops.cleanup_if)
131 adapter->if_ops.cleanup_if(adapter);
132
Avinash Patil629873f2014-02-18 15:47:55 -0800133 del_timer_sync(&adapter->cmd_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700134
135 /* Free private structures */
136 for (i = 0; i < adapter->priv_num; i++) {
137 if (adapter->priv[i]) {
138 mwifiex_free_curr_bcn(adapter->priv[i]);
139 kfree(adapter->priv[i]);
140 }
141 }
142
chunfan chen7d7f07d2016-01-13 01:26:54 -0800143 if (adapter->nd_info) {
144 for (i = 0 ; i < adapter->nd_info->n_matches ; i++)
145 kfree(adapter->nd_info->matches[i]);
146 kfree(adapter->nd_info);
147 adapter->nd_info = NULL;
148 }
149
Amitkumar Karwar72539792016-08-09 20:20:46 +0530150 kfree(adapter->regd);
151
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700152 kfree(adapter);
153 return 0;
154}
155
Shengzhen Lib2713f62015-03-13 17:37:54 +0530156void mwifiex_queue_main_work(struct mwifiex_adapter *adapter)
157{
158 unsigned long flags;
159
160 spin_lock_irqsave(&adapter->main_proc_lock, flags);
161 if (adapter->mwifiex_processing) {
162 adapter->more_task_flag = true;
163 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
164 } else {
165 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
166 queue_work(adapter->workqueue, &adapter->main_work);
167 }
168}
169EXPORT_SYMBOL_GPL(mwifiex_queue_main_work);
170
171static void mwifiex_queue_rx_work(struct mwifiex_adapter *adapter)
172{
173 unsigned long flags;
174
175 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
176 if (adapter->rx_processing) {
177 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
178 } else {
179 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
180 queue_work(adapter->rx_workqueue, &adapter->rx_work);
181 }
182}
183
Avinash Patil6e251172014-09-12 20:08:59 +0530184static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
185{
186 unsigned long flags;
187 struct sk_buff *skb;
Zhaoyang Liu92263a82015-03-13 17:37:58 +0530188 struct mwifiex_rxinfo *rx_info;
Avinash Patil6e251172014-09-12 20:08:59 +0530189
190 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
191 if (adapter->rx_processing || adapter->rx_locked) {
192 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
193 goto exit_rx_proc;
194 } else {
195 adapter->rx_processing = true;
196 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
197 }
198
199 /* Check for Rx data */
200 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
201 atomic_dec(&adapter->rx_pending);
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800202 if ((adapter->delay_main_work ||
203 adapter->iface_type == MWIFIEX_USB) &&
Avinash Patilb43a0d92014-09-29 21:44:14 +0530204 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530205 if (adapter->if_ops.submit_rem_rx_urbs)
206 adapter->if_ops.submit_rem_rx_urbs(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530207 adapter->delay_main_work = false;
Shengzhen Lib2713f62015-03-13 17:37:54 +0530208 mwifiex_queue_main_work(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530209 }
Zhaoyang Liu92263a82015-03-13 17:37:58 +0530210 rx_info = MWIFIEX_SKB_RXCB(skb);
211 if (rx_info->buf_type == MWIFIEX_TYPE_AGGR_DATA) {
212 if (adapter->if_ops.deaggr_pkt)
213 adapter->if_ops.deaggr_pkt(adapter, skb);
214 dev_kfree_skb_any(skb);
215 } else {
216 mwifiex_handle_rx_packet(adapter, skb);
217 }
Avinash Patil6e251172014-09-12 20:08:59 +0530218 }
219 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
220 adapter->rx_processing = false;
221 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
222
Avinash Patil6e251172014-09-12 20:08:59 +0530223exit_rx_proc:
224 return 0;
225}
226
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700227/*
228 * The main process.
229 *
230 * This function is the main procedure of the driver and handles various driver
231 * operations. It runs in a loop and provides the core functionalities.
232 *
233 * The main responsibilities of this function are -
234 * - Ensure concurrency control
235 * - Handle pending interrupts and call interrupt handlers
236 * - Wake up the card if required
237 * - Handle command responses and call response handlers
238 * - Handle events and call event handlers
239 * - Execute pending commands
240 * - Transmit pending data packets
241 */
242int mwifiex_main_process(struct mwifiex_adapter *adapter)
243{
244 int ret = 0;
245 unsigned long flags;
246
247 spin_lock_irqsave(&adapter->main_proc_lock, flags);
248
249 /* Check if already processing */
Avinash Patila9adbcb2015-03-13 17:37:51 +0530250 if (adapter->mwifiex_processing || adapter->main_locked) {
Shengzhen Li04c7b362015-02-11 23:12:24 +0530251 adapter->more_task_flag = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700252 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
Xinming Hu5bf15e32016-11-16 18:39:05 +0530253 return 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700254 } else {
255 adapter->mwifiex_processing = true;
Cathy Luo91457ea2015-04-17 04:18:29 -0700256 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700257 }
258process_start:
259 do {
Xinming Hu5bf15e32016-11-16 18:39:05 +0530260 if (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700261 break;
262
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800263 /* For non-USB interfaces, If we process interrupts first, it
264 * would increase RX pending even further. Avoid this by
265 * checking if rx_pending has crossed high threshold and
266 * schedule rx work queue and then process interrupts.
267 * For USB interface, there are no interrupts. We already have
268 * HIGH_RX_PENDING check in usb.c
Avinash Patil6e251172014-09-12 20:08:59 +0530269 */
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800270 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
271 adapter->iface_type != MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +0530272 adapter->delay_main_work = true;
Shengzhen Lib2713f62015-03-13 17:37:54 +0530273 mwifiex_queue_rx_work(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530274 break;
275 }
276
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700277 /* Handle pending interrupt if any */
278 if (adapter->int_status) {
279 if (adapter->hs_activated)
280 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700281 if (adapter->if_ops.process_int_status)
282 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700283 }
284
Avinash Patil6e251172014-09-12 20:08:59 +0530285 if (adapter->rx_work_enabled && adapter->data_received)
Shengzhen Lib2713f62015-03-13 17:37:54 +0530286 mwifiex_queue_rx_work(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530287
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700288 /* Need to wake up the card ? */
289 if ((adapter->ps_state == PS_STATE_SLEEP) &&
290 (adapter->pm_wakeup_card_req &&
291 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700292 (is_command_pending(adapter) ||
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530293 !skb_queue_empty(&adapter->tx_data_q) ||
Avinash Patila1777322015-06-22 19:06:17 +0530294 !mwifiex_bypass_txlist_empty(adapter) ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700295 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700296 adapter->pm_wakeup_fw_try = true;
Amitkumar Karwar46361872014-12-31 02:36:41 -0800297 mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700298 adapter->if_ops.wakeup(adapter);
299 continue;
300 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700301
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700302 if (IS_CARD_RX_RCVD(adapter)) {
Avinash Patil6e251172014-09-12 20:08:59 +0530303 adapter->data_received = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700304 adapter->pm_wakeup_fw_try = false;
Amitkumar Karwar6e9344f2015-03-12 00:38:40 -0700305 del_timer(&adapter->wakeup_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700306 if (adapter->ps_state == PS_STATE_SLEEP)
307 adapter->ps_state = PS_STATE_AWAKE;
308 } else {
309 /* We have tried to wakeup the card already */
310 if (adapter->pm_wakeup_fw_try)
311 break;
Shengzhen Li67120762016-11-18 19:30:25 +0530312 if (adapter->ps_state == PS_STATE_PRE_SLEEP)
313 mwifiex_check_ps_cond(adapter);
314
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700315 if (adapter->ps_state != PS_STATE_AWAKE)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700316 break;
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700317 if (adapter->tx_lock_flag) {
318 if (adapter->iface_type == MWIFIEX_USB) {
319 if (!adapter->usb_mc_setup)
320 break;
321 } else
322 break;
323 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700324
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530325 if ((!adapter->scan_chan_gap_enabled &&
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530326 adapter->scan_processing) || adapter->data_sent ||
Xinming Huba101ad2015-06-22 19:06:10 +0530327 mwifiex_is_tdls_chan_switching
328 (mwifiex_get_priv(adapter,
329 MWIFIEX_BSS_ROLE_STA)) ||
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530330 (mwifiex_wmm_lists_empty(adapter) &&
Avinash Patila1777322015-06-22 19:06:17 +0530331 mwifiex_bypass_txlist_empty(adapter) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530332 skb_queue_empty(&adapter->tx_data_q))) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700333 if (adapter->cmd_sent || adapter->curr_cmd ||
Xinming Huba101ad2015-06-22 19:06:10 +0530334 !mwifiex_is_send_cmd_allowed
335 (mwifiex_get_priv(adapter,
336 MWIFIEX_BSS_ROLE_STA)) ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700337 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700338 break;
339 }
340 }
341
Amitkumar Karwar20474122014-04-14 15:31:05 -0700342 /* Check for event */
343 if (adapter->event_received) {
344 adapter->event_received = false;
345 mwifiex_process_event(adapter);
346 }
347
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700348 /* Check for Cmd Resp */
349 if (adapter->cmd_resp_received) {
350 adapter->cmd_resp_received = false;
351 mwifiex_process_cmdresp(adapter);
352
353 /* call mwifiex back when init_fw is done */
354 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
355 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
356 mwifiex_init_fw_complete(adapter);
357 }
358 }
359
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700360 /* Check if we need to confirm Sleep Request
361 received previously */
Shengzhen Li67120762016-11-18 19:30:25 +0530362 if (adapter->ps_state == PS_STATE_PRE_SLEEP)
363 mwifiex_check_ps_cond(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700364
365 /* * The ps_state may have been changed during processing of
366 * Sleep Request event.
367 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700368 if ((adapter->ps_state == PS_STATE_SLEEP) ||
369 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700370 (adapter->ps_state == PS_STATE_SLEEP_CFM)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700371 continue;
Shengzhen Li04c7b362015-02-11 23:12:24 +0530372 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700373
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700374 if (adapter->tx_lock_flag) {
375 if (adapter->iface_type == MWIFIEX_USB) {
376 if (!adapter->usb_mc_setup)
377 continue;
378 } else
379 continue;
380 }
381
Xinming Huba101ad2015-06-22 19:06:10 +0530382 if (!adapter->cmd_sent && !adapter->curr_cmd &&
383 mwifiex_is_send_cmd_allowed
384 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700385 if (mwifiex_exec_next_cmd(adapter) == -1) {
386 ret = -1;
387 break;
388 }
389 }
390
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -0700391 /** If USB Multi channel setup ongoing,
392 * wait for ready to tx data.
393 */
394 if (adapter->iface_type == MWIFIEX_USB &&
395 adapter->usb_mc_setup)
396 continue;
397
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530398 if ((adapter->scan_chan_gap_enabled ||
Amitkumar Karward8d91252014-09-12 20:08:58 +0530399 !adapter->scan_processing) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530400 !adapter->data_sent &&
401 !skb_queue_empty(&adapter->tx_data_q)) {
402 mwifiex_process_tx_queue(adapter);
403 if (adapter->hs_activated) {
404 adapter->is_hs_configured = false;
405 mwifiex_hs_activated_event
406 (mwifiex_get_priv
407 (adapter, MWIFIEX_BSS_ROLE_ANY),
408 false);
409 }
410 }
411
412 if ((adapter->scan_chan_gap_enabled ||
413 !adapter->scan_processing) &&
Avinash Patila1777322015-06-22 19:06:17 +0530414 !adapter->data_sent &&
415 !mwifiex_bypass_txlist_empty(adapter) &&
416 !mwifiex_is_tdls_chan_switching
417 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
418 mwifiex_process_bypass_tx(adapter);
419 if (adapter->hs_activated) {
420 adapter->is_hs_configured = false;
421 mwifiex_hs_activated_event
422 (mwifiex_get_priv
423 (adapter, MWIFIEX_BSS_ROLE_ANY),
424 false);
425 }
426 }
427
428 if ((adapter->scan_chan_gap_enabled ||
429 !adapter->scan_processing) &&
Xinming Huba101ad2015-06-22 19:06:10 +0530430 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter) &&
431 !mwifiex_is_tdls_chan_switching
432 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700433 mwifiex_wmm_process_tx(adapter);
434 if (adapter->hs_activated) {
435 adapter->is_hs_configured = false;
436 mwifiex_hs_activated_event
437 (mwifiex_get_priv
438 (adapter, MWIFIEX_BSS_ROLE_ANY),
439 false);
440 }
441 }
442
443 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700444 !adapter->curr_cmd && !is_command_pending(adapter) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530445 (mwifiex_wmm_lists_empty(adapter) &&
Avinash Patila1777322015-06-22 19:06:17 +0530446 mwifiex_bypass_txlist_empty(adapter) &&
Zhaoyang Liue35000e2015-03-13 17:37:57 +0530447 skb_queue_empty(&adapter->tx_data_q))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700448 if (!mwifiex_send_null_packet
449 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
450 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
451 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
452 adapter->delay_null_pkt = false;
453 adapter->ps_state = PS_STATE_SLEEP;
454 }
455 break;
456 }
457 } while (true);
458
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700459 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Cathy Luo91457ea2015-04-17 04:18:29 -0700460 if (adapter->more_task_flag) {
461 adapter->more_task_flag = false;
462 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700463 goto process_start;
Cathy Luo91457ea2015-04-17 04:18:29 -0700464 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700465 adapter->mwifiex_processing = false;
466 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
467
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700468 return ret;
469}
Bing Zhao601216e2012-11-05 16:59:15 -0800470EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700471
472/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700473 * This function frees the adapter structure.
474 *
475 * Additionally, this closes the netlink socket, frees the timers
476 * and private structures.
477 */
478static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
479{
480 if (!adapter) {
481 pr_err("%s: adapter is NULL\n", __func__);
482 return;
483 }
484
485 mwifiex_unregister(adapter);
486 pr_debug("info: %s: free adapter\n", __func__);
487}
488
489/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700490 * This function cancels all works in the queue and destroys
491 * the main workqueue.
492 */
493static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
494{
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +0530495 if (adapter->workqueue) {
496 flush_workqueue(adapter->workqueue);
497 destroy_workqueue(adapter->workqueue);
498 adapter->workqueue = NULL;
499 }
Avinash Patil6e251172014-09-12 20:08:59 +0530500
501 if (adapter->rx_workqueue) {
502 flush_workqueue(adapter->rx_workqueue);
503 destroy_workqueue(adapter->rx_workqueue);
504 adapter->rx_workqueue = NULL;
505 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700506}
507
508/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700509 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700510 *
511 * The main initialization steps followed are -
512 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700513 * - Issue the init commands to firmware
514 */
Brian Norris755b37c2017-03-28 16:59:33 -0700515static int _mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700516{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800517 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700518 char fmt[64];
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700519 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700520 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700521 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800522 struct wireless_dev *wdev;
Brian Norris4a79aa12016-11-18 19:30:26 +0530523 struct completion *fw_done = adapter->fw_done;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700524
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700525 if (!firmware) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530526 mwifiex_dbg(adapter, ERROR,
527 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700528 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700529 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700530
531 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
532 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700533 fw.fw_buf = (u8 *) adapter->firmware->data;
534 fw.fw_len = adapter->firmware->size;
535
Wei-Ning Huang65c71ef2016-05-19 11:53:43 +0800536 if (adapter->if_ops.dnld_fw) {
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700537 ret = adapter->if_ops.dnld_fw(adapter, &fw);
Wei-Ning Huang65c71ef2016-05-19 11:53:43 +0800538 } else {
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700539 ret = mwifiex_dnld_fw(adapter, &fw);
Wei-Ning Huang65c71ef2016-05-19 11:53:43 +0800540 }
541
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700542 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700543 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700544
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530545 mwifiex_dbg(adapter, MSG, "WLAN FW is active\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700546
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700547 if (cal_data_cfg) {
548 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
549 adapter->dev)) < 0)
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530550 mwifiex_dbg(adapter, ERROR,
551 "Cal data request_firmware() failed\n");
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700552 }
553
Daniel Drake232fde02013-07-13 10:57:10 -0400554 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700555 if (adapter->if_ops.enable_int) {
556 if (adapter->if_ops.enable_int(adapter))
557 goto err_dnld_fw;
558 }
Daniel Drake232fde02013-07-13 10:57:10 -0400559
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700560 adapter->init_wait_q_woken = false;
561 ret = mwifiex_init_fw(adapter);
562 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700563 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700564 } else if (!ret) {
565 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
566 goto done;
567 }
568 /* Wait for mwifiex_init to complete */
Xinming Hucf5383b2016-09-02 13:05:06 +0530569 if (!adapter->mfg_mode) {
570 wait_event_interruptible(adapter->init_wait_q,
571 adapter->init_wait_q_woken);
572 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
573 goto err_init_fw;
574 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700575
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +0530576 if (!adapter->wiphy) {
577 if (mwifiex_register_cfg80211(adapter)) {
578 mwifiex_dbg(adapter, ERROR,
579 "cannot register with cfg80211\n");
580 goto err_init_fw;
581 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700582 }
583
Avinash Patilbf354432014-10-31 16:08:26 +0530584 if (mwifiex_init_channel_scan_gap(adapter)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530585 mwifiex_dbg(adapter, ERROR,
586 "could not init channel stats table\n");
Avinash Patilbf354432014-10-31 16:08:26 +0530587 goto err_init_fw;
588 }
589
Avinash Patil0013c7c2014-11-05 19:38:11 +0530590 if (driver_mode) {
591 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
592 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
593 }
594
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700595 rtnl_lock();
596 /* Create station interface by default */
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100597 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d", NET_NAME_ENUM,
Johannes Berg818a9862017-04-12 11:23:28 +0200598 NL80211_IFTYPE_STATION, NULL);
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800599 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530600 mwifiex_dbg(adapter, ERROR,
601 "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800602 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700603 goto err_add_intf;
604 }
Avinash Patil0013c7c2014-11-05 19:38:11 +0530605
606 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100607 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d", NET_NAME_ENUM,
Johannes Berg818a9862017-04-12 11:23:28 +0200608 NL80211_IFTYPE_AP, NULL);
Avinash Patil0013c7c2014-11-05 19:38:11 +0530609 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530610 mwifiex_dbg(adapter, ERROR,
611 "cannot create AP interface\n");
Avinash Patil0013c7c2014-11-05 19:38:11 +0530612 rtnl_unlock();
613 goto err_add_intf;
614 }
615 }
616
617 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
Tom Gundersen6bab2e192015-03-18 11:13:39 +0100618 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d", NET_NAME_ENUM,
Johannes Berg818a9862017-04-12 11:23:28 +0200619 NL80211_IFTYPE_P2P_CLIENT, NULL);
Avinash Patil0013c7c2014-11-05 19:38:11 +0530620 if (IS_ERR(wdev)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530621 mwifiex_dbg(adapter, ERROR,
622 "cannot create p2p client interface\n");
Avinash Patil0013c7c2014-11-05 19:38:11 +0530623 rtnl_unlock();
624 goto err_add_intf;
625 }
626 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700627 rtnl_unlock();
628
629 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530630 mwifiex_dbg(adapter, MSG, "driver_version = %s\n", fmt);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700631 goto done;
632
633err_add_intf:
Brian Norrisfb9e67b2017-04-14 14:51:20 -0700634 vfree(adapter->chan_stats);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700635 wiphy_unregister(adapter->wiphy);
636 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700637err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400638 if (adapter->if_ops.disable_int)
639 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700640err_dnld_fw:
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530641 mwifiex_dbg(adapter, ERROR,
642 "info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700643 if (adapter->if_ops.unregister_dev)
644 adapter->if_ops.unregister_dev(adapter);
645
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700646 adapter->surprise_removed = true;
647 mwifiex_terminate_workqueue(adapter);
Xinming Hu5bf15e32016-11-16 18:39:05 +0530648
649 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
650 pr_debug("info: %s: shutdown mwifiex\n", __func__);
651 mwifiex_shutdown_drv(adapter);
652 }
653
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700654 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700655done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700656 if (adapter->cal_data) {
657 release_firmware(adapter->cal_data);
658 adapter->cal_data = NULL;
659 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700660 if (adapter->firmware) {
661 release_firmware(adapter->firmware);
662 adapter->firmware = NULL;
663 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700664 if (init_failed)
665 mwifiex_free_adapter(adapter);
Brian Norris4a79aa12016-11-18 19:30:26 +0530666 /* Tell all current and future waiters we're finished */
667 complete_all(fw_done);
Brian Norris755b37c2017-03-28 16:59:33 -0700668
669 return init_failed ? -EIO : 0;
670}
671
672static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
673{
674 _mwifiex_fw_dpc(firmware, context);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700675}
676
677/*
Brian Norris755b37c2017-03-28 16:59:33 -0700678 * This function gets the firmware and (if called asynchronously) kicks off the
679 * HW init when done.
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700680 */
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +0530681static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter,
682 bool req_fw_nowait)
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700683{
684 int ret;
685
Xinming Hucf5383b2016-09-02 13:05:06 +0530686 /* Override default firmware with manufacturing one if
687 * manufacturing mode is enabled
688 */
689 if (mfg_mode) {
690 if (strlcpy(adapter->fw_name, MFG_FIRMWARE,
691 sizeof(adapter->fw_name)) >=
692 sizeof(adapter->fw_name)) {
693 pr_err("%s: fw_name too long!\n", __func__);
694 return -1;
695 }
696 }
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +0530697
698 if (req_fw_nowait) {
699 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
700 adapter->dev, GFP_KERNEL, adapter,
701 mwifiex_fw_dpc);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +0530702 } else {
703 ret = request_firmware(&adapter->firmware,
704 adapter->fw_name,
705 adapter->dev);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +0530706 }
707
Brian Norris755b37c2017-03-28 16:59:33 -0700708 if (ret < 0)
709 mwifiex_dbg(adapter, ERROR, "request_firmware%s error %d\n",
710 req_fw_nowait ? "_nowait" : "", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700711 return ret;
712}
713
714/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700715 * CFG802.11 network device handler for open.
716 *
717 * Starts the data queue.
718 */
719static int
720mwifiex_open(struct net_device *dev)
721{
Johannes Bergea2325b2015-01-19 15:54:36 +0530722 netif_carrier_off(dev);
723
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700724 return 0;
725}
726
727/*
728 * CFG802.11 network device handler for close.
729 */
730static int
731mwifiex_close(struct net_device *dev)
732{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700733 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
734
735 if (priv->scan_request) {
Avraham Stern1d762502016-07-05 17:10:13 +0300736 struct cfg80211_scan_info info = {
737 .aborted = true,
738 };
739
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530740 mwifiex_dbg(priv->adapter, INFO,
741 "aborting scan on ndo_stop\n");
Avraham Stern1d762502016-07-05 17:10:13 +0300742 cfg80211_scan_done(priv->scan_request, &info);
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700743 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700744 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700745 }
746
Xinming Hueaf46b52016-04-18 06:42:55 -0700747 if (priv->sched_scanning) {
748 mwifiex_dbg(priv->adapter, INFO,
749 "aborting bgscan on ndo_stop\n");
750 mwifiex_stop_bg_scan(priv);
Arend Van Sprielb34939b2017-04-28 13:40:28 +0100751 cfg80211_sched_scan_stopped(priv->wdev.wiphy, 0);
Xinming Hueaf46b52016-04-18 06:42:55 -0700752 }
753
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700754 return 0;
755}
756
Avinash Patila1777322015-06-22 19:06:17 +0530757static bool
758mwifiex_bypass_tx_queue(struct mwifiex_private *priv,
759 struct sk_buff *skb)
760{
761 struct ethhdr *eth_hdr = (struct ethhdr *)skb->data;
762
763 if (ntohs(eth_hdr->h_proto) == ETH_P_PAE ||
764 mwifiex_is_skb_mgmt_frame(skb) ||
765 (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA &&
766 ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
767 (ntohs(eth_hdr->h_proto) == ETH_P_TDLS))) {
768 mwifiex_dbg(priv->adapter, DATA,
769 "bypass txqueue; eth type %#x, mgmt %d\n",
770 ntohs(eth_hdr->h_proto),
771 mwifiex_is_skb_mgmt_frame(skb));
772 return true;
773 }
774
775 return false;
776}
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700777/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700778 * Add buffer into wmm tx queue and queue work to transmit it.
779 */
780int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
781{
Avinash Patil47411a02012-11-01 18:44:16 -0700782 struct netdev_queue *txq;
783 int index = mwifiex_1d_to_wmm_queue[skb->priority];
784
785 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
786 txq = netdev_get_tx_queue(priv->netdev, index);
787 if (!netif_tx_queue_stopped(txq)) {
788 netif_tx_stop_queue(txq);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530789 mwifiex_dbg(priv->adapter, DATA,
790 "stop queue: %d\n", index);
Avinash Patil47411a02012-11-01 18:44:16 -0700791 }
792 }
793
Avinash Patila1777322015-06-22 19:06:17 +0530794 if (mwifiex_bypass_tx_queue(priv, skb)) {
795 atomic_inc(&priv->adapter->tx_pending);
796 atomic_inc(&priv->adapter->bypass_tx_pending);
797 mwifiex_wmm_add_buf_bypass_txqueue(priv, skb);
798 } else {
799 atomic_inc(&priv->adapter->tx_pending);
800 mwifiex_wmm_add_buf_txqueue(priv, skb);
801 }
Stone Piaoe39faa72012-09-25 20:23:32 -0700802
Shengzhen Lib2713f62015-03-13 17:37:54 +0530803 mwifiex_queue_main_work(priv->adapter);
Stone Piaoe39faa72012-09-25 20:23:32 -0700804
805 return 0;
806}
807
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800808struct sk_buff *
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800809mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800810 struct sk_buff *skb, u8 flag, u64 *cookie)
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800811{
812 struct sk_buff *orig_skb = skb;
813 struct mwifiex_txinfo *tx_info, *orig_tx_info;
814
815 skb = skb_clone(skb, GFP_ATOMIC);
816 if (skb) {
817 unsigned long flags;
818 int id;
819
820 spin_lock_irqsave(&priv->ack_status_lock, flags);
821 id = idr_alloc(&priv->ack_status_frames, orig_skb,
Amitkumar Karwaree548d42015-12-31 06:24:15 -0800822 1, 0x10, GFP_ATOMIC);
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800823 spin_unlock_irqrestore(&priv->ack_status_lock, flags);
824
825 if (id >= 0) {
826 tx_info = MWIFIEX_SKB_TXCB(skb);
827 tx_info->ack_frame_id = id;
828 tx_info->flags |= flag;
829 orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
830 orig_tx_info->ack_frame_id = id;
831 orig_tx_info->flags |= flag;
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800832
833 if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
834 orig_tx_info->cookie = *cookie;
835
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800836 } else if (skb_shared(skb)) {
837 kfree_skb(orig_skb);
838 } else {
839 kfree_skb(skb);
840 skb = orig_skb;
841 }
842 } else {
843 /* couldn't clone -- lose tx status ... */
844 skb = orig_skb;
845 }
846
847 return skb;
848}
849
Stone Piaoe39faa72012-09-25 20:23:32 -0700850/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700851 * CFG802.11 network device handler for data transmission.
852 */
853static int
854mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
855{
856 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700857 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700858 struct mwifiex_txinfo *tx_info;
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800859 bool multicast;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700860
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530861 mwifiex_dbg(priv->adapter, DATA,
862 "data: %lu BSS(%d-%d): Data <= kernel\n",
863 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700864
865 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200866 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700867 priv->stats.tx_dropped++;
868 return 0;
869 }
870 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530871 mwifiex_dbg(priv->adapter, ERROR,
872 "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200873 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700874 priv->stats.tx_dropped++;
875 return 0;
876 }
877 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530878 mwifiex_dbg(priv->adapter, DATA,
879 "data: Tx: insufficient skb headroom %d\n",
880 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700881 /* Insufficient skb headroom - allocate a new skb */
882 new_skb =
883 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
884 if (unlikely(!new_skb)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530885 mwifiex_dbg(priv->adapter, ERROR,
886 "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200887 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700888 priv->stats.tx_dropped++;
889 return 0;
890 }
891 kfree_skb(skb);
892 skb = new_skb;
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530893 mwifiex_dbg(priv->adapter, INFO,
894 "info: new skb headroomd %d\n",
895 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700896 }
897
898 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700899 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800900 tx_info->bss_num = priv->bss_num;
901 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800902 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700903
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800904 multicast = is_multicast_ether_addr(skb->data);
905
906 if (unlikely(!multicast && skb->sk &&
907 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
908 priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
909 skb = mwifiex_clone_skb_for_tx_status(priv,
910 skb,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800911 MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800912
Avinash Patil47411a02012-11-01 18:44:16 -0700913 /* Record the current time the packet was queued; used to
914 * determine the amount of time the packet was queued in
915 * the driver before it was sent to the firmware.
916 * The delay is then sent along with the packet to the
917 * firmware for aggregate delay calculation for stats and
918 * MSDU lifetime expiry.
919 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100920 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700921
Avinash Patil9927baa2014-11-13 21:54:16 +0530922 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
923 priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
924 !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
925 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
926 mwifiex_tdls_check_tx(priv, skb);
927 }
928
Stone Piaoe39faa72012-09-25 20:23:32 -0700929 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700930
931 return 0;
932}
933
934/*
935 * CFG802.11 network device handler for setting MAC address.
936 */
937static int
938mwifiex_set_mac_address(struct net_device *dev, void *addr)
939{
940 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700941 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700942 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700943
944 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
945
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700946 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800947 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
948 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700949
950 if (!ret)
951 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
952 else
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530953 mwifiex_dbg(priv->adapter, ERROR,
954 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700955
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700956 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
957
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700958 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700959}
960
961/*
962 * CFG802.11 network device handler for setting multicast list.
963 */
964static void mwifiex_set_multicast_list(struct net_device *dev)
965{
966 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700967 struct mwifiex_multicast_list mcast_list;
968
969 if (dev->flags & IFF_PROMISC) {
970 mcast_list.mode = MWIFIEX_PROMISC_MODE;
971 } else if (dev->flags & IFF_ALLMULTI ||
972 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
973 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
974 } else {
975 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400976 mcast_list.num_multicast_addr =
977 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700978 }
979 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700980}
981
982/*
983 * CFG802.11 network device handler for transmission timeout.
984 */
985static void
986mwifiex_tx_timeout(struct net_device *dev)
987{
988 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
989
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700990 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800991 priv->tx_timeout_cnt++;
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +0530992 mwifiex_dbg(priv->adapter, ERROR,
993 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
994 jiffies, priv->tx_timeout_cnt, priv->bss_type,
995 priv->bss_num);
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800996 mwifiex_set_trans_start(dev);
997
998 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
999 priv->adapter->if_ops.card_reset) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301000 mwifiex_dbg(priv->adapter, ERROR,
1001 "tx_timeout_cnt exceeds threshold.\t"
1002 "Triggering card reset!\n");
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -08001003 priv->adapter->if_ops.card_reset(priv->adapter);
1004 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001005}
1006
Zhaoyang Liu7e4e5d22015-09-18 06:32:17 -07001007void mwifiex_multi_chan_resync(struct mwifiex_adapter *adapter)
1008{
1009 struct usb_card_rec *card = adapter->card;
1010 struct mwifiex_private *priv;
1011 u16 tx_buf_size;
1012 int i, ret;
1013
1014 card->mc_resync_flag = true;
1015 for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1016 if (atomic_read(&card->port[i].tx_data_urb_pending)) {
1017 mwifiex_dbg(adapter, WARN, "pending data urb in sys\n");
1018 return;
1019 }
1020 }
1021
1022 card->mc_resync_flag = false;
1023 tx_buf_size = 0xffff;
1024 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1025 ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
1026 HostCmd_ACT_GEN_SET, 0, &tx_buf_size, false);
1027 if (ret)
1028 mwifiex_dbg(adapter, ERROR,
1029 "send reconfig tx buf size cmd err\n");
1030}
1031EXPORT_SYMBOL_GPL(mwifiex_multi_chan_resync);
1032
Xinming Hud27121f2016-11-16 18:39:07 +05301033int mwifiex_drv_info_dump(struct mwifiex_adapter *adapter, void **drv_info)
Xinming Hu11cd07a2014-12-23 19:14:10 +05301034{
1035 void *p;
1036 char drv_version[64];
1037 struct usb_card_rec *cardp;
1038 struct sdio_mmc_card *sdio_card;
1039 struct mwifiex_private *priv;
1040 int i, idx;
1041 struct netdev_queue *txq;
1042 struct mwifiex_debug_info *debug_info;
Xinming Hud27121f2016-11-16 18:39:07 +05301043 void *drv_info_dump;
Xinming Hu11cd07a2014-12-23 19:14:10 +05301044
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -07001045 mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump start===\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +05301046
Xinming Hud27121f2016-11-16 18:39:07 +05301047 /* memory allocate here should be free in mwifiex_upload_device_dump*/
1048 drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
Xinming Hu11cd07a2014-12-23 19:14:10 +05301049
Xinming Hud27121f2016-11-16 18:39:07 +05301050 if (!drv_info_dump)
1051 return 0;
Xinming Hu11cd07a2014-12-23 19:14:10 +05301052
Xinming Hud27121f2016-11-16 18:39:07 +05301053 p = (char *)(drv_info_dump);
Xinming Hu11cd07a2014-12-23 19:14:10 +05301054 p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
1055
1056 mwifiex_drv_get_driver_version(adapter, drv_version,
1057 sizeof(drv_version) - 1);
1058 p += sprintf(p, "driver_version = %s\n", drv_version);
1059
1060 if (adapter->iface_type == MWIFIEX_USB) {
1061 cardp = (struct usb_card_rec *)adapter->card;
1062 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
1063 atomic_read(&cardp->tx_cmd_urb_pending));
Zhaoyang Liu308fe292015-09-18 06:32:16 -07001064 p += sprintf(p, "tx_data_urb_pending_port_0 = %d\n",
1065 atomic_read(&cardp->port[0].tx_data_urb_pending));
1066 p += sprintf(p, "tx_data_urb_pending_port_1 = %d\n",
1067 atomic_read(&cardp->port[1].tx_data_urb_pending));
Xinming Hu11cd07a2014-12-23 19:14:10 +05301068 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
1069 atomic_read(&cardp->rx_cmd_urb_pending));
1070 p += sprintf(p, "rx_data_urb_pending = %d\n",
1071 atomic_read(&cardp->rx_data_urb_pending));
1072 }
1073
1074 p += sprintf(p, "tx_pending = %d\n",
1075 atomic_read(&adapter->tx_pending));
1076 p += sprintf(p, "rx_pending = %d\n",
1077 atomic_read(&adapter->rx_pending));
1078
1079 if (adapter->iface_type == MWIFIEX_SDIO) {
1080 sdio_card = (struct sdio_mmc_card *)adapter->card;
1081 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
1082 sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
1083 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
1084 sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
1085 }
1086
1087 for (i = 0; i < adapter->priv_num; i++) {
1088 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1089 continue;
1090 priv = adapter->priv[i];
1091 p += sprintf(p, "\n[interface : \"%s\"]\n",
1092 priv->netdev->name);
1093 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
1094 atomic_read(&priv->wmm_tx_pending[0]));
1095 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
1096 atomic_read(&priv->wmm_tx_pending[1]));
1097 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
1098 atomic_read(&priv->wmm_tx_pending[2]));
1099 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
1100 atomic_read(&priv->wmm_tx_pending[3]));
1101 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
1102 "Disconnected" : "Connected");
1103 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
1104 ? "on" : "off"));
1105 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
1106 txq = netdev_get_tx_queue(priv->netdev, idx);
1107 p += sprintf(p, "tx queue %d:%s ", idx,
1108 netif_tx_queue_stopped(txq) ?
1109 "stopped" : "started");
1110 }
1111 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
1112 priv->netdev->name, priv->num_tx_timeout);
1113 }
1114
Xinming Hu46469682016-04-05 01:04:40 -07001115 if (adapter->iface_type == MWIFIEX_SDIO ||
1116 adapter->iface_type == MWIFIEX_PCIE) {
1117 p += sprintf(p, "\n=== %s register dump===\n",
1118 adapter->iface_type == MWIFIEX_SDIO ?
1119 "SDIO" : "PCIE");
Xinming Hu809c6ea2014-12-23 19:14:11 +05301120 if (adapter->if_ops.reg_dump)
1121 p += adapter->if_ops.reg_dump(adapter, p);
1122 }
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -07001123 p += sprintf(p, "\n=== more debug information\n");
Xinming Hu11cd07a2014-12-23 19:14:10 +05301124 debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
1125 if (debug_info) {
1126 for (i = 0; i < adapter->priv_num; i++) {
1127 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
1128 continue;
1129 priv = adapter->priv[i];
1130 mwifiex_get_debug_info(priv, debug_info);
1131 p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
1132 break;
1133 }
1134 kfree(debug_info);
1135 }
1136
Amitkumar Karwar9cc0dbf02015-05-26 06:34:30 -07001137 mwifiex_dbg(adapter, MSG, "===mwifiex driverinfo dump end===\n");
Xinming Hud27121f2016-11-16 18:39:07 +05301138 *drv_info = drv_info_dump;
1139 return p - drv_info_dump;
Xinming Hu11cd07a2014-12-23 19:14:10 +05301140}
Amitkumar Karwarfc697152015-05-26 06:34:31 -07001141EXPORT_SYMBOL_GPL(mwifiex_drv_info_dump);
Xinming Hu11cd07a2014-12-23 19:14:10 +05301142
Xinming Hud27121f2016-11-16 18:39:07 +05301143void mwifiex_upload_device_dump(struct mwifiex_adapter *adapter, void *drv_info,
1144 int drv_info_size)
Amitkumar Karwar57670ee2015-05-26 06:34:32 -07001145{
1146 u8 idx, *dump_data, *fw_dump_ptr;
1147 u32 dump_len;
1148
1149 dump_len = (strlen("========Start dump driverinfo========\n") +
Xinming Hud27121f2016-11-16 18:39:07 +05301150 drv_info_size +
Amitkumar Karwar57670ee2015-05-26 06:34:32 -07001151 strlen("\n========End dump========\n"));
1152
1153 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1154 struct memory_type_mapping *entry =
1155 &adapter->mem_type_mapping_tbl[idx];
1156
1157 if (entry->mem_ptr) {
1158 dump_len += (strlen("========Start dump ") +
1159 strlen(entry->mem_name) +
1160 strlen("========\n") +
1161 (entry->mem_size + 1) +
1162 strlen("\n========End dump========\n"));
1163 }
1164 }
1165
1166 dump_data = vzalloc(dump_len + 1);
1167 if (!dump_data)
1168 goto done;
1169
1170 fw_dump_ptr = dump_data;
1171
1172 /* Dump all the memory data into single file, a userspace script will
1173 * be used to split all the memory data to multiple files
1174 */
1175 mwifiex_dbg(adapter, MSG,
1176 "== mwifiex dump information to /sys/class/devcoredump start");
1177
1178 strcpy(fw_dump_ptr, "========Start dump driverinfo========\n");
1179 fw_dump_ptr += strlen("========Start dump driverinfo========\n");
Xinming Hud27121f2016-11-16 18:39:07 +05301180 memcpy(fw_dump_ptr, drv_info, drv_info_size);
1181 fw_dump_ptr += drv_info_size;
Amitkumar Karwar57670ee2015-05-26 06:34:32 -07001182 strcpy(fw_dump_ptr, "\n========End dump========\n");
1183 fw_dump_ptr += strlen("\n========End dump========\n");
1184
1185 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1186 struct memory_type_mapping *entry =
1187 &adapter->mem_type_mapping_tbl[idx];
1188
1189 if (entry->mem_ptr) {
1190 strcpy(fw_dump_ptr, "========Start dump ");
1191 fw_dump_ptr += strlen("========Start dump ");
1192
1193 strcpy(fw_dump_ptr, entry->mem_name);
1194 fw_dump_ptr += strlen(entry->mem_name);
1195
1196 strcpy(fw_dump_ptr, "========\n");
1197 fw_dump_ptr += strlen("========\n");
1198
1199 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1200 fw_dump_ptr += entry->mem_size;
1201
1202 strcpy(fw_dump_ptr, "\n========End dump========\n");
1203 fw_dump_ptr += strlen("\n========End dump========\n");
1204 }
1205 }
1206
1207 /* device dump data will be free in device coredump release function
1208 * after 5 min
1209 */
1210 dev_coredumpv(adapter->dev, dump_data, dump_len, GFP_KERNEL);
1211 mwifiex_dbg(adapter, MSG,
1212 "== mwifiex dump information to /sys/class/devcoredump end");
1213
1214done:
1215 for (idx = 0; idx < adapter->num_mem_types; idx++) {
1216 struct memory_type_mapping *entry =
1217 &adapter->mem_type_mapping_tbl[idx];
1218
Xinming Hud27121f2016-11-16 18:39:07 +05301219 vfree(entry->mem_ptr);
1220 entry->mem_ptr = NULL;
Amitkumar Karwar57670ee2015-05-26 06:34:32 -07001221 entry->mem_size = 0;
1222 }
1223
Xinming Hud27121f2016-11-16 18:39:07 +05301224 vfree(drv_info);
Amitkumar Karwar57670ee2015-05-26 06:34:32 -07001225}
1226EXPORT_SYMBOL_GPL(mwifiex_upload_device_dump);
1227
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001228/*
1229 * CFG802.11 network device handler for statistics retrieval.
1230 */
1231static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
1232{
1233 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
1234
1235 return &priv->stats;
1236}
1237
Avinash Patil47411a02012-11-01 18:44:16 -07001238static u16
Jason Wangf663dd92014-01-10 16:18:26 +08001239mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +01001240 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -07001241{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -08001242 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -07001243 return mwifiex_1d_to_wmm_queue[skb->priority];
1244}
1245
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001246/* Network device handlers */
1247static const struct net_device_ops mwifiex_netdev_ops = {
1248 .ndo_open = mwifiex_open,
1249 .ndo_stop = mwifiex_close,
1250 .ndo_start_xmit = mwifiex_hard_start_xmit,
1251 .ndo_set_mac_address = mwifiex_set_mac_address,
Amitkumar Karwarfe243722015-10-09 04:26:34 -07001252 .ndo_validate_addr = eth_validate_addr,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001253 .ndo_tx_timeout = mwifiex_tx_timeout,
1254 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001255 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -07001256 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001257};
1258
1259/*
1260 * This function initializes the private structure parameters.
1261 *
1262 * The following wait queues are initialized -
1263 * - IOCTL wait queue
1264 * - Command wait queue
1265 * - Statistics wait queue
1266 *
1267 * ...and the following default parameters are set -
1268 * - Current key index : Set to 0
1269 * - Rate index : Set to auto
1270 * - Media connected : Set to disconnected
1271 * - Adhoc link sensed : Set to false
1272 * - Nick name : Set to null
1273 * - Number of Tx timeout : Set to 0
1274 * - Device address : Set to current address
Xinming Hucbf6e052014-12-23 19:14:07 +05301275 * - Rx histogram statistc : Set to 0
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001276 *
1277 * In addition, the CFG80211 work queue is also created.
1278 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001279void mwifiex_init_priv_params(struct mwifiex_private *priv,
Avinash Patil047eaaf2015-01-28 15:42:05 +05301280 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001281{
1282 dev->netdev_ops = &mwifiex_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -04001283 dev->needs_free_netdev = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001284 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001285 priv->current_key_index = 0;
1286 priv->media_connected = false;
Avinash Patilede98bf2012-05-08 18:30:28 -07001287 memset(priv->mgmt_ie, 0,
1288 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -07001289 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
1290 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
1291 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
Avinash Patil2ade5662015-01-28 15:54:20 +05301292 priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001293 priv->num_tx_timeout = 0;
Avinash Patil8d05eb22015-01-28 15:42:01 +05301294 ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001295 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
Xinming Hucbf6e052014-12-23 19:14:07 +05301296
1297 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
1298 GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
1299 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
1300 if (priv->hist_data)
1301 mwifiex_hist_data_reset(priv);
1302 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001303}
1304
1305/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001306 * This function check if command is pending.
1307 */
1308int is_command_pending(struct mwifiex_adapter *adapter)
1309{
1310 unsigned long flags;
1311 int is_cmd_pend_q_empty;
1312
1313 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1314 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1315 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1316
1317 return !is_cmd_pend_q_empty;
1318}
1319
1320/*
Avinash Patil6e251172014-09-12 20:08:59 +05301321 * This is the RX work queue function.
1322 *
1323 * It handles the RX operations.
1324 */
1325static void mwifiex_rx_work_queue(struct work_struct *work)
1326{
1327 struct mwifiex_adapter *adapter =
1328 container_of(work, struct mwifiex_adapter, rx_work);
1329
1330 if (adapter->surprise_removed)
1331 return;
1332 mwifiex_process_rx(adapter);
1333}
1334
1335/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001336 * This is the main work queue function.
1337 *
1338 * It handles the main process, which in turn handles the complete
1339 * driver operations.
1340 */
1341static void mwifiex_main_work_queue(struct work_struct *work)
1342{
1343 struct mwifiex_adapter *adapter =
1344 container_of(work, struct mwifiex_adapter, main_work);
1345
1346 if (adapter->surprise_removed)
1347 return;
1348 mwifiex_main_process(adapter);
1349}
1350
1351/*
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301352 * This function gets called during PCIe function level reset. Required
1353 * code is extracted from mwifiex_remove_card()
1354 */
Xinming Hu8750ab62016-12-14 19:40:47 +05301355int
Brian Norris4a79aa12016-11-18 19:30:26 +05301356mwifiex_shutdown_sw(struct mwifiex_adapter *adapter)
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301357{
1358 struct mwifiex_private *priv;
1359 int i;
1360
Colin Ian King80ba4f12016-09-16 10:37:31 +01001361 if (!adapter)
1362 goto exit_return;
1363
Brian Norris4a79aa12016-11-18 19:30:26 +05301364 wait_for_completion(adapter->fw_done);
1365 /* Caller should ensure we aren't suspending while this happens */
1366 reinit_completion(adapter->fw_done);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301367
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301368 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
1369 mwifiex_deauthenticate(priv, NULL);
1370
1371 /* We can no longer handle interrupts once we start doing the teardown
1372 * below.
1373 */
1374 if (adapter->if_ops.disable_int)
1375 adapter->if_ops.disable_int(adapter);
1376
1377 adapter->surprise_removed = true;
1378 mwifiex_terminate_workqueue(adapter);
1379
1380 /* Stop data */
1381 for (i = 0; i < adapter->priv_num; i++) {
1382 priv = adapter->priv[i];
1383 if (priv && priv->netdev) {
1384 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
1385 if (netif_carrier_ok(priv->netdev))
1386 netif_carrier_off(priv->netdev);
1387 netif_device_detach(priv->netdev);
1388 }
1389 }
1390
1391 mwifiex_dbg(adapter, CMD, "cmd: calling mwifiex_shutdown_drv...\n");
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301392
Xinming Hu5bf15e32016-11-16 18:39:05 +05301393 mwifiex_shutdown_drv(adapter);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301394 if (adapter->if_ops.down_dev)
1395 adapter->if_ops.down_dev(adapter);
1396
1397 mwifiex_dbg(adapter, CMD, "cmd: mwifiex_shutdown_drv done\n");
1398 if (atomic_read(&adapter->rx_pending) ||
1399 atomic_read(&adapter->tx_pending) ||
1400 atomic_read(&adapter->cmd_pending)) {
1401 mwifiex_dbg(adapter, ERROR,
1402 "rx_pending=%d, tx_pending=%d,\t"
1403 "cmd_pending=%d\n",
1404 atomic_read(&adapter->rx_pending),
1405 atomic_read(&adapter->tx_pending),
1406 atomic_read(&adapter->cmd_pending));
1407 }
1408
1409 for (i = 0; i < adapter->priv_num; i++) {
1410 priv = adapter->priv[i];
1411 if (!priv)
1412 continue;
1413 rtnl_lock();
1414 if (priv->netdev &&
1415 priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1416 mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
1417 rtnl_unlock();
1418 }
Brian Norrisfb9e67b2017-04-14 14:51:20 -07001419 vfree(adapter->chan_stats);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301420
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301421 mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
Colin Ian King80ba4f12016-09-16 10:37:31 +01001422exit_return:
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301423 return 0;
1424}
Xinming Hu8750ab62016-12-14 19:40:47 +05301425EXPORT_SYMBOL_GPL(mwifiex_shutdown_sw);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301426
1427/* This function gets called during PCIe function level reset. Required
1428 * code is extracted from mwifiex_add_card()
1429 */
Xinming Hu8750ab62016-12-14 19:40:47 +05301430int
1431mwifiex_reinit_sw(struct mwifiex_adapter *adapter)
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301432{
Brian Norris755b37c2017-03-28 16:59:33 -07001433 int ret;
1434
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301435 mwifiex_init_lock_list(adapter);
1436 if (adapter->if_ops.up_dev)
1437 adapter->if_ops.up_dev(adapter);
1438
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301439 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
1440 adapter->surprise_removed = false;
1441 init_waitqueue_head(&adapter->init_wait_q);
1442 adapter->is_suspended = false;
1443 adapter->hs_activated = false;
Brian Norris9ae3fbd2017-04-14 14:51:18 -07001444 adapter->is_cmd_timedout = 0;
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301445 init_waitqueue_head(&adapter->hs_activate_wait_q);
1446 init_waitqueue_head(&adapter->cmd_wait_q.wait);
1447 adapter->cmd_wait_q.status = 0;
1448 adapter->scan_wait_q_woken = false;
1449
1450 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB)
1451 adapter->rx_work_enabled = true;
1452
1453 adapter->workqueue =
1454 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1455 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
1456 if (!adapter->workqueue)
1457 goto err_kmalloc;
1458
1459 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
1460
1461 if (adapter->rx_work_enabled) {
1462 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1463 WQ_HIGHPRI |
1464 WQ_MEM_RECLAIM |
1465 WQ_UNBOUND, 1);
1466 if (!adapter->rx_workqueue)
1467 goto err_kmalloc;
1468 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1469 }
1470
1471 /* Register the device. Fill up the private data structure with
1472 * relevant information from the card. Some code extracted from
1473 * mwifiex_register_dev()
1474 */
1475 mwifiex_dbg(adapter, INFO, "%s, mwifiex_init_hw_fw()...\n", __func__);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301476
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301477 if (mwifiex_init_hw_fw(adapter, false)) {
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301478 mwifiex_dbg(adapter, ERROR,
1479 "%s: firmware init failed\n", __func__);
1480 goto err_init_fw;
1481 }
Brian Norris755b37c2017-03-28 16:59:33 -07001482
1483 /* _mwifiex_fw_dpc() does its own cleanup */
1484 ret = _mwifiex_fw_dpc(adapter->firmware, adapter);
1485 if (ret) {
1486 pr_err("Failed to bring up adapter: %d\n", ret);
1487 return ret;
1488 }
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301489 mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
Brian Norris4a79aa12016-11-18 19:30:26 +05301490
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301491 return 0;
1492
1493err_init_fw:
1494 mwifiex_dbg(adapter, ERROR, "info: %s: unregister device\n", __func__);
1495 if (adapter->if_ops.unregister_dev)
1496 adapter->if_ops.unregister_dev(adapter);
Xinming Hu5bf15e32016-11-16 18:39:05 +05301497
1498err_kmalloc:
1499 adapter->surprise_removed = true;
1500 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301501 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1502 mwifiex_dbg(adapter, ERROR,
1503 "info: %s: shutdown mwifiex\n", __func__);
Xinming Hu5bf15e32016-11-16 18:39:05 +05301504 mwifiex_shutdown_drv(adapter);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301505 }
1506
Brian Norris4a79aa12016-11-18 19:30:26 +05301507 complete_all(adapter->fw_done);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301508 mwifiex_dbg(adapter, INFO, "%s, error\n", __func__);
1509
1510 return -1;
1511}
Xinming Hu8750ab62016-12-14 19:40:47 +05301512EXPORT_SYMBOL_GPL(mwifiex_reinit_sw);
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301513
Rajat Jain853402a2016-11-15 19:06:04 +05301514static irqreturn_t mwifiex_irq_wakeup_handler(int irq, void *priv)
1515{
1516 struct mwifiex_adapter *adapter = priv;
1517
Xinming Hu625b4db2017-04-13 06:48:19 +00001518 dev_dbg(adapter->dev, "%s: wake by wifi", __func__);
1519 adapter->wake_by_wifi = true;
1520 disable_irq_nosync(irq);
Rajat Jain853402a2016-11-15 19:06:04 +05301521
1522 /* Notify PM core we are wakeup source */
1523 pm_wakeup_event(adapter->dev, 0);
Jeffy Chenef7e0712017-02-24 14:24:31 +08001524 pm_system_wakeup();
Rajat Jain853402a2016-11-15 19:06:04 +05301525
1526 return IRQ_HANDLED;
1527}
1528
Rajat Jain5e28e5f2016-11-15 19:06:03 +05301529static void mwifiex_probe_of(struct mwifiex_adapter *adapter)
1530{
Rajat Jain853402a2016-11-15 19:06:04 +05301531 int ret;
Rajat Jain5e28e5f2016-11-15 19:06:03 +05301532 struct device *dev = adapter->dev;
1533
1534 if (!dev->of_node)
Brian Norris2447e2c2017-02-10 13:55:25 -08001535 goto err_exit;
Rajat Jain5e28e5f2016-11-15 19:06:03 +05301536
1537 adapter->dt_node = dev->of_node;
Rajat Jain853402a2016-11-15 19:06:04 +05301538 adapter->irq_wakeup = irq_of_parse_and_map(adapter->dt_node, 0);
1539 if (!adapter->irq_wakeup) {
Brian Norris2447e2c2017-02-10 13:55:25 -08001540 dev_dbg(dev, "fail to parse irq_wakeup from device tree\n");
1541 goto err_exit;
Rajat Jain853402a2016-11-15 19:06:04 +05301542 }
1543
1544 ret = devm_request_irq(dev, adapter->irq_wakeup,
1545 mwifiex_irq_wakeup_handler, IRQF_TRIGGER_LOW,
1546 "wifi_wake", adapter);
1547 if (ret) {
1548 dev_err(dev, "Failed to request irq_wakeup %d (%d)\n",
1549 adapter->irq_wakeup, ret);
1550 goto err_exit;
1551 }
1552
1553 disable_irq(adapter->irq_wakeup);
1554 if (device_init_wakeup(dev, true)) {
1555 dev_err(dev, "fail to init wakeup for mwifiex\n");
1556 goto err_exit;
1557 }
1558 return;
1559
1560err_exit:
Brian Norris2447e2c2017-02-10 13:55:25 -08001561 adapter->irq_wakeup = -1;
Rajat Jain5e28e5f2016-11-15 19:06:03 +05301562}
1563
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301564/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001565 * This function adds the card.
1566 *
1567 * This function follows the following major steps to set up the device -
1568 * - Initialize software. This includes probing the card, registering
1569 * the interface operations table, and allocating/initializing the
1570 * adapter structure
1571 * - Set up the netlink socket
1572 * - Create and start the main work queue
1573 * - Register the device
1574 * - Initialize firmware and hardware
1575 * - Add logical interfaces
1576 */
1577int
Brian Norris4a79aa12016-11-18 19:30:26 +05301578mwifiex_add_card(void *card, struct completion *fw_done,
Rajat Jain2e02b582016-11-15 19:06:02 +05301579 struct mwifiex_if_ops *if_ops, u8 iface_type,
1580 struct device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001581{
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001582 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001583
Brian Norrisba1c7e42017-03-10 17:39:22 -08001584 if (mwifiex_register(card, dev, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001585 pr_err("%s: software init failed\n", __func__);
1586 goto err_init_sw;
1587 }
1588
Rajat Jain5e28e5f2016-11-15 19:06:03 +05301589 mwifiex_probe_of(adapter);
1590
Amitkumar Karward930fae2011-10-11 17:41:21 -07001591 adapter->iface_type = iface_type;
Brian Norris4a79aa12016-11-18 19:30:26 +05301592 adapter->fw_done = fw_done;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001593
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001594 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001595 adapter->surprise_removed = false;
1596 init_waitqueue_head(&adapter->init_wait_q);
1597 adapter->is_suspended = false;
1598 adapter->hs_activated = false;
1599 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001600 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001601 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -07001602 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001603
Avinash Patilec4a16b2014-11-05 17:04:27 +05301604 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +05301605 adapter->rx_work_enabled = true;
1606 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
1607 }
1608
Amitkumar Karwar448a71c2013-10-11 18:33:04 -07001609 adapter->workqueue =
1610 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1611 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001612 if (!adapter->workqueue)
1613 goto err_kmalloc;
1614
1615 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Avinash Patil6e251172014-09-12 20:08:59 +05301616
1617 if (adapter->rx_work_enabled) {
1618 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1619 WQ_HIGHPRI |
1620 WQ_MEM_RECLAIM |
1621 WQ_UNBOUND, 1);
1622 if (!adapter->rx_workqueue)
1623 goto err_kmalloc;
1624
1625 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1626 }
1627
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001628 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -04001629 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001630 if (adapter->if_ops.register_dev(adapter)) {
1631 pr_err("%s: failed to register mwifiex device\n", __func__);
1632 goto err_registerdev;
1633 }
1634
Amitkumar Karwar4c5dae52016-07-26 19:31:44 +05301635 if (mwifiex_init_hw_fw(adapter, true)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001636 pr_err("%s: firmware init failed\n", __func__);
1637 goto err_init_fw;
1638 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001639
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001640 return 0;
1641
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001642err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001643 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001644 if (adapter->if_ops.unregister_dev)
1645 adapter->if_ops.unregister_dev(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001646err_registerdev:
1647 adapter->surprise_removed = true;
1648 mwifiex_terminate_workqueue(adapter);
Xinming Hu5bf15e32016-11-16 18:39:05 +05301649 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
1650 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1651 mwifiex_shutdown_drv(adapter);
1652 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001653err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001654 mwifiex_free_adapter(adapter);
1655
1656err_init_sw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001657
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001658 return -1;
1659}
1660EXPORT_SYMBOL_GPL(mwifiex_add_card);
1661
1662/*
1663 * This function removes the card.
1664 *
1665 * This function follows the following major steps to remove the device -
1666 * - Stop data traffic
1667 * - Shutdown firmware
1668 * - Remove the logical interfaces
1669 * - Terminate the work queue
1670 * - Unregister the device
1671 * - Free the adapter structure
1672 */
Brian Norris4a79aa12016-11-18 19:30:26 +05301673int mwifiex_remove_card(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001674{
1675 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001676 int i;
1677
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001678 if (!adapter)
1679 goto exit_remove;
1680
Daniel Drake232fde02013-07-13 10:57:10 -04001681 /* We can no longer handle interrupts once we start doing the teardown
1682 * below. */
1683 if (adapter->if_ops.disable_int)
1684 adapter->if_ops.disable_int(adapter);
1685
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001686 adapter->surprise_removed = true;
1687
Marc Yang9d2e85e2014-12-31 02:36:39 -08001688 mwifiex_terminate_workqueue(adapter);
1689
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001690 /* Stop data */
1691 for (i = 0; i < adapter->priv_num; i++) {
1692 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001693 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -07001694 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001695 if (netif_carrier_ok(priv->netdev))
1696 netif_carrier_off(priv->netdev);
1697 }
1698 }
1699
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301700 mwifiex_dbg(adapter, CMD,
1701 "cmd: calling mwifiex_shutdown_drv...\n");
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001702
Xinming Hu5bf15e32016-11-16 18:39:05 +05301703 mwifiex_shutdown_drv(adapter);
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301704 mwifiex_dbg(adapter, CMD,
1705 "cmd: mwifiex_shutdown_drv done\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001706 if (atomic_read(&adapter->rx_pending) ||
1707 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001708 atomic_read(&adapter->cmd_pending)) {
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301709 mwifiex_dbg(adapter, ERROR,
1710 "rx_pending=%d, tx_pending=%d,\t"
1711 "cmd_pending=%d\n",
1712 atomic_read(&adapter->rx_pending),
1713 atomic_read(&adapter->tx_pending),
1714 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001715 }
1716
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001717 for (i = 0; i < adapter->priv_num; i++) {
1718 priv = adapter->priv[i];
1719
1720 if (!priv)
1721 continue;
1722
1723 rtnl_lock();
Avinash Patil4facc342015-01-28 15:42:00 +05301724 if (priv->netdev &&
1725 priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1726 mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001727 rtnl_unlock();
1728 }
Brian Norrisfb9e67b2017-04-14 14:51:20 -07001729 vfree(adapter->chan_stats);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001730
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -08001731 wiphy_unregister(adapter->wiphy);
1732 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -07001733
Brian Norris36908c42017-03-10 17:39:23 -08001734 if (adapter->irq_wakeup >= 0)
1735 device_init_wakeup(adapter->dev, false);
1736
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001737 /* Unregister device */
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301738 mwifiex_dbg(adapter, INFO,
1739 "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001740 if (adapter->if_ops.unregister_dev)
1741 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001742 /* Free adapter structure */
Zhaoyang Liuacebe8c2015-05-12 00:48:20 +05301743 mwifiex_dbg(adapter, INFO,
1744 "info: free adapter\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001745 mwifiex_free_adapter(adapter);
1746
1747exit_remove:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001748 return 0;
1749}
1750EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1751
Joe Perches36925e52015-08-31 10:46:45 -07001752void _mwifiex_dbg(const struct mwifiex_adapter *adapter, int mask,
1753 const char *fmt, ...)
1754{
1755 struct va_format vaf;
1756 va_list args;
1757
Xinming Huef6c7d32017-04-13 06:48:20 +00001758 if (!(adapter->debug_mask & mask))
Joe Perches36925e52015-08-31 10:46:45 -07001759 return;
1760
1761 va_start(args, fmt);
1762
1763 vaf.fmt = fmt;
1764 vaf.va = &args;
1765
Xinming Huef6c7d32017-04-13 06:48:20 +00001766 if (adapter->dev)
1767 dev_info(adapter->dev, "%pV", &vaf);
1768 else
1769 pr_info("%pV", &vaf);
Joe Perches36925e52015-08-31 10:46:45 -07001770
1771 va_end(args);
1772}
1773EXPORT_SYMBOL_GPL(_mwifiex_dbg);
1774
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001775/*
1776 * This function initializes the module.
1777 *
1778 * The debug FS is also initialized if configured.
1779 */
1780static int
1781mwifiex_init_module(void)
1782{
1783#ifdef CONFIG_DEBUG_FS
1784 mwifiex_debugfs_init();
1785#endif
1786 return 0;
1787}
1788
1789/*
1790 * This function cleans up the module.
1791 *
1792 * The debug FS is removed if available.
1793 */
1794static void
1795mwifiex_cleanup_module(void)
1796{
1797#ifdef CONFIG_DEBUG_FS
1798 mwifiex_debugfs_remove();
1799#endif
1800}
1801
1802module_init(mwifiex_init_module);
1803module_exit(mwifiex_cleanup_module);
1804
1805MODULE_AUTHOR("Marvell International Ltd.");
1806MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1807MODULE_VERSION(VERSION);
1808MODULE_LICENSE("GPL v2");