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;
268 if (ha->qla_watchdog_exit) {
269 ha->qla_watchdog_exited = 1;
272 ha->qla_watchdog_exited = 0;
274 if (!ha->qla_watchdog_pause) {
276 (ql_hw_check_health(ha) || ha->qla_initiate_recovery ||
277 (ha->msg_from_peer == QL_PEER_MSG_RESET))) {
278 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
279 ql_update_link_state(ha);
281 if (ha->enable_error_recovery) {
282 ha->qla_watchdog_paused = 1;
283 ha->qla_watchdog_pause = 1;
285 device_printf(ha->pci_dev,
286 "%s: taskqueue_enqueue(err_task) \n",
288 taskqueue_enqueue(ha->err_tq, &ha->err_task);
291 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
297 if (ha->qla_interface_up) {
298 ha->watchdog_ticks++;
300 if (ha->watchdog_ticks > 1000)
301 ha->watchdog_ticks = 0;
303 if (!ha->watchdog_ticks && QL_RUNNING(ifp)) {
304 taskqueue_enqueue(ha->stats_tq,
308 if (ha->async_event) {
309 taskqueue_enqueue(ha->async_event_tq,
310 &ha->async_event_task);
313 ha->qla_watchdog_paused = 0;
316 ha->qla_watchdog_paused = 1;
319 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
324 * Name: qla_pci_attach
325 * Function: attaches the device to the operating system
328 qla_pci_attach(device_t dev)
330 qla_host_t *ha = NULL;
333 uint32_t num_rcvq = 0;
335 if ((ha = device_get_softc(dev)) == NULL) {
336 device_printf(dev, "cannot get softc\n");
340 memset(ha, 0, sizeof (qla_host_t));
342 if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8030) {
343 device_printf(dev, "device is not ISP8030\n");
347 ha->pci_func = pci_get_function(dev) & 0x1;
351 pci_enable_busmaster(dev);
353 ha->reg_rid = PCIR_BAR(0);
354 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
357 if (ha->pci_reg == NULL) {
358 device_printf(dev, "unable to map any ports\n");
359 goto qla_pci_attach_err;
362 rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
365 mtx_init(&ha->hw_lock, "qla83xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
366 mtx_init(&ha->sp_log_lock, "qla83xx_sp_log_lock", MTX_NETWORK_LOCK, MTX_DEF);
367 ha->flags.lock_init = 1;
371 ha->hw.num_sds_rings = MAX_SDS_RINGS;
372 ha->hw.num_rds_rings = MAX_RDS_RINGS;
373 ha->hw.num_tx_rings = NUM_TX_RINGS;
375 ha->reg_rid1 = PCIR_BAR(2);
376 ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
377 &ha->reg_rid1, RF_ACTIVE);
379 ha->msix_count = pci_msix_count(dev);
381 if (ha->msix_count < 1 ) {
382 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
384 goto qla_pci_attach_err;
387 if (ha->msix_count < (ha->hw.num_sds_rings + 1)) {
388 ha->hw.num_sds_rings = ha->msix_count - 1;
391 QL_DPRINT2(ha, (dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
392 " msix_count 0x%x pci_reg %p pci_reg1 %p\n", __func__, ha,
393 ha->pci_func, rsrc_len, ha->msix_count, ha->pci_reg,
396 /* initialize hardware */
397 if (ql_init_hw(ha)) {
398 device_printf(dev, "%s: ql_init_hw failed\n", __func__);
399 goto qla_pci_attach_err;
402 device_printf(dev, "%s: firmware[%d.%d.%d.%d]\n", __func__,
403 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
405 snprintf(ha->fw_ver_str, sizeof(ha->fw_ver_str), "%d.%d.%d.%d",
406 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
409 if (qla_get_nic_partition(ha, NULL, &num_rcvq)) {
410 device_printf(dev, "%s: qla_get_nic_partition failed\n",
412 goto qla_pci_attach_err;
414 device_printf(dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
415 " msix_count 0x%x pci_reg %p pci_reg1 %p num_rcvq = %d\n",
416 __func__, ha, ha->pci_func, rsrc_len, ha->msix_count,
417 ha->pci_reg, ha->pci_reg1, num_rcvq);
419 if ((ha->msix_count < 64) || (num_rcvq != 32)) {
420 if (ha->hw.num_sds_rings > 15) {
421 ha->hw.num_sds_rings = 15;
425 ha->hw.num_rds_rings = ha->hw.num_sds_rings;
426 ha->hw.num_tx_rings = ha->hw.num_sds_rings;
428 #ifdef QL_ENABLE_ISCSI_TLV
429 ha->hw.num_tx_rings = ha->hw.num_sds_rings * 2;
430 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
432 ql_hw_add_sysctls(ha);
434 ha->msix_count = ha->hw.num_sds_rings + 1;
436 if (pci_alloc_msix(dev, &ha->msix_count)) {
437 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
440 goto qla_pci_attach_err;
444 ha->mbx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
446 (RF_ACTIVE | RF_SHAREABLE));
447 if (ha->mbx_irq == NULL) {
448 device_printf(dev, "could not allocate mbx interrupt\n");
449 goto qla_pci_attach_err;
451 if (bus_setup_intr(dev, ha->mbx_irq, (INTR_TYPE_NET | INTR_MPSAFE),
452 NULL, ql_mbx_isr, ha, &ha->mbx_handle)) {
453 device_printf(dev, "could not setup mbx interrupt\n");
454 goto qla_pci_attach_err;
457 for (i = 0; i < ha->hw.num_sds_rings; i++) {
458 ha->irq_vec[i].sds_idx = i;
459 ha->irq_vec[i].ha = ha;
460 ha->irq_vec[i].irq_rid = 2 + i;
462 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
463 &ha->irq_vec[i].irq_rid,
464 (RF_ACTIVE | RF_SHAREABLE));
466 if (ha->irq_vec[i].irq == NULL) {
467 device_printf(dev, "could not allocate interrupt\n");
468 goto qla_pci_attach_err;
470 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
471 (INTR_TYPE_NET | INTR_MPSAFE),
472 NULL, ql_isr, &ha->irq_vec[i],
473 &ha->irq_vec[i].handle)) {
474 device_printf(dev, "could not setup interrupt\n");
475 goto qla_pci_attach_err;
478 ha->tx_fp[i].ha = ha;
479 ha->tx_fp[i].txr_idx = i;
481 if (qla_alloc_tx_br(ha, &ha->tx_fp[i])) {
482 device_printf(dev, "%s: could not allocate tx_br[%d]\n",
484 goto qla_pci_attach_err;
488 if (qla_create_fp_taskqueues(ha) != 0)
489 goto qla_pci_attach_err;
491 printf("%s: mp__ncpus %d sds %d rds %d msi-x %d\n", __func__, mp_ncpus,
492 ha->hw.num_sds_rings, ha->hw.num_rds_rings, ha->msix_count);
494 ql_read_mac_addr(ha);
496 /* allocate parent dma tag */
497 if (qla_alloc_parent_dma_tag(ha)) {
498 device_printf(dev, "%s: qla_alloc_parent_dma_tag failed\n",
500 goto qla_pci_attach_err;
503 /* alloc all dma buffers */
504 if (ql_alloc_dma(ha)) {
505 device_printf(dev, "%s: ql_alloc_dma failed\n", __func__);
506 goto qla_pci_attach_err;
510 if (ql_minidump_init(ha) != 0) {
511 device_printf(dev, "%s: ql_minidump_init failed\n", __func__);
512 goto qla_pci_attach_err;
514 ql_alloc_drvr_state_buffer(ha);
515 ql_alloc_sp_log_buffer(ha);
516 /* create the o.s ethernet interface */
517 qla_init_ifnet(dev, ha);
519 ha->flags.qla_watchdog_active = 1;
520 ha->qla_watchdog_pause = 0;
522 callout_init(&ha->tx_callout, TRUE);
523 ha->flags.qla_callout_init = 1;
525 /* create ioctl device interface */
526 if (ql_make_cdev(ha)) {
527 device_printf(dev, "%s: ql_make_cdev failed\n", __func__);
528 goto qla_pci_attach_err;
531 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
534 TASK_INIT(&ha->err_task, 0, qla_error_recovery, ha);
535 ha->err_tq = taskqueue_create("qla_errq", M_NOWAIT,
536 taskqueue_thread_enqueue, &ha->err_tq);
537 taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
538 device_get_nameunit(ha->pci_dev));
540 TASK_INIT(&ha->async_event_task, 0, qla_async_event, ha);
541 ha->async_event_tq = taskqueue_create("qla_asyncq", M_NOWAIT,
542 taskqueue_thread_enqueue, &ha->async_event_tq);
543 taskqueue_start_threads(&ha->async_event_tq, 1, PI_NET, "%s asyncq",
544 device_get_nameunit(ha->pci_dev));
546 TASK_INIT(&ha->stats_task, 0, qla_stats, ha);
547 ha->stats_tq = taskqueue_create("qla_statsq", M_NOWAIT,
548 taskqueue_thread_enqueue, &ha->stats_tq);
549 taskqueue_start_threads(&ha->stats_tq, 1, PI_NET, "%s taskq",
550 device_get_nameunit(ha->pci_dev));
552 QL_DPRINT2(ha, (dev, "%s: exit 0\n", __func__));
559 if (ha->flags.lock_init) {
560 mtx_destroy(&ha->hw_lock);
561 mtx_destroy(&ha->sp_log_lock);
564 QL_DPRINT2(ha, (dev, "%s: exit ENXIO\n", __func__));
569 * Name: qla_pci_detach
570 * Function: Unhooks the device from the operating system
573 qla_pci_detach(device_t dev)
575 qla_host_t *ha = NULL;
578 if ((ha = device_get_softc(dev)) == NULL) {
579 device_printf(dev, "cannot get softc\n");
583 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
587 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
588 QLA_LOCK(ha, __func__, -1, 0);
590 ha->qla_detach_active = 1;
595 QLA_UNLOCK(ha, __func__);
597 if (ha->flags.lock_init) {
598 mtx_destroy(&ha->hw_lock);
599 mtx_destroy(&ha->sp_log_lock);
602 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
608 * SYSCTL Related Callbacks
611 qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS)
616 err = sysctl_handle_int(oidp, &ret, 0, req);
618 if (err || !req->newptr)
622 ha = (qla_host_t *)arg1;
623 ql_hw_link_status(ha);
630 * Function: Releases the resources allocated for the device
633 qla_release(qla_host_t *ha)
640 if (ha->async_event_tq) {
641 taskqueue_drain_all(ha->async_event_tq);
642 taskqueue_free(ha->async_event_tq);
646 taskqueue_drain_all(ha->err_tq);
647 taskqueue_free(ha->err_tq);
651 taskqueue_drain_all(ha->stats_tq);
652 taskqueue_free(ha->stats_tq);
657 if (ha->flags.qla_watchdog_active) {
658 ha->qla_watchdog_exit = 1;
660 while (ha->qla_watchdog_exited == 0)
661 qla_mdelay(__func__, 1);
664 if (ha->flags.qla_callout_init)
665 callout_stop(&ha->tx_callout);
668 ether_ifdetach(ha->ifp);
670 ql_free_drvr_state_buffer(ha);
671 ql_free_sp_log_buffer(ha);
673 qla_free_parent_dma_tag(ha);
676 (void)bus_teardown_intr(dev, ha->mbx_irq, ha->mbx_handle);
679 (void) bus_release_resource(dev, SYS_RES_IRQ, ha->mbx_irq_rid,
682 for (i = 0; i < ha->hw.num_sds_rings; i++) {
683 if (ha->irq_vec[i].handle) {
684 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
685 ha->irq_vec[i].handle);
688 if (ha->irq_vec[i].irq) {
689 (void)bus_release_resource(dev, SYS_RES_IRQ,
690 ha->irq_vec[i].irq_rid,
694 qla_free_tx_br(ha, &ha->tx_fp[i]);
696 qla_destroy_fp_taskqueues(ha);
699 pci_release_msi(dev);
702 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
706 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
713 * DMA Related Functions
717 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
719 *((bus_addr_t *)arg) = 0;
722 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
726 *((bus_addr_t *)arg) = segs[0].ds_addr;
732 ql_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
740 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
742 ret = bus_dma_tag_create(
743 ha->parent_tag,/* parent */
745 ((bus_size_t)(1ULL << 32)),/* boundary */
746 BUS_SPACE_MAXADDR, /* lowaddr */
747 BUS_SPACE_MAXADDR, /* highaddr */
748 NULL, NULL, /* filter, filterarg */
749 dma_buf->size, /* maxsize */
751 dma_buf->size, /* maxsegsize */
753 NULL, NULL, /* lockfunc, lockarg */
757 device_printf(dev, "%s: could not create dma tag\n", __func__);
758 goto ql_alloc_dmabuf_exit;
760 ret = bus_dmamem_alloc(dma_buf->dma_tag,
761 (void **)&dma_buf->dma_b,
762 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
765 bus_dma_tag_destroy(dma_buf->dma_tag);
766 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
767 goto ql_alloc_dmabuf_exit;
770 ret = bus_dmamap_load(dma_buf->dma_tag,
775 &b_addr, BUS_DMA_NOWAIT);
777 if (ret || !b_addr) {
778 bus_dma_tag_destroy(dma_buf->dma_tag);
779 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
782 goto ql_alloc_dmabuf_exit;
785 dma_buf->dma_addr = b_addr;
787 ql_alloc_dmabuf_exit:
788 QL_DPRINT2(ha, (dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
789 __func__, ret, (void *)dma_buf->dma_tag,
790 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
797 ql_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
799 bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
800 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
801 bus_dma_tag_destroy(dma_buf->dma_tag);
805 qla_alloc_parent_dma_tag(qla_host_t *ha)
813 * Allocate parent DMA Tag
815 ret = bus_dma_tag_create(
816 bus_get_dma_tag(dev), /* parent */
817 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
818 BUS_SPACE_MAXADDR, /* lowaddr */
819 BUS_SPACE_MAXADDR, /* highaddr */
820 NULL, NULL, /* filter, filterarg */
821 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
823 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
825 NULL, NULL, /* lockfunc, lockarg */
829 device_printf(dev, "%s: could not create parent dma tag\n",
834 ha->flags.parent_tag = 1;
840 qla_free_parent_dma_tag(qla_host_t *ha)
842 if (ha->flags.parent_tag) {
843 bus_dma_tag_destroy(ha->parent_tag);
844 ha->flags.parent_tag = 0;
849 * Name: qla_init_ifnet
850 * Function: Creates the Network Device Interface and Registers it with the O.S
854 qla_init_ifnet(device_t dev, qla_host_t *ha)
858 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
860 ifp = ha->ifp = if_alloc(IFT_ETHER);
863 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
865 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
867 ifp->if_baudrate = IF_Gbps(10);
868 ifp->if_capabilities = IFCAP_LINKSTATE;
869 ifp->if_mtu = ETHERMTU;
871 ifp->if_init = qla_init;
873 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
874 ifp->if_ioctl = qla_ioctl;
876 ifp->if_transmit = qla_transmit;
877 ifp->if_qflush = qla_qflush;
879 IFQ_SET_MAXLEN(&ifp->if_snd, qla_get_ifq_snd_maxlen(ha));
880 ifp->if_snd.ifq_drv_maxlen = qla_get_ifq_snd_maxlen(ha);
881 IFQ_SET_READY(&ifp->if_snd);
883 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
885 ether_ifattach(ifp, qla_get_mac_addr(ha));
887 ifp->if_capabilities |= IFCAP_HWCSUM |
891 IFCAP_VLAN_HWTAGGING |
896 ifp->if_capenable = ifp->if_capabilities;
898 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
900 ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
902 ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
904 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
906 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
908 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
914 qla_init_locked(qla_host_t *ha)
916 struct ifnet *ifp = ha->ifp;
918 ql_sp_log(ha, 14, 0, 0, 0, 0, 0, 0);
922 if (qla_alloc_xmt_bufs(ha) != 0)
925 qla_confirm_9kb_enable(ha);
927 if (qla_alloc_rcv_bufs(ha) != 0)
930 bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
932 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
933 ifp->if_hwassist |= CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
936 if (ql_init_hw_if(ha) == 0) {
938 ifp->if_drv_flags |= IFF_DRV_RUNNING;
939 ha->hw_vlan_tx_frames = 0;
940 ha->tx_tso_frames = 0;
941 ha->qla_interface_up = 1;
942 ql_update_link_state(ha);
944 if (ha->hw.sp_log_stop_events & Q8_SP_LOG_STOP_IF_START_FAILURE)
945 ha->hw.sp_log_stop = -1;
948 ha->qla_watchdog_pause = 0;
958 ha = (qla_host_t *)arg;
960 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
962 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
967 QLA_UNLOCK(ha, __func__);
969 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
973 qla_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int mcnt)
977 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
980 bcopy(LLADDR(sdl), &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
986 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
988 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
990 struct ifnet *ifp = ha->ifp;
993 mcnt = if_foreach_llmaddr(ifp, qla_copy_maddr, mta);
995 if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
996 QLA_LOCK_NO_SLEEP) != 0)
999 ql_sp_log(ha, 12, 4, ifp->if_drv_flags,
1000 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1001 add_multi, (uint32_t)mcnt, 0);
1003 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1005 ret = qla_hw_del_all_mcast(ha);
1008 device_printf(ha->pci_dev,
1009 "%s: qla_hw_del_all_mcast() failed\n",
1014 ret = ql_hw_set_multi(ha, mta, mcnt, 1);
1017 QLA_UNLOCK(ha, __func__);
1023 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1026 struct ifreq *ifr = (struct ifreq *)data;
1027 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",
1039 if (ifa->ifa_addr->sa_family == AF_INET) {
1040 ret = QLA_LOCK(ha, __func__,
1041 QLA_LOCK_DEFAULT_MS_TIMEOUT,
1046 ifp->if_flags |= IFF_UP;
1048 ql_sp_log(ha, 8, 3, ifp->if_drv_flags,
1049 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1050 ntohl(IA_SIN(ifa)->sin_addr.s_addr), 0, 0);
1052 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1053 qla_init_locked(ha);
1056 QLA_UNLOCK(ha, __func__);
1057 QL_DPRINT4(ha, (ha->pci_dev,
1058 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1060 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1062 arp_ifinit(ifp, ifa);
1064 ether_ioctl(ifp, cmd, data);
1069 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1072 if (ifr->ifr_mtu > QLA_MAX_MTU) {
1075 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1081 ifp->if_mtu = ifr->ifr_mtu;
1082 ha->max_frame_size =
1083 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1085 ql_sp_log(ha, 9, 4, ifp->if_drv_flags,
1086 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1087 ha->max_frame_size, ifp->if_mtu, 0);
1089 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1090 qla_init_locked(ha);
1093 if (ifp->if_mtu > ETHERMTU)
1094 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1096 ha->std_replenish = QL_STD_REPLENISH_THRES;
1099 QLA_UNLOCK(ha, __func__);
1105 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1108 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1114 ql_sp_log(ha, 10, 4, ifp->if_drv_flags,
1115 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1116 ha->if_flags, ifp->if_flags, 0);
1118 if (ifp->if_flags & IFF_UP) {
1119 ha->max_frame_size = ifp->if_mtu +
1120 ETHER_HDR_LEN + ETHER_CRC_LEN;
1121 qla_init_locked(ha);
1123 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1124 if ((ifp->if_flags ^ ha->if_flags) &
1126 ret = ql_set_promisc(ha);
1127 } else if ((ifp->if_flags ^ ha->if_flags) &
1129 ret = ql_set_allmulti(ha);
1133 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1135 ha->if_flags = ifp->if_flags;
1138 QLA_UNLOCK(ha, __func__);
1142 QL_DPRINT4(ha, (ha->pci_dev,
1143 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1145 if (qla_set_multi(ha, 1))
1150 QL_DPRINT4(ha, (ha->pci_dev,
1151 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1153 if (qla_set_multi(ha, 0))
1159 QL_DPRINT4(ha, (ha->pci_dev,
1160 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1162 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1167 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1169 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1172 if (mask & IFCAP_HWCSUM)
1173 ifp->if_capenable ^= IFCAP_HWCSUM;
1174 if (mask & IFCAP_TSO4)
1175 ifp->if_capenable ^= IFCAP_TSO4;
1176 if (mask & IFCAP_TSO6)
1177 ifp->if_capenable ^= IFCAP_TSO6;
1178 if (mask & IFCAP_VLAN_HWTAGGING)
1179 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1180 if (mask & IFCAP_VLAN_HWTSO)
1181 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1182 if (mask & IFCAP_LRO)
1183 ifp->if_capenable ^= IFCAP_LRO;
1185 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1186 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1192 ql_sp_log(ha, 11, 4, ifp->if_drv_flags,
1193 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1194 mask, ifp->if_capenable, 0);
1196 qla_init_locked(ha);
1198 QLA_UNLOCK(ha, __func__);
1200 VLAN_CAPABILITIES(ifp);
1205 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1207 ret = ether_ioctl(ifp, cmd, data);
1215 qla_media_change(struct ifnet *ifp)
1218 struct ifmedia *ifm;
1221 ha = (qla_host_t *)ifp->if_softc;
1223 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1227 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1230 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1236 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1240 ha = (qla_host_t *)ifp->if_softc;
1242 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1244 ifmr->ifm_status = IFM_AVALID;
1245 ifmr->ifm_active = IFM_ETHER;
1247 ql_update_link_state(ha);
1248 if (ha->hw.link_up) {
1249 ifmr->ifm_status |= IFM_ACTIVE;
1250 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1253 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1254 (ha->hw.link_up ? "link_up" : "link_down")));
1260 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1263 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1268 struct mbuf *m_head = *m_headp;
1270 QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1272 tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1274 if ((NULL != ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head) ||
1275 (QL_ERR_INJECT(ha, INJCT_TXBUF_MBUF_NON_NULL))){
1276 QL_ASSERT(ha, 0, ("%s [%d]: txr_idx = %d tx_idx = %d "\
1277 "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,\
1278 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head));
1280 device_printf(ha->pci_dev, "%s [%d]: txr_idx = %d tx_idx = %d "
1281 "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,
1282 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head);
1287 QL_INITIATE_RECOVERY(ha);
1291 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1293 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1299 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1300 m_head->m_pkthdr.len));
1302 m = m_defrag(m_head, M_NOWAIT);
1304 ha->err_tx_defrag++;
1307 device_printf(ha->pci_dev,
1308 "%s: m_defrag() = NULL [%d]\n",
1315 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1316 segs, &nsegs, BUS_DMA_NOWAIT))) {
1317 ha->err_tx_dmamap_load++;
1319 device_printf(ha->pci_dev,
1320 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1321 __func__, ret, m_head->m_pkthdr.len);
1323 if (ret != ENOMEM) {
1331 ha->err_tx_dmamap_load++;
1333 device_printf(ha->pci_dev,
1334 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1335 __func__, ret, m_head->m_pkthdr.len);
1337 if (ret != ENOMEM) {
1344 QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1346 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1348 if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1350 ha->tx_ring[txr_idx].count++;
1352 ha->tx_ring[txr_idx].iscsi_pkt_count++;
1353 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1355 bus_dmamap_unload(ha->tx_tag, map);
1356 if (ret == EINVAL) {
1363 QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1368 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1370 snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1371 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1373 mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1375 fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1376 M_NOWAIT, &fp->tx_mtx);
1377 if (fp->tx_br == NULL) {
1378 QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1379 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1386 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1389 struct ifnet *ifp = ha->ifp;
1391 if (mtx_initialized(&fp->tx_mtx)) {
1392 if (fp->tx_br != NULL) {
1393 mtx_lock(&fp->tx_mtx);
1395 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1399 mtx_unlock(&fp->tx_mtx);
1401 buf_ring_free(fp->tx_br, M_DEVBUF);
1404 mtx_destroy(&fp->tx_mtx);
1410 qla_fp_taskqueue(void *context, int pending)
1415 struct mbuf *mp = NULL;
1418 uint32_t iscsi_pdu = 0;
1419 uint32_t rx_pkts_left = -1;
1426 ha = (qla_host_t *)fp->ha;
1430 txr_idx = fp->txr_idx;
1432 mtx_lock(&fp->tx_mtx);
1434 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1435 mtx_unlock(&fp->tx_mtx);
1436 goto qla_fp_taskqueue_exit;
1439 while (rx_pkts_left && !ha->stop_rcv &&
1440 (ifp->if_drv_flags & IFF_DRV_RUNNING) && ha->hw.link_up) {
1441 rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1443 #ifdef QL_ENABLE_ISCSI_TLV
1444 ql_hw_tx_done_locked(ha, fp->txr_idx);
1445 ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1447 ql_hw_tx_done_locked(ha, fp->txr_idx);
1448 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1450 mp = drbr_peek(ifp, fp->tx_br);
1452 while (mp != NULL) {
1453 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1454 #ifdef QL_ENABLE_ISCSI_TLV
1455 if (ql_iscsi_pdu(ha, mp) == 0) {
1457 (ha->hw.num_tx_rings >> 1);
1461 txr_idx = fp->txr_idx;
1463 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1466 ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1470 drbr_putback(ifp, fp->tx_br, mp);
1472 drbr_advance(ifp, fp->tx_br);
1475 mtx_unlock(&fp->tx_mtx);
1477 goto qla_fp_taskqueue_exit0;
1479 drbr_advance(ifp, fp->tx_br);
1482 /* Send a copy of the frame to the BPF listener */
1483 ETHER_BPF_MTAP(ifp, mp);
1485 if (((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) ||
1489 mp = drbr_peek(ifp, fp->tx_br);
1492 mtx_unlock(&fp->tx_mtx);
1494 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1495 goto qla_fp_taskqueue_exit;
1497 qla_fp_taskqueue_exit0:
1499 if (rx_pkts_left || ((mp != NULL) && ret)) {
1500 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1502 if (!ha->stop_rcv) {
1503 QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1507 qla_fp_taskqueue_exit:
1509 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1514 qla_create_fp_taskqueues(qla_host_t *ha)
1517 uint8_t tq_name[32];
1519 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1520 qla_tx_fp_t *fp = &ha->tx_fp[i];
1522 bzero(tq_name, sizeof (tq_name));
1523 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1525 NET_TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1527 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1528 taskqueue_thread_enqueue,
1531 if (fp->fp_taskqueue == NULL)
1534 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1537 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1545 qla_destroy_fp_taskqueues(qla_host_t *ha)
1549 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1550 qla_tx_fp_t *fp = &ha->tx_fp[i];
1552 if (fp->fp_taskqueue != NULL) {
1553 taskqueue_drain_all(fp->fp_taskqueue);
1554 taskqueue_free(fp->fp_taskqueue);
1555 fp->fp_taskqueue = NULL;
1562 qla_drain_fp_taskqueues(qla_host_t *ha)
1566 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1567 qla_tx_fp_t *fp = &ha->tx_fp[i];
1569 if (fp->fp_taskqueue != NULL) {
1570 taskqueue_drain_all(fp->fp_taskqueue);
1577 qla_transmit(struct ifnet *ifp, struct mbuf *mp)
1579 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1584 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1586 #if __FreeBSD_version >= 1100000
1587 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1589 if (mp->m_flags & M_FLOWID)
1591 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1592 ha->hw.num_sds_rings;
1593 fp = &ha->tx_fp[rss_id];
1595 if (fp->tx_br == NULL) {
1597 goto qla_transmit_exit;
1601 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1604 if (fp->fp_taskqueue != NULL)
1605 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1611 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1616 qla_qflush(struct ifnet *ifp)
1623 ha = (qla_host_t *)ifp->if_softc;
1625 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1627 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1634 mtx_lock(&fp->tx_mtx);
1636 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1639 mtx_unlock(&fp->tx_mtx);
1642 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1648 qla_stop(qla_host_t *ha)
1650 struct ifnet *ifp = ha->ifp;
1654 ql_sp_log(ha, 13, 0, 0, 0, 0, 0, 0);
1658 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1659 ha->qla_watchdog_pause = 1;
1661 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1669 if (fp->tx_br != NULL) {
1670 mtx_lock(&fp->tx_mtx);
1671 mtx_unlock(&fp->tx_mtx);
1675 while (!ha->qla_watchdog_paused)
1676 qla_mdelay(__func__, 1);
1678 ha->qla_interface_up = 0;
1680 qla_drain_fp_taskqueues(ha);
1684 qla_free_xmt_bufs(ha);
1685 qla_free_rcv_bufs(ha);
1691 * Buffer Management Functions for Transmit and Receive Rings
1694 qla_alloc_xmt_bufs(qla_host_t *ha)
1700 if (bus_dma_tag_create(NULL, /* parent */
1701 1, 0, /* alignment, bounds */
1702 BUS_SPACE_MAXADDR, /* lowaddr */
1703 BUS_SPACE_MAXADDR, /* highaddr */
1704 NULL, NULL, /* filter, filterarg */
1705 QLA_MAX_TSO_FRAME_SIZE, /* maxsize */
1706 QLA_MAX_SEGMENTS, /* nsegments */
1707 PAGE_SIZE, /* maxsegsize */
1708 BUS_DMA_ALLOCNOW, /* flags */
1709 NULL, /* lockfunc */
1710 NULL, /* lockfuncarg */
1712 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1717 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1718 bzero((void *)ha->tx_ring[i].tx_buf,
1719 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1722 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1723 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1724 txb = &ha->tx_ring[j].tx_buf[i];
1726 if ((ret = bus_dmamap_create(ha->tx_tag,
1727 BUS_DMA_NOWAIT, &txb->map))) {
1728 ha->err_tx_dmamap_create++;
1729 device_printf(ha->pci_dev,
1730 "%s: bus_dmamap_create failed[%d]\n",
1733 qla_free_xmt_bufs(ha);
1744 * Release mbuf after it sent on the wire
1747 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1749 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1752 bus_dmamap_sync(ha->tx_tag, txb->map,
1753 BUS_DMASYNC_POSTWRITE);
1755 bus_dmamap_unload(ha->tx_tag, txb->map);
1757 m_freem(txb->m_head);
1760 bus_dmamap_destroy(ha->tx_tag, txb->map);
1765 bus_dmamap_unload(ha->tx_tag, txb->map);
1766 bus_dmamap_destroy(ha->tx_tag, txb->map);
1770 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1774 qla_free_xmt_bufs(qla_host_t *ha)
1778 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1779 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1780 qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1783 if (ha->tx_tag != NULL) {
1784 bus_dma_tag_destroy(ha->tx_tag);
1788 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1789 bzero((void *)ha->tx_ring[i].tx_buf,
1790 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1796 qla_alloc_rcv_std(qla_host_t *ha)
1798 int i, j, k, r, ret = 0;
1800 qla_rx_ring_t *rx_ring;
1802 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1803 rx_ring = &ha->rx_ring[r];
1805 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1806 rxb = &rx_ring->rx_buf[i];
1808 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1812 device_printf(ha->pci_dev,
1813 "%s: dmamap[%d, %d] failed\n",
1816 for (k = 0; k < r; k++) {
1817 for (j = 0; j < NUM_RX_DESCRIPTORS;
1819 rxb = &ha->rx_ring[k].rx_buf[j];
1820 bus_dmamap_destroy(ha->rx_tag,
1825 for (j = 0; j < i; j++) {
1826 bus_dmamap_destroy(ha->rx_tag,
1827 rx_ring->rx_buf[j].map);
1829 goto qla_alloc_rcv_std_err;
1834 qla_init_hw_rcv_descriptors(ha);
1836 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1837 rx_ring = &ha->rx_ring[r];
1839 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1840 rxb = &rx_ring->rx_buf[i];
1842 if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1844 * set the physical address in the
1845 * corresponding descriptor entry in the
1846 * receive ring/queue for the hba
1848 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1850 (rxb->m_head)->m_pkthdr.len);
1852 device_printf(ha->pci_dev,
1853 "%s: ql_get_mbuf [%d, %d] failed\n",
1855 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1856 goto qla_alloc_rcv_std_err;
1862 qla_alloc_rcv_std_err:
1867 qla_free_rcv_std(qla_host_t *ha)
1872 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1873 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1874 rxb = &ha->rx_ring[r].rx_buf[i];
1875 if (rxb->m_head != NULL) {
1876 bus_dmamap_unload(ha->rx_tag, rxb->map);
1877 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1878 m_freem(rxb->m_head);
1887 qla_alloc_rcv_bufs(qla_host_t *ha)
1891 if (bus_dma_tag_create(NULL, /* parent */
1892 1, 0, /* alignment, bounds */
1893 BUS_SPACE_MAXADDR, /* lowaddr */
1894 BUS_SPACE_MAXADDR, /* highaddr */
1895 NULL, NULL, /* filter, filterarg */
1896 MJUM9BYTES, /* maxsize */
1898 MJUM9BYTES, /* maxsegsize */
1899 BUS_DMA_ALLOCNOW, /* flags */
1900 NULL, /* lockfunc */
1901 NULL, /* lockfuncarg */
1903 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1909 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1911 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1912 ha->hw.sds[i].sdsr_next = 0;
1913 ha->hw.sds[i].rxb_free = NULL;
1914 ha->hw.sds[i].rx_free = 0;
1917 ret = qla_alloc_rcv_std(ha);
1923 qla_free_rcv_bufs(qla_host_t *ha)
1927 qla_free_rcv_std(ha);
1929 if (ha->rx_tag != NULL) {
1930 bus_dma_tag_destroy(ha->rx_tag);
1934 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1936 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1937 ha->hw.sds[i].sdsr_next = 0;
1938 ha->hw.sds[i].rxb_free = NULL;
1939 ha->hw.sds[i].rx_free = 0;
1946 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1948 register struct mbuf *mp = nmp;
1952 bus_dma_segment_t segs[1];
1953 int nsegs, mbuf_size;
1955 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1959 if (ha->hw.enable_9kb)
1960 mbuf_size = MJUM9BYTES;
1962 mbuf_size = MCLBYTES;
1965 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1968 if (ha->hw.enable_9kb)
1969 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
1971 mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1976 device_printf(ha->pci_dev,
1977 "%s: m_getcl failed\n", __func__);
1978 goto exit_ql_get_mbuf;
1980 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1982 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1983 mp->m_data = mp->m_ext.ext_buf;
1987 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1989 offset = 8 - offset;
1994 * Using memory from the mbuf cluster pool, invoke the bus_dma
1995 * machinery to arrange the memory mapping.
1997 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1998 mp, segs, &nsegs, BUS_DMA_NOWAIT);
1999 rxb->paddr = segs[0].ds_addr;
2001 if (ret || !rxb->paddr || (nsegs != 1)) {
2004 device_printf(ha->pci_dev,
2005 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
2006 __func__, ret, (long long unsigned int)rxb->paddr,
2009 goto exit_ql_get_mbuf;
2012 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
2015 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
2020 qla_get_peer(qla_host_t *ha)
2024 int my_slot = pci_get_slot(ha->pci_dev);
2026 if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
2029 for (i = 0; i < count; i++) {
2030 slot = pci_get_slot(peers[i]);
2032 if ((slot >= 0) && (slot == my_slot) &&
2033 (pci_get_device(peers[i]) ==
2034 pci_get_device(ha->pci_dev))) {
2035 if (ha->pci_dev != peers[i])
2036 ha->peer_dev = peers[i];
2042 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
2044 qla_host_t *ha_peer;
2047 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
2048 ha_peer->msg_from_peer = msg_to_peer;
2054 qla_set_error_recovery(qla_host_t *ha)
2056 struct ifnet *ifp = ha->ifp;
2058 if (!cold && ha->enable_error_recovery) {
2060 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2061 ha->qla_initiate_recovery = 1;
2068 qla_error_recovery(void *context, int pending)
2070 qla_host_t *ha = context;
2071 uint32_t msecs_100 = 400;
2072 struct ifnet *ifp = ha->ifp;
2075 device_printf(ha->pci_dev, "%s: enter\n", __func__);
2076 ha->hw.imd_compl = 1;
2078 taskqueue_drain_all(ha->stats_tq);
2079 taskqueue_drain_all(ha->async_event_tq);
2081 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2084 device_printf(ha->pci_dev, "%s: ts_usecs = %ld start\n",
2085 __func__, qla_get_usec_timestamp());
2087 if (ha->qla_interface_up) {
2088 qla_mdelay(__func__, 300);
2090 //ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2092 for (i = 0; i < ha->hw.num_sds_rings; i++) {
2100 if (fp->tx_br != NULL) {
2101 mtx_lock(&fp->tx_mtx);
2102 mtx_unlock(&fp->tx_mtx);
2107 qla_drain_fp_taskqueues(ha);
2109 if ((ha->pci_func & 0x1) == 0) {
2110 if (!ha->msg_from_peer) {
2111 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2113 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2115 qla_mdelay(__func__, 100);
2118 ha->msg_from_peer = 0;
2120 if (ha->enable_minidump)
2123 if (ha->enable_driverstate_dump)
2124 ql_capture_drvr_state(ha);
2126 if (ql_init_hw(ha)) {
2127 device_printf(ha->pci_dev,
2128 "%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2129 __func__, qla_get_usec_timestamp());
2131 goto qla_error_recovery_exit;
2134 if (ha->qla_interface_up) {
2135 qla_free_xmt_bufs(ha);
2136 qla_free_rcv_bufs(ha);
2139 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2140 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2143 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2144 ha->msg_from_peer = 0;
2146 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2147 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2149 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2152 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) && msecs_100--)
2153 qla_mdelay(__func__, 100);
2154 ha->msg_from_peer = 0;
2156 if (ha->enable_driverstate_dump)
2157 ql_capture_drvr_state(ha);
2159 if (msecs_100 == 0) {
2160 device_printf(ha->pci_dev,
2161 "%s: ts_usecs = %ld exit: QL_PEER_MSG_ACK not received\n",
2162 __func__, qla_get_usec_timestamp());
2164 goto qla_error_recovery_exit;
2167 if (ql_init_hw(ha)) {
2168 device_printf(ha->pci_dev,
2169 "%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2170 __func__, qla_get_usec_timestamp());
2172 goto qla_error_recovery_exit;
2175 if (ha->qla_interface_up) {
2176 qla_free_xmt_bufs(ha);
2177 qla_free_rcv_bufs(ha);
2181 qla_mdelay(__func__, ha->ms_delay_after_init);
2183 *((uint32_t *)&ha->hw.flags) = 0;
2184 ha->qla_initiate_recovery = 0;
2186 if (ha->qla_interface_up) {
2187 if (qla_alloc_xmt_bufs(ha) != 0) {
2189 goto qla_error_recovery_exit;
2192 qla_confirm_9kb_enable(ha);
2194 if (qla_alloc_rcv_bufs(ha) != 0) {
2196 goto qla_error_recovery_exit;
2201 if (ql_init_hw_if(ha) == 0) {
2203 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2204 ha->qla_watchdog_pause = 0;
2205 ql_update_link_state(ha);
2209 if (ha->hw.sp_log_stop_events &
2210 Q8_SP_LOG_STOP_IF_START_FAILURE)
2211 ha->hw.sp_log_stop = -1;
2214 ha->qla_watchdog_pause = 0;
2217 qla_error_recovery_exit:
2220 device_printf(ha->pci_dev, "%s: ts_usecs = %ld port offline\n",
2221 __func__, qla_get_usec_timestamp());
2222 if (ha->hw.sp_log_stop_events &
2223 Q8_SP_LOG_STOP_ERR_RECOVERY_FAILURE)
2224 ha->hw.sp_log_stop = -1;
2227 QLA_UNLOCK(ha, __func__);
2230 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2233 device_printf(ha->pci_dev,
2234 "%s: ts_usecs = %ld exit\n",
2235 __func__, qla_get_usec_timestamp());
2240 qla_async_event(void *context, int pending)
2242 qla_host_t *ha = context;
2244 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2247 if (ha->async_event) {
2248 ha->async_event = 0;
2249 qla_hw_async_event(ha);
2252 QLA_UNLOCK(ha, __func__);
2258 qla_stats(void *context, int pending)