blob: b44a31523461e2c5ba7d768c941fc0934abaacb6 [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 Karwar05889f82013-05-17 17:50:27 -070087 }
88
Bing Zhao5e6e3a92011-03-21 18:00:50 -070089 sdio_claim_host(func);
90 ret = sdio_enable_func(func);
91 sdio_release_host(func);
92
93 if (ret) {
94 pr_err("%s: failed to enable function\n", __func__);
Christoph Fritzb53575e2011-05-08 22:50:09 +020095 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070096 return -EIO;
97 }
98
Amitkumar Karward930fae2011-10-11 17:41:21 -070099 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
100 MWIFIEX_SDIO)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700101 pr_err("%s: add card failed\n", __func__);
102 kfree(card);
103 sdio_claim_host(func);
104 ret = sdio_disable_func(func);
105 sdio_release_host(func);
106 ret = -1;
107 }
108
109 return ret;
110}
111
112/*
Amitkumar Karwar2cdf3592013-07-22 19:17:56 -0700113 * SDIO resume.
114 *
115 * Kernel needs to suspend all functions separately. Therefore all
116 * registered functions must have drivers with suspend and resume
117 * methods. Failing that the kernel simply removes the whole card.
118 *
119 * If already not resumed, this function turns on the traffic and
120 * sends a host sleep cancel request to the firmware.
121 */
122static int mwifiex_sdio_resume(struct device *dev)
123{
124 struct sdio_func *func = dev_to_sdio_func(dev);
125 struct sdio_mmc_card *card;
126 struct mwifiex_adapter *adapter;
127 mmc_pm_flag_t pm_flag = 0;
128
129 if (func) {
130 pm_flag = sdio_get_host_pm_caps(func);
131 card = sdio_get_drvdata(func);
132 if (!card || !card->adapter) {
133 pr_err("resume: invalid card or adapter\n");
134 return 0;
135 }
136 } else {
137 pr_err("resume: sdio_func is not specified\n");
138 return 0;
139 }
140
141 adapter = card->adapter;
142
143 if (!adapter->is_suspended) {
144 dev_warn(adapter->dev, "device already resumed\n");
145 return 0;
146 }
147
148 adapter->is_suspended = false;
149
150 /* Disable Host Sleep */
151 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
152 MWIFIEX_ASYNC_CMD);
153
154 return 0;
155}
156
157/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700158 * SDIO remove.
159 *
160 * This function removes the interface and frees up the card structure.
161 */
162static void
163mwifiex_sdio_remove(struct sdio_func *func)
164{
165 struct sdio_mmc_card *card;
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530166 struct mwifiex_adapter *adapter;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700167 struct mwifiex_private *priv;
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530168 int i;
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
187 for (i = 0; i < adapter->priv_num; i++)
188 if ((GET_BSS_ROLE(adapter->priv[i]) ==
189 MWIFIEX_BSS_ROLE_STA) &&
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700190 adapter->priv[i]->media_connected)
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530191 mwifiex_deauthenticate(adapter->priv[i], NULL);
192
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700193 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
194 mwifiex_disable_auto_ds(priv);
195 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700196 }
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530197
198 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700199}
200
201/*
202 * SDIO suspend.
203 *
204 * Kernel needs to suspend all functions separately. Therefore all
205 * registered functions must have drivers with suspend and resume
206 * methods. Failing that the kernel simply removes the whole card.
207 *
208 * If already not suspended, this function allocates and sends a host
209 * sleep activate request to the firmware and turns off the traffic.
210 */
211static int mwifiex_sdio_suspend(struct device *dev)
212{
213 struct sdio_func *func = dev_to_sdio_func(dev);
214 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700215 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700216 mmc_pm_flag_t pm_flag = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700217 int ret = 0;
218
219 if (func) {
220 pm_flag = sdio_get_host_pm_caps(func);
221 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700222 sdio_func_id(func), pm_flag);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700223 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
224 pr_err("%s: cannot remain alive while host is"
225 " suspended\n", sdio_func_id(func));
226 return -ENOSYS;
227 }
228
229 card = sdio_get_drvdata(func);
230 if (!card || !card->adapter) {
231 pr_err("suspend: invalid card or adapter\n");
232 return 0;
233 }
234 } else {
235 pr_err("suspend: sdio_func is not specified\n");
236 return 0;
237 }
238
239 adapter = card->adapter;
240
241 /* Enable the Host Sleep */
Bing Zhaodd321ac2012-11-15 15:58:48 -0800242 if (!mwifiex_enable_hs(adapter)) {
243 dev_err(adapter->dev, "cmd: failed to suspend\n");
244 return -EFAULT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700245 }
246
Bing Zhaodd321ac2012-11-15 15:58:48 -0800247 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
248 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
249
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700250 /* Indicate device suspended */
251 adapter->is_suspended = true;
252
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700253 return ret;
254}
255
WarheadsSE98e6b9d2012-04-24 15:57:21 -0400256/* Device ID for SD8786 */
257#define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700258/* Device ID for SD8787 */
259#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
Bing Zhaoe3bea1c2011-11-16 20:40:35 -0800260/* Device ID for SD8797 */
261#define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700262/* Device ID for SD8897 */
263#define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700264
265/* WLAN IDs */
266static const struct sdio_device_id mwifiex_ids[] = {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700267 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
268 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
269 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
270 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
271 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
272 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700273 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
274 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700275 {},
276};
277
278MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
279
280static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
281 .suspend = mwifiex_sdio_suspend,
282 .resume = mwifiex_sdio_resume,
283};
284
285static struct sdio_driver mwifiex_sdio = {
286 .name = "mwifiex_sdio",
287 .id_table = mwifiex_ids,
288 .probe = mwifiex_sdio_probe,
289 .remove = mwifiex_sdio_remove,
290 .drv = {
291 .owner = THIS_MODULE,
292 .pm = &mwifiex_sdio_pm_ops,
293 }
294};
295
Daniel Drake232fde02013-07-13 10:57:10 -0400296/* Write data into SDIO card register. Caller claims SDIO device. */
297static int
298mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
299{
300 int ret = -1;
301 sdio_writeb(func, data, reg, &ret);
302 return ret;
303}
304
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700305/*
306 * This function writes data into SDIO card register.
307 */
308static int
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700309mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700310{
311 struct sdio_mmc_card *card = adapter->card;
Daniel Drake232fde02013-07-13 10:57:10 -0400312 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700313
314 sdio_claim_host(card->func);
Daniel Drake232fde02013-07-13 10:57:10 -0400315 ret = mwifiex_write_reg_locked(card->func, reg, data);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700316 sdio_release_host(card->func);
317
318 return ret;
319}
320
321/*
322 * This function reads data from SDIO card register.
323 */
324static int
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700325mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700326{
327 struct sdio_mmc_card *card = adapter->card;
328 int ret = -1;
329 u8 val;
330
331 sdio_claim_host(card->func);
332 val = sdio_readb(card->func, reg, &ret);
333 sdio_release_host(card->func);
334
335 *data = val;
336
337 return ret;
338}
339
340/*
341 * This function writes multiple data into SDIO card memory.
342 *
343 * This does not work in suspended mode.
344 */
345static int
346mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700347 u8 *buffer, u32 pkt_len, u32 port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700348{
349 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800350 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700351 u8 blk_mode =
352 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
353 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
354 u32 blk_cnt =
355 (blk_mode ==
356 BLOCK_MODE) ? (pkt_len /
357 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
358 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
359
360 if (adapter->is_suspended) {
361 dev_err(adapter->dev,
362 "%s: not allowed while suspended\n", __func__);
363 return -1;
364 }
365
366 sdio_claim_host(card->func);
367
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800368 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700369
370 sdio_release_host(card->func);
371
372 return ret;
373}
374
375/*
376 * This function reads multiple data from SDIO card memory.
377 */
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700378static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
379 u32 len, u32 port, u8 claim)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700380{
381 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800382 int ret;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700383 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
384 : BLOCK_MODE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700385 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700386 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
387 : len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700388 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
389
390 if (claim)
391 sdio_claim_host(card->func);
392
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800393 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700394
395 if (claim)
396 sdio_release_host(card->func);
397
398 return ret;
399}
400
401/*
402 * This function wakes up the card.
403 *
404 * A host power up command is written to the card configuration
405 * register to wake up the card.
406 */
407static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
408{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700409 dev_dbg(adapter->dev, "event: wakeup device...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700410
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700411 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700412}
413
414/*
415 * This function is called after the card has woken up.
416 *
417 * The card configuration register is reset.
418 */
419static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
420{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700421 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700422
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700423 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700424}
425
426/*
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700427 * This function is used to initialize IO ports for the
428 * chipsets supporting SDIO new mode eg SD8897.
429 */
430static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
431{
432 u8 reg;
433
434 adapter->ioport = MEM_PORT;
435
436 /* enable sdio new mode */
437 if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, &reg))
438 return -1;
439 if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
440 reg | CMD53_NEW_MODE))
441 return -1;
442
443 /* Configure cmd port and enable reading rx length from the register */
444 if (mwifiex_read_reg(adapter, CMD_CONFIG_0, &reg))
445 return -1;
446 if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
447 return -1;
448
449 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
450 * completed
451 */
452 if (mwifiex_read_reg(adapter, CMD_CONFIG_1, &reg))
453 return -1;
454 if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
455 return -1;
456
457 return 0;
458}
459
460/* This function initializes the IO ports.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700461 *
462 * The following operations are performed -
463 * - Read the IO ports (0, 1 and 2)
464 * - Set host interrupt Reset-To-Read to clear
465 * - Set auto re-enable interrupt
466 */
467static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
468{
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700469 u8 reg;
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700470 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700471
472 adapter->ioport = 0;
473
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700474 if (card->supports_sdio_new_mode) {
475 if (mwifiex_init_sdio_new_mode(adapter))
476 return -1;
477 goto cont;
478 }
479
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700480 /* Read the IO port */
481 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
482 adapter->ioport |= (reg & 0xff);
483 else
484 return -1;
485
486 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
487 adapter->ioport |= ((reg & 0xff) << 8);
488 else
489 return -1;
490
491 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
492 adapter->ioport |= ((reg & 0xff) << 16);
493 else
494 return -1;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700495cont:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700496 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
497
498 /* Set Host interrupt reset to read to clear */
499 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
500 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700501 reg | card->reg->sdio_int_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700502 else
503 return -1;
504
505 /* Dnld/Upld ready set to auto reset */
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700506 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
507 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700508 reg | AUTO_RE_ENABLE_INT);
509 else
510 return -1;
511
512 return 0;
513}
514
515/*
516 * This function sends data to the card.
517 */
518static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
519 u8 *payload, u32 pkt_len, u32 port)
520{
521 u32 i = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700522 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700523
524 do {
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700525 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700526 if (ret) {
527 i++;
528 dev_err(adapter->dev, "host_to_card, write iomem"
529 " (%d) failed: %d\n", i, ret);
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700530 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700531 dev_err(adapter->dev, "write CFG reg failed\n");
532
533 ret = -1;
534 if (i > MAX_WRITE_IOMEM_RETRY)
535 return ret;
536 }
537 } while (ret == -1);
538
539 return ret;
540}
541
542/*
543 * This function gets the read port.
544 *
545 * If control port bit is set in MP read bitmap, the control port
546 * is returned, otherwise the current read port is returned and
547 * the value is increased (provided it does not reach the maximum
548 * limit, in which case it is reset to 1)
549 */
550static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
551{
552 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700553 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700554 u32 rd_bitmap = card->mp_rd_bitmap;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700555
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700556 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700557
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700558 if (card->supports_sdio_new_mode) {
559 if (!(rd_bitmap & reg->data_port_mask))
560 return -1;
561 } else {
562 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
563 return -1;
564 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700565
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700566 if ((card->has_control_mask) &&
567 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700568 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700569 *port = CTRL_PORT;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700570 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700571 *port, card->mp_rd_bitmap);
Amitkumar Karware6a520302013-05-17 17:53:56 -0700572 return 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700573 }
Amitkumar Karware6a520302013-05-17 17:53:56 -0700574
575 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
576 return -1;
577
578 /* We are now handling the SDIO data ports */
579 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
580 *port = card->curr_rd_port;
581
582 if (++card->curr_rd_port == card->max_ports)
583 card->curr_rd_port = reg->start_rd_port;
584
585 dev_dbg(adapter->dev,
586 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
587 *port, rd_bitmap, card->mp_rd_bitmap);
588
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700589 return 0;
590}
591
592/*
593 * This function gets the write port for data.
594 *
595 * The current write port is returned if available and the value is
596 * increased (provided it does not reach the maximum limit, in which
597 * case it is reset to 1)
598 */
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -0700599static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700600{
601 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700602 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700603 u32 wr_bitmap = card->mp_wr_bitmap;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700604
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700605 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700606
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700607 if (card->supports_sdio_new_mode &&
608 !(wr_bitmap & reg->data_port_mask)) {
609 adapter->data_sent = true;
610 return -EBUSY;
611 } else if (!card->supports_sdio_new_mode &&
612 !(wr_bitmap & card->mp_data_port_mask)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700613 return -1;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700614 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700615
616 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700617 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700618 *port = card->curr_wr_port;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700619 if (((card->supports_sdio_new_mode) &&
620 (++card->curr_wr_port == card->max_ports)) ||
621 ((!card->supports_sdio_new_mode) &&
622 (++card->curr_wr_port == card->mp_end_port)))
623 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700624 } else {
625 adapter->data_sent = true;
626 return -EBUSY;
627 }
628
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700629 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700630 dev_err(adapter->dev,
631 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700632 *port, card->curr_wr_port, wr_bitmap,
633 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700634 return -1;
635 }
636
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700637 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700638 *port, wr_bitmap, card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700639
640 return 0;
641}
642
643/*
644 * This function polls the card status.
645 */
646static int
647mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
648{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700649 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700650 u32 tries;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700651 u8 cs;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700652
653 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700654 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700655 break;
656 else if ((cs & bits) == bits)
657 return 0;
658
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700659 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700660 }
661
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700662 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
663
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700664 return -1;
665}
666
667/*
668 * This function reads the firmware status.
669 */
670static int
671mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
672{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700673 struct sdio_mmc_card *card = adapter->card;
674 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700675 u8 fws0, fws1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700676
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700677 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700678 return -1;
679
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700680 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700681 return -1;
682
683 *dat = (u16) ((fws1 << 8) | fws0);
684
685 return 0;
686}
687
688/*
689 * This function disables the host interrupt.
690 *
691 * The host interrupt mask is read, the disable bit is reset and
692 * written back to the card host interrupt mask register.
693 */
Daniel Drake232fde02013-07-13 10:57:10 -0400694static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700695{
Daniel Drake232fde02013-07-13 10:57:10 -0400696 struct sdio_mmc_card *card = adapter->card;
697 struct sdio_func *func = card->func;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700698
Daniel Drake232fde02013-07-13 10:57:10 -0400699 sdio_claim_host(func);
700 mwifiex_write_reg_locked(func, HOST_INT_MASK_REG, 0);
701 sdio_release_irq(func);
702 sdio_release_host(func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700703}
704
705/*
Amitkumar Karwar2cdf3592013-07-22 19:17:56 -0700706 * This function reads the interrupt status from card.
707 */
708static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
709{
710 struct sdio_mmc_card *card = adapter->card;
711 u8 sdio_ireg;
712 unsigned long flags;
713
714 if (mwifiex_read_data_sync(adapter, card->mp_regs,
715 card->reg->max_mp_regs,
716 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
717 dev_err(adapter->dev, "read mp_regs failed\n");
718 return;
719 }
720
721 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
722 if (sdio_ireg) {
723 /*
724 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
725 * For SDIO new mode CMD port interrupts
726 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
727 * UP_LD_CMD_PORT_HOST_INT_STATUS
728 * Clear the interrupt status register
729 */
730 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
731 spin_lock_irqsave(&adapter->int_lock, flags);
732 adapter->int_status |= sdio_ireg;
733 spin_unlock_irqrestore(&adapter->int_lock, flags);
734 }
735}
736
737/*
738 * SDIO interrupt handler.
739 *
740 * This function reads the interrupt status from firmware and handles
741 * the interrupt in current thread (ksdioirqd) right away.
742 */
743static void
744mwifiex_sdio_interrupt(struct sdio_func *func)
745{
746 struct mwifiex_adapter *adapter;
747 struct sdio_mmc_card *card;
748
749 card = sdio_get_drvdata(func);
750 if (!card || !card->adapter) {
751 pr_debug("int: func=%p card=%p adapter=%p\n",
752 func, card, card ? card->adapter : NULL);
753 return;
754 }
755 adapter = card->adapter;
756
757 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
758 adapter->ps_state = PS_STATE_AWAKE;
759
760 mwifiex_interrupt_status(adapter);
761 mwifiex_main_process(adapter);
762}
763
764/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700765 * This function enables the host interrupt.
766 *
767 * The host interrupt enable mask is written to the card
768 * host interrupt mask register.
769 */
770static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
771{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700772 struct sdio_mmc_card *card = adapter->card;
Daniel Drake232fde02013-07-13 10:57:10 -0400773 struct sdio_func *func = card->func;
774 int ret;
775
776 sdio_claim_host(func);
777
778 /* Request the SDIO IRQ */
779 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
780 if (ret) {
781 dev_err(adapter->dev, "claim irq failed: ret=%d\n", ret);
782 goto out;
783 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700784
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700785 /* Simply write the mask to the register */
Daniel Drake232fde02013-07-13 10:57:10 -0400786 ret = mwifiex_write_reg_locked(func, HOST_INT_MASK_REG,
787 card->reg->host_int_enable);
788 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700789 dev_err(adapter->dev, "enable host interrupt failed\n");
Daniel Drake232fde02013-07-13 10:57:10 -0400790 sdio_release_irq(func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700791 }
Daniel Drake232fde02013-07-13 10:57:10 -0400792
793out:
794 sdio_release_host(func);
795 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700796}
797
798/*
799 * This function sends a data buffer to the card.
800 */
801static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
802 u32 *type, u8 *buffer,
803 u32 npayload, u32 ioport)
804{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700805 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700806 u32 nb;
807
808 if (!buffer) {
809 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
810 return -1;
811 }
812
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700813 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700814
815 if (ret) {
816 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700817 ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700818 return -1;
819 }
820
821 nb = le16_to_cpu(*(__le16 *) (buffer));
822 if (nb > npayload) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700823 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
824 __func__, nb, npayload);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700825 return -1;
826 }
827
828 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
829
830 return ret;
831}
832
833/*
834 * This function downloads the firmware to the card.
835 *
836 * Firmware is downloaded to the card in blocks. Every block download
837 * is tested for CRC errors, and retried a number of times before
838 * returning failure.
839 */
840static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
841 struct mwifiex_fw_image *fw)
842{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700843 struct sdio_mmc_card *card = adapter->card;
844 const struct mwifiex_sdio_card_reg *reg = card->reg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700845 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700846 u8 *firmware = fw->fw_buf;
847 u32 firmware_len = fw->fw_len;
848 u32 offset = 0;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700849 u8 base0, base1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700850 u8 *fwbuf;
851 u16 len = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700852 u32 txlen, tx_blocks = 0, tries;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700853 u32 i = 0;
854
855 if (!firmware_len) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700856 dev_err(adapter->dev,
857 "firmware image not found! Terminating download\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700858 return -1;
859 }
860
861 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700862 firmware_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700863
864 /* Assume that the allocated buffer is 8-byte aligned */
865 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
Joe Perches0d2e7a52013-02-03 17:28:14 +0000866 if (!fwbuf)
Christoph Fritzb53575e2011-05-08 22:50:09 +0200867 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700868
869 /* Perform firmware data transfer */
870 do {
871 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
872 bits */
873 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
874 DN_LD_CARD_RDY);
875 if (ret) {
876 dev_err(adapter->dev, "FW download with helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700877 " poll status timeout @ %d\n", offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700878 goto done;
879 }
880
881 /* More data? */
882 if (offset >= firmware_len)
883 break;
884
885 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700886 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700887 &base0);
888 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700889 dev_err(adapter->dev,
890 "dev BASE0 register read failed: "
891 "base0=%#04X(%d). Terminating dnld\n",
892 base0, base0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700893 goto done;
894 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700895 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700896 &base1);
897 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700898 dev_err(adapter->dev,
899 "dev BASE1 register read failed: "
900 "base1=%#04X(%d). Terminating dnld\n",
901 base1, base1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700902 goto done;
903 }
904 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
905
906 if (len)
907 break;
908
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700909 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700910 }
911
912 if (!len) {
913 break;
914 } else if (len > MWIFIEX_UPLD_SIZE) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700915 dev_err(adapter->dev,
916 "FW dnld failed @ %d, invalid length %d\n",
917 offset, len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700918 ret = -1;
919 goto done;
920 }
921
922 txlen = len;
923
924 if (len & BIT(0)) {
925 i++;
926 if (i > MAX_WRITE_IOMEM_RETRY) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700927 dev_err(adapter->dev,
928 "FW dnld failed @ %d, over max retry\n",
929 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700930 ret = -1;
931 goto done;
932 }
933 dev_err(adapter->dev, "CRC indicated by the helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700934 " len = 0x%04X, txlen = %d\n", len, txlen);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700935 len &= ~BIT(0);
936 /* Setting this to 0 to resend from same offset */
937 txlen = 0;
938 } else {
939 i = 0;
940
941 /* Set blocksize to transfer - checking for last
942 block */
943 if (firmware_len - offset < txlen)
944 txlen = firmware_len - offset;
945
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700946 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
947 / MWIFIEX_SDIO_BLOCK_SIZE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700948
949 /* Copy payload to buffer */
950 memmove(fwbuf, &firmware[offset], txlen);
951 }
952
953 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
954 MWIFIEX_SDIO_BLOCK_SIZE,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700955 adapter->ioport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700956 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700957 dev_err(adapter->dev,
958 "FW download, write iomem (%d) failed @ %d\n",
959 i, offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700960 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
961 dev_err(adapter->dev, "write CFG reg failed\n");
962
963 ret = -1;
964 goto done;
965 }
966
967 offset += txlen;
968 } while (true);
969
970 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700971 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700972
973 ret = 0;
974done:
975 kfree(fwbuf);
976 return ret;
977}
978
979/*
980 * This function checks the firmware status in card.
981 *
982 * The winner interface is also determined by this function.
983 */
984static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700985 u32 poll_num)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700986{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700987 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700988 int ret = 0;
989 u16 firmware_stat;
990 u32 tries;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700991 u8 winner_status;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700992
993 /* Wait for firmware initialization event */
994 for (tries = 0; tries < poll_num; tries++) {
995 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
996 if (ret)
997 continue;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700998 if (firmware_stat == FIRMWARE_READY_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700999 ret = 0;
1000 break;
1001 } else {
Amitkumar Karwara76b20e2013-07-22 19:17:53 -07001002 msleep(100);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001003 ret = -1;
1004 }
1005 }
1006
Amitkumar Karward930fae2011-10-11 17:41:21 -07001007 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001008 if (mwifiex_read_reg
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001009 (adapter, card->reg->status_reg_0, &winner_status))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001010 winner_status = 0;
1011
1012 if (winner_status)
Amitkumar Karward930fae2011-10-11 17:41:21 -07001013 adapter->winner = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001014 else
Amitkumar Karward930fae2011-10-11 17:41:21 -07001015 adapter->winner = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001016 }
1017 return ret;
1018}
1019
1020/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001021 * This function decodes a received packet.
1022 *
1023 * Based on the type, the packet is treated as either a data, or
1024 * a command response, or an event, and the correct handler
1025 * function is invoked.
1026 */
1027static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1028 struct sk_buff *skb, u32 upld_typ)
1029{
1030 u8 *cmd_buf;
Avinash Patild03b4aa2013-11-05 15:01:44 -08001031 __le16 *curr_ptr = (__le16 *)skb->data;
1032 u16 pkt_len = le16_to_cpu(*curr_ptr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001033
Avinash Patild03b4aa2013-11-05 15:01:44 -08001034 skb_trim(skb, pkt_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001035 skb_pull(skb, INTF_HEADER_LEN);
1036
1037 switch (upld_typ) {
1038 case MWIFIEX_TYPE_DATA:
1039 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1040 mwifiex_handle_rx_packet(adapter, skb);
1041 break;
1042
1043 case MWIFIEX_TYPE_CMD:
1044 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1045 /* take care of curr_cmd = NULL case */
1046 if (!adapter->curr_cmd) {
1047 cmd_buf = adapter->upld_buf;
1048
1049 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1050 mwifiex_process_sleep_confirm_resp(adapter,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001051 skb->data,
1052 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001053
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001054 memcpy(cmd_buf, skb->data,
1055 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1056 skb->len));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001057
1058 dev_kfree_skb_any(skb);
1059 } else {
1060 adapter->cmd_resp_received = true;
1061 adapter->curr_cmd->resp_skb = skb;
1062 }
1063 break;
1064
1065 case MWIFIEX_TYPE_EVENT:
1066 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
Tobias Waldekranzcfcd9262013-08-26 09:18:06 +02001067 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001068
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001069 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
Amitkumar Karware80c81d2012-06-20 20:21:12 -07001070 memcpy(adapter->event_body,
1071 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1072 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001073
1074 /* event cause has been saved to adapter->event_cause */
1075 adapter->event_received = true;
1076 adapter->event_skb = skb;
1077
1078 break;
1079
1080 default:
1081 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1082 dev_kfree_skb_any(skb);
1083 break;
1084 }
1085
1086 return 0;
1087}
1088
1089/*
1090 * This function transfers received packets from card to driver, performing
1091 * aggregation if required.
1092 *
1093 * For data received on control port, or if aggregation is disabled, the
1094 * received buffers are uploaded as separate packets. However, if aggregation
1095 * is enabled and required, the buffers are copied onto an aggregation buffer,
1096 * provided there is space left, processed and finally uploaded.
1097 */
1098static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1099 struct sk_buff *skb, u8 port)
1100{
1101 struct sdio_mmc_card *card = adapter->card;
1102 s32 f_do_rx_aggr = 0;
1103 s32 f_do_rx_cur = 0;
1104 s32 f_aggr_cur = 0;
1105 struct sk_buff *skb_deaggr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001106 u32 pind;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001107 u32 pkt_len, pkt_type, mport;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001108 u8 *curr_ptr;
1109 u32 rx_len = skb->len;
1110
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001111 if ((card->has_control_mask) && (port == CTRL_PORT)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001112 /* Read the command Resp without aggr */
1113 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001114 "response\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001115
1116 f_do_rx_cur = 1;
1117 goto rx_curr_single;
1118 }
1119
1120 if (!card->mpa_rx.enabled) {
1121 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001122 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001123
1124 f_do_rx_cur = 1;
1125 goto rx_curr_single;
1126 }
1127
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001128 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1129 card->reg->data_port_mask)) ||
1130 (card->has_control_mask && (card->mp_rd_bitmap &
1131 (~((u32) CTRL_PORT_MASK))))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001132 /* Some more data RX pending */
1133 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1134
1135 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1136 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1137 f_aggr_cur = 1;
1138 } else {
1139 /* No room in Aggr buf, do rx aggr now */
1140 f_do_rx_aggr = 1;
1141 f_do_rx_cur = 1;
1142 }
1143 } else {
1144 /* Rx aggr not in progress */
1145 f_aggr_cur = 1;
1146 }
1147
1148 } else {
1149 /* No more data RX pending */
1150 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1151
1152 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1153 f_do_rx_aggr = 1;
1154 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1155 f_aggr_cur = 1;
1156 else
1157 /* No room in Aggr buf, do rx aggr now */
1158 f_do_rx_cur = 1;
1159 } else {
1160 f_do_rx_cur = 1;
1161 }
1162 }
1163
1164 if (f_aggr_cur) {
1165 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1166 /* Curr pkt can be aggregated */
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001167 mp_rx_aggr_setup(card, skb, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001168
1169 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001170 mp_rx_aggr_port_limit_reached(card)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001171 dev_dbg(adapter->dev, "info: %s: aggregated packet "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001172 "limit reached\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001173 /* No more pkts allowed in Aggr buf, rx it */
1174 f_do_rx_aggr = 1;
1175 }
1176 }
1177
1178 if (f_do_rx_aggr) {
1179 /* do aggr RX now */
1180 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001181 card->mpa_rx.pkt_cnt);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001182
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001183 if (card->supports_sdio_new_mode) {
1184 int i;
1185 u32 port_count;
1186
1187 for (i = 0, port_count = 0; i < card->max_ports; i++)
1188 if (card->mpa_rx.ports & BIT(i))
1189 port_count++;
1190
1191 /* Reading data from "start_port + 0" to "start_port +
1192 * port_count -1", so decrease the count by 1
1193 */
1194 port_count--;
1195 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1196 (port_count << 8)) + card->mpa_rx.start_port;
1197 } else {
1198 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1199 (card->mpa_rx.ports << 4)) +
1200 card->mpa_rx.start_port;
1201 }
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001202
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001203 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001204 card->mpa_rx.buf_len, mport, 1))
Avinash Patil17a60b42011-12-08 20:41:04 -08001205 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001206
1207 curr_ptr = card->mpa_rx.buf;
1208
1209 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1210
1211 /* get curr PKT len & type */
Tobias Waldekranzcfcd9262013-08-26 09:18:06 +02001212 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1213 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001214
1215 /* copy pkt to deaggr buf */
1216 skb_deaggr = card->mpa_rx.skb_arr[pind];
1217
1218 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1219 card->mpa_rx.len_arr[pind])) {
1220
1221 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1222
1223 skb_trim(skb_deaggr, pkt_len);
1224
1225 /* Process de-aggr packet */
1226 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1227 pkt_type);
1228 } else {
1229 dev_err(adapter->dev, "wrong aggr pkt:"
1230 " type=%d len=%d max_len=%d\n",
1231 pkt_type, pkt_len,
1232 card->mpa_rx.len_arr[pind]);
1233 dev_kfree_skb_any(skb_deaggr);
1234 }
1235 curr_ptr += card->mpa_rx.len_arr[pind];
1236 }
1237 MP_RX_AGGR_BUF_RESET(card);
1238 }
1239
1240rx_curr_single:
1241 if (f_do_rx_cur) {
1242 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1243 port, rx_len);
1244
1245 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1246 skb->data, skb->len,
1247 adapter->ioport + port))
Avinash Patil17a60b42011-12-08 20:41:04 -08001248 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001249
1250 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1251 }
1252
1253 return 0;
Avinash Patil17a60b42011-12-08 20:41:04 -08001254
1255error:
1256 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1257 /* Multiport-aggregation transfer failed - cleanup */
1258 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1259 /* copy pkt to deaggr buf */
1260 skb_deaggr = card->mpa_rx.skb_arr[pind];
1261 dev_kfree_skb_any(skb_deaggr);
1262 }
1263 MP_RX_AGGR_BUF_RESET(card);
1264 }
1265
1266 if (f_do_rx_cur)
1267 /* Single transfer pending. Free curr buff also */
1268 dev_kfree_skb_any(skb);
1269
1270 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001271}
1272
1273/*
1274 * This function checks the current interrupt status.
1275 *
1276 * The following interrupts are checked and handled by this function -
1277 * - Data sent
1278 * - Command sent
1279 * - Packets received
1280 *
1281 * Since the firmware does not generate download ready interrupt if the
1282 * port updated is command port only, command sent interrupt checking
1283 * should be done manually, and for every SDIO interrupt.
1284 *
1285 * In case of Rx packets received, the packets are uploaded from card to
1286 * host and processed accordingly.
1287 */
1288static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1289{
1290 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001291 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001292 int ret = 0;
1293 u8 sdio_ireg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001294 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001295 u8 port = CTRL_PORT;
1296 u32 len_reg_l, len_reg_u;
1297 u32 rx_blocks;
1298 u16 rx_len;
1299 unsigned long flags;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001300 u32 bitmap;
1301 u8 cr;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001302
1303 spin_lock_irqsave(&adapter->int_lock, flags);
1304 sdio_ireg = adapter->int_status;
1305 adapter->int_status = 0;
1306 spin_unlock_irqrestore(&adapter->int_lock, flags);
1307
1308 if (!sdio_ireg)
1309 return ret;
1310
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001311 /* Following interrupt is only for SDIO new mode */
1312 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1313 adapter->cmd_sent = false;
1314
1315 /* Following interrupt is only for SDIO new mode */
1316 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1317 u32 pkt_type;
1318
1319 /* read the len of control packet */
1320 rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1321 rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1322 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1323 if (rx_len <= INTF_HEADER_LEN ||
1324 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1325 MWIFIEX_RX_DATA_BUF_SIZE)
1326 return -1;
1327 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1328
1329 skb = dev_alloc_skb(rx_len);
1330 if (!skb)
1331 return -1;
1332
1333 skb_put(skb, rx_len);
1334
1335 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1336 skb->len, adapter->ioport |
1337 CMD_PORT_SLCT)) {
1338 dev_err(adapter->dev,
1339 "%s: failed to card_to_host", __func__);
1340 dev_kfree_skb_any(skb);
1341 goto term_cmd;
1342 }
1343
1344 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1345 (pkt_type != MWIFIEX_TYPE_EVENT))
1346 dev_err(adapter->dev,
1347 "%s:Received wrong packet on cmd port",
1348 __func__);
1349
1350 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1351 }
1352
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001353 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001354 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1355 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1356 if (card->supports_sdio_new_mode) {
1357 bitmap |=
1358 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1359 bitmap |=
1360 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1361 }
1362 card->mp_wr_bitmap = bitmap;
1363
1364 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001365 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001366 if (adapter->data_sent &&
1367 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1368 dev_dbg(adapter->dev,
1369 "info: <--- Tx DONE Interrupt --->\n");
1370 adapter->data_sent = false;
1371 }
1372 }
1373
1374 /* As firmware will not generate download ready interrupt if the port
1375 updated is command port only, cmd_sent should be done for any SDIO
1376 interrupt. */
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001377 if (card->has_control_mask && adapter->cmd_sent) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001378 /* Check if firmware has attach buffer at command port and
1379 update just that in wr_bit_map. */
1380 card->mp_wr_bitmap |=
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001381 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001382 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1383 adapter->cmd_sent = false;
1384 }
1385
1386 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001387 adapter->cmd_sent, adapter->data_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001388 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001389 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1390 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1391 if (card->supports_sdio_new_mode) {
1392 bitmap |=
1393 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1394 bitmap |=
1395 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1396 }
1397 card->mp_rd_bitmap = bitmap;
1398 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001399 card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001400
1401 while (true) {
1402 ret = mwifiex_get_rd_port(adapter, &port);
1403 if (ret) {
1404 dev_dbg(adapter->dev,
1405 "info: no more rd_port available\n");
1406 break;
1407 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001408 len_reg_l = reg->rd_len_p0_l + (port << 1);
1409 len_reg_u = reg->rd_len_p0_u + (port << 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001410 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1411 rx_len |= (u16) card->mp_regs[len_reg_l];
1412 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001413 port, rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001414 rx_blocks =
1415 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1416 1) / MWIFIEX_SDIO_BLOCK_SIZE;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001417 if (rx_len <= INTF_HEADER_LEN ||
1418 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1419 MWIFIEX_RX_DATA_BUF_SIZE) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001420 dev_err(adapter->dev, "invalid rx_len=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001421 rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001422 return -1;
1423 }
1424 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1425
1426 skb = dev_alloc_skb(rx_len);
1427
1428 if (!skb) {
1429 dev_err(adapter->dev, "%s: failed to alloc skb",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001430 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001431 return -1;
1432 }
1433
1434 skb_put(skb, rx_len);
1435
1436 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001437 rx_len, skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001438
1439 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1440 port)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001441 dev_err(adapter->dev, "card_to_host_mpa failed:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001442 " int status=%#x\n", sdio_ireg);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001443 goto term_cmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001444 }
1445 }
1446 }
1447
1448 return 0;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001449
1450term_cmd:
1451 /* terminate cmd */
1452 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1453 dev_err(adapter->dev, "read CFG reg failed\n");
1454 else
1455 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1456
1457 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1458 dev_err(adapter->dev, "write CFG reg failed\n");
1459 else
1460 dev_dbg(adapter->dev, "info: write success\n");
1461
1462 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1463 dev_err(adapter->dev, "read CFG reg failed\n");
1464 else
1465 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1466
1467 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001468}
1469
1470/*
1471 * This function aggregates transmission buffers in driver and downloads
1472 * the aggregated packet to card.
1473 *
1474 * The individual packets are aggregated by copying into an aggregation
1475 * buffer and then downloaded to the card. Previous unsent packets in the
1476 * aggregation buffer are pre-copied first before new packets are added.
1477 * Aggregation is done till there is space left in the aggregation buffer,
1478 * or till new packets are available.
1479 *
1480 * The function will only download the packet to the card when aggregation
1481 * stops, otherwise it will just aggregate the packet in aggregation buffer
1482 * and return.
1483 */
1484static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001485 u8 *payload, u32 pkt_len, u32 port,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001486 u32 next_pkt_len)
1487{
1488 struct sdio_mmc_card *card = adapter->card;
1489 int ret = 0;
1490 s32 f_send_aggr_buf = 0;
1491 s32 f_send_cur_buf = 0;
1492 s32 f_precopy_cur_buf = 0;
1493 s32 f_postcopy_cur_buf = 0;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001494 u32 mport;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001495
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001496 if (!card->mpa_tx.enabled ||
1497 (card->has_control_mask && (port == CTRL_PORT)) ||
1498 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001499 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001500 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001501
1502 f_send_cur_buf = 1;
1503 goto tx_curr_single;
1504 }
1505
1506 if (next_pkt_len) {
1507 /* More pkt in TX queue */
1508 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001509 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001510
1511 if (MP_TX_AGGR_IN_PROGRESS(card)) {
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001512 if (!mp_tx_aggr_port_limit_reached(card) &&
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001513 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1514 f_precopy_cur_buf = 1;
1515
1516 if (!(card->mp_wr_bitmap &
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001517 (1 << card->curr_wr_port)) ||
1518 !MP_TX_AGGR_BUF_HAS_ROOM(
1519 card, pkt_len + next_pkt_len))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001520 f_send_aggr_buf = 1;
1521 } else {
1522 /* No room in Aggr buf, send it */
1523 f_send_aggr_buf = 1;
1524
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001525 if (mp_tx_aggr_port_limit_reached(card) ||
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001526 !(card->mp_wr_bitmap &
1527 (1 << card->curr_wr_port)))
1528 f_send_cur_buf = 1;
1529 else
1530 f_postcopy_cur_buf = 1;
1531 }
1532 } else {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001533 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1534 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001535 f_precopy_cur_buf = 1;
1536 else
1537 f_send_cur_buf = 1;
1538 }
1539 } else {
1540 /* Last pkt in TX queue */
1541 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001542 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001543
1544 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1545 /* some packs in Aggr buf already */
1546 f_send_aggr_buf = 1;
1547
1548 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1549 f_precopy_cur_buf = 1;
1550 else
1551 /* No room in Aggr buf, send it */
1552 f_send_cur_buf = 1;
1553 } else {
1554 f_send_cur_buf = 1;
1555 }
1556 }
1557
1558 if (f_precopy_cur_buf) {
1559 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001560 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001561 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1562
1563 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001564 mp_tx_aggr_port_limit_reached(card))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001565 /* No more pkts allowed in Aggr buf, send it */
1566 f_send_aggr_buf = 1;
1567 }
1568
1569 if (f_send_aggr_buf) {
1570 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001571 __func__,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001572 card->mpa_tx.start_port, card->mpa_tx.ports);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001573 if (card->supports_sdio_new_mode) {
1574 u32 port_count;
1575 int i;
1576
1577 for (i = 0, port_count = 0; i < card->max_ports; i++)
1578 if (card->mpa_tx.ports & BIT(i))
1579 port_count++;
1580
1581 /* Writing data from "start_port + 0" to "start_port +
1582 * port_count -1", so decrease the count by 1
1583 */
1584 port_count--;
1585 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1586 (port_count << 8)) + card->mpa_tx.start_port;
1587 } else {
1588 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1589 (card->mpa_tx.ports << 4)) +
1590 card->mpa_tx.start_port;
1591 }
1592
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001593 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001594 card->mpa_tx.buf_len, mport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001595
1596 MP_TX_AGGR_BUF_RESET(card);
1597 }
1598
1599tx_curr_single:
1600 if (f_send_cur_buf) {
1601 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001602 __func__, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001603 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1604 adapter->ioport + port);
1605 }
1606
1607 if (f_postcopy_cur_buf) {
1608 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001609 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001610 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1611 }
1612
1613 return ret;
1614}
1615
1616/*
1617 * This function downloads data from driver to card.
1618 *
1619 * Both commands and data packets are transferred to the card by this
1620 * function.
1621 *
1622 * This function adds the SDIO specific header to the front of the buffer
1623 * before transferring. The header contains the length of the packet and
1624 * the type. The firmware handles the packets based upon this set type.
1625 */
1626static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001627 u8 type, struct sk_buff *skb,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001628 struct mwifiex_tx_param *tx_param)
1629{
1630 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001631 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001632 u32 buf_block_len;
1633 u32 blk_size;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001634 u32 port = CTRL_PORT;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001635 u8 *payload = (u8 *)skb->data;
1636 u32 pkt_len = skb->len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001637
1638 /* Allocate buffer and copy payload */
1639 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1640 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
Tomasz Moń83e612f2013-07-23 07:42:49 +02001641 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1642 *(__le16 *)&payload[2] = cpu_to_le16(type);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001643
1644 /*
1645 * This is SDIO specific header
1646 * u16 length,
1647 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1648 * MWIFIEX_TYPE_EVENT = 3)
1649 */
1650 if (type == MWIFIEX_TYPE_DATA) {
1651 ret = mwifiex_get_wr_port_data(adapter, &port);
1652 if (ret) {
1653 dev_err(adapter->dev, "%s: no wr_port available\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001654 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001655 return ret;
1656 }
1657 } else {
1658 adapter->cmd_sent = true;
1659 /* Type must be MWIFIEX_TYPE_CMD */
1660
1661 if (pkt_len <= INTF_HEADER_LEN ||
1662 pkt_len > MWIFIEX_UPLD_SIZE)
1663 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001664 __func__, payload, pkt_len);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001665
1666 if (card->supports_sdio_new_mode)
1667 port = CMD_PORT_SLCT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001668 }
1669
1670 /* Transfer data to card */
1671 pkt_len = buf_block_len * blk_size;
1672
1673 if (tx_param)
1674 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001675 port, tx_param->next_pkt_len
1676 );
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001677 else
1678 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001679 port, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001680
1681 if (ret) {
1682 if (type == MWIFIEX_TYPE_CMD)
1683 adapter->cmd_sent = false;
1684 if (type == MWIFIEX_TYPE_DATA)
1685 adapter->data_sent = false;
1686 } else {
1687 if (type == MWIFIEX_TYPE_DATA) {
1688 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1689 adapter->data_sent = true;
1690 else
1691 adapter->data_sent = false;
1692 }
1693 }
1694
1695 return ret;
1696}
1697
1698/*
1699 * This function allocates the MPA Tx and Rx buffers.
1700 */
1701static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1702 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1703{
1704 struct sdio_mmc_card *card = adapter->card;
1705 int ret = 0;
1706
1707 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1708 if (!card->mpa_tx.buf) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001709 ret = -1;
1710 goto error;
1711 }
1712
1713 card->mpa_tx.buf_size = mpa_tx_buf_size;
1714
1715 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1716 if (!card->mpa_rx.buf) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001717 ret = -1;
1718 goto error;
1719 }
1720
1721 card->mpa_rx.buf_size = mpa_rx_buf_size;
1722
1723error:
1724 if (ret) {
1725 kfree(card->mpa_tx.buf);
1726 kfree(card->mpa_rx.buf);
1727 }
1728
1729 return ret;
1730}
1731
1732/*
1733 * This function unregisters the SDIO device.
1734 *
1735 * The SDIO IRQ is released, the function is disabled and driver
1736 * data is set to null.
1737 */
1738static void
1739mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1740{
1741 struct sdio_mmc_card *card = adapter->card;
1742
1743 if (adapter->card) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001744 sdio_claim_host(card->func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001745 sdio_disable_func(card->func);
1746 sdio_release_host(card->func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001747 }
1748}
1749
1750/*
1751 * This function registers the SDIO device.
1752 *
1753 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1754 */
1755static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1756{
Daniel Drake232fde02013-07-13 10:57:10 -04001757 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001758 struct sdio_mmc_card *card = adapter->card;
1759 struct sdio_func *func = card->func;
1760
1761 /* save adapter pointer in card */
1762 card->adapter = adapter;
1763
1764 sdio_claim_host(func);
1765
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001766 /* Set block size */
1767 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
Daniel Drake232fde02013-07-13 10:57:10 -04001768 sdio_release_host(func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001769 if (ret) {
1770 pr_err("cannot set SDIO block size\n");
Daniel Drake232fde02013-07-13 10:57:10 -04001771 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001772 }
1773
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001774
1775 adapter->dev = &func->dev;
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001776
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001777 strcpy(adapter->fw_name, card->firmware);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001778
1779 return 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001780}
1781
1782/*
1783 * This function initializes the SDIO driver.
1784 *
1785 * The following initializations steps are followed -
1786 * - Read the Host interrupt status register to acknowledge
1787 * the first interrupt got from bootloader
1788 * - Disable host interrupt mask register
1789 * - Get SDIO port
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001790 * - Initialize SDIO variables in card
1791 * - Allocate MP registers
1792 * - Allocate MPA Tx and Rx buffers
1793 */
1794static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1795{
1796 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001797 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001798 int ret;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -07001799 u8 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001800
Amitkumar Karwar3c59e322013-11-14 19:10:41 -08001801 sdio_set_drvdata(card->func, card);
1802
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001803 /*
1804 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1805 * from the bootloader. If we don't do this we get a interrupt
1806 * as soon as we register the irq.
1807 */
1808 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1809
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001810 /* Get SDIO ioport */
1811 mwifiex_init_sdio_ioport(adapter);
1812
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001813 /* Initialize SDIO variables in card */
1814 card->mp_rd_bitmap = 0;
1815 card->mp_wr_bitmap = 0;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001816 card->curr_rd_port = reg->start_rd_port;
1817 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001818
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001819 card->mp_data_port_mask = reg->data_port_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001820
1821 card->mpa_tx.buf_len = 0;
1822 card->mpa_tx.pkt_cnt = 0;
1823 card->mpa_tx.start_port = 0;
1824
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001825 card->mpa_tx.enabled = 1;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001826 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001827
1828 card->mpa_rx.buf_len = 0;
1829 card->mpa_rx.pkt_cnt = 0;
1830 card->mpa_rx.start_port = 0;
1831
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001832 card->mpa_rx.enabled = 1;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001833 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001834
1835 /* Allocate buffers for SDIO MP-A */
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001836 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
Joe Perches0d2e7a52013-02-03 17:28:14 +00001837 if (!card->mp_regs)
Christoph Fritzb53575e2011-05-08 22:50:09 +02001838 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001839
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001840 /* Allocate skb pointer buffers */
1841 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1842 card->mp_agg_pkt_limit, GFP_KERNEL);
1843 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1844 card->mp_agg_pkt_limit, GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001845 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1846 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1847 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1848 if (ret) {
1849 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1850 kfree(card->mp_regs);
1851 return -1;
1852 }
1853
1854 return ret;
1855}
1856
1857/*
1858 * This function resets the MPA Tx and Rx buffers.
1859 */
1860static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1861{
1862 struct sdio_mmc_card *card = adapter->card;
1863
1864 MP_TX_AGGR_BUF_RESET(card);
1865 MP_RX_AGGR_BUF_RESET(card);
1866}
1867
1868/*
1869 * This function cleans up the allocated card buffers.
1870 *
1871 * The following are freed by this function -
1872 * - MP registers
1873 * - MPA Tx buffer
1874 * - MPA Rx buffer
1875 */
1876static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1877{
1878 struct sdio_mmc_card *card = adapter->card;
1879
1880 kfree(card->mp_regs);
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001881 kfree(card->mpa_rx.skb_arr);
1882 kfree(card->mpa_rx.len_arr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001883 kfree(card->mpa_tx.buf);
1884 kfree(card->mpa_rx.buf);
Amitkumar Karwar3c59e322013-11-14 19:10:41 -08001885 sdio_set_drvdata(card->func, NULL);
1886 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001887}
1888
1889/*
1890 * This function updates the MP end port in card.
1891 */
1892static void
1893mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1894{
1895 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001896 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001897 int i;
1898
1899 card->mp_end_port = port;
1900
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001901 card->mp_data_port_mask = reg->data_port_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001902
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001903 if (reg->start_wr_port) {
1904 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1905 card->mp_data_port_mask &=
1906 ~(1 << (card->max_ports - i));
1907 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001908
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001909 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001910
1911 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001912 port, card->mp_data_port_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001913}
1914
Amitkumar Karward31ab352012-11-01 18:44:14 -07001915static struct mmc_host *reset_host;
1916static void sdio_card_reset_worker(struct work_struct *work)
1917{
Tejun Heo7f5855c2012-12-21 17:56:57 -08001918 struct mmc_host *target = reset_host;
1919
Amitkumar Karward31ab352012-11-01 18:44:14 -07001920 /* The actual reset operation must be run outside of driver thread.
1921 * This is because mmc_remove_host() will cause the device to be
1922 * instantly destroyed, and the driver then needs to end its thread,
1923 * leading to a deadlock.
1924 *
1925 * We run it in a totally independent workqueue.
1926 */
1927
1928 pr_err("Resetting card...\n");
Tejun Heo7f5855c2012-12-21 17:56:57 -08001929 mmc_remove_host(target);
Amitkumar Karward31ab352012-11-01 18:44:14 -07001930 /* 20ms delay is based on experiment with sdhci controller */
1931 mdelay(20);
Tejun Heo7f5855c2012-12-21 17:56:57 -08001932 mmc_add_host(target);
Amitkumar Karward31ab352012-11-01 18:44:14 -07001933}
1934static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1935
1936/* This function resets the card */
1937static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1938{
1939 struct sdio_mmc_card *card = adapter->card;
1940
Amitkumar Karward31ab352012-11-01 18:44:14 -07001941 reset_host = card->func->card->host;
1942 schedule_work(&card_reset_work);
1943}
1944
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001945static struct mwifiex_if_ops sdio_ops = {
1946 .init_if = mwifiex_init_sdio,
1947 .cleanup_if = mwifiex_cleanup_sdio,
1948 .check_fw_status = mwifiex_check_fw_status,
1949 .prog_fw = mwifiex_prog_fw_w_helper,
1950 .register_dev = mwifiex_register_dev,
1951 .unregister_dev = mwifiex_unregister_dev,
1952 .enable_int = mwifiex_sdio_enable_host_int,
Daniel Drake232fde02013-07-13 10:57:10 -04001953 .disable_int = mwifiex_sdio_disable_host_int,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001954 .process_int_status = mwifiex_process_int_status,
1955 .host_to_card = mwifiex_sdio_host_to_card,
1956 .wakeup = mwifiex_pm_wakeup_card,
1957 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1958
1959 /* SDIO specific */
1960 .update_mp_end_port = mwifiex_update_mp_end_port,
1961 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001962 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1963 .event_complete = mwifiex_sdio_event_complete,
Amitkumar Karward31ab352012-11-01 18:44:14 -07001964 .card_reset = mwifiex_sdio_card_reset,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001965};
1966
1967/*
1968 * This function initializes the SDIO driver.
1969 *
1970 * This initiates the semaphore and registers the device with
1971 * SDIO bus.
1972 */
1973static int
1974mwifiex_sdio_init_module(void)
1975{
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001976 sema_init(&add_remove_card_sem, 1);
1977
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301978 /* Clear the flag in case user removes the card. */
1979 user_rmmod = 0;
1980
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001981 return sdio_register_driver(&mwifiex_sdio);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001982}
1983
1984/*
1985 * This function cleans up the SDIO driver.
1986 *
1987 * The following major steps are followed for cleanup -
1988 * - Resume the device if its suspended
1989 * - Disconnect the device if connected
1990 * - Shutdown the firmware
1991 * - Unregister the device from SDIO bus.
1992 */
1993static void
1994mwifiex_sdio_cleanup_module(void)
1995{
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301996 if (!down_interruptible(&add_remove_card_sem))
1997 up(&add_remove_card_sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001998
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301999 /* Set the flag as user is removing this module. */
2000 user_rmmod = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07002001
Amitkumar Karward31ab352012-11-01 18:44:14 -07002002 cancel_work_sync(&card_reset_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07002003 sdio_unregister_driver(&mwifiex_sdio);
2004}
2005
2006module_init(mwifiex_sdio_init_module);
2007module_exit(mwifiex_sdio_cleanup_module);
2008
2009MODULE_AUTHOR("Marvell International Ltd.");
2010MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2011MODULE_VERSION(SDIO_VERSION);
2012MODULE_LICENSE("GPL v2");
WarheadsSE98e6b9d2012-04-24 15:57:21 -04002013MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08002014MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2015MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07002016MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);