xref: /freebsd/sys/dev/qlxgbe/ql_os.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013-2016 Qlogic Corporation
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  *  1. Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  *  2. Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  *  and ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  *  POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * File: ql_os.c
32  * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
33  */
34 
35 #include <sys/cdefs.h>
36 #include "ql_os.h"
37 #include "ql_hw.h"
38 #include "ql_def.h"
39 #include "ql_inline.h"
40 #include "ql_ver.h"
41 #include "ql_glbl.h"
42 #include "ql_dbg.h"
43 #include <sys/smp.h>
44 
45 /*
46  * Some PCI Configuration Space Related Defines
47  */
48 
49 #ifndef PCI_VENDOR_QLOGIC
50 #define PCI_VENDOR_QLOGIC	0x1077
51 #endif
52 
53 #ifndef PCI_PRODUCT_QLOGIC_ISP8030
54 #define PCI_PRODUCT_QLOGIC_ISP8030	0x8030
55 #endif
56 
57 #define PCI_QLOGIC_ISP8030 \
58 	((PCI_PRODUCT_QLOGIC_ISP8030 << 16) | PCI_VENDOR_QLOGIC)
59 
60 /*
61  * static functions
62  */
63 static int qla_alloc_parent_dma_tag(qla_host_t *ha);
64 static void qla_free_parent_dma_tag(qla_host_t *ha);
65 static int qla_alloc_xmt_bufs(qla_host_t *ha);
66 static void qla_free_xmt_bufs(qla_host_t *ha);
67 static int qla_alloc_rcv_bufs(qla_host_t *ha);
68 static void qla_free_rcv_bufs(qla_host_t *ha);
69 static void qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb);
70 
71 static void qla_init_ifnet(device_t dev, qla_host_t *ha);
72 static int qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS);
73 static void qla_release(qla_host_t *ha);
74 static void qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs,
75 		int error);
76 static void qla_stop(qla_host_t *ha);
77 static void qla_get_peer(qla_host_t *ha);
78 static void qla_error_recovery(void *context, int pending);
79 static void qla_async_event(void *context, int pending);
80 static void qla_stats(void *context, int pending);
81 static int qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
82 		uint32_t iscsi_pdu);
83 
84 /*
85  * Hooks to the Operating Systems
86  */
87 static int qla_pci_probe (device_t);
88 static int qla_pci_attach (device_t);
89 static int qla_pci_detach (device_t);
90 
91 static void qla_init(void *arg);
92 static int qla_ioctl(if_t ifp, u_long cmd, caddr_t data);
93 static int qla_media_change(if_t ifp);
94 static void qla_media_status(if_t ifp, struct ifmediareq *ifmr);
95 
96 static int qla_transmit(if_t ifp, struct mbuf  *mp);
97 static void qla_qflush(if_t ifp);
98 static int qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *tx_fp);
99 static void qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *tx_fp);
100 static int qla_create_fp_taskqueues(qla_host_t *ha);
101 static void qla_destroy_fp_taskqueues(qla_host_t *ha);
102 static void qla_drain_fp_taskqueues(qla_host_t *ha);
103 
104 static device_method_t qla_pci_methods[] = {
105 	/* Device interface */
106 	DEVMETHOD(device_probe, qla_pci_probe),
107 	DEVMETHOD(device_attach, qla_pci_attach),
108 	DEVMETHOD(device_detach, qla_pci_detach),
109 	{ 0, 0 }
110 };
111 
112 static driver_t qla_pci_driver = {
113 	"ql", qla_pci_methods, sizeof (qla_host_t),
114 };
115 
116 DRIVER_MODULE(qla83xx, pci, qla_pci_driver, 0, 0);
117 
118 MODULE_DEPEND(qla83xx, pci, 1, 1, 1);
119 MODULE_DEPEND(qla83xx, ether, 1, 1, 1);
120 
121 MALLOC_DEFINE(M_QLA83XXBUF, "qla83xxbuf", "Buffers for qla83xx driver");
122 
123 #define QL_STD_REPLENISH_THRES		0
124 #define QL_JUMBO_REPLENISH_THRES	32
125 
126 static char dev_str[64];
127 static char ver_str[64];
128 
129 /*
130  * Name:	qla_pci_probe
131  * Function:	Validate the PCI device to be a QLA80XX device
132  */
133 static int
134 qla_pci_probe(device_t dev)
135 {
136         switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
137         case PCI_QLOGIC_ISP8030:
138 		snprintf(dev_str, sizeof(dev_str), "%s v%d.%d.%d",
139 			"Qlogic ISP 83xx PCI CNA Adapter-Ethernet Function",
140 			QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
141 			QLA_VERSION_BUILD);
142 		snprintf(ver_str, sizeof(ver_str), "v%d.%d.%d",
143 			QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
144 			QLA_VERSION_BUILD);
145                 device_set_desc(dev, dev_str);
146                 break;
147         default:
148                 return (ENXIO);
149         }
150 
151         if (bootverbose)
152                 printf("%s: %s\n ", __func__, dev_str);
153 
154         return (BUS_PROBE_DEFAULT);
155 }
156 
157 static void
158 qla_add_sysctls(qla_host_t *ha)
159 {
160         device_t dev = ha->pci_dev;
161 
162 	SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
163 		SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
164 		OID_AUTO, "version", CTLFLAG_RD,
165 		ver_str, 0, "Driver Version");
166 
167         SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
168                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
169                 OID_AUTO, "fw_version", CTLFLAG_RD,
170                 ha->fw_ver_str, 0, "firmware version");
171 
172         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
173             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
174 	    "link_status", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
175 	    (void *)ha, 0, qla_sysctl_get_link_status, "I", "Link Status");
176 
177 	ha->dbg_level = 0;
178         SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
179                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
180                 OID_AUTO, "debug", CTLFLAG_RW,
181                 &ha->dbg_level, ha->dbg_level, "Debug Level");
182 
183 	ha->enable_minidump = 1;
184 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
185 		SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
186 		OID_AUTO, "enable_minidump", CTLFLAG_RW,
187 		&ha->enable_minidump, ha->enable_minidump,
188 		"Minidump retrival prior to error recovery "
189 		"is enabled only when this is set");
190 
191 	ha->enable_driverstate_dump = 1;
192 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
193 		SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
194 		OID_AUTO, "enable_driverstate_dump", CTLFLAG_RW,
195 		&ha->enable_driverstate_dump, ha->enable_driverstate_dump,
196 		"Driver State retrival prior to error recovery "
197 		"is enabled only when this is set");
198 
199 	ha->enable_error_recovery = 1;
200 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
201 		SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
202 		OID_AUTO, "enable_error_recovery", CTLFLAG_RW,
203 		&ha->enable_error_recovery, ha->enable_error_recovery,
204 		"when set error recovery is enabled on fatal errors "
205 		"otherwise the port is turned offline");
206 
207 	ha->ms_delay_after_init = 1000;
208 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
209 		SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
210 		OID_AUTO, "ms_delay_after_init", CTLFLAG_RW,
211 		&ha->ms_delay_after_init, ha->ms_delay_after_init,
212 		"millisecond delay after hw_init");
213 
214 	ha->std_replenish = QL_STD_REPLENISH_THRES;
215         SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
216                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
217                 OID_AUTO, "std_replenish", CTLFLAG_RW,
218                 &ha->std_replenish, ha->std_replenish,
219                 "Threshold for Replenishing Standard Frames");
220 
221         SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
222                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
223                 OID_AUTO, "ipv4_lro",
224                 CTLFLAG_RD, &ha->ipv4_lro,
225                 "number of ipv4 lro completions");
226 
227         SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
228                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
229                 OID_AUTO, "ipv6_lro",
230                 CTLFLAG_RD, &ha->ipv6_lro,
231                 "number of ipv6 lro completions");
232 
233 	SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
234 		SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
235 		OID_AUTO, "tx_tso_frames",
236 		CTLFLAG_RD, &ha->tx_tso_frames,
237 		"number of Tx TSO Frames");
238 
239 	SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
240                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
241 		OID_AUTO, "hw_vlan_tx_frames",
242 		CTLFLAG_RD, &ha->hw_vlan_tx_frames,
243 		"number of Tx VLAN Frames");
244 
245 	SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
246                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
247 		OID_AUTO, "hw_lock_failed",
248 		CTLFLAG_RD, &ha->hw_lock_failed,
249 		"number of hw_lock failures");
250 
251         return;
252 }
253 
254 static void
255 qla_watchdog(void *arg)
256 {
257 	qla_host_t *ha = arg;
258 	if_t ifp;
259 
260 	ifp = ha->ifp;
261 
262         if (ha->qla_watchdog_exit) {
263 		ha->qla_watchdog_exited = 1;
264 		return;
265 	}
266 	ha->qla_watchdog_exited = 0;
267 
268 	if (!ha->qla_watchdog_pause) {
269                 if (!ha->offline &&
270                         (ql_hw_check_health(ha) || ha->qla_initiate_recovery ||
271                         (ha->msg_from_peer == QL_PEER_MSG_RESET))) {
272 	        	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
273 			ql_update_link_state(ha);
274 
275 			if (ha->enable_error_recovery) {
276 				ha->qla_watchdog_paused = 1;
277 				ha->qla_watchdog_pause = 1;
278 				ha->err_inject = 0;
279 				device_printf(ha->pci_dev,
280 					"%s: taskqueue_enqueue(err_task) \n",
281 					__func__);
282 				taskqueue_enqueue(ha->err_tq, &ha->err_task);
283 			} else {
284 				if (ifp != NULL)
285 					if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
286 				ha->offline = 1;
287 			}
288 			return;
289 
290 		} else {
291 			if (ha->qla_interface_up) {
292 				ha->watchdog_ticks++;
293 
294 				if (ha->watchdog_ticks > 1000)
295 					ha->watchdog_ticks = 0;
296 
297 				if (!ha->watchdog_ticks && QL_RUNNING(ifp)) {
298 					taskqueue_enqueue(ha->stats_tq,
299 						&ha->stats_task);
300 				}
301 
302 				if (ha->async_event) {
303 					taskqueue_enqueue(ha->async_event_tq,
304 						&ha->async_event_task);
305 				}
306 			}
307 			ha->qla_watchdog_paused = 0;
308 		}
309 	} else {
310 		ha->qla_watchdog_paused = 1;
311 	}
312 
313 	callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
314 		qla_watchdog, ha);
315 }
316 
317 /*
318  * Name:	qla_pci_attach
319  * Function:	attaches the device to the operating system
320  */
321 static int
322 qla_pci_attach(device_t dev)
323 {
324 	qla_host_t *ha = NULL;
325 	uint32_t rsrc_len __unused;
326 	int i;
327 	uint32_t num_rcvq = 0;
328 
329         if ((ha = device_get_softc(dev)) == NULL) {
330                 device_printf(dev, "cannot get softc\n");
331                 return (ENOMEM);
332         }
333 
334         memset(ha, 0, sizeof (qla_host_t));
335 
336         if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8030) {
337                 device_printf(dev, "device is not ISP8030\n");
338                 return (ENXIO);
339 	}
340 
341         ha->pci_func = pci_get_function(dev) & 0x1;
342 
343         ha->pci_dev = dev;
344 
345 	pci_enable_busmaster(dev);
346 
347 	ha->reg_rid = PCIR_BAR(0);
348 	ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
349 				RF_ACTIVE);
350 
351         if (ha->pci_reg == NULL) {
352                 device_printf(dev, "unable to map any ports\n");
353                 goto qla_pci_attach_err;
354         }
355 
356 	rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
357 					ha->reg_rid);
358 
359 	mtx_init(&ha->hw_lock, "qla83xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
360 	mtx_init(&ha->sp_log_lock, "qla83xx_sp_log_lock", MTX_NETWORK_LOCK, MTX_DEF);
361 	ha->flags.lock_init = 1;
362 
363 	qla_add_sysctls(ha);
364 
365 	ha->hw.num_sds_rings = MAX_SDS_RINGS;
366 	ha->hw.num_rds_rings = MAX_RDS_RINGS;
367 	ha->hw.num_tx_rings = NUM_TX_RINGS;
368 
369 	ha->reg_rid1 = PCIR_BAR(2);
370 	ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
371 			&ha->reg_rid1, RF_ACTIVE);
372 
373 	ha->msix_count = pci_msix_count(dev);
374 
375 	if (ha->msix_count < 1 ) {
376 		device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
377 			ha->msix_count);
378 		goto qla_pci_attach_err;
379 	}
380 
381 	if (ha->msix_count < (ha->hw.num_sds_rings + 1)) {
382 		ha->hw.num_sds_rings = ha->msix_count - 1;
383 	}
384 
385 	QL_DPRINT2(ha, (dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
386 		" msix_count 0x%x pci_reg %p pci_reg1 %p\n", __func__, ha,
387 		ha->pci_func, rsrc_len, ha->msix_count, ha->pci_reg,
388 		ha->pci_reg1));
389 
390         /* initialize hardware */
391         if (ql_init_hw(ha)) {
392                 device_printf(dev, "%s: ql_init_hw failed\n", __func__);
393                 goto qla_pci_attach_err;
394         }
395 
396         device_printf(dev, "%s: firmware[%d.%d.%d.%d]\n", __func__,
397                 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
398                 ha->fw_ver_build);
399         snprintf(ha->fw_ver_str, sizeof(ha->fw_ver_str), "%d.%d.%d.%d",
400                         ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
401                         ha->fw_ver_build);
402 
403         if (qla_get_nic_partition(ha, NULL, &num_rcvq)) {
404                 device_printf(dev, "%s: qla_get_nic_partition failed\n",
405                         __func__);
406                 goto qla_pci_attach_err;
407         }
408         QL_DPRINT2(ha, (dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
409                 " msix_count 0x%x pci_reg %p pci_reg1 %p num_rcvq = %d\n",
410 		__func__, ha, ha->pci_func, rsrc_len, ha->msix_count,
411 		ha->pci_reg, ha->pci_reg1, num_rcvq));
412 
413         if ((ha->msix_count  < 64) || (num_rcvq != 32)) {
414 		if (ha->hw.num_sds_rings > 15) {
415                 	ha->hw.num_sds_rings = 15;
416 		}
417         }
418 
419 	ha->hw.num_rds_rings = ha->hw.num_sds_rings;
420 	ha->hw.num_tx_rings = ha->hw.num_sds_rings;
421 
422 #ifdef QL_ENABLE_ISCSI_TLV
423 	ha->hw.num_tx_rings = ha->hw.num_sds_rings * 2;
424 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
425 
426 	ql_hw_add_sysctls(ha);
427 
428 	ha->msix_count = ha->hw.num_sds_rings + 1;
429 
430 	if (pci_alloc_msix(dev, &ha->msix_count)) {
431 		device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
432 			ha->msix_count);
433 		ha->msix_count = 0;
434 		goto qla_pci_attach_err;
435 	}
436 
437 	ha->mbx_irq_rid = 1;
438 	ha->mbx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
439 				&ha->mbx_irq_rid,
440 				(RF_ACTIVE | RF_SHAREABLE));
441 	if (ha->mbx_irq == NULL) {
442 		device_printf(dev, "could not allocate mbx interrupt\n");
443 		goto qla_pci_attach_err;
444 	}
445 	if (bus_setup_intr(dev, ha->mbx_irq, (INTR_TYPE_NET | INTR_MPSAFE),
446 		NULL, ql_mbx_isr, ha, &ha->mbx_handle)) {
447 		device_printf(dev, "could not setup mbx interrupt\n");
448 		goto qla_pci_attach_err;
449 	}
450 
451 	for (i = 0; i < ha->hw.num_sds_rings; i++) {
452 		ha->irq_vec[i].sds_idx = i;
453                 ha->irq_vec[i].ha = ha;
454                 ha->irq_vec[i].irq_rid = 2 + i;
455 
456 		ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
457 				&ha->irq_vec[i].irq_rid,
458 				(RF_ACTIVE | RF_SHAREABLE));
459 
460 		if (ha->irq_vec[i].irq == NULL) {
461 			device_printf(dev, "could not allocate interrupt\n");
462 			goto qla_pci_attach_err;
463 		}
464 		if (bus_setup_intr(dev, ha->irq_vec[i].irq,
465 			(INTR_TYPE_NET | INTR_MPSAFE),
466 			NULL, ql_isr, &ha->irq_vec[i],
467 			&ha->irq_vec[i].handle)) {
468 			device_printf(dev, "could not setup interrupt\n");
469 			goto qla_pci_attach_err;
470 		}
471 
472 		ha->tx_fp[i].ha = ha;
473 		ha->tx_fp[i].txr_idx = i;
474 
475 		if (qla_alloc_tx_br(ha, &ha->tx_fp[i])) {
476 			device_printf(dev, "%s: could not allocate tx_br[%d]\n",
477 				__func__, i);
478 			goto qla_pci_attach_err;
479 		}
480 	}
481 
482 	if (qla_create_fp_taskqueues(ha) != 0)
483 		goto qla_pci_attach_err;
484 
485 	printf("%s: mp__ncpus %d sds %d rds %d msi-x %d\n", __func__, mp_ncpus,
486 		ha->hw.num_sds_rings, ha->hw.num_rds_rings, ha->msix_count);
487 
488 	ql_read_mac_addr(ha);
489 
490 	/* allocate parent dma tag */
491 	if (qla_alloc_parent_dma_tag(ha)) {
492 		device_printf(dev, "%s: qla_alloc_parent_dma_tag failed\n",
493 			__func__);
494 		goto qla_pci_attach_err;
495 	}
496 
497 	/* alloc all dma buffers */
498 	if (ql_alloc_dma(ha)) {
499 		device_printf(dev, "%s: ql_alloc_dma failed\n", __func__);
500 		goto qla_pci_attach_err;
501 	}
502 	qla_get_peer(ha);
503 
504 	if (ql_minidump_init(ha) != 0) {
505 		device_printf(dev, "%s: ql_minidump_init failed\n", __func__);
506 		goto qla_pci_attach_err;
507 	}
508 	ql_alloc_drvr_state_buffer(ha);
509 	ql_alloc_sp_log_buffer(ha);
510 	/* create the o.s ethernet interface */
511 	qla_init_ifnet(dev, ha);
512 
513 	ha->flags.qla_watchdog_active = 1;
514 	ha->qla_watchdog_pause = 0;
515 
516 	callout_init(&ha->tx_callout, TRUE);
517 	ha->flags.qla_callout_init = 1;
518 
519 	/* create ioctl device interface */
520 	if (ql_make_cdev(ha)) {
521 		device_printf(dev, "%s: ql_make_cdev failed\n", __func__);
522 		goto qla_pci_attach_err;
523 	}
524 
525 	callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
526 		qla_watchdog, ha);
527 
528 	TASK_INIT(&ha->err_task, 0, qla_error_recovery, ha);
529 	ha->err_tq = taskqueue_create("qla_errq", M_NOWAIT,
530 			taskqueue_thread_enqueue, &ha->err_tq);
531 	taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
532 		device_get_nameunit(ha->pci_dev));
533 
534         TASK_INIT(&ha->async_event_task, 0, qla_async_event, ha);
535         ha->async_event_tq = taskqueue_create("qla_asyncq", M_NOWAIT,
536                         taskqueue_thread_enqueue, &ha->async_event_tq);
537         taskqueue_start_threads(&ha->async_event_tq, 1, PI_NET, "%s asyncq",
538                 device_get_nameunit(ha->pci_dev));
539 
540         TASK_INIT(&ha->stats_task, 0, qla_stats, ha);
541         ha->stats_tq = taskqueue_create("qla_statsq", M_NOWAIT,
542                         taskqueue_thread_enqueue, &ha->stats_tq);
543         taskqueue_start_threads(&ha->stats_tq, 1, PI_NET, "%s taskq",
544                 device_get_nameunit(ha->pci_dev));
545 
546 	QL_DPRINT2(ha, (dev, "%s: exit 0\n", __func__));
547         return (0);
548 
549 qla_pci_attach_err:
550 
551 	qla_release(ha);
552 
553 	if (ha->flags.lock_init) {
554 		mtx_destroy(&ha->hw_lock);
555 		mtx_destroy(&ha->sp_log_lock);
556 	}
557 
558 	QL_DPRINT2(ha, (dev, "%s: exit ENXIO\n", __func__));
559         return (ENXIO);
560 }
561 
562 /*
563  * Name:	qla_pci_detach
564  * Function:	Unhooks the device from the operating system
565  */
566 static int
567 qla_pci_detach(device_t dev)
568 {
569 	qla_host_t *ha = NULL;
570 	if_t ifp;
571 
572         if ((ha = device_get_softc(dev)) == NULL) {
573                 device_printf(dev, "cannot get softc\n");
574                 return (ENOMEM);
575         }
576 
577 	QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
578 
579 	ifp = ha->ifp;
580 
581 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
582 	QLA_LOCK(ha, __func__, -1, 0);
583 
584 	ha->qla_detach_active = 1;
585 	qla_stop(ha);
586 
587 	qla_release(ha);
588 
589 	QLA_UNLOCK(ha, __func__);
590 
591 	if (ha->flags.lock_init) {
592 		mtx_destroy(&ha->hw_lock);
593 		mtx_destroy(&ha->sp_log_lock);
594 	}
595 
596 	QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
597 
598         return (0);
599 }
600 
601 /*
602  * SYSCTL Related Callbacks
603  */
604 static int
605 qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS)
606 {
607 	int err, ret = 0;
608 	qla_host_t *ha;
609 
610 	err = sysctl_handle_int(oidp, &ret, 0, req);
611 
612 	if (err || !req->newptr)
613 		return (err);
614 
615 	if (ret == 1) {
616 		ha = (qla_host_t *)arg1;
617 		ql_hw_link_status(ha);
618 	}
619 	return (err);
620 }
621 
622 /*
623  * Name:	qla_release
624  * Function:	Releases the resources allocated for the device
625  */
626 static void
627 qla_release(qla_host_t *ha)
628 {
629 	device_t dev;
630 	int i;
631 
632 	dev = ha->pci_dev;
633 
634         if (ha->async_event_tq) {
635                 taskqueue_drain_all(ha->async_event_tq);
636                 taskqueue_free(ha->async_event_tq);
637         }
638 
639 	if (ha->err_tq) {
640 		taskqueue_drain_all(ha->err_tq);
641 		taskqueue_free(ha->err_tq);
642 	}
643 
644 	if (ha->stats_tq) {
645 		taskqueue_drain_all(ha->stats_tq);
646 		taskqueue_free(ha->stats_tq);
647 	}
648 
649 	ql_del_cdev(ha);
650 
651 	if (ha->flags.qla_watchdog_active) {
652 		ha->qla_watchdog_exit = 1;
653 
654 		while (ha->qla_watchdog_exited == 0)
655 			qla_mdelay(__func__, 1);
656 	}
657 
658 	if (ha->flags.qla_callout_init)
659 		callout_stop(&ha->tx_callout);
660 
661 	if (ha->ifp != NULL)
662 		ether_ifdetach(ha->ifp);
663 
664 	ql_free_drvr_state_buffer(ha);
665 	ql_free_sp_log_buffer(ha);
666 	ql_free_dma(ha);
667 	qla_free_parent_dma_tag(ha);
668 
669 	if (ha->mbx_handle)
670 		(void)bus_teardown_intr(dev, ha->mbx_irq, ha->mbx_handle);
671 
672 	if (ha->mbx_irq)
673 		(void) bus_release_resource(dev, SYS_RES_IRQ, ha->mbx_irq_rid,
674 				ha->mbx_irq);
675 
676 	for (i = 0; i < ha->hw.num_sds_rings; i++) {
677 		if (ha->irq_vec[i].handle) {
678 			(void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
679 					ha->irq_vec[i].handle);
680 		}
681 
682 		if (ha->irq_vec[i].irq) {
683 			(void)bus_release_resource(dev, SYS_RES_IRQ,
684 				ha->irq_vec[i].irq_rid,
685 				ha->irq_vec[i].irq);
686 		}
687 
688 		qla_free_tx_br(ha, &ha->tx_fp[i]);
689 	}
690 	qla_destroy_fp_taskqueues(ha);
691 
692 	if (ha->msix_count)
693 		pci_release_msi(dev);
694 
695         if (ha->pci_reg)
696                 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
697 				ha->pci_reg);
698 
699         if (ha->pci_reg1)
700                 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
701 				ha->pci_reg1);
702 
703 	return;
704 }
705 
706 /*
707  * DMA Related Functions
708  */
709 
710 static void
711 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
712 {
713         *((bus_addr_t *)arg) = 0;
714 
715         if (error) {
716                 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
717                 return;
718 	}
719 
720         *((bus_addr_t *)arg) = segs[0].ds_addr;
721 
722 	return;
723 }
724 
725 int
726 ql_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
727 {
728         int             ret = 0;
729         device_t        dev;
730         bus_addr_t      b_addr;
731 
732         dev = ha->pci_dev;
733 
734         QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
735 
736         ret = bus_dma_tag_create(
737                         ha->parent_tag,/* parent */
738                         dma_buf->alignment,
739                         ((bus_size_t)(1ULL << 32)),/* boundary */
740                         BUS_SPACE_MAXADDR,      /* lowaddr */
741                         BUS_SPACE_MAXADDR,      /* highaddr */
742                         NULL, NULL,             /* filter, filterarg */
743                         dma_buf->size,          /* maxsize */
744                         1,                      /* nsegments */
745                         dma_buf->size,          /* maxsegsize */
746                         0,                      /* flags */
747                         NULL, NULL,             /* lockfunc, lockarg */
748                         &dma_buf->dma_tag);
749 
750         if (ret) {
751                 device_printf(dev, "%s: could not create dma tag\n", __func__);
752                 goto ql_alloc_dmabuf_exit;
753         }
754         ret = bus_dmamem_alloc(dma_buf->dma_tag,
755                         (void **)&dma_buf->dma_b,
756                         (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
757                         &dma_buf->dma_map);
758         if (ret) {
759                 bus_dma_tag_destroy(dma_buf->dma_tag);
760                 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
761                 goto ql_alloc_dmabuf_exit;
762         }
763 
764         ret = bus_dmamap_load(dma_buf->dma_tag,
765                         dma_buf->dma_map,
766                         dma_buf->dma_b,
767                         dma_buf->size,
768                         qla_dmamap_callback,
769                         &b_addr, BUS_DMA_NOWAIT);
770 
771         if (ret || !b_addr) {
772                 bus_dma_tag_destroy(dma_buf->dma_tag);
773                 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
774                         dma_buf->dma_map);
775                 ret = -1;
776                 goto ql_alloc_dmabuf_exit;
777         }
778 
779         dma_buf->dma_addr = b_addr;
780 
781 ql_alloc_dmabuf_exit:
782         QL_DPRINT2(ha, (dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
783                 __func__, ret, (void *)dma_buf->dma_tag,
784                 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
785 		dma_buf->size));
786 
787         return ret;
788 }
789 
790 void
791 ql_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
792 {
793 	bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
794         bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
795         bus_dma_tag_destroy(dma_buf->dma_tag);
796 }
797 
798 static int
799 qla_alloc_parent_dma_tag(qla_host_t *ha)
800 {
801 	int		ret;
802 	device_t	dev;
803 
804 	dev = ha->pci_dev;
805 
806         /*
807          * Allocate parent DMA Tag
808          */
809         ret = bus_dma_tag_create(
810                         bus_get_dma_tag(dev),   /* parent */
811                         1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
812                         BUS_SPACE_MAXADDR,      /* lowaddr */
813                         BUS_SPACE_MAXADDR,      /* highaddr */
814                         NULL, NULL,             /* filter, filterarg */
815                         BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
816                         0,                      /* nsegments */
817                         BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
818                         0,                      /* flags */
819                         NULL, NULL,             /* lockfunc, lockarg */
820                         &ha->parent_tag);
821 
822         if (ret) {
823                 device_printf(dev, "%s: could not create parent dma tag\n",
824                         __func__);
825 		return (-1);
826         }
827 
828         ha->flags.parent_tag = 1;
829 
830 	return (0);
831 }
832 
833 static void
834 qla_free_parent_dma_tag(qla_host_t *ha)
835 {
836         if (ha->flags.parent_tag) {
837                 bus_dma_tag_destroy(ha->parent_tag);
838                 ha->flags.parent_tag = 0;
839         }
840 }
841 
842 /*
843  * Name: qla_init_ifnet
844  * Function: Creates the Network Device Interface and Registers it with the O.S
845  */
846 
847 static void
848 qla_init_ifnet(device_t dev, qla_host_t *ha)
849 {
850 	if_t ifp;
851 
852 	QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
853 
854 	ifp = ha->ifp = if_alloc(IFT_ETHER);
855 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
856 
857 	if_setbaudrate(ifp, IF_Gbps(10));
858 	if_setcapabilities(ifp, IFCAP_LINKSTATE);
859 	if_setmtu(ifp, ETHERMTU);
860 
861 	if_setinitfn(ifp, qla_init);
862 	if_setsoftc(ifp, ha);
863 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
864 	if_setioctlfn(ifp, qla_ioctl);
865 
866 	if_settransmitfn(ifp, qla_transmit);
867 	if_setqflushfn(ifp, qla_qflush);
868 
869 	if_setsendqlen(ifp, qla_get_ifq_snd_maxlen(ha));
870 	if_setsendqready(ifp);
871 
872 	ha->max_frame_size = if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN;
873 
874 	ether_ifattach(ifp, qla_get_mac_addr(ha));
875 
876 	if_setcapabilitiesbit(ifp, IFCAP_HWCSUM |
877 				IFCAP_TSO4 |
878 				IFCAP_TSO6 |
879 				IFCAP_JUMBO_MTU |
880 				IFCAP_VLAN_HWTAGGING |
881 				IFCAP_VLAN_MTU |
882 				IFCAP_VLAN_HWTSO |
883 				IFCAP_LRO, 0);
884 
885 	if_setcapenable(ifp, if_getcapabilities(ifp));
886 
887 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
888 
889 	ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
890 
891 	ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
892 		NULL);
893 	ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
894 
895 	ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
896 
897 	QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
898 
899 	return;
900 }
901 
902 static void
903 qla_init_locked(qla_host_t *ha)
904 {
905 	if_t ifp = ha->ifp;
906 
907 	ql_sp_log(ha, 14, 0, 0, 0, 0, 0, 0);
908 
909 	qla_stop(ha);
910 
911 	if (qla_alloc_xmt_bufs(ha) != 0)
912 		return;
913 
914 	qla_confirm_9kb_enable(ha);
915 
916 	if (qla_alloc_rcv_bufs(ha) != 0)
917 		return;
918 
919 	bcopy(if_getlladdr(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
920 
921 	if_sethwassist(ifp, CSUM_TCP | CSUM_UDP | CSUM_TSO);
922 	if_sethwassistbits(ifp, CSUM_TCP_IPV6 | CSUM_UDP_IPV6, 0);
923 
924 	ha->stop_rcv = 0;
925  	if (ql_init_hw_if(ha) == 0) {
926 		ifp = ha->ifp;
927 		if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
928 		ha->hw_vlan_tx_frames = 0;
929 		ha->tx_tso_frames = 0;
930 		ha->qla_interface_up = 1;
931 		ql_update_link_state(ha);
932 	} else {
933 		if (ha->hw.sp_log_stop_events & Q8_SP_LOG_STOP_IF_START_FAILURE)
934 			ha->hw.sp_log_stop = -1;
935 	}
936 
937 	ha->qla_watchdog_pause = 0;
938 
939 	return;
940 }
941 
942 static void
943 qla_init(void *arg)
944 {
945 	qla_host_t *ha;
946 
947 	ha = (qla_host_t *)arg;
948 
949 	QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
950 
951 	if (QLA_LOCK(ha, __func__, -1, 0) != 0)
952 		return;
953 
954 	qla_init_locked(ha);
955 
956 	QLA_UNLOCK(ha, __func__);
957 
958 	QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
959 }
960 
961 static u_int
962 qla_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int mcnt)
963 {
964 	uint8_t *mta = arg;
965 
966 	if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
967 		return (0);
968 
969 	bcopy(LLADDR(sdl), &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
970 
971 	return (1);
972 }
973 
974 static int
975 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
976 {
977 	uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
978 	int mcnt = 0;
979 	if_t ifp = ha->ifp;
980 	int ret = 0;
981 
982 	mcnt = if_foreach_llmaddr(ifp, qla_copy_maddr, mta);
983 
984 	if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
985 		QLA_LOCK_NO_SLEEP) != 0)
986 		return (-1);
987 
988 	ql_sp_log(ha, 12, 4, if_getdrvflags(ifp),
989 		(if_getdrvflags(ifp) & IFF_DRV_RUNNING),
990 		add_multi, (uint32_t)mcnt, 0);
991 
992 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
993 		if (!add_multi) {
994 			ret = qla_hw_del_all_mcast(ha);
995 
996 			if (ret)
997 				device_printf(ha->pci_dev,
998 					"%s: qla_hw_del_all_mcast() failed\n",
999 				__func__);
1000 		}
1001 
1002 		if (!ret)
1003 			ret = ql_hw_set_multi(ha, mta, mcnt, 1);
1004 	}
1005 
1006 	QLA_UNLOCK(ha, __func__);
1007 
1008 	return (ret);
1009 }
1010 
1011 static int
1012 qla_ioctl(if_t ifp, u_long cmd, caddr_t data)
1013 {
1014 	int ret = 0;
1015 	struct ifreq *ifr = (struct ifreq *)data;
1016 #ifdef INET
1017 	struct ifaddr *ifa = (struct ifaddr *)data;
1018 #endif
1019 	qla_host_t *ha;
1020 
1021 	ha = (qla_host_t *)if_getsoftc(ifp);
1022 	if (ha->offline || ha->qla_initiate_recovery)
1023 		return (ret);
1024 
1025 	switch (cmd) {
1026 	case SIOCSIFADDR:
1027 		QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
1028 			__func__, cmd));
1029 
1030 #ifdef INET
1031 		if (ifa->ifa_addr->sa_family == AF_INET) {
1032 			ret = QLA_LOCK(ha, __func__,
1033 					QLA_LOCK_DEFAULT_MS_TIMEOUT,
1034 					QLA_LOCK_NO_SLEEP);
1035 			if (ret)
1036 				break;
1037 
1038 			if_setflagbits(ifp, IFF_UP, 0);
1039 
1040 			ql_sp_log(ha, 8, 3, if_getdrvflags(ifp),
1041 				(if_getdrvflags(ifp) & IFF_DRV_RUNNING),
1042 				ntohl(IA_SIN(ifa)->sin_addr.s_addr), 0, 0);
1043 
1044 			if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
1045 				qla_init_locked(ha);
1046 			}
1047 
1048 			QLA_UNLOCK(ha, __func__);
1049 			QL_DPRINT4(ha, (ha->pci_dev,
1050 				"%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1051 				__func__, cmd,
1052 				ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1053 
1054 			arp_ifinit(ifp, ifa);
1055 			break;
1056 		}
1057 #endif
1058 		ether_ioctl(ifp, cmd, data);
1059 		break;
1060 
1061 	case SIOCSIFMTU:
1062 		QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1063 			__func__, cmd));
1064 
1065 		if (ifr->ifr_mtu > QLA_MAX_MTU) {
1066 			ret = EINVAL;
1067 		} else {
1068 			ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1069 					QLA_LOCK_NO_SLEEP);
1070 
1071 			if (ret)
1072 				break;
1073 
1074 			if_setmtu(ifp, ifr->ifr_mtu);
1075 			ha->max_frame_size =
1076 				if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN;
1077 
1078 			ql_sp_log(ha, 9, 4, if_getdrvflags(ifp),
1079 				(if_getdrvflags(ifp) & IFF_DRV_RUNNING),
1080 				ha->max_frame_size, if_getmtu(ifp), 0);
1081 
1082 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1083 				qla_init_locked(ha);
1084 			}
1085 
1086 			if (if_getmtu(ifp) > ETHERMTU)
1087 				ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1088 			else
1089 				ha->std_replenish = QL_STD_REPLENISH_THRES;
1090 
1091 
1092 			QLA_UNLOCK(ha, __func__);
1093 		}
1094 
1095 		break;
1096 
1097 	case SIOCSIFFLAGS:
1098 		QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1099 			__func__, cmd));
1100 
1101 		ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1102 				QLA_LOCK_NO_SLEEP);
1103 
1104 		if (ret)
1105 			break;
1106 
1107 		ql_sp_log(ha, 10, 4, if_getdrvflags(ifp),
1108 			(if_getdrvflags(ifp) & IFF_DRV_RUNNING),
1109 			ha->if_flags, if_getflags(ifp), 0);
1110 
1111 		if (if_getflags(ifp) & IFF_UP) {
1112 			ha->max_frame_size = if_getmtu(ifp) +
1113 					ETHER_HDR_LEN + ETHER_CRC_LEN;
1114 			qla_init_locked(ha);
1115 
1116 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1117 				if ((if_getflags(ifp) ^ ha->if_flags) &
1118 					IFF_PROMISC) {
1119 					ret = ql_set_promisc(ha);
1120 				} else if ((if_getflags(ifp) ^ ha->if_flags) &
1121 					IFF_ALLMULTI) {
1122 					ret = ql_set_allmulti(ha);
1123 				}
1124 			}
1125 		} else {
1126 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1127 				qla_stop(ha);
1128 			ha->if_flags = if_getflags(ifp);
1129 		}
1130 
1131 		QLA_UNLOCK(ha, __func__);
1132 		break;
1133 
1134 	case SIOCADDMULTI:
1135 		QL_DPRINT4(ha, (ha->pci_dev,
1136 			"%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1137 
1138 		if (qla_set_multi(ha, 1))
1139 			ret = EINVAL;
1140 		break;
1141 
1142 	case SIOCDELMULTI:
1143 		QL_DPRINT4(ha, (ha->pci_dev,
1144 			"%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1145 
1146 		if (qla_set_multi(ha, 0))
1147 			ret = EINVAL;
1148 		break;
1149 
1150 	case SIOCSIFMEDIA:
1151 	case SIOCGIFMEDIA:
1152 		QL_DPRINT4(ha, (ha->pci_dev,
1153 			"%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1154 			__func__, cmd));
1155 		ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1156 		break;
1157 
1158 	case SIOCSIFCAP:
1159 	{
1160 		int mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
1161 
1162 		QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1163 			__func__, cmd));
1164 
1165 		if (mask & IFCAP_HWCSUM)
1166 			if_togglecapenable(ifp, IFCAP_HWCSUM);
1167 		if (mask & IFCAP_TSO4)
1168 			if_togglecapenable(ifp, IFCAP_TSO4);
1169 		if (mask & IFCAP_TSO6)
1170 			if_togglecapenable(ifp, IFCAP_TSO6);
1171 		if (mask & IFCAP_VLAN_HWTAGGING)
1172 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
1173 		if (mask & IFCAP_VLAN_HWTSO)
1174 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
1175 		if (mask & IFCAP_LRO)
1176 			if_togglecapenable(ifp, IFCAP_LRO);
1177 
1178 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1179 			ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1180 				QLA_LOCK_NO_SLEEP);
1181 
1182 			if (ret)
1183 				break;
1184 
1185 			ql_sp_log(ha, 11, 4, if_getdrvflags(ifp),
1186 				(if_getdrvflags(ifp) & IFF_DRV_RUNNING),
1187 				mask, if_getcapenable(ifp), 0);
1188 
1189 			qla_init_locked(ha);
1190 
1191 			QLA_UNLOCK(ha, __func__);
1192 		}
1193 		VLAN_CAPABILITIES(ifp);
1194 		break;
1195 	}
1196 
1197 	default:
1198 		QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1199 			__func__, cmd));
1200 		ret = ether_ioctl(ifp, cmd, data);
1201 		break;
1202 	}
1203 
1204 	return (ret);
1205 }
1206 
1207 static int
1208 qla_media_change(if_t ifp)
1209 {
1210 	qla_host_t *ha;
1211 	struct ifmedia *ifm;
1212 	int ret = 0;
1213 
1214 	ha = (qla_host_t *)if_getsoftc(ifp);
1215 
1216 	QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1217 
1218 	ifm = &ha->media;
1219 
1220 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1221 		ret = EINVAL;
1222 
1223 	QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1224 
1225 	return (ret);
1226 }
1227 
1228 static void
1229 qla_media_status(if_t ifp, struct ifmediareq *ifmr)
1230 {
1231 	qla_host_t *ha;
1232 
1233 	ha = (qla_host_t *)if_getsoftc(ifp);
1234 
1235 	QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1236 
1237 	ifmr->ifm_status = IFM_AVALID;
1238 	ifmr->ifm_active = IFM_ETHER;
1239 
1240 	ql_update_link_state(ha);
1241 	if (ha->hw.link_up) {
1242 		ifmr->ifm_status |= IFM_ACTIVE;
1243 		ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1244 	}
1245 
1246 	QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1247 		(ha->hw.link_up ? "link_up" : "link_down")));
1248 
1249 	return;
1250 }
1251 
1252 static int
1253 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1254 	uint32_t iscsi_pdu)
1255 {
1256 	bus_dma_segment_t	segs[QLA_MAX_SEGMENTS];
1257 	bus_dmamap_t		map;
1258 	int			nsegs;
1259 	int			ret = -1;
1260 	uint32_t		tx_idx;
1261 	struct mbuf		*m_head = *m_headp;
1262 
1263 	QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1264 
1265 	tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1266 
1267 	if ((NULL != ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head) ||
1268 		(QL_ERR_INJECT(ha, INJCT_TXBUF_MBUF_NON_NULL))){
1269 		QL_ASSERT(ha, 0, ("%s [%d]: txr_idx = %d tx_idx = %d "\
1270 			"mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,\
1271 			ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head));
1272 
1273 		QL_DPRINT2(ha, (ha->pci_dev, "%s [%d]: txr_idx = %d tx_idx = %d "
1274 			"mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,
1275 			ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head));
1276 
1277 		if (m_head)
1278 			m_freem(m_head);
1279 		*m_headp = NULL;
1280 		QL_INITIATE_RECOVERY(ha);
1281 		return (ret);
1282 	}
1283 
1284 	map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1285 
1286 	ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1287 			BUS_DMA_NOWAIT);
1288 
1289 	if (ret == EFBIG) {
1290 		struct mbuf *m;
1291 
1292 		QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1293 			m_head->m_pkthdr.len));
1294 
1295 		m = m_defrag(m_head, M_NOWAIT);
1296 		if (m == NULL) {
1297 			ha->err_tx_defrag++;
1298 			m_freem(m_head);
1299 			*m_headp = NULL;
1300 			device_printf(ha->pci_dev,
1301 				"%s: m_defrag() = NULL [%d]\n",
1302 				__func__, ret);
1303 			return (ENOBUFS);
1304 		}
1305 		m_head = m;
1306 		*m_headp = m_head;
1307 
1308 		if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1309 					segs, &nsegs, BUS_DMA_NOWAIT))) {
1310 			ha->err_tx_dmamap_load++;
1311 
1312 			device_printf(ha->pci_dev,
1313 				"%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1314 				__func__, ret, m_head->m_pkthdr.len);
1315 
1316 			if (ret != ENOMEM) {
1317 				m_freem(m_head);
1318 				*m_headp = NULL;
1319 			}
1320 			return (ret);
1321 		}
1322 
1323 	} else if (ret) {
1324 		ha->err_tx_dmamap_load++;
1325 
1326 		device_printf(ha->pci_dev,
1327 			"%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1328 			__func__, ret, m_head->m_pkthdr.len);
1329 
1330 		if (ret != ENOMEM) {
1331 			m_freem(m_head);
1332 			*m_headp = NULL;
1333 		}
1334 		return (ret);
1335 	}
1336 
1337 	QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1338 
1339 	bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1340 
1341         if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1342 				iscsi_pdu))) {
1343 		ha->tx_ring[txr_idx].count++;
1344 		if (iscsi_pdu)
1345 			ha->tx_ring[txr_idx].iscsi_pkt_count++;
1346 		ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1347 	} else {
1348 		bus_dmamap_unload(ha->tx_tag, map);
1349 		if (ret == EINVAL) {
1350 			if (m_head)
1351 				m_freem(m_head);
1352 			*m_headp = NULL;
1353 		}
1354 	}
1355 
1356 	QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1357 	return (ret);
1358 }
1359 
1360 static int
1361 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1362 {
1363         snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1364                 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1365 
1366         mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1367 
1368         fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1369                                    M_NOWAIT, &fp->tx_mtx);
1370         if (fp->tx_br == NULL) {
1371             QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1372                 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1373             return (-ENOMEM);
1374         }
1375         return 0;
1376 }
1377 
1378 static void
1379 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1380 {
1381         struct mbuf *mp;
1382         if_t ifp = ha->ifp;
1383 
1384         if (mtx_initialized(&fp->tx_mtx)) {
1385                 if (fp->tx_br != NULL) {
1386                         mtx_lock(&fp->tx_mtx);
1387 
1388                         while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1389                                 m_freem(mp);
1390                         }
1391 
1392                         mtx_unlock(&fp->tx_mtx);
1393 
1394                         buf_ring_free(fp->tx_br, M_DEVBUF);
1395                         fp->tx_br = NULL;
1396                 }
1397                 mtx_destroy(&fp->tx_mtx);
1398         }
1399         return;
1400 }
1401 
1402 static void
1403 qla_fp_taskqueue(void *context, int pending)
1404 {
1405         qla_tx_fp_t *fp;
1406         qla_host_t *ha;
1407         if_t ifp;
1408         struct mbuf  *mp = NULL;
1409         int ret = 0;
1410 	uint32_t txr_idx;
1411 	uint32_t iscsi_pdu = 0;
1412 	uint32_t rx_pkts_left = -1;
1413 
1414         fp = context;
1415 
1416         if (fp == NULL)
1417                 return;
1418 
1419         ha = (qla_host_t *)fp->ha;
1420 
1421         ifp = ha->ifp;
1422 
1423 	txr_idx = fp->txr_idx;
1424 
1425         mtx_lock(&fp->tx_mtx);
1426 
1427         if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1428                 mtx_unlock(&fp->tx_mtx);
1429                 goto qla_fp_taskqueue_exit;
1430         }
1431 
1432 	while (rx_pkts_left && !ha->stop_rcv &&
1433 		(if_getdrvflags(ifp) & IFF_DRV_RUNNING) && ha->hw.link_up) {
1434 		rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1435 
1436 #ifdef QL_ENABLE_ISCSI_TLV
1437 		ql_hw_tx_done_locked(ha, fp->txr_idx);
1438 		ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1439 #else
1440 		ql_hw_tx_done_locked(ha, fp->txr_idx);
1441 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1442 
1443 		mp = drbr_peek(ifp, fp->tx_br);
1444 
1445         	while (mp != NULL) {
1446 			if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1447 #ifdef QL_ENABLE_ISCSI_TLV
1448 				if (ql_iscsi_pdu(ha, mp) == 0) {
1449 					txr_idx = txr_idx +
1450 						(ha->hw.num_tx_rings >> 1);
1451 					iscsi_pdu = 1;
1452 				} else {
1453 					iscsi_pdu = 0;
1454 					txr_idx = fp->txr_idx;
1455 				}
1456 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1457 			}
1458 
1459 			ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1460 
1461 			if (ret) {
1462 				if (mp != NULL)
1463 					drbr_putback(ifp, fp->tx_br, mp);
1464 				else {
1465 					drbr_advance(ifp, fp->tx_br);
1466 				}
1467 
1468 				mtx_unlock(&fp->tx_mtx);
1469 
1470 				goto qla_fp_taskqueue_exit0;
1471 			} else {
1472 				drbr_advance(ifp, fp->tx_br);
1473 			}
1474 
1475 			/* Send a copy of the frame to the BPF listener */
1476 			ETHER_BPF_MTAP(ifp, mp);
1477 
1478 			if (((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) ||
1479 				(!ha->hw.link_up))
1480 				break;
1481 
1482 			mp = drbr_peek(ifp, fp->tx_br);
1483 		}
1484 	}
1485         mtx_unlock(&fp->tx_mtx);
1486 
1487 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
1488 		goto qla_fp_taskqueue_exit;
1489 
1490 qla_fp_taskqueue_exit0:
1491 
1492 	if (rx_pkts_left || ((mp != NULL) && ret)) {
1493 		taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1494 	} else {
1495 		if (!ha->stop_rcv) {
1496 			QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1497 		}
1498 	}
1499 
1500 qla_fp_taskqueue_exit:
1501 
1502         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1503         return;
1504 }
1505 
1506 static int
1507 qla_create_fp_taskqueues(qla_host_t *ha)
1508 {
1509         int     i;
1510         uint8_t tq_name[32];
1511 
1512         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1513                 qla_tx_fp_t *fp = &ha->tx_fp[i];
1514 
1515                 bzero(tq_name, sizeof (tq_name));
1516                 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1517 
1518                 NET_TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1519 
1520                 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1521                                         taskqueue_thread_enqueue,
1522                                         &fp->fp_taskqueue);
1523 
1524                 if (fp->fp_taskqueue == NULL)
1525                         return (-1);
1526 
1527                 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1528                         tq_name);
1529 
1530                 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1531                         fp->fp_taskqueue));
1532         }
1533 
1534         return (0);
1535 }
1536 
1537 static void
1538 qla_destroy_fp_taskqueues(qla_host_t *ha)
1539 {
1540         int     i;
1541 
1542         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1543                 qla_tx_fp_t *fp = &ha->tx_fp[i];
1544 
1545                 if (fp->fp_taskqueue != NULL) {
1546                         taskqueue_drain_all(fp->fp_taskqueue);
1547                         taskqueue_free(fp->fp_taskqueue);
1548                         fp->fp_taskqueue = NULL;
1549                 }
1550         }
1551         return;
1552 }
1553 
1554 static void
1555 qla_drain_fp_taskqueues(qla_host_t *ha)
1556 {
1557         int     i;
1558 
1559         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1560                 qla_tx_fp_t *fp = &ha->tx_fp[i];
1561 
1562                 if (fp->fp_taskqueue != NULL) {
1563                         taskqueue_drain_all(fp->fp_taskqueue);
1564                 }
1565         }
1566         return;
1567 }
1568 
1569 static int
1570 qla_transmit(if_t ifp, struct mbuf  *mp)
1571 {
1572 	qla_host_t *ha = (qla_host_t *)if_getsoftc(ifp);
1573         qla_tx_fp_t *fp;
1574         int rss_id = 0;
1575         int ret = 0;
1576 
1577         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1578 
1579         if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1580                 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1581                                         ha->hw.num_sds_rings;
1582         fp = &ha->tx_fp[rss_id];
1583 
1584         if (fp->tx_br == NULL) {
1585                 ret = EINVAL;
1586                 goto qla_transmit_exit;
1587         }
1588 
1589         if (mp != NULL) {
1590                 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1591         }
1592 
1593         if (fp->fp_taskqueue != NULL)
1594                 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1595 
1596         ret = 0;
1597 
1598 qla_transmit_exit:
1599 
1600         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1601         return ret;
1602 }
1603 
1604 static void
1605 qla_qflush(if_t ifp)
1606 {
1607         int                     i;
1608         qla_tx_fp_t		*fp;
1609         struct mbuf             *mp;
1610         qla_host_t              *ha;
1611 
1612         ha = (qla_host_t *)if_getsoftc(ifp);
1613 
1614         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1615 
1616         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1617                 fp = &ha->tx_fp[i];
1618 
1619                 if (fp == NULL)
1620                         continue;
1621 
1622                 if (fp->tx_br) {
1623                         mtx_lock(&fp->tx_mtx);
1624 
1625                         while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1626                                 m_freem(mp);
1627                         }
1628                         mtx_unlock(&fp->tx_mtx);
1629                 }
1630         }
1631         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1632 
1633         return;
1634 }
1635 
1636 static void
1637 qla_stop(qla_host_t *ha)
1638 {
1639 	if_t ifp = ha->ifp;
1640 	int i = 0;
1641 
1642 	ql_sp_log(ha, 13, 0, 0, 0, 0, 0, 0);
1643 
1644 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1645 	ha->qla_watchdog_pause = 1;
1646 
1647         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1648         	qla_tx_fp_t *fp;
1649 
1650 		fp = &ha->tx_fp[i];
1651 
1652                 if (fp == NULL)
1653                         continue;
1654 
1655 		if (fp->tx_br != NULL) {
1656                         mtx_lock(&fp->tx_mtx);
1657                         mtx_unlock(&fp->tx_mtx);
1658 		}
1659 	}
1660 
1661 	while (!ha->qla_watchdog_paused)
1662 		qla_mdelay(__func__, 1);
1663 
1664 	ha->qla_interface_up = 0;
1665 
1666 	qla_drain_fp_taskqueues(ha);
1667 
1668 	ql_del_hw_if(ha);
1669 
1670 	qla_free_xmt_bufs(ha);
1671 	qla_free_rcv_bufs(ha);
1672 
1673 	return;
1674 }
1675 
1676 /*
1677  * Buffer Management Functions for Transmit and Receive Rings
1678  */
1679 static int
1680 qla_alloc_xmt_bufs(qla_host_t *ha)
1681 {
1682 	int ret = 0;
1683 	uint32_t i, j;
1684 	qla_tx_buf_t *txb;
1685 
1686 	if (bus_dma_tag_create(NULL,    /* parent */
1687 		1, 0,    /* alignment, bounds */
1688 		BUS_SPACE_MAXADDR,       /* lowaddr */
1689 		BUS_SPACE_MAXADDR,       /* highaddr */
1690 		NULL, NULL,      /* filter, filterarg */
1691 		QLA_MAX_TSO_FRAME_SIZE,     /* maxsize */
1692 		QLA_MAX_SEGMENTS,        /* nsegments */
1693 		PAGE_SIZE,        /* maxsegsize */
1694 		BUS_DMA_ALLOCNOW,        /* flags */
1695 		NULL,    /* lockfunc */
1696 		NULL,    /* lockfuncarg */
1697 		&ha->tx_tag)) {
1698 		device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1699 			__func__);
1700 		return (ENOMEM);
1701 	}
1702 
1703 	for (i = 0; i < ha->hw.num_tx_rings; i++) {
1704 		bzero((void *)ha->tx_ring[i].tx_buf,
1705 			(sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1706 	}
1707 
1708 	for (j = 0; j < ha->hw.num_tx_rings; j++) {
1709 		for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1710 			txb = &ha->tx_ring[j].tx_buf[i];
1711 
1712 			if ((ret = bus_dmamap_create(ha->tx_tag,
1713 					BUS_DMA_NOWAIT, &txb->map))) {
1714 				ha->err_tx_dmamap_create++;
1715 				device_printf(ha->pci_dev,
1716 					"%s: bus_dmamap_create failed[%d]\n",
1717 					__func__, ret);
1718 
1719 				qla_free_xmt_bufs(ha);
1720 
1721 				return (ret);
1722 			}
1723 		}
1724 	}
1725 
1726 	return 0;
1727 }
1728 
1729 /*
1730  * Release mbuf after it sent on the wire
1731  */
1732 static void
1733 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1734 {
1735 	QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1736 
1737 	if (txb->m_head) {
1738 		bus_dmamap_sync(ha->tx_tag, txb->map,
1739 			BUS_DMASYNC_POSTWRITE);
1740 
1741 		bus_dmamap_unload(ha->tx_tag, txb->map);
1742 
1743 		m_freem(txb->m_head);
1744 		txb->m_head = NULL;
1745 
1746 		bus_dmamap_destroy(ha->tx_tag, txb->map);
1747 		txb->map = NULL;
1748 	}
1749 
1750 	if (txb->map) {
1751 		bus_dmamap_unload(ha->tx_tag, txb->map);
1752 		bus_dmamap_destroy(ha->tx_tag, txb->map);
1753 		txb->map = NULL;
1754 	}
1755 
1756 	QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1757 }
1758 
1759 static void
1760 qla_free_xmt_bufs(qla_host_t *ha)
1761 {
1762 	int		i, j;
1763 
1764 	for (j = 0; j < ha->hw.num_tx_rings; j++) {
1765 		for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1766 			qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1767 	}
1768 
1769 	if (ha->tx_tag != NULL) {
1770 		bus_dma_tag_destroy(ha->tx_tag);
1771 		ha->tx_tag = NULL;
1772 	}
1773 
1774 	for (i = 0; i < ha->hw.num_tx_rings; i++) {
1775 		bzero((void *)ha->tx_ring[i].tx_buf,
1776 			(sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1777 	}
1778 	return;
1779 }
1780 
1781 static int
1782 qla_alloc_rcv_std(qla_host_t *ha)
1783 {
1784 	int		i, j, k, r, ret = 0;
1785 	qla_rx_buf_t	*rxb;
1786 	qla_rx_ring_t	*rx_ring;
1787 
1788 	for (r = 0; r < ha->hw.num_rds_rings; r++) {
1789 		rx_ring = &ha->rx_ring[r];
1790 
1791 		for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1792 			rxb = &rx_ring->rx_buf[i];
1793 
1794 			ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1795 					&rxb->map);
1796 
1797 			if (ret) {
1798 				device_printf(ha->pci_dev,
1799 					"%s: dmamap[%d, %d] failed\n",
1800 					__func__, r, i);
1801 
1802 				for (k = 0; k < r; k++) {
1803 					for (j = 0; j < NUM_RX_DESCRIPTORS;
1804 						j++) {
1805 						rxb = &ha->rx_ring[k].rx_buf[j];
1806 						bus_dmamap_destroy(ha->rx_tag,
1807 							rxb->map);
1808 					}
1809 				}
1810 
1811 				for (j = 0; j < i; j++) {
1812 					bus_dmamap_destroy(ha->rx_tag,
1813 						rx_ring->rx_buf[j].map);
1814 				}
1815 				goto qla_alloc_rcv_std_err;
1816 			}
1817 		}
1818 	}
1819 
1820 	qla_init_hw_rcv_descriptors(ha);
1821 
1822 	for (r = 0; r < ha->hw.num_rds_rings; r++) {
1823 		rx_ring = &ha->rx_ring[r];
1824 
1825 		for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1826 			rxb = &rx_ring->rx_buf[i];
1827 			rxb->handle = i;
1828 			if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1829 				/*
1830 			 	 * set the physical address in the
1831 				 * corresponding descriptor entry in the
1832 				 * receive ring/queue for the hba
1833 				 */
1834 				qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1835 					rxb->paddr,
1836 					(rxb->m_head)->m_pkthdr.len);
1837 			} else {
1838 				device_printf(ha->pci_dev,
1839 					"%s: ql_get_mbuf [%d, %d] failed\n",
1840 					__func__, r, i);
1841 				bus_dmamap_destroy(ha->rx_tag, rxb->map);
1842 				goto qla_alloc_rcv_std_err;
1843 			}
1844 		}
1845 	}
1846 	return 0;
1847 
1848 qla_alloc_rcv_std_err:
1849 	return (-1);
1850 }
1851 
1852 static void
1853 qla_free_rcv_std(qla_host_t *ha)
1854 {
1855 	int		i, r;
1856 	qla_rx_buf_t	*rxb;
1857 
1858 	for (r = 0; r < ha->hw.num_rds_rings; r++) {
1859 		for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1860 			rxb = &ha->rx_ring[r].rx_buf[i];
1861 			if (rxb->m_head != NULL) {
1862 				bus_dmamap_unload(ha->rx_tag, rxb->map);
1863 				bus_dmamap_destroy(ha->rx_tag, rxb->map);
1864 				m_freem(rxb->m_head);
1865 				rxb->m_head = NULL;
1866 			}
1867 		}
1868 	}
1869 	return;
1870 }
1871 
1872 static int
1873 qla_alloc_rcv_bufs(qla_host_t *ha)
1874 {
1875 	int		i, ret = 0;
1876 
1877 	if (bus_dma_tag_create(NULL,    /* parent */
1878 			1, 0,    /* alignment, bounds */
1879 			BUS_SPACE_MAXADDR,       /* lowaddr */
1880 			BUS_SPACE_MAXADDR,       /* highaddr */
1881 			NULL, NULL,      /* filter, filterarg */
1882 			MJUM9BYTES,     /* maxsize */
1883 			1,        /* nsegments */
1884 			MJUM9BYTES,        /* maxsegsize */
1885 			BUS_DMA_ALLOCNOW,        /* flags */
1886 			NULL,    /* lockfunc */
1887 			NULL,    /* lockfuncarg */
1888 			&ha->rx_tag)) {
1889 		device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1890 			__func__);
1891 
1892 		return (ENOMEM);
1893 	}
1894 
1895 	bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1896 
1897 	for (i = 0; i < ha->hw.num_sds_rings; i++) {
1898 		ha->hw.sds[i].sdsr_next = 0;
1899 		ha->hw.sds[i].rxb_free = NULL;
1900 		ha->hw.sds[i].rx_free = 0;
1901 	}
1902 
1903 	ret = qla_alloc_rcv_std(ha);
1904 
1905 	return (ret);
1906 }
1907 
1908 static void
1909 qla_free_rcv_bufs(qla_host_t *ha)
1910 {
1911 	int		i;
1912 
1913 	qla_free_rcv_std(ha);
1914 
1915 	if (ha->rx_tag != NULL) {
1916 		bus_dma_tag_destroy(ha->rx_tag);
1917 		ha->rx_tag = NULL;
1918 	}
1919 
1920 	bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1921 
1922 	for (i = 0; i < ha->hw.num_sds_rings; i++) {
1923 		ha->hw.sds[i].sdsr_next = 0;
1924 		ha->hw.sds[i].rxb_free = NULL;
1925 		ha->hw.sds[i].rx_free = 0;
1926 	}
1927 
1928 	return;
1929 }
1930 
1931 int
1932 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1933 {
1934 	register struct mbuf *mp = nmp;
1935 	int            		ret = 0;
1936 	uint32_t		offset;
1937 	bus_dma_segment_t	segs[1];
1938 	int			nsegs, mbuf_size;
1939 
1940 	QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1941 
1942         if (ha->hw.enable_9kb)
1943                 mbuf_size = MJUM9BYTES;
1944         else
1945                 mbuf_size = MCLBYTES;
1946 
1947 	if (mp == NULL) {
1948 		if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1949 			return(-1);
1950 
1951                 if (ha->hw.enable_9kb)
1952                         mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
1953                 else
1954                         mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1955 
1956 		if (mp == NULL) {
1957 			ha->err_m_getcl++;
1958 			ret = ENOBUFS;
1959 			device_printf(ha->pci_dev,
1960 					"%s: m_getcl failed\n", __func__);
1961 			goto exit_ql_get_mbuf;
1962 		}
1963 		mp->m_len = mp->m_pkthdr.len = mbuf_size;
1964 	} else {
1965 		mp->m_len = mp->m_pkthdr.len = mbuf_size;
1966 		mp->m_data = mp->m_ext.ext_buf;
1967 		mp->m_next = NULL;
1968 	}
1969 
1970 	offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1971 	if (offset) {
1972 		offset = 8 - offset;
1973 		m_adj(mp, offset);
1974 	}
1975 
1976 	/*
1977 	 * Using memory from the mbuf cluster pool, invoke the bus_dma
1978 	 * machinery to arrange the memory mapping.
1979 	 */
1980 	ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1981 			mp, segs, &nsegs, BUS_DMA_NOWAIT);
1982 	rxb->paddr = segs[0].ds_addr;
1983 
1984 	if (ret || !rxb->paddr || (nsegs != 1)) {
1985 		m_free(mp);
1986 		rxb->m_head = NULL;
1987 		device_printf(ha->pci_dev,
1988 			"%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1989 			__func__, ret, (long long unsigned int)rxb->paddr,
1990 			nsegs);
1991                 ret = -1;
1992 		goto exit_ql_get_mbuf;
1993 	}
1994 	rxb->m_head = mp;
1995 	bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1996 
1997 exit_ql_get_mbuf:
1998 	QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1999 	return (ret);
2000 }
2001 
2002 static void
2003 qla_get_peer(qla_host_t *ha)
2004 {
2005 	device_t *peers;
2006 	int count, i, slot;
2007 	int my_slot = pci_get_slot(ha->pci_dev);
2008 
2009 	if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
2010 		return;
2011 
2012 	for (i = 0; i < count; i++) {
2013 		slot = pci_get_slot(peers[i]);
2014 
2015 		if ((slot >= 0) && (slot == my_slot) &&
2016 			(pci_get_device(peers[i]) ==
2017 				pci_get_device(ha->pci_dev))) {
2018 			if (ha->pci_dev != peers[i])
2019 				ha->peer_dev = peers[i];
2020 		}
2021 	}
2022 }
2023 
2024 static void
2025 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
2026 {
2027 	qla_host_t *ha_peer;
2028 
2029 	if (ha->peer_dev) {
2030         	if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
2031 			ha_peer->msg_from_peer = msg_to_peer;
2032 		}
2033 	}
2034 }
2035 
2036 void
2037 qla_set_error_recovery(qla_host_t *ha)
2038 {
2039 	if_t ifp = ha->ifp;
2040 
2041 	if (!cold && ha->enable_error_recovery) {
2042 		if (ifp)
2043 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2044 		ha->qla_initiate_recovery = 1;
2045 	} else
2046 		ha->offline = 1;
2047 	return;
2048 }
2049 
2050 static void
2051 qla_error_recovery(void *context, int pending)
2052 {
2053 	qla_host_t *ha = context;
2054 	uint32_t msecs_100 = 400;
2055 	if_t ifp = ha->ifp;
2056 	int i = 0;
2057 
2058 	device_printf(ha->pci_dev, "%s: enter\n", __func__);
2059 	ha->hw.imd_compl = 1;
2060 
2061 	taskqueue_drain_all(ha->stats_tq);
2062 	taskqueue_drain_all(ha->async_event_tq);
2063 
2064 	if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2065 		return;
2066 
2067 	device_printf(ha->pci_dev, "%s: ts_usecs = %ld start\n",
2068 		__func__, qla_get_usec_timestamp());
2069 
2070 	if (ha->qla_interface_up) {
2071 		qla_mdelay(__func__, 300);
2072 
2073 
2074 
2075 		for (i = 0; i < ha->hw.num_sds_rings; i++) {
2076 	        	qla_tx_fp_t *fp;
2077 
2078 			fp = &ha->tx_fp[i];
2079 
2080 			if (fp == NULL)
2081 				continue;
2082 
2083 			if (fp->tx_br != NULL) {
2084 				mtx_lock(&fp->tx_mtx);
2085 				mtx_unlock(&fp->tx_mtx);
2086 			}
2087 		}
2088 	}
2089 
2090 	qla_drain_fp_taskqueues(ha);
2091 
2092 	if ((ha->pci_func & 0x1) == 0) {
2093 		if (!ha->msg_from_peer) {
2094 			qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2095 
2096 			while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2097 				msecs_100--)
2098 				qla_mdelay(__func__, 100);
2099 		}
2100 
2101 		ha->msg_from_peer = 0;
2102 
2103 		if (ha->enable_minidump)
2104 			ql_minidump(ha);
2105 
2106 		if (ha->enable_driverstate_dump)
2107 			ql_capture_drvr_state(ha);
2108 
2109 		if (ql_init_hw(ha)) {
2110 			device_printf(ha->pci_dev,
2111 				"%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2112 				__func__, qla_get_usec_timestamp());
2113 			ha->offline = 1;
2114 			goto qla_error_recovery_exit;
2115 		}
2116 
2117 		if (ha->qla_interface_up) {
2118 			qla_free_xmt_bufs(ha);
2119 			qla_free_rcv_bufs(ha);
2120 		}
2121 
2122 		if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2123 			qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2124 
2125 	} else {
2126 		if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2127 			ha->msg_from_peer = 0;
2128 
2129 			if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2130 				qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2131 		} else {
2132 			qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2133 		}
2134 
2135 		while ((ha->msg_from_peer != QL_PEER_MSG_ACK)  && msecs_100--)
2136 			qla_mdelay(__func__, 100);
2137 		ha->msg_from_peer = 0;
2138 
2139 		if (ha->enable_driverstate_dump)
2140 			ql_capture_drvr_state(ha);
2141 
2142 		if (msecs_100 == 0) {
2143 			device_printf(ha->pci_dev,
2144 				"%s: ts_usecs = %ld exit: QL_PEER_MSG_ACK not received\n",
2145 				__func__, qla_get_usec_timestamp());
2146 			ha->offline = 1;
2147 			goto qla_error_recovery_exit;
2148 		}
2149 
2150 		if (ql_init_hw(ha)) {
2151 			device_printf(ha->pci_dev,
2152 				"%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2153 				__func__, qla_get_usec_timestamp());
2154 			ha->offline = 1;
2155 			goto qla_error_recovery_exit;
2156 		}
2157 
2158 		if (ha->qla_interface_up) {
2159 			qla_free_xmt_bufs(ha);
2160 			qla_free_rcv_bufs(ha);
2161 		}
2162 	}
2163 
2164 	qla_mdelay(__func__, ha->ms_delay_after_init);
2165 
2166 	*((uint32_t *)&ha->hw.flags) = 0;
2167 	ha->qla_initiate_recovery = 0;
2168 
2169 	if (ha->qla_interface_up) {
2170 		if (qla_alloc_xmt_bufs(ha) != 0) {
2171 			ha->offline = 1;
2172 			goto qla_error_recovery_exit;
2173 		}
2174 
2175 		qla_confirm_9kb_enable(ha);
2176 
2177 		if (qla_alloc_rcv_bufs(ha) != 0) {
2178 			ha->offline = 1;
2179 			goto qla_error_recovery_exit;
2180 		}
2181 
2182 		ha->stop_rcv = 0;
2183 
2184 		if (ql_init_hw_if(ha) == 0) {
2185 			ifp = ha->ifp;
2186 			if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
2187 			ha->qla_watchdog_pause = 0;
2188 			ql_update_link_state(ha);
2189 		} else {
2190 			ha->offline = 1;
2191 
2192 			if (ha->hw.sp_log_stop_events &
2193 				Q8_SP_LOG_STOP_IF_START_FAILURE)
2194 				ha->hw.sp_log_stop = -1;
2195 		}
2196 	} else {
2197 		ha->qla_watchdog_pause = 0;
2198 	}
2199 
2200 qla_error_recovery_exit:
2201 
2202 	if (ha->offline ) {
2203 		device_printf(ha->pci_dev, "%s: ts_usecs = %ld port offline\n",
2204 			__func__, qla_get_usec_timestamp());
2205 		if (ha->hw.sp_log_stop_events &
2206 			Q8_SP_LOG_STOP_ERR_RECOVERY_FAILURE)
2207 			ha->hw.sp_log_stop = -1;
2208 	}
2209 
2210         QLA_UNLOCK(ha, __func__);
2211 
2212 	if (!ha->offline)
2213 		callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2214 			qla_watchdog, ha);
2215 
2216 	device_printf(ha->pci_dev,
2217 		"%s: ts_usecs = %ld exit\n",
2218 		__func__, qla_get_usec_timestamp());
2219 	return;
2220 }
2221 
2222 static void
2223 qla_async_event(void *context, int pending)
2224 {
2225         qla_host_t *ha = context;
2226 
2227 	if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2228 		return;
2229 
2230 	if (ha->async_event) {
2231 		ha->async_event = 0;
2232         	qla_hw_async_event(ha);
2233 	}
2234 
2235 	QLA_UNLOCK(ha, __func__);
2236 
2237 	return;
2238 }
2239 
2240 static void
2241 qla_stats(void *context, int pending)
2242 {
2243         qla_host_t *ha;
2244 
2245         ha = context;
2246 
2247 	ql_get_stats(ha);
2248 
2249 	return;
2250 }
2251