mwifiex: correction in sleep confirm command sequence number
[deliverable/linux.git] / drivers / net / wireless / mwifiex / sdio.c
CommitLineData
5e6e3a92
BZ
1/*
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
287546df
AK
34/* 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
5e6e3a92
BZ
49static struct mwifiex_if_ops sdio_ops;
50
51static struct semaphore add_remove_card_sem;
52
53/*
54 * SDIO probe.
55 *
56 * This function probes an mwifiex device and registers it. It allocates
57 * the card structure, enables SDIO function number and initiates the
58 * device registration and initialization procedure by adding a logical
59 * interface.
60 */
61static int
62mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
63{
270e58e8 64 int ret;
5e6e3a92
BZ
65 struct sdio_mmc_card *card = NULL;
66
67 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
5dbd326c 68 func->vendor, func->device, func->class, func->num);
5e6e3a92
BZ
69
70 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
e404decb 71 if (!card)
5e6e3a92 72 return -ENOMEM;
5e6e3a92
BZ
73
74 card->func = func;
75
76 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
77
05889f82
AK
78 if (id->driver_data) {
79 struct mwifiex_sdio_device *data = (void *)id->driver_data;
80
81 card->firmware = data->firmware;
82 card->reg = data->reg;
83 card->max_ports = data->max_ports;
84 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
b60186f8
YAP
85 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
86 card->has_control_mask = data->has_control_mask;
828cf222 87 card->tx_buf_size = data->tx_buf_size;
05889f82
AK
88 }
89
5e6e3a92
BZ
90 sdio_claim_host(func);
91 ret = sdio_enable_func(func);
92 sdio_release_host(func);
93
94 if (ret) {
95 pr_err("%s: failed to enable function\n", __func__);
b53575ec 96 kfree(card);
5e6e3a92
BZ
97 return -EIO;
98 }
99
d930faee
AK
100 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
101 MWIFIEX_SDIO)) {
5e6e3a92
BZ
102 pr_err("%s: add card failed\n", __func__);
103 kfree(card);
104 sdio_claim_host(func);
105 ret = sdio_disable_func(func);
106 sdio_release_host(func);
107 ret = -1;
108 }
109
110 return ret;
111}
112
2cdf359a
AK
113/*
114 * SDIO resume.
115 *
116 * Kernel needs to suspend all functions separately. Therefore all
117 * registered functions must have drivers with suspend and resume
118 * methods. Failing that the kernel simply removes the whole card.
119 *
120 * If already not resumed, this function turns on the traffic and
121 * sends a host sleep cancel request to the firmware.
122 */
123static int mwifiex_sdio_resume(struct device *dev)
124{
125 struct sdio_func *func = dev_to_sdio_func(dev);
126 struct sdio_mmc_card *card;
127 struct mwifiex_adapter *adapter;
128 mmc_pm_flag_t pm_flag = 0;
129
130 if (func) {
131 pm_flag = sdio_get_host_pm_caps(func);
132 card = sdio_get_drvdata(func);
133 if (!card || !card->adapter) {
134 pr_err("resume: invalid card or adapter\n");
135 return 0;
136 }
137 } else {
138 pr_err("resume: sdio_func is not specified\n");
139 return 0;
140 }
141
142 adapter = card->adapter;
143
144 if (!adapter->is_suspended) {
145 dev_warn(adapter->dev, "device already resumed\n");
146 return 0;
147 }
148
149 adapter->is_suspended = false;
150
151 /* Disable Host Sleep */
152 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
153 MWIFIEX_ASYNC_CMD);
154
155 return 0;
156}
157
5e6e3a92
BZ
158/*
159 * SDIO remove.
160 *
161 * This function removes the interface and frees up the card structure.
162 */
163static void
164mwifiex_sdio_remove(struct sdio_func *func)
165{
166 struct sdio_mmc_card *card;
287546df 167 struct mwifiex_adapter *adapter;
5dbd326c 168 struct mwifiex_private *priv;
5e6e3a92
BZ
169
170 pr_debug("info: SDIO func num=%d\n", func->num);
171
287546df
AK
172 card = sdio_get_drvdata(func);
173 if (!card)
174 return;
175
176 adapter = card->adapter;
177 if (!adapter || !adapter->priv_num)
178 return;
179
59a4cc25
AK
180 /* In case driver is removed when asynchronous FW load is in progress */
181 wait_for_completion(&adapter->fw_load);
182
287546df
AK
183 if (user_rmmod) {
184 if (adapter->is_suspended)
185 mwifiex_sdio_resume(adapter->dev);
186
848819f4 187 mwifiex_deauthenticate_all(adapter);
287546df 188
5dbd326c
YAP
189 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
190 mwifiex_disable_auto_ds(priv);
191 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
5e6e3a92 192 }
287546df
AK
193
194 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
5e6e3a92
BZ
195}
196
197/*
198 * SDIO suspend.
199 *
200 * Kernel needs to suspend all functions separately. Therefore all
201 * registered functions must have drivers with suspend and resume
202 * methods. Failing that the kernel simply removes the whole card.
203 *
204 * If already not suspended, this function allocates and sends a host
205 * sleep activate request to the firmware and turns off the traffic.
206 */
207static int mwifiex_sdio_suspend(struct device *dev)
208{
209 struct sdio_func *func = dev_to_sdio_func(dev);
210 struct sdio_mmc_card *card;
270e58e8 211 struct mwifiex_adapter *adapter;
5e6e3a92 212 mmc_pm_flag_t pm_flag = 0;
5e6e3a92
BZ
213 int ret = 0;
214
215 if (func) {
216 pm_flag = sdio_get_host_pm_caps(func);
217 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
5dbd326c 218 sdio_func_id(func), pm_flag);
5e6e3a92
BZ
219 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
220 pr_err("%s: cannot remain alive while host is"
221 " suspended\n", sdio_func_id(func));
222 return -ENOSYS;
223 }
224
225 card = sdio_get_drvdata(func);
226 if (!card || !card->adapter) {
227 pr_err("suspend: invalid card or adapter\n");
228 return 0;
229 }
230 } else {
231 pr_err("suspend: sdio_func is not specified\n");
232 return 0;
233 }
234
235 adapter = card->adapter;
236
237 /* Enable the Host Sleep */
dd321acd
BZ
238 if (!mwifiex_enable_hs(adapter)) {
239 dev_err(adapter->dev, "cmd: failed to suspend\n");
240 return -EFAULT;
5e6e3a92
BZ
241 }
242
dd321acd
BZ
243 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
244 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
245
5e6e3a92
BZ
246 /* Indicate device suspended */
247 adapter->is_suspended = true;
248
5e6e3a92
BZ
249 return ret;
250}
251
98e6b9df
W
252/* Device ID for SD8786 */
253#define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
5e6e3a92
BZ
254/* Device ID for SD8787 */
255#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
e3bea1c8
BZ
256/* Device ID for SD8797 */
257#define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
b60186f8
YAP
258/* Device ID for SD8897 */
259#define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
5e6e3a92
BZ
260
261/* WLAN IDs */
262static const struct sdio_device_id mwifiex_ids[] = {
05889f82
AK
263 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
264 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
265 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
266 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
267 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
268 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
b60186f8
YAP
269 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
270 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
5e6e3a92
BZ
271 {},
272};
273
274MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
275
276static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
277 .suspend = mwifiex_sdio_suspend,
278 .resume = mwifiex_sdio_resume,
279};
280
281static struct sdio_driver mwifiex_sdio = {
282 .name = "mwifiex_sdio",
283 .id_table = mwifiex_ids,
284 .probe = mwifiex_sdio_probe,
285 .remove = mwifiex_sdio_remove,
286 .drv = {
287 .owner = THIS_MODULE,
288 .pm = &mwifiex_sdio_pm_ops,
289 }
290};
291
232fde06
DD
292/* Write data into SDIO card register. Caller claims SDIO device. */
293static int
294mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
295{
296 int ret = -1;
297 sdio_writeb(func, data, reg, &ret);
298 return ret;
299}
300
5e6e3a92
BZ
301/*
302 * This function writes data into SDIO card register.
303 */
304static int
ab93d4ff 305mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
5e6e3a92
BZ
306{
307 struct sdio_mmc_card *card = adapter->card;
232fde06 308 int ret;
5e6e3a92
BZ
309
310 sdio_claim_host(card->func);
232fde06 311 ret = mwifiex_write_reg_locked(card->func, reg, data);
5e6e3a92
BZ
312 sdio_release_host(card->func);
313
314 return ret;
315}
316
317/*
318 * This function reads data from SDIO card register.
319 */
320static int
ab93d4ff 321mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
5e6e3a92
BZ
322{
323 struct sdio_mmc_card *card = adapter->card;
324 int ret = -1;
325 u8 val;
326
327 sdio_claim_host(card->func);
328 val = sdio_readb(card->func, reg, &ret);
329 sdio_release_host(card->func);
330
331 *data = val;
332
333 return ret;
334}
335
336/*
337 * This function writes multiple data into SDIO card memory.
338 *
339 * This does not work in suspended mode.
340 */
341static int
342mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
572e8f3e 343 u8 *buffer, u32 pkt_len, u32 port)
5e6e3a92
BZ
344{
345 struct sdio_mmc_card *card = adapter->card;
5b2e2ecc 346 int ret;
5e6e3a92
BZ
347 u8 blk_mode =
348 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
349 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
350 u32 blk_cnt =
351 (blk_mode ==
352 BLOCK_MODE) ? (pkt_len /
353 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
354 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
355
356 if (adapter->is_suspended) {
357 dev_err(adapter->dev,
358 "%s: not allowed while suspended\n", __func__);
359 return -1;
360 }
361
362 sdio_claim_host(card->func);
363
5b2e2ecc 364 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
5e6e3a92
BZ
365
366 sdio_release_host(card->func);
367
368 return ret;
369}
370
371/*
372 * This function reads multiple data from SDIO card memory.
373 */
572e8f3e
AK
374static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
375 u32 len, u32 port, u8 claim)
5e6e3a92
BZ
376{
377 struct sdio_mmc_card *card = adapter->card;
5b2e2ecc 378 int ret;
5dbd326c
YAP
379 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
380 : BLOCK_MODE;
5e6e3a92 381 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
5dbd326c
YAP
382 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
383 : len;
5e6e3a92
BZ
384 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
385
386 if (claim)
387 sdio_claim_host(card->func);
388
5b2e2ecc 389 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
5e6e3a92
BZ
390
391 if (claim)
392 sdio_release_host(card->func);
393
394 return ret;
395}
396
397/*
398 * This function wakes up the card.
399 *
400 * A host power up command is written to the card configuration
401 * register to wake up the card.
402 */
403static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
404{
5e6e3a92 405 dev_dbg(adapter->dev, "event: wakeup device...\n");
5e6e3a92 406
636c4598 407 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
5e6e3a92
BZ
408}
409
410/*
411 * This function is called after the card has woken up.
412 *
413 * The card configuration register is reset.
414 */
415static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
416{
5e6e3a92 417 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
5e6e3a92 418
636c4598 419 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
5e6e3a92
BZ
420}
421
422/*
b60186f8
YAP
423 * This function is used to initialize IO ports for the
424 * chipsets supporting SDIO new mode eg SD8897.
425 */
426static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
427{
428 u8 reg;
429
430 adapter->ioport = MEM_PORT;
431
432 /* enable sdio new mode */
433 if (mwifiex_read_reg(adapter, CARD_CONFIG_2_1_REG, &reg))
434 return -1;
435 if (mwifiex_write_reg(adapter, CARD_CONFIG_2_1_REG,
436 reg | CMD53_NEW_MODE))
437 return -1;
438
439 /* Configure cmd port and enable reading rx length from the register */
440 if (mwifiex_read_reg(adapter, CMD_CONFIG_0, &reg))
441 return -1;
442 if (mwifiex_write_reg(adapter, CMD_CONFIG_0, reg | CMD_PORT_RD_LEN_EN))
443 return -1;
444
445 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
446 * completed
447 */
448 if (mwifiex_read_reg(adapter, CMD_CONFIG_1, &reg))
449 return -1;
450 if (mwifiex_write_reg(adapter, CMD_CONFIG_1, reg | CMD_PORT_AUTO_EN))
451 return -1;
452
453 return 0;
454}
455
456/* This function initializes the IO ports.
5e6e3a92
BZ
457 *
458 * The following operations are performed -
459 * - Read the IO ports (0, 1 and 2)
460 * - Set host interrupt Reset-To-Read to clear
461 * - Set auto re-enable interrupt
462 */
463static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
464{
ab93d4ff 465 u8 reg;
05889f82 466 struct sdio_mmc_card *card = adapter->card;
5e6e3a92
BZ
467
468 adapter->ioport = 0;
469
b60186f8
YAP
470 if (card->supports_sdio_new_mode) {
471 if (mwifiex_init_sdio_new_mode(adapter))
472 return -1;
473 goto cont;
474 }
475
5e6e3a92
BZ
476 /* Read the IO port */
477 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
478 adapter->ioport |= (reg & 0xff);
479 else
480 return -1;
481
482 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
483 adapter->ioport |= ((reg & 0xff) << 8);
484 else
485 return -1;
486
487 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
488 adapter->ioport |= ((reg & 0xff) << 16);
489 else
490 return -1;
b60186f8 491cont:
5e6e3a92
BZ
492 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
493
494 /* Set Host interrupt reset to read to clear */
495 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
496 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
05889f82 497 reg | card->reg->sdio_int_mask);
5e6e3a92
BZ
498 else
499 return -1;
500
501 /* Dnld/Upld ready set to auto reset */
05889f82
AK
502 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
503 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
5e6e3a92
BZ
504 reg | AUTO_RE_ENABLE_INT);
505 else
506 return -1;
507
508 return 0;
509}
510
511/*
512 * This function sends data to the card.
513 */
514static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
515 u8 *payload, u32 pkt_len, u32 port)
516{
517 u32 i = 0;
270e58e8 518 int ret;
5e6e3a92
BZ
519
520 do {
572e8f3e 521 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
5e6e3a92
BZ
522 if (ret) {
523 i++;
524 dev_err(adapter->dev, "host_to_card, write iomem"
525 " (%d) failed: %d\n", i, ret);
5dbd326c 526 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
5e6e3a92
BZ
527 dev_err(adapter->dev, "write CFG reg failed\n");
528
529 ret = -1;
530 if (i > MAX_WRITE_IOMEM_RETRY)
531 return ret;
532 }
533 } while (ret == -1);
534
535 return ret;
536}
537
538/*
539 * This function gets the read port.
540 *
541 * If control port bit is set in MP read bitmap, the control port
542 * is returned, otherwise the current read port is returned and
543 * the value is increased (provided it does not reach the maximum
544 * limit, in which case it is reset to 1)
545 */
546static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
547{
548 struct sdio_mmc_card *card = adapter->card;
05889f82 549 const struct mwifiex_sdio_card_reg *reg = card->reg;
5ac253d5 550 u32 rd_bitmap = card->mp_rd_bitmap;
5e6e3a92 551
5ac253d5 552 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
5e6e3a92 553
b60186f8
YAP
554 if (card->supports_sdio_new_mode) {
555 if (!(rd_bitmap & reg->data_port_mask))
556 return -1;
557 } else {
558 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
559 return -1;
560 }
5e6e3a92 561
b60186f8
YAP
562 if ((card->has_control_mask) &&
563 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
5ac253d5 564 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
5e6e3a92 565 *port = CTRL_PORT;
5ac253d5 566 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%08x\n",
5dbd326c 567 *port, card->mp_rd_bitmap);
e6a52030 568 return 0;
5e6e3a92 569 }
e6a52030
AK
570
571 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
572 return -1;
573
574 /* We are now handling the SDIO data ports */
575 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
576 *port = card->curr_rd_port;
577
578 if (++card->curr_rd_port == card->max_ports)
579 card->curr_rd_port = reg->start_rd_port;
580
581 dev_dbg(adapter->dev,
582 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
583 *port, rd_bitmap, card->mp_rd_bitmap);
584
5e6e3a92
BZ
585 return 0;
586}
587
588/*
589 * This function gets the write port for data.
590 *
591 * The current write port is returned if available and the value is
592 * increased (provided it does not reach the maximum limit, in which
593 * case it is reset to 1)
594 */
0fc0d43b 595static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
5e6e3a92
BZ
596{
597 struct sdio_mmc_card *card = adapter->card;
b60186f8 598 const struct mwifiex_sdio_card_reg *reg = card->reg;
5ac253d5 599 u32 wr_bitmap = card->mp_wr_bitmap;
5e6e3a92 600
5ac253d5 601 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
5e6e3a92 602
b60186f8
YAP
603 if (card->supports_sdio_new_mode &&
604 !(wr_bitmap & reg->data_port_mask)) {
605 adapter->data_sent = true;
606 return -EBUSY;
607 } else if (!card->supports_sdio_new_mode &&
608 !(wr_bitmap & card->mp_data_port_mask)) {
5e6e3a92 609 return -1;
b60186f8 610 }
5e6e3a92
BZ
611
612 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
5ac253d5 613 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
5e6e3a92 614 *port = card->curr_wr_port;
b60186f8
YAP
615 if (((card->supports_sdio_new_mode) &&
616 (++card->curr_wr_port == card->max_ports)) ||
617 ((!card->supports_sdio_new_mode) &&
618 (++card->curr_wr_port == card->mp_end_port)))
619 card->curr_wr_port = reg->start_wr_port;
5e6e3a92
BZ
620 } else {
621 adapter->data_sent = true;
622 return -EBUSY;
623 }
624
b60186f8 625 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
5ac253d5
AK
626 dev_err(adapter->dev,
627 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
5dbd326c
YAP
628 *port, card->curr_wr_port, wr_bitmap,
629 card->mp_wr_bitmap);
5e6e3a92
BZ
630 return -1;
631 }
632
5ac253d5 633 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
5dbd326c 634 *port, wr_bitmap, card->mp_wr_bitmap);
5e6e3a92
BZ
635
636 return 0;
637}
638
639/*
640 * This function polls the card status.
641 */
642static int
643mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
644{
05889f82 645 struct sdio_mmc_card *card = adapter->card;
5e6e3a92 646 u32 tries;
ab93d4ff 647 u8 cs;
5e6e3a92
BZ
648
649 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
05889f82 650 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
5e6e3a92
BZ
651 break;
652 else if ((cs & bits) == bits)
653 return 0;
654
e7891ba2 655 usleep_range(10, 20);
5e6e3a92
BZ
656 }
657
5dbd326c
YAP
658 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
659
5e6e3a92
BZ
660 return -1;
661}
662
663/*
664 * This function reads the firmware status.
665 */
666static int
667mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
668{
05889f82
AK
669 struct sdio_mmc_card *card = adapter->card;
670 const struct mwifiex_sdio_card_reg *reg = card->reg;
ab93d4ff 671 u8 fws0, fws1;
5e6e3a92 672
05889f82 673 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
5e6e3a92
BZ
674 return -1;
675
05889f82 676 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
5e6e3a92
BZ
677 return -1;
678
679 *dat = (u16) ((fws1 << 8) | fws0);
680
681 return 0;
682}
683
684/*
685 * This function disables the host interrupt.
686 *
687 * The host interrupt mask is read, the disable bit is reset and
688 * written back to the card host interrupt mask register.
689 */
232fde06 690static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
5e6e3a92 691{
232fde06
DD
692 struct sdio_mmc_card *card = adapter->card;
693 struct sdio_func *func = card->func;
5e6e3a92 694
232fde06
DD
695 sdio_claim_host(func);
696 mwifiex_write_reg_locked(func, HOST_INT_MASK_REG, 0);
697 sdio_release_irq(func);
698 sdio_release_host(func);
5e6e3a92
BZ
699}
700
2cdf359a
AK
701/*
702 * This function reads the interrupt status from card.
703 */
704static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
705{
706 struct sdio_mmc_card *card = adapter->card;
707 u8 sdio_ireg;
708 unsigned long flags;
709
710 if (mwifiex_read_data_sync(adapter, card->mp_regs,
711 card->reg->max_mp_regs,
712 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
713 dev_err(adapter->dev, "read mp_regs failed\n");
714 return;
715 }
716
717 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
718 if (sdio_ireg) {
719 /*
720 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
721 * For SDIO new mode CMD port interrupts
722 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
723 * UP_LD_CMD_PORT_HOST_INT_STATUS
724 * Clear the interrupt status register
725 */
726 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
727 spin_lock_irqsave(&adapter->int_lock, flags);
728 adapter->int_status |= sdio_ireg;
729 spin_unlock_irqrestore(&adapter->int_lock, flags);
730 }
731}
732
733/*
734 * SDIO interrupt handler.
735 *
736 * This function reads the interrupt status from firmware and handles
737 * the interrupt in current thread (ksdioirqd) right away.
738 */
739static void
740mwifiex_sdio_interrupt(struct sdio_func *func)
741{
742 struct mwifiex_adapter *adapter;
743 struct sdio_mmc_card *card;
744
745 card = sdio_get_drvdata(func);
746 if (!card || !card->adapter) {
747 pr_debug("int: func=%p card=%p adapter=%p\n",
748 func, card, card ? card->adapter : NULL);
749 return;
750 }
751 adapter = card->adapter;
752
753 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
754 adapter->ps_state = PS_STATE_AWAKE;
755
756 mwifiex_interrupt_status(adapter);
757 mwifiex_main_process(adapter);
758}
759
5e6e3a92
BZ
760/*
761 * This function enables the host interrupt.
762 *
763 * The host interrupt enable mask is written to the card
764 * host interrupt mask register.
765 */
766static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
767{
05889f82 768 struct sdio_mmc_card *card = adapter->card;
232fde06
DD
769 struct sdio_func *func = card->func;
770 int ret;
771
772 sdio_claim_host(func);
773
774 /* Request the SDIO IRQ */
775 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
776 if (ret) {
777 dev_err(adapter->dev, "claim irq failed: ret=%d\n", ret);
778 goto out;
779 }
05889f82 780
5e6e3a92 781 /* Simply write the mask to the register */
232fde06
DD
782 ret = mwifiex_write_reg_locked(func, HOST_INT_MASK_REG,
783 card->reg->host_int_enable);
784 if (ret) {
5e6e3a92 785 dev_err(adapter->dev, "enable host interrupt failed\n");
232fde06 786 sdio_release_irq(func);
5e6e3a92 787 }
232fde06
DD
788
789out:
790 sdio_release_host(func);
791 return ret;
5e6e3a92
BZ
792}
793
794/*
795 * This function sends a data buffer to the card.
796 */
797static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
798 u32 *type, u8 *buffer,
799 u32 npayload, u32 ioport)
800{
270e58e8 801 int ret;
5e6e3a92
BZ
802 u32 nb;
803
804 if (!buffer) {
805 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
806 return -1;
807 }
808
572e8f3e 809 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
5e6e3a92
BZ
810
811 if (ret) {
812 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
5dbd326c 813 ret);
5e6e3a92
BZ
814 return -1;
815 }
816
817 nb = le16_to_cpu(*(__le16 *) (buffer));
818 if (nb > npayload) {
5dbd326c
YAP
819 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
820 __func__, nb, npayload);
5e6e3a92
BZ
821 return -1;
822 }
823
824 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
825
826 return ret;
827}
828
829/*
830 * This function downloads the firmware to the card.
831 *
832 * Firmware is downloaded to the card in blocks. Every block download
833 * is tested for CRC errors, and retried a number of times before
834 * returning failure.
835 */
836static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
837 struct mwifiex_fw_image *fw)
838{
05889f82
AK
839 struct sdio_mmc_card *card = adapter->card;
840 const struct mwifiex_sdio_card_reg *reg = card->reg;
270e58e8 841 int ret;
5e6e3a92
BZ
842 u8 *firmware = fw->fw_buf;
843 u32 firmware_len = fw->fw_len;
844 u32 offset = 0;
ab93d4ff 845 u8 base0, base1;
5e6e3a92
BZ
846 u8 *fwbuf;
847 u16 len = 0;
270e58e8 848 u32 txlen, tx_blocks = 0, tries;
5e6e3a92
BZ
849 u32 i = 0;
850
851 if (!firmware_len) {
5dbd326c
YAP
852 dev_err(adapter->dev,
853 "firmware image not found! Terminating download\n");
5e6e3a92
BZ
854 return -1;
855 }
856
857 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
5dbd326c 858 firmware_len);
5e6e3a92
BZ
859
860 /* Assume that the allocated buffer is 8-byte aligned */
861 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
0d2e7a5c 862 if (!fwbuf)
b53575ec 863 return -ENOMEM;
5e6e3a92
BZ
864
865 /* Perform firmware data transfer */
866 do {
867 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
868 bits */
869 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
870 DN_LD_CARD_RDY);
871 if (ret) {
872 dev_err(adapter->dev, "FW download with helper:"
5dbd326c 873 " poll status timeout @ %d\n", offset);
5e6e3a92
BZ
874 goto done;
875 }
876
877 /* More data? */
878 if (offset >= firmware_len)
879 break;
880
881 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
05889f82 882 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
5e6e3a92
BZ
883 &base0);
884 if (ret) {
5dbd326c
YAP
885 dev_err(adapter->dev,
886 "dev BASE0 register read failed: "
887 "base0=%#04X(%d). Terminating dnld\n",
888 base0, base0);
5e6e3a92
BZ
889 goto done;
890 }
05889f82 891 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
5e6e3a92
BZ
892 &base1);
893 if (ret) {
5dbd326c
YAP
894 dev_err(adapter->dev,
895 "dev BASE1 register read failed: "
896 "base1=%#04X(%d). Terminating dnld\n",
897 base1, base1);
5e6e3a92
BZ
898 goto done;
899 }
900 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
901
902 if (len)
903 break;
904
e7891ba2 905 usleep_range(10, 20);
5e6e3a92
BZ
906 }
907
908 if (!len) {
909 break;
910 } else if (len > MWIFIEX_UPLD_SIZE) {
5dbd326c
YAP
911 dev_err(adapter->dev,
912 "FW dnld failed @ %d, invalid length %d\n",
913 offset, len);
5e6e3a92
BZ
914 ret = -1;
915 goto done;
916 }
917
918 txlen = len;
919
920 if (len & BIT(0)) {
921 i++;
922 if (i > MAX_WRITE_IOMEM_RETRY) {
5dbd326c
YAP
923 dev_err(adapter->dev,
924 "FW dnld failed @ %d, over max retry\n",
925 offset);
5e6e3a92
BZ
926 ret = -1;
927 goto done;
928 }
929 dev_err(adapter->dev, "CRC indicated by the helper:"
5dbd326c 930 " len = 0x%04X, txlen = %d\n", len, txlen);
5e6e3a92
BZ
931 len &= ~BIT(0);
932 /* Setting this to 0 to resend from same offset */
933 txlen = 0;
934 } else {
935 i = 0;
936
937 /* Set blocksize to transfer - checking for last
938 block */
939 if (firmware_len - offset < txlen)
940 txlen = firmware_len - offset;
941
5dbd326c
YAP
942 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
943 / MWIFIEX_SDIO_BLOCK_SIZE;
5e6e3a92
BZ
944
945 /* Copy payload to buffer */
946 memmove(fwbuf, &firmware[offset], txlen);
947 }
948
949 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
950 MWIFIEX_SDIO_BLOCK_SIZE,
572e8f3e 951 adapter->ioport);
5e6e3a92 952 if (ret) {
5dbd326c
YAP
953 dev_err(adapter->dev,
954 "FW download, write iomem (%d) failed @ %d\n",
955 i, offset);
5e6e3a92
BZ
956 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
957 dev_err(adapter->dev, "write CFG reg failed\n");
958
959 ret = -1;
960 goto done;
961 }
962
963 offset += txlen;
964 } while (true);
965
966 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
5dbd326c 967 offset);
5e6e3a92
BZ
968
969 ret = 0;
970done:
971 kfree(fwbuf);
972 return ret;
973}
974
975/*
976 * This function checks the firmware status in card.
977 *
978 * The winner interface is also determined by this function.
979 */
980static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
d930faee 981 u32 poll_num)
5e6e3a92 982{
05889f82 983 struct sdio_mmc_card *card = adapter->card;
5e6e3a92
BZ
984 int ret = 0;
985 u16 firmware_stat;
986 u32 tries;
ab93d4ff 987 u8 winner_status;
5e6e3a92
BZ
988
989 /* Wait for firmware initialization event */
990 for (tries = 0; tries < poll_num; tries++) {
991 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
992 if (ret)
993 continue;
d930faee 994 if (firmware_stat == FIRMWARE_READY_SDIO) {
5e6e3a92
BZ
995 ret = 0;
996 break;
997 } else {
a76b20e5 998 msleep(100);
5e6e3a92
BZ
999 ret = -1;
1000 }
1001 }
1002
d930faee 1003 if (ret) {
5e6e3a92 1004 if (mwifiex_read_reg
05889f82 1005 (adapter, card->reg->status_reg_0, &winner_status))
5e6e3a92
BZ
1006 winner_status = 0;
1007
1008 if (winner_status)
d930faee 1009 adapter->winner = 0;
5e6e3a92 1010 else
d930faee 1011 adapter->winner = 1;
5e6e3a92
BZ
1012 }
1013 return ret;
1014}
1015
5e6e3a92
BZ
1016/*
1017 * This function decodes a received packet.
1018 *
1019 * Based on the type, the packet is treated as either a data, or
1020 * a command response, or an event, and the correct handler
1021 * function is invoked.
1022 */
1023static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1024 struct sk_buff *skb, u32 upld_typ)
1025{
1026 u8 *cmd_buf;
d03b4aa7
AP
1027 __le16 *curr_ptr = (__le16 *)skb->data;
1028 u16 pkt_len = le16_to_cpu(*curr_ptr);
5e6e3a92 1029
d03b4aa7 1030 skb_trim(skb, pkt_len);
5e6e3a92
BZ
1031 skb_pull(skb, INTF_HEADER_LEN);
1032
1033 switch (upld_typ) {
1034 case MWIFIEX_TYPE_DATA:
1035 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
1036 mwifiex_handle_rx_packet(adapter, skb);
1037 break;
1038
1039 case MWIFIEX_TYPE_CMD:
1040 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
1041 /* take care of curr_cmd = NULL case */
1042 if (!adapter->curr_cmd) {
1043 cmd_buf = adapter->upld_buf;
1044
1045 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1046 mwifiex_process_sleep_confirm_resp(adapter,
5dbd326c
YAP
1047 skb->data,
1048 skb->len);
5e6e3a92 1049
5dbd326c
YAP
1050 memcpy(cmd_buf, skb->data,
1051 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1052 skb->len));
5e6e3a92
BZ
1053
1054 dev_kfree_skb_any(skb);
1055 } else {
1056 adapter->cmd_resp_received = true;
1057 adapter->curr_cmd->resp_skb = skb;
1058 }
1059 break;
1060
1061 case MWIFIEX_TYPE_EVENT:
1062 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
cfcd926e 1063 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
5e6e3a92 1064
5e6e3a92 1065 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
e80c81dc
AK
1066 memcpy(adapter->event_body,
1067 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1068 skb->len);
5e6e3a92
BZ
1069
1070 /* event cause has been saved to adapter->event_cause */
1071 adapter->event_received = true;
1072 adapter->event_skb = skb;
1073
1074 break;
1075
1076 default:
1077 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
1078 dev_kfree_skb_any(skb);
1079 break;
1080 }
1081
1082 return 0;
1083}
1084
1085/*
1086 * This function transfers received packets from card to driver, performing
1087 * aggregation if required.
1088 *
1089 * For data received on control port, or if aggregation is disabled, the
1090 * received buffers are uploaded as separate packets. However, if aggregation
1091 * is enabled and required, the buffers are copied onto an aggregation buffer,
1092 * provided there is space left, processed and finally uploaded.
1093 */
1094static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1095 struct sk_buff *skb, u8 port)
1096{
1097 struct sdio_mmc_card *card = adapter->card;
1098 s32 f_do_rx_aggr = 0;
1099 s32 f_do_rx_cur = 0;
1100 s32 f_aggr_cur = 0;
1101 struct sk_buff *skb_deaggr;
270e58e8 1102 u32 pind;
0fc0d43b 1103 u32 pkt_len, pkt_type, mport;
5e6e3a92
BZ
1104 u8 *curr_ptr;
1105 u32 rx_len = skb->len;
1106
b60186f8 1107 if ((card->has_control_mask) && (port == CTRL_PORT)) {
5e6e3a92
BZ
1108 /* Read the command Resp without aggr */
1109 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
5dbd326c 1110 "response\n", __func__);
5e6e3a92
BZ
1111
1112 f_do_rx_cur = 1;
1113 goto rx_curr_single;
1114 }
1115
1116 if (!card->mpa_rx.enabled) {
1117 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
5dbd326c 1118 __func__);
5e6e3a92
BZ
1119
1120 f_do_rx_cur = 1;
1121 goto rx_curr_single;
1122 }
1123
b60186f8
YAP
1124 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1125 card->reg->data_port_mask)) ||
1126 (card->has_control_mask && (card->mp_rd_bitmap &
1127 (~((u32) CTRL_PORT_MASK))))) {
5e6e3a92
BZ
1128 /* Some more data RX pending */
1129 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1130
1131 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1132 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1133 f_aggr_cur = 1;
1134 } else {
1135 /* No room in Aggr buf, do rx aggr now */
1136 f_do_rx_aggr = 1;
1137 f_do_rx_cur = 1;
1138 }
1139 } else {
1140 /* Rx aggr not in progress */
1141 f_aggr_cur = 1;
1142 }
1143
1144 } else {
1145 /* No more data RX pending */
1146 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1147
1148 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1149 f_do_rx_aggr = 1;
1150 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1151 f_aggr_cur = 1;
1152 else
1153 /* No room in Aggr buf, do rx aggr now */
1154 f_do_rx_cur = 1;
1155 } else {
1156 f_do_rx_cur = 1;
1157 }
1158 }
1159
1160 if (f_aggr_cur) {
1161 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1162 /* Curr pkt can be aggregated */
c23b7c8f 1163 mp_rx_aggr_setup(card, skb, port);
5e6e3a92
BZ
1164
1165 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
c23b7c8f 1166 mp_rx_aggr_port_limit_reached(card)) {
5e6e3a92 1167 dev_dbg(adapter->dev, "info: %s: aggregated packet "
5dbd326c 1168 "limit reached\n", __func__);
5e6e3a92
BZ
1169 /* No more pkts allowed in Aggr buf, rx it */
1170 f_do_rx_aggr = 1;
1171 }
1172 }
1173
1174 if (f_do_rx_aggr) {
1175 /* do aggr RX now */
1176 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
5dbd326c 1177 card->mpa_rx.pkt_cnt);
5e6e3a92 1178
b60186f8
YAP
1179 if (card->supports_sdio_new_mode) {
1180 int i;
1181 u32 port_count;
1182
1183 for (i = 0, port_count = 0; i < card->max_ports; i++)
1184 if (card->mpa_rx.ports & BIT(i))
1185 port_count++;
1186
1187 /* Reading data from "start_port + 0" to "start_port +
1188 * port_count -1", so decrease the count by 1
1189 */
1190 port_count--;
1191 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1192 (port_count << 8)) + card->mpa_rx.start_port;
1193 } else {
1194 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1195 (card->mpa_rx.ports << 4)) +
1196 card->mpa_rx.start_port;
1197 }
0fc0d43b 1198
5e6e3a92 1199 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
0fc0d43b 1200 card->mpa_rx.buf_len, mport, 1))
17a60b48 1201 goto error;
5e6e3a92
BZ
1202
1203 curr_ptr = card->mpa_rx.buf;
1204
1205 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1206
1207 /* get curr PKT len & type */
cfcd926e
TW
1208 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1209 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
5e6e3a92
BZ
1210
1211 /* copy pkt to deaggr buf */
1212 skb_deaggr = card->mpa_rx.skb_arr[pind];
1213
1214 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1215 card->mpa_rx.len_arr[pind])) {
1216
1217 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1218
1219 skb_trim(skb_deaggr, pkt_len);
1220
1221 /* Process de-aggr packet */
1222 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1223 pkt_type);
1224 } else {
1225 dev_err(adapter->dev, "wrong aggr pkt:"
1226 " type=%d len=%d max_len=%d\n",
1227 pkt_type, pkt_len,
1228 card->mpa_rx.len_arr[pind]);
1229 dev_kfree_skb_any(skb_deaggr);
1230 }
1231 curr_ptr += card->mpa_rx.len_arr[pind];
1232 }
1233 MP_RX_AGGR_BUF_RESET(card);
1234 }
1235
1236rx_curr_single:
1237 if (f_do_rx_cur) {
1238 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1239 port, rx_len);
1240
1241 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1242 skb->data, skb->len,
1243 adapter->ioport + port))
17a60b48 1244 goto error;
5e6e3a92
BZ
1245
1246 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1247 }
1248
1249 return 0;
17a60b48
AP
1250
1251error:
1252 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1253 /* Multiport-aggregation transfer failed - cleanup */
1254 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1255 /* copy pkt to deaggr buf */
1256 skb_deaggr = card->mpa_rx.skb_arr[pind];
1257 dev_kfree_skb_any(skb_deaggr);
1258 }
1259 MP_RX_AGGR_BUF_RESET(card);
1260 }
1261
1262 if (f_do_rx_cur)
1263 /* Single transfer pending. Free curr buff also */
1264 dev_kfree_skb_any(skb);
1265
1266 return -1;
5e6e3a92
BZ
1267}
1268
1269/*
1270 * This function checks the current interrupt status.
1271 *
1272 * The following interrupts are checked and handled by this function -
1273 * - Data sent
1274 * - Command sent
1275 * - Packets received
1276 *
1277 * Since the firmware does not generate download ready interrupt if the
1278 * port updated is command port only, command sent interrupt checking
1279 * should be done manually, and for every SDIO interrupt.
1280 *
1281 * In case of Rx packets received, the packets are uploaded from card to
1282 * host and processed accordingly.
1283 */
1284static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1285{
1286 struct sdio_mmc_card *card = adapter->card;
05889f82 1287 const struct mwifiex_sdio_card_reg *reg = card->reg;
5e6e3a92
BZ
1288 int ret = 0;
1289 u8 sdio_ireg;
270e58e8 1290 struct sk_buff *skb;
5e6e3a92
BZ
1291 u8 port = CTRL_PORT;
1292 u32 len_reg_l, len_reg_u;
1293 u32 rx_blocks;
1294 u16 rx_len;
1295 unsigned long flags;
b60186f8
YAP
1296 u32 bitmap;
1297 u8 cr;
5e6e3a92
BZ
1298
1299 spin_lock_irqsave(&adapter->int_lock, flags);
1300 sdio_ireg = adapter->int_status;
1301 adapter->int_status = 0;
1302 spin_unlock_irqrestore(&adapter->int_lock, flags);
1303
1304 if (!sdio_ireg)
1305 return ret;
1306
b60186f8
YAP
1307 /* Following interrupt is only for SDIO new mode */
1308 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1309 adapter->cmd_sent = false;
1310
1311 /* Following interrupt is only for SDIO new mode */
1312 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1313 u32 pkt_type;
1314
1315 /* read the len of control packet */
1316 rx_len = card->mp_regs[CMD_RD_LEN_1] << 8;
1317 rx_len |= (u16) card->mp_regs[CMD_RD_LEN_0];
1318 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1319 if (rx_len <= INTF_HEADER_LEN ||
1320 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1321 MWIFIEX_RX_DATA_BUF_SIZE)
1322 return -1;
1323 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1324
1325 skb = dev_alloc_skb(rx_len);
1326 if (!skb)
1327 return -1;
1328
1329 skb_put(skb, rx_len);
1330
1331 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1332 skb->len, adapter->ioport |
1333 CMD_PORT_SLCT)) {
1334 dev_err(adapter->dev,
1335 "%s: failed to card_to_host", __func__);
1336 dev_kfree_skb_any(skb);
1337 goto term_cmd;
1338 }
1339
1340 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1341 (pkt_type != MWIFIEX_TYPE_EVENT))
1342 dev_err(adapter->dev,
1343 "%s:Received wrong packet on cmd port",
1344 __func__);
1345
1346 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1347 }
1348
5e6e3a92 1349 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
b60186f8
YAP
1350 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1351 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1352 if (card->supports_sdio_new_mode) {
1353 bitmap |=
1354 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1355 bitmap |=
1356 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1357 }
1358 card->mp_wr_bitmap = bitmap;
1359
1360 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%x\n",
5dbd326c 1361 card->mp_wr_bitmap);
5e6e3a92
BZ
1362 if (adapter->data_sent &&
1363 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1364 dev_dbg(adapter->dev,
1365 "info: <--- Tx DONE Interrupt --->\n");
1366 adapter->data_sent = false;
1367 }
1368 }
1369
1370 /* As firmware will not generate download ready interrupt if the port
1371 updated is command port only, cmd_sent should be done for any SDIO
1372 interrupt. */
b60186f8 1373 if (card->has_control_mask && adapter->cmd_sent) {
5e6e3a92
BZ
1374 /* Check if firmware has attach buffer at command port and
1375 update just that in wr_bit_map. */
1376 card->mp_wr_bitmap |=
05889f82 1377 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
5e6e3a92
BZ
1378 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1379 adapter->cmd_sent = false;
1380 }
1381
1382 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
5dbd326c 1383 adapter->cmd_sent, adapter->data_sent);
5e6e3a92 1384 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
b60186f8
YAP
1385 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1386 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1387 if (card->supports_sdio_new_mode) {
1388 bitmap |=
1389 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1390 bitmap |=
1391 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1392 }
1393 card->mp_rd_bitmap = bitmap;
1394 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%x\n",
5dbd326c 1395 card->mp_rd_bitmap);
5e6e3a92
BZ
1396
1397 while (true) {
1398 ret = mwifiex_get_rd_port(adapter, &port);
1399 if (ret) {
1400 dev_dbg(adapter->dev,
1401 "info: no more rd_port available\n");
1402 break;
1403 }
05889f82
AK
1404 len_reg_l = reg->rd_len_p0_l + (port << 1);
1405 len_reg_u = reg->rd_len_p0_u + (port << 1);
5e6e3a92
BZ
1406 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1407 rx_len |= (u16) card->mp_regs[len_reg_l];
1408 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
5dbd326c 1409 port, rx_len);
5e6e3a92
BZ
1410 rx_blocks =
1411 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1412 1) / MWIFIEX_SDIO_BLOCK_SIZE;
5dbd326c
YAP
1413 if (rx_len <= INTF_HEADER_LEN ||
1414 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1415 MWIFIEX_RX_DATA_BUF_SIZE) {
5e6e3a92 1416 dev_err(adapter->dev, "invalid rx_len=%d\n",
5dbd326c 1417 rx_len);
5e6e3a92
BZ
1418 return -1;
1419 }
1420 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1421
1422 skb = dev_alloc_skb(rx_len);
1423
1424 if (!skb) {
1425 dev_err(adapter->dev, "%s: failed to alloc skb",
5dbd326c 1426 __func__);
5e6e3a92
BZ
1427 return -1;
1428 }
1429
1430 skb_put(skb, rx_len);
1431
1432 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
5dbd326c 1433 rx_len, skb->len);
5e6e3a92
BZ
1434
1435 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1436 port)) {
5e6e3a92 1437 dev_err(adapter->dev, "card_to_host_mpa failed:"
5dbd326c 1438 " int status=%#x\n", sdio_ireg);
b60186f8 1439 goto term_cmd;
5e6e3a92
BZ
1440 }
1441 }
1442 }
1443
1444 return 0;
b60186f8
YAP
1445
1446term_cmd:
1447 /* terminate cmd */
1448 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1449 dev_err(adapter->dev, "read CFG reg failed\n");
1450 else
1451 dev_dbg(adapter->dev, "info: CFG reg val = %d\n", cr);
1452
1453 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1454 dev_err(adapter->dev, "write CFG reg failed\n");
1455 else
1456 dev_dbg(adapter->dev, "info: write success\n");
1457
1458 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1459 dev_err(adapter->dev, "read CFG reg failed\n");
1460 else
1461 dev_dbg(adapter->dev, "info: CFG reg val =%x\n", cr);
1462
1463 return -1;
5e6e3a92
BZ
1464}
1465
1466/*
1467 * This function aggregates transmission buffers in driver and downloads
1468 * the aggregated packet to card.
1469 *
1470 * The individual packets are aggregated by copying into an aggregation
1471 * buffer and then downloaded to the card. Previous unsent packets in the
1472 * aggregation buffer are pre-copied first before new packets are added.
1473 * Aggregation is done till there is space left in the aggregation buffer,
1474 * or till new packets are available.
1475 *
1476 * The function will only download the packet to the card when aggregation
1477 * stops, otherwise it will just aggregate the packet in aggregation buffer
1478 * and return.
1479 */
1480static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
0fc0d43b 1481 u8 *payload, u32 pkt_len, u32 port,
5e6e3a92
BZ
1482 u32 next_pkt_len)
1483{
1484 struct sdio_mmc_card *card = adapter->card;
1485 int ret = 0;
1486 s32 f_send_aggr_buf = 0;
1487 s32 f_send_cur_buf = 0;
1488 s32 f_precopy_cur_buf = 0;
1489 s32 f_postcopy_cur_buf = 0;
0fc0d43b 1490 u32 mport;
5e6e3a92 1491
b60186f8
YAP
1492 if (!card->mpa_tx.enabled ||
1493 (card->has_control_mask && (port == CTRL_PORT)) ||
1494 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
5e6e3a92 1495 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
5dbd326c 1496 __func__);
5e6e3a92
BZ
1497
1498 f_send_cur_buf = 1;
1499 goto tx_curr_single;
1500 }
1501
1502 if (next_pkt_len) {
1503 /* More pkt in TX queue */
1504 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
5dbd326c 1505 __func__);
5e6e3a92
BZ
1506
1507 if (MP_TX_AGGR_IN_PROGRESS(card)) {
c23b7c8f 1508 if (!mp_tx_aggr_port_limit_reached(card) &&
5e6e3a92
BZ
1509 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1510 f_precopy_cur_buf = 1;
1511
1512 if (!(card->mp_wr_bitmap &
5dbd326c
YAP
1513 (1 << card->curr_wr_port)) ||
1514 !MP_TX_AGGR_BUF_HAS_ROOM(
1515 card, pkt_len + next_pkt_len))
5e6e3a92
BZ
1516 f_send_aggr_buf = 1;
1517 } else {
1518 /* No room in Aggr buf, send it */
1519 f_send_aggr_buf = 1;
1520
c23b7c8f 1521 if (mp_tx_aggr_port_limit_reached(card) ||
5e6e3a92
BZ
1522 !(card->mp_wr_bitmap &
1523 (1 << card->curr_wr_port)))
1524 f_send_cur_buf = 1;
1525 else
1526 f_postcopy_cur_buf = 1;
1527 }
1528 } else {
5dbd326c
YAP
1529 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1530 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
5e6e3a92
BZ
1531 f_precopy_cur_buf = 1;
1532 else
1533 f_send_cur_buf = 1;
1534 }
1535 } else {
1536 /* Last pkt in TX queue */
1537 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
5dbd326c 1538 __func__);
5e6e3a92
BZ
1539
1540 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1541 /* some packs in Aggr buf already */
1542 f_send_aggr_buf = 1;
1543
1544 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1545 f_precopy_cur_buf = 1;
1546 else
1547 /* No room in Aggr buf, send it */
1548 f_send_cur_buf = 1;
1549 } else {
1550 f_send_cur_buf = 1;
1551 }
1552 }
1553
1554 if (f_precopy_cur_buf) {
1555 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
5dbd326c 1556 __func__);
5e6e3a92
BZ
1557 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1558
1559 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
c23b7c8f 1560 mp_tx_aggr_port_limit_reached(card))
5e6e3a92
BZ
1561 /* No more pkts allowed in Aggr buf, send it */
1562 f_send_aggr_buf = 1;
1563 }
1564
1565 if (f_send_aggr_buf) {
1566 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
5dbd326c 1567 __func__,
5e6e3a92 1568 card->mpa_tx.start_port, card->mpa_tx.ports);
b60186f8
YAP
1569 if (card->supports_sdio_new_mode) {
1570 u32 port_count;
1571 int i;
1572
1573 for (i = 0, port_count = 0; i < card->max_ports; i++)
1574 if (card->mpa_tx.ports & BIT(i))
1575 port_count++;
1576
1577 /* Writing data from "start_port + 0" to "start_port +
1578 * port_count -1", so decrease the count by 1
1579 */
1580 port_count--;
1581 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1582 (port_count << 8)) + card->mpa_tx.start_port;
1583 } else {
1584 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1585 (card->mpa_tx.ports << 4)) +
1586 card->mpa_tx.start_port;
1587 }
1588
5e6e3a92 1589 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
0fc0d43b 1590 card->mpa_tx.buf_len, mport);
5e6e3a92
BZ
1591
1592 MP_TX_AGGR_BUF_RESET(card);
1593 }
1594
1595tx_curr_single:
1596 if (f_send_cur_buf) {
1597 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
5dbd326c 1598 __func__, port);
5e6e3a92
BZ
1599 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1600 adapter->ioport + port);
1601 }
1602
1603 if (f_postcopy_cur_buf) {
1604 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
5dbd326c 1605 __func__);
5e6e3a92
BZ
1606 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1607 }
1608
1609 return ret;
1610}
1611
1612/*
1613 * This function downloads data from driver to card.
1614 *
1615 * Both commands and data packets are transferred to the card by this
1616 * function.
1617 *
1618 * This function adds the SDIO specific header to the front of the buffer
1619 * before transferring. The header contains the length of the packet and
1620 * the type. The firmware handles the packets based upon this set type.
1621 */
1622static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
d930faee 1623 u8 type, struct sk_buff *skb,
5e6e3a92
BZ
1624 struct mwifiex_tx_param *tx_param)
1625{
1626 struct sdio_mmc_card *card = adapter->card;
270e58e8 1627 int ret;
5e6e3a92
BZ
1628 u32 buf_block_len;
1629 u32 blk_size;
0fc0d43b 1630 u32 port = CTRL_PORT;
d930faee
AK
1631 u8 *payload = (u8 *)skb->data;
1632 u32 pkt_len = skb->len;
5e6e3a92
BZ
1633
1634 /* Allocate buffer and copy payload */
1635 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1636 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
83e612f6
TM
1637 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1638 *(__le16 *)&payload[2] = cpu_to_le16(type);
5e6e3a92
BZ
1639
1640 /*
1641 * This is SDIO specific header
1642 * u16 length,
1643 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1644 * MWIFIEX_TYPE_EVENT = 3)
1645 */
1646 if (type == MWIFIEX_TYPE_DATA) {
1647 ret = mwifiex_get_wr_port_data(adapter, &port);
1648 if (ret) {
1649 dev_err(adapter->dev, "%s: no wr_port available\n",
5dbd326c 1650 __func__);
5e6e3a92
BZ
1651 return ret;
1652 }
1653 } else {
1654 adapter->cmd_sent = true;
1655 /* Type must be MWIFIEX_TYPE_CMD */
1656
1657 if (pkt_len <= INTF_HEADER_LEN ||
1658 pkt_len > MWIFIEX_UPLD_SIZE)
1659 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
5dbd326c 1660 __func__, payload, pkt_len);
b60186f8
YAP
1661
1662 if (card->supports_sdio_new_mode)
1663 port = CMD_PORT_SLCT;
5e6e3a92
BZ
1664 }
1665
1666 /* Transfer data to card */
1667 pkt_len = buf_block_len * blk_size;
1668
1669 if (tx_param)
1670 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
5dbd326c
YAP
1671 port, tx_param->next_pkt_len
1672 );
5e6e3a92
BZ
1673 else
1674 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
5dbd326c 1675 port, 0);
5e6e3a92
BZ
1676
1677 if (ret) {
1678 if (type == MWIFIEX_TYPE_CMD)
1679 adapter->cmd_sent = false;
1680 if (type == MWIFIEX_TYPE_DATA)
1681 adapter->data_sent = false;
1682 } else {
1683 if (type == MWIFIEX_TYPE_DATA) {
1684 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1685 adapter->data_sent = true;
1686 else
1687 adapter->data_sent = false;
1688 }
1689 }
1690
1691 return ret;
1692}
1693
1694/*
1695 * This function allocates the MPA Tx and Rx buffers.
1696 */
1697static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1698 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1699{
1700 struct sdio_mmc_card *card = adapter->card;
1701 int ret = 0;
1702
1703 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1704 if (!card->mpa_tx.buf) {
5e6e3a92
BZ
1705 ret = -1;
1706 goto error;
1707 }
1708
1709 card->mpa_tx.buf_size = mpa_tx_buf_size;
1710
1711 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1712 if (!card->mpa_rx.buf) {
5e6e3a92
BZ
1713 ret = -1;
1714 goto error;
1715 }
1716
1717 card->mpa_rx.buf_size = mpa_rx_buf_size;
1718
1719error:
1720 if (ret) {
1721 kfree(card->mpa_tx.buf);
1722 kfree(card->mpa_rx.buf);
1723 }
1724
1725 return ret;
1726}
1727
1728/*
1729 * This function unregisters the SDIO device.
1730 *
1731 * The SDIO IRQ is released, the function is disabled and driver
1732 * data is set to null.
1733 */
1734static void
1735mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1736{
1737 struct sdio_mmc_card *card = adapter->card;
1738
1739 if (adapter->card) {
5e6e3a92 1740 sdio_claim_host(card->func);
5e6e3a92
BZ
1741 sdio_disable_func(card->func);
1742 sdio_release_host(card->func);
5e6e3a92
BZ
1743 }
1744}
1745
1746/*
1747 * This function registers the SDIO device.
1748 *
1749 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1750 */
1751static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1752{
232fde06 1753 int ret;
5e6e3a92
BZ
1754 struct sdio_mmc_card *card = adapter->card;
1755 struct sdio_func *func = card->func;
1756
1757 /* save adapter pointer in card */
1758 card->adapter = adapter;
828cf222 1759 adapter->tx_buf_size = card->tx_buf_size;
5e6e3a92
BZ
1760
1761 sdio_claim_host(func);
1762
5e6e3a92
BZ
1763 /* Set block size */
1764 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
232fde06 1765 sdio_release_host(func);
5e6e3a92
BZ
1766 if (ret) {
1767 pr_err("cannot set SDIO block size\n");
232fde06 1768 return ret;
5e6e3a92
BZ
1769 }
1770
5e6e3a92
BZ
1771
1772 adapter->dev = &func->dev;
e3bea1c8 1773
05889f82 1774 strcpy(adapter->fw_name, card->firmware);
5e6e3a92
BZ
1775
1776 return 0;
5e6e3a92
BZ
1777}
1778
1779/*
1780 * This function initializes the SDIO driver.
1781 *
1782 * The following initializations steps are followed -
1783 * - Read the Host interrupt status register to acknowledge
1784 * the first interrupt got from bootloader
1785 * - Disable host interrupt mask register
1786 * - Get SDIO port
5e6e3a92
BZ
1787 * - Initialize SDIO variables in card
1788 * - Allocate MP registers
1789 * - Allocate MPA Tx and Rx buffers
1790 */
1791static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1792{
1793 struct sdio_mmc_card *card = adapter->card;
05889f82 1794 const struct mwifiex_sdio_card_reg *reg = card->reg;
5e6e3a92 1795 int ret;
ab93d4ff 1796 u8 sdio_ireg;
5e6e3a92 1797
3c59e328
AK
1798 sdio_set_drvdata(card->func, card);
1799
5e6e3a92
BZ
1800 /*
1801 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1802 * from the bootloader. If we don't do this we get a interrupt
1803 * as soon as we register the irq.
1804 */
1805 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1806
5e6e3a92
BZ
1807 /* Get SDIO ioport */
1808 mwifiex_init_sdio_ioport(adapter);
1809
5e6e3a92
BZ
1810 /* Initialize SDIO variables in card */
1811 card->mp_rd_bitmap = 0;
1812 card->mp_wr_bitmap = 0;
05889f82
AK
1813 card->curr_rd_port = reg->start_rd_port;
1814 card->curr_wr_port = reg->start_wr_port;
5e6e3a92 1815
05889f82 1816 card->mp_data_port_mask = reg->data_port_mask;
5e6e3a92
BZ
1817
1818 card->mpa_tx.buf_len = 0;
1819 card->mpa_tx.pkt_cnt = 0;
1820 card->mpa_tx.start_port = 0;
1821
7d7ab022 1822 card->mpa_tx.enabled = 1;
05889f82 1823 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
5e6e3a92
BZ
1824
1825 card->mpa_rx.buf_len = 0;
1826 card->mpa_rx.pkt_cnt = 0;
1827 card->mpa_rx.start_port = 0;
1828
7d7ab022 1829 card->mpa_rx.enabled = 1;
05889f82 1830 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
5e6e3a92
BZ
1831
1832 /* Allocate buffers for SDIO MP-A */
05889f82 1833 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
0d2e7a5c 1834 if (!card->mp_regs)
b53575ec 1835 return -ENOMEM;
5e6e3a92 1836
c23b7c8f
AK
1837 /* Allocate skb pointer buffers */
1838 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
1839 card->mp_agg_pkt_limit, GFP_KERNEL);
1840 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
1841 card->mp_agg_pkt_limit, GFP_KERNEL);
5e6e3a92
BZ
1842 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1843 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1844 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1845 if (ret) {
1846 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1847 kfree(card->mp_regs);
1848 return -1;
1849 }
1850
1851 return ret;
1852}
1853
1854/*
1855 * This function resets the MPA Tx and Rx buffers.
1856 */
1857static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1858{
1859 struct sdio_mmc_card *card = adapter->card;
1860
1861 MP_TX_AGGR_BUF_RESET(card);
1862 MP_RX_AGGR_BUF_RESET(card);
1863}
1864
1865/*
1866 * This function cleans up the allocated card buffers.
1867 *
1868 * The following are freed by this function -
1869 * - MP registers
1870 * - MPA Tx buffer
1871 * - MPA Rx buffer
1872 */
1873static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1874{
1875 struct sdio_mmc_card *card = adapter->card;
1876
1877 kfree(card->mp_regs);
c23b7c8f
AK
1878 kfree(card->mpa_rx.skb_arr);
1879 kfree(card->mpa_rx.len_arr);
5e6e3a92
BZ
1880 kfree(card->mpa_tx.buf);
1881 kfree(card->mpa_rx.buf);
3c59e328
AK
1882 sdio_set_drvdata(card->func, NULL);
1883 kfree(card);
5e6e3a92
BZ
1884}
1885
1886/*
1887 * This function updates the MP end port in card.
1888 */
1889static void
1890mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1891{
1892 struct sdio_mmc_card *card = adapter->card;
05889f82 1893 const struct mwifiex_sdio_card_reg *reg = card->reg;
5e6e3a92
BZ
1894 int i;
1895
1896 card->mp_end_port = port;
1897
05889f82 1898 card->mp_data_port_mask = reg->data_port_mask;
5e6e3a92 1899
b60186f8
YAP
1900 if (reg->start_wr_port) {
1901 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
1902 card->mp_data_port_mask &=
1903 ~(1 << (card->max_ports - i));
1904 }
5e6e3a92 1905
05889f82 1906 card->curr_wr_port = reg->start_wr_port;
5e6e3a92
BZ
1907
1908 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
5dbd326c 1909 port, card->mp_data_port_mask);
5e6e3a92
BZ
1910}
1911
d31ab357
AK
1912static struct mmc_host *reset_host;
1913static void sdio_card_reset_worker(struct work_struct *work)
1914{
7f5855c9
TH
1915 struct mmc_host *target = reset_host;
1916
d31ab357
AK
1917 /* The actual reset operation must be run outside of driver thread.
1918 * This is because mmc_remove_host() will cause the device to be
1919 * instantly destroyed, and the driver then needs to end its thread,
1920 * leading to a deadlock.
1921 *
1922 * We run it in a totally independent workqueue.
1923 */
1924
1925 pr_err("Resetting card...\n");
7f5855c9 1926 mmc_remove_host(target);
d31ab357
AK
1927 /* 20ms delay is based on experiment with sdhci controller */
1928 mdelay(20);
7f5855c9 1929 mmc_add_host(target);
d31ab357
AK
1930}
1931static DECLARE_WORK(card_reset_work, sdio_card_reset_worker);
1932
1933/* This function resets the card */
1934static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
1935{
1936 struct sdio_mmc_card *card = adapter->card;
1937
d31ab357
AK
1938 reset_host = card->func->card->host;
1939 schedule_work(&card_reset_work);
1940}
1941
5e6e3a92
BZ
1942static struct mwifiex_if_ops sdio_ops = {
1943 .init_if = mwifiex_init_sdio,
1944 .cleanup_if = mwifiex_cleanup_sdio,
1945 .check_fw_status = mwifiex_check_fw_status,
1946 .prog_fw = mwifiex_prog_fw_w_helper,
1947 .register_dev = mwifiex_register_dev,
1948 .unregister_dev = mwifiex_unregister_dev,
1949 .enable_int = mwifiex_sdio_enable_host_int,
232fde06 1950 .disable_int = mwifiex_sdio_disable_host_int,
5e6e3a92
BZ
1951 .process_int_status = mwifiex_process_int_status,
1952 .host_to_card = mwifiex_sdio_host_to_card,
1953 .wakeup = mwifiex_pm_wakeup_card,
1954 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1955
1956 /* SDIO specific */
1957 .update_mp_end_port = mwifiex_update_mp_end_port,
1958 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
d930faee
AK
1959 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1960 .event_complete = mwifiex_sdio_event_complete,
d31ab357 1961 .card_reset = mwifiex_sdio_card_reset,
5e6e3a92
BZ
1962};
1963
1964/*
1965 * This function initializes the SDIO driver.
1966 *
1967 * This initiates the semaphore and registers the device with
1968 * SDIO bus.
1969 */
1970static int
1971mwifiex_sdio_init_module(void)
1972{
5e6e3a92
BZ
1973 sema_init(&add_remove_card_sem, 1);
1974
287546df
AK
1975 /* Clear the flag in case user removes the card. */
1976 user_rmmod = 0;
1977
636c4598 1978 return sdio_register_driver(&mwifiex_sdio);
5e6e3a92
BZ
1979}
1980
1981/*
1982 * This function cleans up the SDIO driver.
1983 *
1984 * The following major steps are followed for cleanup -
1985 * - Resume the device if its suspended
1986 * - Disconnect the device if connected
1987 * - Shutdown the firmware
1988 * - Unregister the device from SDIO bus.
1989 */
1990static void
1991mwifiex_sdio_cleanup_module(void)
1992{
287546df
AK
1993 if (!down_interruptible(&add_remove_card_sem))
1994 up(&add_remove_card_sem);
5e6e3a92 1995
287546df
AK
1996 /* Set the flag as user is removing this module. */
1997 user_rmmod = 1;
5e6e3a92 1998
d31ab357 1999 cancel_work_sync(&card_reset_work);
5e6e3a92
BZ
2000 sdio_unregister_driver(&mwifiex_sdio);
2001}
2002
2003module_init(mwifiex_sdio_init_module);
2004module_exit(mwifiex_sdio_cleanup_module);
2005
2006MODULE_AUTHOR("Marvell International Ltd.");
2007MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2008MODULE_VERSION(SDIO_VERSION);
2009MODULE_LICENSE("GPL v2");
98e6b9df 2010MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
e3bea1c8
BZ
2011MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2012MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
b60186f8 2013MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
This page took 0.375553 seconds and 5 git commands to generate.