xref: /freebsd/sys/dev/liquidio/lio_main.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Cavium, Inc.. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Cavium, Inc. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*$FreeBSD$*/
34 
35 #include "lio_bsd.h"
36 #include "lio_common.h"
37 
38 #include "lio_droq.h"
39 #include "lio_iq.h"
40 #include "lio_response_manager.h"
41 #include "lio_device.h"
42 #include "lio_ctrl.h"
43 #include "lio_main.h"
44 #include "lio_network.h"
45 #include "cn23xx_pf_device.h"
46 #include "lio_image.h"
47 #include "lio_ioctl.h"
48 #include "lio_rxtx.h"
49 #include "lio_rss.h"
50 
51 /* Number of milliseconds to wait for DDR initialization */
52 #define LIO_DDR_TIMEOUT	10000
53 #define LIO_MAX_FW_TYPE_LEN	8
54 
55 static char fw_type[LIO_MAX_FW_TYPE_LEN];
56 TUNABLE_STR("hw.lio.fw_type", fw_type, sizeof(fw_type));
57 
58 /*
59  * Integers that specify number of queues per PF.
60  * Valid range is 0 to 64.
61  * Use 0 to derive from CPU count.
62  */
63 static int	num_queues_per_pf0;
64 static int	num_queues_per_pf1;
65 TUNABLE_INT("hw.lio.num_queues_per_pf0", &num_queues_per_pf0);
66 TUNABLE_INT("hw.lio.num_queues_per_pf1", &num_queues_per_pf1);
67 
68 #ifdef RSS
69 static int	lio_rss = 1;
70 TUNABLE_INT("hw.lio.rss", &lio_rss);
71 #endif	/* RSS */
72 
73 /* Hardware LRO */
74 unsigned int	lio_hwlro = 0;
75 TUNABLE_INT("hw.lio.hwlro", &lio_hwlro);
76 
77 /*
78  * Bitmask indicating which consoles have debug
79  * output redirected to syslog.
80  */
81 static unsigned long	console_bitmask;
82 TUNABLE_ULONG("hw.lio.console_bitmask", &console_bitmask);
83 
84 /*
85  * \brief determines if a given console has debug enabled.
86  * @param console console to check
87  * @returns  1 = enabled. 0 otherwise
88  */
89 int
90 lio_console_debug_enabled(uint32_t console)
91 {
92 
93 	return (console_bitmask >> (console)) & 0x1;
94 }
95 
96 static int	lio_detach(device_t dev);
97 
98 static int	lio_device_init(struct octeon_device *octeon_dev);
99 static int	lio_chip_specific_setup(struct octeon_device *oct);
100 static void	lio_watchdog(void *param);
101 static int	lio_load_firmware(struct octeon_device *oct);
102 static int	lio_nic_starter(struct octeon_device *oct);
103 static int	lio_init_nic_module(struct octeon_device *oct);
104 static int	lio_setup_nic_devices(struct octeon_device *octeon_dev);
105 static int	lio_link_info(struct lio_recv_info *recv_info, void *ptr);
106 static void	lio_if_cfg_callback(struct octeon_device *oct, uint32_t status,
107 				    void *buf);
108 static int	lio_set_rxcsum_command(if_t ifp, int command,
109 				       uint8_t rx_cmd);
110 static int	lio_setup_glists(struct octeon_device *oct, struct lio *lio,
111 				 int num_iqs);
112 static void	lio_destroy_nic_device(struct octeon_device *oct, int ifidx);
113 static inline void	lio_update_link_status(if_t ifp,
114 					       union octeon_link_status *ls);
115 static void	lio_send_rx_ctrl_cmd(struct lio *lio, int start_stop);
116 static int	lio_stop_nic_module(struct octeon_device *oct);
117 static void	lio_destroy_resources(struct octeon_device *oct);
118 static int	lio_setup_rx_oom_poll_fn(if_t ifp);
119 
120 static void	lio_vlan_rx_add_vid(void *arg, if_t ifp, uint16_t vid);
121 static void	lio_vlan_rx_kill_vid(void *arg, if_t ifp,
122 				     uint16_t vid);
123 static struct octeon_device *
124 	lio_get_other_octeon_device(struct octeon_device *oct);
125 
126 static int	lio_wait_for_oq_pkts(struct octeon_device *oct);
127 
128 int	lio_send_rss_param(struct lio *lio);
129 static int	lio_dbg_console_print(struct octeon_device *oct,
130 				      uint32_t console_num, char *prefix,
131 				      char *suffix);
132 
133 /* Polling interval for determining when NIC application is alive */
134 #define LIO_STARTER_POLL_INTERVAL_MS	100
135 
136 /*
137  * vendor_info_array.
138  * This array contains the list of IDs on which the driver should load.
139  */
140 struct lio_vendor_info {
141 	uint16_t	vendor_id;
142 	uint16_t	device_id;
143 	uint16_t	subdevice_id;
144 	uint8_t		revision_id;
145 	uint8_t		index;
146 };
147 
148 static struct lio_vendor_info lio_pci_tbl[] = {
149 	/* CN2350 10G */
150 	{PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_PF_VID, LIO_CN2350_10G_SUBDEVICE,
151 		0x02, 0},
152 
153 	/* CN2350 10G */
154 	{PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_PF_VID, LIO_CN2350_10G_SUBDEVICE1,
155 		0x02, 0},
156 
157 	/* CN2360 10G */
158 	{PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_PF_VID, LIO_CN2360_10G_SUBDEVICE,
159 		0x02, 1},
160 
161 	/* CN2350 25G */
162 	{PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_PF_VID, LIO_CN2350_25G_SUBDEVICE,
163 		0x02, 2},
164 
165 	/* CN2360 25G */
166 	{PCI_VENDOR_ID_CAVIUM, LIO_CN23XX_PF_VID, LIO_CN2360_25G_SUBDEVICE,
167 		0x02, 3},
168 
169 	{0, 0, 0, 0, 0}
170 };
171 
172 static char *lio_strings[] = {
173 	"LiquidIO 2350 10GbE Server Adapter",
174 	"LiquidIO 2360 10GbE Server Adapter",
175 	"LiquidIO 2350 25GbE Server Adapter",
176 	"LiquidIO 2360 25GbE Server Adapter",
177 };
178 
179 struct lio_if_cfg_resp {
180 	uint64_t	rh;
181 	struct octeon_if_cfg_info cfg_info;
182 	uint64_t	status;
183 };
184 
185 struct lio_if_cfg_context {
186 	int		octeon_id;
187 	volatile int	cond;
188 };
189 
190 struct lio_rx_ctl_context {
191 	int		octeon_id;
192 	volatile int	cond;
193 };
194 
195 static int
196 lio_probe(device_t dev)
197 {
198 	struct lio_vendor_info	*tbl;
199 
200 	uint16_t	vendor_id;
201 	uint16_t	device_id;
202 	uint16_t	subdevice_id;
203 	uint8_t		revision_id;
204 	char		device_ver[256];
205 
206 	vendor_id = pci_get_vendor(dev);
207 	if (vendor_id != PCI_VENDOR_ID_CAVIUM)
208 		return (ENXIO);
209 
210 	device_id = pci_get_device(dev);
211 	subdevice_id = pci_get_subdevice(dev);
212 	revision_id = pci_get_revid(dev);
213 
214 	tbl = lio_pci_tbl;
215 	while (tbl->vendor_id) {
216 		if ((vendor_id == tbl->vendor_id) &&
217 		    (device_id == tbl->device_id) &&
218 		    (subdevice_id == tbl->subdevice_id) &&
219 		    (revision_id == tbl->revision_id)) {
220 			sprintf(device_ver, "%s, Version - %s",
221 				lio_strings[tbl->index], LIO_VERSION);
222 			device_set_desc_copy(dev, device_ver);
223 			return (BUS_PROBE_DEFAULT);
224 		}
225 
226 		tbl++;
227 	}
228 
229 	return (ENXIO);
230 }
231 
232 static int
233 lio_attach(device_t device)
234 {
235 	struct octeon_device	*oct_dev = NULL;
236 	uint64_t	scratch1;
237 	uint32_t	error;
238 	int		timeout, ret = 1;
239 	uint8_t		bus, dev, function;
240 
241 	oct_dev = lio_allocate_device(device);
242 	if (oct_dev == NULL) {
243 		device_printf(device, "Error: Unable to allocate device\n");
244 		return (-ENOMEM);
245 	}
246 
247 	oct_dev->tx_budget = LIO_DEFAULT_TX_PKTS_PROCESS_BUDGET;
248 	oct_dev->rx_budget = LIO_DEFAULT_RX_PKTS_PROCESS_BUDGET;
249 	oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
250 
251 	oct_dev->device = device;
252 	bus = pci_get_bus(device);
253 	dev = pci_get_slot(device);
254 	function = pci_get_function(device);
255 
256 	lio_dev_info(oct_dev, "Initializing device %x:%x %02x:%02x.%01x\n",
257 		     pci_get_vendor(device), pci_get_device(device), bus, dev,
258 		     function);
259 
260 	if (lio_device_init(oct_dev)) {
261 		lio_dev_err(oct_dev, "Failed to init device\n");
262 		lio_detach(device);
263 		return (-ENOMEM);
264 	}
265 
266 	scratch1 = lio_read_csr64(oct_dev, LIO_CN23XX_SLI_SCRATCH1);
267 	if (!(scratch1 & 4ULL)) {
268 		/*
269 		 * Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
270 		 * the lio watchdog kernel thread is running for this
271 		 * NIC.  Each NIC gets one watchdog kernel thread.
272 		 */
273 		scratch1 |= 4ULL;
274 		lio_write_csr64(oct_dev, LIO_CN23XX_SLI_SCRATCH1, scratch1);
275 
276 		error = kproc_create(lio_watchdog, oct_dev,
277 				     &oct_dev->watchdog_task, 0, 0,
278 				     "liowd/%02hhx:%02hhx.%hhx", bus,
279 				     dev, function);
280 		if (!error) {
281 			kproc_resume(oct_dev->watchdog_task);
282 		} else {
283 			oct_dev->watchdog_task = NULL;
284 			lio_dev_err(oct_dev,
285 				    "failed to create kernel_thread\n");
286 			lio_detach(device);
287 			return (-1);
288 		}
289 	}
290 	oct_dev->rx_pause = 1;
291 	oct_dev->tx_pause = 1;
292 
293 	timeout = 0;
294 	while (timeout < LIO_NIC_STARTER_TIMEOUT) {
295 		lio_mdelay(LIO_STARTER_POLL_INTERVAL_MS);
296 		timeout += LIO_STARTER_POLL_INTERVAL_MS;
297 
298 		/*
299 		 * During the boot process interrupts are not available.
300 		 * So polling for first control message from FW.
301 		 */
302 		if (cold)
303 			lio_droq_bh(oct_dev->droq[0], 0);
304 
305 		if (atomic_load_acq_int(&oct_dev->status) == LIO_DEV_CORE_OK) {
306 			ret = lio_nic_starter(oct_dev);
307 			break;
308 		}
309 	}
310 
311 	if (ret) {
312 		lio_dev_err(oct_dev, "Firmware failed to start\n");
313 		lio_detach(device);
314 		return (-EIO);
315 	}
316 
317 	lio_dev_dbg(oct_dev, "Device is ready\n");
318 
319 	return (0);
320 }
321 
322 static int
323 lio_detach(device_t dev)
324 {
325 	struct octeon_device	*oct_dev = device_get_softc(dev);
326 
327 	lio_dev_dbg(oct_dev, "Stopping device\n");
328 	if (oct_dev->watchdog_task) {
329 		uint64_t	scratch1;
330 
331 		kproc_suspend(oct_dev->watchdog_task, 0);
332 
333 		scratch1 = lio_read_csr64(oct_dev, LIO_CN23XX_SLI_SCRATCH1);
334 		scratch1 &= ~4ULL;
335 		lio_write_csr64(oct_dev, LIO_CN23XX_SLI_SCRATCH1, scratch1);
336 	}
337 
338 	if (oct_dev->app_mode && (oct_dev->app_mode == LIO_DRV_NIC_APP))
339 		lio_stop_nic_module(oct_dev);
340 
341 	/*
342 	 * Reset the octeon device and cleanup all memory allocated for
343 	 * the octeon device by  driver.
344 	 */
345 	lio_destroy_resources(oct_dev);
346 
347 	lio_dev_info(oct_dev, "Device removed\n");
348 
349 	/*
350 	 * This octeon device has been removed. Update the global
351 	 * data structure to reflect this. Free the device structure.
352 	 */
353 	lio_free_device_mem(oct_dev);
354 	return (0);
355 }
356 
357 static int
358 lio_shutdown(device_t dev)
359 {
360 	struct octeon_device	*oct_dev = device_get_softc(dev);
361 	struct lio	*lio = if_getsoftc(oct_dev->props.ifp);
362 
363 	lio_send_rx_ctrl_cmd(lio, 0);
364 
365 	return (0);
366 }
367 
368 static int
369 lio_suspend(device_t dev)
370 {
371 
372 	return (ENXIO);
373 }
374 
375 static int
376 lio_resume(device_t dev)
377 {
378 
379 	return (ENXIO);
380 }
381 
382 static int
383 lio_event(struct module *mod, int event, void *junk)
384 {
385 
386 	switch (event) {
387 	case MOD_LOAD:
388 		lio_init_device_list(LIO_CFG_TYPE_DEFAULT);
389 		break;
390 	default:
391 		break;
392 	}
393 
394 	return (0);
395 }
396 
397 /*********************************************************************
398  *  FreeBSD Device Interface Entry Points
399  * *******************************************************************/
400 static device_method_t lio_methods[] = {
401 	/* Device interface */
402 	DEVMETHOD(device_probe, lio_probe),
403 	DEVMETHOD(device_attach, lio_attach),
404 	DEVMETHOD(device_detach, lio_detach),
405 	DEVMETHOD(device_shutdown, lio_shutdown),
406 	DEVMETHOD(device_suspend, lio_suspend),
407 	DEVMETHOD(device_resume, lio_resume),
408 	DEVMETHOD_END
409 };
410 
411 static driver_t lio_driver = {
412 	LIO_DRV_NAME, lio_methods, sizeof(struct octeon_device),
413 };
414 
415 DRIVER_MODULE(lio, pci, lio_driver, lio_event, NULL);
416 
417 MODULE_DEPEND(lio, pci, 1, 1, 1);
418 MODULE_DEPEND(lio, ether, 1, 1, 1);
419 MODULE_DEPEND(lio, firmware, 1, 1, 1);
420 
421 static bool
422 fw_type_is_none(void)
423 {
424 	return strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
425 		       sizeof(LIO_FW_NAME_TYPE_NONE)) == 0;
426 }
427 
428 /*
429  * \brief Device initialization for each Octeon device that is probed
430  * @param octeon_dev  octeon device
431  */
432 static int
433 lio_device_init(struct octeon_device *octeon_dev)
434 {
435 	unsigned long	ddr_timeout = LIO_DDR_TIMEOUT;
436 	char	*dbg_enb = NULL;
437 	int	fw_loaded = 0;
438 	int	i, j, ret;
439 	uint8_t	bus, dev, function;
440 	char	bootcmd[] = "\n";
441 
442 	bus = pci_get_bus(octeon_dev->device);
443 	dev = pci_get_slot(octeon_dev->device);
444 	function = pci_get_function(octeon_dev->device);
445 
446 	atomic_store_rel_int(&octeon_dev->status, LIO_DEV_BEGIN_STATE);
447 
448 	/* Enable access to the octeon device */
449 	if (pci_enable_busmaster(octeon_dev->device)) {
450 		lio_dev_err(octeon_dev, "pci_enable_device failed\n");
451 		return (1);
452 	}
453 
454 	atomic_store_rel_int(&octeon_dev->status, LIO_DEV_PCI_ENABLE_DONE);
455 
456 	/* Identify the Octeon type and map the BAR address space. */
457 	if (lio_chip_specific_setup(octeon_dev)) {
458 		lio_dev_err(octeon_dev, "Chip specific setup failed\n");
459 		return (1);
460 	}
461 
462 	atomic_store_rel_int(&octeon_dev->status, LIO_DEV_PCI_MAP_DONE);
463 
464 	/*
465 	 * Only add a reference after setting status 'OCT_DEV_PCI_MAP_DONE',
466 	 * since that is what is required for the reference to be removed
467 	 * during de-initialization (see 'octeon_destroy_resources').
468 	 */
469 	lio_register_device(octeon_dev, bus, dev, function, true);
470 
471 
472 	octeon_dev->app_mode = LIO_DRV_INVALID_APP;
473 
474 	if (!lio_cn23xx_pf_fw_loaded(octeon_dev) && !fw_type_is_none()) {
475 		fw_loaded = 0;
476 		/* Do a soft reset of the Octeon device. */
477 		if (octeon_dev->fn_list.soft_reset(octeon_dev))
478 			return (1);
479 
480 		/* things might have changed */
481 		if (!lio_cn23xx_pf_fw_loaded(octeon_dev))
482 			fw_loaded = 0;
483 		else
484 			fw_loaded = 1;
485 	} else {
486 		fw_loaded = 1;
487 	}
488 
489 	/*
490 	 * Initialize the dispatch mechanism used to push packets arriving on
491 	 * Octeon Output queues.
492 	 */
493 	if (lio_init_dispatch_list(octeon_dev))
494 		return (1);
495 
496 	lio_register_dispatch_fn(octeon_dev, LIO_OPCODE_NIC,
497 				 LIO_OPCODE_NIC_CORE_DRV_ACTIVE,
498 				 lio_core_drv_init, octeon_dev);
499 	atomic_store_rel_int(&octeon_dev->status, LIO_DEV_DISPATCH_INIT_DONE);
500 
501 	ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
502 	if (ret) {
503 		lio_dev_err(octeon_dev,
504 			    "Failed to configure device registers\n");
505 		return (ret);
506 	}
507 
508 	/* Initialize soft command buffer pool */
509 	if (lio_setup_sc_buffer_pool(octeon_dev)) {
510 		lio_dev_err(octeon_dev, "sc buffer pool allocation failed\n");
511 		return (1);
512 	}
513 
514 	atomic_store_rel_int(&octeon_dev->status,
515 			     LIO_DEV_SC_BUFF_POOL_INIT_DONE);
516 
517 	if (lio_allocate_ioq_vector(octeon_dev)) {
518 		lio_dev_err(octeon_dev,
519 			    "IOQ vector allocation failed\n");
520 		return (1);
521 	}
522 
523 	atomic_store_rel_int(&octeon_dev->status,
524 			     LIO_DEV_MSIX_ALLOC_VECTOR_DONE);
525 
526 	for (i = 0; i < LIO_MAX_POSSIBLE_INSTR_QUEUES; i++) {
527 		octeon_dev->instr_queue[i] =
528 			malloc(sizeof(struct lio_instr_queue),
529 			       M_DEVBUF, M_NOWAIT | M_ZERO);
530 		if (octeon_dev->instr_queue[i] == NULL)
531 			return (1);
532 	}
533 
534 	/* Setup the data structures that manage this Octeon's Input queues. */
535 	if (lio_setup_instr_queue0(octeon_dev)) {
536 		lio_dev_err(octeon_dev,
537 			    "Instruction queue initialization failed\n");
538 		return (1);
539 	}
540 
541 	atomic_store_rel_int(&octeon_dev->status,
542 			     LIO_DEV_INSTR_QUEUE_INIT_DONE);
543 
544 	/*
545 	 * Initialize lists to manage the requests of different types that
546 	 * arrive from user & kernel applications for this octeon device.
547 	 */
548 
549 	if (lio_setup_response_list(octeon_dev)) {
550 		lio_dev_err(octeon_dev, "Response list allocation failed\n");
551 		return (1);
552 	}
553 
554 	atomic_store_rel_int(&octeon_dev->status, LIO_DEV_RESP_LIST_INIT_DONE);
555 
556 	for (i = 0; i < LIO_MAX_POSSIBLE_OUTPUT_QUEUES; i++) {
557 		octeon_dev->droq[i] = malloc(sizeof(*octeon_dev->droq[i]),
558 					     M_DEVBUF, M_NOWAIT | M_ZERO);
559 		if (octeon_dev->droq[i] == NULL)
560 			return (1);
561 	}
562 
563 	if (lio_setup_output_queue0(octeon_dev)) {
564 		lio_dev_err(octeon_dev, "Output queue initialization failed\n");
565 		return (1);
566 	}
567 
568 	atomic_store_rel_int(&octeon_dev->status, LIO_DEV_DROQ_INIT_DONE);
569 
570 	/*
571 	 * Setup the interrupt handler and record the INT SUM register address
572 	 */
573 	if (lio_setup_interrupt(octeon_dev,
574 				octeon_dev->sriov_info.num_pf_rings))
575 		return (1);
576 
577 	/* Enable Octeon device interrupts */
578 	octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
579 
580 	atomic_store_rel_int(&octeon_dev->status, LIO_DEV_INTR_SET_DONE);
581 
582 	/*
583 	 * Send Credit for Octeon Output queues. Credits are always sent BEFORE
584 	 * the output queue is enabled.
585 	 * This ensures that we'll receive the f/w CORE DRV_ACTIVE message in
586 	 * case we've configured CN23XX_SLI_GBL_CONTROL[NOPTR_D] = 0.
587 	 * Otherwise, it is possible that the DRV_ACTIVE message will be sent
588 	 * before any credits have been issued, causing the ring to be reset
589 	 * (and the f/w appear to never have started).
590 	 */
591 	for (j = 0; j < octeon_dev->num_oqs; j++)
592 		lio_write_csr32(octeon_dev,
593 				octeon_dev->droq[j]->pkts_credit_reg,
594 				octeon_dev->droq[j]->max_count);
595 
596 	/* Enable the input and output queues for this Octeon device */
597 	ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
598 	if (ret) {
599 		lio_dev_err(octeon_dev, "Failed to enable input/output queues");
600 		return (ret);
601 	}
602 
603 	atomic_store_rel_int(&octeon_dev->status, LIO_DEV_IO_QUEUES_DONE);
604 
605 	if (!fw_loaded) {
606 		lio_dev_dbg(octeon_dev, "Waiting for DDR initialization...\n");
607 		if (!ddr_timeout) {
608 			lio_dev_info(octeon_dev,
609 				     "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
610 		}
611 
612 		lio_sleep_timeout(LIO_RESET_MSECS);
613 
614 		/*
615 		 * Wait for the octeon to initialize DDR after the
616 		 * soft-reset.
617 		 */
618 		while (!ddr_timeout) {
619 			if (pause("-", lio_ms_to_ticks(100))) {
620 				/* user probably pressed Control-C */
621 				return (1);
622 			}
623 		}
624 
625 		ret = lio_wait_for_ddr_init(octeon_dev, &ddr_timeout);
626 		if (ret) {
627 			lio_dev_err(octeon_dev,
628 				    "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
629 				    ret);
630 			return (1);
631 		}
632 
633 		if (lio_wait_for_bootloader(octeon_dev, 1100)) {
634 			lio_dev_err(octeon_dev, "Board not responding\n");
635 			return (1);
636 		}
637 
638 		/* Divert uboot to take commands from host instead. */
639 		ret = lio_console_send_cmd(octeon_dev, bootcmd, 50);
640 
641 		lio_dev_dbg(octeon_dev, "Initializing consoles\n");
642 		ret = lio_init_consoles(octeon_dev);
643 		if (ret) {
644 			lio_dev_err(octeon_dev, "Could not access board consoles\n");
645 			return (1);
646 		}
647 
648 		/*
649 		 * If console debug enabled, specify empty string to
650 		 * use default enablement ELSE specify NULL string for
651 		 * 'disabled'.
652 		 */
653 		dbg_enb = lio_console_debug_enabled(0) ? "" : NULL;
654 		ret = lio_add_console(octeon_dev, 0, dbg_enb);
655 
656 		if (ret) {
657 			lio_dev_err(octeon_dev, "Could not access board console\n");
658 			return (1);
659 		} else if (lio_console_debug_enabled(0)) {
660 			/*
661 			 * If console was added AND we're logging console output
662 			 * then set our console print function.
663 			 */
664 			octeon_dev->console[0].print = lio_dbg_console_print;
665 		}
666 
667 		atomic_store_rel_int(&octeon_dev->status,
668 				     LIO_DEV_CONSOLE_INIT_DONE);
669 
670 		lio_dev_dbg(octeon_dev, "Loading firmware\n");
671 
672 		ret = lio_load_firmware(octeon_dev);
673 		if (ret) {
674 			lio_dev_err(octeon_dev, "Could not load firmware to board\n");
675 			return (1);
676 		}
677 	}
678 
679 	atomic_store_rel_int(&octeon_dev->status, LIO_DEV_HOST_OK);
680 
681 	return (0);
682 }
683 
684 /*
685  * \brief PCI FLR for each Octeon device.
686  * @param oct octeon device
687  */
688 static void
689 lio_pci_flr(struct octeon_device *oct)
690 {
691 	uint32_t	exppos, status;
692 
693 	pci_find_cap(oct->device, PCIY_EXPRESS, &exppos);
694 
695 	pci_save_state(oct->device);
696 
697 	/* Quiesce the device completely */
698 	pci_write_config(oct->device, PCIR_COMMAND, PCIM_CMD_INTxDIS, 2);
699 
700 	/* Wait for Transaction Pending bit clean */
701 	lio_mdelay(100);
702 
703 	status = pci_read_config(oct->device, exppos + PCIER_DEVICE_STA, 2);
704 	if (status & PCIEM_STA_TRANSACTION_PND) {
705 		lio_dev_info(oct, "Function reset incomplete after 100ms, sleeping for 5 seconds\n");
706 		lio_mdelay(5);
707 
708 		status = pci_read_config(oct->device, exppos + PCIER_DEVICE_STA, 2);
709 		if (status & PCIEM_STA_TRANSACTION_PND)
710 			lio_dev_info(oct, "Function reset still incomplete after 5s, reset anyway\n");
711 	}
712 
713 	pci_write_config(oct->device, exppos + PCIER_DEVICE_CTL, PCIEM_CTL_INITIATE_FLR, 2);
714 	lio_mdelay(100);
715 
716 	pci_restore_state(oct->device);
717 }
718 
719 /*
720  * \brief Debug console print function
721  * @param octeon_dev  octeon device
722  * @param console_num console number
723  * @param prefix      first portion of line to display
724  * @param suffix      second portion of line to display
725  *
726  * The OCTEON debug console outputs entire lines (excluding '\n').
727  * Normally, the line will be passed in the 'prefix' parameter.
728  * However, due to buffering, it is possible for a line to be split into two
729  * parts, in which case they will be passed as the 'prefix' parameter and
730  * 'suffix' parameter.
731  */
732 static int
733 lio_dbg_console_print(struct octeon_device *oct, uint32_t console_num,
734 		      char *prefix, char *suffix)
735 {
736 
737 	if (prefix != NULL && suffix != NULL)
738 		lio_dev_info(oct, "%u: %s%s\n", console_num, prefix, suffix);
739 	else if (prefix != NULL)
740 		lio_dev_info(oct, "%u: %s\n", console_num, prefix);
741 	else if (suffix != NULL)
742 		lio_dev_info(oct, "%u: %s\n", console_num, suffix);
743 
744 	return (0);
745 }
746 
747 static void
748 lio_watchdog(void *param)
749 {
750 	int		core_num;
751 	uint16_t	mask_of_crashed_or_stuck_cores = 0;
752 	struct octeon_device	*oct = param;
753 	bool		err_msg_was_printed[12];
754 
755 	bzero(err_msg_was_printed, sizeof(err_msg_was_printed));
756 
757 	while (1) {
758 		kproc_suspend_check(oct->watchdog_task);
759 		mask_of_crashed_or_stuck_cores =
760 			(uint16_t)lio_read_csr64(oct, LIO_CN23XX_SLI_SCRATCH2);
761 
762 		if (mask_of_crashed_or_stuck_cores) {
763 			struct octeon_device *other_oct;
764 
765 			oct->cores_crashed = true;
766 			other_oct = lio_get_other_octeon_device(oct);
767 			if (other_oct != NULL)
768 				other_oct->cores_crashed = true;
769 
770 			for (core_num = 0; core_num < LIO_MAX_CORES;
771 			     core_num++) {
772 				bool core_crashed_or_got_stuck;
773 
774 				core_crashed_or_got_stuck =
775 				    (mask_of_crashed_or_stuck_cores >>
776 				     core_num) & 1;
777 				if (core_crashed_or_got_stuck &&
778 				    !err_msg_was_printed[core_num]) {
779 					lio_dev_err(oct,
780 						    "ERROR: Octeon core %d crashed or got stuck! See oct-fwdump for details.\n",
781 						    core_num);
782 					err_msg_was_printed[core_num] = true;
783 				}
784 			}
785 
786 		}
787 
788 		/* sleep for two seconds */
789 		pause("-", lio_ms_to_ticks(2000));
790 	}
791 }
792 
793 static int
794 lio_chip_specific_setup(struct octeon_device *oct)
795 {
796 	char		*s;
797 	uint32_t	dev_id;
798 	int		ret = 1;
799 
800 	dev_id = lio_read_pci_cfg(oct, 0);
801 	oct->subdevice_id = pci_get_subdevice(oct->device);
802 
803 	switch (dev_id) {
804 	case LIO_CN23XX_PF_PCIID:
805 		oct->chip_id = LIO_CN23XX_PF_VID;
806 		if (pci_get_function(oct->device) == 0) {
807 			if (num_queues_per_pf0 < 0) {
808 				lio_dev_info(oct, "Invalid num_queues_per_pf0: %d, Setting it to default\n",
809 					     num_queues_per_pf0);
810 				num_queues_per_pf0 = 0;
811 			}
812 
813 			oct->sriov_info.num_pf_rings = num_queues_per_pf0;
814 		} else {
815 			if (num_queues_per_pf1 < 0) {
816 				lio_dev_info(oct, "Invalid num_queues_per_pf1: %d, Setting it to default\n",
817 					     num_queues_per_pf1);
818 				num_queues_per_pf1 = 0;
819 			}
820 
821 			oct->sriov_info.num_pf_rings = num_queues_per_pf1;
822 		}
823 
824 		ret = lio_cn23xx_pf_setup_device(oct);
825 		s = "CN23XX";
826 		break;
827 
828 	default:
829 		s = "?";
830 		lio_dev_err(oct, "Unknown device found (dev_id: %x)\n", dev_id);
831 	}
832 
833 	if (!ret)
834 		lio_dev_info(oct, "%s PASS%d.%d %s Version: %s\n", s,
835 			     OCTEON_MAJOR_REV(oct), OCTEON_MINOR_REV(oct),
836 			     lio_get_conf(oct)->card_name, LIO_VERSION);
837 
838 	return (ret);
839 }
840 
841 static struct octeon_device *
842 lio_get_other_octeon_device(struct octeon_device *oct)
843 {
844 	struct octeon_device	*other_oct;
845 
846 	other_oct = lio_get_device(oct->octeon_id + 1);
847 
848 	if ((other_oct != NULL) && other_oct->device) {
849 		int	oct_busnum, other_oct_busnum;
850 
851 		oct_busnum = pci_get_bus(oct->device);
852 		other_oct_busnum = pci_get_bus(other_oct->device);
853 
854 		if (oct_busnum == other_oct_busnum) {
855 			int	oct_slot, other_oct_slot;
856 
857 			oct_slot = pci_get_slot(oct->device);
858 			other_oct_slot = pci_get_slot(other_oct->device);
859 
860 			if (oct_slot == other_oct_slot)
861 				return (other_oct);
862 		}
863 	}
864 	return (NULL);
865 }
866 
867 /*
868  * \brief Load firmware to device
869  * @param oct octeon device
870  *
871  * Maps device to firmware filename, requests firmware, and downloads it
872  */
873 static int
874 lio_load_firmware(struct octeon_device *oct)
875 {
876 	const struct firmware	*fw;
877 	char	*tmp_fw_type = NULL;
878 	int	ret = 0;
879 	char	fw_name[LIO_MAX_FW_FILENAME_LEN];
880 
881 	if (fw_type[0] == '\0')
882 		tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
883 	else
884 		tmp_fw_type = fw_type;
885 
886 	sprintf(fw_name, "%s%s_%s%s", LIO_FW_BASE_NAME,
887 		lio_get_conf(oct)->card_name, tmp_fw_type, LIO_FW_NAME_SUFFIX);
888 
889 	fw = firmware_get(fw_name);
890 	if (fw == NULL) {
891 		lio_dev_err(oct, "Request firmware failed. Could not find file %s.\n",
892 			    fw_name);
893 		return (EINVAL);
894 	}
895 
896 	ret = lio_download_firmware(oct, fw->data, fw->datasize);
897 
898 	firmware_put(fw, FIRMWARE_UNLOAD);
899 
900 	return (ret);
901 }
902 
903 static int
904 lio_nic_starter(struct octeon_device *oct)
905 {
906 	int	ret = 0;
907 
908 	atomic_store_rel_int(&oct->status, LIO_DEV_RUNNING);
909 
910 	if (oct->app_mode && oct->app_mode == LIO_DRV_NIC_APP) {
911 		if (lio_init_nic_module(oct)) {
912 			lio_dev_err(oct, "NIC initialization failed\n");
913 			ret = -1;
914 #ifdef CAVIUM_ONiLY_23XX_VF
915 		} else {
916 			if (octeon_enable_sriov(oct) < 0)
917 				ret = -1;
918 #endif
919 		}
920 	} else {
921 		lio_dev_err(oct,
922 			    "Unexpected application running on NIC (%d). Check firmware.\n",
923 			    oct->app_mode);
924 		ret = -1;
925 	}
926 
927 	return (ret);
928 }
929 
930 static int
931 lio_init_nic_module(struct octeon_device *oct)
932 {
933 	int	num_nic_ports = LIO_GET_NUM_NIC_PORTS_CFG(lio_get_conf(oct));
934 	int	retval = 0;
935 
936 	lio_dev_dbg(oct, "Initializing network interfaces\n");
937 
938 	/*
939 	 * only default iq and oq were initialized
940 	 * initialize the rest as well
941 	 */
942 
943 	/* run port_config command for each port */
944 	oct->ifcount = num_nic_ports;
945 
946 	bzero(&oct->props, sizeof(struct lio_if_props));
947 
948 	oct->props.gmxport = -1;
949 
950 	retval = lio_setup_nic_devices(oct);
951 	if (retval) {
952 		lio_dev_err(oct, "Setup NIC devices failed\n");
953 		goto lio_init_failure;
954 	}
955 
956 	lio_dev_dbg(oct, "Network interfaces ready\n");
957 
958 	return (retval);
959 
960 lio_init_failure:
961 
962 	oct->ifcount = 0;
963 
964 	return (retval);
965 }
966 
967 static int
968 lio_ifmedia_update(if_t ifp)
969 {
970 	struct lio	*lio = if_getsoftc(ifp);
971 	struct ifmedia	*ifm;
972 
973 	ifm = &lio->ifmedia;
974 
975 	/* We only support Ethernet media type. */
976 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
977 		return (EINVAL);
978 
979 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
980 	case IFM_AUTO:
981 		break;
982 	case IFM_10G_CX4:
983 	case IFM_10G_SR:
984 	case IFM_10G_T:
985 	case IFM_10G_TWINAX:
986 	default:
987 		/* We don't support changing the media type. */
988 		lio_dev_err(lio->oct_dev, "Invalid media type (%d)\n",
989 			    IFM_SUBTYPE(ifm->ifm_media));
990 		return (EINVAL);
991 	}
992 
993 	return (0);
994 }
995 
996 static int
997 lio_get_media_subtype(struct octeon_device *oct)
998 {
999 
1000 	switch(oct->subdevice_id) {
1001 	case LIO_CN2350_10G_SUBDEVICE:
1002 	case LIO_CN2350_10G_SUBDEVICE1:
1003 	case LIO_CN2360_10G_SUBDEVICE:
1004 		return (IFM_10G_SR);
1005 
1006 	case LIO_CN2350_25G_SUBDEVICE:
1007 	case LIO_CN2360_25G_SUBDEVICE:
1008 		return (IFM_25G_SR);
1009 	}
1010 
1011 	return (IFM_10G_SR);
1012 }
1013 
1014 static uint64_t
1015 lio_get_baudrate(struct octeon_device *oct)
1016 {
1017 
1018 	switch(oct->subdevice_id) {
1019 	case LIO_CN2350_10G_SUBDEVICE:
1020 	case LIO_CN2350_10G_SUBDEVICE1:
1021 	case LIO_CN2360_10G_SUBDEVICE:
1022 		return (IF_Gbps(10));
1023 
1024 	case LIO_CN2350_25G_SUBDEVICE:
1025 	case LIO_CN2360_25G_SUBDEVICE:
1026 		return (IF_Gbps(25));
1027 	}
1028 
1029 	return (IF_Gbps(10));
1030 }
1031 
1032 static void
1033 lio_ifmedia_status(if_t ifp, struct ifmediareq *ifmr)
1034 {
1035 	struct lio	*lio = if_getsoftc(ifp);
1036 
1037 	/* Report link down if the driver isn't running. */
1038 	if (!lio_ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
1039 		ifmr->ifm_active |= IFM_NONE;
1040 		return;
1041 	}
1042 
1043 	/* Setup the default interface info. */
1044 	ifmr->ifm_status = IFM_AVALID;
1045 	ifmr->ifm_active = IFM_ETHER;
1046 
1047 	if (lio->linfo.link.s.link_up) {
1048 		ifmr->ifm_status |= IFM_ACTIVE;
1049 	} else {
1050 		ifmr->ifm_active |= IFM_NONE;
1051 		return;
1052 	}
1053 
1054 	ifmr->ifm_active |= lio_get_media_subtype(lio->oct_dev);
1055 
1056 	if (lio->linfo.link.s.duplex)
1057 		ifmr->ifm_active |= IFM_FDX;
1058 	else
1059 		ifmr->ifm_active |= IFM_HDX;
1060 }
1061 
1062 static uint64_t
1063 lio_get_counter(if_t ifp, ift_counter cnt)
1064 {
1065 	struct lio	*lio = if_getsoftc(ifp);
1066 	struct octeon_device	*oct = lio->oct_dev;
1067 	uint64_t	counter = 0;
1068 	int		i, q_no;
1069 
1070 	switch (cnt) {
1071 	case IFCOUNTER_IPACKETS:
1072 		for (i = 0; i < oct->num_oqs; i++) {
1073 			q_no = lio->linfo.rxpciq[i].s.q_no;
1074 			counter += oct->droq[q_no]->stats.rx_pkts_received;
1075 		}
1076 		break;
1077 	case IFCOUNTER_OPACKETS:
1078 		for (i = 0; i < oct->num_iqs; i++) {
1079 			q_no = lio->linfo.txpciq[i].s.q_no;
1080 			counter += oct->instr_queue[q_no]->stats.tx_done;
1081 		}
1082 		break;
1083 	case IFCOUNTER_IBYTES:
1084 		for (i = 0; i < oct->num_oqs; i++) {
1085 			q_no = lio->linfo.rxpciq[i].s.q_no;
1086 			counter += oct->droq[q_no]->stats.rx_bytes_received;
1087 		}
1088 		break;
1089 	case IFCOUNTER_OBYTES:
1090 		for (i = 0; i < oct->num_iqs; i++) {
1091 			q_no = lio->linfo.txpciq[i].s.q_no;
1092 			counter += oct->instr_queue[q_no]->stats.tx_tot_bytes;
1093 		}
1094 		break;
1095 	case IFCOUNTER_IQDROPS:
1096 		for (i = 0; i < oct->num_oqs; i++) {
1097 			q_no = lio->linfo.rxpciq[i].s.q_no;
1098 			counter += oct->droq[q_no]->stats.rx_dropped;
1099 		}
1100 		break;
1101 	case IFCOUNTER_OQDROPS:
1102 		for (i = 0; i < oct->num_iqs; i++) {
1103 			q_no = lio->linfo.txpciq[i].s.q_no;
1104 			counter += oct->instr_queue[q_no]->stats.tx_dropped;
1105 		}
1106 		break;
1107 	case IFCOUNTER_IMCASTS:
1108 		counter = oct->link_stats.fromwire.total_mcst;
1109 		break;
1110 	case IFCOUNTER_OMCASTS:
1111 		counter = oct->link_stats.fromhost.mcast_pkts_sent;
1112 		break;
1113 	case IFCOUNTER_COLLISIONS:
1114 		counter = oct->link_stats.fromhost.total_collisions;
1115 		break;
1116 	case IFCOUNTER_IERRORS:
1117 		counter = oct->link_stats.fromwire.fcs_err +
1118 		    oct->link_stats.fromwire.l2_err +
1119 		    oct->link_stats.fromwire.frame_err;
1120 		break;
1121 	default:
1122 		return (if_get_counter_default(ifp, cnt));
1123 	}
1124 
1125 	return (counter);
1126 }
1127 
1128 static int
1129 lio_init_ifnet(struct lio *lio)
1130 {
1131 	struct octeon_device	*oct = lio->oct_dev;
1132 	if_t			ifp = lio->ifp;
1133 
1134 	/* ifconfig entrypoint for media type/status reporting */
1135 	ifmedia_init(&lio->ifmedia, IFM_IMASK, lio_ifmedia_update,
1136 		     lio_ifmedia_status);
1137 
1138 	/* set the default interface values */
1139 	ifmedia_add(&lio->ifmedia,
1140 		    (IFM_ETHER | IFM_FDX | lio_get_media_subtype(oct)),
1141 		    0, NULL);
1142 	ifmedia_add(&lio->ifmedia, (IFM_ETHER | IFM_AUTO), 0, NULL);
1143 	ifmedia_set(&lio->ifmedia, (IFM_ETHER | IFM_AUTO));
1144 
1145 	lio->ifmedia.ifm_media = lio->ifmedia.ifm_cur->ifm_media;
1146 	lio_dev_dbg(oct, "IFMEDIA flags : %x\n", lio->ifmedia.ifm_media);
1147 
1148 	if_initname(ifp, device_get_name(oct->device),
1149 		    device_get_unit(oct->device));
1150 	if_setflags(ifp, (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST));
1151 	if_setioctlfn(ifp, lio_ioctl);
1152 	if_setgetcounterfn(ifp, lio_get_counter);
1153 	if_settransmitfn(ifp, lio_mq_start);
1154 	if_setqflushfn(ifp, lio_qflush);
1155 	if_setinitfn(ifp, lio_open);
1156 	if_setmtu(ifp, lio->linfo.link.s.mtu);
1157 	lio->mtu = lio->linfo.link.s.mtu;
1158 	if_sethwassist(ifp, (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_TSO |
1159 			     CSUM_TCP_IPV6 | CSUM_UDP_IPV6));
1160 
1161 	if_setcapabilitiesbit(ifp, (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 |
1162 				    IFCAP_TSO | IFCAP_LRO |
1163 				    IFCAP_JUMBO_MTU | IFCAP_HWSTATS |
1164 				    IFCAP_LINKSTATE | IFCAP_VLAN_HWFILTER |
1165 				    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTAGGING |
1166 				    IFCAP_VLAN_HWTSO | IFCAP_VLAN_MTU), 0);
1167 
1168 	if_setcapenable(ifp, if_getcapabilities(ifp));
1169 	if_setbaudrate(ifp, lio_get_baudrate(oct));
1170 
1171 	return (0);
1172 }
1173 
1174 static void
1175 lio_tcp_lro_free(struct octeon_device *octeon_dev, if_t ifp)
1176 {
1177 	struct lio	*lio = if_getsoftc(ifp);
1178 	struct lio_droq	*droq;
1179 	int		q_no;
1180 	int		i;
1181 
1182 	for (i = 0; i < octeon_dev->num_oqs; i++) {
1183 		q_no = lio->linfo.rxpciq[i].s.q_no;
1184 		droq = octeon_dev->droq[q_no];
1185 		if (droq->lro.ifp) {
1186 			tcp_lro_free(&droq->lro);
1187 			droq->lro.ifp = NULL;
1188 		}
1189 	}
1190 }
1191 
1192 static int
1193 lio_tcp_lro_init(struct octeon_device *octeon_dev, if_t ifp)
1194 {
1195 	struct lio	*lio = if_getsoftc(ifp);
1196 	struct lio_droq	*droq;
1197 	struct lro_ctrl	*lro;
1198 	int		i, q_no, ret = 0;
1199 
1200 	for (i = 0; i < octeon_dev->num_oqs; i++) {
1201 		q_no = lio->linfo.rxpciq[i].s.q_no;
1202 		droq = octeon_dev->droq[q_no];
1203 		lro = &droq->lro;
1204 		ret = tcp_lro_init(lro);
1205 		if (ret) {
1206 			lio_dev_err(octeon_dev, "LRO Initialization failed ret %d\n",
1207 				    ret);
1208 			goto lro_init_failed;
1209 		}
1210 
1211 		lro->ifp = ifp;
1212 	}
1213 
1214 	return (ret);
1215 
1216 lro_init_failed:
1217 	lio_tcp_lro_free(octeon_dev, ifp);
1218 
1219 	return (ret);
1220 }
1221 
1222 static int
1223 lio_setup_nic_devices(struct octeon_device *octeon_dev)
1224 {
1225 	union		octeon_if_cfg if_cfg;
1226 	struct lio	*lio = NULL;
1227 	if_t		ifp = NULL;
1228 	struct lio_version		*vdata;
1229 	struct lio_soft_command		*sc;
1230 	struct lio_if_cfg_context	*ctx;
1231 	struct lio_if_cfg_resp		*resp;
1232 	struct lio_if_props		*props;
1233 	int		num_iqueues, num_oqueues, retval;
1234 	unsigned int	base_queue;
1235 	unsigned int	gmx_port_id;
1236 	uint32_t	ctx_size, data_size;
1237 	uint32_t	ifidx_or_pfnum, resp_size;
1238 	uint8_t		mac[ETHER_HDR_LEN], i, j;
1239 
1240 	/* This is to handle link status changes */
1241 	lio_register_dispatch_fn(octeon_dev, LIO_OPCODE_NIC,
1242 				 LIO_OPCODE_NIC_INFO,
1243 				 lio_link_info, octeon_dev);
1244 
1245 	for (i = 0; i < octeon_dev->ifcount; i++) {
1246 		resp_size = sizeof(struct lio_if_cfg_resp);
1247 		ctx_size = sizeof(struct lio_if_cfg_context);
1248 		data_size = sizeof(struct lio_version);
1249 		sc = lio_alloc_soft_command(octeon_dev, data_size, resp_size,
1250 					    ctx_size);
1251 		if (sc == NULL)
1252 			return (ENOMEM);
1253 
1254 		resp = (struct lio_if_cfg_resp *)sc->virtrptr;
1255 		ctx = (struct lio_if_cfg_context *)sc->ctxptr;
1256 		vdata = (struct lio_version *)sc->virtdptr;
1257 
1258 		*((uint64_t *)vdata) = 0;
1259 		vdata->major = htobe16(LIO_BASE_MAJOR_VERSION);
1260 		vdata->minor = htobe16(LIO_BASE_MINOR_VERSION);
1261 		vdata->micro = htobe16(LIO_BASE_MICRO_VERSION);
1262 
1263 		num_iqueues = octeon_dev->sriov_info.num_pf_rings;
1264 		num_oqueues = octeon_dev->sriov_info.num_pf_rings;
1265 		base_queue = octeon_dev->sriov_info.pf_srn;
1266 
1267 		gmx_port_id = octeon_dev->pf_num;
1268 		ifidx_or_pfnum = octeon_dev->pf_num;
1269 
1270 		lio_dev_dbg(octeon_dev, "requesting config for interface %d, iqs %d, oqs %d\n",
1271 			    ifidx_or_pfnum, num_iqueues, num_oqueues);
1272 		ctx->cond = 0;
1273 		ctx->octeon_id = lio_get_device_id(octeon_dev);
1274 
1275 		if_cfg.if_cfg64 = 0;
1276 		if_cfg.s.num_iqueues = num_iqueues;
1277 		if_cfg.s.num_oqueues = num_oqueues;
1278 		if_cfg.s.base_queue = base_queue;
1279 		if_cfg.s.gmx_port_id = gmx_port_id;
1280 
1281 		sc->iq_no = 0;
1282 
1283 		lio_prepare_soft_command(octeon_dev, sc, LIO_OPCODE_NIC,
1284 					 LIO_OPCODE_NIC_IF_CFG, 0,
1285 					 if_cfg.if_cfg64, 0);
1286 
1287 		sc->callback = lio_if_cfg_callback;
1288 		sc->callback_arg = sc;
1289 		sc->wait_time = 3000;
1290 
1291 		retval = lio_send_soft_command(octeon_dev, sc);
1292 		if (retval == LIO_IQ_SEND_FAILED) {
1293 			lio_dev_err(octeon_dev, "iq/oq config failed status: %x\n",
1294 				    retval);
1295 			/* Soft instr is freed by driver in case of failure. */
1296 			goto setup_nic_dev_fail;
1297 		}
1298 
1299 		/*
1300 		 * Sleep on a wait queue till the cond flag indicates that the
1301 		 * response arrived or timed-out.
1302 		 */
1303 		lio_sleep_cond(octeon_dev, &ctx->cond);
1304 
1305 		retval = resp->status;
1306 		if (retval) {
1307 			lio_dev_err(octeon_dev, "iq/oq config failed\n");
1308 			goto setup_nic_dev_fail;
1309 		}
1310 
1311 		lio_swap_8B_data((uint64_t *)(&resp->cfg_info),
1312 				 (sizeof(struct octeon_if_cfg_info)) >> 3);
1313 
1314 		num_iqueues = bitcount64(resp->cfg_info.iqmask);
1315 		num_oqueues = bitcount64(resp->cfg_info.oqmask);
1316 
1317 		if (!(num_iqueues) || !(num_oqueues)) {
1318 			lio_dev_err(octeon_dev,
1319 				    "Got bad iqueues (%016llX) or oqueues (%016llX) from firmware.\n",
1320 				    LIO_CAST64(resp->cfg_info.iqmask),
1321 				    LIO_CAST64(resp->cfg_info.oqmask));
1322 			goto setup_nic_dev_fail;
1323 		}
1324 
1325 		lio_dev_dbg(octeon_dev,
1326 			    "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
1327 			    i, LIO_CAST64(resp->cfg_info.iqmask),
1328 			    LIO_CAST64(resp->cfg_info.oqmask),
1329 			    num_iqueues, num_oqueues);
1330 
1331 		ifp = if_alloc(IFT_ETHER);
1332 
1333 		if (ifp == NULL) {
1334 			lio_dev_err(octeon_dev, "Device allocation failed\n");
1335 			goto setup_nic_dev_fail;
1336 		}
1337 
1338 		lio = malloc(sizeof(struct lio), M_DEVBUF, M_NOWAIT | M_ZERO);
1339 
1340 		if (lio == NULL) {
1341 			lio_dev_err(octeon_dev, "Lio allocation failed\n");
1342 			goto setup_nic_dev_fail;
1343 		}
1344 
1345 		if_setsoftc(ifp, lio);
1346 
1347 		if_sethwtsomax(ifp, LIO_MAX_FRAME_SIZE);
1348 		if_sethwtsomaxsegcount(ifp, LIO_MAX_SG);
1349 		if_sethwtsomaxsegsize(ifp, PAGE_SIZE);
1350 
1351 		lio->ifidx = ifidx_or_pfnum;
1352 
1353 		props = &octeon_dev->props;
1354 		props->gmxport = resp->cfg_info.linfo.gmxport;
1355 		props->ifp = ifp;
1356 
1357 		lio->linfo.num_rxpciq = num_oqueues;
1358 		lio->linfo.num_txpciq = num_iqueues;
1359 		for (j = 0; j < num_oqueues; j++) {
1360 			lio->linfo.rxpciq[j].rxpciq64 =
1361 			    resp->cfg_info.linfo.rxpciq[j].rxpciq64;
1362 		}
1363 
1364 		for (j = 0; j < num_iqueues; j++) {
1365 			lio->linfo.txpciq[j].txpciq64 =
1366 			    resp->cfg_info.linfo.txpciq[j].txpciq64;
1367 		}
1368 
1369 		lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
1370 		lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
1371 		lio->linfo.link.link_status64 =
1372 		    resp->cfg_info.linfo.link.link_status64;
1373 
1374 		/*
1375 		 * Point to the properties for octeon device to which this
1376 		 * interface belongs.
1377 		 */
1378 		lio->oct_dev = octeon_dev;
1379 		lio->ifp = ifp;
1380 
1381 		lio_dev_dbg(octeon_dev, "if%d gmx: %d hw_addr: 0x%llx\n", i,
1382 			    lio->linfo.gmxport, LIO_CAST64(lio->linfo.hw_addr));
1383 		lio_init_ifnet(lio);
1384 		/* 64-bit swap required on LE machines */
1385 		lio_swap_8B_data(&lio->linfo.hw_addr, 1);
1386 		for (j = 0; j < 6; j++)
1387 			mac[j] = *((uint8_t *)(
1388 				   ((uint8_t *)&lio->linfo.hw_addr) + 2 + j));
1389 
1390 		ether_ifattach(ifp, mac);
1391 
1392 		/*
1393 		 * By default all interfaces on a single Octeon uses the same
1394 		 * tx and rx queues
1395 		 */
1396 		lio->txq = lio->linfo.txpciq[0].s.q_no;
1397 		lio->rxq = lio->linfo.rxpciq[0].s.q_no;
1398 		if (lio_setup_io_queues(octeon_dev, i, lio->linfo.num_txpciq,
1399 					lio->linfo.num_rxpciq)) {
1400 			lio_dev_err(octeon_dev, "I/O queues creation failed\n");
1401 			goto setup_nic_dev_fail;
1402 		}
1403 
1404 		lio_ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
1405 
1406 		lio->tx_qsize = lio_get_tx_qsize(octeon_dev, lio->txq);
1407 		lio->rx_qsize = lio_get_rx_qsize(octeon_dev, lio->rxq);
1408 
1409 		if (lio_setup_glists(octeon_dev, lio, num_iqueues)) {
1410 			lio_dev_err(octeon_dev, "Gather list allocation failed\n");
1411 			goto setup_nic_dev_fail;
1412 		}
1413 
1414 		if ((lio_hwlro == 0) && lio_tcp_lro_init(octeon_dev, ifp))
1415 			goto setup_nic_dev_fail;
1416 
1417 		if (lio_hwlro &&
1418 		    (if_getcapenable(ifp) & IFCAP_LRO) &&
1419 		    (if_getcapenable(ifp) & IFCAP_RXCSUM) &&
1420 		    (if_getcapenable(ifp) & IFCAP_RXCSUM_IPV6))
1421 			lio_set_feature(ifp, LIO_CMD_LRO_ENABLE,
1422 					LIO_LROIPV4 | LIO_LROIPV6);
1423 
1424 		if ((if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER))
1425 			lio_set_feature(ifp, LIO_CMD_VLAN_FILTER_CTL, 1);
1426 		else
1427 			lio_set_feature(ifp, LIO_CMD_VLAN_FILTER_CTL, 0);
1428 
1429 		if (lio_setup_rx_oom_poll_fn(ifp))
1430 			goto setup_nic_dev_fail;
1431 
1432 		lio_dev_dbg(octeon_dev, "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
1433 			    i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1434 		lio->link_changes++;
1435 
1436 		lio_ifstate_set(lio, LIO_IFSTATE_REGISTERED);
1437 
1438 		/*
1439 		 * Sending command to firmware to enable Rx checksum offload
1440 		 * by default at the time of setup of Liquidio driver for
1441 		 * this device
1442 		 */
1443 		lio_set_rxcsum_command(ifp, LIO_CMD_TNL_RX_CSUM_CTL,
1444 				       LIO_CMD_RXCSUM_ENABLE);
1445 		lio_set_feature(ifp, LIO_CMD_TNL_TX_CSUM_CTL,
1446 				LIO_CMD_TXCSUM_ENABLE);
1447 
1448 #ifdef RSS
1449 		if (lio_rss) {
1450 			if (lio_send_rss_param(lio))
1451 				goto setup_nic_dev_fail;
1452 		} else
1453 #endif	/* RSS */
1454 
1455 			lio_set_feature(ifp, LIO_CMD_SET_FNV,
1456 					LIO_CMD_FNV_ENABLE);
1457 
1458 		lio_dev_dbg(octeon_dev, "NIC ifidx:%d Setup successful\n", i);
1459 
1460 		lio_free_soft_command(octeon_dev, sc);
1461 		lio->vlan_attach =
1462 		    EVENTHANDLER_REGISTER(vlan_config,
1463 					  lio_vlan_rx_add_vid, lio,
1464 					  EVENTHANDLER_PRI_FIRST);
1465 		lio->vlan_detach =
1466 		    EVENTHANDLER_REGISTER(vlan_unconfig,
1467 					  lio_vlan_rx_kill_vid, lio,
1468 					  EVENTHANDLER_PRI_FIRST);
1469 
1470 		/* Update stats periodically */
1471 		callout_init(&lio->stats_timer, 0);
1472 		lio->stats_interval = LIO_DEFAULT_STATS_INTERVAL;
1473 
1474 		lio_add_hw_stats(lio);
1475 	}
1476 
1477 	return (0);
1478 
1479 setup_nic_dev_fail:
1480 
1481 	lio_free_soft_command(octeon_dev, sc);
1482 
1483 	while (i--) {
1484 		lio_dev_err(octeon_dev, "NIC ifidx:%d Setup failed\n", i);
1485 		lio_destroy_nic_device(octeon_dev, i);
1486 	}
1487 
1488 	return (ENODEV);
1489 }
1490 
1491 static int
1492 lio_link_info(struct lio_recv_info *recv_info, void *ptr)
1493 {
1494 	struct octeon_device	*oct = (struct octeon_device *)ptr;
1495 	struct lio_recv_pkt	*recv_pkt = recv_info->recv_pkt;
1496 	union octeon_link_status *ls;
1497 	int	gmxport = 0, i;
1498 
1499 	lio_dev_dbg(oct, "%s Called\n", __func__);
1500 	if (recv_pkt->buffer_size[0] != (sizeof(*ls) + LIO_DROQ_INFO_SIZE)) {
1501 		lio_dev_err(oct, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
1502 			    recv_pkt->buffer_size[0],
1503 			    recv_pkt->rh.r_nic_info.gmxport);
1504 		goto nic_info_err;
1505 	}
1506 	gmxport = recv_pkt->rh.r_nic_info.gmxport;
1507 	ls = (union octeon_link_status *)(recv_pkt->buffer_ptr[0]->m_data +
1508 					  LIO_DROQ_INFO_SIZE);
1509 	lio_swap_8B_data((uint64_t *)ls,
1510 			 (sizeof(union octeon_link_status)) >> 3);
1511 
1512 	if (oct->props.gmxport == gmxport)
1513 		lio_update_link_status(oct->props.ifp, ls);
1514 
1515 nic_info_err:
1516 	for (i = 0; i < recv_pkt->buffer_count; i++)
1517 		lio_recv_buffer_free(recv_pkt->buffer_ptr[i]);
1518 
1519 	lio_free_recv_info(recv_info);
1520 	return (0);
1521 }
1522 
1523 void
1524 lio_free_mbuf(struct lio_instr_queue *iq, struct lio_mbuf_free_info *finfo)
1525 {
1526 
1527 	bus_dmamap_sync(iq->txtag, finfo->map, BUS_DMASYNC_POSTWRITE);
1528 	bus_dmamap_unload(iq->txtag, finfo->map);
1529 	m_freem(finfo->mb);
1530 }
1531 
1532 void
1533 lio_free_sgmbuf(struct lio_instr_queue *iq, struct lio_mbuf_free_info *finfo)
1534 {
1535 	struct lio_gather	*g;
1536 	struct octeon_device	*oct;
1537 	struct lio		*lio;
1538 	int	iq_no;
1539 
1540 	g = finfo->g;
1541 	iq_no = iq->txpciq.s.q_no;
1542 	oct = iq->oct_dev;
1543 	lio = if_getsoftc(oct->props.ifp);
1544 
1545 	mtx_lock(&lio->glist_lock[iq_no]);
1546 	STAILQ_INSERT_TAIL(&lio->ghead[iq_no], &g->node, entries);
1547 	mtx_unlock(&lio->glist_lock[iq_no]);
1548 
1549 	bus_dmamap_sync(iq->txtag, finfo->map, BUS_DMASYNC_POSTWRITE);
1550 	bus_dmamap_unload(iq->txtag, finfo->map);
1551 	m_freem(finfo->mb);
1552 }
1553 
1554 static void
1555 lio_if_cfg_callback(struct octeon_device *oct, uint32_t status, void *buf)
1556 {
1557 	struct lio_soft_command	*sc = (struct lio_soft_command *)buf;
1558 	struct lio_if_cfg_resp	*resp;
1559 	struct lio_if_cfg_context *ctx;
1560 
1561 	resp = (struct lio_if_cfg_resp *)sc->virtrptr;
1562 	ctx = (struct lio_if_cfg_context *)sc->ctxptr;
1563 
1564 	oct = lio_get_device(ctx->octeon_id);
1565 	if (resp->status)
1566 		lio_dev_err(oct, "nic if cfg instruction failed. Status: %llx (0x%08x)\n",
1567 			    LIO_CAST64(resp->status), status);
1568 	ctx->cond = 1;
1569 
1570 	snprintf(oct->fw_info.lio_firmware_version, 32, "%s",
1571 		 resp->cfg_info.lio_firmware_version);
1572 
1573 	/*
1574 	 * This barrier is required to be sure that the response has been
1575 	 * written fully before waking up the handler
1576 	 */
1577 	wmb();
1578 }
1579 
1580 static int
1581 lio_is_mac_changed(uint8_t *new, uint8_t *old)
1582 {
1583 
1584 	return ((new[0] != old[0]) || (new[1] != old[1]) ||
1585 		(new[2] != old[2]) || (new[3] != old[3]) ||
1586 		(new[4] != old[4]) || (new[5] != old[5]));
1587 }
1588 
1589 void
1590 lio_open(void *arg)
1591 {
1592 	struct lio	*lio = arg;
1593 	if_t		ifp = lio->ifp;
1594 	struct octeon_device	*oct = lio->oct_dev;
1595 	uint8_t	*mac_new, mac_old[ETHER_HDR_LEN];
1596 	int	ret = 0;
1597 
1598 	lio_ifstate_set(lio, LIO_IFSTATE_RUNNING);
1599 
1600 	/* Ready for link status updates */
1601 	lio->intf_open = 1;
1602 
1603 	lio_dev_info(oct, "Interface Open, ready for traffic\n");
1604 
1605 	/* tell Octeon to start forwarding packets to host */
1606 	lio_send_rx_ctrl_cmd(lio, 1);
1607 
1608 	mac_new = if_getlladdr(ifp);
1609 	memcpy(mac_old, ((uint8_t *)&lio->linfo.hw_addr) + 2, ETHER_HDR_LEN);
1610 
1611 	if (lio_is_mac_changed(mac_new, mac_old)) {
1612 		ret = lio_set_mac(ifp, mac_new);
1613 		if (ret)
1614 			lio_dev_err(oct, "MAC change failed, error: %d\n", ret);
1615 	}
1616 
1617 	/* Now inform the stack we're ready */
1618 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
1619 
1620 	lio_dev_info(oct, "Interface is opened\n");
1621 }
1622 
1623 static int
1624 lio_set_rxcsum_command(if_t ifp, int command, uint8_t rx_cmd)
1625 {
1626 	struct lio_ctrl_pkt	nctrl;
1627 	struct lio		*lio = if_getsoftc(ifp);
1628 	struct octeon_device	*oct = lio->oct_dev;
1629 	int	ret = 0;
1630 
1631 	nctrl.ncmd.cmd64 = 0;
1632 	nctrl.ncmd.s.cmd = command;
1633 	nctrl.ncmd.s.param1 = rx_cmd;
1634 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
1635 	nctrl.wait_time = 100;
1636 	nctrl.lio = lio;
1637 	nctrl.cb_fn = lio_ctrl_cmd_completion;
1638 
1639 	ret = lio_send_ctrl_pkt(lio->oct_dev, &nctrl);
1640 	if (ret < 0) {
1641 		lio_dev_err(oct, "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
1642 			    ret);
1643 	}
1644 
1645 	return (ret);
1646 }
1647 
1648 static int
1649 lio_stop_nic_module(struct octeon_device *oct)
1650 {
1651 	int		i, j;
1652 	struct lio	*lio;
1653 
1654 	lio_dev_dbg(oct, "Stopping network interfaces\n");
1655 	if (!oct->ifcount) {
1656 		lio_dev_err(oct, "Init for Octeon was not completed\n");
1657 		return (1);
1658 	}
1659 
1660 	mtx_lock(&oct->cmd_resp_wqlock);
1661 	oct->cmd_resp_state = LIO_DRV_OFFLINE;
1662 	mtx_unlock(&oct->cmd_resp_wqlock);
1663 
1664 	for (i = 0; i < oct->ifcount; i++) {
1665 		lio = if_getsoftc(oct->props.ifp);
1666 		for (j = 0; j < oct->num_oqs; j++)
1667 			lio_unregister_droq_ops(oct,
1668 						lio->linfo.rxpciq[j].s.q_no);
1669 	}
1670 
1671 	callout_drain(&lio->stats_timer);
1672 
1673 	for (i = 0; i < oct->ifcount; i++)
1674 		lio_destroy_nic_device(oct, i);
1675 
1676 	lio_dev_dbg(oct, "Network interface stopped\n");
1677 
1678 	return (0);
1679 }
1680 
1681 static void
1682 lio_delete_glists(struct octeon_device *oct, struct lio *lio)
1683 {
1684 	struct lio_gather	*g;
1685 	int	i;
1686 
1687 	if (lio->glist_lock != NULL) {
1688 		free((void *)lio->glist_lock, M_DEVBUF);
1689 		lio->glist_lock = NULL;
1690 	}
1691 
1692 	if (lio->ghead == NULL)
1693 		return;
1694 
1695 	for (i = 0; i < lio->linfo.num_txpciq; i++) {
1696 		do {
1697 			g = (struct lio_gather *)
1698 			    lio_delete_first_node(&lio->ghead[i]);
1699 			free(g, M_DEVBUF);
1700 		} while (g);
1701 
1702 		if ((lio->glists_virt_base != NULL) &&
1703 		    (lio->glists_virt_base[i] != NULL)) {
1704 			lio_dma_free(lio->glist_entry_size * lio->tx_qsize,
1705 				     lio->glists_virt_base[i]);
1706 		}
1707 	}
1708 
1709 	free(lio->glists_virt_base, M_DEVBUF);
1710 	lio->glists_virt_base = NULL;
1711 
1712 	free(lio->glists_dma_base, M_DEVBUF);
1713 	lio->glists_dma_base = NULL;
1714 
1715 	free(lio->ghead, M_DEVBUF);
1716 	lio->ghead = NULL;
1717 }
1718 
1719 static int
1720 lio_setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
1721 {
1722 	struct lio_gather	*g;
1723 	int	i, j;
1724 
1725 	lio->glist_lock = malloc(num_iqs * sizeof(*lio->glist_lock), M_DEVBUF,
1726 				 M_NOWAIT | M_ZERO);
1727 	if (lio->glist_lock == NULL)
1728 		return (1);
1729 
1730 	lio->ghead = malloc(num_iqs * sizeof(*lio->ghead), M_DEVBUF,
1731 			    M_NOWAIT | M_ZERO);
1732 	if (lio->ghead == NULL) {
1733 		free((void *)lio->glist_lock, M_DEVBUF);
1734 		lio->glist_lock = NULL;
1735 		return (1);
1736 	}
1737 
1738 	lio->glist_entry_size = ROUNDUP8((ROUNDUP4(LIO_MAX_SG) >> 2) *
1739 					 LIO_SG_ENTRY_SIZE);
1740 	/*
1741 	 * allocate memory to store virtual and dma base address of
1742 	 * per glist consistent memory
1743 	 */
1744 	lio->glists_virt_base = malloc(num_iqs * sizeof(void *), M_DEVBUF,
1745 				       M_NOWAIT | M_ZERO);
1746 	lio->glists_dma_base = malloc(num_iqs * sizeof(vm_paddr_t), M_DEVBUF,
1747 				      M_NOWAIT | M_ZERO);
1748 	if ((lio->glists_virt_base == NULL) || (lio->glists_dma_base == NULL)) {
1749 		lio_delete_glists(oct, lio);
1750 		return (1);
1751 	}
1752 
1753 	for (i = 0; i < num_iqs; i++) {
1754 		mtx_init(&lio->glist_lock[i], "glist_lock", NULL, MTX_DEF);
1755 
1756 		STAILQ_INIT(&lio->ghead[i]);
1757 
1758 		lio->glists_virt_base[i] =
1759 		    lio_dma_alloc(lio->glist_entry_size * lio->tx_qsize,
1760 				  (vm_paddr_t *)&lio->glists_dma_base[i]);
1761 		if (lio->glists_virt_base[i] == NULL) {
1762 			lio_delete_glists(oct, lio);
1763 			return (1);
1764 		}
1765 
1766 		for (j = 0; j < lio->tx_qsize; j++) {
1767 			g = malloc(sizeof(*g), M_DEVBUF, M_NOWAIT | M_ZERO);
1768 			if (g == NULL)
1769 				break;
1770 
1771 			g->sg = (struct lio_sg_entry *)(uintptr_t)
1772 			    ((uint64_t)(uintptr_t)lio->glists_virt_base[i] +
1773 			     (j * lio->glist_entry_size));
1774 			g->sg_dma_ptr = (uint64_t)lio->glists_dma_base[i] +
1775 				(j * lio->glist_entry_size);
1776 			STAILQ_INSERT_TAIL(&lio->ghead[i], &g->node, entries);
1777 		}
1778 
1779 		if (j != lio->tx_qsize) {
1780 			lio_delete_glists(oct, lio);
1781 			return (1);
1782 		}
1783 	}
1784 
1785 	return (0);
1786 }
1787 
1788 void
1789 lio_stop(if_t ifp)
1790 {
1791 	struct lio	*lio = if_getsoftc(ifp);
1792 	struct octeon_device	*oct = lio->oct_dev;
1793 
1794 	lio_ifstate_reset(lio, LIO_IFSTATE_RUNNING);
1795 	if_link_state_change(ifp, LINK_STATE_DOWN);
1796 
1797 	lio->intf_open = 0;
1798 	lio->linfo.link.s.link_up = 0;
1799 	lio->link_changes++;
1800 
1801 	lio_send_rx_ctrl_cmd(lio, 0);
1802 
1803 	/* Tell the stack that the interface is no longer active */
1804 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1805 
1806 	lio_dev_info(oct, "Interface is stopped\n");
1807 }
1808 
1809 static void
1810 lio_check_rx_oom_status(struct lio *lio)
1811 {
1812 	struct lio_droq	*droq;
1813 	struct octeon_device *oct = lio->oct_dev;
1814 	int	desc_refilled;
1815 	int	q, q_no = 0;
1816 
1817 	for (q = 0; q < oct->num_oqs; q++) {
1818 		q_no = lio->linfo.rxpciq[q].s.q_no;
1819 		droq = oct->droq[q_no];
1820 		if (droq == NULL)
1821 			continue;
1822 		if (lio_read_csr32(oct, droq->pkts_credit_reg) <= 0x40) {
1823 			mtx_lock(&droq->lock);
1824 			desc_refilled = lio_droq_refill(oct, droq);
1825 			/*
1826 			 * Flush the droq descriptor data to memory to be sure
1827 			 * that when we update the credits the data in memory
1828 			 * is accurate.
1829 			 */
1830 			wmb();
1831 			lio_write_csr32(oct, droq->pkts_credit_reg,
1832 					desc_refilled);
1833 			/* make sure mmio write completes */
1834 			__compiler_membar();
1835 			mtx_unlock(&droq->lock);
1836 		}
1837 	}
1838 }
1839 
1840 static void
1841 lio_poll_check_rx_oom_status(void *arg, int pending __unused)
1842 {
1843 	struct lio_tq	*rx_status_tq = arg;
1844 	struct lio	*lio = rx_status_tq->ctxptr;
1845 
1846 	if (lio_ifstate_check(lio, LIO_IFSTATE_RUNNING))
1847 		lio_check_rx_oom_status(lio);
1848 
1849 	taskqueue_enqueue_timeout(rx_status_tq->tq, &rx_status_tq->work,
1850 				  lio_ms_to_ticks(50));
1851 }
1852 
1853 static int
1854 lio_setup_rx_oom_poll_fn(if_t ifp)
1855 {
1856 	struct lio	*lio = if_getsoftc(ifp);
1857 	struct octeon_device	*oct = lio->oct_dev;
1858 	struct lio_tq	*rx_status_tq;
1859 
1860 	rx_status_tq = &lio->rx_status_tq;
1861 
1862 	rx_status_tq->tq = taskqueue_create("lio_rx_oom_status", M_WAITOK,
1863 					    taskqueue_thread_enqueue,
1864 					    &rx_status_tq->tq);
1865 	if (rx_status_tq->tq == NULL) {
1866 		lio_dev_err(oct, "unable to create lio rx oom status tq\n");
1867 		return (-1);
1868 	}
1869 
1870 	TIMEOUT_TASK_INIT(rx_status_tq->tq, &rx_status_tq->work, 0,
1871 			  lio_poll_check_rx_oom_status, (void *)rx_status_tq);
1872 
1873 	rx_status_tq->ctxptr = lio;
1874 
1875 	taskqueue_start_threads(&rx_status_tq->tq, 1, PI_NET,
1876 				"lio%d_rx_oom_status",
1877 				oct->octeon_id);
1878 
1879 	taskqueue_enqueue_timeout(rx_status_tq->tq, &rx_status_tq->work,
1880 				  lio_ms_to_ticks(50));
1881 
1882 	return (0);
1883 }
1884 
1885 static void
1886 lio_cleanup_rx_oom_poll_fn(if_t ifp)
1887 {
1888 	struct lio	*lio = if_getsoftc(ifp);
1889 
1890 	if (lio->rx_status_tq.tq != NULL) {
1891 		while (taskqueue_cancel_timeout(lio->rx_status_tq.tq,
1892 						&lio->rx_status_tq.work, NULL))
1893 			taskqueue_drain_timeout(lio->rx_status_tq.tq,
1894 						&lio->rx_status_tq.work);
1895 
1896 		taskqueue_free(lio->rx_status_tq.tq);
1897 
1898 		lio->rx_status_tq.tq = NULL;
1899 	}
1900 }
1901 
1902 static void
1903 lio_destroy_nic_device(struct octeon_device *oct, int ifidx)
1904 {
1905 	if_t		ifp = oct->props.ifp;
1906 	struct lio	*lio;
1907 
1908 	if (ifp == NULL) {
1909 		lio_dev_err(oct, "%s No ifp ptr for index %d\n",
1910 			    __func__, ifidx);
1911 		return;
1912 	}
1913 
1914 	lio = if_getsoftc(ifp);
1915 
1916 	lio_ifstate_set(lio, LIO_IFSTATE_DETACH);
1917 
1918 	lio_dev_dbg(oct, "NIC device cleanup\n");
1919 
1920 	if (atomic_load_acq_int(&lio->ifstate) & LIO_IFSTATE_RUNNING)
1921 		lio_stop(ifp);
1922 
1923 	if (lio_wait_for_pending_requests(oct))
1924 		lio_dev_err(oct, "There were pending requests\n");
1925 
1926 	if (lio_wait_for_instr_fetch(oct))
1927 		lio_dev_err(oct, "IQ had pending instructions\n");
1928 
1929 	if (lio_wait_for_oq_pkts(oct))
1930 		lio_dev_err(oct, "OQ had pending packets\n");
1931 
1932 	if (atomic_load_acq_int(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
1933 		ether_ifdetach(ifp);
1934 
1935 	lio_tcp_lro_free(oct, ifp);
1936 
1937 	lio_cleanup_rx_oom_poll_fn(ifp);
1938 
1939 	lio_delete_glists(oct, lio);
1940 
1941 	EVENTHANDLER_DEREGISTER(vlan_config, lio->vlan_attach);
1942 	EVENTHANDLER_DEREGISTER(vlan_unconfig, lio->vlan_detach);
1943 
1944 	free(lio, M_DEVBUF);
1945 
1946 	if_free(ifp);
1947 
1948 	oct->props.gmxport = -1;
1949 
1950 	oct->props.ifp = NULL;
1951 }
1952 
1953 static void
1954 print_link_info(if_t ifp)
1955 {
1956 	struct lio	*lio = if_getsoftc(ifp);
1957 
1958 	if (!lio_ifstate_check(lio, LIO_IFSTATE_RESETTING) &&
1959 	    lio_ifstate_check(lio, LIO_IFSTATE_REGISTERED)) {
1960 		struct octeon_link_info *linfo = &lio->linfo;
1961 
1962 		if (linfo->link.s.link_up) {
1963 			lio_dev_info(lio->oct_dev, "%d Mbps %s Duplex UP\n",
1964 				     linfo->link.s.speed,
1965 				     (linfo->link.s.duplex) ? "Full" : "Half");
1966 		} else {
1967 			lio_dev_info(lio->oct_dev, "Link Down\n");
1968 		}
1969 	}
1970 }
1971 
1972 static inline void
1973 lio_update_link_status(if_t ifp, union octeon_link_status *ls)
1974 {
1975 	struct lio	*lio = if_getsoftc(ifp);
1976 	int	changed = (lio->linfo.link.link_status64 != ls->link_status64);
1977 
1978 	lio->linfo.link.link_status64 = ls->link_status64;
1979 
1980 	if ((lio->intf_open) && (changed)) {
1981 		print_link_info(ifp);
1982 		lio->link_changes++;
1983 		if (lio->linfo.link.s.link_up)
1984 			if_link_state_change(ifp, LINK_STATE_UP);
1985 		else
1986 			if_link_state_change(ifp, LINK_STATE_DOWN);
1987 	}
1988 }
1989 
1990 /*
1991  * \brief Callback for rx ctrl
1992  * @param status status of request
1993  * @param buf pointer to resp structure
1994  */
1995 static void
1996 lio_rx_ctl_callback(struct octeon_device *oct, uint32_t status, void *buf)
1997 {
1998 	struct lio_soft_command	*sc = (struct lio_soft_command *)buf;
1999 	struct lio_rx_ctl_context *ctx;
2000 
2001 	ctx = (struct lio_rx_ctl_context *)sc->ctxptr;
2002 
2003 	oct = lio_get_device(ctx->octeon_id);
2004 	if (status)
2005 		lio_dev_err(oct, "rx ctl instruction failed. Status: %llx\n",
2006 			    LIO_CAST64(status));
2007 	ctx->cond = 1;
2008 
2009 	/*
2010 	 * This barrier is required to be sure that the response has been
2011 	 * written fully before waking up the handler
2012 	 */
2013 	wmb();
2014 }
2015 
2016 static void
2017 lio_send_rx_ctrl_cmd(struct lio *lio, int start_stop)
2018 {
2019 	struct lio_soft_command	*sc;
2020 	struct lio_rx_ctl_context *ctx;
2021 	union octeon_cmd	*ncmd;
2022 	struct octeon_device	*oct = (struct octeon_device *)lio->oct_dev;
2023 	int	ctx_size = sizeof(struct lio_rx_ctl_context);
2024 	int	retval;
2025 
2026 	if (oct->props.rx_on == start_stop)
2027 		return;
2028 
2029 	sc = lio_alloc_soft_command(oct, OCTEON_CMD_SIZE, 16, ctx_size);
2030 	if (sc == NULL)
2031 		return;
2032 
2033 	ncmd = (union octeon_cmd *)sc->virtdptr;
2034 	ctx = (struct lio_rx_ctl_context *)sc->ctxptr;
2035 
2036 	ctx->cond = 0;
2037 	ctx->octeon_id = lio_get_device_id(oct);
2038 	ncmd->cmd64 = 0;
2039 	ncmd->s.cmd = LIO_CMD_RX_CTL;
2040 	ncmd->s.param1 = start_stop;
2041 
2042 	lio_swap_8B_data((uint64_t *)ncmd, (OCTEON_CMD_SIZE >> 3));
2043 
2044 	sc->iq_no = lio->linfo.txpciq[0].s.q_no;
2045 
2046 	lio_prepare_soft_command(oct, sc, LIO_OPCODE_NIC, LIO_OPCODE_NIC_CMD, 0,
2047 				 0, 0);
2048 
2049 	sc->callback = lio_rx_ctl_callback;
2050 	sc->callback_arg = sc;
2051 	sc->wait_time = 5000;
2052 
2053 	retval = lio_send_soft_command(oct, sc);
2054 	if (retval == LIO_IQ_SEND_FAILED) {
2055 		lio_dev_err(oct, "Failed to send RX Control message\n");
2056 	} else {
2057 		/*
2058 		 * Sleep on a wait queue till the cond flag indicates that the
2059 		 * response arrived or timed-out.
2060 		 */
2061 		lio_sleep_cond(oct, &ctx->cond);
2062 		oct->props.rx_on = start_stop;
2063 	}
2064 
2065 	lio_free_soft_command(oct, sc);
2066 }
2067 
2068 static void
2069 lio_vlan_rx_add_vid(void *arg, if_t ifp, uint16_t vid)
2070 {
2071 	struct lio_ctrl_pkt	nctrl;
2072 	struct lio		*lio = if_getsoftc(ifp);
2073 	struct octeon_device	*oct = lio->oct_dev;
2074 	int	ret = 0;
2075 
2076 	if (if_getsoftc(ifp) != arg)	/* Not our event */
2077 		return;
2078 
2079 	if ((vid == 0) || (vid > 4095))	/* Invalid */
2080 		return;
2081 
2082 	bzero(&nctrl, sizeof(struct lio_ctrl_pkt));
2083 
2084 	nctrl.ncmd.cmd64 = 0;
2085 	nctrl.ncmd.s.cmd = LIO_CMD_ADD_VLAN_FILTER;
2086 	nctrl.ncmd.s.param1 = vid;
2087 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2088 	nctrl.wait_time = 100;
2089 	nctrl.lio = lio;
2090 	nctrl.cb_fn = lio_ctrl_cmd_completion;
2091 
2092 	ret = lio_send_ctrl_pkt(lio->oct_dev, &nctrl);
2093 	if (ret < 0) {
2094 		lio_dev_err(oct, "Add VLAN filter failed in core (ret: 0x%x)\n",
2095 			    ret);
2096 	}
2097 }
2098 
2099 static void
2100 lio_vlan_rx_kill_vid(void *arg, if_t ifp, uint16_t vid)
2101 {
2102 	struct lio_ctrl_pkt	nctrl;
2103 	struct lio		*lio = if_getsoftc(ifp);
2104 	struct octeon_device	*oct = lio->oct_dev;
2105 	int	ret = 0;
2106 
2107 	if (if_getsoftc(ifp) != arg)	/* Not our event */
2108 		return;
2109 
2110 	if ((vid == 0) || (vid > 4095))	/* Invalid */
2111 		return;
2112 
2113 	bzero(&nctrl, sizeof(struct lio_ctrl_pkt));
2114 
2115 	nctrl.ncmd.cmd64 = 0;
2116 	nctrl.ncmd.s.cmd = LIO_CMD_DEL_VLAN_FILTER;
2117 	nctrl.ncmd.s.param1 = vid;
2118 	nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
2119 	nctrl.wait_time = 100;
2120 	nctrl.lio = lio;
2121 	nctrl.cb_fn = lio_ctrl_cmd_completion;
2122 
2123 	ret = lio_send_ctrl_pkt(lio->oct_dev, &nctrl);
2124 	if (ret < 0) {
2125 		lio_dev_err(oct,
2126 			    "Kill VLAN filter failed in core (ret: 0x%x)\n",
2127 			    ret);
2128 	}
2129 }
2130 
2131 static int
2132 lio_wait_for_oq_pkts(struct octeon_device *oct)
2133 {
2134 	int	i, pending_pkts, pkt_cnt = 0, retry = 100;
2135 
2136 	do {
2137 		pending_pkts = 0;
2138 
2139 		for (i = 0; i < LIO_MAX_OUTPUT_QUEUES(oct); i++) {
2140 			if (!(oct->io_qmask.oq & BIT_ULL(i)))
2141 				continue;
2142 
2143 			pkt_cnt = lio_droq_check_hw_for_pkts(oct->droq[i]);
2144 			if (pkt_cnt > 0) {
2145 				pending_pkts += pkt_cnt;
2146 				taskqueue_enqueue(oct->droq[i]->droq_taskqueue,
2147 						  &oct->droq[i]->droq_task);
2148 			}
2149 		}
2150 
2151 		pkt_cnt = 0;
2152 		lio_sleep_timeout(1);
2153 	} while (retry-- && pending_pkts);
2154 
2155 	return (pkt_cnt);
2156 }
2157 
2158 static void
2159 lio_destroy_resources(struct octeon_device *oct)
2160 {
2161 	int i, refcount;
2162 
2163 	switch (atomic_load_acq_int(&oct->status)) {
2164 	case LIO_DEV_RUNNING:
2165 	case LIO_DEV_CORE_OK:
2166 		/* No more instructions will be forwarded. */
2167 		atomic_store_rel_int(&oct->status, LIO_DEV_IN_RESET);
2168 
2169 		oct->app_mode = LIO_DRV_INVALID_APP;
2170 		lio_dev_dbg(oct, "Device state is now %s\n",
2171 			    lio_get_state_string(&oct->status));
2172 
2173 		lio_sleep_timeout(100);
2174 
2175 		/* fallthrough */
2176 	case LIO_DEV_HOST_OK:
2177 
2178 		/* fallthrough */
2179 	case LIO_DEV_CONSOLE_INIT_DONE:
2180 		/* Remove any consoles */
2181 		lio_remove_consoles(oct);
2182 
2183 		/* fallthrough */
2184 	case LIO_DEV_IO_QUEUES_DONE:
2185 		if (lio_wait_for_pending_requests(oct))
2186 			lio_dev_err(oct, "There were pending requests\n");
2187 
2188 		if (lio_wait_for_instr_fetch(oct))
2189 			lio_dev_err(oct, "IQ had pending instructions\n");
2190 
2191 		/*
2192 		 * Disable the input and output queues now. No more packets will
2193 		 * arrive from Octeon, but we should wait for all packet
2194 		 * processing to finish.
2195 		 */
2196 		oct->fn_list.disable_io_queues(oct);
2197 
2198 		if (lio_wait_for_oq_pkts(oct))
2199 			lio_dev_err(oct, "OQ had pending packets\n");
2200 
2201 		/* fallthrough */
2202 	case LIO_DEV_INTR_SET_DONE:
2203 		/* Disable interrupts  */
2204 		oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
2205 
2206 		if (oct->msix_on) {
2207 			for (i = 0; i < oct->num_msix_irqs - 1; i++) {
2208 				if (oct->ioq_vector[i].tag != NULL) {
2209 					bus_teardown_intr(oct->device,
2210 						  oct->ioq_vector[i].msix_res,
2211 						      oct->ioq_vector[i].tag);
2212 					oct->ioq_vector[i].tag = NULL;
2213 				}
2214 				if (oct->ioq_vector[i].msix_res != NULL) {
2215 					bus_release_resource(oct->device,
2216 						SYS_RES_IRQ,
2217 						oct->ioq_vector[i].vector,
2218 						oct->ioq_vector[i].msix_res);
2219 					oct->ioq_vector[i].msix_res = NULL;
2220 				}
2221 			}
2222 			/* non-iov vector's argument is oct struct */
2223 			if (oct->tag != NULL) {
2224 				bus_teardown_intr(oct->device, oct->msix_res,
2225 						  oct->tag);
2226 				oct->tag = NULL;
2227 			}
2228 
2229 			if (oct->msix_res != NULL) {
2230 				bus_release_resource(oct->device, SYS_RES_IRQ,
2231 						     oct->aux_vector,
2232 						     oct->msix_res);
2233 				oct->msix_res = NULL;
2234 			}
2235 
2236 			pci_release_msi(oct->device);
2237 		}
2238 		/* fallthrough */
2239 	case LIO_DEV_IN_RESET:
2240 	case LIO_DEV_DROQ_INIT_DONE:
2241 		/* Wait for any pending operations */
2242 		lio_mdelay(100);
2243 		for (i = 0; i < LIO_MAX_OUTPUT_QUEUES(oct); i++) {
2244 			if (!(oct->io_qmask.oq & BIT_ULL(i)))
2245 				continue;
2246 			lio_delete_droq(oct, i);
2247 		}
2248 
2249 		/* fallthrough */
2250 	case LIO_DEV_RESP_LIST_INIT_DONE:
2251 		for (i = 0; i < LIO_MAX_POSSIBLE_OUTPUT_QUEUES; i++) {
2252 			if (oct->droq[i] != NULL) {
2253 				free(oct->droq[i], M_DEVBUF);
2254 				oct->droq[i] = NULL;
2255 			}
2256 		}
2257 		lio_delete_response_list(oct);
2258 
2259 		/* fallthrough */
2260 	case LIO_DEV_INSTR_QUEUE_INIT_DONE:
2261 		for (i = 0; i < LIO_MAX_INSTR_QUEUES(oct); i++) {
2262 			if (!(oct->io_qmask.iq & BIT_ULL(i)))
2263 				continue;
2264 
2265 			lio_delete_instr_queue(oct, i);
2266 		}
2267 
2268 		/* fallthrough */
2269 	case LIO_DEV_MSIX_ALLOC_VECTOR_DONE:
2270 		for (i = 0; i < LIO_MAX_POSSIBLE_INSTR_QUEUES; i++) {
2271 			if (oct->instr_queue[i] != NULL) {
2272 				free(oct->instr_queue[i], M_DEVBUF);
2273 				oct->instr_queue[i] = NULL;
2274 			}
2275 		}
2276 		lio_free_ioq_vector(oct);
2277 
2278 		/* fallthrough */
2279 	case LIO_DEV_SC_BUFF_POOL_INIT_DONE:
2280 		lio_free_sc_buffer_pool(oct);
2281 
2282 		/* fallthrough */
2283 	case LIO_DEV_DISPATCH_INIT_DONE:
2284 		lio_delete_dispatch_list(oct);
2285 
2286 		/* fallthrough */
2287 	case LIO_DEV_PCI_MAP_DONE:
2288 		refcount = lio_deregister_device(oct);
2289 
2290 		if (fw_type_is_none())
2291 			lio_pci_flr(oct);
2292 
2293 		if (!refcount)
2294 			oct->fn_list.soft_reset(oct);
2295 
2296 		lio_unmap_pci_barx(oct, 0);
2297 		lio_unmap_pci_barx(oct, 1);
2298 
2299 		/* fallthrough */
2300 	case LIO_DEV_PCI_ENABLE_DONE:
2301 		/* Disable the device, releasing the PCI INT */
2302 		pci_disable_busmaster(oct->device);
2303 
2304 		/* fallthrough */
2305 	case LIO_DEV_BEGIN_STATE:
2306 		break;
2307 	}	/* end switch (oct->status) */
2308 }
2309