Version:  2.0.40 2.2.26 2.4.37 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18

Linux/drivers/net/wireless/mwifiex/sdio.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us