blob: 29d83f0c1fb8156ae681e8a16e0f5006de2d2e1b [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;
672 /* Initialize private structure */
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700673 priv->current_key_index = 0;
674 priv->media_connected = false;
675 memset(&priv->nick_name, 0, sizeof(priv->nick_name));
Avinash Patilede98bf2012-05-08 18:30:28 -0700676 memset(priv->mgmt_ie, 0,
677 sizeof(struct mwifiex_ie) * MAX_MGMT_IE_INDEX);
Avinash Patilf31acab2012-05-08 18:30:29 -0700678 priv->beacon_idx = MWIFIEX_AUTO_IDX_MASK;
679 priv->proberesp_idx = MWIFIEX_AUTO_IDX_MASK;
680 priv->assocresp_idx = MWIFIEX_AUTO_IDX_MASK;
681 priv->rsn_idx = MWIFIEX_AUTO_IDX_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700682 priv->num_tx_timeout = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700683 memcpy(dev->dev_addr, priv->curr_addr, ETH_ALEN);
684}
685
686/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700687 * This function check if command is pending.
688 */
689int is_command_pending(struct mwifiex_adapter *adapter)
690{
691 unsigned long flags;
692 int is_cmd_pend_q_empty;
693
694 spin_lock_irqsave(&adapter->cmd_pending_q_lock, flags);
695 is_cmd_pend_q_empty = list_empty(&adapter->cmd_pending_q);
696 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, flags);
697
698 return !is_cmd_pend_q_empty;
699}
700
701/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700702 * This is the main work queue function.
703 *
704 * It handles the main process, which in turn handles the complete
705 * driver operations.
706 */
707static void mwifiex_main_work_queue(struct work_struct *work)
708{
709 struct mwifiex_adapter *adapter =
710 container_of(work, struct mwifiex_adapter, main_work);
711
712 if (adapter->surprise_removed)
713 return;
714 mwifiex_main_process(adapter);
715}
716
717/*
718 * This function cancels all works in the queue and destroys
719 * the main workqueue.
720 */
721static void
722mwifiex_terminate_workqueue(struct mwifiex_adapter *adapter)
723{
724 flush_workqueue(adapter->workqueue);
725 destroy_workqueue(adapter->workqueue);
726 adapter->workqueue = NULL;
727}
728
729/*
730 * This function adds the card.
731 *
732 * This function follows the following major steps to set up the device -
733 * - Initialize software. This includes probing the card, registering
734 * the interface operations table, and allocating/initializing the
735 * adapter structure
736 * - Set up the netlink socket
737 * - Create and start the main work queue
738 * - Register the device
739 * - Initialize firmware and hardware
740 * - Add logical interfaces
741 */
742int
743mwifiex_add_card(void *card, struct semaphore *sem,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700744 struct mwifiex_if_ops *if_ops, u8 iface_type)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700745{
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700746 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700747
748 if (down_interruptible(sem))
749 goto exit_sem_err;
750
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700751 if (mwifiex_register(card, if_ops, (void **)&adapter)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700752 pr_err("%s: software init failed\n", __func__);
753 goto err_init_sw;
754 }
755
Amitkumar Karward930fae2011-10-11 17:41:21 -0700756 adapter->iface_type = iface_type;
757
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700758 adapter->hw_status = MWIFIEX_HW_STATUS_INITIALIZING;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700759 adapter->surprise_removed = false;
760 init_waitqueue_head(&adapter->init_wait_q);
761 adapter->is_suspended = false;
762 adapter->hs_activated = false;
763 init_waitqueue_head(&adapter->hs_activate_wait_q);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700764 adapter->cmd_wait_q_required = false;
765 init_waitqueue_head(&adapter->cmd_wait_q.wait);
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700766 adapter->cmd_wait_q.status = 0;
Amitkumar Karwarefaaa8b2011-10-12 20:28:06 -0700767 adapter->scan_wait_q_woken = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700768
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700769 adapter->workqueue = create_workqueue("MWIFIEX_WORK_QUEUE");
770 if (!adapter->workqueue)
771 goto err_kmalloc;
772
773 INIT_WORK(&adapter->main_work, mwifiex_main_work_queue);
774
775 /* Register the device. Fill up the private data structure with relevant
776 information from the card and request for the required IRQ. */
777 if (adapter->if_ops.register_dev(adapter)) {
778 pr_err("%s: failed to register mwifiex device\n", __func__);
779 goto err_registerdev;
780 }
781
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700782 if (mwifiex_init_hw_fw(adapter)) {
783 pr_err("%s: firmware init failed\n", __func__);
784 goto err_init_fw;
785 }
Amitkumar Karwar2be78592011-04-15 20:50:42 -0700786
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700787 up(sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700788 return 0;
789
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700790err_init_fw:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700791 pr_debug("info: %s: unregister device\n", __func__);
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700792 if (adapter->if_ops.unregister_dev)
793 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700794err_registerdev:
795 adapter->surprise_removed = true;
796 mwifiex_terminate_workqueue(adapter);
797err_kmalloc:
798 if ((adapter->hw_status == MWIFIEX_HW_STATUS_FW_READY) ||
799 (adapter->hw_status == MWIFIEX_HW_STATUS_READY)) {
800 pr_debug("info: %s: shutdown mwifiex\n", __func__);
801 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700802
803 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700804 wait_event_interruptible(adapter->init_wait_q,
805 adapter->init_wait_q_woken);
806 }
807
808 mwifiex_free_adapter(adapter);
809
810err_init_sw:
811 up(sem);
812
813exit_sem_err:
814 return -1;
815}
816EXPORT_SYMBOL_GPL(mwifiex_add_card);
817
818/*
819 * This function removes the card.
820 *
821 * This function follows the following major steps to remove the device -
822 * - Stop data traffic
823 * - Shutdown firmware
824 * - Remove the logical interfaces
825 * - Terminate the work queue
826 * - Unregister the device
827 * - Free the adapter structure
828 */
829int mwifiex_remove_card(struct mwifiex_adapter *adapter, struct semaphore *sem)
830{
831 struct mwifiex_private *priv = NULL;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700832 int i;
833
834 if (down_interruptible(sem))
835 goto exit_sem_err;
836
837 if (!adapter)
838 goto exit_remove;
839
840 adapter->surprise_removed = true;
841
842 /* Stop data */
843 for (i = 0; i < adapter->priv_num; i++) {
844 priv = adapter->priv[i];
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700845 if (priv && priv->netdev) {
Avinash Patil47411a02012-11-01 18:44:16 -0700846 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700847 if (netif_carrier_ok(priv->netdev))
848 netif_carrier_off(priv->netdev);
849 }
850 }
851
852 dev_dbg(adapter->dev, "cmd: calling mwifiex_shutdown_drv...\n");
853 adapter->init_wait_q_woken = false;
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700854
855 if (mwifiex_shutdown_drv(adapter) == -EINPROGRESS)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700856 wait_event_interruptible(adapter->init_wait_q,
857 adapter->init_wait_q_woken);
858 dev_dbg(adapter->dev, "cmd: mwifiex_shutdown_drv done\n");
859 if (atomic_read(&adapter->rx_pending) ||
860 atomic_read(&adapter->tx_pending) ||
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700861 atomic_read(&adapter->cmd_pending)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700862 dev_err(adapter->dev, "rx_pending=%d, tx_pending=%d, "
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700863 "cmd_pending=%d\n",
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700864 atomic_read(&adapter->rx_pending),
865 atomic_read(&adapter->tx_pending),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700866 atomic_read(&adapter->cmd_pending));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700867 }
868
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700869 for (i = 0; i < adapter->priv_num; i++) {
870 priv = adapter->priv[i];
871
872 if (!priv)
873 continue;
874
875 rtnl_lock();
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800876 if (priv->wdev && priv->netdev)
Johannes Berg84efbb82012-06-16 00:00:26 +0200877 mwifiex_del_virtual_intf(adapter->wiphy, priv->wdev);
Yogesh Ashok Powar93a1df42011-09-26 20:37:26 -0700878 rtnl_unlock();
879 }
880
Yogesh Ashok Powar3d82de02011-10-05 14:58:24 -0700881 priv = adapter->priv[0];
Avinash Patil64b05e22012-05-08 18:30:13 -0700882 if (!priv || !priv->wdev)
Yogesh Ashok Powar3d82de02011-10-05 14:58:24 -0700883 goto exit_remove;
884
Avinash Patil64b05e22012-05-08 18:30:13 -0700885 wiphy_unregister(priv->wdev->wiphy);
886 wiphy_free(priv->wdev->wiphy);
887
888 for (i = 0; i < adapter->priv_num; i++) {
889 priv = adapter->priv[i];
890 if (priv)
891 kfree(priv->wdev);
Amitkumar Karwar2da8cbf2012-02-03 20:34:02 -0800892 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700893
894 mwifiex_terminate_workqueue(adapter);
895
896 /* Unregister device */
897 dev_dbg(adapter->dev, "info: unregister device\n");
Amitkumar Karwar4daffe32012-04-18 20:08:28 -0700898 if (adapter->if_ops.unregister_dev)
899 adapter->if_ops.unregister_dev(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700900 /* Free adapter structure */
901 dev_dbg(adapter->dev, "info: free adapter\n");
902 mwifiex_free_adapter(adapter);
903
904exit_remove:
905 up(sem);
906exit_sem_err:
907 return 0;
908}
909EXPORT_SYMBOL_GPL(mwifiex_remove_card);
910
911/*
912 * This function initializes the module.
913 *
914 * The debug FS is also initialized if configured.
915 */
916static int
917mwifiex_init_module(void)
918{
919#ifdef CONFIG_DEBUG_FS
920 mwifiex_debugfs_init();
921#endif
922 return 0;
923}
924
925/*
926 * This function cleans up the module.
927 *
928 * The debug FS is removed if available.
929 */
930static void
931mwifiex_cleanup_module(void)
932{
933#ifdef CONFIG_DEBUG_FS
934 mwifiex_debugfs_remove();
935#endif
936}
937
938module_init(mwifiex_init_module);
939module_exit(mwifiex_cleanup_module);
940
941MODULE_AUTHOR("Marvell International Ltd.");
942MODULE_DESCRIPTION("Marvell WiFi-Ex Driver version " VERSION);
943MODULE_VERSION(VERSION);
944MODULE_LICENSE("GPL v2");