blob: 4be8ccc8e9a7df011e8ee6db950ce1e7a6351087 [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
Amitkumar Karwar287546d2011-06-08 20:39:20 +053034/* The mwifiex_sdio_remove() callback function is called when
35 * user removes this module from kernel space or ejects
36 * the card from the slot. The driver handles these 2 cases
37 * differently.
38 * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
39 * HS_CANCEL etc.) are sent to the firmware.
40 * If the card is removed, there is no need to send these command.
41 *
42 * The variable 'user_rmmod' is used to distinguish these two
43 * scenarios. This flag is initialized as FALSE in case the card
44 * is removed, and will be set to TRUE for module removal when
45 * module_exit function is called.
46 */
47static u8 user_rmmod;
48
Bing Zhao5e6e3a92011-03-21 18:00:50 -070049static struct mwifiex_if_ops sdio_ops;
50
51static struct semaphore add_remove_card_sem;
52
Amitkumar Karwar287546d2011-06-08 20:39:20 +053053static int mwifiex_sdio_resume(struct device *dev);
54
Bing Zhao5e6e3a92011-03-21 18:00:50 -070055/*
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 */
63static int
64mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
65{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -070066 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070067 struct sdio_mmc_card *card = NULL;
68
69 pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -070070 func->vendor, func->device, func->class, func->num);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070071
72 card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +000073 if (!card)
Bing Zhao5e6e3a92011-03-21 18:00:50 -070074 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -070075
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__);
Christoph Fritzb53575e2011-05-08 22:50:09 +020086 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -070087 return -EIO;
88 }
89
Amitkumar Karward930fae2011-10-11 17:41:21 -070090 if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
91 MWIFIEX_SDIO)) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -070092 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 */
108static void
109mwifiex_sdio_remove(struct sdio_func *func)
110{
111 struct sdio_mmc_card *card;
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530112 struct mwifiex_adapter *adapter;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700113 struct mwifiex_private *priv;
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530114 int i;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700115
116 pr_debug("info: SDIO func num=%d\n", func->num);
117
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530118 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 if (user_rmmod) {
127 if (adapter->is_suspended)
128 mwifiex_sdio_resume(adapter->dev);
129
130 for (i = 0; i < adapter->priv_num; i++)
131 if ((GET_BSS_ROLE(adapter->priv[i]) ==
132 MWIFIEX_BSS_ROLE_STA) &&
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700133 adapter->priv[i]->media_connected)
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530134 mwifiex_deauthenticate(adapter->priv[i], NULL);
135
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700136 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
137 mwifiex_disable_auto_ds(priv);
138 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700139 }
Amitkumar Karwar287546d2011-06-08 20:39:20 +0530140
141 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
142 kfree(card);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700143}
144
145/*
146 * SDIO suspend.
147 *
148 * Kernel needs to suspend all functions separately. Therefore all
149 * registered functions must have drivers with suspend and resume
150 * methods. Failing that the kernel simply removes the whole card.
151 *
152 * If already not suspended, this function allocates and sends a host
153 * sleep activate request to the firmware and turns off the traffic.
154 */
155static int mwifiex_sdio_suspend(struct device *dev)
156{
157 struct sdio_func *func = dev_to_sdio_func(dev);
158 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700159 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700160 mmc_pm_flag_t pm_flag = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700161 int i;
162 int ret = 0;
163
164 if (func) {
165 pm_flag = sdio_get_host_pm_caps(func);
166 pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700167 sdio_func_id(func), pm_flag);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700168 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
169 pr_err("%s: cannot remain alive while host is"
170 " suspended\n", sdio_func_id(func));
171 return -ENOSYS;
172 }
173
174 card = sdio_get_drvdata(func);
175 if (!card || !card->adapter) {
176 pr_err("suspend: invalid card or adapter\n");
177 return 0;
178 }
179 } else {
180 pr_err("suspend: sdio_func is not specified\n");
181 return 0;
182 }
183
184 adapter = card->adapter;
185
186 /* Enable the Host Sleep */
Bing Zhao6966e1b2012-11-15 15:58:48 -0800187 if (!mwifiex_enable_hs(adapter)) {
188 dev_err(adapter->dev, "cmd: failed to suspend\n");
189 return -EFAULT;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700190 }
191
Bing Zhao6966e1b2012-11-15 15:58:48 -0800192 dev_dbg(adapter->dev, "cmd: suspend with MMC_PM_KEEP_POWER\n");
193 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
194
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700195 /* Indicate device suspended */
196 adapter->is_suspended = true;
197
198 for (i = 0; i < adapter->priv_num; i++)
199 netif_carrier_off(adapter->priv[i]->netdev);
200
201 return ret;
202}
203
204/*
205 * SDIO resume.
206 *
207 * Kernel needs to suspend all functions separately. Therefore all
208 * registered functions must have drivers with suspend and resume
209 * methods. Failing that the kernel simply removes the whole card.
210 *
211 * If already not resumed, this function turns on the traffic and
212 * sends a host sleep cancel request to the firmware.
213 */
214static int mwifiex_sdio_resume(struct device *dev)
215{
216 struct sdio_func *func = dev_to_sdio_func(dev);
217 struct sdio_mmc_card *card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700218 struct mwifiex_adapter *adapter;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700219 mmc_pm_flag_t pm_flag = 0;
220 int i;
221
222 if (func) {
223 pm_flag = sdio_get_host_pm_caps(func);
224 card = sdio_get_drvdata(func);
225 if (!card || !card->adapter) {
226 pr_err("resume: invalid card or adapter\n");
227 return 0;
228 }
229 } else {
230 pr_err("resume: sdio_func is not specified\n");
231 return 0;
232 }
233
234 adapter = card->adapter;
235
236 if (!adapter->is_suspended) {
237 dev_warn(adapter->dev, "device already resumed\n");
238 return 0;
239 }
240
241 adapter->is_suspended = false;
242
243 for (i = 0; i < adapter->priv_num; i++)
244 if (adapter->priv[i]->media_connected)
245 netif_carrier_on(adapter->priv[i]->netdev);
246
247 /* Disable Host Sleep */
248 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
Amitkumar Karwar600f5d92011-04-13 17:27:06 -0700249 MWIFIEX_ASYNC_CMD);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700250
251 return 0;
252}
253
254/* Device ID for SD8787 */
255#define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
Bing Zhaoe3bea1c2011-11-16 20:40:35 -0800256/* Device ID for SD8797 */
257#define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700258
259/* WLAN IDs */
260static const struct sdio_device_id mwifiex_ids[] = {
261 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787)},
Bing Zhaoe3bea1c2011-11-16 20:40:35 -0800262 {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797)},
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700263 {},
264};
265
266MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
267
268static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
269 .suspend = mwifiex_sdio_suspend,
270 .resume = mwifiex_sdio_resume,
271};
272
273static struct sdio_driver mwifiex_sdio = {
274 .name = "mwifiex_sdio",
275 .id_table = mwifiex_ids,
276 .probe = mwifiex_sdio_probe,
277 .remove = mwifiex_sdio_remove,
278 .drv = {
279 .owner = THIS_MODULE,
280 .pm = &mwifiex_sdio_pm_ops,
281 }
282};
283
284/*
285 * This function writes data into SDIO card register.
286 */
287static int
288mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u32 data)
289{
290 struct sdio_mmc_card *card = adapter->card;
291 int ret = -1;
292
293 sdio_claim_host(card->func);
294 sdio_writeb(card->func, (u8) data, reg, &ret);
295 sdio_release_host(card->func);
296
297 return ret;
298}
299
300/*
301 * This function reads data from SDIO card register.
302 */
303static int
304mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u32 *data)
305{
306 struct sdio_mmc_card *card = adapter->card;
307 int ret = -1;
308 u8 val;
309
310 sdio_claim_host(card->func);
311 val = sdio_readb(card->func, reg, &ret);
312 sdio_release_host(card->func);
313
314 *data = val;
315
316 return ret;
317}
318
319/*
320 * This function writes multiple data into SDIO card memory.
321 *
322 * This does not work in suspended mode.
323 */
324static int
325mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700326 u8 *buffer, u32 pkt_len, u32 port)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700327{
328 struct sdio_mmc_card *card = adapter->card;
329 int ret = -1;
330 u8 blk_mode =
331 (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
332 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
333 u32 blk_cnt =
334 (blk_mode ==
335 BLOCK_MODE) ? (pkt_len /
336 MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
337 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
338
339 if (adapter->is_suspended) {
340 dev_err(adapter->dev,
341 "%s: not allowed while suspended\n", __func__);
342 return -1;
343 }
344
345 sdio_claim_host(card->func);
346
347 if (!sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size))
348 ret = 0;
349
350 sdio_release_host(card->func);
351
352 return ret;
353}
354
355/*
356 * This function reads multiple data from SDIO card memory.
357 */
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700358static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
359 u32 len, u32 port, u8 claim)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700360{
361 struct sdio_mmc_card *card = adapter->card;
362 int ret = -1;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700363 u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
364 : BLOCK_MODE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700365 u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700366 u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
367 : len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700368 u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
369
370 if (claim)
371 sdio_claim_host(card->func);
372
373 if (!sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size))
374 ret = 0;
375
376 if (claim)
377 sdio_release_host(card->func);
378
379 return ret;
380}
381
382/*
383 * This function wakes up the card.
384 *
385 * A host power up command is written to the card configuration
386 * register to wake up the card.
387 */
388static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
389{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700390 dev_dbg(adapter->dev, "event: wakeup device...\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700391
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700392 return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700393}
394
395/*
396 * This function is called after the card has woken up.
397 *
398 * The card configuration register is reset.
399 */
400static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
401{
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700402 dev_dbg(adapter->dev, "cmd: wakeup device completed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700403
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -0700404 return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700405}
406
407/*
408 * This function initializes the IO ports.
409 *
410 * The following operations are performed -
411 * - Read the IO ports (0, 1 and 2)
412 * - Set host interrupt Reset-To-Read to clear
413 * - Set auto re-enable interrupt
414 */
415static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
416{
417 u32 reg;
418
419 adapter->ioport = 0;
420
421 /* Read the IO port */
422 if (!mwifiex_read_reg(adapter, IO_PORT_0_REG, &reg))
423 adapter->ioport |= (reg & 0xff);
424 else
425 return -1;
426
427 if (!mwifiex_read_reg(adapter, IO_PORT_1_REG, &reg))
428 adapter->ioport |= ((reg & 0xff) << 8);
429 else
430 return -1;
431
432 if (!mwifiex_read_reg(adapter, IO_PORT_2_REG, &reg))
433 adapter->ioport |= ((reg & 0xff) << 16);
434 else
435 return -1;
436
437 pr_debug("info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
438
439 /* Set Host interrupt reset to read to clear */
440 if (!mwifiex_read_reg(adapter, HOST_INT_RSR_REG, &reg))
441 mwifiex_write_reg(adapter, HOST_INT_RSR_REG,
442 reg | SDIO_INT_MASK);
443 else
444 return -1;
445
446 /* Dnld/Upld ready set to auto reset */
447 if (!mwifiex_read_reg(adapter, CARD_MISC_CFG_REG, &reg))
448 mwifiex_write_reg(adapter, CARD_MISC_CFG_REG,
449 reg | AUTO_RE_ENABLE_INT);
450 else
451 return -1;
452
453 return 0;
454}
455
456/*
457 * This function sends data to the card.
458 */
459static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
460 u8 *payload, u32 pkt_len, u32 port)
461{
462 u32 i = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700463 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700464
465 do {
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700466 ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700467 if (ret) {
468 i++;
469 dev_err(adapter->dev, "host_to_card, write iomem"
470 " (%d) failed: %d\n", i, ret);
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700471 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700472 dev_err(adapter->dev, "write CFG reg failed\n");
473
474 ret = -1;
475 if (i > MAX_WRITE_IOMEM_RETRY)
476 return ret;
477 }
478 } while (ret == -1);
479
480 return ret;
481}
482
483/*
484 * This function gets the read port.
485 *
486 * If control port bit is set in MP read bitmap, the control port
487 * is returned, otherwise the current read port is returned and
488 * the value is increased (provided it does not reach the maximum
489 * limit, in which case it is reset to 1)
490 */
491static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
492{
493 struct sdio_mmc_card *card = adapter->card;
494 u16 rd_bitmap = card->mp_rd_bitmap;
495
496 dev_dbg(adapter->dev, "data: mp_rd_bitmap=0x%04x\n", rd_bitmap);
497
498 if (!(rd_bitmap & (CTRL_PORT_MASK | DATA_PORT_MASK)))
499 return -1;
500
501 if (card->mp_rd_bitmap & CTRL_PORT_MASK) {
502 card->mp_rd_bitmap &= (u16) (~CTRL_PORT_MASK);
503 *port = CTRL_PORT;
504 dev_dbg(adapter->dev, "data: port=%d mp_rd_bitmap=0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700505 *port, card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700506 } else {
507 if (card->mp_rd_bitmap & (1 << card->curr_rd_port)) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700508 card->mp_rd_bitmap &= (u16)
509 (~(1 << card->curr_rd_port));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700510 *port = card->curr_rd_port;
511
512 if (++card->curr_rd_port == MAX_PORT)
513 card->curr_rd_port = 1;
514 } else {
515 return -1;
516 }
517
518 dev_dbg(adapter->dev,
519 "data: port=%d mp_rd_bitmap=0x%04x -> 0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700520 *port, rd_bitmap, card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700521 }
522 return 0;
523}
524
525/*
526 * This function gets the write port for data.
527 *
528 * The current write port is returned if available and the value is
529 * increased (provided it does not reach the maximum limit, in which
530 * case it is reset to 1)
531 */
532static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u8 *port)
533{
534 struct sdio_mmc_card *card = adapter->card;
535 u16 wr_bitmap = card->mp_wr_bitmap;
536
537 dev_dbg(adapter->dev, "data: mp_wr_bitmap=0x%04x\n", wr_bitmap);
538
539 if (!(wr_bitmap & card->mp_data_port_mask))
540 return -1;
541
542 if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
543 card->mp_wr_bitmap &= (u16) (~(1 << card->curr_wr_port));
544 *port = card->curr_wr_port;
545 if (++card->curr_wr_port == card->mp_end_port)
546 card->curr_wr_port = 1;
547 } else {
548 adapter->data_sent = true;
549 return -EBUSY;
550 }
551
552 if (*port == CTRL_PORT) {
553 dev_err(adapter->dev, "invalid data port=%d cur port=%d"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700554 " mp_wr_bitmap=0x%04x -> 0x%04x\n",
555 *port, card->curr_wr_port, wr_bitmap,
556 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700557 return -1;
558 }
559
560 dev_dbg(adapter->dev, "data: port=%d mp_wr_bitmap=0x%04x -> 0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700561 *port, wr_bitmap, card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700562
563 return 0;
564}
565
566/*
567 * This function polls the card status.
568 */
569static int
570mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
571{
572 u32 tries;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700573 u32 cs;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700574
575 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
576 if (mwifiex_read_reg(adapter, CARD_STATUS_REG, &cs))
577 break;
578 else if ((cs & bits) == bits)
579 return 0;
580
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700581 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700582 }
583
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700584 dev_err(adapter->dev, "poll card status failed, tries = %d\n", tries);
585
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700586 return -1;
587}
588
589/*
590 * This function reads the firmware status.
591 */
592static int
593mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
594{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700595 u32 fws0, fws1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700596
597 if (mwifiex_read_reg(adapter, CARD_FW_STATUS0_REG, &fws0))
598 return -1;
599
600 if (mwifiex_read_reg(adapter, CARD_FW_STATUS1_REG, &fws1))
601 return -1;
602
603 *dat = (u16) ((fws1 << 8) | fws0);
604
605 return 0;
606}
607
608/*
609 * This function disables the host interrupt.
610 *
611 * The host interrupt mask is read, the disable bit is reset and
612 * written back to the card host interrupt mask register.
613 */
614static int mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
615{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700616 u32 host_int_mask;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700617
618 /* Read back the host_int_mask register */
619 if (mwifiex_read_reg(adapter, HOST_INT_MASK_REG, &host_int_mask))
620 return -1;
621
622 /* Update with the mask and write back to the register */
623 host_int_mask &= ~HOST_INT_DISABLE;
624
625 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, host_int_mask)) {
626 dev_err(adapter->dev, "disable host interrupt failed\n");
627 return -1;
628 }
629
630 return 0;
631}
632
633/*
634 * This function enables the host interrupt.
635 *
636 * The host interrupt enable mask is written to the card
637 * host interrupt mask register.
638 */
639static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
640{
641 /* Simply write the mask to the register */
642 if (mwifiex_write_reg(adapter, HOST_INT_MASK_REG, HOST_INT_ENABLE)) {
643 dev_err(adapter->dev, "enable host interrupt failed\n");
644 return -1;
645 }
646 return 0;
647}
648
649/*
650 * This function sends a data buffer to the card.
651 */
652static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
653 u32 *type, u8 *buffer,
654 u32 npayload, u32 ioport)
655{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700656 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700657 u32 nb;
658
659 if (!buffer) {
660 dev_err(adapter->dev, "%s: buffer is NULL\n", __func__);
661 return -1;
662 }
663
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700664 ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700665
666 if (ret) {
667 dev_err(adapter->dev, "%s: read iomem failed: %d\n", __func__,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700668 ret);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700669 return -1;
670 }
671
672 nb = le16_to_cpu(*(__le16 *) (buffer));
673 if (nb > npayload) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700674 dev_err(adapter->dev, "%s: invalid packet, nb=%d npayload=%d\n",
675 __func__, nb, npayload);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700676 return -1;
677 }
678
679 *type = le16_to_cpu(*(__le16 *) (buffer + 2));
680
681 return ret;
682}
683
684/*
685 * This function downloads the firmware to the card.
686 *
687 * Firmware is downloaded to the card in blocks. Every block download
688 * is tested for CRC errors, and retried a number of times before
689 * returning failure.
690 */
691static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
692 struct mwifiex_fw_image *fw)
693{
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700694 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700695 u8 *firmware = fw->fw_buf;
696 u32 firmware_len = fw->fw_len;
697 u32 offset = 0;
698 u32 base0, base1;
699 u8 *fwbuf;
700 u16 len = 0;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700701 u32 txlen, tx_blocks = 0, tries;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700702 u32 i = 0;
703
704 if (!firmware_len) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700705 dev_err(adapter->dev,
706 "firmware image not found! Terminating download\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700707 return -1;
708 }
709
710 dev_dbg(adapter->dev, "info: downloading FW image (%d bytes)\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700711 firmware_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700712
713 /* Assume that the allocated buffer is 8-byte aligned */
714 fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
715 if (!fwbuf) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700716 dev_err(adapter->dev,
717 "unable to alloc buffer for FW. Terminating dnld\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +0200718 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700719 }
720
721 /* Perform firmware data transfer */
722 do {
723 /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
724 bits */
725 ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
726 DN_LD_CARD_RDY);
727 if (ret) {
728 dev_err(adapter->dev, "FW download with helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700729 " poll status timeout @ %d\n", offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700730 goto done;
731 }
732
733 /* More data? */
734 if (offset >= firmware_len)
735 break;
736
737 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
738 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_0,
739 &base0);
740 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700741 dev_err(adapter->dev,
742 "dev BASE0 register read failed: "
743 "base0=%#04X(%d). Terminating dnld\n",
744 base0, base0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700745 goto done;
746 }
747 ret = mwifiex_read_reg(adapter, HOST_F1_RD_BASE_1,
748 &base1);
749 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700750 dev_err(adapter->dev,
751 "dev BASE1 register read failed: "
752 "base1=%#04X(%d). Terminating dnld\n",
753 base1, base1);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700754 goto done;
755 }
756 len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
757
758 if (len)
759 break;
760
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700761 usleep_range(10, 20);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700762 }
763
764 if (!len) {
765 break;
766 } else if (len > MWIFIEX_UPLD_SIZE) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700767 dev_err(adapter->dev,
768 "FW dnld failed @ %d, invalid length %d\n",
769 offset, len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700770 ret = -1;
771 goto done;
772 }
773
774 txlen = len;
775
776 if (len & BIT(0)) {
777 i++;
778 if (i > MAX_WRITE_IOMEM_RETRY) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700779 dev_err(adapter->dev,
780 "FW dnld failed @ %d, over max retry\n",
781 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700782 ret = -1;
783 goto done;
784 }
785 dev_err(adapter->dev, "CRC indicated by the helper:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700786 " len = 0x%04X, txlen = %d\n", len, txlen);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700787 len &= ~BIT(0);
788 /* Setting this to 0 to resend from same offset */
789 txlen = 0;
790 } else {
791 i = 0;
792
793 /* Set blocksize to transfer - checking for last
794 block */
795 if (firmware_len - offset < txlen)
796 txlen = firmware_len - offset;
797
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700798 tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
799 / MWIFIEX_SDIO_BLOCK_SIZE;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700800
801 /* Copy payload to buffer */
802 memmove(fwbuf, &firmware[offset], txlen);
803 }
804
805 ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
806 MWIFIEX_SDIO_BLOCK_SIZE,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700807 adapter->ioport);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700808 if (ret) {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700809 dev_err(adapter->dev,
810 "FW download, write iomem (%d) failed @ %d\n",
811 i, offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700812 if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
813 dev_err(adapter->dev, "write CFG reg failed\n");
814
815 ret = -1;
816 goto done;
817 }
818
819 offset += txlen;
820 } while (true);
821
822 dev_dbg(adapter->dev, "info: FW download over, size %d bytes\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700823 offset);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700824
825 ret = 0;
826done:
827 kfree(fwbuf);
828 return ret;
829}
830
831/*
832 * This function checks the firmware status in card.
833 *
834 * The winner interface is also determined by this function.
835 */
836static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700837 u32 poll_num)
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700838{
839 int ret = 0;
840 u16 firmware_stat;
841 u32 tries;
842 u32 winner_status;
843
844 /* Wait for firmware initialization event */
845 for (tries = 0; tries < poll_num; tries++) {
846 ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
847 if (ret)
848 continue;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700849 if (firmware_stat == FIRMWARE_READY_SDIO) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700850 ret = 0;
851 break;
852 } else {
853 mdelay(100);
854 ret = -1;
855 }
856 }
857
Amitkumar Karward930fae2011-10-11 17:41:21 -0700858 if (ret) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700859 if (mwifiex_read_reg
860 (adapter, CARD_FW_STATUS0_REG, &winner_status))
861 winner_status = 0;
862
863 if (winner_status)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700864 adapter->winner = 0;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700865 else
Amitkumar Karward930fae2011-10-11 17:41:21 -0700866 adapter->winner = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700867 }
868 return ret;
869}
870
871/*
872 * This function reads the interrupt status from card.
873 */
874static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
875{
876 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -0700877 u32 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700878 unsigned long flags;
879
880 if (mwifiex_read_data_sync(adapter, card->mp_regs, MAX_MP_REGS,
Amitkumar Karwar572e8f32011-04-13 17:27:08 -0700881 REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK,
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700882 0)) {
883 dev_err(adapter->dev, "read mp_regs failed\n");
884 return;
885 }
886
887 sdio_ireg = card->mp_regs[HOST_INTSTATUS_REG];
888 if (sdio_ireg) {
889 /*
890 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
891 * Clear the interrupt status register
892 */
893 dev_dbg(adapter->dev, "int: sdio_ireg = %#x\n", sdio_ireg);
894 spin_lock_irqsave(&adapter->int_lock, flags);
895 adapter->int_status |= sdio_ireg;
896 spin_unlock_irqrestore(&adapter->int_lock, flags);
897 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700898}
899
900/*
901 * SDIO interrupt handler.
902 *
903 * This function reads the interrupt status from firmware and assigns
904 * the main process in workqueue which will handle the interrupt.
905 */
906static void
907mwifiex_sdio_interrupt(struct sdio_func *func)
908{
909 struct mwifiex_adapter *adapter;
910 struct sdio_mmc_card *card;
911
912 card = sdio_get_drvdata(func);
913 if (!card || !card->adapter) {
914 pr_debug("int: func=%p card=%p adapter=%p\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700915 func, card, card ? card->adapter : NULL);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700916 return;
917 }
918 adapter = card->adapter;
919
920 if (adapter->surprise_removed)
921 return;
922
923 if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
924 adapter->ps_state = PS_STATE_AWAKE;
925
926 mwifiex_interrupt_status(adapter);
927 queue_work(adapter->workqueue, &adapter->main_work);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700928}
929
930/*
931 * This function decodes a received packet.
932 *
933 * Based on the type, the packet is treated as either a data, or
934 * a command response, or an event, and the correct handler
935 * function is invoked.
936 */
937static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
938 struct sk_buff *skb, u32 upld_typ)
939{
940 u8 *cmd_buf;
941
942 skb_pull(skb, INTF_HEADER_LEN);
943
944 switch (upld_typ) {
945 case MWIFIEX_TYPE_DATA:
946 dev_dbg(adapter->dev, "info: --- Rx: Data packet ---\n");
947 mwifiex_handle_rx_packet(adapter, skb);
948 break;
949
950 case MWIFIEX_TYPE_CMD:
951 dev_dbg(adapter->dev, "info: --- Rx: Cmd Response ---\n");
952 /* take care of curr_cmd = NULL case */
953 if (!adapter->curr_cmd) {
954 cmd_buf = adapter->upld_buf;
955
956 if (adapter->ps_state == PS_STATE_SLEEP_CFM)
957 mwifiex_process_sleep_confirm_resp(adapter,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700958 skb->data,
959 skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700960
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -0700961 memcpy(cmd_buf, skb->data,
962 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
963 skb->len));
Bing Zhao5e6e3a92011-03-21 18:00:50 -0700964
965 dev_kfree_skb_any(skb);
966 } else {
967 adapter->cmd_resp_received = true;
968 adapter->curr_cmd->resp_skb = skb;
969 }
970 break;
971
972 case MWIFIEX_TYPE_EVENT:
973 dev_dbg(adapter->dev, "info: --- Rx: Event ---\n");
974 adapter->event_cause = *(u32 *) skb->data;
975
976 skb_pull(skb, MWIFIEX_EVENT_HEADER_LEN);
977
978 if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
979 memcpy(adapter->event_body, skb->data, skb->len);
980
981 /* event cause has been saved to adapter->event_cause */
982 adapter->event_received = true;
983 adapter->event_skb = skb;
984
985 break;
986
987 default:
988 dev_err(adapter->dev, "unknown upload type %#x\n", upld_typ);
989 dev_kfree_skb_any(skb);
990 break;
991 }
992
993 return 0;
994}
995
996/*
997 * This function transfers received packets from card to driver, performing
998 * aggregation if required.
999 *
1000 * For data received on control port, or if aggregation is disabled, the
1001 * received buffers are uploaded as separate packets. However, if aggregation
1002 * is enabled and required, the buffers are copied onto an aggregation buffer,
1003 * provided there is space left, processed and finally uploaded.
1004 */
1005static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
1006 struct sk_buff *skb, u8 port)
1007{
1008 struct sdio_mmc_card *card = adapter->card;
1009 s32 f_do_rx_aggr = 0;
1010 s32 f_do_rx_cur = 0;
1011 s32 f_aggr_cur = 0;
1012 struct sk_buff *skb_deaggr;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001013 u32 pind;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001014 u32 pkt_len, pkt_type = 0;
1015 u8 *curr_ptr;
1016 u32 rx_len = skb->len;
1017
1018 if (port == CTRL_PORT) {
1019 /* Read the command Resp without aggr */
1020 dev_dbg(adapter->dev, "info: %s: no aggregation for cmd "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001021 "response\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001022
1023 f_do_rx_cur = 1;
1024 goto rx_curr_single;
1025 }
1026
1027 if (!card->mpa_rx.enabled) {
1028 dev_dbg(adapter->dev, "info: %s: rx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001029 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001030
1031 f_do_rx_cur = 1;
1032 goto rx_curr_single;
1033 }
1034
1035 if (card->mp_rd_bitmap & (~((u16) CTRL_PORT_MASK))) {
1036 /* Some more data RX pending */
1037 dev_dbg(adapter->dev, "info: %s: not last packet\n", __func__);
1038
1039 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1040 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len)) {
1041 f_aggr_cur = 1;
1042 } else {
1043 /* No room in Aggr buf, do rx aggr now */
1044 f_do_rx_aggr = 1;
1045 f_do_rx_cur = 1;
1046 }
1047 } else {
1048 /* Rx aggr not in progress */
1049 f_aggr_cur = 1;
1050 }
1051
1052 } else {
1053 /* No more data RX pending */
1054 dev_dbg(adapter->dev, "info: %s: last packet\n", __func__);
1055
1056 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1057 f_do_rx_aggr = 1;
1058 if (MP_RX_AGGR_BUF_HAS_ROOM(card, skb->len))
1059 f_aggr_cur = 1;
1060 else
1061 /* No room in Aggr buf, do rx aggr now */
1062 f_do_rx_cur = 1;
1063 } else {
1064 f_do_rx_cur = 1;
1065 }
1066 }
1067
1068 if (f_aggr_cur) {
1069 dev_dbg(adapter->dev, "info: current packet aggregation\n");
1070 /* Curr pkt can be aggregated */
1071 MP_RX_AGGR_SETUP(card, skb, port);
1072
1073 if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
1074 MP_RX_AGGR_PORT_LIMIT_REACHED(card)) {
1075 dev_dbg(adapter->dev, "info: %s: aggregated packet "
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001076 "limit reached\n", __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001077 /* No more pkts allowed in Aggr buf, rx it */
1078 f_do_rx_aggr = 1;
1079 }
1080 }
1081
1082 if (f_do_rx_aggr) {
1083 /* do aggr RX now */
1084 dev_dbg(adapter->dev, "info: do_rx_aggr: num of packets: %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001085 card->mpa_rx.pkt_cnt);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001086
1087 if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
1088 card->mpa_rx.buf_len,
1089 (adapter->ioport | 0x1000 |
1090 (card->mpa_rx.ports << 4)) +
Amitkumar Karwar572e8f32011-04-13 17:27:08 -07001091 card->mpa_rx.start_port, 1))
Avinash Patil17a60b42011-12-08 20:41:04 -08001092 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001093
1094 curr_ptr = card->mpa_rx.buf;
1095
1096 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1097
1098 /* get curr PKT len & type */
1099 pkt_len = *(u16 *) &curr_ptr[0];
1100 pkt_type = *(u16 *) &curr_ptr[2];
1101
1102 /* copy pkt to deaggr buf */
1103 skb_deaggr = card->mpa_rx.skb_arr[pind];
1104
1105 if ((pkt_type == MWIFIEX_TYPE_DATA) && (pkt_len <=
1106 card->mpa_rx.len_arr[pind])) {
1107
1108 memcpy(skb_deaggr->data, curr_ptr, pkt_len);
1109
1110 skb_trim(skb_deaggr, pkt_len);
1111
1112 /* Process de-aggr packet */
1113 mwifiex_decode_rx_packet(adapter, skb_deaggr,
1114 pkt_type);
1115 } else {
1116 dev_err(adapter->dev, "wrong aggr pkt:"
1117 " type=%d len=%d max_len=%d\n",
1118 pkt_type, pkt_len,
1119 card->mpa_rx.len_arr[pind]);
1120 dev_kfree_skb_any(skb_deaggr);
1121 }
1122 curr_ptr += card->mpa_rx.len_arr[pind];
1123 }
1124 MP_RX_AGGR_BUF_RESET(card);
1125 }
1126
1127rx_curr_single:
1128 if (f_do_rx_cur) {
1129 dev_dbg(adapter->dev, "info: RX: port: %d, rx_len: %d\n",
1130 port, rx_len);
1131
1132 if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
1133 skb->data, skb->len,
1134 adapter->ioport + port))
Avinash Patil17a60b42011-12-08 20:41:04 -08001135 goto error;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001136
1137 mwifiex_decode_rx_packet(adapter, skb, pkt_type);
1138 }
1139
1140 return 0;
Avinash Patil17a60b42011-12-08 20:41:04 -08001141
1142error:
1143 if (MP_RX_AGGR_IN_PROGRESS(card)) {
1144 /* Multiport-aggregation transfer failed - cleanup */
1145 for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
1146 /* copy pkt to deaggr buf */
1147 skb_deaggr = card->mpa_rx.skb_arr[pind];
1148 dev_kfree_skb_any(skb_deaggr);
1149 }
1150 MP_RX_AGGR_BUF_RESET(card);
1151 }
1152
1153 if (f_do_rx_cur)
1154 /* Single transfer pending. Free curr buff also */
1155 dev_kfree_skb_any(skb);
1156
1157 return -1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001158}
1159
1160/*
1161 * This function checks the current interrupt status.
1162 *
1163 * The following interrupts are checked and handled by this function -
1164 * - Data sent
1165 * - Command sent
1166 * - Packets received
1167 *
1168 * Since the firmware does not generate download ready interrupt if the
1169 * port updated is command port only, command sent interrupt checking
1170 * should be done manually, and for every SDIO interrupt.
1171 *
1172 * In case of Rx packets received, the packets are uploaded from card to
1173 * host and processed accordingly.
1174 */
1175static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1176{
1177 struct sdio_mmc_card *card = adapter->card;
1178 int ret = 0;
1179 u8 sdio_ireg;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001180 struct sk_buff *skb;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001181 u8 port = CTRL_PORT;
1182 u32 len_reg_l, len_reg_u;
1183 u32 rx_blocks;
1184 u16 rx_len;
1185 unsigned long flags;
1186
1187 spin_lock_irqsave(&adapter->int_lock, flags);
1188 sdio_ireg = adapter->int_status;
1189 adapter->int_status = 0;
1190 spin_unlock_irqrestore(&adapter->int_lock, flags);
1191
1192 if (!sdio_ireg)
1193 return ret;
1194
1195 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
1196 card->mp_wr_bitmap = ((u16) card->mp_regs[WR_BITMAP_U]) << 8;
1197 card->mp_wr_bitmap |= (u16) card->mp_regs[WR_BITMAP_L];
1198 dev_dbg(adapter->dev, "int: DNLD: wr_bitmap=0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001199 card->mp_wr_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001200 if (adapter->data_sent &&
1201 (card->mp_wr_bitmap & card->mp_data_port_mask)) {
1202 dev_dbg(adapter->dev,
1203 "info: <--- Tx DONE Interrupt --->\n");
1204 adapter->data_sent = false;
1205 }
1206 }
1207
1208 /* As firmware will not generate download ready interrupt if the port
1209 updated is command port only, cmd_sent should be done for any SDIO
1210 interrupt. */
1211 if (adapter->cmd_sent) {
1212 /* Check if firmware has attach buffer at command port and
1213 update just that in wr_bit_map. */
1214 card->mp_wr_bitmap |=
1215 (u16) card->mp_regs[WR_BITMAP_L] & CTRL_PORT_MASK;
1216 if (card->mp_wr_bitmap & CTRL_PORT_MASK)
1217 adapter->cmd_sent = false;
1218 }
1219
1220 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001221 adapter->cmd_sent, adapter->data_sent);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001222 if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
1223 card->mp_rd_bitmap = ((u16) card->mp_regs[RD_BITMAP_U]) << 8;
1224 card->mp_rd_bitmap |= (u16) card->mp_regs[RD_BITMAP_L];
1225 dev_dbg(adapter->dev, "int: UPLD: rd_bitmap=0x%04x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001226 card->mp_rd_bitmap);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001227
1228 while (true) {
1229 ret = mwifiex_get_rd_port(adapter, &port);
1230 if (ret) {
1231 dev_dbg(adapter->dev,
1232 "info: no more rd_port available\n");
1233 break;
1234 }
1235 len_reg_l = RD_LEN_P0_L + (port << 1);
1236 len_reg_u = RD_LEN_P0_U + (port << 1);
1237 rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
1238 rx_len |= (u16) card->mp_regs[len_reg_l];
1239 dev_dbg(adapter->dev, "info: RX: port=%d rx_len=%u\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001240 port, rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001241 rx_blocks =
1242 (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
1243 1) / MWIFIEX_SDIO_BLOCK_SIZE;
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001244 if (rx_len <= INTF_HEADER_LEN ||
1245 (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
1246 MWIFIEX_RX_DATA_BUF_SIZE) {
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001247 dev_err(adapter->dev, "invalid rx_len=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001248 rx_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001249 return -1;
1250 }
1251 rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
1252
1253 skb = dev_alloc_skb(rx_len);
1254
1255 if (!skb) {
1256 dev_err(adapter->dev, "%s: failed to alloc skb",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001257 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001258 return -1;
1259 }
1260
1261 skb_put(skb, rx_len);
1262
1263 dev_dbg(adapter->dev, "info: rx_len = %d skb->len = %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001264 rx_len, skb->len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001265
1266 if (mwifiex_sdio_card_to_host_mp_aggr(adapter, skb,
1267 port)) {
1268 u32 cr = 0;
1269
1270 dev_err(adapter->dev, "card_to_host_mpa failed:"
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001271 " int status=%#x\n", sdio_ireg);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001272 if (mwifiex_read_reg(adapter,
1273 CONFIGURATION_REG, &cr))
1274 dev_err(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001275 "read CFG reg failed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001276
1277 dev_dbg(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001278 "info: CFG reg val = %d\n", cr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001279 if (mwifiex_write_reg(adapter,
1280 CONFIGURATION_REG,
1281 (cr | 0x04)))
1282 dev_err(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001283 "write CFG reg failed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001284
1285 dev_dbg(adapter->dev, "info: write success\n");
1286 if (mwifiex_read_reg(adapter,
1287 CONFIGURATION_REG, &cr))
1288 dev_err(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001289 "read CFG reg failed\n");
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001290
1291 dev_dbg(adapter->dev,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001292 "info: CFG reg val =%x\n", cr);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001293 return -1;
1294 }
1295 }
1296 }
1297
1298 return 0;
1299}
1300
1301/*
1302 * This function aggregates transmission buffers in driver and downloads
1303 * the aggregated packet to card.
1304 *
1305 * The individual packets are aggregated by copying into an aggregation
1306 * buffer and then downloaded to the card. Previous unsent packets in the
1307 * aggregation buffer are pre-copied first before new packets are added.
1308 * Aggregation is done till there is space left in the aggregation buffer,
1309 * or till new packets are available.
1310 *
1311 * The function will only download the packet to the card when aggregation
1312 * stops, otherwise it will just aggregate the packet in aggregation buffer
1313 * and return.
1314 */
1315static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
1316 u8 *payload, u32 pkt_len, u8 port,
1317 u32 next_pkt_len)
1318{
1319 struct sdio_mmc_card *card = adapter->card;
1320 int ret = 0;
1321 s32 f_send_aggr_buf = 0;
1322 s32 f_send_cur_buf = 0;
1323 s32 f_precopy_cur_buf = 0;
1324 s32 f_postcopy_cur_buf = 0;
1325
1326 if ((!card->mpa_tx.enabled) || (port == CTRL_PORT)) {
1327 dev_dbg(adapter->dev, "info: %s: tx aggregation disabled\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001328 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001329
1330 f_send_cur_buf = 1;
1331 goto tx_curr_single;
1332 }
1333
1334 if (next_pkt_len) {
1335 /* More pkt in TX queue */
1336 dev_dbg(adapter->dev, "info: %s: more packets in queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001337 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001338
1339 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1340 if (!MP_TX_AGGR_PORT_LIMIT_REACHED(card) &&
1341 MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
1342 f_precopy_cur_buf = 1;
1343
1344 if (!(card->mp_wr_bitmap &
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001345 (1 << card->curr_wr_port)) ||
1346 !MP_TX_AGGR_BUF_HAS_ROOM(
1347 card, pkt_len + next_pkt_len))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001348 f_send_aggr_buf = 1;
1349 } else {
1350 /* No room in Aggr buf, send it */
1351 f_send_aggr_buf = 1;
1352
1353 if (MP_TX_AGGR_PORT_LIMIT_REACHED(card) ||
1354 !(card->mp_wr_bitmap &
1355 (1 << card->curr_wr_port)))
1356 f_send_cur_buf = 1;
1357 else
1358 f_postcopy_cur_buf = 1;
1359 }
1360 } else {
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001361 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
1362 (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001363 f_precopy_cur_buf = 1;
1364 else
1365 f_send_cur_buf = 1;
1366 }
1367 } else {
1368 /* Last pkt in TX queue */
1369 dev_dbg(adapter->dev, "info: %s: Last packet in Tx Queue.\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001370 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001371
1372 if (MP_TX_AGGR_IN_PROGRESS(card)) {
1373 /* some packs in Aggr buf already */
1374 f_send_aggr_buf = 1;
1375
1376 if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
1377 f_precopy_cur_buf = 1;
1378 else
1379 /* No room in Aggr buf, send it */
1380 f_send_cur_buf = 1;
1381 } else {
1382 f_send_cur_buf = 1;
1383 }
1384 }
1385
1386 if (f_precopy_cur_buf) {
1387 dev_dbg(adapter->dev, "data: %s: precopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001388 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001389 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1390
1391 if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
1392 MP_TX_AGGR_PORT_LIMIT_REACHED(card))
1393 /* No more pkts allowed in Aggr buf, send it */
1394 f_send_aggr_buf = 1;
1395 }
1396
1397 if (f_send_aggr_buf) {
1398 dev_dbg(adapter->dev, "data: %s: send aggr buffer: %d %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001399 __func__,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001400 card->mpa_tx.start_port, card->mpa_tx.ports);
1401 ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
1402 card->mpa_tx.buf_len,
1403 (adapter->ioport | 0x1000 |
1404 (card->mpa_tx.ports << 4)) +
1405 card->mpa_tx.start_port);
1406
1407 MP_TX_AGGR_BUF_RESET(card);
1408 }
1409
1410tx_curr_single:
1411 if (f_send_cur_buf) {
1412 dev_dbg(adapter->dev, "data: %s: send current buffer %d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001413 __func__, port);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001414 ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
1415 adapter->ioport + port);
1416 }
1417
1418 if (f_postcopy_cur_buf) {
1419 dev_dbg(adapter->dev, "data: %s: postcopy current buffer\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001420 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001421 MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
1422 }
1423
1424 return ret;
1425}
1426
1427/*
1428 * This function downloads data from driver to card.
1429 *
1430 * Both commands and data packets are transferred to the card by this
1431 * function.
1432 *
1433 * This function adds the SDIO specific header to the front of the buffer
1434 * before transferring. The header contains the length of the packet and
1435 * the type. The firmware handles the packets based upon this set type.
1436 */
1437static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001438 u8 type, struct sk_buff *skb,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001439 struct mwifiex_tx_param *tx_param)
1440{
1441 struct sdio_mmc_card *card = adapter->card;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001442 int ret;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001443 u32 buf_block_len;
1444 u32 blk_size;
1445 u8 port = CTRL_PORT;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001446 u8 *payload = (u8 *)skb->data;
1447 u32 pkt_len = skb->len;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001448
1449 /* Allocate buffer and copy payload */
1450 blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
1451 buf_block_len = (pkt_len + blk_size - 1) / blk_size;
Tomasz Moń33f91a12013-07-23 07:42:49 +02001452 *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
1453 *(__le16 *)&payload[2] = cpu_to_le16(type);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001454
1455 /*
1456 * This is SDIO specific header
1457 * u16 length,
1458 * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
1459 * MWIFIEX_TYPE_EVENT = 3)
1460 */
1461 if (type == MWIFIEX_TYPE_DATA) {
1462 ret = mwifiex_get_wr_port_data(adapter, &port);
1463 if (ret) {
1464 dev_err(adapter->dev, "%s: no wr_port available\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001465 __func__);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001466 return ret;
1467 }
1468 } else {
1469 adapter->cmd_sent = true;
1470 /* Type must be MWIFIEX_TYPE_CMD */
1471
1472 if (pkt_len <= INTF_HEADER_LEN ||
1473 pkt_len > MWIFIEX_UPLD_SIZE)
1474 dev_err(adapter->dev, "%s: payload=%p, nb=%d\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001475 __func__, payload, pkt_len);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001476 }
1477
1478 /* Transfer data to card */
1479 pkt_len = buf_block_len * blk_size;
1480
1481 if (tx_param)
1482 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001483 port, tx_param->next_pkt_len
1484 );
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001485 else
1486 ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001487 port, 0);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001488
1489 if (ret) {
1490 if (type == MWIFIEX_TYPE_CMD)
1491 adapter->cmd_sent = false;
1492 if (type == MWIFIEX_TYPE_DATA)
1493 adapter->data_sent = false;
1494 } else {
1495 if (type == MWIFIEX_TYPE_DATA) {
1496 if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
1497 adapter->data_sent = true;
1498 else
1499 adapter->data_sent = false;
1500 }
1501 }
1502
1503 return ret;
1504}
1505
1506/*
1507 * This function allocates the MPA Tx and Rx buffers.
1508 */
1509static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
1510 u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
1511{
1512 struct sdio_mmc_card *card = adapter->card;
1513 int ret = 0;
1514
1515 card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
1516 if (!card->mpa_tx.buf) {
1517 dev_err(adapter->dev, "could not alloc buffer for MP-A TX\n");
1518 ret = -1;
1519 goto error;
1520 }
1521
1522 card->mpa_tx.buf_size = mpa_tx_buf_size;
1523
1524 card->mpa_rx.buf = kzalloc(mpa_rx_buf_size, GFP_KERNEL);
1525 if (!card->mpa_rx.buf) {
1526 dev_err(adapter->dev, "could not alloc buffer for MP-A RX\n");
1527 ret = -1;
1528 goto error;
1529 }
1530
1531 card->mpa_rx.buf_size = mpa_rx_buf_size;
1532
1533error:
1534 if (ret) {
1535 kfree(card->mpa_tx.buf);
1536 kfree(card->mpa_rx.buf);
1537 }
1538
1539 return ret;
1540}
1541
1542/*
1543 * This function unregisters the SDIO device.
1544 *
1545 * The SDIO IRQ is released, the function is disabled and driver
1546 * data is set to null.
1547 */
1548static void
1549mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1550{
1551 struct sdio_mmc_card *card = adapter->card;
1552
1553 if (adapter->card) {
1554 /* Release the SDIO IRQ */
1555 sdio_claim_host(card->func);
1556 sdio_release_irq(card->func);
1557 sdio_disable_func(card->func);
1558 sdio_release_host(card->func);
1559 sdio_set_drvdata(card->func, NULL);
1560 }
1561}
1562
1563/*
1564 * This function registers the SDIO device.
1565 *
1566 * SDIO IRQ is claimed, block size is set and driver data is initialized.
1567 */
1568static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1569{
1570 int ret = 0;
1571 struct sdio_mmc_card *card = adapter->card;
1572 struct sdio_func *func = card->func;
1573
1574 /* save adapter pointer in card */
1575 card->adapter = adapter;
1576
1577 sdio_claim_host(func);
1578
1579 /* Request the SDIO IRQ */
1580 ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
1581 if (ret) {
1582 pr_err("claim irq failed: ret=%d\n", ret);
1583 goto disable_func;
1584 }
1585
1586 /* Set block size */
1587 ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
1588 if (ret) {
1589 pr_err("cannot set SDIO block size\n");
1590 ret = -1;
1591 goto release_irq;
1592 }
1593
1594 sdio_release_host(func);
1595 sdio_set_drvdata(func, card);
1596
1597 adapter->dev = &func->dev;
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001598
1599 switch (func->device) {
1600 case SDIO_DEVICE_ID_MARVELL_8797:
1601 strcpy(adapter->fw_name, SD8797_DEFAULT_FW_NAME);
1602 break;
1603 case SDIO_DEVICE_ID_MARVELL_8787:
1604 default:
1605 strcpy(adapter->fw_name, SD8787_DEFAULT_FW_NAME);
1606 break;
1607 }
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001608
1609 return 0;
1610
1611release_irq:
1612 sdio_release_irq(func);
1613disable_func:
1614 sdio_disable_func(func);
1615 sdio_release_host(func);
1616 adapter->card = NULL;
1617
1618 return -1;
1619}
1620
1621/*
1622 * This function initializes the SDIO driver.
1623 *
1624 * The following initializations steps are followed -
1625 * - Read the Host interrupt status register to acknowledge
1626 * the first interrupt got from bootloader
1627 * - Disable host interrupt mask register
1628 * - Get SDIO port
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001629 * - Initialize SDIO variables in card
1630 * - Allocate MP registers
1631 * - Allocate MPA Tx and Rx buffers
1632 */
1633static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
1634{
1635 struct sdio_mmc_card *card = adapter->card;
1636 int ret;
Yogesh Ashok Powar270e58e2011-05-03 20:11:46 -07001637 u32 sdio_ireg;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001638
1639 /*
1640 * Read the HOST_INT_STATUS_REG for ACK the first interrupt got
1641 * from the bootloader. If we don't do this we get a interrupt
1642 * as soon as we register the irq.
1643 */
1644 mwifiex_read_reg(adapter, HOST_INTSTATUS_REG, &sdio_ireg);
1645
1646 /* Disable host interrupt mask register for SDIO */
1647 mwifiex_sdio_disable_host_int(adapter);
1648
1649 /* Get SDIO ioport */
1650 mwifiex_init_sdio_ioport(adapter);
1651
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001652 /* Initialize SDIO variables in card */
1653 card->mp_rd_bitmap = 0;
1654 card->mp_wr_bitmap = 0;
1655 card->curr_rd_port = 1;
1656 card->curr_wr_port = 1;
1657
1658 card->mp_data_port_mask = DATA_PORT_MASK;
1659
1660 card->mpa_tx.buf_len = 0;
1661 card->mpa_tx.pkt_cnt = 0;
1662 card->mpa_tx.start_port = 0;
1663
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001664 card->mpa_tx.enabled = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001665 card->mpa_tx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1666
1667 card->mpa_rx.buf_len = 0;
1668 card->mpa_rx.pkt_cnt = 0;
1669 card->mpa_rx.start_port = 0;
1670
Amitkumar Karwar7d7ab022011-11-07 19:31:46 -08001671 card->mpa_rx.enabled = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001672 card->mpa_rx.pkt_aggr_limit = SDIO_MP_AGGR_DEF_PKT_LIMIT;
1673
1674 /* Allocate buffers for SDIO MP-A */
1675 card->mp_regs = kzalloc(MAX_MP_REGS, GFP_KERNEL);
1676 if (!card->mp_regs) {
1677 dev_err(adapter->dev, "failed to alloc mp_regs\n");
Christoph Fritzb53575e2011-05-08 22:50:09 +02001678 return -ENOMEM;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001679 }
1680
1681 ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
1682 SDIO_MP_TX_AGGR_DEF_BUF_SIZE,
1683 SDIO_MP_RX_AGGR_DEF_BUF_SIZE);
1684 if (ret) {
1685 dev_err(adapter->dev, "failed to alloc sdio mp-a buffers\n");
1686 kfree(card->mp_regs);
1687 return -1;
1688 }
1689
1690 return ret;
1691}
1692
1693/*
1694 * This function resets the MPA Tx and Rx buffers.
1695 */
1696static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
1697{
1698 struct sdio_mmc_card *card = adapter->card;
1699
1700 MP_TX_AGGR_BUF_RESET(card);
1701 MP_RX_AGGR_BUF_RESET(card);
1702}
1703
1704/*
1705 * This function cleans up the allocated card buffers.
1706 *
1707 * The following are freed by this function -
1708 * - MP registers
1709 * - MPA Tx buffer
1710 * - MPA Rx buffer
1711 */
1712static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
1713{
1714 struct sdio_mmc_card *card = adapter->card;
1715
1716 kfree(card->mp_regs);
1717 kfree(card->mpa_tx.buf);
1718 kfree(card->mpa_rx.buf);
1719}
1720
1721/*
1722 * This function updates the MP end port in card.
1723 */
1724static void
1725mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
1726{
1727 struct sdio_mmc_card *card = adapter->card;
1728 int i;
1729
1730 card->mp_end_port = port;
1731
1732 card->mp_data_port_mask = DATA_PORT_MASK;
1733
1734 for (i = 1; i <= MAX_PORT - card->mp_end_port; i++)
1735 card->mp_data_port_mask &= ~(1 << (MAX_PORT - i));
1736
1737 card->curr_wr_port = 1;
1738
1739 dev_dbg(adapter->dev, "cmd: mp_end_port %d, data port mask 0x%x\n",
Yogesh Ashok Powar5dbd3262012-03-13 19:22:39 -07001740 port, card->mp_data_port_mask);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001741}
1742
1743static struct mwifiex_if_ops sdio_ops = {
1744 .init_if = mwifiex_init_sdio,
1745 .cleanup_if = mwifiex_cleanup_sdio,
1746 .check_fw_status = mwifiex_check_fw_status,
1747 .prog_fw = mwifiex_prog_fw_w_helper,
1748 .register_dev = mwifiex_register_dev,
1749 .unregister_dev = mwifiex_unregister_dev,
1750 .enable_int = mwifiex_sdio_enable_host_int,
1751 .process_int_status = mwifiex_process_int_status,
1752 .host_to_card = mwifiex_sdio_host_to_card,
1753 .wakeup = mwifiex_pm_wakeup_card,
1754 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1755
1756 /* SDIO specific */
1757 .update_mp_end_port = mwifiex_update_mp_end_port,
1758 .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001759 .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
1760 .event_complete = mwifiex_sdio_event_complete,
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001761};
1762
1763/*
1764 * This function initializes the SDIO driver.
1765 *
1766 * This initiates the semaphore and registers the device with
1767 * SDIO bus.
1768 */
1769static int
1770mwifiex_sdio_init_module(void)
1771{
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001772 sema_init(&add_remove_card_sem, 1);
1773
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301774 /* Clear the flag in case user removes the card. */
1775 user_rmmod = 0;
1776
Yogesh Ashok Powar636c4592011-04-15 20:50:40 -07001777 return sdio_register_driver(&mwifiex_sdio);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001778}
1779
1780/*
1781 * This function cleans up the SDIO driver.
1782 *
1783 * The following major steps are followed for cleanup -
1784 * - Resume the device if its suspended
1785 * - Disconnect the device if connected
1786 * - Shutdown the firmware
1787 * - Unregister the device from SDIO bus.
1788 */
1789static void
1790mwifiex_sdio_cleanup_module(void)
1791{
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301792 if (!down_interruptible(&add_remove_card_sem))
1793 up(&add_remove_card_sem);
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001794
Amitkumar Karwar287546d2011-06-08 20:39:20 +05301795 /* Set the flag as user is removing this module. */
1796 user_rmmod = 1;
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001797
Bing Zhao5e6e3a92011-03-21 18:00:50 -07001798 sdio_unregister_driver(&mwifiex_sdio);
1799}
1800
1801module_init(mwifiex_sdio_init_module);
1802module_exit(mwifiex_sdio_cleanup_module);
1803
1804MODULE_AUTHOR("Marvell International Ltd.");
1805MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
1806MODULE_VERSION(SDIO_VERSION);
1807MODULE_LICENSE("GPL v2");
Bing Zhaoe3bea1c2011-11-16 20:40:35 -08001808MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
1809MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);