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