xref: /linux/drivers/net/ethernet/cavium/liquidio/lio_main.c (revision f412eed9dfdeeb6becd7de2ffe8b5d0a8b3f81ca)
1 /**********************************************************************
2  * Author: Cavium, Inc.
3  *
4  * Contact: support@cavium.com
5  *          Please include "LiquidIO" in the subject.
6  *
7  * Copyright (c) 2003-2016 Cavium, Inc.
8  *
9  * This file is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License, Version 2, as
11  * published by the Free Software Foundation.
12  *
13  * This file is distributed in the hope that it will be useful, but
14  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16  * NONINFRINGEMENT.  See the GNU General Public License for more details.
17  ***********************************************************************/
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/pci.h>
21 #include <linux/firmware.h>
22 #include <net/vxlan.h>
23 #include <linux/kthread.h>
24 #include <net/switchdev.h>
25 #include "liquidio_common.h"
26 #include "octeon_droq.h"
27 #include "octeon_iq.h"
28 #include "response_manager.h"
29 #include "octeon_device.h"
30 #include "octeon_nic.h"
31 #include "octeon_main.h"
32 #include "octeon_network.h"
33 #include "cn66xx_regs.h"
34 #include "cn66xx_device.h"
35 #include "cn68xx_device.h"
36 #include "cn23xx_pf_device.h"
37 #include "liquidio_image.h"
38 #include "lio_vf_rep.h"
39 
40 MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
41 MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
42 MODULE_LICENSE("GPL");
43 MODULE_VERSION(LIQUIDIO_VERSION);
44 MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME
45 		"_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
46 MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME
47 		"_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
48 MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME
49 		"_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
50 MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_23XX_NAME
51 		"_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
52 
53 static int ddr_timeout = 10000;
54 module_param(ddr_timeout, int, 0644);
55 MODULE_PARM_DESC(ddr_timeout,
56 		 "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
57 
58 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
59 
60 static int debug = -1;
61 module_param(debug, int, 0644);
62 MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
63 
64 static char fw_type[LIO_MAX_FW_TYPE_LEN] = LIO_FW_NAME_TYPE_AUTO;
65 module_param_string(fw_type, fw_type, sizeof(fw_type), 0444);
66 MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded (default is \"auto\"), which uses firmware in flash, if present, else loads \"nic\".");
67 
68 static u32 console_bitmask;
69 module_param(console_bitmask, int, 0644);
70 MODULE_PARM_DESC(console_bitmask,
71 		 "Bitmask indicating which consoles have debug output redirected to syslog.");
72 
73 /**
74  * \brief determines if a given console has debug enabled.
75  * @param console console to check
76  * @returns  1 = enabled. 0 otherwise
77  */
78 static int octeon_console_debug_enabled(u32 console)
79 {
80 	return (console_bitmask >> (console)) & 0x1;
81 }
82 
83 /* Polling interval for determining when NIC application is alive */
84 #define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
85 
86 /* runtime link query interval */
87 #define LIQUIDIO_LINK_QUERY_INTERVAL_MS         1000
88 /* update localtime to octeon firmware every 60 seconds.
89  * make firmware to use same time reference, so that it will be easy to
90  * correlate firmware logged events/errors with host events, for debugging.
91  */
92 #define LIO_SYNC_OCTEON_TIME_INTERVAL_MS 60000
93 
94 struct lio_trusted_vf_ctx {
95 	struct completion complete;
96 	int status;
97 };
98 
99 struct liquidio_rx_ctl_context {
100 	int octeon_id;
101 
102 	wait_queue_head_t wc;
103 
104 	int cond;
105 };
106 
107 struct oct_link_status_resp {
108 	u64 rh;
109 	struct oct_link_info link_info;
110 	u64 status;
111 };
112 
113 struct oct_timestamp_resp {
114 	u64 rh;
115 	u64 timestamp;
116 	u64 status;
117 };
118 
119 #define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
120 
121 union tx_info {
122 	u64 u64;
123 	struct {
124 #ifdef __BIG_ENDIAN_BITFIELD
125 		u16 gso_size;
126 		u16 gso_segs;
127 		u32 reserved;
128 #else
129 		u32 reserved;
130 		u16 gso_segs;
131 		u16 gso_size;
132 #endif
133 	} s;
134 };
135 
136 /** Octeon device properties to be used by the NIC module.
137  * Each octeon device in the system will be represented
138  * by this structure in the NIC module.
139  */
140 
141 #define OCTNIC_MAX_SG  (MAX_SKB_FRAGS)
142 
143 #define OCTNIC_GSO_MAX_HEADER_SIZE 128
144 #define OCTNIC_GSO_MAX_SIZE                                                    \
145 	(CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
146 
147 /** Structure of a node in list of gather components maintained by
148  * NIC driver for each network device.
149  */
150 struct octnic_gather {
151 	/** List manipulation. Next and prev pointers. */
152 	struct list_head list;
153 
154 	/** Size of the gather component at sg in bytes. */
155 	int sg_size;
156 
157 	/** Number of bytes that sg was adjusted to make it 8B-aligned. */
158 	int adjust;
159 
160 	/** Gather component that can accommodate max sized fragment list
161 	 *  received from the IP layer.
162 	 */
163 	struct octeon_sg_entry *sg;
164 
165 	dma_addr_t sg_dma_ptr;
166 };
167 
168 struct handshake {
169 	struct completion init;
170 	struct completion started;
171 	struct pci_dev *pci_dev;
172 	int init_ok;
173 	int started_ok;
174 };
175 
176 #ifdef CONFIG_PCI_IOV
177 static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
178 #endif
179 
180 static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
181 				    char *prefix, char *suffix);
182 
183 static int octeon_device_init(struct octeon_device *);
184 static int liquidio_stop(struct net_device *netdev);
185 static void liquidio_remove(struct pci_dev *pdev);
186 static int liquidio_probe(struct pci_dev *pdev,
187 			  const struct pci_device_id *ent);
188 static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
189 				      int linkstate);
190 
191 static struct handshake handshake[MAX_OCTEON_DEVICES];
192 static struct completion first_stage;
193 
194 static void octeon_droq_bh(unsigned long pdev)
195 {
196 	int q_no;
197 	int reschedule = 0;
198 	struct octeon_device *oct = (struct octeon_device *)pdev;
199 	struct octeon_device_priv *oct_priv =
200 		(struct octeon_device_priv *)oct->priv;
201 
202 	for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
203 		if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
204 			continue;
205 		reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
206 							  MAX_PACKET_BUDGET);
207 		lio_enable_irq(oct->droq[q_no], NULL);
208 
209 		if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
210 			/* set time and cnt interrupt thresholds for this DROQ
211 			 * for NAPI
212 			 */
213 			int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
214 
215 			octeon_write_csr64(
216 			    oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
217 			    0x5700000040ULL);
218 			octeon_write_csr64(
219 			    oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
220 		}
221 	}
222 
223 	if (reschedule)
224 		tasklet_schedule(&oct_priv->droq_tasklet);
225 }
226 
227 static int lio_wait_for_oq_pkts(struct octeon_device *oct)
228 {
229 	struct octeon_device_priv *oct_priv =
230 		(struct octeon_device_priv *)oct->priv;
231 	int retry = 100, pkt_cnt = 0, pending_pkts = 0;
232 	int i;
233 
234 	do {
235 		pending_pkts = 0;
236 
237 		for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
238 			if (!(oct->io_qmask.oq & BIT_ULL(i)))
239 				continue;
240 			pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
241 		}
242 		if (pkt_cnt > 0) {
243 			pending_pkts += pkt_cnt;
244 			tasklet_schedule(&oct_priv->droq_tasklet);
245 		}
246 		pkt_cnt = 0;
247 		schedule_timeout_uninterruptible(1);
248 
249 	} while (retry-- && pending_pkts);
250 
251 	return pkt_cnt;
252 }
253 
254 /**
255  * \brief Forces all IO queues off on a given device
256  * @param oct Pointer to Octeon device
257  */
258 static void force_io_queues_off(struct octeon_device *oct)
259 {
260 	if ((oct->chip_id == OCTEON_CN66XX) ||
261 	    (oct->chip_id == OCTEON_CN68XX)) {
262 		/* Reset the Enable bits for Input Queues. */
263 		octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
264 
265 		/* Reset the Enable bits for Output Queues. */
266 		octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
267 	}
268 }
269 
270 /**
271  * \brief Cause device to go quiet so it can be safely removed/reset/etc
272  * @param oct Pointer to Octeon device
273  */
274 static inline void pcierror_quiesce_device(struct octeon_device *oct)
275 {
276 	int i;
277 
278 	/* Disable the input and output queues now. No more packets will
279 	 * arrive from Octeon, but we should wait for all packet processing
280 	 * to finish.
281 	 */
282 	force_io_queues_off(oct);
283 
284 	/* To allow for in-flight requests */
285 	schedule_timeout_uninterruptible(100);
286 
287 	if (wait_for_pending_requests(oct))
288 		dev_err(&oct->pci_dev->dev, "There were pending requests\n");
289 
290 	/* Force all requests waiting to be fetched by OCTEON to complete. */
291 	for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
292 		struct octeon_instr_queue *iq;
293 
294 		if (!(oct->io_qmask.iq & BIT_ULL(i)))
295 			continue;
296 		iq = oct->instr_queue[i];
297 
298 		if (atomic_read(&iq->instr_pending)) {
299 			spin_lock_bh(&iq->lock);
300 			iq->fill_cnt = 0;
301 			iq->octeon_read_index = iq->host_write_index;
302 			iq->stats.instr_processed +=
303 				atomic_read(&iq->instr_pending);
304 			lio_process_iq_request_list(oct, iq, 0);
305 			spin_unlock_bh(&iq->lock);
306 		}
307 	}
308 
309 	/* Force all pending ordered list requests to time out. */
310 	lio_process_ordered_list(oct, 1);
311 
312 	/* We do not need to wait for output queue packets to be processed. */
313 }
314 
315 /**
316  * \brief Cleanup PCI AER uncorrectable error status
317  * @param dev Pointer to PCI device
318  */
319 static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
320 {
321 	int pos = 0x100;
322 	u32 status, mask;
323 
324 	pr_info("%s :\n", __func__);
325 
326 	pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
327 	pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
328 	if (dev->error_state == pci_channel_io_normal)
329 		status &= ~mask;        /* Clear corresponding nonfatal bits */
330 	else
331 		status &= mask;         /* Clear corresponding fatal bits */
332 	pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
333 }
334 
335 /**
336  * \brief Stop all PCI IO to a given device
337  * @param dev Pointer to Octeon device
338  */
339 static void stop_pci_io(struct octeon_device *oct)
340 {
341 	/* No more instructions will be forwarded. */
342 	atomic_set(&oct->status, OCT_DEV_IN_RESET);
343 
344 	pci_disable_device(oct->pci_dev);
345 
346 	/* Disable interrupts  */
347 	oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
348 
349 	pcierror_quiesce_device(oct);
350 
351 	/* Release the interrupt line */
352 	free_irq(oct->pci_dev->irq, oct);
353 
354 	if (oct->flags & LIO_FLAG_MSI_ENABLED)
355 		pci_disable_msi(oct->pci_dev);
356 
357 	dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
358 		lio_get_state_string(&oct->status));
359 
360 	/* making it a common function for all OCTEON models */
361 	cleanup_aer_uncorrect_error_status(oct->pci_dev);
362 }
363 
364 /**
365  * \brief called when PCI error is detected
366  * @param pdev Pointer to PCI device
367  * @param state The current pci connection state
368  *
369  * This function is called after a PCI bus error affecting
370  * this device has been detected.
371  */
372 static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
373 						     pci_channel_state_t state)
374 {
375 	struct octeon_device *oct = pci_get_drvdata(pdev);
376 
377 	/* Non-correctable Non-fatal errors */
378 	if (state == pci_channel_io_normal) {
379 		dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
380 		cleanup_aer_uncorrect_error_status(oct->pci_dev);
381 		return PCI_ERS_RESULT_CAN_RECOVER;
382 	}
383 
384 	/* Non-correctable Fatal errors */
385 	dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
386 	stop_pci_io(oct);
387 
388 	/* Always return a DISCONNECT. There is no support for recovery but only
389 	 * for a clean shutdown.
390 	 */
391 	return PCI_ERS_RESULT_DISCONNECT;
392 }
393 
394 /**
395  * \brief mmio handler
396  * @param pdev Pointer to PCI device
397  */
398 static pci_ers_result_t liquidio_pcie_mmio_enabled(
399 				struct pci_dev *pdev __attribute__((unused)))
400 {
401 	/* We should never hit this since we never ask for a reset for a Fatal
402 	 * Error. We always return DISCONNECT in io_error above.
403 	 * But play safe and return RECOVERED for now.
404 	 */
405 	return PCI_ERS_RESULT_RECOVERED;
406 }
407 
408 /**
409  * \brief called after the pci bus has been reset.
410  * @param pdev Pointer to PCI device
411  *
412  * Restart the card from scratch, as if from a cold-boot. Implementation
413  * resembles the first-half of the octeon_resume routine.
414  */
415 static pci_ers_result_t liquidio_pcie_slot_reset(
416 				struct pci_dev *pdev __attribute__((unused)))
417 {
418 	/* We should never hit this since we never ask for a reset for a Fatal
419 	 * Error. We always return DISCONNECT in io_error above.
420 	 * But play safe and return RECOVERED for now.
421 	 */
422 	return PCI_ERS_RESULT_RECOVERED;
423 }
424 
425 /**
426  * \brief called when traffic can start flowing again.
427  * @param pdev Pointer to PCI device
428  *
429  * This callback is called when the error recovery driver tells us that
430  * its OK to resume normal operation. Implementation resembles the
431  * second-half of the octeon_resume routine.
432  */
433 static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
434 {
435 	/* Nothing to be done here. */
436 }
437 
438 #ifdef CONFIG_PM
439 /**
440  * \brief called when suspending
441  * @param pdev Pointer to PCI device
442  * @param state state to suspend to
443  */
444 static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
445 			    pm_message_t state __attribute__((unused)))
446 {
447 	return 0;
448 }
449 
450 /**
451  * \brief called when resuming
452  * @param pdev Pointer to PCI device
453  */
454 static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
455 {
456 	return 0;
457 }
458 #endif
459 
460 /* For PCI-E Advanced Error Recovery (AER) Interface */
461 static const struct pci_error_handlers liquidio_err_handler = {
462 	.error_detected = liquidio_pcie_error_detected,
463 	.mmio_enabled	= liquidio_pcie_mmio_enabled,
464 	.slot_reset	= liquidio_pcie_slot_reset,
465 	.resume		= liquidio_pcie_resume,
466 };
467 
468 static const struct pci_device_id liquidio_pci_tbl[] = {
469 	{       /* 68xx */
470 		PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
471 	},
472 	{       /* 66xx */
473 		PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
474 	},
475 	{       /* 23xx pf */
476 		PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
477 	},
478 	{
479 		0, 0, 0, 0, 0, 0, 0
480 	}
481 };
482 MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
483 
484 static struct pci_driver liquidio_pci_driver = {
485 	.name		= "LiquidIO",
486 	.id_table	= liquidio_pci_tbl,
487 	.probe		= liquidio_probe,
488 	.remove		= liquidio_remove,
489 	.err_handler	= &liquidio_err_handler,    /* For AER */
490 
491 #ifdef CONFIG_PM
492 	.suspend	= liquidio_suspend,
493 	.resume		= liquidio_resume,
494 #endif
495 #ifdef CONFIG_PCI_IOV
496 	.sriov_configure = liquidio_enable_sriov,
497 #endif
498 };
499 
500 /**
501  * \brief register PCI driver
502  */
503 static int liquidio_init_pci(void)
504 {
505 	return pci_register_driver(&liquidio_pci_driver);
506 }
507 
508 /**
509  * \brief unregister PCI driver
510  */
511 static void liquidio_deinit_pci(void)
512 {
513 	pci_unregister_driver(&liquidio_pci_driver);
514 }
515 
516 /**
517  * \brief Check Tx queue status, and take appropriate action
518  * @param lio per-network private data
519  * @returns 0 if full, number of queues woken up otherwise
520  */
521 static inline int check_txq_status(struct lio *lio)
522 {
523 	int numqs = lio->netdev->num_tx_queues;
524 	int ret_val = 0;
525 	int q, iq;
526 
527 	/* check each sub-queue state */
528 	for (q = 0; q < numqs; q++) {
529 		iq = lio->linfo.txpciq[q %
530 			lio->oct_dev->num_iqs].s.q_no;
531 		if (octnet_iq_is_full(lio->oct_dev, iq))
532 			continue;
533 		if (__netif_subqueue_stopped(lio->netdev, q)) {
534 			netif_wake_subqueue(lio->netdev, q);
535 			INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
536 						  tx_restart, 1);
537 			ret_val++;
538 		}
539 	}
540 
541 	return ret_val;
542 }
543 
544 /**
545  * Remove the node at the head of the list. The list would be empty at
546  * the end of this call if there are no more nodes in the list.
547  */
548 static inline struct list_head *list_delete_head(struct list_head *root)
549 {
550 	struct list_head *node;
551 
552 	if ((root->prev == root) && (root->next == root))
553 		node = NULL;
554 	else
555 		node = root->next;
556 
557 	if (node)
558 		list_del(node);
559 
560 	return node;
561 }
562 
563 /**
564  * \brief Delete gather lists
565  * @param lio per-network private data
566  */
567 static void delete_glists(struct lio *lio)
568 {
569 	struct octnic_gather *g;
570 	int i;
571 
572 	kfree(lio->glist_lock);
573 	lio->glist_lock = NULL;
574 
575 	if (!lio->glist)
576 		return;
577 
578 	for (i = 0; i < lio->linfo.num_txpciq; i++) {
579 		do {
580 			g = (struct octnic_gather *)
581 				list_delete_head(&lio->glist[i]);
582 			if (g)
583 				kfree(g);
584 		} while (g);
585 
586 		if (lio->glists_virt_base && lio->glists_virt_base[i] &&
587 		    lio->glists_dma_base && lio->glists_dma_base[i]) {
588 			lio_dma_free(lio->oct_dev,
589 				     lio->glist_entry_size * lio->tx_qsize,
590 				     lio->glists_virt_base[i],
591 				     lio->glists_dma_base[i]);
592 		}
593 	}
594 
595 	kfree(lio->glists_virt_base);
596 	lio->glists_virt_base = NULL;
597 
598 	kfree(lio->glists_dma_base);
599 	lio->glists_dma_base = NULL;
600 
601 	kfree(lio->glist);
602 	lio->glist = NULL;
603 }
604 
605 /**
606  * \brief Setup gather lists
607  * @param lio per-network private data
608  */
609 static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
610 {
611 	int i, j;
612 	struct octnic_gather *g;
613 
614 	lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
615 				  GFP_KERNEL);
616 	if (!lio->glist_lock)
617 		return -ENOMEM;
618 
619 	lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
620 			     GFP_KERNEL);
621 	if (!lio->glist) {
622 		kfree(lio->glist_lock);
623 		lio->glist_lock = NULL;
624 		return -ENOMEM;
625 	}
626 
627 	lio->glist_entry_size =
628 		ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
629 
630 	/* allocate memory to store virtual and dma base address of
631 	 * per glist consistent memory
632 	 */
633 	lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
634 					GFP_KERNEL);
635 	lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
636 				       GFP_KERNEL);
637 
638 	if (!lio->glists_virt_base || !lio->glists_dma_base) {
639 		delete_glists(lio);
640 		return -ENOMEM;
641 	}
642 
643 	for (i = 0; i < num_iqs; i++) {
644 		int numa_node = dev_to_node(&oct->pci_dev->dev);
645 
646 		spin_lock_init(&lio->glist_lock[i]);
647 
648 		INIT_LIST_HEAD(&lio->glist[i]);
649 
650 		lio->glists_virt_base[i] =
651 			lio_dma_alloc(oct,
652 				      lio->glist_entry_size * lio->tx_qsize,
653 				      &lio->glists_dma_base[i]);
654 
655 		if (!lio->glists_virt_base[i]) {
656 			delete_glists(lio);
657 			return -ENOMEM;
658 		}
659 
660 		for (j = 0; j < lio->tx_qsize; j++) {
661 			g = kzalloc_node(sizeof(*g), GFP_KERNEL,
662 					 numa_node);
663 			if (!g)
664 				g = kzalloc(sizeof(*g), GFP_KERNEL);
665 			if (!g)
666 				break;
667 
668 			g->sg = lio->glists_virt_base[i] +
669 				(j * lio->glist_entry_size);
670 
671 			g->sg_dma_ptr = lio->glists_dma_base[i] +
672 					(j * lio->glist_entry_size);
673 
674 			list_add_tail(&g->list, &lio->glist[i]);
675 		}
676 
677 		if (j != lio->tx_qsize) {
678 			delete_glists(lio);
679 			return -ENOMEM;
680 		}
681 	}
682 
683 	return 0;
684 }
685 
686 /**
687  * \brief Print link information
688  * @param netdev network device
689  */
690 static void print_link_info(struct net_device *netdev)
691 {
692 	struct lio *lio = GET_LIO(netdev);
693 
694 	if (!ifstate_check(lio, LIO_IFSTATE_RESETTING) &&
695 	    ifstate_check(lio, LIO_IFSTATE_REGISTERED)) {
696 		struct oct_link_info *linfo = &lio->linfo;
697 
698 		if (linfo->link.s.link_up) {
699 			netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
700 				   linfo->link.s.speed,
701 				   (linfo->link.s.duplex) ? "Full" : "Half");
702 		} else {
703 			netif_info(lio, link, lio->netdev, "Link Down\n");
704 		}
705 	}
706 }
707 
708 /**
709  * \brief Routine to notify MTU change
710  * @param work work_struct data structure
711  */
712 static void octnet_link_status_change(struct work_struct *work)
713 {
714 	struct cavium_wk *wk = (struct cavium_wk *)work;
715 	struct lio *lio = (struct lio *)wk->ctxptr;
716 
717 	/* lio->linfo.link.s.mtu always contains max MTU of the lio interface.
718 	 * this API is invoked only when new max-MTU of the interface is
719 	 * less than current MTU.
720 	 */
721 	rtnl_lock();
722 	dev_set_mtu(lio->netdev, lio->linfo.link.s.mtu);
723 	rtnl_unlock();
724 }
725 
726 /**
727  * \brief Sets up the mtu status change work
728  * @param netdev network device
729  */
730 static inline int setup_link_status_change_wq(struct net_device *netdev)
731 {
732 	struct lio *lio = GET_LIO(netdev);
733 	struct octeon_device *oct = lio->oct_dev;
734 
735 	lio->link_status_wq.wq = alloc_workqueue("link-status",
736 						 WQ_MEM_RECLAIM, 0);
737 	if (!lio->link_status_wq.wq) {
738 		dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
739 		return -1;
740 	}
741 	INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
742 			  octnet_link_status_change);
743 	lio->link_status_wq.wk.ctxptr = lio;
744 
745 	return 0;
746 }
747 
748 static inline void cleanup_link_status_change_wq(struct net_device *netdev)
749 {
750 	struct lio *lio = GET_LIO(netdev);
751 
752 	if (lio->link_status_wq.wq) {
753 		cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
754 		destroy_workqueue(lio->link_status_wq.wq);
755 	}
756 }
757 
758 /**
759  * \brief Update link status
760  * @param netdev network device
761  * @param ls link status structure
762  *
763  * Called on receipt of a link status response from the core application to
764  * update each interface's link status.
765  */
766 static inline void update_link_status(struct net_device *netdev,
767 				      union oct_link_status *ls)
768 {
769 	struct lio *lio = GET_LIO(netdev);
770 	int changed = (lio->linfo.link.u64 != ls->u64);
771 	int current_max_mtu = lio->linfo.link.s.mtu;
772 	struct octeon_device *oct = lio->oct_dev;
773 
774 	dev_dbg(&oct->pci_dev->dev, "%s: lio->linfo.link.u64=%llx, ls->u64=%llx\n",
775 		__func__, lio->linfo.link.u64, ls->u64);
776 	lio->linfo.link.u64 = ls->u64;
777 
778 	if ((lio->intf_open) && (changed)) {
779 		print_link_info(netdev);
780 		lio->link_changes++;
781 
782 		if (lio->linfo.link.s.link_up) {
783 			dev_dbg(&oct->pci_dev->dev, "%s: link_up", __func__);
784 			netif_carrier_on(netdev);
785 			wake_txqs(netdev);
786 		} else {
787 			dev_dbg(&oct->pci_dev->dev, "%s: link_off", __func__);
788 			netif_carrier_off(netdev);
789 			stop_txqs(netdev);
790 		}
791 		if (lio->linfo.link.s.mtu != current_max_mtu) {
792 			netif_info(lio, probe, lio->netdev, "Max MTU changed from %d to %d\n",
793 				   current_max_mtu, lio->linfo.link.s.mtu);
794 			netdev->max_mtu = lio->linfo.link.s.mtu;
795 		}
796 		if (lio->linfo.link.s.mtu < netdev->mtu) {
797 			dev_warn(&oct->pci_dev->dev,
798 				 "Current MTU is higher than new max MTU; Reducing the current mtu from %d to %d\n",
799 				     netdev->mtu, lio->linfo.link.s.mtu);
800 			queue_delayed_work(lio->link_status_wq.wq,
801 					   &lio->link_status_wq.wk.work, 0);
802 		}
803 	}
804 }
805 
806 /**
807  * lio_sync_octeon_time_cb - callback that is invoked when soft command
808  * sent by lio_sync_octeon_time() has completed successfully or failed
809  *
810  * @oct - octeon device structure
811  * @status - indicates success or failure
812  * @buf - pointer to the command that was sent to firmware
813  **/
814 static void lio_sync_octeon_time_cb(struct octeon_device *oct,
815 				    u32 status, void *buf)
816 {
817 	struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
818 
819 	if (status)
820 		dev_err(&oct->pci_dev->dev,
821 			"Failed to sync time to octeon; error=%d\n", status);
822 
823 	octeon_free_soft_command(oct, sc);
824 }
825 
826 /**
827  * lio_sync_octeon_time - send latest localtime to octeon firmware so that
828  * firmware will correct it's time, in case there is a time skew
829  *
830  * @work: work scheduled to send time update to octeon firmware
831  **/
832 static void lio_sync_octeon_time(struct work_struct *work)
833 {
834 	struct cavium_wk *wk = (struct cavium_wk *)work;
835 	struct lio *lio = (struct lio *)wk->ctxptr;
836 	struct octeon_device *oct = lio->oct_dev;
837 	struct octeon_soft_command *sc;
838 	struct timespec64 ts;
839 	struct lio_time *lt;
840 	int ret;
841 
842 	sc = octeon_alloc_soft_command(oct, sizeof(struct lio_time), 0, 0);
843 	if (!sc) {
844 		dev_err(&oct->pci_dev->dev,
845 			"Failed to sync time to octeon: soft command allocation failed\n");
846 		return;
847 	}
848 
849 	lt = (struct lio_time *)sc->virtdptr;
850 
851 	/* Get time of the day */
852 	getnstimeofday64(&ts);
853 	lt->sec = ts.tv_sec;
854 	lt->nsec = ts.tv_nsec;
855 	octeon_swap_8B_data((u64 *)lt, (sizeof(struct lio_time)) / 8);
856 
857 	sc->iq_no = lio->linfo.txpciq[0].s.q_no;
858 	octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
859 				    OPCODE_NIC_SYNC_OCTEON_TIME, 0, 0, 0);
860 
861 	sc->callback = lio_sync_octeon_time_cb;
862 	sc->callback_arg = sc;
863 	sc->wait_time = 1000;
864 
865 	ret = octeon_send_soft_command(oct, sc);
866 	if (ret == IQ_SEND_FAILED) {
867 		dev_err(&oct->pci_dev->dev,
868 			"Failed to sync time to octeon: failed to send soft command\n");
869 		octeon_free_soft_command(oct, sc);
870 	}
871 
872 	queue_delayed_work(lio->sync_octeon_time_wq.wq,
873 			   &lio->sync_octeon_time_wq.wk.work,
874 			   msecs_to_jiffies(LIO_SYNC_OCTEON_TIME_INTERVAL_MS));
875 }
876 
877 /**
878  * setup_sync_octeon_time_wq - Sets up the work to periodically update
879  * local time to octeon firmware
880  *
881  * @netdev - network device which should send time update to firmware
882  **/
883 static inline int setup_sync_octeon_time_wq(struct net_device *netdev)
884 {
885 	struct lio *lio = GET_LIO(netdev);
886 	struct octeon_device *oct = lio->oct_dev;
887 
888 	lio->sync_octeon_time_wq.wq =
889 		alloc_workqueue("update-octeon-time", WQ_MEM_RECLAIM, 0);
890 	if (!lio->sync_octeon_time_wq.wq) {
891 		dev_err(&oct->pci_dev->dev, "Unable to create wq to update octeon time\n");
892 		return -1;
893 	}
894 	INIT_DELAYED_WORK(&lio->sync_octeon_time_wq.wk.work,
895 			  lio_sync_octeon_time);
896 	lio->sync_octeon_time_wq.wk.ctxptr = lio;
897 	queue_delayed_work(lio->sync_octeon_time_wq.wq,
898 			   &lio->sync_octeon_time_wq.wk.work,
899 			   msecs_to_jiffies(LIO_SYNC_OCTEON_TIME_INTERVAL_MS));
900 
901 	return 0;
902 }
903 
904 /**
905  * cleanup_sync_octeon_time_wq - stop scheduling and destroy the work created
906  * to periodically update local time to octeon firmware
907  *
908  * @netdev - network device which should send time update to firmware
909  **/
910 static inline void cleanup_sync_octeon_time_wq(struct net_device *netdev)
911 {
912 	struct lio *lio = GET_LIO(netdev);
913 	struct cavium_wq *time_wq = &lio->sync_octeon_time_wq;
914 
915 	if (time_wq->wq) {
916 		cancel_delayed_work_sync(&time_wq->wk.work);
917 		destroy_workqueue(time_wq->wq);
918 	}
919 }
920 
921 static struct octeon_device *get_other_octeon_device(struct octeon_device *oct)
922 {
923 	struct octeon_device *other_oct;
924 
925 	other_oct = lio_get_device(oct->octeon_id + 1);
926 
927 	if (other_oct && other_oct->pci_dev) {
928 		int oct_busnum, other_oct_busnum;
929 
930 		oct_busnum = oct->pci_dev->bus->number;
931 		other_oct_busnum = other_oct->pci_dev->bus->number;
932 
933 		if (oct_busnum == other_oct_busnum) {
934 			int oct_slot, other_oct_slot;
935 
936 			oct_slot = PCI_SLOT(oct->pci_dev->devfn);
937 			other_oct_slot = PCI_SLOT(other_oct->pci_dev->devfn);
938 
939 			if (oct_slot == other_oct_slot)
940 				return other_oct;
941 		}
942 	}
943 
944 	return NULL;
945 }
946 
947 static void disable_all_vf_links(struct octeon_device *oct)
948 {
949 	struct net_device *netdev;
950 	int max_vfs, vf, i;
951 
952 	if (!oct)
953 		return;
954 
955 	max_vfs = oct->sriov_info.max_vfs;
956 
957 	for (i = 0; i < oct->ifcount; i++) {
958 		netdev = oct->props[i].netdev;
959 		if (!netdev)
960 			continue;
961 
962 		for (vf = 0; vf < max_vfs; vf++)
963 			liquidio_set_vf_link_state(netdev, vf,
964 						   IFLA_VF_LINK_STATE_DISABLE);
965 	}
966 }
967 
968 static int liquidio_watchdog(void *param)
969 {
970 	bool err_msg_was_printed[LIO_MAX_CORES];
971 	u16 mask_of_crashed_or_stuck_cores = 0;
972 	bool all_vf_links_are_disabled = false;
973 	struct octeon_device *oct = param;
974 	struct octeon_device *other_oct;
975 #ifdef CONFIG_MODULE_UNLOAD
976 	long refcount, vfs_referencing_pf;
977 	u64 vfs_mask1, vfs_mask2;
978 #endif
979 	int core;
980 
981 	memset(err_msg_was_printed, 0, sizeof(err_msg_was_printed));
982 
983 	while (!kthread_should_stop()) {
984 		/* sleep for a couple of seconds so that we don't hog the CPU */
985 		set_current_state(TASK_INTERRUPTIBLE);
986 		schedule_timeout(msecs_to_jiffies(2000));
987 
988 		mask_of_crashed_or_stuck_cores =
989 		    (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
990 
991 		if (!mask_of_crashed_or_stuck_cores)
992 			continue;
993 
994 		WRITE_ONCE(oct->cores_crashed, true);
995 		other_oct = get_other_octeon_device(oct);
996 		if (other_oct)
997 			WRITE_ONCE(other_oct->cores_crashed, true);
998 
999 		for (core = 0; core < LIO_MAX_CORES; core++) {
1000 			bool core_crashed_or_got_stuck;
1001 
1002 			core_crashed_or_got_stuck =
1003 						(mask_of_crashed_or_stuck_cores
1004 						 >> core) & 1;
1005 
1006 			if (core_crashed_or_got_stuck &&
1007 			    !err_msg_was_printed[core]) {
1008 				dev_err(&oct->pci_dev->dev,
1009 					"ERROR: Octeon core %d crashed or got stuck!  See oct-fwdump for details.\n",
1010 					core);
1011 				err_msg_was_printed[core] = true;
1012 			}
1013 		}
1014 
1015 		if (all_vf_links_are_disabled)
1016 			continue;
1017 
1018 		disable_all_vf_links(oct);
1019 		disable_all_vf_links(other_oct);
1020 		all_vf_links_are_disabled = true;
1021 
1022 #ifdef CONFIG_MODULE_UNLOAD
1023 		vfs_mask1 = READ_ONCE(oct->sriov_info.vf_drv_loaded_mask);
1024 		vfs_mask2 = READ_ONCE(other_oct->sriov_info.vf_drv_loaded_mask);
1025 
1026 		vfs_referencing_pf  = hweight64(vfs_mask1);
1027 		vfs_referencing_pf += hweight64(vfs_mask2);
1028 
1029 		refcount = module_refcount(THIS_MODULE);
1030 		if (refcount >= vfs_referencing_pf) {
1031 			while (vfs_referencing_pf) {
1032 				module_put(THIS_MODULE);
1033 				vfs_referencing_pf--;
1034 			}
1035 		}
1036 #endif
1037 	}
1038 
1039 	return 0;
1040 }
1041 
1042 /**
1043  * \brief PCI probe handler
1044  * @param pdev PCI device structure
1045  * @param ent unused
1046  */
1047 static int
1048 liquidio_probe(struct pci_dev *pdev,
1049 	       const struct pci_device_id *ent __attribute__((unused)))
1050 {
1051 	struct octeon_device *oct_dev = NULL;
1052 	struct handshake *hs;
1053 
1054 	oct_dev = octeon_allocate_device(pdev->device,
1055 					 sizeof(struct octeon_device_priv));
1056 	if (!oct_dev) {
1057 		dev_err(&pdev->dev, "Unable to allocate device\n");
1058 		return -ENOMEM;
1059 	}
1060 
1061 	if (pdev->device == OCTEON_CN23XX_PF_VID)
1062 		oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
1063 
1064 	/* Enable PTP for 6XXX Device */
1065 	if (((pdev->device == OCTEON_CN66XX) ||
1066 	     (pdev->device == OCTEON_CN68XX)))
1067 		oct_dev->ptp_enable = true;
1068 	else
1069 		oct_dev->ptp_enable = false;
1070 
1071 	dev_info(&pdev->dev, "Initializing device %x:%x.\n",
1072 		 (u32)pdev->vendor, (u32)pdev->device);
1073 
1074 	/* Assign octeon_device for this device to the private data area. */
1075 	pci_set_drvdata(pdev, oct_dev);
1076 
1077 	/* set linux specific device pointer */
1078 	oct_dev->pci_dev = (void *)pdev;
1079 
1080 	hs = &handshake[oct_dev->octeon_id];
1081 	init_completion(&hs->init);
1082 	init_completion(&hs->started);
1083 	hs->pci_dev = pdev;
1084 
1085 	if (oct_dev->octeon_id == 0)
1086 		/* first LiquidIO NIC is detected */
1087 		complete(&first_stage);
1088 
1089 	if (octeon_device_init(oct_dev)) {
1090 		complete(&hs->init);
1091 		liquidio_remove(pdev);
1092 		return -ENOMEM;
1093 	}
1094 
1095 	if (OCTEON_CN23XX_PF(oct_dev)) {
1096 		u8 bus, device, function;
1097 
1098 		if (atomic_read(oct_dev->adapter_refcount) == 1) {
1099 			/* Each NIC gets one watchdog kernel thread.  The first
1100 			 * PF (of each NIC) that gets pci_driver->probe()'d
1101 			 * creates that thread.
1102 			 */
1103 			bus = pdev->bus->number;
1104 			device = PCI_SLOT(pdev->devfn);
1105 			function = PCI_FUNC(pdev->devfn);
1106 			oct_dev->watchdog_task = kthread_create(
1107 			    liquidio_watchdog, oct_dev,
1108 			    "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
1109 			if (!IS_ERR(oct_dev->watchdog_task)) {
1110 				wake_up_process(oct_dev->watchdog_task);
1111 			} else {
1112 				oct_dev->watchdog_task = NULL;
1113 				dev_err(&oct_dev->pci_dev->dev,
1114 					"failed to create kernel_thread\n");
1115 				liquidio_remove(pdev);
1116 				return -1;
1117 			}
1118 		}
1119 	}
1120 
1121 	oct_dev->rx_pause = 1;
1122 	oct_dev->tx_pause = 1;
1123 
1124 	dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
1125 
1126 	return 0;
1127 }
1128 
1129 static bool fw_type_is_auto(void)
1130 {
1131 	return strncmp(fw_type, LIO_FW_NAME_TYPE_AUTO,
1132 		       sizeof(LIO_FW_NAME_TYPE_AUTO)) == 0;
1133 }
1134 
1135 /**
1136  * \brief PCI FLR for each Octeon device.
1137  * @param oct octeon device
1138  */
1139 static void octeon_pci_flr(struct octeon_device *oct)
1140 {
1141 	int rc;
1142 
1143 	pci_save_state(oct->pci_dev);
1144 
1145 	pci_cfg_access_lock(oct->pci_dev);
1146 
1147 	/* Quiesce the device completely */
1148 	pci_write_config_word(oct->pci_dev, PCI_COMMAND,
1149 			      PCI_COMMAND_INTX_DISABLE);
1150 
1151 	rc = __pci_reset_function_locked(oct->pci_dev);
1152 
1153 	if (rc != 0)
1154 		dev_err(&oct->pci_dev->dev, "Error %d resetting PCI function %d\n",
1155 			rc, oct->pf_num);
1156 
1157 	pci_cfg_access_unlock(oct->pci_dev);
1158 
1159 	pci_restore_state(oct->pci_dev);
1160 }
1161 
1162 /**
1163  *\brief Destroy resources associated with octeon device
1164  * @param pdev PCI device structure
1165  * @param ent unused
1166  */
1167 static void octeon_destroy_resources(struct octeon_device *oct)
1168 {
1169 	int i, refcount;
1170 	struct msix_entry *msix_entries;
1171 	struct octeon_device_priv *oct_priv =
1172 		(struct octeon_device_priv *)oct->priv;
1173 
1174 	struct handshake *hs;
1175 
1176 	switch (atomic_read(&oct->status)) {
1177 	case OCT_DEV_RUNNING:
1178 	case OCT_DEV_CORE_OK:
1179 
1180 		/* No more instructions will be forwarded. */
1181 		atomic_set(&oct->status, OCT_DEV_IN_RESET);
1182 
1183 		oct->app_mode = CVM_DRV_INVALID_APP;
1184 		dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
1185 			lio_get_state_string(&oct->status));
1186 
1187 		schedule_timeout_uninterruptible(HZ / 10);
1188 
1189 		/* fallthrough */
1190 	case OCT_DEV_HOST_OK:
1191 
1192 		/* fallthrough */
1193 	case OCT_DEV_CONSOLE_INIT_DONE:
1194 		/* Remove any consoles */
1195 		octeon_remove_consoles(oct);
1196 
1197 		/* fallthrough */
1198 	case OCT_DEV_IO_QUEUES_DONE:
1199 		if (wait_for_pending_requests(oct))
1200 			dev_err(&oct->pci_dev->dev, "There were pending requests\n");
1201 
1202 		if (lio_wait_for_instr_fetch(oct))
1203 			dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
1204 
1205 		/* Disable the input and output queues now. No more packets will
1206 		 * arrive from Octeon, but we should wait for all packet
1207 		 * processing to finish.
1208 		 */
1209 		oct->fn_list.disable_io_queues(oct);
1210 
1211 		if (lio_wait_for_oq_pkts(oct))
1212 			dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
1213 
1214 	/* fallthrough */
1215 	case OCT_DEV_INTR_SET_DONE:
1216 		/* Disable interrupts  */
1217 		oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
1218 
1219 		if (oct->msix_on) {
1220 			msix_entries = (struct msix_entry *)oct->msix_entries;
1221 			for (i = 0; i < oct->num_msix_irqs - 1; i++) {
1222 				if (oct->ioq_vector[i].vector) {
1223 					/* clear the affinity_cpumask */
1224 					irq_set_affinity_hint(
1225 							msix_entries[i].vector,
1226 							NULL);
1227 					free_irq(msix_entries[i].vector,
1228 						 &oct->ioq_vector[i]);
1229 					oct->ioq_vector[i].vector = 0;
1230 				}
1231 			}
1232 			/* non-iov vector's argument is oct struct */
1233 			free_irq(msix_entries[i].vector, oct);
1234 
1235 			pci_disable_msix(oct->pci_dev);
1236 			kfree(oct->msix_entries);
1237 			oct->msix_entries = NULL;
1238 		} else {
1239 			/* Release the interrupt line */
1240 			free_irq(oct->pci_dev->irq, oct);
1241 
1242 			if (oct->flags & LIO_FLAG_MSI_ENABLED)
1243 				pci_disable_msi(oct->pci_dev);
1244 		}
1245 
1246 		kfree(oct->irq_name_storage);
1247 		oct->irq_name_storage = NULL;
1248 
1249 	/* fallthrough */
1250 	case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
1251 		if (OCTEON_CN23XX_PF(oct))
1252 			octeon_free_ioq_vector(oct);
1253 
1254 	/* fallthrough */
1255 	case OCT_DEV_MBOX_SETUP_DONE:
1256 		if (OCTEON_CN23XX_PF(oct))
1257 			oct->fn_list.free_mbox(oct);
1258 
1259 	/* fallthrough */
1260 	case OCT_DEV_IN_RESET:
1261 	case OCT_DEV_DROQ_INIT_DONE:
1262 		/* Wait for any pending operations */
1263 		mdelay(100);
1264 		for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
1265 			if (!(oct->io_qmask.oq & BIT_ULL(i)))
1266 				continue;
1267 			octeon_delete_droq(oct, i);
1268 		}
1269 
1270 		/* Force any pending handshakes to complete */
1271 		for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
1272 			hs = &handshake[i];
1273 
1274 			if (hs->pci_dev) {
1275 				handshake[oct->octeon_id].init_ok = 0;
1276 				complete(&handshake[oct->octeon_id].init);
1277 				handshake[oct->octeon_id].started_ok = 0;
1278 				complete(&handshake[oct->octeon_id].started);
1279 			}
1280 		}
1281 
1282 		/* fallthrough */
1283 	case OCT_DEV_RESP_LIST_INIT_DONE:
1284 		octeon_delete_response_list(oct);
1285 
1286 		/* fallthrough */
1287 	case OCT_DEV_INSTR_QUEUE_INIT_DONE:
1288 		for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
1289 			if (!(oct->io_qmask.iq & BIT_ULL(i)))
1290 				continue;
1291 			octeon_delete_instr_queue(oct, i);
1292 		}
1293 #ifdef CONFIG_PCI_IOV
1294 		if (oct->sriov_info.sriov_enabled)
1295 			pci_disable_sriov(oct->pci_dev);
1296 #endif
1297 		/* fallthrough */
1298 	case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
1299 		octeon_free_sc_buffer_pool(oct);
1300 
1301 		/* fallthrough */
1302 	case OCT_DEV_DISPATCH_INIT_DONE:
1303 		octeon_delete_dispatch_list(oct);
1304 		cancel_delayed_work_sync(&oct->nic_poll_work.work);
1305 
1306 		/* fallthrough */
1307 	case OCT_DEV_PCI_MAP_DONE:
1308 		refcount = octeon_deregister_device(oct);
1309 
1310 		/* Soft reset the octeon device before exiting.
1311 		 * However, if fw was loaded from card (i.e. autoboot),
1312 		 * perform an FLR instead.
1313 		 * Implementation note: only soft-reset the device
1314 		 * if it is a CN6XXX OR the LAST CN23XX device.
1315 		 */
1316 		if (atomic_read(oct->adapter_fw_state) == FW_IS_PRELOADED)
1317 			octeon_pci_flr(oct);
1318 		else if (OCTEON_CN6XXX(oct) || !refcount)
1319 			oct->fn_list.soft_reset(oct);
1320 
1321 		octeon_unmap_pci_barx(oct, 0);
1322 		octeon_unmap_pci_barx(oct, 1);
1323 
1324 		/* fallthrough */
1325 	case OCT_DEV_PCI_ENABLE_DONE:
1326 		pci_clear_master(oct->pci_dev);
1327 		/* Disable the device, releasing the PCI INT */
1328 		pci_disable_device(oct->pci_dev);
1329 
1330 		/* fallthrough */
1331 	case OCT_DEV_BEGIN_STATE:
1332 		/* Nothing to be done here either */
1333 		break;
1334 	}                       /* end switch (oct->status) */
1335 
1336 	tasklet_kill(&oct_priv->droq_tasklet);
1337 }
1338 
1339 /**
1340  * \brief Callback for rx ctrl
1341  * @param status status of request
1342  * @param buf pointer to resp structure
1343  */
1344 static void rx_ctl_callback(struct octeon_device *oct,
1345 			    u32 status,
1346 			    void *buf)
1347 {
1348 	struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1349 	struct liquidio_rx_ctl_context *ctx;
1350 
1351 	ctx  = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1352 
1353 	oct = lio_get_device(ctx->octeon_id);
1354 	if (status)
1355 		dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
1356 			CVM_CAST64(status));
1357 	WRITE_ONCE(ctx->cond, 1);
1358 
1359 	/* This barrier is required to be sure that the response has been
1360 	 * written fully before waking up the handler
1361 	 */
1362 	wmb();
1363 
1364 	wake_up_interruptible(&ctx->wc);
1365 }
1366 
1367 /**
1368  * \brief Send Rx control command
1369  * @param lio per-network private data
1370  * @param start_stop whether to start or stop
1371  */
1372 static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
1373 {
1374 	struct octeon_soft_command *sc;
1375 	struct liquidio_rx_ctl_context *ctx;
1376 	union octnet_cmd *ncmd;
1377 	int ctx_size = sizeof(struct liquidio_rx_ctl_context);
1378 	struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1379 	int retval;
1380 
1381 	if (oct->props[lio->ifidx].rx_on == start_stop)
1382 		return;
1383 
1384 	sc = (struct octeon_soft_command *)
1385 		octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
1386 					  16, ctx_size);
1387 
1388 	ncmd = (union octnet_cmd *)sc->virtdptr;
1389 	ctx  = (struct liquidio_rx_ctl_context *)sc->ctxptr;
1390 
1391 	WRITE_ONCE(ctx->cond, 0);
1392 	ctx->octeon_id = lio_get_device_id(oct);
1393 	init_waitqueue_head(&ctx->wc);
1394 
1395 	ncmd->u64 = 0;
1396 	ncmd->s.cmd = OCTNET_CMD_RX_CTL;
1397 	ncmd->s.param1 = start_stop;
1398 
1399 	octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
1400 
1401 	sc->iq_no = lio->linfo.txpciq[0].s.q_no;
1402 
1403 	octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
1404 				    OPCODE_NIC_CMD, 0, 0, 0);
1405 
1406 	sc->callback = rx_ctl_callback;
1407 	sc->callback_arg = sc;
1408 	sc->wait_time = 5000;
1409 
1410 	retval = octeon_send_soft_command(oct, sc);
1411 	if (retval == IQ_SEND_FAILED) {
1412 		netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
1413 	} else {
1414 		/* Sleep on a wait queue till the cond flag indicates that the
1415 		 * response arrived or timed-out.
1416 		 */
1417 		if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
1418 			return;
1419 		oct->props[lio->ifidx].rx_on = start_stop;
1420 	}
1421 
1422 	octeon_free_soft_command(oct, sc);
1423 }
1424 
1425 /**
1426  * \brief Destroy NIC device interface
1427  * @param oct octeon device
1428  * @param ifidx which interface to destroy
1429  *
1430  * Cleanup associated with each interface for an Octeon device  when NIC
1431  * module is being unloaded or if initialization fails during load.
1432  */
1433 static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1434 {
1435 	struct net_device *netdev = oct->props[ifidx].netdev;
1436 	struct lio *lio;
1437 	struct napi_struct *napi, *n;
1438 
1439 	if (!netdev) {
1440 		dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
1441 			__func__, ifidx);
1442 		return;
1443 	}
1444 
1445 	lio = GET_LIO(netdev);
1446 
1447 	dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
1448 
1449 	if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
1450 		liquidio_stop(netdev);
1451 
1452 	if (oct->props[lio->ifidx].napi_enabled == 1) {
1453 		list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1454 			napi_disable(napi);
1455 
1456 		oct->props[lio->ifidx].napi_enabled = 0;
1457 
1458 		if (OCTEON_CN23XX_PF(oct))
1459 			oct->droq[0]->ops.poll_mode = 0;
1460 	}
1461 
1462 	/* Delete NAPI */
1463 	list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
1464 		netif_napi_del(napi);
1465 
1466 	if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1467 		unregister_netdev(netdev);
1468 
1469 	cleanup_sync_octeon_time_wq(netdev);
1470 	cleanup_link_status_change_wq(netdev);
1471 
1472 	cleanup_rx_oom_poll_fn(netdev);
1473 
1474 	delete_glists(lio);
1475 
1476 	free_netdev(netdev);
1477 
1478 	oct->props[ifidx].gmxport = -1;
1479 
1480 	oct->props[ifidx].netdev = NULL;
1481 }
1482 
1483 /**
1484  * \brief Stop complete NIC functionality
1485  * @param oct octeon device
1486  */
1487 static int liquidio_stop_nic_module(struct octeon_device *oct)
1488 {
1489 	int i, j;
1490 	struct lio *lio;
1491 
1492 	dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
1493 	if (!oct->ifcount) {
1494 		dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
1495 		return 1;
1496 	}
1497 
1498 	spin_lock_bh(&oct->cmd_resp_wqlock);
1499 	oct->cmd_resp_state = OCT_DRV_OFFLINE;
1500 	spin_unlock_bh(&oct->cmd_resp_wqlock);
1501 
1502 	lio_vf_rep_destroy(oct);
1503 
1504 	for (i = 0; i < oct->ifcount; i++) {
1505 		lio = GET_LIO(oct->props[i].netdev);
1506 		for (j = 0; j < oct->num_oqs; j++)
1507 			octeon_unregister_droq_ops(oct,
1508 						   lio->linfo.rxpciq[j].s.q_no);
1509 	}
1510 
1511 	for (i = 0; i < oct->ifcount; i++)
1512 		liquidio_destroy_nic_device(oct, i);
1513 
1514 	if (oct->devlink) {
1515 		devlink_unregister(oct->devlink);
1516 		devlink_free(oct->devlink);
1517 		oct->devlink = NULL;
1518 	}
1519 
1520 	dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
1521 	return 0;
1522 }
1523 
1524 /**
1525  * \brief Cleans up resources at unload time
1526  * @param pdev PCI device structure
1527  */
1528 static void liquidio_remove(struct pci_dev *pdev)
1529 {
1530 	struct octeon_device *oct_dev = pci_get_drvdata(pdev);
1531 
1532 	dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
1533 
1534 	if (oct_dev->watchdog_task)
1535 		kthread_stop(oct_dev->watchdog_task);
1536 
1537 	if (!oct_dev->octeon_id &&
1538 	    oct_dev->fw_info.app_cap_flags & LIQUIDIO_SWITCHDEV_CAP)
1539 		lio_vf_rep_modexit();
1540 
1541 	if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
1542 		liquidio_stop_nic_module(oct_dev);
1543 
1544 	/* Reset the octeon device and cleanup all memory allocated for
1545 	 * the octeon device by driver.
1546 	 */
1547 	octeon_destroy_resources(oct_dev);
1548 
1549 	dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
1550 
1551 	/* This octeon device has been removed. Update the global
1552 	 * data structure to reflect this. Free the device structure.
1553 	 */
1554 	octeon_free_device_mem(oct_dev);
1555 }
1556 
1557 /**
1558  * \brief Identify the Octeon device and to map the BAR address space
1559  * @param oct octeon device
1560  */
1561 static int octeon_chip_specific_setup(struct octeon_device *oct)
1562 {
1563 	u32 dev_id, rev_id;
1564 	int ret = 1;
1565 	char *s;
1566 
1567 	pci_read_config_dword(oct->pci_dev, 0, &dev_id);
1568 	pci_read_config_dword(oct->pci_dev, 8, &rev_id);
1569 	oct->rev_id = rev_id & 0xff;
1570 
1571 	switch (dev_id) {
1572 	case OCTEON_CN68XX_PCIID:
1573 		oct->chip_id = OCTEON_CN68XX;
1574 		ret = lio_setup_cn68xx_octeon_device(oct);
1575 		s = "CN68XX";
1576 		break;
1577 
1578 	case OCTEON_CN66XX_PCIID:
1579 		oct->chip_id = OCTEON_CN66XX;
1580 		ret = lio_setup_cn66xx_octeon_device(oct);
1581 		s = "CN66XX";
1582 		break;
1583 
1584 	case OCTEON_CN23XX_PCIID_PF:
1585 		oct->chip_id = OCTEON_CN23XX_PF_VID;
1586 		ret = setup_cn23xx_octeon_pf_device(oct);
1587 		if (ret)
1588 			break;
1589 #ifdef CONFIG_PCI_IOV
1590 		if (!ret)
1591 			pci_sriov_set_totalvfs(oct->pci_dev,
1592 					       oct->sriov_info.max_vfs);
1593 #endif
1594 		s = "CN23XX";
1595 		break;
1596 
1597 	default:
1598 		s = "?";
1599 		dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
1600 			dev_id);
1601 	}
1602 
1603 	if (!ret)
1604 		dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
1605 			 OCTEON_MAJOR_REV(oct),
1606 			 OCTEON_MINOR_REV(oct),
1607 			 octeon_get_conf(oct)->card_name,
1608 			 LIQUIDIO_VERSION);
1609 
1610 	return ret;
1611 }
1612 
1613 /**
1614  * \brief PCI initialization for each Octeon device.
1615  * @param oct octeon device
1616  */
1617 static int octeon_pci_os_setup(struct octeon_device *oct)
1618 {
1619 	/* setup PCI stuff first */
1620 	if (pci_enable_device(oct->pci_dev)) {
1621 		dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
1622 		return 1;
1623 	}
1624 
1625 	if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
1626 		dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
1627 		pci_disable_device(oct->pci_dev);
1628 		return 1;
1629 	}
1630 
1631 	/* Enable PCI DMA Master. */
1632 	pci_set_master(oct->pci_dev);
1633 
1634 	return 0;
1635 }
1636 
1637 /**
1638  * \brief Unmap and free network buffer
1639  * @param buf buffer
1640  */
1641 static void free_netbuf(void *buf)
1642 {
1643 	struct sk_buff *skb;
1644 	struct octnet_buf_free_info *finfo;
1645 	struct lio *lio;
1646 
1647 	finfo = (struct octnet_buf_free_info *)buf;
1648 	skb = finfo->skb;
1649 	lio = finfo->lio;
1650 
1651 	dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
1652 			 DMA_TO_DEVICE);
1653 
1654 	tx_buffer_free(skb);
1655 }
1656 
1657 /**
1658  * \brief Unmap and free gather buffer
1659  * @param buf buffer
1660  */
1661 static void free_netsgbuf(void *buf)
1662 {
1663 	struct octnet_buf_free_info *finfo;
1664 	struct sk_buff *skb;
1665 	struct lio *lio;
1666 	struct octnic_gather *g;
1667 	int i, frags, iq;
1668 
1669 	finfo = (struct octnet_buf_free_info *)buf;
1670 	skb = finfo->skb;
1671 	lio = finfo->lio;
1672 	g = finfo->g;
1673 	frags = skb_shinfo(skb)->nr_frags;
1674 
1675 	dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1676 			 g->sg[0].ptr[0], (skb->len - skb->data_len),
1677 			 DMA_TO_DEVICE);
1678 
1679 	i = 1;
1680 	while (frags--) {
1681 		struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1682 
1683 		pci_unmap_page((lio->oct_dev)->pci_dev,
1684 			       g->sg[(i >> 2)].ptr[(i & 3)],
1685 			       frag->size, DMA_TO_DEVICE);
1686 		i++;
1687 	}
1688 
1689 	iq = skb_iq(lio, skb);
1690 	spin_lock(&lio->glist_lock[iq]);
1691 	list_add_tail(&g->list, &lio->glist[iq]);
1692 	spin_unlock(&lio->glist_lock[iq]);
1693 
1694 	tx_buffer_free(skb);
1695 }
1696 
1697 /**
1698  * \brief Unmap and free gather buffer with response
1699  * @param buf buffer
1700  */
1701 static void free_netsgbuf_with_resp(void *buf)
1702 {
1703 	struct octeon_soft_command *sc;
1704 	struct octnet_buf_free_info *finfo;
1705 	struct sk_buff *skb;
1706 	struct lio *lio;
1707 	struct octnic_gather *g;
1708 	int i, frags, iq;
1709 
1710 	sc = (struct octeon_soft_command *)buf;
1711 	skb = (struct sk_buff *)sc->callback_arg;
1712 	finfo = (struct octnet_buf_free_info *)&skb->cb;
1713 
1714 	lio = finfo->lio;
1715 	g = finfo->g;
1716 	frags = skb_shinfo(skb)->nr_frags;
1717 
1718 	dma_unmap_single(&lio->oct_dev->pci_dev->dev,
1719 			 g->sg[0].ptr[0], (skb->len - skb->data_len),
1720 			 DMA_TO_DEVICE);
1721 
1722 	i = 1;
1723 	while (frags--) {
1724 		struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
1725 
1726 		pci_unmap_page((lio->oct_dev)->pci_dev,
1727 			       g->sg[(i >> 2)].ptr[(i & 3)],
1728 			       frag->size, DMA_TO_DEVICE);
1729 		i++;
1730 	}
1731 
1732 	iq = skb_iq(lio, skb);
1733 
1734 	spin_lock(&lio->glist_lock[iq]);
1735 	list_add_tail(&g->list, &lio->glist[iq]);
1736 	spin_unlock(&lio->glist_lock[iq]);
1737 
1738 	/* Don't free the skb yet */
1739 }
1740 
1741 /**
1742  * \brief Adjust ptp frequency
1743  * @param ptp PTP clock info
1744  * @param ppb how much to adjust by, in parts-per-billion
1745  */
1746 static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
1747 {
1748 	struct lio *lio = container_of(ptp, struct lio, ptp_info);
1749 	struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1750 	u64 comp, delta;
1751 	unsigned long flags;
1752 	bool neg_adj = false;
1753 
1754 	if (ppb < 0) {
1755 		neg_adj = true;
1756 		ppb = -ppb;
1757 	}
1758 
1759 	/* The hardware adds the clock compensation value to the
1760 	 * PTP clock on every coprocessor clock cycle, so we
1761 	 * compute the delta in terms of coprocessor clocks.
1762 	 */
1763 	delta = (u64)ppb << 32;
1764 	do_div(delta, oct->coproc_clock_rate);
1765 
1766 	spin_lock_irqsave(&lio->ptp_lock, flags);
1767 	comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
1768 	if (neg_adj)
1769 		comp -= delta;
1770 	else
1771 		comp += delta;
1772 	lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1773 	spin_unlock_irqrestore(&lio->ptp_lock, flags);
1774 
1775 	return 0;
1776 }
1777 
1778 /**
1779  * \brief Adjust ptp time
1780  * @param ptp PTP clock info
1781  * @param delta how much to adjust by, in nanosecs
1782  */
1783 static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1784 {
1785 	unsigned long flags;
1786 	struct lio *lio = container_of(ptp, struct lio, ptp_info);
1787 
1788 	spin_lock_irqsave(&lio->ptp_lock, flags);
1789 	lio->ptp_adjust += delta;
1790 	spin_unlock_irqrestore(&lio->ptp_lock, flags);
1791 
1792 	return 0;
1793 }
1794 
1795 /**
1796  * \brief Get hardware clock time, including any adjustment
1797  * @param ptp PTP clock info
1798  * @param ts timespec
1799  */
1800 static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
1801 				struct timespec64 *ts)
1802 {
1803 	u64 ns;
1804 	unsigned long flags;
1805 	struct lio *lio = container_of(ptp, struct lio, ptp_info);
1806 	struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1807 
1808 	spin_lock_irqsave(&lio->ptp_lock, flags);
1809 	ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
1810 	ns += lio->ptp_adjust;
1811 	spin_unlock_irqrestore(&lio->ptp_lock, flags);
1812 
1813 	*ts = ns_to_timespec64(ns);
1814 
1815 	return 0;
1816 }
1817 
1818 /**
1819  * \brief Set hardware clock time. Reset adjustment
1820  * @param ptp PTP clock info
1821  * @param ts timespec
1822  */
1823 static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
1824 				const struct timespec64 *ts)
1825 {
1826 	u64 ns;
1827 	unsigned long flags;
1828 	struct lio *lio = container_of(ptp, struct lio, ptp_info);
1829 	struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1830 
1831 	ns = timespec64_to_ns(ts);
1832 
1833 	spin_lock_irqsave(&lio->ptp_lock, flags);
1834 	lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
1835 	lio->ptp_adjust = 0;
1836 	spin_unlock_irqrestore(&lio->ptp_lock, flags);
1837 
1838 	return 0;
1839 }
1840 
1841 /**
1842  * \brief Check if PTP is enabled
1843  * @param ptp PTP clock info
1844  * @param rq request
1845  * @param on is it on
1846  */
1847 static int
1848 liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
1849 		    struct ptp_clock_request *rq __attribute__((unused)),
1850 		    int on __attribute__((unused)))
1851 {
1852 	return -EOPNOTSUPP;
1853 }
1854 
1855 /**
1856  * \brief Open PTP clock source
1857  * @param netdev network device
1858  */
1859 static void oct_ptp_open(struct net_device *netdev)
1860 {
1861 	struct lio *lio = GET_LIO(netdev);
1862 	struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
1863 
1864 	spin_lock_init(&lio->ptp_lock);
1865 
1866 	snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
1867 	lio->ptp_info.owner = THIS_MODULE;
1868 	lio->ptp_info.max_adj = 250000000;
1869 	lio->ptp_info.n_alarm = 0;
1870 	lio->ptp_info.n_ext_ts = 0;
1871 	lio->ptp_info.n_per_out = 0;
1872 	lio->ptp_info.pps = 0;
1873 	lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
1874 	lio->ptp_info.adjtime = liquidio_ptp_adjtime;
1875 	lio->ptp_info.gettime64 = liquidio_ptp_gettime;
1876 	lio->ptp_info.settime64 = liquidio_ptp_settime;
1877 	lio->ptp_info.enable = liquidio_ptp_enable;
1878 
1879 	lio->ptp_adjust = 0;
1880 
1881 	lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
1882 					     &oct->pci_dev->dev);
1883 
1884 	if (IS_ERR(lio->ptp_clock))
1885 		lio->ptp_clock = NULL;
1886 }
1887 
1888 /**
1889  * \brief Init PTP clock
1890  * @param oct octeon device
1891  */
1892 static void liquidio_ptp_init(struct octeon_device *oct)
1893 {
1894 	u64 clock_comp, cfg;
1895 
1896 	clock_comp = (u64)NSEC_PER_SEC << 32;
1897 	do_div(clock_comp, oct->coproc_clock_rate);
1898 	lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
1899 
1900 	/* Enable */
1901 	cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
1902 	lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
1903 }
1904 
1905 /**
1906  * \brief Load firmware to device
1907  * @param oct octeon device
1908  *
1909  * Maps device to firmware filename, requests firmware, and downloads it
1910  */
1911 static int load_firmware(struct octeon_device *oct)
1912 {
1913 	int ret = 0;
1914 	const struct firmware *fw;
1915 	char fw_name[LIO_MAX_FW_FILENAME_LEN];
1916 	char *tmp_fw_type;
1917 
1918 	if (fw_type_is_auto()) {
1919 		tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
1920 		strncpy(fw_type, tmp_fw_type, sizeof(fw_type));
1921 	} else {
1922 		tmp_fw_type = fw_type;
1923 	}
1924 
1925 	sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
1926 		octeon_get_conf(oct)->card_name, tmp_fw_type,
1927 		LIO_FW_NAME_SUFFIX);
1928 
1929 	ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
1930 	if (ret) {
1931 		dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
1932 			fw_name);
1933 		release_firmware(fw);
1934 		return ret;
1935 	}
1936 
1937 	ret = octeon_download_firmware(oct, fw->data, fw->size);
1938 
1939 	release_firmware(fw);
1940 
1941 	return ret;
1942 }
1943 
1944 /**
1945  * \brief Callback for getting interface configuration
1946  * @param status status of request
1947  * @param buf pointer to resp structure
1948  */
1949 static void if_cfg_callback(struct octeon_device *oct,
1950 			    u32 status __attribute__((unused)),
1951 			    void *buf)
1952 {
1953 	struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
1954 	struct liquidio_if_cfg_resp *resp;
1955 	struct liquidio_if_cfg_context *ctx;
1956 
1957 	resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
1958 	ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
1959 
1960 	oct = lio_get_device(ctx->octeon_id);
1961 	if (resp->status)
1962 		dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
1963 			CVM_CAST64(resp->status), status);
1964 	WRITE_ONCE(ctx->cond, 1);
1965 
1966 	snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
1967 		 resp->cfg_info.liquidio_firmware_version);
1968 
1969 	/* This barrier is required to be sure that the response has been
1970 	 * written fully before waking up the handler
1971 	 */
1972 	wmb();
1973 
1974 	wake_up_interruptible(&ctx->wc);
1975 }
1976 
1977 /**
1978  * \brief Poll routine for checking transmit queue status
1979  * @param work work_struct data structure
1980  */
1981 static void octnet_poll_check_txq_status(struct work_struct *work)
1982 {
1983 	struct cavium_wk *wk = (struct cavium_wk *)work;
1984 	struct lio *lio = (struct lio *)wk->ctxptr;
1985 
1986 	if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
1987 		return;
1988 
1989 	check_txq_status(lio);
1990 	queue_delayed_work(lio->txq_status_wq.wq,
1991 			   &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
1992 }
1993 
1994 /**
1995  * \brief Sets up the txq poll check
1996  * @param netdev network device
1997  */
1998 static inline int setup_tx_poll_fn(struct net_device *netdev)
1999 {
2000 	struct lio *lio = GET_LIO(netdev);
2001 	struct octeon_device *oct = lio->oct_dev;
2002 
2003 	lio->txq_status_wq.wq = alloc_workqueue("txq-status",
2004 						WQ_MEM_RECLAIM, 0);
2005 	if (!lio->txq_status_wq.wq) {
2006 		dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
2007 		return -1;
2008 	}
2009 	INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
2010 			  octnet_poll_check_txq_status);
2011 	lio->txq_status_wq.wk.ctxptr = lio;
2012 	queue_delayed_work(lio->txq_status_wq.wq,
2013 			   &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
2014 	return 0;
2015 }
2016 
2017 static inline void cleanup_tx_poll_fn(struct net_device *netdev)
2018 {
2019 	struct lio *lio = GET_LIO(netdev);
2020 
2021 	if (lio->txq_status_wq.wq) {
2022 		cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
2023 		destroy_workqueue(lio->txq_status_wq.wq);
2024 	}
2025 }
2026 
2027 /**
2028  * \brief Net device open for LiquidIO
2029  * @param netdev network device
2030  */
2031 static int liquidio_open(struct net_device *netdev)
2032 {
2033 	struct lio *lio = GET_LIO(netdev);
2034 	struct octeon_device *oct = lio->oct_dev;
2035 	struct napi_struct *napi, *n;
2036 
2037 	if (oct->props[lio->ifidx].napi_enabled == 0) {
2038 		list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2039 			napi_enable(napi);
2040 
2041 		oct->props[lio->ifidx].napi_enabled = 1;
2042 
2043 		if (OCTEON_CN23XX_PF(oct))
2044 			oct->droq[0]->ops.poll_mode = 1;
2045 	}
2046 
2047 	if (oct->ptp_enable)
2048 		oct_ptp_open(netdev);
2049 
2050 	ifstate_set(lio, LIO_IFSTATE_RUNNING);
2051 
2052 	/* Ready for link status updates */
2053 	lio->intf_open = 1;
2054 
2055 	netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
2056 
2057 	if (OCTEON_CN23XX_PF(oct)) {
2058 		if (!oct->msix_on)
2059 			if (setup_tx_poll_fn(netdev))
2060 				return -1;
2061 	} else {
2062 		if (setup_tx_poll_fn(netdev))
2063 			return -1;
2064 	}
2065 
2066 	start_txqs(netdev);
2067 
2068 	/* tell Octeon to start forwarding packets to host */
2069 	send_rx_ctrl_cmd(lio, 1);
2070 
2071 	dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
2072 		 netdev->name);
2073 
2074 	return 0;
2075 }
2076 
2077 /**
2078  * \brief Net device stop for LiquidIO
2079  * @param netdev network device
2080  */
2081 static int liquidio_stop(struct net_device *netdev)
2082 {
2083 	struct lio *lio = GET_LIO(netdev);
2084 	struct octeon_device *oct = lio->oct_dev;
2085 	struct napi_struct *napi, *n;
2086 
2087 	ifstate_reset(lio, LIO_IFSTATE_RUNNING);
2088 
2089 	netif_tx_disable(netdev);
2090 
2091 	/* Inform that netif carrier is down */
2092 	netif_carrier_off(netdev);
2093 	lio->intf_open = 0;
2094 	lio->linfo.link.s.link_up = 0;
2095 	lio->link_changes++;
2096 
2097 	/* Tell Octeon that nic interface is down. */
2098 	send_rx_ctrl_cmd(lio, 0);
2099 
2100 	if (OCTEON_CN23XX_PF(oct)) {
2101 		if (!oct->msix_on)
2102 			cleanup_tx_poll_fn(netdev);
2103 	} else {
2104 		cleanup_tx_poll_fn(netdev);
2105 	}
2106 
2107 	if (lio->ptp_clock) {
2108 		ptp_clock_unregister(lio->ptp_clock);
2109 		lio->ptp_clock = NULL;
2110 	}
2111 
2112 	/* Wait for any pending Rx descriptors */
2113 	if (lio_wait_for_clean_oq(oct))
2114 		netif_info(lio, rx_err, lio->netdev,
2115 			   "Proceeding with stop interface after partial RX desc processing\n");
2116 
2117 	if (oct->props[lio->ifidx].napi_enabled == 1) {
2118 		list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
2119 			napi_disable(napi);
2120 
2121 		oct->props[lio->ifidx].napi_enabled = 0;
2122 
2123 		if (OCTEON_CN23XX_PF(oct))
2124 			oct->droq[0]->ops.poll_mode = 0;
2125 	}
2126 
2127 	dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
2128 
2129 	return 0;
2130 }
2131 
2132 /**
2133  * \brief Converts a mask based on net device flags
2134  * @param netdev network device
2135  *
2136  * This routine generates a octnet_ifflags mask from the net device flags
2137  * received from the OS.
2138  */
2139 static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
2140 {
2141 	enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
2142 
2143 	if (netdev->flags & IFF_PROMISC)
2144 		f |= OCTNET_IFFLAG_PROMISC;
2145 
2146 	if (netdev->flags & IFF_ALLMULTI)
2147 		f |= OCTNET_IFFLAG_ALLMULTI;
2148 
2149 	if (netdev->flags & IFF_MULTICAST) {
2150 		f |= OCTNET_IFFLAG_MULTICAST;
2151 
2152 		/* Accept all multicast addresses if there are more than we
2153 		 * can handle
2154 		 */
2155 		if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
2156 			f |= OCTNET_IFFLAG_ALLMULTI;
2157 	}
2158 
2159 	if (netdev->flags & IFF_BROADCAST)
2160 		f |= OCTNET_IFFLAG_BROADCAST;
2161 
2162 	return f;
2163 }
2164 
2165 /**
2166  * \brief Net device set_multicast_list
2167  * @param netdev network device
2168  */
2169 static void liquidio_set_mcast_list(struct net_device *netdev)
2170 {
2171 	struct lio *lio = GET_LIO(netdev);
2172 	struct octeon_device *oct = lio->oct_dev;
2173 	struct octnic_ctrl_pkt nctrl;
2174 	struct netdev_hw_addr *ha;
2175 	u64 *mc;
2176 	int ret;
2177 	int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
2178 
2179 	memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2180 
2181 	/* Create a ctrl pkt command to be sent to core app. */
2182 	nctrl.ncmd.u64 = 0;
2183 	nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
2184 	nctrl.ncmd.s.param1 = get_new_flags(netdev);
2185 	nctrl.ncmd.s.param2 = mc_count;
2186 	nctrl.ncmd.s.more = mc_count;
2187 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2188 	nctrl.netpndev = (u64)netdev;
2189 	nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2190 
2191 	/* copy all the addresses into the udd */
2192 	mc = &nctrl.udd[0];
2193 	netdev_for_each_mc_addr(ha, netdev) {
2194 		*mc = 0;
2195 		memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
2196 		/* no need to swap bytes */
2197 
2198 		if (++mc > &nctrl.udd[mc_count])
2199 			break;
2200 	}
2201 
2202 	/* Apparently, any activity in this call from the kernel has to
2203 	 * be atomic. So we won't wait for response.
2204 	 */
2205 	nctrl.wait_time = 0;
2206 
2207 	ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2208 	if (ret < 0) {
2209 		dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
2210 			ret);
2211 	}
2212 }
2213 
2214 /**
2215  * \brief Net device set_mac_address
2216  * @param netdev network device
2217  */
2218 static int liquidio_set_mac(struct net_device *netdev, void *p)
2219 {
2220 	int ret = 0;
2221 	struct lio *lio = GET_LIO(netdev);
2222 	struct octeon_device *oct = lio->oct_dev;
2223 	struct sockaddr *addr = (struct sockaddr *)p;
2224 	struct octnic_ctrl_pkt nctrl;
2225 
2226 	if (!is_valid_ether_addr(addr->sa_data))
2227 		return -EADDRNOTAVAIL;
2228 
2229 	memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2230 
2231 	nctrl.ncmd.u64 = 0;
2232 	nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
2233 	nctrl.ncmd.s.param1 = 0;
2234 	nctrl.ncmd.s.more = 1;
2235 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2236 	nctrl.netpndev = (u64)netdev;
2237 	nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2238 	nctrl.wait_time = 100;
2239 
2240 	nctrl.udd[0] = 0;
2241 	/* The MAC Address is presented in network byte order. */
2242 	memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
2243 
2244 	ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2245 	if (ret < 0) {
2246 		dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
2247 		return -ENOMEM;
2248 	}
2249 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2250 	memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
2251 
2252 	return 0;
2253 }
2254 
2255 /**
2256  * \brief Net device get_stats
2257  * @param netdev network device
2258  */
2259 static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
2260 {
2261 	struct lio *lio = GET_LIO(netdev);
2262 	struct net_device_stats *stats = &netdev->stats;
2263 	struct octeon_device *oct;
2264 	u64 pkts = 0, drop = 0, bytes = 0;
2265 	struct oct_droq_stats *oq_stats;
2266 	struct oct_iq_stats *iq_stats;
2267 	int i, iq_no, oq_no;
2268 
2269 	oct = lio->oct_dev;
2270 
2271 	if (ifstate_check(lio, LIO_IFSTATE_RESETTING))
2272 		return stats;
2273 
2274 	for (i = 0; i < oct->num_iqs; i++) {
2275 		iq_no = lio->linfo.txpciq[i].s.q_no;
2276 		iq_stats = &oct->instr_queue[iq_no]->stats;
2277 		pkts += iq_stats->tx_done;
2278 		drop += iq_stats->tx_dropped;
2279 		bytes += iq_stats->tx_tot_bytes;
2280 	}
2281 
2282 	stats->tx_packets = pkts;
2283 	stats->tx_bytes = bytes;
2284 	stats->tx_dropped = drop;
2285 
2286 	pkts = 0;
2287 	drop = 0;
2288 	bytes = 0;
2289 
2290 	for (i = 0; i < oct->num_oqs; i++) {
2291 		oq_no = lio->linfo.rxpciq[i].s.q_no;
2292 		oq_stats = &oct->droq[oq_no]->stats;
2293 		pkts += oq_stats->rx_pkts_received;
2294 		drop += (oq_stats->rx_dropped +
2295 			 oq_stats->dropped_nodispatch +
2296 			 oq_stats->dropped_toomany +
2297 			 oq_stats->dropped_nomem);
2298 		bytes += oq_stats->rx_bytes_received;
2299 	}
2300 
2301 	stats->rx_bytes = bytes;
2302 	stats->rx_packets = pkts;
2303 	stats->rx_dropped = drop;
2304 
2305 	return stats;
2306 }
2307 
2308 /**
2309  * \brief Handler for SIOCSHWTSTAMP ioctl
2310  * @param netdev network device
2311  * @param ifr interface request
2312  * @param cmd command
2313  */
2314 static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
2315 {
2316 	struct hwtstamp_config conf;
2317 	struct lio *lio = GET_LIO(netdev);
2318 
2319 	if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
2320 		return -EFAULT;
2321 
2322 	if (conf.flags)
2323 		return -EINVAL;
2324 
2325 	switch (conf.tx_type) {
2326 	case HWTSTAMP_TX_ON:
2327 	case HWTSTAMP_TX_OFF:
2328 		break;
2329 	default:
2330 		return -ERANGE;
2331 	}
2332 
2333 	switch (conf.rx_filter) {
2334 	case HWTSTAMP_FILTER_NONE:
2335 		break;
2336 	case HWTSTAMP_FILTER_ALL:
2337 	case HWTSTAMP_FILTER_SOME:
2338 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2339 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2340 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2341 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2342 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2343 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2344 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2345 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2346 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2347 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
2348 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
2349 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2350 	case HWTSTAMP_FILTER_NTP_ALL:
2351 		conf.rx_filter = HWTSTAMP_FILTER_ALL;
2352 		break;
2353 	default:
2354 		return -ERANGE;
2355 	}
2356 
2357 	if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
2358 		ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2359 
2360 	else
2361 		ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
2362 
2363 	return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
2364 }
2365 
2366 /**
2367  * \brief ioctl handler
2368  * @param netdev network device
2369  * @param ifr interface request
2370  * @param cmd command
2371  */
2372 static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2373 {
2374 	struct lio *lio = GET_LIO(netdev);
2375 
2376 	switch (cmd) {
2377 	case SIOCSHWTSTAMP:
2378 		if (lio->oct_dev->ptp_enable)
2379 			return hwtstamp_ioctl(netdev, ifr);
2380 	default:
2381 		return -EOPNOTSUPP;
2382 	}
2383 }
2384 
2385 /**
2386  * \brief handle a Tx timestamp response
2387  * @param status response status
2388  * @param buf pointer to skb
2389  */
2390 static void handle_timestamp(struct octeon_device *oct,
2391 			     u32 status,
2392 			     void *buf)
2393 {
2394 	struct octnet_buf_free_info *finfo;
2395 	struct octeon_soft_command *sc;
2396 	struct oct_timestamp_resp *resp;
2397 	struct lio *lio;
2398 	struct sk_buff *skb = (struct sk_buff *)buf;
2399 
2400 	finfo = (struct octnet_buf_free_info *)skb->cb;
2401 	lio = finfo->lio;
2402 	sc = finfo->sc;
2403 	oct = lio->oct_dev;
2404 	resp = (struct oct_timestamp_resp *)sc->virtrptr;
2405 
2406 	if (status != OCTEON_REQUEST_DONE) {
2407 		dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
2408 			CVM_CAST64(status));
2409 		resp->timestamp = 0;
2410 	}
2411 
2412 	octeon_swap_8B_data(&resp->timestamp, 1);
2413 
2414 	if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
2415 		struct skb_shared_hwtstamps ts;
2416 		u64 ns = resp->timestamp;
2417 
2418 		netif_info(lio, tx_done, lio->netdev,
2419 			   "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
2420 			   skb, (unsigned long long)ns);
2421 		ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
2422 		skb_tstamp_tx(skb, &ts);
2423 	}
2424 
2425 	octeon_free_soft_command(oct, sc);
2426 	tx_buffer_free(skb);
2427 }
2428 
2429 /* \brief Send a data packet that will be timestamped
2430  * @param oct octeon device
2431  * @param ndata pointer to network data
2432  * @param finfo pointer to private network data
2433  */
2434 static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
2435 					 struct octnic_data_pkt *ndata,
2436 					 struct octnet_buf_free_info *finfo,
2437 					 int xmit_more)
2438 {
2439 	int retval;
2440 	struct octeon_soft_command *sc;
2441 	struct lio *lio;
2442 	int ring_doorbell;
2443 	u32 len;
2444 
2445 	lio = finfo->lio;
2446 
2447 	sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
2448 					    sizeof(struct oct_timestamp_resp));
2449 	finfo->sc = sc;
2450 
2451 	if (!sc) {
2452 		dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
2453 		return IQ_SEND_FAILED;
2454 	}
2455 
2456 	if (ndata->reqtype == REQTYPE_NORESP_NET)
2457 		ndata->reqtype = REQTYPE_RESP_NET;
2458 	else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
2459 		ndata->reqtype = REQTYPE_RESP_NET_SG;
2460 
2461 	sc->callback = handle_timestamp;
2462 	sc->callback_arg = finfo->skb;
2463 	sc->iq_no = ndata->q_no;
2464 
2465 	if (OCTEON_CN23XX_PF(oct))
2466 		len = (u32)((struct octeon_instr_ih3 *)
2467 			    (&sc->cmd.cmd3.ih3))->dlengsz;
2468 	else
2469 		len = (u32)((struct octeon_instr_ih2 *)
2470 			    (&sc->cmd.cmd2.ih2))->dlengsz;
2471 
2472 	ring_doorbell = !xmit_more;
2473 
2474 	retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
2475 				     sc, len, ndata->reqtype);
2476 
2477 	if (retval == IQ_SEND_FAILED) {
2478 		dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
2479 			retval);
2480 		octeon_free_soft_command(oct, sc);
2481 	} else {
2482 		netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
2483 	}
2484 
2485 	return retval;
2486 }
2487 
2488 /** \brief Transmit networks packets to the Octeon interface
2489  * @param skbuff   skbuff struct to be passed to network layer.
2490  * @param netdev    pointer to network device
2491  * @returns whether the packet was transmitted to the device okay or not
2492  *             (NETDEV_TX_OK or NETDEV_TX_BUSY)
2493  */
2494 static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
2495 {
2496 	struct lio *lio;
2497 	struct octnet_buf_free_info *finfo;
2498 	union octnic_cmd_setup cmdsetup;
2499 	struct octnic_data_pkt ndata;
2500 	struct octeon_device *oct;
2501 	struct oct_iq_stats *stats;
2502 	struct octeon_instr_irh *irh;
2503 	union tx_info *tx_info;
2504 	int status = 0;
2505 	int q_idx = 0, iq_no = 0;
2506 	int j, xmit_more = 0;
2507 	u64 dptr = 0;
2508 	u32 tag = 0;
2509 
2510 	lio = GET_LIO(netdev);
2511 	oct = lio->oct_dev;
2512 
2513 	q_idx = skb_iq(lio, skb);
2514 	tag = q_idx;
2515 	iq_no = lio->linfo.txpciq[q_idx].s.q_no;
2516 
2517 	stats = &oct->instr_queue[iq_no]->stats;
2518 
2519 	/* Check for all conditions in which the current packet cannot be
2520 	 * transmitted.
2521 	 */
2522 	if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
2523 	    (!lio->linfo.link.s.link_up) ||
2524 	    (skb->len <= 0)) {
2525 		netif_info(lio, tx_err, lio->netdev,
2526 			   "Transmit failed link_status : %d\n",
2527 			   lio->linfo.link.s.link_up);
2528 		goto lio_xmit_failed;
2529 	}
2530 
2531 	/* Use space in skb->cb to store info used to unmap and
2532 	 * free the buffers.
2533 	 */
2534 	finfo = (struct octnet_buf_free_info *)skb->cb;
2535 	finfo->lio = lio;
2536 	finfo->skb = skb;
2537 	finfo->sc = NULL;
2538 
2539 	/* Prepare the attributes for the data to be passed to OSI. */
2540 	memset(&ndata, 0, sizeof(struct octnic_data_pkt));
2541 
2542 	ndata.buf = (void *)finfo;
2543 
2544 	ndata.q_no = iq_no;
2545 
2546 	if (octnet_iq_is_full(oct, ndata.q_no)) {
2547 		/* defer sending if queue is full */
2548 		netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
2549 			   ndata.q_no);
2550 		stats->tx_iq_busy++;
2551 		return NETDEV_TX_BUSY;
2552 	}
2553 
2554 	/* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu:  %d, q_no:%d\n",
2555 	 *	lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
2556 	 */
2557 
2558 	ndata.datasize = skb->len;
2559 
2560 	cmdsetup.u64 = 0;
2561 	cmdsetup.s.iq_no = iq_no;
2562 
2563 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2564 		if (skb->encapsulation) {
2565 			cmdsetup.s.tnl_csum = 1;
2566 			stats->tx_vxlan++;
2567 		} else {
2568 			cmdsetup.s.transport_csum = 1;
2569 		}
2570 	}
2571 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
2572 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2573 		cmdsetup.s.timestamp = 1;
2574 	}
2575 
2576 	if (skb_shinfo(skb)->nr_frags == 0) {
2577 		cmdsetup.s.u.datasize = skb->len;
2578 		octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
2579 
2580 		/* Offload checksum calculation for TCP/UDP packets */
2581 		dptr = dma_map_single(&oct->pci_dev->dev,
2582 				      skb->data,
2583 				      skb->len,
2584 				      DMA_TO_DEVICE);
2585 		if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
2586 			dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
2587 				__func__);
2588 			stats->tx_dmamap_fail++;
2589 			return NETDEV_TX_BUSY;
2590 		}
2591 
2592 		if (OCTEON_CN23XX_PF(oct))
2593 			ndata.cmd.cmd3.dptr = dptr;
2594 		else
2595 			ndata.cmd.cmd2.dptr = dptr;
2596 		finfo->dptr = dptr;
2597 		ndata.reqtype = REQTYPE_NORESP_NET;
2598 
2599 	} else {
2600 		int i, frags;
2601 		struct skb_frag_struct *frag;
2602 		struct octnic_gather *g;
2603 
2604 		spin_lock(&lio->glist_lock[q_idx]);
2605 		g = (struct octnic_gather *)
2606 			list_delete_head(&lio->glist[q_idx]);
2607 		spin_unlock(&lio->glist_lock[q_idx]);
2608 
2609 		if (!g) {
2610 			netif_info(lio, tx_err, lio->netdev,
2611 				   "Transmit scatter gather: glist null!\n");
2612 			goto lio_xmit_failed;
2613 		}
2614 
2615 		cmdsetup.s.gather = 1;
2616 		cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
2617 		octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
2618 
2619 		memset(g->sg, 0, g->sg_size);
2620 
2621 		g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
2622 						 skb->data,
2623 						 (skb->len - skb->data_len),
2624 						 DMA_TO_DEVICE);
2625 		if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
2626 			dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
2627 				__func__);
2628 			stats->tx_dmamap_fail++;
2629 			return NETDEV_TX_BUSY;
2630 		}
2631 		add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
2632 
2633 		frags = skb_shinfo(skb)->nr_frags;
2634 		i = 1;
2635 		while (frags--) {
2636 			frag = &skb_shinfo(skb)->frags[i - 1];
2637 
2638 			g->sg[(i >> 2)].ptr[(i & 3)] =
2639 				dma_map_page(&oct->pci_dev->dev,
2640 					     frag->page.p,
2641 					     frag->page_offset,
2642 					     frag->size,
2643 					     DMA_TO_DEVICE);
2644 
2645 			if (dma_mapping_error(&oct->pci_dev->dev,
2646 					      g->sg[i >> 2].ptr[i & 3])) {
2647 				dma_unmap_single(&oct->pci_dev->dev,
2648 						 g->sg[0].ptr[0],
2649 						 skb->len - skb->data_len,
2650 						 DMA_TO_DEVICE);
2651 				for (j = 1; j < i; j++) {
2652 					frag = &skb_shinfo(skb)->frags[j - 1];
2653 					dma_unmap_page(&oct->pci_dev->dev,
2654 						       g->sg[j >> 2].ptr[j & 3],
2655 						       frag->size,
2656 						       DMA_TO_DEVICE);
2657 				}
2658 				dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
2659 					__func__);
2660 				return NETDEV_TX_BUSY;
2661 			}
2662 
2663 			add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
2664 			i++;
2665 		}
2666 
2667 		dptr = g->sg_dma_ptr;
2668 
2669 		if (OCTEON_CN23XX_PF(oct))
2670 			ndata.cmd.cmd3.dptr = dptr;
2671 		else
2672 			ndata.cmd.cmd2.dptr = dptr;
2673 		finfo->dptr = dptr;
2674 		finfo->g = g;
2675 
2676 		ndata.reqtype = REQTYPE_NORESP_NET_SG;
2677 	}
2678 
2679 	if (OCTEON_CN23XX_PF(oct)) {
2680 		irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
2681 		tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
2682 	} else {
2683 		irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
2684 		tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
2685 	}
2686 
2687 	if (skb_shinfo(skb)->gso_size) {
2688 		tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
2689 		tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
2690 		stats->tx_gso++;
2691 	}
2692 
2693 	/* HW insert VLAN tag */
2694 	if (skb_vlan_tag_present(skb)) {
2695 		irh->priority = skb_vlan_tag_get(skb) >> 13;
2696 		irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
2697 	}
2698 
2699 	xmit_more = skb->xmit_more;
2700 
2701 	if (unlikely(cmdsetup.s.timestamp))
2702 		status = send_nic_timestamp_pkt(oct, &ndata, finfo, xmit_more);
2703 	else
2704 		status = octnet_send_nic_data_pkt(oct, &ndata, xmit_more);
2705 	if (status == IQ_SEND_FAILED)
2706 		goto lio_xmit_failed;
2707 
2708 	netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
2709 
2710 	if (status == IQ_SEND_STOP)
2711 		netif_stop_subqueue(netdev, q_idx);
2712 
2713 	netif_trans_update(netdev);
2714 
2715 	if (tx_info->s.gso_segs)
2716 		stats->tx_done += tx_info->s.gso_segs;
2717 	else
2718 		stats->tx_done++;
2719 	stats->tx_tot_bytes += ndata.datasize;
2720 
2721 	return NETDEV_TX_OK;
2722 
2723 lio_xmit_failed:
2724 	stats->tx_dropped++;
2725 	netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
2726 		   iq_no, stats->tx_dropped);
2727 	if (dptr)
2728 		dma_unmap_single(&oct->pci_dev->dev, dptr,
2729 				 ndata.datasize, DMA_TO_DEVICE);
2730 
2731 	octeon_ring_doorbell_locked(oct, iq_no);
2732 
2733 	tx_buffer_free(skb);
2734 	return NETDEV_TX_OK;
2735 }
2736 
2737 /** \brief Network device Tx timeout
2738  * @param netdev    pointer to network device
2739  */
2740 static void liquidio_tx_timeout(struct net_device *netdev)
2741 {
2742 	struct lio *lio;
2743 
2744 	lio = GET_LIO(netdev);
2745 
2746 	netif_info(lio, tx_err, lio->netdev,
2747 		   "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
2748 		   netdev->stats.tx_dropped);
2749 	netif_trans_update(netdev);
2750 	wake_txqs(netdev);
2751 }
2752 
2753 static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
2754 				    __be16 proto __attribute__((unused)),
2755 				    u16 vid)
2756 {
2757 	struct lio *lio = GET_LIO(netdev);
2758 	struct octeon_device *oct = lio->oct_dev;
2759 	struct octnic_ctrl_pkt nctrl;
2760 	int ret = 0;
2761 
2762 	memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2763 
2764 	nctrl.ncmd.u64 = 0;
2765 	nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
2766 	nctrl.ncmd.s.param1 = vid;
2767 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2768 	nctrl.wait_time = 100;
2769 	nctrl.netpndev = (u64)netdev;
2770 	nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2771 
2772 	ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2773 	if (ret < 0) {
2774 		dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
2775 			ret);
2776 	}
2777 
2778 	return ret;
2779 }
2780 
2781 static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
2782 				     __be16 proto __attribute__((unused)),
2783 				     u16 vid)
2784 {
2785 	struct lio *lio = GET_LIO(netdev);
2786 	struct octeon_device *oct = lio->oct_dev;
2787 	struct octnic_ctrl_pkt nctrl;
2788 	int ret = 0;
2789 
2790 	memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2791 
2792 	nctrl.ncmd.u64 = 0;
2793 	nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
2794 	nctrl.ncmd.s.param1 = vid;
2795 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2796 	nctrl.wait_time = 100;
2797 	nctrl.netpndev = (u64)netdev;
2798 	nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2799 
2800 	ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2801 	if (ret < 0) {
2802 		dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
2803 			ret);
2804 	}
2805 	return ret;
2806 }
2807 
2808 /** Sending command to enable/disable RX checksum offload
2809  * @param netdev                pointer to network device
2810  * @param command               OCTNET_CMD_TNL_RX_CSUM_CTL
2811  * @param rx_cmd_bit            OCTNET_CMD_RXCSUM_ENABLE/
2812  *                              OCTNET_CMD_RXCSUM_DISABLE
2813  * @returns                     SUCCESS or FAILURE
2814  */
2815 static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
2816 				       u8 rx_cmd)
2817 {
2818 	struct lio *lio = GET_LIO(netdev);
2819 	struct octeon_device *oct = lio->oct_dev;
2820 	struct octnic_ctrl_pkt nctrl;
2821 	int ret = 0;
2822 
2823 	memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2824 
2825 	nctrl.ncmd.u64 = 0;
2826 	nctrl.ncmd.s.cmd = command;
2827 	nctrl.ncmd.s.param1 = rx_cmd;
2828 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2829 	nctrl.wait_time = 100;
2830 	nctrl.netpndev = (u64)netdev;
2831 	nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2832 
2833 	ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2834 	if (ret < 0) {
2835 		dev_err(&oct->pci_dev->dev,
2836 			"DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
2837 			ret);
2838 	}
2839 	return ret;
2840 }
2841 
2842 /** Sending command to add/delete VxLAN UDP port to firmware
2843  * @param netdev                pointer to network device
2844  * @param command               OCTNET_CMD_VXLAN_PORT_CONFIG
2845  * @param vxlan_port            VxLAN port to be added or deleted
2846  * @param vxlan_cmd_bit         OCTNET_CMD_VXLAN_PORT_ADD,
2847  *                              OCTNET_CMD_VXLAN_PORT_DEL
2848  * @returns                     SUCCESS or FAILURE
2849  */
2850 static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
2851 				       u16 vxlan_port, u8 vxlan_cmd_bit)
2852 {
2853 	struct lio *lio = GET_LIO(netdev);
2854 	struct octeon_device *oct = lio->oct_dev;
2855 	struct octnic_ctrl_pkt nctrl;
2856 	int ret = 0;
2857 
2858 	memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
2859 
2860 	nctrl.ncmd.u64 = 0;
2861 	nctrl.ncmd.s.cmd = command;
2862 	nctrl.ncmd.s.more = vxlan_cmd_bit;
2863 	nctrl.ncmd.s.param1 = vxlan_port;
2864 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2865 	nctrl.wait_time = 100;
2866 	nctrl.netpndev = (u64)netdev;
2867 	nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
2868 
2869 	ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
2870 	if (ret < 0) {
2871 		dev_err(&oct->pci_dev->dev,
2872 			"VxLAN port add/delete failed in core (ret:0x%x)\n",
2873 			ret);
2874 	}
2875 	return ret;
2876 }
2877 
2878 /** \brief Net device fix features
2879  * @param netdev  pointer to network device
2880  * @param request features requested
2881  * @returns updated features list
2882  */
2883 static netdev_features_t liquidio_fix_features(struct net_device *netdev,
2884 					       netdev_features_t request)
2885 {
2886 	struct lio *lio = netdev_priv(netdev);
2887 
2888 	if ((request & NETIF_F_RXCSUM) &&
2889 	    !(lio->dev_capability & NETIF_F_RXCSUM))
2890 		request &= ~NETIF_F_RXCSUM;
2891 
2892 	if ((request & NETIF_F_HW_CSUM) &&
2893 	    !(lio->dev_capability & NETIF_F_HW_CSUM))
2894 		request &= ~NETIF_F_HW_CSUM;
2895 
2896 	if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
2897 		request &= ~NETIF_F_TSO;
2898 
2899 	if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
2900 		request &= ~NETIF_F_TSO6;
2901 
2902 	if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
2903 		request &= ~NETIF_F_LRO;
2904 
2905 	/*Disable LRO if RXCSUM is off */
2906 	if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
2907 	    (lio->dev_capability & NETIF_F_LRO))
2908 		request &= ~NETIF_F_LRO;
2909 
2910 	if ((request & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2911 	    !(lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER))
2912 		request &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
2913 
2914 	return request;
2915 }
2916 
2917 /** \brief Net device set features
2918  * @param netdev  pointer to network device
2919  * @param features features to enable/disable
2920  */
2921 static int liquidio_set_features(struct net_device *netdev,
2922 				 netdev_features_t features)
2923 {
2924 	struct lio *lio = netdev_priv(netdev);
2925 
2926 	if ((features & NETIF_F_LRO) &&
2927 	    (lio->dev_capability & NETIF_F_LRO) &&
2928 	    !(netdev->features & NETIF_F_LRO))
2929 		liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
2930 				     OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
2931 	else if (!(features & NETIF_F_LRO) &&
2932 		 (lio->dev_capability & NETIF_F_LRO) &&
2933 		 (netdev->features & NETIF_F_LRO))
2934 		liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
2935 				     OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
2936 
2937 	/* Sending command to firmware to enable/disable RX checksum
2938 	 * offload settings using ethtool
2939 	 */
2940 	if (!(netdev->features & NETIF_F_RXCSUM) &&
2941 	    (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
2942 	    (features & NETIF_F_RXCSUM))
2943 		liquidio_set_rxcsum_command(netdev,
2944 					    OCTNET_CMD_TNL_RX_CSUM_CTL,
2945 					    OCTNET_CMD_RXCSUM_ENABLE);
2946 	else if ((netdev->features & NETIF_F_RXCSUM) &&
2947 		 (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
2948 		 !(features & NETIF_F_RXCSUM))
2949 		liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
2950 					    OCTNET_CMD_RXCSUM_DISABLE);
2951 
2952 	if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2953 	    (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2954 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
2955 		liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
2956 				     OCTNET_CMD_VLAN_FILTER_ENABLE);
2957 	else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2958 		 (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
2959 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
2960 		liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
2961 				     OCTNET_CMD_VLAN_FILTER_DISABLE);
2962 
2963 	return 0;
2964 }
2965 
2966 static void liquidio_add_vxlan_port(struct net_device *netdev,
2967 				    struct udp_tunnel_info *ti)
2968 {
2969 	if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2970 		return;
2971 
2972 	liquidio_vxlan_port_command(netdev,
2973 				    OCTNET_CMD_VXLAN_PORT_CONFIG,
2974 				    htons(ti->port),
2975 				    OCTNET_CMD_VXLAN_PORT_ADD);
2976 }
2977 
2978 static void liquidio_del_vxlan_port(struct net_device *netdev,
2979 				    struct udp_tunnel_info *ti)
2980 {
2981 	if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2982 		return;
2983 
2984 	liquidio_vxlan_port_command(netdev,
2985 				    OCTNET_CMD_VXLAN_PORT_CONFIG,
2986 				    htons(ti->port),
2987 				    OCTNET_CMD_VXLAN_PORT_DEL);
2988 }
2989 
2990 static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
2991 				 u8 *mac, bool is_admin_assigned)
2992 {
2993 	struct lio *lio = GET_LIO(netdev);
2994 	struct octeon_device *oct = lio->oct_dev;
2995 	struct octnic_ctrl_pkt nctrl;
2996 
2997 	if (!is_valid_ether_addr(mac))
2998 		return -EINVAL;
2999 
3000 	if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
3001 		return -EINVAL;
3002 
3003 	memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3004 
3005 	nctrl.ncmd.u64 = 0;
3006 	nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
3007 	/* vfidx is 0 based, but vf_num (param1) is 1 based */
3008 	nctrl.ncmd.s.param1 = vfidx + 1;
3009 	nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
3010 	nctrl.ncmd.s.more = 1;
3011 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3012 	nctrl.netpndev = (u64)netdev;
3013 	nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
3014 	nctrl.wait_time = LIO_CMD_WAIT_TM;
3015 
3016 	nctrl.udd[0] = 0;
3017 	/* The MAC Address is presented in network byte order. */
3018 	ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
3019 
3020 	oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
3021 
3022 	octnet_send_nic_ctrl_pkt(oct, &nctrl);
3023 
3024 	return 0;
3025 }
3026 
3027 static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
3028 {
3029 	struct lio *lio = GET_LIO(netdev);
3030 	struct octeon_device *oct = lio->oct_dev;
3031 	int retval;
3032 
3033 	if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3034 		return -EINVAL;
3035 
3036 	retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
3037 	if (!retval)
3038 		cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
3039 
3040 	return retval;
3041 }
3042 
3043 static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
3044 				u16 vlan, u8 qos, __be16 vlan_proto)
3045 {
3046 	struct lio *lio = GET_LIO(netdev);
3047 	struct octeon_device *oct = lio->oct_dev;
3048 	struct octnic_ctrl_pkt nctrl;
3049 	u16 vlantci;
3050 
3051 	if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3052 		return -EINVAL;
3053 
3054 	if (vlan_proto != htons(ETH_P_8021Q))
3055 		return -EPROTONOSUPPORT;
3056 
3057 	if (vlan >= VLAN_N_VID || qos > 7)
3058 		return -EINVAL;
3059 
3060 	if (vlan)
3061 		vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
3062 	else
3063 		vlantci = 0;
3064 
3065 	if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
3066 		return 0;
3067 
3068 	memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3069 
3070 	if (vlan)
3071 		nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
3072 	else
3073 		nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
3074 
3075 	nctrl.ncmd.s.param1 = vlantci;
3076 	nctrl.ncmd.s.param2 =
3077 	    vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
3078 	nctrl.ncmd.s.more = 0;
3079 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3080 	nctrl.cb_fn = 0;
3081 	nctrl.wait_time = LIO_CMD_WAIT_TM;
3082 
3083 	octnet_send_nic_ctrl_pkt(oct, &nctrl);
3084 
3085 	oct->sriov_info.vf_vlantci[vfidx] = vlantci;
3086 
3087 	return 0;
3088 }
3089 
3090 static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
3091 				  struct ifla_vf_info *ivi)
3092 {
3093 	struct lio *lio = GET_LIO(netdev);
3094 	struct octeon_device *oct = lio->oct_dev;
3095 	u8 *macaddr;
3096 
3097 	if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3098 		return -EINVAL;
3099 
3100 	ivi->vf = vfidx;
3101 	macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
3102 	ether_addr_copy(&ivi->mac[0], macaddr);
3103 	ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
3104 	ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
3105 	if (oct->sriov_info.trusted_vf.active &&
3106 	    oct->sriov_info.trusted_vf.id == vfidx)
3107 		ivi->trusted = true;
3108 	else
3109 		ivi->trusted = false;
3110 	ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
3111 	return 0;
3112 }
3113 
3114 static void trusted_vf_callback(struct octeon_device *oct_dev,
3115 				u32 status, void *ptr)
3116 {
3117 	struct octeon_soft_command *sc = (struct octeon_soft_command *)ptr;
3118 	struct lio_trusted_vf_ctx *ctx;
3119 
3120 	ctx = (struct lio_trusted_vf_ctx *)sc->ctxptr;
3121 	ctx->status = status;
3122 
3123 	complete(&ctx->complete);
3124 }
3125 
3126 static int liquidio_send_vf_trust_cmd(struct lio *lio, int vfidx, bool trusted)
3127 {
3128 	struct octeon_device *oct = lio->oct_dev;
3129 	struct lio_trusted_vf_ctx *ctx;
3130 	struct octeon_soft_command *sc;
3131 	int ctx_size, retval;
3132 
3133 	ctx_size = sizeof(struct lio_trusted_vf_ctx);
3134 	sc = octeon_alloc_soft_command(oct, 0, 0, ctx_size);
3135 
3136 	ctx  = (struct lio_trusted_vf_ctx *)sc->ctxptr;
3137 	init_completion(&ctx->complete);
3138 
3139 	sc->iq_no = lio->linfo.txpciq[0].s.q_no;
3140 
3141 	/* vfidx is 0 based, but vf_num (param1) is 1 based */
3142 	octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
3143 				    OPCODE_NIC_SET_TRUSTED_VF, 0, vfidx + 1,
3144 				    trusted);
3145 
3146 	sc->callback = trusted_vf_callback;
3147 	sc->callback_arg = sc;
3148 	sc->wait_time = 1000;
3149 
3150 	retval = octeon_send_soft_command(oct, sc);
3151 	if (retval == IQ_SEND_FAILED) {
3152 		retval = -1;
3153 	} else {
3154 		/* Wait for response or timeout */
3155 		if (wait_for_completion_timeout(&ctx->complete,
3156 						msecs_to_jiffies(2000)))
3157 			retval = ctx->status;
3158 		else
3159 			retval = -1;
3160 	}
3161 
3162 	octeon_free_soft_command(oct, sc);
3163 
3164 	return retval;
3165 }
3166 
3167 static int liquidio_set_vf_trust(struct net_device *netdev, int vfidx,
3168 				 bool setting)
3169 {
3170 	struct lio *lio = GET_LIO(netdev);
3171 	struct octeon_device *oct = lio->oct_dev;
3172 
3173 	if (strcmp(oct->fw_info.liquidio_firmware_version, "1.7.1") < 0) {
3174 		/* trusted vf is not supported by firmware older than 1.7.1 */
3175 		return -EOPNOTSUPP;
3176 	}
3177 
3178 	if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced) {
3179 		netif_info(lio, drv, lio->netdev, "Invalid vfidx %d\n", vfidx);
3180 		return -EINVAL;
3181 	}
3182 
3183 	if (setting) {
3184 		/* Set */
3185 
3186 		if (oct->sriov_info.trusted_vf.active &&
3187 		    oct->sriov_info.trusted_vf.id == vfidx)
3188 			return 0;
3189 
3190 		if (oct->sriov_info.trusted_vf.active) {
3191 			netif_info(lio, drv, lio->netdev, "More than one trusted VF is not allowed\n");
3192 			return -EPERM;
3193 		}
3194 	} else {
3195 		/* Clear */
3196 
3197 		if (!oct->sriov_info.trusted_vf.active)
3198 			return 0;
3199 	}
3200 
3201 	if (!liquidio_send_vf_trust_cmd(lio, vfidx, setting)) {
3202 		if (setting) {
3203 			oct->sriov_info.trusted_vf.id = vfidx;
3204 			oct->sriov_info.trusted_vf.active = true;
3205 		} else {
3206 			oct->sriov_info.trusted_vf.active = false;
3207 		}
3208 
3209 		netif_info(lio, drv, lio->netdev, "VF %u is %strusted\n", vfidx,
3210 			   setting ? "" : "not ");
3211 	} else {
3212 		netif_info(lio, drv, lio->netdev, "Failed to set VF trusted\n");
3213 		return -1;
3214 	}
3215 
3216 	return 0;
3217 }
3218 
3219 static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
3220 				      int linkstate)
3221 {
3222 	struct lio *lio = GET_LIO(netdev);
3223 	struct octeon_device *oct = lio->oct_dev;
3224 	struct octnic_ctrl_pkt nctrl;
3225 
3226 	if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3227 		return -EINVAL;
3228 
3229 	if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
3230 		return 0;
3231 
3232 	memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
3233 	nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
3234 	nctrl.ncmd.s.param1 =
3235 	    vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
3236 	nctrl.ncmd.s.param2 = linkstate;
3237 	nctrl.ncmd.s.more = 0;
3238 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
3239 	nctrl.cb_fn = 0;
3240 	nctrl.wait_time = LIO_CMD_WAIT_TM;
3241 
3242 	octnet_send_nic_ctrl_pkt(oct, &nctrl);
3243 
3244 	oct->sriov_info.vf_linkstate[vfidx] = linkstate;
3245 
3246 	return 0;
3247 }
3248 
3249 static int
3250 liquidio_eswitch_mode_get(struct devlink *devlink, u16 *mode)
3251 {
3252 	struct lio_devlink_priv *priv;
3253 	struct octeon_device *oct;
3254 
3255 	priv = devlink_priv(devlink);
3256 	oct = priv->oct;
3257 
3258 	*mode = oct->eswitch_mode;
3259 
3260 	return 0;
3261 }
3262 
3263 static int
3264 liquidio_eswitch_mode_set(struct devlink *devlink, u16 mode)
3265 {
3266 	struct lio_devlink_priv *priv;
3267 	struct octeon_device *oct;
3268 	int ret = 0;
3269 
3270 	priv = devlink_priv(devlink);
3271 	oct = priv->oct;
3272 
3273 	if (!(oct->fw_info.app_cap_flags & LIQUIDIO_SWITCHDEV_CAP))
3274 		return -EINVAL;
3275 
3276 	if (oct->eswitch_mode == mode)
3277 		return 0;
3278 
3279 	switch (mode) {
3280 	case DEVLINK_ESWITCH_MODE_SWITCHDEV:
3281 		oct->eswitch_mode = mode;
3282 		ret = lio_vf_rep_create(oct);
3283 		break;
3284 
3285 	case DEVLINK_ESWITCH_MODE_LEGACY:
3286 		lio_vf_rep_destroy(oct);
3287 		oct->eswitch_mode = mode;
3288 		break;
3289 
3290 	default:
3291 		ret = -EINVAL;
3292 	}
3293 
3294 	return ret;
3295 }
3296 
3297 static const struct devlink_ops liquidio_devlink_ops = {
3298 	.eswitch_mode_get = liquidio_eswitch_mode_get,
3299 	.eswitch_mode_set = liquidio_eswitch_mode_set,
3300 };
3301 
3302 static int
3303 lio_pf_switchdev_attr_get(struct net_device *dev, struct switchdev_attr *attr)
3304 {
3305 	struct lio *lio = GET_LIO(dev);
3306 	struct octeon_device *oct = lio->oct_dev;
3307 
3308 	if (oct->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
3309 		return -EOPNOTSUPP;
3310 
3311 	switch (attr->id) {
3312 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
3313 		attr->u.ppid.id_len = ETH_ALEN;
3314 		ether_addr_copy(attr->u.ppid.id,
3315 				(void *)&lio->linfo.hw_addr + 2);
3316 		break;
3317 
3318 	default:
3319 		return -EOPNOTSUPP;
3320 	}
3321 
3322 	return 0;
3323 }
3324 
3325 static const struct switchdev_ops lio_pf_switchdev_ops = {
3326 	.switchdev_port_attr_get = lio_pf_switchdev_attr_get,
3327 };
3328 
3329 static int liquidio_get_vf_stats(struct net_device *netdev, int vfidx,
3330 				 struct ifla_vf_stats *vf_stats)
3331 {
3332 	struct lio *lio = GET_LIO(netdev);
3333 	struct octeon_device *oct = lio->oct_dev;
3334 	struct oct_vf_stats stats;
3335 	int ret;
3336 
3337 	if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
3338 		return -EINVAL;
3339 
3340 	memset(&stats, 0, sizeof(struct oct_vf_stats));
3341 	ret = cn23xx_get_vf_stats(oct, vfidx, &stats);
3342 	if (!ret) {
3343 		vf_stats->rx_packets = stats.rx_packets;
3344 		vf_stats->tx_packets = stats.tx_packets;
3345 		vf_stats->rx_bytes = stats.rx_bytes;
3346 		vf_stats->tx_bytes = stats.tx_bytes;
3347 		vf_stats->broadcast = stats.broadcast;
3348 		vf_stats->multicast = stats.multicast;
3349 	}
3350 
3351 	return ret;
3352 }
3353 
3354 static const struct net_device_ops lionetdevops = {
3355 	.ndo_open		= liquidio_open,
3356 	.ndo_stop		= liquidio_stop,
3357 	.ndo_start_xmit		= liquidio_xmit,
3358 	.ndo_get_stats		= liquidio_get_stats,
3359 	.ndo_set_mac_address	= liquidio_set_mac,
3360 	.ndo_set_rx_mode	= liquidio_set_mcast_list,
3361 	.ndo_tx_timeout		= liquidio_tx_timeout,
3362 
3363 	.ndo_vlan_rx_add_vid    = liquidio_vlan_rx_add_vid,
3364 	.ndo_vlan_rx_kill_vid   = liquidio_vlan_rx_kill_vid,
3365 	.ndo_change_mtu		= liquidio_change_mtu,
3366 	.ndo_do_ioctl		= liquidio_ioctl,
3367 	.ndo_fix_features	= liquidio_fix_features,
3368 	.ndo_set_features	= liquidio_set_features,
3369 	.ndo_udp_tunnel_add	= liquidio_add_vxlan_port,
3370 	.ndo_udp_tunnel_del	= liquidio_del_vxlan_port,
3371 	.ndo_set_vf_mac		= liquidio_set_vf_mac,
3372 	.ndo_set_vf_vlan	= liquidio_set_vf_vlan,
3373 	.ndo_get_vf_config	= liquidio_get_vf_config,
3374 	.ndo_set_vf_trust	= liquidio_set_vf_trust,
3375 	.ndo_set_vf_link_state  = liquidio_set_vf_link_state,
3376 	.ndo_get_vf_stats	= liquidio_get_vf_stats,
3377 };
3378 
3379 /** \brief Entry point for the liquidio module
3380  */
3381 static int __init liquidio_init(void)
3382 {
3383 	int i;
3384 	struct handshake *hs;
3385 
3386 	init_completion(&first_stage);
3387 
3388 	octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
3389 
3390 	if (liquidio_init_pci())
3391 		return -EINVAL;
3392 
3393 	wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
3394 
3395 	for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3396 		hs = &handshake[i];
3397 		if (hs->pci_dev) {
3398 			wait_for_completion(&hs->init);
3399 			if (!hs->init_ok) {
3400 				/* init handshake failed */
3401 				dev_err(&hs->pci_dev->dev,
3402 					"Failed to init device\n");
3403 				liquidio_deinit_pci();
3404 				return -EIO;
3405 			}
3406 		}
3407 	}
3408 
3409 	for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
3410 		hs = &handshake[i];
3411 		if (hs->pci_dev) {
3412 			wait_for_completion_timeout(&hs->started,
3413 						    msecs_to_jiffies(30000));
3414 			if (!hs->started_ok) {
3415 				/* starter handshake failed */
3416 				dev_err(&hs->pci_dev->dev,
3417 					"Firmware failed to start\n");
3418 				liquidio_deinit_pci();
3419 				return -EIO;
3420 			}
3421 		}
3422 	}
3423 
3424 	return 0;
3425 }
3426 
3427 static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
3428 {
3429 	struct octeon_device *oct = (struct octeon_device *)buf;
3430 	struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
3431 	int gmxport = 0;
3432 	union oct_link_status *ls;
3433 	int i;
3434 
3435 	if (recv_pkt->buffer_size[0] != (sizeof(*ls) + OCT_DROQ_INFO_SIZE)) {
3436 		dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
3437 			recv_pkt->buffer_size[0],
3438 			recv_pkt->rh.r_nic_info.gmxport);
3439 		goto nic_info_err;
3440 	}
3441 
3442 	gmxport = recv_pkt->rh.r_nic_info.gmxport;
3443 	ls = (union oct_link_status *)(get_rbd(recv_pkt->buffer_ptr[0]) +
3444 		OCT_DROQ_INFO_SIZE);
3445 
3446 	octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
3447 	for (i = 0; i < oct->ifcount; i++) {
3448 		if (oct->props[i].gmxport == gmxport) {
3449 			update_link_status(oct->props[i].netdev, ls);
3450 			break;
3451 		}
3452 	}
3453 
3454 nic_info_err:
3455 	for (i = 0; i < recv_pkt->buffer_count; i++)
3456 		recv_buffer_free(recv_pkt->buffer_ptr[i]);
3457 	octeon_free_recv_info(recv_info);
3458 	return 0;
3459 }
3460 
3461 /**
3462  * \brief Setup network interfaces
3463  * @param octeon_dev  octeon device
3464  *
3465  * Called during init time for each device. It assumes the NIC
3466  * is already up and running.  The link information for each
3467  * interface is passed in link_info.
3468  */
3469 static int setup_nic_devices(struct octeon_device *octeon_dev)
3470 {
3471 	struct lio *lio = NULL;
3472 	struct net_device *netdev;
3473 	u8 mac[6], i, j, *fw_ver;
3474 	struct octeon_soft_command *sc;
3475 	struct liquidio_if_cfg_context *ctx;
3476 	struct liquidio_if_cfg_resp *resp;
3477 	struct octdev_props *props;
3478 	int retval, num_iqueues, num_oqueues;
3479 	union oct_nic_if_cfg if_cfg;
3480 	unsigned int base_queue;
3481 	unsigned int gmx_port_id;
3482 	u32 resp_size, ctx_size, data_size;
3483 	u32 ifidx_or_pfnum;
3484 	struct lio_version *vdata;
3485 	struct devlink *devlink;
3486 	struct lio_devlink_priv *lio_devlink;
3487 
3488 	/* This is to handle link status changes */
3489 	octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
3490 				    OPCODE_NIC_INFO,
3491 				    lio_nic_info, octeon_dev);
3492 
3493 	/* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
3494 	 * They are handled directly.
3495 	 */
3496 	octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
3497 					free_netbuf);
3498 
3499 	octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
3500 					free_netsgbuf);
3501 
3502 	octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
3503 					free_netsgbuf_with_resp);
3504 
3505 	for (i = 0; i < octeon_dev->ifcount; i++) {
3506 		resp_size = sizeof(struct liquidio_if_cfg_resp);
3507 		ctx_size = sizeof(struct liquidio_if_cfg_context);
3508 		data_size = sizeof(struct lio_version);
3509 		sc = (struct octeon_soft_command *)
3510 			octeon_alloc_soft_command(octeon_dev, data_size,
3511 						  resp_size, ctx_size);
3512 		resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
3513 		ctx  = (struct liquidio_if_cfg_context *)sc->ctxptr;
3514 		vdata = (struct lio_version *)sc->virtdptr;
3515 
3516 		*((u64 *)vdata) = 0;
3517 		vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
3518 		vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
3519 		vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
3520 
3521 		if (OCTEON_CN23XX_PF(octeon_dev)) {
3522 			num_iqueues = octeon_dev->sriov_info.num_pf_rings;
3523 			num_oqueues = octeon_dev->sriov_info.num_pf_rings;
3524 			base_queue = octeon_dev->sriov_info.pf_srn;
3525 
3526 			gmx_port_id = octeon_dev->pf_num;
3527 			ifidx_or_pfnum = octeon_dev->pf_num;
3528 		} else {
3529 			num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
3530 						octeon_get_conf(octeon_dev), i);
3531 			num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
3532 						octeon_get_conf(octeon_dev), i);
3533 			base_queue = CFG_GET_BASE_QUE_NIC_IF(
3534 						octeon_get_conf(octeon_dev), i);
3535 			gmx_port_id = CFG_GET_GMXID_NIC_IF(
3536 						octeon_get_conf(octeon_dev), i);
3537 			ifidx_or_pfnum = i;
3538 		}
3539 
3540 		dev_dbg(&octeon_dev->pci_dev->dev,
3541 			"requesting config for interface %d, iqs %d, oqs %d\n",
3542 			ifidx_or_pfnum, num_iqueues, num_oqueues);
3543 		WRITE_ONCE(ctx->cond, 0);
3544 		ctx->octeon_id = lio_get_device_id(octeon_dev);
3545 		init_waitqueue_head(&ctx->wc);
3546 
3547 		if_cfg.u64 = 0;
3548 		if_cfg.s.num_iqueues = num_iqueues;
3549 		if_cfg.s.num_oqueues = num_oqueues;
3550 		if_cfg.s.base_queue = base_queue;
3551 		if_cfg.s.gmx_port_id = gmx_port_id;
3552 
3553 		sc->iq_no = 0;
3554 
3555 		octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
3556 					    OPCODE_NIC_IF_CFG, 0,
3557 					    if_cfg.u64, 0);
3558 
3559 		sc->callback = if_cfg_callback;
3560 		sc->callback_arg = sc;
3561 		sc->wait_time = 3000;
3562 
3563 		retval = octeon_send_soft_command(octeon_dev, sc);
3564 		if (retval == IQ_SEND_FAILED) {
3565 			dev_err(&octeon_dev->pci_dev->dev,
3566 				"iq/oq config failed status: %x\n",
3567 				retval);
3568 			/* Soft instr is freed by driver in case of failure. */
3569 			goto setup_nic_dev_fail;
3570 		}
3571 
3572 		/* Sleep on a wait queue till the cond flag indicates that the
3573 		 * response arrived or timed-out.
3574 		 */
3575 		if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
3576 			dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
3577 			goto setup_nic_wait_intr;
3578 		}
3579 
3580 		retval = resp->status;
3581 		if (retval) {
3582 			dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
3583 			goto setup_nic_dev_fail;
3584 		}
3585 
3586 		/* Verify f/w version (in case of 'auto' loading from flash) */
3587 		fw_ver = octeon_dev->fw_info.liquidio_firmware_version;
3588 		if (memcmp(LIQUIDIO_BASE_VERSION,
3589 			   fw_ver,
3590 			   strlen(LIQUIDIO_BASE_VERSION))) {
3591 			dev_err(&octeon_dev->pci_dev->dev,
3592 				"Unmatched firmware version. Expected %s.x, got %s.\n",
3593 				LIQUIDIO_BASE_VERSION, fw_ver);
3594 			goto setup_nic_dev_fail;
3595 		} else if (atomic_read(octeon_dev->adapter_fw_state) ==
3596 			   FW_IS_PRELOADED) {
3597 			dev_info(&octeon_dev->pci_dev->dev,
3598 				 "Using auto-loaded firmware version %s.\n",
3599 				 fw_ver);
3600 		}
3601 
3602 		octeon_swap_8B_data((u64 *)(&resp->cfg_info),
3603 				    (sizeof(struct liquidio_if_cfg_info)) >> 3);
3604 
3605 		num_iqueues = hweight64(resp->cfg_info.iqmask);
3606 		num_oqueues = hweight64(resp->cfg_info.oqmask);
3607 
3608 		if (!(num_iqueues) || !(num_oqueues)) {
3609 			dev_err(&octeon_dev->pci_dev->dev,
3610 				"Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
3611 				resp->cfg_info.iqmask,
3612 				resp->cfg_info.oqmask);
3613 			goto setup_nic_dev_fail;
3614 		}
3615 		dev_dbg(&octeon_dev->pci_dev->dev,
3616 			"interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
3617 			i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
3618 			num_iqueues, num_oqueues);
3619 		netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
3620 
3621 		if (!netdev) {
3622 			dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
3623 			goto setup_nic_dev_fail;
3624 		}
3625 
3626 		SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
3627 
3628 		/* Associate the routines that will handle different
3629 		 * netdev tasks.
3630 		 */
3631 		netdev->netdev_ops = &lionetdevops;
3632 		SWITCHDEV_SET_OPS(netdev, &lio_pf_switchdev_ops);
3633 
3634 		lio = GET_LIO(netdev);
3635 
3636 		memset(lio, 0, sizeof(struct lio));
3637 
3638 		lio->ifidx = ifidx_or_pfnum;
3639 
3640 		props = &octeon_dev->props[i];
3641 		props->gmxport = resp->cfg_info.linfo.gmxport;
3642 		props->netdev = netdev;
3643 
3644 		lio->linfo.num_rxpciq = num_oqueues;
3645 		lio->linfo.num_txpciq = num_iqueues;
3646 		for (j = 0; j < num_oqueues; j++) {
3647 			lio->linfo.rxpciq[j].u64 =
3648 				resp->cfg_info.linfo.rxpciq[j].u64;
3649 		}
3650 		for (j = 0; j < num_iqueues; j++) {
3651 			lio->linfo.txpciq[j].u64 =
3652 				resp->cfg_info.linfo.txpciq[j].u64;
3653 		}
3654 		lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
3655 		lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
3656 		lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
3657 
3658 		lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3659 
3660 		if (OCTEON_CN23XX_PF(octeon_dev) ||
3661 		    OCTEON_CN6XXX(octeon_dev)) {
3662 			lio->dev_capability = NETIF_F_HIGHDMA
3663 					      | NETIF_F_IP_CSUM
3664 					      | NETIF_F_IPV6_CSUM
3665 					      | NETIF_F_SG | NETIF_F_RXCSUM
3666 					      | NETIF_F_GRO
3667 					      | NETIF_F_TSO | NETIF_F_TSO6
3668 					      | NETIF_F_LRO;
3669 		}
3670 		netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
3671 
3672 		/*  Copy of transmit encapsulation capabilities:
3673 		 *  TSO, TSO6, Checksums for this device
3674 		 */
3675 		lio->enc_dev_capability = NETIF_F_IP_CSUM
3676 					  | NETIF_F_IPV6_CSUM
3677 					  | NETIF_F_GSO_UDP_TUNNEL
3678 					  | NETIF_F_HW_CSUM | NETIF_F_SG
3679 					  | NETIF_F_RXCSUM
3680 					  | NETIF_F_TSO | NETIF_F_TSO6
3681 					  | NETIF_F_LRO;
3682 
3683 		netdev->hw_enc_features = (lio->enc_dev_capability &
3684 					   ~NETIF_F_LRO);
3685 
3686 		lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
3687 
3688 		netdev->vlan_features = lio->dev_capability;
3689 		/* Add any unchangeable hw features */
3690 		lio->dev_capability |=  NETIF_F_HW_VLAN_CTAG_FILTER |
3691 					NETIF_F_HW_VLAN_CTAG_RX |
3692 					NETIF_F_HW_VLAN_CTAG_TX;
3693 
3694 		netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
3695 
3696 		netdev->hw_features = lio->dev_capability;
3697 		/*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
3698 		netdev->hw_features = netdev->hw_features &
3699 			~NETIF_F_HW_VLAN_CTAG_RX;
3700 
3701 		/* MTU range: 68 - 16000 */
3702 		netdev->min_mtu = LIO_MIN_MTU_SIZE;
3703 		netdev->max_mtu = LIO_MAX_MTU_SIZE;
3704 
3705 		/* Point to the  properties for octeon device to which this
3706 		 * interface belongs.
3707 		 */
3708 		lio->oct_dev = octeon_dev;
3709 		lio->octprops = props;
3710 		lio->netdev = netdev;
3711 
3712 		dev_dbg(&octeon_dev->pci_dev->dev,
3713 			"if%d gmx: %d hw_addr: 0x%llx\n", i,
3714 			lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
3715 
3716 		for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
3717 			u8 vfmac[ETH_ALEN];
3718 
3719 			random_ether_addr(&vfmac[0]);
3720 			if (__liquidio_set_vf_mac(netdev, j,
3721 						  &vfmac[0], false)) {
3722 				dev_err(&octeon_dev->pci_dev->dev,
3723 					"Error setting VF%d MAC address\n",
3724 					j);
3725 				goto setup_nic_dev_fail;
3726 			}
3727 		}
3728 
3729 		/* 64-bit swap required on LE machines */
3730 		octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
3731 		for (j = 0; j < 6; j++)
3732 			mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
3733 
3734 		/* Copy MAC Address to OS network device structure */
3735 
3736 		ether_addr_copy(netdev->dev_addr, mac);
3737 
3738 		/* By default all interfaces on a single Octeon uses the same
3739 		 * tx and rx queues
3740 		 */
3741 		lio->txq = lio->linfo.txpciq[0].s.q_no;
3742 		lio->rxq = lio->linfo.rxpciq[0].s.q_no;
3743 		if (liquidio_setup_io_queues(octeon_dev, i,
3744 					     lio->linfo.num_txpciq,
3745 					     lio->linfo.num_rxpciq)) {
3746 			dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
3747 			goto setup_nic_dev_fail;
3748 		}
3749 
3750 		ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
3751 
3752 		lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
3753 		lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
3754 
3755 		if (setup_glists(octeon_dev, lio, num_iqueues)) {
3756 			dev_err(&octeon_dev->pci_dev->dev,
3757 				"Gather list allocation failed\n");
3758 			goto setup_nic_dev_fail;
3759 		}
3760 
3761 		/* Register ethtool support */
3762 		liquidio_set_ethtool_ops(netdev);
3763 		if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
3764 			octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
3765 		else
3766 			octeon_dev->priv_flags = 0x0;
3767 
3768 		if (netdev->features & NETIF_F_LRO)
3769 			liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
3770 					     OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
3771 
3772 		liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
3773 				     OCTNET_CMD_VLAN_FILTER_ENABLE);
3774 
3775 		if ((debug != -1) && (debug & NETIF_MSG_HW))
3776 			liquidio_set_feature(netdev,
3777 					     OCTNET_CMD_VERBOSE_ENABLE, 0);
3778 
3779 		if (setup_link_status_change_wq(netdev))
3780 			goto setup_nic_dev_fail;
3781 
3782 		if ((octeon_dev->fw_info.app_cap_flags &
3783 		     LIQUIDIO_TIME_SYNC_CAP) &&
3784 		    setup_sync_octeon_time_wq(netdev))
3785 			goto setup_nic_dev_fail;
3786 
3787 		if (setup_rx_oom_poll_fn(netdev))
3788 			goto setup_nic_dev_fail;
3789 
3790 		/* Register the network device with the OS */
3791 		if (register_netdev(netdev)) {
3792 			dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
3793 			goto setup_nic_dev_fail;
3794 		}
3795 
3796 		dev_dbg(&octeon_dev->pci_dev->dev,
3797 			"Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
3798 			i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3799 		netif_carrier_off(netdev);
3800 		lio->link_changes++;
3801 
3802 		ifstate_set(lio, LIO_IFSTATE_REGISTERED);
3803 
3804 		/* Sending command to firmware to enable Rx checksum offload
3805 		 * by default at the time of setup of Liquidio driver for
3806 		 * this device
3807 		 */
3808 		liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
3809 					    OCTNET_CMD_RXCSUM_ENABLE);
3810 		liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
3811 				     OCTNET_CMD_TXCSUM_ENABLE);
3812 
3813 		dev_dbg(&octeon_dev->pci_dev->dev,
3814 			"NIC ifidx:%d Setup successful\n", i);
3815 
3816 		octeon_free_soft_command(octeon_dev, sc);
3817 	}
3818 
3819 	devlink = devlink_alloc(&liquidio_devlink_ops,
3820 				sizeof(struct lio_devlink_priv));
3821 	if (!devlink) {
3822 		dev_err(&octeon_dev->pci_dev->dev, "devlink alloc failed\n");
3823 		goto setup_nic_wait_intr;
3824 	}
3825 
3826 	lio_devlink = devlink_priv(devlink);
3827 	lio_devlink->oct = octeon_dev;
3828 
3829 	if (devlink_register(devlink, &octeon_dev->pci_dev->dev)) {
3830 		devlink_free(devlink);
3831 		dev_err(&octeon_dev->pci_dev->dev,
3832 			"devlink registration failed\n");
3833 		goto setup_nic_wait_intr;
3834 	}
3835 
3836 	octeon_dev->devlink = devlink;
3837 	octeon_dev->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
3838 
3839 	return 0;
3840 
3841 setup_nic_dev_fail:
3842 
3843 	octeon_free_soft_command(octeon_dev, sc);
3844 
3845 setup_nic_wait_intr:
3846 
3847 	while (i--) {
3848 		dev_err(&octeon_dev->pci_dev->dev,
3849 			"NIC ifidx:%d Setup failed\n", i);
3850 		liquidio_destroy_nic_device(octeon_dev, i);
3851 	}
3852 	return -ENODEV;
3853 }
3854 
3855 #ifdef CONFIG_PCI_IOV
3856 static int octeon_enable_sriov(struct octeon_device *oct)
3857 {
3858 	unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
3859 	struct pci_dev *vfdev;
3860 	int err;
3861 	u32 u;
3862 
3863 	if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
3864 		err = pci_enable_sriov(oct->pci_dev,
3865 				       oct->sriov_info.num_vfs_alloced);
3866 		if (err) {
3867 			dev_err(&oct->pci_dev->dev,
3868 				"OCTEON: Failed to enable PCI sriov: %d\n",
3869 				err);
3870 			oct->sriov_info.num_vfs_alloced = 0;
3871 			return err;
3872 		}
3873 		oct->sriov_info.sriov_enabled = 1;
3874 
3875 		/* init lookup table that maps DPI ring number to VF pci_dev
3876 		 * struct pointer
3877 		 */
3878 		u = 0;
3879 		vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
3880 				       OCTEON_CN23XX_VF_VID, NULL);
3881 		while (vfdev) {
3882 			if (vfdev->is_virtfn &&
3883 			    (vfdev->physfn == oct->pci_dev)) {
3884 				oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
3885 					vfdev;
3886 				u += oct->sriov_info.rings_per_vf;
3887 			}
3888 			vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
3889 					       OCTEON_CN23XX_VF_VID, vfdev);
3890 		}
3891 	}
3892 
3893 	return num_vfs_alloced;
3894 }
3895 
3896 static int lio_pci_sriov_disable(struct octeon_device *oct)
3897 {
3898 	int u;
3899 
3900 	if (pci_vfs_assigned(oct->pci_dev)) {
3901 		dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
3902 		return -EPERM;
3903 	}
3904 
3905 	pci_disable_sriov(oct->pci_dev);
3906 
3907 	u = 0;
3908 	while (u < MAX_POSSIBLE_VFS) {
3909 		oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
3910 		u += oct->sriov_info.rings_per_vf;
3911 	}
3912 
3913 	oct->sriov_info.num_vfs_alloced = 0;
3914 	dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
3915 		 oct->pf_num);
3916 
3917 	return 0;
3918 }
3919 
3920 static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
3921 {
3922 	struct octeon_device *oct = pci_get_drvdata(dev);
3923 	int ret = 0;
3924 
3925 	if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
3926 	    (oct->sriov_info.sriov_enabled)) {
3927 		dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
3928 			 oct->pf_num, num_vfs);
3929 		return 0;
3930 	}
3931 
3932 	if (!num_vfs) {
3933 		lio_vf_rep_destroy(oct);
3934 		ret = lio_pci_sriov_disable(oct);
3935 	} else if (num_vfs > oct->sriov_info.max_vfs) {
3936 		dev_err(&oct->pci_dev->dev,
3937 			"OCTEON: Max allowed VFs:%d user requested:%d",
3938 			oct->sriov_info.max_vfs, num_vfs);
3939 		ret = -EPERM;
3940 	} else {
3941 		oct->sriov_info.num_vfs_alloced = num_vfs;
3942 		ret = octeon_enable_sriov(oct);
3943 		dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
3944 			 oct->pf_num, num_vfs);
3945 		ret = lio_vf_rep_create(oct);
3946 		if (ret)
3947 			dev_info(&oct->pci_dev->dev,
3948 				 "vf representor create failed");
3949 	}
3950 
3951 	return ret;
3952 }
3953 #endif
3954 
3955 /**
3956  * \brief initialize the NIC
3957  * @param oct octeon device
3958  *
3959  * This initialization routine is called once the Octeon device application is
3960  * up and running
3961  */
3962 static int liquidio_init_nic_module(struct octeon_device *oct)
3963 {
3964 	int i, retval = 0;
3965 	int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
3966 
3967 	dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
3968 
3969 	/* only default iq and oq were initialized
3970 	 * initialize the rest as well
3971 	 */
3972 	/* run port_config command for each port */
3973 	oct->ifcount = num_nic_ports;
3974 
3975 	memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
3976 
3977 	for (i = 0; i < MAX_OCTEON_LINKS; i++)
3978 		oct->props[i].gmxport = -1;
3979 
3980 	retval = setup_nic_devices(oct);
3981 	if (retval) {
3982 		dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
3983 		goto octnet_init_failure;
3984 	}
3985 
3986 	/* Call vf_rep_modinit if the firmware is switchdev capable
3987 	 * and do it from the first liquidio function probed.
3988 	 */
3989 	if (!oct->octeon_id &&
3990 	    oct->fw_info.app_cap_flags & LIQUIDIO_SWITCHDEV_CAP) {
3991 		retval = lio_vf_rep_modinit();
3992 		if (retval) {
3993 			liquidio_stop_nic_module(oct);
3994 			goto octnet_init_failure;
3995 		}
3996 	}
3997 
3998 	liquidio_ptp_init(oct);
3999 
4000 	dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
4001 
4002 	return retval;
4003 
4004 octnet_init_failure:
4005 
4006 	oct->ifcount = 0;
4007 
4008 	return retval;
4009 }
4010 
4011 /**
4012  * \brief starter callback that invokes the remaining initialization work after
4013  * the NIC is up and running.
4014  * @param octptr  work struct work_struct
4015  */
4016 static void nic_starter(struct work_struct *work)
4017 {
4018 	struct octeon_device *oct;
4019 	struct cavium_wk *wk = (struct cavium_wk *)work;
4020 
4021 	oct = (struct octeon_device *)wk->ctxptr;
4022 
4023 	if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
4024 		return;
4025 
4026 	/* If the status of the device is CORE_OK, the core
4027 	 * application has reported its application type. Call
4028 	 * any registered handlers now and move to the RUNNING
4029 	 * state.
4030 	 */
4031 	if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
4032 		schedule_delayed_work(&oct->nic_poll_work.work,
4033 				      LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4034 		return;
4035 	}
4036 
4037 	atomic_set(&oct->status, OCT_DEV_RUNNING);
4038 
4039 	if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
4040 		dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
4041 
4042 		if (liquidio_init_nic_module(oct))
4043 			dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
4044 		else
4045 			handshake[oct->octeon_id].started_ok = 1;
4046 	} else {
4047 		dev_err(&oct->pci_dev->dev,
4048 			"Unexpected application running on NIC (%d). Check firmware.\n",
4049 			oct->app_mode);
4050 	}
4051 
4052 	complete(&handshake[oct->octeon_id].started);
4053 }
4054 
4055 static int
4056 octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
4057 {
4058 	struct octeon_device *oct = (struct octeon_device *)buf;
4059 	struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
4060 	int i, notice, vf_idx;
4061 	bool cores_crashed;
4062 	u64 *data, vf_num;
4063 
4064 	notice = recv_pkt->rh.r.ossp;
4065 	data = (u64 *)(get_rbd(recv_pkt->buffer_ptr[0]) + OCT_DROQ_INFO_SIZE);
4066 
4067 	/* the first 64-bit word of data is the vf_num */
4068 	vf_num = data[0];
4069 	octeon_swap_8B_data(&vf_num, 1);
4070 	vf_idx = (int)vf_num - 1;
4071 
4072 	cores_crashed = READ_ONCE(oct->cores_crashed);
4073 
4074 	if (notice == VF_DRV_LOADED) {
4075 		if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
4076 			oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
4077 			dev_info(&oct->pci_dev->dev,
4078 				 "driver for VF%d was loaded\n", vf_idx);
4079 			if (!cores_crashed)
4080 				try_module_get(THIS_MODULE);
4081 		}
4082 	} else if (notice == VF_DRV_REMOVED) {
4083 		if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
4084 			oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
4085 			dev_info(&oct->pci_dev->dev,
4086 				 "driver for VF%d was removed\n", vf_idx);
4087 			if (!cores_crashed)
4088 				module_put(THIS_MODULE);
4089 		}
4090 	} else if (notice == VF_DRV_MACADDR_CHANGED) {
4091 		u8 *b = (u8 *)&data[1];
4092 
4093 		oct->sriov_info.vf_macaddr[vf_idx] = data[1];
4094 		dev_info(&oct->pci_dev->dev,
4095 			 "VF driver changed VF%d's MAC address to %pM\n",
4096 			 vf_idx, b + 2);
4097 	}
4098 
4099 	for (i = 0; i < recv_pkt->buffer_count; i++)
4100 		recv_buffer_free(recv_pkt->buffer_ptr[i]);
4101 	octeon_free_recv_info(recv_info);
4102 
4103 	return 0;
4104 }
4105 
4106 /**
4107  * \brief Device initialization for each Octeon device that is probed
4108  * @param octeon_dev  octeon device
4109  */
4110 static int octeon_device_init(struct octeon_device *octeon_dev)
4111 {
4112 	int j, ret;
4113 	char bootcmd[] = "\n";
4114 	char *dbg_enb = NULL;
4115 	enum lio_fw_state fw_state;
4116 	struct octeon_device_priv *oct_priv =
4117 		(struct octeon_device_priv *)octeon_dev->priv;
4118 	atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
4119 
4120 	/* Enable access to the octeon device and make its DMA capability
4121 	 * known to the OS.
4122 	 */
4123 	if (octeon_pci_os_setup(octeon_dev))
4124 		return 1;
4125 
4126 	atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
4127 
4128 	/* Identify the Octeon type and map the BAR address space. */
4129 	if (octeon_chip_specific_setup(octeon_dev)) {
4130 		dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
4131 		return 1;
4132 	}
4133 
4134 	atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
4135 
4136 	/* Only add a reference after setting status 'OCT_DEV_PCI_MAP_DONE',
4137 	 * since that is what is required for the reference to be removed
4138 	 * during de-initialization (see 'octeon_destroy_resources').
4139 	 */
4140 	octeon_register_device(octeon_dev, octeon_dev->pci_dev->bus->number,
4141 			       PCI_SLOT(octeon_dev->pci_dev->devfn),
4142 			       PCI_FUNC(octeon_dev->pci_dev->devfn),
4143 			       true);
4144 
4145 	octeon_dev->app_mode = CVM_DRV_INVALID_APP;
4146 
4147 	/* CN23XX supports preloaded firmware if the following is true:
4148 	 *
4149 	 * The adapter indicates that firmware is currently running AND
4150 	 * 'fw_type' is 'auto'.
4151 	 *
4152 	 * (default state is NEEDS_TO_BE_LOADED, override it if appropriate).
4153 	 */
4154 	if (OCTEON_CN23XX_PF(octeon_dev) &&
4155 	    cn23xx_fw_loaded(octeon_dev) && fw_type_is_auto()) {
4156 		atomic_cmpxchg(octeon_dev->adapter_fw_state,
4157 			       FW_NEEDS_TO_BE_LOADED, FW_IS_PRELOADED);
4158 	}
4159 
4160 	/* If loading firmware, only first device of adapter needs to do so. */
4161 	fw_state = atomic_cmpxchg(octeon_dev->adapter_fw_state,
4162 				  FW_NEEDS_TO_BE_LOADED,
4163 				  FW_IS_BEING_LOADED);
4164 
4165 	/* Here, [local variable] 'fw_state' is set to one of:
4166 	 *
4167 	 *   FW_IS_PRELOADED:       No firmware is to be loaded (see above)
4168 	 *   FW_NEEDS_TO_BE_LOADED: The driver's first instance will load
4169 	 *                          firmware to the adapter.
4170 	 *   FW_IS_BEING_LOADED:    The driver's second instance will not load
4171 	 *                          firmware to the adapter.
4172 	 */
4173 
4174 	/* Prior to f/w load, perform a soft reset of the Octeon device;
4175 	 * if error resetting, return w/error.
4176 	 */
4177 	if (fw_state == FW_NEEDS_TO_BE_LOADED)
4178 		if (octeon_dev->fn_list.soft_reset(octeon_dev))
4179 			return 1;
4180 
4181 	/* Initialize the dispatch mechanism used to push packets arriving on
4182 	 * Octeon Output queues.
4183 	 */
4184 	if (octeon_init_dispatch_list(octeon_dev))
4185 		return 1;
4186 
4187 	octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4188 				    OPCODE_NIC_CORE_DRV_ACTIVE,
4189 				    octeon_core_drv_init,
4190 				    octeon_dev);
4191 
4192 	octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
4193 				    OPCODE_NIC_VF_DRV_NOTICE,
4194 				    octeon_recv_vf_drv_notice, octeon_dev);
4195 	INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
4196 	octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
4197 	schedule_delayed_work(&octeon_dev->nic_poll_work.work,
4198 			      LIQUIDIO_STARTER_POLL_INTERVAL_MS);
4199 
4200 	atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
4201 
4202 	if (octeon_set_io_queues_off(octeon_dev)) {
4203 		dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
4204 		return 1;
4205 	}
4206 
4207 	if (OCTEON_CN23XX_PF(octeon_dev)) {
4208 		ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4209 		if (ret) {
4210 			dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
4211 			return ret;
4212 		}
4213 	}
4214 
4215 	/* Initialize soft command buffer pool
4216 	 */
4217 	if (octeon_setup_sc_buffer_pool(octeon_dev)) {
4218 		dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
4219 		return 1;
4220 	}
4221 	atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
4222 
4223 	/*  Setup the data structures that manage this Octeon's Input queues. */
4224 	if (octeon_setup_instr_queues(octeon_dev)) {
4225 		dev_err(&octeon_dev->pci_dev->dev,
4226 			"instruction queue initialization failed\n");
4227 		return 1;
4228 	}
4229 	atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
4230 
4231 	/* Initialize lists to manage the requests of different types that
4232 	 * arrive from user & kernel applications for this octeon device.
4233 	 */
4234 	if (octeon_setup_response_list(octeon_dev)) {
4235 		dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
4236 		return 1;
4237 	}
4238 	atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
4239 
4240 	if (octeon_setup_output_queues(octeon_dev)) {
4241 		dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
4242 		return 1;
4243 	}
4244 
4245 	atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
4246 
4247 	if (OCTEON_CN23XX_PF(octeon_dev)) {
4248 		if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
4249 			dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
4250 			return 1;
4251 		}
4252 		atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
4253 
4254 		if (octeon_allocate_ioq_vector(octeon_dev)) {
4255 			dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
4256 			return 1;
4257 		}
4258 		atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
4259 
4260 	} else {
4261 		/* The input and output queue registers were setup earlier (the
4262 		 * queues were not enabled). Any additional registers
4263 		 * that need to be programmed should be done now.
4264 		 */
4265 		ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
4266 		if (ret) {
4267 			dev_err(&octeon_dev->pci_dev->dev,
4268 				"Failed to configure device registers\n");
4269 			return ret;
4270 		}
4271 	}
4272 
4273 	/* Initialize the tasklet that handles output queue packet processing.*/
4274 	dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
4275 	tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
4276 		     (unsigned long)octeon_dev);
4277 
4278 	/* Setup the interrupt handler and record the INT SUM register address
4279 	 */
4280 	if (octeon_setup_interrupt(octeon_dev,
4281 				   octeon_dev->sriov_info.num_pf_rings))
4282 		return 1;
4283 
4284 	/* Enable Octeon device interrupts */
4285 	octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
4286 
4287 	atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
4288 
4289 	/* Send Credit for Octeon Output queues. Credits are always sent BEFORE
4290 	 * the output queue is enabled.
4291 	 * This ensures that we'll receive the f/w CORE DRV_ACTIVE message in
4292 	 * case we've configured CN23XX_SLI_GBL_CONTROL[NOPTR_D] = 0.
4293 	 * Otherwise, it is possible that the DRV_ACTIVE message will be sent
4294 	 * before any credits have been issued, causing the ring to be reset
4295 	 * (and the f/w appear to never have started).
4296 	 */
4297 	for (j = 0; j < octeon_dev->num_oqs; j++)
4298 		writel(octeon_dev->droq[j]->max_count,
4299 		       octeon_dev->droq[j]->pkts_credit_reg);
4300 
4301 	/* Enable the input and output queues for this Octeon device */
4302 	ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
4303 	if (ret) {
4304 		dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
4305 		return ret;
4306 	}
4307 
4308 	atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
4309 
4310 	if (fw_state == FW_NEEDS_TO_BE_LOADED) {
4311 		dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
4312 		if (!ddr_timeout) {
4313 			dev_info(&octeon_dev->pci_dev->dev,
4314 				 "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
4315 		}
4316 
4317 		schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
4318 
4319 		/* Wait for the octeon to initialize DDR after the soft-reset.*/
4320 		while (!ddr_timeout) {
4321 			set_current_state(TASK_INTERRUPTIBLE);
4322 			if (schedule_timeout(HZ / 10)) {
4323 				/* user probably pressed Control-C */
4324 				return 1;
4325 			}
4326 		}
4327 		ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
4328 		if (ret) {
4329 			dev_err(&octeon_dev->pci_dev->dev,
4330 				"DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
4331 				ret);
4332 			return 1;
4333 		}
4334 
4335 		if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
4336 			dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
4337 			return 1;
4338 		}
4339 
4340 		/* Divert uboot to take commands from host instead. */
4341 		ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
4342 
4343 		dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
4344 		ret = octeon_init_consoles(octeon_dev);
4345 		if (ret) {
4346 			dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
4347 			return 1;
4348 		}
4349 		/* If console debug enabled, specify empty string to use default
4350 		 * enablement ELSE specify NULL string for 'disabled'.
4351 		 */
4352 		dbg_enb = octeon_console_debug_enabled(0) ? "" : NULL;
4353 		ret = octeon_add_console(octeon_dev, 0, dbg_enb);
4354 		if (ret) {
4355 			dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
4356 			return 1;
4357 		} else if (octeon_console_debug_enabled(0)) {
4358 			/* If console was added AND we're logging console output
4359 			 * then set our console print function.
4360 			 */
4361 			octeon_dev->console[0].print = octeon_dbg_console_print;
4362 		}
4363 
4364 		atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
4365 
4366 		dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
4367 		ret = load_firmware(octeon_dev);
4368 		if (ret) {
4369 			dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
4370 			return 1;
4371 		}
4372 
4373 		atomic_set(octeon_dev->adapter_fw_state, FW_HAS_BEEN_LOADED);
4374 	}
4375 
4376 	handshake[octeon_dev->octeon_id].init_ok = 1;
4377 	complete(&handshake[octeon_dev->octeon_id].init);
4378 
4379 	atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
4380 
4381 	return 0;
4382 }
4383 
4384 /**
4385  * \brief Debug console print function
4386  * @param octeon_dev  octeon device
4387  * @param console_num console number
4388  * @param prefix      first portion of line to display
4389  * @param suffix      second portion of line to display
4390  *
4391  * The OCTEON debug console outputs entire lines (excluding '\n').
4392  * Normally, the line will be passed in the 'prefix' parameter.
4393  * However, due to buffering, it is possible for a line to be split into two
4394  * parts, in which case they will be passed as the 'prefix' parameter and
4395  * 'suffix' parameter.
4396  */
4397 static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
4398 				    char *prefix, char *suffix)
4399 {
4400 	if (prefix && suffix)
4401 		dev_info(&oct->pci_dev->dev, "%u: %s%s\n", console_num, prefix,
4402 			 suffix);
4403 	else if (prefix)
4404 		dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, prefix);
4405 	else if (suffix)
4406 		dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, suffix);
4407 
4408 	return 0;
4409 }
4410 
4411 /**
4412  * \brief Exits the module
4413  */
4414 static void __exit liquidio_exit(void)
4415 {
4416 	liquidio_deinit_pci();
4417 
4418 	pr_info("LiquidIO network module is now unloaded\n");
4419 }
4420 
4421 module_init(liquidio_init);
4422 module_exit(liquidio_exit);
4423