blob: 7e74b4fccddd56e67eb8b28453553e4083eec895 [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: major functions
3 *
Xinming Hu65da33f2014-06-19 21:38:57 -07004 * Copyright (C) 2011-2014, Marvell International Ltd.
Bing Zhao5e6e3a92011-03-21 18:00:50 -07005 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
18 */
19
20#include "main.h"
21#include "wmm.h"
22#include "cfg80211.h"
23#include "11n.h"
24
25#define VERSION "1.0"
26
27const char driver_version[] = "mwifiex " VERSION " (%s) ";
Amitkumar Karwar388ec382013-05-17 17:50:25 -070028static char *cal_data_cfg;
29module_param(cal_data_cfg, charp, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070030
Avinash Patil0013c7c2014-11-05 19:38:11 +053031static unsigned short driver_mode;
32module_param(driver_mode, ushort, 0);
33MODULE_PARM_DESC(driver_mode,
34 "station=0x1(default), ap-sta=0x3, station-p2p=0x5, ap-sta-p2p=0x7");
35
Bing Zhao5e6e3a92011-03-21 18:00:50 -070036/*
37 * This function registers the device and performs all the necessary
38 * initializations.
39 *
40 * The following initialization operations are performed -
41 * - Allocate adapter structure
42 * - Save interface specific operations table in adapter
43 * - Call interface specific initialization routine
44 * - Allocate private structures
45 * - Set default adapter structure parameters
46 * - Initialize locks
47 *
48 * In case of any errors during inittialization, this function also ensures
49 * proper cleanup before exiting.
50 */
51static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
Amitkumar Karwar287546d2011-06-08 20:39:20 +053052 void **padapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070053{
Amitkumar Karwar2be78592011-04-15 20:50:42 -070054 struct mwifiex_adapter *adapter;
55 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070056
57 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070058 if (!adapter)
Christoph Fritzb53575e2011-05-08 22:50:09 +020059 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070060
Amitkumar Karwar287546d2011-06-08 20:39:20 +053061 *padapter = adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070062 adapter->card = card;
63
64 /* Save interface specific operations in adapter */
65 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
66
67 /* card specific initialization has been deferred until now .. */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070068 if (adapter->if_ops.init_if)
69 if (adapter->if_ops.init_if(adapter))
70 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070071
72 adapter->priv_num = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070073
Avinash Patil64b05e22012-05-08 18:30:13 -070074 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
75 /* Allocate memory for private structure */
76 adapter->priv[i] =
77 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
78 if (!adapter->priv[i])
79 goto error;
80
81 adapter->priv[i]->adapter = adapter;
Avinash Patil64b05e22012-05-08 18:30:13 -070082 adapter->priv_num++;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070083 }
Amitkumar Karwar44b815c2011-09-29 20:43:41 -070084 mwifiex_init_lock_list(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070085
Julia Lawallc6c33e72014-12-26 15:35:56 +010086 setup_timer(&adapter->cmd_timer, mwifiex_cmd_timeout_func,
87 (unsigned long)adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070088
Bing Zhao5e6e3a92011-03-21 18:00:50 -070089 return 0;
90
91error:
92 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
93
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -070094 for (i = 0; i < adapter->priv_num; i++)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070095 kfree(adapter->priv[i]);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -070096
Bing Zhao5e6e3a92011-03-21 18:00:50 -070097 kfree(adapter);
98
99 return -1;
100}
101
102/*
103 * This function unregisters the device and performs all the necessary
104 * cleanups.
105 *
106 * The following cleanup operations are performed -
107 * - Free the timers
108 * - Free beacon buffers
109 * - Free private structures
110 * - Free adapter structure
111 */
112static int mwifiex_unregister(struct mwifiex_adapter *adapter)
113{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700114 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700115
Amitkumar Karwar4cfda672013-07-22 19:17:50 -0700116 if (adapter->if_ops.cleanup_if)
117 adapter->if_ops.cleanup_if(adapter);
118
Avinash Patil629873f2014-02-18 15:47:55 -0800119 del_timer_sync(&adapter->cmd_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700120
121 /* Free private structures */
122 for (i = 0; i < adapter->priv_num; i++) {
123 if (adapter->priv[i]) {
124 mwifiex_free_curr_bcn(adapter->priv[i]);
125 kfree(adapter->priv[i]);
126 }
127 }
128
Avinash Patilbf354432014-10-31 16:08:26 +0530129 vfree(adapter->chan_stats);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700130 kfree(adapter);
131 return 0;
132}
133
Avinash Patil6e251172014-09-12 20:08:59 +0530134static int mwifiex_process_rx(struct mwifiex_adapter *adapter)
135{
136 unsigned long flags;
137 struct sk_buff *skb;
Avinash Patil6e251172014-09-12 20:08:59 +0530138
139 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
140 if (adapter->rx_processing || adapter->rx_locked) {
141 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
142 goto exit_rx_proc;
143 } else {
144 adapter->rx_processing = true;
145 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
146 }
147
148 /* Check for Rx data */
149 while ((skb = skb_dequeue(&adapter->rx_data_q))) {
150 atomic_dec(&adapter->rx_pending);
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800151 if ((adapter->delay_main_work ||
152 adapter->iface_type == MWIFIEX_USB) &&
Avinash Patilb43a0d92014-09-29 21:44:14 +0530153 (atomic_read(&adapter->rx_pending) < LOW_RX_PENDING)) {
Amitkumar Karwarcf6a64f2014-11-05 17:04:29 +0530154 if (adapter->if_ops.submit_rem_rx_urbs)
155 adapter->if_ops.submit_rem_rx_urbs(adapter);
Avinash Patil6e251172014-09-12 20:08:59 +0530156 adapter->delay_main_work = false;
Avinash Patilb43a0d92014-09-29 21:44:14 +0530157 queue_work(adapter->workqueue, &adapter->main_work);
Avinash Patil6e251172014-09-12 20:08:59 +0530158 }
159 mwifiex_handle_rx_packet(adapter, skb);
160 }
161 spin_lock_irqsave(&adapter->rx_proc_lock, flags);
162 adapter->rx_processing = false;
163 spin_unlock_irqrestore(&adapter->rx_proc_lock, flags);
164
Avinash Patil6e251172014-09-12 20:08:59 +0530165exit_rx_proc:
166 return 0;
167}
168
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700169/*
170 * The main process.
171 *
172 * This function is the main procedure of the driver and handles various driver
173 * operations. It runs in a loop and provides the core functionalities.
174 *
175 * The main responsibilities of this function are -
176 * - Ensure concurrency control
177 * - Handle pending interrupts and call interrupt handlers
178 * - Wake up the card if required
179 * - Handle command responses and call response handlers
180 * - Handle events and call event handlers
181 * - Execute pending commands
182 * - Transmit pending data packets
183 */
184int mwifiex_main_process(struct mwifiex_adapter *adapter)
185{
186 int ret = 0;
187 unsigned long flags;
188
189 spin_lock_irqsave(&adapter->main_proc_lock, flags);
190
191 /* Check if already processing */
192 if (adapter->mwifiex_processing) {
193 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
194 goto exit_main_proc;
195 } else {
196 adapter->mwifiex_processing = true;
197 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
198 }
199process_start:
200 do {
201 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
202 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
203 break;
204
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800205 /* For non-USB interfaces, If we process interrupts first, it
206 * would increase RX pending even further. Avoid this by
207 * checking if rx_pending has crossed high threshold and
208 * schedule rx work queue and then process interrupts.
209 * For USB interface, there are no interrupts. We already have
210 * HIGH_RX_PENDING check in usb.c
Avinash Patil6e251172014-09-12 20:08:59 +0530211 */
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800212 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
213 adapter->iface_type != MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +0530214 adapter->delay_main_work = true;
215 if (!adapter->rx_processing)
216 queue_work(adapter->rx_workqueue,
217 &adapter->rx_work);
218 break;
219 }
220
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700221 /* Handle pending interrupt if any */
222 if (adapter->int_status) {
223 if (adapter->hs_activated)
224 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700225 if (adapter->if_ops.process_int_status)
226 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700227 }
228
Avinash Patil6e251172014-09-12 20:08:59 +0530229 if (adapter->rx_work_enabled && adapter->data_received)
230 queue_work(adapter->rx_workqueue, &adapter->rx_work);
231
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700232 /* Need to wake up the card ? */
233 if ((adapter->ps_state == PS_STATE_SLEEP) &&
234 (adapter->pm_wakeup_card_req &&
235 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700236 (is_command_pending(adapter) ||
237 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700238 adapter->pm_wakeup_fw_try = true;
Amitkumar Karwar46361872014-12-31 02:36:41 -0800239 mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700240 adapter->if_ops.wakeup(adapter);
241 continue;
242 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700243
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700244 if (IS_CARD_RX_RCVD(adapter)) {
Avinash Patil6e251172014-09-12 20:08:59 +0530245 adapter->data_received = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700246 adapter->pm_wakeup_fw_try = false;
Amitkumar Karwar46361872014-12-31 02:36:41 -0800247 del_timer_sync(&adapter->wakeup_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700248 if (adapter->ps_state == PS_STATE_SLEEP)
249 adapter->ps_state = PS_STATE_AWAKE;
250 } else {
251 /* We have tried to wakeup the card already */
252 if (adapter->pm_wakeup_fw_try)
253 break;
254 if (adapter->ps_state != PS_STATE_AWAKE ||
255 adapter->tx_lock_flag)
256 break;
257
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530258 if ((!adapter->scan_chan_gap_enabled &&
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530259 adapter->scan_processing) || adapter->data_sent ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700260 mwifiex_wmm_lists_empty(adapter)) {
261 if (adapter->cmd_sent || adapter->curr_cmd ||
262 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700263 break;
264 }
265 }
266
Amitkumar Karwar20474122014-04-14 15:31:05 -0700267 /* Check for event */
268 if (adapter->event_received) {
269 adapter->event_received = false;
270 mwifiex_process_event(adapter);
271 }
272
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700273 /* Check for Cmd Resp */
274 if (adapter->cmd_resp_received) {
275 adapter->cmd_resp_received = false;
276 mwifiex_process_cmdresp(adapter);
277
278 /* call mwifiex back when init_fw is done */
279 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
280 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
281 mwifiex_init_fw_complete(adapter);
282 }
283 }
284
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700285 /* Check if we need to confirm Sleep Request
286 received previously */
287 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
288 if (!adapter->cmd_sent && !adapter->curr_cmd)
289 mwifiex_check_ps_cond(adapter);
290 }
291
292 /* * The ps_state may have been changed during processing of
293 * Sleep Request event.
294 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700295 if ((adapter->ps_state == PS_STATE_SLEEP) ||
296 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
297 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
298 adapter->tx_lock_flag)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700299 continue;
300
301 if (!adapter->cmd_sent && !adapter->curr_cmd) {
302 if (mwifiex_exec_next_cmd(adapter) == -1) {
303 ret = -1;
304 break;
305 }
306 }
307
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530308 if ((adapter->scan_chan_gap_enabled ||
Amitkumar Karward8d91252014-09-12 20:08:58 +0530309 !adapter->scan_processing) &&
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700310 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700311 mwifiex_wmm_process_tx(adapter);
312 if (adapter->hs_activated) {
313 adapter->is_hs_configured = false;
314 mwifiex_hs_activated_event
315 (mwifiex_get_priv
316 (adapter, MWIFIEX_BSS_ROLE_ANY),
317 false);
318 }
319 }
320
321 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700322 !adapter->curr_cmd && !is_command_pending(adapter) &&
323 mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700324 if (!mwifiex_send_null_packet
325 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
326 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
327 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
328 adapter->delay_null_pkt = false;
329 adapter->ps_state = PS_STATE_SLEEP;
330 }
331 break;
332 }
333 } while (true);
334
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700335 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Avinash Patilf73e5572014-09-29 21:44:13 +0530336 if (!adapter->delay_main_work &&
337 (adapter->int_status || IS_CARD_RX_RCVD(adapter))) {
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700338 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
339 goto process_start;
340 }
341
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700342 adapter->mwifiex_processing = false;
343 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
344
345exit_main_proc:
346 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
347 mwifiex_shutdown_drv(adapter);
348 return ret;
349}
Bing Zhao601216e2012-11-05 16:59:15 -0800350EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700351
352/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700353 * This function frees the adapter structure.
354 *
355 * Additionally, this closes the netlink socket, frees the timers
356 * and private structures.
357 */
358static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
359{
360 if (!adapter) {
361 pr_err("%s: adapter is NULL\n", __func__);
362 return;
363 }
364
365 mwifiex_unregister(adapter);
366 pr_debug("info: %s: free adapter\n", __func__);
367}
368
369/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700370 * This function cancels all works in the queue and destroys
371 * the main workqueue.
372 */
373static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
374{
375 flush_workqueue(adapter->workqueue);
376 destroy_workqueue(adapter->workqueue);
377 adapter->workqueue = NULL;
Avinash Patil6e251172014-09-12 20:08:59 +0530378
379 if (adapter->rx_workqueue) {
380 flush_workqueue(adapter->rx_workqueue);
381 destroy_workqueue(adapter->rx_workqueue);
382 adapter->rx_workqueue = NULL;
383 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700384}
385
386/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700387 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700388 *
389 * The main initialization steps followed are -
390 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700391 * - Issue the init commands to firmware
392 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700393static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700394{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800395 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700396 char fmt[64];
397 struct mwifiex_private *priv;
398 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700399 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700400 struct semaphore *sem = adapter->card_sem;
401 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800402 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700403
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700404 if (!firmware) {
405 dev_err(adapter->dev,
406 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700407 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700408 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700409
410 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
411 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700412 fw.fw_buf = (u8 *) adapter->firmware->data;
413 fw.fw_len = adapter->firmware->size;
414
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700415 if (adapter->if_ops.dnld_fw)
416 ret = adapter->if_ops.dnld_fw(adapter, &fw);
417 else
418 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700419 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700420 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700421
422 dev_notice(adapter->dev, "WLAN FW is active\n");
423
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700424 if (cal_data_cfg) {
425 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
426 adapter->dev)) < 0)
427 dev_err(adapter->dev,
428 "Cal data request_firmware() failed\n");
429 }
430
Daniel Drake232fde02013-07-13 10:57:10 -0400431 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700432 if (adapter->if_ops.enable_int) {
433 if (adapter->if_ops.enable_int(adapter))
434 goto err_dnld_fw;
435 }
Daniel Drake232fde02013-07-13 10:57:10 -0400436
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700437 adapter->init_wait_q_woken = false;
438 ret = mwifiex_init_fw(adapter);
439 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700440 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700441 } else if (!ret) {
442 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
443 goto done;
444 }
445 /* Wait for mwifiex_init to complete */
446 wait_event_interruptible(adapter->init_wait_q,
447 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700448 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700449 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700450
Avinash Patild6bffe82012-05-08 18:30:15 -0700451 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
452 if (mwifiex_register_cfg80211(adapter)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700453 dev_err(adapter->dev, "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800454 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700455 }
456
Avinash Patilbf354432014-10-31 16:08:26 +0530457 if (mwifiex_init_channel_scan_gap(adapter)) {
458 dev_err(adapter->dev, "could not init channel stats table\n");
459 goto err_init_fw;
460 }
461
Avinash Patil0013c7c2014-11-05 19:38:11 +0530462 if (driver_mode) {
463 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
464 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
465 }
466
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700467 rtnl_lock();
468 /* Create station interface by default */
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800469 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
470 NL80211_IFTYPE_STATION, NULL, NULL);
471 if (IS_ERR(wdev)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700472 dev_err(adapter->dev, "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800473 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700474 goto err_add_intf;
475 }
Avinash Patil0013c7c2014-11-05 19:38:11 +0530476
477 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
478 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d",
479 NL80211_IFTYPE_AP, NULL, NULL);
480 if (IS_ERR(wdev)) {
481 dev_err(adapter->dev, "cannot create AP interface\n");
482 rtnl_unlock();
483 goto err_add_intf;
484 }
485 }
486
487 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
488 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d",
489 NL80211_IFTYPE_P2P_CLIENT, NULL,
490 NULL);
491 if (IS_ERR(wdev)) {
492 dev_err(adapter->dev,
493 "cannot create p2p client interface\n");
494 rtnl_unlock();
495 goto err_add_intf;
496 }
497 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700498 rtnl_unlock();
499
500 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
501 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
502 goto done;
503
504err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700505 wiphy_unregister(adapter->wiphy);
506 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700507err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400508 if (adapter->if_ops.disable_int)
509 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700510err_dnld_fw:
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700511 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700512 if (adapter->if_ops.unregister_dev)
513 adapter->if_ops.unregister_dev(adapter);
514
Amitkumar Karwar71973252014-12-31 02:36:38 -0800515 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700516 pr_debug("info: %s: shutdown mwifiex\n", __func__);
517 adapter->init_wait_q_woken = false;
518
519 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
520 wait_event_interruptible(adapter->init_wait_q,
521 adapter->init_wait_q_woken);
522 }
523 adapter->surprise_removed = true;
524 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700525 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700526done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700527 if (adapter->cal_data) {
528 release_firmware(adapter->cal_data);
529 adapter->cal_data = NULL;
530 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700531 if (adapter->firmware) {
532 release_firmware(adapter->firmware);
533 adapter->firmware = NULL;
534 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700535 if (init_failed)
536 mwifiex_free_adapter(adapter);
537 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700538 return;
539}
540
541/*
542 * This function initializes the hardware and gets firmware.
543 */
544static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
545{
546 int ret;
547
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700548 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
549 adapter->dev, GFP_KERNEL, adapter,
550 mwifiex_fw_dpc);
551 if (ret < 0)
552 dev_err(adapter->dev,
553 "request_firmware_nowait() returned error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700554 return ret;
555}
556
557/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700558 * CFG802.11 network device handler for open.
559 *
560 * Starts the data queue.
561 */
562static int
563mwifiex_open(struct net_device *dev)
564{
Johannes Bergea2325b2015-01-19 15:54:36 +0530565 netif_carrier_off(dev);
566
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700567 return 0;
568}
569
570/*
571 * CFG802.11 network device handler for close.
572 */
573static int
574mwifiex_close(struct net_device *dev)
575{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700576 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
577
578 if (priv->scan_request) {
579 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
580 cfg80211_scan_done(priv->scan_request, 1);
581 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700582 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700583 }
584
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700585 return 0;
586}
587
588/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700589 * Add buffer into wmm tx queue and queue work to transmit it.
590 */
591int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
592{
Avinash Patil47411a02012-11-01 18:44:16 -0700593 struct netdev_queue *txq;
594 int index = mwifiex_1d_to_wmm_queue[skb->priority];
595
596 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
597 txq = netdev_get_tx_queue(priv->netdev, index);
598 if (!netif_tx_queue_stopped(txq)) {
599 netif_tx_stop_queue(txq);
600 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
601 }
602 }
603
Stone Piaoe39faa72012-09-25 20:23:32 -0700604 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700605 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700606
Stone Piaoe39faa72012-09-25 20:23:32 -0700607 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
608
609 return 0;
610}
611
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800612struct sk_buff *
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800613mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800614 struct sk_buff *skb, u8 flag, u64 *cookie)
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800615{
616 struct sk_buff *orig_skb = skb;
617 struct mwifiex_txinfo *tx_info, *orig_tx_info;
618
619 skb = skb_clone(skb, GFP_ATOMIC);
620 if (skb) {
621 unsigned long flags;
622 int id;
623
624 spin_lock_irqsave(&priv->ack_status_lock, flags);
625 id = idr_alloc(&priv->ack_status_frames, orig_skb,
626 1, 0xff, GFP_ATOMIC);
627 spin_unlock_irqrestore(&priv->ack_status_lock, flags);
628
629 if (id >= 0) {
630 tx_info = MWIFIEX_SKB_TXCB(skb);
631 tx_info->ack_frame_id = id;
632 tx_info->flags |= flag;
633 orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
634 orig_tx_info->ack_frame_id = id;
635 orig_tx_info->flags |= flag;
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800636
637 if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
638 orig_tx_info->cookie = *cookie;
639
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800640 } else if (skb_shared(skb)) {
641 kfree_skb(orig_skb);
642 } else {
643 kfree_skb(skb);
644 skb = orig_skb;
645 }
646 } else {
647 /* couldn't clone -- lose tx status ... */
648 skb = orig_skb;
649 }
650
651 return skb;
652}
653
Stone Piaoe39faa72012-09-25 20:23:32 -0700654/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700655 * CFG802.11 network device handler for data transmission.
656 */
657static int
658mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
659{
660 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700661 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700662 struct mwifiex_txinfo *tx_info;
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800663 bool multicast;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700664
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800665 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700666 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700667
668 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200669 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700670 priv->stats.tx_dropped++;
671 return 0;
672 }
673 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
674 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200675 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700676 priv->stats.tx_dropped++;
677 return 0;
678 }
679 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
680 dev_dbg(priv->adapter->dev,
681 "data: Tx: insufficient skb headroom %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700682 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700683 /* Insufficient skb headroom - allocate a new skb */
684 new_skb =
685 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
686 if (unlikely(!new_skb)) {
687 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200688 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700689 priv->stats.tx_dropped++;
690 return 0;
691 }
692 kfree_skb(skb);
693 skb = new_skb;
694 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700695 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700696 }
697
698 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700699 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800700 tx_info->bss_num = priv->bss_num;
701 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800702 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700703
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800704 multicast = is_multicast_ether_addr(skb->data);
705
706 if (unlikely(!multicast && skb->sk &&
707 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
708 priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
709 skb = mwifiex_clone_skb_for_tx_status(priv,
710 skb,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800711 MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800712
Avinash Patil47411a02012-11-01 18:44:16 -0700713 /* Record the current time the packet was queued; used to
714 * determine the amount of time the packet was queued in
715 * the driver before it was sent to the firmware.
716 * The delay is then sent along with the packet to the
717 * firmware for aggregate delay calculation for stats and
718 * MSDU lifetime expiry.
719 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100720 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700721
Avinash Patil9927baa2014-11-13 21:54:16 +0530722 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
723 priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
724 !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
725 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
726 mwifiex_tdls_check_tx(priv, skb);
727 }
728
Stone Piaoe39faa72012-09-25 20:23:32 -0700729 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700730
731 return 0;
732}
733
734/*
735 * CFG802.11 network device handler for setting MAC address.
736 */
737static int
738mwifiex_set_mac_address(struct net_device *dev, void *addr)
739{
740 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700741 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700742 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700743
744 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
745
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700746 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800747 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
748 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700749
750 if (!ret)
751 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
752 else
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700753 dev_err(priv->adapter->dev,
754 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700755
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700756 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
757
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700758 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700759}
760
761/*
762 * CFG802.11 network device handler for setting multicast list.
763 */
764static void mwifiex_set_multicast_list(struct net_device *dev)
765{
766 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700767 struct mwifiex_multicast_list mcast_list;
768
769 if (dev->flags & IFF_PROMISC) {
770 mcast_list.mode = MWIFIEX_PROMISC_MODE;
771 } else if (dev->flags & IFF_ALLMULTI ||
772 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
773 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
774 } else {
775 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400776 mcast_list.num_multicast_addr =
777 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700778 }
779 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700780}
781
782/*
783 * CFG802.11 network device handler for transmission timeout.
784 */
785static void
786mwifiex_tx_timeout(struct net_device *dev)
787{
788 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
789
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700790 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800791 priv->tx_timeout_cnt++;
792 dev_err(priv->adapter->dev,
793 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
794 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
795 mwifiex_set_trans_start(dev);
796
797 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
798 priv->adapter->if_ops.card_reset) {
799 dev_err(priv->adapter->dev,
800 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
801 priv->adapter->if_ops.card_reset(priv->adapter);
802 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700803}
804
Xinming Hu11cd07a2014-12-23 19:14:10 +0530805void mwifiex_dump_drv_info(struct mwifiex_adapter *adapter)
806{
807 void *p;
808 char drv_version[64];
809 struct usb_card_rec *cardp;
810 struct sdio_mmc_card *sdio_card;
811 struct mwifiex_private *priv;
812 int i, idx;
813 struct netdev_queue *txq;
814 struct mwifiex_debug_info *debug_info;
815
816 if (adapter->drv_info_dump) {
817 vfree(adapter->drv_info_dump);
818 adapter->drv_info_size = 0;
819 }
820
821 dev_info(adapter->dev, "=== DRIVER INFO DUMP START===\n");
822
823 adapter->drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
824
825 if (!adapter->drv_info_dump)
826 return;
827
828 p = (char *)(adapter->drv_info_dump);
829 p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
830
831 mwifiex_drv_get_driver_version(adapter, drv_version,
832 sizeof(drv_version) - 1);
833 p += sprintf(p, "driver_version = %s\n", drv_version);
834
835 if (adapter->iface_type == MWIFIEX_USB) {
836 cardp = (struct usb_card_rec *)adapter->card;
837 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
838 atomic_read(&cardp->tx_cmd_urb_pending));
839 p += sprintf(p, "tx_data_urb_pending = %d\n",
840 atomic_read(&cardp->tx_data_urb_pending));
841 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
842 atomic_read(&cardp->rx_cmd_urb_pending));
843 p += sprintf(p, "rx_data_urb_pending = %d\n",
844 atomic_read(&cardp->rx_data_urb_pending));
845 }
846
847 p += sprintf(p, "tx_pending = %d\n",
848 atomic_read(&adapter->tx_pending));
849 p += sprintf(p, "rx_pending = %d\n",
850 atomic_read(&adapter->rx_pending));
851
852 if (adapter->iface_type == MWIFIEX_SDIO) {
853 sdio_card = (struct sdio_mmc_card *)adapter->card;
854 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
855 sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
856 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
857 sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
858 }
859
860 for (i = 0; i < adapter->priv_num; i++) {
861 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
862 continue;
863 priv = adapter->priv[i];
864 p += sprintf(p, "\n[interface : \"%s\"]\n",
865 priv->netdev->name);
866 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
867 atomic_read(&priv->wmm_tx_pending[0]));
868 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
869 atomic_read(&priv->wmm_tx_pending[1]));
870 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
871 atomic_read(&priv->wmm_tx_pending[2]));
872 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
873 atomic_read(&priv->wmm_tx_pending[3]));
874 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
875 "Disconnected" : "Connected");
876 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
877 ? "on" : "off"));
878 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
879 txq = netdev_get_tx_queue(priv->netdev, idx);
880 p += sprintf(p, "tx queue %d:%s ", idx,
881 netif_tx_queue_stopped(txq) ?
882 "stopped" : "started");
883 }
884 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
885 priv->netdev->name, priv->num_tx_timeout);
886 }
887
Xinming Hu809c6ea2014-12-23 19:14:11 +0530888 if (adapter->iface_type == MWIFIEX_SDIO) {
889 p += sprintf(p, "\n=== SDIO register DUMP===\n");
890 if (adapter->if_ops.reg_dump)
891 p += adapter->if_ops.reg_dump(adapter, p);
892 }
893
Xinming Hu11cd07a2014-12-23 19:14:10 +0530894 p += sprintf(p, "\n=== MORE DEBUG INFORMATION\n");
895 debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
896 if (debug_info) {
897 for (i = 0; i < adapter->priv_num; i++) {
898 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
899 continue;
900 priv = adapter->priv[i];
901 mwifiex_get_debug_info(priv, debug_info);
902 p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
903 break;
904 }
905 kfree(debug_info);
906 }
907
908 adapter->drv_info_size = p - adapter->drv_info_dump;
909 dev_info(adapter->dev, "=== DRIVER INFO DUMP END===\n");
910}
911EXPORT_SYMBOL_GPL(mwifiex_dump_drv_info);
912
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700913/*
914 * CFG802.11 network device handler for statistics retrieval.
915 */
916static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
917{
918 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
919
920 return &priv->stats;
921}
922
Avinash Patil47411a02012-11-01 18:44:16 -0700923static u16
Jason Wangf663dd92014-01-10 16:18:26 +0800924mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100925 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -0700926{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800927 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -0700928 return mwifiex_1d_to_wmm_queue[skb->priority];
929}
930
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700931/* Network device handlers */
932static const struct net_device_ops mwifiex_netdev_ops = {
933 .ndo_open = mwifiex_open,
934 .ndo_stop = mwifiex_close,
935 .ndo_start_xmit = mwifiex_hard_start_xmit,
936 .ndo_set_mac_address = mwifiex_set_mac_address,
937 .ndo_tx_timeout = mwifiex_tx_timeout,
938 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000939 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -0700940 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700941};
942
943/*
944 * This function initializes the private structure parameters.
945 *
946 * The following wait queues are initialized -
947 * - IOCTL wait queue
948 * - Command wait queue
949 * - Statistics wait queue
950 *
951 * ...and the following default parameters are set -
952 * - Current key index : Set to 0
953 * - Rate index : Set to auto
954 * - Media connected : Set to disconnected
955 * - Adhoc link sensed : Set to false
956 * - Nick name : Set to null
957 * - Number of Tx timeout : Set to 0
958 * - Device address : Set to current address
Xinming Hucbf6e052014-12-23 19:14:07 +0530959 * - Rx histogram statistc : Set to 0
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700960 *
961 * In addition, the CFG80211 work queue is also created.
962 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700963void mwifiex_init_priv_params(struct mwifiex_private *priv,
Avinash Patil047eaaf2015-01-28 15:42:05 +0530964 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700965{
966 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -0700967 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700968 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700969 priv->current_key_index = 0;
970 priv->media_connected = false;
Avinash Patilede98bf2012-05-08 18:30:28 -0700971 memset(priv->mgmt_ie, 0,
972 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700973 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
974 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
975 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
Avinash Patil2ade5662015-01-28 15:54:20 +0530976 priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700977 priv->num_tx_timeout = 0;
Avinash Patil8d05eb22015-01-28 15:42:01 +0530978 ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700979 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
Xinming Hucbf6e052014-12-23 19:14:07 +0530980
981 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
982 GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
983 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
984 if (priv->hist_data)
985 mwifiex_hist_data_reset(priv);
986 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700987}
988
989/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700990 * This function check if command is pending.
991 */
992int is_command_pending(struct mwifiex_adapter *adapter)
993{
994 unsigned long flags;
995 int is_cmd_pend_q_empty;
996
997 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
998 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
999 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1000
1001 return !is_cmd_pend_q_empty;
1002}
1003
1004/*
Avinash Patil6e251172014-09-12 20:08:59 +05301005 * This is the RX work queue function.
1006 *
1007 * It handles the RX operations.
1008 */
1009static void mwifiex_rx_work_queue(struct work_struct *work)
1010{
1011 struct mwifiex_adapter *adapter =
1012 container_of(work, struct mwifiex_adapter, rx_work);
1013
1014 if (adapter->surprise_removed)
1015 return;
1016 mwifiex_process_rx(adapter);
1017}
1018
1019/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001020 * This is the main work queue function.
1021 *
1022 * It handles the main process, which in turn handles the complete
1023 * driver operations.
1024 */
1025static void mwifiex_main_work_queue(struct work_struct *work)
1026{
1027 struct mwifiex_adapter *adapter =
1028 container_of(work, struct mwifiex_adapter, main_work);
1029
1030 if (adapter->surprise_removed)
1031 return;
1032 mwifiex_main_process(adapter);
1033}
1034
1035/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001036 * This function adds the card.
1037 *
1038 * This function follows the following major steps to set up the device -
1039 * - Initialize software. This includes probing the card, registering
1040 * the interface operations table, and allocating/initializing the
1041 * adapter structure
1042 * - Set up the netlink socket
1043 * - Create and start the main work queue
1044 * - Register the device
1045 * - Initialize firmware and hardware
1046 * - Add logical interfaces
1047 */
1048int
1049mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001050 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001051{
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001052 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001053
1054 if (down_interruptible(sem))
1055 goto exit_sem_err;
1056
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001057 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001058 pr_err("%s: software init failed\n", __func__);
1059 goto err_init_sw;
1060 }
1061
Amitkumar Karward930fae2011-10-11 17:41:21 -07001062 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001063 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001064
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001065 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001066 adapter->surprise_removed = false;
1067 init_waitqueue_head(&adapter->init_wait_q);
1068 adapter->is_suspended = false;
1069 adapter->hs_activated = false;
1070 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001071 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001072 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -07001073 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001074
Avinash Patilec4a16b2014-11-05 17:04:27 +05301075 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +05301076 adapter->rx_work_enabled = true;
1077 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
1078 }
1079
Amitkumar Karwar448a71c2013-10-11 18:33:04 -07001080 adapter->workqueue =
1081 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1082 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001083 if (!adapter->workqueue)
1084 goto err_kmalloc;
1085
1086 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Avinash Patil6e251172014-09-12 20:08:59 +05301087
1088 if (adapter->rx_work_enabled) {
1089 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1090 WQ_HIGHPRI |
1091 WQ_MEM_RECLAIM |
1092 WQ_UNBOUND, 1);
1093 if (!adapter->rx_workqueue)
1094 goto err_kmalloc;
1095
1096 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1097 }
1098
Amitkumar Karwar92c25382014-06-19 21:38:52 -07001099 if (adapter->if_ops.iface_work)
1100 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001101
1102 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -04001103 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001104 if (adapter->if_ops.register_dev(adapter)) {
1105 pr_err("%s: failed to register mwifiex device\n", __func__);
1106 goto err_registerdev;
1107 }
1108
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001109 if (mwifiex_init_hw_fw(adapter)) {
1110 pr_err("%s: firmware init failed\n", __func__);
1111 goto err_init_fw;
1112 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001113
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001114 return 0;
1115
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001116err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001117 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001118 if (adapter->if_ops.unregister_dev)
1119 adapter->if_ops.unregister_dev(adapter);
Amitkumar Karwar71973252014-12-31 02:36:38 -08001120 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001121 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1122 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001123
1124 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001125 wait_event_interruptible(adapter->init_wait_q,
1126 adapter->init_wait_q_woken);
1127 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001128err_registerdev:
1129 adapter->surprise_removed = true;
1130 mwifiex_terminate_workqueue(adapter);
1131err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001132 mwifiex_free_adapter(adapter);
1133
1134err_init_sw:
1135 up(sem);
1136
1137exit_sem_err:
1138 return -1;
1139}
1140EXPORT_SYMBOL_GPL(mwifiex_add_card);
1141
1142/*
1143 * This function removes the card.
1144 *
1145 * This function follows the following major steps to remove the device -
1146 * - Stop data traffic
1147 * - Shutdown firmware
1148 * - Remove the logical interfaces
1149 * - Terminate the work queue
1150 * - Unregister the device
1151 * - Free the adapter structure
1152 */
1153int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
1154{
1155 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001156 int i;
1157
1158 if (down_interruptible(sem))
1159 goto exit_sem_err;
1160
1161 if (!adapter)
1162 goto exit_remove;
1163
Daniel Drake232fde02013-07-13 10:57:10 -04001164 /* We can no longer handle interrupts once we start doing the teardown
1165 * below. */
1166 if (adapter->if_ops.disable_int)
1167 adapter->if_ops.disable_int(adapter);
1168
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001169 adapter->surprise_removed = true;
1170
Marc Yang9d2e85e2014-12-31 02:36:39 -08001171 mwifiex_terminate_workqueue(adapter);
1172
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001173 /* Stop data */
1174 for (i = 0; i < adapter->priv_num; i++) {
1175 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001176 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -07001177 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001178 if (netif_carrier_ok(priv->netdev))
1179 netif_carrier_off(priv->netdev);
1180 }
1181 }
1182
1183 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
1184 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001185
1186 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001187 wait_event_interruptible(adapter->init_wait_q,
1188 adapter->init_wait_q_woken);
1189 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
1190 if (atomic_read(&adapter->rx_pending) ||
1191 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001192 atomic_read(&adapter->cmd_pending)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001193 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001194 "cmd_pending=%d\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001195 atomic_read(&adapter->rx_pending),
1196 atomic_read(&adapter->tx_pending),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001197 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001198 }
1199
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001200 for (i = 0; i < adapter->priv_num; i++) {
1201 priv = adapter->priv[i];
1202
1203 if (!priv)
1204 continue;
1205
1206 rtnl_lock();
Avinash Patil4facc342015-01-28 15:42:00 +05301207 if (priv->netdev &&
1208 priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1209 mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001210 rtnl_unlock();
1211 }
1212
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -08001213 wiphy_unregister(adapter->wiphy);
1214 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -07001215
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001216 /* Unregister device */
1217 dev_dbg(adapter->dev, "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001218 if (adapter->if_ops.unregister_dev)
1219 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001220 /* Free adapter structure */
1221 dev_dbg(adapter->dev, "info: free adapter\n");
1222 mwifiex_free_adapter(adapter);
1223
1224exit_remove:
1225 up(sem);
1226exit_sem_err:
1227 return 0;
1228}
1229EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1230
1231/*
1232 * This function initializes the module.
1233 *
1234 * The debug FS is also initialized if configured.
1235 */
1236static int
1237mwifiex_init_module(void)
1238{
1239#ifdef CONFIG_DEBUG_FS
1240 mwifiex_debugfs_init();
1241#endif
1242 return 0;
1243}
1244
1245/*
1246 * This function cleans up the module.
1247 *
1248 * The debug FS is removed if available.
1249 */
1250static void
1251mwifiex_cleanup_module(void)
1252{
1253#ifdef CONFIG_DEBUG_FS
1254 mwifiex_debugfs_remove();
1255#endif
1256}
1257
1258module_init(mwifiex_init_module);
1259module_exit(mwifiex_cleanup_module);
1260
1261MODULE_AUTHOR("Marvell International Ltd.");
1262MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1263MODULE_VERSION(VERSION);
1264MODULE_LICENSE("GPL v2");