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