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)),
180 OID_AUTO, "link_status", CTLTYPE_INT | CTLFLAG_RW,
182 qla_sysctl_get_link_status, "I", "Link Status");
185 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
186 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
187 OID_AUTO, "debug", CTLFLAG_RW,
188 &ha->dbg_level, ha->dbg_level, "Debug Level");
190 ha->enable_minidump = 1;
191 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
192 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
193 OID_AUTO, "enable_minidump", CTLFLAG_RW,
194 &ha->enable_minidump, ha->enable_minidump,
195 "Minidump retrival prior to error recovery "
196 "is enabled only when this is set");
198 ha->enable_driverstate_dump = 1;
199 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
200 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
201 OID_AUTO, "enable_driverstate_dump", CTLFLAG_RW,
202 &ha->enable_driverstate_dump, ha->enable_driverstate_dump,
203 "Driver State retrival prior to error recovery "
204 "is enabled only when this is set");
206 ha->enable_error_recovery = 1;
207 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
208 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
209 OID_AUTO, "enable_error_recovery", CTLFLAG_RW,
210 &ha->enable_error_recovery, ha->enable_error_recovery,
211 "when set error recovery is enabled on fatal errors "
212 "otherwise the port is turned offline");
214 ha->ms_delay_after_init = 1000;
215 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
216 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
217 OID_AUTO, "ms_delay_after_init", CTLFLAG_RW,
218 &ha->ms_delay_after_init, ha->ms_delay_after_init,
219 "millisecond delay after hw_init");
221 ha->std_replenish = QL_STD_REPLENISH_THRES;
222 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
223 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
224 OID_AUTO, "std_replenish", CTLFLAG_RW,
225 &ha->std_replenish, ha->std_replenish,
226 "Threshold for Replenishing Standard Frames");
228 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
229 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
230 OID_AUTO, "ipv4_lro",
231 CTLFLAG_RD, &ha->ipv4_lro,
232 "number of ipv4 lro completions");
234 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
235 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
236 OID_AUTO, "ipv6_lro",
237 CTLFLAG_RD, &ha->ipv6_lro,
238 "number of ipv6 lro completions");
240 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
241 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
242 OID_AUTO, "tx_tso_frames",
243 CTLFLAG_RD, &ha->tx_tso_frames,
244 "number of Tx TSO Frames");
246 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
247 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
248 OID_AUTO, "hw_vlan_tx_frames",
249 CTLFLAG_RD, &ha->hw_vlan_tx_frames,
250 "number of Tx VLAN Frames");
252 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
253 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
254 OID_AUTO, "hw_lock_failed",
255 CTLFLAG_RD, &ha->hw_lock_failed,
256 "number of hw_lock failures");
262 qla_watchdog(void *arg)
264 qla_host_t *ha = arg;
271 if (ha->qla_watchdog_exit) {
272 ha->qla_watchdog_exited = 1;
275 ha->qla_watchdog_exited = 0;
277 if (!ha->qla_watchdog_pause) {
279 (ql_hw_check_health(ha) || ha->qla_initiate_recovery ||
280 (ha->msg_from_peer == QL_PEER_MSG_RESET))) {
282 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
283 ql_update_link_state(ha);
285 if (ha->enable_error_recovery) {
286 ha->qla_watchdog_paused = 1;
287 ha->qla_watchdog_pause = 1;
289 device_printf(ha->pci_dev,
290 "%s: taskqueue_enqueue(err_task) \n",
292 taskqueue_enqueue(ha->err_tq, &ha->err_task);
295 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
301 if (ha->qla_interface_up) {
303 ha->watchdog_ticks++;
305 if (ha->watchdog_ticks > 1000)
306 ha->watchdog_ticks = 0;
308 if (!ha->watchdog_ticks && QL_RUNNING(ifp)) {
309 taskqueue_enqueue(ha->stats_tq,
313 if (ha->async_event) {
314 taskqueue_enqueue(ha->async_event_tq,
315 &ha->async_event_task);
319 ha->qla_watchdog_paused = 0;
322 ha->qla_watchdog_paused = 1;
325 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
330 * Name: qla_pci_attach
331 * Function: attaches the device to the operating system
334 qla_pci_attach(device_t dev)
336 qla_host_t *ha = NULL;
339 uint32_t num_rcvq = 0;
341 if ((ha = device_get_softc(dev)) == NULL) {
342 device_printf(dev, "cannot get softc\n");
346 memset(ha, 0, sizeof (qla_host_t));
348 if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8030) {
349 device_printf(dev, "device is not ISP8030\n");
353 ha->pci_func = pci_get_function(dev) & 0x1;
357 pci_enable_busmaster(dev);
359 ha->reg_rid = PCIR_BAR(0);
360 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
363 if (ha->pci_reg == NULL) {
364 device_printf(dev, "unable to map any ports\n");
365 goto qla_pci_attach_err;
368 rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
371 mtx_init(&ha->hw_lock, "qla83xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
372 mtx_init(&ha->sp_log_lock, "qla83xx_sp_log_lock", MTX_NETWORK_LOCK, MTX_DEF);
373 ha->flags.lock_init = 1;
377 ha->hw.num_sds_rings = MAX_SDS_RINGS;
378 ha->hw.num_rds_rings = MAX_RDS_RINGS;
379 ha->hw.num_tx_rings = NUM_TX_RINGS;
381 ha->reg_rid1 = PCIR_BAR(2);
382 ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
383 &ha->reg_rid1, RF_ACTIVE);
385 ha->msix_count = pci_msix_count(dev);
387 if (ha->msix_count < 1 ) {
388 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
390 goto qla_pci_attach_err;
393 if (ha->msix_count < (ha->hw.num_sds_rings + 1)) {
394 ha->hw.num_sds_rings = ha->msix_count - 1;
397 QL_DPRINT2(ha, (dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
398 " msix_count 0x%x pci_reg %p pci_reg1 %p\n", __func__, ha,
399 ha->pci_func, rsrc_len, ha->msix_count, ha->pci_reg,
402 /* initialize hardware */
403 if (ql_init_hw(ha)) {
404 device_printf(dev, "%s: ql_init_hw failed\n", __func__);
405 goto qla_pci_attach_err;
408 device_printf(dev, "%s: firmware[%d.%d.%d.%d]\n", __func__,
409 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
411 snprintf(ha->fw_ver_str, sizeof(ha->fw_ver_str), "%d.%d.%d.%d",
412 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
415 if (qla_get_nic_partition(ha, NULL, &num_rcvq)) {
416 device_printf(dev, "%s: qla_get_nic_partition failed\n",
418 goto qla_pci_attach_err;
420 device_printf(dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
421 " msix_count 0x%x pci_reg %p pci_reg1 %p num_rcvq = %d\n",
422 __func__, ha, ha->pci_func, rsrc_len, ha->msix_count,
423 ha->pci_reg, ha->pci_reg1, num_rcvq);
425 if ((ha->msix_count < 64) || (num_rcvq != 32)) {
426 if (ha->hw.num_sds_rings > 15) {
427 ha->hw.num_sds_rings = 15;
431 ha->hw.num_rds_rings = ha->hw.num_sds_rings;
432 ha->hw.num_tx_rings = ha->hw.num_sds_rings;
434 #ifdef QL_ENABLE_ISCSI_TLV
435 ha->hw.num_tx_rings = ha->hw.num_sds_rings * 2;
436 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
438 ql_hw_add_sysctls(ha);
440 ha->msix_count = ha->hw.num_sds_rings + 1;
442 if (pci_alloc_msix(dev, &ha->msix_count)) {
443 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
446 goto qla_pci_attach_err;
450 ha->mbx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
452 (RF_ACTIVE | RF_SHAREABLE));
453 if (ha->mbx_irq == NULL) {
454 device_printf(dev, "could not allocate mbx interrupt\n");
455 goto qla_pci_attach_err;
457 if (bus_setup_intr(dev, ha->mbx_irq, (INTR_TYPE_NET | INTR_MPSAFE),
458 NULL, ql_mbx_isr, ha, &ha->mbx_handle)) {
459 device_printf(dev, "could not setup mbx interrupt\n");
460 goto qla_pci_attach_err;
463 for (i = 0; i < ha->hw.num_sds_rings; i++) {
464 ha->irq_vec[i].sds_idx = i;
465 ha->irq_vec[i].ha = ha;
466 ha->irq_vec[i].irq_rid = 2 + i;
468 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
469 &ha->irq_vec[i].irq_rid,
470 (RF_ACTIVE | RF_SHAREABLE));
472 if (ha->irq_vec[i].irq == NULL) {
473 device_printf(dev, "could not allocate interrupt\n");
474 goto qla_pci_attach_err;
476 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
477 (INTR_TYPE_NET | INTR_MPSAFE),
478 NULL, ql_isr, &ha->irq_vec[i],
479 &ha->irq_vec[i].handle)) {
480 device_printf(dev, "could not setup interrupt\n");
481 goto qla_pci_attach_err;
484 ha->tx_fp[i].ha = ha;
485 ha->tx_fp[i].txr_idx = i;
487 if (qla_alloc_tx_br(ha, &ha->tx_fp[i])) {
488 device_printf(dev, "%s: could not allocate tx_br[%d]\n",
490 goto qla_pci_attach_err;
494 if (qla_create_fp_taskqueues(ha) != 0)
495 goto qla_pci_attach_err;
497 printf("%s: mp__ncpus %d sds %d rds %d msi-x %d\n", __func__, mp_ncpus,
498 ha->hw.num_sds_rings, ha->hw.num_rds_rings, ha->msix_count);
500 ql_read_mac_addr(ha);
502 /* allocate parent dma tag */
503 if (qla_alloc_parent_dma_tag(ha)) {
504 device_printf(dev, "%s: qla_alloc_parent_dma_tag failed\n",
506 goto qla_pci_attach_err;
509 /* alloc all dma buffers */
510 if (ql_alloc_dma(ha)) {
511 device_printf(dev, "%s: ql_alloc_dma failed\n", __func__);
512 goto qla_pci_attach_err;
516 if (ql_minidump_init(ha) != 0) {
517 device_printf(dev, "%s: ql_minidump_init failed\n", __func__);
518 goto qla_pci_attach_err;
520 ql_alloc_drvr_state_buffer(ha);
521 ql_alloc_sp_log_buffer(ha);
522 /* create the o.s ethernet interface */
523 qla_init_ifnet(dev, ha);
525 ha->flags.qla_watchdog_active = 1;
526 ha->qla_watchdog_pause = 0;
528 callout_init(&ha->tx_callout, TRUE);
529 ha->flags.qla_callout_init = 1;
531 /* create ioctl device interface */
532 if (ql_make_cdev(ha)) {
533 device_printf(dev, "%s: ql_make_cdev failed\n", __func__);
534 goto qla_pci_attach_err;
537 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
540 TASK_INIT(&ha->err_task, 0, qla_error_recovery, ha);
541 ha->err_tq = taskqueue_create("qla_errq", M_NOWAIT,
542 taskqueue_thread_enqueue, &ha->err_tq);
543 taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
544 device_get_nameunit(ha->pci_dev));
546 TASK_INIT(&ha->async_event_task, 0, qla_async_event, ha);
547 ha->async_event_tq = taskqueue_create("qla_asyncq", M_NOWAIT,
548 taskqueue_thread_enqueue, &ha->async_event_tq);
549 taskqueue_start_threads(&ha->async_event_tq, 1, PI_NET, "%s asyncq",
550 device_get_nameunit(ha->pci_dev));
552 TASK_INIT(&ha->stats_task, 0, qla_stats, ha);
553 ha->stats_tq = taskqueue_create("qla_statsq", M_NOWAIT,
554 taskqueue_thread_enqueue, &ha->stats_tq);
555 taskqueue_start_threads(&ha->stats_tq, 1, PI_NET, "%s taskq",
556 device_get_nameunit(ha->pci_dev));
558 QL_DPRINT2(ha, (dev, "%s: exit 0\n", __func__));
565 if (ha->flags.lock_init) {
566 mtx_destroy(&ha->hw_lock);
567 mtx_destroy(&ha->sp_log_lock);
570 QL_DPRINT2(ha, (dev, "%s: exit ENXIO\n", __func__));
575 * Name: qla_pci_detach
576 * Function: Unhooks the device from the operating system
579 qla_pci_detach(device_t dev)
581 qla_host_t *ha = NULL;
585 if ((ha = device_get_softc(dev)) == NULL) {
586 device_printf(dev, "cannot get softc\n");
590 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
594 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
595 QLA_LOCK(ha, __func__, -1, 0);
597 ha->qla_detach_active = 1;
602 QLA_UNLOCK(ha, __func__);
604 if (ha->flags.lock_init) {
605 mtx_destroy(&ha->hw_lock);
606 mtx_destroy(&ha->sp_log_lock);
609 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
615 * SYSCTL Related Callbacks
618 qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS)
623 err = sysctl_handle_int(oidp, &ret, 0, req);
625 if (err || !req->newptr)
629 ha = (qla_host_t *)arg1;
630 ql_hw_link_status(ha);
637 * Function: Releases the resources allocated for the device
640 qla_release(qla_host_t *ha)
647 if (ha->async_event_tq) {
648 taskqueue_drain_all(ha->async_event_tq);
649 taskqueue_free(ha->async_event_tq);
653 taskqueue_drain_all(ha->err_tq);
654 taskqueue_free(ha->err_tq);
658 taskqueue_drain_all(ha->stats_tq);
659 taskqueue_free(ha->stats_tq);
664 if (ha->flags.qla_watchdog_active) {
665 ha->qla_watchdog_exit = 1;
667 while (ha->qla_watchdog_exited == 0)
668 qla_mdelay(__func__, 1);
671 if (ha->flags.qla_callout_init)
672 callout_stop(&ha->tx_callout);
675 ether_ifdetach(ha->ifp);
677 ql_free_drvr_state_buffer(ha);
678 ql_free_sp_log_buffer(ha);
680 qla_free_parent_dma_tag(ha);
683 (void)bus_teardown_intr(dev, ha->mbx_irq, ha->mbx_handle);
686 (void) bus_release_resource(dev, SYS_RES_IRQ, ha->mbx_irq_rid,
689 for (i = 0; i < ha->hw.num_sds_rings; i++) {
691 if (ha->irq_vec[i].handle) {
692 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
693 ha->irq_vec[i].handle);
696 if (ha->irq_vec[i].irq) {
697 (void)bus_release_resource(dev, SYS_RES_IRQ,
698 ha->irq_vec[i].irq_rid,
702 qla_free_tx_br(ha, &ha->tx_fp[i]);
704 qla_destroy_fp_taskqueues(ha);
707 pci_release_msi(dev);
710 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
714 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
721 * DMA Related Functions
725 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
727 *((bus_addr_t *)arg) = 0;
730 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
734 *((bus_addr_t *)arg) = segs[0].ds_addr;
740 ql_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
748 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
750 ret = bus_dma_tag_create(
751 ha->parent_tag,/* parent */
753 ((bus_size_t)(1ULL << 32)),/* boundary */
754 BUS_SPACE_MAXADDR, /* lowaddr */
755 BUS_SPACE_MAXADDR, /* highaddr */
756 NULL, NULL, /* filter, filterarg */
757 dma_buf->size, /* maxsize */
759 dma_buf->size, /* maxsegsize */
761 NULL, NULL, /* lockfunc, lockarg */
765 device_printf(dev, "%s: could not create dma tag\n", __func__);
766 goto ql_alloc_dmabuf_exit;
768 ret = bus_dmamem_alloc(dma_buf->dma_tag,
769 (void **)&dma_buf->dma_b,
770 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
773 bus_dma_tag_destroy(dma_buf->dma_tag);
774 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
775 goto ql_alloc_dmabuf_exit;
778 ret = bus_dmamap_load(dma_buf->dma_tag,
783 &b_addr, BUS_DMA_NOWAIT);
785 if (ret || !b_addr) {
786 bus_dma_tag_destroy(dma_buf->dma_tag);
787 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
790 goto ql_alloc_dmabuf_exit;
793 dma_buf->dma_addr = b_addr;
795 ql_alloc_dmabuf_exit:
796 QL_DPRINT2(ha, (dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
797 __func__, ret, (void *)dma_buf->dma_tag,
798 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
805 ql_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
807 bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
808 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
809 bus_dma_tag_destroy(dma_buf->dma_tag);
813 qla_alloc_parent_dma_tag(qla_host_t *ha)
821 * Allocate parent DMA Tag
823 ret = bus_dma_tag_create(
824 bus_get_dma_tag(dev), /* parent */
825 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
826 BUS_SPACE_MAXADDR, /* lowaddr */
827 BUS_SPACE_MAXADDR, /* highaddr */
828 NULL, NULL, /* filter, filterarg */
829 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
831 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
833 NULL, NULL, /* lockfunc, lockarg */
837 device_printf(dev, "%s: could not create parent dma tag\n",
842 ha->flags.parent_tag = 1;
848 qla_free_parent_dma_tag(qla_host_t *ha)
850 if (ha->flags.parent_tag) {
851 bus_dma_tag_destroy(ha->parent_tag);
852 ha->flags.parent_tag = 0;
857 * Name: qla_init_ifnet
858 * Function: Creates the Network Device Interface and Registers it with the O.S
862 qla_init_ifnet(device_t dev, qla_host_t *ha)
866 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
868 ifp = ha->ifp = if_alloc(IFT_ETHER);
871 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
873 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
875 ifp->if_baudrate = IF_Gbps(10);
876 ifp->if_capabilities = IFCAP_LINKSTATE;
877 ifp->if_mtu = ETHERMTU;
879 ifp->if_init = qla_init;
881 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
882 ifp->if_ioctl = qla_ioctl;
884 ifp->if_transmit = qla_transmit;
885 ifp->if_qflush = qla_qflush;
887 IFQ_SET_MAXLEN(&ifp->if_snd, qla_get_ifq_snd_maxlen(ha));
888 ifp->if_snd.ifq_drv_maxlen = qla_get_ifq_snd_maxlen(ha);
889 IFQ_SET_READY(&ifp->if_snd);
891 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
893 ether_ifattach(ifp, qla_get_mac_addr(ha));
895 ifp->if_capabilities |= IFCAP_HWCSUM |
899 IFCAP_VLAN_HWTAGGING |
904 ifp->if_capenable = ifp->if_capabilities;
906 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
908 ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
910 ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
912 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
914 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
916 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
922 qla_init_locked(qla_host_t *ha)
924 struct ifnet *ifp = ha->ifp;
926 ql_sp_log(ha, 14, 0, 0, 0, 0, 0, 0);
930 if (qla_alloc_xmt_bufs(ha) != 0)
933 qla_confirm_9kb_enable(ha);
935 if (qla_alloc_rcv_bufs(ha) != 0)
938 bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
940 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
941 ifp->if_hwassist |= CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
944 if (ql_init_hw_if(ha) == 0) {
946 ifp->if_drv_flags |= IFF_DRV_RUNNING;
947 ha->hw_vlan_tx_frames = 0;
948 ha->tx_tso_frames = 0;
949 ha->qla_interface_up = 1;
950 ql_update_link_state(ha);
952 if (ha->hw.sp_log_stop_events & Q8_SP_LOG_STOP_IF_START_FAILURE)
953 ha->hw.sp_log_stop = -1;
956 ha->qla_watchdog_pause = 0;
966 ha = (qla_host_t *)arg;
968 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
970 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
975 QLA_UNLOCK(ha, __func__);
977 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
981 qla_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int mcnt)
985 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
988 bcopy(LLADDR(sdl), &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
994 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
996 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
998 struct ifnet *ifp = ha->ifp;
1001 mcnt = if_foreach_llmaddr(ifp, qla_copy_maddr, mta);
1003 if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1004 QLA_LOCK_NO_SLEEP) != 0)
1007 ql_sp_log(ha, 12, 4, ifp->if_drv_flags,
1008 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1009 add_multi, (uint32_t)mcnt, 0);
1011 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1014 ret = qla_hw_del_all_mcast(ha);
1017 device_printf(ha->pci_dev,
1018 "%s: qla_hw_del_all_mcast() failed\n",
1023 ret = ql_hw_set_multi(ha, mta, mcnt, 1);
1027 QLA_UNLOCK(ha, __func__);
1033 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1036 struct ifreq *ifr = (struct ifreq *)data;
1037 struct ifaddr *ifa = (struct ifaddr *)data;
1040 ha = (qla_host_t *)ifp->if_softc;
1041 if (ha->offline || ha->qla_initiate_recovery)
1046 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
1049 if (ifa->ifa_addr->sa_family == AF_INET) {
1051 ret = QLA_LOCK(ha, __func__,
1052 QLA_LOCK_DEFAULT_MS_TIMEOUT,
1057 ifp->if_flags |= IFF_UP;
1059 ql_sp_log(ha, 8, 3, ifp->if_drv_flags,
1060 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1061 ntohl(IA_SIN(ifa)->sin_addr.s_addr), 0, 0);
1063 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1064 qla_init_locked(ha);
1067 QLA_UNLOCK(ha, __func__);
1068 QL_DPRINT4(ha, (ha->pci_dev,
1069 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1071 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1073 arp_ifinit(ifp, ifa);
1075 ether_ioctl(ifp, cmd, data);
1080 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1083 if (ifr->ifr_mtu > QLA_MAX_MTU) {
1086 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1092 ifp->if_mtu = ifr->ifr_mtu;
1093 ha->max_frame_size =
1094 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1096 ql_sp_log(ha, 9, 4, ifp->if_drv_flags,
1097 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1098 ha->max_frame_size, ifp->if_mtu, 0);
1100 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1101 qla_init_locked(ha);
1104 if (ifp->if_mtu > ETHERMTU)
1105 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1107 ha->std_replenish = QL_STD_REPLENISH_THRES;
1110 QLA_UNLOCK(ha, __func__);
1116 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1119 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1125 ql_sp_log(ha, 10, 4, ifp->if_drv_flags,
1126 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1127 ha->if_flags, ifp->if_flags, 0);
1129 if (ifp->if_flags & IFF_UP) {
1131 ha->max_frame_size = ifp->if_mtu +
1132 ETHER_HDR_LEN + ETHER_CRC_LEN;
1133 qla_init_locked(ha);
1135 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1136 if ((ifp->if_flags ^ ha->if_flags) &
1138 ret = ql_set_promisc(ha);
1139 } else if ((ifp->if_flags ^ ha->if_flags) &
1141 ret = ql_set_allmulti(ha);
1145 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1147 ha->if_flags = ifp->if_flags;
1150 QLA_UNLOCK(ha, __func__);
1154 QL_DPRINT4(ha, (ha->pci_dev,
1155 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1157 if (qla_set_multi(ha, 1))
1162 QL_DPRINT4(ha, (ha->pci_dev,
1163 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1165 if (qla_set_multi(ha, 0))
1171 QL_DPRINT4(ha, (ha->pci_dev,
1172 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1174 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1179 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1181 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1184 if (mask & IFCAP_HWCSUM)
1185 ifp->if_capenable ^= IFCAP_HWCSUM;
1186 if (mask & IFCAP_TSO4)
1187 ifp->if_capenable ^= IFCAP_TSO4;
1188 if (mask & IFCAP_TSO6)
1189 ifp->if_capenable ^= IFCAP_TSO6;
1190 if (mask & IFCAP_VLAN_HWTAGGING)
1191 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1192 if (mask & IFCAP_VLAN_HWTSO)
1193 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1194 if (mask & IFCAP_LRO)
1195 ifp->if_capenable ^= IFCAP_LRO;
1197 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1198 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1204 ql_sp_log(ha, 11, 4, ifp->if_drv_flags,
1205 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1206 mask, ifp->if_capenable, 0);
1208 qla_init_locked(ha);
1210 QLA_UNLOCK(ha, __func__);
1213 VLAN_CAPABILITIES(ifp);
1218 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1220 ret = ether_ioctl(ifp, cmd, data);
1228 qla_media_change(struct ifnet *ifp)
1231 struct ifmedia *ifm;
1234 ha = (qla_host_t *)ifp->if_softc;
1236 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1240 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1243 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1249 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1253 ha = (qla_host_t *)ifp->if_softc;
1255 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1257 ifmr->ifm_status = IFM_AVALID;
1258 ifmr->ifm_active = IFM_ETHER;
1260 ql_update_link_state(ha);
1261 if (ha->hw.link_up) {
1262 ifmr->ifm_status |= IFM_ACTIVE;
1263 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1266 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1267 (ha->hw.link_up ? "link_up" : "link_down")));
1274 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1277 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1282 struct mbuf *m_head = *m_headp;
1284 QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1286 tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1288 if ((NULL != ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head) ||
1289 (QL_ERR_INJECT(ha, INJCT_TXBUF_MBUF_NON_NULL))){
1290 QL_ASSERT(ha, 0, ("%s [%d]: txr_idx = %d tx_idx = %d "\
1291 "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,\
1292 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head));
1294 device_printf(ha->pci_dev, "%s [%d]: txr_idx = %d tx_idx = %d "
1295 "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,
1296 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head);
1301 QL_INITIATE_RECOVERY(ha);
1305 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1307 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1314 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1315 m_head->m_pkthdr.len));
1317 m = m_defrag(m_head, M_NOWAIT);
1319 ha->err_tx_defrag++;
1322 device_printf(ha->pci_dev,
1323 "%s: m_defrag() = NULL [%d]\n",
1330 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1331 segs, &nsegs, BUS_DMA_NOWAIT))) {
1333 ha->err_tx_dmamap_load++;
1335 device_printf(ha->pci_dev,
1336 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1337 __func__, ret, m_head->m_pkthdr.len);
1339 if (ret != ENOMEM) {
1348 ha->err_tx_dmamap_load++;
1350 device_printf(ha->pci_dev,
1351 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1352 __func__, ret, m_head->m_pkthdr.len);
1354 if (ret != ENOMEM) {
1361 QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1363 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1365 if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1367 ha->tx_ring[txr_idx].count++;
1369 ha->tx_ring[txr_idx].iscsi_pkt_count++;
1370 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1372 bus_dmamap_unload(ha->tx_tag, map);
1373 if (ret == EINVAL) {
1380 QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1385 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1387 snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1388 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1390 mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1392 fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1393 M_NOWAIT, &fp->tx_mtx);
1394 if (fp->tx_br == NULL) {
1395 QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1396 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1403 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1406 struct ifnet *ifp = ha->ifp;
1408 if (mtx_initialized(&fp->tx_mtx)) {
1410 if (fp->tx_br != NULL) {
1412 mtx_lock(&fp->tx_mtx);
1414 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1418 mtx_unlock(&fp->tx_mtx);
1420 buf_ring_free(fp->tx_br, M_DEVBUF);
1423 mtx_destroy(&fp->tx_mtx);
1429 qla_fp_taskqueue(void *context, int pending)
1434 struct mbuf *mp = NULL;
1437 uint32_t iscsi_pdu = 0;
1438 uint32_t rx_pkts_left = -1;
1445 ha = (qla_host_t *)fp->ha;
1449 txr_idx = fp->txr_idx;
1451 mtx_lock(&fp->tx_mtx);
1453 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1454 mtx_unlock(&fp->tx_mtx);
1455 goto qla_fp_taskqueue_exit;
1458 while (rx_pkts_left && !ha->stop_rcv &&
1459 (ifp->if_drv_flags & IFF_DRV_RUNNING) && ha->hw.link_up) {
1460 rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1462 #ifdef QL_ENABLE_ISCSI_TLV
1463 ql_hw_tx_done_locked(ha, fp->txr_idx);
1464 ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1466 ql_hw_tx_done_locked(ha, fp->txr_idx);
1467 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1469 mp = drbr_peek(ifp, fp->tx_br);
1471 while (mp != NULL) {
1473 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1474 #ifdef QL_ENABLE_ISCSI_TLV
1475 if (ql_iscsi_pdu(ha, mp) == 0) {
1477 (ha->hw.num_tx_rings >> 1);
1481 txr_idx = fp->txr_idx;
1483 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1486 ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1490 drbr_putback(ifp, fp->tx_br, mp);
1492 drbr_advance(ifp, fp->tx_br);
1495 mtx_unlock(&fp->tx_mtx);
1497 goto qla_fp_taskqueue_exit0;
1499 drbr_advance(ifp, fp->tx_br);
1502 /* Send a copy of the frame to the BPF listener */
1503 ETHER_BPF_MTAP(ifp, mp);
1505 if (((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) ||
1509 mp = drbr_peek(ifp, fp->tx_br);
1512 mtx_unlock(&fp->tx_mtx);
1514 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1515 goto qla_fp_taskqueue_exit;
1517 qla_fp_taskqueue_exit0:
1519 if (rx_pkts_left || ((mp != NULL) && ret)) {
1520 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1522 if (!ha->stop_rcv) {
1523 QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1527 qla_fp_taskqueue_exit:
1529 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1534 qla_create_fp_taskqueues(qla_host_t *ha)
1537 uint8_t tq_name[32];
1539 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1541 qla_tx_fp_t *fp = &ha->tx_fp[i];
1543 bzero(tq_name, sizeof (tq_name));
1544 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1546 TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1548 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1549 taskqueue_thread_enqueue,
1552 if (fp->fp_taskqueue == NULL)
1555 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1558 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1566 qla_destroy_fp_taskqueues(qla_host_t *ha)
1570 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1572 qla_tx_fp_t *fp = &ha->tx_fp[i];
1574 if (fp->fp_taskqueue != NULL) {
1575 taskqueue_drain_all(fp->fp_taskqueue);
1576 taskqueue_free(fp->fp_taskqueue);
1577 fp->fp_taskqueue = NULL;
1584 qla_drain_fp_taskqueues(qla_host_t *ha)
1588 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1589 qla_tx_fp_t *fp = &ha->tx_fp[i];
1591 if (fp->fp_taskqueue != NULL) {
1592 taskqueue_drain_all(fp->fp_taskqueue);
1599 qla_transmit(struct ifnet *ifp, struct mbuf *mp)
1601 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1606 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1608 #if __FreeBSD_version >= 1100000
1609 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1611 if (mp->m_flags & M_FLOWID)
1613 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1614 ha->hw.num_sds_rings;
1615 fp = &ha->tx_fp[rss_id];
1617 if (fp->tx_br == NULL) {
1619 goto qla_transmit_exit;
1623 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1626 if (fp->fp_taskqueue != NULL)
1627 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1633 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1638 qla_qflush(struct ifnet *ifp)
1645 ha = (qla_host_t *)ifp->if_softc;
1647 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1649 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1657 mtx_lock(&fp->tx_mtx);
1659 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1662 mtx_unlock(&fp->tx_mtx);
1665 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1671 qla_stop(qla_host_t *ha)
1673 struct ifnet *ifp = ha->ifp;
1677 ql_sp_log(ha, 13, 0, 0, 0, 0, 0, 0);
1681 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1682 ha->qla_watchdog_pause = 1;
1684 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1692 if (fp->tx_br != NULL) {
1693 mtx_lock(&fp->tx_mtx);
1694 mtx_unlock(&fp->tx_mtx);
1698 while (!ha->qla_watchdog_paused)
1699 qla_mdelay(__func__, 1);
1701 ha->qla_interface_up = 0;
1703 qla_drain_fp_taskqueues(ha);
1707 qla_free_xmt_bufs(ha);
1708 qla_free_rcv_bufs(ha);
1714 * Buffer Management Functions for Transmit and Receive Rings
1717 qla_alloc_xmt_bufs(qla_host_t *ha)
1723 if (bus_dma_tag_create(NULL, /* parent */
1724 1, 0, /* alignment, bounds */
1725 BUS_SPACE_MAXADDR, /* lowaddr */
1726 BUS_SPACE_MAXADDR, /* highaddr */
1727 NULL, NULL, /* filter, filterarg */
1728 QLA_MAX_TSO_FRAME_SIZE, /* maxsize */
1729 QLA_MAX_SEGMENTS, /* nsegments */
1730 PAGE_SIZE, /* maxsegsize */
1731 BUS_DMA_ALLOCNOW, /* flags */
1732 NULL, /* lockfunc */
1733 NULL, /* lockfuncarg */
1735 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1740 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1741 bzero((void *)ha->tx_ring[i].tx_buf,
1742 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1745 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1746 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1748 txb = &ha->tx_ring[j].tx_buf[i];
1750 if ((ret = bus_dmamap_create(ha->tx_tag,
1751 BUS_DMA_NOWAIT, &txb->map))) {
1753 ha->err_tx_dmamap_create++;
1754 device_printf(ha->pci_dev,
1755 "%s: bus_dmamap_create failed[%d]\n",
1758 qla_free_xmt_bufs(ha);
1769 * Release mbuf after it sent on the wire
1772 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1774 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1777 bus_dmamap_sync(ha->tx_tag, txb->map,
1778 BUS_DMASYNC_POSTWRITE);
1780 bus_dmamap_unload(ha->tx_tag, txb->map);
1782 m_freem(txb->m_head);
1785 bus_dmamap_destroy(ha->tx_tag, txb->map);
1790 bus_dmamap_unload(ha->tx_tag, txb->map);
1791 bus_dmamap_destroy(ha->tx_tag, txb->map);
1795 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1799 qla_free_xmt_bufs(qla_host_t *ha)
1803 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1804 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1805 qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1808 if (ha->tx_tag != NULL) {
1809 bus_dma_tag_destroy(ha->tx_tag);
1813 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1814 bzero((void *)ha->tx_ring[i].tx_buf,
1815 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1822 qla_alloc_rcv_std(qla_host_t *ha)
1824 int i, j, k, r, ret = 0;
1826 qla_rx_ring_t *rx_ring;
1828 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1830 rx_ring = &ha->rx_ring[r];
1832 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1834 rxb = &rx_ring->rx_buf[i];
1836 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1840 device_printf(ha->pci_dev,
1841 "%s: dmamap[%d, %d] failed\n",
1844 for (k = 0; k < r; k++) {
1845 for (j = 0; j < NUM_RX_DESCRIPTORS;
1847 rxb = &ha->rx_ring[k].rx_buf[j];
1848 bus_dmamap_destroy(ha->rx_tag,
1853 for (j = 0; j < i; j++) {
1854 bus_dmamap_destroy(ha->rx_tag,
1855 rx_ring->rx_buf[j].map);
1857 goto qla_alloc_rcv_std_err;
1862 qla_init_hw_rcv_descriptors(ha);
1865 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1867 rx_ring = &ha->rx_ring[r];
1869 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1870 rxb = &rx_ring->rx_buf[i];
1872 if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1874 * set the physical address in the
1875 * corresponding descriptor entry in the
1876 * receive ring/queue for the hba
1878 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1880 (rxb->m_head)->m_pkthdr.len);
1882 device_printf(ha->pci_dev,
1883 "%s: ql_get_mbuf [%d, %d] failed\n",
1885 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1886 goto qla_alloc_rcv_std_err;
1892 qla_alloc_rcv_std_err:
1897 qla_free_rcv_std(qla_host_t *ha)
1902 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1903 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1904 rxb = &ha->rx_ring[r].rx_buf[i];
1905 if (rxb->m_head != NULL) {
1906 bus_dmamap_unload(ha->rx_tag, rxb->map);
1907 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1908 m_freem(rxb->m_head);
1917 qla_alloc_rcv_bufs(qla_host_t *ha)
1921 if (bus_dma_tag_create(NULL, /* parent */
1922 1, 0, /* alignment, bounds */
1923 BUS_SPACE_MAXADDR, /* lowaddr */
1924 BUS_SPACE_MAXADDR, /* highaddr */
1925 NULL, NULL, /* filter, filterarg */
1926 MJUM9BYTES, /* maxsize */
1928 MJUM9BYTES, /* maxsegsize */
1929 BUS_DMA_ALLOCNOW, /* flags */
1930 NULL, /* lockfunc */
1931 NULL, /* lockfuncarg */
1934 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1940 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1942 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1943 ha->hw.sds[i].sdsr_next = 0;
1944 ha->hw.sds[i].rxb_free = NULL;
1945 ha->hw.sds[i].rx_free = 0;
1948 ret = qla_alloc_rcv_std(ha);
1954 qla_free_rcv_bufs(qla_host_t *ha)
1958 qla_free_rcv_std(ha);
1960 if (ha->rx_tag != NULL) {
1961 bus_dma_tag_destroy(ha->rx_tag);
1965 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1967 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1968 ha->hw.sds[i].sdsr_next = 0;
1969 ha->hw.sds[i].rxb_free = NULL;
1970 ha->hw.sds[i].rx_free = 0;
1977 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1979 register struct mbuf *mp = nmp;
1983 bus_dma_segment_t segs[1];
1984 int nsegs, mbuf_size;
1986 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1990 if (ha->hw.enable_9kb)
1991 mbuf_size = MJUM9BYTES;
1993 mbuf_size = MCLBYTES;
1997 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
2000 if (ha->hw.enable_9kb)
2001 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
2003 mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2008 device_printf(ha->pci_dev,
2009 "%s: m_getcl failed\n", __func__);
2010 goto exit_ql_get_mbuf;
2012 mp->m_len = mp->m_pkthdr.len = mbuf_size;
2014 mp->m_len = mp->m_pkthdr.len = mbuf_size;
2015 mp->m_data = mp->m_ext.ext_buf;
2019 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
2021 offset = 8 - offset;
2026 * Using memory from the mbuf cluster pool, invoke the bus_dma
2027 * machinery to arrange the memory mapping.
2029 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
2030 mp, segs, &nsegs, BUS_DMA_NOWAIT);
2031 rxb->paddr = segs[0].ds_addr;
2033 if (ret || !rxb->paddr || (nsegs != 1)) {
2036 device_printf(ha->pci_dev,
2037 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
2038 __func__, ret, (long long unsigned int)rxb->paddr,
2041 goto exit_ql_get_mbuf;
2044 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
2047 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
2053 qla_get_peer(qla_host_t *ha)
2057 int my_slot = pci_get_slot(ha->pci_dev);
2059 if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
2062 for (i = 0; i < count; i++) {
2063 slot = pci_get_slot(peers[i]);
2065 if ((slot >= 0) && (slot == my_slot) &&
2066 (pci_get_device(peers[i]) ==
2067 pci_get_device(ha->pci_dev))) {
2068 if (ha->pci_dev != peers[i])
2069 ha->peer_dev = peers[i];
2075 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
2077 qla_host_t *ha_peer;
2080 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
2082 ha_peer->msg_from_peer = msg_to_peer;
2088 qla_set_error_recovery(qla_host_t *ha)
2090 struct ifnet *ifp = ha->ifp;
2092 if (!cold && ha->enable_error_recovery) {
2094 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2095 ha->qla_initiate_recovery = 1;
2102 qla_error_recovery(void *context, int pending)
2104 qla_host_t *ha = context;
2105 uint32_t msecs_100 = 400;
2106 struct ifnet *ifp = ha->ifp;
2109 device_printf(ha->pci_dev, "%s: enter\n", __func__);
2110 ha->hw.imd_compl = 1;
2112 taskqueue_drain_all(ha->stats_tq);
2113 taskqueue_drain_all(ha->async_event_tq);
2115 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2118 device_printf(ha->pci_dev, "%s: ts_usecs = %ld start\n",
2119 __func__, qla_get_usec_timestamp());
2121 if (ha->qla_interface_up) {
2123 qla_mdelay(__func__, 300);
2125 //ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2127 for (i = 0; i < ha->hw.num_sds_rings; i++) {
2135 if (fp->tx_br != NULL) {
2136 mtx_lock(&fp->tx_mtx);
2137 mtx_unlock(&fp->tx_mtx);
2142 qla_drain_fp_taskqueues(ha);
2144 if ((ha->pci_func & 0x1) == 0) {
2146 if (!ha->msg_from_peer) {
2147 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2149 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2151 qla_mdelay(__func__, 100);
2154 ha->msg_from_peer = 0;
2156 if (ha->enable_minidump)
2159 if (ha->enable_driverstate_dump)
2160 ql_capture_drvr_state(ha);
2162 if (ql_init_hw(ha)) {
2163 device_printf(ha->pci_dev,
2164 "%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2165 __func__, qla_get_usec_timestamp());
2167 goto qla_error_recovery_exit;
2170 if (ha->qla_interface_up) {
2171 qla_free_xmt_bufs(ha);
2172 qla_free_rcv_bufs(ha);
2175 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2176 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2179 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2181 ha->msg_from_peer = 0;
2183 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2184 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2186 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2189 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) && msecs_100--)
2190 qla_mdelay(__func__, 100);
2191 ha->msg_from_peer = 0;
2193 if (ha->enable_driverstate_dump)
2194 ql_capture_drvr_state(ha);
2196 if (msecs_100 == 0) {
2197 device_printf(ha->pci_dev,
2198 "%s: ts_usecs = %ld exit: QL_PEER_MSG_ACK not received\n",
2199 __func__, qla_get_usec_timestamp());
2201 goto qla_error_recovery_exit;
2204 if (ql_init_hw(ha)) {
2205 device_printf(ha->pci_dev,
2206 "%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2207 __func__, qla_get_usec_timestamp());
2209 goto qla_error_recovery_exit;
2212 if (ha->qla_interface_up) {
2213 qla_free_xmt_bufs(ha);
2214 qla_free_rcv_bufs(ha);
2218 qla_mdelay(__func__, ha->ms_delay_after_init);
2220 *((uint32_t *)&ha->hw.flags) = 0;
2221 ha->qla_initiate_recovery = 0;
2223 if (ha->qla_interface_up) {
2225 if (qla_alloc_xmt_bufs(ha) != 0) {
2227 goto qla_error_recovery_exit;
2230 qla_confirm_9kb_enable(ha);
2232 if (qla_alloc_rcv_bufs(ha) != 0) {
2234 goto qla_error_recovery_exit;
2239 if (ql_init_hw_if(ha) == 0) {
2241 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2242 ha->qla_watchdog_pause = 0;
2243 ql_update_link_state(ha);
2247 if (ha->hw.sp_log_stop_events &
2248 Q8_SP_LOG_STOP_IF_START_FAILURE)
2249 ha->hw.sp_log_stop = -1;
2252 ha->qla_watchdog_pause = 0;
2255 qla_error_recovery_exit:
2258 device_printf(ha->pci_dev, "%s: ts_usecs = %ld port offline\n",
2259 __func__, qla_get_usec_timestamp());
2260 if (ha->hw.sp_log_stop_events &
2261 Q8_SP_LOG_STOP_ERR_RECOVERY_FAILURE)
2262 ha->hw.sp_log_stop = -1;
2266 QLA_UNLOCK(ha, __func__);
2269 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2272 device_printf(ha->pci_dev,
2273 "%s: ts_usecs = %ld exit\n",
2274 __func__, qla_get_usec_timestamp());
2279 qla_async_event(void *context, int pending)
2281 qla_host_t *ha = context;
2283 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2286 if (ha->async_event) {
2287 ha->async_event = 0;
2288 qla_hw_async_event(ha);
2291 QLA_UNLOCK(ha, __func__);
2297 qla_stats(void *context, int pending)