mwifiex: minor changes in debug messages
[deliverable/linux.git] / drivers / net / wireless / mwifiex / sdio.c
CommitLineData
5e6e3a92
BZ
1/*
2 * Marvell Wireless LAN device driver: SDIO specific handling
3 *
65da33f5 4 * Copyright (C) 2011-2014, Marvell International Ltd.
5e6e3a92
BZ
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 49static struct mwifiex_if_ops sdio_ops;
2f5872b6 50static unsigned long iface_work_flags;
5e6e3a92
BZ
51
52static struct semaphore add_remove_card_sem;
53
54881c6b
AK
54static struct memory_type_mapping mem_type_mapping_tbl[] = {
55 {"ITCM", NULL, 0, 0xF0},
56 {"DTCM", NULL, 0, 0xF1},
57 {"SQRAM", NULL, 0, 0xF2},
58 {"APU", NULL, 0, 0xF3},
59 {"CIU", NULL, 0, 0xF4},
60 {"ICU", NULL, 0, 0xF5},
61 {"MAC", NULL, 0, 0xF6},
62 {"EXT7", NULL, 0, 0xF7},
63 {"EXT8", NULL, 0, 0xF8},
64 {"EXT9", NULL, 0, 0xF9},
65 {"EXT10", NULL, 0, 0xFA},
66 {"EXT11", NULL, 0, 0xFB},
67 {"EXT12", NULL, 0, 0xFC},
68 {"EXT13", NULL, 0, 0xFD},
69 {"EXTLAST", NULL, 0, 0xFE},
70};
71
5e6e3a92
BZ
72/*
73 * SDIO probe.
74 *
75 * This function probes an mwifiex device and registers it. It allocates
76 * the card structure, enables SDIO function number and initiates the
77 * device registration and initialization procedure by adding a logical
78 * interface.
79 */
80static int
81mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
82{
270e58e8 83 int ret;
5e6e3a92
BZ
84 struct sdio_mmc_card *card = NULL;
85
86 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
5dbd326c 87 func->vendor, func->device, func->class, func->num);
5e6e3a92
BZ
88
89 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
e404decb 90 if (!card)
5e6e3a92 91 return -ENOMEM;
5e6e3a92
BZ
92
93 card->func = func;
94
95 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
96
05889f82
AK
97 if (id->driver_data) {
98 struct mwifiex_sdio_device *data = (void *)id->driver_data;
99
100 card->firmware = data->firmware;
101 card->reg = data->reg;
102 card->max_ports = data->max_ports;
103 card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
b60186f8
YAP
104 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
105 card->has_control_mask = data->has_control_mask;
828cf222 106 card->tx_buf_size = data->tx_buf_size;
e1aa93a4
AK
107 card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
108 card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
b4e8aebb
AP
109 card->can_dump_fw = data->can_dump_fw;
110 card->can_auto_tdls = data->can_auto_tdls;
1fe192d8 111 card->can_ext_scan = data->can_ext_scan;
05889f82
AK
112 }
113
5e6e3a92
BZ
114 sdio_claim_host(func);
115 ret = sdio_enable_func(func);
116 sdio_release_host(func);
117
118 if (ret) {
119 pr_err("%s: failed to enable function\n", __func__);
b53575ec 120 kfree(card);
5e6e3a92
BZ
121 return -EIO;
122 }
123
d930faee
AK
124 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
125 MWIFIEX_SDIO)) {
5e6e3a92
BZ
126 pr_err("%s: add card failed\n", __func__);
127 kfree(card);
128 sdio_claim_host(func);
129 ret = sdio_disable_func(func);
130 sdio_release_host(func);
131 ret = -1;
132 }
133
134 return ret;
135}
136
2cdf359a
AK
137/*
138 * SDIO resume.
139 *
140 * Kernel needs to suspend all functions separately. Therefore all
141 * registered functions must have drivers with suspend and resume
142 * methods. Failing that the kernel simply removes the whole card.
143 *
144 * If already not resumed, this function turns on the traffic and
145 * sends a host sleep cancel request to the firmware.
146 */
147static int mwifiex_sdio_resume(struct device *dev)
148{
149 struct sdio_func *func = dev_to_sdio_func(dev);
150 struct sdio_mmc_card *card;
151 struct mwifiex_adapter *adapter;
152 mmc_pm_flag_t pm_flag = 0;
153
154 if (func) {
155 pm_flag = sdio_get_host_pm_caps(func);
156 card = sdio_get_drvdata(func);
157 if (!card || !card->adapter) {
158 pr_err("resume: invalid card or adapter\n");
159 return 0;
160 }
161 } else {
162 pr_err("resume: sdio_func is not specified\n");
163 return 0;
164 }
165
166 adapter = card->adapter;
167
168 if (!adapter->is_suspended) {
acebe8c1
ZL
169 mwifiex_dbg(adapter, WARN,
170 "device already resumed\n");
2cdf359a
AK
171 return 0;
172 }
173
174 adapter->is_suspended = false;
175
176 /* Disable Host Sleep */
177 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
178 MWIFIEX_ASYNC_CMD);
179
180 return 0;
181}
182
5e6e3a92
BZ
183/*
184 * SDIO remove.
185 *
186 * This function removes the interface and frees up the card structure.
187 */
188static void
189mwifiex_sdio_remove(struct sdio_func *func)
190{
191 struct sdio_mmc_card *card;
287546df 192 struct mwifiex_adapter *adapter;
5dbd326c 193 struct mwifiex_private *priv;
5e6e3a92 194
287546df
AK
195 card = sdio_get_drvdata(func);
196 if (!card)
197 return;
198
199 adapter = card->adapter;
200 if (!adapter || !adapter->priv_num)
201 return;
202
acebe8c1
ZL
203 mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
204
287546df
AK
205 if (user_rmmod) {
206 if (adapter->is_suspended)
207 mwifiex_sdio_resume(adapter->dev);
208
848819f4 209 mwifiex_deauthenticate_all(adapter);
287546df 210
5dbd326c
YAP
211 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
212 mwifiex_disable_auto_ds(priv);
213 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
5e6e3a92 214 }
287546df
AK
215
216 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
5e6e3a92
BZ
217}
218
219/*
220 * SDIO suspend.
221 *
222 * Kernel needs to suspend all functions separately. Therefore all
223 * registered functions must have drivers with suspend and resume
224 * methods. Failing that the kernel simply removes the whole card.
225 *
226 * If already not suspended, this function allocates and sends a host
227 * sleep activate request to the firmware and turns off the traffic.
228 */
229static int mwifiex_sdio_suspend(struct device *dev)
230{
231 struct sdio_func *func = dev_to_sdio_func(dev);
232 struct sdio_mmc_card *card;
270e58e8 233 struct mwifiex_adapter *adapter;
5e6e3a92 234 mmc_pm_flag_t pm_flag = 0;
5e6e3a92
BZ
235 int ret = 0;
236
237 if (func) {
238 pm_flag = sdio_get_host_pm_caps(func);
239 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
5dbd326c 240 sdio_func_id(func), pm_flag);
5e6e3a92
BZ
241 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
242 pr_err("%s: cannot remain alive while host is"
243 " suspended\n", sdio_func_id(func));
244 return -ENOSYS;
245 }
246
247 card = sdio_get_drvdata(func);
248 if (!card || !card->adapter) {
249 pr_err("suspend: invalid card or adapter\n");
250 return 0;
251 }
252 } else {
253 pr_err("suspend: sdio_func is not specified\n");
254 return 0;
255 }
256
257 adapter = card->adapter;
258
259 /* Enable the Host Sleep */
dd321acd 260 if (!mwifiex_enable_hs(adapter)) {
acebe8c1
ZL
261 mwifiex_dbg(adapter, ERROR,
262 "cmd: failed to suspend\n");
c0dbba66 263 adapter->hs_enabling = false;
dd321acd 264 return -EFAULT;
5e6e3a92
BZ
265 }
266
acebe8c1
ZL
267 mwifiex_dbg(adapter, INFO,
268 "cmd: suspend with MMC_PM_KEEP_POWER\n");
dd321acd
BZ
269 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
270
5e6e3a92
BZ
271 /* Indicate device suspended */
272 adapter->is_suspended = true;
c0dbba66 273 adapter->hs_enabling = false;
5e6e3a92 274
5e6e3a92
BZ
275 return ret;
276}
277
98e6b9df
W
278/* Device ID for SD8786 */
279#define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
5e6e3a92
BZ
280/* Device ID for SD8787 */
281#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
e3bea1c8
BZ
282/* Device ID for SD8797 */
283#define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
b60186f8
YAP
284/* Device ID for SD8897 */
285#define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
030bb75a
AP
286/* Device ID for SD8887 */
287#define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
52bd3d20
YAP
288/* Device ID for SD8801 */
289#define SDIO_DEVICE_ID_MARVELL_8801 (0x9139)
290
5e6e3a92
BZ
291
292/* WLAN IDs */
293static const struct sdio_device_id mwifiex_ids[] = {
05889f82
AK
294 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
295 .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
296 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
297 .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
298 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
299 .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
b60186f8
YAP
300 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
301 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
030bb75a
AP
302 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
303 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
52bd3d20
YAP
304 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
305 .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
5e6e3a92
BZ
306 {},
307};
308
309MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
310
311static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
312 .suspend = mwifiex_sdio_suspend,
313 .resume = mwifiex_sdio_resume,
314};
315
316static struct sdio_driver mwifiex_sdio = {
317 .name = "mwifiex_sdio",
318 .id_table = mwifiex_ids,
319 .probe = mwifiex_sdio_probe,
320 .remove = mwifiex_sdio_remove,
321 .drv = {
322 .owner = THIS_MODULE,
323 .pm = &mwifiex_sdio_pm_ops,
324 }
325};
326
232fde06
DD
327/* Write data into SDIO card register. Caller claims SDIO device. */
328static int
329mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
330{
331 int ret = -1;
332 sdio_writeb(func, data, reg, &ret);
333 return ret;
334}
335
5e6e3a92
BZ
336/*
337 * This function writes data into SDIO card register.
338 */
339static int
ab93d4ff 340mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
5e6e3a92
BZ
341{
342 struct sdio_mmc_card *card = adapter->card;
232fde06 343 int ret;
5e6e3a92
BZ
344
345 sdio_claim_host(card->func);
232fde06 346 ret = mwifiex_write_reg_locked(card->func, reg, data);
5e6e3a92
BZ
347 sdio_release_host(card->func);
348
349 return ret;
350}
351
352/*
353 * This function reads data from SDIO card register.
354 */
355static int
ab93d4ff 356mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
5e6e3a92
BZ
357{
358 struct sdio_mmc_card *card = adapter->card;
359 int ret = -1;
360 u8 val;
361
362 sdio_claim_host(card->func);
363 val = sdio_readb(card->func, reg, &ret);
364 sdio_release_host(card->func);
365
366 *data = val;
367
368 return ret;
369}
370
371/*
372 * This function writes multiple data into SDIO card memory.
373 *
374 * This does not work in suspended mode.
375 */
376static int
377mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
572e8f3e 378 u8 *buffer, u32 pkt_len, u32 port)
5e6e3a92
BZ
379{
380 struct sdio_mmc_card *card = adapter->card;
5b2e2ecc 381 int ret;
5e6e3a92
BZ
382 u8 blk_mode =
383 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
384 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
385 u32 blk_cnt =
386 (blk_mode ==
387 BLOCK_MODE) ? (pkt_len /
388 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
389 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
390
391 if (adapter->is_suspended) {
acebe8c1
ZL
392 mwifiex_dbg(adapter, ERROR,
393 "%s: not allowed while suspended\n", __func__);
5e6e3a92
BZ
394 return -1;
395 }
396
397 sdio_claim_host(card->func);
398
5b2e2ecc 399 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
5e6e3a92
BZ
400
401 sdio_release_host(card->func);
402
403 return ret;
404}
405
406/*
407 * This function reads multiple data from SDIO card memory.
408 */
572e8f3e
AK
409static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
410 u32 len, u32 port, u8 claim)
5e6e3a92
BZ
411{
412 struct sdio_mmc_card *card = adapter->card;
5b2e2ecc 413 int ret;
5dbd326c
YAP
414 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
415 : BLOCK_MODE;
5e6e3a92 416 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
5dbd326c
YAP
417 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
418 : len;
5e6e3a92
BZ
419 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
420
421 if (claim)
422 sdio_claim_host(card->func);
423
5b2e2ecc 424 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
5e6e3a92
BZ
425
426 if (claim)
427 sdio_release_host(card->func);
428
429 return ret;
430}
431
432/*
433 * This function wakes up the card.
434 *
435 * A host power up command is written to the card configuration
436 * register to wake up the card.
437 */
438static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
439{
acebe8c1
ZL
440 mwifiex_dbg(adapter, EVENT,
441 "event: wakeup device...\n");
5e6e3a92 442
636c4598 443 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
5e6e3a92
BZ
444}
445
446/*
447 * This function is called after the card has woken up.
448 *
449 * The card configuration register is reset.
450 */
451static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
452{
acebe8c1
ZL
453 mwifiex_dbg(adapter, EVENT,
454 "cmd: wakeup device completed\n");
5e6e3a92 455
636c4598 456 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
5e6e3a92
BZ
457}
458
459/*
b60186f8
YAP
460 * This function is used to initialize IO ports for the
461 * chipsets supporting SDIO new mode eg SD8897.
462 */
463static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
464{
465 u8 reg;
554a0113 466 struct sdio_mmc_card *card = adapter->card;
b60186f8
YAP
467
468 adapter->ioport = MEM_PORT;
469
470 /* enable sdio new mode */
554a0113 471 if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, &reg))
b60186f8 472 return -1;
554a0113 473 if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
b60186f8
YAP
474 reg | CMD53_NEW_MODE))
475 return -1;
476
477 /* Configure cmd port and enable reading rx length from the register */
554a0113 478 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, &reg))
b60186f8 479 return -1;
554a0113
AP
480 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
481 reg | CMD_PORT_RD_LEN_EN))
b60186f8
YAP
482 return -1;
483
484 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
485 * completed
486 */
554a0113 487 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, &reg))
b60186f8 488 return -1;
554a0113
AP
489 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
490 reg | CMD_PORT_AUTO_EN))
b60186f8
YAP
491 return -1;
492
493 return 0;
494}
495
496/* This function initializes the IO ports.
5e6e3a92
BZ
497 *
498 * The following operations are performed -
499 * - Read the IO ports (0, 1 and 2)
500 * - Set host interrupt Reset-To-Read to clear
501 * - Set auto re-enable interrupt
502 */
503static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
504{
ab93d4ff 505 u8 reg;
05889f82 506 struct sdio_mmc_card *card = adapter->card;
5e6e3a92
BZ
507
508 adapter->ioport = 0;
509
b60186f8
YAP
510 if (card->supports_sdio_new_mode) {
511 if (mwifiex_init_sdio_new_mode(adapter))
512 return -1;
513 goto cont;
514 }
515
5e6e3a92 516 /* Read the IO port */
554a0113 517 if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, &reg))
5e6e3a92
BZ
518 adapter->ioport |= (reg & 0xff);
519 else
520 return -1;
521
554a0113 522 if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, &reg))
5e6e3a92
BZ
523 adapter->ioport |= ((reg & 0xff) << 8);
524 else
525 return -1;
526
554a0113 527 if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, &reg))
5e6e3a92
BZ
528 adapter->ioport |= ((reg & 0xff) << 16);
529 else
530 return -1;
b60186f8 531cont:
acebe8c1
ZL
532 mwifiex_dbg(adapter, INFO,
533 "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
5e6e3a92
BZ
534
535 /* Set Host interrupt reset to read to clear */
554a0113
AP
536 if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, &reg))
537 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
05889f82 538 reg | card->reg->sdio_int_mask);
5e6e3a92
BZ
539 else
540 return -1;
541
542 /* Dnld/Upld ready set to auto reset */
05889f82
AK
543 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
544 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
5e6e3a92
BZ
545 reg | AUTO_RE_ENABLE_INT);
546 else
547 return -1;
548
549 return 0;
550}
551
552/*
553 * This function sends data to the card.
554 */
555static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
556 u8 *payload, u32 pkt_len, u32 port)
557{
558 u32 i = 0;
270e58e8 559 int ret;
5e6e3a92
BZ
560
561 do {
572e8f3e 562 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
5e6e3a92
BZ
563 if (ret) {
564 i++;
acebe8c1
ZL
565 mwifiex_dbg(adapter, ERROR,
566 "host_to_card, write iomem\t"
567 "(%d) failed: %d\n", i, ret);
5dbd326c 568 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
acebe8c1
ZL
569 mwifiex_dbg(adapter, ERROR,
570 "write CFG reg failed\n");
5e6e3a92
BZ
571
572 ret = -1;
573 if (i > MAX_WRITE_IOMEM_RETRY)
574 return ret;
575 }
576 } while (ret == -1);
577
578 return ret;
579}
580
581/*
582 * This function gets the read port.
583 *
584 * If control port bit is set in MP read bitmap, the control port
585 * is returned, otherwise the current read port is returned and
586 * the value is increased (provided it does not reach the maximum
587 * limit, in which case it is reset to 1)
588 */
589static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
590{
591 struct sdio_mmc_card *card = adapter->card;
05889f82 592 const struct mwifiex_sdio_card_reg *reg = card->reg;
5ac253d5 593 u32 rd_bitmap = card->mp_rd_bitmap;
5e6e3a92 594
acebe8c1
ZL
595 mwifiex_dbg(adapter, DATA,
596 "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
5e6e3a92 597
b60186f8
YAP
598 if (card->supports_sdio_new_mode) {
599 if (!(rd_bitmap & reg->data_port_mask))
600 return -1;
601 } else {
602 if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
603 return -1;
604 }
5e6e3a92 605
b60186f8
YAP
606 if ((card->has_control_mask) &&
607 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
5ac253d5 608 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
5e6e3a92 609 *port = CTRL_PORT;
acebe8c1
ZL
610 mwifiex_dbg(adapter, DATA,
611 "data: port=%d mp_rd_bitmap=0x%08x\n",
612 *port, card->mp_rd_bitmap);
e6a52030 613 return 0;
5e6e3a92 614 }
e6a52030
AK
615
616 if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
617 return -1;
618
619 /* We are now handling the SDIO data ports */
620 card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
621 *port = card->curr_rd_port;
622
623 if (++card->curr_rd_port == card->max_ports)
624 card->curr_rd_port = reg->start_rd_port;
625
acebe8c1
ZL
626 mwifiex_dbg(adapter, DATA,
627 "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
628 *port, rd_bitmap, card->mp_rd_bitmap);
e6a52030 629
5e6e3a92
BZ
630 return 0;
631}
632
633/*
634 * This function gets the write port for data.
635 *
636 * The current write port is returned if available and the value is
637 * increased (provided it does not reach the maximum limit, in which
638 * case it is reset to 1)
639 */
0fc0d43b 640static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
5e6e3a92
BZ
641{
642 struct sdio_mmc_card *card = adapter->card;
b60186f8 643 const struct mwifiex_sdio_card_reg *reg = card->reg;
5ac253d5 644 u32 wr_bitmap = card->mp_wr_bitmap;
5e6e3a92 645
acebe8c1
ZL
646 mwifiex_dbg(adapter, DATA,
647 "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
5e6e3a92 648
c48ba040 649 if (!(wr_bitmap & card->mp_data_port_mask)) {
b60186f8
YAP
650 adapter->data_sent = true;
651 return -EBUSY;
b60186f8 652 }
5e6e3a92
BZ
653
654 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
5ac253d5 655 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
5e6e3a92 656 *port = card->curr_wr_port;
c48ba040 657 if (++card->curr_wr_port == card->mp_end_port)
b60186f8 658 card->curr_wr_port = reg->start_wr_port;
5e6e3a92
BZ
659 } else {
660 adapter->data_sent = true;
661 return -EBUSY;
662 }
663
b60186f8 664 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
acebe8c1
ZL
665 mwifiex_dbg(adapter, ERROR,
666 "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
667 *port, card->curr_wr_port, wr_bitmap,
668 card->mp_wr_bitmap);
5e6e3a92
BZ
669 return -1;
670 }
671
acebe8c1
ZL
672 mwifiex_dbg(adapter, DATA,
673 "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
674 *port, wr_bitmap, card->mp_wr_bitmap);
5e6e3a92
BZ
675
676 return 0;
677}
678
679/*
680 * This function polls the card status.
681 */
682static int
683mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
684{
05889f82 685 struct sdio_mmc_card *card = adapter->card;
5e6e3a92 686 u32 tries;
ab93d4ff 687 u8 cs;
5e6e3a92
BZ
688
689 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
05889f82 690 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
5e6e3a92
BZ
691 break;
692 else if ((cs & bits) == bits)
693 return 0;
694
e7891ba2 695 usleep_range(10, 20);
5e6e3a92
BZ
696 }
697
acebe8c1
ZL
698 mwifiex_dbg(adapter, ERROR,
699 "poll card status failed, tries = %d\n", tries);
5dbd326c 700
5e6e3a92
BZ
701 return -1;
702}
703
704/*
705 * This function reads the firmware status.
706 */
707static int
708mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
709{
05889f82
AK
710 struct sdio_mmc_card *card = adapter->card;
711 const struct mwifiex_sdio_card_reg *reg = card->reg;
ab93d4ff 712 u8 fws0, fws1;
5e6e3a92 713
05889f82 714 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
5e6e3a92
BZ
715 return -1;
716
05889f82 717 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
5e6e3a92
BZ
718 return -1;
719
720 *dat = (u16) ((fws1 << 8) | fws0);
721
722 return 0;
723}
724
725/*
726 * This function disables the host interrupt.
727 *
728 * The host interrupt mask is read, the disable bit is reset and
729 * written back to the card host interrupt mask register.
730 */
232fde06 731static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
5e6e3a92 732{
232fde06
DD
733 struct sdio_mmc_card *card = adapter->card;
734 struct sdio_func *func = card->func;
5e6e3a92 735
232fde06 736 sdio_claim_host(func);
554a0113 737 mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
232fde06
DD
738 sdio_release_irq(func);
739 sdio_release_host(func);
5e6e3a92
BZ
740}
741
2cdf359a
AK
742/*
743 * This function reads the interrupt status from card.
744 */
745static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
746{
747 struct sdio_mmc_card *card = adapter->card;
748 u8 sdio_ireg;
749 unsigned long flags;
750
751 if (mwifiex_read_data_sync(adapter, card->mp_regs,
752 card->reg->max_mp_regs,
753 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
acebe8c1 754 mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
2cdf359a
AK
755 return;
756 }
757
554a0113 758 sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
2cdf359a
AK
759 if (sdio_ireg) {
760 /*
761 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
762 * For SDIO new mode CMD port interrupts
763 * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
764 * UP_LD_CMD_PORT_HOST_INT_STATUS
765 * Clear the interrupt status register
766 */
acebe8c1
ZL
767 mwifiex_dbg(adapter, INTR,
768 "int: sdio_ireg = %#x\n", sdio_ireg);
2cdf359a
AK
769 spin_lock_irqsave(&adapter->int_lock, flags);
770 adapter->int_status |= sdio_ireg;
771 spin_unlock_irqrestore(&adapter->int_lock, flags);
772 }
773}
774
775/*
776 * SDIO interrupt handler.
777 *
778 * This function reads the interrupt status from firmware and handles
779 * the interrupt in current thread (ksdioirqd) right away.
780 */
781static void
782mwifiex_sdio_interrupt(struct sdio_func *func)
783{
784 struct mwifiex_adapter *adapter;
785 struct sdio_mmc_card *card;
786
787 card = sdio_get_drvdata(func);
788 if (!card || !card->adapter) {
789 pr_debug("int: func=%p card=%p adapter=%p\n",
790 func, card, card ? card->adapter : NULL);
791 return;
792 }
793 adapter = card->adapter;
794
795 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
796 adapter->ps_state = PS_STATE_AWAKE;
797
798 mwifiex_interrupt_status(adapter);
799 mwifiex_main_process(adapter);
800}
801
5e6e3a92
BZ
802/*
803 * This function enables the host interrupt.
804 *
805 * The host interrupt enable mask is written to the card
806 * host interrupt mask register.
807 */
808static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
809{
05889f82 810 struct sdio_mmc_card *card = adapter->card;
232fde06
DD
811 struct sdio_func *func = card->func;
812 int ret;
813
814 sdio_claim_host(func);
815
816 /* Request the SDIO IRQ */
817 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
818 if (ret) {
acebe8c1
ZL
819 mwifiex_dbg(adapter, ERROR,
820 "claim irq failed: ret=%d\n", ret);
232fde06
DD
821 goto out;
822 }
05889f82 823
5e6e3a92 824 /* Simply write the mask to the register */
554a0113 825 ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
232fde06
DD
826 card->reg->host_int_enable);
827 if (ret) {
acebe8c1
ZL
828 mwifiex_dbg(adapter, ERROR,
829 "enable host interrupt failed\n");
232fde06 830 sdio_release_irq(func);
5e6e3a92 831 }
232fde06
DD
832
833out:
834 sdio_release_host(func);
835 return ret;
5e6e3a92
BZ
836}
837
838/*
839 * This function sends a data buffer to the card.
840 */
841static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
842 u32 *type, u8 *buffer,
843 u32 npayload, u32 ioport)
844{
270e58e8 845 int ret;
5e6e3a92
BZ
846 u32 nb;
847
848 if (!buffer) {
acebe8c1
ZL
849 mwifiex_dbg(adapter, ERROR,
850 "%s: buffer is NULL\n", __func__);
5e6e3a92
BZ
851 return -1;
852 }
853
572e8f3e 854 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
5e6e3a92
BZ
855
856 if (ret) {
acebe8c1
ZL
857 mwifiex_dbg(adapter, ERROR,
858 "%s: read iomem failed: %d\n", __func__,
5dbd326c 859 ret);
5e6e3a92
BZ
860 return -1;
861 }
862
863 nb = le16_to_cpu(*(__le16 *) (buffer));
864 if (nb > npayload) {
acebe8c1
ZL
865 mwifiex_dbg(adapter, ERROR,
866 "%s: invalid packet, nb=%d npayload=%d\n",
867 __func__, nb, npayload);
5e6e3a92
BZ
868 return -1;
869 }
870
871 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
872
873 return ret;
874}
875
876/*
877 * This function downloads the firmware to the card.
878 *
879 * Firmware is downloaded to the card in blocks. Every block download
880 * is tested for CRC errors, and retried a number of times before
881 * returning failure.
882 */
883static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
884 struct mwifiex_fw_image *fw)
885{
05889f82
AK
886 struct sdio_mmc_card *card = adapter->card;
887 const struct mwifiex_sdio_card_reg *reg = card->reg;
270e58e8 888 int ret;
5e6e3a92
BZ
889 u8 *firmware = fw->fw_buf;
890 u32 firmware_len = fw->fw_len;
891 u32 offset = 0;
ab93d4ff 892 u8 base0, base1;
5e6e3a92
BZ
893 u8 *fwbuf;
894 u16 len = 0;
270e58e8 895 u32 txlen, tx_blocks = 0, tries;
5e6e3a92
BZ
896 u32 i = 0;
897
898 if (!firmware_len) {
acebe8c1
ZL
899 mwifiex_dbg(adapter, ERROR,
900 "firmware image not found! Terminating download\n");
5e6e3a92
BZ
901 return -1;
902 }
903
acebe8c1
ZL
904 mwifiex_dbg(adapter, INFO,
905 "info: downloading FW image (%d bytes)\n",
906 firmware_len);
5e6e3a92
BZ
907
908 /* Assume that the allocated buffer is 8-byte aligned */
909 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
0d2e7a5c 910 if (!fwbuf)
b53575ec 911 return -ENOMEM;
5e6e3a92
BZ
912
913 /* Perform firmware data transfer */
914 do {
915 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
916 bits */
917 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
918 DN_LD_CARD_RDY);
919 if (ret) {
acebe8c1
ZL
920 mwifiex_dbg(adapter, ERROR,
921 "FW download with helper:\t"
922 "poll status timeout @ %d\n", offset);
5e6e3a92
BZ
923 goto done;
924 }
925
926 /* More data? */
927 if (offset >= firmware_len)
928 break;
929
930 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
05889f82 931 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
5e6e3a92
BZ
932 &base0);
933 if (ret) {
acebe8c1
ZL
934 mwifiex_dbg(adapter, ERROR,
935 "dev BASE0 register read failed:\t"
936 "base0=%#04X(%d). Terminating dnld\n",
937 base0, base0);
5e6e3a92
BZ
938 goto done;
939 }
05889f82 940 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
5e6e3a92
BZ
941 &base1);
942 if (ret) {
acebe8c1
ZL
943 mwifiex_dbg(adapter, ERROR,
944 "dev BASE1 register read failed:\t"
945 "base1=%#04X(%d). Terminating dnld\n",
946 base1, base1);
5e6e3a92
BZ
947 goto done;
948 }
949 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
950
951 if (len)
952 break;
953
e7891ba2 954 usleep_range(10, 20);
5e6e3a92
BZ
955 }
956
957 if (!len) {
958 break;
959 } else if (len > MWIFIEX_UPLD_SIZE) {
acebe8c1
ZL
960 mwifiex_dbg(adapter, ERROR,
961 "FW dnld failed @ %d, invalid length %d\n",
962 offset, len);
5e6e3a92
BZ
963 ret = -1;
964 goto done;
965 }
966
967 txlen = len;
968
969 if (len & BIT(0)) {
970 i++;
971 if (i > MAX_WRITE_IOMEM_RETRY) {
acebe8c1
ZL
972 mwifiex_dbg(adapter, ERROR,
973 "FW dnld failed @ %d, over max retry\n",
974 offset);
5e6e3a92
BZ
975 ret = -1;
976 goto done;
977 }
acebe8c1
ZL
978 mwifiex_dbg(adapter, ERROR,
979 "CRC indicated by the helper:\t"
980 "len = 0x%04X, txlen = %d\n", len, txlen);
5e6e3a92
BZ
981 len &= ~BIT(0);
982 /* Setting this to 0 to resend from same offset */
983 txlen = 0;
984 } else {
985 i = 0;
986
987 /* Set blocksize to transfer - checking for last
988 block */
989 if (firmware_len - offset < txlen)
990 txlen = firmware_len - offset;
991
5dbd326c
YAP
992 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
993 / MWIFIEX_SDIO_BLOCK_SIZE;
5e6e3a92
BZ
994
995 /* Copy payload to buffer */
996 memmove(fwbuf, &firmware[offset], txlen);
997 }
998
999 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
1000 MWIFIEX_SDIO_BLOCK_SIZE,
572e8f3e 1001 adapter->ioport);
5e6e3a92 1002 if (ret) {
acebe8c1
ZL
1003 mwifiex_dbg(adapter, ERROR,
1004 "FW download, write iomem (%d) failed @ %d\n",
1005 i, offset);
5e6e3a92 1006 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
acebe8c1
ZL
1007 mwifiex_dbg(adapter, ERROR,
1008 "write CFG reg failed\n");
5e6e3a92
BZ
1009
1010 ret = -1;
1011 goto done;
1012 }
1013
1014 offset += txlen;
1015 } while (true);
1016
acebe8c1
ZL
1017 mwifiex_dbg(adapter, MSG,
1018 "info: FW download over, size %d bytes\n", offset);
5e6e3a92
BZ
1019
1020 ret = 0;
1021done:
1022 kfree(fwbuf);
1023 return ret;
1024}
1025
1026/*
1027 * This function checks the firmware status in card.
1028 *
1029 * The winner interface is also determined by this function.
1030 */
1031static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
d930faee 1032 u32 poll_num)
5e6e3a92 1033{
05889f82 1034 struct sdio_mmc_card *card = adapter->card;
5e6e3a92
BZ
1035 int ret = 0;
1036 u16 firmware_stat;
1037 u32 tries;
ab93d4ff 1038 u8 winner_status;
5e6e3a92
BZ
1039
1040 /* Wait for firmware initialization event */
1041 for (tries = 0; tries < poll_num; tries++) {
1042 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
1043 if (ret)
1044 continue;
d930faee 1045 if (firmware_stat == FIRMWARE_READY_SDIO) {
5e6e3a92
BZ
1046 ret = 0;
1047 break;
1048 } else {
a76b20e5 1049 msleep(100);
5e6e3a92
BZ
1050 ret = -1;
1051 }
1052 }
1053
d930faee 1054 if (ret) {
5e6e3a92 1055 if (mwifiex_read_reg
05889f82 1056 (adapter, card->reg->status_reg_0, &winner_status))
5e6e3a92
BZ
1057 winner_status = 0;
1058
1059 if (winner_status)
d930faee 1060 adapter->winner = 0;
5e6e3a92 1061 else
d930faee 1062 adapter->winner = 1;
5e6e3a92
BZ
1063 }
1064 return ret;
1065}
1066
92263a84
ZL
1067/*
1068 * This function decode sdio aggreation pkt.
1069 *
1070 * Based on the the data block size and pkt_len,
1071 * skb data will be decoded to few packets.
1072 */
1073static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
1074 struct sk_buff *skb)
1075{
1076 u32 total_pkt_len, pkt_len;
1077 struct sk_buff *skb_deaggr;
1078 u32 pkt_type;
1079 u16 blk_size;
1080 u8 blk_num;
1081 u8 *data;
1082
1083 data = skb->data;
1084 total_pkt_len = skb->len;
1085
1086 while (total_pkt_len >= (SDIO_HEADER_OFFSET + INTF_HEADER_LEN)) {
1087 if (total_pkt_len < adapter->sdio_rx_block_size)
1088 break;
1089 blk_num = *(data + BLOCK_NUMBER_OFFSET);
1090 blk_size = adapter->sdio_rx_block_size * blk_num;
1091 if (blk_size > total_pkt_len) {
acebe8c1
ZL
1092 mwifiex_dbg(adapter, ERROR,
1093 "%s: error in blk_size,\t"
1094 "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
1095 __func__, blk_num, blk_size, total_pkt_len);
92263a84
ZL
1096 break;
1097 }
1098 pkt_len = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET));
1099 pkt_type = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET +
1100 2));
1101 if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
acebe8c1
ZL
1102 mwifiex_dbg(adapter, ERROR,
1103 "%s: error in pkt_len,\t"
1104 "pkt_len=%d, blk_size=%d\n",
1105 __func__, pkt_len, blk_size);
92263a84
ZL
1106 break;
1107 }
1108 skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len,
1109 GFP_KERNEL | GFP_DMA);
1110 if (!skb_deaggr)
1111 break;
1112 skb_put(skb_deaggr, pkt_len);
1113 memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
1114 skb_pull(skb_deaggr, INTF_HEADER_LEN);
1115
1116 mwifiex_handle_rx_packet(adapter, skb_deaggr);
1117 data += blk_size;
1118 total_pkt_len -= blk_size;
1119 }
1120}
1121
5e6e3a92
BZ
1122/*
1123 * This function decodes a received packet.
1124 *
1125 * Based on the type, the packet is treated as either a data, or
1126 * a command response, or an event, and the correct handler
1127 * function is invoked.
1128 */
1129static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1130 struct sk_buff *skb, u32 upld_typ)
1131{
1132 u8 *cmd_buf;
d03b4aa7
AP
1133 __le16 *curr_ptr = (__le16 *)skb->data;
1134 u16 pkt_len = le16_to_cpu(*curr_ptr);
92263a84 1135 struct mwifiex_rxinfo *rx_info;
5e6e3a92 1136
92263a84
ZL
1137 if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1138 skb_trim(skb, pkt_len);
1139 skb_pull(skb, INTF_HEADER_LEN);
1140 }
5e6e3a92
BZ
1141
1142 switch (upld_typ) {
92263a84 1143 case MWIFIEX_TYPE_AGGR_DATA:
acebe8c1
ZL
1144 mwifiex_dbg(adapter, INFO,
1145 "info: --- Rx: Aggr Data packet ---\n");
92263a84
ZL
1146 rx_info = MWIFIEX_SKB_RXCB(skb);
1147 rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
1148 if (adapter->rx_work_enabled) {
1149 skb_queue_tail(&adapter->rx_data_q, skb);
1150 atomic_inc(&adapter->rx_pending);
1151 adapter->data_received = true;
1152 } else {
1153 mwifiex_deaggr_sdio_pkt(adapter, skb);
1154 dev_kfree_skb_any(skb);
1155 }
1156 break;
1157
5e6e3a92 1158 case MWIFIEX_TYPE_DATA:
acebe8c1
ZL
1159 mwifiex_dbg(adapter, DATA,
1160 "info: --- Rx: Data packet ---\n");
6e251174 1161 if (adapter->rx_work_enabled) {
6e251174 1162 skb_queue_tail(&adapter->rx_data_q, skb);
6e251174
AP
1163 adapter->data_received = true;
1164 atomic_inc(&adapter->rx_pending);
1165 } else {
1166 mwifiex_handle_rx_packet(adapter, skb);
1167 }
5e6e3a92
BZ
1168 break;
1169
1170 case MWIFIEX_TYPE_CMD:
acebe8c1
ZL
1171 mwifiex_dbg(adapter, CMD,
1172 "info: --- Rx: Cmd Response ---\n");
5e6e3a92
BZ
1173 /* take care of curr_cmd = NULL case */
1174 if (!adapter->curr_cmd) {
1175 cmd_buf = adapter->upld_buf;
1176
1177 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
1178 mwifiex_process_sleep_confirm_resp(adapter,
5dbd326c
YAP
1179 skb->data,
1180 skb->len);
5e6e3a92 1181
5dbd326c
YAP
1182 memcpy(cmd_buf, skb->data,
1183 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1184 skb->len));
5e6e3a92
BZ
1185
1186 dev_kfree_skb_any(skb);
1187 } else {
1188 adapter->cmd_resp_received = true;
1189 adapter->curr_cmd->resp_skb = skb;
1190 }
1191 break;
1192
1193 case MWIFIEX_TYPE_EVENT:
acebe8c1
ZL
1194 mwifiex_dbg(adapter, EVENT,
1195 "info: --- Rx: Event ---\n");
cfcd926e 1196 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
5e6e3a92 1197
5e6e3a92 1198 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
e80c81dc
AK
1199 memcpy(adapter->event_body,
1200 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1201 skb->len);
5e6e3a92
BZ
1202
1203 /* event cause has been saved to adapter->event_cause */
1204 adapter->event_received = true;
1205 adapter->event_skb = skb;
1206
1207 break;
1208
1209 default:
acebe8c1
ZL
1210 mwifiex_dbg(adapter, ERROR,
1211 "unknown upload type %#x\n", upld_typ);
5e6e3a92
BZ
1212 dev_kfree_skb_any(skb);
1213 break;
1214 }
1215
1216 return 0;
1217}
1218
1219/*
1220 * This function transfers received packets from card to driver, performing
1221 * aggregation if required.
1222 *
1223 * For data received on control port, or if aggregation is disabled, the
1224 * received buffers are uploaded as separate packets. However, if aggregation
1225 * is enabled and required, the buffers are copied onto an aggregation buffer,
1226 * provided there is space left, processed and finally uploaded.
1227 */
1228static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
960d6d08 1229 u16 rx_len, u8 port)
5e6e3a92
BZ
1230{
1231 struct sdio_mmc_card *card = adapter->card;
1232 s32 f_do_rx_aggr = 0;
1233 s32 f_do_rx_cur = 0;
1234 s32 f_aggr_cur = 0;
2c11ab90 1235 s32 f_post_aggr_cur = 0;
5e6e3a92 1236 struct sk_buff *skb_deaggr;
960d6d08
ZL
1237 struct sk_buff *skb = NULL;
1238 u32 pkt_len, pkt_type, mport, pind;
5e6e3a92 1239 u8 *curr_ptr;
5e6e3a92 1240
b60186f8 1241 if ((card->has_control_mask) && (port == CTRL_PORT)) {
5e6e3a92 1242 /* Read the command Resp without aggr */
acebe8c1
ZL
1243 mwifiex_dbg(adapter, CMD,
1244 "info: %s: no aggregation for cmd\t"
1245 "response\n", __func__);
5e6e3a92
BZ
1246
1247 f_do_rx_cur = 1;
1248 goto rx_curr_single;
1249 }
1250
1251 if (!card->mpa_rx.enabled) {
acebe8c1
ZL
1252 mwifiex_dbg(adapter, WARN,
1253 "info: %s: rx aggregation disabled\n",
1254 __func__);
5e6e3a92
BZ
1255
1256 f_do_rx_cur = 1;
1257 goto rx_curr_single;
1258 }
1259
b60186f8
YAP
1260 if ((!card->has_control_mask && (card->mp_rd_bitmap &
1261 card->reg->data_port_mask)) ||
1262 (card->has_control_mask && (card->mp_rd_bitmap &
1263 (~((u32) CTRL_PORT_MASK))))) {
5e6e3a92 1264 /* Some more data RX pending */
acebe8c1
ZL
1265 mwifiex_dbg(adapter, INFO,
1266 "info: %s: not last packet\n", __func__);
5e6e3a92
BZ
1267
1268 if (MP_RX_AGGR_IN_PROGRESS(card)) {
960d6d08 1269 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
5e6e3a92
BZ
1270 f_aggr_cur = 1;
1271 } else {
1272 /* No room in Aggr buf, do rx aggr now */
1273 f_do_rx_aggr = 1;
2c11ab90 1274 f_post_aggr_cur = 1;
5e6e3a92
BZ
1275 }
1276 } else {
1277 /* Rx aggr not in progress */
1278 f_aggr_cur = 1;
1279 }
1280
1281 } else {
1282 /* No more data RX pending */
acebe8c1
ZL
1283 mwifiex_dbg(adapter, INFO,
1284 "info: %s: last packet\n", __func__);
5e6e3a92
BZ
1285
1286 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1287 f_do_rx_aggr = 1;
960d6d08 1288 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
5e6e3a92
BZ
1289 f_aggr_cur = 1;
1290 else
1291 /* No room in Aggr buf, do rx aggr now */
1292 f_do_rx_cur = 1;
1293 } else {
1294 f_do_rx_cur = 1;
1295 }
1296 }
1297
1298 if (f_aggr_cur) {
acebe8c1
ZL
1299 mwifiex_dbg(adapter, INFO,
1300 "info: current packet aggregation\n");
5e6e3a92 1301 /* Curr pkt can be aggregated */
960d6d08 1302 mp_rx_aggr_setup(card, rx_len, port);
5e6e3a92
BZ
1303
1304 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
c23b7c8f 1305 mp_rx_aggr_port_limit_reached(card)) {
acebe8c1
ZL
1306 mwifiex_dbg(adapter, INFO,
1307 "info: %s: aggregated packet\t"
1308 "limit reached\n", __func__);
5e6e3a92
BZ
1309 /* No more pkts allowed in Aggr buf, rx it */
1310 f_do_rx_aggr = 1;
1311 }
1312 }
1313
1314 if (f_do_rx_aggr) {
1315 /* do aggr RX now */
acebe8c1
ZL
1316 mwifiex_dbg(adapter, DATA,
1317 "info: do_rx_aggr: num of packets: %d\n",
1318 card->mpa_rx.pkt_cnt);
5e6e3a92 1319
b60186f8
YAP
1320 if (card->supports_sdio_new_mode) {
1321 int i;
1322 u32 port_count;
1323
1324 for (i = 0, port_count = 0; i < card->max_ports; i++)
1325 if (card->mpa_rx.ports & BIT(i))
1326 port_count++;
1327
1328 /* Reading data from "start_port + 0" to "start_port +
1329 * port_count -1", so decrease the count by 1
1330 */
1331 port_count--;
1332 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1333 (port_count << 8)) + card->mpa_rx.start_port;
1334 } else {
1335 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1336 (card->mpa_rx.ports << 4)) +
1337 card->mpa_rx.start_port;
1338 }
0fc0d43b 1339
5e6e3a92 1340 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
0fc0d43b 1341 card->mpa_rx.buf_len, mport, 1))
17a60b48 1342 goto error;
5e6e3a92
BZ
1343
1344 curr_ptr = card->mpa_rx.buf;
1345
1346 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
960d6d08 1347 u32 *len_arr = card->mpa_rx.len_arr;
5e6e3a92
BZ
1348
1349 /* get curr PKT len & type */
cfcd926e
TW
1350 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1351 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
5e6e3a92
BZ
1352
1353 /* copy pkt to deaggr buf */
960d6d08
ZL
1354 skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1355 GFP_KERNEL |
1356 GFP_DMA);
4acaf048 1357 if (!skb_deaggr) {
acebe8c1
ZL
1358 mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1359 "drop pkt len=%d type=%d\n",
1360 pkt_len, pkt_type);
4acaf048
ZL
1361 curr_ptr += len_arr[pind];
1362 continue;
1363 }
1364
960d6d08 1365 skb_put(skb_deaggr, len_arr[pind]);
5e6e3a92 1366
92263a84
ZL
1367 if ((pkt_type == MWIFIEX_TYPE_DATA ||
1368 (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1369 adapter->sdio_rx_aggr_enable)) &&
960d6d08 1370 (pkt_len <= len_arr[pind])) {
5e6e3a92
BZ
1371
1372 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1373
1374 skb_trim(skb_deaggr, pkt_len);
1375
1376 /* Process de-aggr packet */
1377 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1378 pkt_type);
1379 } else {
acebe8c1
ZL
1380 mwifiex_dbg(adapter, ERROR,
1381 "drop wrong aggr pkt:\t"
1382 "sdio_single_port_rx_aggr=%d\t"
1383 "type=%d len=%d max_len=%d\n",
1384 adapter->sdio_rx_aggr_enable,
1385 pkt_type, pkt_len, len_arr[pind]);
5e6e3a92
BZ
1386 dev_kfree_skb_any(skb_deaggr);
1387 }
960d6d08 1388 curr_ptr += len_arr[pind];
5e6e3a92
BZ
1389 }
1390 MP_RX_AGGR_BUF_RESET(card);
1391 }
1392
1393rx_curr_single:
1394 if (f_do_rx_cur) {
acebe8c1
ZL
1395 mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1396 port, rx_len);
4acaf048 1397
960d6d08 1398 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
4acaf048 1399 if (!skb) {
acebe8c1
ZL
1400 mwifiex_dbg(adapter, ERROR,
1401 "single skb allocated fail,\t"
1402 "drop pkt port=%d len=%d\n", port, rx_len);
4acaf048
ZL
1403 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1404 card->mpa_rx.buf, rx_len,
1405 adapter->ioport + port))
1406 goto error;
1407 return 0;
1408 }
1409
960d6d08 1410 skb_put(skb, rx_len);
5e6e3a92
BZ
1411
1412 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1413 skb->data, skb->len,
1414 adapter->ioport + port))
17a60b48 1415 goto error;
92263a84
ZL
1416 if (!adapter->sdio_rx_aggr_enable &&
1417 pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
acebe8c1
ZL
1418 mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1419 "current SDIO RX Aggr not enabled\n",
1420 pkt_type);
4acaf048
ZL
1421 dev_kfree_skb_any(skb);
1422 return 0;
92263a84 1423 }
5e6e3a92
BZ
1424
1425 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1426 }
2c11ab90 1427 if (f_post_aggr_cur) {
acebe8c1
ZL
1428 mwifiex_dbg(adapter, INFO,
1429 "info: current packet aggregation\n");
2c11ab90 1430 /* Curr pkt can be aggregated */
080465f2 1431 mp_rx_aggr_setup(card, rx_len, port);
2c11ab90 1432 }
5e6e3a92
BZ
1433
1434 return 0;
17a60b48 1435error:
4acaf048 1436 if (MP_RX_AGGR_IN_PROGRESS(card))
17a60b48 1437 MP_RX_AGGR_BUF_RESET(card);
17a60b48 1438
960d6d08 1439 if (f_do_rx_cur && skb)
17a60b48
AP
1440 /* Single transfer pending. Free curr buff also */
1441 dev_kfree_skb_any(skb);
1442
1443 return -1;
5e6e3a92
BZ
1444}
1445
1446/*
1447 * This function checks the current interrupt status.
1448 *
1449 * The following interrupts are checked and handled by this function -
1450 * - Data sent
1451 * - Command sent
1452 * - Packets received
1453 *
1454 * Since the firmware does not generate download ready interrupt if the
1455 * port updated is command port only, command sent interrupt checking
1456 * should be done manually, and for every SDIO interrupt.
1457 *
1458 * In case of Rx packets received, the packets are uploaded from card to
1459 * host and processed accordingly.
1460 */
1461static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1462{
1463 struct sdio_mmc_card *card = adapter->card;
05889f82 1464 const struct mwifiex_sdio_card_reg *reg = card->reg;
5e6e3a92
BZ
1465 int ret = 0;
1466 u8 sdio_ireg;
270e58e8 1467 struct sk_buff *skb;
5e6e3a92
BZ
1468 u8 port = CTRL_PORT;
1469 u32 len_reg_l, len_reg_u;
1470 u32 rx_blocks;
1471 u16 rx_len;
1472 unsigned long flags;
b60186f8
YAP
1473 u32 bitmap;
1474 u8 cr;
5e6e3a92
BZ
1475
1476 spin_lock_irqsave(&adapter->int_lock, flags);
1477 sdio_ireg = adapter->int_status;
1478 adapter->int_status = 0;
1479 spin_unlock_irqrestore(&adapter->int_lock, flags);
1480
1481 if (!sdio_ireg)
1482 return ret;
1483
b60186f8
YAP
1484 /* Following interrupt is only for SDIO new mode */
1485 if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
1486 adapter->cmd_sent = false;
1487
1488 /* Following interrupt is only for SDIO new mode */
1489 if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
1490 u32 pkt_type;
1491
1492 /* read the len of control packet */
554a0113
AP
1493 rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1494 rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
b60186f8
YAP
1495 rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
1496 if (rx_len <= INTF_HEADER_LEN ||
1497 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1498 MWIFIEX_RX_DATA_BUF_SIZE)
1499 return -1;
1500 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
acebe8c1 1501 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
b60186f8 1502
62159944 1503 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
b60186f8
YAP
1504 if (!skb)
1505 return -1;
1506
1507 skb_put(skb, rx_len);
1508
1509 if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
1510 skb->len, adapter->ioport |
1511 CMD_PORT_SLCT)) {
acebe8c1
ZL
1512 mwifiex_dbg(adapter, ERROR,
1513 "%s: failed to card_to_host", __func__);
b60186f8
YAP
1514 dev_kfree_skb_any(skb);
1515 goto term_cmd;
1516 }
1517
1518 if ((pkt_type != MWIFIEX_TYPE_CMD) &&
1519 (pkt_type != MWIFIEX_TYPE_EVENT))
acebe8c1
ZL
1520 mwifiex_dbg(adapter, ERROR,
1521 "%s:Received wrong packet on cmd port",
1522 __func__);
b60186f8
YAP
1523
1524 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1525 }
1526
5e6e3a92 1527 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
b60186f8
YAP
1528 bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
1529 bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
1530 if (card->supports_sdio_new_mode) {
1531 bitmap |=
1532 ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
1533 bitmap |=
1534 ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
1535 }
1536 card->mp_wr_bitmap = bitmap;
1537
acebe8c1
ZL
1538 mwifiex_dbg(adapter, INTR,
1539 "int: DNLD: wr_bitmap=0x%x\n",
1540 card->mp_wr_bitmap);
5e6e3a92
BZ
1541 if (adapter->data_sent &&
1542 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
acebe8c1
ZL
1543 mwifiex_dbg(adapter, INTR,
1544 "info: <--- Tx DONE Interrupt --->\n");
5e6e3a92
BZ
1545 adapter->data_sent = false;
1546 }
1547 }
1548
1549 /* As firmware will not generate download ready interrupt if the port
1550 updated is command port only, cmd_sent should be done for any SDIO
1551 interrupt. */
b60186f8 1552 if (card->has_control_mask && adapter->cmd_sent) {
5e6e3a92
BZ
1553 /* Check if firmware has attach buffer at command port and
1554 update just that in wr_bit_map. */
1555 card->mp_wr_bitmap |=
05889f82 1556 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
5e6e3a92
BZ
1557 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1558 adapter->cmd_sent = false;
1559 }
1560
acebe8c1
ZL
1561 mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
1562 adapter->cmd_sent, adapter->data_sent);
5e6e3a92 1563 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
b60186f8
YAP
1564 bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
1565 bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
1566 if (card->supports_sdio_new_mode) {
1567 bitmap |=
1568 ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
1569 bitmap |=
1570 ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
1571 }
1572 card->mp_rd_bitmap = bitmap;
acebe8c1
ZL
1573 mwifiex_dbg(adapter, INTR,
1574 "int: UPLD: rd_bitmap=0x%x\n",
1575 card->mp_rd_bitmap);
5e6e3a92
BZ
1576
1577 while (true) {
1578 ret = mwifiex_get_rd_port(adapter, &port);
1579 if (ret) {
acebe8c1
ZL
1580 mwifiex_dbg(adapter, INFO,
1581 "info: no more rd_port available\n");
5e6e3a92
BZ
1582 break;
1583 }
05889f82
AK
1584 len_reg_l = reg->rd_len_p0_l + (port << 1);
1585 len_reg_u = reg->rd_len_p0_u + (port << 1);
5e6e3a92
BZ
1586 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1587 rx_len |= (u16) card->mp_regs[len_reg_l];
acebe8c1
ZL
1588 mwifiex_dbg(adapter, INFO,
1589 "info: RX: port=%d rx_len=%u\n",
1590 port, rx_len);
5e6e3a92
BZ
1591 rx_blocks =
1592 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1593 1) / MWIFIEX_SDIO_BLOCK_SIZE;
5dbd326c
YAP
1594 if (rx_len <= INTF_HEADER_LEN ||
1595 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
92263a84 1596 card->mpa_rx.buf_size) {
acebe8c1
ZL
1597 mwifiex_dbg(adapter, ERROR,
1598 "invalid rx_len=%d\n",
1599 rx_len);
5e6e3a92
BZ
1600 return -1;
1601 }
5e6e3a92 1602
960d6d08 1603 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
acebe8c1
ZL
1604 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
1605 rx_len);
5e6e3a92 1606
960d6d08 1607 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
5e6e3a92 1608 port)) {
acebe8c1
ZL
1609 mwifiex_dbg(adapter, ERROR,
1610 "card_to_host_mpa failed: int status=%#x\n",
1611 sdio_ireg);
b60186f8 1612 goto term_cmd;
5e6e3a92
BZ
1613 }
1614 }
1615 }
1616
1617 return 0;
b60186f8
YAP
1618
1619term_cmd:
1620 /* terminate cmd */
1621 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
acebe8c1 1622 mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
b60186f8 1623 else
acebe8c1
ZL
1624 mwifiex_dbg(adapter, INFO,
1625 "info: CFG reg val = %d\n", cr);
b60186f8
YAP
1626
1627 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
acebe8c1
ZL
1628 mwifiex_dbg(adapter, ERROR,
1629 "write CFG reg failed\n");
b60186f8 1630 else
acebe8c1 1631 mwifiex_dbg(adapter, INFO, "info: write success\n");
b60186f8
YAP
1632
1633 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
acebe8c1
ZL
1634 mwifiex_dbg(adapter, ERROR,
1635 "read CFG reg failed\n");
b60186f8 1636 else
acebe8c1
ZL
1637 mwifiex_dbg(adapter, INFO,
1638 "info: CFG reg val =%x\n", cr);
b60186f8
YAP
1639
1640 return -1;
5e6e3a92
BZ
1641}
1642
1643/*
1644 * This function aggregates transmission buffers in driver and downloads
1645 * the aggregated packet to card.
1646 *
1647 * The individual packets are aggregated by copying into an aggregation
1648 * buffer and then downloaded to the card. Previous unsent packets in the
1649 * aggregation buffer are pre-copied first before new packets are added.
1650 * Aggregation is done till there is space left in the aggregation buffer,
1651 * or till new packets are available.
1652 *
1653 * The function will only download the packet to the card when aggregation
1654 * stops, otherwise it will just aggregate the packet in aggregation buffer
1655 * and return.
1656 */
1657static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
0fc0d43b 1658 u8 *payload, u32 pkt_len, u32 port,
5e6e3a92
BZ
1659 u32 next_pkt_len)
1660{
1661 struct sdio_mmc_card *card = adapter->card;
1662 int ret = 0;
1663 s32 f_send_aggr_buf = 0;
1664 s32 f_send_cur_buf = 0;
1665 s32 f_precopy_cur_buf = 0;
1666 s32 f_postcopy_cur_buf = 0;
0fc0d43b 1667 u32 mport;
5e6e3a92 1668
b60186f8
YAP
1669 if (!card->mpa_tx.enabled ||
1670 (card->has_control_mask && (port == CTRL_PORT)) ||
1671 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
acebe8c1
ZL
1672 mwifiex_dbg(adapter, WARN,
1673 "info: %s: tx aggregation disabled\n",
1674 __func__);
5e6e3a92
BZ
1675
1676 f_send_cur_buf = 1;
1677 goto tx_curr_single;
1678 }
1679
1680 if (next_pkt_len) {
1681 /* More pkt in TX queue */
acebe8c1
ZL
1682 mwifiex_dbg(adapter, INFO,
1683 "info: %s: more packets in queue.\n",
1684 __func__);
5e6e3a92
BZ
1685
1686 if (MP_TX_AGGR_IN_PROGRESS(card)) {
c7346c32 1687 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
5e6e3a92
BZ
1688 f_precopy_cur_buf = 1;
1689
1690 if (!(card->mp_wr_bitmap &
5dbd326c
YAP
1691 (1 << card->curr_wr_port)) ||
1692 !MP_TX_AGGR_BUF_HAS_ROOM(
1693 card, pkt_len + next_pkt_len))
5e6e3a92
BZ
1694 f_send_aggr_buf = 1;
1695 } else {
1696 /* No room in Aggr buf, send it */
1697 f_send_aggr_buf = 1;
1698
c7346c32 1699 if (!(card->mp_wr_bitmap &
5e6e3a92
BZ
1700 (1 << card->curr_wr_port)))
1701 f_send_cur_buf = 1;
1702 else
1703 f_postcopy_cur_buf = 1;
1704 }
1705 } else {
5dbd326c
YAP
1706 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1707 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
5e6e3a92
BZ
1708 f_precopy_cur_buf = 1;
1709 else
1710 f_send_cur_buf = 1;
1711 }
1712 } else {
1713 /* Last pkt in TX queue */
acebe8c1
ZL
1714 mwifiex_dbg(adapter, INFO,
1715 "info: %s: Last packet in Tx Queue.\n",
1716 __func__);
5e6e3a92
BZ
1717
1718 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1719 /* some packs in Aggr buf already */
1720 f_send_aggr_buf = 1;
1721
1722 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1723 f_precopy_cur_buf = 1;
1724 else
1725 /* No room in Aggr buf, send it */
1726 f_send_cur_buf = 1;
1727 } else {
1728 f_send_cur_buf = 1;
1729 }
1730 }
1731
1732 if (f_precopy_cur_buf) {
acebe8c1
ZL
1733 mwifiex_dbg(adapter, DATA,
1734 "data: %s: precopy current buffer\n",
1735 __func__);
5e6e3a92
BZ
1736 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1737
1738 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
c23b7c8f 1739 mp_tx_aggr_port_limit_reached(card))
5e6e3a92
BZ
1740 /* No more pkts allowed in Aggr buf, send it */
1741 f_send_aggr_buf = 1;
1742 }
1743
1744 if (f_send_aggr_buf) {
acebe8c1
ZL
1745 mwifiex_dbg(adapter, DATA,
1746 "data: %s: send aggr buffer: %d %d\n",
1747 __func__, card->mpa_tx.start_port,
1748 card->mpa_tx.ports);
b60186f8
YAP
1749 if (card->supports_sdio_new_mode) {
1750 u32 port_count;
1751 int i;
1752
1753 for (i = 0, port_count = 0; i < card->max_ports; i++)
1754 if (card->mpa_tx.ports & BIT(i))
1755 port_count++;
1756
1757 /* Writing data from "start_port + 0" to "start_port +
1758 * port_count -1", so decrease the count by 1
1759 */
1760 port_count--;
1761 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1762 (port_count << 8)) + card->mpa_tx.start_port;
1763 } else {
1764 mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
1765 (card->mpa_tx.ports << 4)) +
1766 card->mpa_tx.start_port;
1767 }
1768
5e6e3a92 1769 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
0fc0d43b 1770 card->mpa_tx.buf_len, mport);
5e6e3a92
BZ
1771
1772 MP_TX_AGGR_BUF_RESET(card);
1773 }
1774
1775tx_curr_single:
1776 if (f_send_cur_buf) {
acebe8c1
ZL
1777 mwifiex_dbg(adapter, DATA,
1778 "data: %s: send current buffer %d\n",
1779 __func__, port);
5e6e3a92
BZ
1780 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1781 adapter->ioport + port);
1782 }
1783
1784 if (f_postcopy_cur_buf) {
acebe8c1
ZL
1785 mwifiex_dbg(adapter, DATA,
1786 "data: %s: postcopy current buffer\n",
1787 __func__);
5e6e3a92
BZ
1788 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1789 }
1790
1791 return ret;
1792}
1793
1794/*
1795 * This function downloads data from driver to card.
1796 *
1797 * Both commands and data packets are transferred to the card by this
1798 * function.
1799 *
1800 * This function adds the SDIO specific header to the front of the buffer
1801 * before transferring. The header contains the length of the packet and
1802 * the type. The firmware handles the packets based upon this set type.
1803 */
1804static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
d930faee 1805 u8 type, struct sk_buff *skb,
5e6e3a92
BZ
1806 struct mwifiex_tx_param *tx_param)
1807{
1808 struct sdio_mmc_card *card = adapter->card;
270e58e8 1809 int ret;
5e6e3a92
BZ
1810 u32 buf_block_len;
1811 u32 blk_size;
0fc0d43b 1812 u32 port = CTRL_PORT;
d930faee
AK
1813 u8 *payload = (u8 *)skb->data;
1814 u32 pkt_len = skb->len;
5e6e3a92
BZ
1815
1816 /* Allocate buffer and copy payload */
1817 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1818 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
83e612f6
TM
1819 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1820 *(__le16 *)&payload[2] = cpu_to_le16(type);
5e6e3a92
BZ
1821
1822 /*
1823 * This is SDIO specific header
1824 * u16 length,
1825 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1826 * MWIFIEX_TYPE_EVENT = 3)
1827 */
1828 if (type == MWIFIEX_TYPE_DATA) {
1829 ret = mwifiex_get_wr_port_data(adapter, &port);
1830 if (ret) {
acebe8c1
ZL
1831 mwifiex_dbg(adapter, ERROR,
1832 "%s: no wr_port available\n",
1833 __func__);
5e6e3a92
BZ
1834 return ret;
1835 }
1836 } else {
1837 adapter->cmd_sent = true;
1838 /* Type must be MWIFIEX_TYPE_CMD */
1839
1840 if (pkt_len <= INTF_HEADER_LEN ||
1841 pkt_len > MWIFIEX_UPLD_SIZE)
acebe8c1
ZL
1842 mwifiex_dbg(adapter, ERROR,
1843 "%s: payload=%p, nb=%d\n",
1844 __func__, payload, pkt_len);
b60186f8
YAP
1845
1846 if (card->supports_sdio_new_mode)
1847 port = CMD_PORT_SLCT;
5e6e3a92
BZ
1848 }
1849
1850 /* Transfer data to card */
1851 pkt_len = buf_block_len * blk_size;
1852
1853 if (tx_param)
1854 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
5dbd326c
YAP
1855 port, tx_param->next_pkt_len
1856 );
5e6e3a92
BZ
1857 else
1858 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
5dbd326c 1859 port, 0);
5e6e3a92
BZ
1860
1861 if (ret) {
1862 if (type == MWIFIEX_TYPE_CMD)
1863 adapter->cmd_sent = false;
bb71d01a 1864 if (type == MWIFIEX_TYPE_DATA) {
5e6e3a92 1865 adapter->data_sent = false;
bb71d01a
AP
1866 /* restore curr_wr_port in error cases */
1867 card->curr_wr_port = port;
1868 card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
1869 }
5e6e3a92
BZ
1870 } else {
1871 if (type == MWIFIEX_TYPE_DATA) {
1872 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1873 adapter->data_sent = true;
1874 else
1875 adapter->data_sent = false;
1876 }
1877 }
1878
1879 return ret;
1880}
1881
1882/*
1883 * This function allocates the MPA Tx and Rx buffers.
1884 */
1885static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1886 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1887{
1888 struct sdio_mmc_card *card = adapter->card;
92263a84 1889 u32 rx_buf_size;
5e6e3a92
BZ
1890 int ret = 0;
1891
1892 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1893 if (!card->mpa_tx.buf) {
5e6e3a92
BZ
1894 ret = -1;
1895 goto error;
1896 }
1897
1898 card->mpa_tx.buf_size = mpa_tx_buf_size;
1899
92263a84
ZL
1900 rx_buf_size = max_t(u32, mpa_rx_buf_size,
1901 (u32)SDIO_MAX_AGGR_BUF_SIZE);
1902 card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
5e6e3a92 1903 if (!card->mpa_rx.buf) {
5e6e3a92
BZ
1904 ret = -1;
1905 goto error;
1906 }
1907
92263a84 1908 card->mpa_rx.buf_size = rx_buf_size;
5e6e3a92
BZ
1909
1910error:
1911 if (ret) {
1912 kfree(card->mpa_tx.buf);
1913 kfree(card->mpa_rx.buf);
1914 }
1915
1916 return ret;
1917}
1918
1919/*
1920 * This function unregisters the SDIO device.
1921 *
1922 * The SDIO IRQ is released, the function is disabled and driver
1923 * data is set to null.
1924 */
1925static void
1926mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1927{
1928 struct sdio_mmc_card *card = adapter->card;
1929
1930 if (adapter->card) {
5e6e3a92 1931 sdio_claim_host(card->func);
5e6e3a92
BZ
1932 sdio_disable_func(card->func);
1933 sdio_release_host(card->func);
5e6e3a92
BZ
1934 }
1935}
1936
1937/*
1938 * This function registers the SDIO device.
1939 *
1940 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1941 */
1942static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1943{
232fde06 1944 int ret;
5e6e3a92
BZ
1945 struct sdio_mmc_card *card = adapter->card;
1946 struct sdio_func *func = card->func;
1947
1948 /* save adapter pointer in card */
1949 card->adapter = adapter;
828cf222 1950 adapter->tx_buf_size = card->tx_buf_size;
5e6e3a92
BZ
1951
1952 sdio_claim_host(func);
1953
5e6e3a92
BZ
1954 /* Set block size */
1955 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
232fde06 1956 sdio_release_host(func);
5e6e3a92 1957 if (ret) {
acebe8c1
ZL
1958 mwifiex_dbg(adapter, ERROR,
1959 "cannot set SDIO block size\n");
232fde06 1960 return ret;
5e6e3a92
BZ
1961 }
1962
5e6e3a92
BZ
1963
1964 adapter->dev = &func->dev;
e3bea1c8 1965
05889f82 1966 strcpy(adapter->fw_name, card->firmware);
54881c6b
AK
1967 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
1968 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
5e6e3a92
BZ
1969
1970 return 0;
5e6e3a92
BZ
1971}
1972
1973/*
1974 * This function initializes the SDIO driver.
1975 *
1976 * The following initializations steps are followed -
1977 * - Read the Host interrupt status register to acknowledge
1978 * the first interrupt got from bootloader
1979 * - Disable host interrupt mask register
1980 * - Get SDIO port
5e6e3a92
BZ
1981 * - Initialize SDIO variables in card
1982 * - Allocate MP registers
1983 * - Allocate MPA Tx and Rx buffers
1984 */
1985static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1986{
1987 struct sdio_mmc_card *card = adapter->card;
05889f82 1988 const struct mwifiex_sdio_card_reg *reg = card->reg;
5e6e3a92 1989 int ret;
ab93d4ff 1990 u8 sdio_ireg;
5e6e3a92 1991
3c59e328
AK
1992 sdio_set_drvdata(card->func, card);
1993
5e6e3a92 1994 /*
554a0113 1995 * Read the host_int_status_reg for ACK the first interrupt got
5e6e3a92
BZ
1996 * from the bootloader. If we don't do this we get a interrupt
1997 * as soon as we register the irq.
1998 */
554a0113 1999 mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
5e6e3a92 2000
5e6e3a92
BZ
2001 /* Get SDIO ioport */
2002 mwifiex_init_sdio_ioport(adapter);
2003
5e6e3a92
BZ
2004 /* Initialize SDIO variables in card */
2005 card->mp_rd_bitmap = 0;
2006 card->mp_wr_bitmap = 0;
05889f82
AK
2007 card->curr_rd_port = reg->start_rd_port;
2008 card->curr_wr_port = reg->start_wr_port;
5e6e3a92 2009
05889f82 2010 card->mp_data_port_mask = reg->data_port_mask;
5e6e3a92
BZ
2011
2012 card->mpa_tx.buf_len = 0;
2013 card->mpa_tx.pkt_cnt = 0;
2014 card->mpa_tx.start_port = 0;
2015
7d7ab022 2016 card->mpa_tx.enabled = 1;
05889f82 2017 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
5e6e3a92
BZ
2018
2019 card->mpa_rx.buf_len = 0;
2020 card->mpa_rx.pkt_cnt = 0;
2021 card->mpa_rx.start_port = 0;
2022
7d7ab022 2023 card->mpa_rx.enabled = 1;
05889f82 2024 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
5e6e3a92
BZ
2025
2026 /* Allocate buffers for SDIO MP-A */
05889f82 2027 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
0d2e7a5c 2028 if (!card->mp_regs)
b53575ec 2029 return -ENOMEM;
5e6e3a92 2030
c23b7c8f
AK
2031 /* Allocate skb pointer buffers */
2032 card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
2033 card->mp_agg_pkt_limit, GFP_KERNEL);
2034 card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
2035 card->mp_agg_pkt_limit, GFP_KERNEL);
5e6e3a92 2036 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
e1aa93a4
AK
2037 card->mp_tx_agg_buf_size,
2038 card->mp_rx_agg_buf_size);
5e6e3a92 2039 if (ret) {
acebe8c1
ZL
2040 mwifiex_dbg(adapter, ERROR,
2041 "failed to alloc sdio mp-a buffers\n");
5e6e3a92
BZ
2042 kfree(card->mp_regs);
2043 return -1;
2044 }
2045
b4e8aebb 2046 adapter->auto_tdls = card->can_auto_tdls;
1fe192d8 2047 adapter->ext_scan = card->can_ext_scan;
5e6e3a92
BZ
2048 return ret;
2049}
2050
2051/*
2052 * This function resets the MPA Tx and Rx buffers.
2053 */
2054static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
2055{
2056 struct sdio_mmc_card *card = adapter->card;
2057
2058 MP_TX_AGGR_BUF_RESET(card);
2059 MP_RX_AGGR_BUF_RESET(card);
2060}
2061
2062/*
2063 * This function cleans up the allocated card buffers.
2064 *
2065 * The following are freed by this function -
2066 * - MP registers
2067 * - MPA Tx buffer
2068 * - MPA Rx buffer
2069 */
2070static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2071{
2072 struct sdio_mmc_card *card = adapter->card;
2073
2074 kfree(card->mp_regs);
c23b7c8f
AK
2075 kfree(card->mpa_rx.skb_arr);
2076 kfree(card->mpa_rx.len_arr);
5e6e3a92
BZ
2077 kfree(card->mpa_tx.buf);
2078 kfree(card->mpa_rx.buf);
3c59e328
AK
2079 sdio_set_drvdata(card->func, NULL);
2080 kfree(card);
5e6e3a92
BZ
2081}
2082
2083/*
2084 * This function updates the MP end port in card.
2085 */
2086static void
2087mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2088{
2089 struct sdio_mmc_card *card = adapter->card;
05889f82 2090 const struct mwifiex_sdio_card_reg *reg = card->reg;
5e6e3a92
BZ
2091 int i;
2092
2093 card->mp_end_port = port;
2094
05889f82 2095 card->mp_data_port_mask = reg->data_port_mask;
5e6e3a92 2096
b60186f8
YAP
2097 if (reg->start_wr_port) {
2098 for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
2099 card->mp_data_port_mask &=
2100 ~(1 << (card->max_ports - i));
2101 }
5e6e3a92 2102
05889f82 2103 card->curr_wr_port = reg->start_wr_port;
5e6e3a92 2104
acebe8c1
ZL
2105 mwifiex_dbg(adapter, CMD,
2106 "cmd: mp_end_port %d, data port mask 0x%x\n",
2107 port, card->mp_data_port_mask);
5e6e3a92
BZ
2108}
2109
2f5872b6 2110static struct mwifiex_adapter *save_adapter;
088df424 2111static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
d31ab357 2112{
088df424
AK
2113 struct sdio_mmc_card *card = adapter->card;
2114 struct mmc_host *target = card->func->card->host;
7f5855c9 2115
d31ab357
AK
2116 /* The actual reset operation must be run outside of driver thread.
2117 * This is because mmc_remove_host() will cause the device to be
2118 * instantly destroyed, and the driver then needs to end its thread,
2119 * leading to a deadlock.
2120 *
2121 * We run it in a totally independent workqueue.
2122 */
2123
acebe8c1 2124 mwifiex_dbg(adapter, WARN, "Resetting card...\n");
7f5855c9 2125 mmc_remove_host(target);
b4f1b177
MY
2126 /* 200ms delay is based on experiment with sdhci controller */
2127 mdelay(200);
81df6caf 2128 target->rescan_entered = 0; /* rescan non-removable cards */
7f5855c9 2129 mmc_add_host(target);
d31ab357 2130}
8915d738 2131
54881c6b
AK
2132/* This function read/write firmware */
2133static enum
2134rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
2135 u8 doneflag)
2136{
2137 struct sdio_mmc_card *card = adapter->card;
2138 int ret, tries;
2139 u8 ctrl_data = 0;
2140
2141 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
2142 &ret);
2143 if (ret) {
acebe8c1 2144 mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
54881c6b
AK
2145 return RDWR_STATUS_FAILURE;
2146 }
2147 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2148 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
2149 &ret);
2150 if (ret) {
acebe8c1 2151 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
54881c6b
AK
2152 return RDWR_STATUS_FAILURE;
2153 }
2154 if (ctrl_data == FW_DUMP_DONE)
2155 break;
2156 if (doneflag && ctrl_data == doneflag)
2157 return RDWR_STATUS_DONE;
2158 if (ctrl_data != FW_DUMP_HOST_READY) {
acebe8c1
ZL
2159 mwifiex_dbg(adapter, WARN,
2160 "The ctrl reg was changed, re-try again!\n");
54881c6b
AK
2161 sdio_writeb(card->func, FW_DUMP_HOST_READY,
2162 card->reg->fw_dump_ctrl, &ret);
2163 if (ret) {
acebe8c1 2164 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
54881c6b
AK
2165 return RDWR_STATUS_FAILURE;
2166 }
2167 }
2168 usleep_range(100, 200);
2169 }
2170 if (ctrl_data == FW_DUMP_HOST_READY) {
acebe8c1
ZL
2171 mwifiex_dbg(adapter, ERROR,
2172 "Fail to pull ctrl_data\n");
54881c6b
AK
2173 return RDWR_STATUS_FAILURE;
2174 }
2175
2176 return RDWR_STATUS_SUCCESS;
2177}
2178
2179/* This function dump firmware memory to file */
2f5872b6 2180static void mwifiex_sdio_fw_dump_work(struct mwifiex_adapter *adapter)
54881c6b 2181{
54881c6b
AK
2182 struct sdio_mmc_card *card = adapter->card;
2183 int ret = 0;
2184 unsigned int reg, reg_start, reg_end;
2185 u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
54881c6b
AK
2186 enum rdwr_status stat;
2187 u32 memory_size;
2188 static char *env[] = { "DRIVER=mwifiex_sdio", "EVENT=fw_dump", NULL };
2189
11cd07a9
XH
2190 mwifiex_dump_drv_info(adapter);
2191
b4e8aebb 2192 if (!card->can_dump_fw)
54881c6b
AK
2193 return;
2194
2195 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2196 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2197
2198 if (entry->mem_ptr) {
2199 vfree(entry->mem_ptr);
2200 entry->mem_ptr = NULL;
2201 }
2202 entry->mem_size = 0;
2203 }
2204
2205 mwifiex_pm_wakeup_card(adapter);
2206 sdio_claim_host(card->func);
2207
acebe8c1 2208 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
54881c6b
AK
2209
2210 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2211 if (stat == RDWR_STATUS_FAILURE)
2212 goto done;
2213
2214 reg = card->reg->fw_dump_start;
2215 /* Read the number of the memories which will dump */
2216 dump_num = sdio_readb(card->func, reg, &ret);
2217 if (ret) {
acebe8c1 2218 mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
54881c6b
AK
2219 goto done;
2220 }
2221
2222 /* Read the length of every memory which will dump */
2223 for (idx = 0; idx < dump_num; idx++) {
2224 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2225
2226 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2227 if (stat == RDWR_STATUS_FAILURE)
2228 goto done;
2229
2230 memory_size = 0;
2231 reg = card->reg->fw_dump_start;
2232 for (i = 0; i < 4; i++) {
2233 read_reg = sdio_readb(card->func, reg, &ret);
2234 if (ret) {
acebe8c1 2235 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
54881c6b
AK
2236 goto done;
2237 }
2238 memory_size |= (read_reg << i*8);
2239 reg++;
2240 }
2241
2242 if (memory_size == 0) {
acebe8c1 2243 mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
e065ddb8
AK
2244 ret = mwifiex_write_reg(adapter,
2245 card->reg->fw_dump_ctrl,
2246 FW_DUMP_READ_DONE);
2247 if (ret) {
2248 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2249 return;
2250 }
54881c6b
AK
2251 break;
2252 }
2253
acebe8c1
ZL
2254 mwifiex_dbg(adapter, DUMP,
2255 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
54881c6b
AK
2256 entry->mem_ptr = vmalloc(memory_size + 1);
2257 entry->mem_size = memory_size;
2258 if (!entry->mem_ptr) {
acebe8c1
ZL
2259 mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2260 entry->mem_name);
54881c6b
AK
2261 goto done;
2262 }
2263 dbg_ptr = entry->mem_ptr;
2264 end_ptr = dbg_ptr + memory_size;
2265
2266 doneflag = entry->done_flag;
acebe8c1
ZL
2267 mwifiex_dbg(adapter, DUMP,
2268 "Start %s output, please wait...\n",
2269 entry->mem_name);
54881c6b
AK
2270
2271 do {
2272 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2273 if (stat == RDWR_STATUS_FAILURE)
2274 goto done;
2275
2276 reg_start = card->reg->fw_dump_start;
2277 reg_end = card->reg->fw_dump_end;
2278 for (reg = reg_start; reg <= reg_end; reg++) {
2279 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2280 if (ret) {
acebe8c1
ZL
2281 mwifiex_dbg(adapter, ERROR,
2282 "SDIO read err\n");
54881c6b
AK
2283 goto done;
2284 }
2285 if (dbg_ptr < end_ptr)
2286 dbg_ptr++;
2287 else
acebe8c1
ZL
2288 mwifiex_dbg(adapter, ERROR,
2289 "Allocated buf not enough\n");
54881c6b
AK
2290 }
2291
2292 if (stat != RDWR_STATUS_DONE)
2293 continue;
2294
acebe8c1
ZL
2295 mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2296 entry->mem_name, dbg_ptr - entry->mem_ptr);
54881c6b
AK
2297 break;
2298 } while (1);
2299 }
acebe8c1 2300 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
54881c6b
AK
2301
2302 kobject_uevent_env(&adapter->wiphy->dev.kobj, KOBJ_CHANGE, env);
2303
2304done:
2305 sdio_release_host(card->func);
2306 adapter->curr_mem_idx = 0;
2307}
2308
8915d738
AK
2309static void mwifiex_sdio_work(struct work_struct *work)
2310{
54881c6b 2311 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_FW_DUMP,
2f5872b6
MH
2312 &iface_work_flags))
2313 mwifiex_sdio_fw_dump_work(save_adapter);
2314 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2315 &iface_work_flags))
2316 mwifiex_sdio_card_reset_work(save_adapter);
8915d738 2317}
d31ab357 2318
2f5872b6 2319static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
d31ab357
AK
2320/* This function resets the card */
2321static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2322{
2f5872b6
MH
2323 save_adapter = adapter;
2324 if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
8915d738
AK
2325 return;
2326
2f5872b6 2327 set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
8915d738 2328
2f5872b6 2329 schedule_work(&sdio_work);
d31ab357
AK
2330}
2331
54881c6b
AK
2332/* This function dumps FW information */
2333static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2334{
2f5872b6
MH
2335 save_adapter = adapter;
2336 if (test_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &iface_work_flags))
54881c6b
AK
2337 return;
2338
2f5872b6
MH
2339 set_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &iface_work_flags);
2340 schedule_work(&sdio_work);
54881c6b
AK
2341}
2342
809c6ea8
XH
2343/* Function to dump SDIO function registers and SDIO scratch registers in case
2344 * of FW crash
2345 */
2346static int
2347mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2348{
2349 char *p = drv_buf;
2350 struct sdio_mmc_card *cardp = adapter->card;
2351 int ret = 0;
2352 u8 count, func, data, index = 0, size = 0;
2353 u8 reg, reg_start, reg_end;
2354 char buf[256], *ptr;
2355
2356 if (!p)
2357 return 0;
2358
9cc0dbf0 2359 mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
809c6ea8
XH
2360
2361 mwifiex_pm_wakeup_card(adapter);
2362
2363 sdio_claim_host(cardp->func);
2364
2365 for (count = 0; count < 5; count++) {
2366 memset(buf, 0, sizeof(buf));
2367 ptr = buf;
2368
2369 switch (count) {
2370 case 0:
2371 /* Read the registers of SDIO function0 */
2372 func = count;
2373 reg_start = 0;
2374 reg_end = 9;
2375 break;
2376 case 1:
2377 /* Read the registers of SDIO function1 */
2378 func = count;
2379 reg_start = cardp->reg->func1_dump_reg_start;
2380 reg_end = cardp->reg->func1_dump_reg_end;
2381 break;
2382 case 2:
2383 index = 0;
2384 func = 1;
2385 reg_start = cardp->reg->func1_spec_reg_table[index++];
2386 size = cardp->reg->func1_spec_reg_num;
2387 reg_end = cardp->reg->func1_spec_reg_table[size-1];
2388 break;
2389 default:
2390 /* Read the scratch registers of SDIO function1 */
2391 if (count == 4)
2392 mdelay(100);
2393 func = 1;
2394 reg_start = cardp->reg->func1_scratch_reg;
2395 reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
2396 }
2397
2398 if (count != 2)
2399 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
2400 func, reg_start, reg_end);
2401 else
2402 ptr += sprintf(ptr, "SDIO Func%d: ", func);
2403
2404 for (reg = reg_start; reg <= reg_end;) {
2405 if (func == 0)
2406 data = sdio_f0_readb(cardp->func, reg, &ret);
2407 else
2408 data = sdio_readb(cardp->func, reg, &ret);
2409
2410 if (count == 2)
2411 ptr += sprintf(ptr, "(%#x) ", reg);
2412 if (!ret) {
2413 ptr += sprintf(ptr, "%02x ", data);
2414 } else {
2415 ptr += sprintf(ptr, "ERR");
2416 break;
2417 }
2418
2419 if (count == 2 && reg < reg_end)
2420 reg = cardp->reg->func1_spec_reg_table[index++];
2421 else
2422 reg++;
2423 }
2424
acebe8c1 2425 mwifiex_dbg(adapter, MSG, "%s\n", buf);
809c6ea8
XH
2426 p += sprintf(p, "%s\n", buf);
2427 }
2428
2429 sdio_release_host(cardp->func);
2430
9cc0dbf0 2431 mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
809c6ea8
XH
2432
2433 return p - drv_buf;
2434}
2435
5e6e3a92
BZ
2436static struct mwifiex_if_ops sdio_ops = {
2437 .init_if = mwifiex_init_sdio,
2438 .cleanup_if = mwifiex_cleanup_sdio,
2439 .check_fw_status = mwifiex_check_fw_status,
2440 .prog_fw = mwifiex_prog_fw_w_helper,
2441 .register_dev = mwifiex_register_dev,
2442 .unregister_dev = mwifiex_unregister_dev,
2443 .enable_int = mwifiex_sdio_enable_host_int,
232fde06 2444 .disable_int = mwifiex_sdio_disable_host_int,
5e6e3a92
BZ
2445 .process_int_status = mwifiex_process_int_status,
2446 .host_to_card = mwifiex_sdio_host_to_card,
2447 .wakeup = mwifiex_pm_wakeup_card,
2448 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2449
2450 /* SDIO specific */
2451 .update_mp_end_port = mwifiex_update_mp_end_port,
2452 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
d930faee
AK
2453 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2454 .event_complete = mwifiex_sdio_event_complete,
d31ab357 2455 .card_reset = mwifiex_sdio_card_reset,
54881c6b 2456 .fw_dump = mwifiex_sdio_fw_dump,
809c6ea8 2457 .reg_dump = mwifiex_sdio_reg_dump,
92263a84 2458 .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
5e6e3a92
BZ
2459};
2460
2461/*
2462 * This function initializes the SDIO driver.
2463 *
2464 * This initiates the semaphore and registers the device with
2465 * SDIO bus.
2466 */
2467static int
2468mwifiex_sdio_init_module(void)
2469{
5e6e3a92
BZ
2470 sema_init(&add_remove_card_sem, 1);
2471
287546df
AK
2472 /* Clear the flag in case user removes the card. */
2473 user_rmmod = 0;
2474
636c4598 2475 return sdio_register_driver(&mwifiex_sdio);
5e6e3a92
BZ
2476}
2477
2478/*
2479 * This function cleans up the SDIO driver.
2480 *
2481 * The following major steps are followed for cleanup -
2482 * - Resume the device if its suspended
2483 * - Disconnect the device if connected
2484 * - Shutdown the firmware
2485 * - Unregister the device from SDIO bus.
2486 */
2487static void
2488mwifiex_sdio_cleanup_module(void)
2489{
287546df
AK
2490 if (!down_interruptible(&add_remove_card_sem))
2491 up(&add_remove_card_sem);
5e6e3a92 2492
287546df
AK
2493 /* Set the flag as user is removing this module. */
2494 user_rmmod = 1;
2f5872b6 2495 cancel_work_sync(&sdio_work);
5e6e3a92 2496
5e6e3a92
BZ
2497 sdio_unregister_driver(&mwifiex_sdio);
2498}
2499
2500module_init(mwifiex_sdio_init_module);
2501module_exit(mwifiex_sdio_cleanup_module);
2502
2503MODULE_AUTHOR("Marvell International Ltd.");
2504MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2505MODULE_VERSION(SDIO_VERSION);
2506MODULE_LICENSE("GPL v2");
98e6b9df 2507MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
e3bea1c8
BZ
2508MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2509MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
b60186f8 2510MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
030bb75a 2511MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
This page took 0.467674 seconds and 5 git commands to generate.