xref: /freebsd/sys/dev/qlxgbe/ql_os.c (revision 690b7ea081790eef2c890f63a4fe7e195cf51df0)
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 #ifdef INET
1026 	struct ifaddr *ifa = (struct ifaddr *)data;
1027 #endif
1028 	qla_host_t *ha;
1029 
1030 	ha = (qla_host_t *)ifp->if_softc;
1031 	if (ha->offline || ha->qla_initiate_recovery)
1032 		return (ret);
1033 
1034 	switch (cmd) {
1035 	case SIOCSIFADDR:
1036 		QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
1037 			__func__, cmd));
1038 
1039 #ifdef INET
1040 		if (ifa->ifa_addr->sa_family == AF_INET) {
1041 			ret = QLA_LOCK(ha, __func__,
1042 					QLA_LOCK_DEFAULT_MS_TIMEOUT,
1043 					QLA_LOCK_NO_SLEEP);
1044 			if (ret)
1045 				break;
1046 
1047 			ifp->if_flags |= IFF_UP;
1048 
1049 			ql_sp_log(ha, 8, 3, ifp->if_drv_flags,
1050 				(ifp->if_drv_flags & IFF_DRV_RUNNING),
1051 				ntohl(IA_SIN(ifa)->sin_addr.s_addr), 0, 0);
1052 
1053 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1054 				qla_init_locked(ha);
1055 			}
1056 
1057 			QLA_UNLOCK(ha, __func__);
1058 			QL_DPRINT4(ha, (ha->pci_dev,
1059 				"%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1060 				__func__, cmd,
1061 				ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1062 
1063 			arp_ifinit(ifp, ifa);
1064 			break;
1065 		}
1066 #endif
1067 		ether_ioctl(ifp, cmd, data);
1068 		break;
1069 
1070 	case SIOCSIFMTU:
1071 		QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1072 			__func__, cmd));
1073 
1074 		if (ifr->ifr_mtu > QLA_MAX_MTU) {
1075 			ret = EINVAL;
1076 		} else {
1077 			ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1078 					QLA_LOCK_NO_SLEEP);
1079 
1080 			if (ret)
1081 				break;
1082 
1083 			ifp->if_mtu = ifr->ifr_mtu;
1084 			ha->max_frame_size =
1085 				ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1086 
1087 			ql_sp_log(ha, 9, 4, ifp->if_drv_flags,
1088 				(ifp->if_drv_flags & IFF_DRV_RUNNING),
1089 				ha->max_frame_size, ifp->if_mtu, 0);
1090 
1091 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1092 				qla_init_locked(ha);
1093 			}
1094 
1095 			if (ifp->if_mtu > ETHERMTU)
1096 				ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1097 			else
1098 				ha->std_replenish = QL_STD_REPLENISH_THRES;
1099 
1100 
1101 			QLA_UNLOCK(ha, __func__);
1102 		}
1103 
1104 		break;
1105 
1106 	case SIOCSIFFLAGS:
1107 		QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1108 			__func__, cmd));
1109 
1110 		ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1111 				QLA_LOCK_NO_SLEEP);
1112 
1113 		if (ret)
1114 			break;
1115 
1116 		ql_sp_log(ha, 10, 4, ifp->if_drv_flags,
1117 			(ifp->if_drv_flags & IFF_DRV_RUNNING),
1118 			ha->if_flags, ifp->if_flags, 0);
1119 
1120 		if (ifp->if_flags & IFF_UP) {
1121 			ha->max_frame_size = ifp->if_mtu +
1122 					ETHER_HDR_LEN + ETHER_CRC_LEN;
1123 			qla_init_locked(ha);
1124 
1125 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1126 				if ((ifp->if_flags ^ ha->if_flags) &
1127 					IFF_PROMISC) {
1128 					ret = ql_set_promisc(ha);
1129 				} else if ((ifp->if_flags ^ ha->if_flags) &
1130 					IFF_ALLMULTI) {
1131 					ret = ql_set_allmulti(ha);
1132 				}
1133 			}
1134 		} else {
1135 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1136 				qla_stop(ha);
1137 			ha->if_flags = ifp->if_flags;
1138 		}
1139 
1140 		QLA_UNLOCK(ha, __func__);
1141 		break;
1142 
1143 	case SIOCADDMULTI:
1144 		QL_DPRINT4(ha, (ha->pci_dev,
1145 			"%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1146 
1147 		if (qla_set_multi(ha, 1))
1148 			ret = EINVAL;
1149 		break;
1150 
1151 	case SIOCDELMULTI:
1152 		QL_DPRINT4(ha, (ha->pci_dev,
1153 			"%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1154 
1155 		if (qla_set_multi(ha, 0))
1156 			ret = EINVAL;
1157 		break;
1158 
1159 	case SIOCSIFMEDIA:
1160 	case SIOCGIFMEDIA:
1161 		QL_DPRINT4(ha, (ha->pci_dev,
1162 			"%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1163 			__func__, cmd));
1164 		ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1165 		break;
1166 
1167 	case SIOCSIFCAP:
1168 	{
1169 		int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1170 
1171 		QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1172 			__func__, cmd));
1173 
1174 		if (mask & IFCAP_HWCSUM)
1175 			ifp->if_capenable ^= IFCAP_HWCSUM;
1176 		if (mask & IFCAP_TSO4)
1177 			ifp->if_capenable ^= IFCAP_TSO4;
1178 		if (mask & IFCAP_TSO6)
1179 			ifp->if_capenable ^= IFCAP_TSO6;
1180 		if (mask & IFCAP_VLAN_HWTAGGING)
1181 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1182 		if (mask & IFCAP_VLAN_HWTSO)
1183 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1184 		if (mask & IFCAP_LRO)
1185 			ifp->if_capenable ^= IFCAP_LRO;
1186 
1187 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1188 			ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1189 				QLA_LOCK_NO_SLEEP);
1190 
1191 			if (ret)
1192 				break;
1193 
1194 			ql_sp_log(ha, 11, 4, ifp->if_drv_flags,
1195 				(ifp->if_drv_flags & IFF_DRV_RUNNING),
1196 				mask, ifp->if_capenable, 0);
1197 
1198 			qla_init_locked(ha);
1199 
1200 			QLA_UNLOCK(ha, __func__);
1201 		}
1202 		VLAN_CAPABILITIES(ifp);
1203 		break;
1204 	}
1205 
1206 	default:
1207 		QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1208 			__func__, cmd));
1209 		ret = ether_ioctl(ifp, cmd, data);
1210 		break;
1211 	}
1212 
1213 	return (ret);
1214 }
1215 
1216 static int
1217 qla_media_change(struct ifnet *ifp)
1218 {
1219 	qla_host_t *ha;
1220 	struct ifmedia *ifm;
1221 	int ret = 0;
1222 
1223 	ha = (qla_host_t *)ifp->if_softc;
1224 
1225 	QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1226 
1227 	ifm = &ha->media;
1228 
1229 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1230 		ret = EINVAL;
1231 
1232 	QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1233 
1234 	return (ret);
1235 }
1236 
1237 static void
1238 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1239 {
1240 	qla_host_t *ha;
1241 
1242 	ha = (qla_host_t *)ifp->if_softc;
1243 
1244 	QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1245 
1246 	ifmr->ifm_status = IFM_AVALID;
1247 	ifmr->ifm_active = IFM_ETHER;
1248 
1249 	ql_update_link_state(ha);
1250 	if (ha->hw.link_up) {
1251 		ifmr->ifm_status |= IFM_ACTIVE;
1252 		ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1253 	}
1254 
1255 	QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1256 		(ha->hw.link_up ? "link_up" : "link_down")));
1257 
1258 	return;
1259 }
1260 
1261 static int
1262 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1263 	uint32_t iscsi_pdu)
1264 {
1265 	bus_dma_segment_t	segs[QLA_MAX_SEGMENTS];
1266 	bus_dmamap_t		map;
1267 	int			nsegs;
1268 	int			ret = -1;
1269 	uint32_t		tx_idx;
1270 	struct mbuf		*m_head = *m_headp;
1271 
1272 	QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1273 
1274 	tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1275 
1276 	if ((NULL != ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head) ||
1277 		(QL_ERR_INJECT(ha, INJCT_TXBUF_MBUF_NON_NULL))){
1278 		QL_ASSERT(ha, 0, ("%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 		device_printf(ha->pci_dev, "%s [%d]: txr_idx = %d tx_idx = %d "
1283 			"mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,
1284 			ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head);
1285 
1286 		if (m_head)
1287 			m_freem(m_head);
1288 		*m_headp = NULL;
1289 		QL_INITIATE_RECOVERY(ha);
1290 		return (ret);
1291 	}
1292 
1293 	map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1294 
1295 	ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1296 			BUS_DMA_NOWAIT);
1297 
1298 	if (ret == EFBIG) {
1299 		struct mbuf *m;
1300 
1301 		QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1302 			m_head->m_pkthdr.len));
1303 
1304 		m = m_defrag(m_head, M_NOWAIT);
1305 		if (m == NULL) {
1306 			ha->err_tx_defrag++;
1307 			m_freem(m_head);
1308 			*m_headp = NULL;
1309 			device_printf(ha->pci_dev,
1310 				"%s: m_defrag() = NULL [%d]\n",
1311 				__func__, ret);
1312 			return (ENOBUFS);
1313 		}
1314 		m_head = m;
1315 		*m_headp = m_head;
1316 
1317 		if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1318 					segs, &nsegs, BUS_DMA_NOWAIT))) {
1319 			ha->err_tx_dmamap_load++;
1320 
1321 			device_printf(ha->pci_dev,
1322 				"%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1323 				__func__, ret, m_head->m_pkthdr.len);
1324 
1325 			if (ret != ENOMEM) {
1326 				m_freem(m_head);
1327 				*m_headp = NULL;
1328 			}
1329 			return (ret);
1330 		}
1331 
1332 	} else if (ret) {
1333 		ha->err_tx_dmamap_load++;
1334 
1335 		device_printf(ha->pci_dev,
1336 			"%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1337 			__func__, ret, m_head->m_pkthdr.len);
1338 
1339 		if (ret != ENOMEM) {
1340 			m_freem(m_head);
1341 			*m_headp = NULL;
1342 		}
1343 		return (ret);
1344 	}
1345 
1346 	QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1347 
1348 	bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1349 
1350         if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1351 				iscsi_pdu))) {
1352 		ha->tx_ring[txr_idx].count++;
1353 		if (iscsi_pdu)
1354 			ha->tx_ring[txr_idx].iscsi_pkt_count++;
1355 		ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1356 	} else {
1357 		bus_dmamap_unload(ha->tx_tag, map);
1358 		if (ret == EINVAL) {
1359 			if (m_head)
1360 				m_freem(m_head);
1361 			*m_headp = NULL;
1362 		}
1363 	}
1364 
1365 	QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1366 	return (ret);
1367 }
1368 
1369 static int
1370 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1371 {
1372         snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1373                 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1374 
1375         mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1376 
1377         fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1378                                    M_NOWAIT, &fp->tx_mtx);
1379         if (fp->tx_br == NULL) {
1380             QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1381                 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1382             return (-ENOMEM);
1383         }
1384         return 0;
1385 }
1386 
1387 static void
1388 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1389 {
1390         struct mbuf *mp;
1391         struct ifnet *ifp = ha->ifp;
1392 
1393         if (mtx_initialized(&fp->tx_mtx)) {
1394                 if (fp->tx_br != NULL) {
1395                         mtx_lock(&fp->tx_mtx);
1396 
1397                         while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1398                                 m_freem(mp);
1399                         }
1400 
1401                         mtx_unlock(&fp->tx_mtx);
1402 
1403                         buf_ring_free(fp->tx_br, M_DEVBUF);
1404                         fp->tx_br = NULL;
1405                 }
1406                 mtx_destroy(&fp->tx_mtx);
1407         }
1408         return;
1409 }
1410 
1411 static void
1412 qla_fp_taskqueue(void *context, int pending)
1413 {
1414         qla_tx_fp_t *fp;
1415         qla_host_t *ha;
1416         struct ifnet *ifp;
1417         struct mbuf  *mp = NULL;
1418         int ret = 0;
1419 	uint32_t txr_idx;
1420 	uint32_t iscsi_pdu = 0;
1421 	uint32_t rx_pkts_left = -1;
1422 
1423         fp = context;
1424 
1425         if (fp == NULL)
1426                 return;
1427 
1428         ha = (qla_host_t *)fp->ha;
1429 
1430         ifp = ha->ifp;
1431 
1432 	txr_idx = fp->txr_idx;
1433 
1434         mtx_lock(&fp->tx_mtx);
1435 
1436         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1437                 mtx_unlock(&fp->tx_mtx);
1438                 goto qla_fp_taskqueue_exit;
1439         }
1440 
1441 	while (rx_pkts_left && !ha->stop_rcv &&
1442 		(ifp->if_drv_flags & IFF_DRV_RUNNING) && ha->hw.link_up) {
1443 		rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1444 
1445 #ifdef QL_ENABLE_ISCSI_TLV
1446 		ql_hw_tx_done_locked(ha, fp->txr_idx);
1447 		ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1448 #else
1449 		ql_hw_tx_done_locked(ha, fp->txr_idx);
1450 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1451 
1452 		mp = drbr_peek(ifp, fp->tx_br);
1453 
1454         	while (mp != NULL) {
1455 			if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1456 #ifdef QL_ENABLE_ISCSI_TLV
1457 				if (ql_iscsi_pdu(ha, mp) == 0) {
1458 					txr_idx = txr_idx +
1459 						(ha->hw.num_tx_rings >> 1);
1460 					iscsi_pdu = 1;
1461 				} else {
1462 					iscsi_pdu = 0;
1463 					txr_idx = fp->txr_idx;
1464 				}
1465 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1466 			}
1467 
1468 			ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1469 
1470 			if (ret) {
1471 				if (mp != NULL)
1472 					drbr_putback(ifp, fp->tx_br, mp);
1473 				else {
1474 					drbr_advance(ifp, fp->tx_br);
1475 				}
1476 
1477 				mtx_unlock(&fp->tx_mtx);
1478 
1479 				goto qla_fp_taskqueue_exit0;
1480 			} else {
1481 				drbr_advance(ifp, fp->tx_br);
1482 			}
1483 
1484 			/* Send a copy of the frame to the BPF listener */
1485 			ETHER_BPF_MTAP(ifp, mp);
1486 
1487 			if (((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) ||
1488 				(!ha->hw.link_up))
1489 				break;
1490 
1491 			mp = drbr_peek(ifp, fp->tx_br);
1492 		}
1493 	}
1494         mtx_unlock(&fp->tx_mtx);
1495 
1496 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1497 		goto qla_fp_taskqueue_exit;
1498 
1499 qla_fp_taskqueue_exit0:
1500 
1501 	if (rx_pkts_left || ((mp != NULL) && ret)) {
1502 		taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1503 	} else {
1504 		if (!ha->stop_rcv) {
1505 			QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1506 		}
1507 	}
1508 
1509 qla_fp_taskqueue_exit:
1510 
1511         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1512         return;
1513 }
1514 
1515 static int
1516 qla_create_fp_taskqueues(qla_host_t *ha)
1517 {
1518         int     i;
1519         uint8_t tq_name[32];
1520 
1521         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1522                 qla_tx_fp_t *fp = &ha->tx_fp[i];
1523 
1524                 bzero(tq_name, sizeof (tq_name));
1525                 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1526 
1527                 NET_TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1528 
1529                 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1530                                         taskqueue_thread_enqueue,
1531                                         &fp->fp_taskqueue);
1532 
1533                 if (fp->fp_taskqueue == NULL)
1534                         return (-1);
1535 
1536                 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1537                         tq_name);
1538 
1539                 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1540                         fp->fp_taskqueue));
1541         }
1542 
1543         return (0);
1544 }
1545 
1546 static void
1547 qla_destroy_fp_taskqueues(qla_host_t *ha)
1548 {
1549         int     i;
1550 
1551         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1552                 qla_tx_fp_t *fp = &ha->tx_fp[i];
1553 
1554                 if (fp->fp_taskqueue != NULL) {
1555                         taskqueue_drain_all(fp->fp_taskqueue);
1556                         taskqueue_free(fp->fp_taskqueue);
1557                         fp->fp_taskqueue = NULL;
1558                 }
1559         }
1560         return;
1561 }
1562 
1563 static void
1564 qla_drain_fp_taskqueues(qla_host_t *ha)
1565 {
1566         int     i;
1567 
1568         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1569                 qla_tx_fp_t *fp = &ha->tx_fp[i];
1570 
1571                 if (fp->fp_taskqueue != NULL) {
1572                         taskqueue_drain_all(fp->fp_taskqueue);
1573                 }
1574         }
1575         return;
1576 }
1577 
1578 static int
1579 qla_transmit(struct ifnet *ifp, struct mbuf  *mp)
1580 {
1581 	qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1582         qla_tx_fp_t *fp;
1583         int rss_id = 0;
1584         int ret = 0;
1585 
1586         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1587 
1588 #if __FreeBSD_version >= 1100000
1589         if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1590 #else
1591         if (mp->m_flags & M_FLOWID)
1592 #endif
1593                 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1594                                         ha->hw.num_sds_rings;
1595         fp = &ha->tx_fp[rss_id];
1596 
1597         if (fp->tx_br == NULL) {
1598                 ret = EINVAL;
1599                 goto qla_transmit_exit;
1600         }
1601 
1602         if (mp != NULL) {
1603                 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1604         }
1605 
1606         if (fp->fp_taskqueue != NULL)
1607                 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1608 
1609         ret = 0;
1610 
1611 qla_transmit_exit:
1612 
1613         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1614         return ret;
1615 }
1616 
1617 static void
1618 qla_qflush(struct ifnet *ifp)
1619 {
1620         int                     i;
1621         qla_tx_fp_t		*fp;
1622         struct mbuf             *mp;
1623         qla_host_t              *ha;
1624 
1625         ha = (qla_host_t *)ifp->if_softc;
1626 
1627         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1628 
1629         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1630                 fp = &ha->tx_fp[i];
1631 
1632                 if (fp == NULL)
1633                         continue;
1634 
1635                 if (fp->tx_br) {
1636                         mtx_lock(&fp->tx_mtx);
1637 
1638                         while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1639                                 m_freem(mp);
1640                         }
1641                         mtx_unlock(&fp->tx_mtx);
1642                 }
1643         }
1644         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1645 
1646         return;
1647 }
1648 
1649 static void
1650 qla_stop(qla_host_t *ha)
1651 {
1652 	struct ifnet *ifp = ha->ifp;
1653 	int i = 0;
1654 
1655 	ql_sp_log(ha, 13, 0, 0, 0, 0, 0, 0);
1656 
1657 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1658 	ha->qla_watchdog_pause = 1;
1659 
1660         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1661         	qla_tx_fp_t *fp;
1662 
1663 		fp = &ha->tx_fp[i];
1664 
1665                 if (fp == NULL)
1666                         continue;
1667 
1668 		if (fp->tx_br != NULL) {
1669                         mtx_lock(&fp->tx_mtx);
1670                         mtx_unlock(&fp->tx_mtx);
1671 		}
1672 	}
1673 
1674 	while (!ha->qla_watchdog_paused)
1675 		qla_mdelay(__func__, 1);
1676 
1677 	ha->qla_interface_up = 0;
1678 
1679 	qla_drain_fp_taskqueues(ha);
1680 
1681 	ql_del_hw_if(ha);
1682 
1683 	qla_free_xmt_bufs(ha);
1684 	qla_free_rcv_bufs(ha);
1685 
1686 	return;
1687 }
1688 
1689 /*
1690  * Buffer Management Functions for Transmit and Receive Rings
1691  */
1692 static int
1693 qla_alloc_xmt_bufs(qla_host_t *ha)
1694 {
1695 	int ret = 0;
1696 	uint32_t i, j;
1697 	qla_tx_buf_t *txb;
1698 
1699 	if (bus_dma_tag_create(NULL,    /* parent */
1700 		1, 0,    /* alignment, bounds */
1701 		BUS_SPACE_MAXADDR,       /* lowaddr */
1702 		BUS_SPACE_MAXADDR,       /* highaddr */
1703 		NULL, NULL,      /* filter, filterarg */
1704 		QLA_MAX_TSO_FRAME_SIZE,     /* maxsize */
1705 		QLA_MAX_SEGMENTS,        /* nsegments */
1706 		PAGE_SIZE,        /* maxsegsize */
1707 		BUS_DMA_ALLOCNOW,        /* flags */
1708 		NULL,    /* lockfunc */
1709 		NULL,    /* lockfuncarg */
1710 		&ha->tx_tag)) {
1711 		device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1712 			__func__);
1713 		return (ENOMEM);
1714 	}
1715 
1716 	for (i = 0; i < ha->hw.num_tx_rings; i++) {
1717 		bzero((void *)ha->tx_ring[i].tx_buf,
1718 			(sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1719 	}
1720 
1721 	for (j = 0; j < ha->hw.num_tx_rings; j++) {
1722 		for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1723 			txb = &ha->tx_ring[j].tx_buf[i];
1724 
1725 			if ((ret = bus_dmamap_create(ha->tx_tag,
1726 					BUS_DMA_NOWAIT, &txb->map))) {
1727 				ha->err_tx_dmamap_create++;
1728 				device_printf(ha->pci_dev,
1729 					"%s: bus_dmamap_create failed[%d]\n",
1730 					__func__, ret);
1731 
1732 				qla_free_xmt_bufs(ha);
1733 
1734 				return (ret);
1735 			}
1736 		}
1737 	}
1738 
1739 	return 0;
1740 }
1741 
1742 /*
1743  * Release mbuf after it sent on the wire
1744  */
1745 static void
1746 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1747 {
1748 	QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1749 
1750 	if (txb->m_head) {
1751 		bus_dmamap_sync(ha->tx_tag, txb->map,
1752 			BUS_DMASYNC_POSTWRITE);
1753 
1754 		bus_dmamap_unload(ha->tx_tag, txb->map);
1755 
1756 		m_freem(txb->m_head);
1757 		txb->m_head = NULL;
1758 
1759 		bus_dmamap_destroy(ha->tx_tag, txb->map);
1760 		txb->map = NULL;
1761 	}
1762 
1763 	if (txb->map) {
1764 		bus_dmamap_unload(ha->tx_tag, txb->map);
1765 		bus_dmamap_destroy(ha->tx_tag, txb->map);
1766 		txb->map = NULL;
1767 	}
1768 
1769 	QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1770 }
1771 
1772 static void
1773 qla_free_xmt_bufs(qla_host_t *ha)
1774 {
1775 	int		i, j;
1776 
1777 	for (j = 0; j < ha->hw.num_tx_rings; j++) {
1778 		for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1779 			qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1780 	}
1781 
1782 	if (ha->tx_tag != NULL) {
1783 		bus_dma_tag_destroy(ha->tx_tag);
1784 		ha->tx_tag = NULL;
1785 	}
1786 
1787 	for (i = 0; i < ha->hw.num_tx_rings; i++) {
1788 		bzero((void *)ha->tx_ring[i].tx_buf,
1789 			(sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1790 	}
1791 	return;
1792 }
1793 
1794 static int
1795 qla_alloc_rcv_std(qla_host_t *ha)
1796 {
1797 	int		i, j, k, r, ret = 0;
1798 	qla_rx_buf_t	*rxb;
1799 	qla_rx_ring_t	*rx_ring;
1800 
1801 	for (r = 0; r < ha->hw.num_rds_rings; r++) {
1802 		rx_ring = &ha->rx_ring[r];
1803 
1804 		for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1805 			rxb = &rx_ring->rx_buf[i];
1806 
1807 			ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1808 					&rxb->map);
1809 
1810 			if (ret) {
1811 				device_printf(ha->pci_dev,
1812 					"%s: dmamap[%d, %d] failed\n",
1813 					__func__, r, i);
1814 
1815 				for (k = 0; k < r; k++) {
1816 					for (j = 0; j < NUM_RX_DESCRIPTORS;
1817 						j++) {
1818 						rxb = &ha->rx_ring[k].rx_buf[j];
1819 						bus_dmamap_destroy(ha->rx_tag,
1820 							rxb->map);
1821 					}
1822 				}
1823 
1824 				for (j = 0; j < i; j++) {
1825 					bus_dmamap_destroy(ha->rx_tag,
1826 						rx_ring->rx_buf[j].map);
1827 				}
1828 				goto qla_alloc_rcv_std_err;
1829 			}
1830 		}
1831 	}
1832 
1833 	qla_init_hw_rcv_descriptors(ha);
1834 
1835 	for (r = 0; r < ha->hw.num_rds_rings; r++) {
1836 		rx_ring = &ha->rx_ring[r];
1837 
1838 		for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1839 			rxb = &rx_ring->rx_buf[i];
1840 			rxb->handle = i;
1841 			if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1842 				/*
1843 			 	 * set the physical address in the
1844 				 * corresponding descriptor entry in the
1845 				 * receive ring/queue for the hba
1846 				 */
1847 				qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1848 					rxb->paddr,
1849 					(rxb->m_head)->m_pkthdr.len);
1850 			} else {
1851 				device_printf(ha->pci_dev,
1852 					"%s: ql_get_mbuf [%d, %d] failed\n",
1853 					__func__, r, i);
1854 				bus_dmamap_destroy(ha->rx_tag, rxb->map);
1855 				goto qla_alloc_rcv_std_err;
1856 			}
1857 		}
1858 	}
1859 	return 0;
1860 
1861 qla_alloc_rcv_std_err:
1862 	return (-1);
1863 }
1864 
1865 static void
1866 qla_free_rcv_std(qla_host_t *ha)
1867 {
1868 	int		i, r;
1869 	qla_rx_buf_t	*rxb;
1870 
1871 	for (r = 0; r < ha->hw.num_rds_rings; r++) {
1872 		for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1873 			rxb = &ha->rx_ring[r].rx_buf[i];
1874 			if (rxb->m_head != NULL) {
1875 				bus_dmamap_unload(ha->rx_tag, rxb->map);
1876 				bus_dmamap_destroy(ha->rx_tag, rxb->map);
1877 				m_freem(rxb->m_head);
1878 				rxb->m_head = NULL;
1879 			}
1880 		}
1881 	}
1882 	return;
1883 }
1884 
1885 static int
1886 qla_alloc_rcv_bufs(qla_host_t *ha)
1887 {
1888 	int		i, ret = 0;
1889 
1890 	if (bus_dma_tag_create(NULL,    /* parent */
1891 			1, 0,    /* alignment, bounds */
1892 			BUS_SPACE_MAXADDR,       /* lowaddr */
1893 			BUS_SPACE_MAXADDR,       /* highaddr */
1894 			NULL, NULL,      /* filter, filterarg */
1895 			MJUM9BYTES,     /* maxsize */
1896 			1,        /* nsegments */
1897 			MJUM9BYTES,        /* maxsegsize */
1898 			BUS_DMA_ALLOCNOW,        /* flags */
1899 			NULL,    /* lockfunc */
1900 			NULL,    /* lockfuncarg */
1901 			&ha->rx_tag)) {
1902 		device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1903 			__func__);
1904 
1905 		return (ENOMEM);
1906 	}
1907 
1908 	bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1909 
1910 	for (i = 0; i < ha->hw.num_sds_rings; i++) {
1911 		ha->hw.sds[i].sdsr_next = 0;
1912 		ha->hw.sds[i].rxb_free = NULL;
1913 		ha->hw.sds[i].rx_free = 0;
1914 	}
1915 
1916 	ret = qla_alloc_rcv_std(ha);
1917 
1918 	return (ret);
1919 }
1920 
1921 static void
1922 qla_free_rcv_bufs(qla_host_t *ha)
1923 {
1924 	int		i;
1925 
1926 	qla_free_rcv_std(ha);
1927 
1928 	if (ha->rx_tag != NULL) {
1929 		bus_dma_tag_destroy(ha->rx_tag);
1930 		ha->rx_tag = NULL;
1931 	}
1932 
1933 	bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1934 
1935 	for (i = 0; i < ha->hw.num_sds_rings; i++) {
1936 		ha->hw.sds[i].sdsr_next = 0;
1937 		ha->hw.sds[i].rxb_free = NULL;
1938 		ha->hw.sds[i].rx_free = 0;
1939 	}
1940 
1941 	return;
1942 }
1943 
1944 int
1945 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1946 {
1947 	register struct mbuf *mp = nmp;
1948 	int            		ret = 0;
1949 	uint32_t		offset;
1950 	bus_dma_segment_t	segs[1];
1951 	int			nsegs, mbuf_size;
1952 
1953 	QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1954 
1955         if (ha->hw.enable_9kb)
1956                 mbuf_size = MJUM9BYTES;
1957         else
1958                 mbuf_size = MCLBYTES;
1959 
1960 	if (mp == NULL) {
1961 		if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1962 			return(-1);
1963 
1964                 if (ha->hw.enable_9kb)
1965                         mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
1966                 else
1967                         mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1968 
1969 		if (mp == NULL) {
1970 			ha->err_m_getcl++;
1971 			ret = ENOBUFS;
1972 			device_printf(ha->pci_dev,
1973 					"%s: m_getcl failed\n", __func__);
1974 			goto exit_ql_get_mbuf;
1975 		}
1976 		mp->m_len = mp->m_pkthdr.len = mbuf_size;
1977 	} else {
1978 		mp->m_len = mp->m_pkthdr.len = mbuf_size;
1979 		mp->m_data = mp->m_ext.ext_buf;
1980 		mp->m_next = NULL;
1981 	}
1982 
1983 	offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1984 	if (offset) {
1985 		offset = 8 - offset;
1986 		m_adj(mp, offset);
1987 	}
1988 
1989 	/*
1990 	 * Using memory from the mbuf cluster pool, invoke the bus_dma
1991 	 * machinery to arrange the memory mapping.
1992 	 */
1993 	ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1994 			mp, segs, &nsegs, BUS_DMA_NOWAIT);
1995 	rxb->paddr = segs[0].ds_addr;
1996 
1997 	if (ret || !rxb->paddr || (nsegs != 1)) {
1998 		m_free(mp);
1999 		rxb->m_head = NULL;
2000 		device_printf(ha->pci_dev,
2001 			"%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
2002 			__func__, ret, (long long unsigned int)rxb->paddr,
2003 			nsegs);
2004                 ret = -1;
2005 		goto exit_ql_get_mbuf;
2006 	}
2007 	rxb->m_head = mp;
2008 	bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
2009 
2010 exit_ql_get_mbuf:
2011 	QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
2012 	return (ret);
2013 }
2014 
2015 static void
2016 qla_get_peer(qla_host_t *ha)
2017 {
2018 	device_t *peers;
2019 	int count, i, slot;
2020 	int my_slot = pci_get_slot(ha->pci_dev);
2021 
2022 	if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
2023 		return;
2024 
2025 	for (i = 0; i < count; i++) {
2026 		slot = pci_get_slot(peers[i]);
2027 
2028 		if ((slot >= 0) && (slot == my_slot) &&
2029 			(pci_get_device(peers[i]) ==
2030 				pci_get_device(ha->pci_dev))) {
2031 			if (ha->pci_dev != peers[i])
2032 				ha->peer_dev = peers[i];
2033 		}
2034 	}
2035 }
2036 
2037 static void
2038 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
2039 {
2040 	qla_host_t *ha_peer;
2041 
2042 	if (ha->peer_dev) {
2043         	if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
2044 			ha_peer->msg_from_peer = msg_to_peer;
2045 		}
2046 	}
2047 }
2048 
2049 void
2050 qla_set_error_recovery(qla_host_t *ha)
2051 {
2052 	struct ifnet *ifp = ha->ifp;
2053 
2054 	if (!cold && ha->enable_error_recovery) {
2055 		if (ifp)
2056 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2057 		ha->qla_initiate_recovery = 1;
2058 	} else
2059 		ha->offline = 1;
2060 	return;
2061 }
2062 
2063 static void
2064 qla_error_recovery(void *context, int pending)
2065 {
2066 	qla_host_t *ha = context;
2067 	uint32_t msecs_100 = 400;
2068 	struct ifnet *ifp = ha->ifp;
2069 	int i = 0;
2070 
2071 	device_printf(ha->pci_dev, "%s: enter\n", __func__);
2072 	ha->hw.imd_compl = 1;
2073 
2074 	taskqueue_drain_all(ha->stats_tq);
2075 	taskqueue_drain_all(ha->async_event_tq);
2076 
2077 	if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2078 		return;
2079 
2080 	device_printf(ha->pci_dev, "%s: ts_usecs = %ld start\n",
2081 		__func__, qla_get_usec_timestamp());
2082 
2083 	if (ha->qla_interface_up) {
2084 		qla_mdelay(__func__, 300);
2085 
2086 	        //ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2087 
2088 		for (i = 0; i < ha->hw.num_sds_rings; i++) {
2089 	        	qla_tx_fp_t *fp;
2090 
2091 			fp = &ha->tx_fp[i];
2092 
2093 			if (fp == NULL)
2094 				continue;
2095 
2096 			if (fp->tx_br != NULL) {
2097 				mtx_lock(&fp->tx_mtx);
2098 				mtx_unlock(&fp->tx_mtx);
2099 			}
2100 		}
2101 	}
2102 
2103 	qla_drain_fp_taskqueues(ha);
2104 
2105 	if ((ha->pci_func & 0x1) == 0) {
2106 		if (!ha->msg_from_peer) {
2107 			qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2108 
2109 			while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2110 				msecs_100--)
2111 				qla_mdelay(__func__, 100);
2112 		}
2113 
2114 		ha->msg_from_peer = 0;
2115 
2116 		if (ha->enable_minidump)
2117 			ql_minidump(ha);
2118 
2119 		if (ha->enable_driverstate_dump)
2120 			ql_capture_drvr_state(ha);
2121 
2122 		if (ql_init_hw(ha)) {
2123 			device_printf(ha->pci_dev,
2124 				"%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2125 				__func__, qla_get_usec_timestamp());
2126 			ha->offline = 1;
2127 			goto qla_error_recovery_exit;
2128 		}
2129 
2130 		if (ha->qla_interface_up) {
2131 			qla_free_xmt_bufs(ha);
2132 			qla_free_rcv_bufs(ha);
2133 		}
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 
2138 	} else {
2139 		if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2140 			ha->msg_from_peer = 0;
2141 
2142 			if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2143 				qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2144 		} else {
2145 			qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2146 		}
2147 
2148 		while ((ha->msg_from_peer != QL_PEER_MSG_ACK)  && msecs_100--)
2149 			qla_mdelay(__func__, 100);
2150 		ha->msg_from_peer = 0;
2151 
2152 		if (ha->enable_driverstate_dump)
2153 			ql_capture_drvr_state(ha);
2154 
2155 		if (msecs_100 == 0) {
2156 			device_printf(ha->pci_dev,
2157 				"%s: ts_usecs = %ld exit: QL_PEER_MSG_ACK not received\n",
2158 				__func__, qla_get_usec_timestamp());
2159 			ha->offline = 1;
2160 			goto qla_error_recovery_exit;
2161 		}
2162 
2163 		if (ql_init_hw(ha)) {
2164 			device_printf(ha->pci_dev,
2165 				"%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2166 				__func__, qla_get_usec_timestamp());
2167 			ha->offline = 1;
2168 			goto qla_error_recovery_exit;
2169 		}
2170 
2171 		if (ha->qla_interface_up) {
2172 			qla_free_xmt_bufs(ha);
2173 			qla_free_rcv_bufs(ha);
2174 		}
2175 	}
2176 
2177 	qla_mdelay(__func__, ha->ms_delay_after_init);
2178 
2179 	*((uint32_t *)&ha->hw.flags) = 0;
2180 	ha->qla_initiate_recovery = 0;
2181 
2182 	if (ha->qla_interface_up) {
2183 		if (qla_alloc_xmt_bufs(ha) != 0) {
2184 			ha->offline = 1;
2185 			goto qla_error_recovery_exit;
2186 		}
2187 
2188 		qla_confirm_9kb_enable(ha);
2189 
2190 		if (qla_alloc_rcv_bufs(ha) != 0) {
2191 			ha->offline = 1;
2192 			goto qla_error_recovery_exit;
2193 		}
2194 
2195 		ha->stop_rcv = 0;
2196 
2197 		if (ql_init_hw_if(ha) == 0) {
2198 			ifp = ha->ifp;
2199 			ifp->if_drv_flags |= IFF_DRV_RUNNING;
2200 			ha->qla_watchdog_pause = 0;
2201 			ql_update_link_state(ha);
2202 		} else {
2203 			ha->offline = 1;
2204 
2205 			if (ha->hw.sp_log_stop_events &
2206 				Q8_SP_LOG_STOP_IF_START_FAILURE)
2207 				ha->hw.sp_log_stop = -1;
2208 		}
2209 	} else {
2210 		ha->qla_watchdog_pause = 0;
2211 	}
2212 
2213 qla_error_recovery_exit:
2214 
2215 	if (ha->offline ) {
2216 		device_printf(ha->pci_dev, "%s: ts_usecs = %ld port offline\n",
2217 			__func__, qla_get_usec_timestamp());
2218 		if (ha->hw.sp_log_stop_events &
2219 			Q8_SP_LOG_STOP_ERR_RECOVERY_FAILURE)
2220 			ha->hw.sp_log_stop = -1;
2221 	}
2222 
2223         QLA_UNLOCK(ha, __func__);
2224 
2225 	if (!ha->offline)
2226 		callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2227 			qla_watchdog, ha);
2228 
2229 	device_printf(ha->pci_dev,
2230 		"%s: ts_usecs = %ld exit\n",
2231 		__func__, qla_get_usec_timestamp());
2232 	return;
2233 }
2234 
2235 static void
2236 qla_async_event(void *context, int pending)
2237 {
2238         qla_host_t *ha = context;
2239 
2240 	if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2241 		return;
2242 
2243 	if (ha->async_event) {
2244 		ha->async_event = 0;
2245         	qla_hw_async_event(ha);
2246 	}
2247 
2248 	QLA_UNLOCK(ha, __func__);
2249 
2250 	return;
2251 }
2252 
2253 static void
2254 qla_stats(void *context, int pending)
2255 {
2256         qla_host_t *ha;
2257 
2258         ha = context;
2259 
2260 	ql_get_stats(ha);
2261 
2262 	return;
2263 }
2264