blob: e35b67a9e6a64a18b8dc4fb5570e0b1fc84d5c7e [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
Amitkumar Karwar287546d2011-06-08 20:39:20 +053053static int mwifiex_sdio_resume(struct device *dev);
54
Bing Zhao5e6e3a92011-03-21 18:00:50 -070055/*
56 * SDIO probe.
57 *
58 * This function probes an mwifiex device and registers it. It allocates
59 * the card structure, enables SDIO function number and initiates the
60 * device registration and initialization procedure by adding a logical
61 * interface.
62 */
63static int
64mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
65{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -070066 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070067 struct sdio_mmc_card *card = NULL;
68
69 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -070070 func->vendor, func->device, func->class, func->num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070071
72 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +000073 if (!card)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070074 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070075
76 card->func = func;
77
78 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
79
80 sdio_claim_host(func);
81 ret = sdio_enable_func(func);
82 sdio_release_host(func);
83
84 if (ret) {
85 pr_err("%s: failed to enable function\n", __func__);
Christoph Fritzb53575e2011-05-08 22:50:09 +020086 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070087 return -EIO;
88 }
89
Amitkumar Karward930fae2011-10-11 17:41:21 -070090 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
91 MWIFIEX_SDIO)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -070092 pr_err("%s: add card failed\n", __func__);
93 kfree(card);
94 sdio_claim_host(func);
95 ret = sdio_disable_func(func);
96 sdio_release_host(func);
97 ret = -1;
98 }
99
100 return ret;
101}
102
103/*
104 * SDIO remove.
105 *
106 * This function removes the interface and frees up the card structure.
107 */
108static void
109mwifiex_sdio_remove(struct sdio_func *func)
110{
111 struct sdio_mmc_card *card;
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530112 struct mwifiex_adapter *adapter;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700113 struct mwifiex_private *priv;
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530114 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700115
116 pr_debug("info: SDIO func num=%d\n", func->num);
117
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530118 card = sdio_get_drvdata(func);
119 if (!card)
120 return;
121
122 adapter = card->adapter;
123 if (!adapter || !adapter->priv_num)
124 return;
125
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700126 /* In case driver is removed when asynchronous FW load is in progress */
127 wait_for_completion(&adapter->fw_load);
128
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530129 if (user_rmmod) {
130 if (adapter->is_suspended)
131 mwifiex_sdio_resume(adapter->dev);
132
133 for (i = 0; i < adapter->priv_num; i++)
134 if ((GET_BSS_ROLE(adapter->priv[i]) ==
135 MWIFIEX_BSS_ROLE_STA) &&
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700136 adapter->priv[i]->media_connected)
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530137 mwifiex_deauthenticate(adapter->priv[i], NULL);
138
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700139 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
140 mwifiex_disable_auto_ds(priv);
141 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700142 }
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530143
144 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
145 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700146}
147
148/*
149 * SDIO suspend.
150 *
151 * Kernel needs to suspend all functions separately. Therefore all
152 * registered functions must have drivers with suspend and resume
153 * methods. Failing that the kernel simply removes the whole card.
154 *
155 * If already not suspended, this function allocates and sends a host
156 * sleep activate request to the firmware and turns off the traffic.
157 */
158static int mwifiex_sdio_suspend(struct device *dev)
159{
160 struct sdio_func *func = dev_to_sdio_func(dev);
161 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700162 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700163 mmc_pm_flag_t pm_flag = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700164 int i;
165 int ret = 0;
166
167 if (func) {
168 pm_flag = sdio_get_host_pm_caps(func);
169 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700170 sdio_func_id(func), pm_flag);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700171 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
172 pr_err("%s: cannot remain alive while host is"
173 " suspended\n", sdio_func_id(func));
174 return -ENOSYS;
175 }
176
177 card = sdio_get_drvdata(func);
178 if (!card || !card->adapter) {
179 pr_err("suspend: invalid card or adapter\n");
180 return 0;
181 }
182 } else {
183 pr_err("suspend: sdio_func is not specified\n");
184 return 0;
185 }
186
187 adapter = card->adapter;
188
189 /* Enable the Host Sleep */
Bing Zhaodd321ac2012-11-15 15:58:48 -0800190 if (!mwifiex_enable_hs(adapter)) {
191 dev_err(adapter->dev, "cmd: failed to suspend\n");
192 return -EFAULT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700193 }
194
Bing Zhaodd321ac2012-11-15 15:58:48 -0800195 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
196 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
197
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700198 /* Indicate device suspended */
199 adapter->is_suspended = true;
200
201 for (i = 0; i < adapter->priv_num; i++)
202 netif_carrier_off(adapter->priv[i]->netdev);
203
204 return ret;
205}
206
207/*
208 * SDIO resume.
209 *
210 * Kernel needs to suspend all functions separately. Therefore all
211 * registered functions must have drivers with suspend and resume
212 * methods. Failing that the kernel simply removes the whole card.
213 *
214 * If already not resumed, this function turns on the traffic and
215 * sends a host sleep cancel request to the firmware.
216 */
217static int mwifiex_sdio_resume(struct device *dev)
218{
219 struct sdio_func *func = dev_to_sdio_func(dev);
220 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700221 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700222 mmc_pm_flag_t pm_flag = 0;
223 int i;
224
225 if (func) {
226 pm_flag = sdio_get_host_pm_caps(func);
227 card = sdio_get_drvdata(func);
228 if (!card || !card->adapter) {
229 pr_err("resume: invalid card or adapter\n");
230 return 0;
231 }
232 } else {
233 pr_err("resume: sdio_func is not specified\n");
234 return 0;
235 }
236
237 adapter = card->adapter;
238
239 if (!adapter->is_suspended) {
240 dev_warn(adapter->dev, "device already resumed\n");
241 return 0;
242 }
243
244 adapter->is_suspended = false;
245
246 for (i = 0; i < adapter->priv_num; i++)
247 if (adapter->priv[i]->media_connected)
248 netif_carrier_on(adapter->priv[i]->netdev);
249
250 /* Disable Host Sleep */
251 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700252 MWIFIEX_ASYNC_CMD);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700253
254 return 0;
255}
256
WarheadsSE98e6b9d2012-04-24 15:57:21 -0400257/* Device ID for SD8786 */
258#define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700259/* Device ID for SD8787 */
260#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
Bing Zhaoe3bea1c2011-11-16 20:40:35 -0800261/* Device ID for SD8797 */
262#define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700263
264/* WLAN IDs */
265static const struct sdio_device_id mwifiex_ids[] = {
WarheadsSE98e6b9d2012-04-24 15:57:21 -0400266 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786)},
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700267 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
Bing Zhaoe3bea1c2011-11-16 20:40:35 -0800268 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797)},
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700269 {},
270};
271
272MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
273
274static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
275 .suspend = mwifiex_sdio_suspend,
276 .resume = mwifiex_sdio_resume,
277};
278
279static struct sdio_driver mwifiex_sdio = {
280 .name = "mwifiex_sdio",
281 .id_table = mwifiex_ids,
282 .probe = mwifiex_sdio_probe,
283 .remove = mwifiex_sdio_remove,
284 .drv = {
285 .owner = THIS_MODULE,
286 .pm = &mwifiex_sdio_pm_ops,
287 }
288};
289
290/*
291 * This function writes data into SDIO card register.
292 */
293static int
294mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
295{
296 struct sdio_mmc_card *card = adapter->card;
297 int ret = -1;
298
299 sdio_claim_host(card->func);
300 sdio_writeb(card->func, (u8) data, reg, &ret);
301 sdio_release_host(card->func);
302
303 return ret;
304}
305
306/*
307 * This function reads data from SDIO card register.
308 */
309static int
310mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
311{
312 struct sdio_mmc_card *card = adapter->card;
313 int ret = -1;
314 u8 val;
315
316 sdio_claim_host(card->func);
317 val = sdio_readb(card->func, reg, &ret);
318 sdio_release_host(card->func);
319
320 *data = val;
321
322 return ret;
323}
324
325/*
326 * This function writes multiple data into SDIO card memory.
327 *
328 * This does not work in suspended mode.
329 */
330static int
331mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700332 u8 *buffer, u32 pkt_len, u32 port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700333{
334 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800335 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700336 u8 blk_mode =
337 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
338 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
339 u32 blk_cnt =
340 (blk_mode ==
341 BLOCK_MODE) ? (pkt_len /
342 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
343 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
344
345 if (adapter->is_suspended) {
346 dev_err(adapter->dev,
347 "%s: not allowed while suspended\n", __func__);
348 return -1;
349 }
350
351 sdio_claim_host(card->func);
352
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800353 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700354
355 sdio_release_host(card->func);
356
357 return ret;
358}
359
360/*
361 * This function reads multiple data from SDIO card memory.
362 */
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700363static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
364 u32 len, u32 port, u8 claim)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700365{
366 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800367 int ret;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700368 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
369 : BLOCK_MODE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700370 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700371 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
372 : len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700373 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
374
375 if (claim)
376 sdio_claim_host(card->func);
377
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800378 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700379
380 if (claim)
381 sdio_release_host(card->func);
382
383 return ret;
384}
385
386/*
387 * This function wakes up the card.
388 *
389 * A host power up command is written to the card configuration
390 * register to wake up the card.
391 */
392static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
393{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700394 dev_dbg(adapter->dev, "event: wakeup device...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700395
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700396 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700397}
398
399/*
400 * This function is called after the card has woken up.
401 *
402 * The card configuration register is reset.
403 */
404static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
405{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700406 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700407
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700408 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700409}
410
411/*
412 * This function initializes the IO ports.
413 *
414 * The following operations are performed -
415 * - Read the IO ports (0, 1 and 2)
416 * - Set host interrupt Reset-To-Read to clear
417 * - Set auto re-enable interrupt
418 */
419static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
420{
421 u32 reg;
422
423 adapter->ioport = 0;
424
425 /* Read the IO port */
426 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
427 adapter->ioport |= (reg & 0xff);
428 else
429 return -1;
430
431 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
432 adapter->ioport |= ((reg & 0xff) << 8);
433 else
434 return -1;
435
436 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
437 adapter->ioport |= ((reg & 0xff) << 16);
438 else
439 return -1;
440
441 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
442
443 /* Set Host interrupt reset to read to clear */
444 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
445 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
446 reg | SDIO_INT_MASK);
447 else
448 return -1;
449
450 /* Dnld/Upld ready set to auto reset */
451 if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, &reg))
452 mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
453 reg | AUTO_RE_ENABLE_INT);
454 else
455 return -1;
456
457 return 0;
458}
459
460/*
461 * This function sends data to the card.
462 */
463static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
464 u8 *payload, u32 pkt_len, u32 port)
465{
466 u32 i = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700467 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700468
469 do {
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700470 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700471 if (ret) {
472 i++;
473 dev_err(adapter->dev, "host_to_card, write iomem"
474 " (%d) failed: %d\n", i, ret);
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700475 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700476 dev_err(adapter->dev, "write CFG reg failed\n");
477
478 ret = -1;
479 if (i > MAX_WRITE_IOMEM_RETRY)
480 return ret;
481 }
482 } while (ret == -1);
483
484 return ret;
485}
486
487/*
488 * This function gets the read port.
489 *
490 * If control port bit is set in MP read bitmap, the control port
491 * is returned, otherwise the current read port is returned and
492 * the value is increased (provided it does not reach the maximum
493 * limit, in which case it is reset to 1)
494 */
495static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
496{
497 struct sdio_mmc_card *card = adapter->card;
498 u16 rd_bitmap = card->mp_rd_bitmap;
499
500 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
501
502 if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
503 return -1;
504
505 if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
506 card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
507 *port = CTRL_PORT;
508 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700509 *port, card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700510 } else {
511 if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700512 card->mp_rd_bitmap &= (u16)
513 (~(1 << card->curr_rd_port));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700514 *port = card->curr_rd_port;
515
516 if (++card->curr_rd_port == MAX_PORT)
517 card->curr_rd_port = 1;
518 } else {
519 return -1;
520 }
521
522 dev_dbg(adapter->dev,
523 "data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700524 *port, rd_bitmap, card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700525 }
526 return 0;
527}
528
529/*
530 * This function gets the write port for data.
531 *
532 * The current write port is returned if available and the value is
533 * increased (provided it does not reach the maximum limit, in which
534 * case it is reset to 1)
535 */
536static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
537{
538 struct sdio_mmc_card *card = adapter->card;
539 u16 wr_bitmap = card->mp_wr_bitmap;
540
541 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
542
543 if (!(wr_bitmap & card->mp_data_port_mask))
544 return -1;
545
546 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
547 card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
548 *port = card->curr_wr_port;
549 if (++card->curr_wr_port == card->mp_end_port)
550 card->curr_wr_port = 1;
551 } else {
552 adapter->data_sent = true;
553 return -EBUSY;
554 }
555
556 if (*port == CTRL_PORT) {
557 dev_err(adapter->dev, "invalid data port=%d cur port=%d"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700558 " mp_wr_bitmap=0x%04x -> 0x%04x\n",
559 *port, card->curr_wr_port, wr_bitmap,
560 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700561 return -1;
562 }
563
564 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700565 *port, wr_bitmap, card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700566
567 return 0;
568}
569
570/*
571 * This function polls the card status.
572 */
573static int
574mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
575{
576 u32 tries;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700577 u32 cs;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700578
579 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
580 if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
581 break;
582 else if ((cs & bits) == bits)
583 return 0;
584
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700585 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700586 }
587
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700588 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
589
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700590 return -1;
591}
592
593/*
594 * This function reads the firmware status.
595 */
596static int
597mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
598{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700599 u32 fws0, fws1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700600
601 if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
602 return -1;
603
604 if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
605 return -1;
606
607 *dat = (u16) ((fws1 << 8) | fws0);
608
609 return 0;
610}
611
612/*
613 * This function disables the host interrupt.
614 *
615 * The host interrupt mask is read, the disable bit is reset and
616 * written back to the card host interrupt mask register.
617 */
618static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
619{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700620 u32 host_int_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700621
622 /* Read back the host_int_mask register */
623 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
624 return -1;
625
626 /* Update with the mask and write back to the register */
627 host_int_mask &= ~HOST_INT_DISABLE;
628
629 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
630 dev_err(adapter->dev, "disable host interrupt failed\n");
631 return -1;
632 }
633
634 return 0;
635}
636
637/*
638 * This function enables the host interrupt.
639 *
640 * The host interrupt enable mask is written to the card
641 * host interrupt mask register.
642 */
643static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
644{
645 /* Simply write the mask to the register */
646 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
647 dev_err(adapter->dev, "enable host interrupt failed\n");
648 return -1;
649 }
650 return 0;
651}
652
653/*
654 * This function sends a data buffer to the card.
655 */
656static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
657 u32 *type, u8 *buffer,
658 u32 npayload, u32 ioport)
659{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700660 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700661 u32 nb;
662
663 if (!buffer) {
664 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
665 return -1;
666 }
667
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700668 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700669
670 if (ret) {
671 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700672 ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700673 return -1;
674 }
675
676 nb = le16_to_cpu(*(__le16 *) (buffer));
677 if (nb > npayload) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700678 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
679 __func__, nb, npayload);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700680 return -1;
681 }
682
683 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
684
685 return ret;
686}
687
688/*
689 * This function downloads the firmware to the card.
690 *
691 * Firmware is downloaded to the card in blocks. Every block download
692 * is tested for CRC errors, and retried a number of times before
693 * returning failure.
694 */
695static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
696 struct mwifiex_fw_image *fw)
697{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700698 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700699 u8 *firmware = fw->fw_buf;
700 u32 firmware_len = fw->fw_len;
701 u32 offset = 0;
702 u32 base0, base1;
703 u8 *fwbuf;
704 u16 len = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700705 u32 txlen, tx_blocks = 0, tries;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700706 u32 i = 0;
707
708 if (!firmware_len) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700709 dev_err(adapter->dev,
710 "firmware image not found! Terminating download\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700711 return -1;
712 }
713
714 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700715 firmware_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700716
717 /* Assume that the allocated buffer is 8-byte aligned */
718 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
719 if (!fwbuf) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700720 dev_err(adapter->dev,
721 "unable to alloc buffer for FW. Terminating dnld\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200722 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700723 }
724
725 /* Perform firmware data transfer */
726 do {
727 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
728 bits */
729 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
730 DN_LD_CARD_RDY);
731 if (ret) {
732 dev_err(adapter->dev, "FW download with helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700733 " poll status timeout @ %d\n", offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700734 goto done;
735 }
736
737 /* More data? */
738 if (offset >= firmware_len)
739 break;
740
741 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
742 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
743 &base0);
744 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700745 dev_err(adapter->dev,
746 "dev BASE0 register read failed: "
747 "base0=%#04X(%d). Terminating dnld\n",
748 base0, base0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700749 goto done;
750 }
751 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
752 &base1);
753 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700754 dev_err(adapter->dev,
755 "dev BASE1 register read failed: "
756 "base1=%#04X(%d). Terminating dnld\n",
757 base1, base1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700758 goto done;
759 }
760 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
761
762 if (len)
763 break;
764
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700765 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700766 }
767
768 if (!len) {
769 break;
770 } else if (len > MWIFIEX_UPLD_SIZE) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700771 dev_err(adapter->dev,
772 "FW dnld failed @ %d, invalid length %d\n",
773 offset, len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700774 ret = -1;
775 goto done;
776 }
777
778 txlen = len;
779
780 if (len & BIT(0)) {
781 i++;
782 if (i > MAX_WRITE_IOMEM_RETRY) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700783 dev_err(adapter->dev,
784 "FW dnld failed @ %d, over max retry\n",
785 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700786 ret = -1;
787 goto done;
788 }
789 dev_err(adapter->dev, "CRC indicated by the helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700790 " len = 0x%04X, txlen = %d\n", len, txlen);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700791 len &= ~BIT(0);
792 /* Setting this to 0 to resend from same offset */
793 txlen = 0;
794 } else {
795 i = 0;
796
797 /* Set blocksize to transfer - checking for last
798 block */
799 if (firmware_len - offset < txlen)
800 txlen = firmware_len - offset;
801
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700802 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
803 / MWIFIEX_SDIO_BLOCK_SIZE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700804
805 /* Copy payload to buffer */
806 memmove(fwbuf, &firmware[offset], txlen);
807 }
808
809 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
810 MWIFIEX_SDIO_BLOCK_SIZE,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700811 adapter->ioport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700812 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700813 dev_err(adapter->dev,
814 "FW download, write iomem (%d) failed @ %d\n",
815 i, offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700816 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
817 dev_err(adapter->dev, "write CFG reg failed\n");
818
819 ret = -1;
820 goto done;
821 }
822
823 offset += txlen;
824 } while (true);
825
826 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700827 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700828
829 ret = 0;
830done:
831 kfree(fwbuf);
832 return ret;
833}
834
835/*
836 * This function checks the firmware status in card.
837 *
838 * The winner interface is also determined by this function.
839 */
840static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700841 u32 poll_num)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700842{
843 int ret = 0;
844 u16 firmware_stat;
845 u32 tries;
846 u32 winner_status;
847
848 /* Wait for firmware initialization event */
849 for (tries = 0; tries < poll_num; tries++) {
850 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
851 if (ret)
852 continue;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700853 if (firmware_stat == FIRMWARE_READY_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700854 ret = 0;
855 break;
856 } else {
857 mdelay(100);
858 ret = -1;
859 }
860 }
861
Amitkumar Karward930fae2011-10-11 17:41:21 -0700862 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700863 if (mwifiex_read_reg
864 (adapter, CARD_FW_STATUS0_REG, &winner_status))
865 winner_status = 0;
866
867 if (winner_status)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700868 adapter->winner = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700869 else
Amitkumar Karward930fae2011-10-11 17:41:21 -0700870 adapter->winner = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700871 }
872 return ret;
873}
874
875/*
876 * This function reads the interrupt status from card.
877 */
878static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
879{
880 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700881 u32 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700882 unsigned long flags;
883
884 if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700885 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700886 0)) {
887 dev_err(adapter->dev, "read mp_regs failed\n");
888 return;
889 }
890
891 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
892 if (sdio_ireg) {
893 /*
894 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
895 * Clear the interrupt status register
896 */
897 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
898 spin_lock_irqsave(&adapter->int_lock, flags);
899 adapter->int_status |= sdio_ireg;
900 spin_unlock_irqrestore(&adapter->int_lock, flags);
901 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700902}
903
904/*
905 * SDIO interrupt handler.
906 *
Bing Zhao601216e2012-11-05 16:59:15 -0800907 * This function reads the interrupt status from firmware and handles
908 * the interrupt in current thread (ksdioirqd) right away.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700909 */
910static void
911mwifiex_sdio_interrupt(struct sdio_func *func)
912{
913 struct mwifiex_adapter *adapter;
914 struct sdio_mmc_card *card;
915
916 card = sdio_get_drvdata(func);
917 if (!card || !card->adapter) {
918 pr_debug("int: func=%p card=%p adapter=%p\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700919 func, card, card ? card->adapter : NULL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700920 return;
921 }
922 adapter = card->adapter;
923
924 if (adapter->surprise_removed)
925 return;
926
927 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
928 adapter->ps_state = PS_STATE_AWAKE;
929
930 mwifiex_interrupt_status(adapter);
Bing Zhao601216e2012-11-05 16:59:15 -0800931 mwifiex_main_process(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700932}
933
934/*
935 * This function decodes a received packet.
936 *
937 * Based on the type, the packet is treated as either a data, or
938 * a command response, or an event, and the correct handler
939 * function is invoked.
940 */
941static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
942 struct sk_buff *skb, u32 upld_typ)
943{
944 u8 *cmd_buf;
945
946 skb_pull(skb, INTF_HEADER_LEN);
947
948 switch (upld_typ) {
949 case MWIFIEX_TYPE_DATA:
950 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
951 mwifiex_handle_rx_packet(adapter, skb);
952 break;
953
954 case MWIFIEX_TYPE_CMD:
955 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
956 /* take care of curr_cmd = NULL case */
957 if (!adapter->curr_cmd) {
958 cmd_buf = adapter->upld_buf;
959
960 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
961 mwifiex_process_sleep_confirm_resp(adapter,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700962 skb->data,
963 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700964
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700965 memcpy(cmd_buf, skb->data,
966 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
967 skb->len));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700968
969 dev_kfree_skb_any(skb);
970 } else {
971 adapter->cmd_resp_received = true;
972 adapter->curr_cmd->resp_skb = skb;
973 }
974 break;
975
976 case MWIFIEX_TYPE_EVENT:
977 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
978 adapter->event_cause = *(u32 *) skb->data;
979
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700980 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
Amitkumar Karware80c81d2012-06-20 20:21:12 -0700981 memcpy(adapter->event_body,
982 skb->data + MWIFIEX_EVENT_HEADER_LEN,
983 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700984
985 /* event cause has been saved to adapter->event_cause */
986 adapter->event_received = true;
987 adapter->event_skb = skb;
988
989 break;
990
991 default:
992 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
993 dev_kfree_skb_any(skb);
994 break;
995 }
996
997 return 0;
998}
999
1000/*
1001 * This function transfers received packets from card to driver, performing
1002 * aggregation if required.
1003 *
1004 * For data received on control port, or if aggregation is disabled, the
1005 * received buffers are uploaded as separate packets. However, if aggregation
1006 * is enabled and required, the buffers are copied onto an aggregation buffer,
1007 * provided there is space left, processed and finally uploaded.
1008 */
1009static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1010 struct sk_buff *skb, u8 port)
1011{
1012 struct sdio_mmc_card *card = adapter->card;
1013 s32 f_do_rx_aggr = 0;
1014 s32 f_do_rx_cur = 0;
1015 s32 f_aggr_cur = 0;
1016 struct sk_buff *skb_deaggr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001017 u32 pind;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001018 u32 pkt_len, pkt_type = 0;
1019 u8 *curr_ptr;
1020 u32 rx_len = skb->len;
1021
1022 if (port == CTRL_PORT) {
1023 /* Read the command Resp without aggr */
1024 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001025 "response\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001026
1027 f_do_rx_cur = 1;
1028 goto rx_curr_single;
1029 }
1030
1031 if (!card->mpa_rx.enabled) {
1032 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001033 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001034
1035 f_do_rx_cur = 1;
1036 goto rx_curr_single;
1037 }
1038
1039 if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1040 /* Some more data RX pending */
1041 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1042
1043 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1044 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1045 f_aggr_cur = 1;
1046 } else {
1047 /* No room in Aggr buf, do rx aggr now */
1048 f_do_rx_aggr = 1;
1049 f_do_rx_cur = 1;
1050 }
1051 } else {
1052 /* Rx aggr not in progress */
1053 f_aggr_cur = 1;
1054 }
1055
1056 } else {
1057 /* No more data RX pending */
1058 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1059
1060 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1061 f_do_rx_aggr = 1;
1062 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1063 f_aggr_cur = 1;
1064 else
1065 /* No room in Aggr buf, do rx aggr now */
1066 f_do_rx_cur = 1;
1067 } else {
1068 f_do_rx_cur = 1;
1069 }
1070 }
1071
1072 if (f_aggr_cur) {
1073 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1074 /* Curr pkt can be aggregated */
1075 MP_RX_AGGR_SETUP(card, skb, port);
1076
1077 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1078 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1079 dev_dbg(adapter->dev, "info: %s: aggregated packet "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001080 "limit reached\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001081 /* No more pkts allowed in Aggr buf, rx it */
1082 f_do_rx_aggr = 1;
1083 }
1084 }
1085
1086 if (f_do_rx_aggr) {
1087 /* do aggr RX now */
1088 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001089 card->mpa_rx.pkt_cnt);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001090
1091 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1092 card->mpa_rx.buf_len,
1093 (adapter->ioport | 0x1000 |
1094 (card->mpa_rx.ports << 4)) +
Amitkumar Karwar572e8f32011-04-13 17:27:08 -07001095 card->mpa_rx.start_port, 1))
Avinash Patil17a60b42011-12-08 20:41:04 -08001096 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001097
1098 curr_ptr = card->mpa_rx.buf;
1099
1100 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1101
1102 /* get curr PKT len & type */
1103 pkt_len = *(u16 *) &curr_ptr[0];
1104 pkt_type = *(u16 *) &curr_ptr[2];
1105
1106 /* copy pkt to deaggr buf */
1107 skb_deaggr = card->mpa_rx.skb_arr[pind];
1108
1109 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1110 card->mpa_rx.len_arr[pind])) {
1111
1112 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1113
1114 skb_trim(skb_deaggr, pkt_len);
1115
1116 /* Process de-aggr packet */
1117 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1118 pkt_type);
1119 } else {
1120 dev_err(adapter->dev, "wrong aggr pkt:"
1121 " type=%d len=%d max_len=%d\n",
1122 pkt_type, pkt_len,
1123 card->mpa_rx.len_arr[pind]);
1124 dev_kfree_skb_any(skb_deaggr);
1125 }
1126 curr_ptr += card->mpa_rx.len_arr[pind];
1127 }
1128 MP_RX_AGGR_BUF_RESET(card);
1129 }
1130
1131rx_curr_single:
1132 if (f_do_rx_cur) {
1133 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1134 port, rx_len);
1135
1136 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1137 skb->data, skb->len,
1138 adapter->ioport + port))
Avinash Patil17a60b42011-12-08 20:41:04 -08001139 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001140
1141 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1142 }
1143
1144 return 0;
Avinash Patil17a60b42011-12-08 20:41:04 -08001145
1146error:
1147 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1148 /* Multiport-aggregation transfer failed - cleanup */
1149 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1150 /* copy pkt to deaggr buf */
1151 skb_deaggr = card->mpa_rx.skb_arr[pind];
1152 dev_kfree_skb_any(skb_deaggr);
1153 }
1154 MP_RX_AGGR_BUF_RESET(card);
1155 }
1156
1157 if (f_do_rx_cur)
1158 /* Single transfer pending. Free curr buff also */
1159 dev_kfree_skb_any(skb);
1160
1161 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001162}
1163
1164/*
1165 * This function checks the current interrupt status.
1166 *
1167 * The following interrupts are checked and handled by this function -
1168 * - Data sent
1169 * - Command sent
1170 * - Packets received
1171 *
1172 * Since the firmware does not generate download ready interrupt if the
1173 * port updated is command port only, command sent interrupt checking
1174 * should be done manually, and for every SDIO interrupt.
1175 *
1176 * In case of Rx packets received, the packets are uploaded from card to
1177 * host and processed accordingly.
1178 */
1179static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1180{
1181 struct sdio_mmc_card *card = adapter->card;
1182 int ret = 0;
1183 u8 sdio_ireg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001184 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001185 u8 port = CTRL_PORT;
1186 u32 len_reg_l, len_reg_u;
1187 u32 rx_blocks;
1188 u16 rx_len;
1189 unsigned long flags;
1190
1191 spin_lock_irqsave(&adapter->int_lock, flags);
1192 sdio_ireg = adapter->int_status;
1193 adapter->int_status = 0;
1194 spin_unlock_irqrestore(&adapter->int_lock, flags);
1195
1196 if (!sdio_ireg)
1197 return ret;
1198
1199 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1200 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1201 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1202 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001203 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001204 if (adapter->data_sent &&
1205 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1206 dev_dbg(adapter->dev,
1207 "info: <--- Tx DONE Interrupt --->\n");
1208 adapter->data_sent = false;
1209 }
1210 }
1211
1212 /* As firmware will not generate download ready interrupt if the port
1213 updated is command port only, cmd_sent should be done for any SDIO
1214 interrupt. */
1215 if (adapter->cmd_sent) {
1216 /* Check if firmware has attach buffer at command port and
1217 update just that in wr_bit_map. */
1218 card->mp_wr_bitmap |=
1219 (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1220 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1221 adapter->cmd_sent = false;
1222 }
1223
1224 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001225 adapter->cmd_sent, adapter->data_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001226 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1227 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1228 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1229 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001230 card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001231
1232 while (true) {
1233 ret = mwifiex_get_rd_port(adapter, &port);
1234 if (ret) {
1235 dev_dbg(adapter->dev,
1236 "info: no more rd_port available\n");
1237 break;
1238 }
1239 len_reg_l = RD_LEN_P0_L + (port << 1);
1240 len_reg_u = RD_LEN_P0_U + (port << 1);
1241 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1242 rx_len |= (u16) card->mp_regs[len_reg_l];
1243 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001244 port, rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001245 rx_blocks =
1246 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1247 1) / MWIFIEX_SDIO_BLOCK_SIZE;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001248 if (rx_len <= INTF_HEADER_LEN ||
1249 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1250 MWIFIEX_RX_DATA_BUF_SIZE) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001251 dev_err(adapter->dev, "invalid rx_len=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001252 rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001253 return -1;
1254 }
1255 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1256
1257 skb = dev_alloc_skb(rx_len);
1258
1259 if (!skb) {
1260 dev_err(adapter->dev, "%s: failed to alloc skb",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001261 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001262 return -1;
1263 }
1264
1265 skb_put(skb, rx_len);
1266
1267 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001268 rx_len, skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001269
1270 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1271 port)) {
1272 u32 cr = 0;
1273
1274 dev_err(adapter->dev, "card_to_host_mpa failed:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001275 " int status=%#x\n", sdio_ireg);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001276 if (mwifiex_read_reg(adapter,
1277 CONFIGURATION_REG, &cr))
1278 dev_err(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001279 "read CFG reg failed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001280
1281 dev_dbg(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001282 "info: CFG reg val = %d\n", cr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001283 if (mwifiex_write_reg(adapter,
1284 CONFIGURATION_REG,
1285 (cr | 0x04)))
1286 dev_err(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001287 "write CFG reg failed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001288
1289 dev_dbg(adapter->dev, "info: write success\n");
1290 if (mwifiex_read_reg(adapter,
1291 CONFIGURATION_REG, &cr))
1292 dev_err(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001293 "read CFG reg failed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001294
1295 dev_dbg(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001296 "info: CFG reg val =%x\n", cr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001297 return -1;
1298 }
1299 }
1300 }
1301
1302 return 0;
1303}
1304
1305/*
1306 * This function aggregates transmission buffers in driver and downloads
1307 * the aggregated packet to card.
1308 *
1309 * The individual packets are aggregated by copying into an aggregation
1310 * buffer and then downloaded to the card. Previous unsent packets in the
1311 * aggregation buffer are pre-copied first before new packets are added.
1312 * Aggregation is done till there is space left in the aggregation buffer,
1313 * or till new packets are available.
1314 *
1315 * The function will only download the packet to the card when aggregation
1316 * stops, otherwise it will just aggregate the packet in aggregation buffer
1317 * and return.
1318 */
1319static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1320 u8 *payload, u32 pkt_len, u8 port,
1321 u32 next_pkt_len)
1322{
1323 struct sdio_mmc_card *card = adapter->card;
1324 int ret = 0;
1325 s32 f_send_aggr_buf = 0;
1326 s32 f_send_cur_buf = 0;
1327 s32 f_precopy_cur_buf = 0;
1328 s32 f_postcopy_cur_buf = 0;
1329
1330 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1331 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001332 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001333
1334 f_send_cur_buf = 1;
1335 goto tx_curr_single;
1336 }
1337
1338 if (next_pkt_len) {
1339 /* More pkt in TX queue */
1340 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001341 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001342
1343 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1344 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1345 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1346 f_precopy_cur_buf = 1;
1347
1348 if (!(card->mp_wr_bitmap &
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001349 (1 << card->curr_wr_port)) ||
1350 !MP_TX_AGGR_BUF_HAS_ROOM(
1351 card, pkt_len + next_pkt_len))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001352 f_send_aggr_buf = 1;
1353 } else {
1354 /* No room in Aggr buf, send it */
1355 f_send_aggr_buf = 1;
1356
1357 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1358 !(card->mp_wr_bitmap &
1359 (1 << card->curr_wr_port)))
1360 f_send_cur_buf = 1;
1361 else
1362 f_postcopy_cur_buf = 1;
1363 }
1364 } else {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001365 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1366 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001367 f_precopy_cur_buf = 1;
1368 else
1369 f_send_cur_buf = 1;
1370 }
1371 } else {
1372 /* Last pkt in TX queue */
1373 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001374 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001375
1376 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1377 /* some packs in Aggr buf already */
1378 f_send_aggr_buf = 1;
1379
1380 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1381 f_precopy_cur_buf = 1;
1382 else
1383 /* No room in Aggr buf, send it */
1384 f_send_cur_buf = 1;
1385 } else {
1386 f_send_cur_buf = 1;
1387 }
1388 }
1389
1390 if (f_precopy_cur_buf) {
1391 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001392 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001393 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1394
1395 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1396 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1397 /* No more pkts allowed in Aggr buf, send it */
1398 f_send_aggr_buf = 1;
1399 }
1400
1401 if (f_send_aggr_buf) {
1402 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001403 __func__,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001404 card->mpa_tx.start_port, card->mpa_tx.ports);
1405 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1406 card->mpa_tx.buf_len,
1407 (adapter->ioport | 0x1000 |
1408 (card->mpa_tx.ports << 4)) +
1409 card->mpa_tx.start_port);
1410
1411 MP_TX_AGGR_BUF_RESET(card);
1412 }
1413
1414tx_curr_single:
1415 if (f_send_cur_buf) {
1416 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001417 __func__, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001418 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1419 adapter->ioport + port);
1420 }
1421
1422 if (f_postcopy_cur_buf) {
1423 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001424 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001425 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1426 }
1427
1428 return ret;
1429}
1430
1431/*
1432 * This function downloads data from driver to card.
1433 *
1434 * Both commands and data packets are transferred to the card by this
1435 * function.
1436 *
1437 * This function adds the SDIO specific header to the front of the buffer
1438 * before transferring. The header contains the length of the packet and
1439 * the type. The firmware handles the packets based upon this set type.
1440 */
1441static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001442 u8 type, struct sk_buff *skb,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001443 struct mwifiex_tx_param *tx_param)
1444{
1445 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001446 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001447 u32 buf_block_len;
1448 u32 blk_size;
1449 u8 port = CTRL_PORT;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001450 u8 *payload = (u8 *)skb->data;
1451 u32 pkt_len = skb->len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001452
1453 /* Allocate buffer and copy payload */
1454 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1455 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
1456 *(u16 *) &payload[0] = (u16) pkt_len;
1457 *(u16 *) &payload[2] = type;
1458
1459 /*
1460 * This is SDIO specific header
1461 * u16 length,
1462 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1463 * MWIFIEX_TYPE_EVENT = 3)
1464 */
1465 if (type == MWIFIEX_TYPE_DATA) {
1466 ret = mwifiex_get_wr_port_data(adapter, &port);
1467 if (ret) {
1468 dev_err(adapter->dev, "%s: no wr_port available\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001469 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001470 return ret;
1471 }
1472 } else {
1473 adapter->cmd_sent = true;
1474 /* Type must be MWIFIEX_TYPE_CMD */
1475
1476 if (pkt_len <= INTF_HEADER_LEN ||
1477 pkt_len > MWIFIEX_UPLD_SIZE)
1478 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001479 __func__, payload, pkt_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001480 }
1481
1482 /* Transfer data to card */
1483 pkt_len = buf_block_len * blk_size;
1484
1485 if (tx_param)
1486 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001487 port, tx_param->next_pkt_len
1488 );
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001489 else
1490 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001491 port, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001492
1493 if (ret) {
1494 if (type == MWIFIEX_TYPE_CMD)
1495 adapter->cmd_sent = false;
1496 if (type == MWIFIEX_TYPE_DATA)
1497 adapter->data_sent = false;
1498 } else {
1499 if (type == MWIFIEX_TYPE_DATA) {
1500 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1501 adapter->data_sent = true;
1502 else
1503 adapter->data_sent = false;
1504 }
1505 }
1506
1507 return ret;
1508}
1509
1510/*
1511 * This function allocates the MPA Tx and Rx buffers.
1512 */
1513static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1514 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1515{
1516 struct sdio_mmc_card *card = adapter->card;
1517 int ret = 0;
1518
1519 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1520 if (!card->mpa_tx.buf) {
1521 dev_err(adapter->dev, "could not alloc buffer for MP-A TX\n");
1522 ret = -1;
1523 goto error;
1524 }
1525
1526 card->mpa_tx.buf_size = mpa_tx_buf_size;
1527
1528 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1529 if (!card->mpa_rx.buf) {
1530 dev_err(adapter->dev, "could not alloc buffer for MP-A RX\n");
1531 ret = -1;
1532 goto error;
1533 }
1534
1535 card->mpa_rx.buf_size = mpa_rx_buf_size;
1536
1537error:
1538 if (ret) {
1539 kfree(card->mpa_tx.buf);
1540 kfree(card->mpa_rx.buf);
1541 }
1542
1543 return ret;
1544}
1545
1546/*
1547 * This function unregisters the SDIO device.
1548 *
1549 * The SDIO IRQ is released, the function is disabled and driver
1550 * data is set to null.
1551 */
1552static void
1553mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1554{
1555 struct sdio_mmc_card *card = adapter->card;
1556
1557 if (adapter->card) {
1558 /* Release the SDIO IRQ */
1559 sdio_claim_host(card->func);
1560 sdio_release_irq(card->func);
1561 sdio_disable_func(card->func);
1562 sdio_release_host(card->func);
1563 sdio_set_drvdata(card->func, NULL);
1564 }
1565}
1566
1567/*
1568 * This function registers the SDIO device.
1569 *
1570 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1571 */
1572static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1573{
1574 int ret = 0;
1575 struct sdio_mmc_card *card = adapter->card;
1576 struct sdio_func *func = card->func;
1577
1578 /* save adapter pointer in card */
1579 card->adapter = adapter;
1580
1581 sdio_claim_host(func);
1582
1583 /* Request the SDIO IRQ */
1584 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1585 if (ret) {
1586 pr_err("claim irq failed: ret=%d\n", ret);
1587 goto disable_func;
1588 }
1589
1590 /* Set block size */
1591 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1592 if (ret) {
1593 pr_err("cannot set SDIO block size\n");
1594 ret = -1;
1595 goto release_irq;
1596 }
1597
1598 sdio_release_host(func);
1599 sdio_set_drvdata(func, card);
1600
1601 adapter->dev = &func->dev;
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001602
1603 switch (func->device) {
WarheadsSE98e6b9d2012-04-24 15:57:21 -04001604 case SDIO_DEVICE_ID_MARVELL_8786:
1605 strcpy(adapter->fw_name, SD8786_DEFAULT_FW_NAME);
1606 break;
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001607 case SDIO_DEVICE_ID_MARVELL_8797:
1608 strcpy(adapter->fw_name, SD8797_DEFAULT_FW_NAME);
1609 break;
1610 case SDIO_DEVICE_ID_MARVELL_8787:
1611 default:
1612 strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
1613 break;
1614 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001615
1616 return 0;
1617
1618release_irq:
1619 sdio_release_irq(func);
1620disable_func:
1621 sdio_disable_func(func);
1622 sdio_release_host(func);
1623 adapter->card = NULL;
1624
1625 return -1;
1626}
1627
1628/*
1629 * This function initializes the SDIO driver.
1630 *
1631 * The following initializations steps are followed -
1632 * - Read the Host interrupt status register to acknowledge
1633 * the first interrupt got from bootloader
1634 * - Disable host interrupt mask register
1635 * - Get SDIO port
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001636 * - Initialize SDIO variables in card
1637 * - Allocate MP registers
1638 * - Allocate MPA Tx and Rx buffers
1639 */
1640static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1641{
1642 struct sdio_mmc_card *card = adapter->card;
1643 int ret;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001644 u32 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001645
1646 /*
1647 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1648 * from the bootloader. If we don't do this we get a interrupt
1649 * as soon as we register the irq.
1650 */
1651 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1652
1653 /* Disable host interrupt mask register for SDIO */
1654 mwifiex_sdio_disable_host_int(adapter);
1655
1656 /* Get SDIO ioport */
1657 mwifiex_init_sdio_ioport(adapter);
1658
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001659 /* Initialize SDIO variables in card */
1660 card->mp_rd_bitmap = 0;
1661 card->mp_wr_bitmap = 0;
1662 card->curr_rd_port = 1;
1663 card->curr_wr_port = 1;
1664
1665 card->mp_data_port_mask = DATA_PORT_MASK;
1666
1667 card->mpa_tx.buf_len = 0;
1668 card->mpa_tx.pkt_cnt = 0;
1669 card->mpa_tx.start_port = 0;
1670
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001671 card->mpa_tx.enabled = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001672 card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1673
1674 card->mpa_rx.buf_len = 0;
1675 card->mpa_rx.pkt_cnt = 0;
1676 card->mpa_rx.start_port = 0;
1677
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001678 card->mpa_rx.enabled = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001679 card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1680
1681 /* Allocate buffers for SDIO MP-A */
1682 card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1683 if (!card->mp_regs) {
1684 dev_err(adapter->dev, "failed to alloc mp_regs\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +02001685 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001686 }
1687
1688 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1689 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1690 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1691 if (ret) {
1692 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1693 kfree(card->mp_regs);
1694 return -1;
1695 }
1696
1697 return ret;
1698}
1699
1700/*
1701 * This function resets the MPA Tx and Rx buffers.
1702 */
1703static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1704{
1705 struct sdio_mmc_card *card = adapter->card;
1706
1707 MP_TX_AGGR_BUF_RESET(card);
1708 MP_RX_AGGR_BUF_RESET(card);
1709}
1710
1711/*
1712 * This function cleans up the allocated card buffers.
1713 *
1714 * The following are freed by this function -
1715 * - MP registers
1716 * - MPA Tx buffer
1717 * - MPA Rx buffer
1718 */
1719static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1720{
1721 struct sdio_mmc_card *card = adapter->card;
1722
1723 kfree(card->mp_regs);
1724 kfree(card->mpa_tx.buf);
1725 kfree(card->mpa_rx.buf);
1726}
1727
1728/*
1729 * This function updates the MP end port in card.
1730 */
1731static void
1732mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1733{
1734 struct sdio_mmc_card *card = adapter->card;
1735 int i;
1736
1737 card->mp_end_port = port;
1738
1739 card->mp_data_port_mask = DATA_PORT_MASK;
1740
1741 for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1742 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1743
1744 card->curr_wr_port = 1;
1745
1746 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001747 port, card->mp_data_port_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001748}
1749
Amitkumar Karward31ab352012-11-01 18:44:14 -07001750static struct mmc_host *reset_host;
1751static void sdio_card_reset_worker(struct work_struct *work)
1752{
1753 /* The actual reset operation must be run outside of driver thread.
1754 * This is because mmc_remove_host() will cause the device to be
1755 * instantly destroyed, and the driver then needs to end its thread,
1756 * leading to a deadlock.
1757 *
1758 * We run it in a totally independent workqueue.
1759 */
1760
1761 pr_err("Resetting card...\n");
1762 mmc_remove_host(reset_host);
1763 /* 20ms delay is based on experiment with sdhci controller */
1764 mdelay(20);
1765 mmc_add_host(reset_host);
1766}
1767static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1768
1769/* This function resets the card */
1770static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1771{
1772 struct sdio_mmc_card *card = adapter->card;
1773
1774 if (work_pending(&card_reset_work))
1775 return;
1776
1777 reset_host = card->func->card->host;
1778 schedule_work(&card_reset_work);
1779}
1780
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001781static struct mwifiex_if_ops sdio_ops = {
1782 .init_if = mwifiex_init_sdio,
1783 .cleanup_if = mwifiex_cleanup_sdio,
1784 .check_fw_status = mwifiex_check_fw_status,
1785 .prog_fw = mwifiex_prog_fw_w_helper,
1786 .register_dev = mwifiex_register_dev,
1787 .unregister_dev = mwifiex_unregister_dev,
1788 .enable_int = mwifiex_sdio_enable_host_int,
1789 .process_int_status = mwifiex_process_int_status,
1790 .host_to_card = mwifiex_sdio_host_to_card,
1791 .wakeup = mwifiex_pm_wakeup_card,
1792 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1793
1794 /* SDIO specific */
1795 .update_mp_end_port = mwifiex_update_mp_end_port,
1796 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001797 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1798 .event_complete = mwifiex_sdio_event_complete,
Amitkumar Karward31ab352012-11-01 18:44:14 -07001799 .card_reset = mwifiex_sdio_card_reset,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001800};
1801
1802/*
1803 * This function initializes the SDIO driver.
1804 *
1805 * This initiates the semaphore and registers the device with
1806 * SDIO bus.
1807 */
1808static int
1809mwifiex_sdio_init_module(void)
1810{
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001811 sema_init(&add_remove_card_sem, 1);
1812
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301813 /* Clear the flag in case user removes the card. */
1814 user_rmmod = 0;
1815
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001816 return sdio_register_driver(&mwifiex_sdio);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001817}
1818
1819/*
1820 * This function cleans up the SDIO driver.
1821 *
1822 * The following major steps are followed for cleanup -
1823 * - Resume the device if its suspended
1824 * - Disconnect the device if connected
1825 * - Shutdown the firmware
1826 * - Unregister the device from SDIO bus.
1827 */
1828static void
1829mwifiex_sdio_cleanup_module(void)
1830{
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301831 if (!down_interruptible(&add_remove_card_sem))
1832 up(&add_remove_card_sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001833
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301834 /* Set the flag as user is removing this module. */
1835 user_rmmod = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001836
Amitkumar Karward31ab352012-11-01 18:44:14 -07001837 cancel_work_sync(&card_reset_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001838 sdio_unregister_driver(&mwifiex_sdio);
1839}
1840
1841module_init(mwifiex_sdio_init_module);
1842module_exit(mwifiex_sdio_cleanup_module);
1843
1844MODULE_AUTHOR("Marvell International Ltd.");
1845MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1846MODULE_VERSION(SDIO_VERSION);
1847MODULE_LICENSE("GPL v2");
WarheadsSE98e6b9d2012-04-24 15:57:21 -04001848MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001849MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
1850MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);