GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / net / wireless / marvell / mwifiex / pcie.c
1 /*
2  * Marvell Wireless LAN device driver: PCIE 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 "pcie.h"
30
31 #define PCIE_VERSION    "1.0"
32 #define DRV_NAME        "Marvell mwifiex PCIe"
33
34 static struct mwifiex_if_ops pcie_ops;
35
36 static const struct of_device_id mwifiex_pcie_of_match_table[] = {
37         { .compatible = "pci11ab,2b42" },
38         { .compatible = "pci1b4b,2b42" },
39         { }
40 };
41
42 static int mwifiex_pcie_probe_of(struct device *dev)
43 {
44         if (!of_match_node(mwifiex_pcie_of_match_table, dev->of_node)) {
45                 dev_err(dev, "required compatible string missing\n");
46                 return -EINVAL;
47         }
48
49         return 0;
50 }
51
52 static void mwifiex_pcie_work(struct work_struct *work);
53
54 static int
55 mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb,
56                        size_t size, int flags)
57 {
58         struct pcie_service_card *card = adapter->card;
59         struct mwifiex_dma_mapping mapping;
60
61         mapping.addr = pci_map_single(card->dev, skb->data, size, flags);
62         if (pci_dma_mapping_error(card->dev, mapping.addr)) {
63                 mwifiex_dbg(adapter, ERROR, "failed to map pci memory!\n");
64                 return -1;
65         }
66         mapping.len = size;
67         mwifiex_store_mapping(skb, &mapping);
68         return 0;
69 }
70
71 static void mwifiex_unmap_pci_memory(struct mwifiex_adapter *adapter,
72                                      struct sk_buff *skb, int flags)
73 {
74         struct pcie_service_card *card = adapter->card;
75         struct mwifiex_dma_mapping mapping;
76
77         mwifiex_get_mapping(skb, &mapping);
78         pci_unmap_single(card->dev, mapping.addr, mapping.len, flags);
79 }
80
81 /*
82  * This function writes data into PCIE card register.
83  */
84 static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
85 {
86         struct pcie_service_card *card = adapter->card;
87
88         iowrite32(data, card->pci_mmap1 + reg);
89
90         return 0;
91 }
92
93 /* This function reads data from PCIE card register.
94  */
95 static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
96 {
97         struct pcie_service_card *card = adapter->card;
98
99         *data = ioread32(card->pci_mmap1 + reg);
100         if (*data == 0xffffffff)
101                 return 0xffffffff;
102
103         return 0;
104 }
105
106 /* This function reads u8 data from PCIE card register. */
107 static int mwifiex_read_reg_byte(struct mwifiex_adapter *adapter,
108                                  int reg, u8 *data)
109 {
110         struct pcie_service_card *card = adapter->card;
111
112         *data = ioread8(card->pci_mmap1 + reg);
113
114         return 0;
115 }
116
117 /*
118  * This function reads sleep cookie and checks if FW is ready
119  */
120 static bool mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter *adapter)
121 {
122         u32 cookie_value;
123         struct pcie_service_card *card = adapter->card;
124         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
125
126         if (!reg->sleep_cookie)
127                 return true;
128
129         if (card->sleep_cookie_vbase) {
130                 cookie_value = get_unaligned_le32(card->sleep_cookie_vbase);
131                 mwifiex_dbg(adapter, INFO,
132                             "info: ACCESS_HW: sleep cookie=0x%x\n",
133                             cookie_value);
134                 if (cookie_value == FW_AWAKE_COOKIE)
135                         return true;
136         }
137
138         return false;
139 }
140
141 #ifdef CONFIG_PM_SLEEP
142 /*
143  * Kernel needs to suspend all functions separately. Therefore all
144  * registered functions must have drivers with suspend and resume
145  * methods. Failing that the kernel simply removes the whole card.
146  *
147  * If already not suspended, this function allocates and sends a host
148  * sleep activate request to the firmware and turns off the traffic.
149  */
150 static int mwifiex_pcie_suspend(struct device *dev)
151 {
152         struct mwifiex_adapter *adapter;
153         struct pcie_service_card *card;
154         struct pci_dev *pdev = to_pci_dev(dev);
155
156         card = pci_get_drvdata(pdev);
157
158         /* Might still be loading firmware */
159         wait_for_completion(&card->fw_done);
160
161         adapter = card->adapter;
162         if (!adapter) {
163                 dev_err(dev, "adapter is not valid\n");
164                 return 0;
165         }
166
167         mwifiex_enable_wake(adapter);
168
169         /* Enable the Host Sleep */
170         if (!mwifiex_enable_hs(adapter)) {
171                 mwifiex_dbg(adapter, ERROR,
172                             "cmd: failed to suspend\n");
173                 clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
174                 mwifiex_disable_wake(adapter);
175                 return -EFAULT;
176         }
177
178         flush_workqueue(adapter->workqueue);
179
180         /* Indicate device suspended */
181         set_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
182         clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
183
184         return 0;
185 }
186
187 /*
188  * Kernel needs to suspend all functions separately. Therefore all
189  * registered functions must have drivers with suspend and resume
190  * methods. Failing that the kernel simply removes the whole card.
191  *
192  * If already not resumed, this function turns on the traffic and
193  * sends a host sleep cancel request to the firmware.
194  */
195 static int mwifiex_pcie_resume(struct device *dev)
196 {
197         struct mwifiex_adapter *adapter;
198         struct pcie_service_card *card;
199         struct pci_dev *pdev = to_pci_dev(dev);
200
201         card = pci_get_drvdata(pdev);
202
203         if (!card->adapter) {
204                 dev_err(dev, "adapter structure is not valid\n");
205                 return 0;
206         }
207
208         adapter = card->adapter;
209
210         if (!test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
211                 mwifiex_dbg(adapter, WARN,
212                             "Device already resumed\n");
213                 return 0;
214         }
215
216         clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
217
218         mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
219                           MWIFIEX_ASYNC_CMD);
220         mwifiex_disable_wake(adapter);
221
222         return 0;
223 }
224 #endif
225
226 /*
227  * This function probes an mwifiex device and registers it. It allocates
228  * the card structure, enables PCIE function number and initiates the
229  * device registration and initialization procedure by adding a logical
230  * interface.
231  */
232 static int mwifiex_pcie_probe(struct pci_dev *pdev,
233                                         const struct pci_device_id *ent)
234 {
235         struct pcie_service_card *card;
236         int ret;
237
238         pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
239                  pdev->vendor, pdev->device, pdev->revision);
240
241         card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
242         if (!card)
243                 return -ENOMEM;
244
245         init_completion(&card->fw_done);
246
247         card->dev = pdev;
248
249         if (ent->driver_data) {
250                 struct mwifiex_pcie_device *data = (void *)ent->driver_data;
251                 card->pcie.reg = data->reg;
252                 card->pcie.blksz_fw_dl = data->blksz_fw_dl;
253                 card->pcie.tx_buf_size = data->tx_buf_size;
254                 card->pcie.can_dump_fw = data->can_dump_fw;
255                 card->pcie.mem_type_mapping_tbl = data->mem_type_mapping_tbl;
256                 card->pcie.num_mem_types = data->num_mem_types;
257                 card->pcie.can_ext_scan = data->can_ext_scan;
258                 INIT_WORK(&card->work, mwifiex_pcie_work);
259         }
260
261         /* device tree node parsing and platform specific configuration*/
262         if (pdev->dev.of_node) {
263                 ret = mwifiex_pcie_probe_of(&pdev->dev);
264                 if (ret)
265                         return ret;
266         }
267
268         if (mwifiex_add_card(card, &card->fw_done, &pcie_ops,
269                              MWIFIEX_PCIE, &pdev->dev)) {
270                 pr_err("%s failed\n", __func__);
271                 return -1;
272         }
273
274         return 0;
275 }
276
277 /*
278  * This function removes the interface and frees up the card structure.
279  */
280 static void mwifiex_pcie_remove(struct pci_dev *pdev)
281 {
282         struct pcie_service_card *card;
283         struct mwifiex_adapter *adapter;
284         struct mwifiex_private *priv;
285         const struct mwifiex_pcie_card_reg *reg;
286         u32 fw_status;
287         int ret;
288
289         card = pci_get_drvdata(pdev);
290
291         wait_for_completion(&card->fw_done);
292
293         adapter = card->adapter;
294         if (!adapter || !adapter->priv_num)
295                 return;
296
297         reg = card->pcie.reg;
298         if (reg)
299                 ret = mwifiex_read_reg(adapter, reg->fw_status, &fw_status);
300         else
301                 fw_status = -1;
302
303         if (fw_status == FIRMWARE_READY_PCIE && !adapter->mfg_mode) {
304                 mwifiex_deauthenticate_all(adapter);
305
306                 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
307
308                 mwifiex_disable_auto_ds(priv);
309
310                 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
311         }
312
313         mwifiex_remove_card(adapter);
314 }
315
316 static void mwifiex_pcie_shutdown(struct pci_dev *pdev)
317 {
318         mwifiex_pcie_remove(pdev);
319
320         return;
321 }
322
323 static void mwifiex_pcie_coredump(struct device *dev)
324 {
325         struct pci_dev *pdev;
326         struct pcie_service_card *card;
327
328         pdev = container_of(dev, struct pci_dev, dev);
329         card = pci_get_drvdata(pdev);
330
331         if (!test_and_set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
332                               &card->work_flags))
333                 schedule_work(&card->work);
334 }
335
336 static const struct pci_device_id mwifiex_ids[] = {
337         {
338                 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
339                 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
340                 .driver_data = (unsigned long)&mwifiex_pcie8766,
341         },
342         {
343                 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8897,
344                 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
345                 .driver_data = (unsigned long)&mwifiex_pcie8897,
346         },
347         {
348                 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8997,
349                 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
350                 .driver_data = (unsigned long)&mwifiex_pcie8997,
351         },
352         {
353                 PCIE_VENDOR_ID_V2_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8997,
354                 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
355                 .driver_data = (unsigned long)&mwifiex_pcie8997,
356         },
357         {},
358 };
359
360 MODULE_DEVICE_TABLE(pci, mwifiex_ids);
361
362 /*
363  * Cleanup all software without cleaning anything related to PCIe and HW.
364  */
365 static void mwifiex_pcie_reset_prepare(struct pci_dev *pdev)
366 {
367         struct pcie_service_card *card = pci_get_drvdata(pdev);
368         struct mwifiex_adapter *adapter = card->adapter;
369
370         if (!adapter) {
371                 dev_err(&pdev->dev, "%s: adapter structure is not valid\n",
372                         __func__);
373                 return;
374         }
375
376         mwifiex_dbg(adapter, INFO,
377                     "%s: vendor=0x%4.04x device=0x%4.04x rev=%d Pre-FLR\n",
378                     __func__, pdev->vendor, pdev->device, pdev->revision);
379
380         mwifiex_shutdown_sw(adapter);
381         clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &card->work_flags);
382         clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags);
383         mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
384
385         card->pci_reset_ongoing = true;
386 }
387
388 /*
389  * Kernel stores and restores PCIe function context before and after performing
390  * FLR respectively. Reconfigure the software and firmware including firmware
391  * redownload.
392  */
393 static void mwifiex_pcie_reset_done(struct pci_dev *pdev)
394 {
395         struct pcie_service_card *card = pci_get_drvdata(pdev);
396         struct mwifiex_adapter *adapter = card->adapter;
397         int ret;
398
399         if (!adapter) {
400                 dev_err(&pdev->dev, "%s: adapter structure is not valid\n",
401                         __func__);
402                 return;
403         }
404
405         mwifiex_dbg(adapter, INFO,
406                     "%s: vendor=0x%4.04x device=0x%4.04x rev=%d Post-FLR\n",
407                     __func__, pdev->vendor, pdev->device, pdev->revision);
408
409         ret = mwifiex_reinit_sw(adapter);
410         if (ret)
411                 dev_err(&pdev->dev, "reinit failed: %d\n", ret);
412         else
413                 mwifiex_dbg(adapter, INFO, "%s, successful\n", __func__);
414
415         card->pci_reset_ongoing = false;
416 }
417
418 static const struct pci_error_handlers mwifiex_pcie_err_handler = {
419         .reset_prepare          = mwifiex_pcie_reset_prepare,
420         .reset_done             = mwifiex_pcie_reset_done,
421 };
422
423 #ifdef CONFIG_PM_SLEEP
424 /* Power Management Hooks */
425 static SIMPLE_DEV_PM_OPS(mwifiex_pcie_pm_ops, mwifiex_pcie_suspend,
426                                 mwifiex_pcie_resume);
427 #endif
428
429 /* PCI Device Driver */
430 static struct pci_driver __refdata mwifiex_pcie = {
431         .name     = "mwifiex_pcie",
432         .id_table = mwifiex_ids,
433         .probe    = mwifiex_pcie_probe,
434         .remove   = mwifiex_pcie_remove,
435         .driver   = {
436                 .coredump = mwifiex_pcie_coredump,
437 #ifdef CONFIG_PM_SLEEP
438                 .pm = &mwifiex_pcie_pm_ops,
439 #endif
440         },
441         .shutdown = mwifiex_pcie_shutdown,
442         .err_handler = &mwifiex_pcie_err_handler,
443 };
444
445 /*
446  * This function adds delay loop to ensure FW is awake before proceeding.
447  */
448 static void mwifiex_pcie_dev_wakeup_delay(struct mwifiex_adapter *adapter)
449 {
450         int i = 0;
451
452         while (mwifiex_pcie_ok_to_access_hw(adapter)) {
453                 i++;
454                 usleep_range(10, 20);
455                 /* 50ms max wait */
456                 if (i == 5000)
457                         break;
458         }
459
460         return;
461 }
462
463 static void mwifiex_delay_for_sleep_cookie(struct mwifiex_adapter *adapter,
464                                            u32 max_delay_loop_cnt)
465 {
466         struct pcie_service_card *card = adapter->card;
467         u8 *buffer;
468         u32 sleep_cookie, count;
469         struct sk_buff *cmdrsp = card->cmdrsp_buf;
470
471         for (count = 0; count < max_delay_loop_cnt; count++) {
472                 pci_dma_sync_single_for_cpu(card->dev,
473                                             MWIFIEX_SKB_DMA_ADDR(cmdrsp),
474                                             sizeof(sleep_cookie),
475                                             PCI_DMA_FROMDEVICE);
476                 buffer = cmdrsp->data;
477                 sleep_cookie = get_unaligned_le32(buffer);
478
479                 if (sleep_cookie == MWIFIEX_DEF_SLEEP_COOKIE) {
480                         mwifiex_dbg(adapter, INFO,
481                                     "sleep cookie found at count %d\n", count);
482                         break;
483                 }
484                 pci_dma_sync_single_for_device(card->dev,
485                                                MWIFIEX_SKB_DMA_ADDR(cmdrsp),
486                                                sizeof(sleep_cookie),
487                                                PCI_DMA_FROMDEVICE);
488                 usleep_range(20, 30);
489         }
490
491         if (count >= max_delay_loop_cnt)
492                 mwifiex_dbg(adapter, INFO,
493                             "max count reached while accessing sleep cookie\n");
494 }
495
496 /* This function wakes up the card by reading fw_status register. */
497 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
498 {
499         struct pcie_service_card *card = adapter->card;
500         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
501
502         mwifiex_dbg(adapter, EVENT,
503                     "event: Wakeup device...\n");
504
505         if (reg->sleep_cookie)
506                 mwifiex_pcie_dev_wakeup_delay(adapter);
507
508         /* Accessing fw_status register will wakeup device */
509         if (mwifiex_write_reg(adapter, reg->fw_status, FIRMWARE_READY_PCIE)) {
510                 mwifiex_dbg(adapter, ERROR,
511                             "Writing fw_status register failed\n");
512                 return -1;
513         }
514
515         if (reg->sleep_cookie) {
516                 mwifiex_pcie_dev_wakeup_delay(adapter);
517                 mwifiex_dbg(adapter, INFO,
518                             "PCIE wakeup: Setting PS_STATE_AWAKE\n");
519                 adapter->ps_state = PS_STATE_AWAKE;
520         }
521
522         return 0;
523 }
524
525 /*
526  * This function is called after the card has woken up.
527  *
528  * The card configuration register is reset.
529  */
530 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
531 {
532         mwifiex_dbg(adapter, CMD,
533                     "cmd: Wakeup device completed\n");
534
535         return 0;
536 }
537
538 /*
539  * This function disables the host interrupt.
540  *
541  * The host interrupt mask is read, the disable bit is reset and
542  * written back to the card host interrupt mask register.
543  */
544 static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
545 {
546         if (mwifiex_pcie_ok_to_access_hw(adapter)) {
547                 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
548                                       0x00000000)) {
549                         mwifiex_dbg(adapter, ERROR,
550                                     "Disable host interrupt failed\n");
551                         return -1;
552                 }
553         }
554
555         atomic_set(&adapter->tx_hw_pending, 0);
556         return 0;
557 }
558
559 static void mwifiex_pcie_disable_host_int_noerr(struct mwifiex_adapter *adapter)
560 {
561         WARN_ON(mwifiex_pcie_disable_host_int(adapter));
562 }
563
564 /*
565  * This function enables the host interrupt.
566  *
567  * The host interrupt enable mask is written to the card
568  * host interrupt mask register.
569  */
570 static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
571 {
572         if (mwifiex_pcie_ok_to_access_hw(adapter)) {
573                 /* Simply write the mask to the register */
574                 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
575                                       HOST_INTR_MASK)) {
576                         mwifiex_dbg(adapter, ERROR,
577                                     "Enable host interrupt failed\n");
578                         return -1;
579                 }
580         }
581
582         return 0;
583 }
584
585 /*
586  * This function initializes TX buffer ring descriptors
587  */
588 static int mwifiex_init_txq_ring(struct mwifiex_adapter *adapter)
589 {
590         struct pcie_service_card *card = adapter->card;
591         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
592         struct mwifiex_pcie_buf_desc *desc;
593         struct mwifiex_pfu_buf_desc *desc2;
594         int i;
595
596         for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
597                 card->tx_buf_list[i] = NULL;
598                 if (reg->pfu_enabled) {
599                         card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
600                                              (sizeof(*desc2) * i);
601                         desc2 = card->txbd_ring[i];
602                         memset(desc2, 0, sizeof(*desc2));
603                 } else {
604                         card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
605                                              (sizeof(*desc) * i);
606                         desc = card->txbd_ring[i];
607                         memset(desc, 0, sizeof(*desc));
608                 }
609         }
610
611         return 0;
612 }
613
614 /* This function initializes RX buffer ring descriptors. Each SKB is allocated
615  * here and after mapping PCI memory, its physical address is assigned to
616  * PCIE Rx buffer descriptor's physical address.
617  */
618 static int mwifiex_init_rxq_ring(struct mwifiex_adapter *adapter)
619 {
620         struct pcie_service_card *card = adapter->card;
621         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
622         struct sk_buff *skb;
623         struct mwifiex_pcie_buf_desc *desc;
624         struct mwifiex_pfu_buf_desc *desc2;
625         dma_addr_t buf_pa;
626         int i;
627
628         for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
629                 /* Allocate skb here so that firmware can DMA data from it */
630                 skb = mwifiex_alloc_dma_align_buf(MWIFIEX_RX_DATA_BUF_SIZE,
631                                                   GFP_KERNEL);
632                 if (!skb) {
633                         mwifiex_dbg(adapter, ERROR,
634                                     "Unable to allocate skb for RX ring.\n");
635                         kfree(card->rxbd_ring_vbase);
636                         return -ENOMEM;
637                 }
638
639                 if (mwifiex_map_pci_memory(adapter, skb,
640                                            MWIFIEX_RX_DATA_BUF_SIZE,
641                                            PCI_DMA_FROMDEVICE))
642                         return -1;
643
644                 buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
645
646                 mwifiex_dbg(adapter, INFO,
647                             "info: RX ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
648                             skb, skb->len, skb->data, (u32)buf_pa,
649                             (u32)((u64)buf_pa >> 32));
650
651                 card->rx_buf_list[i] = skb;
652                 if (reg->pfu_enabled) {
653                         card->rxbd_ring[i] = (void *)card->rxbd_ring_vbase +
654                                              (sizeof(*desc2) * i);
655                         desc2 = card->rxbd_ring[i];
656                         desc2->paddr = buf_pa;
657                         desc2->len = (u16)skb->len;
658                         desc2->frag_len = (u16)skb->len;
659                         desc2->flags = reg->ring_flag_eop | reg->ring_flag_sop;
660                         desc2->offset = 0;
661                 } else {
662                         card->rxbd_ring[i] = (void *)(card->rxbd_ring_vbase +
663                                              (sizeof(*desc) * i));
664                         desc = card->rxbd_ring[i];
665                         desc->paddr = buf_pa;
666                         desc->len = (u16)skb->len;
667                         desc->flags = 0;
668                 }
669         }
670
671         return 0;
672 }
673
674 /* This function initializes event buffer ring descriptors. Each SKB is
675  * allocated here and after mapping PCI memory, its physical address is assigned
676  * to PCIE Rx buffer descriptor's physical address
677  */
678 static int mwifiex_pcie_init_evt_ring(struct mwifiex_adapter *adapter)
679 {
680         struct pcie_service_card *card = adapter->card;
681         struct mwifiex_evt_buf_desc *desc;
682         struct sk_buff *skb;
683         dma_addr_t buf_pa;
684         int i;
685
686         for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
687                 /* Allocate skb here so that firmware can DMA data from it */
688                 skb = dev_alloc_skb(MAX_EVENT_SIZE);
689                 if (!skb) {
690                         mwifiex_dbg(adapter, ERROR,
691                                     "Unable to allocate skb for EVENT buf.\n");
692                         kfree(card->evtbd_ring_vbase);
693                         return -ENOMEM;
694                 }
695                 skb_put(skb, MAX_EVENT_SIZE);
696
697                 if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE,
698                                            PCI_DMA_FROMDEVICE)) {
699                         kfree_skb(skb);
700                         kfree(card->evtbd_ring_vbase);
701                         return -1;
702                 }
703
704                 buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
705
706                 mwifiex_dbg(adapter, EVENT,
707                             "info: EVT ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
708                             skb, skb->len, skb->data, (u32)buf_pa,
709                             (u32)((u64)buf_pa >> 32));
710
711                 card->evt_buf_list[i] = skb;
712                 card->evtbd_ring[i] = (void *)(card->evtbd_ring_vbase +
713                                       (sizeof(*desc) * i));
714                 desc = card->evtbd_ring[i];
715                 desc->paddr = buf_pa;
716                 desc->len = (u16)skb->len;
717                 desc->flags = 0;
718         }
719
720         return 0;
721 }
722
723 /* This function cleans up TX buffer rings. If any of the buffer list has valid
724  * SKB address, associated SKB is freed.
725  */
726 static void mwifiex_cleanup_txq_ring(struct mwifiex_adapter *adapter)
727 {
728         struct pcie_service_card *card = adapter->card;
729         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
730         struct sk_buff *skb;
731         struct mwifiex_pcie_buf_desc *desc;
732         struct mwifiex_pfu_buf_desc *desc2;
733         int i;
734
735         for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
736                 if (reg->pfu_enabled) {
737                         desc2 = card->txbd_ring[i];
738                         if (card->tx_buf_list[i]) {
739                                 skb = card->tx_buf_list[i];
740                                 mwifiex_unmap_pci_memory(adapter, skb,
741                                                          PCI_DMA_TODEVICE);
742                                 dev_kfree_skb_any(skb);
743                         }
744                         memset(desc2, 0, sizeof(*desc2));
745                 } else {
746                         desc = card->txbd_ring[i];
747                         if (card->tx_buf_list[i]) {
748                                 skb = card->tx_buf_list[i];
749                                 mwifiex_unmap_pci_memory(adapter, skb,
750                                                          PCI_DMA_TODEVICE);
751                                 dev_kfree_skb_any(skb);
752                         }
753                         memset(desc, 0, sizeof(*desc));
754                 }
755                 card->tx_buf_list[i] = NULL;
756         }
757
758         atomic_set(&adapter->tx_hw_pending, 0);
759         return;
760 }
761
762 /* This function cleans up RX buffer rings. If any of the buffer list has valid
763  * SKB address, associated SKB is freed.
764  */
765 static void mwifiex_cleanup_rxq_ring(struct mwifiex_adapter *adapter)
766 {
767         struct pcie_service_card *card = adapter->card;
768         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
769         struct mwifiex_pcie_buf_desc *desc;
770         struct mwifiex_pfu_buf_desc *desc2;
771         struct sk_buff *skb;
772         int i;
773
774         for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
775                 if (reg->pfu_enabled) {
776                         desc2 = card->rxbd_ring[i];
777                         if (card->rx_buf_list[i]) {
778                                 skb = card->rx_buf_list[i];
779                                 mwifiex_unmap_pci_memory(adapter, skb,
780                                                          PCI_DMA_FROMDEVICE);
781                                 dev_kfree_skb_any(skb);
782                         }
783                         memset(desc2, 0, sizeof(*desc2));
784                 } else {
785                         desc = card->rxbd_ring[i];
786                         if (card->rx_buf_list[i]) {
787                                 skb = card->rx_buf_list[i];
788                                 mwifiex_unmap_pci_memory(adapter, skb,
789                                                          PCI_DMA_FROMDEVICE);
790                                 dev_kfree_skb_any(skb);
791                         }
792                         memset(desc, 0, sizeof(*desc));
793                 }
794                 card->rx_buf_list[i] = NULL;
795         }
796
797         return;
798 }
799
800 /* This function cleans up event buffer rings. If any of the buffer list has
801  * valid SKB address, associated SKB is freed.
802  */
803 static void mwifiex_cleanup_evt_ring(struct mwifiex_adapter *adapter)
804 {
805         struct pcie_service_card *card = adapter->card;
806         struct mwifiex_evt_buf_desc *desc;
807         struct sk_buff *skb;
808         int i;
809
810         for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
811                 desc = card->evtbd_ring[i];
812                 if (card->evt_buf_list[i]) {
813                         skb = card->evt_buf_list[i];
814                         mwifiex_unmap_pci_memory(adapter, skb,
815                                                  PCI_DMA_FROMDEVICE);
816                         dev_kfree_skb_any(skb);
817                 }
818                 card->evt_buf_list[i] = NULL;
819                 memset(desc, 0, sizeof(*desc));
820         }
821
822         return;
823 }
824
825 /* This function creates buffer descriptor ring for TX
826  */
827 static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
828 {
829         struct pcie_service_card *card = adapter->card;
830         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
831
832         /*
833          * driver maintaines the write pointer and firmware maintaines the read
834          * pointer. The write pointer starts at 0 (zero) while the read pointer
835          * starts at zero with rollover bit set
836          */
837         card->txbd_wrptr = 0;
838
839         if (reg->pfu_enabled)
840                 card->txbd_rdptr = 0;
841         else
842                 card->txbd_rdptr |= reg->tx_rollover_ind;
843
844         /* allocate shared memory for the BD ring and divide the same in to
845            several descriptors */
846         if (reg->pfu_enabled)
847                 card->txbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
848                                        MWIFIEX_MAX_TXRX_BD;
849         else
850                 card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
851                                        MWIFIEX_MAX_TXRX_BD;
852
853         mwifiex_dbg(adapter, INFO,
854                     "info: txbd_ring: Allocating %d bytes\n",
855                     card->txbd_ring_size);
856         card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
857                                                      card->txbd_ring_size,
858                                                      &card->txbd_ring_pbase);
859         if (!card->txbd_ring_vbase) {
860                 mwifiex_dbg(adapter, ERROR,
861                             "allocate consistent memory (%d bytes) failed!\n",
862                             card->txbd_ring_size);
863                 return -ENOMEM;
864         }
865         mwifiex_dbg(adapter, DATA,
866                     "info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
867                     card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
868                     (u32)((u64)card->txbd_ring_pbase >> 32),
869                     card->txbd_ring_size);
870
871         return mwifiex_init_txq_ring(adapter);
872 }
873
874 static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
875 {
876         struct pcie_service_card *card = adapter->card;
877         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
878
879         mwifiex_cleanup_txq_ring(adapter);
880
881         if (card->txbd_ring_vbase)
882                 pci_free_consistent(card->dev, card->txbd_ring_size,
883                                     card->txbd_ring_vbase,
884                                     card->txbd_ring_pbase);
885         card->txbd_ring_size = 0;
886         card->txbd_wrptr = 0;
887         card->txbd_rdptr = 0 | reg->tx_rollover_ind;
888         card->txbd_ring_vbase = NULL;
889         card->txbd_ring_pbase = 0;
890
891         return 0;
892 }
893
894 /*
895  * This function creates buffer descriptor ring for RX
896  */
897 static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
898 {
899         struct pcie_service_card *card = adapter->card;
900         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
901
902         /*
903          * driver maintaines the read pointer and firmware maintaines the write
904          * pointer. The write pointer starts at 0 (zero) while the read pointer
905          * starts at zero with rollover bit set
906          */
907         card->rxbd_wrptr = 0;
908         card->rxbd_rdptr = reg->rx_rollover_ind;
909
910         if (reg->pfu_enabled)
911                 card->rxbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
912                                        MWIFIEX_MAX_TXRX_BD;
913         else
914                 card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
915                                        MWIFIEX_MAX_TXRX_BD;
916
917         mwifiex_dbg(adapter, INFO,
918                     "info: rxbd_ring: Allocating %d bytes\n",
919                     card->rxbd_ring_size);
920         card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
921                                                      card->rxbd_ring_size,
922                                                      &card->rxbd_ring_pbase);
923         if (!card->rxbd_ring_vbase) {
924                 mwifiex_dbg(adapter, ERROR,
925                             "allocate consistent memory (%d bytes) failed!\n",
926                             card->rxbd_ring_size);
927                 return -ENOMEM;
928         }
929
930         mwifiex_dbg(adapter, DATA,
931                     "info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n",
932                     card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase,
933                     (u32)((u64)card->rxbd_ring_pbase >> 32),
934                     card->rxbd_ring_size);
935
936         return mwifiex_init_rxq_ring(adapter);
937 }
938
939 /*
940  * This function deletes Buffer descriptor ring for RX
941  */
942 static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
943 {
944         struct pcie_service_card *card = adapter->card;
945         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
946
947         mwifiex_cleanup_rxq_ring(adapter);
948
949         if (card->rxbd_ring_vbase)
950                 pci_free_consistent(card->dev, card->rxbd_ring_size,
951                                     card->rxbd_ring_vbase,
952                                     card->rxbd_ring_pbase);
953         card->rxbd_ring_size = 0;
954         card->rxbd_wrptr = 0;
955         card->rxbd_rdptr = 0 | reg->rx_rollover_ind;
956         card->rxbd_ring_vbase = NULL;
957         card->rxbd_ring_pbase = 0;
958
959         return 0;
960 }
961
962 /*
963  * This function creates buffer descriptor ring for Events
964  */
965 static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
966 {
967         struct pcie_service_card *card = adapter->card;
968         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
969
970         /*
971          * driver maintaines the read pointer and firmware maintaines the write
972          * pointer. The write pointer starts at 0 (zero) while the read pointer
973          * starts at zero with rollover bit set
974          */
975         card->evtbd_wrptr = 0;
976         card->evtbd_rdptr = reg->evt_rollover_ind;
977
978         card->evtbd_ring_size = sizeof(struct mwifiex_evt_buf_desc) *
979                                 MWIFIEX_MAX_EVT_BD;
980
981         mwifiex_dbg(adapter, INFO,
982                     "info: evtbd_ring: Allocating %d bytes\n",
983                 card->evtbd_ring_size);
984         card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
985                                                       card->evtbd_ring_size,
986                                                       &card->evtbd_ring_pbase);
987         if (!card->evtbd_ring_vbase) {
988                 mwifiex_dbg(adapter, ERROR,
989                             "allocate consistent memory (%d bytes) failed!\n",
990                             card->evtbd_ring_size);
991                 return -ENOMEM;
992         }
993
994         mwifiex_dbg(adapter, EVENT,
995                     "info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n",
996                     card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase,
997                     (u32)((u64)card->evtbd_ring_pbase >> 32),
998                     card->evtbd_ring_size);
999
1000         return mwifiex_pcie_init_evt_ring(adapter);
1001 }
1002
1003 /*
1004  * This function deletes Buffer descriptor ring for Events
1005  */
1006 static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
1007 {
1008         struct pcie_service_card *card = adapter->card;
1009         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1010
1011         mwifiex_cleanup_evt_ring(adapter);
1012
1013         if (card->evtbd_ring_vbase)
1014                 pci_free_consistent(card->dev, card->evtbd_ring_size,
1015                                     card->evtbd_ring_vbase,
1016                                     card->evtbd_ring_pbase);
1017         card->evtbd_wrptr = 0;
1018         card->evtbd_rdptr = 0 | reg->evt_rollover_ind;
1019         card->evtbd_ring_size = 0;
1020         card->evtbd_ring_vbase = NULL;
1021         card->evtbd_ring_pbase = 0;
1022
1023         return 0;
1024 }
1025
1026 /*
1027  * This function allocates a buffer for CMDRSP
1028  */
1029 static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
1030 {
1031         struct pcie_service_card *card = adapter->card;
1032         struct sk_buff *skb;
1033
1034         /* Allocate memory for receiving command response data */
1035         skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
1036         if (!skb) {
1037                 mwifiex_dbg(adapter, ERROR,
1038                             "Unable to allocate skb for command response data.\n");
1039                 return -ENOMEM;
1040         }
1041         skb_put(skb, MWIFIEX_UPLD_SIZE);
1042         if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1043                                    PCI_DMA_FROMDEVICE)) {
1044                 kfree_skb(skb);
1045                 return -1;
1046         }
1047
1048         card->cmdrsp_buf = skb;
1049
1050         return 0;
1051 }
1052
1053 /*
1054  * This function deletes a buffer for CMDRSP
1055  */
1056 static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
1057 {
1058         struct pcie_service_card *card;
1059
1060         if (!adapter)
1061                 return 0;
1062
1063         card = adapter->card;
1064
1065         if (card && card->cmdrsp_buf) {
1066                 mwifiex_unmap_pci_memory(adapter, card->cmdrsp_buf,
1067                                          PCI_DMA_FROMDEVICE);
1068                 dev_kfree_skb_any(card->cmdrsp_buf);
1069                 card->cmdrsp_buf = NULL;
1070         }
1071
1072         if (card && card->cmd_buf) {
1073                 mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
1074                                          PCI_DMA_TODEVICE);
1075                 dev_kfree_skb_any(card->cmd_buf);
1076                 card->cmd_buf = NULL;
1077         }
1078         return 0;
1079 }
1080
1081 /*
1082  * This function allocates a buffer for sleep cookie
1083  */
1084 static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
1085 {
1086         struct pcie_service_card *card = adapter->card;
1087         u32 *cookie;
1088
1089         card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32),
1090                                                      &card->sleep_cookie_pbase);
1091         if (!card->sleep_cookie_vbase) {
1092                 mwifiex_dbg(adapter, ERROR,
1093                             "pci_alloc_consistent failed!\n");
1094                 return -ENOMEM;
1095         }
1096         cookie = (u32 *)card->sleep_cookie_vbase;
1097         /* Init val of Sleep Cookie */
1098         *cookie = FW_AWAKE_COOKIE;
1099
1100         mwifiex_dbg(adapter, INFO, "alloc_scook: sleep cookie=0x%x\n", *cookie);
1101
1102         return 0;
1103 }
1104
1105 /*
1106  * This function deletes buffer for sleep cookie
1107  */
1108 static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
1109 {
1110         struct pcie_service_card *card;
1111
1112         if (!adapter)
1113                 return 0;
1114
1115         card = adapter->card;
1116
1117         if (card && card->sleep_cookie_vbase) {
1118                 pci_free_consistent(card->dev, sizeof(u32),
1119                                     card->sleep_cookie_vbase,
1120                                     card->sleep_cookie_pbase);
1121                 card->sleep_cookie_vbase = NULL;
1122         }
1123
1124         return 0;
1125 }
1126
1127 /* This function flushes the TX buffer descriptor ring
1128  * This function defined as handler is also called while cleaning TXRX
1129  * during disconnect/ bss stop.
1130  */
1131 static int mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter *adapter)
1132 {
1133         struct pcie_service_card *card = adapter->card;
1134
1135         if (!mwifiex_pcie_txbd_empty(card, card->txbd_rdptr)) {
1136                 card->txbd_flush = 1;
1137                 /* write pointer already set at last send
1138                  * send dnld-rdy intr again, wait for completion.
1139                  */
1140                 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1141                                       CPU_INTR_DNLD_RDY)) {
1142                         mwifiex_dbg(adapter, ERROR,
1143                                     "failed to assert dnld-rdy interrupt.\n");
1144                         return -1;
1145                 }
1146         }
1147         return 0;
1148 }
1149
1150 /*
1151  * This function unmaps and frees downloaded data buffer
1152  */
1153 static int mwifiex_pcie_send_data_complete(struct mwifiex_adapter *adapter)
1154 {
1155         struct sk_buff *skb;
1156         u32 wrdoneidx, rdptr, num_tx_buffs, unmap_count = 0;
1157         struct mwifiex_pcie_buf_desc *desc;
1158         struct mwifiex_pfu_buf_desc *desc2;
1159         struct pcie_service_card *card = adapter->card;
1160         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1161
1162         if (!mwifiex_pcie_ok_to_access_hw(adapter))
1163                 mwifiex_pm_wakeup_card(adapter);
1164
1165         /* Read the TX ring read pointer set by firmware */
1166         if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
1167                 mwifiex_dbg(adapter, ERROR,
1168                             "SEND COMP: failed to read reg->tx_rdptr\n");
1169                 return -1;
1170         }
1171
1172         mwifiex_dbg(adapter, DATA,
1173                     "SEND COMP: rdptr_prev=0x%x, rdptr=0x%x\n",
1174                     card->txbd_rdptr, rdptr);
1175
1176         num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1177         /* free from previous txbd_rdptr to current txbd_rdptr */
1178         while (((card->txbd_rdptr & reg->tx_mask) !=
1179                 (rdptr & reg->tx_mask)) ||
1180                ((card->txbd_rdptr & reg->tx_rollover_ind) !=
1181                 (rdptr & reg->tx_rollover_ind))) {
1182                 wrdoneidx = (card->txbd_rdptr & reg->tx_mask) >>
1183                             reg->tx_start_ptr;
1184
1185                 skb = card->tx_buf_list[wrdoneidx];
1186
1187                 if (skb) {
1188                         mwifiex_dbg(adapter, DATA,
1189                                     "SEND COMP: Detach skb %p at txbd_rdidx=%d\n",
1190                                     skb, wrdoneidx);
1191                         mwifiex_unmap_pci_memory(adapter, skb,
1192                                                  PCI_DMA_TODEVICE);
1193
1194                         unmap_count++;
1195
1196                         if (card->txbd_flush)
1197                                 mwifiex_write_data_complete(adapter, skb, 0,
1198                                                             -1);
1199                         else
1200                                 mwifiex_write_data_complete(adapter, skb, 0, 0);
1201                         atomic_dec(&adapter->tx_hw_pending);
1202                 }
1203
1204                 card->tx_buf_list[wrdoneidx] = NULL;
1205
1206                 if (reg->pfu_enabled) {
1207                         desc2 = card->txbd_ring[wrdoneidx];
1208                         memset(desc2, 0, sizeof(*desc2));
1209                 } else {
1210                         desc = card->txbd_ring[wrdoneidx];
1211                         memset(desc, 0, sizeof(*desc));
1212                 }
1213                 switch (card->dev->device) {
1214                 case PCIE_DEVICE_ID_MARVELL_88W8766P:
1215                         card->txbd_rdptr++;
1216                         break;
1217                 case PCIE_DEVICE_ID_MARVELL_88W8897:
1218                 case PCIE_DEVICE_ID_MARVELL_88W8997:
1219                         card->txbd_rdptr += reg->ring_tx_start_ptr;
1220                         break;
1221                 }
1222
1223
1224                 if ((card->txbd_rdptr & reg->tx_mask) == num_tx_buffs)
1225                         card->txbd_rdptr = ((card->txbd_rdptr &
1226                                              reg->tx_rollover_ind) ^
1227                                              reg->tx_rollover_ind);
1228         }
1229
1230         if (unmap_count)
1231                 adapter->data_sent = false;
1232
1233         if (card->txbd_flush) {
1234                 if (mwifiex_pcie_txbd_empty(card, card->txbd_rdptr))
1235                         card->txbd_flush = 0;
1236                 else
1237                         mwifiex_clean_pcie_ring_buf(adapter);
1238         }
1239
1240         return 0;
1241 }
1242
1243 /* This function sends data buffer to device. First 4 bytes of payload
1244  * are filled with payload length and payload type. Then this payload
1245  * is mapped to PCI device memory. Tx ring pointers are advanced accordingly.
1246  * Download ready interrupt to FW is deffered if Tx ring is not full and
1247  * additional payload can be accomodated.
1248  * Caller must ensure tx_param parameter to this function is not NULL.
1249  */
1250 static int
1251 mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb,
1252                        struct mwifiex_tx_param *tx_param)
1253 {
1254         struct pcie_service_card *card = adapter->card;
1255         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1256         u32 wrindx, num_tx_buffs, rx_val;
1257         int ret;
1258         dma_addr_t buf_pa;
1259         struct mwifiex_pcie_buf_desc *desc = NULL;
1260         struct mwifiex_pfu_buf_desc *desc2 = NULL;
1261
1262         if (!(skb->data && skb->len)) {
1263                 mwifiex_dbg(adapter, ERROR,
1264                             "%s(): invalid parameter <%p, %#x>\n",
1265                             __func__, skb->data, skb->len);
1266                 return -1;
1267         }
1268
1269         if (!mwifiex_pcie_ok_to_access_hw(adapter))
1270                 mwifiex_pm_wakeup_card(adapter);
1271
1272         num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1273         mwifiex_dbg(adapter, DATA,
1274                     "info: SEND DATA: <Rd: %#x, Wr: %#x>\n",
1275                 card->txbd_rdptr, card->txbd_wrptr);
1276         if (mwifiex_pcie_txbd_not_full(card)) {
1277                 u8 *payload;
1278
1279                 adapter->data_sent = true;
1280                 payload = skb->data;
1281                 put_unaligned_le16((u16)skb->len, payload + 0);
1282                 put_unaligned_le16(MWIFIEX_TYPE_DATA, payload + 2);
1283
1284                 if (mwifiex_map_pci_memory(adapter, skb, skb->len,
1285                                            PCI_DMA_TODEVICE))
1286                         return -1;
1287
1288                 wrindx = (card->txbd_wrptr & reg->tx_mask) >> reg->tx_start_ptr;
1289                 buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1290                 card->tx_buf_list[wrindx] = skb;
1291                 atomic_inc(&adapter->tx_hw_pending);
1292
1293                 if (reg->pfu_enabled) {
1294                         desc2 = card->txbd_ring[wrindx];
1295                         desc2->paddr = buf_pa;
1296                         desc2->len = (u16)skb->len;
1297                         desc2->frag_len = (u16)skb->len;
1298                         desc2->offset = 0;
1299                         desc2->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1300                                          MWIFIEX_BD_FLAG_LAST_DESC;
1301                 } else {
1302                         desc = card->txbd_ring[wrindx];
1303                         desc->paddr = buf_pa;
1304                         desc->len = (u16)skb->len;
1305                         desc->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1306                                       MWIFIEX_BD_FLAG_LAST_DESC;
1307                 }
1308
1309                 switch (card->dev->device) {
1310                 case PCIE_DEVICE_ID_MARVELL_88W8766P:
1311                         card->txbd_wrptr++;
1312                         break;
1313                 case PCIE_DEVICE_ID_MARVELL_88W8897:
1314                 case PCIE_DEVICE_ID_MARVELL_88W8997:
1315                         card->txbd_wrptr += reg->ring_tx_start_ptr;
1316                         break;
1317                 }
1318
1319                 if ((card->txbd_wrptr & reg->tx_mask) == num_tx_buffs)
1320                         card->txbd_wrptr = ((card->txbd_wrptr &
1321                                                 reg->tx_rollover_ind) ^
1322                                                 reg->tx_rollover_ind);
1323
1324                 rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
1325                 /* Write the TX ring write pointer in to reg->tx_wrptr */
1326                 if (mwifiex_write_reg(adapter, reg->tx_wrptr,
1327                                       card->txbd_wrptr | rx_val)) {
1328                         mwifiex_dbg(adapter, ERROR,
1329                                     "SEND DATA: failed to write reg->tx_wrptr\n");
1330                         ret = -1;
1331                         goto done_unmap;
1332                 }
1333
1334                 /* The firmware (latest version 15.68.19.p21) of the 88W8897 PCIe+USB card
1335                  * seems to crash randomly after setting the TX ring write pointer when
1336                  * ASPM powersaving is enabled. A workaround seems to be keeping the bus
1337                  * busy by reading a random register afterwards.
1338                  */
1339                 mwifiex_read_reg(adapter, PCI_VENDOR_ID, &rx_val);
1340
1341                 if ((mwifiex_pcie_txbd_not_full(card)) &&
1342                     tx_param->next_pkt_len) {
1343                         /* have more packets and TxBD still can hold more */
1344                         mwifiex_dbg(adapter, DATA,
1345                                     "SEND DATA: delay dnld-rdy interrupt.\n");
1346                         adapter->data_sent = false;
1347                 } else {
1348                         /* Send the TX ready interrupt */
1349                         if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1350                                               CPU_INTR_DNLD_RDY)) {
1351                                 mwifiex_dbg(adapter, ERROR,
1352                                             "SEND DATA: failed to assert dnld-rdy interrupt.\n");
1353                                 ret = -1;
1354                                 goto done_unmap;
1355                         }
1356                 }
1357                 mwifiex_dbg(adapter, DATA,
1358                             "info: SEND DATA: Updated <Rd: %#x, Wr:\t"
1359                             "%#x> and sent packet to firmware successfully\n",
1360                             card->txbd_rdptr, card->txbd_wrptr);
1361         } else {
1362                 mwifiex_dbg(adapter, DATA,
1363                             "info: TX Ring full, can't send packets to fw\n");
1364                 adapter->data_sent = true;
1365                 /* Send the TX ready interrupt */
1366                 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1367                                       CPU_INTR_DNLD_RDY))
1368                         mwifiex_dbg(adapter, ERROR,
1369                                     "SEND DATA: failed to assert door-bell intr\n");
1370                 return -EBUSY;
1371         }
1372
1373         return -EINPROGRESS;
1374 done_unmap:
1375         mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1376         card->tx_buf_list[wrindx] = NULL;
1377         atomic_dec(&adapter->tx_hw_pending);
1378         if (reg->pfu_enabled)
1379                 memset(desc2, 0, sizeof(*desc2));
1380         else
1381                 memset(desc, 0, sizeof(*desc));
1382
1383         return ret;
1384 }
1385
1386 /*
1387  * This function handles received buffer ring and
1388  * dispatches packets to upper
1389  */
1390 static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
1391 {
1392         struct pcie_service_card *card = adapter->card;
1393         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1394         u32 wrptr, rd_index, tx_val;
1395         dma_addr_t buf_pa;
1396         int ret = 0;
1397         struct sk_buff *skb_tmp = NULL;
1398         struct mwifiex_pcie_buf_desc *desc;
1399         struct mwifiex_pfu_buf_desc *desc2;
1400
1401         if (!mwifiex_pcie_ok_to_access_hw(adapter))
1402                 mwifiex_pm_wakeup_card(adapter);
1403
1404         /* Read the RX ring Write pointer set by firmware */
1405         if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1406                 mwifiex_dbg(adapter, ERROR,
1407                             "RECV DATA: failed to read reg->rx_wrptr\n");
1408                 ret = -1;
1409                 goto done;
1410         }
1411         card->rxbd_wrptr = wrptr;
1412
1413         while (((wrptr & reg->rx_mask) !=
1414                 (card->rxbd_rdptr & reg->rx_mask)) ||
1415                ((wrptr & reg->rx_rollover_ind) ==
1416                 (card->rxbd_rdptr & reg->rx_rollover_ind))) {
1417                 struct sk_buff *skb_data;
1418                 u16 rx_len;
1419
1420                 rd_index = card->rxbd_rdptr & reg->rx_mask;
1421                 skb_data = card->rx_buf_list[rd_index];
1422
1423                 /* If skb allocation was failed earlier for Rx packet,
1424                  * rx_buf_list[rd_index] would have been left with a NULL.
1425                  */
1426                 if (!skb_data)
1427                         return -ENOMEM;
1428
1429                 mwifiex_unmap_pci_memory(adapter, skb_data, PCI_DMA_FROMDEVICE);
1430                 card->rx_buf_list[rd_index] = NULL;
1431
1432                 /* Get data length from interface header -
1433                  * first 2 bytes for len, next 2 bytes is for type
1434                  */
1435                 rx_len = get_unaligned_le16(skb_data->data);
1436                 if (WARN_ON(rx_len <= adapter->intf_hdr_len ||
1437                             rx_len > MWIFIEX_RX_DATA_BUF_SIZE)) {
1438                         mwifiex_dbg(adapter, ERROR,
1439                                     "Invalid RX len %d, Rd=%#x, Wr=%#x\n",
1440                                     rx_len, card->rxbd_rdptr, wrptr);
1441                         dev_kfree_skb_any(skb_data);
1442                 } else {
1443                         skb_put(skb_data, rx_len);
1444                         mwifiex_dbg(adapter, DATA,
1445                                     "info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
1446                                     card->rxbd_rdptr, wrptr, rx_len);
1447                         skb_pull(skb_data, adapter->intf_hdr_len);
1448                         if (adapter->rx_work_enabled) {
1449                                 skb_queue_tail(&adapter->rx_data_q, skb_data);
1450                                 adapter->data_received = true;
1451                                 atomic_inc(&adapter->rx_pending);
1452                         } else {
1453                                 mwifiex_handle_rx_packet(adapter, skb_data);
1454                         }
1455                 }
1456
1457                 skb_tmp = mwifiex_alloc_dma_align_buf(MWIFIEX_RX_DATA_BUF_SIZE,
1458                                                       GFP_KERNEL);
1459                 if (!skb_tmp) {
1460                         mwifiex_dbg(adapter, ERROR,
1461                                     "Unable to allocate skb.\n");
1462                         return -ENOMEM;
1463                 }
1464
1465                 if (mwifiex_map_pci_memory(adapter, skb_tmp,
1466                                            MWIFIEX_RX_DATA_BUF_SIZE,
1467                                            PCI_DMA_FROMDEVICE))
1468                         return -1;
1469
1470                 buf_pa = MWIFIEX_SKB_DMA_ADDR(skb_tmp);
1471
1472                 mwifiex_dbg(adapter, INFO,
1473                             "RECV DATA: Attach new sk_buff %p at rxbd_rdidx=%d\n",
1474                             skb_tmp, rd_index);
1475                 card->rx_buf_list[rd_index] = skb_tmp;
1476
1477                 if (reg->pfu_enabled) {
1478                         desc2 = card->rxbd_ring[rd_index];
1479                         desc2->paddr = buf_pa;
1480                         desc2->len = skb_tmp->len;
1481                         desc2->frag_len = skb_tmp->len;
1482                         desc2->offset = 0;
1483                         desc2->flags = reg->ring_flag_sop | reg->ring_flag_eop;
1484                 } else {
1485                         desc = card->rxbd_ring[rd_index];
1486                         desc->paddr = buf_pa;
1487                         desc->len = skb_tmp->len;
1488                         desc->flags = 0;
1489                 }
1490
1491                 if ((++card->rxbd_rdptr & reg->rx_mask) ==
1492                                                         MWIFIEX_MAX_TXRX_BD) {
1493                         card->rxbd_rdptr = ((card->rxbd_rdptr &
1494                                              reg->rx_rollover_ind) ^
1495                                              reg->rx_rollover_ind);
1496                 }
1497                 mwifiex_dbg(adapter, DATA,
1498                             "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
1499                             card->rxbd_rdptr, wrptr);
1500
1501                 tx_val = card->txbd_wrptr & reg->tx_wrap_mask;
1502                 /* Write the RX ring read pointer in to reg->rx_rdptr */
1503                 if (mwifiex_write_reg(adapter, reg->rx_rdptr,
1504                                       card->rxbd_rdptr | tx_val)) {
1505                         mwifiex_dbg(adapter, DATA,
1506                                     "RECV DATA: failed to write reg->rx_rdptr\n");
1507                         ret = -1;
1508                         goto done;
1509                 }
1510
1511                 /* Read the RX ring Write pointer set by firmware */
1512                 if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1513                         mwifiex_dbg(adapter, ERROR,
1514                                     "RECV DATA: failed to read reg->rx_wrptr\n");
1515                         ret = -1;
1516                         goto done;
1517                 }
1518                 mwifiex_dbg(adapter, DATA,
1519                             "info: RECV DATA: Rcvd packet from fw successfully\n");
1520                 card->rxbd_wrptr = wrptr;
1521         }
1522
1523 done:
1524         return ret;
1525 }
1526
1527 /*
1528  * This function downloads the boot command to device
1529  */
1530 static int
1531 mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1532 {
1533         dma_addr_t buf_pa;
1534         struct pcie_service_card *card = adapter->card;
1535         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1536
1537         if (!(skb->data && skb->len)) {
1538                 mwifiex_dbg(adapter, ERROR,
1539                             "Invalid parameter in %s <%p. len %d>\n",
1540                             __func__, skb->data, skb->len);
1541                 return -1;
1542         }
1543
1544         if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1545                 return -1;
1546
1547         buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1548
1549         /* Write the lower 32bits of the physical address to low command
1550          * address scratch register
1551          */
1552         if (mwifiex_write_reg(adapter, reg->cmd_addr_lo, (u32)buf_pa)) {
1553                 mwifiex_dbg(adapter, ERROR,
1554                             "%s: failed to write download command to boot code.\n",
1555                             __func__);
1556                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1557                 return -1;
1558         }
1559
1560         /* Write the upper 32bits of the physical address to high command
1561          * address scratch register
1562          */
1563         if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1564                               (u32)((u64)buf_pa >> 32))) {
1565                 mwifiex_dbg(adapter, ERROR,
1566                             "%s: failed to write download command to boot code.\n",
1567                             __func__);
1568                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1569                 return -1;
1570         }
1571
1572         /* Write the command length to cmd_size scratch register */
1573         if (mwifiex_write_reg(adapter, reg->cmd_size, skb->len)) {
1574                 mwifiex_dbg(adapter, ERROR,
1575                             "%s: failed to write command len to cmd_size scratch reg\n",
1576                             __func__);
1577                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1578                 return -1;
1579         }
1580
1581         /* Ring the door bell */
1582         if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1583                               CPU_INTR_DOOR_BELL)) {
1584                 mwifiex_dbg(adapter, ERROR,
1585                             "%s: failed to assert door-bell intr\n", __func__);
1586                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1587                 return -1;
1588         }
1589
1590         return 0;
1591 }
1592
1593 /* This function init rx port in firmware which in turn enables to receive data
1594  * from device before transmitting any packet.
1595  */
1596 static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
1597 {
1598         struct pcie_service_card *card = adapter->card;
1599         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1600         int tx_wrap = card->txbd_wrptr & reg->tx_wrap_mask;
1601
1602         /* Write the RX ring read pointer in to reg->rx_rdptr */
1603         if (mwifiex_write_reg(adapter, reg->rx_rdptr, card->rxbd_rdptr |
1604                               tx_wrap)) {
1605                 mwifiex_dbg(adapter, ERROR,
1606                             "RECV DATA: failed to write reg->rx_rdptr\n");
1607                 return -1;
1608         }
1609         return 0;
1610 }
1611
1612 /* This function downloads commands to the device
1613  */
1614 static int
1615 mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1616 {
1617         struct pcie_service_card *card = adapter->card;
1618         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1619         int ret = 0;
1620         dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
1621         u8 *payload = (u8 *)skb->data;
1622
1623         if (!(skb->data && skb->len)) {
1624                 mwifiex_dbg(adapter, ERROR,
1625                             "Invalid parameter in %s <%p, %#x>\n",
1626                             __func__, skb->data, skb->len);
1627                 return -1;
1628         }
1629
1630         /* Make sure a command response buffer is available */
1631         if (!card->cmdrsp_buf) {
1632                 mwifiex_dbg(adapter, ERROR,
1633                             "No response buffer available, send command failed\n");
1634                 return -EBUSY;
1635         }
1636
1637         if (!mwifiex_pcie_ok_to_access_hw(adapter))
1638                 mwifiex_pm_wakeup_card(adapter);
1639
1640         adapter->cmd_sent = true;
1641
1642         put_unaligned_le16((u16)skb->len, &payload[0]);
1643         put_unaligned_le16(MWIFIEX_TYPE_CMD, &payload[2]);
1644
1645         if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1646                 return -1;
1647
1648         card->cmd_buf = skb;
1649         /*
1650          * Need to keep a reference, since core driver might free up this
1651          * buffer before we've unmapped it.
1652          */
1653         skb_get(skb);
1654
1655         /* To send a command, the driver will:
1656                 1. Write the 64bit physical address of the data buffer to
1657                    cmd response address low  + cmd response address high
1658                 2. Ring the door bell (i.e. set the door bell interrupt)
1659
1660                 In response to door bell interrupt, the firmware will perform
1661                 the DMA of the command packet (first header to obtain the total
1662                 length and then rest of the command).
1663         */
1664
1665         if (card->cmdrsp_buf) {
1666                 cmdrsp_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmdrsp_buf);
1667                 /* Write the lower 32bits of the cmdrsp buffer physical
1668                    address */
1669                 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo,
1670                                       (u32)cmdrsp_buf_pa)) {
1671                         mwifiex_dbg(adapter, ERROR,
1672                                     "Failed to write download cmd to boot code.\n");
1673                         ret = -1;
1674                         goto done;
1675                 }
1676                 /* Write the upper 32bits of the cmdrsp buffer physical
1677                    address */
1678                 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi,
1679                                       (u32)((u64)cmdrsp_buf_pa >> 32))) {
1680                         mwifiex_dbg(adapter, ERROR,
1681                                     "Failed to write download cmd to boot code.\n");
1682                         ret = -1;
1683                         goto done;
1684                 }
1685         }
1686
1687         cmd_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmd_buf);
1688         /* Write the lower 32bits of the physical address to reg->cmd_addr_lo */
1689         if (mwifiex_write_reg(adapter, reg->cmd_addr_lo,
1690                               (u32)cmd_buf_pa)) {
1691                 mwifiex_dbg(adapter, ERROR,
1692                             "Failed to write download cmd to boot code.\n");
1693                 ret = -1;
1694                 goto done;
1695         }
1696         /* Write the upper 32bits of the physical address to reg->cmd_addr_hi */
1697         if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1698                               (u32)((u64)cmd_buf_pa >> 32))) {
1699                 mwifiex_dbg(adapter, ERROR,
1700                             "Failed to write download cmd to boot code.\n");
1701                 ret = -1;
1702                 goto done;
1703         }
1704
1705         /* Write the command length to reg->cmd_size */
1706         if (mwifiex_write_reg(adapter, reg->cmd_size,
1707                               card->cmd_buf->len)) {
1708                 mwifiex_dbg(adapter, ERROR,
1709                             "Failed to write cmd len to reg->cmd_size\n");
1710                 ret = -1;
1711                 goto done;
1712         }
1713
1714         /* Ring the door bell */
1715         if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1716                               CPU_INTR_DOOR_BELL)) {
1717                 mwifiex_dbg(adapter, ERROR,
1718                             "Failed to assert door-bell intr\n");
1719                 ret = -1;
1720                 goto done;
1721         }
1722
1723 done:
1724         if (ret)
1725                 adapter->cmd_sent = false;
1726
1727         return 0;
1728 }
1729
1730 /*
1731  * This function handles command complete interrupt
1732  */
1733 static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1734 {
1735         struct pcie_service_card *card = adapter->card;
1736         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1737         struct sk_buff *skb = card->cmdrsp_buf;
1738         int count = 0;
1739         u16 rx_len;
1740
1741         mwifiex_dbg(adapter, CMD,
1742                     "info: Rx CMD Response\n");
1743
1744         if (adapter->curr_cmd)
1745                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_FROMDEVICE);
1746         else
1747                 pci_dma_sync_single_for_cpu(card->dev,
1748                                             MWIFIEX_SKB_DMA_ADDR(skb),
1749                                             MWIFIEX_UPLD_SIZE,
1750                                             PCI_DMA_FROMDEVICE);
1751
1752         /* Unmap the command as a response has been received. */
1753         if (card->cmd_buf) {
1754                 mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
1755                                          PCI_DMA_TODEVICE);
1756                 dev_kfree_skb_any(card->cmd_buf);
1757                 card->cmd_buf = NULL;
1758         }
1759
1760         rx_len = get_unaligned_le16(skb->data);
1761         skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1762         skb_trim(skb, rx_len);
1763
1764         if (!adapter->curr_cmd) {
1765                 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
1766                         pci_dma_sync_single_for_device(card->dev,
1767                                                 MWIFIEX_SKB_DMA_ADDR(skb),
1768                                                 MWIFIEX_SLEEP_COOKIE_SIZE,
1769                                                 PCI_DMA_FROMDEVICE);
1770                         if (mwifiex_write_reg(adapter,
1771                                               PCIE_CPU_INT_EVENT,
1772                                               CPU_INTR_SLEEP_CFM_DONE)) {
1773                                 mwifiex_dbg(adapter, ERROR,
1774                                             "Write register failed\n");
1775                                 return -1;
1776                         }
1777                         mwifiex_delay_for_sleep_cookie(adapter,
1778                                                        MWIFIEX_MAX_DELAY_COUNT);
1779                         mwifiex_unmap_pci_memory(adapter, skb,
1780                                                  PCI_DMA_FROMDEVICE);
1781                         skb_pull(skb, adapter->intf_hdr_len);
1782                         while (reg->sleep_cookie && (count++ < 10) &&
1783                                mwifiex_pcie_ok_to_access_hw(adapter))
1784                                 usleep_range(50, 60);
1785                         mwifiex_pcie_enable_host_int(adapter);
1786                         mwifiex_process_sleep_confirm_resp(adapter, skb->data,
1787                                                            skb->len);
1788                 } else {
1789                         mwifiex_dbg(adapter, ERROR,
1790                                     "There is no command but got cmdrsp\n");
1791                 }
1792                 memcpy(adapter->upld_buf, skb->data,
1793                        min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
1794                 skb_push(skb, adapter->intf_hdr_len);
1795                 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1796                                            PCI_DMA_FROMDEVICE))
1797                         return -1;
1798         } else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1799                 skb_pull(skb, adapter->intf_hdr_len);
1800                 adapter->curr_cmd->resp_skb = skb;
1801                 adapter->cmd_resp_received = true;
1802                 /* Take the pointer and set it to CMD node and will
1803                    return in the response complete callback */
1804                 card->cmdrsp_buf = NULL;
1805
1806                 /* Clear the cmd-rsp buffer address in scratch registers. This
1807                    will prevent firmware from writing to the same response
1808                    buffer again. */
1809                 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo, 0)) {
1810                         mwifiex_dbg(adapter, ERROR,
1811                                     "cmd_done: failed to clear cmd_rsp_addr_lo\n");
1812                         return -1;
1813                 }
1814                 /* Write the upper 32bits of the cmdrsp buffer physical
1815                    address */
1816                 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi, 0)) {
1817                         mwifiex_dbg(adapter, ERROR,
1818                                     "cmd_done: failed to clear cmd_rsp_addr_hi\n");
1819                         return -1;
1820                 }
1821         }
1822
1823         return 0;
1824 }
1825
1826 /*
1827  * Command Response processing complete handler
1828  */
1829 static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1830                                         struct sk_buff *skb)
1831 {
1832         struct pcie_service_card *card = adapter->card;
1833
1834         if (skb) {
1835                 card->cmdrsp_buf = skb;
1836                 skb_push(card->cmdrsp_buf, adapter->intf_hdr_len);
1837                 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1838                                            PCI_DMA_FROMDEVICE))
1839                         return -1;
1840         }
1841
1842         return 0;
1843 }
1844
1845 /*
1846  * This function handles firmware event ready interrupt
1847  */
1848 static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1849 {
1850         struct pcie_service_card *card = adapter->card;
1851         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1852         u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1853         u32 wrptr, event;
1854         struct mwifiex_evt_buf_desc *desc;
1855
1856         if (!mwifiex_pcie_ok_to_access_hw(adapter))
1857                 mwifiex_pm_wakeup_card(adapter);
1858
1859         if (adapter->event_received) {
1860                 mwifiex_dbg(adapter, EVENT,
1861                             "info: Event being processed,\t"
1862                             "do not process this interrupt just yet\n");
1863                 return 0;
1864         }
1865
1866         if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1867                 mwifiex_dbg(adapter, ERROR,
1868                             "info: Invalid read pointer...\n");
1869                 return -1;
1870         }
1871
1872         /* Read the event ring write pointer set by firmware */
1873         if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1874                 mwifiex_dbg(adapter, ERROR,
1875                             "EventReady: failed to read reg->evt_wrptr\n");
1876                 return -1;
1877         }
1878
1879         mwifiex_dbg(adapter, EVENT,
1880                     "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
1881                     card->evtbd_rdptr, wrptr);
1882         if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
1883                                               & MWIFIEX_EVTBD_MASK)) ||
1884             ((wrptr & reg->evt_rollover_ind) ==
1885              (card->evtbd_rdptr & reg->evt_rollover_ind))) {
1886                 struct sk_buff *skb_cmd;
1887                 __le16 data_len = 0;
1888                 u16 evt_len;
1889
1890                 mwifiex_dbg(adapter, INFO,
1891                             "info: Read Index: %d\n", rdptr);
1892                 skb_cmd = card->evt_buf_list[rdptr];
1893                 mwifiex_unmap_pci_memory(adapter, skb_cmd, PCI_DMA_FROMDEVICE);
1894
1895                 /* Take the pointer and set it to event pointer in adapter
1896                    and will return back after event handling callback */
1897                 card->evt_buf_list[rdptr] = NULL;
1898                 desc = card->evtbd_ring[rdptr];
1899                 memset(desc, 0, sizeof(*desc));
1900
1901                 event = get_unaligned_le32(
1902                         &skb_cmd->data[adapter->intf_hdr_len]);
1903                 adapter->event_cause = event;
1904                 /* The first 4bytes will be the event transfer header
1905                    len is 2 bytes followed by type which is 2 bytes */
1906                 memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1907                 evt_len = le16_to_cpu(data_len);
1908                 skb_trim(skb_cmd, evt_len);
1909                 skb_pull(skb_cmd, adapter->intf_hdr_len);
1910                 mwifiex_dbg(adapter, EVENT,
1911                             "info: Event length: %d\n", evt_len);
1912
1913                 if (evt_len > MWIFIEX_EVENT_HEADER_LEN &&
1914                     evt_len < MAX_EVENT_SIZE)
1915                         memcpy(adapter->event_body, skb_cmd->data +
1916                                MWIFIEX_EVENT_HEADER_LEN, evt_len -
1917                                MWIFIEX_EVENT_HEADER_LEN);
1918
1919                 adapter->event_received = true;
1920                 adapter->event_skb = skb_cmd;
1921
1922                 /* Do not update the event read pointer here, wait till the
1923                    buffer is released. This is just to make things simpler,
1924                    we need to find a better method of managing these buffers.
1925                 */
1926         } else {
1927                 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1928                                       CPU_INTR_EVENT_DONE)) {
1929                         mwifiex_dbg(adapter, ERROR,
1930                                     "Write register failed\n");
1931                         return -1;
1932                 }
1933         }
1934
1935         return 0;
1936 }
1937
1938 /*
1939  * Event processing complete handler
1940  */
1941 static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1942                                        struct sk_buff *skb)
1943 {
1944         struct pcie_service_card *card = adapter->card;
1945         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1946         int ret = 0;
1947         u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1948         u32 wrptr;
1949         struct mwifiex_evt_buf_desc *desc;
1950
1951         if (!skb)
1952                 return 0;
1953
1954         if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1955                 mwifiex_dbg(adapter, ERROR,
1956                             "event_complete: Invalid rdptr 0x%x\n",
1957                             rdptr);
1958                 return -EINVAL;
1959         }
1960
1961         /* Read the event ring write pointer set by firmware */
1962         if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1963                 mwifiex_dbg(adapter, ERROR,
1964                             "event_complete: failed to read reg->evt_wrptr\n");
1965                 return -1;
1966         }
1967
1968         if (!card->evt_buf_list[rdptr]) {
1969                 skb_push(skb, adapter->intf_hdr_len);
1970                 skb_put(skb, MAX_EVENT_SIZE - skb->len);
1971                 if (mwifiex_map_pci_memory(adapter, skb,
1972                                            MAX_EVENT_SIZE,
1973                                            PCI_DMA_FROMDEVICE))
1974                         return -1;
1975                 card->evt_buf_list[rdptr] = skb;
1976                 desc = card->evtbd_ring[rdptr];
1977                 desc->paddr = MWIFIEX_SKB_DMA_ADDR(skb);
1978                 desc->len = (u16)skb->len;
1979                 desc->flags = 0;
1980                 skb = NULL;
1981         } else {
1982                 mwifiex_dbg(adapter, ERROR,
1983                             "info: ERROR: buf still valid at index %d, <%p, %p>\n",
1984                             rdptr, card->evt_buf_list[rdptr], skb);
1985         }
1986
1987         if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
1988                 card->evtbd_rdptr = ((card->evtbd_rdptr &
1989                                         reg->evt_rollover_ind) ^
1990                                         reg->evt_rollover_ind);
1991         }
1992
1993         mwifiex_dbg(adapter, EVENT,
1994                     "info: Updated <Rd: 0x%x, Wr: 0x%x>",
1995                     card->evtbd_rdptr, wrptr);
1996
1997         /* Write the event ring read pointer in to reg->evt_rdptr */
1998         if (mwifiex_write_reg(adapter, reg->evt_rdptr,
1999                               card->evtbd_rdptr)) {
2000                 mwifiex_dbg(adapter, ERROR,
2001                             "event_complete: failed to read reg->evt_rdptr\n");
2002                 return -1;
2003         }
2004
2005         mwifiex_dbg(adapter, EVENT,
2006                     "info: Check Events Again\n");
2007         ret = mwifiex_pcie_process_event_ready(adapter);
2008
2009         return ret;
2010 }
2011
2012 /* Combo firmware image is a combination of
2013  * (1) combo crc heaer, start with CMD5
2014  * (2) bluetooth image, start with CMD7, end with CMD6, data wrapped in CMD1.
2015  * (3) wifi image.
2016  *
2017  * This function bypass the header and bluetooth part, return
2018  * the offset of tail wifi-only part. If the image is already wifi-only,
2019  * that is start with CMD1, return 0.
2020  */
2021
2022 static int mwifiex_extract_wifi_fw(struct mwifiex_adapter *adapter,
2023                                    const void *firmware, u32 firmware_len) {
2024         const struct mwifiex_fw_data *fwdata;
2025         u32 offset = 0, data_len, dnld_cmd;
2026         int ret = 0;
2027         bool cmd7_before = false, first_cmd = false;
2028
2029         while (1) {
2030                 /* Check for integer and buffer overflow */
2031                 if (offset + sizeof(fwdata->header) < sizeof(fwdata->header) ||
2032                     offset + sizeof(fwdata->header) >= firmware_len) {
2033                         mwifiex_dbg(adapter, ERROR,
2034                                     "extract wifi-only fw failure!\n");
2035                         ret = -1;
2036                         goto done;
2037                 }
2038
2039                 fwdata = firmware + offset;
2040                 dnld_cmd = le32_to_cpu(fwdata->header.dnld_cmd);
2041                 data_len = le32_to_cpu(fwdata->header.data_length);
2042
2043                 /* Skip past header */
2044                 offset += sizeof(fwdata->header);
2045
2046                 switch (dnld_cmd) {
2047                 case MWIFIEX_FW_DNLD_CMD_1:
2048                         if (offset + data_len < data_len) {
2049                                 mwifiex_dbg(adapter, ERROR, "bad FW parse\n");
2050                                 ret = -1;
2051                                 goto done;
2052                         }
2053
2054                         /* Image start with cmd1, already wifi-only firmware */
2055                         if (!first_cmd) {
2056                                 mwifiex_dbg(adapter, MSG,
2057                                             "input wifi-only firmware\n");
2058                                 return 0;
2059                         }
2060
2061                         if (!cmd7_before) {
2062                                 mwifiex_dbg(adapter, ERROR,
2063                                             "no cmd7 before cmd1!\n");
2064                                 ret = -1;
2065                                 goto done;
2066                         }
2067                         offset += data_len;
2068                         break;
2069                 case MWIFIEX_FW_DNLD_CMD_5:
2070                         first_cmd = true;
2071                         /* Check for integer overflow */
2072                         if (offset + data_len < data_len) {
2073                                 mwifiex_dbg(adapter, ERROR, "bad FW parse\n");
2074                                 ret = -1;
2075                                 goto done;
2076                         }
2077                         offset += data_len;
2078                         break;
2079                 case MWIFIEX_FW_DNLD_CMD_6:
2080                         first_cmd = true;
2081                         /* Check for integer overflow */
2082                         if (offset + data_len < data_len) {
2083                                 mwifiex_dbg(adapter, ERROR, "bad FW parse\n");
2084                                 ret = -1;
2085                                 goto done;
2086                         }
2087                         offset += data_len;
2088                         if (offset >= firmware_len) {
2089                                 mwifiex_dbg(adapter, ERROR,
2090                                             "extract wifi-only fw failure!\n");
2091                                 ret = -1;
2092                         } else {
2093                                 ret = offset;
2094                         }
2095                         goto done;
2096                 case MWIFIEX_FW_DNLD_CMD_7:
2097                         first_cmd = true;
2098                         cmd7_before = true;
2099                         break;
2100                 default:
2101                         mwifiex_dbg(adapter, ERROR, "unknown dnld_cmd %d\n",
2102                                     dnld_cmd);
2103                         ret = -1;
2104                         goto done;
2105                 }
2106         }
2107
2108 done:
2109         return ret;
2110 }
2111
2112 /*
2113  * This function downloads the firmware to the card.
2114  *
2115  * Firmware is downloaded to the card in blocks. Every block download
2116  * is tested for CRC errors, and retried a number of times before
2117  * returning failure.
2118  */
2119 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
2120                                     struct mwifiex_fw_image *fw)
2121 {
2122         int ret;
2123         u8 *firmware = fw->fw_buf;
2124         u32 firmware_len = fw->fw_len;
2125         u32 offset = 0;
2126         struct sk_buff *skb;
2127         u32 txlen, tx_blocks = 0, tries, len, val;
2128         u32 block_retry_cnt = 0;
2129         struct pcie_service_card *card = adapter->card;
2130         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2131
2132         if (!firmware || !firmware_len) {
2133                 mwifiex_dbg(adapter, ERROR,
2134                             "No firmware image found! Terminating download\n");
2135                 return -1;
2136         }
2137
2138         mwifiex_dbg(adapter, INFO,
2139                     "info: Downloading FW image (%d bytes)\n",
2140                     firmware_len);
2141
2142         if (mwifiex_pcie_disable_host_int(adapter)) {
2143                 mwifiex_dbg(adapter, ERROR,
2144                             "%s: Disabling interrupts failed.\n", __func__);
2145                 return -1;
2146         }
2147
2148         skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
2149         if (!skb) {
2150                 ret = -ENOMEM;
2151                 goto done;
2152         }
2153
2154         ret = mwifiex_read_reg(adapter, PCIE_SCRATCH_13_REG, &val);
2155         if (ret) {
2156                 mwifiex_dbg(adapter, FATAL, "Failed to read scratch register 13\n");
2157                 goto done;
2158         }
2159
2160         /* PCIE FLR case: extract wifi part from combo firmware*/
2161         if (val == MWIFIEX_PCIE_FLR_HAPPENS) {
2162                 ret = mwifiex_extract_wifi_fw(adapter, firmware, firmware_len);
2163                 if (ret < 0) {
2164                         mwifiex_dbg(adapter, ERROR, "Failed to extract wifi fw\n");
2165                         goto done;
2166                 }
2167                 offset = ret;
2168                 mwifiex_dbg(adapter, MSG,
2169                             "info: dnld wifi firmware from %d bytes\n", offset);
2170         }
2171
2172         /* Perform firmware data transfer */
2173         do {
2174                 u32 ireg_intr = 0;
2175
2176                 /* More data? */
2177                 if (offset >= firmware_len)
2178                         break;
2179
2180                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2181                         ret = mwifiex_read_reg(adapter, reg->cmd_size,
2182                                                &len);
2183                         if (ret) {
2184                                 mwifiex_dbg(adapter, FATAL,
2185                                             "Failed reading len from boot code\n");
2186                                 goto done;
2187                         }
2188                         if (len)
2189                                 break;
2190                         usleep_range(10, 20);
2191                 }
2192
2193                 if (!len) {
2194                         break;
2195                 } else if (len > MWIFIEX_UPLD_SIZE) {
2196                         mwifiex_dbg(adapter, ERROR,
2197                                     "FW download failure @ %d, invalid length %d\n",
2198                                     offset, len);
2199                         ret = -1;
2200                         goto done;
2201                 }
2202
2203                 txlen = len;
2204
2205                 if (len & BIT(0)) {
2206                         block_retry_cnt++;
2207                         if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
2208                                 mwifiex_dbg(adapter, ERROR,
2209                                             "FW download failure @ %d, over max\t"
2210                                             "retry count\n", offset);
2211                                 ret = -1;
2212                                 goto done;
2213                         }
2214                         mwifiex_dbg(adapter, ERROR,
2215                                     "FW CRC error indicated by the\t"
2216                                     "helper: len = 0x%04X, txlen = %d\n",
2217                                     len, txlen);
2218                         len &= ~BIT(0);
2219                         /* Setting this to 0 to resend from same offset */
2220                         txlen = 0;
2221                 } else {
2222                         block_retry_cnt = 0;
2223                         /* Set blocksize to transfer - checking for
2224                            last block */
2225                         if (firmware_len - offset < txlen)
2226                                 txlen = firmware_len - offset;
2227
2228                         tx_blocks = (txlen + card->pcie.blksz_fw_dl - 1) /
2229                                     card->pcie.blksz_fw_dl;
2230
2231                         /* Copy payload to buffer */
2232                         memmove(skb->data, &firmware[offset], txlen);
2233                 }
2234
2235                 skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
2236                 skb_trim(skb, tx_blocks * card->pcie.blksz_fw_dl);
2237
2238                 /* Send the boot command to device */
2239                 if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
2240                         mwifiex_dbg(adapter, ERROR,
2241                                     "Failed to send firmware download command\n");
2242                         ret = -1;
2243                         goto done;
2244                 }
2245
2246                 /* Wait for the command done interrupt */
2247                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2248                         if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
2249                                              &ireg_intr)) {
2250                                 mwifiex_dbg(adapter, ERROR,
2251                                             "%s: Failed to read\t"
2252                                             "interrupt status during fw dnld.\n",
2253                                             __func__);
2254                                 mwifiex_unmap_pci_memory(adapter, skb,
2255                                                          PCI_DMA_TODEVICE);
2256                                 ret = -1;
2257                                 goto done;
2258                         }
2259                         if (!(ireg_intr & CPU_INTR_DOOR_BELL))
2260                                 break;
2261                         usleep_range(10, 20);
2262                 }
2263                 if (ireg_intr & CPU_INTR_DOOR_BELL) {
2264                         mwifiex_dbg(adapter, ERROR, "%s: Card failed to ACK download\n",
2265                                     __func__);
2266                         mwifiex_unmap_pci_memory(adapter, skb,
2267                                                  PCI_DMA_TODEVICE);
2268                         ret = -1;
2269                         goto done;
2270                 }
2271
2272                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
2273
2274                 offset += txlen;
2275         } while (true);
2276
2277         mwifiex_dbg(adapter, MSG,
2278                     "info: FW download over, size %d bytes\n", offset);
2279
2280         ret = 0;
2281
2282 done:
2283         dev_kfree_skb_any(skb);
2284         return ret;
2285 }
2286
2287 /*
2288  * This function checks the firmware status in card.
2289  */
2290 static int
2291 mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
2292 {
2293         int ret = 0;
2294         u32 firmware_stat;
2295         struct pcie_service_card *card = adapter->card;
2296         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2297         u32 tries;
2298
2299         /* Mask spurios interrupts */
2300         if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
2301                               HOST_INTR_MASK)) {
2302                 mwifiex_dbg(adapter, ERROR,
2303                             "Write register failed\n");
2304                 return -1;
2305         }
2306
2307         mwifiex_dbg(adapter, INFO,
2308                     "Setting driver ready signature\n");
2309         if (mwifiex_write_reg(adapter, reg->drv_rdy,
2310                               FIRMWARE_READY_PCIE)) {
2311                 mwifiex_dbg(adapter, ERROR,
2312                             "Failed to write driver ready signature\n");
2313                 return -1;
2314         }
2315
2316         /* Wait for firmware initialization event */
2317         for (tries = 0; tries < poll_num; tries++) {
2318                 if (mwifiex_read_reg(adapter, reg->fw_status,
2319                                      &firmware_stat))
2320                         ret = -1;
2321                 else
2322                         ret = 0;
2323
2324                 mwifiex_dbg(adapter, INFO, "Try %d if FW is ready <%d,%#x>",
2325                             tries, ret, firmware_stat);
2326
2327                 if (ret)
2328                         continue;
2329                 if (firmware_stat == FIRMWARE_READY_PCIE) {
2330                         ret = 0;
2331                         break;
2332                 } else {
2333                         msleep(100);
2334                         ret = -1;
2335                 }
2336         }
2337
2338         return ret;
2339 }
2340
2341 /* This function checks if WLAN is the winner.
2342  */
2343 static int
2344 mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
2345 {
2346         u32 winner = 0;
2347         int ret = 0;
2348         struct pcie_service_card *card = adapter->card;
2349         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2350
2351         if (mwifiex_read_reg(adapter, reg->fw_status, &winner)) {
2352                 ret = -1;
2353         } else if (!winner) {
2354                 mwifiex_dbg(adapter, INFO, "PCI-E is the winner\n");
2355                 adapter->winner = 1;
2356         } else {
2357                 mwifiex_dbg(adapter, ERROR,
2358                             "PCI-E is not the winner <%#x>", winner);
2359         }
2360
2361         return ret;
2362 }
2363
2364 /*
2365  * This function reads the interrupt status from card.
2366  */
2367 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter,
2368                                      int msg_id)
2369 {
2370         u32 pcie_ireg;
2371         unsigned long flags;
2372         struct pcie_service_card *card = adapter->card;
2373
2374         if (card->msi_enable) {
2375                 spin_lock_irqsave(&adapter->int_lock, flags);
2376                 adapter->int_status = 1;
2377                 spin_unlock_irqrestore(&adapter->int_lock, flags);
2378                 return;
2379         }
2380
2381         if (!mwifiex_pcie_ok_to_access_hw(adapter))
2382                 return;
2383
2384         if (card->msix_enable && msg_id >= 0) {
2385                 pcie_ireg = BIT(msg_id);
2386         } else {
2387                 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2388                                      &pcie_ireg)) {
2389                         mwifiex_dbg(adapter, ERROR, "Read register failed\n");
2390                         return;
2391                 }
2392
2393                 if ((pcie_ireg == 0xFFFFFFFF) || !pcie_ireg)
2394                         return;
2395
2396
2397                 mwifiex_pcie_disable_host_int(adapter);
2398
2399                 /* Clear the pending interrupts */
2400                 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
2401                                       ~pcie_ireg)) {
2402                         mwifiex_dbg(adapter, ERROR,
2403                                     "Write register failed\n");
2404                         return;
2405                 }
2406         }
2407
2408         if (!adapter->pps_uapsd_mode &&
2409             adapter->ps_state == PS_STATE_SLEEP &&
2410             mwifiex_pcie_ok_to_access_hw(adapter)) {
2411                 /* Potentially for PCIe we could get other
2412                  * interrupts like shared. Don't change power
2413                  * state until cookie is set
2414                  */
2415                 adapter->ps_state = PS_STATE_AWAKE;
2416                 adapter->pm_wakeup_fw_try = false;
2417                 del_timer(&adapter->wakeup_timer);
2418         }
2419
2420         spin_lock_irqsave(&adapter->int_lock, flags);
2421         adapter->int_status |= pcie_ireg;
2422         spin_unlock_irqrestore(&adapter->int_lock, flags);
2423         mwifiex_dbg(adapter, INTR, "ireg: 0x%08x\n", pcie_ireg);
2424 }
2425
2426 /*
2427  * Interrupt handler for PCIe root port
2428  *
2429  * This function reads the interrupt status from firmware and assigns
2430  * the main process in workqueue which will handle the interrupt.
2431  */
2432 static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
2433 {
2434         struct mwifiex_msix_context *ctx = context;
2435         struct pci_dev *pdev = ctx->dev;
2436         struct pcie_service_card *card;
2437         struct mwifiex_adapter *adapter;
2438
2439         card = pci_get_drvdata(pdev);
2440
2441         if (!card->adapter) {
2442                 pr_err("info: %s: card=%p adapter=%p\n", __func__, card,
2443                        card ? card->adapter : NULL);
2444                 goto exit;
2445         }
2446         adapter = card->adapter;
2447
2448         if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
2449                 goto exit;
2450
2451         if (card->msix_enable)
2452                 mwifiex_interrupt_status(adapter, ctx->msg_id);
2453         else
2454                 mwifiex_interrupt_status(adapter, -1);
2455
2456         mwifiex_queue_main_work(adapter);
2457
2458 exit:
2459         return IRQ_HANDLED;
2460 }
2461
2462 /*
2463  * This function checks the current interrupt status.
2464  *
2465  * The following interrupts are checked and handled by this function -
2466  *      - Data sent
2467  *      - Command sent
2468  *      - Command received
2469  *      - Packets received
2470  *      - Events received
2471  *
2472  * In case of Rx packets received, the packets are uploaded from card to
2473  * host and processed accordingly.
2474  */
2475 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
2476 {
2477         int ret;
2478         u32 pcie_ireg = 0;
2479         unsigned long flags;
2480         struct pcie_service_card *card = adapter->card;
2481
2482         spin_lock_irqsave(&adapter->int_lock, flags);
2483         if (!card->msi_enable) {
2484                 /* Clear out unused interrupts */
2485                 pcie_ireg = adapter->int_status;
2486         }
2487         adapter->int_status = 0;
2488         spin_unlock_irqrestore(&adapter->int_lock, flags);
2489
2490         if (card->msi_enable) {
2491                 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
2492                         if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2493                                              &pcie_ireg)) {
2494                                 mwifiex_dbg(adapter, ERROR,
2495                                             "Read register failed\n");
2496                                 return -1;
2497                         }
2498
2499                         if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2500                                 if (mwifiex_write_reg(adapter,
2501                                                       PCIE_HOST_INT_STATUS,
2502                                                       ~pcie_ireg)) {
2503                                         mwifiex_dbg(adapter, ERROR,
2504                                                     "Write register failed\n");
2505                                         return -1;
2506                                 }
2507                                 if (!adapter->pps_uapsd_mode &&
2508                                     adapter->ps_state == PS_STATE_SLEEP) {
2509                                         adapter->ps_state = PS_STATE_AWAKE;
2510                                         adapter->pm_wakeup_fw_try = false;
2511                                         del_timer(&adapter->wakeup_timer);
2512                                 }
2513                         }
2514                 }
2515         }
2516
2517         if (pcie_ireg & HOST_INTR_DNLD_DONE) {
2518                 mwifiex_dbg(adapter, INTR, "info: TX DNLD Done\n");
2519                 ret = mwifiex_pcie_send_data_complete(adapter);
2520                 if (ret)
2521                         return ret;
2522         }
2523         if (pcie_ireg & HOST_INTR_UPLD_RDY) {
2524                 mwifiex_dbg(adapter, INTR, "info: Rx DATA\n");
2525                 ret = mwifiex_pcie_process_recv_data(adapter);
2526                 if (ret)
2527                         return ret;
2528         }
2529         if (pcie_ireg & HOST_INTR_EVENT_RDY) {
2530                 mwifiex_dbg(adapter, INTR, "info: Rx EVENT\n");
2531                 ret = mwifiex_pcie_process_event_ready(adapter);
2532                 if (ret)
2533                         return ret;
2534         }
2535         if (pcie_ireg & HOST_INTR_CMD_DONE) {
2536                 if (adapter->cmd_sent) {
2537                         mwifiex_dbg(adapter, INTR,
2538                                     "info: CMD sent Interrupt\n");
2539                         adapter->cmd_sent = false;
2540                 }
2541                 /* Handle command response */
2542                 ret = mwifiex_pcie_process_cmd_complete(adapter);
2543                 if (ret)
2544                         return ret;
2545         }
2546
2547         mwifiex_dbg(adapter, INTR,
2548                     "info: cmd_sent=%d data_sent=%d\n",
2549                     adapter->cmd_sent, adapter->data_sent);
2550         if (!card->msi_enable && !card->msix_enable &&
2551                                  adapter->ps_state != PS_STATE_SLEEP)
2552                 mwifiex_pcie_enable_host_int(adapter);
2553
2554         return 0;
2555 }
2556
2557 /*
2558  * This function downloads data from driver to card.
2559  *
2560  * Both commands and data packets are transferred to the card by this
2561  * function.
2562  *
2563  * This function adds the PCIE specific header to the front of the buffer
2564  * before transferring. The header contains the length of the packet and
2565  * the type. The firmware handles the packets based upon this set type.
2566  */
2567 static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
2568                                      struct sk_buff *skb,
2569                                      struct mwifiex_tx_param *tx_param)
2570 {
2571         if (!skb) {
2572                 mwifiex_dbg(adapter, ERROR,
2573                             "Passed NULL skb to %s\n", __func__);
2574                 return -1;
2575         }
2576
2577         if (type == MWIFIEX_TYPE_DATA)
2578                 return mwifiex_pcie_send_data(adapter, skb, tx_param);
2579         else if (type == MWIFIEX_TYPE_CMD)
2580                 return mwifiex_pcie_send_cmd(adapter, skb);
2581
2582         return 0;
2583 }
2584
2585 /* Function to dump PCIE scratch registers in case of FW crash
2586  */
2587 static int
2588 mwifiex_pcie_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2589 {
2590         char *p = drv_buf;
2591         char buf[256], *ptr;
2592         int i;
2593         u32 value;
2594         struct pcie_service_card *card = adapter->card;
2595         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2596         int pcie_scratch_reg[] = {PCIE_SCRATCH_12_REG,
2597                                   PCIE_SCRATCH_14_REG,
2598                                   PCIE_SCRATCH_15_REG};
2599
2600         if (!p)
2601                 return 0;
2602
2603         mwifiex_dbg(adapter, MSG, "PCIE register dump start\n");
2604
2605         if (mwifiex_read_reg(adapter, reg->fw_status, &value)) {
2606                 mwifiex_dbg(adapter, ERROR, "failed to read firmware status");
2607                 return 0;
2608         }
2609
2610         ptr = buf;
2611         mwifiex_dbg(adapter, MSG, "pcie scratch register:");
2612         for (i = 0; i < ARRAY_SIZE(pcie_scratch_reg); i++) {
2613                 mwifiex_read_reg(adapter, pcie_scratch_reg[i], &value);
2614                 ptr += sprintf(ptr, "reg:0x%x, value=0x%x\n",
2615                                pcie_scratch_reg[i], value);
2616         }
2617
2618         mwifiex_dbg(adapter, MSG, "%s\n", buf);
2619         p += sprintf(p, "%s\n", buf);
2620
2621         mwifiex_dbg(adapter, MSG, "PCIE register dump end\n");
2622
2623         return p - drv_buf;
2624 }
2625
2626 /* This function read/write firmware */
2627 static enum rdwr_status
2628 mwifiex_pcie_rdwr_firmware(struct mwifiex_adapter *adapter, u8 doneflag)
2629 {
2630         int ret, tries;
2631         u8 ctrl_data;
2632         u32 fw_status;
2633         struct pcie_service_card *card = adapter->card;
2634         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2635
2636         if (mwifiex_read_reg(adapter, reg->fw_status, &fw_status))
2637                 return RDWR_STATUS_FAILURE;
2638
2639         ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl,
2640                                 reg->fw_dump_host_ready);
2641         if (ret) {
2642                 mwifiex_dbg(adapter, ERROR,
2643                             "PCIE write err\n");
2644                 return RDWR_STATUS_FAILURE;
2645         }
2646
2647         for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2648                 mwifiex_read_reg_byte(adapter, reg->fw_dump_ctrl, &ctrl_data);
2649                 if (ctrl_data == FW_DUMP_DONE)
2650                         return RDWR_STATUS_SUCCESS;
2651                 if (doneflag && ctrl_data == doneflag)
2652                         return RDWR_STATUS_DONE;
2653                 if (ctrl_data != reg->fw_dump_host_ready) {
2654                         mwifiex_dbg(adapter, WARN,
2655                                     "The ctrl reg was changed, re-try again!\n");
2656                         ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl,
2657                                                 reg->fw_dump_host_ready);
2658                         if (ret) {
2659                                 mwifiex_dbg(adapter, ERROR,
2660                                             "PCIE write err\n");
2661                                 return RDWR_STATUS_FAILURE;
2662                         }
2663                 }
2664                 usleep_range(100, 200);
2665         }
2666
2667         mwifiex_dbg(adapter, ERROR, "Fail to pull ctrl_data\n");
2668         return RDWR_STATUS_FAILURE;
2669 }
2670
2671 /* This function dump firmware memory to file */
2672 static void mwifiex_pcie_fw_dump(struct mwifiex_adapter *adapter)
2673 {
2674         struct pcie_service_card *card = adapter->card;
2675         const struct mwifiex_pcie_card_reg *creg = card->pcie.reg;
2676         unsigned int reg, reg_start, reg_end;
2677         u8 *dbg_ptr, *end_ptr, *tmp_ptr, fw_dump_num, dump_num;
2678         u8 idx, i, read_reg, doneflag = 0;
2679         enum rdwr_status stat;
2680         u32 memory_size;
2681         int ret;
2682
2683         if (!card->pcie.can_dump_fw)
2684                 return;
2685
2686         for (idx = 0; idx < adapter->num_mem_types; idx++) {
2687                 struct memory_type_mapping *entry =
2688                                 &adapter->mem_type_mapping_tbl[idx];
2689
2690                 if (entry->mem_ptr) {
2691                         vfree(entry->mem_ptr);
2692                         entry->mem_ptr = NULL;
2693                 }
2694                 entry->mem_size = 0;
2695         }
2696
2697         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2698
2699         /* Read the number of the memories which will dump */
2700         stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2701         if (stat == RDWR_STATUS_FAILURE)
2702                 return;
2703
2704         reg = creg->fw_dump_start;
2705         mwifiex_read_reg_byte(adapter, reg, &fw_dump_num);
2706
2707         /* W8997 chipset firmware dump will be restore in single region*/
2708         if (fw_dump_num == 0)
2709                 dump_num = 1;
2710         else
2711                 dump_num = fw_dump_num;
2712
2713         /* Read the length of every memory which will dump */
2714         for (idx = 0; idx < dump_num; idx++) {
2715                 struct memory_type_mapping *entry =
2716                                 &adapter->mem_type_mapping_tbl[idx];
2717                 memory_size = 0;
2718                 if (fw_dump_num != 0) {
2719                         stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2720                         if (stat == RDWR_STATUS_FAILURE)
2721                                 return;
2722
2723                         reg = creg->fw_dump_start;
2724                         for (i = 0; i < 4; i++) {
2725                                 mwifiex_read_reg_byte(adapter, reg, &read_reg);
2726                                 memory_size |= (read_reg << (i * 8));
2727                                 reg++;
2728                         }
2729                 } else {
2730                         memory_size = MWIFIEX_FW_DUMP_MAX_MEMSIZE;
2731                 }
2732
2733                 if (memory_size == 0) {
2734                         mwifiex_dbg(adapter, MSG, "Firmware dump Finished!\n");
2735                         ret = mwifiex_write_reg(adapter, creg->fw_dump_ctrl,
2736                                                 creg->fw_dump_read_done);
2737                         if (ret) {
2738                                 mwifiex_dbg(adapter, ERROR, "PCIE write err\n");
2739                                 return;
2740                         }
2741                         break;
2742                 }
2743
2744                 mwifiex_dbg(adapter, DUMP,
2745                             "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2746                 entry->mem_ptr = vmalloc(memory_size + 1);
2747                 entry->mem_size = memory_size;
2748                 if (!entry->mem_ptr) {
2749                         mwifiex_dbg(adapter, ERROR,
2750                                     "Vmalloc %s failed\n", entry->mem_name);
2751                         return;
2752                 }
2753                 dbg_ptr = entry->mem_ptr;
2754                 end_ptr = dbg_ptr + memory_size;
2755
2756                 doneflag = entry->done_flag;
2757                 mwifiex_dbg(adapter, DUMP, "Start %s output, please wait...\n",
2758                             entry->mem_name);
2759
2760                 do {
2761                         stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2762                         if (RDWR_STATUS_FAILURE == stat)
2763                                 return;
2764
2765                         reg_start = creg->fw_dump_start;
2766                         reg_end = creg->fw_dump_end;
2767                         for (reg = reg_start; reg <= reg_end; reg++) {
2768                                 mwifiex_read_reg_byte(adapter, reg, dbg_ptr);
2769                                 if (dbg_ptr < end_ptr) {
2770                                         dbg_ptr++;
2771                                         continue;
2772                                 }
2773                                 mwifiex_dbg(adapter, ERROR,
2774                                             "pre-allocated buf not enough\n");
2775                                 tmp_ptr =
2776                                         vzalloc(memory_size + MWIFIEX_SIZE_4K);
2777                                 if (!tmp_ptr)
2778                                         return;
2779                                 memcpy(tmp_ptr, entry->mem_ptr, memory_size);
2780                                 vfree(entry->mem_ptr);
2781                                 entry->mem_ptr = tmp_ptr;
2782                                 tmp_ptr = NULL;
2783                                 dbg_ptr = entry->mem_ptr + memory_size;
2784                                 memory_size += MWIFIEX_SIZE_4K;
2785                                 end_ptr = entry->mem_ptr + memory_size;
2786                         }
2787
2788                         if (stat != RDWR_STATUS_DONE)
2789                                 continue;
2790
2791                         mwifiex_dbg(adapter, DUMP,
2792                                     "%s done: size=0x%tx\n",
2793                                     entry->mem_name, dbg_ptr - entry->mem_ptr);
2794                         break;
2795                 } while (true);
2796         }
2797         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2798 }
2799
2800 static void mwifiex_pcie_device_dump_work(struct mwifiex_adapter *adapter)
2801 {
2802         adapter->devdump_data = vzalloc(MWIFIEX_FW_DUMP_SIZE);
2803         if (!adapter->devdump_data) {
2804                 mwifiex_dbg(adapter, ERROR,
2805                             "vzalloc devdump data failure!\n");
2806                 return;
2807         }
2808
2809         mwifiex_drv_info_dump(adapter);
2810         mwifiex_pcie_fw_dump(adapter);
2811         mwifiex_prepare_fw_dump_info(adapter);
2812         mwifiex_upload_device_dump(adapter);
2813 }
2814
2815 static void mwifiex_pcie_card_reset_work(struct mwifiex_adapter *adapter)
2816 {
2817         struct pcie_service_card *card = adapter->card;
2818
2819         /* We can't afford to wait here; remove() might be waiting on us. If we
2820          * can't grab the device lock, maybe we'll get another chance later.
2821          */
2822         pci_try_reset_function(card->dev);
2823 }
2824
2825 static void mwifiex_pcie_work(struct work_struct *work)
2826 {
2827         struct pcie_service_card *card =
2828                 container_of(work, struct pcie_service_card, work);
2829
2830         if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2831                                &card->work_flags))
2832                 mwifiex_pcie_device_dump_work(card->adapter);
2833         if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2834                                &card->work_flags))
2835                 mwifiex_pcie_card_reset_work(card->adapter);
2836 }
2837
2838 /* This function dumps FW information */
2839 static void mwifiex_pcie_device_dump(struct mwifiex_adapter *adapter)
2840 {
2841         struct pcie_service_card *card = adapter->card;
2842
2843         if (!test_and_set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2844                               &card->work_flags))
2845                 schedule_work(&card->work);
2846 }
2847
2848 static void mwifiex_pcie_card_reset(struct mwifiex_adapter *adapter)
2849 {
2850         struct pcie_service_card *card = adapter->card;
2851
2852         if (!test_and_set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags))
2853                 schedule_work(&card->work);
2854 }
2855
2856 static int mwifiex_pcie_alloc_buffers(struct mwifiex_adapter *adapter)
2857 {
2858         struct pcie_service_card *card = adapter->card;
2859         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2860         int ret;
2861
2862         card->cmdrsp_buf = NULL;
2863         ret = mwifiex_pcie_create_txbd_ring(adapter);
2864         if (ret) {
2865                 mwifiex_dbg(adapter, ERROR, "Failed to create txbd ring\n");
2866                 goto err_cre_txbd;
2867         }
2868
2869         ret = mwifiex_pcie_create_rxbd_ring(adapter);
2870         if (ret) {
2871                 mwifiex_dbg(adapter, ERROR, "Failed to create rxbd ring\n");
2872                 goto err_cre_rxbd;
2873         }
2874
2875         ret = mwifiex_pcie_create_evtbd_ring(adapter);
2876         if (ret) {
2877                 mwifiex_dbg(adapter, ERROR, "Failed to create evtbd ring\n");
2878                 goto err_cre_evtbd;
2879         }
2880
2881         ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
2882         if (ret) {
2883                 mwifiex_dbg(adapter, ERROR, "Failed to allocate cmdbuf buffer\n");
2884                 goto err_alloc_cmdbuf;
2885         }
2886
2887         if (reg->sleep_cookie) {
2888                 ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
2889                 if (ret) {
2890                         mwifiex_dbg(adapter, ERROR, "Failed to allocate sleep_cookie buffer\n");
2891                         goto err_alloc_cookie;
2892                 }
2893         } else {
2894                 card->sleep_cookie_vbase = NULL;
2895         }
2896
2897         return 0;
2898
2899 err_alloc_cookie:
2900         mwifiex_pcie_delete_cmdrsp_buf(adapter);
2901 err_alloc_cmdbuf:
2902         mwifiex_pcie_delete_evtbd_ring(adapter);
2903 err_cre_evtbd:
2904         mwifiex_pcie_delete_rxbd_ring(adapter);
2905 err_cre_rxbd:
2906         mwifiex_pcie_delete_txbd_ring(adapter);
2907 err_cre_txbd:
2908         return ret;
2909 }
2910
2911 static void mwifiex_pcie_free_buffers(struct mwifiex_adapter *adapter)
2912 {
2913         struct pcie_service_card *card = adapter->card;
2914         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2915
2916         if (reg->sleep_cookie)
2917                 mwifiex_pcie_delete_sleep_cookie_buf(adapter);
2918
2919         mwifiex_pcie_delete_cmdrsp_buf(adapter);
2920         mwifiex_pcie_delete_evtbd_ring(adapter);
2921         mwifiex_pcie_delete_rxbd_ring(adapter);
2922         mwifiex_pcie_delete_txbd_ring(adapter);
2923 }
2924
2925 /*
2926  * This function initializes the PCI-E host memory space, WCB rings, etc.
2927  */
2928 static int mwifiex_init_pcie(struct mwifiex_adapter *adapter)
2929 {
2930         struct pcie_service_card *card = adapter->card;
2931         int ret;
2932         struct pci_dev *pdev = card->dev;
2933
2934         pci_set_drvdata(pdev, card);
2935
2936         ret = pci_enable_device(pdev);
2937         if (ret)
2938                 goto err_enable_dev;
2939
2940         pci_set_master(pdev);
2941
2942         pr_notice("try set_consistent_dma_mask(32)\n");
2943         ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2944         if (ret) {
2945                 pr_err("set_dma_mask(32) failed\n");
2946                 goto err_set_dma_mask;
2947         }
2948
2949         ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2950         if (ret) {
2951                 pr_err("set_consistent_dma_mask(64) failed\n");
2952                 goto err_set_dma_mask;
2953         }
2954
2955         ret = pci_request_region(pdev, 0, DRV_NAME);
2956         if (ret) {
2957                 pr_err("req_reg(0) error\n");
2958                 goto err_req_region0;
2959         }
2960         card->pci_mmap = pci_iomap(pdev, 0, 0);
2961         if (!card->pci_mmap) {
2962                 pr_err("iomap(0) error\n");
2963                 ret = -EIO;
2964                 goto err_iomap0;
2965         }
2966         ret = pci_request_region(pdev, 2, DRV_NAME);
2967         if (ret) {
2968                 pr_err("req_reg(2) error\n");
2969                 goto err_req_region2;
2970         }
2971         card->pci_mmap1 = pci_iomap(pdev, 2, 0);
2972         if (!card->pci_mmap1) {
2973                 pr_err("iomap(2) error\n");
2974                 ret = -EIO;
2975                 goto err_iomap2;
2976         }
2977
2978         pr_notice("PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
2979                   card->pci_mmap, card->pci_mmap1);
2980
2981         ret = mwifiex_pcie_alloc_buffers(adapter);
2982         if (ret)
2983                 goto err_alloc_buffers;
2984
2985         return 0;
2986
2987 err_alloc_buffers:
2988         pci_iounmap(pdev, card->pci_mmap1);
2989 err_iomap2:
2990         pci_release_region(pdev, 2);
2991 err_req_region2:
2992         pci_iounmap(pdev, card->pci_mmap);
2993 err_iomap0:
2994         pci_release_region(pdev, 0);
2995 err_req_region0:
2996 err_set_dma_mask:
2997         pci_disable_device(pdev);
2998 err_enable_dev:
2999         return ret;
3000 }
3001
3002 /*
3003  * This function cleans up the allocated card buffers.
3004  */
3005 static void mwifiex_cleanup_pcie(struct mwifiex_adapter *adapter)
3006 {
3007         struct pcie_service_card *card = adapter->card;
3008         struct pci_dev *pdev = card->dev;
3009         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
3010         int ret;
3011         u32 fw_status;
3012
3013         /* Perform the cancel_work_sync() only when we're not resetting
3014          * the card. It's because that function never returns if we're
3015          * in reset path. If we're here when resetting the card, it means
3016          * that we failed to reset the card (reset failure path).
3017          */
3018         if (!card->pci_reset_ongoing) {
3019                 mwifiex_dbg(adapter, MSG, "performing cancel_work_sync()...\n");
3020                 cancel_work_sync(&card->work);
3021                 mwifiex_dbg(adapter, MSG, "cancel_work_sync() done\n");
3022         } else {
3023                 mwifiex_dbg(adapter, MSG,
3024                             "skipped cancel_work_sync() because we're in card reset failure path\n");
3025         }
3026
3027         ret = mwifiex_read_reg(adapter, reg->fw_status, &fw_status);
3028         if (fw_status == FIRMWARE_READY_PCIE) {
3029                 mwifiex_dbg(adapter, INFO,
3030                             "Clearing driver ready signature\n");
3031                 if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
3032                         mwifiex_dbg(adapter, ERROR,
3033                                     "Failed to write driver not-ready signature\n");
3034         }
3035
3036         pci_disable_device(pdev);
3037
3038         pci_iounmap(pdev, card->pci_mmap);
3039         pci_iounmap(pdev, card->pci_mmap1);
3040         pci_release_region(pdev, 2);
3041         pci_release_region(pdev, 0);
3042
3043         mwifiex_pcie_free_buffers(adapter);
3044 }
3045
3046 static int mwifiex_pcie_request_irq(struct mwifiex_adapter *adapter)
3047 {
3048         int ret, i, j;
3049         struct pcie_service_card *card = adapter->card;
3050         struct pci_dev *pdev = card->dev;
3051
3052         if (card->pcie.reg->msix_support) {
3053                 for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++)
3054                         card->msix_entries[i].entry = i;
3055                 ret = pci_enable_msix_exact(pdev, card->msix_entries,
3056                                             MWIFIEX_NUM_MSIX_VECTORS);
3057                 if (!ret) {
3058                         for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++) {
3059                                 card->msix_ctx[i].dev = pdev;
3060                                 card->msix_ctx[i].msg_id = i;
3061
3062                                 ret = request_irq(card->msix_entries[i].vector,
3063                                                   mwifiex_pcie_interrupt, 0,
3064                                                   "MWIFIEX_PCIE_MSIX",
3065                                                   &card->msix_ctx[i]);
3066                                 if (ret)
3067                                         break;
3068                         }
3069
3070                         if (ret) {
3071                                 mwifiex_dbg(adapter, INFO, "request_irq fail: %d\n",
3072                                             ret);
3073                                 for (j = 0; j < i; j++)
3074                                         free_irq(card->msix_entries[j].vector,
3075                                                  &card->msix_ctx[i]);
3076                                 pci_disable_msix(pdev);
3077                         } else {
3078                                 mwifiex_dbg(adapter, MSG, "MSIx enabled!");
3079                                 card->msix_enable = 1;
3080                                 return 0;
3081                         }
3082                 }
3083         }
3084
3085         if (pci_enable_msi(pdev) != 0)
3086                 pci_disable_msi(pdev);
3087         else
3088                 card->msi_enable = 1;
3089
3090         mwifiex_dbg(adapter, INFO, "msi_enable = %d\n", card->msi_enable);
3091
3092         card->share_irq_ctx.dev = pdev;
3093         card->share_irq_ctx.msg_id = -1;
3094         ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
3095                           "MRVL_PCIE", &card->share_irq_ctx);
3096         if (ret) {
3097                 pr_err("request_irq failed: ret=%d\n", ret);
3098                 return -1;
3099         }
3100
3101         return 0;
3102 }
3103
3104 /*
3105  * This function gets the firmware name for downloading by revision id
3106  *
3107  * Read revision id register to get revision id
3108  */
3109 static void mwifiex_pcie_get_fw_name(struct mwifiex_adapter *adapter)
3110 {
3111         int revision_id = 0;
3112         int version, magic;
3113         struct pcie_service_card *card = adapter->card;
3114
3115         switch (card->dev->device) {
3116         case PCIE_DEVICE_ID_MARVELL_88W8766P:
3117                 strcpy(adapter->fw_name, PCIE8766_DEFAULT_FW_NAME);
3118                 break;
3119         case PCIE_DEVICE_ID_MARVELL_88W8897:
3120                 mwifiex_write_reg(adapter, 0x0c58, 0x80c00000);
3121                 mwifiex_read_reg(adapter, 0x0c58, &revision_id);
3122                 revision_id &= 0xff00;
3123                 switch (revision_id) {
3124                 case PCIE8897_A0:
3125                         strcpy(adapter->fw_name, PCIE8897_A0_FW_NAME);
3126                         break;
3127                 case PCIE8897_B0:
3128                         strcpy(adapter->fw_name, PCIE8897_B0_FW_NAME);
3129                         break;
3130                 default:
3131                         strcpy(adapter->fw_name, PCIE8897_DEFAULT_FW_NAME);
3132
3133                         break;
3134                 }
3135                 break;
3136         case PCIE_DEVICE_ID_MARVELL_88W8997:
3137                 mwifiex_read_reg(adapter, 0x8, &revision_id);
3138                 mwifiex_read_reg(adapter, 0x0cd0, &version);
3139                 mwifiex_read_reg(adapter, 0x0cd4, &magic);
3140                 revision_id &= 0xff;
3141                 version &= 0x7;
3142                 magic &= 0xff;
3143                 if (revision_id == PCIE8997_A1 &&
3144                     magic == CHIP_MAGIC_VALUE &&
3145                     version == CHIP_VER_PCIEUART)
3146                         strcpy(adapter->fw_name, PCIEUART8997_FW_NAME_V4);
3147                 else
3148                         strcpy(adapter->fw_name, PCIEUSB8997_FW_NAME_V4);
3149                 break;
3150         default:
3151                 break;
3152         }
3153 }
3154
3155 /*
3156  * This function registers the PCIE device.
3157  *
3158  * PCIE IRQ is claimed, block size is set and driver data is initialized.
3159  */
3160 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
3161 {
3162         struct pcie_service_card *card = adapter->card;
3163
3164         /* save adapter pointer in card */
3165         card->adapter = adapter;
3166
3167         if (mwifiex_pcie_request_irq(adapter))
3168                 return -1;
3169
3170         adapter->tx_buf_size = card->pcie.tx_buf_size;
3171         adapter->mem_type_mapping_tbl = card->pcie.mem_type_mapping_tbl;
3172         adapter->num_mem_types = card->pcie.num_mem_types;
3173         adapter->ext_scan = card->pcie.can_ext_scan;
3174         mwifiex_pcie_get_fw_name(adapter);
3175
3176         return 0;
3177 }
3178
3179 /*
3180  * This function unregisters the PCIE device.
3181  *
3182  * The PCIE IRQ is released, the function is disabled and driver
3183  * data is set to null.
3184  */
3185 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
3186 {
3187         struct pcie_service_card *card = adapter->card;
3188         struct pci_dev *pdev = card->dev;
3189         int i;
3190
3191         if (card->msix_enable) {
3192                 for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++)
3193                         synchronize_irq(card->msix_entries[i].vector);
3194
3195                 for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++)
3196                         free_irq(card->msix_entries[i].vector,
3197                                  &card->msix_ctx[i]);
3198
3199                 card->msix_enable = 0;
3200                 pci_disable_msix(pdev);
3201         } else {
3202                 mwifiex_dbg(adapter, INFO,
3203                             "%s(): calling free_irq()\n", __func__);
3204                free_irq(card->dev->irq, &card->share_irq_ctx);
3205
3206                 if (card->msi_enable)
3207                         pci_disable_msi(pdev);
3208         }
3209         card->adapter = NULL;
3210 }
3211
3212 /*
3213  * This function initializes the PCI-E host memory space, WCB rings, etc.,
3214  * similar to mwifiex_init_pcie(), but without resetting PCI-E state.
3215  */
3216 static void mwifiex_pcie_up_dev(struct mwifiex_adapter *adapter)
3217 {
3218         struct pcie_service_card *card = adapter->card;
3219         struct pci_dev *pdev = card->dev;
3220
3221         /* tx_buf_size might be changed to 3584 by firmware during
3222          * data transfer, we should reset it to default size.
3223          */
3224         adapter->tx_buf_size = card->pcie.tx_buf_size;
3225
3226         mwifiex_pcie_alloc_buffers(adapter);
3227
3228         pci_set_master(pdev);
3229 }
3230
3231 /* This function cleans up the PCI-E host memory space. */
3232 static void mwifiex_pcie_down_dev(struct mwifiex_adapter *adapter)
3233 {
3234         struct pcie_service_card *card = adapter->card;
3235         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
3236         struct pci_dev *pdev = card->dev;
3237
3238         if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
3239                 mwifiex_dbg(adapter, ERROR, "Failed to write driver not-ready signature\n");
3240
3241         pci_clear_master(pdev);
3242
3243         adapter->seq_num = 0;
3244
3245         mwifiex_pcie_free_buffers(adapter);
3246 }
3247
3248 static struct mwifiex_if_ops pcie_ops = {
3249         .init_if =                      mwifiex_init_pcie,
3250         .cleanup_if =                   mwifiex_cleanup_pcie,
3251         .check_fw_status =              mwifiex_check_fw_status,
3252         .check_winner_status =          mwifiex_check_winner_status,
3253         .prog_fw =                      mwifiex_prog_fw_w_helper,
3254         .register_dev =                 mwifiex_register_dev,
3255         .unregister_dev =               mwifiex_unregister_dev,
3256         .enable_int =                   mwifiex_pcie_enable_host_int,
3257         .disable_int =                  mwifiex_pcie_disable_host_int_noerr,
3258         .process_int_status =           mwifiex_process_int_status,
3259         .host_to_card =                 mwifiex_pcie_host_to_card,
3260         .wakeup =                       mwifiex_pm_wakeup_card,
3261         .wakeup_complete =              mwifiex_pm_wakeup_card_complete,
3262
3263         /* PCIE specific */
3264         .cmdrsp_complete =              mwifiex_pcie_cmdrsp_complete,
3265         .event_complete =               mwifiex_pcie_event_complete,
3266         .update_mp_end_port =           NULL,
3267         .cleanup_mpa_buf =              NULL,
3268         .init_fw_port =                 mwifiex_pcie_init_fw_port,
3269         .clean_pcie_ring =              mwifiex_clean_pcie_ring_buf,
3270         .card_reset =                   mwifiex_pcie_card_reset,
3271         .reg_dump =                     mwifiex_pcie_reg_dump,
3272         .device_dump =                  mwifiex_pcie_device_dump,
3273         .down_dev =                     mwifiex_pcie_down_dev,
3274         .up_dev =                       mwifiex_pcie_up_dev,
3275 };
3276
3277 module_pci_driver(mwifiex_pcie);
3278
3279 MODULE_AUTHOR("Marvell International Ltd.");
3280 MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
3281 MODULE_VERSION(PCIE_VERSION);
3282 MODULE_LICENSE("GPL v2");