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$");
41 #include "ql_inline.h"
48 * Some PCI Configuration Space Related Defines
51 #ifndef PCI_VENDOR_QLOGIC
52 #define PCI_VENDOR_QLOGIC 0x1077
55 #ifndef PCI_PRODUCT_QLOGIC_ISP8030
56 #define PCI_PRODUCT_QLOGIC_ISP8030 0x8030
59 #define PCI_QLOGIC_ISP8030 \
60 ((PCI_PRODUCT_QLOGIC_ISP8030 << 16) | PCI_VENDOR_QLOGIC)
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);
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,
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,
87 * Hooks to the Operating Systems
89 static int qla_pci_probe (device_t);
90 static int qla_pci_attach (device_t);
91 static int qla_pci_detach (device_t);
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);
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);
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),
114 static driver_t qla_pci_driver = {
115 "ql", qla_pci_methods, sizeof (qla_host_t),
118 static devclass_t qla83xx_devclass;
120 DRIVER_MODULE(qla83xx, pci, qla_pci_driver, qla83xx_devclass, 0, 0);
122 MODULE_DEPEND(qla83xx, pci, 1, 1, 1);
123 MODULE_DEPEND(qla83xx, ether, 1, 1, 1);
125 MALLOC_DEFINE(M_QLA83XXBUF, "qla83xxbuf", "Buffers for qla83xx driver");
127 #define QL_STD_REPLENISH_THRES 0
128 #define QL_JUMBO_REPLENISH_THRES 32
130 static char dev_str[64];
131 static char ver_str[64];
134 * Name: qla_pci_probe
135 * Function: Validate the PCI device to be a QLA80XX device
138 qla_pci_probe(device_t dev)
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,
146 snprintf(ver_str, sizeof(ver_str), "v%d.%d.%d",
147 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
149 device_set_desc(dev, dev_str);
156 printf("%s: %s\n ", __func__, dev_str);
158 return (BUS_PROBE_DEFAULT);
162 qla_add_sysctls(qla_host_t *ha)
164 device_t dev = ha->pci_dev;
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
259 qla_watchdog(void *arg)
261 qla_host_t *ha = arg;
266 if (ha->qla_watchdog_exit) {
267 ha->qla_watchdog_exited = 1;
270 ha->qla_watchdog_exited = 0;
272 if (!ha->qla_watchdog_pause) {
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);
279 if (ha->enable_error_recovery) {
280 ha->qla_watchdog_paused = 1;
281 ha->qla_watchdog_pause = 1;
283 device_printf(ha->pci_dev,
284 "%s: taskqueue_enqueue(err_task) \n",
286 taskqueue_enqueue(ha->err_tq, &ha->err_task);
289 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
295 if (ha->qla_interface_up) {
296 ha->watchdog_ticks++;
298 if (ha->watchdog_ticks > 1000)
299 ha->watchdog_ticks = 0;
301 if (!ha->watchdog_ticks && QL_RUNNING(ifp)) {
302 taskqueue_enqueue(ha->stats_tq,
306 if (ha->async_event) {
307 taskqueue_enqueue(ha->async_event_tq,
308 &ha->async_event_task);
311 ha->qla_watchdog_paused = 0;
314 ha->qla_watchdog_paused = 1;
317 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
322 * Name: qla_pci_attach
323 * Function: attaches the device to the operating system
326 qla_pci_attach(device_t dev)
328 qla_host_t *ha = NULL;
331 uint32_t num_rcvq = 0;
333 if ((ha = device_get_softc(dev)) == NULL) {
334 device_printf(dev, "cannot get softc\n");
338 memset(ha, 0, sizeof (qla_host_t));
340 if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8030) {
341 device_printf(dev, "device is not ISP8030\n");
345 ha->pci_func = pci_get_function(dev) & 0x1;
349 pci_enable_busmaster(dev);
351 ha->reg_rid = PCIR_BAR(0);
352 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
355 if (ha->pci_reg == NULL) {
356 device_printf(dev, "unable to map any ports\n");
357 goto qla_pci_attach_err;
360 rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
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;
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;
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);
377 ha->msix_count = pci_msix_count(dev);
379 if (ha->msix_count < 1 ) {
380 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
382 goto qla_pci_attach_err;
385 if (ha->msix_count < (ha->hw.num_sds_rings + 1)) {
386 ha->hw.num_sds_rings = ha->msix_count - 1;
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,
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;
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,
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,
407 if (qla_get_nic_partition(ha, NULL, &num_rcvq)) {
408 device_printf(dev, "%s: qla_get_nic_partition failed\n",
410 goto qla_pci_attach_err;
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);
417 if ((ha->msix_count < 64) || (num_rcvq != 32)) {
418 if (ha->hw.num_sds_rings > 15) {
419 ha->hw.num_sds_rings = 15;
423 ha->hw.num_rds_rings = ha->hw.num_sds_rings;
424 ha->hw.num_tx_rings = ha->hw.num_sds_rings;
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 */
430 ql_hw_add_sysctls(ha);
432 ha->msix_count = ha->hw.num_sds_rings + 1;
434 if (pci_alloc_msix(dev, &ha->msix_count)) {
435 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
438 goto qla_pci_attach_err;
442 ha->mbx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
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;
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;
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;
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));
464 if (ha->irq_vec[i].irq == NULL) {
465 device_printf(dev, "could not allocate interrupt\n");
466 goto qla_pci_attach_err;
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;
476 ha->tx_fp[i].ha = ha;
477 ha->tx_fp[i].txr_idx = i;
479 if (qla_alloc_tx_br(ha, &ha->tx_fp[i])) {
480 device_printf(dev, "%s: could not allocate tx_br[%d]\n",
482 goto qla_pci_attach_err;
486 if (qla_create_fp_taskqueues(ha) != 0)
487 goto qla_pci_attach_err;
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);
492 ql_read_mac_addr(ha);
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",
498 goto qla_pci_attach_err;
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;
508 if (ql_minidump_init(ha) != 0) {
509 device_printf(dev, "%s: ql_minidump_init failed\n", __func__);
510 goto qla_pci_attach_err;
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);
517 ha->flags.qla_watchdog_active = 1;
518 ha->qla_watchdog_pause = 0;
520 callout_init(&ha->tx_callout, TRUE);
521 ha->flags.qla_callout_init = 1;
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;
529 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
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));
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));
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));
550 QL_DPRINT2(ha, (dev, "%s: exit 0\n", __func__));
557 if (ha->flags.lock_init) {
558 mtx_destroy(&ha->hw_lock);
559 mtx_destroy(&ha->sp_log_lock);
562 QL_DPRINT2(ha, (dev, "%s: exit ENXIO\n", __func__));
567 * Name: qla_pci_detach
568 * Function: Unhooks the device from the operating system
571 qla_pci_detach(device_t dev)
573 qla_host_t *ha = NULL;
576 if ((ha = device_get_softc(dev)) == NULL) {
577 device_printf(dev, "cannot get softc\n");
581 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
585 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
586 QLA_LOCK(ha, __func__, -1, 0);
588 ha->qla_detach_active = 1;
593 QLA_UNLOCK(ha, __func__);
595 if (ha->flags.lock_init) {
596 mtx_destroy(&ha->hw_lock);
597 mtx_destroy(&ha->sp_log_lock);
600 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
606 * SYSCTL Related Callbacks
609 qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS)
614 err = sysctl_handle_int(oidp, &ret, 0, req);
616 if (err || !req->newptr)
620 ha = (qla_host_t *)arg1;
621 ql_hw_link_status(ha);
628 * Function: Releases the resources allocated for the device
631 qla_release(qla_host_t *ha)
638 if (ha->async_event_tq) {
639 taskqueue_drain_all(ha->async_event_tq);
640 taskqueue_free(ha->async_event_tq);
644 taskqueue_drain_all(ha->err_tq);
645 taskqueue_free(ha->err_tq);
649 taskqueue_drain_all(ha->stats_tq);
650 taskqueue_free(ha->stats_tq);
655 if (ha->flags.qla_watchdog_active) {
656 ha->qla_watchdog_exit = 1;
658 while (ha->qla_watchdog_exited == 0)
659 qla_mdelay(__func__, 1);
662 if (ha->flags.qla_callout_init)
663 callout_stop(&ha->tx_callout);
666 ether_ifdetach(ha->ifp);
668 ql_free_drvr_state_buffer(ha);
669 ql_free_sp_log_buffer(ha);
671 qla_free_parent_dma_tag(ha);
674 (void)bus_teardown_intr(dev, ha->mbx_irq, ha->mbx_handle);
677 (void) bus_release_resource(dev, SYS_RES_IRQ, ha->mbx_irq_rid,
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);
686 if (ha->irq_vec[i].irq) {
687 (void)bus_release_resource(dev, SYS_RES_IRQ,
688 ha->irq_vec[i].irq_rid,
692 qla_free_tx_br(ha, &ha->tx_fp[i]);
694 qla_destroy_fp_taskqueues(ha);
697 pci_release_msi(dev);
700 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
704 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
711 * DMA Related Functions
715 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
717 *((bus_addr_t *)arg) = 0;
720 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
724 *((bus_addr_t *)arg) = segs[0].ds_addr;
730 ql_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
738 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
740 ret = bus_dma_tag_create(
741 ha->parent_tag,/* parent */
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 */
749 dma_buf->size, /* maxsegsize */
751 NULL, NULL, /* lockfunc, lockarg */
755 device_printf(dev, "%s: could not create dma tag\n", __func__);
756 goto ql_alloc_dmabuf_exit;
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),
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;
768 ret = bus_dmamap_load(dma_buf->dma_tag,
773 &b_addr, BUS_DMA_NOWAIT);
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,
780 goto ql_alloc_dmabuf_exit;
783 dma_buf->dma_addr = b_addr;
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,
795 ql_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
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);
803 qla_alloc_parent_dma_tag(qla_host_t *ha)
811 * Allocate parent DMA Tag
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 */
821 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
823 NULL, NULL, /* lockfunc, lockarg */
827 device_printf(dev, "%s: could not create parent dma tag\n",
832 ha->flags.parent_tag = 1;
838 qla_free_parent_dma_tag(qla_host_t *ha)
840 if (ha->flags.parent_tag) {
841 bus_dma_tag_destroy(ha->parent_tag);
842 ha->flags.parent_tag = 0;
847 * Name: qla_init_ifnet
848 * Function: Creates the Network Device Interface and Registers it with the O.S
852 qla_init_ifnet(device_t dev, qla_host_t *ha)
856 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
858 ifp = ha->ifp = if_alloc(IFT_ETHER);
861 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
863 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
865 ifp->if_baudrate = IF_Gbps(10);
866 ifp->if_capabilities = IFCAP_LINKSTATE;
867 ifp->if_mtu = ETHERMTU;
869 ifp->if_init = qla_init;
871 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
872 ifp->if_ioctl = qla_ioctl;
874 ifp->if_transmit = qla_transmit;
875 ifp->if_qflush = qla_qflush;
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);
881 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
883 ether_ifattach(ifp, qla_get_mac_addr(ha));
885 ifp->if_capabilities |= IFCAP_HWCSUM |
889 IFCAP_VLAN_HWTAGGING |
894 ifp->if_capenable = ifp->if_capabilities;
896 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
898 ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
900 ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
902 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
904 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
906 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
912 qla_init_locked(qla_host_t *ha)
914 struct ifnet *ifp = ha->ifp;
916 ql_sp_log(ha, 14, 0, 0, 0, 0, 0, 0);
920 if (qla_alloc_xmt_bufs(ha) != 0)
923 qla_confirm_9kb_enable(ha);
925 if (qla_alloc_rcv_bufs(ha) != 0)
928 bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
930 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
931 ifp->if_hwassist |= CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
934 if (ql_init_hw_if(ha) == 0) {
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);
942 if (ha->hw.sp_log_stop_events & Q8_SP_LOG_STOP_IF_START_FAILURE)
943 ha->hw.sp_log_stop = -1;
946 ha->qla_watchdog_pause = 0;
956 ha = (qla_host_t *)arg;
958 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
960 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
965 QLA_UNLOCK(ha, __func__);
967 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
971 qla_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int mcnt)
975 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
978 bcopy(LLADDR(sdl), &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
984 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
986 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
988 struct ifnet *ifp = ha->ifp;
991 mcnt = if_foreach_llmaddr(ifp, qla_copy_maddr, mta);
993 if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
994 QLA_LOCK_NO_SLEEP) != 0)
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);
1001 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1003 ret = qla_hw_del_all_mcast(ha);
1006 device_printf(ha->pci_dev,
1007 "%s: qla_hw_del_all_mcast() failed\n",
1012 ret = ql_hw_set_multi(ha, mta, mcnt, 1);
1015 QLA_UNLOCK(ha, __func__);
1021 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1024 struct ifreq *ifr = (struct ifreq *)data;
1026 struct ifaddr *ifa = (struct ifaddr *)data;
1030 ha = (qla_host_t *)ifp->if_softc;
1031 if (ha->offline || ha->qla_initiate_recovery)
1036 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
1040 if (ifa->ifa_addr->sa_family == AF_INET) {
1041 ret = QLA_LOCK(ha, __func__,
1042 QLA_LOCK_DEFAULT_MS_TIMEOUT,
1047 ifp->if_flags |= IFF_UP;
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);
1053 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1054 qla_init_locked(ha);
1057 QLA_UNLOCK(ha, __func__);
1058 QL_DPRINT4(ha, (ha->pci_dev,
1059 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1061 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1063 arp_ifinit(ifp, ifa);
1067 ether_ioctl(ifp, cmd, data);
1071 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1074 if (ifr->ifr_mtu > QLA_MAX_MTU) {
1077 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1083 ifp->if_mtu = ifr->ifr_mtu;
1084 ha->max_frame_size =
1085 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
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);
1091 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1092 qla_init_locked(ha);
1095 if (ifp->if_mtu > ETHERMTU)
1096 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1098 ha->std_replenish = QL_STD_REPLENISH_THRES;
1101 QLA_UNLOCK(ha, __func__);
1107 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1110 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
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);
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);
1125 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1126 if ((ifp->if_flags ^ ha->if_flags) &
1128 ret = ql_set_promisc(ha);
1129 } else if ((ifp->if_flags ^ ha->if_flags) &
1131 ret = ql_set_allmulti(ha);
1135 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1137 ha->if_flags = ifp->if_flags;
1140 QLA_UNLOCK(ha, __func__);
1144 QL_DPRINT4(ha, (ha->pci_dev,
1145 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1147 if (qla_set_multi(ha, 1))
1152 QL_DPRINT4(ha, (ha->pci_dev,
1153 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1155 if (qla_set_multi(ha, 0))
1161 QL_DPRINT4(ha, (ha->pci_dev,
1162 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1164 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1169 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1171 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
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;
1187 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1188 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
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);
1198 qla_init_locked(ha);
1200 QLA_UNLOCK(ha, __func__);
1202 VLAN_CAPABILITIES(ifp);
1207 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1209 ret = ether_ioctl(ifp, cmd, data);
1217 qla_media_change(struct ifnet *ifp)
1220 struct ifmedia *ifm;
1223 ha = (qla_host_t *)ifp->if_softc;
1225 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1229 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1232 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1238 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1242 ha = (qla_host_t *)ifp->if_softc;
1244 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1246 ifmr->ifm_status = IFM_AVALID;
1247 ifmr->ifm_active = IFM_ETHER;
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));
1255 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1256 (ha->hw.link_up ? "link_up" : "link_down")));
1262 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1265 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1270 struct mbuf *m_head = *m_headp;
1272 QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1274 tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
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));
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);
1289 QL_INITIATE_RECOVERY(ha);
1293 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1295 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1301 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1302 m_head->m_pkthdr.len));
1304 m = m_defrag(m_head, M_NOWAIT);
1306 ha->err_tx_defrag++;
1309 device_printf(ha->pci_dev,
1310 "%s: m_defrag() = NULL [%d]\n",
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++;
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);
1325 if (ret != ENOMEM) {
1333 ha->err_tx_dmamap_load++;
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);
1339 if (ret != ENOMEM) {
1346 QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1348 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1350 if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1352 ha->tx_ring[txr_idx].count++;
1354 ha->tx_ring[txr_idx].iscsi_pkt_count++;
1355 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1357 bus_dmamap_unload(ha->tx_tag, map);
1358 if (ret == EINVAL) {
1365 QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1370 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
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);
1375 mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
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));
1388 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1391 struct ifnet *ifp = ha->ifp;
1393 if (mtx_initialized(&fp->tx_mtx)) {
1394 if (fp->tx_br != NULL) {
1395 mtx_lock(&fp->tx_mtx);
1397 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1401 mtx_unlock(&fp->tx_mtx);
1403 buf_ring_free(fp->tx_br, M_DEVBUF);
1406 mtx_destroy(&fp->tx_mtx);
1412 qla_fp_taskqueue(void *context, int pending)
1417 struct mbuf *mp = NULL;
1420 uint32_t iscsi_pdu = 0;
1421 uint32_t rx_pkts_left = -1;
1428 ha = (qla_host_t *)fp->ha;
1432 txr_idx = fp->txr_idx;
1434 mtx_lock(&fp->tx_mtx);
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;
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);
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)));
1449 ql_hw_tx_done_locked(ha, fp->txr_idx);
1450 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1452 mp = drbr_peek(ifp, fp->tx_br);
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) {
1459 (ha->hw.num_tx_rings >> 1);
1463 txr_idx = fp->txr_idx;
1465 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1468 ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1472 drbr_putback(ifp, fp->tx_br, mp);
1474 drbr_advance(ifp, fp->tx_br);
1477 mtx_unlock(&fp->tx_mtx);
1479 goto qla_fp_taskqueue_exit0;
1481 drbr_advance(ifp, fp->tx_br);
1484 /* Send a copy of the frame to the BPF listener */
1485 ETHER_BPF_MTAP(ifp, mp);
1487 if (((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) ||
1491 mp = drbr_peek(ifp, fp->tx_br);
1494 mtx_unlock(&fp->tx_mtx);
1496 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1497 goto qla_fp_taskqueue_exit;
1499 qla_fp_taskqueue_exit0:
1501 if (rx_pkts_left || ((mp != NULL) && ret)) {
1502 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1504 if (!ha->stop_rcv) {
1505 QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1509 qla_fp_taskqueue_exit:
1511 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1516 qla_create_fp_taskqueues(qla_host_t *ha)
1519 uint8_t tq_name[32];
1521 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1522 qla_tx_fp_t *fp = &ha->tx_fp[i];
1524 bzero(tq_name, sizeof (tq_name));
1525 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1527 NET_TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1529 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1530 taskqueue_thread_enqueue,
1533 if (fp->fp_taskqueue == NULL)
1536 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1539 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1547 qla_destroy_fp_taskqueues(qla_host_t *ha)
1551 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1552 qla_tx_fp_t *fp = &ha->tx_fp[i];
1554 if (fp->fp_taskqueue != NULL) {
1555 taskqueue_drain_all(fp->fp_taskqueue);
1556 taskqueue_free(fp->fp_taskqueue);
1557 fp->fp_taskqueue = NULL;
1564 qla_drain_fp_taskqueues(qla_host_t *ha)
1568 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1569 qla_tx_fp_t *fp = &ha->tx_fp[i];
1571 if (fp->fp_taskqueue != NULL) {
1572 taskqueue_drain_all(fp->fp_taskqueue);
1579 qla_transmit(struct ifnet *ifp, struct mbuf *mp)
1581 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1586 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1588 #if __FreeBSD_version >= 1100000
1589 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1591 if (mp->m_flags & M_FLOWID)
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];
1597 if (fp->tx_br == NULL) {
1599 goto qla_transmit_exit;
1603 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1606 if (fp->fp_taskqueue != NULL)
1607 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1613 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1618 qla_qflush(struct ifnet *ifp)
1625 ha = (qla_host_t *)ifp->if_softc;
1627 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1629 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1636 mtx_lock(&fp->tx_mtx);
1638 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1641 mtx_unlock(&fp->tx_mtx);
1644 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1650 qla_stop(qla_host_t *ha)
1652 struct ifnet *ifp = ha->ifp;
1655 ql_sp_log(ha, 13, 0, 0, 0, 0, 0, 0);
1657 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1658 ha->qla_watchdog_pause = 1;
1660 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1668 if (fp->tx_br != NULL) {
1669 mtx_lock(&fp->tx_mtx);
1670 mtx_unlock(&fp->tx_mtx);
1674 while (!ha->qla_watchdog_paused)
1675 qla_mdelay(__func__, 1);
1677 ha->qla_interface_up = 0;
1679 qla_drain_fp_taskqueues(ha);
1683 qla_free_xmt_bufs(ha);
1684 qla_free_rcv_bufs(ha);
1690 * Buffer Management Functions for Transmit and Receive Rings
1693 qla_alloc_xmt_bufs(qla_host_t *ha)
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 */
1711 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
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));
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];
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",
1732 qla_free_xmt_bufs(ha);
1743 * Release mbuf after it sent on the wire
1746 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1748 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1751 bus_dmamap_sync(ha->tx_tag, txb->map,
1752 BUS_DMASYNC_POSTWRITE);
1754 bus_dmamap_unload(ha->tx_tag, txb->map);
1756 m_freem(txb->m_head);
1759 bus_dmamap_destroy(ha->tx_tag, txb->map);
1764 bus_dmamap_unload(ha->tx_tag, txb->map);
1765 bus_dmamap_destroy(ha->tx_tag, txb->map);
1769 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1773 qla_free_xmt_bufs(qla_host_t *ha)
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]);
1782 if (ha->tx_tag != NULL) {
1783 bus_dma_tag_destroy(ha->tx_tag);
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));
1795 qla_alloc_rcv_std(qla_host_t *ha)
1797 int i, j, k, r, ret = 0;
1799 qla_rx_ring_t *rx_ring;
1801 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1802 rx_ring = &ha->rx_ring[r];
1804 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1805 rxb = &rx_ring->rx_buf[i];
1807 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1811 device_printf(ha->pci_dev,
1812 "%s: dmamap[%d, %d] failed\n",
1815 for (k = 0; k < r; k++) {
1816 for (j = 0; j < NUM_RX_DESCRIPTORS;
1818 rxb = &ha->rx_ring[k].rx_buf[j];
1819 bus_dmamap_destroy(ha->rx_tag,
1824 for (j = 0; j < i; j++) {
1825 bus_dmamap_destroy(ha->rx_tag,
1826 rx_ring->rx_buf[j].map);
1828 goto qla_alloc_rcv_std_err;
1833 qla_init_hw_rcv_descriptors(ha);
1835 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1836 rx_ring = &ha->rx_ring[r];
1838 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1839 rxb = &rx_ring->rx_buf[i];
1841 if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1843 * set the physical address in the
1844 * corresponding descriptor entry in the
1845 * receive ring/queue for the hba
1847 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1849 (rxb->m_head)->m_pkthdr.len);
1851 device_printf(ha->pci_dev,
1852 "%s: ql_get_mbuf [%d, %d] failed\n",
1854 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1855 goto qla_alloc_rcv_std_err;
1861 qla_alloc_rcv_std_err:
1866 qla_free_rcv_std(qla_host_t *ha)
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);
1886 qla_alloc_rcv_bufs(qla_host_t *ha)
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 */
1897 MJUM9BYTES, /* maxsegsize */
1898 BUS_DMA_ALLOCNOW, /* flags */
1899 NULL, /* lockfunc */
1900 NULL, /* lockfuncarg */
1902 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1908 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
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;
1916 ret = qla_alloc_rcv_std(ha);
1922 qla_free_rcv_bufs(qla_host_t *ha)
1926 qla_free_rcv_std(ha);
1928 if (ha->rx_tag != NULL) {
1929 bus_dma_tag_destroy(ha->rx_tag);
1933 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
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;
1945 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1947 register struct mbuf *mp = nmp;
1950 bus_dma_segment_t segs[1];
1951 int nsegs, mbuf_size;
1953 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1955 if (ha->hw.enable_9kb)
1956 mbuf_size = MJUM9BYTES;
1958 mbuf_size = MCLBYTES;
1961 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1964 if (ha->hw.enable_9kb)
1965 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
1967 mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1972 device_printf(ha->pci_dev,
1973 "%s: m_getcl failed\n", __func__);
1974 goto exit_ql_get_mbuf;
1976 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1978 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1979 mp->m_data = mp->m_ext.ext_buf;
1983 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1985 offset = 8 - offset;
1990 * Using memory from the mbuf cluster pool, invoke the bus_dma
1991 * machinery to arrange the memory mapping.
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;
1997 if (ret || !rxb->paddr || (nsegs != 1)) {
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,
2005 goto exit_ql_get_mbuf;
2008 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
2011 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
2016 qla_get_peer(qla_host_t *ha)
2020 int my_slot = pci_get_slot(ha->pci_dev);
2022 if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
2025 for (i = 0; i < count; i++) {
2026 slot = pci_get_slot(peers[i]);
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];
2038 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
2040 qla_host_t *ha_peer;
2043 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
2044 ha_peer->msg_from_peer = msg_to_peer;
2050 qla_set_error_recovery(qla_host_t *ha)
2052 struct ifnet *ifp = ha->ifp;
2054 if (!cold && ha->enable_error_recovery) {
2056 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2057 ha->qla_initiate_recovery = 1;
2064 qla_error_recovery(void *context, int pending)
2066 qla_host_t *ha = context;
2067 uint32_t msecs_100 = 400;
2068 struct ifnet *ifp = ha->ifp;
2071 device_printf(ha->pci_dev, "%s: enter\n", __func__);
2072 ha->hw.imd_compl = 1;
2074 taskqueue_drain_all(ha->stats_tq);
2075 taskqueue_drain_all(ha->async_event_tq);
2077 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2080 device_printf(ha->pci_dev, "%s: ts_usecs = %ld start\n",
2081 __func__, qla_get_usec_timestamp());
2083 if (ha->qla_interface_up) {
2084 qla_mdelay(__func__, 300);
2086 //ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2088 for (i = 0; i < ha->hw.num_sds_rings; i++) {
2096 if (fp->tx_br != NULL) {
2097 mtx_lock(&fp->tx_mtx);
2098 mtx_unlock(&fp->tx_mtx);
2103 qla_drain_fp_taskqueues(ha);
2105 if ((ha->pci_func & 0x1) == 0) {
2106 if (!ha->msg_from_peer) {
2107 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2109 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2111 qla_mdelay(__func__, 100);
2114 ha->msg_from_peer = 0;
2116 if (ha->enable_minidump)
2119 if (ha->enable_driverstate_dump)
2120 ql_capture_drvr_state(ha);
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());
2127 goto qla_error_recovery_exit;
2130 if (ha->qla_interface_up) {
2131 qla_free_xmt_bufs(ha);
2132 qla_free_rcv_bufs(ha);
2135 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2136 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2139 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2140 ha->msg_from_peer = 0;
2142 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2143 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2145 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2148 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) && msecs_100--)
2149 qla_mdelay(__func__, 100);
2150 ha->msg_from_peer = 0;
2152 if (ha->enable_driverstate_dump)
2153 ql_capture_drvr_state(ha);
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());
2160 goto qla_error_recovery_exit;
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());
2168 goto qla_error_recovery_exit;
2171 if (ha->qla_interface_up) {
2172 qla_free_xmt_bufs(ha);
2173 qla_free_rcv_bufs(ha);
2177 qla_mdelay(__func__, ha->ms_delay_after_init);
2179 *((uint32_t *)&ha->hw.flags) = 0;
2180 ha->qla_initiate_recovery = 0;
2182 if (ha->qla_interface_up) {
2183 if (qla_alloc_xmt_bufs(ha) != 0) {
2185 goto qla_error_recovery_exit;
2188 qla_confirm_9kb_enable(ha);
2190 if (qla_alloc_rcv_bufs(ha) != 0) {
2192 goto qla_error_recovery_exit;
2197 if (ql_init_hw_if(ha) == 0) {
2199 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2200 ha->qla_watchdog_pause = 0;
2201 ql_update_link_state(ha);
2205 if (ha->hw.sp_log_stop_events &
2206 Q8_SP_LOG_STOP_IF_START_FAILURE)
2207 ha->hw.sp_log_stop = -1;
2210 ha->qla_watchdog_pause = 0;
2213 qla_error_recovery_exit:
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;
2223 QLA_UNLOCK(ha, __func__);
2226 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2229 device_printf(ha->pci_dev,
2230 "%s: ts_usecs = %ld exit\n",
2231 __func__, qla_get_usec_timestamp());
2236 qla_async_event(void *context, int pending)
2238 qla_host_t *ha = context;
2240 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2243 if (ha->async_event) {
2244 ha->async_event = 0;
2245 qla_hw_async_event(ha);
2248 QLA_UNLOCK(ha, __func__);
2254 qla_stats(void *context, int pending)