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 is enabled only when this is set");
197 ha->std_replenish = QL_STD_REPLENISH_THRES;
198 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
199 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
200 OID_AUTO, "std_replenish", CTLFLAG_RW,
201 &ha->std_replenish, ha->std_replenish,
202 "Threshold for Replenishing Standard Frames");
204 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
205 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
206 OID_AUTO, "ipv4_lro",
207 CTLFLAG_RD, &ha->ipv4_lro,
208 "number of ipv4 lro completions");
210 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
211 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
212 OID_AUTO, "ipv6_lro",
213 CTLFLAG_RD, &ha->ipv6_lro,
214 "number of ipv6 lro completions");
216 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
217 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
218 OID_AUTO, "tx_tso_frames",
219 CTLFLAG_RD, &ha->tx_tso_frames,
220 "number of Tx TSO Frames");
222 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
223 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
224 OID_AUTO, "hw_vlan_tx_frames",
225 CTLFLAG_RD, &ha->hw_vlan_tx_frames,
226 "number of Tx VLAN Frames");
228 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
229 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
230 OID_AUTO, "hw_lock_failed",
231 CTLFLAG_RD, &ha->hw_lock_failed,
232 "number of hw_lock failures");
238 qla_watchdog(void *arg)
240 qla_host_t *ha = arg;
247 if (ha->qla_watchdog_exit) {
248 ha->qla_watchdog_exited = 1;
251 ha->qla_watchdog_exited = 0;
253 if (!ha->qla_watchdog_pause) {
254 if (ql_hw_check_health(ha) || ha->qla_initiate_recovery ||
255 (ha->msg_from_peer == QL_PEER_MSG_RESET)) {
257 if (!(ha->dbg_level & 0x8000)) {
258 ha->qla_watchdog_paused = 1;
259 ha->qla_watchdog_pause = 1;
260 ha->qla_initiate_recovery = 0;
262 device_printf(ha->pci_dev,
263 "%s: taskqueue_enqueue(err_task) \n",
265 taskqueue_enqueue(ha->err_tq, &ha->err_task);
269 } else if (ha->qla_interface_up) {
271 ha->watchdog_ticks++;
273 if (ha->watchdog_ticks > 1000)
274 ha->watchdog_ticks = 0;
276 if (!ha->watchdog_ticks && QL_RUNNING(ifp)) {
277 taskqueue_enqueue(ha->stats_tq, &ha->stats_task);
280 if (ha->async_event) {
281 taskqueue_enqueue(ha->async_event_tq,
282 &ha->async_event_task);
286 for (i = 0; ((i < ha->hw.num_sds_rings) &&
287 !ha->watchdog_ticks); i++) {
288 qla_tx_fp_t *fp = &ha->tx_fp[i];
290 if (fp->fp_taskqueue != NULL)
291 taskqueue_enqueue(fp->fp_taskqueue,
295 ha->qla_watchdog_paused = 0;
297 ha->qla_watchdog_paused = 0;
300 ha->qla_watchdog_paused = 1;
303 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
308 * Name: qla_pci_attach
309 * Function: attaches the device to the operating system
312 qla_pci_attach(device_t dev)
314 qla_host_t *ha = NULL;
317 uint32_t num_rcvq = 0;
319 if ((ha = device_get_softc(dev)) == NULL) {
320 device_printf(dev, "cannot get softc\n");
324 memset(ha, 0, sizeof (qla_host_t));
326 if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8030) {
327 device_printf(dev, "device is not ISP8030\n");
331 ha->pci_func = pci_get_function(dev) & 0x1;
335 pci_enable_busmaster(dev);
337 ha->reg_rid = PCIR_BAR(0);
338 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
341 if (ha->pci_reg == NULL) {
342 device_printf(dev, "unable to map any ports\n");
343 goto qla_pci_attach_err;
346 rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
349 mtx_init(&ha->hw_lock, "qla83xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
350 ha->flags.lock_init = 1;
354 ha->hw.num_sds_rings = MAX_SDS_RINGS;
355 ha->hw.num_rds_rings = MAX_RDS_RINGS;
356 ha->hw.num_tx_rings = NUM_TX_RINGS;
358 ha->reg_rid1 = PCIR_BAR(2);
359 ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
360 &ha->reg_rid1, RF_ACTIVE);
362 ha->msix_count = pci_msix_count(dev);
364 if (ha->msix_count < 1 ) {
365 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
367 goto qla_pci_attach_err;
370 if (ha->msix_count < (ha->hw.num_sds_rings + 1)) {
371 ha->hw.num_sds_rings = ha->msix_count - 1;
374 QL_DPRINT2(ha, (dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
375 " msix_count 0x%x pci_reg %p pci_reg1 %p\n", __func__, ha,
376 ha->pci_func, rsrc_len, ha->msix_count, ha->pci_reg,
379 /* initialize hardware */
380 if (ql_init_hw(ha)) {
381 device_printf(dev, "%s: ql_init_hw failed\n", __func__);
382 goto qla_pci_attach_err;
385 device_printf(dev, "%s: firmware[%d.%d.%d.%d]\n", __func__,
386 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
388 snprintf(ha->fw_ver_str, sizeof(ha->fw_ver_str), "%d.%d.%d.%d",
389 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
392 if (qla_get_nic_partition(ha, NULL, &num_rcvq)) {
393 device_printf(dev, "%s: qla_get_nic_partition failed\n",
395 goto qla_pci_attach_err;
397 device_printf(dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
398 " msix_count 0x%x pci_reg %p pci_reg1 %p num_rcvq = %d\n",
399 __func__, ha, ha->pci_func, rsrc_len, ha->msix_count,
400 ha->pci_reg, ha->pci_reg1, num_rcvq);
402 if ((ha->msix_count < 64) || (num_rcvq != 32)) {
403 if (ha->hw.num_sds_rings > 15) {
404 ha->hw.num_sds_rings = 15;
408 ha->hw.num_rds_rings = ha->hw.num_sds_rings;
409 ha->hw.num_tx_rings = ha->hw.num_sds_rings;
411 #ifdef QL_ENABLE_ISCSI_TLV
412 ha->hw.num_tx_rings = ha->hw.num_sds_rings * 2;
413 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
415 ql_hw_add_sysctls(ha);
417 ha->msix_count = ha->hw.num_sds_rings + 1;
419 if (pci_alloc_msix(dev, &ha->msix_count)) {
420 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
423 goto qla_pci_attach_err;
427 ha->mbx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
429 (RF_ACTIVE | RF_SHAREABLE));
430 if (ha->mbx_irq == NULL) {
431 device_printf(dev, "could not allocate mbx interrupt\n");
432 goto qla_pci_attach_err;
434 if (bus_setup_intr(dev, ha->mbx_irq, (INTR_TYPE_NET | INTR_MPSAFE),
435 NULL, ql_mbx_isr, ha, &ha->mbx_handle)) {
436 device_printf(dev, "could not setup mbx interrupt\n");
437 goto qla_pci_attach_err;
440 for (i = 0; i < ha->hw.num_sds_rings; i++) {
441 ha->irq_vec[i].sds_idx = i;
442 ha->irq_vec[i].ha = ha;
443 ha->irq_vec[i].irq_rid = 2 + i;
445 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
446 &ha->irq_vec[i].irq_rid,
447 (RF_ACTIVE | RF_SHAREABLE));
449 if (ha->irq_vec[i].irq == NULL) {
450 device_printf(dev, "could not allocate interrupt\n");
451 goto qla_pci_attach_err;
453 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
454 (INTR_TYPE_NET | INTR_MPSAFE),
455 NULL, ql_isr, &ha->irq_vec[i],
456 &ha->irq_vec[i].handle)) {
457 device_printf(dev, "could not setup interrupt\n");
458 goto qla_pci_attach_err;
461 ha->tx_fp[i].ha = ha;
462 ha->tx_fp[i].txr_idx = i;
464 if (qla_alloc_tx_br(ha, &ha->tx_fp[i])) {
465 device_printf(dev, "%s: could not allocate tx_br[%d]\n",
467 goto qla_pci_attach_err;
471 if (qla_create_fp_taskqueues(ha) != 0)
472 goto qla_pci_attach_err;
474 printf("%s: mp__ncpus %d sds %d rds %d msi-x %d\n", __func__, mp_ncpus,
475 ha->hw.num_sds_rings, ha->hw.num_rds_rings, ha->msix_count);
477 ql_read_mac_addr(ha);
479 /* allocate parent dma tag */
480 if (qla_alloc_parent_dma_tag(ha)) {
481 device_printf(dev, "%s: qla_alloc_parent_dma_tag failed\n",
483 goto qla_pci_attach_err;
486 /* alloc all dma buffers */
487 if (ql_alloc_dma(ha)) {
488 device_printf(dev, "%s: ql_alloc_dma failed\n", __func__);
489 goto qla_pci_attach_err;
493 if (ql_minidump_init(ha) != 0) {
494 device_printf(dev, "%s: ql_minidump_init failed\n", __func__);
495 goto qla_pci_attach_err;
497 ql_alloc_drvr_state_buffer(ha);
498 /* create the o.s ethernet interface */
499 qla_init_ifnet(dev, ha);
501 ha->flags.qla_watchdog_active = 1;
502 ha->qla_watchdog_pause = 0;
504 callout_init(&ha->tx_callout, TRUE);
505 ha->flags.qla_callout_init = 1;
507 /* create ioctl device interface */
508 if (ql_make_cdev(ha)) {
509 device_printf(dev, "%s: ql_make_cdev failed\n", __func__);
510 goto qla_pci_attach_err;
513 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
516 TASK_INIT(&ha->err_task, 0, qla_error_recovery, ha);
517 ha->err_tq = taskqueue_create("qla_errq", M_NOWAIT,
518 taskqueue_thread_enqueue, &ha->err_tq);
519 taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
520 device_get_nameunit(ha->pci_dev));
522 TASK_INIT(&ha->async_event_task, 0, qla_async_event, ha);
523 ha->async_event_tq = taskqueue_create("qla_asyncq", M_NOWAIT,
524 taskqueue_thread_enqueue, &ha->async_event_tq);
525 taskqueue_start_threads(&ha->async_event_tq, 1, PI_NET, "%s asyncq",
526 device_get_nameunit(ha->pci_dev));
528 TASK_INIT(&ha->stats_task, 0, qla_stats, ha);
529 ha->stats_tq = taskqueue_create("qla_statsq", M_NOWAIT,
530 taskqueue_thread_enqueue, &ha->stats_tq);
531 taskqueue_start_threads(&ha->stats_tq, 1, PI_NET, "%s taskq",
532 device_get_nameunit(ha->pci_dev));
534 QL_DPRINT2(ha, (dev, "%s: exit 0\n", __func__));
541 if (ha->flags.lock_init) {
542 mtx_destroy(&ha->hw_lock);
545 QL_DPRINT2(ha, (dev, "%s: exit ENXIO\n", __func__));
550 * Name: qla_pci_detach
551 * Function: Unhooks the device from the operating system
554 qla_pci_detach(device_t dev)
556 qla_host_t *ha = NULL;
560 if ((ha = device_get_softc(dev)) == NULL) {
561 device_printf(dev, "cannot get softc\n");
565 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
569 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
570 QLA_LOCK(ha, __func__, -1, 0);
572 ha->qla_detach_active = 1;
577 QLA_UNLOCK(ha, __func__);
579 if (ha->flags.lock_init) {
580 mtx_destroy(&ha->hw_lock);
583 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
589 * SYSCTL Related Callbacks
592 qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS)
597 err = sysctl_handle_int(oidp, &ret, 0, req);
599 if (err || !req->newptr)
603 ha = (qla_host_t *)arg1;
604 ql_hw_link_status(ha);
611 * Function: Releases the resources allocated for the device
614 qla_release(qla_host_t *ha)
621 if (ha->async_event_tq) {
622 taskqueue_drain(ha->async_event_tq, &ha->async_event_task);
623 taskqueue_free(ha->async_event_tq);
627 taskqueue_drain(ha->err_tq, &ha->err_task);
628 taskqueue_free(ha->err_tq);
632 taskqueue_drain(ha->stats_tq, &ha->stats_task);
633 taskqueue_free(ha->stats_tq);
638 if (ha->flags.qla_watchdog_active) {
639 ha->qla_watchdog_exit = 1;
641 while (ha->qla_watchdog_exited == 0)
642 qla_mdelay(__func__, 1);
645 if (ha->flags.qla_callout_init)
646 callout_stop(&ha->tx_callout);
649 ether_ifdetach(ha->ifp);
651 ql_free_drvr_state_buffer(ha);
653 qla_free_parent_dma_tag(ha);
656 (void)bus_teardown_intr(dev, ha->mbx_irq, ha->mbx_handle);
659 (void) bus_release_resource(dev, SYS_RES_IRQ, ha->mbx_irq_rid,
662 for (i = 0; i < ha->hw.num_sds_rings; i++) {
664 if (ha->irq_vec[i].handle) {
665 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
666 ha->irq_vec[i].handle);
669 if (ha->irq_vec[i].irq) {
670 (void)bus_release_resource(dev, SYS_RES_IRQ,
671 ha->irq_vec[i].irq_rid,
675 qla_free_tx_br(ha, &ha->tx_fp[i]);
677 qla_destroy_fp_taskqueues(ha);
680 pci_release_msi(dev);
682 // if (ha->flags.lock_init) {
683 // mtx_destroy(&ha->hw_lock);
687 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
691 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
698 * DMA Related Functions
702 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
704 *((bus_addr_t *)arg) = 0;
707 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
711 *((bus_addr_t *)arg) = segs[0].ds_addr;
717 ql_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
725 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
727 ret = bus_dma_tag_create(
728 ha->parent_tag,/* parent */
730 ((bus_size_t)(1ULL << 32)),/* boundary */
731 BUS_SPACE_MAXADDR, /* lowaddr */
732 BUS_SPACE_MAXADDR, /* highaddr */
733 NULL, NULL, /* filter, filterarg */
734 dma_buf->size, /* maxsize */
736 dma_buf->size, /* maxsegsize */
738 NULL, NULL, /* lockfunc, lockarg */
742 device_printf(dev, "%s: could not create dma tag\n", __func__);
743 goto ql_alloc_dmabuf_exit;
745 ret = bus_dmamem_alloc(dma_buf->dma_tag,
746 (void **)&dma_buf->dma_b,
747 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
750 bus_dma_tag_destroy(dma_buf->dma_tag);
751 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
752 goto ql_alloc_dmabuf_exit;
755 ret = bus_dmamap_load(dma_buf->dma_tag,
760 &b_addr, BUS_DMA_NOWAIT);
762 if (ret || !b_addr) {
763 bus_dma_tag_destroy(dma_buf->dma_tag);
764 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
767 goto ql_alloc_dmabuf_exit;
770 dma_buf->dma_addr = b_addr;
772 ql_alloc_dmabuf_exit:
773 QL_DPRINT2(ha, (dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
774 __func__, ret, (void *)dma_buf->dma_tag,
775 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
782 ql_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
784 bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
785 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
786 bus_dma_tag_destroy(dma_buf->dma_tag);
790 qla_alloc_parent_dma_tag(qla_host_t *ha)
798 * Allocate parent DMA Tag
800 ret = bus_dma_tag_create(
801 bus_get_dma_tag(dev), /* parent */
802 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
803 BUS_SPACE_MAXADDR, /* lowaddr */
804 BUS_SPACE_MAXADDR, /* highaddr */
805 NULL, NULL, /* filter, filterarg */
806 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
808 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
810 NULL, NULL, /* lockfunc, lockarg */
814 device_printf(dev, "%s: could not create parent dma tag\n",
819 ha->flags.parent_tag = 1;
825 qla_free_parent_dma_tag(qla_host_t *ha)
827 if (ha->flags.parent_tag) {
828 bus_dma_tag_destroy(ha->parent_tag);
829 ha->flags.parent_tag = 0;
834 * Name: qla_init_ifnet
835 * Function: Creates the Network Device Interface and Registers it with the O.S
839 qla_init_ifnet(device_t dev, qla_host_t *ha)
843 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
845 ifp = ha->ifp = if_alloc(IFT_ETHER);
848 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
850 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
852 ifp->if_baudrate = IF_Gbps(10);
853 ifp->if_capabilities = IFCAP_LINKSTATE;
854 ifp->if_mtu = ETHERMTU;
856 ifp->if_init = qla_init;
858 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
859 ifp->if_ioctl = qla_ioctl;
861 ifp->if_transmit = qla_transmit;
862 ifp->if_qflush = qla_qflush;
864 IFQ_SET_MAXLEN(&ifp->if_snd, qla_get_ifq_snd_maxlen(ha));
865 ifp->if_snd.ifq_drv_maxlen = qla_get_ifq_snd_maxlen(ha);
866 IFQ_SET_READY(&ifp->if_snd);
868 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
870 ether_ifattach(ifp, qla_get_mac_addr(ha));
872 ifp->if_capabilities |= IFCAP_HWCSUM |
875 IFCAP_VLAN_HWTAGGING |
880 ifp->if_capenable = ifp->if_capabilities;
882 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
884 ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
886 ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
888 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
890 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
892 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
898 qla_init_locked(qla_host_t *ha)
900 struct ifnet *ifp = ha->ifp;
904 if (qla_alloc_xmt_bufs(ha) != 0)
907 qla_confirm_9kb_enable(ha);
909 if (qla_alloc_rcv_bufs(ha) != 0)
912 bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
914 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
915 ifp->if_hwassist |= CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
918 if (ql_init_hw_if(ha) == 0) {
920 ifp->if_drv_flags |= IFF_DRV_RUNNING;
921 ha->qla_watchdog_pause = 0;
922 ha->hw_vlan_tx_frames = 0;
923 ha->tx_tso_frames = 0;
924 ha->qla_interface_up = 1;
925 ql_update_link_state(ha);
936 ha = (qla_host_t *)arg;
938 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
940 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
945 QLA_UNLOCK(ha, __func__);
947 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
951 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
953 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
954 struct ifmultiaddr *ifma;
956 struct ifnet *ifp = ha->ifp;
961 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
963 if (ifma->ifma_addr->sa_family != AF_LINK)
966 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
969 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
970 &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
975 if_maddr_runlock(ifp);
977 if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
978 QLA_LOCK_NO_SLEEP) != 0)
981 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
984 ret = qla_hw_del_all_mcast(ha);
987 device_printf(ha->pci_dev,
988 "%s: qla_hw_del_all_mcast() failed\n",
993 ret = ql_hw_set_multi(ha, mta, mcnt, 1);
997 QLA_UNLOCK(ha, __func__);
1003 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1006 struct ifreq *ifr = (struct ifreq *)data;
1007 struct ifaddr *ifa = (struct ifaddr *)data;
1010 ha = (qla_host_t *)ifp->if_softc;
1014 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
1017 if (ifa->ifa_addr->sa_family == AF_INET) {
1019 ret = QLA_LOCK(ha, __func__,
1020 QLA_LOCK_DEFAULT_MS_TIMEOUT,
1025 ifp->if_flags |= IFF_UP;
1027 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1028 qla_init_locked(ha);
1031 QLA_UNLOCK(ha, __func__);
1032 QL_DPRINT4(ha, (ha->pci_dev,
1033 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1035 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1037 arp_ifinit(ifp, ifa);
1039 ether_ioctl(ifp, cmd, data);
1044 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1047 if (ifr->ifr_mtu > QLA_MAX_MTU) {
1050 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1056 ifp->if_mtu = ifr->ifr_mtu;
1057 ha->max_frame_size =
1058 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1060 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1061 qla_init_locked(ha);
1064 if (ifp->if_mtu > ETHERMTU)
1065 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1067 ha->std_replenish = QL_STD_REPLENISH_THRES;
1070 QLA_UNLOCK(ha, __func__);
1076 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1079 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1085 if (ifp->if_flags & IFF_UP) {
1087 ha->max_frame_size = ifp->if_mtu +
1088 ETHER_HDR_LEN + ETHER_CRC_LEN;
1089 qla_init_locked(ha);
1091 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1092 if ((ifp->if_flags ^ ha->if_flags) &
1094 ret = ql_set_promisc(ha);
1095 } else if ((ifp->if_flags ^ ha->if_flags) &
1097 ret = ql_set_allmulti(ha);
1101 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1103 ha->if_flags = ifp->if_flags;
1106 QLA_UNLOCK(ha, __func__);
1110 QL_DPRINT4(ha, (ha->pci_dev,
1111 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1113 if (qla_set_multi(ha, 1))
1118 QL_DPRINT4(ha, (ha->pci_dev,
1119 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1121 if (qla_set_multi(ha, 0))
1127 QL_DPRINT4(ha, (ha->pci_dev,
1128 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1130 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1135 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1137 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1140 if (mask & IFCAP_HWCSUM)
1141 ifp->if_capenable ^= IFCAP_HWCSUM;
1142 if (mask & IFCAP_TSO4)
1143 ifp->if_capenable ^= IFCAP_TSO4;
1144 if (mask & IFCAP_TSO6)
1145 ifp->if_capenable ^= IFCAP_TSO6;
1146 if (mask & IFCAP_VLAN_HWTAGGING)
1147 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1148 if (mask & IFCAP_VLAN_HWTSO)
1149 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1150 if (mask & IFCAP_LRO)
1151 ifp->if_capenable ^= IFCAP_LRO;
1153 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1154 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1160 qla_init_locked(ha);
1162 QLA_UNLOCK(ha, __func__);
1165 VLAN_CAPABILITIES(ifp);
1170 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1172 ret = ether_ioctl(ifp, cmd, data);
1180 qla_media_change(struct ifnet *ifp)
1183 struct ifmedia *ifm;
1186 ha = (qla_host_t *)ifp->if_softc;
1188 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1192 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1195 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1201 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1205 ha = (qla_host_t *)ifp->if_softc;
1207 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1209 ifmr->ifm_status = IFM_AVALID;
1210 ifmr->ifm_active = IFM_ETHER;
1212 ql_update_link_state(ha);
1213 if (ha->hw.link_up) {
1214 ifmr->ifm_status |= IFM_ACTIVE;
1215 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1218 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1219 (ha->hw.link_up ? "link_up" : "link_down")));
1226 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1229 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1234 struct mbuf *m_head = *m_headp;
1236 QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1238 tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1240 if (NULL != ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head) {
1241 QL_ASSERT(ha, 0, ("%s [%d]: txr_idx = %d tx_idx = %d "\
1242 "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,\
1243 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head));
1250 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1252 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1259 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1260 m_head->m_pkthdr.len));
1262 m = m_defrag(m_head, M_NOWAIT);
1264 ha->err_tx_defrag++;
1267 device_printf(ha->pci_dev,
1268 "%s: m_defrag() = NULL [%d]\n",
1275 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1276 segs, &nsegs, BUS_DMA_NOWAIT))) {
1278 ha->err_tx_dmamap_load++;
1280 device_printf(ha->pci_dev,
1281 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1282 __func__, ret, m_head->m_pkthdr.len);
1284 if (ret != ENOMEM) {
1293 ha->err_tx_dmamap_load++;
1295 device_printf(ha->pci_dev,
1296 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1297 __func__, ret, m_head->m_pkthdr.len);
1299 if (ret != ENOMEM) {
1306 QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1308 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1310 if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1312 ha->tx_ring[txr_idx].count++;
1314 ha->tx_ring[txr_idx].iscsi_pkt_count++;
1315 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1317 bus_dmamap_unload(ha->tx_tag, map);
1318 if (ret == EINVAL) {
1325 QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1330 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1332 snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1333 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1335 mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1337 fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1338 M_NOWAIT, &fp->tx_mtx);
1339 if (fp->tx_br == NULL) {
1340 QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1341 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1348 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1351 struct ifnet *ifp = ha->ifp;
1353 if (mtx_initialized(&fp->tx_mtx)) {
1355 if (fp->tx_br != NULL) {
1357 mtx_lock(&fp->tx_mtx);
1359 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1363 mtx_unlock(&fp->tx_mtx);
1365 buf_ring_free(fp->tx_br, M_DEVBUF);
1368 mtx_destroy(&fp->tx_mtx);
1374 qla_fp_taskqueue(void *context, int pending)
1382 uint32_t iscsi_pdu = 0;
1383 uint32_t rx_pkts_left = -1;
1390 ha = (qla_host_t *)fp->ha;
1394 txr_idx = fp->txr_idx;
1396 mtx_lock(&fp->tx_mtx);
1398 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1399 mtx_unlock(&fp->tx_mtx);
1400 goto qla_fp_taskqueue_exit;
1403 while (rx_pkts_left && !ha->stop_rcv &&
1404 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1405 rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1407 #ifdef QL_ENABLE_ISCSI_TLV
1408 ql_hw_tx_done_locked(ha, fp->txr_idx);
1409 ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1411 ql_hw_tx_done_locked(ha, fp->txr_idx);
1412 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1414 mp = drbr_peek(ifp, fp->tx_br);
1416 while (mp != NULL) {
1418 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1419 #ifdef QL_ENABLE_ISCSI_TLV
1420 if (ql_iscsi_pdu(ha, mp) == 0) {
1422 (ha->hw.num_tx_rings >> 1);
1426 txr_idx = fp->txr_idx;
1428 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1431 ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1435 drbr_putback(ifp, fp->tx_br, mp);
1437 drbr_advance(ifp, fp->tx_br);
1440 mtx_unlock(&fp->tx_mtx);
1442 goto qla_fp_taskqueue_exit0;
1444 drbr_advance(ifp, fp->tx_br);
1447 /* Send a copy of the frame to the BPF listener */
1448 ETHER_BPF_MTAP(ifp, mp);
1449 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1452 mp = drbr_peek(ifp, fp->tx_br);
1455 mtx_unlock(&fp->tx_mtx);
1457 qla_fp_taskqueue_exit0:
1459 if (rx_pkts_left || ((mp != NULL) && ret)) {
1460 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1462 if (!ha->stop_rcv) {
1463 QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1467 qla_fp_taskqueue_exit:
1469 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1474 qla_create_fp_taskqueues(qla_host_t *ha)
1477 uint8_t tq_name[32];
1479 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1481 qla_tx_fp_t *fp = &ha->tx_fp[i];
1483 bzero(tq_name, sizeof (tq_name));
1484 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1486 TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1488 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1489 taskqueue_thread_enqueue,
1492 if (fp->fp_taskqueue == NULL)
1495 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1498 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1506 qla_destroy_fp_taskqueues(qla_host_t *ha)
1510 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1512 qla_tx_fp_t *fp = &ha->tx_fp[i];
1514 if (fp->fp_taskqueue != NULL) {
1515 taskqueue_drain(fp->fp_taskqueue, &fp->fp_task);
1516 taskqueue_free(fp->fp_taskqueue);
1517 fp->fp_taskqueue = NULL;
1524 qla_drain_fp_taskqueues(qla_host_t *ha)
1528 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1529 qla_tx_fp_t *fp = &ha->tx_fp[i];
1531 if (fp->fp_taskqueue != NULL) {
1532 taskqueue_drain(fp->fp_taskqueue, &fp->fp_task);
1539 qla_transmit(struct ifnet *ifp, struct mbuf *mp)
1541 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1546 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1548 #if __FreeBSD_version >= 1100000
1549 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1551 if (mp->m_flags & M_FLOWID)
1553 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1554 ha->hw.num_sds_rings;
1555 fp = &ha->tx_fp[rss_id];
1557 if (fp->tx_br == NULL) {
1559 goto qla_transmit_exit;
1563 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1566 if (fp->fp_taskqueue != NULL)
1567 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1573 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1578 qla_qflush(struct ifnet *ifp)
1585 ha = (qla_host_t *)ifp->if_softc;
1587 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1589 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1597 mtx_lock(&fp->tx_mtx);
1599 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1602 mtx_unlock(&fp->tx_mtx);
1605 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1611 qla_stop(qla_host_t *ha)
1613 struct ifnet *ifp = ha->ifp;
1619 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1620 ha->qla_watchdog_pause = 1;
1622 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1630 if (fp->tx_br != NULL) {
1631 mtx_lock(&fp->tx_mtx);
1632 mtx_unlock(&fp->tx_mtx);
1636 while (!ha->qla_watchdog_paused)
1637 qla_mdelay(__func__, 1);
1639 ha->qla_interface_up = 0;
1641 qla_drain_fp_taskqueues(ha);
1645 qla_free_xmt_bufs(ha);
1646 qla_free_rcv_bufs(ha);
1652 * Buffer Management Functions for Transmit and Receive Rings
1655 qla_alloc_xmt_bufs(qla_host_t *ha)
1661 if (bus_dma_tag_create(NULL, /* parent */
1662 1, 0, /* alignment, bounds */
1663 BUS_SPACE_MAXADDR, /* lowaddr */
1664 BUS_SPACE_MAXADDR, /* highaddr */
1665 NULL, NULL, /* filter, filterarg */
1666 QLA_MAX_TSO_FRAME_SIZE, /* maxsize */
1667 QLA_MAX_SEGMENTS, /* nsegments */
1668 PAGE_SIZE, /* maxsegsize */
1669 BUS_DMA_ALLOCNOW, /* flags */
1670 NULL, /* lockfunc */
1671 NULL, /* lockfuncarg */
1673 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1678 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1679 bzero((void *)ha->tx_ring[i].tx_buf,
1680 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1683 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1684 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1686 txb = &ha->tx_ring[j].tx_buf[i];
1688 if ((ret = bus_dmamap_create(ha->tx_tag,
1689 BUS_DMA_NOWAIT, &txb->map))) {
1691 ha->err_tx_dmamap_create++;
1692 device_printf(ha->pci_dev,
1693 "%s: bus_dmamap_create failed[%d]\n",
1696 qla_free_xmt_bufs(ha);
1707 * Release mbuf after it sent on the wire
1710 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1712 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1715 bus_dmamap_sync(ha->tx_tag, txb->map,
1716 BUS_DMASYNC_POSTWRITE);
1718 bus_dmamap_unload(ha->tx_tag, txb->map);
1720 m_freem(txb->m_head);
1723 bus_dmamap_destroy(ha->tx_tag, txb->map);
1728 bus_dmamap_unload(ha->tx_tag, txb->map);
1729 bus_dmamap_destroy(ha->tx_tag, txb->map);
1733 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1737 qla_free_xmt_bufs(qla_host_t *ha)
1741 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1742 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1743 qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1746 if (ha->tx_tag != NULL) {
1747 bus_dma_tag_destroy(ha->tx_tag);
1751 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1752 bzero((void *)ha->tx_ring[i].tx_buf,
1753 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1760 qla_alloc_rcv_std(qla_host_t *ha)
1762 int i, j, k, r, ret = 0;
1764 qla_rx_ring_t *rx_ring;
1766 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1768 rx_ring = &ha->rx_ring[r];
1770 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1772 rxb = &rx_ring->rx_buf[i];
1774 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1778 device_printf(ha->pci_dev,
1779 "%s: dmamap[%d, %d] failed\n",
1782 for (k = 0; k < r; k++) {
1783 for (j = 0; j < NUM_RX_DESCRIPTORS;
1785 rxb = &ha->rx_ring[k].rx_buf[j];
1786 bus_dmamap_destroy(ha->rx_tag,
1791 for (j = 0; j < i; j++) {
1792 bus_dmamap_destroy(ha->rx_tag,
1793 rx_ring->rx_buf[j].map);
1795 goto qla_alloc_rcv_std_err;
1800 qla_init_hw_rcv_descriptors(ha);
1803 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1805 rx_ring = &ha->rx_ring[r];
1807 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1808 rxb = &rx_ring->rx_buf[i];
1810 if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1812 * set the physical address in the
1813 * corresponding descriptor entry in the
1814 * receive ring/queue for the hba
1816 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1818 (rxb->m_head)->m_pkthdr.len);
1820 device_printf(ha->pci_dev,
1821 "%s: ql_get_mbuf [%d, %d] failed\n",
1823 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1824 goto qla_alloc_rcv_std_err;
1830 qla_alloc_rcv_std_err:
1835 qla_free_rcv_std(qla_host_t *ha)
1840 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1841 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1842 rxb = &ha->rx_ring[r].rx_buf[i];
1843 if (rxb->m_head != NULL) {
1844 bus_dmamap_unload(ha->rx_tag, rxb->map);
1845 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1846 m_freem(rxb->m_head);
1855 qla_alloc_rcv_bufs(qla_host_t *ha)
1859 if (bus_dma_tag_create(NULL, /* parent */
1860 1, 0, /* alignment, bounds */
1861 BUS_SPACE_MAXADDR, /* lowaddr */
1862 BUS_SPACE_MAXADDR, /* highaddr */
1863 NULL, NULL, /* filter, filterarg */
1864 MJUM9BYTES, /* maxsize */
1866 MJUM9BYTES, /* maxsegsize */
1867 BUS_DMA_ALLOCNOW, /* flags */
1868 NULL, /* lockfunc */
1869 NULL, /* lockfuncarg */
1872 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1878 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1880 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1881 ha->hw.sds[i].sdsr_next = 0;
1882 ha->hw.sds[i].rxb_free = NULL;
1883 ha->hw.sds[i].rx_free = 0;
1886 ret = qla_alloc_rcv_std(ha);
1892 qla_free_rcv_bufs(qla_host_t *ha)
1896 qla_free_rcv_std(ha);
1898 if (ha->rx_tag != NULL) {
1899 bus_dma_tag_destroy(ha->rx_tag);
1903 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1905 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1906 ha->hw.sds[i].sdsr_next = 0;
1907 ha->hw.sds[i].rxb_free = NULL;
1908 ha->hw.sds[i].rx_free = 0;
1915 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1917 register struct mbuf *mp = nmp;
1921 bus_dma_segment_t segs[1];
1922 int nsegs, mbuf_size;
1924 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1928 if (ha->hw.enable_9kb)
1929 mbuf_size = MJUM9BYTES;
1931 mbuf_size = MCLBYTES;
1935 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1938 if (ha->hw.enable_9kb)
1939 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
1941 mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1946 device_printf(ha->pci_dev,
1947 "%s: m_getcl failed\n", __func__);
1948 goto exit_ql_get_mbuf;
1950 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1952 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1953 mp->m_data = mp->m_ext.ext_buf;
1957 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1959 offset = 8 - offset;
1964 * Using memory from the mbuf cluster pool, invoke the bus_dma
1965 * machinery to arrange the memory mapping.
1967 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1968 mp, segs, &nsegs, BUS_DMA_NOWAIT);
1969 rxb->paddr = segs[0].ds_addr;
1971 if (ret || !rxb->paddr || (nsegs != 1)) {
1974 device_printf(ha->pci_dev,
1975 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1976 __func__, ret, (long long unsigned int)rxb->paddr,
1979 goto exit_ql_get_mbuf;
1982 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1985 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1991 qla_get_peer(qla_host_t *ha)
1995 int my_slot = pci_get_slot(ha->pci_dev);
1997 if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
2000 for (i = 0; i < count; i++) {
2001 slot = pci_get_slot(peers[i]);
2003 if ((slot >= 0) && (slot == my_slot) &&
2004 (pci_get_device(peers[i]) ==
2005 pci_get_device(ha->pci_dev))) {
2006 if (ha->pci_dev != peers[i])
2007 ha->peer_dev = peers[i];
2013 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
2015 qla_host_t *ha_peer;
2018 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
2020 ha_peer->msg_from_peer = msg_to_peer;
2026 qla_error_recovery(void *context, int pending)
2028 qla_host_t *ha = context;
2029 uint32_t msecs_100 = 100;
2030 struct ifnet *ifp = ha->ifp;
2033 device_printf(ha->pci_dev, "%s: \n", __func__);
2034 ha->hw.imd_compl = 1;
2036 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2039 device_printf(ha->pci_dev, "%s: enter\n", __func__);
2041 if (ha->qla_interface_up) {
2043 qla_mdelay(__func__, 300);
2045 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2047 for (i = 0; i < ha->hw.num_sds_rings; i++) {
2055 if (fp->tx_br != NULL) {
2056 mtx_lock(&fp->tx_mtx);
2057 mtx_unlock(&fp->tx_mtx);
2063 qla_drain_fp_taskqueues(ha);
2065 if ((ha->pci_func & 0x1) == 0) {
2067 if (!ha->msg_from_peer) {
2068 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2070 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2072 qla_mdelay(__func__, 100);
2075 ha->msg_from_peer = 0;
2077 if (ha->enable_minidump)
2080 (void) ql_init_hw(ha);
2082 if (ha->qla_interface_up) {
2083 qla_free_xmt_bufs(ha);
2084 qla_free_rcv_bufs(ha);
2087 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2090 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2092 ha->msg_from_peer = 0;
2094 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2096 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2099 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) && msecs_100--)
2100 qla_mdelay(__func__, 100);
2101 ha->msg_from_peer = 0;
2103 (void) ql_init_hw(ha);
2105 qla_mdelay(__func__, 1000);
2107 if (ha->qla_interface_up) {
2108 qla_free_xmt_bufs(ha);
2109 qla_free_rcv_bufs(ha);
2113 if (ha->qla_interface_up) {
2115 if (qla_alloc_xmt_bufs(ha) != 0) {
2116 goto qla_error_recovery_exit;
2118 qla_confirm_9kb_enable(ha);
2120 if (qla_alloc_rcv_bufs(ha) != 0) {
2121 goto qla_error_recovery_exit;
2126 if (ql_init_hw_if(ha) == 0) {
2128 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2129 ha->qla_watchdog_pause = 0;
2132 ha->qla_watchdog_pause = 0;
2134 qla_error_recovery_exit:
2136 device_printf(ha->pci_dev, "%s: exit\n", __func__);
2138 QLA_UNLOCK(ha, __func__);
2140 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2146 qla_async_event(void *context, int pending)
2148 qla_host_t *ha = context;
2150 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2153 if (ha->async_event) {
2154 ha->async_event = 0;
2155 qla_hw_async_event(ha);
2158 QLA_UNLOCK(ha, __func__);
2164 qla_stats(void *context, int pending)