Merge tag 'sound-fix-4.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[deliverable/linux.git] / drivers / net / wireless / mwifiex / sdio.c
1 /*
2 * Marvell Wireless LAN device driver: SDIO specific handling
3 *
4 * Copyright (C) 2011-2014, 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
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 */
47 static u8 user_rmmod;
48
49 static struct mwifiex_if_ops sdio_ops;
50 static unsigned long iface_work_flags;
51
52 static struct semaphore add_remove_card_sem;
53
54 static 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
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 */
80 static int
81 mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
82 {
83 int ret;
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",
87 func->vendor, func->device, func->class, func->num);
88
89 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
90 if (!card)
91 return -ENOMEM;
92
93 card->func = func;
94
95 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
96
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;
104 card->supports_sdio_new_mode = data->supports_sdio_new_mode;
105 card->has_control_mask = data->has_control_mask;
106 card->tx_buf_size = data->tx_buf_size;
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;
109 card->can_dump_fw = data->can_dump_fw;
110 card->can_auto_tdls = data->can_auto_tdls;
111 card->can_ext_scan = data->can_ext_scan;
112 }
113
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__);
120 kfree(card);
121 return -EIO;
122 }
123
124 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
125 MWIFIEX_SDIO)) {
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
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 */
147 static 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) {
169 mwifiex_dbg(adapter, WARN,
170 "device already resumed\n");
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
183 /*
184 * SDIO remove.
185 *
186 * This function removes the interface and frees up the card structure.
187 */
188 static void
189 mwifiex_sdio_remove(struct sdio_func *func)
190 {
191 struct sdio_mmc_card *card;
192 struct mwifiex_adapter *adapter;
193 struct mwifiex_private *priv;
194
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
203 mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
204
205 if (user_rmmod) {
206 if (adapter->is_suspended)
207 mwifiex_sdio_resume(adapter->dev);
208
209 mwifiex_deauthenticate_all(adapter);
210
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);
214 }
215
216 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
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 */
229 static int mwifiex_sdio_suspend(struct device *dev)
230 {
231 struct sdio_func *func = dev_to_sdio_func(dev);
232 struct sdio_mmc_card *card;
233 struct mwifiex_adapter *adapter;
234 mmc_pm_flag_t pm_flag = 0;
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",
240 sdio_func_id(func), pm_flag);
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 */
260 if (!mwifiex_enable_hs(adapter)) {
261 mwifiex_dbg(adapter, ERROR,
262 "cmd: failed to suspend\n");
263 adapter->hs_enabling = false;
264 return -EFAULT;
265 }
266
267 mwifiex_dbg(adapter, INFO,
268 "cmd: suspend with MMC_PM_KEEP_POWER\n");
269 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
270
271 /* Indicate device suspended */
272 adapter->is_suspended = true;
273 adapter->hs_enabling = false;
274
275 return ret;
276 }
277
278 /* Device ID for SD8786 */
279 #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
280 /* Device ID for SD8787 */
281 #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
282 /* Device ID for SD8797 */
283 #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
284 /* Device ID for SD8897 */
285 #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
286 /* Device ID for SD8887 */
287 #define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
288 /* Device ID for SD8801 */
289 #define SDIO_DEVICE_ID_MARVELL_8801 (0x9139)
290
291
292 /* WLAN IDs */
293 static const struct sdio_device_id mwifiex_ids[] = {
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},
300 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
301 .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
302 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
303 .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
304 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
305 .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
306 {},
307 };
308
309 MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
310
311 static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
312 .suspend = mwifiex_sdio_suspend,
313 .resume = mwifiex_sdio_resume,
314 };
315
316 static 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
327 /* Write data into SDIO card register. Caller claims SDIO device. */
328 static int
329 mwifiex_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
336 /*
337 * This function writes data into SDIO card register.
338 */
339 static int
340 mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
341 {
342 struct sdio_mmc_card *card = adapter->card;
343 int ret;
344
345 sdio_claim_host(card->func);
346 ret = mwifiex_write_reg_locked(card->func, reg, data);
347 sdio_release_host(card->func);
348
349 return ret;
350 }
351
352 /*
353 * This function reads data from SDIO card register.
354 */
355 static int
356 mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
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 */
376 static int
377 mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
378 u8 *buffer, u32 pkt_len, u32 port)
379 {
380 struct sdio_mmc_card *card = adapter->card;
381 int ret;
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) {
392 mwifiex_dbg(adapter, ERROR,
393 "%s: not allowed while suspended\n", __func__);
394 return -1;
395 }
396
397 sdio_claim_host(card->func);
398
399 ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
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 */
409 static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
410 u32 len, u32 port, u8 claim)
411 {
412 struct sdio_mmc_card *card = adapter->card;
413 int ret;
414 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
415 : BLOCK_MODE;
416 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
417 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
418 : len;
419 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
420
421 if (claim)
422 sdio_claim_host(card->func);
423
424 ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
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 */
438 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
439 {
440 mwifiex_dbg(adapter, EVENT,
441 "event: wakeup device...\n");
442
443 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
444 }
445
446 /*
447 * This function is called after the card has woken up.
448 *
449 * The card configuration register is reset.
450 */
451 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
452 {
453 mwifiex_dbg(adapter, EVENT,
454 "cmd: wakeup device completed\n");
455
456 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
457 }
458
459 /*
460 * This function is used to initialize IO ports for the
461 * chipsets supporting SDIO new mode eg SD8897.
462 */
463 static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
464 {
465 u8 reg;
466 struct sdio_mmc_card *card = adapter->card;
467
468 adapter->ioport = MEM_PORT;
469
470 /* enable sdio new mode */
471 if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, &reg))
472 return -1;
473 if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
474 reg | CMD53_NEW_MODE))
475 return -1;
476
477 /* Configure cmd port and enable reading rx length from the register */
478 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, &reg))
479 return -1;
480 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
481 reg | CMD_PORT_RD_LEN_EN))
482 return -1;
483
484 /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
485 * completed
486 */
487 if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, &reg))
488 return -1;
489 if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
490 reg | CMD_PORT_AUTO_EN))
491 return -1;
492
493 return 0;
494 }
495
496 /* This function initializes the IO ports.
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 */
503 static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
504 {
505 u8 reg;
506 struct sdio_mmc_card *card = adapter->card;
507
508 adapter->ioport = 0;
509
510 if (card->supports_sdio_new_mode) {
511 if (mwifiex_init_sdio_new_mode(adapter))
512 return -1;
513 goto cont;
514 }
515
516 /* Read the IO port */
517 if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, &reg))
518 adapter->ioport |= (reg & 0xff);
519 else
520 return -1;
521
522 if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, &reg))
523 adapter->ioport |= ((reg & 0xff) << 8);
524 else
525 return -1;
526
527 if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, &reg))
528 adapter->ioport |= ((reg & 0xff) << 16);
529 else
530 return -1;
531 cont:
532 mwifiex_dbg(adapter, INFO,
533 "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
534
535 /* Set Host interrupt reset to read to clear */
536 if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, &reg))
537 mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
538 reg | card->reg->sdio_int_mask);
539 else
540 return -1;
541
542 /* Dnld/Upld ready set to auto reset */
543 if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
544 mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
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 */
555 static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
556 u8 *payload, u32 pkt_len, u32 port)
557 {
558 u32 i = 0;
559 int ret;
560
561 do {
562 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
563 if (ret) {
564 i++;
565 mwifiex_dbg(adapter, ERROR,
566 "host_to_card, write iomem\t"
567 "(%d) failed: %d\n", i, ret);
568 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
569 mwifiex_dbg(adapter, ERROR,
570 "write CFG reg failed\n");
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 */
589 static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
590 {
591 struct sdio_mmc_card *card = adapter->card;
592 const struct mwifiex_sdio_card_reg *reg = card->reg;
593 u32 rd_bitmap = card->mp_rd_bitmap;
594
595 mwifiex_dbg(adapter, DATA,
596 "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
597
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 }
605
606 if ((card->has_control_mask) &&
607 (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
608 card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
609 *port = CTRL_PORT;
610 mwifiex_dbg(adapter, DATA,
611 "data: port=%d mp_rd_bitmap=0x%08x\n",
612 *port, card->mp_rd_bitmap);
613 return 0;
614 }
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
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);
629
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 */
640 static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
641 {
642 struct sdio_mmc_card *card = adapter->card;
643 const struct mwifiex_sdio_card_reg *reg = card->reg;
644 u32 wr_bitmap = card->mp_wr_bitmap;
645
646 mwifiex_dbg(adapter, DATA,
647 "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
648
649 if (!(wr_bitmap & card->mp_data_port_mask)) {
650 adapter->data_sent = true;
651 return -EBUSY;
652 }
653
654 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
655 card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
656 *port = card->curr_wr_port;
657 if (++card->curr_wr_port == card->mp_end_port)
658 card->curr_wr_port = reg->start_wr_port;
659 } else {
660 adapter->data_sent = true;
661 return -EBUSY;
662 }
663
664 if ((card->has_control_mask) && (*port == CTRL_PORT)) {
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);
669 return -1;
670 }
671
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);
675
676 return 0;
677 }
678
679 /*
680 * This function polls the card status.
681 */
682 static int
683 mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
684 {
685 struct sdio_mmc_card *card = adapter->card;
686 u32 tries;
687 u8 cs;
688
689 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
690 if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
691 break;
692 else if ((cs & bits) == bits)
693 return 0;
694
695 usleep_range(10, 20);
696 }
697
698 mwifiex_dbg(adapter, ERROR,
699 "poll card status failed, tries = %d\n", tries);
700
701 return -1;
702 }
703
704 /*
705 * This function reads the firmware status.
706 */
707 static int
708 mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
709 {
710 struct sdio_mmc_card *card = adapter->card;
711 const struct mwifiex_sdio_card_reg *reg = card->reg;
712 u8 fws0, fws1;
713
714 if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
715 return -1;
716
717 if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
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 */
731 static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
732 {
733 struct sdio_mmc_card *card = adapter->card;
734 struct sdio_func *func = card->func;
735
736 sdio_claim_host(func);
737 mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
738 sdio_release_irq(func);
739 sdio_release_host(func);
740 }
741
742 /*
743 * This function reads the interrupt status from card.
744 */
745 static 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)) {
754 mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
755 return;
756 }
757
758 sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
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 */
767 mwifiex_dbg(adapter, INTR,
768 "int: sdio_ireg = %#x\n", sdio_ireg);
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 */
781 static void
782 mwifiex_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
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 */
808 static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
809 {
810 struct sdio_mmc_card *card = adapter->card;
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) {
819 mwifiex_dbg(adapter, ERROR,
820 "claim irq failed: ret=%d\n", ret);
821 goto out;
822 }
823
824 /* Simply write the mask to the register */
825 ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
826 card->reg->host_int_enable);
827 if (ret) {
828 mwifiex_dbg(adapter, ERROR,
829 "enable host interrupt failed\n");
830 sdio_release_irq(func);
831 }
832
833 out:
834 sdio_release_host(func);
835 return ret;
836 }
837
838 /*
839 * This function sends a data buffer to the card.
840 */
841 static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
842 u32 *type, u8 *buffer,
843 u32 npayload, u32 ioport)
844 {
845 int ret;
846 u32 nb;
847
848 if (!buffer) {
849 mwifiex_dbg(adapter, ERROR,
850 "%s: buffer is NULL\n", __func__);
851 return -1;
852 }
853
854 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
855
856 if (ret) {
857 mwifiex_dbg(adapter, ERROR,
858 "%s: read iomem failed: %d\n", __func__,
859 ret);
860 return -1;
861 }
862
863 nb = le16_to_cpu(*(__le16 *) (buffer));
864 if (nb > npayload) {
865 mwifiex_dbg(adapter, ERROR,
866 "%s: invalid packet, nb=%d npayload=%d\n",
867 __func__, nb, npayload);
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 */
883 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
884 struct mwifiex_fw_image *fw)
885 {
886 struct sdio_mmc_card *card = adapter->card;
887 const struct mwifiex_sdio_card_reg *reg = card->reg;
888 int ret;
889 u8 *firmware = fw->fw_buf;
890 u32 firmware_len = fw->fw_len;
891 u32 offset = 0;
892 u8 base0, base1;
893 u8 *fwbuf;
894 u16 len = 0;
895 u32 txlen, tx_blocks = 0, tries;
896 u32 i = 0;
897
898 if (!firmware_len) {
899 mwifiex_dbg(adapter, ERROR,
900 "firmware image not found! Terminating download\n");
901 return -1;
902 }
903
904 mwifiex_dbg(adapter, INFO,
905 "info: downloading FW image (%d bytes)\n",
906 firmware_len);
907
908 /* Assume that the allocated buffer is 8-byte aligned */
909 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
910 if (!fwbuf)
911 return -ENOMEM;
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) {
920 mwifiex_dbg(adapter, ERROR,
921 "FW download with helper:\t"
922 "poll status timeout @ %d\n", offset);
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++) {
931 ret = mwifiex_read_reg(adapter, reg->base_0_reg,
932 &base0);
933 if (ret) {
934 mwifiex_dbg(adapter, ERROR,
935 "dev BASE0 register read failed:\t"
936 "base0=%#04X(%d). Terminating dnld\n",
937 base0, base0);
938 goto done;
939 }
940 ret = mwifiex_read_reg(adapter, reg->base_1_reg,
941 &base1);
942 if (ret) {
943 mwifiex_dbg(adapter, ERROR,
944 "dev BASE1 register read failed:\t"
945 "base1=%#04X(%d). Terminating dnld\n",
946 base1, base1);
947 goto done;
948 }
949 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
950
951 if (len)
952 break;
953
954 usleep_range(10, 20);
955 }
956
957 if (!len) {
958 break;
959 } else if (len > MWIFIEX_UPLD_SIZE) {
960 mwifiex_dbg(adapter, ERROR,
961 "FW dnld failed @ %d, invalid length %d\n",
962 offset, len);
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) {
972 mwifiex_dbg(adapter, ERROR,
973 "FW dnld failed @ %d, over max retry\n",
974 offset);
975 ret = -1;
976 goto done;
977 }
978 mwifiex_dbg(adapter, ERROR,
979 "CRC indicated by the helper:\t"
980 "len = 0x%04X, txlen = %d\n", len, txlen);
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
992 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
993 / MWIFIEX_SDIO_BLOCK_SIZE;
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,
1001 adapter->ioport);
1002 if (ret) {
1003 mwifiex_dbg(adapter, ERROR,
1004 "FW download, write iomem (%d) failed @ %d\n",
1005 i, offset);
1006 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
1007 mwifiex_dbg(adapter, ERROR,
1008 "write CFG reg failed\n");
1009
1010 ret = -1;
1011 goto done;
1012 }
1013
1014 offset += txlen;
1015 } while (true);
1016
1017 mwifiex_dbg(adapter, MSG,
1018 "info: FW download over, size %d bytes\n", offset);
1019
1020 ret = 0;
1021 done:
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 */
1031 static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
1032 u32 poll_num)
1033 {
1034 struct sdio_mmc_card *card = adapter->card;
1035 int ret = 0;
1036 u16 firmware_stat;
1037 u32 tries;
1038 u8 winner_status;
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;
1045 if (firmware_stat == FIRMWARE_READY_SDIO) {
1046 ret = 0;
1047 break;
1048 } else {
1049 msleep(100);
1050 ret = -1;
1051 }
1052 }
1053
1054 if (ret) {
1055 if (mwifiex_read_reg
1056 (adapter, card->reg->status_reg_0, &winner_status))
1057 winner_status = 0;
1058
1059 if (winner_status)
1060 adapter->winner = 0;
1061 else
1062 adapter->winner = 1;
1063 }
1064 return ret;
1065 }
1066
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 */
1073 static 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) {
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);
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) {
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);
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
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 */
1129 static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
1130 struct sk_buff *skb, u32 upld_typ)
1131 {
1132 u8 *cmd_buf;
1133 __le16 *curr_ptr = (__le16 *)skb->data;
1134 u16 pkt_len = le16_to_cpu(*curr_ptr);
1135 struct mwifiex_rxinfo *rx_info;
1136
1137 if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
1138 skb_trim(skb, pkt_len);
1139 skb_pull(skb, INTF_HEADER_LEN);
1140 }
1141
1142 switch (upld_typ) {
1143 case MWIFIEX_TYPE_AGGR_DATA:
1144 mwifiex_dbg(adapter, INFO,
1145 "info: --- Rx: Aggr Data packet ---\n");
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
1158 case MWIFIEX_TYPE_DATA:
1159 mwifiex_dbg(adapter, DATA,
1160 "info: --- Rx: Data packet ---\n");
1161 if (adapter->rx_work_enabled) {
1162 skb_queue_tail(&adapter->rx_data_q, skb);
1163 adapter->data_received = true;
1164 atomic_inc(&adapter->rx_pending);
1165 } else {
1166 mwifiex_handle_rx_packet(adapter, skb);
1167 }
1168 break;
1169
1170 case MWIFIEX_TYPE_CMD:
1171 mwifiex_dbg(adapter, CMD,
1172 "info: --- Rx: Cmd Response ---\n");
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,
1179 skb->data,
1180 skb->len);
1181
1182 memcpy(cmd_buf, skb->data,
1183 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1184 skb->len));
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:
1194 mwifiex_dbg(adapter, EVENT,
1195 "info: --- Rx: Event ---\n");
1196 adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
1197
1198 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
1199 memcpy(adapter->event_body,
1200 skb->data + MWIFIEX_EVENT_HEADER_LEN,
1201 skb->len);
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:
1210 mwifiex_dbg(adapter, ERROR,
1211 "unknown upload type %#x\n", upld_typ);
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 */
1228 static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1229 u16 rx_len, u8 port)
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;
1235 s32 f_post_aggr_cur = 0;
1236 struct sk_buff *skb_deaggr;
1237 struct sk_buff *skb = NULL;
1238 u32 pkt_len, pkt_type, mport, pind;
1239 u8 *curr_ptr;
1240
1241 if ((card->has_control_mask) && (port == CTRL_PORT)) {
1242 /* Read the command Resp without aggr */
1243 mwifiex_dbg(adapter, CMD,
1244 "info: %s: no aggregation for cmd\t"
1245 "response\n", __func__);
1246
1247 f_do_rx_cur = 1;
1248 goto rx_curr_single;
1249 }
1250
1251 if (!card->mpa_rx.enabled) {
1252 mwifiex_dbg(adapter, WARN,
1253 "info: %s: rx aggregation disabled\n",
1254 __func__);
1255
1256 f_do_rx_cur = 1;
1257 goto rx_curr_single;
1258 }
1259
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))))) {
1264 /* Some more data RX pending */
1265 mwifiex_dbg(adapter, INFO,
1266 "info: %s: not last packet\n", __func__);
1267
1268 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1269 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
1270 f_aggr_cur = 1;
1271 } else {
1272 /* No room in Aggr buf, do rx aggr now */
1273 f_do_rx_aggr = 1;
1274 f_post_aggr_cur = 1;
1275 }
1276 } else {
1277 /* Rx aggr not in progress */
1278 f_aggr_cur = 1;
1279 }
1280
1281 } else {
1282 /* No more data RX pending */
1283 mwifiex_dbg(adapter, INFO,
1284 "info: %s: last packet\n", __func__);
1285
1286 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1287 f_do_rx_aggr = 1;
1288 if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
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) {
1299 mwifiex_dbg(adapter, INFO,
1300 "info: current packet aggregation\n");
1301 /* Curr pkt can be aggregated */
1302 mp_rx_aggr_setup(card, rx_len, port);
1303
1304 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1305 mp_rx_aggr_port_limit_reached(card)) {
1306 mwifiex_dbg(adapter, INFO,
1307 "info: %s: aggregated packet\t"
1308 "limit reached\n", __func__);
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 */
1316 mwifiex_dbg(adapter, DATA,
1317 "info: do_rx_aggr: num of packets: %d\n",
1318 card->mpa_rx.pkt_cnt);
1319
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 }
1339
1340 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1341 card->mpa_rx.buf_len, mport, 1))
1342 goto error;
1343
1344 curr_ptr = card->mpa_rx.buf;
1345
1346 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1347 u32 *len_arr = card->mpa_rx.len_arr;
1348
1349 /* get curr PKT len & type */
1350 pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
1351 pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
1352
1353 /* copy pkt to deaggr buf */
1354 skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
1355 GFP_KERNEL |
1356 GFP_DMA);
1357 if (!skb_deaggr) {
1358 mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
1359 "drop pkt len=%d type=%d\n",
1360 pkt_len, pkt_type);
1361 curr_ptr += len_arr[pind];
1362 continue;
1363 }
1364
1365 skb_put(skb_deaggr, len_arr[pind]);
1366
1367 if ((pkt_type == MWIFIEX_TYPE_DATA ||
1368 (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
1369 adapter->sdio_rx_aggr_enable)) &&
1370 (pkt_len <= len_arr[pind])) {
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 {
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]);
1386 dev_kfree_skb_any(skb_deaggr);
1387 }
1388 curr_ptr += len_arr[pind];
1389 }
1390 MP_RX_AGGR_BUF_RESET(card);
1391 }
1392
1393 rx_curr_single:
1394 if (f_do_rx_cur) {
1395 mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
1396 port, rx_len);
1397
1398 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
1399 if (!skb) {
1400 mwifiex_dbg(adapter, ERROR,
1401 "single skb allocated fail,\t"
1402 "drop pkt port=%d len=%d\n", port, rx_len);
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
1410 skb_put(skb, rx_len);
1411
1412 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1413 skb->data, skb->len,
1414 adapter->ioport + port))
1415 goto error;
1416 if (!adapter->sdio_rx_aggr_enable &&
1417 pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
1418 mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
1419 "current SDIO RX Aggr not enabled\n",
1420 pkt_type);
1421 dev_kfree_skb_any(skb);
1422 return 0;
1423 }
1424
1425 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1426 }
1427 if (f_post_aggr_cur) {
1428 mwifiex_dbg(adapter, INFO,
1429 "info: current packet aggregation\n");
1430 /* Curr pkt can be aggregated */
1431 mp_rx_aggr_setup(card, rx_len, port);
1432 }
1433
1434 return 0;
1435 error:
1436 if (MP_RX_AGGR_IN_PROGRESS(card))
1437 MP_RX_AGGR_BUF_RESET(card);
1438
1439 if (f_do_rx_cur && skb)
1440 /* Single transfer pending. Free curr buff also */
1441 dev_kfree_skb_any(skb);
1442
1443 return -1;
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 */
1461 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1462 {
1463 struct sdio_mmc_card *card = adapter->card;
1464 const struct mwifiex_sdio_card_reg *reg = card->reg;
1465 int ret = 0;
1466 u8 sdio_ireg;
1467 struct sk_buff *skb;
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;
1473 u32 bitmap;
1474 u8 cr;
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
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 */
1493 rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
1494 rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
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);
1501 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
1502
1503 skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
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)) {
1512 mwifiex_dbg(adapter, ERROR,
1513 "%s: failed to card_to_host", __func__);
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))
1520 mwifiex_dbg(adapter, ERROR,
1521 "%s:Received wrong packet on cmd port",
1522 __func__);
1523
1524 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1525 }
1526
1527 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
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
1538 mwifiex_dbg(adapter, INTR,
1539 "int: DNLD: wr_bitmap=0x%x\n",
1540 card->mp_wr_bitmap);
1541 if (adapter->data_sent &&
1542 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1543 mwifiex_dbg(adapter, INTR,
1544 "info: <--- Tx DONE Interrupt --->\n");
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. */
1552 if (card->has_control_mask && adapter->cmd_sent) {
1553 /* Check if firmware has attach buffer at command port and
1554 update just that in wr_bit_map. */
1555 card->mp_wr_bitmap |=
1556 (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
1557 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1558 adapter->cmd_sent = false;
1559 }
1560
1561 mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
1562 adapter->cmd_sent, adapter->data_sent);
1563 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
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;
1573 mwifiex_dbg(adapter, INTR,
1574 "int: UPLD: rd_bitmap=0x%x\n",
1575 card->mp_rd_bitmap);
1576
1577 while (true) {
1578 ret = mwifiex_get_rd_port(adapter, &port);
1579 if (ret) {
1580 mwifiex_dbg(adapter, INFO,
1581 "info: no more rd_port available\n");
1582 break;
1583 }
1584 len_reg_l = reg->rd_len_p0_l + (port << 1);
1585 len_reg_u = reg->rd_len_p0_u + (port << 1);
1586 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1587 rx_len |= (u16) card->mp_regs[len_reg_l];
1588 mwifiex_dbg(adapter, INFO,
1589 "info: RX: port=%d rx_len=%u\n",
1590 port, rx_len);
1591 rx_blocks =
1592 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1593 1) / MWIFIEX_SDIO_BLOCK_SIZE;
1594 if (rx_len <= INTF_HEADER_LEN ||
1595 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1596 card->mpa_rx.buf_size) {
1597 mwifiex_dbg(adapter, ERROR,
1598 "invalid rx_len=%d\n",
1599 rx_len);
1600 return -1;
1601 }
1602
1603 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1604 mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
1605 rx_len);
1606
1607 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
1608 port)) {
1609 mwifiex_dbg(adapter, ERROR,
1610 "card_to_host_mpa failed: int status=%#x\n",
1611 sdio_ireg);
1612 goto term_cmd;
1613 }
1614 }
1615 }
1616
1617 return 0;
1618
1619 term_cmd:
1620 /* terminate cmd */
1621 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1622 mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
1623 else
1624 mwifiex_dbg(adapter, INFO,
1625 "info: CFG reg val = %d\n", cr);
1626
1627 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
1628 mwifiex_dbg(adapter, ERROR,
1629 "write CFG reg failed\n");
1630 else
1631 mwifiex_dbg(adapter, INFO, "info: write success\n");
1632
1633 if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
1634 mwifiex_dbg(adapter, ERROR,
1635 "read CFG reg failed\n");
1636 else
1637 mwifiex_dbg(adapter, INFO,
1638 "info: CFG reg val =%x\n", cr);
1639
1640 return -1;
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 */
1657 static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1658 u8 *payload, u32 pkt_len, u32 port,
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;
1667 u32 mport;
1668
1669 if (!card->mpa_tx.enabled ||
1670 (card->has_control_mask && (port == CTRL_PORT)) ||
1671 (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
1672 mwifiex_dbg(adapter, WARN,
1673 "info: %s: tx aggregation disabled\n",
1674 __func__);
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 */
1682 mwifiex_dbg(adapter, INFO,
1683 "info: %s: more packets in queue.\n",
1684 __func__);
1685
1686 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1687 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1688 f_precopy_cur_buf = 1;
1689
1690 if (!(card->mp_wr_bitmap &
1691 (1 << card->curr_wr_port)) ||
1692 !MP_TX_AGGR_BUF_HAS_ROOM(
1693 card, pkt_len + next_pkt_len))
1694 f_send_aggr_buf = 1;
1695 } else {
1696 /* No room in Aggr buf, send it */
1697 f_send_aggr_buf = 1;
1698
1699 if (!(card->mp_wr_bitmap &
1700 (1 << card->curr_wr_port)))
1701 f_send_cur_buf = 1;
1702 else
1703 f_postcopy_cur_buf = 1;
1704 }
1705 } else {
1706 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1707 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1708 f_precopy_cur_buf = 1;
1709 else
1710 f_send_cur_buf = 1;
1711 }
1712 } else {
1713 /* Last pkt in TX queue */
1714 mwifiex_dbg(adapter, INFO,
1715 "info: %s: Last packet in Tx Queue.\n",
1716 __func__);
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) {
1733 mwifiex_dbg(adapter, DATA,
1734 "data: %s: precopy current buffer\n",
1735 __func__);
1736 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1737
1738 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1739 mp_tx_aggr_port_limit_reached(card))
1740 /* No more pkts allowed in Aggr buf, send it */
1741 f_send_aggr_buf = 1;
1742 }
1743
1744 if (f_send_aggr_buf) {
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);
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
1769 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1770 card->mpa_tx.buf_len, mport);
1771
1772 MP_TX_AGGR_BUF_RESET(card);
1773 }
1774
1775 tx_curr_single:
1776 if (f_send_cur_buf) {
1777 mwifiex_dbg(adapter, DATA,
1778 "data: %s: send current buffer %d\n",
1779 __func__, port);
1780 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1781 adapter->ioport + port);
1782 }
1783
1784 if (f_postcopy_cur_buf) {
1785 mwifiex_dbg(adapter, DATA,
1786 "data: %s: postcopy current buffer\n",
1787 __func__);
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 */
1804 static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
1805 u8 type, struct sk_buff *skb,
1806 struct mwifiex_tx_param *tx_param)
1807 {
1808 struct sdio_mmc_card *card = adapter->card;
1809 int ret;
1810 u32 buf_block_len;
1811 u32 blk_size;
1812 u32 port = CTRL_PORT;
1813 u8 *payload = (u8 *)skb->data;
1814 u32 pkt_len = skb->len;
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;
1819 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1820 *(__le16 *)&payload[2] = cpu_to_le16(type);
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) {
1831 mwifiex_dbg(adapter, ERROR,
1832 "%s: no wr_port available\n",
1833 __func__);
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)
1842 mwifiex_dbg(adapter, ERROR,
1843 "%s: payload=%p, nb=%d\n",
1844 __func__, payload, pkt_len);
1845
1846 if (card->supports_sdio_new_mode)
1847 port = CMD_PORT_SLCT;
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,
1855 port, tx_param->next_pkt_len
1856 );
1857 else
1858 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
1859 port, 0);
1860
1861 if (ret) {
1862 if (type == MWIFIEX_TYPE_CMD)
1863 adapter->cmd_sent = false;
1864 if (type == MWIFIEX_TYPE_DATA) {
1865 adapter->data_sent = false;
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 }
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 */
1885 static 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;
1889 u32 rx_buf_size;
1890 int ret = 0;
1891
1892 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1893 if (!card->mpa_tx.buf) {
1894 ret = -1;
1895 goto error;
1896 }
1897
1898 card->mpa_tx.buf_size = mpa_tx_buf_size;
1899
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);
1903 if (!card->mpa_rx.buf) {
1904 ret = -1;
1905 goto error;
1906 }
1907
1908 card->mpa_rx.buf_size = rx_buf_size;
1909
1910 error:
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 */
1925 static void
1926 mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1927 {
1928 struct sdio_mmc_card *card = adapter->card;
1929
1930 if (adapter->card) {
1931 sdio_claim_host(card->func);
1932 sdio_disable_func(card->func);
1933 sdio_release_host(card->func);
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 */
1942 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1943 {
1944 int ret;
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;
1950 adapter->tx_buf_size = card->tx_buf_size;
1951
1952 sdio_claim_host(func);
1953
1954 /* Set block size */
1955 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1956 sdio_release_host(func);
1957 if (ret) {
1958 mwifiex_dbg(adapter, ERROR,
1959 "cannot set SDIO block size\n");
1960 return ret;
1961 }
1962
1963
1964 adapter->dev = &func->dev;
1965
1966 strcpy(adapter->fw_name, card->firmware);
1967 adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
1968 adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
1969
1970 return 0;
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
1981 * - Initialize SDIO variables in card
1982 * - Allocate MP registers
1983 * - Allocate MPA Tx and Rx buffers
1984 */
1985 static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1986 {
1987 struct sdio_mmc_card *card = adapter->card;
1988 const struct mwifiex_sdio_card_reg *reg = card->reg;
1989 int ret;
1990 u8 sdio_ireg;
1991
1992 sdio_set_drvdata(card->func, card);
1993
1994 /*
1995 * Read the host_int_status_reg for ACK the first interrupt got
1996 * from the bootloader. If we don't do this we get a interrupt
1997 * as soon as we register the irq.
1998 */
1999 mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
2000
2001 /* Get SDIO ioport */
2002 mwifiex_init_sdio_ioport(adapter);
2003
2004 /* Initialize SDIO variables in card */
2005 card->mp_rd_bitmap = 0;
2006 card->mp_wr_bitmap = 0;
2007 card->curr_rd_port = reg->start_rd_port;
2008 card->curr_wr_port = reg->start_wr_port;
2009
2010 card->mp_data_port_mask = reg->data_port_mask;
2011
2012 card->mpa_tx.buf_len = 0;
2013 card->mpa_tx.pkt_cnt = 0;
2014 card->mpa_tx.start_port = 0;
2015
2016 card->mpa_tx.enabled = 1;
2017 card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2018
2019 card->mpa_rx.buf_len = 0;
2020 card->mpa_rx.pkt_cnt = 0;
2021 card->mpa_rx.start_port = 0;
2022
2023 card->mpa_rx.enabled = 1;
2024 card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
2025
2026 /* Allocate buffers for SDIO MP-A */
2027 card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
2028 if (!card->mp_regs)
2029 return -ENOMEM;
2030
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);
2036 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
2037 card->mp_tx_agg_buf_size,
2038 card->mp_rx_agg_buf_size);
2039 if (ret) {
2040 mwifiex_dbg(adapter, ERROR,
2041 "failed to alloc sdio mp-a buffers\n");
2042 kfree(card->mp_regs);
2043 return -1;
2044 }
2045
2046 adapter->auto_tdls = card->can_auto_tdls;
2047 adapter->ext_scan = card->can_ext_scan;
2048 return ret;
2049 }
2050
2051 /*
2052 * This function resets the MPA Tx and Rx buffers.
2053 */
2054 static 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 */
2070 static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
2071 {
2072 struct sdio_mmc_card *card = adapter->card;
2073
2074 kfree(card->mp_regs);
2075 kfree(card->mpa_rx.skb_arr);
2076 kfree(card->mpa_rx.len_arr);
2077 kfree(card->mpa_tx.buf);
2078 kfree(card->mpa_rx.buf);
2079 sdio_set_drvdata(card->func, NULL);
2080 kfree(card);
2081 }
2082
2083 /*
2084 * This function updates the MP end port in card.
2085 */
2086 static void
2087 mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
2088 {
2089 struct sdio_mmc_card *card = adapter->card;
2090 const struct mwifiex_sdio_card_reg *reg = card->reg;
2091 int i;
2092
2093 card->mp_end_port = port;
2094
2095 card->mp_data_port_mask = reg->data_port_mask;
2096
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 }
2102
2103 card->curr_wr_port = reg->start_wr_port;
2104
2105 mwifiex_dbg(adapter, CMD,
2106 "cmd: mp_end_port %d, data port mask 0x%x\n",
2107 port, card->mp_data_port_mask);
2108 }
2109
2110 static struct mwifiex_adapter *save_adapter;
2111 static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
2112 {
2113 struct sdio_mmc_card *card = adapter->card;
2114 struct mmc_host *target = card->func->card->host;
2115
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
2124 mwifiex_dbg(adapter, WARN, "Resetting card...\n");
2125 mmc_remove_host(target);
2126 /* 200ms delay is based on experiment with sdhci controller */
2127 mdelay(200);
2128 target->rescan_entered = 0; /* rescan non-removable cards */
2129 mmc_add_host(target);
2130 }
2131
2132 /* This function read/write firmware */
2133 static enum
2134 rdwr_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) {
2144 mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
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) {
2151 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
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) {
2159 mwifiex_dbg(adapter, WARN,
2160 "The ctrl reg was changed, re-try again!\n");
2161 sdio_writeb(card->func, FW_DUMP_HOST_READY,
2162 card->reg->fw_dump_ctrl, &ret);
2163 if (ret) {
2164 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2165 return RDWR_STATUS_FAILURE;
2166 }
2167 }
2168 usleep_range(100, 200);
2169 }
2170 if (ctrl_data == FW_DUMP_HOST_READY) {
2171 mwifiex_dbg(adapter, ERROR,
2172 "Fail to pull ctrl_data\n");
2173 return RDWR_STATUS_FAILURE;
2174 }
2175
2176 return RDWR_STATUS_SUCCESS;
2177 }
2178
2179 /* This function dump firmware memory to file */
2180 static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
2181 {
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;
2186 enum rdwr_status stat;
2187 u32 memory_size;
2188
2189 if (!card->can_dump_fw)
2190 return;
2191
2192 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2193 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2194
2195 if (entry->mem_ptr) {
2196 vfree(entry->mem_ptr);
2197 entry->mem_ptr = NULL;
2198 }
2199 entry->mem_size = 0;
2200 }
2201
2202 mwifiex_pm_wakeup_card(adapter);
2203 sdio_claim_host(card->func);
2204
2205 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2206
2207 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2208 if (stat == RDWR_STATUS_FAILURE)
2209 goto done;
2210
2211 reg = card->reg->fw_dump_start;
2212 /* Read the number of the memories which will dump */
2213 dump_num = sdio_readb(card->func, reg, &ret);
2214 if (ret) {
2215 mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
2216 goto done;
2217 }
2218
2219 /* Read the length of every memory which will dump */
2220 for (idx = 0; idx < dump_num; idx++) {
2221 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2222
2223 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2224 if (stat == RDWR_STATUS_FAILURE)
2225 goto done;
2226
2227 memory_size = 0;
2228 reg = card->reg->fw_dump_start;
2229 for (i = 0; i < 4; i++) {
2230 read_reg = sdio_readb(card->func, reg, &ret);
2231 if (ret) {
2232 mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
2233 goto done;
2234 }
2235 memory_size |= (read_reg << i*8);
2236 reg++;
2237 }
2238
2239 if (memory_size == 0) {
2240 mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
2241 ret = mwifiex_write_reg(adapter,
2242 card->reg->fw_dump_ctrl,
2243 FW_DUMP_READ_DONE);
2244 if (ret) {
2245 mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
2246 return;
2247 }
2248 break;
2249 }
2250
2251 mwifiex_dbg(adapter, DUMP,
2252 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2253 entry->mem_ptr = vmalloc(memory_size + 1);
2254 entry->mem_size = memory_size;
2255 if (!entry->mem_ptr) {
2256 mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
2257 entry->mem_name);
2258 goto done;
2259 }
2260 dbg_ptr = entry->mem_ptr;
2261 end_ptr = dbg_ptr + memory_size;
2262
2263 doneflag = entry->done_flag;
2264 mwifiex_dbg(adapter, DUMP,
2265 "Start %s output, please wait...\n",
2266 entry->mem_name);
2267
2268 do {
2269 stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
2270 if (stat == RDWR_STATUS_FAILURE)
2271 goto done;
2272
2273 reg_start = card->reg->fw_dump_start;
2274 reg_end = card->reg->fw_dump_end;
2275 for (reg = reg_start; reg <= reg_end; reg++) {
2276 *dbg_ptr = sdio_readb(card->func, reg, &ret);
2277 if (ret) {
2278 mwifiex_dbg(adapter, ERROR,
2279 "SDIO read err\n");
2280 goto done;
2281 }
2282 if (dbg_ptr < end_ptr)
2283 dbg_ptr++;
2284 else
2285 mwifiex_dbg(adapter, ERROR,
2286 "Allocated buf not enough\n");
2287 }
2288
2289 if (stat != RDWR_STATUS_DONE)
2290 continue;
2291
2292 mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
2293 entry->mem_name, dbg_ptr - entry->mem_ptr);
2294 break;
2295 } while (1);
2296 }
2297 mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2298
2299 done:
2300 sdio_release_host(card->func);
2301 }
2302
2303 static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
2304 {
2305 mwifiex_drv_info_dump(adapter);
2306 mwifiex_sdio_fw_dump(adapter);
2307 mwifiex_upload_device_dump(adapter);
2308 }
2309
2310 static void mwifiex_sdio_work(struct work_struct *work)
2311 {
2312 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2313 &iface_work_flags))
2314 mwifiex_sdio_device_dump_work(save_adapter);
2315 if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2316 &iface_work_flags))
2317 mwifiex_sdio_card_reset_work(save_adapter);
2318 }
2319
2320 static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
2321 /* This function resets the card */
2322 static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
2323 {
2324 save_adapter = adapter;
2325 if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
2326 return;
2327
2328 set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
2329
2330 schedule_work(&sdio_work);
2331 }
2332
2333 /* This function dumps FW information */
2334 static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
2335 {
2336 save_adapter = adapter;
2337 if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
2338 return;
2339
2340 set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
2341 schedule_work(&sdio_work);
2342 }
2343
2344 /* Function to dump SDIO function registers and SDIO scratch registers in case
2345 * of FW crash
2346 */
2347 static int
2348 mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2349 {
2350 char *p = drv_buf;
2351 struct sdio_mmc_card *cardp = adapter->card;
2352 int ret = 0;
2353 u8 count, func, data, index = 0, size = 0;
2354 u8 reg, reg_start, reg_end;
2355 char buf[256], *ptr;
2356
2357 if (!p)
2358 return 0;
2359
2360 mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
2361
2362 mwifiex_pm_wakeup_card(adapter);
2363
2364 sdio_claim_host(cardp->func);
2365
2366 for (count = 0; count < 5; count++) {
2367 memset(buf, 0, sizeof(buf));
2368 ptr = buf;
2369
2370 switch (count) {
2371 case 0:
2372 /* Read the registers of SDIO function0 */
2373 func = count;
2374 reg_start = 0;
2375 reg_end = 9;
2376 break;
2377 case 1:
2378 /* Read the registers of SDIO function1 */
2379 func = count;
2380 reg_start = cardp->reg->func1_dump_reg_start;
2381 reg_end = cardp->reg->func1_dump_reg_end;
2382 break;
2383 case 2:
2384 index = 0;
2385 func = 1;
2386 reg_start = cardp->reg->func1_spec_reg_table[index++];
2387 size = cardp->reg->func1_spec_reg_num;
2388 reg_end = cardp->reg->func1_spec_reg_table[size-1];
2389 break;
2390 default:
2391 /* Read the scratch registers of SDIO function1 */
2392 if (count == 4)
2393 mdelay(100);
2394 func = 1;
2395 reg_start = cardp->reg->func1_scratch_reg;
2396 reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
2397 }
2398
2399 if (count != 2)
2400 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
2401 func, reg_start, reg_end);
2402 else
2403 ptr += sprintf(ptr, "SDIO Func%d: ", func);
2404
2405 for (reg = reg_start; reg <= reg_end;) {
2406 if (func == 0)
2407 data = sdio_f0_readb(cardp->func, reg, &ret);
2408 else
2409 data = sdio_readb(cardp->func, reg, &ret);
2410
2411 if (count == 2)
2412 ptr += sprintf(ptr, "(%#x) ", reg);
2413 if (!ret) {
2414 ptr += sprintf(ptr, "%02x ", data);
2415 } else {
2416 ptr += sprintf(ptr, "ERR");
2417 break;
2418 }
2419
2420 if (count == 2 && reg < reg_end)
2421 reg = cardp->reg->func1_spec_reg_table[index++];
2422 else
2423 reg++;
2424 }
2425
2426 mwifiex_dbg(adapter, MSG, "%s\n", buf);
2427 p += sprintf(p, "%s\n", buf);
2428 }
2429
2430 sdio_release_host(cardp->func);
2431
2432 mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
2433
2434 return p - drv_buf;
2435 }
2436
2437 static struct mwifiex_if_ops sdio_ops = {
2438 .init_if = mwifiex_init_sdio,
2439 .cleanup_if = mwifiex_cleanup_sdio,
2440 .check_fw_status = mwifiex_check_fw_status,
2441 .prog_fw = mwifiex_prog_fw_w_helper,
2442 .register_dev = mwifiex_register_dev,
2443 .unregister_dev = mwifiex_unregister_dev,
2444 .enable_int = mwifiex_sdio_enable_host_int,
2445 .disable_int = mwifiex_sdio_disable_host_int,
2446 .process_int_status = mwifiex_process_int_status,
2447 .host_to_card = mwifiex_sdio_host_to_card,
2448 .wakeup = mwifiex_pm_wakeup_card,
2449 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2450
2451 /* SDIO specific */
2452 .update_mp_end_port = mwifiex_update_mp_end_port,
2453 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
2454 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
2455 .event_complete = mwifiex_sdio_event_complete,
2456 .card_reset = mwifiex_sdio_card_reset,
2457 .reg_dump = mwifiex_sdio_reg_dump,
2458 .device_dump = mwifiex_sdio_device_dump,
2459 .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
2460 };
2461
2462 /*
2463 * This function initializes the SDIO driver.
2464 *
2465 * This initiates the semaphore and registers the device with
2466 * SDIO bus.
2467 */
2468 static int
2469 mwifiex_sdio_init_module(void)
2470 {
2471 sema_init(&add_remove_card_sem, 1);
2472
2473 /* Clear the flag in case user removes the card. */
2474 user_rmmod = 0;
2475
2476 return sdio_register_driver(&mwifiex_sdio);
2477 }
2478
2479 /*
2480 * This function cleans up the SDIO driver.
2481 *
2482 * The following major steps are followed for cleanup -
2483 * - Resume the device if its suspended
2484 * - Disconnect the device if connected
2485 * - Shutdown the firmware
2486 * - Unregister the device from SDIO bus.
2487 */
2488 static void
2489 mwifiex_sdio_cleanup_module(void)
2490 {
2491 if (!down_interruptible(&add_remove_card_sem))
2492 up(&add_remove_card_sem);
2493
2494 /* Set the flag as user is removing this module. */
2495 user_rmmod = 1;
2496 cancel_work_sync(&sdio_work);
2497
2498 sdio_unregister_driver(&mwifiex_sdio);
2499 }
2500
2501 module_init(mwifiex_sdio_init_module);
2502 module_exit(mwifiex_sdio_cleanup_module);
2503
2504 MODULE_AUTHOR("Marvell International Ltd.");
2505 MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
2506 MODULE_VERSION(SDIO_VERSION);
2507 MODULE_LICENSE("GPL v2");
2508 MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
2509 MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
2510 MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
2511 MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
2512 MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
This page took 0.080173 seconds and 6 git commands to generate.