blob: 5bc7ef8d04d64cb78ba3abd676423366c2488b3c [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: major functions
3 *
4 * Copyright (C) 2011, Marvell International Ltd.
5 *
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
Bing Zhao5e6e3a92011-03-21 18:00:50 -070031/*
32 * This function registers the device and performs all the necessary
33 * initializations.
34 *
35 * The following initialization operations are performed -
36 * - Allocate adapter structure
37 * - Save interface specific operations table in adapter
38 * - Call interface specific initialization routine
39 * - Allocate private structures
40 * - Set default adapter structure parameters
41 * - Initialize locks
42 *
43 * In case of any errors during inittialization, this function also ensures
44 * proper cleanup before exiting.
45 */
46static int mwifiex_register(void *card, struct mwifiex_if_ops *if_ops,
Amitkumar Karwar287546d2011-06-08 20:39:20 +053047 void **padapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070048{
Amitkumar Karwar2be78592011-04-15 20:50:42 -070049 struct mwifiex_adapter *adapter;
50 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070051
52 adapter = kzalloc(sizeof(struct mwifiex_adapter), GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070053 if (!adapter)
Christoph Fritzb53575e2011-05-08 22:50:09 +020054 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070055
Amitkumar Karwar287546d2011-06-08 20:39:20 +053056 *padapter = adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070057 adapter->card = card;
58
59 /* Save interface specific operations in adapter */
60 memmove(&adapter->if_ops, if_ops, sizeof(struct mwifiex_if_ops));
61
62 /* card specific initialization has been deferred until now .. */
Amitkumar Karwar4daffe32012-04-18 20:08:28 -070063 if (adapter->if_ops.init_if)
64 if (adapter->if_ops.init_if(adapter))
65 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070066
67 adapter->priv_num = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070068
Avinash Patil64b05e22012-05-08 18:30:13 -070069 for (i = 0; i < MWIFIEX_MAX_BSS_NUM; i++) {
70 /* Allocate memory for private structure */
71 adapter->priv[i] =
72 kzalloc(sizeof(struct mwifiex_private), GFP_KERNEL);
73 if (!adapter->priv[i])
74 goto error;
75
76 adapter->priv[i]->adapter = adapter;
Avinash Patil64b05e22012-05-08 18:30:13 -070077 adapter->priv_num++;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070078 }
Amitkumar Karwar44b815c2011-09-29 20:43:41 -070079 mwifiex_init_lock_list(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070080
81 init_timer(&adapter->cmd_timer);
82 adapter->cmd_timer.function = mwifiex_cmd_timeout_func;
83 adapter->cmd_timer.data = (unsigned long) adapter;
84
Bing Zhao5e6e3a92011-03-21 18:00:50 -070085 return 0;
86
87error:
88 dev_dbg(adapter->dev, "info: leave mwifiex_register with error\n");
89
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -070090 for (i = 0; i < adapter->priv_num; i++)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070091 kfree(adapter->priv[i]);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -070092
Bing Zhao5e6e3a92011-03-21 18:00:50 -070093 kfree(adapter);
94
95 return -1;
96}
97
98/*
99 * This function unregisters the device and performs all the necessary
100 * cleanups.
101 *
102 * The following cleanup operations are performed -
103 * - Free the timers
104 * - Free beacon buffers
105 * - Free private structures
106 * - Free adapter structure
107 */
108static int mwifiex_unregister(struct mwifiex_adapter *adapter)
109{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700110 s32 i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700111
112 del_timer(&adapter->cmd_timer);
113
114 /* Free private structures */
115 for (i = 0; i < adapter->priv_num; i++) {
116 if (adapter->priv[i]) {
117 mwifiex_free_curr_bcn(adapter->priv[i]);
118 kfree(adapter->priv[i]);
119 }
120 }
121
122 kfree(adapter);
123 return 0;
124}
125
126/*
127 * The main process.
128 *
129 * This function is the main procedure of the driver and handles various driver
130 * operations. It runs in a loop and provides the core functionalities.
131 *
132 * The main responsibilities of this function are -
133 * - Ensure concurrency control
134 * - Handle pending interrupts and call interrupt handlers
135 * - Wake up the card if required
136 * - Handle command responses and call response handlers
137 * - Handle events and call event handlers
138 * - Execute pending commands
139 * - Transmit pending data packets
140 */
141int mwifiex_main_process(struct mwifiex_adapter *adapter)
142{
143 int ret = 0;
144 unsigned long flags;
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700145 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700146
147 spin_lock_irqsave(&adapter->main_proc_lock, flags);
148
149 /* Check if already processing */
150 if (adapter->mwifiex_processing) {
151 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
152 goto exit_main_proc;
153 } else {
154 adapter->mwifiex_processing = true;
155 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
156 }
157process_start:
158 do {
159 if ((adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING) ||
160 (adapter->hw_status == MWIFIEX_HW_STATUS_NOT_READY))
161 break;
162
163 /* Handle pending interrupt if any */
164 if (adapter->int_status) {
165 if (adapter->hs_activated)
166 mwifiex_process_hs_config(adapter);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700167 if (adapter->if_ops.process_int_status)
168 adapter->if_ops.process_int_status(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700169 }
170
171 /* Need to wake up the card ? */
172 if ((adapter->ps_state == PS_STATE_SLEEP) &&
173 (adapter->pm_wakeup_card_req &&
174 !adapter->pm_wakeup_fw_try) &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700175 (is_command_pending(adapter) ||
176 !mwifiex_wmm_lists_empty(adapter))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700177 adapter->pm_wakeup_fw_try = true;
178 adapter->if_ops.wakeup(adapter);
179 continue;
180 }
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700181
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700182 if (IS_CARD_RX_RCVD(adapter)) {
183 adapter->pm_wakeup_fw_try = false;
184 if (adapter->ps_state == PS_STATE_SLEEP)
185 adapter->ps_state = PS_STATE_AWAKE;
186 } else {
187 /* We have tried to wakeup the card already */
188 if (adapter->pm_wakeup_fw_try)
189 break;
190 if (adapter->ps_state != PS_STATE_AWAKE ||
191 adapter->tx_lock_flag)
192 break;
193
Amitkumar Karwarf931c772012-06-20 19:58:36 -0700194 if ((adapter->scan_processing &&
195 !adapter->scan_delay_cnt) || adapter->data_sent ||
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700196 mwifiex_wmm_lists_empty(adapter)) {
197 if (adapter->cmd_sent || adapter->curr_cmd ||
198 (!is_command_pending(adapter)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700199 break;
200 }
201 }
202
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700203 /* Check Rx data for USB */
204 if (adapter->iface_type == MWIFIEX_USB)
205 while ((skb = skb_dequeue(&adapter->usb_rx_data_q)))
206 mwifiex_handle_rx_packet(adapter, skb);
207
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700208 /* Check for Cmd Resp */
209 if (adapter->cmd_resp_received) {
210 adapter->cmd_resp_received = false;
211 mwifiex_process_cmdresp(adapter);
212
213 /* call mwifiex back when init_fw is done */
214 if (adapter->hw_status == MWIFIEX_HW_STATUS_INIT_DONE) {
215 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
216 mwifiex_init_fw_complete(adapter);
217 }
218 }
219
220 /* Check for event */
221 if (adapter->event_received) {
222 adapter->event_received = false;
223 mwifiex_process_event(adapter);
224 }
225
226 /* Check if we need to confirm Sleep Request
227 received previously */
228 if (adapter->ps_state == PS_STATE_PRE_SLEEP) {
229 if (!adapter->cmd_sent && !adapter->curr_cmd)
230 mwifiex_check_ps_cond(adapter);
231 }
232
233 /* * The ps_state may have been changed during processing of
234 * Sleep Request event.
235 */
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700236 if ((adapter->ps_state == PS_STATE_SLEEP) ||
237 (adapter->ps_state == PS_STATE_PRE_SLEEP) ||
238 (adapter->ps_state == PS_STATE_SLEEP_CFM) ||
239 adapter->tx_lock_flag)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700240 continue;
241
242 if (!adapter->cmd_sent && !adapter->curr_cmd) {
243 if (mwifiex_exec_next_cmd(adapter) == -1) {
244 ret = -1;
245 break;
246 }
247 }
248
Amitkumar Karwar3249ba72012-06-06 21:12:41 -0700249 if ((!adapter->scan_processing || adapter->scan_delay_cnt) &&
250 !adapter->data_sent && !mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700251 mwifiex_wmm_process_tx(adapter);
252 if (adapter->hs_activated) {
253 adapter->is_hs_configured = false;
254 mwifiex_hs_activated_event
255 (mwifiex_get_priv
256 (adapter, MWIFIEX_BSS_ROLE_ANY),
257 false);
258 }
259 }
260
261 if (adapter->delay_null_pkt && !adapter->cmd_sent &&
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700262 !adapter->curr_cmd && !is_command_pending(adapter) &&
263 mwifiex_wmm_lists_empty(adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700264 if (!mwifiex_send_null_packet
265 (mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
266 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
267 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET)) {
268 adapter->delay_null_pkt = false;
269 adapter->ps_state = PS_STATE_SLEEP;
270 }
271 break;
272 }
273 } while (true);
274
275 if ((adapter->int_status) || IS_CARD_RX_RCVD(adapter))
276 goto process_start;
277
278 spin_lock_irqsave(&adapter->main_proc_lock, flags);
279 adapter->mwifiex_processing = false;
280 spin_unlock_irqrestore(&adapter->main_proc_lock, flags);
281
282exit_main_proc:
283 if (adapter->hw_status == MWIFIEX_HW_STATUS_CLOSING)
284 mwifiex_shutdown_drv(adapter);
285 return ret;
286}
Bing Zhao601216e2012-11-05 16:59:15 -0800287EXPORT_SYMBOL_GPL(mwifiex_main_process);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700288
289/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700290 * This function frees the adapter structure.
291 *
292 * Additionally, this closes the netlink socket, frees the timers
293 * and private structures.
294 */
295static void mwifiex_free_adapter(struct mwifiex_adapter *adapter)
296{
297 if (!adapter) {
298 pr_err("%s: adapter is NULL\n", __func__);
299 return;
300 }
301
302 mwifiex_unregister(adapter);
303 pr_debug("info: %s: free adapter\n", __func__);
304}
305
306/*
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700307 * This function gets firmware and initializes it.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700308 *
309 * The main initialization steps followed are -
310 * - Download the correct firmware to card
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700311 * - Issue the init commands to firmware
312 */
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700313static void mwifiex_fw_dpc(const struct firmware *firmware, void *context)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700314{
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700315 int ret;
316 char fmt[64];
317 struct mwifiex_private *priv;
318 struct mwifiex_adapter *adapter = context;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700319 struct mwifiex_fw_image fw;
320
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700321 if (!firmware) {
322 dev_err(adapter->dev,
323 "Failed to get firmware %s\n", adapter->fw_name);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700324 goto done;
325 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700326
327 memset(&fw, 0, sizeof(struct mwifiex_fw_image));
328 adapter->firmware = firmware;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700329 fw.fw_buf = (u8 *) adapter->firmware->data;
330 fw.fw_len = adapter->firmware->size;
331
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700332 if (adapter->if_ops.dnld_fw)
333 ret = adapter->if_ops.dnld_fw(adapter, &fw);
334 else
335 ret = mwifiex_dnld_fw(adapter, &fw);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700336 if (ret == -1)
337 goto done;
338
339 dev_notice(adapter->dev, "WLAN FW is active\n");
340
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700341 if (cal_data_cfg) {
342 if ((request_firmware(&adapter->cal_data, cal_data_cfg,
343 adapter->dev)) < 0)
344 dev_err(adapter->dev,
345 "Cal data request_firmware() failed\n");
346 }
347
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700348 adapter->init_wait_q_woken = false;
349 ret = mwifiex_init_fw(adapter);
350 if (ret == -1) {
351 goto done;
352 } else if (!ret) {
353 adapter->hw_status = MWIFIEX_HW_STATUS_READY;
354 goto done;
355 }
356 /* Wait for mwifiex_init to complete */
357 wait_event_interruptible(adapter->init_wait_q,
358 adapter->init_wait_q_woken);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700359 if (adapter->hw_status != MWIFIEX_HW_STATUS_READY)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700360 goto done;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700361
Avinash Patild6bffe82012-05-08 18:30:15 -0700362 priv = adapter->priv[MWIFIEX_BSS_ROLE_STA];
363 if (mwifiex_register_cfg80211(adapter)) {
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700364 dev_err(adapter->dev, "cannot register with cfg80211\n");
365 goto err_init_fw;
366 }
367
368 rtnl_lock();
369 /* Create station interface by default */
Avinash Patild6bffe82012-05-08 18:30:15 -0700370 if (!mwifiex_add_virtual_intf(adapter->wiphy, "mlan%d",
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700371 NL80211_IFTYPE_STATION, NULL, NULL)) {
372 dev_err(adapter->dev, "cannot create default STA interface\n");
373 goto err_add_intf;
374 }
Avinash Patild6bffe82012-05-08 18:30:15 -0700375
376 /* Create AP interface by default */
377 if (!mwifiex_add_virtual_intf(adapter->wiphy, "uap%d",
378 NL80211_IFTYPE_AP, NULL, NULL)) {
379 dev_err(adapter->dev, "cannot create default AP interface\n");
380 goto err_add_intf;
381 }
Stone Piao197f4a22012-09-25 20:23:40 -0700382
383 /* Create P2P interface by default */
384 if (!mwifiex_add_virtual_intf(adapter->wiphy, "p2p%d",
385 NL80211_IFTYPE_P2P_CLIENT, NULL, NULL)) {
386 dev_err(adapter->dev, "cannot create default P2P interface\n");
387 goto err_add_intf;
388 }
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700389 rtnl_unlock();
390
391 mwifiex_drv_get_driver_version(adapter, fmt, sizeof(fmt) - 1);
392 dev_notice(adapter->dev, "driver_version = %s\n", fmt);
393 goto done;
394
395err_add_intf:
Johannes Berg84efbb82012-06-16 00:00:26 +0200396 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700397 rtnl_unlock();
398err_init_fw:
399 pr_debug("info: %s: unregister device\n", __func__);
400 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700401done:
Amitkumar Karwar388ec382013-05-17 17:50:25 -0700402 if (adapter->cal_data) {
403 release_firmware(adapter->cal_data);
404 adapter->cal_data = NULL;
405 }
Jesper Juhl4fb25c52012-04-09 22:51:12 +0200406 release_firmware(adapter->firmware);
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700407 complete(&adapter->fw_load);
408 return;
409}
410
411/*
412 * This function initializes the hardware and gets firmware.
413 */
414static int mwifiex_init_hw_fw(struct mwifiex_adapter *adapter)
415{
416 int ret;
417
418 init_completion(&adapter->fw_load);
419 ret = request_firmware_nowait(THIS_MODULE, 1, adapter->fw_name,
420 adapter->dev, GFP_KERNEL, adapter,
421 mwifiex_fw_dpc);
422 if (ret < 0)
423 dev_err(adapter->dev,
424 "request_firmware_nowait() returned error %d\n", ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700425 return ret;
426}
427
428/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700429 * CFG802.11 network device handler for open.
430 *
431 * Starts the data queue.
432 */
433static int
434mwifiex_open(struct net_device *dev)
435{
Avinash Patilbbea3bc2011-12-08 20:41:05 -0800436 netif_tx_start_all_queues(dev);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700437 return 0;
438}
439
440/*
441 * CFG802.11 network device handler for close.
442 */
443static int
444mwifiex_close(struct net_device *dev)
445{
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700446 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
447
448 if (priv->scan_request) {
449 dev_dbg(priv->adapter->dev, "aborting scan on ndo_stop\n");
450 cfg80211_scan_done(priv->scan_request, 1);
451 priv->scan_request = NULL;
Amitkumar Karwar75ab7532013-05-17 17:50:20 -0700452 priv->scan_aborting = true;
Amitkumar Karwarf162cac2012-10-19 19:19:16 -0700453 }
454
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700455 return 0;
456}
457
458/*
Stone Piaoe39faa72012-09-25 20:23:32 -0700459 * Add buffer into wmm tx queue and queue work to transmit it.
460 */
461int mwifiex_queue_tx_pkt(struct mwifiex_private *priv, struct sk_buff *skb)
462{
Avinash Patil47411a02012-11-01 18:44:16 -0700463 struct netdev_queue *txq;
464 int index = mwifiex_1d_to_wmm_queue[skb->priority];
465
466 if (atomic_inc_return(&priv->wmm_tx_pending[index]) >= MAX_TX_PENDING) {
467 txq = netdev_get_tx_queue(priv->netdev, index);
468 if (!netif_tx_queue_stopped(txq)) {
469 netif_tx_stop_queue(txq);
470 dev_dbg(priv->adapter->dev, "stop queue: %d\n", index);
471 }
472 }
473
Stone Piaoe39faa72012-09-25 20:23:32 -0700474 atomic_inc(&priv->adapter->tx_pending);
Avinash Patil47411a02012-11-01 18:44:16 -0700475 mwifiex_wmm_add_buf_txqueue(priv, skb);
Stone Piaoe39faa72012-09-25 20:23:32 -0700476
477 if (priv->adapter->scan_delay_cnt)
478 atomic_set(&priv->adapter->is_tx_received, true);
479
Stone Piaoe39faa72012-09-25 20:23:32 -0700480 queue_work(priv->adapter->workqueue, &priv->adapter->main_work);
481
482 return 0;
483}
484
485/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700486 * CFG802.11 network device handler for data transmission.
487 */
488static int
489mwifiex_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
490{
491 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700492 struct sk_buff *new_skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700493 struct mwifiex_txinfo *tx_info;
Avinash Patil47411a02012-11-01 18:44:16 -0700494 struct timeval tv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700495
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800496 dev_dbg(priv->adapter->dev, "data: %lu BSS(%d-%d): Data <= kernel\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700497 jiffies, priv->bss_type, priv->bss_num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700498
499 if (priv->adapter->surprise_removed) {
Christoph Fritzb53575e2011-05-08 22:50:09 +0200500 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700501 priv->stats.tx_dropped++;
502 return 0;
503 }
504 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
505 dev_err(priv->adapter->dev, "Tx: bad skb len %d\n", skb->len);
Christoph Fritzb53575e2011-05-08 22:50:09 +0200506 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700507 priv->stats.tx_dropped++;
508 return 0;
509 }
510 if (skb_headroom(skb) < MWIFIEX_MIN_DATA_HEADER_LEN) {
511 dev_dbg(priv->adapter->dev,
512 "data: Tx: insufficient skb headroom %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700513 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700514 /* Insufficient skb headroom - allocate a new skb */
515 new_skb =
516 skb_realloc_headroom(skb, MWIFIEX_MIN_DATA_HEADER_LEN);
517 if (unlikely(!new_skb)) {
518 dev_err(priv->adapter->dev, "Tx: cannot alloca new_skb\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200519 kfree_skb(skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700520 priv->stats.tx_dropped++;
521 return 0;
522 }
523 kfree_skb(skb);
524 skb = new_skb;
525 dev_dbg(priv->adapter->dev, "info: new skb headroomd %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700526 skb_headroom(skb));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700527 }
528
529 tx_info = MWIFIEX_SKB_TXCB(skb);
Yogesh Ashok Powar9da9a3b2012-01-11 20:06:11 -0800530 tx_info->bss_num = priv->bss_num;
531 tx_info->bss_type = priv->bss_type;
Avinash Patil47411a02012-11-01 18:44:16 -0700532
533 /* Record the current time the packet was queued; used to
534 * determine the amount of time the packet was queued in
535 * the driver before it was sent to the firmware.
536 * The delay is then sent along with the packet to the
537 * firmware for aggregate delay calculation for stats and
538 * MSDU lifetime expiry.
539 */
540 do_gettimeofday(&tv);
541 skb->tstamp = timeval_to_ktime(tv);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700542
Stone Piaoe39faa72012-09-25 20:23:32 -0700543 mwifiex_queue_tx_pkt(priv, skb);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700544
545 return 0;
546}
547
548/*
549 * CFG802.11 network device handler for setting MAC address.
550 */
551static int
552mwifiex_set_mac_address(struct net_device *dev, void *addr)
553{
554 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwara5ffddb2011-06-20 15:21:48 -0700555 struct sockaddr *hw_addr = addr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700556 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700557
558 memcpy(priv->curr_addr, hw_addr->sa_data, ETH_ALEN);
559
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700560 /* Send request to firmware */
561 ret = mwifiex_send_cmd_sync(priv, HostCmd_CMD_802_11_MAC_ADDRESS,
562 HostCmd_ACT_GEN_SET, 0, NULL);
563
564 if (!ret)
565 memcpy(priv->netdev->dev_addr, priv->curr_addr, ETH_ALEN);
566 else
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700567 dev_err(priv->adapter->dev,
568 "set mac address failed: ret=%d\n", ret);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700569
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700570 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
571
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700572 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700573}
574
575/*
576 * CFG802.11 network device handler for setting multicast list.
577 */
578static void mwifiex_set_multicast_list(struct net_device *dev)
579{
580 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700581 struct mwifiex_multicast_list mcast_list;
582
583 if (dev->flags & IFF_PROMISC) {
584 mcast_list.mode = MWIFIEX_PROMISC_MODE;
585 } else if (dev->flags & IFF_ALLMULTI ||
586 netdev_mc_count(dev) > MWIFIEX_MAX_MULTICAST_LIST_SIZE) {
587 mcast_list.mode = MWIFIEX_ALL_MULTI_MODE;
588 } else {
589 mcast_list.mode = MWIFIEX_MULTICAST_MODE;
590 if (netdev_mc_count(dev))
591 mcast_list.num_multicast_addr =
592 mwifiex_copy_mcast_addr(&mcast_list, dev);
593 }
594 mwifiex_request_set_multicast_list(priv, &mcast_list);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700595}
596
597/*
598 * CFG802.11 network device handler for transmission timeout.
599 */
600static void
601mwifiex_tx_timeout(struct net_device *dev)
602{
603 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
604
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700605 priv->num_tx_timeout++;
Ashok Nagarajan8908c7d2013-03-08 10:58:45 -0800606 priv->tx_timeout_cnt++;
607 dev_err(priv->adapter->dev,
608 "%lu : Tx timeout(#%d), bss_type-num = %d-%d\n",
609 jiffies, priv->tx_timeout_cnt, priv->bss_type, priv->bss_num);
610 mwifiex_set_trans_start(dev);
611
612 if (priv->tx_timeout_cnt > TX_TIMEOUT_THRESHOLD &&
613 priv->adapter->if_ops.card_reset) {
614 dev_err(priv->adapter->dev,
615 "tx_timeout_cnt exceeds threshold. Triggering card reset!\n");
616 priv->adapter->if_ops.card_reset(priv->adapter);
617 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700618}
619
620/*
621 * CFG802.11 network device handler for statistics retrieval.
622 */
623static struct net_device_stats *mwifiex_get_stats(struct net_device *dev)
624{
625 struct mwifiex_private *priv = mwifiex_netdev_get_priv(dev);
626
627 return &priv->stats;
628}
629
Avinash Patil47411a02012-11-01 18:44:16 -0700630static u16
631mwifiex_netdev_select_wmm_queue(struct net_device *dev, struct sk_buff *skb)
632{
633 skb->priority = cfg80211_classify8021d(skb);
634 return mwifiex_1d_to_wmm_queue[skb->priority];
635}
636
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700637/* Network device handlers */
638static const struct net_device_ops mwifiex_netdev_ops = {
639 .ndo_open = mwifiex_open,
640 .ndo_stop = mwifiex_close,
641 .ndo_start_xmit = mwifiex_hard_start_xmit,
642 .ndo_set_mac_address = mwifiex_set_mac_address,
643 .ndo_tx_timeout = mwifiex_tx_timeout,
644 .ndo_get_stats = mwifiex_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000645 .ndo_set_rx_mode = mwifiex_set_multicast_list,
Avinash Patil47411a02012-11-01 18:44:16 -0700646 .ndo_select_queue = mwifiex_netdev_select_wmm_queue,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700647};
648
649/*
650 * This function initializes the private structure parameters.
651 *
652 * The following wait queues are initialized -
653 * - IOCTL wait queue
654 * - Command wait queue
655 * - Statistics wait queue
656 *
657 * ...and the following default parameters are set -
658 * - Current key index : Set to 0
659 * - Rate index : Set to auto
660 * - Media connected : Set to disconnected
661 * - Adhoc link sensed : Set to false
662 * - Nick name : Set to null
663 * - Number of Tx timeout : Set to 0
664 * - Device address : Set to current address
665 *
666 * In addition, the CFG80211 work queue is also created.
667 */
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700668void mwifiex_init_priv_params(struct mwifiex_private *priv,
669 struct net_device *dev)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700670{
671 dev->netdev_ops = &mwifiex_netdev_ops;
Amitkumar Karwarf16fdc92013-05-06 19:46:54 -0700672 dev->destructor = free_netdev;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700673 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700674 priv->current_key_index = 0;
675 priv->media_connected = false;
676 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
Avinash Patilede98bf2012-05-08 18:30:28 -0700677 memset(priv->mgmt_ie, 0,
678 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700679 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
680 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
681 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
682 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700683 priv->num_tx_timeout = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700684 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
685}
686
687/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700688 * This function check if command is pending.
689 */
690int is_command_pending(struct mwifiex_adapter *adapter)
691{
692 unsigned long flags;
693 int is_cmd_pend_q_empty;
694
695 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
696 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
697 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
698
699 return !is_cmd_pend_q_empty;
700}
701
702/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700703 * This is the main work queue function.
704 *
705 * It handles the main process, which in turn handles the complete
706 * driver operations.
707 */
708static void mwifiex_main_work_queue(struct work_struct *work)
709{
710 struct mwifiex_adapter *adapter =
711 container_of(work, struct mwifiex_adapter, main_work);
712
713 if (adapter->surprise_removed)
714 return;
715 mwifiex_main_process(adapter);
716}
717
718/*
719 * This function cancels all works in the queue and destroys
720 * the main workqueue.
721 */
722static void
723mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
724{
725 flush_workqueue(adapter->workqueue);
726 destroy_workqueue(adapter->workqueue);
727 adapter->workqueue = NULL;
728}
729
730/*
731 * This function adds the card.
732 *
733 * This function follows the following major steps to set up the device -
734 * - Initialize software. This includes probing the card, registering
735 * the interface operations table, and allocating/initializing the
736 * adapter structure
737 * - Set up the netlink socket
738 * - Create and start the main work queue
739 * - Register the device
740 * - Initialize firmware and hardware
741 * - Add logical interfaces
742 */
743int
744mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700745 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700746{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700747 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700748
749 if (down_interruptible(sem))
750 goto exit_sem_err;
751
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700752 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700753 pr_err("%s: software init failed\n", __func__);
754 goto err_init_sw;
755 }
756
Amitkumar Karward930fae2011-10-11 17:41:21 -0700757 adapter->iface_type = iface_type;
758
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700759 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700760 adapter->surprise_removed = false;
761 init_waitqueue_head(&adapter->init_wait_q);
762 adapter->is_suspended = false;
763 adapter->hs_activated = false;
764 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700765 adapter->cmd_wait_q_required = false;
766 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700767 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700768 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700769
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700770 adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
771 if (!adapter->workqueue)
772 goto err_kmalloc;
773
774 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
775
776 /* Register the device. Fill up the private data structure with relevant
777 information from the card and request for the required IRQ. */
778 if (adapter->if_ops.register_dev(adapter)) {
779 pr_err("%s: failed to register mwifiex device\n", __func__);
780 goto err_registerdev;
781 }
782
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700783 if (mwifiex_init_hw_fw(adapter)) {
784 pr_err("%s: firmware init failed\n", __func__);
785 goto err_init_fw;
786 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700787
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700788 up(sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700789 return 0;
790
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700791err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700792 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700793 if (adapter->if_ops.unregister_dev)
794 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700795err_registerdev:
796 adapter->surprise_removed = true;
797 mwifiex_terminate_workqueue(adapter);
798err_kmalloc:
799 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
800 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
801 pr_debug("info: %s: shutdown mwifiex\n", __func__);
802 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700803
804 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700805 wait_event_interruptible(adapter->init_wait_q,
806 adapter->init_wait_q_woken);
807 }
808
809 mwifiex_free_adapter(adapter);
810
811err_init_sw:
812 up(sem);
813
814exit_sem_err:
815 return -1;
816}
817EXPORT_SYMBOL_GPL(mwifiex_add_card);
818
819/*
820 * This function removes the card.
821 *
822 * This function follows the following major steps to remove the device -
823 * - Stop data traffic
824 * - Shutdown firmware
825 * - Remove the logical interfaces
826 * - Terminate the work queue
827 * - Unregister the device
828 * - Free the adapter structure
829 */
830int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
831{
832 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700833 int i;
834
835 if (down_interruptible(sem))
836 goto exit_sem_err;
837
838 if (!adapter)
839 goto exit_remove;
840
841 adapter->surprise_removed = true;
842
843 /* Stop data */
844 for (i = 0; i < adapter->priv_num; i++) {
845 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700846 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -0700847 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700848 if (netif_carrier_ok(priv->netdev))
849 netif_carrier_off(priv->netdev);
850 }
851 }
852
853 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
854 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700855
856 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700857 wait_event_interruptible(adapter->init_wait_q,
858 adapter->init_wait_q_woken);
859 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
860 if (atomic_read(&adapter->rx_pending) ||
861 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700862 atomic_read(&adapter->cmd_pending)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700863 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700864 "cmd_pending=%d\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700865 atomic_read(&adapter->rx_pending),
866 atomic_read(&adapter->tx_pending),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700867 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700868 }
869
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700870 for (i = 0; i < adapter->priv_num; i++) {
871 priv = adapter->priv[i];
872
873 if (!priv)
874 continue;
875
876 rtnl_lock();
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800877 if (priv->wdev && priv->netdev)
Johannes Berg84efbb82012-06-16 00:00:26 +0200878 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700879 rtnl_unlock();
880 }
881
Yogesh Ashok Powar3d82de02011-10-05 14:58:24 -0700882 priv = adapter->priv[0];
Avinash Patil64b05e22012-05-08 18:30:13 -0700883 if (!priv || !priv->wdev)
Yogesh Ashok Powar3d82de02011-10-05 14:58:24 -0700884 goto exit_remove;
885
Avinash Patil64b05e22012-05-08 18:30:13 -0700886 wiphy_unregister(priv->wdev->wiphy);
887 wiphy_free(priv->wdev->wiphy);
888
889 for (i = 0; i < adapter->priv_num; i++) {
890 priv = adapter->priv[i];
891 if (priv)
892 kfree(priv->wdev);
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800893 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700894
895 mwifiex_terminate_workqueue(adapter);
896
897 /* Unregister device */
898 dev_dbg(adapter->dev, "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700899 if (adapter->if_ops.unregister_dev)
900 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700901 /* Free adapter structure */
902 dev_dbg(adapter->dev, "info: free adapter\n");
903 mwifiex_free_adapter(adapter);
904
905exit_remove:
906 up(sem);
907exit_sem_err:
908 return 0;
909}
910EXPORT_SYMBOL_GPL(mwifiex_remove_card);
911
912/*
913 * This function initializes the module.
914 *
915 * The debug FS is also initialized if configured.
916 */
917static int
918mwifiex_init_module(void)
919{
920#ifdef CONFIG_DEBUG_FS
921 mwifiex_debugfs_init();
922#endif
923 return 0;
924}
925
926/*
927 * This function cleans up the module.
928 *
929 * The debug FS is removed if available.
930 */
931static void
932mwifiex_cleanup_module(void)
933{
934#ifdef CONFIG_DEBUG_FS
935 mwifiex_debugfs_remove();
936#endif
937}
938
939module_init(mwifiex_init_module);
940module_exit(mwifiex_cleanup_module);
941
942MODULE_AUTHOR("Marvell International Ltd.");
943MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
944MODULE_VERSION(VERSION);
945MODULE_LICENSE("GPL v2");