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