• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Marvell Wireless LAN device driver: PCIE specific handling
3  *
4  * Copyright (C) 2011-2014, 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 
34 static u8 user_rmmod;
35 
36 static struct mwifiex_if_ops pcie_ops;
37 
38 static struct semaphore add_remove_card_sem;
39 
40 static struct memory_type_mapping mem_type_mapping_tbl[] = {
41 	{"ITCM", NULL, 0, 0xF0},
42 	{"DTCM", NULL, 0, 0xF1},
43 	{"SQRAM", NULL, 0, 0xF2},
44 	{"IRAM", NULL, 0, 0xF3},
45 	{"APU", NULL, 0, 0xF4},
46 	{"CIU", NULL, 0, 0xF5},
47 	{"ICU", NULL, 0, 0xF6},
48 	{"MAC", NULL, 0, 0xF7},
49 };
50 
51 static int
mwifiex_map_pci_memory(struct mwifiex_adapter * adapter,struct sk_buff * skb,size_t size,int flags)52 mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb,
53 		       size_t size, int flags)
54 {
55 	struct pcie_service_card *card = adapter->card;
56 	struct mwifiex_dma_mapping mapping;
57 
58 	mapping.addr = pci_map_single(card->dev, skb->data, size, flags);
59 	if (pci_dma_mapping_error(card->dev, mapping.addr)) {
60 		dev_err(adapter->dev, "failed to map pci memory!\n");
61 		return -1;
62 	}
63 	mapping.len = size;
64 	mwifiex_store_mapping(skb, &mapping);
65 	return 0;
66 }
67 
mwifiex_unmap_pci_memory(struct mwifiex_adapter * adapter,struct sk_buff * skb,int flags)68 static void mwifiex_unmap_pci_memory(struct mwifiex_adapter *adapter,
69 				     struct sk_buff *skb, int flags)
70 {
71 	struct pcie_service_card *card = adapter->card;
72 	struct mwifiex_dma_mapping mapping;
73 
74 	mwifiex_get_mapping(skb, &mapping);
75 	pci_unmap_single(card->dev, mapping.addr, mapping.len, flags);
76 }
77 
78 /*
79  * This function reads sleep cookie and checks if FW is ready
80  */
mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter * adapter)81 static bool mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter *adapter)
82 {
83 	u32 *cookie_addr;
84 	struct pcie_service_card *card = adapter->card;
85 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
86 
87 	if (!reg->sleep_cookie)
88 		return true;
89 
90 	if (card->sleep_cookie_vbase) {
91 		cookie_addr = (u32 *)card->sleep_cookie_vbase;
92 		dev_dbg(adapter->dev, "info: ACCESS_HW: sleep cookie=0x%x\n",
93 			*cookie_addr);
94 		if (*cookie_addr == FW_AWAKE_COOKIE)
95 			return true;
96 	}
97 
98 	return false;
99 }
100 
101 #ifdef CONFIG_PM_SLEEP
102 /*
103  * Kernel needs to suspend all functions separately. Therefore all
104  * registered functions must have drivers with suspend and resume
105  * methods. Failing that the kernel simply removes the whole card.
106  *
107  * If already not suspended, this function allocates and sends a host
108  * sleep activate request to the firmware and turns off the traffic.
109  */
mwifiex_pcie_suspend(struct device * dev)110 static int mwifiex_pcie_suspend(struct device *dev)
111 {
112 	struct mwifiex_adapter *adapter;
113 	struct pcie_service_card *card;
114 	int hs_actived;
115 	struct pci_dev *pdev = to_pci_dev(dev);
116 
117 	if (pdev) {
118 		card = pci_get_drvdata(pdev);
119 		if (!card || !card->adapter) {
120 			pr_err("Card or adapter structure is not valid\n");
121 			return 0;
122 		}
123 	} else {
124 		pr_err("PCIE device is not specified\n");
125 		return 0;
126 	}
127 
128 	adapter = card->adapter;
129 
130 	hs_actived = mwifiex_enable_hs(adapter);
131 
132 	/* Indicate device suspended */
133 	adapter->is_suspended = true;
134 	adapter->hs_enabling = false;
135 
136 	return 0;
137 }
138 
139 /*
140  * Kernel needs to suspend all functions separately. Therefore all
141  * registered functions must have drivers with suspend and resume
142  * methods. Failing that the kernel simply removes the whole card.
143  *
144  * If already not resumed, this function turns on the traffic and
145  * sends a host sleep cancel request to the firmware.
146  */
mwifiex_pcie_resume(struct device * dev)147 static int mwifiex_pcie_resume(struct device *dev)
148 {
149 	struct mwifiex_adapter *adapter;
150 	struct pcie_service_card *card;
151 	struct pci_dev *pdev = to_pci_dev(dev);
152 
153 	if (pdev) {
154 		card = pci_get_drvdata(pdev);
155 		if (!card || !card->adapter) {
156 			pr_err("Card or adapter structure is not valid\n");
157 			return 0;
158 		}
159 	} else {
160 		pr_err("PCIE device is not specified\n");
161 		return 0;
162 	}
163 
164 	adapter = card->adapter;
165 
166 	if (!adapter->is_suspended) {
167 		dev_warn(adapter->dev, "Device already resumed\n");
168 		return 0;
169 	}
170 
171 	adapter->is_suspended = false;
172 
173 	mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
174 			  MWIFIEX_ASYNC_CMD);
175 
176 	return 0;
177 }
178 #endif
179 
180 /*
181  * This function probes an mwifiex device and registers it. It allocates
182  * the card structure, enables PCIE function number and initiates the
183  * device registration and initialization procedure by adding a logical
184  * interface.
185  */
mwifiex_pcie_probe(struct pci_dev * pdev,const struct pci_device_id * ent)186 static int mwifiex_pcie_probe(struct pci_dev *pdev,
187 					const struct pci_device_id *ent)
188 {
189 	struct pcie_service_card *card;
190 
191 	pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
192 		 pdev->vendor, pdev->device, pdev->revision);
193 
194 	card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
195 	if (!card)
196 		return -ENOMEM;
197 
198 	card->dev = pdev;
199 
200 	if (ent->driver_data) {
201 		struct mwifiex_pcie_device *data = (void *)ent->driver_data;
202 		card->pcie.firmware = data->firmware;
203 		card->pcie.reg = data->reg;
204 		card->pcie.blksz_fw_dl = data->blksz_fw_dl;
205 		card->pcie.tx_buf_size = data->tx_buf_size;
206 		card->pcie.supports_fw_dump = data->supports_fw_dump;
207 	}
208 
209 	if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops,
210 			     MWIFIEX_PCIE)) {
211 		pr_err("%s failed\n", __func__);
212 		kfree(card);
213 		return -1;
214 	}
215 
216 	return 0;
217 }
218 
219 /*
220  * This function removes the interface and frees up the card structure.
221  */
mwifiex_pcie_remove(struct pci_dev * pdev)222 static void mwifiex_pcie_remove(struct pci_dev *pdev)
223 {
224 	struct pcie_service_card *card;
225 	struct mwifiex_adapter *adapter;
226 	struct mwifiex_private *priv;
227 
228 	card = pci_get_drvdata(pdev);
229 	if (!card)
230 		return;
231 
232 	adapter = card->adapter;
233 	if (!adapter || !adapter->priv_num)
234 		return;
235 
236 	cancel_work_sync(&adapter->iface_work);
237 
238 	if (user_rmmod) {
239 #ifdef CONFIG_PM_SLEEP
240 		if (adapter->is_suspended)
241 			mwifiex_pcie_resume(&pdev->dev);
242 #endif
243 
244 		mwifiex_deauthenticate_all(adapter);
245 
246 		priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
247 
248 		mwifiex_disable_auto_ds(priv);
249 
250 		mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
251 	}
252 
253 	mwifiex_remove_card(card->adapter, &add_remove_card_sem);
254 }
255 
mwifiex_pcie_shutdown(struct pci_dev * pdev)256 static void mwifiex_pcie_shutdown(struct pci_dev *pdev)
257 {
258 	user_rmmod = 1;
259 	mwifiex_pcie_remove(pdev);
260 
261 	return;
262 }
263 
264 static const struct pci_device_id mwifiex_ids[] = {
265 	{
266 		PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
267 		PCI_ANY_ID, PCI_ANY_ID, 0, 0,
268 		.driver_data = (unsigned long) &mwifiex_pcie8766,
269 	},
270 	{
271 		PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8897,
272 		PCI_ANY_ID, PCI_ANY_ID, 0, 0,
273 		.driver_data = (unsigned long) &mwifiex_pcie8897,
274 	},
275 	{},
276 };
277 
278 MODULE_DEVICE_TABLE(pci, mwifiex_ids);
279 
280 #ifdef CONFIG_PM_SLEEP
281 /* Power Management Hooks */
282 static SIMPLE_DEV_PM_OPS(mwifiex_pcie_pm_ops, mwifiex_pcie_suspend,
283 				mwifiex_pcie_resume);
284 #endif
285 
286 /* PCI Device Driver */
287 static struct pci_driver __refdata mwifiex_pcie = {
288 	.name     = "mwifiex_pcie",
289 	.id_table = mwifiex_ids,
290 	.probe    = mwifiex_pcie_probe,
291 	.remove   = mwifiex_pcie_remove,
292 #ifdef CONFIG_PM_SLEEP
293 	.driver   = {
294 		.pm = &mwifiex_pcie_pm_ops,
295 	},
296 #endif
297 	.shutdown = mwifiex_pcie_shutdown,
298 };
299 
300 /*
301  * This function writes data into PCIE card register.
302  */
mwifiex_write_reg(struct mwifiex_adapter * adapter,int reg,u32 data)303 static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
304 {
305 	struct pcie_service_card *card = adapter->card;
306 
307 	iowrite32(data, card->pci_mmap1 + reg);
308 
309 	return 0;
310 }
311 
312 /*
313  * This function reads data from PCIE card register.
314  */
mwifiex_read_reg(struct mwifiex_adapter * adapter,int reg,u32 * data)315 static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
316 {
317 	struct pcie_service_card *card = adapter->card;
318 
319 	*data = ioread32(card->pci_mmap1 + reg);
320 
321 	return 0;
322 }
323 
324 /* This function reads u8 data from PCIE card register. */
mwifiex_read_reg_byte(struct mwifiex_adapter * adapter,int reg,u8 * data)325 static int mwifiex_read_reg_byte(struct mwifiex_adapter *adapter,
326 				 int reg, u8 *data)
327 {
328 	struct pcie_service_card *card = adapter->card;
329 
330 	*data = ioread8(card->pci_mmap1 + reg);
331 
332 	return 0;
333 }
334 
335 /*
336  * This function adds delay loop to ensure FW is awake before proceeding.
337  */
mwifiex_pcie_dev_wakeup_delay(struct mwifiex_adapter * adapter)338 static void mwifiex_pcie_dev_wakeup_delay(struct mwifiex_adapter *adapter)
339 {
340 	int i = 0;
341 
342 	while (mwifiex_pcie_ok_to_access_hw(adapter)) {
343 		i++;
344 		usleep_range(10, 20);
345 		/* 50ms max wait */
346 		if (i == 5000)
347 			break;
348 	}
349 
350 	return;
351 }
352 
mwifiex_delay_for_sleep_cookie(struct mwifiex_adapter * adapter,u32 max_delay_loop_cnt)353 static void mwifiex_delay_for_sleep_cookie(struct mwifiex_adapter *adapter,
354 					   u32 max_delay_loop_cnt)
355 {
356 	struct pcie_service_card *card = adapter->card;
357 	u8 *buffer;
358 	u32 sleep_cookie, count;
359 
360 	for (count = 0; count < max_delay_loop_cnt; count++) {
361 		buffer = card->cmdrsp_buf->data - INTF_HEADER_LEN;
362 		sleep_cookie = *(u32 *)buffer;
363 
364 		if (sleep_cookie == MWIFIEX_DEF_SLEEP_COOKIE) {
365 			dev_dbg(adapter->dev,
366 				"sleep cookie found at count %d\n", count);
367 			break;
368 		}
369 		usleep_range(20, 30);
370 	}
371 
372 	if (count >= max_delay_loop_cnt)
373 		dev_dbg(adapter->dev,
374 			"max count reached while accessing sleep cookie\n");
375 }
376 
377 /* This function wakes up the card by reading fw_status register. */
mwifiex_pm_wakeup_card(struct mwifiex_adapter * adapter)378 static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
379 {
380 	u32 fw_status;
381 	struct pcie_service_card *card = adapter->card;
382 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
383 
384 	dev_dbg(adapter->dev, "event: Wakeup device...\n");
385 
386 	if (reg->sleep_cookie)
387 		mwifiex_pcie_dev_wakeup_delay(adapter);
388 
389 	/* Reading fw_status register will wakeup device */
390 	if (mwifiex_read_reg(adapter, reg->fw_status, &fw_status)) {
391 		dev_warn(adapter->dev, "Reading fw_status register failed\n");
392 		return -1;
393 	}
394 
395 	if (reg->sleep_cookie) {
396 		mwifiex_pcie_dev_wakeup_delay(adapter);
397 		dev_dbg(adapter->dev, "PCIE wakeup: Setting PS_STATE_AWAKE\n");
398 		adapter->ps_state = PS_STATE_AWAKE;
399 	}
400 
401 	return 0;
402 }
403 
404 /*
405  * This function is called after the card has woken up.
406  *
407  * The card configuration register is reset.
408  */
mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter * adapter)409 static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
410 {
411 	dev_dbg(adapter->dev, "cmd: Wakeup device completed\n");
412 
413 	return 0;
414 }
415 
416 /*
417  * This function disables the host interrupt.
418  *
419  * The host interrupt mask is read, the disable bit is reset and
420  * written back to the card host interrupt mask register.
421  */
mwifiex_pcie_disable_host_int(struct mwifiex_adapter * adapter)422 static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
423 {
424 	if (mwifiex_pcie_ok_to_access_hw(adapter)) {
425 		if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
426 				      0x00000000)) {
427 			dev_warn(adapter->dev, "Disable host interrupt failed\n");
428 			return -1;
429 		}
430 	}
431 
432 	return 0;
433 }
434 
435 /*
436  * This function enables the host interrupt.
437  *
438  * The host interrupt enable mask is written to the card
439  * host interrupt mask register.
440  */
mwifiex_pcie_enable_host_int(struct mwifiex_adapter * adapter)441 static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
442 {
443 	if (mwifiex_pcie_ok_to_access_hw(adapter)) {
444 		/* Simply write the mask to the register */
445 		if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
446 				      HOST_INTR_MASK)) {
447 			dev_warn(adapter->dev, "Enable host interrupt failed\n");
448 			return -1;
449 		}
450 	}
451 
452 	return 0;
453 }
454 
455 /*
456  * This function initializes TX buffer ring descriptors
457  */
mwifiex_init_txq_ring(struct mwifiex_adapter * adapter)458 static int mwifiex_init_txq_ring(struct mwifiex_adapter *adapter)
459 {
460 	struct pcie_service_card *card = adapter->card;
461 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
462 	struct mwifiex_pcie_buf_desc *desc;
463 	struct mwifiex_pfu_buf_desc *desc2;
464 	int i;
465 
466 	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
467 		card->tx_buf_list[i] = NULL;
468 		if (reg->pfu_enabled) {
469 			card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
470 					     (sizeof(*desc2) * i);
471 			desc2 = card->txbd_ring[i];
472 			memset(desc2, 0, sizeof(*desc2));
473 		} else {
474 			card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
475 					     (sizeof(*desc) * i);
476 			desc = card->txbd_ring[i];
477 			memset(desc, 0, sizeof(*desc));
478 		}
479 	}
480 
481 	return 0;
482 }
483 
484 /* This function initializes RX buffer ring descriptors. Each SKB is allocated
485  * here and after mapping PCI memory, its physical address is assigned to
486  * PCIE Rx buffer descriptor's physical address.
487  */
mwifiex_init_rxq_ring(struct mwifiex_adapter * adapter)488 static int mwifiex_init_rxq_ring(struct mwifiex_adapter *adapter)
489 {
490 	struct pcie_service_card *card = adapter->card;
491 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
492 	struct sk_buff *skb;
493 	struct mwifiex_pcie_buf_desc *desc;
494 	struct mwifiex_pfu_buf_desc *desc2;
495 	dma_addr_t buf_pa;
496 	int i;
497 
498 	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
499 		/* Allocate skb here so that firmware can DMA data from it */
500 		skb = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
501 		if (!skb) {
502 			dev_err(adapter->dev,
503 				"Unable to allocate skb for RX ring.\n");
504 			kfree(card->rxbd_ring_vbase);
505 			return -ENOMEM;
506 		}
507 
508 		if (mwifiex_map_pci_memory(adapter, skb,
509 					   MWIFIEX_RX_DATA_BUF_SIZE,
510 					   PCI_DMA_FROMDEVICE))
511 			return -1;
512 
513 		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
514 
515 		dev_dbg(adapter->dev,
516 			"info: RX ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
517 			skb, skb->len, skb->data, (u32)buf_pa,
518 			(u32)((u64)buf_pa >> 32));
519 
520 		card->rx_buf_list[i] = skb;
521 		if (reg->pfu_enabled) {
522 			card->rxbd_ring[i] = (void *)card->rxbd_ring_vbase +
523 					     (sizeof(*desc2) * i);
524 			desc2 = card->rxbd_ring[i];
525 			desc2->paddr = buf_pa;
526 			desc2->len = (u16)skb->len;
527 			desc2->frag_len = (u16)skb->len;
528 			desc2->flags = reg->ring_flag_eop | reg->ring_flag_sop;
529 			desc2->offset = 0;
530 		} else {
531 			card->rxbd_ring[i] = (void *)(card->rxbd_ring_vbase +
532 					     (sizeof(*desc) * i));
533 			desc = card->rxbd_ring[i];
534 			desc->paddr = buf_pa;
535 			desc->len = (u16)skb->len;
536 			desc->flags = 0;
537 		}
538 	}
539 
540 	return 0;
541 }
542 
543 /* This function initializes event buffer ring descriptors. Each SKB is
544  * allocated here and after mapping PCI memory, its physical address is assigned
545  * to PCIE Rx buffer descriptor's physical address
546  */
mwifiex_pcie_init_evt_ring(struct mwifiex_adapter * adapter)547 static int mwifiex_pcie_init_evt_ring(struct mwifiex_adapter *adapter)
548 {
549 	struct pcie_service_card *card = adapter->card;
550 	struct mwifiex_evt_buf_desc *desc;
551 	struct sk_buff *skb;
552 	dma_addr_t buf_pa;
553 	int i;
554 
555 	for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
556 		/* Allocate skb here so that firmware can DMA data from it */
557 		skb = dev_alloc_skb(MAX_EVENT_SIZE);
558 		if (!skb) {
559 			dev_err(adapter->dev,
560 				"Unable to allocate skb for EVENT buf.\n");
561 			kfree(card->evtbd_ring_vbase);
562 			return -ENOMEM;
563 		}
564 		skb_put(skb, MAX_EVENT_SIZE);
565 
566 		if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE,
567 					   PCI_DMA_FROMDEVICE))
568 			return -1;
569 
570 		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
571 
572 		dev_dbg(adapter->dev,
573 			"info: EVT ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
574 			skb, skb->len, skb->data, (u32)buf_pa,
575 			(u32)((u64)buf_pa >> 32));
576 
577 		card->evt_buf_list[i] = skb;
578 		card->evtbd_ring[i] = (void *)(card->evtbd_ring_vbase +
579 				      (sizeof(*desc) * i));
580 		desc = card->evtbd_ring[i];
581 		desc->paddr = buf_pa;
582 		desc->len = (u16)skb->len;
583 		desc->flags = 0;
584 	}
585 
586 	return 0;
587 }
588 
589 /* This function cleans up TX buffer rings. If any of the buffer list has valid
590  * SKB address, associated SKB is freed.
591  */
mwifiex_cleanup_txq_ring(struct mwifiex_adapter * adapter)592 static void mwifiex_cleanup_txq_ring(struct mwifiex_adapter *adapter)
593 {
594 	struct pcie_service_card *card = adapter->card;
595 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
596 	struct sk_buff *skb;
597 	struct mwifiex_pcie_buf_desc *desc;
598 	struct mwifiex_pfu_buf_desc *desc2;
599 	int i;
600 
601 	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
602 		if (reg->pfu_enabled) {
603 			desc2 = card->txbd_ring[i];
604 			if (card->tx_buf_list[i]) {
605 				skb = card->tx_buf_list[i];
606 				mwifiex_unmap_pci_memory(adapter, skb,
607 							 PCI_DMA_TODEVICE);
608 				dev_kfree_skb_any(skb);
609 			}
610 			memset(desc2, 0, sizeof(*desc2));
611 		} else {
612 			desc = card->txbd_ring[i];
613 			if (card->tx_buf_list[i]) {
614 				skb = card->tx_buf_list[i];
615 				mwifiex_unmap_pci_memory(adapter, skb,
616 							 PCI_DMA_TODEVICE);
617 				dev_kfree_skb_any(skb);
618 			}
619 			memset(desc, 0, sizeof(*desc));
620 		}
621 		card->tx_buf_list[i] = NULL;
622 	}
623 
624 	return;
625 }
626 
627 /* This function cleans up RX buffer rings. If any of the buffer list has valid
628  * SKB address, associated SKB is freed.
629  */
mwifiex_cleanup_rxq_ring(struct mwifiex_adapter * adapter)630 static void mwifiex_cleanup_rxq_ring(struct mwifiex_adapter *adapter)
631 {
632 	struct pcie_service_card *card = adapter->card;
633 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
634 	struct mwifiex_pcie_buf_desc *desc;
635 	struct mwifiex_pfu_buf_desc *desc2;
636 	struct sk_buff *skb;
637 	int i;
638 
639 	for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
640 		if (reg->pfu_enabled) {
641 			desc2 = card->rxbd_ring[i];
642 			if (card->rx_buf_list[i]) {
643 				skb = card->rx_buf_list[i];
644 				mwifiex_unmap_pci_memory(adapter, skb,
645 							 PCI_DMA_FROMDEVICE);
646 				dev_kfree_skb_any(skb);
647 			}
648 			memset(desc2, 0, sizeof(*desc2));
649 		} else {
650 			desc = card->rxbd_ring[i];
651 			if (card->rx_buf_list[i]) {
652 				skb = card->rx_buf_list[i];
653 				mwifiex_unmap_pci_memory(adapter, skb,
654 							 PCI_DMA_FROMDEVICE);
655 				dev_kfree_skb_any(skb);
656 			}
657 			memset(desc, 0, sizeof(*desc));
658 		}
659 		card->rx_buf_list[i] = NULL;
660 	}
661 
662 	return;
663 }
664 
665 /* This function cleans up event buffer rings. If any of the buffer list has
666  * valid SKB address, associated SKB is freed.
667  */
mwifiex_cleanup_evt_ring(struct mwifiex_adapter * adapter)668 static void mwifiex_cleanup_evt_ring(struct mwifiex_adapter *adapter)
669 {
670 	struct pcie_service_card *card = adapter->card;
671 	struct mwifiex_evt_buf_desc *desc;
672 	struct sk_buff *skb;
673 	int i;
674 
675 	for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
676 		desc = card->evtbd_ring[i];
677 		if (card->evt_buf_list[i]) {
678 			skb = card->evt_buf_list[i];
679 			mwifiex_unmap_pci_memory(adapter, skb,
680 						 PCI_DMA_FROMDEVICE);
681 			dev_kfree_skb_any(skb);
682 		}
683 		card->evt_buf_list[i] = NULL;
684 		memset(desc, 0, sizeof(*desc));
685 	}
686 
687 	return;
688 }
689 
690 /* This function creates buffer descriptor ring for TX
691  */
mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter * adapter)692 static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
693 {
694 	struct pcie_service_card *card = adapter->card;
695 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
696 
697 	/*
698 	 * driver maintaines the write pointer and firmware maintaines the read
699 	 * pointer. The write pointer starts at 0 (zero) while the read pointer
700 	 * starts at zero with rollover bit set
701 	 */
702 	card->txbd_wrptr = 0;
703 
704 	if (reg->pfu_enabled)
705 		card->txbd_rdptr = 0;
706 	else
707 		card->txbd_rdptr |= reg->tx_rollover_ind;
708 
709 	/* allocate shared memory for the BD ring and divide the same in to
710 	   several descriptors */
711 	if (reg->pfu_enabled)
712 		card->txbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
713 				       MWIFIEX_MAX_TXRX_BD;
714 	else
715 		card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
716 				       MWIFIEX_MAX_TXRX_BD;
717 
718 	dev_dbg(adapter->dev, "info: txbd_ring: Allocating %d bytes\n",
719 		card->txbd_ring_size);
720 	card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
721 						     card->txbd_ring_size,
722 						     &card->txbd_ring_pbase);
723 	if (!card->txbd_ring_vbase) {
724 		dev_err(adapter->dev,
725 			"allocate consistent memory (%d bytes) failed!\n",
726 			card->txbd_ring_size);
727 		return -ENOMEM;
728 	}
729 	dev_dbg(adapter->dev,
730 		"info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
731 		card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
732 		(u32)((u64)card->txbd_ring_pbase >> 32), card->txbd_ring_size);
733 
734 	return mwifiex_init_txq_ring(adapter);
735 }
736 
mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter * adapter)737 static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
738 {
739 	struct pcie_service_card *card = adapter->card;
740 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
741 
742 	mwifiex_cleanup_txq_ring(adapter);
743 
744 	if (card->txbd_ring_vbase)
745 		pci_free_consistent(card->dev, card->txbd_ring_size,
746 				    card->txbd_ring_vbase,
747 				    card->txbd_ring_pbase);
748 	card->txbd_ring_size = 0;
749 	card->txbd_wrptr = 0;
750 	card->txbd_rdptr = 0 | reg->tx_rollover_ind;
751 	card->txbd_ring_vbase = NULL;
752 	card->txbd_ring_pbase = 0;
753 
754 	return 0;
755 }
756 
757 /*
758  * This function creates buffer descriptor ring for RX
759  */
mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter * adapter)760 static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
761 {
762 	struct pcie_service_card *card = adapter->card;
763 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
764 
765 	/*
766 	 * driver maintaines the read pointer and firmware maintaines the write
767 	 * pointer. The write pointer starts at 0 (zero) while the read pointer
768 	 * starts at zero with rollover bit set
769 	 */
770 	card->rxbd_wrptr = 0;
771 	card->rxbd_rdptr = reg->rx_rollover_ind;
772 
773 	if (reg->pfu_enabled)
774 		card->rxbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
775 				       MWIFIEX_MAX_TXRX_BD;
776 	else
777 		card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
778 				       MWIFIEX_MAX_TXRX_BD;
779 
780 	dev_dbg(adapter->dev, "info: rxbd_ring: Allocating %d bytes\n",
781 		card->rxbd_ring_size);
782 	card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
783 						     card->rxbd_ring_size,
784 						     &card->rxbd_ring_pbase);
785 	if (!card->rxbd_ring_vbase) {
786 		dev_err(adapter->dev,
787 			"allocate consistent memory (%d bytes) failed!\n",
788 			card->rxbd_ring_size);
789 		return -ENOMEM;
790 	}
791 
792 	dev_dbg(adapter->dev,
793 		"info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n",
794 		card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase,
795 		(u32)((u64)card->rxbd_ring_pbase >> 32),
796 		card->rxbd_ring_size);
797 
798 	return mwifiex_init_rxq_ring(adapter);
799 }
800 
801 /*
802  * This function deletes Buffer descriptor ring for RX
803  */
mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter * adapter)804 static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
805 {
806 	struct pcie_service_card *card = adapter->card;
807 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
808 
809 	mwifiex_cleanup_rxq_ring(adapter);
810 
811 	if (card->rxbd_ring_vbase)
812 		pci_free_consistent(card->dev, card->rxbd_ring_size,
813 				    card->rxbd_ring_vbase,
814 				    card->rxbd_ring_pbase);
815 	card->rxbd_ring_size = 0;
816 	card->rxbd_wrptr = 0;
817 	card->rxbd_rdptr = 0 | reg->rx_rollover_ind;
818 	card->rxbd_ring_vbase = NULL;
819 	card->rxbd_ring_pbase = 0;
820 
821 	return 0;
822 }
823 
824 /*
825  * This function creates buffer descriptor ring for Events
826  */
mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter * adapter)827 static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
828 {
829 	struct pcie_service_card *card = adapter->card;
830 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
831 
832 	/*
833 	 * driver maintaines the read pointer and firmware maintaines the write
834 	 * pointer. The write pointer starts at 0 (zero) while the read pointer
835 	 * starts at zero with rollover bit set
836 	 */
837 	card->evtbd_wrptr = 0;
838 	card->evtbd_rdptr = reg->evt_rollover_ind;
839 
840 	card->evtbd_ring_size = sizeof(struct mwifiex_evt_buf_desc) *
841 				MWIFIEX_MAX_EVT_BD;
842 
843 	dev_dbg(adapter->dev, "info: evtbd_ring: Allocating %d bytes\n",
844 		card->evtbd_ring_size);
845 	card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
846 						      card->evtbd_ring_size,
847 						      &card->evtbd_ring_pbase);
848 	if (!card->evtbd_ring_vbase) {
849 		dev_err(adapter->dev,
850 			"allocate consistent memory (%d bytes) failed!\n",
851 			card->evtbd_ring_size);
852 		return -ENOMEM;
853 	}
854 
855 	dev_dbg(adapter->dev,
856 		"info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n",
857 		card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase,
858 		(u32)((u64)card->evtbd_ring_pbase >> 32),
859 		card->evtbd_ring_size);
860 
861 	return mwifiex_pcie_init_evt_ring(adapter);
862 }
863 
864 /*
865  * This function deletes Buffer descriptor ring for Events
866  */
mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter * adapter)867 static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
868 {
869 	struct pcie_service_card *card = adapter->card;
870 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
871 
872 	mwifiex_cleanup_evt_ring(adapter);
873 
874 	if (card->evtbd_ring_vbase)
875 		pci_free_consistent(card->dev, card->evtbd_ring_size,
876 				    card->evtbd_ring_vbase,
877 				    card->evtbd_ring_pbase);
878 	card->evtbd_wrptr = 0;
879 	card->evtbd_rdptr = 0 | reg->evt_rollover_ind;
880 	card->evtbd_ring_size = 0;
881 	card->evtbd_ring_vbase = NULL;
882 	card->evtbd_ring_pbase = 0;
883 
884 	return 0;
885 }
886 
887 /*
888  * This function allocates a buffer for CMDRSP
889  */
mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter * adapter)890 static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
891 {
892 	struct pcie_service_card *card = adapter->card;
893 	struct sk_buff *skb;
894 
895 	/* Allocate memory for receiving command response data */
896 	skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
897 	if (!skb) {
898 		dev_err(adapter->dev,
899 			"Unable to allocate skb for command response data.\n");
900 		return -ENOMEM;
901 	}
902 	skb_put(skb, MWIFIEX_UPLD_SIZE);
903 	if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
904 				   PCI_DMA_FROMDEVICE))
905 		return -1;
906 
907 	card->cmdrsp_buf = skb;
908 
909 	return 0;
910 }
911 
912 /*
913  * This function deletes a buffer for CMDRSP
914  */
mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter * adapter)915 static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
916 {
917 	struct pcie_service_card *card;
918 
919 	if (!adapter)
920 		return 0;
921 
922 	card = adapter->card;
923 
924 	if (card && card->cmdrsp_buf) {
925 		mwifiex_unmap_pci_memory(adapter, card->cmdrsp_buf,
926 					 PCI_DMA_FROMDEVICE);
927 		dev_kfree_skb_any(card->cmdrsp_buf);
928 	}
929 
930 	if (card && card->cmd_buf) {
931 		mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
932 					 PCI_DMA_TODEVICE);
933 		dev_kfree_skb_any(card->cmd_buf);
934 	}
935 	return 0;
936 }
937 
938 /*
939  * This function allocates a buffer for sleep cookie
940  */
mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter * adapter)941 static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
942 {
943 	struct pcie_service_card *card = adapter->card;
944 
945 	card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32),
946 						     &card->sleep_cookie_pbase);
947 	if (!card->sleep_cookie_vbase) {
948 		dev_err(adapter->dev, "pci_alloc_consistent failed!\n");
949 		return -ENOMEM;
950 	}
951 	/* Init val of Sleep Cookie */
952 	*(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE;
953 
954 	dev_dbg(adapter->dev, "alloc_scook: sleep cookie=0x%x\n",
955 		*((u32 *)card->sleep_cookie_vbase));
956 
957 	return 0;
958 }
959 
960 /*
961  * This function deletes buffer for sleep cookie
962  */
mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter * adapter)963 static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
964 {
965 	struct pcie_service_card *card;
966 
967 	if (!adapter)
968 		return 0;
969 
970 	card = adapter->card;
971 
972 	if (card && card->sleep_cookie_vbase) {
973 		pci_free_consistent(card->dev, sizeof(u32),
974 				    card->sleep_cookie_vbase,
975 				    card->sleep_cookie_pbase);
976 		card->sleep_cookie_vbase = NULL;
977 	}
978 
979 	return 0;
980 }
981 
982 /* This function flushes the TX buffer descriptor ring
983  * This function defined as handler is also called while cleaning TXRX
984  * during disconnect/ bss stop.
985  */
mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter * adapter)986 static int mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter *adapter)
987 {
988 	struct pcie_service_card *card = adapter->card;
989 
990 	if (!mwifiex_pcie_txbd_empty(card, card->txbd_rdptr)) {
991 		card->txbd_flush = 1;
992 		/* write pointer already set at last send
993 		 * send dnld-rdy intr again, wait for completion.
994 		 */
995 		if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
996 				      CPU_INTR_DNLD_RDY)) {
997 			dev_err(adapter->dev,
998 				"failed to assert dnld-rdy interrupt.\n");
999 			return -1;
1000 		}
1001 	}
1002 	return 0;
1003 }
1004 
1005 /*
1006  * This function unmaps and frees downloaded data buffer
1007  */
mwifiex_pcie_send_data_complete(struct mwifiex_adapter * adapter)1008 static int mwifiex_pcie_send_data_complete(struct mwifiex_adapter *adapter)
1009 {
1010 	struct sk_buff *skb;
1011 	u32 wrdoneidx, rdptr, num_tx_buffs, unmap_count = 0;
1012 	struct mwifiex_pcie_buf_desc *desc;
1013 	struct mwifiex_pfu_buf_desc *desc2;
1014 	struct pcie_service_card *card = adapter->card;
1015 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1016 
1017 	if (!mwifiex_pcie_ok_to_access_hw(adapter))
1018 		mwifiex_pm_wakeup_card(adapter);
1019 
1020 	/* Read the TX ring read pointer set by firmware */
1021 	if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
1022 		dev_err(adapter->dev,
1023 			"SEND COMP: failed to read reg->tx_rdptr\n");
1024 		return -1;
1025 	}
1026 
1027 	dev_dbg(adapter->dev, "SEND COMP: rdptr_prev=0x%x, rdptr=0x%x\n",
1028 		card->txbd_rdptr, rdptr);
1029 
1030 	num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1031 	/* free from previous txbd_rdptr to current txbd_rdptr */
1032 	while (((card->txbd_rdptr & reg->tx_mask) !=
1033 		(rdptr & reg->tx_mask)) ||
1034 	       ((card->txbd_rdptr & reg->tx_rollover_ind) !=
1035 		(rdptr & reg->tx_rollover_ind))) {
1036 		wrdoneidx = (card->txbd_rdptr & reg->tx_mask) >>
1037 			    reg->tx_start_ptr;
1038 
1039 		skb = card->tx_buf_list[wrdoneidx];
1040 
1041 		if (skb) {
1042 			dev_dbg(adapter->dev,
1043 				"SEND COMP: Detach skb %p at txbd_rdidx=%d\n",
1044 				skb, wrdoneidx);
1045 			mwifiex_unmap_pci_memory(adapter, skb,
1046 						 PCI_DMA_TODEVICE);
1047 
1048 			unmap_count++;
1049 
1050 			if (card->txbd_flush)
1051 				mwifiex_write_data_complete(adapter, skb, 0,
1052 							    -1);
1053 			else
1054 				mwifiex_write_data_complete(adapter, skb, 0, 0);
1055 		}
1056 
1057 		card->tx_buf_list[wrdoneidx] = NULL;
1058 
1059 		if (reg->pfu_enabled) {
1060 			desc2 = card->txbd_ring[wrdoneidx];
1061 			memset(desc2, 0, sizeof(*desc2));
1062 		} else {
1063 			desc = card->txbd_ring[wrdoneidx];
1064 			memset(desc, 0, sizeof(*desc));
1065 		}
1066 		switch (card->dev->device) {
1067 		case PCIE_DEVICE_ID_MARVELL_88W8766P:
1068 			card->txbd_rdptr++;
1069 			break;
1070 		case PCIE_DEVICE_ID_MARVELL_88W8897:
1071 			card->txbd_rdptr += reg->ring_tx_start_ptr;
1072 			break;
1073 		}
1074 
1075 
1076 		if ((card->txbd_rdptr & reg->tx_mask) == num_tx_buffs)
1077 			card->txbd_rdptr = ((card->txbd_rdptr &
1078 					     reg->tx_rollover_ind) ^
1079 					     reg->tx_rollover_ind);
1080 	}
1081 
1082 	if (unmap_count)
1083 		adapter->data_sent = false;
1084 
1085 	if (card->txbd_flush) {
1086 		if (mwifiex_pcie_txbd_empty(card, card->txbd_rdptr))
1087 			card->txbd_flush = 0;
1088 		else
1089 			mwifiex_clean_pcie_ring_buf(adapter);
1090 	}
1091 
1092 	return 0;
1093 }
1094 
1095 /* This function sends data buffer to device. First 4 bytes of payload
1096  * are filled with payload length and payload type. Then this payload
1097  * is mapped to PCI device memory. Tx ring pointers are advanced accordingly.
1098  * Download ready interrupt to FW is deffered if Tx ring is not full and
1099  * additional payload can be accomodated.
1100  * Caller must ensure tx_param parameter to this function is not NULL.
1101  */
1102 static int
mwifiex_pcie_send_data(struct mwifiex_adapter * adapter,struct sk_buff * skb,struct mwifiex_tx_param * tx_param)1103 mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb,
1104 		       struct mwifiex_tx_param *tx_param)
1105 {
1106 	struct pcie_service_card *card = adapter->card;
1107 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1108 	u32 wrindx, num_tx_buffs, rx_val;
1109 	int ret;
1110 	dma_addr_t buf_pa;
1111 	struct mwifiex_pcie_buf_desc *desc = NULL;
1112 	struct mwifiex_pfu_buf_desc *desc2 = NULL;
1113 	__le16 *tmp;
1114 
1115 	if (!(skb->data && skb->len)) {
1116 		dev_err(adapter->dev, "%s(): invalid parameter <%p, %#x>\n",
1117 			__func__, skb->data, skb->len);
1118 		return -1;
1119 	}
1120 
1121 	if (!mwifiex_pcie_ok_to_access_hw(adapter))
1122 		mwifiex_pm_wakeup_card(adapter);
1123 
1124 	num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
1125 	dev_dbg(adapter->dev, "info: SEND DATA: <Rd: %#x, Wr: %#x>\n",
1126 		card->txbd_rdptr, card->txbd_wrptr);
1127 	if (mwifiex_pcie_txbd_not_full(card)) {
1128 		u8 *payload;
1129 
1130 		adapter->data_sent = true;
1131 		payload = skb->data;
1132 		tmp = (__le16 *)&payload[0];
1133 		*tmp = cpu_to_le16((u16)skb->len);
1134 		tmp = (__le16 *)&payload[2];
1135 		*tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
1136 
1137 		if (mwifiex_map_pci_memory(adapter, skb, skb->len,
1138 					   PCI_DMA_TODEVICE))
1139 			return -1;
1140 
1141 		wrindx = (card->txbd_wrptr & reg->tx_mask) >> reg->tx_start_ptr;
1142 		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1143 		card->tx_buf_list[wrindx] = skb;
1144 
1145 		if (reg->pfu_enabled) {
1146 			desc2 = card->txbd_ring[wrindx];
1147 			desc2->paddr = buf_pa;
1148 			desc2->len = (u16)skb->len;
1149 			desc2->frag_len = (u16)skb->len;
1150 			desc2->offset = 0;
1151 			desc2->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1152 					 MWIFIEX_BD_FLAG_LAST_DESC;
1153 		} else {
1154 			desc = card->txbd_ring[wrindx];
1155 			desc->paddr = buf_pa;
1156 			desc->len = (u16)skb->len;
1157 			desc->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
1158 				      MWIFIEX_BD_FLAG_LAST_DESC;
1159 		}
1160 
1161 		switch (card->dev->device) {
1162 		case PCIE_DEVICE_ID_MARVELL_88W8766P:
1163 			card->txbd_wrptr++;
1164 			break;
1165 		case PCIE_DEVICE_ID_MARVELL_88W8897:
1166 			card->txbd_wrptr += reg->ring_tx_start_ptr;
1167 			break;
1168 		}
1169 
1170 		if ((card->txbd_wrptr & reg->tx_mask) == num_tx_buffs)
1171 			card->txbd_wrptr = ((card->txbd_wrptr &
1172 						reg->tx_rollover_ind) ^
1173 						reg->tx_rollover_ind);
1174 
1175 		rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
1176 		/* Write the TX ring write pointer in to reg->tx_wrptr */
1177 		if (mwifiex_write_reg(adapter, reg->tx_wrptr,
1178 				      card->txbd_wrptr | rx_val)) {
1179 			dev_err(adapter->dev,
1180 				"SEND DATA: failed to write reg->tx_wrptr\n");
1181 			ret = -1;
1182 			goto done_unmap;
1183 		}
1184 		if ((mwifiex_pcie_txbd_not_full(card)) &&
1185 		    tx_param->next_pkt_len) {
1186 			/* have more packets and TxBD still can hold more */
1187 			dev_dbg(adapter->dev,
1188 				"SEND DATA: delay dnld-rdy interrupt.\n");
1189 			adapter->data_sent = false;
1190 		} else {
1191 			/* Send the TX ready interrupt */
1192 			if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1193 					      CPU_INTR_DNLD_RDY)) {
1194 				dev_err(adapter->dev,
1195 					"SEND DATA: failed to assert dnld-rdy interrupt.\n");
1196 				ret = -1;
1197 				goto done_unmap;
1198 			}
1199 		}
1200 		dev_dbg(adapter->dev, "info: SEND DATA: Updated <Rd: %#x, Wr: "
1201 			"%#x> and sent packet to firmware successfully\n",
1202 			card->txbd_rdptr, card->txbd_wrptr);
1203 	} else {
1204 		dev_dbg(adapter->dev,
1205 			"info: TX Ring full, can't send packets to fw\n");
1206 		adapter->data_sent = true;
1207 		/* Send the TX ready interrupt */
1208 		if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1209 				      CPU_INTR_DNLD_RDY))
1210 			dev_err(adapter->dev,
1211 				"SEND DATA: failed to assert door-bell intr\n");
1212 		return -EBUSY;
1213 	}
1214 
1215 	return -EINPROGRESS;
1216 done_unmap:
1217 	mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1218 	card->tx_buf_list[wrindx] = NULL;
1219 	if (reg->pfu_enabled)
1220 		memset(desc2, 0, sizeof(*desc2));
1221 	else
1222 		memset(desc, 0, sizeof(*desc));
1223 
1224 	return ret;
1225 }
1226 
1227 /*
1228  * This function handles received buffer ring and
1229  * dispatches packets to upper
1230  */
mwifiex_pcie_process_recv_data(struct mwifiex_adapter * adapter)1231 static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
1232 {
1233 	struct pcie_service_card *card = adapter->card;
1234 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1235 	u32 wrptr, rd_index, tx_val;
1236 	dma_addr_t buf_pa;
1237 	int ret = 0;
1238 	struct sk_buff *skb_tmp = NULL;
1239 	struct mwifiex_pcie_buf_desc *desc;
1240 	struct mwifiex_pfu_buf_desc *desc2;
1241 
1242 	if (!mwifiex_pcie_ok_to_access_hw(adapter))
1243 		mwifiex_pm_wakeup_card(adapter);
1244 
1245 	/* Read the RX ring Write pointer set by firmware */
1246 	if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1247 		dev_err(adapter->dev,
1248 			"RECV DATA: failed to read reg->rx_wrptr\n");
1249 		ret = -1;
1250 		goto done;
1251 	}
1252 	card->rxbd_wrptr = wrptr;
1253 
1254 	while (((wrptr & reg->rx_mask) !=
1255 		(card->rxbd_rdptr & reg->rx_mask)) ||
1256 	       ((wrptr & reg->rx_rollover_ind) ==
1257 		(card->rxbd_rdptr & reg->rx_rollover_ind))) {
1258 		struct sk_buff *skb_data;
1259 		u16 rx_len;
1260 		__le16 pkt_len;
1261 
1262 		rd_index = card->rxbd_rdptr & reg->rx_mask;
1263 		skb_data = card->rx_buf_list[rd_index];
1264 
1265 		/* If skb allocation was failed earlier for Rx packet,
1266 		 * rx_buf_list[rd_index] would have been left with a NULL.
1267 		 */
1268 		if (!skb_data)
1269 			return -ENOMEM;
1270 
1271 		mwifiex_unmap_pci_memory(adapter, skb_data, PCI_DMA_FROMDEVICE);
1272 		card->rx_buf_list[rd_index] = NULL;
1273 
1274 		/* Get data length from interface header -
1275 		 * first 2 bytes for len, next 2 bytes is for type
1276 		 */
1277 		pkt_len = *((__le16 *)skb_data->data);
1278 		rx_len = le16_to_cpu(pkt_len);
1279 		if (WARN_ON(rx_len <= INTF_HEADER_LEN ||
1280 			    rx_len > MWIFIEX_RX_DATA_BUF_SIZE)) {
1281 			dev_err(adapter->dev,
1282 				"Invalid RX len %d, Rd=%#x, Wr=%#x\n",
1283 				rx_len, card->rxbd_rdptr, wrptr);
1284 			dev_kfree_skb_any(skb_data);
1285 		} else {
1286 			skb_put(skb_data, rx_len);
1287 			dev_dbg(adapter->dev,
1288 				"info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
1289 				card->rxbd_rdptr, wrptr, rx_len);
1290 			skb_pull(skb_data, INTF_HEADER_LEN);
1291 			if (adapter->rx_work_enabled) {
1292 				skb_queue_tail(&adapter->rx_data_q, skb_data);
1293 				adapter->data_received = true;
1294 				atomic_inc(&adapter->rx_pending);
1295 			} else {
1296 				mwifiex_handle_rx_packet(adapter, skb_data);
1297 			}
1298 		}
1299 
1300 		skb_tmp = dev_alloc_skb(MWIFIEX_RX_DATA_BUF_SIZE);
1301 		if (!skb_tmp) {
1302 			dev_err(adapter->dev,
1303 				"Unable to allocate skb.\n");
1304 			return -ENOMEM;
1305 		}
1306 
1307 		if (mwifiex_map_pci_memory(adapter, skb_tmp,
1308 					   MWIFIEX_RX_DATA_BUF_SIZE,
1309 					   PCI_DMA_FROMDEVICE))
1310 			return -1;
1311 
1312 		buf_pa = MWIFIEX_SKB_DMA_ADDR(skb_tmp);
1313 
1314 		dev_dbg(adapter->dev,
1315 			"RECV DATA: Attach new sk_buff %p at rxbd_rdidx=%d\n",
1316 			skb_tmp, rd_index);
1317 		card->rx_buf_list[rd_index] = skb_tmp;
1318 
1319 		if (reg->pfu_enabled) {
1320 			desc2 = card->rxbd_ring[rd_index];
1321 			desc2->paddr = buf_pa;
1322 			desc2->len = skb_tmp->len;
1323 			desc2->frag_len = skb_tmp->len;
1324 			desc2->offset = 0;
1325 			desc2->flags = reg->ring_flag_sop | reg->ring_flag_eop;
1326 		} else {
1327 			desc = card->rxbd_ring[rd_index];
1328 			desc->paddr = buf_pa;
1329 			desc->len = skb_tmp->len;
1330 			desc->flags = 0;
1331 		}
1332 
1333 		if ((++card->rxbd_rdptr & reg->rx_mask) ==
1334 							MWIFIEX_MAX_TXRX_BD) {
1335 			card->rxbd_rdptr = ((card->rxbd_rdptr &
1336 					     reg->rx_rollover_ind) ^
1337 					     reg->rx_rollover_ind);
1338 		}
1339 		dev_dbg(adapter->dev, "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
1340 			card->rxbd_rdptr, wrptr);
1341 
1342 		tx_val = card->txbd_wrptr & reg->tx_wrap_mask;
1343 		/* Write the RX ring read pointer in to reg->rx_rdptr */
1344 		if (mwifiex_write_reg(adapter, reg->rx_rdptr,
1345 				      card->rxbd_rdptr | tx_val)) {
1346 			dev_err(adapter->dev,
1347 				"RECV DATA: failed to write reg->rx_rdptr\n");
1348 			ret = -1;
1349 			goto done;
1350 		}
1351 
1352 		/* Read the RX ring Write pointer set by firmware */
1353 		if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
1354 			dev_err(adapter->dev,
1355 				"RECV DATA: failed to read reg->rx_wrptr\n");
1356 			ret = -1;
1357 			goto done;
1358 		}
1359 		dev_dbg(adapter->dev,
1360 			"info: RECV DATA: Rcvd packet from fw successfully\n");
1361 		card->rxbd_wrptr = wrptr;
1362 	}
1363 
1364 done:
1365 	return ret;
1366 }
1367 
1368 /*
1369  * This function downloads the boot command to device
1370  */
1371 static int
mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter * adapter,struct sk_buff * skb)1372 mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1373 {
1374 	dma_addr_t buf_pa;
1375 	struct pcie_service_card *card = adapter->card;
1376 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1377 
1378 	if (!(skb->data && skb->len)) {
1379 		dev_err(adapter->dev,
1380 			"Invalid parameter in %s <%p. len %d>\n",
1381 			__func__, skb->data, skb->len);
1382 		return -1;
1383 	}
1384 
1385 	if (mwifiex_map_pci_memory(adapter, skb, skb->len , PCI_DMA_TODEVICE))
1386 		return -1;
1387 
1388 	buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
1389 
1390 	/* Write the lower 32bits of the physical address to low command
1391 	 * address scratch register
1392 	 */
1393 	if (mwifiex_write_reg(adapter, reg->cmd_addr_lo, (u32)buf_pa)) {
1394 		dev_err(adapter->dev,
1395 			"%s: failed to write download command to boot code.\n",
1396 			__func__);
1397 		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1398 		return -1;
1399 	}
1400 
1401 	/* Write the upper 32bits of the physical address to high command
1402 	 * address scratch register
1403 	 */
1404 	if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1405 			      (u32)((u64)buf_pa >> 32))) {
1406 		dev_err(adapter->dev,
1407 			"%s: failed to write download command to boot code.\n",
1408 			__func__);
1409 		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1410 		return -1;
1411 	}
1412 
1413 	/* Write the command length to cmd_size scratch register */
1414 	if (mwifiex_write_reg(adapter, reg->cmd_size, skb->len)) {
1415 		dev_err(adapter->dev,
1416 			"%s: failed to write command len to cmd_size scratch reg\n",
1417 			__func__);
1418 		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1419 		return -1;
1420 	}
1421 
1422 	/* Ring the door bell */
1423 	if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1424 			      CPU_INTR_DOOR_BELL)) {
1425 		dev_err(adapter->dev,
1426 			"%s: failed to assert door-bell intr\n", __func__);
1427 		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1428 		return -1;
1429 	}
1430 
1431 	return 0;
1432 }
1433 
1434 /* This function init rx port in firmware which in turn enables to receive data
1435  * from device before transmitting any packet.
1436  */
mwifiex_pcie_init_fw_port(struct mwifiex_adapter * adapter)1437 static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
1438 {
1439 	struct pcie_service_card *card = adapter->card;
1440 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1441 	int tx_wrap = card->txbd_wrptr & reg->tx_wrap_mask;
1442 
1443 	/* Write the RX ring read pointer in to reg->rx_rdptr */
1444 	if (mwifiex_write_reg(adapter, reg->rx_rdptr, card->rxbd_rdptr |
1445 			      tx_wrap)) {
1446 		dev_err(adapter->dev,
1447 			"RECV DATA: failed to write reg->rx_rdptr\n");
1448 		return -1;
1449 	}
1450 	return 0;
1451 }
1452 
1453 /* This function downloads commands to the device
1454  */
1455 static int
mwifiex_pcie_send_cmd(struct mwifiex_adapter * adapter,struct sk_buff * skb)1456 mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
1457 {
1458 	struct pcie_service_card *card = adapter->card;
1459 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1460 	int ret = 0;
1461 	dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
1462 	u8 *payload = (u8 *)skb->data;
1463 
1464 	if (!(skb->data && skb->len)) {
1465 		dev_err(adapter->dev, "Invalid parameter in %s <%p, %#x>\n",
1466 			__func__, skb->data, skb->len);
1467 		return -1;
1468 	}
1469 
1470 	/* Make sure a command response buffer is available */
1471 	if (!card->cmdrsp_buf) {
1472 		dev_err(adapter->dev,
1473 			"No response buffer available, send command failed\n");
1474 		return -EBUSY;
1475 	}
1476 
1477 	if (!mwifiex_pcie_ok_to_access_hw(adapter))
1478 		mwifiex_pm_wakeup_card(adapter);
1479 
1480 	adapter->cmd_sent = true;
1481 
1482 	*(__le16 *)&payload[0] = cpu_to_le16((u16)skb->len);
1483 	*(__le16 *)&payload[2] = cpu_to_le16(MWIFIEX_TYPE_CMD);
1484 
1485 	if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
1486 		return -1;
1487 
1488 	card->cmd_buf = skb;
1489 	/*
1490 	 * Need to keep a reference, since core driver might free up this
1491 	 * buffer before we've unmapped it.
1492 	 */
1493 	skb_get(skb);
1494 
1495 	/* To send a command, the driver will:
1496 		1. Write the 64bit physical address of the data buffer to
1497 		   cmd response address low  + cmd response address high
1498 		2. Ring the door bell (i.e. set the door bell interrupt)
1499 
1500 		In response to door bell interrupt, the firmware will perform
1501 		the DMA of the command packet (first header to obtain the total
1502 		length and then rest of the command).
1503 	*/
1504 
1505 	if (card->cmdrsp_buf) {
1506 		cmdrsp_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmdrsp_buf);
1507 		/* Write the lower 32bits of the cmdrsp buffer physical
1508 		   address */
1509 		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo,
1510 				      (u32)cmdrsp_buf_pa)) {
1511 			dev_err(adapter->dev,
1512 				"Failed to write download cmd to boot code.\n");
1513 			ret = -1;
1514 			goto done;
1515 		}
1516 		/* Write the upper 32bits of the cmdrsp buffer physical
1517 		   address */
1518 		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi,
1519 				      (u32)((u64)cmdrsp_buf_pa >> 32))) {
1520 			dev_err(adapter->dev,
1521 				"Failed to write download cmd to boot code.\n");
1522 			ret = -1;
1523 			goto done;
1524 		}
1525 	}
1526 
1527 	cmd_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmd_buf);
1528 	/* Write the lower 32bits of the physical address to reg->cmd_addr_lo */
1529 	if (mwifiex_write_reg(adapter, reg->cmd_addr_lo,
1530 			      (u32)cmd_buf_pa)) {
1531 		dev_err(adapter->dev,
1532 			"Failed to write download cmd to boot code.\n");
1533 		ret = -1;
1534 		goto done;
1535 	}
1536 	/* Write the upper 32bits of the physical address to reg->cmd_addr_hi */
1537 	if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
1538 			      (u32)((u64)cmd_buf_pa >> 32))) {
1539 		dev_err(adapter->dev,
1540 			"Failed to write download cmd to boot code.\n");
1541 		ret = -1;
1542 		goto done;
1543 	}
1544 
1545 	/* Write the command length to reg->cmd_size */
1546 	if (mwifiex_write_reg(adapter, reg->cmd_size,
1547 			      card->cmd_buf->len)) {
1548 		dev_err(adapter->dev,
1549 			"Failed to write cmd len to reg->cmd_size\n");
1550 		ret = -1;
1551 		goto done;
1552 	}
1553 
1554 	/* Ring the door bell */
1555 	if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1556 			      CPU_INTR_DOOR_BELL)) {
1557 		dev_err(adapter->dev,
1558 			"Failed to assert door-bell intr\n");
1559 		ret = -1;
1560 		goto done;
1561 	}
1562 
1563 done:
1564 	if (ret)
1565 		adapter->cmd_sent = false;
1566 
1567 	return 0;
1568 }
1569 
1570 /*
1571  * This function handles command complete interrupt
1572  */
mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter * adapter)1573 static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
1574 {
1575 	struct pcie_service_card *card = adapter->card;
1576 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1577 	struct sk_buff *skb = card->cmdrsp_buf;
1578 	int count = 0;
1579 	u16 rx_len;
1580 	__le16 pkt_len;
1581 
1582 	dev_dbg(adapter->dev, "info: Rx CMD Response\n");
1583 
1584 	mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_FROMDEVICE);
1585 
1586 	/* Unmap the command as a response has been received. */
1587 	if (card->cmd_buf) {
1588 		mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
1589 					 PCI_DMA_TODEVICE);
1590 		dev_kfree_skb_any(card->cmd_buf);
1591 		card->cmd_buf = NULL;
1592 	}
1593 
1594 	pkt_len = *((__le16 *)skb->data);
1595 	rx_len = le16_to_cpu(pkt_len);
1596 	skb_trim(skb, rx_len);
1597 	skb_pull(skb, INTF_HEADER_LEN);
1598 
1599 	if (!adapter->curr_cmd) {
1600 		if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
1601 			mwifiex_process_sleep_confirm_resp(adapter, skb->data,
1602 							   skb->len);
1603 			mwifiex_pcie_enable_host_int(adapter);
1604 			if (mwifiex_write_reg(adapter,
1605 					      PCIE_CPU_INT_EVENT,
1606 					      CPU_INTR_SLEEP_CFM_DONE)) {
1607 				dev_warn(adapter->dev,
1608 					 "Write register failed\n");
1609 				return -1;
1610 			}
1611 			mwifiex_delay_for_sleep_cookie(adapter,
1612 						       MWIFIEX_MAX_DELAY_COUNT);
1613 			while (reg->sleep_cookie && (count++ < 10) &&
1614 			       mwifiex_pcie_ok_to_access_hw(adapter))
1615 				usleep_range(50, 60);
1616 		} else {
1617 			dev_err(adapter->dev,
1618 				"There is no command but got cmdrsp\n");
1619 		}
1620 		memcpy(adapter->upld_buf, skb->data,
1621 		       min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
1622 		skb_push(skb, INTF_HEADER_LEN);
1623 		if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1624 					   PCI_DMA_FROMDEVICE))
1625 			return -1;
1626 	} else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
1627 		adapter->curr_cmd->resp_skb = skb;
1628 		adapter->cmd_resp_received = true;
1629 		/* Take the pointer and set it to CMD node and will
1630 		   return in the response complete callback */
1631 		card->cmdrsp_buf = NULL;
1632 
1633 		/* Clear the cmd-rsp buffer address in scratch registers. This
1634 		   will prevent firmware from writing to the same response
1635 		   buffer again. */
1636 		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo, 0)) {
1637 			dev_err(adapter->dev,
1638 				"cmd_done: failed to clear cmd_rsp_addr_lo\n");
1639 			return -1;
1640 		}
1641 		/* Write the upper 32bits of the cmdrsp buffer physical
1642 		   address */
1643 		if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi, 0)) {
1644 			dev_err(adapter->dev,
1645 				"cmd_done: failed to clear cmd_rsp_addr_hi\n");
1646 			return -1;
1647 		}
1648 	}
1649 
1650 	return 0;
1651 }
1652 
1653 /*
1654  * Command Response processing complete handler
1655  */
mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter * adapter,struct sk_buff * skb)1656 static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
1657 					struct sk_buff *skb)
1658 {
1659 	struct pcie_service_card *card = adapter->card;
1660 
1661 	if (skb) {
1662 		card->cmdrsp_buf = skb;
1663 		skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
1664 		if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
1665 					   PCI_DMA_FROMDEVICE))
1666 			return -1;
1667 	}
1668 
1669 	return 0;
1670 }
1671 
1672 /*
1673  * This function handles firmware event ready interrupt
1674  */
mwifiex_pcie_process_event_ready(struct mwifiex_adapter * adapter)1675 static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
1676 {
1677 	struct pcie_service_card *card = adapter->card;
1678 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1679 	u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1680 	u32 wrptr, event;
1681 	struct mwifiex_evt_buf_desc *desc;
1682 
1683 	if (!mwifiex_pcie_ok_to_access_hw(adapter))
1684 		mwifiex_pm_wakeup_card(adapter);
1685 
1686 	if (adapter->event_received) {
1687 		dev_dbg(adapter->dev, "info: Event being processed, "
1688 			"do not process this interrupt just yet\n");
1689 		return 0;
1690 	}
1691 
1692 	if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1693 		dev_dbg(adapter->dev, "info: Invalid read pointer...\n");
1694 		return -1;
1695 	}
1696 
1697 	/* Read the event ring write pointer set by firmware */
1698 	if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1699 		dev_err(adapter->dev,
1700 			"EventReady: failed to read reg->evt_wrptr\n");
1701 		return -1;
1702 	}
1703 
1704 	dev_dbg(adapter->dev, "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
1705 		card->evtbd_rdptr, wrptr);
1706 	if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
1707 					      & MWIFIEX_EVTBD_MASK)) ||
1708 	    ((wrptr & reg->evt_rollover_ind) ==
1709 	     (card->evtbd_rdptr & reg->evt_rollover_ind))) {
1710 		struct sk_buff *skb_cmd;
1711 		__le16 data_len = 0;
1712 		u16 evt_len;
1713 
1714 		dev_dbg(adapter->dev, "info: Read Index: %d\n", rdptr);
1715 		skb_cmd = card->evt_buf_list[rdptr];
1716 		mwifiex_unmap_pci_memory(adapter, skb_cmd, PCI_DMA_FROMDEVICE);
1717 
1718 		/* Take the pointer and set it to event pointer in adapter
1719 		   and will return back after event handling callback */
1720 		card->evt_buf_list[rdptr] = NULL;
1721 		desc = card->evtbd_ring[rdptr];
1722 		memset(desc, 0, sizeof(*desc));
1723 
1724 		event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
1725 		adapter->event_cause = event;
1726 		/* The first 4bytes will be the event transfer header
1727 		   len is 2 bytes followed by type which is 2 bytes */
1728 		memcpy(&data_len, skb_cmd->data, sizeof(__le16));
1729 		evt_len = le16_to_cpu(data_len);
1730 
1731 		skb_pull(skb_cmd, INTF_HEADER_LEN);
1732 		dev_dbg(adapter->dev, "info: Event length: %d\n", evt_len);
1733 
1734 		if ((evt_len > 0) && (evt_len  < MAX_EVENT_SIZE))
1735 			memcpy(adapter->event_body, skb_cmd->data +
1736 			       MWIFIEX_EVENT_HEADER_LEN, evt_len -
1737 			       MWIFIEX_EVENT_HEADER_LEN);
1738 
1739 		adapter->event_received = true;
1740 		adapter->event_skb = skb_cmd;
1741 
1742 		/* Do not update the event read pointer here, wait till the
1743 		   buffer is released. This is just to make things simpler,
1744 		   we need to find a better method of managing these buffers.
1745 		*/
1746 	} else {
1747 		if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
1748 				      CPU_INTR_EVENT_DONE)) {
1749 			dev_warn(adapter->dev,
1750 				 "Write register failed\n");
1751 			return -1;
1752 		}
1753 	}
1754 
1755 	return 0;
1756 }
1757 
1758 /*
1759  * Event processing complete handler
1760  */
mwifiex_pcie_event_complete(struct mwifiex_adapter * adapter,struct sk_buff * skb)1761 static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
1762 				       struct sk_buff *skb)
1763 {
1764 	struct pcie_service_card *card = adapter->card;
1765 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1766 	int ret = 0;
1767 	u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
1768 	u32 wrptr;
1769 	struct mwifiex_evt_buf_desc *desc;
1770 
1771 	if (!skb)
1772 		return 0;
1773 
1774 	if (rdptr >= MWIFIEX_MAX_EVT_BD) {
1775 		dev_err(adapter->dev, "event_complete: Invalid rdptr 0x%x\n",
1776 			rdptr);
1777 		return -EINVAL;
1778 	}
1779 
1780 	/* Read the event ring write pointer set by firmware */
1781 	if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
1782 		dev_err(adapter->dev,
1783 			"event_complete: failed to read reg->evt_wrptr\n");
1784 		return -1;
1785 	}
1786 
1787 	if (!card->evt_buf_list[rdptr]) {
1788 		skb_push(skb, INTF_HEADER_LEN);
1789 		if (mwifiex_map_pci_memory(adapter, skb,
1790 					   MAX_EVENT_SIZE,
1791 					   PCI_DMA_FROMDEVICE))
1792 			return -1;
1793 		card->evt_buf_list[rdptr] = skb;
1794 		desc = card->evtbd_ring[rdptr];
1795 		desc->paddr = MWIFIEX_SKB_DMA_ADDR(skb);
1796 		desc->len = (u16)skb->len;
1797 		desc->flags = 0;
1798 		skb = NULL;
1799 	} else {
1800 		dev_dbg(adapter->dev,
1801 			"info: ERROR: buf still valid at index %d, <%p, %p>\n",
1802 			rdptr, card->evt_buf_list[rdptr], skb);
1803 	}
1804 
1805 	if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
1806 		card->evtbd_rdptr = ((card->evtbd_rdptr &
1807 					reg->evt_rollover_ind) ^
1808 					reg->evt_rollover_ind);
1809 	}
1810 
1811 	dev_dbg(adapter->dev, "info: Updated <Rd: 0x%x, Wr: 0x%x>",
1812 		card->evtbd_rdptr, wrptr);
1813 
1814 	/* Write the event ring read pointer in to reg->evt_rdptr */
1815 	if (mwifiex_write_reg(adapter, reg->evt_rdptr,
1816 			      card->evtbd_rdptr)) {
1817 		dev_err(adapter->dev,
1818 			"event_complete: failed to read reg->evt_rdptr\n");
1819 		return -1;
1820 	}
1821 
1822 	dev_dbg(adapter->dev, "info: Check Events Again\n");
1823 	ret = mwifiex_pcie_process_event_ready(adapter);
1824 
1825 	return ret;
1826 }
1827 
1828 /*
1829  * This function downloads the firmware to the card.
1830  *
1831  * Firmware is downloaded to the card in blocks. Every block download
1832  * is tested for CRC errors, and retried a number of times before
1833  * returning failure.
1834  */
mwifiex_prog_fw_w_helper(struct mwifiex_adapter * adapter,struct mwifiex_fw_image * fw)1835 static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1836 				    struct mwifiex_fw_image *fw)
1837 {
1838 	int ret;
1839 	u8 *firmware = fw->fw_buf;
1840 	u32 firmware_len = fw->fw_len;
1841 	u32 offset = 0;
1842 	struct sk_buff *skb;
1843 	u32 txlen, tx_blocks = 0, tries, len;
1844 	u32 block_retry_cnt = 0;
1845 	struct pcie_service_card *card = adapter->card;
1846 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1847 
1848 	if (!firmware || !firmware_len) {
1849 		dev_err(adapter->dev,
1850 			"No firmware image found! Terminating download\n");
1851 		return -1;
1852 	}
1853 
1854 	dev_dbg(adapter->dev, "info: Downloading FW image (%d bytes)\n",
1855 		firmware_len);
1856 
1857 	if (mwifiex_pcie_disable_host_int(adapter)) {
1858 		dev_err(adapter->dev,
1859 			"%s: Disabling interrupts failed.\n", __func__);
1860 		return -1;
1861 	}
1862 
1863 	skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
1864 	if (!skb) {
1865 		ret = -ENOMEM;
1866 		goto done;
1867 	}
1868 
1869 	/* Perform firmware data transfer */
1870 	do {
1871 		u32 ireg_intr = 0;
1872 
1873 		/* More data? */
1874 		if (offset >= firmware_len)
1875 			break;
1876 
1877 		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1878 			ret = mwifiex_read_reg(adapter, reg->cmd_size,
1879 					       &len);
1880 			if (ret) {
1881 				dev_warn(adapter->dev,
1882 					 "Failed reading len from boot code\n");
1883 				goto done;
1884 			}
1885 			if (len)
1886 				break;
1887 			usleep_range(10, 20);
1888 		}
1889 
1890 		if (!len) {
1891 			break;
1892 		} else if (len > MWIFIEX_UPLD_SIZE) {
1893 			pr_err("FW download failure @ %d, invalid length %d\n",
1894 			       offset, len);
1895 			ret = -1;
1896 			goto done;
1897 		}
1898 
1899 		txlen = len;
1900 
1901 		if (len & BIT(0)) {
1902 			block_retry_cnt++;
1903 			if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
1904 				pr_err("FW download failure @ %d, over max "
1905 				       "retry count\n", offset);
1906 				ret = -1;
1907 				goto done;
1908 			}
1909 			dev_err(adapter->dev, "FW CRC error indicated by the "
1910 				"helper: len = 0x%04X, txlen = %d\n",
1911 				len, txlen);
1912 			len &= ~BIT(0);
1913 			/* Setting this to 0 to resend from same offset */
1914 			txlen = 0;
1915 		} else {
1916 			block_retry_cnt = 0;
1917 			/* Set blocksize to transfer - checking for
1918 			   last block */
1919 			if (firmware_len - offset < txlen)
1920 				txlen = firmware_len - offset;
1921 
1922 			dev_dbg(adapter->dev, ".");
1923 
1924 			tx_blocks = (txlen + card->pcie.blksz_fw_dl - 1) /
1925 				    card->pcie.blksz_fw_dl;
1926 
1927 			/* Copy payload to buffer */
1928 			memmove(skb->data, &firmware[offset], txlen);
1929 		}
1930 
1931 		skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
1932 		skb_trim(skb, tx_blocks * card->pcie.blksz_fw_dl);
1933 
1934 		/* Send the boot command to device */
1935 		if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
1936 			dev_err(adapter->dev,
1937 				"Failed to send firmware download command\n");
1938 			ret = -1;
1939 			goto done;
1940 		}
1941 
1942 		/* Wait for the command done interrupt */
1943 		do {
1944 			if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
1945 					     &ireg_intr)) {
1946 				dev_err(adapter->dev, "%s: Failed to read "
1947 					"interrupt status during fw dnld.\n",
1948 					__func__);
1949 				mwifiex_unmap_pci_memory(adapter, skb,
1950 							 PCI_DMA_TODEVICE);
1951 				ret = -1;
1952 				goto done;
1953 			}
1954 		} while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
1955 			 CPU_INTR_DOOR_BELL);
1956 
1957 		mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
1958 
1959 		offset += txlen;
1960 	} while (true);
1961 
1962 	dev_dbg(adapter->dev, "info:\nFW download over, size %d bytes\n",
1963 		offset);
1964 
1965 	ret = 0;
1966 
1967 done:
1968 	dev_kfree_skb_any(skb);
1969 	return ret;
1970 }
1971 
1972 /*
1973  * This function checks the firmware status in card.
1974  *
1975  * The winner interface is also determined by this function.
1976  */
1977 static int
mwifiex_check_fw_status(struct mwifiex_adapter * adapter,u32 poll_num)1978 mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
1979 {
1980 	int ret = 0;
1981 	u32 firmware_stat, winner_status;
1982 	struct pcie_service_card *card = adapter->card;
1983 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
1984 	u32 tries;
1985 
1986 	/* Mask spurios interrupts */
1987 	if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
1988 			      HOST_INTR_MASK)) {
1989 		dev_warn(adapter->dev, "Write register failed\n");
1990 		return -1;
1991 	}
1992 
1993 	dev_dbg(adapter->dev, "Setting driver ready signature\n");
1994 	if (mwifiex_write_reg(adapter, reg->drv_rdy,
1995 			      FIRMWARE_READY_PCIE)) {
1996 		dev_err(adapter->dev,
1997 			"Failed to write driver ready signature\n");
1998 		return -1;
1999 	}
2000 
2001 	/* Wait for firmware initialization event */
2002 	for (tries = 0; tries < poll_num; tries++) {
2003 		if (mwifiex_read_reg(adapter, reg->fw_status,
2004 				     &firmware_stat))
2005 			ret = -1;
2006 		else
2007 			ret = 0;
2008 		if (ret)
2009 			continue;
2010 		if (firmware_stat == FIRMWARE_READY_PCIE) {
2011 			ret = 0;
2012 			break;
2013 		} else {
2014 			msleep(100);
2015 			ret = -1;
2016 		}
2017 	}
2018 
2019 	if (ret) {
2020 		if (mwifiex_read_reg(adapter, reg->fw_status,
2021 				     &winner_status))
2022 			ret = -1;
2023 		else if (!winner_status) {
2024 			dev_err(adapter->dev, "PCI-E is the winner\n");
2025 			adapter->winner = 1;
2026 		} else {
2027 			dev_err(adapter->dev,
2028 				"PCI-E is not the winner <%#x,%d>, exit dnld\n",
2029 				ret, adapter->winner);
2030 		}
2031 	}
2032 
2033 	return ret;
2034 }
2035 
2036 /*
2037  * This function reads the interrupt status from card.
2038  */
mwifiex_interrupt_status(struct mwifiex_adapter * adapter)2039 static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
2040 {
2041 	u32 pcie_ireg;
2042 	unsigned long flags;
2043 
2044 	if (!mwifiex_pcie_ok_to_access_hw(adapter))
2045 		return;
2046 
2047 	if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) {
2048 		dev_warn(adapter->dev, "Read register failed\n");
2049 		return;
2050 	}
2051 
2052 	if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2053 
2054 		mwifiex_pcie_disable_host_int(adapter);
2055 
2056 		/* Clear the pending interrupts */
2057 		if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
2058 				      ~pcie_ireg)) {
2059 			dev_warn(adapter->dev, "Write register failed\n");
2060 			return;
2061 		}
2062 		spin_lock_irqsave(&adapter->int_lock, flags);
2063 		adapter->int_status |= pcie_ireg;
2064 		spin_unlock_irqrestore(&adapter->int_lock, flags);
2065 
2066 		if (!adapter->pps_uapsd_mode &&
2067 		    adapter->ps_state == PS_STATE_SLEEP &&
2068 		    mwifiex_pcie_ok_to_access_hw(adapter)) {
2069 				/* Potentially for PCIe we could get other
2070 				 * interrupts like shared. Don't change power
2071 				 * state until cookie is set */
2072 				adapter->ps_state = PS_STATE_AWAKE;
2073 				adapter->pm_wakeup_fw_try = false;
2074 		}
2075 	}
2076 }
2077 
2078 /*
2079  * Interrupt handler for PCIe root port
2080  *
2081  * This function reads the interrupt status from firmware and assigns
2082  * the main process in workqueue which will handle the interrupt.
2083  */
mwifiex_pcie_interrupt(int irq,void * context)2084 static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
2085 {
2086 	struct pci_dev *pdev = (struct pci_dev *)context;
2087 	struct pcie_service_card *card;
2088 	struct mwifiex_adapter *adapter;
2089 
2090 	if (!pdev) {
2091 		pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev);
2092 		goto exit;
2093 	}
2094 
2095 	card = pci_get_drvdata(pdev);
2096 	if (!card || !card->adapter) {
2097 		pr_debug("info: %s: card=%p adapter=%p\n", __func__, card,
2098 			 card ? card->adapter : NULL);
2099 		goto exit;
2100 	}
2101 	adapter = card->adapter;
2102 
2103 	if (adapter->surprise_removed)
2104 		goto exit;
2105 
2106 	mwifiex_interrupt_status(adapter);
2107 	queue_work(adapter->workqueue, &adapter->main_work);
2108 
2109 exit:
2110 	return IRQ_HANDLED;
2111 }
2112 
2113 /*
2114  * This function checks the current interrupt status.
2115  *
2116  * The following interrupts are checked and handled by this function -
2117  *      - Data sent
2118  *      - Command sent
2119  *      - Command received
2120  *      - Packets received
2121  *      - Events received
2122  *
2123  * In case of Rx packets received, the packets are uploaded from card to
2124  * host and processed accordingly.
2125  */
mwifiex_process_int_status(struct mwifiex_adapter * adapter)2126 static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
2127 {
2128 	int ret;
2129 	u32 pcie_ireg;
2130 	unsigned long flags;
2131 
2132 	spin_lock_irqsave(&adapter->int_lock, flags);
2133 	/* Clear out unused interrupts */
2134 	pcie_ireg = adapter->int_status;
2135 	adapter->int_status = 0;
2136 	spin_unlock_irqrestore(&adapter->int_lock, flags);
2137 
2138 	while (pcie_ireg & HOST_INTR_MASK) {
2139 		if (pcie_ireg & HOST_INTR_DNLD_DONE) {
2140 			pcie_ireg &= ~HOST_INTR_DNLD_DONE;
2141 			dev_dbg(adapter->dev, "info: TX DNLD Done\n");
2142 			ret = mwifiex_pcie_send_data_complete(adapter);
2143 			if (ret)
2144 				return ret;
2145 		}
2146 		if (pcie_ireg & HOST_INTR_UPLD_RDY) {
2147 			pcie_ireg &= ~HOST_INTR_UPLD_RDY;
2148 			dev_dbg(adapter->dev, "info: Rx DATA\n");
2149 			ret = mwifiex_pcie_process_recv_data(adapter);
2150 			if (ret)
2151 				return ret;
2152 		}
2153 		if (pcie_ireg & HOST_INTR_EVENT_RDY) {
2154 			pcie_ireg &= ~HOST_INTR_EVENT_RDY;
2155 			dev_dbg(adapter->dev, "info: Rx EVENT\n");
2156 			ret = mwifiex_pcie_process_event_ready(adapter);
2157 			if (ret)
2158 				return ret;
2159 		}
2160 
2161 		if (pcie_ireg & HOST_INTR_CMD_DONE) {
2162 			pcie_ireg &= ~HOST_INTR_CMD_DONE;
2163 			if (adapter->cmd_sent) {
2164 				dev_dbg(adapter->dev,
2165 					"info: CMD sent Interrupt\n");
2166 				adapter->cmd_sent = false;
2167 			}
2168 			/* Handle command response */
2169 			ret = mwifiex_pcie_process_cmd_complete(adapter);
2170 			if (ret)
2171 				return ret;
2172 		}
2173 
2174 		if (mwifiex_pcie_ok_to_access_hw(adapter)) {
2175 			if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
2176 					     &pcie_ireg)) {
2177 				dev_warn(adapter->dev,
2178 					 "Read register failed\n");
2179 				return -1;
2180 			}
2181 
2182 			if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
2183 				if (mwifiex_write_reg(adapter,
2184 						      PCIE_HOST_INT_STATUS,
2185 						      ~pcie_ireg)) {
2186 					dev_warn(adapter->dev,
2187 						 "Write register failed\n");
2188 					return -1;
2189 				}
2190 			}
2191 
2192 		}
2193 	}
2194 	dev_dbg(adapter->dev, "info: cmd_sent=%d data_sent=%d\n",
2195 		adapter->cmd_sent, adapter->data_sent);
2196 	if (adapter->ps_state != PS_STATE_SLEEP)
2197 		mwifiex_pcie_enable_host_int(adapter);
2198 
2199 	return 0;
2200 }
2201 
2202 /*
2203  * This function downloads data from driver to card.
2204  *
2205  * Both commands and data packets are transferred to the card by this
2206  * function.
2207  *
2208  * This function adds the PCIE specific header to the front of the buffer
2209  * before transferring. The header contains the length of the packet and
2210  * the type. The firmware handles the packets based upon this set type.
2211  */
mwifiex_pcie_host_to_card(struct mwifiex_adapter * adapter,u8 type,struct sk_buff * skb,struct mwifiex_tx_param * tx_param)2212 static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
2213 				     struct sk_buff *skb,
2214 				     struct mwifiex_tx_param *tx_param)
2215 {
2216 	if (!skb) {
2217 		dev_err(adapter->dev, "Passed NULL skb to %s\n", __func__);
2218 		return -1;
2219 	}
2220 
2221 	if (type == MWIFIEX_TYPE_DATA)
2222 		return mwifiex_pcie_send_data(adapter, skb, tx_param);
2223 	else if (type == MWIFIEX_TYPE_CMD)
2224 		return mwifiex_pcie_send_cmd(adapter, skb);
2225 
2226 	return 0;
2227 }
2228 
2229 /* This function read/write firmware */
2230 static enum rdwr_status
mwifiex_pcie_rdwr_firmware(struct mwifiex_adapter * adapter,u8 doneflag)2231 mwifiex_pcie_rdwr_firmware(struct mwifiex_adapter *adapter, u8 doneflag)
2232 {
2233 	int ret, tries;
2234 	u8 ctrl_data;
2235 	struct pcie_service_card *card = adapter->card;
2236 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2237 
2238 	ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl, FW_DUMP_HOST_READY);
2239 	if (ret) {
2240 		dev_err(adapter->dev, "PCIE write err\n");
2241 		return RDWR_STATUS_FAILURE;
2242 	}
2243 
2244 	for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
2245 		mwifiex_read_reg_byte(adapter, reg->fw_dump_ctrl, &ctrl_data);
2246 		if (ctrl_data == FW_DUMP_DONE)
2247 			return RDWR_STATUS_SUCCESS;
2248 		if (doneflag && ctrl_data == doneflag)
2249 			return RDWR_STATUS_DONE;
2250 		if (ctrl_data != FW_DUMP_HOST_READY) {
2251 			dev_info(adapter->dev,
2252 				 "The ctrl reg was changed, re-try again!\n");
2253 			ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl,
2254 						FW_DUMP_HOST_READY);
2255 			if (ret) {
2256 				dev_err(adapter->dev, "PCIE write err\n");
2257 				return RDWR_STATUS_FAILURE;
2258 			}
2259 		}
2260 		usleep_range(100, 200);
2261 	}
2262 
2263 	dev_err(adapter->dev, "Fail to pull ctrl_data\n");
2264 	return RDWR_STATUS_FAILURE;
2265 }
2266 
2267 /* This function dump firmware memory to file */
mwifiex_pcie_fw_dump_work(struct mwifiex_adapter * adapter)2268 static void mwifiex_pcie_fw_dump_work(struct mwifiex_adapter *adapter)
2269 {
2270 	struct pcie_service_card *card = adapter->card;
2271 	const struct mwifiex_pcie_card_reg *creg = card->pcie.reg;
2272 	unsigned int reg, reg_start, reg_end;
2273 	u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
2274 	enum rdwr_status stat;
2275 	u32 memory_size;
2276 	int ret;
2277 	static char *env[] = { "DRIVER=mwifiex_pcie", "EVENT=fw_dump", NULL };
2278 
2279 	if (!card->pcie.supports_fw_dump)
2280 		return;
2281 
2282 	for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
2283 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2284 
2285 		if (entry->mem_ptr) {
2286 			vfree(entry->mem_ptr);
2287 			entry->mem_ptr = NULL;
2288 		}
2289 		entry->mem_size = 0;
2290 	}
2291 
2292 	dev_info(adapter->dev, "== mwifiex firmware dump start ==\n");
2293 
2294 	/* Read the number of the memories which will dump */
2295 	stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2296 	if (stat == RDWR_STATUS_FAILURE)
2297 		goto done;
2298 
2299 	reg = creg->fw_dump_start;
2300 	mwifiex_read_reg_byte(adapter, reg, &dump_num);
2301 
2302 	/* Read the length of every memory which will dump */
2303 	for (idx = 0; idx < dump_num; idx++) {
2304 		struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
2305 
2306 		stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2307 		if (stat == RDWR_STATUS_FAILURE)
2308 			goto done;
2309 
2310 		memory_size = 0;
2311 		reg = creg->fw_dump_start;
2312 		for (i = 0; i < 4; i++) {
2313 			mwifiex_read_reg_byte(adapter, reg, &read_reg);
2314 			memory_size |= (read_reg << (i * 8));
2315 			reg++;
2316 		}
2317 
2318 		if (memory_size == 0) {
2319 			dev_info(adapter->dev, "Firmware dump Finished!\n");
2320 			ret = mwifiex_write_reg(adapter, creg->fw_dump_ctrl,
2321 						FW_DUMP_READ_DONE);
2322 			if (ret) {
2323 				dev_err(adapter->dev, "PCIE write err\n");
2324 				goto done;
2325 			}
2326 			break;
2327 		}
2328 
2329 		dev_info(adapter->dev,
2330 			 "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
2331 		entry->mem_ptr = vmalloc(memory_size + 1);
2332 		entry->mem_size = memory_size;
2333 		if (!entry->mem_ptr) {
2334 			dev_err(adapter->dev,
2335 				"Vmalloc %s failed\n", entry->mem_name);
2336 			goto done;
2337 		}
2338 		dbg_ptr = entry->mem_ptr;
2339 		end_ptr = dbg_ptr + memory_size;
2340 
2341 		doneflag = entry->done_flag;
2342 		dev_info(adapter->dev, "Start %s output, please wait...\n",
2343 			 entry->mem_name);
2344 
2345 		do {
2346 			stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
2347 			if (RDWR_STATUS_FAILURE == stat)
2348 				goto done;
2349 
2350 			reg_start = creg->fw_dump_start;
2351 			reg_end = creg->fw_dump_end;
2352 			for (reg = reg_start; reg <= reg_end; reg++) {
2353 				mwifiex_read_reg_byte(adapter, reg, dbg_ptr);
2354 				if (dbg_ptr < end_ptr) {
2355 					dbg_ptr++;
2356 				} else {
2357 					dev_err(adapter->dev,
2358 						"Allocated buf not enough\n");
2359 					goto done;
2360 				}
2361 			}
2362 
2363 			if (stat != RDWR_STATUS_DONE)
2364 				continue;
2365 
2366 			dev_info(adapter->dev, "%s done: size=0x%tx\n",
2367 				 entry->mem_name, dbg_ptr - entry->mem_ptr);
2368 			break;
2369 		} while (true);
2370 	}
2371 	dev_info(adapter->dev, "== mwifiex firmware dump end ==\n");
2372 
2373 	kobject_uevent_env(&adapter->wiphy->dev.kobj, KOBJ_CHANGE, env);
2374 
2375 done:
2376 	adapter->curr_mem_idx = 0;
2377 }
2378 
mwifiex_pcie_work(struct work_struct * work)2379 static void mwifiex_pcie_work(struct work_struct *work)
2380 {
2381 	struct mwifiex_adapter *adapter =
2382 			container_of(work, struct mwifiex_adapter, iface_work);
2383 
2384 	if (test_and_clear_bit(MWIFIEX_IFACE_WORK_FW_DUMP,
2385 			       &adapter->iface_work_flags))
2386 		mwifiex_pcie_fw_dump_work(adapter);
2387 }
2388 
2389 /* This function dumps FW information */
mwifiex_pcie_fw_dump(struct mwifiex_adapter * adapter)2390 static void mwifiex_pcie_fw_dump(struct mwifiex_adapter *adapter)
2391 {
2392 	if (test_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &adapter->iface_work_flags))
2393 		return;
2394 
2395 	set_bit(MWIFIEX_IFACE_WORK_FW_DUMP, &adapter->iface_work_flags);
2396 
2397 	schedule_work(&adapter->iface_work);
2398 }
2399 
2400 /*
2401  * This function initializes the PCI-E host memory space, WCB rings, etc.
2402  *
2403  * The following initializations steps are followed -
2404  *      - Allocate TXBD ring buffers
2405  *      - Allocate RXBD ring buffers
2406  *      - Allocate event BD ring buffers
2407  *      - Allocate command response ring buffer
2408  *      - Allocate sleep cookie buffer
2409  */
mwifiex_pcie_init(struct mwifiex_adapter * adapter)2410 static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
2411 {
2412 	struct pcie_service_card *card = adapter->card;
2413 	int ret;
2414 	struct pci_dev *pdev = card->dev;
2415 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2416 
2417 	pci_set_drvdata(pdev, card);
2418 
2419 	ret = pci_enable_device(pdev);
2420 	if (ret)
2421 		goto err_enable_dev;
2422 
2423 	pci_set_master(pdev);
2424 
2425 	dev_dbg(adapter->dev, "try set_consistent_dma_mask(32)\n");
2426 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2427 	if (ret) {
2428 		dev_err(adapter->dev, "set_dma_mask(32) failed\n");
2429 		goto err_set_dma_mask;
2430 	}
2431 
2432 	ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2433 	if (ret) {
2434 		dev_err(adapter->dev, "set_consistent_dma_mask(64) failed\n");
2435 		goto err_set_dma_mask;
2436 	}
2437 
2438 	ret = pci_request_region(pdev, 0, DRV_NAME);
2439 	if (ret) {
2440 		dev_err(adapter->dev, "req_reg(0) error\n");
2441 		goto err_req_region0;
2442 	}
2443 	card->pci_mmap = pci_iomap(pdev, 0, 0);
2444 	if (!card->pci_mmap) {
2445 		dev_err(adapter->dev, "iomap(0) error\n");
2446 		ret = -EIO;
2447 		goto err_iomap0;
2448 	}
2449 	ret = pci_request_region(pdev, 2, DRV_NAME);
2450 	if (ret) {
2451 		dev_err(adapter->dev, "req_reg(2) error\n");
2452 		goto err_req_region2;
2453 	}
2454 	card->pci_mmap1 = pci_iomap(pdev, 2, 0);
2455 	if (!card->pci_mmap1) {
2456 		dev_err(adapter->dev, "iomap(2) error\n");
2457 		ret = -EIO;
2458 		goto err_iomap2;
2459 	}
2460 
2461 	dev_dbg(adapter->dev,
2462 		"PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
2463 		card->pci_mmap, card->pci_mmap1);
2464 
2465 	card->cmdrsp_buf = NULL;
2466 	ret = mwifiex_pcie_create_txbd_ring(adapter);
2467 	if (ret)
2468 		goto err_cre_txbd;
2469 	ret = mwifiex_pcie_create_rxbd_ring(adapter);
2470 	if (ret)
2471 		goto err_cre_rxbd;
2472 	ret = mwifiex_pcie_create_evtbd_ring(adapter);
2473 	if (ret)
2474 		goto err_cre_evtbd;
2475 	ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
2476 	if (ret)
2477 		goto err_alloc_cmdbuf;
2478 	if (reg->sleep_cookie) {
2479 		ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
2480 		if (ret)
2481 			goto err_alloc_cookie;
2482 	} else {
2483 		card->sleep_cookie_vbase = NULL;
2484 	}
2485 	return ret;
2486 
2487 err_alloc_cookie:
2488 	mwifiex_pcie_delete_cmdrsp_buf(adapter);
2489 err_alloc_cmdbuf:
2490 	mwifiex_pcie_delete_evtbd_ring(adapter);
2491 err_cre_evtbd:
2492 	mwifiex_pcie_delete_rxbd_ring(adapter);
2493 err_cre_rxbd:
2494 	mwifiex_pcie_delete_txbd_ring(adapter);
2495 err_cre_txbd:
2496 	pci_iounmap(pdev, card->pci_mmap1);
2497 err_iomap2:
2498 	pci_release_region(pdev, 2);
2499 err_req_region2:
2500 	pci_iounmap(pdev, card->pci_mmap);
2501 err_iomap0:
2502 	pci_release_region(pdev, 0);
2503 err_req_region0:
2504 err_set_dma_mask:
2505 	pci_disable_device(pdev);
2506 err_enable_dev:
2507 	pci_set_drvdata(pdev, NULL);
2508 	return ret;
2509 }
2510 
2511 /*
2512  * This function cleans up the allocated card buffers.
2513  *
2514  * The following are freed by this function -
2515  *      - TXBD ring buffers
2516  *      - RXBD ring buffers
2517  *      - Event BD ring buffers
2518  *      - Command response ring buffer
2519  *      - Sleep cookie buffer
2520  */
mwifiex_pcie_cleanup(struct mwifiex_adapter * adapter)2521 static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
2522 {
2523 	struct pcie_service_card *card = adapter->card;
2524 	struct pci_dev *pdev = card->dev;
2525 	const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
2526 
2527 	if (user_rmmod) {
2528 		dev_dbg(adapter->dev, "Clearing driver ready signature\n");
2529 		if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
2530 			dev_err(adapter->dev,
2531 				"Failed to write driver not-ready signature\n");
2532 	}
2533 
2534 	if (pdev) {
2535 		pci_iounmap(pdev, card->pci_mmap);
2536 		pci_iounmap(pdev, card->pci_mmap1);
2537 		pci_disable_device(pdev);
2538 		pci_release_region(pdev, 2);
2539 		pci_release_region(pdev, 0);
2540 		pci_set_drvdata(pdev, NULL);
2541 	}
2542 	kfree(card);
2543 }
2544 
2545 /*
2546  * This function registers the PCIE device.
2547  *
2548  * PCIE IRQ is claimed, block size is set and driver data is initialized.
2549  */
mwifiex_register_dev(struct mwifiex_adapter * adapter)2550 static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
2551 {
2552 	int ret;
2553 	struct pcie_service_card *card = adapter->card;
2554 	struct pci_dev *pdev = card->dev;
2555 
2556 	/* save adapter pointer in card */
2557 	card->adapter = adapter;
2558 
2559 	ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
2560 			  "MRVL_PCIE", pdev);
2561 	if (ret) {
2562 		pr_err("request_irq failed: ret=%d\n", ret);
2563 		adapter->card = NULL;
2564 		return -1;
2565 	}
2566 
2567 	adapter->dev = &pdev->dev;
2568 	adapter->tx_buf_size = card->pcie.tx_buf_size;
2569 	adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
2570 	adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
2571 	strcpy(adapter->fw_name, card->pcie.firmware);
2572 
2573 	return 0;
2574 }
2575 
2576 /*
2577  * This function unregisters the PCIE device.
2578  *
2579  * The PCIE IRQ is released, the function is disabled and driver
2580  * data is set to null.
2581  */
mwifiex_unregister_dev(struct mwifiex_adapter * adapter)2582 static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
2583 {
2584 	struct pcie_service_card *card = adapter->card;
2585 	const struct mwifiex_pcie_card_reg *reg;
2586 
2587 	if (card) {
2588 		dev_dbg(adapter->dev, "%s(): calling free_irq()\n", __func__);
2589 		free_irq(card->dev->irq, card->dev);
2590 
2591 		reg = card->pcie.reg;
2592 		if (reg->sleep_cookie)
2593 			mwifiex_pcie_delete_sleep_cookie_buf(adapter);
2594 
2595 		mwifiex_pcie_delete_cmdrsp_buf(adapter);
2596 		mwifiex_pcie_delete_evtbd_ring(adapter);
2597 		mwifiex_pcie_delete_rxbd_ring(adapter);
2598 		mwifiex_pcie_delete_txbd_ring(adapter);
2599 		card->cmdrsp_buf = NULL;
2600 	}
2601 }
2602 
2603 static struct mwifiex_if_ops pcie_ops = {
2604 	.init_if =			mwifiex_pcie_init,
2605 	.cleanup_if =			mwifiex_pcie_cleanup,
2606 	.check_fw_status =		mwifiex_check_fw_status,
2607 	.prog_fw =			mwifiex_prog_fw_w_helper,
2608 	.register_dev =			mwifiex_register_dev,
2609 	.unregister_dev =		mwifiex_unregister_dev,
2610 	.enable_int =			mwifiex_pcie_enable_host_int,
2611 	.process_int_status =		mwifiex_process_int_status,
2612 	.host_to_card =			mwifiex_pcie_host_to_card,
2613 	.wakeup =			mwifiex_pm_wakeup_card,
2614 	.wakeup_complete =		mwifiex_pm_wakeup_card_complete,
2615 
2616 	/* PCIE specific */
2617 	.cmdrsp_complete =		mwifiex_pcie_cmdrsp_complete,
2618 	.event_complete =		mwifiex_pcie_event_complete,
2619 	.update_mp_end_port =		NULL,
2620 	.cleanup_mpa_buf =		NULL,
2621 	.init_fw_port =			mwifiex_pcie_init_fw_port,
2622 	.clean_pcie_ring =		mwifiex_clean_pcie_ring_buf,
2623 	.fw_dump =			mwifiex_pcie_fw_dump,
2624 	.iface_work =			mwifiex_pcie_work,
2625 };
2626 
2627 /*
2628  * This function initializes the PCIE driver module.
2629  *
2630  * This initiates the semaphore and registers the device with
2631  * PCIE bus.
2632  */
mwifiex_pcie_init_module(void)2633 static int mwifiex_pcie_init_module(void)
2634 {
2635 	int ret;
2636 
2637 	pr_debug("Marvell PCIe Driver\n");
2638 
2639 	sema_init(&add_remove_card_sem, 1);
2640 
2641 	/* Clear the flag in case user removes the card. */
2642 	user_rmmod = 0;
2643 
2644 	ret = pci_register_driver(&mwifiex_pcie);
2645 	if (ret)
2646 		pr_err("Driver register failed!\n");
2647 	else
2648 		pr_debug("info: Driver registered successfully!\n");
2649 
2650 	return ret;
2651 }
2652 
2653 /*
2654  * This function cleans up the PCIE driver.
2655  *
2656  * The following major steps are followed for cleanup -
2657  *      - Resume the device if its suspended
2658  *      - Disconnect the device if connected
2659  *      - Shutdown the firmware
2660  *      - Unregister the device from PCIE bus.
2661  */
mwifiex_pcie_cleanup_module(void)2662 static void mwifiex_pcie_cleanup_module(void)
2663 {
2664 	if (!down_interruptible(&add_remove_card_sem))
2665 		up(&add_remove_card_sem);
2666 
2667 	/* Set the flag as user is removing this module. */
2668 	user_rmmod = 1;
2669 
2670 	pci_unregister_driver(&mwifiex_pcie);
2671 }
2672 
2673 module_init(mwifiex_pcie_init_module);
2674 module_exit(mwifiex_pcie_cleanup_module);
2675 
2676 MODULE_AUTHOR("Marvell International Ltd.");
2677 MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
2678 MODULE_VERSION(PCIE_VERSION);
2679 MODULE_LICENSE("GPL v2");
2680 MODULE_FIRMWARE(PCIE8766_DEFAULT_FW_NAME);
2681 MODULE_FIRMWARE(PCIE8897_DEFAULT_FW_NAME);
2682