spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / drivers / net / wireless / mwifiex / pcie.c
blob405350940a45aad728059f0909cdcdd6098de0b3
1 /*
2 * Marvell Wireless LAN device driver: PCIE specific handling
4 * Copyright (C) 2011, Marvell International Ltd.
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.
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.
20 #include <linux/firmware.h>
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"
31 #define PCIE_VERSION "1.0"
32 #define DRV_NAME "Marvell mwifiex PCIe"
34 static u8 user_rmmod;
36 static struct mwifiex_if_ops pcie_ops;
38 static struct semaphore add_remove_card_sem;
39 static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter);
40 static int mwifiex_pcie_resume(struct pci_dev *pdev);
43 * This function is called after skb allocation to update
44 * "skb->cb" with physical address of data pointer.
46 static phys_addr_t *mwifiex_update_sk_buff_pa(struct sk_buff *skb)
48 phys_addr_t *buf_pa = MWIFIEX_SKB_PACB(skb);
50 *buf_pa = (phys_addr_t)virt_to_phys(skb->data);
52 return buf_pa;
56 * This function reads sleep cookie and checks if FW is ready
58 static bool mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter *adapter)
60 u32 *cookie_addr;
61 struct pcie_service_card *card = adapter->card;
63 if (card->sleep_cookie) {
64 cookie_addr = (u32 *)card->sleep_cookie->data;
65 dev_dbg(adapter->dev, "info: ACCESS_HW: sleep cookie=0x%x\n",
66 *cookie_addr);
67 if (*cookie_addr == FW_AWAKE_COOKIE)
68 return true;
71 return false;
75 * This function probes an mwifiex device and registers it. It allocates
76 * the card structure, enables PCIE function number and initiates the
77 * device registration and initialization procedure by adding a logical
78 * interface.
80 static int mwifiex_pcie_probe(struct pci_dev *pdev,
81 const struct pci_device_id *ent)
83 struct pcie_service_card *card;
85 pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
86 pdev->vendor, pdev->device, pdev->revision);
88 card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
89 if (!card) {
90 pr_err("%s: failed to alloc memory\n", __func__);
91 return -ENOMEM;
94 card->dev = pdev;
96 if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops,
97 MWIFIEX_PCIE)) {
98 pr_err("%s failed\n", __func__);
99 kfree(card);
100 return -1;
103 return 0;
107 * This function removes the interface and frees up the card structure.
109 static void mwifiex_pcie_remove(struct pci_dev *pdev)
111 struct pcie_service_card *card;
112 struct mwifiex_adapter *adapter;
113 int i;
115 card = pci_get_drvdata(pdev);
116 if (!card)
117 return;
119 adapter = card->adapter;
120 if (!adapter || !adapter->priv_num)
121 return;
123 if (user_rmmod) {
124 #ifdef CONFIG_PM
125 if (adapter->is_suspended)
126 mwifiex_pcie_resume(pdev);
127 #endif
129 for (i = 0; i < adapter->priv_num; i++)
130 if ((GET_BSS_ROLE(adapter->priv[i]) ==
131 MWIFIEX_BSS_ROLE_STA) &&
132 adapter->priv[i]->media_connected)
133 mwifiex_deauthenticate(adapter->priv[i], NULL);
135 mwifiex_disable_auto_ds(mwifiex_get_priv(adapter,
136 MWIFIEX_BSS_ROLE_ANY));
138 mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
139 MWIFIEX_BSS_ROLE_ANY),
140 MWIFIEX_FUNC_SHUTDOWN);
143 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
144 kfree(card);
148 * Kernel needs to suspend all functions separately. Therefore all
149 * registered functions must have drivers with suspend and resume
150 * methods. Failing that the kernel simply removes the whole card.
152 * If already not suspended, this function allocates and sends a host
153 * sleep activate request to the firmware and turns off the traffic.
155 static int mwifiex_pcie_suspend(struct pci_dev *pdev, pm_message_t state)
157 struct mwifiex_adapter *adapter;
158 struct pcie_service_card *card;
159 int hs_actived, i;
161 if (pdev) {
162 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
163 if (!card || card->adapter) {
164 pr_err("Card or adapter structure is not valid\n");
165 return 0;
167 } else {
168 pr_err("PCIE device is not specified\n");
169 return 0;
172 adapter = card->adapter;
174 hs_actived = mwifiex_enable_hs(adapter);
176 /* Indicate device suspended */
177 adapter->is_suspended = true;
179 for (i = 0; i < adapter->priv_num; i++)
180 netif_carrier_off(adapter->priv[i]->netdev);
182 return 0;
186 * Kernel needs to suspend all functions separately. Therefore all
187 * registered functions must have drivers with suspend and resume
188 * methods. Failing that the kernel simply removes the whole card.
190 * If already not resumed, this function turns on the traffic and
191 * sends a host sleep cancel request to the firmware.
193 static int mwifiex_pcie_resume(struct pci_dev *pdev)
195 struct mwifiex_adapter *adapter;
196 struct pcie_service_card *card;
197 int i;
199 if (pdev) {
200 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
201 if (!card || !card->adapter) {
202 pr_err("Card or adapter structure is not valid\n");
203 return 0;
205 } else {
206 pr_err("PCIE device is not specified\n");
207 return 0;
210 adapter = card->adapter;
212 if (!adapter->is_suspended) {
213 dev_warn(adapter->dev, "Device already resumed\n");
214 return 0;
217 adapter->is_suspended = false;
219 for (i = 0; i < adapter->priv_num; i++)
220 if (adapter->priv[i]->media_connected)
221 netif_carrier_on(adapter->priv[i]->netdev);
223 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
224 MWIFIEX_ASYNC_CMD);
226 return 0;
229 #define PCIE_VENDOR_ID_MARVELL (0x11ab)
230 #define PCIE_DEVICE_ID_MARVELL_88W8766P (0x2b30)
232 static DEFINE_PCI_DEVICE_TABLE(mwifiex_ids) = {
234 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
235 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
240 MODULE_DEVICE_TABLE(pci, mwifiex_ids);
242 /* PCI Device Driver */
243 static struct pci_driver __refdata mwifiex_pcie = {
244 .name = "mwifiex_pcie",
245 .id_table = mwifiex_ids,
246 .probe = mwifiex_pcie_probe,
247 .remove = mwifiex_pcie_remove,
248 #ifdef CONFIG_PM
249 /* Power Management Hooks */
250 .suspend = mwifiex_pcie_suspend,
251 .resume = mwifiex_pcie_resume,
252 #endif
256 * This function writes data into PCIE card register.
258 static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
260 struct pcie_service_card *card = adapter->card;
262 iowrite32(data, card->pci_mmap1 + reg);
264 return 0;
268 * This function reads data from PCIE card register.
270 static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
272 struct pcie_service_card *card = adapter->card;
274 *data = ioread32(card->pci_mmap1 + reg);
276 return 0;
280 * This function wakes up the card.
282 * A host power up command is written to the card configuration
283 * register to wake up the card.
285 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
287 int i = 0;
289 while (mwifiex_pcie_ok_to_access_hw(adapter)) {
290 i++;
291 udelay(10);
292 /* 50ms max wait */
293 if (i == 50000)
294 break;
297 dev_dbg(adapter->dev, "event: Wakeup device...\n");
299 /* Enable interrupts or any chip access will wakeup device */
300 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK, HOST_INTR_MASK)) {
301 dev_warn(adapter->dev, "Enable host interrupt failed\n");
302 return -1;
305 dev_dbg(adapter->dev, "PCIE wakeup: Setting PS_STATE_AWAKE\n");
306 adapter->ps_state = PS_STATE_AWAKE;
308 return 0;
312 * This function is called after the card has woken up.
314 * The card configuration register is reset.
316 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
318 dev_dbg(adapter->dev, "cmd: Wakeup device completed\n");
320 return 0;
324 * This function disables the host interrupt.
326 * The host interrupt mask is read, the disable bit is reset and
327 * written back to the card host interrupt mask register.
329 static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
331 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
332 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
333 0x00000000)) {
334 dev_warn(adapter->dev, "Disable host interrupt failed\n");
335 return -1;
339 return 0;
343 * This function enables the host interrupt.
345 * The host interrupt enable mask is written to the card
346 * host interrupt mask register.
348 static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
350 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
351 /* Simply write the mask to the register */
352 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
353 HOST_INTR_MASK)) {
354 dev_warn(adapter->dev, "Enable host interrupt failed\n");
355 return -1;
359 return 0;
363 * This function creates buffer descriptor ring for TX
365 static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
367 struct pcie_service_card *card = adapter->card;
368 struct sk_buff *skb;
369 int i;
370 phys_addr_t *buf_pa;
373 * driver maintaines the write pointer and firmware maintaines the read
374 * pointer. The write pointer starts at 0 (zero) while the read pointer
375 * starts at zero with rollover bit set
377 card->txbd_wrptr = 0;
378 card->txbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND;
380 /* allocate shared memory for the BD ring and divide the same in to
381 several descriptors */
382 card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
383 MWIFIEX_MAX_TXRX_BD;
384 dev_dbg(adapter->dev, "info: txbd_ring: Allocating %d bytes\n",
385 card->txbd_ring_size);
386 card->txbd_ring_vbase = kzalloc(card->txbd_ring_size, GFP_KERNEL);
387 if (!card->txbd_ring_vbase) {
388 dev_err(adapter->dev, "Unable to allocate buffer for txbd ring.\n");
389 return -ENOMEM;
391 card->txbd_ring_pbase = virt_to_phys(card->txbd_ring_vbase);
393 dev_dbg(adapter->dev, "info: txbd_ring - base: %p, pbase: %#x:%x,"
394 "len: %x\n", card->txbd_ring_vbase,
395 (u32)card->txbd_ring_pbase,
396 (u32)((u64)card->txbd_ring_pbase >> 32),
397 card->txbd_ring_size);
399 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
400 card->txbd_ring[i] = (struct mwifiex_pcie_buf_desc *)
401 (card->txbd_ring_vbase +
402 (sizeof(struct mwifiex_pcie_buf_desc) * i));
404 /* Allocate buffer here so that firmware can DMA data from it */
405 skb = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
406 if (!skb) {
407 dev_err(adapter->dev, "Unable to allocate skb for TX ring.\n");
408 kfree(card->txbd_ring_vbase);
409 return -ENOMEM;
411 buf_pa = mwifiex_update_sk_buff_pa(skb);
413 skb_put(skb, MWIFIEX_RX_DATA_BUF_SIZE);
414 dev_dbg(adapter->dev, "info: TX ring: add new skb base: %p, "
415 "buf_base: %p, buf_pbase: %#x:%x, "
416 "buf_len: %#x\n", skb, skb->data,
417 (u32)*buf_pa, (u32)(((u64)*buf_pa >> 32)),
418 skb->len);
420 card->tx_buf_list[i] = skb;
421 card->txbd_ring[i]->paddr = *buf_pa;
422 card->txbd_ring[i]->len = (u16)skb->len;
423 card->txbd_ring[i]->flags = 0;
426 return 0;
429 static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
431 struct pcie_service_card *card = adapter->card;
432 int i;
434 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
435 if (card->tx_buf_list[i])
436 dev_kfree_skb_any(card->tx_buf_list[i]);
437 card->tx_buf_list[i] = NULL;
438 card->txbd_ring[i]->paddr = 0;
439 card->txbd_ring[i]->len = 0;
440 card->txbd_ring[i]->flags = 0;
441 card->txbd_ring[i] = NULL;
444 kfree(card->txbd_ring_vbase);
445 card->txbd_ring_size = 0;
446 card->txbd_wrptr = 0;
447 card->txbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND;
448 card->txbd_ring_vbase = NULL;
450 return 0;
454 * This function creates buffer descriptor ring for RX
456 static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
458 struct pcie_service_card *card = adapter->card;
459 struct sk_buff *skb;
460 int i;
461 phys_addr_t *buf_pa;
464 * driver maintaines the read pointer and firmware maintaines the write
465 * pointer. The write pointer starts at 0 (zero) while the read pointer
466 * starts at zero with rollover bit set
468 card->rxbd_wrptr = 0;
469 card->rxbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND;
471 card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
472 MWIFIEX_MAX_TXRX_BD;
473 dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n",
474 card->rxbd_ring_size);
475 card->rxbd_ring_vbase = kzalloc(card->rxbd_ring_size, GFP_KERNEL);
476 if (!card->rxbd_ring_vbase) {
477 dev_err(adapter->dev, "Unable to allocate buffer for "
478 "rxbd_ring.\n");
479 return -ENOMEM;
481 card->rxbd_ring_pbase = virt_to_phys(card->rxbd_ring_vbase);
483 dev_dbg(adapter->dev, "info: rxbd_ring - base: %p, pbase: %#x:%x,"
484 "len: %#x\n", card->rxbd_ring_vbase,
485 (u32)card->rxbd_ring_pbase,
486 (u32)((u64)card->rxbd_ring_pbase >> 32),
487 card->rxbd_ring_size);
489 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
490 card->rxbd_ring[i] = (struct mwifiex_pcie_buf_desc *)
491 (card->rxbd_ring_vbase +
492 (sizeof(struct mwifiex_pcie_buf_desc) * i));
494 /* Allocate skb here so that firmware can DMA data from it */
495 skb = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
496 if (!skb) {
497 dev_err(adapter->dev, "Unable to allocate skb for RX ring.\n");
498 kfree(card->rxbd_ring_vbase);
499 return -ENOMEM;
501 buf_pa = mwifiex_update_sk_buff_pa(skb);
502 skb_put(skb, MWIFIEX_RX_DATA_BUF_SIZE);
504 dev_dbg(adapter->dev, "info: RX ring: add new skb base: %p, "
505 "buf_base: %p, buf_pbase: %#x:%x, "
506 "buf_len: %#x\n", skb, skb->data,
507 (u32)*buf_pa, (u32)((u64)*buf_pa >> 32),
508 skb->len);
510 card->rx_buf_list[i] = skb;
511 card->rxbd_ring[i]->paddr = *buf_pa;
512 card->rxbd_ring[i]->len = (u16)skb->len;
513 card->rxbd_ring[i]->flags = 0;
516 return 0;
520 * This function deletes Buffer descriptor ring for RX
522 static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
524 struct pcie_service_card *card = adapter->card;
525 int i;
527 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
528 if (card->rx_buf_list[i])
529 dev_kfree_skb_any(card->rx_buf_list[i]);
530 card->rx_buf_list[i] = NULL;
531 card->rxbd_ring[i]->paddr = 0;
532 card->rxbd_ring[i]->len = 0;
533 card->rxbd_ring[i]->flags = 0;
534 card->rxbd_ring[i] = NULL;
537 kfree(card->rxbd_ring_vbase);
538 card->rxbd_ring_size = 0;
539 card->rxbd_wrptr = 0;
540 card->rxbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND;
541 card->rxbd_ring_vbase = NULL;
543 return 0;
547 * This function creates buffer descriptor ring for Events
549 static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
551 struct pcie_service_card *card = adapter->card;
552 struct sk_buff *skb;
553 int i;
554 phys_addr_t *buf_pa;
557 * driver maintaines the read pointer and firmware maintaines the write
558 * pointer. The write pointer starts at 0 (zero) while the read pointer
559 * starts at zero with rollover bit set
561 card->evtbd_wrptr = 0;
562 card->evtbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND;
564 card->evtbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
565 MWIFIEX_MAX_EVT_BD;
566 dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n",
567 card->evtbd_ring_size);
568 card->evtbd_ring_vbase = kzalloc(card->evtbd_ring_size, GFP_KERNEL);
569 if (!card->evtbd_ring_vbase) {
570 dev_err(adapter->dev, "Unable to allocate buffer. "
571 "Terminating download\n");
572 return -ENOMEM;
574 card->evtbd_ring_pbase = virt_to_phys(card->evtbd_ring_vbase);
576 dev_dbg(adapter->dev, "info: CMDRSP/EVT bd_ring - base: %p, "
577 "pbase: %#x:%x, len: %#x\n", card->evtbd_ring_vbase,
578 (u32)card->evtbd_ring_pbase,
579 (u32)((u64)card->evtbd_ring_pbase >> 32),
580 card->evtbd_ring_size);
582 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
583 card->evtbd_ring[i] = (struct mwifiex_pcie_buf_desc *)
584 (card->evtbd_ring_vbase +
585 (sizeof(struct mwifiex_pcie_buf_desc) * i));
587 /* Allocate skb here so that firmware can DMA data from it */
588 skb = dev_alloc_skb(MAX_EVENT_SIZE);
589 if (!skb) {
590 dev_err(adapter->dev, "Unable to allocate skb for EVENT buf.\n");
591 kfree(card->evtbd_ring_vbase);
592 return -ENOMEM;
594 buf_pa = mwifiex_update_sk_buff_pa(skb);
595 skb_put(skb, MAX_EVENT_SIZE);
597 dev_dbg(adapter->dev, "info: Evt ring: add new skb. base: %p, "
598 "buf_base: %p, buf_pbase: %#x:%x, "
599 "buf_len: %#x\n", skb, skb->data,
600 (u32)*buf_pa, (u32)((u64)*buf_pa >> 32),
601 skb->len);
603 card->evt_buf_list[i] = skb;
604 card->evtbd_ring[i]->paddr = *buf_pa;
605 card->evtbd_ring[i]->len = (u16)skb->len;
606 card->evtbd_ring[i]->flags = 0;
609 return 0;
613 * This function deletes Buffer descriptor ring for Events
615 static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
617 struct pcie_service_card *card = adapter->card;
618 int i;
620 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
621 if (card->evt_buf_list[i])
622 dev_kfree_skb_any(card->evt_buf_list[i]);
623 card->evt_buf_list[i] = NULL;
624 card->evtbd_ring[i]->paddr = 0;
625 card->evtbd_ring[i]->len = 0;
626 card->evtbd_ring[i]->flags = 0;
627 card->evtbd_ring[i] = NULL;
630 kfree(card->evtbd_ring_vbase);
631 card->evtbd_wrptr = 0;
632 card->evtbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND;
633 card->evtbd_ring_size = 0;
634 card->evtbd_ring_vbase = NULL;
636 return 0;
640 * This function allocates a buffer for CMDRSP
642 static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
644 struct pcie_service_card *card = adapter->card;
645 struct sk_buff *skb;
647 /* Allocate memory for receiving command response data */
648 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
649 if (!skb) {
650 dev_err(adapter->dev, "Unable to allocate skb for command "
651 "response data.\n");
652 return -ENOMEM;
654 mwifiex_update_sk_buff_pa(skb);
655 skb_put(skb, MWIFIEX_UPLD_SIZE);
656 card->cmdrsp_buf = skb;
658 skb = NULL;
659 /* Allocate memory for sending command to firmware */
660 skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
661 if (!skb) {
662 dev_err(adapter->dev, "Unable to allocate skb for command "
663 "data.\n");
664 return -ENOMEM;
666 mwifiex_update_sk_buff_pa(skb);
667 skb_put(skb, MWIFIEX_SIZE_OF_CMD_BUFFER);
668 card->cmd_buf = skb;
670 return 0;
674 * This function deletes a buffer for CMDRSP
676 static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
678 struct pcie_service_card *card;
680 if (!adapter)
681 return 0;
683 card = adapter->card;
685 if (card && card->cmdrsp_buf)
686 dev_kfree_skb_any(card->cmdrsp_buf);
688 if (card && card->cmd_buf)
689 dev_kfree_skb_any(card->cmd_buf);
691 return 0;
695 * This function allocates a buffer for sleep cookie
697 static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
699 struct sk_buff *skb;
700 struct pcie_service_card *card = adapter->card;
702 /* Allocate memory for sleep cookie */
703 skb = dev_alloc_skb(sizeof(u32));
704 if (!skb) {
705 dev_err(adapter->dev, "Unable to allocate skb for sleep "
706 "cookie!\n");
707 return -ENOMEM;
709 mwifiex_update_sk_buff_pa(skb);
710 skb_put(skb, sizeof(u32));
712 /* Init val of Sleep Cookie */
713 *(u32 *)skb->data = FW_AWAKE_COOKIE;
715 dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n",
716 *((u32 *)skb->data));
718 /* Save the sleep cookie */
719 card->sleep_cookie = skb;
721 return 0;
725 * This function deletes buffer for sleep cookie
727 static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
729 struct pcie_service_card *card;
731 if (!adapter)
732 return 0;
734 card = adapter->card;
736 if (card && card->sleep_cookie) {
737 dev_kfree_skb_any(card->sleep_cookie);
738 card->sleep_cookie = NULL;
741 return 0;
745 * This function sends data buffer to device
747 static int
748 mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb)
750 struct pcie_service_card *card = adapter->card;
751 u32 wrindx, rdptr;
752 phys_addr_t *buf_pa;
753 __le16 *tmp;
755 if (!mwifiex_pcie_ok_to_access_hw(adapter))
756 mwifiex_pm_wakeup_card(adapter);
758 /* Read the TX ring read pointer set by firmware */
759 if (mwifiex_read_reg(adapter, REG_TXBD_RDPTR, &rdptr)) {
760 dev_err(adapter->dev, "SEND DATA: failed to read "
761 "REG_TXBD_RDPTR\n");
762 return -1;
765 wrindx = card->txbd_wrptr & MWIFIEX_TXBD_MASK;
767 dev_dbg(adapter->dev, "info: SEND DATA: <Rd: %#x, Wr: %#x>\n", rdptr,
768 card->txbd_wrptr);
769 if (((card->txbd_wrptr & MWIFIEX_TXBD_MASK) !=
770 (rdptr & MWIFIEX_TXBD_MASK)) ||
771 ((card->txbd_wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) !=
772 (rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) {
773 struct sk_buff *skb_data;
774 u8 *payload;
776 adapter->data_sent = true;
777 skb_data = card->tx_buf_list[wrindx];
778 memcpy(skb_data->data, skb->data, skb->len);
779 payload = skb_data->data;
780 tmp = (__le16 *)&payload[0];
781 *tmp = cpu_to_le16((u16)skb->len);
782 tmp = (__le16 *)&payload[2];
783 *tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
784 skb_put(skb_data, MWIFIEX_RX_DATA_BUF_SIZE - skb_data->len);
785 skb_trim(skb_data, skb->len);
786 buf_pa = MWIFIEX_SKB_PACB(skb_data);
787 card->txbd_ring[wrindx]->paddr = *buf_pa;
788 card->txbd_ring[wrindx]->len = (u16)skb_data->len;
789 card->txbd_ring[wrindx]->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
790 MWIFIEX_BD_FLAG_LAST_DESC;
792 if ((++card->txbd_wrptr & MWIFIEX_TXBD_MASK) ==
793 MWIFIEX_MAX_TXRX_BD)
794 card->txbd_wrptr = ((card->txbd_wrptr &
795 MWIFIEX_BD_FLAG_ROLLOVER_IND) ^
796 MWIFIEX_BD_FLAG_ROLLOVER_IND);
798 /* Write the TX ring write pointer in to REG_TXBD_WRPTR */
799 if (mwifiex_write_reg(adapter, REG_TXBD_WRPTR,
800 card->txbd_wrptr)) {
801 dev_err(adapter->dev, "SEND DATA: failed to write "
802 "REG_TXBD_WRPTR\n");
803 return 0;
806 /* Send the TX ready interrupt */
807 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
808 CPU_INTR_DNLD_RDY)) {
809 dev_err(adapter->dev, "SEND DATA: failed to assert "
810 "door-bell interrupt.\n");
811 return -1;
813 dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: "
814 "%#x> and sent packet to firmware "
815 "successfully\n", rdptr,
816 card->txbd_wrptr);
817 } else {
818 dev_dbg(adapter->dev, "info: TX Ring full, can't send anymore "
819 "packets to firmware\n");
820 adapter->data_sent = true;
821 /* Send the TX ready interrupt */
822 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
823 CPU_INTR_DNLD_RDY))
824 dev_err(adapter->dev, "SEND DATA: failed to assert "
825 "door-bell interrupt\n");
826 return -EBUSY;
829 return 0;
833 * This function handles received buffer ring and
834 * dispatches packets to upper
836 static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
838 struct pcie_service_card *card = adapter->card;
839 u32 wrptr, rd_index;
840 int ret = 0;
841 struct sk_buff *skb_tmp = NULL;
843 /* Read the RX ring Write pointer set by firmware */
844 if (mwifiex_read_reg(adapter, REG_RXBD_WRPTR, &wrptr)) {
845 dev_err(adapter->dev, "RECV DATA: failed to read "
846 "REG_TXBD_RDPTR\n");
847 ret = -1;
848 goto done;
851 while (((wrptr & MWIFIEX_RXBD_MASK) !=
852 (card->rxbd_rdptr & MWIFIEX_RXBD_MASK)) ||
853 ((wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) ==
854 (card->rxbd_rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) {
855 struct sk_buff *skb_data;
856 u16 rx_len;
858 rd_index = card->rxbd_rdptr & MWIFIEX_RXBD_MASK;
859 skb_data = card->rx_buf_list[rd_index];
861 /* Get data length from interface header -
862 first byte is len, second byte is type */
863 rx_len = *((u16 *)skb_data->data);
864 dev_dbg(adapter->dev, "info: RECV DATA: Rd=%#x, Wr=%#x, "
865 "Len=%d\n", card->rxbd_rdptr, wrptr, rx_len);
866 skb_tmp = dev_alloc_skb(rx_len);
867 if (!skb_tmp) {
868 dev_dbg(adapter->dev, "info: Failed to alloc skb "
869 "for RX\n");
870 ret = -EBUSY;
871 goto done;
874 skb_put(skb_tmp, rx_len);
876 memcpy(skb_tmp->data, skb_data->data + INTF_HEADER_LEN, rx_len);
877 if ((++card->rxbd_rdptr & MWIFIEX_RXBD_MASK) ==
878 MWIFIEX_MAX_TXRX_BD) {
879 card->rxbd_rdptr = ((card->rxbd_rdptr &
880 MWIFIEX_BD_FLAG_ROLLOVER_IND) ^
881 MWIFIEX_BD_FLAG_ROLLOVER_IND);
883 dev_dbg(adapter->dev, "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
884 card->rxbd_rdptr, wrptr);
886 /* Write the RX ring read pointer in to REG_RXBD_RDPTR */
887 if (mwifiex_write_reg(adapter, REG_RXBD_RDPTR,
888 card->rxbd_rdptr)) {
889 dev_err(adapter->dev, "RECV DATA: failed to "
890 "write REG_RXBD_RDPTR\n");
891 ret = -1;
892 goto done;
895 /* Read the RX ring Write pointer set by firmware */
896 if (mwifiex_read_reg(adapter, REG_RXBD_WRPTR, &wrptr)) {
897 dev_err(adapter->dev, "RECV DATA: failed to read "
898 "REG_TXBD_RDPTR\n");
899 ret = -1;
900 goto done;
902 dev_dbg(adapter->dev, "info: RECV DATA: Received packet from "
903 "firmware successfully\n");
904 mwifiex_handle_rx_packet(adapter, skb_tmp);
907 done:
908 if (ret && skb_tmp)
909 dev_kfree_skb_any(skb_tmp);
910 return ret;
914 * This function downloads the boot command to device
916 static int
917 mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
919 phys_addr_t *buf_pa = MWIFIEX_SKB_PACB(skb);
921 if (!(skb->data && skb->len && *buf_pa)) {
922 dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x:%x, "
923 "%x>\n", __func__, skb->data, skb->len,
924 (u32)*buf_pa, (u32)((u64)*buf_pa >> 32));
925 return -1;
928 /* Write the lower 32bits of the physical address to scratch
929 * register 0 */
930 if (mwifiex_write_reg(adapter, PCIE_SCRATCH_0_REG, (u32)*buf_pa)) {
931 dev_err(adapter->dev, "%s: failed to write download command "
932 "to boot code.\n", __func__);
933 return -1;
936 /* Write the upper 32bits of the physical address to scratch
937 * register 1 */
938 if (mwifiex_write_reg(adapter, PCIE_SCRATCH_1_REG,
939 (u32)((u64)*buf_pa >> 32))) {
940 dev_err(adapter->dev, "%s: failed to write download command "
941 "to boot code.\n", __func__);
942 return -1;
945 /* Write the command length to scratch register 2 */
946 if (mwifiex_write_reg(adapter, PCIE_SCRATCH_2_REG, skb->len)) {
947 dev_err(adapter->dev, "%s: failed to write command length to "
948 "scratch register 2\n", __func__);
949 return -1;
952 /* Ring the door bell */
953 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
954 CPU_INTR_DOOR_BELL)) {
955 dev_err(adapter->dev, "%s: failed to assert door-bell "
956 "interrupt.\n", __func__);
957 return -1;
960 return 0;
964 * This function downloads commands to the device
966 static int
967 mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
969 struct pcie_service_card *card = adapter->card;
970 int ret = 0;
971 phys_addr_t *cmd_buf_pa;
972 phys_addr_t *cmdrsp_buf_pa;
974 if (!(skb->data && skb->len)) {
975 dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x>\n",
976 __func__, skb->data, skb->len);
977 return -1;
980 /* Make sure a command response buffer is available */
981 if (!card->cmdrsp_buf) {
982 dev_err(adapter->dev, "No response buffer available, send "
983 "command failed\n");
984 return -EBUSY;
987 /* Make sure a command buffer is available */
988 if (!card->cmd_buf) {
989 dev_err(adapter->dev, "Command buffer not available\n");
990 return -EBUSY;
993 adapter->cmd_sent = true;
994 /* Copy the given skb in to DMA accessable shared buffer */
995 skb_put(card->cmd_buf, MWIFIEX_SIZE_OF_CMD_BUFFER - card->cmd_buf->len);
996 skb_trim(card->cmd_buf, skb->len);
997 memcpy(card->cmd_buf->data, skb->data, skb->len);
999 /* To send a command, the driver will:
1000 1. Write the 64bit physical address of the data buffer to
1001 SCRATCH1 + SCRATCH0
1002 2. Ring the door bell (i.e. set the door bell interrupt)
1004 In response to door bell interrupt, the firmware will perform
1005 the DMA of the command packet (first header to obtain the total
1006 length and then rest of the command).
1009 if (card->cmdrsp_buf) {
1010 cmdrsp_buf_pa = MWIFIEX_SKB_PACB(card->cmdrsp_buf);
1011 /* Write the lower 32bits of the cmdrsp buffer physical
1012 address */
1013 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_LO,
1014 (u32)*cmdrsp_buf_pa)) {
1015 dev_err(adapter->dev, "Failed to write download command to boot code.\n");
1016 ret = -1;
1017 goto done;
1019 /* Write the upper 32bits of the cmdrsp buffer physical
1020 address */
1021 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_HI,
1022 (u32)((u64)*cmdrsp_buf_pa >> 32))) {
1023 dev_err(adapter->dev, "Failed to write download command"
1024 " to boot code.\n");
1025 ret = -1;
1026 goto done;
1030 cmd_buf_pa = MWIFIEX_SKB_PACB(card->cmd_buf);
1031 /* Write the lower 32bits of the physical address to REG_CMD_ADDR_LO */
1032 if (mwifiex_write_reg(adapter, REG_CMD_ADDR_LO,
1033 (u32)*cmd_buf_pa)) {
1034 dev_err(adapter->dev, "Failed to write download command "
1035 "to boot code.\n");
1036 ret = -1;
1037 goto done;
1039 /* Write the upper 32bits of the physical address to REG_CMD_ADDR_HI */
1040 if (mwifiex_write_reg(adapter, REG_CMD_ADDR_HI,
1041 (u32)((u64)*cmd_buf_pa >> 32))) {
1042 dev_err(adapter->dev, "Failed to write download command "
1043 "to boot code.\n");
1044 ret = -1;
1045 goto done;
1048 /* Write the command length to REG_CMD_SIZE */
1049 if (mwifiex_write_reg(adapter, REG_CMD_SIZE,
1050 card->cmd_buf->len)) {
1051 dev_err(adapter->dev, "Failed to write command length to "
1052 "REG_CMD_SIZE\n");
1053 ret = -1;
1054 goto done;
1057 /* Ring the door bell */
1058 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1059 CPU_INTR_DOOR_BELL)) {
1060 dev_err(adapter->dev, "Failed to assert door-bell "
1061 "interrupt.\n");
1062 ret = -1;
1063 goto done;
1066 done:
1067 if (ret)
1068 adapter->cmd_sent = false;
1070 return 0;
1074 * This function handles command complete interrupt
1076 static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1078 struct pcie_service_card *card = adapter->card;
1079 int count = 0;
1081 dev_dbg(adapter->dev, "info: Rx CMD Response\n");
1083 if (!adapter->curr_cmd) {
1084 skb_pull(card->cmdrsp_buf, INTF_HEADER_LEN);
1085 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
1086 mwifiex_process_sleep_confirm_resp(adapter,
1087 card->cmdrsp_buf->data,
1088 card->cmdrsp_buf->len);
1089 while (mwifiex_pcie_ok_to_access_hw(adapter) &&
1090 (count++ < 10))
1091 udelay(50);
1092 } else {
1093 dev_err(adapter->dev, "There is no command but "
1094 "got cmdrsp\n");
1096 memcpy(adapter->upld_buf, card->cmdrsp_buf->data,
1097 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1098 card->cmdrsp_buf->len));
1099 skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
1100 } else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1101 skb_pull(card->cmdrsp_buf, INTF_HEADER_LEN);
1102 adapter->curr_cmd->resp_skb = card->cmdrsp_buf;
1103 adapter->cmd_resp_received = true;
1104 /* Take the pointer and set it to CMD node and will
1105 return in the response complete callback */
1106 card->cmdrsp_buf = NULL;
1108 /* Clear the cmd-rsp buffer address in scratch registers. This
1109 will prevent firmware from writing to the same response
1110 buffer again. */
1111 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_LO, 0)) {
1112 dev_err(adapter->dev, "cmd_done: failed to clear "
1113 "cmd_rsp address.\n");
1114 return -1;
1116 /* Write the upper 32bits of the cmdrsp buffer physical
1117 address */
1118 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_HI, 0)) {
1119 dev_err(adapter->dev, "cmd_done: failed to clear "
1120 "cmd_rsp address.\n");
1121 return -1;
1125 return 0;
1129 * Command Response processing complete handler
1131 static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1132 struct sk_buff *skb)
1134 struct pcie_service_card *card = adapter->card;
1136 if (skb) {
1137 card->cmdrsp_buf = skb;
1138 skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
1141 return 0;
1145 * This function handles firmware event ready interrupt
1147 static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1149 struct pcie_service_card *card = adapter->card;
1150 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1151 u32 wrptr, event;
1153 if (adapter->event_received) {
1154 dev_dbg(adapter->dev, "info: Event being processed, "\
1155 "do not process this interrupt just yet\n");
1156 return 0;
1159 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1160 dev_dbg(adapter->dev, "info: Invalid read pointer...\n");
1161 return -1;
1164 /* Read the event ring write pointer set by firmware */
1165 if (mwifiex_read_reg(adapter, REG_EVTBD_WRPTR, &wrptr)) {
1166 dev_err(adapter->dev, "EventReady: failed to read REG_EVTBD_WRPTR\n");
1167 return -1;
1170 dev_dbg(adapter->dev, "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
1171 card->evtbd_rdptr, wrptr);
1172 if (((wrptr & MWIFIEX_EVTBD_MASK) !=
1173 (card->evtbd_rdptr & MWIFIEX_EVTBD_MASK)) ||
1174 ((wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) ==
1175 (card->evtbd_rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) {
1176 struct sk_buff *skb_cmd;
1177 __le16 data_len = 0;
1178 u16 evt_len;
1180 dev_dbg(adapter->dev, "info: Read Index: %d\n", rdptr);
1181 skb_cmd = card->evt_buf_list[rdptr];
1182 /* Take the pointer and set it to event pointer in adapter
1183 and will return back after event handling callback */
1184 card->evt_buf_list[rdptr] = NULL;
1185 card->evtbd_ring[rdptr]->paddr = 0;
1186 card->evtbd_ring[rdptr]->len = 0;
1187 card->evtbd_ring[rdptr]->flags = 0;
1189 event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
1190 adapter->event_cause = event;
1191 /* The first 4bytes will be the event transfer header
1192 len is 2 bytes followed by type which is 2 bytes */
1193 memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1194 evt_len = le16_to_cpu(data_len);
1196 skb_pull(skb_cmd, INTF_HEADER_LEN);
1197 dev_dbg(adapter->dev, "info: Event length: %d\n", evt_len);
1199 if ((evt_len > 0) && (evt_len < MAX_EVENT_SIZE))
1200 memcpy(adapter->event_body, skb_cmd->data +
1201 MWIFIEX_EVENT_HEADER_LEN, evt_len -
1202 MWIFIEX_EVENT_HEADER_LEN);
1204 adapter->event_received = true;
1205 adapter->event_skb = skb_cmd;
1207 /* Do not update the event read pointer here, wait till the
1208 buffer is released. This is just to make things simpler,
1209 we need to find a better method of managing these buffers.
1213 return 0;
1217 * Event processing complete handler
1219 static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1220 struct sk_buff *skb)
1222 struct pcie_service_card *card = adapter->card;
1223 int ret = 0;
1224 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1225 u32 wrptr;
1226 phys_addr_t *buf_pa;
1228 if (!skb)
1229 return 0;
1231 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1232 dev_err(adapter->dev, "event_complete: Invalid rdptr 0x%x\n",
1233 rdptr);
1234 return -EINVAL;
1237 /* Read the event ring write pointer set by firmware */
1238 if (mwifiex_read_reg(adapter, REG_EVTBD_WRPTR, &wrptr)) {
1239 dev_err(adapter->dev, "event_complete: failed to read REG_EVTBD_WRPTR\n");
1240 return -1;
1243 if (!card->evt_buf_list[rdptr]) {
1244 skb_push(skb, INTF_HEADER_LEN);
1245 card->evt_buf_list[rdptr] = skb;
1246 buf_pa = MWIFIEX_SKB_PACB(skb);
1247 card->evtbd_ring[rdptr]->paddr = *buf_pa;
1248 card->evtbd_ring[rdptr]->len = (u16)skb->len;
1249 card->evtbd_ring[rdptr]->flags = 0;
1250 skb = NULL;
1251 } else {
1252 dev_dbg(adapter->dev, "info: ERROR: Buffer is still valid at "
1253 "index %d, <%p, %p>\n", rdptr,
1254 card->evt_buf_list[rdptr], skb);
1257 if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
1258 card->evtbd_rdptr = ((card->evtbd_rdptr &
1259 MWIFIEX_BD_FLAG_ROLLOVER_IND) ^
1260 MWIFIEX_BD_FLAG_ROLLOVER_IND);
1263 dev_dbg(adapter->dev, "info: Updated <Rd: 0x%x, Wr: 0x%x>",
1264 card->evtbd_rdptr, wrptr);
1266 /* Write the event ring read pointer in to REG_EVTBD_RDPTR */
1267 if (mwifiex_write_reg(adapter, REG_EVTBD_RDPTR, card->evtbd_rdptr)) {
1268 dev_err(adapter->dev, "event_complete: failed to read REG_EVTBD_RDPTR\n");
1269 return -1;
1272 dev_dbg(adapter->dev, "info: Check Events Again\n");
1273 ret = mwifiex_pcie_process_event_ready(adapter);
1275 return ret;
1279 * This function downloads the firmware to the card.
1281 * Firmware is downloaded to the card in blocks. Every block download
1282 * is tested for CRC errors, and retried a number of times before
1283 * returning failure.
1285 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1286 struct mwifiex_fw_image *fw)
1288 int ret;
1289 u8 *firmware = fw->fw_buf;
1290 u32 firmware_len = fw->fw_len;
1291 u32 offset = 0;
1292 struct sk_buff *skb;
1293 u32 txlen, tx_blocks = 0, tries, len;
1294 u32 block_retry_cnt = 0;
1296 if (!adapter) {
1297 pr_err("adapter structure is not valid\n");
1298 return -1;
1301 if (!firmware || !firmware_len) {
1302 dev_err(adapter->dev, "No firmware image found! "
1303 "Terminating download\n");
1304 return -1;
1307 dev_dbg(adapter->dev, "info: Downloading FW image (%d bytes)\n",
1308 firmware_len);
1310 if (mwifiex_pcie_disable_host_int(adapter)) {
1311 dev_err(adapter->dev, "%s: Disabling interrupts"
1312 " failed.\n", __func__);
1313 return -1;
1316 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
1317 if (!skb) {
1318 ret = -ENOMEM;
1319 goto done;
1321 mwifiex_update_sk_buff_pa(skb);
1323 /* Perform firmware data transfer */
1324 do {
1325 u32 ireg_intr = 0;
1327 /* More data? */
1328 if (offset >= firmware_len)
1329 break;
1331 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1332 ret = mwifiex_read_reg(adapter, PCIE_SCRATCH_2_REG,
1333 &len);
1334 if (ret) {
1335 dev_warn(adapter->dev, "Failed reading length from boot code\n");
1336 goto done;
1338 if (len)
1339 break;
1340 udelay(10);
1343 if (!len) {
1344 break;
1345 } else if (len > MWIFIEX_UPLD_SIZE) {
1346 pr_err("FW download failure @ %d, invalid length %d\n",
1347 offset, len);
1348 ret = -1;
1349 goto done;
1352 txlen = len;
1354 if (len & BIT(0)) {
1355 block_retry_cnt++;
1356 if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
1357 pr_err("FW download failure @ %d, over max "
1358 "retry count\n", offset);
1359 ret = -1;
1360 goto done;
1362 dev_err(adapter->dev, "FW CRC error indicated by the "
1363 "helper: len = 0x%04X, txlen = "
1364 "%d\n", len, txlen);
1365 len &= ~BIT(0);
1366 /* Setting this to 0 to resend from same offset */
1367 txlen = 0;
1368 } else {
1369 block_retry_cnt = 0;
1370 /* Set blocksize to transfer - checking for
1371 last block */
1372 if (firmware_len - offset < txlen)
1373 txlen = firmware_len - offset;
1375 dev_dbg(adapter->dev, ".");
1377 tx_blocks =
1378 (txlen + MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD - 1) /
1379 MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD;
1381 /* Copy payload to buffer */
1382 memmove(skb->data, &firmware[offset], txlen);
1385 skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1386 skb_trim(skb, tx_blocks * MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD);
1388 /* Send the boot command to device */
1389 if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
1390 dev_err(adapter->dev, "Failed to send firmware download command\n");
1391 ret = -1;
1392 goto done;
1394 /* Wait for the command done interrupt */
1395 do {
1396 if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
1397 &ireg_intr)) {
1398 dev_err(adapter->dev, "%s: Failed to read "
1399 "interrupt status during "
1400 "fw dnld.\n", __func__);
1401 ret = -1;
1402 goto done;
1404 } while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
1405 CPU_INTR_DOOR_BELL);
1406 offset += txlen;
1407 } while (true);
1409 dev_dbg(adapter->dev, "info:\nFW download over, size %d bytes\n",
1410 offset);
1412 ret = 0;
1414 done:
1415 dev_kfree_skb_any(skb);
1416 return ret;
1420 * This function checks the firmware status in card.
1422 * The winner interface is also determined by this function.
1424 static int
1425 mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
1427 int ret = 0;
1428 u32 firmware_stat, winner_status;
1429 u32 tries;
1431 /* Mask spurios interrupts */
1432 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
1433 HOST_INTR_MASK)) {
1434 dev_warn(adapter->dev, "Write register failed\n");
1435 return -1;
1438 dev_dbg(adapter->dev, "Setting driver ready signature\n");
1439 if (mwifiex_write_reg(adapter, REG_DRV_READY, FIRMWARE_READY_PCIE)) {
1440 dev_err(adapter->dev, "Failed to write driver ready signature\n");
1441 return -1;
1444 /* Wait for firmware initialization event */
1445 for (tries = 0; tries < poll_num; tries++) {
1446 if (mwifiex_read_reg(adapter, PCIE_SCRATCH_3_REG,
1447 &firmware_stat))
1448 ret = -1;
1449 else
1450 ret = 0;
1451 if (ret)
1452 continue;
1453 if (firmware_stat == FIRMWARE_READY_PCIE) {
1454 ret = 0;
1455 break;
1456 } else {
1457 mdelay(100);
1458 ret = -1;
1462 if (ret) {
1463 if (mwifiex_read_reg(adapter, PCIE_SCRATCH_3_REG,
1464 &winner_status))
1465 ret = -1;
1466 else if (!winner_status) {
1467 dev_err(adapter->dev, "PCI-E is the winner\n");
1468 adapter->winner = 1;
1469 ret = -1;
1470 } else {
1471 dev_err(adapter->dev, "PCI-E is not the winner <%#x, %d>, exit download\n",
1472 ret, adapter->winner);
1473 ret = 0;
1477 return ret;
1481 * This function reads the interrupt status from card.
1483 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
1485 u32 pcie_ireg;
1486 unsigned long flags;
1488 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1489 return;
1491 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) {
1492 dev_warn(adapter->dev, "Read register failed\n");
1493 return;
1496 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
1498 mwifiex_pcie_disable_host_int(adapter);
1500 /* Clear the pending interrupts */
1501 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
1502 ~pcie_ireg)) {
1503 dev_warn(adapter->dev, "Write register failed\n");
1504 return;
1506 spin_lock_irqsave(&adapter->int_lock, flags);
1507 adapter->int_status |= pcie_ireg;
1508 spin_unlock_irqrestore(&adapter->int_lock, flags);
1510 if (pcie_ireg & HOST_INTR_CMD_DONE) {
1511 if ((adapter->ps_state == PS_STATE_SLEEP_CFM) ||
1512 (adapter->ps_state == PS_STATE_SLEEP)) {
1513 mwifiex_pcie_enable_host_int(adapter);
1514 if (mwifiex_write_reg(adapter,
1515 PCIE_CPU_INT_EVENT,
1516 CPU_INTR_SLEEP_CFM_DONE)) {
1517 dev_warn(adapter->dev, "Write register"
1518 " failed\n");
1519 return;
1523 } else if (!adapter->pps_uapsd_mode &&
1524 adapter->ps_state == PS_STATE_SLEEP) {
1525 /* Potentially for PCIe we could get other
1526 * interrupts like shared. Don't change power
1527 * state until cookie is set */
1528 if (mwifiex_pcie_ok_to_access_hw(adapter))
1529 adapter->ps_state = PS_STATE_AWAKE;
1535 * Interrupt handler for PCIe root port
1537 * This function reads the interrupt status from firmware and assigns
1538 * the main process in workqueue which will handle the interrupt.
1540 static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
1542 struct pci_dev *pdev = (struct pci_dev *)context;
1543 struct pcie_service_card *card;
1544 struct mwifiex_adapter *adapter;
1546 if (!pdev) {
1547 pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev);
1548 goto exit;
1551 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
1552 if (!card || !card->adapter) {
1553 pr_debug("info: %s: card=%p adapter=%p\n", __func__, card,
1554 card ? card->adapter : NULL);
1555 goto exit;
1557 adapter = card->adapter;
1559 if (adapter->surprise_removed)
1560 goto exit;
1562 mwifiex_interrupt_status(adapter);
1563 queue_work(adapter->workqueue, &adapter->main_work);
1565 exit:
1566 return IRQ_HANDLED;
1570 * This function checks the current interrupt status.
1572 * The following interrupts are checked and handled by this function -
1573 * - Data sent
1574 * - Command sent
1575 * - Command received
1576 * - Packets received
1577 * - Events received
1579 * In case of Rx packets received, the packets are uploaded from card to
1580 * host and processed accordingly.
1582 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1584 int ret;
1585 u32 pcie_ireg = 0;
1586 unsigned long flags;
1588 spin_lock_irqsave(&adapter->int_lock, flags);
1589 /* Clear out unused interrupts */
1590 adapter->int_status &= HOST_INTR_MASK;
1591 spin_unlock_irqrestore(&adapter->int_lock, flags);
1593 while (adapter->int_status & HOST_INTR_MASK) {
1594 if (adapter->int_status & HOST_INTR_DNLD_DONE) {
1595 adapter->int_status &= ~HOST_INTR_DNLD_DONE;
1596 if (adapter->data_sent) {
1597 dev_dbg(adapter->dev, "info: DATA sent Interrupt\n");
1598 adapter->data_sent = false;
1601 if (adapter->int_status & HOST_INTR_UPLD_RDY) {
1602 adapter->int_status &= ~HOST_INTR_UPLD_RDY;
1603 dev_dbg(adapter->dev, "info: Rx DATA\n");
1604 ret = mwifiex_pcie_process_recv_data(adapter);
1605 if (ret)
1606 return ret;
1608 if (adapter->int_status & HOST_INTR_EVENT_RDY) {
1609 adapter->int_status &= ~HOST_INTR_EVENT_RDY;
1610 dev_dbg(adapter->dev, "info: Rx EVENT\n");
1611 ret = mwifiex_pcie_process_event_ready(adapter);
1612 if (ret)
1613 return ret;
1616 if (adapter->int_status & HOST_INTR_CMD_DONE) {
1617 adapter->int_status &= ~HOST_INTR_CMD_DONE;
1618 if (adapter->cmd_sent) {
1619 dev_dbg(adapter->dev, "info: CMD sent Interrupt\n");
1620 adapter->cmd_sent = false;
1622 /* Handle command response */
1623 ret = mwifiex_pcie_process_cmd_complete(adapter);
1624 if (ret)
1625 return ret;
1628 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1629 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
1630 &pcie_ireg)) {
1631 dev_warn(adapter->dev, "Read register failed\n");
1632 return -1;
1635 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
1636 if (mwifiex_write_reg(adapter,
1637 PCIE_HOST_INT_STATUS, ~pcie_ireg)) {
1638 dev_warn(adapter->dev, "Write register"
1639 " failed\n");
1640 return -1;
1642 adapter->int_status |= pcie_ireg;
1643 adapter->int_status &= HOST_INTR_MASK;
1648 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1649 adapter->cmd_sent, adapter->data_sent);
1650 mwifiex_pcie_enable_host_int(adapter);
1652 return 0;
1656 * This function downloads data from driver to card.
1658 * Both commands and data packets are transferred to the card by this
1659 * function.
1661 * This function adds the PCIE specific header to the front of the buffer
1662 * before transferring. The header contains the length of the packet and
1663 * the type. The firmware handles the packets based upon this set type.
1665 static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
1666 struct sk_buff *skb,
1667 struct mwifiex_tx_param *tx_param)
1669 if (!skb) {
1670 dev_err(adapter->dev, "Passed NULL skb to %s\n", __func__);
1671 return -1;
1674 if (type == MWIFIEX_TYPE_DATA)
1675 return mwifiex_pcie_send_data(adapter, skb);
1676 else if (type == MWIFIEX_TYPE_CMD)
1677 return mwifiex_pcie_send_cmd(adapter, skb);
1679 return 0;
1683 * This function initializes the PCI-E host memory space, WCB rings, etc.
1685 * The following initializations steps are followed -
1686 * - Allocate TXBD ring buffers
1687 * - Allocate RXBD ring buffers
1688 * - Allocate event BD ring buffers
1689 * - Allocate command response ring buffer
1690 * - Allocate sleep cookie buffer
1692 static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
1694 struct pcie_service_card *card = adapter->card;
1695 int ret;
1696 struct pci_dev *pdev = card->dev;
1698 pci_set_drvdata(pdev, card);
1700 ret = pci_enable_device(pdev);
1701 if (ret)
1702 goto err_enable_dev;
1704 pci_set_master(pdev);
1706 dev_dbg(adapter->dev, "try set_consistent_dma_mask(32)\n");
1707 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1708 if (ret) {
1709 dev_err(adapter->dev, "set_dma_mask(32) failed\n");
1710 goto err_set_dma_mask;
1713 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1714 if (ret) {
1715 dev_err(adapter->dev, "set_consistent_dma_mask(64) failed\n");
1716 goto err_set_dma_mask;
1719 ret = pci_request_region(pdev, 0, DRV_NAME);
1720 if (ret) {
1721 dev_err(adapter->dev, "req_reg(0) error\n");
1722 goto err_req_region0;
1724 card->pci_mmap = pci_iomap(pdev, 0, 0);
1725 if (!card->pci_mmap) {
1726 dev_err(adapter->dev, "iomap(0) error\n");
1727 goto err_iomap0;
1729 ret = pci_request_region(pdev, 2, DRV_NAME);
1730 if (ret) {
1731 dev_err(adapter->dev, "req_reg(2) error\n");
1732 goto err_req_region2;
1734 card->pci_mmap1 = pci_iomap(pdev, 2, 0);
1735 if (!card->pci_mmap1) {
1736 dev_err(adapter->dev, "iomap(2) error\n");
1737 goto err_iomap2;
1740 dev_dbg(adapter->dev, "PCI memory map Virt0: %p PCI memory map Virt2: "
1741 "%p\n", card->pci_mmap, card->pci_mmap1);
1743 card->cmdrsp_buf = NULL;
1744 ret = mwifiex_pcie_create_txbd_ring(adapter);
1745 if (ret)
1746 goto err_cre_txbd;
1747 ret = mwifiex_pcie_create_rxbd_ring(adapter);
1748 if (ret)
1749 goto err_cre_rxbd;
1750 ret = mwifiex_pcie_create_evtbd_ring(adapter);
1751 if (ret)
1752 goto err_cre_evtbd;
1753 ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
1754 if (ret)
1755 goto err_alloc_cmdbuf;
1756 ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
1757 if (ret)
1758 goto err_alloc_cookie;
1760 return ret;
1762 err_alloc_cookie:
1763 mwifiex_pcie_delete_cmdrsp_buf(adapter);
1764 err_alloc_cmdbuf:
1765 mwifiex_pcie_delete_evtbd_ring(adapter);
1766 err_cre_evtbd:
1767 mwifiex_pcie_delete_rxbd_ring(adapter);
1768 err_cre_rxbd:
1769 mwifiex_pcie_delete_txbd_ring(adapter);
1770 err_cre_txbd:
1771 pci_iounmap(pdev, card->pci_mmap1);
1772 err_iomap2:
1773 pci_release_region(pdev, 2);
1774 err_req_region2:
1775 pci_iounmap(pdev, card->pci_mmap);
1776 err_iomap0:
1777 pci_release_region(pdev, 0);
1778 err_req_region0:
1779 err_set_dma_mask:
1780 pci_disable_device(pdev);
1781 err_enable_dev:
1782 pci_set_drvdata(pdev, NULL);
1783 return ret;
1787 * This function cleans up the allocated card buffers.
1789 * The following are freed by this function -
1790 * - TXBD ring buffers
1791 * - RXBD ring buffers
1792 * - Event BD ring buffers
1793 * - Command response ring buffer
1794 * - Sleep cookie buffer
1796 static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
1798 struct pcie_service_card *card = adapter->card;
1799 struct pci_dev *pdev = card->dev;
1801 mwifiex_pcie_delete_sleep_cookie_buf(adapter);
1802 mwifiex_pcie_delete_cmdrsp_buf(adapter);
1803 mwifiex_pcie_delete_evtbd_ring(adapter);
1804 mwifiex_pcie_delete_rxbd_ring(adapter);
1805 mwifiex_pcie_delete_txbd_ring(adapter);
1806 card->cmdrsp_buf = NULL;
1808 dev_dbg(adapter->dev, "Clearing driver ready signature\n");
1809 if (user_rmmod) {
1810 if (mwifiex_write_reg(adapter, REG_DRV_READY, 0x00000000))
1811 dev_err(adapter->dev, "Failed to write driver not-ready signature\n");
1814 if (pdev) {
1815 pci_iounmap(pdev, card->pci_mmap);
1816 pci_iounmap(pdev, card->pci_mmap1);
1818 pci_release_regions(pdev);
1819 pci_disable_device(pdev);
1820 pci_set_drvdata(pdev, NULL);
1825 * This function registers the PCIE device.
1827 * PCIE IRQ is claimed, block size is set and driver data is initialized.
1829 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1831 int ret;
1832 struct pcie_service_card *card = adapter->card;
1833 struct pci_dev *pdev = card->dev;
1835 /* save adapter pointer in card */
1836 card->adapter = adapter;
1838 ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
1839 "MRVL_PCIE", pdev);
1840 if (ret) {
1841 pr_err("request_irq failed: ret=%d\n", ret);
1842 adapter->card = NULL;
1843 return -1;
1846 adapter->dev = &pdev->dev;
1847 strcpy(adapter->fw_name, PCIE8766_DEFAULT_FW_NAME);
1849 return 0;
1853 * This function unregisters the PCIE device.
1855 * The PCIE IRQ is released, the function is disabled and driver
1856 * data is set to null.
1858 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1860 struct pcie_service_card *card = adapter->card;
1862 if (card) {
1863 dev_dbg(adapter->dev, "%s(): calling free_irq()\n", __func__);
1864 free_irq(card->dev->irq, card->dev);
1868 static struct mwifiex_if_ops pcie_ops = {
1869 .init_if = mwifiex_pcie_init,
1870 .cleanup_if = mwifiex_pcie_cleanup,
1871 .check_fw_status = mwifiex_check_fw_status,
1872 .prog_fw = mwifiex_prog_fw_w_helper,
1873 .register_dev = mwifiex_register_dev,
1874 .unregister_dev = mwifiex_unregister_dev,
1875 .enable_int = mwifiex_pcie_enable_host_int,
1876 .process_int_status = mwifiex_process_int_status,
1877 .host_to_card = mwifiex_pcie_host_to_card,
1878 .wakeup = mwifiex_pm_wakeup_card,
1879 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1881 /* PCIE specific */
1882 .cmdrsp_complete = mwifiex_pcie_cmdrsp_complete,
1883 .event_complete = mwifiex_pcie_event_complete,
1884 .update_mp_end_port = NULL,
1885 .cleanup_mpa_buf = NULL,
1889 * This function initializes the PCIE driver module.
1891 * This initiates the semaphore and registers the device with
1892 * PCIE bus.
1894 static int mwifiex_pcie_init_module(void)
1896 int ret;
1898 pr_debug("Marvell 8766 PCIe Driver\n");
1900 sema_init(&add_remove_card_sem, 1);
1902 /* Clear the flag in case user removes the card. */
1903 user_rmmod = 0;
1905 ret = pci_register_driver(&mwifiex_pcie);
1906 if (ret)
1907 pr_err("Driver register failed!\n");
1908 else
1909 pr_debug("info: Driver registered successfully!\n");
1911 return ret;
1915 * This function cleans up the PCIE driver.
1917 * The following major steps are followed for cleanup -
1918 * - Resume the device if its suspended
1919 * - Disconnect the device if connected
1920 * - Shutdown the firmware
1921 * - Unregister the device from PCIE bus.
1923 static void mwifiex_pcie_cleanup_module(void)
1925 if (!down_interruptible(&add_remove_card_sem))
1926 up(&add_remove_card_sem);
1928 /* Set the flag as user is removing this module. */
1929 user_rmmod = 1;
1931 pci_unregister_driver(&mwifiex_pcie);
1934 module_init(mwifiex_pcie_init_module);
1935 module_exit(mwifiex_pcie_cleanup_module);
1937 MODULE_AUTHOR("Marvell International Ltd.");
1938 MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
1939 MODULE_VERSION(PCIE_VERSION);
1940 MODULE_LICENSE("GPL v2");
1941 MODULE_FIRMWARE("mrvl/pcie8766_uapsta.bin");