blob: 047a6f348d3d606e0866dfce56f2808c4846d6a2 [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 ret = 0;
165
166 if (func) {
167 pm_flag = sdio_get_host_pm_caps(func);
168 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700169 sdio_func_id(func), pm_flag);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700170 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
171 pr_err("%s: cannot remain alive while host is"
172 " suspended\n", sdio_func_id(func));
173 return -ENOSYS;
174 }
175
176 card = sdio_get_drvdata(func);
177 if (!card || !card->adapter) {
178 pr_err("suspend: invalid card or adapter\n");
179 return 0;
180 }
181 } else {
182 pr_err("suspend: sdio_func is not specified\n");
183 return 0;
184 }
185
186 adapter = card->adapter;
187
188 /* Enable the Host Sleep */
Bing Zhaodd321ac2012-11-15 15:58:48 -0800189 if (!mwifiex_enable_hs(adapter)) {
190 dev_err(adapter->dev, "cmd: failed to suspend\n");
191 return -EFAULT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700192 }
193
Bing Zhaodd321ac2012-11-15 15:58:48 -0800194 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
195 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
196
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700197 /* Indicate device suspended */
198 adapter->is_suspended = true;
199
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700200 return ret;
201}
202
203/*
204 * SDIO resume.
205 *
206 * Kernel needs to suspend all functions separately. Therefore all
207 * registered functions must have drivers with suspend and resume
208 * methods. Failing that the kernel simply removes the whole card.
209 *
210 * If already not resumed, this function turns on the traffic and
211 * sends a host sleep cancel request to the firmware.
212 */
213static int mwifiex_sdio_resume(struct device *dev)
214{
215 struct sdio_func *func = dev_to_sdio_func(dev);
216 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700217 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700218 mmc_pm_flag_t pm_flag = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700219
220 if (func) {
221 pm_flag = sdio_get_host_pm_caps(func);
222 card = sdio_get_drvdata(func);
223 if (!card || !card->adapter) {
224 pr_err("resume: invalid card or adapter\n");
225 return 0;
226 }
227 } else {
228 pr_err("resume: sdio_func is not specified\n");
229 return 0;
230 }
231
232 adapter = card->adapter;
233
234 if (!adapter->is_suspended) {
235 dev_warn(adapter->dev, "device already resumed\n");
236 return 0;
237 }
238
239 adapter->is_suspended = false;
240
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700241 /* Disable Host Sleep */
242 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700243 MWIFIEX_ASYNC_CMD);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700244
245 return 0;
246}
247
WarheadsSE98e6b9d2012-04-24 15:57:21 -0400248/* Device ID for SD8786 */
249#define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700250/* Device ID for SD8787 */
251#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
Bing Zhaoe3bea1c2011-11-16 20:40:35 -0800252/* Device ID for SD8797 */
253#define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700254
255/* WLAN IDs */
256static const struct sdio_device_id mwifiex_ids[] = {
WarheadsSE98e6b9d2012-04-24 15:57:21 -0400257 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786)},
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700258 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
Bing Zhaoe3bea1c2011-11-16 20:40:35 -0800259 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797)},
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700260 {},
261};
262
263MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
264
265static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
266 .suspend = mwifiex_sdio_suspend,
267 .resume = mwifiex_sdio_resume,
268};
269
270static struct sdio_driver mwifiex_sdio = {
271 .name = "mwifiex_sdio",
272 .id_table = mwifiex_ids,
273 .probe = mwifiex_sdio_probe,
274 .remove = mwifiex_sdio_remove,
275 .drv = {
276 .owner = THIS_MODULE,
277 .pm = &mwifiex_sdio_pm_ops,
278 }
279};
280
281/*
282 * This function writes data into SDIO card register.
283 */
284static int
285mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
286{
287 struct sdio_mmc_card *card = adapter->card;
288 int ret = -1;
289
290 sdio_claim_host(card->func);
291 sdio_writeb(card->func, (u8) data, reg, &ret);
292 sdio_release_host(card->func);
293
294 return ret;
295}
296
297/*
298 * This function reads data from SDIO card register.
299 */
300static int
301mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
302{
303 struct sdio_mmc_card *card = adapter->card;
304 int ret = -1;
305 u8 val;
306
307 sdio_claim_host(card->func);
308 val = sdio_readb(card->func, reg, &ret);
309 sdio_release_host(card->func);
310
311 *data = val;
312
313 return ret;
314}
315
316/*
317 * This function writes multiple data into SDIO card memory.
318 *
319 * This does not work in suspended mode.
320 */
321static int
322mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700323 u8 *buffer, u32 pkt_len, u32 port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700324{
325 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800326 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700327 u8 blk_mode =
328 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
329 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
330 u32 blk_cnt =
331 (blk_mode ==
332 BLOCK_MODE) ? (pkt_len /
333 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
334 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
335
336 if (adapter->is_suspended) {
337 dev_err(adapter->dev,
338 "%s: not allowed while suspended\n", __func__);
339 return -1;
340 }
341
342 sdio_claim_host(card->func);
343
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800344 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700345
346 sdio_release_host(card->func);
347
348 return ret;
349}
350
351/*
352 * This function reads multiple data from SDIO card memory.
353 */
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700354static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
355 u32 len, u32 port, u8 claim)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700356{
357 struct sdio_mmc_card *card = adapter->card;
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800358 int ret;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700359 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
360 : BLOCK_MODE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700361 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700362 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
363 : len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700364 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
365
366 if (claim)
367 sdio_claim_host(card->func);
368
Bing Zhao5b2e2ec2013-01-25 18:23:00 -0800369 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700370
371 if (claim)
372 sdio_release_host(card->func);
373
374 return ret;
375}
376
377/*
378 * This function wakes up the card.
379 *
380 * A host power up command is written to the card configuration
381 * register to wake up the card.
382 */
383static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
384{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700385 dev_dbg(adapter->dev, "event: wakeup device...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700386
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700387 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700388}
389
390/*
391 * This function is called after the card has woken up.
392 *
393 * The card configuration register is reset.
394 */
395static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
396{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700397 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700398
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700399 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700400}
401
402/*
403 * This function initializes the IO ports.
404 *
405 * The following operations are performed -
406 * - Read the IO ports (0, 1 and 2)
407 * - Set host interrupt Reset-To-Read to clear
408 * - Set auto re-enable interrupt
409 */
410static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
411{
412 u32 reg;
413
414 adapter->ioport = 0;
415
416 /* Read the IO port */
417 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
418 adapter->ioport |= (reg & 0xff);
419 else
420 return -1;
421
422 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
423 adapter->ioport |= ((reg & 0xff) << 8);
424 else
425 return -1;
426
427 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
428 adapter->ioport |= ((reg & 0xff) << 16);
429 else
430 return -1;
431
432 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
433
434 /* Set Host interrupt reset to read to clear */
435 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
436 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
437 reg | SDIO_INT_MASK);
438 else
439 return -1;
440
441 /* Dnld/Upld ready set to auto reset */
442 if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, &reg))
443 mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
444 reg | AUTO_RE_ENABLE_INT);
445 else
446 return -1;
447
448 return 0;
449}
450
451/*
452 * This function sends data to the card.
453 */
454static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
455 u8 *payload, u32 pkt_len, u32 port)
456{
457 u32 i = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700458 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700459
460 do {
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700461 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700462 if (ret) {
463 i++;
464 dev_err(adapter->dev, "host_to_card, write iomem"
465 " (%d) failed: %d\n", i, ret);
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700466 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700467 dev_err(adapter->dev, "write CFG reg failed\n");
468
469 ret = -1;
470 if (i > MAX_WRITE_IOMEM_RETRY)
471 return ret;
472 }
473 } while (ret == -1);
474
475 return ret;
476}
477
478/*
479 * This function gets the read port.
480 *
481 * If control port bit is set in MP read bitmap, the control port
482 * is returned, otherwise the current read port is returned and
483 * the value is increased (provided it does not reach the maximum
484 * limit, in which case it is reset to 1)
485 */
486static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
487{
488 struct sdio_mmc_card *card = adapter->card;
489 u16 rd_bitmap = card->mp_rd_bitmap;
490
491 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
492
493 if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
494 return -1;
495
496 if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
497 card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
498 *port = CTRL_PORT;
499 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700500 *port, card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700501 } else {
502 if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700503 card->mp_rd_bitmap &= (u16)
504 (~(1 << card->curr_rd_port));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700505 *port = card->curr_rd_port;
506
507 if (++card->curr_rd_port == MAX_PORT)
508 card->curr_rd_port = 1;
509 } else {
510 return -1;
511 }
512
513 dev_dbg(adapter->dev,
514 "data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700515 *port, rd_bitmap, card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700516 }
517 return 0;
518}
519
520/*
521 * This function gets the write port for data.
522 *
523 * The current write port is returned if available and the value is
524 * increased (provided it does not reach the maximum limit, in which
525 * case it is reset to 1)
526 */
527static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
528{
529 struct sdio_mmc_card *card = adapter->card;
530 u16 wr_bitmap = card->mp_wr_bitmap;
531
532 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
533
534 if (!(wr_bitmap & card->mp_data_port_mask))
535 return -1;
536
537 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
538 card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
539 *port = card->curr_wr_port;
540 if (++card->curr_wr_port == card->mp_end_port)
541 card->curr_wr_port = 1;
542 } else {
543 adapter->data_sent = true;
544 return -EBUSY;
545 }
546
547 if (*port == CTRL_PORT) {
548 dev_err(adapter->dev, "invalid data port=%d cur port=%d"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700549 " mp_wr_bitmap=0x%04x -> 0x%04x\n",
550 *port, card->curr_wr_port, wr_bitmap,
551 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700552 return -1;
553 }
554
555 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700556 *port, wr_bitmap, card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700557
558 return 0;
559}
560
561/*
562 * This function polls the card status.
563 */
564static int
565mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
566{
567 u32 tries;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700568 u32 cs;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700569
570 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
571 if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
572 break;
573 else if ((cs & bits) == bits)
574 return 0;
575
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700576 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700577 }
578
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700579 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
580
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700581 return -1;
582}
583
584/*
585 * This function reads the firmware status.
586 */
587static int
588mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
589{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700590 u32 fws0, fws1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700591
592 if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
593 return -1;
594
595 if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
596 return -1;
597
598 *dat = (u16) ((fws1 << 8) | fws0);
599
600 return 0;
601}
602
603/*
604 * This function disables the host interrupt.
605 *
606 * The host interrupt mask is read, the disable bit is reset and
607 * written back to the card host interrupt mask register.
608 */
609static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
610{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700611 u32 host_int_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700612
613 /* Read back the host_int_mask register */
614 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
615 return -1;
616
617 /* Update with the mask and write back to the register */
618 host_int_mask &= ~HOST_INT_DISABLE;
619
620 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
621 dev_err(adapter->dev, "disable host interrupt failed\n");
622 return -1;
623 }
624
625 return 0;
626}
627
628/*
629 * This function enables the host interrupt.
630 *
631 * The host interrupt enable mask is written to the card
632 * host interrupt mask register.
633 */
634static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
635{
636 /* Simply write the mask to the register */
637 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
638 dev_err(adapter->dev, "enable host interrupt failed\n");
639 return -1;
640 }
641 return 0;
642}
643
644/*
645 * This function sends a data buffer to the card.
646 */
647static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
648 u32 *type, u8 *buffer,
649 u32 npayload, u32 ioport)
650{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700651 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700652 u32 nb;
653
654 if (!buffer) {
655 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
656 return -1;
657 }
658
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700659 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700660
661 if (ret) {
662 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700663 ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700664 return -1;
665 }
666
667 nb = le16_to_cpu(*(__le16 *) (buffer));
668 if (nb > npayload) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700669 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
670 __func__, nb, npayload);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700671 return -1;
672 }
673
674 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
675
676 return ret;
677}
678
679/*
680 * This function downloads the firmware to the card.
681 *
682 * Firmware is downloaded to the card in blocks. Every block download
683 * is tested for CRC errors, and retried a number of times before
684 * returning failure.
685 */
686static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
687 struct mwifiex_fw_image *fw)
688{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700689 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700690 u8 *firmware = fw->fw_buf;
691 u32 firmware_len = fw->fw_len;
692 u32 offset = 0;
693 u32 base0, base1;
694 u8 *fwbuf;
695 u16 len = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700696 u32 txlen, tx_blocks = 0, tries;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700697 u32 i = 0;
698
699 if (!firmware_len) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700700 dev_err(adapter->dev,
701 "firmware image not found! Terminating download\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700702 return -1;
703 }
704
705 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700706 firmware_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700707
708 /* Assume that the allocated buffer is 8-byte aligned */
709 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
Joe Perches0d2e7a52013-02-03 17:28:14 +0000710 if (!fwbuf)
Christoph Fritzb53575e2011-05-08 22:50:09 +0200711 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700712
713 /* Perform firmware data transfer */
714 do {
715 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
716 bits */
717 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
718 DN_LD_CARD_RDY);
719 if (ret) {
720 dev_err(adapter->dev, "FW download with helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700721 " poll status timeout @ %d\n", offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700722 goto done;
723 }
724
725 /* More data? */
726 if (offset >= firmware_len)
727 break;
728
729 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
730 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
731 &base0);
732 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700733 dev_err(adapter->dev,
734 "dev BASE0 register read failed: "
735 "base0=%#04X(%d). Terminating dnld\n",
736 base0, base0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700737 goto done;
738 }
739 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
740 &base1);
741 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700742 dev_err(adapter->dev,
743 "dev BASE1 register read failed: "
744 "base1=%#04X(%d). Terminating dnld\n",
745 base1, base1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700746 goto done;
747 }
748 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
749
750 if (len)
751 break;
752
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700753 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700754 }
755
756 if (!len) {
757 break;
758 } else if (len > MWIFIEX_UPLD_SIZE) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700759 dev_err(adapter->dev,
760 "FW dnld failed @ %d, invalid length %d\n",
761 offset, len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700762 ret = -1;
763 goto done;
764 }
765
766 txlen = len;
767
768 if (len & BIT(0)) {
769 i++;
770 if (i > MAX_WRITE_IOMEM_RETRY) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700771 dev_err(adapter->dev,
772 "FW dnld failed @ %d, over max retry\n",
773 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700774 ret = -1;
775 goto done;
776 }
777 dev_err(adapter->dev, "CRC indicated by the helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700778 " len = 0x%04X, txlen = %d\n", len, txlen);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700779 len &= ~BIT(0);
780 /* Setting this to 0 to resend from same offset */
781 txlen = 0;
782 } else {
783 i = 0;
784
785 /* Set blocksize to transfer - checking for last
786 block */
787 if (firmware_len - offset < txlen)
788 txlen = firmware_len - offset;
789
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700790 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
791 / MWIFIEX_SDIO_BLOCK_SIZE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700792
793 /* Copy payload to buffer */
794 memmove(fwbuf, &firmware[offset], txlen);
795 }
796
797 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
798 MWIFIEX_SDIO_BLOCK_SIZE,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700799 adapter->ioport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700800 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700801 dev_err(adapter->dev,
802 "FW download, write iomem (%d) failed @ %d\n",
803 i, offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700804 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
805 dev_err(adapter->dev, "write CFG reg failed\n");
806
807 ret = -1;
808 goto done;
809 }
810
811 offset += txlen;
812 } while (true);
813
814 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700815 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700816
817 ret = 0;
818done:
819 kfree(fwbuf);
820 return ret;
821}
822
823/*
824 * This function checks the firmware status in card.
825 *
826 * The winner interface is also determined by this function.
827 */
828static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700829 u32 poll_num)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700830{
831 int ret = 0;
832 u16 firmware_stat;
833 u32 tries;
834 u32 winner_status;
835
836 /* Wait for firmware initialization event */
837 for (tries = 0; tries < poll_num; tries++) {
838 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
839 if (ret)
840 continue;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700841 if (firmware_stat == FIRMWARE_READY_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700842 ret = 0;
843 break;
844 } else {
845 mdelay(100);
846 ret = -1;
847 }
848 }
849
Amitkumar Karward930fae2011-10-11 17:41:21 -0700850 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700851 if (mwifiex_read_reg
852 (adapter, CARD_FW_STATUS0_REG, &winner_status))
853 winner_status = 0;
854
855 if (winner_status)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700856 adapter->winner = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700857 else
Amitkumar Karward930fae2011-10-11 17:41:21 -0700858 adapter->winner = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700859 }
860 return ret;
861}
862
863/*
864 * This function reads the interrupt status from card.
865 */
866static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
867{
868 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700869 u32 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700870 unsigned long flags;
871
872 if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700873 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700874 0)) {
875 dev_err(adapter->dev, "read mp_regs failed\n");
876 return;
877 }
878
879 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
880 if (sdio_ireg) {
881 /*
882 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
883 * Clear the interrupt status register
884 */
885 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
886 spin_lock_irqsave(&adapter->int_lock, flags);
887 adapter->int_status |= sdio_ireg;
888 spin_unlock_irqrestore(&adapter->int_lock, flags);
889 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700890}
891
892/*
893 * SDIO interrupt handler.
894 *
Bing Zhao601216e2012-11-05 16:59:15 -0800895 * This function reads the interrupt status from firmware and handles
896 * the interrupt in current thread (ksdioirqd) right away.
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700897 */
898static void
899mwifiex_sdio_interrupt(struct sdio_func *func)
900{
901 struct mwifiex_adapter *adapter;
902 struct sdio_mmc_card *card;
903
904 card = sdio_get_drvdata(func);
905 if (!card || !card->adapter) {
906 pr_debug("int: func=%p card=%p adapter=%p\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700907 func, card, card ? card->adapter : NULL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700908 return;
909 }
910 adapter = card->adapter;
911
912 if (adapter->surprise_removed)
913 return;
914
915 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
916 adapter->ps_state = PS_STATE_AWAKE;
917
918 mwifiex_interrupt_status(adapter);
Bing Zhao601216e2012-11-05 16:59:15 -0800919 mwifiex_main_process(adapter);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700920}
921
922/*
923 * This function decodes a received packet.
924 *
925 * Based on the type, the packet is treated as either a data, or
926 * a command response, or an event, and the correct handler
927 * function is invoked.
928 */
929static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
930 struct sk_buff *skb, u32 upld_typ)
931{
932 u8 *cmd_buf;
Avinash Patil0c17d922013-11-05 15:01:44 -0800933 __le16 *curr_ptr = (__le16 *)skb->data;
934 u16 pkt_len = le16_to_cpu(*curr_ptr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700935
Avinash Patil0c17d922013-11-05 15:01:44 -0800936 skb_trim(skb, pkt_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700937 skb_pull(skb, INTF_HEADER_LEN);
938
939 switch (upld_typ) {
940 case MWIFIEX_TYPE_DATA:
941 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
942 mwifiex_handle_rx_packet(adapter, skb);
943 break;
944
945 case MWIFIEX_TYPE_CMD:
946 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
947 /* take care of curr_cmd = NULL case */
948 if (!adapter->curr_cmd) {
949 cmd_buf = adapter->upld_buf;
950
951 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
952 mwifiex_process_sleep_confirm_resp(adapter,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700953 skb->data,
954 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700955
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700956 memcpy(cmd_buf, skb->data,
957 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
958 skb->len));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700959
960 dev_kfree_skb_any(skb);
961 } else {
962 adapter->cmd_resp_received = true;
963 adapter->curr_cmd->resp_skb = skb;
964 }
965 break;
966
967 case MWIFIEX_TYPE_EVENT:
968 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
969 adapter->event_cause = *(u32 *) skb->data;
970
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700971 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
Amitkumar Karware80c81d2012-06-20 20:21:12 -0700972 memcpy(adapter->event_body,
973 skb->data + MWIFIEX_EVENT_HEADER_LEN,
974 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700975
976 /* event cause has been saved to adapter->event_cause */
977 adapter->event_received = true;
978 adapter->event_skb = skb;
979
980 break;
981
982 default:
983 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
984 dev_kfree_skb_any(skb);
985 break;
986 }
987
988 return 0;
989}
990
991/*
992 * This function transfers received packets from card to driver, performing
993 * aggregation if required.
994 *
995 * For data received on control port, or if aggregation is disabled, the
996 * received buffers are uploaded as separate packets. However, if aggregation
997 * is enabled and required, the buffers are copied onto an aggregation buffer,
998 * provided there is space left, processed and finally uploaded.
999 */
1000static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1001 struct sk_buff *skb, u8 port)
1002{
1003 struct sdio_mmc_card *card = adapter->card;
1004 s32 f_do_rx_aggr = 0;
1005 s32 f_do_rx_cur = 0;
1006 s32 f_aggr_cur = 0;
1007 struct sk_buff *skb_deaggr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001008 u32 pind;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001009 u32 pkt_len, pkt_type = 0;
1010 u8 *curr_ptr;
1011 u32 rx_len = skb->len;
1012
1013 if (port == CTRL_PORT) {
1014 /* Read the command Resp without aggr */
1015 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001016 "response\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001017
1018 f_do_rx_cur = 1;
1019 goto rx_curr_single;
1020 }
1021
1022 if (!card->mpa_rx.enabled) {
1023 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001024 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001025
1026 f_do_rx_cur = 1;
1027 goto rx_curr_single;
1028 }
1029
1030 if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1031 /* Some more data RX pending */
1032 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1033
1034 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1035 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1036 f_aggr_cur = 1;
1037 } else {
1038 /* No room in Aggr buf, do rx aggr now */
1039 f_do_rx_aggr = 1;
1040 f_do_rx_cur = 1;
1041 }
1042 } else {
1043 /* Rx aggr not in progress */
1044 f_aggr_cur = 1;
1045 }
1046
1047 } else {
1048 /* No more data RX pending */
1049 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1050
1051 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1052 f_do_rx_aggr = 1;
1053 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1054 f_aggr_cur = 1;
1055 else
1056 /* No room in Aggr buf, do rx aggr now */
1057 f_do_rx_cur = 1;
1058 } else {
1059 f_do_rx_cur = 1;
1060 }
1061 }
1062
1063 if (f_aggr_cur) {
1064 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1065 /* Curr pkt can be aggregated */
1066 MP_RX_AGGR_SETUP(card, skb, port);
1067
1068 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1069 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1070 dev_dbg(adapter->dev, "info: %s: aggregated packet "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001071 "limit reached\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001072 /* No more pkts allowed in Aggr buf, rx it */
1073 f_do_rx_aggr = 1;
1074 }
1075 }
1076
1077 if (f_do_rx_aggr) {
1078 /* do aggr RX now */
1079 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001080 card->mpa_rx.pkt_cnt);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001081
1082 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1083 card->mpa_rx.buf_len,
1084 (adapter->ioport | 0x1000 |
1085 (card->mpa_rx.ports << 4)) +
Amitkumar Karwar572e8f32011-04-13 17:27:08 -07001086 card->mpa_rx.start_port, 1))
Avinash Patil17a60b42011-12-08 20:41:04 -08001087 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001088
1089 curr_ptr = card->mpa_rx.buf;
1090
1091 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1092
1093 /* get curr PKT len & type */
1094 pkt_len = *(u16 *) &curr_ptr[0];
1095 pkt_type = *(u16 *) &curr_ptr[2];
1096
1097 /* copy pkt to deaggr buf */
1098 skb_deaggr = card->mpa_rx.skb_arr[pind];
1099
1100 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1101 card->mpa_rx.len_arr[pind])) {
1102
1103 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1104
1105 skb_trim(skb_deaggr, pkt_len);
1106
1107 /* Process de-aggr packet */
1108 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1109 pkt_type);
1110 } else {
1111 dev_err(adapter->dev, "wrong aggr pkt:"
1112 " type=%d len=%d max_len=%d\n",
1113 pkt_type, pkt_len,
1114 card->mpa_rx.len_arr[pind]);
1115 dev_kfree_skb_any(skb_deaggr);
1116 }
1117 curr_ptr += card->mpa_rx.len_arr[pind];
1118 }
1119 MP_RX_AGGR_BUF_RESET(card);
1120 }
1121
1122rx_curr_single:
1123 if (f_do_rx_cur) {
1124 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1125 port, rx_len);
1126
1127 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1128 skb->data, skb->len,
1129 adapter->ioport + port))
Avinash Patil17a60b42011-12-08 20:41:04 -08001130 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001131
1132 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1133 }
1134
1135 return 0;
Avinash Patil17a60b42011-12-08 20:41:04 -08001136
1137error:
1138 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1139 /* Multiport-aggregation transfer failed - cleanup */
1140 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1141 /* copy pkt to deaggr buf */
1142 skb_deaggr = card->mpa_rx.skb_arr[pind];
1143 dev_kfree_skb_any(skb_deaggr);
1144 }
1145 MP_RX_AGGR_BUF_RESET(card);
1146 }
1147
1148 if (f_do_rx_cur)
1149 /* Single transfer pending. Free curr buff also */
1150 dev_kfree_skb_any(skb);
1151
1152 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001153}
1154
1155/*
1156 * This function checks the current interrupt status.
1157 *
1158 * The following interrupts are checked and handled by this function -
1159 * - Data sent
1160 * - Command sent
1161 * - Packets received
1162 *
1163 * Since the firmware does not generate download ready interrupt if the
1164 * port updated is command port only, command sent interrupt checking
1165 * should be done manually, and for every SDIO interrupt.
1166 *
1167 * In case of Rx packets received, the packets are uploaded from card to
1168 * host and processed accordingly.
1169 */
1170static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1171{
1172 struct sdio_mmc_card *card = adapter->card;
1173 int ret = 0;
1174 u8 sdio_ireg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001175 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001176 u8 port = CTRL_PORT;
1177 u32 len_reg_l, len_reg_u;
1178 u32 rx_blocks;
1179 u16 rx_len;
1180 unsigned long flags;
1181
1182 spin_lock_irqsave(&adapter->int_lock, flags);
1183 sdio_ireg = adapter->int_status;
1184 adapter->int_status = 0;
1185 spin_unlock_irqrestore(&adapter->int_lock, flags);
1186
1187 if (!sdio_ireg)
1188 return ret;
1189
1190 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1191 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1192 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1193 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001194 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001195 if (adapter->data_sent &&
1196 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1197 dev_dbg(adapter->dev,
1198 "info: <--- Tx DONE Interrupt --->\n");
1199 adapter->data_sent = false;
1200 }
1201 }
1202
1203 /* As firmware will not generate download ready interrupt if the port
1204 updated is command port only, cmd_sent should be done for any SDIO
1205 interrupt. */
1206 if (adapter->cmd_sent) {
1207 /* Check if firmware has attach buffer at command port and
1208 update just that in wr_bit_map. */
1209 card->mp_wr_bitmap |=
1210 (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1211 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1212 adapter->cmd_sent = false;
1213 }
1214
1215 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001216 adapter->cmd_sent, adapter->data_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001217 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1218 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1219 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1220 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001221 card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001222
1223 while (true) {
1224 ret = mwifiex_get_rd_port(adapter, &port);
1225 if (ret) {
1226 dev_dbg(adapter->dev,
1227 "info: no more rd_port available\n");
1228 break;
1229 }
1230 len_reg_l = RD_LEN_P0_L + (port << 1);
1231 len_reg_u = RD_LEN_P0_U + (port << 1);
1232 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1233 rx_len |= (u16) card->mp_regs[len_reg_l];
1234 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001235 port, rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001236 rx_blocks =
1237 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1238 1) / MWIFIEX_SDIO_BLOCK_SIZE;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001239 if (rx_len <= INTF_HEADER_LEN ||
1240 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1241 MWIFIEX_RX_DATA_BUF_SIZE) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001242 dev_err(adapter->dev, "invalid rx_len=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001243 rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001244 return -1;
1245 }
1246 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1247
1248 skb = dev_alloc_skb(rx_len);
1249
1250 if (!skb) {
1251 dev_err(adapter->dev, "%s: failed to alloc skb",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001252 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001253 return -1;
1254 }
1255
1256 skb_put(skb, rx_len);
1257
1258 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001259 rx_len, skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001260
1261 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1262 port)) {
1263 u32 cr = 0;
1264
1265 dev_err(adapter->dev, "card_to_host_mpa failed:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001266 " int status=%#x\n", sdio_ireg);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001267 if (mwifiex_read_reg(adapter,
1268 CONFIGURATION_REG, &cr))
1269 dev_err(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001270 "read CFG reg failed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001271
1272 dev_dbg(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001273 "info: CFG reg val = %d\n", cr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001274 if (mwifiex_write_reg(adapter,
1275 CONFIGURATION_REG,
1276 (cr | 0x04)))
1277 dev_err(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001278 "write CFG reg failed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001279
1280 dev_dbg(adapter->dev, "info: write success\n");
1281 if (mwifiex_read_reg(adapter,
1282 CONFIGURATION_REG, &cr))
1283 dev_err(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001284 "read CFG reg failed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001285
1286 dev_dbg(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001287 "info: CFG reg val =%x\n", cr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001288 return -1;
1289 }
1290 }
1291 }
1292
1293 return 0;
1294}
1295
1296/*
1297 * This function aggregates transmission buffers in driver and downloads
1298 * the aggregated packet to card.
1299 *
1300 * The individual packets are aggregated by copying into an aggregation
1301 * buffer and then downloaded to the card. Previous unsent packets in the
1302 * aggregation buffer are pre-copied first before new packets are added.
1303 * Aggregation is done till there is space left in the aggregation buffer,
1304 * or till new packets are available.
1305 *
1306 * The function will only download the packet to the card when aggregation
1307 * stops, otherwise it will just aggregate the packet in aggregation buffer
1308 * and return.
1309 */
1310static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1311 u8 *payload, u32 pkt_len, u8 port,
1312 u32 next_pkt_len)
1313{
1314 struct sdio_mmc_card *card = adapter->card;
1315 int ret = 0;
1316 s32 f_send_aggr_buf = 0;
1317 s32 f_send_cur_buf = 0;
1318 s32 f_precopy_cur_buf = 0;
1319 s32 f_postcopy_cur_buf = 0;
1320
1321 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1322 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001323 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001324
1325 f_send_cur_buf = 1;
1326 goto tx_curr_single;
1327 }
1328
1329 if (next_pkt_len) {
1330 /* More pkt in TX queue */
1331 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001332 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001333
1334 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1335 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1336 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1337 f_precopy_cur_buf = 1;
1338
1339 if (!(card->mp_wr_bitmap &
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001340 (1 << card->curr_wr_port)) ||
1341 !MP_TX_AGGR_BUF_HAS_ROOM(
1342 card, pkt_len + next_pkt_len))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001343 f_send_aggr_buf = 1;
1344 } else {
1345 /* No room in Aggr buf, send it */
1346 f_send_aggr_buf = 1;
1347
1348 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1349 !(card->mp_wr_bitmap &
1350 (1 << card->curr_wr_port)))
1351 f_send_cur_buf = 1;
1352 else
1353 f_postcopy_cur_buf = 1;
1354 }
1355 } else {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001356 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1357 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001358 f_precopy_cur_buf = 1;
1359 else
1360 f_send_cur_buf = 1;
1361 }
1362 } else {
1363 /* Last pkt in TX queue */
1364 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001365 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001366
1367 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1368 /* some packs in Aggr buf already */
1369 f_send_aggr_buf = 1;
1370
1371 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1372 f_precopy_cur_buf = 1;
1373 else
1374 /* No room in Aggr buf, send it */
1375 f_send_cur_buf = 1;
1376 } else {
1377 f_send_cur_buf = 1;
1378 }
1379 }
1380
1381 if (f_precopy_cur_buf) {
1382 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001383 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001384 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1385
1386 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1387 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1388 /* No more pkts allowed in Aggr buf, send it */
1389 f_send_aggr_buf = 1;
1390 }
1391
1392 if (f_send_aggr_buf) {
1393 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001394 __func__,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001395 card->mpa_tx.start_port, card->mpa_tx.ports);
1396 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1397 card->mpa_tx.buf_len,
1398 (adapter->ioport | 0x1000 |
1399 (card->mpa_tx.ports << 4)) +
1400 card->mpa_tx.start_port);
1401
1402 MP_TX_AGGR_BUF_RESET(card);
1403 }
1404
1405tx_curr_single:
1406 if (f_send_cur_buf) {
1407 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001408 __func__, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001409 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1410 adapter->ioport + port);
1411 }
1412
1413 if (f_postcopy_cur_buf) {
1414 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001415 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001416 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1417 }
1418
1419 return ret;
1420}
1421
1422/*
1423 * This function downloads data from driver to card.
1424 *
1425 * Both commands and data packets are transferred to the card by this
1426 * function.
1427 *
1428 * This function adds the SDIO specific header to the front of the buffer
1429 * before transferring. The header contains the length of the packet and
1430 * the type. The firmware handles the packets based upon this set type.
1431 */
1432static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001433 u8 type, struct sk_buff *skb,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001434 struct mwifiex_tx_param *tx_param)
1435{
1436 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001437 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001438 u32 buf_block_len;
1439 u32 blk_size;
1440 u8 port = CTRL_PORT;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001441 u8 *payload = (u8 *)skb->data;
1442 u32 pkt_len = skb->len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001443
1444 /* Allocate buffer and copy payload */
1445 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1446 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
Tomasz Moń558c62c2013-07-23 07:42:49 +02001447 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1448 *(__le16 *)&payload[2] = cpu_to_le16(type);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001449
1450 /*
1451 * This is SDIO specific header
1452 * u16 length,
1453 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1454 * MWIFIEX_TYPE_EVENT = 3)
1455 */
1456 if (type == MWIFIEX_TYPE_DATA) {
1457 ret = mwifiex_get_wr_port_data(adapter, &port);
1458 if (ret) {
1459 dev_err(adapter->dev, "%s: no wr_port available\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001460 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001461 return ret;
1462 }
1463 } else {
1464 adapter->cmd_sent = true;
1465 /* Type must be MWIFIEX_TYPE_CMD */
1466
1467 if (pkt_len <= INTF_HEADER_LEN ||
1468 pkt_len > MWIFIEX_UPLD_SIZE)
1469 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001470 __func__, payload, pkt_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001471 }
1472
1473 /* Transfer data to card */
1474 pkt_len = buf_block_len * blk_size;
1475
1476 if (tx_param)
1477 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001478 port, tx_param->next_pkt_len
1479 );
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001480 else
1481 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001482 port, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001483
1484 if (ret) {
1485 if (type == MWIFIEX_TYPE_CMD)
1486 adapter->cmd_sent = false;
1487 if (type == MWIFIEX_TYPE_DATA)
1488 adapter->data_sent = false;
1489 } else {
1490 if (type == MWIFIEX_TYPE_DATA) {
1491 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1492 adapter->data_sent = true;
1493 else
1494 adapter->data_sent = false;
1495 }
1496 }
1497
1498 return ret;
1499}
1500
1501/*
1502 * This function allocates the MPA Tx and Rx buffers.
1503 */
1504static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1505 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1506{
1507 struct sdio_mmc_card *card = adapter->card;
1508 int ret = 0;
1509
1510 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1511 if (!card->mpa_tx.buf) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001512 ret = -1;
1513 goto error;
1514 }
1515
1516 card->mpa_tx.buf_size = mpa_tx_buf_size;
1517
1518 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1519 if (!card->mpa_rx.buf) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001520 ret = -1;
1521 goto error;
1522 }
1523
1524 card->mpa_rx.buf_size = mpa_rx_buf_size;
1525
1526error:
1527 if (ret) {
1528 kfree(card->mpa_tx.buf);
1529 kfree(card->mpa_rx.buf);
1530 }
1531
1532 return ret;
1533}
1534
1535/*
1536 * This function unregisters the SDIO device.
1537 *
1538 * The SDIO IRQ is released, the function is disabled and driver
1539 * data is set to null.
1540 */
1541static void
1542mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1543{
1544 struct sdio_mmc_card *card = adapter->card;
1545
1546 if (adapter->card) {
1547 /* Release the SDIO IRQ */
1548 sdio_claim_host(card->func);
1549 sdio_release_irq(card->func);
1550 sdio_disable_func(card->func);
1551 sdio_release_host(card->func);
1552 sdio_set_drvdata(card->func, NULL);
1553 }
1554}
1555
1556/*
1557 * This function registers the SDIO device.
1558 *
1559 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1560 */
1561static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1562{
1563 int ret = 0;
1564 struct sdio_mmc_card *card = adapter->card;
1565 struct sdio_func *func = card->func;
1566
1567 /* save adapter pointer in card */
1568 card->adapter = adapter;
1569
1570 sdio_claim_host(func);
1571
1572 /* Request the SDIO IRQ */
1573 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1574 if (ret) {
1575 pr_err("claim irq failed: ret=%d\n", ret);
1576 goto disable_func;
1577 }
1578
1579 /* Set block size */
1580 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1581 if (ret) {
1582 pr_err("cannot set SDIO block size\n");
1583 ret = -1;
1584 goto release_irq;
1585 }
1586
1587 sdio_release_host(func);
1588 sdio_set_drvdata(func, card);
1589
1590 adapter->dev = &func->dev;
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001591
1592 switch (func->device) {
WarheadsSE98e6b9d2012-04-24 15:57:21 -04001593 case SDIO_DEVICE_ID_MARVELL_8786:
1594 strcpy(adapter->fw_name, SD8786_DEFAULT_FW_NAME);
1595 break;
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001596 case SDIO_DEVICE_ID_MARVELL_8797:
1597 strcpy(adapter->fw_name, SD8797_DEFAULT_FW_NAME);
1598 break;
1599 case SDIO_DEVICE_ID_MARVELL_8787:
1600 default:
1601 strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
1602 break;
1603 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001604
1605 return 0;
1606
1607release_irq:
1608 sdio_release_irq(func);
1609disable_func:
1610 sdio_disable_func(func);
1611 sdio_release_host(func);
1612 adapter->card = NULL;
1613
1614 return -1;
1615}
1616
1617/*
1618 * This function initializes the SDIO driver.
1619 *
1620 * The following initializations steps are followed -
1621 * - Read the Host interrupt status register to acknowledge
1622 * the first interrupt got from bootloader
1623 * - Disable host interrupt mask register
1624 * - Get SDIO port
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001625 * - Initialize SDIO variables in card
1626 * - Allocate MP registers
1627 * - Allocate MPA Tx and Rx buffers
1628 */
1629static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1630{
1631 struct sdio_mmc_card *card = adapter->card;
1632 int ret;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001633 u32 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001634
1635 /*
1636 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1637 * from the bootloader. If we don't do this we get a interrupt
1638 * as soon as we register the irq.
1639 */
1640 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1641
1642 /* Disable host interrupt mask register for SDIO */
1643 mwifiex_sdio_disable_host_int(adapter);
1644
1645 /* Get SDIO ioport */
1646 mwifiex_init_sdio_ioport(adapter);
1647
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001648 /* Initialize SDIO variables in card */
1649 card->mp_rd_bitmap = 0;
1650 card->mp_wr_bitmap = 0;
1651 card->curr_rd_port = 1;
1652 card->curr_wr_port = 1;
1653
1654 card->mp_data_port_mask = DATA_PORT_MASK;
1655
1656 card->mpa_tx.buf_len = 0;
1657 card->mpa_tx.pkt_cnt = 0;
1658 card->mpa_tx.start_port = 0;
1659
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001660 card->mpa_tx.enabled = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001661 card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1662
1663 card->mpa_rx.buf_len = 0;
1664 card->mpa_rx.pkt_cnt = 0;
1665 card->mpa_rx.start_port = 0;
1666
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001667 card->mpa_rx.enabled = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001668 card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1669
1670 /* Allocate buffers for SDIO MP-A */
1671 card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
Joe Perches0d2e7a52013-02-03 17:28:14 +00001672 if (!card->mp_regs)
Christoph Fritzb53575e2011-05-08 22:50:09 +02001673 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001674
1675 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1676 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1677 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1678 if (ret) {
1679 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1680 kfree(card->mp_regs);
1681 return -1;
1682 }
1683
1684 return ret;
1685}
1686
1687/*
1688 * This function resets the MPA Tx and Rx buffers.
1689 */
1690static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1691{
1692 struct sdio_mmc_card *card = adapter->card;
1693
1694 MP_TX_AGGR_BUF_RESET(card);
1695 MP_RX_AGGR_BUF_RESET(card);
1696}
1697
1698/*
1699 * This function cleans up the allocated card buffers.
1700 *
1701 * The following are freed by this function -
1702 * - MP registers
1703 * - MPA Tx buffer
1704 * - MPA Rx buffer
1705 */
1706static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1707{
1708 struct sdio_mmc_card *card = adapter->card;
1709
1710 kfree(card->mp_regs);
1711 kfree(card->mpa_tx.buf);
1712 kfree(card->mpa_rx.buf);
1713}
1714
1715/*
1716 * This function updates the MP end port in card.
1717 */
1718static void
1719mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1720{
1721 struct sdio_mmc_card *card = adapter->card;
1722 int i;
1723
1724 card->mp_end_port = port;
1725
1726 card->mp_data_port_mask = DATA_PORT_MASK;
1727
1728 for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1729 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1730
1731 card->curr_wr_port = 1;
1732
1733 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001734 port, card->mp_data_port_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001735}
1736
Amitkumar Karward31ab352012-11-01 18:44:14 -07001737static struct mmc_host *reset_host;
1738static void sdio_card_reset_worker(struct work_struct *work)
1739{
Tejun Heo7f5855c2012-12-21 17:56:57 -08001740 struct mmc_host *target = reset_host;
1741
Amitkumar Karward31ab352012-11-01 18:44:14 -07001742 /* The actual reset operation must be run outside of driver thread.
1743 * This is because mmc_remove_host() will cause the device to be
1744 * instantly destroyed, and the driver then needs to end its thread,
1745 * leading to a deadlock.
1746 *
1747 * We run it in a totally independent workqueue.
1748 */
1749
1750 pr_err("Resetting card...\n");
Tejun Heo7f5855c2012-12-21 17:56:57 -08001751 mmc_remove_host(target);
Amitkumar Karward31ab352012-11-01 18:44:14 -07001752 /* 20ms delay is based on experiment with sdhci controller */
1753 mdelay(20);
Tejun Heo7f5855c2012-12-21 17:56:57 -08001754 mmc_add_host(target);
Amitkumar Karward31ab352012-11-01 18:44:14 -07001755}
1756static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1757
1758/* This function resets the card */
1759static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1760{
1761 struct sdio_mmc_card *card = adapter->card;
1762
Amitkumar Karward31ab352012-11-01 18:44:14 -07001763 reset_host = card->func->card->host;
1764 schedule_work(&card_reset_work);
1765}
1766
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001767static struct mwifiex_if_ops sdio_ops = {
1768 .init_if = mwifiex_init_sdio,
1769 .cleanup_if = mwifiex_cleanup_sdio,
1770 .check_fw_status = mwifiex_check_fw_status,
1771 .prog_fw = mwifiex_prog_fw_w_helper,
1772 .register_dev = mwifiex_register_dev,
1773 .unregister_dev = mwifiex_unregister_dev,
1774 .enable_int = mwifiex_sdio_enable_host_int,
1775 .process_int_status = mwifiex_process_int_status,
1776 .host_to_card = mwifiex_sdio_host_to_card,
1777 .wakeup = mwifiex_pm_wakeup_card,
1778 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1779
1780 /* SDIO specific */
1781 .update_mp_end_port = mwifiex_update_mp_end_port,
1782 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001783 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1784 .event_complete = mwifiex_sdio_event_complete,
Amitkumar Karward31ab352012-11-01 18:44:14 -07001785 .card_reset = mwifiex_sdio_card_reset,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001786};
1787
1788/*
1789 * This function initializes the SDIO driver.
1790 *
1791 * This initiates the semaphore and registers the device with
1792 * SDIO bus.
1793 */
1794static int
1795mwifiex_sdio_init_module(void)
1796{
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001797 sema_init(&add_remove_card_sem, 1);
1798
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301799 /* Clear the flag in case user removes the card. */
1800 user_rmmod = 0;
1801
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001802 return sdio_register_driver(&mwifiex_sdio);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001803}
1804
1805/*
1806 * This function cleans up the SDIO driver.
1807 *
1808 * The following major steps are followed for cleanup -
1809 * - Resume the device if its suspended
1810 * - Disconnect the device if connected
1811 * - Shutdown the firmware
1812 * - Unregister the device from SDIO bus.
1813 */
1814static void
1815mwifiex_sdio_cleanup_module(void)
1816{
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301817 if (!down_interruptible(&add_remove_card_sem))
1818 up(&add_remove_card_sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001819
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301820 /* Set the flag as user is removing this module. */
1821 user_rmmod = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001822
Amitkumar Karward31ab352012-11-01 18:44:14 -07001823 cancel_work_sync(&card_reset_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001824 sdio_unregister_driver(&mwifiex_sdio);
1825}
1826
1827module_init(mwifiex_sdio_init_module);
1828module_exit(mwifiex_sdio_cleanup_module);
1829
1830MODULE_AUTHOR("Marvell International Ltd.");
1831MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1832MODULE_VERSION(SDIO_VERSION);
1833MODULE_LICENSE("GPL v2");
WarheadsSE98e6b9d2012-04-24 15:57:21 -04001834MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001835MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
1836MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);