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