2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013-2016 Qlogic Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
32 * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
42 #include "ql_inline.h"
49 * Some PCI Configuration Space Related Defines
52 #ifndef PCI_VENDOR_QLOGIC
53 #define PCI_VENDOR_QLOGIC 0x1077
56 #ifndef PCI_PRODUCT_QLOGIC_ISP8030
57 #define PCI_PRODUCT_QLOGIC_ISP8030 0x8030
60 #define PCI_QLOGIC_ISP8030 \
61 ((PCI_PRODUCT_QLOGIC_ISP8030 << 16) | PCI_VENDOR_QLOGIC)
66 static int qla_alloc_parent_dma_tag(qla_host_t *ha);
67 static void qla_free_parent_dma_tag(qla_host_t *ha);
68 static int qla_alloc_xmt_bufs(qla_host_t *ha);
69 static void qla_free_xmt_bufs(qla_host_t *ha);
70 static int qla_alloc_rcv_bufs(qla_host_t *ha);
71 static void qla_free_rcv_bufs(qla_host_t *ha);
72 static void qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb);
74 static void qla_init_ifnet(device_t dev, qla_host_t *ha);
75 static int qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS);
76 static void qla_release(qla_host_t *ha);
77 static void qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs,
79 static void qla_stop(qla_host_t *ha);
80 static void qla_get_peer(qla_host_t *ha);
81 static void qla_error_recovery(void *context, int pending);
82 static void qla_async_event(void *context, int pending);
83 static void qla_stats(void *context, int pending);
84 static int qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
88 * Hooks to the Operating Systems
90 static int qla_pci_probe (device_t);
91 static int qla_pci_attach (device_t);
92 static int qla_pci_detach (device_t);
94 static void qla_init(void *arg);
95 static int qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
96 static int qla_media_change(struct ifnet *ifp);
97 static void qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr);
99 static int qla_transmit(struct ifnet *ifp, struct mbuf *mp);
100 static void qla_qflush(struct ifnet *ifp);
101 static int qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *tx_fp);
102 static void qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *tx_fp);
103 static int qla_create_fp_taskqueues(qla_host_t *ha);
104 static void qla_destroy_fp_taskqueues(qla_host_t *ha);
105 static void qla_drain_fp_taskqueues(qla_host_t *ha);
107 static device_method_t qla_pci_methods[] = {
108 /* Device interface */
109 DEVMETHOD(device_probe, qla_pci_probe),
110 DEVMETHOD(device_attach, qla_pci_attach),
111 DEVMETHOD(device_detach, qla_pci_detach),
115 static driver_t qla_pci_driver = {
116 "ql", qla_pci_methods, sizeof (qla_host_t),
119 static devclass_t qla83xx_devclass;
121 DRIVER_MODULE(qla83xx, pci, qla_pci_driver, qla83xx_devclass, 0, 0);
123 MODULE_DEPEND(qla83xx, pci, 1, 1, 1);
124 MODULE_DEPEND(qla83xx, ether, 1, 1, 1);
126 MALLOC_DEFINE(M_QLA83XXBUF, "qla83xxbuf", "Buffers for qla83xx driver");
128 #define QL_STD_REPLENISH_THRES 0
129 #define QL_JUMBO_REPLENISH_THRES 32
132 static char dev_str[64];
133 static char ver_str[64];
136 * Name: qla_pci_probe
137 * Function: Validate the PCI device to be a QLA80XX device
140 qla_pci_probe(device_t dev)
142 switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
143 case PCI_QLOGIC_ISP8030:
144 snprintf(dev_str, sizeof(dev_str), "%s v%d.%d.%d",
145 "Qlogic ISP 83xx PCI CNA Adapter-Ethernet Function",
146 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
148 snprintf(ver_str, sizeof(ver_str), "v%d.%d.%d",
149 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
151 device_set_desc(dev, dev_str);
158 printf("%s: %s\n ", __func__, dev_str);
160 return (BUS_PROBE_DEFAULT);
164 qla_add_sysctls(qla_host_t *ha)
166 device_t dev = ha->pci_dev;
168 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
169 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
170 OID_AUTO, "version", CTLFLAG_RD,
171 ver_str, 0, "Driver Version");
173 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
174 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
175 OID_AUTO, "fw_version", CTLFLAG_RD,
176 ha->fw_ver_str, 0, "firmware version");
178 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
179 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
180 "link_status", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
181 (void *)ha, 0, qla_sysctl_get_link_status, "I", "Link Status");
184 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
185 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
186 OID_AUTO, "debug", CTLFLAG_RW,
187 &ha->dbg_level, ha->dbg_level, "Debug Level");
189 ha->enable_minidump = 1;
190 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
191 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
192 OID_AUTO, "enable_minidump", CTLFLAG_RW,
193 &ha->enable_minidump, ha->enable_minidump,
194 "Minidump retrival prior to error recovery "
195 "is enabled only when this is set");
197 ha->enable_driverstate_dump = 1;
198 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
199 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
200 OID_AUTO, "enable_driverstate_dump", CTLFLAG_RW,
201 &ha->enable_driverstate_dump, ha->enable_driverstate_dump,
202 "Driver State retrival prior to error recovery "
203 "is enabled only when this is set");
205 ha->enable_error_recovery = 1;
206 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
207 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
208 OID_AUTO, "enable_error_recovery", CTLFLAG_RW,
209 &ha->enable_error_recovery, ha->enable_error_recovery,
210 "when set error recovery is enabled on fatal errors "
211 "otherwise the port is turned offline");
213 ha->ms_delay_after_init = 1000;
214 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
215 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
216 OID_AUTO, "ms_delay_after_init", CTLFLAG_RW,
217 &ha->ms_delay_after_init, ha->ms_delay_after_init,
218 "millisecond delay after hw_init");
220 ha->std_replenish = QL_STD_REPLENISH_THRES;
221 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
222 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
223 OID_AUTO, "std_replenish", CTLFLAG_RW,
224 &ha->std_replenish, ha->std_replenish,
225 "Threshold for Replenishing Standard Frames");
227 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
228 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
229 OID_AUTO, "ipv4_lro",
230 CTLFLAG_RD, &ha->ipv4_lro,
231 "number of ipv4 lro completions");
233 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
234 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
235 OID_AUTO, "ipv6_lro",
236 CTLFLAG_RD, &ha->ipv6_lro,
237 "number of ipv6 lro completions");
239 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
240 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
241 OID_AUTO, "tx_tso_frames",
242 CTLFLAG_RD, &ha->tx_tso_frames,
243 "number of Tx TSO Frames");
245 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
246 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
247 OID_AUTO, "hw_vlan_tx_frames",
248 CTLFLAG_RD, &ha->hw_vlan_tx_frames,
249 "number of Tx VLAN Frames");
251 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
252 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
253 OID_AUTO, "hw_lock_failed",
254 CTLFLAG_RD, &ha->hw_lock_failed,
255 "number of hw_lock failures");
261 qla_watchdog(void *arg)
263 qla_host_t *ha = arg;
270 if (ha->qla_watchdog_exit) {
271 ha->qla_watchdog_exited = 1;
274 ha->qla_watchdog_exited = 0;
276 if (!ha->qla_watchdog_pause) {
278 (ql_hw_check_health(ha) || ha->qla_initiate_recovery ||
279 (ha->msg_from_peer == QL_PEER_MSG_RESET))) {
281 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
282 ql_update_link_state(ha);
284 if (ha->enable_error_recovery) {
285 ha->qla_watchdog_paused = 1;
286 ha->qla_watchdog_pause = 1;
288 device_printf(ha->pci_dev,
289 "%s: taskqueue_enqueue(err_task) \n",
291 taskqueue_enqueue(ha->err_tq, &ha->err_task);
294 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
300 if (ha->qla_interface_up) {
302 ha->watchdog_ticks++;
304 if (ha->watchdog_ticks > 1000)
305 ha->watchdog_ticks = 0;
307 if (!ha->watchdog_ticks && QL_RUNNING(ifp)) {
308 taskqueue_enqueue(ha->stats_tq,
312 if (ha->async_event) {
313 taskqueue_enqueue(ha->async_event_tq,
314 &ha->async_event_task);
318 ha->qla_watchdog_paused = 0;
321 ha->qla_watchdog_paused = 1;
324 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
329 * Name: qla_pci_attach
330 * Function: attaches the device to the operating system
333 qla_pci_attach(device_t dev)
335 qla_host_t *ha = NULL;
338 uint32_t num_rcvq = 0;
340 if ((ha = device_get_softc(dev)) == NULL) {
341 device_printf(dev, "cannot get softc\n");
345 memset(ha, 0, sizeof (qla_host_t));
347 if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8030) {
348 device_printf(dev, "device is not ISP8030\n");
352 ha->pci_func = pci_get_function(dev) & 0x1;
356 pci_enable_busmaster(dev);
358 ha->reg_rid = PCIR_BAR(0);
359 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
362 if (ha->pci_reg == NULL) {
363 device_printf(dev, "unable to map any ports\n");
364 goto qla_pci_attach_err;
367 rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
370 mtx_init(&ha->hw_lock, "qla83xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
371 mtx_init(&ha->sp_log_lock, "qla83xx_sp_log_lock", MTX_NETWORK_LOCK, MTX_DEF);
372 ha->flags.lock_init = 1;
376 ha->hw.num_sds_rings = MAX_SDS_RINGS;
377 ha->hw.num_rds_rings = MAX_RDS_RINGS;
378 ha->hw.num_tx_rings = NUM_TX_RINGS;
380 ha->reg_rid1 = PCIR_BAR(2);
381 ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
382 &ha->reg_rid1, RF_ACTIVE);
384 ha->msix_count = pci_msix_count(dev);
386 if (ha->msix_count < 1 ) {
387 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
389 goto qla_pci_attach_err;
392 if (ha->msix_count < (ha->hw.num_sds_rings + 1)) {
393 ha->hw.num_sds_rings = ha->msix_count - 1;
396 QL_DPRINT2(ha, (dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
397 " msix_count 0x%x pci_reg %p pci_reg1 %p\n", __func__, ha,
398 ha->pci_func, rsrc_len, ha->msix_count, ha->pci_reg,
401 /* initialize hardware */
402 if (ql_init_hw(ha)) {
403 device_printf(dev, "%s: ql_init_hw failed\n", __func__);
404 goto qla_pci_attach_err;
407 device_printf(dev, "%s: firmware[%d.%d.%d.%d]\n", __func__,
408 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
410 snprintf(ha->fw_ver_str, sizeof(ha->fw_ver_str), "%d.%d.%d.%d",
411 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
414 if (qla_get_nic_partition(ha, NULL, &num_rcvq)) {
415 device_printf(dev, "%s: qla_get_nic_partition failed\n",
417 goto qla_pci_attach_err;
419 device_printf(dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
420 " msix_count 0x%x pci_reg %p pci_reg1 %p num_rcvq = %d\n",
421 __func__, ha, ha->pci_func, rsrc_len, ha->msix_count,
422 ha->pci_reg, ha->pci_reg1, num_rcvq);
424 if ((ha->msix_count < 64) || (num_rcvq != 32)) {
425 if (ha->hw.num_sds_rings > 15) {
426 ha->hw.num_sds_rings = 15;
430 ha->hw.num_rds_rings = ha->hw.num_sds_rings;
431 ha->hw.num_tx_rings = ha->hw.num_sds_rings;
433 #ifdef QL_ENABLE_ISCSI_TLV
434 ha->hw.num_tx_rings = ha->hw.num_sds_rings * 2;
435 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
437 ql_hw_add_sysctls(ha);
439 ha->msix_count = ha->hw.num_sds_rings + 1;
441 if (pci_alloc_msix(dev, &ha->msix_count)) {
442 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
445 goto qla_pci_attach_err;
449 ha->mbx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
451 (RF_ACTIVE | RF_SHAREABLE));
452 if (ha->mbx_irq == NULL) {
453 device_printf(dev, "could not allocate mbx interrupt\n");
454 goto qla_pci_attach_err;
456 if (bus_setup_intr(dev, ha->mbx_irq, (INTR_TYPE_NET | INTR_MPSAFE),
457 NULL, ql_mbx_isr, ha, &ha->mbx_handle)) {
458 device_printf(dev, "could not setup mbx interrupt\n");
459 goto qla_pci_attach_err;
462 for (i = 0; i < ha->hw.num_sds_rings; i++) {
463 ha->irq_vec[i].sds_idx = i;
464 ha->irq_vec[i].ha = ha;
465 ha->irq_vec[i].irq_rid = 2 + i;
467 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
468 &ha->irq_vec[i].irq_rid,
469 (RF_ACTIVE | RF_SHAREABLE));
471 if (ha->irq_vec[i].irq == NULL) {
472 device_printf(dev, "could not allocate interrupt\n");
473 goto qla_pci_attach_err;
475 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
476 (INTR_TYPE_NET | INTR_MPSAFE),
477 NULL, ql_isr, &ha->irq_vec[i],
478 &ha->irq_vec[i].handle)) {
479 device_printf(dev, "could not setup interrupt\n");
480 goto qla_pci_attach_err;
483 ha->tx_fp[i].ha = ha;
484 ha->tx_fp[i].txr_idx = i;
486 if (qla_alloc_tx_br(ha, &ha->tx_fp[i])) {
487 device_printf(dev, "%s: could not allocate tx_br[%d]\n",
489 goto qla_pci_attach_err;
493 if (qla_create_fp_taskqueues(ha) != 0)
494 goto qla_pci_attach_err;
496 printf("%s: mp__ncpus %d sds %d rds %d msi-x %d\n", __func__, mp_ncpus,
497 ha->hw.num_sds_rings, ha->hw.num_rds_rings, ha->msix_count);
499 ql_read_mac_addr(ha);
501 /* allocate parent dma tag */
502 if (qla_alloc_parent_dma_tag(ha)) {
503 device_printf(dev, "%s: qla_alloc_parent_dma_tag failed\n",
505 goto qla_pci_attach_err;
508 /* alloc all dma buffers */
509 if (ql_alloc_dma(ha)) {
510 device_printf(dev, "%s: ql_alloc_dma failed\n", __func__);
511 goto qla_pci_attach_err;
515 if (ql_minidump_init(ha) != 0) {
516 device_printf(dev, "%s: ql_minidump_init failed\n", __func__);
517 goto qla_pci_attach_err;
519 ql_alloc_drvr_state_buffer(ha);
520 ql_alloc_sp_log_buffer(ha);
521 /* create the o.s ethernet interface */
522 qla_init_ifnet(dev, ha);
524 ha->flags.qla_watchdog_active = 1;
525 ha->qla_watchdog_pause = 0;
527 callout_init(&ha->tx_callout, TRUE);
528 ha->flags.qla_callout_init = 1;
530 /* create ioctl device interface */
531 if (ql_make_cdev(ha)) {
532 device_printf(dev, "%s: ql_make_cdev failed\n", __func__);
533 goto qla_pci_attach_err;
536 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
539 TASK_INIT(&ha->err_task, 0, qla_error_recovery, ha);
540 ha->err_tq = taskqueue_create("qla_errq", M_NOWAIT,
541 taskqueue_thread_enqueue, &ha->err_tq);
542 taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
543 device_get_nameunit(ha->pci_dev));
545 TASK_INIT(&ha->async_event_task, 0, qla_async_event, ha);
546 ha->async_event_tq = taskqueue_create("qla_asyncq", M_NOWAIT,
547 taskqueue_thread_enqueue, &ha->async_event_tq);
548 taskqueue_start_threads(&ha->async_event_tq, 1, PI_NET, "%s asyncq",
549 device_get_nameunit(ha->pci_dev));
551 TASK_INIT(&ha->stats_task, 0, qla_stats, ha);
552 ha->stats_tq = taskqueue_create("qla_statsq", M_NOWAIT,
553 taskqueue_thread_enqueue, &ha->stats_tq);
554 taskqueue_start_threads(&ha->stats_tq, 1, PI_NET, "%s taskq",
555 device_get_nameunit(ha->pci_dev));
557 QL_DPRINT2(ha, (dev, "%s: exit 0\n", __func__));
564 if (ha->flags.lock_init) {
565 mtx_destroy(&ha->hw_lock);
566 mtx_destroy(&ha->sp_log_lock);
569 QL_DPRINT2(ha, (dev, "%s: exit ENXIO\n", __func__));
574 * Name: qla_pci_detach
575 * Function: Unhooks the device from the operating system
578 qla_pci_detach(device_t dev)
580 qla_host_t *ha = NULL;
584 if ((ha = device_get_softc(dev)) == NULL) {
585 device_printf(dev, "cannot get softc\n");
589 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
593 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
594 QLA_LOCK(ha, __func__, -1, 0);
596 ha->qla_detach_active = 1;
601 QLA_UNLOCK(ha, __func__);
603 if (ha->flags.lock_init) {
604 mtx_destroy(&ha->hw_lock);
605 mtx_destroy(&ha->sp_log_lock);
608 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
614 * SYSCTL Related Callbacks
617 qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS)
622 err = sysctl_handle_int(oidp, &ret, 0, req);
624 if (err || !req->newptr)
628 ha = (qla_host_t *)arg1;
629 ql_hw_link_status(ha);
636 * Function: Releases the resources allocated for the device
639 qla_release(qla_host_t *ha)
646 if (ha->async_event_tq) {
647 taskqueue_drain_all(ha->async_event_tq);
648 taskqueue_free(ha->async_event_tq);
652 taskqueue_drain_all(ha->err_tq);
653 taskqueue_free(ha->err_tq);
657 taskqueue_drain_all(ha->stats_tq);
658 taskqueue_free(ha->stats_tq);
663 if (ha->flags.qla_watchdog_active) {
664 ha->qla_watchdog_exit = 1;
666 while (ha->qla_watchdog_exited == 0)
667 qla_mdelay(__func__, 1);
670 if (ha->flags.qla_callout_init)
671 callout_stop(&ha->tx_callout);
674 ether_ifdetach(ha->ifp);
676 ql_free_drvr_state_buffer(ha);
677 ql_free_sp_log_buffer(ha);
679 qla_free_parent_dma_tag(ha);
682 (void)bus_teardown_intr(dev, ha->mbx_irq, ha->mbx_handle);
685 (void) bus_release_resource(dev, SYS_RES_IRQ, ha->mbx_irq_rid,
688 for (i = 0; i < ha->hw.num_sds_rings; i++) {
690 if (ha->irq_vec[i].handle) {
691 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
692 ha->irq_vec[i].handle);
695 if (ha->irq_vec[i].irq) {
696 (void)bus_release_resource(dev, SYS_RES_IRQ,
697 ha->irq_vec[i].irq_rid,
701 qla_free_tx_br(ha, &ha->tx_fp[i]);
703 qla_destroy_fp_taskqueues(ha);
706 pci_release_msi(dev);
709 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
713 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
720 * DMA Related Functions
724 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
726 *((bus_addr_t *)arg) = 0;
729 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
733 *((bus_addr_t *)arg) = segs[0].ds_addr;
739 ql_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
747 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
749 ret = bus_dma_tag_create(
750 ha->parent_tag,/* parent */
752 ((bus_size_t)(1ULL << 32)),/* boundary */
753 BUS_SPACE_MAXADDR, /* lowaddr */
754 BUS_SPACE_MAXADDR, /* highaddr */
755 NULL, NULL, /* filter, filterarg */
756 dma_buf->size, /* maxsize */
758 dma_buf->size, /* maxsegsize */
760 NULL, NULL, /* lockfunc, lockarg */
764 device_printf(dev, "%s: could not create dma tag\n", __func__);
765 goto ql_alloc_dmabuf_exit;
767 ret = bus_dmamem_alloc(dma_buf->dma_tag,
768 (void **)&dma_buf->dma_b,
769 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
772 bus_dma_tag_destroy(dma_buf->dma_tag);
773 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
774 goto ql_alloc_dmabuf_exit;
777 ret = bus_dmamap_load(dma_buf->dma_tag,
782 &b_addr, BUS_DMA_NOWAIT);
784 if (ret || !b_addr) {
785 bus_dma_tag_destroy(dma_buf->dma_tag);
786 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
789 goto ql_alloc_dmabuf_exit;
792 dma_buf->dma_addr = b_addr;
794 ql_alloc_dmabuf_exit:
795 QL_DPRINT2(ha, (dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
796 __func__, ret, (void *)dma_buf->dma_tag,
797 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
804 ql_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
806 bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
807 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
808 bus_dma_tag_destroy(dma_buf->dma_tag);
812 qla_alloc_parent_dma_tag(qla_host_t *ha)
820 * Allocate parent DMA Tag
822 ret = bus_dma_tag_create(
823 bus_get_dma_tag(dev), /* parent */
824 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
825 BUS_SPACE_MAXADDR, /* lowaddr */
826 BUS_SPACE_MAXADDR, /* highaddr */
827 NULL, NULL, /* filter, filterarg */
828 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
830 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
832 NULL, NULL, /* lockfunc, lockarg */
836 device_printf(dev, "%s: could not create parent dma tag\n",
841 ha->flags.parent_tag = 1;
847 qla_free_parent_dma_tag(qla_host_t *ha)
849 if (ha->flags.parent_tag) {
850 bus_dma_tag_destroy(ha->parent_tag);
851 ha->flags.parent_tag = 0;
856 * Name: qla_init_ifnet
857 * Function: Creates the Network Device Interface and Registers it with the O.S
861 qla_init_ifnet(device_t dev, qla_host_t *ha)
865 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
867 ifp = ha->ifp = if_alloc(IFT_ETHER);
870 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
872 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
874 ifp->if_baudrate = IF_Gbps(10);
875 ifp->if_capabilities = IFCAP_LINKSTATE;
876 ifp->if_mtu = ETHERMTU;
878 ifp->if_init = qla_init;
880 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
881 ifp->if_ioctl = qla_ioctl;
883 ifp->if_transmit = qla_transmit;
884 ifp->if_qflush = qla_qflush;
886 IFQ_SET_MAXLEN(&ifp->if_snd, qla_get_ifq_snd_maxlen(ha));
887 ifp->if_snd.ifq_drv_maxlen = qla_get_ifq_snd_maxlen(ha);
888 IFQ_SET_READY(&ifp->if_snd);
890 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
892 ether_ifattach(ifp, qla_get_mac_addr(ha));
894 ifp->if_capabilities |= IFCAP_HWCSUM |
898 IFCAP_VLAN_HWTAGGING |
903 ifp->if_capenable = ifp->if_capabilities;
905 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
907 ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
909 ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
911 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
913 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
915 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
921 qla_init_locked(qla_host_t *ha)
923 struct ifnet *ifp = ha->ifp;
925 ql_sp_log(ha, 14, 0, 0, 0, 0, 0, 0);
929 if (qla_alloc_xmt_bufs(ha) != 0)
932 qla_confirm_9kb_enable(ha);
934 if (qla_alloc_rcv_bufs(ha) != 0)
937 bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
939 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
940 ifp->if_hwassist |= CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
943 if (ql_init_hw_if(ha) == 0) {
945 ifp->if_drv_flags |= IFF_DRV_RUNNING;
946 ha->hw_vlan_tx_frames = 0;
947 ha->tx_tso_frames = 0;
948 ha->qla_interface_up = 1;
949 ql_update_link_state(ha);
951 if (ha->hw.sp_log_stop_events & Q8_SP_LOG_STOP_IF_START_FAILURE)
952 ha->hw.sp_log_stop = -1;
955 ha->qla_watchdog_pause = 0;
965 ha = (qla_host_t *)arg;
967 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
969 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
974 QLA_UNLOCK(ha, __func__);
976 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
980 qla_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int mcnt)
984 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
987 bcopy(LLADDR(sdl), &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
993 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
995 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
997 struct ifnet *ifp = ha->ifp;
1000 mcnt = if_foreach_llmaddr(ifp, qla_copy_maddr, mta);
1002 if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1003 QLA_LOCK_NO_SLEEP) != 0)
1006 ql_sp_log(ha, 12, 4, ifp->if_drv_flags,
1007 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1008 add_multi, (uint32_t)mcnt, 0);
1010 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1013 ret = qla_hw_del_all_mcast(ha);
1016 device_printf(ha->pci_dev,
1017 "%s: qla_hw_del_all_mcast() failed\n",
1022 ret = ql_hw_set_multi(ha, mta, mcnt, 1);
1026 QLA_UNLOCK(ha, __func__);
1032 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1035 struct ifreq *ifr = (struct ifreq *)data;
1036 struct ifaddr *ifa = (struct ifaddr *)data;
1039 ha = (qla_host_t *)ifp->if_softc;
1040 if (ha->offline || ha->qla_initiate_recovery)
1045 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
1048 if (ifa->ifa_addr->sa_family == AF_INET) {
1050 ret = QLA_LOCK(ha, __func__,
1051 QLA_LOCK_DEFAULT_MS_TIMEOUT,
1056 ifp->if_flags |= IFF_UP;
1058 ql_sp_log(ha, 8, 3, ifp->if_drv_flags,
1059 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1060 ntohl(IA_SIN(ifa)->sin_addr.s_addr), 0, 0);
1062 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1063 qla_init_locked(ha);
1066 QLA_UNLOCK(ha, __func__);
1067 QL_DPRINT4(ha, (ha->pci_dev,
1068 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1070 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1072 arp_ifinit(ifp, ifa);
1074 ether_ioctl(ifp, cmd, data);
1079 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1082 if (ifr->ifr_mtu > QLA_MAX_MTU) {
1085 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1091 ifp->if_mtu = ifr->ifr_mtu;
1092 ha->max_frame_size =
1093 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1095 ql_sp_log(ha, 9, 4, ifp->if_drv_flags,
1096 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1097 ha->max_frame_size, ifp->if_mtu, 0);
1099 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1100 qla_init_locked(ha);
1103 if (ifp->if_mtu > ETHERMTU)
1104 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1106 ha->std_replenish = QL_STD_REPLENISH_THRES;
1109 QLA_UNLOCK(ha, __func__);
1115 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1118 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1124 ql_sp_log(ha, 10, 4, ifp->if_drv_flags,
1125 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1126 ha->if_flags, ifp->if_flags, 0);
1128 if (ifp->if_flags & IFF_UP) {
1130 ha->max_frame_size = ifp->if_mtu +
1131 ETHER_HDR_LEN + ETHER_CRC_LEN;
1132 qla_init_locked(ha);
1134 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1135 if ((ifp->if_flags ^ ha->if_flags) &
1137 ret = ql_set_promisc(ha);
1138 } else if ((ifp->if_flags ^ ha->if_flags) &
1140 ret = ql_set_allmulti(ha);
1144 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1146 ha->if_flags = ifp->if_flags;
1149 QLA_UNLOCK(ha, __func__);
1153 QL_DPRINT4(ha, (ha->pci_dev,
1154 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1156 if (qla_set_multi(ha, 1))
1161 QL_DPRINT4(ha, (ha->pci_dev,
1162 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1164 if (qla_set_multi(ha, 0))
1170 QL_DPRINT4(ha, (ha->pci_dev,
1171 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1173 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1178 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1180 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1183 if (mask & IFCAP_HWCSUM)
1184 ifp->if_capenable ^= IFCAP_HWCSUM;
1185 if (mask & IFCAP_TSO4)
1186 ifp->if_capenable ^= IFCAP_TSO4;
1187 if (mask & IFCAP_TSO6)
1188 ifp->if_capenable ^= IFCAP_TSO6;
1189 if (mask & IFCAP_VLAN_HWTAGGING)
1190 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1191 if (mask & IFCAP_VLAN_HWTSO)
1192 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1193 if (mask & IFCAP_LRO)
1194 ifp->if_capenable ^= IFCAP_LRO;
1196 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1197 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1203 ql_sp_log(ha, 11, 4, ifp->if_drv_flags,
1204 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1205 mask, ifp->if_capenable, 0);
1207 qla_init_locked(ha);
1209 QLA_UNLOCK(ha, __func__);
1212 VLAN_CAPABILITIES(ifp);
1217 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1219 ret = ether_ioctl(ifp, cmd, data);
1227 qla_media_change(struct ifnet *ifp)
1230 struct ifmedia *ifm;
1233 ha = (qla_host_t *)ifp->if_softc;
1235 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1239 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1242 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1248 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1252 ha = (qla_host_t *)ifp->if_softc;
1254 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1256 ifmr->ifm_status = IFM_AVALID;
1257 ifmr->ifm_active = IFM_ETHER;
1259 ql_update_link_state(ha);
1260 if (ha->hw.link_up) {
1261 ifmr->ifm_status |= IFM_ACTIVE;
1262 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1265 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1266 (ha->hw.link_up ? "link_up" : "link_down")));
1273 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1276 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1281 struct mbuf *m_head = *m_headp;
1283 QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1285 tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1287 if ((NULL != ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head) ||
1288 (QL_ERR_INJECT(ha, INJCT_TXBUF_MBUF_NON_NULL))){
1289 QL_ASSERT(ha, 0, ("%s [%d]: txr_idx = %d tx_idx = %d "\
1290 "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,\
1291 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head));
1293 device_printf(ha->pci_dev, "%s [%d]: txr_idx = %d tx_idx = %d "
1294 "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,
1295 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head);
1300 QL_INITIATE_RECOVERY(ha);
1304 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1306 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1313 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1314 m_head->m_pkthdr.len));
1316 m = m_defrag(m_head, M_NOWAIT);
1318 ha->err_tx_defrag++;
1321 device_printf(ha->pci_dev,
1322 "%s: m_defrag() = NULL [%d]\n",
1329 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1330 segs, &nsegs, BUS_DMA_NOWAIT))) {
1332 ha->err_tx_dmamap_load++;
1334 device_printf(ha->pci_dev,
1335 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1336 __func__, ret, m_head->m_pkthdr.len);
1338 if (ret != ENOMEM) {
1347 ha->err_tx_dmamap_load++;
1349 device_printf(ha->pci_dev,
1350 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1351 __func__, ret, m_head->m_pkthdr.len);
1353 if (ret != ENOMEM) {
1360 QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1362 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1364 if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1366 ha->tx_ring[txr_idx].count++;
1368 ha->tx_ring[txr_idx].iscsi_pkt_count++;
1369 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1371 bus_dmamap_unload(ha->tx_tag, map);
1372 if (ret == EINVAL) {
1379 QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1384 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1386 snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1387 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1389 mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1391 fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1392 M_NOWAIT, &fp->tx_mtx);
1393 if (fp->tx_br == NULL) {
1394 QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1395 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1402 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1405 struct ifnet *ifp = ha->ifp;
1407 if (mtx_initialized(&fp->tx_mtx)) {
1409 if (fp->tx_br != NULL) {
1411 mtx_lock(&fp->tx_mtx);
1413 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1417 mtx_unlock(&fp->tx_mtx);
1419 buf_ring_free(fp->tx_br, M_DEVBUF);
1422 mtx_destroy(&fp->tx_mtx);
1428 qla_fp_taskqueue(void *context, int pending)
1433 struct mbuf *mp = NULL;
1436 uint32_t iscsi_pdu = 0;
1437 uint32_t rx_pkts_left = -1;
1444 ha = (qla_host_t *)fp->ha;
1448 txr_idx = fp->txr_idx;
1450 mtx_lock(&fp->tx_mtx);
1452 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1453 mtx_unlock(&fp->tx_mtx);
1454 goto qla_fp_taskqueue_exit;
1457 while (rx_pkts_left && !ha->stop_rcv &&
1458 (ifp->if_drv_flags & IFF_DRV_RUNNING) && ha->hw.link_up) {
1459 rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1461 #ifdef QL_ENABLE_ISCSI_TLV
1462 ql_hw_tx_done_locked(ha, fp->txr_idx);
1463 ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1465 ql_hw_tx_done_locked(ha, fp->txr_idx);
1466 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1468 mp = drbr_peek(ifp, fp->tx_br);
1470 while (mp != NULL) {
1472 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1473 #ifdef QL_ENABLE_ISCSI_TLV
1474 if (ql_iscsi_pdu(ha, mp) == 0) {
1476 (ha->hw.num_tx_rings >> 1);
1480 txr_idx = fp->txr_idx;
1482 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1485 ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1489 drbr_putback(ifp, fp->tx_br, mp);
1491 drbr_advance(ifp, fp->tx_br);
1494 mtx_unlock(&fp->tx_mtx);
1496 goto qla_fp_taskqueue_exit0;
1498 drbr_advance(ifp, fp->tx_br);
1501 /* Send a copy of the frame to the BPF listener */
1502 ETHER_BPF_MTAP(ifp, mp);
1504 if (((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) ||
1508 mp = drbr_peek(ifp, fp->tx_br);
1511 mtx_unlock(&fp->tx_mtx);
1513 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1514 goto qla_fp_taskqueue_exit;
1516 qla_fp_taskqueue_exit0:
1518 if (rx_pkts_left || ((mp != NULL) && ret)) {
1519 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1521 if (!ha->stop_rcv) {
1522 QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1526 qla_fp_taskqueue_exit:
1528 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1533 qla_create_fp_taskqueues(qla_host_t *ha)
1536 uint8_t tq_name[32];
1538 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1540 qla_tx_fp_t *fp = &ha->tx_fp[i];
1542 bzero(tq_name, sizeof (tq_name));
1543 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1545 NET_TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1547 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1548 taskqueue_thread_enqueue,
1551 if (fp->fp_taskqueue == NULL)
1554 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1557 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1565 qla_destroy_fp_taskqueues(qla_host_t *ha)
1569 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1571 qla_tx_fp_t *fp = &ha->tx_fp[i];
1573 if (fp->fp_taskqueue != NULL) {
1574 taskqueue_drain_all(fp->fp_taskqueue);
1575 taskqueue_free(fp->fp_taskqueue);
1576 fp->fp_taskqueue = NULL;
1583 qla_drain_fp_taskqueues(qla_host_t *ha)
1587 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1588 qla_tx_fp_t *fp = &ha->tx_fp[i];
1590 if (fp->fp_taskqueue != NULL) {
1591 taskqueue_drain_all(fp->fp_taskqueue);
1598 qla_transmit(struct ifnet *ifp, struct mbuf *mp)
1600 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1605 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1607 #if __FreeBSD_version >= 1100000
1608 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1610 if (mp->m_flags & M_FLOWID)
1612 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1613 ha->hw.num_sds_rings;
1614 fp = &ha->tx_fp[rss_id];
1616 if (fp->tx_br == NULL) {
1618 goto qla_transmit_exit;
1622 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1625 if (fp->fp_taskqueue != NULL)
1626 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1632 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1637 qla_qflush(struct ifnet *ifp)
1644 ha = (qla_host_t *)ifp->if_softc;
1646 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1648 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1656 mtx_lock(&fp->tx_mtx);
1658 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1661 mtx_unlock(&fp->tx_mtx);
1664 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1670 qla_stop(qla_host_t *ha)
1672 struct ifnet *ifp = ha->ifp;
1676 ql_sp_log(ha, 13, 0, 0, 0, 0, 0, 0);
1680 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1681 ha->qla_watchdog_pause = 1;
1683 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1691 if (fp->tx_br != NULL) {
1692 mtx_lock(&fp->tx_mtx);
1693 mtx_unlock(&fp->tx_mtx);
1697 while (!ha->qla_watchdog_paused)
1698 qla_mdelay(__func__, 1);
1700 ha->qla_interface_up = 0;
1702 qla_drain_fp_taskqueues(ha);
1706 qla_free_xmt_bufs(ha);
1707 qla_free_rcv_bufs(ha);
1713 * Buffer Management Functions for Transmit and Receive Rings
1716 qla_alloc_xmt_bufs(qla_host_t *ha)
1722 if (bus_dma_tag_create(NULL, /* parent */
1723 1, 0, /* alignment, bounds */
1724 BUS_SPACE_MAXADDR, /* lowaddr */
1725 BUS_SPACE_MAXADDR, /* highaddr */
1726 NULL, NULL, /* filter, filterarg */
1727 QLA_MAX_TSO_FRAME_SIZE, /* maxsize */
1728 QLA_MAX_SEGMENTS, /* nsegments */
1729 PAGE_SIZE, /* maxsegsize */
1730 BUS_DMA_ALLOCNOW, /* flags */
1731 NULL, /* lockfunc */
1732 NULL, /* lockfuncarg */
1734 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1739 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1740 bzero((void *)ha->tx_ring[i].tx_buf,
1741 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1744 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1745 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1747 txb = &ha->tx_ring[j].tx_buf[i];
1749 if ((ret = bus_dmamap_create(ha->tx_tag,
1750 BUS_DMA_NOWAIT, &txb->map))) {
1752 ha->err_tx_dmamap_create++;
1753 device_printf(ha->pci_dev,
1754 "%s: bus_dmamap_create failed[%d]\n",
1757 qla_free_xmt_bufs(ha);
1768 * Release mbuf after it sent on the wire
1771 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1773 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1776 bus_dmamap_sync(ha->tx_tag, txb->map,
1777 BUS_DMASYNC_POSTWRITE);
1779 bus_dmamap_unload(ha->tx_tag, txb->map);
1781 m_freem(txb->m_head);
1784 bus_dmamap_destroy(ha->tx_tag, txb->map);
1789 bus_dmamap_unload(ha->tx_tag, txb->map);
1790 bus_dmamap_destroy(ha->tx_tag, txb->map);
1794 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1798 qla_free_xmt_bufs(qla_host_t *ha)
1802 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1803 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1804 qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1807 if (ha->tx_tag != NULL) {
1808 bus_dma_tag_destroy(ha->tx_tag);
1812 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1813 bzero((void *)ha->tx_ring[i].tx_buf,
1814 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1821 qla_alloc_rcv_std(qla_host_t *ha)
1823 int i, j, k, r, ret = 0;
1825 qla_rx_ring_t *rx_ring;
1827 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1829 rx_ring = &ha->rx_ring[r];
1831 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1833 rxb = &rx_ring->rx_buf[i];
1835 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1839 device_printf(ha->pci_dev,
1840 "%s: dmamap[%d, %d] failed\n",
1843 for (k = 0; k < r; k++) {
1844 for (j = 0; j < NUM_RX_DESCRIPTORS;
1846 rxb = &ha->rx_ring[k].rx_buf[j];
1847 bus_dmamap_destroy(ha->rx_tag,
1852 for (j = 0; j < i; j++) {
1853 bus_dmamap_destroy(ha->rx_tag,
1854 rx_ring->rx_buf[j].map);
1856 goto qla_alloc_rcv_std_err;
1861 qla_init_hw_rcv_descriptors(ha);
1864 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1866 rx_ring = &ha->rx_ring[r];
1868 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1869 rxb = &rx_ring->rx_buf[i];
1871 if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1873 * set the physical address in the
1874 * corresponding descriptor entry in the
1875 * receive ring/queue for the hba
1877 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1879 (rxb->m_head)->m_pkthdr.len);
1881 device_printf(ha->pci_dev,
1882 "%s: ql_get_mbuf [%d, %d] failed\n",
1884 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1885 goto qla_alloc_rcv_std_err;
1891 qla_alloc_rcv_std_err:
1896 qla_free_rcv_std(qla_host_t *ha)
1901 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1902 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1903 rxb = &ha->rx_ring[r].rx_buf[i];
1904 if (rxb->m_head != NULL) {
1905 bus_dmamap_unload(ha->rx_tag, rxb->map);
1906 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1907 m_freem(rxb->m_head);
1916 qla_alloc_rcv_bufs(qla_host_t *ha)
1920 if (bus_dma_tag_create(NULL, /* parent */
1921 1, 0, /* alignment, bounds */
1922 BUS_SPACE_MAXADDR, /* lowaddr */
1923 BUS_SPACE_MAXADDR, /* highaddr */
1924 NULL, NULL, /* filter, filterarg */
1925 MJUM9BYTES, /* maxsize */
1927 MJUM9BYTES, /* maxsegsize */
1928 BUS_DMA_ALLOCNOW, /* flags */
1929 NULL, /* lockfunc */
1930 NULL, /* lockfuncarg */
1933 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1939 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1941 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1942 ha->hw.sds[i].sdsr_next = 0;
1943 ha->hw.sds[i].rxb_free = NULL;
1944 ha->hw.sds[i].rx_free = 0;
1947 ret = qla_alloc_rcv_std(ha);
1953 qla_free_rcv_bufs(qla_host_t *ha)
1957 qla_free_rcv_std(ha);
1959 if (ha->rx_tag != NULL) {
1960 bus_dma_tag_destroy(ha->rx_tag);
1964 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1966 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1967 ha->hw.sds[i].sdsr_next = 0;
1968 ha->hw.sds[i].rxb_free = NULL;
1969 ha->hw.sds[i].rx_free = 0;
1976 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1978 register struct mbuf *mp = nmp;
1982 bus_dma_segment_t segs[1];
1983 int nsegs, mbuf_size;
1985 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1989 if (ha->hw.enable_9kb)
1990 mbuf_size = MJUM9BYTES;
1992 mbuf_size = MCLBYTES;
1996 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1999 if (ha->hw.enable_9kb)
2000 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
2002 mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2007 device_printf(ha->pci_dev,
2008 "%s: m_getcl failed\n", __func__);
2009 goto exit_ql_get_mbuf;
2011 mp->m_len = mp->m_pkthdr.len = mbuf_size;
2013 mp->m_len = mp->m_pkthdr.len = mbuf_size;
2014 mp->m_data = mp->m_ext.ext_buf;
2018 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
2020 offset = 8 - offset;
2025 * Using memory from the mbuf cluster pool, invoke the bus_dma
2026 * machinery to arrange the memory mapping.
2028 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
2029 mp, segs, &nsegs, BUS_DMA_NOWAIT);
2030 rxb->paddr = segs[0].ds_addr;
2032 if (ret || !rxb->paddr || (nsegs != 1)) {
2035 device_printf(ha->pci_dev,
2036 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
2037 __func__, ret, (long long unsigned int)rxb->paddr,
2040 goto exit_ql_get_mbuf;
2043 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
2046 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
2052 qla_get_peer(qla_host_t *ha)
2056 int my_slot = pci_get_slot(ha->pci_dev);
2058 if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
2061 for (i = 0; i < count; i++) {
2062 slot = pci_get_slot(peers[i]);
2064 if ((slot >= 0) && (slot == my_slot) &&
2065 (pci_get_device(peers[i]) ==
2066 pci_get_device(ha->pci_dev))) {
2067 if (ha->pci_dev != peers[i])
2068 ha->peer_dev = peers[i];
2074 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
2076 qla_host_t *ha_peer;
2079 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
2081 ha_peer->msg_from_peer = msg_to_peer;
2087 qla_set_error_recovery(qla_host_t *ha)
2089 struct ifnet *ifp = ha->ifp;
2091 if (!cold && ha->enable_error_recovery) {
2093 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2094 ha->qla_initiate_recovery = 1;
2101 qla_error_recovery(void *context, int pending)
2103 qla_host_t *ha = context;
2104 uint32_t msecs_100 = 400;
2105 struct ifnet *ifp = ha->ifp;
2108 device_printf(ha->pci_dev, "%s: enter\n", __func__);
2109 ha->hw.imd_compl = 1;
2111 taskqueue_drain_all(ha->stats_tq);
2112 taskqueue_drain_all(ha->async_event_tq);
2114 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2117 device_printf(ha->pci_dev, "%s: ts_usecs = %ld start\n",
2118 __func__, qla_get_usec_timestamp());
2120 if (ha->qla_interface_up) {
2122 qla_mdelay(__func__, 300);
2124 //ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2126 for (i = 0; i < ha->hw.num_sds_rings; i++) {
2134 if (fp->tx_br != NULL) {
2135 mtx_lock(&fp->tx_mtx);
2136 mtx_unlock(&fp->tx_mtx);
2141 qla_drain_fp_taskqueues(ha);
2143 if ((ha->pci_func & 0x1) == 0) {
2145 if (!ha->msg_from_peer) {
2146 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2148 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2150 qla_mdelay(__func__, 100);
2153 ha->msg_from_peer = 0;
2155 if (ha->enable_minidump)
2158 if (ha->enable_driverstate_dump)
2159 ql_capture_drvr_state(ha);
2161 if (ql_init_hw(ha)) {
2162 device_printf(ha->pci_dev,
2163 "%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2164 __func__, qla_get_usec_timestamp());
2166 goto qla_error_recovery_exit;
2169 if (ha->qla_interface_up) {
2170 qla_free_xmt_bufs(ha);
2171 qla_free_rcv_bufs(ha);
2174 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2175 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2178 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2180 ha->msg_from_peer = 0;
2182 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2183 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2185 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2188 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) && msecs_100--)
2189 qla_mdelay(__func__, 100);
2190 ha->msg_from_peer = 0;
2192 if (ha->enable_driverstate_dump)
2193 ql_capture_drvr_state(ha);
2195 if (msecs_100 == 0) {
2196 device_printf(ha->pci_dev,
2197 "%s: ts_usecs = %ld exit: QL_PEER_MSG_ACK not received\n",
2198 __func__, qla_get_usec_timestamp());
2200 goto qla_error_recovery_exit;
2203 if (ql_init_hw(ha)) {
2204 device_printf(ha->pci_dev,
2205 "%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2206 __func__, qla_get_usec_timestamp());
2208 goto qla_error_recovery_exit;
2211 if (ha->qla_interface_up) {
2212 qla_free_xmt_bufs(ha);
2213 qla_free_rcv_bufs(ha);
2217 qla_mdelay(__func__, ha->ms_delay_after_init);
2219 *((uint32_t *)&ha->hw.flags) = 0;
2220 ha->qla_initiate_recovery = 0;
2222 if (ha->qla_interface_up) {
2224 if (qla_alloc_xmt_bufs(ha) != 0) {
2226 goto qla_error_recovery_exit;
2229 qla_confirm_9kb_enable(ha);
2231 if (qla_alloc_rcv_bufs(ha) != 0) {
2233 goto qla_error_recovery_exit;
2238 if (ql_init_hw_if(ha) == 0) {
2240 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2241 ha->qla_watchdog_pause = 0;
2242 ql_update_link_state(ha);
2246 if (ha->hw.sp_log_stop_events &
2247 Q8_SP_LOG_STOP_IF_START_FAILURE)
2248 ha->hw.sp_log_stop = -1;
2251 ha->qla_watchdog_pause = 0;
2254 qla_error_recovery_exit:
2257 device_printf(ha->pci_dev, "%s: ts_usecs = %ld port offline\n",
2258 __func__, qla_get_usec_timestamp());
2259 if (ha->hw.sp_log_stop_events &
2260 Q8_SP_LOG_STOP_ERR_RECOVERY_FAILURE)
2261 ha->hw.sp_log_stop = -1;
2265 QLA_UNLOCK(ha, __func__);
2268 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2271 device_printf(ha->pci_dev,
2272 "%s: ts_usecs = %ld exit\n",
2273 __func__, qla_get_usec_timestamp());
2278 qla_async_event(void *context, int pending)
2280 qla_host_t *ha = context;
2282 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2285 if (ha->async_event) {
2286 ha->async_event = 0;
2287 qla_hw_async_event(ha);
2290 QLA_UNLOCK(ha, __func__);
2296 qla_stats(void *context, int pending)