blob: 492655c048d1c44ee069e27508bb5e1fd9db6efb [file] [log] [blame]
Amitkumar Karward930fae2011-10-11 17:41:21 -07001/*
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
Avinash Patilfc331462013-01-03 21:21:30 -080042static int
43mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb,
44 int size, int flags)
Amitkumar Karward930fae2011-10-11 17:41:21 -070045{
Avinash Patilfc331462013-01-03 21:21:30 -080046 struct pcie_service_card *card = adapter->card;
47 dma_addr_t buf_pa;
Amitkumar Karward930fae2011-10-11 17:41:21 -070048
Avinash Patilfc331462013-01-03 21:21:30 -080049 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;
Amitkumar Karward930fae2011-10-11 17:41:21 -070056}
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;
Avinash Patil52301a82013-02-12 14:38:32 -080065 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
66
67 if (!reg->sleep_cookie)
68 return true;
Amitkumar Karward930fae2011-10-11 17:41:21 -070069
Avinash Patilfc331462013-01-03 21:21:30 -080070 if (card->sleep_cookie_vbase) {
71 cookie_addr = (u32 *)card->sleep_cookie_vbase;
Amitkumar Karward930fae2011-10-11 17:41:21 -070072 dev_dbg(adapter->dev, "info: ACCESS_HW: sleep cookie=0x%x\n",
73 *cookie_addr);
74 if (*cookie_addr == FW_AWAKE_COOKIE)
75 return true;
76 }
77
78 return false;
79}
80
81/*
82 * This function probes an mwifiex device and registers it. It allocates
83 * the card structure, enables PCIE function number and initiates the
84 * device registration and initialization procedure by adding a logical
85 * interface.
86 */
87static int mwifiex_pcie_probe(struct pci_dev *pdev,
88 const struct pci_device_id *ent)
89{
90 struct pcie_service_card *card;
91
92 pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -070093 pdev->vendor, pdev->device, pdev->revision);
Amitkumar Karward930fae2011-10-11 17:41:21 -070094
95 card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +000096 if (!card)
Amitkumar Karward930fae2011-10-11 17:41:21 -070097 return -ENOMEM;
Amitkumar Karward930fae2011-10-11 17:41:21 -070098
99 card->dev = pdev;
100
Avinash Patildd04e6a2013-02-08 18:18:06 -0800101 if (ent->driver_data) {
102 struct mwifiex_pcie_device *data = (void *)ent->driver_data;
103 card->pcie.firmware = data->firmware;
104 card->pcie.reg = data->reg;
105 card->pcie.blksz_fw_dl = data->blksz_fw_dl;
106 }
107
Amitkumar Karward930fae2011-10-11 17:41:21 -0700108 if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops,
109 MWIFIEX_PCIE)) {
110 pr_err("%s failed\n", __func__);
111 kfree(card);
112 return -1;
113 }
114
115 return 0;
116}
117
118/*
119 * This function removes the interface and frees up the card structure.
120 */
121static void mwifiex_pcie_remove(struct pci_dev *pdev)
122{
123 struct pcie_service_card *card;
124 struct mwifiex_adapter *adapter;
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700125 struct mwifiex_private *priv;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700126 int i;
127
128 card = pci_get_drvdata(pdev);
129 if (!card)
130 return;
131
132 adapter = card->adapter;
133 if (!adapter || !adapter->priv_num)
134 return;
135
Amitkumar Karwar59a4cc22012-04-09 20:06:57 -0700136 /* In case driver is removed when asynchronous FW load is in progress */
137 wait_for_completion(&adapter->fw_load);
138
Amitkumar Karward930fae2011-10-11 17:41:21 -0700139 if (user_rmmod) {
140#ifdef CONFIG_PM
141 if (adapter->is_suspended)
142 mwifiex_pcie_resume(pdev);
143#endif
144
145 for (i = 0; i < adapter->priv_num; i++)
146 if ((GET_BSS_ROLE(adapter->priv[i]) ==
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700147 MWIFIEX_BSS_ROLE_STA) &&
148 adapter->priv[i]->media_connected)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700149 mwifiex_deauthenticate(adapter->priv[i], NULL);
150
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700151 priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700152
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700153 mwifiex_disable_auto_ds(priv);
154
155 mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700156 }
157
158 mwifiex_remove_card(card->adapter, &add_remove_card_sem);
159 kfree(card);
160}
161
162/*
163 * Kernel needs to suspend all functions separately. Therefore all
164 * registered functions must have drivers with suspend and resume
165 * methods. Failing that the kernel simply removes the whole card.
166 *
167 * If already not suspended, this function allocates and sends a host
168 * sleep activate request to the firmware and turns off the traffic.
169 */
170static int mwifiex_pcie_suspend(struct pci_dev *pdev, pm_message_t state)
171{
172 struct mwifiex_adapter *adapter;
173 struct pcie_service_card *card;
174 int hs_actived, i;
175
176 if (pdev) {
177 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
Avinash Patil83f0c6d2013-01-21 21:04:10 -0800178 if (!card || !card->adapter) {
Amitkumar Karward930fae2011-10-11 17:41:21 -0700179 pr_err("Card or adapter structure is not valid\n");
180 return 0;
181 }
182 } else {
183 pr_err("PCIE device is not specified\n");
184 return 0;
185 }
186
187 adapter = card->adapter;
188
189 hs_actived = mwifiex_enable_hs(adapter);
190
191 /* Indicate device suspended */
192 adapter->is_suspended = true;
193
194 for (i = 0; i < adapter->priv_num; i++)
195 netif_carrier_off(adapter->priv[i]->netdev);
196
197 return 0;
198}
199
200/*
201 * Kernel needs to suspend all functions separately. Therefore all
202 * registered functions must have drivers with suspend and resume
203 * methods. Failing that the kernel simply removes the whole card.
204 *
205 * If already not resumed, this function turns on the traffic and
206 * sends a host sleep cancel request to the firmware.
207 */
208static int mwifiex_pcie_resume(struct pci_dev *pdev)
209{
210 struct mwifiex_adapter *adapter;
211 struct pcie_service_card *card;
212 int i;
213
214 if (pdev) {
215 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
216 if (!card || !card->adapter) {
217 pr_err("Card or adapter structure is not valid\n");
218 return 0;
219 }
220 } else {
221 pr_err("PCIE device is not specified\n");
222 return 0;
223 }
224
225 adapter = card->adapter;
226
227 if (!adapter->is_suspended) {
228 dev_warn(adapter->dev, "Device already resumed\n");
229 return 0;
230 }
231
232 adapter->is_suspended = false;
233
234 for (i = 0; i < adapter->priv_num; i++)
235 if (adapter->priv[i]->media_connected)
236 netif_carrier_on(adapter->priv[i]->netdev);
237
238 mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700239 MWIFIEX_ASYNC_CMD);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700240
241 return 0;
242}
243
Amitkumar Karward930fae2011-10-11 17:41:21 -0700244static DEFINE_PCI_DEVICE_TABLE(mwifiex_ids) = {
245 {
246 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
247 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
Avinash Patildd04e6a2013-02-08 18:18:06 -0800248 .driver_data = (unsigned long) &mwifiex_pcie8766,
Amitkumar Karward930fae2011-10-11 17:41:21 -0700249 },
Avinash Patilca8f2112013-02-08 18:18:09 -0800250 {
251 PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8897,
252 PCI_ANY_ID, PCI_ANY_ID, 0, 0,
253 .driver_data = (unsigned long) &mwifiex_pcie8897,
254 },
Amitkumar Karward930fae2011-10-11 17:41:21 -0700255 {},
256};
257
258MODULE_DEVICE_TABLE(pci, mwifiex_ids);
259
260/* PCI Device Driver */
261static struct pci_driver __refdata mwifiex_pcie = {
262 .name = "mwifiex_pcie",
263 .id_table = mwifiex_ids,
264 .probe = mwifiex_pcie_probe,
265 .remove = mwifiex_pcie_remove,
266#ifdef CONFIG_PM
267 /* Power Management Hooks */
268 .suspend = mwifiex_pcie_suspend,
269 .resume = mwifiex_pcie_resume,
270#endif
271};
272
273/*
274 * This function writes data into PCIE card register.
275 */
276static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
277{
278 struct pcie_service_card *card = adapter->card;
279
280 iowrite32(data, card->pci_mmap1 + reg);
281
282 return 0;
283}
284
285/*
286 * This function reads data from PCIE card register.
287 */
288static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
289{
290 struct pcie_service_card *card = adapter->card;
291
292 *data = ioread32(card->pci_mmap1 + reg);
293
294 return 0;
295}
296
297/*
298 * This function wakes up the card.
299 *
300 * A host power up command is written to the card configuration
301 * register to wake up the card.
302 */
303static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
304{
305 int i = 0;
Avinash Patil52301a82013-02-12 14:38:32 -0800306 struct pcie_service_card *card = adapter->card;
307 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700308
Avinash Patil52301a82013-02-12 14:38:32 -0800309 while (reg->sleep_cookie && mwifiex_pcie_ok_to_access_hw(adapter)) {
Amitkumar Karward930fae2011-10-11 17:41:21 -0700310 i++;
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -0700311 usleep_range(10, 20);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700312 /* 50ms max wait */
313 if (i == 50000)
314 break;
315 }
316
317 dev_dbg(adapter->dev, "event: Wakeup device...\n");
318
319 /* Enable interrupts or any chip access will wakeup device */
320 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK, HOST_INTR_MASK)) {
321 dev_warn(adapter->dev, "Enable host interrupt failed\n");
322 return -1;
323 }
324
325 dev_dbg(adapter->dev, "PCIE wakeup: Setting PS_STATE_AWAKE\n");
326 adapter->ps_state = PS_STATE_AWAKE;
327
328 return 0;
329}
330
331/*
332 * This function is called after the card has woken up.
333 *
334 * The card configuration register is reset.
335 */
336static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
337{
338 dev_dbg(adapter->dev, "cmd: Wakeup device completed\n");
339
340 return 0;
341}
342
343/*
344 * This function disables the host interrupt.
345 *
346 * The host interrupt mask is read, the disable bit is reset and
347 * written back to the card host interrupt mask register.
348 */
349static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
350{
351 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
352 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
353 0x00000000)) {
354 dev_warn(adapter->dev, "Disable host interrupt failed\n");
355 return -1;
356 }
357 }
358
359 return 0;
360}
361
362/*
363 * This function enables the host interrupt.
364 *
365 * The host interrupt enable mask is written to the card
366 * host interrupt mask register.
367 */
368static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
369{
370 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
371 /* Simply write the mask to the register */
372 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
373 HOST_INTR_MASK)) {
374 dev_warn(adapter->dev, "Enable host interrupt failed\n");
375 return -1;
376 }
377 }
378
379 return 0;
380}
381
382/*
Avinash Patil07324842013-02-08 18:18:07 -0800383 * This function initializes TX buffer ring descriptors
384 */
385static int mwifiex_init_txq_ring(struct mwifiex_adapter *adapter)
386{
387 struct pcie_service_card *card = adapter->card;
Avinash Patilca8f2112013-02-08 18:18:09 -0800388 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Avinash Patil07324842013-02-08 18:18:07 -0800389 struct mwifiex_pcie_buf_desc *desc;
Avinash Patilca8f2112013-02-08 18:18:09 -0800390 struct mwifiex_pfu_buf_desc *desc2;
Avinash Patil07324842013-02-08 18:18:07 -0800391 int i;
392
393 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
394 card->tx_buf_list[i] = NULL;
Avinash Patilca8f2112013-02-08 18:18:09 -0800395 if (reg->pfu_enabled) {
396 card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
397 (sizeof(*desc2) * i);
398 desc2 = card->txbd_ring[i];
399 memset(desc2, 0, sizeof(*desc2));
400 } else {
401 card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
402 (sizeof(*desc) * i);
403 desc = card->txbd_ring[i];
404 memset(desc, 0, sizeof(*desc));
405 }
Avinash Patil07324842013-02-08 18:18:07 -0800406 }
407
408 return 0;
409}
410
411/* This function initializes RX buffer ring descriptors. Each SKB is allocated
412 * here and after mapping PCI memory, its physical address is assigned to
413 * PCIE Rx buffer descriptor's physical address.
414 */
415static int mwifiex_init_rxq_ring(struct mwifiex_adapter *adapter)
416{
417 struct pcie_service_card *card = adapter->card;
Avinash Patilca8f2112013-02-08 18:18:09 -0800418 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Avinash Patil07324842013-02-08 18:18:07 -0800419 struct sk_buff *skb;
420 struct mwifiex_pcie_buf_desc *desc;
Avinash Patilca8f2112013-02-08 18:18:09 -0800421 struct mwifiex_pfu_buf_desc *desc2;
Avinash Patil07324842013-02-08 18:18:07 -0800422 dma_addr_t buf_pa;
423 int i;
424
425 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
426 /* Allocate skb here so that firmware can DMA data from it */
427 skb = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
428 if (!skb) {
429 dev_err(adapter->dev,
430 "Unable to allocate skb for RX ring.\n");
431 kfree(card->rxbd_ring_vbase);
432 return -ENOMEM;
433 }
434
435 if (mwifiex_map_pci_memory(adapter, skb,
436 MWIFIEX_RX_DATA_BUF_SIZE,
437 PCI_DMA_FROMDEVICE))
438 return -1;
439
440 MWIFIEX_SKB_PACB(skb, &buf_pa);
441
442 dev_dbg(adapter->dev,
443 "info: RX ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
444 skb, skb->len, skb->data, (u32)buf_pa,
445 (u32)((u64)buf_pa >> 32));
446
447 card->rx_buf_list[i] = skb;
Avinash Patilca8f2112013-02-08 18:18:09 -0800448 if (reg->pfu_enabled) {
449 card->rxbd_ring[i] = (void *)card->rxbd_ring_vbase +
450 (sizeof(*desc2) * i);
451 desc2 = card->rxbd_ring[i];
452 desc2->paddr = buf_pa;
453 desc2->len = (u16)skb->len;
454 desc2->frag_len = (u16)skb->len;
455 desc2->flags = reg->ring_flag_eop | reg->ring_flag_sop;
456 desc2->offset = 0;
457 } else {
458 card->rxbd_ring[i] = (void *)(card->rxbd_ring_vbase +
459 (sizeof(*desc) * i));
460 desc = card->rxbd_ring[i];
461 desc->paddr = buf_pa;
462 desc->len = (u16)skb->len;
463 desc->flags = 0;
464 }
Avinash Patil07324842013-02-08 18:18:07 -0800465 }
466
467 return 0;
468}
469
470/* This function initializes event buffer ring descriptors. Each SKB is
471 * allocated here and after mapping PCI memory, its physical address is assigned
472 * to PCIE Rx buffer descriptor's physical address
473 */
474static int mwifiex_pcie_init_evt_ring(struct mwifiex_adapter *adapter)
475{
476 struct pcie_service_card *card = adapter->card;
Avinash Patile05dc3e2013-02-08 18:18:08 -0800477 struct mwifiex_evt_buf_desc *desc;
Avinash Patil07324842013-02-08 18:18:07 -0800478 struct sk_buff *skb;
479 dma_addr_t buf_pa;
480 int i;
481
482 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
483 /* Allocate skb here so that firmware can DMA data from it */
484 skb = dev_alloc_skb(MAX_EVENT_SIZE);
485 if (!skb) {
486 dev_err(adapter->dev,
487 "Unable to allocate skb for EVENT buf.\n");
488 kfree(card->evtbd_ring_vbase);
489 return -ENOMEM;
490 }
491 skb_put(skb, MAX_EVENT_SIZE);
492
493 if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE,
494 PCI_DMA_FROMDEVICE))
495 return -1;
496
497 MWIFIEX_SKB_PACB(skb, &buf_pa);
498
499 dev_dbg(adapter->dev,
500 "info: EVT ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
501 skb, skb->len, skb->data, (u32)buf_pa,
502 (u32)((u64)buf_pa >> 32));
503
504 card->evt_buf_list[i] = skb;
505 card->evtbd_ring[i] = (void *)(card->evtbd_ring_vbase +
506 (sizeof(*desc) * i));
Avinash Patil07324842013-02-08 18:18:07 -0800507 desc = card->evtbd_ring[i];
508 desc->paddr = buf_pa;
509 desc->len = (u16)skb->len;
510 desc->flags = 0;
511 }
512
513 return 0;
514}
515
516/* This function cleans up TX buffer rings. If any of the buffer list has valid
517 * SKB address, associated SKB is freed.
518 */
519static void mwifiex_cleanup_txq_ring(struct mwifiex_adapter *adapter)
520{
521 struct pcie_service_card *card = adapter->card;
Avinash Patilca8f2112013-02-08 18:18:09 -0800522 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Avinash Patil07324842013-02-08 18:18:07 -0800523 struct sk_buff *skb;
524 struct mwifiex_pcie_buf_desc *desc;
Avinash Patilca8f2112013-02-08 18:18:09 -0800525 struct mwifiex_pfu_buf_desc *desc2;
Avinash Patil07324842013-02-08 18:18:07 -0800526 int i;
527
528 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
Avinash Patilca8f2112013-02-08 18:18:09 -0800529 if (reg->pfu_enabled) {
530 desc2 = card->txbd_ring[i];
531 if (card->tx_buf_list[i]) {
532 skb = card->tx_buf_list[i];
533 pci_unmap_single(card->dev, desc2->paddr,
534 skb->len, PCI_DMA_TODEVICE);
535 dev_kfree_skb_any(skb);
536 }
537 memset(desc2, 0, sizeof(*desc2));
538 } else {
539 desc = card->txbd_ring[i];
540 if (card->tx_buf_list[i]) {
541 skb = card->tx_buf_list[i];
542 pci_unmap_single(card->dev, desc->paddr,
543 skb->len, PCI_DMA_TODEVICE);
544 dev_kfree_skb_any(skb);
545 }
546 memset(desc, 0, sizeof(*desc));
Avinash Patil07324842013-02-08 18:18:07 -0800547 }
548 card->tx_buf_list[i] = NULL;
Avinash Patil07324842013-02-08 18:18:07 -0800549 }
550
551 return;
552}
553
554/* This function cleans up RX buffer rings. If any of the buffer list has valid
555 * SKB address, associated SKB is freed.
556 */
557static void mwifiex_cleanup_rxq_ring(struct mwifiex_adapter *adapter)
558{
559 struct pcie_service_card *card = adapter->card;
Avinash Patilca8f2112013-02-08 18:18:09 -0800560 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Avinash Patil07324842013-02-08 18:18:07 -0800561 struct mwifiex_pcie_buf_desc *desc;
Avinash Patilca8f2112013-02-08 18:18:09 -0800562 struct mwifiex_pfu_buf_desc *desc2;
Avinash Patil07324842013-02-08 18:18:07 -0800563 struct sk_buff *skb;
564 int i;
565
566 for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
Avinash Patilca8f2112013-02-08 18:18:09 -0800567 if (reg->pfu_enabled) {
568 desc2 = card->rxbd_ring[i];
569 if (card->rx_buf_list[i]) {
570 skb = card->rx_buf_list[i];
571 pci_unmap_single(card->dev, desc2->paddr,
572 skb->len, PCI_DMA_TODEVICE);
573 dev_kfree_skb_any(skb);
574 }
575 memset(desc2, 0, sizeof(*desc2));
576 } else {
577 desc = card->rxbd_ring[i];
578 if (card->rx_buf_list[i]) {
579 skb = card->rx_buf_list[i];
580 pci_unmap_single(card->dev, desc->paddr,
581 skb->len, PCI_DMA_TODEVICE);
582 dev_kfree_skb_any(skb);
583 }
584 memset(desc, 0, sizeof(*desc));
Avinash Patil07324842013-02-08 18:18:07 -0800585 }
Avinash Patilca8f2112013-02-08 18:18:09 -0800586 card->rx_buf_list[i] = NULL;
Avinash Patil07324842013-02-08 18:18:07 -0800587 }
588
589 return;
590}
591
592/* This function cleans up event buffer rings. If any of the buffer list has
593 * valid SKB address, associated SKB is freed.
594 */
595static void mwifiex_cleanup_evt_ring(struct mwifiex_adapter *adapter)
596{
597 struct pcie_service_card *card = adapter->card;
Avinash Patile05dc3e2013-02-08 18:18:08 -0800598 struct mwifiex_evt_buf_desc *desc;
Avinash Patil07324842013-02-08 18:18:07 -0800599 struct sk_buff *skb;
600 int i;
601
602 for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
603 desc = card->evtbd_ring[i];
604 if (card->evt_buf_list[i]) {
605 skb = card->evt_buf_list[i];
606 pci_unmap_single(card->dev, desc->paddr, MAX_EVENT_SIZE,
607 PCI_DMA_FROMDEVICE);
608 dev_kfree_skb_any(skb);
609 }
610 card->evt_buf_list[i] = NULL;
611 memset(desc, 0, sizeof(*desc));
612 }
613
614 return;
615}
616
617/* This function creates buffer descriptor ring for TX
Amitkumar Karward930fae2011-10-11 17:41:21 -0700618 */
619static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
620{
621 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800622 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700623
624 /*
625 * driver maintaines the write pointer and firmware maintaines the read
626 * pointer. The write pointer starts at 0 (zero) while the read pointer
627 * starts at zero with rollover bit set
628 */
629 card->txbd_wrptr = 0;
Avinash Patilca8f2112013-02-08 18:18:09 -0800630
631 if (reg->pfu_enabled)
632 card->txbd_rdptr = 0;
633 else
634 card->txbd_rdptr |= reg->tx_rollover_ind;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700635
636 /* allocate shared memory for the BD ring and divide the same in to
637 several descriptors */
Avinash Patilca8f2112013-02-08 18:18:09 -0800638 if (reg->pfu_enabled)
639 card->txbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
640 MWIFIEX_MAX_TXRX_BD;
641 else
642 card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
643 MWIFIEX_MAX_TXRX_BD;
644
Amitkumar Karward930fae2011-10-11 17:41:21 -0700645 dev_dbg(adapter->dev, "info: txbd_ring: Allocating %d bytes\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700646 card->txbd_ring_size);
Avinash Patilfc331462013-01-03 21:21:30 -0800647 card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
648 card->txbd_ring_size,
649 &card->txbd_ring_pbase);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700650 if (!card->txbd_ring_vbase) {
Avinash Patilfc331462013-01-03 21:21:30 -0800651 dev_err(adapter->dev,
652 "allocate consistent memory (%d bytes) failed!\n",
653 card->txbd_ring_size);
Amitkumar Karwar8c53e422011-12-08 20:41:03 -0800654 return -ENOMEM;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700655 }
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700656 dev_dbg(adapter->dev,
657 "info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
Avinash Patilfc331462013-01-03 21:21:30 -0800658 card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700659 (u32)((u64)card->txbd_ring_pbase >> 32), card->txbd_ring_size);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700660
Avinash Patil07324842013-02-08 18:18:07 -0800661 return mwifiex_init_txq_ring(adapter);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700662}
663
664static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
665{
666 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800667 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700668
Avinash Patil07324842013-02-08 18:18:07 -0800669 mwifiex_cleanup_txq_ring(adapter);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700670
Avinash Patilfc331462013-01-03 21:21:30 -0800671 if (card->txbd_ring_vbase)
672 pci_free_consistent(card->dev, card->txbd_ring_size,
673 card->txbd_ring_vbase,
674 card->txbd_ring_pbase);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700675 card->txbd_ring_size = 0;
676 card->txbd_wrptr = 0;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800677 card->txbd_rdptr = 0 | reg->tx_rollover_ind;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700678 card->txbd_ring_vbase = NULL;
Avinash Patilfc331462013-01-03 21:21:30 -0800679 card->txbd_ring_pbase = 0;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700680
681 return 0;
682}
683
684/*
685 * This function creates buffer descriptor ring for RX
686 */
687static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
688{
689 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800690 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700691
692 /*
693 * driver maintaines the read pointer and firmware maintaines the write
694 * pointer. The write pointer starts at 0 (zero) while the read pointer
695 * starts at zero with rollover bit set
696 */
697 card->rxbd_wrptr = 0;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800698 card->rxbd_rdptr = reg->rx_rollover_ind;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700699
Avinash Patilca8f2112013-02-08 18:18:09 -0800700 if (reg->pfu_enabled)
701 card->rxbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
702 MWIFIEX_MAX_TXRX_BD;
703 else
704 card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
705 MWIFIEX_MAX_TXRX_BD;
706
Amitkumar Karward930fae2011-10-11 17:41:21 -0700707 dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700708 card->rxbd_ring_size);
Avinash Patilfc331462013-01-03 21:21:30 -0800709 card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
710 card->rxbd_ring_size,
711 &card->rxbd_ring_pbase);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700712 if (!card->rxbd_ring_vbase) {
Avinash Patilfc331462013-01-03 21:21:30 -0800713 dev_err(adapter->dev,
714 "allocate consistent memory (%d bytes) failed!\n",
715 card->rxbd_ring_size);
Amitkumar Karwar8c53e422011-12-08 20:41:03 -0800716 return -ENOMEM;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700717 }
Amitkumar Karward930fae2011-10-11 17:41:21 -0700718
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700719 dev_dbg(adapter->dev,
720 "info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n",
721 card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase,
722 (u32)((u64)card->rxbd_ring_pbase >> 32),
723 card->rxbd_ring_size);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700724
Avinash Patil07324842013-02-08 18:18:07 -0800725 return mwifiex_init_rxq_ring(adapter);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700726}
727
728/*
729 * This function deletes Buffer descriptor ring for RX
730 */
731static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
732{
733 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800734 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700735
Avinash Patil07324842013-02-08 18:18:07 -0800736 mwifiex_cleanup_rxq_ring(adapter);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700737
Avinash Patilfc331462013-01-03 21:21:30 -0800738 if (card->rxbd_ring_vbase)
739 pci_free_consistent(card->dev, card->rxbd_ring_size,
740 card->rxbd_ring_vbase,
741 card->rxbd_ring_pbase);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700742 card->rxbd_ring_size = 0;
743 card->rxbd_wrptr = 0;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800744 card->rxbd_rdptr = 0 | reg->rx_rollover_ind;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700745 card->rxbd_ring_vbase = NULL;
Avinash Patilfc331462013-01-03 21:21:30 -0800746 card->rxbd_ring_pbase = 0;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700747
748 return 0;
749}
750
751/*
752 * This function creates buffer descriptor ring for Events
753 */
754static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
755{
756 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800757 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700758
759 /*
760 * driver maintaines the read pointer and firmware maintaines the write
761 * pointer. The write pointer starts at 0 (zero) while the read pointer
762 * starts at zero with rollover bit set
763 */
764 card->evtbd_wrptr = 0;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800765 card->evtbd_rdptr = reg->evt_rollover_ind;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700766
Avinash Patile05dc3e2013-02-08 18:18:08 -0800767 card->evtbd_ring_size = sizeof(struct mwifiex_evt_buf_desc) *
Avinash Patilca8f2112013-02-08 18:18:09 -0800768 MWIFIEX_MAX_EVT_BD;
769
Amitkumar Karward930fae2011-10-11 17:41:21 -0700770 dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700771 card->evtbd_ring_size);
Avinash Patilfc331462013-01-03 21:21:30 -0800772 card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
773 card->evtbd_ring_size,
774 &card->evtbd_ring_pbase);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700775 if (!card->evtbd_ring_vbase) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700776 dev_err(adapter->dev,
Avinash Patilfc331462013-01-03 21:21:30 -0800777 "allocate consistent memory (%d bytes) failed!\n",
778 card->evtbd_ring_size);
Amitkumar Karwar8c53e422011-12-08 20:41:03 -0800779 return -ENOMEM;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700780 }
Amitkumar Karward930fae2011-10-11 17:41:21 -0700781
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700782 dev_dbg(adapter->dev,
783 "info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n",
784 card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase,
785 (u32)((u64)card->evtbd_ring_pbase >> 32),
786 card->evtbd_ring_size);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700787
Avinash Patil07324842013-02-08 18:18:07 -0800788 return mwifiex_pcie_init_evt_ring(adapter);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700789}
790
791/*
792 * This function deletes Buffer descriptor ring for Events
793 */
794static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
795{
796 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800797 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700798
Avinash Patil07324842013-02-08 18:18:07 -0800799 mwifiex_cleanup_evt_ring(adapter);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700800
Avinash Patilfc331462013-01-03 21:21:30 -0800801 if (card->evtbd_ring_vbase)
802 pci_free_consistent(card->dev, card->evtbd_ring_size,
803 card->evtbd_ring_vbase,
804 card->evtbd_ring_pbase);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700805 card->evtbd_wrptr = 0;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800806 card->evtbd_rdptr = 0 | reg->evt_rollover_ind;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700807 card->evtbd_ring_size = 0;
808 card->evtbd_ring_vbase = NULL;
Avinash Patilfc331462013-01-03 21:21:30 -0800809 card->evtbd_ring_pbase = 0;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700810
811 return 0;
812}
813
814/*
815 * This function allocates a buffer for CMDRSP
816 */
817static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
818{
819 struct pcie_service_card *card = adapter->card;
820 struct sk_buff *skb;
821
822 /* Allocate memory for receiving command response data */
823 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
824 if (!skb) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700825 dev_err(adapter->dev,
826 "Unable to allocate skb for command response data.\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -0700827 return -ENOMEM;
828 }
Amitkumar Karward930fae2011-10-11 17:41:21 -0700829 skb_put(skb, MWIFIEX_UPLD_SIZE);
Avinash Patilfc331462013-01-03 21:21:30 -0800830 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
831 PCI_DMA_FROMDEVICE))
832 return -1;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700833
Avinash Patilfc331462013-01-03 21:21:30 -0800834 card->cmdrsp_buf = skb;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700835
836 return 0;
837}
838
839/*
840 * This function deletes a buffer for CMDRSP
841 */
842static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
843{
844 struct pcie_service_card *card;
Avinash Patilfc331462013-01-03 21:21:30 -0800845 dma_addr_t buf_pa;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700846
847 if (!adapter)
848 return 0;
849
850 card = adapter->card;
851
Avinash Patilfc331462013-01-03 21:21:30 -0800852 if (card && card->cmdrsp_buf) {
853 MWIFIEX_SKB_PACB(card->cmdrsp_buf, &buf_pa);
854 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
855 PCI_DMA_FROMDEVICE);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700856 dev_kfree_skb_any(card->cmdrsp_buf);
Avinash Patilfc331462013-01-03 21:21:30 -0800857 }
Amitkumar Karward930fae2011-10-11 17:41:21 -0700858
Avinash Patilfc331462013-01-03 21:21:30 -0800859 if (card && card->cmd_buf) {
860 MWIFIEX_SKB_PACB(card->cmd_buf, &buf_pa);
861 pci_unmap_single(card->dev, buf_pa, MWIFIEX_SIZE_OF_CMD_BUFFER,
862 PCI_DMA_TODEVICE);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700863 dev_kfree_skb_any(card->cmd_buf);
Avinash Patilfc331462013-01-03 21:21:30 -0800864 }
Amitkumar Karward930fae2011-10-11 17:41:21 -0700865 return 0;
866}
867
868/*
869 * This function allocates a buffer for sleep cookie
870 */
871static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
872{
Amitkumar Karward930fae2011-10-11 17:41:21 -0700873 struct pcie_service_card *card = adapter->card;
874
Avinash Patilfc331462013-01-03 21:21:30 -0800875 card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32),
876 &card->sleep_cookie_pbase);
877 if (!card->sleep_cookie_vbase) {
878 dev_err(adapter->dev, "pci_alloc_consistent failed!\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -0700879 return -ENOMEM;
880 }
Amitkumar Karward930fae2011-10-11 17:41:21 -0700881 /* Init val of Sleep Cookie */
Avinash Patilfc331462013-01-03 21:21:30 -0800882 *(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700883
884 dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n",
Avinash Patilfc331462013-01-03 21:21:30 -0800885 *((u32 *)card->sleep_cookie_vbase));
Amitkumar Karward930fae2011-10-11 17:41:21 -0700886
887 return 0;
888}
889
890/*
891 * This function deletes buffer for sleep cookie
892 */
893static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
894{
895 struct pcie_service_card *card;
896
897 if (!adapter)
898 return 0;
899
900 card = adapter->card;
901
Avinash Patilfc331462013-01-03 21:21:30 -0800902 if (card && card->sleep_cookie_vbase) {
903 pci_free_consistent(card->dev, sizeof(u32),
904 card->sleep_cookie_vbase,
905 card->sleep_cookie_pbase);
906 card->sleep_cookie_vbase = NULL;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700907 }
908
909 return 0;
910}
911
Avinash Patilfbd7e7a2013-01-03 21:21:31 -0800912/* This function flushes the TX buffer descriptor ring
913 * This function defined as handler is also called while cleaning TXRX
914 * during disconnect/ bss stop.
915 */
916static int mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter *adapter)
917{
918 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800919 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Avinash Patilfbd7e7a2013-01-03 21:21:31 -0800920 u32 rdptr;
921
922 /* Read the TX ring read pointer set by firmware */
Avinash Patildd04e6a2013-02-08 18:18:06 -0800923 if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
Avinash Patilfbd7e7a2013-01-03 21:21:31 -0800924 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -0800925 "Flush TXBD: failed to read reg->tx_rdptr\n");
Avinash Patilfbd7e7a2013-01-03 21:21:31 -0800926 return -1;
927 }
928
929 if (!mwifiex_pcie_txbd_empty(card, rdptr)) {
930 card->txbd_flush = 1;
931 /* write pointer already set at last send
932 * send dnld-rdy intr again, wait for completion.
933 */
934 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
935 CPU_INTR_DNLD_RDY)) {
936 dev_err(adapter->dev,
937 "failed to assert dnld-rdy interrupt.\n");
938 return -1;
939 }
940 }
941 return 0;
942}
943
Amitkumar Karward930fae2011-10-11 17:41:21 -0700944/*
Avinash Patile7f767a2013-01-03 21:21:32 -0800945 * This function unmaps and frees downloaded data buffer
Amitkumar Karward930fae2011-10-11 17:41:21 -0700946 */
Avinash Patile7f767a2013-01-03 21:21:32 -0800947static int mwifiex_pcie_send_data_complete(struct mwifiex_adapter *adapter)
Amitkumar Karward930fae2011-10-11 17:41:21 -0700948{
Avinash Patile7f767a2013-01-03 21:21:32 -0800949 struct sk_buff *skb;
950 dma_addr_t buf_pa;
Avinash Patilca8f2112013-02-08 18:18:09 -0800951 u32 wrdoneidx, rdptr, num_tx_buffs, unmap_count = 0;
Avinash Patile05dc3e2013-02-08 18:18:08 -0800952 struct mwifiex_pcie_buf_desc *desc;
Avinash Patilca8f2112013-02-08 18:18:09 -0800953 struct mwifiex_pfu_buf_desc *desc2;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700954 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -0800955 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -0700956
957 if (!mwifiex_pcie_ok_to_access_hw(adapter))
958 mwifiex_pm_wakeup_card(adapter);
959
960 /* Read the TX ring read pointer set by firmware */
Avinash Patildd04e6a2013-02-08 18:18:06 -0800961 if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -0700962 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -0800963 "SEND COMP: failed to read reg->tx_rdptr\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -0700964 return -1;
965 }
966
Avinash Patile7f767a2013-01-03 21:21:32 -0800967 dev_dbg(adapter->dev, "SEND COMP: rdptr_prev=0x%x, rdptr=0x%x\n",
968 card->txbd_rdptr, rdptr);
Amitkumar Karward930fae2011-10-11 17:41:21 -0700969
Avinash Patilca8f2112013-02-08 18:18:09 -0800970 num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
Avinash Patile7f767a2013-01-03 21:21:32 -0800971 /* free from previous txbd_rdptr to current txbd_rdptr */
Avinash Patildd04e6a2013-02-08 18:18:06 -0800972 while (((card->txbd_rdptr & reg->tx_mask) !=
973 (rdptr & reg->tx_mask)) ||
974 ((card->txbd_rdptr & reg->tx_rollover_ind) !=
975 (rdptr & reg->tx_rollover_ind))) {
Avinash Patilca8f2112013-02-08 18:18:09 -0800976 wrdoneidx = (card->txbd_rdptr & reg->tx_mask) >>
977 reg->tx_start_ptr;
Avinash Patile7f767a2013-01-03 21:21:32 -0800978
979 skb = card->tx_buf_list[wrdoneidx];
980 if (skb) {
981 dev_dbg(adapter->dev,
982 "SEND COMP: Detach skb %p at txbd_rdidx=%d\n",
983 skb, wrdoneidx);
984 MWIFIEX_SKB_PACB(skb, &buf_pa);
985 pci_unmap_single(card->dev, buf_pa, skb->len,
986 PCI_DMA_TODEVICE);
987
988 unmap_count++;
989
990 if (card->txbd_flush)
991 mwifiex_write_data_complete(adapter, skb, 0,
992 -1);
993 else
994 mwifiex_write_data_complete(adapter, skb, 0, 0);
995 }
996
997 card->tx_buf_list[wrdoneidx] = NULL;
Avinash Patilca8f2112013-02-08 18:18:09 -0800998
999 if (reg->pfu_enabled) {
1000 desc2 = (void *)card->txbd_ring[wrdoneidx];
1001 memset(desc2, 0, sizeof(*desc2));
1002 } else {
1003 desc = card->txbd_ring[wrdoneidx];
1004 memset(desc, 0, sizeof(*desc));
1005 }
1006 switch (card->dev->device) {
1007 case PCIE_DEVICE_ID_MARVELL_88W8766P:
1008 card->txbd_rdptr++;
1009 break;
1010 case PCIE_DEVICE_ID_MARVELL_88W8897:
1011 card->txbd_rdptr += reg->ring_tx_start_ptr;
1012 break;
1013 }
1014
Avinash Patile7f767a2013-01-03 21:21:32 -08001015
Avinash Patildd04e6a2013-02-08 18:18:06 -08001016 if ((card->txbd_rdptr & reg->tx_mask) == num_tx_buffs)
Avinash Patile7f767a2013-01-03 21:21:32 -08001017 card->txbd_rdptr = ((card->txbd_rdptr &
Avinash Patildd04e6a2013-02-08 18:18:06 -08001018 reg->tx_rollover_ind) ^
1019 reg->tx_rollover_ind);
Avinash Patile7f767a2013-01-03 21:21:32 -08001020 }
1021
1022 if (unmap_count)
1023 adapter->data_sent = false;
1024
1025 if (card->txbd_flush) {
Avinash Patildd04e6a2013-02-08 18:18:06 -08001026 if (((card->txbd_wrptr & reg->tx_mask) ==
1027 (card->txbd_rdptr & reg->tx_mask)) &&
1028 ((card->txbd_wrptr & reg->tx_rollover_ind) !=
1029 (card->txbd_rdptr & reg->tx_rollover_ind)))
Avinash Patile7f767a2013-01-03 21:21:32 -08001030 card->txbd_flush = 0;
1031 else
1032 mwifiex_clean_pcie_ring_buf(adapter);
1033 }
1034
1035 return 0;
1036}
1037
1038/* This function sends data buffer to device. First 4 bytes of payload
1039 * are filled with payload length and payload type. Then this payload
1040 * is mapped to PCI device memory. Tx ring pointers are advanced accordingly.
1041 * Download ready interrupt to FW is deffered if Tx ring is not full and
1042 * additional payload can be accomodated.
1043 */
1044static int
1045mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb,
1046 struct mwifiex_tx_param *tx_param)
1047{
1048 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001049 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Avinash Patilca8f2112013-02-08 18:18:09 -08001050 u32 wrindx, num_tx_buffs, rx_val;
Avinash Patile7f767a2013-01-03 21:21:32 -08001051 int ret;
1052 dma_addr_t buf_pa;
Avinash Patile05dc3e2013-02-08 18:18:08 -08001053 struct mwifiex_pcie_buf_desc *desc;
Avinash Patilca8f2112013-02-08 18:18:09 -08001054 struct mwifiex_pfu_buf_desc *desc2;
Avinash Patile7f767a2013-01-03 21:21:32 -08001055 __le16 *tmp;
1056
1057 if (!(skb->data && skb->len)) {
1058 dev_err(adapter->dev, "%s(): invalid parameter <%p, %#x>\n",
1059 __func__, skb->data, skb->len);
1060 return -1;
1061 }
1062
1063 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1064 mwifiex_pm_wakeup_card(adapter);
1065
Avinash Patilca8f2112013-02-08 18:18:09 -08001066 num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
Avinash Patile7f767a2013-01-03 21:21:32 -08001067 dev_dbg(adapter->dev, "info: SEND DATA: <Rd: %#x, Wr: %#x>\n",
1068 card->txbd_rdptr, card->txbd_wrptr);
1069 if (mwifiex_pcie_txbd_not_full(card)) {
Amitkumar Karward930fae2011-10-11 17:41:21 -07001070 u8 *payload;
1071
1072 adapter->data_sent = true;
Avinash Patile7f767a2013-01-03 21:21:32 -08001073 payload = skb->data;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001074 tmp = (__le16 *)&payload[0];
1075 *tmp = cpu_to_le16((u16)skb->len);
1076 tmp = (__le16 *)&payload[2];
1077 *tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
Avinash Patile7f767a2013-01-03 21:21:32 -08001078
1079 if (mwifiex_map_pci_memory(adapter, skb, skb->len ,
1080 PCI_DMA_TODEVICE))
1081 return -1;
1082
Avinash Patilca8f2112013-02-08 18:18:09 -08001083 wrindx = (card->txbd_wrptr & reg->tx_mask) >> reg->tx_start_ptr;
Avinash Patile7f767a2013-01-03 21:21:32 -08001084 MWIFIEX_SKB_PACB(skb, &buf_pa);
1085 card->tx_buf_list[wrindx] = skb;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001086
Avinash Patilca8f2112013-02-08 18:18:09 -08001087 if (reg->pfu_enabled) {
1088 desc2 = (void *)card->txbd_ring[wrindx];
1089 desc2->paddr = buf_pa;
1090 desc2->len = (u16)skb->len;
1091 desc2->frag_len = (u16)skb->len;
1092 desc2->offset = 0;
1093 desc2->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1094 MWIFIEX_BD_FLAG_LAST_DESC;
1095 } else {
1096 desc = card->txbd_ring[wrindx];
1097 desc->paddr = buf_pa;
1098 desc->len = (u16)skb->len;
1099 desc->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1100 MWIFIEX_BD_FLAG_LAST_DESC;
1101 }
1102
1103 switch (card->dev->device) {
1104 case PCIE_DEVICE_ID_MARVELL_88W8766P:
1105 card->txbd_wrptr++;
1106 break;
1107 case PCIE_DEVICE_ID_MARVELL_88W8897:
1108 card->txbd_wrptr += reg->ring_tx_start_ptr;
1109 break;
1110 }
1111
1112 if ((card->txbd_wrptr & reg->tx_mask) == num_tx_buffs)
Amitkumar Karward930fae2011-10-11 17:41:21 -07001113 card->txbd_wrptr = ((card->txbd_wrptr &
Avinash Patildd04e6a2013-02-08 18:18:06 -08001114 reg->tx_rollover_ind) ^
1115 reg->tx_rollover_ind);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001116
Avinash Patilca8f2112013-02-08 18:18:09 -08001117 rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001118 /* Write the TX ring write pointer in to reg->tx_wrptr */
1119 if (mwifiex_write_reg(adapter, reg->tx_wrptr,
Avinash Patilca8f2112013-02-08 18:18:09 -08001120 card->txbd_wrptr | rx_val)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001121 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -08001122 "SEND DATA: failed to write reg->tx_wrptr\n");
Avinash Patile7f767a2013-01-03 21:21:32 -08001123 ret = -1;
1124 goto done_unmap;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001125 }
Avinash Patile7f767a2013-01-03 21:21:32 -08001126 if ((mwifiex_pcie_txbd_not_full(card)) &&
1127 tx_param->next_pkt_len) {
1128 /* have more packets and TxBD still can hold more */
1129 dev_dbg(adapter->dev,
1130 "SEND DATA: delay dnld-rdy interrupt.\n");
1131 adapter->data_sent = false;
1132 } else {
1133 /* Send the TX ready interrupt */
1134 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1135 CPU_INTR_DNLD_RDY)) {
1136 dev_err(adapter->dev,
1137 "SEND DATA: failed to assert dnld-rdy interrupt.\n");
1138 ret = -1;
1139 goto done_unmap;
1140 }
Amitkumar Karward930fae2011-10-11 17:41:21 -07001141 }
1142 dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: "
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001143 "%#x> and sent packet to firmware successfully\n",
Avinash Patile7f767a2013-01-03 21:21:32 -08001144 card->txbd_rdptr, card->txbd_wrptr);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001145 } else {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001146 dev_dbg(adapter->dev,
1147 "info: TX Ring full, can't send packets to fw\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001148 adapter->data_sent = true;
1149 /* Send the TX ready interrupt */
1150 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1151 CPU_INTR_DNLD_RDY))
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001152 dev_err(adapter->dev,
1153 "SEND DATA: failed to assert door-bell intr\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001154 return -EBUSY;
1155 }
1156
Avinash Patile7f767a2013-01-03 21:21:32 -08001157 return -EINPROGRESS;
1158done_unmap:
1159 MWIFIEX_SKB_PACB(skb, &buf_pa);
1160 pci_unmap_single(card->dev, buf_pa, skb->len, PCI_DMA_TODEVICE);
1161 card->tx_buf_list[wrindx] = NULL;
Avinash Patilca8f2112013-02-08 18:18:09 -08001162 if (reg->pfu_enabled)
1163 memset(desc2, 0, sizeof(*desc2));
1164 else
1165 memset(desc, 0, sizeof(*desc));
1166
Avinash Patile7f767a2013-01-03 21:21:32 -08001167 return ret;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001168}
1169
1170/*
1171 * This function handles received buffer ring and
1172 * dispatches packets to upper
1173 */
1174static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
1175{
1176 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001177 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Avinash Patilca8f2112013-02-08 18:18:09 -08001178 u32 wrptr, rd_index, tx_val;
Avinash Patile7f767a2013-01-03 21:21:32 -08001179 dma_addr_t buf_pa;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001180 int ret = 0;
1181 struct sk_buff *skb_tmp = NULL;
Avinash Patile05dc3e2013-02-08 18:18:08 -08001182 struct mwifiex_pcie_buf_desc *desc;
Avinash Patilca8f2112013-02-08 18:18:09 -08001183 struct mwifiex_pfu_buf_desc *desc2;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001184
Avinash Patile7f767a2013-01-03 21:21:32 -08001185 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1186 mwifiex_pm_wakeup_card(adapter);
1187
Amitkumar Karward930fae2011-10-11 17:41:21 -07001188 /* Read the RX ring Write pointer set by firmware */
Avinash Patildd04e6a2013-02-08 18:18:06 -08001189 if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001190 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -08001191 "RECV DATA: failed to read reg->rx_wrptr\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001192 ret = -1;
1193 goto done;
1194 }
Avinash Patile7f767a2013-01-03 21:21:32 -08001195 card->rxbd_wrptr = wrptr;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001196
Avinash Patildd04e6a2013-02-08 18:18:06 -08001197 while (((wrptr & reg->rx_mask) !=
1198 (card->rxbd_rdptr & reg->rx_mask)) ||
1199 ((wrptr & reg->rx_rollover_ind) ==
1200 (card->rxbd_rdptr & reg->rx_rollover_ind))) {
Amitkumar Karward930fae2011-10-11 17:41:21 -07001201 struct sk_buff *skb_data;
1202 u16 rx_len;
Avinash Patile7f767a2013-01-03 21:21:32 -08001203 __le16 pkt_len;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001204
Avinash Patildd04e6a2013-02-08 18:18:06 -08001205 rd_index = card->rxbd_rdptr & reg->rx_mask;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001206 skb_data = card->rx_buf_list[rd_index];
1207
Avinash Patile7f767a2013-01-03 21:21:32 -08001208 MWIFIEX_SKB_PACB(skb_data, &buf_pa);
1209 pci_unmap_single(card->dev, buf_pa, MWIFIEX_RX_DATA_BUF_SIZE,
1210 PCI_DMA_FROMDEVICE);
1211 card->rx_buf_list[rd_index] = NULL;
1212
Amitkumar Karward930fae2011-10-11 17:41:21 -07001213 /* Get data length from interface header -
Avinash Patile7f767a2013-01-03 21:21:32 -08001214 * first 2 bytes for len, next 2 bytes is for type
1215 */
1216 pkt_len = *((__le16 *)skb_data->data);
1217 rx_len = le16_to_cpu(pkt_len);
1218 skb_put(skb_data, rx_len);
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001219 dev_dbg(adapter->dev,
1220 "info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
1221 card->rxbd_rdptr, wrptr, rx_len);
Avinash Patile7f767a2013-01-03 21:21:32 -08001222 skb_pull(skb_data, INTF_HEADER_LEN);
1223 mwifiex_handle_rx_packet(adapter, skb_data);
1224
1225 skb_tmp = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001226 if (!skb_tmp) {
Avinash Patile7f767a2013-01-03 21:21:32 -08001227 dev_err(adapter->dev,
1228 "Unable to allocate skb.\n");
1229 return -ENOMEM;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001230 }
1231
Avinash Patile7f767a2013-01-03 21:21:32 -08001232 if (mwifiex_map_pci_memory(adapter, skb_tmp,
1233 MWIFIEX_RX_DATA_BUF_SIZE,
1234 PCI_DMA_FROMDEVICE))
1235 return -1;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001236
Avinash Patile7f767a2013-01-03 21:21:32 -08001237 MWIFIEX_SKB_PACB(skb_tmp, &buf_pa);
1238
1239 dev_dbg(adapter->dev,
1240 "RECV DATA: Attach new sk_buff %p at rxbd_rdidx=%d\n",
1241 skb_tmp, rd_index);
1242 card->rx_buf_list[rd_index] = skb_tmp;
Avinash Patilca8f2112013-02-08 18:18:09 -08001243
1244 if (reg->pfu_enabled) {
1245 desc2 = (void *)card->rxbd_ring[rd_index];
1246 desc2->paddr = buf_pa;
1247 desc2->len = skb_tmp->len;
1248 desc2->frag_len = skb_tmp->len;
1249 desc2->offset = 0;
1250 desc2->flags = reg->ring_flag_sop | reg->ring_flag_eop;
1251 } else {
1252 desc = card->rxbd_ring[rd_index];
1253 desc->paddr = buf_pa;
1254 desc->len = skb_tmp->len;
1255 desc->flags = 0;
1256 }
Avinash Patile7f767a2013-01-03 21:21:32 -08001257
Avinash Patildd04e6a2013-02-08 18:18:06 -08001258 if ((++card->rxbd_rdptr & reg->rx_mask) ==
Amitkumar Karward930fae2011-10-11 17:41:21 -07001259 MWIFIEX_MAX_TXRX_BD) {
1260 card->rxbd_rdptr = ((card->rxbd_rdptr &
Avinash Patildd04e6a2013-02-08 18:18:06 -08001261 reg->rx_rollover_ind) ^
1262 reg->rx_rollover_ind);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001263 }
1264 dev_dbg(adapter->dev, "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001265 card->rxbd_rdptr, wrptr);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001266
Avinash Patilca8f2112013-02-08 18:18:09 -08001267 tx_val = card->txbd_wrptr & reg->tx_wrap_mask;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001268 /* Write the RX ring read pointer in to reg->rx_rdptr */
1269 if (mwifiex_write_reg(adapter, reg->rx_rdptr,
Avinash Patilca8f2112013-02-08 18:18:09 -08001270 card->rxbd_rdptr | tx_val)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001271 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -08001272 "RECV DATA: failed to write reg->rx_rdptr\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001273 ret = -1;
1274 goto done;
1275 }
1276
1277 /* Read the RX ring Write pointer set by firmware */
Avinash Patildd04e6a2013-02-08 18:18:06 -08001278 if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001279 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -08001280 "RECV DATA: failed to read reg->rx_wrptr\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001281 ret = -1;
1282 goto done;
1283 }
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001284 dev_dbg(adapter->dev,
1285 "info: RECV DATA: Rcvd packet from fw successfully\n");
Avinash Patile7f767a2013-01-03 21:21:32 -08001286 card->rxbd_wrptr = wrptr;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001287 }
1288
1289done:
Amitkumar Karward930fae2011-10-11 17:41:21 -07001290 return ret;
1291}
1292
1293/*
1294 * This function downloads the boot command to device
1295 */
1296static int
1297mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1298{
Avinash Patilfc331462013-01-03 21:21:30 -08001299 dma_addr_t buf_pa;
1300 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001301 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001302
Avinash Patilfc331462013-01-03 21:21:30 -08001303 if (!(skb->data && skb->len)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001304 dev_err(adapter->dev,
Avinash Patilfc331462013-01-03 21:21:30 -08001305 "Invalid parameter in %s <%p. len %d>\n",
1306 __func__, skb->data, skb->len);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001307 return -1;
1308 }
1309
Avinash Patilfc331462013-01-03 21:21:30 -08001310 if (mwifiex_map_pci_memory(adapter, skb, skb->len , PCI_DMA_TODEVICE))
1311 return -1;
1312
1313 MWIFIEX_SKB_PACB(skb, &buf_pa);
1314
Avinash Patildd04e6a2013-02-08 18:18:06 -08001315 /* Write the lower 32bits of the physical address to low command
1316 * address scratch register
1317 */
1318 if (mwifiex_write_reg(adapter, reg->cmd_addr_lo, (u32)buf_pa)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001319 dev_err(adapter->dev,
1320 "%s: failed to write download command to boot code.\n",
1321 __func__);
Avinash Patilfc331462013-01-03 21:21:30 -08001322 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1323 PCI_DMA_TODEVICE);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001324 return -1;
1325 }
1326
Avinash Patildd04e6a2013-02-08 18:18:06 -08001327 /* Write the upper 32bits of the physical address to high command
1328 * address scratch register
1329 */
1330 if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
Avinash Patilfc331462013-01-03 21:21:30 -08001331 (u32)((u64)buf_pa >> 32))) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001332 dev_err(adapter->dev,
1333 "%s: failed to write download command to boot code.\n",
1334 __func__);
Avinash Patilfc331462013-01-03 21:21:30 -08001335 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1336 PCI_DMA_TODEVICE);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001337 return -1;
1338 }
1339
Avinash Patildd04e6a2013-02-08 18:18:06 -08001340 /* Write the command length to cmd_size scratch register */
1341 if (mwifiex_write_reg(adapter, reg->cmd_size, skb->len)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001342 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -08001343 "%s: failed to write command len to cmd_size scratch reg\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001344 __func__);
Avinash Patilfc331462013-01-03 21:21:30 -08001345 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1346 PCI_DMA_TODEVICE);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001347 return -1;
1348 }
1349
1350 /* Ring the door bell */
1351 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1352 CPU_INTR_DOOR_BELL)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001353 dev_err(adapter->dev,
1354 "%s: failed to assert door-bell intr\n", __func__);
Avinash Patilfc331462013-01-03 21:21:30 -08001355 pci_unmap_single(card->dev, buf_pa,
1356 MWIFIEX_UPLD_SIZE, PCI_DMA_TODEVICE);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001357 return -1;
1358 }
1359
1360 return 0;
1361}
1362
Avinash Patilc6d1d872013-01-03 21:21:29 -08001363/* This function init rx port in firmware which in turn enables to receive data
1364 * from device before transmitting any packet.
1365 */
1366static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
1367{
1368 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001369 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Avinash Patilca8f2112013-02-08 18:18:09 -08001370 int tx_wrap = card->txbd_wrptr & reg->tx_wrap_mask;
Avinash Patilc6d1d872013-01-03 21:21:29 -08001371
Avinash Patildd04e6a2013-02-08 18:18:06 -08001372 /* Write the RX ring read pointer in to reg->rx_rdptr */
Avinash Patilca8f2112013-02-08 18:18:09 -08001373 if (mwifiex_write_reg(adapter, reg->rx_rdptr, card->rxbd_rdptr |
1374 tx_wrap)) {
Avinash Patilc6d1d872013-01-03 21:21:29 -08001375 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -08001376 "RECV DATA: failed to write reg->rx_rdptr\n");
Avinash Patilc6d1d872013-01-03 21:21:29 -08001377 return -1;
1378 }
1379 return 0;
1380}
1381
1382/* This function downloads commands to the device
Amitkumar Karward930fae2011-10-11 17:41:21 -07001383 */
1384static int
1385mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1386{
1387 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001388 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001389 int ret = 0;
Avinash Patilfc331462013-01-03 21:21:30 -08001390 dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
1391 u8 *payload = (u8 *)skb->data;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001392
1393 if (!(skb->data && skb->len)) {
1394 dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x>\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001395 __func__, skb->data, skb->len);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001396 return -1;
1397 }
1398
1399 /* Make sure a command response buffer is available */
1400 if (!card->cmdrsp_buf) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001401 dev_err(adapter->dev,
1402 "No response buffer available, send command failed\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001403 return -EBUSY;
1404 }
1405
Avinash Patilfc331462013-01-03 21:21:30 -08001406 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1407 mwifiex_pm_wakeup_card(adapter);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001408
1409 adapter->cmd_sent = true;
Avinash Patilfc331462013-01-03 21:21:30 -08001410
1411 *(__le16 *)&payload[0] = cpu_to_le16((u16)skb->len);
1412 *(__le16 *)&payload[2] = cpu_to_le16(MWIFIEX_TYPE_CMD);
1413
1414 if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1415 return -1;
1416
1417 card->cmd_buf = skb;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001418
1419 /* To send a command, the driver will:
1420 1. Write the 64bit physical address of the data buffer to
Avinash Patildd04e6a2013-02-08 18:18:06 -08001421 cmd response address low + cmd response address high
Amitkumar Karward930fae2011-10-11 17:41:21 -07001422 2. Ring the door bell (i.e. set the door bell interrupt)
1423
1424 In response to door bell interrupt, the firmware will perform
1425 the DMA of the command packet (first header to obtain the total
1426 length and then rest of the command).
1427 */
1428
1429 if (card->cmdrsp_buf) {
Avinash Patilfc331462013-01-03 21:21:30 -08001430 MWIFIEX_SKB_PACB(card->cmdrsp_buf, &cmdrsp_buf_pa);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001431 /* Write the lower 32bits of the cmdrsp buffer physical
1432 address */
Avinash Patildd04e6a2013-02-08 18:18:06 -08001433 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo,
Avinash Patilfc331462013-01-03 21:21:30 -08001434 (u32)cmdrsp_buf_pa)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001435 dev_err(adapter->dev,
1436 "Failed to write download cmd to boot code.\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001437 ret = -1;
1438 goto done;
1439 }
1440 /* Write the upper 32bits of the cmdrsp buffer physical
1441 address */
Avinash Patildd04e6a2013-02-08 18:18:06 -08001442 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi,
Avinash Patilfc331462013-01-03 21:21:30 -08001443 (u32)((u64)cmdrsp_buf_pa >> 32))) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001444 dev_err(adapter->dev,
1445 "Failed to write download cmd to boot code.\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001446 ret = -1;
1447 goto done;
1448 }
1449 }
1450
Avinash Patilfc331462013-01-03 21:21:30 -08001451 MWIFIEX_SKB_PACB(card->cmd_buf, &cmd_buf_pa);
Avinash Patildd04e6a2013-02-08 18:18:06 -08001452 /* Write the lower 32bits of the physical address to reg->cmd_addr_lo */
1453 if (mwifiex_write_reg(adapter, reg->cmd_addr_lo,
1454 (u32)cmd_buf_pa)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001455 dev_err(adapter->dev,
1456 "Failed to write download cmd to boot code.\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001457 ret = -1;
1458 goto done;
1459 }
Avinash Patildd04e6a2013-02-08 18:18:06 -08001460 /* Write the upper 32bits of the physical address to reg->cmd_addr_hi */
1461 if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
Avinash Patilfc331462013-01-03 21:21:30 -08001462 (u32)((u64)cmd_buf_pa >> 32))) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001463 dev_err(adapter->dev,
1464 "Failed to write download cmd to boot code.\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001465 ret = -1;
1466 goto done;
1467 }
1468
Avinash Patildd04e6a2013-02-08 18:18:06 -08001469 /* Write the command length to reg->cmd_size */
1470 if (mwifiex_write_reg(adapter, reg->cmd_size,
1471 card->cmd_buf->len)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001472 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -08001473 "Failed to write cmd len to reg->cmd_size\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001474 ret = -1;
1475 goto done;
1476 }
1477
1478 /* Ring the door bell */
1479 if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1480 CPU_INTR_DOOR_BELL)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001481 dev_err(adapter->dev,
1482 "Failed to assert door-bell intr\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001483 ret = -1;
1484 goto done;
1485 }
1486
1487done:
1488 if (ret)
1489 adapter->cmd_sent = false;
1490
1491 return 0;
1492}
1493
1494/*
1495 * This function handles command complete interrupt
1496 */
1497static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1498{
1499 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001500 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001501 struct sk_buff *skb = card->cmdrsp_buf;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001502 int count = 0;
Avinash Patilfc331462013-01-03 21:21:30 -08001503 u16 rx_len;
1504 __le16 pkt_len;
1505 dma_addr_t buf_pa;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001506
1507 dev_dbg(adapter->dev, "info: Rx CMD Response\n");
1508
Avinash Patilfc331462013-01-03 21:21:30 -08001509 MWIFIEX_SKB_PACB(skb, &buf_pa);
1510 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1511 PCI_DMA_FROMDEVICE);
1512
1513 pkt_len = *((__le16 *)skb->data);
1514 rx_len = le16_to_cpu(pkt_len);
1515 skb_trim(skb, rx_len);
1516 skb_pull(skb, INTF_HEADER_LEN);
1517
Amitkumar Karward930fae2011-10-11 17:41:21 -07001518 if (!adapter->curr_cmd) {
Amitkumar Karward930fae2011-10-11 17:41:21 -07001519 if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001520 mwifiex_process_sleep_confirm_resp(adapter, skb->data,
1521 skb->len);
Avinash Patil52301a82013-02-12 14:38:32 -08001522 while (reg->sleep_cookie && (count++ < 10) &&
1523 mwifiex_pcie_ok_to_access_hw(adapter))
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -07001524 usleep_range(50, 60);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001525 } else {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001526 dev_err(adapter->dev,
1527 "There is no command but got cmdrsp\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001528 }
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001529 memcpy(adapter->upld_buf, skb->data,
1530 min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
Avinash Patilfc331462013-01-03 21:21:30 -08001531 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1532 PCI_DMA_FROMDEVICE))
1533 return -1;
1534
1535 MWIFIEX_SKB_PACB(skb, &buf_pa);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001536 } else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001537 adapter->curr_cmd->resp_skb = skb;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001538 adapter->cmd_resp_received = true;
1539 /* Take the pointer and set it to CMD node and will
1540 return in the response complete callback */
1541 card->cmdrsp_buf = NULL;
1542
1543 /* Clear the cmd-rsp buffer address in scratch registers. This
1544 will prevent firmware from writing to the same response
1545 buffer again. */
Avinash Patildd04e6a2013-02-08 18:18:06 -08001546 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo, 0)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001547 dev_err(adapter->dev,
1548 "cmd_done: failed to clear cmd_rsp_addr_lo\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001549 return -1;
1550 }
1551 /* Write the upper 32bits of the cmdrsp buffer physical
1552 address */
Avinash Patildd04e6a2013-02-08 18:18:06 -08001553 if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi, 0)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001554 dev_err(adapter->dev,
1555 "cmd_done: failed to clear cmd_rsp_addr_hi\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001556 return -1;
1557 }
1558 }
1559
1560 return 0;
1561}
1562
1563/*
1564 * Command Response processing complete handler
1565 */
1566static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1567 struct sk_buff *skb)
1568{
1569 struct pcie_service_card *card = adapter->card;
Avinash Patilfc331462013-01-03 21:21:30 -08001570 dma_addr_t buf_pa;
1571 struct sk_buff *skb_tmp;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001572
1573 if (skb) {
1574 card->cmdrsp_buf = skb;
1575 skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
Avinash Patilfc331462013-01-03 21:21:30 -08001576 if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1577 PCI_DMA_FROMDEVICE))
1578 return -1;
1579 }
1580
1581 skb_tmp = card->cmd_buf;
1582 if (skb_tmp) {
1583 MWIFIEX_SKB_PACB(skb_tmp, &buf_pa);
1584 pci_unmap_single(card->dev, buf_pa, MWIFIEX_UPLD_SIZE,
1585 PCI_DMA_FROMDEVICE);
1586 card->cmd_buf = NULL;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001587 }
1588
1589 return 0;
1590}
1591
1592/*
1593 * This function handles firmware event ready interrupt
1594 */
1595static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1596{
1597 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001598 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001599 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1600 u32 wrptr, event;
Avinash Patilfc331462013-01-03 21:21:30 -08001601 dma_addr_t buf_pa;
Avinash Patile05dc3e2013-02-08 18:18:08 -08001602 struct mwifiex_evt_buf_desc *desc;
Avinash Patilfc331462013-01-03 21:21:30 -08001603
1604 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1605 mwifiex_pm_wakeup_card(adapter);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001606
1607 if (adapter->event_received) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001608 dev_dbg(adapter->dev, "info: Event being processed, "
1609 "do not process this interrupt just yet\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001610 return 0;
1611 }
1612
1613 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1614 dev_dbg(adapter->dev, "info: Invalid read pointer...\n");
1615 return -1;
1616 }
1617
1618 /* Read the event ring write pointer set by firmware */
Avinash Patildd04e6a2013-02-08 18:18:06 -08001619 if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001620 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -08001621 "EventReady: failed to read reg->evt_wrptr\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001622 return -1;
1623 }
1624
1625 dev_dbg(adapter->dev, "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001626 card->evtbd_rdptr, wrptr);
1627 if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
1628 & MWIFIEX_EVTBD_MASK)) ||
Avinash Patildd04e6a2013-02-08 18:18:06 -08001629 ((wrptr & reg->evt_rollover_ind) ==
1630 (card->evtbd_rdptr & reg->evt_rollover_ind))) {
Amitkumar Karward930fae2011-10-11 17:41:21 -07001631 struct sk_buff *skb_cmd;
1632 __le16 data_len = 0;
1633 u16 evt_len;
1634
1635 dev_dbg(adapter->dev, "info: Read Index: %d\n", rdptr);
1636 skb_cmd = card->evt_buf_list[rdptr];
Avinash Patilfc331462013-01-03 21:21:30 -08001637 MWIFIEX_SKB_PACB(skb_cmd, &buf_pa);
1638 pci_unmap_single(card->dev, buf_pa, MAX_EVENT_SIZE,
1639 PCI_DMA_FROMDEVICE);
1640
Amitkumar Karward930fae2011-10-11 17:41:21 -07001641 /* Take the pointer and set it to event pointer in adapter
1642 and will return back after event handling callback */
1643 card->evt_buf_list[rdptr] = NULL;
Avinash Patile05dc3e2013-02-08 18:18:08 -08001644 desc = card->evtbd_ring[rdptr];
1645 memset(desc, 0, sizeof(*desc));
Amitkumar Karward930fae2011-10-11 17:41:21 -07001646
1647 event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
1648 adapter->event_cause = event;
1649 /* The first 4bytes will be the event transfer header
1650 len is 2 bytes followed by type which is 2 bytes */
1651 memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1652 evt_len = le16_to_cpu(data_len);
1653
1654 skb_pull(skb_cmd, INTF_HEADER_LEN);
1655 dev_dbg(adapter->dev, "info: Event length: %d\n", evt_len);
1656
1657 if ((evt_len > 0) && (evt_len < MAX_EVENT_SIZE))
1658 memcpy(adapter->event_body, skb_cmd->data +
1659 MWIFIEX_EVENT_HEADER_LEN, evt_len -
1660 MWIFIEX_EVENT_HEADER_LEN);
1661
1662 adapter->event_received = true;
1663 adapter->event_skb = skb_cmd;
1664
1665 /* Do not update the event read pointer here, wait till the
1666 buffer is released. This is just to make things simpler,
1667 we need to find a better method of managing these buffers.
1668 */
1669 }
1670
1671 return 0;
1672}
1673
1674/*
1675 * Event processing complete handler
1676 */
1677static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1678 struct sk_buff *skb)
1679{
1680 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001681 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001682 int ret = 0;
1683 u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1684 u32 wrptr;
Avinash Patilfc331462013-01-03 21:21:30 -08001685 dma_addr_t buf_pa;
Avinash Patile05dc3e2013-02-08 18:18:08 -08001686 struct mwifiex_evt_buf_desc *desc;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001687
1688 if (!skb)
1689 return 0;
1690
Dan Carpenter1eb54c82011-11-07 19:31:47 -08001691 if (rdptr >= MWIFIEX_MAX_EVT_BD) {
Amitkumar Karward930fae2011-10-11 17:41:21 -07001692 dev_err(adapter->dev, "event_complete: Invalid rdptr 0x%x\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001693 rdptr);
Amitkumar Karwar8c53e422011-12-08 20:41:03 -08001694 return -EINVAL;
Dan Carpenter1eb54c82011-11-07 19:31:47 -08001695 }
Amitkumar Karward930fae2011-10-11 17:41:21 -07001696
1697 /* Read the event ring write pointer set by firmware */
Avinash Patildd04e6a2013-02-08 18:18:06 -08001698 if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001699 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -08001700 "event_complete: failed to read reg->evt_wrptr\n");
Amitkumar Karwar8c53e422011-12-08 20:41:03 -08001701 return -1;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001702 }
1703
1704 if (!card->evt_buf_list[rdptr]) {
1705 skb_push(skb, INTF_HEADER_LEN);
Avinash Patilfc331462013-01-03 21:21:30 -08001706 if (mwifiex_map_pci_memory(adapter, skb,
1707 MAX_EVENT_SIZE,
1708 PCI_DMA_FROMDEVICE))
1709 return -1;
1710 MWIFIEX_SKB_PACB(skb, &buf_pa);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001711 card->evt_buf_list[rdptr] = skb;
Avinash Patilfc331462013-01-03 21:21:30 -08001712 MWIFIEX_SKB_PACB(skb, &buf_pa);
Avinash Patile05dc3e2013-02-08 18:18:08 -08001713 desc = card->evtbd_ring[rdptr];
1714 desc->paddr = buf_pa;
1715 desc->len = (u16)skb->len;
1716 desc->flags = 0;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001717 skb = NULL;
1718 } else {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001719 dev_dbg(adapter->dev,
1720 "info: ERROR: buf still valid at index %d, <%p, %p>\n",
1721 rdptr, card->evt_buf_list[rdptr], skb);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001722 }
1723
1724 if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
1725 card->evtbd_rdptr = ((card->evtbd_rdptr &
Avinash Patildd04e6a2013-02-08 18:18:06 -08001726 reg->evt_rollover_ind) ^
1727 reg->evt_rollover_ind);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001728 }
1729
1730 dev_dbg(adapter->dev, "info: Updated <Rd: 0x%x, Wr: 0x%x>",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001731 card->evtbd_rdptr, wrptr);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001732
Avinash Patildd04e6a2013-02-08 18:18:06 -08001733 /* Write the event ring read pointer in to reg->evt_rdptr */
1734 if (mwifiex_write_reg(adapter, reg->evt_rdptr,
1735 card->evtbd_rdptr)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001736 dev_err(adapter->dev,
Avinash Patildd04e6a2013-02-08 18:18:06 -08001737 "event_complete: failed to read reg->evt_rdptr\n");
Amitkumar Karwar8c53e422011-12-08 20:41:03 -08001738 return -1;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001739 }
1740
Amitkumar Karward930fae2011-10-11 17:41:21 -07001741 dev_dbg(adapter->dev, "info: Check Events Again\n");
1742 ret = mwifiex_pcie_process_event_ready(adapter);
1743
1744 return ret;
1745}
1746
1747/*
1748 * This function downloads the firmware to the card.
1749 *
1750 * Firmware is downloaded to the card in blocks. Every block download
1751 * is tested for CRC errors, and retried a number of times before
1752 * returning failure.
1753 */
1754static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1755 struct mwifiex_fw_image *fw)
1756{
1757 int ret;
1758 u8 *firmware = fw->fw_buf;
1759 u32 firmware_len = fw->fw_len;
1760 u32 offset = 0;
1761 struct sk_buff *skb;
1762 u32 txlen, tx_blocks = 0, tries, len;
1763 u32 block_retry_cnt = 0;
Avinash Patilfc331462013-01-03 21:21:30 -08001764 dma_addr_t buf_pa;
1765 struct pcie_service_card *card = adapter->card;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001766 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001767
1768 if (!firmware || !firmware_len) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001769 dev_err(adapter->dev,
1770 "No firmware image found! Terminating download\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001771 return -1;
1772 }
1773
1774 dev_dbg(adapter->dev, "info: Downloading FW image (%d bytes)\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001775 firmware_len);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001776
1777 if (mwifiex_pcie_disable_host_int(adapter)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001778 dev_err(adapter->dev,
1779 "%s: Disabling interrupts failed.\n", __func__);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001780 return -1;
1781 }
1782
1783 skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
1784 if (!skb) {
1785 ret = -ENOMEM;
1786 goto done;
1787 }
Amitkumar Karward930fae2011-10-11 17:41:21 -07001788
1789 /* Perform firmware data transfer */
1790 do {
1791 u32 ireg_intr = 0;
1792
1793 /* More data? */
1794 if (offset >= firmware_len)
1795 break;
1796
1797 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
Avinash Patildd04e6a2013-02-08 18:18:06 -08001798 ret = mwifiex_read_reg(adapter, reg->cmd_size,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001799 &len);
1800 if (ret) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001801 dev_warn(adapter->dev,
1802 "Failed reading len from boot code\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001803 goto done;
1804 }
1805 if (len)
1806 break;
Yogesh Ashok Poware7891ba2012-03-12 19:35:11 -07001807 usleep_range(10, 20);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001808 }
1809
1810 if (!len) {
1811 break;
1812 } else if (len > MWIFIEX_UPLD_SIZE) {
1813 pr_err("FW download failure @ %d, invalid length %d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001814 offset, len);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001815 ret = -1;
1816 goto done;
1817 }
1818
1819 txlen = len;
1820
1821 if (len & BIT(0)) {
1822 block_retry_cnt++;
1823 if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
1824 pr_err("FW download failure @ %d, over max "
1825 "retry count\n", offset);
1826 ret = -1;
1827 goto done;
1828 }
1829 dev_err(adapter->dev, "FW CRC error indicated by the "
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001830 "helper: len = 0x%04X, txlen = %d\n",
1831 len, txlen);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001832 len &= ~BIT(0);
1833 /* Setting this to 0 to resend from same offset */
1834 txlen = 0;
1835 } else {
1836 block_retry_cnt = 0;
1837 /* Set blocksize to transfer - checking for
1838 last block */
1839 if (firmware_len - offset < txlen)
1840 txlen = firmware_len - offset;
1841
1842 dev_dbg(adapter->dev, ".");
1843
Avinash Patildd04e6a2013-02-08 18:18:06 -08001844 tx_blocks = (txlen + card->pcie.blksz_fw_dl - 1) /
1845 card->pcie.blksz_fw_dl;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001846
1847 /* Copy payload to buffer */
1848 memmove(skb->data, &firmware[offset], txlen);
1849 }
1850
1851 skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
Avinash Patildd04e6a2013-02-08 18:18:06 -08001852 skb_trim(skb, tx_blocks * card->pcie.blksz_fw_dl);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001853
1854 /* Send the boot command to device */
1855 if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001856 dev_err(adapter->dev,
1857 "Failed to send firmware download command\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001858 ret = -1;
1859 goto done;
1860 }
Avinash Patilfc331462013-01-03 21:21:30 -08001861
1862 MWIFIEX_SKB_PACB(skb, &buf_pa);
1863
Amitkumar Karward930fae2011-10-11 17:41:21 -07001864 /* Wait for the command done interrupt */
1865 do {
1866 if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
1867 &ireg_intr)) {
1868 dev_err(adapter->dev, "%s: Failed to read "
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001869 "interrupt status during fw dnld.\n",
1870 __func__);
Avinash Patilfc331462013-01-03 21:21:30 -08001871 pci_unmap_single(card->dev, buf_pa, skb->len,
1872 PCI_DMA_TODEVICE);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001873 ret = -1;
1874 goto done;
1875 }
1876 } while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
1877 CPU_INTR_DOOR_BELL);
Avinash Patilfc331462013-01-03 21:21:30 -08001878
1879 pci_unmap_single(card->dev, buf_pa, skb->len,
1880 PCI_DMA_TODEVICE);
1881
Amitkumar Karward930fae2011-10-11 17:41:21 -07001882 offset += txlen;
1883 } while (true);
1884
1885 dev_dbg(adapter->dev, "info:\nFW download over, size %d bytes\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001886 offset);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001887
1888 ret = 0;
1889
1890done:
1891 dev_kfree_skb_any(skb);
1892 return ret;
1893}
1894
1895/*
1896 * This function checks the firmware status in card.
1897 *
1898 * The winner interface is also determined by this function.
1899 */
1900static int
1901mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
1902{
1903 int ret = 0;
1904 u32 firmware_stat, winner_status;
Avinash Patildd04e6a2013-02-08 18:18:06 -08001905 struct pcie_service_card *card = adapter->card;
1906 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -07001907 u32 tries;
1908
1909 /* Mask spurios interrupts */
1910 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001911 HOST_INTR_MASK)) {
Amitkumar Karward930fae2011-10-11 17:41:21 -07001912 dev_warn(adapter->dev, "Write register failed\n");
1913 return -1;
1914 }
1915
1916 dev_dbg(adapter->dev, "Setting driver ready signature\n");
Avinash Patildd04e6a2013-02-08 18:18:06 -08001917 if (mwifiex_write_reg(adapter, reg->drv_rdy,
1918 FIRMWARE_READY_PCIE)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001919 dev_err(adapter->dev,
1920 "Failed to write driver ready signature\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07001921 return -1;
1922 }
1923
1924 /* Wait for firmware initialization event */
1925 for (tries = 0; tries < poll_num; tries++) {
Avinash Patildd04e6a2013-02-08 18:18:06 -08001926 if (mwifiex_read_reg(adapter, reg->fw_status,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001927 &firmware_stat))
1928 ret = -1;
1929 else
1930 ret = 0;
1931 if (ret)
1932 continue;
1933 if (firmware_stat == FIRMWARE_READY_PCIE) {
1934 ret = 0;
1935 break;
1936 } else {
1937 mdelay(100);
1938 ret = -1;
1939 }
1940 }
1941
1942 if (ret) {
Avinash Patildd04e6a2013-02-08 18:18:06 -08001943 if (mwifiex_read_reg(adapter, reg->fw_status,
Amitkumar Karward930fae2011-10-11 17:41:21 -07001944 &winner_status))
1945 ret = -1;
1946 else if (!winner_status) {
1947 dev_err(adapter->dev, "PCI-E is the winner\n");
1948 adapter->winner = 1;
1949 ret = -1;
1950 } else {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001951 dev_err(adapter->dev,
1952 "PCI-E is not the winner <%#x,%d>, exit dnld\n",
1953 ret, adapter->winner);
Amitkumar Karward930fae2011-10-11 17:41:21 -07001954 ret = 0;
1955 }
1956 }
1957
1958 return ret;
1959}
1960
1961/*
1962 * This function reads the interrupt status from card.
1963 */
1964static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
1965{
1966 u32 pcie_ireg;
1967 unsigned long flags;
1968
1969 if (!mwifiex_pcie_ok_to_access_hw(adapter))
1970 return;
1971
1972 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) {
1973 dev_warn(adapter->dev, "Read register failed\n");
1974 return;
1975 }
1976
1977 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
1978
1979 mwifiex_pcie_disable_host_int(adapter);
1980
1981 /* Clear the pending interrupts */
1982 if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
1983 ~pcie_ireg)) {
1984 dev_warn(adapter->dev, "Write register failed\n");
1985 return;
1986 }
1987 spin_lock_irqsave(&adapter->int_lock, flags);
1988 adapter->int_status |= pcie_ireg;
1989 spin_unlock_irqrestore(&adapter->int_lock, flags);
1990
1991 if (pcie_ireg & HOST_INTR_CMD_DONE) {
1992 if ((adapter->ps_state == PS_STATE_SLEEP_CFM) ||
1993 (adapter->ps_state == PS_STATE_SLEEP)) {
1994 mwifiex_pcie_enable_host_int(adapter);
1995 if (mwifiex_write_reg(adapter,
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07001996 PCIE_CPU_INT_EVENT,
1997 CPU_INTR_SLEEP_CFM_DONE)
1998 ) {
1999 dev_warn(adapter->dev,
2000 "Write register failed\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07002001 return;
2002
2003 }
2004 }
2005 } else if (!adapter->pps_uapsd_mode &&
2006 adapter->ps_state == PS_STATE_SLEEP) {
2007 /* Potentially for PCIe we could get other
2008 * interrupts like shared. Don't change power
2009 * state until cookie is set */
2010 if (mwifiex_pcie_ok_to_access_hw(adapter))
2011 adapter->ps_state = PS_STATE_AWAKE;
2012 }
2013 }
2014}
2015
2016/*
2017 * Interrupt handler for PCIe root port
2018 *
2019 * This function reads the interrupt status from firmware and assigns
2020 * the main process in workqueue which will handle the interrupt.
2021 */
2022static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
2023{
2024 struct pci_dev *pdev = (struct pci_dev *)context;
2025 struct pcie_service_card *card;
2026 struct mwifiex_adapter *adapter;
2027
2028 if (!pdev) {
2029 pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev);
2030 goto exit;
2031 }
2032
2033 card = (struct pcie_service_card *) pci_get_drvdata(pdev);
2034 if (!card || !card->adapter) {
2035 pr_debug("info: %s: card=%p adapter=%p\n", __func__, card,
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07002036 card ? card->adapter : NULL);
Amitkumar Karward930fae2011-10-11 17:41:21 -07002037 goto exit;
2038 }
2039 adapter = card->adapter;
2040
2041 if (adapter->surprise_removed)
2042 goto exit;
2043
2044 mwifiex_interrupt_status(adapter);
2045 queue_work(adapter->workqueue, &adapter->main_work);
2046
2047exit:
2048 return IRQ_HANDLED;
2049}
2050
2051/*
2052 * This function checks the current interrupt status.
2053 *
2054 * The following interrupts are checked and handled by this function -
2055 * - Data sent
2056 * - Command sent
2057 * - Command received
2058 * - Packets received
2059 * - Events received
2060 *
2061 * In case of Rx packets received, the packets are uploaded from card to
2062 * host and processed accordingly.
2063 */
2064static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
2065{
2066 int ret;
Avinash Patil659c4782013-01-03 21:21:28 -08002067 u32 pcie_ireg;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002068 unsigned long flags;
2069
2070 spin_lock_irqsave(&adapter->int_lock, flags);
2071 /* Clear out unused interrupts */
Avinash Patil659c4782013-01-03 21:21:28 -08002072 pcie_ireg = adapter->int_status;
2073 adapter->int_status = 0;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002074 spin_unlock_irqrestore(&adapter->int_lock, flags);
2075
Avinash Patil659c4782013-01-03 21:21:28 -08002076 while (pcie_ireg & HOST_INTR_MASK) {
2077 if (pcie_ireg & HOST_INTR_DNLD_DONE) {
2078 pcie_ireg &= ~HOST_INTR_DNLD_DONE;
Avinash Patile7f767a2013-01-03 21:21:32 -08002079 dev_dbg(adapter->dev, "info: TX DNLD Done\n");
2080 ret = mwifiex_pcie_send_data_complete(adapter);
2081 if (ret)
2082 return ret;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002083 }
Avinash Patil659c4782013-01-03 21:21:28 -08002084 if (pcie_ireg & HOST_INTR_UPLD_RDY) {
2085 pcie_ireg &= ~HOST_INTR_UPLD_RDY;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002086 dev_dbg(adapter->dev, "info: Rx DATA\n");
2087 ret = mwifiex_pcie_process_recv_data(adapter);
2088 if (ret)
2089 return ret;
2090 }
Avinash Patil659c4782013-01-03 21:21:28 -08002091 if (pcie_ireg & HOST_INTR_EVENT_RDY) {
2092 pcie_ireg &= ~HOST_INTR_EVENT_RDY;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002093 dev_dbg(adapter->dev, "info: Rx EVENT\n");
2094 ret = mwifiex_pcie_process_event_ready(adapter);
2095 if (ret)
2096 return ret;
2097 }
2098
Avinash Patil659c4782013-01-03 21:21:28 -08002099 if (pcie_ireg & HOST_INTR_CMD_DONE) {
2100 pcie_ireg &= ~HOST_INTR_CMD_DONE;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002101 if (adapter->cmd_sent) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07002102 dev_dbg(adapter->dev,
2103 "info: CMD sent Interrupt\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07002104 adapter->cmd_sent = false;
2105 }
2106 /* Handle command response */
2107 ret = mwifiex_pcie_process_cmd_complete(adapter);
2108 if (ret)
2109 return ret;
2110 }
2111
2112 if (mwifiex_pcie_ok_to_access_hw(adapter)) {
2113 if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2114 &pcie_ireg)) {
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07002115 dev_warn(adapter->dev,
2116 "Read register failed\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07002117 return -1;
2118 }
2119
2120 if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2121 if (mwifiex_write_reg(adapter,
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07002122 PCIE_HOST_INT_STATUS,
2123 ~pcie_ireg)) {
2124 dev_warn(adapter->dev,
2125 "Write register failed\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07002126 return -1;
2127 }
Amitkumar Karward930fae2011-10-11 17:41:21 -07002128 }
2129
2130 }
2131 }
2132 dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07002133 adapter->cmd_sent, adapter->data_sent);
Amitkumar Karward930fae2011-10-11 17:41:21 -07002134 mwifiex_pcie_enable_host_int(adapter);
2135
2136 return 0;
2137}
2138
2139/*
2140 * This function downloads data from driver to card.
2141 *
2142 * Both commands and data packets are transferred to the card by this
2143 * function.
2144 *
2145 * This function adds the PCIE specific header to the front of the buffer
2146 * before transferring. The header contains the length of the packet and
2147 * the type. The firmware handles the packets based upon this set type.
2148 */
2149static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
2150 struct sk_buff *skb,
2151 struct mwifiex_tx_param *tx_param)
2152{
Dan Carpenterfa161cb2011-11-07 19:31:45 -08002153 if (!skb) {
2154 dev_err(adapter->dev, "Passed NULL skb to %s\n", __func__);
Amitkumar Karward930fae2011-10-11 17:41:21 -07002155 return -1;
2156 }
2157
2158 if (type == MWIFIEX_TYPE_DATA)
Avinash Patile7f767a2013-01-03 21:21:32 -08002159 return mwifiex_pcie_send_data(adapter, skb, tx_param);
Amitkumar Karward930fae2011-10-11 17:41:21 -07002160 else if (type == MWIFIEX_TYPE_CMD)
2161 return mwifiex_pcie_send_cmd(adapter, skb);
2162
2163 return 0;
2164}
2165
2166/*
2167 * This function initializes the PCI-E host memory space, WCB rings, etc.
2168 *
2169 * The following initializations steps are followed -
2170 * - Allocate TXBD ring buffers
2171 * - Allocate RXBD ring buffers
2172 * - Allocate event BD ring buffers
2173 * - Allocate command response ring buffer
2174 * - Allocate sleep cookie buffer
2175 */
2176static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
2177{
2178 struct pcie_service_card *card = adapter->card;
2179 int ret;
2180 struct pci_dev *pdev = card->dev;
Avinash Patil52301a82013-02-12 14:38:32 -08002181 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002182
2183 pci_set_drvdata(pdev, card);
2184
2185 ret = pci_enable_device(pdev);
2186 if (ret)
2187 goto err_enable_dev;
2188
2189 pci_set_master(pdev);
2190
2191 dev_dbg(adapter->dev, "try set_consistent_dma_mask(32)\n");
2192 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2193 if (ret) {
2194 dev_err(adapter->dev, "set_dma_mask(32) failed\n");
2195 goto err_set_dma_mask;
2196 }
2197
2198 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2199 if (ret) {
2200 dev_err(adapter->dev, "set_consistent_dma_mask(64) failed\n");
2201 goto err_set_dma_mask;
2202 }
2203
2204 ret = pci_request_region(pdev, 0, DRV_NAME);
2205 if (ret) {
2206 dev_err(adapter->dev, "req_reg(0) error\n");
2207 goto err_req_region0;
2208 }
2209 card->pci_mmap = pci_iomap(pdev, 0, 0);
2210 if (!card->pci_mmap) {
2211 dev_err(adapter->dev, "iomap(0) error\n");
Alexey Khoroshilov32207122013-01-26 00:56:03 +04002212 ret = -EIO;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002213 goto err_iomap0;
2214 }
2215 ret = pci_request_region(pdev, 2, DRV_NAME);
2216 if (ret) {
2217 dev_err(adapter->dev, "req_reg(2) error\n");
2218 goto err_req_region2;
2219 }
2220 card->pci_mmap1 = pci_iomap(pdev, 2, 0);
2221 if (!card->pci_mmap1) {
2222 dev_err(adapter->dev, "iomap(2) error\n");
Alexey Khoroshilov32207122013-01-26 00:56:03 +04002223 ret = -EIO;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002224 goto err_iomap2;
2225 }
2226
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07002227 dev_dbg(adapter->dev,
2228 "PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
2229 card->pci_mmap, card->pci_mmap1);
Amitkumar Karward930fae2011-10-11 17:41:21 -07002230
2231 card->cmdrsp_buf = NULL;
2232 ret = mwifiex_pcie_create_txbd_ring(adapter);
2233 if (ret)
2234 goto err_cre_txbd;
2235 ret = mwifiex_pcie_create_rxbd_ring(adapter);
2236 if (ret)
2237 goto err_cre_rxbd;
2238 ret = mwifiex_pcie_create_evtbd_ring(adapter);
2239 if (ret)
2240 goto err_cre_evtbd;
2241 ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
2242 if (ret)
2243 goto err_alloc_cmdbuf;
Avinash Patil52301a82013-02-12 14:38:32 -08002244 if (reg->sleep_cookie) {
2245 ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
2246 if (ret)
2247 goto err_alloc_cookie;
2248 } else {
2249 card->sleep_cookie_vbase = NULL;
2250 }
Amitkumar Karward930fae2011-10-11 17:41:21 -07002251 return ret;
2252
2253err_alloc_cookie:
2254 mwifiex_pcie_delete_cmdrsp_buf(adapter);
2255err_alloc_cmdbuf:
2256 mwifiex_pcie_delete_evtbd_ring(adapter);
2257err_cre_evtbd:
2258 mwifiex_pcie_delete_rxbd_ring(adapter);
2259err_cre_rxbd:
2260 mwifiex_pcie_delete_txbd_ring(adapter);
2261err_cre_txbd:
2262 pci_iounmap(pdev, card->pci_mmap1);
2263err_iomap2:
2264 pci_release_region(pdev, 2);
2265err_req_region2:
2266 pci_iounmap(pdev, card->pci_mmap);
2267err_iomap0:
2268 pci_release_region(pdev, 0);
2269err_req_region0:
2270err_set_dma_mask:
2271 pci_disable_device(pdev);
2272err_enable_dev:
2273 pci_set_drvdata(pdev, NULL);
2274 return ret;
2275}
2276
2277/*
2278 * This function cleans up the allocated card buffers.
2279 *
2280 * The following are freed by this function -
2281 * - TXBD ring buffers
2282 * - RXBD ring buffers
2283 * - Event BD ring buffers
2284 * - Command response ring buffer
2285 * - Sleep cookie buffer
2286 */
2287static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
2288{
2289 struct pcie_service_card *card = adapter->card;
2290 struct pci_dev *pdev = card->dev;
Avinash Patildd04e6a2013-02-08 18:18:06 -08002291 const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002292
Amitkumar Karward930fae2011-10-11 17:41:21 -07002293 if (user_rmmod) {
Avinash Patilfc331462013-01-03 21:21:30 -08002294 dev_dbg(adapter->dev, "Clearing driver ready signature\n");
Avinash Patildd04e6a2013-02-08 18:18:06 -08002295 if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
Yogesh Ashok Powarf57c1ed2012-03-13 19:22:37 -07002296 dev_err(adapter->dev,
2297 "Failed to write driver not-ready signature\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07002298 }
2299
2300 if (pdev) {
2301 pci_iounmap(pdev, card->pci_mmap);
2302 pci_iounmap(pdev, card->pci_mmap1);
2303
2304 pci_release_regions(pdev);
2305 pci_disable_device(pdev);
2306 pci_set_drvdata(pdev, NULL);
2307 }
2308}
2309
2310/*
2311 * This function registers the PCIE device.
2312 *
2313 * PCIE IRQ is claimed, block size is set and driver data is initialized.
2314 */
2315static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2316{
2317 int ret;
2318 struct pcie_service_card *card = adapter->card;
2319 struct pci_dev *pdev = card->dev;
2320
2321 /* save adapter pointer in card */
2322 card->adapter = adapter;
2323
2324 ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
2325 "MRVL_PCIE", pdev);
2326 if (ret) {
2327 pr_err("request_irq failed: ret=%d\n", ret);
2328 adapter->card = NULL;
2329 return -1;
2330 }
2331
2332 adapter->dev = &pdev->dev;
Avinash Patildd04e6a2013-02-08 18:18:06 -08002333 strcpy(adapter->fw_name, card->pcie.firmware);
Amitkumar Karward930fae2011-10-11 17:41:21 -07002334
2335 return 0;
2336}
2337
2338/*
2339 * This function unregisters the PCIE device.
2340 *
2341 * The PCIE IRQ is released, the function is disabled and driver
2342 * data is set to null.
2343 */
2344static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2345{
2346 struct pcie_service_card *card = adapter->card;
Avinash Patil52301a82013-02-12 14:38:32 -08002347 const struct mwifiex_pcie_card_reg *reg;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002348
2349 if (card) {
2350 dev_dbg(adapter->dev, "%s(): calling free_irq()\n", __func__);
2351 free_irq(card->dev->irq, card->dev);
Avinash Patilfc331462013-01-03 21:21:30 -08002352
Avinash Patil52301a82013-02-12 14:38:32 -08002353 reg = card->pcie.reg;
2354 if (reg->sleep_cookie)
2355 mwifiex_pcie_delete_sleep_cookie_buf(adapter);
2356
Avinash Patilfc331462013-01-03 21:21:30 -08002357 mwifiex_pcie_delete_cmdrsp_buf(adapter);
2358 mwifiex_pcie_delete_evtbd_ring(adapter);
2359 mwifiex_pcie_delete_rxbd_ring(adapter);
2360 mwifiex_pcie_delete_txbd_ring(adapter);
2361 card->cmdrsp_buf = NULL;
Amitkumar Karward930fae2011-10-11 17:41:21 -07002362 }
2363}
2364
2365static struct mwifiex_if_ops pcie_ops = {
2366 .init_if = mwifiex_pcie_init,
2367 .cleanup_if = mwifiex_pcie_cleanup,
2368 .check_fw_status = mwifiex_check_fw_status,
2369 .prog_fw = mwifiex_prog_fw_w_helper,
2370 .register_dev = mwifiex_register_dev,
2371 .unregister_dev = mwifiex_unregister_dev,
2372 .enable_int = mwifiex_pcie_enable_host_int,
2373 .process_int_status = mwifiex_process_int_status,
2374 .host_to_card = mwifiex_pcie_host_to_card,
2375 .wakeup = mwifiex_pm_wakeup_card,
2376 .wakeup_complete = mwifiex_pm_wakeup_card_complete,
2377
2378 /* PCIE specific */
2379 .cmdrsp_complete = mwifiex_pcie_cmdrsp_complete,
2380 .event_complete = mwifiex_pcie_event_complete,
2381 .update_mp_end_port = NULL,
2382 .cleanup_mpa_buf = NULL,
Avinash Patilc6d1d872013-01-03 21:21:29 -08002383 .init_fw_port = mwifiex_pcie_init_fw_port,
Avinash Patilfbd7e7a2013-01-03 21:21:31 -08002384 .clean_pcie_ring = mwifiex_clean_pcie_ring_buf,
Amitkumar Karward930fae2011-10-11 17:41:21 -07002385};
2386
2387/*
2388 * This function initializes the PCIE driver module.
2389 *
2390 * This initiates the semaphore and registers the device with
2391 * PCIE bus.
2392 */
2393static int mwifiex_pcie_init_module(void)
2394{
2395 int ret;
2396
Avinash Patilca8f2112013-02-08 18:18:09 -08002397 pr_debug("Marvell PCIe Driver\n");
Amitkumar Karward930fae2011-10-11 17:41:21 -07002398
2399 sema_init(&add_remove_card_sem, 1);
2400
2401 /* Clear the flag in case user removes the card. */
2402 user_rmmod = 0;
2403
2404 ret = pci_register_driver(&mwifiex_pcie);
2405 if (ret)
2406 pr_err("Driver register failed!\n");
2407 else
2408 pr_debug("info: Driver registered successfully!\n");
2409
2410 return ret;
2411}
2412
2413/*
2414 * This function cleans up the PCIE driver.
2415 *
2416 * The following major steps are followed for cleanup -
2417 * - Resume the device if its suspended
2418 * - Disconnect the device if connected
2419 * - Shutdown the firmware
2420 * - Unregister the device from PCIE bus.
2421 */
2422static void mwifiex_pcie_cleanup_module(void)
2423{
2424 if (!down_interruptible(&add_remove_card_sem))
2425 up(&add_remove_card_sem);
2426
2427 /* Set the flag as user is removing this module. */
2428 user_rmmod = 1;
2429
2430 pci_unregister_driver(&mwifiex_pcie);
2431}
2432
2433module_init(mwifiex_pcie_init_module);
2434module_exit(mwifiex_pcie_cleanup_module);
2435
2436MODULE_AUTHOR("Marvell International Ltd.");
2437MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
2438MODULE_VERSION(PCIE_VERSION);
2439MODULE_LICENSE("GPL v2");
Avinash Patilca8f2112013-02-08 18:18:09 -08002440MODULE_FIRMWARE(PCIE8766_DEFAULT_FW_NAME);
2441MODULE_FIRMWARE(PCIE8897_DEFAULT_FW_NAME);