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