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