blob: d206f04d499498d6d9c7ba92a80685588ae7bc96 [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");
Amitkumar Karwarc0dbba62014-03-25 19:01:20 -0700240 adapter->hs_enabling = false;
Bing Zhaodd321ac2012-11-15 15:58:48 -0800241 return -EFAULT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700242 }
243
Bing Zhaodd321ac2012-11-15 15:58:48 -0800244 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
245 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
246
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700247 /* Indicate device suspended */
248 adapter->is_suspended = true;
Amitkumar Karwarc0dbba62014-03-25 19:01:20 -0700249 adapter->hs_enabling = false;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700250
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700251 return ret;
252}
253
WarheadsSE98e6b9d2012-04-24 15:57:21 -0400254/* Device ID for SD8786 */
255#define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700256/* Device ID for SD8787 */
257#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
Bing Zhaoe3bea1c2011-11-16 20:40:35 -0800258/* Device ID for SD8797 */
259#define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700260/* Device ID for SD8897 */
261#define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700262
263/* WLAN IDs */
264static const struct sdio_device_id mwifiex_ids[] = {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700265 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
266 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
267 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
268 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
269 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
270 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700271 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
272 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700273 {},
274};
275
276MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
277
278static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
279 .suspend = mwifiex_sdio_suspend,
280 .resume = mwifiex_sdio_resume,
281};
282
283static struct sdio_driver mwifiex_sdio = {
284 .name = "mwifiex_sdio",
285 .id_table = mwifiex_ids,
286 .probe = mwifiex_sdio_probe,
287 .remove = mwifiex_sdio_remove,
288 .drv = {
289 .owner = THIS_MODULE,
290 .pm = &mwifiex_sdio_pm_ops,
291 }
292};
293
Daniel Drake232fde02013-07-13 10:57:10 -0400294/* Write data into SDIO card register. Caller claims SDIO device. */
295static int
296mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
297{
298 int ret = -1;
299 sdio_writeb(func, data, reg, &ret);
300 return ret;
301}
302
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700303/*
304 * This function writes data into SDIO card register.
305 */
306static int
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700307mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700308{
309 struct sdio_mmc_card *card = adapter->card;
Daniel Drake232fde02013-07-13 10:57:10 -0400310 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700311
312 sdio_claim_host(card->func);
Daniel Drake232fde02013-07-13 10:57:10 -0400313 ret = mwifiex_write_reg_locked(card->func, reg, data);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700314 sdio_release_host(card->func);
315
316 return ret;
317}
318
319/*
320 * This function reads data from SDIO card register.
321 */
322static int
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700323mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700324{
325 struct sdio_mmc_card *card = adapter->card;
326 int ret = -1;
327 u8 val;
328
329 sdio_claim_host(card->func);
330 val = sdio_readb(card->func, reg, &ret);
331 sdio_release_host(card->func);
332
333 *data = val;
334
335 return ret;
336}
337
338/*
339 * This function writes multiple data into SDIO card memory.
340 *
341 * This does not work in suspended mode.
342 */
343static int
344mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700345 u8 *buffer, u32 pkt_len, u32 port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700346{
347 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800348 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700349 u8 blk_mode =
350 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
351 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
352 u32 blk_cnt =
353 (blk_mode ==
354 BLOCK_MODE) ? (pkt_len /
355 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
356 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
357
358 if (adapter->is_suspended) {
359 dev_err(adapter->dev,
360 "%s: not allowed while suspended\n", __func__);
361 return -1;
362 }
363
364 sdio_claim_host(card->func);
365
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800366 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700367
368 sdio_release_host(card->func);
369
370 return ret;
371}
372
373/*
374 * This function reads multiple data from SDIO card memory.
375 */
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700376static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
377 u32 len, u32 port, u8 claim)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700378{
379 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800380 int ret;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700381 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
382 : BLOCK_MODE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700383 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700384 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
385 : len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700386 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
387
388 if (claim)
389 sdio_claim_host(card->func);
390
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800391 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700392
393 if (claim)
394 sdio_release_host(card->func);
395
396 return ret;
397}
398
399/*
400 * This function wakes up the card.
401 *
402 * A host power up command is written to the card configuration
403 * register to wake up the card.
404 */
405static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
406{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700407 dev_dbg(adapter->dev, "event: wakeup device...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700408
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700409 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700410}
411
412/*
413 * This function is called after the card has woken up.
414 *
415 * The card configuration register is reset.
416 */
417static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
418{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700419 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700420
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700421 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700422}
423
424/*
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700425 * This function is used to initialize IO ports for the
426 * chipsets supporting SDIO new mode eg SD8897.
427 */
428static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
429{
430 u8 reg;
431
432 adapter->ioport = MEM_PORT;
433
434 /* enable sdio new mode */
435 if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, &reg))
436 return -1;
437 if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
438 reg | CMD53_NEW_MODE))
439 return -1;
440
441 /* Configure cmd port and enable reading rx length from the register */
442 if (mwifiex_read_reg(adapter, CMD_CONFIG_0, &reg))
443 return -1;
444 if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
445 return -1;
446
447 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
448 * completed
449 */
450 if (mwifiex_read_reg(adapter, CMD_CONFIG_1, &reg))
451 return -1;
452 if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
453 return -1;
454
455 return 0;
456}
457
458/* This function initializes the IO ports.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700459 *
460 * The following operations are performed -
461 * - Read the IO ports (0, 1 and 2)
462 * - Set host interrupt Reset-To-Read to clear
463 * - Set auto re-enable interrupt
464 */
465static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
466{
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700467 u8 reg;
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700468 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700469
470 adapter->ioport = 0;
471
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700472 if (card->supports_sdio_new_mode) {
473 if (mwifiex_init_sdio_new_mode(adapter))
474 return -1;
475 goto cont;
476 }
477
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700478 /* Read the IO port */
479 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
480 adapter->ioport |= (reg & 0xff);
481 else
482 return -1;
483
484 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
485 adapter->ioport |= ((reg & 0xff) << 8);
486 else
487 return -1;
488
489 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
490 adapter->ioport |= ((reg & 0xff) << 16);
491 else
492 return -1;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700493cont:
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700494 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
495
496 /* Set Host interrupt reset to read to clear */
497 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
498 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700499 reg | card->reg->sdio_int_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700500 else
501 return -1;
502
503 /* Dnld/Upld ready set to auto reset */
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700504 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
505 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700506 reg | AUTO_RE_ENABLE_INT);
507 else
508 return -1;
509
510 return 0;
511}
512
513/*
514 * This function sends data to the card.
515 */
516static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
517 u8 *payload, u32 pkt_len, u32 port)
518{
519 u32 i = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700520 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700521
522 do {
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700523 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700524 if (ret) {
525 i++;
526 dev_err(adapter->dev, "host_to_card, write iomem"
527 " (%d) failed: %d\n", i, ret);
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700528 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700529 dev_err(adapter->dev, "write CFG reg failed\n");
530
531 ret = -1;
532 if (i > MAX_WRITE_IOMEM_RETRY)
533 return ret;
534 }
535 } while (ret == -1);
536
537 return ret;
538}
539
540/*
541 * This function gets the read port.
542 *
543 * If control port bit is set in MP read bitmap, the control port
544 * is returned, otherwise the current read port is returned and
545 * the value is increased (provided it does not reach the maximum
546 * limit, in which case it is reset to 1)
547 */
548static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
549{
550 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700551 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700552 u32 rd_bitmap = card->mp_rd_bitmap;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700553
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700554 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700555
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700556 if (card->supports_sdio_new_mode) {
557 if (!(rd_bitmap & reg->data_port_mask))
558 return -1;
559 } else {
560 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
561 return -1;
562 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700563
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700564 if ((card->has_control_mask) &&
565 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700566 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700567 *port = CTRL_PORT;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700568 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700569 *port, card->mp_rd_bitmap);
Amitkumar Karware6a520302013-05-17 17:53:56 -0700570 return 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700571 }
Amitkumar Karware6a520302013-05-17 17:53:56 -0700572
573 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
574 return -1;
575
576 /* We are now handling the SDIO data ports */
577 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
578 *port = card->curr_rd_port;
579
580 if (++card->curr_rd_port == card->max_ports)
581 card->curr_rd_port = reg->start_rd_port;
582
583 dev_dbg(adapter->dev,
584 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
585 *port, rd_bitmap, card->mp_rd_bitmap);
586
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700587 return 0;
588}
589
590/*
591 * This function gets the write port for data.
592 *
593 * The current write port is returned if available and the value is
594 * increased (provided it does not reach the maximum limit, in which
595 * case it is reset to 1)
596 */
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -0700597static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700598{
599 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700600 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700601 u32 wr_bitmap = card->mp_wr_bitmap;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700602
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700603 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700604
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700605 if (card->supports_sdio_new_mode &&
606 !(wr_bitmap & reg->data_port_mask)) {
607 adapter->data_sent = true;
608 return -EBUSY;
609 } else if (!card->supports_sdio_new_mode &&
610 !(wr_bitmap & card->mp_data_port_mask)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700611 return -1;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700612 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700613
614 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700615 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700616 *port = card->curr_wr_port;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700617 if (((card->supports_sdio_new_mode) &&
618 (++card->curr_wr_port == card->max_ports)) ||
619 ((!card->supports_sdio_new_mode) &&
620 (++card->curr_wr_port == card->mp_end_port)))
621 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700622 } else {
623 adapter->data_sent = true;
624 return -EBUSY;
625 }
626
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -0700627 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700628 dev_err(adapter->dev,
629 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700630 *port, card->curr_wr_port, wr_bitmap,
631 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700632 return -1;
633 }
634
Amitkumar Karwar5ac253d2013-05-17 17:50:26 -0700635 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700636 *port, wr_bitmap, card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700637
638 return 0;
639}
640
641/*
642 * This function polls the card status.
643 */
644static int
645mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
646{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700647 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700648 u32 tries;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700649 u8 cs;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700650
651 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700652 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700653 break;
654 else if ((cs & bits) == bits)
655 return 0;
656
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700657 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700658 }
659
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700660 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
661
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700662 return -1;
663}
664
665/*
666 * This function reads the firmware status.
667 */
668static int
669mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
670{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700671 struct sdio_mmc_card *card = adapter->card;
672 const struct mwifiex_sdio_card_reg *reg = card->reg;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700673 u8 fws0, fws1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700674
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700675 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700676 return -1;
677
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700678 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700679 return -1;
680
681 *dat = (u16) ((fws1 << 8) | fws0);
682
683 return 0;
684}
685
686/*
687 * This function disables the host interrupt.
688 *
689 * The host interrupt mask is read, the disable bit is reset and
690 * written back to the card host interrupt mask register.
691 */
Daniel Drake232fde02013-07-13 10:57:10 -0400692static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700693{
Daniel Drake232fde02013-07-13 10:57:10 -0400694 struct sdio_mmc_card *card = adapter->card;
695 struct sdio_func *func = card->func;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700696
Daniel Drake232fde02013-07-13 10:57:10 -0400697 sdio_claim_host(func);
698 mwifiex_write_reg_locked(func, HOST_INT_MASK_REG, 0);
699 sdio_release_irq(func);
700 sdio_release_host(func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700701}
702
703/*
Amitkumar Karwar2cdf3592013-07-22 19:17:56 -0700704 * This function reads the interrupt status from card.
705 */
706static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
707{
708 struct sdio_mmc_card *card = adapter->card;
709 u8 sdio_ireg;
710 unsigned long flags;
711
712 if (mwifiex_read_data_sync(adapter, card->mp_regs,
713 card->reg->max_mp_regs,
714 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
715 dev_err(adapter->dev, "read mp_regs failed\n");
716 return;
717 }
718
719 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
720 if (sdio_ireg) {
721 /*
722 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
723 * For SDIO new mode CMD port interrupts
724 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
725 * UP_LD_CMD_PORT_HOST_INT_STATUS
726 * Clear the interrupt status register
727 */
728 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
729 spin_lock_irqsave(&adapter->int_lock, flags);
730 adapter->int_status |= sdio_ireg;
731 spin_unlock_irqrestore(&adapter->int_lock, flags);
732 }
733}
734
735/*
736 * SDIO interrupt handler.
737 *
738 * This function reads the interrupt status from firmware and handles
739 * the interrupt in current thread (ksdioirqd) right away.
740 */
741static void
742mwifiex_sdio_interrupt(struct sdio_func *func)
743{
744 struct mwifiex_adapter *adapter;
745 struct sdio_mmc_card *card;
746
747 card = sdio_get_drvdata(func);
748 if (!card || !card->adapter) {
749 pr_debug("int: func=%p card=%p adapter=%p\n",
750 func, card, card ? card->adapter : NULL);
751 return;
752 }
753 adapter = card->adapter;
754
755 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
756 adapter->ps_state = PS_STATE_AWAKE;
757
758 mwifiex_interrupt_status(adapter);
759 mwifiex_main_process(adapter);
760}
761
762/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700763 * This function enables the host interrupt.
764 *
765 * The host interrupt enable mask is written to the card
766 * host interrupt mask register.
767 */
768static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
769{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700770 struct sdio_mmc_card *card = adapter->card;
Daniel Drake232fde02013-07-13 10:57:10 -0400771 struct sdio_func *func = card->func;
772 int ret;
773
774 sdio_claim_host(func);
775
776 /* Request the SDIO IRQ */
777 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
778 if (ret) {
779 dev_err(adapter->dev, "claim irq failed: ret=%d\n", ret);
780 goto out;
781 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700782
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700783 /* Simply write the mask to the register */
Daniel Drake232fde02013-07-13 10:57:10 -0400784 ret = mwifiex_write_reg_locked(func, HOST_INT_MASK_REG,
785 card->reg->host_int_enable);
786 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700787 dev_err(adapter->dev, "enable host interrupt failed\n");
Daniel Drake232fde02013-07-13 10:57:10 -0400788 sdio_release_irq(func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700789 }
Daniel Drake232fde02013-07-13 10:57:10 -0400790
791out:
792 sdio_release_host(func);
793 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700794}
795
796/*
797 * This function sends a data buffer to the card.
798 */
799static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
800 u32 *type, u8 *buffer,
801 u32 npayload, u32 ioport)
802{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700803 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700804 u32 nb;
805
806 if (!buffer) {
807 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
808 return -1;
809 }
810
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700811 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700812
813 if (ret) {
814 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700815 ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700816 return -1;
817 }
818
819 nb = le16_to_cpu(*(__le16 *) (buffer));
820 if (nb > npayload) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700821 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
822 __func__, nb, npayload);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700823 return -1;
824 }
825
826 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
827
828 return ret;
829}
830
831/*
832 * This function downloads the firmware to the card.
833 *
834 * Firmware is downloaded to the card in blocks. Every block download
835 * is tested for CRC errors, and retried a number of times before
836 * returning failure.
837 */
838static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
839 struct mwifiex_fw_image *fw)
840{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700841 struct sdio_mmc_card *card = adapter->card;
842 const struct mwifiex_sdio_card_reg *reg = card->reg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700843 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700844 u8 *firmware = fw->fw_buf;
845 u32 firmware_len = fw->fw_len;
846 u32 offset = 0;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700847 u8 base0, base1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700848 u8 *fwbuf;
849 u16 len = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700850 u32 txlen, tx_blocks = 0, tries;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700851 u32 i = 0;
852
853 if (!firmware_len) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700854 dev_err(adapter->dev,
855 "firmware image not found! Terminating download\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700856 return -1;
857 }
858
859 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700860 firmware_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700861
862 /* Assume that the allocated buffer is 8-byte aligned */
863 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
Joe Perches0d2e7a52013-02-03 17:28:14 +0000864 if (!fwbuf)
Christoph Fritzb53575e2011-05-08 22:50:09 +0200865 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700866
867 /* Perform firmware data transfer */
868 do {
869 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
870 bits */
871 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
872 DN_LD_CARD_RDY);
873 if (ret) {
874 dev_err(adapter->dev, "FW download with helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700875 " poll status timeout @ %d\n", offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700876 goto done;
877 }
878
879 /* More data? */
880 if (offset >= firmware_len)
881 break;
882
883 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700884 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700885 &base0);
886 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700887 dev_err(adapter->dev,
888 "dev BASE0 register read failed: "
889 "base0=%#04X(%d). Terminating dnld\n",
890 base0, base0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700891 goto done;
892 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700893 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700894 &base1);
895 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700896 dev_err(adapter->dev,
897 "dev BASE1 register read failed: "
898 "base1=%#04X(%d). Terminating dnld\n",
899 base1, base1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700900 goto done;
901 }
902 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
903
904 if (len)
905 break;
906
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700907 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700908 }
909
910 if (!len) {
911 break;
912 } else if (len > MWIFIEX_UPLD_SIZE) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700913 dev_err(adapter->dev,
914 "FW dnld failed @ %d, invalid length %d\n",
915 offset, len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700916 ret = -1;
917 goto done;
918 }
919
920 txlen = len;
921
922 if (len & BIT(0)) {
923 i++;
924 if (i > MAX_WRITE_IOMEM_RETRY) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700925 dev_err(adapter->dev,
926 "FW dnld failed @ %d, over max retry\n",
927 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700928 ret = -1;
929 goto done;
930 }
931 dev_err(adapter->dev, "CRC indicated by the helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700932 " len = 0x%04X, txlen = %d\n", len, txlen);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700933 len &= ~BIT(0);
934 /* Setting this to 0 to resend from same offset */
935 txlen = 0;
936 } else {
937 i = 0;
938
939 /* Set blocksize to transfer - checking for last
940 block */
941 if (firmware_len - offset < txlen)
942 txlen = firmware_len - offset;
943
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700944 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
945 / MWIFIEX_SDIO_BLOCK_SIZE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700946
947 /* Copy payload to buffer */
948 memmove(fwbuf, &firmware[offset], txlen);
949 }
950
951 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
952 MWIFIEX_SDIO_BLOCK_SIZE,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700953 adapter->ioport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700954 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700955 dev_err(adapter->dev,
956 "FW download, write iomem (%d) failed @ %d\n",
957 i, offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700958 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
959 dev_err(adapter->dev, "write CFG reg failed\n");
960
961 ret = -1;
962 goto done;
963 }
964
965 offset += txlen;
966 } while (true);
967
968 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700969 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700970
971 ret = 0;
972done:
973 kfree(fwbuf);
974 return ret;
975}
976
977/*
978 * This function checks the firmware status in card.
979 *
980 * The winner interface is also determined by this function.
981 */
982static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700983 u32 poll_num)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700984{
Amitkumar Karwar05889f82013-05-17 17:50:27 -0700985 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700986 int ret = 0;
987 u16 firmware_stat;
988 u32 tries;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -0700989 u8 winner_status;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700990
991 /* Wait for firmware initialization event */
992 for (tries = 0; tries < poll_num; tries++) {
993 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
994 if (ret)
995 continue;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700996 if (firmware_stat == FIRMWARE_READY_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700997 ret = 0;
998 break;
999 } else {
Amitkumar Karwara76b20e2013-07-22 19:17:53 -07001000 msleep(100);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001001 ret = -1;
1002 }
1003 }
1004
Amitkumar Karward930fae2011-10-11 17:41:21 -07001005 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001006 if (mwifiex_read_reg
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001007 (adapter, card->reg->status_reg_0, &winner_status))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001008 winner_status = 0;
1009
1010 if (winner_status)
Amitkumar Karward930fae2011-10-11 17:41:21 -07001011 adapter->winner = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001012 else
Amitkumar Karward930fae2011-10-11 17:41:21 -07001013 adapter->winner = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001014 }
1015 return ret;
1016}
1017
1018/*
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001019 * This function decodes a received packet.
1020 *
1021 * Based on the type, the packet is treated as either a data, or
1022 * a command response, or an event, and the correct handler
1023 * function is invoked.
1024 */
1025static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1026 struct sk_buff *skb, u32 upld_typ)
1027{
1028 u8 *cmd_buf;
Avinash Patild03b4aa2013-11-05 15:01:44 -08001029 __le16 *curr_ptr = (__le16 *)skb->data;
1030 u16 pkt_len = le16_to_cpu(*curr_ptr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001031
Avinash Patild03b4aa2013-11-05 15:01:44 -08001032 skb_trim(skb, pkt_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001033 skb_pull(skb, INTF_HEADER_LEN);
1034
1035 switch (upld_typ) {
1036 case MWIFIEX_TYPE_DATA:
1037 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1038 mwifiex_handle_rx_packet(adapter, skb);
1039 break;
1040
1041 case MWIFIEX_TYPE_CMD:
1042 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1043 /* take care of curr_cmd = NULL case */
1044 if (!adapter->curr_cmd) {
1045 cmd_buf = adapter->upld_buf;
1046
1047 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1048 mwifiex_process_sleep_confirm_resp(adapter,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001049 skb->data,
1050 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001051
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001052 memcpy(cmd_buf, skb->data,
1053 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1054 skb->len));
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001055
1056 dev_kfree_skb_any(skb);
1057 } else {
1058 adapter->cmd_resp_received = true;
1059 adapter->curr_cmd->resp_skb = skb;
1060 }
1061 break;
1062
1063 case MWIFIEX_TYPE_EVENT:
1064 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
Tobias Waldekranzcfcd9262013-08-26 09:18:06 +02001065 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001066
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001067 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
Amitkumar Karware80c81d2012-06-20 20:21:12 -07001068 memcpy(adapter->event_body,
1069 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1070 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001071
1072 /* event cause has been saved to adapter->event_cause */
1073 adapter->event_received = true;
1074 adapter->event_skb = skb;
1075
1076 break;
1077
1078 default:
1079 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1080 dev_kfree_skb_any(skb);
1081 break;
1082 }
1083
1084 return 0;
1085}
1086
1087/*
1088 * This function transfers received packets from card to driver, performing
1089 * aggregation if required.
1090 *
1091 * For data received on control port, or if aggregation is disabled, the
1092 * received buffers are uploaded as separate packets. However, if aggregation
1093 * is enabled and required, the buffers are copied onto an aggregation buffer,
1094 * provided there is space left, processed and finally uploaded.
1095 */
1096static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1097 struct sk_buff *skb, u8 port)
1098{
1099 struct sdio_mmc_card *card = adapter->card;
1100 s32 f_do_rx_aggr = 0;
1101 s32 f_do_rx_cur = 0;
1102 s32 f_aggr_cur = 0;
1103 struct sk_buff *skb_deaggr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001104 u32 pind;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001105 u32 pkt_len, pkt_type, mport;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001106 u8 *curr_ptr;
1107 u32 rx_len = skb->len;
1108
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001109 if ((card->has_control_mask) && (port == CTRL_PORT)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001110 /* Read the command Resp without aggr */
1111 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001112 "response\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001113
1114 f_do_rx_cur = 1;
1115 goto rx_curr_single;
1116 }
1117
1118 if (!card->mpa_rx.enabled) {
1119 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001120 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001121
1122 f_do_rx_cur = 1;
1123 goto rx_curr_single;
1124 }
1125
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001126 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1127 card->reg->data_port_mask)) ||
1128 (card->has_control_mask && (card->mp_rd_bitmap &
1129 (~((u32) CTRL_PORT_MASK))))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001130 /* Some more data RX pending */
1131 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1132
1133 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1134 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1135 f_aggr_cur = 1;
1136 } else {
1137 /* No room in Aggr buf, do rx aggr now */
1138 f_do_rx_aggr = 1;
1139 f_do_rx_cur = 1;
1140 }
1141 } else {
1142 /* Rx aggr not in progress */
1143 f_aggr_cur = 1;
1144 }
1145
1146 } else {
1147 /* No more data RX pending */
1148 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1149
1150 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1151 f_do_rx_aggr = 1;
1152 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1153 f_aggr_cur = 1;
1154 else
1155 /* No room in Aggr buf, do rx aggr now */
1156 f_do_rx_cur = 1;
1157 } else {
1158 f_do_rx_cur = 1;
1159 }
1160 }
1161
1162 if (f_aggr_cur) {
1163 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1164 /* Curr pkt can be aggregated */
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001165 mp_rx_aggr_setup(card, skb, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001166
1167 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001168 mp_rx_aggr_port_limit_reached(card)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001169 dev_dbg(adapter->dev, "info: %s: aggregated packet "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001170 "limit reached\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001171 /* No more pkts allowed in Aggr buf, rx it */
1172 f_do_rx_aggr = 1;
1173 }
1174 }
1175
1176 if (f_do_rx_aggr) {
1177 /* do aggr RX now */
1178 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001179 card->mpa_rx.pkt_cnt);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001180
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001181 if (card->supports_sdio_new_mode) {
1182 int i;
1183 u32 port_count;
1184
1185 for (i = 0, port_count = 0; i < card->max_ports; i++)
1186 if (card->mpa_rx.ports & BIT(i))
1187 port_count++;
1188
1189 /* Reading data from "start_port + 0" to "start_port +
1190 * port_count -1", so decrease the count by 1
1191 */
1192 port_count--;
1193 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1194 (port_count << 8)) + card->mpa_rx.start_port;
1195 } else {
1196 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1197 (card->mpa_rx.ports << 4)) +
1198 card->mpa_rx.start_port;
1199 }
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001200
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001201 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001202 card->mpa_rx.buf_len, mport, 1))
Avinash Patil17a60b42011-12-08 20:41:04 -08001203 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001204
1205 curr_ptr = card->mpa_rx.buf;
1206
1207 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1208
1209 /* get curr PKT len & type */
Tobias Waldekranzcfcd9262013-08-26 09:18:06 +02001210 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1211 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001212
1213 /* copy pkt to deaggr buf */
1214 skb_deaggr = card->mpa_rx.skb_arr[pind];
1215
1216 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1217 card->mpa_rx.len_arr[pind])) {
1218
1219 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1220
1221 skb_trim(skb_deaggr, pkt_len);
1222
1223 /* Process de-aggr packet */
1224 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1225 pkt_type);
1226 } else {
1227 dev_err(adapter->dev, "wrong aggr pkt:"
1228 " type=%d len=%d max_len=%d\n",
1229 pkt_type, pkt_len,
1230 card->mpa_rx.len_arr[pind]);
1231 dev_kfree_skb_any(skb_deaggr);
1232 }
1233 curr_ptr += card->mpa_rx.len_arr[pind];
1234 }
1235 MP_RX_AGGR_BUF_RESET(card);
1236 }
1237
1238rx_curr_single:
1239 if (f_do_rx_cur) {
1240 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1241 port, rx_len);
1242
1243 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1244 skb->data, skb->len,
1245 adapter->ioport + port))
Avinash Patil17a60b42011-12-08 20:41:04 -08001246 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001247
1248 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1249 }
1250
1251 return 0;
Avinash Patil17a60b42011-12-08 20:41:04 -08001252
1253error:
1254 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1255 /* Multiport-aggregation transfer failed - cleanup */
1256 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1257 /* copy pkt to deaggr buf */
1258 skb_deaggr = card->mpa_rx.skb_arr[pind];
1259 dev_kfree_skb_any(skb_deaggr);
1260 }
1261 MP_RX_AGGR_BUF_RESET(card);
1262 }
1263
1264 if (f_do_rx_cur)
1265 /* Single transfer pending. Free curr buff also */
1266 dev_kfree_skb_any(skb);
1267
1268 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001269}
1270
1271/*
1272 * This function checks the current interrupt status.
1273 *
1274 * The following interrupts are checked and handled by this function -
1275 * - Data sent
1276 * - Command sent
1277 * - Packets received
1278 *
1279 * Since the firmware does not generate download ready interrupt if the
1280 * port updated is command port only, command sent interrupt checking
1281 * should be done manually, and for every SDIO interrupt.
1282 *
1283 * In case of Rx packets received, the packets are uploaded from card to
1284 * host and processed accordingly.
1285 */
1286static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1287{
1288 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001289 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001290 int ret = 0;
1291 u8 sdio_ireg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001292 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001293 u8 port = CTRL_PORT;
1294 u32 len_reg_l, len_reg_u;
1295 u32 rx_blocks;
1296 u16 rx_len;
1297 unsigned long flags;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001298 u32 bitmap;
1299 u8 cr;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001300
1301 spin_lock_irqsave(&adapter->int_lock, flags);
1302 sdio_ireg = adapter->int_status;
1303 adapter->int_status = 0;
1304 spin_unlock_irqrestore(&adapter->int_lock, flags);
1305
1306 if (!sdio_ireg)
1307 return ret;
1308
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001309 /* Following interrupt is only for SDIO new mode */
1310 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1311 adapter->cmd_sent = false;
1312
1313 /* Following interrupt is only for SDIO new mode */
1314 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1315 u32 pkt_type;
1316
1317 /* read the len of control packet */
1318 rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1319 rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1320 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1321 if (rx_len <= INTF_HEADER_LEN ||
1322 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1323 MWIFIEX_RX_DATA_BUF_SIZE)
1324 return -1;
1325 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1326
1327 skb = dev_alloc_skb(rx_len);
1328 if (!skb)
1329 return -1;
1330
1331 skb_put(skb, rx_len);
1332
1333 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1334 skb->len, adapter->ioport |
1335 CMD_PORT_SLCT)) {
1336 dev_err(adapter->dev,
1337 "%s: failed to card_to_host", __func__);
1338 dev_kfree_skb_any(skb);
1339 goto term_cmd;
1340 }
1341
1342 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1343 (pkt_type != MWIFIEX_TYPE_EVENT))
1344 dev_err(adapter->dev,
1345 "%s:Received wrong packet on cmd port",
1346 __func__);
1347
1348 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1349 }
1350
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001351 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001352 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1353 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1354 if (card->supports_sdio_new_mode) {
1355 bitmap |=
1356 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1357 bitmap |=
1358 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1359 }
1360 card->mp_wr_bitmap = bitmap;
1361
1362 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001363 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001364 if (adapter->data_sent &&
1365 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1366 dev_dbg(adapter->dev,
1367 "info: <--- Tx DONE Interrupt --->\n");
1368 adapter->data_sent = false;
1369 }
1370 }
1371
1372 /* As firmware will not generate download ready interrupt if the port
1373 updated is command port only, cmd_sent should be done for any SDIO
1374 interrupt. */
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001375 if (card->has_control_mask && adapter->cmd_sent) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001376 /* Check if firmware has attach buffer at command port and
1377 update just that in wr_bit_map. */
1378 card->mp_wr_bitmap |=
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001379 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001380 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1381 adapter->cmd_sent = false;
1382 }
1383
1384 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001385 adapter->cmd_sent, adapter->data_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001386 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001387 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1388 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1389 if (card->supports_sdio_new_mode) {
1390 bitmap |=
1391 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1392 bitmap |=
1393 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1394 }
1395 card->mp_rd_bitmap = bitmap;
1396 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001397 card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001398
1399 while (true) {
1400 ret = mwifiex_get_rd_port(adapter, &port);
1401 if (ret) {
1402 dev_dbg(adapter->dev,
1403 "info: no more rd_port available\n");
1404 break;
1405 }
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001406 len_reg_l = reg->rd_len_p0_l + (port << 1);
1407 len_reg_u = reg->rd_len_p0_u + (port << 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001408 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1409 rx_len |= (u16) card->mp_regs[len_reg_l];
1410 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001411 port, rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001412 rx_blocks =
1413 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1414 1) / MWIFIEX_SDIO_BLOCK_SIZE;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001415 if (rx_len <= INTF_HEADER_LEN ||
1416 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1417 MWIFIEX_RX_DATA_BUF_SIZE) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001418 dev_err(adapter->dev, "invalid rx_len=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001419 rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001420 return -1;
1421 }
1422 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1423
1424 skb = dev_alloc_skb(rx_len);
1425
1426 if (!skb) {
1427 dev_err(adapter->dev, "%s: failed to alloc skb",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001428 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001429 return -1;
1430 }
1431
1432 skb_put(skb, rx_len);
1433
1434 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001435 rx_len, skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001436
1437 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1438 port)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001439 dev_err(adapter->dev, "card_to_host_mpa failed:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001440 " int status=%#x\n", sdio_ireg);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001441 goto term_cmd;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001442 }
1443 }
1444 }
1445
1446 return 0;
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001447
1448term_cmd:
1449 /* terminate cmd */
1450 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1451 dev_err(adapter->dev, "read CFG reg failed\n");
1452 else
1453 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1454
1455 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1456 dev_err(adapter->dev, "write CFG reg failed\n");
1457 else
1458 dev_dbg(adapter->dev, "info: write success\n");
1459
1460 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1461 dev_err(adapter->dev, "read CFG reg failed\n");
1462 else
1463 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1464
1465 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001466}
1467
1468/*
1469 * This function aggregates transmission buffers in driver and downloads
1470 * the aggregated packet to card.
1471 *
1472 * The individual packets are aggregated by copying into an aggregation
1473 * buffer and then downloaded to the card. Previous unsent packets in the
1474 * aggregation buffer are pre-copied first before new packets are added.
1475 * Aggregation is done till there is space left in the aggregation buffer,
1476 * or till new packets are available.
1477 *
1478 * The function will only download the packet to the card when aggregation
1479 * stops, otherwise it will just aggregate the packet in aggregation buffer
1480 * and return.
1481 */
1482static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001483 u8 *payload, u32 pkt_len, u32 port,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001484 u32 next_pkt_len)
1485{
1486 struct sdio_mmc_card *card = adapter->card;
1487 int ret = 0;
1488 s32 f_send_aggr_buf = 0;
1489 s32 f_send_cur_buf = 0;
1490 s32 f_precopy_cur_buf = 0;
1491 s32 f_postcopy_cur_buf = 0;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001492 u32 mport;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001493
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001494 if (!card->mpa_tx.enabled ||
1495 (card->has_control_mask && (port == CTRL_PORT)) ||
1496 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001497 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001498 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001499
1500 f_send_cur_buf = 1;
1501 goto tx_curr_single;
1502 }
1503
1504 if (next_pkt_len) {
1505 /* More pkt in TX queue */
1506 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001507 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001508
1509 if (MP_TX_AGGR_IN_PROGRESS(card)) {
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001510 if (!mp_tx_aggr_port_limit_reached(card) &&
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001511 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1512 f_precopy_cur_buf = 1;
1513
1514 if (!(card->mp_wr_bitmap &
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001515 (1 << card->curr_wr_port)) ||
1516 !MP_TX_AGGR_BUF_HAS_ROOM(
1517 card, pkt_len + next_pkt_len))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001518 f_send_aggr_buf = 1;
1519 } else {
1520 /* No room in Aggr buf, send it */
1521 f_send_aggr_buf = 1;
1522
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001523 if (mp_tx_aggr_port_limit_reached(card) ||
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001524 !(card->mp_wr_bitmap &
1525 (1 << card->curr_wr_port)))
1526 f_send_cur_buf = 1;
1527 else
1528 f_postcopy_cur_buf = 1;
1529 }
1530 } else {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001531 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1532 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001533 f_precopy_cur_buf = 1;
1534 else
1535 f_send_cur_buf = 1;
1536 }
1537 } else {
1538 /* Last pkt in TX queue */
1539 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001540 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001541
1542 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1543 /* some packs in Aggr buf already */
1544 f_send_aggr_buf = 1;
1545
1546 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1547 f_precopy_cur_buf = 1;
1548 else
1549 /* No room in Aggr buf, send it */
1550 f_send_cur_buf = 1;
1551 } else {
1552 f_send_cur_buf = 1;
1553 }
1554 }
1555
1556 if (f_precopy_cur_buf) {
1557 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001558 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001559 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1560
1561 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001562 mp_tx_aggr_port_limit_reached(card))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001563 /* No more pkts allowed in Aggr buf, send it */
1564 f_send_aggr_buf = 1;
1565 }
1566
1567 if (f_send_aggr_buf) {
1568 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001569 __func__,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001570 card->mpa_tx.start_port, card->mpa_tx.ports);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001571 if (card->supports_sdio_new_mode) {
1572 u32 port_count;
1573 int i;
1574
1575 for (i = 0, port_count = 0; i < card->max_ports; i++)
1576 if (card->mpa_tx.ports & BIT(i))
1577 port_count++;
1578
1579 /* Writing data from "start_port + 0" to "start_port +
1580 * port_count -1", so decrease the count by 1
1581 */
1582 port_count--;
1583 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1584 (port_count << 8)) + card->mpa_tx.start_port;
1585 } else {
1586 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1587 (card->mpa_tx.ports << 4)) +
1588 card->mpa_tx.start_port;
1589 }
1590
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001591 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001592 card->mpa_tx.buf_len, mport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001593
1594 MP_TX_AGGR_BUF_RESET(card);
1595 }
1596
1597tx_curr_single:
1598 if (f_send_cur_buf) {
1599 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001600 __func__, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001601 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1602 adapter->ioport + port);
1603 }
1604
1605 if (f_postcopy_cur_buf) {
1606 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001607 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001608 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1609 }
1610
1611 return ret;
1612}
1613
1614/*
1615 * This function downloads data from driver to card.
1616 *
1617 * Both commands and data packets are transferred to the card by this
1618 * function.
1619 *
1620 * This function adds the SDIO specific header to the front of the buffer
1621 * before transferring. The header contains the length of the packet and
1622 * the type. The firmware handles the packets based upon this set type.
1623 */
1624static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001625 u8 type, struct sk_buff *skb,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001626 struct mwifiex_tx_param *tx_param)
1627{
1628 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001629 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001630 u32 buf_block_len;
1631 u32 blk_size;
Amitkumar Karwar0fc0d432013-05-17 17:54:23 -07001632 u32 port = CTRL_PORT;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001633 u8 *payload = (u8 *)skb->data;
1634 u32 pkt_len = skb->len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001635
1636 /* Allocate buffer and copy payload */
1637 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1638 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
Tomasz Moń83e612f2013-07-23 07:42:49 +02001639 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1640 *(__le16 *)&payload[2] = cpu_to_le16(type);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001641
1642 /*
1643 * This is SDIO specific header
1644 * u16 length,
1645 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1646 * MWIFIEX_TYPE_EVENT = 3)
1647 */
1648 if (type == MWIFIEX_TYPE_DATA) {
1649 ret = mwifiex_get_wr_port_data(adapter, &port);
1650 if (ret) {
1651 dev_err(adapter->dev, "%s: no wr_port available\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001652 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001653 return ret;
1654 }
1655 } else {
1656 adapter->cmd_sent = true;
1657 /* Type must be MWIFIEX_TYPE_CMD */
1658
1659 if (pkt_len <= INTF_HEADER_LEN ||
1660 pkt_len > MWIFIEX_UPLD_SIZE)
1661 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001662 __func__, payload, pkt_len);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001663
1664 if (card->supports_sdio_new_mode)
1665 port = CMD_PORT_SLCT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001666 }
1667
1668 /* Transfer data to card */
1669 pkt_len = buf_block_len * blk_size;
1670
1671 if (tx_param)
1672 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001673 port, tx_param->next_pkt_len
1674 );
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001675 else
1676 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001677 port, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001678
1679 if (ret) {
1680 if (type == MWIFIEX_TYPE_CMD)
1681 adapter->cmd_sent = false;
1682 if (type == MWIFIEX_TYPE_DATA)
1683 adapter->data_sent = false;
1684 } else {
1685 if (type == MWIFIEX_TYPE_DATA) {
1686 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1687 adapter->data_sent = true;
1688 else
1689 adapter->data_sent = false;
1690 }
1691 }
1692
1693 return ret;
1694}
1695
1696/*
1697 * This function allocates the MPA Tx and Rx buffers.
1698 */
1699static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1700 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1701{
1702 struct sdio_mmc_card *card = adapter->card;
1703 int ret = 0;
1704
1705 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1706 if (!card->mpa_tx.buf) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001707 ret = -1;
1708 goto error;
1709 }
1710
1711 card->mpa_tx.buf_size = mpa_tx_buf_size;
1712
1713 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1714 if (!card->mpa_rx.buf) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001715 ret = -1;
1716 goto error;
1717 }
1718
1719 card->mpa_rx.buf_size = mpa_rx_buf_size;
1720
1721error:
1722 if (ret) {
1723 kfree(card->mpa_tx.buf);
1724 kfree(card->mpa_rx.buf);
1725 }
1726
1727 return ret;
1728}
1729
1730/*
1731 * This function unregisters the SDIO device.
1732 *
1733 * The SDIO IRQ is released, the function is disabled and driver
1734 * data is set to null.
1735 */
1736static void
1737mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1738{
1739 struct sdio_mmc_card *card = adapter->card;
1740
1741 if (adapter->card) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001742 sdio_claim_host(card->func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001743 sdio_disable_func(card->func);
1744 sdio_release_host(card->func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001745 }
1746}
1747
1748/*
1749 * This function registers the SDIO device.
1750 *
1751 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1752 */
1753static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1754{
Daniel Drake232fde02013-07-13 10:57:10 -04001755 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001756 struct sdio_mmc_card *card = adapter->card;
1757 struct sdio_func *func = card->func;
1758
1759 /* save adapter pointer in card */
1760 card->adapter = adapter;
Amitkumar Karwar828cf222014-02-27 19:35:13 -08001761 adapter->tx_buf_size = card->tx_buf_size;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001762
1763 sdio_claim_host(func);
1764
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001765 /* Set block size */
1766 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
Daniel Drake232fde02013-07-13 10:57:10 -04001767 sdio_release_host(func);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001768 if (ret) {
1769 pr_err("cannot set SDIO block size\n");
Daniel Drake232fde02013-07-13 10:57:10 -04001770 return ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001771 }
1772
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001773
1774 adapter->dev = &func->dev;
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001775
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001776 strcpy(adapter->fw_name, card->firmware);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001777
1778 return 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001779}
1780
1781/*
1782 * This function initializes the SDIO driver.
1783 *
1784 * The following initializations steps are followed -
1785 * - Read the Host interrupt status register to acknowledge
1786 * the first interrupt got from bootloader
1787 * - Disable host interrupt mask register
1788 * - Get SDIO port
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001789 * - Initialize SDIO variables in card
1790 * - Allocate MP registers
1791 * - Allocate MPA Tx and Rx buffers
1792 */
1793static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1794{
1795 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001796 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001797 int ret;
Amitkumar Karwarab93d4f2013-05-17 17:53:42 -07001798 u8 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001799
Amitkumar Karwar3c59e322013-11-14 19:10:41 -08001800 sdio_set_drvdata(card->func, card);
1801
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001802 /*
1803 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1804 * from the bootloader. If we don't do this we get a interrupt
1805 * as soon as we register the irq.
1806 */
1807 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1808
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001809 /* Get SDIO ioport */
1810 mwifiex_init_sdio_ioport(adapter);
1811
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001812 /* Initialize SDIO variables in card */
1813 card->mp_rd_bitmap = 0;
1814 card->mp_wr_bitmap = 0;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001815 card->curr_rd_port = reg->start_rd_port;
1816 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001817
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001818 card->mp_data_port_mask = reg->data_port_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001819
1820 card->mpa_tx.buf_len = 0;
1821 card->mpa_tx.pkt_cnt = 0;
1822 card->mpa_tx.start_port = 0;
1823
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001824 card->mpa_tx.enabled = 1;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001825 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001826
1827 card->mpa_rx.buf_len = 0;
1828 card->mpa_rx.pkt_cnt = 0;
1829 card->mpa_rx.start_port = 0;
1830
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001831 card->mpa_rx.enabled = 1;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001832 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001833
1834 /* Allocate buffers for SDIO MP-A */
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001835 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
Joe Perches0d2e7a52013-02-03 17:28:14 +00001836 if (!card->mp_regs)
Christoph Fritzb53575e2011-05-08 22:50:09 +02001837 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001838
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001839 /* Allocate skb pointer buffers */
1840 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1841 card->mp_agg_pkt_limit, GFP_KERNEL);
1842 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1843 card->mp_agg_pkt_limit, GFP_KERNEL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001844 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1845 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1846 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1847 if (ret) {
1848 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1849 kfree(card->mp_regs);
1850 return -1;
1851 }
1852
1853 return ret;
1854}
1855
1856/*
1857 * This function resets the MPA Tx and Rx buffers.
1858 */
1859static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1860{
1861 struct sdio_mmc_card *card = adapter->card;
1862
1863 MP_TX_AGGR_BUF_RESET(card);
1864 MP_RX_AGGR_BUF_RESET(card);
1865}
1866
1867/*
1868 * This function cleans up the allocated card buffers.
1869 *
1870 * The following are freed by this function -
1871 * - MP registers
1872 * - MPA Tx buffer
1873 * - MPA Rx buffer
1874 */
1875static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1876{
1877 struct sdio_mmc_card *card = adapter->card;
1878
1879 kfree(card->mp_regs);
Amitkumar Karwarc23b7c82013-05-17 17:54:51 -07001880 kfree(card->mpa_rx.skb_arr);
1881 kfree(card->mpa_rx.len_arr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001882 kfree(card->mpa_tx.buf);
1883 kfree(card->mpa_rx.buf);
Amitkumar Karwar3c59e322013-11-14 19:10:41 -08001884 sdio_set_drvdata(card->func, NULL);
1885 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001886}
1887
1888/*
1889 * This function updates the MP end port in card.
1890 */
1891static void
1892mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1893{
1894 struct sdio_mmc_card *card = adapter->card;
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001895 const struct mwifiex_sdio_card_reg *reg = card->reg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001896 int i;
1897
1898 card->mp_end_port = port;
1899
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001900 card->mp_data_port_mask = reg->data_port_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001901
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07001902 if (reg->start_wr_port) {
1903 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1904 card->mp_data_port_mask &=
1905 ~(1 << (card->max_ports - i));
1906 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001907
Amitkumar Karwar05889f82013-05-17 17:50:27 -07001908 card->curr_wr_port = reg->start_wr_port;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001909
1910 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001911 port, card->mp_data_port_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001912}
1913
Amitkumar Karward31ab352012-11-01 18:44:14 -07001914static struct mmc_host *reset_host;
1915static void sdio_card_reset_worker(struct work_struct *work)
1916{
Tejun Heo7f5855c2012-12-21 17:56:57 -08001917 struct mmc_host *target = reset_host;
1918
Amitkumar Karward31ab352012-11-01 18:44:14 -07001919 /* The actual reset operation must be run outside of driver thread.
1920 * This is because mmc_remove_host() will cause the device to be
1921 * instantly destroyed, and the driver then needs to end its thread,
1922 * leading to a deadlock.
1923 *
1924 * We run it in a totally independent workqueue.
1925 */
1926
1927 pr_err("Resetting card...\n");
Tejun Heo7f5855c2012-12-21 17:56:57 -08001928 mmc_remove_host(target);
Amitkumar Karward31ab352012-11-01 18:44:14 -07001929 /* 20ms delay is based on experiment with sdhci controller */
1930 mdelay(20);
Tejun Heo7f5855c2012-12-21 17:56:57 -08001931 mmc_add_host(target);
Amitkumar Karward31ab352012-11-01 18:44:14 -07001932}
1933static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1934
1935/* This function resets the card */
1936static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1937{
1938 struct sdio_mmc_card *card = adapter->card;
1939
Amitkumar Karward31ab352012-11-01 18:44:14 -07001940 reset_host = card->func->card->host;
1941 schedule_work(&card_reset_work);
1942}
1943
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001944static struct mwifiex_if_ops sdio_ops = {
1945 .init_if = mwifiex_init_sdio,
1946 .cleanup_if = mwifiex_cleanup_sdio,
1947 .check_fw_status = mwifiex_check_fw_status,
1948 .prog_fw = mwifiex_prog_fw_w_helper,
1949 .register_dev = mwifiex_register_dev,
1950 .unregister_dev = mwifiex_unregister_dev,
1951 .enable_int = mwifiex_sdio_enable_host_int,
Daniel Drake232fde02013-07-13 10:57:10 -04001952 .disable_int = mwifiex_sdio_disable_host_int,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001953 .process_int_status = mwifiex_process_int_status,
1954 .host_to_card = mwifiex_sdio_host_to_card,
1955 .wakeup = mwifiex_pm_wakeup_card,
1956 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1957
1958 /* SDIO specific */
1959 .update_mp_end_port = mwifiex_update_mp_end_port,
1960 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001961 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1962 .event_complete = mwifiex_sdio_event_complete,
Amitkumar Karward31ab352012-11-01 18:44:14 -07001963 .card_reset = mwifiex_sdio_card_reset,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001964};
1965
1966/*
1967 * This function initializes the SDIO driver.
1968 *
1969 * This initiates the semaphore and registers the device with
1970 * SDIO bus.
1971 */
1972static int
1973mwifiex_sdio_init_module(void)
1974{
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001975 sema_init(&add_remove_card_sem, 1);
1976
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301977 /* Clear the flag in case user removes the card. */
1978 user_rmmod = 0;
1979
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001980 return sdio_register_driver(&mwifiex_sdio);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001981}
1982
1983/*
1984 * This function cleans up the SDIO driver.
1985 *
1986 * The following major steps are followed for cleanup -
1987 * - Resume the device if its suspended
1988 * - Disconnect the device if connected
1989 * - Shutdown the firmware
1990 * - Unregister the device from SDIO bus.
1991 */
1992static void
1993mwifiex_sdio_cleanup_module(void)
1994{
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301995 if (!down_interruptible(&add_remove_card_sem))
1996 up(&add_remove_card_sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001997
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301998 /* Set the flag as user is removing this module. */
1999 user_rmmod = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07002000
Amitkumar Karward31ab352012-11-01 18:44:14 -07002001 cancel_work_sync(&card_reset_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07002002 sdio_unregister_driver(&mwifiex_sdio);
2003}
2004
2005module_init(mwifiex_sdio_init_module);
2006module_exit(mwifiex_sdio_cleanup_module);
2007
2008MODULE_AUTHOR("Marvell International Ltd.");
2009MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2010MODULE_VERSION(SDIO_VERSION);
2011MODULE_LICENSE("GPL v2");
WarheadsSE98e6b9d2012-04-24 15:57:21 -04002012MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08002013MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2014MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
Yogesh Ashok Powarb60186f2013-05-17 17:54:58 -07002015MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);