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