2 * Copyright (c) 2013-2016 Qlogic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * and ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
30 * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
40 #include "ql_inline.h"
47 * Some PCI Configuration Space Related Defines
50 #ifndef PCI_VENDOR_QLOGIC
51 #define PCI_VENDOR_QLOGIC 0x1077
54 #ifndef PCI_PRODUCT_QLOGIC_ISP8030
55 #define PCI_PRODUCT_QLOGIC_ISP8030 0x8030
58 #define PCI_QLOGIC_ISP8030 \
59 ((PCI_PRODUCT_QLOGIC_ISP8030 << 16) | PCI_VENDOR_QLOGIC)
64 static int qla_alloc_parent_dma_tag(qla_host_t *ha);
65 static void qla_free_parent_dma_tag(qla_host_t *ha);
66 static int qla_alloc_xmt_bufs(qla_host_t *ha);
67 static void qla_free_xmt_bufs(qla_host_t *ha);
68 static int qla_alloc_rcv_bufs(qla_host_t *ha);
69 static void qla_free_rcv_bufs(qla_host_t *ha);
70 static void qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb);
72 static void qla_init_ifnet(device_t dev, qla_host_t *ha);
73 static int qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS);
74 static void qla_release(qla_host_t *ha);
75 static void qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs,
77 static void qla_stop(qla_host_t *ha);
78 static void qla_get_peer(qla_host_t *ha);
79 static void qla_error_recovery(void *context, int pending);
80 static void qla_async_event(void *context, int pending);
81 static void qla_stats(void *context, int pending);
82 static int qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
86 * Hooks to the Operating Systems
88 static int qla_pci_probe (device_t);
89 static int qla_pci_attach (device_t);
90 static int qla_pci_detach (device_t);
92 static void qla_init(void *arg);
93 static int qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
94 static int qla_media_change(struct ifnet *ifp);
95 static void qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr);
97 static int qla_transmit(struct ifnet *ifp, struct mbuf *mp);
98 static void qla_qflush(struct ifnet *ifp);
99 static int qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *tx_fp);
100 static void qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *tx_fp);
101 static int qla_create_fp_taskqueues(qla_host_t *ha);
102 static void qla_destroy_fp_taskqueues(qla_host_t *ha);
103 static void qla_drain_fp_taskqueues(qla_host_t *ha);
105 static device_method_t qla_pci_methods[] = {
106 /* Device interface */
107 DEVMETHOD(device_probe, qla_pci_probe),
108 DEVMETHOD(device_attach, qla_pci_attach),
109 DEVMETHOD(device_detach, qla_pci_detach),
113 static driver_t qla_pci_driver = {
114 "ql", qla_pci_methods, sizeof (qla_host_t),
117 static devclass_t qla83xx_devclass;
119 DRIVER_MODULE(qla83xx, pci, qla_pci_driver, qla83xx_devclass, 0, 0);
121 MODULE_DEPEND(qla83xx, pci, 1, 1, 1);
122 MODULE_DEPEND(qla83xx, ether, 1, 1, 1);
124 MALLOC_DEFINE(M_QLA83XXBUF, "qla83xxbuf", "Buffers for qla83xx driver");
126 #define QL_STD_REPLENISH_THRES 0
127 #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)),
178 OID_AUTO, "link_status", CTLTYPE_INT | CTLFLAG_RW,
180 qla_sysctl_get_link_status, "I", "Link Status");
183 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
184 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
185 OID_AUTO, "debug", CTLFLAG_RW,
186 &ha->dbg_level, ha->dbg_level, "Debug Level");
188 ha->std_replenish = QL_STD_REPLENISH_THRES;
189 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
190 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
191 OID_AUTO, "std_replenish", CTLFLAG_RW,
192 &ha->std_replenish, ha->std_replenish,
193 "Threshold for Replenishing Standard Frames");
195 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
196 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
197 OID_AUTO, "ipv4_lro",
198 CTLFLAG_RD, &ha->ipv4_lro,
199 "number of ipv4 lro completions");
201 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
202 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
203 OID_AUTO, "ipv6_lro",
204 CTLFLAG_RD, &ha->ipv6_lro,
205 "number of ipv6 lro completions");
207 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
208 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
209 OID_AUTO, "tx_tso_frames",
210 CTLFLAG_RD, &ha->tx_tso_frames,
211 "number of Tx TSO Frames");
213 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
214 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
215 OID_AUTO, "hw_vlan_tx_frames",
216 CTLFLAG_RD, &ha->hw_vlan_tx_frames,
217 "number of Tx VLAN Frames");
219 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
220 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
221 OID_AUTO, "hw_lock_failed",
222 CTLFLAG_RD, &ha->hw_lock_failed,
223 "number of hw_lock failures");
229 qla_watchdog(void *arg)
231 qla_host_t *ha = arg;
238 if (ha->qla_watchdog_exit) {
239 ha->qla_watchdog_exited = 1;
242 ha->qla_watchdog_exited = 0;
244 if (!ha->qla_watchdog_pause) {
245 if (ql_hw_check_health(ha) || ha->qla_initiate_recovery ||
246 (ha->msg_from_peer == QL_PEER_MSG_RESET)) {
248 if (!(ha->dbg_level & 0x8000)) {
249 ha->qla_watchdog_paused = 1;
250 ha->qla_watchdog_pause = 1;
251 ha->qla_initiate_recovery = 0;
253 device_printf(ha->pci_dev,
254 "%s: taskqueue_enqueue(err_task) \n",
256 taskqueue_enqueue(ha->err_tq, &ha->err_task);
260 } else if (ha->qla_interface_up) {
262 ha->watchdog_ticks++;
264 if (ha->watchdog_ticks > 1000)
265 ha->watchdog_ticks = 0;
267 if (!ha->watchdog_ticks && QL_RUNNING(ifp)) {
268 taskqueue_enqueue(ha->stats_tq, &ha->stats_task);
271 if (ha->async_event) {
272 taskqueue_enqueue(ha->async_event_tq,
273 &ha->async_event_task);
277 for (i = 0; ((i < ha->hw.num_sds_rings) &&
278 !ha->watchdog_ticks); i++) {
279 qla_tx_fp_t *fp = &ha->tx_fp[i];
281 if (fp->fp_taskqueue != NULL)
282 taskqueue_enqueue(fp->fp_taskqueue,
286 ha->qla_watchdog_paused = 0;
288 ha->qla_watchdog_paused = 0;
291 ha->qla_watchdog_paused = 1;
294 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
299 * Name: qla_pci_attach
300 * Function: attaches the device to the operating system
303 qla_pci_attach(device_t dev)
305 qla_host_t *ha = NULL;
308 uint32_t num_rcvq = 0;
310 if ((ha = device_get_softc(dev)) == NULL) {
311 device_printf(dev, "cannot get softc\n");
315 memset(ha, 0, sizeof (qla_host_t));
317 if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8030) {
318 device_printf(dev, "device is not ISP8030\n");
322 ha->pci_func = pci_get_function(dev) & 0x1;
326 pci_enable_busmaster(dev);
328 ha->reg_rid = PCIR_BAR(0);
329 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
332 if (ha->pci_reg == NULL) {
333 device_printf(dev, "unable to map any ports\n");
334 goto qla_pci_attach_err;
337 rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
340 mtx_init(&ha->hw_lock, "qla83xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
341 ha->flags.lock_init = 1;
345 ha->hw.num_sds_rings = MAX_SDS_RINGS;
346 ha->hw.num_rds_rings = MAX_RDS_RINGS;
347 ha->hw.num_tx_rings = NUM_TX_RINGS;
349 ha->reg_rid1 = PCIR_BAR(2);
350 ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
351 &ha->reg_rid1, RF_ACTIVE);
353 ha->msix_count = pci_msix_count(dev);
355 if (ha->msix_count < 1 ) {
356 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
358 goto qla_pci_attach_err;
361 if (ha->msix_count < (ha->hw.num_sds_rings + 1)) {
362 ha->hw.num_sds_rings = ha->msix_count - 1;
365 QL_DPRINT2(ha, (dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
366 " msix_count 0x%x pci_reg %p pci_reg1 %p\n", __func__, ha,
367 ha->pci_func, rsrc_len, ha->msix_count, ha->pci_reg,
370 /* initialize hardware */
371 if (ql_init_hw(ha)) {
372 device_printf(dev, "%s: ql_init_hw failed\n", __func__);
373 goto qla_pci_attach_err;
376 device_printf(dev, "%s: firmware[%d.%d.%d.%d]\n", __func__,
377 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
379 snprintf(ha->fw_ver_str, sizeof(ha->fw_ver_str), "%d.%d.%d.%d",
380 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
383 if (qla_get_nic_partition(ha, NULL, &num_rcvq)) {
384 device_printf(dev, "%s: qla_get_nic_partition failed\n",
386 goto qla_pci_attach_err;
388 device_printf(dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
389 " msix_count 0x%x pci_reg %p pci_reg1 %p num_rcvq = %d\n",
390 __func__, ha, ha->pci_func, rsrc_len, ha->msix_count,
391 ha->pci_reg, ha->pci_reg1, num_rcvq);
393 if ((ha->msix_count < 64) || (num_rcvq != 32)) {
394 if (ha->hw.num_sds_rings > 15) {
395 ha->hw.num_sds_rings = 15;
399 ha->hw.num_rds_rings = ha->hw.num_sds_rings;
400 ha->hw.num_tx_rings = ha->hw.num_sds_rings;
402 #ifdef QL_ENABLE_ISCSI_TLV
403 ha->hw.num_tx_rings = ha->hw.num_sds_rings * 2;
404 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
406 ql_hw_add_sysctls(ha);
408 ha->msix_count = ha->hw.num_sds_rings + 1;
410 if (pci_alloc_msix(dev, &ha->msix_count)) {
411 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
414 goto qla_pci_attach_err;
418 ha->mbx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
420 (RF_ACTIVE | RF_SHAREABLE));
421 if (ha->mbx_irq == NULL) {
422 device_printf(dev, "could not allocate mbx interrupt\n");
423 goto qla_pci_attach_err;
425 if (bus_setup_intr(dev, ha->mbx_irq, (INTR_TYPE_NET | INTR_MPSAFE),
426 NULL, ql_mbx_isr, ha, &ha->mbx_handle)) {
427 device_printf(dev, "could not setup mbx interrupt\n");
428 goto qla_pci_attach_err;
431 for (i = 0; i < ha->hw.num_sds_rings; i++) {
432 ha->irq_vec[i].sds_idx = i;
433 ha->irq_vec[i].ha = ha;
434 ha->irq_vec[i].irq_rid = 2 + i;
436 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
437 &ha->irq_vec[i].irq_rid,
438 (RF_ACTIVE | RF_SHAREABLE));
440 if (ha->irq_vec[i].irq == NULL) {
441 device_printf(dev, "could not allocate interrupt\n");
442 goto qla_pci_attach_err;
444 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
445 (INTR_TYPE_NET | INTR_MPSAFE),
446 NULL, ql_isr, &ha->irq_vec[i],
447 &ha->irq_vec[i].handle)) {
448 device_printf(dev, "could not setup interrupt\n");
449 goto qla_pci_attach_err;
452 ha->tx_fp[i].ha = ha;
453 ha->tx_fp[i].txr_idx = i;
455 if (qla_alloc_tx_br(ha, &ha->tx_fp[i])) {
456 device_printf(dev, "%s: could not allocate tx_br[%d]\n",
458 goto qla_pci_attach_err;
462 if (qla_create_fp_taskqueues(ha) != 0)
463 goto qla_pci_attach_err;
465 printf("%s: mp__ncpus %d sds %d rds %d msi-x %d\n", __func__, mp_ncpus,
466 ha->hw.num_sds_rings, ha->hw.num_rds_rings, ha->msix_count);
468 ql_read_mac_addr(ha);
470 /* allocate parent dma tag */
471 if (qla_alloc_parent_dma_tag(ha)) {
472 device_printf(dev, "%s: qla_alloc_parent_dma_tag failed\n",
474 goto qla_pci_attach_err;
477 /* alloc all dma buffers */
478 if (ql_alloc_dma(ha)) {
479 device_printf(dev, "%s: ql_alloc_dma failed\n", __func__);
480 goto qla_pci_attach_err;
484 if (ql_minidump_init(ha) != 0) {
485 device_printf(dev, "%s: ql_minidump_init failed\n", __func__);
486 goto qla_pci_attach_err;
488 /* create the o.s ethernet interface */
489 qla_init_ifnet(dev, ha);
491 ha->flags.qla_watchdog_active = 1;
492 ha->qla_watchdog_pause = 0;
494 callout_init(&ha->tx_callout, TRUE);
495 ha->flags.qla_callout_init = 1;
497 /* create ioctl device interface */
498 if (ql_make_cdev(ha)) {
499 device_printf(dev, "%s: ql_make_cdev failed\n", __func__);
500 goto qla_pci_attach_err;
503 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
506 TASK_INIT(&ha->err_task, 0, qla_error_recovery, ha);
507 ha->err_tq = taskqueue_create("qla_errq", M_NOWAIT,
508 taskqueue_thread_enqueue, &ha->err_tq);
509 taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
510 device_get_nameunit(ha->pci_dev));
512 TASK_INIT(&ha->async_event_task, 0, qla_async_event, ha);
513 ha->async_event_tq = taskqueue_create("qla_asyncq", M_NOWAIT,
514 taskqueue_thread_enqueue, &ha->async_event_tq);
515 taskqueue_start_threads(&ha->async_event_tq, 1, PI_NET, "%s asyncq",
516 device_get_nameunit(ha->pci_dev));
518 TASK_INIT(&ha->stats_task, 0, qla_stats, ha);
519 ha->stats_tq = taskqueue_create("qla_statsq", M_NOWAIT,
520 taskqueue_thread_enqueue, &ha->stats_tq);
521 taskqueue_start_threads(&ha->stats_tq, 1, PI_NET, "%s taskq",
522 device_get_nameunit(ha->pci_dev));
524 QL_DPRINT2(ha, (dev, "%s: exit 0\n", __func__));
531 if (ha->flags.lock_init) {
532 mtx_destroy(&ha->hw_lock);
535 QL_DPRINT2(ha, (dev, "%s: exit ENXIO\n", __func__));
540 * Name: qla_pci_detach
541 * Function: Unhooks the device from the operating system
544 qla_pci_detach(device_t dev)
546 qla_host_t *ha = NULL;
550 if ((ha = device_get_softc(dev)) == NULL) {
551 device_printf(dev, "cannot get softc\n");
555 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
559 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
560 QLA_LOCK(ha, __func__, -1, 0);
562 ha->qla_detach_active = 1;
567 QLA_UNLOCK(ha, __func__);
569 if (ha->flags.lock_init) {
570 mtx_destroy(&ha->hw_lock);
573 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
579 * SYSCTL Related Callbacks
582 qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS)
587 err = sysctl_handle_int(oidp, &ret, 0, req);
589 if (err || !req->newptr)
593 ha = (qla_host_t *)arg1;
594 ql_hw_link_status(ha);
601 * Function: Releases the resources allocated for the device
604 qla_release(qla_host_t *ha)
611 if (ha->async_event_tq) {
612 taskqueue_drain(ha->async_event_tq, &ha->async_event_task);
613 taskqueue_free(ha->async_event_tq);
617 taskqueue_drain(ha->err_tq, &ha->err_task);
618 taskqueue_free(ha->err_tq);
622 taskqueue_drain(ha->stats_tq, &ha->stats_task);
623 taskqueue_free(ha->stats_tq);
628 if (ha->flags.qla_watchdog_active) {
629 ha->qla_watchdog_exit = 1;
631 while (ha->qla_watchdog_exited == 0)
632 qla_mdelay(__func__, 1);
635 if (ha->flags.qla_callout_init)
636 callout_stop(&ha->tx_callout);
639 ether_ifdetach(ha->ifp);
642 qla_free_parent_dma_tag(ha);
645 (void)bus_teardown_intr(dev, ha->mbx_irq, ha->mbx_handle);
648 (void) bus_release_resource(dev, SYS_RES_IRQ, ha->mbx_irq_rid,
651 for (i = 0; i < ha->hw.num_sds_rings; i++) {
653 if (ha->irq_vec[i].handle) {
654 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
655 ha->irq_vec[i].handle);
658 if (ha->irq_vec[i].irq) {
659 (void)bus_release_resource(dev, SYS_RES_IRQ,
660 ha->irq_vec[i].irq_rid,
664 qla_free_tx_br(ha, &ha->tx_fp[i]);
666 qla_destroy_fp_taskqueues(ha);
669 pci_release_msi(dev);
671 // if (ha->flags.lock_init) {
672 // mtx_destroy(&ha->hw_lock);
676 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
680 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
687 * DMA Related Functions
691 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
693 *((bus_addr_t *)arg) = 0;
696 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
700 *((bus_addr_t *)arg) = segs[0].ds_addr;
706 ql_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
714 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
716 ret = bus_dma_tag_create(
717 ha->parent_tag,/* parent */
719 ((bus_size_t)(1ULL << 32)),/* boundary */
720 BUS_SPACE_MAXADDR, /* lowaddr */
721 BUS_SPACE_MAXADDR, /* highaddr */
722 NULL, NULL, /* filter, filterarg */
723 dma_buf->size, /* maxsize */
725 dma_buf->size, /* maxsegsize */
727 NULL, NULL, /* lockfunc, lockarg */
731 device_printf(dev, "%s: could not create dma tag\n", __func__);
732 goto ql_alloc_dmabuf_exit;
734 ret = bus_dmamem_alloc(dma_buf->dma_tag,
735 (void **)&dma_buf->dma_b,
736 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
739 bus_dma_tag_destroy(dma_buf->dma_tag);
740 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
741 goto ql_alloc_dmabuf_exit;
744 ret = bus_dmamap_load(dma_buf->dma_tag,
749 &b_addr, BUS_DMA_NOWAIT);
751 if (ret || !b_addr) {
752 bus_dma_tag_destroy(dma_buf->dma_tag);
753 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
756 goto ql_alloc_dmabuf_exit;
759 dma_buf->dma_addr = b_addr;
761 ql_alloc_dmabuf_exit:
762 QL_DPRINT2(ha, (dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
763 __func__, ret, (void *)dma_buf->dma_tag,
764 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
771 ql_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
773 bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
774 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
775 bus_dma_tag_destroy(dma_buf->dma_tag);
779 qla_alloc_parent_dma_tag(qla_host_t *ha)
787 * Allocate parent DMA Tag
789 ret = bus_dma_tag_create(
790 bus_get_dma_tag(dev), /* parent */
791 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
792 BUS_SPACE_MAXADDR, /* lowaddr */
793 BUS_SPACE_MAXADDR, /* highaddr */
794 NULL, NULL, /* filter, filterarg */
795 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
797 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
799 NULL, NULL, /* lockfunc, lockarg */
803 device_printf(dev, "%s: could not create parent dma tag\n",
808 ha->flags.parent_tag = 1;
814 qla_free_parent_dma_tag(qla_host_t *ha)
816 if (ha->flags.parent_tag) {
817 bus_dma_tag_destroy(ha->parent_tag);
818 ha->flags.parent_tag = 0;
823 * Name: qla_init_ifnet
824 * Function: Creates the Network Device Interface and Registers it with the O.S
828 qla_init_ifnet(device_t dev, qla_host_t *ha)
832 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
834 ifp = ha->ifp = if_alloc(IFT_ETHER);
837 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
839 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
841 #if __FreeBSD_version >= 1000000
842 if_initbaudrate(ifp, IF_Gbps(10));
843 ifp->if_capabilities = IFCAP_LINKSTATE;
845 ifp->if_mtu = ETHERMTU;
846 ifp->if_baudrate = (1 * 1000 * 1000 *1000);
848 #endif /* #if __FreeBSD_version >= 1000000 */
850 ifp->if_init = qla_init;
852 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
853 ifp->if_ioctl = qla_ioctl;
855 ifp->if_transmit = qla_transmit;
856 ifp->if_qflush = qla_qflush;
858 IFQ_SET_MAXLEN(&ifp->if_snd, qla_get_ifq_snd_maxlen(ha));
859 ifp->if_snd.ifq_drv_maxlen = qla_get_ifq_snd_maxlen(ha);
860 IFQ_SET_READY(&ifp->if_snd);
862 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
864 ether_ifattach(ifp, qla_get_mac_addr(ha));
866 ifp->if_capabilities |= IFCAP_HWCSUM |
869 IFCAP_VLAN_HWTAGGING |
874 ifp->if_capenable = ifp->if_capabilities;
876 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
878 ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
880 ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
882 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
884 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
886 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
892 qla_init_locked(qla_host_t *ha)
894 struct ifnet *ifp = ha->ifp;
898 if (qla_alloc_xmt_bufs(ha) != 0)
901 qla_confirm_9kb_enable(ha);
903 if (qla_alloc_rcv_bufs(ha) != 0)
906 bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
908 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
909 ifp->if_hwassist |= CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
912 if (ql_init_hw_if(ha) == 0) {
914 ifp->if_drv_flags |= IFF_DRV_RUNNING;
915 ha->qla_watchdog_pause = 0;
916 ha->hw_vlan_tx_frames = 0;
917 ha->tx_tso_frames = 0;
918 ha->qla_interface_up = 1;
919 ql_update_link_state(ha);
930 ha = (qla_host_t *)arg;
932 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
934 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
939 QLA_UNLOCK(ha, __func__);
941 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
945 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
947 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
948 struct ifmultiaddr *ifma;
950 struct ifnet *ifp = ha->ifp;
955 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
957 if (ifma->ifma_addr->sa_family != AF_LINK)
960 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
963 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
964 &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
969 if_maddr_runlock(ifp);
971 if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
972 QLA_LOCK_NO_SLEEP) != 0)
975 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
976 ret = ql_hw_set_multi(ha, mta, mcnt, add_multi);
979 QLA_UNLOCK(ha, __func__);
985 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
988 struct ifreq *ifr = (struct ifreq *)data;
989 struct ifaddr *ifa = (struct ifaddr *)data;
992 ha = (qla_host_t *)ifp->if_softc;
996 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
999 if (ifa->ifa_addr->sa_family == AF_INET) {
1001 ret = QLA_LOCK(ha, __func__,
1002 QLA_LOCK_DEFAULT_MS_TIMEOUT,
1007 ifp->if_flags |= IFF_UP;
1009 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1010 qla_init_locked(ha);
1013 QLA_UNLOCK(ha, __func__);
1014 QL_DPRINT4(ha, (ha->pci_dev,
1015 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1017 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1019 arp_ifinit(ifp, ifa);
1021 ether_ioctl(ifp, cmd, data);
1026 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1029 if (ifr->ifr_mtu > QLA_MAX_MTU) {
1032 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1038 ifp->if_mtu = ifr->ifr_mtu;
1039 ha->max_frame_size =
1040 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1042 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1043 qla_init_locked(ha);
1046 if (ifp->if_mtu > ETHERMTU)
1047 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1049 ha->std_replenish = QL_STD_REPLENISH_THRES;
1052 QLA_UNLOCK(ha, __func__);
1058 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1061 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1067 if (ifp->if_flags & IFF_UP) {
1069 ha->max_frame_size = ifp->if_mtu +
1070 ETHER_HDR_LEN + ETHER_CRC_LEN;
1071 qla_init_locked(ha);
1073 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1074 if ((ifp->if_flags ^ ha->if_flags) &
1076 ret = ql_set_promisc(ha);
1077 } else if ((ifp->if_flags ^ ha->if_flags) &
1079 ret = ql_set_allmulti(ha);
1083 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1085 ha->if_flags = ifp->if_flags;
1088 QLA_UNLOCK(ha, __func__);
1092 QL_DPRINT4(ha, (ha->pci_dev,
1093 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1095 if (qla_set_multi(ha, 1))
1100 QL_DPRINT4(ha, (ha->pci_dev,
1101 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1103 if (qla_set_multi(ha, 0))
1109 QL_DPRINT4(ha, (ha->pci_dev,
1110 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1112 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1117 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1119 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1122 if (mask & IFCAP_HWCSUM)
1123 ifp->if_capenable ^= IFCAP_HWCSUM;
1124 if (mask & IFCAP_TSO4)
1125 ifp->if_capenable ^= IFCAP_TSO4;
1126 if (mask & IFCAP_TSO6)
1127 ifp->if_capenable ^= IFCAP_TSO6;
1128 if (mask & IFCAP_VLAN_HWTAGGING)
1129 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1130 if (mask & IFCAP_VLAN_HWTSO)
1131 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1132 if (mask & IFCAP_LRO)
1133 ifp->if_capenable ^= IFCAP_LRO;
1135 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1136 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1142 qla_init_locked(ha);
1144 QLA_UNLOCK(ha, __func__);
1147 VLAN_CAPABILITIES(ifp);
1152 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1154 ret = ether_ioctl(ifp, cmd, data);
1162 qla_media_change(struct ifnet *ifp)
1165 struct ifmedia *ifm;
1168 ha = (qla_host_t *)ifp->if_softc;
1170 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1174 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1177 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1183 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1187 ha = (qla_host_t *)ifp->if_softc;
1189 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1191 ifmr->ifm_status = IFM_AVALID;
1192 ifmr->ifm_active = IFM_ETHER;
1194 ql_update_link_state(ha);
1195 if (ha->hw.link_up) {
1196 ifmr->ifm_status |= IFM_ACTIVE;
1197 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1200 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1201 (ha->hw.link_up ? "link_up" : "link_down")));
1208 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1211 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1216 struct mbuf *m_head = *m_headp;
1218 QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1220 if (m_head->m_flags & M_FLOWID) {
1221 #ifdef QL_ENABLE_ISCSI_TLV
1222 if (qla_iscsi_pdu(ha, m_head) == 0) {
1224 txr_idx = m_head->m_pkthdr.flowid &
1225 ((ha->hw.num_tx_rings >> 1) - 1);
1227 txr_idx = m_head->m_pkthdr.flowid &
1228 (ha->hw.num_tx_rings - 1);
1231 txr_idx = m_head->m_pkthdr.flowid & (ha->hw.num_tx_rings - 1);
1232 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1236 tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1237 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1239 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1246 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1247 m_head->m_pkthdr.len));
1249 m = m_defrag(m_head, M_NOWAIT);
1251 ha->err_tx_defrag++;
1254 device_printf(ha->pci_dev,
1255 "%s: m_defrag() = NULL [%d]\n",
1262 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1263 segs, &nsegs, BUS_DMA_NOWAIT))) {
1265 ha->err_tx_dmamap_load++;
1267 device_printf(ha->pci_dev,
1268 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1269 __func__, ret, m_head->m_pkthdr.len);
1271 if (ret != ENOMEM) {
1280 ha->err_tx_dmamap_load++;
1282 device_printf(ha->pci_dev,
1283 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1284 __func__, ret, m_head->m_pkthdr.len);
1286 if (ret != ENOMEM) {
1293 QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1295 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1297 if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1299 ha->tx_ring[txr_idx].count++;
1301 ha->tx_ring[txr_idx].iscsi_pkt_count++;
1302 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1304 if (ret == EINVAL) {
1311 QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1316 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1318 snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1319 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1321 mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1323 fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1324 M_NOWAIT, &fp->tx_mtx);
1325 if (fp->tx_br == NULL) {
1326 QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1327 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1334 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1337 struct ifnet *ifp = ha->ifp;
1339 if (mtx_initialized(&fp->tx_mtx)) {
1341 if (fp->tx_br != NULL) {
1343 mtx_lock(&fp->tx_mtx);
1345 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1349 mtx_unlock(&fp->tx_mtx);
1351 buf_ring_free(fp->tx_br, M_DEVBUF);
1354 mtx_destroy(&fp->tx_mtx);
1360 qla_fp_taskqueue(void *context, int pending)
1365 struct mbuf *mp = NULL;
1368 uint32_t iscsi_pdu = 0;
1369 uint32_t rx_pkts_left = -1;
1376 ha = (qla_host_t *)fp->ha;
1380 txr_idx = fp->txr_idx;
1382 mtx_lock(&fp->tx_mtx);
1384 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1385 mtx_unlock(&fp->tx_mtx);
1386 goto qla_fp_taskqueue_exit;
1389 while (rx_pkts_left && !ha->stop_rcv) {
1390 rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1392 #ifdef QL_ENABLE_ISCSI_TLV
1393 ql_hw_tx_done_locked(ha, fp->txr_idx);
1394 ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1396 ql_hw_tx_done_locked(ha, fp->txr_idx);
1397 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1399 mp = drbr_peek(ifp, fp->tx_br);
1401 while (mp != NULL) {
1403 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1404 #ifdef QL_ENABLE_ISCSI_TLV
1405 if (ql_iscsi_pdu(ha, mp) == 0) {
1407 (ha->hw.num_tx_rings >> 1);
1411 txr_idx = fp->txr_idx;
1413 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1416 ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1420 drbr_putback(ifp, fp->tx_br, mp);
1422 drbr_advance(ifp, fp->tx_br);
1425 mtx_unlock(&fp->tx_mtx);
1427 goto qla_fp_taskqueue_exit0;
1429 drbr_advance(ifp, fp->tx_br);
1432 mp = drbr_peek(ifp, fp->tx_br);
1435 mtx_unlock(&fp->tx_mtx);
1437 qla_fp_taskqueue_exit0:
1439 if (rx_pkts_left || ((mp != NULL) && ret)) {
1440 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1442 if (!ha->stop_rcv) {
1443 QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1447 qla_fp_taskqueue_exit:
1449 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1454 qla_create_fp_taskqueues(qla_host_t *ha)
1457 uint8_t tq_name[32];
1459 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1461 qla_tx_fp_t *fp = &ha->tx_fp[i];
1463 bzero(tq_name, sizeof (tq_name));
1464 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1466 TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1468 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1469 taskqueue_thread_enqueue,
1472 if (fp->fp_taskqueue == NULL)
1475 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1478 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1486 qla_destroy_fp_taskqueues(qla_host_t *ha)
1490 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1492 qla_tx_fp_t *fp = &ha->tx_fp[i];
1494 if (fp->fp_taskqueue != NULL) {
1495 taskqueue_drain(fp->fp_taskqueue, &fp->fp_task);
1496 taskqueue_free(fp->fp_taskqueue);
1497 fp->fp_taskqueue = NULL;
1504 qla_drain_fp_taskqueues(qla_host_t *ha)
1508 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1509 qla_tx_fp_t *fp = &ha->tx_fp[i];
1511 if (fp->fp_taskqueue != NULL) {
1512 taskqueue_drain(fp->fp_taskqueue, &fp->fp_task);
1519 qla_transmit(struct ifnet *ifp, struct mbuf *mp)
1521 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1526 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1528 #if __FreeBSD_version >= 1100000
1529 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1531 if (mp->m_flags & M_FLOWID)
1533 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1534 ha->hw.num_sds_rings;
1535 fp = &ha->tx_fp[rss_id];
1537 if (fp->tx_br == NULL) {
1539 goto qla_transmit_exit;
1543 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1546 if (fp->fp_taskqueue != NULL)
1547 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1553 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1558 qla_qflush(struct ifnet *ifp)
1565 ha = (qla_host_t *)ifp->if_softc;
1567 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1569 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1577 mtx_lock(&fp->tx_mtx);
1579 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1582 mtx_unlock(&fp->tx_mtx);
1585 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1591 qla_stop(qla_host_t *ha)
1593 struct ifnet *ifp = ha->ifp;
1599 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1600 ha->qla_watchdog_pause = 1;
1602 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1610 if (fp->tx_br != NULL) {
1611 mtx_lock(&fp->tx_mtx);
1612 mtx_unlock(&fp->tx_mtx);
1616 while (!ha->qla_watchdog_paused)
1617 qla_mdelay(__func__, 1);
1619 ha->qla_interface_up = 0;
1621 qla_drain_fp_taskqueues(ha);
1625 qla_free_xmt_bufs(ha);
1626 qla_free_rcv_bufs(ha);
1632 * Buffer Management Functions for Transmit and Receive Rings
1635 qla_alloc_xmt_bufs(qla_host_t *ha)
1641 if (bus_dma_tag_create(NULL, /* parent */
1642 1, 0, /* alignment, bounds */
1643 BUS_SPACE_MAXADDR, /* lowaddr */
1644 BUS_SPACE_MAXADDR, /* highaddr */
1645 NULL, NULL, /* filter, filterarg */
1646 QLA_MAX_TSO_FRAME_SIZE, /* maxsize */
1647 QLA_MAX_SEGMENTS, /* nsegments */
1648 PAGE_SIZE, /* maxsegsize */
1649 BUS_DMA_ALLOCNOW, /* flags */
1650 NULL, /* lockfunc */
1651 NULL, /* lockfuncarg */
1653 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1658 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1659 bzero((void *)ha->tx_ring[i].tx_buf,
1660 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1663 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1664 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1666 txb = &ha->tx_ring[j].tx_buf[i];
1668 if ((ret = bus_dmamap_create(ha->tx_tag,
1669 BUS_DMA_NOWAIT, &txb->map))) {
1671 ha->err_tx_dmamap_create++;
1672 device_printf(ha->pci_dev,
1673 "%s: bus_dmamap_create failed[%d]\n",
1676 qla_free_xmt_bufs(ha);
1687 * Release mbuf after it sent on the wire
1690 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1692 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1694 if (txb->m_head && txb->map) {
1696 bus_dmamap_unload(ha->tx_tag, txb->map);
1698 m_freem(txb->m_head);
1703 bus_dmamap_destroy(ha->tx_tag, txb->map);
1705 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1709 qla_free_xmt_bufs(qla_host_t *ha)
1713 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1714 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1715 qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1718 if (ha->tx_tag != NULL) {
1719 bus_dma_tag_destroy(ha->tx_tag);
1723 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1724 bzero((void *)ha->tx_ring[i].tx_buf,
1725 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1732 qla_alloc_rcv_std(qla_host_t *ha)
1734 int i, j, k, r, ret = 0;
1736 qla_rx_ring_t *rx_ring;
1738 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1740 rx_ring = &ha->rx_ring[r];
1742 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1744 rxb = &rx_ring->rx_buf[i];
1746 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1750 device_printf(ha->pci_dev,
1751 "%s: dmamap[%d, %d] failed\n",
1754 for (k = 0; k < r; k++) {
1755 for (j = 0; j < NUM_RX_DESCRIPTORS;
1757 rxb = &ha->rx_ring[k].rx_buf[j];
1758 bus_dmamap_destroy(ha->rx_tag,
1763 for (j = 0; j < i; j++) {
1764 bus_dmamap_destroy(ha->rx_tag,
1765 rx_ring->rx_buf[j].map);
1767 goto qla_alloc_rcv_std_err;
1772 qla_init_hw_rcv_descriptors(ha);
1775 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1777 rx_ring = &ha->rx_ring[r];
1779 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1780 rxb = &rx_ring->rx_buf[i];
1782 if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1784 * set the physical address in the
1785 * corresponding descriptor entry in the
1786 * receive ring/queue for the hba
1788 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1790 (rxb->m_head)->m_pkthdr.len);
1792 device_printf(ha->pci_dev,
1793 "%s: ql_get_mbuf [%d, %d] failed\n",
1795 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1796 goto qla_alloc_rcv_std_err;
1802 qla_alloc_rcv_std_err:
1807 qla_free_rcv_std(qla_host_t *ha)
1812 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1813 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1814 rxb = &ha->rx_ring[r].rx_buf[i];
1815 if (rxb->m_head != NULL) {
1816 bus_dmamap_unload(ha->rx_tag, rxb->map);
1817 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1818 m_freem(rxb->m_head);
1827 qla_alloc_rcv_bufs(qla_host_t *ha)
1831 if (bus_dma_tag_create(NULL, /* parent */
1832 1, 0, /* alignment, bounds */
1833 BUS_SPACE_MAXADDR, /* lowaddr */
1834 BUS_SPACE_MAXADDR, /* highaddr */
1835 NULL, NULL, /* filter, filterarg */
1836 MJUM9BYTES, /* maxsize */
1838 MJUM9BYTES, /* maxsegsize */
1839 BUS_DMA_ALLOCNOW, /* flags */
1840 NULL, /* lockfunc */
1841 NULL, /* lockfuncarg */
1844 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1850 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1852 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1853 ha->hw.sds[i].sdsr_next = 0;
1854 ha->hw.sds[i].rxb_free = NULL;
1855 ha->hw.sds[i].rx_free = 0;
1858 ret = qla_alloc_rcv_std(ha);
1864 qla_free_rcv_bufs(qla_host_t *ha)
1868 qla_free_rcv_std(ha);
1870 if (ha->rx_tag != NULL) {
1871 bus_dma_tag_destroy(ha->rx_tag);
1875 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1877 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1878 ha->hw.sds[i].sdsr_next = 0;
1879 ha->hw.sds[i].rxb_free = NULL;
1880 ha->hw.sds[i].rx_free = 0;
1887 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1889 register struct mbuf *mp = nmp;
1893 bus_dma_segment_t segs[1];
1894 int nsegs, mbuf_size;
1896 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1900 if (ha->hw.enable_9kb)
1901 mbuf_size = MJUM9BYTES;
1903 mbuf_size = MCLBYTES;
1907 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1910 if (ha->hw.enable_9kb)
1911 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
1913 mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1918 device_printf(ha->pci_dev,
1919 "%s: m_getcl failed\n", __func__);
1920 goto exit_ql_get_mbuf;
1922 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1924 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1925 mp->m_data = mp->m_ext.ext_buf;
1929 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1931 offset = 8 - offset;
1936 * Using memory from the mbuf cluster pool, invoke the bus_dma
1937 * machinery to arrange the memory mapping.
1939 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1940 mp, segs, &nsegs, BUS_DMA_NOWAIT);
1941 rxb->paddr = segs[0].ds_addr;
1943 if (ret || !rxb->paddr || (nsegs != 1)) {
1946 device_printf(ha->pci_dev,
1947 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1948 __func__, ret, (long long unsigned int)rxb->paddr,
1951 goto exit_ql_get_mbuf;
1954 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1957 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1963 qla_get_peer(qla_host_t *ha)
1967 int my_slot = pci_get_slot(ha->pci_dev);
1969 if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
1972 for (i = 0; i < count; i++) {
1973 slot = pci_get_slot(peers[i]);
1975 if ((slot >= 0) && (slot == my_slot) &&
1976 (pci_get_device(peers[i]) ==
1977 pci_get_device(ha->pci_dev))) {
1978 if (ha->pci_dev != peers[i])
1979 ha->peer_dev = peers[i];
1985 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
1987 qla_host_t *ha_peer;
1990 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
1992 ha_peer->msg_from_peer = msg_to_peer;
1998 qla_error_recovery(void *context, int pending)
2000 qla_host_t *ha = context;
2001 uint32_t msecs_100 = 100;
2002 struct ifnet *ifp = ha->ifp;
2005 device_printf(ha->pci_dev, "%s: \n", __func__);
2006 ha->hw.imd_compl = 1;
2008 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2011 device_printf(ha->pci_dev, "%s: enter\n", __func__);
2013 if (ha->qla_interface_up) {
2015 qla_mdelay(__func__, 300);
2017 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2019 for (i = 0; i < ha->hw.num_sds_rings; i++) {
2027 if (fp->tx_br != NULL) {
2028 mtx_lock(&fp->tx_mtx);
2029 mtx_unlock(&fp->tx_mtx);
2035 qla_drain_fp_taskqueues(ha);
2037 if ((ha->pci_func & 0x1) == 0) {
2039 if (!ha->msg_from_peer) {
2040 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2042 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2044 qla_mdelay(__func__, 100);
2047 ha->msg_from_peer = 0;
2051 (void) ql_init_hw(ha);
2053 if (ha->qla_interface_up) {
2054 qla_free_xmt_bufs(ha);
2055 qla_free_rcv_bufs(ha);
2058 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2061 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2063 ha->msg_from_peer = 0;
2065 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2067 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2070 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) && msecs_100--)
2071 qla_mdelay(__func__, 100);
2072 ha->msg_from_peer = 0;
2074 (void) ql_init_hw(ha);
2076 qla_mdelay(__func__, 1000);
2078 if (ha->qla_interface_up) {
2079 qla_free_xmt_bufs(ha);
2080 qla_free_rcv_bufs(ha);
2084 if (ha->qla_interface_up) {
2086 if (qla_alloc_xmt_bufs(ha) != 0) {
2087 goto qla_error_recovery_exit;
2089 qla_confirm_9kb_enable(ha);
2091 if (qla_alloc_rcv_bufs(ha) != 0) {
2092 goto qla_error_recovery_exit;
2097 if (ql_init_hw_if(ha) == 0) {
2099 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2100 ha->qla_watchdog_pause = 0;
2103 ha->qla_watchdog_pause = 0;
2105 qla_error_recovery_exit:
2107 device_printf(ha->pci_dev, "%s: exit\n", __func__);
2109 QLA_UNLOCK(ha, __func__);
2111 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2117 qla_async_event(void *context, int pending)
2119 qla_host_t *ha = context;
2121 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2124 if (ha->async_event) {
2125 ha->async_event = 0;
2126 qla_hw_async_event(ha);
2129 QLA_UNLOCK(ha, __func__);
2135 qla_stats(void *context, int pending)