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

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

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