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_stats(SYSCTL_HANDLER_ARGS);
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 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_PROC(device_get_sysctl_ctx(dev),
172 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
173 OID_AUTO, "stats", CTLTYPE_INT | CTLFLAG_RW,
175 qla_sysctl_get_stats, "I", "Statistics");
177 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
178 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
179 OID_AUTO, "fw_version", CTLFLAG_RD,
180 ha->fw_ver_str, 0, "firmware version");
182 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
183 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
184 OID_AUTO, "link_status", CTLTYPE_INT | CTLFLAG_RW,
186 qla_sysctl_get_link_status, "I", "Link Status");
189 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
190 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
191 OID_AUTO, "debug", CTLFLAG_RW,
192 &ha->dbg_level, ha->dbg_level, "Debug Level");
194 ha->std_replenish = QL_STD_REPLENISH_THRES;
195 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
196 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
197 OID_AUTO, "std_replenish", CTLFLAG_RW,
198 &ha->std_replenish, ha->std_replenish,
199 "Threshold for Replenishing Standard Frames");
201 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
202 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
203 OID_AUTO, "ipv4_lro",
204 CTLFLAG_RD, &ha->ipv4_lro,
205 "number of ipv4 lro completions");
207 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
208 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
209 OID_AUTO, "ipv6_lro",
210 CTLFLAG_RD, &ha->ipv6_lro,
211 "number of ipv6 lro completions");
213 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
214 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
215 OID_AUTO, "tx_tso_frames",
216 CTLFLAG_RD, &ha->tx_tso_frames,
217 "number of Tx TSO Frames");
219 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
220 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
221 OID_AUTO, "hw_vlan_tx_frames",
222 CTLFLAG_RD, &ha->hw_vlan_tx_frames,
223 "number of Tx VLAN Frames");
229 qla_watchdog(void *arg)
231 qla_host_t *ha = arg;
239 if (ha->flags.qla_watchdog_exit) {
240 ha->qla_watchdog_exited = 1;
243 ha->qla_watchdog_exited = 0;
245 if (!ha->flags.qla_watchdog_pause) {
246 if (ql_hw_check_health(ha) || ha->qla_initiate_recovery ||
247 (ha->msg_from_peer == QL_PEER_MSG_RESET)) {
248 ha->qla_watchdog_paused = 1;
249 ha->flags.qla_watchdog_pause = 1;
250 ha->qla_initiate_recovery = 0;
252 device_printf(ha->pci_dev,
253 "%s: taskqueue_enqueue(err_task) \n", __func__);
254 taskqueue_enqueue(ha->err_tq, &ha->err_task);
255 } else if (ha->flags.qla_interface_up) {
257 if (ha->async_event) {
259 taskqueue_enqueue(ha->async_event_tq,
260 &ha->async_event_task);
263 for (i = 0; i < ha->hw.num_sds_rings; i++) {
264 qla_tx_fp_t *fp = &ha->tx_fp[i];
266 if (fp->fp_taskqueue != NULL)
267 taskqueue_enqueue(fp->fp_taskqueue,
271 ha->qla_watchdog_paused = 0;
273 ha->qla_watchdog_paused = 0;
276 ha->qla_watchdog_paused = 1;
279 ha->watchdog_ticks = ha->watchdog_ticks++ % 1000;
280 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
285 * Name: qla_pci_attach
286 * Function: attaches the device to the operating system
289 qla_pci_attach(device_t dev)
291 qla_host_t *ha = NULL;
294 uint32_t num_rcvq = 0;
296 if ((ha = device_get_softc(dev)) == NULL) {
297 device_printf(dev, "cannot get softc\n");
301 memset(ha, 0, sizeof (qla_host_t));
303 if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8030) {
304 device_printf(dev, "device is not ISP8030\n");
308 ha->pci_func = pci_get_function(dev) & 0x1;
312 pci_enable_busmaster(dev);
314 ha->reg_rid = PCIR_BAR(0);
315 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
318 if (ha->pci_reg == NULL) {
319 device_printf(dev, "unable to map any ports\n");
320 goto qla_pci_attach_err;
323 rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
326 mtx_init(&ha->hw_lock, "qla83xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
329 ql_hw_add_sysctls(ha);
331 ha->flags.lock_init = 1;
333 ha->reg_rid1 = PCIR_BAR(2);
334 ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
335 &ha->reg_rid1, RF_ACTIVE);
337 ha->msix_count = pci_msix_count(dev);
339 if (ha->msix_count < (ha->hw.num_sds_rings + 1)) {
340 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
342 goto qla_pci_attach_err;
345 QL_DPRINT2(ha, (dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
346 " msix_count 0x%x pci_reg %p pci_reg1 %p\n", __func__, ha,
347 ha->pci_func, rsrc_len, ha->msix_count, ha->pci_reg,
350 /* initialize hardware */
351 if (ql_init_hw(ha)) {
352 device_printf(dev, "%s: ql_init_hw failed\n", __func__);
353 goto qla_pci_attach_err;
356 device_printf(dev, "%s: firmware[%d.%d.%d.%d]\n", __func__,
357 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
359 snprintf(ha->fw_ver_str, sizeof(ha->fw_ver_str), "%d.%d.%d.%d",
360 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
363 if (qla_get_nic_partition(ha, NULL, &num_rcvq)) {
364 device_printf(dev, "%s: qla_get_nic_partition failed\n",
366 goto qla_pci_attach_err;
368 device_printf(dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
369 " msix_count 0x%x pci_reg %p pci_reg1 %p num_rcvq = %d\n",
370 __func__, ha, ha->pci_func, rsrc_len, ha->msix_count,
371 ha->pci_reg, ha->pci_reg1, num_rcvq);
374 #ifdef QL_ENABLE_ISCSI_TLV
375 if ((ha->msix_count < 64) || (num_rcvq != 32)) {
376 ha->hw.num_sds_rings = 15;
377 ha->hw.num_tx_rings = ha->hw.num_sds_rings * 2;
379 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
380 ha->hw.num_rds_rings = ha->hw.num_sds_rings;
382 ha->msix_count = ha->hw.num_sds_rings + 1;
384 if (pci_alloc_msix(dev, &ha->msix_count)) {
385 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
388 goto qla_pci_attach_err;
392 ha->mbx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
394 (RF_ACTIVE | RF_SHAREABLE));
395 if (ha->mbx_irq == NULL) {
396 device_printf(dev, "could not allocate mbx interrupt\n");
397 goto qla_pci_attach_err;
399 if (bus_setup_intr(dev, ha->mbx_irq, (INTR_TYPE_NET | INTR_MPSAFE),
400 NULL, ql_mbx_isr, ha, &ha->mbx_handle)) {
401 device_printf(dev, "could not setup mbx interrupt\n");
402 goto qla_pci_attach_err;
405 for (i = 0; i < ha->hw.num_sds_rings; i++) {
406 ha->irq_vec[i].sds_idx = i;
407 ha->irq_vec[i].ha = ha;
408 ha->irq_vec[i].irq_rid = 2 + i;
410 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
411 &ha->irq_vec[i].irq_rid,
412 (RF_ACTIVE | RF_SHAREABLE));
414 if (ha->irq_vec[i].irq == NULL) {
415 device_printf(dev, "could not allocate interrupt\n");
416 goto qla_pci_attach_err;
418 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
419 (INTR_TYPE_NET | INTR_MPSAFE),
420 NULL, ql_isr, &ha->irq_vec[i],
421 &ha->irq_vec[i].handle)) {
422 device_printf(dev, "could not setup interrupt\n");
423 goto qla_pci_attach_err;
426 ha->tx_fp[i].ha = ha;
427 ha->tx_fp[i].txr_idx = i;
429 if (qla_alloc_tx_br(ha, &ha->tx_fp[i])) {
430 device_printf(dev, "%s: could not allocate tx_br[%d]\n",
432 goto qla_pci_attach_err;
436 if (qla_create_fp_taskqueues(ha) != 0)
437 goto qla_pci_attach_err;
439 printf("%s: mp__ncpus %d sds %d rds %d msi-x %d\n", __func__, mp_ncpus,
440 ha->hw.num_sds_rings, ha->hw.num_rds_rings, ha->msix_count);
442 ql_read_mac_addr(ha);
444 /* allocate parent dma tag */
445 if (qla_alloc_parent_dma_tag(ha)) {
446 device_printf(dev, "%s: qla_alloc_parent_dma_tag failed\n",
448 goto qla_pci_attach_err;
451 /* alloc all dma buffers */
452 if (ql_alloc_dma(ha)) {
453 device_printf(dev, "%s: ql_alloc_dma failed\n", __func__);
454 goto qla_pci_attach_err;
458 if (ql_minidump_init(ha) != 0) {
459 device_printf(dev, "%s: ql_minidump_init failed\n", __func__);
460 goto qla_pci_attach_err;
462 /* create the o.s ethernet interface */
463 qla_init_ifnet(dev, ha);
465 ha->flags.qla_watchdog_active = 1;
466 ha->flags.qla_watchdog_pause = 0;
468 callout_init(&ha->tx_callout, TRUE);
469 ha->flags.qla_callout_init = 1;
471 /* create ioctl device interface */
472 if (ql_make_cdev(ha)) {
473 device_printf(dev, "%s: ql_make_cdev failed\n", __func__);
474 goto qla_pci_attach_err;
477 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
480 TASK_INIT(&ha->err_task, 0, qla_error_recovery, ha);
481 ha->err_tq = taskqueue_create("qla_errq", M_NOWAIT,
482 taskqueue_thread_enqueue, &ha->err_tq);
483 taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
484 device_get_nameunit(ha->pci_dev));
486 TASK_INIT(&ha->async_event_task, 0, qla_async_event, ha);
487 ha->async_event_tq = taskqueue_create("qla_asyncq", M_NOWAIT,
488 taskqueue_thread_enqueue, &ha->async_event_tq);
489 taskqueue_start_threads(&ha->async_event_tq, 1, PI_NET, "%s asyncq",
490 device_get_nameunit(ha->pci_dev));
492 QL_DPRINT2(ha, (dev, "%s: exit 0\n", __func__));
499 QL_DPRINT2(ha, (dev, "%s: exit ENXIO\n", __func__));
504 * Name: qla_pci_detach
505 * Function: Unhooks the device from the operating system
508 qla_pci_detach(device_t dev)
510 qla_host_t *ha = NULL;
513 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
515 if ((ha = device_get_softc(dev)) == NULL) {
516 device_printf(dev, "cannot get softc\n");
522 (void)QLA_LOCK(ha, __func__, 0);
524 QLA_UNLOCK(ha, __func__);
528 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
534 * SYSCTL Related Callbacks
537 qla_sysctl_get_stats(SYSCTL_HANDLER_ARGS)
542 err = sysctl_handle_int(oidp, &ret, 0, req);
544 if (err || !req->newptr)
548 ha = (qla_host_t *)arg1;
554 qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS)
559 err = sysctl_handle_int(oidp, &ret, 0, req);
561 if (err || !req->newptr)
565 ha = (qla_host_t *)arg1;
566 ql_hw_link_status(ha);
573 * Function: Releases the resources allocated for the device
576 qla_release(qla_host_t *ha)
583 if (ha->async_event_tq) {
584 taskqueue_drain(ha->async_event_tq, &ha->async_event_task);
585 taskqueue_free(ha->async_event_tq);
589 taskqueue_drain(ha->err_tq, &ha->err_task);
590 taskqueue_free(ha->err_tq);
595 if (ha->flags.qla_watchdog_active) {
596 ha->flags.qla_watchdog_exit = 1;
598 while (ha->qla_watchdog_exited == 0)
599 qla_mdelay(__func__, 1);
602 if (ha->flags.qla_callout_init)
603 callout_stop(&ha->tx_callout);
606 ether_ifdetach(ha->ifp);
609 qla_free_parent_dma_tag(ha);
612 (void)bus_teardown_intr(dev, ha->mbx_irq, ha->mbx_handle);
615 (void) bus_release_resource(dev, SYS_RES_IRQ, ha->mbx_irq_rid,
618 for (i = 0; i < ha->hw.num_sds_rings; i++) {
620 if (ha->irq_vec[i].handle) {
621 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
622 ha->irq_vec[i].handle);
625 if (ha->irq_vec[i].irq) {
626 (void)bus_release_resource(dev, SYS_RES_IRQ,
627 ha->irq_vec[i].irq_rid,
631 qla_free_tx_br(ha, &ha->tx_fp[i]);
633 qla_destroy_fp_taskqueues(ha);
636 pci_release_msi(dev);
638 if (ha->flags.lock_init) {
639 mtx_destroy(&ha->hw_lock);
643 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
647 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
652 * DMA Related Functions
656 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
658 *((bus_addr_t *)arg) = 0;
661 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
665 *((bus_addr_t *)arg) = segs[0].ds_addr;
671 ql_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
679 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
681 ret = bus_dma_tag_create(
682 ha->parent_tag,/* parent */
684 ((bus_size_t)(1ULL << 32)),/* boundary */
685 BUS_SPACE_MAXADDR, /* lowaddr */
686 BUS_SPACE_MAXADDR, /* highaddr */
687 NULL, NULL, /* filter, filterarg */
688 dma_buf->size, /* maxsize */
690 dma_buf->size, /* maxsegsize */
692 NULL, NULL, /* lockfunc, lockarg */
696 device_printf(dev, "%s: could not create dma tag\n", __func__);
697 goto ql_alloc_dmabuf_exit;
699 ret = bus_dmamem_alloc(dma_buf->dma_tag,
700 (void **)&dma_buf->dma_b,
701 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
704 bus_dma_tag_destroy(dma_buf->dma_tag);
705 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
706 goto ql_alloc_dmabuf_exit;
709 ret = bus_dmamap_load(dma_buf->dma_tag,
714 &b_addr, BUS_DMA_NOWAIT);
716 if (ret || !b_addr) {
717 bus_dma_tag_destroy(dma_buf->dma_tag);
718 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
721 goto ql_alloc_dmabuf_exit;
724 dma_buf->dma_addr = b_addr;
726 ql_alloc_dmabuf_exit:
727 QL_DPRINT2(ha, (dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
728 __func__, ret, (void *)dma_buf->dma_tag,
729 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
736 ql_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
738 bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
739 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
740 bus_dma_tag_destroy(dma_buf->dma_tag);
744 qla_alloc_parent_dma_tag(qla_host_t *ha)
752 * Allocate parent DMA Tag
754 ret = bus_dma_tag_create(
755 bus_get_dma_tag(dev), /* parent */
756 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
757 BUS_SPACE_MAXADDR, /* lowaddr */
758 BUS_SPACE_MAXADDR, /* highaddr */
759 NULL, NULL, /* filter, filterarg */
760 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
762 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
764 NULL, NULL, /* lockfunc, lockarg */
768 device_printf(dev, "%s: could not create parent dma tag\n",
773 ha->flags.parent_tag = 1;
779 qla_free_parent_dma_tag(qla_host_t *ha)
781 if (ha->flags.parent_tag) {
782 bus_dma_tag_destroy(ha->parent_tag);
783 ha->flags.parent_tag = 0;
788 * Name: qla_init_ifnet
789 * Function: Creates the Network Device Interface and Registers it with the O.S
793 qla_init_ifnet(device_t dev, qla_host_t *ha)
797 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
799 ifp = ha->ifp = if_alloc(IFT_ETHER);
802 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
804 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
806 #if __FreeBSD_version >= 1000000
807 if_initbaudrate(ifp, IF_Gbps(10));
808 ifp->if_capabilities = IFCAP_LINKSTATE;
810 ifp->if_mtu = ETHERMTU;
811 ifp->if_baudrate = (1 * 1000 * 1000 *1000);
813 #endif /* #if __FreeBSD_version >= 1000000 */
815 ifp->if_init = qla_init;
817 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
818 ifp->if_ioctl = qla_ioctl;
820 ifp->if_transmit = qla_transmit;
821 ifp->if_qflush = qla_qflush;
823 IFQ_SET_MAXLEN(&ifp->if_snd, qla_get_ifq_snd_maxlen(ha));
824 ifp->if_snd.ifq_drv_maxlen = qla_get_ifq_snd_maxlen(ha);
825 IFQ_SET_READY(&ifp->if_snd);
827 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
829 ether_ifattach(ifp, qla_get_mac_addr(ha));
831 ifp->if_capabilities |= IFCAP_HWCSUM |
834 IFCAP_VLAN_HWTAGGING |
839 ifp->if_capenable = ifp->if_capabilities;
841 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
843 ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
845 ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
847 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
849 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
851 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
857 qla_init_locked(qla_host_t *ha)
859 struct ifnet *ifp = ha->ifp;
863 if (qla_alloc_xmt_bufs(ha) != 0)
866 qla_confirm_9kb_enable(ha);
868 if (qla_alloc_rcv_bufs(ha) != 0)
871 bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
873 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
875 ha->flags.stop_rcv = 0;
876 if (ql_init_hw_if(ha) == 0) {
878 ifp->if_drv_flags |= IFF_DRV_RUNNING;
879 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
880 ha->flags.qla_watchdog_pause = 0;
881 ha->hw_vlan_tx_frames = 0;
882 ha->tx_tso_frames = 0;
883 ha->flags.qla_interface_up = 1;
894 ha = (qla_host_t *)arg;
896 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
898 (void)QLA_LOCK(ha, __func__, 0);
900 QLA_UNLOCK(ha, __func__);
902 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
906 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
908 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
909 struct ifmultiaddr *ifma;
911 struct ifnet *ifp = ha->ifp;
916 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
918 if (ifma->ifma_addr->sa_family != AF_LINK)
921 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
924 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
925 &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
930 if_maddr_runlock(ifp);
932 //if (QLA_LOCK(ha, __func__, 1) == 0) {
933 // ret = ql_hw_set_multi(ha, mta, mcnt, add_multi);
934 // QLA_UNLOCK(ha, __func__);
936 QLA_LOCK(ha, __func__, 1);
937 ret = ql_hw_set_multi(ha, mta, mcnt, add_multi);
938 QLA_UNLOCK(ha, __func__);
944 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
947 struct ifreq *ifr = (struct ifreq *)data;
948 struct ifaddr *ifa = (struct ifaddr *)data;
951 ha = (qla_host_t *)ifp->if_softc;
955 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
958 if (ifa->ifa_addr->sa_family == AF_INET) {
959 ifp->if_flags |= IFF_UP;
960 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
961 (void)QLA_LOCK(ha, __func__, 0);
963 QLA_UNLOCK(ha, __func__);
965 QL_DPRINT4(ha, (ha->pci_dev,
966 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
968 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
970 arp_ifinit(ifp, ifa);
972 ether_ioctl(ifp, cmd, data);
977 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
980 if (ifr->ifr_mtu > QLA_MAX_MTU) {
983 (void) QLA_LOCK(ha, __func__, 0);
984 ifp->if_mtu = ifr->ifr_mtu;
986 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
987 if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
988 ret = ql_set_max_mtu(ha, ha->max_frame_size,
989 ha->hw.rcv_cntxt_id);
992 if (ifp->if_mtu > ETHERMTU)
993 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
995 ha->std_replenish = QL_STD_REPLENISH_THRES;
998 QLA_UNLOCK(ha, __func__);
1007 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1010 (void)QLA_LOCK(ha, __func__, 0);
1012 if (ifp->if_flags & IFF_UP) {
1013 if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1014 if ((ifp->if_flags ^ ha->if_flags) &
1016 ret = ql_set_promisc(ha);
1017 } else if ((ifp->if_flags ^ ha->if_flags) &
1019 ret = ql_set_allmulti(ha);
1022 qla_init_locked(ha);
1023 ha->max_frame_size = ifp->if_mtu +
1024 ETHER_HDR_LEN + ETHER_CRC_LEN;
1025 ret = ql_set_max_mtu(ha, ha->max_frame_size,
1026 ha->hw.rcv_cntxt_id);
1029 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1031 ha->if_flags = ifp->if_flags;
1034 QLA_UNLOCK(ha, __func__);
1038 QL_DPRINT4(ha, (ha->pci_dev,
1039 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1041 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1042 if (qla_set_multi(ha, 1))
1048 QL_DPRINT4(ha, (ha->pci_dev,
1049 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1051 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1052 if (qla_set_multi(ha, 0))
1059 QL_DPRINT4(ha, (ha->pci_dev,
1060 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1062 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1067 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1069 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1072 if (mask & IFCAP_HWCSUM)
1073 ifp->if_capenable ^= IFCAP_HWCSUM;
1074 if (mask & IFCAP_TSO4)
1075 ifp->if_capenable ^= IFCAP_TSO4;
1076 if (mask & IFCAP_VLAN_HWTAGGING)
1077 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1078 if (mask & IFCAP_VLAN_HWTSO)
1079 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1080 if (mask & IFCAP_LRO)
1081 ifp->if_capenable ^= IFCAP_LRO;
1083 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1086 VLAN_CAPABILITIES(ifp);
1091 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1093 ret = ether_ioctl(ifp, cmd, data);
1101 qla_media_change(struct ifnet *ifp)
1104 struct ifmedia *ifm;
1107 ha = (qla_host_t *)ifp->if_softc;
1109 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1113 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1116 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1122 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1126 ha = (qla_host_t *)ifp->if_softc;
1128 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1130 ifmr->ifm_status = IFM_AVALID;
1131 ifmr->ifm_active = IFM_ETHER;
1133 ql_update_link_state(ha);
1134 if (ha->hw.link_up) {
1135 ifmr->ifm_status |= IFM_ACTIVE;
1136 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1139 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1140 (ha->hw.link_up ? "link_up" : "link_down")));
1147 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1150 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1155 struct mbuf *m_head = *m_headp;
1157 QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1159 if (m_head->m_flags & M_FLOWID) {
1160 #ifdef QL_ENABLE_ISCSI_TLV
1161 if (qla_iscsi_pdu(ha, m_head) == 0) {
1163 txr_idx = m_head->m_pkthdr.flowid &
1164 ((ha->hw.num_tx_rings >> 1) - 1);
1166 txr_idx = m_head->m_pkthdr.flowid &
1167 (ha->hw.num_tx_rings - 1);
1170 txr_idx = m_head->m_pkthdr.flowid & (ha->hw.num_tx_rings - 1);
1171 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1175 tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1176 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1178 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1185 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1186 m_head->m_pkthdr.len));
1188 m = m_defrag(m_head, M_NOWAIT);
1190 ha->err_tx_defrag++;
1193 device_printf(ha->pci_dev,
1194 "%s: m_defrag() = NULL [%d]\n",
1201 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1202 segs, &nsegs, BUS_DMA_NOWAIT))) {
1204 ha->err_tx_dmamap_load++;
1206 device_printf(ha->pci_dev,
1207 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1208 __func__, ret, m_head->m_pkthdr.len);
1210 if (ret != ENOMEM) {
1219 ha->err_tx_dmamap_load++;
1221 device_printf(ha->pci_dev,
1222 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1223 __func__, ret, m_head->m_pkthdr.len);
1225 if (ret != ENOMEM) {
1232 QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1234 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1236 if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1238 ha->tx_ring[txr_idx].count++;
1239 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1241 if (ret == EINVAL) {
1248 QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1253 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1255 snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1256 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1258 mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1260 fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1261 M_NOWAIT, &fp->tx_mtx);
1262 if (fp->tx_br == NULL) {
1263 QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1264 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1271 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1274 struct ifnet *ifp = ha->ifp;
1276 if (mtx_initialized(&fp->tx_mtx)) {
1278 if (fp->tx_br != NULL) {
1280 mtx_lock(&fp->tx_mtx);
1282 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1286 mtx_unlock(&fp->tx_mtx);
1288 buf_ring_free(fp->tx_br, M_DEVBUF);
1291 mtx_destroy(&fp->tx_mtx);
1297 qla_fp_taskqueue(void *context, int pending)
1305 uint32_t iscsi_pdu = 0;
1306 uint32_t rx_pkts_left;
1313 ha = (qla_host_t *)fp->ha;
1317 txr_idx = fp->txr_idx;
1319 mtx_lock(&fp->tx_mtx);
1321 if (((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1322 IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1323 mtx_unlock(&fp->tx_mtx);
1324 goto qla_fp_taskqueue_exit;
1327 rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1329 #ifdef QL_ENABLE_ISCSI_TLV
1330 ql_hw_tx_done_locked(ha, fp->txr_idx);
1331 ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1332 txr_idx = txr_idx + (ha->hw.num_tx_rings >> 1);
1334 ql_hw_tx_done_locked(ha, fp->txr_idx);
1335 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1337 mp = drbr_peek(ifp, fp->tx_br);
1339 while (mp != NULL) {
1341 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1342 #ifdef QL_ENABLE_ISCSI_TLV
1343 if (ql_iscsi_pdu(ha, mp) == 0) {
1346 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1349 ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1353 drbr_putback(ifp, fp->tx_br, mp);
1355 drbr_advance(ifp, fp->tx_br);
1358 mtx_unlock(&fp->tx_mtx);
1360 goto qla_fp_taskqueue_exit0;
1362 drbr_advance(ifp, fp->tx_br);
1365 mp = drbr_peek(ifp, fp->tx_br);
1368 mtx_unlock(&fp->tx_mtx);
1370 qla_fp_taskqueue_exit0:
1372 if (rx_pkts_left || ((mp != NULL) && ret)) {
1373 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1375 if (!ha->flags.stop_rcv) {
1376 QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1380 qla_fp_taskqueue_exit:
1382 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1387 qla_create_fp_taskqueues(qla_host_t *ha)
1390 uint8_t tq_name[32];
1392 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1394 qla_tx_fp_t *fp = &ha->tx_fp[i];
1396 bzero(tq_name, sizeof (tq_name));
1397 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1399 TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1401 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1402 taskqueue_thread_enqueue,
1405 if (fp->fp_taskqueue == NULL)
1408 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1411 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1419 qla_destroy_fp_taskqueues(qla_host_t *ha)
1423 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1425 qla_tx_fp_t *fp = &ha->tx_fp[i];
1427 if (fp->fp_taskqueue != NULL) {
1428 taskqueue_drain(fp->fp_taskqueue, &fp->fp_task);
1429 taskqueue_free(fp->fp_taskqueue);
1430 fp->fp_taskqueue = NULL;
1437 qla_drain_fp_taskqueues(qla_host_t *ha)
1441 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1442 qla_tx_fp_t *fp = &ha->tx_fp[i];
1444 if (fp->fp_taskqueue != NULL) {
1445 taskqueue_drain(fp->fp_taskqueue, &fp->fp_task);
1452 qla_transmit(struct ifnet *ifp, struct mbuf *mp)
1454 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1459 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1461 #if __FreeBSD_version >= 1100000
1462 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1464 if (mp->m_flags & M_FLOWID)
1466 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1467 ha->hw.num_sds_rings;
1468 fp = &ha->tx_fp[rss_id];
1470 if (fp->tx_br == NULL) {
1472 goto qla_transmit_exit;
1476 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1479 if (fp->fp_taskqueue != NULL)
1480 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1486 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1491 qla_qflush(struct ifnet *ifp)
1498 ha = (qla_host_t *)ifp->if_softc;
1500 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1502 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1510 mtx_lock(&fp->tx_mtx);
1512 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1515 mtx_unlock(&fp->tx_mtx);
1518 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1524 qla_stop(qla_host_t *ha)
1526 struct ifnet *ifp = ha->ifp;
1532 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
1534 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1542 if (fp->tx_br != NULL) {
1543 mtx_lock(&fp->tx_mtx);
1544 mtx_unlock(&fp->tx_mtx);
1548 ha->flags.qla_watchdog_pause = 1;
1550 while (!ha->qla_watchdog_paused)
1551 qla_mdelay(__func__, 1);
1553 ha->flags.qla_interface_up = 0;
1555 qla_drain_fp_taskqueues(ha);
1561 qla_free_xmt_bufs(ha);
1562 qla_free_rcv_bufs(ha);
1568 * Buffer Management Functions for Transmit and Receive Rings
1571 qla_alloc_xmt_bufs(qla_host_t *ha)
1577 if (bus_dma_tag_create(NULL, /* parent */
1578 1, 0, /* alignment, bounds */
1579 BUS_SPACE_MAXADDR, /* lowaddr */
1580 BUS_SPACE_MAXADDR, /* highaddr */
1581 NULL, NULL, /* filter, filterarg */
1582 QLA_MAX_TSO_FRAME_SIZE, /* maxsize */
1583 QLA_MAX_SEGMENTS, /* nsegments */
1584 PAGE_SIZE, /* maxsegsize */
1585 BUS_DMA_ALLOCNOW, /* flags */
1586 NULL, /* lockfunc */
1587 NULL, /* lockfuncarg */
1589 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1594 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1595 bzero((void *)ha->tx_ring[i].tx_buf,
1596 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1599 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1600 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1602 txb = &ha->tx_ring[j].tx_buf[i];
1604 if ((ret = bus_dmamap_create(ha->tx_tag,
1605 BUS_DMA_NOWAIT, &txb->map))) {
1607 ha->err_tx_dmamap_create++;
1608 device_printf(ha->pci_dev,
1609 "%s: bus_dmamap_create failed[%d]\n",
1612 qla_free_xmt_bufs(ha);
1623 * Release mbuf after it sent on the wire
1626 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1628 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1630 if (txb->m_head && txb->map) {
1632 bus_dmamap_unload(ha->tx_tag, txb->map);
1634 m_freem(txb->m_head);
1639 bus_dmamap_destroy(ha->tx_tag, txb->map);
1641 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1645 qla_free_xmt_bufs(qla_host_t *ha)
1649 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1650 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1651 qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1654 if (ha->tx_tag != NULL) {
1655 bus_dma_tag_destroy(ha->tx_tag);
1659 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1660 bzero((void *)ha->tx_ring[i].tx_buf,
1661 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1668 qla_alloc_rcv_std(qla_host_t *ha)
1670 int i, j, k, r, ret = 0;
1672 qla_rx_ring_t *rx_ring;
1674 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1676 rx_ring = &ha->rx_ring[r];
1678 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1680 rxb = &rx_ring->rx_buf[i];
1682 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1686 device_printf(ha->pci_dev,
1687 "%s: dmamap[%d, %d] failed\n",
1690 for (k = 0; k < r; k++) {
1691 for (j = 0; j < NUM_RX_DESCRIPTORS;
1693 rxb = &ha->rx_ring[k].rx_buf[j];
1694 bus_dmamap_destroy(ha->rx_tag,
1699 for (j = 0; j < i; j++) {
1700 bus_dmamap_destroy(ha->rx_tag,
1701 rx_ring->rx_buf[j].map);
1703 goto qla_alloc_rcv_std_err;
1708 qla_init_hw_rcv_descriptors(ha);
1711 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1713 rx_ring = &ha->rx_ring[r];
1715 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1716 rxb = &rx_ring->rx_buf[i];
1718 if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1720 * set the physical address in the
1721 * corresponding descriptor entry in the
1722 * receive ring/queue for the hba
1724 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1726 (rxb->m_head)->m_pkthdr.len);
1728 device_printf(ha->pci_dev,
1729 "%s: ql_get_mbuf [%d, %d] failed\n",
1731 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1732 goto qla_alloc_rcv_std_err;
1738 qla_alloc_rcv_std_err:
1743 qla_free_rcv_std(qla_host_t *ha)
1748 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1749 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1750 rxb = &ha->rx_ring[r].rx_buf[i];
1751 if (rxb->m_head != NULL) {
1752 bus_dmamap_unload(ha->rx_tag, rxb->map);
1753 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1754 m_freem(rxb->m_head);
1763 qla_alloc_rcv_bufs(qla_host_t *ha)
1767 if (bus_dma_tag_create(NULL, /* parent */
1768 1, 0, /* alignment, bounds */
1769 BUS_SPACE_MAXADDR, /* lowaddr */
1770 BUS_SPACE_MAXADDR, /* highaddr */
1771 NULL, NULL, /* filter, filterarg */
1772 MJUM9BYTES, /* maxsize */
1774 MJUM9BYTES, /* maxsegsize */
1775 BUS_DMA_ALLOCNOW, /* flags */
1776 NULL, /* lockfunc */
1777 NULL, /* lockfuncarg */
1780 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1786 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1788 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1789 ha->hw.sds[i].sdsr_next = 0;
1790 ha->hw.sds[i].rxb_free = NULL;
1791 ha->hw.sds[i].rx_free = 0;
1794 ret = qla_alloc_rcv_std(ha);
1800 qla_free_rcv_bufs(qla_host_t *ha)
1804 qla_free_rcv_std(ha);
1806 if (ha->rx_tag != NULL) {
1807 bus_dma_tag_destroy(ha->rx_tag);
1811 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1813 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1814 ha->hw.sds[i].sdsr_next = 0;
1815 ha->hw.sds[i].rxb_free = NULL;
1816 ha->hw.sds[i].rx_free = 0;
1823 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1825 register struct mbuf *mp = nmp;
1829 bus_dma_segment_t segs[1];
1830 int nsegs, mbuf_size;
1832 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1836 if (ha->hw.enable_9kb)
1837 mbuf_size = MJUM9BYTES;
1839 mbuf_size = MCLBYTES;
1843 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1846 if (ha->hw.enable_9kb)
1847 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
1849 mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1854 device_printf(ha->pci_dev,
1855 "%s: m_getcl failed\n", __func__);
1856 goto exit_ql_get_mbuf;
1858 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1860 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1861 mp->m_data = mp->m_ext.ext_buf;
1865 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1867 offset = 8 - offset;
1872 * Using memory from the mbuf cluster pool, invoke the bus_dma
1873 * machinery to arrange the memory mapping.
1875 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1876 mp, segs, &nsegs, BUS_DMA_NOWAIT);
1877 rxb->paddr = segs[0].ds_addr;
1879 if (ret || !rxb->paddr || (nsegs != 1)) {
1882 device_printf(ha->pci_dev,
1883 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1884 __func__, ret, (long long unsigned int)rxb->paddr,
1887 goto exit_ql_get_mbuf;
1890 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1893 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1899 qla_get_peer(qla_host_t *ha)
1903 int my_slot = pci_get_slot(ha->pci_dev);
1905 if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
1908 for (i = 0; i < count; i++) {
1909 slot = pci_get_slot(peers[i]);
1911 if ((slot >= 0) && (slot == my_slot) &&
1912 (pci_get_device(peers[i]) ==
1913 pci_get_device(ha->pci_dev))) {
1914 if (ha->pci_dev != peers[i])
1915 ha->peer_dev = peers[i];
1921 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
1923 qla_host_t *ha_peer;
1926 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
1928 ha_peer->msg_from_peer = msg_to_peer;
1934 qla_error_recovery(void *context, int pending)
1936 qla_host_t *ha = context;
1937 uint32_t msecs_100 = 100;
1938 struct ifnet *ifp = ha->ifp;
1941 (void)QLA_LOCK(ha, __func__, 0);
1943 if (ha->flags.qla_interface_up) {
1945 ha->hw.imd_compl = 1;
1946 qla_mdelay(__func__, 300);
1950 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
1952 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1960 if (fp->tx_br != NULL) {
1961 mtx_lock(&fp->tx_mtx);
1962 mtx_unlock(&fp->tx_mtx);
1967 QLA_UNLOCK(ha, __func__);
1969 if ((ha->pci_func & 0x1) == 0) {
1971 if (!ha->msg_from_peer) {
1972 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
1974 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
1976 qla_mdelay(__func__, 100);
1979 ha->msg_from_peer = 0;
1981 (void)QLA_LOCK(ha, __func__, 0);
1983 QLA_UNLOCK(ha, __func__);
1985 (void) ql_init_hw(ha);
1987 (void)QLA_LOCK(ha, __func__, 0);
1988 if (ha->flags.qla_interface_up) {
1989 qla_free_xmt_bufs(ha);
1990 qla_free_rcv_bufs(ha);
1992 QLA_UNLOCK(ha, __func__);
1994 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
1997 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
1999 ha->msg_from_peer = 0;
2001 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2003 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2006 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) && msecs_100--)
2007 qla_mdelay(__func__, 100);
2008 ha->msg_from_peer = 0;
2010 (void) ql_init_hw(ha);
2012 (void)QLA_LOCK(ha, __func__, 0);
2013 if (ha->flags.qla_interface_up) {
2014 qla_free_xmt_bufs(ha);
2015 qla_free_rcv_bufs(ha);
2017 QLA_UNLOCK(ha, __func__);
2020 (void)QLA_LOCK(ha, __func__, 0);
2022 if (ha->flags.qla_interface_up) {
2023 if (qla_alloc_xmt_bufs(ha) != 0) {
2024 QLA_UNLOCK(ha, __func__);
2027 qla_confirm_9kb_enable(ha);
2029 if (qla_alloc_rcv_bufs(ha) != 0) {
2030 QLA_UNLOCK(ha, __func__);
2034 ha->flags.stop_rcv = 0;
2035 if (ql_init_hw_if(ha) == 0) {
2037 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2038 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2039 ha->flags.qla_watchdog_pause = 0;
2042 ha->flags.qla_watchdog_pause = 0;
2044 QLA_UNLOCK(ha, __func__);
2048 qla_async_event(void *context, int pending)
2050 qla_host_t *ha = context;
2052 (void)QLA_LOCK(ha, __func__, 0);
2053 qla_hw_async_event(ha);
2054 QLA_UNLOCK(ha, __func__);