Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / net / wireless / mwifiex / pcie.c
blobd34acf082d3ae03fdc0b35b58f19a924415915a0
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 kfree(card->txbd_ring_vbase);
390 return -1;
392 card->txbd_ring_pbase = virt_to_phys(card->txbd_ring_vbase);
394 dev_dbg(adapter->dev, "info: txbd_ring - base: %p, pbase: %#x:%x,"
395 "len: %x\n", card->txbd_ring_vbase,
396 (u32)card->txbd_ring_pbase,
397 (u32)((u64)card->txbd_ring_pbase >> 32),
398 card->txbd_ring_size);
400 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
401 card->txbd_ring[i] = (struct mwifiex_pcie_buf_desc *)
402 (card->txbd_ring_vbase +
403 (sizeof(struct mwifiex_pcie_buf_desc) * i));
405 /* Allocate buffer here so that firmware can DMA data from it */
406 skb = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
407 if (!skb) {
408 dev_err(adapter->dev, "Unable to allocate skb for TX ring.\n");
409 kfree(card->txbd_ring_vbase);
410 return -ENOMEM;
412 buf_pa = mwifiex_update_sk_buff_pa(skb);
414 skb_put(skb, MWIFIEX_RX_DATA_BUF_SIZE);
415 dev_dbg(adapter->dev, "info: TX ring: add new skb base: %p, "
416 "buf_base: %p, buf_pbase: %#x:%x, "
417 "buf_len: %#x\n", skb, skb->data,
418 (u32)*buf_pa, (u32)(((u64)*buf_pa >> 32)),
419 skb->len);
421 card->tx_buf_list[i] = skb;
422 card->txbd_ring[i]->paddr = *buf_pa;
423 card->txbd_ring[i]->len = (u16)skb->len;
424 card->txbd_ring[i]->flags = 0;
427 return 0;
430 static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
432 struct pcie_service_card *card = adapter->card;
433 int i;
435 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
436 if (card->tx_buf_list[i])
437 dev_kfree_skb_any(card->tx_buf_list[i]);
438 card->tx_buf_list[i] = NULL;
439 card->txbd_ring[i]->paddr = 0;
440 card->txbd_ring[i]->len = 0;
441 card->txbd_ring[i]->flags = 0;
442 card->txbd_ring[i] = NULL;
445 kfree(card->txbd_ring_vbase);
446 card->txbd_ring_size = 0;
447 card->txbd_wrptr = 0;
448 card->txbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND;
449 card->txbd_ring_vbase = NULL;
451 return 0;
455 * This function creates buffer descriptor ring for RX
457 static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
459 struct pcie_service_card *card = adapter->card;
460 struct sk_buff *skb;
461 int i;
462 phys_addr_t *buf_pa;
465 * driver maintaines the read pointer and firmware maintaines the write
466 * pointer. The write pointer starts at 0 (zero) while the read pointer
467 * starts at zero with rollover bit set
469 card->rxbd_wrptr = 0;
470 card->rxbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND;
472 card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
473 MWIFIEX_MAX_TXRX_BD;
474 dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n",
475 card->rxbd_ring_size);
476 card->rxbd_ring_vbase = kzalloc(card->rxbd_ring_size, GFP_KERNEL);
477 if (!card->rxbd_ring_vbase) {
478 dev_err(adapter->dev, "Unable to allocate buffer for "
479 "rxbd_ring.\n");
480 return -1;
482 card->rxbd_ring_pbase = virt_to_phys(card->rxbd_ring_vbase);
484 dev_dbg(adapter->dev, "info: rxbd_ring - base: %p, pbase: %#x:%x,"
485 "len: %#x\n", card->rxbd_ring_vbase,
486 (u32)card->rxbd_ring_pbase,
487 (u32)((u64)card->rxbd_ring_pbase >> 32),
488 card->rxbd_ring_size);
490 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
491 card->rxbd_ring[i] = (struct mwifiex_pcie_buf_desc *)
492 (card->rxbd_ring_vbase +
493 (sizeof(struct mwifiex_pcie_buf_desc) * i));
495 /* Allocate skb here so that firmware can DMA data from it */
496 skb = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
497 if (!skb) {
498 dev_err(adapter->dev, "Unable to allocate skb for RX ring.\n");
499 kfree(card->rxbd_ring_vbase);
500 return -ENOMEM;
502 buf_pa = mwifiex_update_sk_buff_pa(skb);
503 skb_put(skb, MWIFIEX_RX_DATA_BUF_SIZE);
505 dev_dbg(adapter->dev, "info: RX ring: add new skb base: %p, "
506 "buf_base: %p, buf_pbase: %#x:%x, "
507 "buf_len: %#x\n", skb, skb->data,
508 (u32)*buf_pa, (u32)((u64)*buf_pa >> 32),
509 skb->len);
511 card->rx_buf_list[i] = skb;
512 card->rxbd_ring[i]->paddr = *buf_pa;
513 card->rxbd_ring[i]->len = (u16)skb->len;
514 card->rxbd_ring[i]->flags = 0;
517 return 0;
521 * This function deletes Buffer descriptor ring for RX
523 static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
525 struct pcie_service_card *card = adapter->card;
526 int i;
528 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
529 if (card->rx_buf_list[i])
530 dev_kfree_skb_any(card->rx_buf_list[i]);
531 card->rx_buf_list[i] = NULL;
532 card->rxbd_ring[i]->paddr = 0;
533 card->rxbd_ring[i]->len = 0;
534 card->rxbd_ring[i]->flags = 0;
535 card->rxbd_ring[i] = NULL;
538 kfree(card->rxbd_ring_vbase);
539 card->rxbd_ring_size = 0;
540 card->rxbd_wrptr = 0;
541 card->rxbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND;
542 card->rxbd_ring_vbase = NULL;
544 return 0;
548 * This function creates buffer descriptor ring for Events
550 static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
552 struct pcie_service_card *card = adapter->card;
553 struct sk_buff *skb;
554 int i;
555 phys_addr_t *buf_pa;
558 * driver maintaines the read pointer and firmware maintaines the write
559 * pointer. The write pointer starts at 0 (zero) while the read pointer
560 * starts at zero with rollover bit set
562 card->evtbd_wrptr = 0;
563 card->evtbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND;
565 card->evtbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
566 MWIFIEX_MAX_EVT_BD;
567 dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n",
568 card->evtbd_ring_size);
569 card->evtbd_ring_vbase = kzalloc(card->evtbd_ring_size, GFP_KERNEL);
570 if (!card->evtbd_ring_vbase) {
571 dev_err(adapter->dev, "Unable to allocate buffer. "
572 "Terminating download\n");
573 return -1;
575 card->evtbd_ring_pbase = virt_to_phys(card->evtbd_ring_vbase);
577 dev_dbg(adapter->dev, "info: CMDRSP/EVT bd_ring - base: %p, "
578 "pbase: %#x:%x, len: %#x\n", card->evtbd_ring_vbase,
579 (u32)card->evtbd_ring_pbase,
580 (u32)((u64)card->evtbd_ring_pbase >> 32),
581 card->evtbd_ring_size);
583 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
584 card->evtbd_ring[i] = (struct mwifiex_pcie_buf_desc *)
585 (card->evtbd_ring_vbase +
586 (sizeof(struct mwifiex_pcie_buf_desc) * i));
588 /* Allocate skb here so that firmware can DMA data from it */
589 skb = dev_alloc_skb(MAX_EVENT_SIZE);
590 if (!skb) {
591 dev_err(adapter->dev, "Unable to allocate skb for EVENT buf.\n");
592 kfree(card->evtbd_ring_vbase);
593 return -ENOMEM;
595 buf_pa = mwifiex_update_sk_buff_pa(skb);
596 skb_put(skb, MAX_EVENT_SIZE);
598 dev_dbg(adapter->dev, "info: Evt ring: add new skb. base: %p, "
599 "buf_base: %p, buf_pbase: %#x:%x, "
600 "buf_len: %#x\n", skb, skb->data,
601 (u32)*buf_pa, (u32)((u64)*buf_pa >> 32),
602 skb->len);
604 card->evt_buf_list[i] = skb;
605 card->evtbd_ring[i]->paddr = *buf_pa;
606 card->evtbd_ring[i]->len = (u16)skb->len;
607 card->evtbd_ring[i]->flags = 0;
610 return 0;
614 * This function deletes Buffer descriptor ring for Events
616 static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
618 struct pcie_service_card *card = adapter->card;
619 int i;
621 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
622 if (card->evt_buf_list[i])
623 dev_kfree_skb_any(card->evt_buf_list[i]);
624 card->evt_buf_list[i] = NULL;
625 card->evtbd_ring[i]->paddr = 0;
626 card->evtbd_ring[i]->len = 0;
627 card->evtbd_ring[i]->flags = 0;
628 card->evtbd_ring[i] = NULL;
631 kfree(card->evtbd_ring_vbase);
632 card->evtbd_wrptr = 0;
633 card->evtbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND;
634 card->evtbd_ring_size = 0;
635 card->evtbd_ring_vbase = NULL;
637 return 0;
641 * This function allocates a buffer for CMDRSP
643 static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
645 struct pcie_service_card *card = adapter->card;
646 struct sk_buff *skb;
648 /* Allocate memory for receiving command response data */
649 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
650 if (!skb) {
651 dev_err(adapter->dev, "Unable to allocate skb for command "
652 "response data.\n");
653 return -ENOMEM;
655 mwifiex_update_sk_buff_pa(skb);
656 skb_put(skb, MWIFIEX_UPLD_SIZE);
657 card->cmdrsp_buf = skb;
659 skb = NULL;
660 /* Allocate memory for sending command to firmware */
661 skb = dev_alloc_skb(MWIFIEX_SIZE_OF_CMD_BUFFER);
662 if (!skb) {
663 dev_err(adapter->dev, "Unable to allocate skb for command "
664 "data.\n");
665 return -ENOMEM;
667 mwifiex_update_sk_buff_pa(skb);
668 skb_put(skb, MWIFIEX_SIZE_OF_CMD_BUFFER);
669 card->cmd_buf = skb;
671 return 0;
675 * This function deletes a buffer for CMDRSP
677 static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
679 struct pcie_service_card *card;
681 if (!adapter)
682 return 0;
684 card = adapter->card;
686 if (card && card->cmdrsp_buf)
687 dev_kfree_skb_any(card->cmdrsp_buf);
689 if (card && card->cmd_buf)
690 dev_kfree_skb_any(card->cmd_buf);
692 return 0;
696 * This function allocates a buffer for sleep cookie
698 static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
700 struct sk_buff *skb;
701 struct pcie_service_card *card = adapter->card;
703 /* Allocate memory for sleep cookie */
704 skb = dev_alloc_skb(sizeof(u32));
705 if (!skb) {
706 dev_err(adapter->dev, "Unable to allocate skb for sleep "
707 "cookie!\n");
708 return -ENOMEM;
710 mwifiex_update_sk_buff_pa(skb);
711 skb_put(skb, sizeof(u32));
713 /* Init val of Sleep Cookie */
714 *(u32 *)skb->data = FW_AWAKE_COOKIE;
716 dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n",
717 *((u32 *)skb->data));
719 /* Save the sleep cookie */
720 card->sleep_cookie = skb;
722 return 0;
726 * This function deletes buffer for sleep cookie
728 static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
730 struct pcie_service_card *card;
732 if (!adapter)
733 return 0;
735 card = adapter->card;
737 if (card && card->sleep_cookie) {
738 dev_kfree_skb_any(card->sleep_cookie);
739 card->sleep_cookie = NULL;
742 return 0;
746 * This function sends data buffer to device
748 static int
749 mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb)
751 struct pcie_service_card *card = adapter->card;
752 u32 wrindx, rdptr;
753 phys_addr_t *buf_pa;
754 __le16 *tmp;
756 if (!mwifiex_pcie_ok_to_access_hw(adapter))
757 mwifiex_pm_wakeup_card(adapter);
759 /* Read the TX ring read pointer set by firmware */
760 if (mwifiex_read_reg(adapter, REG_TXBD_RDPTR, &rdptr)) {
761 dev_err(adapter->dev, "SEND DATA: failed to read "
762 "REG_TXBD_RDPTR\n");
763 return -1;
766 wrindx = card->txbd_wrptr & MWIFIEX_TXBD_MASK;
768 dev_dbg(adapter->dev, "info: SEND DATA: <Rd: %#x, Wr: %#x>\n", rdptr,
769 card->txbd_wrptr);
770 if (((card->txbd_wrptr & MWIFIEX_TXBD_MASK) !=
771 (rdptr & MWIFIEX_TXBD_MASK)) ||
772 ((card->txbd_wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) !=
773 (rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) {
774 struct sk_buff *skb_data;
775 u8 *payload;
777 adapter->data_sent = true;
778 skb_data = card->tx_buf_list[wrindx];
779 memcpy(skb_data->data, skb->data, skb->len);
780 payload = skb_data->data;
781 tmp = (__le16 *)&payload[0];
782 *tmp = cpu_to_le16((u16)skb->len);
783 tmp = (__le16 *)&payload[2];
784 *tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
785 skb_put(skb_data, MWIFIEX_RX_DATA_BUF_SIZE - skb_data->len);
786 skb_trim(skb_data, skb->len);
787 buf_pa = MWIFIEX_SKB_PACB(skb_data);
788 card->txbd_ring[wrindx]->paddr = *buf_pa;
789 card->txbd_ring[wrindx]->len = (u16)skb_data->len;
790 card->txbd_ring[wrindx]->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
791 MWIFIEX_BD_FLAG_LAST_DESC;
793 if ((++card->txbd_wrptr & MWIFIEX_TXBD_MASK) ==
794 MWIFIEX_MAX_TXRX_BD)
795 card->txbd_wrptr = ((card->txbd_wrptr &
796 MWIFIEX_BD_FLAG_ROLLOVER_IND) ^
797 MWIFIEX_BD_FLAG_ROLLOVER_IND);
799 /* Write the TX ring write pointer in to REG_TXBD_WRPTR */
800 if (mwifiex_write_reg(adapter, REG_TXBD_WRPTR,
801 card->txbd_wrptr)) {
802 dev_err(adapter->dev, "SEND DATA: failed to write "
803 "REG_TXBD_WRPTR\n");
804 return 0;
807 /* Send the TX ready interrupt */
808 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
809 CPU_INTR_DNLD_RDY)) {
810 dev_err(adapter->dev, "SEND DATA: failed to assert "
811 "door-bell interrupt.\n");
812 return -1;
814 dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: "
815 "%#x> and sent packet to firmware "
816 "successfully\n", rdptr,
817 card->txbd_wrptr);
818 } else {
819 dev_dbg(adapter->dev, "info: TX Ring full, can't send anymore "
820 "packets to firmware\n");
821 adapter->data_sent = true;
822 /* Send the TX ready interrupt */
823 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
824 CPU_INTR_DNLD_RDY))
825 dev_err(adapter->dev, "SEND DATA: failed to assert "
826 "door-bell interrupt\n");
827 return -EBUSY;
830 return 0;
834 * This function handles received buffer ring and
835 * dispatches packets to upper
837 static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
839 struct pcie_service_card *card = adapter->card;
840 u32 wrptr, rd_index;
841 int ret = 0;
842 struct sk_buff *skb_tmp = NULL;
844 /* Read the RX ring Write pointer set by firmware */
845 if (mwifiex_read_reg(adapter, REG_RXBD_WRPTR, &wrptr)) {
846 dev_err(adapter->dev, "RECV DATA: failed to read "
847 "REG_TXBD_RDPTR\n");
848 ret = -1;
849 goto done;
852 while (((wrptr & MWIFIEX_RXBD_MASK) !=
853 (card->rxbd_rdptr & MWIFIEX_RXBD_MASK)) ||
854 ((wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) ==
855 (card->rxbd_rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) {
856 struct sk_buff *skb_data;
857 u16 rx_len;
859 rd_index = card->rxbd_rdptr & MWIFIEX_RXBD_MASK;
860 skb_data = card->rx_buf_list[rd_index];
862 /* Get data length from interface header -
863 first byte is len, second byte is type */
864 rx_len = *((u16 *)skb_data->data);
865 dev_dbg(adapter->dev, "info: RECV DATA: Rd=%#x, Wr=%#x, "
866 "Len=%d\n", card->rxbd_rdptr, wrptr, rx_len);
867 skb_tmp = dev_alloc_skb(rx_len);
868 if (!skb_tmp) {
869 dev_dbg(adapter->dev, "info: Failed to alloc skb "
870 "for RX\n");
871 ret = -EBUSY;
872 goto done;
875 skb_put(skb_tmp, rx_len);
877 memcpy(skb_tmp->data, skb_data->data + INTF_HEADER_LEN, rx_len);
878 if ((++card->rxbd_rdptr & MWIFIEX_RXBD_MASK) ==
879 MWIFIEX_MAX_TXRX_BD) {
880 card->rxbd_rdptr = ((card->rxbd_rdptr &
881 MWIFIEX_BD_FLAG_ROLLOVER_IND) ^
882 MWIFIEX_BD_FLAG_ROLLOVER_IND);
884 dev_dbg(adapter->dev, "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
885 card->rxbd_rdptr, wrptr);
887 /* Write the RX ring read pointer in to REG_RXBD_RDPTR */
888 if (mwifiex_write_reg(adapter, REG_RXBD_RDPTR,
889 card->rxbd_rdptr)) {
890 dev_err(adapter->dev, "RECV DATA: failed to "
891 "write REG_RXBD_RDPTR\n");
892 ret = -1;
893 goto done;
896 /* Read the RX ring Write pointer set by firmware */
897 if (mwifiex_read_reg(adapter, REG_RXBD_WRPTR, &wrptr)) {
898 dev_err(adapter->dev, "RECV DATA: failed to read "
899 "REG_TXBD_RDPTR\n");
900 ret = -1;
901 goto done;
903 dev_dbg(adapter->dev, "info: RECV DATA: Received packet from "
904 "firmware successfully\n");
905 mwifiex_handle_rx_packet(adapter, skb_tmp);
908 done:
909 if (ret && skb_tmp)
910 dev_kfree_skb_any(skb_tmp);
911 return ret;
915 * This function downloads the boot command to device
917 static int
918 mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
920 phys_addr_t *buf_pa = MWIFIEX_SKB_PACB(skb);
922 if (!(skb->data && skb->len && *buf_pa)) {
923 dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x:%x, "
924 "%x>\n", __func__, skb->data, skb->len,
925 (u32)*buf_pa, (u32)((u64)*buf_pa >> 32));
926 return -1;
929 /* Write the lower 32bits of the physical address to scratch
930 * register 0 */
931 if (mwifiex_write_reg(adapter, PCIE_SCRATCH_0_REG, (u32)*buf_pa)) {
932 dev_err(adapter->dev, "%s: failed to write download command "
933 "to boot code.\n", __func__);
934 return -1;
937 /* Write the upper 32bits of the physical address to scratch
938 * register 1 */
939 if (mwifiex_write_reg(adapter, PCIE_SCRATCH_1_REG,
940 (u32)((u64)*buf_pa >> 32))) {
941 dev_err(adapter->dev, "%s: failed to write download command "
942 "to boot code.\n", __func__);
943 return -1;
946 /* Write the command length to scratch register 2 */
947 if (mwifiex_write_reg(adapter, PCIE_SCRATCH_2_REG, skb->len)) {
948 dev_err(adapter->dev, "%s: failed to write command length to "
949 "scratch register 2\n", __func__);
950 return -1;
953 /* Ring the door bell */
954 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
955 CPU_INTR_DOOR_BELL)) {
956 dev_err(adapter->dev, "%s: failed to assert door-bell "
957 "interrupt.\n", __func__);
958 return -1;
961 return 0;
965 * This function downloads commands to the device
967 static int
968 mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
970 struct pcie_service_card *card = adapter->card;
971 int ret = 0;
972 phys_addr_t *cmd_buf_pa;
973 phys_addr_t *cmdrsp_buf_pa;
975 if (!(skb->data && skb->len)) {
976 dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x>\n",
977 __func__, skb->data, skb->len);
978 return -1;
981 /* Make sure a command response buffer is available */
982 if (!card->cmdrsp_buf) {
983 dev_err(adapter->dev, "No response buffer available, send "
984 "command failed\n");
985 return -EBUSY;
988 /* Make sure a command buffer is available */
989 if (!card->cmd_buf) {
990 dev_err(adapter->dev, "Command buffer not available\n");
991 return -EBUSY;
994 adapter->cmd_sent = true;
995 /* Copy the given skb in to DMA accessable shared buffer */
996 skb_put(card->cmd_buf, MWIFIEX_SIZE_OF_CMD_BUFFER - card->cmd_buf->len);
997 skb_trim(card->cmd_buf, skb->len);
998 memcpy(card->cmd_buf->data, skb->data, skb->len);
1000 /* To send a command, the driver will:
1001 1. Write the 64bit physical address of the data buffer to
1002 SCRATCH1 + SCRATCH0
1003 2. Ring the door bell (i.e. set the door bell interrupt)
1005 In response to door bell interrupt, the firmware will perform
1006 the DMA of the command packet (first header to obtain the total
1007 length and then rest of the command).
1010 if (card->cmdrsp_buf) {
1011 cmdrsp_buf_pa = MWIFIEX_SKB_PACB(card->cmdrsp_buf);
1012 /* Write the lower 32bits of the cmdrsp buffer physical
1013 address */
1014 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_LO,
1015 (u32)*cmdrsp_buf_pa)) {
1016 dev_err(adapter->dev, "Failed to write download command to boot code.\n");
1017 ret = -1;
1018 goto done;
1020 /* Write the upper 32bits of the cmdrsp buffer physical
1021 address */
1022 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_HI,
1023 (u32)((u64)*cmdrsp_buf_pa >> 32))) {
1024 dev_err(adapter->dev, "Failed to write download command"
1025 " to boot code.\n");
1026 ret = -1;
1027 goto done;
1031 cmd_buf_pa = MWIFIEX_SKB_PACB(card->cmd_buf);
1032 /* Write the lower 32bits of the physical address to REG_CMD_ADDR_LO */
1033 if (mwifiex_write_reg(adapter, REG_CMD_ADDR_LO,
1034 (u32)*cmd_buf_pa)) {
1035 dev_err(adapter->dev, "Failed to write download command "
1036 "to boot code.\n");
1037 ret = -1;
1038 goto done;
1040 /* Write the upper 32bits of the physical address to REG_CMD_ADDR_HI */
1041 if (mwifiex_write_reg(adapter, REG_CMD_ADDR_HI,
1042 (u32)((u64)*cmd_buf_pa >> 32))) {
1043 dev_err(adapter->dev, "Failed to write download command "
1044 "to boot code.\n");
1045 ret = -1;
1046 goto done;
1049 /* Write the command length to REG_CMD_SIZE */
1050 if (mwifiex_write_reg(adapter, REG_CMD_SIZE,
1051 card->cmd_buf->len)) {
1052 dev_err(adapter->dev, "Failed to write command length to "
1053 "REG_CMD_SIZE\n");
1054 ret = -1;
1055 goto done;
1058 /* Ring the door bell */
1059 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1060 CPU_INTR_DOOR_BELL)) {
1061 dev_err(adapter->dev, "Failed to assert door-bell "
1062 "interrupt.\n");
1063 ret = -1;
1064 goto done;
1067 done:
1068 if (ret)
1069 adapter->cmd_sent = false;
1071 return 0;
1075 * This function handles command complete interrupt
1077 static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1079 struct pcie_service_card *card = adapter->card;
1080 int count = 0;
1082 dev_dbg(adapter->dev, "info: Rx CMD Response\n");
1084 if (!adapter->curr_cmd) {
1085 skb_pull(card->cmdrsp_buf, INTF_HEADER_LEN);
1086 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
1087 mwifiex_process_sleep_confirm_resp(adapter,
1088 card->cmdrsp_buf->data,
1089 card->cmdrsp_buf->len);
1090 while (mwifiex_pcie_ok_to_access_hw(adapter) &&
1091 (count++ < 10))
1092 udelay(50);
1093 } else {
1094 dev_err(adapter->dev, "There is no command but "
1095 "got cmdrsp\n");
1097 memcpy(adapter->upld_buf, card->cmdrsp_buf->data,
1098 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
1099 card->cmdrsp_buf->len));
1100 skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
1101 } else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1102 skb_pull(card->cmdrsp_buf, INTF_HEADER_LEN);
1103 adapter->curr_cmd->resp_skb = card->cmdrsp_buf;
1104 adapter->cmd_resp_received = true;
1105 /* Take the pointer and set it to CMD node and will
1106 return in the response complete callback */
1107 card->cmdrsp_buf = NULL;
1109 /* Clear the cmd-rsp buffer address in scratch registers. This
1110 will prevent firmware from writing to the same response
1111 buffer again. */
1112 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_LO, 0)) {
1113 dev_err(adapter->dev, "cmd_done: failed to clear "
1114 "cmd_rsp address.\n");
1115 return -1;
1117 /* Write the upper 32bits of the cmdrsp buffer physical
1118 address */
1119 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_HI, 0)) {
1120 dev_err(adapter->dev, "cmd_done: failed to clear "
1121 "cmd_rsp address.\n");
1122 return -1;
1126 return 0;
1130 * Command Response processing complete handler
1132 static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1133 struct sk_buff *skb)
1135 struct pcie_service_card *card = adapter->card;
1137 if (skb) {
1138 card->cmdrsp_buf = skb;
1139 skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
1142 return 0;
1146 * This function handles firmware event ready interrupt
1148 static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1150 struct pcie_service_card *card = adapter->card;
1151 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1152 u32 wrptr, event;
1154 if (adapter->event_received) {
1155 dev_dbg(adapter->dev, "info: Event being processed, "\
1156 "do not process this interrupt just yet\n");
1157 return 0;
1160 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1161 dev_dbg(adapter->dev, "info: Invalid read pointer...\n");
1162 return -1;
1165 /* Read the event ring write pointer set by firmware */
1166 if (mwifiex_read_reg(adapter, REG_EVTBD_WRPTR, &wrptr)) {
1167 dev_err(adapter->dev, "EventReady: failed to read REG_EVTBD_WRPTR\n");
1168 return -1;
1171 dev_dbg(adapter->dev, "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
1172 card->evtbd_rdptr, wrptr);
1173 if (((wrptr & MWIFIEX_EVTBD_MASK) !=
1174 (card->evtbd_rdptr & MWIFIEX_EVTBD_MASK)) ||
1175 ((wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) ==
1176 (card->evtbd_rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) {
1177 struct sk_buff *skb_cmd;
1178 __le16 data_len = 0;
1179 u16 evt_len;
1181 dev_dbg(adapter->dev, "info: Read Index: %d\n", rdptr);
1182 skb_cmd = card->evt_buf_list[rdptr];
1183 /* Take the pointer and set it to event pointer in adapter
1184 and will return back after event handling callback */
1185 card->evt_buf_list[rdptr] = NULL;
1186 card->evtbd_ring[rdptr]->paddr = 0;
1187 card->evtbd_ring[rdptr]->len = 0;
1188 card->evtbd_ring[rdptr]->flags = 0;
1190 event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
1191 adapter->event_cause = event;
1192 /* The first 4bytes will be the event transfer header
1193 len is 2 bytes followed by type which is 2 bytes */
1194 memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1195 evt_len = le16_to_cpu(data_len);
1197 skb_pull(skb_cmd, INTF_HEADER_LEN);
1198 dev_dbg(adapter->dev, "info: Event length: %d\n", evt_len);
1200 if ((evt_len > 0) && (evt_len < MAX_EVENT_SIZE))
1201 memcpy(adapter->event_body, skb_cmd->data +
1202 MWIFIEX_EVENT_HEADER_LEN, evt_len -
1203 MWIFIEX_EVENT_HEADER_LEN);
1205 adapter->event_received = true;
1206 adapter->event_skb = skb_cmd;
1208 /* Do not update the event read pointer here, wait till the
1209 buffer is released. This is just to make things simpler,
1210 we need to find a better method of managing these buffers.
1214 return 0;
1218 * Event processing complete handler
1220 static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1221 struct sk_buff *skb)
1223 struct pcie_service_card *card = adapter->card;
1224 int ret = 0;
1225 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1226 u32 wrptr;
1227 phys_addr_t *buf_pa;
1229 if (!skb)
1230 return 0;
1232 if (rdptr >= MWIFIEX_MAX_EVT_BD)
1233 dev_err(adapter->dev, "event_complete: Invalid rdptr 0x%x\n",
1234 rdptr);
1236 /* Read the event ring write pointer set by firmware */
1237 if (mwifiex_read_reg(adapter, REG_EVTBD_WRPTR, &wrptr)) {
1238 dev_err(adapter->dev, "event_complete: failed to read REG_EVTBD_WRPTR\n");
1239 ret = -1;
1240 goto done;
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 ret = -1;
1270 goto done;
1273 done:
1274 /* Free the buffer for failure case */
1275 if (ret && skb)
1276 dev_kfree_skb_any(skb);
1278 dev_dbg(adapter->dev, "info: Check Events Again\n");
1279 ret = mwifiex_pcie_process_event_ready(adapter);
1281 return ret;
1285 * This function downloads the firmware to the card.
1287 * Firmware is downloaded to the card in blocks. Every block download
1288 * is tested for CRC errors, and retried a number of times before
1289 * returning failure.
1291 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1292 struct mwifiex_fw_image *fw)
1294 int ret;
1295 u8 *firmware = fw->fw_buf;
1296 u32 firmware_len = fw->fw_len;
1297 u32 offset = 0;
1298 struct sk_buff *skb;
1299 u32 txlen, tx_blocks = 0, tries, len;
1300 u32 block_retry_cnt = 0;
1302 if (!adapter) {
1303 pr_err("adapter structure is not valid\n");
1304 return -1;
1307 if (!firmware || !firmware_len) {
1308 dev_err(adapter->dev, "No firmware image found! "
1309 "Terminating download\n");
1310 return -1;
1313 dev_dbg(adapter->dev, "info: Downloading FW image (%d bytes)\n",
1314 firmware_len);
1316 if (mwifiex_pcie_disable_host_int(adapter)) {
1317 dev_err(adapter->dev, "%s: Disabling interrupts"
1318 " failed.\n", __func__);
1319 return -1;
1322 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
1323 if (!skb) {
1324 ret = -ENOMEM;
1325 goto done;
1327 mwifiex_update_sk_buff_pa(skb);
1329 /* Perform firmware data transfer */
1330 do {
1331 u32 ireg_intr = 0;
1333 /* More data? */
1334 if (offset >= firmware_len)
1335 break;
1337 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1338 ret = mwifiex_read_reg(adapter, PCIE_SCRATCH_2_REG,
1339 &len);
1340 if (ret) {
1341 dev_warn(adapter->dev, "Failed reading length from boot code\n");
1342 goto done;
1344 if (len)
1345 break;
1346 udelay(10);
1349 if (!len) {
1350 break;
1351 } else if (len > MWIFIEX_UPLD_SIZE) {
1352 pr_err("FW download failure @ %d, invalid length %d\n",
1353 offset, len);
1354 ret = -1;
1355 goto done;
1358 txlen = len;
1360 if (len & BIT(0)) {
1361 block_retry_cnt++;
1362 if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
1363 pr_err("FW download failure @ %d, over max "
1364 "retry count\n", offset);
1365 ret = -1;
1366 goto done;
1368 dev_err(adapter->dev, "FW CRC error indicated by the "
1369 "helper: len = 0x%04X, txlen = "
1370 "%d\n", len, txlen);
1371 len &= ~BIT(0);
1372 /* Setting this to 0 to resend from same offset */
1373 txlen = 0;
1374 } else {
1375 block_retry_cnt = 0;
1376 /* Set blocksize to transfer - checking for
1377 last block */
1378 if (firmware_len - offset < txlen)
1379 txlen = firmware_len - offset;
1381 dev_dbg(adapter->dev, ".");
1383 tx_blocks =
1384 (txlen + MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD - 1) /
1385 MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD;
1387 /* Copy payload to buffer */
1388 memmove(skb->data, &firmware[offset], txlen);
1391 skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1392 skb_trim(skb, tx_blocks * MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD);
1394 /* Send the boot command to device */
1395 if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
1396 dev_err(adapter->dev, "Failed to send firmware download command\n");
1397 ret = -1;
1398 goto done;
1400 /* Wait for the command done interrupt */
1401 do {
1402 if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
1403 &ireg_intr)) {
1404 dev_err(adapter->dev, "%s: Failed to read "
1405 "interrupt status during "
1406 "fw dnld.\n", __func__);
1407 ret = -1;
1408 goto done;
1410 } while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
1411 CPU_INTR_DOOR_BELL);
1412 offset += txlen;
1413 } while (true);
1415 dev_dbg(adapter->dev, "info:\nFW download over, size %d bytes\n",
1416 offset);
1418 ret = 0;
1420 done:
1421 dev_kfree_skb_any(skb);
1422 return ret;
1426 * This function checks the firmware status in card.
1428 * The winner interface is also determined by this function.
1430 static int
1431 mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
1433 int ret = 0;
1434 u32 firmware_stat, winner_status;
1435 u32 tries;
1437 /* Mask spurios interrupts */
1438 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
1439 HOST_INTR_MASK)) {
1440 dev_warn(adapter->dev, "Write register failed\n");
1441 return -1;
1444 dev_dbg(adapter->dev, "Setting driver ready signature\n");
1445 if (mwifiex_write_reg(adapter, REG_DRV_READY, FIRMWARE_READY_PCIE)) {
1446 dev_err(adapter->dev, "Failed to write driver ready signature\n");
1447 return -1;
1450 /* Wait for firmware initialization event */
1451 for (tries = 0; tries < poll_num; tries++) {
1452 if (mwifiex_read_reg(adapter, PCIE_SCRATCH_3_REG,
1453 &firmware_stat))
1454 ret = -1;
1455 else
1456 ret = 0;
1457 if (ret)
1458 continue;
1459 if (firmware_stat == FIRMWARE_READY_PCIE) {
1460 ret = 0;
1461 break;
1462 } else {
1463 mdelay(100);
1464 ret = -1;
1468 if (ret) {
1469 if (mwifiex_read_reg(adapter, PCIE_SCRATCH_3_REG,
1470 &winner_status))
1471 ret = -1;
1472 else if (!winner_status) {
1473 dev_err(adapter->dev, "PCI-E is the winner\n");
1474 adapter->winner = 1;
1475 ret = -1;
1476 } else {
1477 dev_err(adapter->dev, "PCI-E is not the winner <%#x, %d>, exit download\n",
1478 ret, adapter->winner);
1479 ret = 0;
1483 return ret;
1487 * This function reads the interrupt status from card.
1489 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
1491 u32 pcie_ireg;
1492 unsigned long flags;
1494 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1495 return;
1497 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) {
1498 dev_warn(adapter->dev, "Read register failed\n");
1499 return;
1502 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
1504 mwifiex_pcie_disable_host_int(adapter);
1506 /* Clear the pending interrupts */
1507 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
1508 ~pcie_ireg)) {
1509 dev_warn(adapter->dev, "Write register failed\n");
1510 return;
1512 spin_lock_irqsave(&adapter->int_lock, flags);
1513 adapter->int_status |= pcie_ireg;
1514 spin_unlock_irqrestore(&adapter->int_lock, flags);
1516 if (pcie_ireg & HOST_INTR_CMD_DONE) {
1517 if ((adapter->ps_state == PS_STATE_SLEEP_CFM) ||
1518 (adapter->ps_state == PS_STATE_SLEEP)) {
1519 mwifiex_pcie_enable_host_int(adapter);
1520 if (mwifiex_write_reg(adapter,
1521 PCIE_CPU_INT_EVENT,
1522 CPU_INTR_SLEEP_CFM_DONE)) {
1523 dev_warn(adapter->dev, "Write register"
1524 " failed\n");
1525 return;
1529 } else if (!adapter->pps_uapsd_mode &&
1530 adapter->ps_state == PS_STATE_SLEEP) {
1531 /* Potentially for PCIe we could get other
1532 * interrupts like shared. Don't change power
1533 * state until cookie is set */
1534 if (mwifiex_pcie_ok_to_access_hw(adapter))
1535 adapter->ps_state = PS_STATE_AWAKE;
1541 * Interrupt handler for PCIe root port
1543 * This function reads the interrupt status from firmware and assigns
1544 * the main process in workqueue which will handle the interrupt.
1546 static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
1548 struct pci_dev *pdev = (struct pci_dev *)context;
1549 struct pcie_service_card *card;
1550 struct mwifiex_adapter *adapter;
1552 if (!pdev) {
1553 pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev);
1554 goto exit;
1557 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
1558 if (!card || !card->adapter) {
1559 pr_debug("info: %s: card=%p adapter=%p\n", __func__, card,
1560 card ? card->adapter : NULL);
1561 goto exit;
1563 adapter = card->adapter;
1565 if (adapter->surprise_removed)
1566 goto exit;
1568 mwifiex_interrupt_status(adapter);
1569 queue_work(adapter->workqueue, &adapter->main_work);
1571 exit:
1572 return IRQ_HANDLED;
1576 * This function checks the current interrupt status.
1578 * The following interrupts are checked and handled by this function -
1579 * - Data sent
1580 * - Command sent
1581 * - Command received
1582 * - Packets received
1583 * - Events received
1585 * In case of Rx packets received, the packets are uploaded from card to
1586 * host and processed accordingly.
1588 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1590 int ret;
1591 u32 pcie_ireg = 0;
1592 unsigned long flags;
1594 spin_lock_irqsave(&adapter->int_lock, flags);
1595 /* Clear out unused interrupts */
1596 adapter->int_status &= HOST_INTR_MASK;
1597 spin_unlock_irqrestore(&adapter->int_lock, flags);
1599 while (adapter->int_status & HOST_INTR_MASK) {
1600 if (adapter->int_status & HOST_INTR_DNLD_DONE) {
1601 adapter->int_status &= ~HOST_INTR_DNLD_DONE;
1602 if (adapter->data_sent) {
1603 dev_dbg(adapter->dev, "info: DATA sent Interrupt\n");
1604 adapter->data_sent = false;
1607 if (adapter->int_status & HOST_INTR_UPLD_RDY) {
1608 adapter->int_status &= ~HOST_INTR_UPLD_RDY;
1609 dev_dbg(adapter->dev, "info: Rx DATA\n");
1610 ret = mwifiex_pcie_process_recv_data(adapter);
1611 if (ret)
1612 return ret;
1614 if (adapter->int_status & HOST_INTR_EVENT_RDY) {
1615 adapter->int_status &= ~HOST_INTR_EVENT_RDY;
1616 dev_dbg(adapter->dev, "info: Rx EVENT\n");
1617 ret = mwifiex_pcie_process_event_ready(adapter);
1618 if (ret)
1619 return ret;
1622 if (adapter->int_status & HOST_INTR_CMD_DONE) {
1623 adapter->int_status &= ~HOST_INTR_CMD_DONE;
1624 if (adapter->cmd_sent) {
1625 dev_dbg(adapter->dev, "info: CMD sent Interrupt\n");
1626 adapter->cmd_sent = false;
1628 /* Handle command response */
1629 ret = mwifiex_pcie_process_cmd_complete(adapter);
1630 if (ret)
1631 return ret;
1634 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1635 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
1636 &pcie_ireg)) {
1637 dev_warn(adapter->dev, "Read register failed\n");
1638 return -1;
1641 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
1642 if (mwifiex_write_reg(adapter,
1643 PCIE_HOST_INT_STATUS, ~pcie_ireg)) {
1644 dev_warn(adapter->dev, "Write register"
1645 " failed\n");
1646 return -1;
1648 adapter->int_status |= pcie_ireg;
1649 adapter->int_status &= HOST_INTR_MASK;
1654 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
1655 adapter->cmd_sent, adapter->data_sent);
1656 mwifiex_pcie_enable_host_int(adapter);
1658 return 0;
1662 * This function downloads data from driver to card.
1664 * Both commands and data packets are transferred to the card by this
1665 * function.
1667 * This function adds the PCIE specific header to the front of the buffer
1668 * before transferring. The header contains the length of the packet and
1669 * the type. The firmware handles the packets based upon this set type.
1671 static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
1672 struct sk_buff *skb,
1673 struct mwifiex_tx_param *tx_param)
1675 if (!adapter || !skb) {
1676 dev_err(adapter->dev, "Invalid parameter in %s <%p, %p>\n",
1677 __func__, adapter, skb);
1678 return -1;
1681 if (type == MWIFIEX_TYPE_DATA)
1682 return mwifiex_pcie_send_data(adapter, skb);
1683 else if (type == MWIFIEX_TYPE_CMD)
1684 return mwifiex_pcie_send_cmd(adapter, skb);
1686 return 0;
1690 * This function initializes the PCI-E host memory space, WCB rings, etc.
1692 * The following initializations steps are followed -
1693 * - Allocate TXBD ring buffers
1694 * - Allocate RXBD ring buffers
1695 * - Allocate event BD ring buffers
1696 * - Allocate command response ring buffer
1697 * - Allocate sleep cookie buffer
1699 static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
1701 struct pcie_service_card *card = adapter->card;
1702 int ret;
1703 struct pci_dev *pdev = card->dev;
1705 pci_set_drvdata(pdev, card);
1707 ret = pci_enable_device(pdev);
1708 if (ret)
1709 goto err_enable_dev;
1711 pci_set_master(pdev);
1713 dev_dbg(adapter->dev, "try set_consistent_dma_mask(32)\n");
1714 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1715 if (ret) {
1716 dev_err(adapter->dev, "set_dma_mask(32) failed\n");
1717 goto err_set_dma_mask;
1720 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1721 if (ret) {
1722 dev_err(adapter->dev, "set_consistent_dma_mask(64) failed\n");
1723 goto err_set_dma_mask;
1726 ret = pci_request_region(pdev, 0, DRV_NAME);
1727 if (ret) {
1728 dev_err(adapter->dev, "req_reg(0) error\n");
1729 goto err_req_region0;
1731 card->pci_mmap = pci_iomap(pdev, 0, 0);
1732 if (!card->pci_mmap) {
1733 dev_err(adapter->dev, "iomap(0) error\n");
1734 goto err_iomap0;
1736 ret = pci_request_region(pdev, 2, DRV_NAME);
1737 if (ret) {
1738 dev_err(adapter->dev, "req_reg(2) error\n");
1739 goto err_req_region2;
1741 card->pci_mmap1 = pci_iomap(pdev, 2, 0);
1742 if (!card->pci_mmap1) {
1743 dev_err(adapter->dev, "iomap(2) error\n");
1744 goto err_iomap2;
1747 dev_dbg(adapter->dev, "PCI memory map Virt0: %p PCI memory map Virt2: "
1748 "%p\n", card->pci_mmap, card->pci_mmap1);
1750 card->cmdrsp_buf = NULL;
1751 ret = mwifiex_pcie_create_txbd_ring(adapter);
1752 if (ret)
1753 goto err_cre_txbd;
1754 ret = mwifiex_pcie_create_rxbd_ring(adapter);
1755 if (ret)
1756 goto err_cre_rxbd;
1757 ret = mwifiex_pcie_create_evtbd_ring(adapter);
1758 if (ret)
1759 goto err_cre_evtbd;
1760 ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
1761 if (ret)
1762 goto err_alloc_cmdbuf;
1763 ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
1764 if (ret)
1765 goto err_alloc_cookie;
1767 return ret;
1769 err_alloc_cookie:
1770 mwifiex_pcie_delete_cmdrsp_buf(adapter);
1771 err_alloc_cmdbuf:
1772 mwifiex_pcie_delete_evtbd_ring(adapter);
1773 err_cre_evtbd:
1774 mwifiex_pcie_delete_rxbd_ring(adapter);
1775 err_cre_rxbd:
1776 mwifiex_pcie_delete_txbd_ring(adapter);
1777 err_cre_txbd:
1778 pci_iounmap(pdev, card->pci_mmap1);
1779 err_iomap2:
1780 pci_release_region(pdev, 2);
1781 err_req_region2:
1782 pci_iounmap(pdev, card->pci_mmap);
1783 err_iomap0:
1784 pci_release_region(pdev, 0);
1785 err_req_region0:
1786 err_set_dma_mask:
1787 pci_disable_device(pdev);
1788 err_enable_dev:
1789 pci_set_drvdata(pdev, NULL);
1790 return ret;
1794 * This function cleans up the allocated card buffers.
1796 * The following are freed by this function -
1797 * - TXBD ring buffers
1798 * - RXBD ring buffers
1799 * - Event BD ring buffers
1800 * - Command response ring buffer
1801 * - Sleep cookie buffer
1803 static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
1805 struct pcie_service_card *card = adapter->card;
1806 struct pci_dev *pdev = card->dev;
1808 mwifiex_pcie_delete_sleep_cookie_buf(adapter);
1809 mwifiex_pcie_delete_cmdrsp_buf(adapter);
1810 mwifiex_pcie_delete_evtbd_ring(adapter);
1811 mwifiex_pcie_delete_rxbd_ring(adapter);
1812 mwifiex_pcie_delete_txbd_ring(adapter);
1813 card->cmdrsp_buf = NULL;
1815 dev_dbg(adapter->dev, "Clearing driver ready signature\n");
1816 if (user_rmmod) {
1817 if (mwifiex_write_reg(adapter, REG_DRV_READY, 0x00000000))
1818 dev_err(adapter->dev, "Failed to write driver not-ready signature\n");
1821 if (pdev) {
1822 pci_iounmap(pdev, card->pci_mmap);
1823 pci_iounmap(pdev, card->pci_mmap1);
1825 pci_release_regions(pdev);
1826 pci_disable_device(pdev);
1827 pci_set_drvdata(pdev, NULL);
1832 * This function registers the PCIE device.
1834 * PCIE IRQ is claimed, block size is set and driver data is initialized.
1836 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1838 int ret;
1839 struct pcie_service_card *card = adapter->card;
1840 struct pci_dev *pdev = card->dev;
1842 /* save adapter pointer in card */
1843 card->adapter = adapter;
1845 ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
1846 "MRVL_PCIE", pdev);
1847 if (ret) {
1848 pr_err("request_irq failed: ret=%d\n", ret);
1849 adapter->card = NULL;
1850 return -1;
1853 adapter->dev = &pdev->dev;
1854 strcpy(adapter->fw_name, PCIE8766_DEFAULT_FW_NAME);
1856 return 0;
1860 * This function unregisters the PCIE device.
1862 * The PCIE IRQ is released, the function is disabled and driver
1863 * data is set to null.
1865 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1867 struct pcie_service_card *card = adapter->card;
1869 if (card) {
1870 dev_dbg(adapter->dev, "%s(): calling free_irq()\n", __func__);
1871 free_irq(card->dev->irq, card->dev);
1875 static struct mwifiex_if_ops pcie_ops = {
1876 .init_if = mwifiex_pcie_init,
1877 .cleanup_if = mwifiex_pcie_cleanup,
1878 .check_fw_status = mwifiex_check_fw_status,
1879 .prog_fw = mwifiex_prog_fw_w_helper,
1880 .register_dev = mwifiex_register_dev,
1881 .unregister_dev = mwifiex_unregister_dev,
1882 .enable_int = mwifiex_pcie_enable_host_int,
1883 .process_int_status = mwifiex_process_int_status,
1884 .host_to_card = mwifiex_pcie_host_to_card,
1885 .wakeup = mwifiex_pm_wakeup_card,
1886 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1888 /* PCIE specific */
1889 .cmdrsp_complete = mwifiex_pcie_cmdrsp_complete,
1890 .event_complete = mwifiex_pcie_event_complete,
1891 .update_mp_end_port = NULL,
1892 .cleanup_mpa_buf = NULL,
1896 * This function initializes the PCIE driver module.
1898 * This initiates the semaphore and registers the device with
1899 * PCIE bus.
1901 static int mwifiex_pcie_init_module(void)
1903 int ret;
1905 pr_debug("Marvell 8766 PCIe Driver\n");
1907 sema_init(&add_remove_card_sem, 1);
1909 /* Clear the flag in case user removes the card. */
1910 user_rmmod = 0;
1912 ret = pci_register_driver(&mwifiex_pcie);
1913 if (ret)
1914 pr_err("Driver register failed!\n");
1915 else
1916 pr_debug("info: Driver registered successfully!\n");
1918 return ret;
1922 * This function cleans up the PCIE driver.
1924 * The following major steps are followed for cleanup -
1925 * - Resume the device if its suspended
1926 * - Disconnect the device if connected
1927 * - Shutdown the firmware
1928 * - Unregister the device from PCIE bus.
1930 static void mwifiex_pcie_cleanup_module(void)
1932 if (!down_interruptible(&add_remove_card_sem))
1933 up(&add_remove_card_sem);
1935 /* Set the flag as user is removing this module. */
1936 user_rmmod = 1;
1938 pci_unregister_driver(&mwifiex_pcie);
1941 module_init(mwifiex_pcie_init_module);
1942 module_exit(mwifiex_pcie_cleanup_module);
1944 MODULE_AUTHOR("Marvell International Ltd.");
1945 MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
1946 MODULE_VERSION(PCIE_VERSION);
1947 MODULE_LICENSE("GPL v2");
1948 MODULE_FIRMWARE("mrvl/pcie8766_uapsta.bin");