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