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 ifp->if_baudrate = IF_Gbps(10);
842 ifp->if_capabilities = IFCAP_LINKSTATE;
843 ifp->if_mtu = ETHERMTU;
845 ifp->if_init = qla_init;
847 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
848 ifp->if_ioctl = qla_ioctl;
850 ifp->if_transmit = qla_transmit;
851 ifp->if_qflush = qla_qflush;
853 IFQ_SET_MAXLEN(&ifp->if_snd, qla_get_ifq_snd_maxlen(ha));
854 ifp->if_snd.ifq_drv_maxlen = qla_get_ifq_snd_maxlen(ha);
855 IFQ_SET_READY(&ifp->if_snd);
857 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
859 ether_ifattach(ifp, qla_get_mac_addr(ha));
861 ifp->if_capabilities |= IFCAP_HWCSUM |
864 IFCAP_VLAN_HWTAGGING |
869 ifp->if_capenable = ifp->if_capabilities;
871 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
873 ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
875 ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
877 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
879 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
881 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
887 qla_init_locked(qla_host_t *ha)
889 struct ifnet *ifp = ha->ifp;
893 if (qla_alloc_xmt_bufs(ha) != 0)
896 qla_confirm_9kb_enable(ha);
898 if (qla_alloc_rcv_bufs(ha) != 0)
901 bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
903 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
904 ifp->if_hwassist |= CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
907 if (ql_init_hw_if(ha) == 0) {
909 ifp->if_drv_flags |= IFF_DRV_RUNNING;
910 ha->qla_watchdog_pause = 0;
911 ha->hw_vlan_tx_frames = 0;
912 ha->tx_tso_frames = 0;
913 ha->qla_interface_up = 1;
914 ql_update_link_state(ha);
925 ha = (qla_host_t *)arg;
927 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
929 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
934 QLA_UNLOCK(ha, __func__);
936 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
940 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
942 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
943 struct ifmultiaddr *ifma;
945 struct ifnet *ifp = ha->ifp;
950 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
952 if (ifma->ifma_addr->sa_family != AF_LINK)
955 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
958 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
959 &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
964 if_maddr_runlock(ifp);
966 if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
967 QLA_LOCK_NO_SLEEP) != 0)
970 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
971 ret = ql_hw_set_multi(ha, mta, mcnt, add_multi);
974 QLA_UNLOCK(ha, __func__);
980 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
983 struct ifreq *ifr = (struct ifreq *)data;
984 struct ifaddr *ifa = (struct ifaddr *)data;
987 ha = (qla_host_t *)ifp->if_softc;
991 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
994 if (ifa->ifa_addr->sa_family == AF_INET) {
996 ret = QLA_LOCK(ha, __func__,
997 QLA_LOCK_DEFAULT_MS_TIMEOUT,
1002 ifp->if_flags |= IFF_UP;
1004 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1005 qla_init_locked(ha);
1008 QLA_UNLOCK(ha, __func__);
1009 QL_DPRINT4(ha, (ha->pci_dev,
1010 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1012 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1014 arp_ifinit(ifp, ifa);
1016 ether_ioctl(ifp, cmd, data);
1021 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1024 if (ifr->ifr_mtu > QLA_MAX_MTU) {
1027 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1033 ifp->if_mtu = ifr->ifr_mtu;
1034 ha->max_frame_size =
1035 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1037 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1038 qla_init_locked(ha);
1041 if (ifp->if_mtu > ETHERMTU)
1042 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1044 ha->std_replenish = QL_STD_REPLENISH_THRES;
1047 QLA_UNLOCK(ha, __func__);
1053 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1056 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1062 if (ifp->if_flags & IFF_UP) {
1064 ha->max_frame_size = ifp->if_mtu +
1065 ETHER_HDR_LEN + ETHER_CRC_LEN;
1066 qla_init_locked(ha);
1068 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1069 if ((ifp->if_flags ^ ha->if_flags) &
1071 ret = ql_set_promisc(ha);
1072 } else if ((ifp->if_flags ^ ha->if_flags) &
1074 ret = ql_set_allmulti(ha);
1078 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1080 ha->if_flags = ifp->if_flags;
1083 QLA_UNLOCK(ha, __func__);
1087 QL_DPRINT4(ha, (ha->pci_dev,
1088 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1090 if (qla_set_multi(ha, 1))
1095 QL_DPRINT4(ha, (ha->pci_dev,
1096 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1098 if (qla_set_multi(ha, 0))
1104 QL_DPRINT4(ha, (ha->pci_dev,
1105 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1107 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1112 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1114 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1117 if (mask & IFCAP_HWCSUM)
1118 ifp->if_capenable ^= IFCAP_HWCSUM;
1119 if (mask & IFCAP_TSO4)
1120 ifp->if_capenable ^= IFCAP_TSO4;
1121 if (mask & IFCAP_TSO6)
1122 ifp->if_capenable ^= IFCAP_TSO6;
1123 if (mask & IFCAP_VLAN_HWTAGGING)
1124 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1125 if (mask & IFCAP_VLAN_HWTSO)
1126 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1127 if (mask & IFCAP_LRO)
1128 ifp->if_capenable ^= IFCAP_LRO;
1130 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1131 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1137 qla_init_locked(ha);
1139 QLA_UNLOCK(ha, __func__);
1142 VLAN_CAPABILITIES(ifp);
1147 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1149 ret = ether_ioctl(ifp, cmd, data);
1157 qla_media_change(struct ifnet *ifp)
1160 struct ifmedia *ifm;
1163 ha = (qla_host_t *)ifp->if_softc;
1165 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1169 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1172 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1178 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1182 ha = (qla_host_t *)ifp->if_softc;
1184 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1186 ifmr->ifm_status = IFM_AVALID;
1187 ifmr->ifm_active = IFM_ETHER;
1189 ql_update_link_state(ha);
1190 if (ha->hw.link_up) {
1191 ifmr->ifm_status |= IFM_ACTIVE;
1192 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1195 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1196 (ha->hw.link_up ? "link_up" : "link_down")));
1203 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1206 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1211 struct mbuf *m_head = *m_headp;
1213 QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1215 tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1216 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1218 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1225 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1226 m_head->m_pkthdr.len));
1228 m = m_defrag(m_head, M_NOWAIT);
1230 ha->err_tx_defrag++;
1233 device_printf(ha->pci_dev,
1234 "%s: m_defrag() = NULL [%d]\n",
1241 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1242 segs, &nsegs, BUS_DMA_NOWAIT))) {
1244 ha->err_tx_dmamap_load++;
1246 device_printf(ha->pci_dev,
1247 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1248 __func__, ret, m_head->m_pkthdr.len);
1250 if (ret != ENOMEM) {
1259 ha->err_tx_dmamap_load++;
1261 device_printf(ha->pci_dev,
1262 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1263 __func__, ret, m_head->m_pkthdr.len);
1265 if (ret != ENOMEM) {
1272 QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1274 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1276 if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1278 ha->tx_ring[txr_idx].count++;
1280 ha->tx_ring[txr_idx].iscsi_pkt_count++;
1281 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1283 if (ret == EINVAL) {
1290 QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1295 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1297 snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1298 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1300 mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1302 fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1303 M_NOWAIT, &fp->tx_mtx);
1304 if (fp->tx_br == NULL) {
1305 QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1306 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1313 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1316 struct ifnet *ifp = ha->ifp;
1318 if (mtx_initialized(&fp->tx_mtx)) {
1320 if (fp->tx_br != NULL) {
1322 mtx_lock(&fp->tx_mtx);
1324 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1328 mtx_unlock(&fp->tx_mtx);
1330 buf_ring_free(fp->tx_br, M_DEVBUF);
1333 mtx_destroy(&fp->tx_mtx);
1339 qla_fp_taskqueue(void *context, int pending)
1347 uint32_t iscsi_pdu = 0;
1348 uint32_t rx_pkts_left = -1;
1355 ha = (qla_host_t *)fp->ha;
1359 txr_idx = fp->txr_idx;
1361 mtx_lock(&fp->tx_mtx);
1363 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1364 mtx_unlock(&fp->tx_mtx);
1365 goto qla_fp_taskqueue_exit;
1368 while (rx_pkts_left && !ha->stop_rcv) {
1369 rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1371 #ifdef QL_ENABLE_ISCSI_TLV
1372 ql_hw_tx_done_locked(ha, fp->txr_idx);
1373 ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1375 ql_hw_tx_done_locked(ha, fp->txr_idx);
1376 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1378 mp = drbr_peek(ifp, fp->tx_br);
1380 while (mp != NULL) {
1382 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1383 #ifdef QL_ENABLE_ISCSI_TLV
1384 if (ql_iscsi_pdu(ha, mp) == 0) {
1386 (ha->hw.num_tx_rings >> 1);
1390 txr_idx = fp->txr_idx;
1392 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1395 ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1399 drbr_putback(ifp, fp->tx_br, mp);
1401 drbr_advance(ifp, fp->tx_br);
1404 mtx_unlock(&fp->tx_mtx);
1406 goto qla_fp_taskqueue_exit0;
1408 drbr_advance(ifp, fp->tx_br);
1411 mp = drbr_peek(ifp, fp->tx_br);
1414 mtx_unlock(&fp->tx_mtx);
1416 qla_fp_taskqueue_exit0:
1418 if (rx_pkts_left || ((mp != NULL) && ret)) {
1419 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1421 if (!ha->stop_rcv) {
1422 QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1426 qla_fp_taskqueue_exit:
1428 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1433 qla_create_fp_taskqueues(qla_host_t *ha)
1436 uint8_t tq_name[32];
1438 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1440 qla_tx_fp_t *fp = &ha->tx_fp[i];
1442 bzero(tq_name, sizeof (tq_name));
1443 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1445 TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1447 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1448 taskqueue_thread_enqueue,
1451 if (fp->fp_taskqueue == NULL)
1454 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1457 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1465 qla_destroy_fp_taskqueues(qla_host_t *ha)
1469 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1471 qla_tx_fp_t *fp = &ha->tx_fp[i];
1473 if (fp->fp_taskqueue != NULL) {
1474 taskqueue_drain(fp->fp_taskqueue, &fp->fp_task);
1475 taskqueue_free(fp->fp_taskqueue);
1476 fp->fp_taskqueue = NULL;
1483 qla_drain_fp_taskqueues(qla_host_t *ha)
1487 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1488 qla_tx_fp_t *fp = &ha->tx_fp[i];
1490 if (fp->fp_taskqueue != NULL) {
1491 taskqueue_drain(fp->fp_taskqueue, &fp->fp_task);
1498 qla_transmit(struct ifnet *ifp, struct mbuf *mp)
1500 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1505 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1507 #if __FreeBSD_version >= 1100000
1508 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1510 if (mp->m_flags & M_FLOWID)
1512 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1513 ha->hw.num_sds_rings;
1514 fp = &ha->tx_fp[rss_id];
1516 if (fp->tx_br == NULL) {
1518 goto qla_transmit_exit;
1522 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1525 if (fp->fp_taskqueue != NULL)
1526 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1532 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1537 qla_qflush(struct ifnet *ifp)
1544 ha = (qla_host_t *)ifp->if_softc;
1546 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1548 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1556 mtx_lock(&fp->tx_mtx);
1558 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1561 mtx_unlock(&fp->tx_mtx);
1564 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1570 qla_stop(qla_host_t *ha)
1572 struct ifnet *ifp = ha->ifp;
1578 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1579 ha->qla_watchdog_pause = 1;
1581 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1589 if (fp->tx_br != NULL) {
1590 mtx_lock(&fp->tx_mtx);
1591 mtx_unlock(&fp->tx_mtx);
1595 while (!ha->qla_watchdog_paused)
1596 qla_mdelay(__func__, 1);
1598 ha->qla_interface_up = 0;
1600 qla_drain_fp_taskqueues(ha);
1604 qla_free_xmt_bufs(ha);
1605 qla_free_rcv_bufs(ha);
1611 * Buffer Management Functions for Transmit and Receive Rings
1614 qla_alloc_xmt_bufs(qla_host_t *ha)
1620 if (bus_dma_tag_create(NULL, /* parent */
1621 1, 0, /* alignment, bounds */
1622 BUS_SPACE_MAXADDR, /* lowaddr */
1623 BUS_SPACE_MAXADDR, /* highaddr */
1624 NULL, NULL, /* filter, filterarg */
1625 QLA_MAX_TSO_FRAME_SIZE, /* maxsize */
1626 QLA_MAX_SEGMENTS, /* nsegments */
1627 PAGE_SIZE, /* maxsegsize */
1628 BUS_DMA_ALLOCNOW, /* flags */
1629 NULL, /* lockfunc */
1630 NULL, /* lockfuncarg */
1632 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1637 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1638 bzero((void *)ha->tx_ring[i].tx_buf,
1639 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1642 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1643 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1645 txb = &ha->tx_ring[j].tx_buf[i];
1647 if ((ret = bus_dmamap_create(ha->tx_tag,
1648 BUS_DMA_NOWAIT, &txb->map))) {
1650 ha->err_tx_dmamap_create++;
1651 device_printf(ha->pci_dev,
1652 "%s: bus_dmamap_create failed[%d]\n",
1655 qla_free_xmt_bufs(ha);
1666 * Release mbuf after it sent on the wire
1669 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1671 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1673 if (txb->m_head && txb->map) {
1675 bus_dmamap_unload(ha->tx_tag, txb->map);
1677 m_freem(txb->m_head);
1682 bus_dmamap_destroy(ha->tx_tag, txb->map);
1684 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1688 qla_free_xmt_bufs(qla_host_t *ha)
1692 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1693 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1694 qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1697 if (ha->tx_tag != NULL) {
1698 bus_dma_tag_destroy(ha->tx_tag);
1702 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1703 bzero((void *)ha->tx_ring[i].tx_buf,
1704 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1711 qla_alloc_rcv_std(qla_host_t *ha)
1713 int i, j, k, r, ret = 0;
1715 qla_rx_ring_t *rx_ring;
1717 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1719 rx_ring = &ha->rx_ring[r];
1721 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1723 rxb = &rx_ring->rx_buf[i];
1725 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1729 device_printf(ha->pci_dev,
1730 "%s: dmamap[%d, %d] failed\n",
1733 for (k = 0; k < r; k++) {
1734 for (j = 0; j < NUM_RX_DESCRIPTORS;
1736 rxb = &ha->rx_ring[k].rx_buf[j];
1737 bus_dmamap_destroy(ha->rx_tag,
1742 for (j = 0; j < i; j++) {
1743 bus_dmamap_destroy(ha->rx_tag,
1744 rx_ring->rx_buf[j].map);
1746 goto qla_alloc_rcv_std_err;
1751 qla_init_hw_rcv_descriptors(ha);
1754 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1756 rx_ring = &ha->rx_ring[r];
1758 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1759 rxb = &rx_ring->rx_buf[i];
1761 if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1763 * set the physical address in the
1764 * corresponding descriptor entry in the
1765 * receive ring/queue for the hba
1767 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1769 (rxb->m_head)->m_pkthdr.len);
1771 device_printf(ha->pci_dev,
1772 "%s: ql_get_mbuf [%d, %d] failed\n",
1774 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1775 goto qla_alloc_rcv_std_err;
1781 qla_alloc_rcv_std_err:
1786 qla_free_rcv_std(qla_host_t *ha)
1791 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1792 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1793 rxb = &ha->rx_ring[r].rx_buf[i];
1794 if (rxb->m_head != NULL) {
1795 bus_dmamap_unload(ha->rx_tag, rxb->map);
1796 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1797 m_freem(rxb->m_head);
1806 qla_alloc_rcv_bufs(qla_host_t *ha)
1810 if (bus_dma_tag_create(NULL, /* parent */
1811 1, 0, /* alignment, bounds */
1812 BUS_SPACE_MAXADDR, /* lowaddr */
1813 BUS_SPACE_MAXADDR, /* highaddr */
1814 NULL, NULL, /* filter, filterarg */
1815 MJUM9BYTES, /* maxsize */
1817 MJUM9BYTES, /* maxsegsize */
1818 BUS_DMA_ALLOCNOW, /* flags */
1819 NULL, /* lockfunc */
1820 NULL, /* lockfuncarg */
1823 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1829 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1831 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1832 ha->hw.sds[i].sdsr_next = 0;
1833 ha->hw.sds[i].rxb_free = NULL;
1834 ha->hw.sds[i].rx_free = 0;
1837 ret = qla_alloc_rcv_std(ha);
1843 qla_free_rcv_bufs(qla_host_t *ha)
1847 qla_free_rcv_std(ha);
1849 if (ha->rx_tag != NULL) {
1850 bus_dma_tag_destroy(ha->rx_tag);
1854 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1856 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1857 ha->hw.sds[i].sdsr_next = 0;
1858 ha->hw.sds[i].rxb_free = NULL;
1859 ha->hw.sds[i].rx_free = 0;
1866 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1868 register struct mbuf *mp = nmp;
1872 bus_dma_segment_t segs[1];
1873 int nsegs, mbuf_size;
1875 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1879 if (ha->hw.enable_9kb)
1880 mbuf_size = MJUM9BYTES;
1882 mbuf_size = MCLBYTES;
1886 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1889 if (ha->hw.enable_9kb)
1890 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
1892 mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1897 device_printf(ha->pci_dev,
1898 "%s: m_getcl failed\n", __func__);
1899 goto exit_ql_get_mbuf;
1901 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1903 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1904 mp->m_data = mp->m_ext.ext_buf;
1908 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1910 offset = 8 - offset;
1915 * Using memory from the mbuf cluster pool, invoke the bus_dma
1916 * machinery to arrange the memory mapping.
1918 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1919 mp, segs, &nsegs, BUS_DMA_NOWAIT);
1920 rxb->paddr = segs[0].ds_addr;
1922 if (ret || !rxb->paddr || (nsegs != 1)) {
1925 device_printf(ha->pci_dev,
1926 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1927 __func__, ret, (long long unsigned int)rxb->paddr,
1930 goto exit_ql_get_mbuf;
1933 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1936 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1942 qla_get_peer(qla_host_t *ha)
1946 int my_slot = pci_get_slot(ha->pci_dev);
1948 if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
1951 for (i = 0; i < count; i++) {
1952 slot = pci_get_slot(peers[i]);
1954 if ((slot >= 0) && (slot == my_slot) &&
1955 (pci_get_device(peers[i]) ==
1956 pci_get_device(ha->pci_dev))) {
1957 if (ha->pci_dev != peers[i])
1958 ha->peer_dev = peers[i];
1964 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
1966 qla_host_t *ha_peer;
1969 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
1971 ha_peer->msg_from_peer = msg_to_peer;
1977 qla_error_recovery(void *context, int pending)
1979 qla_host_t *ha = context;
1980 uint32_t msecs_100 = 100;
1981 struct ifnet *ifp = ha->ifp;
1984 device_printf(ha->pci_dev, "%s: \n", __func__);
1985 ha->hw.imd_compl = 1;
1987 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
1990 device_printf(ha->pci_dev, "%s: enter\n", __func__);
1992 if (ha->qla_interface_up) {
1994 qla_mdelay(__func__, 300);
1996 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1998 for (i = 0; i < ha->hw.num_sds_rings; i++) {
2006 if (fp->tx_br != NULL) {
2007 mtx_lock(&fp->tx_mtx);
2008 mtx_unlock(&fp->tx_mtx);
2014 qla_drain_fp_taskqueues(ha);
2016 if ((ha->pci_func & 0x1) == 0) {
2018 if (!ha->msg_from_peer) {
2019 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2021 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2023 qla_mdelay(__func__, 100);
2026 ha->msg_from_peer = 0;
2030 (void) ql_init_hw(ha);
2032 if (ha->qla_interface_up) {
2033 qla_free_xmt_bufs(ha);
2034 qla_free_rcv_bufs(ha);
2037 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2040 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2042 ha->msg_from_peer = 0;
2044 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2046 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2049 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) && msecs_100--)
2050 qla_mdelay(__func__, 100);
2051 ha->msg_from_peer = 0;
2053 (void) ql_init_hw(ha);
2055 qla_mdelay(__func__, 1000);
2057 if (ha->qla_interface_up) {
2058 qla_free_xmt_bufs(ha);
2059 qla_free_rcv_bufs(ha);
2063 if (ha->qla_interface_up) {
2065 if (qla_alloc_xmt_bufs(ha) != 0) {
2066 goto qla_error_recovery_exit;
2068 qla_confirm_9kb_enable(ha);
2070 if (qla_alloc_rcv_bufs(ha) != 0) {
2071 goto qla_error_recovery_exit;
2076 if (ql_init_hw_if(ha) == 0) {
2078 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2079 ha->qla_watchdog_pause = 0;
2082 ha->qla_watchdog_pause = 0;
2084 qla_error_recovery_exit:
2086 device_printf(ha->pci_dev, "%s: exit\n", __func__);
2088 QLA_UNLOCK(ha, __func__);
2090 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2096 qla_async_event(void *context, int pending)
2098 qla_host_t *ha = context;
2100 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2103 if (ha->async_event) {
2104 ha->async_event = 0;
2105 qla_hw_async_event(ha);
2108 QLA_UNLOCK(ha, __func__);
2114 qla_stats(void *context, int pending)