mwifiex: use pci_alloc/free_consistent APIs for PCIe
[deliverable/linux.git] / drivers / net / wireless / mwifiex / pcie.c
CommitLineData
d930faee
AK
1/*
2 * Marvell Wireless LAN device driver: PCIE specific handling
3 *
4 * Copyright (C) 2011, Marvell International Ltd.
5 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
18 */
19
20#include <linux/firmware.h>
21
22#include "decl.h"
23#include "ioctl.h"
24#include "util.h"
25#include "fw.h"
26#include "main.h"
27#include "wmm.h"
28#include "11n.h"
29#include "pcie.h"
30
31#define PCIE_VERSION "1.0"
32#define DRV_NAME "Marvell mwifiex PCIe"
33
34static u8 user_rmmod;
35
36static struct mwifiex_if_ops pcie_ops;
37
38static struct semaphore add_remove_card_sem;
39static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter);
40static int mwifiex_pcie_resume(struct pci_dev *pdev);
41
fc331460
AP
42static int
43mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb,
44 int size, int flags)
d930faee 45{
fc331460
AP
46 struct pcie_service_card *card = adapter->card;
47 dma_addr_t buf_pa;
d930faee 48
fc331460
AP
49 buf_pa = pci_map_single(card->dev, skb->data, size, flags);
50 if (pci_dma_mapping_error(card->dev, buf_pa)) {
51 dev_err(adapter->dev, "failed to map pci memory!\n");
52 return -1;
53 }
54 memcpy(skb->cb, &buf_pa, sizeof(dma_addr_t));
55 return 0;
d930faee
AK
56}
57
58/*
59 * This function reads sleep cookie and checks if FW is ready
60 */
61static bool mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter *adapter)
62{
63 u32 *cookie_addr;
64 struct pcie_service_card *card = adapter->card;
65
fc331460
AP
66 if (card->sleep_cookie_vbase) {
67 cookie_addr = (u32 *)card->sleep_cookie_vbase;
d930faee
AK
68 dev_dbg(adapter->dev, "info: ACCESS_HW: sleep cookie=0x%x\n",
69 *cookie_addr);
70 if (*cookie_addr == FW_AWAKE_COOKIE)
71 return true;
72 }
73
74 return false;
75}
76
77/*
78 * This function probes an mwifiex device and registers it. It allocates
79 * the card structure, enables PCIE function number and initiates the
80 * device registration and initialization procedure by adding a logical
81 * interface.
82 */
83static int mwifiex_pcie_probe(struct pci_dev *pdev,
84 const struct pci_device_id *ent)
85{
86 struct pcie_service_card *card;
87
88 pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
f57c1edc 89 pdev->vendor, pdev->device, pdev->revision);
d930faee
AK
90
91 card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
e404decb 92 if (!card)
d930faee 93 return -ENOMEM;
d930faee
AK
94
95 card->dev = pdev;
96
97 if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops,
98 MWIFIEX_PCIE)) {
99 pr_err("%s failed\n", __func__);
100 kfree(card);
101 return -1;
102 }
103
104 return 0;
105}
106
107/*
108 * This function removes the interface and frees up the card structure.
109 */
110static void mwifiex_pcie_remove(struct pci_dev *pdev)
111{
112 struct pcie_service_card *card;
113 struct mwifiex_adapter *adapter;
f57c1edc 114 struct mwifiex_private *priv;
d930faee
AK
115 int i;
116
117 card = pci_get_drvdata(pdev);
118 if (!card)
119 return;
120
121 adapter = card->adapter;
122 if (!adapter || !adapter->priv_num)
123 return;
124
59a4cc25
AK
125 /* In case driver is removed when asynchronous FW load is in progress */
126 wait_for_completion(&adapter->fw_load);
127
d930faee
AK
128 if (user_rmmod) {
129#ifdef CONFIG_PM
130 if (adapter->is_suspended)
131 mwifiex_pcie_resume(pdev);
132#endif
133
134 for (i = 0; i < adapter->priv_num; i++)
135 if ((GET_BSS_ROLE(adapter->priv[i]) ==
f57c1edc
YAP
136 MWIFIEX_BSS_ROLE_STA) &&
137 adapter->priv[i]->media_connected)
d930faee
AK
138 mwifiex_deauthenticate(adapter->priv[i], NULL);
139
f57c1edc 140 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
d930faee 141
f57c1edc
YAP
142 mwifiex_disable_auto_ds(priv);
143
144 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
d930faee
AK
145 }
146
147 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
148 kfree(card);
149}
150
151/*
152 * Kernel needs to suspend all functions separately. Therefore all
153 * registered functions must have drivers with suspend and resume
154 * methods. Failing that the kernel simply removes the whole card.
155 *
156 * If already not suspended, this function allocates and sends a host
157 * sleep activate request to the firmware and turns off the traffic.
158 */
159static int mwifiex_pcie_suspend(struct pci_dev *pdev, pm_message_t state)
160{
161 struct mwifiex_adapter *adapter;
162 struct pcie_service_card *card;
163 int hs_actived, i;
164
165 if (pdev) {
166 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
167 if (!card || card->adapter) {
168 pr_err("Card or adapter structure is not valid\n");
169 return 0;
170 }
171 } else {
172 pr_err("PCIE device is not specified\n");
173 return 0;
174 }
175
176 adapter = card->adapter;
177
178 hs_actived = mwifiex_enable_hs(adapter);
179
180 /* Indicate device suspended */
181 adapter->is_suspended = true;
182
183 for (i = 0; i < adapter->priv_num; i++)
184 netif_carrier_off(adapter->priv[i]->netdev);
185
186 return 0;
187}
188
189/*
190 * Kernel needs to suspend all functions separately. Therefore all
191 * registered functions must have drivers with suspend and resume
192 * methods. Failing that the kernel simply removes the whole card.
193 *
194 * If already not resumed, this function turns on the traffic and
195 * sends a host sleep cancel request to the firmware.
196 */
197static int mwifiex_pcie_resume(struct pci_dev *pdev)
198{
199 struct mwifiex_adapter *adapter;
200 struct pcie_service_card *card;
201 int i;
202
203 if (pdev) {
204 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
205 if (!card || !card->adapter) {
206 pr_err("Card or adapter structure is not valid\n");
207 return 0;
208 }
209 } else {
210 pr_err("PCIE device is not specified\n");
211 return 0;
212 }
213
214 adapter = card->adapter;
215
216 if (!adapter->is_suspended) {
217 dev_warn(adapter->dev, "Device already resumed\n");
218 return 0;
219 }
220
221 adapter->is_suspended = false;
222
223 for (i = 0; i < adapter->priv_num; i++)
224 if (adapter->priv[i]->media_connected)
225 netif_carrier_on(adapter->priv[i]->netdev);
226
227 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
f57c1edc 228 MWIFIEX_ASYNC_CMD);
d930faee
AK
229
230 return 0;
231}
232
233#define PCIE_VENDOR_ID_MARVELL (0x11ab)
234#define PCIE_DEVICE_ID_MARVELL_88W8766P (0x2b30)
235
236static DEFINE_PCI_DEVICE_TABLE(mwifiex_ids) = {
237 {
238 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
239 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
240 },
241 {},
242};
243
244MODULE_DEVICE_TABLE(pci, mwifiex_ids);
245
246/* PCI Device Driver */
247static struct pci_driver __refdata mwifiex_pcie = {
248 .name = "mwifiex_pcie",
249 .id_table = mwifiex_ids,
250 .probe = mwifiex_pcie_probe,
251 .remove = mwifiex_pcie_remove,
252#ifdef CONFIG_PM
253 /* Power Management Hooks */
254 .suspend = mwifiex_pcie_suspend,
255 .resume = mwifiex_pcie_resume,
256#endif
257};
258
259/*
260 * This function writes data into PCIE card register.
261 */
262static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
263{
264 struct pcie_service_card *card = adapter->card;
265
266 iowrite32(data, card->pci_mmap1 + reg);
267
268 return 0;
269}
270
271/*
272 * This function reads data from PCIE card register.
273 */
274static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
275{
276 struct pcie_service_card *card = adapter->card;
277
278 *data = ioread32(card->pci_mmap1 + reg);
279
280 return 0;
281}
282
283/*
284 * This function wakes up the card.
285 *
286 * A host power up command is written to the card configuration
287 * register to wake up the card.
288 */
289static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
290{
291 int i = 0;
292
293 while (mwifiex_pcie_ok_to_access_hw(adapter)) {
294 i++;
e7891ba2 295 usleep_range(10, 20);
d930faee
AK
296 /* 50ms max wait */
297 if (i == 50000)
298 break;
299 }
300
301 dev_dbg(adapter->dev, "event: Wakeup device...\n");
302
303 /* Enable interrupts or any chip access will wakeup device */
304 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK, HOST_INTR_MASK)) {
305 dev_warn(adapter->dev, "Enable host interrupt failed\n");
306 return -1;
307 }
308
309 dev_dbg(adapter->dev, "PCIE wakeup: Setting PS_STATE_AWAKE\n");
310 adapter->ps_state = PS_STATE_AWAKE;
311
312 return 0;
313}
314
315/*
316 * This function is called after the card has woken up.
317 *
318 * The card configuration register is reset.
319 */
320static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
321{
322 dev_dbg(adapter->dev, "cmd: Wakeup device completed\n");
323
324 return 0;
325}
326
327/*
328 * This function disables the host interrupt.
329 *
330 * The host interrupt mask is read, the disable bit is reset and
331 * written back to the card host interrupt mask register.
332 */
333static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
334{
335 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
336 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
337 0x00000000)) {
338 dev_warn(adapter->dev, "Disable host interrupt failed\n");
339 return -1;
340 }
341 }
342
343 return 0;
344}
345
346/*
347 * This function enables the host interrupt.
348 *
349 * The host interrupt enable mask is written to the card
350 * host interrupt mask register.
351 */
352static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
353{
354 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
355 /* Simply write the mask to the register */
356 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
357 HOST_INTR_MASK)) {
358 dev_warn(adapter->dev, "Enable host interrupt failed\n");
359 return -1;
360 }
361 }
362
363 return 0;
364}
365
366/*
367 * This function creates buffer descriptor ring for TX
368 */
369static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
370{
371 struct pcie_service_card *card = adapter->card;
d930faee 372 int i;
d930faee
AK
373
374 /*
375 * driver maintaines the write pointer and firmware maintaines the read
376 * pointer. The write pointer starts at 0 (zero) while the read pointer
377 * starts at zero with rollover bit set
378 */
379 card->txbd_wrptr = 0;
380 card->txbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND;
381
382 /* allocate shared memory for the BD ring and divide the same in to
383 several descriptors */
384 card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
f57c1edc 385 MWIFIEX_MAX_TXRX_BD;
d930faee 386 dev_dbg(adapter->dev, "info: txbd_ring: Allocating %d bytes\n",
f57c1edc 387 card->txbd_ring_size);
fc331460
AP
388 card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
389 card->txbd_ring_size,
390 &card->txbd_ring_pbase);
d930faee 391 if (!card->txbd_ring_vbase) {
fc331460
AP
392 dev_err(adapter->dev,
393 "allocate consistent memory (%d bytes) failed!\n",
394 card->txbd_ring_size);
8c53e42d 395 return -ENOMEM;
d930faee 396 }
f57c1edc
YAP
397 dev_dbg(adapter->dev,
398 "info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
fc331460 399 card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
f57c1edc 400 (u32)((u64)card->txbd_ring_pbase >> 32), card->txbd_ring_size);
d930faee
AK
401
402 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
403 card->txbd_ring[i] = (struct mwifiex_pcie_buf_desc *)
f57c1edc
YAP
404 (card->txbd_ring_vbase +
405 (sizeof(struct mwifiex_pcie_buf_desc)
406 * i));
d930faee 407
fc331460
AP
408 card->tx_buf_list[i] = NULL;
409 card->txbd_ring[i]->paddr = 0;
410 card->txbd_ring[i]->len = 0;
d930faee
AK
411 card->txbd_ring[i]->flags = 0;
412 }
413
414 return 0;
415}
416
417static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
418{
419 struct pcie_service_card *card = adapter->card;
fc331460 420 struct sk_buff *skb;
d930faee
AK
421 int i;
422
423 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
fc331460
AP
424 if (card->tx_buf_list[i]) {
425 skb = card->tx_buf_list[i];
426 pci_unmap_single(card->dev, card->txbd_ring[i]->paddr,
427 skb->len, PCI_DMA_TODEVICE);
428 dev_kfree_skb_any(skb);
429 }
d930faee
AK
430 card->tx_buf_list[i] = NULL;
431 card->txbd_ring[i]->paddr = 0;
432 card->txbd_ring[i]->len = 0;
433 card->txbd_ring[i]->flags = 0;
434 card->txbd_ring[i] = NULL;
435 }
436
fc331460
AP
437 if (card->txbd_ring_vbase)
438 pci_free_consistent(card->dev, card->txbd_ring_size,
439 card->txbd_ring_vbase,
440 card->txbd_ring_pbase);
d930faee
AK
441 card->txbd_ring_size = 0;
442 card->txbd_wrptr = 0;
443 card->txbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND;
444 card->txbd_ring_vbase = NULL;
fc331460 445 card->txbd_ring_pbase = 0;
d930faee
AK
446
447 return 0;
448}
449
450/*
451 * This function creates buffer descriptor ring for RX
452 */
453static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
454{
455 struct pcie_service_card *card = adapter->card;
456 struct sk_buff *skb;
457 int i;
fc331460 458 dma_addr_t buf_pa;
d930faee
AK
459
460 /*
461 * driver maintaines the read pointer and firmware maintaines the write
462 * pointer. The write pointer starts at 0 (zero) while the read pointer
463 * starts at zero with rollover bit set
464 */
465 card->rxbd_wrptr = 0;
466 card->rxbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND;
467
468 card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
f57c1edc 469 MWIFIEX_MAX_TXRX_BD;
d930faee 470 dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n",
f57c1edc 471 card->rxbd_ring_size);
fc331460
AP
472 card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
473 card->rxbd_ring_size,
474 &card->rxbd_ring_pbase);
d930faee 475 if (!card->rxbd_ring_vbase) {
fc331460
AP
476 dev_err(adapter->dev,
477 "allocate consistent memory (%d bytes) failed!\n",
478 card->rxbd_ring_size);
8c53e42d 479 return -ENOMEM;
d930faee 480 }
d930faee 481
f57c1edc
YAP
482 dev_dbg(adapter->dev,
483 "info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n",
484 card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase,
485 (u32)((u64)card->rxbd_ring_pbase >> 32),
486 card->rxbd_ring_size);
d930faee
AK
487
488 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
489 card->rxbd_ring[i] = (struct mwifiex_pcie_buf_desc *)
f57c1edc
YAP
490 (card->rxbd_ring_vbase +
491 (sizeof(struct mwifiex_pcie_buf_desc)
492 * i));
d930faee
AK
493
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) {
f57c1edc
YAP
497 dev_err(adapter->dev,
498 "Unable to allocate skb for RX ring.\n");
d930faee
AK
499 kfree(card->rxbd_ring_vbase);
500 return -ENOMEM;
501 }
fc331460
AP
502 if (mwifiex_map_pci_memory(adapter, skb,
503 MWIFIEX_RX_DATA_BUF_SIZE,
504 PCI_DMA_FROMDEVICE))
505 return -1;
506
507 MWIFIEX_SKB_PACB(skb, &buf_pa);
d930faee
AK
508
509 dev_dbg(adapter->dev, "info: RX ring: add new skb base: %p, "
f57c1edc 510 "buf_base: %p, buf_pbase: %#x:%x, buf_len: %#x\n",
fc331460 511 skb, skb->data, (u32)buf_pa, (u32)((u64)buf_pa >> 32),
f57c1edc 512 skb->len);
d930faee
AK
513
514 card->rx_buf_list[i] = skb;
fc331460 515 card->rxbd_ring[i]->paddr = buf_pa;
d930faee
AK
516 card->rxbd_ring[i]->len = (u16)skb->len;
517 card->rxbd_ring[i]->flags = 0;
518 }
519
520 return 0;
521}
522
523/*
524 * This function deletes Buffer descriptor ring for RX
525 */
526static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
527{
528 struct pcie_service_card *card = adapter->card;
fc331460 529 struct sk_buff *skb;
d930faee
AK
530 int i;
531
532 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
fc331460
AP
533 if (card->rx_buf_list[i]) {
534 skb = card->rx_buf_list[i];
535 pci_unmap_single(card->dev, card->rxbd_ring[i]->paddr ,
536 MWIFIEX_RX_DATA_BUF_SIZE,
537 PCI_DMA_FROMDEVICE);
538 dev_kfree_skb_any(skb);
539 }
d930faee
AK
540 card->rx_buf_list[i] = NULL;
541 card->rxbd_ring[i]->paddr = 0;
542 card->rxbd_ring[i]->len = 0;
543 card->rxbd_ring[i]->flags = 0;
544 card->rxbd_ring[i] = NULL;
545 }
546
fc331460
AP
547 if (card->rxbd_ring_vbase)
548 pci_free_consistent(card->dev, card->rxbd_ring_size,
549 card->rxbd_ring_vbase,
550 card->rxbd_ring_pbase);
d930faee
AK
551 card->rxbd_ring_size = 0;
552 card->rxbd_wrptr = 0;
553 card->rxbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND;
554 card->rxbd_ring_vbase = NULL;
fc331460 555 card->rxbd_ring_pbase = 0;
d930faee
AK
556
557 return 0;
558}
559
560/*
561 * This function creates buffer descriptor ring for Events
562 */
563static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
564{
565 struct pcie_service_card *card = adapter->card;
566 struct sk_buff *skb;
567 int i;
fc331460 568 dma_addr_t buf_pa;
d930faee
AK
569
570 /*
571 * driver maintaines the read pointer and firmware maintaines the write
572 * pointer. The write pointer starts at 0 (zero) while the read pointer
573 * starts at zero with rollover bit set
574 */
575 card->evtbd_wrptr = 0;
576 card->evtbd_rdptr |= MWIFIEX_BD_FLAG_ROLLOVER_IND;
577
578 card->evtbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
f57c1edc 579 MWIFIEX_MAX_EVT_BD;
d930faee 580 dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n",
f57c1edc 581 card->evtbd_ring_size);
fc331460
AP
582 card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
583 card->evtbd_ring_size,
584 &card->evtbd_ring_pbase);
d930faee 585 if (!card->evtbd_ring_vbase) {
f57c1edc 586 dev_err(adapter->dev,
fc331460
AP
587 "allocate consistent memory (%d bytes) failed!\n",
588 card->evtbd_ring_size);
8c53e42d 589 return -ENOMEM;
d930faee 590 }
d930faee 591
f57c1edc
YAP
592 dev_dbg(adapter->dev,
593 "info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n",
594 card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase,
595 (u32)((u64)card->evtbd_ring_pbase >> 32),
596 card->evtbd_ring_size);
d930faee
AK
597
598 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
599 card->evtbd_ring[i] = (struct mwifiex_pcie_buf_desc *)
f57c1edc
YAP
600 (card->evtbd_ring_vbase +
601 (sizeof(struct mwifiex_pcie_buf_desc)
602 * i));
d930faee
AK
603
604 /* Allocate skb here so that firmware can DMA data from it */
605 skb = dev_alloc_skb(MAX_EVENT_SIZE);
606 if (!skb) {
f57c1edc
YAP
607 dev_err(adapter->dev,
608 "Unable to allocate skb for EVENT buf.\n");
d930faee
AK
609 kfree(card->evtbd_ring_vbase);
610 return -ENOMEM;
611 }
d930faee
AK
612 skb_put(skb, MAX_EVENT_SIZE);
613
fc331460
AP
614 if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE,
615 PCI_DMA_FROMDEVICE))
616 return -1;
617
618 MWIFIEX_SKB_PACB(skb, &buf_pa);
d930faee 619 dev_dbg(adapter->dev, "info: Evt ring: add new skb. base: %p, "
f57c1edc 620 "buf_base: %p, buf_pbase: %#x:%x, buf_len: %#x\n",
fc331460 621 skb, skb->data, (u32)buf_pa, (u32)((u64)buf_pa >> 32),
f57c1edc 622 skb->len);
d930faee
AK
623
624 card->evt_buf_list[i] = skb;
fc331460 625 card->evtbd_ring[i]->paddr = buf_pa;
d930faee
AK
626 card->evtbd_ring[i]->len = (u16)skb->len;
627 card->evtbd_ring[i]->flags = 0;
628 }
629
630 return 0;
631}
632
633/*
634 * This function deletes Buffer descriptor ring for Events
635 */
636static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
637{
638 struct pcie_service_card *card = adapter->card;
fc331460 639 struct sk_buff *skb;
d930faee
AK
640 int i;
641
642 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
fc331460
AP
643 if (card->evt_buf_list[i]) {
644 skb = card->evt_buf_list[i];
645 pci_unmap_single(card->dev, card->evtbd_ring[i]->paddr,
646 MAX_EVENT_SIZE, PCI_DMA_FROMDEVICE);
647 dev_kfree_skb_any(skb);
648 }
d930faee
AK
649 card->evt_buf_list[i] = NULL;
650 card->evtbd_ring[i]->paddr = 0;
651 card->evtbd_ring[i]->len = 0;
652 card->evtbd_ring[i]->flags = 0;
653 card->evtbd_ring[i] = NULL;
654 }
655
fc331460
AP
656 if (card->evtbd_ring_vbase)
657 pci_free_consistent(card->dev, card->evtbd_ring_size,
658 card->evtbd_ring_vbase,
659 card->evtbd_ring_pbase);
d930faee
AK
660 card->evtbd_wrptr = 0;
661 card->evtbd_rdptr = 0 | MWIFIEX_BD_FLAG_ROLLOVER_IND;
662 card->evtbd_ring_size = 0;
663 card->evtbd_ring_vbase = NULL;
fc331460 664 card->evtbd_ring_pbase = 0;
d930faee
AK
665
666 return 0;
667}
668
669/*
670 * This function allocates a buffer for CMDRSP
671 */
672static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
673{
674 struct pcie_service_card *card = adapter->card;
675 struct sk_buff *skb;
676
677 /* Allocate memory for receiving command response data */
678 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
679 if (!skb) {
f57c1edc
YAP
680 dev_err(adapter->dev,
681 "Unable to allocate skb for command response data.\n");
d930faee
AK
682 return -ENOMEM;
683 }
d930faee 684 skb_put(skb, MWIFIEX_UPLD_SIZE);
fc331460
AP
685 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
686 PCI_DMA_FROMDEVICE))
687 return -1;
d930faee 688
fc331460 689 card->cmdrsp_buf = skb;
d930faee
AK
690
691 return 0;
692}
693
694/*
695 * This function deletes a buffer for CMDRSP
696 */
697static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
698{
699 struct pcie_service_card *card;
fc331460 700 dma_addr_t buf_pa;
d930faee
AK
701
702 if (!adapter)
703 return 0;
704
705 card = adapter->card;
706
fc331460
AP
707 if (card && card->cmdrsp_buf) {
708 MWIFIEX_SKB_PACB(card->cmdrsp_buf, &buf_pa);
709 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
710 PCI_DMA_FROMDEVICE);
d930faee 711 dev_kfree_skb_any(card->cmdrsp_buf);
fc331460 712 }
d930faee 713
fc331460
AP
714 if (card && card->cmd_buf) {
715 MWIFIEX_SKB_PACB(card->cmd_buf, &buf_pa);
716 pci_unmap_single(card->dev, buf_pa, MWIFIEX_SIZE_OF_CMD_BUFFER,
717 PCI_DMA_TODEVICE);
d930faee 718 dev_kfree_skb_any(card->cmd_buf);
fc331460 719 }
d930faee
AK
720 return 0;
721}
722
723/*
724 * This function allocates a buffer for sleep cookie
725 */
726static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
727{
d930faee
AK
728 struct pcie_service_card *card = adapter->card;
729
fc331460
AP
730 card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32),
731 &card->sleep_cookie_pbase);
732 if (!card->sleep_cookie_vbase) {
733 dev_err(adapter->dev, "pci_alloc_consistent failed!\n");
d930faee
AK
734 return -ENOMEM;
735 }
d930faee 736 /* Init val of Sleep Cookie */
fc331460 737 *(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE;
d930faee
AK
738
739 dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n",
fc331460 740 *((u32 *)card->sleep_cookie_vbase));
d930faee
AK
741
742 return 0;
743}
744
745/*
746 * This function deletes buffer for sleep cookie
747 */
748static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
749{
750 struct pcie_service_card *card;
751
752 if (!adapter)
753 return 0;
754
755 card = adapter->card;
756
fc331460
AP
757 if (card && card->sleep_cookie_vbase) {
758 pci_free_consistent(card->dev, sizeof(u32),
759 card->sleep_cookie_vbase,
760 card->sleep_cookie_pbase);
761 card->sleep_cookie_vbase = NULL;
d930faee
AK
762 }
763
764 return 0;
765}
766
767/*
768 * This function sends data buffer to device
769 */
770static int
771mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb)
772{
773 struct pcie_service_card *card = adapter->card;
774 u32 wrindx, rdptr;
fc331460 775 phys_addr_t buf_pa;
d930faee
AK
776 __le16 *tmp;
777
778 if (!mwifiex_pcie_ok_to_access_hw(adapter))
779 mwifiex_pm_wakeup_card(adapter);
780
781 /* Read the TX ring read pointer set by firmware */
782 if (mwifiex_read_reg(adapter, REG_TXBD_RDPTR, &rdptr)) {
f57c1edc
YAP
783 dev_err(adapter->dev,
784 "SEND DATA: failed to read REG_TXBD_RDPTR\n");
d930faee
AK
785 return -1;
786 }
787
788 wrindx = card->txbd_wrptr & MWIFIEX_TXBD_MASK;
789
790 dev_dbg(adapter->dev, "info: SEND DATA: <Rd: %#x, Wr: %#x>\n", rdptr,
f57c1edc 791 card->txbd_wrptr);
d930faee
AK
792 if (((card->txbd_wrptr & MWIFIEX_TXBD_MASK) !=
793 (rdptr & MWIFIEX_TXBD_MASK)) ||
794 ((card->txbd_wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) !=
795 (rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) {
796 struct sk_buff *skb_data;
797 u8 *payload;
798
799 adapter->data_sent = true;
800 skb_data = card->tx_buf_list[wrindx];
801 memcpy(skb_data->data, skb->data, skb->len);
802 payload = skb_data->data;
803 tmp = (__le16 *)&payload[0];
804 *tmp = cpu_to_le16((u16)skb->len);
805 tmp = (__le16 *)&payload[2];
806 *tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
807 skb_put(skb_data, MWIFIEX_RX_DATA_BUF_SIZE - skb_data->len);
808 skb_trim(skb_data, skb->len);
fc331460
AP
809 MWIFIEX_SKB_PACB(skb_data, &buf_pa);
810 card->txbd_ring[wrindx]->paddr = buf_pa;
d930faee
AK
811 card->txbd_ring[wrindx]->len = (u16)skb_data->len;
812 card->txbd_ring[wrindx]->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
813 MWIFIEX_BD_FLAG_LAST_DESC;
814
815 if ((++card->txbd_wrptr & MWIFIEX_TXBD_MASK) ==
816 MWIFIEX_MAX_TXRX_BD)
817 card->txbd_wrptr = ((card->txbd_wrptr &
818 MWIFIEX_BD_FLAG_ROLLOVER_IND) ^
819 MWIFIEX_BD_FLAG_ROLLOVER_IND);
820
821 /* Write the TX ring write pointer in to REG_TXBD_WRPTR */
822 if (mwifiex_write_reg(adapter, REG_TXBD_WRPTR,
f57c1edc
YAP
823 card->txbd_wrptr)) {
824 dev_err(adapter->dev,
825 "SEND DATA: failed to write REG_TXBD_WRPTR\n");
d930faee
AK
826 return 0;
827 }
828
829 /* Send the TX ready interrupt */
830 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
831 CPU_INTR_DNLD_RDY)) {
f57c1edc
YAP
832 dev_err(adapter->dev,
833 "SEND DATA: failed to assert door-bell intr\n");
d930faee
AK
834 return -1;
835 }
836 dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: "
f57c1edc
YAP
837 "%#x> and sent packet to firmware successfully\n",
838 rdptr, card->txbd_wrptr);
d930faee 839 } else {
f57c1edc
YAP
840 dev_dbg(adapter->dev,
841 "info: TX Ring full, can't send packets to fw\n");
d930faee
AK
842 adapter->data_sent = true;
843 /* Send the TX ready interrupt */
844 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
845 CPU_INTR_DNLD_RDY))
f57c1edc
YAP
846 dev_err(adapter->dev,
847 "SEND DATA: failed to assert door-bell intr\n");
d930faee
AK
848 return -EBUSY;
849 }
850
851 return 0;
852}
853
854/*
855 * This function handles received buffer ring and
856 * dispatches packets to upper
857 */
858static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
859{
860 struct pcie_service_card *card = adapter->card;
861 u32 wrptr, rd_index;
862 int ret = 0;
863 struct sk_buff *skb_tmp = NULL;
864
865 /* Read the RX ring Write pointer set by firmware */
866 if (mwifiex_read_reg(adapter, REG_RXBD_WRPTR, &wrptr)) {
f57c1edc
YAP
867 dev_err(adapter->dev,
868 "RECV DATA: failed to read REG_TXBD_RDPTR\n");
d930faee
AK
869 ret = -1;
870 goto done;
871 }
872
873 while (((wrptr & MWIFIEX_RXBD_MASK) !=
874 (card->rxbd_rdptr & MWIFIEX_RXBD_MASK)) ||
875 ((wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) ==
876 (card->rxbd_rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) {
877 struct sk_buff *skb_data;
878 u16 rx_len;
879
880 rd_index = card->rxbd_rdptr & MWIFIEX_RXBD_MASK;
881 skb_data = card->rx_buf_list[rd_index];
882
883 /* Get data length from interface header -
884 first byte is len, second byte is type */
885 rx_len = *((u16 *)skb_data->data);
f57c1edc
YAP
886 dev_dbg(adapter->dev,
887 "info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
888 card->rxbd_rdptr, wrptr, rx_len);
d930faee
AK
889 skb_tmp = dev_alloc_skb(rx_len);
890 if (!skb_tmp) {
f57c1edc
YAP
891 dev_dbg(adapter->dev,
892 "info: Failed to alloc skb for RX\n");
d930faee
AK
893 ret = -EBUSY;
894 goto done;
895 }
896
897 skb_put(skb_tmp, rx_len);
898
899 memcpy(skb_tmp->data, skb_data->data + INTF_HEADER_LEN, rx_len);
900 if ((++card->rxbd_rdptr & MWIFIEX_RXBD_MASK) ==
901 MWIFIEX_MAX_TXRX_BD) {
902 card->rxbd_rdptr = ((card->rxbd_rdptr &
903 MWIFIEX_BD_FLAG_ROLLOVER_IND) ^
904 MWIFIEX_BD_FLAG_ROLLOVER_IND);
905 }
906 dev_dbg(adapter->dev, "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
f57c1edc 907 card->rxbd_rdptr, wrptr);
d930faee
AK
908
909 /* Write the RX ring read pointer in to REG_RXBD_RDPTR */
910 if (mwifiex_write_reg(adapter, REG_RXBD_RDPTR,
911 card->rxbd_rdptr)) {
f57c1edc
YAP
912 dev_err(adapter->dev,
913 "RECV DATA: failed to write REG_RXBD_RDPTR\n");
d930faee
AK
914 ret = -1;
915 goto done;
916 }
917
918 /* Read the RX ring Write pointer set by firmware */
919 if (mwifiex_read_reg(adapter, REG_RXBD_WRPTR, &wrptr)) {
f57c1edc
YAP
920 dev_err(adapter->dev,
921 "RECV DATA: failed to read REG_TXBD_RDPTR\n");
d930faee
AK
922 ret = -1;
923 goto done;
924 }
f57c1edc
YAP
925 dev_dbg(adapter->dev,
926 "info: RECV DATA: Rcvd packet from fw successfully\n");
d930faee
AK
927 mwifiex_handle_rx_packet(adapter, skb_tmp);
928 }
929
930done:
931 if (ret && skb_tmp)
932 dev_kfree_skb_any(skb_tmp);
933 return ret;
934}
935
936/*
937 * This function downloads the boot command to device
938 */
939static int
940mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
941{
fc331460
AP
942 dma_addr_t buf_pa;
943 struct pcie_service_card *card = adapter->card;
d930faee 944
fc331460 945 if (!(skb->data && skb->len)) {
f57c1edc 946 dev_err(adapter->dev,
fc331460
AP
947 "Invalid parameter in %s <%p. len %d>\n",
948 __func__, skb->data, skb->len);
d930faee
AK
949 return -1;
950 }
951
fc331460
AP
952 if (mwifiex_map_pci_memory(adapter, skb, skb->len , PCI_DMA_TODEVICE))
953 return -1;
954
955 MWIFIEX_SKB_PACB(skb, &buf_pa);
956
d930faee
AK
957 /* Write the lower 32bits of the physical address to scratch
958 * register 0 */
fc331460 959 if (mwifiex_write_reg(adapter, PCIE_SCRATCH_0_REG, (u32)buf_pa)) {
f57c1edc
YAP
960 dev_err(adapter->dev,
961 "%s: failed to write download command to boot code.\n",
962 __func__);
fc331460
AP
963 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
964 PCI_DMA_TODEVICE);
d930faee
AK
965 return -1;
966 }
967
968 /* Write the upper 32bits of the physical address to scratch
969 * register 1 */
970 if (mwifiex_write_reg(adapter, PCIE_SCRATCH_1_REG,
fc331460 971 (u32)((u64)buf_pa >> 32))) {
f57c1edc
YAP
972 dev_err(adapter->dev,
973 "%s: failed to write download command to boot code.\n",
974 __func__);
fc331460
AP
975 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
976 PCI_DMA_TODEVICE);
d930faee
AK
977 return -1;
978 }
979
980 /* Write the command length to scratch register 2 */
981 if (mwifiex_write_reg(adapter, PCIE_SCRATCH_2_REG, skb->len)) {
f57c1edc
YAP
982 dev_err(adapter->dev,
983 "%s: failed to write command len to scratch reg 2\n",
984 __func__);
fc331460
AP
985 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
986 PCI_DMA_TODEVICE);
d930faee
AK
987 return -1;
988 }
989
990 /* Ring the door bell */
991 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
992 CPU_INTR_DOOR_BELL)) {
f57c1edc
YAP
993 dev_err(adapter->dev,
994 "%s: failed to assert door-bell intr\n", __func__);
fc331460
AP
995 pci_unmap_single(card->dev, buf_pa,
996 MWIFIEX_UPLD_SIZE, PCI_DMA_TODEVICE);
d930faee
AK
997 return -1;
998 }
999
1000 return 0;
1001}
1002
c6d1d87a
AP
1003/* This function init rx port in firmware which in turn enables to receive data
1004 * from device before transmitting any packet.
1005 */
1006static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
1007{
1008 struct pcie_service_card *card = adapter->card;
1009
1010 /* Write the RX ring read pointer in to REG_RXBD_RDPTR */
1011 if (mwifiex_write_reg(adapter, REG_RXBD_RDPTR, card->rxbd_rdptr | 0)) {
1012 dev_err(adapter->dev,
1013 "RECV DATA: failed to write REG_RXBD_RDPTR\n");
1014 return -1;
1015 }
1016 return 0;
1017}
1018
1019/* This function downloads commands to the device
d930faee
AK
1020 */
1021static int
1022mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1023{
1024 struct pcie_service_card *card = adapter->card;
1025 int ret = 0;
fc331460
AP
1026 dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
1027 u8 *payload = (u8 *)skb->data;
d930faee
AK
1028
1029 if (!(skb->data && skb->len)) {
1030 dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x>\n",
f57c1edc 1031 __func__, skb->data, skb->len);
d930faee
AK
1032 return -1;
1033 }
1034
1035 /* Make sure a command response buffer is available */
1036 if (!card->cmdrsp_buf) {
f57c1edc
YAP
1037 dev_err(adapter->dev,
1038 "No response buffer available, send command failed\n");
d930faee
AK
1039 return -EBUSY;
1040 }
1041
fc331460
AP
1042 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1043 mwifiex_pm_wakeup_card(adapter);
d930faee
AK
1044
1045 adapter->cmd_sent = true;
fc331460
AP
1046
1047 *(__le16 *)&payload[0] = cpu_to_le16((u16)skb->len);
1048 *(__le16 *)&payload[2] = cpu_to_le16(MWIFIEX_TYPE_CMD);
1049
1050 if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1051 return -1;
1052
1053 card->cmd_buf = skb;
d930faee
AK
1054
1055 /* To send a command, the driver will:
1056 1. Write the 64bit physical address of the data buffer to
1057 SCRATCH1 + SCRATCH0
1058 2. Ring the door bell (i.e. set the door bell interrupt)
1059
1060 In response to door bell interrupt, the firmware will perform
1061 the DMA of the command packet (first header to obtain the total
1062 length and then rest of the command).
1063 */
1064
1065 if (card->cmdrsp_buf) {
fc331460 1066 MWIFIEX_SKB_PACB(card->cmdrsp_buf, &cmdrsp_buf_pa);
d930faee
AK
1067 /* Write the lower 32bits of the cmdrsp buffer physical
1068 address */
1069 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_LO,
fc331460 1070 (u32)cmdrsp_buf_pa)) {
f57c1edc
YAP
1071 dev_err(adapter->dev,
1072 "Failed to write download cmd to boot code.\n");
d930faee
AK
1073 ret = -1;
1074 goto done;
1075 }
1076 /* Write the upper 32bits of the cmdrsp buffer physical
1077 address */
1078 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_HI,
fc331460 1079 (u32)((u64)cmdrsp_buf_pa >> 32))) {
f57c1edc
YAP
1080 dev_err(adapter->dev,
1081 "Failed to write download cmd to boot code.\n");
d930faee
AK
1082 ret = -1;
1083 goto done;
1084 }
1085 }
1086
fc331460 1087 MWIFIEX_SKB_PACB(card->cmd_buf, &cmd_buf_pa);
d930faee 1088 /* Write the lower 32bits of the physical address to REG_CMD_ADDR_LO */
fc331460 1089 if (mwifiex_write_reg(adapter, REG_CMD_ADDR_LO, (u32)cmd_buf_pa)) {
f57c1edc
YAP
1090 dev_err(adapter->dev,
1091 "Failed to write download cmd to boot code.\n");
d930faee
AK
1092 ret = -1;
1093 goto done;
1094 }
1095 /* Write the upper 32bits of the physical address to REG_CMD_ADDR_HI */
1096 if (mwifiex_write_reg(adapter, REG_CMD_ADDR_HI,
fc331460 1097 (u32)((u64)cmd_buf_pa >> 32))) {
f57c1edc
YAP
1098 dev_err(adapter->dev,
1099 "Failed to write download cmd to boot code.\n");
d930faee
AK
1100 ret = -1;
1101 goto done;
1102 }
1103
1104 /* Write the command length to REG_CMD_SIZE */
f57c1edc
YAP
1105 if (mwifiex_write_reg(adapter, REG_CMD_SIZE, card->cmd_buf->len)) {
1106 dev_err(adapter->dev,
1107 "Failed to write cmd len to REG_CMD_SIZE\n");
d930faee
AK
1108 ret = -1;
1109 goto done;
1110 }
1111
1112 /* Ring the door bell */
1113 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1114 CPU_INTR_DOOR_BELL)) {
f57c1edc
YAP
1115 dev_err(adapter->dev,
1116 "Failed to assert door-bell intr\n");
d930faee
AK
1117 ret = -1;
1118 goto done;
1119 }
1120
1121done:
1122 if (ret)
1123 adapter->cmd_sent = false;
1124
1125 return 0;
1126}
1127
1128/*
1129 * This function handles command complete interrupt
1130 */
1131static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1132{
1133 struct pcie_service_card *card = adapter->card;
f57c1edc 1134 struct sk_buff *skb = card->cmdrsp_buf;
d930faee 1135 int count = 0;
fc331460
AP
1136 u16 rx_len;
1137 __le16 pkt_len;
1138 dma_addr_t buf_pa;
d930faee
AK
1139
1140 dev_dbg(adapter->dev, "info: Rx CMD Response\n");
1141
fc331460
AP
1142 MWIFIEX_SKB_PACB(skb, &buf_pa);
1143 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1144 PCI_DMA_FROMDEVICE);
1145
1146 pkt_len = *((__le16 *)skb->data);
1147 rx_len = le16_to_cpu(pkt_len);
1148 skb_trim(skb, rx_len);
1149 skb_pull(skb, INTF_HEADER_LEN);
1150
d930faee 1151 if (!adapter->curr_cmd) {
d930faee 1152 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
f57c1edc
YAP
1153 mwifiex_process_sleep_confirm_resp(adapter, skb->data,
1154 skb->len);
d930faee
AK
1155 while (mwifiex_pcie_ok_to_access_hw(adapter) &&
1156 (count++ < 10))
e7891ba2 1157 usleep_range(50, 60);
d930faee 1158 } else {
f57c1edc
YAP
1159 dev_err(adapter->dev,
1160 "There is no command but got cmdrsp\n");
d930faee 1161 }
f57c1edc
YAP
1162 memcpy(adapter->upld_buf, skb->data,
1163 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
fc331460
AP
1164 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1165 PCI_DMA_FROMDEVICE))
1166 return -1;
1167
1168 MWIFIEX_SKB_PACB(skb, &buf_pa);
d930faee 1169 } else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
f57c1edc 1170 adapter->curr_cmd->resp_skb = skb;
d930faee
AK
1171 adapter->cmd_resp_received = true;
1172 /* Take the pointer and set it to CMD node and will
1173 return in the response complete callback */
1174 card->cmdrsp_buf = NULL;
1175
1176 /* Clear the cmd-rsp buffer address in scratch registers. This
1177 will prevent firmware from writing to the same response
1178 buffer again. */
1179 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_LO, 0)) {
f57c1edc
YAP
1180 dev_err(adapter->dev,
1181 "cmd_done: failed to clear cmd_rsp_addr_lo\n");
d930faee
AK
1182 return -1;
1183 }
1184 /* Write the upper 32bits of the cmdrsp buffer physical
1185 address */
1186 if (mwifiex_write_reg(adapter, REG_CMDRSP_ADDR_HI, 0)) {
f57c1edc
YAP
1187 dev_err(adapter->dev,
1188 "cmd_done: failed to clear cmd_rsp_addr_hi\n");
d930faee
AK
1189 return -1;
1190 }
1191 }
1192
1193 return 0;
1194}
1195
1196/*
1197 * Command Response processing complete handler
1198 */
1199static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1200 struct sk_buff *skb)
1201{
1202 struct pcie_service_card *card = adapter->card;
fc331460
AP
1203 dma_addr_t buf_pa;
1204 struct sk_buff *skb_tmp;
d930faee
AK
1205
1206 if (skb) {
1207 card->cmdrsp_buf = skb;
1208 skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
fc331460
AP
1209 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1210 PCI_DMA_FROMDEVICE))
1211 return -1;
1212 }
1213
1214 skb_tmp = card->cmd_buf;
1215 if (skb_tmp) {
1216 MWIFIEX_SKB_PACB(skb_tmp, &buf_pa);
1217 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1218 PCI_DMA_FROMDEVICE);
1219 card->cmd_buf = NULL;
d930faee
AK
1220 }
1221
1222 return 0;
1223}
1224
1225/*
1226 * This function handles firmware event ready interrupt
1227 */
1228static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1229{
1230 struct pcie_service_card *card = adapter->card;
1231 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1232 u32 wrptr, event;
fc331460
AP
1233 dma_addr_t buf_pa;
1234
1235 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1236 mwifiex_pm_wakeup_card(adapter);
d930faee
AK
1237
1238 if (adapter->event_received) {
f57c1edc
YAP
1239 dev_dbg(adapter->dev, "info: Event being processed, "
1240 "do not process this interrupt just yet\n");
d930faee
AK
1241 return 0;
1242 }
1243
1244 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1245 dev_dbg(adapter->dev, "info: Invalid read pointer...\n");
1246 return -1;
1247 }
1248
1249 /* Read the event ring write pointer set by firmware */
1250 if (mwifiex_read_reg(adapter, REG_EVTBD_WRPTR, &wrptr)) {
f57c1edc
YAP
1251 dev_err(adapter->dev,
1252 "EventReady: failed to read REG_EVTBD_WRPTR\n");
d930faee
AK
1253 return -1;
1254 }
1255
1256 dev_dbg(adapter->dev, "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
f57c1edc
YAP
1257 card->evtbd_rdptr, wrptr);
1258 if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
1259 & MWIFIEX_EVTBD_MASK)) ||
d930faee
AK
1260 ((wrptr & MWIFIEX_BD_FLAG_ROLLOVER_IND) ==
1261 (card->evtbd_rdptr & MWIFIEX_BD_FLAG_ROLLOVER_IND))) {
1262 struct sk_buff *skb_cmd;
1263 __le16 data_len = 0;
1264 u16 evt_len;
1265
1266 dev_dbg(adapter->dev, "info: Read Index: %d\n", rdptr);
1267 skb_cmd = card->evt_buf_list[rdptr];
fc331460
AP
1268 MWIFIEX_SKB_PACB(skb_cmd, &buf_pa);
1269 pci_unmap_single(card->dev, buf_pa, MAX_EVENT_SIZE,
1270 PCI_DMA_FROMDEVICE);
1271
d930faee
AK
1272 /* Take the pointer and set it to event pointer in adapter
1273 and will return back after event handling callback */
1274 card->evt_buf_list[rdptr] = NULL;
1275 card->evtbd_ring[rdptr]->paddr = 0;
1276 card->evtbd_ring[rdptr]->len = 0;
1277 card->evtbd_ring[rdptr]->flags = 0;
1278
1279 event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
1280 adapter->event_cause = event;
1281 /* The first 4bytes will be the event transfer header
1282 len is 2 bytes followed by type which is 2 bytes */
1283 memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1284 evt_len = le16_to_cpu(data_len);
1285
1286 skb_pull(skb_cmd, INTF_HEADER_LEN);
1287 dev_dbg(adapter->dev, "info: Event length: %d\n", evt_len);
1288
1289 if ((evt_len > 0) && (evt_len < MAX_EVENT_SIZE))
1290 memcpy(adapter->event_body, skb_cmd->data +
1291 MWIFIEX_EVENT_HEADER_LEN, evt_len -
1292 MWIFIEX_EVENT_HEADER_LEN);
1293
1294 adapter->event_received = true;
1295 adapter->event_skb = skb_cmd;
1296
1297 /* Do not update the event read pointer here, wait till the
1298 buffer is released. This is just to make things simpler,
1299 we need to find a better method of managing these buffers.
1300 */
1301 }
1302
1303 return 0;
1304}
1305
1306/*
1307 * Event processing complete handler
1308 */
1309static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1310 struct sk_buff *skb)
1311{
1312 struct pcie_service_card *card = adapter->card;
1313 int ret = 0;
1314 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1315 u32 wrptr;
fc331460 1316 dma_addr_t buf_pa;
d930faee
AK
1317
1318 if (!skb)
1319 return 0;
1320
1eb54c8a 1321 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
d930faee 1322 dev_err(adapter->dev, "event_complete: Invalid rdptr 0x%x\n",
f57c1edc 1323 rdptr);
8c53e42d 1324 return -EINVAL;
1eb54c8a 1325 }
d930faee
AK
1326
1327 /* Read the event ring write pointer set by firmware */
1328 if (mwifiex_read_reg(adapter, REG_EVTBD_WRPTR, &wrptr)) {
f57c1edc
YAP
1329 dev_err(adapter->dev,
1330 "event_complete: failed to read REG_EVTBD_WRPTR\n");
8c53e42d 1331 return -1;
d930faee
AK
1332 }
1333
1334 if (!card->evt_buf_list[rdptr]) {
1335 skb_push(skb, INTF_HEADER_LEN);
fc331460
AP
1336 if (mwifiex_map_pci_memory(adapter, skb,
1337 MAX_EVENT_SIZE,
1338 PCI_DMA_FROMDEVICE))
1339 return -1;
1340 MWIFIEX_SKB_PACB(skb, &buf_pa);
d930faee 1341 card->evt_buf_list[rdptr] = skb;
fc331460
AP
1342 MWIFIEX_SKB_PACB(skb, &buf_pa);
1343 card->evtbd_ring[rdptr]->paddr = buf_pa;
d930faee
AK
1344 card->evtbd_ring[rdptr]->len = (u16)skb->len;
1345 card->evtbd_ring[rdptr]->flags = 0;
1346 skb = NULL;
1347 } else {
f57c1edc
YAP
1348 dev_dbg(adapter->dev,
1349 "info: ERROR: buf still valid at index %d, <%p, %p>\n",
1350 rdptr, card->evt_buf_list[rdptr], skb);
d930faee
AK
1351 }
1352
1353 if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
1354 card->evtbd_rdptr = ((card->evtbd_rdptr &
1355 MWIFIEX_BD_FLAG_ROLLOVER_IND) ^
1356 MWIFIEX_BD_FLAG_ROLLOVER_IND);
1357 }
1358
1359 dev_dbg(adapter->dev, "info: Updated <Rd: 0x%x, Wr: 0x%x>",
f57c1edc 1360 card->evtbd_rdptr, wrptr);
d930faee
AK
1361
1362 /* Write the event ring read pointer in to REG_EVTBD_RDPTR */
1363 if (mwifiex_write_reg(adapter, REG_EVTBD_RDPTR, card->evtbd_rdptr)) {
f57c1edc
YAP
1364 dev_err(adapter->dev,
1365 "event_complete: failed to read REG_EVTBD_RDPTR\n");
8c53e42d 1366 return -1;
d930faee
AK
1367 }
1368
d930faee
AK
1369 dev_dbg(adapter->dev, "info: Check Events Again\n");
1370 ret = mwifiex_pcie_process_event_ready(adapter);
1371
1372 return ret;
1373}
1374
1375/*
1376 * This function downloads the firmware to the card.
1377 *
1378 * Firmware is downloaded to the card in blocks. Every block download
1379 * is tested for CRC errors, and retried a number of times before
1380 * returning failure.
1381 */
1382static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1383 struct mwifiex_fw_image *fw)
1384{
1385 int ret;
1386 u8 *firmware = fw->fw_buf;
1387 u32 firmware_len = fw->fw_len;
1388 u32 offset = 0;
1389 struct sk_buff *skb;
1390 u32 txlen, tx_blocks = 0, tries, len;
1391 u32 block_retry_cnt = 0;
fc331460
AP
1392 dma_addr_t buf_pa;
1393 struct pcie_service_card *card = adapter->card;
d930faee
AK
1394
1395 if (!firmware || !firmware_len) {
f57c1edc
YAP
1396 dev_err(adapter->dev,
1397 "No firmware image found! Terminating download\n");
d930faee
AK
1398 return -1;
1399 }
1400
1401 dev_dbg(adapter->dev, "info: Downloading FW image (%d bytes)\n",
f57c1edc 1402 firmware_len);
d930faee
AK
1403
1404 if (mwifiex_pcie_disable_host_int(adapter)) {
f57c1edc
YAP
1405 dev_err(adapter->dev,
1406 "%s: Disabling interrupts failed.\n", __func__);
d930faee
AK
1407 return -1;
1408 }
1409
1410 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
1411 if (!skb) {
1412 ret = -ENOMEM;
1413 goto done;
1414 }
d930faee
AK
1415
1416 /* Perform firmware data transfer */
1417 do {
1418 u32 ireg_intr = 0;
1419
1420 /* More data? */
1421 if (offset >= firmware_len)
1422 break;
1423
1424 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1425 ret = mwifiex_read_reg(adapter, PCIE_SCRATCH_2_REG,
1426 &len);
1427 if (ret) {
f57c1edc
YAP
1428 dev_warn(adapter->dev,
1429 "Failed reading len from boot code\n");
d930faee
AK
1430 goto done;
1431 }
1432 if (len)
1433 break;
e7891ba2 1434 usleep_range(10, 20);
d930faee
AK
1435 }
1436
1437 if (!len) {
1438 break;
1439 } else if (len > MWIFIEX_UPLD_SIZE) {
1440 pr_err("FW download failure @ %d, invalid length %d\n",
f57c1edc 1441 offset, len);
d930faee
AK
1442 ret = -1;
1443 goto done;
1444 }
1445
1446 txlen = len;
1447
1448 if (len & BIT(0)) {
1449 block_retry_cnt++;
1450 if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
1451 pr_err("FW download failure @ %d, over max "
1452 "retry count\n", offset);
1453 ret = -1;
1454 goto done;
1455 }
1456 dev_err(adapter->dev, "FW CRC error indicated by the "
f57c1edc
YAP
1457 "helper: len = 0x%04X, txlen = %d\n",
1458 len, txlen);
d930faee
AK
1459 len &= ~BIT(0);
1460 /* Setting this to 0 to resend from same offset */
1461 txlen = 0;
1462 } else {
1463 block_retry_cnt = 0;
1464 /* Set blocksize to transfer - checking for
1465 last block */
1466 if (firmware_len - offset < txlen)
1467 txlen = firmware_len - offset;
1468
1469 dev_dbg(adapter->dev, ".");
1470
f57c1edc
YAP
1471 tx_blocks = (txlen +
1472 MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD - 1) /
1473 MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD;
d930faee
AK
1474
1475 /* Copy payload to buffer */
1476 memmove(skb->data, &firmware[offset], txlen);
1477 }
1478
1479 skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1480 skb_trim(skb, tx_blocks * MWIFIEX_PCIE_BLOCK_SIZE_FW_DNLD);
1481
1482 /* Send the boot command to device */
1483 if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
f57c1edc
YAP
1484 dev_err(adapter->dev,
1485 "Failed to send firmware download command\n");
d930faee
AK
1486 ret = -1;
1487 goto done;
1488 }
fc331460
AP
1489
1490 MWIFIEX_SKB_PACB(skb, &buf_pa);
1491
d930faee
AK
1492 /* Wait for the command done interrupt */
1493 do {
1494 if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
1495 &ireg_intr)) {
1496 dev_err(adapter->dev, "%s: Failed to read "
f57c1edc
YAP
1497 "interrupt status during fw dnld.\n",
1498 __func__);
fc331460
AP
1499 pci_unmap_single(card->dev, buf_pa, skb->len,
1500 PCI_DMA_TODEVICE);
d930faee
AK
1501 ret = -1;
1502 goto done;
1503 }
1504 } while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
1505 CPU_INTR_DOOR_BELL);
fc331460
AP
1506
1507 pci_unmap_single(card->dev, buf_pa, skb->len,
1508 PCI_DMA_TODEVICE);
1509
d930faee
AK
1510 offset += txlen;
1511 } while (true);
1512
1513 dev_dbg(adapter->dev, "info:\nFW download over, size %d bytes\n",
f57c1edc 1514 offset);
d930faee
AK
1515
1516 ret = 0;
1517
1518done:
1519 dev_kfree_skb_any(skb);
1520 return ret;
1521}
1522
1523/*
1524 * This function checks the firmware status in card.
1525 *
1526 * The winner interface is also determined by this function.
1527 */
1528static int
1529mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
1530{
1531 int ret = 0;
1532 u32 firmware_stat, winner_status;
1533 u32 tries;
1534
1535 /* Mask spurios interrupts */
1536 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
f57c1edc 1537 HOST_INTR_MASK)) {
d930faee
AK
1538 dev_warn(adapter->dev, "Write register failed\n");
1539 return -1;
1540 }
1541
1542 dev_dbg(adapter->dev, "Setting driver ready signature\n");
1543 if (mwifiex_write_reg(adapter, REG_DRV_READY, FIRMWARE_READY_PCIE)) {
f57c1edc
YAP
1544 dev_err(adapter->dev,
1545 "Failed to write driver ready signature\n");
d930faee
AK
1546 return -1;
1547 }
1548
1549 /* Wait for firmware initialization event */
1550 for (tries = 0; tries < poll_num; tries++) {
1551 if (mwifiex_read_reg(adapter, PCIE_SCRATCH_3_REG,
1552 &firmware_stat))
1553 ret = -1;
1554 else
1555 ret = 0;
1556 if (ret)
1557 continue;
1558 if (firmware_stat == FIRMWARE_READY_PCIE) {
1559 ret = 0;
1560 break;
1561 } else {
1562 mdelay(100);
1563 ret = -1;
1564 }
1565 }
1566
1567 if (ret) {
1568 if (mwifiex_read_reg(adapter, PCIE_SCRATCH_3_REG,
1569 &winner_status))
1570 ret = -1;
1571 else if (!winner_status) {
1572 dev_err(adapter->dev, "PCI-E is the winner\n");
1573 adapter->winner = 1;
1574 ret = -1;
1575 } else {
f57c1edc
YAP
1576 dev_err(adapter->dev,
1577 "PCI-E is not the winner <%#x,%d>, exit dnld\n",
1578 ret, adapter->winner);
d930faee
AK
1579 ret = 0;
1580 }
1581 }
1582
1583 return ret;
1584}
1585
1586/*
1587 * This function reads the interrupt status from card.
1588 */
1589static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
1590{
1591 u32 pcie_ireg;
1592 unsigned long flags;
1593
1594 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1595 return;
1596
1597 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) {
1598 dev_warn(adapter->dev, "Read register failed\n");
1599 return;
1600 }
1601
1602 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
1603
1604 mwifiex_pcie_disable_host_int(adapter);
1605
1606 /* Clear the pending interrupts */
1607 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
1608 ~pcie_ireg)) {
1609 dev_warn(adapter->dev, "Write register failed\n");
1610 return;
1611 }
1612 spin_lock_irqsave(&adapter->int_lock, flags);
1613 adapter->int_status |= pcie_ireg;
1614 spin_unlock_irqrestore(&adapter->int_lock, flags);
1615
1616 if (pcie_ireg & HOST_INTR_CMD_DONE) {
1617 if ((adapter->ps_state == PS_STATE_SLEEP_CFM) ||
1618 (adapter->ps_state == PS_STATE_SLEEP)) {
1619 mwifiex_pcie_enable_host_int(adapter);
1620 if (mwifiex_write_reg(adapter,
f57c1edc
YAP
1621 PCIE_CPU_INT_EVENT,
1622 CPU_INTR_SLEEP_CFM_DONE)
1623 ) {
1624 dev_warn(adapter->dev,
1625 "Write register failed\n");
d930faee
AK
1626 return;
1627
1628 }
1629 }
1630 } else if (!adapter->pps_uapsd_mode &&
1631 adapter->ps_state == PS_STATE_SLEEP) {
1632 /* Potentially for PCIe we could get other
1633 * interrupts like shared. Don't change power
1634 * state until cookie is set */
1635 if (mwifiex_pcie_ok_to_access_hw(adapter))
1636 adapter->ps_state = PS_STATE_AWAKE;
1637 }
1638 }
1639}
1640
1641/*
1642 * Interrupt handler for PCIe root port
1643 *
1644 * This function reads the interrupt status from firmware and assigns
1645 * the main process in workqueue which will handle the interrupt.
1646 */
1647static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
1648{
1649 struct pci_dev *pdev = (struct pci_dev *)context;
1650 struct pcie_service_card *card;
1651 struct mwifiex_adapter *adapter;
1652
1653 if (!pdev) {
1654 pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev);
1655 goto exit;
1656 }
1657
1658 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
1659 if (!card || !card->adapter) {
1660 pr_debug("info: %s: card=%p adapter=%p\n", __func__, card,
f57c1edc 1661 card ? card->adapter : NULL);
d930faee
AK
1662 goto exit;
1663 }
1664 adapter = card->adapter;
1665
1666 if (adapter->surprise_removed)
1667 goto exit;
1668
1669 mwifiex_interrupt_status(adapter);
1670 queue_work(adapter->workqueue, &adapter->main_work);
1671
1672exit:
1673 return IRQ_HANDLED;
1674}
1675
1676/*
1677 * This function checks the current interrupt status.
1678 *
1679 * The following interrupts are checked and handled by this function -
1680 * - Data sent
1681 * - Command sent
1682 * - Command received
1683 * - Packets received
1684 * - Events received
1685 *
1686 * In case of Rx packets received, the packets are uploaded from card to
1687 * host and processed accordingly.
1688 */
1689static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
1690{
1691 int ret;
659c4788 1692 u32 pcie_ireg;
d930faee
AK
1693 unsigned long flags;
1694
1695 spin_lock_irqsave(&adapter->int_lock, flags);
1696 /* Clear out unused interrupts */
659c4788
AP
1697 pcie_ireg = adapter->int_status;
1698 adapter->int_status = 0;
d930faee
AK
1699 spin_unlock_irqrestore(&adapter->int_lock, flags);
1700
659c4788
AP
1701 while (pcie_ireg & HOST_INTR_MASK) {
1702 if (pcie_ireg & HOST_INTR_DNLD_DONE) {
1703 pcie_ireg &= ~HOST_INTR_DNLD_DONE;
d930faee 1704 if (adapter->data_sent) {
f57c1edc 1705 dev_dbg(adapter->dev, "info: DATA sent intr\n");
d930faee
AK
1706 adapter->data_sent = false;
1707 }
1708 }
659c4788
AP
1709 if (pcie_ireg & HOST_INTR_UPLD_RDY) {
1710 pcie_ireg &= ~HOST_INTR_UPLD_RDY;
d930faee
AK
1711 dev_dbg(adapter->dev, "info: Rx DATA\n");
1712 ret = mwifiex_pcie_process_recv_data(adapter);
1713 if (ret)
1714 return ret;
1715 }
659c4788
AP
1716 if (pcie_ireg & HOST_INTR_EVENT_RDY) {
1717 pcie_ireg &= ~HOST_INTR_EVENT_RDY;
d930faee
AK
1718 dev_dbg(adapter->dev, "info: Rx EVENT\n");
1719 ret = mwifiex_pcie_process_event_ready(adapter);
1720 if (ret)
1721 return ret;
1722 }
1723
659c4788
AP
1724 if (pcie_ireg & HOST_INTR_CMD_DONE) {
1725 pcie_ireg &= ~HOST_INTR_CMD_DONE;
d930faee 1726 if (adapter->cmd_sent) {
f57c1edc
YAP
1727 dev_dbg(adapter->dev,
1728 "info: CMD sent Interrupt\n");
d930faee
AK
1729 adapter->cmd_sent = false;
1730 }
1731 /* Handle command response */
1732 ret = mwifiex_pcie_process_cmd_complete(adapter);
1733 if (ret)
1734 return ret;
1735 }
1736
1737 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1738 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
1739 &pcie_ireg)) {
f57c1edc
YAP
1740 dev_warn(adapter->dev,
1741 "Read register failed\n");
d930faee
AK
1742 return -1;
1743 }
1744
1745 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
1746 if (mwifiex_write_reg(adapter,
f57c1edc
YAP
1747 PCIE_HOST_INT_STATUS,
1748 ~pcie_ireg)) {
1749 dev_warn(adapter->dev,
1750 "Write register failed\n");
d930faee
AK
1751 return -1;
1752 }
d930faee
AK
1753 }
1754
1755 }
1756 }
1757 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
f57c1edc 1758 adapter->cmd_sent, adapter->data_sent);
d930faee
AK
1759 mwifiex_pcie_enable_host_int(adapter);
1760
1761 return 0;
1762}
1763
1764/*
1765 * This function downloads data from driver to card.
1766 *
1767 * Both commands and data packets are transferred to the card by this
1768 * function.
1769 *
1770 * This function adds the PCIE specific header to the front of the buffer
1771 * before transferring. The header contains the length of the packet and
1772 * the type. The firmware handles the packets based upon this set type.
1773 */
1774static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
1775 struct sk_buff *skb,
1776 struct mwifiex_tx_param *tx_param)
1777{
fa161cb7
DC
1778 if (!skb) {
1779 dev_err(adapter->dev, "Passed NULL skb to %s\n", __func__);
d930faee
AK
1780 return -1;
1781 }
1782
1783 if (type == MWIFIEX_TYPE_DATA)
1784 return mwifiex_pcie_send_data(adapter, skb);
1785 else if (type == MWIFIEX_TYPE_CMD)
1786 return mwifiex_pcie_send_cmd(adapter, skb);
1787
1788 return 0;
1789}
1790
1791/*
1792 * This function initializes the PCI-E host memory space, WCB rings, etc.
1793 *
1794 * The following initializations steps are followed -
1795 * - Allocate TXBD ring buffers
1796 * - Allocate RXBD ring buffers
1797 * - Allocate event BD ring buffers
1798 * - Allocate command response ring buffer
1799 * - Allocate sleep cookie buffer
1800 */
1801static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
1802{
1803 struct pcie_service_card *card = adapter->card;
1804 int ret;
1805 struct pci_dev *pdev = card->dev;
1806
1807 pci_set_drvdata(pdev, card);
1808
1809 ret = pci_enable_device(pdev);
1810 if (ret)
1811 goto err_enable_dev;
1812
1813 pci_set_master(pdev);
1814
1815 dev_dbg(adapter->dev, "try set_consistent_dma_mask(32)\n");
1816 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1817 if (ret) {
1818 dev_err(adapter->dev, "set_dma_mask(32) failed\n");
1819 goto err_set_dma_mask;
1820 }
1821
1822 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1823 if (ret) {
1824 dev_err(adapter->dev, "set_consistent_dma_mask(64) failed\n");
1825 goto err_set_dma_mask;
1826 }
1827
1828 ret = pci_request_region(pdev, 0, DRV_NAME);
1829 if (ret) {
1830 dev_err(adapter->dev, "req_reg(0) error\n");
1831 goto err_req_region0;
1832 }
1833 card->pci_mmap = pci_iomap(pdev, 0, 0);
1834 if (!card->pci_mmap) {
1835 dev_err(adapter->dev, "iomap(0) error\n");
1836 goto err_iomap0;
1837 }
1838 ret = pci_request_region(pdev, 2, DRV_NAME);
1839 if (ret) {
1840 dev_err(adapter->dev, "req_reg(2) error\n");
1841 goto err_req_region2;
1842 }
1843 card->pci_mmap1 = pci_iomap(pdev, 2, 0);
1844 if (!card->pci_mmap1) {
1845 dev_err(adapter->dev, "iomap(2) error\n");
1846 goto err_iomap2;
1847 }
1848
f57c1edc
YAP
1849 dev_dbg(adapter->dev,
1850 "PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
1851 card->pci_mmap, card->pci_mmap1);
d930faee
AK
1852
1853 card->cmdrsp_buf = NULL;
1854 ret = mwifiex_pcie_create_txbd_ring(adapter);
1855 if (ret)
1856 goto err_cre_txbd;
1857 ret = mwifiex_pcie_create_rxbd_ring(adapter);
1858 if (ret)
1859 goto err_cre_rxbd;
1860 ret = mwifiex_pcie_create_evtbd_ring(adapter);
1861 if (ret)
1862 goto err_cre_evtbd;
1863 ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
1864 if (ret)
1865 goto err_alloc_cmdbuf;
1866 ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
1867 if (ret)
1868 goto err_alloc_cookie;
1869
1870 return ret;
1871
1872err_alloc_cookie:
1873 mwifiex_pcie_delete_cmdrsp_buf(adapter);
1874err_alloc_cmdbuf:
1875 mwifiex_pcie_delete_evtbd_ring(adapter);
1876err_cre_evtbd:
1877 mwifiex_pcie_delete_rxbd_ring(adapter);
1878err_cre_rxbd:
1879 mwifiex_pcie_delete_txbd_ring(adapter);
1880err_cre_txbd:
1881 pci_iounmap(pdev, card->pci_mmap1);
1882err_iomap2:
1883 pci_release_region(pdev, 2);
1884err_req_region2:
1885 pci_iounmap(pdev, card->pci_mmap);
1886err_iomap0:
1887 pci_release_region(pdev, 0);
1888err_req_region0:
1889err_set_dma_mask:
1890 pci_disable_device(pdev);
1891err_enable_dev:
1892 pci_set_drvdata(pdev, NULL);
1893 return ret;
1894}
1895
1896/*
1897 * This function cleans up the allocated card buffers.
1898 *
1899 * The following are freed by this function -
1900 * - TXBD ring buffers
1901 * - RXBD ring buffers
1902 * - Event BD ring buffers
1903 * - Command response ring buffer
1904 * - Sleep cookie buffer
1905 */
1906static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
1907{
1908 struct pcie_service_card *card = adapter->card;
1909 struct pci_dev *pdev = card->dev;
1910
d930faee 1911 if (user_rmmod) {
fc331460 1912 dev_dbg(adapter->dev, "Clearing driver ready signature\n");
d930faee 1913 if (mwifiex_write_reg(adapter, REG_DRV_READY, 0x00000000))
f57c1edc
YAP
1914 dev_err(adapter->dev,
1915 "Failed to write driver not-ready signature\n");
d930faee
AK
1916 }
1917
1918 if (pdev) {
1919 pci_iounmap(pdev, card->pci_mmap);
1920 pci_iounmap(pdev, card->pci_mmap1);
1921
1922 pci_release_regions(pdev);
1923 pci_disable_device(pdev);
1924 pci_set_drvdata(pdev, NULL);
1925 }
1926}
1927
1928/*
1929 * This function registers the PCIE device.
1930 *
1931 * PCIE IRQ is claimed, block size is set and driver data is initialized.
1932 */
1933static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1934{
1935 int ret;
1936 struct pcie_service_card *card = adapter->card;
1937 struct pci_dev *pdev = card->dev;
1938
1939 /* save adapter pointer in card */
1940 card->adapter = adapter;
1941
1942 ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
1943 "MRVL_PCIE", pdev);
1944 if (ret) {
1945 pr_err("request_irq failed: ret=%d\n", ret);
1946 adapter->card = NULL;
1947 return -1;
1948 }
1949
1950 adapter->dev = &pdev->dev;
1951 strcpy(adapter->fw_name, PCIE8766_DEFAULT_FW_NAME);
1952
1953 return 0;
1954}
1955
1956/*
1957 * This function unregisters the PCIE device.
1958 *
1959 * The PCIE IRQ is released, the function is disabled and driver
1960 * data is set to null.
1961 */
1962static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1963{
1964 struct pcie_service_card *card = adapter->card;
1965
1966 if (card) {
1967 dev_dbg(adapter->dev, "%s(): calling free_irq()\n", __func__);
1968 free_irq(card->dev->irq, card->dev);
fc331460
AP
1969
1970 mwifiex_pcie_delete_sleep_cookie_buf(adapter);
1971 mwifiex_pcie_delete_cmdrsp_buf(adapter);
1972 mwifiex_pcie_delete_evtbd_ring(adapter);
1973 mwifiex_pcie_delete_rxbd_ring(adapter);
1974 mwifiex_pcie_delete_txbd_ring(adapter);
1975 card->cmdrsp_buf = NULL;
d930faee
AK
1976 }
1977}
1978
1979static struct mwifiex_if_ops pcie_ops = {
1980 .init_if = mwifiex_pcie_init,
1981 .cleanup_if = mwifiex_pcie_cleanup,
1982 .check_fw_status = mwifiex_check_fw_status,
1983 .prog_fw = mwifiex_prog_fw_w_helper,
1984 .register_dev = mwifiex_register_dev,
1985 .unregister_dev = mwifiex_unregister_dev,
1986 .enable_int = mwifiex_pcie_enable_host_int,
1987 .process_int_status = mwifiex_process_int_status,
1988 .host_to_card = mwifiex_pcie_host_to_card,
1989 .wakeup = mwifiex_pm_wakeup_card,
1990 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
1991
1992 /* PCIE specific */
1993 .cmdrsp_complete = mwifiex_pcie_cmdrsp_complete,
1994 .event_complete = mwifiex_pcie_event_complete,
1995 .update_mp_end_port = NULL,
1996 .cleanup_mpa_buf = NULL,
c6d1d87a 1997 .init_fw_port = mwifiex_pcie_init_fw_port,
d930faee
AK
1998};
1999
2000/*
2001 * This function initializes the PCIE driver module.
2002 *
2003 * This initiates the semaphore and registers the device with
2004 * PCIE bus.
2005 */
2006static int mwifiex_pcie_init_module(void)
2007{
2008 int ret;
2009
2010 pr_debug("Marvell 8766 PCIe Driver\n");
2011
2012 sema_init(&add_remove_card_sem, 1);
2013
2014 /* Clear the flag in case user removes the card. */
2015 user_rmmod = 0;
2016
2017 ret = pci_register_driver(&mwifiex_pcie);
2018 if (ret)
2019 pr_err("Driver register failed!\n");
2020 else
2021 pr_debug("info: Driver registered successfully!\n");
2022
2023 return ret;
2024}
2025
2026/*
2027 * This function cleans up the PCIE driver.
2028 *
2029 * The following major steps are followed for cleanup -
2030 * - Resume the device if its suspended
2031 * - Disconnect the device if connected
2032 * - Shutdown the firmware
2033 * - Unregister the device from PCIE bus.
2034 */
2035static void mwifiex_pcie_cleanup_module(void)
2036{
2037 if (!down_interruptible(&add_remove_card_sem))
2038 up(&add_remove_card_sem);
2039
2040 /* Set the flag as user is removing this module. */
2041 user_rmmod = 1;
2042
2043 pci_unregister_driver(&mwifiex_pcie);
2044}
2045
2046module_init(mwifiex_pcie_init_module);
2047module_exit(mwifiex_pcie_cleanup_module);
2048
2049MODULE_AUTHOR("Marvell International Ltd.");
2050MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
2051MODULE_VERSION(PCIE_VERSION);
2052MODULE_LICENSE("GPL v2");
2053MODULE_FIRMWARE("mrvl/pcie8766_uapsta.bin");
This page took 0.208513 seconds and 5 git commands to generate.