blob: 42bf8846771b184d48375489290e004651d2552e [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) {
Shengzhen Li04c7b362015-02-11 23:12:24 +0530193 adapter->more_task_flag = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700194 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
195 goto exit_main_proc;
196 } else {
197 adapter->mwifiex_processing = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700198 }
199process_start:
200 do {
Shengzhen Li04c7b362015-02-11 23:12:24 +0530201 adapter->more_task_flag = false;
202 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700203 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
204 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
205 break;
206
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800207 /* For non-USB interfaces, If we process interrupts first, it
208 * would increase RX pending even further. Avoid this by
209 * checking if rx_pending has crossed high threshold and
210 * schedule rx work queue and then process interrupts.
211 * For USB interface, there are no interrupts. We already have
212 * HIGH_RX_PENDING check in usb.c
Avinash Patil6e251172014-09-12 20:08:59 +0530213 */
Amitkumar Karwar381e9ff2014-11-25 06:43:04 -0800214 if (atomic_read(&adapter->rx_pending) >= HIGH_RX_PENDING &&
215 adapter->iface_type != MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +0530216 adapter->delay_main_work = true;
217 if (!adapter->rx_processing)
218 queue_work(adapter->rx_workqueue,
219 &adapter->rx_work);
220 break;
221 }
222
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700223 /* Handle pending interrupt if any */
224 if (adapter->int_status) {
225 if (adapter->hs_activated)
226 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700227 if (adapter->if_ops.process_int_status)
228 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700229 }
230
Avinash Patil6e251172014-09-12 20:08:59 +0530231 if (adapter->rx_work_enabled && adapter->data_received)
232 queue_work(adapter->rx_workqueue, &adapter->rx_work);
233
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700234 /* Need to wake up the card ? */
235 if ((adapter->ps_state == PS_STATE_SLEEP) &&
236 (adapter->pm_wakeup_card_req &&
237 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700238 (is_command_pending(adapter) ||
239 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700240 adapter->pm_wakeup_fw_try = true;
Amitkumar Karwar46361872014-12-31 02:36:41 -0800241 mod_timer(&adapter->wakeup_timer, jiffies + (HZ*3));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700242 adapter->if_ops.wakeup(adapter);
Shengzhen Li04c7b362015-02-11 23:12:24 +0530243 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700244 continue;
245 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700246
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700247 if (IS_CARD_RX_RCVD(adapter)) {
Avinash Patil6e251172014-09-12 20:08:59 +0530248 adapter->data_received = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700249 adapter->pm_wakeup_fw_try = false;
Amitkumar Karwar6e9344f2015-03-12 00:38:40 -0700250 del_timer(&adapter->wakeup_timer);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700251 if (adapter->ps_state == PS_STATE_SLEEP)
252 adapter->ps_state = PS_STATE_AWAKE;
253 } else {
254 /* We have tried to wakeup the card already */
255 if (adapter->pm_wakeup_fw_try)
256 break;
257 if (adapter->ps_state != PS_STATE_AWAKE ||
258 adapter->tx_lock_flag)
259 break;
260
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530261 if ((!adapter->scan_chan_gap_enabled &&
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530262 adapter->scan_processing) || adapter->data_sent ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700263 mwifiex_wmm_lists_empty(adapter)) {
264 if (adapter->cmd_sent || adapter->curr_cmd ||
265 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700266 break;
267 }
268 }
269
Amitkumar Karwar20474122014-04-14 15:31:05 -0700270 /* Check for event */
271 if (adapter->event_received) {
272 adapter->event_received = false;
273 mwifiex_process_event(adapter);
274 }
275
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700276 /* Check for Cmd Resp */
277 if (adapter->cmd_resp_received) {
278 adapter->cmd_resp_received = false;
279 mwifiex_process_cmdresp(adapter);
280
281 /* call mwifiex back when init_fw is done */
282 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
283 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
284 mwifiex_init_fw_complete(adapter);
285 }
286 }
287
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700288 /* Check if we need to confirm Sleep Request
289 received previously */
290 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
291 if (!adapter->cmd_sent && !adapter->curr_cmd)
292 mwifiex_check_ps_cond(adapter);
293 }
294
295 /* * The ps_state may have been changed during processing of
296 * Sleep Request event.
297 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700298 if ((adapter->ps_state == PS_STATE_SLEEP) ||
299 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
300 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
Shengzhen Li04c7b362015-02-11 23:12:24 +0530301 adapter->tx_lock_flag){
302 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700303 continue;
Shengzhen Li04c7b362015-02-11 23:12:24 +0530304 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700305
306 if (!adapter->cmd_sent && !adapter->curr_cmd) {
307 if (mwifiex_exec_next_cmd(adapter) == -1) {
308 ret = -1;
309 break;
310 }
311 }
312
Avinash Patil5ec39ef2014-09-12 20:08:56 +0530313 if ((adapter->scan_chan_gap_enabled ||
Amitkumar Karward8d91252014-09-12 20:08:58 +0530314 !adapter->scan_processing) &&
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700315 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700316 mwifiex_wmm_process_tx(adapter);
317 if (adapter->hs_activated) {
318 adapter->is_hs_configured = false;
319 mwifiex_hs_activated_event
320 (mwifiex_get_priv
321 (adapter, MWIFIEX_BSS_ROLE_ANY),
322 false);
323 }
324 }
325
326 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700327 !adapter->curr_cmd && !is_command_pending(adapter) &&
328 mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700329 if (!mwifiex_send_null_packet
330 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
331 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
332 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
333 adapter->delay_null_pkt = false;
334 adapter->ps_state = PS_STATE_SLEEP;
335 }
336 break;
337 }
Shengzhen Li04c7b362015-02-11 23:12:24 +0530338 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700339 } while (true);
340
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700341 spin_lock_irqsave(&adapter->main_proc_lock, flags);
Shengzhen Li04c7b362015-02-11 23:12:24 +0530342 if (adapter->more_task_flag)
Amitkumar Karwar453b0c32013-09-27 10:55:38 -0700343 goto process_start;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700344 adapter->mwifiex_processing = false;
345 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
346
347exit_main_proc:
348 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
349 mwifiex_shutdown_drv(adapter);
350 return ret;
351}
Bing Zhao601216e2012-11-05 16:59:15 -0800352EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700353
354/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700355 * This function frees the adapter structure.
356 *
357 * Additionally, this closes the netlink socket, frees the timers
358 * and private structures.
359 */
360static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
361{
362 if (!adapter) {
363 pr_err("%s: adapter is NULL\n", __func__);
364 return;
365 }
366
367 mwifiex_unregister(adapter);
368 pr_debug("info: %s: free adapter\n", __func__);
369}
370
371/*
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700372 * This function cancels all works in the queue and destroys
373 * the main workqueue.
374 */
375static void mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
376{
377 flush_workqueue(adapter->workqueue);
378 destroy_workqueue(adapter->workqueue);
379 adapter->workqueue = NULL;
Avinash Patil6e251172014-09-12 20:08:59 +0530380
381 if (adapter->rx_workqueue) {
382 flush_workqueue(adapter->rx_workqueue);
383 destroy_workqueue(adapter->rx_workqueue);
384 adapter->rx_workqueue = NULL;
385 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700386}
387
388/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700389 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700390 *
391 * The main initialization steps followed are -
392 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700393 * - Issue the init commands to firmware
394 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700395static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700396{
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800397 int ret;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700398 char fmt[64];
399 struct mwifiex_private *priv;
400 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700401 struct mwifiex_fw_image fw;
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700402 struct semaphore *sem = adapter->card_sem;
403 bool init_failed = false;
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800404 struct wireless_dev *wdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700405
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700406 if (!firmware) {
407 dev_err(adapter->dev,
408 "Failed to get firmware %s\n", adapter->fw_name);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700409 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700410 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700411
412 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
413 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700414 fw.fw_buf = (u8 *) adapter->firmware->data;
415 fw.fw_len = adapter->firmware->size;
416
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700417 if (adapter->if_ops.dnld_fw)
418 ret = adapter->if_ops.dnld_fw(adapter, &fw);
419 else
420 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700421 if (ret == -1)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700422 goto err_dnld_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700423
424 dev_notice(adapter->dev, "WLAN FW is active\n");
425
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700426 if (cal_data_cfg) {
427 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
428 adapter->dev)) < 0)
429 dev_err(adapter->dev,
430 "Cal data request_firmware() failed\n");
431 }
432
Daniel Drake232fde02013-07-13 10:57:10 -0400433 /* enable host interrupt after fw dnld is successful */
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700434 if (adapter->if_ops.enable_int) {
435 if (adapter->if_ops.enable_int(adapter))
436 goto err_dnld_fw;
437 }
Daniel Drake232fde02013-07-13 10:57:10 -0400438
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700439 adapter->init_wait_q_woken = false;
440 ret = mwifiex_init_fw(adapter);
441 if (ret == -1) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700442 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700443 } else if (!ret) {
444 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
445 goto done;
446 }
447 /* Wait for mwifiex_init to complete */
448 wait_event_interruptible(adapter->init_wait_q,
449 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700450 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700451 goto err_init_fw;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700452
Avinash Patild6bffe82012-05-08 18:30:15 -0700453 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
454 if (mwifiex_register_cfg80211(adapter)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700455 dev_err(adapter->dev, "cannot register with cfg80211\n");
Amitkumar Karward1af2942013-11-14 19:10:39 -0800456 goto err_init_fw;
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700457 }
458
Avinash Patilbf354432014-10-31 16:08:26 +0530459 if (mwifiex_init_channel_scan_gap(adapter)) {
460 dev_err(adapter->dev, "could not init channel stats table\n");
461 goto err_init_fw;
462 }
463
Avinash Patil0013c7c2014-11-05 19:38:11 +0530464 if (driver_mode) {
465 driver_mode &= MWIFIEX_DRIVER_MODE_BITMASK;
466 driver_mode |= MWIFIEX_DRIVER_MODE_STA;
467 }
468
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700469 rtnl_lock();
470 /* Create station interface by default */
Amitkumar Karwar68b76e92013-11-14 19:10:37 -0800471 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
472 NL80211_IFTYPE_STATION, NULL, NULL);
473 if (IS_ERR(wdev)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700474 dev_err(adapter->dev, "cannot create default STA interface\n");
Amitkumar Karwarbec568f2013-11-14 19:10:38 -0800475 rtnl_unlock();
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700476 goto err_add_intf;
477 }
Avinash Patil0013c7c2014-11-05 19:38:11 +0530478
479 if (driver_mode & MWIFIEX_DRIVER_MODE_UAP) {
480 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "uap%d",
481 NL80211_IFTYPE_AP, NULL, NULL);
482 if (IS_ERR(wdev)) {
483 dev_err(adapter->dev, "cannot create AP interface\n");
484 rtnl_unlock();
485 goto err_add_intf;
486 }
487 }
488
489 if (driver_mode & MWIFIEX_DRIVER_MODE_P2P) {
490 wdev = mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d",
491 NL80211_IFTYPE_P2P_CLIENT, NULL,
492 NULL);
493 if (IS_ERR(wdev)) {
494 dev_err(adapter->dev,
495 "cannot create p2p client interface\n");
496 rtnl_unlock();
497 goto err_add_intf;
498 }
499 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700500 rtnl_unlock();
501
502 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
503 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
504 goto done;
505
506err_add_intf:
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700507 wiphy_unregister(adapter->wiphy);
508 wiphy_free(adapter->wiphy);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700509err_init_fw:
Daniel Drake232fde02013-07-13 10:57:10 -0400510 if (adapter->if_ops.disable_int)
511 adapter->if_ops.disable_int(adapter);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700512err_dnld_fw:
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700513 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700514 if (adapter->if_ops.unregister_dev)
515 adapter->if_ops.unregister_dev(adapter);
516
Amitkumar Karwar71973252014-12-31 02:36:38 -0800517 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
Amitkumar Karwar6b41f942013-07-22 19:17:55 -0700518 pr_debug("info: %s: shutdown mwifiex\n", __func__);
519 adapter->init_wait_q_woken = false;
520
521 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
522 wait_event_interruptible(adapter->init_wait_q,
523 adapter->init_wait_q_woken);
524 }
525 adapter->surprise_removed = true;
526 mwifiex_terminate_workqueue(adapter);
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700527 init_failed = true;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700528done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700529 if (adapter->cal_data) {
530 release_firmware(adapter->cal_data);
531 adapter->cal_data = NULL;
532 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700533 if (adapter->firmware) {
534 release_firmware(adapter->firmware);
535 adapter->firmware = NULL;
536 }
Amitkumar Karwarc3afd992013-07-30 17:18:15 -0700537 if (init_failed)
538 mwifiex_free_adapter(adapter);
539 up(sem);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700540 return;
541}
542
543/*
544 * This function initializes the hardware and gets firmware.
545 */
546static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
547{
548 int ret;
549
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700550 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
551 adapter->dev, GFP_KERNEL, adapter,
552 mwifiex_fw_dpc);
553 if (ret < 0)
554 dev_err(adapter->dev,
555 "request_firmware_nowait() returned error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700556 return ret;
557}
558
559/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700560 * CFG802.11 network device handler for open.
561 *
562 * Starts the data queue.
563 */
564static int
565mwifiex_open(struct net_device *dev)
566{
Johannes Bergea2325b2015-01-19 15:54:36 +0530567 netif_carrier_off(dev);
568
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700569 return 0;
570}
571
572/*
573 * CFG802.11 network device handler for close.
574 */
575static int
576mwifiex_close(struct net_device *dev)
577{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700578 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
579
580 if (priv->scan_request) {
581 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
582 cfg80211_scan_done(priv->scan_request, 1);
583 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700584 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700585 }
586
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700587 return 0;
588}
589
590/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700591 * Add buffer into wmm tx queue and queue work to transmit it.
592 */
593int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
594{
Avinash Patil47411a02012-11-01 18:44:16 -0700595 struct netdev_queue *txq;
596 int index = mwifiex_1d_to_wmm_queue[skb->priority];
597
598 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
599 txq = netdev_get_tx_queue(priv->netdev, index);
600 if (!netif_tx_queue_stopped(txq)) {
601 netif_tx_stop_queue(txq);
602 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
603 }
604 }
605
Stone Piaoe39faa72012-09-25 20:23:32 -0700606 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700607 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700608
Stone Piaoe39faa72012-09-25 20:23:32 -0700609 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
610
611 return 0;
612}
613
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800614struct sk_buff *
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800615mwifiex_clone_skb_for_tx_status(struct mwifiex_private *priv,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800616 struct sk_buff *skb, u8 flag, u64 *cookie)
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800617{
618 struct sk_buff *orig_skb = skb;
619 struct mwifiex_txinfo *tx_info, *orig_tx_info;
620
621 skb = skb_clone(skb, GFP_ATOMIC);
622 if (skb) {
623 unsigned long flags;
624 int id;
625
626 spin_lock_irqsave(&priv->ack_status_lock, flags);
627 id = idr_alloc(&priv->ack_status_frames, orig_skb,
628 1, 0xff, GFP_ATOMIC);
629 spin_unlock_irqrestore(&priv->ack_status_lock, flags);
630
631 if (id >= 0) {
632 tx_info = MWIFIEX_SKB_TXCB(skb);
633 tx_info->ack_frame_id = id;
634 tx_info->flags |= flag;
635 orig_tx_info = MWIFIEX_SKB_TXCB(orig_skb);
636 orig_tx_info->ack_frame_id = id;
637 orig_tx_info->flags |= flag;
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800638
639 if (flag == MWIFIEX_BUF_FLAG_ACTION_TX_STATUS && cookie)
640 orig_tx_info->cookie = *cookie;
641
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800642 } else if (skb_shared(skb)) {
643 kfree_skb(orig_skb);
644 } else {
645 kfree_skb(skb);
646 skb = orig_skb;
647 }
648 } else {
649 /* couldn't clone -- lose tx status ... */
650 skb = orig_skb;
651 }
652
653 return skb;
654}
655
Stone Piaoe39faa72012-09-25 20:23:32 -0700656/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700657 * CFG802.11 network device handler for data transmission.
658 */
659static int
660mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
661{
662 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700663 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700664 struct mwifiex_txinfo *tx_info;
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800665 bool multicast;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700666
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800667 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700668 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700669
670 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200671 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700672 priv->stats.tx_dropped++;
673 return 0;
674 }
675 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
676 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200677 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700678 priv->stats.tx_dropped++;
679 return 0;
680 }
681 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
682 dev_dbg(priv->adapter->dev,
683 "data: Tx: insufficient skb headroom %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700684 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700685 /* Insufficient skb headroom - allocate a new skb */
686 new_skb =
687 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
688 if (unlikely(!new_skb)) {
689 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200690 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700691 priv->stats.tx_dropped++;
692 return 0;
693 }
694 kfree_skb(skb);
695 skb = new_skb;
696 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700697 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700698 }
699
700 tx_info = MWIFIEX_SKB_TXCB(skb);
Amitkumar Karward76744a2014-06-20 11:45:25 -0700701 memset(tx_info, 0, sizeof(*tx_info));
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800702 tx_info->bss_num = priv->bss_num;
703 tx_info->bss_type = priv->bss_type;
Ujjal Roya1ed4842013-12-02 23:17:55 -0800704 tx_info->pkt_len = skb->len;
Avinash Patil47411a02012-11-01 18:44:16 -0700705
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800706 multicast = is_multicast_ether_addr(skb->data);
707
708 if (unlikely(!multicast && skb->sk &&
709 skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
710 priv->adapter->fw_api_ver == MWIFIEX_FW_V15))
711 skb = mwifiex_clone_skb_for_tx_status(priv,
712 skb,
Amitkumar Karwar18ca4382014-11-25 06:43:06 -0800713 MWIFIEX_BUF_FLAG_EAPOL_TX_STATUS, NULL);
Amitkumar Karwar808bbeb2014-11-25 06:43:05 -0800714
Avinash Patil47411a02012-11-01 18:44:16 -0700715 /* Record the current time the packet was queued; used to
716 * determine the amount of time the packet was queued in
717 * the driver before it was sent to the firmware.
718 * The delay is then sent along with the packet to the
719 * firmware for aggregate delay calculation for stats and
720 * MSDU lifetime expiry.
721 */
Thomas Gleixnerc64800e2014-06-12 08:31:34 +0100722 __net_timestamp(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700723
Avinash Patil9927baa2014-11-13 21:54:16 +0530724 if (ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info) &&
725 priv->bss_type == MWIFIEX_BSS_TYPE_STA &&
726 !ether_addr_equal_unaligned(priv->cfg_bssid, skb->data)) {
727 if (priv->adapter->auto_tdls && priv->check_tdls_tx)
728 mwifiex_tdls_check_tx(priv, skb);
729 }
730
Stone Piaoe39faa72012-09-25 20:23:32 -0700731 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700732
733 return 0;
734}
735
736/*
737 * CFG802.11 network device handler for setting MAC address.
738 */
739static int
740mwifiex_set_mac_address(struct net_device *dev, void *addr)
741{
742 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700743 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700744 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700745
746 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
747
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700748 /* Send request to firmware */
Bing Zhaofa0ecbb2014-02-27 19:35:12 -0800749 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
750 HostCmd_ACT_GEN_SET, 0, NULL, true);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700751
752 if (!ret)
753 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
754 else
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700755 dev_err(priv->adapter->dev,
756 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700757
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700758 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
759
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700760 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700761}
762
763/*
764 * CFG802.11 network device handler for setting multicast list.
765 */
766static void mwifiex_set_multicast_list(struct net_device *dev)
767{
768 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700769 struct mwifiex_multicast_list mcast_list;
770
771 if (dev->flags & IFF_PROMISC) {
772 mcast_list.mode = MWIFIEX_PROMISC_MODE;
773 } else if (dev->flags & IFF_ALLMULTI ||
774 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
775 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
776 } else {
777 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
Daniel Drake6390d882013-06-14 15:24:24 -0400778 mcast_list.num_multicast_addr =
779 mwifiex_copy_mcast_addr(&mcast_list, dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700780 }
781 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700782}
783
784/*
785 * CFG802.11 network device handler for transmission timeout.
786 */
787static void
788mwifiex_tx_timeout(struct net_device *dev)
789{
790 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
791
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700792 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800793 priv->tx_timeout_cnt++;
794 dev_err(priv->adapter->dev,
795 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
796 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
797 mwifiex_set_trans_start(dev);
798
799 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
800 priv->adapter->if_ops.card_reset) {
801 dev_err(priv->adapter->dev,
802 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
803 priv->adapter->if_ops.card_reset(priv->adapter);
804 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700805}
806
Xinming Hu11cd07a2014-12-23 19:14:10 +0530807void mwifiex_dump_drv_info(struct mwifiex_adapter *adapter)
808{
809 void *p;
810 char drv_version[64];
811 struct usb_card_rec *cardp;
812 struct sdio_mmc_card *sdio_card;
813 struct mwifiex_private *priv;
814 int i, idx;
815 struct netdev_queue *txq;
816 struct mwifiex_debug_info *debug_info;
817
818 if (adapter->drv_info_dump) {
819 vfree(adapter->drv_info_dump);
820 adapter->drv_info_size = 0;
821 }
822
823 dev_info(adapter->dev, "=== DRIVER INFO DUMP START===\n");
824
825 adapter->drv_info_dump = vzalloc(MWIFIEX_DRV_INFO_SIZE_MAX);
826
827 if (!adapter->drv_info_dump)
828 return;
829
830 p = (char *)(adapter->drv_info_dump);
831 p += sprintf(p, "driver_name = " "\"mwifiex\"\n");
832
833 mwifiex_drv_get_driver_version(adapter, drv_version,
834 sizeof(drv_version) - 1);
835 p += sprintf(p, "driver_version = %s\n", drv_version);
836
837 if (adapter->iface_type == MWIFIEX_USB) {
838 cardp = (struct usb_card_rec *)adapter->card;
839 p += sprintf(p, "tx_cmd_urb_pending = %d\n",
840 atomic_read(&cardp->tx_cmd_urb_pending));
841 p += sprintf(p, "tx_data_urb_pending = %d\n",
842 atomic_read(&cardp->tx_data_urb_pending));
843 p += sprintf(p, "rx_cmd_urb_pending = %d\n",
844 atomic_read(&cardp->rx_cmd_urb_pending));
845 p += sprintf(p, "rx_data_urb_pending = %d\n",
846 atomic_read(&cardp->rx_data_urb_pending));
847 }
848
849 p += sprintf(p, "tx_pending = %d\n",
850 atomic_read(&adapter->tx_pending));
851 p += sprintf(p, "rx_pending = %d\n",
852 atomic_read(&adapter->rx_pending));
853
854 if (adapter->iface_type == MWIFIEX_SDIO) {
855 sdio_card = (struct sdio_mmc_card *)adapter->card;
856 p += sprintf(p, "\nmp_rd_bitmap=0x%x curr_rd_port=0x%x\n",
857 sdio_card->mp_rd_bitmap, sdio_card->curr_rd_port);
858 p += sprintf(p, "mp_wr_bitmap=0x%x curr_wr_port=0x%x\n",
859 sdio_card->mp_wr_bitmap, sdio_card->curr_wr_port);
860 }
861
862 for (i = 0; i < adapter->priv_num; i++) {
863 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
864 continue;
865 priv = adapter->priv[i];
866 p += sprintf(p, "\n[interface : \"%s\"]\n",
867 priv->netdev->name);
868 p += sprintf(p, "wmm_tx_pending[0] = %d\n",
869 atomic_read(&priv->wmm_tx_pending[0]));
870 p += sprintf(p, "wmm_tx_pending[1] = %d\n",
871 atomic_read(&priv->wmm_tx_pending[1]));
872 p += sprintf(p, "wmm_tx_pending[2] = %d\n",
873 atomic_read(&priv->wmm_tx_pending[2]));
874 p += sprintf(p, "wmm_tx_pending[3] = %d\n",
875 atomic_read(&priv->wmm_tx_pending[3]));
876 p += sprintf(p, "media_state=\"%s\"\n", !priv->media_connected ?
877 "Disconnected" : "Connected");
878 p += sprintf(p, "carrier %s\n", (netif_carrier_ok(priv->netdev)
879 ? "on" : "off"));
880 for (idx = 0; idx < priv->netdev->num_tx_queues; idx++) {
881 txq = netdev_get_tx_queue(priv->netdev, idx);
882 p += sprintf(p, "tx queue %d:%s ", idx,
883 netif_tx_queue_stopped(txq) ?
884 "stopped" : "started");
885 }
886 p += sprintf(p, "\n%s: num_tx_timeout = %d\n",
887 priv->netdev->name, priv->num_tx_timeout);
888 }
889
Xinming Hu809c6ea2014-12-23 19:14:11 +0530890 if (adapter->iface_type == MWIFIEX_SDIO) {
891 p += sprintf(p, "\n=== SDIO register DUMP===\n");
892 if (adapter->if_ops.reg_dump)
893 p += adapter->if_ops.reg_dump(adapter, p);
894 }
895
Xinming Hu11cd07a2014-12-23 19:14:10 +0530896 p += sprintf(p, "\n=== MORE DEBUG INFORMATION\n");
897 debug_info = kzalloc(sizeof(*debug_info), GFP_KERNEL);
898 if (debug_info) {
899 for (i = 0; i < adapter->priv_num; i++) {
900 if (!adapter->priv[i] || !adapter->priv[i]->netdev)
901 continue;
902 priv = adapter->priv[i];
903 mwifiex_get_debug_info(priv, debug_info);
904 p += mwifiex_debug_info_to_buffer(priv, p, debug_info);
905 break;
906 }
907 kfree(debug_info);
908 }
909
910 adapter->drv_info_size = p - adapter->drv_info_dump;
911 dev_info(adapter->dev, "=== DRIVER INFO DUMP END===\n");
912}
913EXPORT_SYMBOL_GPL(mwifiex_dump_drv_info);
914
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700915/*
916 * CFG802.11 network device handler for statistics retrieval.
917 */
918static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
919{
920 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
921
922 return &priv->stats;
923}
924
Avinash Patil47411a02012-11-01 18:44:16 -0700925static u16
Jason Wangf663dd92014-01-10 16:18:26 +0800926mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb,
Daniel Borkmann99932d42014-02-16 15:55:20 +0100927 void *accel_priv, select_queue_fallback_t fallback)
Avinash Patil47411a02012-11-01 18:44:16 -0700928{
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800929 skb->priority = cfg80211_classify8021d(skb, NULL);
Avinash Patil47411a02012-11-01 18:44:16 -0700930 return mwifiex_1d_to_wmm_queue[skb->priority];
931}
932
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700933/* Network device handlers */
934static const struct net_device_ops mwifiex_netdev_ops = {
935 .ndo_open = mwifiex_open,
936 .ndo_stop = mwifiex_close,
937 .ndo_start_xmit = mwifiex_hard_start_xmit,
938 .ndo_set_mac_address = mwifiex_set_mac_address,
939 .ndo_tx_timeout = mwifiex_tx_timeout,
940 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000941 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -0700942 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700943};
944
945/*
946 * This function initializes the private structure parameters.
947 *
948 * The following wait queues are initialized -
949 * - IOCTL wait queue
950 * - Command wait queue
951 * - Statistics wait queue
952 *
953 * ...and the following default parameters are set -
954 * - Current key index : Set to 0
955 * - Rate index : Set to auto
956 * - Media connected : Set to disconnected
957 * - Adhoc link sensed : Set to false
958 * - Nick name : Set to null
959 * - Number of Tx timeout : Set to 0
960 * - Device address : Set to current address
Xinming Hucbf6e052014-12-23 19:14:07 +0530961 * - Rx histogram statistc : Set to 0
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700962 *
963 * In addition, the CFG80211 work queue is also created.
964 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700965void mwifiex_init_priv_params(struct mwifiex_private *priv,
Avinash Patil047eaaf2015-01-28 15:42:05 +0530966 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700967{
968 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -0700969 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700970 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700971 priv->current_key_index = 0;
972 priv->media_connected = false;
Avinash Patilede98bf2012-05-08 18:30:28 -0700973 memset(priv->mgmt_ie, 0,
974 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700975 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
976 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
977 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
Avinash Patil2ade5662015-01-28 15:54:20 +0530978 priv->gen_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700979 priv->num_tx_timeout = 0;
Avinash Patil8d05eb22015-01-28 15:42:01 +0530980 ether_addr_copy(priv->curr_addr, priv->adapter->perm_addr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700981 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
Xinming Hucbf6e052014-12-23 19:14:07 +0530982
983 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
984 GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
985 priv->hist_data = kmalloc(sizeof(*priv->hist_data), GFP_KERNEL);
986 if (priv->hist_data)
987 mwifiex_hist_data_reset(priv);
988 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700989}
990
991/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700992 * This function check if command is pending.
993 */
994int is_command_pending(struct mwifiex_adapter *adapter)
995{
996 unsigned long flags;
997 int is_cmd_pend_q_empty;
998
999 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
1000 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
1001 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
1002
1003 return !is_cmd_pend_q_empty;
1004}
1005
1006/*
Avinash Patil6e251172014-09-12 20:08:59 +05301007 * This is the RX work queue function.
1008 *
1009 * It handles the RX operations.
1010 */
1011static void mwifiex_rx_work_queue(struct work_struct *work)
1012{
1013 struct mwifiex_adapter *adapter =
1014 container_of(work, struct mwifiex_adapter, rx_work);
1015
1016 if (adapter->surprise_removed)
1017 return;
1018 mwifiex_process_rx(adapter);
1019}
1020
1021/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001022 * This is the main work queue function.
1023 *
1024 * It handles the main process, which in turn handles the complete
1025 * driver operations.
1026 */
1027static void mwifiex_main_work_queue(struct work_struct *work)
1028{
1029 struct mwifiex_adapter *adapter =
1030 container_of(work, struct mwifiex_adapter, main_work);
1031
1032 if (adapter->surprise_removed)
1033 return;
1034 mwifiex_main_process(adapter);
1035}
1036
1037/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001038 * This function adds the card.
1039 *
1040 * This function follows the following major steps to set up the device -
1041 * - Initialize software. This includes probing the card, registering
1042 * the interface operations table, and allocating/initializing the
1043 * adapter structure
1044 * - Set up the netlink socket
1045 * - Create and start the main work queue
1046 * - Register the device
1047 * - Initialize firmware and hardware
1048 * - Add logical interfaces
1049 */
1050int
1051mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001052 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001053{
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001054 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001055
1056 if (down_interruptible(sem))
1057 goto exit_sem_err;
1058
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001059 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001060 pr_err("%s: software init failed\n", __func__);
1061 goto err_init_sw;
1062 }
1063
Amitkumar Karward930fae2011-10-11 17:41:21 -07001064 adapter->iface_type = iface_type;
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001065 adapter->card_sem = sem;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001066
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001067 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001068 adapter->surprise_removed = false;
1069 init_waitqueue_head(&adapter->init_wait_q);
1070 adapter->is_suspended = false;
1071 adapter->hs_activated = false;
1072 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001073 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001074 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -07001075 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001076
Avinash Patilec4a16b2014-11-05 17:04:27 +05301077 if ((num_possible_cpus() > 1) || adapter->iface_type == MWIFIEX_USB) {
Avinash Patil6e251172014-09-12 20:08:59 +05301078 adapter->rx_work_enabled = true;
1079 pr_notice("rx work enabled, cpus %d\n", num_possible_cpus());
1080 }
1081
Amitkumar Karwar448a71c2013-10-11 18:33:04 -07001082 adapter->workqueue =
1083 alloc_workqueue("MWIFIEX_WORK_QUEUE",
1084 WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001085 if (!adapter->workqueue)
1086 goto err_kmalloc;
1087
1088 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
Avinash Patil6e251172014-09-12 20:08:59 +05301089
1090 if (adapter->rx_work_enabled) {
1091 adapter->rx_workqueue = alloc_workqueue("MWIFIEX_RX_WORK_QUEUE",
1092 WQ_HIGHPRI |
1093 WQ_MEM_RECLAIM |
1094 WQ_UNBOUND, 1);
1095 if (!adapter->rx_workqueue)
1096 goto err_kmalloc;
1097
1098 INIT_WORK(&adapter->rx_work, mwifiex_rx_work_queue);
1099 }
1100
Amitkumar Karwar92c25382014-06-19 21:38:52 -07001101 if (adapter->if_ops.iface_work)
1102 INIT_WORK(&adapter->iface_work, adapter->if_ops.iface_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001103
1104 /* Register the device. Fill up the private data structure with relevant
Daniel Drake232fde02013-07-13 10:57:10 -04001105 information from the card. */
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001106 if (adapter->if_ops.register_dev(adapter)) {
1107 pr_err("%s: failed to register mwifiex device\n", __func__);
1108 goto err_registerdev;
1109 }
1110
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001111 if (mwifiex_init_hw_fw(adapter)) {
1112 pr_err("%s: firmware init failed\n", __func__);
1113 goto err_init_fw;
1114 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -07001115
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001116 return 0;
1117
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001118err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001119 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001120 if (adapter->if_ops.unregister_dev)
1121 adapter->if_ops.unregister_dev(adapter);
Amitkumar Karwar71973252014-12-31 02:36:38 -08001122 if (adapter->hw_status == MWIFIEX_HW_STATUS_READY) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001123 pr_debug("info: %s: shutdown mwifiex\n", __func__);
1124 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001125
1126 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001127 wait_event_interruptible(adapter->init_wait_q,
1128 adapter->init_wait_q_woken);
1129 }
Amitkumar Karwar6b41f942013-07-22 19:17:55 -07001130err_registerdev:
1131 adapter->surprise_removed = true;
1132 mwifiex_terminate_workqueue(adapter);
1133err_kmalloc:
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001134 mwifiex_free_adapter(adapter);
1135
1136err_init_sw:
1137 up(sem);
1138
1139exit_sem_err:
1140 return -1;
1141}
1142EXPORT_SYMBOL_GPL(mwifiex_add_card);
1143
1144/*
1145 * This function removes the card.
1146 *
1147 * This function follows the following major steps to remove the device -
1148 * - Stop data traffic
1149 * - Shutdown firmware
1150 * - Remove the logical interfaces
1151 * - Terminate the work queue
1152 * - Unregister the device
1153 * - Free the adapter structure
1154 */
1155int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
1156{
1157 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001158 int i;
1159
1160 if (down_interruptible(sem))
1161 goto exit_sem_err;
1162
1163 if (!adapter)
1164 goto exit_remove;
1165
Daniel Drake232fde02013-07-13 10:57:10 -04001166 /* We can no longer handle interrupts once we start doing the teardown
1167 * below. */
1168 if (adapter->if_ops.disable_int)
1169 adapter->if_ops.disable_int(adapter);
1170
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001171 adapter->surprise_removed = true;
1172
Marc Yang9d2e85e2014-12-31 02:36:39 -08001173 mwifiex_terminate_workqueue(adapter);
1174
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001175 /* Stop data */
1176 for (i = 0; i < adapter->priv_num; i++) {
1177 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001178 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -07001179 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001180 if (netif_carrier_ok(priv->netdev))
1181 netif_carrier_off(priv->netdev);
1182 }
1183 }
1184
1185 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
1186 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001187
1188 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001189 wait_event_interruptible(adapter->init_wait_q,
1190 adapter->init_wait_q_woken);
1191 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
1192 if (atomic_read(&adapter->rx_pending) ||
1193 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001194 atomic_read(&adapter->cmd_pending)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001195 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001196 "cmd_pending=%d\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001197 atomic_read(&adapter->rx_pending),
1198 atomic_read(&adapter->tx_pending),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -07001199 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001200 }
1201
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001202 for (i = 0; i < adapter->priv_num; i++) {
1203 priv = adapter->priv[i];
1204
1205 if (!priv)
1206 continue;
1207
1208 rtnl_lock();
Avinash Patil4facc342015-01-28 15:42:00 +05301209 if (priv->netdev &&
1210 priv->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
1211 mwifiex_del_virtual_intf(adapter->wiphy, &priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -07001212 rtnl_unlock();
1213 }
1214
Amitkumar Karwarc2868ad2013-12-02 23:17:57 -08001215 wiphy_unregister(adapter->wiphy);
1216 wiphy_free(adapter->wiphy);
Avinash Patil64b05e22012-05-08 18:30:13 -07001217
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001218 /* Unregister device */
1219 dev_dbg(adapter->dev, "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -07001220 if (adapter->if_ops.unregister_dev)
1221 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001222 /* Free adapter structure */
1223 dev_dbg(adapter->dev, "info: free adapter\n");
1224 mwifiex_free_adapter(adapter);
1225
1226exit_remove:
1227 up(sem);
1228exit_sem_err:
1229 return 0;
1230}
1231EXPORT_SYMBOL_GPL(mwifiex_remove_card);
1232
1233/*
1234 * This function initializes the module.
1235 *
1236 * The debug FS is also initialized if configured.
1237 */
1238static int
1239mwifiex_init_module(void)
1240{
1241#ifdef CONFIG_DEBUG_FS
1242 mwifiex_debugfs_init();
1243#endif
1244 return 0;
1245}
1246
1247/*
1248 * This function cleans up the module.
1249 *
1250 * The debug FS is removed if available.
1251 */
1252static void
1253mwifiex_cleanup_module(void)
1254{
1255#ifdef CONFIG_DEBUG_FS
1256 mwifiex_debugfs_remove();
1257#endif
1258}
1259
1260module_init(mwifiex_init_module);
1261module_exit(mwifiex_cleanup_module);
1262
1263MODULE_AUTHOR("Marvell International Ltd.");
1264MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
1265MODULE_VERSION(VERSION);
1266MODULE_LICENSE("GPL v2");