blob: e0dcd3ed7a69d01d1a5342be587a8901e2b34b57 [file] [log] [blame]
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001/*
2 * Marvell Wireless LAN device driver: SDIO specific handling
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 <linux/firmware.h>
21
22#include "decl.h"
23#include "ioctl.h"
24#include "util.h"
25#include "fw.h"
26#include "main.h"
27#include "wmm.h"
28#include "11n.h"
29#include "sdio.h"
30
31
32#define SDIO_VERSION "1.0"
33
Amitkumar Karwar287546d2011-06-08 20:39:20 +053034/* The mwifiex_sdio_remove() callback function is called when
35 * user removes this module from kernel space or ejects
36 * the card from the slot. The driver handles these 2 cases
37 * differently.
38 * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
39 * HS_CANCEL etc.) are sent to the firmware.
40 * If the card is removed, there is no need to send these command.
41 *
42 * The variable 'user_rmmod' is used to distinguish these two
43 * scenarios. This flag is initialized as FALSE in case the card
44 * is removed, and will be set to TRUE for module removal when
45 * module_exit function is called.
46 */
47static u8 user_rmmod;
48
Bing Zhao5e6e3a92011-03-21 18:00:50 -070049static struct mwifiex_if_ops sdio_ops;
50
51static struct semaphore add_remove_card_sem;
52
53/*
54 * SDIO probe.
55 *
56 * This function probes an mwifiex device and registers it. It allocates
57 * the card structure, enables SDIO function number and initiates the
58 * device registration and initialization procedure by adding a logical
59 * interface.
60 */
61static int
62mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
63{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -070064 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070065 struct sdio_mmc_card *card = NULL;
66
67 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -070068 func->vendor, func->device, func->class, func->num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070069
70 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +000071 if (!card)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070072 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070073
74 card->func = func;
75
76 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
77
Amitkumar Karwar05889f82013-05-17 17:50:27 -070078 if (id->driver_data) {
79 struct mwifiex_sdio_device *data = (void *)id->driver_data;
80
81 card->firmware = data->firmware;
82 card->reg = data->reg;
83 card->max_ports = data->max_ports;
84 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -070085 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
86 card->has_control_mask = data->has_control_mask;
Amitkumar Karwar828cf222014-02-27 19:35:13 -080087 card->tx_buf_size = data->tx_buf_size;
Amitkumar Karwar05889f82013-05-17 17:50:27 -070088 }
89
Bing Zhao5e6e3a92011-03-21 18:00:50 -070090 sdio_claim_host(func);
91 ret = sdio_enable_func(func);
92 sdio_release_host(func);
93
94 if (ret) {
95 pr_err("%s: failed to enable function\n", __func__);
Christoph Fritzb53575e2011-05-08 22:50:09 +020096 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070097 return -EIO;
98 }
99
Amitkumar Karward930fae2011-10-11 17:41:21 -0700100 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
101 MWIFIEX_SDIO)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700102 pr_err("%s: add card failed\n", __func__);
103 kfree(card);
104 sdio_claim_host(func);
105 ret = sdio_disable_func(func);
106 sdio_release_host(func);
107 ret = -1;
108 }
109
110 return ret;
111}
112
113/*
Amitkumar Karwar2cdf3592013-07-22 19:17:56 -0700114 * SDIO resume.
115 *
116 * Kernel needs to suspend all functions separately. Therefore all
117 * registered functions must have drivers with suspend and resume
118 * methods. Failing that the kernel simply removes the whole card.
119 *
120 * If already not resumed, this function turns on the traffic and
121 * sends a host sleep cancel request to the firmware.
122 */
123static int mwifiex_sdio_resume(struct device *dev)
124{
125 struct sdio_func *func = dev_to_sdio_func(dev);
126 struct sdio_mmc_card *card;
127 struct mwifiex_adapter *adapter;
128 mmc_pm_flag_t pm_flag = 0;
129
130 if (func) {
131 pm_flag = sdio_get_host_pm_caps(func);
132 card = sdio_get_drvdata(func);
133 if (!card || !card->adapter) {
134 pr_err("resume: invalid card or adapter\n");
135 return 0;
136 }
137 } else {
138 pr_err("resume: sdio_func is not specified\n");
139 return 0;
140 }
141
142 adapter = card->adapter;
143
144 if (!adapter->is_suspended) {
145 dev_warn(adapter->dev, "device already resumed\n");
146 return 0;
147 }
148
149 adapter->is_suspended = false;
150
151 /* Disable Host Sleep */
152 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
153 MWIFIEX_ASYNC_CMD);
154
155 return 0;
156}
157
158/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700159 * SDIO remove.
160 *
161 * This function removes the interface and frees up the card structure.
162 */
163static void
164mwifiex_sdio_remove(struct sdio_func *func)
165{
166 struct sdio_mmc_card *card;
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530167 struct mwifiex_adapter *adapter;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700168 struct mwifiex_private *priv;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700169
170 pr_debug("info: SDIO func num=%d\n", func->num);
171
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530172 card = sdio_get_drvdata(func);
173 if (!card)
174 return;
175
176 adapter = card->adapter;
177 if (!adapter || !adapter->priv_num)
178 return;
179
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700180 /* In case driver is removed when asynchronous FW load is in progress */
181 wait_for_completion(&adapter->fw_load);
182
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530183 if (user_rmmod) {
184 if (adapter->is_suspended)
185 mwifiex_sdio_resume(adapter->dev);
186
Amitkumar Karwar848819f2014-02-27 19:35:17 -0800187 mwifiex_deauthenticate_all(adapter);
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530188
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700189 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
190 mwifiex_disable_auto_ds(priv);
191 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700192 }
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530193
194 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700195}
196
197/*
198 * SDIO suspend.
199 *
200 * Kernel needs to suspend all functions separately. Therefore all
201 * registered functions must have drivers with suspend and resume
202 * methods. Failing that the kernel simply removes the whole card.
203 *
204 * If already not suspended, this function allocates and sends a host
205 * sleep activate request to the firmware and turns off the traffic.
206 */
207static int mwifiex_sdio_suspend(struct device *dev)
208{
209 struct sdio_func *func = dev_to_sdio_func(dev);
210 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700211 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700212 mmc_pm_flag_t pm_flag = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700213 int ret = 0;
214
215 if (func) {
216 pm_flag = sdio_get_host_pm_caps(func);
217 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700218 sdio_func_id(func), pm_flag);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700219 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
220 pr_err("%s: cannot remain alive while host is"
221 " suspended\n", sdio_func_id(func));
222 return -ENOSYS;
223 }
224
225 card = sdio_get_drvdata(func);
226 if (!card || !card->adapter) {
227 pr_err("suspend: invalid card or adapter\n");
228 return 0;
229 }
230 } else {
231 pr_err("suspend: sdio_func is not specified\n");
232 return 0;
233 }
234
235 adapter = card->adapter;
236
237 /* Enable the Host Sleep */
Bing Zhaodd321ac2012-11-15 15:58:48 -0800238 if (!mwifiex_enable_hs(adapter)) {
239 dev_err(adapter->dev, "cmd: failed to suspend\n");
240 return -EFAULT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700241 }
242
Bing Zhaodd321ac2012-11-15 15:58:48 -0800243 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
244 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
245
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700246 /* Indicate device suspended */
247 adapter->is_suspended = true;
248
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700249 return ret;
250}
251
WarheadsSE98e6b9d2012-04-24 15:57:21 -0400252/* Device ID for SD8786 */
253#define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700254/* Device ID for SD8787 */
255#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
Bing Zhaoe3bea1c2011-11-16 20:40:35 -0800256/* Device ID for SD8797 */
257#define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700258/* Device ID for SD8897 */
259#define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700260
261/* WLAN IDs */
262static const struct sdio_device_id mwifiex_ids[] = {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700263 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
264 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
265 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
266 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
267 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
268 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700269 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
270 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700271 {},
272};
273
274MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
275
276static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
277 .suspend = mwifiex_sdio_suspend,
278 .resume = mwifiex_sdio_resume,
279};
280
281static struct sdio_driver mwifiex_sdio = {
282 .name = "mwifiex_sdio",
283 .id_table = mwifiex_ids,
284 .probe = mwifiex_sdio_probe,
285 .remove = mwifiex_sdio_remove,
286 .drv = {
287 .owner = THIS_MODULE,
288 .pm = &mwifiex_sdio_pm_ops,
289 }
290};
291
Daniel Drake232fde02013-07-13 10:57:10 -0400292/* Write data into SDIO card register. Caller claims SDIO device. */
293static int
294mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
295{
296 int ret = -1;
297 sdio_writeb(func, data, reg, &ret);
298 return ret;
299}
300
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700301/*
302 * This function writes data into SDIO card register.
303 */
304static int
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700305mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700306{
307 struct sdio_mmc_card *card = adapter->card;
Daniel Drake232fde02013-07-13 10:57:10 -0400308 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700309
310 sdio_claim_host(card->func);
Daniel Drake232fde02013-07-13 10:57:10 -0400311 ret = mwifiex_write_reg_locked(card->func, reg, data);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700312 sdio_release_host(card->func);
313
314 return ret;
315}
316
317/*
318 * This function reads data from SDIO card register.
319 */
320static int
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700321mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700322{
323 struct sdio_mmc_card *card = adapter->card;
324 int ret = -1;
325 u8 val;
326
327 sdio_claim_host(card->func);
328 val = sdio_readb(card->func, reg, &ret);
329 sdio_release_host(card->func);
330
331 *data = val;
332
333 return ret;
334}
335
336/*
337 * This function writes multiple data into SDIO card memory.
338 *
339 * This does not work in suspended mode.
340 */
341static int
342mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700343 u8 *buffer, u32 pkt_len, u32 port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700344{
345 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800346 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700347 u8 blk_mode =
348 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
349 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
350 u32 blk_cnt =
351 (blk_mode ==
352 BLOCK_MODE) ? (pkt_len /
353 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
354 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
355
356 if (adapter->is_suspended) {
357 dev_err(adapter->dev,
358 "%s: not allowed while suspended\n", __func__);
359 return -1;
360 }
361
362 sdio_claim_host(card->func);
363
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800364 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700365
366 sdio_release_host(card->func);
367
368 return ret;
369}
370
371/*
372 * This function reads multiple data from SDIO card memory.
373 */
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700374static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
375 u32 len, u32 port, u8 claim)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700376{
377 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800378 int ret;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700379 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
380 : BLOCK_MODE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700381 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700382 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
383 : len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700384 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
385
386 if (claim)
387 sdio_claim_host(card->func);
388
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800389 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700390
391 if (claim)
392 sdio_release_host(card->func);
393
394 return ret;
395}
396
397/*
398 * This function wakes up the card.
399 *
400 * A host power up command is written to the card configuration
401 * register to wake up the card.
402 */
403static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
404{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700405 dev_dbg(adapter->dev, "event: wakeup device...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700406
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700407 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700408}
409
410/*
411 * This function is called after the card has woken up.
412 *
413 * The card configuration register is reset.
414 */
415static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
416{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700417 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700418
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700419 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700420}
421
422/*
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700423 * This function is used to initialize IO ports for the
424 * chipsets supporting SDIO new mode eg SD8897.
425 */
426static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
427{
428 u8 reg;
429
430 adapter->ioport = MEM_PORT;
431
432 /* enable sdio new mode */
433 if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, &reg))
434 return -1;
435 if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
436 reg | CMD53_NEW_MODE))
437 return -1;
438
439 /* Configure cmd port and enable reading rx length from the register */
440 if (mwifiex_read_reg(adapter, CMD_CONFIG_0, &reg))
441 return -1;
442 if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
443 return -1;
444
445 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
446 * completed
447 */
448 if (mwifiex_read_reg(adapter, CMD_CONFIG_1, &reg))
449 return -1;
450 if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
451 return -1;
452
453 return 0;
454}
455
456/* This function initializes the IO ports.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700457 *
458 * The following operations are performed -
459 * - Read the IO ports (0, 1 and 2)
460 * - Set host interrupt Reset-To-Read to clear
461 * - Set auto re-enable interrupt
462 */
463static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
464{
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700465 u8 reg;
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700466 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700467
468 adapter->ioport = 0;
469
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700470 if (card->supports_sdio_new_mode) {
471 if (mwifiex_init_sdio_new_mode(adapter))
472 return -1;
473 goto cont;
474 }
475
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700476 /* Read the IO port */
477 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
478 adapter->ioport |= (reg & 0xff);
479 else
480 return -1;
481
482 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
483 adapter->ioport |= ((reg & 0xff) << 8);
484 else
485 return -1;
486
487 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
488 adapter->ioport |= ((reg & 0xff) << 16);
489 else
490 return -1;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700491cont:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700492 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
493
494 /* Set Host interrupt reset to read to clear */
495 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
496 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700497 reg | card->reg->sdio_int_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700498 else
499 return -1;
500
501 /* Dnld/Upld ready set to auto reset */
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700502 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
503 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700504 reg | AUTO_RE_ENABLE_INT);
505 else
506 return -1;
507
508 return 0;
509}
510
511/*
512 * This function sends data to the card.
513 */
514static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
515 u8 *payload, u32 pkt_len, u32 port)
516{
517 u32 i = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700518 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700519
520 do {
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700521 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700522 if (ret) {
523 i++;
524 dev_err(adapter->dev, "host_to_card, write iomem"
525 " (%d) failed: %d\n", i, ret);
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700526 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700527 dev_err(adapter->dev, "write CFG reg failed\n");
528
529 ret = -1;
530 if (i > MAX_WRITE_IOMEM_RETRY)
531 return ret;
532 }
533 } while (ret == -1);
534
535 return ret;
536}
537
538/*
539 * This function gets the read port.
540 *
541 * If control port bit is set in MP read bitmap, the control port
542 * is returned, otherwise the current read port is returned and
543 * the value is increased (provided it does not reach the maximum
544 * limit, in which case it is reset to 1)
545 */
546static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
547{
548 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700549 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700550 u32 rd_bitmap = card->mp_rd_bitmap;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700551
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700552 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700553
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700554 if (card->supports_sdio_new_mode) {
555 if (!(rd_bitmap & reg->data_port_mask))
556 return -1;
557 } else {
558 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
559 return -1;
560 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700561
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700562 if ((card->has_control_mask) &&
563 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700564 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700565 *port = CTRL_PORT;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700566 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700567 *port, card->mp_rd_bitmap);
Amitkumar Karware6a520302013-05-17 17:53:56 -0700568 return 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700569 }
Amitkumar Karware6a520302013-05-17 17:53:56 -0700570
571 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
572 return -1;
573
574 /* We are now handling the SDIO data ports */
575 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
576 *port = card->curr_rd_port;
577
578 if (++card->curr_rd_port == card->max_ports)
579 card->curr_rd_port = reg->start_rd_port;
580
581 dev_dbg(adapter->dev,
582 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
583 *port, rd_bitmap, card->mp_rd_bitmap);
584
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700585 return 0;
586}
587
588/*
589 * This function gets the write port for data.
590 *
591 * The current write port is returned if available and the value is
592 * increased (provided it does not reach the maximum limit, in which
593 * case it is reset to 1)
594 */
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -0700595static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700596{
597 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700598 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700599 u32 wr_bitmap = card->mp_wr_bitmap;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700600
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700601 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700602
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700603 if (card->supports_sdio_new_mode &&
604 !(wr_bitmap & reg->data_port_mask)) {
605 adapter->data_sent = true;
606 return -EBUSY;
607 } else if (!card->supports_sdio_new_mode &&
608 !(wr_bitmap & card->mp_data_port_mask)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700609 return -1;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700610 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700611
612 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700613 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700614 *port = card->curr_wr_port;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700615 if (((card->supports_sdio_new_mode) &&
616 (++card->curr_wr_port == card->max_ports)) ||
617 ((!card->supports_sdio_new_mode) &&
618 (++card->curr_wr_port == card->mp_end_port)))
619 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700620 } else {
621 adapter->data_sent = true;
622 return -EBUSY;
623 }
624
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700625 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700626 dev_err(adapter->dev,
627 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700628 *port, card->curr_wr_port, wr_bitmap,
629 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700630 return -1;
631 }
632
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700633 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700634 *port, wr_bitmap, card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700635
636 return 0;
637}
638
639/*
640 * This function polls the card status.
641 */
642static int
643mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
644{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700645 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700646 u32 tries;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700647 u8 cs;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700648
649 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700650 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700651 break;
652 else if ((cs & bits) == bits)
653 return 0;
654
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700655 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700656 }
657
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700658 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
659
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700660 return -1;
661}
662
663/*
664 * This function reads the firmware status.
665 */
666static int
667mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
668{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700669 struct sdio_mmc_card *card = adapter->card;
670 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700671 u8 fws0, fws1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700672
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700673 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700674 return -1;
675
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700676 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700677 return -1;
678
679 *dat = (u16) ((fws1 << 8) | fws0);
680
681 return 0;
682}
683
684/*
685 * This function disables the host interrupt.
686 *
687 * The host interrupt mask is read, the disable bit is reset and
688 * written back to the card host interrupt mask register.
689 */
Daniel Drake232fde02013-07-13 10:57:10 -0400690static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700691{
Daniel Drake232fde02013-07-13 10:57:10 -0400692 struct sdio_mmc_card *card = adapter->card;
693 struct sdio_func *func = card->func;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700694
Daniel Drake232fde02013-07-13 10:57:10 -0400695 sdio_claim_host(func);
696 mwifiex_write_reg_locked(func, HOST_INT_MASK_REG, 0);
697 sdio_release_irq(func);
698 sdio_release_host(func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700699}
700
701/*
Amitkumar Karwar2cdf3592013-07-22 19:17:56 -0700702 * This function reads the interrupt status from card.
703 */
704static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
705{
706 struct sdio_mmc_card *card = adapter->card;
707 u8 sdio_ireg;
708 unsigned long flags;
709
710 if (mwifiex_read_data_sync(adapter, card->mp_regs,
711 card->reg->max_mp_regs,
712 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
713 dev_err(adapter->dev, "read mp_regs failed\n");
714 return;
715 }
716
717 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
718 if (sdio_ireg) {
719 /*
720 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
721 * For SDIO new mode CMD port interrupts
722 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
723 * UP_LD_CMD_PORT_HOST_INT_STATUS
724 * Clear the interrupt status register
725 */
726 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
727 spin_lock_irqsave(&adapter->int_lock, flags);
728 adapter->int_status |= sdio_ireg;
729 spin_unlock_irqrestore(&adapter->int_lock, flags);
730 }
731}
732
733/*
734 * SDIO interrupt handler.
735 *
736 * This function reads the interrupt status from firmware and handles
737 * the interrupt in current thread (ksdioirqd) right away.
738 */
739static void
740mwifiex_sdio_interrupt(struct sdio_func *func)
741{
742 struct mwifiex_adapter *adapter;
743 struct sdio_mmc_card *card;
744
745 card = sdio_get_drvdata(func);
746 if (!card || !card->adapter) {
747 pr_debug("int: func=%p card=%p adapter=%p\n",
748 func, card, card ? card->adapter : NULL);
749 return;
750 }
751 adapter = card->adapter;
752
753 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
754 adapter->ps_state = PS_STATE_AWAKE;
755
756 mwifiex_interrupt_status(adapter);
757 mwifiex_main_process(adapter);
758}
759
760/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700761 * This function enables the host interrupt.
762 *
763 * The host interrupt enable mask is written to the card
764 * host interrupt mask register.
765 */
766static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
767{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700768 struct sdio_mmc_card *card = adapter->card;
Daniel Drake232fde02013-07-13 10:57:10 -0400769 struct sdio_func *func = card->func;
770 int ret;
771
772 sdio_claim_host(func);
773
774 /* Request the SDIO IRQ */
775 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
776 if (ret) {
777 dev_err(adapter->dev, "claim irq failed: ret=%d\n", ret);
778 goto out;
779 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700780
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700781 /* Simply write the mask to the register */
Daniel Drake232fde02013-07-13 10:57:10 -0400782 ret = mwifiex_write_reg_locked(func, HOST_INT_MASK_REG,
783 card->reg->host_int_enable);
784 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700785 dev_err(adapter->dev, "enable host interrupt failed\n");
Daniel Drake232fde02013-07-13 10:57:10 -0400786 sdio_release_irq(func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700787 }
Daniel Drake232fde02013-07-13 10:57:10 -0400788
789out:
790 sdio_release_host(func);
791 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700792}
793
794/*
795 * This function sends a data buffer to the card.
796 */
797static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
798 u32 *type, u8 *buffer,
799 u32 npayload, u32 ioport)
800{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700801 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700802 u32 nb;
803
804 if (!buffer) {
805 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
806 return -1;
807 }
808
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700809 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700810
811 if (ret) {
812 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700813 ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700814 return -1;
815 }
816
817 nb = le16_to_cpu(*(__le16 *) (buffer));
818 if (nb > npayload) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700819 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
820 __func__, nb, npayload);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700821 return -1;
822 }
823
824 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
825
826 return ret;
827}
828
829/*
830 * This function downloads the firmware to the card.
831 *
832 * Firmware is downloaded to the card in blocks. Every block download
833 * is tested for CRC errors, and retried a number of times before
834 * returning failure.
835 */
836static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
837 struct mwifiex_fw_image *fw)
838{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700839 struct sdio_mmc_card *card = adapter->card;
840 const struct mwifiex_sdio_card_reg *reg = card->reg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700841 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700842 u8 *firmware = fw->fw_buf;
843 u32 firmware_len = fw->fw_len;
844 u32 offset = 0;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700845 u8 base0, base1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700846 u8 *fwbuf;
847 u16 len = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700848 u32 txlen, tx_blocks = 0, tries;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700849 u32 i = 0;
850
851 if (!firmware_len) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700852 dev_err(adapter->dev,
853 "firmware image not found! Terminating download\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700854 return -1;
855 }
856
857 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700858 firmware_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700859
860 /* Assume that the allocated buffer is 8-byte aligned */
861 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
Joe Perches0d2e7a52013-02-03 17:28:14 +0000862 if (!fwbuf)
Christoph Fritzb53575e2011-05-08 22:50:09 +0200863 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700864
865 /* Perform firmware data transfer */
866 do {
867 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
868 bits */
869 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
870 DN_LD_CARD_RDY);
871 if (ret) {
872 dev_err(adapter->dev, "FW download with helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700873 " poll status timeout @ %d\n", offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700874 goto done;
875 }
876
877 /* More data? */
878 if (offset >= firmware_len)
879 break;
880
881 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700882 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700883 &base0);
884 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700885 dev_err(adapter->dev,
886 "dev BASE0 register read failed: "
887 "base0=%#04X(%d). Terminating dnld\n",
888 base0, base0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700889 goto done;
890 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700891 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700892 &base1);
893 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700894 dev_err(adapter->dev,
895 "dev BASE1 register read failed: "
896 "base1=%#04X(%d). Terminating dnld\n",
897 base1, base1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700898 goto done;
899 }
900 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
901
902 if (len)
903 break;
904
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700905 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700906 }
907
908 if (!len) {
909 break;
910 } else if (len > MWIFIEX_UPLD_SIZE) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700911 dev_err(adapter->dev,
912 "FW dnld failed @ %d, invalid length %d\n",
913 offset, len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700914 ret = -1;
915 goto done;
916 }
917
918 txlen = len;
919
920 if (len & BIT(0)) {
921 i++;
922 if (i > MAX_WRITE_IOMEM_RETRY) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700923 dev_err(adapter->dev,
924 "FW dnld failed @ %d, over max retry\n",
925 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700926 ret = -1;
927 goto done;
928 }
929 dev_err(adapter->dev, "CRC indicated by the helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700930 " len = 0x%04X, txlen = %d\n", len, txlen);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700931 len &= ~BIT(0);
932 /* Setting this to 0 to resend from same offset */
933 txlen = 0;
934 } else {
935 i = 0;
936
937 /* Set blocksize to transfer - checking for last
938 block */
939 if (firmware_len - offset < txlen)
940 txlen = firmware_len - offset;
941
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700942 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
943 / MWIFIEX_SDIO_BLOCK_SIZE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700944
945 /* Copy payload to buffer */
946 memmove(fwbuf, &firmware[offset], txlen);
947 }
948
949 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
950 MWIFIEX_SDIO_BLOCK_SIZE,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700951 adapter->ioport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700952 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700953 dev_err(adapter->dev,
954 "FW download, write iomem (%d) failed @ %d\n",
955 i, offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700956 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
957 dev_err(adapter->dev, "write CFG reg failed\n");
958
959 ret = -1;
960 goto done;
961 }
962
963 offset += txlen;
964 } while (true);
965
966 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700967 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700968
969 ret = 0;
970done:
971 kfree(fwbuf);
972 return ret;
973}
974
975/*
976 * This function checks the firmware status in card.
977 *
978 * The winner interface is also determined by this function.
979 */
980static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700981 u32 poll_num)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700982{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700983 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700984 int ret = 0;
985 u16 firmware_stat;
986 u32 tries;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700987 u8 winner_status;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700988
989 /* Wait for firmware initialization event */
990 for (tries = 0; tries < poll_num; tries++) {
991 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
992 if (ret)
993 continue;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700994 if (firmware_stat == FIRMWARE_READY_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700995 ret = 0;
996 break;
997 } else {
Amitkumar Karwara76b20e2013-07-22 19:17:53 -0700998 msleep(100);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700999 ret = -1;
1000 }
1001 }
1002
Amitkumar Karward930fae2011-10-11 17:41:21 -07001003 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001004 if (mwifiex_read_reg
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001005 (adapter, card->reg->status_reg_0, &winner_status))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001006 winner_status = 0;
1007
1008 if (winner_status)
Amitkumar Karward930fae2011-10-11 17:41:21 -07001009 adapter->winner = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001010 else
Amitkumar Karward930fae2011-10-11 17:41:21 -07001011 adapter->winner = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001012 }
1013 return ret;
1014}
1015
1016/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001017 * This function decodes a received packet.
1018 *
1019 * Based on the type, the packet is treated as either a data, or
1020 * a command response, or an event, and the correct handler
1021 * function is invoked.
1022 */
1023static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1024 struct sk_buff *skb, u32 upld_typ)
1025{
1026 u8 *cmd_buf;
Avinash Patild03b4aa2013-11-05 15:01:44 -08001027 __le16 *curr_ptr = (__le16 *)skb->data;
1028 u16 pkt_len = le16_to_cpu(*curr_ptr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001029
Avinash Patild03b4aa2013-11-05 15:01:44 -08001030 skb_trim(skb, pkt_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001031 skb_pull(skb, INTF_HEADER_LEN);
1032
1033 switch (upld_typ) {
1034 case MWIFIEX_TYPE_DATA:
1035 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1036 mwifiex_handle_rx_packet(adapter, skb);
1037 break;
1038
1039 case MWIFIEX_TYPE_CMD:
1040 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1041 /* take care of curr_cmd = NULL case */
1042 if (!adapter->curr_cmd) {
1043 cmd_buf = adapter->upld_buf;
1044
1045 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1046 mwifiex_process_sleep_confirm_resp(adapter,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001047 skb->data,
1048 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001049
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001050 memcpy(cmd_buf, skb->data,
1051 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1052 skb->len));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001053
1054 dev_kfree_skb_any(skb);
1055 } else {
1056 adapter->cmd_resp_received = true;
1057 adapter->curr_cmd->resp_skb = skb;
1058 }
1059 break;
1060
1061 case MWIFIEX_TYPE_EVENT:
1062 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
Tobias Waldekranzcfcd9262013-08-26 09:18:06 +02001063 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001064
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001065 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
Amitkumar Karware80c81d2012-06-20 20:21:12 -07001066 memcpy(adapter->event_body,
1067 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1068 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001069
1070 /* event cause has been saved to adapter->event_cause */
1071 adapter->event_received = true;
1072 adapter->event_skb = skb;
1073
1074 break;
1075
1076 default:
1077 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1078 dev_kfree_skb_any(skb);
1079 break;
1080 }
1081
1082 return 0;
1083}
1084
1085/*
1086 * This function transfers received packets from card to driver, performing
1087 * aggregation if required.
1088 *
1089 * For data received on control port, or if aggregation is disabled, the
1090 * received buffers are uploaded as separate packets. However, if aggregation
1091 * is enabled and required, the buffers are copied onto an aggregation buffer,
1092 * provided there is space left, processed and finally uploaded.
1093 */
1094static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1095 struct sk_buff *skb, u8 port)
1096{
1097 struct sdio_mmc_card *card = adapter->card;
1098 s32 f_do_rx_aggr = 0;
1099 s32 f_do_rx_cur = 0;
1100 s32 f_aggr_cur = 0;
1101 struct sk_buff *skb_deaggr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001102 u32 pind;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001103 u32 pkt_len, pkt_type, mport;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001104 u8 *curr_ptr;
1105 u32 rx_len = skb->len;
1106
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001107 if ((card->has_control_mask) && (port == CTRL_PORT)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001108 /* Read the command Resp without aggr */
1109 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001110 "response\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001111
1112 f_do_rx_cur = 1;
1113 goto rx_curr_single;
1114 }
1115
1116 if (!card->mpa_rx.enabled) {
1117 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001118 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001119
1120 f_do_rx_cur = 1;
1121 goto rx_curr_single;
1122 }
1123
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001124 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1125 card->reg->data_port_mask)) ||
1126 (card->has_control_mask && (card->mp_rd_bitmap &
1127 (~((u32) CTRL_PORT_MASK))))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001128 /* Some more data RX pending */
1129 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1130
1131 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1132 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1133 f_aggr_cur = 1;
1134 } else {
1135 /* No room in Aggr buf, do rx aggr now */
1136 f_do_rx_aggr = 1;
1137 f_do_rx_cur = 1;
1138 }
1139 } else {
1140 /* Rx aggr not in progress */
1141 f_aggr_cur = 1;
1142 }
1143
1144 } else {
1145 /* No more data RX pending */
1146 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1147
1148 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1149 f_do_rx_aggr = 1;
1150 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1151 f_aggr_cur = 1;
1152 else
1153 /* No room in Aggr buf, do rx aggr now */
1154 f_do_rx_cur = 1;
1155 } else {
1156 f_do_rx_cur = 1;
1157 }
1158 }
1159
1160 if (f_aggr_cur) {
1161 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1162 /* Curr pkt can be aggregated */
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001163 mp_rx_aggr_setup(card, skb, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001164
1165 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001166 mp_rx_aggr_port_limit_reached(card)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001167 dev_dbg(adapter->dev, "info: %s: aggregated packet "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001168 "limit reached\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001169 /* No more pkts allowed in Aggr buf, rx it */
1170 f_do_rx_aggr = 1;
1171 }
1172 }
1173
1174 if (f_do_rx_aggr) {
1175 /* do aggr RX now */
1176 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001177 card->mpa_rx.pkt_cnt);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001178
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001179 if (card->supports_sdio_new_mode) {
1180 int i;
1181 u32 port_count;
1182
1183 for (i = 0, port_count = 0; i < card->max_ports; i++)
1184 if (card->mpa_rx.ports & BIT(i))
1185 port_count++;
1186
1187 /* Reading data from "start_port + 0" to "start_port +
1188 * port_count -1", so decrease the count by 1
1189 */
1190 port_count--;
1191 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1192 (port_count << 8)) + card->mpa_rx.start_port;
1193 } else {
1194 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1195 (card->mpa_rx.ports << 4)) +
1196 card->mpa_rx.start_port;
1197 }
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001198
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001199 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001200 card->mpa_rx.buf_len, mport, 1))
Avinash Patil17a60b42011-12-08 20:41:04 -08001201 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001202
1203 curr_ptr = card->mpa_rx.buf;
1204
1205 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1206
1207 /* get curr PKT len & type */
Tobias Waldekranzcfcd9262013-08-26 09:18:06 +02001208 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1209 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001210
1211 /* copy pkt to deaggr buf */
1212 skb_deaggr = card->mpa_rx.skb_arr[pind];
1213
1214 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1215 card->mpa_rx.len_arr[pind])) {
1216
1217 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1218
1219 skb_trim(skb_deaggr, pkt_len);
1220
1221 /* Process de-aggr packet */
1222 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1223 pkt_type);
1224 } else {
1225 dev_err(adapter->dev, "wrong aggr pkt:"
1226 " type=%d len=%d max_len=%d\n",
1227 pkt_type, pkt_len,
1228 card->mpa_rx.len_arr[pind]);
1229 dev_kfree_skb_any(skb_deaggr);
1230 }
1231 curr_ptr += card->mpa_rx.len_arr[pind];
1232 }
1233 MP_RX_AGGR_BUF_RESET(card);
1234 }
1235
1236rx_curr_single:
1237 if (f_do_rx_cur) {
1238 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1239 port, rx_len);
1240
1241 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1242 skb->data, skb->len,
1243 adapter->ioport + port))
Avinash Patil17a60b42011-12-08 20:41:04 -08001244 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001245
1246 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1247 }
1248
1249 return 0;
Avinash Patil17a60b42011-12-08 20:41:04 -08001250
1251error:
1252 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1253 /* Multiport-aggregation transfer failed - cleanup */
1254 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1255 /* copy pkt to deaggr buf */
1256 skb_deaggr = card->mpa_rx.skb_arr[pind];
1257 dev_kfree_skb_any(skb_deaggr);
1258 }
1259 MP_RX_AGGR_BUF_RESET(card);
1260 }
1261
1262 if (f_do_rx_cur)
1263 /* Single transfer pending. Free curr buff also */
1264 dev_kfree_skb_any(skb);
1265
1266 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001267}
1268
1269/*
1270 * This function checks the current interrupt status.
1271 *
1272 * The following interrupts are checked and handled by this function -
1273 * - Data sent
1274 * - Command sent
1275 * - Packets received
1276 *
1277 * Since the firmware does not generate download ready interrupt if the
1278 * port updated is command port only, command sent interrupt checking
1279 * should be done manually, and for every SDIO interrupt.
1280 *
1281 * In case of Rx packets received, the packets are uploaded from card to
1282 * host and processed accordingly.
1283 */
1284static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1285{
1286 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001287 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001288 int ret = 0;
1289 u8 sdio_ireg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001290 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001291 u8 port = CTRL_PORT;
1292 u32 len_reg_l, len_reg_u;
1293 u32 rx_blocks;
1294 u16 rx_len;
1295 unsigned long flags;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001296 u32 bitmap;
1297 u8 cr;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001298
1299 spin_lock_irqsave(&adapter->int_lock, flags);
1300 sdio_ireg = adapter->int_status;
1301 adapter->int_status = 0;
1302 spin_unlock_irqrestore(&adapter->int_lock, flags);
1303
1304 if (!sdio_ireg)
1305 return ret;
1306
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001307 /* Following interrupt is only for SDIO new mode */
1308 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1309 adapter->cmd_sent = false;
1310
1311 /* Following interrupt is only for SDIO new mode */
1312 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1313 u32 pkt_type;
1314
1315 /* read the len of control packet */
1316 rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1317 rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1318 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1319 if (rx_len <= INTF_HEADER_LEN ||
1320 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1321 MWIFIEX_RX_DATA_BUF_SIZE)
1322 return -1;
1323 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1324
1325 skb = dev_alloc_skb(rx_len);
1326 if (!skb)
1327 return -1;
1328
1329 skb_put(skb, rx_len);
1330
1331 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1332 skb->len, adapter->ioport |
1333 CMD_PORT_SLCT)) {
1334 dev_err(adapter->dev,
1335 "%s: failed to card_to_host", __func__);
1336 dev_kfree_skb_any(skb);
1337 goto term_cmd;
1338 }
1339
1340 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1341 (pkt_type != MWIFIEX_TYPE_EVENT))
1342 dev_err(adapter->dev,
1343 "%s:Received wrong packet on cmd port",
1344 __func__);
1345
1346 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1347 }
1348
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001349 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001350 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1351 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1352 if (card->supports_sdio_new_mode) {
1353 bitmap |=
1354 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1355 bitmap |=
1356 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1357 }
1358 card->mp_wr_bitmap = bitmap;
1359
1360 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001361 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001362 if (adapter->data_sent &&
1363 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1364 dev_dbg(adapter->dev,
1365 "info: <--- Tx DONE Interrupt --->\n");
1366 adapter->data_sent = false;
1367 }
1368 }
1369
1370 /* As firmware will not generate download ready interrupt if the port
1371 updated is command port only, cmd_sent should be done for any SDIO
1372 interrupt. */
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001373 if (card->has_control_mask && adapter->cmd_sent) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001374 /* Check if firmware has attach buffer at command port and
1375 update just that in wr_bit_map. */
1376 card->mp_wr_bitmap |=
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001377 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001378 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1379 adapter->cmd_sent = false;
1380 }
1381
1382 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001383 adapter->cmd_sent, adapter->data_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001384 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001385 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1386 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1387 if (card->supports_sdio_new_mode) {
1388 bitmap |=
1389 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1390 bitmap |=
1391 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1392 }
1393 card->mp_rd_bitmap = bitmap;
1394 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001395 card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001396
1397 while (true) {
1398 ret = mwifiex_get_rd_port(adapter, &port);
1399 if (ret) {
1400 dev_dbg(adapter->dev,
1401 "info: no more rd_port available\n");
1402 break;
1403 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001404 len_reg_l = reg->rd_len_p0_l + (port << 1);
1405 len_reg_u = reg->rd_len_p0_u + (port << 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001406 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1407 rx_len |= (u16) card->mp_regs[len_reg_l];
1408 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001409 port, rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001410 rx_blocks =
1411 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1412 1) / MWIFIEX_SDIO_BLOCK_SIZE;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001413 if (rx_len <= INTF_HEADER_LEN ||
1414 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1415 MWIFIEX_RX_DATA_BUF_SIZE) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001416 dev_err(adapter->dev, "invalid rx_len=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001417 rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001418 return -1;
1419 }
1420 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1421
1422 skb = dev_alloc_skb(rx_len);
1423
1424 if (!skb) {
1425 dev_err(adapter->dev, "%s: failed to alloc skb",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001426 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001427 return -1;
1428 }
1429
1430 skb_put(skb, rx_len);
1431
1432 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001433 rx_len, skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001434
1435 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1436 port)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001437 dev_err(adapter->dev, "card_to_host_mpa failed:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001438 " int status=%#x\n", sdio_ireg);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001439 goto term_cmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001440 }
1441 }
1442 }
1443
1444 return 0;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001445
1446term_cmd:
1447 /* terminate cmd */
1448 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1449 dev_err(adapter->dev, "read CFG reg failed\n");
1450 else
1451 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1452
1453 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1454 dev_err(adapter->dev, "write CFG reg failed\n");
1455 else
1456 dev_dbg(adapter->dev, "info: write success\n");
1457
1458 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1459 dev_err(adapter->dev, "read CFG reg failed\n");
1460 else
1461 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1462
1463 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001464}
1465
1466/*
1467 * This function aggregates transmission buffers in driver and downloads
1468 * the aggregated packet to card.
1469 *
1470 * The individual packets are aggregated by copying into an aggregation
1471 * buffer and then downloaded to the card. Previous unsent packets in the
1472 * aggregation buffer are pre-copied first before new packets are added.
1473 * Aggregation is done till there is space left in the aggregation buffer,
1474 * or till new packets are available.
1475 *
1476 * The function will only download the packet to the card when aggregation
1477 * stops, otherwise it will just aggregate the packet in aggregation buffer
1478 * and return.
1479 */
1480static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001481 u8 *payload, u32 pkt_len, u32 port,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001482 u32 next_pkt_len)
1483{
1484 struct sdio_mmc_card *card = adapter->card;
1485 int ret = 0;
1486 s32 f_send_aggr_buf = 0;
1487 s32 f_send_cur_buf = 0;
1488 s32 f_precopy_cur_buf = 0;
1489 s32 f_postcopy_cur_buf = 0;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001490 u32 mport;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001491
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001492 if (!card->mpa_tx.enabled ||
1493 (card->has_control_mask && (port == CTRL_PORT)) ||
1494 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001495 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001496 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001497
1498 f_send_cur_buf = 1;
1499 goto tx_curr_single;
1500 }
1501
1502 if (next_pkt_len) {
1503 /* More pkt in TX queue */
1504 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001505 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001506
1507 if (MP_TX_AGGR_IN_PROGRESS(card)) {
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001508 if (!mp_tx_aggr_port_limit_reached(card) &&
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001509 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1510 f_precopy_cur_buf = 1;
1511
1512 if (!(card->mp_wr_bitmap &
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001513 (1 << card->curr_wr_port)) ||
1514 !MP_TX_AGGR_BUF_HAS_ROOM(
1515 card, pkt_len + next_pkt_len))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001516 f_send_aggr_buf = 1;
1517 } else {
1518 /* No room in Aggr buf, send it */
1519 f_send_aggr_buf = 1;
1520
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001521 if (mp_tx_aggr_port_limit_reached(card) ||
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001522 !(card->mp_wr_bitmap &
1523 (1 << card->curr_wr_port)))
1524 f_send_cur_buf = 1;
1525 else
1526 f_postcopy_cur_buf = 1;
1527 }
1528 } else {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001529 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1530 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001531 f_precopy_cur_buf = 1;
1532 else
1533 f_send_cur_buf = 1;
1534 }
1535 } else {
1536 /* Last pkt in TX queue */
1537 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001538 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001539
1540 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1541 /* some packs in Aggr buf already */
1542 f_send_aggr_buf = 1;
1543
1544 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1545 f_precopy_cur_buf = 1;
1546 else
1547 /* No room in Aggr buf, send it */
1548 f_send_cur_buf = 1;
1549 } else {
1550 f_send_cur_buf = 1;
1551 }
1552 }
1553
1554 if (f_precopy_cur_buf) {
1555 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001556 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001557 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1558
1559 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001560 mp_tx_aggr_port_limit_reached(card))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001561 /* No more pkts allowed in Aggr buf, send it */
1562 f_send_aggr_buf = 1;
1563 }
1564
1565 if (f_send_aggr_buf) {
1566 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001567 __func__,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001568 card->mpa_tx.start_port, card->mpa_tx.ports);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001569 if (card->supports_sdio_new_mode) {
1570 u32 port_count;
1571 int i;
1572
1573 for (i = 0, port_count = 0; i < card->max_ports; i++)
1574 if (card->mpa_tx.ports & BIT(i))
1575 port_count++;
1576
1577 /* Writing data from "start_port + 0" to "start_port +
1578 * port_count -1", so decrease the count by 1
1579 */
1580 port_count--;
1581 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1582 (port_count << 8)) + card->mpa_tx.start_port;
1583 } else {
1584 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1585 (card->mpa_tx.ports << 4)) +
1586 card->mpa_tx.start_port;
1587 }
1588
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001589 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001590 card->mpa_tx.buf_len, mport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001591
1592 MP_TX_AGGR_BUF_RESET(card);
1593 }
1594
1595tx_curr_single:
1596 if (f_send_cur_buf) {
1597 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001598 __func__, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001599 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1600 adapter->ioport + port);
1601 }
1602
1603 if (f_postcopy_cur_buf) {
1604 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001605 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001606 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1607 }
1608
1609 return ret;
1610}
1611
1612/*
1613 * This function downloads data from driver to card.
1614 *
1615 * Both commands and data packets are transferred to the card by this
1616 * function.
1617 *
1618 * This function adds the SDIO specific header to the front of the buffer
1619 * before transferring. The header contains the length of the packet and
1620 * the type. The firmware handles the packets based upon this set type.
1621 */
1622static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001623 u8 type, struct sk_buff *skb,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001624 struct mwifiex_tx_param *tx_param)
1625{
1626 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001627 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001628 u32 buf_block_len;
1629 u32 blk_size;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001630 u32 port = CTRL_PORT;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001631 u8 *payload = (u8 *)skb->data;
1632 u32 pkt_len = skb->len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001633
1634 /* Allocate buffer and copy payload */
1635 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1636 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
Tomasz Moń83e612f2013-07-23 07:42:49 +02001637 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1638 *(__le16 *)&payload[2] = cpu_to_le16(type);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001639
1640 /*
1641 * This is SDIO specific header
1642 * u16 length,
1643 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1644 * MWIFIEX_TYPE_EVENT = 3)
1645 */
1646 if (type == MWIFIEX_TYPE_DATA) {
1647 ret = mwifiex_get_wr_port_data(adapter, &port);
1648 if (ret) {
1649 dev_err(adapter->dev, "%s: no wr_port available\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001650 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001651 return ret;
1652 }
1653 } else {
1654 adapter->cmd_sent = true;
1655 /* Type must be MWIFIEX_TYPE_CMD */
1656
1657 if (pkt_len <= INTF_HEADER_LEN ||
1658 pkt_len > MWIFIEX_UPLD_SIZE)
1659 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001660 __func__, payload, pkt_len);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001661
1662 if (card->supports_sdio_new_mode)
1663 port = CMD_PORT_SLCT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001664 }
1665
1666 /* Transfer data to card */
1667 pkt_len = buf_block_len * blk_size;
1668
1669 if (tx_param)
1670 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001671 port, tx_param->next_pkt_len
1672 );
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001673 else
1674 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001675 port, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001676
1677 if (ret) {
1678 if (type == MWIFIEX_TYPE_CMD)
1679 adapter->cmd_sent = false;
1680 if (type == MWIFIEX_TYPE_DATA)
1681 adapter->data_sent = false;
1682 } else {
1683 if (type == MWIFIEX_TYPE_DATA) {
1684 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1685 adapter->data_sent = true;
1686 else
1687 adapter->data_sent = false;
1688 }
1689 }
1690
1691 return ret;
1692}
1693
1694/*
1695 * This function allocates the MPA Tx and Rx buffers.
1696 */
1697static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1698 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1699{
1700 struct sdio_mmc_card *card = adapter->card;
1701 int ret = 0;
1702
1703 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1704 if (!card->mpa_tx.buf) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001705 ret = -1;
1706 goto error;
1707 }
1708
1709 card->mpa_tx.buf_size = mpa_tx_buf_size;
1710
1711 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1712 if (!card->mpa_rx.buf) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001713 ret = -1;
1714 goto error;
1715 }
1716
1717 card->mpa_rx.buf_size = mpa_rx_buf_size;
1718
1719error:
1720 if (ret) {
1721 kfree(card->mpa_tx.buf);
1722 kfree(card->mpa_rx.buf);
1723 }
1724
1725 return ret;
1726}
1727
1728/*
1729 * This function unregisters the SDIO device.
1730 *
1731 * The SDIO IRQ is released, the function is disabled and driver
1732 * data is set to null.
1733 */
1734static void
1735mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1736{
1737 struct sdio_mmc_card *card = adapter->card;
1738
1739 if (adapter->card) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001740 sdio_claim_host(card->func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001741 sdio_disable_func(card->func);
1742 sdio_release_host(card->func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001743 }
1744}
1745
1746/*
1747 * This function registers the SDIO device.
1748 *
1749 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1750 */
1751static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1752{
Daniel Drake232fde02013-07-13 10:57:10 -04001753 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001754 struct sdio_mmc_card *card = adapter->card;
1755 struct sdio_func *func = card->func;
1756
1757 /* save adapter pointer in card */
1758 card->adapter = adapter;
Amitkumar Karwar828cf222014-02-27 19:35:13 -08001759 adapter->tx_buf_size = card->tx_buf_size;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001760
1761 sdio_claim_host(func);
1762
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001763 /* Set block size */
1764 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
Daniel Drake232fde02013-07-13 10:57:10 -04001765 sdio_release_host(func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001766 if (ret) {
1767 pr_err("cannot set SDIO block size\n");
Daniel Drake232fde02013-07-13 10:57:10 -04001768 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001769 }
1770
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001771
1772 adapter->dev = &func->dev;
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001773
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001774 strcpy(adapter->fw_name, card->firmware);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001775
1776 return 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001777}
1778
1779/*
1780 * This function initializes the SDIO driver.
1781 *
1782 * The following initializations steps are followed -
1783 * - Read the Host interrupt status register to acknowledge
1784 * the first interrupt got from bootloader
1785 * - Disable host interrupt mask register
1786 * - Get SDIO port
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001787 * - Initialize SDIO variables in card
1788 * - Allocate MP registers
1789 * - Allocate MPA Tx and Rx buffers
1790 */
1791static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1792{
1793 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001794 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001795 int ret;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -07001796 u8 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001797
Amitkumar Karwar3c59e322013-11-14 19:10:41 -08001798 sdio_set_drvdata(card->func, card);
1799
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001800 /*
1801 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1802 * from the bootloader. If we don't do this we get a interrupt
1803 * as soon as we register the irq.
1804 */
1805 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1806
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001807 /* Get SDIO ioport */
1808 mwifiex_init_sdio_ioport(adapter);
1809
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001810 /* Initialize SDIO variables in card */
1811 card->mp_rd_bitmap = 0;
1812 card->mp_wr_bitmap = 0;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001813 card->curr_rd_port = reg->start_rd_port;
1814 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001815
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001816 card->mp_data_port_mask = reg->data_port_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001817
1818 card->mpa_tx.buf_len = 0;
1819 card->mpa_tx.pkt_cnt = 0;
1820 card->mpa_tx.start_port = 0;
1821
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001822 card->mpa_tx.enabled = 1;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001823 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001824
1825 card->mpa_rx.buf_len = 0;
1826 card->mpa_rx.pkt_cnt = 0;
1827 card->mpa_rx.start_port = 0;
1828
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001829 card->mpa_rx.enabled = 1;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001830 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001831
1832 /* Allocate buffers for SDIO MP-A */
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001833 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
Joe Perches0d2e7a52013-02-03 17:28:14 +00001834 if (!card->mp_regs)
Christoph Fritzb53575e2011-05-08 22:50:09 +02001835 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001836
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001837 /* Allocate skb pointer buffers */
1838 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1839 card->mp_agg_pkt_limit, GFP_KERNEL);
1840 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1841 card->mp_agg_pkt_limit, GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001842 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1843 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1844 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1845 if (ret) {
1846 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1847 kfree(card->mp_regs);
1848 return -1;
1849 }
1850
1851 return ret;
1852}
1853
1854/*
1855 * This function resets the MPA Tx and Rx buffers.
1856 */
1857static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1858{
1859 struct sdio_mmc_card *card = adapter->card;
1860
1861 MP_TX_AGGR_BUF_RESET(card);
1862 MP_RX_AGGR_BUF_RESET(card);
1863}
1864
1865/*
1866 * This function cleans up the allocated card buffers.
1867 *
1868 * The following are freed by this function -
1869 * - MP registers
1870 * - MPA Tx buffer
1871 * - MPA Rx buffer
1872 */
1873static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1874{
1875 struct sdio_mmc_card *card = adapter->card;
1876
1877 kfree(card->mp_regs);
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001878 kfree(card->mpa_rx.skb_arr);
1879 kfree(card->mpa_rx.len_arr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001880 kfree(card->mpa_tx.buf);
1881 kfree(card->mpa_rx.buf);
Amitkumar Karwar3c59e322013-11-14 19:10:41 -08001882 sdio_set_drvdata(card->func, NULL);
1883 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001884}
1885
1886/*
1887 * This function updates the MP end port in card.
1888 */
1889static void
1890mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1891{
1892 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001893 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001894 int i;
1895
1896 card->mp_end_port = port;
1897
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001898 card->mp_data_port_mask = reg->data_port_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001899
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001900 if (reg->start_wr_port) {
1901 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1902 card->mp_data_port_mask &=
1903 ~(1 << (card->max_ports - i));
1904 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001905
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001906 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001907
1908 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001909 port, card->mp_data_port_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001910}
1911
Amitkumar Karward31ab352012-11-01 18:44:14 -07001912static struct mmc_host *reset_host;
1913static void sdio_card_reset_worker(struct work_struct *work)
1914{
Tejun Heo7f5855c2012-12-21 17:56:57 -08001915 struct mmc_host *target = reset_host;
1916
Amitkumar Karward31ab352012-11-01 18:44:14 -07001917 /* The actual reset operation must be run outside of driver thread.
1918 * This is because mmc_remove_host() will cause the device to be
1919 * instantly destroyed, and the driver then needs to end its thread,
1920 * leading to a deadlock.
1921 *
1922 * We run it in a totally independent workqueue.
1923 */
1924
1925 pr_err("Resetting card...\n");
Tejun Heo7f5855c2012-12-21 17:56:57 -08001926 mmc_remove_host(target);
Amitkumar Karward31ab352012-11-01 18:44:14 -07001927 /* 20ms delay is based on experiment with sdhci controller */
1928 mdelay(20);
Tejun Heo7f5855c2012-12-21 17:56:57 -08001929 mmc_add_host(target);
Amitkumar Karward31ab352012-11-01 18:44:14 -07001930}
1931static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1932
1933/* This function resets the card */
1934static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1935{
1936 struct sdio_mmc_card *card = adapter->card;
1937
Amitkumar Karward31ab352012-11-01 18:44:14 -07001938 reset_host = card->func->card->host;
1939 schedule_work(&card_reset_work);
1940}
1941
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001942static struct mwifiex_if_ops sdio_ops = {
1943 .init_if = mwifiex_init_sdio,
1944 .cleanup_if = mwifiex_cleanup_sdio,
1945 .check_fw_status = mwifiex_check_fw_status,
1946 .prog_fw = mwifiex_prog_fw_w_helper,
1947 .register_dev = mwifiex_register_dev,
1948 .unregister_dev = mwifiex_unregister_dev,
1949 .enable_int = mwifiex_sdio_enable_host_int,
Daniel Drake232fde02013-07-13 10:57:10 -04001950 .disable_int = mwifiex_sdio_disable_host_int,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001951 .process_int_status = mwifiex_process_int_status,
1952 .host_to_card = mwifiex_sdio_host_to_card,
1953 .wakeup = mwifiex_pm_wakeup_card,
1954 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1955
1956 /* SDIO specific */
1957 .update_mp_end_port = mwifiex_update_mp_end_port,
1958 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001959 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1960 .event_complete = mwifiex_sdio_event_complete,
Amitkumar Karward31ab352012-11-01 18:44:14 -07001961 .card_reset = mwifiex_sdio_card_reset,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001962};
1963
1964/*
1965 * This function initializes the SDIO driver.
1966 *
1967 * This initiates the semaphore and registers the device with
1968 * SDIO bus.
1969 */
1970static int
1971mwifiex_sdio_init_module(void)
1972{
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001973 sema_init(&add_remove_card_sem, 1);
1974
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301975 /* Clear the flag in case user removes the card. */
1976 user_rmmod = 0;
1977
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001978 return sdio_register_driver(&mwifiex_sdio);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001979}
1980
1981/*
1982 * This function cleans up the SDIO driver.
1983 *
1984 * The following major steps are followed for cleanup -
1985 * - Resume the device if its suspended
1986 * - Disconnect the device if connected
1987 * - Shutdown the firmware
1988 * - Unregister the device from SDIO bus.
1989 */
1990static void
1991mwifiex_sdio_cleanup_module(void)
1992{
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301993 if (!down_interruptible(&add_remove_card_sem))
1994 up(&add_remove_card_sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001995
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301996 /* Set the flag as user is removing this module. */
1997 user_rmmod = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001998
Amitkumar Karward31ab352012-11-01 18:44:14 -07001999 cancel_work_sync(&card_reset_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07002000 sdio_unregister_driver(&mwifiex_sdio);
2001}
2002
2003module_init(mwifiex_sdio_init_module);
2004module_exit(mwifiex_sdio_cleanup_module);
2005
2006MODULE_AUTHOR("Marvell International Ltd.");
2007MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2008MODULE_VERSION(SDIO_VERSION);
2009MODULE_LICENSE("GPL v2");
WarheadsSE98e6b9d2012-04-24 15:57:21 -04002010MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08002011MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2012MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07002013MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);