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_set_multi(qla_host_t *ha, uint32_t add_multi)
983 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
984 struct ifmultiaddr *ifma;
986 struct ifnet *ifp = ha->ifp;
991 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
993 if (ifma->ifma_addr->sa_family != AF_LINK)
996 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
999 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
1000 &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
1005 if_maddr_runlock(ifp);
1007 if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1008 QLA_LOCK_NO_SLEEP) != 0)
1011 ql_sp_log(ha, 12, 4, ifp->if_drv_flags,
1012 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1013 add_multi, (uint32_t)mcnt, 0);
1015 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1018 ret = qla_hw_del_all_mcast(ha);
1021 device_printf(ha->pci_dev,
1022 "%s: qla_hw_del_all_mcast() failed\n",
1027 ret = ql_hw_set_multi(ha, mta, mcnt, 1);
1031 QLA_UNLOCK(ha, __func__);
1037 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1040 struct ifreq *ifr = (struct ifreq *)data;
1041 struct ifaddr *ifa = (struct ifaddr *)data;
1044 ha = (qla_host_t *)ifp->if_softc;
1045 if (ha->offline || ha->qla_initiate_recovery)
1050 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
1053 if (ifa->ifa_addr->sa_family == AF_INET) {
1055 ret = QLA_LOCK(ha, __func__,
1056 QLA_LOCK_DEFAULT_MS_TIMEOUT,
1061 ifp->if_flags |= IFF_UP;
1063 ql_sp_log(ha, 8, 3, ifp->if_drv_flags,
1064 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1065 ntohl(IA_SIN(ifa)->sin_addr.s_addr), 0, 0);
1067 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1068 qla_init_locked(ha);
1071 QLA_UNLOCK(ha, __func__);
1072 QL_DPRINT4(ha, (ha->pci_dev,
1073 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1075 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1077 arp_ifinit(ifp, ifa);
1079 ether_ioctl(ifp, cmd, data);
1084 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1087 if (ifr->ifr_mtu > QLA_MAX_MTU) {
1090 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1096 ifp->if_mtu = ifr->ifr_mtu;
1097 ha->max_frame_size =
1098 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1100 ql_sp_log(ha, 9, 4, ifp->if_drv_flags,
1101 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1102 ha->max_frame_size, ifp->if_mtu, 0);
1104 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1105 qla_init_locked(ha);
1108 if (ifp->if_mtu > ETHERMTU)
1109 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1111 ha->std_replenish = QL_STD_REPLENISH_THRES;
1114 QLA_UNLOCK(ha, __func__);
1120 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1123 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1129 ql_sp_log(ha, 10, 4, ifp->if_drv_flags,
1130 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1131 ha->if_flags, ifp->if_flags, 0);
1133 if (ifp->if_flags & IFF_UP) {
1135 ha->max_frame_size = ifp->if_mtu +
1136 ETHER_HDR_LEN + ETHER_CRC_LEN;
1137 qla_init_locked(ha);
1139 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1140 if ((ifp->if_flags ^ ha->if_flags) &
1142 ret = ql_set_promisc(ha);
1143 } else if ((ifp->if_flags ^ ha->if_flags) &
1145 ret = ql_set_allmulti(ha);
1149 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1151 ha->if_flags = ifp->if_flags;
1154 QLA_UNLOCK(ha, __func__);
1158 QL_DPRINT4(ha, (ha->pci_dev,
1159 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1161 if (qla_set_multi(ha, 1))
1166 QL_DPRINT4(ha, (ha->pci_dev,
1167 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1169 if (qla_set_multi(ha, 0))
1175 QL_DPRINT4(ha, (ha->pci_dev,
1176 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1178 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1183 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1185 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1188 if (mask & IFCAP_HWCSUM)
1189 ifp->if_capenable ^= IFCAP_HWCSUM;
1190 if (mask & IFCAP_TSO4)
1191 ifp->if_capenable ^= IFCAP_TSO4;
1192 if (mask & IFCAP_TSO6)
1193 ifp->if_capenable ^= IFCAP_TSO6;
1194 if (mask & IFCAP_VLAN_HWTAGGING)
1195 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1196 if (mask & IFCAP_VLAN_HWTSO)
1197 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1198 if (mask & IFCAP_LRO)
1199 ifp->if_capenable ^= IFCAP_LRO;
1201 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1202 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1208 ql_sp_log(ha, 11, 4, ifp->if_drv_flags,
1209 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1210 mask, ifp->if_capenable, 0);
1212 qla_init_locked(ha);
1214 QLA_UNLOCK(ha, __func__);
1217 VLAN_CAPABILITIES(ifp);
1222 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1224 ret = ether_ioctl(ifp, cmd, data);
1232 qla_media_change(struct ifnet *ifp)
1235 struct ifmedia *ifm;
1238 ha = (qla_host_t *)ifp->if_softc;
1240 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1244 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1247 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1253 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1257 ha = (qla_host_t *)ifp->if_softc;
1259 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1261 ifmr->ifm_status = IFM_AVALID;
1262 ifmr->ifm_active = IFM_ETHER;
1264 ql_update_link_state(ha);
1265 if (ha->hw.link_up) {
1266 ifmr->ifm_status |= IFM_ACTIVE;
1267 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1270 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1271 (ha->hw.link_up ? "link_up" : "link_down")));
1278 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1281 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1286 struct mbuf *m_head = *m_headp;
1288 QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1290 tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1292 if ((NULL != ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head) ||
1293 (QL_ERR_INJECT(ha, INJCT_TXBUF_MBUF_NON_NULL))){
1294 QL_ASSERT(ha, 0, ("%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));
1298 device_printf(ha->pci_dev, "%s [%d]: txr_idx = %d tx_idx = %d "
1299 "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,
1300 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head);
1305 QL_INITIATE_RECOVERY(ha);
1309 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1311 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1318 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1319 m_head->m_pkthdr.len));
1321 m = m_defrag(m_head, M_NOWAIT);
1323 ha->err_tx_defrag++;
1326 device_printf(ha->pci_dev,
1327 "%s: m_defrag() = NULL [%d]\n",
1334 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1335 segs, &nsegs, BUS_DMA_NOWAIT))) {
1337 ha->err_tx_dmamap_load++;
1339 device_printf(ha->pci_dev,
1340 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1341 __func__, ret, m_head->m_pkthdr.len);
1343 if (ret != ENOMEM) {
1352 ha->err_tx_dmamap_load++;
1354 device_printf(ha->pci_dev,
1355 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1356 __func__, ret, m_head->m_pkthdr.len);
1358 if (ret != ENOMEM) {
1365 QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1367 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1369 if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1371 ha->tx_ring[txr_idx].count++;
1373 ha->tx_ring[txr_idx].iscsi_pkt_count++;
1374 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1376 bus_dmamap_unload(ha->tx_tag, map);
1377 if (ret == EINVAL) {
1384 QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1389 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1391 snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1392 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1394 mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1396 fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1397 M_NOWAIT, &fp->tx_mtx);
1398 if (fp->tx_br == NULL) {
1399 QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1400 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1407 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1410 struct ifnet *ifp = ha->ifp;
1412 if (mtx_initialized(&fp->tx_mtx)) {
1414 if (fp->tx_br != NULL) {
1416 mtx_lock(&fp->tx_mtx);
1418 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1422 mtx_unlock(&fp->tx_mtx);
1424 buf_ring_free(fp->tx_br, M_DEVBUF);
1427 mtx_destroy(&fp->tx_mtx);
1433 qla_fp_taskqueue(void *context, int pending)
1438 struct mbuf *mp = NULL;
1441 uint32_t iscsi_pdu = 0;
1442 uint32_t rx_pkts_left = -1;
1449 ha = (qla_host_t *)fp->ha;
1453 txr_idx = fp->txr_idx;
1455 mtx_lock(&fp->tx_mtx);
1457 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1458 mtx_unlock(&fp->tx_mtx);
1459 goto qla_fp_taskqueue_exit;
1462 while (rx_pkts_left && !ha->stop_rcv &&
1463 (ifp->if_drv_flags & IFF_DRV_RUNNING) && ha->hw.link_up) {
1464 rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1466 #ifdef QL_ENABLE_ISCSI_TLV
1467 ql_hw_tx_done_locked(ha, fp->txr_idx);
1468 ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1470 ql_hw_tx_done_locked(ha, fp->txr_idx);
1471 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1473 mp = drbr_peek(ifp, fp->tx_br);
1475 while (mp != NULL) {
1477 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1478 #ifdef QL_ENABLE_ISCSI_TLV
1479 if (ql_iscsi_pdu(ha, mp) == 0) {
1481 (ha->hw.num_tx_rings >> 1);
1485 txr_idx = fp->txr_idx;
1487 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1490 ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1494 drbr_putback(ifp, fp->tx_br, mp);
1496 drbr_advance(ifp, fp->tx_br);
1499 mtx_unlock(&fp->tx_mtx);
1501 goto qla_fp_taskqueue_exit0;
1503 drbr_advance(ifp, fp->tx_br);
1506 /* Send a copy of the frame to the BPF listener */
1507 ETHER_BPF_MTAP(ifp, mp);
1509 if (((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) ||
1513 mp = drbr_peek(ifp, fp->tx_br);
1516 mtx_unlock(&fp->tx_mtx);
1518 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1519 goto qla_fp_taskqueue_exit;
1521 qla_fp_taskqueue_exit0:
1523 if (rx_pkts_left || ((mp != NULL) && ret)) {
1524 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1526 if (!ha->stop_rcv) {
1527 QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1531 qla_fp_taskqueue_exit:
1533 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1538 qla_create_fp_taskqueues(qla_host_t *ha)
1541 uint8_t tq_name[32];
1543 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1545 qla_tx_fp_t *fp = &ha->tx_fp[i];
1547 bzero(tq_name, sizeof (tq_name));
1548 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1550 TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1552 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1553 taskqueue_thread_enqueue,
1556 if (fp->fp_taskqueue == NULL)
1559 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1562 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1570 qla_destroy_fp_taskqueues(qla_host_t *ha)
1574 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1576 qla_tx_fp_t *fp = &ha->tx_fp[i];
1578 if (fp->fp_taskqueue != NULL) {
1579 taskqueue_drain_all(fp->fp_taskqueue);
1580 taskqueue_free(fp->fp_taskqueue);
1581 fp->fp_taskqueue = NULL;
1588 qla_drain_fp_taskqueues(qla_host_t *ha)
1592 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1593 qla_tx_fp_t *fp = &ha->tx_fp[i];
1595 if (fp->fp_taskqueue != NULL) {
1596 taskqueue_drain_all(fp->fp_taskqueue);
1603 qla_transmit(struct ifnet *ifp, struct mbuf *mp)
1605 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1610 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1612 #if __FreeBSD_version >= 1100000
1613 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1615 if (mp->m_flags & M_FLOWID)
1617 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1618 ha->hw.num_sds_rings;
1619 fp = &ha->tx_fp[rss_id];
1621 if (fp->tx_br == NULL) {
1623 goto qla_transmit_exit;
1627 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1630 if (fp->fp_taskqueue != NULL)
1631 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1637 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1642 qla_qflush(struct ifnet *ifp)
1649 ha = (qla_host_t *)ifp->if_softc;
1651 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1653 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1661 mtx_lock(&fp->tx_mtx);
1663 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1666 mtx_unlock(&fp->tx_mtx);
1669 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1675 qla_stop(qla_host_t *ha)
1677 struct ifnet *ifp = ha->ifp;
1681 ql_sp_log(ha, 13, 0, 0, 0, 0, 0, 0);
1685 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1686 ha->qla_watchdog_pause = 1;
1688 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1696 if (fp->tx_br != NULL) {
1697 mtx_lock(&fp->tx_mtx);
1698 mtx_unlock(&fp->tx_mtx);
1702 while (!ha->qla_watchdog_paused)
1703 qla_mdelay(__func__, 1);
1705 ha->qla_interface_up = 0;
1707 qla_drain_fp_taskqueues(ha);
1711 qla_free_xmt_bufs(ha);
1712 qla_free_rcv_bufs(ha);
1718 * Buffer Management Functions for Transmit and Receive Rings
1721 qla_alloc_xmt_bufs(qla_host_t *ha)
1727 if (bus_dma_tag_create(NULL, /* parent */
1728 1, 0, /* alignment, bounds */
1729 BUS_SPACE_MAXADDR, /* lowaddr */
1730 BUS_SPACE_MAXADDR, /* highaddr */
1731 NULL, NULL, /* filter, filterarg */
1732 QLA_MAX_TSO_FRAME_SIZE, /* maxsize */
1733 QLA_MAX_SEGMENTS, /* nsegments */
1734 PAGE_SIZE, /* maxsegsize */
1735 BUS_DMA_ALLOCNOW, /* flags */
1736 NULL, /* lockfunc */
1737 NULL, /* lockfuncarg */
1739 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1744 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1745 bzero((void *)ha->tx_ring[i].tx_buf,
1746 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1749 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1750 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1752 txb = &ha->tx_ring[j].tx_buf[i];
1754 if ((ret = bus_dmamap_create(ha->tx_tag,
1755 BUS_DMA_NOWAIT, &txb->map))) {
1757 ha->err_tx_dmamap_create++;
1758 device_printf(ha->pci_dev,
1759 "%s: bus_dmamap_create failed[%d]\n",
1762 qla_free_xmt_bufs(ha);
1773 * Release mbuf after it sent on the wire
1776 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1778 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1781 bus_dmamap_sync(ha->tx_tag, txb->map,
1782 BUS_DMASYNC_POSTWRITE);
1784 bus_dmamap_unload(ha->tx_tag, txb->map);
1786 m_freem(txb->m_head);
1789 bus_dmamap_destroy(ha->tx_tag, txb->map);
1794 bus_dmamap_unload(ha->tx_tag, txb->map);
1795 bus_dmamap_destroy(ha->tx_tag, txb->map);
1799 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1803 qla_free_xmt_bufs(qla_host_t *ha)
1807 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1808 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1809 qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1812 if (ha->tx_tag != NULL) {
1813 bus_dma_tag_destroy(ha->tx_tag);
1817 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1818 bzero((void *)ha->tx_ring[i].tx_buf,
1819 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1826 qla_alloc_rcv_std(qla_host_t *ha)
1828 int i, j, k, r, ret = 0;
1830 qla_rx_ring_t *rx_ring;
1832 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1834 rx_ring = &ha->rx_ring[r];
1836 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1838 rxb = &rx_ring->rx_buf[i];
1840 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1844 device_printf(ha->pci_dev,
1845 "%s: dmamap[%d, %d] failed\n",
1848 for (k = 0; k < r; k++) {
1849 for (j = 0; j < NUM_RX_DESCRIPTORS;
1851 rxb = &ha->rx_ring[k].rx_buf[j];
1852 bus_dmamap_destroy(ha->rx_tag,
1857 for (j = 0; j < i; j++) {
1858 bus_dmamap_destroy(ha->rx_tag,
1859 rx_ring->rx_buf[j].map);
1861 goto qla_alloc_rcv_std_err;
1866 qla_init_hw_rcv_descriptors(ha);
1869 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1871 rx_ring = &ha->rx_ring[r];
1873 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1874 rxb = &rx_ring->rx_buf[i];
1876 if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1878 * set the physical address in the
1879 * corresponding descriptor entry in the
1880 * receive ring/queue for the hba
1882 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1884 (rxb->m_head)->m_pkthdr.len);
1886 device_printf(ha->pci_dev,
1887 "%s: ql_get_mbuf [%d, %d] failed\n",
1889 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1890 goto qla_alloc_rcv_std_err;
1896 qla_alloc_rcv_std_err:
1901 qla_free_rcv_std(qla_host_t *ha)
1906 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1907 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1908 rxb = &ha->rx_ring[r].rx_buf[i];
1909 if (rxb->m_head != NULL) {
1910 bus_dmamap_unload(ha->rx_tag, rxb->map);
1911 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1912 m_freem(rxb->m_head);
1921 qla_alloc_rcv_bufs(qla_host_t *ha)
1925 if (bus_dma_tag_create(NULL, /* parent */
1926 1, 0, /* alignment, bounds */
1927 BUS_SPACE_MAXADDR, /* lowaddr */
1928 BUS_SPACE_MAXADDR, /* highaddr */
1929 NULL, NULL, /* filter, filterarg */
1930 MJUM9BYTES, /* maxsize */
1932 MJUM9BYTES, /* maxsegsize */
1933 BUS_DMA_ALLOCNOW, /* flags */
1934 NULL, /* lockfunc */
1935 NULL, /* lockfuncarg */
1938 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1944 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1946 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1947 ha->hw.sds[i].sdsr_next = 0;
1948 ha->hw.sds[i].rxb_free = NULL;
1949 ha->hw.sds[i].rx_free = 0;
1952 ret = qla_alloc_rcv_std(ha);
1958 qla_free_rcv_bufs(qla_host_t *ha)
1962 qla_free_rcv_std(ha);
1964 if (ha->rx_tag != NULL) {
1965 bus_dma_tag_destroy(ha->rx_tag);
1969 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1971 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1972 ha->hw.sds[i].sdsr_next = 0;
1973 ha->hw.sds[i].rxb_free = NULL;
1974 ha->hw.sds[i].rx_free = 0;
1981 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1983 register struct mbuf *mp = nmp;
1987 bus_dma_segment_t segs[1];
1988 int nsegs, mbuf_size;
1990 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1994 if (ha->hw.enable_9kb)
1995 mbuf_size = MJUM9BYTES;
1997 mbuf_size = MCLBYTES;
2001 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
2004 if (ha->hw.enable_9kb)
2005 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
2007 mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2012 device_printf(ha->pci_dev,
2013 "%s: m_getcl failed\n", __func__);
2014 goto exit_ql_get_mbuf;
2016 mp->m_len = mp->m_pkthdr.len = mbuf_size;
2018 mp->m_len = mp->m_pkthdr.len = mbuf_size;
2019 mp->m_data = mp->m_ext.ext_buf;
2023 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
2025 offset = 8 - offset;
2030 * Using memory from the mbuf cluster pool, invoke the bus_dma
2031 * machinery to arrange the memory mapping.
2033 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
2034 mp, segs, &nsegs, BUS_DMA_NOWAIT);
2035 rxb->paddr = segs[0].ds_addr;
2037 if (ret || !rxb->paddr || (nsegs != 1)) {
2040 device_printf(ha->pci_dev,
2041 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
2042 __func__, ret, (long long unsigned int)rxb->paddr,
2045 goto exit_ql_get_mbuf;
2048 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
2051 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
2057 qla_get_peer(qla_host_t *ha)
2061 int my_slot = pci_get_slot(ha->pci_dev);
2063 if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
2066 for (i = 0; i < count; i++) {
2067 slot = pci_get_slot(peers[i]);
2069 if ((slot >= 0) && (slot == my_slot) &&
2070 (pci_get_device(peers[i]) ==
2071 pci_get_device(ha->pci_dev))) {
2072 if (ha->pci_dev != peers[i])
2073 ha->peer_dev = peers[i];
2079 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
2081 qla_host_t *ha_peer;
2084 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
2086 ha_peer->msg_from_peer = msg_to_peer;
2092 qla_set_error_recovery(qla_host_t *ha)
2094 struct ifnet *ifp = ha->ifp;
2096 if (!cold && ha->enable_error_recovery) {
2098 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2099 ha->qla_initiate_recovery = 1;
2106 qla_error_recovery(void *context, int pending)
2108 qla_host_t *ha = context;
2109 uint32_t msecs_100 = 400;
2110 struct ifnet *ifp = ha->ifp;
2113 device_printf(ha->pci_dev, "%s: enter\n", __func__);
2114 ha->hw.imd_compl = 1;
2116 taskqueue_drain_all(ha->stats_tq);
2117 taskqueue_drain_all(ha->async_event_tq);
2119 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2122 device_printf(ha->pci_dev, "%s: ts_usecs = %ld start\n",
2123 __func__, qla_get_usec_timestamp());
2125 if (ha->qla_interface_up) {
2127 qla_mdelay(__func__, 300);
2129 //ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2131 for (i = 0; i < ha->hw.num_sds_rings; i++) {
2139 if (fp->tx_br != NULL) {
2140 mtx_lock(&fp->tx_mtx);
2141 mtx_unlock(&fp->tx_mtx);
2146 qla_drain_fp_taskqueues(ha);
2148 if ((ha->pci_func & 0x1) == 0) {
2150 if (!ha->msg_from_peer) {
2151 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2153 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2155 qla_mdelay(__func__, 100);
2158 ha->msg_from_peer = 0;
2160 if (ha->enable_minidump)
2163 if (ha->enable_driverstate_dump)
2164 ql_capture_drvr_state(ha);
2166 if (ql_init_hw(ha)) {
2167 device_printf(ha->pci_dev,
2168 "%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2169 __func__, qla_get_usec_timestamp());
2171 goto qla_error_recovery_exit;
2174 if (ha->qla_interface_up) {
2175 qla_free_xmt_bufs(ha);
2176 qla_free_rcv_bufs(ha);
2179 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2180 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2183 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2185 ha->msg_from_peer = 0;
2187 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2188 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2190 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2193 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) && msecs_100--)
2194 qla_mdelay(__func__, 100);
2195 ha->msg_from_peer = 0;
2197 if (ha->enable_driverstate_dump)
2198 ql_capture_drvr_state(ha);
2200 if (msecs_100 == 0) {
2201 device_printf(ha->pci_dev,
2202 "%s: ts_usecs = %ld exit: QL_PEER_MSG_ACK not received\n",
2203 __func__, qla_get_usec_timestamp());
2205 goto qla_error_recovery_exit;
2208 if (ql_init_hw(ha)) {
2209 device_printf(ha->pci_dev,
2210 "%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2211 __func__, qla_get_usec_timestamp());
2213 goto qla_error_recovery_exit;
2216 if (ha->qla_interface_up) {
2217 qla_free_xmt_bufs(ha);
2218 qla_free_rcv_bufs(ha);
2222 qla_mdelay(__func__, ha->ms_delay_after_init);
2224 *((uint32_t *)&ha->hw.flags) = 0;
2225 ha->qla_initiate_recovery = 0;
2227 if (ha->qla_interface_up) {
2229 if (qla_alloc_xmt_bufs(ha) != 0) {
2231 goto qla_error_recovery_exit;
2234 qla_confirm_9kb_enable(ha);
2236 if (qla_alloc_rcv_bufs(ha) != 0) {
2238 goto qla_error_recovery_exit;
2243 if (ql_init_hw_if(ha) == 0) {
2245 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2246 ha->qla_watchdog_pause = 0;
2247 ql_update_link_state(ha);
2251 if (ha->hw.sp_log_stop_events &
2252 Q8_SP_LOG_STOP_IF_START_FAILURE)
2253 ha->hw.sp_log_stop = -1;
2256 ha->qla_watchdog_pause = 0;
2259 qla_error_recovery_exit:
2262 device_printf(ha->pci_dev, "%s: ts_usecs = %ld port offline\n",
2263 __func__, qla_get_usec_timestamp());
2264 if (ha->hw.sp_log_stop_events &
2265 Q8_SP_LOG_STOP_ERR_RECOVERY_FAILURE)
2266 ha->hw.sp_log_stop = -1;
2270 QLA_UNLOCK(ha, __func__);
2273 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2276 device_printf(ha->pci_dev,
2277 "%s: ts_usecs = %ld exit\n",
2278 __func__, qla_get_usec_timestamp());
2283 qla_async_event(void *context, int pending)
2285 qla_host_t *ha = context;
2287 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2290 if (ha->async_event) {
2291 ha->async_event = 0;
2292 qla_hw_async_event(ha);
2295 QLA_UNLOCK(ha, __func__);
2301 qla_stats(void *context, int pending)