GNU Linux-libre 4.19.207-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                 if ((mwifiex_pcie_txbd_not_full(card)) &&
1334                     tx_param->next_pkt_len) {
1335                         /* have more packets and TxBD still can hold more */
1336                         mwifiex_dbg(adapter, DATA,
1337                                     "SEND DATA: delay dnld-rdy interrupt.\n");
1338                         adapter->data_sent = false;
1339                 } else {
1340                         /* Send the TX ready interrupt */
1341                         if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1342                                               CPU_INTR_DNLD_RDY)) {
1343                                 mwifiex_dbg(adapter, ERROR,
1344                                             "SEND DATA: failed to assert dnld-rdy interrupt.\n");
1345                                 ret = -1;
1346                                 goto done_unmap;
1347                         }
1348                 }
1349                 mwifiex_dbg(adapter, DATA,
1350                             "info: SEND DATA: Updated <Rd: %#x, Wr:\t"
1351                             "%#x> and sent packet to firmware successfully\n",
1352                             card->txbd_rdptr, card->txbd_wrptr);
1353         } else {
1354                 mwifiex_dbg(adapter, DATA,
1355                             "info: TX Ring full, can't send packets to fw\n");
1356                 adapter->data_sent = true;
1357                 /* Send the TX ready interrupt */
1358                 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1359                                       CPU_INTR_DNLD_RDY))
1360                         mwifiex_dbg(adapter, ERROR,
1361                                     "SEND DATA: failed to assert door-bell intr\n");
1362                 return -EBUSY;
1363         }
1364
1365         return -EINPROGRESS;
1366 done_unmap:
1367         mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1368         card->tx_buf_list[wrindx] = NULL;
1369         atomic_dec(&adapter->tx_hw_pending);
1370         if (reg->pfu_enabled)
1371                 memset(desc2, 0, sizeof(*desc2));
1372         else
1373                 memset(desc, 0, sizeof(*desc));
1374
1375         return ret;
1376 }
1377
1378 /*
1379  * This function handles received buffer ring and
1380  * dispatches packets to upper
1381  */
1382 static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
1383 {
1384         struct pcie_service_card *card = adapter->card;
1385         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1386         u32 wrptr, rd_index, tx_val;
1387         dma_addr_t buf_pa;
1388         int ret = 0;
1389         struct sk_buff *skb_tmp = NULL;
1390         struct mwifiex_pcie_buf_desc *desc;
1391         struct mwifiex_pfu_buf_desc *desc2;
1392
1393         if (!mwifiex_pcie_ok_to_access_hw(adapter))
1394                 mwifiex_pm_wakeup_card(adapter);
1395
1396         /* Read the RX ring Write pointer set by firmware */
1397         if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1398                 mwifiex_dbg(adapter, ERROR,
1399                             "RECV DATA: failed to read reg->rx_wrptr\n");
1400                 ret = -1;
1401                 goto done;
1402         }
1403         card->rxbd_wrptr = wrptr;
1404
1405         while (((wrptr & reg->rx_mask) !=
1406                 (card->rxbd_rdptr & reg->rx_mask)) ||
1407                ((wrptr & reg->rx_rollover_ind) ==
1408                 (card->rxbd_rdptr & reg->rx_rollover_ind))) {
1409                 struct sk_buff *skb_data;
1410                 u16 rx_len;
1411
1412                 rd_index = card->rxbd_rdptr & reg->rx_mask;
1413                 skb_data = card->rx_buf_list[rd_index];
1414
1415                 /* If skb allocation was failed earlier for Rx packet,
1416                  * rx_buf_list[rd_index] would have been left with a NULL.
1417                  */
1418                 if (!skb_data)
1419                         return -ENOMEM;
1420
1421                 mwifiex_unmap_pci_memory(adapter, skb_data, PCI_DMA_FROMDEVICE);
1422                 card->rx_buf_list[rd_index] = NULL;
1423
1424                 /* Get data length from interface header -
1425                  * first 2 bytes for len, next 2 bytes is for type
1426                  */
1427                 rx_len = get_unaligned_le16(skb_data->data);
1428                 if (WARN_ON(rx_len <= adapter->intf_hdr_len ||
1429                             rx_len > MWIFIEX_RX_DATA_BUF_SIZE)) {
1430                         mwifiex_dbg(adapter, ERROR,
1431                                     "Invalid RX len %d, Rd=%#x, Wr=%#x\n",
1432                                     rx_len, card->rxbd_rdptr, wrptr);
1433                         dev_kfree_skb_any(skb_data);
1434                 } else {
1435                         skb_put(skb_data, rx_len);
1436                         mwifiex_dbg(adapter, DATA,
1437                                     "info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
1438                                     card->rxbd_rdptr, wrptr, rx_len);
1439                         skb_pull(skb_data, adapter->intf_hdr_len);
1440                         if (adapter->rx_work_enabled) {
1441                                 skb_queue_tail(&adapter->rx_data_q, skb_data);
1442                                 adapter->data_received = true;
1443                                 atomic_inc(&adapter->rx_pending);
1444                         } else {
1445                                 mwifiex_handle_rx_packet(adapter, skb_data);
1446                         }
1447                 }
1448
1449                 skb_tmp = mwifiex_alloc_dma_align_buf(MWIFIEX_RX_DATA_BUF_SIZE,
1450                                                       GFP_KERNEL);
1451                 if (!skb_tmp) {
1452                         mwifiex_dbg(adapter, ERROR,
1453                                     "Unable to allocate skb.\n");
1454                         return -ENOMEM;
1455                 }
1456
1457                 if (mwifiex_map_pci_memory(adapter, skb_tmp,
1458                                            MWIFIEX_RX_DATA_BUF_SIZE,
1459                                            PCI_DMA_FROMDEVICE))
1460                         return -1;
1461
1462                 buf_pa = MWIFIEX_SKB_DMA_ADDR(skb_tmp);
1463
1464                 mwifiex_dbg(adapter, INFO,
1465                             "RECV DATA: Attach new sk_buff %p at rxbd_rdidx=%d\n",
1466                             skb_tmp, rd_index);
1467                 card->rx_buf_list[rd_index] = skb_tmp;
1468
1469                 if (reg->pfu_enabled) {
1470                         desc2 = card->rxbd_ring[rd_index];
1471                         desc2->paddr = buf_pa;
1472                         desc2->len = skb_tmp->len;
1473                         desc2->frag_len = skb_tmp->len;
1474                         desc2->offset = 0;
1475                         desc2->flags = reg->ring_flag_sop | reg->ring_flag_eop;
1476                 } else {
1477                         desc = card->rxbd_ring[rd_index];
1478                         desc->paddr = buf_pa;
1479                         desc->len = skb_tmp->len;
1480                         desc->flags = 0;
1481                 }
1482
1483                 if ((++card->rxbd_rdptr & reg->rx_mask) ==
1484                                                         MWIFIEX_MAX_TXRX_BD) {
1485                         card->rxbd_rdptr = ((card->rxbd_rdptr &
1486                                              reg->rx_rollover_ind) ^
1487                                              reg->rx_rollover_ind);
1488                 }
1489                 mwifiex_dbg(adapter, DATA,
1490                             "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
1491                             card->rxbd_rdptr, wrptr);
1492
1493                 tx_val = card->txbd_wrptr & reg->tx_wrap_mask;
1494                 /* Write the RX ring read pointer in to reg->rx_rdptr */
1495                 if (mwifiex_write_reg(adapter, reg->rx_rdptr,
1496                                       card->rxbd_rdptr | tx_val)) {
1497                         mwifiex_dbg(adapter, DATA,
1498                                     "RECV DATA: failed to write reg->rx_rdptr\n");
1499                         ret = -1;
1500                         goto done;
1501                 }
1502
1503                 /* Read the RX ring Write pointer set by firmware */
1504                 if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1505                         mwifiex_dbg(adapter, ERROR,
1506                                     "RECV DATA: failed to read reg->rx_wrptr\n");
1507                         ret = -1;
1508                         goto done;
1509                 }
1510                 mwifiex_dbg(adapter, DATA,
1511                             "info: RECV DATA: Rcvd packet from fw successfully\n");
1512                 card->rxbd_wrptr = wrptr;
1513         }
1514
1515 done:
1516         return ret;
1517 }
1518
1519 /*
1520  * This function downloads the boot command to device
1521  */
1522 static int
1523 mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1524 {
1525         dma_addr_t buf_pa;
1526         struct pcie_service_card *card = adapter->card;
1527         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1528
1529         if (!(skb->data && skb->len)) {
1530                 mwifiex_dbg(adapter, ERROR,
1531                             "Invalid parameter in %s <%p. len %d>\n",
1532                             __func__, skb->data, skb->len);
1533                 return -1;
1534         }
1535
1536         if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1537                 return -1;
1538
1539         buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1540
1541         /* Write the lower 32bits of the physical address to low command
1542          * address scratch register
1543          */
1544         if (mwifiex_write_reg(adapter, reg->cmd_addr_lo, (u32)buf_pa)) {
1545                 mwifiex_dbg(adapter, ERROR,
1546                             "%s: failed to write download command to boot code.\n",
1547                             __func__);
1548                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1549                 return -1;
1550         }
1551
1552         /* Write the upper 32bits of the physical address to high command
1553          * address scratch register
1554          */
1555         if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1556                               (u32)((u64)buf_pa >> 32))) {
1557                 mwifiex_dbg(adapter, ERROR,
1558                             "%s: failed to write download command to boot code.\n",
1559                             __func__);
1560                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1561                 return -1;
1562         }
1563
1564         /* Write the command length to cmd_size scratch register */
1565         if (mwifiex_write_reg(adapter, reg->cmd_size, skb->len)) {
1566                 mwifiex_dbg(adapter, ERROR,
1567                             "%s: failed to write command len to cmd_size scratch reg\n",
1568                             __func__);
1569                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1570                 return -1;
1571         }
1572
1573         /* Ring the door bell */
1574         if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1575                               CPU_INTR_DOOR_BELL)) {
1576                 mwifiex_dbg(adapter, ERROR,
1577                             "%s: failed to assert door-bell intr\n", __func__);
1578                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1579                 return -1;
1580         }
1581
1582         return 0;
1583 }
1584
1585 /* This function init rx port in firmware which in turn enables to receive data
1586  * from device before transmitting any packet.
1587  */
1588 static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
1589 {
1590         struct pcie_service_card *card = adapter->card;
1591         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1592         int tx_wrap = card->txbd_wrptr & reg->tx_wrap_mask;
1593
1594         /* Write the RX ring read pointer in to reg->rx_rdptr */
1595         if (mwifiex_write_reg(adapter, reg->rx_rdptr, card->rxbd_rdptr |
1596                               tx_wrap)) {
1597                 mwifiex_dbg(adapter, ERROR,
1598                             "RECV DATA: failed to write reg->rx_rdptr\n");
1599                 return -1;
1600         }
1601         return 0;
1602 }
1603
1604 /* This function downloads commands to the device
1605  */
1606 static int
1607 mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1608 {
1609         struct pcie_service_card *card = adapter->card;
1610         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1611         int ret = 0;
1612         dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
1613         u8 *payload = (u8 *)skb->data;
1614
1615         if (!(skb->data && skb->len)) {
1616                 mwifiex_dbg(adapter, ERROR,
1617                             "Invalid parameter in %s <%p, %#x>\n",
1618                             __func__, skb->data, skb->len);
1619                 return -1;
1620         }
1621
1622         /* Make sure a command response buffer is available */
1623         if (!card->cmdrsp_buf) {
1624                 mwifiex_dbg(adapter, ERROR,
1625                             "No response buffer available, send command failed\n");
1626                 return -EBUSY;
1627         }
1628
1629         if (!mwifiex_pcie_ok_to_access_hw(adapter))
1630                 mwifiex_pm_wakeup_card(adapter);
1631
1632         adapter->cmd_sent = true;
1633
1634         put_unaligned_le16((u16)skb->len, &payload[0]);
1635         put_unaligned_le16(MWIFIEX_TYPE_CMD, &payload[2]);
1636
1637         if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1638                 return -1;
1639
1640         card->cmd_buf = skb;
1641         /*
1642          * Need to keep a reference, since core driver might free up this
1643          * buffer before we've unmapped it.
1644          */
1645         skb_get(skb);
1646
1647         /* To send a command, the driver will:
1648                 1. Write the 64bit physical address of the data buffer to
1649                    cmd response address low  + cmd response address high
1650                 2. Ring the door bell (i.e. set the door bell interrupt)
1651
1652                 In response to door bell interrupt, the firmware will perform
1653                 the DMA of the command packet (first header to obtain the total
1654                 length and then rest of the command).
1655         */
1656
1657         if (card->cmdrsp_buf) {
1658                 cmdrsp_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmdrsp_buf);
1659                 /* Write the lower 32bits of the cmdrsp buffer physical
1660                    address */
1661                 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo,
1662                                       (u32)cmdrsp_buf_pa)) {
1663                         mwifiex_dbg(adapter, ERROR,
1664                                     "Failed to write download cmd to boot code.\n");
1665                         ret = -1;
1666                         goto done;
1667                 }
1668                 /* Write the upper 32bits of the cmdrsp buffer physical
1669                    address */
1670                 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi,
1671                                       (u32)((u64)cmdrsp_buf_pa >> 32))) {
1672                         mwifiex_dbg(adapter, ERROR,
1673                                     "Failed to write download cmd to boot code.\n");
1674                         ret = -1;
1675                         goto done;
1676                 }
1677         }
1678
1679         cmd_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmd_buf);
1680         /* Write the lower 32bits of the physical address to reg->cmd_addr_lo */
1681         if (mwifiex_write_reg(adapter, reg->cmd_addr_lo,
1682                               (u32)cmd_buf_pa)) {
1683                 mwifiex_dbg(adapter, ERROR,
1684                             "Failed to write download cmd to boot code.\n");
1685                 ret = -1;
1686                 goto done;
1687         }
1688         /* Write the upper 32bits of the physical address to reg->cmd_addr_hi */
1689         if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1690                               (u32)((u64)cmd_buf_pa >> 32))) {
1691                 mwifiex_dbg(adapter, ERROR,
1692                             "Failed to write download cmd to boot code.\n");
1693                 ret = -1;
1694                 goto done;
1695         }
1696
1697         /* Write the command length to reg->cmd_size */
1698         if (mwifiex_write_reg(adapter, reg->cmd_size,
1699                               card->cmd_buf->len)) {
1700                 mwifiex_dbg(adapter, ERROR,
1701                             "Failed to write cmd len to reg->cmd_size\n");
1702                 ret = -1;
1703                 goto done;
1704         }
1705
1706         /* Ring the door bell */
1707         if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1708                               CPU_INTR_DOOR_BELL)) {
1709                 mwifiex_dbg(adapter, ERROR,
1710                             "Failed to assert door-bell intr\n");
1711                 ret = -1;
1712                 goto done;
1713         }
1714
1715 done:
1716         if (ret)
1717                 adapter->cmd_sent = false;
1718
1719         return 0;
1720 }
1721
1722 /*
1723  * This function handles command complete interrupt
1724  */
1725 static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1726 {
1727         struct pcie_service_card *card = adapter->card;
1728         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1729         struct sk_buff *skb = card->cmdrsp_buf;
1730         int count = 0;
1731         u16 rx_len;
1732
1733         mwifiex_dbg(adapter, CMD,
1734                     "info: Rx CMD Response\n");
1735
1736         if (adapter->curr_cmd)
1737                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_FROMDEVICE);
1738         else
1739                 pci_dma_sync_single_for_cpu(card->dev,
1740                                             MWIFIEX_SKB_DMA_ADDR(skb),
1741                                             MWIFIEX_UPLD_SIZE,
1742                                             PCI_DMA_FROMDEVICE);
1743
1744         /* Unmap the command as a response has been received. */
1745         if (card->cmd_buf) {
1746                 mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
1747                                          PCI_DMA_TODEVICE);
1748                 dev_kfree_skb_any(card->cmd_buf);
1749                 card->cmd_buf = NULL;
1750         }
1751
1752         rx_len = get_unaligned_le16(skb->data);
1753         skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1754         skb_trim(skb, rx_len);
1755
1756         if (!adapter->curr_cmd) {
1757                 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
1758                         pci_dma_sync_single_for_device(card->dev,
1759                                                 MWIFIEX_SKB_DMA_ADDR(skb),
1760                                                 MWIFIEX_SLEEP_COOKIE_SIZE,
1761                                                 PCI_DMA_FROMDEVICE);
1762                         if (mwifiex_write_reg(adapter,
1763                                               PCIE_CPU_INT_EVENT,
1764                                               CPU_INTR_SLEEP_CFM_DONE)) {
1765                                 mwifiex_dbg(adapter, ERROR,
1766                                             "Write register failed\n");
1767                                 return -1;
1768                         }
1769                         mwifiex_delay_for_sleep_cookie(adapter,
1770                                                        MWIFIEX_MAX_DELAY_COUNT);
1771                         mwifiex_unmap_pci_memory(adapter, skb,
1772                                                  PCI_DMA_FROMDEVICE);
1773                         skb_pull(skb, adapter->intf_hdr_len);
1774                         while (reg->sleep_cookie && (count++ < 10) &&
1775                                mwifiex_pcie_ok_to_access_hw(adapter))
1776                                 usleep_range(50, 60);
1777                         mwifiex_pcie_enable_host_int(adapter);
1778                         mwifiex_process_sleep_confirm_resp(adapter, skb->data,
1779                                                            skb->len);
1780                 } else {
1781                         mwifiex_dbg(adapter, ERROR,
1782                                     "There is no command but got cmdrsp\n");
1783                 }
1784                 memcpy(adapter->upld_buf, skb->data,
1785                        min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
1786                 skb_push(skb, adapter->intf_hdr_len);
1787                 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1788                                            PCI_DMA_FROMDEVICE))
1789                         return -1;
1790         } else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1791                 skb_pull(skb, adapter->intf_hdr_len);
1792                 adapter->curr_cmd->resp_skb = skb;
1793                 adapter->cmd_resp_received = true;
1794                 /* Take the pointer and set it to CMD node and will
1795                    return in the response complete callback */
1796                 card->cmdrsp_buf = NULL;
1797
1798                 /* Clear the cmd-rsp buffer address in scratch registers. This
1799                    will prevent firmware from writing to the same response
1800                    buffer again. */
1801                 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo, 0)) {
1802                         mwifiex_dbg(adapter, ERROR,
1803                                     "cmd_done: failed to clear cmd_rsp_addr_lo\n");
1804                         return -1;
1805                 }
1806                 /* Write the upper 32bits of the cmdrsp buffer physical
1807                    address */
1808                 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi, 0)) {
1809                         mwifiex_dbg(adapter, ERROR,
1810                                     "cmd_done: failed to clear cmd_rsp_addr_hi\n");
1811                         return -1;
1812                 }
1813         }
1814
1815         return 0;
1816 }
1817
1818 /*
1819  * Command Response processing complete handler
1820  */
1821 static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1822                                         struct sk_buff *skb)
1823 {
1824         struct pcie_service_card *card = adapter->card;
1825
1826         if (skb) {
1827                 card->cmdrsp_buf = skb;
1828                 skb_push(card->cmdrsp_buf, adapter->intf_hdr_len);
1829                 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1830                                            PCI_DMA_FROMDEVICE))
1831                         return -1;
1832         }
1833
1834         return 0;
1835 }
1836
1837 /*
1838  * This function handles firmware event ready interrupt
1839  */
1840 static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1841 {
1842         struct pcie_service_card *card = adapter->card;
1843         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1844         u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1845         u32 wrptr, event;
1846         struct mwifiex_evt_buf_desc *desc;
1847
1848         if (!mwifiex_pcie_ok_to_access_hw(adapter))
1849                 mwifiex_pm_wakeup_card(adapter);
1850
1851         if (adapter->event_received) {
1852                 mwifiex_dbg(adapter, EVENT,
1853                             "info: Event being processed,\t"
1854                             "do not process this interrupt just yet\n");
1855                 return 0;
1856         }
1857
1858         if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1859                 mwifiex_dbg(adapter, ERROR,
1860                             "info: Invalid read pointer...\n");
1861                 return -1;
1862         }
1863
1864         /* Read the event ring write pointer set by firmware */
1865         if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1866                 mwifiex_dbg(adapter, ERROR,
1867                             "EventReady: failed to read reg->evt_wrptr\n");
1868                 return -1;
1869         }
1870
1871         mwifiex_dbg(adapter, EVENT,
1872                     "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
1873                     card->evtbd_rdptr, wrptr);
1874         if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
1875                                               & MWIFIEX_EVTBD_MASK)) ||
1876             ((wrptr & reg->evt_rollover_ind) ==
1877              (card->evtbd_rdptr & reg->evt_rollover_ind))) {
1878                 struct sk_buff *skb_cmd;
1879                 __le16 data_len = 0;
1880                 u16 evt_len;
1881
1882                 mwifiex_dbg(adapter, INFO,
1883                             "info: Read Index: %d\n", rdptr);
1884                 skb_cmd = card->evt_buf_list[rdptr];
1885                 mwifiex_unmap_pci_memory(adapter, skb_cmd, PCI_DMA_FROMDEVICE);
1886
1887                 /* Take the pointer and set it to event pointer in adapter
1888                    and will return back after event handling callback */
1889                 card->evt_buf_list[rdptr] = NULL;
1890                 desc = card->evtbd_ring[rdptr];
1891                 memset(desc, 0, sizeof(*desc));
1892
1893                 event = get_unaligned_le32(
1894                         &skb_cmd->data[adapter->intf_hdr_len]);
1895                 adapter->event_cause = event;
1896                 /* The first 4bytes will be the event transfer header
1897                    len is 2 bytes followed by type which is 2 bytes */
1898                 memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1899                 evt_len = le16_to_cpu(data_len);
1900                 skb_trim(skb_cmd, evt_len);
1901                 skb_pull(skb_cmd, adapter->intf_hdr_len);
1902                 mwifiex_dbg(adapter, EVENT,
1903                             "info: Event length: %d\n", evt_len);
1904
1905                 if (evt_len > MWIFIEX_EVENT_HEADER_LEN &&
1906                     evt_len < MAX_EVENT_SIZE)
1907                         memcpy(adapter->event_body, skb_cmd->data +
1908                                MWIFIEX_EVENT_HEADER_LEN, evt_len -
1909                                MWIFIEX_EVENT_HEADER_LEN);
1910
1911                 adapter->event_received = true;
1912                 adapter->event_skb = skb_cmd;
1913
1914                 /* Do not update the event read pointer here, wait till the
1915                    buffer is released. This is just to make things simpler,
1916                    we need to find a better method of managing these buffers.
1917                 */
1918         } else {
1919                 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1920                                       CPU_INTR_EVENT_DONE)) {
1921                         mwifiex_dbg(adapter, ERROR,
1922                                     "Write register failed\n");
1923                         return -1;
1924                 }
1925         }
1926
1927         return 0;
1928 }
1929
1930 /*
1931  * Event processing complete handler
1932  */
1933 static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1934                                        struct sk_buff *skb)
1935 {
1936         struct pcie_service_card *card = adapter->card;
1937         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1938         int ret = 0;
1939         u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1940         u32 wrptr;
1941         struct mwifiex_evt_buf_desc *desc;
1942
1943         if (!skb)
1944                 return 0;
1945
1946         if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1947                 mwifiex_dbg(adapter, ERROR,
1948                             "event_complete: Invalid rdptr 0x%x\n",
1949                             rdptr);
1950                 return -EINVAL;
1951         }
1952
1953         /* Read the event ring write pointer set by firmware */
1954         if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1955                 mwifiex_dbg(adapter, ERROR,
1956                             "event_complete: failed to read reg->evt_wrptr\n");
1957                 return -1;
1958         }
1959
1960         if (!card->evt_buf_list[rdptr]) {
1961                 skb_push(skb, adapter->intf_hdr_len);
1962                 skb_put(skb, MAX_EVENT_SIZE - skb->len);
1963                 if (mwifiex_map_pci_memory(adapter, skb,
1964                                            MAX_EVENT_SIZE,
1965                                            PCI_DMA_FROMDEVICE))
1966                         return -1;
1967                 card->evt_buf_list[rdptr] = skb;
1968                 desc = card->evtbd_ring[rdptr];
1969                 desc->paddr = MWIFIEX_SKB_DMA_ADDR(skb);
1970                 desc->len = (u16)skb->len;
1971                 desc->flags = 0;
1972                 skb = NULL;
1973         } else {
1974                 mwifiex_dbg(adapter, ERROR,
1975                             "info: ERROR: buf still valid at index %d, <%p, %p>\n",
1976                             rdptr, card->evt_buf_list[rdptr], skb);
1977         }
1978
1979         if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
1980                 card->evtbd_rdptr = ((card->evtbd_rdptr &
1981                                         reg->evt_rollover_ind) ^
1982                                         reg->evt_rollover_ind);
1983         }
1984
1985         mwifiex_dbg(adapter, EVENT,
1986                     "info: Updated <Rd: 0x%x, Wr: 0x%x>",
1987                     card->evtbd_rdptr, wrptr);
1988
1989         /* Write the event ring read pointer in to reg->evt_rdptr */
1990         if (mwifiex_write_reg(adapter, reg->evt_rdptr,
1991                               card->evtbd_rdptr)) {
1992                 mwifiex_dbg(adapter, ERROR,
1993                             "event_complete: failed to read reg->evt_rdptr\n");
1994                 return -1;
1995         }
1996
1997         mwifiex_dbg(adapter, EVENT,
1998                     "info: Check Events Again\n");
1999         ret = mwifiex_pcie_process_event_ready(adapter);
2000
2001         return ret;
2002 }
2003
2004 /* Combo firmware image is a combination of
2005  * (1) combo crc heaer, start with CMD5
2006  * (2) bluetooth image, start with CMD7, end with CMD6, data wrapped in CMD1.
2007  * (3) wifi image.
2008  *
2009  * This function bypass the header and bluetooth part, return
2010  * the offset of tail wifi-only part. If the image is already wifi-only,
2011  * that is start with CMD1, return 0.
2012  */
2013
2014 static int mwifiex_extract_wifi_fw(struct mwifiex_adapter *adapter,
2015                                    const void *firmware, u32 firmware_len) {
2016         const struct mwifiex_fw_data *fwdata;
2017         u32 offset = 0, data_len, dnld_cmd;
2018         int ret = 0;
2019         bool cmd7_before = false, first_cmd = false;
2020
2021         while (1) {
2022                 /* Check for integer and buffer overflow */
2023                 if (offset + sizeof(fwdata->header) < sizeof(fwdata->header) ||
2024                     offset + sizeof(fwdata->header) >= firmware_len) {
2025                         mwifiex_dbg(adapter, ERROR,
2026                                     "extract wifi-only fw failure!\n");
2027                         ret = -1;
2028                         goto done;
2029                 }
2030
2031                 fwdata = firmware + offset;
2032                 dnld_cmd = le32_to_cpu(fwdata->header.dnld_cmd);
2033                 data_len = le32_to_cpu(fwdata->header.data_length);
2034
2035                 /* Skip past header */
2036                 offset += sizeof(fwdata->header);
2037
2038                 switch (dnld_cmd) {
2039                 case MWIFIEX_FW_DNLD_CMD_1:
2040                         if (offset + data_len < data_len) {
2041                                 mwifiex_dbg(adapter, ERROR, "bad FW parse\n");
2042                                 ret = -1;
2043                                 goto done;
2044                         }
2045
2046                         /* Image start with cmd1, already wifi-only firmware */
2047                         if (!first_cmd) {
2048                                 mwifiex_dbg(adapter, MSG,
2049                                             "input wifi-only firmware\n");
2050                                 return 0;
2051                         }
2052
2053                         if (!cmd7_before) {
2054                                 mwifiex_dbg(adapter, ERROR,
2055                                             "no cmd7 before cmd1!\n");
2056                                 ret = -1;
2057                                 goto done;
2058                         }
2059                         offset += data_len;
2060                         break;
2061                 case MWIFIEX_FW_DNLD_CMD_5:
2062                         first_cmd = true;
2063                         /* Check for integer overflow */
2064                         if (offset + data_len < data_len) {
2065                                 mwifiex_dbg(adapter, ERROR, "bad FW parse\n");
2066                                 ret = -1;
2067                                 goto done;
2068                         }
2069                         offset += data_len;
2070                         break;
2071                 case MWIFIEX_FW_DNLD_CMD_6:
2072                         first_cmd = true;
2073                         /* Check for integer overflow */
2074                         if (offset + data_len < data_len) {
2075                                 mwifiex_dbg(adapter, ERROR, "bad FW parse\n");
2076                                 ret = -1;
2077                                 goto done;
2078                         }
2079                         offset += data_len;
2080                         if (offset >= firmware_len) {
2081                                 mwifiex_dbg(adapter, ERROR,
2082                                             "extract wifi-only fw failure!\n");
2083                                 ret = -1;
2084                         } else {
2085                                 ret = offset;
2086                         }
2087                         goto done;
2088                 case MWIFIEX_FW_DNLD_CMD_7:
2089                         first_cmd = true;
2090                         cmd7_before = true;
2091                         break;
2092                 default:
2093                         mwifiex_dbg(adapter, ERROR, "unknown dnld_cmd %d\n",
2094                                     dnld_cmd);
2095                         ret = -1;
2096                         goto done;
2097                 }
2098         }
2099
2100 done:
2101         return ret;
2102 }
2103
2104 /*
2105  * This function downloads the firmware to the card.
2106  *
2107  * Firmware is downloaded to the card in blocks. Every block download
2108  * is tested for CRC errors, and retried a number of times before
2109  * returning failure.
2110  */
2111 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
2112                                     struct mwifiex_fw_image *fw)
2113 {
2114         int ret;
2115         u8 *firmware = fw->fw_buf;
2116         u32 firmware_len = fw->fw_len;
2117         u32 offset = 0;
2118         struct sk_buff *skb;
2119         u32 txlen, tx_blocks = 0, tries, len, val;
2120         u32 block_retry_cnt = 0;
2121         struct pcie_service_card *card = adapter->card;
2122         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2123
2124         if (!firmware || !firmware_len) {
2125                 mwifiex_dbg(adapter, ERROR,
2126                             "No firmware image found! Terminating download\n");
2127                 return -1;
2128         }
2129
2130         mwifiex_dbg(adapter, INFO,
2131                     "info: Downloading FW image (%d bytes)\n",
2132                     firmware_len);
2133
2134         if (mwifiex_pcie_disable_host_int(adapter)) {
2135                 mwifiex_dbg(adapter, ERROR,
2136                             "%s: Disabling interrupts failed.\n", __func__);
2137                 return -1;
2138         }
2139
2140         skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
2141         if (!skb) {
2142                 ret = -ENOMEM;
2143                 goto done;
2144         }
2145
2146         ret = mwifiex_read_reg(adapter, PCIE_SCRATCH_13_REG, &val);
2147         if (ret) {
2148                 mwifiex_dbg(adapter, FATAL, "Failed to read scratch register 13\n");
2149                 goto done;
2150         }
2151
2152         /* PCIE FLR case: extract wifi part from combo firmware*/
2153         if (val == MWIFIEX_PCIE_FLR_HAPPENS) {
2154                 ret = mwifiex_extract_wifi_fw(adapter, firmware, firmware_len);
2155                 if (ret < 0) {
2156                         mwifiex_dbg(adapter, ERROR, "Failed to extract wifi fw\n");
2157                         goto done;
2158                 }
2159                 offset = ret;
2160                 mwifiex_dbg(adapter, MSG,
2161                             "info: dnld wifi firmware from %d bytes\n", offset);
2162         }
2163
2164         /* Perform firmware data transfer */
2165         do {
2166                 u32 ireg_intr = 0;
2167
2168                 /* More data? */
2169                 if (offset >= firmware_len)
2170                         break;
2171
2172                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2173                         ret = mwifiex_read_reg(adapter, reg->cmd_size,
2174                                                &len);
2175                         if (ret) {
2176                                 mwifiex_dbg(adapter, FATAL,
2177                                             "Failed reading len from boot code\n");
2178                                 goto done;
2179                         }
2180                         if (len)
2181                                 break;
2182                         usleep_range(10, 20);
2183                 }
2184
2185                 if (!len) {
2186                         break;
2187                 } else if (len > MWIFIEX_UPLD_SIZE) {
2188                         mwifiex_dbg(adapter, ERROR,
2189                                     "FW download failure @ %d, invalid length %d\n",
2190                                     offset, len);
2191                         ret = -1;
2192                         goto done;
2193                 }
2194
2195                 txlen = len;
2196
2197                 if (len & BIT(0)) {
2198                         block_retry_cnt++;
2199                         if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
2200                                 mwifiex_dbg(adapter, ERROR,
2201                                             "FW download failure @ %d, over max\t"
2202                                             "retry count\n", offset);
2203                                 ret = -1;
2204                                 goto done;
2205                         }
2206                         mwifiex_dbg(adapter, ERROR,
2207                                     "FW CRC error indicated by the\t"
2208                                     "helper: len = 0x%04X, txlen = %d\n",
2209                                     len, txlen);
2210                         len &= ~BIT(0);
2211                         /* Setting this to 0 to resend from same offset */
2212                         txlen = 0;
2213                 } else {
2214                         block_retry_cnt = 0;
2215                         /* Set blocksize to transfer - checking for
2216                            last block */
2217                         if (firmware_len - offset < txlen)
2218                                 txlen = firmware_len - offset;
2219
2220                         tx_blocks = (txlen + card->pcie.blksz_fw_dl - 1) /
2221                                     card->pcie.blksz_fw_dl;
2222
2223                         /* Copy payload to buffer */
2224                         memmove(skb->data, &firmware[offset], txlen);
2225                 }
2226
2227                 skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
2228                 skb_trim(skb, tx_blocks * card->pcie.blksz_fw_dl);
2229
2230                 /* Send the boot command to device */
2231                 if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
2232                         mwifiex_dbg(adapter, ERROR,
2233                                     "Failed to send firmware download command\n");
2234                         ret = -1;
2235                         goto done;
2236                 }
2237
2238                 /* Wait for the command done interrupt */
2239                 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2240                         if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
2241                                              &ireg_intr)) {
2242                                 mwifiex_dbg(adapter, ERROR,
2243                                             "%s: Failed to read\t"
2244                                             "interrupt status during fw dnld.\n",
2245                                             __func__);
2246                                 mwifiex_unmap_pci_memory(adapter, skb,
2247                                                          PCI_DMA_TODEVICE);
2248                                 ret = -1;
2249                                 goto done;
2250                         }
2251                         if (!(ireg_intr & CPU_INTR_DOOR_BELL))
2252                                 break;
2253                         usleep_range(10, 20);
2254                 }
2255                 if (ireg_intr & CPU_INTR_DOOR_BELL) {
2256                         mwifiex_dbg(adapter, ERROR, "%s: Card failed to ACK download\n",
2257                                     __func__);
2258                         mwifiex_unmap_pci_memory(adapter, skb,
2259                                                  PCI_DMA_TODEVICE);
2260                         ret = -1;
2261                         goto done;
2262                 }
2263
2264                 mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
2265
2266                 offset += txlen;
2267         } while (true);
2268
2269         mwifiex_dbg(adapter, MSG,
2270                     "info: FW download over, size %d bytes\n", offset);
2271
2272         ret = 0;
2273
2274 done:
2275         dev_kfree_skb_any(skb);
2276         return ret;
2277 }
2278
2279 /*
2280  * This function checks the firmware status in card.
2281  */
2282 static int
2283 mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
2284 {
2285         int ret = 0;
2286         u32 firmware_stat;
2287         struct pcie_service_card *card = adapter->card;
2288         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2289         u32 tries;
2290
2291         /* Mask spurios interrupts */
2292         if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
2293                               HOST_INTR_MASK)) {
2294                 mwifiex_dbg(adapter, ERROR,
2295                             "Write register failed\n");
2296                 return -1;
2297         }
2298
2299         mwifiex_dbg(adapter, INFO,
2300                     "Setting driver ready signature\n");
2301         if (mwifiex_write_reg(adapter, reg->drv_rdy,
2302                               FIRMWARE_READY_PCIE)) {
2303                 mwifiex_dbg(adapter, ERROR,
2304                             "Failed to write driver ready signature\n");
2305                 return -1;
2306         }
2307
2308         /* Wait for firmware initialization event */
2309         for (tries = 0; tries < poll_num; tries++) {
2310                 if (mwifiex_read_reg(adapter, reg->fw_status,
2311                                      &firmware_stat))
2312                         ret = -1;
2313                 else
2314                         ret = 0;
2315
2316                 mwifiex_dbg(adapter, INFO, "Try %d if FW is ready <%d,%#x>",
2317                             tries, ret, firmware_stat);
2318
2319                 if (ret)
2320                         continue;
2321                 if (firmware_stat == FIRMWARE_READY_PCIE) {
2322                         ret = 0;
2323                         break;
2324                 } else {
2325                         msleep(100);
2326                         ret = -1;
2327                 }
2328         }
2329
2330         return ret;
2331 }
2332
2333 /* This function checks if WLAN is the winner.
2334  */
2335 static int
2336 mwifiex_check_winner_status(struct mwifiex_adapter *adapter)
2337 {
2338         u32 winner = 0;
2339         int ret = 0;
2340         struct pcie_service_card *card = adapter->card;
2341         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2342
2343         if (mwifiex_read_reg(adapter, reg->fw_status, &winner)) {
2344                 ret = -1;
2345         } else if (!winner) {
2346                 mwifiex_dbg(adapter, INFO, "PCI-E is the winner\n");
2347                 adapter->winner = 1;
2348         } else {
2349                 mwifiex_dbg(adapter, ERROR,
2350                             "PCI-E is not the winner <%#x>", winner);
2351         }
2352
2353         return ret;
2354 }
2355
2356 /*
2357  * This function reads the interrupt status from card.
2358  */
2359 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter,
2360                                      int msg_id)
2361 {
2362         u32 pcie_ireg;
2363         unsigned long flags;
2364         struct pcie_service_card *card = adapter->card;
2365
2366         if (card->msi_enable) {
2367                 spin_lock_irqsave(&adapter->int_lock, flags);
2368                 adapter->int_status = 1;
2369                 spin_unlock_irqrestore(&adapter->int_lock, flags);
2370                 return;
2371         }
2372
2373         if (!mwifiex_pcie_ok_to_access_hw(adapter))
2374                 return;
2375
2376         if (card->msix_enable && msg_id >= 0) {
2377                 pcie_ireg = BIT(msg_id);
2378         } else {
2379                 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2380                                      &pcie_ireg)) {
2381                         mwifiex_dbg(adapter, ERROR, "Read register failed\n");
2382                         return;
2383                 }
2384
2385                 if ((pcie_ireg == 0xFFFFFFFF) || !pcie_ireg)
2386                         return;
2387
2388
2389                 mwifiex_pcie_disable_host_int(adapter);
2390
2391                 /* Clear the pending interrupts */
2392                 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
2393                                       ~pcie_ireg)) {
2394                         mwifiex_dbg(adapter, ERROR,
2395                                     "Write register failed\n");
2396                         return;
2397                 }
2398         }
2399
2400         if (!adapter->pps_uapsd_mode &&
2401             adapter->ps_state == PS_STATE_SLEEP &&
2402             mwifiex_pcie_ok_to_access_hw(adapter)) {
2403                 /* Potentially for PCIe we could get other
2404                  * interrupts like shared. Don't change power
2405                  * state until cookie is set
2406                  */
2407                 adapter->ps_state = PS_STATE_AWAKE;
2408                 adapter->pm_wakeup_fw_try = false;
2409                 del_timer(&adapter->wakeup_timer);
2410         }
2411
2412         spin_lock_irqsave(&adapter->int_lock, flags);
2413         adapter->int_status |= pcie_ireg;
2414         spin_unlock_irqrestore(&adapter->int_lock, flags);
2415         mwifiex_dbg(adapter, INTR, "ireg: 0x%08x\n", pcie_ireg);
2416 }
2417
2418 /*
2419  * Interrupt handler for PCIe root port
2420  *
2421  * This function reads the interrupt status from firmware and assigns
2422  * the main process in workqueue which will handle the interrupt.
2423  */
2424 static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
2425 {
2426         struct mwifiex_msix_context *ctx = context;
2427         struct pci_dev *pdev = ctx->dev;
2428         struct pcie_service_card *card;
2429         struct mwifiex_adapter *adapter;
2430
2431         card = pci_get_drvdata(pdev);
2432
2433         if (!card->adapter) {
2434                 pr_err("info: %s: card=%p adapter=%p\n", __func__, card,
2435                        card ? card->adapter : NULL);
2436                 goto exit;
2437         }
2438         adapter = card->adapter;
2439
2440         if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags))
2441                 goto exit;
2442
2443         if (card->msix_enable)
2444                 mwifiex_interrupt_status(adapter, ctx->msg_id);
2445         else
2446                 mwifiex_interrupt_status(adapter, -1);
2447
2448         mwifiex_queue_main_work(adapter);
2449
2450 exit:
2451         return IRQ_HANDLED;
2452 }
2453
2454 /*
2455  * This function checks the current interrupt status.
2456  *
2457  * The following interrupts are checked and handled by this function -
2458  *      - Data sent
2459  *      - Command sent
2460  *      - Command received
2461  *      - Packets received
2462  *      - Events received
2463  *
2464  * In case of Rx packets received, the packets are uploaded from card to
2465  * host and processed accordingly.
2466  */
2467 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
2468 {
2469         int ret;
2470         u32 pcie_ireg = 0;
2471         unsigned long flags;
2472         struct pcie_service_card *card = adapter->card;
2473
2474         spin_lock_irqsave(&adapter->int_lock, flags);
2475         if (!card->msi_enable) {
2476                 /* Clear out unused interrupts */
2477                 pcie_ireg = adapter->int_status;
2478         }
2479         adapter->int_status = 0;
2480         spin_unlock_irqrestore(&adapter->int_lock, flags);
2481
2482         if (card->msi_enable) {
2483                 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
2484                         if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2485                                              &pcie_ireg)) {
2486                                 mwifiex_dbg(adapter, ERROR,
2487                                             "Read register failed\n");
2488                                 return -1;
2489                         }
2490
2491                         if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2492                                 if (mwifiex_write_reg(adapter,
2493                                                       PCIE_HOST_INT_STATUS,
2494                                                       ~pcie_ireg)) {
2495                                         mwifiex_dbg(adapter, ERROR,
2496                                                     "Write register failed\n");
2497                                         return -1;
2498                                 }
2499                                 if (!adapter->pps_uapsd_mode &&
2500                                     adapter->ps_state == PS_STATE_SLEEP) {
2501                                         adapter->ps_state = PS_STATE_AWAKE;
2502                                         adapter->pm_wakeup_fw_try = false;
2503                                         del_timer(&adapter->wakeup_timer);
2504                                 }
2505                         }
2506                 }
2507         }
2508
2509         if (pcie_ireg & HOST_INTR_DNLD_DONE) {
2510                 mwifiex_dbg(adapter, INTR, "info: TX DNLD Done\n");
2511                 ret = mwifiex_pcie_send_data_complete(adapter);
2512                 if (ret)
2513                         return ret;
2514         }
2515         if (pcie_ireg & HOST_INTR_UPLD_RDY) {
2516                 mwifiex_dbg(adapter, INTR, "info: Rx DATA\n");
2517                 ret = mwifiex_pcie_process_recv_data(adapter);
2518                 if (ret)
2519                         return ret;
2520         }
2521         if (pcie_ireg & HOST_INTR_EVENT_RDY) {
2522                 mwifiex_dbg(adapter, INTR, "info: Rx EVENT\n");
2523                 ret = mwifiex_pcie_process_event_ready(adapter);
2524                 if (ret)
2525                         return ret;
2526         }
2527         if (pcie_ireg & HOST_INTR_CMD_DONE) {
2528                 if (adapter->cmd_sent) {
2529                         mwifiex_dbg(adapter, INTR,
2530                                     "info: CMD sent Interrupt\n");
2531                         adapter->cmd_sent = false;
2532                 }
2533                 /* Handle command response */
2534                 ret = mwifiex_pcie_process_cmd_complete(adapter);
2535                 if (ret)
2536                         return ret;
2537         }
2538
2539         mwifiex_dbg(adapter, INTR,
2540                     "info: cmd_sent=%d data_sent=%d\n",
2541                     adapter->cmd_sent, adapter->data_sent);
2542         if (!card->msi_enable && !card->msix_enable &&
2543                                  adapter->ps_state != PS_STATE_SLEEP)
2544                 mwifiex_pcie_enable_host_int(adapter);
2545
2546         return 0;
2547 }
2548
2549 /*
2550  * This function downloads data from driver to card.
2551  *
2552  * Both commands and data packets are transferred to the card by this
2553  * function.
2554  *
2555  * This function adds the PCIE specific header to the front of the buffer
2556  * before transferring. The header contains the length of the packet and
2557  * the type. The firmware handles the packets based upon this set type.
2558  */
2559 static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
2560                                      struct sk_buff *skb,
2561                                      struct mwifiex_tx_param *tx_param)
2562 {
2563         if (!skb) {
2564                 mwifiex_dbg(adapter, ERROR,
2565                             "Passed NULL skb to %s\n", __func__);
2566                 return -1;
2567         }
2568
2569         if (type == MWIFIEX_TYPE_DATA)
2570                 return mwifiex_pcie_send_data(adapter, skb, tx_param);
2571         else if (type == MWIFIEX_TYPE_CMD)
2572                 return mwifiex_pcie_send_cmd(adapter, skb);
2573
2574         return 0;
2575 }
2576
2577 /* Function to dump PCIE scratch registers in case of FW crash
2578  */
2579 static int
2580 mwifiex_pcie_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
2581 {
2582         char *p = drv_buf;
2583         char buf[256], *ptr;
2584         int i;
2585         u32 value;
2586         struct pcie_service_card *card = adapter->card;
2587         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2588         int pcie_scratch_reg[] = {PCIE_SCRATCH_12_REG,
2589                                   PCIE_SCRATCH_14_REG,
2590                                   PCIE_SCRATCH_15_REG};
2591
2592         if (!p)
2593                 return 0;
2594
2595         mwifiex_dbg(adapter, MSG, "PCIE register dump start\n");
2596
2597         if (mwifiex_read_reg(adapter, reg->fw_status, &value)) {
2598                 mwifiex_dbg(adapter, ERROR, "failed to read firmware status");
2599                 return 0;
2600         }
2601
2602         ptr = buf;
2603         mwifiex_dbg(adapter, MSG, "pcie scratch register:");
2604         for (i = 0; i < ARRAY_SIZE(pcie_scratch_reg); i++) {
2605                 mwifiex_read_reg(adapter, pcie_scratch_reg[i], &value);
2606                 ptr += sprintf(ptr, "reg:0x%x, value=0x%x\n",
2607                                pcie_scratch_reg[i], value);
2608         }
2609
2610         mwifiex_dbg(adapter, MSG, "%s\n", buf);
2611         p += sprintf(p, "%s\n", buf);
2612
2613         mwifiex_dbg(adapter, MSG, "PCIE register dump end\n");
2614
2615         return p - drv_buf;
2616 }
2617
2618 /* This function read/write firmware */
2619 static enum rdwr_status
2620 mwifiex_pcie_rdwr_firmware(struct mwifiex_adapter *adapter, u8 doneflag)
2621 {
2622         int ret, tries;
2623         u8 ctrl_data;
2624         u32 fw_status;
2625         struct pcie_service_card *card = adapter->card;
2626         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2627
2628         if (mwifiex_read_reg(adapter, reg->fw_status, &fw_status))
2629                 return RDWR_STATUS_FAILURE;
2630
2631         ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl,
2632                                 reg->fw_dump_host_ready);
2633         if (ret) {
2634                 mwifiex_dbg(adapter, ERROR,
2635                             "PCIE write err\n");
2636                 return RDWR_STATUS_FAILURE;
2637         }
2638
2639         for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2640                 mwifiex_read_reg_byte(adapter, reg->fw_dump_ctrl, &ctrl_data);
2641                 if (ctrl_data == FW_DUMP_DONE)
2642                         return RDWR_STATUS_SUCCESS;
2643                 if (doneflag && ctrl_data == doneflag)
2644                         return RDWR_STATUS_DONE;
2645                 if (ctrl_data != reg->fw_dump_host_ready) {
2646                         mwifiex_dbg(adapter, WARN,
2647                                     "The ctrl reg was changed, re-try again!\n");
2648                         ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl,
2649                                                 reg->fw_dump_host_ready);
2650                         if (ret) {
2651                                 mwifiex_dbg(adapter, ERROR,
2652                                             "PCIE write err\n");
2653                                 return RDWR_STATUS_FAILURE;
2654                         }
2655                 }
2656                 usleep_range(100, 200);
2657         }
2658
2659         mwifiex_dbg(adapter, ERROR, "Fail to pull ctrl_data\n");
2660         return RDWR_STATUS_FAILURE;
2661 }
2662
2663 /* This function dump firmware memory to file */
2664 static void mwifiex_pcie_fw_dump(struct mwifiex_adapter *adapter)
2665 {
2666         struct pcie_service_card *card = adapter->card;
2667         const struct mwifiex_pcie_card_reg *creg = card->pcie.reg;
2668         unsigned int reg, reg_start, reg_end;
2669         u8 *dbg_ptr, *end_ptr, *tmp_ptr, fw_dump_num, dump_num;
2670         u8 idx, i, read_reg, doneflag = 0;
2671         enum rdwr_status stat;
2672         u32 memory_size;
2673         int ret;
2674
2675         if (!card->pcie.can_dump_fw)
2676                 return;
2677
2678         for (idx = 0; idx < adapter->num_mem_types; idx++) {
2679                 struct memory_type_mapping *entry =
2680                                 &adapter->mem_type_mapping_tbl[idx];
2681
2682                 if (entry->mem_ptr) {
2683                         vfree(entry->mem_ptr);
2684                         entry->mem_ptr = NULL;
2685                 }
2686                 entry->mem_size = 0;
2687         }
2688
2689         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
2690
2691         /* Read the number of the memories which will dump */
2692         stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2693         if (stat == RDWR_STATUS_FAILURE)
2694                 return;
2695
2696         reg = creg->fw_dump_start;
2697         mwifiex_read_reg_byte(adapter, reg, &fw_dump_num);
2698
2699         /* W8997 chipset firmware dump will be restore in single region*/
2700         if (fw_dump_num == 0)
2701                 dump_num = 1;
2702         else
2703                 dump_num = fw_dump_num;
2704
2705         /* Read the length of every memory which will dump */
2706         for (idx = 0; idx < dump_num; idx++) {
2707                 struct memory_type_mapping *entry =
2708                                 &adapter->mem_type_mapping_tbl[idx];
2709                 memory_size = 0;
2710                 if (fw_dump_num != 0) {
2711                         stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2712                         if (stat == RDWR_STATUS_FAILURE)
2713                                 return;
2714
2715                         reg = creg->fw_dump_start;
2716                         for (i = 0; i < 4; i++) {
2717                                 mwifiex_read_reg_byte(adapter, reg, &read_reg);
2718                                 memory_size |= (read_reg << (i * 8));
2719                                 reg++;
2720                         }
2721                 } else {
2722                         memory_size = MWIFIEX_FW_DUMP_MAX_MEMSIZE;
2723                 }
2724
2725                 if (memory_size == 0) {
2726                         mwifiex_dbg(adapter, MSG, "Firmware dump Finished!\n");
2727                         ret = mwifiex_write_reg(adapter, creg->fw_dump_ctrl,
2728                                                 creg->fw_dump_read_done);
2729                         if (ret) {
2730                                 mwifiex_dbg(adapter, ERROR, "PCIE write err\n");
2731                                 return;
2732                         }
2733                         break;
2734                 }
2735
2736                 mwifiex_dbg(adapter, DUMP,
2737                             "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2738                 entry->mem_ptr = vmalloc(memory_size + 1);
2739                 entry->mem_size = memory_size;
2740                 if (!entry->mem_ptr) {
2741                         mwifiex_dbg(adapter, ERROR,
2742                                     "Vmalloc %s failed\n", entry->mem_name);
2743                         return;
2744                 }
2745                 dbg_ptr = entry->mem_ptr;
2746                 end_ptr = dbg_ptr + memory_size;
2747
2748                 doneflag = entry->done_flag;
2749                 mwifiex_dbg(adapter, DUMP, "Start %s output, please wait...\n",
2750                             entry->mem_name);
2751
2752                 do {
2753                         stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2754                         if (RDWR_STATUS_FAILURE == stat)
2755                                 return;
2756
2757                         reg_start = creg->fw_dump_start;
2758                         reg_end = creg->fw_dump_end;
2759                         for (reg = reg_start; reg <= reg_end; reg++) {
2760                                 mwifiex_read_reg_byte(adapter, reg, dbg_ptr);
2761                                 if (dbg_ptr < end_ptr) {
2762                                         dbg_ptr++;
2763                                         continue;
2764                                 }
2765                                 mwifiex_dbg(adapter, ERROR,
2766                                             "pre-allocated buf not enough\n");
2767                                 tmp_ptr =
2768                                         vzalloc(memory_size + MWIFIEX_SIZE_4K);
2769                                 if (!tmp_ptr)
2770                                         return;
2771                                 memcpy(tmp_ptr, entry->mem_ptr, memory_size);
2772                                 vfree(entry->mem_ptr);
2773                                 entry->mem_ptr = tmp_ptr;
2774                                 tmp_ptr = NULL;
2775                                 dbg_ptr = entry->mem_ptr + memory_size;
2776                                 memory_size += MWIFIEX_SIZE_4K;
2777                                 end_ptr = entry->mem_ptr + memory_size;
2778                         }
2779
2780                         if (stat != RDWR_STATUS_DONE)
2781                                 continue;
2782
2783                         mwifiex_dbg(adapter, DUMP,
2784                                     "%s done: size=0x%tx\n",
2785                                     entry->mem_name, dbg_ptr - entry->mem_ptr);
2786                         break;
2787                 } while (true);
2788         }
2789         mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
2790 }
2791
2792 static void mwifiex_pcie_device_dump_work(struct mwifiex_adapter *adapter)
2793 {
2794         adapter->devdump_data = vzalloc(MWIFIEX_FW_DUMP_SIZE);
2795         if (!adapter->devdump_data) {
2796                 mwifiex_dbg(adapter, ERROR,
2797                             "vzalloc devdump data failure!\n");
2798                 return;
2799         }
2800
2801         mwifiex_drv_info_dump(adapter);
2802         mwifiex_pcie_fw_dump(adapter);
2803         mwifiex_prepare_fw_dump_info(adapter);
2804         mwifiex_upload_device_dump(adapter);
2805 }
2806
2807 static void mwifiex_pcie_card_reset_work(struct mwifiex_adapter *adapter)
2808 {
2809         struct pcie_service_card *card = adapter->card;
2810
2811         /* We can't afford to wait here; remove() might be waiting on us. If we
2812          * can't grab the device lock, maybe we'll get another chance later.
2813          */
2814         pci_try_reset_function(card->dev);
2815 }
2816
2817 static void mwifiex_pcie_work(struct work_struct *work)
2818 {
2819         struct pcie_service_card *card =
2820                 container_of(work, struct pcie_service_card, work);
2821
2822         if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2823                                &card->work_flags))
2824                 mwifiex_pcie_device_dump_work(card->adapter);
2825         if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
2826                                &card->work_flags))
2827                 mwifiex_pcie_card_reset_work(card->adapter);
2828 }
2829
2830 /* This function dumps FW information */
2831 static void mwifiex_pcie_device_dump(struct mwifiex_adapter *adapter)
2832 {
2833         struct pcie_service_card *card = adapter->card;
2834
2835         if (!test_and_set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
2836                               &card->work_flags))
2837                 schedule_work(&card->work);
2838 }
2839
2840 static void mwifiex_pcie_card_reset(struct mwifiex_adapter *adapter)
2841 {
2842         struct pcie_service_card *card = adapter->card;
2843
2844         if (!test_and_set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &card->work_flags))
2845                 schedule_work(&card->work);
2846 }
2847
2848 static int mwifiex_pcie_alloc_buffers(struct mwifiex_adapter *adapter)
2849 {
2850         struct pcie_service_card *card = adapter->card;
2851         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2852         int ret;
2853
2854         card->cmdrsp_buf = NULL;
2855         ret = mwifiex_pcie_create_txbd_ring(adapter);
2856         if (ret) {
2857                 mwifiex_dbg(adapter, ERROR, "Failed to create txbd ring\n");
2858                 goto err_cre_txbd;
2859         }
2860
2861         ret = mwifiex_pcie_create_rxbd_ring(adapter);
2862         if (ret) {
2863                 mwifiex_dbg(adapter, ERROR, "Failed to create rxbd ring\n");
2864                 goto err_cre_rxbd;
2865         }
2866
2867         ret = mwifiex_pcie_create_evtbd_ring(adapter);
2868         if (ret) {
2869                 mwifiex_dbg(adapter, ERROR, "Failed to create evtbd ring\n");
2870                 goto err_cre_evtbd;
2871         }
2872
2873         ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
2874         if (ret) {
2875                 mwifiex_dbg(adapter, ERROR, "Failed to allocate cmdbuf buffer\n");
2876                 goto err_alloc_cmdbuf;
2877         }
2878
2879         if (reg->sleep_cookie) {
2880                 ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
2881                 if (ret) {
2882                         mwifiex_dbg(adapter, ERROR, "Failed to allocate sleep_cookie buffer\n");
2883                         goto err_alloc_cookie;
2884                 }
2885         } else {
2886                 card->sleep_cookie_vbase = NULL;
2887         }
2888
2889         return 0;
2890
2891 err_alloc_cookie:
2892         mwifiex_pcie_delete_cmdrsp_buf(adapter);
2893 err_alloc_cmdbuf:
2894         mwifiex_pcie_delete_evtbd_ring(adapter);
2895 err_cre_evtbd:
2896         mwifiex_pcie_delete_rxbd_ring(adapter);
2897 err_cre_rxbd:
2898         mwifiex_pcie_delete_txbd_ring(adapter);
2899 err_cre_txbd:
2900         return ret;
2901 }
2902
2903 static void mwifiex_pcie_free_buffers(struct mwifiex_adapter *adapter)
2904 {
2905         struct pcie_service_card *card = adapter->card;
2906         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2907
2908         if (reg->sleep_cookie)
2909                 mwifiex_pcie_delete_sleep_cookie_buf(adapter);
2910
2911         mwifiex_pcie_delete_cmdrsp_buf(adapter);
2912         mwifiex_pcie_delete_evtbd_ring(adapter);
2913         mwifiex_pcie_delete_rxbd_ring(adapter);
2914         mwifiex_pcie_delete_txbd_ring(adapter);
2915 }
2916
2917 /*
2918  * This function initializes the PCI-E host memory space, WCB rings, etc.
2919  */
2920 static int mwifiex_init_pcie(struct mwifiex_adapter *adapter)
2921 {
2922         struct pcie_service_card *card = adapter->card;
2923         int ret;
2924         struct pci_dev *pdev = card->dev;
2925
2926         pci_set_drvdata(pdev, card);
2927
2928         ret = pci_enable_device(pdev);
2929         if (ret)
2930                 goto err_enable_dev;
2931
2932         pci_set_master(pdev);
2933
2934         pr_notice("try set_consistent_dma_mask(32)\n");
2935         ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2936         if (ret) {
2937                 pr_err("set_dma_mask(32) failed\n");
2938                 goto err_set_dma_mask;
2939         }
2940
2941         ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2942         if (ret) {
2943                 pr_err("set_consistent_dma_mask(64) failed\n");
2944                 goto err_set_dma_mask;
2945         }
2946
2947         ret = pci_request_region(pdev, 0, DRV_NAME);
2948         if (ret) {
2949                 pr_err("req_reg(0) error\n");
2950                 goto err_req_region0;
2951         }
2952         card->pci_mmap = pci_iomap(pdev, 0, 0);
2953         if (!card->pci_mmap) {
2954                 pr_err("iomap(0) error\n");
2955                 ret = -EIO;
2956                 goto err_iomap0;
2957         }
2958         ret = pci_request_region(pdev, 2, DRV_NAME);
2959         if (ret) {
2960                 pr_err("req_reg(2) error\n");
2961                 goto err_req_region2;
2962         }
2963         card->pci_mmap1 = pci_iomap(pdev, 2, 0);
2964         if (!card->pci_mmap1) {
2965                 pr_err("iomap(2) error\n");
2966                 ret = -EIO;
2967                 goto err_iomap2;
2968         }
2969
2970         pr_notice("PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
2971                   card->pci_mmap, card->pci_mmap1);
2972
2973         ret = mwifiex_pcie_alloc_buffers(adapter);
2974         if (ret)
2975                 goto err_alloc_buffers;
2976
2977         return 0;
2978
2979 err_alloc_buffers:
2980         pci_iounmap(pdev, card->pci_mmap1);
2981 err_iomap2:
2982         pci_release_region(pdev, 2);
2983 err_req_region2:
2984         pci_iounmap(pdev, card->pci_mmap);
2985 err_iomap0:
2986         pci_release_region(pdev, 0);
2987 err_req_region0:
2988 err_set_dma_mask:
2989         pci_disable_device(pdev);
2990 err_enable_dev:
2991         return ret;
2992 }
2993
2994 /*
2995  * This function cleans up the allocated card buffers.
2996  */
2997 static void mwifiex_cleanup_pcie(struct mwifiex_adapter *adapter)
2998 {
2999         struct pcie_service_card *card = adapter->card;
3000         struct pci_dev *pdev = card->dev;
3001         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
3002         int ret;
3003         u32 fw_status;
3004
3005         /* Perform the cancel_work_sync() only when we're not resetting
3006          * the card. It's because that function never returns if we're
3007          * in reset path. If we're here when resetting the card, it means
3008          * that we failed to reset the card (reset failure path).
3009          */
3010         if (!card->pci_reset_ongoing) {
3011                 mwifiex_dbg(adapter, MSG, "performing cancel_work_sync()...\n");
3012                 cancel_work_sync(&card->work);
3013                 mwifiex_dbg(adapter, MSG, "cancel_work_sync() done\n");
3014         } else {
3015                 mwifiex_dbg(adapter, MSG,
3016                             "skipped cancel_work_sync() because we're in card reset failure path\n");
3017         }
3018
3019         ret = mwifiex_read_reg(adapter, reg->fw_status, &fw_status);
3020         if (fw_status == FIRMWARE_READY_PCIE) {
3021                 mwifiex_dbg(adapter, INFO,
3022                             "Clearing driver ready signature\n");
3023                 if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
3024                         mwifiex_dbg(adapter, ERROR,
3025                                     "Failed to write driver not-ready signature\n");
3026         }
3027
3028         pci_disable_device(pdev);
3029
3030         pci_iounmap(pdev, card->pci_mmap);
3031         pci_iounmap(pdev, card->pci_mmap1);
3032         pci_release_region(pdev, 2);
3033         pci_release_region(pdev, 0);
3034
3035         mwifiex_pcie_free_buffers(adapter);
3036 }
3037
3038 static int mwifiex_pcie_request_irq(struct mwifiex_adapter *adapter)
3039 {
3040         int ret, i, j;
3041         struct pcie_service_card *card = adapter->card;
3042         struct pci_dev *pdev = card->dev;
3043
3044         if (card->pcie.reg->msix_support) {
3045                 for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++)
3046                         card->msix_entries[i].entry = i;
3047                 ret = pci_enable_msix_exact(pdev, card->msix_entries,
3048                                             MWIFIEX_NUM_MSIX_VECTORS);
3049                 if (!ret) {
3050                         for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++) {
3051                                 card->msix_ctx[i].dev = pdev;
3052                                 card->msix_ctx[i].msg_id = i;
3053
3054                                 ret = request_irq(card->msix_entries[i].vector,
3055                                                   mwifiex_pcie_interrupt, 0,
3056                                                   "MWIFIEX_PCIE_MSIX",
3057                                                   &card->msix_ctx[i]);
3058                                 if (ret)
3059                                         break;
3060                         }
3061
3062                         if (ret) {
3063                                 mwifiex_dbg(adapter, INFO, "request_irq fail: %d\n",
3064                                             ret);
3065                                 for (j = 0; j < i; j++)
3066                                         free_irq(card->msix_entries[j].vector,
3067                                                  &card->msix_ctx[i]);
3068                                 pci_disable_msix(pdev);
3069                         } else {
3070                                 mwifiex_dbg(adapter, MSG, "MSIx enabled!");
3071                                 card->msix_enable = 1;
3072                                 return 0;
3073                         }
3074                 }
3075         }
3076
3077         if (pci_enable_msi(pdev) != 0)
3078                 pci_disable_msi(pdev);
3079         else
3080                 card->msi_enable = 1;
3081
3082         mwifiex_dbg(adapter, INFO, "msi_enable = %d\n", card->msi_enable);
3083
3084         card->share_irq_ctx.dev = pdev;
3085         card->share_irq_ctx.msg_id = -1;
3086         ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
3087                           "MRVL_PCIE", &card->share_irq_ctx);
3088         if (ret) {
3089                 pr_err("request_irq failed: ret=%d\n", ret);
3090                 return -1;
3091         }
3092
3093         return 0;
3094 }
3095
3096 /*
3097  * This function gets the firmware name for downloading by revision id
3098  *
3099  * Read revision id register to get revision id
3100  */
3101 static void mwifiex_pcie_get_fw_name(struct mwifiex_adapter *adapter)
3102 {
3103         int revision_id = 0;
3104         int version, magic;
3105         struct pcie_service_card *card = adapter->card;
3106
3107         switch (card->dev->device) {
3108         case PCIE_DEVICE_ID_MARVELL_88W8766P:
3109                 strcpy(adapter->fw_name, PCIE8766_DEFAULT_FW_NAME);
3110                 break;
3111         case PCIE_DEVICE_ID_MARVELL_88W8897:
3112                 mwifiex_write_reg(adapter, 0x0c58, 0x80c00000);
3113                 mwifiex_read_reg(adapter, 0x0c58, &revision_id);
3114                 revision_id &= 0xff00;
3115                 switch (revision_id) {
3116                 case PCIE8897_A0:
3117                         strcpy(adapter->fw_name, PCIE8897_A0_FW_NAME);
3118                         break;
3119                 case PCIE8897_B0:
3120                         strcpy(adapter->fw_name, PCIE8897_B0_FW_NAME);
3121                         break;
3122                 default:
3123                         strcpy(adapter->fw_name, PCIE8897_DEFAULT_FW_NAME);
3124
3125                         break;
3126                 }
3127                 break;
3128         case PCIE_DEVICE_ID_MARVELL_88W8997:
3129                 mwifiex_read_reg(adapter, 0x8, &revision_id);
3130                 mwifiex_read_reg(adapter, 0x0cd0, &version);
3131                 mwifiex_read_reg(adapter, 0x0cd4, &magic);
3132                 revision_id &= 0xff;
3133                 version &= 0x7;
3134                 magic &= 0xff;
3135                 if (revision_id == PCIE8997_A1 &&
3136                     magic == CHIP_MAGIC_VALUE &&
3137                     version == CHIP_VER_PCIEUART)
3138                         strcpy(adapter->fw_name, PCIEUART8997_FW_NAME_V4);
3139                 else
3140                         strcpy(adapter->fw_name, PCIEUSB8997_FW_NAME_V4);
3141                 break;
3142         default:
3143                 break;
3144         }
3145 }
3146
3147 /*
3148  * This function registers the PCIE device.
3149  *
3150  * PCIE IRQ is claimed, block size is set and driver data is initialized.
3151  */
3152 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
3153 {
3154         struct pcie_service_card *card = adapter->card;
3155
3156         /* save adapter pointer in card */
3157         card->adapter = adapter;
3158
3159         if (mwifiex_pcie_request_irq(adapter))
3160                 return -1;
3161
3162         adapter->tx_buf_size = card->pcie.tx_buf_size;
3163         adapter->mem_type_mapping_tbl = card->pcie.mem_type_mapping_tbl;
3164         adapter->num_mem_types = card->pcie.num_mem_types;
3165         adapter->ext_scan = card->pcie.can_ext_scan;
3166         mwifiex_pcie_get_fw_name(adapter);
3167
3168         return 0;
3169 }
3170
3171 /*
3172  * This function unregisters the PCIE device.
3173  *
3174  * The PCIE IRQ is released, the function is disabled and driver
3175  * data is set to null.
3176  */
3177 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
3178 {
3179         struct pcie_service_card *card = adapter->card;
3180         struct pci_dev *pdev = card->dev;
3181         int i;
3182
3183         if (card->msix_enable) {
3184                 for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++)
3185                         synchronize_irq(card->msix_entries[i].vector);
3186
3187                 for (i = 0; i < MWIFIEX_NUM_MSIX_VECTORS; i++)
3188                         free_irq(card->msix_entries[i].vector,
3189                                  &card->msix_ctx[i]);
3190
3191                 card->msix_enable = 0;
3192                 pci_disable_msix(pdev);
3193         } else {
3194                 mwifiex_dbg(adapter, INFO,
3195                             "%s(): calling free_irq()\n", __func__);
3196                free_irq(card->dev->irq, &card->share_irq_ctx);
3197
3198                 if (card->msi_enable)
3199                         pci_disable_msi(pdev);
3200         }
3201         card->adapter = NULL;
3202 }
3203
3204 /*
3205  * This function initializes the PCI-E host memory space, WCB rings, etc.,
3206  * similar to mwifiex_init_pcie(), but without resetting PCI-E state.
3207  */
3208 static void mwifiex_pcie_up_dev(struct mwifiex_adapter *adapter)
3209 {
3210         struct pcie_service_card *card = adapter->card;
3211         struct pci_dev *pdev = card->dev;
3212
3213         /* tx_buf_size might be changed to 3584 by firmware during
3214          * data transfer, we should reset it to default size.
3215          */
3216         adapter->tx_buf_size = card->pcie.tx_buf_size;
3217
3218         mwifiex_pcie_alloc_buffers(adapter);
3219
3220         pci_set_master(pdev);
3221 }
3222
3223 /* This function cleans up the PCI-E host memory space. */
3224 static void mwifiex_pcie_down_dev(struct mwifiex_adapter *adapter)
3225 {
3226         struct pcie_service_card *card = adapter->card;
3227         const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
3228         struct pci_dev *pdev = card->dev;
3229
3230         if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
3231                 mwifiex_dbg(adapter, ERROR, "Failed to write driver not-ready signature\n");
3232
3233         pci_clear_master(pdev);
3234
3235         adapter->seq_num = 0;
3236
3237         mwifiex_pcie_free_buffers(adapter);
3238 }
3239
3240 static struct mwifiex_if_ops pcie_ops = {
3241         .init_if =                      mwifiex_init_pcie,
3242         .cleanup_if =                   mwifiex_cleanup_pcie,
3243         .check_fw_status =              mwifiex_check_fw_status,
3244         .check_winner_status =          mwifiex_check_winner_status,
3245         .prog_fw =                      mwifiex_prog_fw_w_helper,
3246         .register_dev =                 mwifiex_register_dev,
3247         .unregister_dev =               mwifiex_unregister_dev,
3248         .enable_int =                   mwifiex_pcie_enable_host_int,
3249         .disable_int =                  mwifiex_pcie_disable_host_int_noerr,
3250         .process_int_status =           mwifiex_process_int_status,
3251         .host_to_card =                 mwifiex_pcie_host_to_card,
3252         .wakeup =                       mwifiex_pm_wakeup_card,
3253         .wakeup_complete =              mwifiex_pm_wakeup_card_complete,
3254
3255         /* PCIE specific */
3256         .cmdrsp_complete =              mwifiex_pcie_cmdrsp_complete,
3257         .event_complete =               mwifiex_pcie_event_complete,
3258         .update_mp_end_port =           NULL,
3259         .cleanup_mpa_buf =              NULL,
3260         .init_fw_port =                 mwifiex_pcie_init_fw_port,
3261         .clean_pcie_ring =              mwifiex_clean_pcie_ring_buf,
3262         .card_reset =                   mwifiex_pcie_card_reset,
3263         .reg_dump =                     mwifiex_pcie_reg_dump,
3264         .device_dump =                  mwifiex_pcie_device_dump,
3265         .down_dev =                     mwifiex_pcie_down_dev,
3266         .up_dev =                       mwifiex_pcie_up_dev,
3267 };
3268
3269 module_pci_driver(mwifiex_pcie);
3270
3271 MODULE_AUTHOR("Marvell International Ltd.");
3272 MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
3273 MODULE_VERSION(PCIE_VERSION);
3274 MODULE_LICENSE("GPL v2");