Version:  2.0.40 2.2.26 2.4.37 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 3.19 4.0

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

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