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

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

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

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