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